By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,374 Members | 2,014 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,374 IT Pros & Developers. It's quick & easy.

non blocked read in tk

P: 62
Hi!

I tryed this script to non blocked read in tk

Expand|Select|Wrap|Line Numbers
  1. use Tk;                        
  2. use Term::ReadKey;
  3.  
  4. ReadMode 4, STDIN;
  5. $in = \*STDIN;
  6.  
  7. $mw=MainWindow->new();    
  8. $mw->maxsize(500,800);                                
  9. $mw->minsize(500,800);
  10.  
  11. $text=$mw->Text( -width=>65,            
  12.                  -height=>5,
  13.                 );
  14.     $text->place(-x => 20,-y => 700);
  15. $text->focus();
  16.  
  17. $g2 = $mw->Button(-text => "Button",                    
  18.                  -command => sub{ &rutin}
  19.                             );
  20. $g2->place(-x => 240, -y => 100);
  21.  
  22. Mainloop();
  23.  
  24. sub rutin
  25.        {
  26.             while(1)
  27.                   {
  28.                        $k = "";
  29.                 $k=ReadKey(-1, $in);
  30.                     if($k ne "")
  31.                         {
  32.                             $text->insert('insert',$k);
  33.                             $text->focus();
  34.                             last;
  35.                         }
  36.                   }
  37.  
  38.        }

It doesnt work just if I change to the prompt shell and there hit a key.
Have anybody idea how possible take non blocked read whithout this

WIN XP, Active Perl 5.10
Oct 23 '08 #1
Share this Question
Share on Google+
9 Replies


Icecrack
Expert 100+
P: 174
Hi!

I tryed this script to non blocked read in tk

Expand|Select|Wrap|Line Numbers
  1. use Tk;                        
  2. use Term::ReadKey;
  3.  
  4. ReadMode 4, STDIN;
  5. $in = \*STDIN;
  6.  
  7. $mw=MainWindow->new();    
  8. $mw->maxsize(500,800);                                
  9. $mw->minsize(500,800);
  10.  
  11. $text=$mw->Text( -width=>65,            
  12.                  -height=>5,
  13.                 );
  14.     $text->place(-x => 20,-y => 700);
  15. $text->focus();
  16.  
  17. $g2 = $mw->Button(-text => "Button",                    
  18.                  -command => sub{ &rutin}
  19.                             );
  20. $g2->place(-x => 240, -y => 100);
  21.  
  22. Mainloop();
  23.  
  24. sub rutin
  25.        {
  26.             while(1)
  27.                   {
  28.                        $k = "";
  29.                 $k=ReadKey(-1, $in);
  30.                     if($k ne "")
  31.                         {
  32.                             $text->insert('insert',$k);
  33.                             $text->focus();
  34.                             last;
  35.                         }
  36.                   }
  37.  
  38.        }

It doesnt work just if I change to the prompt shell and there hit a key.
Have anybody idea how possible take non blocked read whithout this

WIN XP, Active Perl 5.10
Change This:

Expand|Select|Wrap|Line Numbers
  1. $g2 = $mw->Button(-text => "Button",                    
  2.                  -command => sub{ &rutin}
  3.                             );
to:
Expand|Select|Wrap|Line Numbers
  1. $g2 = $mw->Button(-text => "Button",                    
  2.                  -command => sub \&rutin
  3.                             );
Oct 23 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
You can't say "sub \&rutin". "sub" needs brackets

Expand|Select|Wrap|Line Numbers
  1. sub {anonymous function};
  2.  
  3. sub named_function {
  4. ....
  5. }
So this will work:

Expand|Select|Wrap|Line Numbers
  1. $command =  sub {&foo};
  2. $command->();
  3. sub foo {
  4.    print "here we are";
  5. }
  6.  
and this will work the same

Expand|Select|Wrap|Line Numbers
  1. $command =  \&foo;
  2. $command->();
  3. sub foo {
  4.    print "here we are";
  5. }
Oct 23 '08 #3

Icecrack
Expert 100+
P: 174
My Bad, I made a mistake it was meant to be
Expand|Select|Wrap|Line Numbers
  1. $g2 = $mw->Button(-text => "Button",                    
  2.                  -command => \&rutin
  3.                             );
Thank you for picking that up kevin
Oct 23 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
My Bad, I made a mistake it was meant to be
Expand|Select|Wrap|Line Numbers
  1. $g2 = $mw->Button(-text => "Button",                    
  2.                  -command => \&rutin
  3.                             );
Thank you for picking that up kevin
"Experts" are allowed two mistakes a year otherwise they get demoted to "Amatuer". ;)
Oct 24 '08 #5

Icecrack
Expert 100+
P: 174
Also Mainloop(); is wrong

Case its meant to be MainLoop(); with the upper case L in Loop.

Expand|Select|Wrap|Line Numbers
  1. MainLoop();
Oct 24 '08 #6

KevinADC
Expert 2.5K+
P: 4,059

Icecrack
Expert 100+
P: 174
"Experts" are allowed two mistakes a year otherwise they get demoted to "Amatuer". ;)

at least i found the problem :P
Oct 24 '08 #8

Icecrack
Expert 100+
P: 174
Anyways.....

http://www.tek-tips.com/viewthread.c...1508918&page=1

i guess he found his answer.
Oct 24 '08 #9

P: 62
i guess he found his answer.
No im not, my problem is different:
http://www.tek-tips.com/viewthread.c...1508918&page=1

Any idea?

Thanks!
Oct 24 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.