perltutorial
waxmop
<p>After you've gotten fairly comfortable with perl, I strongly recommend you always load the strict module in all your programs. Putting <code>use strict;</code> 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.
<p>This code shows some of the weird and scary places you can end up if you don't do <code>use strict;</code> in your programs.
<p>After you understand it, uncomment the <code>use strict;</code> line and watch what happens when you run the script again.
<code>
#!/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.
</code>
<p><small>Edit by [tye], remove PRE from around CODE</small></p>