Branden wrote:

   > I was reading RFC 271 and thinking about this pre/post handler
   > thing. Why instead of having 2 subs, one for pre and other for post
   > condition, and having to deal with things as strange as $_[-1], why
   > don't we have only one handler that calls the real sub?

Because, by specifying the handlers separately, all the messy
implementation details are automated and hidden. A single handler would
foist them back on the programmer. One of the main uses (possibly *the*
main use) of pre and post handlers is in Design-by-Contract OO
programming, where there can be dozens or hundreds of handlers required.

And -- more compellingly -- because the inheritance semantics of
pre and post handlers are distinctly different. See:

        http://dev.perl.org/rfc/271.html#Inheritance_of_postfix_handlers
        http://dev.perl.org/rfc/271.html#Inheritance_of_prefix_handlers


In any case, the RFC already allows for the kind of all-in-one handlers you
suggested (and still with less coding required! ;-)

For example:

   >    sub abc_handler {
   >        my $real_abc = shift;
   >        do_pre_handler(@_);
   >        my $result = $real_abc->(@_);
   >        do_post_handler($result);
   >        return $result;
   >    }


is just:

        pre abc {
            my $real_abc = (caller(0))[10];
            do_pre_handler(@_);
            $_[-1] = $real_abc->(@_);
            do_post_handler($_[-1]);
        }

Damian

Reply via email to