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 =~.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|