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;