【问题标题】:how to execute the perl script when the script names are written in txt file当脚本名称写入txt文件时如何执行perl脚本
【发布时间】:2014-10-15 08:47:58
【问题描述】:

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

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

提前致谢。

【问题讨论】:

  • 到目前为止您尝试过什么?要非常小心 - 从文件中读取“要运行的东西”可能非常危险。
  • 我是 perl 新手,我能够读取文件的内容,也能够检查该文件是否存在,但不知道如何从那里运行。
  • 在这种情况下,这可能会有所帮助:stackoverflow.com/help/how-to-ask
  • 感谢您的建议。!!

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


【解决方案1】:

试试下面的代码。它将执行所有将标志设置为 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);

【讨论】:

    猜你喜欢
    • 2014-03-24
    • 2016-08-17
    • 2014-03-02
    • 2013-04-08
    • 2019-11-11
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多