Scalar is a basic data type in Perl. There are no basic numeric or basic string data types.
Perl will interpret a scalar as either a number or a string depending upon its context, but a scalar is both a number and a string at the same time to perl.
Here is an example of this feature:
sub testString {
my $myVar1 = "12.0";
my $myVar2 = "12";
if ($myVar1 eq $myVar2) {
print "String Equality Found\n";
}
if ($myVar1 = $myVar2) {
print "Numerical Equality Found\n";
}
}
The above subroutine returns "Numerical Equality Found". "eq" is a test for equality between strings, and "=" is a test for equality between numbers.
Since "12" does not equal "12.0" as a string the test for "eq" fails. Since 12 = 12.0 the test for "=" succeeds. Perl is testing the exact same variables, but it figures out from the context whether to treat those variables as strings or numbers.
That is why Perl is called a "weakly typed" language. Attempting to do something like the above in C would fail.
|