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

I'm trying to export scalars from a module as below. MODULE:
use Exporter; use vars qw(@ISA @EXPORT $VERSION); @ISA = qw(Exporter); @EXPORT = qw(Execute GetValue $PACKAGE_REQUEST_ID $BUGNUMBER $KBARTICL +E $DISTMETHOD $MINSP $MAXSP $ISNONINSTALLINGPKG $JOBBRANCHNAME $HOTPA +TCHBINARIES $BINARIESAFFECTED $BUGTITLE); $VERSION = '1.00'; my($PACKAGE_REQUEST_ID, $BUGNUMBER, $KBARTICLE, $DISTMETHOD, $MINSP, $ +MAXSP, $ISNONINSTALLINGPKG, $JOBBRANCHNAME, $HOTPATCHBINARIES, $BINAR +IESAFFECTED, $BUGTITLE); BEGIN { print "Begin has begun\n"; $PACKAGE_REQUEST_ID = "PackageRequestID"; $BUGNUMBER = "bugnumber"; $KBARTICLE = "kbarticle"; $DISTMETHOD = "distmethod"; $MINSP = "minsp"; $MAXSP = "maxsp"; $ISNONINSTALLINGPKG = "isnoninstallingpkg"; $JOBBRANCHNAME = "jobbranchname"; $HOTPATCHBINARIES = "hotpathbinaries"; $BINARIESAFFECTED = "binariesaffected"; $BUGTITLE = "bugtitle"; print "PRI is $PACKAGE_REQUEST_ID\n"; }
I believe this successfully exports the scalars because the interpreter doesn't complain when I access them from a pl file where use strict is enabled. However, when I try to access these variables, they are set to no value:
print "WHAT VALUE? $PACKAGE_REQUEST_ID" . "\n";
prints "WHAT VALUE?\n". What am I doing wrong?

Replies are listed 'Best First'.
Re: Exporting scalars from a module
by leriksen (Curate) on May 03, 2004 at 07:21 UTC
    OK, what you think you are doing is not what you are actually doing...

    The problem is the my(...). This declares a whole bunch of lexically scoped variables, which are completely different to the symbols in the @EXPORT array. Things in the @EXPORT... structures are things in the packages symbol table - and lexically scoped variables are most definitely not in the symbol table.

    I'll say again - the things in the my() are not the same as the things in the @EXPORT array. This is the crux of your issue.

    The quickest solution is to change the my to our (if your perl has this new-ish keyword) or change the my to use vars qw(...) and remove the commas in the list.

    package thingy; use warnings; use strict; use Exporter; use vars qw(@ISA @EXPORT $VERSION); use vars qw($PACKAGE_REQUEST_ID $BUGNUMBER $KBARTICLE $DISTMETHOD $MIN +SP $MAXSP $ISNONINSTALLINGPKG $JOBBRANCHNAME $HOTPATCHBINARIES $BINAR +IESAFFECTED $BUGTITLE); @ISA = qw(Exporter); @EXPORT = qw(Execute GetValue $PACKAGE_REQUEST_ID $BUGNUMBER $KBARTICL +E $DISTMETHOD $MINSP $MAXSP $ISNONINSTALLINGPKG $JOBBRANCHNAME $HOTPA +TCHBINARIES $BINARIESAFFECTED $BUGTITLE); $VERSION = '1.00'; ...
    The longer solution is to read every article in Tutorials on variables. Only then may you claim true mastery.

    +++++++++++++++++
    #!/usr/bin/perl
    use warnings;use strict;use brain;

      Rule #1 of programming: Repetition is evil.
      my @xvars; BEGIN { @xvars = qw($PACKAGE_REQUEST_ID $BUGNUMBER $KBARTICLE $DISTMET +HOD $MINSP $MAXSP $ISNONINSTALLINGPKG $JOBBRANCHNAME $HOTPATCHBINARIE +S $BINARIESAFFECTED $BUGTITLE) } use vars ( qw(@ISA @EXPORT $VERSION), @xvars ); require Exporter; @ISA = qw(Exporter); @EXPORT = ( qw(Execute GetValue), @xvars );

          -- Chip Salzenberg, Free-Floating Agent of Chaos

        How can I enable use strict and not use my to declare the variables? (The version of perl we use does not support the our keyword):

        package ExitCode; my @varsToExport = qw(%exitCodes $EXIT_SUCCESS $EXIT_GENERAL_FAILURE $ +EXIT_CHANGELIST_CONSUMED $EXIT_FUTURE_CHANGELIST); require Exporter; use vars (qw(@ISA @EXPORT), @varsToExport); @ISA = qw(Exporter); @EXPORT = qw(@varsToExport); %exitCodes; # Define exit codes $EXIT_SUCCESS = 0;