Re: File::Spec-collapse() proof of concept

2005-07-11 Thread Michael G Schwern
On Sat, Jul 09, 2005 at 08:24:31AM +0200, demerphq wrote:
 This should support a $base arguement just as rel2abs does. And on
 WIn32 it should use GetFullPathName().

If you want a relative path with a given base, run it through rel2abs() 
first and THEN hand it to whatever other method you like.

What benefit does giving collapse() (or anything else) a $base parameter 
have over:

my $collapsed_path = File::Spec-collapse(
File::Spec-rel2abs($path, $base)
);


-- 
Michael G Schwern [EMAIL PROTECTED] http://www.pobox.com/~schwern
Don't try the paranormal until you know what's normal.
-- Lords and Ladies by Terry Prachett


Re: File::Spec-collapse() proof of concept

2005-07-11 Thread Michael G Schwern
On Sat, Jul 09, 2005 at 10:17:09AM -0500, Ken Williams wrote:
 Personally I think we should recommend (as it does now in the 
 canonpath() docs) that the user use Cwd::realpath() when they want to 
 collapse foo/.. in paths.

realpath() returns an absolute path.  collapse() leaves relative paths
relative and absolute paths absolute.  They do different things.


  If we provide a logical-only collapse() 
 method, we know it's buggy (in the sense that it sometimes will point 
 the user to a different file) before we even release it.

Its not buggy, its just another transform you can perform on a path.  
collapse() does not guarantee that the result will point to the same file
any more than catdir() does.  It would only be buggy if we automatically did 
it as part of canonpath().  

collapse() is a tool.  Let the user use it as they see fit and shoot off
as many toes as they want.

What would be worse is if they tried to hand code something like this and
got it wrong, as illustrated in Win32-canonpath().


-- 
Michael G Schwern [EMAIL PROTECTED] http://www.pobox.com/~schwern
You are wicked and wrong to have broken inside and peeked at the
implementation and then relied upon it.
-- tchrist in [EMAIL PROTECTED]


Re: File::Spec-collapse() proof of concept

2005-07-09 Thread demerphq
On 7/9/05, Michael G Schwern [EMAIL PROTECTED] wrote:
 I'm not wedded to the name, but attached is a proof-of-concept of a routine
 which collapses updirs.  This is a solution to the problem whether or not
 canonpath() should collapse them.  Let the user decide.
 
 0 ~$ perl -w ~/tmp/foo.plx '../../bar'
 ../../bar
 0 ~$ perl -w ~/tmp/foo.plx 'foo/../../bar'
 ../bar
 0 ~$ perl -w ~/tmp/foo.plx '../foo/../../bar'
 ../../bar
 0 ~$ perl -w ~/tmp/foo.plx '/../../../bar'
 /bar
 
 There are some problems in Win32 and they look like they're due to
 File::Spec bugs.
 
 0 ~$ perl -w ~/tmp/foo.plx 'C:\foo\..\..\..\bar' Win32
 C:bar
 
 This is because:
 
 0 ~$ perl -MFile::Spec::Win32 -wle 'print join \n, 
 File::Spec::Win32-catdir(, .., .., )'
 
 It should be the root dir as it is on Unix.
 
 0 ~$ perl -MFile::Spec::Unix -wle 'print join \n, 
 File::Spec::Unix-catdir(, .., .., )'
 /


This should support a $base arguement just as rel2abs does. And on
WIn32 it should use GetFullPathName().

speak to you all in a long while im off to Toronto for a month...

cheers,
yves

-- 
perl -Mre=debug -e /just|another|perl|hacker/


Re: File::Spec-collapse() proof of concept

2005-07-09 Thread Ken Williams


On Jul 8, 2005, at 5:30 PM, Michael G Schwern wrote:

I'm not wedded to the name, but attached is a proof-of-concept of a 
routine
which collapses updirs.  This is a solution to the problem whether or 
not

canonpath() should collapse them.  Let the user decide.


Personally I think we should recommend (as it does now in the 
canonpath() docs) that the user use Cwd::realpath() when they want to 
collapse foo/.. in paths.  If we provide a logical-only collapse() 
method, we know it's buggy (in the sense that it sometimes will point 
the user to a different file) before we even release it.


I'm all for providing some kind of pass-through to realpath() in 
File::Spec, though.


 -Ken



File::Spec-collapse() proof of concept

2005-07-08 Thread Michael G Schwern
I'm not wedded to the name, but attached is a proof-of-concept of a routine
which collapses updirs.  This is a solution to the problem whether or not
canonpath() should collapse them.  Let the user decide.

0 ~$ perl -w ~/tmp/foo.plx '../../bar'
../../bar
0 ~$ perl -w ~/tmp/foo.plx 'foo/../../bar'
../bar
0 ~$ perl -w ~/tmp/foo.plx '../foo/../../bar'
../../bar
0 ~$ perl -w ~/tmp/foo.plx '/../../../bar'
/bar

There are some problems in Win32 and they look like they're due to 
File::Spec bugs.

0 ~$ perl -w ~/tmp/foo.plx 'C:\foo\..\..\..\bar' Win32
C:bar

This is because:

0 ~$ perl -MFile::Spec::Win32 -wle 'print join \n, 
File::Spec::Win32-catdir(, .., .., )'

It should be the root dir as it is on Unix.

0 ~$ perl -MFile::Spec::Unix -wle 'print join \n, 
File::Spec::Unix-catdir(, .., .., )'
/


-- 
Michael G Schwern [EMAIL PROTECTED] http://www.pobox.com/~schwern
Reality is that which, when you stop believing in it, doesn't go away.
-- Phillip K. Dick
#!/usr/bin/perl -lw

use File::Spec;
use File::Spec::Unix;

package File::Spec::Unix;

sub collapse {
my($fs, $path) = @_;

my $updir  = $fs-updir;
my $curdir = $fs-curdir;

my($vol, $dirs, $file) = $fs-splitpath($path);
my @dirs = $fs-splitdir($dirs);

my @collapsed;
push @collapsed, $curdir unless $fs-file_name_is_absolute($path);

foreach my $dir (@dirs) {
if( $dir eq $updir  and   # if we have an updir
@collapsed  and   # and something to collapse
length $collapsed[-1]   and   # and its not the rootdir
$collapsed[-1] ne $updirand   # nor another updir
$collapsed[-1] ne $curdir # nor the curdir
  ) 
{ # then
pop @collapsed;   # collapse
}
else {# else
push @collapsed, $dir;# just hang onto it
}
}

return $fs-catpath($vol,
$fs-catdir(@collapsed),
$file
   );
}

package main;

my($path, $os) = @ARGV;
$os ||= '';

my $class = 'File::Spec';
$class .= ::$os if $os;
eval qq{require $class};

print $class-collapse($path);


Re: File::Spec-collapse() proof of concept

2005-07-08 Thread Michael G Schwern
On Fri, Jul 08, 2005 at 03:30:26PM -0700, Michael G Schwern wrote:
 There are some problems in Win32 and they look like they're due to 
 File::Spec bugs.
 
 0 ~$ perl -w ~/tmp/foo.plx 'C:\foo\..\..\..\bar' Win32
 C:bar
 
 This is because:
 
 0 ~$ perl -MFile::Spec::Win32 -wle 'print join \n, 
 File::Spec::Win32-catdir(, .., .., )'
 
 It should be the root dir as it is on Unix.
 
 0 ~$ perl -MFile::Spec::Unix -wle 'print join \n, 
 File::Spec::Unix-catdir(, .., .., )'
 /

I just sent off a patch to Ken to fix this.
http://rt.cpan.org/NoAuth/Bug.html?id=13606


-- 
Michael G Schwern [EMAIL PROTECTED] http://www.pobox.com/~schwern
ROCKS FALL! EVERYONE DIES!
http://www.somethingpositive.net/sp05032002.shtml