Przeglądaj źródła

Code and POD cleanip

Paul Trost 11 lat temu
rodzic
commit
9f224c33de
2 zmienionych plików z 22 dodań i 17 usunięć
  1. 21 16
      lib/Disk/SMART.pm
  2. 1 1
      t/01-function_tests.t

+ 21 - 16
lib/Disk/SMART.pm

@@ -6,11 +6,10 @@ use Carp;
 use Math::Round;
 
 {
-    $Disk::SMART::VERSION = '0.10'
+    $Disk::SMART::VERSION = '0.11'
 }
 
-our $smartctl = qx(which smartctl);
-chomp($smartctl);
+chomp( our $smartctl = qx(which smartctl) );
 
 =head1 NAME
 
@@ -154,7 +153,7 @@ sub get_disk_temp {
 
 Updates the SMART output and attributes of a device. Returns undef.
 
-C<DEVICE> - Device identifier of SSD/ Hard Drive
+C<DEVICE> - Device identifier of SSD Hard Drive
 
     $smart->update_data('/dev/sda');
 
@@ -178,7 +177,7 @@ sub update_data {
     $self->_process_disk_model($device);
     $self->_process_disk_temp($device);
 
-    return 1;
+    return;
 }
 
 =head2 B<run_short_test(DEVICE)>
@@ -201,12 +200,12 @@ sub run_short_test {
         sleep( $short_test_time * 60 );
     }
 
-    my $smart_output = ( defined $ENV{'MOCK_TEST_DATA'} ) ? $ENV{'MOCK_TEST_DATA'} : qx($smartctl -a $device);
+    my $smart_output = $ENV{'MOCK_TEST_DATA'} // qx($smartctl -a $device);
     ($smart_output) = $smart_output =~ /(SMART Self-test log.*)\nSMART Selective self-test/s;
     my @device_tests      = split /\n/, $smart_output;
     my $short_test_number = $device_tests[2];
     my $short_test_status = substr $short_test_number, 25, +30;
-    $short_test_status =~ s/\s+$//g;    #trim beginning and ending whitepace
+    $short_test_status = _trim($short_test_status);
 
     return $short_test_status;
 }
@@ -225,8 +224,8 @@ sub _process_disk_attributes {
     foreach my $attribute (@attributes) {
         my $name  = substr $attribute, 4,  +24;
         my $value = substr $attribute, 83, +50;
-        $name  =~ s/\s+$//g;    # trim ending whitespace
-        $value =~ s/^\s+//g;    # trim beginning and ending whitepace
+        $name  = _trim($name);
+        $value = _trim($value);
         $self->{'devices'}->{$device}->{'attributes'}->{$name} = $value;
     }
 
@@ -238,8 +237,8 @@ sub _process_disk_errors {
     $self->_validate_param($device);
 
     my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
-    my ($errors) = $smart_output =~ /SMART Error Log Version: [1-9](.*)SMART Self-test log/s;
-    $errors =~ s/^\s+|\s+$//g;    #trim beginning and ending whitepace
+    my ($errors)     = $smart_output =~ /SMART Error Log Version: [1-9](.*)SMART Self-test log/s;
+    $errors = _trim($errors);
     $errors = 'N/A' if !$errors;
 
     return $self->{'devices'}->{$device}->{'errors'} = $errors;
@@ -250,8 +249,8 @@ sub _process_disk_health {
     $self->_validate_param($device);
 
     my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
-    my ($health) = $smart_output =~ /SMART overall-health self-assessment test result:(.*)\n/;
-    $health =~ s/^\s+|\s+$//g;    #trim beginning and ending whitepace
+    my ($health)     = $smart_output =~ /SMART overall-health self-assessment test result:(.*)\n/;
+    $health = _trim($health);
     $health = 'N/A' if !$health || $health !~ /PASSED|FAILED/x;
 
     return $self->{'devices'}->{$device}->{'health'} = $health;
@@ -262,8 +261,8 @@ sub _process_disk_model {
     $self->_validate_param($device);
     
     my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
-    my ($model) = $smart_output =~ /Device\ Model:(.*)\n/;
-    $model =~ s/^\s+|\s+$//g;    #trim beginning and ending whitepace
+    my ($model)      = $smart_output =~ /Device\ Model:(.*)\n/;
+    $model = _trim($model);
     $model = 'N/A' if !$model;
 
     return $self->{'devices'}->{$device}->{'model'} = $model;
@@ -279,7 +278,7 @@ sub _process_disk_temp {
 
     if ($temp_c) {
         $temp_c = substr $temp_c, 83, +3;
-        $temp_c =~ s/^\s+|\s+$//g;    #trim beginning and ending whitepace
+        $temp_c = _trim($temp_c);
         $temp_f = round( ( $temp_c * 9 ) / 5 + 32 );
         $temp_c = int $temp_c;
         $temp_f = int $temp_f;
@@ -292,6 +291,12 @@ sub _process_disk_temp {
     return $self->{'devices'}->{$device}->{'temp'} = [ ( $temp_c, $temp_f ) ];
 }
 
+sub _trim {
+    my $string = shift;
+    $string =~ s/^\s+|\s+$//g;    #trim beginning and ending whitepace
+    return $string;
+}
+
 sub _validate_param {
     my ( $self, $device ) = @_;
     croak "$device not found in object. Verify you specified the right device identifier.\n"

+ 1 - 1
t/01-function_tests.t

@@ -114,7 +114,7 @@ is( keys %attribs, 18, 'get_disk_attributes() returns hash of device attributes'
 is( $smart->run_short_test($disk), 'Completed without error', 'run_short_test() returns proper string' );
 
 $ENV{'MOCK_TEST_DATA'} =~ s/ST3250410AS//;
-is( $smart->update_data($disk), 1, 'update_data() updated object with changed device data' );
+is( $smart->update_data($disk), undef, 'update_data() updated object with changed device data' );
 is( $smart->get_disk_model($disk), 'N/A', 'get_disk_model() returns N/A with changed device data' );
 
 #Negative testing