怎样用perl实现dos提示符下,让那个光标旋转的程序

怎样用perl实现dos提示符下,让那个光标旋转的程序

比如 C:\>\
让第二个“\”分别变化为 |  / - \   ......... 就很像它在旋转了
你是想做进度条么?
测试一下头像

[Copy to clipboard] [ - ]
CODE:
#!/usr/bin/perl
$|++;
my @c = qw! - \ | / !;

my $i = 0;
my $j = 0;
print ". ";
while (1) {
    select(undef, undef, undef, 0.2);
    print "\b", $c[$i];
    $i++;
    $j++;
    print "\b. " unless ($j % 10);
    $i = 0 if ($i > 3);
}
   

$|++ 看了半天都没转过弯来.....

执行的时候想ctrl+c的,结果按成ctrl+x了
结果很诡异,
1 #!/usr/bin/perl -w
  2 select((select(STDOUT),$|=1)[0]);  
  3 $|= 1;   
  4 while(1){
  5     print "\b", "-";
  6     sleep(1);
  7     print "\b","\\";
  8     sleep(1);
  9     print "\b", "|";
10     sleep(1);
11     print "\b", "/";
12     sleep(1);
13 }
select哪位大哥能解释下?
http://perldoc.perl.org/functions/select.html

Watch this page,U will get it
大哥你好,你的代码太强了,很好用!
能不能进一步帮我改进一下,就是说:
C:\WINDOWS\system32>×
小棍棍在×那个位置旋转,但是不影响前面的路径啊,提示符啊这样的信息。
我把 dajun高手的代码改了一小下,这回符合我的要求了,看起来很好玩的样子,感谢大家的帮助,尤其感谢dajun!

#!/usr/bin/perl
$|++;
my @c = qw! - \ | / !;

my $i = 0;
my $j = 0;

$aa = qx(cd);
chomp $aa;

print $aa;
print ">";
print " ";

while (1) {
    select(undef, undef, undef, 0.2);
   
    print "\b", $c[$i];
    #print $c[$i];
    $i++;
    #$j++;
    #print "\b. " unless ($j % 10);
    $i = 0 if ($i > 3);
}
对了  $|++;  到底是啥意义呢?
$|      If set to nonzero, forces a flush right away and after every
               write or print on the currently selected output channel.
               Default is 0 (regardless of whether the channel is really
               buffered by the system or not; $| tells you only whether you've
               asked Perl explicitly to flush after each write).  STDOUT will
               typically be line buffered if output is to the terminal and
               block buffered otherwise.  Setting this variable is useful pri-
               marily when you are outputting to a pipe or socket, such as
               when you are running a Perl program under rsh and want to see
               the output as it's happening.  This has no effect on input
               buffering.  See "getc" in perlfunc for that.  (Mnemonic: when
               you want your pipes to be piping hot.)


意思是默认的时候STDOUT行缓冲, print "\b", $c[$i]; 后面没加"\n", 所以不会立刻就打印出来,而是留在缓冲区里了, 我们的要求是要实时显示, 又不想打印"\n", 所以不能让他缓冲,
也就是把$|设置为一个非0值,

要是直接写$|=1, 可能更清晰一些,但是很多人的代码常$|++; 我也就习惯这样写了