File: Mite.pm

package info (click to toggle)
libsub-handlesvia-perl 0.052000-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,740 kB
  • sloc: perl: 9,645; makefile: 2
file content (314 lines) | stat: -rw-r--r-- 9,817 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# NOTE: Since the intention is to ship this file with a project, this file
# cannot have any non-core dependencies.
package Sub::HandlesVia::Mite;
use 5.008001;
use strict;
use warnings;
no strict 'refs';

if ( $] < 5.009005 ) { require MRO::Compat; }
else                 { require mro;         }

defined ${^GLOBAL_PHASE}
or eval { require Devel::GlobalDestruction; 1 }
or do {
    carp( "WARNING: Devel::GlobalDestruction recommended!" );
    *Devel::GlobalDestruction::in_global_destruction = sub { undef; };
};

# Constants
sub true  () { !!1 }    sub false () { !!0 }
sub ro    () { 'ro' }   sub rw    () { 'rw' }   sub rwp   () { 'rwp' }
sub lazy  () { 'lazy' } sub bare  () { 'bare' }

# More complicated constants
BEGIN {
    my @bool = ( \&false, \&true );
    *_HAS_AUTOCLEAN = $bool[ 0+!! eval { require namespace::autoclean } ];
    *STRICT         = $bool[ 0+!! ( $ENV{PERL_STRICT} || $ENV{EXTENDED_TESTING} || $ENV{AUTHOR_TESTING} || $ENV{RELEASE_TESTING} ) ];
};

# Exportable error handlers
sub _error_handler {
    my ( $func, $message, @args ) = @_;
    if ( @args ) {
        require Data::Dumper;
        local $Data::Dumper::Terse  = 1;
        local $Data::Dumper::Indent = 0;
        $message = sprintf $message, map {
            ref($_) ? Data::Dumper::Dumper($_) : defined($_) ? $_ : '(undef)'
        } @args;
    }
    my $next = do { require Carp; \&{"Carp::$func"} };
    @_ = ( $message );
    goto $next;
}

sub carp    { unshift @_, 'carp'   ; goto \&_error_handler }
sub croak   { unshift @_, 'croak'  ; goto \&_error_handler }
sub confess { unshift @_, 'confess'; goto \&_error_handler }

# Exportable guard function
{
    my $GUARD_PACKAGE = __PACKAGE__ . '::Guard';
    *{"$GUARD_PACKAGE\::DESTROY"} = sub { $_[0][0] or $_[0][1]->() };
    *{"$GUARD_PACKAGE\::restore"} = sub { $_[0]->DESTROY; $_[0][0] = true };
    *{"$GUARD_PACKAGE\::dismiss"} = sub {                 $_[0][0] = true };
    *{"$GUARD_PACKAGE\::peek"}    = sub { $_[0][2] };
    *guard = sub (&) { bless [ 0, @_ ] => $GUARD_PACKAGE };
}

# Exportable lock and unlock
sub _lul {
    my ( $lul, $ref ) = @_;
    if ( ref $ref eq 'ARRAY' ) {
        &Internals::SvREADONLY( $ref, $lul );
        &Internals::SvREADONLY( \$_, $lul ) for @$ref;
        return;
    }
    if ( ref $ref eq 'HASH' ) {
        &Internals::hv_clear_placeholders( $ref );
        &Internals::SvREADONLY( $ref, $lul );
        &Internals::SvREADONLY( \$_, $lul ) for values %$ref;
        return;
    }
    return;
}

sub lock {
    unshift @_, true;
    goto \&_lul;
}

sub unlock {
    my $ref = shift;
    _lul( 0 , $ref );
    &guard( sub { _lul( 1, $ref ) } );
}

sub _is_compiling {
    defined $Mite::COMPILING and $Mite::COMPILING eq __PACKAGE__;
}

sub import {
    my $me = shift;
    my %arg = map +( lc($_) => true ), @_;
    my ( $caller, $file ) = caller;

    if( _is_compiling() ) {
        require Mite::Project;
        'Mite::Project'->default->inject_mite_functions(
            'package' => $caller,
            'file'    => $file,
            'arg'     => \%arg,
            'shim'    => $me,
        );
    }
    else {
        # Try to determine original filename for caller, minus libdir.
        # This would normally be in %INC but caller hasn't finished loading yet.
        require File::Spec;
        my $orig = $file;
        for my $base ( @INC ) {
            $base eq substr $file, 0, length $base
            and -f File::Spec->catfile( $base, substr $file, 1 + length $base )
            and $orig = File::Spec->abs2rel( $file, $base )
            and last;
        }

        # Changes to this filename must be coordinated with Mite::Compiled
        my $mite_file = $orig . '.mite.pm';
        local $@;
        if ( not eval { require $mite_file; 1 } ) {
            my $e = $@;
            croak "Compiled Mite file ($mite_file) for $file is missing or an error occurred loading it: $e";
        }
    }

    'warnings'->import;
    'strict'->import;
    'namespace::autoclean'->import( -cleanee => $caller )
        if _HAS_AUTOCLEAN && !$arg{'-unclean'};
}

{
    my ( $cb_before, $cb_after );
    sub _finalize_application_roletiny {
        my ( $me, $role, $caller, $args ) = @_;
        if ( $INC{'Role/Hooks.pm'} ) {
            $cb_before ||= \%Role::Hooks::CALLBACKS_BEFORE_APPLY;
            $cb_after  ||= \%Role::Hooks::CALLBACKS_AFTER_APPLY;
        }
        if ( $cb_before ) {
            $_->( $role, $caller ) for @{ $cb_before->{$role} || [] };
        }
        'Role::Tiny'->_check_requires( $caller, $role );
        my $info = $Role::Tiny::INFO{$role};
        for ( @{ $info->{modifiers} || [] } ) {
            my @args         = @$_;
            my $modification = shift @args;
            my $handler      = "HANDLE_$modification";
            $me->$handler( $caller, undef, @args );
        }
        if ( $cb_after ) {
            $_->( $role, $caller ) for @{ $cb_after->{$role} || [] };
        }
        return;
    }

    # Usage: $me, $caller, @with_args
    sub HANDLE_with {
        my ( $me, $caller ) = ( shift, shift );
        while ( @_ ) {
            my $role = shift;
            my $args = ref($_[0]) ? shift : undef;
            if ( $INC{'Role/Tiny.pm'} and 'Role::Tiny'->is_role( $role ) ) {
                $me->_finalize_application_roletiny( $role, $caller, $args );
            }
            else {
                $role->__FINALIZE_APPLICATION__( $caller, $args );
            }
        }
        return;
    }
}

# Usage: $me, $caller, $keyword, @has_args
sub HANDLE_has {
    my ( $me, $caller, $keyword, $names ) = ( shift, shift, shift, shift );
    if ( @_ % 2 ) {
        my $default = shift;
        unshift @_, ( 'CODE' eq ref( $default ) )
            ? ( is => lazy, builder => $default )
            : ( is => ro, default => $default );
    }
    my %spec = @_;
    my $code;
    for my $name ( ref($names) ? @$names : $names ) {
        $name =~ s/^\+//;
        'CODE' eq ref( $code = $spec{default} )
            and ${"$caller\::__$name\_DEFAULT__"} = $code;
        'CODE' eq ref( $code = $spec{builder} )
            and *{"$caller\::_build_$name"} = $code;
        'CODE' eq ref( $code = $spec{trigger} )
            and *{"$caller\::_trigger_$name"} = $code;
        'CODE' eq ref( $code = $spec{clone} )
            and *{"$caller\::_clone_$name"} = $code;
    }
    return;
}

{
    my $_kind = sub { ${ shift() . '::USES_MITE' } =~ /Role/ ? 'role' : 'class' };

    sub _get_orig_method {
        my ( $caller, $name ) = @_;
        my $orig = $caller->can( $name );
        return $orig if $orig;
        croak "Cannot modify method $name in $caller: no such method";
    }

    sub _parse_mm_args {
        my $coderef = pop;
        my $names   = [ map { ref($_) ? @$_ : $_ } @_ ];
        ( $names, $coderef );
    }

    # Usage: $me, $caller, $caller_kind, @before_args
    sub HANDLE_before {
        my ( $me, $caller, $kind ) = ( shift, shift, shift );
        my ( $names, $coderef ) = &_parse_mm_args;
        $kind ||= $caller->$_kind;
        if ( $kind eq 'role' ) {
            push @{"$caller\::METHOD_MODIFIERS"},
                [ before => $names, $coderef ];
            return;
        }
        for my $name ( @$names ) {
            my $orig = _get_orig_method( $caller, $name );
            local $@;
            eval <<"BEFORE" or die $@;
                package $caller;
                no warnings 'redefine';
                sub $name {
                    \$coderef->( \@_ );
                    \$orig->( \@_ );
                }
                1;
BEFORE
        }
        return;
    }

    # Usage: $me, $caller, $caller_kind, @after_args
    sub HANDLE_after {
        my ( $me, $caller, $kind ) = ( shift, shift, shift );
        my ( $names, $coderef ) = &_parse_mm_args;
        $kind ||= $caller->$_kind;
        if ( $kind eq 'role' ) {
            push @{"$caller\::METHOD_MODIFIERS"},
                [ after => $names, $coderef ];
            return;
        }
        for my $name ( @$names ) {
            my $orig = _get_orig_method( $caller, $name );
            local $@;
            eval <<"AFTER" or die $@;
                package $caller;
                no warnings 'redefine';
                sub $name {
                    my \@r;
                    if ( wantarray ) {
                        \@r = \$orig->( \@_ );
                    }
                    elsif ( defined wantarray ) {
                        \@r = scalar \$orig->( \@_ );
                    }
                    else {
                        \$orig->( \@_ );
                        1;
                    }
                    \$coderef->( \@_ );
                    wantarray ? \@r : \$r[0];
                }
                1;
AFTER
        }
        return;
    }

    # Usage: $me, $caller, $caller_kind, @around_args
    sub HANDLE_around {
        my ( $me, $caller, $kind ) = ( shift, shift, shift );
        my ( $names, $coderef ) = &_parse_mm_args;
        $kind ||= $caller->$_kind;
        if ( $kind eq 'role' ) {
            push @{"$caller\::METHOD_MODIFIERS"},
                [ around => $names, $coderef ];
            return;
        }
        for my $name ( @$names ) {
            my $orig = _get_orig_method( $caller, $name );
            local $@;
            eval <<"AROUND" or die $@;
                package $caller;
                no warnings 'redefine';
                sub $name {
                    \$coderef->( \$orig, \@_ );
                }
                1;
AROUND
        }
        return;
    }
}

# Usage: $me, $caller, $caller_kind, @signature_for_args
sub HANDLE_signature_for {
    my ( $me, $caller, $kind, $name ) = @_;
    $name =~ s/^\+//;
    $me->HANDLE_around( $caller, $kind, $name, ${"$caller\::SIGNATURE_FOR"}{$name} );
    return;
}

1;