### Comment on

 Need Help??

Different Operating Systems use different characters as their path separator when specifying directory and file paths:

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!)


In Perl you can generally just use a / as your path separator (except on Mac OS 9, thanks Hanamaki). Why? Because Perl will automagically convert the / to the correct path separator for the system it is running on! This means that coding Windows paths like this

$path = "\\foo\\bar\\baz"; is not required. You can just use this: $path = "/foo/bar/baz";

and things will be fine. In fact using \\ can be problematic, but you probably already know that :-)

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:

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"; [download] If you need to do lots of conversions just write a sub like this: 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; } [download] 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 \\ .... In reply to Paths in Perl by tachyon 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, 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, 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: & & < < > > [ [ ] ] • Link using PerlMonks shortcuts! What shortcuts can I use for linking? • See Writeup Formatting Tips and other pages linked from there for more info. • Log In?  Username: Password: What's my password? Create A New User Chatterbox?  [shmem]: i.e accessed as$array[$i] where$i would be the loop index in a C-style "for" loop: for($i=0;$i<=$# array;$i++){ ... } [james28909]: i gotcha [Discipulus]: reference is a road sign; alias like Constantinple and Istnbul Discipulus well istanbul was a road sign but this is another story: eis ten polin [shmem]: Discipulus++; # and I give you an "a" [james28909]: and you are right, i already tried to go C but ended up coming back, mainly because data types confuse me, well that and things like long long double and float point things all scare me [james28909]: lol [Discipulus]: thanks for the "a" shemem was what I needed to get IstAnbul.. InstantBull

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2018-05-20 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (150 votes). Check out past polls.

Notices?