Just another Perl shrine PerlMonks

### Re: [implementation specific to windows] writing a proper batch file for terminal start-up

by harangzsolt33 (Pilgrim)
 on Oct 15, 2019 at 23:20 UTC Need Help??

I think, the solution to your problem might be getting rid of one or more instances of perl. Just have one. Why have more?

I'm currently running 32-bit Windows XP Pro with TinyPerl 5.8.0 here. This is the only perl on my computer located at C:\BIN\PERL. In that same folder I created a file called RUN.BAT, which contains the following:

@ECHO OFF
C:\BIN\PERL\TINYPERL -I C:\BIN\PERL\LIB "%1" %2 %3
ECHO.
PAUSE

When I double-click on a PL file, I associated it with RUN.BAT, so when I click to open, it runs and the terminal window stays open until I press enter. It's a pretty simple solution. (I don't have any environmental variables set for perl. I just pass the necessary values in the command line.)

I have also downloaded an icon that looks like a blue script thingy. And I created a REG file, which ties the PL and PM and CGI extensions to that icon file. So, all PL files on my computer appear with that little blue icon. I can very easily distinguish perl scripts from other files.

So, the PERL.REG file looks like this:

REGEDIT4

[HKEY_CLASSES_ROOT\pm_auto_file]
[HKEY_CLASSES_ROOT\pm_auto_file]
@=""
[HKEY_CLASSES_ROOT\pm_auto_file\DefaultIcon]
[HKEY_CLASSES_ROOT\pm_auto_file\DefaultIcon]
@="C:\\BIN\\PERL\\PERL.ICO"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\pm_auto_file]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\pm_auto_file\DefaultIcon]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\pm_auto_file\DefaultIcon]
@="C:\\BIN\\PERL\\PERL.ICO"

[HKEY_CLASSES_ROOT\cgi_auto_file]
[HKEY_CLASSES_ROOT\cgi_auto_file]
@=""
[HKEY_CLASSES_ROOT\cgi_auto_file\DefaultIcon]
[HKEY_CLASSES_ROOT\cgi_auto_file\DefaultIcon]
@="C:\\BIN\\PERL\\PERL.ICO"

[HKEY_CLASSES_ROOT\cgi_auto_file\shell]
[HKEY_CLASSES_ROOT\cgi_auto_file\shell\edit]
[HKEY_CLASSES_ROOT\cgi_auto_file\shell\edit\command]

[HKEY_CLASSES_ROOT\cgi_auto_file\shell\open]
[HKEY_CLASSES_ROOT\cgi_auto_file\shell\open\command]
@="\"C:\\BIN\\PERL\\runcgi.bat\" \"%1\""

[HKEY_CLASSES_ROOT\pl_auto_file]
[HKEY_CLASSES_ROOT\pl_auto_file]
@=""
[HKEY_CLASSES_ROOT\pl_auto_file\DefaultIcon]
[HKEY_CLASSES_ROOT\pl_auto_file\DefaultIcon]
@="C:\\BIN\\PERL\\PERL.ICO"

[HKEY_CLASSES_ROOT\pl_auto_file\shell]
[HKEY_CLASSES_ROOT\pl_auto_file\shell\edit]
[HKEY_CLASSES_ROOT\pl_auto_file\shell\edit\command]

[HKEY_CLASSES_ROOT\pl_auto_file\shell\open]
[HKEY_CLASSES_ROOT\pl_auto_file\shell\open\command]
@="\"C:\\BIN\\PERL\\run.bat\" \"%1\""

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\cgi_auto_file]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\cgi_auto_file\DefaultIcon]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\cgi_auto_file\DefaultIcon]
@="C:\\BIN\\PERL\\PERL.ICO"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\cgi_auto_file\shell]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\cgi_auto_file\shell\edit]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\cgi_auto_file\shell\edit\command]

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\cgi_auto_file\shell\edit\command]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\cgi_auto_file\shell\open]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\cgi_auto_file\shell\open\command]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\cgi_auto_file\shell\open\command]
@="\"C:\\BIN\\PERL\\runcgi.bat\" \"%1\""

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\pl_auto_file]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\pl_auto_file\DefaultIcon]
@="C:\\BIN\\PERL\\PERL.ICO"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\pl_auto_file\shell]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\pl_auto_file\shell\edit]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\pl_auto_file\shell\edit\command]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\pl_auto_file\shell\open]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\pl_auto_file\shell\open\command]
@="\"C:\\BIN\\PERL\\run.bat\" \"%1\""

[download]

When I click on CGI files, an entirely different script runs which saves the perl output as a html file and then opens it with Internet Explorer. So, I get to see what the output might look like if the script were to run on a server.

RUNCGI.BAT:

@ECHO OFF
C:\BIN\PERL\TINYPERL -I C:\BIN\PERL\LIB "%1" > C:\BIN\PERL\OUTPUT.HTM

C:
CD "C:\Program Files\Internet Explorer"
START IEXPLORE.EXE C:\BIN\PERL\OUTPUT.HTM
EXIT
[download]
• Comment on Re: [implementation specific to windows] writing a proper batch file for terminal start-up

Replies are listed 'Best First'.
Re^2: [implementation specific to windows] writing a proper batch file for terminal start-up
by swl (Curate) on Oct 15, 2019 at 23:34 UTC

The issue is that Git for Windows comes with its own perl, as do other systems like MSYS2. If these are all in the path the simplest(?) solution is to ensure the one you want to use is pushed to the front of the path when it is needed.

In the case of Strawberry perl, the portable version comes with a script to do so, and this would appear to be the original source of the OP's script (or share a common source). See https://github.com/StrawberryPerl/Perl-Dist-Strawberry/blob/master/share/portable/portableshell.bat.

Re^2: [implementation specific to windows] writing a proper batch file for terminal start-up
by Aldebaran (Chaplain) on Oct 19, 2019 at 00:35 UTC
When I double-click on a PL file, I associated it with RUN.BAT, so when I click to open, it runs and the terminal window stays open until I press enter.

I have been reseaching and puzzling on this response and thank you for it. Right now, when I click on a .pl, a dos terminal with strawberry perl exe on the top appears, executes the program and then disappears before I can lay eyes on it. This would be a great time to change this. In this script:

@ECHO OFF
C:\BIN\PERL\TINYPERL -I C:\BIN\PERL\LIB "%1" %2 %3
ECHO.
PAUSE
[download]

, what are to be %2 %3 ?

I have been trying to replicate what I see here and at this link another link, but I seem to have come up short in a command prompt in administrator mode:

PS C:\Strawberry> Ftype Perl_program_file="C:\Strawberry\perl\bin\perl
+.exe" %1
Ftype : The term 'Ftype' is not recognized as the name of a cmdlet, fu
+nction, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the p
+ath is correct and try again.
At line:1 char:1
+ Ftype Perl_program_file="C:\Strawberry\perl\bin\perl.exe" %1
+ ~~~~~
+ CategoryInfo          : ObjectNotFound: (Ftype:String) [], Comma
+ndNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

PS C:\Strawberry>
[download]

The assoc command works here:

C:\Users\tblaz>assoc .pl
.pl=Perl_program_file

C:\Users\tblaz>
[download]

When I try to fold this into a useful batch file, Perl_program_file doesn't seem to do anything yet:

@echo off
rem set env variables
set TERM=dumb
set PERL_JSON_BACKEND=JSON::XS
set PERL_YAML_BACKEND=YAML
rem avoid collisions with other perl stuff on your system
rem set PERL5LIB=
set PERL5OPT=
set PERL_MM_OPT=
set PERL_MB_OPT=

perl -e "printf("""Perl executable: %%s\nPerl version   : %%vd\n""", $+^X,$^V)" 2>nul
if ERRORLEVEL==1 echo.&echo FATAL ERROR: 'perl' does not work; check i
+f your strawberry pack is complete!

Ftype Perl_program_file="C:\Strawberry\perl\bin\perl.exe" %1
ECHO.
PAUSE
[download]

When I double click on the .bat file that this is I get:

Perl executable: C:\Strawberry\perl\bin\perl.exe
Perl version   : 5.30.0
+with it.

Press any key to continue . . .
[download]

At least I can read the output, but I'm not getting this yet. Do I need to create a .reg file to reassociate .pl?

"I seem to have come up short in a command prompt in administrator mode:"

The examples you link to instruct you to open a command prompt, you have executed this in a power shell session, not a command prompt. I find it simpler, more productive and more portable just to open up a a command prompt and run the programs from there, after the initial setup. If you are using a fancy IDE it may already have such functionality available.

Update: typo excited/executed, autocorrect...

The examples you link to instruct you to open a command prompt, you have [executed] this in a power shell session, not a command prompt.

I repeated and found the command prompt that opened as admin. The commands accepted the syntax, but it did not seem to change the behavior of windows when a .pl file was thereafter double-clicked on. In settings, I changed the default app to open to be notepad++, which is probably the best outcome anyways. They don't want to let you open files with programs that you have rolled yourself but that it fit into the app world. I felt very uncomfortable in both the power shell and the admin shell. If it's not a one and done, then I don't want to do it again.

I find it simpler, more productive and more portable just to open up a a command prompt and run the programs from there, after the initial setup.

Indeed, what I *really, really* miss is the ability in ubuntu to right click and "open terminal here." Right now, I *can* get a terminal to open on right-click, but it's the bash terminal from Git, and it's a whole different animal than a dos terminal. I really feel like I crawl around with dos. It's one of the reasons that I find myself always returning to 'nix systems. I heard about a Raspberry Pi at a social event this weekend and can't wait to get one.

I would like to thank everyone for taking the time to address these implementation-specific issues. Speaking only for me, I wouldn't understand anything about contemporary Windows without perl. Here is my latest startup script,

C:\Users\tblaz\Documents\evelyn>type 3.startup.bat
@echo off
rem set env variables
set TERM=dumb
set PERL_JSON_BACKEND=JSON::XS
set PERL_YAML_BACKEND=YAML
rem avoid collisions with other perl stuff on your system
rem set PERL5LIB=
set PERL5OPT=
set PERL_MM_OPT=
set PERL_MB_OPT=

perl -e "printf("""Perl executable: %%s\nPerl version   : %%vd\n""", $+^X,$^V)" 2>nul
if ERRORLEVEL==1 echo.&echo FATAL ERROR: 'perl' does not work; check i
+f your strawberry pack is complete!

rem a couple things that didn't quite work...privileges a problem
rem assoc .pl=PerlScript
rem ftype PerlScript=C:\Strawberry\perl\bin\perl.exe "%1" %*

cd C:\Users\tblaz\Documents\evelyn\
perl 8.monktag.pl

C:\Users\tblaz\Documents\evelyn>
[download]

This is probably good enough for now.

"Right now, when I click on a .pl, a dos terminal with strawberry perl exe on the top appears, executes the program and then disappears before I can lay eyes on it."

Yes, see, that's why I associated the .pl files with a bat file. Here's how to do it:

* On Windows XP, you would right-click on any .pl file, and go to "Open With..." and click on "Choose Program..." and then "Browse." Then find the bat file and put a checkmark next to "Always use the selected program to open this kind of file" and click on OK.

* On Windows 7, right-click on any .pl file, and go to "Open With..." and click on "Choose default program..." Then click on "Browse" and find the bat file and put a checkmark next to "Always use the selected program to open this kind of file" and click OK.

* On Windows 10, you have to right-click on any .pl file, and click on "Open With" and then "Choose another app" and then in the new window click "More Apps" and then scroll down and click on "Look for anther app on this PC." and then find that bat file, and put a checkmark next to "Always use this app to open .pl files." and click on OK.

The REG file changes the icon that appears next to .pl files. You can ignore that. That just changes the look and feel. If you do decide to use a custom icon for pl files, you have to get the icon file first, then edit the reg file. Make sure the paths are correct. Double-click on the reg file... and then associate the pl file with the bat file. That should be the last thing.

But you have to have the bat file ready first. I would just call it run.bat to make it short, and in your case it may look like this:

@ECHO OFF
set TERM=
set PERL_JSON_BACKEND=
set PERL_YAML_BACKEND=
set PERL5LIB=
set PERL5OPT=
set PERL_MM_OPT=
set PERL_MB_OPT=
C:\Strawberry\perl\bin\perl.exe -I C:\Strawberry\Perl\lib "%1" %2 %3
ECHO.
PAUSE
[download]

Explanation: I am not sure why you have to set all those various environmental variables in the beginning, but I am assuming that they are necessary for something. So, we clear those values first. Then we call C:\Strawberry\perl\bin\perl.exe by giving its exact location. If you say you want to run the perl.exe which is located in the folder "C:\Strawberry\perl\bin" then you can be sure that that's the version of perl that will run! Especially if you also specify the location of the lib folder, you'll definitely get that perl running, and not something else. The %1 and %2 and %3 in the command line are the arguments. The first argument is the file name which is going to be %1. And then if you pass two more arguments to your perl program, then those would be %2 and %3. You could add %4 and %5 %6 %7 %8 %9 to make a full list.

So, let's say you're in command prompt, and you enter:

C:\Strawberry\perl>run.bat example.pl Hello World

In this case, your run.bat program runs the perl program and passes two arguments "Hello" and "World" so "Hello" becomes %2 and "World" becomes %3. Your perl program will receive those arguments. On the other hand, if you omit %2 %3 %4 etc... then your perl program won't receive any arguments even if you do try to pass some arguments.

The bat file is a pretty simple program. You could try and omit all those lines that start with "set" and see what happens. I think, those are totally unnecessary, but I could be wrong.

You could insert these two lines however:

SET PATH=C:\Strawberry\perl\site\bin;%PATH%
SET PATH=C:\Strawberry\perl\bin;%PATH%

These two lines add the strawberry bin folder to the path, so if perl or your program calls any executable that may be located in Strawberry/perl/bin, then those programs get executed.

SET PATH=C:\WINDOWS;WINDOWS\SYSTEM32;C:\Strawberry\Perl\bin;
[download]

That's all you have to do, and it should work.

Okay. I am not sure what this is what you wrote:

"C:\Strawberry> Ftype Perl_program_file="C:\Strawberry\perl\bin\perl +.exe" %1 Ftype : The term 'Ftype' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + Ftype Perl_program_file="C:\Strawberry\perl\bin\perl.exe" %1"

If you're in the terminal window staring at command prompt, then you would run a perl script by entering the following command:

C:\Users\tblaz\Desktop>C:\Strawberry\perl\run example.pl

OR you could just enter:

C:\Users\tblaz\Desktop>run example.pl

This will work if you have added "C:\Strawberry\perl" to your system PATH. If you haven't yet, then go to Control Panel >> System >> Advanced >> Environment Variables. and find the Path string and update it by adding "C:\Strawberry\perl" to the end of the long string. Separate each path with a semi-colon ; So, your path string will probably look something like this :

Path=%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Strawberry\Perl;

* On Windows 10, you have to right-click on any .pl file, and click on "Open With" and then "Choose another app" and then in the new window click "More Apps" and then scroll down and click on "Look for anther app on this PC." and then find that bat file, and put a checkmark next to "Always use this app to open .pl files." and click on OK.

I'm not finding this functionality. I either get to open this with an existing app or one that MS is gonna sell me.

It took me a while to realize that there is no run command in neither dos nor bash:

C:\Users\tblaz\Documents\evelyn>run 8.monk.tag
'run' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\tblaz\Documents\evelyn>
[download]
tblaz@LAPTOP-ECV9SQTM MINGW64 ~/Documents/evelyn
$run anything bash: run: command not found tblaz@LAPTOP-ECV9SQTM MINGW64 ~/Documents/evelyn$
[download]

When you suggest syntax like this:

C:\Users\tblaz\Desktop>run example.pl

, it is because you have a run.bat in the path. I have a nomenclature where I prefer to put a numeral first on any file I can. I copied the idea tho and have this:

C:\Strawberry\perl\site\bin>type 2.run.bat
@ECHO OFF
rem set env variables
set TERM=dumb
set PERL_JSON_BACKEND=JSON::XS
set PERL_YAML_BACKEND=YAML
rem avoid collisions with other perl stuff on your system
rem set PERL5LIB=
set PERL5OPT=
set PERL_MM_OPT=
set PERL_MB_OPT=
rem change encoding to show cyrillic chars
chcp 65001
rem show which perl
perl -e "printf("""Perl executable: %%s\nPerl version   : %%vd\n""", $+^X,$^V)" 2>nul
if ERRORLEVEL==1 echo.&echo FATAL ERROR: 'perl' does not work; check i
+f your strawberry pack is complete!

C:\Strawberry\perl\bin\perl.exe "%1" %2 %3

ECHO.
PAUSE
cmd \K
C:\Strawberry\perl\site\bin>
[download]

Changing the active code page is a definite plus. Now, instantiated back where my perl scripts are:

C:\Users\tblaz\Documents\evelyn>2.run.bat 2.cyr.pl
Active code page: 65001
Perl executable: C:\Strawberry\perl\bin\perl.exe
Perl version   : 5.30.0
...
Content-Type: text/html; charset=utf-8

<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<title>Ìîé òåñò</title>
<body>
Áàêñ2811
</body>
</html>
Press any key to continue . . .

[download]

I'm happy with the results. There was nothing in this directory before today, so I think it's a fine place for such scripts. (?)

 Directory of C:\Strawberry\perl\site\bin

10/21/2019  04:00 PM    <DIR>          .
10/21/2019  04:00 PM    <DIR>          ..
10/21/2019  12:56 PM                71 1.run.bat
10/21/2019  04:15 PM               581 2.run.bat
2 File(s)            652 bytes
2 Dir(s)  453,237,977,088 bytes free
[download]

Create A New User
Node Status?
node history
Node Type: note [id://11107528]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2019-12-05 23:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Strict and warnings: which comes first?

Results (154 votes). Check out past polls.

Notices?