Syntactic Confectionery Delight | |
PerlMonks |
Re: Re: Re: Re: Re: Re: Re: Code review: validation regexesby bigj (Monk) |
on Jul 10, 2003 at 08:23 UTC ( [id://272914]=note: print w/replies, xml ) | Need Help?? |
With that said, I'd like to comment on your counterpoints. I didn't want to offense you, all I wanted to say is there are more than one way to do it, and they are all acceptable. If you don't use (?:..|..|..), do you also not use ?!, ?=, ?<, ?>, ?<=, ?>=, ?{}. Its a shame to give up the functionality provided by these idioms, simply due to the fact that they look funny. In fact, I like to use the non-capturing groups, positive/negative look aheads and behinds and closefisted regexp parts. I use them where they are implied by the algorithm. (In doubt, I choose the way with the least type strokes and the least reading effort). The ?: construct is needed when we want to capture some parts, and some other group parts are only needed for simple grouping. In addition, the ?: construct is important, if the regexp is reused in other regexps (e.g. build with the qr-operator). In the OP's case, he only wanted to find out whether a string is exactly one of some alternatives. In that case you need a group consisting of the alternatives and enclosed by the ^ and $. It doesn't matter whether it is captured or not and in doubt I would prefer the easier solution. The ?: solution is not worse, nor better as you wrote for this task. (It's perhaps only a bit quicker, but for the price of a longer and a bit more complicated script).In terms of my way of collecting a month versus your way. It appears that you didn't actually test the line to see what ends up in $mth, but rather assumed it was the numerical value, which it is not. To say the truth, I confound the list to the scalar context and I also confound the min of the doc as mon(th) :-( But please note, that it is also simple to get confused, as the list context is suggested by the (...)[1] part, allthough it comes from a split. Of course, it was my error, but as I think making errors is typical for humans - at least for me - thus I prefer to program as directly as possible. That involves, trying to express the algorithm without any indirection. A split call has nothing to do with time per se, the (from me called magical) 1 has nothing to do with months per se, it is only technical. Of course, it is a common idiom, but the algorithm is hided on the first glance.In contrast, my prefered solution my $month = strftime "%b", localtime; is at least not more cryptic, it's shorter and for a lot of typical date formats, there are handy, simple to understand shortcuts (like %d for day, %m for month (number), %y for year, %w for weekday(number) and so on). It is simple to change and often simple to understand, even without a manual. Don't misunderstand me, I don't want to say, that your style is wrong. Indeed, I like it also. But it is also a good style to program in another way and it is also quite full acceptable to advert readability and error preventing style. Greetings,Janek
In Section
Seekers of Perl Wisdom
|
|