Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Tracing warnings: how to show the regexp values?

by nikolay (Beadle)
on Jan 07, 2017 at 13:50 UTC ( [id://1179127]=perlquestion: print w/replies, xml ) Need Help??

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

In the script

#!/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" ); $sod=~s#$baza_tek[$i]#$baza_tek[$i+1]#gee;

i have such an error on execution:

Use of uninitialized value $2 in concatenation (.) or string at...

My question is,

How to tell PERL to show the values of current baza_tek[$i] or $baza_tek[$i+1] at the time, when the error occured?

Replies are listed 'Best First'.
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.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re: On error, how to show the regexp values?
by 1nickt (Canon) on Jan 07, 2017 at 14:15 UTC

    You should always check to see if your capture matched anything before using it.

    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.
      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/ )

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

      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.
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.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Even outside the debugger, you can establish a warnings handler:

      #!/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).

        Awesome! Thank you very much!

        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. ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1179127]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-23 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found