Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: CPAN Module for mixing Unix/Windows path

by rovf (Priest)
on Jun 05, 2008 at 12:22 UTC ( [id://690408]=note: print w/replies, xml ) Need Help??


in reply to Re: CPAN Module for mixing Unix/Windows path
in thread CPAN Module for mixing Unix/Windows path

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>

Replies are listed 'Best First'.
Re^3: CPAN Module for mixing Unix/Windows path
by lodin (Hermit) on Jun 05, 2008 at 18:01 UTC

    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>
Re^3: CPAN Module for mixing Unix/Windows path
by mandarin (Hermit) on Jun 05, 2008 at 12:48 UTC
    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://690408]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-20 04:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found