use strict; use warnings; use Data::Dumper; sub openRandom { my $fh = { name => (int rand 1000) . '.txt' }; return $fh if open($fh->{'handle'}, ">$fh->{'name'}"); } print Dumper(openRandom());