Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Greetings fellow monks,

I have a file "friends.pm" which contains this :

my %friends = (Paul => 1, Suzie => 1);

and now I'd like to import this into my main namespace, so that I could so something simple like

# %friends isn't defined as of this line do "friends.pm"; # doesn't work foreach my $friend (keys %friends) { # etc ... }

I'm looking for a quick and easy way to do this. Import the variable declared in another file into the current namespace.

thanks you for sharing the wisdom, may the Perl be with you

Amen.

Replies are listed 'Best First'.
Re: Import a variable from another file
by particle (Vicar) on Jun 23, 2003 at 18:19 UTC

    the reason do doesn't, is because of your my declaration. you've created a lexical variable that doesn't exist past the do statement. either use vars (vars.pm), use Exporter (Exporter.pm), or drop the my declaration.

    and read up on perlmodtut and perlmod for more information on modules, including how to use them, and how to create them.

    ~Particle *accelerates*

Re: IMport a variable from another file
by hardburn (Abbot) on Jun 23, 2003 at 18:14 UTC

    You need Exporter.pm

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: IMport a variable from another file
by artist (Parson) on Jun 23, 2003 at 18:25 UTC
    See if you can make use of following

    File: bb.pl #===== use AB; use strict; use warnings; our %friends; print keys %friends; #--------------------- File: AB.pm #========= use strict; use warnings; our %friends = (Paul => 1, Suzie => 1);

    artist

      Please Note: Our is not yet supported in perl 5.005.05. This version is commonly distributed on Solaris 8 (and other places).

      If you find yourself in this situation, and think you need to declare a global then try this:

      use vars qw(%friends);
      If you would like to avoid globals ( this is common wisdom) you might consider defining a subroutine to return your values:
      sub getFriends { # Initializes my little hash our %friends = (Paul => 1, Suzie => 1); return %friends; }
      then you can do this in your main code
      use Friends; my %friends = &getFriends(); # for lexical scope
      If you want to happily ignore scopes, you can do this in your main file:
      require "outsidefile"; print %friends;
      in outsidefile:
      # # guess what? I'm in main's scope !!! # %friends = (Paul => 1, Suzie => 1);
      This is BAD, and strict will complain about this.

      Can anyone explain how to properly create two files that share the same package name (name space) that works under strict?

      -------------------------------------
      Nothing is too wonderful to be true
      -- Michael Faraday

        In your outside file you can just use vars, and it will make those variables strict-safe in that package anywhere in code.

        Merely using the variable doesn't do it, you have to declare it.

Re: IMport a variable from another file
by diotalevi (Canon) on Jun 23, 2003 at 20:43 UTC

    Added>> I forgot to mention this but the other responses gave you the right information. <<

    There's an example of something spectacularly wrong in Re: Stealing lexicals - best practice suggestions which happens to be exactly what you're looking for. It steals private my() variables out of another package and turns them into globals. The wicked witch of the west's evil monkey minions will cart you off if you actually do this.

    All this proves is that it is possible to do what you asked but only through wrong doing.

Re: IMport a variable from another file
by tos (Deacon) on Jun 23, 2003 at 20:01 UTC
    If you use global variables instead of lexically defined ones you have the possbility to access vars of another package (=namespace) via ::-notation. You then can access the "Paul"-value of friends.pm's hash %friends for instance with

    print $friends::friends{"Paul"}