2017-02-28 8 views
0

存在する場合、/ proc/partitionsファイル内の/ dev/rawで始まるすべてのデバイスをスキップして、他のものを配列に格納しようとします。だから私のようなコードのブロックを持っている:パターンマッチでの初期化されていない値の使用(m //)

sub get_proc_partitions { 
    my ($self, $device_name) = @_; 
    my @partitions; 

    open(PART, "/proc/partitions") || die "can't open /proc/partitions: $!"; 
    while (<PART>) { 
     my @field = split; 
     # Skip this line if the fourth field starts with 'ram' 
     next if $field[3] =~ /^ram/; 
     # this regex matches lines like the following. 
     # in this example it will capture hdb 
     # 3 64 78150744 hdb 157 735 2168 1720 1745 437 17432 
     if (/^\s*(?:\d+\s+){3}(\S+)\s.*/) { 
      my $part = $1; 
      if (defined $device_name) { 
       push(@partitions, $part) if ($part =~ /$device_name/); 
      } else { 
       push(@partitions, $part); 
      } 
     } 
     } 
close(PART); 
return \@partitions; 
} 

そして、このコードは次のように私にエラーを返します:

Use of uninitialized value in pattern match (m//) at <filename> line 928, <PART> line 2 

そして、この行が参照:私はcat /proc/partitionsを発行すると

next if $field[3] =~ /^ram/; 
+1

ファイル内の空行があるかもしれません、そしてフィールド#4が –

+0

に定義されることはありません。 org/pod/Linux :: Info :: DiskStats)が役に立つかもしれません。 – ThisSuitIsBlackNot

答えて

2

チェック番号:空行をスキップする権利whilenext unless /\S/;を挿入します。 // metacpan:あなたは、Linux、[Linuxの::情報:: DiskStats](HTTPSのようなものにしている場合は

# "@field <4" - less than four fields 
next if @field <4 || $field[3] =~ /^ram/; 
2

2行目は空です。

$ cat /proc/partitions 
major minor #blocks name 

    1  0  65536 ram0 
    1  1  65536 ram1 
... 

あなたもそうですね。検出/分割によって生成されたか確認し、フィールドのデフォルト値を仮定したフィールドの

while (<PART>) { 
    next unless /\S/; # skip empty lines 
    my @field = split; 
    ... 
関連する問題