my ( $just_name, $just_ext );
my $last_dot_pos = rindex $filename, '.';
if ( $last_dot_pos > -1 )
{
$just_name = substr $filename, 0, $last_dot_pos;
$just_ext = substr $filename, $last_dot_pos+1;
}
else
{
$just_name = $filename;
$just_ext = undef;
}
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:
- try a match;
- see if it worked;
- capture subexpressions if it did;
- complain if it didn't.
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. :-)
|