How do I chomp a string if I have Perl 4?

早过忘川 提交于 2019-12-24 01:55:11

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/797993/how-do-i-chomp-a-string-if-i-have-perl-4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!