in reply to comparing two values under 'perl -w'
$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:Larry hasn't said so explicitly, but I'm pretty sure that two undefs will also compare equal (silently) under the new =~.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 7That 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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: comparing two values under 'perl -w'
by kappa (Chaplain) on Apr 03, 2002 at 07:07 UTC | |
by TheDamian (Vicar) on Apr 03, 2002 at 07:32 UTC |