RE: RFC on first perl script

From: Dan Anderson (dan_at_mathjunkies.com)
Date: 11/06/03


To: drowl@23.me.uk
Date: Thu, 06 Nov 2003 13:17:00 -0500


> Global symbol "$site" requires explicit package name at ./makeArf.pl line 17.

One of the things about strict is it makes you declare the scope of your
variables before using them. So, for instance, while:

#! /usr/bin/perl
$foo = "foo\n";
print $foo;

Would run, the following wouldn't:

#! /usr/bin/perl
use warnings; # yelp and whine if we screw up
use strict; # force us to not be sloppy.
$foo = "foo\n";
print $foo;

It would cause perl to say:
Global symbol "$foo" requires explicit package name at - line 4

We could fix that by changing like 4 to one of the following:
my $foo = "foo\n";
our $foo = "foo\n";
local $foo = "foo\n";

>>From Perldoc:

    my EXPR
    my TYPE EXPR
    my EXPR : ATTRS
    my TYPE EXPR : ATTRS

            A "my" declares the listed variables to be local (lexically)
to the enclosing block, file, or "eval". If more than one value is
listed, the list must be placed in parentheses.

            The exact semantics and interface of TYPE and ATTRS are
still evolving. TYPE is currently bound to the use of "fields" pragma,
and attributes are handled using the "attributes" pragma, or starting
from Perl 5.8.0 also via the "Attribute::Handlers" module. See "Private
Variables via my()" in perlsub for details, and fields, attributes, and
Attribute::Handlers.

local EXPR

You really probably want to be using "my" instead, because
"local" isn't what most people think of as "local". See
"Private Variables via my()" in perlsub for details.

A local modifies the listed variables to be local to the
enclosing block, file, or eval. If more than one value is
listed, the list must be placed in parentheses. See "Temporary our
EXPR
    our EXPR TYPE
    our EXPR : ATTRS
    our TYPE EXPR : ATTRS

An "our" declares the listed variables to be valid globals
within the enclosing block, file, or "eval". That is, it has the
same scoping rules as a "my" declaration, but does not create a
local variable. If more than one value is listed, the list must
be placed in parentheses. The "our" declaration has no semantic
effect unless "use strict vars" is in effect, in which case it
lets you use the declared global variable without qualifying it
with a package name. (But only within the lexical scope of the
"our" declaration. In this it differs from "use vars", which is
package scoped.)

An "our" declaration declares a global variable that will be
visible across its entire lexical scope, even across package
boundaries. The package in which the variable is entered is
determined at the point of the declaration, not at the point of
use. This means the following behavior holds:

    package Foo;
    our $bar; # declares $Foo::bar for rest of lexical scope
    $bar = 20;

    package Bar;
    print $bar; # prints 20

Multiple "our" declarations in the same lexical scope are
allowed if they are in different packages. If they happened to
be in the same package, Perl will emit warnings if you have
asked for them.

    use warnings;
    package Foo;
    our $bar; # declares $Foo::bar for rest of lexical scope
    $bar = 20;

    package Bar;
    our $bar = 30; # declares $Bar::bar for rest of lexical scope
    print $bar; # prints 30

    our $bar; # emits warning

An "our" declaration may also have a list of attributes
associated with it.

The exact semantics and interface of TYPE and ATTRS are still
evolving. TYPE is currently bound to the use of "fields" pragma,
and attributes are handled using the "attributes" pragma, or
starting from Perl 5.8.0 also via the "Attribute::Handlers"
module. See "Private Variables via my()" in perlsub for details,
and fields, attributes, and Attribute::Handlers.

The only currently recognized "our()" attribute is "unique"
which indicates that a single copy of the global is to be used
by all interpreters should the program happen to be running in a
multi-interpreter environment. (The default behaviour would be
for each interpreter to have its own copy of the global.)
Examples:

    our @EXPORT : unique = qw(foo);
    our %EXPORT_TAGS : unique = (bar => [qw(aa bb cc)]);
    our $VERSION : unique = "1.00";

Note that this attribute also has the effect of making the
global readonly when the first new interpreter is cloned (for
example, when the first new thread is created).

Multi-interpreter environments can come to being either through
the fork() emulation on Windows platforms, or by embedding perl
in a multi-threaded application. The "unique" attribute does
nothing in all other environments.

Values via local()" in perlsub for details, including issues
with tied arrays and hashes.

-Dan



Relevant Pages

  • Re: First attempt at writing simple Module
    ... I'm trying to factor out the declaration and setting of a bunch of variables ... that a suite of programs will need, as well as defining some subroutines ... Package manage to become visible to main (although that's exactly what I ... No. "my $foo" does create a brand new scalar variable named ...
    (perl.beginners)
  • Re: Question about multiple files
    ... No. extern just means: The actual definition is somewhere else. ... the compiler can differentiate between declaration and definition just ... > declaration it finds and in this way can I write code expecting it to be ... When the compiler reaches the line foo(); ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Newbie static class member question [C++]
    ... >and allows subsequent declarations to refer to Foo in certain contexts ... there is a name 'i' which designates an int ... In some contexts a declaration is also a definition. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Question regarding prototypes for 0-ary functions.
    ... int fooint x; ... extern int foo(int, ... ... extern int foo(); is not a valid declaration, ... The definition of foo says it takes one parameter, while the declaration ...
    (comp.lang.c)
  • Re: Question about multiple files
    ... but say I declare with extern. ... > the compiler can differentiate between declaration and definition just ... >> declaration it finds and in this way can I write code expecting it to be ... > When the compiler reaches the line foo(); ...
    (alt.comp.lang.learn.c-cpp)