Professional Web Applications Themes

simple windows process list - PERL Beginners

The object of the code below is to output a list of space seperated fields with PID, username and process. The code generates te correct output. My guess is that my perl code can be smaller. Who dares? #!perl # # Object: # To output a tab separated list of PID, username and process # for windows XP # # Prerequisites: # 1) ActiveState Perl # 2) Windows XP use warnings; use strict; for my $line (`tasklist /v /nh`) { chomp($line); if ( $line ne "" ) { # extract PID my $pid = substr($line, 26, 6); # remove leading ...

  1. #1

    Default simple windows process list

    The object of the code below is to output a list of space seperated fields
    with PID, username and process. The code generates te correct output. My
    guess is that my perl code can be smaller. Who dares?

    #!perl
    #
    # Object:
    # To output a tab separated list of PID, username and process
    # for windows XP
    #
    # Prerequisites:
    # 1) ActiveState Perl
    # 2) Windows XP

    use warnings;
    use strict;

    for my $line (`tasklist /v /nh`) {
    chomp($line);
    if ( $line ne "" ) {
    # extract PID
    my $pid = substr($line, 26, 6);
    # remove leading spaces
    $pid =~ s/^ *([0-9]+)$/$1/g;

    # extract username
    my $user = substr($line, 88, 50);
    # remove trailing spaces
    $user =~ s/^(.*\S)\s+$/$1/g;
    # change spaces in username to underscores
    $user =~ s/\s/\_/g;

    # extract process
    my $proc = substr($line, 0, 24).substr($line, 152, 72);
    # change multiple spaces to single spaces
    $proc =~ s/\s\s\s*/ /g;
    # remove trailing space
    $proc =~ s/\s$//g;
    # remove trailing N/A
    $proc =~ s/ N\/A$//g;

    # print tab seperated fields
    print $pid, " ", $user, " ", $proc, "\n";
    }
    }


    Jp Guest

  2. #2

    Default Re: simple windows process list

    In article <develooper.com>,
    nl (Jp) writes: 

    Don't care about smaller. Care about clearer. Sometimes the two
    go together.
     

    Avoid creating a whole new block by instead writing:
    $line ne "" or next;
     

    Why check for digits? What's going to go wrong if you don't?
     

    Why?
     

    This is usually written as s/\s+/ /g. even though
    technically there is a redundancy there. Or if you
    really mean 'space' and not 'white space', you can do:
    $proc =~ tr/ //s;
     

    I don't see any tabs there. Try this:
    print "$pid\t$user\t$proc\n";
     

    It is better design to have a subroutine that returns the
    values you want than to print them so close to having
    figured them out. One day you may want to do something
    other than printing them and you would like to be able to
    use the same code.

    --
    Peter Scott
    http://www.perldebugged.com/
    *** NEW *** http://www.perlmedic.com/


    Peter Guest

  3. #3

    Default RE: simple windows process list

    JP wrote: 

    unpack() with 'A' is handy for extracting and removing trailing blanks in
    one step.

    for (grep /\S/, `tasklist /v /nh`) {
    chomp;
    my ($proc, $pid, $user, $title) = unpack 'A24 A8 x56 A50 x14 A*', $_;
    $proc = "$proc $title" unless $title eq 'N/A';
    $proc =~ s/ +/ /g; # compress multiple spaces
    $pid += 0; # convert to number
    $user =~ tr/ /_/; # change blanks to underscores
    print join("\t", $pid, $user, $proc), "\n";
    }
    Bob Guest

  4. #4

    Default Re: simple windows process list

    On 21 Sep 2004 13:03:21 -0000, Peter Scott <com> wrote: 
    >
    > Don't care about smaller. Care about clearer. Sometimes the two
    > go together.
    >[/ref]

    <<SNIP>>
     


    Hi JP,

    I implemented some of Peter's suggestions for you, including making
    the meat of this code a subroutine for future use.

    Here's the sub:
    sub proclist {
    my %output;
    foreach my $line ( `tasklist /v /nh` ) {
    chomp( $line );
    $line ne "" or next;
    # extract PID
    my $pid = substr($line, 26, 6);
    # remove leading spaces
    $pid =~ s/^\s*//;

    # extract username
    my $user = substr($line, 88, 50);
    # remove trailing spaces
    $user =~ s/\s*$//;

    # extract process
    my $proc = substr($line, 0, 24).substr($line, 152, 72);
    # change multiple spaces to single spaces
    $proc =~ s/\s+/ /g;
    # remove trailing N/A
    $proc =~ s/N\/A\s*$//g;

    # build the return hash
    $output{$pid} = join( ':', $user, $proc );
    }
    return %output;
    }

    And here's some code that uses that sub to produce the output you were
    looking for:

    #!perl
    #
    # Object:
    # To output a tab separated list of PID, username and process
    # for windows XP
    #
    # Prerequisites:
    # 1) ActiveState Perl
    # 2) Windows XP

    use warnings;
    use strict;

    # here is an example of using the sub 'proclist' to produce the output
    # you described. Notice that
    # split(':', $proclisthash{$pid})
    # will seperate the user info from the process info for you
    my %plist = proclist();

    foreach my $pid( keys %plist ) {
    my( $out_pid, $out_user, $out_proc ) = ( $pid, split( ':', $plist{$pid} ) );

    print "$out_pid\t$out_user\t$out_proc\n";
    }

    cheers,
    --Errin
    Errin Guest

  5. #5

    Default Re: simple windows process list

    Cool! I'm down to 161 bytes now, short enough for a oneliner at the C:\>
    prompt.

    for(grep/\S/,`tasklist /v /nh`){
    chomp;my($p,$i,$u,$t)=unpack'A24A8x56A50x14A*',$_; $p="$p $t"unless$t
    eq'N/A';$p=~s/ +/ /g;$i+=0;$u=~tr/ /_/;print"$i $u $p\n"}

    Don't worry guys, my other scripts are far more readable :-)

    JP


    "Bob Showalter" <com> wrote in message
    news:.. 
    >
    > unpack() with 'A' is handy for extracting and removing trailing blanks in
    > one step.
    >
    > for (grep /\S/, `tasklist /v /nh`) {
    > chomp;
    > my ($proc, $pid, $user, $title) = unpack 'A24 A8 x56 A50 x14 A*',[/ref]
    $_; 


    Jp Guest

Similar Threads

  1. Process Email to Specific Mailing List
    By saxmn1974 in forum Coldfusion - Advanced Techniques
    Replies: 1
    Last Post: April 21st, 09:19 PM
  2. Win32::Process Kill Process in Windows ME
    By Stefan Mueller in forum PERL Modules
    Replies: 3
    Last Post: April 15th, 07:35 PM
  3. 5.3 Process list
    By Tim Traver in forum FreeBSD
    Replies: 0
    Last Post: February 26th, 01:15 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139