[oak perl] test

Belden Lyman blyman at iii.com
Mon Jan 31 10:47:52 PST 2005


On Sun, 2005-01-30 at 15:18, Sandy Santra wrote:
> George said there might be problems with the list, so this is a test post.
> 
> And here's a snippet of code I wrote recently that I'm very proud of 
> (considering I haven't even yet finished the regex chapters in the camel 
> book); and yes, I did crib some of it from the camel book.  I know it needs 
> improvement--any suggestions welcome.

It's helpful (but not required) to put

   use strict;
   use warnings;

at the top of your script. "strict" will enforce good programming
behavior, and "warnings" will catch many easy mistakes (mis-spelled
variables, etc.).

One of the "good behaviors" that strict will enforce is the scoping
of your variables, for example:

   foreach my $file ( glob '*' ) {
       my $newfile = $file ;

       # ... everything else as before
   }

The camel book no doubt talks about the importance of scoping.

>  (Like how to look over and over 
> again in each file/directory name for the textstring in case it appears 
> more times than one...)
> 

Read up on the /g modifier... covered a bit further in your chapter,
no doubt.

You'll be able to handle

   foo(1).txt
   bar(1)(1)(1).txt

very easily.

   baz(1(1(1))).txt

will be more difficult to match. (If you've got this type of data,
look into using Regexp::Common, available from CPAN.)

Belden

> Happy new year everyone!
> 
> BEGIN PERL SCRIPT:
> 
> #find [text string] and delete it from all file and directory names
> 
> chdir "c:/temp" or die "cannot chdir to that directory: $!";
> foreach $file (glob "*") {
>          $newfile = $file;
>          $newfile =~ s/\(1\)//;
>          if (-e $newfile) {
>                  ## warn "can't rename $file to $newfile: $newfile exists\n";
>          } elsif (rename $file, $newfile) {
>                  ## success, do nothing
>          } else {
>                  warn "rename $file to $newfile failed: $!\n";
>          }
> }
> 
> 
> 
> --Sandy Santra
> 
> _______________________________________________
> Oakland mailing list
> Oakland at pm.org
> http://mail.pm.org/mailman/listinfo/oakland



More information about the Oakland mailing list