You often see a certainty check, so you don’t have to deal with a warning about using the undef value (and in Perl 5.10 it tells you an abusive variable):
Use of uninitialized value $name in ...
So, to get around this warning, people come up with all kinds of codes, and this code begins to look like an important part of the solution, rather than bubble gum and duct tape. Sometimes it's better to show what you are doing by explicitly disabling the warning you are trying to avoid:
{ no warnings 'uninitialized'; if( length $name ) { ... } }
In other cases, instead of data, you need to use some zero value. Using a Perl 5.10 specific or operator , you can give length explicit empty string (defined and return zero length) instead of a variable, this will raise a warning:
use 5.010; if( length( $name // '' ) ) { ... }
In Perl 5.12, this is a bit simpler because length in the value undefined also returns undefined . It may seem a little stupid, but it pleases the math I might have wanted to be. This does not raise a warning, so there is a reason for this.
use 5.012; use warnings; my $name; if( length $name ) {
brian d foy Sep 26 '09 at 19:27 2009-09-26 19:27
source share