Go Back   CodingForums.com > :: Server side development > Perl/ CGI

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-21-2002, 08:00 AM   PM User | #1
premshree
Regular Coder

 
Join Date: Jun 2002
Location: Mumbai, India
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
premshree is an unknown quantity at this point
Perl Function Problem

Hello,

I am using the following code to test for operators :
Code:
sub isOperator
{
	($who)=@_;
	if($who=="+" || $who=="-"|| $who=="*" || $who=="/" || $who=="^")
	{
		return(true);
	}
	else
	{
		return(false);
	}
}

$get=<STDIN>;
if(isOperator($get))
{
	print $get;
}
The code should return true only when I enter '+', '-', '*', '/' or '^' but the code returns true for anything that I enter.

I can't figure out what is wrong. Please help!

Last edited by premshree; 09-21-2002 at 08:03 AM..
premshree is offline   Reply With Quote
Old 09-21-2002, 09:03 AM   PM User | #2
FlashCoder
New Coder

 
Join Date: Sep 2002
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
FlashCoder is an unknown quantity at this point
== wrong !

Try instead "==" will write "eq".
Ok?
__________________
No comments!
FlashCoder is offline   Reply With Quote
Old 09-21-2002, 06:18 PM   PM User | #3
premshree
Regular Coder

 
Join Date: Jun 2002
Location: Mumbai, India
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
premshree is an unknown quantity at this point
You mean instead of using '==' to use 'eq' ?
premshree is offline   Reply With Quote
Old 09-21-2002, 07:51 PM   PM User | #4
Mouldy_Goat
Regular Coder

 
Join Date: Jul 2002
Location: London, UK
Posts: 126
Thanks: 0
Thanked 0 Times in 0 Posts
Mouldy_Goat is an unknown quantity at this point
Yes, use 'eq' instead of '=='. The former does a string comparison, whilst the second does a numerical comparison, which isn't what you want here.

Two other points worth noting:

Firstly, in your function you're writing return (true);. What this will actually do is return the string 'true', and so whilst it will evaluate to true in a boolean context - it means that return (false); will also return true.

Perl doesn't have any boolean 'true' or 'false'. Instead just use a '1' and a '0' to get the same effect - it's less typing anyway.

Also you're forgetting to chomp() $get. This means getting rid of the newline (if any) on the end of the variable, which will cause all of your equality tests in the isOperator() function to return false, because "+\n" is not the same as "+". (The \n stands for newline).

So your script should probably look something like this:
Code:
sub isOperator
{
	($who) = @_;
	if($who eq "+" || $who eq "-" || $who eq "*" || $who eq "/" || $who eq "^")
	{
		return 1;
	}
	else
	{
# because nothing is being returned, this will evaluate to false
		return;
	}
}

$get = <STDIN>;
chomp $get;
if(isOperator($get))
{
	print $get;
}
Hope that helps a bit.
Mouldy_Goat is offline   Reply With Quote
Old 09-22-2002, 03:33 PM   PM User | #5
premshree
Regular Coder

 
Join Date: Jun 2002
Location: Mumbai, India
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
premshree is an unknown quantity at this point
Yea that worked! Thanks.
premshree is offline   Reply With Quote
Old 09-22-2002, 08:08 PM   PM User | #6
FlashCoder
New Coder

 
Join Date: Sep 2002
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
FlashCoder is an unknown quantity at this point
:)

Quote:
Originally posted by premshree
Yea that worked! Thanks.
__________________
No comments!
FlashCoder is offline   Reply With Quote
Old 09-23-2002, 09:50 AM   PM User | #7
premshree
Regular Coder

 
Join Date: Jun 2002
Location: Mumbai, India
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
premshree is an unknown quantity at this point
Yet another problem!

I have a file print.pl

Code:
print '<html><head><title>Welcome</title></head><body>';
print param('exp');
print '</body></html>';
I run the file in the browser with URL as
http://localhost/cgi-bin/print.pl?exp=Premshree but the parameter 'exp' is not printed.
premshree is offline   Reply With Quote
Old 09-23-2002, 05:47 PM   PM User | #8
FlashCoder
New Coder

 
Join Date: Sep 2002
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
FlashCoder is an unknown quantity at this point
Re: Yet another problem!

Quote:
Originally posted by premshree
I have a file print.pl

Code:
print '<html><head><title>Welcome</title></head><body>';
print param('exp');
print '</body></html>';
I run the file in the browser with URL as
http://localhost/cgi-bin/print.pl?exp=Premshree but the parameter 'exp' is not printed.
code:

print $ENV{CONTENT_LENGTH};

Please, for more information e-mail me (bdenis@list.ru) and I ask to your question!!!
__________________
No comments!
FlashCoder is offline   Reply With Quote
Old 09-24-2002, 03:05 PM   PM User | #9
premshree
Regular Coder

 
Join Date: Jun 2002
Location: Mumbai, India
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
premshree is an unknown quantity at this point
I am just trying to pass a parameter to a Perl file and print it on the browser. I don't want to use any CGI modules.
premshree is offline   Reply With Quote
Old 09-24-2002, 06:30 PM   PM User | #10
FlashCoder
New Coder

 
Join Date: Sep 2002
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
FlashCoder is an unknown quantity at this point
Easy !

Quote:
Originally posted by premshree
I am just trying to pass a parameter to a Perl file and print it on the browser. I don't want to use any CGI modules.
Sorry:

print $ENV{QUERY_STRING};

It write your parametr. It worked?
__________________
No comments!
FlashCoder is offline   Reply With Quote
Old 09-24-2002, 06:32 PM   PM User | #11
FlashCoder
New Coder

 
Join Date: Sep 2002
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
FlashCoder is an unknown quantity at this point
Re: Easy !

Quote:
Originally posted by FlashCoder


Sorry:

print $ENV{QUERY_STRING};

It write your parametr. It worked?
For more information, Please e-mail me the letter in address^
den86@hotbox.ru or bdenis@list.ru and a will ask on your all questions and problems !!!

__________________
No comments!
FlashCoder is offline   Reply With Quote
Old 09-25-2002, 08:02 AM   PM User | #12
premshree
Regular Coder

 
Join Date: Jun 2002
Location: Mumbai, India
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
premshree is an unknown quantity at this point
Yes, that printed the query string that I passed. Maybe I'll try to manipulate the query string to get the name-value pair.
premshree is offline   Reply With Quote
Old 09-25-2002, 10:52 AM   PM User | #13
FlashCoder
New Coder

 
Join Date: Sep 2002
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
FlashCoder is an unknown quantity at this point
Quote:
Originally posted by premshree
Yes, that printed the query string that I passed. Maybe I'll try to manipulate the query string to get the name-value pair.
code:
($a, $b)=split(/=/,$ENV{QUERY_STRING});

print "\$a is ";$a;

Did I help you?
__________________
No comments!
FlashCoder is offline   Reply With Quote
Old 09-25-2002, 02:21 PM   PM User | #14
premshree
Regular Coder

 
Join Date: Jun 2002
Location: Mumbai, India
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
premshree is an unknown quantity at this point
I used this :

Code:
@string=split(/&/,$ENV{QUERY_STRING});
@exp=split(/=/,$string[0]);
$expression=$exp[1];
$expression =~ tr/+/ /;
$expression =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$expression =~ s/<!--(.|\n)*-->//g;
@act=split(/=/,$string[1]);
$action=$act[1];
It works great!
premshree is offline   Reply With Quote
Old 09-25-2002, 05:25 PM   PM User | #15
Mouldy_Goat
Regular Coder

 
Join Date: Jul 2002
Location: London, UK
Posts: 126
Thanks: 0
Thanked 0 Times in 0 Posts
Mouldy_Goat is an unknown quantity at this point
Just out of interest why aren't you using the CGI.pm module?

It comes as standard with the Perl distribution so you shouldn't have to fiddle about with installing it.

All you should have to do is:
Code:
#!/usr/bin/perl
use CGI qw(:standard);
print header;
print '<html><head><title>Welcome</title></head><body>';
print param('exp');
print '</body></html>';
Mouldy_Goat is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:40 AM.


Advertisement
Log in to turn off these ads.