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

Why you should use strict

by waxmop (Beadle)
on Jun 27, 2003 at 16:06 UTC ( #269642=perltutorial: 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

Comment on Why you should use strict
Select or Download Code
Re: Why you should use strict
by cchampion (Curate) on Jun 27, 2003 at 18:10 UTC

      I wrote this to explain a particular example and shared it with some coworkers. I thought maybe somebody here could benefit from it also, so I submitted to be considered. I don't run this site - if this is a redundant story, then vote against it.

Re: Why you should use strict
by Abigail-II (Bishop) on Jun 27, 2003 at 22:50 UTC
    I'm not quite sure what you think the uncommenting of use strict is going to show. It's certainly not going to forbid the use of symbolic references! Because that's something that happens at run time, and the code above doesn't compile when turning on strict. You can fix this by using $::ref, $::aaa, etc. Then you will a "useful" message about using scalar references when turning on strict.

    However, the example doesn't impress me at all. Without strict, there's nothing dangerous or even strange happening. In fact, Perl is quite doing what people would expect. You'd have to come up with a better example if you want people to get hooked to use strict - this example will just get them hooked to using symbolic references.


Re: Why you should use strict
by jdklueber (Beadle) on Jul 03, 2003 at 14:54 UTC
    I strongly recommend you always load the strict module
    "use strict;" isn't loading the strict module. It's loading the strict pragma... strictly speaking. :-)
    Jason Klueber

Re: Why you should use strict
by davido (Archbishop) on Sep 07, 2003 at 20:32 UTC
    After you've gotten fairly comfortable with perl, I strongly recommend you always load the strict module in all your programs.

    Why wait until one has become fairly comfortable with potentially bad habbits? When developing in Perl, always:

    use strict; use warnings;

    There is no reason not to. Using strict and warnings will speed up your learning and your development process. Failing to use them will inhibit you considerably.

    One might argue that there are situations where strict and warnings might prevent someone from intentionally, and with full understanding of the ramifications, doing something that strict and warnings see as suspect. I submit that in those rare instances, strict and warnings may be temporarily disabled, or directed to accept a particular thingy. But don't throw out the baby with the bathwater. Just disable them in the portions of code that you know and understand why a stricture violation is still ok. I venture to assert that such cases will be few and far between in normal development.

    It is a "Good Idea" to reinforce among both novices and experts alike, the use of strict and warnings. Experts will already know its value. Beginners and novices might not. They should be encouraged to use those pragma's from their very first "Hello world!\n";.


    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perltutorial [id://269642]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2014-09-21 20:25 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (175 votes), past polls