?

Log in

No account? Create an account
Today's discovery is that Perl doesn't have a null value. In the… - 'Twas brillig, and the slithy toves did gyre and gimble in the wabe [entries|archive|friends|userinfo]
Thomas

[ website | Beware the Jabberwock... ]
[ deviantArt | the-boggyb ]
[ FanFiction | Torkell ]
[ Tumblr | torkellr ]

Links
[Random links| BBC news | Vulture Central | Slashdot | Dangerous Prototypes | LWN | Raspberry Pi]
[Fellow blogs| a Half Empty Glass | the Broken Cube | The Music Jungle | Please remove your feet | A letter from home]
[Other haunts| Un4seen Developments | Jazz 2 Online | EmuTalk.net | Feng's shui]

[Friday 21st November 2008 at 10:32 pm]
Thomas

boggyb
Today's discovery is that Perl doesn't have a null value. In the following code, the if statement will throw an error:

undef $foo;
if ($foo == 2)
{
    # do something
}


This is because Perl doesn't actually have a concept of null. The nearest thing is an undefined variable (undefined in the sense that the variable does not exist, not in the sense that the variable exists with unknown contents), except you can't directly use it with comparisions or logical operators. I like VB, as it does have such a thing:

Dim Foo
Foo = Null
If Foo = 2 Then
    # do something
End If


In VB, Null is a distinct value from zero (the classical C representation), and does pretty much what you expect. There are also distinct values for Nothing (an object variable that does not refer to an actual object, like a C null pointer but with error handling), and Empty (an optional parameter that was not provided). 'Course, to use these you do need to use Variants which have a performance hit, but it's not really significant anymore in this age of multi-gigahertz processors and it makes database work nice.
Link | Previous Entry | Share | Next Entry[ 2 pennies | Penny for your thoughts? ]

Comments:
[User Picture]From: tau_iota_mu_c
Saturday 22nd November 2008 at 2:05 am (UTC)
Hmmm, I'll take the fast operation over the lazy programming. Lazy programming is what gave us mozilla and openoffice et al.

undef $foo;
if (defined $foo && $foo == 2)
{
    # do something
}
(Reply) (Thread)
[User Picture]From: ralesk
Sunday 23rd November 2008 at 4:10 pm (UTC)

It gives you a warning if you use warnings — otherwise that block silently goes on the else path. undef is actually fairly close to a NULL style value, can be used as that fairly easily. But there is no difference between having an undef value and not existing in the first place, unless you use strict (you should) which will gladly smack your fingernails if you use an undeclared variable. It should be noted that any and every variable declared will initially have an undef value.

ralesk@eretnek:~$ perl -e 'use warnings; undef $foo; if ($foo == 2) { print "do something\n"; }'
Use of uninitialized value $foo in numeric eq (==) at -e line 1.
ralesk@eretnek:~$ perl -e 'use strict; undef $foo; if ($foo == 2) { print "do something\n"; }'
Global symbol "$foo" requires explicit package name at -e line 1.
Global symbol "$foo" requires explicit package name at -e line 1.
Execution of -e aborted due to compilation errors.
ralesk@eretnek:~$ perl -e 'use strict; my $foo; undef $foo; if ($foo == 2) { print "do something\n"; }'
ralesk@eretnek:~$
(Reply) (Thread)