The stupid question is the question not asked | |
PerlMonks |
Re: rename fileby kcott (Archbishop) |
on Aug 25, 2020 at 11:06 UTC ( [id://11121076]=note: print w/replies, xml ) | Need Help?? |
G'day frank1, Welcome to the Monastery. Firstly, I think you should define exactly what you mean by extension. For instance, is the extension of archive.tar.gz, .tar.gz or tar.gz or .gz or gz? As you've already loaded File::Basename, you could use its fileparse() function. This function can be tricky; in particular, the @suffixes argument is a list of patterns. Consider these:
And the order of the patterns matters. Consider these:
If all you want is the final part after the last '.', or a zero-length string if no such part exists, you can avoid the overhead, and possible frustration, of getting the patterns correct and in the right order, with simple functions which operate on strings (these are typically much faster than regexes but, in the scenario you present, I doubt that will matter much, if at all). This expression will do that for you:
And here it is in action, with an assortment of filenames with zero or more '.'s and with and without an actual extension.
— Ken
In Section
Seekers of Perl Wisdom
|
|