priti has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, Can some one clear my doubt i.e If after i write to session should i flush() it always?? What exactly flush() function do ...Overwrite the previously created file or simply append the new registered parameter by $session->param('k','v'); ? Regards

Replies are listed 'Best First'.
Re: CGI::Session flush() functions
by moritz (Cardinal) on Aug 20, 2008 at 10:59 UTC
    flush() writes the session object to disk (or database, or whatever you use as a storage backend).

    When you store a new param in a session it will be lost after the end of your scriptunless you call flush.

      I think it actually tries to flush when the Session object goes out of scope. I believe it fails to flush only when the script dies unexpectedly.

      UPDATE: No doubt. Explicitly flushing is better in every way. I was just saying that there are occasions where it will flush on its own, not terribly reliable, but they exist.

      -Paul

        Not only when it dies unexpectedly. I had a whole lot of cases of normal exits where it also failed to flush the session, which is why I recommend to always manually call $session->flush().

        The CGI::Session maintainers know of these problems, and encourage you to not rely on auto flushing.