Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
Can this code:

BEGIN { $0 =~ '(.*[\\\/])\w+\.\w+$'; $curr_dir = $1 || "./"; }


Instead of giving me the current directory can it be able to stop one directory before so I could add if I would like to the path to go one directory above or not?
If the code find its currently directory in c:/files/more/tes.pl
I would like the code to stop here:

c:/files

And I can do this:

${curr_dir}/images/or any other directory

Just trying to make this a little more flexible.
Thanks for the Help!

Replies are listed 'Best First'.
Re: Current Directory
by davidrw (Prior) on Sep 14, 2005 at 13:30 UTC
    This will do the trick (get the directory of $0, then get the directory of that):
    use File::Basename; my $curr_dir = dirname(dirname($0)) . '/';
      Could I manipulate that going on directory UP instead?
        "UP"? do you mean universal path (i.e. absolute path)? if so, then yes, just convert the directory that $0 is in to a absolute path first:
        use File::Basename; use Cwd 'abs_path'; my $curr_dir = dirname(abs_path dirname($0)) . '/';
Re: Current Directory
by Hue-Bond (Priest) on Sep 14, 2005 at 13:35 UTC

    Tested on Linux:

    #!/usr/bin/perl use strict; use warnings; use Cwd 'abs_path'; sub in_dir { $_ = shift; m|(.*)/.*$|; ## we take advantage of greediness $1 || "/"; } my $self = abs_path $0; my $stripped = in_dir in_dir $self; print "$self -> $stripped\n"; __OUTPUT__ $ # there's a copy in my home and other in / $ ./a.pl /home/hue/a.pl -> /home $ /a.pl /a.pl -> / $ cd / $ ./a.pl /a.pl -> /

    UPDATE: I knew I was reinventing the wheel, I just didn't find that File::Basename thingy.

    --
    David Serrano

Re: Current Directory
by fizbin (Chaplain) on Sep 14, 2005 at 14:20 UTC

    It took me three times reading that to figure out what you were trying to say.

    In any case, the general question you ask about finding, given a directory, its parent directory, is best done through using File::Spec. Actually, though, if you're going to be using it anyway, you probably want to use it in the snippet of code you already posted. This code gets you $currdir as before, its parent, and the parent of that:

    BEGIN { use File::Spec; use strict; my ($currdir, $currdirp, $currdirpp); my ($zvolume, $zdirs, $zfile) = File::Spec->splitpath( File::Spec->rel2abs($0) +); my @dirs = File::Spec->splitdir($zdirs); $currdir = File::Spec->catpath($zvolume, $zdirs, ''); pop @dirs; $currdirp = File::Spec->catpath($zvolume, File::Spec->catdir(@dirs), + ''); pop @dirs; $currdirpp = File::Spec->catpath($zvolume, File::Spec->catdir(@dirs) +, ''); # Now do something with those variables. }
    --
    @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/
Re: Current Directory
by svenXY (Deacon) on Sep 14, 2005 at 13:33 UTC
    Hi,
    I'm not sure I really understood what you want, but how about this:
    my $pwd = `pwd`; print $pwd; my ($parent_dir) = $pwd =~ m|^(.*)/[^/]+.*$|g; print $parent_dir;
    prints:
    /home/myself/develop/0_test /home/myself/develop
    cheers,
    svenXY
Re: Current Directory
by strat (Canon) on Sep 15, 2005 at 07:44 UTC

    Another way is to use FindBin instead of $0, e.g

    use FindBin; use File::Spec; my $path = File::Spec->catdir($FindBin::Bin, "..");
    or the like.

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Re: Current Directory
by sh1tn (Priest) on Sep 14, 2005 at 13:36 UTC
    # Cwd - get pathname of current working directory use Cwd; my $dir = getcwd;