Hello, while reading Intermediate perl, I got stuck on page 97. which has this code
#!/usr/bin/perl -w use strict; use Data::Dumper; sub data_for_path { my $path = shift; if (-f $path or -l $path) { #files or symbolic links return undef; } if (-d $path) { my %directory; opendir PATH, $path or die "cannot opendir $path: $!"; my @names = readdir PATH; closedir PATH; for my $name (@names) { next if $name eq '.' or $name eq '..'; $directory{$name} = data_for_path("$path/$name"); } return \%directory; } warn "$path is neither a file nor a directory\n"; return undef; } print Dumper(data_for_path('.'));
which shows me the output of
$VAR1 = { 'perl.1' => undef, 'bin' => { 'perl.1' => undef, 'chaos.1' => undef }, 'perl.sub_sub' => undef, 'perl.sub_multi' => undef, 'chaos.1' => undef, 'perl.filehandle' => undef, 'man' => {}, 'lib' => { 'yahoo3' => { 'yahoo3' => { 'yahoo1111' => undef }, 'yahoo5' => undef }, 'yahoo2' => undef, 'yahoo' => undef }, 'perl.log' => undef, 'perl.recursive_data' => undef, 'perl.callbacks' => undef };
But I was trying to do it without datadumper to see if I can print out the structure..(possibly with references) tried couple things but does not work.. any suggestion? Problem I see is that I am not so sure what is being return from the sub routines.. and even if I know what it looks like, if sub directory has multiple subdirectories.. woudln't it make it almost impossible to build a right structure of reference?

In reply to subroutine reference questions by convenientstore

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.