use Tk; my $mw = tkinit; $mw->bind('' => sub { $mw->dock_window(50) }); MainLoop(); package MainWindow; sub dock_window {    my $self = shift;    my $px = shift || 10;    # Pixel vom Bildschirmrand        my $sh = $self->screenheight();    my $sw  = $self->screenwidth();        my($width, $height, $pos_x, $pos_y) = split(/[x+-]/, $self->geometry());        my $pos_x2 = $pos_x + $width;    my $pos_y2 = $pos_y + $height;        return if ($pos_x  == 0   || $pos_x  > $px)           && ($pos_x2 == $sw || $pos_x2 < ($sw - $px))           && ($pos_y  == 0   || $pos_y  > $px)           && ($pos_y2 == $sh || $pos_y2 < ($sh - $px));    $pos_x = $pos_x <= $px ? 0 : $pos_x;    $pos_y = $pos_y <= $px ? 0 : $pos_y;        $pos_x = $pos_x2 > ($sw - $px) ? $sw - $width  : $pos_x;    $pos_y = $pos_y2 > ($sh - $px) ? $sh - $height : $pos_y;    $self->geometry(sprintf("+%s+%s", $pos_x, $pos_y)); }