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,


In reply to Re^5: who can help me with a very interesting Perl program? by Athanasius
in thread who can help me for a very interesting perl program by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.