Re: On error, how to show the regexp values? (?: doesn't capture)
by LanX (Saint) on Jan 07, 2017 at 14:16 UTC
|
From
perlre#Extended-Patterns
> "(?^aluimsx:pattern)"
This is for clustering, not capturing ; it groups
subexpressions like "()", but doesn't make backreferences as
"()" does.
| [reply] |
Re: On error, how to show the regexp values?
by 1nickt (Canon) on Jan 07, 2017 at 14:15 UTC
|
if ( $2 ) {
...
} else {
...
}
Edit: ... By which I do not mean to suggest that you should always use an explicit if conditional: as my learned colleague explains below, there are any number of reasons you might get an undefined value. The point is your code should be ready for it, whether with an explicit test, no warnings, or howsoever you deem best.
The way forward always starts with a minimal test.
| [reply] [d/l] [select] |
|
I dare to disagree for global substitutions s///g
Either it doesn't match, so the error can't occur with a tested regex!
Or one used or clauses or conditional matching like in /(a)|(b)(c)/ then having an undefined $2 is intended behaviour.
in the latter case silencing the warning of an undefined value becoming an empty string is the normal approach. ( no warnings qw/uninitialized/ )
| [reply] [d/l] [select] |
|
Thank you. But i do not know exactly the number of the variables that will be used. I have brought here just a simple example.
| [reply] |
Re: On error, how to show the regexp values? (tracing warnings)
by LanX (Saint) on Jan 07, 2017 at 14:27 UTC
|
I order to capture a warning for debugging, you could (locally with local ) change sigwarn
DB<120> $SIG{__WARN__} = sub { print "<<<$_[0]>>>" }
DB<121> warn "xxx"
<<<xxx at (eval 58)[multi_perl5db.pl:644] line 2.
>>>
you can also run perlcode in a regex which checks $2 explicitly
edit
like in
DB<133> $x =~ s/ (?{ print "\$2 undefined" unless $2 })/ /g
$2 undefined
And finally: please note that you are already using two eval levels with /ee (why?) so how to run check code should be obvious.
| [reply] [d/l] [select] |
|
#!/usr/bin/perl
use warnings;
use strict;
my $sod='ip6 is used for...';
my $i=0;
my @baza_tek=qw( (?^u:ip(\d)) "internet_protocol-$1$2" );
{
local $SIG{__WARN__} = sub {
warn @_;
warn "\$baza_tek[$i] is '$baza_tek[$i]";
my $j = $i + 1;
warn "\$baza_tek[$j] is '$baza_tek[$j]";
};
$sod=~s#$baza_tek[$i]#$baza_tek[$i+1]#gee;
}
And the reason $2 is undefined is that your regular expression has only one capture group, (\d). | [reply] [d/l] [select] |
|
?
> Even outside the debugger, you can establish a warnings handler:
Yes, since "for debugging" doesn't mean "inside debugger"
> And the reason $2 is undefined
see Re: On error, how to show the regexp values? (?: doesn't capture)
> has only one capture group, (\d).
capture groups are unrelated to \d
The outer parens would capture without the colon :
| [reply] [d/l] [select] |
|
|
| [reply] |
|
But farther testing, on real data (that is much and is taken from different sources) did show, that the kind of exception, shows not the culprit at the time the error occurs. -- Like, if we have 1 000 patterns, and pattern #345 contains the error in focus, then the exception will show, say, pattern #378 -- back or forth in the row of patterns from the problem pattern, i do not know, having brought that exapmple just for better illustration of what i mean.
Personally, i think that the PERL mechanism for error handling, simply is late, and except it can be fixed, i think additional peace of code needs to be written like above have said -- that checks every pattern regexp, but, again, then we will get a lot matches, among whom will be the culprit.
So, before getting that mess of results, in order to make things done, i ask here for "wisdom" -- as it is kept saying here. ;-)
| [reply] |
|