Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

dynamic loading of modules

by opensourcer (Monk)
on Aug 28, 2006 at 06:26 UTC ( [id://569935]=perlquestion: print w/replies, xml ) Need Help??

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

i'm trying to load modules based on conditions(dynamic).I have a code snippet to understand better.
package SearchMachine; $mod1 = "/path/Mysearch.pm"; BEGIN { require qq($mod1); } 1;
when i try to execute i get an error:
Null filename used at test.pl line xx.
BEGIN failed--compilation aborted at test.pl line xx.

Replies are listed 'Best First'.
Re: dynamic loading of modules
by davido (Cardinal) on Aug 28, 2006 at 06:31 UTC

    You're assigning $mod1 a value, but doing so outside of the BEGIN{} block. So any code inside the BEGIN{} block doesn't know about $mod1's value. Example:

    $testvar = "Hello world!\n"; BEGIN{ print $testvar; }

    The output will be...

    Use of uninitialized value in print at mytest.pl line 4.

    Dave

      thanks for ur repply,>/br> i do undertand that, but is there any solution or work-around for this?
        Put your assignment into the BEGIN block.

        package SearchMachine; BEGIN { $mod1 = "/path/Mysearch.pm"; require qq($mod1); } 1;
        or put it into a BEGIN block of it's own:
        package SearchMachine; BEGIN { $mod1 = "/path/Mysearch.pm"; } BEGIN { require qq($mod1); } 1;
        All BEGIN blocks are executed first, in order, before any other code is even compiled. See perlmod, section BEGIN, CHECK, INIT and END. And, for the record, it's spelled dynamic loading ;-)

        --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}
Re: dynamic loading of modules
by strat (Canon) on Aug 28, 2006 at 08:23 UTC
    there is a "workaround": either move $mod1 into the begin-block:
    package SearchMachine; use warnings; use strict; BEGIN { my $mod1 = "/path/MySearch.pm"; require $mod1; }
    or move the require out of the begin block:
    package SearchMachine; use warnings; use strict; my $mod1 = "/path/MySearch.pm"; require $mod1;

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Re: dynamic loading of modules
by aufflick (Deacon) on Aug 28, 2006 at 08:33 UTC
    There's nothing magical about BEGIN blocks - they just get done before (nearly) everything else. Once you know that it will make perfect sense about why $mod1 has no value when you're trying to use it in the require.

    On a related note, I would suggest that you try to find a way to avoid programatic require statements. There's nothing particularly wrong with them, but they reduce the ability for you to easily check whether your code compiles correctly and also means that code inspection tools (like Doxygen automatic documentation generation) won't be able to determine dependencies etc.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://569935]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-03-28 13:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found