Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is our keyword necessary at all?

perl -e 'use strict;use warnings;$a=2;print $a'

It seems no warning is reported when I declare a global variable without our,so when is this keyword necessary?

like image 854
new_perl Avatar asked Jul 14 '11 08:07

new_perl


2 Answers

$a and $b are special cases - they are reserved for sort (e.g. sort { $a <=> $b }) and shouldn't be used for your own variable names - try again using something else and see what happens!

like image 84
plusplus Avatar answered Oct 01 '22 00:10

plusplus


From perlvar:

   $a
   $b      Special package variables when using "sort()", see "sort" in
           perlfunc.  Because of this specialness $a and $b don't need to
           be declared (using "use vars", or "our()") even when using the
           "strict 'vars'" pragma.
like image 37
Eugene Yarmash Avatar answered Sep 30 '22 22:09

Eugene Yarmash