Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8458405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:04:19+00:00 2026-06-10T13:04:19+00:00

I have written a function that is a countdown timer. I want to print

  • 0

I have written a function that is a countdown timer.

I want to print this way, Starts in 00:05

Hence I did this, but it doesn’t print correctly, it overwrites my sentence. Can you help fix it?

printf("\nStarts in %02d:%02d",countdownsleep(5));

# Sub for countdown
sub countdownsleep {
    my $x = shift;
    my $t = 1 *$x;
    my ($m,$s);
    my $stoptime = time + $t;
     while((my $now = time) < $stoptime) {
   #printf( "%02d:%02d\r", ($stoptime - $now) / 60, ($stoptime - $now) % 60);
   $m = ($stoptime - $now) / 60;
   $s = ($stoptime - $now) % 60;
    select(undef,undef,undef,1);
 }
 return ($m,$s);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:04:21+00:00Added an answer on June 10, 2026 at 1:04 pm

    The problem is that you are using \r (carriage return) – it returns the carriage to the very start of string (thus overwriting the first 5 characters in the best case scenario); AND causes weird printing behavior in the absence of “\n” (thus possibly not printing anything else after 5 characters).


    To fix your problem, you need to do this in your loop inside countdownsleep ():

    $prefix = "Starts in "; # could be passed in as parameter to countdownsleep()
    printf( "$prefix %02d:%02d\r", ($stoptime - $now) / 60
                                   , ($stoptime - $now) % 60);
    # NOTE this ^^^ - now you re-print your prefix every time and not lose due to \r
    

    And in your call:

    countdownsleep(5); print "\n"; # printing is done by the loop inside already
    # or if you added a $prefix parameter to it:
    countdownsleep("Starts in ", 5); print "\n";
    

    Here is why you need to print “\n” at the VERY END

    $ perl -e '{print "1234567"; printf("1\r");}'
    
    $ perl -e '{print "1234567"; printf("8\r"); print "\n";}' # Works now
    12345678
    
    # And this is what CR really dows
    $ perl -e '{print "1234567"; printf("z\r");  printf("yy\r");  print "\n";}'
    yy34567z
    $ perl -e '{print "1234567"; printf("z\r");  printf("yy\r");  print "zzzz";}'
    zzzz
    

    In other words, printing a carriage return (\r) at the end of the string WITHOUT newline (\n) will effectively not print the string at all – more specifically, will erase everything that was suppose to be printed.

    Printing (\r) before some other characters in the string will cause the subsequent characters to be printed from the beginning of the line, overwriting existing characters (as many as the new ones), but will keep the subsequent characters intact – with the caveat that the non-overwritten characters won’t be printed unless \n is printed at the end.

    • print "$something\r"; # prints nothing

    • print "$something\r$finish"; # prints $finish but not $something

      # $finish is assumed to not contain "\r"
      
    • print "$something\r$finish\n";

      # * prints $something (entirely)
      # * Moves to start of the line
      # * prints $finish overwriting as many characters  from $somthing as needed
      # * prints the rest of $something if it was longer than $finish
      # * prints newline.
      

    On a different note, you should consider using existing coundown/progress CPAN modules instead of rolling your own.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written function (actually a macro, but whatever) that works similarly to progn
I have written a helper function that can retrieve content from a url. This
I am new Jquery and have written a toggle function that looks like this:
I have written this function that will give me a monthly sum for two
I have written a function that sorts a big scale of data. To test
I have written a function that extract the domain from hostname. e.g. www.domain.com ->
I have written a function that positions a tooltip just above a textbox. The
I have written a javascript function that uses setInterval to manipulate a string every
I have written code to perform a function that could take a while to
I have written a PHP function to take a video embed code that has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.