On Sun, 2 Jan 2005, Siegfried Heintze wrote:

> I am posting this query in beginners instead of beginners-cgi because I
> believe this is a question about the defined statement and not the $q->param
> statement/function.
> 
> I'm using this code:
> $q = new CGI;
> my $nUserId                  = $q->param("userId") ;
> 
> I was hoping the defined keyword would tell me if userId was present, but it
> does not seem to be doing that.
> 
> How can I make this execute the die statement when userId is missing from my
> get/post parameters?
> 
>         if (defined $nUserId) {
>           $juror_number = $nUserId;
>         } else {
>           die "No valid ID for User";
>         }
 
I usually just write in a way similar to this:

  my $q       = new CGI;
  my $nUserId = $q->param("userId") or die "No valid ID for User";

Or if I'm feeling less draconian,

  my $nUserId = $q->param("userId") or "No valid ID for User";

Which will set $nUserId to "No valid ID for User" if userId isn't 
defined. In a lot of cases, this is acceptable, but in your code, maybe 
everything falls apart without a userid and it really does need to die.

In any case, I think the '$foo = $bar || "default";' construct is very 
useful: it's succinct, clear, and generally does what you want...
 


-- 
Chris Devers

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to