in reply to Re: 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

Thank you very much for your help. It was a typo in my code that I had written "print" instead of "printf". I succeeded in reading the packages in the current directory where my script is.

However, I am running this case on a Cygwin platform and Perl interpreter cannot find the packages residing other than current directories although I declare them even within @INC. The directory of the said Perl script resides in
/home/UTKU/devel/my_script
The said packages are located in
/home/UTKU/devel/my_script/dir_one/my_package.pm /home/UTKU/devel/my_script/dir_two/my_package.pm
Now the script tries following:
#!/usr/bin/perl -w BEGIN { push @INC, "/home/UTKU/devel/my_script" } use Data::Dumper; use dir_two::my_package; print Dumper(\%my_hash);
Perl interpreter says me that my_hash is used only once, ie. it seems that the package has not been bound:
$ ./my_script.pl Name "main::my_hash" used only once: possible typo at ./my_script.pl l +ine 9. $VAR1 = {};
I know, interpreter takes the current directory into @INC but it also does not work. Can't I define packages with relative paths? The content of the package in /home/UTKU/devel/my_script/dir_two/my_package.pm is like in my first posting:
##! /usr/local/bin/perl package my_package; require(Exporter); @ISA = qw(Exporter); @EXPORT = qw( %my_hash ); %my_hash = ( ... );
Is there any mechanism to switch on verbose messages during compilation/elaboration/linkage time of the interpreter to see what it is doing with the packages?

Replies are listed 'Best First'.
Re^3: access to hashes with same name, from different packages with same name
by almut (Canon) on Feb 11, 2007 at 16:01 UTC

    I think your problem is that you haven't put the right package names in the .pm files, i.e.

    dir_one/my_package.pm --> package dir_one::my_package; dir_two/my_package.pm --> package dir_two::my_package; ^^^^^^^

    BTW, if the hash is the only thing in the packages, you can just leave off any package declarations entirely. Then you also don't need the Exporter stuff, as the hash will end up in the main:: namespace anyway... (However, don't do that, if there's other code in the modules!)

      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?