Nath, Alok (STSD) wrote:
> Hi,
Hello,
> 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: $!";
flock() locks the file, it is NOT changing the size.
> truncate(FH, 0) or die "can't truncate filename: $!";
truncate() changes the size of the file, here you are changing the size to
zero bytes!
> $twig->parsefile( $registerFile );
> $twig->print_to_file( $registerFile ) ;
>
> close(FH)
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>