Leser: 17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#!/usr/local/bin/perl use strict; use warnings; use Tk; create_input_window('Test1','testsub1'); create_input_window('Test2','testsub2'); MainLoop; sub create_input_window { my $title = shift; # first line is the window title my $thissub = shift; # second line is the sub that is executed when the submit button is pressed my $mw = MainWindow->new(-title=>"$title"); my $mw_edit = $mw->Scrolled('Text',-relief => 'sunken', -borderwidth => 2, -scrollbars=>'osoe')->pack(-expand => '1', -fill => 'both'); my $subtoexecute = sub{testsub1($mw_edit->get('1.0', 'end'))}; # <-- wie kann man hier testsub1 durch die Variable $thissub ersetzen ? my $mw_submit_button = $mw->Button(-text => 'Submit',-background=>'gray',-command=> $subtoexecute )->pack(-side=>'left'); my $mw_close_button = $mw->Button(-text => 'Close',-background=>'gray',-command => sub{$mw->destroy})->pack(-side => 'top',-expand => '0',-fill => 'x'); $mw_edit->insert('end',"aA\nbB\ncC"); } sub testsub1 { my $contents=shift; my $linecount=0; foreach my $line (split(/\n/,$contents)) { $linecount++; print "$linecount: ".uc($line)."\n"; } } sub testsub2 { my $contents=shift; my $linecount=0; foreach my $line (split(/\n/,$contents)) { $linecount++; print "$linecount: ".lc($line)."\n"; } }
1 2 3 4 5 6 7 8
create_input_window('Test1',\&testsub1); #übergebe Referenz eines benannten Subs create_input_window('Test2',\&testsub2); #... # my $subtoexecute = sub{testsub1($mw_edit->get('1.0', 'end'))}; # <-- wie # kann man hier testsub1 durch die Variable $thissub ersetzen ? my $subtoexecute = sub{ $thissub->($mw_edit->get('1.0', 'end') ) };
1 2 3
create_input_window('Test1','testsub1'); #... my $subtoexecute = sub{ $table{$thissub}->($mw_edit->get('1.0', 'end') ) };
1 2 3 4 5 6
my $table{Test1}=sub {...}; create_input_window('Test1'); #... my $subtoexecute = sub{ $table{$title}->($mw_edit->get('1.0', 'end') ) };
1
2
3
4
no strict 'refs';
my $subtoexecute = sub{ $thissub->($mw_edit->get('1.0', 'end')) };
my $mw_submit_button = $mw->Button(-text => 'Submit',-background=>'gray',-command=> $subtoexecute )->pack(-side=>'left');