Thind, Aman said:
> Hello Friends,
>
> I was just writing some code and observed that :
>
> My ($cur_line) = <IN>;
>
> While ($cur_line = <IN>)
> {
> .........
>
>
> Even though <IN> is a handle of a file with many lines, control does not
> enter the while loop.
>
> However when I do :
>
> My ($cur_line);
>
> $cur_line  = <IN>;
>
> While ($cur_line = <IN>)
> {
> .........
>
> It enters the while loop as I preseume it should.
>
> Why is it so ? Value of $cur_line is the same before the while loop i.e.
> only 1 line is read into the variable in both the cases.

When you write "my ($cur_line) = <IN>;" you put <IN> in list context, thus
it reads all the input it can, creating a list with each element being one
line  of the input.  You then assign this list to the list ($cur_line),
which assigns the first element to $cur_line and discards the rest.

What you really want is "my $cur_line = <IN>;".  Automatically adding
parentheses to lexical variable declarations is not a good habit to get
into.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net


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


Reply via email to