http://www.perlmonks.org?node_id=274552

Grygonos has asked for the wisdom of the Perl Monks concerning the following question:

Anyone know of a good resource for learning how to use strict;
If anyone is going to take themself seriously as a perl programmer they need to learn to use strict from what it semms.
Thanks for any info

Replies are listed 'Best First'.
Re: best way to learn use strict
by sgifford (Prior) on Jul 15, 2003 at 20:21 UTC

    Put use strict; at the top of your program, run it, and see what doesn't work anymore. If it's not obvious from the error message what's wrong with a particular line, look through the secion on strict in the Camel book or the strict perldoc.

    You should also get in the habit of turning warnings on, with either the -w flag to Perl or use warnings;. I'd give the same advice for learning how to write warning-free code: turn it on, see what breaks, see why, fix it.

Re: best way to learn use strict
by Zaxo (Archbishop) on Jul 15, 2003 at 21:01 UTC

    I assume you want to learn how to write code that strict.pm likes. A worthy goal.

    Try combining use strict; with use diagnostics;. That will give you verbose informative error messages when strict kicks up a fuss,

    $ perl -Mstrict -Mdiagnostics -e'$foo = "bar";$bar = "baz";print $$foo +, $/' Global symbol "$foo" requires explicit package name at -e line 1 Global symbol "$bar" requires explicit package name at -e line 1 Global symbol "$foo" requires explicit package name at -e line 1 Execution of -e aborted due to compilation errors (#1) (F) You've said "use strict vars", which indicates that all variables must either be lexically scoped (using "my"), declared beforehand using "our", or explicitly qualified to say which package the global variable is in (using "::"). Uncaught exception from user code: Global symbol "$foo" requires explicit package name at -e line 1 Global symbol "$bar" requires explicit package name at -e line 1 Global symbol "$foo" requires explicit package name at -e line 1 Execution of -e aborted due to compilation errors $
    Ok, that's clear enough, stick in a my before the first use of $foo and $bar,
    $ perl -Mstrict -Mdiagnostics -e'my $foo = "bar";my $bar = "baz";print + $$foo, $/' Can't use string ("bar") as a SCALAR ref while "strict refs" in use at + -e line 1 (#1) (F) Only hard references are allowed by "strict refs". Symbolic references are disallowed. See perlref' Uncaught exception from user code: Can't use string ("bar") as a SCALAR ref while "strict refs" i +n use at -e line 1 $
    Now that's a little meatier. It addresses a design weakness of the program. To clear that stricture we need to think what we mean to be doing. Is it more important to have a string representation of $bar's name, or is the reference the important thing? In the former case, a hash is preferred for holding the data, so we can say $hash{$foo}, where before we said $$foo. If the reference is the thing, we make it a hard reference. Let's assume that the reference is the thing,
    $ perl -Mstrict -Mdiagnostics -e'my $foo = \my $bar; $bar = "baz"; pri +nt $$foo, $/' baz $
    Success. It doesn't take many sessions of this to teach you the rules. Have fun!

    After Compline,
    Zaxo

      couldn't find this on super search.. thanks for the node links.
Re: best way to learn use strict
by Enlil (Parson) on Jul 15, 2003 at 20:25 UTC
    take a look at: perldoc strict and make note of what kinds of things will cause runtime errors when using the pragma (for instance the docs note that "strict refs" will cause a runtime error if you use symbolic references") and avoid those types of things when programming in Perl.

    IMHO though, I think the best way to learn how to "use strict" is to take a program that you have programmed in the past without strict (that breaks when you add "strict") and fix it so it runs under strict, and/or just start using "strict" in anything that will is not just a one time throw away script, before long you will just habitually put "my" (or "our", or fully qualify them.. etc.. ) in front of variables when you declare them, avoid the use of symbolic references, and not use bareword identifiers, etc.

    -enlil

Re: best way to learn use strict
by Trimbach (Curate) on Jul 15, 2003 at 20:21 UTC
    The best way to learn how to use strict is to throw it at the top of some of your code (along with use warnings and maybe even use diagnostics) and then see what happens.

    If the code is long enough and bad enough, you will learn alot figuring out why you're getting all those errors. :-)

    Gary Blackburn
    Trained Killer

    Corrected: It's little "d" diagnostics, not big "D". :-)

Re: best way to learn use strict
by rnahi (Curate) on Jul 15, 2003 at 20:43 UTC
Re: best way to learn use strict
by runrig (Abbot) on Jul 15, 2003 at 20:46 UTC
Re: best way to learn use strict
by cjcollier (Novice) on Jul 15, 2003 at 20:47 UTC
    Running all of your scripts with the -w flag will help you remove any problems. Probably the best way to learn to use strict and remove warnings is to put
    #!/usr/bin/perl -w use strict;
    at the top of your script and rectify any of the warnings that perl prints.

    Cheers,

    C.J.
      I have two problems with this: #!/usr/bin/perl -w use strict; The -w, causes double print on stdout, this breaks my CGI scripts, on solaris. use strict, doesn't catch undefined variables in subroutines, that has the same name as variables outside, that has been declared, this causes hard to find bugs.
        use strict, doesn't catch undefined variables in subroutines, that has the same name as variables outside, that has been declared, this causes hard to find bugs.

        You don't want this. It is the entire point of strictures to be able to safely use a variable name without knowing whether it has been used before and not make code break.

        If you want the opposite, then you're using quasi-globals. Instead, define variables in the smallest possible scope, so that they're not visible from anywhere other than where they get used.

        Makeshifts last the longest.