Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: problem referencing global variable in self-written module

by crashtest (Curate)
on Jun 08, 2006 at 02:36 UTC ( [id://554202]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^2: problem referencing global variable in self-written module
by yburge (Acolyte) on Jun 10, 2006 at 01:42 UTC
    Well, crashtest, you're no dummy! I followed your suggestion, and it worked like a charm!! Thank you SO much!!!
Re^2: problem referencing global variable in self-written module
by yburge (Acolyte) on Jun 13, 2006 at 21:25 UTC
    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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://554202]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-18 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found