From your description, I'd say what you're doing is 100% ok. Putting "my" variables in the module is just normal modular data hiding and should work fine with both "use" (compile time) and "require" (run time) in the common case where you don't need the data to be known at compile time. If you need it at compile time (e.g. to push onto @INC), simply enclose in a BEGIN block.

Since data hiding is a good thing, be sure to declare these "in the wild" variables in the smallest scope you can. In simple cases, organise your module so that the "my" globals and the functions that use them are stuffed at the very end of the file. Alternatively, put a "my" variable and the function/s that use it together in a bare block. For example:

# some module code # ... { my %some_global = ( ... ); sub fred { # fred() is the only function in the module # that uses %some_global # ... } } # the %some_global variable is not in scope here. # ... rest of module
Note that fred() is globally known, while %some_global is not. Again, this is just plain old hiding of data into the smallest scope you can. Notice that this is a simple "modular" style of programming. As a good rule of thumb, ask the question "do I need more than one of these?"; if the answer is yes, use O-O style, otherwise you can often get away with the simpler modular style described above.


In reply to Re: code that runs at module loading by eyepopslikeamosquito
in thread code that runs at module loading by spurperl

Title:
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, details, 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, summary, 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.