Granted, I just modified some Perl 6 I had previously written, but it was exciting. So what does your new code look like?#!/usr/bin/perl use feature qw/say state switch/; use strict; use warnings; sub gen_cashier { my ($start) = @_; state $cash_in_store = $start // 0; return sub { my ($method, $amount) = @_; given ($method) { when (undef) { warn "Method name required\n" } when ('add') { $cash_in_store += $amount } when ('del') { $cash_in_store -= $amount } when ('bal') { return $cash_in_store } when (/^(?<name>[a-zA-Z]+)$/) { warn "$+{name} unimplement +ed\n" } default { warn "$method is illegal method name\n" } } }; } my @drawer = map gen_cashier, 1..4; $drawer[0]->(add => 59); $drawer[1]->(del => 17); $drawer[2]->(steal => 20); say $drawer[3]->('bal');
Cheers - L~R
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Bring Out Your New Perl Code
by grinder (Bishop) on Dec 19, 2007 at 07:29 UTC | |
by ambrus (Abbot) on Dec 19, 2007 at 15:23 UTC | |
|
Re: Bring Out Your New Perl Code
by zby (Vicar) on Dec 19, 2007 at 09:27 UTC | |
by grinder (Bishop) on Dec 19, 2007 at 14:15 UTC | |
by sgt (Deacon) on Dec 20, 2007 at 00:13 UTC | |
by pfaut (Priest) on Dec 19, 2007 at 11:15 UTC | |
|
Re: Bring Out Your New Perl Code
by ambrus (Abbot) on Dec 19, 2007 at 15:17 UTC | |
|
Re: Bring Out Your New Perl Code (humour)
by blazar (Canon) on Dec 29, 2007 at 23:07 UTC |