Hello! Finally had a question I couldn't figure out or find on my own, so I request the wisdom of the keepers of Perl knowledge. I have a program which uses Tk::canvas, and I am trying to gather the coordinates of a mouse click on the canvas. What I would like to know is why the first bit of code works, where as numerous attempts at code similar to the second bit does not.

Works:
$canvas{'ref'}->CanvasBind("<1>", [ \&print_xy, Ev('x'), Ev('y') ]); sub print_xy{ my ($canv, $x, $y) = @_; print "(x,y) = ", $canv->canvasx($x), ", ", $canv->canvasy($y), "\n +"; }

Doesn't work:

$canvas{'ref'}->CanvasBind("<1>" => sub{ my $screenX = Ev('x'); my $screenY = Ev('y'); my $x = $canvas{'ref'}->canvasx($screenX); my $y = $canvas{'ref'}->canvasy($screenY); print "($x,$y)\n"; });
The second bit of code gives me 'bad screen distance x', which I know from trying is what I get when I dereference $x. I've tried various bits of dereferencing and such playfulness with the $screenX and $x values, and various things to try and get useful data, but it is all for not. I suspect there's something fundamental that is different which I don't know; can anyone shed some light on my conundrum? Thanks!

In reply to Tk::canvas - Cavas coord problem by Anonymous Monk

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.