use strict; use warnings; sub recursiveFringe { for( @{ +shift } ) { if( ref $_ ) { recursiveFringe( $_ ) } else { print "$_\n" }; } } sub sameFringe { # create a subthread for each tree executing the above recursiveFringe # in such a way that the output from print above can be read in # while(<...>) in the main thread. # once a difference occurs return FALSE and terminate threads } my @trees = ( [ [ 1, [ [2, [4, 7] ], 5 ] ], [3, [6, [8, 9] ] ] ], [ [ 1, [ [2, [4, 7] ], 5 ] ], [3, [6, [8, 9] ] ] ], [ [ 1, [ [ [2, 4], 7], 5 ] ], [3, [6, [8, 9] ] ] ], [ [ 1, [ [2, [4, 7] ], 5 ] ], [3, [6, [8, [9, 0 ] ] ] ] ], [ [ 1, [ [0, [4, 7] ], 5 ] ], [3, [6, [8, 9] ] ] ], ); print sameFringe( $trees[0], $_ )?"Same\n":"Different\n" for @trees;