Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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


In reply to Why you should use strict by waxmop

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (4)
    As of 2014-08-30 02:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (291 votes), past polls