tags:

views:

454

answers:

3

I am writing a script on my personal machine which is connected to the remote server. I think the remote server has Perl 4.0 or lesser version installed and that is why it is unable to recognize the same. Is there an alternative to the chomp command?

+3  A: 

It's not an exact replacement, but you could try:

$var =~ s/\r?\n?$//

which will strip either CRLF (DOS), LF (Unix), CR (Mac?).

The normal chomp operator always strips out the currently defined $INPUT_RECORD_SEPARATOR for the current O/S.

Alnitak
Good idea, but if $var might contain multiple \n-separated lines, beware -- depending on how Perl 4 interprets "$" in a regex, the above might remove the first \n rather than the last.
j_random_hacker
Instead of chomping \r\n, just get rid of the value of $/, like you mentioned.
brian d foy
I thought about that, but didn't have a set of Perl 4 manpages to hand to check it would work. In any event, I prefer my input parsers to follow Postel's Law ("be strict in what you produce, and liberal in what you accept"), so my version works regardless of whether the input text file is in DOS or Unix format.
Alnitak
p.s. and thanks for the downvote...
Alnitak
A: 

I believe chop() was used in pre-5 perl. Not as useful as chomp() of course. If you're handling random input you would probably be better off using a regexp, but if you're always parsing a unix-formatted file:

while (<F>) {
  chop();
  do_stuff();
}

As the comment below states, chop() always removes the last character of the lvalue, not just if it is a newline character (or what's in the line ending var). I knew this (hence the "not as useful as chomp()" comment) but somehow forgot to actually type it out.

jj33
To clarify: the difference between chop and chomp is that chop *always* removes the last character, no matter if it is the current $INPUT_RECORD_SEPARATOR or not.
And, the input record separator might be more than one character :)
brian d foy
Brian, what was then convention in perl4? I only really "speak" perl5, but the perl4 code I have had to review used "chop()" the way people use "chomp()" now, because they understood the format of the file they were going to be parsing. If it was idiomatic to not use chop() or to wrap a lot of it in protective logic, could you provide an example? Otherwise, despite my downvotes, I feel my answer of "chop() was the not-as-good alternative to chomp() in perl4" is a very valid answer to "Is there an alternative to chomp() in perl4?"
jj33
+1  A: 

Going the chop route is ill advised, I would use the regex, they are more mantainable and transparent for others. Modifiying the record variable habitually is eventually going to eventually polute some poor package and cause odd things to occur.

hpavc