| 1 | package Class::NonOO; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Use methods as functions with an implicit singleton | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use v5.10.1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Exporter qw/ import /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use List::MoreUtils qw/ uniq /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Package::Stash; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Scalar::Util qw/ blessed /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | { | ||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use version; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 4 | $Class::NonOO::VERSION = version->declare('v0.4.1'); | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # RECOMMEND PREREQ: Package::Stash::XS 0 | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 NAME | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | Class::NonOO - Use methods as functions with an implicit singleton | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =for readme plugin version | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 SYNOPSYS | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | In a module: | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | package MyModule; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | use Class::NonOO; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | ... | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub my_method { | ||||||||||||||||||||||||||||
| 39 | my ($self, @args) = @_; | ||||||||||||||||||||||||||||
| 40 | ... | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | as_function | ||||||||||||||||||||||||||||
| 44 | export => [ 'my_method' ], # methods to export | ||||||||||||||||||||||||||||
| 45 | args => [ ]; # constructor args | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | The module can be be used with a function calling style: | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | use MyModule; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | ... | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | my_method(@args); | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =begin :readme | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head1 INSTALLATION | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | See | ||||||||||||||||||||||||||||
| 60 | L<How to install CPAN modules|http://www.cpan.org/modules/INSTALL.html>. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =for readme plugin requires heading-level=2 title="Required Modules" | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =for readme plugin changes | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =end :readme | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | This module allows you to turn a class into a module that exports | ||||||||||||||||||||||||||||
| 71 | methods as functions that use an implicit singleton. This allows you | ||||||||||||||||||||||||||||
| 72 | to provide a "hybrid" object-oriented/functional interface. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head1 EXPORTS | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =cut | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 4 | our @EXPORT = qw/ as_function _Class_NonOO_instance /; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 40 | ▶ | sub _Class_NonOO_instance { | ||||||||||||||||||||||||||
|
Subroutine
_Class_NonOO_instance: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 81 | 40 | my $class = shift; | |||||||||||||||||||||||||||
| 82 | 40 | my $user = shift; | |||||||||||||||||||||||||||
| 83 | 40 | state $symbol = '%_DEFAULT_SINGLETONS'; | |||||||||||||||||||||||||||
| 84 | 40 | my $stash = Package::Stash->new($class); | |||||||||||||||||||||||||||
| 85 | 40 | my $instances = $stash->get_or_add_symbol($symbol); | |||||||||||||||||||||||||||
| 86 | 40 | ▶ | return $instances->{$user} //= $class->new(@_); | ||||||||||||||||||||||||||
|
Condition66%2/3
$instances->{$user} //= $class->new(@_)
MC/DC0%0/2
$instances->{$user} //= $class->new(@_)
$instances->{$user}
$class->new(@_)
Truth table66%2/3
A //= B
A= $instances->{$user}B= $class->new(@_)
| |||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head2 C<as_function> | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | as_function | ||||||||||||||||||||||||||||
| 92 | export => [ ... ], # @EXPORT | ||||||||||||||||||||||||||||
| 93 | export_ok => [ ... ], # @EXPORT_OK (optional) | ||||||||||||||||||||||||||||
| 94 | export_tags => { ... }, # %EXPORT_TAGS (optional) | ||||||||||||||||||||||||||||
| 95 | args => [ ... ], # constructor args (optional) | ||||||||||||||||||||||||||||
| 96 | global => 0; # no global state (default) | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | This wraps methods in a function that checks the first argument. If | ||||||||||||||||||||||||||||
| 99 | the argument is an instance of the class, then it assumes it is a | ||||||||||||||||||||||||||||
| 100 | normal method call. Otherwise it assumes it is a function call, and | ||||||||||||||||||||||||||||
| 101 | it calls the method with the singleton instance. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | If the C<export> option is omitted, it will default to the contents of | ||||||||||||||||||||||||||||
| 104 | the C<@EXPORT> variable. The same holds for the C<export_ok> and | ||||||||||||||||||||||||||||
| 105 | C<export_tags> options and the C<@EXPORT_OK> and C<%EXPORT_TAGS> | ||||||||||||||||||||||||||||
| 106 | variables, respectively. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Note that this will not work properly on methods that take an instance | ||||||||||||||||||||||||||||
| 109 | of the class as the first argument. | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | By default, there is no global state. That means that there is a | ||||||||||||||||||||||||||||
| 112 | different implicit singleton for each namespace. This offers some | ||||||||||||||||||||||||||||
| 113 | protection when the state is changed in one module, so that it does | ||||||||||||||||||||||||||||
| 114 | not affect the state in another module. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | If you want to enable global state, you can set C<global> to a true | ||||||||||||||||||||||||||||
| 117 | value. This is not recommended for CPAN modules. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | You might work around this by using something like | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | local %MyClass::_DEFAULT_SINGLETONS; | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | but this is not recommended. If you need to modify state and share it | ||||||||||||||||||||||||||||
| 124 | across modules, you should be passing around individual objects | ||||||||||||||||||||||||||||
| 125 | instead of singletons. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 4 | ▶ | sub as_function { | ||||||||||||||||||||||||||
|
Subroutine
as_function: called CC=10 SCAR=23.4
Pod
documented
| |||||||||||||||||||||||||||||
| 130 | 4 | my %opts = @_; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 4 | my ($caller) = caller; | |||||||||||||||||||||||||||
| 133 | 4 | my $stash = Package::Stash->new($caller); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 4 | my $export = $stash->get_or_add_symbol('@EXPORT'); | |||||||||||||||||||||||||||
| 136 | 4 | my $export_ok = $stash->get_or_add_symbol('@EXPORT_OK'); | |||||||||||||||||||||||||||
| 137 | 4 | my $export_tags = $stash->get_or_add_symbol('%EXPORT_TAGS'); | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 4 | ▶ | my $global = $opts{global} // 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"global"} // 0
MC/DC100%1/1
$opts{"global"} // 0
$opts{"global"}
Truth table100%2/2
A
A= $opts{"global"}
| |||||||||||||||||||||||||||||
| 140 | 4 | ▶ | my @args = @{ $opts{args} // [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{"args"} // []
MC/DC0%0/1
$opts{"args"} // []
$opts{"args"}
Truth table50%1/2
A
A= $opts{"args"}
| |||||||||||||||||||||||||||||
| 141 | 4 | ▶ | my @export = @{ $opts{export} // $export }; | ||||||||||||||||||||||||||
|
Condition66%2/3
$opts{"export"} // $export
MC/DC0%0/2
$opts{"export"} // $export
$opts{"export"}
$export
Truth table66%2/3
A // B
A= $opts{"export"}B= $export
| |||||||||||||||||||||||||||||
| 142 | 4 | ▶ | my @export_ok = @{ $opts{export_ok} // $export_ok }; | ||||||||||||||||||||||||||
|
Condition66%2/3
$opts{"export_ok"} // $export_ok
MC/DC0%0/2
$opts{"export_ok"} // $export_ok
$opts{"export_ok"}
$export_ok
Truth table66%2/3
A // B
A= $opts{"export_ok"}B= $export_ok
| |||||||||||||||||||||||||||||
| 143 | 4 | ▶ | my %export_tags = %{ $opts{export_tags} // $export_tags }; | ||||||||||||||||||||||||||
|
Condition33%1/3
$opts{"export_tags"} // $export_tags
MC/DC0%0/2
$opts{"export_tags"} // $export_tags
$opts{"export_tags"}
$export_tags
Truth table33%1/3
A // B
A= $opts{"export_tags"}B= $export_tags
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 4 | my %in_export_ok = map { $_ => 1 } @{$export_ok}; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 4 | foreach | |||||||||||||||||||||||||||
| 148 | 0 | my $name ( uniq @export, @export_ok, map { @$_ } values %export_tags ) | |||||||||||||||||||||||||||
| 149 | { | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 12 | $stash->add_symbol( '&import', \&Exporter::import ); | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 12 | my $symbol = '&' . $name; | |||||||||||||||||||||||||||
| 154 | 12 | ▶ | if ( my $method = $stash->get_symbol($symbol) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 120 | ▶ | my $new = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 157 | 120 | ▶ | if ( blessed( $_[0] ) && $_[0]->isa($caller) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($_[0]) and $_[0]->isa($caller)
MC/DC50%1/2
builtin::blessed($_[0]) and $_[0]->isa($caller)
builtin::blessed($_[0])
$_[0]->isa($caller)
Truth table66%2/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa($caller)
| |||||||||||||||||||||||||||||
| 158 | 80 | return $method->(@_); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | else { | ||||||||||||||||||||||||||||
| 161 | 40 | ▶ | my $user = $global ? 'default' : caller; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 40 | my $self = $caller->_Class_NonOO_instance( $user, @args ); | |||||||||||||||||||||||||||
| 163 | 40 | return $self->$method(@_); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 12 | }; | |||||||||||||||||||||||||||
| 166 | 12 | $stash->add_symbol( $symbol, $new ); | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 12 | ▶ | push @{$export_ok}, $name unless $in_export_ok{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | else { | ||||||||||||||||||||||||||||
| 171 | 0 | die "No method named ${name}"; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 4 | push @{$export}, $_ for @export; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 4 | $export_tags->{all} = $export_ok; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | L<Class::Exporter> is a similar module. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | Robert Rothenberg, C<< <rrwo at cpan.org> >> | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | Copyright 2015-2016 Robert Rothenberg. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 194 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||||||||||||||||||||
| 195 | copy of the full license at: | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =for readme stop | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | Any use, modification, and distribution of the Standard or Modified | ||||||||||||||||||||||||||||
| 202 | Versions is governed by this Artistic License. By using, modifying or | ||||||||||||||||||||||||||||
| 203 | distributing the Package, you accept this license. Do not use, modify, | ||||||||||||||||||||||||||||
| 204 | or distribute the Package, if you do not accept this license. | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | If your Modified Version has been derived from a Modified Version made | ||||||||||||||||||||||||||||
| 207 | by someone other than you, you are nevertheless required to ensure that | ||||||||||||||||||||||||||||
| 208 | your Modified Version complies with the requirements of this license. | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | This license does not grant you the right to use any trademark, service | ||||||||||||||||||||||||||||
| 211 | mark, tradename, or logo of the Copyright Holder. | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | This license includes the non-exclusive, worldwide, free-of-charge | ||||||||||||||||||||||||||||
| 214 | patent license to make, have made, use, offer to sell, sell, import and | ||||||||||||||||||||||||||||
| 215 | otherwise transfer the Package with respect to any patent claims | ||||||||||||||||||||||||||||
| 216 | licensable by the Copyright Holder that are necessarily infringed by the | ||||||||||||||||||||||||||||
| 217 | Package. If you institute patent litigation (including a cross-claim or | ||||||||||||||||||||||||||||
| 218 | counterclaim) against any party alleging that the Package constitutes | ||||||||||||||||||||||||||||
| 219 | direct or contributory patent infringement, then this Artistic License | ||||||||||||||||||||||||||||
| 220 | to you shall terminate on the date that such litigation is filed. | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||||||||||||||||||||||||||||
| 223 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||||||||||||||||||||||||||||
| 224 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||||||||||||||||||||||||||||
| 225 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||||||||||||||||||||||||||||
| 226 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||||||||||||||||||||||||||||
| 227 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||||||||||||||||||||||||||||
| 228 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||||||||||||||||||||||||||||
| 229 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =for readme continue | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =cut | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 4 | 1; | |||||||||||||||||||||||||||