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

Hi !

I have a module which exports application level variables in EXPORT array, and the variables are defined with 'our' keyword. This works for 5.6.0
e.g.
our test1='test_first';

Unfortunately I have coded a lot of scripts with this and I have to run them on 5.005 which does not support our.

What should be the exact syntax in 5.005 if I want to export the variables?

Also on the sideline I tried to put all these variables in a file say "incl.pl"
#!/usr/bin/perl
$test1='test_first';
$test2='test_second';
1;


Now when I use 'use strict' and then 'require ('incl.pl')' and these variables are not available and Global package error occurs. What could be a workaround for this?

Thanks

Replies are listed 'Best First'.
Re: Exporting variable from Module (5.005)
by jarich (Curate) on Oct 04, 2002 at 06:23 UTC
    Try use vars qw//;. For example:
    #!/usr/bin/perl package fred; use strict; use vars qw/$test1 $test2/; $test1 = "foo"; $test2 = "frobizten"; package main; print "test1 from fred is: $fred::test1\n"; print "test2 from fred is: $fred::test2\n";
    The use vars pragma(?) is better replaced with our in later version of Perl but works in Perl 5.005 which is great for backwards compatibility. :)

    I don't have any advice for why your require isn't working except that I generally advise people to stay away from require. It's a general rule, and sometimes best broken, but every script I've ever had to maintain, that has relied on require hasn't been strict compliant either.

    Hope it helps.

    jarich

Re: Exporting variable from Module (5.005)
by Anonymous Monk on Oct 04, 2002 at 06:54 UTC
    for your first question (exporting variables without our, one option is explained by jarich above. A second option would be to fully qualify your exported variables with the package name, as in:

    package I_am_an_example; use strict; use vars qw(@EXPORT @ISA); use Exporter; @ISA='Exporter'; @EXPORT=qw($jimmy $bob); $I_am_an_example::jimmy="White"; $I_am_an_example::bob="Hope"; 1;

    Your require does not work because with use strict;, you need to declare the scope of your variables. To pull in the incl.pl as above, you would do:

    use strict; use vars qw($test1 $test2); require ('inc.pl'); print "$test1\n$test2\n";

    Also, see the faq on the differences between use and require.

Re: Exporting variable from Module (5.005)
by broquaint (Abbot) on Oct 04, 2002 at 11:51 UTC
    All you need to do is declare the variables at package level which you can do in a variety of ways.
    package MyPkg; # use a pragma use vars qw( $foo @bar %baz ); # or declare the full path $MyPkg::foo = "this"; @MyPkg::bar = qw(that); %MyPkg::baz = ( the => 'other' ); # or declare the variables before 'use strict' $foo = "a string"; @bar = qw( an array ); %baz = ( a => 'hash' ); use strict; ...
    All of the above methods will declare variables into the current package.
    HTH

    _________
    broquaint