Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

There are (currently) three options for "strictness". Though you should usually use all three by placing:

use strict;
near the top of each of your Perl files. This will help to make your source code easier to maintain.

use strict "vars"

If you use strict "vars", then you will get an error if you ever use a variable without "declaring" it. You can "declare" a variable via:

  • use vars qw( $scalar @array %hash );
  • my( $scalar, @array, %hash );
  • our( $scalar, @array, %hash ); (only for Perl 5.6 and higher).
  • Having the variable imported from a module (not common).
  • Including the package name in the variable name: $main::scalar

Detection of undeclared variables happens at compile time.

use strict "subs"

If you use strict "subs", then you will get an error for most uses of "barewords". Without this strictness, Perl will often interpret an unadorned identifier as a string:

print " ", Hello, ", ", World::ALL, "!\n"; # prints " Hello, World::ALL!"

It is good to use this strictness because forgetting to "adorn" an identifier is a common mistake. Perhaps you meant to write:

print " ", Hello(), ", ", $World::ALL, "!\n";

There are lots of ways to adorn an identifier so that it won't be a bareword:

  • Put it inside quotes or any of Perl's many other quoting operators.
  • Put $, @, %, or & in front of it.
  • Put parentheses after it (making it a function call).
  • Put : after it (making it a label).

And there are several ways you are expected to use barewords that will not be complained about even if you use strict "subs":

  • Reserved words (of course): print if "a" le $_ and not -s $_;
  • File handles: open(BAREWORD,"<$file"); print BAREWORD "Text\n";
  • Hash keys: $hash{bareword}
  • Hash keys in front of =>: %hash= ( bareword => "value" );
  • Declared subroutines (where use strict "subs" got its name). You can declare a subroutine several ways:
    • Import from a module:
      use Fcntl qw( LOCK_EX LOCK_NB ); flock FILE, LOCK_EX|LOCK_NB or die "File already locked"
    • Just define the subroutine before you use it:
      sub Log { warn localtime(), ": ", @_, "\n"; } Log "Ready.";
    • Predeclare the subroutine before you use it and then define it elsewhere:
      sub Log; Log "Ready."; sub Log { warn localtime(), ": ", @_, "\n"; }
      Note that you aren't protected from naming your subroutine "log" instead of "Log" which would result in you trying to take the natural logarithm of "Ready." in both of the examples above because just declaring a subroutine doesn't override built-in routines like log(). So it is a good idea to name subroutines with mixed letter case [you can also invoke your subroutine named "log" using &log("Ready.") but trying &log "Ready." won't work even if you have predeclared sub log as using & without parentheses is a special form of function invocation that takes no arguments, reusing the current values in @_ instead].
  • Package names used to invoke class methods:
    $o= My::Package->new();
    Here My::Package is usually a bareword. Note that this is a bit of an ambiguous case because you could have a subroutine called sub My::Package and the above code would be interpretted as:
    $o= My::Package()->new();
    which is why, for Perl5.6 and later, you may wish to write:
    $o= My::Package::->new();
    to avoid the ambiguity.

Detection of barewords happens at compile time. This is particularly nice because you can make a policy of making sure many of your subroutines are declared before you use them (especially in the case of constants which are usually imported from modules) and them call them as barewords (no parens and no &) and then Perl will detect typos in these names at compile time for you.

use strict "refs"

If you use strict "refs", then you will get an error if you try to access a variable by "computing" its name. This is called a symbolic reference. For example:

use vars qw( $this $that ); my $varname= @ARGV ? "this" : "that"; ${$varname}= "In use"; # This line uses a symbolic reference.

Symbolic references were often used in Perl4. Perl5 has real references (often created with the backslash operator, \) that should be used instead (or perhaps you should be using a hash and computing key names instead of computing variable names). Perl5 also has lexical variables (see the my operator) that can't be accessed via symbolic references. Catching symbolic reference is good because a common mistake is having a variable that should contain a real reference but doesn't. Dereferencing such a variable might silently fetch you garbage without this strictness.

Detection of using symbolic references happens at run time.

If you have one of those truely rare cases where you need to do symbolic references in Perl5, then you can delcare no strict "refs" for just that chunk of code or use eval.

Updated to cover a few more cases.

        - tye (but my friends call me "Tye")

In reply to strict.pm by tye

Title:
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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-28 21:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found