Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-20 02:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found