200 likes | 322 Views
Programming with Perl. CSCE 330 Group presentation by: Robert Shannon Ryan Mullaney Anthony So. What is Perl?. Started in 1987 by Larry Wall P ractical E xtraction and R eporting L anguage Originally developed as a utility for UNIX to replace awk
E N D
Programming with Perl CSCE 330 Group presentation by: Robert Shannon Ryan Mullaney Anthony So
What is Perl? • Started in 1987 by Larry Wall • Practical Extraction and Reporting Language • Originally developed as a utility for UNIX to replace awk • Currently used as replacement for awk, sed, grep, shell scripts, C… • May be referenced as scripting or programming language depending on the source
Features • Why should I learn Perl? • Similar syntax to C therefore familiar to most programmers • Extremely expressive so a few lines of code will do a lot • Typical uses: • World Wide Web through CGI libraries (Common Gate-way Interface) – the interaction between the browser & server • ActiveX scripting • Processing log files • Windows network administration
Timeline • 1987 - Oct - Perl 1.0 is released • 1988 - Jun - Perl 2.0 is released • 1989 - Oct - Perl 3.0 is released • 1991 - Mar - Perl 4.0 is released • 1992 - Jan - MacPerl 4.0.2 is released • 1992 - Dec - MacPerl 4.0.5 is released • 1993 - Feb - Perl 4.036 is released • 1993 - Oct - MacPerl 4.1.0 is released
Timeline continued • 1994 - Oct - Perl 5.000 is released • 1995 - Mar - Perl 5.001 is released • 1995 - Aug - MacPerl 5.0.0 is released • 1996 - Jul - Perl 6 announced • 2000 - Mar - Perl 5.6 is released • Presently - Perl 6 is still in work
Advantages • Perl is compiled every time it is executed • This allows for easy modifications and portability • Unlike interpretive languages Perl must completely compile before any of it runs • This saves many headaches from partially changed text files • A few lines of Perl can do what it would take a complicated C language program to do. • Many built in functions that save time
Disadvantages • Perl is compiled each time it is executed • Since any one can look at your script and change it there is the issue of security • Limited support for data hiding when dealing with classes. • There are no warnings when variables are coerced between types. Example: $scalar=@array; • Because of this finding logic errors can be hard • Perl offers a number of ways to do the same thing, some more efficient than others, therefore a badly written Perl script can monopolize system resources.
Single or Double Quotes • Single quotes are literal. They will pass on exactly what is inside of them. $myname="David"; print 'Hello World! My name is $myname'; RESULTS : Hello World! My name is $myname • Double quotes variables are interpolated and analyzed for values $myname="David"; print "Hello World! My name is $myname"; RESULTS: Hello World! My name is David
Scalars • A scalar is an individual piece of data. In Java it would be the same as a variable. • Scalar data is symbolized by a $ sign. • For correct processing, the $ must be followed by a letter (non-numeric) character. • Scalar names are CaSe SeNsItIvE. $A is different than $a. • Can use up to 255 characters for a scalar name which includes numbers, letters, and underscores.
Scalars continued $answer = 42; # Interger$pi = 3.14159; # Floating point$pet = “Camel”; # string$msg = “I love $pet”; # interpolated string$cost = ‘I costs $100’; # not interpolated$idea = ‘Don\’t do it’; # not interpolated$dst = $src; # Copy variables$x = $y + 5; # expression
Scalar Example $a=5; # a contains value of 5 $b=2.5; # b contains value of 2.5 $a=$b + $a; # a now contains value of 7.5 OR $mymessage="Hello World!"; print $mymessage; $mymessage="Bye World!"; print $mymessage; # symbol signifies a comment in the code
Arrays • Arrays are groups of data stored in a single spot. • While scalar data use the $ sign, arrays use the @ symbol. • Example: #!/usr/bin/perl @days=("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"); print "Content-type: text/html\n\n"; print "These are the days of the week: "; print "@days"; • Results: These are the days of the week: Monday Tuesday Wednesday Thursday Friday Saturday Sunday
Array continued • What about accessing an individual array item? print "The value for the fourth array item is $days[3]"; Notice the use of $ and not @ in this instance. Therefore the breakdown of: @days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"); Is: $days[0] = "Monday";$days[1] = "Tuesday";$days[2] = "Wednesday";$days[3] = "Thursday";$days[4] = "Friday";$days[5] = "Saturday";$days[6] = "Sunday";
Hashes • Hashes are very similar to an arrays. • A hash is an array of pairs often called an "associative array". • A hash uses the % symbol instead of the @ symbol. • Arrays use 0 to whatever number as an index. • Hashes are keys and value pairs. key, element, key, element, and so on... • Hashes use the curly brackets {} instead of the square brackets [] to find individual elements.
Hash Example %chores = ("Monday", "dishes", "Tuesday", "vacuum", "Wednesday", "garbage"); $value = $chores{Wednesday}; print "$value"; The output of the example script is garbage
Comparison to C C: Void main() { printf(“Hello World!”); } Perl: print ‘Hello World!’
Example of Perl in Action #!c:\perl\bin\perl.exe -w my ($dir); $dir = "."; opendir(DIR, "$dir") || Error("open", "directory"); my @dirfiles = readdir(DIR); closedir(DIR) || Error("close", "directory"); ren(@dirfiles);
sub ren { my ($old_name, $regex, $repstr, @files); @files = @_; $regex = "\.old\$"; $repstr = ".new"; foreach (@files){ $old_name = $_; $_ =~ s/$regex/$repstr/i; rename($old_name, $_); } } sub Error { print "The server can't $_[0] the $_[1]: $! \n"; }
Sources • http://www.perl.com/pub/a/2000/10/begperl1.html • http://www.linuxjournal.com/article.php?sid=3394 • http://www.htmlite.com/PERLintro.php • http://www.perl.org/press/fast_facts.html • http://history.perl.org/PerlTimeline.html • http://www.Planet-Source-Code.com