I definitely think using
if(! $q->param('userid'))
is a good idea.
I think you may be getting an error because you aren't specifying a Content-type.
Try putting one of these at the top of your script.
print "Content-type: text/html\n\n";
or
print $q->header;