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

Fellow Monks,

Is there a way to determine on a windows platform if a perl script is invoked by double-clicking on the file, through the "File association" ?

My issue is that when I run from a DOS prompt and my script provides logging to STDOUT, the logging is visible after the script ends. When I double-click, the perl script starts in its own DOS box, outputs its logging to STDOUT, finishes, and the DOS box vanishes. I end up having to put:

print "Hit <Enter> to continue\n"; my $y = <STDIN>;
at the end of all my scripts. I want that code to be conditional on being invoked from a double-click file association.

thanks for you help,

- j


I was having trouble with the solution. It's good for double-clicking. However, if I invoke a script from the command line as:
instead of
> perl
I get the wait condition in the first case because the file association is still being used.

I updated to use the Win32::Process::Info module to see if the parent process is Windows Explorer. UPdate 2: forgot a line
=head1 NAME =head1 SYNOPSIS perl -Mouse =head1 DESCRIPTION Install in your perl/lib or perl/site/lib directory in a file named "". Then enter the following commands: assoc .pl=Perl ftype Perl=C:\Progra~1\perl\bin\perl -Mouse "%1" %* Then clicking on a *.pl file will cause the script to run in a new window but the window will (usually) not close when the script finishes. =head1 AUTHOR tye =cut use Win32::Process::Info; END { #-- find the parent process; my $pi = Win32::Process::Info->new(); my @info = $pi->GetProcInfo ( $$); my @pinfo = $pi->GetProcInfo ( @info[0]->{ParentProcessId} ); #-- see if the parent process is the windows explorer. if ( $pinfo[0]->{CommandLine} =~ /explorer/i ) { eval " use Term::ReadKey; "; unless($@) { print "Press any key to close..."; ReadMode(3); ReadKey(); ReadMode(0); } else { print "Press ENTER to close: "; <STDIN>; } } } 1;

Replies are listed 'Best First'.
Re: Windows .pl file assoc vs. cmd prompt
by VSarkiss (Monsignor) on Apr 08, 2005 at 14:10 UTC
      Excellent reference! Elegant and exactly what I was looking for. Thanks

      - j

Re: Windows .pl file assoc vs. cmd prompt
by inman (Curate) on Apr 08, 2005 at 14:56 UTC
    Windows Explorer | Tools | Folder options | File Types tab. Locate the PL association and click the advanced button. Leave the default 'open' to cope with normal running of Perl scripts. Add a new action 'launch' with the following as the command.
    cmd.exe /K C:\Perl\bin\perl.exe "%1" %*

    Close all the dialogs. You can then right click and 'launch' the application. The command windows opens, runs the script and stays open for furter input. i.e. to run your app again with some args.

Re: Windows .pl file assoc vs. cmd prompt
by Anonymous Monk on Apr 08, 2005 at 14:15 UTC

    As far as I understand it, there's not really any way to know that.

    Assuming you're using ActiveState Perl, the default action on files of type .pl is (approximately):

    "d:\perl\bin\perl.exe" "%1" %*

    for values of your AS Perl bin directory, of course. It calls perl, but not in any particularly special way.

    You could maybe modify that to include a special option, like

    "d:\perl\bin\perl.exe" "%1" "--I-was-double-clicked" %*

    but that's an ugly hack, and it'll fail with scripts using Getopt::Long, because (IIRC) those scripts choke and die on command-line options they don't understand.

    You might want to look into Win32 and Win32::GUI. I know it's possible to hide your console window using the latter, and if I can turn up the code in which I did that (a couple years ago), I'll post the relevant exemplary bits. I don't know much about the Win32 API interface in general, though, so can't be much help to you there.

    HTH; HAND.

      ...or I'm not half as smart as I think I am. Anyway...