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


I am having problems with the chomp command. It's, for
some reason, taking out "/a" from the $summaryFile var. Please help!
while ($summaryFile = <INFO_FILE>) { print "DEBUG: Before chomp: $summaryFile \n"; #chomp $summaryFile; $summaryFile =~s/\n//; print "DEBUG: After chomp: $summaryFile \n"; . . } ;# End of while

-------
Output
=======
DEBUG: Before chomp: /warp/pub/testbot2/users/rehmanz/2009Aug23_151934/essw/bats/autobats/summary.log

DEBUG: After chomp: /warp/pub/testbot2/users/rehmanz/2009Aug23_151934/essw/bats/ utobats/summary.log

Replies are listed 'Best First'.
Re: Problems with chomp
by ikegami (Patriarch) on Aug 24, 2009 at 01:29 UTC

    I am having problems with the chomp command

    Not true. You proved yourself it's not a problem with chomp by using s/// and still having the same problem. chomp only removes from the end of string, and only removes the string found in $/ ("\n" by default).

    I suspect that your string might have characters your terminal interprets as control characters, causing some or all of the line to be overwritten. Specifically, the "a" is being overwritten by the following space:

    print "DEBUG: After chomp: $summaryFile \n"; ^ |

    The "a" wasn't being overwritten before the chomp because the newline was printing the space on a new line.

    I suspect you actually get the following output:

    DEBUG: Before chomp: /warp/pub/testbot2/users/rehmanz/2009Aug23_151934 +/essw/bats /autobats/summary.log DEBUG: After chomp: /warp/pub/testbot2/users/rehmanz/2009Aug23_151934/ +essw/bats/ utobats/summary.log

    (Boooo! Saying it adds a space in the middle of the output is very inaccurate.)

    This can be obtained on an 80-col terminal with the following input:

    "/warp/pub/testbot2/users/rehmanz/2009Aug23_151934/essw/bats/autobats/ +summary.log\r\n"

    Your file has DOS line endings, but you're not using the :crlf PerlIO layer. You should convert your file, but using the following should do the trick (and trims trailing whitespace as a bonus):

    s/\s+\z//;

    If that's not the case, check what your string actually is by piping the output through od -c.

    Update: Added everything after "I suspect" except for the last paragraph.

Re: Problems with chomp
by bichonfrise74 (Vicar) on Aug 24, 2009 at 01:32 UTC
    Interesting... It works fine for me.
    #!/usr/bin/perl use strict; my $summaryFile = '/warp/pub/testbot2/users/rehmanz/2009Aug23_151934/' . 'essw/bats/autobats/summary.log'; print "DEBUG: Before chomp: $summaryFile \n"; chomp $summaryFile; $summaryFile =~ s/\n//; print "DEBUG: After chomp: $summaryFile \n";
Re: Problems with chomp
by Marshall (Canon) on Aug 24, 2009 at 08:42 UTC
    The first problems that I see are lack of examples of the problem. There is (1): no "chomp()" in your code, (2): there is no example input or output (or at least none that I can access) Ok, maybe I made some mistake in trying to access your URL(s), but you should make this easy, or show 4 lines or something like that!

    chomp() removes trailing end of line char(s). On Unix this is just \n, on Windows this might be just \n or \r\n.

    Your code starts off wrong for line processing with chomp(),  $summaryFile = <INFO_FILE> reads INFO_FILE into one $var. chomp($summaryFile); will just remove \n\r like explained above for that single line.

    Anyway, most important issue is that if you ask a question about chomp(), and have some demo code, then chomp() should be in that code! Then some easy example of the problem. I tried a couple of formulations of "/warp/..." to get it to display in my browser. You are asking folks who would like to help you to work too hard. Use simple examples or provide full URL name for off-site data.

Re: Problems with chomp
by dsheroh (Monsignor) on Aug 24, 2009 at 10:56 UTC
    I'll second ikegami's assessment that you're probably working with a file that uses Windows line endings (\r\n) on a system that uses unix-style line endings (\n). In that circumstance, chomp (or s/\n// on any system) will remove only the \n (line feed, which moves the cursor to the next line and, on unix-type systems, also implicitly returns the cursor to the beginning of the line), leaving you with a bare \r (carriage return, which returns the cursor to the beginning of the line, but does not move it to the next line), causing the text which would have otherwise been on the second line to instead overwrite the first line.

    Assuming your representation of the output is accurate, it appears that $summaryFile may contain both an embedded end-of-line (producing the bare \r in the middle of your text) and a second end-of-line at the end of the string (producing the blank line between the two debug prints). In such a case, s/\n// will only remove the first \n, while chomp will only remove the second. Use s/\n//g (or s/\r?\n//g) to remove all line ends if you're actually getting more than one in your input data.