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,
|