Why 'and' not '&&' ? (was Cleaning up 'uninitialized value')

> Use:
> 
>   if (@ARGV and -T $ARGV[0]) { ... }

Just wondering if there is a reason for using the lower precedence 'and'
here instead of '&&'?  I haven't been finding many uses for 'and' which
tells me I might not fully understand it.

Thanks,
Peter C.
0
c
5/30/2001 6:18:26 PM
perl.beginners 29368 articles. 3 followers. Follow

6 Replies
686 Views

Similar Articles

[PageSpeed] 12

On May 30, Peter Cornelius said:

>>   if (@ARGV and -T $ARGV[0]) { ... }
>
>Just wondering if there is a reason for using the lower precedence 'and'
>here instead of '&&'?  I haven't been finding many uses for 'and' which
>tells me I might not fully understand it.

You must be careful about using operators like '&&' and their
lower-precedence companions like 'and'.  I tend to use the spelled out
ones more often, except where it would require machinations to ensure the
proper execution of code:

  $x = $foo || $bar;  # that's ok
  $x = $foo or $bar;  # like:  ($x = $foo) || $bar

That second case is due to 'or' having lower precedence than '='.  You'd
have to do

  $x = ($foo or $bar);

It is at that point that I would decide not to use 'or'.  On the other
hand, I'd rather do

  open FOO, $file or die "...";

instead of

  open(FOO, $file) || die "...";

or even worse,

  open FOO, $file || die "...";  # like open FOO, ($file || die "...");

I prefer to de-line-noise my programs for readability[1].  As such, I use
parens only where needed.



[1] And yet, I love to use regexes... something for you to discuss among
yourselves, I guess.

-- 
Jeff "japhy" Pinyan      japhy@pobox.com      http://www.pobox.com/~japhy/
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734
** I need a publisher for my book "Learning Perl's Regular Expressions" **


0
jeffp
5/30/2001 6:34:24 PM
--- Peter Cornelius <c-peter.cornelius@liveplanet.com> wrote:
> > Use:
> >   if (@ARGV and -T $ARGV[0]) { ... }
> 
> Just wondering if there is a reason for using the lower precedence
> 'and' here instead of '&&'?  I haven't been finding many uses for
> 'and' which tells me I might not fully understand it.

I've almost never written code that used && in perl.
'and' has a lower precedence, which is usually what you want with
boolean expressions. It lets you write the same syntax with less line
noise.

  if ($a = func() and $b) { # assigns $a from func(), then checks $b

does something different than

  if ($a = func() && $b) {  # assigns $a from (func() && $b) compare

Not that assignments in if's come highly recommended, but I have used
them....

Also, while a lot of C programmers tend to try doing things in the
familiar fashion, I believe 'and' is a lot more readable than '&&',
which is a plus, and it also helps you remember this is Perl, not C.
The same mentality that hangs onto && will write

  for(my $i=0; $i<10; $i++) { print $i }

when it's much cleaner, clearer, and more efficient to write

  for my $i (0..9) { print $i }

and to my mind, even better to say

  print for 0..9;

Why the different structures?
Because everyone does it differently.
Perl doesn't make you abandon one for another, but each has its
advantages....



__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/
0
ydbxmhc
5/30/2001 6:42:15 PM
Lightning flashed, thunder crashed and Peter Cornelius <c-peter.cornelius@livep
lanet.com> whispered:
| > Use:
| > 
| >   if (@ARGV and -T $ARGV[0]) { ... }
| 
| Just wondering if there is a reason for using the lower precedence 'and'
| here instead of '&&'?  I haven't been finding many uses for 'and' which
| tells me I might not fully understand it.

In this particular case, not really.  Other than Jeff prefers to.

Understanding precidence levels and determining which is the best choice is
often a case of individual preference.

-spp
0
spp
5/30/2001 7:02:12 PM
At 11:18 AM 5/30/01 -0700, Peter Cornelius wrote:
> > Use:
> >
> >   if (@ARGV and -T $ARGV[0]) { ... }
>
>Just wondering if there is a reason for using the lower precedence 'and'
>here instead of '&&'?  I haven't been finding many uses for 'and' which
>tells me I might not fully understand it.

The way I teach it is, I use && in expressions, and 'and' for (lazy ways 
of) changing the flow of control.  Yeah, I know there's no difference, but 
it reads nicer to me and the different precedences happen to minimize 
parenthesization for these usages.

So

         $user = $ENV{USER} || $ENV{USERNAME} || $ENV{LOGNAME};
         if ($flirting && $flouncing) { ... }

but

         open GRIMBLE, $plugh or die "open $plug: $!\n";
         $found = 1 and next if /$pattern/;

YMMV.

--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com

0
Peter
5/30/2001 11:22:25 PM
On Wed, May 30, 2001 at 11:42:15AM -0700, Paul wrote:
> 
> --- Peter Cornelius <c-peter.cornelius@liveplanet.com> wrote:
> > > Use:
> > >   if (@ARGV and -T $ARGV[0]) { ... }
> > 
> > Just wondering if there is a reason for using the lower precedence
> > 'and' here instead of '&&'?  I haven't been finding many uses for
> > 'and' which tells me I might not fully understand it.
> 
> I've almost never written code that used && in perl.

Whereas I rarely use "and".

> 'and' has a lower precedence, which is usually what you want with
> boolean expressions. It lets you write the same syntax with less line
> noise.

> Also, while a lot of C programmers tend to try doing things in the
> familiar fashion, I believe 'and' is a lot more readable than '&&',

But I think && and || stand out better.

> which is a plus, and it also helps you remember this is Perl, not C.
> The same mentality that hangs onto && will write
> 
>   for(my $i=0; $i<10; $i++) { print $i }

Hmmm.

> Why the different structures?
> Because everyone does it differently.
> Perl doesn't make you abandon one for another, but each has its
> advantages....

..... and disadvantages.

I dabbled with "and" and "or" when they were added to the language, but
decided that in general I preferred && and ||, "if" and "unless".  Of
course the lower precedence operators have their place, but there is no
need to banish the higher precedence operators.

So, learn the precedence and use them as you see fit, or choose what
you're happy with and use parentheses as appropriate.

http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-05/msg01072.html

-- 
Paul Johnson - paul@pjcj.net
http://www.pjcj.net
0
paul
5/30/2001 11:59:11 PM
On Wed, 30 May 2001, Paul <ydbxmhc@yahoo.com> wrote,

> --- Peter Cornelius <c-peter.cornelius@liveplanet.com> wrote:
> I've almost never written code that used && in perl.
> 'and' has a lower precedence, which is usually what you want with
> boolean expressions. It lets you write the same syntax with less line
> noise.
> 
>   if ($a = func() and $b) { # assigns $a from func(), then checks $b

Assign $a to whatever func() returns, and *if it's true value*,
check $b (also, don't bother to check $b if func() return false).

> 
> does something different than
> 
>   if ($a = func() && $b) {  # assigns $a from (func() && $b) compare

If func() returns false, $a set to it, otherwise $b.
Perl doesn't only evaluate the truthness of the expression, but
also returns the value of the last expression evaluated.


s.a.n
-- 
Hasanuddin Tamir: hasant@trabas.com - Trabas: www.trabas.com

0
hasant
5/31/2001 8:35:48 AM
Reply:

Web resources about - Why 'and' not '&&' ? (was Cleaning up 'uninitialized value') - perl.beginners

Resources last updated: 12/21/2015 1:03:26 AM