[resending with a better counter-example]

   Luke Bakken wrote:

  >>> use strict;
  >>>
  >>> my $str = 'AAAA1111';
  >>> matchit();
  >>> $str = '!!!!!!!!';
  >>> matchit();
  >>>
  >>> sub matchit
  >>> {
  >>>       if (length $str == 8) {
  >>>               $str =~ /^(\D+)(\d+)$/;
  >>>               print "\$1 $1 \$2 $2\n";
  >>>       }
  >>> }
  >>>
  >> There is one potential problem with this algorithm, which is
  >> that you aren't checking to make sure that the string actually
  >> matches your regex.  This can result in $1 and $2 containing
  >> the values from the last string that worked instead of being
  >> blank or failing, which can be hard to track down when you
  >> start getting different values than what you expected.
  >
  > Maybe you're used to some old behavior in Perl, but that doesn't
  > appear to be the case in 5.8.8 at least. I get this output:

  The code works because $1 and $2 are being recreated within their
  {} block.  The counter-example I gave in the earlier email was
  weak;  try this one:
  
      use strict;
      my $str = ''; 
      
      $str = 'AAAA1111';
                    $str =~ /^(\D+)(\d+)$/;
                    print "\$1 $1 \$2 $2\n";
      $str = '!!!!!!!!';
                    $str =~ /^(\D+)(\d+)$/;
                    print "\$1 $1 \$2 $2\n";
      
      matchit();
      
      sub matchit
      {
            if (length $str == 8) {
                    $str =~ /^(\D+)(\d+)$/;
                    print "\$1 $1 \$2 $2\n";
            }
      }

  whose output is:

    $1 AAAA $2 1111
    $1 AAAA $2 1111
    $1 AAAA $2 1111
    
  --Suresh

_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to