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

I'm having some issues with dynamically requiring perl mods ( see node:650816 )

I've tried using Module::Pluggable Module::List::Pluggable and a few of my own homebrew ideas - but nothing is working as i want it to. perl either spits errors on finding the files or on addressing package vars
package main; use warnings; use strict; use lib '/'; use com_2xlp::Test::SuperClass ();
package com_2xlp::Test::SuperClass; use warnings; use strict; use Module::List::Pluggable(); our %_REGISTERED_NETWORKS; Module::List::Pluggable::import_modules( "com_2xlp::Test" ); sub register_subclass { my ($package)= @_; print $package; print $package::var; } 1;
package com_2xlp::Test::SuperClass::SubClass; use warnings; use strict; our $var= "var"; com_2xlp::Test::SuperClass::register_subclass(__PACKAGE__); 1;
in the code above ( i created a /com_2xlp dir to simplify namespace issues on testing ) the subclass module will correctly call register_subclass -- evidenced by the print command. however the package vars are uninitialized if i try to address $package::var . similarly, perl will toss errors if i try to require $package or anything similar, unable to find the package.
can anyone give some suggestions on what i'm doing wrong?

Replies are listed 'Best First'.
Re: Dynamically Requiring Perl Modules
by shmem (Chancellor) on Nov 18, 2007 at 12:45 UTC
    print $package::var;

    Nope. You want

    no strict 'refs'; print ${$package.'::var'};
    And
    require $package;

    doesn't work. See require:

    If EXPR is a bareword, the require assumes a ".pm" extension and replaces "::" with "/" in the filename for you, to make it easy to load standard modules. This form of loading of modules does not risk altering your namespace.

    In other words, if you try this:

    require Foo::Bar; # a splendid bareword

    The require function will actually look for the "Foo/Bar.pm" file in the directories specified in the @INC array.

    But if you try this:

    $class = 'Foo::Bar'; require $class; # $class is not a bareword #or require "Foo::Bar"; # not a bareword because of the ""

    The require function will look for the "Foo::Bar" file in the @INC array and will complain about not finding "Foo::Bar" there. In this case you can do:

    eval "require $class";

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      thanks kindly!