Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Time until event

by halxd2 (Monk)
on Sep 19, 2002 at 15:03 UTC ( [id://199191]=CUFP: print w/replies, xml ) Need Help??

I run a lot of "setup" scripts. I made them to speed installs of software.
Now other people are using them. Several times
in the scripts users need to edit a file or some other
activity that will change the way in which the user might expect the script to run.
S/he needs to realize this _before_ the the event starts.
Hence a snippet!


Takes one arg requried, the time to countdown.
One arg possibe, a description of what's comming.
kinda looks like this with the number changing each second:
5 seconds until event
sub timetill($;$) { my $pipe = $|;#pipe value is saved $| = 1;#pipe value changed to flush the buffer my $time = shift || return -1;#how long my $line = shift || " seconds until event";#what chomp $time; chomp $line; my $backcount = length $line; $backcount+=7;#this is where you might need to edit #to suit you. Spaces, and number display is added #to the total count to backup while($time > 0) { print " " . ' ' x (3 - length($time)) . "$time $line"; foreach (1..$backcount){print "\b"} $time -= 1; sleep 1; } $| = $pipe;#yea maybe I don't need to replace $| #but I sleep better knowing I'd put #everyting back the way I found it. #I tend to use it like this: #timetill(10, 'edit /etc/inetd.conf'); #system("vi /etc/inetd.conf"); }

Replies are listed 'Best First'.
Re: Time until event
by blokhead (Monsignor) on Sep 19, 2002 at 15:44 UTC
    You may want to consider changing the following. It's shorter, and it's probably how I would have written the loop. I realize life is not one big Perl-golf challenge, but in this case I prefer the shorter version.
    while ($time > 0) { print " " . ' ' x (3 - length($time)) . "$time $line"; foreach (1..$backcount){print "\b"} $time -= 1; sleep 1; }
    to
    while ($time--) { printf(" %3s %s", $time, $line); print "\b" for (1..$backcount); # or this could even be written as: # print "\b" x $backcount; sleep 1; }
    No need to do the formatting of the time yourself, sprintf or printf can do it for you. The %3s part means format the string $time, right-justified within a column of 3 characters (space-padded if necessary). You could also do %3d as part of your printf format to do the same thing with zero-padding instead. See the perldoc page for printf for more fun options.

    Having said that, I haven't actually executed your code, but I wonder if printing out backspaces isn't the most efficient way to erase the line. I don't have any other suggestions though, so I may be off base. Anyway, good job in saving/replacing $| within your sub. Returning globals to their previous values can save a lot of headaches.

    blokhead

        "I wonder if printing out backspaces isn't the most efficient way to erase the line."

      Actually, "\r" serves that purpose, unless you are overprinting in mid-line.   But even then, simply reprinting the unchanging beginning of the line still seems simplest.
      eg,
      printf(" %3d %s\r", $time, $line); while(--$time) { sleep 1; printf(" %3d\r", $time); } print"\n";
      See this whirligig for a similar function.

        p
Re: Time until event
by Aristotle (Chancellor) on Sep 19, 2002 at 19:18 UTC
    See comments.
    sub timetill { # don't use prototypes local $| = 1; # will be restored on exit scope my $time = shift || return; # use "undef" to signal errors in Perl my $line = shift || " seconds until event"; # why chomp? while($time > 0) { my $prompt = sprintf "%3d $line", $time; print $prompt; sleep 1; print "\b" x length $prompt; # no need to edit the function $time--; } }

    Please read Are prototypes evil? and Tom Christiansen's explanation on prototypes.

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://199191]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-19 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found