my ($list) = @_; my @clones; # check that $list implements # the Iterable interface if ($list->isa("Iterable")) { # the Iterable interface implements # the iterator method, so we call it my $iterator = $list->iterator(); # now make sure that the $iterator # returned actually implements the # Iterator interface ($iterator->isa("Iterator")) || die "bad object interface : not Iterator"; # now that we know we have a proper # Iterator, we can be assured it will # implement the hasNext and next methods # so we can use them with confidence in # what they will do. while ($iterator->hasNext()) { my $item = $iterator->next(); # now after getting the $item from the # Iterator, we want to clone it (if possible) # and so we first check to see if the $item # implements the Cloneable interface, which # tells us that the object will clone itself # when the method clone is called push @clones, $item->clone() if $item->isa("Cloneable"); } } # at this point @clones is filled with all # the items in the given list that are able # to be properly cloned