To find a subset of a set, having a specific sum, you can use this regex. It works for any set of non-negative integers.
sub subset_sum { my $targ = shift; my $re = join '', map '(' . '.' x $_ . ')?', @_; return map defined() ? length : (), (1 x $targ) =~ /^$re$/; }