| 1 | package FindApp::Utils::Objects; | ||
| 2 | |||
| 3 | use v5.10; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use FindApp::Vars qw( :all ); | ||
| 8 | use FindApp::Utils::Foreign qw( | ||
| 9 | blessed | ||
| 10 | pp | ||
| 11 | reftype | ||
| 12 | refaddr | ||
| 13 | ); | ||
| 14 | |||
| 15 | ################################################################# | ||
| 16 | |||
| 17 | sub as_number ( $ ) ; | ||
| 18 | sub as_string ( $ ) ; | ||
| 19 | sub class_prune ( _ ) ; | ||
| 20 | sub myself ; | ||
| 21 | sub op_equals ( $$$ ) ; | ||
| 22 | sub op_notequals ( $$$ ) ; | ||
| 23 | sub pkg_abbr ( $ ) ; | ||
| 24 | |||
| 25 | ################################################################# | ||
| 26 | |||
| 27 | use Exporter qw(import); | ||
| 28 | our $VERSION = v1.0; | ||
| 29 | our @EXPORT_OK = qw( | ||
| 30 | as_number | ||
| 31 | as_string | ||
| 32 | class_prune | ||
| 33 | myself | ||
| 34 | op_equals | ||
| 35 | op_notequals | ||
| 36 | pkg_abbr | ||
| 37 | |||
| 38 | ); | ||
| 39 | our %EXPORT_TAGS = ( | ||
| 40 | all => \@EXPORT_OK, | ||
| 41 | overload => [ grep /^(?:as|op)_/, @EXPORT_OK ], | ||
| 42 | ); | ||
| 43 | |||
| 44 | ################################################################# | ||
| 45 | |||
| 46 | # Have to do this the hard way instead of just calling | ||
| 47 | # PACKAGE($1)->abbreviate because of load-order issues. | ||
| 48 | sub pkg_abbr($) { | ||
| 49 | my $long_pack = shift; | ||
| 50 | require FindApp::Utils::Package::Object; | ||
| 51 | FindApp::Utils::Package::Object->new($long_pack)->abbreviate(1); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub class_prune(_) { | ||
| 55 | my($string) = @_; | ||
| 56 | return $string unless $FINDAPP_DEBUG_SHORTEN; | ||
| 57 | my @lines = split "\n", $string, -1; | ||
| 58 | for (@lines) { | ||
| 59 | s/\b(FindApp(?:::\w+)+)(?=::)/pkg_abbr($1)/ge; | ||
| 60 | s/^ / /; | ||
| 61 | s/^ / /; | ||
| 62 | s/^ //; | ||
| 63 | s/ (?==)// || s/ (?==)//; # this may not be a good idea | ||
| 64 | } | ||
| 65 | $string = join "\n", @lines; | ||
| 66 | return $string; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub as_string($) { | ||
| 70 | my $self = shift; | ||
| 71 | my $pretty = pp($self) . ";\n"; | ||
| 72 | $pretty = class_prune($pretty); | ||
| 73 | my $string = sprintf "%s=%s(%#x)", map { (ref, reftype, refaddr) } $self; | ||
| 74 | $pretty =~ s/.*\K/ # $string/; | ||
| 75 | return $pretty; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub as_number($) { | ||
| 79 | my($self) = @_; | ||
| 80 | no overloading; | ||
| 81 | return refaddr($self); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub op_equals($$$) { | ||
| 85 | my($this, $that, $swapped) = @_; | ||
| 86 | no overloading; | ||
| 87 | return $this == $that; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub op_notequals($$$) { !&op_equals } | ||
| 91 | |||
| 92 | |||
| 93 | # So that if you call an instance method on a class, you | ||
| 94 | # deliberately get the current singleton object instead. | ||
| 95 | sub myself { | ||
| 96 | my $self = shift; | ||
| 97 | return $self if blessed $self; | ||
| 98 | return $self->old; | ||
| 99 | } | ||
| 100 | |||
| 101 | 1; | ||
| 102 | |||
| 103 | =encoding utf8 | ||
| 104 | |||
| 105 | =head1 NAME | ||
| 106 | |||
| 107 | FindApp::Utils::Objects - FIXME | ||
| 108 | |||
| 109 | =head1 SYNOPSIS | ||
| 110 | |||
| 111 | use FindApp::Utils::Objects; | ||
| 112 | |||
| 113 | =head1 DESCRIPTION | ||
| 114 | |||
| 115 | =head2 Public Methods | ||
| 116 | |||
| 117 | =over | ||
| 118 | |||
| 119 | =item FIXME | ||
| 120 | |||
| 121 | =back | ||
| 122 | |||
| 123 | =head2 Exports | ||
| 124 | |||
| 125 | =over | ||
| 126 | |||
| 127 | =item as_number | ||
| 128 | |||
| 129 | =item as_string | ||
| 130 | |||
| 131 | =item class_prune | ||
| 132 | |||
| 133 | =item myself | ||
| 134 | |||
| 135 | Highly magical function that allows instance methods to act on a cached | ||
| 136 | class singleton when invoked as a class method not an instance | ||
| 137 | |||
| 138 | =item op_equals | ||
| 139 | |||
| 140 | =item op_notequals | ||
| 141 | |||
| 142 | =item pkg_abbr | ||
| 143 | |||
| 144 | =back | ||
| 145 | |||
| 146 | =head1 EXAMPLES | ||
| 147 | |||
| 148 | =head1 ENVIRONMENT | ||
| 149 | |||
| 150 | =head1 SEE ALSO | ||
| 151 | |||
| 152 | =over | ||
| 153 | |||
| 154 | =item L<FindApp> | ||
| 155 | |||
| 156 | =back | ||
| 157 | |||
| 158 | =head1 CAVEATS AND PROVISOS | ||
| 159 | |||
| 160 | =head1 BUGS AND LIMITATIONS | ||
| 161 | |||
| 162 | =head1 HISTORY | ||
| 163 | |||
| 164 | =head1 AUTHOR | ||
| 165 | |||
| 166 | Tom Christiansen << <tchrist@perl.com> >> | ||
| 167 | |||
| 168 | =head1 LICENCE AND COPYRIGHT | ||
| 169 | |||
| 170 | Copyright (c) 2016, Tom Christiansen C<< <tchrist@perl.com> >>. | ||
| 171 | All Rights Reserved. | ||
| 172 | |||
| 173 | This module is free software; you can redistribute it and/or | ||
| 174 | modify it under the same terms as Perl itself. See L<perlartistic>. | ||
| 175 | |||
| 176 | =cut | ||
| 177 |