[Chicago-talk] default methods

Steven Lembark lembark at wrkhors.com
Mon Jul 26 23:08:13 CDT 2004

-- Jim Thomason <thomasoniii at gmail.com>

> package MyClass;
> sub AUTOLOAD {
>   goto &defaultHandler;
> }

Why have a default handler if you have an AUTOLOAD in the
first place? The main use would be something that installs
a newly minted subroutine, e.g.,

    sub AUTOLOAD
        my $name = (split /::/, $AUTOLOAD)[-1];

        my obj = $_[0];

        my $sth = $dbh->prepare( $obj->{$name} );

        my $sub =
            my $obj = shift;
                # caller gets back undef (false) on
                # failure or the result of the query
                # (which may be undef).

                $sth->execute( @_ );

        # where symbolic ref's Do the Deed (tm).

            no strict 'refs';
            *$AUTOLOAD = $sub;

        # doublecheck my syntax on this, it may be
        # just *AUTOLOAD or require braces -- the
        # camel has a working example.

        goto &*AUTOLOAD

i.e., if the object contains a key with the name of your
called subroutine then treat it as sql, prepare a statement
handle, construct a closure on it, install it as
the subroutine to call that statement handle, then dispatch
to it with the current arguments.

This is how the .al items work; see also the Shell module.

Steven Lembark                           9 Music Square South, Box 344
Workhorse Computing                                Nashville, TN 37203
lembark at wrkhors.com                                     1 888 359 3508

More information about the Chicago-talk mailing list