Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Unusual Exporting
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


Report this thread to moderator Post Follow-up to this message
Old Post
James Edward Gray II
08-03-04 02:02 PM


Re: Unusual Exporting
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


Report this thread to moderator Post Follow-up to this message
Old Post
James Edward Gray II
08-03-04 02:02 PM


Re: Unusual Exporting
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


Report this thread to moderator Post Follow-up to this message
Old Post
Jeff 'Japhy' Pinyan
08-03-04 02:02 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:36 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.