Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Refresh display in Perl

by Anonymous Monk
on Jul 30, 2003 at 18:41 UTC ( [id://279312]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi , I am displaying a few lines on the command screen using the "who" command in the perl script. I want to refresh the "who" command results after every 10 seconds . Can u suggest me how it can be done in Perl .

Replies are listed 'Best First'.
Re: Refresh display in Perl
by sauoq (Abbot) on Jul 30, 2003 at 19:04 UTC

    perl -e 'system("who"),sleep 10 while 1'
    or
    perl -e 'system("who"),sleep 10,system("clear") while 1'
    to clear the screen between who invocations.

    -sauoq
    "My two cents aren't worth a dime.";
    
      Instead of launching the 'clear' command everytime you can also store the clear sequence in a variable then just print it:
      perl -e '$clear=`clear`; system("who"),sleep 10,print $clear while 1'

        Uh... Yes... But that makes your one-liner longer and saving the overhead of calling clear really isn't worth it.

        -sauoq
        "My two cents aren't worth a dime.";
        
Re: Refresh display in Perl
by The Mad Hatter (Priest) on Jul 30, 2003 at 18:52 UTC
    If you just want to refresh the output of who every 10 seconds, there is no need for Perl, just use watch: watch -n 10 'who' on the command line. See man watch for more information.

      Although watch(1) is great, it's not standard on all (most) platforms. There is a world outside of Linux. :-)

      -sauoq
      "My two cents aren't worth a dime.";
      
        I was under the impression that it was standard on most *nix systems... Anyway, thanks for letting me know. ; )
Re: Refresh display in Perl
by derby (Abbot) on Jul 30, 2003 at 18:44 UTC
    perlintro (especially the looping section) and sleep are good starts.

    -derby

Re: Refresh display in Perl
by waswas-fng (Curate) on Jul 30, 2003 at 22:26 UTC
    also if this is for unix, you may want to look at Sys::Utmp which give you direct access to the data that is parsed by who. You can get more info that who gives you about current activities on the system.

    -Waswas
Re: Refresh display in Perl
by l2kashe (Deacon) on Jul 30, 2003 at 19:07 UTC

    The above suggestions are good. If you are sure the command will be run in a terminal you could do

    #!/usr/bin/perl use strict; my $cmd = '/bin/who'; # or wherever it is on your system my $clear = '/bin/clear'; my $sleep = '10' while (1) { system("$clear"); # clear the display system("$cmd"); # send the command to STDOUT sleep($sleep); # sleep a bit }

    use perl;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://279312]
Approved by sauoq
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-19 17:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found