utku has asked for the wisdom of the Perl Monks concerning the following question:

I have two packages with same name, but located in different directories, each of which has a hash with same name again:
dir_one/my_package.pm dir_two/my_package.pm
Both file include following:
##! /usr/local/bin/perl package my_package; require(Exporter); @ISA = qw(Exporter); @EXPORT = qw( %my_hash ); %my_hash = ( ... );
The hashes have the same name in each package but their content is different. Now I want to load both packages during runtime, process the hashes in each and so forth. I have done following:
#!/usr/bin/perl -w use Data::Dumper; require dir_one::my_package q( %my_hash ); print "%s\n", Dumper(\%my_hash); require dir_two::my_package q( %my_hash ); print "%s\n", Dumper(\%my_hash);
Perl interpreter tells me that the syntax of "require" is wrong:
syntax error at ./my_script.pl line 7, near "require dir_one::my_packa +ge qw( %my_hash )" Execution of ./my_script.pl aborted due to compilation errors.
Where is my mistake? How can access the hashes in this way during runtime?

Replies are listed 'Best First'.
Re: access to hashes with same name, from different packages with same name
by Joost (Canon) on Feb 11, 2007 at 02:03 UTC
    require() doesn't import. You need to use "use" or call import() yourself. See Exporter and use. Also, q() does not strip spaces. you generally want to use qw() anyway, since that allows you to easily specify a list of space-separated "words".

    Additionally, you can only have one package-global variable of the same type and the same name in the same package, so the second import will override the first.

    use dir_one::my_package qw(%my_hash); BEGIN { # force this to run before the "use" below print "%s\n", Dumper(\%my_hash); } use dir_two::my_package qw(%my_hash); print "%s\n", Dumper(\%my_hash);

    update: also, print() doesn't do what you seem to think it does. See print and printf/sprintf. in this case,

    print Dumper(\%my_hash);
    will suffice.

    update 2: almut reminded me that since you specified "%my_hash" in @EXPORT (and not @EXPORT_OK) it will get exported automatically, so you don't need to specify it again when importing. You still need to use "use" or call import yourself, though. That would make the final code something like this:

    use dir_one::my_package; BEGIN { # force this to run before the "use" below print Dumper(\%my_hash); } use dir_two::my_package; print Dumper(\%my_hash);
Re: access to hashes with same name, from different packages with same name
by Anonymous Monk on Feb 11, 2007 at 11:25 UTC
      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?

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