barvin has asked for the wisdom of the Perl Monks concerning the following question:

I'm using a run time require to delegate object create to an appropriate subclass like this.
eval "require My::Object::$subclass"; my $obj = My::Object::$subclass->new(@params);

This works fine, but as I edit subclass.pm and introduce syntax errors, they of course can't be caught at compile time. However I would have expected them to be caught at runtime when the require executes, but they aren't.

What actually happens is that every subroutine including and after the one where the syntax error is, just goes missing and the program complains "Undefined subroutine..." when it first tries to call one of the missing subs. I can confirm the loss of the subroutines in the debugger with 'S' - the sub in the required module where the syntax error is and every sub after it is not listed in the current namespace. The Perl Programming book is silent about the syntax checking behavior of the require command.

Thanks for any help.

Barry

Replies are listed 'Best First'.
Re: Runtime syntax checking of a 'require'ed module
by ikegami (Patriarch) on Jul 03, 2008 at 01:10 UTC
    It does throw syntax errors, but like other exceptions, eval catches them. Quite the opposite of what you said, they *are* caught when using eval and they are *not* caught without it. Feel free to rethrow them using die.
    eval "require My::Object::$subclass; 1" or die $@;
Re: Runtime syntax checking of a 'require'ed module
by Joost (Canon) on Jul 02, 2008 at 23:53 UTC
    Is this the expected behavior?
    Dunno. It seems like you expect this code to work like it would in a persistent process. Are you running this as a persistent process (from a LISP-like REPL, or the debugger for instance) or are you just running the process from scratch each time?

    Does require not do any syntax checking?
    No it does do syntax checking.
    Is there a way to force syntax checking of a 'required' module?
    That depends on what you want to do. In general, just require()ing a module should work.

    Is there a better way that I should be implementing runtime delegation?
    That depends on the situation. During development you may want to keep the process running and only reload modules when you've changed them (keeping everything else in memory) but on deployed systems it may be better to start from scratch whenever the system is updated. And you can certainly find people who think you should do either from-scratch or run-time updates regardless.

Re: Runtime syntax checking of a 'require'ed module
by BrowserUk (Patriarch) on Jul 02, 2008 at 23:37 UTC
Re: Runtime syntax checking of a 'require'ed module
by Your Mother (Archbishop) on Jul 02, 2008 at 23:56 UTC

    Try UNIVERSAL::require?

    require UNIVERSAL::require; # Same as "require Some::Module" my $module = 'Some::Module'; $module->require or die $@;

    Docs suggest using in a BEGIN block to emulate "use" which might work at compile time for you.

      There's absolutely no advantage of using that module over eval require and it has the small disadvantage of requiring the installation of another module and the huge disadvantage of polluting every class and object.