Numbers and Operators
Because a Perl scalar isn't declared as a number or a string — it's just whichever one context demands — Perl keeps two entirely separate sets of comparison operators. == , != , < , > , <= , >= compare numerically, converting both sides to numbers first. eq , ne , lt , gt , le , ge compare the strings character by character, with no numeric conversion at all. Mixing them up is a classic Perl bug, because both are completely legal on the same values and silently give different answers.
use strict; use warnings; my $a = "10"; my $b = "9"; print "Numeric: ", ($a == $b ? "equal" : "not equal"), "\n"; print "String: ", ($a eq $b ? "equal" : "not equal"), "\n"; print "Numeric <: ", ($a < $b ? "yes" : "no"), "\n"; print "String lt: ", ($a lt $b ? "yes" : "no"), "\n";
Numeric: not equal String: not equal Numeric <: no String lt: yes
Numerically, 10 is greater than 9, so $a < $b is false ('no'). But lt compares the strings "10" and "9" character by character — '1' sorts before '9' — so $a lt $b is true ('yes'). Same two values, opposite-feeling answers, depending entirely on which operator you reach for.
What a real "isn't numeric" warning looks like
use warnings doesn't stop code from running when it does something numerically dubious — it just tells you. Adding a number to a string that doesn't start with one is a classic case. This is the real warning Perl produces, captured from an actual run:
use strict; use warnings; my $value = "7 apples"; my $total = $value + 3; print "Total: $total\n";
Total: 10 stderr Argument "7 apples" isn't numeric in addition (+) at script.pl line 5.
Perl still computed an answer — it took the leading numeric prefix of "7 apples" (7) and added 3, giving 10 — but use warnings flagged that the string wasn't purely numeric to begin with. Without use warnings, this would happen completely silently. Next: arrays — Perl's ordered, growable lists.
Practice exercise
Given my $a = "042" and my $b = "42", print "numeric equal" if $a == $b, and print "string equal" if $a eq $b. (Only one of the two should print.)