BrianAtWork
Programmer
I found a perl script that gives you the disk usage of a directory and outputs it in a tree structure.
This isn't coded the best, but it does bring up some interesting techniques that I thought PaulTEG, WarBlade, Kevin, and others might enjoy explaining? There were a few lines in this code especially that I was wondering if people could explain to me.
So here's the code:
In the "input" subroutine, there is what I am guessing is a pointer to the array @kid. Can someone explain what happens on the last line of the "input" sub?
And can someone explain the 2nd to last line in the "output" sub? It's a little confusing.
It's a Friday, so I just thought I would generate some discussion. This code is a little hard to read, but I thought the more experienced people could explain some of the "features"?
Thanks in advance!
Brian
This isn't coded the best, but it does bring up some interesting techniques that I thought PaulTEG, WarBlade, Kevin, and others might enjoy explaining? There were a few lines in this code especially that I was wondering if people could explain to me.
So here's the code:
Code:
#!/usr/bin/perl
@lines = `du -k @ARGV`;
chomp(@lines);
&input($top = pop @lines);
&output($top);
exit $?;
sub input {
local($root, *kid, $him) = @_[0,0];
while (@lines && &childof($root, $lines[$#lines])) {
&input($him = pop(@lines));
push(@kid, $him);
}
@kid = &sizesort(*kid);
}
sub output {
local($root, *kid, $prefix) = @_[0,0,1];
local($size, $path) = split(' ', $root);
$path =~ s!.*/!!;
$line = sprintf("%${width}d %s", $size, $path);
print $prefix, $line, "\n";
$prefix .= $line;
$prefix =~ s/\d /| /; $prefix =~ s/[^|]/ /g;
local($width) = $kid[0] =~ /(\d+)/ && length("$1");
for (@kid) { &output($_, $prefix); };
}
sub sizesort {
local(*list, @index) = shift;
sub bynum { $index[$b] <=> $index[$a]; }
for (@list) { push(@index, /(\d+)/); }
@list[sort bynum 0..$#list];
}
sub childof {
local(@pair) = @_;
for (@pair) { s/^\d+\s+//g; }
index($pair[1], $pair[0]) >= 0;
}
In the "input" subroutine, there is what I am guessing is a pointer to the array @kid. Can someone explain what happens on the last line of the "input" sub?
And can someone explain the 2nd to last line in the "output" sub? It's a little confusing.
It's a Friday, so I just thought I would generate some discussion. This code is a little hard to read, but I thought the more experienced people could explain some of the "features"?
Thanks in advance!
Brian