Re: Pragmas use strict and use warnings



On 30 Oct 2007 at 10:22, Paul Lalli wrote:
On Oct 30, 11:15 am, chas.ow...@xxxxxxxxx (Chas. Owens) wrote:
On 10/30/07, Kaushal Shriyan <kaushalshri...@xxxxxxxxx> wrote:
snip> Whats the exact purpose of use strict

snip

The strict pragma has three effects (unless modified by arguments):
1. most variables must be declared (there are some exceptions)

This is a common misperception. use strict 'vars' in fact does not
require anything to be declared. All it requires is that global
variables must be fully qualified. You cannot use the short-name of
globals that belong to the current package.

<snipped>

With strict enabled, the only way to use a short-name of a variable is
to declare a lexical of that name (the right choice) using 'my', or to
disable strict 'vars' on a variable-by-variable case using 'our' (the
wrong choice). This is what leads people to assert "use strict forces
you to declare your variables".

Technicaly you are right, but generally it's better to present use
strict 'vars' as a way to force oneself to declare variables. It may
not be exactly what it does, but it's why it exists.

Plus you forgot two ways to allow using the short names. Apart from
the

our $global;

there is also the older

use vars qw($global);

that does the same, except it is not block, but package scoped;


Another option is to disable use vars for a block:

some( $strict, $code);
{
no strict 'vars';
$global = 784;
}
other( $strict, $code)

Jenda
===== Jenda@xxxxxxxxxxx === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

.



Relevant Pages

  • Re: Pragmas use strict and use warnings
    ... disable strict 'vars' on a variable-by-variable case using 'our' (the ... you to declare your variables". ... strict 'vars' as a way to force oneself to declare variables. ... if the resulting error message was something along the ...
    (perl.beginners)
  • Re: our vs. use vars
    ... > except that use vars is backwards compatable. ... All these code snippets pass strict, and they each set the package ... a "use vars" declaration doesn't extend ...
    (perl.beginners)
  • Re: How to use select (select(2)) in Perl?
    ... If you find the 'vars' pragma ... clearly shouts "innards" to the casual reader..:o)]. ... I don't even know the true thing that makes use-vars work. ... 'strict' documentation if anywhere]. ...
    (comp.lang.perl.misc)
  • Custom rmodule with use strict;
    ... I'm using a simpewlmmodule to get vars an dfunctions into one place. ... It works dandy except I can't have use strict in the module. ...
    (perl.beginners)
  • small mistake: http://perl.plover.com/FAQs/Namespaces.html
    ... Perl 5.6.0 introduced a new ourdeclaration. ... Its syntax is the same as for my, and it is a replacement for use ... ouris just like use vars; ... the exception that it creates to the strict checking ...
    (comp.lang.perl.misc)