structure thermal devices
[metriccd.git] / client / common.pl
blob753d146483059bdea981b5cbfbbffc64e27ac113
1 #!/usr/bin/env perl
3 sub file_get_contents
5 open my $f, '<', $_[0];
6 local $/ = undef;
7 my $c = <$f>;
8 close $f;
9 return $c;
12 sub file_get_line
14 open my $f, '<', $_[0];
15 my $c = <$f>;
16 chomp $c;
17 close $f;
18 return $c;
21 sub file_get_int
23 my $path = shift;
24 my $s = file_get_line $path;
25 my ($x) = $s =~ /(-?\d+)/;
26 return $x;
29 sub svlhk_cb_print
31 my $prefix = shift;
32 my $value = shift;
34 printf "%s\t%s\n", join('.', @$prefix), $value;
37 sub svlhk_dump
39 my $objref = shift;
40 my $prefix = shift;
41 my $cb_ref = shift // \&svlhk_cb_print;
43 my $type = ref $objref;
45 if($type eq '')
47 $cb_ref->($prefix, $objref);
49 elsif($type eq 'SCALAR')
51 $cb_ref->($prefix, $$objref);
53 elsif($type eq 'ARRAY')
55 my $idx = 0;
56 for my $elem (@$objref)
58 svlhk_dump($elem, [@$prefix, $idx], $cb_ref);
59 $idx++;
62 elsif($type eq 'HASH')
64 for my $key (keys %$objref)
66 svlhk_dump($objref->{$key}, [@$prefix, $key], $cb_ref);
69 else
71 $cb_ref->($prefix, $type);
73 # here you may handle different class of blassed hashes
74 if($type eq 'Linux::FD::Pid')
76 $cb_ref->([@$prefix, 'fileno'], $objref->fileno);
78 else
80 # dump the HASH if it's a blessed HASH
81 if(eval { keys %$objref; 1 })
83 svlhk_dump({%$objref}, $prefix, $cb_ref);