> -----Original Message-----
> From: Leon [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, January 09, 2002 3:07 PM
> To: [EMAIL PROTECTED]
> Subject: Re: pull line #1 from a file
> 
> ...
> If I am sitting for a test, I failed because I would probably 
> think that
> $line = <FILE> would read the last line, I say so because of 
> the following
> reasons:-
> 
> my @line =<FILEHANDLE>; reads every line into @line, 
> therefore I see the
> 'read FILEHANDLE' just like an array of lines like this :-
> <FILEHANDLE> equivalent to ('line 1\n' , 'line2\n' ..... and 
> so on.. ) and
> if this is so, therefore $line should read the last line like 
> this example
> :-
> 
> my $line = ( 'line1', 'line2' , 'line3' );
> print $line; # print line3

This is tricky, because although the stuff on the right side of
the assignment looks like an array of lines, it really isn't. What
you have here is three expressions joined by the "comma" operator,
with the parentheses providing grouping to override precedence,
but not creating a list construct.

So Perl processes this by evaluating the expressions in the
parens from left-to-right order. The result of the overall
evaluation is simply the value of the rightmost expression, which
is then assigned to $line.

The parens are "needed" here because the comma operator has a
lower precedence than the assignment operator.

To make it clearer what's going on, the following statement is
exactly equivalent to yours:

    'line1', 'line2', my $line = 'line3';

If you force the right-hand side to be a list, the result changes.
Consider:

    @lines = ('line1', 'line2', 'line3');
    my $line = @lines;   # now RHS is definitely an array of lines
    print $line;         # prints "3"

$line gets the value 3, because the assignment to a scalar causes
perl to evaluate the right-hand side in "scalar context". An array
(@lines) evaluated in scalar context results in the number of 
elements in the array (3 in this case), and *not* the last element.

So, an assignment like:

    my $line = <FILEHANDLE>;

Causes the right-hand side to be evaluated in scalar context. From
'perldoc -f readline', we can see that:

   ...In scalar context, each call reads and returns the next line,
   until end-of-file is reached, whereupon the subsequent call
   returns undef.  In list context, reads until end-of-file is
   reached and returns a list of lines.

The key is that a function or operator can detect the context Perl
is evaluating it in (scalar or list), and can do different things
depending on the context. So <FILEHANDLE> can return a single line
in scalar context and an array of lines in list context. 

(Note that if <FILEHANDLE> always returned an array of lines, then
an assignment like:

    my $line = <FILEHANDLE>;

would set $line to the *number of lines read*, since an array 
evaluated in scalar context results in the number of elements in
the array.)

HTH

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to