| 1 | package DBIx::VersionedSubs::AutoLoad; | ||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 4 | ▶ | use base 'DBIx::VersionedSubs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 4 | ▶ | use vars qw($VERSION); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 4 | ▶ | use Carp qw(carp croak); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 4 | $VERSION = '0.09'; | |||||||||||
| 8 | |||||||||||||
| 9 | =head1 NAME | ||||||||||||
| 10 | |||||||||||||
| 11 | DBIx::VersionedSubs::AutoLoad - autoload subroutines from the database | ||||||||||||
| 12 | |||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||
| 14 | |||||||||||||
| 15 | package My::App; | ||||||||||||
| 16 | use strict; | ||||||||||||
| 17 | use base 'DBIx::VersionedSubs::AutoLoad'; | ||||||||||||
| 18 | |||||||||||||
| 19 | package main; | ||||||||||||
| 20 | use strict; | ||||||||||||
| 21 | |||||||||||||
| 22 | My::App->startup($dsn); | ||||||||||||
| 23 | while (my $request = Some::Server->get_request) { | ||||||||||||
| 24 | My::App->update_code; # update code from the DB | ||||||||||||
| 25 | My::App->handle_request($request); | ||||||||||||
| 26 | } | ||||||||||||
| 27 | |||||||||||||
| 28 | =head1 ABSTRACT | ||||||||||||
| 29 | |||||||||||||
| 30 | This module overrides some methods in L<DBIx::VersionedSubs> | ||||||||||||
| 31 | to prevent loading of the whole code at startup and installs | ||||||||||||
| 32 | an AUTOLOAD handler to load the needed code on demand. This | ||||||||||||
| 33 | is useful if startup time is more important than response time | ||||||||||||
| 34 | or you fork() before loading the code from the database. | ||||||||||||
| 35 | |||||||||||||
| 36 | =head1 CAVEATS | ||||||||||||
| 37 | |||||||||||||
| 38 | You should be able to switch between the two implementations | ||||||||||||
| 39 | without almost any further code changes. There is one | ||||||||||||
| 40 | drawback of the AUTOLOAD implementation: | ||||||||||||
| 41 | |||||||||||||
| 42 | =head2 Preexisting functions don't get overwritten from the database | ||||||||||||
| 43 | |||||||||||||
| 44 | You need to explicitly load functions from the database | ||||||||||||
| 45 | that you wish to overwrite Perl code obtained from elsewhere. | ||||||||||||
| 46 | |||||||||||||
| 47 | This is the price you pay for using AUTOLOAD. | ||||||||||||
| 48 | |||||||||||||
| 49 | =head1 CLASS METHODS | ||||||||||||
| 50 | |||||||||||||
| 51 | =cut | ||||||||||||
| 52 | |||||||||||||
| 53 | =head2 C<< __PACKAGE__->init_code >> | ||||||||||||
| 54 | |||||||||||||
| 55 | Overridden to just install the AUTOLOAD handler. | ||||||||||||
| 56 | |||||||||||||
| 57 | =cut | ||||||||||||
| 58 | |||||||||||||
| 59 | 3 | ▶ | sub init_code { | ||||||||||
|
Subroutine
init_code: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||
| 60 | 3 | my ($package) = @_; | |||||||||||
| 61 | 4 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 62 | 3 | ▶ | if (! defined &{"$package\::AUTOLOAD"}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | 3 | ▶ | *{"$package\::AUTOLOAD"} = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||
| 64 | 4 | ▶ | use vars qw($AUTOLOAD); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 65 | 3 | ▶ | if ($AUTOLOAD !~ /::(\w+)$/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | 0 | croak "Undecipherable subroutine '$AUTOLOAD' called"; | |||||||||||
| 67 | }; | ||||||||||||
| 68 | 3 | my $name = $1; | |||||||||||
| 69 | 3 | $package->install_and_invoke($name,@_); | |||||||||||
| 70 | 3 | }; | |||||||||||
| 71 | } else { | ||||||||||||
| 72 | 0 | carp "$package->init_code called, but there already is an AUTOLOAD handler installed."; | |||||||||||
| 73 | }; | ||||||||||||
| 74 | |||||||||||||
| 75 | 3 | my $begin = $package->retrieve_code('BEGIN'); | |||||||||||
| 76 | 3 | ▶ | if (defined $begin) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | 1 | eval "{ $begin }"; | |||||||||||
| 78 | 1 | ▶ | carp "$package\::BEGIN: $@" if $@ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | }; | ||||||||||||
| 80 | }; | ||||||||||||
| 81 | |||||||||||||
| 82 | =head2 C<< __PACKAGE__->install_and_invoke NAME, ARGS >> | ||||||||||||
| 83 | |||||||||||||
| 84 | Loads code from the database, installs it | ||||||||||||
| 85 | into the namespace and immediately calls it | ||||||||||||
| 86 | with the remaining arguments via C<< goto &code; >>. | ||||||||||||
| 87 | |||||||||||||
| 88 | If no row with a matching name exists, an | ||||||||||||
| 89 | error is raised. | ||||||||||||
| 90 | |||||||||||||
| 91 | =cut | ||||||||||||
| 92 | |||||||||||||
| 93 | 3 | ▶ | sub install_and_invoke { | ||||||||||
|
Subroutine
install_and_invoke: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||
| 94 | 3 | my ($package,$name) = splice @_,0,2; | |||||||||||
| 95 | |||||||||||||
| 96 | 3 | my $code = $package->load_code($name); | |||||||||||
| 97 | 3 | ▶ | if (defined $code) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 98 | 3 | goto &$code; | |||||||||||
| 99 | } else { | ||||||||||||
| 100 | 0 | croak "Undefined subroutine $package\::$name called"; | |||||||||||
| 101 | }; | ||||||||||||
| 102 | }; | ||||||||||||
| 103 | |||||||||||||
| 104 | =head2 C<< __PACKAGE__->update_code >> | ||||||||||||
| 105 | |||||||||||||
| 106 | Overridden to do lazy updates. It wipes all code that | ||||||||||||
| 107 | is out of date from the namespace and lets the AUTOLOAD | ||||||||||||
| 108 | handler sort out the reloading. | ||||||||||||
| 109 | |||||||||||||
| 110 | =cut | ||||||||||||
| 111 | |||||||||||||
| 112 | 1 | ▶ | sub update_code { | ||||||||||
|
Subroutine
update_code: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||
| 113 | 1 | my ($package) = @_; | |||||||||||
| 114 | |||||||||||||
| 115 | 1 | ▶ | my $version = $package->code_version || 0; | ||||||||||
|
Condition50%1/2
$package->code_version || 0
MC/DC0%0/1
$package->code_version || 0
$package->code_version
Truth table50%1/2
A
A= $package->code_version
| |||||||||||||
| 116 | 1 | my $sth = $package->dbh->prepare_cached(sprintf <<'SQL', $package->code_history); | |||||||||||
| 117 | SELECT distinct name,version FROM %s | ||||||||||||
| 118 | WHERE version > ? | ||||||||||||
| 119 | ORDER BY version DESC | ||||||||||||
| 120 | SQL | ||||||||||||
| 121 | |||||||||||||
| 122 | 1 | $sth->execute($version); | |||||||||||
| 123 | |||||||||||||
| 124 | # If update is needed, wipe the touched elements: | ||||||||||||
| 125 | 1 | my %seen; | |||||||||||
| 126 | |||||||||||||
| 127 | 1 | ▶ | my $current_version = $version || 0; | ||||||||||
|
Condition50%1/2
$version || 0
MC/DC0%0/1
$version || 0
$version
Truth table50%1/2
A
A= $version
| |||||||||||||
| 128 | 1 | ▶ | while (my ($name,$new_version) = $sth->fetchrow()) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 129 | 1 | ▶ | next if $seen{$name}++; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 130 | |||||||||||||
| 131 | 1 | ▶ | $current_version = $current_version < $new_version | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 132 | ? $new_version | ||||||||||||
| 133 | : $current_version; | ||||||||||||
| 134 | |||||||||||||
| 135 | 1 | delete $package->code_source->{$name}; | |||||||||||
| 136 | |||||||||||||
| 137 | # This manual AUTOLOAD is less than ideal | ||||||||||||
| 138 | 4 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 139 | 4 | ▶ | no warnings 'redefine'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 140 | 1 | ▶ | *{"$package\::$name"} = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 141 | 1 | local *AUTOLOAD = "$package\::$name"; | |||||||||||
| 142 | 1 | goto &{"$package\::AUTOLOAD"}; | |||||||||||
| 143 | 1 | }; | |||||||||||
| 144 | # = sub { $package->install_and_invoke( $name, @_ ); }; | ||||||||||||
| 145 | } | ||||||||||||
| 146 | 1 | $package->code_version($current_version); | |||||||||||
| 147 | }; | ||||||||||||
| 148 | |||||||||||||
| 149 | =head2 C<< __PACKAGE__->load_code NAME >> | ||||||||||||
| 150 | |||||||||||||
| 151 | Retrieves the code for the subroutine C<NAME> | ||||||||||||
| 152 | from the database and calls | ||||||||||||
| 153 | C<< __PACKAGE__->install_code $name,$code >> | ||||||||||||
| 154 | to install it. | ||||||||||||
| 155 | |||||||||||||
| 156 | =cut | ||||||||||||
| 157 | |||||||||||||
| 158 | 3 | ▶ | sub load_code { | ||||||||||
|
Subroutine
load_code: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||
| 159 | 3 | my ($package,$name) = @_; | |||||||||||
| 160 | |||||||||||||
| 161 | 3 | my $code = $package->retrieve_code($name); | |||||||||||
| 162 | 3 | ▶ | if (! defined $code) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 163 | # let caller decide whether to croak or to ignore | ||||||||||||
| 164 | 0 | return; | |||||||||||
| 165 | }; | ||||||||||||
| 166 | 3 | $package->create_sub($name,$code); | |||||||||||
| 167 | }; | ||||||||||||
| 168 | |||||||||||||
| 169 | =head2 C<< __PACKAGE__->retrieve_code NAME >> | ||||||||||||
| 170 | |||||||||||||
| 171 | Retrieves the code for the subroutine C<NAME> | ||||||||||||
| 172 | from the database and returns it as a string. | ||||||||||||
| 173 | |||||||||||||
| 174 | =cut | ||||||||||||
| 175 | |||||||||||||
| 176 | 6 | ▶ | sub retrieve_code { | ||||||||||
|
Subroutine
retrieve_code: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 177 | 6 | my ($package,$name) = @_; | |||||||||||
| 178 | |||||||||||||
| 179 | 6 | my $sql = sprintf <<'SQL', $package->code_live; | |||||||||||
| 180 | SELECT code FROM %s | ||||||||||||
| 181 | WHERE name = ? | ||||||||||||
| 182 | SQL | ||||||||||||
| 183 | |||||||||||||
| 184 | 6 | my $sth = $package->dbh->prepare_cached($sql); | |||||||||||
| 185 | 6 | ▶ | if (! $sth->execute($name)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 186 | # let caller decide whether to croak or to ignore | ||||||||||||
| 187 | 0 | return; | |||||||||||
| 188 | } | ||||||||||||
| 189 | 6 | my($code) = $sth->fetchrow; | |||||||||||
| 190 | 6 | $sth->finish; | |||||||||||
| 191 | |||||||||||||
| 192 | 6 | return $code | |||||||||||
| 193 | }; | ||||||||||||
| 194 | |||||||||||||
| 195 | =head1 INSTALLED CODE | ||||||||||||
| 196 | |||||||||||||
| 197 | =head2 C<< AUTOLOAD >> | ||||||||||||
| 198 | |||||||||||||
| 199 | An AUTOLOAD handler is installed to manage the loading | ||||||||||||
| 200 | of code that has not been retrieved from the database | ||||||||||||
| 201 | yet. If another AUTOLOAD handler already exists, | ||||||||||||
| 202 | the AUTOLOAD handler is not installed and a warning | ||||||||||||
| 203 | is issued. | ||||||||||||
| 204 | |||||||||||||
| 205 | =cut | ||||||||||||
| 206 | |||||||||||||
| 207 | 4 | 1; | |||||||||||
| 208 | |||||||||||||
| 209 | =head1 BUGS | ||||||||||||
| 210 | |||||||||||||
| 211 | =over 4 | ||||||||||||
| 212 | |||||||||||||
| 213 | =item * Currently, if a routine gets changed, the AUTOLOAD | ||||||||||||
| 214 | handler is not fired directly but by using a callback. This | ||||||||||||
| 215 | is because I couldn't delete the typeglob properly such | ||||||||||||
| 216 | that the AUTOLOAD fires again. | ||||||||||||
| 217 | |||||||||||||
| 218 | =back | ||||||||||||
| 219 | |||||||||||||
| 220 | =head1 AUTHOR | ||||||||||||
| 221 | |||||||||||||
| 222 | Max Maischein, E<lt>corion@cpan.orgE<gt> | ||||||||||||
| 223 | |||||||||||||
| 224 | =head1 LICENSE | ||||||||||||
| 225 | |||||||||||||
| 226 | This module is licensed under the same terms as Perl itself. | ||||||||||||
| 227 | |||||||||||||
| 228 | =cut | ||||||||||||
| 229 | |||||||||||||