The main part of this script creates some sets for testing similar to those used in the previous example. It uses dclone() from Storable to copy the sets and calls the subroutine to create the Cartesian products. This is necessary since the subroutine is destructive in its use of the sets.

The first step handles the degenerate case. If there are no sets, an array holding an empty array is returned.

The second set saves the first set.

The third step calls the subroutine recursively. It stores the partial results.

The final step is to combine the saved set with the partial results. This is done with a loop within a loop to create new sets by adding each element of the saved set to the sets of the partial results.

The first step of this subroutine is to assume there are no sets and the products are assign an array holding only an empty array. Then the sets are scanned in reverse order. This is just to ensure the result are the same as the previous script.

The partial products are saved and a new array is created to hold the new products.

The inner two loops that follow are the same as those in the previous script. They create the final products for the set.