如何在脚本名称写入txt文件时执行perl脚本

时间:2014-10-15 08:47:59

标签: perl perl-module perl-data-structures

问题:我有一个普通的txt文件,其中包含Perl脚本名称和标志,请参阅下面的示例,我想读取该txt文件,如果该标志设置为1,则执行perl脚本。 怎么做?

test.txt
TC1connect.pl = 1
TC2disconnect.pl = 1
TC3action = 0

先谢谢。

1 个答案:

答案 0 :(得分:0)

尝试以下代码。它将执行所有将标志设置为1

的perl脚本

输入文件:(test.txt)

TC1connect.pl = 1
TC2disconnect.pl = 1
TC3action = 0

<强>代码:

use strict;
use warnings;

open my $fh, '<', "test.txt" or die "Couldn't open the text file";

while (<$fh>) {
    chomp;
    if ( $_ =~ /(.*?\.pl)\s*=\s*(0|1)/ ) {
        my $perl_program = $1;
        if ( $2 == 1 ) {
            system "perl $perl_program" and die "Error running $perl_program: $!";
        }
    }
}

close($fh);
相关问题