J
James Bonanno
Hello;
I've written a basic program that will scan a directory, and then
build a basic tree and turn that into HTML. The program is using the
File::Find::Rule and HTML::Template modules. I am building a basic
"project manager/reporting" software. I've arranged all my projects on
my computer in this strucutre:
Projects
--- Project1
----------Visio
----------Word
----------Schematics
----------Verilog
--- Project2
----------Visio
----------Word
----------Schematics
----------Verilog
. . . and so on. What I really want to do is to be able to have
"irregular" numbers of subprojects under each project, and gracefully
handle them with my code. For example, if Project1 contained a
subdirectory "Excel" in addition to Visio,Word,Schematic, and Verilog,
I'd like the program to be able to handle that rather than having a
"fixed number" of subprojects. My code is shown below, where the my
%row hash is the problem. I want to be able to make proj1, proj2,
proj3 a "dynamic size". I'm a newbie at perl, please forgive my
approach if another is clearly more obvious. Any suggestions are
welcome.
Thanks,
James
use File::Find::Rule;
use HTML::Template;
$root = "C:/DATA_JAMES/Projects/";
@projects_to_display = &dirscan_root("$root");
# Build an array of project names
@projects = &dirscan("$root");
# Build a hash of multi-value keys associating
# each project with its various subprojects/subdirectories
foreach $dir ( @projects ) {
$HoA{$dir} = [dirscan_root($dir)];
}
my $template = HTML::Template->new(filename => 'Find_Dir.html');
my @loop; # the loop data will be put in here
# fill in the loop, sorted by directory name
foreach my $name (sort keys %HoA) {
# get the directory and three proejcts from HoA hash
my ($dir, $proj1, $proj2, $proj3) = @{$HoA{$name}};
# make a new row for each project - the keys are <TMPL_VAR> names
# and the values are subprojects
my %row = (
dir => $name,
proj1 => $proj1,
proj2 => $proj2,
proj3 => $proj3,
);
# put this row into the loop by reference
push(@loop, \%row);
}
$nowstring = localtime;
# call param to fill in the loop with the loop data by reference.
$template->param(project_loop => \@loop);
$template->param(path => $nowstring);
print "Content-Type: text/html\n\n";
# print the template
print $template->output;
# This function scans the given directory and returns
# every subdirectory
sub dirscan_root{
$dir_to_scan = shift(@_);
@tree=File::Find::Rule->directory->relative->maxdepth(1)->in
("$dir_to_scan");
shift(@tree);
return (@tree);
};
# This function scans the given directory and returns
# an array with subdirectories indicated with a full path
sub dirscan{
$dir_to_scan = shift(@_);
@tree=File::Find::Rule->directory->maxdepth(1)->in("$dir_to_scan");
shift(@tree);
return (@tree);
};
I've written a basic program that will scan a directory, and then
build a basic tree and turn that into HTML. The program is using the
File::Find::Rule and HTML::Template modules. I am building a basic
"project manager/reporting" software. I've arranged all my projects on
my computer in this strucutre:
Projects
--- Project1
----------Visio
----------Word
----------Schematics
----------Verilog
--- Project2
----------Visio
----------Word
----------Schematics
----------Verilog
. . . and so on. What I really want to do is to be able to have
"irregular" numbers of subprojects under each project, and gracefully
handle them with my code. For example, if Project1 contained a
subdirectory "Excel" in addition to Visio,Word,Schematic, and Verilog,
I'd like the program to be able to handle that rather than having a
"fixed number" of subprojects. My code is shown below, where the my
%row hash is the problem. I want to be able to make proj1, proj2,
proj3 a "dynamic size". I'm a newbie at perl, please forgive my
approach if another is clearly more obvious. Any suggestions are
welcome.
Thanks,
James
use File::Find::Rule;
use HTML::Template;
$root = "C:/DATA_JAMES/Projects/";
@projects_to_display = &dirscan_root("$root");
# Build an array of project names
@projects = &dirscan("$root");
# Build a hash of multi-value keys associating
# each project with its various subprojects/subdirectories
foreach $dir ( @projects ) {
$HoA{$dir} = [dirscan_root($dir)];
}
my $template = HTML::Template->new(filename => 'Find_Dir.html');
my @loop; # the loop data will be put in here
# fill in the loop, sorted by directory name
foreach my $name (sort keys %HoA) {
# get the directory and three proejcts from HoA hash
my ($dir, $proj1, $proj2, $proj3) = @{$HoA{$name}};
# make a new row for each project - the keys are <TMPL_VAR> names
# and the values are subprojects
my %row = (
dir => $name,
proj1 => $proj1,
proj2 => $proj2,
proj3 => $proj3,
);
# put this row into the loop by reference
push(@loop, \%row);
}
$nowstring = localtime;
# call param to fill in the loop with the loop data by reference.
$template->param(project_loop => \@loop);
$template->param(path => $nowstring);
print "Content-Type: text/html\n\n";
# print the template
print $template->output;
# This function scans the given directory and returns
# every subdirectory
sub dirscan_root{
$dir_to_scan = shift(@_);
@tree=File::Find::Rule->directory->relative->maxdepth(1)->in
("$dir_to_scan");
shift(@tree);
return (@tree);
};
# This function scans the given directory and returns
# an array with subdirectories indicated with a full path
sub dirscan{
$dir_to_scan = shift(@_);
@tree=File::Find::Rule->directory->maxdepth(1)->in("$dir_to_scan");
shift(@tree);
return (@tree);
};