develooper Front page | perl.perl5.porters | Postings from September 2013

Re: [perl #119667] smartmatch example in perlop is broken

Thread Previous | Thread Next
From:
Leon Timmermans
Date:
September 8, 2013 15:22
Subject:
Re: [perl #119667] smartmatch example in perlop is broken
Message ID:
CAHhgV8gjjy3+ENZtM_F_otbxQ0e-8KFXwgSN6vuO+XmcL1wZZA@mail.gmail.com
On Sun, Sep 8, 2013 at 3:57 PM, Father Chrysostomos via RT <
perlbug-followup@perl.org> wrote:

> On Sun Sep 08 02:03:42 2013, mauke- wrote:
> > perldoc perlop:
> >
> > | or, if other non-required fields are allowed, use ARRAY ~~ HASH:
> > |
> > |     use v5.10.1;
> > |     sub make_dogtag {
> > |         state $REQUIRED_FIELDS = { name=>1, rank=>1, serial_num=>1
> >    };
> > |
> > |         my ($class, $init_fields) = @_;
> > |
> > |         die "Must supply (at least) name, rank, and serial number"
> > |             unless [keys %{$init_fields}] ~~ $REQUIRED_FIELDS;
> > |
> > |         ...
> > |     }
> >
> > But:
> >
> > | ARRAY     HASH       any ARRAY elements exist as HASH keys
> > |                like: grep { exists HASH->{$_} } ARRAY
> >
> >
> > So the make_dogtag example above doesn't check whether all of the
> >    required
> > fields (name, rank, serial_num) are present. Instead it checks whether
> >    at least
> > one of them is present (which is of course much less useful).
>
> We’ve always had that problem.  Any non-trivial example of smartmatch
> usually does something other than the author intended.
>

<shameless plug>

In Smart::Match, that would have just been «$init_fields ~~
hash_keys(contains(qw/name rank serial_num/))», which is both intuitive to
read and trivial to write even without knowing the details of smartmatch
semantics.

</shameless plug>

Leon

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About