quick help message; or delayed 'use <pkg>'

B

badarisj

folks,

in our perl programs we have bunch of 'use <pkg>' that load bunch of
module
to perform the actual function of the program.

But when the user specifies
a simple '-help', it is annoying for the users to have perl program
load everything
and then spit out the help message. so couple of questions:

- is there anything like 'delayed use' ?
- may be i could place the 'use <pkg>' statements
of the modules not related displaying help message inside an 'eval'
so that perl would
load those modules only when help is not specified.
- probably we could go back to use of 'require <file>' ; but that
would have caveat that
we may miss the non-presence of a certain module loaded only under
certain circumstance.

just curious as to if others faced similar problem and how they
addressed it.

thanks,
-badari
 
P

Paul Lalli

in our perl programs we have bunch of 'use <pkg>' that load bunch of
module
to perform the actual function of the program.

But when the user specifies
a simple '-help', it is annoying for the users to have perl program
load everything
and then spit out the help message. so couple of questions:

- is there anything like 'delayed use' ?

Yes.

use Foo;

is exactly identical to

BEGIN {
require Foo;
Foo->import;
}

If you don't want your module loading to happen immeidately, write
those two lines yourself, not inside a BEGIN{} block, whenever you want
them to happen.

That being said, however, I would suggest taking the exact opposite
approach. If you have code you wanted executed before the module
loading, put this code inside a BEGIN{} block before the use Foo;
statements:

BEGIN {
if ($ARGV[0] eq '--help'){
print "To use this program. . . . \n";
}
}
use Foo;
use Bar;
#etc


Paul Lalli
 
B

Brian McCauley

Paul said:
use Foo;

is exactly identical to

BEGIN {
require Foo;
Foo->import;
}

If you don't want your module loading to happen immeidately, write
those two lines yourself, not inside a BEGIN{} block, whenever you want
them to happen.

Good advice.
That being said, however, I would suggest taking the exact opposite
approach. If you have code you wanted executed before the module
loading, put this code inside a BEGIN{} block before the use Foo;
statements:

BEGIN {
if ($ARGV[0] eq '--help'){
print "To use this program. . . . \n";
}
}

I think this is bad advice. This is mostly based on a gut feeling and
not so easy to justify to anyone who can't see "it's just wrong".

Except in "perl -ne" the BEGIN{} block is there for stuff that really
needs to be done at compile time even when not executing the script.

If you really do want to abuse BEGIN{} this way you should at least
check $^C.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top