in reply to Re: Exporting subs from package required in an eval
in thread Exporting subs from package required in an eval

Sorry for grave-digging, but this seems to be the closest thread that search manages to discover.

I'm trying to use a runtime-determined module (a game rules plugin for a game server). I want to import some functions and scalars into main namespace. Both eval "use $modulename"; and "eval require then manually import" as described above import the subs successfully, but fail to import scalars. I could, of course, manage with wrapping scalars into getter-setter functions but something seems funny here... Could anyone explain/help?

main.pl

use strict; use warnings; my $x='Exports'; eval "use $x"; asub(); #fails if I uncomment the next line - Global symbol "$ascalar" require +s explicit package name #print $ascalar, "\n";

Exports.pm

package Exports; use strict; use warnings; use Exporter; use base 'Exporter'; our @EXPORT = qw( $ascalar asub); our $ascalar = 42; sub asub { print "sub called\n"; } 1;
I'm using latest ActivePerl on Windows This is perl, v5.10.1 built for MSWin32-x86-multi-thread (with 2 registered patches, see perl -V for more detail) Copyright 1987-2009, Larry Wall Binary build 1007 291969 provided by ActiveState http://www.ActiveState.com Built Jan 26 2010 23:15:11

Replies are listed 'Best First'.
Re^3: Exporting subs from package required in an eval
by Corion (Patriarch) on May 06, 2010 at 14:10 UTC

    You can't do that. As use strict; does not know that you'll be declaring these variables later on at runtime, it raises an error. Either declare those variables in your main program, or load your plugin at compile time in a BEGIN block. See use.

      I'm unwilling to give up strict, so I decided to declare them in the main program - after all, they are "global" in some important sense, and all plugins could be expected to provide them. It works, of course :) Thanks a lot!