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?
views:
454answers:
3It'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.
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.
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.