PERL conditional equation error

Posted on 2008-10-06
Last Modified: 2008-10-06
Hi -
I have a line of code in a PERL cgi script as follows -

 if($first_char == '\*')

When I run the script it generates the following error -

Argument "*" isn't numeric in numeric eq (==) at ............line 488

Why am I getting this error and how should this line be written to avoid the error

I've tried several things but can't seem to get it "right" and don't understand why -

Points mostly for instruction -

Really trying to learn to avoid problems in the future -

Thanks -

Question by:rwinnick
LVL 48

Assisted Solution

Tintin earned 50 total points
ID: 22655716
The error is pretty obvious I would have thought.  The \ and * symbols aren't numeric.  The == test is for numeric tests.  For strings, you should use eq, eg:

if ($first_char eq '*') {

Note that you don't need to backslash the * if it is contained in single quotes.
LVL 84

Assisted Solution

ozo earned 200 total points
ID: 22655721
Did you mean to compare for string equality with eq
instead of for numeric equality with ==
Or did you mean to bind to a regex match with =~
LVL 84

Assisted Solution

ozo earned 200 total points
ID: 22655726
/usr/bin/splain: Reading from STDIN
Argument "*" isn't numeric in numeric eq (==) at ............line 488
Argument "*" isn't numeric in numeric eq (==) at ............line 488 (#1)
    (W numeric) The indicated string was fed as an argument to an operator
    that expected a numeric value instead.  If you're fortunate the message
    will identify which operator was so unfortunate.
LVL 39

Accepted Solution

Adam314 earned 250 total points
ID: 22655730
There are two ways to compare for equality in perl: as a string, or as a number.
To compare as a number, you use the == operator.  eg:    $a == $b
To compare as a string, you use the eq operator.  eg:  $a eq $b

Because your $first_char and '\*' are string, you need to use the eq operator.

Note that '\*' is two characters.  The * character isn't special in a single-quoted string (or in a double-quoted string), so it doesn't need to be escaped.

Also note that another way to determine if a string begins with a particular character (instead of getting the first character, and comparing it to some character) you could use a regex:
    if($str =~ /^\*/) {
        print "str begins with *\n";
    else {
        print "str does not begin with *\n";
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

LVL 84

Assisted Solution

ozo earned 200 total points
ID: 22655774
As strings, "0", "0.0", "0E0", "-0", "", "\\*" are all unequal
as numbers, they are all equal.
What are you trying to do?
LVL 84

Assisted Solution

ozo earned 200 total points
ID: 22655796
$first_char =~ '\*'
would be true if any character in $first_char was *
$first_char =~ '^\*'
would be true if  $first_char starts with *
$first_char eq '\*'
would be true if length($first_char) == 2 and the first character in $first_char was \ and the second character in  $first_char  was *

Author Comment

ID: 22655807
Thanks everyone -
I have a post that if it has an * as the first character I do one thing and if the first character is not an * I do something else -

So I am trying to get the first character of the post and see if it is an * -

Being VERY NEW at this stuff I didn't realize all the 'possibilities" -

This is all extremely helpful -

I really appreciate the 'instruction' -

Will try it out in my code right now -


Author Comment

ID: 22655829
for a more complete understanding what would not equal to be :-)


Author Comment

ID: 22655902
OK :-)
it works !!
a little learning is a good thing -
Just looked up string inequality eg: ne
I'm on a roll -

Thanks you guys are all great and a  major help

Will close this question with divided points

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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 (…
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 video discusses moving either the default database or any database to a new volume.

746 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

13 Experts available now in Live!

Get 1:1 Help Now