in reply to Preventing Use of uninitialized value.

Don't test for a match if there is no line:

if (defined $line and $line =~ /(\bTest\b\s+.+\s+\baccount\b)?/)
if ( ($line // '') =~ /(\bTest\b\s+.+\s+\baccount\b)?/)
(Update: added parens to handle precedence of //, thx to choroba)

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Preventing Use of uninitialized value.
by choroba (Cardinal) on Dec 13, 2018 at 17:05 UTC
    Unfortunately, // has a different precedence:
    if (($line // '') =~ /(\bTest\b\s+.+\s+\baccount\b)?/)

    Easy to verify with B::Deparse:

    $ perl -MO=Deparse,-p -e 'if ($line // "" =~ /(\bTest\b\s+.+\s+\baccou +nt\b)?/) {}' if (($line // ('' =~ /(\bTest\b\s+.+\s+\baccount\b)?/))) { (); } -e syntax OK

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Yep! As you were writing that I was composing another reply in which I discovered that fact.

      Thanks for the correction.


      The way forward always starts with a minimal test.