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

Re: [FWP] A Perl curiosity



On Thu, Mar 02, 2000 at 09:35:04PM -0800, Yitzchak Scott-Thoennes wrote:
> #!perl -wl
> @array = (1,0,0,1,0);
> print scalar grep { 1 if !$_ } @array;
> print scalar grep { 1 unless $_ } @array;
> 
> Lines 3 and 4 produce different output, for reasons that should be
> apparent, given some thought.

Neat!

> I guess that's what Perl gets for blurring C's iron-clad distinction
> between statement and expression.

Even if such blurring exists (for which I see no evidence, though
all I read is perlsyn, not the C standard), it has nothing to do
with the unexpected output.  The following (which I think you'll
agree exhibits no blurring) produces the same output:

#!perl -wl
@array = (1,0,0,1,0);
print scalar grep { if (!$_) { 1 } } @array;
print scalar grep { unless ($_) { 1 } } @array;
__END__

The key is Perl's implicit return value, which has no analog in C.

Andrew

-- 
The best way to judge a language is to look at the code written by its 
proponents.
- Alex Stepanov

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe