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

Re: Extend the fat comma to deal with undef

Thread Previous | Thread Next
From:
Sam Kington
Date:
October 6, 2016 21:21
Subject:
Re: Extend the fat comma to deal with undef
Message ID:
0EBD7AB1-19D4-4B85-9B1A-789B90BF8FA0@illuminated.co.uk
Hi,

Thanks for the feedback, everyone! A few comments to follow, but I’d like to emphasise a point of mine which perhaps got missed, which is that this proposal is about making sure that the fat comma - in its revised form, at least - does more than just mean you don’t have to quote the lhs; it makes sure that the things you thought would be in pairs, actually were in pairs.

So the use of =>? or =>! (or whatever syntax we use - are ?=> or !=> better?) means “if this ends up being only one scalar, fix it”.

On 6 Oct 2016, at 11:54, Paul LeoNerd Evans <leonerd@leonerd.org.uk> wrote:
>>    optional(
>>        thing => $thing,
>>        this =>? $params{this},
>>        that =>? $params{that},
>>    );
> 
> That's what hash slices are for
> 
>    optional(
>       thing => $thing,
>       %{$params}{qw( this that )}
>    )

OK, but what if the variables aren’t already handily in a hash? I can’t use a hash slice if the naive code would normally have been e.g.

do_thing(
    thing => $thing,
    this => $this, # might be undef or blank
    that => get_that_maybe()
);

It’s one thing to program defensively and say e.g.

do_thing(
    thing => $thing,
    this => $this // undef,
    that => get_that_maybe()
);

if all I care about is that I end up with 6 elements in the list. It’s fiddly and annoying and it would be nice to have a shortcut, but it’s doable. It has the problem that if I add something after that, and forget to add a // undef after get_that_maybe() I might introduce a bug. But that’s something that can be fixed by code culture.

If the code I’m calling cares about whether the *key* exists, however, all bets are off. I can no longer inline this stuff in an anonymous list I pass to some other bit of code. I have to carefully unpick every part that should only be included sometimes, e.g.

my %thing_params = (thing => $thing);
$thing_params{this} = $this if $this;
if (my $that = get_that_maybe()) {
    $thing_params{that} = $that;
}
do_thing(%thing_params);

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,
            }
        )
    }
);

And that’s annoyingly messy. It would be a lot nicer if there was an infix operator that acted like the fat comma, but gobbled the lhs if the rhs was undef or missing:

do_thing(
    thing => $thing,
    this =>? $this,
    that =>? get_that_maybe(),
);

(Or, again, ?=> or something if =>? won’t work.)

And I think you might find that useful in other contexts as well, not just building hashes.

Sam
-- 
Website: http://www.illuminated.co.uk/


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