in reply to Last undefines a for loop's itererator?

Indeed, as has been mentioned:

use List::Util qw( first ); my $picture = first { /$match/ } sort @pictures;

Without List::Util, I’d use a while:

my $picture = do { my @candidate = sort @pictures; shift @candidate while @candidate and $candidate[0] !~ /$match/; shift @candidate; };

After all, you need a temporary array to store the sorted list anyway – so you can afford to iterate it destructively, and thus you get to use while( @list ), which lets you get away without much synthetic index fiddling.

Update: fixed wrong sigil on 2nd shift; thanks, Tanktalus.

Makeshifts last the longest.