We don't bite newbies here... much | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Edit: Because I couldn't find the documentation I expected the return value of an if block to be undefined behaviour, but Haukex's answer makes much more sense. You can ignore the rest of this post (except maybe the bit about Deparse, which actually confirms Haukex's version). You used an if in a do block. So I'll give you an answer for a for loop, maybe in a function ;-)
perlsub: If the last statement is a loop control structure like a foreach or a while, the returned value is unspecified perlsyn: Perceptive Perl hackers may have noticed that a for loop has a return value, and that this value can be captured by wrapping the loop in a do block. The reward for this discovery is this cautionary advice: The return value of a for loop is unspecified and may change without notice. Do not rely on it. I quote those because I couldn't even find the relevant documentation for other kind of blocks. As far as I can tell it's not just unspecified, it's undocumented. The reason is quite clearer though, some freedom is left for perl to try and optimize your statement. So if (0) {SOMETHING} might be removed entirely, or replaced by a no-op. The condition in if (1) might be removed because the block is always executed. Actually this is confirmed by deparsing the code (perl -MO=Deparse yourfile.pl): You can see that the if (1) {} is turned into do {}, and the if (0) is turned into 0. But this is not reliable behaviour, that's just a side effect of perl trying to replace some statements by the best equivalent it could find. In reply to Re: printing unitialized value of the 'do BLOCK'
by Eily
|
|