in reply to Re: Style question: regex versus string builtin function
in thread Style question: regex versus string builtin function

there are some issues about using $`, check perlre.
what do you want is m// then pos, this will be faster.

Oha

update: check the tye's note below

  • Comment on Re^2: Style question: regex versus string builtin function

Replies are listed 'Best First'.
Re^3: Style question: regex versus string builtin function (pos)
by tye (Sage) on Oct 02, 2007 at 13:53 UTC

    Make that m//g (note the 'g') in a scalar context and then pos.

    - tye        

Re^3: Style question: regex versus string builtin function
by lima1 (Curate) on Oct 02, 2007 at 13:17 UTC
    Well, you must be careful when you use match variables, especially when you work with big strings. But they aren't slow per se:

    Update: Thank you all for your comments and suggestions (here and in the CB)! See How do I get what is to the left of my match? for an updated benchmark and better explanations.

      You seem to have missed the point. The problem with $`, $&, $' is not that it slows down a regex, it's that it slows down *all* regexs that have no captures, say like the one in matchcontext. Your Benchmark is useless.

        No, it is not useless. Comment out the match variables in matchvars. It is not really "considerable slower" with that small strings. I just wanted to say that match variables are not as evil as everybody says and that they CAN speed things significantly up (like 500% in the benchmark). But you must be careful.

      Well, you must be careful when you use match variables, especially when you work with big strings. But they aren't slow per se
      From the Devel::SawAmpersand docs:

      There's a global variable in the perl source, called PL_sawampersand. It gets set to true in that moment in which the parser sees one of $`, $', and $&. It never can be set to false again. Trying to set it to false breaks the handling of the $`, $&, and $' completely.

      If the global variable PL_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 are considerable slower than necessary.