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

Re: [MacPerl] BUG: can't print both array and "array as scalar" in same statement



[First of all (and this is not specifically targeted at Bart), the list has
 started drifting too much into discussions of general Perl questions again.
 Try to stick to MacPerl questions (like the one about interaction with Alpha);
 if there's too much general traffic, people start unsubscribing and the value
 of this list as a MacPerl support forum declines.]

Bart Lateur <bart.mediamind@tornado.be> writes:
>I've just discovered a bug in MacPerl version 5.1.1.r2.

It's not a bug, and it's not MacPerl specific.

>The following code was ment to read in the first line of a 
>database-as-text file, and print out both the field names and the number 
>of fields. It won't work.

># This works:
>  print (@field+0);	#number of fields
>  print @field;		#every field

># This line fails: it only prints the number of fields:
>print (@field+0),@field;

The reason for this is Perl's parsing of list operators, nothing more sinister:
Most operators can be written both as

print(a, b, c)

and as

print a, b, c;

Perl makes up its mind which form is meant by checking whether the operator is
followed by a (, thus only the parenthesized stuff makes it into arguments of
print.

The Camel book has a lot to say on this topic; in short, if you want your
example to work, write it as:

print +(@field+0), @field;

Matthias

-----
Matthias Neeracher   <neeri@iis.ee.ethz.ch>   http://www.iis.ee.ethz.ch/~neeri
   "One fine day in my odd past..." -- Pixies, _Planet of Sound_