Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^4: who can help me for a very interesting perl program

by Anonymous Monk
on May 03, 2016 at 09:04 UTC ( [id://1162081]=note: print w/replies, xml ) Need Help??


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

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.

  • Comment on Re^4: who can help me for a very interesting perl program

Replies are listed 'Best First'.
Re^5: who can help me with a very interesting Perl program?
by Athanasius (Archbishop) on May 04, 2016 at 04:39 UTC

    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

        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://1162081]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-19 03:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found