Ask a Question related to PERL Beginners, Design and Development.
-
Aman Thind #1
my ($var) = <IN>; and my ($var); $var = <IN> different ?
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.
Thanx
Aman
Aman Thind Guest
-
Paul Johnson #2
Re: my ($var) = <IN>; and my ($var); $var = <IN> different ?
Thind, Aman said:When you write "my ($cur_line) = <IN>;" you put <IN> in list context, thus> 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.
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]paul@pjcj.net[/email]
[url]http://www.pjcj.net[/url]
Paul Johnson Guest
-
Aman Thind #3
RE: my ($var) = <IN>; and my ($var); $var = <IN> different ?
Thanx Paul
That explains the behaviour very clearly.
I rushed to my code and replaced all the my ($var) with my $var :)
-----Original Message-----
From: Paul Johnson [mailto:paul@pjcj.net]
Sent: 10 February 2004 19:20
To: Thind, Aman
Cc: 'beginners@perl.org'
Subject: Re: my ($var) = <IN>; and my ($var); $var = <IN> different ?
Thind, Aman said:When you write "my ($cur_line) = <IN>;" you put <IN> in list context, thus> 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.
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]paul@pjcj.net[/email]
[url]http://www.pjcj.net[/url]
Aman Thind Guest
-
Randal L. Schwartz #4
Re: my ($var) = <IN>; and my ($var); $var = <IN> different ?
>>>>> "Aman" == Aman Thind <aman.thind@fidelity.co.in> writes:
Aman> I rushed to my code and replaced all the my ($var) with my $var :)
Except that this can also break things. Witness:
sub optimal {
my ($first) = @_;
...
}
If you remove the parens there, you break the subroutine, as it will
be getting the number of arguments, not the first argument.
Rather than having a rule like "always" or "never" there, you should
understand precisely what it is you are doing. And do the right thing.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Randal L. Schwartz Guest



Reply With Quote

