http://www.perlmonks.org?node_id=1018273


in reply to How do you open a PDF file from a perl script in Windows?

Wow, that was fast. Thanks. All good questions. OK, here's what I found when I looked at how I created $outfile:
$0 =~ /(.*)\.pl/; my $outname = "$1.pdf";
This was in a subroutine so $outfile was local. I then tried to use it outside the subroutine so that was the problem. I removed the my and now the above call works. I can also invoke just the name and that works too:
system $outname;
I tried this with exec and I get:
There was an error opening this document. This file cannot be found.
and then it opens when I 'OK' the dialog. What is the syntax for exec? I though it was the same.

Replies are listed 'Best First'.
Re^2: How do you open a PDF file from a perl script in Windows?
by davido (Cardinal) on Feb 12, 2013 at 16:00 UTC

    I'm glad you got it working, but this line haunts me:

    This was in a subroutine so $outfile was local. I then tried to use it outside the subroutine so that was the problem. I removed the my and now the above call works.

    What that tells me is that you aren't using strict, and that you are now creating variables inside of subroutines that you are accessing outside of the subroutines. A better approach would be to let your subroutine return a value that the calling code uses to initialize $outname


    Dave

      Yes, of course, if I had used strict and warnings, this would have been avoided. I know I am following Perl Worst Practices. I will reform my undisciplined coding ways. Thanks, oh brother sufferer, for the admonition. ;) Meanwhile, why is exec behaving so?

        exec requires an executable to run, not a regular file.