Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by Anonymous Monk
on Apr 27, 2003 at 17:06 UTC ( [id://253498]=note: print w/replies, xml ) Need Help??


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

Wouldn't it be enough to just do the following:

Package:

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

Script:

use Foo; print $Foo::foo;

It works for me. But is there something wrong with this method? Apart from the fact, that I have to explicity call $foo with the package name.

Replies are listed 'Best First'.
Re: Re: Re: How to use variables from other package?
by integral (Hermit) on Apr 27, 2003 at 17:34 UTC

    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
    

      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 Foo.pm:

      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 usefoo.pl line 7. 1 Use of uninitialized value in concatenation (.) or string at usefoo.pl + 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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://253498]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2025-01-25 06:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (70 votes). Check out past polls.