"chomp takes off any and all trailing newlines,..."

Nope. If there are multiple trailing newlines, chomp only removes one of them. That's the way it works unless you set $/ to an empty string, in which case it gobbles them all.

Consider:

my $str = "some string\n\n\n\n"; chomp $str; print "[$str]"; # still three trailing newlines $/ = ""; # 'paragraph mode' chomp $str; print "[$str]"; # all gone
Note also that while chomp does return the number of characters removed, that result may not be what you think if you are in Windows or Mac OS where \n gets written out as two characters. During the time that Perl still has the string in memory, the \n only counts as one character and the second chomp in the example above returns 3 (not 6) on my Windows machine -- just as it would on Linux.

On the other hand, if you assign a value to $/ that truly is more than one character (internally), that count gets returned as you would expect.

$/ = 'ggl'; # giggle mode my $str2 = "a string ggl"; my $cnt = chomp $str2; print "$cnt"; # prints: 3

------------------------------------------------------------
"Perl is a mess and that's good because the
problem space is also a mess.
" - Larry Wall


In reply to Re: Re: When do you use chop insetad of comp? by dvergin
in thread When do you use chop instead of chomp? by Jemts

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.