[Melbourne-pm] Regexps - how does the lexical scope of capture buffers work? (Was: Regexp: What's the right way to do this?)

Nathan Bailey nathan.bailey at monash.edu
Wed Oct 17 22:59:02 PDT 2012

On 18/10/2012, at 4:23 PM, Michael G Schwern wrote:
> On 2012.10.17 4:33 PM, Nathan Bailey wrote:
> I'm not sure what you mean by "regexp evaluations short circuit on failure".

As I understand it, the 'c' in the below regular expression never gets evaluated:
	if ("aa" =~ /bc/) { ...

> I'm going to assume you're asking why when you run this code...
>    if( $foo = bar() ) {
> And then there's side effects, printing to the screen, setting global
> variables, network, disk and database access... how do you control them?  I
> don't even think STE can account for that.

Thank-you, that's actually a really good answer - if the if statement includes some major side effect, it's not reasonable to expect that it could be undone on failure, and it is reasonable to expect that someone might want to record that failure, separate from the execution of the subsequent block of code.

> The regex would return a match object you could get information out of.
>    # something like this
>    if( my $match = $string =~ /foo (.*?) bar/ ) {
>        print $match->capture(1);
>    }

Interesting. That has a certain elegance to it. Maybe we should hassle Damian :-)

>> I was wondering if there was a deep fu way that I hadn't considered.
> Use a p--... oh nevermind. :P

parser? I suspect Peter's Parse::RecDescent suggestion is actually the generic answer to my question (of which HTML::{TreeBuilder,TokeParser} and its cousins are a specific case for HTML). Beyond a certain point of regexp fu, you have to look at the document rather than the line.

> -- 
> Anyway, last I saw him, the TPF goons were pouring concrete around him,
> leaving only one hole each for air, tea, and power.  No ethernet,
> because he's using git.
>    -- Eric Wilhelm on one of my disappearances

There seems to be a certain lack of output capacity in this model? (and I'm not referring to the code :-)

More information about the Melbourne-pm mailing list