Edit: almut beat me to it. Thats what I get for walking the dogs mid-post.

A couple of things...

First, it probably should not matter to you whether something is a 'string' or a 'number', as internally perl will convert it between strings and numbers as needed. The only use case I can think of for needing to care is if you are passing things to a serialization routine, where the output is destined for another programming language where it matters.

Second, I'm not sure I'd take the output of Data::Dumper at face value. Since from perl's perspective 123.1 is more or less the same as '123.1', it may just display the string.

If you really care to see the internal representation of a perl variable, you can use Devel::Peek.

use Devel::Peek qw| Dump |; my $x = 123.1; my $y = '123.10'; my $z = $y + 0; Dump $x; Dump $y; Dump $z;

Outputs

SV = NV(0x81ac10) at 0x801794 REFCNT = 1 FLAGS = (PADBUSY,PADMY,NOK,pNOK) NV = 123.1 SV = PVNV(0x8044d0) at 0x801770 REFCNT = 1 FLAGS = (PADBUSY,PADMY,NOK,POK,pIOK,pNOK,pPOK) IV = 123 NV = 123.1 PV = 0x301200 "123.10"\0 CUR = 6 LEN = 8 SV = NV(0x81ac20) at 0x801788 REFCNT = 1 FLAGS = (PADBUSY,PADMY,NOK,pNOK) NV = 123.1

So, as you can see, $x and $z are strictly NVs ( numeric values ) while $y is a PVNV, which has both numeric and string values contained. More information on the 'c' types for perl variables can be found in perlguts.

-- AlexLC

In reply to Re: Need some wisdom on strings to numbers by alexlc
in thread Need some wisdom on strings to numbers by decebel

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.