Search code examples
perlundef

Initialising with "undef" for self-documenting code?


Sometimes I'd like to write:

my $var = shift // undef;    # argument is optional
                             # or

my $var = $val{optional_value} // undef;

to indicate that it is ok to have the argument missing, otherwise // undef is useless, of course.

How expensive is this, is it optimized away by the compiler?


Solution

  • To answer your questions: Not and no.