CGI/Perl Guide | Learning Center | Forums | Advertise | Login Site Search: in Perl Guide PerlGuru Forums Learning Ctr
 MAIN INDEX SEARCHPOSTS WHO'S ONLINE LOG IN

Home: Perl Programming Help: Intermediate:
How to Get Perl to Automatically Look in Specific Directories for Scripts?

 Print Thread

WingedKnight
Novice

Sep 15, 2016, 6:37 PM

Post #1 of 20 (12366 views)
 How to Get Perl to Automatically Look in Specific Directories for Scripts? Can't Post
I have ActiveState Perl installed on a Windows machine.

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

 Code
`"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...:

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

...just fine.

But the following...:

 Code
`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?

(This post was edited by WingedKnight on Sep 16, 2016, 12:12 AM)

Laurent_R
Veteran / Moderator

Sep 15, 2016, 11:18 PM

Post #2 of 20 (12361 views)
 Re: [WingedKnight] How to Get Perl to Automatically Look in Specific Directories for Scripts? [In reply to] Can't Post
I am using Perl only quite rarely on Windows, so I may be wrong, but this sounds like a Windows configuration issue rather than Perl configuration issue.

WingedKnight
Novice

Sep 16, 2016, 12:21 AM

Post #3 of 20 (12356 views)
 Re: [Laurent_R] How to Get Perl to Automatically Look in Specific Directories for Scripts? [In reply to] Can't Post
 In Reply To
I am using Perl only quite rarely on Windows, so I may be wrong, but this sounds like a Windows configuration issue rather than Perl configuration issue.

The Windows command prompt shell doesn't automatically act on or understand parameters passed to executables. The parameter, the script filename, is being passed to the Perl executable, and it is the Perl executable, not Windows, that is deciding how to interpret the parameter. So it is a Perl configuration issue.

(This post was edited by WingedKnight on Sep 16, 2016, 12:22 AM)

FishMonger
Veteran / Moderator

Sep 16, 2016, 6:17 AM

Post #4 of 20 (12349 views)
 Re: [WingedKnight] How to Get Perl to Automatically Look in Specific Directories for Scripts? [In reply to] Can't Post
How would you do this when using notepad or any other Windows program instead of perl?

The answer is you can't.

Perl works just like every other program in this respect. If not given the path, it will look in the CWD and not some unknown or random or arbitrary directory.

However, since perl is an open source language, you could modify it's source code and build your own custom version of perl to do what you want. Are you up to that challenge?

BillKSmith
Veteran

Sep 16, 2016, 6:47 AM

Post #5 of 20 (12346 views)
 Re: [WingedKnight] How to Get Perl to Automatically Look in Specific Directories for Scripts? [In reply to] Can't Post
There may be a workaround.

I believe that the ActiveState installation associates the extension ".pl" with the perl interpreter. If so, you can type
 Code
`SomeScript.pl`
at the command prompt. (You must type the .pl and not type perl) Windows will search for it in all the directories specified by your environmental variable "Path" and executes it the way you expect when it finds it.
Good Luck,
Bill

FishMonger
Veteran / Moderator

Sep 16, 2016, 7:18 AM

Post #6 of 20 (12344 views)
 Re: [BillKSmith] How to Get Perl to Automatically Look in Specific Directories for Scripts? [In reply to] Can't Post
The file type association specifies the location of the perl interpreter (i.e. perl.exe), not the location of the script you're trying to execute.

If you want to hard code that script directory location to a single directory, then it "might" be possible if you add that path to the value of the "Run" key. That would need to be done by manually editing the registry key.

(This post was edited by FishMonger on Sep 16, 2016, 7:19 AM)

FishMonger
Veteran / Moderator

Sep 16, 2016, 8:35 AM

Post #7 of 20 (12340 views)
 Re: [BillKSmith] How to Get Perl to Automatically Look in Specific Directories for Scripts? [In reply to] Can't Post
I'm going to walk back part of what I previously said.

When installing AS perl via an msi file, having it configure the file type association is an option which can be selected or not selected. In the past the default was not selected, but I think it's now selected by default. However, I've often come across cases where it didn't get configured correctly i.e, it didn't add the required parameters to allow the script to accept arguments.

Once the file type association is setup correctly, you can run the scripts directly, as Bill has shown, without calling perl.exe and passing it the script as an arg.

Just as with any other executable, if the script is not in the CWD, the OS will look for it in the %PATH% directories, which is what Bill was saying. If the OS can't find the script within one of those directories, you'll receive and error. This has nothing to do with perl, it's how the OS works.

If the script is not within one of the %PATH% directories or the CWD, then you MUST specify the full or relative path when executing the script. Again, this is not a perl config issue. It's how operating systems work.

BillKSmith
Veteran

Sep 16, 2016, 12:06 PM

Post #8 of 20 (12334 views)
 Re: [FishMonger] How to Get Perl to Automatically Look in Specific Directories for Scripts? [In reply to] Can't Post
You pointed this out to me several years ago. See post #4 of http://perlguru.com/gforum.cgi?post=47605
Good Luck,
Bill

FishMonger
Veteran / Moderator

Sep 16, 2016, 12:13 PM

Post #9 of 20 (12332 views)
 Re: [BillKSmith] How to Get Perl to Automatically Look in Specific Directories for Scripts? [In reply to] Can't Post
I'm getting old, memory fading and forgetting past postings. :(

WingedKnight
Novice

Sep 16, 2016, 6:35 PM

Post #10 of 20 (12323 views)
 Re: [FishMonger] How to Get Perl to Automatically Look in Specific Directories for Scripts? [In reply to] Can't Post
 Quote
How would you do this when using notepad or any other Windows program instead of perl?

The answer is you can't.

Perl works just like every other program in this respect. If not given the path, it will look in the CWD and not some unknown or random or arbitrary directory.

Notepad can't do it, but there are other text editors which actually do have configuration options letting you set directories that they automatically check when passed a bare filename without a directory path.

It seemed reasonable to think that Perl might have that option considering it does have an @INC variable that controls what directories it looks for modules.

WingedKnight
Novice

Sep 17, 2016, 6:20 PM

Post #11 of 20 (12306 views)
 Re: [WingedKnight] How to Get Perl to Automatically Look in Specific Directories for Scripts? [In reply to] Can't Post
I just learned elsewhere that this actually CAN be done, through the following two steps:

(1) add the directory to the Windows PATH environment file

(2) run Perl with the -S switch

:)

Laurent_R
Veteran / Moderator

Sep 18, 2016, 1:13 AM

Post #12 of 20 (12299 views)
 Re: [WingedKnight] How to Get Perl to Automatically Look in Specific Directories for Scripts? [In reply to] Can't Post
 In Reply To

(1) add the directory to the Windows PATH environment file

As I said, a Windows configuration issue.

WingedKnight
Novice

Sep 18, 2016, 8:24 AM

Post #13 of 20 (12296 views)
 Re: [Laurent_R] How to Get Perl to Automatically Look in Specific Directories for Scripts? [In reply to] Can't Post
 In Reply To
 In Reply To

(1) add the directory to the Windows PATH environment file

As I said, a Windows configuration issue.

Not really. That's ignoring the

 Quote
(2) run Perl with the -S switch

part. It's not a Windows configuration issue when the same switch works on Perl on other operating systems besides Windows. It's in the Perl docs as a general switch that makes Perl check the PATH environmental variable, regardless of OS.

(This post was edited by WingedKnight on Sep 18, 2016, 8:25 AM)

FishMonger
Veteran / Moderator

Sep 18, 2016, 8:26 AM

Post #14 of 20 (12294 views)
 Re: [Laurent_R] How to Get Perl to Automatically Look in Specific Directories for Scripts? [In reply to] Can't Post
 In Reply To
 In Reply To

(1) add the directory to the Windows PATH environment file

As I said, a Windows configuration issue.

Bill and I also pointed that out

FishMonger
Veteran / Moderator

Sep 18, 2016, 8:48 AM

Post #15 of 20 (12291 views)
 Re: [WingedKnight] How to Get Perl to Automatically Look in Specific Directories for Scripts? [In reply to] Can't Post
The -S switch tells perl to use the path env to locate the perl interpreter. Typically this is used to emulate "#!" on systems that don't support the #! shebang line. It is not used to locate the perl script.

Here's an example where I put a "hello world" script in my php directory which is in my path environment.
 Code
`C:\>dir hello.pl  Volume in drive C is Windows  Volume Serial Number is 1699-B030   Directory of C:\  File Not Found  C:\>dir c:\php\hello.pl  Volume in drive C is Windows  Volume Serial Number is 1699-B030   Directory of c:\php  09/18/2016  08:34 AM                58 hello.pl                1 File(s)             58 bytes                0 Dir(s)  884,337,692,672 bytes free  C:\>type c:\php\hello.pl #!/usr/bin/perl  use 5.010;  say "Hello World!";  C:\>hello.pl Hello World!`

WingedKnight
Novice

Sep 18, 2016, 9:02 AM

Post #16 of 20 (12288 views)
 Re: [WingedKnight] How to Get Perl to Automatically Look in Specific Directories for Scripts? [In reply to] Can't Post
 In Reply To
It is not used to locate the perl script.

It can be. This is what happens when I test it:

 Code
`C:\>perl C:\Example\Foo\Bar\SomeScript.pl  [script runs]  C:\>perl SomeScript.pl Can't open perl script "SomeScript.pl": No such file or directory  C:\>perl -S SomeScript.pl  [script runs]`

FishMonger
Veteran / Moderator

Sep 18, 2016, 9:18 AM

Post #17 of 20 (12285 views)
 Re: [FishMonger] How to Get Perl to Automatically Look in Specific Directories for Scripts? [In reply to] Can't Post
Ok, sorry I do need to walk that back. The -S option will let perl search the path for the script. HOWEVER, if your system is configured (or should I say misconfigured) to require you to execute your script by calling the interpreter directly and pass the script name to it as an arg, then your system is not configured correctly, IMO it's broken, and should be fixed.

You should always be able to call/execute the script directly without first calling perl. Both Windows and *nix systems, by default, search the path environment if the path to the script is not specified. The main difference between Windows and *nix systems in this regard is that on Windows the CWD is by default in the path but on *nix systems it isn't.

One problem with relying on the path to locate the script is that it's very possible to have multiple scripts with the exact same name spread across the system and the one found via the path env may not be the one you want.

FishMonger
Veteran / Moderator

Sep 18, 2016, 9:20 AM

Post #18 of 20 (12284 views)
 Re: [WingedKnight] How to Get Perl to Automatically Look in Specific Directories for Scripts? [In reply to] Can't Post
 In Reply To
 In Reply To
It is not used to locate the perl script.

It can be. This is what happens when I test it:

 Code
`C:\>perl C:\Example\Foo\Bar\SomeScript.pl  [script runs]  C:\>perl SomeScript.pl Can't open perl script "SomeScript.pl": No such file or directory  C:\>perl -S SomeScript.pl  [script runs]`

Can you execute that same script via:
 Code
`C:\>SomeScript.pl`
If you can't, your Windows configuration is broken.

WingedKnight
Novice

Sep 18, 2016, 10:20 AM

Post #19 of 20 (12281 views)
 Re: [FishMonger] How to Get Perl to Automatically Look in Specific Directories for Scripts? [In reply to] Can't Post
 Quote
HOWEVER, if your system is configured (or should I say misconfigured) to require you to execute your script by calling the interpreter directly and pass the script name to it as an arg, then your system is not configured correctly, IMO it's broken, and should be fixed.

No, that was a deliberate choice. I'm well aware of file associations and how to configure them, whether by editing the Registry directly or using the Windows ftype and assoc utilities.

In fact, I deliberately changed the file association so that *.pl files are associated with a script editor rather than the Perl executable, because, in my work, most of the time when I open a Perl script file directly, I want to edit it, not run it.

It's because the file association was deliberately not set to Perl that I was asking this question in the first place, about how to make Perl search specific directories when the interpreter is explicitly called from the command line.

FishMonger
Veteran / Moderator

Sep 18, 2016, 2:33 PM

Post #20 of 20 (12265 views)
 Re: [WingedKnight] How to Get Perl to Automatically Look in Specific Directories for Scripts? [In reply to] Can't Post
That's an unusual choice, but if it works for you, that's ok. Do you do the same type of file type association with the scripts from other languages?

If you had included that explanation in your original post then the direction of this thread would have taken a different route and you would have been given the -S solution in the 1st or 2nd response.

 Announcements     PerlGuru Announcements Perl Programming Help     Frequently Asked Questions     Beginner     Intermediate     Advanced     Regular Expressions     mod_perl     DBI     Win32 Programming Help Fun With Perl     Perl Quizzes - Learn Perl the Fun Way     Perl Golf     Perl Poetry Need a Custom or Prewritten Perl Program?     I need a program that...     I Need a Programmer for Freelance Work     Throw Down The Gauntlet General Discussions     General Questions     Feedback     Tutorial/Article Suggestions for The Learning Cent     Internet Security Other Programming Languages     Javascript     PHP

 Search this forum this category all forums for All words Any words Whole Phrase (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives