For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2004 > passing hashes form packages









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author passing hashes form packages
Bzzt

2004-07-28, 8:56 pm

Hi,
I am trying to pass a hash from a package to my script with the following.
Can anyone tell me why it doesn't work?
the_package.pm-------------------
#!/usr/local/ActivePerl-5.6/bin/perl -w
package the_package;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(%exp_hash);
use WWW::Mechanize;
use strict;

my %exp_hash;
$exp_hash{val1}=1;
$exp_hash{val2}=2;
$exp_hash{val3}=2;

the_script.pl-------------------------------------
#!/usr/local/ActivePerl-5.6/bin/perl -w
use warnings;
use strict;
use the_package;
my $value;
foreach $value (keys %exp_hash) {
print $exp_hash{$value};
}
print $exp_hash{val1};


Wiggins D'Anconia

2004-07-28, 8:56 pm

bzzt wrote:
> Hi,
> I am trying to pass a hash from a package to my script with the following.
> Can anyone tell me why it doesn't work?
> the_package.pm-------------------
> #!/usr/local/ActivePerl-5.6/bin/perl -w
> package the_package;
> require Exporter;
> @ISA = qw(Exporter);
> @EXPORT = qw(%exp_hash);
> use WWW::Mechanize;
> use strict;
>
> my %exp_hash;


You have declared your hash as a lexical as opposed to a package
variable. To export the package variable of the same name you need to
declare it as a package variable. You can do so by switching 'my' to
'our' (on older Perl's you will need 'use vars'),

perldoc -f my
perldoc -f our

http://danconia.org

> $exp_hash{val1}=1;
> $exp_hash{val2}=2;
> $exp_hash{val3}=2;
>
> the_script.pl-------------------------------------
> #!/usr/local/ActivePerl-5.6/bin/perl -w
> use warnings;
> use strict;
> use the_package;
> my $value;
> foreach $value (keys %exp_hash) {
> print $exp_hash{$value};
> }
> print $exp_hash{val1};
>
>
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com