Try running your script with some extra print statements and you can see what it is doing as it progresses. I expect the data structure it is building is not what you want and may help explain why you are using so much memory.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %filesystem; my $ref = $filesystem{root} ||= {}; traverse(".",""); sub traverse{ my ($folder,@path) = @_; print "traverse called for $folder, @path\n"; opendir DIR,$folder; my @list = grep {!/^\.{1,2}$/} readdir DIR; # exclude . and .. closedir DIR; foreach my $entry(@list){ print "processing $entry\n"; print "%filesystem (" . \%filesystem . ") contains:\n" + . Dumper(\%filesystem); print "\$ref ($ref) is pointing to:\n" . Dumper($ref); sleep(1); next if ( -l $folder."/". $entry); # skip links $ref = $$ref{$_} ||= {} for @path; if( -d $folder."/". $entry){ traverse($folder."/".$entry,@path,$entry); }else{ $$ref{$folder} = $entry; }; }; }; print Dumper(\%filesystem);

You should think carefully about the line $ref = $$ref{$_} ||= {} for @path; in your subroutine.

Also, your subroutine is recursive. It keeps changing the global variable $ref. Think about what $ref is referencing and how this changes as your program progresses.


In reply to Re^3: Case of autovivification by ig
in thread Case of autovivification by resistance

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.