| 1 | package Lab::Moose::Countdown; | ||||||||||||
| 2 | 6 | $Lab::Moose::Countdown::VERSION = '3.881'; | |||||||||||
| 3 | #ABSTRACT: Verbose countdown/delay with pretty printing of remaining time | ||||||||||||
| 4 | |||||||||||||
| 5 | 6 | ▶ | use v5.20; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 6 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 6 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 6 | ▶ | use Exporter 'import'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 6 | ▶ | use Time::HiRes qw/time sleep/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 6 | ▶ | use Time::Seconds; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 6 | our @EXPORT = qw/countdown/; | |||||||||||
| 15 | |||||||||||||
| 16 | |||||||||||||
| 17 | |||||||||||||
| 18 | sub countdown { | ||||||||||||
| 19 | |||||||||||||
| 20 | # Do not use MooseX::Params::Validate for performance reasons. | ||||||||||||
| 21 | 354 | ▶ | my $delay = shift; | ||||||||||
|
Subroutine
countdown: called CC=6 SCAR=29.8
Pod
documented
| |||||||||||||
| 22 | 354 | ▶ | my $prefix = shift // "Sleeping for "; | ||||||||||
|
Condition50%1/2
shift() // "Sleeping for "
MC/DC0%0/1
shift() // "Sleeping for "
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 23 | |||||||||||||
| 24 | 354 | ▶ | if ( $delay < 0.5 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 25 | 354 | ▶ | if ( $delay > 0 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 26 | 0 | sleep $delay; | |||||||||||
| 27 | } | ||||||||||||
| 28 | 354 | return; | |||||||||||
| 29 | } | ||||||||||||
| 30 | |||||||||||||
| 31 | 0 | my $t1 = time(); | |||||||||||
| 32 | |||||||||||||
| 33 | 0 | my $autoflush = STDOUT->autoflush(); | |||||||||||
| 34 | |||||||||||||
| 35 | 0 | while () { | |||||||||||
| 36 | 0 | my $remaining = $delay - ( time() - $t1 ); | |||||||||||
| 37 | 0 | ▶ | if ( $remaining < 0.5 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | 0 | ▶ | if ( $remaining > 0 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 39 | 0 | sleep $remaining; | |||||||||||
| 40 | } | ||||||||||||
| 41 | 0 | last; | |||||||||||
| 42 | } | ||||||||||||
| 43 | 0 | $remaining = Time::Seconds->new( int($remaining) + 1 ); | |||||||||||
| 44 | 0 | sleep 0.1; | |||||||||||
| 45 | 0 | print $prefix, $remaining->pretty, " \r"; | |||||||||||
| 46 | } | ||||||||||||
| 47 | 0 | say " " x 80; | |||||||||||
| 48 | 0 | STDOUT->autoflush($autoflush); | |||||||||||
| 49 | } | ||||||||||||
| 50 | |||||||||||||
| 51 | __END__ | ||||||||||||
| 52 | |||||||||||||
| 53 | =pod | ||||||||||||
| 54 | |||||||||||||
| 55 | =encoding UTF-8 | ||||||||||||
| 56 | |||||||||||||
| 57 | =head1 NAME | ||||||||||||
| 58 | |||||||||||||
| 59 | Lab::Moose::Countdown - Verbose countdown/delay with pretty printing of remaining time | ||||||||||||
| 60 | |||||||||||||
| 61 | =head1 VERSION | ||||||||||||
| 62 | |||||||||||||
| 63 | version 3.881 | ||||||||||||
| 64 | |||||||||||||
| 65 | =head1 SYNOPSIS | ||||||||||||
| 66 | |||||||||||||
| 67 | use Lab::Moose::Countdown; | ||||||||||||
| 68 | |||||||||||||
| 69 | # Sleep for 23.45678 seconds with pretty countdown | ||||||||||||
| 70 | countdown(23.45678, "Getting ready, Remaining time is "); | ||||||||||||
| 71 | |||||||||||||
| 72 | =head1 FUNCTIONS | ||||||||||||
| 73 | |||||||||||||
| 74 | =head2 countdown | ||||||||||||
| 75 | |||||||||||||
| 76 | my $delay = 2 # seconds | ||||||||||||
| 77 | countdown($delay) | ||||||||||||
| 78 | |||||||||||||
| 79 | my $prefix = "Some prefix text"; | ||||||||||||
| 80 | countdown($delay, $prefix); | ||||||||||||
| 81 | |||||||||||||
| 82 | Replacement for C<Time::HiRes::sleep>. Pretty print the remaining | ||||||||||||
| 83 | hours/minutes/seconds. If the argument is smaller than 0.5 seconds, no | ||||||||||||
| 84 | countdown is printed and the function behaves exactly like C<Time::HiRes::sleep>. | ||||||||||||
| 85 | Default C<$prefix> is C<"Sleeping for">. | ||||||||||||
| 86 | |||||||||||||
| 87 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 88 | |||||||||||||
| 89 | This software is copyright (c) 2023 by the Lab::Measurement team; in detail: | ||||||||||||
| 90 | |||||||||||||
| 91 | Copyright 2018 Simon Reinhardt | ||||||||||||
| 92 | 2020 Andreas K. Huettel, Simon Reinhardt | ||||||||||||
| 93 | |||||||||||||
| 94 | |||||||||||||
| 95 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 96 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 97 | |||||||||||||
| 98 | =cut | ||||||||||||