in reply to Re: Trouble Getting Local Variable $_ to return reformatted string
in thread Trouble Getting Local Variable $_ to return reformatted string

Thanks to jbert, grep and shmem for the excellent pointers and explanations.

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.

The simplest solution for me (based on the existing code and needing to add cleanup code elsewhere) was to let the CGI and HTML:TEMPLATE modules handle it.

Yep, shoulda thunk more 'bout not re-creating the wheel. :-)

THANKS!
  • Comment on Re^2: Trouble Getting Local Variable $_ to return reformatted string

Replies are listed 'Best First'.
Re^3: Trouble Getting Local Variable $_ to return reformatted string
by ysth (Canon) on Jun 18, 2007 at 18:32 UTC
    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
    # 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;
    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 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;