develooper Front page | perl.perl5.porters | Postings from October 2016

Re: Extend the fat comma to deal with undef

Thread Previous | Thread Next
From:
Paul "LeoNerd" Evans
Date:
October 6, 2016 21:53
Subject:
Re: Extend the fat comma to deal with undef
Message ID:
20161006225305.1c6577c9@shy.leonerd.org.uk
On Thu, 6 Oct 2016 22:20:51 +0100
Sam Kington <sam@illuminated.co.uk> wrote:

> Or turn to the CPAN and use the defensive programming from above:
> 
> use Hash::MoreUtils qw(slice_def);
> do_thing(
>     %{
>         slice_def(
>             {
>                thing => $thing,
>                this => $this // undef,
>                that => get_that_maybe() // undef,
>             }
>         )
>     }
> );

It's not *particularly* neat syntax, but if it helps 'pairgrep' from
List::Util has been around for a while now (and backports on CPAN work
to earlier perls):

  do_thing( pairgrep { defined $_->value }
     thing => $thing,
     this  => $this // undef,
     that  => get_that_maybe() // undef,
  )

Could always be neatened with

  sub definedvals { pairgrep { $_->value } @_ }

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

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