Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comment on

( #3333=superdoc: 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;
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


In reply to Re: "undef" is not NULL and what to do about it by flexvault
in thread "undef" is not NULL and what to do about it by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (8)
    As of 2014-09-21 16:12 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (172 votes), past polls