saintex has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm a newbie in Perl (but not in another programming languages). I'm trying to learn it throught Lama book. This is my question: Why these results are different?
chomp (my @lines=<STDIN>); my @due=sort @lines; print "@due\n";
# and:
print sort chomp (my @lines=<STDIN>);
The second one print a number. I think this number is the quantity of lines chomped (or the number of elements in array), but I don't understand why the two statments are different. Thank you!

Replies are listed 'Best First'.
Re: Unexpected chomp behavior
by almut (Canon) on Feb 05, 2010 at 08:37 UTC

    chomp doesn't return the chomped lines, they're modified "in place". It does, however, return the number of characters removed — which is what you're passing to the sort function in the second case, while you're discarding that value in the first case.

Re: Unexpected chomp behavior
by ikegami (Patriarch) on Feb 05, 2010 at 08:50 UTC
    my @due=sort chomp (my @lines=<STDIN>);
    is basically
    my @anon = chomp (my @lines=<STDIN>); my @due=sort @anon;
    It is not the same as
    chomp (my @lines=<STDIN>); my @due=sort @lines;

    In the first snippet you posted, you are sorting @lines. In the second, you are sorting the result of chomp. These are very different, as described in chomp's documentation.

Re: Unexpected chomp behavior
by johngg (Canon) on Feb 05, 2010 at 11:07 UTC

    You might want to have a look at map which you could use here. Again, you need to pass out the chomped string, not the return value of chomp.

    knoppix@Microknoppix:~$ cat xxxx line 4 line 2 line 5 line 1 line 6 line 3 knoppix@Microknoppix:~$ cat xxxx | perl -E ' > say for sort map { chomp; $_ } <>;' line 1 line 2 line 3 line 4 line 5 line 6 knoppix@Microknoppix:~$

    I hope this is helpful.

    Cheers,

    JohnGG

Re: Unexpected chomp behavior
by FunkyMonk (Bishop) on Feb 05, 2010 at 11:40 UTC
    I think this number is the quantity of lines chomped

    You can easily find out, in a number of different ways:

    • From the command line, type perldoc -f chomp
    • Googling for perldoc chomp usually takes you to perldoc.perl.org
    • You can use the wonderful perldoc.perl.org website
    • You may have HTML documentation installed depending on which Perl you're using
Re: Unexpected chomp behavior
by Anonymous Monk on Feb 05, 2010 at 08:25 UTC
    • my $snap = "a$/"; warn unpack 'H*', $snap; warn unpack 'H*', chomp ( my $crackle = $snap ); warn unpack 'H*', $crackle; warn unpack 'H*', $/; __END__ 610a at - line 2. 31 at - line 3. 61 at - line 4. 0a at - line 5.
      chomp, perlvar, Tutorials
    • subject , not your status , goes in Title field (your question in one sentance )
Re: Unexpected chomp behavior
by JavaFan (Canon) on Feb 05, 2010 at 09:18 UTC
    Hmmm, as a non-newbie of other programming languages, you surely know the difference between the return value of a function, and its side-effects?

    I'm a bit baffled by the question - it's not that you assume chomp leaves its arguments untouched and returns the the chopped lines - then you would have written the first fragment as

    my @lines = chomp(<STDIN>);
    which would result in an error.
Re: Unexpected chomp behavior
by Anonymous Monk on Feb 05, 2010 at 09:53 UTC
    thank you for your answers! They are really useful to understand my errors! :-)