Bryan R Harris wrote:
From: Bryan R Harris <[EMAIL PROTECTED]>
Given an open filehandle, why don't these two things do the same thing?

**************************************
@l2r{"a","b"} = (<FILE>, <FILE>);
$c = <FILE>;

**************************************
$l2r{"a"} = <FILE>;
$l2r{"b"} = <FILE>;
$c = <FILE>;

**************************************

The first seems to be slurping the whole file into $l2r{"b"} and leaving $c
undefined...  The second does what I want.  Doesn't seem to make sense.
Context. The <FILEHANDLE> returns a single line in scalar context and
a list of all lines in a list context. And there is no such thing as
a two-item-list context.

So in the first case the assignment to @l2r{"a","b"} provides a list
context so the very first <FILE> reads all the lines left in FILE,
the second and third return an empty list. The first two lists are
concatenated together and the first two items of the resulting list
are assigned to $l2r{"a"} and $l2r{"b"}. And the rest of the list is
forgotten. You'd have to do something like

@l2r{"a","b"} = (scalar(<FILE>), scalar(<FILE>));
or
@l2r{"a","b"} = (<FILE>.'', <FILE>.'');

to ensure that the <FILE> is evaluated in scalar context.


Which part is forcing the list context?  The fact that the <FILE> is inside
parenthesis () or the @l2r{...} part?

The left hand side of the assignment determines context so the @l2r{...} part.


Is every element of a list () contextualized as a list itself?

A list is composed of zero or more scalars, if that's what you mean?



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to