A bit of defensive programming here will certainly help. You can use the core module File::Spec::Functions function canonpath to normalize your path to the canonical form for the operating system. The following code works under both Windows and Unix happily.
use strict; use File::Spec::Functions; use Data::Dumper; # for debugging purpose only my $path = canonpath("../somedir/path/"); print "$path\n"; my @dirs = qw( ../dir/path1 ../dir/path2 ); my @canon_dirs = map { canonpath($_) } @dirs; print Dumper(\@canon_dirs);
And the results when run under Solaris 2.8 -
../somedir/path $VAR1 = [ '../dir/path1', '../dir/path2' ];
and the same script run under Windows XP -
..\somedir\path $VAR1 = [ '..\\dir\\path1', '..\\dir\\path2' ];
And another word of advice - try to keep the directories and files in the Perl script relative to a particular root directory. And append the root directory in front of the relative path when openning files. This will keep the amount of work minimal when porting between different platforms.

use strict; use File::Spec::Functions; my %root = ( MSWin32 => "C:\\Data", solaris => "/var/data", ); my $ROOTDIR = $root{$^O} or die "Unknown platform"; my $file1 = canonpath("$ROOTDIR/data.txt"); print "$file1\n"; my $file2 = canonpath("$ROOTDIR/../input/data2.txt"); print "$file2\n";
And the result on Windows XP -
C:\Data\data.txt C:\Data\..\input\data2.txt
the same script run on Solaris without modification -
/var/data/data.txt /var/data/../input/data2.txt

In reply to Re: win32/unix compatible code question by Roger
in thread win32/unix compatible code question by Grygonos

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.