Perl Basics Chapters 1-6 of “Learning Perl” By Randal Schwartz, Tom Christiansen & Larry Wall;...

23
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.
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    222
  • download

    3

Transcript of Perl Basics Chapters 1-6 of “Learning Perl” By Randal Schwartz, Tom Christiansen & Larry Wall;...

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

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.

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

Page 3: 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.

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

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

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.

Page 5: 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.

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

Page 6: 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.

Small Perl Script

• while (<>) {– print;

• }

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

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.

Page 8: 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.

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=<STDIN>;

• String variable• Numeric variable• Array of values• Hash table• Hash entry• Array entry• Read from keyboard

Page 9: 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.

Standard arithmetic operators

• +• -• *• /• %• ++• --

• Addition• Subtraction• Multiplication• Division• Remainder (mod)• Increment• Decrement

Page 10: 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.

String operators

• $a = “a”.$b;• $line=“-”x80;• chop($a);• chomp($a)

• Concatenation• Repetition• Drop last letter• Drop last white space

Page 11: 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.

Logical Operators

EqualNot equalLess thanGreater thanLess than orequal toGreater thanor equal to

==!=<>

<=

>=

eqneltgtle

ge

Page 12: 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.

Controlling Perl

• Statement Blocks (curly braces {})

• if/unless

• while/until

• for

• foreach

Page 13: 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.

Statement Blocks

• A sequence of statements enclosed in curly braces.

• {– Stuff;

• }

Page 14: 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.

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

Page 15: 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.

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

Page 16: 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.

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

• }

Page 17: 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.

The “foreach” command

• Iterate over a list of elements

• Format:– foreach $value (@array) {

• print “$value\n”;

• }

Page 18: 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.

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

Page 19: 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.

Get the names of the disks

• open(DEVICES,”show devices |”);

• while(<DEVICES>) {– if (/DK/) {

• $name[$nd++]=$_;

• }

– };

• close(DEVICES);

Page 20: 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.

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

• }

Page 21: 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.

Print out results

• foreach $disk (sort (keys $free)) {– print “$disk\t$free{$disk}\t$total{$disk}\n”;– }

Page 22: 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.

Basic I/O

• Perl can change the way it reads infrom a file.

• $/ (Input record separator)

• $\ (Output record separator)

• $_ (Current line)

Page 23: 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.

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