NAME

Data::TreeDumper::Utils - A selection of utilities to use with Data::TreeDumper

SYNOPSIS

use Data::TreeDumper::Utils qw(:all) ;

DumpTree
  (
  $requirements_structure,
  'Requirements structure:',
  FILTER => \&first_nsort_last_filter,
  FILTER_ARGUMENT => {...},
  ) ;

DumpTree
  (
  $ixhash_hash_ref,
  'An IxHash hash',
  FILTER => \&no_sort_filter,
  ) ;

DumpTree
  (
  $structure,
  'sorted',
  FILTER => 
    CreateChainingFilter
    (
    \&remove_keys_starting_with_A,
    \&hash_keys_sorter
    ),
  ) ;

DumpTree
  (
  $structure,
  'filter_class_keys example:',
  FILTER => filter_class_keys(T1 => ['A'], 'HASH' => [qr/./],),
  ) ;

DumpTree(get_caller_stack(), 'Stack dump:') ;

DESCRIPTION

A collection useful sorting filters and utilities that can be used with Data::TreeDumper. You can also study the source if you examples of how to write filters.

SUBROUTINES/METHODS

first_nsort_last_filter()

This filter will apply to all hashes and object derived from hashes, it allows you to change the order in which the keys are rendered.

print DumpTree
  (
  $structure,
  'Structure:',
  
  # force specific keys to be rendered last
  FILTER => \&first_nsort_last_filter,
  FILTER_ARGUMENT =>
    {
    AT_START => ['ZZZ'],
    AT_END => [qr/AB/],
    },
  ) ;

generates:

Structure:
|- ZZZ = 1  
|- A = 1  
|- B = 1  
`- ABC = 1  

Arguments

The arguments are passed through the call to Data::TreeDumper in the FILTER_ARGUMENT option. FILTER_ARGUMENT points to a hash reference with the possible following keys. All the keys are optional.

Each key is an array reference containing a list of regexes or strings. Keys matching the regexes or string will be sorted in the category in which the matching regex or string was declared. The categories are, in priority order:

  • AT_START - the keys that should be rendered first

  • AT_END - the keys that should be rendered last

  • non categorized - the keys that are rendered between AT_START and AT_END. Any key that doesn't match a regex or a string will automatically be in this category

Note that if multiple keys belong to a category, they will be sorted by Sort::Naturally.

Returns - the keys sorted according to the defined categories.

See - Filters in Data::TreeDumper.

[p] first_nsort_last(AT_START => [regex, string, ...], AT_END => [regex, string, ...], DATA => [keys to sort] )

Implementation of first_nsort_last_filter key sorting.

Arguments

  • At_START - a reference to an array containing regexes or strings

  • AT_END - a reference to an array containing regexes or strings

  • DATA - a reference to an array containing the keys to sort

Returns - the sorted keys

no_sort_filter()

A hash filter to replace the default Data::TreeDumper filter which sorts hash keys. This is useful if you have a hash based on Tie::IxHash, or equivalent, that keep the key order internally.

print DumpTree
  (
  $ixhash_hash_ref,
  'An IxHash hash',
  FILTER => \&no_sort_filter,
  ) ;

Arguments - none

Returns - hash keys unsorted

hash_keys_sorter()

When no filter is given to Data::TreeDumper, it will sort hash keys using Sort::Naturally. If you create your own filter or have chaining filters, you will have to do the sorting yourself (if you want keys to be sorted) or you can use this filter to do the sorting.

  # Remove keys starting with A, return in keys in the order the hash returns them
  DumpTree($s, 'not sorted', FILTER => \&remove_keys_starting_with_A,) ;
  
  # Remove keys starting with A, sort keys
  DumpTree
    (
    $s,
    'sorted',
    FILTER => CreateChainingFilter(\&remove_keys_starting_with_A, \&hash_keys_sorter),
    ) ;
	

Arguments - none

Returns - the sorted keys

filter_class_keys($class => \@keys, $class => \@keys,, ...)

A filter that allows you select which keys to render depending on the type of the structure elements. This lets you filter out data you don't want to render.

Note: this filter does not sort the keys!

  package Potatoe ;
  
  package BlueCongo;
  @ISA = ("Potatoe");
  
  package main ;
  
  use strict ;
  use warnings ;
  
  use Data::TreeDumper ;
  
  my $data_1 = bless({ A => 1, B => 2, C => 3}, 'T1') ;
  my $data_2 = bless({ A => 1, B => 2, C => 3}, 'T2') ;
  my $data_3 = bless({ A => 1, B => 2, C => 3}, 'T3') ;
  my $blue_congo = bless({IAM => 'A_BLUE_CONGO', COLOR => 'blue'}, 'BlueCongo') ;
  
  print DumpTree
    (
    {D1 => $data_1, D2 => $data_2, D3 => $data_3, Z => $blue_congo,},
    'filter_class_keys example:',
    
    FILTER => filter_class_keys
      (
      # match class containing 'T1' in its name, show the 'A' key
      T1 => ['A'],
      
      # match T2 class, show all the key that don't contain 'C'
      qr/2/ => [qr/[^C]/], 
	
      # match BlueCongo class via regex
      # qr/congo/i => [qr/I/],
      
      # match BlueCongo class
      # BlueCongo => [qr/I/], 
      
      # match any Potatoe, can't use a regex for class
      Potatoe => [qr/I/],
    
      # mach any hash or hash based object, displays all the keys
      'HASH' => [qr/./],
      ),
    ) ;

generates:

filter_class_keys example:
|- Z =  blessed in 'BlueCongo'  [OH1]
|  `- IAM = A_BLUE_CONGO  [S2]
|- D2 =  blessed in 'T2'  [OH3]
|  |- A = 1  [S4]
|  `- B = 2  [S5]
|- D3 =  blessed in 'T3'  [OH6]
|  |- A = 1  [S7]
|  |- C = 3  [S8]
|  `- B = 2  [S9]
`- D1 =  blessed in 'T1'  [OH10]
   `- A = 1  [S11]    

Arguments

A list of:

  • $class - either a regex or a string.

  • \@keys - a reference to an array containing the keys to display. The keys can be a string or a regex.

Returns - the keys to render

get_caller_stack($levels_to_dump)

Creates a data structure containing information about the call stack.

s1() ;

sub s1 { my $x = eval {package xxx ; main::s2() ;} ; }
sub s2 { s3('a', [1, 2, 3]) ; }
sub s3 { print DumpTree(get_caller_stack(), 'Stack dump:') ; }

will generate this stack dump:

Stack dump:
|- 0 
|  `- main::s1 
|     |- ARGS (no elements) 
|     |- AT = try_me.pl:20 
|     |- CALLERS_PACKAGE = main 
|     `- CONTEXT = void 
|- 1 
|  `- (eval) 
|     |- AT = try_me.pl:24 
|     |- CALLERS_PACKAGE = main 
|     |- CONTEXT = scalar 
|     `- EVAL = yes 
|- 2 
|  `- main::s2 
|     |- ARGS (no elements) 
|     |- AT = try_me.pl:24 
|     |- CALLERS_PACKAGE = xxx 
|     `- CONTEXT = scalar 
`- 3 
   `- main::s3 
      |- ARGS 
      |  |- 0 = a 
      |  `- 1 
      |     |- 0 = 1 
      |     |- 1 = 2 
      |     `- 2 = 3 
      |- AT = try_me.pl:29 
      |- CALLERS_PACKAGE = main 
      `- CONTEXT = scalar 
      

Arguments

  • $levels_to_dump - the number of level that should be included in the call stack

Returns - the call stack structure

BUGS AND LIMITATIONS

None so far.

AUTHOR

Nadim ibn hamouda el Khemir
CPAN ID: NH
mailto: nadim@cpan.org

LICENSE AND COPYRIGHT

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Data::TreeDumper::Utils

You can also look for information at:

SEE ALSO