In the absence of the disgraced given I like to use for as a localizer. These are effectively aliases $_ or $_[0] , or a local copy depending on the value of the in_place hash element. It is directly comparable to your $ref , but with aliases and much cleaner.
I see no reason to return a useless undef / () if the string is changed in place; a routine can also return a new value for a string. (I suspect that the old value may be more useful after the $x++ mod, but that makes the code uglier!)
I'm not sure if this is readable code for anyone other than me, so comments are welcome!
use strict; use warnings; my $ss = 'abcabc'; printf "%s %s\n", mod_str($ss), $ss; $ss = 'abcabc'; printf "%s %s\n", mod_str($ss, in_place => 1), $ss; sub mod_str { my ($copy, %opt) = @_; for ( $opt{in_place} ? $_[0] : $copy ) { s/a/A/g;
Exit
AbcAbc abcabc AbcAbc AbcAbc
Borodin
source share