Readline question

Discussion in 'Perl Misc' started by mauro papandrea, Mar 3, 2011.

  1. > cat dati
    line1
    line2
    line3
    line4

    This simple program ( an oversimplified version of my original one for
    sake of simplicity ) works:

    #!/bin/perl
    open (FILE, "dati");
    while ( <FILE> ) {
    $line = readline(FILE);
    print "$. $line";
    }
    close(FILE);

    this is its output:

    2 line2
    4 line4

    However, this oneliner gives a weird error:

    > perl -ne '$line = readline; print "$. $line"; ' dati


    Modification of a read-only value attempted at -e line 1.

    What am I missing?

    Thank you

    Regards

    Mauro
    mauro papandrea, Mar 3, 2011
    #1
    1. Advertising

  2. On 03.03.2011 12:23, mauro papandrea wrote:
    > > cat dati

    > line1
    > line2
    > line3
    > line4
    >
    > However, this oneliner gives a weird error:
    >
    > > perl -ne '$line = readline; print "$. $line"; ' dati

    >
    > Modification of a read-only value attempted at -e line 1.
    >
    > What am I missing?


    Seems to be a bug in older Perl versions. I can reproduce this behaviour
    with 5.8.8, however it works without any problem in 5.10.1.

    - Wolf
    Wolf Behrenhoff, Mar 3, 2011
    #2
    1. Advertising

  3. On 03/03/2011 16.01, Wolf Behrenhoff wrote:
    > On 03.03.2011 12:23, mauro papandrea wrote:
    >> > cat dati

    >> line1
    >> line2
    >> line3
    >> line4
    >>
    >> However, this oneliner gives a weird error:
    >>
    >> > perl -ne '$line = readline; print "$. $line"; ' dati

    >>
    >> Modification of a read-only value attempted at -e line 1.
    >>
    >> What am I missing?

    >
    > Seems to be a bug in older Perl versions. I can reproduce this behaviour
    > with 5.8.8, however it works without any problem in 5.10.1.
    >
    > - Wolf



    Thank you very much.
    As a matter of fact, I was using 5.8 version ( I apologize for not
    having posted that ).

    Have a nice day

    Mauro
    mauro papandrea, Mar 3, 2011
    #3
  4. mauro papandrea

    Steve C Guest

    On 03/03/2011 10:01 AM, Wolf Behrenhoff wrote:
    > On 03.03.2011 12:23, mauro papandrea wrote:
    >> > cat dati

    >> line1
    >> line2
    >> line3
    >> line4
    >>
    >> However, this oneliner gives a weird error:
    >>
    >> > perl -ne '$line = readline; print "$. $line"; ' dati

    >>
    >> Modification of a read-only value attempted at -e line 1.
    >>
    >> What am I missing?

    >
    > Seems to be a bug in older Perl versions. I can reproduce this behaviour
    > with 5.8.8, however it works without any problem in 5.10.1.
    >


    It's not a bug. In perl 5.8.8 the parameter to readline had no default to ARGV.
    The documentation is clear.
    Steve C, Mar 3, 2011
    #4
  5. mauro papandrea

    Guest

    On 03/03/2011 16.55, Steve C wrote:
    > On 03/03/2011 10:01 AM, Wolf Behrenhoff wrote:
    >> On 03.03.2011 12:23, mauro papandrea wrote:
    >>> > cat dati
    >>> line1
    >>> line2
    >>> line3
    >>> line4
    >>>
    >>> However, this oneliner gives a weird error:
    >>>
    >>> > perl -ne '$line = readline; print "$. $line"; ' dati
    >>>
    >>> Modification of a read-only value attempted at -e line 1.
    >>>
    >>> What am I missing?

    >>
    >> Seems to be a bug in older Perl versions. I can reproduce this behaviour
    >> with 5.8.8, however it works without any problem in 5.10.1.
    >>

    >
    > It's not a bug. In perl 5.8.8 the parameter to readline had no default
    > to ARGV.
    > The documentation is clear.
    >
    >


    Great!
    Thank you very much.
    I had read documentation but it was for 5.10, not for 5.8
    and that was probably what I missed

    Have a nice day

    Mauro
    , Mar 4, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason
    Replies:
    1
    Views:
    338
    Jack Klein
    Oct 18, 2003
  2. Dave
    Replies:
    3
    Views:
    353
    Arne Vajhøj
    Feb 12, 2008
  3. gavino
    Replies:
    4
    Views:
    536
    gavino
    Sep 20, 2010
  4. Jean-Michel
    Replies:
    0
    Views:
    359
    Jean-Michel
    Dec 22, 2007
  5. Andrew DeFaria
    Replies:
    1
    Views:
    208
    Ben Morrow
    Jan 30, 2008
Loading...

Share This Page