Re: mini-quiz
"Mike South" <[email protected]> Wed, 20 Jun 2007 05:13:42 -0400
| Newsgroups | gmane.comp.lang.perl.qotw.discuss |
|---|---|
| Message-ID | <[email protected]> |
On 6/19/07, Joshua Kronengold <[email protected]> wrote: > On Tue, 19 Jun 2007 12:50am, Ron Isaacson wrote: > > Joshua Kronengold wrote: > >> >> my $country = map +(/^(?:|gbr)$/ ? '' : uc),$card->country; > >> > >> [...] is much better. > > > Sorry, but you lost me there. I consider myself to be a pretty darn > > good perl programmer, and even I'd have to read that line a few times > > to understand its meaning. > > *blink* really? which part and why? The part where you are using map, which returns an array, and you are getting a scalar back which will always be 1? :) Of course I'm partly saying that just to be funny, but there are a lot of things that have to be right in your idiom: parens around the $country in my ($country) ^, $ anchors on the regex because you're really specifying values for the whole string parens on the regex so that you don't get alternation between the ^ zero-width assertion and gbr$ + in front of the () after the map so it doesn't look like map() , after the () after the map since it's the map EXPR, ARRAY form (Everything else aside, I would use the "map {} " form--is there a reason not to? All I see is two extra characters to type, but I'm interested to know if there's a good reason.) I do firmly believe in using idiom when it serves as both a mental and typographical shortcut. I'm not sure this is the best idiom for this situation. However, even trying to say something like "best idiom" is problematic, as idioms tend to localize. mike > Is some of this idiomatic > unfamiliarity? Would this help? > > my $country = map +(/^(?:|gbr)$/ ? '' : uc "[$_]") => > $card->country; > > How about: > my %ignored = map +($_ => undef) => ('', qw(gbr)): > > my $country = map +(exists $ignored{$_} > ? '' > : uc "[$_]") => $card->country; > > > if ($country eq 'gbr') { > > $country = ''; > > } elsif ($country ne '') { > > $country = uc "[$country]"; > > } > > > > I'm sure someone will find a way to clarify it even more, but to me, > > that's a one-pass read and the meaning smacks you over the head. > > Yes. But the same is true of my code, ignoring idiomatic unfamilarity. > > > You're honestly telling me this block would be HARDER for an expert > > programmer to maintain (i.e., tweak if the desired logic changes > > slightly) than your one-line version above? > ...