Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with length() function

Status
Not open for further replies.

tviman

Programmer
Jul 25, 2002
2,123
US
Here's my script...

#!/usr/bin/perl -w

use DBI;
use CGI qw(param);
use CGI qw:)standard);
use CGI::Carp qw(fatalsToBrowser);
use Fcntl ":flock";

my $cpnnbr = param("cpn_nbr");
my $coup = param("coupon");

if (length($cpnnbr) > 0){

script to get data from a file...

}

Problem is that even though $cpnnbr has data and it's length is greater than 0 (verified with tests), the 'if' statement is evaluating to false and the script inside the statement isn't being evaluated.

What am I not seeing?

There's always a better way...
 
I sent the data to a web page. (I don't have control of the server). Like this:

print "Content-type: text/html\n\n";

print <<EOF;
<HTML>
<HEAD>
</HEAD>

<BODY BGCOLOR=&quot;#FFFFFF&quot;>

cpnnbr = $cpnnbr<BR>
length = length($cpnnbr)<BR>
coup = $coup<BR>

</BODY>
</HTML>
EOF

There's always a better way...
 
Actually, I assigned the length to another variable

I sent the data to a web page. (I don't have control of the server). Like this:

my $lc = length($cpnnbr);

print &quot;Content-type: text/html\n\n&quot;;

print <<EOF;
<HTML>
<HEAD>
</HEAD>

<BODY BGCOLOR=&quot;#FFFFFF&quot;>

cpnnbr = $cpnnbr<BR>
length = $lc<BR>
coup = $coup<BR>

</BODY>
</HTML>
EOF

Sorry for the confusion... There's always a better way...
 
Are you certain your script is behaving as it should? Have you tried doing some other type of test within the if statement to make sure it isn't being executed?
 
Yes. Inside the &quot;if&quot; statement I create and initialize other variables. None of these vaiables are being created. There is also a connection to a database that I have deliberately &quot;sabatoged&quot; to see if the &quot;die&quot; directive shows anything. This is really weird and is driving me nuts!

Thanks to all who are scratching their heads with me! There's always a better way...
 
I suspect that you have a typo somewhere in your script. Did you cut and paste the code that you posted or did you type it? Try [tt]use strict;[/tt] and see if that catches it.

jaa
 
jaa - thanks for your help.

I've cut, pasted, typed, and retyped. Ran it through 2 debuggers with no errors. I've even cut the whole thing down to the bare bones just to see if I could get the if statement to work. No luck!

Is it possible that there's a problem with CGI.pm on my ISP's servers? I don't know where else to look!

Barkeep - pour me a HUGE one! There's always a better way...
 
Hi tviman,

I took you script and run it minus DBI as it is not yet installed on my windows XP and it ran fine. I ran it from the dos prompt like &quot;perl test.pl&quot;.


#!/usr/bin/perl -w

use CGI qw(param);
use CGI qw:)standard);
use CGI::Carp qw(fatalsToBrowser);
use Fcntl &quot;:flock&quot;;

my $cpnnbr = &quot;lee&quot;;

$test=length($cpnnbr);

print $test.&quot;\n&quot;;

if (length($cpnnbr) > 0){

print &quot;Hello World&quot;;

}


The results returned were as follows:


3
Hello World
Leland F. Jackson, CPA
Software - Master (TM)
Nothing Runs Like the Fox
 
You could try using the OO interface of CGI.pm instead. The underlying code is essentially the same except that the module creates and carries around the CGI object for you internally, but it might work.
Code:
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $query = new CGI;
my $cpnnbr = $query->param(&quot;cpn_nbr&quot;);
my $coup = $query->param(&quot;coupon&quot;);

if (length($cpnnbr) > 0){

script to get data from a file...

}
You could also try changing the if test to [tt]if ($cpnnbr)[/tt] because the empty string (a string of length 0) evaluates to false in boolean context.

Just some ideas.
jaa
 
Thanks to all for your help. Am taking the weekend off - my brain hurts! - but will put all your suggestions to work on Monday morning and will let you know of the outcome(s). BTW - just as a precaution I had my ISP reload CGI.pm on my server space. Don't know if this has anything to do with it but desperate people tend to do desperate things.

There's always a better way...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top