[Chicago-talk] Two Perl curiosities

Andy Lester andy at petdance.com
Tue May 19 11:12:27 PDT 2015


> I meant to type "delta" but I typed "b".  Shouldn't Perl throw an error
> because of the strict pragma or at least a warning? I get no warnings
> when I "perl -c" and when running, all the warning I get is that $b is
> uninitialized.  When I create a small test script with this kind of
> error, I get a warning: "Name "main::b" used only once: possible typo at
> ./test.pl line 11."


$a and $b are always declared behind the scenes because they are special variables with special meaning to the sort() function.  You can have custom comparison functions for sort() and those functions use $a and $b for the two items to compare.

Like this:

my @names = qw( Moe Larry Curly );
my @sorted_names = sort &case_insensitive @names;
sub case_insensitive {
    return lc($a) cmp lc($b);
}

$a and $b aren’t passed in to the function.  They just exist.  So that’s why your typo didn’t fire a warning.

--
Andy Lester => www.petdance.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/chicago-talk/attachments/20150519/c109d996/attachment.html>


More information about the Chicago-talk mailing list