2013-03-07 103 views
1

我打开一个文件并在屏幕上打印一些数据,但是我想在输出数据后清理屏幕,我用的是clear;在程序中,但我没有看到干净的效果。它不干净。有没有 任何命令或功能可以让我这样做?如何清除输出

我想看到的只包含一个文件,而不是看到一些屏幕上的一个文件的...

这是我的计划

`ls > File_List`; 
open List , "<./File_List"; 
while(eof(List)!=1) 
{ 
     $Each = readline(*List); 
     chomp $Each; 

     print $Each; 
     print "\n"; 
     `clear`; 
     open F , "<./$Each"; 
     while(eof(F)!=1) 
     { 
       for($i=0;$i<20;$i++) 
       { 
         $L = readline(*F); 
         print $L; 
       } 
       last; 
     } 
     close(F); 
     sleep(3); 
     $Each = ""; 
} 
close List; 

感谢

+0

'使用严格的;使用警告;' – 2013-03-07 14:10:05

回答

1

clear不工作,因为它输出到清屏控制序列被捕获并返回到您的程序,而不是发送到显示器。

尝试

print `clear` 

system('clear') 

代替

2

您的程序使用非惯用Perl。一个更自然的风格将是

#!/usr/bin/env perl 

use strict; 
use warnings; 
no warnings 'exec'; 

opendir my $dh, "." or die "$0: opendir: $!"; 

while (defined(my $name = readdir $dh)) { 
    if (-T $name) { 
    system("clear") == 0 or warn "$0: clear exited " . ($? >> 8); 
    print $name, "\n"; 
    system("head", "-20", $name) == 0 or warn "$0: head exited " . ($? >> 8); 

    sleep 3; 
    } 
} 

而不是写名字的列表到另一个文件中,直接与opendirreaddir读的名字。如果你有一个名为0的文件,Perl认为这是一个错误的值,并且会过早地终止循环,所以需要检查defined

你不想打印的一切。目录条目可以是目录或可执行映像或tarball。 -T文件测试尝试猜测文件是否为文本文件。使用Perl的system调用外部命令clear

最后,使用外部的head命令打印每个文本文件的前20行。

+0

感谢它的工作... – user2131116 2013-03-07 14:13:57

+0

不客气。我很高兴它有帮助。 – 2013-03-07 14:15:37

+0

似乎很遗憾放弃'ls'而采用'opendir' /'readdir',然后为了使用'head'而掏空。 – Borodin 2013-03-07 14:37:14

0

您提供的解决方案不起作用,因为clear命令在子外壳中执行。我建议使用CPAN模块(多平台支持):Term::Screen::Uni

例子:

use Term::Screen::Uni; 
my $screen = Term::Screen::Uni->new; 
$screen->clrscr; 
+0

这与子shell的存在没有任何关系。问题是,'\'clear \''的输出不会进入显示器。 – Borodin 2013-03-07 14:46:16

0

使用system(),它的工作原理。

system("ls > File_List"); 
system("clear;");