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.


In reply to Re: Re: Getting the Ordinal Value of a string (Kindof) by sierrathedog04
in thread Getting the Ordinal Value of a string (Kindof) by Splinter

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.