Re: gtk-perl bug, bad programming, or other?



Hello...

On Wed, Jul 6, 2016 at 11:52 AM, Quentin Sculo <squentin free fr> wrote:
Hi
It seems you are confusing arrays and hashes.
In perl $button["five"] is the same as $button[0], as "five" is 0 when converted to a number.
That's why you should "use strict" for everything but one-liners.
Don't worry, we all do stupid mistakes :)



D'oh , thanks

Here is the code working:

 ./test.pl 
get_label for button{one} is:  one 
get_label for button{two} is:  two 
get_label for button{three} is:  three 
get_label for button{quit} is:  quit 

$ cat test.pl 
#!/usr/bin/perl

use Gtk2 -init;
use strict; # <- IMPORTANT 

my $window = Gtk2::Window->new ("toplevel");

my $layout = Gtk2::Layout->new(undef,undef);
$window->add ($layout);

my $vbox = Gtk2::VBox->new();
$layout->add($vbox);

my @buttons = qw( one two three quit);
my %button;

foreach my $row ( @buttons ) {
  $button{$row}=Gtk2::Button->new($row);
  $vbox->add($button{$row});
  $button{$row}->signal_connect (clicked => sub {
     print "get_label for button{$row} is:  ".$button{$row}->get_label." \n";
    }
  );
};

$window->show_all;
Gtk2->main;

exit 0;





<snip>

--
Christopher McCrory
To the optimist, the glass is half full.
To the pessimist, the glass is half empty.
To the engineer, the glass is twice as big as it needs to be.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]