17

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

4 Answers 4

23

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It's better to use system '/usr/bin/pdf2txt.py', $arg1, $arg2 as this will avoid problems (and dangerous bugs) with shell interpolation.
You can capture both STDOUT and exit status using backticks using $? For example: my $output = `/usr/bin/pdf2txt.py arg1 arg2 2>&1`; my $ret = $?;
10
my $output = `/usr/bin/pdf2txt.py arg1 arg2`;

1 Comment

make sure the script is executable (chmod +x) and that it has a proper hashbang (e.g. #!/usr/bin/env python).
3

If you don't need the script output, but you want the return code, use system():

...
my $bin = "/usr/bin/pdf2txt.py";
my @args = qw(arg1 arg2 arg3);
my $cmd = "$bin ".join(" ", @args);

system ($cmd) == 0 or die "command was unable to run to completion:\n$cmd\n";

1 Comment

Don't join the @args. Use system $bin, @args; - that avoids a number of potentially harmful bugs.
0

IF you want to see output in "real time" and not when script has finished running , Add -u after python. example :

my $ret = system("python -u pdf2txt.py arg1 arg2");

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.