There are two problems here:
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:\
likeC:\> perl myplace/test.pl
then you have created the file in
C\test.txt
. TheFindBin
solution fixes this.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!
or
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.