0

I've this subroutine whitch start a .bat script but I don't see which line do that.

sub Traitement_Proc {

   foreach my $input (@_) {

    my $cmd = Fonctions_communes::ResolvEnvDos($input);
    my ($proc, $args);
    my $chExec;
    if ($cmd =~ /^\"/) {
        ($proc, $args) = $cmd =~ /^\"(.+?)\"(.*)/;
    } else {
        ($proc, $args) = $cmd =~ /^([^\s]+)(.*)/;
    }
    $chExec = File::Spec->catfile($::G_ParamTable{$::cstExecDir}, $proc);
    $chExec = File::Spec->rel2abs($chExec, File::Spec->curdir());
    $chExec = "\"".$chExec."\"" . $args;

    Fonctions_communes::PrintError("  PROC : "._("Execution of script")." <" . $chExec . ">");

    open PROC_OUT, $chExec." 2>&1"." & if ERRORLEVEL 1 exit/b 1"." |";
    while(my $output = <PROC_OUT>) {
        chomp($output);
        Fonctions_communes::PrintError(decode($Fonctions_communes::console_encoding,$output));
    }
    close(PROC_OUT);
    if ($? == 1) {
        Fonctions_communes::PrintError(_("The script [_1] ended in failure.",basename($chExec)).".\n");
        return 0;
    }
}

return 1;

}

inside $input there is the name of bat file whitch passed in argument, there is no $args in my case so the chExec variable is "C:\Users\anes.yahiaoui\Desktop\SPOOC_BD_TU_BD_XX_BD\tieme_RE_PCCA_BD_MAIN\RE_PCCA\BD\avantBDD\Gene_CSV\Proc\IMPORT_INV.bat".

when I call this function (Traitement_proc) my IMPORT_INV will start but I don't see which line do that ?

5
  • Try to use '\\' instead '\' Commented Mar 13, 2020 at 11:27
  • @AndreCarneiro I didn't got what do u mean Commented Mar 13, 2020 at 11:34
  • @AndreCarneiro there is no problem in the code it works! the problem is that I don't see in witch line it starts the .bat file. I think we can start a .bat script with "system" command in perl not "open" ! Commented Mar 13, 2020 at 11:39
  • My mistake! I accidentally give a answer for other question. Sorry! Commented Mar 13, 2020 at 11:41
  • You can use readpipe, exec or system to call your outside function. Commented Mar 13, 2020 at 11:46

1 Answer 1

2

It's open executing the command. Both open(my $pipe, "shell_cmd |") and open(my $pipe, "-|", "shell_cmd") execute a shell command with the other end of the pipe in $pipe attached to its STDOUT.

For example,

use strict;
use warnings;
use feature qw( say );

use Win32::ShellQuote qw( quote_system_string );

open(my $pipe, quote_system_string(@ARGV)." |")
   or die $!;

while (<$pipe>) {
   chomp;
   say("[$_]");
}

if (!close($pipe)) {
   die("Error waiting for child to exit: $!\n")      if $!;
   die("Child killed by signal ".( $? & 0x7F )."\n") if $? & 0x7F;
   die("Child exited with error ".( $? >> 8 )."\n")  if $? >> 8;
}

say("Child completed successfully.");
>a.pl perl -le"print for 1..5"
[1]
[2]
[3]
[4]
[5]
Child completed successfully.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.