[pm-h] Meeting Reminder
Mike Flannigan
mikeflan at earthlink.net
Thu Feb 16 06:23:34 PST 2006
"G. Wade Johnson" wrote:
> At a glance, I'd say you could replace the 'local's with 'my' variables. Using
> 'my' or lexical variables is usually considered safer than 'local's.
>
> Using File::Spec to manipulate filenames is cleaner and makes your code
> portable to other OSs.
>
> Of course, File::Find is a great module for doing the recursing file thing.
>
> See below for additional comments.
snip
>
> > {
> > local ( $dirname ) = @_;
> # This is pretty much the standard idiom for getting subroutine parameters.
> my $dirname = shift;
>
> > local ( @entries , $entry , $path , @dirs_list , $dircount , @files
> > );
> >
>
> If you are using a modern Perl, it's better to use 'my $dir' in place of DIR
> in the opendir() and $dir everywhere else you use DIR.
>
> > if ( ! opendir(DIR,$dirname) ) {
snip
>
> > $dircount += &search_dir($dirname."\\".$entry);
>
> You don't need the '&' anymore.
snip
> >
> > # print "Directories under \"$dirname\"\n";
>
> To save yourself from the backslashes I prefer qq{} when I need quotes in a
> string.
> # print qq{Directories under "$dirname"\n};
>
> > $count = &search_dir($dirname);
>
> You don't need the '&' anymore.
>
> > print "\n";
Thanks a bunch Wade. Now I understand the code
much better. I didn't realize until now that this code
was not using File::Find. I guess maybe it borrows
some code from the module - maybe.
I finally got 'use strict' in there. This is the first I have
tried to get 'use strict' in there.
I guess I should use the 'my's throughout each section
of code, but I put most of the variable as globals.
More work to do later.
Here is what I ended up with so far:
use English;
use strict;
use warnings;
my ($sizemax, $countnum, $pathren, $filename, $filenameren, $len, $renfull, $dir);
print "\n";
sub search_dir
{
my $dirname = shift;
my ( @entries, $entry, $path, @dirs_list, $dircount, @files );
if ( ! opendir($dir,$dirname) ) {
print STDERR "opendir failed for \"$dirname\" : $!\n";
return 0;
} # IF
@entries = readdir $dir;
closedir $dir;
@dirs_list = ();
@files = ();
$dircount = 0;
foreach $entry ( @entries ) {
next if ($entry =~ /\.sys/);
if ( $entry eq "." || $entry eq ".." ) {
next;
} # IF
$path = sprintf "%s\\%s",$dirname,$entry;
if ( -d $path && $entry ne "." && $entry ne ".." ) {
$dircount += 1;
push(@dirs_list,$entry);
} # IF
else {
$path =~ s#C:/Copy2/\\#C:\\Copy2\\#i;
$path =~ m#(C:\\Copy2\\.*\\)(.*)#i;
$pathren = $1 if ($1);
$filename = $2 if ($2);
if (length $filename > $sizemax) {
$countnum++;
$countnum = sprintf("%06d", $countnum);
$len = $sizemax - 10;
($filenameren = $filename) =~
s/^(.{$len}).*(\..{3})$/$1$countnum$2/;
$renfull = $pathren . $filenameren;
rename $path, $renfull;
push
@files,("Renamed\n".$filename."\nto\n".$filenameren."\n");
}
} # ELSE
} # FOREACH
if ( 0 < @files ) {
print "\n";
foreach ( @files ) {
print "$_\n";
} # FOREACH
} # IF
foreach $entry ( @dirs_list ) {
$dircount += search_dir($dirname."\\".$entry);
} # FOREACH;
return $dircount;
} # end of search_dir
MAIN:
{
my ($dirname, $count);
$dirname = 'C:/Copy2/';
$sizemax = 60;
$sizemax += 4;
$countnum = 0;
# print qq{Directories under "$dirname"\n};
$count = search_dir($dirname);
print "\n";
print "\n$count total directories\n";
exit 0;
} # end of MAIN
__END__
More information about the Houston
mailing list