Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: "undef" is not NULL and what to do about it

by flexvault (Monsignor)
on Feb 21, 2013 at 11:14 UTC ( [id://1019935]=note: print w/replies, xml ) Need Help??


in reply to "undef" is not NULL and what to do about it

Good Day Ovid,

You stated:


    So, between the ambiguity of Perl's "undef" and the clarity of SQL's NULL, it's probably not surprising that some code could use the latter instead of the former. In fact, I will go further as to assert that the code is often clearer if you do that. Case in point:
    foreach my $employee (@employees) { if ( $employee->salary < $threshold ) { increase_salary( $employee, 3_000 ); } }
    I'm sure plenty of you see the bug: what if the employee doesn't have a salary? I've already outlined several reasons above for why the salary might be undefined, not all of which mean "this employee has no salary..."


I'm confused by your example, and the code below shows why I'm confused. In memory the value for 'salary' is undefined, but once it written to disk, whenever it is accessed after the initial 'write', the field is defined. So if this is a quirk of the DB you're using, why expect Perl to fix it? Everyone else seems to understand you're problem, but it seems related to the DB you're using and not to a specific Perl problem.

use strict; use warnings; my %Undef = ( Show => 'okay', Go => 'maybe', ); foreach my $key ( keys %Undef ) { if ( ! defined $Undef{$key} ) { print "1. $key value is undef +ined!\n"; } } $Undef{Salary} = undef; foreach my $key ( keys %Undef ) { if ( ! defined $Undef{$key} ) { print "2. $key value is undef +ined!\n"; } } open ( my $file, ">","./ondisk") || die " ! open |./ondisk| $!\n"; foreach my $key ( keys %Undef ) { print $file "$key\t$Undef{$key}\n"; ## line 20 } close $file; my %Defined; print "\n"; open ( $file, "<","./ondisk") || die " ! open |./ondisk| $!\n"; while ( my $value = <$file> ) { chomp $value; my ( $newkey, $newvalue ) = split(/\t/,$value); $Defined{$newkey} = $newvalue; } close $file; foreach my $key ( keys %Defined ) { if ( defined $Defined{$key} ) { print "$key\t|$Defined{$key}| +\n"; } } 1;
Results:
> perl unknown.plx 2. Salary value is undefined! Use of uninitialized value $Undef{"Salary"} in concatenation (.) or st +ring at unknown.plx line 20. Go |maybe| Salary || Show |okay|

Now if this is to solve a DB related flaw, then I see your point. But I don't understand how 'unknown' is better then 'undef/defined' in Perl scripts. YMMV!

Maybe the title should have SQL in front of "NULL" to clarify the intent of the 'unknown'.

Regards...Ed

"Well done is better than well said." - Benjamin Franklin

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1019935]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-16 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found