How do I create drag-and-drop Strawberry Perl programs?
Asked Answered
A

3

13

I've got a Strawberry Perl program that accepts a single-file as a command-line argument. How can I set things up such that I can drag and drop the desired file onto the Strawberry Perl program (or a wrapper around it) and the program runs with that file's name as an argument?

Actium answered 1/4, 2009 at 14:44 Comment(3)
I believe that depends much more on your operating system than your programming language. What OS and version are you using?Commissariat
@Jack M. -- Strawberry Perl is the new official perl for Windows. The operating system is therefore implicit in my question for (I presume) anyone who would know the answer.Actium
For clarity: Strawberry Perl is a community-supported Perl for Windows with a bundled gcc compiler. While it is not "the official perl" (an irrelevant concept for Perl), it has become quite popular in the last couple years. strawberryperl.comPolycythemia
C
21

Under Windows (tested with XP), you can create a .cmd file and simply have it run the Perl program with the argument of %1 to pass the filename over, as if executed by commandline.

perl c:\test.pl %1

Then you can simply drag and drop a file onto the .cmd file to execute.

Celaeno answered 1/4, 2009 at 14:57 Comment(7)
You can also then drop this file in your 'send to' folder and you can then right click -> send to in the shell.Discriminant
Frakkle, you are the man! Followup question: how do I make the console window not close immediately after displaying its output? :)Actium
Robert, thanks; I have a coworker who wanted to make it work that way.Actium
Answer to my followup question: pauseActium
@Actium - Add "&& pause" to end of the command you're running. For example, I made a wrapper to the RCS checkout command (co.exe) on my desktop (here's a case where a wrapper is good!). The contents of the wrapper simply say "co.exe -l %1 && pause" and it will pause after checking out the file!Dental
jimtut is correct; you can add pause to make it wait until you press a key.Celaeno
Careful with the "&& pause"... this will only pause if the previous command succeeds. Instead, use "& pause" (to always pause), or "|| pause" (to pause only on errors).Ene
D
19

Eeek! Please don't create a wrapper script/cmd when you don't need to.

Go into your Registry or your File Type dialog box in Windows, and redefine the Perl default action to say:

"C:\path-to-perl-folders\perl.exe" "%1" %*

This will cause double-clicking the .PL to launch perl.exe with the name of the double-clicked file (%1). The %* stuff (passing any filename arguments to the Perl script) is trickier.

Go into the Registry again (really, it's not as scary as people think) and find/create a "shellex" key under the Perl class, and then create a sub-key called "DropHandler" with a default string value of "{86C86720-42A0-1069-A2E8-08002B30309D}" (at least, that's my DropHandler in the US version of Windows XP).

This allows .pl files (actually, anything associated with the Perl class) to have a drop handler that tells Explorer what to do when you drop file(s) on the .pl script. In this case, it just means "run the Perl script with the dropped file(s) as arguments".

Hmmm, I don't think I explained that very well, but that's how I've set up Perl (running off a network drive) for a large engineering organization. Google for Perl and DropHandler, and you should be able to get the .reg Registry script to do this for you.

Dental answered 1/4, 2009 at 16:38 Comment(1)
Good call! I was unaware of this. Of course, the advantage of the wrapper script method is that it is very easy to deploy to other machines. There's pros and cons to both methods I suppose, but this is a cool solution.Celaeno
E
1

Here's another alternative to a "wrapper", but it requires a slight modification to the perl script:

  1. Rename your script.pl script to script.cmd.
  2. Add the following to the top of the file:

@SETLOCAL ENABLEEXTENSIONS
@c:\path\to\perl.exe -x "%~f0" %*
@exit /b %ERRORLEVEL%
#!perl
#line 6
# ...perl script continues here...

The script is run like any other batch file. The first three lines basically invokes Perl on the CMD file itself (%~f0, which only works if CMD extensions are turned on). The -x paremeter to perl.exe tells Perl to skip everything until the #!perl line. "#line 6" just aids in debugging.

This is my preferred solution when I don't know much about the target system (and may not be able to edit the registry).

Ene answered 29/6, 2009 at 1:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.