in reply to modules as variables

Ok, you can't specify which module to load via @ARGV with use. use basically works its magic at compile time, and has already done its thing before $ARGV[0] is ever determined.

You might be able to do it with require. The documentation for use states the following:

It (use) is exactly equivalent to

BEGIN { require Module; import Module LIST; }

Please consider using variables other than $a and $b. Those variables have special meaning inside sort routines, and you'll create potential for all sorts of havoc if you steal them for your own use.

You know, Perl actually already offers you a better solution anyway. If your need is to supply the module name on the command line, don't rely on @ARGV. Instead, use Perl's command line switch, "-M", like this:

perl -MFoo myscript.pl

See perlrun for details.

An update:
You can probably still use -M even if your module resides along a path not specified in @INC. You can deal with this difficulty using either the -I[directory] switch, or the -f switch, which are also documented in perlrun.


Dave

Replies are listed 'Best First'.
Re^2: modules as variables
by ikegami (Patriarch) on Jun 28, 2006 at 18:50 UTC

    Ok, you can't specify which module to load via @ARGV with use. use basically works its magic at compile time, and has already done its thing before $ARGV[0] is ever determined.

    No, not at all. @ARGV is available at compile time, and Perl is capable of executing expressions such as $ARGV[0] at compile time. The problem is that use expects a bareword for argument, not an expression. The generic workarounds (eval "use $ARGV[0]" and converting from namespace to filename for require) have already been posted.

Re^2: modules as variables
by tcf03 (Deacon) on Jun 28, 2006 at 18:29 UTC
    Thanks.
    $a and $b are just used for the snippet - they arent the vars used.
    Ted
    --
    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
      --Ralph Waldo Emerson

      Neither should they be used in snippets, as they lead to panic in the responses you get. ;)


      Dave