Aug 22, 2013, 9:56 AM
Post #3 of 5
Re: [zp] Is there a way to force undef to be undef, instead of 0?
[In reply to]
Is there a way that i can get undef instead of -1 for b?
I don't think you can. If you declare a variable with;
it is undefined, which you can test with the defined function:
But it you increment or decrement $foo, or add or substract a number, $foo is transformed into 0 before the arithmetic operation is made. This is plausibly an application of the followinf principle: "the Perl compiler/interpreter is trying to do what you mean."
print "foo is undef" unless defined $foo;
If you need undef to be propagated to other variables, you'll have to test yourself (or possibly create a class that overloads the arithmetic operators, but that seems to be overkill).