> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, September 13, 2001 8:09 PM
> To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
> Subject: Re: to remove the file extension
> 
> 
> >>>>> "Jason" == Jason Tiller <[EMAIL PROTECTED]> writes:
> 
> Bob> perl -e '-f $_ || print "Missing $_\n" for map 
> {/^(.*)\.c$/; "$1.out"}
> Bob> @ARGV' *.c
> 
> >> No.  You've used $1 without verifying the match matched (in which
> >> case it would get the PREVIOUS match).  For example, I can touch
> >> "foo\nbar.c" and run your script, and you'll get the wrong answer.
> 
> Jason> What's wrong with this?  Note that he's passing the 
> *.c glob, so you'd
> Jason> expect to only find .c files in @ARGV, right?  And if 
> there're no *.c
> Jason> files in the current directory, then the script just 
> prints nothing.
> Jason> Isn't that the correct behavior?
> 
> He's matching /^.*\.c$/, which will fail to match 
> "foo\nbar.c", although
> the glob will happily return that.  Therefore, yes, there is precise
> specific input that will fail this.
> 
> And in general, this is a bad idea anyway, hence my next rule:
> 
> >> IMPORTANT RULE - Never ever ever use $1 except in a conditional
> >> context!!!
> 
> Jason> That makes sense if you don't feel completely 
> confident about your
> Jason> inputs... but all the time?
> 
> Yes.  All the time.  As you can see, there's confidence that both you
> and he have in the input, and both of you have *unjustified*
> confidence, since I gave a counterexample that would break it.
> 
> Therefore.  ALL THE TIME.  Program defensively.

Point well taken.

Here's a different approach that I think avoids that defect:

   perl -e 's/c$/out/, -e $_ || print "Missing $_\n" for grep /\.c$/, @ARGV'
*

This has the advantage of accepting non-.c filenames as input, which are
silently ignored. However, I guess I am still assuming the substition will
always succeed, which is dependent on the form of the second regex (although

not on the input data, as far as I can tell. i.e. any file that makes it
through
the grep *will* be properly substituted, right?)

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to