converting from 'use' to 'require'

J

John

Just checking I'm doing the right thing.

I have a main program (main.pl) and several libraries (LIB1.pm LIBmatrix.pm
LIBgraph.pm) each containing about 50 routines.

I begin:

use warnings;
use strict;
use LIB1;
use LIBmatrix;
use LIBgraph;

There are many options for the user, but no user ever uses all the routines.
The problem is that 'use' will load all the routines when main.pl is
invoked.
As the program has grown I am aware that when main.pl is invoked it is taken
quite a long time to load.

My solution would be to use 'require'. Change LIB.pm to LIB.pl and access
via:

require 'LIB1.pl';
my $x=LIB1::curve($z,$w);

Am I right in thinking that memory usage will fall and loading will be
faster as I only access the routines when requested by the user?

Regards
John
 
C

C.DeRykus

Just checking I'm doing the right thing.

I have a main program (main.pl) and several libraries (LIB1.pm LIBmatrix.pm
LIBgraph.pm) each containing about 50 routines.

I begin:

use warnings;
use strict;
use LIB1;
use LIBmatrix;
use LIBgraph;

There are many options for the user, but no user ever uses all the routines.
The problem is that  'use' will load all the routines when main.pl is
invoked.
As the program has grown I am aware that when main.pl is invoked it is taken
quite a long time to load.

My solution would be to use 'require'.  Change LIB.pm to LIB.pl and access
via:

require 'LIB1.pl';
my $x=LIB1::curve($z,$w);

Am I right in thinking that memory usage will fall and loading will be
faster as I only access the routines when requested by the user?

perldoc autouse


NAME
autouse - postpone load of modules until a function is used


SYNOPSIS
use autouse 'Carp' => qw(carp croak);
carp "this carp was predeclared and autoused ";
....
 
U

Uri Guttman

b> Surely both use and require read-and-parse the file, differing
b> only in wether symbols are imported?

b> How does changing from "use" to "require" speed anything up
b> (unless import is a lot slower than I thought)

require is done at run time but use is at compile time. so if you want
to only load some code when you want it, require works for that. use
would load all the code no matter what. but you do need to handle how to
load code on demand. the autouse module does that as someone else
posted. i bet it puts in trampoline subs which when get called will load
the needed modules and then goto the real sub which should now have
replaced it in the symbol table.

uri
 
J

Jürgen Exner

bugbear said:
Surely both use and require read-and-parse the file, differing
only in wether symbols are imported?

How does changing from "use" to "require" speed anything up
(unless import is a lot slower than I thought)

Simple. Because use() is executed at compile time there is no way _NOT_
to execute it. require() on the other hand is executed at runtime,
Therefore it is possible to control its execution with standard flow
control commands, e.g.

if ($debug) {
require MyDebugFunctions;}

Unless $debug is true there is nothing being loaded or executed.

jue
 
W

Wanna-Be Sys Admin

John said:
Just checking I'm doing the right thing.

I have a main program (main.pl) and several libraries (LIB1.pm
LIBmatrix.pm LIBgraph.pm) each containing about 50 routines.

I begin:

use warnings;
use strict;
use LIB1;
use LIBmatrix;
use LIBgraph;

There are many options for the user, but no user ever uses all the
routines.
The problem is that 'use' will load all the routines when main.pl is
invoked.
As the program has grown I am aware that when main.pl is invoked it is
taken quite a long time to load.

My solution would be to use 'require'. Change LIB.pm to LIB.pl and
access via:

require 'LIB1.pl';
my $x=LIB1::curve($z,$w);

Am I right in thinking that memory usage will fall and loading will be
faster as I only access the routines when requested by the user?

Regards
John

To be clear, with use, you can export/import the routines you want to
use (or only use), it doesn't have to be everything. Or, do you mean
loading a module altogether that won't be used at all? Do you mean
unused routines in a module, or unused modules?
 
J

John

John said:
Just checking I'm doing the right thing.

I have a main program (main.pl) and several libraries (LIB1.pm
LIBmatrix.pm LIBgraph.pm) each containing about 50 routines.

I begin:

use warnings;
use strict;
use LIB1;
use LIBmatrix;
use LIBgraph;

There are many options for the user, but no user ever uses all the
routines. The problem is that 'use' will load all the routines when
main.pl is invoked.
As the program has grown I am aware that when main.pl is invoked it is
taken quite a long time to load.

My solution would be to use 'require'. Change LIB.pm to LIB.pl and access
via:

require 'LIB1.pl';
my $x=LIB1::curve($z,$w);

Am I right in thinking that memory usage will fall and loading will be
faster as I only access the routines when requested by the user?

Regards
John

Thanks gentlemen. I will try

if ($ok) {require LibGraph;}

then

use Class::Autouse {LibGraph}

and see which is better.

I may have to split my three libraries into smaller ones.

Regards
John
 
J

Jochen Lehmeier

Oh, I see. I do that all the time.
I thought you meant *only* changing use to require :-(

You do not need an eval with require to make it conditional. require is
not executed at compile time. See:

~> perl -e 'use XYZZY;'
Can't locate XYZZY.pm in @INC (@INC contains: /etc/perl
/usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5
/usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8
/usr/local/lib/site_perl .) at -e line 1.
BEGIN failed--compilation aborted at -e line 1.
~> perl -e 'require XYZZY;'
Can't locate XYZZY.pm in @INC (@INC contains: /etc/perl
/usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5
/usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8
/usr/local/lib/site_perl .) at -e line 1.
bilbo:~# perl -e 'require XYZZY if 0; print "OK\n";'
OK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top