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

I have some experience writting perl scripts on Unix and have a written a couple of scripts on windows platform. However, this is the first time I need to write a script that can run on windows and Unix ( ksh, csh, bash environments). I am using File::Spec module which provides cross platform functions. However, I am stuck on one particular issue. In my script, I need to adjust the $PATH and $CLASSPATH envrionment variables before launching external programs. But depending on the platform the separator for the entries in $PATH could be different. Is there a way for me to append to $PATH in a platform independent manner. I could not even find a way to get the separator character for the current shell. Any help would be appreciated. Cheers! Data
  • Comment on cross platform perl - how to modify $path

Replies are listed 'Best First'.
Re: cross platform perl - how to modify $path
by Rudif (Hermit) on Aug 21, 2001 at 02:37 UTC
    Would this be of any help?
    use Config; my $pathsep = $Config{path_sep}; print "pathsep $pathsep\n";

    Rudif

Re: cross platform perl - how to modify $path
by data64 (Chaplain) on Aug 21, 2001 at 00:59 UTC
    Since posting this question, I came up with the following hack as a solution.
    Any comments?
    use Env; sub GetPathVarSeparator{ my( $len, $separator ); #get the length of the first item in the path $len = length( (File::Spec->path)[0] ); $separator = substr( $PATH, $len, 1 ); return $separator; }
    The idea is to use the difference between what File::Spec->path returns and the actual value of the $PATH variable. This correctly gives me ";" on win2k and ":" on my linux bash shell.
Re (tilly) 1: cross platform perl - how to modify $path
by tilly (Archbishop) on Aug 21, 2001 at 06:59 UTC
    If aesthetics don't matter, just use "/" everywhere.

    Perl will cheerfully use that as a path separator on any OS.

      Yea, but "external programs" != Perl, and those might indeed care; and the question has to do with how entries in PATH are separated, not how path nodes within each entry are separated.
Re: cross platform perl - how to modify $path
by THRAK (Monk) on Aug 21, 2001 at 00:37 UTC
    Determine which OS you are on using $^O within an if/else and set the separator character.
    $separator = '/'; $separator = '\\' if ($^O =~ /Win/);


    -THRAK
    www.polarlava.com
      I actually thought about this, but I was not sure if the separator char is different for the different unix shells, ie: between csh and sh. I don't even want to think about zsh and other less common ones.

      data
        It's not; the seperator between directories is / on all unix systems. The shell is irrelevant.