Search code examples
pythonperl

How to call a python script from Perl?


I need to call "/usr/bin/pdf2txt.py" with few arguments from my Perl script. How should i do this ?


Solution

  • If you need to capture STDOUT:

    my $ret = `/usr/bin/pdf2txt.py arg1 arg2`;
    

    You can easily capture STDERR redirecting it to STDOUT:

    my $ret = `/usr/bin/pdf2txt.py arg1 arg2 2>&1`;
    

    If you need to capture the exit status, then you can use:

    my $ret = system("/usr/bin/pdf2txt.py arg1 arg2");
    

    Take in mind that both `` and system() block until the program finishes execution.

    If you don't want to wait, or you need to capture both STDOUT/STDERR and exit status, then you should use IPC::Open3.