in reply to IMport a variable from another file

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

Replies are listed 'Best First'.
Re: Re: IMport a variable from another file
by freddo411 (Chaplain) on Jun 23, 2003 at 22:36 UTC
    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.