passing value

G

greymaus

I am trying to find out what I am doing wrong with this test program,
when I hit return, the print in the sub getword outouts
Tk::Entry=HASH(0x8467234). using $word as a global gives the correct
answer, but is obviously unsatisfactory. Debian etch. Perl 5.8.8.


#!/usr/bin/perl -w
use Tk;
my $word='word';
$mw=MainWindow->new();
$mf0=$mw->Frame->grid();
$me=$mf0->Entry(-textvariable=>\$word);
$me->bind('<Key-Return>'=>[\&getword,$word]);
$ml=$mf0->Label(-text=>'Enter Word');
$mgo=$mf0->Button(-text=>'Go',-command=>[\&getword,$word])->grid($me,$ml);

$mf1=$mw->Frame->grid();
$mll=$mf1->Scrolled("Listbox",-width=>25,-height=>15)->grid();

$mf2=$mw->Frame->grid();
$mee=$mf2->Button(-text=>'Exit',-command=>sub{exit})->grid();

MainLoop;

sub getword(){
$d=shift;
print "$d\n";
}
 
J

Josef Moellers

greymaus said:
I am trying to find out what I am doing wrong with this test program,
when I hit return, the print in the sub getword outouts
Tk::Entry=HASH(0x8467234). using $word as a global gives the correct
answer, but is obviously unsatisfactory. Debian etch. Perl 5.8.8.


#!/usr/bin/perl -w
use Tk;
my $word='word';
$mw=MainWindow->new();
$mf0=$mw->Frame->grid();
$me=$mf0->Entry(-textvariable=>\$word);
$me->bind('<Key-Return>'=>[\&getword,$word]);
$ml=$mf0->Label(-text=>'Enter Word');
$mgo=$mf0->Button(-text=>'Go',-command=>[\&getword,$word])->grid($me,$ml);

$mf1=$mw->Frame->grid();
$mll=$mf1->Scrolled("Listbox",-width=>25,-height=>15)->grid();

$mf2=$mw->Frame->grid();
$mee=$mf2->Button(-text=>'Exit',-command=>sub{exit})->grid();

MainLoop;

sub getword(){
$d=shift;
print "$d\n";
}

Every callback is called with the object (the button in this case) as
the first argument, then all additional arguments are attached.

try

sub getword {
print join(',', @_), "\n";
}

and you'll find your variable in the second location.

so, better write that as

sub getword {
my ($button, $d) = @_;
print "$d\n";
}

Josef
 
G

greymaus

greymaus said:
I am trying to find out what I am doing wrong with this test program,
when I hit return, the print in the sub getword outouts
Tk::Entry=HASH(0x8467234). using $word as a global gives the correct
answer, but is obviously unsatisfactory. Debian etch. Perl 5.8.8.


#!/usr/bin/perl -w
use Tk;
my $word='word';
$mw=MainWindow->new();
$mf0=$mw->Frame->grid();
$me=$mf0->Entry(-textvariable=>\$word);
$me->bind('<Key-Return>'=>[\&getword,$word]);
$ml=$mf0->Label(-text=>'Enter Word');
$mgo=$mf0->Button(-text=>'Go',-command=>[\&getword,$word])->grid($me,$ml);

$mf1=$mw->Frame->grid();
$mll=$mf1->Scrolled("Listbox",-width=>25,-height=>15)->grid();

$mf2=$mw->Frame->grid();
$mee=$mf2->Button(-text=>'Exit',-command=>sub{exit})->grid();

MainLoop;

sub getword(){
$d=shift;
print "$d\n";
}

Every callback is called with the object (the button in this case) as
the first argument, then all additional arguments are attached.

try

sub getword {
print join(',', @_), "\n";
}

and you'll find your variable in the second location.

so, better write that as

sub getword {
my ($button, $d) = @_;
print "$d\n";
}

Josef

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top