Search code examples
perlpdfpdflib

How do I gather output from an external command in a Perl script?


I have a a tool named TET.EXE, product of the PDFlib family, it is used to extract the co-ordinates of a particular text. Using those coordinates in the Perl script we can extract the required text. This is a manual process to run the .EXE and then give the co-ordinates to Perl, so could any one suggest me to make this entire process hands off.

What I mean is Perl script itself should run the .EXE and get the coordinates required and extract the text. What are the commands to be used in linux to run this perl script? Please, I need your suggestions for the following.
Thanks in advance.


Solution

  • If i understand correctly, you want perl to launch an executable and do something with the text printed to stdout.... in that case there are a few options:

    Using backticks:

    my $output = `TED.EXE`;
    

    This puts the output of the TED.EXE command in the variable $output, and is most likely sufficient for what you need.

    using IPC::Open3:

    use IPC::Open3;
    my($wtr, $rdr, $err);
    my $pid = open3($wtr, $rdr, $err,
                    'some cmd and args', 'optarg', ...);
    

    This runs your command and associates $wtr, $rdr and $err to the standard input, output and error streams.

    There are other ways to do what you want (Expect.pm, Run3, etc), but i believe the above mentioned should be sufficient.