in reply to comparing two values under 'perl -w'
There are several ways to solve this. One is to turn off warnings within the scope in which you have defined the variables. The following solution should work under most versions of Perl. It should also match your requirement that undef match undef.
use strict; use warnings; my ($w, $x, $y, $z)=(undef,undef,7,7); if ( equal( $x, $y ) ) { print "Equal\n"; } else { print "Not equal\n"; } if ( equal( $w, $x ) ) { print "Equal\n"; } else { print "Not equal\n"; } if ( equal( $y, $z ) ) { print "Equal\n"; } else { print "Not equal\n"; } sub equal { my ( $first, $second ) = @_; local $^W; # < 5.6 no warnings; # >= 5.6 return $first == $second; }
Of course, I've encapsulated that in a sub to minimize its effects.
Cheers,
Ovid
Update: Fixed code per some pointers by VSarkiss.
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
|
|---|