File locking issue



Hi,
I am trying to save data in an xml file.
What I am seeing is when I enable the file locking commands
the file is completely wiped out.Just zero bytes.

But it works correctly when I remove the file locking commands.

Please help ?? What I am doing wrong .

Regards,
Alok

use XML::Twig;
use Fcntl qw(:DEFAULT :flock);

my $registerFile = "Register.xml" ;
my $twig = XML::Twig->new( pretty_print => 'indented' );

# Save data into the xml file
my $node = XML::Twig::Elt->new(
'User', {'name' => $usr},
XML::Twig::Elt->new( 'email' =>
$email )) ;
$node->paste( last_child => $twig->root ) ;

#use lock to write into the file
sysopen( FH, $registerFile, O_RDWR )
or die "can't open $registerFile: $!";
flock( FH, LOCK_EX)
or die "can't lock filename: $!";
truncate(FH, 0) or die "can't truncate filename: $!";

$twig->parsefile( $registerFile );
$twig->print_to_file( $registerFile ) ;

close(FH)
.