Loginskip to content

October 30th, 2006

15 $cases_sold->{$type} += $sold; 16 } 17 else

15 $cases_sold->{$type} += $sold; 16 } 17 else 18 { 19 warn “I don’t know the food type $foodn”; 20 } 21 } 23 foreach my $type (keys %$cases_bought) 24 { 25 print $cases_bought->{$type} - $cases_sold->{$type}, 26 ” cases of $type on handn”; 27 } We used a function new to Perl 5.6.0, fields::phash, which takes the sting out of creating pseudo-hashes, and at the same time avoids the repetition we had on the first two lines of our previous version. If pseudo-hashes are retained in later versions of Perl but their implementation alters, fields::phash will be changed to hide that implementation change from you. When we want to iterate over the different food types to print our report, we just use the keys of a pseudo-hash (which means we no longer get to pick the order, unless we sort the keys). 9.2 Check That Return Code! The Perl built-in functions that might fail return a special value to alert you that they have done so. Usually, this value is false. (Functions that call out to the operating system to do their work will put the type of error in the $!variable.) This allows a number of common colloquialisms for checking and handling it, the most famous of which is the open-or-dieidiom: open DICT, ‘/usr/dict/words’ or die “I’m at a loss for words: $!”; This being Perl, there are many other ways of expressing the same thing. Pick one that works for you: if (mkdir $tmpdir, 0755) { copy $dbfile, “$tmpdir/$dbfile.$$” } else { die “Couldn’t create $tmpdir because $!”; } die “Can’t chdir to $subdir: $!” unless chdir $subdir; print chmod (0644, @files) ? “Success!” : “Failure: $!”;

Hint: This post is supported by Gama hrvatski web hosting services

Comments are closed.