use strict; use warnings; # call as: # full_peal( \@remaining_bells, built-up-path-in-tree ) sub full_peal { my $bells_ar = shift; # @_ now contains the path so far if ( @$bells_ar == 0 ) # we've reached a "leaf" { local( $\, $, ) = ( "\n", " " ); print @_; # or whatever you want to do with this permutation return; } # pull out each item in turn for my $i ( 0 .. $#{$bells_ar} ) { # make a local destructable copy my @bells = @{$bells_ar}; # pull out the item my $bell = splice @bells, $i, 1; # recurse, with this item added to the branch full_peal( \@bells, @_, $bell ); } } my @bells = reverse( 1..3 ); # descending order # begin, with an empty tree full_peal( \@bells );