What does this script print?
#!/usr/bin/perl
use strict;
$a = 1;
$b = 2;
$c = 3;
print $a+$b+$c, "\n";
Notice the use strict
statement.
It’s definetely not good to use variables without declaring them.
There are 3 undeclared variables in the script. Of course, it doesn’t work! But where exactly does it fail?
>perl abc.pl
Global symbol "$c" requires explicit package name at abc.pl line 7.
Global symbol "$c" requires explicit package name at abc.pl line 9.
Execution of abc.pl aborted due to compilation errors.
$a
and $b
are always declared global variables
intended for use in sort
function.
They always present in any package and don’t break
compilation even when use strict
is in effect.
So the first undeclared variable is in fact $c
.
Resume: always do use strict
,
declare your variables with my
or our
,
otherwise you’ll be sorry ;)
And using $a
and $b
outside of
comparison function for sort
is a bad idea.