f



"Can't call method "disconnect" on an undefined value..."

Hey there anyone,

I am new at this and I am trying to figure this out and I don't know
too much, but I keep getting this error when trying to run my cgi page
can anyone help, please.

Here is the error:

Can't call method "disconnect" on an undefined value at
/home/virtual/site7/fst/var/www/cgi-bin/affiliates/common.sub line 44.

Here is the script I am trying to run:

#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);
#!/usr/local/bin/perl -w  

##################   Form Processing Function #########

 if ($ENV{'REQUEST_METHOD'} eq 'GET') { 
        # Split the name-value pairs
        @pairs = split(/&/, $ENV{'QUERY_STRING'});
    }
    elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
        # Get the input
        read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
 
        # Split the name-value pairs
        @pairs = split(/&/, $buffer);
    }
foreach $pair (@pairs) {

	($name, $value) = split(/=/, $pair);
   	$value =~ tr/+/ /;
   	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   	$value =~ s/<([^>]|\n)*>//g;
   	$value =~ s/\<//g;
   	$value =~ s/\>//g;
   	$value =~ s/\|//g;
   	$value =~ s/\*/\\\*/g;
   	$value =~ s/\?/\\\?/g;
   	$value =~ s/\+/\\\+/g;
	$FORM{$name} = $value;
}

#require "/var/www/cgi-bin/affiliates/config.cgi"; # you might need
the full path to this file
require "/home/virtual/site7/fst/var/www/cgi-bin/affiliates/config.cgi";
# you might need the full path to this file
require "/home/virtual/site7/fst/var/www/cgi-bin/affiliates/common.sub";
# you might need the full path to this file
use CGI;
use CGI qw(param);
use CGI qw/:standard/; 

$q = new CGI;

$ip = $q->remote_host();

@stuff = $q->param;
foreach $stuff (@stuff) {${$stuff} = $q->param($stuff);}

&gettheaffiliate;

	

sub gettheaffiliate {

$getcookie = $q->cookie($cookiename);
if ($getcookie) {}
else {
&Create_DB_Connection;
 $SQL="SELECT * FROM hits WHERE Ip='$ip'";
  &Do_SQL;
  while ($pointer = $sth->fetchrow_hashref){
  $getcookie = $pointer->{'Id'};
 }
 $sth->finish;
  $dbh->disconnect;
}
}
if ($getcookie ne ''){$getcookie = "Affiliate: $getcookie"}


$quantity = $FORM{'Quantity'};
$price = '54.95';
$shipping = $FORM{'s'};


$sub_shipping =  sprintf("%.2f", $shipping * $quantity);
$sub_price = sprintf("%.2f", $price * $quantity);
$sub_total = sprintf("%.2f", $sub_price + $sub_shipping);
$amount = sprintf("%.2f", $sub_price + $sub_shipping);

if ($FORM{s} eq '8.95'){ $ship = 'Shipping: Ground';}
if ($FORM{s} eq '21.95'){ $ship = 'Shipping: Rush';}
if ($FORM{s} eq '26.95'){ $ship = 'Shipping: Canada';}
if ($FORM{s} eq '33.95'){ $ship = 'Shipping: International';}






&returnhtml;
#$amount = '39.95';
 

#use CGI::Carp qw(fatalsToBrowser);
#use CGI qw (:standard);
#$q = new CGI;
#$cashflow = $amount; #where the $amount is whatever variable contains
the final purchase price.
#require "/home/hostcrew/hostcrew-www/cgi-bin/affiliates/config.cgi";
#require "/home/hostcrew/hostcrew-www/cgi-bin/affiliates/common.sub";
#require "/home/hostcrew/hostcrew-www/cgi-bin/affiliates/sale.cgi";
#&salecommission; 
#exit;

I hope someone can help, Thank You in advanced!

Justin
0
6/30/2003 5:32:35 PM
comp.lang.perl.modules 4194 articles. 0 followers. jerrykrinock (6) is leader. Post Follow

4 Replies
865 Views

Similar Articles

[PageSpeed] 58

Metalbomer wrote:

> I am new at this and I am trying to figure this out and I don't know
> too much, but I keep getting this error when trying to run my cgi page
> can anyone help, please.
>
> Here is the error:
>
> Can't call method "disconnect" on an undefined value at
> /home/virtual/site7/fst/var/www/cgi-bin/affiliates/common.sub line 44.

As the error message says, the value is undefined -- without counting lines
in your code, I suspect it's the line $dbh->disconnect, which would hint at
the fact that allocating the database handle may have failed and you should
have checked that error condition there. If you expect $dbh to sometimes be
undefined, $dbh->disconnect if ($dbh) would help.

--
Klaus Johannes Rusch
KlausRusch@atmedia.net
http://www.atmedia.net/KlausRusch/


0
KlausRusch (39)
7/1/2003 10:58:03 AM
Klaus Johannes Rusch <KlausRusch@atmedia.net> wrote in message news:<3F01693B.3EC7CD61@atmedia.net>...
> Metalbomer wrote:
> 
> > I am new at this and I am trying to figure this out and I don't know
> > too much, but I keep getting this error when trying to run my cgi page
> > can anyone help, please.
> >
> > Here is the error:
> >
> > Can't call method "disconnect" on an undefined value at
> > /home/virtual/site7/fst/var/www/cgi-bin/affiliates/common.sub line 44.
> 
> As the error message says, the value is undefined -- without counting lines
> in your code, I suspect it's the line $dbh->disconnect, which would hint at
> the fact that allocating the database handle may have failed and you should
> have checked that error condition there. If you expect $dbh to sometimes be
> undefined, $dbh->disconnect if ($dbh) would help.

That would fine I understood that but the line that errors out is blank. Look here:

(LINE 43)$ip = $q->remote_host();
(LINE 44)
(LINE 45)@stuff = $q->param;
(LINE 46)foreach $stuff (@stuff) {${$stuff} = $q->param($stuff);}

That is my problem and also as to why I don't understand the error message.
0
7/1/2003 7:17:33 PM
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Metalbomer1@hotmail.com (Metalbomer) wrote in 
news:18b2b862.0306300932.50e9f7ae@posting.google.com:

> Hey there anyone,
> 
> I am new at this and I am trying to figure this out and I don't know
> too much, but I keep getting this error when trying to run my cgi page
> can anyone help, please.

Here's a nickel's worth of free advice:  Toss this script, and never 
return to wherever you got it.  It's crap.

 
> Here is the error:
> 
> Can't call method "disconnect" on an undefined value at
> /home/virtual/site7/fst/var/www/cgi-bin/affiliates/common.sub line 44.
> 
> Here is the script I am trying to run:

I suspect that the script you're trying to run is not called 
"common.sub".  Look in that file for line 44.
 
> #!/usr/bin/perl
> 
> use CGI::Carp qw(fatalsToBrowser);
> #!/usr/local/bin/perl -w  

There is no point in duplicating the #! line.

 
> ##################   Form Processing Function #########

This form processing function is utter crap.  Not only does it have bugs, 
it does not have a mechanism for preventing DOS attacks by hax0rs who 
upload huge files.  It is utterly useless, given that your script also 
uses the fine CGI module, which is not buggy and allows you to limit 
uploads.

 
> #require "/var/www/cgi-bin/affiliates/config.cgi"; # you might need
> the full path to this file
> require "/home/virtual/site7/fst/var/www/cgi-
bin/affiliates/config.cgi";
> # you might need the full path to this file
> require "/home/virtual/site7/fst/var/www/cgi-
bin/affiliates/common.sub";
> # you might need the full path to this file

According to the error message you posted, the error is in the above 
file.

> use CGI;
> use CGI qw(param);
> use CGI qw/:standard/; 

It's silly to "use" the same module three times.  Also, ":standard" 
includes "param".  You (or the original author of this script) would have 
known this if you had consulted the CGI module documentation.

> $q = new CGI;

....and importing "param" and ":standard" are superfluous if you use the 
CGI module this way.
 
> $ip = $q->remote_host();
> 
> @stuff = $q->param;
> foreach $stuff (@stuff) {${$stuff} = $q->param($stuff);}

This is a VERY bad way to set variables in your script.  First, it does 
no error checking whatsoever.  What if the form has a CGI parameter 
called "q"?  What if a user maliciously submits a "q" parameter?  Here's 
what'll happen.  @stuff will have "q" among its values.  When the loop 
gets to that value, it'll assign "q" to $stuff.  Then the body of the 
loop will overwrite your $q variable.  The next time through the loop, 
perl will try to call the "param" method on a variable that's no longer 
an object.  And your users will see a 500 screen.

The same goes for any other variable in your script.

1) Don't use symbolic references until you *really* know what you're 
doing.  This will probably not be for years.  (that's no reflection on 
you personally; it just takes that long).  Using symbolic references is 
"breaking the rules", and you have to know when and why to do so.

2) There's no real reason to initialize a bunch of global variables with 
every CGI variable.  Instead of "$foo", say "$q->param('foo')".  Not much 
more overhead, and far safer.
 
> &gettheaffiliate;

In your particular case, this is not really a mistake.  But you should 
know that invoking subroutines with a leading & is generally a bad 
practice, and is to be avoided.  Break the habit before you develop it. 

 
> sub gettheaffiliate {
> 
> $getcookie = $q->cookie($cookiename);
> if ($getcookie) {}
> else {
> &Create_DB_Connection;
>  $SQL="SELECT * FROM hits WHERE Ip='$ip'";
>   &Do_SQL;
>   while ($pointer = $sth->fetchrow_hashref){
>   $getcookie = $pointer->{'Id'};
>  }
>  $sth->finish;
>   $dbh->disconnect;
> }
> }

1) Learn to indent code.  The above function is unnecessarily hard to 
read.

2) This function's reliance (and your program's reliance) on global 
variables is really a bad idea.  You have variables being set one place 
and used far, far away.  It makes for difficult-to-find bugs.  It's 
sloppy.

3. Instead of

    if ($something) {}
    else {...

you can do

    if (!$something) {...

or

    unless ($something) {...

> 
> I hope someone can help, Thank You in advanced!
> 
> Justin

I hope I didn't come down on you too hard.  My aim is not to discourage 
you from programming, but rather to encourage you to *think* when you do 
program.  Avoid using someone else's code verbatim without grokking what 
it does and why.

- -- 
Eric
$_ =  reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPwIRD2PeouIeTNHoEQJk8wCgnH273vl4VF2ib3Zq+OjgwlNR+EcAmwc3
XgE+6zBmEwKFVfqTk2Tcgyu4
=zmgS
-----END PGP SIGNATURE-----
0
7/1/2003 10:54:06 PM
On 1 Jul 2003 12:17:33 -0700,
	Metalbomer <Metalbomer1@hotmail.com> wrote:
> Klaus Johannes Rusch <KlausRusch@atmedia.net> wrote in message news:<3F01693B.3EC7CD61@atmedia.net>...
>> Metalbomer wrote:
>> 
>> > I am new at this and I am trying to figure this out and I don't know
>> > too much, but I keep getting this error when trying to run my cgi page
>> > can anyone help, please.
>> >
>> > Here is the error:
>> >
>> > Can't call method "disconnect" on an undefined value at
>> > /home/virtual/site7/fst/var/www/cgi-bin/affiliates/common.sub line 44.
                                                        ^^^^^^^^^^

>> As the error message says, the value is undefined -- without counting lines
>> in your code, I suspect it's the line $dbh->disconnect, which would hint at

> That would fine I understood that but the line that errors out is blank. Look here:
> 
> (LINE 43)$ip = $q->remote_host();
> (LINE 44)
> (LINE 45)@stuff = $q->param;
> (LINE 46)foreach $stuff (@stuff) {${$stuff} = $q->param($stuff);}

Is this the file common.sub that you're quoting here? The code you
posted had a suspiciously commented out line including common.sub,
while you're actually getting an error about that file.

Are you sure you're looking at the same sources that you're trying
to run?

Martien
-- 
                        | 
Martien Verbruggen      | Never hire a poor lawyer. Never buy from a
Trading Post Australia  | rich salesperson.
                        | 
0
mgjv (156)
7/1/2003 11:44:21 PM
Reply: