I think there are actually several reasons why. But I only really understand one, so that's the only one I'll comment on. :)

You're right when you say that you could dynamically create the function. You can do this using a special subroutine called AUTOLOAD--if you call a function in a particular package, and that function doesn't exist, Perl looks for an AUTOLOAD function in your package. Among other things, you can use that AUTOLOAD function to create other functions on the fly and run them. From perltoot, in case you want a clearer explanation:

When Perl tries to call an undefined function in a particular package and that function is not defined, it looks for a function in that same package called AUTOLOAD. If one exists, it's called with the same arguments as the original function would have had.
Another way to dynamically create functions is eval. Things that you wrap in an eval aren't executed until runtime (ie. not at compile time), so if you're using an eval to dynamically create a subroutine that you'll later call, there's no possible way for Perl to know about this function. It's perfectly valid Perl, though, so you won't get an error.

See, I could write this:

#!/usr/local/bin/perl -w use strict; my $code = <<CODE; sub foo { print "in foo\n"; } CODE eval $code; foo();
At compile time, Perl doesn't know that a subroutine called foo is going to exist. But it doesn't complain. And I can run it:
% foo.pl in foo
and everything's fine.

In terms of your particular problem... I'm not sure what to suggest. Would it be possible for you to use eval to check the syntax of the script? Then you could catch any errors, but your own script wouldn't die.


In reply to Re: -c by btrott
in thread -c by Adam

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.