Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > importing a hash from package

Reply
Thread Tools

importing a hash from package

 
 
Jeff Thies
Guest
Posts: n/a
 
      09-15-2003
I'm sure this is easy, but I'm not finding it...

I have a package with a heash in it:

package VARIABLES;

my %HASH;
$HASH{a}='a';

$some_scalar='some_scalar'

....

I want to refer to that hash elsewhere.

I know I can do this:

my $local_some_scalar=VARIABLE::$some_scalar;

But, how do I get at HASH? Do I have to write a reference to it in
package VARIABLES?

Cheers,
Jeff
 
Reply With Quote
 
 
 
 
Steve Grazzini
Guest
Posts: n/a
 
      09-15-2003
Jeff Thies <(E-Mail Removed)> wrote:
> package VARIABLES;
>
> my %HASH;
> $HASH{a}='a';
>
> $some_scalar='some_scalar'
>
> ...
>
> I want to refer to that hash elsewhere.
>
> I know I can do this:
>
> my $local_some_scalar=VARIABLE::$some_scalar;

^^^^^^^^^^^^^^^^^^^^^^

That's not quite right.

> But, how do I get at HASH? Do I have to write a reference to it
> in package VARIABLES?


The difference is between lexical (my) variables, which you can't
use outside the scope where they were declared, and package variables,
which are global.

You can use our() to declare package variables.

our %HASH; # %VARIABLES::HASH

Is that what you were after?

--
Steve
 
Reply With Quote
 
 
 
 
Jeff Thies
Guest
Posts: n/a
 
      09-15-2003


Steve Grazzini wrote:
>
> Jeff Thies <(E-Mail Removed)> wrote:
> > package VARIABLES;
> >
> > my %HASH;
> > $HASH{a}='a';
> >
> > $some_scalar='some_scalar'
> >
> > ...
> >
> > I want to refer to that hash elsewhere.
> >
> > I know I can do this:
> >
> > my $local_some_scalar=VARIABLE::$some_scalar;

> ^^^^^^^^^^^^^^^^^^^^^^


oops!

>
> That's not quite right.
>
> > But, how do I get at HASH? Do I have to write a reference to it
> > in package VARIABLES?

>
> The difference is between lexical (my) variables, which you can't
> use outside the scope where they were declared, and package variables,
> which are global.
>
> You can use our() to declare package variables.
>
> our %HASH; # %VARIABLES::HASH
>
> Is that what you were after?


Yes, thanks!

Jeff
>
> --
> Steve

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      09-15-2003
Jeff Thies <(E-Mail Removed)> wrote:
> Steve Grazzini wrote:


>> The difference is between lexical (my) variables, which you can't
>> use outside the scope where they were declared, and package variables,
>> which are global.
>>
>> You can use our() to declare package variables.
>>
>> our %HASH; # %VARIABLES::HASH
>>
>> Is that what you were after?

>
> Yes,



Then see also:

"Coping with Scoping":

http://perl.plover.com/FAQs/Namespaces.html


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
How to keep a module with the same name as a module it is importing from importing itself? plb Python 2 02-08-2005 03:14 PM
Importing a package and looping through modules in the package Dave Python 2 02-10-2004 08:14 PM
importing classs without package in JSP ? mhk Java 1 11-28-2003 11:46 PM



Advertisments