On 4/27/05, Peter Rabbitson wrote:
> > also
> > "or die" is more preferable than "|| die"
> 
> Why is that? :) 

It's a question of style, it is not better as in "the code will work
better". It is mentioned in "perldoc perlstyle" that it is preferable
to use "or" and "and" instead of "||" and "&&". However, see below for
an example of when || is actually better (IMO).

> I was actually going to post a question about ambiguity
> syntax later, but here it is anyway. Are the following 4 equivalent?
> 
> 1)
> if ($b) {
>   $a = $b;
> }
> else {
>   $a = $c;
> }
> 
> 2)
> $a = $b or $c;
> 
> 3)
> $a = $b || $c;
> 
> 4)
> $a = $b ? $b : $c;
> 

Almost - 1 and 4 are too verbose, and 2 is just plain wrong. You have to write:
$a = ($b or $c);
Since the precedence of "or" is lower than "=", if you write it
without the parens, you're actually writing:
($a = $b) or $c;
which will give you a warning under "use warnings;". So in this case I
feel it is better to write:
$a = $b || $c;
Since in this case the || saves you the trouble of using parens and
the expression means exactly what you think it means. See "perldoc
perlop" for the precedence or Perl operatods.

> Also there was an example on the web that completely threw me off. Although
> this works:
> 
> local ($/);
> 
> ,I have no idea how it undefs $/. Points to a good reading on the
> subject are equally appreciated.
> 

It declares $/ to be local without giving an init value. So now $/ has
the value of "undef". This isn't specific to $/ - local will do the
same to any var:
use Data::Dumper;
our $foo = "foo";
{
   local $foo;
   print Dumper($foo);
}
print Dumper($foo);

Read:
http://perldoc.perl.org/perlsub.html#Temporary-Values-via-local--

HTH,
-- 
Offer Kaye

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to