Docsity

Perl Programming: A Comprehensive Guide, Slides of Computer Science

Partial preview of the text

Download Perl Programming: A Comprehensive Guide and more Slides Computer Science in PDF only on Docsity!

Perl

Why Perl?

  • Perl is built around regular expressions
    • REs are good for string processing
    • Therefore Perl is a good scripting language
    • Perl is especially popular for CGI scripts
  • Perl makes full use of the power of UNIX
  • Short Perl programs can be very short
    • “Perl is designed to make the easy jobs easy, without making the difficult jobs impossible.” -- Larry Wall, Programming Perl

What is a scripting language?

  • Operating systems can do many things
    • copy, move, create, delete, compare files
    • execute programs, including compilers
    • schedule activities, monitor processes, etc.
  • A command-line interface gives you access to these functions, but only one at a time
  • A scripting language is a “wrapper” language that integrates OS functions

Major scripting languages

  • UNIX has sh, Perl
  • Macintosh has AppleScript, Frontier
  • Windows has no major scripting languages
    • probably due to the weaknesses of DOS
  • Generic scripting languages include:
    • Perl (most popular)
    • Tcl (easiest for beginners)
    • Python (new, Java-like, best for large programs)

Comments on “Hello, World”

  • Comments are # to end of line
    • But the first line, #!/usr/local/bin/perl, tells where to find the Perl compiler on your system
  • Perl statements end with semicolons
  • Perl is case-sensitive
  • Perl is compiled and run in a single operation

Perl Example 2

#!/ex2/usr/bin/perl

Remove blank lines from a file

Usage: singlespace < oldfile > newfile

while ($line = ) { if ($line eq "\n") { next; } print "$line"; }

Perl Example 3

#!/usr/local/bin/perl

Usage: fixm

Replace \r with \n -- replaces input files

foreach $file (@ARGV) { print "Processing $file\n"; if (-e "fixm_temp") { die "*** File fixm_temp already exists!\n"; } if (! -e $file) { die "*** No such file: $file!\n"; } open DOIT, "| tr '\015' '\012' < $file > fixm_temp" or die "*** Can't: tr '\015' '\012' < $infile > $outfile\n"; close DOIT; open DOIT, "| mv -f fixm_temp $file" or die "*** Can't: mv -f fixm_temp $file\n"; close DOIT; }

Comments on example 3

  • In # Usage: fixm , the angle brackets just mean to supply a list of file names here
  • In UNIX text editors, the \r (carriage return) character usually shows up as ^M (hence the name fixm_temp)
  • The UNIX command tr '\015' '\012' replaces all characters (\r) with \012 (\n) characters
  • The format of the open and close commands is:
    • open fileHandle , fileName
    • close fileHandle , fileName
  • "| tr '\015' '\012' < $file > fixm_temp" says: Take inputDocsity.com

String and assignment operators

$a = $b. $c; # Concatenate $b and $c

$a = $b x $c; # $b repeated $c times

$a = $b; # Assign $b to $a

$a += $b; # Add $b to $a

$a -= $b; # Subtract $b from $a

$a .= $b; # Append $b onto $a

Single and double quotes

  • $a = 'apples';
  • $b = 'bananas';
  • print $a. ' and '. $b;
    • prints: apples and bananas
  • print '$a and $b';
    • prints: $a and $b
  • print "$a and $b";
    • prints: apples and bananas

push and pop

  • push adds one or more things to the end of a list - push (@food, "eggs", "bread"); - push returns the new length of the list
  • pop removes and returns the last element
    • $sandwich = pop(@food);
  • $len = @food; # $len gets length of @food
  • $#food # returns index of last element

foreach

Visit each item in turn and call it $morsel

foreach $morsel (@food) { print "$morsel\n"; print "Yum yum\n"; }

for loops

  • for loops are just as in C or Java
  • for ($i = 0; $i < 10; ++$i) { print "$i\n"; }

while loops

#!/usr/local/bin/perl print "Password? "; $a = ; chop $a; # Remove the newline at end while ($a ne "fred") { print "sorry. Again? "; $a = ; chop $a; }