[pm-h] Meeting Reminder

G. Wade Johnson gwadej at anomaly.org
Tue Feb 14 16:11:03 PST 2006


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.

On Tue, 14 Feb 2006 07:31:27 -0600
Mike Flannigan <mikeflan at earthlink.net> wrote:

> 
> The script below works fine, I think, but I've only run it twice.
> I'm only posting it to see if anybody has any comments
> about trivial things that may be wrong with it.  Especially
> the "local" statement in MAIN.  I suspect I don't have
> that quite correct.
> 
> The main recursive routine was obtained from others
> long ago.  I now use it for many things - this being
> the file name shortening routine.  99% of my scripts
> have 'use warnings' and 'use strict', but this one doesn't.
> I never took the time to convert it and I seem to get
> by modifying it without these pragmas in place.
> 
> This script is kinda Windows specific, so those on
> Unix may not choose to fool with it.
> 
> I won't be at the meeting tonight.
> 
> 
> Mike
> 
> 
> #
> #
> # This perl script renames files in the 'C:/Copy2/'
> # directory and any subdirectories whose filename
> # exceeds the length specificied by $sizemax.
> #
> # It appends a 6 digit number to the renamed files
> # and outputs a list of the renamed files to the screen.
> # Just run it with no arguments.
> #
> #
> use English;
> use warnings;
> 
> print "\n";
> 
> sub search_dir
> {
>         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) ) {
>                 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");
>                         }
> 
> #                        push @files,($filename.$space." : ".$size) if
> ($sizecomp
> > $sizemax);
>                 } # ELSE
>         } # FOREACH
>         if ( 0 < @files ) {
>                 print "\n";
>                 foreach ( @files ) {
>                         print "$_\n";
>                 } # FOREACH
>         } # IF
>         foreach $entry ( @dirs_list ) {
>                 $dircount += &search_dir($dirname."\\".$entry);

You don't need the '&' anymore.

>         } # FOREACH;
>         return $dircount;
> } # end of search_dir
> 
> MAIN:
> {
> #        open DIROUT, ">C:/Copy2/README - File Listing.txt" or die "$0: open
> README - File Listing.txt: $!";
>         local ( $dirname , $count );
>         $dirname = 'C:/Copy2/';
> 
>         $sizemax = 60;
>         $sizemax += 4;
> 
>         $countnum = 0;
> 
> #        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";
>         print  "\n$count total directories\n";
>         exit 0;
> } # end of MAIN
> 
> 
> __END__
> 
> 
> "G. Wade Johnson" wrote:
> 
> > Just the monthly reminder about tomorrow's meeting. We'd love to have any
> > you willing(and able) to spend a couple of hours on Valentine's Day at
> > HAL-PC headquarters.
> >
> > G. Wade
> > --
> > "And so it begins"                                -- Ambassador Kosh
> > _______________________________________________
> > Houston mailing list
> > Houston at pm.org
> > http://mail.pm.org/mailman/listinfo/houston
> 
> _______________________________________________
> Houston mailing list
> Houston at pm.org
> http://mail.pm.org/mailman/listinfo/houston


-- 
Don't kill him!! If you kill him, he won't learn nothin'!
                                    -- The Riddler, "Batman Forever"


More information about the Houston mailing list