in reply to Time in Milliseconds or NanoSeconds

You mention that you do not want to create a temporary file but the file you are creating is not that clean either.

Note that File::Temp does not have to "delete" the file. Is that why you were hesistating to use temp files? You can set the behavior using this method   $fh->unlink_on_destroy( 0 );

Do you want the username prefix or is it ok if you don't have that?

-SK

Replies are listed 'Best First'.
Re^2: Time in Milliseconds or NanoSeconds
by ramya2005 (Scribe) on Aug 12, 2005 at 01:34 UTC

    I mention I do not want to create temporary file because what I am dealing with is a text (one of the elements in XML).
    For example say I have an XML file "Filters.xml".
    <filterobj> <filter name="abc"/> <filter name="cde"/> </filterobj>
    My requirement is to read this XML and do the following.
    Convert 'abc' into 'abc.processID.TimeStamp' Convert 'cde' into 'cde.ProcessID.TimeStamp'
    Update the above XML file and Update another XML file which is referring to 'abc' and 'cde'.

    So there is no temp file concept. I have to create unique names to use in the same file.

    The problem now is I am using timestamp in seconds and there could be more than one name generated in the same second with same user provided name. So I asked for some input in creating better unique names and I asked Perl monger's comments on using milli seconds.
    Thanks
      The problem now is I am using timestamp in seconds and there could be more than one name generated in the same second with same user provided name.

      How about using a simple incrementing number in this case? You can use sysopen to do this and avoid race conditions:

      use Fcntl; my $username = "..."; my $filename = $username . time; my $inc = 1; my $fh; until (sysopen $fh, $filename.$inc, O_WRONLY|O_CREAT|O_EXCL) { $inc++; } print $fh time, "\n";
Re^2: Time in Milliseconds or NanoSeconds
by jdporter (Paladin) on Aug 14, 2005 at 17:44 UTC
    More the the point, File::Temp has a function (as Zaxo alludes to) that can generate names without creating an actual file. (The function in question is tmpnam.) Note, however, that uniqueness is not absolutely guaranteed, although it does to a very good job.