Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
And what would you (they) do if a module is subsequently needed in another subroutine in the same file?

Unless the other subroutine is in a different package it doesn't matter whether it also "use"s the module or not, it's loaded and will be available (I think you are probably aware of this, just putting it in as clarification). See this example

#!/usr/bin/perl use strict; use warnings; mainsub(); secsub(); thirdsub(); Frob::fourthsub(); sub mainsub { use Data::Dumper; } sub secsub { my $r = "secsub"; print Dumper $r; } sub thirdsub { use Data::Dumper; my $r = "thirdsub"; print Dumper $r; } package Frob; sub fourthsub { my $r = "thirdsub"; print Dumper $r; }
Name "Frob::Dumper" used only once: possible typo at line 31 +. $VAR1 = 'secsub'; $VAR1 = 'thirdsub'; print() on unopened filehandle Dumper at line 31.

IMO there are several very good reasons not to put "use" into a subroutine (or other block) but rather at the top of a package:

  • It obfuscates which modules you are using, one has to hunt through the entire file to find out which modules are loaded.
  • To make this more confusing, if the file contains multiple packages one needs to limit ones search to just that package, which requires thought instead of just editor-fu
  • Programmers unfamiliar with the compile-time loading property of "use" might be surprised/confused about symbols from that module being available in other subroutines
  • Conversely, programmers who come to rely on just sticking "use" anywhere in a file might be surprised that it's not available in a different package (this goes for putting "use" underneath the package declaration as well, but at least here the ordering gives a hint that the two are relevant to each other).

All dogma is stupid.

In reply to Re^2: Code style advice: where to put "use" statements? by tirwhan
in thread Code style advice: where to put "use" statements? by eyepopslikeamosquito

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [Corion]: Your Mother: I think that's because (in the west) the Nazi-Germans are recognized as universally evil. Of course, you could do some number games to calculate other measures of evil than "historic losers of second world war" to come up with other evils:)
    [Corion]: I've heard "Troll" described as the new Punk, and in a way, it can be as destructive as living the Punk lifestyle, and you don't have to sit out in the cold...
    [LanX]: Anti-Germans
    [LanX]: Socrates was a Troll
    [Your Mother]: It's very, very dangerous... Thinking that a group is intrinsically evil... buries the fact that all humans can be so deep that it starts to become likely they will be.
    [Your Mother]: LanX++
    [Corion]: (also the "troll for trolls sake" could seen be much like the "punk for punks beer")
    [Your Mother]: I'm completely (historically anyway) a troll in real life. It's not fun online, you can't really win. :P
    Happy-the-monk orders a Punk IPA.
    [Your Mother]: Also, I'm too old to start fights with strangers anymore.

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (16)
    As of 2018-03-19 13:45 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (240 votes). Check out past polls.