[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] hash slice silliness



jeffp@crusoe.net wrote
JP> I'd like some more ideas for idioms.


Actually, if you're looking for ideas, then you probably
aren't looking for idioms. Maybe you're looking for clever
tricks or something... but if it's really an idiom, then
fluent folks should already be speaking it.


JP> Submissions I incorporate will be acknowledged.


feel free to use what I provide below, but please do not
provide any acknowledgment... I'm sure lots of folks use
something similar and I'd rather not "claim credit" for what
was just as much their idea as mine.


uri@sysarch.com wrote
UG> check out my hash slice tutorial


Slice Tutorial
at http://www.deja.com/getdoc.xp?AN=475543324&fmt=text
ST> think of others as an exercise and report them back


So, have you got a nice colection hash slice code fragments
now? :)


I find hash slices to be fun when doing data validation
(generally in a cgi context in my case). The following
example stands out in my mind because I got to use the
first rule of real estate in a hash slice :)

(hope the phrase "real estate" doesn't trigger too many
spam filters out there)

The code went roughly like what I've got below. When a
field was required, but I wasn't very picky about what
the user filled in, I just checked for /\w+/... and as
there were multiple fields of this type:

    my %validate = (
        'email'     => sub { return &valid_em($_[0])
                ? [] : ['Helpful error message here.'];
        },
        'phone'     => sub {
            return &valid_ph($_[0])
                ? [] : ['Helpful error message here.'];
        },
        # ...
        # additional specfic data validation subs refs
        # ...
        'location'  => sub { return $_[0] =~ m/\w+/
                ? [] : ['This field must be filled in.'];
        },
    );
    @validate{qw(firstname lastname explanation)}
        = @validate{qw(location location location)}; # :)
    @errors{@fields}
        = map { $validate{$_}->($input{$_}) }  @fields;


I couldn't help it... was smiling to myself for the rest of
the day. Yeah yeah... "get a life" and all that ;)

-matt



==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe