perlquestion
AnomalousMonk
<p>
In a recent [id://1158961|post], I saw a question about why
<c>
while ()
{
<>;
last unless /\S/;
print FILE;
}
</C>
produced a <i>"Use of uninitialized value $_ in pattern match (m//) ..."</I> warning. This is easily fixed by assigning the value returned by the <c> <> </C> (diamond) operator (aka the [doc://readline] function) to the appropriate variable:
<c>
c:\@Work\Perl>perl -wMstrict -le
"while ()
{
$_ = <>;
last unless /\S/;
print qq{>$_<};
}
"
qeert
>qeert
<
ghjk
>ghjk
<
</C>
(This example works the same under ActiveState 5.8.9 and Strawberry 5.14.4.)
</P>
<p>
What caught my eye was the null loop condition expression in the <br>
<c> while () { ...; last unless ...; ...; }</c> <br>
infinite loop. I would have thought of something along the lines of <br>
<c> while (1) { ...; last unless ...; ...; }</c> <br>
</P>
<p>
I'm surprised that a null loop condition expression is accepted by the compiler or, being accepted, that it evaluates as true. Because other boolean "nullities" (undef, the empty string, 0) are false, I would have expected a null condition expression, if it compiled at all, to be false. (A [mod://B::Deparse] of <c> while () { ... } </C> shows it to compile to <c> while (1) { ... } </C> in both Perl versions I tried.)
</P>
<p>
So, my questions:
<ul>
<li>
Is there any documentation or discussion of this behavior? (Offhand, I can't find any in a quick scan of [doc://perlsyn].)
</LI>
<li>
What is the rationale for this behavior? (Well, it does make a neat shorthand.)
</LI>
</UL>
</P>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-634253">
<hr><p>Give a man a fish<b>:</B> <c> <%-{-{-{-<</C></P>
</div></div>