passing value

Discussion in 'Perl Misc' started by greymaus, Jan 12, 2009.

  1. greymaus

    greymaus Guest

    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";
    }


    --
    greymaus
    greymaus, Jan 12, 2009
    #1
    1. Advertising

  2. greymaus wrote:
    > 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
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
    Josef Moellers, Jan 12, 2009
    #2
    1. Advertising

  3. greymaus

    greymaus Guest

    On 2009-01-12, Josef Moellers <> wrote:
    > greymaus wrote:
    >> 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

    --
    greymaus
    greymaus, Jan 12, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. memememe

    passing value using eventargs

    memememe, Jul 7, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    7,123
    Kevin Spencer
    Jul 7, 2003
  2. Victor Bazarov
    Replies:
    25
    Views:
    841
    E. Robert Tisdale
    Mar 23, 2005
  3. pereges
    Replies:
    7
    Views:
    420
    pereges
    Jun 1, 2008
  4. Ramon F Herrera
    Replies:
    8
    Views:
    462
    Pascal J. Bourguignon
    Sep 13, 2009
  5. deadpickle
    Replies:
    1
    Views:
    946
    Jens Thoms Toerring
    Nov 7, 2010
Loading...

Share This Page