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

Here's the gotcha:
# Put in HTML-friendly characters for any odd characters &ncw_com_library::cleanup($attr_descr);

You expect your cleanup() subroutine to do inplace edits of its argument, instead it takes a copy and returns it modified. Your $attr_descr doesn't get altered.

Don't use the & prefix for sub calls, unless you know what it does and you need that because you do know ;-)

# Put in HTML-friendly characters for any odd characters $attr_descr = ncw_com_library::cleanup($attr_descr);

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: Trouble Getting Local Variable $_ to return reformatted string
by Anonymous Monk on Jun 18, 2007 at 14:56 UTC
    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!
      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;