230 likes | 341 Views
Perl Basics. Chapters 1-6 of “Learning Perl” By Randal Schwartz, Tom Christiansen & Larry Wall; ISBN 1-56592-284-0, 302 pages. Second Edition, July 1997. What is Perl?. Perl is short for "Practical Extraction and Report Language," or "Pathologically Eclectic Rubbish Lister.”
E N D
Perl Basics Chapters 1-6 of “Learning Perl” By Randal Schwartz, Tom Christiansen & Larry Wall; ISBN 1-56592-284-0, 302 pages. Second Edition, July 1997.
What is Perl? • Perl is short for "Practical Extraction and Report Language," or "Pathologically Eclectic Rubbish Lister.” • Both are endorsed by Larry Wall, Perl's creator and chief architect, implementor, and maintainer. • He created Perl when he was trying to produce some reports from a Usenet-news-like hierarchy of files for a bug-reporting system, and awk ran out of steam.
Where does Perl fit? • Perl is a high level language that fits between C and a shell interpreter. • Since it is an interpreted language, it can be used between systems. • It is available on UNIX systems, VMS, Windows 9x/NT, and Macintosh. • Go to http://www.perl.com/CPAN
Things to remember about Perl • There’s usually more than two ways to do things in Perl. • It’s easy to write code that may become unable to be read seconds afterwards. • If you learn how to use “regular expressions”, you can do great things in one or two lines.
Walking through a Perl program • First line usually starts with • #!/usr/bin/env perl • This is a UNIX notation to indicate that it is a Perl program. • Usually name perl programs with .pl
Small Perl Script • while (<>) { • print; • }
Defined/Undefined variables • Variables do not have to be defined before you use them. They just don’t exist until you give them a value. • “undef” can delete a variable from the running script.
$var = “Text”; $var = 100; @array=(1,2,3,4); %hash=qw(a b c d); $hash{“a”}=“b”; $array[0]=1; $a=<STDIN>; String variable Numeric variable Array of values Hash table Hash entry Array entry Read from keyboard Basic Variables in Perl
+ - * / % ++ -- Addition Subtraction Multiplication Division Remainder (mod) Increment Decrement Standard arithmetic operators
$a = “a”.$b; $line=“-”x80; chop($a); chomp($a) Concatenation Repetition Drop last letter Drop last white space String operators
Controlling Perl • Statement Blocks (curly braces {}) • if/unless • while/until • for • foreach
Statement Blocks • A sequence of statements enclosed in curly braces. • { • Stuff; • }
Conditional: if/unless • if, if/else, if/elsif/else • “if something is true, do it” • if ($x > 50) {print “too much }; • print “Too much” if ($x > 50); • unless • “do it if something is false” • print “Too much” unless ($x < 50);
Iteration with while/until • while • “while something is true, do block” • until • “until something is true, do block” • do/while, do/until • Check after doing the block
The “for” statement • Iterate over a range of values • Format: • for (initial; test; increment/decrement) • for ($i=0;$i<@array;$i++) { • print “$a[$i]\n”; • }
The “foreach” command • Iterate over a list of elements • Format: • foreach $value (@array) { • print “$value\n”; • }
“showdisks.pl” • A script to compute how much disk space is on sgenab. • Uses hash tables to store info • Uses the ability to look through the output of system commands.
Get the names of the disks • open(DEVICES,”show devices |”); • while(<DEVICES>) { • if (/DK/) { • $name[$nd++]=$_; • } • }; • close(DEVICES);
For each disk... • foreach $disk (@name) { • ($d,$stuff)=split(/ /,$disk,2); • open(DISKINFO,”show dev/full $d |”); • while(<DISKINFO>) { • $total{$d}=substr($_,12,20) if (/Total blocks/); • $free{$d}=substr($_,12,20) if (/Free blocks/); • } • close(DISKINFO); • }
Print out results • foreach $disk (sort (keys $free)) { • print “$disk\t$free{$disk}\t$total{$disk}\n”; • }
Basic I/O • Perl can change the way it reads infrom a file. • $/ (Input record separator) • $\ (Output record separator) • $_ (Current line)
Printing stuff out in Perl • Standard print • print “Hello World, $name.\n”; • printf • printf (“Hello World, %s.\n”,$name); • Formatted printing • Later... • “\n” newline, “\t” tab