Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Platform independant directory separator

by coreolyn (Parson)
on Mar 01, 2004 at 16:00 UTC ( [id://332944]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://332944]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-03-28 21:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found