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

Sigh, another package problem... I have a script that has "Plugins" I want the script to require the appropriate plugin based on what a user enters. Here's some code
#!/usr/bin/perl -T use strict; use lib '.'; use CGI; my $cgi = new CGI; my $plugin = $cgi->param('p'); if($plugin =~ /^([A-Za-z0-9_]+)$/) { $plugin = $1; } else { die "Bad plugin name"; } require Plugins::$plugin; # ...

But this fails with "bad name after :: in require". I also tried
my $req = "Plugins::$plugin"; require $req;

But this fails saying it can't find the Plugin in @INC even though I've specifically supplied the path to it in use lib. I couldn't find anything in the search that was particularly helpful..

Thanks in advance,
Chris

Replies are listed 'Best First'.
Re: Variables in require
by impossiblerobot (Deacon) on Dec 04, 2001 at 23:43 UTC
    The trick that converts the '::' into directory separators (as well as assuming a '.pm' extension) only works on barewords.
    So you will need to specify the directory and filename explicitly.

    Update: Looking at the other answers, I guess I was wrong. :-)
    Update 2: Excepting tadman's suggestion (using 'eval'), according to perlfunc at least, I was right. I just gave up too easily. :-)

    Impossible Robot
      No, you were right. I had a thinko. (Been a long week, short on sleep.)
Re: Variables in require
by broquaint (Abbot) on Dec 04, 2001 at 23:39 UTC
    Try sticking a '.pm' after it
    my $req = "Plugins::${plugin}.pm"; require $req;
    Check out the require() docs for further info.
    HTH

    broquaint

Re (tilly) 1: Variables in require
by tilly (Archbishop) on Dec 04, 2001 at 23:40 UTC
    require "Plugins/$plugin.pm"; The reason you need to say it this way is in the documentation.

    UPDATE
    Minor thinko.

Re: Variables in require
by belg4mit (Prior) on Dec 05, 2001 at 00:04 UTC
    FYI don't bother trying to compile this script with the Indigo Perl compiler (and quite probably any other), ain't gonna happen. (More specifically it will but the require will fail upon execution).

    --
    perl -p -e "s/(?:\w);([st])/'\$1/mg"