Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: subroutine good practise

by chromatic (Archbishop)
on Oct 29, 2012 at 18:49 UTC ( #1001404=note: print w/replies, xml ) Need Help??

in reply to subroutine good practise

I find it easier to understand a file when all of its use statements appear together near the start of the file.

Improve your skills with Modern Perl: the free book.

Replies are listed 'Best First'.
Re^2: subroutine good practise
by Kenosis (Priest) on Oct 29, 2012 at 19:22 UTC

    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?

      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.

      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.

      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.
      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://1001404]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2018-06-22 20:01 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.