How to use File::Spec->no_upwards?

P

Peng Yu

Hi,

According to http://perldoc.perl.org/File/Spec.html, no_upwards should
remove '.' or '..'. But the following example does not do so. Can
somebody let me know how to use no_upwards correctly?

Thanks,
Peng

#!/usr/bin/perl

use warnings;
use strict;

use File::Spec;

my @path = ("../../../something/backup/home/../home/Desktop/");
File::Spec->no_upwards(@path);
print "\@path = @path\n";
 
P

Peng Yu

Hi,

According tohttp://perldoc.perl.org/File/Spec.html, no_upwards should
remove '.' or '..'. But the following example does not do so. Can
somebody let me know how to use no_upwards correctly?

Hi,

I just find that I can use Cwd to remove '..'.

Thanks,
Peng

#!/usr/bin/perl

use warnings;
use strict;

use Cwd;

my $path = '../../../something/backup/home/../home/Desktop/';
$path = Cwd::abs_path($path);
print "\$path = $path\n";
 
P

Peter J. Holzer

According to http://perldoc.perl.org/File/Spec.html, no_upwards should
remove '.' or '..'. But the following example does not do so. Can
somebody let me know how to use no_upwards correctly?

Thanks,
Peng

#!/usr/bin/perl

use warnings;
use strict;

use File::Spec;

my @path = ("../../../something/backup/home/../home/Desktop/");
File::Spec->no_upwards(@path);

You are ignoring the result of File::Spec->no_upwards here.
print "\@path = @path\n";

But anyway, looking at the source:

sub no_upwards {
my $self = shift;
return grep(!/^\.{1,2}\z/s, @_);
}

no_upwards is obviously not intended to be applied to whole paths, but
only to file names. The discription says so, but the use of the variable
name @paths in the example is confusing.

This works as expected:

#!/usr/bin/perl
use warnings;
use strict;
use File::Spec;

opendir(my $dh, '.') or die "opendir . failed: $!";
my @files = File::Spec->no_upwards(readdir($dh));
print "$_\n" for @files;
__END__


hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top