File Opening, Input, Output and Sorting

File Handles open (FILE_HANDLE, “file.name”); Open for reading: open (SONGS, “song_list.txt”); open (SONGS, “song_list.txt”) or die “Can’t open that file! Error is $!”;   Retrieve lines using < > : open (SONGS, “song_list.txt”) or die “Can’t open that file!”; for $line (<SONGS>) { print $line; }   Open for overwriting: open (SONGS, “>song_list.txt”) or die …

Perl String Comparisons and String Replacements

Strings and String Comparisons Be careful which operators you use! Are you assigning, or testing? $answer = “no”;if ($answer == “yes”) {print “Answer is Yes.\n”;} Exercise: do test operatiors make a difference? Test this code: $i = 11;if ($i == “11”) { print “Equivalent numbers.\n”; }if ($i eq “11”) { print “Equivalent strings.\n”; } Getting …

Perl “while” and “until” Loops

while and until $a = 0; while ($a != 3) { $a++; print “Counting upward: now \$a is $a…\n”; } until ($a == 0) { $a–; print “Now we go backward:  $a…\n”; } Resources http://www.tizag.com/perlT/perlwhile.php

Perl “if” Testing

if: Testing a Condition if ($user eq “fred”) {print “Welcome in, Fred.\n”;}elsif ($user eq “joe” || $user eq “jill”){print “Get out, $user\!\n”;}else {print “No Admittance.\n”;} unless ($age > 21 && $smile == “big”) {print “You’re too young for me.\n”;}else {print “How about a date?\n”; } Note the “and” ( && ) and “or” ( || …

Perl Operators

Operators The ever-friendly Tizag.com explanation: http://www.tizag.com/perlT/perloperators.php A nice introduction to Perl Operators The more formal Perlop re. Operators Compare Perl vs. Shell Operators: http://gnorman.org/index.php?option=com_content&view=article&id=208:operators-wildcards-expressions&catid=7:shell-scripting&Itemid=2786

Perl Functions

Functions print ( ) print “You can print a single string like this one.\n”; print “Or you can “, “supply a list “, “of strings separated by commas.”; Resources http://perldoc.perl.org/index-functions.html