in reply to Re: Identifying Reverse Position of Specific Character in a String
in thread Identifying Reverse Position of Specific Character in a String

Use for (shift) instead of local $_ = shift to avoid bugs.

Replies are listed 'Best First'.
Re^3: Identifying Reverse Position of Specific Character in a String
by jdporter (Paladin) on Aug 25, 2006 at 19:56 UTC

    There's no benefit to that in this case, since the function makes no attempt to alter $_.

    Furthermore, using the for(shift) idiom with code that does modify $_ is dangerous, unless your intent is to modify $_[0].

    As for pos, one of the lessons you do (or should) learn early in your Perl edjication is to call pos as soon after the regex that sets it as possible — ideally, immediately after it. Otherwise, you're taking a gamble.

    We're building the house of the future together.
      since the function makes no attempt to alter $_.

      Odd, I see two things that modify it. An assignment, and the regexp alters the associated pos. The point of local is to protect the caller, but it doesn't necessarily do so in this situation.

        Point. Or two. ;-)