Phoenix.pm: package Autoloading

Tran Forsythe forsythe at primenet.com
Fri Oct 22 01:09:11 CDT 1999


Yep, this technique goes over fairly well; there's a number of modules that
load their own sub-modules (ie: Net::FTP & Net::FTP::A) at runtime only.

The only downside is if (like myself) you're cursed with not being able to
expect a Perl installation whereever you go, you must compile it into a
binary.  Aaaand, here's the downfall... such Perl-2-C compilers can only see
what you've included at (Perl's) compile time (by using use/require).  So,
such modules pose a threat to those who have to distribute scripts in
binary.

<steps off his soapbox>

Any flames, pls mail me not the group ;)
-Kurt

ps: Sorry I missed last meeting; sounded like it was interesting.  Me, I
lost a cluster and thought it was next Tues.

------

"Push to test."
<click>
"Release to detonate."
-Brad Morrison

----- Original Message -----
From: Pablo Velasquez <pablo at dosomething.org>
To: <phoenix-pm-list at happyfunball.pm.org>
Sent: Wednesday, October 20, 1999 10:30 AM
Subject: Re: Phoenix.pm: package Autoloading


Tim,
This sounds really intriguing...I wish I knew OOP to participate :)

I did just get the OOP book though (Monday), any minute now it will sink in.

-Pablo

At 10:42 AM 10/20/99 EDT, you wrote:
>I talked about a certain AUTOLOAD technique at the meeting.  There are
>others, but this seemed to suit my particular situation.
>
>My problem was that I wanted to just be able to call
>
>FormVar::Text->new()  # for example,
>
>without having to worry about whether or not the FormVar::Text package
(also
>read file) was required already.  If FormVar::Text is a completely empty
and
>undefined package at the time of calling new(), then you will always get an
>error, because it cant find new, and has no information on where else to
look.
>
>So, my (I'm sure unoriginal) idea was to use an AUTOLOAD routine in a
package
>whose only reason for existence is to AUTOLOAD the FormVar packages.  I
call
>this package LoadMe for lack of a better name.  The LoadMe package AUTOLOAD
>routine parses the package name out of the called method (in the
TOLOAD( 
>variable), requires the package, and then calls the method again.  The 
>package called should then reassign @ISA, since you don't want 'LoadMe' as a 
>parent anymore.
>
>***** The Code *****
>
>package FormVar;
>
># all of the possible packages that could be called.
>@FormVar_Packages = ( qw(
>    FormVar::Text
>    FormVar::Password
>    FormVar::Checkbox
>    FormVar::CheckboxGroup
>    FormVar::Button
>    FormVar::SubmitButton
>    FormVar::Option
>    FormVar::OptionGroup
>    FormVar::Radio
>    FormVar::VarGroup
>    FormVar::ERR
>    FormVar::SelectList
>    FormVar::SelectListMultiple
>    FormVar::Cluster
>    FormVar::Textarea
>    FormVar::Page
>));
>
># To make this work, we need each package already built with an @ISA
pointing 
>to
># 'FormVar::LoadMe'
>
>map { @{"$_\::ISA"} = ('FormVar::LoadMe') }  @FormVar_Packages;  # @ISA will 
>be overwritten in file
>
># Now for the 'LoadMe' AUTOLOAD routine...
>
>package FormVar::LoadMe;
>
>sub AUTOLOAD  {
>    my $self = $_[0];
>    my $class = ref($self) || $self;
># use and eval to capture an error and spit out something more meaningful.
>    eval ("require HTML::$class");
>    $@ and die "AZWEBS ERROR: error in requiring '$class' (error 
>follows)...\n$@";
>
># now lets try the same method call again with the same arguments
># You will get an error if you don't reassign @ISA in the required package
># as this then becomes recursive.
>
>    &{$AUTOLOAD}(@_)
>
>}
>
># Then return to the FormVar package definitions
>package FormVar;
>
>***** End Code *****
>
>If anyone has any questions, give me a buzz!!
>
>Check 6!
>
>Tim
>







More information about the Phoenix-pm mailing list