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([EMAIL PROTECTED]) };

No infinite loop involved.

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

    &function.wrap( { try_some_stuff || call([EMAIL PROTECTED]) } );

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

Damian

Reply via email to