They differ in precedence, and are otherwise the same.
This means that if you write:
$a=$b || $c;
$a=$b or $c;
Perl evaluates this:
$a=($b || $c);
($a=$b) or $c;
This changes what gets assigned to $a, and can also change
the context of a statement (from
perlop):
@info = stat($file) || die; # oops, scalar sense of stat!
@info = stat($file) or die; # better, now @info gets its due
This occurs because die is in scalar context, (so || is
great for obfuscation!). || is also appropriate where you
want to cause the rightmost value to get assigned. That is:
$a=$b || $c
Would assign $c to $a if $b was false, otherwise it would
assign $b to $a. Whereas:
$a=$b or $c
Would
not assign $c to $a when $b was false! In scalar
context, using || for
dieing is fine, but you'll run
into trouble with things where context is important.
or
is almost always what you mean in these cases.
If you still don't believe me, try this:
$a=0 or 1;
$b=0 || 1;
$a will be 0, and $b will be 1.
Andrew.
In reply to Re: or or
by ahunter
in thread or or
by ybiC
-
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.