Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Re: Re: How to use variables from other package?

by integral (Hermit)
on Apr 27, 2003 at 17:34 UTC ( #253502=note: print w/replies, xml ) Need Help??

in reply to Re: Re: How to use variables from other package?
in thread How to use variables from other package?

One problem I've had when doing this is that strict no longer provides protection against a typo in the variable name. At least by using an import you are making the name available unqualified at compile-time so that perl can check it for you.

use Foo; print $Foo::fooo; # woops!

One argument for using a fully qualified name is that you prevent a name clash with a variable in the current scope, although I would consider it bad style to reuse a variable name in two scopes, where it is used in both and from both. It would also indicate to me that the variable has perhaps been badly named.

As usual it depends on the situation, but you need to be aware of the pitfalls of the approach you take. One module which uses package globals to modify its behaviour is Data::Dumper, which has variables such as Purity and Terse. These have quite common names, but you wouldn't want to export them into the current package because they are only used by the Data::Dumper code.

Data::Dumper on the other hand does have a more 'modern' interface to the same functions by using an object to store the settings which has the advantage of allows different callers to use different options.

integral, resident of freenode's #perl

Replies are listed 'Best First'.
Re: Re: Re: Re: How to use variables from other package?
by chromatic (Archbishop) on Apr 27, 2003 at 19:55 UTC

    I'm not sure I understand. Perl can indeed check variable names at compile time, even if they're fully qualified. Create a file called

    package Foo; use vars qw( $foo ); $foo = 1;

    Use it within a test program like so:

    #!/usr/bin/perl -w use strict; use Foo; print "$Foo::foo\n"; print "$Foo::fooo\n";

    This gives me:

    Name "Foo::fooo" used only once: possible typo at line 7. 1 Use of uninitialized value in concatenation (.) or string at + line 7.

      I forgot about warnings. Sometimes they can be easily missed (I certainly have), so reading the 'Fatal Warnings' section of perllexwarn may be an idea.

      use warnings FATAL => qw(all);

      integral, resident of freenode's #perl

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://253502]
and snow settles gently...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (1)
As of 2017-03-26 11:06 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (315 votes). Check out past polls.