I am trying to compare two variables in which are usually strings. These variables are generated from a database, $var1 from one db and $var2 from another.
When I compare them in a loop I use the ne operator. However there are times when I these variables are null or undef. The comparison is done as follows:
foreach my $var1 (@$varlist)
{
if ($var1 ne $var2)
{
print "vars are not equal";
}
}
The issue is that if $var1 or $var2 are undef then I get an error. However, I need to be able to compare then values as undef b/c I will have to write them. I considered converting the variables to a string ‘NULL’ and then back but that seemed inefficient.
Any way to fix this?
Thanks!
Check if they are defined, too:
This prints that they’re not equal if both are undefined. If you want another behaviour, just change the
ifexpression.