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

hi

I've written this perl script and it works.
I have only one problem and don't know howto change it.
During sleep time it should show the message:

"pls wait, ntop will be started"

but after I get the "Status" this message should be disappear.
On the end I should see only the "Status", but now I still have this message in the browser.
#!/usr/bin/perl -w use CGI::Carp qw(fatalsToBrowser); use CGI; my $query = new CGI; print $query->header; print "<html>\n"; print "<head>\n"; print "<title>ntop startup script</title>\n"; print "<STYLE TYPE='text/css'>\n"; print "h3 { color: red }\n"; print "a { text-decoration: none; font: bold 14pt/16pt Ariel, serif }" +; print "a:hover { color: red } /* when mouse is over link */"; print "</style>\n"; print "</head>\n"; print "<body bgcolor='#c0c0d0'>\n"; print "<center>"; print "<p><br></p>"; print "<p><br></p>"; print "pls wait, ntop will be started\n"; system `/usr/local/www/cgi-bin/ntop/ntop.sh stop` or die "cannot stop +ntop: $!"; sleep(8); $cc = `sudo /usr/local/www/cgi-bin/ntop/ntop.sh start` or die "cannot +start ntop: $!"; print "Status: ",$cc,"\n"; print "</center>"; print "</body>"; print "</html>"; exit($cc);
kind regards
cc

Replies are listed 'Best First'.
Re: show a message only during the sleep time
by davidrw (Prior) on Jul 24, 2005 at 13:00 UTC
    well, keeping in mind you can't "unsend" or "erase" stuff send out to the browser, there are a few options..
    • general tip: use strict; and use warnings;
    • Refer to this thread: CGI::Application timeout, especially the merlyn article
    • general tip for this case: make sure auto-flush is on.. do: $|=1; before you start printing.
    • You could put the "pls wait" message in a <div> tag and add javascript code via <body onLoad="..."> that uses css dom to hide the message div so that it appears to vanish.
    • (probably best.. this will be a very simplifed, specific case of merlyn's article) Store the $cc result somewhere (i like Cache::FileCache) and redirect (i think using javascript/onLoad will work) to a cgi (same file w/flag maybe) that will fetch the $cc from the cache and display it.
      hi davidrw

      I tried your trick,
      but I still have "pls wait !" on the screen and will not vanish:
      ................................... print "<body bgcolor='#c0c0d0'>\n"; print "<center>"; print "<p><br></p>"; print "<p><br></p>"; print "<div> pls wait ! </div>\n"; print "<SCRIPT LANGUAGE = \"JavaScript\">\n"; print "<!-- \n"; print "<body onLoad="...">\n"; print "// -->\n"; print "</SCRIPT>\n"; ...................................
        Well, you have write the "..." javascript -- the elipsis was just a place holder (and note that we want to add javascript code by using the <body onLoad=""> attribute, NOT embed the body tag inside script tags). Also, note the clunkiness of print statements for this.. (and this is pretty general advice) at the minimum, at least use here-docs, and also keep in mind a more robust solution like Template::Toolkit or HTML::Template. So, back to the problem at hand, this is what i was envisioning:
        print <<EOF; <html> <head> <title>ntop startup script</title> <STYLE TYPE='text/css'> h3 { color: red } a { text-decoration: none; font: bold 14pt/16pt Ariel, serif } a:hover { color: red } /* when mouse is over link */ </style> <script language="javascript"> function hideWaitMsg(){ var obj = getElementById('wait_msg_text'); obj.visible = false; } </script> </head> <body bgcolor='#c0c0d0' onLoad="hideWaitMsg()"> <center> <p><br></p> <p><br></p> <div id="wait_msg_text">pls wait, ntop will be started</div> EOF
        That's the basic setup .. note that i just guessed at the javascript code in the hideWaitMsg() function -- that part is left as an excercise for the reader.
Re: show a message only during the sleep time
by Joost (Canon) on Jul 24, 2005 at 12:57 UTC
    You can either put some kind of javascript in your output that removes the "pls wait" line, or you can use CSS to place the "status:" line on top of the other, obscuring the old message. You might need to disable output buffering ($|=1). Ofcourse, you could also have the page reload once a second until the code is done, but that requires a bit more coordination.

    On an unrelated note: why are you exit()ing with the STDOUT output of sudo as the status code? And you should probably use taint mode.

Re: show a message only during the sleep time
by kwaping (Priest) on Jul 24, 2005 at 14:47 UTC
    This is a little "outside the box", but how about this: redirect (>) your ntop.sh output to an HTML file, then use an HTTP Refresh to load that page after a given time?
Re: show a message only during the sleep time
by NateTut (Deacon) on Jul 24, 2005 at 15:25 UTC
    I'm not sure this would work in a browser, but it might, put a \r before your status print statement. This is the "carrige return" character which should just return you to the beginning of the line. \n does a "carrige return" to go back to the beginning of the line as well as a "line feed" to go to the next line (at least in Windoze systems).

    This is a trick used in console applications to simulate a continuously updating "status line" without all the fancy schmancy GUI stuff.
      can you pls give some more details, how it should work.

      greetings
      cc
        It (just printing \r) won't work in the context of a browser .. It can, as the poster states, work in the context of a console application, but note that something from curses or console or term will probably be better/more robust (again, just for console apps -- not your browser-context issue).