On Sat, 02 Apr 2005 09:23:53 -0500, Peter Rabbitson wrote:
> This one is about my Tools.pm which uses Exporter and delivers a set of 5-
> 10 line subroutines for stupud tasks (number rounder, strip commas from
> numbers, commify them back, customized html entity decoder etc etc etc).
> What I am currently doing is automatically exporting ALL these subroutines
> (using fairly descriptive names to avoid clashes) and use Tools; in all
> modules that might benefit from them. Pretty standard. However I do not
> understand if importing just a subset of just the necessary functions is
> beneficial in terms of speed. What I am thinking is that since every single
> function IS going to be used sooner or later by at least one of the modules,
> and since EVERY module is called at least once from the main program over
> its runtime - the entire Tools.pm is being processed anyway and wether I
> import all functions or just a few makes no difference on the entire
> picture. Or am I wrong? 

Confused.  The first "use Tools" will load Tools.pm and cause *all* of the
subroutines to be compiled.  Subsequent "use Tools" won't load Tools.pm
because it's in %INC; but, like the first time, *will* call Tools->import().
That call will result in the names being exported.  Observe:

% cat foo
#!/usr/bin/perl -l
use Bar;
use Foo;

% cat Foo.pm
package Foo;
use Bar;
1;

% cat Bar.pm
package Bar;
sub import { print "Bar import" }
print "Bar load";
1;

% ./foo
Bar load
Bar import
Bar import

> And if I am, and specifying explicit imports for each module is beneficial - 

Not for reason of speed.  For reason of maintainability.  You should only
import names you are going to use, and then do so explicitly, so that a
maintenance programmer doesn't look at the program wondering where
thunk_widgets() has come from.  The exceptions to this rule are few and
far between.

> is Autoloader applicable to this situation? I've seen tons of examples
> of how OO can benefit from calls to non-existent instances, but I didn't
> see a single usage of Autoloader in a non-OO envirnoment which leads me
> to believe that it is not applicable.

I think you're confused again.  If AUTOLOAD() exists in a package it will
be called when a non-existent function is called in that package.  Doesn't
matter whether or not it's O-O.  It isn't going to help you.  I assume you
meant "non-existent methods," because "non-existent instances" is an
oxymoron.

Autoloader.pm allows you to put subroutines in a data block so they are
only compiled when needed (the autosplit function must be used to copy
them to individual files).  I've never used it with the Exporter and don't
know what would happen; at worst, though, you might need to declare stubs
in the main body and then suppress redefinition warnings in the individual
definitions.

Given the case you describe though, I can't see it making any measurable
difference in performance.  If you really need to speed things up there
are more appropriate ways of doing it.  If you don't know you need to
speed things up yet, don't throw wacky devices at the code thinking
they're doing any good :-)

There's more I could say, but I already wrote large parts of a book about
it :-)

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to