Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Parent directory - regex

by periapt (Hermit)
on Jun 17, 2004 at 12:30 UTC ( #367596=note: print w/replies, xml ) Need Help??


in reply to Parent directory - regex

There are many ways to do this sort of thing. If your path is always known in a string then one simple approach is this
my $path = "/dir1/dir2/dir3/ .. /dirn-1/dirLast//"; my @pathary = split(/\//,$path); my $parentpath = join('/',@pathary[0..$#pathary-1]); # pop @path; # also # my $parentpath = join('/',@pathary); # works print "$path\n$parentpath\n";
An alternative, if the directory your are in is unknown or changes in an unpredictible manner, might be something like this. Note: This is rather inefficient but works.
use Cwd; my $oldpath = getcwd(); print "$oldpath\n"; $path = '/var/tmpdir'; chdir $path; $path = getcwd(); # for info only print "$path\n"; # for info only chdir "../"; $path = getcwd(); print "$path\n";

PJ
We are drowning in information and starving for knowledge - Rutherford D. Rogers
What good is knowledge if you have to pull teeth to get it - anonymous

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://367596]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2022-11-30 06:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?