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

Hi, I'm trying to load a module at runtime and then accessing a variable within that module. Loading the module and importing the symbols seems to work fine, but I'm having difficulty with the syntax of accessing the variable.
my $module_name = sprintf "Plugin::",$plugin_name; eval "require $module_name"; die if $@; $module_name->import(); print "Plugin $module_name describes its purpose as:\n"; #syntax problem in the following line print $module_name->description, "\n";

$module_name->description obviously tries to invoke a subroutine called description in the relevant package.

$$module_name->description or ${$module_name}->description would act as if $module_name was a ref.

I'm a bit at a loss here. What would the correct syntax be? (Is there a correct syntax, or would I be forced to interact with the plugins through routines?)

Thanks in advance!

Replies are listed 'Best First'.
Re: Accessing variables in a runtime-loaded module
by brian_d_foy (Abbot) on Jun 09, 2005 at 13:06 UTC

    What's the error message that you get? That problematic line works for me.

    #!/usr/bin/perl use lib "/Users/brian/Desktop"; require "Foo.pm"; $module = 'Foo'; print $module->description, "\n";
    where Foo.pm is
    package Foo; sub description { "Hello there!" } 1;
    --
    brian d foy <brian@stonehenge.com>

      Ah, but I'm trying to use this as plugin:

      package Foo; use vars( qw (@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION) ); require Exporter; @ISA = qw( Exporter ); @EXPORT = qw( $description ); @EXPORT_OK = (); %EXPORT_TAGS = (); $VERSION = "0.1"; my $description="Plugin I am!" 1;

      So Foo::description is a scalar, not a subroutine. And when trying to access the variable content perl says that it's undefined.

        lexical variables can never be accessed from outside your package/file. You need a package global variable, eg. our $description to be able to access it.
        See perlmod for more on packages and modules.

        Paul

Re: Accessing variables in a runtime-loaded module
by splinky (Hermit) on Jun 09, 2005 at 13:31 UTC
    The correct syntax for accessing a variable inside another package is $pkgname::varname, but note that this only works if varname is a package variable. If it's lexical (my $varname), then you can't get to it at all from outside the package.

    Given that you have the package name inside a variable, things get a little messy. I couldn't find a way to do it without "eval":

    print eval("\$${module_name}::description"), "\n";
      Ok, thanks. I'll switch to using package variable then. Less pain all around.
Re: Accessing variables in a runtime-loaded module
by Samy_rio (Vicar) on Jun 09, 2005 at 14:12 UTC

    Hi, I am beginner in perl. To my knowledge we have to create an object before to invoke a subroutine for that package.

    For Example:

    my $module_name = sprintf "Win32::".$ARGV[0]; eval "require $module_name"; die if $@; $module_name->import(); $W1 = new Win32::GUI::Window(-name => "W1",-title => "Main Window",-s +ize => [ 300, 200 ],); $W1->Show(); Win32::GUI::Dialog(); sub W1_Terminate { return -1; }

    I don’t have much idea about Plugin, so, I tried this example in GUI.

    Regards,

    Samy_rio

      Well, my question is aiming at package variables, not subroutines. Subroutines aren't that hard. Variables are syntactically tricky, subroutines relatively easy.