#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 => \@input, result => \@result);
#print Dumper($args{input});
&perm(\%args);


 sub perm
{
	my ($argsRef) = @_;
	#print Dumper($argsRef); 
	my @input = @{$argsRef->{input}};
	my $result = $argsRef->{result};
	
	for (my $i = 0; $i < 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
			&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"; 
	    &perm({input => \@inputR, result => $result});
	    
	    #remove element $value from result array
	    pop @{$result};
	    
	  }
  }
}

sub printArray
{
  print $_[0];
 	shift @_;
  
  foreach my $i (@_)
  {
    print ", $i";
  }
  
  print "\n";
}