http://qs321.pair.com?node_id=285107


in reply to Re: file name parsing to get the original file name
in thread file name parsing to get the original file name

If we are trying for 'best UNIX-only solution that requires no modules', I vote for:

my($name) = $path =~ /([^\/]+)\z/;

I second Abigail-II's suggestion that a module is used, though, as these sorts of problems are generic in nature, and it is very scary to see hundreds of different solutions to the same problem, each with their own independent set of failings.

At least if a single module is used by everybody, then the code is being excercised in a higher percentage of the possible contexts, and problems will be fixed sooner, rather than being discovered much later.

UPDATE: Optimizing the above expression, we can see the speed improve by a factor of 6:

$path =~ /(?:.*\/)?(.+)/s; my $name = $1;

It seems that the Perl regular expression engine does a poor job of dealing with matching a pattern at the end of a string. This is not surprising given that most regular expression engines start searching from the beginning of the string.