Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Use strict and warnings

by runrig (Abbot)
on Sep 08, 2001 at 04:09 UTC ( [id://111088]=perltutorial: print w/replies, xml ) Need Help??

When you want to use strict

Whenever your program gets over a few lines long, definitely when you can't view the whole program on one page, or sometimes when you just can't figure out what else could be wrong.

Why do you want to use strict?

To help you catch typos so you can quickly get on to finding more significant problems (and so we don't have to catch the typos for you either), among other reasons.
Its difficult to spot '$recieve_date' when on the previous page you've been calling it '$receive_date'. Also, to give your variables as small a scope as possible so that you don't have to worry about what they're doing to other parts of your program (although that's the function of my, it forces you to use my which when properly used helps achieve this goal).

Why it's not 'too much trouble' to use strict

It's just 11 extra characters at the top of your script(use strict;), and two extra characters throughout your script(my).

That sounds great. How do I use strict?

Put this line at the top of your script (after the shebang, e.g., '#!/usr/bin/perl' line):
use strict;

Now my program's broken. What do I do?

The most common error showing up looks something like this:
Global symbol "$xxx" requires explicit package name at ./tst line 5.
This is the error we're going to focus on fixing.
(If you are getting 'Server Error' or the like, then either check your web server error logs or run your script from the command line, or look into using CGI::Carp).
Whenever you first use a variable, put 'my' in front of it, e.g.:
# Change this: $string = "hello world"; @array = qw(ABC DEF); %hash = (A=>1, B=>2); # To this: my $string = "hello world"; my @array = qw(ABC DEF); my %hash = (A=>1, B=>2); # Change this: # '$name' is global here @names = qw(Bob Fred); foreach $name (@names) { print "Name: $name\n"; } # To this: my @names = qw(Bob Fred); foreach my $name (@names) { # Now '$name' only exists in this block print "Name: $name\n"; } # Change this: # Likewise, '$digit' is global here @digits = (5,3,4); foreach $digit (@digits) { $number = 10*$number + $digit; } print "Number: $number\n"; # To this (variables used in an outer scope ('$number') # will have to be declared in an outer scope): my @digits = (5,3,4); my $number = 0; foreach my $digit (@digits) # Now '$digit' only exists in this block $number = 10*$number + $digit; } print "Number: $number\n"; # Change this: sub my_sub { ($arg1, $arg2) = @_; print "Arg1: $arg1 Arg2: $arg2\n"; } # To this: sub my_sub { my ($arg1, $arg2) = @_; print "Arg1: $arg1 Arg2: $arg2\n"; } # Using DBI? You can change this: $sth->bind_columns(\$field1, \$field2); while ($sth->fetch) { print "F1: $field1 F2: $field2\n"; } # To this (the '\' is distributed over a list of values): $sth->bind_columns(\my ($field1, $field2)); while ($sth->fetch) { print "F1: $field1 F2: $field2\n"; }

That seems like too much trouble. Isn't laziness a virtue?

Sure we're lazy. And we don't like spending time looking for simple mistakes in your program that you could have found yourself with 'use strict'.

What about warnings?

Oh yeah. In version 5.6 or later you can put this right around the same place you put 'use strict;':
use warnings;
In perl's before 5.6 (or if you just want to be portable between the versions), you can put '-w' on the 'shebang' line, or set the $^W variable (however, setting $^W will not catch compile time warnings unless its in a BEGIN{} block, so '-w' is usually preferable):
#!/usr/local/bin/perl -w # Or $^W = 1; # Or BEGIN { $^W = 1 }
If you know you want to disable warnings, you can do it in a limited scope:
# Change this: sub add_two_numbers_which_might_be_undef { $_[0] + $_[1]; } # To one of these (depending on perl version): # 1 sub add_two_numbers_which_might_be_undef { # See 'perldoc perllexwarn' for all the categories of warnings # because its better to only disable the warnings you're expecting no warnings "uninitialized"; $_[0] + $_[1]; } # 2 sub add_two_numbers_which_might_be_undef { local $^W; $_[0] + $_[1]; }
Or sometimes you'll have to initialize variables as in the example above that uses '$number'.

See Also:

Also read Ovid's excellent 'use strict' is not Perl.

And (as wog pointed out): Use strict warnings and diagnostics.
And Use strict warnings and diagnostics or die.
And The strictures, according to Seuss.

And that's it! Now you have no excuse for not using strict or warnings. And it'll make life easier for all of us :)

Replies are listed 'Best First'.
(elbie): Use strict and warnings
by elbie (Curate) on Sep 10, 2001 at 17:03 UTC

    A good article for beginning programmers to read. It might be nice if you can include some common error/warning messages that a programmer might encounter when using those pragmas for the first time.


      I found this most useful. Thank you.
Re: Use strict and warnings
by Qiang (Friar) on May 26, 2005 at 13:41 UTC
    This tutorial is translated into Chinese ! more to come :)
Re: Use strict and warnings
by ansh batra (Friar) on Nov 02, 2011 at 05:05 UTC
    nice writeup!!!
    i have been into perl from since last 3 months
    but got the concepts of warnings n strict cleared now
Re: Use strict and warnings
by Anonymous Monk on Jul 24, 2012 at 13:37 UTC
    I was recently asked by a guy during Interview about usage of strict and my in Perl. I was unable to answer and searched for it in google. Definitely this page gives the best introduction. Cheers, Mo
Re: Use strict and warnings
by Anonymous Monk on May 24, 2007 at 10:11 UTC
    Good Article.....

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-05-18 20:11 GMT
Find Nodes?
    Voting Booth?

    No recent polls found