I had looked at the various ways to call the sub (do I put in & or leave it out), but didn't think in this case it mattered.It didn't matter. I'm not sure why that was pointed out to you. The basic thing is that, to modify a passed parameter, you need to do it via @_, like
Or you need to not try to modify the parameter, but instead return it, and have the caller call the sub appropriately:# surround all uppercase characters in the passed parameter with *'s # and all lowercase characters with _'s # (modifies parameter in-place) sub transmogrify { my $string = $_[0]; $string =~ s/([A-Z])/*$1*/g; $string =~ s/([a-z])/_$1_/g; $_[0] = $string; return; } $text = "I met this guy, and he looked like he might have been a hat c +heck clerk at an ice rink, which in fact, he turned out to be. Let X + = X."; transmogrify($text); print $text;
# surround all uppercase characters in the passed parameter with *'s # and all lowercase characters with _'s sub transmogrify { my $string = $_[0]; $string =~ s/([A-Z])/*$1*/g; $string =~ s/([a-z])/_$1_/g; return $string; } $text = "I met this guy, and he looked like he might have been a hat c +heck clerk at an ice rink, which in fact, he turned out to be. Let X + = X."; $text = transmogrify($text); print $text;
In reply to Re^3: Trouble Getting Local Variable $_ to return reformatted string
by ysth
in thread Trouble Getting Local Variable $_ to return reformatted string
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |