Re: use vs require



On Thu, Jan 22, 2009 at 18:35, Owen <rcook@xxxxxxxxxxx> wrote:
Hi Chas,

Can you give me an example when one would be used over the other? So,
is
require used more for efficiency, so we load the module only if we
need it?
Thanks,
-Ben


Bit of a conundrum there, if you don't need a module, why include it
in your program.
snip

Happens quite often to me. If I am running on a UNIX machine, I don't
need several Win32:: modules, and I don't want to even try to load
them, but keeping two versions of the script (one for Win32 and one
for UNIX-like machines) is too much trouble. See also the Factory
pattern in my previous email.

snip
Anyway you might have your own local subroutines to suit your
environment and procedures and may or may not be used by your main
programs . Place them in a perl program and register the program with
a 'require' statement.
snip

That is the old, bad way of doing things. Don't do it. Build a
module and use use; it isn't hard. The require function should only
be used when it is necessary to load modules at runtime.

snip
If you get real keen, you can make a private module out of your local
subroutines.

I have a general purpose perl program where I lump all my odd
requirements, the main one being 'trim' (a la php) for stripping
leading and trailing spaces and some odd date formatting for my own
use.
snip

This is the sort of thing I hate to see when I show up at a job.
Someone has a pet file with all of their little odds and ends. And
trim is one of the most useless functions you could write. What is so
hard about

$string =~ s/^[ ]*(.*)[ ]*$/$1/;

True, it takes more characters, but I know exactly what is meant by
it. If it is someones pet version of trim I have to go hunting for it
to find out if they are trimming on the right, left, or both; does it
work on whitespace, spaces, tabs, newlines, or some combination; does
it modify in place, return a copy, or both; etc. Now, if you
absolutely have to a trim function, don't reinvent the wheel, there
are perfectly good modules on CPAN such as Text::Trim and
String::Strip that are tested and documented. Remember, you are not
just writing code for yourself, you are writing it for the sociopathic
axe-murder who will work on it after you.

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
.



Relevant Pages

  • Re: How to quickly turn on/off N-FET switching high side of 55V?
    ... John Fields wrote: ... With the load looking like a couple of ohms it won't make any ... With the switch turned off, any current in the driver will have ... Version 4 [snip ' ...
    (sci.electronics.design)
  • Re: PID question
    ... Each sample you take must be used as quickly as possible, to minimize time delay inside your control loop. ... especially with load impedances other than for what the control loop tuning was optimized. ... Because it is digital he KNOWS the voltage. ...
    (sci.electronics.design)
  • Re: Which online RPG? - AOnline gameplay cont...
    ... entrails of the porn spammer to utter "The Augury is good, ... is load & patch the game on the broadband PC, ...
    (comp.sys.ibm.pc.games.rpg)
  • Re: PID question
    ... minimize time delay inside your control loop. ... Then he connects some load and the sampled ... Because it is digital he KNOWS the voltage. ...
    (sci.electronics.design)
  • Re: New install window trim 1/4" from wall
    ... and a nail was driven into the trim of the ... Offers very little support. ... to support the stool. ...
    (alt.home.repair)