in reply to Re: Share Variable in Script with a Module
in thread Share Variable in Script with a Module

Okay, that does work, but seems to introduce another problem, which I don't quite understand.

The test module, and test script, are shown below:

The module
package my_testmodule; $VERSION = '1.00'; use strict; use warnings; our $gMessage; undef our @list; use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(@list); sub import { my $class = shift; $gMessage = shift; print "Yep, I'm in here ...\n\n"; my_sub(); } # &my_sub; sub my_sub { print "$gMessage\n\n" if ($gMessage); push (@list,'oranges','lemons','apples'); } 1;
The script
#!/usr/bin/perl use strict; use warnings; use my_testmodule 'xyz'; print "$list[0]\n\n"; exit;

Previously, before trying to pass the module anything at all, and thus without the import sub, I would be able to read @list, defined in the module, in test.pl.

However, now, with the import sub, I'll get an error when running test.pl, "Global symbol "@list" requires explicit package name at test.pl line 8."

I don't see why this should be, seeing as it appears to be defined and exported properly in the module; in fact nothing to do with @list has changed.

I did also try referencing @list with the package name, but that doesn't work either.

Any help/suggestions would be much appreciated. Thanks!

Replies are listed 'Best First'.
Re^3: Share Variable in Script with a Module
by tangent (Parson) on Oct 10, 2013 at 14:52 UTC
    By defining an import method in your module you are overriding Exporter's own import method so it never gets called and doesn't export your list. To fix that, add the following line:
    sub import { my_testmodule->export_to_level(1, @_); my $class = shift; $gMessage = shift; print "Yep, I'm in here ...\n\n"; my_sub(); }
    See the Exporter module's documentation for more.

      Thank you very much.

      In fact, I did re-post as a new question, because I figured that this original thread was too far down the list to get any attention, but I received the same answer on each, and it works great now.

      Thank you again.