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

Ok, here is a quick question about creating a package for my global variables, and then trying to group some of the globals in an array AS WELL AS adding them to @Export. I don't want to have to list them out twice for maintainability reasons. Here is what I tried (note this does NOT work):
package my_global; require Exporter; our $VERSION = 1.00; our @ISA = qw(Exporter); our @EXPORT = @var_arr, qw( $var1 $var2 $var3); our @EXPORT_OK = qw(); @var_arr = qw( $var4 $var5 $var6); return 1;

Justin Eltoft

"If at all god's gaze upon us falls, its with a mischievous grin, look at him" -- Dave Matthews

Replies are listed 'Best First'.
Re: @Export Quickie
by joealba (Hermit) on Dec 14, 2001 at 02:30 UTC
    Move @var_arr up and put parenthesis around the list of exported variables to toss it in a more explicit list context. It's a good idea to put your variables in @EXPORT_OK first, because someday you'll see the light and want to only export those variables that your program asks for. :)
    package my_global; require Exporter; my @var_arr = qw( $var4 $var5 $var6 ); our $VERSION = 1.00; our @ISA = qw(Exporter); our @EXPORT_OK = (@var_arr, qw( $var1 $var2 $var3 )); our @EXPORT = @EXPORT_OK; # Just to test... $var1 = "hi"; $var4 = "bye"; 1;
    And the test code:
    use strict; use my_global; #or <B>use my_global qw($var1 $var4);</B> print "var1: $var1\n"; print "var4: $var4\n"; exit;
      Actually, you should never use @EXPORT unless you know why you shouldn't. :-)

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re (tilly) 1: @Export Quickie
by tilly (Archbishop) on Dec 15, 2001 at 12:13 UTC
    As noted already, the solution is to put parens around the left-hand side.

    However the reason is not something about making the list context more explicit. It is that , has a lower precedence than =. Details at perlop.

    Incidentally warnings would have told you what was wrong, and dragonchild is perfectly correct that you are strongly advised to use @EXPORT_OK only and leave @EXPORT empty.