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

Hi,
I'm converting a pdf file to HTML file using the following command.
my $cmd = 'pdftohtml ' . $filename_pdf . ' ' . $filename_html . ' -nof +rames -c -i >/dev/null' ; system("$cmd"); open(IN,"$filename_html");
Once after the conversion I'm reading the HTML file and returning back the content.
Most of the time the system call never waits for the conversion to happen.
It tries to open the non existing HTML file ends up with "Cannot open File" Is there any other way to wait till the process gets complete.

Thanks
Jey

Replies are listed 'Best First'.
Re: Sytem Call Not Waiting
by almut (Canon) on Feb 24, 2010 at 07:14 UTC

    (note: OP silently changed the code after my post)

    ... >/null.txt

    That would try to open a file in the root directory. Unless you have the appropriate privileges (i.e. run the command as root) this is likely to fail with "permission denied"... and the entire command won't execute.  Maybe you meant >/dev/null?

    Why aren't you checking if system(...) succeeded?

Re: Sytem Call Not Waiting
by zentara (Cardinal) on Feb 24, 2010 at 13:37 UTC
    My first guess is that the string you put togther for $cmd is not being interpreted right by pdftohtml, and is just quickly failing. It's not that it isn't waiting. Try something like this instead: (experiment around till it works) :-)
    #my $cmd = 'pdftohtml ' . $filename_pdf . ' ' . $filename_html . ' -no +f #+rames -c -i >/dev/null' ; #system("$cmd"); my @args = ( $filename_pdf, $filename_html, 'noframes', '-c-, '-i' , ' +>', '/dev/null' ); system ('pdftohtml', @args);
    Sometimes programs want '>', '/dev/null' , separated. Others might want '> /dev/null' as a single string. Try some variations in the @arg list until you get the desired output. I remember one tricky program I had, that wouldn't accept @args like '-o', "$filename", but needed "-o $filename".... took me all day to figure it out by trial and error. :-)

    UPDATE It was pointed out to me that the /dev/null redirection, may be nonsense, in this particular instance. It didn't make much sense to me either, when I first saw it, but I have seen other apps, like mplayer or mencoder, which required the following odd redirection

    $pid = open(MP, "| mplayer @options init.mpg >/dev/null 2>&1 ");

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: Sytem Call Not Waiting
by Khen1950fx (Canon) on Feb 24, 2010 at 07:37 UTC
    I tried it your way, and I got "No such file" and "Permission denied". pdftohtml generates its output in the current working directory. I believe that if you try any other directory, it'll ignore the command and generate it in the current directory anyway. I tried it this way:
    #!/usr/bin/perl use strict; use warnings; open(STDOUT, '>', 'htmllog'); my $cmd = 'pdftohtml -noframes -c /path/to/.pdf'; system($cmd); exit;
    Note that I wanted to keep a log to double check the number of pages.
Re: Sytem Call Not Waiting
by ZlR (Chaplain) on Feb 24, 2010 at 08:13 UTC
    From perldoc system :

    system PROGRAM LIST Does exactly the same thing as exec LIST , except that a fork is done +first,

    This fork is why "the system call never waits for the conversion to happen"

    Perldoc also provides a way to wait for return :

    system(@args) == 0 or die "system @args failed: $?"

      This fork is why "the system call never waits for the conversion to happen"

      This is wrong.  system always waits for the command to complete. The only case where it won't wait is if the command itself forks another time to run in the background, because in this case the command's parent process will exit immediately.