NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.
All the Perl that's Practical to Extract and Report
Stories, comments, journals, and other submissions on use Perl; are Copyright 1998-2006, their respective owners.
define it (Score:2)
That depends... (Score:1)
Ordinary morality is for ordinary people. -- Aleister Crowley
Something like this (Score:2)
Re: (Score:1)
Yuck. Using a pattern match to test string equality is smelly.
Re: (Score:2)
One is ugly syntax, which I find a silly argument. All Perl code is ugly, but elegance is more important than beauty. And I think this is very elegant.
The other is that you can only have 2_501_710 such matches per second (on my PM 1,5 GHz laptop, the average of 50% matches and 50% non-matches), which can be a valid argument in some cases. But if performance is the issue here, the method call should go first: my laptop can do just 1_415_988 method calls per second, and that's on an
Re: (Score:1)
I am honestly somewhat alarmed that you could only come up with straw men. How about “a regex match always has many more subtleties and edge/corner cases than straight string comparison”?
As soon as I saw your code I wondered: “Did he mean
^GBR$or was the omission of anchors deliberate?” And next: “Hmm, he uppercased it but there’s no/i– was that on purpose?”I prefer to minimise the number of speed bumps that a casual reader of the code has to pass.
Re: (Score:2)
if I know $lang will always be 2 characters. Here, I didn't know, and should have used anchors.
The case sensitivity has nothing to do with regexes. With $foo eq 'GBR' you can (should) ask yourself the same question: shouldn't uc($foo) eq 'GBR' be used instead? However this is somewhat irrelevant here, as the line just before the match, an
Re: (Score:1)
I’m not advocating baby-talk code like that.
Note that neither of the variants you suggest preserves undefs/zeroes. For that case, I gave a much simpler version that folds both conditions into a single ternary.
I don’t see how putting two conditions inside a regex as alternation is abstraction. You don’t have two Perl expressions, but instead a regex with two match possibilities. The complexity hasn’t gone anywhere, it’s still there right in front of the reader’s eyes,
Re: (Score:2)
The complexity is still there entirely, but written in a more compact way: there's only one character in between. This makes it easier --for me-- to notice that in practice, both conditions lead to the same thing.
I don
Re: (Score:1)
OK, being alarmed is hyperbole... to an extent. This particular example does not warrant it, but on general principle I *would* be worried if the only reasons you could come up with for preferring string comparison over regexes are performance and ugly syntax.
Compare:
With:
Regardless of how you turn it,
probably not like this (Score:1)
(my $country = $card->country || '') =~ s/^(?!gbr)(.+)$/\U$1/;Much like barbie… (Score:1)
… only I’d write the conditional the other way around:
Re: (Score:1)
Oops, that’s not actually the same. Well, it might be OK; if zeroes and undefs are not valid values that you need to care about, then that is how I’d write it.
If you do need to distinguish, though, then I’d put it this way:
Re: (Score:2)
Re: (Score:1)
Did you miss the part where I said this complex solution is only necessary if undef and zero need to be preserved? Sure this one is complex – because a three-way condition is always complex.
Re: (Score:2)
(I swapped the uc and gbr check around, because it makes the code prettier IMO.)
Though what's really needed in this code, regardless of the Perl syntax used, is a comment stating its intention. Perhaps:
Re: (Score:2)
As for comments: they provide redundancy. When the comment and the code are no longer in sync, that's a good sign something's wrong, and that helps to write good code.
where "use warnings" burns (Score:2)
My guess is that sometimes, $country is undef. And because it might be, the author writes horribly perverted code just to avoid getting an irrelevant warning message. This is the downside of warnings.
And given that, I'd have written this code as:
$country = $country eq 'gbr' ? '' : uc $country;See how much clearer it is when you stop worrying about stupid warnings? Don't be pedantic about warnings. Stop using them when they make your code look ugly. (/end rant).Re: (Score:1)
Re:where “use warnings” burns (Score:1)
If it’s really just undef, then an
or ''in the appropriate spot would suffice – no need for convolution.That said, almost all my code starts like so:
Re: (Score:2)
Sometimes you refer to a variable in another file:
That snippet will prevent a DOS on your server from folks repeatedly uploading files. However, it's probably only in your code once and warnings will issue a warning about it which you can disable with no warnings 'once'.
It's designed, however, for cases where you declare something and never use it again. That's probably cruft that you don't actually want in your code (or worse, you declare something but refer to it by the
Oh yeah – I know! (Score:1)
SCNR… :-)
Re: (Score:1)
Keep it simple.
Re: (Score:1)
Except that this will cause warnings when
$countrystarts out undefined.Refactor (Score:2)