SI/tools.pm
changeset 28 7d7ca3f05f25
parent 27 de0a25512844
child 29 4e40cf0eba95
equal deleted inserted replaced
27:de0a25512844 28:7d7ca3f05f25
     1 package SI::tools;
       
     2 
       
     3 use if $ENV{DEBUG} ~~ /tools|all/ => "Smart::Comments";
       
     4 
       
     5 use strict;
       
     6 use warnings;
       
     7 use File::Find;
       
     8 use Data::Dumper;
       
     9 use base "Exporter";
       
    10 
       
    11 our @EXPORT = qw(&run &verbose &find_by_devid &cat &barf);
       
    12 
       
    13 sub run(@) {
       
    14     system(@_);
       
    15     die "$_[0] failed with exit code " . ($? >> 8) . "\n"
       
    16       if $?;
       
    17 }
       
    18 
       
    19 sub barf(@) { die Dumper @_ }
       
    20 
       
    21 sub cat($) {
       
    22     my $fh = new IO::File $_[0]
       
    23       or die "Can't open $_[0]: $!\n";
       
    24     return (<$fh>) if wantarray;
       
    25     return join "", <$fh>;
       
    26 }
       
    27 
       
    28 my $last = "\n";
       
    29 
       
    30 sub verbose(@) {
       
    31     print $last eq "\n" ? "" : " "
       
    32       if not(@_ == 1 and length($_[0]) == 1);
       
    33     print @_;
       
    34     $last = substr($_[-1], -1, 1);
       
    35 }
       
    36 
       
    37 sub find_by_devid($$) {
       
    38     my ($dir, $id) = @_;
       
    39     my @found;
       
    40     find(
       
    41         sub {
       
    42             push @found, $File::Find::name if (stat)[6] == $id;
       
    43             return;
       
    44         },
       
    45         $dir
       
    46     );
       
    47     die "ERR: found more than one alias (@found) for $id\n" if @found > 1;
       
    48     die "ERR: found no alias for $id\n" if not @found;
       
    49     return $found[0];
       
    50 }
       
    51 
       
    52 1;
       
    53 
       
    54 # vim:sts=4 sw=4 aw ai si: