Perl Practical Extraction and Reporting Languagegeneral-purpose, high level, general-purpose, interpreted, dynamic programming languageinvented by Larry Wall 1987 (a linguist working at NASA)different versions of Perl, upcoming: Perl 6links:www.perl.org/ general sitehttp://perldoc.perl.org/index-tutorials.html tutorials1Running Perl perl [-c] fileName-c argument only checks for syntax but does not execute the scriptperl -v -bash-3.2$ perl -vThis is perl, v5.8.8 built for x86_64-linux-thread-multiCopyright 1987-2006, Larry WallPerl may be copied only under the terms of either the Artistic License or theGNU General Public License, which may be found in the Perl 5 source kit.Complete documentation for Perl, including FAQ lists, should be found onthis system using "man perl" or "perldoc perl". If you have access to theInternet, point your browser at http://www.perl.org/, the Perl Home Page.2Perl Running a Perl script-bash-3.2$ perl file.plor use #! in first line of script#!/usr/bin/perlperl version of “hello world”print “hello world. \n”; each line must end with “;”3Perl Simple Variablesvariables always use $ sign, e.g.,$i =3;4Perl Variables, Strings and Integersstrings specified by text in quotation marksstrings can be concatenated by operator “.”integers support a range operator, e.g., 3..15print 1, 2, 3..15, "\n"; # range operatorprint "A", "B", "C", "\n"; # strings$i = "A" . "B" ; # concatenation operatorprint "$i", "\n" ;output123456789101112131415ABCAB5Perl Arraysdynamic allocation (don’t need to worry about allocation, it is done for you)arrays use @ symbol, e.g., @arr@arr = (1,2,3,4,5);This line defines the array "arr" and puts 5 values in it. Same as@arr = (1..5); print @arr[0],"\n"; prints out first element of arr6Perl Arraysarray elements start with 0use array index to access specific elementif only array name is printed, the entire array is printedusing an array in a scalar operation will be interpreted as the number of elements in the array7Perl Arrays@a1 = (1); # array of 1 element@a2 = (1,2,3,4,5); # array of 5 elements@a3 = (1..10); # array of 10 elementsprint @a1, " ", @a2, " ", @a3, "\n";print @a1[0], " ", @a2[1], " ", @a3[2], "\n";# using as scalar will yield number of itemsprint @a2 + @a3, "\n";will result in the following output:1 12345 123456789101 2 3158Perl Associated arraysrather than using index with value 0 to maximum size of array the array value can be used to access elements@month{'January'} = 1;@month{'February'} = 2; ...and so on. Then you can read in the month name and access its numeric value this way:$monthnum = $month{$monthname};9Perl alternative way to set up array%month = ("January", 1, "February", 2, "March", 3, "April", 4, "May", 5, "June", 6, "July", 7, "August", 8, "September", 9, "October", 10, "November", 11, "December", 12);The set of values that can be used in an associative array, or the keys to the array, are returned as a regular array by a call to the Perl function keys():@monthnames = keys(%month);10Perl Mathematical and Logical Operatorssimilar to other languages+, -, *, /integer increments before or after value is used11Perl examples$n = 2;print ("\$n=", $n, "\n");$n = 2 ; print ("increment after \$n=", $n++, "\n");$n = 2 ; print ("increment before \$n=", ++$n, "\n");$n = 2 ; print ("decrement after \$n=", $n--, "\n");$n = 2 ; print ("decrement before \$n=", --$n, "\n");This script generates the following output:$n=2increment after $n=2increment before $n=3decrement after $n=2decrement before $n=112Perl examples$n = 2;print ("\$n+2=", $n + 2, "\n");print ("\$n-2=", $n - 2, "\n");print ("\$n*2=", $n * 2, "\n");print ("\$n/2=", $n / 2, "\n");This script generates the following output:$n+2=4$n-2=0$n*2=4$n/2=113Perl examples$r = 3.14; # real numberprint ("\$r=", $r, "\n");print ("\$r*2=", $r * 2, "\n"); # doubleprint ("\$r/2=", $r / 2, "\n"); # cut in halfprint ("1 && 1 -> ", 1 && 1, "\n");print ("1 && 0 -> ", 1 && 0, "\n");print ("1 || 1 -> ", 1 || 1, "\n");print ("1 || 0 -> ", 1 || 0, "\n");This script generates the following output:$r=3.14$r*2=6.28$r/2=1.571 && 1 -> 11 && 0 -> 01 || 1 -> 11 || 0 -> 114Perl String Operatorsonly simple operation is concatination$firstname = "Bob";$lastname = "Smith";$fullname = $firstname . " " . $lastname;print "$fullname\n";results in the output:Bob Smith15Perl String Operatorsseveral simple matching operations are availableif ($value =~ /abc/) { print "contains 'abc'\n"};$value =~ s/abc/def/; # change 'abc' to 'def'$value =~ tr/a-z/A-Z/; # translate to upper case16Perl Comparison
View Full Document