Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Re: Re: Re: Re: Re: Re: Code review: validation regexes

by bigj (Monk)
on Jul 10, 2003 at 08:23 UTC ( [id://272914]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Re: Re: Re: Code review: validation regexes
in thread Code review: validation regexes

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

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: Re: Re: Code review: validation regexes
by l2kashe (Deacon) on Jul 10, 2003 at 16:04 UTC
    Well spoken :)

    I also wasn't trying to offend, simply trying to extend the conversation a bit. Personally I think that threads like these will be beneficial for people passing through later, as they can see a few different sides of an issue and follow not only the technical reasoning, but also the stylistic reasoning for why this snippet is better in this case, over a different snippet.

    With that said, I was glad to chat with you, and I think I'm done with this thread ;).

    P.s: I've been thinking about the problem, and I think the way I would solve the particular issue of "reserved" usernames might be
    my @reserved = qw(administrator mail-admin spam-admin); die "Please choose another name\n" if ( grep(/^$username$/i, @reserved +) );
    A hash would also work, but then if you want $hash{$user}, you have to do your own manual case checking (or can force a particular casedness), or grep (/blah/i, keys %reserved), or possibly grep(/blah/, each %reserved)



    MMMMM... Chocolaty Perl Goodness.....

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://272914]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found