Take the 2-minute tour ×

I'm working on Windows 7 and I have installed Strawberry. I would like to run a perl skript (test.pl):

open OUTPUT, ">test.txt";
print OUTPUT "This is a test\n";

by just clicking on the file or redirect with left mouse click to Perl-program (open with/perl.exe). When I do this a console opens for less than a second and disappears but the file test.txt is not created. If I go to the MS command and enter

> C:\myplace>perl test.pl  

it works. I never had this experience before (WinXP, other Windows 7 PC with ActivePerl and Windows 8 with strawberry). I would be very happy if somebody could give me a hint how to solve this problem.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

There are two problems here:

  1. Creating the file where you want it. When double-clicking a perl script to launch it, it is not executed in the context of the folder you have opened in Explorer. If you want to specify an explicit context, do the following near the top of your script:

    use FindBin;  # This is a module that finds the location of your script
    BEGIN { chdir $FindBin::Bin }  # set context to that directory.

    When you then create a new file without an aboslute path, the path is considered relative to that directory.

    You do not have the problem when running the script from the command line, because you have specified the correct path. But if you run it from C:\ like

    C:\> perl myplace/test.pl

    then you have created the file in C\test.txt. The FindBin solution fixes this.

  2. When running a script by double-clicking it, the command line exits before you can inspect the output. This “problem” is shared by all programming languages on Windows. You can force the window to stay open by waiting for some input. You can either do

    system("PAUSE");  # not portable to non-Windows!


    warn "Press enter to exit...\n";
    <STDIN>;  # Read a line from the command line and discard it.
              # Feels akward when launching from the command line

    to wait until an Enter ⏎ is pressed .

    The other solution is to always use the command line for your scripts, which is what I'd actually suggest.

share|improve this answer
Thanks for the answer. I didn't know about FindBin. Very useful. But why this simple test-code above works on all various computers and OS but in one it does not behave in this way? I just wonder ... –  giordano Sep 3 '13 at 20:03

Check what is your script executing folder, as it might differ from C:\myplace

use Cwd;
print getcwd();
sleep 7;
share|improve this answer
Thanks for answer. Yes, it differs (C:\strawberry\perl\bin). But is this a problem? The environment variable is included. Usually, I never had such kind of problems. –  giordano Sep 2 '13 at 15:13
@giordano to be sure, you can try absolute path when writing, ie. open OUTPUT, ">", 'C:\myplace\test.txt'; –  Сухой27 Sep 2 '13 at 15:19

Your Answer


