perlmaven.com icon indicating copy to clipboard operation
perlmaven.com copied to clipboard

is this an unnecessary warning from Perl's undef

Open richardalg opened this issue 1 year ago • 1 comments

https://perlmaven.com/use-of-uninitialized-value

In this use case, $x becomes undef. I doubt it matters why, but as an example, reading an expected numerical value from an external database can cause it if the code return is empty. Then a choice has to be made : create another database with not-null values, or assign a value of '0' (zero) in Perl, thus :
< if ($x[$k] eq '') {$x[$k] = 0}> where $k is a variable that sets an index to the array @x.

When the code is run, a warning is returned for each undefined value. This isn't necessarily a problem, as the warnings can be ignored, but I would prefer to be able to set a flag to temporarily prevent the warning message being thrown.

richardalg avatar Aug 15 '23 10:08 richardalg

either check if defined $x[$k] and handle that case or you can turn off warnings that will impact till the end of the lexical scope.

See this new article: https://perlmaven.com/selectively-ignore-warnings

szabgab avatar Aug 16 '23 04:08 szabgab