in reply to Re: Using $'
in thread appending to end of string

thanks.

Replies are listed 'Best First'.
Re: Re: Re: Using $'
by jc23 (Acolyte) on Jun 25, 2003 at 21:38 UTC
    replace $1 with $& and it works. Thanks!

      PLEASE DON'T USE $&! Take a look at perldoc perlvar for $&. For why $& is generally consider to be a Bad Thing™, check out the documentation for Devel::SawAmpersand. The important parts:

      There's a global variable in the perl source, called sawampersand. It gets set to true [..whenever..] the parser sees one of $`, $', and $&. It never can be set to false again.

      If the global variable sawampersand is set to true, all subsequent RE operations will be accompanied by massive in-memory copying, because there is nobody in the perl source who could predict, when the (necessary) copy for the ampersand family will be needed. So all subsequent REs (even in places outside of scope) considerably slower than necessary.

      That page even offers helpful ways that you can avoid the $&, $`, and $' variables.

      Update: Proof of concept since my buddy didn't believe me:

      antirice    
      The first rule of Perl club is - use Perl
      The
      ith rule of Perl club is - follow rule i - 1 for i > 1