Just a guess but maybe you shout use this:
    foreach my $child (sort keys( %{$parent{$pid}} )) {
        dump_process( $child );
    }


Yaron Kahanovitch
----- Original Message -----
From: "Ken Foskey" <[EMAIL PROTECTED]>
To: "Perl Beginners" <beginners@perl.org>
Sent: 16:48:17 (GMT+0200) Asia/Jerusalem שבת 5 מאי 2007
Subject: hash containing a hash, sorting keys help

I cannot get the syntax right for child lookup, Data::Dumper confirms
that I have the structure as I expect (logic may be totally wrong
though).  I going to do a webpage pstree command.

    foreach my $child (sort keys( $parent{$pid} )) {
        dump_process( $child );
    }

Type of arg 1 to keys must be hash (not hash element) at ./visualise.cgi
line 46, near "} )"

Initial $pid is zero and the entry looks like this:

            '0' => {
                     'ntp' => 1,
                     'hplip' => 1,
                     '1' => 1,
                     'klog' => 1,
                     'cupsys' => 1,
                     '101' => 1,
                     'root' => 1,
                     '117' => 1,
                     'avahi' => 1,
                     'user' => 1
                   },


#!/usr/bin/perl -w
# vi:set sw=4 ts=4 et cin:
# $Id:$

=head1 SYNOPSIS
 
Provide a tree function of processes on a unix server
 
   usage: visualise.cgi user=username

=head1 DESCRIPTION

Use the PS command to generate a tree of commands on a Unix server.

=head1 AUTHOR

Ken Foskey

=head1 COPYRIGHT

Copyright 2007 Salmat

=cut

use warnings;
use strict;

use CGI qw(:standard);
use Data::Dumper::Simple;

my ( $line, $user, $pid, $ppid, $c, $stime, $tty, $time, $command);
my $indent = 0;

my %parent;
my %process;

my $user_selected = param('user');

sub dump_process
{
    my $pid = shift;
    $indent++;
    my $spaces = substr( "            ", 0, $indent );
    print "$spaces<li>$process{$pid}</li>\n";
    print "$spaces<ul>\n";
#    foreach my $child (sort keys( $parent{$pid} )) {
#        dump_process( $child );
#    }
    print "$spaces</ul>\n";
    $indent--;
}

open( PS, '-|', 'ps -ef' ) or die "Unable to run PS command $!";

$line = <PS>;  # ignore heading!

while( $line = <PS> ) {
    chomp $line;

    ($user, $pid, $ppid, $c, $stime, $tty, $time, $command) = split( /
*/, $line, 8 );
    if( ! $user_selected or $user_selected eq $user ) {
        $process{$pid} = $command;
        if( $ppid eq "1" ) {
            $ppid = $user;  # group users commands under 1.
            $parent{0}->{$ppid} = 1; # link up to top process.
        }
        $parent{$ppid}->{$pid} = 1; 
    }
}

print Dumper %parent;

$process{0} = "*nix" if( ! $process{0} );

dump_process( '0' );

close( PS );



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to