How on the Earth...
Давайте, не мудрствуя лукаво, рассмотрим пример кода, в котором немного не достаёт функционала одной проверки:
sub f {
my $refStr=\$_[0];
return if passed_as_constant($_[0]) and !defined(wantarray);
substr(${$refStr}, 0, 1)='D';
# return the copy of the string if it was passed
# to the function as constant aka "literally" or if copying was explicitly requested by the left-side context
(passed_as_constant($_[0]) or defined(wantarray)) and return ${$refStr};
}
# Argument was passed literally and function called without left-side context - we may simply "do nothing"
f('Bingo!');
# Argument was passed literally and left-side context is here. We must return a copy of literal/constant
say my $s=f('Bingo!');
# Here we can modify the $s "in-place"
f(my $s='Bingo!');
Собственно, вопрос: как (on the Earth) реализовать вот это самое passed_as_constant? :)
ОТВЕТ:
use Scalar::Util qw(readonly);
{ no strict 'refs'; *{'main::passed_as_constant'}=\&readonly; }