Code Comments
Programming Forum and web based access to our favorite programming groups.I have a module that needs to export a hash and a subroutine. I used
"Exporter" and that's now happening, but there's a catch...
I need the subroutine to make changes to that hash, when called. (I'm
aware this is an unusual interface and I do have good reasons for
wanting to do it.) I can't seem to get this part right.
I'm using something like:
#!/usr/bin/perl
package MyExporter;
use strict;
use warnings;
use Exporter;
our @ISA = 'Exporter';
our @EXPORT = qw/ %hash routine /;
our %hash = (Test => 'Works!');
sub routine {
my($caller) = caller;
$$caller::hash{Another_Test} = 'Doesn\'t work!';
}
1;
__END__
When I use Data::Dumper from the module using the example above, I get:
$VAR1 = {
'Test' => 'Works!'
};
I'm
about why the above doesn't work. Exporter isn't somehow
creating a lexical variable out of that hash, is it? I guess this is
my first question: What am I not understanding about the above?
Second question: What is the correct way to do what I'm trying to do?
Thanks for your time and any tips you can pass on.
James
Post Follow-up to this messageSorry to answer my own question but...
On Aug 2, 2004, at 1:37 PM, James Edward Gray II wrote:
> #!/usr/bin/perl
>
> package MyExporter;
>
> use strict;
> use warnings;
>
> use Exporter;
> our @ISA = 'Exporter';
> our @EXPORT = qw/ %hash routine /;
>
> our %hash = (Test => 'Works!');
>
> sub routine {
> my($caller) = caller;
> $$caller::hash{Another_Test} = 'Doesn't work!';
Changing that to:
$hash{Another_Test} = 'Now works!';
Does the trick.
> }
>
> 1;
>
> __END__
>
> I'm
about why the above doesn't work. Exporter isn't somehow
> creating a lexical variable out of that hash, is it? I guess this is
> my first question: What am I not understanding about the above?
I'm still a little
about why that works. Is it because after
the subroutine is "exported" it's called from inside the same namespace
as the hash and can manipulate it at will? Or am I just lost (quite
possible)?
Thanks again.
James
Post Follow-up to this messageOn Aug 2, James Edward Gray II said: >I'm still a littleabout why that works. Is it because after >the subroutine is "exported" it's called from inside the same namespace >as the hash and can manipulate it at will? Or am I just lost (quite >possible)? When you export a hash, you're not copying it, you're aliasing it. %main::hash is not a COPY of %MyExporter::hash, but an alias to it. The exporting procedure is: my $pkg = caller; # who is exporting us? *{ $pkg . '::hash' } = \%hash; That's basically what happens. -- Jeff "japhy" Pinyan % How can we ever be the sold short or RPI Acacia Brother #734 % the cheated, we who for every service http://japhy.perlmonk.org/ % have long ago been overpaid? http://www.perlmonks.org/ % -- Meister Eckhart
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.