我有这样的代码:Perl的格格不会删除所有的换行符
#!/usr/bin/perl
use strict;
use warnings;
open(IO,"<source.html");
my $variable = do {local $/; <IO>};
chomp($variable);
print $variable;
然而,当我打印出来,它仍然有换行符?
我有这样的代码:Perl的格格不会删除所有的换行符
#!/usr/bin/perl
use strict;
use warnings;
open(IO,"<source.html");
my $variable = do {local $/; <IO>};
chomp($variable);
print $variable;
然而,当我打印出来,它仍然有换行符?
它消除了最后换行符。
既然你在整个文件啜,你将不得不做一个正则表达式替换,以摆脱他们:
$variable =~ s/\n//g;
Chomp只从字符串末尾删除一个换行符(实际上,当前值为$/
,但这是换行符)。要删除所有换行符,这样做:
$variable =~ y/\n//d;
或者你可以chomp
每一行,你在读它:
#!/usr/bin/perl
use strict;
use warnings;
open my $io, '<', 'source.html';
my $chomped_text = join '', map {chomp(my $line = $_); $line} <$io>;
print $chomped_text;
and downvote for ...? – 2011-04-14 01:10:11
下一次问自己,如果你真的认为一个主流语言的10岁的功能实在是坏,或者如果你只是使用它错了。 – jiggy 2011-04-13 19:23:45
['select' is not broken](http://www.google.com/search?q=select+isn%27t+broken) – 2011-04-13 20:57:45