http://qs321.pair.com?node_id=1162142


in reply to Re^4: who can help me for a very interesting perl program
in thread who can help me for a very interesting perl program

The line:

unshift @queue, map { [$_, $hash] } @new_paths;

does indeed put a reference to an empty hash into the queue (note: “empty” here is not the same as undef). In a later iteration, this hash reference is the $ref read from the queue in these lines:

while (my $next = shift @queue) { my ($path, $ref) = @$next; ...

At this point, yes, the hash is still empty. But two lines later in the while loop there is this statement:

$ref->{$basename} = do { ... };

And that’s where the hash is populated: assigning something (in this case, whatever is returned by the do block) to a hash key is a way of populating the hash. Don’t be confused by the fact that the hash is referenced here by the name $ref rather than $hash. They’re both references, and they both point to the same (anonymous) hash.

It might help you to visualize what is going on if you display the value of the queue on each iteration of the while loop. Here is the code I used:

use strict; use warnings; use diagnostics; use File::Basename; # basename use File::Spec::Functions; # catdir use Data::Dumper; # Dumper use Data::Dump; # dd use constant DD => 1; # 0 = none, 1 = Dumper, 2 += Dump my $data = data_for_path('./test'); # '/root/excerise_perl/tes +t' print "RESULT:\n"; DD == 2 ? dd $data : print Dumper($data); # default to Data::Dumper sub data_for_path { my ($path) = @_; my $data = {}; my @queue = ( [ $path, $data ] ); my $count = 0; show_queue($count, \@queue) if DD; while (my $next = shift @queue) { if (DD) { print "($count) \$next:\n"; DD == 2 ? dd $next : print Dumper($next); print '=' x 50, "\n"; } my ($path, $ref) = @$next; my $basename = basename($path); $ref->{$basename} = do { if (-f $path or -l $path) # plain file or symbolic link { 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; } }; } continue { show_queue(++$count, \@queue) if DD; } $data; } sub show_queue { my ($count, $queue) = @_; print "($count) \@queue:\n"; DD == 2 ? dd $queue : print Dumper($queue); print '-' x 50, "\n"; }

(I was experimenting with Data::Dumper vs. Data::Dump for the output. Surprisingly — as I normally prefer Data::Dump — the output from Data::Dumper was easier to read.) Here is the output with DD set to 1:

14:34 >perl 1620_SoPW.pl (0) @queue: $VAR1 = [ [ './test', {} ] ]; -------------------------------------------------- (0) $next: $VAR1 = [ './test', {} ]; ================================================== (1) @queue: $VAR1 = [ [ 'test\\dir1', {} ], [ 'test\\file1', $VAR1->[0][1] ], [ 'test\\file2', $VAR1->[0][1] ] ]; -------------------------------------------------- (1) $next: $VAR1 = [ 'test\\dir1', {} ]; ================================================== (2) @queue: $VAR1 = [ [ 'test\\dir1\\file3', {} ], [ 'test\\file1', { 'dir1' => $VAR1->[0][1] } ], [ 'test\\file2', $VAR1->[1][1] ] ]; -------------------------------------------------- (2) $next: $VAR1 = [ 'test\\dir1\\file3', {} ]; ================================================== (3) @queue: $VAR1 = [ [ 'test\\file1', { 'dir1' => { 'file3' => undef } } ], [ 'test\\file2', $VAR1->[0][1] ] ]; -------------------------------------------------- (3) $next: $VAR1 = [ 'test\\file1', { 'dir1' => { 'file3' => undef } } ]; ================================================== (4) @queue: $VAR1 = [ [ 'test\\file2', { 'dir1' => { 'file3' => undef }, 'file1' => undef } ] ]; -------------------------------------------------- (4) $next: $VAR1 = [ 'test\\file2', { 'dir1' => { 'file3' => undef }, 'file1' => undef } ]; ================================================== (5) @queue: $VAR1 = []; -------------------------------------------------- RESULT: $VAR1 = { 'test' => { 'file1' => undef, 'file2' => undef, 'dir1' => { 'file3' => undef } } }; 14:34 >

As you can see, the anonymous hash is indeed populated as the loop iterates.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,