my $maxtries = 10; my $i = 1; while ($i <= $maxtries) { $mech->follow_link(text => "Hey Jude (lennon/mccartney)", n => "1"); last if $mech->success; $mech->back(); $i++; } $mech->success() or die "Can't find song page\n";