I have a Perl script that produces a pdf $pdffile in a temporary directory $tempdir and opens it using xdg-open. The script should then delete the working directory once the user is done looking at the file. Here's the part of the code that I'm having trouble with:
system "xdg-open $pdffile";
remove_tree($tempdir);
My understanding of system is that it should wait until the command returns before continuing the program. However, when I execute this code, I get a message "Could not open /tmp/diff14969/diffs.pdf". If I replace xdg-open with okular (which is my system default) in the system command, it works as I want it to. (Similarly, it works if I hardcode any pdf viewer that lives on my system, but I don't want to do that for portability reasons.)
My guess is that xdg-open is starting the viewer in a new process and that the system command only waits for xdg-open to finish. Once xdg-open returns successfully, the script removes the temp directory before the viewer can open the file. How can I make the script wait for the actual viewer to finish instead?