Lilith Lilith - 12 days ago 5
Perl Question

String overloaded variable is considered defined no matter what

I have the following lines in my script:

my $spec = shift;
if (!defined $spec) {
return ("Invalid specification", undef);
$spec = "$spec" // '';

I would naturally expect this to, when passed
, return the warning
Invalid specification
in the array, with the second item being
. Instead, the check is passed, and I get a console message warning me about
Use of uninitialized value $spec in string
on the next line.

is an object with string and number overloading, and is unfortunately written such that attempting to test for truthiness in this particular subroutine (by way of
if ($spec)
for instance) results in deep recursion and a segfault.

While I am interested in why, exactly, this is happening, I'm more interested in how to make it stop happening. I want to eliminate the console warning, preferable without
no warnings qw/uninitialized/
. Is this possible, and if so, how do I do it?


You say that $spec is an object with string overloading.

If that's the case then you need to coerce it into String form before checking for it being defined:

if (! defined overload::StrVal($spec)) {