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

Dear Monks,

I have a menu bar:
my $menu=$mw->Menubutton ( -text=>'Information', -background=>'lightblue', -foreground=>'blue', -activebackground=>'cyan', -activeforeground=>'blue', -menuitems=>[ [ 'command' => "About this program", "-command" => \&about, "-underline" => 0], [ 'command' => "User Guide", "-command" => \&help, "-underline" => 0 ] ])->pack(-anchor => 'ne');
and a sub:
sub help { #open (file.pdf); }#help
I would like that when my user clicks on "User Guide" s/he can read that file in its original format (pdf) and keep it open while s/he is interacting with the application.
I would really appreciate any help.
Thanks,
FM

Replies are listed 'Best First'.
Re: Perl Tk: opening a file in its original format
by Corion (Patriarch) on Oct 17, 2005 at 06:31 UTC

    What you want to do is commonly called "launch an external program and display a file through it", or at least that's how I understand your question.

    If you're using Windows, it is really easy to launch whatever program the user configured for whatever kind of file you want to "display":

    my @args = ("start",$file,$file); for (@args) { # Apply smart-ish double quotes if (/ /) { $_ = qq{"$_"}; } }; system(@args) == 0 or warn "Couldn't launch '$file' : $!/$?/$^E";

    Under Unix, it is much harder to know what program the user wants to be launched and you will likely want to make this an option in a config file or in the environment. The idea still remains the same though.

    Updated: Fixed missing closing brace, spotted by rcseege

      Under Unix, it is much harder to know what program the user wants to be launched and you will likely want to make this an option in a config file or in the environment. The idea still remains the same though.

      For values of "Unix" near "OS X" you can use the open command which functions similarly to start (opening the file in the associated application).

      I want to thank you both very much for your help. However, since I am not that familiar with Perl yet, I would love for Corion to explain to me why we need "start" and then two $file in the array? What does the qq stands for? Hope to hear from you. FM

        Let's look at my code:

        Starting at the top, I assemble the command and parameter list in the @args array. The start command on Win32 takes the command to run as the first or second parameter, depending no the shell/OS. command.com takes it as the first parameter while cmd.exe takes it as the second parameter. So you can consider it as an old habit to put the file twice there. Typing help start in cmd.exe will tell you lots about other options.

        my @args = ("start",$file,$file);

        Next, I put all parameters that have spaces in them into double quotes. This is a very crude mechanism, because it doesn't pay attention to other characters that might confuse the shell, like ?,&,| or %. To prepend and append a double quote, I use alternative double quotes by using the qq quoting operator, see perlop or perldoc perlop, in case the link doesn't work.

        for (@args) { # Apply smart-ish double quotes if (/ /) { $_ = qq{"$_"}; } };

        Two alternative ways to achieve the same quoting would be :

        $_ = '"' . $_ . '"'; # or $_ = "\"$_\"";

        So I think you should see why I prefer $_ = qq{"$_"} over that.

        The only thing that remains is to hand the parameter list to the shell:

        system(@args) == 0 or warn "Couldn't launch '$file' : $!/$?/$^E";
Re: Perl Tk: opening a file in its original format
by rcseege (Pilgrim) on Oct 17, 2005 at 06:35 UTC

    Another way is to use Win32::Process. I knew from the Chatterbox that you were looking for a Windows solution. This worked for me, but it will probably need modification from you to work correctly. In particular, look at the second and third params to the Create function.

    use strict; use Tk; use Win32::Process; my $process; my $mw = MainWindow->new; $mw->Button( -text => "Open Adobe", -command => \&openAdobe )->pack; MainLoop; sub openAdobe { Win32::Process::Create( $process, "C:\\Program Files\\Adobe\\Acrobat 6.0\\Reader\\AcroRd32.exe", "/n /s my.pdf", 0, DETACHED_PROCESS, "."); }

    Do a search for Win32::Process on the the comp.lang.perl.tk newsgroup through Google groups or lots of other examples. Also check out the docs for Win32::Process.

    Update: Corion's approach is better in many ways, though he is missing a closing brace ;-) The advantages should be clear, but I'll post them anyhow:

    1. It's simpler.
    2. No pesky Win32::Process library with the UGLY Create call
    3. No need to have knowledge of where Acrobat is installed on client machines.

    Rob