G Lams wrote:
>
> I'm new to perl (that's why I'm here :-). I'm building a few cgis allowing
> me to add, modify, delete users in a text file (perl version 5.8.0 on a
> redhat 8 machine).
> So far, I've been able to create the form and the perl scripts to add
> users and verify their existence.
> I'm now trying to delete one user/record inside my text file.
> You will find below the script I wrote.
> Actually the temporary file is created the way it should be but I didn't
> succeed in renaming it to users.dat, saying "no such file or directory".
> This should be not possible as the script succeeds in opening users.dat
> and reading from it.
>
> I know that a nicer solution is to use Tie::File but I have to say that
> from the documentation I found I don't really understand how to do it :-(
>
> Thanks in advance for any help
>
> Gael
> # value we will read from the html form
> my $name=$ARGV[0];
>
> $data_file="users.dat";
>
> # we will read the database record by record, copy each recor to the
> temporary file
> # and simply forget to write the record to be deleted.
>
> $tmp_file="users.tmp";
>
> open(DAT, "<$data_file") || die ("Could not open users file!");
> open(TMPDAT, ">$tmp_file") || die ("Could not open temporary file!");
>
> while (<DAT>)
> {
> # extract the username (the first field) from the record
> my ($username)=split(/\|/,$_);
>
> # test the name agains the username
> if ($name eq $username) {
>
> # we've found the record to delete, so skip it and move to next record
> next;
> }
> # write the original record out to the temporary file
> print TMPDAT $_ or die "Error writing $tmp_file: $!\n";
> }
>
> close DAT or die ("closing the users file!\n");
> close TMPDAT or die ("closing $temp_file: $!\n");
>
> # we delete the old file
> unlink $data_file or die ("Can't delete old $data_file: $!\n");
>
> # and rename the new file to replace the old one.
> rename ($temp_file, $data_file) or die ("Can't rename '$tmp_file' to
> '$data_file': $!\n");

Hi G.

(Be nice to know your name?)

Your problem is mainly one of far too much junk in your source.
Comments should be present only if the code is obscure or devious
for a non-obvious reason. Look at this code (which I had to write
to understand what you were trying to do!)

  use strict;
  use warnings;

  my $name = shift;

  my $data_file = 'users.dat';
  my $tmp_file = 'users.tmp';

  open DAT, $data_file or die $!;
  open TMPDAT, "> $tmp_file" or die $!;

  while (<DAT>) {
    my ($username) = split /\|/;
    print TMPDAT $_ unless $name eq $username;
  }

  close TMPDAT or die $!;
  close DAT or die $!;

  unlink $data_file or die $!;

  rename $temp_file, $data_file or die $!;

It may not be what you meant to write, but it's semantically
nearly identical (yes, except all that die text). But

  use strict;   # always
  use warnings; # usually

gave me the error

  Global symbol "$temp_file" requires explicit package name

showing straight away that your problem is that you've used
$tmp_file initially, and $temp_file later on. Now isn't that
easy?

BTW, ignore stuff like Tie::File and File::Slurp unless you need
them. I have never had an application that did.

Cheers,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to