views:

110

answers:

4

Why is there no error issued by strict:

use strict;

$a = $a + 1;
+13  A: 

$a and $b are special globals used by sort, so they're always defined. Try it with $c instead and you will get an error.

Dave Sherohman
Note that this means using $a and $b for other purposes is a really bad idea. (Using a variable named $c is also bad, but less so and for other reasons.)
ysth
+4  A: 

$a is a special global variable. It doesn't need to be declared. See perldoc perlvar.

eugene y
+1  A: 

In Perl there are some global variables. Here $a and $b are used in the sort function.

I think you might have noticed, like in this statement:

sort { $a <=> $b } @array_name ; 
pavun_cool
+5  A: 

Although strict does not complain about the special $a and $b variables, perlcritic will detect their usage:

Magic variables should be assigned as "local"... (Severity: 4)

toolic