http://www.perlmonks.org?node_id=1019926


in reply to Re^2: "undef" is not NULL and what to do about it
in thread "undef" is not NULL and what to do about it

we probably hit code like this

Assuming that probably things are going to happen always is a recipe for disaster.

Your implementation of decrease_salary seems plausible, but it is not the only one. In order to ensure that you are handling "unknown" values right you will be forced to examine it, and even then, this is not perfectly safe, because somebody may rewrite that function in the future without noticing that subtle dependency.

IMO, the only unknown value that makes sense is one that croaks when used as part of any operation (including comparisons) and that has to be explicitly checked with is_unknown.

Or you can just use...

use warnings FATAL => 'uninitialized';

though, this will not cross lexical scopes, so I can still see value on having a magic unknown value.

Replies are listed 'Best First'.
Re^4: "undef" is not NULL and what to do about it
by Ovid (Cardinal) on Feb 21, 2013 at 14:44 UTC
      It occurs to me that there is another way to attack this problem, because, well, as you say in your blogs.perl.org post, you don't want the app to die after several hours just because some unknown value has slipped in.

      My idea is to imitate the taint approach. In the place where data is generated (i.e., DBI) it is flagged as maybe_unknown, then when it is used in other parts of the program the app will croak unless the flag had been removed with some check as is_unknown.

      Also, this feature should be easyly disabled, so that it can be used in development but turned off in production environments. For instance:

      sub data_generator { return maybe_unknown(@real_data); # attach maybe_unknown flag } sub data_processor { for my $data (@_) { if ($data =~ /foo/) { # in development this line croaks # because $data may be unknown. # Note that it will croak even when $data # is not actually 'unknown'! ... } unless (is_unknown($data)) { # clears the maybe_unknown flag if ($data =~ /foo/) { # does not croak because the maybe_unknown # flag has already been removed from $data ... } } } sub main { data_processor(data_generator); }