1

コードは次のとおりです。

use Cwd;
use Win32::Console::ANSI;# installed module with ppm
use Term::ANSIColor;

$parent =  cwd();

@files = <*>;
print "\n";
foreach (@files)
{
    $child = "$parent/$_";
    if (-f $_){print "$child\n";}; #file test
    if (-d $_)#directory test
    {
        print color("green")."$child/\n".color("reset");
        my($command) = "cd $child/";#submerge one directory down ( recursive here?)
        $command=~s/\//\\/g;
        system( $command );
    };
}

私が抱えている問題は、出力が色付けされる方法にあります。私が期待しているのは、黒い背景に緑色の「いくつかのディレクトリ」です。代わりに、背景色を緑に、テキストを黒に、場合によっては白をランダムに取得します。color() を使用する他のすべてのコードでこの問題が発生します。再起動すると問題が解決することに気付きました。また、他の perl コードを実行すると、DOS とそのすべてのウィンドウに影響を与える問題が返されるのではないかと疑っています。基本的に、問題が発生すると、color() のすべてのインスタンスを再起動するまで存在します。グリッチのように見えます。助けてください。

4

1 に答える 1

0

私は問題を観察しません。ただし、使用しているという事実system(コマンドを実行するために別のシェルを開始するため、実際にはディレクトリは変更されません)が干渉している可能性があります。CTRL-Cまたは、スクリプトから抜け出すために使用している場合、コンソールが不確定な状態のままになる可能性があります。

これがあなたが望むことを達成するためのより良い方法です:

#!/usr/bin/perl

use Cwd;
use File::Spec::Functions qw( catfile canonpath );
use Win32::Console::ANSI;
use Term::ANSIColor;

local $SIG{INT} = sub { print color('reset') };

my $top = cwd;

print_contents($top);

sub print_contents {
    my ($dir) = @_;

    opendir my $dir_h, $dir
        or die "Cannot open directory: '$dir': $!";

    while ( defined (my $entry = readdir $dir_h) ) {
        next if $entry =~ /^[.][.]?\z/;
        my $path = canonpath catfile $dir => $entry;
        if ( -f $path ) {
            print "$path\n";
        }
        elsif ( -d $path ) {
            print color('green'), $path, color('reset'), "\n";
            print_contents($path);
        }
    }

    closedir $dir_h
        or die "Cannot close directory: '$dir': $!";
}
于 2010-12-22T17:59:56.840 に答える