I must be blind... 
Author Message
 I must be blind...

I know I'll take a beating for the unchecked opens etc.... but I'll ask
anyway. I'm trying (unsuccessfully) to push a new user into a text file,
but I have screwed up the the textbook example somehow.... I've tried a
while (<>) loop etc. to no avail.

Any clues large or small appreciated. Except that complete car reference
witht the ingear(), push() yada yada yada.....

---------------- SNIPPET --------------------
open(READ,$filename);
open(TEMP,">$tempfile");



flock (TEMP, $UNLOCK);
close(READ);
close(TEMP);
unlink($filename);
rename($tempfile,$filename);

---------------------------------------------

Thanks, and Happy Holidays
Trent



Thu, 07 Jun 2001 03:00:00 GMT  
 I must be blind...
: I know I'll take a beating for the unchecked opens etc.... but I'll ask
: anyway. I'm trying (unsuccessfully) to push a new user into a text file,
: but I have screwed up the the textbook example somehow.... I've tried a
: while (<>) loop etc. to no avail.

: ---------------- SNIPPET --------------------
: open(READ,$filename);
: open(TEMP,">$tempfile");

The first thing to do is, yes, to check the open status in order to find
out whether the problem is really in your code or in your directory
structure, permissions, etc.

: flock (TEMP, $EXCLUSIVE);

:      

How do you know for sure that that's where the problem is?  Did you print

order to see what, if anything, got pushed?

I don't see anything terribly wrong there, though I ought to point out
that you should make sure that the content of $newuser is terminated with
a newline; if it isn't, multiple invocations of your script will keep
appending to the last line rather than appending additional lines.


: flock (TEMP, $UNLOCK);

Not a good idea to unlock a file before closing it; newer versions of
Perl have some built-in defensive code to eliminate some race conditions
that could arise there, but it's good practice to avoid letting the
situation arise.

: close(READ);
: close(TEMP);

Check the results of those closes, too.  If, for some reason, you were
unable to open TEMP for writing, you would *not* want to delete the original
file and then try to replace it with the file that doesn't exist because you
couldn't create it, would you?

: unlink($filename);
: rename($tempfile,$filename);

And check these too.



Thu, 07 Jun 2001 03:00:00 GMT  
 I must be blind...

Quote:

>I know I'll take a beating for the unchecked opens

That's like coming into rec.motorcycles and saying ``I keep getting
really badly injured, is there any advice anyone can offer me to help
with this?  By the way, I don't wear a helmet.''

If you don't check your calls for failure, you're going to lose big
time; I don't know why you bothered with some subtle little thing like
flock when you didn't bother with the really obvious thing of making
sure your opens succeed.  

Eric Bohlman already saw most of the problems I saw, but there's also
this one:

Quote:
>unlink($filename);
>rename($tempfile,$filename);

It's silly to unlink the original before you rename the temporary
file.  That leaves a small window during which the original file is
missing.  Just do the `rename'; if it's successful, the original file
will be removed anyway; that's the whole point of `rename'.

Also I'll just assume that you created the flock constants correctly:

Quote:
>flock (TEMP, $EXCLUSIVE);
>flock (TEMP, $UNLOCK);

because I see that you didn't get the from the usual place, which is
the Fcntl module.


Thu, 07 Jun 2001 03:00:00 GMT  
 I must be blind...
Can you be more specific when you say "it doesn't work"?  It (the push)
should.  But there are many other things in your code that might not be ;) .

~ M. Kahn



Thu, 07 Jun 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. newbie-perl or why am i blind to this simple

2. Blind Vigilantes

3. Blind Vigilantes

4. Perl for the Blind

5. Blind Vigilantes

6. Syntax blind-spot

7. Blind Vigilantes

8. Blind copy with sendmail

9. keyboard input in BLIND MODE

10. Regexp - I must be blind

11. Blind Vigilantes

12. Who am I? What am I doing? (looking for input)

 

 
Powered by phpBB® Forum Software