Require versus just including the script

B

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
 
I

it_says_BALLS_on_your forehead

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.)
 
T

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
 
T

Tad McClellan

it_says_BALLS_on_your forehead said:
(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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top