[Chicago-talk] Is it worth to be put in CPAN?

tiger peng tigerpeng2001 at yahoo.com
Mon Oct 16 17:45:18 PDT 2006

Hi all,
Recently, I am handling a couple of hierarchy data, such as purging log files, purging data base table without foreign key being implemented in DBMS, generating catalog XML files, etc.
After searching CPAN without finding very simple/generic modules. So, I made one module for myself. It seems very convenient to me. I am wandering if it is worth to be put on CPAN?

The module have only one recursive function, hierarchy_traverse, with the interface as below:
hierarchy_traverse($roots,                      # a scalar for one root,
                                                           # or a ref to a list of roots,
                                                           # or a ref to a list of list that contain information about the roots.
                          \&get_children,           # a function for get child nodes
                           {                                                                                    #Options
                                  depth             => 1,                                                 # how depth limitaion. (default undef, no limitation)
                                  pre_branch     => sub {print "dir: ", $_[0],"\n"},            # the function called before visit childeren nodes
                                  post_branch    => sub {},                                          # the fucntion called after visite all it children nodes
                                  bare_branch    => sub {print "empty dir: ", $_[0],"\n"}, # the function for empty branches
                                  leaf                 => sub {...}                                        # the function for leaf nodes
                                  is_leaf             => sub { ! -d $_[0]} ,                           # the function for check if a node is leaf
                                                                                                                # all default functions are default to be {} (do nothing and return false)
                            } ,

And Here is a test script for that get total size of ".XML" file under some directions.


#!/usr/local/bin/perl -w
use strict;
use lib "/home/tiger/playground/tigercvs/powerReviews/lib";
use Hierarchy::Traverser;
my $total_file_size;
my $roots=["/home/tiger/playground/tigercvs/powerReviews/bin"];#, "/home/tiger/playground/p2000"];
hierarchy_traverse($roots, \&read_dir,
                       #pre_branch     => sub {print "dir: ", $_[0],"\n"},
                       post_branch    => sub {},
                       #bare_branch    => sub {print "empty dir: ", $_[0],"\n"},
                       is_leaf        => sub { ! -d $_[0]} ,
                       leaf           => sub {if ($_[0] =~ /\.xml/i) {
                                                print "file: $_[0]";
                                                my $file_size = -s $_[0];
                                                print "\t$file_size\n";
                                                $total_file_size += $file_size}
                                              } ,
print "total file size $total_file_size \n";

sub read_dir{
  if (! -d  $_[0]) {
    warn "Try to open dir where $_[0] is not dir\n";
  my $dirhandle;
  my $files =[];
  opendir ($dirhandle, $_[0]) or die "Cannot open $_[0] dir";
  while ( defined (my $file = readdir($dirhandle)) ) {
     next if $file =~ /^\.\.?$/;
     if (-l $file) {warn "Ignore $_[0]/$file, which is a link.\n"; next}
     push @$files, "$_[0]/$file";
  closedir $dirhandle;
  return $files;

Out put:

Ignore /home/tiger/playground/tigercvs/powerReviews/bin/cvslink, which is a link.
Ignore /home/tiger/playground/tigercvs/powerReviews/bin/tree1.data.link, which is a link.
file: /home/tiger/playground/tigercvs/powerReviews/bin/catalog_print.xml        100739
file: /home/tiger/playground/tigercvs/powerReviews/bin/catalog_xmlsimple.xml    83738
file: /home/tiger/playground/tigercvs/powerReviews/bin/catalog_print_pretty.xml 93965
file: /home/tiger/playground/tigercvs/powerReviews/bin/empty.xml        100739
file: /home/tiger/playground/tigercvs/powerReviews/bin/catalog_xmlsimple_with_empty.xml 88969
file: /home/tiger/playground/tigercvs/powerReviews/bin/allCata.xml      166937
file: /home/tiger/playground/tigercvs/powerReviews/bin/tmp.xml  79003
total file size 714090

More information about the Chicago-talk mailing list