Require versus just including the script

Require versus just including the script

am 14.08.2007 20:14:41 von Bill H

Are there any upsides / downsides of putting commonly used routines in
a seperate module and "require"ing it in different programs versus
just putting the routines in the program other than the smaller
program size and ease of editting? Does using a module slow down
program execution?

Bill H

Re: Require versus just including the script

am 14.08.2007 21:10:45 von it_says_BALLS_on_your forehead

On Aug 14, 2:14 pm, Bill H wrote:
> Are there any upsides / downsides of putting commonly used routines in
> a seperate module and "require"ing it in different programs versus
> just putting the routines in the program other than the smaller
> program size and ease of editting? Does using a module slow down
> program execution?

Upside 1: you obviate the need to copy/paste or rewrite routines in
scripts that need them
Upside 2: if you need to fix/enhance a routine, you only need to do it
once
Upside 3: smaller overall code base (more maintainable/manageable)

also, consider using 'use' rather than require--it includes the code
at compile time instead of run-time like 'require'. (actually, use is
the equivalent of wrapping a require in a BEGIN block.)

Re: Require versus just including the script

am 14.08.2007 21:10:52 von Tim Southerwood

Bill H coughed up some electrons that declared:

> Are there any upsides / downsides of putting commonly used routines in
> a seperate module and "require"ing it in different programs versus
> just putting the routines in the program other than the smaller
> program size and ease of editting? Does using a module slow down
> program execution?
>
> Bill H

It's fine. In fact it's generally a "good idea". You'll get a few more
stat() calls and one more open() call for each module, but if you strace
perl, you see that pales into relative insignificance compared to all the
other stat()s and open()s it needs to do anyway just to get loaded.

If I've had a big project I might have a dozen modules, each handling a
logical group of functionality. For a one off, I've just lobbed the
routines into 3 .pl files, lightly grouped by functionality, just to make
the main program a bit smaller and more readable.

In both cases it's a winner for sanity, in the former, not doing so would
have been totally insane.

Cheers

Tim

Re: Require versus just including the script

am 15.08.2007 02:54:03 von Tad McClellan

it_says_BALLS_on_your forehead wrote:

> (actually, use is
> the equivalent of wrapping a require in a BEGIN block.)


actually, use is the equivalent of wrapping a require and
an import in a BEGIN block.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: Require versus just including the script

am 15.08.2007 13:31:11 von simon.chao

On Aug 14, 8:54 pm, Tad McClellan wrote:
> it_says_BALLS_on_your forehead wrote:
> > (actually, use is
> > the equivalent of wrapping a require in a BEGIN block.)
>
> actually, use is the equivalent of wrapping a require and
> an import in a BEGIN block.

woops! right you are, Tad.