Re: Syntax checking without BEGIN blocks
by duff (Parson) on Apr 27, 2006 at 18:47 UTC
|
AFAIK, you can't. The BEGIN subs need to execute as they have the potential to warp the syntax.
| [reply] |
|
Seems (to my unfamiliar-with-internals eyes) like it wouldn't be too much of a stretch to have a check option for each phase, so you could syntax-check only the BEGIN phase, for example. I don't even know in what order BEGIN, CHECK, and INIT get run, but each setting would only run "up to" whatever phase you're interested in.
Would someone who has a better understanding comment on whether that's doable?
Update: Looks like people know enough to downvote, but not enough to explain. That's never happened before.
Caution: Contents may have been coded under pressure.
| [reply] |
|
I think the down votes are because a) syntax checking only through BEGIN, CHECK, INIT doesn't solve the OP's problem and b) because you said you don't know what order BEGIN, etc run in when it's easily discoverable from the docs (never admit weakness ;-).
Though, in any case your post does present a potential "solution". The OP could modifiy the modules such that any initialization code is in an INIT block rather than free of subroutine shackles and so executed at module use-time. Of course, this is assuming that the code really isn't warping the syntax of the language to its own nefarious desires and really isn't setting up structures needed by future BEGIN-time processes.
In some sense, perhaps "free" code in modules should automatically be placed in INIT blocks and the module author, if they really need BEGIN-time processing, should be required to say BEGIN { ... }. I'd wager that that's what many people mean with "free" code anyway.
| [reply] [d/l] |
|
|
|
Re: Syntax checking without BEGIN blocks
by isotope (Deacon) on May 01, 2006 at 22:20 UTC
|
PerlTidy can do some basic syntax checking without running the script through perl, but it's not perfect, because it's not running the script through perl. It can also optionally pass it through perl, which of course will execute the BEGIN blocks.
Your real problem is that you have a bad block of code in the module you want to check. Just what are you going to do with this unsafe code after you add your debug statement?
I'd also offer the advice that being rude to people you ask for free help probably doesn't get you very far, but I doubt you'd take that advice.
| [reply] |
Re: Syntax checking without BEGIN blocks
by bluto (Curate) on Apr 28, 2006 at 16:36 UTC
|
I like to syntax check my own code, esp during a development cycle. (This isn't as useful for other people's modules but that's why test suites exist.) As others have mentioned, perl itself is unable to do this in all cases. IMO it is a module writer's responsibility, if at all possible, to allow the module user to check their own code without side effects. For example, it appears that most simple side effects (e.g. unlink) can be "commented" out with the $^C variable like this...
unlink("myfile") unless $^C;
I haven't seen much mention of $^C in the past, so perhaps someone more knowledgable can comment on whether this is always effective.
| [reply] [d/l] |
|
IMO it is a module writer's responsibility, if at all possible, to allow the module user to check their own code without side effects.
That assumes no incompetence or malice on the part of the module writer. One of the first tests of competence is to see if the module is will actually pass a syntax check.
The problem of how to do that safely is really the one I'd like to address.
| [reply] |
|
If you are worred about incompetance or malice, you better visually inspect the module and the Makefile that comes with it (I certainly do). Syntax checking is great for finding bugs during compiling your own code, but don't confuse it with keeping things safe. In fact the safest code is code that fails a syntax check.Syntax checking can't stop someone from writing syntactically correct, non-BEGIN block, destructive code. All they have to do is bury something like system("rm -rf *") in the module somewhere and you will have lots of fun trying to find out why your system doesn't work.
| [reply] |
A reply falls below the community's threshold of quality. You may see it by logging in.
|
Re: Syntax checking without BEGIN blocks
by TedPride (Priest) on Apr 27, 2006 at 18:55 UTC
|
Just write a Perl script to move the BEGIN blocks to the front, and replace the BEGIN with do and comment markers. Test your code. Then use another Perl script to move the blocks back. | [reply] |
|
| [reply] [d/l] |
|
| [reply] |
|
I think you might have missed the point about begin blocks. In order for perl to check syntax it HAS to run the use statments, because the use statments can change perls syntax. Thinks like subs in the modules included could add behaviour to bare words, especialy subs with prototypes.
| [reply] |
|
|
|