3

A similar question about the use of

#! /usr/bin/perl

has been answered. But I think I have a slightly different question, although probably still similar to people in the know given my lack of understanding of computation in general.

I don't have UNIX on my Windows so could not test the following directly, but while the shebang line is indeed not needed for me to run Perl under Windows, with the shebang line in the hello.pl, I still could NOT run the file if I move from

c:\Users\XYZ\Desktop\BegPerl

to

c:\Users\XYZ\Desktop

My script is stored under

c:\Users\XYZ\Desktop\BegPerl

My perl folder is stored under

c:\Strawberry\perl\bin\perl.exe

So my questions are:

  1. How does Windows find "perl.exe"?
  2. Why could Windows find hello.pl ONLY when I type perl hello.pl under c:\Users\XYZ\Desktop\BegPerl but NOT under c:\Users\XYZ\Desktop even with the shebang line included?

The answer I got about the "purpose" of the shebang line is

for UNIX-like systems to locate the executable for scripts

So, I guess the answers to my two questions might have something to do with the difference between UNIX-like systems and Windows.

smartmeta
  • 1,149
  • 1
  • 17
  • 38
B Chen
  • 923
  • 2
  • 12
  • 21
  • Look up the windows PATH and this stackoverflow for setup http://stackoverflow.com/questions/667471/how-do-i-run-programs-with-strawberry-perl. – scrappedcola Feb 12 '13 at 18:38
  • 1
    On Windows, Perl will examine the shebang line for command-line switches (e.g., the `-T -w` in `#!C:/Strawberry/perl/bin/perl.exe -T -w`). But the OP is right that Windows will not use the shebang line to determine where to find which Perl executable to use. – mob Feb 12 '13 at 18:48

2 Answers2

6

By associating the pl file extension with perl.exe in the OS registry. The directory where perl.exe is should be in the system PATH.

Both should happen when you install perl but if they are not, that is what you need to do.

amphibient
  • 29,770
  • 54
  • 146
  • 240
  • Thanks, foampile. perl.exe (and strawberry) is indeed in PATH. And I've added pl in PATHEXT. didn't seem to have effects (on perl hello.pl) though. But still thanks a lot for all the info. I am not a comp sci guy, but am just interested in knowing how things work and the provided answers have helped me understand a bit better how Windows retrieves files and executes commands. Will give you a vote :D – B Chen Feb 12 '13 at 19:47
1
  1. dir/perl script.pl or /dir/perl script.pl

    • Unix: The OS will search for perl in the specified path. The shebang line will not be used.*
    • Win: The OS will search for perl in the specified path. The shebang line will not be used.*

  2. perl script.pl

    • Unix: The OS will search for perl at the paths in the PATH env var. The shebang line will not be used.*
    • Win: The OS will search for perl relative to the current directory and at the paths in the PATH env var. The shebang line will not be used.*

  3. script.pl

    • Unix: The OS will for look for a shebang line, and execute the indicated program and arguments followed by name of the script and command line arguments.
    • Win: The OS will look for a file association for a file of that extension, and execute the indicated program and arguments. %1 will be replaced with the file's name. %* will be replaced with the command line arguments.

* — The shebang line will not be used by the system. Perl will actually do its own interpretation of the shebang line in case it wasn't used. This allows a -w there to take effect even when the shebang line wasn't used.

ikegami
  • 367,544
  • 15
  • 269
  • 518