Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

win32/unix compatible code question

by Grygonos (Chaplain)
on Dec 09, 2003 at 18:22 UTC ( #313469=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Monks,

I am currently working on a Tk FTP client just for fun. In UNIX directories are noted like so , / and in win32 like so, \ or more appropriately to perl \\. I was wondering is there a method to determine which to use. is  $env{comspec} enough? Is parsing the contents of $ftp->pwd enough to determine which OS the ftp server is running?

Let me know if I'm on the totally wrong track here. Thanks,

Grygonos

Replies are listed 'Best First'.
Re: win32/unix compatible code question
by jsprat (Curate) on Dec 09, 2003 at 19:06 UTC
    Internally, perl and Windows understand '/' as the separator just fine. If you are using external programs or libraries, all bets are off - some will handle it, some won't.

    If you need to change the path separator based on underlying OS, File::Spec's catfile and catdir should help. It's a standard module, and its purpose is to "portably perform operations on file names".

    An example:

    #!perl use File::Spec::Functions; print catfile( qw/dir subdir file/ ); __END__ On my FreeBSD box, the result is "dir/subdir/file" On my Win2K box , the result is "dir\subdir\file"

Re: win32/unix compatible code question
by Roger (Parson) on Dec 09, 2003 at 23:17 UTC
    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
      Thanks that's exactly what I was looking for.. I would rather not entrust it to the perl interpreter.

      Grygonos
Re: win32/unix compatible code question
by holo (Monk) on Dec 09, 2003 at 18:35 UTC

    You can use / safely for both Unix and Win32. Perl will know which one to use and abstract that away from the code. To detect if you're on a windows machine, check the contents of $^O (see perlvar).

Re: win32/unix compatible code question
by BUU (Prior) on Dec 09, 2003 at 18:31 UTC
    As far as I remember, perl will dwym if you use forward slashes as file delimiters.
      Not entirely true though. Whether a bug or not, the ActiveState Perl 5.8 under Windows does not interprete the path delimiter in qx/ / or ` ` correctly if you use the unix forward slashes (/) and you will get run-time errors. There was a discussion on this particular issue last month.

        if you use qw{ } would that not correct it for qw?

        Grygonos

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2020-08-07 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (44 votes). Check out past polls.

    Notices?