my %paths = ( LOG => '/path/to/log.txt', ERROR => '/path/to/errorlog.txt', INFO => '/path/to/info.txt', ); my %fh; # key: token, value: filehandle ... if (...) { $text = ... append('LOG',$text); } else { ... } sub append { my ($token, $text) = @_; my$ fh unless (exists $fh{$token} ) { open my $fh, '>>', $paths{$token} or die $!; $fh{$token} = $fh; } print {$fh{$token}} $text; }