http://www.perlmonks.org?node_id=852954


in reply to Re: how to check syntax of code inside a scalar?
in thread how to check syntax of code inside a scalar?

Uggh, it's a bit shortsighted that there's no built-in function for this. However, after playing a bit, I've found that this can be simulated without having to actually execute the scalar's contents by pre-pending "return;" and testing with eval:

sub syntax_ok { my $source = "return;\n" . shift; eval $source; return 1 unless $@; return 0; }

Thanks!