Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Short syntax for call function if defined

is there a shorter syntax or operator for

defined $functionpointer ? $functionpointer->($value) : $value

i would like to have sth like the //-Operator, so that I can shortly write

$functionpointer //->() $value

or anything in that direction

what I don't want to do is write an extra method, overload operators or so

like image 543
Hachi Avatar asked Feb 20 '23 07:02

Hachi


2 Answers

No, there is none. There are discussion, though, about introducing it: What operator should p5p use for safe dereferencing at PerlMonks.

like image 117
choroba Avatar answered Feb 26 '23 22:02

choroba


You can replace the $functionpointer by an anonymous constant function that returns your default value like this (tested in 5.12.1):

($functionpointer // sub {$default})->(@args)

It's a little hackish, but it works. :)

like image 20
memowe Avatar answered Feb 26 '23 21:02

memowe