in reply to Re: IMport a variable from another file
in thread IMport a variable from another file

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

Replies are listed 'Best First'.
Re: Re: Re: IMport a variable from another file
by tilly (Archbishop) on Jun 25, 2003 at 12:00 UTC
    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.