Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (6)
    As of 2018-06-19 10:32 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (113 votes). Check out past polls.