http://www.perlmonks.org?node_id=590753


in reply to File::Basename is dog slow

I assume you're talking about this code (from parsefile):
if (@suffices) { $tail = ''; foreach $suffix (@suffices) { my $pat = ($igncase ? '(?i)' : '') . "($suffix)\$"; if ($basename =~ s/$pat//s) { $taint .= substr($suffix,0,0); $tail = $1 . $tail; } } }
It recompiles a regex for every suffix on every call to parsefile. Yuck! This module could use another function that saves a regex to do the suffix checking (in a closure or an object). Or something :-)

Replies are listed 'Best First'.
Re^2: File::Basename is dog slow
by tye (Sage) on Dec 19, 2006 at 20:35 UTC

    How about just defaulting to stripping s{[.]([^./\\]*)$}{} (slightly more portably) instead of having to pass in a huge (and likely incomplete) list of possible extensions. That interface never made much sense to me, and hence I don't use it (and I'm not surprised that it is slow). I'd say, if it hurts, stop doing it (use something other than File::BaseName for stripping extensions, such as that regex I show above, which is portable enough for most work, likely). (:

    - tye        

Re^2: File::Basename is dog slow
by petdance (Parson) on Dec 20, 2006 at 06:03 UTC
    Might be. I didn't look into it. I just wanted to avoid it.

    xoxo,
    Andy

      You could keep File::Basename and just stop passing in a list of extensions. Just replace that part of the functionality. Stripping extensions is the part of Basename's parsing that is easy to replace portably, since it is just stripping the list of things you told it to. Keeping the added portability of the rest of the parsing is probably worthwhile and probably isn't dog slow. (:

      - tye