For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2005 > Re: Question about || (was REGEXP removing - il- - -b-f and - il- - - - f)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Question about || (was REGEXP removing - il- - -b-f and - il- - - - f)
Offer Kaye

2005-04-27, 3:56 pm

On 4/27/05, Peter Rabbitson wrote:
>=20
> Why is that? :)=20


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?
>=20
> 1)
> if ($b) {
> $a =3D $b;
> }
> else {
> $a =3D $c;
> }
>=20
> 2)
> $a =3D $b or $c;
>=20
> 3)
> $a =3D $b || $c;
>=20
> 4)
> $a =3D $b ? $b : $c;
>=20


Almost - 1 and 4 are too verbose, and 2 is just plain wrong. You have to wr=
ite:
$a =3D ($b or $c);
Since the precedence of "or" is lower than "=3D", if you write it
without the parens, you're actually writing:
($a =3D $b) or $c;
which will give you a warning under "use warnings;". So in this case I
feel it is better to write:
$a =3D $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. Althou=

gh
> this works:
>=20
> local ($/);
>=20
> ,I have no idea how it undefs $/. Points to a good reading on the
> subject are equally appreciated.
>=20


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 =3D "foo";
{
local $foo;
print Dumper($foo);
}
print Dumper($foo);

Read:
http://perldoc.perl.org/perlsub.htm...ues-via-local--

HTH,
--=20
Offer Kaye
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com