0

I have ActiveState Perl installed on a Windows machine.

Now, suppose I have scripts, including one called "SomeScript.pl", in this directory:

"C:\Example\Foo\Bar\"

How do I configure Perl to automatically look in that directory for scripts when it is passed a script path?

For example, from the command line, I can run...:

perl C:\Example\Foo\Bar\SomeScript.pl

...just fine.

But the following...:

perl SomeScript.pl

fails because Perl doesn't know to look in the "C:\Example\Foo\Bar\" directory. How do I configure Perl to automatically look in some specific directory for scripts when passed a script name from the command line?

  • 1
    This might help - http://superuser.com/questions/284342/what-are-path-and-other-environment-variables-and-how-can-i-set-or-use-them – yoniyes Sep 16 '16 at 08:07
  • That link doesn't help. Setting the Windows `PATH` environment variable is only useful when a command is passed to the Windows shell directly, rather than in this case, which is a script filename being passed as a parameter to the Perl executable. – WingedKnight Sep 17 '16 at 01:14

2 Answers2

2

This is possible in at least some Windows Perls (I'm using Strawberry 5.24).

  1. Add the desired path to your Windows PATH. Control Panel > System > Advanced system settings > Environment variables

  2. Run perl with the -S switch.

    perl -S program.plx

All the usual headaches of managing PATH apply (e.g. the first file with that name gets run).

mp3
  • 154
  • 1
  • 6
  • Thank you so much! This works! I tried to upvote you, but as a new user, I apparently don't have enough reputation for the upvote to count. – WingedKnight Sep 18 '16 at 01:07
  • I think maybe there is an option to flag/accept it as the answer. I'm new too so I'm not sure. – mp3 Sep 18 '16 at 14:01
0

It's not a question of perl, it's more to do with your $PATH variable.

If you are on the newest windows 10 you can add the directory to your .bashrc file by appending export PATH=<your directory here>:$PATH to the end of that file.

Alternately, you can also add that directory to your PATH via menus. Edit Environment Variables via the "Advanced" section of "System Properties".

Then restart your command window and you can run the script thus:

SomeScript.pl
Community
  • 1
  • 1
Mike Tung
  • 4,735
  • 1
  • 17
  • 24
  • I don't understand what you mean. There's no `.bashrc` file since the shell is the Windows command prompt, not Bash. If you mean adding the script directory to the Windows `%PATH%` environment variable, I already tried that, and it doesn't work; Perl apparently doesn't check the Windows `%PATH%` environment variable when deciding where to look for scripts. – WingedKnight Sep 16 '16 at 07:03
  • You can't do what you are asking. Only executable programs are searched for in the path, not parameters. Your script isn't an executable. Putting the directory in %PATH% won't do anything. If you really want to just set a variable `set MYFILE=c:\foo\bar\script.pl` then you can do `perl %MYFILE%` – Secto Kia Sep 16 '16 at 09:30
  • it is possible to do it, see link http://stackoverflow.com/questions/23400030/windows-7-add-path – Mike Tung Sep 16 '16 at 13:34
  • Feel free to edit my edit of your answer. :) I was excited when I heard about having bash on Windows, but apparently my versions of Windows 10 have all been too old. – Christopher Bottoms Sep 16 '16 at 14:28
  • That http://www.stackoverflow.com/questions/23400030/windows-7-add-path link is about modifying the Windows `PATH` variable, which is only useful when a command is passed to the Windows shell directly, rather than in this case, which is a script filename being passed as a parameter to the Perl executable. – WingedKnight Sep 17 '16 at 01:28