Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

(Ovid) Re: comparing two values under 'perl -w'

by Ovid (Cardinal)
on Apr 02, 2002 at 16:02 UTC ( [id://156042]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://156042]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-19 22:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found