Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Whats the syntax for or, as opposed to and?
Thanx

Replies are listed 'Best First'.
Re: or syntax
by DamnDirtyApe (Curate) on Nov 07, 2002 at 22:56 UTC

    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: unlink "alpha", "beta", "gamma" or gripe(), next LINE; With the C-style operators that would have been written like this: unlink("alpha", "beta", "gamma") || (gripe(), next LINE); Using "or" for assignment is unlikely to do what you want; see below.

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: or syntax
by emilford (Friar) on Nov 07, 2002 at 21:50 UTC
    I'm not sure what you mean, but this might be on the right track.
    # and syntax if (($i > 0) && ($i < 40)) { # do something } # or syntax if (($i >0) || ($i < 40)) { # do something }
    You can of course also use the actually words 'and' and 'or', but I believe they have different precedence compared to the symbol forms.

    Update: The symbol form of 'or' (||) has higher precedence than the word form.
Re: or syntax
by Angel (Friar) on Nov 07, 2002 at 21:44 UTC
    The question is a little vague but I will gove some examples and hope this helps:
    #have a cgi var default to something #option 1 $x = $cgi->param("input_var") or "foo"; #option 2 $x = $cgi->param("input_var") || "foo"; #have open a file or die #option 1 open( HANDLE, "foo.txt" ) or die; #option 2 open( HANDLE, "foo.txt" ) || die; #have some sort of logic test #option 1 if( $x eq "foo" or $y eq "bar" ) #option 2 if( $x eq "foo" || $y eq "bar" )
Re: or syntax
by vek (Prior) on Nov 08, 2002 at 05:58 UTC