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

I don't know if my question will make sense, but the problem I have is that my subroutine once it gets called return a value like:
1- Car Parts - Log in: 57- 3- DI activity: 13 3- FA activity: 13 3- FA +I activity: 4 3- Login activity: 23 3- Not Found activity: 4
This information gets printed to the browser, but when the subroutine gets called again I would like to keep this information on the screen by saving to another variable or into a buffer and get the new value also printed to the screen. Therefore every time the program runs and the subroutine gets called I will not loose the previous value printed to the screen.

I am having a problem trying to do that, it would be nice if someone could give me some help with this. If it is possible!!

Thanks very much!

janitored by ybiC: Retitle from "Buffer Question" for descriptiveness, and wee tiny format tweaks for legibility

Replies are listed 'Best First'.
Re: Accumulate results from multiple CGI calls for browser viewing
by Belgarion (Chaplain) on Apr 22, 2004 at 18:38 UTC

    HTTP is a stateless protocol. Whenever someone views your page they will get whatever your program outputs. If you want to display a running log of lines, you will need to store the information somewhere (a log file usually) and then display it.

    Update: After messaging with dragonchild for a bit, I think I need to clarify what I think the OP is asking.

    From what I can undertstand, the OP has a subroutine that generates new data each time it's run. This information is displayed on a web page. This is where I'm a little confused. Does the OP want to display the old information in addition to the new information, or just display the new information, but keep a copy of the old information somewhere else?

    Maybe I'm just not thinking too well today, but dragonchild has me questioning what I'm reading in the question. :)

Re: Accumulate results from multiple CGI calls for browser viewing
by dragonchild (Archbishop) on Apr 22, 2004 at 18:40 UTC
    JavaScript is your friend, here. Or, you can re-output it. Or, you can use frames. There's a number of solutions.

    The stateless answer isn't quite correct. The issue is that the page completely refreshes every click, unless you specifically tell it not to.

    OpenThought is also a possible solution.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Re: Accumulate results from multiple CGI calls for browser viewing
by sgifford (Prior) on Apr 22, 2004 at 21:15 UTC

    As others have said, the problem is that when the user visits the page again, your script is run from scratch, and doesn't have any information about what happened last time it ran.

    The most common way to deal with this is with sessions. When a user runs your script for the first time, generate some kind of unique identifier for them. Either store that as a hidden CGI parameter, put it into the URL as a parameter or path component, or stick it in a cookie. When your script runs again, retreive that token; you can use it to track the same user between multiple runs of your script, and do things like store information about previous runs in a file named for the session key.

    There are lots of modules to do this. Search CPAN for Session for a few.

Re: Accumulate results from multiple CGI calls for browser viewing
by TomDLux (Vicar) on Apr 22, 2004 at 22:35 UTC

    If you make the page be a form, you can assign the currently displayed text to a textarea, with editing disabled. Each time you click the <emSubmit button, the text is passed back to the cgi script which can invoke the routine and extend the displayed text.

    Alternately, you can use Java or Javascript to connect to the server secretly without refreshing the page. Eric Andreychek deescribed soemthing of the sort, OpenThought & OpenPlugin, at the Ottawa YAPC::Canada in 2003.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA