Scope of variables in a function
tech at gordonbanner.me.uk
Sat Jun 1 19:58:16 BST 2013
I've seen this "feature" in action on about 3 occasions ... each time
after someone spent a couple of frustrated hours debugging. I have also
seen it mentioned as a "cunning idiom", before we had the state keyword.
I suspect that it's used far more often by accident than cunning plan
... and that using it intentionally is pretty much obfuscation (as well
as relying on undefined behaviour).
It's all very well to say RTFM, but I don't think every user should have
to RT *whole* FM before starting to code, just the bits they intend to
use. And this is not something a beginner will notice and internalise
while reading about "my" and "if" for the first time, even if perlsyn is
the tutorial(!) they start with.
The problem is this is kind-of in-between documentation ... faced with
that behaviour, what would you search for? It's not like there's a
mysterious foo() call that you could track down.
My vote would be to actively deprecate the usage, start generating
warnings, and suggest that it won't compile under "use strict" in a
release or two...
On 01/06/2013 19:15, Anthony Lucas wrote:
> On 1 June 2013 18:58, Andrew Beverley <andy at andybev.com> wrote:
>> I try to avoid separately declaring variables where
>> possible, as IMHO it clutters the code.
> Cluttered !== Less Lines of Code
> I would suggest re-adjusting your concept of cluttered.
> I would call any statement including a declaration, doing anything other
> than declarations to be the more cluttered option.
> On 1 June 2013 19:03, Andrew Beverley <andy at andybev.com> wrote:
>> Thanks for that. Obviously I should have RTM, but it does seem strange
>> that something that is not recommended does not produce any sort of
> It's not that it's not recommended, it's just that it's usually not what
> you meant. There's nothing objectively wrong with not reinitialising a
> variable on every call into your function.
More information about the london.pm