Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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.


Comment on Re: Or Operator
Select or Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://378833]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2014-12-28 03:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (178 votes), past polls