On Thu, Jan 22, 2009 at 18:35, Owen <rc...@pcug.org.au> 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.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to