File: GenericClass.pm

package info (click to toggle)
libdata-printer-perl 1.001000-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 732 kB
  • sloc: perl: 4,305; makefile: 7; sh: 1
file content (373 lines) | stat: -rw-r--r-- 14,026 bytes parent folder | download | duplicates (2)
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package Data::Printer::Filter::GenericClass;
use strict;
use warnings;
use Data::Printer::Filter;
use Data::Printer::Common;
use Scalar::Util;

filter '-class' => sub {
    my ($object, $ddp) = @_;

    # if the class implements its own Data::Printer method, we use it!
    if ($ddp->class_method and my $method = $object->can( $ddp->class_method )) {
        return $method->($object, $ddp) if ref $method eq 'CODE';
    }

    my $class_name = ref $object;

    # there are many parts of the class filter that require the object's
    # linear ISA, so we declare it earlier and load it only once:
    my $linear_ISA = Data::Printer::Common::_linear_ISA_for($class_name, $ddp);

    # if the object overloads stringification, use it!
    # except for PDF::API2 which has a destructive stringify()
    if ($ddp->class->stringify && $class_name ne 'PDF::API2') {
        my $str = _get_stringification($ddp, $object, $class_name);
        return $ddp->maybe_colorize("$str ($class_name)", 'class')
            if defined $str;
    }

    # otherwise, do our generic object representation:
    my $show_reftype = $ddp->class->show_reftype;
    my $show_internals = $ddp->class->internals;
    my $reftype;
    if ($show_reftype || $show_internals) {
        $reftype = Scalar::Util::reftype($object);
        $reftype = 'Regexp' if $reftype eq 'REGEXP';
    }

    $ddp->{_class_depth}++;
    my $string = $ddp->maybe_colorize( $class_name, 'class' );

    if ($show_reftype) {
        $string .= ' '
                . $ddp->maybe_colorize('(', 'brackets')
                . $ddp->maybe_colorize( $reftype, 'class' )
                . $ddp->maybe_colorize(')', 'brackets');
    }

    if ($ddp->class->expand eq 'all' || $ddp->class->expand >= $ddp->{_class_depth}) {
        $ddp->indent;
        $string .= '  ' . $ddp->maybe_colorize('{', 'brackets');

        my @superclasses = Data::Printer::Common::_get_superclasses_for($class_name);
        if (@superclasses && $ddp->class->parents) {
            $string .= $ddp->newline . 'parents: '
                    . join(', ', map $ddp->maybe_colorize($_, 'class'), @superclasses)
                    ;
        }
        my (%roles, %attributes);
        if ($INC{'Role/Tiny.pm'} && exists $Role::Tiny::APPLIED_TO{$class_name}) {
            %roles = %{ $Role::Tiny::APPLIED_TO{$class_name} };
        }
        my $is_moose = 0;

        foreach my $parent (@$linear_ISA) {
            if ($parent eq 'Moo::Object') {
                Data::Printer::Common::_tryme(sub {
                    my $moo_maker = 'Moo'->_constructor_maker_for($class_name);
                    if (defined $moo_maker) {
                        %attributes = %{ $moo_maker->all_attribute_specs };
                    }
                });
                last;
            }
            elsif ($parent eq 'Moose::Object') {
                Data::Printer::Common::_tryme(sub {
                    my $class_meta = $class_name->meta;
                    $is_moose = 1;
                    %attributes = map {
                        $_->name => {
                            index => $_->insertion_order,
                            init_arg => $_->init_arg,
                            is => (defined $_->writer ? 'rw' : 'ro'),
                            reader => $_->reader,
                            required => $_->is_required,
                        }
                    } $class_meta->get_all_attributes();
                    foreach my $role ($class_meta->calculate_all_roles()) {
                        $roles{ $role->name } = 1;
                    }
                });
                last;
            }
            elsif ($parent eq 'Object::Pad::UNIVERSAL') {
                Data::Printer::Common::_tryme(sub {
                    my $meta = Object::Pad::MOP::Class->for_class( $class_name );
                    %attributes = map {
                        $_->name . $_->value($class_name) => {
                        }
                    } $meta->fields;
                    %roles = map { $_->name => 1 } $meta->direct_roles;
                });
            }
        }
        if ($ddp->class->show_methods ne 'none') {
            if (my @role_list = keys %roles) {
                @role_list = Data::Printer::Common::_nsort(@role_list)
                    if @role_list && $ddp->class->sort_methods;
                $string .= $ddp->newline . 'roles (' . scalar(@role_list) . '): '
                        . join(', ' => map $ddp->maybe_colorize($_, 'class'), @role_list)
                        ;
            }

            if (my @attr_list = keys %attributes) {
                @attr_list = Data::Printer::Common::_nsort(@attr_list)
                    if @attr_list && $ddp->class->sort_methods;
                $string .= $ddp->newline . 'attributes (' . scalar(@attr_list) . '): '
                        . join(', ' => map $ddp->maybe_colorize($_, 'method'), @attr_list)
                        ;
            }
        }

        my $show_linear_isa = $ddp->class->linear_isa && (
             ($ddp->class->linear_isa eq 'auto' and @superclasses > 1)
          or ($ddp->class->linear_isa ne 'auto')
        );

        if ($show_linear_isa && @$linear_ISA) {
            $string .= $ddp->newline . 'linear @ISA: '
                    . join(', ' => map $ddp->maybe_colorize($_, 'class'), @$linear_ISA)
                    ;
        }

        if ($ddp->class->show_methods ne 'none') {
            $string .= _show_methods($class_name, $linear_ISA, \%attributes, $ddp);
            if ($is_moose && $ddp->class->show_wrapped) {
                my $modified = '';
                my $modified_count = 0;
                $ddp->indent;
                for my $method ($class_name->meta->get_all_methods) {
                    if (ref $method eq 'Class::MOP::Method::Wrapped') {
                        foreach my $kind (qw(before around after)) {
                            my $getter_method = $kind . '_modifiers';
                            if (my @modlist = $method->$getter_method) {
                                $modified .= $ddp->newline . $kind . ' ' . $method->name . ': '
                                          . (@modlist > 1 ? $ddp->parse(\@modlist) : $ddp->parse($modlist[0]));
                                $modified_count++;
                            }
                        }
                    }
                }
                $ddp->outdent;
                if ($modified_count) {
                    $string .= $ddp->newline . 'method modifiers (' . $modified_count . '):'
                            . $modified;
                }
            }
        }

        if ($ddp->class->show_overloads) {
            my @overloads = _get_overloads($object);
            if (@overloads) {
                $string .= $ddp->newline . 'overloads: ' . join(', ' => @overloads);
            }
        }

        if ($show_internals) {
            $string .= $ddp->newline
                    . 'internals: '
                    . $ddp->parse_as($reftype, $object)
                    ;
        }

        $ddp->outdent;
        $string .= $ddp->newline . $ddp->maybe_colorize('}', 'brackets');
    }
    $ddp->{_class_depth}--;

    if ($ddp->show_tied and my $tie = ref tied $object) {
        $string .= " (tied to $tie)";
    }

    return $string;
};

#######################################
### Private auxiliary helpers below ###
#######################################

sub _get_stringification {
    my ($ddp, $object, $class_name) = @_;
    require overload;
    if (overload::Overloaded($object)
        && (overload::Method($object, q(""))
            || overload::Method($object, q(0+))
        )
    ) {
        my $string;
        my $error = Data::Printer::Common::_tryme(sub { $string = '' . $object });
        if ($error) {
            Data::Printer::Common::_warn(
                $ddp,
                "string/number overload error for object $class_name: $error"
            );
        }
        else {
            return $string;
        }
    }
    foreach my $method (qw(as_string stringify to_string)) {
        if ($object->can($method)) {
            my $string;
            my $error = Data::Printer::Common::_tryme(sub { $string = $object->$method });
            if ($error) {
                Data::Printer::Common::_warn(
                    $ddp,
                    "error stringifying object $class_name with $method\(\): $error"
                );
            }
            else {
                return $string;
            }
        }
    }
    return;
}

# returns array of all overloads in class;
sub _get_overloads {
    my ($object) = @_;
    require overload;
    return () unless overload::Overloaded($object);
    return sort grep overload::Method($object, $_),
           map split(/\s+/), values %overload::ops;
}

sub _show_methods {
    my ($class_name, $linear_ISA, $attributes, $ddp) = @_;

    my %methods = ( public => {}, private => {} );
    my @all_methods = map _methods_of(
        $_, Data::Printer::Common::_get_namespace($_)
    ), @$linear_ISA;
    my $show_methods   = $ddp->class->show_methods;
    my $show_inherited = $ddp->class->inherited;
    my %seen_method_name;
    foreach my $method (@all_methods) {
        my ($package_string, $method_string) = @$method;
        next if exists $attributes->{$method_string};
        next if $seen_method_name{$method_string}++;
        next if $method_string eq '__ANON__'; # anonymous subs don't matter here.
        my $type = substr($method_string, 0, 1) eq '_' ? 'private' : 'public';
        if ($package_string eq $class_name) {
            next unless $show_methods eq 'all' || $show_methods eq $type;
            $methods{$type}{$method_string} = undef;
        }
        else {
            next unless $show_inherited eq 'all' || $show_inherited eq $type;
            $methods{$type}{$method_string} = $package_string;
        }
    }
    my $string = '';
    foreach my $type (qw(public private)) {
        next unless $show_methods   eq 'all' or $show_methods   eq $type
                 or $show_inherited eq 'all' or $show_inherited eq $type
        ;
        if ($ddp->class->format_inheritance eq 'string') {
            my @method_list = keys %{$methods{$type}};
            @method_list = Data::Printer::Common::_nsort(@method_list)
                if @method_list && $ddp->class->sort_methods;

            $string .= $ddp->newline . "$type methods (" . scalar(@method_list) . ')';
            if (@method_list) {
                $string .= ': '
                    . join(', ' => map {
                        $ddp->maybe_colorize(
                            $_ . (defined $methods{$type}{$_} ? " ($methods{$type}{$_})" : ''),
                            'method'
                        )
                      } @method_list)
                    ;
            }
        }
        else { # 'lines'
            # first we convert our hash to { pkg => [ @methods ] }
            my %lined_methods;
            my @base_methods;
            my $total_methods = 0;
            foreach my $method (keys %{$methods{$type}}) {
                my $pkg_name = $methods{$type}{$method};
                if (defined $pkg_name) {
                    push @{ $lined_methods{$pkg_name} }, $method;
                }
                else {
                    push @base_methods, $method;
                }
                $total_methods++;
            }

            # then we print them, starting with our own methods:
            @base_methods = Data::Printer::Common::_nsort(@base_methods)
                if @base_methods && $ddp->class->sort_methods;

            $string .= $ddp->newline . "$type methods ($total_methods)"
                    . ($total_methods ? ':' : '')
                    ;
            if (@base_methods) {
                my $base_string = join(', ' => map {
                    $ddp->maybe_colorize($_, 'method')
                } @base_methods);
                $ddp->indent;
                # newline only if we have parent methods to show:
                $string .= (keys %lined_methods ? $ddp->newline : ' ') . $base_string;
                $ddp->outdent;
            }
            foreach my $pkg (sort keys %lined_methods) {
                $ddp->indent;
                $string .= $ddp->newline . "$pkg:";
                @{$lined_methods{$pkg}} = Data::Printer::Common::_nsort(@{$lined_methods{$pkg}})
                    if $ddp->class->sort_methods;
                $ddp->indent;
                $string .= $ddp->newline . join(', ' => map {
                    $ddp->maybe_colorize($_, 'method')
                  } @{$lined_methods{$pkg}}
                );
                $ddp->outdent;
                $ddp->outdent;
            }
        }
    }

    return $string;
}

sub _methods_of {
    require B;
    my ($class_name, $namespace) = @_;
    my @methods;
    foreach my $subref (_get_all_subs_from($class_name, $namespace)) {
        next unless $subref;
        my $m = B::svref_2object($subref);
        next unless $m && $m->isa('B::CV');
        my $gv = $m->GV;
        next unless $gv && !$gv->isa('B::Special') && $gv->NAME;
        push @methods, [ $gv->STASH->NAME, $gv->NAME ];
    }
    return @methods;
}

sub _get_all_subs_from {
    my ($class_name, $namespace) = @_;
    my @subs;
    foreach my $key (keys %$namespace) {
        # perlsub says any sub starting with '(' is reserved for overload,
        # so we skip those:
        next if substr($key, 0, 1) eq '(';
        if (
            # any non-typeglob in the symbol table is a constant or stub
            ref(\$namespace->{$key}) ne 'GLOB'
            # regular subs are stored in the CODE slot of the typeglob
            || defined(*{$namespace->{$key}}{CODE})
        ) {
            push @subs, $key;
        }
    }
    my @symbols;
    foreach my $sub (@subs) {
        push @symbols, Data::Printer::Common::_get_symbol($class_name, $namespace, $sub, 'CODE');
    }
    return @symbols;
}

1;