Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

&& and and (well, you try searching for an answer)

by Melly (Hermit)
on Sep 02, 2006 at 20:22 UTC ( #570885=perlquestion: print w/ replies, xml ) Need Help??
Melly has asked for the wisdom of the Perl Monks concerning the following question:

Can someone explain the difference between && and and and or and ||?

Extra points if you can offer a search term for this on google and/or perlmonks that would help answer this without a new post.

Super bonus-points if you can render the first paragraph in acceptable and comprehensible english.

Tom Melly, tom@tomandlu.co.uk

Comment on && and and (well, you try searching for an answer)
Re: && and and (well, you try searching for an answer)
by explorer (Chaplain) on Sep 02, 2006 at 20:34 UTC
    The difference is the precedence.

    From perlop:

    As more readable alternatives to "&&" and "||" when used for control flow, Perl provides "and" and "or" operators (see below). The short-circuit behavior is identical. The precedence of "and" and "or" is much lower, however, so that you can safely use them after a list operator without the need for parentheses:
Re: && and and (well, you try searching for an answer)
by traveler (Parson) on Sep 02, 2006 at 20:34 UTC
    Can someone explain the difference between && and and and or and ||?

    'and' and 'or' are lower precedence

    Extra points if you can offer a search term for this on google and/or perlmonks that would help answer this without a new post.

    google for "perl logical operators"

    Super bonus-points if you can render the first paragraph in acceptable and comprehensible english.

    Can someone explain the difference between '&&' and 'and' and 'or' and '||'?

      refactor:

      "Someone please explain the difference between '&&' and 'and', and the difference between 'or' and '||'."

      print substr("Just another Perl hacker", 0, -2);
      - apotheon
      CopyWrite Chad Perrin

Re: && and and (well, you try searching for an answer)
by gellyfish (Monsignor) on Sep 02, 2006 at 20:36 UTC

    and and or were introduced as lower precedence versions of && and ||, infact their precedence is guaranteed to be lower than any other operator so you can use them in places where otherwise you would have to use additional or otherwise unnecessary parentheses to ensure the correct meaning of a statement. This is probably clearest seen in:

    open HANDLE, $file || die $!;
    which is almost certainly a mistake (i.e. it won't die unless $file has a false value because the || has higher precedence than the comma in the argument list.) If you want to use || you would need to use the parentheses thus:
    open(HANDLE, $file) || die $!;
    Whereas:
    open HANDLE, $file or die $!;
    works fine.

    In most places it is mostly a stylistic choice of whether to use the higher priority operator and more parentheses or not.

    /J\

Re: && and and (well, you try searching for an answer)
by GrandFather (Cardinal) on Sep 02, 2006 at 20:36 UTC

    The && and || operators come from C and have the same relative precedence as they do in C. The and && or operators have been added to Perl with much lower precedence, even lower than assignment for example, which allows them to be used as statement joiners of using the Perl die idiom for example:

    open ... or die ...;

    The documentation for the operators can be found in the perlop man page. Note the precedence table at the start in particular.


    DWIM is Perl's answer to Gödel
Re: && and and (well, you try searching for an answer)
by davidrw (Prior) on Sep 02, 2006 at 23:02 UTC
Re: && and and (well, you try searching for an answer)
by shmem (Canon) on Sep 02, 2006 at 23:23 UTC
    Since all has been explained, I'll head for the super bonus points... :-D

    Can someone explain in which way the operator "and" differs from "&&", and "or" differs from "||"?

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Where are the pedants who would have you put your , inside the "? Thanks for not doing so.
Re: && and and (well, you try searching for an answer)
by and (Pilgrim) on Sep 03, 2006 at 05:24 UTC
    && and || should be used inside logical expressions while or should be used between statements for idiomatic error handling ("or die"). Finally, and is only useful for error handling with system.

      Tnx all - the link to the earlier thread was particularily helpful.

      Tom Melly, tom@tomandlu.co.uk
Re: && and and (well, you try searching for an answer)
by codeacrobat (Chaplain) on Sep 04, 2006 at 21:27 UTC
    Apart from precedence issues && takes two chars and "and" three ;-)

    P.S.
    $_=$you_are; /just.*hacker/|/.*another $^X.*/ or $0=~/.pl$/ || die "no +t a hacker"

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://570885]
Approved by gellyfish
Front-paged by apotheon
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2014-07-30 00:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls