[Chicago-talk] default methods

JT Smith jt at plainblack.com
Mon Jul 26 23:21:24 CDT 2004


Oooh. Thanks for this tip. That's good to know too. I'll definitely have a look at 
Shell.


On Tue, 27 Jul 2004 00:08:13 -0400
  Steven Lembark <lembark at wrkhors.com> wrote:
>
>
>-- Jim Thomason <thomasoniii at gmail.com>
>
>> AUTOLOAD.
>>
>> 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 =
>        sub
>        {
>            my $obj = shift;
>            eval
>            {
>                # caller gets back undef (false) on
>                # failure or the result of the query
>                # (which may be undef).
>
>                $sth->execute( @_ );
>                $sth->fetchall_arrayref
>            }
>        };
>
>        # 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
>_______________________________________________
>Chicago-talk mailing list
>Chicago-talk at mail.pm.org
>http://mail.pm.org/mailman/listinfo/chicago-talk


JT ~ Plain Black

Create like a god, command like a king, work like a slave. 



More information about the Chicago-talk mailing list