I knew there were other INIT/BEGIN/END type compile-time subs but couldn't remember what they were, and both perldoc and a grep of the html files failed to turn up were these are documented before I posted. Adding the CHECK (and the others if need be to the regex is trivial. As is handling those same keywords prefixed with sub.
Dealing with use statements seems to be equally simple. Switch use for no. The statement remains syntactically valid but does not cause any processing of the module. Eg. For the following test I replaced the 1; at the end of POSIX.pm with print 'POSIX.pm processed'.$/;
C:\test>perl -c use POSIX qw[ceil floor]; print ceil($a), floor($b); POSIX.pm processed no POSIX qw[ceil floor]; print ceil($a), floor($b); ^Z - syntax OK C:\test>
As you can see, this changed allow the syntax of the statement to be checked without the module it referes to being processed.
However, the sub defined in and exported from a module with a prototype of () is a problem.
C:\test>perl -c sub cool(){ print 'cool',$/; } my $a = cool; ^Z - syntax OK
I can't see a manover around that one other than a adding a restriction to the code that subs must be invoked with either & or (). Depending on what the OP was trying to achieve, that might be acceptable, but as a general facility, would suck a lot.
With regard to the __DATA__ versus __END__. Dunno, sometimes I use one, sometimes the other. For all 'normal' uses it seems to make no difference.
On which note:), why did you use perl -e'BEGIN{ require "prog.pl" }' instead of prog.pl or perl prog.pl?
I thunk and thunk and thunk some more and can't see the logic behind that one:).
test prog as it currently stands
#! perl -slw use strict; while(<DATA>) { chomp; my $code = $_; tr/\n//d; s[(?:sub)*\s*((?:INIT|CHECK|BEGIN|END)\s*{)][sub syntax_check_$1]g +; s[\b(?:use)\b\s*][no ]g; eval 'return;' . $_; print "'$_' \n\t: ", $@ ? "Fails with\n$@" : 'Passes syntax check' +; } __END__ INIT { print '*** INIT GOTCHA!!! ***'; } sub INIT { print '*** INIT GOTCHA!!! ***'; } CHECK{ print '*** CHECK GOTCHA!!! ***'; } sub CHECK{ print '*** CHECK GOTCHA!!! ***'; } BEGIN{ print '*** BEGIN GOTCHA!!! ***'; } sub BEGIN{ print '*** BEGIN GOTCHA!!! ***'; } END { print '*** END GOTCHA!!! ***'; } sub END { print '*** END GOTCHA!!! ***'; } require 'POSIX'; do 'POSIX.pm'; use POSIX qw[ceil floor]; $a=ceil($b); $b=floor($a); my $a = 1; my $a = cool; my $
Output
C:\test>231561 'sub syntax_check_INIT { print '*** INIT GOTCHA!!! ***'; }' : Passes syntax check Subroutine syntax_check_INIT redefined at (eval 2) line 1, <DATA> line + 2. 'sub syntax_check_INIT { print '*** INIT GOTCHA!!! ***'; }' : Passes syntax check 'sub syntax_check_CHECK{ print '*** CHECK GOTCHA!!! ***'; }' : Passes syntax check Subroutine syntax_check_CHECK redefined at (eval 4) line 1, <DATA> lin +e 4. 'sub syntax_check_CHECK{ print '*** CHECK GOTCHA!!! ***'; }' : Passes syntax check 'sub syntax_check_BEGIN{ print '*** BEGIN GOTCHA!!! ***'; }' : Passes syntax check Subroutine syntax_check_BEGIN redefined at (eval 6) line 1, <DATA> lin +e 6. 'sub syntax_check_BEGIN{ print '*** BEGIN GOTCHA!!! ***'; }' : Passes syntax check 'sub syntax_check_END { print '*** END GOTCHA!!! ***'; }' : Passes syntax check Subroutine syntax_check_END redefined at (eval 8) line 1, <DATA> line +8. 'sub syntax_check_END { print '*** END GOTCHA!!! ***'; }' : Passes syntax check 'require 'POSIX';' : Passes syntax check 'do 'POSIX.pm';' : Passes syntax check 'no POSIX qw[ceil floor]; $a=ceil($b); $b=floor($a);' : Passes syntax check 'my $a = 1;' : Passes syntax check 'my $a = cool;' : Fails with Bareword "cool" not allowed while "strict subs" in use at (eval 13) li +ne 1, <DATA> line 13. 'my $' : Fails with Can't use global $; in "my" at (eval 14) line 2, near "my $ ;" syntax error at (eval 14) line 2, at EOF
Examine what is said, not who speaks.
The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.
In reply to Re: Re: Re: run-time syntax checking
by BrowserUk
in thread run-time syntax checking
by Pardus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |