Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Reading output of external program without Shell

by Anonymous Monk
on Nov 15, 2017 at 13:24 UTC ( [id://1203473]=note: print w/replies, xml ) Need Help??


in reply to Reading output of external program without Shell

Thank you all!

At the end I opted for writing the content of the PDF in a temp file and read it again. Not very elegant thought...

use strict; use warnings; use File::Temp qw(tempfile); my $temp = new File::Temp( UNLINK => 0, SUFFIX => '.txt' ); system ("pdftotext","-enc", "UTF-8","myfile.pdf","$temp"); local $/=undef; open(my $fh, '<:encoding(UTF-8)', $temp) or die "Could not open file ' +$temp' $!"; print my $string = <$fh>;

Replies are listed 'Best First'.
Re^2: Reading output of external program without Shell
by haukex (Archbishop) on Nov 15, 2017 at 13:47 UTC

    Good choice to use File::Temp and the list form of system, but don't forget to check its return value - like system(...)==0 or die "system failed, \$?=$?";

    Not very elegant thought...

    I think this is a case where reliability is elegant :-) Temp files are unlikely to have filename conflicts, are created in locations where they don't disturb the user (unlike some programs that create temp files in the user's home directory and/or use fixed names), and you get automatic cleanup (although I'm not sure why you set UNLINK=>0). Overall they're a good solution, and in fact IPC::Run3 makes heavy use of them, AFAIK it's one of the reasons it's so portable.

      Strongly agree – "it is elegant if it works, reliably and consistently, and does not take a lot of head-banging to develop." Such "inelegant" procedures also have the advantage of being easier to debug, or to change to meet future requirements, because they do employ a temporary file whose contents can be inspected. (And also because, "first, one step runs and runs to completion, then, the next step begins," and so on.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-20 05:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found