S
sonet
The code print file in directory.But i have a question about the
variable scope.
Directory tree:
file: /usr/local/app/f1
file: /usr/local/app/f2
dir: /usr/local/app/d1
dir: /usr/local/app/d1/d2
If i use opendir(DIR,$path) not opendir(my $DIR,$path). The code
will terminate. Becase the FILEHANDLE closed as the main loop is
running.
When the code run at /usr/local/app/d1 , It have not terminate.
It will call self(recursive).Why the variable($path) in main loop
will not influence each other?
------------------------------------------------------------------------------------
my $dir='/usr/local/app';
listdir("$dir");
sub listdir{
my $path=shift;
opendir(my $DIR,$path);
while(my $file=readdir($DIR)){
if (-f "$path/$file"){
print "$path/$file\n";
}elsif (-d "$path/$file" && $file ne '.' && $file ne '..'){
listdir("$path/$file");
}
}
close($DIR);
}
variable scope.
Directory tree:
file: /usr/local/app/f1
file: /usr/local/app/f2
dir: /usr/local/app/d1
dir: /usr/local/app/d1/d2
If i use opendir(DIR,$path) not opendir(my $DIR,$path). The code
will terminate. Becase the FILEHANDLE closed as the main loop is
running.
When the code run at /usr/local/app/d1 , It have not terminate.
It will call self(recursive).Why the variable($path) in main loop
will not influence each other?
------------------------------------------------------------------------------------
my $dir='/usr/local/app';
listdir("$dir");
sub listdir{
my $path=shift;
opendir(my $DIR,$path);
while(my $file=readdir($DIR)){
if (-f "$path/$file"){
print "$path/$file\n";
}elsif (-d "$path/$file" && $file ne '.' && $file ne '..'){
listdir("$path/$file");
}
}
close($DIR);
}