为什么 initscr() 在 delwin() return undef 之后?
Why does initscr() after a delwin() return undef?
为什么 delwin
导致第二个 initscr
到 return 什么都没有?我认为 endwin
会重置为调用 initscr
之前的状态。
use NCurses;
my $win = initscr();
addstr( 'AAA' );
nc_refresh();
sleep 2;
delwin( $win );
endwin();
...
my $new_win = initscr();
if ! $new_win.defined {
endwin();
dd $new_win; # NCurses::WINDOW $new_win = NCurses::WINDOW
die "win undefined"; # win undefined
}
addstr( 'BBB' );
nc_refresh();
sleep 2;
delwin( $new_win );
endwin;
实际发生的是 initscr
returns stdscr
(标准 window)。 delwin
删除了它(指针是 SCREEN
结构的一部分,该结构已及时更新),因此随后调用 initscr
(不是使用 newterm
) 创建了一个新的 screen) 将 return 那个 NULL
指针。原则上,应用程序可以引用 curscr
和 newscr
(另外两个 windows 在初始化期间创建) , 但 Perl 界面可能会忽略这些。
不过阅读文档还是有帮助的。引用 initscr
手册页的 Differences 部分:
Differences
X/Open specifies that portable applications must not call initscr
more
than once:
The portable way to use initscr
is once only, using refresh
(see
curs_refresh(3x)) to restore the screen after endwin.
This implementation allows using initscr
after endwin
.
Old versions of curses, e.g., BSD 4.4, may have returned a null pointer
from initscr
when an error is detected, rather than exiting. It is
safe but redundant to check the return value of initscr
in XSI Curses.
为什么 delwin
导致第二个 initscr
到 return 什么都没有?我认为 endwin
会重置为调用 initscr
之前的状态。
use NCurses;
my $win = initscr();
addstr( 'AAA' );
nc_refresh();
sleep 2;
delwin( $win );
endwin();
...
my $new_win = initscr();
if ! $new_win.defined {
endwin();
dd $new_win; # NCurses::WINDOW $new_win = NCurses::WINDOW
die "win undefined"; # win undefined
}
addstr( 'BBB' );
nc_refresh();
sleep 2;
delwin( $new_win );
endwin;
实际发生的是 initscr
returns stdscr
(标准 window)。 delwin
删除了它(指针是 SCREEN
结构的一部分,该结构已及时更新),因此随后调用 initscr
(不是使用 newterm
) 创建了一个新的 screen) 将 return 那个 NULL
指针。原则上,应用程序可以引用 curscr
和 newscr
(另外两个 windows 在初始化期间创建) , 但 Perl 界面可能会忽略这些。
不过阅读文档还是有帮助的。引用 initscr
手册页的 Differences 部分:
Differences
X/Open specifies that portable applications must not call
initscr
more than once:
The portable way to use
initscr
is once only, usingrefresh
(see curs_refresh(3x)) to restore the screen after endwin.This implementation allows using
initscr
afterendwin
.Old versions of curses, e.g., BSD 4.4, may have returned a null pointer from
initscr
when an error is detected, rather than exiting. It is safe but redundant to check the return value ofinitscr
in XSI Curses.