Windows directory path in perl

How to set correct perl path

I have 2 versions of perl installed. perl v5.18.2 and v5.20.0 . But when I do perl -v I get perl v5.18.2. I don’t need v5.18.2 at all. I need v5.20.0. How do I change the path to include v5.20.0 and not v5.18.2?

Here is my $PATH :

v5.18.2 is installed at /usr/bin/perl and /usr/bin/perl5.18.2 , and v5.20.0 at /root/perl5/perlbrew/perls/perl-5.20.0/bin/perl .

3 Answers 3

See the perlbrew documentation:

switch Permanently use the specified perl as default

If you haven’t already, you will need to add source /root/perl5/perlbrew/etc/bashrc to your login script for this to work.

/perl5/perlbrew/perls/perl-5.10.1 && perlbrew switch 5.10.1 – Bodo Hugo Barwich Oct 22 ’20 at 15:05

The following will add the desired build of Perl to the search path so that it’s found first:

You may add that to your login script to make this change permanent.

Note that you’ll need to update the shebang ( #! ) lines of scripts installed with a different perl to the following:

It looks like you have three copies of perl installed, as neither of the paths you mentioned are in the PATH variable yet your shell still finds one

There’s no need for perlbrew. All you need to do is set your PATH variable on the command line

If you want to make that change permanent then add the command to your profile file at

Not the answer you’re looking for? Browse other questions tagged perl or ask your own question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

How do I make Perl understand to navigate to a directory with whitespaces in the path?

I am trying to navigate to a directory where I have my function stored which I need to use in my script for result generation. But I am stuck because Perl is not able to understand my syntax for function location (with whitespaces).

Here is my code:

where SnmpWalk is my function located at path as in variable $path.

Perl returns following error at runtime:

How can I fix this problem?

5 Answers 5

In Perl, q can be used instead of ‘string’ (mnemonic single quote), and qq instead of «string» (mnemonic double quote). Double quote evaluates variables and backslashes, while single quite does not. Also, qx can be used instead of backticks in `cmd` (mnemonic quote and execute).

Читайте также:  Обзор линукс для майнинга

Using these constructs makes it much easier to escape strings in situations like yours:

Also note that even under Windows, you can use forward slashes ( / ) in a path. It works just fine as long as you use full path for executable.

With judicious use of the correct quotation marks, this can be tidied up a lot. Backslashes within single quotes don’t need escaping unless they are the last character in the string, and double quotes need only be escaped if they are also used as the string delimiter. It is also (almost certainly) wrong to put $snmpwalk in quotes when you print it.

On Linux/OS X, escape the whitespace with a \ . On Windows, use quotes around the path.

It is not Perl that is not understanding your path, it is cmd (the Windows shell). I think that by adding some quotes around your path will solve this issue. Try the following not so elegant hack:

I used this to examine the contents of a .jar file from within Perl running on Cygwin:

Print current directory using Perl

I have this code to print the current directory using Perl:

But it is displaying the filename of my script along with the directory.

I want it only to display C:\Perl\ .

How can I do it?

7 Answers 7

Each of the following snippets get the script’s directory, which is not the same as the current directory. It’s not clear which one you want.

To get the current working directory ( pwd on many systems), you could use cwd() instead of abs_path :

Or abs_path without an argument:

See the Cwd docs for details.

To get the directory your perl file is in from outside of the directory:

See the File::Basename docs for more details.

Just remove the ‘$0’

But I think it doesn’t work on Windows.

Here is one simple solution:

I hope this will help.

You could use FindBin :

FindBin is a standard module that is installed when you install Perl. To get a list of the standard pragmatics and modules, see perldoc perlmodlib .

I used my script in dirs with symlinks. The script parses the path and executes commands depending on the path. I was faced with the correct determination of the current path.

Here is example:

Cwd module disclosures path (analogue of readlink -f) http://perldoc.perl.org/Cwd.html

If you need to get current path like pwd, you can use $ENV

Not the answer you’re looking for? Browse other questions tagged perl directory or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Do I need the Perl bin directory in the PATH to run perl programs (on Windows)?

Traditionally, I would just have C:\perl\bin in my PATH variable, but due to version conflicts I would like to keep different perl versions at locations C:\Perl-versionXY\bin and just execute my Perl scripts via directly invoking C:\Perl-. \bin\perl.exe theScript.pl .

Читайте также:  Classic mac os media players

This is actually to be run under an automated system, where we already directly invoke C:\perl\bin\perl.exe for all perl scripts. (But C:\perl\bin is also in the PATH.)

To facilitate different Perl versions side-by-side, I’d like to remove C-perl-bin from the PATH to make sure we don’t ever see side effects from any Perl related PATH settings.

Is this supposed to work? What about modules that require additional DLL files (like LibXML, that requires the LibXML.dll in the bin directory of perl)??

I’ll be using Strawberry Perl portable for the side by side versions. (Who’s readme file mentions some PATH settings, but doesn’t mention which is used for what.)

2 Answers 2

Provided that all the DLLs are in the same directory as the executable, it should work normally. If there is only one entry for Perl in the path, then the DLLs must be in the same directory as the executable (or are being found using some explicit logic) so you should be OK. When an executable loads a DLL, the first place searched is the directory containing the executable.

If you do run into trouble, one option would be to create a command file for each version. You could give these different names, like perl58.cmd, perl514.cmd, etc., put them all in a single directory, and put that directory on the path. In each command file, add the corresponding Perl directory to the path and then launch Perl with the command-line arguments:

Note use of the setlocal command so that the change to the path is not exported back to the command line window you’re running the command file from.

I would caution that if you do not have the Perl bin directory on the PATH, and anything that you executes attempts to invoke program that lives in the bin dir without providing an explicit path (bad practice, but that doesn’t stop it from happening) then you get failure, and depending on how that failure is handled, could manifest in problems that are subtle and hard to debug.

How do I get the full path to a Perl script that is executing?

I have Perl script and need to determine the full path and filename of the script during execution. I discovered that depending on how you call the script $0 varies and sometimes contains the fullpath+filename and sometimes just filename . Because the working directory can vary as well I can’t think of a way to reliably get the fullpath+filename of the script.

Anyone got a solution?

s/\s+$//; return $string; > just thought I’d share – user1210923 Dec 4 ’20 at 17:42

23 Answers 23

There are a few ways:

  • $0 is the currently executing script as provided by POSIX, relative to the current working directory if the script is at or below the CWD
  • Additionally, cwd() , getcwd() and abs_path() are provided by the Cwd module and tell you where the script is being run from
  • The module FindBin provides the $Bin & $RealBin variables that usually are the path to the executing script; this module also provides $Script & $RealScript that are the name of the script
  • __FILE__ is the actual file that the Perl interpreter deals with during compilation, including its full path.
Читайте также:  Операционные системы windows оригинальный образ

I’ve seen the first three ( $0 , the Cwd module and the FindBin module) fail under mod_perl spectacularly, producing worthless output such as ‘.’ or an empty string. In such environments, I use __FILE__ and get the path from that using the File::Basename module:

$0 is typically the name of your program, so how about this?

Seems to me that this should work as abs_path knows if you are using a relative or absolute path.

Update For anyone reading this years later, you should read Drew’s answer. It’s much better than mine.

I think the module you’re looking for is FindBin:

You could use FindBin, Cwd, File::Basename, or a combination of them. They’re all in the base distribution of Perl IIRC.

I used Cwd in the past:

Getting the absolute path to $0 or __FILE__ is what you want. The only trouble is if someone did a chdir() and the $0 was relative — then you need to get the absolute path in a BEGIN<> to prevent any surprises.

FindBin tries to go one better and grovel around in the $PATH for something matching the basename($0) , but there are times when that does far-too-surprising things (specifically: when the file is «right in front of you» in the cwd.)

File::Fu has File::Fu->program_name and File::Fu->program_dir for this.

Some short background:

Unfortunately the Unix API doesn’t provide a running program with the full path to the executable. In fact, the program executing yours can provide whatever it wants in the field that normally tells your program what it is. There are, as all the answers point out, various heuristics for finding likely candidates. But nothing short of searching the entire filesystem will always work, and even that will fail if the executable is moved or removed.

But you don’t want the Perl executable, which is what’s actually running, but the script it is executing. And Perl needs to know where the script is to find it. It stores this in __FILE__ , while $0 is from the Unix API. This can still be a relative path, so take Mark’s suggestion and canonize it with File::Spec->rel2abs( __FILE__ );

It really depends on how it’s being called and if it’s CGI or being run from a normal shell, etc.

In order to get the path to the directory containing my script I used a combination of answers given already.

perlfaq8 answers a very similar question with using the rel2abs() function on $0 . That function can be found in File::Spec.

There’s no need to use external modules, with just one line you can have the file name and relative path. If you are using modules and need to apply a path relative to the script directory, the relative path is enough.

Оцените статью