Perl Programming Language Design and Implementation 4 th





- Slides: 5

Perl Programming Language Design and Implementation (4 th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 3. 3. 3 Appendix A. 9

Perl overview • Perl (Practical Extraction and Report Language) is an interpreted language designed for efficient textprocessing applications • It was developed by Larry Wall in 1986 • It was originally called PEARL, but a conflict with an existing graphics language resulted in the shortening of the name • The language includes pattern matching, file handling, and scalar data • It has syntax patterned after C and was originally designed to operate as a shell script • Its power is that it views programs and files as data • It has proven to be an effective language for interacting with Web pages

Perl structure • Variables in Perl are either integers or strings and begin with the symbol $. • A simple Perl program consists of a series of print statements. • It has the usual sequence of control structures such as for, while, and until loops and if conditional. • Perl shares an ability to process regular expressions like several other process languages • Example use: • $ENV{'USER'} = ‘mvz’ • will be true if login name mvz is the string $ENV{'USER'} (i. e. , is the user running this program). • This shows how Perl can interact with the system environment

Perl example • • 1 2 3 4 5 6 7 8 #!/usr/bin/perl @inputdata = split(/ /, <STDIN>); $count = 0; foreach $nextone (@inputdata) {print "$nextone"; $count = $count + $nextone; }; print "Sum = "; print "$countn"; Figure A. 14 in text

Perl regular expressions • • Perl provides a direct translation of regular expressions. For example, the regular expression a+b+ is the following Perl script: • • #!/usr/bin/perl # This is a Perl script $_ = <STDIN>; # Read in input to argument $_ if (/ a+b+$/) then { print “yesn”; } else { print “non”; } # Match $_ with a+b+ • • The pattern /X/ is matched against the string argument. This pattern succeeds if the regular expression a+b+ extends from the initial character of the input line ( ) to the end of the input line ($). To succeed if a+b+ is contained within a string, we only need the pattern /a+b+/. • •