
my $M;
while (<DATA>) {
  push @$M, [split];
}
print "det = ", det($M), "\n";

sub det {
  my $m = shift;
  if (@$m == 1) { return $m->[0][0] }
#  elsif (@$m == 2) { return $m->[0][0] * $m->[1][1] - $m->[0][1] * $m->[1][0] }
  
  my $total = 0;
  my $s = 1;
  for my $r (0 .. $#$m) {
    $total += $s * $m->[0][$r] * det(minor($m, 0, $r));
    $s *= -1;
  }
  return $total;
}

sub minor {
  my ($m, $r, $c) = @_;
  my $rows = copy($m);
  splice @$rows, $r, 1;
  for (@$rows) {
    splice @$_, $c, 1;
  }
  $rows;
}


sub copy {
  my $m = shift;
  [map [@$_], @$m];
}

__END__
-2 2 -3
-1 1 3
2 0 -1
