Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^6: Removing File Extensions

by tkil (Monk)
on May 02, 2004 at 18:29 UTC ( [id://349842]=note: print w/replies, xml ) Need Help??


in reply to Re: Re^4: Removing File Extensions
in thread Removing File Extensions

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:

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:

  1. try a match;
  2. see if it worked;
  3. capture subexpressions if it did;
  4. 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. :-)

Replies are listed 'Best First'.
Re: Re^6: Removing File Extensions
by dragonchild (Archbishop) on May 02, 2004 at 19:10 UTC
    You made my point for me, which was "Where do you draw the line in overkill?". Using a standard module to the extent it can be used is, very often, the difference between ease of porting and a month's rewrite (q.v. nodes to that effect on this site).

    We are in agreement on the standard template for a given operation, regex or not. The point I was trying to make is that, just as regexen are first-class citizens, so are CPAN modules, especially those modules that are in the core. Although 99% of those using File::Basename will never need to port to VMS, the 30-40% of us who work in the Win/*nix world are very happy to not have to worry about '/' vs. '\'. That's why I promote solutions that use the modules that are both in the core and in the list of commonly-accepted modules. (DBI, CGI::Application, HTML::Template, Template, *::Utils, Text::xSV, etc.)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://349842]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-25 12:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found