Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Access an executable from perl script

by abcdefg (Acolyte)
on Feb 28, 2012 at 22:57 UTC ( [id://956789]=perlquestion: print w/replies, xml ) Need Help??

abcdefg has asked for the wisdom of the Perl Monks concerning the following question:

Hi There, I have a client server application in which client sends a input file to server ,server uses a tool called abc.exe on that input file, which gives an output file and sends it back to client.The name for the output file is basically output_input file.if the output file with the same name is already present then it increments the suffix at each run. I have written this code but its not working correctly.I guess there is some problem with the $abc_cmd.Can anyone tell me what I am doing wrong?

# Read the signing image_type from the client app via the sock +et $image_type = <$client_socket>; chop($image_type); # Setup some default filenames and command strings. my $unsigned_file = "unsigned.bin"; my $signed_filename; my $output_file = "signed.$unsigned_file"; my $rcv_filesize; my $hsm_cmd; my $buf; my $error = "ACK\n"; $rcv_filesize = 0; my $i = 1; while (-e $output_file) { $output_file = "output.$input_file$i"; $i++; } $abc_cmd = "abc.exe" -i $input_file -o $output_file"; print $client_socket "ACK\n"; system $abc_cmd; # Run the command print "DONE\n"; # Open the file for reading open(INFILE, $output_file) || die "ERROR: Could not open out +put_file for reading!\n"; binmode INFILE; { local $/; $output_data = <INFILE>; } close(INFILE); print "$prefix Sending $output_file to the client... "; $client_socket->write($output_data, length($output_data)); print length($output_data), " bytes: DONE\n"; $client_socket->close(); print "$prefix Socket closed, operation completed!\n";

Replies are listed 'Best First'.
Re: Access an executable from perl script
by kennethk (Abbot) on Feb 28, 2012 at 23:03 UTC

    There are a few ways to run external executables from perl. The easiest, and what you probably want, is backticks: see `STRING`. For this, your command might look like:

    $abc_cmd = `abc.exe -i $input_file -o $output_file >null`;

    Other possibilities include system, open and IPC::Open3, for which you can use perlipc for reference.

    Update: (as per JavaFan's comment) But of course, if you redirect your output (with >null) or put it in some output file (with -o $output_file, then why would you expect to capture it?

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      $abc_cmd = `abc.exe -i $input_file -o $output_file >null`;
      I don't get this. You're redirecting the output of abc.exe to the file null, and then you want to capture the output of that.

      What do you expect $abc_cmd to be afterwards?

Re: Access an executable from perl script
by rovf (Priest) on Feb 29, 2012 at 12:05 UTC
    Running the executable is done by system. But what do you mean by
    and return the output file

    ? Do you mean that you want to process the file $output_file afterwards? This is not different from processing any other file. See for instance open or IO::File.

    -- 
    Ronald Fischer <ynnor@mm.st>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://956789]
Approved by kennethk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-25 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found