Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Script Startup Time

by gnu@perl (Pilgrim)
on Sep 17, 2003 at 17:39 UTC ( [id://292191]=perlquestion: print w/replies, xml ) Need Help??

gnu@perl has asked for the wisdom of the Perl Monks concerning the following question:

I have a script (relevent code below) that evaluates some command line options. One of these options prints out the version and exits. The thing I don't like is that it takes a few seconds to print out the version. Not a big deal, but a little irritating.

I know the reason is all of the modules being 'use'ed. I know I could explicitely import the important parts of the modules and that may help, but I'm not sure. If I comment out all the 'use' lines except for 'use Getopt::Long' the './program.pl --version' comes back immediately. This is the type of performance I would like when the whole thing is done.

Does anyone have any ideas on how I could speed this up?

TIA, gnu.

code below

use strict; use IO::Select; use IO::Socket; use Fcntl; use POSIX qw(:signal_h WNOHANG); use Net::SNMP; use DBI; use Sys::Hostname; use Getopt::Long qw(:config pass_through ); $|++; umask(0177); (my $VERSION = '$Revision: 1.23 $') =~ s/\$(.+) \$/$1/; my $MAXCHILDREN = 10; my $inputFile = "./mping.lookup"; my $path = "./unix_socket"; my $OID = '.1.3.6.1.4.1.15102.2.1.1.1'; my $interval = 60; my %source; my %failures; my $dump = 0; my $run = 0; my $version = 0; my %options = ( 'version' => \$version, 'children=i' => \$MAXCHILDREN, 'input' => \$inputFile, 'socket' => \$path, 'dump' => \$dump, 'run' => \$run, ); $SIG{TERM} = $SIG{INT} = sub { unlink $path; exit 0 }; GetOptions(%options); if (${$options{dump}}) { print $VERSION,"\n"; print "There are $MAXCHILDREN child processes by default\n"; print "Input file is ",${$options{input}},":\n"; print "Comm socket is ",${$options{socket}},":\n"; exit if ! ${$options{run}}; } if (${$options{version}}) { print $VERSION,"\n"; exit if ! ${$options{run}}; }

Replies are listed 'Best First'.
Re: Script Startup Time
by liz (Monsignor) on Sep 17, 2003 at 17:49 UTC
    It's all a matter of order and where to BEGIN: ;-)
    use strict; my $VERSION; my $MAXCHILDREN; use Getopt::Long qw(:config pass_through ); BEGIN { ($VERSION = '$Revision: 1.23 $') =~ s/\$(.+) \$/$1/; $MAXCHILDREN = 10; my %options = ( 'version' => \$version, 'children=i' => \$MAXCHILDREN, 'input' => \$inputFile, 'socket' => \$path, 'dump' => \$dump, 'run' => \$run, ); GetOptions(%options); if (${$options{dump}}) { print $VERSION,"\n"; print "There are $MAXCHILDREN child processes by default\n"; print "Input file is ",${$options{input}},":\n"; print "Comm socket is ",${$options{socket}},":\n"; exit if ! ${$options{run}}; } if (${$options{version}}) { print $VERSION,"\n"; exit if ! ${$options{run}}; } } use IO::Select; use IO::Socket; use Fcntl; use POSIX qw(:signal_h WNOHANG); use Net::SNMP; use DBI; use Sys::Hostname; $|++; umask(0177); my $inputFile = "./mping.lookup"; my $path = "./unix_socket"; my $OID = '.1.3.6.1.4.1.15102.2.1.1.1'; my $interval = 60; my %source; my %failures; my $dump = 0; my $run = 0; my $version = 0; $SIG{TERM} = $SIG{INT} = sub { unlink $path; exit 0 };

    This is untested. But the basic idea is that the code you need to execute so you can show the Version info, is moved to a BEGIN {} block, before you load any of the other modules you need.

    Liz

      Yeah, I had that, but I like to try and keep all my 'use' lines together. This does work by the way. Thanks.
Re: Script Startup Time
by sandfly (Beadle) on Sep 17, 2003 at 21:11 UTC
    merlyn suggested the use of autouse in a different node. It looks like a better fit here. From the documentation, it appears to do exactly what you want - defers the cost of useing modules until you need them.
    (I have never used autouse, so I can't say how reliable it is.)
Re: Script Startup Time
by ronzomckelvey (Acolyte) on Sep 24, 2003 at 03:58 UTC
    Can't you just put the use statements inside of an eval() statement?

    I've got code that doesn't know what modules to load until it actaully runs, and loads them through the eval.

    ronzo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://292191]
Approved by blue_cowdawg
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-25 04:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found