Basic OO functions for perl lib
[blerg.git] / lib / perl / Blerg-Database / lib / Blerg / Database.pm
1 package Blerg::Database;
2
3 use 5.008000;
4 use strict;
5 use warnings;
6 use Carp;
7
8 require Exporter;
9 use AutoLoader;
10
11 our @ISA = qw(Exporter);
12
13 # Items to export into callers namespace by default. Note: do not export
14 # names by default without a very good reason. Use EXPORT_OK instead.
15 # Do not simply export all your public functions/methods/constants.
16
17 # This allows declaration       use Blerg::Database ':all';
18 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
19 # will save memory.
20 our %EXPORT_TAGS = ( 'all' => [ qw(
21         
22 ) ] );
23
24 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
25
26 our @EXPORT = qw(
27         
28 );
29
30 our $VERSION = '1.7';
31
32 sub AUTOLOAD {
33     # This AUTOLOAD is used to 'autoload' constants from the constant()
34     # XS function.
35
36     my $constname;
37     our $AUTOLOAD;
38     ($constname = $AUTOLOAD) =~ s/.*:://;
39     croak "&Blerg::Database::constant not defined" if $constname eq 'constant';
40     my ($error, $val) = constant($constname);
41     if ($error) { croak $error; }
42     {
43         no strict 'refs';
44         # Fixed between 5.005_53 and 5.005_61
45 #XXX    if ($] >= 5.00561) {
46 #XXX        *$AUTOLOAD = sub () { $val };
47 #XXX    }
48 #XXX    else {
49             *$AUTOLOAD = sub { $val };
50 #XXX    }
51     }
52     goto &$AUTOLOAD;
53 }
54
55 require XSLoader;
56 XSLoader::load('Blerg::Database', $VERSION);
57
58 # Preloaded methods go here.
59
60 sub open {
61     my ($class, $name) = @_;
62     my $ptr = Blerg::Database::_open($name);
63     my $obj = {
64         ptr => $ptr
65     };
66     return bless $obj, $class;
67 }
68
69 sub open_existing {
70     my ($class, $name) = @_;
71
72     if (Blerg::Database::exists($name)) {
73         return Blerg::Database::open($name);
74     }
75     return undef;
76 }
77
78 sub _ensure_pointer {
79     my ($obj) = @_;
80     if (!defined $obj->{ptr}) {
81         croak "Attempted to use closed Blerg::Database";
82     }
83 }
84
85 sub close {
86     my ($obj) = @_;
87     $obj->_ensure_pointer;
88     Blerg::Database::_close($obj->{ptr});
89     delete $obj->{ptr};
90 }
91
92 DESTROY {
93     my ($obj) = @_;
94     $obj->close;
95 }
96
97 sub store {
98     my ($obj, $data) = @_;
99     $obj->_ensure_pointer;
100     return Blerg::Database::_store($obj->{ptr}, $data);
101 }
102
103 sub fetch {
104     my ($obj, $record) = @_;
105     $obj->_ensure_pointer;
106     return Blerg::Database::_fetch($obj->{ptr}, $record);
107 }
108
109 # Autoload methods go after =cut, and are processed by the autosplit program.
110
111 1;
112 __END__
113 # Below is stub documentation for your module. You'd better edit it!
114
115 =head1 NAME
116
117 Blerg::Database - Perl extension for blah blah blah
118
119 =head1 SYNOPSIS
120
121   use Blerg::Database;
122   blah blah blah
123
124 =head1 DESCRIPTION
125
126 Stub documentation for Blerg::Database, created by h2xs. It looks like the
127 author of the extension was negligent enough to leave the stub
128 unedited.
129
130 Blah blah blah.
131
132 =head2 EXPORT
133
134 None by default.
135
136
137 =head1 HISTORY
138
139 =over 8
140
141 =item 1.7
142
143 Original version; created by h2xs 1.23 with options
144
145   -C
146         -b
147         5.8.0
148         -n
149         Blerg::Database
150         -v
151         1.7
152
153 =back
154
155
156
157 =head1 SEE ALSO
158
159 Mention other useful documentation such as the documentation of
160 related modules or operating system documentation (such as man pages
161 in UNIX), or any relevant external documentation such as RFCs or
162 standards.
163
164 If you have a mailing list set up for your module, mention it here.
165
166 If you have a web site set up for your module, mention it here.
167
168 =head1 AUTHOR
169
170 Chip Black, E<lt>bytex64@bytex64.netE<gt>
171
172 =head1 COPYRIGHT AND LICENSE
173
174 Copyright (C) 2013 by Chip Black
175
176 This library is free software; you can redistribute it and/or modify
177 it under the same terms as Perl itself, either Perl version 5.16.1 or,
178 at your option, any later version of Perl 5 you may have available.
179
180
181 =cut