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

Hi Monks! in my script i chdir allot to move from one directory to another, Now i want to use that script on a windows machine therefore the absolute Unix pathnames should be accepted by windows too. I mean i cannot have just /etc... on windows right ?. The c:\ in Windows and the / in Linux is my issue i used the File::Spec to write the following:
$base = File::Spec->rootdir(); $path = 'home/me/test/'; $rel_path = File::Spec->abs2rel( $path, $base ) ; chdir $rel_path;
but as usually it doesn't work. Maybe i shouldn't be using these functions;

Replies are listed 'Best First'.
Re: Unix-Windows pathnames
by syphilis (Archbishop) on Oct 13, 2007 at 23:18 UTC
    Hi props,

    In perl, the '/' works fine on Windows. The directory '/home/me/test/' would translate to 'X:\home\me\test' on Windows (where X is the current drive).

    In the code you provided, $path is a relative path. Shouldn't it be an absolute path ('/home/me/test') ? I suspect that the code will do as you want if you assign to $path with:
    $rel_path = File::Spec->abs2rel('/home/me/test');
    That's assuming that '/home/me/test' exists and is, in fact, the directory you want to switch to.

    Cheers,
    Rob
      Many thanks syphilis i can chdir fine now.
Re: Unix-Windows pathnames
by graff (Chancellor) on Oct 13, 2007 at 22:02 UTC
    If you do the chdir like this:
    chdir $rel_path or die "chdir failed on $rel_path: $!\n";
    You will probably get a better idea of what's going wrong -- better even than anything we could guess at, based on what we know about your situation.
Re: Unix-Windows pathnames
by NiJo (Friar) on Oct 14, 2007 at 17:10 UTC
    You should have read the full perldoc page:
    $path = File::Spec->catdir(home, me, test);