added AUTOLOAD and pass it down to readers
[Biblio-RFID.git] / lib / RFID / Biblio / Readers.pm
1 package RFID::Biblio::Readers;
2
3 =head1 NAME
4
5 RFID::Biblio::Readers - autodetect supported readers
6
7 =head1 FUNCTIONS
8
9 =head2 _available
10
11 Probe each RFID reader supported and returns succefull ones
12
13   my $rfid_readers = RFID::Biblio::Readers->_available( $regex_filter );
14
15 =head1 SEE ALSO
16
17 =head2 RFID reader implementations
18
19 L<RFID::Biblio::3M810>
20
21 L<RFID::Biblio::CPRM02>
22
23 L<RFID::Biblio::librfid>
24
25 =head1 SEE ALSO
26
27 =head2 RFID reader implementations
28
29 L<RFID::Biblio::3M810>
30
31 L<RFID::Biblio::CPRM02>
32
33 L<RFID::Biblio::librfid>
34
35 =cut
36
37 use warnings;
38 use strict;
39
40 use lib 'lib';
41
42 my @readers = ( '3M810', 'CPRM02', 'librfid' );
43
44 sub _available {
45         my ( $self, $filter ) = @_;
46
47         $filter = 'all' unless defined $filter;
48
49         return $self->{_available}->{$filter} if defined $self->{_available}->{$filter};
50
51         my @rfid;
52
53         foreach my $reader ( @readers ) {
54                 next if $reader !~ /$filter/i;
55                 my $module = "RFID::Biblio::$reader";
56                 eval "use $module";
57                 die $@ if $@;
58                 if ( my $rfid = $module->new( device => '/dev/ttyUSB0' ) ) {
59                         push @rfid, $rfid;
60                         warn "# added $module\n";
61                 } else {
62                         warn "# ignored $module\n";
63                 }
64         }
65
66         die "no readers found" unless @rfid;
67
68         $self->{_available}->{$filter} = [ @rfid ];
69 }
70
71 sub new {
72         my $class = shift;
73         my $self = {};
74         bless $self, $class;
75         return $self;
76 }
77
78 # we don't want DESTROY to fallback into AUTOLOAD
79 sub DESTROY {}
80
81 our $AUTOLOAD;
82 sub AUTOLOAD {
83         my $self = shift;
84         my $command = $AUTOLOAD;
85         $command =~ s/.*://;
86
87         foreach my $r ( @{ $self->_available } ) {
88                 $r->$command(@_);
89         }
90 }
91
92 1