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

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.

Replies are listed 'Best First'.
Re^9: the "our" declaration ?!! (special vars)
by LanX (Saint) on Jan 21, 2009 at 12:24 UTC
    "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

      I respectfully disagree in the strongest of terms on the value of our. See Why is 'our' good? for an explanation of why.

      Incidentally our and my have slightly different rules. One is allowed to use local with our variables but not my ones. Of course that is due to an artificial restriction on my, but still it is a difference.

        I can understand that you are used to vars, and normally I preferre lexicals to packvars.

        Actually I don't know if I really understood "vars" scoping rules, so I "lazily" stick on "our".

        That's maybe a generation conflict, I "grew" up with "our"... ; )

        your right about the missing orthogonality of "local", I really miss the opportunity to use it with lexicals, maybe in a renamed form ("save"?). But IMHO you can't blame it on "our".

        Cheers Rolf