Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: chop vs chomp

by graff (Chancellor)
on May 11, 2007 at 07:52 UTC ( [id://614838]=note: print w/replies, xml ) Need Help??


in reply to chop vs chomp

If we modify $/, e.g. to ';' to parse a Perl program, then of course the last line won't generally terminate with $/ but with "\n". In that case, it is clearly wrong to chomp regardless because the presence of $/ is a syntax requirement. In such cases, chomp() is no good anyway because it returns the length rather than the content of what is chopped.

Either I'm not understanding what you are trying to say in that part, or else you don't understand what $/ and chomp are really doing.

Setting aside the issue of how foolish it would be to "parse" a perl script by setting $/ = ';' , let's look at what actually happens, given a perl script like this:

#!/usr/bin/perl use strict; for my $letter ( qw/h e l l o , w o r l d/ ) { $_ .= $letter; } print; while (1) { last if ( /^o/ ); $_ =~ s/.// }
If some other perl script sets $/=";" and reads this one as data in a  while(<>) loop, it will go through five iterations; the first four records will have ";" as the very last character, and chomp, if applied, will remove it. The fifth record will end in "}", possibly followed by any sort and amount of whitespace, and chomp will have no effect on that.

Do you think that's a problem? I don't. That is the documented behavior. To the best of my knowledge, and based on all my own experience, that is the most desirable behavior, when the objective is to simply remove the input record separator when one is present.

There are other, less common situations where the old "chop" (remove last character, no matter what it is) is still useful, and I'm glad it's still available for that purpose.

If you are complaining about the fact that many novice perl users don't understand $/ and chomp, I can understand your frustration, and I agree more people should know more about it. But if you are complaining that you're having trouble using chomp, I have to wonder why. It's not that complicated.

When there are issues like a single script meant to be used on any OS and having to handle a mixture of CRLF and LF line terminations (in different files or even in a single file), then I agree that chomp is maybe not the best approach, because $/ does not allow that sort of flexibility. For that I would do something like  s/[\r\n]+$// instead.

(In fact, I've done that in a number of scripts, and it has served me quite well.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://614838]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-25 16:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found