in reply to Accessing variables in a runtime-loaded module

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>

Replies are listed 'Best First'.
Re^2: Accessing variables in a runtime-loaded module
by srd (Initiate) on Jun 09, 2005 at 13:19 UTC

    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

        You don't need a package variable: you need an acccessor method. Global variables suck. :)

        my $description = 'foo'; sub description { $description }
        --
        brian d foy <brian@stonehenge.com>