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

Re: Time until event

by blokhead (Monsignor)
on Sep 19, 2002 at 15:44 UTC ( [id://199206]=note: print w/replies, xml ) Need Help??


in reply to Time until event

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

Replies are listed 'Best First'.
Re: Re: Time until event
by petral (Curate) on Sep 19, 2002 at 21:56 UTC
      "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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-25 20:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found