Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Or Operator

by Avitar (Acolyte)
on Jul 30, 2004 at 23:15 UTC ( #378833=note: print w/replies, xml ) Need Help??

in reply to Or Operator

my $page = $cgi->param("page") Or "login";

should prob be rewritten as a conditional statement for clairy...

my($page) = $cgi->param("page"); if($page eq ""){ $page = "login";}

Try it with a lowercase 'or'; Remember if it is worth writing, it is worth writing clear... if possible even difficult code should be understandable by even the most unexperianced.

Replies are listed 'Best First'.
Re^2: Or Operator
by beable (Friar) on Jul 31, 2004 at 01:40 UTC
    I think this is the clearest and most concise way to express it: my $page = $cgi->param("page") || "login"; You might say that inexperienced people won't understand it, but once they see it a few hundred times (because this is a very common way of doing it), they'll probably get it.
      Then again, if there is a chance that $cgi->param("page") will be 0 - and you want to accept that, || will not work correctly. We'll get // (defined-or) in Perl 5.10, fortunately.
Re^2: Or Operator
by Aristotle (Chancellor) on Jul 31, 2004 at 19:31 UTC

    Except that will give you warnings if the page parameter is actually non-existent.

    You want to test definedness as well as emptiness.

    my $page = $cgi->param( "page" ); unless( defined $page and $page ne "" ){ $page = "login"; }

    Of course that's pretty clusmy and contains lots of unnecessary punctuation. Let's use a statement modifier instead:

    my $page = $cgi->param("page"); $page = "login" unless defined $page and $page ne "";

    Makeshifts last the longest.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://378833]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2018-05-25 00:00 GMT
Find Nodes?
    Voting Booth?