views:

119

answers:

2

Can someone please explain to me the below code. This behavior has been like this for a while (tested on 5.8.5, 5.8.8, 5.10.1, 5.12.2) so there must be a reason behind it?

$ perl -M5.012 -E '$aa=2'
Global symbol "$aa" requires explicit package name at -e line 1.

$ perl -M5.012 -E '$a=2'

Thanks.

+11  A: 

$a (and also $b, and many others) is a global variable. It's intended to be used in the sort function, e.g. sort { $a <=> $b } @list. perldoc perlvar lists all of perl's built-in globals and their meaning.

rafl
Actually, I believe $a and $b are 'package globals' rather than full on global globals (if that makes any sense). See [perldoc sort](http://perldoc.perl.org/functions/sort.html) where it says "...the elements to be compared are passed into the subroutine as the package global variables $a and $b...".
Jonathan Leffler
Yes, they are globals that aren't forced into the main:: namespace like some other globals as, for example, `$_`.
rafl
+14  A: 

This is in the strict documentation:

Because of their special use by sort(), the variables $a and $b are exempted from this check.

Always check the docs. Most of the answers are in there :)

brian d foy