Fun With Packages! ---------- Forwarded message ---------- Date: Tue, 6 Jun 2000 22:38:45 -0400 (EDT) From: Jeff Pinyan <jeffp@crusoe.net> Reply-To: japhy@pobox.com To: Charles Huff <...> Subject: Re: namespace problem On Jun 6, Charles Huff said: >file #1 >-------- >$gFoo = "2"; > ><call file#2 somehow> >print $gBar; # should output 8 >File#2 >----------- >... >print $gFoo; # should output 2 >$gBar = 8; And File#2 has a different namespace, right? Well, I think I've solved your problem. :) ### other.pl package Outside; $VAR = 100; sub foo { print $VAR; $VAR *= 2; } ### main.pl $VAR = 300; require "other.pl"; Outside::foo(); # prints 100, sets $Outside::VAR to 200 { my @keys = keys %Outside::; my @values = values %Outside::; @Outside::{@keys} = @main::{@keys}; Outside::foo(); # prints 300, sets $main::VAR to 600 @Outisde::{@keys} = @values; } This assumes, though, that you want to call a FUNCTION in other.pl -- if you want to RUN the entire file "other.pl", but disregard the fact that it's in another package... ### other.pl package Outside; print $VAR; $VAR *= 2; ### main.pl $VAR = 300; { my @to_copy = grep !/^main::/, keys %main::; @Outside::{@to_copy} = @main::{@to_copy}; require "other.pl"; # prints $main::VAR, and makes it 600 @main::{@to_copy} = @Outside::{@to_copy}; } print $VAR; # 600 Of course, if other.pl ISN'T in its own separate namespace, there is nothing to deal with at all, really. Just require() the file, and the variables will be dealt with normally. By the way, the code I've given you is the result of an hour's work. Be glad I did it. ;) -- Jeff "japhy" Pinyan japhy@pobox.com http://www.pobox.com/~japhy/ PerlMonth - An Online Perl Magazine http://www.perlmonth.com/ The Perl Archive - Articles, Forums, etc. http://www.perlarchive.com/ CPAN - #1 Perl Resource (my id: PINYAN) http://search.cpan.org/ ==== Want to unsubscribe from Fun With Perl? Well, if you insist... ==== Send email to <fwp-request@technofile.org> with message _body_ ==== unsubscribe