in reply to dynamic loading of modules

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

Replies are listed 'Best First'.
Re^2: dynamic loading of modules
by opensourcer (Monk) on Aug 28, 2006 at 06:37 UTC
    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}