Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Variable scope in packages.

by CuriousGeorge (Initiate)
on Jun 29, 2001 at 23:27 UTC ( [id://92771]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all. Say I have a script Foo.pl that has a hash %crap. Now I define a package called BooYah.pm. If I do a require "Foo.pl" in BooYah, how can I access %crap? Thanks for all the help!

Replies are listed 'Best First'.
Re: Variable scope in packages.
by damian1301 (Curate) on Jun 29, 2001 at 23:50 UTC
    When you require a script(*.pl), it brings it's variables, subs, and all into your namespace so you can simply access it as %crap. Voila!

    NOTE: That if you require a package it will not be the same. It will not be automatically included in your namespace and you would have to make each call explicitly -- Credit goes to Petruchio on the note.

    $_.=($=+(6<<1));print(chr(my$a=$_));$^H=$_+$_;$_=$^H; print chr($_-39); # Easy but its ok.
      Note: if you are using strict (in BooYah.pm), and %crap is declared with my in Foo.pl, then you won't be able to access it at all. Heck, you won't even be able to run the program (if you try to access it from BooYah.pm that is) :)
Re: Variable scope in packages.
by cLive ;-) (Prior) on Jun 30, 2001 at 03:26 UTC
    BooYah.pm is only a package if you begin it with:
    package BooYah;

    Then, all vars in that package can be accessed as $BooYah::var_name.

    Of course, you can have more than one package in a script :)

    I think what you're asking is "what is the default package?". The answer to that is 'main' or '' by default.

    The var would have to be declared globally (no my/local) and explicitly (you are using strict now, aren't you :). BUT, it's a bad idea to rely on vars in main from the package that was required. If you do though, there are two instances I can think of:

    # (1) # Foo.pl - extract # implicitly %::crap = (turd => 'yellowy brown', crap => 'browny black'); # or explicitly %main::crap = (turd => 'yellowy brown', crap => 'browny black'); # BooYah.pm - extract require 'Foo.pl'; # implicitly print $::crap{turd}; # or, explicitly print $main::crap{turd}; # (2) - give Foo.pl a namespace # Foo.pl - extract package Foo; %Foo::crap = (turd => 'yellowy brown', crap => 'browny black'); # BooYah.pm - extract require 'Foo.pl'; print $Foo::crap{turd};

    Although I'm a little worried about your question. Shouldn't you be requiring the module from the script and not requiring the script from the module?!?!

    Do some research on namespaces, or read perlmod documentation.

    cLive ;-)

      Actually, you can have packages without using the 'package' keyword, by using fully-qualified names. The following defines things in three different packages (and is loadable as it stands as a module):

      sub A::A { } sub B::B { } $C::someVar = 123;
Re: Variable scope in packages.
by runrig (Abbot) on Jun 29, 2001 at 23:49 UTC
    That depends. Are you using strict? Is %crap a lexical scoped or package variable? Have you tried just accessing %crap? What problem do you have? How about %BooYah::crap? I think introducing a new global variable from an outside source into your current package namespace is a bad idea, but what do I know?

    Update: If BooYah.pm is really a package (declared with 'package BooYah;', then declare %crap with 'use vars' or our. Then you can just directly use %crap in BooYah.pm.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-26 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found