From: Branden [mailto:[EMAIL PROTECTED]]
> 
> 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?
> 
> Instead of:
> 
>  :  pre abc  {  do_pre_handler(@_);  }
>  :  post abc {  do_post_handler($_[-1]); }

Because the {...} following pre/post name... are the _real_ subs. There is
no need for &do_pre_handler(@_) and &do_post_handler. On invokation of &abc,
both the anonymous subroutines for pre and post get passed the argument list
for the actual subroutine invokation, and $_[-1] which holds the eventual
return value.

I suppose the $_[-1] is there to avoid adding a new keyword like &value.
This is all very similar to the Class::Contract module originally authored
by Damian Conway... which actually does allow you to use &value within a
post-condition instead of $_[-1].

You might check out the Class::Contract::_methods code to see how he's done
it. It already provides most of the functionality required by RFC 271.
-Though the circumstances are slightly different in Class::Contract, where
pre and post correspond to checks made before and after invoking a method
where you don't want either to be able to effect the actual invocation of
the subroutine, it's arguments, or return value.

> why not make it:
> 
>  :  pre_post_handler abc {
>  :     do_pre_handler(@_);     # pre part
>  :     my $result = REAL_SUB::abc(@_);
>  :     do_post_handler($result);
>  :     return $result;
>  :  }


I believe the idea which you're trying to get across, is already assumed for
implementation purposes. The actual invokation of a subroutine would call
all the pre's, implementations, and post's with the specified arguments.
But, there are a whole lot more features that need to be packed in there:

o  early exit when a pre specifies a result
o  passing exceptions back up the the appropriate calling scope
o  returning the want'ed return value
o  Inheritence which strenghtens post's and weakens pre's
o  named pre's and post's
o  etc.

Personally, I wouldn't mind another keyword like &value instead of $_[-1]...
I wonder if it'd be possible in Perl 6 to have keywords exist only within
certain context. -Like within C<pre> and C<post>?

I.e., read the rest of RFC271... The best examples I can point to are to
read up on Design-By-Contract or Class::Contract.

Garrett

Reply via email to