in reply to unique identifiers for gtk2 canvas objects?

I've never used gtk2 or anything like that, but this is just screaming out for closures. :-) It'd help people like me help you if you showed the code you had, though. I'm stealing code from Gnome2::Canvas's docs.

my $box = Gnome2::Canvas::Item->new ($root, 'Gnome2::Canvas::Rect', x1 => 10, y1 => 5, x2 => 150, y2 => 135, fill_color => 'red', outline_color => 'black'); $box->lower_to_bottom; $box->signal_connect (event => sub { my ($item, $event) = @_; warn "event ".$event->type." on $box\n"; });

The key is to simply use $box, or whatever, in the anonymous subroutine, and it will be forever linked to that instantiation. Even if you create a bunch of different $box's in a loop or in another subroutine, the $box it refers to will always be the correct one.

Replies are listed 'Best First'.
Re^2: unique identifiers for gtk2 canvas objects?
by marksz (Initiate) on Feb 09, 2005 at 22:18 UTC
    isn't ($box==$item) in the example you give? as far as i understand the connected sub gets called with the reference of the object and the event.

    but anyway, i don't see how this would help me. what i whould like to have is something i can use as a key in a hash ref that would hold some related data.

    come to think of it, can't you use objects as hash keys?

      Can't you use the $item as a hash ref? :-)