Here's another solution, inspired by ysth's comment (if it is very long and the last foo is close to the beginning, which leads to a slow regexp): splitline in 'foo' and parse the last element for the number:
my @results = split /foo/, $string;
my ($digits) = ($results[-1] =~ m/^(\d+)/);
Again, I was always with the simplest code until it looked like the code was too long (and this was a problem in the general application), and then I would compare a number of solutions with typical inputs to see which is better.
source
share