Hi jinnicky,

Although I'm no Tk expert, I investigated the AM's hint about dual-valued variables, and there indeed seems to be something buggy going on. I boiled the code down to the following:

use warnings; use strict; use Tk; use Devel::Peek; my $scale = '22.5 '; # note the space in the output as well my $foo = 0 + $scale; # use in numeric context (set NV and IV) Dump($scale); my $mw = MainWindow->new; $mw->Entry(-textvariable=>\$scale)->pack; $mw->bind("<Key-Return>",\&calc); calc(); MainLoop; sub calc { print "str: <$scale> / num: <".(0+$scale)."> \n"; }

Output:

SV = PVNV(0x1e3f350) at 0x1e59a40 REFCNT = 2 FLAGS = (NOK,POK,IsCOW,pIOK,pNOK,pPOK) IV = 22 NV = 22.5 PV = 0x1e6f870 "22.5 "\0 CUR = 5 LEN = 10 COW_REFCNT = 1 str: <22.5 > / num: <22>

It appears that binding $scale to the Tk::Entry widget is causing the variable to become dual-valued with the wrong initial values (I guess the PV and IV values?). This only seems to be happening once on init though, when I input something everything seems to work fine. I don't know enough about the internals of Tk to really suggest a solution (perhaps a bug report is in order), but some workaround(s). You apparently can initialize $scale with a numeric value to begin with, my $scale = 22.5; seems to work for me. Or, as you've already discovered, you can stringify $scale when reading it out.

Hope this helps,
-- Hauke D


In reply to Re: Perl is ignoring fractional part sometimes by haukex
in thread Perl is ignoring fractional part sometimes by jinnicky

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.