Reputation: 2805
I have googled until I ran out of options. I must be doing something wrong but I cannot figure out what. (obviously I am new to Perl and inherited this project). I am just trying to see if a variable is equal to 'Y'. As you can see towards the bottom of the code below, I have tried 'eq', '=' and '=='. Other than setting a new variable equal to the result of an 'eq' operation, all the attempts result in the page blowing up. The result of the string test ($stringtest) appears to be empty and I would not know how to test it if it wasn't.
#!/usr/bin/perl -w
#######################################################################
# Test script to show how we can pass in the needed information
# to a web page and use it to run a program on the webserver and
# redirect its output to a text file.
#######################################################################
require 5; #Perl 5 or greater
require "jwshttputil.pl";
require "jwsdbutil.pl";
# Parse QUERY_STRING
&HTTPGet;
# Parse POST_STRING - NOTE: This CLEARS the post data
&HTTPPost;
print STDOUT "Content-type: text/html\n\n";
print STDOUT "<HTML><BODY>";
$canrun = "Y";
$is4ge = $Data{'is4ge'};
$sYes = "Y";
#Step 1: Check for needed values
if ( !($Data{'user'}) )
{
print "user was not found<br/>";
$canrun = "N";
}
if ( !($Data{'pwd'}) )
{
print "pwd was not found<br/>";
$canrun = "N";
}
if ( !($Data{'is4ge'}) )
{
print "is4ge was not found<br/>";
$canrun = "N";
}
print "$Data{'is4ge'}"; #prints Y
print $is4ge; #prints Y
if ( !($Data{'db'}) )
{
print "db was not found<br/>";
#if (!($is4ge = "Y")) #dies
# $canrun = "N";
#if (!($is4ge eq "Y")) #dies
# $canrun = "N";
#$stringtest = ($is4ge eq $sYes);
#print $stringtest; #displays nothing
#if (($is4ge == $sYes)) #dies
# $canrun = "N";
}
print STDOUT "</BODY></HTML>";
Upvotes: 1
Views: 4706
Reputation: 69264
You have a good answer to your question, but it might be useful to point out how you could have investigated this yourself.
Firstly, your original title for this question was "How do I compare two strings in a CGI page under Apache?" That title has been corrected because this problem has nothing to do with CGI or Apache, it's simply a misunderstanding about Perl syntax. I know you couldn't have known that initially, but a good way to investigate strange errors like this is to eliminate as many of the complications as possible. So forget about CGI and web servers - just write a simple Perl program.
#!/usr/bin/perl
$canrun = 'N';
$is4ge = 'Y';
if (!$is4ge eq 'Y')
$canrun = 'Y';
print $canrun;
Running this, we get:
$ perl testif
Scalar found where operator expected at testif line 7, near ")
$canrun"
(Missing operator before $canrun?)
syntax error at testif line 7, near ")
$canrun "
Execution of testif aborted due to compilation errors.
That makes it clear that the problem is the syntax around lines 6 and 7. That might be enough to send you off to the Perl syntax manual page, where you would learn that condition statements in Perl always require braces around the block. If not, you could add the diagnostics
pragma to get more information.
So as you've already been told, the correct format for your test is
if ($is4ge ne 'Y') {
$canrun = 'Y';
}
But there are a few other things in this program that will make it hard to maintain in the future. Basically, it is rather falling behind with Perl best practices.
-w
when that was replaced with use warnings
in Perl 5.6.0 (released in 2000)use strict
in the code (that will point out many bad practices in your code which you'll want to fix - in particular, you'll need to declare your variables).require
d at the top of the program might well be better rewritten as proper Perl modules.jwshttpdutil.pl
might be a home-baked CGI parser that should be replaced with CGI.pm.Also, I recommend reading CGI::Alternatives to bring yourself up to date with modern Perl methods for writing web applications. You are using techniques that were out of date before the end of the last millennium.
Upvotes: 6
Reputation: 385789
Curlies around the body of flow control statements are not optional in Perl.
eq
is indeed the string comparison operator in Perl.
So,
if (!($is4ge eq "Y")) {
$canrun = "N";
}
Or just
if ($is4ge ne "Y") {
$canrun = "N";
}
You really should be using 1
and 0
instead of 'Y'
and 'N'
, since they are true and a false values that can more easily be tested.
Upvotes: 6