How to use File::Spec->no_upwards?

Discussion in 'Perl Misc' started by Peng Yu, Nov 29, 2008.

  1. Peng Yu

    Peng Yu Guest

    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";
    Peng Yu, Nov 29, 2008
    #1
    1. Advertising

  2. Peng Yu

    Peng Yu Guest

    On Nov 29, 10:39 am, Peng Yu <> wrote:
    > 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";
    Peng Yu, Nov 29, 2008
    #2
    1. Advertising

  3. On 2008-11-29 16:39, Peng Yu <> wrote:
    > 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
    Peter J. Holzer, Nov 29, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marian Jancar

    spec file parsing

    Marian Jancar, Nov 18, 2003, in forum: Python
    Replies:
    4
    Views:
    526
    John Benson
    Nov 18, 2003
  2. Scott Johnson

    Which spec to use?

    Scott Johnson, Apr 3, 2009, in forum: HTML
    Replies:
    23
    Views:
    916
    Kenneth Tilton
    Apr 5, 2009
  3. Mark J. Reed

    File::Spec equivalent?

    Mark J. Reed, Aug 10, 2005, in forum: Ruby
    Replies:
    1
    Views:
    137
    James Edward Gray II
    Aug 10, 2005
  4. Andrew Chen
    Replies:
    1
    Views:
    182
    David Chelimsky
    Mar 25, 2008
  5. Peter Havens
    Replies:
    0
    Views:
    102
    Peter Havens
    Mar 17, 2011
Loading...

Share This Page