您可使用Tie: 更改档案(从Perl 5.8)(从Perl 开始)的文件:
#!/usr/bin/perl
use strict;
use warnings;
use Tie::File;
my $file = shift;
tie my @lines, "Tie::File", $file
or die "could not open $file: $!
";
for my $line (@lines) {
$line .= join ",", , get_data();
}
sub get_data {
my $data = <DATA>;
chomp $data;
return split /-/, $data
}
__DATA__
1-2-3-4
5-6-7-8
您也可以使用“ARGV/<”的电子配对;和 trick,设定“^一”美元:
#!/usr/bin/perl
use strict;
use warnings;
$^I = ".bak";
while (my $line = <>) {
chomp $line;
$line .= join ",", , get_data();
print "$line
";
}
sub get_data {
my $data = <DATA>;
chomp $data;
return split /-/, $data
}
__DATA__
1-2-3-4
5-6-7-8