Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

who can help me for a very interesting perl program

by Anonymous Monk
on May 03, 2016 at 05:41 UTC ( #1162062=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

BEGIN { push ( @INC, '/usr/local/lib/perl/5.14.2/File/') } #!/usr/bin/perl use File::Basename; use File::Spec::Functions; use Data::Dumper; use diagnostics; use warnings; use strict; my $data = data_for_path('/root/excerise_perl/test'); #print Dumper($data); sub data_for_path { my ($path) = @_; my $data = {}; my @queue = ([$path, $data]); while (my $next = shift @queue) { my($path, $ref ) = @$next; my $basename = basename ($path); $ref->{$basename} = do { if (-f $path or -l $path) {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; } }; } $data; }

this script is used to print one directory structure. if it is a file, then this values would be "undef", if it is a directory, it would be the hash reference for its child directory. now I run this script in my pc, the result is as below: root@TestMachine:~/excerise_perl# perl data_for_path.pl $VAR1 = { 'test' => { 'file2' => undef, 'file1' => undef, 'dir1' => { 'file3' => undef } } }; the directory have two files named "file1" and "file2", one director named "dir1" that has only one file named "file3". I can't understand this sentence "unshift @queue , map {$_, $hash} @new_paths;" the $hash seems not to be assigned a value, but why is $hash returned? what thing does "map {$_, $hash} @new_paths"? someone can help me to understand this script? thanks a lot!!

Replies are listed 'Best First'.
Re: who can help me for a very interesting perl program
by choroba (Archbishop) on May 03, 2016 at 07:53 UTC
    The $hash is returned from do, i.e. it gets assigned to $ref->{$basename}. It goes to the queue, too, from where it gets shifted and extracted to $ref in a future iteration.

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

      thanks choroba and QuillMeantTen. I am still confused. what is its value of "$hash" after performing "unshift @queue , map {$_, $hash} @new_paths;" And I always feel that the value of "$hash" is "undef" all alone, because "map" seems not to be able to assign a value to $hash.

      root@TestMachine:~/excerise_perl# perl data_for_path.pl $VAR1 = { 'test' => { 'file2' => undef, 'file1' => undef, 'dir1' => { 'file3' => undef } } };
      as the result from above, How does $$data->{'test'} point to its child directory.

        $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,
Re: who can help me for a very interesting perl program
by QuillMeantTen (Friar) on May 03, 2016 at 06:35 UTC

    Perl's map does exactly what it says on the tin: it maps one set of values to another (yeah sure, depends on what you mean by map). Hash gets returned at the last line of your else because you call it (you know that single line with only hash on it). unshift is used to to put a value at the beginning of an array. so the line you do not understand can be rephrased as (keeping in mind that we evaluate from right to left):

    my @tmparray = map{[$_,$hash]} @new_paths; unshift @queue,@tmparray;
    tmparray being an array of array refs each containing two values: one from new paths and an array_ref.
    since afterward you keep going at queue until it's empty and you do not save anything in data, $data is undefined (beside the fact that it is an empty hash) and you only get the last $hash.

    I invite anyone to point out any mistake, I do have some difficulties with composed statements such as this one too. I also hotly recommend perldoc brand of fishing.
    Update:I dont think its hash that is returned, seems its data in any case because it gets called after the while as last statement

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2022-06-29 19:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (97 votes). Check out past polls.

    Notices?