in reply to Platform independant directory separator

This is going to be a lot more answer than you are looking for but as you are in a cross-platform situation I thought I'd share what I have found to be a very re-usable format. I'm in a situation where CPAN is difficult to utilize effectively in our organization, so I created a module I called OSify.pm. In it I created common OS commands OSrecursive copy, OSmkdir, etc and centralized their logic, It has made it so I can code for DOS, linux, and Solaris with the same commands easily, and can be quickly extended to other OS's as required. The following is just the OSPath() that takes care of the delimiter issue, but using the general approach it's easy to adapt to any given command.

sub OSpath { $_ = $_[0]; debug( "Recieved path as\n $_\n", 0 ); if ( $^O =~ /mswin/i ) { # Everywhere you find a forward back slash replace it with # two backslashes if ( /(\/|\\\w)/ ) { s/(\/|\\)/\\\\/g; } # Incase we've created four backslashes in a row # reduceit back to one if ( /(\\)(\\)(\\)(\\)/ ) { s/\\\\\\\\/\\\\/g; } # Have to make sure there is only 1 slash after a drive letter if ( /(\:\\\\).*/ ) { s/$1/\:\\/; } } elsif ( $^O =~ /solaris/i ) { s/\\/\//g; if ( $_ =~ /.*\/(.*\s.*)\/.*/ ) { my $spacedDir = $1; unless ( $spacedDir =~ /"/ ) { $_ =~ s/$1/"$spacedDir"/g; } } } else { print "Platform $^O is not currently supported\n"; } debug( "Returning path as\n $_\n", 0 ); return $_; }

Replies are listed 'Best First'.
Re: Re: Platform independant directory separator
by chromatic (Archbishop) on Mar 01, 2004 at 20:12 UTC

    File::Copy, File::Path, and File::Spec have been in the core for many years. Are there features they don't provide?

      I'm not saying there's not a better way to do it. It's just the way that's developed for me. The current list of functions in that module are:

      OSmkdir OSpath OSrmdir OSworkdir (Make a temp directory in $TEMP with unique name) OSlogger searchReplaceCopyFile printEnvVars debug getFileExt getEnvVars OScopy OSdelete singleDosDelim file2Array arrayRelevant getShortPath shortDirName (Dos 8 letter dirname) OSrecursiveCopy OSmkWritable