This and other RFCs are available on the web at
  http://dev.perl.org/rfc/

=head1 TITLE

Standardise Function Pre- and Post-Handling

=head1 VERSION

  Maintainer: Jarkko Hietaniemi
  Date: 05 Sep 2000
  Mailing List: [EMAIL PROTECTED]
  Version: 1
  Number: 194
  Status: Developing

=head1 ABSTRACT

A standardised way of registering/unregistering/inspecting pre- and
posthandlers (also known as hooks) for handlers, and possibly also for
code references any, is needed.

NOTE: this RFC does not (yet) propose any particular syntax or
interface.  Its main purpose for now is to point out the potential for
standardising, and to outline the required functionality.

=head1 DESCRIPTION

Many modules [PV:HPPC, JP:HWS, MJD:M] introduce their own way of
either installing/uninstalling generic function handlers [PV:HPPC,
JP:HWS] or installing/uninstalling specific function handlers [MJD:M].
With the more generic approaches one can wrap a function to a
chain/stack of pre- and post-handlers: pre-handlers can do anything
they want to the argument list; post-handlers can do anything they
want to return value list.  To the caller of the original function
there is (should be) no difference.  The [MJD:M] is a special-purpose
prehandler: it caches the return values for idempotent ('pure',
side-effect-free) functions, using the arguments as the key to to 
the cache, after the initial call fetching the return values from
the cache, resulting in a (hopefully) saved computation and faster
function calls. 

All the above support registering and deregistering handler functions
for functions, but in more general case it should be possible to do
the same for code references.  This would be much more orthogonal, and
also kind of mandatory if core functions become referenceable as
[JV:RFC168] proposes.  This would open up a way towards very nifty
features like the ubiquitously-requested URL-aware open().  Just
install your URL-ware prehandler to \CORE::open and off you go.  Want
versioned files?  warn() that also transparently does syslog()?
close() that automatically renames the file to file.YYYYMMDD-hhmmss
after it has been closed?

Prehandlers should stack as BEGIN blocks do; posthandlers should
stack in the reverse order as END blocks do.

There should be a way inspect the pre- and post-stacks of handlers,
and the all handlers should have a symbolic name.  For example,
a handler X may want to install before another handler Y but after
another handler Z.  A meta-handler may want to remove a handler.

=head1 IMPLEMENTATION

The implementation is dependent on what happens to typeglobs in Perl
6, how does one inspect and modify the moral equivalent of the symbol
table?  Also: what will become of prototypes?  Will it become possible
to declare return types of functions?

As pointed out in [JP:HWS] certain intricacies are involved: what are
the semantic of caller()?  Should it see the prehooks?  If yes, how?
How does this relate to the proposed generalized want() [DC:RFC21]?

=head1 REFERENCES

RFC 21: Replace wantarray with a generic want function, by Damian Conway

RFC 168: Built-in functions should be functions, by Johan Vromans

Hook::WrapSub by John Porter, 
http://search.cpan.org/search?dist=Hook-WrapSub

Memoize by Mark-Jason Dominus, 
http://search.cpan.org/search?mode=module&query=Memoize

Hook::PrePostCall by Philippe Verdret,
http://search.cpan.org/search?dist=HookPrePostCall


Reply via email to