Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: moving from mac to PC (updated!)

by haukex (Archbishop)
on Oct 28, 2016 at 07:41 UTC ( [id://1174875]=note: print w/replies, xml ) Need Help??


in reply to moving from mac to PC

Hi flieckster,

For portable filename handling, I recommend the core module File::Spec, or from CPAN Path::Class:

use Data::Dumper; $Data::Dumper::Useqq=1; $Data::Dumper::Terse=1; use File::Spec::Functions qw/catdir catpath/; print Dumper( catdir('','photorepos','Perl','Mother','WorkLoad') ); # the following volume spec is ignored in *NIX print Dumper( catpath('C:', catdir('','photorepos','Perl','Mother','WorkLoad')) ); use Path::Class qw/file dir/; print Dumper( dir('C:\\')->subdir('photorepos')->subdir('Perl') ->subdir('Mother')->subdir('WorkLoad').'' ); my $dir = dir('C:\\','photorepos','Perl','Mother','WorkLoad'); print "Path: ", Dumper( "$dir" ); print $dir->is_absolute ? "Is Absolute\n" : "Is Relative\n"; print "Volume: ", $dir->volume, "\n"; print "Dir List: ",Dumper( [$dir->dir_list] );

I admit I haven't worked with volume specs in either module, and I don't have a Windows machine handy at this moment, so the above may need some tweaking in that respect.

Update 2016-10-29: I tested this on a Windows machine and apparently, in File::Spec, the volume argument to catpath needs to be 'C:', not just 'C', while in Path::Class, the volume needs to be specified as 'C:\\' for things to work properly (unfortunately it seems neither of these points are mentioned in the docs). I updated the above code accordingly and added some more output. The Path::Class code does not work the same under Linux as under Windows: since in Linux "C:\" is a valid name for a directory, that path component is treated like just another regular directory. Here are the outputs:

# Windows "\\photorepos\\Perl\\Mother\\WorkLoad" "C:\\photorepos\\Perl\\Mother\\WorkLoad" "C:\\photorepos\\Perl\\Mother\\WorkLoad" Path: "C:\\photorepos\\Perl\\Mother\\WorkLoad" Is Absolute Volume: C: Dir List: ["", "photorepos", "Perl", "Mother", "WorkLoad"] # Linux "/photorepos/Perl/Mother/WorkLoad" "/photorepos/Perl/Mother/WorkLoad" "C:\\/photorepos/Perl/Mother/WorkLoad" Path: "C:\\/photorepos/Perl/Mother/WorkLoad" Is Relative Volume: Dir List: ["C:\\", "photorepos", "Perl", "Mother", "WorkLoad"]

Hope this helps,
-- Hauke D

Replies are listed 'Best First'.
Re^2: moving from mac to PC
by Your Mother (Archbishop) on Oct 28, 2016 at 12:45 UTC

    Give Path::Tiny a look. I was a fan of Path::Class from its first releases but I think PT is quite a bit better.

      Hi Your Mother,

      I've read many good things about Path::Tiny but haven't had the chance to really use it. One thing that held me back a little from switching from ::Class to ::Tiny is that Path::Class uses File::Spec internally, while Path::Tiny apparently does not (or only partially) - its doc says:

      Path::Tiny does not try to work for anything except Unix-like and Win32 platforms. Even then, it might break if you try something particularly obscure or tortuous. ... All paths are forced to have Unix-style forward slashes.

      So at the moment it feels a little safer and more portable to stick with Path::Class - not a particularly good reason, I know :-) So I'll definitely try out Path::Tiny when I get the chance.

      Thanks,
      -- Hauke D

        All paths are forced to have Unix-style forward slashes.

        So at the moment it feels a little safer and more portable to stick with Path::Class

        Well, except for pre-OSX Macs, forward slashes should not be a problem. But File::Spec has a design problem.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1174875]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-03-28 21:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found