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


In reply to who can help me for a very interesting perl program by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.