in reply to Platform Independent Directory Traversal

RTFM
Returns a string representation of the parent directory. 
    $updir = File::Spec->updir();
  • Comment on Re: Platform Independent Directory Traversal

Replies are listed 'Best First'.
Re^2: Platform Independent Directory Traversal
by wind (Priest) on Jul 29, 2007 at 04:44 UTC
    I was aware of this function from File::Spec, but misunderstood what it meant. Since it did not accept any arguments, I assumed (without testing) that it would simply return the "parent directory" of the "current working directory". Instead it returns '..'.

    This means that my final solution could be simplified to:
    use File::Basename qw(dirname); use File::Spec; print File::Spec->rel2abs( File::Spec->catdir( dirname(__FILE__), File::Spec->updir(), ) );
    However, is this right? Is this how the updir() function was intended to be used? I've probably just been ruminating on the problem for too long. But this solution still somewhat bothers me.

    Also, I appreciate the RTFM reference in the spirit in which it was given. However, the acronym itself is rather rude, and often plays on false assumptions.

    Thanks again,
    - Miller

      Your example code looks like what I usually use. I prefer File::Spec::Functions sometimes, but it seems workable to me.

        Ahhh, now that makes something presentable.
        use File::Basename qw(dirname); use File::Spec::Functions qw(rel2abs catdir updir); print rel2abs(catdir( dirname(__FILE__), updir() ));
        Yeah, I was attempting to follow the suggested standard of not infecting the package namespace by using the OO version. However, the functional version of File::Spec really does make it easier to follow (IMO).

        - Miller
      print File::Spec->rel2abs( File::Spec->catdir( dirname(__FILE__), File::Spec->updir(), ) );
      Bah, that is ugly! I'd do it another way.

      Did you know that rel2abs takes an optional second parameter, the reference directory? So, a much cleaner way (IMO) to do this is:

      print File::Spec->rel2abs( File::Spec->updir, dirname(__FILE__) );
      Even though you probably can just do
      print dirname(dirname(File::Spec->rel2abs(__FILE__)));
      You may probably even drop the rel2abs call, in this specific case, as __FILE__ would appear to return an absolute path – even though I don't quite trust that to always be the case.
      print dirname(dirname(__FILE__)); # ?? Is this reliable?

      All code snippets, mine and yours, show the same string in my test.