in reply to Database backup submission
if I've done something unwise or left something amissYou're not checking the return values of both of your file opens. This is generally a bad thing, because reading from or writing to an un-opened filehandle yields undefined behaviour. Note that if your first open fails, you'll still attempt to do the database copy. Is that desireable?
It also seems to me that you could make your file-copy section much more efficient if you considered the File::Copy module. For example:
### Back up the file, if it exists use File::Copy; if(-e $file{1}) # if the file exists { unless(copy($file{1}, $file{2})) { print "Failed to copy file: $!"; exit; # unless you want to do the copy anyway } } ### Copy the database
Alternately if you like hand-rolled solutions, consider the following:
Hope that helps. :) (Oh, and note that $file1_data, or $filecontents in my example, need not be global at all.)if(-e $file{1}) # if the file exists { open FILE1, "< $file{1} or die "Failed to open $file{1}". " for reading: $!"; local $/ = ""; # prepare to read the whole # file in at once my $filecontents = <FILE1>; close FILE1; open FILE2, "> $file{2} or die "Failed to open $file{2}". " for writing: $!"; print FILE2 $filecontents; close FILE2; }
jarich
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Database backup submission
by fireartist (Chaplain) on Apr 30, 2002 at 10:58 UTC |