in reply to Re: Perl appending \ or / based on windows or linux
in thread Perl appending \ or / based on windows or linux
(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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Perl appending \ or / based on windows or linux
by kennethk (Abbot) on Oct 07, 2014 at 20:51 UTC | |
by Anonymous Monk on Oct 07, 2014 at 21:39 UTC | |
|
Re^3: Perl appending \ or / based on windows or linux
by dasgar (Priest) on Oct 07, 2014 at 20:34 UTC | |
by kennethk (Abbot) on Oct 07, 2014 at 20:39 UTC | |
by Anonymous Monk on Oct 07, 2014 at 21:39 UTC |