Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm using ActiveState on several platforms and am having problems on Windows with the backtick operator. I'd like to use the same command on all platforms:
$verInfo = `bin/version`;
Does anyone know how to get the backtick operator to interpret path separators the way system() does? (system() changes path separators to the appropriate operating-system-specific separator.) Is there a setting for ActiveState that allows this?

Any help would be appreciated!
Thanks!

Edit by tye, remove BR tags

Replies are listed 'Best First'.
Re: backticks on windows (ActiveState)
by Roger (Parson) on Nov 26, 2003 at 01:55 UTC
    It seems to me like a bug in the ActiveState Perl path separator translation. I don't know if it has been fixed on later versions of ActiveState Perl releases, but my version 5.8.0 does not seem to translate the / path separator in `` or qx to \. And I was getting incorrect command format errors.

    There are many work arounds. The method I prefer is to use the Win32::AbsPath module that comes with your ActiveState perl installation to normalize the pathnames into native Windows format.
    use strict; if ($^O eq "MSWin32") { require Win32::AbsPath; # load Win32::AbsPath if under Windows } sub NormalizePath # normalize/fix the path { # if on a Windows platform my $path = shift; $path = Win32::AbsPath::Fix $path if $^O eq "MSWin32"; return $path; } my $command = NormalizePath('C:/Windows/System32/cmd.exe'); my $dir = `$command /C dir`; print "$dir";
    Update: jacques below has mentioned about File::Spec::Functions, which is a cleaner approach than using Win32::AbsPath. Here's an example using the File::Spec::Functions module -

    use strict; use File::Spec::Functions; my $command = canonpath('C:/Windows/System32/cmd.exe'); my $dir = `$command /C dir`; print $dir;
      You shouldn't use Win32::AbsPath. Use File::Spec::Functions, which is a core module. It does the same thing. Unfortunately, Jenda makes no mention of this in the pod.
      Thanks so much. If I can't find a setting for ActiveState that would allow path separators to be interpreted in the backtick command, I'll get one of these workarounds going.

      The problem is that I have several hundred scripts that need to be fixed in this manner, (and while I'm sure I could do the whole lot with a little perl script), I know these all ran on ActiveState before, and so I'd rather go that route, if possible. So, I'd still like to hear if anyone knows how to get ActiveState to behave this way...

Re: backticks on windows (ActiveState)
by jacques (Priest) on Nov 26, 2003 at 02:23 UTC
    Well, if you are trying to execute something, I would find it's path first. You can use File::Which and then use the results within the backticks. This should guarantee that the path separators are correct and what you are trying to execute is, at least, in the path. You could also try File::Spec.
Re: backticks on windows (ActiveState)
by Anonymous Monk on Nov 26, 2003 at 04:07 UTC
    Does anyone know how to get the backtick operator to interpret path separators the way system() does? (system() changes path separators to the appropriate operating-system-specific separator.)
    You're misdiagnosing the problem, observe
    C:\Perl>perl print `bin/perl -V:perl`; system 'bin/perl -V:perl'; __END__ perl='perl'; perl='perl'; C:\Perl>