Regex Captures in Debugger

Ovid on 2009-12-08T14:02:43

Stumbled across this weird behavior today. Took a while to debug it. In the debugger, I'm not seeing the "dollar digit" regex capture variables set, even though the regex matches.

$ perl -de 1

Loading DB routines from perl5db.pl version 1.3
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(-e:1):   1
  DB<1> ($foo) = ('abcd' =~ /(bc)/)                                                                 

  DB<2> x $1                                                                                        
0  undef
  DB<3> x $foo                                                                                      
0  'bc'

Is this documented? I can't find it.

Update: Rafael explained it. The digit variables are lexically scoped. That's why you can assign to a package variable in a debugger, but not a lexical.

  DB<6>  'abcd' =~ /(bc)/ && print $1
bc


Scope

clintongormley on 2009-12-08T14:12:00

I think this is the same way that the debugger doesn't see lexicals declared on the previous line;

eg:
      DB my $foo = 'bar';
      DB x $foo
      0 undef

So this works:

      DB ($foo) = ('abcd' =~ /(bc)/) && print $1

Re:Scope

Ovid on 2009-12-08T14:17:28

Yeah, Rafael pointed that out to me and I updated my post before I saw your response.

By the way, you can use <ecode>some code here</ecode> tags to make your post prettier :)