سؤال

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

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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. :)

I think it is already pretty concise compared with most languages. I don't understand what you are hoping to achieve by making it even less legible

One thing that I would do is remove the defined, leaving

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

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top