in reply to Re: Datafile doesn't update before being read
in thread Datafile doesn't update before being read

Thanks for the reply. I was afraid of that. The script is kind of long, and I'm not sure what is best thought relevant, but here we go. '$file' refers to the datafile I'm working with.
my $do; # function # Get delete_links input (which links should be deleted?) if ( $input =~ m/Delete\+all\+checked\+links%21$/ ) { $do = 'delete'; $input =~ s/(.*?)&delete_links=.*$/$1/ ; @del_links = split /&/, $input; @del_links = map { s/(\d+?)=checked/$1/ ; $_; } @del_links; @del_links = reverse @del_links; # so that last ones are done f +irst } [...] my @metadata = &build_metadata_array; my @strmeta = &parse_metadata(@metadata);

I am hoping you won't need to see the latter subroutines. The first simply reads in a datafile ($file), the second creates an array of references to the split data records. So, for example, @{$strmeta[0]} might consist of ('ap.1', 'ap.2', 'nyt.4', 'some random text about those ids').

if ($do eq 'delete') { foreach my $num ( @del_links ) { splice (@strmeta, $num, 1); } } [...] # Rewrite metadata/$file open (META, ">metadata/$file"); foreach my $rec ( @strmeta ) { my $summary = pop @{$rec}; my $ids = join (/,/, @{$rec}); print META "$ids\n$summary\n\n"; } close META;
Is that enough?

Replies are listed 'Best First'.
Re: Datafile doesn't update before being read
by Abigail-II (Bishop) on Jun 07, 2002 at 17:00 UTC
    Well, I don't know why it's failing (you didn't show us the code that reads the data for instance), but I do know this is going to fail if you are getting two requests in a short time. The first thing that happens when you open the file for write is that it's getting truncated. What if another instance is reading from the file? What if another instance is also writing? Your data will get corrupted. Also, you open the file, and you don't check the return value. What if the file cannot be opened? Same for the close, you might get an error here (disk full, for instance). You don't check.

    Abigail