in reply to Re^2: Correct code for deleting old file?
in thread Correct code for deleting old file?

Your code contains lot of bad practices:

Also, you seem to go through great pains to handle URLs and to issue HTTP and FTP requests using wget. Perl has LWP, so you do not have to mess with the shell and external programs at all. Look first at LWP::Simple, that should nearly be sufficient to replace wget. If you need more control, look at LWP::UserAgent. As you seem to write a spider, also have a look at LWP::RobotUA.

Perl also can decompress gzip files and unpack tar files, if you like, even both in one package, Archive::Tar, and of course without having to mess with the shell.

For URL handling, look at URI and its helpers.

And for my personal taste, the getFile() function is about 200 lines longer than it should be. Too much code for one function, too deeply nested, too many variables. As a rule of thumb, a function should not exceed one screen, because otherwise, you will get lost in the code. At my university, a screen was defined as a VT420, i.e. 25 lines of no more than 80 characters, but in the real world, my editor shows about 40 lines of about 110 characters. Consider splitting the function into smaller parts.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^4: Correct code for deleting old file?
by Anonymous Monk on Jun 19, 2012 at 08:18 UTC

    Thanks a million. However that sounds like a complete rewrite would be ideal.

    However I should point out that the scripts aren't mine (my programming expertise is with REALbasic), they are commercial (which might explain why they are so poorly documented) and I have basically no knowledge of Perl, I'm just the poor sucker who got stuck with setting up our new Mascot PC (the guy who did it the last time has long since left).

    Our Mascot version is a few years old, so the database update script doesn't work right with the new databases (there have been some changes), so the script needed updating.

    I managed to do most of the required changes, but there is an acknowledged bug in there in that a certain file (speclist.txt) needs to be replaced, however the download does not replace it, it just adds it to the folder (as speclist1, speclist2, etc). So their solution is that we should delete the file manually before running the script - yeah, right, who is going to remember that as (a) the script is run every month only, and (b) is being called automatically.

    So I try to add a file deletion routine to the script:

    # # delete old speclist # if ( $matchName eq "speclist.txt" ) { if (-e "$local_taxonomy_directory/speclist.txt") { # does the s +peclist file exist unlink("$local_taxonomy_directory/speclist.txt") #delete + it } else { &lastWords("Failed to delete original speclist"); } } #

    but that doesn't seem to work and I don't know why ... yet

    Thanks

    Markus