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