Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Perl Bug in Regex Code Block?

by demerphq (Chancellor)
on Sep 03, 2001 at 17:38 UTC ( #109866=note: print w/replies, xml ) Need Help??

in reply to Perl Bug in Regex Code Block?

Ok, well I am running 5.6.0 AS 623 and I get different output for your code:
0: 12; @counts = (0) 1: 34; @counts = (0) 2: 56; @counts = (0) @main::counts = (6)
Which says to me that perl is using the dynamic variable inside the regex eval. (Incidentally the docs do say that this is an experimental feature and may not work appropriately. Also they mention localization so I suspect this is maybe intended.) Also if you change the my to a local it produces the desired results.
Just ran it on AS 628 and it produces the results you said it did. Although worked as expected under our and local. My money says this is a bug.
But this all gets weirder.
When I change the code under (only did this under 623) (barely) to
#!/usr/bin/perl use strict; use warnings; use re qw/eval /; my $line = 'ab'; my $pattern = q/(.)(?{print ++$counts[0]})^/; for (0..2) { my @counts = (0); print "$_: "; $line =~ /$pattern/; print "; my \@counts = (@counts)\n"; } { no strict; no warnings; print "our \@counts = (".join(",",@counts).")\n"; #print "our \@counts = (@counts)\n"; }
I get the same result again. Now uncomment the last print line and run it again. I get a
In string, @counts now must be written as \@counts at .\ line + 22, near "our \@counts = (@counts" Execution of .\ aborted due to compilation errors.
Which to me doesnt make any sense at all. It should die in both cases cause the dynamic @counts is not declared, or in neither, but not like this.

And I have another point of weirdness to note in the regex you are using you have placed a '^' caret at the END of the regex, which for some reason makes your print statement fire twice. If I remove the ^ it prints once. Either way I dont see what is going on here at all....

You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)

Replies are listed 'Best First'.
Re2: Perl Bug in Regex Code Block?
by Hofmator (Curate) on Sep 03, 2001 at 20:07 UTC

    Some answers to your questions

    • Concerning the error message in connection with the last print statement ... I cannot reproduce that, it works fine both ways (commented and uncommented). With strict and warnings I get
      Possible unintended interpolation of @counts in string at bug line 20. Global symbol "@counts" requires explicit package name at bug line 19. Global symbol "@counts" requires explicit package name at bug line 20.
      and it dies as expected. Adding the explicit @main:: solves the problem altogether.
    • And I have another point of weirdness to note in the regex you are using you have placed a '^' caret at the END of the regex
      This is weirdness, you are right and actually not necessary for the thing in question here. It is a left-over from the code where I originally encountered the problem. But I can explain the behaviour ... consider this simpler regex "ab" =~ /.^/;
      it matches any character and after that the beginning of the line, so it can never match! Nevertheless the regex tries to match. First the a, then it sees that that doesn't work out and so tries the b after which it fails. If we now sneak in a code block like this "ab" =~ /.(?{print 'hello!'})/;
      the regex passes this block twice! And you can do very nice things with that (see e.g. my twiddle code) ... the original code came from a nonogram solver which I will post here in a couple of weeks (I have to find time to clean up the code a bit :)

    Update: I forgot to mention use re 'debug'. It is always helpful when you don't understand a pattern match.

    -- Hofmator

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2019-05-26 14:16 GMT
Find Nodes?
    Voting Booth?
    Do you enjoy 3D movies?

    Results (153 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!