use Logfile; use Communication; $log = new Logfile (...); $log->open(...); my $func; $func=sub{ $data=shift; $log->write($data) if($log->is_open()); } $com = new Communication ($func, ...); $log->open(...); $log->write(...) if($log->is_open()); $com->do_something(); $log->close(...); .... package Communication; sub new { my $class=shift; my $log=shift; $log=sub{} if(!($log and ref($log) eq 'CODE')); .... $self->{LOG}=$log; .... bless($self, $class); return $self; } sub do_something { my $self=shift; .... $self->{LOG}->(...); .... } ....