It is a dangerous style to call functions with the
&hello_world;
notation because (as perlsub mentions) you get an implicit argument list passed that you probably did not intend.

Also note that just calling something a module doesn't give you any protection if it doesn't use a package statement internally. When loading stuff from a file that doesn't use package you can always do it yourself by declaring a package and then using do to load it.

# Loads a file into a package. sub load_lib { my $file = shift; my $pkg = shift; my $ret = eval "package $pkg; do '$file';"; if (not defined($ret)) { $@ and confess("Cannot parse '$file': $@"); $! and confess("Cannot load '$file': $!"); warn("Loading '$file' did not return a defined value"); } $ret; }
Note that the semantics here are slightly more generous than Perl's requirements of a module. I only warn if you don't get a defined value back. But the reason that I do this is that if it doesn't return something defined I cannot tell the difference between a system error caused executing the file and a system error caused by trying to read the file.

If that restriction bothers you, you can explicitly read the file's contents into a string with the package prepended and do your own eval of that. You may wish to look at the advice at the end of perlsyn on how to control the output of the error message though. (Put the package before the preprocessor message. There is a bug in 5.005 that causes a preprocessor message on the first line of an eval to be ignored. Besides which you need to do this to make the line-number you get be reasonable. :-)

Note that none of these solutions will help much if you have a script that does an exit somewhere. There are ways to override that, but if you are getting to this kind of detail, you probably wanted to make stuff into a module. :-)


In reply to Re (tilly) 2: Calling subroutine in a package from other perl sub. by tilly
in thread Calling subroutine in a package from other perl sub. 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.