<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#usr/bin/perl
#version 1.0 by Eddy Seager edward.seager@googmail.com

use strict;
use warnings;
use Data::Dumper;

my $inputSize = 3;

#change to be your input
my @input = (1, 2, 3, 4);
my @result = ();
my %args = (input =&gt; \@input, result =&gt; \@result);
#print Dumper($args{input});
&amp;perm(\%args);


 sub perm
{
	my ($argsRef) = @_;
	#print Dumper($argsRef); 
	my @input = @{$argsRef-&gt;{input}};
	my $result = $argsRef-&gt;{result};
	
	for (my $i = 0; $i &lt; scalar(@input); $i++)
	{
		my $value = $input[$i];
		push  @{$result}, $value;
		#print Dumper(@input) ."\n";  
		
		#if size of input array is 1
		if ( scalar(@input) == 1)
		{
			#note: passing by value
			&amp;printArray(@{$result});
			
			#remove last element from result array
			pop @{$result};
			#print Dumper(@result) ."\n"; 
	  }
	  else
	  {
	  	#recurse on the input array (removing $value)
	  	#print $i;
	  	my @inputR = @input;
	  	splice @inputR, $i, 1;
	  	#print Dumper(@inputR) ."\n"; 
	    &amp;perm({input =&gt; \@inputR, result =&gt; $result});
	    
	    #remove element $value from result array
	    pop @{$result};
	    
	  }
  }
}

sub printArray
{
  print $_[0];
 	shift @_;
  
  foreach my $i (@_)
  {
    print ", $i";
  }
  
  print "\n";
}</pre></body></html>