BEGIN { push ( @INC, '/usr/local/lib/perl/5.14.2/File/') }
#!/usr/bin/perl
use File::Basename;
use File::Spec::Functions;
use Data::Dumper;
use diagnostics;
use warnings;
use strict;
my $data = data_for_path('/root/excerise_perl/test');
#print Dumper($data);
sub data_for_path {
my ($path) = @_;
my $data = {};
my @queue = ([$path, $data]);
while (my $next = shift @queue) {
my($path, $ref ) = @$next;
my $basename = basename ($path);
$ref->{$basename} = do {
if (-f $path or -l $path) {undef}
else {
my $hash = {};
opendir ((my $dh) , $path);
my @new_paths =map {
catfile ($path , $_)
} grep {! /^\.\.?\z/ } readdir $dh;
unshift @queue , map {[$_, $hash]} @new_paths;
$hash;
}
};
}
$data;
}
this script is used to print one directory structure. if it is a file, then this values would be "undef", if it is a directory, it would be the hash reference for its child directory. now I run this script in my pc, the result is as below:
root@TestMachine:~/excerise_perl# perl data_for_path.pl
$VAR1 = {
'test' => {
'file2' => undef,
'file1' => undef,
'dir1' => {
'file3' => undef
}
}
};
the directory have two files named "file1" and "file2", one director named "dir1" that has only one file named "file3".
I can't understand this sentence "unshift @queue , map {$_, $hash} @new_paths;" the $hash seems not to be assigned a value, but why is $hash returned? what thing does "map {$_, $hash} @new_paths"? someone can help me to understand this script? thanks a lot!!