ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl6-language
perl6-language
Re: return() in pointy blocks
by Piers Cawley other posts by this author
Jun 8 2005 7:01AM messages near this date
Re: return() in pointy blocks | Re: return() in pointy blocks
"TSa (Thomas Sandlaß)" <Thomas.Sandlass@[...].com>  writes:

>  Piers Cawley wrote:
> > My preference is for:
> >     Boo
> >     Boo
> >     Can't dereferene literal numeric literal 42 as a coderef.
> 
>  How do you reach the second 'Boo'? Iff -> does not create a Sub
>  but a Block instance then Luke's code can be interpreted as a
>  much smarter version of

I really wish you'd quote in more detail, it makes it a real PITA to go back
and find the explanatory code.  

    sub foo () {
      return ->  { return 42 }
    }

    my $code =   foo();
    #          ^--- continuations points to the RHS of the assignment
    say "Boo!";
    $code();

So, this is what happens.

1. foo() returns a coderef to the RHS of the assignment.
2. The coderef gets assigned to $code.
3. say "Boo!"
4. We invoke the coderef, which returns 14 to continuation which was current
   when it was created.
5. That means 42 gets returned to the RHS of the assignment
6. say "Boo!"
7. Try to invoke the literal 42. 
8. Die.

In other words, it outputs:

   Foo
   Foo
   # dies

> 
>      sub foo()
>      {
>          enter: 42;
>          if $?RETURN_LABEL { goto $?RETURN_LABEL }
>          return;
>      }
> 
>       say "Boo!";
>       say goto foo::enter; # goto sets up $?RETURN_LABEL
>       say "after goto";
> 
>  which of course prints
> 
>      Boo
>      42
>      after goto
> 
>  The smartness is in the value that &prefix:{'->'} returns
>  while in the snippet above it is explicitly coded.
> 
>  Or do I completely misunderstand the distinction between
>  blocks and closures?

It seems so.
Thread:
Piers Cawley
=22TSa_=28Thomas_Sandla=DF=29=22

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved