Before executing your program, Perl needs to compile your source. This happens at "compile time". This basically means that Perl goes through your source code and creates an internal, executable version of your program.

At compile time, no execution takes place except when it encounters a use (or a BEGIN section, but that's not important right now).

When Perl encounters a use, it basically does a require of that module at compile time and executes the "import" subroutine of the module after it has been loaded.

The "import" subroutine of the module that you loaded, usually exports subroutines to your namespace. So, after the use, Perl "knows" that there is a subroutine called "function_from_ModuleA" because of the actions of the "import" subroutine.

So, when Perl continues compiling your program and it encounters a bare word "function_from_ModuleA", it "knows" it is a subroutine and creates the associated internal representation for a subroutine call (without parameters).

Another way that Perl knows at compile time whether a bareword is intended as a subroutine call, is when you prefix it with '&', or if you put a pair of parentheses after it.

So, in your second case, the module has not been loaded yet and so Perl doesn't know what to do with the bareword. You have to provide a hint to Perl that you intend it to be a subroutine call.

Apart from prefixing with '&" or suffixing '()', you can also tell Perl at compile time that there is going to be a subroutien "foo":

sub foo; # note no body specified!

Hope this helps.

Liz


In reply to Re: eval use, exported functions, barewords by liz
in thread eval use, exported functions, barewords by Anonymous Monk

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.