develooper Front page | perl.perl6.language | Postings from June 2005

Re: proposal: binding with a function

Thread Previous | Thread Next
From:
Damian Conway
Date:
June 23, 2005 15:53
Subject:
Re: proposal: binding with a function
Message ID:
42BB3C19.8040704@conway.org
Piers Cawley wrote:

> Here's a rubyish idiom:
> 
>   my &old_behaviour := &function;
> 
>   &function := sub { try_some_stuff || &old_behaviour }
> 
> Except, with binding it doesn't work like that, you end up with an infinite
> loop. 

But this version *should* work correctly:

     # Bind the name '&old_behaviour' to the closure that is currently bound to
     # the name '&function'...
     my &old_behaviour := &function;

     # Rebind the name '&function' to the specified closure, calling the
     # old behaviour (via its new name) if necessary...
     &function := sub { try_some_stuff || old_behaviour(*@_) };

No infinite loop involved.

Perl 6 even has specific syntactic sugar for this kind of thing:

     &function.wrap( { try_some_stuff || call(*@_) } );

See: http://dev.perl.org/perl6/doc/design/syn/S06.html#Wrapping

Damian


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About