[英]Add the name of the input file to a string as the name of the output file in Perl
我已經使用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.
$base = basename $text;
$text
在任何地方都沒有定義。 因此,您要將undef
傳遞給basename
函數。
你應該有
use warnings;
use strict;
在編寫的每個程序的頂部都可以發現類似的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.