in reply to validating string length with a regular expression

Hmmm, that looks for 0-50 characters after the beginning of the string up to the end of the string, then it matches any character as many times as possible up to the last 'foo' in the string. So I guess that'll match your requirements (and testing seems to prove so).
I can't think of a way to nicely match both length *and* a string with one regex (but then again, I'm no regex ninja ;-), so perhaps this would suffice.
&do_stuff($str) if length($str) <= 50 and $str =~ /foo/;
For a far better explanation of the regex check out japhy's superb YAPE::Regex::Explain.
HTH

broquaint