I love Perl. It lets me get the job done, it is wonderful, it is fantastic, it is - at times - a headache.
London.pm answered my question the other day but the sinking feeling in my gut hasn't gone away and its starting to get me down.
It was a simple problem really: I needed to dispatch a method from each of the objects in a list contained within another object.
Simple.
foreach my $object ( @{ $self->objectlist() } ) { $object->method(); }However, due to a lack of dilligence on my part I was occasionally putting things into the list that didn't belong. Simple solution to that is stop doing it. Well, yes, but it wasn't just me, it was also other people using the classes that I was writing.
use Scalar::Utils qw ( blessed );(Thanks Richard)
sub addObject (MyClass $object) { push @{$self->{objectlist}}, $object; }and be more or less guaranteed to be having the right thing happen is a far easier thing (and therefore more desirable) than:
use Scalar::Utils qw ( blessed );And the more I think about it, the less I think that I'm wrong, so I'm trying not to (think about it, that is).
sub addObject { my $self = shift; my $object = shift; if (blessed($object) && $object->isa('MyClass')) { push @{$self->{objectlist}}, $object; } }