develooper Front page | perl.perl5.porters | Postings from July 2021

Elevator pitch - "assuming"

Thread Next
Veesh Goldman
July 27, 2021 23:35
Elevator pitch - "assuming"
Message ID:
I frequently find myself writing lines of code like the following

$row->{nested}{key} = mutate_it($row->{nested}{key}) if $row->{nested}{key}

And I find it annoying that I have to keep repeating the name of the
variable. The line processes a value if it's truthy, and leaving it falsy

it would be nice if we had a conditional statement that would also
topicalize, so that it could be cut down to
$_ = mutate_it($_) assuming $row->{nested}{key}

Open questions that come to mind is what conditions other than truthiness
would look like. Something like assuming $row->{nested}{key} : exists $_

In terms of what the argument to assuming would be, I guess any lvalue
would make sense.

Existing options:
There is a &&= operator, but it would still require duplication of lengthy

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About