At 06:48 PM 8/20/00 -0600, Tony Olekshy wrote:
>Dave Rolsky wrote:
> >
> > Tony Olekshy wrote:
> >
> > >       try { fragile(); }
> > >       catch { my $caught = 1; }
> > >       finally { $caught and ... }
> >
> > If all those pieces were in the same scope I think it would still
> > work like this (in Perl5-ish code):
> >
> > {
> >   try { fragile(); # It must be Italian }
> >   my $caught;
> >   catch { $caught = 1; }
> >   finally { $caught and ... }
> > }
> >
> > It's the same as in perl5 with a block:
> >
> > {
> >    print $foo;
> >    my $foo = 1;
> > }
> >
> > This is a compile time error because $foo isn't in scope til its
> > declared.
>
>But in the shared scope, $catch isn't referenced until after the my
>in lexical order.  I'm just worrying about the details of what happens
>during the dynamics of execution.  I think it's more like saying:
>
>     {
>     if ($foo) { my $bar = 1; }
>     $bar and ...
>     }
>
>where the outer block represents this "shared lexical scope" concept.
>For this to work, doesn't storage for $bar have to be allocated and
>initialized to undef at the beginning of the outer block?

Nope.  It should work as though each pair of } ... { in between try { and 
the end of the last finally or catch block isn't there.  Storage for 
lexicals is allocated at compile time, assignment happens at run time.  You 
can coerce some odd-looking behavior out of perl if you exploit this.

However, my memory as to what the current perl behavior is was faulty; 
continue blocks do *not* share the lexical scope of their attached loop 
blocks.  I was misremembering the caveat at the end of this part of perlsyn 
(which says the opposite, and is easily confirmed):

>Perl's C-style for loop works exactly like the corresponding while loop; that
>means that this:
>
>     for ($i = 1; $i < 10; $i++) {
>         ...
>     }
>
>is the same as this:
>
>     $i = 1;
>     while ($i < 10) {
>         ...
>     } continue {
>         $i++;
>     }
>
>(There is one minor difference: The first form implies a lexical scope for 
>variables
>declared with my in the initialization expression.)

So no, what I'm proposing is not the same as anything currently in 
Perl.  But I think it's a good reason anyway (and why shouldn't continue 
blocks share the same scope?  Not so 'lexical', I suppose.  Oh well.)

--
Peter Scott
Pacific Systems Design Technologies

Reply via email to