Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: CPAN Module for mixing Unix/Windows path

by lodin (Hermit)
on Jun 05, 2008 at 11:57 UTC ( [id://690403]=note: print w/replies, xml ) Need Help??


in reply to CPAN Module for mixing Unix/Windows path

Path::Class, which you've already looked at, actually seems like a good one. It conveniently has the new_foreign and as_foreign methods. So you can do

use Path::Class; my $path = dir($from_outside)->as_foreign('Unix');
and $path will be Unix style. So if you normalize your paths, i.e. consistantly make your paths into the same type, when you first see them you don't have to worry about it later on. Everything will Just Work. If you know that a path from outside is of a non-native type you can use foreign_dir($type, $path) instead.

Nowadays I pretty much only use Path::Class instead of File::Spec because path manipulation quickly gets dirty and Path::Class has handy methods, while retaining a good control of what's what regarding file system type and file/dir.

lodin

Replies are listed 'Best First'.
Re^2: CPAN Module for mixing Unix/Windows path
by rovf (Priest) on Jun 05, 2008 at 12:22 UTC

    Though Path::Class seems to be really good (and maybe I should have used it in place of File::Spec from the beginning), I don't see how it can handle my case, as it seems to require the *caller* to already know which style the pathes are.

    For example, when running the following piece of code:

    use Path::Class; foreach $s ('x/y','x\y') { $dir=Path::Class:Dir->new($s)->as_foreign('Unix'); print "$dir\n"; }
    I would have wished that this would output
    x/y x/y
    no matter whether the application is running on Unix or on Windows. But on Unix (for example), I get
    x/y x\y
    I understand the behaviour of Path::Class: It is not designed to look into a path string and guess from there what OS type it is supposed to be - but this is the very thing I'm looking for.

    -- 
    Ronald Fischer <ynnor@mm.st>

      I don't quite understand the problem.

      From the OP: Internally, I use / as a separator whenever possible. For path strings I get from the "outside", I use File::Spec to be OS independent.

      Do any paths come from somewhere else, that you can't keep track of and normalize? My point is that Path::Class, just as File::Spec which it uses internally, expects Unix style paths when run on Unix, and Windows style paths when run on Windows, unless you tell it otherwise. So for new never-before-seen paths you do

      my $path = dir($from_outside)->as_foreign('Unix');
      and once they're internal, you just do
      my $path = foreign_dir('Unix', $internal);
      Is this unapplicable to your problem?

      lodin

        It is partiallly applicable. For nearly all pathes coming from the outside, I don't know whether they are Unix or Windows style. They might even be mixed, such as a/b\c\d/e. This is possible since we have the requirement (actually a very common requirement in mixed language projects), that certain characters are forbidden in directory/filenames. For example, no '\', no '@' etc.

        As said before, this is not difficult to implement, but I was thinking that maybe someone has already published utilities I could re-use. In practice, I have to normalize the path name before the first use, by (at least) converting the backslashes to forward slashes, but being careful with things like double backslash etc.

        -- 
        Ronald Fischer <ynnor@mm.st>
      I understand the behaviour of Path::Class: It is not designed to look into a path string and guess from there what OS type it is supposed to be - but this is the very thing I'm looking for.
      I suspect this a difficult task, as '\' is a legal character in Unix filenames.
        Actually there are many things which will get you in trouble with cross-platform filenames. Another example beside of the backslash would be the colon (illegal in Windows and on the Mac). But when you do cross-platform stuff, you forbid anyway a certain set of characters which might get you in trouble (/\@:[]*, just to name a few), so this would be a "garbage in - garbage out" problem anyway, so this is not something I would worry.
        -- 
        Ronald Fischer <ynnor@mm.st>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-25 09:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found