in reply to Chomp(<handle>)

The first error is because it can't choose between chomp SCALAR or chomp ARRAY usage. Should it slurp the whole file into an array and chomp that? Or should it slurp one line and chomp that? Using a named intermediary lvalue gives it the proper context to make such choices.

The second error is what you expected it to return. From (perldoc -f chomp), "It returns the total number of characters removed from all its arguments." So @row gets one element, which is '1'.

--
[ e d @ h a l l e y . c c ]

Replies are listed 'Best First'.
Re: Re: Chomp(<handle>)
by dws (Chancellor) on Jul 14, 2003 at 18:06 UTC
    The first error is because it can't choose between chomp SCALAR or chomp ARRAY usage.

    No, the first error is because he's "passing" something to chomp() that can't be modified in place.

    Try

    chomp(" foo ");
    It's clearly a SCALAR, yet it won't work.

      $ perl -e 'chomp(" foo ");' Can't modify constant item in chomp at - line 1, near "" foo ")"
      $ perl -e 'chomp(<STDIN>);' Can't modify <HANDLE> in chomp at - line 1, near "<STDIN>)"
      Different error messages. In the first case, it complains about modifying a non-lvalue (a constant item). In the second case, it complains about modifying the results of reading from a file handle.

      I'll grant you that chomp SCALAR can't chomp a non-lvalue, if you'll grant that the ambiguous context of the syntax chomp <FOO> is another valid reason that the compiler gives a separate distinct message.

      --
      [ e d @ h a l l e y . c c ]

        In the second case, it complains about modifying the results of reading from a file handle.

        Care to place a small wager on that? I could be wrong. You might make a buck.

        I disagree. They are the same error message:

        Can't modify %s in %s

        (see perldoc perldiag, search for modify)

        chomp can only modify an lvalue, and neither a 'constant item' nor '<filehandle>' are lvalues, just as dws said.

        --
        3dan