在Perl程序中显示进度条之使用Smart::Comments模块

  • 简单的演示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/perl
 
use Smart::Comments;
 
#Can be use for "foreach, for, while, until)
 
for ( 1 .. 10 ) {    ### Progressing...   done
sleep(1);
}
 
for ( 1 .. 10 ) {    ### Progressing===|   done
sleep(1);
}
 
for ( 1 .. 10 ) {    ### Evaluating [===|    ] % done
sleep(1);
}
 
for ( 1 .. 10 ) {    ### Evaluating |===[%]    |
sleep(1);
}
  • 显示剩余时间

When a progress bar is used with a for loop, the module tracks how long each iteration is taking and makes an estimate of how much time will be required to complete the entire loop.

Normally this estimate is not shown, unless the estimate becomes large enough to warrant informing the user. Specifically, the estimate will be shown if, after five seconds, the time remaining exceeds ten seconds. In other words, a time-remaining estimate is shown if the module detects a for loop that is likely to take more than 15 seconds in total.

The precision of the reported time-remaining estimate is deliberately vague, mainly to prevent it being annoyingly wrong.

1
2
3
4
5
6
7
#!/usr/bin/perl
 
use Smart::Comments;
 
for ( 1 .. 10 ) {    ### Fighting: [|||    ]
sleep(5);
}