in reply to -c
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:
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.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.
See, I could write this:
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:#!/usr/local/bin/perl -w use strict; my $code = <<CODE; sub foo { print "in foo\n"; } CODE eval $code; foo();
and everything's fine.% foo.pl in foo
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.
|
|---|