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

...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.

Replies are listed 'Best First'.
Re^6: the "our" declaration ?!!
by tilly (Archbishop) on Jan 20, 2009 at 23:02 UTC
    There are still traps.
    package Foo; our $_; for (1..5) { print "$_\n"; }
      I didn't know that, thanks. But why would you do our $_;? It doesn't work for any other variable (because one would use my on the loop variable if it wasn't $_).
        Someone could do our $_; because they thought that if declaring things is good then it is better to declare everything. Or (admittedly more likely) they could do it to prove a point.

        You are right that the specific code example I gave only works for $_. But some variant of the trap works on any global variable that is normally assumed to be in package main no matter what package you are in. Like @_, %ENV, or %SIG.

        In any case this is an example of why it is better to have a precise understanding of how features work rather than having a vague impression that is going to mostly work, most of the time.

Re^6: the "our" declaration ?!!
by LanX (Saint) on Jan 20, 2009 at 22:42 UTC
    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

      You're forgetting about main, where the our is located
        But we are talking about refactoring "no strict vars" into "our", IMHO most people would place "no strict vars" into the top scope, if there are multiple packages...

        your rule of a thumb is over-simplified, and is not getting better with additional side-rules for curlies and forbidden nesting...