in reply to Re: Troubles with m!(??{substr(...)})!
in thread Troubles with m!(??{substr(...)})!

As one who was puzzled by this in cb, I was happy to see this sensible explanation. I really thought you'd found the answer, but this one-liner seems to say not: perl -e'($foo="bar") =~ m/($_)/;print $1,$/'

Update: ++ Enlil and BrowserUK for figuring this out! It's not m// but (??{}) that does it.

That's a fact worth knowing, and props for curiosity over efficiency! A modest application:

sub isPalindrome { $_[0] =~ /(??{quotemeta scalar reverse $_})/i } sub isAllUpper { $_[0] =~ /(??{uc})/ }

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Re: Troubles with m!(??{substr(...)})!
by Enlil (Parson) on Nov 21, 2002 at 05:30 UTC
    I started playing with the perl debugger and I still think I am right though I do not quite know the reasoning behind it. The debugging went as follows ( I am omitting much, but leaving the important parts). First the program:
    use strict; use warnings; my $test = "fee fi fo free"; if ($test =~ m!((??{ substr($test,0,$_) }))!) {};
    Now the debugging stuff:
    DB<1> V main # $_ never appears after this command DB<2> s main::(bah4.pl:6): if ($test =~ m!((??{ substr($test,0,$_) }))!) +{}; DB<2> V main # $_ never appears after this command DB<3> s main::((reeval 2)[bah4.pl:6]:1): DB<3> V main #At this point again variables in main are #listed there is a line: # $_ = 'fee fi fo free' DB<4> s Argument "fee fi fo free" isn't numeric in substr at (reeval 2)[bah4.p +l:6] line 1.
    and then a bunch of error stuff. I ran the one liner through the debugger and $_ never gets set, but as I mentioned before I do not know why the difference, perchance one of the many wiser monks than I might enlighten us at this point.

    -enlil

    *Note: I have not used the debugger all the much, so if I managed to confuse myself and thereby gave out misinformation let me know.

      It appears that there is an undocumented (as far as I can find) feature of the experimental (??{code}) regex feature. Namely that it makes the string that the regex is bound against available to the code block in a localised copy of $_. I think this is what the following shows:

      $test = 'the quick brown fox'; $_ = 8; if ( $test =~ m/^.*(??{print "$_\n"})/ ) { print "**$_**\n"; }

      Which produces

      the quick brown fox the quick brown fox the quick brown fox the quick brown fox the quick brown fox the quick brown fox the quick brown fox the quick brown fox the quick brown fox the quick brown fox the quick brown fox the quick brown fox the quick brown fox the quick brown fox the quick brown fox the quick brown fox the quick brown fox the quick brown fox the quick brown fox the quick brown fox

      To confirm the speculation I tried

      $test = 'the 1st quick brown fox'; $_ = 8; if ( $test =~ m/^.*(??{print "$_\n"})/ ) { print "**$_**\n"; }

      which gave

      the 1st quick brown fox the 1st quick brown fox the 1st quick brown fox the 1st quick brown fox the 1st quick brown fox the 1st quick brown fox the 1st quick brown fox the 1st quick brown fox the 1st quick brown fox the 1st quick brown fox the 1st quick brown fox the 1st quick brown fox the 1st quick brown fox the 1st quick brown fox the 1st quick brown fox the 1st quick brown fox the 1st quick brown fox the 1st quick brown fox the 1st quick brown fox the 1st quick brown fox **8**
      Which I think demonstrates that because there is a '1' in the bound string which matches the '1' returned from the print in the code block, the match succeeds once the re_engine steps back far enough to find the match. Hence this time the if block is executed and the previous value of $_ is printed.

      Could be a nice feature once you know its there.


      Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
      Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
      Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
      Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.