if ($img =~ m#.*/(.*)#) { $name = $1; } else { # didn't match! } #### use Benchmark; timethese(1000000, { 'regexp' => sub { my($file) = $img =~ m#.*/(.*)# }, 'substr' => sub { my $file = substr $img, rindex($img, '/') + 1 } }); #### Benchmark: timing 1000000 iterations of regexp, substr... regexp: 11 secs (11.18 usr 0.00 sys = 11.18 cpu) substr: 4 secs ( 5.67 usr 0.00 sys = 5.67 cpu)