Re: A delay effect...
by Beatnik (Parson) on Dec 30, 2001 at 21:21 UTC
|
The fact that you want to pause in a CGI process strikes me as odd since CGI data is usually sent in a complete stream. The buffer flushing solution provided above seems like the most plausible one, altho your request is REALLY weird. Buffer flushing with CGIs is usually included when larger files are being generated.
Another, non-sleep method would be to put a meta tag with a refresh that redirects to a congrats page.
Altho there is more than one way to do it, using sleep isn't probably one of those ways :)
Greetz
Beatnik
... Quidquid perl dictum sit, altum viditur. | [reply] |
Re: A delay effect...
by simon.proctor (Vicar) on Dec 30, 2001 at 19:39 UTC
|
Another simple method is to use select, ie:
select(undef undef undef, 2);
You can also use signals:
eval
{
local $SIG{'ALRM'} = sub{die 'timesup';};
alarm 2;
# Now do something as a pause
while(1)
{
select(undef,undef,undef,0.01);
}
alarm 0;
};
if($@ =~ 'timesup')
{
# Call timeout code
}
On the face of it, you gain nothing in the second example. However, you can use this to timeout pieces of code should you not want to hang around forever. Gaining a lock on a file for example.
There are some platform issues using SIG{ALRM}, alarm() and select() but you should be ok on most platforms.
HTH
Simon
PS: I was eating toast when I posted this so please excuse any typos :P | [reply] [d/l] [select] |
Re: A delay effect...
by unb0rn (Scribe) on Dec 30, 2001 at 19:29 UTC
|
sleep(2);
?
And make sure autoflushing ($|++;) is active! | [reply] [d/l] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: A delay effect...
by Juerd (Abbot) on Dec 30, 2001 at 19:49 UTC
|
| [reply] [d/l] |
|
Hi Juerd,
Yes, there is indeed a pause of two seconds. But say I have two print statements and I want the pause to come between them, is that possible?
print "Congrats!<br>";
sleep 2;
print "You guessed you it!";
kiat | [reply] [d/l] |
|
UPDATE: This is my 100th post! :))
Yes, but think about buffering. Perl won't flush it's buffer until it sees a newline.
You have no \n after the <br>, so this is what happens:
- perl fills the buffer
- perl sleeps for two seconds
- perl appends to the buffer, and then flushes it because the end of the script is reached.
Which of course is not what you want.
You could put a \n after the <br>, or turn on the autoflush using $| = 1 (often seen obfuscated as $|++).
print "First<br>\n";
sleep 2
print "Second\n";
###
$| = 1;
print "First<br>";
sleep 2;
print "Second";
I recommend the newline solution, because HTML doesn't care about the newline being there, and autoflushing decreases performance.
Information about $| and many other special variables can be found in perlvar.
2;0 juerd@ouranos:~$ perl -e'undef christmas'
Segmentation fault
2;139 juerd@ouranos:~$
| [reply] [d/l] [select] |
|
|
|
use CGI qw(-oldstyle_urls);
my $q = new CGI;
# ...
if ($q->param('last')) {
last_page($q);
} else {
next_page($q);
}
sub next_page {
# ...
$url = $q->url('-query' => 1);
$last = ($url =~ /\?/) ? '&last=1' : '?last=1';
# Here is the delay, 10 seconds
print $q->header('-Refresh' => "10;URL=$url$last"),
# print the rest of the 1st page here...
}
sub last_page {
# ...
print $q->header(),
# etc...
}
| [reply] [d/l] [select] |
Re: A delay effect...
by cLive ;-) (Prior) on Dec 31, 2001 at 06:58 UTC
|
I wouldn't use Perl for this - I'd use JavaScript.
The main reason being that, odds are these days that you'll be doing this in an HTML table, and a table won't render until the whole thing is received by the browser (and until contained images are received if IMG tags don't contain valid height and width attributes).
So all it will do is slow the page rendering down.
If you are out of a table, you can get away with sleep(2), $|++ at the beginning of the script, and including a \n in the print statement before the sleep.
But, please ask yourself if this is really the best way to go...
.02
cLive ;-) | [reply] |
Re: A delay effect...
by kiat (Vicar) on Dec 30, 2001 at 19:45 UTC
|
Hi unb0rn,
How do I make the autoflusing feature active?
kiat | [reply] |
|
To set the autoflush, just set the magic variable $| to 1.
I used $|++ to increment it, because it's default value is 0, or autoflush turned off.
Autoflush means that Perl sends the output of print's in time they are called. When it's off, Perl puts the print's output in a temporary buffer, and sends the print's output in blocks to the screen/client/etc.
.
Just another not-perfect-but-trying-to-be-legible answer 8-)
| [reply] |
|
Well you can use standard IO modules to do it in an OO fashion but the simplest (and most common in old scripts I would argue) is
$|++;
This increments the $| variable. To learn more take a look at the perlvar man page. | [reply] [d/l] |