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

I would like to run the following code on Windows using Perl. I can run it as a single command from Unix just fine but would like to run it on Windows. while true; do netstat -n; grep 6000; sleep 5; print "\n"; done; Thanks
  • Comment on Change C style Code into Perl on Windows

Replies are listed 'Best First'.
Re: Change C style Code into Perl on Windows
by mifflin (Curate) on Jun 09, 2005 at 22:15 UTC
    It looks like you want to continually do a netstat and grep out only the lines that have the string '6000' in them.
    I tried the following on my PC and on my unix box and it works on both (although I had to search for another number).
    while (1) { print for (grep {/6000/} `netstat -n`); sleep 5; }

      Minor efficiency nit, but grep /6000/, `netstat -n` would be better as it avoids a BLOCK enter/leave for each line. If your grep test is a single EXPR rather than multiple statements it can shave a little time off. (And of course Benchmark to verify for your particular case if you're in doubt).

      --
      We're looking for people in ATL

        yep!
        erickn@cosmora01d:/home/erickn> cat xx use Benchmark qw(:all); cmpthese ( 10000000, {mifflin => &mifflin, fletch => &fletch } ); sub mifflin { grep {/6000/} qw(1 2 3 4 5 6000 7 8 9 10); } sub fletch { grep /6000/, qw(1 2 3 4 5 6000 7 8 9 10); } erickn@cosmora01d:/home/erickn> perl xx Rate mifflin fletch mifflin 5128205/s -- -19% fletch 6329114/s 23% --
Re: Change C style Code into Perl on Windows
by davidrw (Prior) on Jun 09, 2005 at 21:59 UTC
    take a look at while(){ ... } loops in perldoc perlsyn and system(); grep(); sleep(); from perldoc perlfunc

    Update: Actually, in stead of system(), you'll want to capture the output .. here's a starting point for that: Working with other processes and programs
Re: Change C style Code into Perl on Windows
by gellyfish (Monsignor) on Jun 09, 2005 at 22:14 UTC

    It might be a figment of my imagination but doesn't the windows netstat have an option to do an implicit loop anyway? I can't remember the switch if it does.

    Of course you can always do ( albeit far uglier) a similar loop in a windows batch file.

    /J\

      Yes, you can specify a looping interval in seconds as an argument to the Windows version of netstat. For example,

      netstat 10

      displays the netstat info every 10 seconds. But I guess that's not a portable solution.