in reply to Question regarding variable scope

Another option is to move the return statement up to the loop, where $black_player has been localized. Then you can explicitly return undef on a failed match like the function comments state. Here's my slightly condensed (and untested) rewrite:

sub find_paring() { my $wp = $_[0]; foreach my $bp (&shuffle keys %{$matches_remaining{$wp}}) { if (exists $matches_remaining{$bp}{$wp}) { delete $matches_remaining{$wp}{$bp}; delete $matches_remaining{$bp}{$wp}; return ($wp, $bp); } } # Else return undef undef; }