Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot


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


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;
> 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'.



Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2014-12-27 20:19 GMT
Find Nodes?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?

    Results (177 votes), past polls