in reply to Re^2: Do people find warning for undef with string compare useful?
in thread Do people find warning for undef with string compare useful?

What if get two paired variables, and want to check that both are initialized, or both are empty or null strings?

If '0' isn't a valid value

if( ! $one == ! $two ) {

If '0' is a valid value but '' is not

if( ! length $one == ! length $two ) {

If '' is also a valid value

if( defined $one == defined $two ) {

That middle case isn't sufficient on rather old versions of Perl because it used to be that length(undef) was '0' with a warning. Now length(undef) is undef so you'll only get the warning if you treat the length as a number.

Some people replace '!' with '!!' because they use '!!' as an idiom for "I just care about whether it is true or not" or "convert to Boolean".

[ Update: I've seen people use something like

unless( $one xor $two ) {

But I find it way too easy to end up making mistakes when you start using the ultra-low-precendence Boolean operators for things other than flow control (I've found quite a few such mistakes made by some of the most experienced Perl programmers I've worked with), so I don't really like that approach. (I also don't like 'unless' but I won't go into that here.) ]

- tye