user2131116
user2131116

Reputation: 2861

How to clean the Output

I open a file and print some data on the screen , but I want to clean the screen after I output the data , I use clear; in the program but I don't see the effect of clean . It didn't clean .Does there any command or function can let me do that?

I want to see the contain of a file only , but not to see some of the previous file on the screen ...

Here is my programs

`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;

Thanks

Upvotes: 1

Views: 142

Answers (4)

Borodin
Borodin

Reputation: 126722

clear isn't working because the control sequence it outputs to clear the screen is being captured and returned to your program instead of being sent to the display.

Try

print `clear`

or

system('clear')

instead

Upvotes: 1

Krishnachandra Sharma
Krishnachandra Sharma

Reputation: 1342

Use system(), it works.

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

Upvotes: 0

Greg Bacon
Greg Bacon

Reputation: 139511

Your program uses non-idiomatic Perl. A more natural style would be

#!/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;
  }
}

Instead of writing a list of names to another file, read the names directly with opendir and readdir. The defined check is necessary in case you have a file named 0, which Perl considers to be a false value and would terminate the loop prematurely.

You don’t want to print everything. The directory entry may be a directory or an executable image or a tarball. The -T file test attempts to guess whether the file is a text file.

Invoke the external clear command using Perl’s system.

Finally, use the external head command to print the first 20 lines of each text file.

Upvotes: 2

Miguel Prz
Miguel Prz

Reputation: 13792

The solution you provided doesn't work because the clear command is performed in a sub-shell. I suggest the use of a CPAN module (and multi platform supported): Term::Screen::Uni

Example:

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

Upvotes: 0

Related Questions