Darryl Schnell wrote at Thu, 19 Sep 2002 16:09:06 +0200:

> The problem is it's giving me a illegal division by zero error for the
> following line:
> 
> if ($result_count !=0) {
>   $pagecount = int($result_count / $limit);
                                     ^^^^^^

Seems like $limit is 0 :-)

>   if (($pagecount * $limit) != $result_count) { $pagecount++; }
> }
> 
> Can anyone tell me what I might be doing wrong or possibly suggest an
> alternative method of doing this feature.
> 
> Thanks,
> D.
> 
> 
> #!/usr/local/bin/perl

Off topic: It's often better to use taint mode.

> 

use strict;
use warnings;

would have explained you why you get the error.

> use CGI;
> use Net::Telnet;
> 
> require "display-module.pl";
> require "error-module.pl";
> require "groups.ph";
                   ^

Uncommon Perl ending ?!

> 
> $x = new CGI;

The canonical name is $q (like query) :-)

> $choice = $x->param(choice);
                     ^^^^^^^^
Seems like the quotes are forgotten:

$choice = $x->param('choice');
                    ^      ^

Without it, it will be interpreted as a an undefined bareword (or so),
and so $choice and all the rest isn't defined,
what means in numeric context to be zero.
So, that's why $limit is zero and raises a division by zero exception.

> $type = $x->param(type);
> $detail = $x->param(detail);
> $strict = $x->param(strict);
> $limit = $x->param(limit);
> $search_s = $x->param(search_s);
> $action = $x->param(action);
> 
> print "Content-type: text/html \n\n";
                                ^

That blank is very uncommon.

(The best way to avoid it is
 use CGI qw/:standard/;
 print header;

> 
> if ($action eq "Search") {
>   $choice = "$action";
>   if ($search_s eq "") {
>     &field_missing;
>   }
>   if ($detail eq "") {
>     $detail = "off";
>   }  
>   if ($strict eq "") {
>     $strict = "off";
>   }

Seems like you want to set default values.
Another way to achieve it is
$detail ||= "off";
$strict ||= "off";

(or $_ ||= "off" for ($detail,$strict);)

>   &user_search;
> } else {
>   &display_search;
> }
> 
> sub user_search {
>   $reqpage = "0"; 
>   $prompt="/^->/";
>   $string = "$choice#$type#$detail#$strict#$search_s";

Another (more Perlish) way is
$string = join "#", $choice,$type,$detail,$strict,$search_s;

>   $telnet=new Net::Telnet (Telnetmode => 0,Timeout => 60, Errmode =>
> "return");
> 
>   if ($telnet->open(Host=>$server,Port=>9999)) {
>     $telnet->cmd(String=>"$string",Prompt=>$prompt);
>     @results = ();
>     while (defined($line=$telnet->getline())) {
>       chomp ($line);
>       $resul = $line;

BTW: Why not $result ?
                   ^

>       push(@results, $resul);
>       $result_count++;
>     }

It's unnecessary to count the results.
After the while loop has finished
$result_count == scalar @results;

>   }
> 
>   if ($result_count !=0) {

Shorter 
if ($result_count)
or in the above way:
if (@results) { ... }

>     $pagecount = int($result_count / $limit);
>     if (($pagecount * $limit) != $result_count) { $pagecount++; }
>   }
> 
> [snipped the rest because of my laziness]


Best Wishes,
Janek


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to