in reply to Re^3: "use" modules inside modules
in thread "use" modules inside modules

First, let's simplify your code a bit.

In the .pl file, replace

use Cwd; use File::Basename; BEGIN { my $dir = dirname($0); my $path; if ($dir =~ /^\//) { #--absolute path $path = $dir; } elsif ($dir eq ".") { #-- relative, current dir $path = getcwd(); } else { #-- relative, but not from current dir $dir =~ s/^\.\///; $path = getcwd()."/$dir"; } unshift(@INC, "$path/lib"); unshift(@INC, "$path/lib-perl"); unshift(@INC, "$path/lib-perl/sun4-solaris-64int"); }
with
use Cwd qw( realpath ); use File::Basename; my $script_dir; BEGIN { $script_dir = dirname(realpath($0)); } use lib "$script_dir/lib", "$script_dir/lib-perl";

In the .pm files, remove the following

use File::Basename; use Cwd; BEGIN { my $dir = dirname($0); my $path; if ($dir =~ /^\//) { #--absolute path $path = $dir; } elsif ($dir eq ".") { #-- relative, current dir $path = getcwd(); } else { #-- relative, but not from current dir $dir =~ s/^\.\///; $path = getcwd()."/$dir"; } unshift(@INC, "$path/lib"); unshift(@INC, "$path/lib-perl"); }

It makes no sense to tell perl where to find the library after it's already been found!


Now on to your question.

To determine the presence of an element I use the statement defined($ntCapabilities{$type}{'SW_PAR'}).

From where? The .pl file? There is no such variable there. You would have to export %ntCapabilities in order to do that (and roll out your own import since no module will help you export a lexical variable).

But since exporting variables (as oppose to functions) is rather fishy, it would be better to create an accessor (sub is_capable?) and export the accessor from Capability or use its full name.

if (Capability::is_capable(...)) { ... }

Replies are listed 'Best First'.
Re^5: "use" modules inside modules
by Anonymous Monk on Jun 29, 2009 at 06:10 UTC
    Thanks for the cleanup tip. Now on your question: the call defined($ntCapabilities{$type}{'SW_PAR'} is only done in Capability.pm, nowhere else.

      I requested that you show us sufficient code to demonstrate the problem, but no more than that.

      Your failed on both counts. You did not post sufficient code to demonstrate the problem, and you posted extraneous code that prevents us from running your code and distracts from the issue at hand.

        I'm sorry but this is just impossible: the complete package is over 50000 lines of code and extracting enough code to make you run this is not easy at all (let alone that I would have time to do that which I do not have at all at this moment). On top of that the program is interacting with an external device using Telnet (or SSH) and SNMP which makes it all even more complex.

        I'm only trying to understand how perl "imports" data and handles multiple "imports". As far as I understand the data is in a namespace related to the module (so Capability.pm). If loaded once the name space contains all related to %ntCapabilities. If perl would encounter use Capability once more there should be no need to load again the namespace of the module since it is already there. So this is what I do not understand. If I ommit use Capability from Octopus.pm there is no issue, if I include use Capability in Octopus.pm there is an issue. Also remember that Capability.pm has been loaded from the main program swdl.pl. There is no other data assignment to %ntCapabilities then the definition of the hash itself. All other accesses are simply using (defined($ntCapabilities{...}) to verify whether something is supported by the toolset or access using the various keys used in the hash. This hash is intended to be a act as a constant.