For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2004 > Unusual Exporting









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 Unusual Exporting
James Edward Gray II

2004-08-03, 9:02 am

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

James Edward Gray II

2004-08-03, 9:02 am

Sorry 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

Jeff 'Japhy' Pinyan

2004-08-03, 9:02 am

On Aug 2, James Edward Gray II said:

>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)?


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

Sponsored Links







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

Copyright 2008 codecomments.com