in reply to Re: Re^4: Replace zero-width grouping?
in thread Replace zero-width grouping?

I'm not sure "code block assertion" is the proper name, actually. (Maybe the Camel has something to say on the matter; perlre doesn't.) I just made up a term that made clear what I was talking about.

Re "wish I'd thought of that": :) Remember the strategy for optimizing Perl code is to keep the execution of as much of an algorithm's logic as possible in the perl binary. The GRT is an impressive demonstration of this principle.

Makeshifts last the longest.

  • Comment on Re^6: Replace zero-width grouping? (optimizing perl code)

Replies are listed 'Best First'.
Re: Re^6: Replace zero-width grouping?
by diotalevi (Canon) on May 09, 2003 at 13:41 UTC

    "Assertion" is the wrong word here because unless you do gymnastics the code block has no affect on whether the expression succeeds or fails. On the rare occasion that I want to use perl code in an assertion (and this never happens for "real" code) you have to use the eval block in a conditional and then use zero-width assertions to simulate a true-false.

    /(? # Use the conditional construct (?{ perl code goes here}) # Perl code that will assert something (?=) # empty positive assertion. | (?!) # empty negative assertion )/x
      Why don't you use (??{ }) for that?

      Makeshifts last the longest.

        Huh. I wouldn't have thought to use (??{}) for that and still won't. It invokes the regex compiler which isn't necessary for the code2assert I posted. This works identically except its slower.

        /(??{ # delayed regex $condition ? '' # Nothing needed - this succeeds by default : '(?!)' # Failed! })/
Re: Re^6: Replace zero-width grouping?
by BrowserUk (Patriarch) on May 09, 2003 at 15:37 UTC

    History has it that Mr Wilde's response was, "You will Harvey, you will.". So, in this case, I guess I should simply say. "I will" :)

    In C  assert(true); could still be classed as an "assertion". The fact that the assertion is always true doesn't change that. perlre says

    This zero-width assertion evaluate any embedded Perl code. It always s +ucceeds,

    so 'code block assertion' as a phrase to describe (?{ ... }) makes a certain amount of sense, to me at least.

    And the (??{ ... }) is described as a "postponed regular subexpression".

    Both are a bit wordy, but it would be nice to have terms for them, rather than needing to constantly use the notation? Maybe CBA and PP-RE?? Just a thought.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

      FWIW, I tend to think of them as "RE eval" and "deferred eval", influenced in part by:

      perl -wle 'sub a { print +(caller)[1] } ""=~/(?{ a() })/'

      Hugo

        That's much better than either my long-winded versions or the aweful TLA's. It also reminds us of the runtime costs.

        Thanks. I'll update my lexicon:)


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller