In cleaning up some old code recently, I found a string that can be summed up as:
if ($val eq '2') {
print 'foo';
}
This looked glaringly wrong to me, however it works. In my current perl mindset, I immediately want to change this to:
if ($val == '2') {
print 'foo';
}
In my script I am sure that the value of $val will alway be numeric if it is defined at all. However, I'm a bit confused about why == exists if eq works correctly. Reading through perldoc perl, I find the following quote:
(Why do we have separate numeric and string comparisons? Because
we don't have special variable types, and Perl needs to know
whether to sort numerically (where 99 is less than 100) or
alphabetically (where 100 comes before 99)
So does == exist to present a full list of numeric comparison options, when it, unto itself is not necessary?
-c
In reply to eq vs. ==
by c
-
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.
|