Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^5: who can help me with a very interesting Perl program?

by Athanasius (Archbishop)
on May 04, 2016 at 04:39 UTC ( #1162142=note: print w/replies, xml ) Need Help??


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,

Replies are listed 'Best First'.
Re^6: who can help me with a very interesting Perl program?
by Anonymous Monk on May 04, 2016 at 07:55 UTC

    many thanks for Athanasius' great help!! I still want to ask question. maybe I am not good at Perl, so forgive me. According to your debug output, for the initial value for @queue and $next, I can understand them well. but after the first while loop as below:

    (1) @queue: $VAR1 = [ [ 'test\\dir1', {} ], [ 'test\\file1', $VAR1->[0][1] ], [ 'test\\file2', $VAR1->[0][1] ] ]; -------------------------------------------------- (1) $next: $VAR1 = [ 'test\\dir1', {} ];
    why $VAR1->[0][1] is pointing to {}? I don't understand it. $ref->{'test'} or $ref->{'dir1'} seems to be "undef" for ever because the last statement is just a single $hash in "while" loop that is returned to $ref->{'test'} or $ref->{'dir1'}. for the 3rd loop as below
    (3) @queue: $VAR1 = [ [ 'test\\file1', { 'dir1' => { 'file3' => undef } } ], [ 'test\\file2', $VAR1->[0][1] ] ];
    why 'dir1' had pointed to {'file3' => undef}. $hash had never been assigned one value within in "while" loop, so $ref->{$basename} should be empty for ever when $basename was a directory. Thanks again

      I’d like to help, but I don’t know what I can add to what’s already been said.

      Maybe you need to read up on references? Start with perlreftut, then look at perldsc and perlref. The Monastery’s Tutorials section also has a sub-section on References. And if you have access to the Camel Book, have a look at Chapter 8. Then go back over the replies above by choroba and by me.

      Hope that helps,

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1162142]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2022-08-11 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?