in reply to Re: Bad code from the trenches
in thread Bad code from the trenches
First off, I feel the need to be a little defensive and say "This ain't *my* code!" It was in a module written by the head developer of my current client.
Ok, with that out of the way....
Your suggestion about picking from a list of acceptable files is a good one, but wouldn't work in this case (because of information that was not given in the original post). This code was used in a CGI environment and $dest was derived from a CGI param, but this wasn't code that was directly user-interactive, it was called from a PHP script.
As to testing files with -e, -f, and -d before opening them...remember that doing this:
means that you have a race condition. If someone modifies or deletes $file in the moment between lines 1 and 2, the tests were all for nothing. A better solution is to do something like this (almost directly lifted from perlopentut):line 1) if ( -e $file && -f $file && -s $file < $ok ) { line 2) ...open file here.... }
use 5.004; use Fcntl qw(:DEFAULT :flock); open(FH, '<', $file) or die "can't open $file: $!"; unless (flock(FH, LOCK_SH | LOCK_NB)) { local $| = 1; print "Waiting for lock..."; flock(FH, LOCK_SH) or die "can't lock $file: $!"; print "got it.\n" } # now read from FH
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Bad code from the trenches
by dragonchild (Archbishop) on Mar 14, 2005 at 14:36 UTC | |
by Whitehawke (Pilgrim) on Mar 14, 2005 at 20:23 UTC |