Perl script explanation: $type = ( -d "$path\\$entry" )

Status
Not open for further replies.

sreeni

Member level 1
Joined
May 24, 2006
Messages
35
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,286
Activity points
1,557
$type = ( -d "$path\\$entry" ) ? "dir" : "file";

It is a part of a script. What the script does is it will serch the specified directory and list the all the contents of it including files and directories with type specified whether it is directory or file.

The above line of script is intented to set the type as "dir" or "file".
My doubt is how this will be done?
I couldn't understand -d "$path\\$entry"

$path is the path of a directory and a $entry is a patricular file in that direcotry
 

Re: perl - file type

-d tests if given path is directory and than you get TRUE, otherwise you will get FALSE. Based on that you variable will get value "dir" or "file".
 

Re: perl - file type

That's right. But -d "$entry" will do the work. Then what is the use of "$path\\$entry"
 

perl - file type

Hi

$type = ( -d "$path\\$entry" ) ? "dir" : "file";

Says:
If the entity: $path\\$entry is a directoy, then assign $type as "dir" otherwise then it seems that it is a file then assign $type as "file".

You may also change as this also:

if (-d "$path\\$entry"){
$type = "dir";
}
elsif (-e "$path\\$entry"){
$type = "file";
}
else{
$type ="undefined!";
}


Note:
The code seems to be used in win32, but you can use from single slash instead of double backslashses too.

"$path\\$entry"


"$path/$entry"


tnx
 

Re: perl - file type

$type = ( -d "$path\\$entry" ) ? "dir" : "file";
My problems is not with the ?: combiantion.

Thing is $path and $entry are two separate variables.

$path contains the path of a direcotry that we have given as command line input
$entry is the each item within that directory whcih can be either a file or a directory.

What does "$path\\$entry" denote ?
What if we are using just "$entry" instead of "$path\\$entry"
 

Re: perl - file type

That is a absolute path name. Using that you can access that file or directory from any place in the directory tree.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…