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

I think the answer lies in that $_ is set to $test inside the regular expression statement, so it is not a numerical argument unless $test is numerical. As proof (as I was curious to find out what was going on I tried a couple of things to come to a conclusion)

1. When I changed the $_ to a numerical value inside the regex instead:

if ($test =~m#(??{substr($test, 0, 8)})#) { }
and the regex will work, without warning.

2. When I set the string $test to a something numerical (12,444.343,etc.) and again it will work without warning.

3. I tried setting $_ to a number outside and again it failed.

use strict; use warnings; my $test = "fee fi fo fee"; $_++; if ($test =~ m!((??{ substr($test,0,$_) }))!) { };
So what I figured it dealt with the $_ So what I finally did was the following as final proof that I was on the right track:
use strict; use warnings; my $test = "fee fi fo free"; if ($test =~ m!((??{ print "$_\n";substr($test,0,$_) }))!) { };
Which when it runs displays the following:
fee fi fo free Argument "fee fi fo free" isn't numeric in substr at (re_eval 1) line +1.
-enlil

Replies are listed 'Best First'.
Re: Re: Troubles with m!(??{substr(...)})!
by Zaxo (Archbishop) on Nov 21, 2002 at 02:16 UTC

    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

      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.