Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: the "our" declaration ?!!

by ikegami (Patriarch)
on Jan 20, 2009 at 18:49 UTC ( [id://737637]=note: print w/replies, xml ) Need Help??


in reply to Re^2: the "our" declaration ?!!
in thread the "our" declaration ?!!

Thats a common but incomplete interpretation.

Correct, but only if you allow for poorly scoped variables. As I see things, my explanation is complete except in buggy programs. Put curlies around your packages when you have more than one in your file.

Replies are listed 'Best First'.
Re^4: the "our" declaration ?!!
by LanX (Saint) on Jan 20, 2009 at 21:46 UTC
    don't know what you mean, the following code is with curlies but produces different output for the two options.
    use strict; no strict "vars"; # option1 #our $x; # option2 $x="main"; { package other; $x="other"; print $x; } print $x; __END__ 1: othermain 2: otherother

    Cheers Rolf

      ...and don't nest your packages. I thought that went without saying.

      If you do nest your packages to share private variables, you should be using my, not our.

        There are still traps.
        package Foo; our $_; for (1..5) { print "$_\n"; }
        with curlies, no nesting:
        use strict; no warnings; #no strict "vars"; # option1 our $x; # option2 { package one; $x="one"; print $x; } { package two; print $x; } __END__ 1: one 2: oneone
        I know that "use warnings" produces to "Use of uninitialized value" for option 1, but that's not the point here.

        Cheers Rolf

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-26 05:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found