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

Gurus,
If I have a perl program (let's call it x.pl)
and I have defined a variable in x.pl (let's make it %hash1 = ('One', 1, 'Two', 2); )
and I use a package X.pm in x.pl (let's use as "use X;)

Now, whatever functions I write in X.pm, can I access %hash1 from X.pm
. Since X.pm is loaded into the space for x.pl, should I be not able to access %hash1 directly?
. Any help is greatly appreciated.
  • Comment on using main programs variables inside packages

Replies are listed 'Best First'.
Re: using main programs variables inside packages
by wog (Curate) on Oct 19, 2001 at 02:59 UTC
    You will be able to access the main program's %hash1 variable directly from a package if it is not declared with my (whose visibility would be limited to the block (or file if it's not in an explict block) that it is declared in). You will need to explictly specify the package of the variable with like %::hash1 or %main::hash1 (and, if you are using strict as you should be, you must decalre %hash1 in the main program with our or use vars.)

    However, this is probably not good design. It would probably be best if you made your module not be dependent on what variables the main program is using. That is, you would instead pass the relevant hash to the module's functions (ideally as a reference.)

    update: changed references link.

Re: using main programs variables inside packages
by davorg (Chancellor) on Oct 19, 2001 at 13:13 UTC

    Good programming practice would be to pass the variable as a parameter into any function that required it.

    Using "global" variables inside a function, particularly one in a different package, is a sign of bad program design.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

Re: using main programs variables inside packages
by Flame (Deacon) on Oct 19, 2001 at 02:55 UTC
    I'm not sure I understand what you are trying to ask...

    However, if I do understand correctally...

    If you have a variable in one namespace (package) and you want to get at it from another, you need to use a format similar to $package::variable

    In your case, I believe you need to use %main::hash1 to acess your hash that was declared within x.pl, from the package X.

    It is prefixed with main because that is the default namespace/package for any program unless another is declared with a package statement.


    "Weird things happen, get used to it"

    Flame ~ Lead Programmer: GMS
    http://gms.uoe.org
Re: using main programs variables inside packages
by ant (Scribe) on Oct 19, 2001 at 12:50 UTC
    A little confused on about your email, however I'll stick my two cents in. if you create a variable in X.pm, you can then export that variable into X.pl.
    in X.pm
    package X;

    vars $database1 = "database";
    vars $username1 = "user1";
    vars $password1 = "user1";

    use vars qw(@ISA @EXPORT @EXPORT_OK);
    use Exporter;

    @ISA = qw(Exporter Shell);

    @EXPORT = qw( $database1 $username1 $password1);

    1;


    now in X.pl

    use X.pm

    print "$database1,$username1,$password1";

    you can also export sub routines. Hope that helps. Ant