Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

comment on

( #3333=superdoc: 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 :)

In reply to Use strict and warnings by runrig

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 imbibing at the Monastery: (3)
As of 2023-03-29 03:15 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (70 votes). Check out past polls.