Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Vexed $Package::Var Usage

by Anonymous Monk
on Dec 17, 2001 at 03:37 UTC ( [id://132415]=perlquestion: print w/replies, xml ) Need Help??

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

I've got some code; I'm not sure why it isn't working. When the Blah package is in test.pl, it works. When I move it out of the same file, it stops working.
test.pl ------- use Blah; print $Blah::a; Blah.pm ------- package Blah; my $a = 3;
Any ideas? (I know this is bad programming; please spare me the lecture ;)
Thanks!

Replies are listed 'Best First'.
Re: Vexed $Package::Var Usage
by wog (Curate) on Dec 17, 2001 at 04:01 UTC
    my declares lexical scoped variables; they are not visible from outside the block or file in which they are written. To declare variables that can be accessed with the $Package::varname syntax, you need to use vars, or our.
Re: Vexed $Package::Var Usage
by Beatnik (Parson) on Dec 17, 2001 at 03:50 UTC
    Do you end your package with a true value ? (usually a 1; on the last line, uhm before __END__, if you use it).
    package Blah; my $a = 3; 1;
    Update: check perlboot and perltoot for an introduction on writing package, since yours is VERY basic :)
    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
      Guru meditation: 3 is true.

      Hint: what is the return value of '=' ?

      Update: Bigger hint: what is the return value of my $a = 3;?

        I doubt there is a difference between using 1; and 3;. According to the cookbook (IIRC), anything can be used (anything as in barewords). An equal sign, unquoted would generate an error, quoted would work just fine (altho strict would probably freak).

        Greetz
        Beatnik
        ... Quidquid perl dictum sit, altum viditur.
      The true value didn't fix it.

      But thanks for the perldoc refs. I go read them now.
Re: Vexed $Package::Var Usage
by Dylan (Monk) on Dec 17, 2001 at 05:41 UTC

    That is very good at explaining what you want to know, I won't lecture

    my variables, do not have a package name, If I recall Correctly.

    my variables are kinda magical. kiat asked about my variables a while ago. You should read it.
    The right way, the way that will work anyway, is like so:

    Foo.pl ------- #!/usr/bin/perl use Bar; print $Bar::Baz; Bar.pm ------- package Bar; our $Baz=42; -------

    In older version of perl5, instead of our you would do something like:

    use vars qw($Bar); $Bar=42;

    You might also search this site for "Variable scope" or something like that.
    Well, I hope that helps.

Re: Vexed $Package::Var Usage
by Anonymous Monk on Dec 17, 2001 at 04:07 UTC
    Talk about feeling dumb.

    The variable I want to share is actually in the "main" package. Would it just be easier passing the package a ref to the variable than trying to (illegally) import it? (None of the said methods are working..)

    Sorry about that.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-29 11:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found