Also, if each module will always contain only 1 sub, do they need to have a sub at all? If not, how can they be written & called without a sub?

While I see you struck out this question, I can give you an explanation anyway.

That depends on whether you are calling the subroutine more than once.

If you are only calling the subroutine once, then you could skip defining a sub in the file. HOWEVER, any parameters you need to pass to the code in the file would have to be placed in global variables.

Note that repeating a require of a given file will not re-execute the code in it, no matter how many times you require that same file. It only get executed the first time you require a given file. (Though, of course, each different file required will be executed the first time each is required.)

In general, it's better to define subroutines that you call with the needed parameters. Even if the sub is only ever called once. This is cleaner and easier to understand and maintain.

Note: There is a way to "load and execute" the contents of a file any number of times. This can be done with do. HOWEVER, every time you do a file, it will be re-read and re-parsed. Also, unlike require, do only accepts a pathname to the file. Also, like require, any parameters that need to be passed will have to be put in variables.

I strongly suggest you continue to define subroutines that get called instead of trying to "call" a file (or module).


In reply to Re: Call sub in variable module by RonW
in thread Call sub in variable module by tel2

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.