use strict; use warnings; my $data; #write to filehandle open WH, ">", "test$$.txt" or die $!; _write(*WH, "This is a test\n"); close WH; #read from filehandle into $data open RH, "test$$.txt" or die $!; _read(*RH, \$data); close RH; print $data; sub _write { my $fh = shift; my $data = shift; print $fh $data or die $! } sub _read { my $fh = shift; my $data = shift; $$data .= $_ while <$fh> }