Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Perl's strict warn about an undeclared $a?

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.

like image 256
est Avatar asked Sep 09 '10 05:09

est


2 Answers

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 :)

like image 183
brian d foy Avatar answered Nov 15 '22 01:11

brian d foy


$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.

like image 32
rafl Avatar answered Nov 15 '22 00:11

rafl