LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-13-2006, 10:04 PM   #1
Russell Griffiths
LQ Newbie
 
Registered: May 2006
Location: Sydney, Australia
Distribution: Red Hat 7.3
Posts: 27

Rep: Reputation: 15
Use of Sleep function in Perl


Relatively new Perl programmer
Using gchar() and sleep() functions
to 'tickertape' the contents of a text file across the screen. (one char per second)

ie .. read a character from the file, print to screen, sleep, continue the loop.

Code I'm using ..
..
While !EOF(Infile)
{
$a=getc(infile) ;
print "$a" ;
sleep(1) :
}
..

But what happens is it seems to sleep for a while,
then suddenly print all chars to end of line
then sleep again,
then all of next line appears .. et seq.
Sleep time each time seems to be linelength.secs.


Am I using function wrongly,
or am I using wrong function?
especially if I want to do it at subsecond repeat rate,
eg 3 chars per second.

Advice appreciated.

Grif
 
Old 08-14-2006, 04:17 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Perl buffers it's output by default, you need to turn that off first. I.e:

open(INFILE, $infile) or die "Cannot open $infile ($!)";
$| = 1; # make output unbuffered.


There are some other things in your code snippet that are not ok:
While !EOF(Infile) => while (<INFILE>)
sleep(1) : => sleep(1) ;

Hope this gets you going again.
 
Old 08-14-2006, 04:37 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
It don't look much like perl, what's EOF?

What you are trying is actually quite awkward.
file reads/writes are generally buffered.
i.e. the OS likes to grab and print in blocks
to save disk/terminal operations as they are quite expensive.
output is usually line buffered. It likes to print a line
at a time.

you need to set auto flush otherwise printing is buffered
i.e. saves chars until a new line char

try this:
Code:
#!/bin/perl -w

undef local $/; # grab whole file see: perldoc perlvar
local $| =  1;  # set autoflush   see: perldoc perlvar

foreach (split '', <>)
{
    print ;
    sleep(1) ;
}

Last edited by bigearsbilly; 08-14-2006 at 04:38 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to disable the Monitor Sleep function under RH9 ?? 242VDM242 Linux - Newbie 1 05-21-2005 01:51 PM
looking for a Sleep() function Mike Davies Linux - Software 2 10-25-2004 08:11 AM
what are the Hexadecimal function and ASCII function in Perl Bassam Programming 1 06-03-2004 01:44 AM
Is the wait function is the same as the sleep function ? Linh Programming 3 04-28-2004 12:39 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration