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

Re: How to use variables from other package?

by Ovid (Cardinal)
on Apr 27, 2003 at 06:35 UTC ( [id://253454]=note: print w/replies, xml ) Need Help??


in reply to How to use variables from other package?

The classic way to do this is to export those variables via Exporter.

package Foo; use base 'Exporter'; use vars qw( @ISA @EXPORT_OK $foo ); @EXPORT_OK = qw( $foo ); $foo = 3;

And then in your code:

use Foo qw($foo); print $foo;

Note that the qw() operator is used. Those are strings being quoted, not variables. See perldoc Exporter for more information.

Cheers,
Ovid

New address of my CGI Course.
Silence is Evil (feel free to copy and distribute widely - note copyright text)

Replies are listed 'Best First'.
Re: Re: How to use variables from other package?
by Anonymous Monk on Apr 27, 2003 at 17:06 UTC

    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.

      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.
Re: Re: How to use variables from other package?
by Juerd (Abbot) on Apr 28, 2003 at 06:44 UTC

    The classic way to do this is to export those variables via Exporter.

    Another, easier (tidier) way is to do so using Exporter::Tidy. That way, you get to export the lexical so that in your module you don't have the overhead that package global variables normally have.

    package Foo; use Exporter::Tidy _map => { '$foo' => \my $foo }; $foo = 3;
    And then, in the code that uses the package, unchanged:
    use Foo qw($foo); print $foo;

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Log In?
Username:
Password:

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

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












    Results (56 votes). Check out past polls.