| 1 | # | ||||||||||
| 2 | # Copyright (c) 2015-2019 Christian Jaeger, copying@christianjaeger.ch | ||||||||||
| 3 | # | ||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||
| 7 | # bundled with this file. | ||||||||||
| 8 | # | ||||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | FP::Struct::Show -- automatic Show protocol implementation | ||||||||||
| 13 | |||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||
| 15 | |||||||||||
| 16 | package FP_Struct_Show_Example::Foo { | ||||||||||
| 17 | |||||||||||
| 18 | 1 | ▶ | use FP::Struct ["a","b"], | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | 'FP::Struct::Show'; | |||||||||
| 20 | |||||||||||
| 21 | _END_ | ||||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | FP_Struct_Show_Example::Foo::constructors->import; | ||||||||||
| 25 | 1 | ▶ | use FP::Show; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | is( show(Foo(1,1+1)), | ||||||||||
| 27 | "Foo(1, 2)" ); | ||||||||||
| 28 | |||||||||||
| 29 | # Whereas the default: | ||||||||||
| 30 | package FP_Struct_Show_Example::Bar { | ||||||||||
| 31 | |||||||||||
| 32 | 1 | ▶ | use FP::Struct ["a","b"]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | |||||||||||
| 34 | _END_ | ||||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | FP_Struct_Show_Example::Bar::constructors->import; | ||||||||||
| 38 | 1 | ▶ | use FP::Show; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | is( show(Bar(1, 2)), | ||||||||||
| 40 | "bless(+{a => 1, b => 2}, 'FP_Struct_Show_Example::Bar')" ); | ||||||||||
| 41 | |||||||||||
| 42 | =head1 DESCRIPTION | ||||||||||
| 43 | |||||||||||
| 44 | This class, when listed as a superclass of an L<FP::Struct>, | ||||||||||
| 45 | automatically implements the L<FP::Abstract::Show> protocol | ||||||||||
| 46 | (i.e. generates an `FP_Show_show` method that uses inspection specific | ||||||||||
| 47 | to FP::Struct classes to get to know the public field values of the | ||||||||||
| 48 | object it is being called on, and reconstructs a constructor call | ||||||||||
| 49 | based on this information.) For the typical `FP::Struct` based class | ||||||||||
| 50 | that don't have a preferred custom constructor this will be the right | ||||||||||
| 51 | thing. | ||||||||||
| 52 | |||||||||||
| 53 | =head1 SEE ALSO | ||||||||||
| 54 | |||||||||||
| 55 | Creates implementations for: L<FP::Abstract::Show> | ||||||||||
| 56 | |||||||||||
| 57 | L<FP::Struct::Equal> | ||||||||||
| 58 | |||||||||||
| 59 | =head1 NOTE | ||||||||||
| 60 | |||||||||||
| 61 | This is alpha software! Read the status section in the package README | ||||||||||
| 62 | or on the L<website|http://functional-perl.org/>. | ||||||||||
| 63 | |||||||||||
| 64 | =cut | ||||||||||
| 65 | |||||||||||
| 66 | package FP::Struct::Show; | ||||||||||
| 67 | |||||||||||
| 68 | 61 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 69 | 61 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 70 | 61 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 71 | |||||||||||
| 72 | 61 | ▶ | use base 'FP::Abstract::Show'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 73 | |||||||||||
| 74 | 2 | ▶ | sub FP_Show_show { | ||||||||
|
Subroutine
FP_Show_show: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 75 | 2 | my ($self, $show) = @_; | |||||||||
| 76 | 2 | my $class = ref($self); | |||||||||
| 77 | 2 | ▶ | length $class or die "FP_Show_show called on non-object: $self"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 2 | my $fieldnames = do { | |||||||||
| 79 | 61 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 80 | 2 | \@{"${class}::__Struct__fields"} | |||||||||
| 81 | }; | ||||||||||
| 82 | 2 | my @class_parts = split /::/, $class; | |||||||||
| 83 | ( | ||||||||||
| 84 | $class_parts[-1] . "(" . join( | ||||||||||
| 85 | ", ", | ||||||||||
| 86 | map { | ||||||||||
| 87 | 2 | my $fieldname = FP::Struct::field_name($_); | |||||||||
| 88 | 5 | &$show($self->$fieldname) | |||||||||
| 89 | } FP::Struct::all_fields([$class]) | ||||||||||
| 90 | ) | ||||||||||
| 91 | . ")" | ||||||||||
| 92 | ) | ||||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 61 | 1 | |||||||||