Perl Truth

Every time I write a perl script I have to pause to remember how Perl handles true and false. It seems Nathan Torkington answered the question years ago in this article from the Perl Journal. Truth is relatively simple when put in his terms:

– only scalars can be true/false (ie no lists)
– undef is false
– “” is false
– 0 is false
– 0.0 is false
– “0” is false
– all else is true

This means negative numbers are true.

This entry was posted in Perl, Programming on by .

About morgan

Morgan is a freelance IT consultant living in Philadelphia. He lives with his girlfriend in an old house in Fishtown that they may never finish renovating. His focus is enterprise Messaging (think email) and Directory. Many of his customers are education, school districts and Universities. He also gets involved with most aspects of enterprise Linux and UNIX (mostly Solaris) administration, Perl, hopefully Ruby, PHP, some Java and C programming. He holds a romantic attachment to software development though he spends most of his time making software work rather than making software. He rides motorcycles both on and off the track, reads literature with vague thoughts of giving up IT to teach English literature.

One thought on “Perl Truth

  1. Ildiko Rab

    I don’t think that the first statement is true. An empty list is false, e.g.:

    my @a = ();
    unless (@a) {
    print “list is empty\n”;
    }

    In a scalar context, the value of @a is the number of elements.

Leave a Reply

Your email address will not be published. Required fields are marked *