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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.