(Note: This is a reply equally to kennethk, jonadab, and anon)

sravs448 wants to append a trailing slash (whether or not that's an XY Problem is another topic). Unfortunately, File::Spec, despite being the core module to handle file/pathnames properly and portably, does not provide a method to do this. Most likely that's because "append a trailing slash" is not really a portable concept across all these supported OSes.

As long as the script is limited to a known set of OSes, McA's suggestion above is probably better in this case.

If there was a solution with a core module (perhaps I've missed something*), that'd make me happy, since I've faced this problem myself before. (Back when I had this problem I didn't find a decent CPAN module either.)

* I tested several variants of catdir / catfile / catpath on the various File::Spec::* implementations (except VMS):

use warnings; use strict; use feature 'say'; for my $m (map {"File::Spec::$_"} qw/Unix Win32 Mac OS2 VMS Cygwin Epoc/) { say "##### $m #####"; eval qq{ use $m; 1 } or warn "Skipping $m because: $@" and next; say " catdir: ", $m->catdir("foo","bar"); #say " catdir undef: ", $m->catdir("foo","bar",undef); # warns say " catdir blank: ", $m->catdir("foo","bar",""); say " catdir curdir: ", $m->catdir("foo","bar",$m->curdir); #say " catfile undef: ", $m->catfile("foo","bar",undef); # warns say " catfile blank: ", $m->catfile("foo","bar",""); say "catfile curdir: ", $m->catfile("foo","bar",$m->curdir); say " catpath undef: ", $m->catpath(undef,"foo",undef); say " catpath blank: ", $m->catpath(undef,"foo",""); say "catpath curdir: ", $m->catpath(undef,"foo",$m->curdir); } __END__ ##### File::Spec::Unix ##### catdir: foo/bar catdir blank: foo/bar catdir curdir: foo/bar catfile blank: foo/bar/ catfile curdir: foo/bar/. catpath undef: foo catpath blank: foo catpath curdir: foo/. ##### File::Spec::Win32 ##### catdir: foo\bar catdir blank: foo\bar catdir curdir: foo\bar catfile blank: foo\bar catfile curdir: foo\bar catpath undef: foo catpath blank: foo catpath curdir: foo\. ##### File::Spec::Mac ##### catdir: :foo:bar: catdir blank: :foo:bar: catdir curdir: :foo:bar: catfile blank: :foo:bar: catfile curdir: :foo:bar: catpath undef: :foo: catpath blank: :foo: catpath curdir: :foo: ##### File::Spec::OS2 ##### catdir: foo/bar catdir blank: foo/bar catdir curdir: foo/bar catfile blank: foo/bar/ catfile curdir: foo/bar/. catpath undef: foo catpath blank: foo catpath curdir: foo/. ##### File::Spec::VMS ##### Skipping File::Spec::VMS because: Can't locate VMS/Filespec.pm in @INC ##### File::Spec::Cygwin ##### catdir: foo/bar catdir blank: foo/bar catdir curdir: foo/bar catfile blank: foo/bar/ catfile curdir: foo/bar/. catpath undef: foo catpath blank: foo catpath curdir: foo/. ##### File::Spec::Epoc ##### catdir: foo/bar catdir blank: foo/bar catdir curdir: foo/bar catfile blank: foo/bar/ catfile curdir: foo/bar/. catpath undef: foo catpath blank: foo catpath curdir: foo/.

As you can see Win32 and Mac are the most stubborn.


In reply to Re^2: Perl appending \ or / based on windows or linux by Anonymous Monk
in thread Perl appending \ or / based on windows or linux by sravs448

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.