Consider this equally untested code:
sub add_stuff_to_file {
my ($filehandle, $stuff) = @_;
my $i = $stuff->iterator();
while ($i->hasNext()) {
print $filehandle $i->next();
}
}
Or if you want similar error messages:
sub add_stuff_to_file {
my ($filehandle, $stuff) = @_;
($stuff->can('iterator')) || die "Stuff must be iterable";
my $i = $stuff->iterator();
($i->can('hasNext') and $i->can('next'))
|| die "Stuff's iterator must be an Iterator";
while ($i->hasNext()) {
print $filehandle $i->next();
}
}
What is the upside of checking isa when you're deliberately trying to cast a wide net? |