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

Yet another question from a floundering Perler-wannabe... I am testing the use of global variables, since my shop has a couple of dozen scripts using the same variables. Here's my test script (named "testuse.pl" in my directory):
#!perl; use warnings; use Fleece; my $BNAME = Mary; print "$BNAME had a little lamb, little lamb, little lamb,\n"; print "$BNAME had a little lamb,\n"; print "It's fleece was $fleece.\n";

Here's the "Fleece" module, which I created:
package Fleece; use strict; use warnings; use Exporter; $fleece = "white as snow.\n";


The Fleece module is in my Perl\site\lib directory (I read somewhere that non-core modules should go in that directory), named "Fleece.pm".

When I try to run the testuse script, I get the following error:
Global symbol "$fleece" requires explicit package name at C:/Perl/lib/ +Fleece.pm line 7. Compilation failed in require at testuse.pl line 5. BEGIN failed--compilation aborted at testuse.pl line 5.
Any suggestions? By the way, I'm running Windows 2000, if that makes a difference.

Replies are listed 'Best First'.
Re: problem referencing global variable in self-written module
by ikegami (Patriarch) on Jun 08, 2006 at 01:09 UTC

    Fleece won't compile, because you're using strict, but $fleece was never declared. Normally, you'd use my $fleece, but Exporter can only export package variables (and subs). That means you need to use our $fleece;.

    Also, you should explicitely return a true value from your module. In this case, it happens to return true by coincidence, but I recommend you add 1; to the end of your module.

    Finally, your script won't work either because you never told Exporter to export $fleece. Add our @EXPORT = qw( $fleece ); to Fleece if you want it exported by default, or add our @EXPORT_OK = qw( $fleece ); to export it on demand.

    use warnings; use strict; use Fleece qw( $fleece ); my $BNAME = 'Mary'; print "$BNAME had a little lamb, little lamb, little lamb,\n"; print "$BNAME had a little lamb,\n"; print "It's fleece was $fleece.\n";
    package Fleece; use strict; use warnings; use Exporter; our @EXPORT_OK = qw( $fleece ); our $fleece = "white as snow.\n"; 1;
      Okay, I updated both script and module as you suggested:

      testuse.pl
      #!perl; use warnings; use strict; use Fleece qw( $fleece ); my $BNAME = 'Mary'; print "$BNAME had a little lamb, little lamb, little lamb,\n"; print "$BNAME had a little lamb,\n"; print "It's fleece was $fleece.\n";

      Fleece.pm
      package Fleece; use warnings; use Exporter; our @EXPORT_OK = qw( $fleece ); our $fleece = "white as snow.\n"; 1;
      I got this error when I tried to execute the script:
      Global symbol "$fleece" requires explicit package name at testuse.pl l +ine 11. Execution of testuse.pl aborted due to compilation errors.
        our @ISA = 'Exporter'; is missing in the module. I forgot about that in my initial post.
        package Fleece; use strict; use warnings; use Exporter; our @ISA = 'Exporter'; our @EXPORT_OK = qw( $fleece ); our $fleece = "white as snow.\n"; 1;

        Tested.

      BTW, I edited the code I posted so it would be more readable (I missed the part in Writeup Formatting Tips about using the break parm). Hope that helps, and thanks for your patience.

        Use
        <c>...</c>
        or
        <code>...</code>

        <c>...<c>
        and
        <code>...<code>
        don't work.

Re: problem referencing global variable in self-written module
by HuckinFappy (Pilgrim) on Jun 08, 2006 at 01:07 UTC
    Oh, to be a monk of high enough ranking to edit your post and add the <code> tags so it would be readable... Regardless...I'll try and help. You did tell perl that Fleece.pm exports things, by:
    use Exporter;
    But nowhere do you tell it *what* to export.
    @EXPORT = qw( $fleece );
    Or better:
    @EXPORT_OK = qw( $fleece );
    This would be slightly better since users would have to specifically ask for $fleece to get it:
    use Fleece qw( $fleece );
    It's considered good form to not clutter of someone's namespace recklessly. I'd suggest a read of the Exporter Documentation would help you understand all this a little better. HTH, Jeff
Re: problem referencing global variable in self-written module
by crashtest (Curate) on Jun 08, 2006 at 02:36 UTC

    Both replies so far explained how to use Exporter to export the $fleece variable into your main script. I thought I'd point out you don't have to do it that way: if you fix the code in the Fleece package as ikegami suggested (i.e., our $fleece = "..."), you can reference $fleece in your print statement by prefixing the package name:

    print "Its fleece was $Fleece::fleece.\n";
    No need for use Exporter, @EXPORT or @EXPORT_OK.

      Well, crashtest, you're no dummy! I followed your suggestion, and it worked like a charm!! Thank you SO much!!!
      Well, crashtest, I couldn't leave well enough alone. Since I also need to test the exchange of scalar variable values between a script and a module, I started with changing the variable name "$fleece" to "$color", and added a variable to the testuse script ($ANIMAL) to be used by the Fleece module in creating a new variable. A weird thing happened when I tried to test the use of $ANIMAL, and the renamed variable "$color". I got these messages:
      Name "Fleece::color" used only once: possible typo at testuse.pl line 13.
      Use of uninitialized value in concatenation (.) or string at testuse.pl line 13.

      Here's the code from the script:
      #!perl; use warnings; use strict; use Fleece qw( $fleece ); my $BNAME = 'Mary'; my $ANIMAL = 'lamb'; print "$BNAME had a little $ANIMAL, little $ANIMAL, little $ANIMAL,\n" +; print "$BNAME had a little $ANIMAL,\n"; print "Its fleece was $Fleece::color\n";
      Here's the code from Fleece.pm:
      package Fleece; use strict; use warnings; our $color = "white as snow.\n"; 1;
      Do you have any idea what my coding error may be?