in reply to Re^3: access to hashes with same name, from different packages with same name
in thread access to hashes with same name, from different packages with same name

Hello almut, I have double-checked the things you mentioned, but they seem to be correct. I repaste the codes I have used. The package is (with its full absolute path):

/home/UTKU/devel/my_script/dir_two/my_package.pm

The content of this package is as follows:
$ cat dir_two/my_package.pm package my_package; require(Exporter); @ISA = qw(Exporter); @EXPORT = qw( %my_hash ); %my_hash = ( 'my_val' => 7 ); print "my_package is called!\n"; 1;

The script that is expected to take this package is following:

UTKU@utkuhome ~/devel/my_script $ cat my_script.pl #!/usr/bin/perl -w use Data::Dumper; use dir_two::my_package qw( %my_hash ); print @INC; print Dumper(\%my_hash);

Now when the script is called, following comes out to stdout:

UTKU@utkuhome ~/devel/my_script $ ./my_script.pl my_package is called! Name "main::my_hash" used only once: possible typo at ./my_script.pl l +ine 10. /usr/lib/perl5/5.8/cygwin/usr/lib/perl5/5.8/usr/lib/perl5/site_perl/5. +8/cygwin/usr/lib/perl5/site_perl/5.8/usr/lib/perl5/site_perl/5.8/cygw +in/usr/lib/perl5/site_perl/5.8/usr/lib/perl5/vendor_perl/5.8/cygwin/u +sr/lib/perl5/vendor_perl/5.8/usr/lib/perl5/vendor_perl/5.8/cygwin/usr +/lib/perl5/vendor_perl/5.8.$VAR1 = {};

Where is my mistake? Update: I have done the thing you recommended, ie. the package name dir_two::my_package;, it works, thanks. I am not allowed to alter the package namespaces. Is there any other way to do it?

Replies are listed 'Best First'.
Re^5: access to hashes with same name, from different packages with same name
by almut (Canon) on Feb 11, 2007 at 16:25 UTC
      I am really sorry, I have now tried your recommendation, and it worked, thank you very much for your patience. Sorry for "posting-pollution" (I added an update to my previous posting that I tried it juts now).

      Is this the only way to access the package contents? I do not permission to change the package namespaces (ie. package my_package; -> package dir_two::my_package; ). I want to leave these stuff untouched.
        Is this the only way to access the package contents? I do not permission to change the package namespaces (ie. package my_package; -> package dir_two::my_package; ). I want to leave these stuff untouched.

        Ok, if you can't (or don't want to) change the .pm files you could try something like:

        use Data::Dumper; use vars qw(%my_package::my_hash); sub my_special_require ($) { my $pkg = shift; require $pkg; # alias %my_package::my_hash to %main::my_hash (aka %my_hash) *my_hash = \%my_package::my_hash; } my_special_require "dir_one/my_package.pm"; print Dumper(\%my_hash); my_special_require "dir_two/my_package.pm"; print Dumper(\%my_hash);

        Or, simply access the hash from within your script fully qualified as %my_package::my_hash

        Update: added use vars ... to make it run cleanly with -w