Download presentation
Presentation is loading. Please wait.
Published byPenelope Lindsey Modified over 8 years ago
1
PZ02CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ02CX - Perl Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 3.3.3 Appendix A.9
2
PZ02CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 2 Perl overview Perl (Practical Extraction and Report Language) is an interpreted language designed for efficient text- processing 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
3
PZ02CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 3 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
4
PZ02CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 4 Perl example 1 #!/usr/bin/perl 2 @inputdata = split(/ /, ); 3 $count = 0; 4 foreach $nextone (@inputdata) 5 {print "$nextone"; 6 $count = $count + $nextone;}; 7 print "Sum = "; 8 print "$count\n"; Figure A.14 in text
5
PZ02CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 5 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 $_ = ; # Read in input to argument $_ if (/ a+b+$/) then { print “yes\n”;} else { print “no\n”;} # 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+/.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.