Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Perl5-CGI timeout / timer?

Posted on 1999-07-09
15
Medium Priority
?
945 Views
Last Modified: 2013-12-25
Hi, just wondering how I can set a timer / timeout in Perl5.

I'm looking to create web-content on the fly.  Something where, for example, every 10 seconds, I can reload the existing page with new, updated variables.  Ie something like:

begin_some_kind_of_loop
set timer
if ($timer == expired)
   &update_vavriables
   &generate_newpage
   $timer = reset
end_of_loop

I'm not looking for a HTML or JavaScript based solution.  Doing timers there is no problem.  Essentially I'm looking for a Perl function that has the same functionality as the JavaScript setTimeout() function.
0
Comment
Question by:kaufmannm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
15 Comments
 
LVL 5

Expert Comment

by:thoellri
ID: 1834970
Two solutions come to mind:
1.) server push:
This most likely will only work with netscape, but you can give it a try and see what it does for you. I used this script to push a camera-image every x seconds from the server to the users browser. Of course you can replace the part which emits the image-header and image-data with your own html-code:

#!/usr/local/bin/perl
use CGI;
use HTTP::Date qw(time2str);

$| = 1;

my $BOUNDARY = "ThisRandomString";
my $SLEEPTIME = 5;
my $basetime = 0;
my $img="SpyCam.jpg";
my ($read, $buf);
my $cgi=new CGI;
my $t;

$t=time2str;
print $cgi->header(
             -nph => 1,
             -date => $t,
             "Last-modified" => $t,
             -pragma => "no-cache",
             -expires => 'now',
             -type => "multipart/x-mixed-replace;boundary=$BOUNDARY",
            );

print "--$BOUNDARY\n";
{
  $t=time2str;
  print $cgi->header(
             -date => $t,
             "Last-modified" => $t,
             -pragma => "no-cache",
             -expires => '+0d',
             -type => "image/jpeg",
            );
  open($img,"<$img");
  while($read=sysread($img, $buf, 1024)) {
    print $buf;
  }
  close($img);
  print "\n--$BOUNDARY\n";
  sleep $SLEEPTIME;
  redo;
}


2.) refresh:
This is more likely to be the solution you want to go for. Using either the Refresh-http-header or META-tags you can instruct the browser who reads your page to refresh itself after a number of seconds. Here is a simple clock with omits the refresh in the http-header as well as in a META tag :-)

#!/usr/local/bin/perl
use strict;
use CGI;
my $cgi=new CGI;
my $refresh="10"; # in seconds

print $cgi->header(-Refresh => "$refresh; url=".$cgi->url(-path-info=>1));
print $cgi->start_html(
  -title => "Clock",
  -meta => {
     'refresh' => "$refresh; url=".$cgi->url(-path-info=>1)
  }
);
print "<b>The current time is ".localtime."</b>\n";
print $cgi->end_html;


Hope this helps
  Tobias

0
 
LVL 2

Author Comment

by:kaufmannm
ID: 1834971
I should clarify my question ... I'm not looking for a HTML or JavaScript based solution.  Doing timers there is no problem.  Essentially I'm looking for a Perl function that has the same functionality as the JavaScript setTimeout() function.
0
 
LVL 2

Author Comment

by:kaufmannm
ID: 1834972
Edited text of question.
0
Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

 
LVL 5

Expert Comment

by:thoellri
ID: 1834973
Does this mean you can't use the stuff I posted?

Tobias
0
 
LVL 2

Author Comment

by:kaufmannm
ID: 1834974
Oh, heh, sorry!  I already knew about the META tag / refresh stuff.  And while I could play around with the SSI stuff, it's not really what I was looking for.  I'm looking to create a web-site run entirely by CGI and dynamically created HTML docs.  My problem doesn't reside in creating the appropriate HTML code, it lies in not being able to do timers from within the CGI scripts.  Thanks for the reply tho.
0
 
LVL 5

Expert Comment

by:thoellri
ID: 1834975
kaufmann,

I don't get it: The two scripts above are 100% pure perl scripts which generate dynamically HTML pages that get refreshed using two different techniques.

What am I missing here???

Tobias

0
 
LVL 2

Author Comment

by:kaufmannm
ID: 1834976
There will be no .html pages on this site. All browser output will be created using CGI scripts, and all I'm looking for is to be able to time events from within the script, NOT the web page. I am looking for similar functionality to what I can get using the JavaScript below:

<script language="JavaScript">
<!-- hide
function timer() {
  setTimeout("window.open('cache2.htm', 'cache')", 9000);
}
// -->
</script>

I need to basically be able to say to tell the server "if x amount of seconds have gone by, then do something else" whether that 'something  else' is generating more output, or resetting variables shouldn't matter.  I don't want the client to keep track of the time-out, I want the server to keep track of it, because if I simply refresh the existing page, it won't be refreshed using updated variables.  
For example if initially I set $color = "FFFFFF" and generate the output using <body bgcolor="#$color">, I'll get a page with a white background.  Now, 10 seconds later, I'd like to generate the same page, but this time wiith $color = "000000", resulting in a black background color.
0
 
LVL 5

Expert Comment

by:thoellri
ID: 1834977
Sorry, but you must be sick of my comments already, however we seem to be stuck in a situation where either you don't understand what you're talking about or I just stumbled into a whole new field I've never seen in the last 15 years.

Ok User X wants to use your website. Right? What does user X use to connect to your web-site? A webbrowser. What does a web-browser render? HTML and to a certain extent Java/Javascript generated output (alright I didn't include those gazillion different proprietary formats which usually require a plugin to be rendered; and of course I didn't mention XML/XSL here). The HTML can be served in three different ways: static, dynamic, static templates+dynamic content. The CGI-scripts I presented create *dynamic* HTML. There is no need for you to have any HTML pages around (indeed I run several web-servers which are in fact just a huge perl-script with some databases). Whether you create HTML or HTML with JavaScript embedded or HTML with mumble-jumble referenced is totally up to you.
You said in your original post "no Javascript", "perl5" and "timer/timeout". What I presented you are the *only* ways how you can solve this. If anybody else offers a different solution which solves this problem other than with "server-push" or "refresh" I'd more than interested to learn about it.


0
 
LVL 2

Author Comment

by:kaufmannm
ID: 1834978
I have no problem with a plethora of comments.  IMO, the more the better.  Maybe I'm just being dense ...

...
...
...

Hmmm, I just spend a couple of minutes thinking about it, and you know, even if I could specify a timeout (which I suppose I could capturing different instances of the time function and comparing them), I still wouldn't be able to update the info seen by the browser.  I'd merely be able to add more information to it.  Hrmph!

However, the question still remains is there timer/timeout functionality for Perl? I guess I could create something like:

$mytime = localtime(time);
$mytimeplus = $mytime + 60;
while ($mytime <= $mytimeplus) { $mytime = localtime() ;}

Just seems like a backwards way of doing it.  I'm really surprised that there isn't something like a timer() function, where you can just specify the amount of seconds to wait...


0
 
LVL 5

Expert Comment

by:thoellri
ID: 1834979
There is a timeout facility in perl (although not on Windoze). Check the alarm() man-page (perldoc -f alarm) for an example on how to make an operation timeout.

From what you just posted, it looks like you're in fact looking for sleep (check perldoc -f sleep).

Finally I think we're on the right track :-)
  Tobias


0
 
LVL 2

Author Comment

by:kaufmannm
ID: 1834980
Yep, we're definitely on the right track.  Sleep does make the script wait for x amount of seconds, however it looks like no matter where I place it in the script, it gets executed first.  Ie. I can't print text, pause, and then print some more text.  Any tricks to executing sleep after xyz items have been executed in the script?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1834981
$| = 1;
0
 
LVL 2

Author Comment

by:kaufmannm
ID: 1834982
Thanks ozo, that worked!  

Even bigger thanks to Tobias for hanging in there, and making me realize the folly of my thinking, and getting on the same wave-length with me.  Tobias, please post your last response as an answer...
0
 
LVL 2

Author Comment

by:kaufmannm
ID: 1834983
Thanks ozo, that worked!  

Even bigger thanks to Tobias for hanging in there, and making me realize the folly of my thinking, and getting on the same wave-length with me.  Tobias, please post your last response as an answer...
0
 
LVL 5

Accepted Solution

by:
thoellri earned 800 total points
ID: 1834984
There is a timeout facility in perl (although not on Windoze). Check the alarm() man-page (perldoc -f alarm) for an example on how to make an operation timeout.

From what you just posted, it looks like you're in fact looking for sleep (check perldoc -f sleep).

Finally I think we're on the right track :-)
  Tobias



0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to dynamically set the form action using jQuery.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question