I had need for a new #perl operator today, like:
$val //~ s/$prefix//;
that would be equivalent to:
$val =~ s/$prefix// if defined $val;
is it too late to get this for v5.42 ?
I had need for a new #perl operator today, like:
$val //~ s/$prefix//;
that would be equivalent to:
$val =~ s/$prefix// if defined $val;
is it too late to get this for v5.42 ?
@Pyrrhlin @Perl
I think it would also be equivalent to:
{ no warnings 'unintitialized';
$val =~ s/$prefix// }
Or am I missing something here?
This seems more like a code smell to me. If you need to preprocess the content of the scalar, is it warranted for the scalar to still be potentially undef afterwards?
Beware: The seemingly similar '//=' operator does assign something only if the scalar is NOT defined. Your proposed operator would do something only if the scalar IS defined.
@Pyrrhlin I do it all the time for cheap regexp-based switches, as described in perlfaq7: https://perldoc.perl.org/perlfaq7#How-do-I-create-a-switch-or-case-statement?
```
SWITCH: for ($pizza) {
/pepperoni/ and do {say 'yum!'; last SWITCH};
/anchovies/ and do {say 'yuck!'; last SWITCH};
}
```
@Pyrrhlin I like to see the test first when reading from left to right.
Per #perlcritic I reserve postfix controls for program flow control functions like return, last, next, etc. https://metacpan.org/pod/Perl::Critic::Policy::ControlStructures::ProhibitPostfixControls