Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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.

2 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.

3 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

4 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.

5 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

6 Small Perl Script while (<>) { –print; }

7 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.

8 Basic Variables in Perl $var = “Text”; $var = 100; @array=(1,2,3,4); %hash=qw(a b c d); $hash{“a”}=“b”; $array[0]=1; $a= ; String variable Numeric variable Array of values Hash table Hash entry Array entry Read from keyboard

9 Standard arithmetic operators + - * / % ++ -- Addition Subtraction Multiplication Division Remainder (mod) Increment Decrement

10 String operators $a = “a”.$b; $line=“-”x80; chop($a); chomp($a) Concatenation Repetition Drop last letter Drop last white space

11 Logical Operators

12 Controlling Perl Statement Blocks (curly braces {}) if/unless while/until for foreach

13 Statement Blocks A sequence of statements enclosed in curly braces. { –Stuff; }

14 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);

15 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

16 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”; }

17 The “foreach” command Iterate over a list of elements Format: –foreach $value (@array) { print “$value\n”; }

18 “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.

19 Get the names of the disks open(DEVICES,”show devices |”); while( ) { –if (/DK/) { $name[$nd++]=$_; } –}; close(DEVICES);

20 For each disk... foreach $disk (@name) { –($d,$stuff)=split(/ /,$disk,2); –open(DISKINFO,”show dev/full $d |”); –while( ) { $total{$d}=substr($_,12,20) if (/Total blocks/); $free{$d}=substr($_,12,20) if (/Free blocks/); } –close(DISKINFO); }

21 Print out results foreach $disk (sort (keys $free)) { –print “$disk\t$free{$disk}\t$total{$disk}\n”; –}

22 Basic I/O Perl can change the way it reads in from a file. $/ (Input record separator) $\ (Output record separator) $_ (Current line)

23 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


Download ppt "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."

Similar presentations


Ads by Google