Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.perl.misc > #4724
| From | tmcd@panix.com (Tim McDaniel) |
|---|---|
| Newsgroups | comp.lang.perl.misc |
| Subject | Re: getting hash reference from a package |
| Date | 2012-03-13 17:56 +0000 |
| Organization | Tim McDaniel's at Panix |
| Message-ID | <jjo1on$2l8$1@reader1.panix.com> (permalink) |
| References | <jjnsjo$57f$1@dont-email.me> <jjntgq$an4$1@dont-email.me> |
In article <jjntgq$an4$1@dont-email.me>,
Bill M <wpmccormick@just_about_everywhere.com> wrote:
>Bill M wrote, On 3/13/2012 11:28 AM:
>> What is the correct syntax for accessing a hash reference from a package?
>>
>> For example:
>>
>> package MyPackage;
>> my $foo = {
>> animals => ["cat", "dog", "fish"],
>> people => ["fred", "wilma" ]
>> };
>>
>> 1;
I added the missing double-quote before wilma and lowercased
"package".
"1;" usually comes at the end of a module file. So I assume that
that's in its own file, named MyPackage.pm.
>use MyPackage;
Yeah, that requires a separate source file named MyPackage.pm or such.
>
>foreach my $animal (@{$MyPackage::foo->{animals}}) {
> print $animal;
>}
Realize that "my" is lexical only.
(1) "my" does not create a package variable. There is no
$MyPackage::foo variable in MyPackage. When you refer to it in the
other file, it creates on that spot a variable set to undef.
(2) It's lexical only, so it exists only inside the file's scope.
It's not visible outside, so trying to refer to just "$foo" would get
you an error "Global symbol "$foo" requires explicit package name".
So replace "my" by "our" in MyPackage and it all works as you expect.
--
Tim McDaniel, tmcd@panix.com
Back to comp.lang.perl.misc | Previous | Next — Previous in thread | Next in thread | Find similar
getting hash reference from a package Bill M <wpmccormick@just_about_everywhere.com> - 2012-03-13 11:28 -0500
Re: getting hash reference from a package Bill M <wpmccormick@just_about_everywhere.com> - 2012-03-13 11:44 -0500
Re: getting hash reference from a package tmcd@panix.com (Tim McDaniel) - 2012-03-13 17:56 +0000
Re: getting hash reference from a package Bill M <wpmccormick@just_about_everywhere.com> - 2012-03-13 13:07 -0500
Re: getting hash reference from a package Jack <goodcall1@hotmail.com> - 2012-03-14 01:57 -0600
Re: getting hash reference from a package Martijn Lievaart <m@rtij.nl.invlalid> - 2012-03-14 12:06 +0100
Re: getting hash reference from a package tmcd@panix.com (Tim McDaniel) - 2012-03-14 15:37 +0000
Re: getting hash reference from a package Ben Morrow <ben@morrow.me.uk> - 2012-03-14 17:54 +0000
Re: getting hash reference from a package Ben Morrow <ben@morrow.me.uk> - 2012-03-14 14:27 +0000
Re: getting hash reference from a package Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-14 18:28 +0000
Re: getting hash reference from a package Ben Morrow <ben@morrow.me.uk> - 2012-03-14 21:08 +0000
Re: getting hash reference from a package Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-14 21:23 +0000
Re: getting hash reference from a package gbacon@hiwaay.net (Greg Bacon) - 2012-03-14 12:20 -0500
Re: getting hash reference from a package Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-14 18:14 +0000
Re: getting hash reference from a package gbacon@hiwaay.net (Greg Bacon) - 2012-03-16 12:11 -0500
Re: getting hash reference from a package Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-18 22:12 +0000
Re: getting hash reference from a package gbacon@hiwaay.net (Greg Bacon) - 2012-03-20 10:28 -0500
Re: getting hash reference from a package Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-14 20:52 +0000
csiph-web