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


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

$hash is a hash reference. The referenced hash will get populated in a later iteration of the loop when it becomes $ref. The important thing is that the $hash returned from the do is the same empty hash as the one stored in the @queue by the map.

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^4: who can help me for a very interesting perl program
by Anonymous Monk on May 03, 2016 at 09:04 UTC

    Hi choroba, for your words "The referenced hash will get populated in a later iteration of the loop when it becomes $ref ". can you explain/demonstrate it in detail:). I think the $hash is "undef" for ever.. Thanks a lot.

      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:

      Hope that helps,

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

        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