簡體   English   中英

在Perl中將輸入文件的名稱添加為字符串作為輸出文件的名稱

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM