Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^2: subroutine good practise

by Kenosis (Priest)
on Oct 29, 2012 at 19:22 UTC ( #1001408=note: print w/replies, xml ) Need Help??

in reply to Re: subroutine good practise
in thread subroutine good practise

What do you suggest in cases where a module's conditionally needed? For example, a program only needs either Text::Table or HTML::Table, depending upon how that program's called. Should both modules be loaded or conditionally loaded?

For example:

# Conditionally use Modules based on outFormat for ( $config{outFormat} ) { when (TEXT) { eval 'use Text::Table'; die $@ if $@; } when (HTML) { eval 'use HTML::Table'; die $@ if $@; } }

If conditionally loaded if acceptable, would require be better than use in the above case?

Replies are listed 'Best First'.
Re^3: subroutine good practise
by chromatic (Archbishop) on Oct 29, 2012 at 20:02 UTC

    If you must encode this within the module itself, I prefer using something like the if pragma.

    My preference is to pass in the dependency when using the module from the calling code, as it sounds like the calling code is most responsible for deciding which dependency to use.

Re^3: subroutine good practise
by thargas (Deacon) on Oct 30, 2012 at 12:42 UTC

    Have you tried measuring how much difference it will make in the run-time and size of the program to just use both modules? I'm very doubtful that this conditional use will buy you a significant amount of anything, but you're the one who can measure what you're doing.

    It also seems to me that you'd be better served by moving those "use"s to the code which needs them, which should probably be in different modules.

Re^3: subroutine good practise
by tospo (Hermit) on Oct 30, 2012 at 12:04 UTC
    I'd say that depends on whether you would expect that script to use both modules in the end, e.g. if this is in a loop and every time you have a 50% chance of one or the other being needed than you might as well load (use) both modules at compile time with the added advantage of having all your requirements at the top of your script instead of somewhere in a sub.
Re^3: subroutine good practise
by scarytall (Initiate) on Nov 01, 2012 at 15:06 UTC
    Measure. Don't tune for speed until you've measured, and even then don't unless one part of the code overwhelms the rest. ~Rob Pike

    What has measurement shown the difference is between conditionally loading the modules and just loading them all to begin with? If it's not worth measuring, it's not worth optimizing. You then have to weigh that difference in performance against making your code less clear, less maintainable, more complex and less testable.

    I think you'll find that the most straightforward strategy of loading all your modules at the beginning of the file makes the most sense.

    A couple additional notes:

    If your module loads a large number of only loosely-related modules, a code reorganization may be a better strategy. But even that may be more trouble than it's worth unless there are additional reasons to do it.

    If your module is large with lots of subroutines, you might consider adding a comment to each subroutine about what module(s) it uses. That makes it easy to detect if there are any loaded modules that are no longer used in your subroutines.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1001408]
[Discipulus]: chickenman do you know which kind of configuration file do you have? it is some standard?

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-02-20 08:06 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (268 votes). Check out past polls.