in reply to In Search of a Smarter Sub

at first glance the two if-statement blocks differ only in the constants "red" and "blue".

You can factor them out in a common sub:

sub good_sub_name_here { my ($color, $other_color) = @_; if ($old_color eq $color) { if ($_ == 1) { alert(1); } elsif ($button[$_-1]->cget(-background) eq $color) { alert(2); } elsif ($_ >= 2 && $button[$_-1]->cget(-background) eq $other_col +or && $button[$_-2]->c +get(-background) ne "white") { alert(3); } elsif ($button[$_-1]->cget(-background) eq "white") { make_color ($color, $_-1); make_blank ($_); check_win(); } else { make_color ($color, $_-2); make_blank ($_); check_win(); } } } # and call it: good_sub_name_here(qw(blue red)) good_sub_name_here(qw(red blue))
If you declare the sub in the place where the if blocks are now, you shouldn't have any scoping issues