Re: [perl #113930] Lexical subs

2012-07-08 Thread Moritz Lenz
On 07/08/2012 09:57 PM, Father Chrysostomos via RT wrote:
> my $x;
> my sub f { say $x }
> for 1..10 -> $x { f(); }

It prints

Any()
Any()
Any()
Any()
Any()
Any()
Any()
Any()
Any()
Any()

(because Any is the default value in uninitialized variables).

As an aside, you can run short Perl 6 scripts on IRC (on irc.perl.org
and irc.freenode.org) with something like

/msg p6eval p6: my $x; sub sub f { say $x }; for 1..10 -> $x { f() }

This runs it through both rakudo and niecza.

If you want, I can also send the bot into #p5p.

Cheers,
Moritz


Re: [perl #113930] Lexical subs

2012-07-08 Thread Damian Conway
> But by using the term ‘variable’, which is ambiguous, you are not
> answering my question! :-)

Sorry. I tend to think of *every* variable name as merely being
an alias for some underlying storage mechanism. ;-)

> Does
>
> my $x;
> for 1..10 -> $x {}
>
> cause the existing name $x to refer temporarily to each of the numbers,
> or is a new $x name created?

A new one is created (each time through the loop).


> What does this do?
>
> my $x;
> my sub f { say $x }
> for 1..10 -> $x { f(); }

It prints 'Any()' ten times (i.e. the equivalent of printing ten Perl 5 undefs).

The two $x's definitely exist at the same time during the loop.
For example, this:

my $x = 'outer x';
my sub f { say $x }

for 1..10 -> $x {
print $x, ": ";
f();
}

prints:

1: outer x
2: outer x
3: outer x
4: outer x
5: outer x
6: outer x
7: outer x
8: outer x
9: outer x
10: outer x


Damian