in reply to Module and software path

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!

Replies are listed 'Best First'.
Re: Re: Module and software path
by Anonymous Monk on Mar 17, 2003 at 19:12 UTC
    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!
        # ./test.pl Bio.pm did not return a true value at ./test.pl line 7. BEGIN failed--compilation aborted at ./test.pl line 7.
        That's the error I am seeing when I plugin your code in my module. In fact, I commented everything out of my module but just your code, so it should print the path, but I am getting this error instead. What is going wrong?

        test.pl is the file calling the module. It contains:

        use Bio path => "/path/to/software";