confess("Parameter $n stinks") unless $param[$n] =~ /$goodRule[$n]/; #### sub buyWidgets { my $numWidgets = shift; my $supplier = shift; #validate parameters unless ($numWidgets =~ /^\d+$/) { carp("'$numWidgets' is not a valid number of widgets: purchase canceled\n"; return undef; } # Validate, and be flexible if possible. Accept both "ACME" and "hash(0xPtrToACME)", and even ["ACME", "Bob's Widgets", "Black Market"] if (ref($supplier) eq 'ARRAY') { my $buyCount = 0; foreach (@$supplier) { my $result = buyWidgets($numWidgets-$buyCount, $_); $buyCount += $result if defined($result); } return $buyCount; } if (ref($supplier) ne 'Supplier') { unless (exists $globalSupplierDirectory->{$supplier}) { carp("Cannot buy from '$supplier': not a valid supplier\n"); return undef; } $supplier = $globalSupplierDirectory->{$supplier}; } # Do stuff with the good parameters return $supplier->buyWidgets($numWidgets); }