Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Getting path info in a mixed UNIX / NT environment

by el-moe (Scribe)
on Dec 08, 2000 at 03:45 UTC ( [id://45645]=perlquestion: print w/replies, xml ) Need Help??

el-moe has asked for the wisdom of the Perl Monks concerning the following question:

I just got the assignment to port all our existing scripts to run in a mixed environment. The OS's are HP-UX 10.2 and Windows 2000.

I was about to start doing this:

my $DRIVE_LETTER = $ENV{SOME_DRIVE}; my $path = $DRIVE_LETTER/tmp/$some_file;
I think this will work in both cases where a drive is present or absent.

Does anyone have a better way?
And while I"ve got your attention, what kind of other issues should I be aware of?

Even if someone can point me to a good document that would be a great help.

Prost,
Moe

Replies are listed 'Best First'.
Re: Getting path info in a mixed UNIX / NT environment
by chipmunk (Parson) on Dec 08, 2000 at 03:58 UTC
    For handling paths in a platform independent way, I recommend using File::Spec. Unfortunately, the documentation for File::Spec is split between the main module and File::Spec::Unix. I don't think File::Spec::Unix is indexed on this site, so you'll have to read it locally.

    Update: Oops! I originally suggested File::Path instead of File::Spec.

      I think this will be a very good thing for all future development.

      I am looking for examples so I can see how it works.

      Thanks

      Prost,
      Moe

Re: Getting path info in a mixed UNIX / NT environment
by myocom (Deacon) on Dec 08, 2000 at 03:52 UTC
    Your code as written won't work, for at least two reasons. One, unless you've defined one, there is no SOME_DRIVE environment variable. Two, you'll want to put double-quotes around that last bit:
    my $path = "$DRIVE_LETTER/tmp/$some_file";

    Depending on what your scripts do, though, you may not need to worry about what your current path is. And if you use relative paths, you shouldn't need to worry about drive letters whatsoever on Win2K.
      myocom is correct about the relative paths, but I'd go just a bit further and remind you that perl understands the root of the drive that it is being executed from. In other words, if you're installed in D:\Perl\bin\perl.exe, then / = D:\

      This is true for all Windows as far as I know...

      #!/home/bbq/bin/perl
      # Trust no1!
Re: Getting path info in a mixed UNIX / NT environment
by el-moe (Scribe) on Dec 08, 2000 at 04:27 UTC
    Ok. To clarify a little...

    The Environment vars will be set up correctly. And I forgot the "" around the path in the example... but that will be correct also. I looked at File::Path and it appears to be used for making and deleting directory trees. Not really what I'm looking for... I need a porting kind of solution for existing code.

    There are 4 distinct drives(c:,j:,k:,l:) I have to access from the Win2000 systems. On unix they are simply paths. The unix systems are running in production as we speak.

    My concern is that there are well over 50,000 lines of code now and I need a good and simple as possible solution to the drive mapping issue.

    My idea is just that... an idea. I know there must be a better way because I'm relatively new to programming and how is it possibly for me to come up with the best idea as my first attempt?

    So... looking back at my syntactically incorrect "idea"... can anyone tell me if the concept is correct or if there is a better way?

    Prost,
    Moe

      Your idea will probably work fine. Assuming that the environment variable 'SOME_DRIVE' contains something like 'J:', you might also consider something like this:

      $path = '/tmp/some/path'; $path = $ENV{SOME_DRIVE} . $path if $^O eq 'MSWin32'; # rest of code
        I thought of that also. Is that more stable? If so I will use it.

        Prost,
        Moe

      Doh, sorry. I meant File::Spec, not File::Path. File::Spec is good for handling paths in a platform independent manner.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-18 20:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found