Hello,
I've written a script to print the Process Tree. To be able to print it nicely I decided to use Data::TreeDumper.
Here you can see the code:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Tree::Simple; use Tree::Simple::View::HTML; use Data::TreeDumper; use Win32::Process::Info; use 5.010; use feature qw(say); ############################################################ # Get Subprocesses ############################################################ my $pi = Win32::Process::Info->new(); my %sub_pids = $pi->Subprocesses(); remove_win32_subprocess_recursion(\%sub_pids); add_win32_subprocess_root_entry(\%sub_pids); # e.g. 'ROOT' => [204,5944,3824,0] sub add_win32_subprocess_root_entry { my( $ref_subs ) = @_; my @total_sub_pid_list; for my $ref_sub_pid_list (values %$ref_subs) { for my $sub_pid (@$ref_sub_pid_list) { push(@total_sub_pid_list, $sub_pid); } } my @root_pid_list; for my $pid (keys %$ref_subs) { unless( $pid ~~ @total_sub_pid_list ) { push(@root_pid_list, $pid); } } $ref_subs->{'ROOT'} = [@root_pid_list]; } # remove win32 subprocess recursions # e.g. '0' => [0,4] # --> '0' => [4] sub remove_win32_subprocess_recursion { my( $ref_subs ) = @_; while( my ($pid, $ref_sub_pid_list) = each %$ref_subs ) { if( $pid ~~ $ref_sub_pid_list ) { @$ref_sub_pid_list = grep(!/$pid/, @$ref_sub_pid_list); } } } ############################################################ # Create Process Tree ############################################################ my $root_pid = 'ROOT'; my $pid_tree = Tree::Simple->new($root_pid, Tree::Simple->ROOT); build_pid_tree($root_pid, $pid_tree); $pid_tree->DESTROY(); sub build_pid_tree { my ($pid, $pid_tree) = @_; if( scalar @{ $sub_pids{$pid} } == 0 ) { return; } for my $item ( @{ $sub_pids{$pid} } ) { build_pid_tree( $item, Tree::Simple->new($item, $pid_tree) ); } } ############################################################ # TRAVERSE TREE ############################################################ print "Relevant Information:\n"; $pid_tree->traverse( \&print_tree ); print "\n"; sub print_tree { my ($tree) = @_; print (("\t" x $tree->getDepth()), $tree->getNodeValue(), "\n"); } ############################################################ # View Tree (Data::TreeDumper) ############################################################ print "Unfiltered Tree:Simple tree:\n"; print DumpTree($pid_tree, 'Win32 Process Tree'); print "\n"; print "Filtered Tree::Simple tree:\n"; print DumpTree($pid_tree, 'Win32 Process Tree', FILTER => sub { my $s = shift; if('Tree::Simple' eq ref $s) { # TODO } return(Data::TreeDumper::DefaultNodesToDisplay($s)) ; });
The script is detecting all running processes. Then all PIDs are put into a tree (by using Tree::Simple).
To show you what I want to have I traversed the tree and printed it.
Now I want to do the same printing with Data::TreeDumper. But I am not getting it. I do not really understand the filters. I read the help, but I don't get it.
The input is a Tree::Simple process tree. But this tree has of course a lot of more information in which I am NOT interested. You can see this tree by looking at the output directly behind print "Unfiltered Tree:Simple tree:\n";.
After the print "Filtered Tree::Simple tree:\n"; I try to only print the content of the nodes. But I do not understand the filter.
I know that the return value of the filter is consisting of three parts. A node type, an eventual new structure and a list of 'keys' to display.
For example I tried the following to only get the nodes:
print DumpTree($pid_tree, 'Win32 Process Tree', FILTER => sub { my $s = shift; if('Tree::Simple' eq ref $s) { return( 'SCALAR', undef, $_{'node'} ); } return(Data::TreeDumper::DefaultNodesToDisplay($s)) ; });
But I get a weird result and some warnings:
Filtered Tree::Simple tree: Win32 Process Tree blessed in 'Tree::Simple' `- = undef [S1]
I also tried it with an array instead of the scalar. But I also get weird results.
Thanks for your help.
Greetings,
Dirk
In reply to Help to understand the Data::TreeDumper Filters by Dirk80
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |