#! /usr/bin/perl -w

use strict;
use Glib qw/TRUE FALSE/;
use Gtk2 -init;

my $count =0;

my $window = Gtk2::Window->new ('toplevel');
$window->signal_connect (delete_event => sub { Gtk2->main_quit });
	my $button = Gtk2::Button->new ('Action');
 
$window->add ($button);
$window->set_position ('center-always');
$window->show_all;

#beware that the argument is a static snapshot of the varialble!
#To get a dynamic one you have to specify a refrence!
$button->signal_connect('button-release-event' => sub {
	
		my ($widget,$event) = @_;
		#count is in the same namespace as the closure
		&test_button_release($widget,$event,\$count);
	});

Gtk2->main;

sub test_button_release {

my ($widget,$event,$parameter)= @_;

	my $button_nr = $event->button();
	
	#to get the value of a reference, we ad a '$' to the reference's variable
	#from the camel book: "If what's inside is a reference,
	# you can look inside that (dereferencing $foo) by
	# prepending another funny character" -> $$parameter
	$widget->set_label("You Clicked Mouse button NR: $button_nr"."\nCounter data:". $$parameter);
	#we increment the value by 1
	${ $parameter }++;
  	print $count. "\n";
	#good practice to let the event propagate, should we need it somewhere else
	return FALSE;	
}

