given ($payment_method) { # All code samples are Perl 6
when 'credit' { do_credit_card($num, $exp) }
when 'debit' { do_debit_card($num, $exp) }
when 'check' { do_check() }
default { die }
}
####
class MyStore::PayMethod {
# A submethod is a method that isn't inherited.
submethod new { die "Create a subclass, silly." }
method authorize($price) { ... }
method execute($price) { ... }
method store($dbh) { ... }
}
class MyStore::PayMethod::CreditCard is MyStore::PayMethod {
# Colon means private--only code in this class can see
# it.
has ($:ccnum, $:ccexp, $:ccname);
submethod BUILD($:ccname, $:ccnum, $:ccexp) {}
method authorize($price) { (code to authorize) }
method execute($price) { (code to execute) }
method store($dbh) { (code to store) }
}
##
##
my $class;
given($payment_method) {
when 'credit' { $class=MyStore::PayMethod::CreditCard }
when 'debit' { $class=MyStore::PayMethod::DebitCard }
when 'check' { $class=MyStore::PayMethod::Check }
}
my $payobj=$class.new(*%params);