Short syntax for call function, if defined

there is a shorter syntax or statement for

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

I would like to have sth like // -Operator so that I can write briefly

 $functionpointer //->() $value 

or anything in that direction

what I do not want to do is write an additional method, overload operators, or so

+4
source share
3 answers

No no. However, there is a discussion about its implementation: Which operator should use p5p for secure dereferencing in PerlMonks.

+3
source

You can replace $functionpointer with an anonymous constant function that returns a default value similar to this (verified in 5.12.1):

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

This is a bit hacky, but it works. :)

+3
source

I think this is already quite concise compared to most languages. I don’t understand what you hope to achieve by making it even less legible.

One thing I would do is delete defined , leaving

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

as if $functionpointer was defined and is a valid subroutine reference, it will always be true

+2
source

Source: https://habr.com/ru/post/1432231/


All Articles