[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Commify and "1 while" syntax



|>	1 while ($number =~ s/^(-?\d+)(\d{3})/$1,$2/);
|It's the same as:
|while (1) {
|	do stuff;
|}

That's completely wrong. The 1 is a simple statement:

    1;

The while is a modifier on the statement, and the substitution is the
conditional the while tests. So the original code works the same as:

while ($number =~ s/^(-?\d+)(\d{3})/$1,$2/)
{
    1;
}

Leaving out the 1 in the original code is *syntactically* incorrect (i.e.
perl won't even attempt to execute it), but changing it to most other simple
statements works:

42 while ($number =~ s/^(-?\d+)(\d{3})/$1,$2/);
'ignored' while ($number =~ s/^(-?\d+)(\d{3})/$1,$2/);
0 while ($number =~ s/^(-?\d+)(\d{3})/$1,$2/);
'' while ($number =~ s/^(-?\d+)(\d{3})/$1,$2/);

but not *all* simple statements work there:

exit 0 while ($number =~ s/^(-?\d+)(\d{3})/$1,$2/);

(which is why I've argued in the past that using 'ignored' isn't correct,
as it isn't ignored. It's evaluated for side effects, with the resulting
value thrown away.)

See the second paragraph under Simple Statements in perlsyn.pod for more
info.

Brian

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org