113

I have been using this code to check if a string is empty:

if ($str == "")
{
  // ...
}

And also the opposite with the not equals operator...

if ($str != "")
{
  // ...
}

This seems to work (I think), but I'm not sure it's the correct way, or if there are any unforeseen drawbacks. Something just doesn't feel right about it.

Nick Bolton
  • 38,276
  • 70
  • 174
  • 242

7 Answers7

153

For string comparisons in Perl, use eq or ne:

if ($str eq "")
{
  // ...
}

The == and != operators are numeric comparison operators. They will attempt to convert both operands to integers before comparing them.

See the perlop man page for more information.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Yea, using this though you need to be cautious that if it is undef, and if you're using warnings you'll get a runtime warning. Many ways to get around the warning though. – Evan Carroll Jan 11 '10 at 23:34
110
  1. Due to the way that strings are stored in Perl, getting the length of a string is optimized.
    if (length $str) is a good way of checking that a string is non-empty.

  2. If you're in a situation where you haven't already guarded against undef, then the catch-all for "non-empty" that won't warn is if (defined $str and length $str).

hobbs
  • 223,387
  • 19
  • 210
  • 288
  • 8
    I think that length is the closest test that we have to the expression of the idea that there is nothing in the string. – brian d foy Jan 12 '10 at 11:23
  • 7
    Upvoted because `if (length($str))` is a nice approach and doesn't fail if the variable is not defined. – basic6 May 19 '15 at 09:32
13

You probably want to use "eq" instead of "==". If you worry about some edge cases you may also want to check for undefined:

if (not defined $str) {

# this variable is undefined

}
friedo
  • 65,762
  • 16
  • 114
  • 184
DmitryK
  • 5,542
  • 1
  • 22
  • 32
  • 2
    Very useful when you obtained the string by shifting of an array which may have 0 elements. – Dacav Nov 13 '11 at 12:45
9

As already mentioned by several people, eq is the right operator here.

If you use warnings; in your script, you'll get warnings about this (and many other useful things); I'd recommend use strict; as well.

Matthew Slattery
  • 45,290
  • 8
  • 103
  • 119
  • +1 Cool, yeah I normally use `use strict` but I'm updating some old code, so when I add this I get hundreds of errors. I'll probably fix them some day. – Nick Bolton Jan 11 '10 at 23:37
5

The very concept of a "proper" way to do anything, apart from using CPAN, is non existent in Perl.

Anyways those are numeric operators, you should use

if($foo eq "")

or

if(length($foo) == 0)
whatsisname
  • 5,872
  • 2
  • 20
  • 27
  • 5
    It's true that Perl doesn't tend to advocate the "one true way" towards problem solving, but that doesn't mean that there aren't strongly preferred idioms and styles and approaches. Also, as one of the Perl regulars likes to say a lot, even if there is more than one way to do things, *some* ways to do things are really, really bad (stupid, ill-advised, hard-to-maintain, etc.) – Telemachus Jan 12 '10 at 02:05
  • 4
    Just because there are many ways to do it does not mean that all ways are equal. Testing for empty strings using `if ($foo == "")`, for example, is *definitively wrong* unless you actually mean to be testing for whether `$foo`, evaluated in numeric context, has a value of 0 (in which case it would *still* be better written as `if ($foo == 0)`, as that more clearly expresses your intent). – Dave Sherohman Jan 12 '10 at 10:31
4

To check for an empty string you could also do something as follows

if (!defined $val || $val eq '')
{
    # empty
}
Sudheesh Singanamalla
  • 2,283
  • 3
  • 19
  • 36
Roland Ayala
  • 77
  • 1
  • 4
-4

The rest of answers are complicating things. It's just the following.


If filled:

if ($var) {

}

If not filled:

if (! $var) {

}
  • 1
    So, show us how that works with the string `0`. It's not that most answers are complicating things; it's that you never actually tried what you suggested to see all the ways it fails. – brian d foy Dec 11 '22 at 23:57