Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
laziness, impatience, and hubris
 
PerlMonks  

testing a non-existant hash entry...how to handle

by S_Shrum (Pilgrim)
on Mar 01, 2002 at 09:12 UTC ( #148515=perlquestion: print w/ replies, xml ) Need Help??
S_Shrum has asked for the wisdom of the Perl Monks concerning the following question:

My script allows for a &page= parameter. I am trying to set the script up to deal with the case of the call omitting this param and setting it to the &defaultpage parameter.

Note: using CGI.pm to handle parsing

What I'm tring to do: if ( $input->param('page') == "" && $input->param('defaultpage') != "" ) {

...but this line never works. Am I testing for the right thing? I tried: if ( $input->param('page') == null && $input->param('defaultpage') != "" ) {

...but this didn't work either. Any help would be welcome!

======================
Sean Shrum
http://www.shrum.net

Comment on testing a non-existant hash entry...how to handle
Select or Download Code
Re: testing a non-existant hash entry...how to handle
by busunsl (Vicar) on Mar 01, 2002 at 09:19 UTC
    '==' and '!=' are for numerical testing, you should use 'eq' and 'ne' for alpha comparisons.

    You can test for the existence of a cgi parameter like this:

    if ( ! defined $input->param('page') && defined $input->param('default +page') ) {

      That's the stuff...thanx.

      ======================
      Sean Shrum
      http://www.shrum.net

Re: testing a non-existant hash entry...how to handle
by Ryszard (Priest) on Mar 01, 2002 at 09:31 UTC
    Could a better method be testing for the existence of a param, and defaulting if it doesnt exist?
    if(!defined $q->param('page') { #add defaults here } else { #do taint checking here! }
    .. are we talking two different parameters?
      If you have quite a lot of inputs it may be viable to read them in in one hit.
      my $query = new CGI; my %inputs = $query->Vars; # All params passed in now in hash # Now can just use exists check to see if we have it if(exists $input{'page'} && exists $input{'page'} ne '') { # Do stuff } else { }
      Now can just use the 'exists' check to find if you have a particular input Just another way of doing it....
Re: testing a non-existant hash entry...how to handle
by PrakashK (Pilgrim) on Mar 01, 2002 at 15:08 UTC
    How about this?
    my $page = $input->param('page') || $input->param('defaultpage'); if ($page) { # do stuff .... }
    This seems cleaner to me.

    /prakash

Re: testing a non-existant hash entry...how to handle
by ton (Friar) on Mar 01, 2002 at 19:40 UTC
    The best way to check for the existance of a key in a hash is to use the exists function. Checking against the hash value doesn't always work, because you could have that value be 0. In fact, you could have that value be undef, so defined does not always give the right behavior either. Example:
    use strict; my %hash; my $result; $hash{'foo'} = 0; $hash{'bar'} = undef; if ($hash{'foo'} && $hash{'bar'}) { $result = 'present'; } else { $result = 'NOT present'; } print "Direct checking thinks 'foo' and 'bar' are $result in \$hash.\n +"; if (defined($hash{'foo'}) && defined($hash{'bar'})) { $result = 'present'; } else { $result = 'NOT present'; } print "'defined' checking thinks 'foo' and 'bar' are $result in \$hash +.\n"; if (exists($hash{'foo'}) && exists($hash{'bar'})) { $result = 'present'; } else { $result = 'NOT present'; } print "'exists' checking thinks 'foo' and 'bar' are $result in \$hash. +\n";
    -Ton
    -----
    Be bloody, bold, and resolute; laugh to scorn
    The power of man...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2014-04-23 21:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (555 votes), past polls