诅咒:当一次调用它时,“newterm”出现问题

时间:2012-03-23 18:20:40

标签: perl curses

package My_Test;
use warnings; 
use strict;
use Curses;

sub routine {
    my $scr = newterm( $ENV{TERM}, \*STDERR, \*STDIN );
    set_term( $scr );
    noecho();     
    keypad( 1 ); 

    printw "press a key: ";
    refresh;
    my $c = getch();
    printw "$c\n";
    refresh;
    sleep 1;

    endwin();
    delscreen( $scr );
}

1;

当我运行此脚本时

#!/usr/bin/env perl
use warnings;
use strict;
use My_Test;

My_Test::routine();
My_Test::routine();

第二个My_Test::routine产生此错误消息:

Use of uninitialized value in subroutine entry at My_Test.pm line 8.
argument 0 to Curses function 'set_term' is not a Curses screen at My_Test.pm line 8.

为什么这个子程序只在第一次工作?

1 个答案:

答案 0 :(得分:1)

package My_Test;

use strict;
use warnings; 
use Curses;

our $scr = newterm($ENV{TERM}, \*STDERR, \*STDIN);
set_term($scr);

sub routine {
    noecho();     
    keypad(1); 
    printw "press a key: ";
    refresh;
    my $c = getch();
    printw "$c\n";
    refresh;
    sleep 1;
    endwin();
}

1;