[Gllug] Poor scripting?!

Henrik Morsing henrik at morsing.cc
Wed Mar 12 07:59:10 UTC 2008


On Tue, Mar 11, 2008 at 03:57:27PM +0000, Andrew Black - lists wrote:
> Andrew Farnsworth wrote:
> > Henrik,
> >   This won't work, mainly because the perl string operator 'eq' does not 
> > recognize wild cards (i.e. the * character).  You will need to do a 
> > regex match to determine if ($_ =~ /cerprod$/) which will match the 
> > string 'cerprod' at the end of the string contained within the $_ variable.
> 
> Which can more succinctly be written as
>     if ( /cerprod$/ )
> 
> This is because $_ is the default operand to a match.
> 
> 
>  > $regexp = shift @ARGV
> Would normally be written
>    $regexp = shift
> 
> It is a usage that is (IMHO) so common in Perl that I wouldn't stop to 
> think that it is looking at @ARGV
> 

Thanks, I've come up with this now:

#!/usr/local/bin/perl
#
# Usage: rename perlexpr perlexpr2 dir

use File::Find;

($regexp = shift) || die "Usage:  rename perlexpr perlexpr2 directory\n";
($regexp2 = shift) || die "Usage:  rename perlexpr perlexpr2 directory\n";
($dir = shift) || die "Usage:  rename perlexpr perlexpr2 directory\n";

finddepth(sub { if ($_ eq /cerprod$/) {
                   $old_name = $_;
                   eval $regexp;
                   die $@ if $@;
                   rename($old_name, $_) unless $old_name eq $_;
        } elseif ($_ eq /medprod$/) {
                   $old_name = $_;
                   eval $regexp;
                   die $@ if $@;
                   rename($old_name, $_) unless $old_name eq $_;
        } },
        $dir);

}

exit(0);


Hopefully that will work?

Regards,
Henrik Morsing

-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list