in reply to Re^2: Case of autovivification
in thread Case of autovivification

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.