将输入文件的名称添加到字符串中作为Perl中输出文件的名称

时间:2018-01-25 21:05:29

标签: perl output concatenation concat

我使用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.

1 个答案:

答案 0 :(得分:3)

$base = basename $text;

$text未在任何地方定义。因此,您将undef传递给basename函数。

你应该

use warnings;
use strict;

在你编写的每个程序的顶部,以捕捉这样的问题。