To argue that Perl should provide a function to convert a scalar to a float because it provides a function to convert a scalar to an integer is a valid argument, but the premise is false. Perl doesn't have a function that converts a scalar to an integer.

int implements the mathematical function. It doesn't necessarily return an integer in the C sense of the word. For example, here's an example where int returns a float (NV):

>perl -MDevel::Peek -e"Dump int(2**32);" SV = NV(0x184ac1c) at 0x226be4 REFCNT = 1 FLAGS = (NOK,pNOK) NV = 4294967296

Type conversion is always done implicitely in Perl.
If Perl needs a string, the scalar will be coerced into a string.
If Perl needs a number, the scalar will be coerced into a number.
If Perl needs a boolean, the scalar will be coerced into a boolean.
etc.

Of course, you are free to produce your own conversion routines.

sub to_string { "$_[0]" } sub to_num { 0+$_[0] } sub to_bool { !!$_[0] } etc.

In reply to Re^3: When is 'eval' too inefficient? by ikegami
in thread When is 'eval' too inefficient? by argv

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.