Running Perl Scripts Within Eclipse

Christopher H. Laco

Since I've had to tinker myself to death twice to figure this out, trying every variation under the sun to avoid various path, @INC and working directory issues, I might as well post it here so I can find it again later after the next OS reinstall. :-)

It appears that sometime around 3.0 M6 to M8, Eclipse changes the way it interacts with the Working Directory. What used to work, now doesn't. It appears now that I have to make the Working Directory ${project_loc} to get things to wkr as they should. Anyone got any more info on the changes?

Introduction

If you're running the Eclipse IDE and EPIC, the perl editing plugin for Eclipse, at some point you are going to want to run a script you're writing. At that point, you have two options: a) run it in a seperate command window, constantly switching back and forth between that and the IDE , or b) try and convince Eclipse to run it behind the scenes and display the results in the Console view.

I'm lazy, so I chose the latter of the two. :-)

Unfortunately, setting up the External Tools configuration to run my scripts within the IDE turned out to be harder than it should have been. I'm sure part of that was due to Pilot Error(TM). After some RTFMing and a quick scan of the Windows Command Shell, I stumbled upon the solution below.

Get On With It!

  1. First, open the External Tools configuration window (Figure 2) by choosing Run -> External Tools -> External Tools... from the menubar as shown in Figure 1 below.

    Figure 1: Opening External Tools configuration window
    Figure 1: Opening External Tools configuration window

  2. Next, highlight the Program entry in the Configurations box on the left (Figure 2) and click New button below to create a new configuration as shown in Figure 3.

    Figure 2: External Tools configuration window
    Figure 1: External Tools configuration window

    Figure 3: Creating a new configuration
    Figure 3: Creating a new configuration

  3. In the Name field. type in the display name for this configuration. We're going to be creating a simple configuration to run Perl with warnings enable, so enter perl -w

    In the Location field, we will point Eclipse to use the command shell. By using the command shell instead of the perl executable, we can change directories and run anything we need to prior to actually running the perl script in question. The location of cmd.exe will vary depending on which MS OS you're using. I'm running XP Pro, so I entered C:\WINDOWS\system32\cmd.exe.

    In the Working Directory box, I pointed it to C:\WINDOWS\system32, the same location as cmd.exe. There is probably no reason to do so, but what the hell. It can't hurt right?

    Now for the magic. In the Arguments box I entered the following

    /C "cd ${container_loc} && perl -w ${resource_name}"

    What is all that junk anyhow? Here's a breakdown.

    [cmd.exe] /C
    Carries out the command specified by string and then terminates
    cd ${container_loc}
    Change directory to the directory containing the currently selected perl script in the IDE
    perl -w ${resource_name}
    Run the selected perl script using perl.exe with warnings turned on

    Note: I used perl instead of wperl because I'm running the 1.4.x runtime. If I use wperl instead, I actually get the quick DOS popup window even though wperl is modeless , Run in background is checked, and the 1.4.x runtime supposedly fixed this issue (EPIC FAQ Entry). I suspect the opposite would happen if I were using the 1.3.x runtime.

    After entering the data above, click Apply. You should end up with something looking like Figure 4.

    Figure 4: Enter configuration values
    Figure 4: Enter Main configuration values

  4. Next, click on the Common tab in the configuration you just created and click the External Tools checkbox in the Display in favorites menu (Figure 5) settings group and click Apply. Then hit OK to close the External Tools window.

    Figure 5: Add configuration to External Tools menu
    Figure 5: Add configuration to External Tools menu

  5. If all goes well, your new external tool configuration should be listed in the Run -> External Tools as shown in Figure 6. If not, then don't kill the messenger. :-)

    Figure 6: New configuration in Run -> External Tools menu
    Figure 6: New configuration in Run -> External Tools menu

  6. Highlight a perl script of your choosing in the Navigator view and pick your configuration from the Run -> External Tools menu. If all goes well, the results of your Perl script should now appear in the Console view similiar to Figure 7.

    Figure 7: Perl script outputs into Console view
    Figure 7: Perl script outputs into Console view

There, that wasn't to so bad. If you're feeling creative you can create additional configurations for running make, make test and make clean on CPAN-like packages created using ExtUtils::MakeMaker.