有没有办法在本地更改 perl 中的输入记录分隔符?
is there a way to locally change the input record separator in perl?
通过 my $x
将变量 $x
的范围限制在特定的代码块或子例程中,将编码人员从“全局变量”引起的混乱中解救出来。
但是当涉及到输入记录分隔符时,$/
,显然它的范围不能被限制。
我说得对吗?
因此,如果我忘记在循环结束时或在子例程内重置输入记录分隔符,我对子例程的调用下面的代码可以给出意想不到的结果。
以下示例对此进行了演示。
#!/usr/bin/perl
use strict; use warnings;
my $count_records; my $infile = $ARGV[0]; my $HANDLEinfile;
open $HANDLEinfile, '<', $infile or die "cannot open $infile for reading";
$count_records = 0;
while(<$HANDLEinfile>)
{
$count_records++;
print "$count_records:\n";
print;
}
close $HANDLEinfile;
look_through_other_file();
print "\nNOW, after invoking look_through_other_file:\n";
open $HANDLEinfile, '<', $infile or die "cannot open $infile for reading";
$count_records = 0;
while(<$HANDLEinfile>)
{
$count_records++;
print "$count_records:\n";
print;
}
close $HANDLEinfile;
sub look_through_other_file
{
$/ = undef;
# here, look through some other file with a while loop
return;
}
这是它在输入文件上的行为方式:
> z.pl junk
1:
All work
2:
and
3:
no play
4:
makes Jack a dull boy.
NOW, after invoking look_through_other_file:
1:
All work
and
no play
makes Jack a dull boy.
>
请注意,如果有人尝试更改为
my $/ = undef;
在子例程中,这会产生错误。
顺带一提,在Whosebug的标签中,为什么没有“输入记录分隔符”的标签?
my $/ = undef;
题的答案是改成local $/ = undef;
。那么修改后的代码如下
#!/usr/bin/perl
use strict; use warnings;
my $count_records; my $infile = $ARGV[0]; my $HANDLEinfile;
open $HANDLEinfile, '<', $infile or die "cannot open $infile for reading";
$count_records = 0;
while(<$HANDLEinfile>)
{
$count_records++;
print "$count_records:\n";
print;
}
close $HANDLEinfile;
look_through_other_file();
print "\nNOW, after invoking look_through_other_file:\n";
open $HANDLEinfile, '<', $infile or die "cannot open $infile for reading";
$count_records = 0;
while(<$HANDLEinfile>)
{
$count_records++;
print "$count_records:\n";
print;
}
close $HANDLEinfile;
sub look_through_other_file
{
local $/ = undef;
# here, look through some other file with a while loop
return;
}
那么就不需要将输入记录分隔符 return 手动设置为另一个值,或者设置为默认值 $/ = "\n";
。
可以使用local
临时更新全局变量的值,包括$/
.
sub look_through_other_file {
local $/ = undef;
# here, look through some other file with a while loop
return;
}
只要 look_through_other_file
子例程在调用堆栈中,就会使用未定义的 $/
。
您可能会在这个常见的习语中遇到这种结构,将文件的全部内容放入变量中而不改变程序其余部分的 $/
的值:
open my $fh, "<", "/some/file";
my $o = do { local $/; <$fh> };
通过 my $x
将变量 $x
的范围限制在特定的代码块或子例程中,将编码人员从“全局变量”引起的混乱中解救出来。
但是当涉及到输入记录分隔符时,$/
,显然它的范围不能被限制。
我说得对吗?
因此,如果我忘记在循环结束时或在子例程内重置输入记录分隔符,我对子例程的调用下面的代码可以给出意想不到的结果。 以下示例对此进行了演示。
#!/usr/bin/perl
use strict; use warnings;
my $count_records; my $infile = $ARGV[0]; my $HANDLEinfile;
open $HANDLEinfile, '<', $infile or die "cannot open $infile for reading";
$count_records = 0;
while(<$HANDLEinfile>)
{
$count_records++;
print "$count_records:\n";
print;
}
close $HANDLEinfile;
look_through_other_file();
print "\nNOW, after invoking look_through_other_file:\n";
open $HANDLEinfile, '<', $infile or die "cannot open $infile for reading";
$count_records = 0;
while(<$HANDLEinfile>)
{
$count_records++;
print "$count_records:\n";
print;
}
close $HANDLEinfile;
sub look_through_other_file
{
$/ = undef;
# here, look through some other file with a while loop
return;
}
这是它在输入文件上的行为方式:
> z.pl junk
1:
All work
2:
and
3:
no play
4:
makes Jack a dull boy.
NOW, after invoking look_through_other_file:
1:
All work
and
no play
makes Jack a dull boy.
>
请注意,如果有人尝试更改为
my $/ = undef;
在子例程中,这会产生错误。
顺带一提,在Whosebug的标签中,为什么没有“输入记录分隔符”的标签?
my $/ = undef;
题的答案是改成local $/ = undef;
。那么修改后的代码如下
#!/usr/bin/perl
use strict; use warnings;
my $count_records; my $infile = $ARGV[0]; my $HANDLEinfile;
open $HANDLEinfile, '<', $infile or die "cannot open $infile for reading";
$count_records = 0;
while(<$HANDLEinfile>)
{
$count_records++;
print "$count_records:\n";
print;
}
close $HANDLEinfile;
look_through_other_file();
print "\nNOW, after invoking look_through_other_file:\n";
open $HANDLEinfile, '<', $infile or die "cannot open $infile for reading";
$count_records = 0;
while(<$HANDLEinfile>)
{
$count_records++;
print "$count_records:\n";
print;
}
close $HANDLEinfile;
sub look_through_other_file
{
local $/ = undef;
# here, look through some other file with a while loop
return;
}
那么就不需要将输入记录分隔符 return 手动设置为另一个值,或者设置为默认值 $/ = "\n";
。
可以使用local
临时更新全局变量的值,包括$/
.
sub look_through_other_file {
local $/ = undef;
# here, look through some other file with a while loop
return;
}
只要 look_through_other_file
子例程在调用堆栈中,就会使用未定义的 $/
。
您可能会在这个常见的习语中遇到这种结构,将文件的全部内容放入变量中而不改变程序其余部分的 $/
的值:
open my $fh, "<", "/some/file";
my $o = do { local $/; <$fh> };