use IO::File (); # assuming LOCK_FILE is a constant with the filename my $fh = IO::File->new( LOCK_FILE, 'w' ); # now you can pass $fh directly to your sub # as in code_lock( $fh ); # sub code_lock { my $fh = shift(); } #### use Symbol(); my $fh = Symbol::gensym(); open($fh,">".LOCK_FILE); # pass it as in the above