Re: proposal: binding with a function
by Damian Conway other posts by this author
Jun 23 2005 3:53PM messages near this date
Re: proposal: binding with a function
|
Re: proposal: binding with a function
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:
B=C1RTH=C1ZI_Andr=E1s
Larry Wall
Damian Conway
Ingo Blechschmidt
Juerd
Carl Franks
B=C1RTH=C1ZI_Andr=E1s
B=C1RTH=C1ZI_Andr=E1s
BÁRTHÁZI András
Brent 'Dax' Royal-Gordon
BÁRTHÁZI András
Brent 'Dax' Royal-Gordon
BÁRTHÁZI András
Abhijit Mahabal
|