Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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 $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

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2022-08-11 17:14 GMT
Find Nodes?
    Voting Booth?

    No recent polls found