Scope of variables in a function

Dave Mitchell davem at
Sun Jun 2 12:42:16 BST 2013

On Sat, Jun 01, 2013 at 07:03:18PM +0100, James Laver wrote:
> We did formalise an alternative, in the form of :state, but I don't know
> about how well it's used. It also incurs a performance penalty that
> people are presumably unhappy about (but then one assumes it's also
> incurred by the informal version too…)

I'm not aware of any particular performance penalty:

    $ time perl5180o -E'for (1..100_000_000) { my $x = 1 }' 

    real	0m5.638s

    $ time perl5180o -E'for (1..100_000_000) { my $x }'

    real	0m4.012s

    $ time perl5180o -E'for (1..100_000_000) { state $x = 1 }'

    real	0m3.060s

    $ time perl5180o -E'my $x; for (1..100_000_000) { $x }'

    real	0m2.787s

Yes technically its slightly slower than a bare '$x', but it's a lot
faster than a bare 'my $x';

Please note that ash-trays are provided for the use of smokers,
whereas the floor is provided for the use of all patrons.
    -- Bill Royston

More information about the mailing list