Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    [Discipulus]: but even about programs pm can be a preliminare boiler plate
    [Tanktalus]: Creating tutorials on github et al may not be a bad idea either ;)

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (6)
    As of 2017-09-22 21:21 GMT
    Find Nodes?
      Voting Booth?
      During the recent solar eclipse, I:

      Results (269 votes). Check out past polls.