[Israel.pm] array manipulation

Yona Shlomo yona at cs.technion.ac.il
Sun Apr 3 23:50:33 PDT 2005


Consider the following task:

read files from a given directory
foreach file get its datetime and its size
then if the total size of files exceeds some given limit
	remove files (older files come first) until the
	limit is satisfied.

This is actually one possible policy for caching or a
recycle bin...

Now, consider the following implementation

        my $size = 0; # accumulative size of files in the recycle bin
        opendir(DIR, $path_on_disk) or confess "Cannot opendir $path_on_disk : $!\n";

        # delete only files that are too old and exceed the
        # max size limit
        map {
                unlink $_->[0] or confess "Cannot delete file $_->[0] : $!\n";
        } grep {
                $_->[1]>$SIZE_LIMIT;    # test accumulative size
        } map [
                $_->[0], $size+=$_->[2]         # [ name,accumulative size]
        ] => sort {
                $a->[1] <=> $b->[1]     # sort by timedate
        } map [
                $_, (stat($_))[9], -s( $_ )     # [name,time,size]
        ] => grep {
                !/^\./ && -f "$path_on_disk/$_";        # pass files
        } readdir(DIR);
        closedir DIR;


Is the memory allocated for storing the datetime and the
size being re-used in other parts of this chain or can it be
reused only after the whole expression is being evaluated
(actually, only after the next expression begins its


Shlomo Yona
yona at cs.technion.ac.il

More information about the Perl mailing list