in reply to problem referencing global variable in self-written module

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;

Replies are listed 'Best First'.
Re^2: problem referencing global variable in self-written module
by yburge (Acolyte) on Jun 08, 2006 at 20:20 UTC
    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.

        I must be doing something else wrong. I downloaded your code, over-writing the text in my "Fleece.pm" module. The same error greeted me when I tried to run "testuse.pl". Here are more details, though I am unsure if any of them matter. The "testuse.pl" script is in my "C:\Documents and Settings\yburge" folder. When I open a command prompt, that's where I am by default. The "Fleece.pm" module is in the "C:\Perl\site\lib" folder.

        BTW, I noticed in down-loading your code, the "expected" file extension was ".pl". Would that make any difference?
Re^2: problem referencing global variable in self-written module
by yburge (Acolyte) on Jun 08, 2006 at 20:28 UTC
    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.