Categories
Perl

perl tips in case you tend to forget things like I do

So, you want to test string equality in Perl? You just use ‘==’ right? Wrong. In Perl, that will only test number equality which could cause a few problems and inconsistencies.

The Wrong Way:

$mystring = "";

# wrong way
if ( $mystring == "" )
{
      print $mystring; #will never print
}

The Right Way

$mystring = "";
$mystring2 = "foo";

if( $mystring eq "" )
{
      print $mystring; #will print
}

#the 'not equals' counterpart
if ( $mystring2 ne "")
{
      print $mystring2; #will print
}

And a helpful link for good measure.