Should Perl have a `chomped` function?

Edit: orginially rchomp, but Aristotle’s suggestion of chomped is perfect!

brian d foy posed an interesting interview question: “What five things do you hate most about language X?” positing that an experienced user of X should know 5 things (s)he hates about it.

In my list is the return value of chomp. Yes I understand why it works as it does

print "chomped" if chomp $input;

but I find that use case happens far less often than the usual

chomp( my $input = <> );

It looks bad, and it is not intuitive, especially to the new user. Just today another one popped up on StackOverflow. This has got to be one of the most common questions on the site.

Wouldn’t it be great to have a chomp function that returns the chomped value or values? In the spirit of the new s///r flag I originally wanted to call it rchomp, Aristotle’s comment of chomped is my new favorite. You would use it like:

my $input = chomped <>;

Since we all love CPAN of course I could make a CPAN module for this, but no one would add a dependency on it just for one convenience function. I don’t expect large adoption of my Tie::Select for this reason, even thought I think it has reasons to be safer than the core’s SelectSaver in some rare circumstances.

So anyway, is chomped something that the community would want? Could it possibly be in CORE:: so that people might actually use it? I even see that a similar concept made an rfc for Perl6. Just daydreaming I guess, but oooh I do hate it.


Yes yes yes yes yes yes yes.

I hate that I’m forced to write a procedure every time I want a non-destructive, functional chomping. (Which, coincidentally, is always).

I’d expect the r to mean “right”, as in rindex. Instead, your module might just override chomp like Time::local time does.

Although it might be nice to have another function that returns the chomped value, it’s not going to solve the problem for people who already don’t read the docs.

When seeing the title I though WTF right-chomp, what would that do.

I think it would be nice to have a version of chomp that behaves as most of us expects but why lengthen the name? Why not something shorter?

AFAIK these are covered in p6 by the trim* methods, no ? The advantage of p6 is that those methods can be cleanly extended/overridden lexically, like

$str.trim( in-place => True, trailing => True );


my $ret = $str.trim( copy => True, leading => False );

IMHO, trim-trailing and trim-leading should be just subcases of the trim method, explicitly encoded in the signature.

“it is not intuitive, especially to the new user”

Principle of Least Astonishment: Any language that doesn’t occasionally surprise the novice will pay for it by continually surprising the expert.

As it is, the chomp(my $input = <>) is probably the first Perl idiom I leaned, and Perl is like that, and in many cases better for it.

It’s funny how many authors don’t bother to search CPAN first. There already exists String::Trim and Text::Trim and probably others that do exactly this.

my next thought is chompr

That suffers from the same problem. Any “r” that hangs off the name of a string function is going to be thought of as meaning “right” on the first pass.

I think if I were keen on this particular issue I would propose to call the new built-in chomped. As in,

my $input = chomped <>;

use File::Slurp my @lines = read_file $file, chomp => 1 ;

and that isn’t just a convenience function. it is also faster than using <>.

if you are reading line by line, chomp isn’t an issue anyhow. rchomp is not needed.


Actually, Perl 6 simply covers this with its own chomp. In Perl 6, neither chop nor chomp modify their arguments in-place. This is the case with most (all?) core functions/methods. You could call chomp($line) or $line.chomp to return the chomped value, or $line.=chomp to chomp in-place. The method-call assignment operator .= is a cool addition to the various assignment operators like +=. The spec used to include p5chop and p5chomp but those were later removed. Also, chomping is much rarer in Perl 6 because I/O handles autochomping by default since that’s what most people want.

Leave a comment

About Joel Berger

user-pic As I delve into the deeper Perl magic I like to share what I can.