[Gllug] perl script

Simon Trimmer simon at urbanmyth.org
Thu Aug 22 09:52:09 UTC 2002


On Thu, 22 Aug 2002, Simon Stewart wrote:
> On Thu, Aug 22, 2002 at 09:53:54AM +0100, Tethys wrote:
> >
> > >opendir (DIR, '.');
> > >foreach ( grep {/\.jpeg$/} readdir DIR ) {
> > >    /^(.)(.)(.)(.)(.)(.*)$/;
> > >    rename ($_, "$4$5$3$1$2$6");
> > >}
> >
> > Proof positive once again that perl should be avoided wherever possible!
> > Compare the readability of that with:
> >
> > 	#!/bin/sh
> >
> > 	for i in *.jpeg
> > 	do
> > 		n1=`echo "$i" | cut -c1-2`
> > 		n2=`echo "$i" | cut -c3`
> > 		n3=`echo "$i" | cut -c4-5`
> > 		n4=`echo "$i" | cut -c6-`
> >
> > 		newname="${n3}${n2}${n1}${n4}"
> >
> > 		mv "$i" "$newname"
> > 	done
>
> Anyone who's used UNIX for longer than a week will have run into
> regular expressions. In this particular case, I really do think that
> the regex in the first script is perfectly readable (clunky, perhaps)
> whereas assigning variables using the "cut" command is the more
> obfuscated root. Why? Because the first script clearly says "match the
> default input, making a note of the following 6 locations, then rename
> it to this" which the second one doesn't.
>
> Also, on a pedantic note, it would be possible to alter the first
> script to cope with variable length digits more easily than the second
> (I'm thinking "s/^(\d+?)_(\d+?)(.*)/$2_$1$3/" but I've not tested
> this) Thinking about it, the minimum munch isn't necessary, but you
> might get some more speed out of the script that way.
>
> If this makes no sense, it could be because I've not had my early
> morning coffee yet.
>
> Cheers,
>
> Simon

Not wanting to disagree with another Simon, but you need more coffee :)

In this particular case I'm with Tet - it's a simple solution that you'd hope
a novice could understand that employs all our favourite traditional unixisms
like using small tools to do a job.

But, the mantra is "there is more than one way to do it"...they both get the
job done so we're just starting on the path to holy war, emacs vs vi anyone?

-Simon
Simon Trimmer <simon at urbanmyth.org>


-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list