Question on using perl in a cgi script

Discussion in 'Perl Misc' started by q-rious, Dec 14, 2011.

  1. q-rious

    q-rious Guest

    This is a question about using perl in cgi. If this is not the proper perl forum, my apologies.

    I want to build a cgi script using perl that will continously loop through the following:

    a) read from a line from a one line file
    b) draw something on the screen based on that value (using gd)

    I can do this for one iteration of the loop. But how do I update the browser screen for multiple iterations? For other restrictions, I can not reload the entire page each time the script goes through the loop. I can only update the drawing (Step b).

    Thanks in advance for your help.
     
    q-rious, Dec 14, 2011
    #1
    1. Advertising

  2. q-rious <> writes:

    > This is a question about using perl in cgi. If this is not the proper
    > perl forum, my apologies.
    >
    > I want to build a cgi script using perl that will continously loop
    > through the following:
    >
    > a) read from a line from a one line file b) draw something on the
    > screen based on that value (using gd)
    >
    > I can do this for one iteration of the loop. But how do I update the
    > browser screen for multiple iterations? For other restrictions, I can
    > not reload the entire page each time the script goes through the
    > loop. I can only update the drawing (Step b).
    >

    You can't do that with static HTML and CGI, period.

    The only way to do this is using some sort of client side code to
    repeatedly GET the CGI script and use the output to update the page.

    The obvious candidate is JavaScript.

    Mart

    --
    "We will need a longer wall when the revolution comes."
    --- AJS, quoting an uncertain source.
     
    Mart van de Wege, Dec 14, 2011
    #2
    1. Advertising

  3. On 2011-12-14 07:41, Mart van de Wege <> wrote:
    > q-rious <> writes:
    >> This is a question about using perl in cgi. If this is not the proper
    >> perl forum, my apologies.
    >>
    >> I want to build a cgi script using perl that will continously loop
    >> through the following:
    >>
    >> a) read from a line from a one line file b) draw something on the
    >> screen based on that value (using gd)
    >>
    >> I can do this for one iteration of the loop. But how do I update the
    >> browser screen for multiple iterations? For other restrictions, I can
    >> not reload the entire page each time the script goes through the
    >> loop. I can only update the drawing (Step b).
    >>

    > You can't do that with static HTML and CGI, period.


    Yes, you can. Just include a Refresh header in the HTTP response.

    That works at least for whole HTML pages. I'm not sure whether it works
    for inline images. If it doesn't, you can still load the image into an
    iframe.


    > The only way to do this is using some sort of client side code to
    > repeatedly GET the CGI script and use the output to update the page.


    That client side code is called a "browser" ;-).

    > The obvious candidate is JavaScript.


    That works, too (and it is more flexible than the refresh header).

    hp


    --
    _ | Peter J. Holzer | Deprecating human carelessness and
    |_|_) | Sysadmin WSR | ignorance has no successful track record.
    | | | |
    __/ | http://www.hjp.at/ | -- Bill Code on
     
    Peter J. Holzer, Dec 14, 2011
    #3
  4. "Peter J. Holzer" <> writes:

    > On 2011-12-14 07:41, Mart van de Wege <> wrote:
    >> q-rious <> writes:
    >>> This is a question about using perl in cgi. If this is not the proper
    >>> perl forum, my apologies.
    >>>
    >>> I want to build a cgi script using perl that will continously loop
    >>> through the following:
    >>>
    >>> a) read from a line from a one line file b) draw something on the
    >>> screen based on that value (using gd)
    >>>
    >>> I can do this for one iteration of the loop. But how do I update the
    >>> browser screen for multiple iterations? For other restrictions, I can
    >>> not reload the entire page each time the script goes through the
    >>> loop. I can only update the drawing (Step b).
    >>>

    >> You can't do that with static HTML and CGI, period.

    >
    > Yes, you can. Just include a Refresh header in the HTTP response.
    >

    Nope.

    Note that OP specifies that he can't reload the entire page.

    Mart

    --
    "We will need a longer wall when the revolution comes."
    --- AJS, quoting an uncertain source.
     
    Mart van de Wege, Dec 14, 2011
    #4
  5. q-rious

    ccc31807 Guest

    On Dec 14, 2:37 am, q-rious <> wrote:
    > I want to build a cgi script using perl that will continously loop through the following:
    >
    > a) read from a line from a one line file
    > b) draw something on the screen based on that value (using gd)
    >
    > I can do this for one iteration of the loop. But how do I update the browser screen for multiple iterations?


    Your hypothetical CGI script might look something like this:
    use warnings;
    use strict;
    use CGI;
    my $what = param('what');
    #present HTML form unless 'draw' is clicked
    unless ($what eq 'draw')
    {
    print qq(<html><body><form><input type="submit" name="what"
    value="draw" /></form></body></html>);
    }
    #draw lines from file if 'draw' is clicked
    elsif ($what eq 'draw')
    {
    print qq(<html><body>);
    open DRAW, '<', 'draw.txt' or die "$!";
    while (<DRAW>) { print $_; }
    close DRAW;
    print qq(</body></html>);
    }
    else
    {
    print qq(<html><body><h1>ERROR!</h1></body></html>);
    }
    exit(0);

    This script WILL NOT RETURN a resource to the HTTP server until it has
    finished executing, IOW, until the entire file has been read, the HTML
    has been generated in its entirety, and the script exits. At that
    point, the web server will return the resource which consists of the
    complete drawing.

    I can think of several ways to do what you say you want to do, but
    they are all ugly. Perhaps the simplest is to return a JavaScript file
    that will programatically draw what you want, but that would run a
    client side script rather than a server side script (which is what CGI
    does).

    You could write an Applet to do the same thing, but that's not CGI.

    I don't think you want repeated HTTP requests and responses bouncing
    back and forth between your browser and the server -- the latency
    would kill you.

    You might want to post a program or the pseudo code for what you want
    if you think this does not respond to what you think you asked.

    CC.
     
    ccc31807, Dec 14, 2011
    #5
  6. q-rious

    ccc31807 Guest

    On Dec 15, 10:21 am, Tad McClellan <> wrote:
    > The else clause is unreachable, so it should not be there.


    Yeah, and I also left out the
    print "Content-type: text/html\n\n";
    line as well. So, just regard it as what I said it was, a hypothetical
    script written in Perlish pseudocode.

    CC.
     
    ccc31807, Dec 15, 2011
    #6
  7. q-rious

    Ted Zlatanov Guest

    On Thu, 15 Dec 2011 09:54:56 -0800 (PST) ccc31807 <> wrote:

    c> On Dec 15, 10:21 am, Tad McClellan <> wrote:
    >> The else clause is unreachable, so it should not be there.


    c> Yeah, and I also left out the
    c> print "Content-type: text/html\n\n";
    c> line as well. So, just regard it as what I said it was, a hypothetical
    c> script written in Perlish pseudocode.

    But that's not useful.

    Please try to post complete working examples. This one was nearly so
    which is much more confusing than actual pseudocode.

    Ted
     
    Ted Zlatanov, Dec 16, 2011
    #7
  8. On 2011-12-14 09:27, Mart van de Wege <> wrote:
    > "Peter J. Holzer" <> writes:
    >> On 2011-12-14 07:41, Mart van de Wege <> wrote:
    >>> q-rious <> writes:
    >>>> I want to build a cgi script using perl that will continously loop
    >>>> through the following:

    [...]
    >>>> I can do this for one iteration of the loop. But how do I update the
    >>>> browser screen for multiple iterations? For other restrictions, I can
    >>>> not reload the entire page each time the script goes through the
    >>>> loop. I can only update the drawing (Step b).
    >>>>
    >>> You can't do that with static HTML and CGI, period.

    >>
    >> Yes, you can. Just include a Refresh header in the HTTP response.
    >>

    > Nope.
    >
    > Note that OP specifies that he can't reload the entire page.


    I guess you didn't read the second paragraph before replying.

    Here is a simple demonstration:

    http://www.hjp.at/tests/refresh_html2.cgi

    hp


    --
    _ | Peter J. Holzer | Deprecating human carelessness and
    |_|_) | Sysadmin WSR | ignorance has no successful track record.
    | | | |
    __/ | http://www.hjp.at/ | -- Bill Code on
     
    Peter J. Holzer, Dec 17, 2011
    #8
  9. "Peter J. Holzer" <> writes:

    > On 2011-12-14 09:27, Mart van de Wege <> wrote:
    >> "Peter J. Holzer" <> writes:
    >>> On 2011-12-14 07:41, Mart van de Wege <> wrote:
    >>>> q-rious <> writes:
    >>>>> I want to build a cgi script using perl that will continously loop
    >>>>> through the following:

    > [...]
    >>>>> I can do this for one iteration of the loop. But how do I update the
    >>>>> browser screen for multiple iterations? For other restrictions, I can
    >>>>> not reload the entire page each time the script goes through the
    >>>>> loop. I can only update the drawing (Step b).
    >>>>>
    >>>> You can't do that with static HTML and CGI, period.
    >>>
    >>> Yes, you can. Just include a Refresh header in the HTTP response.
    >>>

    >> Nope.
    >>
    >> Note that OP specifies that he can't reload the entire page.

    >
    > I guess you didn't read the second paragraph before replying.
    >
    > Here is a simple demonstration:
    >
    > http://www.hjp.at/tests/refresh_html2.cgi
    >

    OK, so at least Firefox refetches and redraws just the image when you
    serve it up with Refresh header.

    Huh. Didn't know that one. Live and learn, I guess.

    Mart

    --
    "We will need a longer wall when the revolution comes."
    --- AJS, quoting an uncertain source.
     
    Mart van de Wege, Dec 17, 2011
    #9
  10. q-rious

    ccc31807 Guest

    On Dec 16, 11:36 am, Ted Zlatanov <> wrote:
    > Please try to post complete working examples.  This one was nearly so
    > which is much more confusing than actual pseudocode.


    I couldn't post a working example because I have no idea of the kind
    of file the OP wanted to print. In my example, I assumed that it was
    some kind of ASCII file (maybe ASCII are) but it could have been
    something else.

    Working code also requires a working HTTP server set up to serve CGI
    scripts. I just dashed this off at work, while waiting on some other
    processes to complete, and didn't have access to such. If I had, I
    would have picked up on the omission of the Copntent-type header right
    away.

    Still, none of that prevented me from making the point that the OP
    really couldn't use CGI for doing what he said he wanted to do. I just
    wanted to illustrate that point as well as I could while taking a
    short break at work.

    CC.
     
    ccc31807, Dec 17, 2011
    #10
  11. q-rious

    Guest

    Hi Peter,

    Thanks for your reply and example. Will you be able to post the code for refresh_img.cgi so that I or if anyone else who may have similar question inthe future understand the process better?

    Thanks again.
     
    , Dec 18, 2011
    #11
  12. On 2011-12-18 07:27, <> wrote:
    > Hi Peter,
    >
    > Thanks for your reply and example. Will you be able to post the code
    > for refresh_img.cgi so that I or if anyone else who may have similar
    > question inthe future understand the process better?


    The code is really trivial, but here it is:


    #!/usr/bin/perl
    # refresh_html2.cgi

    use warnings;
    use strict;

    print "Content-Type: text/html; charset=utf-8\n";
    print "\n";
    print "<html>\n";
    print " <head>\n";
    print " <title>Refresh header test</title>\n";
    print " </head>\n";
    print " <body>\n";
    print " <h1>Refresh header test</h1>\n";
    print " <p>This script tests the effect of a Refresh header on an\n";
    print " embedded image. The page should not be reloaded, but the\n";
    print " image should.</p>\n";
    print " <p>Time: ", scalar(gmtime), "</p>\n";
    print " <iframe width='200', height='200' src='refresh_img.cgi'></iframe>\n";
    print " <p>Time: ", scalar(gmtime), "</p>\n";
    print " </body>\n";
    print "</html>\n";
    __END__


    #!/usr/bin/perl
    # refresh_img.cgi

    use warnings;
    use strict;

    use Imager;
    use Math::Trig qw(deg2rad);

    print "Content-Type: image/png\n";
    print "Refresh: 1\n";
    print "\n";

    my $img = Imager->new(xsize=>200, ysize=>200, channels=>4);

    my ($sec, $min, $hour) = gmtime;

    my $red = Imager::Color->new(255, 0, 0);
    my $black = Imager::Color->new(0, 0, 0);

    $img->circle(x => 100, y => 100, r => 99, color => $black, filled => 0);
    $img->line(x1 => 100, y1 => 100,
    x2 => 100 + sin(deg2rad($sec * 6)) * 90,
    y2 => 100 - cos(deg2rad($sec * 6)) * 90,
    color => $red);
    $img->line(x1 => 100, y1 => 100,
    x2 => 100 + sin(deg2rad($min * 6)) * 80,
    y2 => 100 - cos(deg2rad($min * 6)) * 80,
    color => $red);
    $img->line(x1 => 100, y1 => 100,
    x2 => 100 + sin(deg2rad($hour * 15)) * 70,
    y2 => 100 - cos(deg2rad($hour * 15)) * 70,
    color => $red);

    $img->write(fh => \*STDOUT, type => 'png');
    __END__

    The relevant parts are the use of an iframe in the first script (it
    doesn't work with <img/>, at least not with Firefox) and the Refresh
    header in the second script.

    hp



    --
    _ | Peter J. Holzer | Deprecating human carelessness and
    |_|_) | Sysadmin WSR | ignorance has no successful track record.
    | | | |
    __/ | http://www.hjp.at/ | -- Bill Code on
     
    Peter J. Holzer, Dec 18, 2011
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,872
  2. praba kar

    Python-cgi or Perl-cgi script doubt

    praba kar, Jul 30, 2005, in forum: Python
    Replies:
    1
    Views:
    813
    Michael Sparks
    Jul 30, 2005
  3. Replies:
    20
    Views:
    886
    Gunnar Hjalmarsson
    Jan 18, 2005
  4. vikrant
    Replies:
    4
    Views:
    296
  5. kath
    Replies:
    4
    Views:
    816
    J. Gleixner
    Apr 9, 2007
Loading...

Share This Page