in reply to Passing a run time string to require

There is a hack that makes "require" work almost natural, although it doesn't reraise fatal errors - UNIVERSAL::require. It makes "require" a method that can be called on any scalar, which is quite convenient if you need to dynamically load modules:

my $module = __PACKAGE__ . "::Plugin\::$class"; if (! $module->require) { warn "$module didn't return a true value:"; croak $@; };

Of course, depending on your actual needs, maybe one of the Plugin modules already does what you want...

Replies are listed 'Best First'.
Re^2: Passing a run time string to require
by rinceWind (Monsignor) on Oct 22, 2004 at 23:29 UTC
    You're right, it is a hack. It's probably useable in tests and standalone scripts. But this is for a CPAN module. I don't want to rely on someone using my module not having an autoloaded method called require. C'est la vie :|.

    Admittedly, a plug-in is pretty close to what I am doing, but in this case, not a plug-in to an existing module (which a CPAN search gives); this is one of my modules. However, the generic modules, such as Module::Pluggable, seem overkill for what I am doing at present.

    --
    I'm Not Just Another Perl Hacker

      You're right, it is a hack. It's probably useable in tests and standalone scripts. But this is for a CPAN module. I don't want to rely on someone using my module not having an autoloaded method called require. C'est la vie :|.
      UNIVERSAL::require( $scalar )
        Not sure what you are alluding to here. If you pull in UNIVERSAL::require, this has polluted the UNIVERSAL name space, and made the require method available everywhere. If you haven't pulled in Schwern's module, Perl will not find the sub UNIVERSAL::require.

        If somebody else has a require method, or rather doesn't actually have one at the moment, but is dependent on AUTOLOAD being called, my calling in UNIVERSAL::require in a completely different module will stop this working, as Schwern's require will get called, not AUTOLOAD.

        --
        I'm Not Just Another Perl Hacker