On 07/09/2007, Chas Owens <[EMAIL PROTECTED]> wrote:
>
> On 9/7/07, Wim Vanderbauwhede <[EMAIL PROTECTED]> wrote:
> > The following program works fine in pugs r17041 (which is the rev of
> > /usr/bin/pugs on feather):
> >
> > my $r=\{say $x+1};
> > my $x=2;
> > $r();
> >
> > With r17041, this gives 3;
> > However, on the latest pugs (r17615 or later), it gives an error:
> > ***
> >     Unexpected "$r"
> >     expecting "=", "::", context, ":" or "("
> >     Variable "$x" requires predeclaration or explicit package name
> >     at pugs_var_bug.p6 line 1, column 4
> > It would think that the r17041 result is correct.
>
> You can't use variables before declaring them, think use strict.

I agree with that, but I am not _using_ the variable, I am just creating a
Code object for later execution.
On execution of the Code object, the variables it accesses should of course
be declared -- which they are.
But on creation of a Code object, that is not necessarily the case.

Even
> if strict weren't in effect this code should not work since the $x in
> the closure is not the one created after the closure:
>
> perl -le 'my$r=sub{print $x+1};my $x = 2;$r->()'
>
> That Perl 5 code prints 1 because undef plus one is one.


I am sorry, but if you put it in a script that Perl 5 code prints 3:
[wv]$ cat test_softref.pl
my $r=sub {print $x+1,"\n" };
my $v='x';
$$v=2;
&$r();
[wv]$ perl test_softref.pl
3
[wv]$


> -------
> > There is also a scoping issue in r17615:
> >
> > my $v=1;
> > if ($v) {
> >         map ->$v {$v},(2);
> > } else {
> >         $v;
> > }
> >
> > With r17041, this gives 2; With r17615 it gives an error:
> > ***
> >     Unexpected end of input
> >     expecting "::"
> >     Variable "$v" requires predeclaration or explicit package name
> >     at pugs_scoping_bug.p6 line 6, column 15
> >
> > Now, if I change $v to $x in the pointy sub, it works fine.
>
> This definitely seems like a bug.  It looks like $v is being destroyed
> after it is used in the map.  It is my understanding that the $v in
> the map should be treated like a temp variable (its old value should
> return after the map).


Well, ->$x {} is a pointy sub, so the variable should only ever be in scope
inside the sub.

Thanks!

Wim






-- 
If it's pointless, what's the point?
If there is a point to it, what's the point?
(Tibor Fischer, "The Thought Gang")

Reply via email to