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

Folks I need to call a function from a module,for that I will get the name of the module and the function from the database in the run time.(I will also get the path from the DB).My question is, may I know how to get this to work.
  • Comment on getting the name of the module in runtime and calling a function from that module

Replies are listed 'Best First'.
Re: getting the name of the module in runtime and calling a function from that module
by Anonymous Monk on Jun 03, 2002 at 19:48 UTC
    #!/usr/bin/perl -w use strict; my $path = 'path/to/modlib'; my $module = "MyMod"; my $func = "some_func"; require "$path/$module.pm"; eval "${module}::$func()";
      Thanks for your help.I also pass a user defined object as an argument but I seeing the following error messages "cant modify constant item in scalar assignment" "Bareword not allowed whil strict subs in use". So I added "no strict subs" to the code and I could avoid the second message but the first one still persists. The code sample is $obj::Process($Task); (Note: Process is the name of the function which is hardcoded) Any help would be appreciated
        This depends on how exactly you want to call the Process sub: as a class method, an object method or a regular subroutine:
        #!/usr/bin/perl -w use strict; package One; sub ding { print "Ding (".join(',',@_).") called!\n"; } package main; my $classname = "One"; my $obj = bless {},$classname; my $packagename = $classname; $classname->ding('class method'); $obj->ding('object method'); no strict 'refs'; &{$packagename."::ding"}('regular sub');
        Will print:
        Ding (One,class method) called! Ding (One=HASH(0x80fbc2c),object method) called! Ding (regular sub) called!

        -- Joost downtime n. The period during which a system is error-free and immune from user input.
Re: getting the name of the module in runtime and calling a function from that module
by Sinister (Friar) on Jun 03, 2002 at 20:39 UTC
    All modules filenames can be found in the keys of %INC. The values of %INC hold the /path/to/lib

    little-old-me didnt read to carefull, sorry for that!

    er formait hyarya.
    -- "Life is a house and the next tornado is never far away"
    -- "lovely by nature"

      The only problem with this is that %INC is only populated when you "require", "use", or "do". If the script hasn't already loaded the module, that element will not exist in the %INC hash.