<?xml version="1.0" encoding="windows-1252"?>
<node id="110030" title="Paths in Perl" created="2001-09-04 13:43:47" updated="2005-08-12 08:23:27">
<type id="956">
perltutorial</type>
<author id="80749">
tachyon</author>
<data>
<field name="doctext">
&lt;p&gt;Different Operating Systems use different characters as their path separator when specifying directory and file paths:
&lt;pre&gt;
foo/bar/baz    # *nix uses a /
foo\bar\baz    # Win32 uses a \
foo:bar:baz    # Mac OS 9 uses a :
foo/bar/baz    # Mac OS X uses a / (usually!)
&lt;/pre&gt;
&lt;p&gt;In Perl you can generally just use a / as your path separator (except on Mac OS 9, thanks [Hanamaki]). Why? Because Perl will &lt;b&gt;automagically&lt;/b&gt; convert the / to the correct path separator for the system it is running on! This means that coding Windows paths like this
&lt;pre&gt;$path = "\\foo\\bar\\baz";&lt;/pre&gt;
&lt;p&gt;is not required. You can just use this:
&lt;pre&gt;$path = "/foo/bar/baz";&lt;/pre&gt;
&lt;p&gt;and things will be fine. In fact using \\ can be problematic, but you probably already know that :-)
&lt;p&gt;If you want to display the expected system delimiter to a user (ie hide the fact that you are using / internally) you can just do something like this:
&lt;code&gt;my $perl_path = '/foo/bar/baz';
(my $win_path = $perl_path) =~ tr!/!\\!;
print "Perl still sees:  $perl_path\n";
print "But we can print: $win_path\n";
&lt;/code&gt;
&lt;p&gt;If you need to do lots of conversions just write a sub like this:
&lt;code&gt;my $perl_path = '/foo/bar/baz';
print "This is the Windows path: ", win_path($perl_path), "\n";

sub win_path {
    (my $path = shift) =~ tr!/!\\!;
  return $path;
}
&lt;/code&gt;
So there you have it. Paths in Perl. By using a / you make it much easier to port your code to another system. For truly portable methods look into the [File::Spec] module (part of the standard distro) and [perlman:perlport]. Thanks to [wog] and [Hanamaki] for this suggestion. And if you have been converting / to \\ ....</field>
</data>
</node>
