-1

Question : I have a normal txt file which has Perl Script names and flag set to it,please see example below, i want to read that txt file and if the flag is set to 1 then execute the perl scripts . How to do this ?

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

Thanks in Advance.

  • 1
    What have you tried so far? Be very careful - reading 'things to run' out of a file is potentially extremely dangerous. – Sobrique Oct 15 '14 at 08:52
  • iam new to perl,iam able to reading the contents of the file also iam able to check whether that file is present or not but dont know how to run from there. – user1652137 Oct 15 '14 at 09:08
  • In which case, this might be helpful: http://stackoverflow.com/help/how-to-ask – Sobrique Oct 15 '14 at 09:19

1 Answers1

0

Try the below code . It will execute all the perl scripts which set the flag to 1

Input File:(test.txt)

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

Code:

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);
Miller
  • 34,962
  • 4
  • 39
  • 60
Praveen
  • 902
  • 6
  • 21