Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: ${^MATCH} regex special variable(s) and /p regex modifier

by JavaFan (Canon)
on Oct 04, 2011 at 07:03 UTC ( #929457=note: print w/ replies, xml ) Need Help??


in reply to ${^MATCH} regex special variable(s) and /p regex modifier

That's because you are using parenthesis. Using parens mays you're paying "the penalty" (for that match) anyway. So, you get ${^PREMATCH}, ${^MATCH}, and ${^POSTMATCH} for "free".

Try running your match without parens, and see the difference.


Comment on Re: ${^MATCH} regex special variable(s) and /p regex modifier
Replies are listed 'Best First'.
Re^2: ${^MATCH} regex special variable(s) and /p regex modifier
by lidden (Curate) on Oct 04, 2011 at 10:33 UTC
    Try running your match without parens, and see the difference.
    I tried without parens and saw no difference. I tried with perl 5.14.1 and 5.12.2.
      With 5.12.2:
      $ perl -wE 'my $s = "foo bar"; $s =~ /(bar)/; say ${^PREMATCH}' foo $ perl -wE 'my $s = "foo bar"; $s =~ /bar/; say ${^PREMATCH}' Use of uninitialized value $^PREMATCH in say at -e line 1. $
      Note also the difference in what's going on:
      $ perl -Mre=debug -wE 'my $s = "foo bar"; $s =~ /bar/;' Compiling REx "bar" Final program: 1: EXACT <bar> (3) 3: END (0) anchored "bar" at 0 (checking anchored isall) minlen 3 Guessing start of match in sv for REx "bar" against "foo bar" Found anchored substr "bar" at offset 4... Starting position does not contradict /^/m... Guessed: match at offset 4 Freeing REx: "bar" $ perl -Mre=debug -wE 'my $s = "foo bar"; $s =~ /(bar)/;' Compiling REx "(bar)" Final program: 1: OPEN1 (3) 3: EXACT <bar> (5) 5: CLOSE1 (7) 7: END (0) anchored "bar" at 0 (checking anchored) minlen 3 Guessing start of match in sv for REx "(bar)" against "foo bar" Found anchored substr "bar" at offset 4... Starting position does not contradict /^/m... Guessed: match at offset 4 Matching REx "(bar)" against "bar" 4 <foo > <bar> | 1:OPEN1(3) 4 <foo > <bar> | 3:EXACT <bar>(5) 7 <foo bar> <> | 5:CLOSE1(7) 7 <foo bar> <> | 7:END(0) Match successful! Freeing REx: "(bar)" $
        Yes, but it seems anything but a literal string will make the variable defined.
        $ perl -wE 'my $s = "foo 123"; $s =~ /\d+/; say ${^PREMATCH}' foo

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://929457]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2015-07-31 23:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (282 votes), past polls