Section for Week 5, Thursday, 2/11/99 1. Go over LA#4 issues: cgi_helper: a) defined($title) or $title = "Page produced with CGI_Header"; b) No indentation on the here doc's counter.cgi: a) (! -e "filename") (REMEMBER THE QUOTES!) b) print FH "0"; (no comma) environ.cgi: a) Get hash values in sorted key order, do: foreach $key (sort keys %hash) {...} 2. Binary "<=>" returns -1, 0, or 1 depending on whether the left argument is numerically less than, equal to, or greater than the right argument. Binary "cmp" returns -1, 0, or 1 depending on whether the left argument is stringwise less than, equal to, or greater than the right argument. 3. Sorting -- sort BLOCK LIST \__ Both of these require the first argument sort SUBROUTINE LIST / to return -1,0, or 1 as LHS is less than, equal sort LIST to, or greater than RHS Examples: # sort lexically @articles = sort @files; # same thing, but with explicit sort routine @articles = sort {$a cmp $b} @files; # now case-insensitively @articles = sort { uc($a) cmp uc($b)} @files; # same thing in reversed order @articles = sort {$b cmp $a} @files; # sort numerically ascending @articles = sort {$a <=> $b} @files; # sort numerically descending @articles = sort {$b <=> $a} @files; # sort using explicit subroutine name sub byage { $age{$a} <=> $age{$b}; # presuming integers } @sortedclass = sort byage @class; # this sorts the %age associative arrays by value # instead of key using an inline function @eldest = sort { $age{$b} <=> $age{$a} } keys %age; 4. $! == the return value (errno) of the last command spawned (`...`) 5. Using the 'g' modifier on a match will match all occurences. In a scalar context, the match will iterate over the pattern. For example, to match all decimal numbers in a string: while("4.2 5.3 5 6.4" =~ /(\d*\.?\d*)/g) { print "$1\n"; }