Jenda has asked for the wisdom of the Perl Monks concerning the following question:

I'm using a slightly modified code from perlre to extract the parameters to a method call in VB, that is , to match the contents of properly nested braces, skipping the braces found in VB-style string constants and I found something strange. It seems I cannot use my, for the $braces variable. Try the following code with and without the "my":

my $braces = qr{ \( (?: (?> [^\(\)"]+ ) # Non-parens without backtracking | (?> "[^"]*")+ # skip string constants | (??{ $braces }) # Group with matching parens )* \) }x; my $Braces = qr{\(((?:[^\(\)"]+|"[^"]*")*(?:$braces(?:[^\(\)"]+|"[^"]* +")*)*)\)}; my $str = 'foo( FieldName, trim(CStr(Request.Form(FieldName))), Countr +yId )'; $str =~ $Braces and print "YES: $1\n" or print "NO!\n";
I get
YES: CStr(Request.Form(FieldName))
with the "my" and
YES: FieldName, trim(CStr(Request.Form(FieldName))), CountryId
without "my" with
This is perl, v5.8.0 built for MSWin32-x86-multi-thread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2002, Larry Wall Binary build 805 provided by ActiveState Corp. http://www.ActiveState. +com Built 18:08:02 Feb 4 2003
running under Win2k Server SP4

Is this a known bug, my misunderstanding, something that did not bite anyone yet, ...?

Thanks, Jenda
We'd like to help you learn to help yourself
Look around you, all you see are sympathetic eyes
Stroll around the grounds until you feel at home
   -- P. Simon in Mrs. Robinson

Replies are listed 'Best First'.
Re: (??{...}) in regexps and lexicals
by bart (Canon) on Jan 03, 2005 at 13:42 UTC
    Separate the declaration from your usage:
    my $braces; $braces = ... ;

    This is a well known pitfall in various situations, for example:

    my $x = foo or $x = bar;
    This uses two different $x variables.

      Uh oh, I'm ashamed of myself. Thanks a lot. Makes sense now.

      Jenda
      We'd like to help you learn to help yourself
      Look around you, all you see are sympathetic eyes
      Stroll around the grounds until you feel at home
         -- P. Simon in Mrs. Robinson

Re: (??{...}) in regexps and lexicals
by dave_the_m (Monsignor) on Jan 03, 2005 at 19:30 UTC
    Note also, that due to a bug in current perls, the $braces in the RE will always refer to the first instance of $braces. If this var is declared in file scope you're okay, but if its declared in a a sub or loop, you may have problems. I'm supposed to be fixing this for 5.10, but never seem to find the tuits. In the meantime you may want to use an our rather than a my.

    Dave.

      Thanks. It's OK in this case, it is file scoped and I have no intention whatsoever to change the variable contents. And I switched to "our". Don't need to "security" of the my's scoping.

      Jenda
      We'd like to help you learn to help yourself
      Look around you, all you see are sympathetic eyes
      Stroll around the grounds until you feel at home
         -- P. Simon in Mrs. Robinson