CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Finding top dir in my sys

 



jeffdox
New User

Aug 13, 2015, 3:47 PM

Post #1 of 2 (1295 views)
Finding top dir in my sys Can't Post

Hi guys
I am a beginner in perl , Just practicing some programs.I came across one program and need your help in understanding it


my $tries = 10;
my $dir = $0;
$dir =~ s/\/[^\/]*$//;
chdir $dir;
my $top;
while (1) {
if (-f ".MARKER") {
$dir = cwd();
$top = $dir;
last;
}
$tries--;
chdir("..");
if ($tries == 0) {
die "Not in a working area";
}

Can I know what these 2 lines
if (-f ".MARKER") and chdir("..") does?

thanks
Jeff


BillKSmith
Veteran

Aug 14, 2015, 6:40 AM

Post #2 of 2 (1268 views)
Re: [jeffdox] Finding top dir in my sys [In reply to] Can't Post

The error message in your "die" suggests that your system defines a "working area". The logic suggests that a "working area" consists of a directory and all its sub-directories. The presence of a file with the name ".MARKER" in a directory indicates that the directory is the top of a "working area". Your code fragment verifies that the current program ($0) is in a "working area". (It dies with the message if it is not)

Now I can answer your specific questions.


Code
if (-f ".MARKER")


tests for the presence of the file (not a directory or symbolic link) ".MARKER" in the current working directory.

In most operating systems, ".." is a special directory name. It refers to the directory which contains the current working directory. So, the command

Code
 chdir("..")


moves the current directory up one level by changing to the directory which contains it.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives