Ein typischer Fall von
use strict hilft. Mit strict und warnings bekommst Du nämlich folgende Meldungen:
Variable "$entry" will not stay shared at C:\community\toplevel.pl line 32.
Variable "$window2" will not stay shared at C:\community\toplevel.pl line 33.
Global symbol "$variable" requires explicit package name at C:\community\toplevel.pl line 22.
Global symbol "$variable" requires explicit package name at C:\community\toplevel.pl line 34.
Execution of C:\community\toplevel.pl aborted due to compilation errors.
Du solltest es besser so schreiben:
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
#!/usr/bin/perl
use strict;
use warnings;
use Tk;
use Tk::HList;
use Tk::ItemStyle;
my $variable;
my $fenster = new MainWindow;
my $hl = $fenster->HList(-width => 50,)->pack();
my $style = $hl->ItemStyle('text',
-foreground => '#FF0000',
-selectforeground => '#FF0000'
);
my $toplevel = $fenster->toplevel;
my $menubar = $toplevel->Menu(-type => 'menubar');
$toplevel->configure(-menu => $menubar);
my $datei = $menubar->cascade(-label => '~Datei',
-tearoff => 0);
$datei->command(-label => 'Zeige', -command => sub{nickchange()});
$datei->command(-label => 'Quit', -command => [$fenster=>'destroy']);
$hl->repeat(1000, sub{print $variable;});
MainLoop;
sub nickchange {
my $window2 = $fenster->Toplevel;
my $text = $window2->Label(-text => "Nickname eingeben")->pack();
my $entry = $window2->Entry()->pack();
my $button = $window2->Button(-text => 'Ok',-command => [\&action,$window2,$entry])->pack();
}
sub action {
my ($window2,$entry) = @_;
my $eingabe = $entry->get();
$window2->destroy();
$variable = $eingabe;
}