Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2023-09-26 05:08 GMT
Find Nodes?
    Voting Booth?

    No recent polls found