Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: module for file handle control?

by Chief of Chaos (Friar)
on Dec 02, 2002 at 17:07 UTC ( [id://216971]=note: print w/replies, xml ) Need Help??


in reply to module for file handle control?

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Re: module for file handle control?
by valdez (Monsignor) on Dec 02, 2002 at 17:23 UTC

    That code will never work... there is a race condition! Please, please don't use it!!! What happens if something else creates a file with the same $filename after your test?

      What is a race condition>

        From www.linuxfocus.org/English/September2001/article198.meta.shtml:

        The general principle defining race conditions is the following : a process wants to access a system resource exclusively. It checks that the resource is not already used by another process, then uses it as it pleases. The race condition occurs when another process tries to use the same resource in the time-lag between the first process checking that resource and actually taking it over. The side effects may vary. The classical case in OS theory is the deadlock of both processes. More often it leads to application malfunction or even to security holes when a process wrongfully benefits from the privileges another.

        In other words, using the above code:

        while (-e $filename){ $filename.= '.new'; } # while being here, another script can create a file with # the same $filename, causing a bug... open(F, "> $filename");

        To be safe, you should 'lock' the resource and be sure that nothing else can use it. The practical solution is inside File::Temp, that is shipped with Perl since version 5.6.1 (I think)

        Ciao, Valerio

(z) Re^2: module for file handle control?
by zigdon (Deacon) on Dec 02, 2002 at 17:21 UTC
    Wouldn't this be dangerous - race conditions, etc? I'd much rather use something like the suggested File::Temp, which does security checks.

    -- Dan

Re: Re: module for file handle control?
by Chief of Chaos (Friar) on Dec 03, 2002 at 07:27 UTC
    Thanks, to the nice replays.

    It is correct, that other scripts can open this special filename while the other process had found it as 'not there'.
    But in the question, was nothing from multiple uses in the same time. I thought the author of this question (matth) wants to run his script often, without hardcoding the output filename.
    The string, which you add to the filename, can be chosen from the md5 checksum of the content you want to paste ...or the a timepoint in msec...or what ever.
    My reply only shows another 'way', when you don't want to use File::Temp.

    Please be aware that File::Temp can cause problems with the network file system (nfs).
Re: Re: module for file handle control?
by matth (Monk) on Dec 02, 2002 at 17:16 UTC
    Thanks. Where would I put that in relation to the open statement?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://216971]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found