Finding the last match before a first match

Question

So how would I find the last 'a' before the first 'foo'?

My latest attempt is:

$tmp = 'abcdefgabcdefgfooabcdefgfoo';
$tmp =~ m/(foo)/ogcs;
[do stuff with $1]   # this part works as I'd hoped
$tmp = substr($tmp, 0, pos($tmp));
$tmp =~ m/.*(a).+?$/os;

But that still got the first 'a'. Also, $tmp can be rather large so the substr is a bit distasteful.

Is there any way to search backward from the current pos() or something similar?

Next