46
Introduction to Perl

Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Embed Size (px)

Citation preview

Page 1: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Introduction to Perl

Page 2: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

What is PerlPerl is an interpreted language. This means

you run it through an interpreter, not a compiler.

Similar to shell script but lot easier and more powerful.

Perl is free to download and is also available for Windows and Macintosh.

File name extension .pl

The first line of a perl program should tell where to find the perl intepreter

#!/usr/bin/perl

Page 3: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Steps to Run a Perl ProgramUse your favorite editor to create a Perl

program, say test.pl.Change the file permission and make it

executable.chmod 700 test.pl

Run Perl programtest.pl

Page 4: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

CommentsThe pound sign "#" is the symbol for

comment entry.Exception: First line, #!/usr/bin/perl, tells

where to find the Perl compiler on your system

Page 5: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

VariablesThree types of variables.

ScalarArrayHash

Page 6: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Scalars

Scalar means single valueIn C/C++, many different kinds of single

values:int, float, double, char, bool

In Perl, Scalar variable can hold all these types, and more, such as string.

In Perl, we do not need to declare the data type of variables

Page 7: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

ScalarsAll Scalar variables begin with a $Examples: $foo, $a, $zebra1, $F87

Page 8: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Scalar AssignmentsScalars hold any data type:$foo = 3;$d = 4.43;$temp = ‘Z’; #could be double quote$My_String = “Hello, I’m Paul.” # could be

single quote$value = TRUE;

Page 9: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Arithmetic in Perl

9

$a = 1 + 2; # Addition$b = 3 - 4; # Subtraction$c = $a * $b; # Multiplication$d = $a / $b; # Division$a = 9 ** 10; # Exponentiation$b = 5 % 2; # Modulo

Page 10: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Single and double quotes

10

$a = 'apples'; # you can also use double quote $b = 'bananas'; # you can also use double

quote

print '$a and $b';display: $a and $bprint "$a and $b";display: apples and bananas

Single quotation marks do not interpret, and double quotation marks do

Page 11: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

ArraysConcept is the same as in C/C++

Groups of other valuesmuch more dynamic than C/C++

no declaration of size, typecan hold any kinds of value, and multiple

kinds of valuesAll array variables start with the @

character@array, @foo, @My_Array, @temp34

Array index stars from 0

Page 12: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Array Operations@a1 = (3, 2, 1, 4);@a2 = (34, ‘z’, “Hi”, 43.2);Assignment

$a2[2] = ‘X’; #@a2: (34, ‘z’, ‘X’, 43.2)Copy array

@a3 = @a1; #@a3: (3, 2, 1, 4);Merge array:

@a5 = (@a1, @a2); #@a5: (3, 2, 1, 4, 34, ‘z’, “Hi”, 43.2)

How about this operation:@a1 = (@a1, @a2); #@a1: (3, 2, 1, 4, 34, ‘z’,

“Hi”, 43.2)

Page 13: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Sort Array@a1 = (3, 2, 1, 4);Sort array

@a4 = sort (@a1) #@a4: (1, 2, 3, 4)How about this one?

@a1 = (3, 22, 11, 4);@a4 = sort (@a1) #@a4: (11, 22, 3, 4)This is same as

@a4 = sort {$a cmp $b }(@a1);Array is sorted alphabetically (elements are

considered as string)

Page 14: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Sort Array (continued)Sorted alphabetically

@a1 = (3, 22, 11, 4);@a4 = sort (@a1) ;

#or @a4 = sort {$a cmp $b }(@a1);#@a4: (11, 22, 3, 4)

Sort numerically@a1 = (3, 22, 111, 4);

@a4 = sort {$a <=> $b }(@a1); #@a4: (3, 4, 11, 22)

Note:Inside sort block, two variables must be $a and

$bIf $a and $b is exchanged, the sorting order is

changed

Page 15: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

More about arraysspecial variable for each array:

@foo = (3, 25, 43, 31);$#foo (a variable: last index of @foo,

which is 3).$#foo+1 (size of array @foo, which is 4).

Page 16: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Program Flow: if statementif ($x == $y) { #...}elsif ($x == ($y+1)){ #...}else { #...}

Page 17: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Program Flow: Comparing variables

Numbers==!=<>

Stringseqneltgt

Page 18: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Program Flow: Logical operators

&& (logical and) || (or) ! (negation)

Page 19: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Program Flow (Loops)

for ($t = 0; $t < 100; $t++){ #...}

while ($x == $y) { #...}

Page 20: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

foreach StatementThis statement takes an array variable

and assigns one item at a time to a scalar variable, executing a block of code.

For example, @list an arrayforeach $var (@list){#

}

Page 21: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Basic IOOutput to terminal

The print statement.Example:print “My name is $name\n”;

Input from keyboardThe <> operatorExample:$input = <>;read one line from keyboard, and save in variable

$input

Page 22: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Task 1Write a Perl program to ask the user to enter a

name, then it will display

Hello name_user_rntered

Page 23: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Perl Program for Task 1

#!/usr/bin/perl

print "Enter your name:";

$name = <>;print "Hello $name\n";

Page 24: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

ChompWhen reading in, carriage return (“\n”) is

included.Usually don’t want that. chomp will take off the last character of a

string, if it is a “\n”.chomp ($foo);

Page 25: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Perl Program for Task 1 (revised)

#!/usr/bin/perl

print "Enter your name:";

$name = <>;chomp($name);print "Hello $name\n";

Page 26: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Read / Write to FilesTo read and write to files we should create

something called handles which refer to the files.

Page 27: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Read from FilesTo create a file handle for reading

Use the OPEN commandExample

open(filehandle1,"filename1");

Page 28: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Read from Files (continued)Once the file handles have been

obtained, we can read data (line by line) from the file.

Example: @lines = <filehandle1>; This will result in each line being read from the file pointed by the file handle and all lines are stored in the array variable @lines, where index 0 ($lines[0]) contains first line of the file, index 1 ($lines[1]) contains second line of the file, and so on.

Page 29: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Read from Files (continued)After read file, we should close the

file.close(filehandle1);

Page 30: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Task 2Write a Perl program that can read a file

(test.cpp) and display each line with a line number

Page 31: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Perl program for Task 2#!/usr/bin/perl

open(fh1, "test.cpp");@input_lines=<fh1>;chomp(@input_lines);close(fh1);

$i=1;foreach $line(@input_lines){

print "$i: $line\n";$i=$i+1;

}

Page 32: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Write to FilesTo create a file handle for writing

Use the OPEN commandExample

open(filehandle2, ">filename2");

Page 33: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Write to Files (continued)Once the file handles have been

obtained, we can write data to the file.

Example: print filehandle2 "$linevalue";

This will result in the value of $linevalue being written to the file pointed by the filehandle2.

Page 34: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Write to Files (continued)After write to file, we should close

the file.close(filehandle2);

Page 35: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Task 3Rewrite the Perl program for Task 2 so that

the result will be write to a file (test_c.cpp) instead of displaying on the screen.

Page 36: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Perl program for Task 3#!/usr/bin/perl

open(fh1, "test.cpp");@input_lines=<fh1>;chomp(@input_lines);close(fh1);

$i=1;open(fh2, ">test_c.cpp");foreach $line(@input_lines){

print fh2 "$i: $line\n";$i=$i+1;

}close(fh2);

Page 37: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Subroutines (functions)To define your own subroutine, use the

keyword sub

Can be defined anywhere in your program

sub function_name{

#commands}

Page 38: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Function Calls$Name = getname(); #return a valuegetname(); #not returning

a value

Page 39: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Parameters of FunctionsParameters are passed in a function as an

array.The parameter is taken in as an array which

is denoted by @_ inside the function. So if you pass only one parameter the size of

array @_ will be one. If you pass two parameters then the @_ size will be two and the two parameters can be accessed by $_[0],$_[1] , and so on.

Page 40: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Subroutines#!/usr/bin/perl

$result = max(11, 12);Print “The largest number is: $result \n”;

sub max {if($_[0] > $_[1]) {

return $_[0];}else {

return $_[1]; }}

Output:The largest number is: 12

Page 41: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

More About FunctionsThe variables declared in the main

program are by default global so they will continue to have their values in the function also.

Local variables are declared by putting my key word while declaring the variable.

Page 42: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Subroutines: local variable example#!/usr/bin/perl

sub max

{

my @num = @_

if($num[0] > $num[1]){

return $num[0];

}

else{

return $num[1];

}

}

$result = max(11, 12);Print “The largest number is: $result \n”;

Page 43: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

A routine (user defined) to read web pagessub getweb{ my $url = $_[0];

require LWP::UserAgent;my $ua = LWP::UserAgent->new;$ua->timeout(10);$ua->env_proxy;my $response = $ua->get($url);

return $response->content; }

This routine takes one parameter (a web address) and returns the contents of a web page as one string

Page 44: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Task 4Display the html code of www.google.com

Page 45: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Perl program for task 4#!/usr/bin/perl

$google = getweb("http://www.google.com");print $google; #the entire page is saved as

one string

sub getweb{

my $url = $_[0];require LWP::UserAgent;my $ua = LWP::UserAgent->new;$ua->timeout(10);$ua->env_proxy;my $response = $ua->get($url);

return $response->content; }

Page 46: Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script

Reading AssignmentTextbook: Chapter 11