Don't ask to ask, just ask | |
PerlMonks |
Re^6: Removing File Extensionsby tkil (Monk) |
on May 02, 2004 at 18:29 UTC ( [id://349842]=note: print w/replies, xml ) | Need Help?? |
Frankly, using a regex for this is even a little overkill. Ah, I fear I must disagree with you again. :) The simplest non-regex solution I can think of (where "simple" is some vague measure of how easy or difficult it is to comprehend what's going on) is this:
Which has its own potentially subtle problems (yay off-by-one errors!), but is as straightforward as it gets. (In particular, this is the sort of code that a perl novice would likely write - so if that is the level of user / maintenance programmer we're aiming for...) While the power and flexibility of regexes might not be used to their fullest in splitting a filename from its extension, the "template" of the operation is a very common and easily comprehended one:
That is the aspect of the regex solution that I find compelling: the regex is a bit hairy, but the structure it is embedded in is one of the core patterns in perl. And the difference between "Here's a core pattern, I can instantly see what's going on, now I just need to grok the regex" versus "Wait, what does that module do again? What does this parameter mean? What are the special cases? What happens if it doesn't match? What errors can it throw?"... that is the difference I was trying to highlight. Finally, I know this is all tradeoff; and different people have different thresholds where they'd draw the line. For me, I don't think that regexes are overkill: in perl, they are a first-class citizen, and an essential part of the programmer's vocabulary. (Heck, even your solutions use it, as the first argument to split -- which is still a regex, so you had better escape it. :-)
In Section
Seekers of Perl Wisdom
|
|