After you've gotten fairly comfortable with perl, I strongly recommend you always load the strict module in all your programs. Putting use strict; at the top of your programs will tell perl to slap your hands with a fatal error whenever you break certain rules. And just like the rules against playing on the roof or freebasing crystal meth, those rules are there to help you.
This code shows some of the weird and scary places you can end up if you don't do use strict; in your programs.
After you understand it, uncomment the use strict; line and watch what happens when you run the script again.
#!/opt/local/bin/perl -w #you'll need to replace the above with the path #to your perl interpreter. #use strict; #uncomment me later! #this demonstrates why it's good to use strict. $ref = 'aaa'; $aaa = 99; @aaa = (1,2,3); #now watch as $ref points to different variables, #depending on the context. print "scalar context: $$ref\n"; print "array context: @$ref\n"; #what is $ref, anyway? print "\$ref: $ref\n"; #here's an example of the same thing using a hash instead of a ref. #this is how I first discovered it. $hash{aaa} = "aaa"; print "hash value in scalar context: $hash{aaa}\n"; print "hash value in array context: @{$hash{aaa}}\n"; #Pretty weird stuff. It all makes sense once you learn symbolic #references, which were a big part of perl4, but they can be ambiguous #depending on context, so if you use strict, you can bar them.
Edit by tye, remove PRE from around CODE
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Why you should use strict
by Abigail-II (Bishop) on Jun 27, 2003 at 22:50 UTC | |
Re: Why you should use strict
by cchampion (Curate) on Jun 27, 2003 at 18:10 UTC | |
by waxmop (Beadle) on Jun 27, 2003 at 18:35 UTC | |
Re: Why you should use strict
by jdklueber (Beadle) on Jul 03, 2003 at 14:54 UTC | |
Re: Why you should use strict
by davido (Cardinal) on Sep 07, 2003 at 20:32 UTC |
Back to
Tutorials