my ($var) = <IN>; and my ($var); $var = <IN> different ?

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default 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

  2. #2

    Default Re: my ($var) = <IN>; and my ($var); $var = <IN> different ?


    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]paul@pjcj.net[/email]
    [url]http://www.pjcj.net[/url]

    Paul Johnson Guest

  3. #3

    Default 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:
    > 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]paul@pjcj.net[/email]
    [url]http://www.pjcj.net[/url]
    Aman Thind Guest

  4. #4

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139