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

There are still traps.
package Foo; our $_; for (1..5) { print "$_\n"; }

Replies are listed 'Best First'.
Re^7: the "our" declaration ?!!
by ikegami (Patriarch) on Jan 21, 2009 at 01:42 UTC
    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.

        "our" is one of the best improvements in perl, because one has not to fiddle around anymore with different scoping rules for "my" and "vars" and allows consistent declarations! That's orthogonality (almost) at it's best! Unfortunately the features are ignored by the wide public, thinking it's just a a new name for an old mechanism.

        Anyway there are some traps when our is used with special vars, for instance something like  our $\="\n"; will make B::Deparse fail ...

        lanx:~$ perl -MO=Deparse perl/myour.pl While deparsing perl/myour.pl near line 3, Unexpected our($\) CHECK failed--call queue aborted. lanx@:~$ perl -version This is perl, v5.8.8 ...

        nice for obfuscation ; )

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

        FULL ACK! Especially because really understanding "our" implies really understanding "my".

        Cheers Rolf