Hello monks I am curious about a little question to do with how PERL deals with compilation of modules. I'm not sure specifically how to state the question as I have only just begun to perceive that the question exists. Lets say you have a module;
package Model::Conf; use Exporter; use base 'Exporter'; our @EXPORT = qw( $conf ); our $conf = { 'somevar' => 'somevalue' }; $conf->{'foo'} = bar(); sub bar() { return 'bar'; } 1;
does the subroutine "bar" run every time I put use Model::Conf in other packages? If so, and assuming I want the code only to run once at compile time and its result to be accessible in any other package I put use Model::Conf in, would the following be correct;
package Model::Conf; use Exporter; use base 'Exporter'; our @EXPORT = qw( $conf ); our $conf = { 'somevar' => 'somevalue' }; $conf->{'foo'} = bar() unless $conf->{'foo'}; sub bar() { return 'bar'; } 1;
The reason I ask is that I want to be able to put use Model::Conf in various other packages without the sub "bar" being run each time and wasting OPCODES on a very tight little MVC 'micro'-framework I am working on for the educational benefit, and I am not sure if the above is the right way to be going or what. Any thoughts? Many thanks.

In reply to package compiling by simonodell

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.