Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: comparing two values under 'perl -w'

by TheDamian (Vicar)
on Apr 02, 2002 at 22:53 UTC ( [id://156157]=note: print w/replies, xml ) Need Help??


in reply to comparing two values under 'perl -w'

Not that it solves your immediate problem (well, not in a practical timeframe anyway), but Perl 6 will have a much easier way to handle this kind of comparison:
$x =~ $y

From the latest Exegesis (which is now finished and coming RSN to a perl.com near you):

The humble Perl 5 "match a string against a regex" operator is promoted in Perl 6 to a "smart-match an anything against an anything" operator. So now:
if ($val1 =~ $val2) {...}

works out the most appropriate way to compare its two scalar operands and does so. The result might be a numeric comparison ($val1 == $val2) or a string comparison ($val1 eq $val2) or a subroutine call ($val1.($val2)) or a pattern match ($val1 =~ /$val2/) or whatever else makes the most sense for the actual run-time types of the two operands.

This new turbo-charged "smart match" operator will also work on arrays and hashes and lists:

if @array =~ $elem {...} # true if @array contains $elem if $key =~ %hash {...} # true if %hash{$key} if $value =~ (1..10) {...} # true if $value is in the list if $value =~ ('a',/\s/,7) {...} # true if $value is eq to 'a' # or if $value contains whitespa +ce # or if $value is == to 7

That final example illustrates some of the extra intelligence that Perl 6's =~ has: when one of its arguments is a list (not an array), the "smart match" operator recursively "smart matches" each element and ORs the results together, short-circuiting if possible.

Larry hasn't said so explicitly, but I'm pretty sure that two undefs will also compare equal (silently) under the new =~.

Replies are listed 'Best First'.
Re: Re: comparing two values under 'perl -w'
by kappa (Chaplain) on Apr 03, 2002 at 07:07 UTC
    Thank you for the peek into the upcoming Exegesis. I eagerly look forward to spend another hour reading and dreaming for the best Perl ever :))
    But I wanted to note that equalness of two undefs is very useful but contradicts with generally accepted laws of three-state logic, does it not?
      Thank you for the peek into the upcoming Exegesis.

      ...which is now fully on-line.

      But I wanted to note that equalness of two undefs is very useful but contradicts with generally accepted laws of three-state logic, does it not?
      It sure does. There was a long discussion around that issue on the perl6-language mailing list...starting here.

      As I recall, there wasn't a clear outcome to the debate (surprise, surprise), but at the time Larry didn't seem keen on undef taking on the tri-state semantics of NULL.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-24 11:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found