我使用open
加载了输入文件。我想将输入文件的名称与字符串'matched_results'连接起来,并将其用作输出文件的名称。
这是我的代码:
use File::Basename;
open(text, "<sampletext7482_ch9.txt");
while (my $line = <text>) {
push @matches, $1 while $line
=~ m{
(###some regex here)
}xgi;
}
$base = basename $text;
my $filename = "matched_results${base}.txt";
open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
print $fh "$_\n" for @matches;
close $fh;
我收到以下错误。
Use of uninitialized value $_[0] in substitution (s///) at /usr/share/perl/5.18/File/Basename.pm line 341, <text> line 1.
答案 0 :(得分:3)
$base = basename $text;
$text
未在任何地方定义。因此,您将undef
传递给basename
函数。
你应该
use warnings;
use strict;
在你编写的每个程序的顶部,以捕捉这样的问题。