package Test_logger; sub new { my $pkg = shift; my $self = { logfile => "/tmp/test_logger.log", # default @_ # merge in user-specified attributes }; open my $fh, ">", $self->{logfile} or die "Cannot open logfile '$self->{logfile}': $!"; $self->{logfh} = $fh; return bless $self, $pkg; } sub message { my $self = shift; my $msg = shift; my $fh = $self->{logfh}; print $fh "$msg\n"; } 1; #### #!/usr/bin/perl use strict; use warnings; use Test_logger; sub test_addition { my $log = shift; $log->message("Inside the subroutine test_addition"); } my $log = Test_logger->new( logfile => "my.log" ); $log->message("Start of program"); test_addition($log);