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

Greetings,

I am creating a module that works with a commercial software package. The problem is that for the module to work right, the module must be given the location of the software on the server (i.e. its path). What's the proper way of doing this? I thought of declaring the path in a variable before using the module. In other words, something like this:

$softwarepath = "/usr/local/etc/software"; use module;
That didn't work. The module couldn't see the variable. Any ideas? Thanks.

Replies are listed 'Best First'.
Re: Module and software path
by jasonk (Parson) on Mar 17, 2003 at 19:07 UTC

    Your problem may come from the fact that the 'use' statements are evaluated at compile time, so the variable wasn't actually defined yet at the time the module was used. If that is the case, you can delay using the module either by wrapping it in an eval, putting the variable declaration in a BEGIN block, or doing require/import on the module yourself, instead of using use, or if the module is your own code (or you are willing to modify it), you could pass the path as part of the import...

    # wrap the initial declaration in a begin block BEGIN { $softwarepath = "/usr/local/etc/software"; } use module; # or eval the use, so it happens at run time $softwarepath = "/usr/local/etc/software"; eval { use module; } # or do the require/import yourself $softwarepath = "/usr/local/etc/software"; require module; module->import(); # or modify the module to accept the path as an option use module path => "/usr/local/etc/software";

    We're not surrounded, we're in a target-rich environment!
      I like the last option.... especially if other people are going to use my module..When you have this:
      use module path => "/usr/local/etc/software";
      How exactly is the path being passed to the module? Is the path a scalar variable? Why do you use the => sign?

        If the module is a subclass of Exporter (most are), then when you use it, it's import() method gets called, so in the module you would put:

        package module; use vars qw/@ISA/; @ISA = qw(Exporter); my $path; sub import { my $self = shift; my %args = @_; # of course you would do something more useful than # just printing it print "The path is $args{path}\n"; # at the very least you probably want to store it # somewhere you can get at it later. $path = $args{path}; }

        The arguments after use are passed to import directly as a list, the => sign has the same meaning as a comma, but I like it because it makes it more obvious that the /usr/local/etc/software is related to the path.


        We're not surrounded, we're in a target-rich environment!
Re: Module and software path
by webfiend (Vicar) on Mar 17, 2003 at 18:56 UTC

    Maybe add $softwarepath to @INC, which tells Perl where to look for modules when applying use.

    push(@INC, "/usr/local/etc/software"); use module;

    I just realized that I was using the same sig for nearly three years.

      That doesn't work at all. I don't think you understand the problem. The module needs to know where the software is located on the server, in order to work with it (ie. open certain files, etc).