Solved

Strange problem .. Help

Posted on 2000-04-17
3
166 Views
Last Modified: 2010-03-05
Hi folks,

I am facing a strange problem. I would appreciate if u can help me finding what I am doing wrong. I am writing a cgi program using CGI.pm. I am reading a parameter value by executing GET from the browser and executing a query with the value.

It is like http://myserver/cgi-bin/test.cgi?contactID='CT0001'

My program looks like,

/****************************************
$myCgi = new CGI;
$contactId = $myCgi -> param('contactID');
chomp($contactId);
# if contact id is empty, show error message and return
if($contactId == "")
{
        Util::showError("Contact ID $contactId cannot be blank");
        exit ;
}
*******************************************

So here I am checking whether the value is passed or not.  Initially the contact id field was numeric and I used to pass

http://myserver/cgi-bin/test.cgi?contactID=001

It used to work fine, but when I started sending CT0001, it executes the showError routine even though I have passed contact Id properly. Even in the showError subroutine, I am trying to print the $contactId, it is printing properly as CT0001. But logically, it should not execute the showError subroutine.

What am I doing wrong here ?? Is ( if ( $contactId == "") is not a valid check for empty string value ?? Why it is working for numeric and not alphanumeric ??

Thanx in advance,
Maha


0
Comment
Question by:maha
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
maneshr earned 10 total points
ID: 2723253
you are using the == operator which is meant for Numeric comparison NOT String comparison.

Replace == with eq and it should work.

== != <=> (equal to, not equal to, signed comparison)

eq ne cmp (their string equivalents)

An even better option would be to use regular expression. That way you dont have to worry about string or numbers.

Eg

## Check if contactId is empty or if the user has entered white spaces!!
if($contactId=~ /^$/ || $contactId=~ /^\s+$/){

.....

}
0
 

Author Comment

by:maha
ID: 2723327
Thanx Maneshr

Actually I found the solution ( use of eq) and it worked. So I did delete this question . I donot understand how it has reappeared again. ANyway thanx for your time and so I accept your answer.

I am new to perl and learning it as I use. Can u recommend good book for regular expressions in perl ??

Maha
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2723388
Glad to be of help to you.

i would suggest you start with...

Learning Perl, 2nd Edition

By Randal L. Schwartz & Tom Christiansen Foreword by Larry Wall
2nd Edition July 1997
1-56592-284-0, Order Number: 2840
302 pages, $29.95

you can get better deals if you search on the NET.

======================
Specifically for Regular Expressions you can use...

Mastering Regular Expressions

Powerful Techniques for Perl and Other Tools

By Jeffrey E. F. Friedl
1st Edition January 1997
1-56592-257-3, Order Number: 2573
368 pages, $34.95

you can get the Table of Contents at ...
http://www.oreilly.com/catalog/regex/toc.html

Hope that helps
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now