Advanced Web Design Unit 8 The Basics of Perl Eduoncloud.com Page 1
Origins and Uses of Perl Perl was developed in 1987 by Larry Wall. What is perl Practical Extraction and Report Language Goal: to expand on the text-processing features of awk. Why perl: Interpreted Language Optimized for String Manipulation and File I/O Full support for Regular Expressions. Widely used for CGI scripting Freely available Basic Syntax Statements end with semicolon ; Comments start with # Only single line comments Variables You don t have to declare a variable before you access it You don't have to declare a variable's type Eduoncloud.com Page 2
What are the benefits of perl? What are the basic datatypes in perl. --10 What are the three categories of perl variables? Give example for each. --06 Benefits of perl support for communications using sockets support for object-oriented programming powerful text pattern-matching capabilities Portability: Perl code that doesn't use system specific features can be run on any platform Basic Data-types. Three categories of variables in Perl: 1. scalar variables - begin with $. - Scalar variables can store numbers character strings references (addresses) 2. vector (Array) variables - begin with @. @mylist = (1, 2, 3); @list_of_names = ('Larry', 'Curly', 'Moe'); 3. hash (Associative Array) variables - begins with %. %myhash = ( "name" => "Larry", "phone" => "111-111-1111" ); Eduoncloud.com Page 3
how can a perl variable acts as string and number? --06 Perl is a weakly typed language, meaning that a "variable" could hold many different forms of data. perl variable as number Perl uses double-precision floating point values for calculation. Example: $x =10.7 #floating-point values $a =1000 #integer values Perl also accepts string literal as a number for example: $s = "2000"; # similar to $s = 2000; perl variable as string Perl defines string as a sequence of characters. $str = "this is a string in Perl". Constants: Numeric Literals All numeric values are represented internally as doubleprecision floating point values. Literal numbers are integers or floating point values. Integers are strings of digits Integers can be written in hexadecimal by preceding them with 0x Floating point values can have decimal points and/or exponents: 12345 # integer -54321 # negative integer 12345.67 # floating point Eduoncloud.com Page 4
6.02E23 # scientific notation 0xffff # hexadecimal 0377 # octal String Literals Quoting Strings 1. Strings enclosed in single quotes : Everything is interpreted literally Can t include: escape sequences such as \n. Include: a single quote \'. single quotes can be replaced by other delimiter using "q" q$i don't want to go, I can't go, I won't go!$ q<i don't want to go, I can't go, I won't go!> 2. Double-quoted string can include special characters (such as \n). A double quote can be embedded by preceding it with a backslash "\"Aha!\", he said." A different delimited can be used proceeding with qq. qq@"why, I never!", said she@ A null string is written as ' ' or " ". 3. With ` (backtick) The text is executed as a separate process, and the output of the command is returned as the value of the string Eduoncloud.com Page 5
Quote and Quote-like Operators Customary Generic Meaning Interpolates '' q Literal no "" qq Literal yes `` qx Command yes* qw Word list no // m Pattern match yes* qr Pattern yes* s Substitution yes* tr Transliteration no y Transliteration no <<EOF here-doc yes* * unless the delimiter is ''. Scalar Variables: Rules The names of scalar variable all begin with $ and then continue with a letter, followed by letters, digits and/or underscores. Variable names are case sensitive, so $FRIZZY, $Frizzy and $frizzy are three different names. Convention: programmer names in Perl should not use uppercase. Interpolation: If variables names are included in double- quoted string, the variable names are Interpolated ( replaced by their values). Example Eduoncloud.com Page 6
$age = 47; $mystring = "Jack is $age years old "; $mystring has the value: "Jack is 47 years old" Declaring Scalar Variables rarely declared explicitly; done implicitly when a value is assigned to the variable. Scalar variables that have not been assigned a value by the program that the value undef, with a numerical value of 0 and a string value of "". Implicit Variables Perl has several predefined, or implicit, variables, whose names begin with $. $_ Set in many situations such as reading from a file or in a foreach loop. $0 Name of the file currently being executed. $] Version of Perl being used. @_ @ARGV Contains the parameters passed to a subroutine. Contains the command line arguments passed to the program. Eduoncloud.com Page 7
Numeric Operators Operator Associativity ++, -- Nonassociative Unary +, - * Right to left ** (exponentiation) Right to left *, /, % Left to right Binary +, - Left to right numeric operations are performed in double-precision floating point. Operator Operation string concatenation x string repetition = concatenation and assignment Concatenation is performed using the operator: Example $first = "Freddy" $first " Freeloader" Result "Freddy Freeloader" x is the repetition operator. Example Result "More! " x 3 "More! More! More! " Eduoncloud.com Page 8
With example explain the string functions in perl. --10 String Functions Name chomp length Lc Uc Hex Join Parameter(s) A string A string A string A string A string A character and the strings concatenated together with a list of strings Actions Removes trailing newlines and returns # of removed characters Returns the # of characters in its parameter string Returns the parameter with all uppercase convert to lowercase Returns the parameter with all lowercase convert to uppercase Returns the decimal value of the hex. number in its parameter string Returns a string constructed by concatenating the strings of the second and subsequent parameters the parameter char. in between. Explain standard input output in perl with example. --10 Keyboard Input All input and output in Perl is treated as file I/O. Files have external names, but are know within the program by their internal names (known as "filehandles"). Three predefined filehandles: STDIN, STDOUT and STDERR. Eduoncloud.com Page 9
We get line input by writing: $in_data = <STDIN> ; #reads including newline chomp($in_data = <STDIN>); #does not read newline Screen Output The standard way of producing output is the print operator/function (which requires parentheses). takes one or more string literals separated by commas. print "This is pretty easy\n", By.. ; C's printf function and its formatting specifiers are also available. -----------------------------Example sum.pl ---------------------------------------- # Input: Two numbers, a and b # Output: sum of two numbers print "Please input the value of a "; $a = <STDIN>; print "Please input the value of b "; $b = <STDIN>; $result = $a + $b ; print "The value of the expression is: $result \n"; ------------------------------------------------- Running Perl programs Under Windows, UNIX and Linux, a Perl program is run by typing perl and the program's file name: perl sum.pl Eduoncloud.com Page 10
to see if there are errors, use the c option. If you want warnings as well, use the w option. perl w sum.pl inputfile.dat uses inputfile.dat as standard input. Relational Operators Operation Numeric Operands Is equal to* == Eq Is not equal to*!= Ne Is less than* < Lt Is greater than* > Gt Is less than or equal to* <= Le Is greater than or equal to* >= Ge Compare: a > b it produces -1, a = b produces 0, a > b produces +1 String Operand s <=> Cmp * Produces +1 if true, "" is false Assignment Operators as Control Expressions Because assignment operators have values (the value being assigned to the left-hand side), they can be used as control expressions. The following loop will execute until end of file is encountered: while ($next = <STDIN>) Eduoncloud.com Page 11
Selection and Loop Statements Explain briefly the different selection structures in PERL. Give examples. --10 if, if-else, if-elsif-else - similar to c and c++ Simple if: if ($ > 10) $b = $a * 2; if constructs with elsif if ($snowrate < 1) print "Light snow\n"; elsif ($snowrate < 2) else print "Moderate snow\n"; print "Heavy snow\n"; There is no switch statement in Perl. unless unless has essentially the same syntax as if, except the statements in the block are executed if the condition is false. Eduoncloud.com Page 12
Example is the same as: unless (sum > 1000) print "We\'re not finished yet!"; if (sum <= 1000) print "We\'re not finished yet!"; Explain briefly the different Loop structures in PERL. Give examples. --10 while, until and for loop while and for are similar to those in C. There is also until, where the loop continue as long as the condition is false. $sum = 0; $count = 1; while ( $count <= 100 ) $sum += $count; $sum = 0; $count = 1; until ( $count > 100 ) $sum += $count; for ($sum = 0, $count = 1; $count <= 100; $count++) $sum += $count; Assignment: Example:1 Write program to read N number of values, and then to compute and print sum and average of N entered values. Eduoncloud.com Page 13
Example:2 Write program to read all numbers till End_of_File from keyboard and then to compute and print sum and average. --06 last and next last and next provides an early exit from a loop. last breaks the program out of the innermost loop (or another loop if you use labels) next takes the program to the next iteration of the innermost (or another loop if you use labels). Example: for ($i = 0; $i > 5; $i++) if ($i == 0) last; Loops and Labels in Perl Using a label allows the programmer to decide which one of several nested loops will be exited: BIGLOOP: while ( ) while ( ) while (..) if ( ) last BIGLOOP Eduoncloud.com Page 14
List Literals A list is an ordered sequence of scalar values. A list literal is a parenthesized list of scalar values and is the way a list value is specified in a program. ( 1, 10, 17 ) Arrays can be assigned lists. @mylist = ( 1, 10, 17 ) Lists can store any combination of different types of scalar values: ( $val + 1, "circles", 17 ) Explain how perl arrays different from c and c++ Arrays Arrays are variables that can store lists and begin with @ Scalars and arrays are in different namespaces, so there is no connection between $a and $a[0]. Eduoncloud.com Page 15
$a = 10 ; @a = (50, 60, 70) ; print scalar = $a ; # display 10 print Element = $a[1] ; # display 60 Arrays can be assigned lists or other arrays: @list = ('boy', 'girl', 'dog', 'cat'); @animals = @list; If a list is assigned to a scalar variable, It returns the array's length. $len = @list A list literal with all variables can be assigned values in another list literal: ($first, $second, $third) = ("George", "Bernard", "Shaw"); This is shorthand for: $first = "George"; $second = "Bernard"; $third = "Shaw"; If an array is in that list, it will be assigned any variables not assigned to list members to its left. ($first, @myarray, $third) This is shorthand for: = ("George", "Bernard", "Shaw"); $first = "George"; @myarray = ( "Bernard","Shaw" ) ; $third = ""; In such case array should be kept at the last Arrays and Subscripts Eduoncloud.com Page 16
Arrays in Perl use integer subscripts beginning at zero. Accessing individual elements: @list = (2, 4, 6, 8); $second = $list[1]; Scalars and arrays are in different namespaces, so there is no connection between $a and $a[0]. Array Lengths Array lengths are dynamic: @list = ("Monday", "Tuesday", "Wednesday"); $list[4] = "Friday"; If you write: @list = (2, 4, 6); list[27] = 8; # Elements are 4, but length is 28. The Last subscript in a list $#list $#list + 1 #gives array length #gives array length $#list = 99 #Set array length to 100 foreach Statement The foreach statement in Perl allows the programmer to process every member of an array: Eduoncloud.com Page 17
foreach $value ( @list ) $value /= 2; # divides every member by 2 $value is local to above loop. foreach treats every vacant element had a value. $list[1] = 17; $list[3] = 34; output foreach $value (@list) print "Next: $value \n"; Next: Next: 17 Next: Next: 34 Built-in Array Functions Explain the built-in Array functions available in perl with examples. 1. pop - remove and return the last item of an array. @list = ("Moe", "Curly", "Larry"); $first = shift @list; 2. shift - remove and return the first item of an array. @list = ("Moe", "Curly", "Larry"); $first = shift @list; 3. push - takes two parameters, An array and scalar or list. - Scalar or list is added to the end of the array. Eduoncloud.com Page 18
push @list, "Shemp"; 4. unshift - takes two parameters, An array and scalar or list. - Scalar or list is added to the beginning of the array. unshift @list, "Shemp"; Explain the built-in List functions available in perl with examples. 1. split - breaks strings into parts - using a specific character as the basis for split: $stoogestring = Curly Larry Moe ; @stooges = split (" ", $stoogestring); New list : ("Curly", "Larry", "Moe") 2. sort - takes an array parameter. - uses a string comparison to sort the elements of the array alphabetically in the list that it returns: @list = ("Moe", "Curly", "Larry"); @newlist = sort @list; foreach $value (@newlist) print "$value\n" Eduoncloud.com Page 19
3. qw - place a sequence of unquoted strings inside quotation: qw(peaches apples pears kumquats) will produce: ("peaches", "apples", "pears", "kumquats") 4. die - takes a variable number of string parameters, concatenates them and sends the result to STDERR, and terminates the program. The implicit opeator $! Stores the number of the most recent error that has occurred: Example: die "Error division by 0 in function fun2 - $! " Write Program to read file supplied from command line, then converted to uppercase, and display them in alphabetical order. An Example: process_names.pl # A simple program to illustrate the use of arrays # Input: A file, specified on command line, where line is a # person's name # Output: converted to uppercase, and display in alphabetical order $index = 0; ($name = <>) #>>> Loop to read the names $arrnames[$index++] = uc($name); #>>> Convert to uppercase and STORE in arrnames array Eduoncloud.com Page 20
print "\nthe sorted list of names is:\n\n\n"; #>>> Display the sorted list of names foreach $name (sort @names) print ("$name \n"); Hashes An associative array is an array in which each data item is paired with a key (which uniquely identifies the data). Associative arrays in Perls are called hashes. Hashes are different from arrays because: Array use numeric subscripts; hashes use string values as keys. Arrays are ordered by subscript; hashes are not really ordered. Eduoncloud.com Page 21
Hash names begin with %. Initializing Hashes using a list: %kids_age= ( "John" => 38, "Genny" = 36, "Jake" => 22, "Darcie" => 21 ); assigning an array to a hash: the even subscripts are the hashes, and the odd subscripts are the values: @arr = (3, 5, 6, 99); %agehash = @arr ; is equivalent to %agehash = ("3" => 5, "6" => 99); Referencing a Hash Element An individual value element of a hash can be referenced by using the hash name along with the appropriate key. Curley braces are used: $myage = $arrage"abhishek"; values are added by assigning the new element value with its key: $arrage"aishwarys" = 20; Removing Hash Elements Remove at a given key : delete $arrage"genny"; Eduoncloud.com Page 22
An entire hash can be set to empty by 2 methods: Assigning to an empty list: %arrage = ( ); using the undef ("undefine") operator: undef %arrage; Working With Hashes exist operator: used to determine if an element with a specific key value is in the hash: if ( exists $arrage "Freddie" ) values operator: Extract hash values only keys operator: Extract hash keys only Example foreach $child ( keys %arrage) print ( "The age of $child is $arrage $child \n" ); @allages = values %arrage; print "All of the ages are @ages \n"; Create a hash table in perl containing at least five entries of pair of car models and company as keys and values respectively. Write perl program to read a car model and print its company. Print all entries in a hash table. --10 Eduoncloud.com Page 23
Environmental Variables %ENV is a predefined hash that stores operating systems environmental variables it store information about the system on which Perl is running. The environmental variables can be accessed by any Perl program, where the keys are the variable names. They can be accessed: foreach $key (sort keys %ENV) print "$key = $ENV$key \n"; Write a perl program to display various server information like server name, server protocol, and CGI version. ---10 #!C:\perl\bin\perl.exe print "Content-type: text/html\n\n"; print "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd'> \n"; print "<html xmlns = 'http://www.w3.org/1999/xhtml' > \n"; print "<head> <title> About this server </title> </head> \n"; print "<body> <h1> About this server </h1>", "\n"; print "<hr>"; print "Server name :", $ENV 'SERVER_NAME',"<br>" ; print "Running on port :", $ENV 'SERVER_PORT', "<br>" ; print "Server Software :", $ENV 'SERVER_SOFTWARE', "<br>" ; print "CGI-Revision : ", $ENV 'GATEWAY_INTERFACE', "<br>" ; print "<hr> \n"; print "</body></html> \n"; References Eduoncloud.com Page 24
A reference is a scalar variable that references another variable or a literal (i.e., it's an address). Similar to pointers in C/C++, but are safer to use. A reference is obtained by the backslash operator before variable name: $age = 42; $ref_age = \$age; @names = ("Curly", "Larry", "Moe"); $ref_names = \@names References to Literals A reference to a list literal can be created by putting the literal value in square brackets: $ref_salaries = [42500, 29800, 50000, 35250]; A reference to a hash literal is created by putting the literal value in braces: $ref_ages = 'Curly' => 41, 'Larry' => 38, 'Moe' => 43 ; Dereferencing A reference can be used to specify two values: its own (which is an address) and the value stored at that address. The latter is called dereferencing. All dereferencing in Perl is explicit. This can be done by placing a extra $ in fron of the variable's name: $$ref_names[3] = "Maxine"; This can also be done by using the -> operator: $ref_names -> [3] = "Maxine"; Eduoncloud.com Page 25
Describe basics of perl functions. --7 Functions A function definition includes the function header and a block of code that specifies its actions. No parameters and No return-type of the result is specified. The header contains the reserved word sub and the function s name. A function declaration tells the compiler that a function with a given name will be defined elsewhere. Function s that return values can be used in an expression. Functions that do not return anything can be standalone statements. Example: sub message print "Hello!\n"; Global Variables: Variables that appear only in a function and that are implicitly declared are global. Local Variables: You can force variables to be local by using the word my or local in front of the variable: local $count = 0; my $age = 30; my($count, $sum) = (0, 0); When local and global variables conflict in name, we use the local variable. [Type text] Page 26
Parameters Passing ( Two mthods) Passing by value a copy of the parameters values are given to the function. (one-way communication) If changes do not need to be shared with the main program, passing by value is preferable. Passing by reference a copy of the values addresses are given to the function. (two-way communication) Passing by value Examples All Perl parameters are passed through a special variable called @_. The actual parameters are copied into @_. If an array is a parameter, it is also copied into @_ (and should be at the end of the parameter list). Hashes are flattened into arrays. @_ is passed by value. sub add $result = $_[0] + $_[1]; print "The result was: $result\n"; To call the subroutine and get a result: add(1,2); sub add ($numbera, $numberb) = @_; [Type text] Page 27
$result = $numbera + $numberb; print "The result was: $result\n"; The shift function returns (and removes) the first element of an array. sub add my $num1 = shift; my $num2 = shift; my $result = $num1 + $num2; print "The result was: $result\n"; Returning value sub myfunc if (@_) return $_[0]+$_[1]; else return 0; Passing by reference References to variables can be used as actual parameters, which provides a pass by reference mechanism. sub squeeze my $ref_list = $_[0]; my $value, @new; foreach $value ( @$ref_list ) if ($value > 0) push(@new, $values); [Type text] Page 28
function call: squeeze( \@mylist ); [Type text] Page 29
The sort Function, Revisited Sort compares members of an array as if they were string (coercing numeric values if necessary). This can be changed by giving it an explicit block of code that specifies what comparison to use: #sort numbers in ascending order @new_list = sort $a <=> $b @list; #sort numbers in descending order @new_list = sort $b <=> $a @list; #sort strings in descending order @new_list = sort $b cmp $a @list; An Example tst_median.pl # program to test a function that computes the median of a given array # Parameter: A reference to an array of numbers # Return value: The median of the array, where median is the middle element # of the sorted array, if the length is odd; if the length is even, the median # is the average of the two middle elements of the sorted array. sub median my $ref_list = $_[0]; $len = $#$ref_list + 1; #>>Compute the length my @list = sort $a <=> $b @$ref_list; #>>Sort the array if( $len % 2 == 1) # length is odd return $list[$len/2]; #return middle element as median else # length is even return ( $list [ $len / 2 ] + $list [ $len / 2 1 ] ) / 2; #>>> return the average of the two middle elements of the sorted array. Eduoncloud.com Page 30
#>>> Begin main program: Create two test arrays @list1 = (5, 6, 8, 0, 9); @list2 = (11, 29, 8, 18); #>>> creates array with odd elements #>>> creates array with even elements $med = median ( \@list1 ); #>>> Call median by passing reference print "The median of the first array is $med \n"; $med = median ( \@list2 ); #>>>Call median by passing reference print "The median of the second array is $med \n"; -------------------------------------------- explain the basics of pattern matching in perl, and also explain substitution and transliterate operators. --10 Pattern Matching Perl has following powerful pattern-matching features 1. Pattern-matching operator: m (won't be using explicitly). 2. pattern between slashes, match implicitly against the implicit variable $_: if ( /rabbit/ ) print "rabbit appears in $_\n"; The binding operator =~ matches a variable against the pattern: if ($str =~ /^rabbit/) print "$str begins with rabbit\n" Substitutions Sometimes we may wish to alter a string that we are saving; we can do this using the substitute operator: s/pattern/newstring/ Eduoncloud.com Page 31
Example $str = "It ain't going to rain no more"; $str =~ s/ain't/is not/; Substitutions With Modifiers g modifier: tells to make changes for all occurrence, not just first. $str = "Roser, Is it Rose, rose or ROSE $str =~ s/rose/rose/g; # changes only two names. i modifier: tells the substitute operator to ignore case of letters: $str = "Is it Rose, rose or ROSE $str =~ s/rose/rose/ig; # changes all three to lower case. The Transliterate Operator - tr replace a character (or a class of characters) with another character (or class of characters): $str =~ tr/;/:/ ; # replace semi-colons with colons $str =~ tr/a-z/a-z/; # translate upper to lower case $str =~ tr/\,\.//; # remove all commas and periods More About split split divide a string based on different separators defined in pattern using regular expressions. Example @words = split /[., ]\s*/, $str Eduoncloud.com Page 32
$str will be divided into strings separated by a blank, period or comma followed by any whitespace. word_table.pl # word_table.pl # Input: A file of text in which all words are separated by # ( whitespace, comma, a semicolon, a question mark, an # exclamation point, a period or a colon) may follow whitespace. # The input file is specified on the command line. # Output: A list of all unique words in the input file, in alphabetical order. #>>> Main loop to get and process lines of input text while (<>) @line_words = split /[\.,;:!\?]\s*/; #>>> Split the lines into words foreach $word ( @line_words ) #>>> Loop to count the words if (exists $freq$word) $freq$word ++; else $freq$word = 1; print "\nword \t\t Frequency \n\n"; foreach $word (sort keys %freq) #>>> Display the words and their frequencies print " $word \t\t $freq$word \n"; Eduoncloud.com Page 33
Remembering Matches Parts of a string can be divided up among implicit variables: "4 July 1776" =~ /(\d+) (\w+) (\d+)/; print "$2 $1, $3\n"; #will display July 4, 1776 It can be very useful to match a pattern and save the portions of the string before the match, matching the pattern and after the match. These are $`(before), $& (matching) and $'(after). An Example $str = "This is a rabbit test\n"; $str =~ /rabbit/; print "Before match: $` \n ; print "Present match: $& \n ; print After match: $' \n"; output: Before match: This is a Present match: rabbit After match: test Eduoncloud.com Page 34
Explain file handling in perl. Explain how files can be opened for input and output in perl. 07 Explain file handling in perl with examples. --10 File Input and Output Files are references by using filehandles, whose names do NOT begin with special characters and are usually written in uppercase for greater readability. The connection between external name and filehandle are established using the open function: open( FileHandle, "FileUseSpecifier FileName" ); File Use Specifiers Character(s) Meaning < Input (the default) Eduoncloud.com Page 35
> >> Output, starting at the beginning of the file Output, starting at the end of the existing data on the file +> Input from and output to the file Using a File An Example open (INDAT. "<temperatures") #Open the file for input. or die "Error unable to open temperatures $!"; #If it won't open, terminate and print the error message # Print a line of output print OUTDAT "The result is: $result \n"; #Read one line of input $next_line = <INDAT>; #using file handler Reading Multiple Lines of Input The read function: used to read multiple lines of input into a scalar variable. read ( filehandle, buffer, length [, offset] ); This will read length bytes into scalar variable buffer. If offset is used, it will begin offset bytes after the file pointer. Example $chars = read (ANIMALS, $buf, 255); Using split The lines of text in the buffer can be separated using split: Eduoncloud.com Page 36
@lines = split /\n/, $buf; using seek Some applications seek to re-write data that has just been read. This requires the use of the +> file use specification and then moving back to the beginning of the data just read using seek. Syntax seek (filehandle, offset, base); The base is 0 (beginning of file), 1 (current position), 2 (end of file) Example seek(updat, 240, 0); Eduoncloud.com Page 37