[Solved] convert 0 into string


If you think you have zero, but the program thinks you have an empty string, you are probably dealing with a dualvar. A dualvar is a scalar that contains both a string and a number. Perl usually returns a dualvar when it needs to return false.

For example,

$ perl -we'my $x = 0; my $y = $x + 1; CORE::say "x=$x"'
x=0

$ perl -we'my $x = ""; my $y = $x + 1; CORE::say "x=$x"'
Argument "" isn't numeric in addition (+) at -e line 1.
x=

$ perl -we'my $x = !1; my $y = $x + 1; CORE::say "x=$x"'
x=

As you can see, the value returned by !1 acts as zero when used as a number, and acts as an empty string when used as a string.

To convert this dualvar into a number (leaving other numbers unchanged), you can use the following:

$x ||= 0;

0

solved convert 0 into string