in reply to Tracing warnings: how to show the regexp values?

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.

Replies are listed 'Best First'.
Re^2: On error, how to show the regexp values?
by LanX (Saint) on Jan 07, 2017 at 14:40 UTC
    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!

Re^2: On error, how to show the regexp values?
by nikolay (Beadle) on Jan 19, 2017 at 07:58 UTC
    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.