| 1 | package Caller::Easy; | ||
| 2 | use 5.014004; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp; | ||
| 6 | |||
| 7 | our $VERSION = "0.02"; | ||
| 8 | |||
| 9 | use overload '""' => sub{ $_[0]->package() }, fallback => 1; | ||
| 10 | |||
| 11 | use Moose; | ||
| 12 | |||
| 13 | has 'depth' => ( is => 'ro', isa => 'Maybe[Num]' ); | ||
| 14 | has 'package' => ( is => 'ro', writer => '_set_package', isa => 'Str' ); | ||
| 15 | has 'filename' => ( is => 'ro', writer => '_set_filename', isa => 'Str' ); | ||
| 16 | has 'line' => ( is => 'ro', writer => '_set_line', isa => 'Num' ); | ||
| 17 | has 'subroutine' => ( is => 'ro', writer => '_set_subroutine', isa => 'Str' ); | ||
| 18 | has 'hasargs' => ( is => 'ro', writer => '_set_hasargs', isa => 'Bool' ); | ||
| 19 | has 'wantarray' => ( is => 'ro', writer => '_set_wantarray', isa => 'Bool' ); | ||
| 20 | has 'evaltext' => ( is => 'ro', writer => '_set_evaltext', isa => 'Str' ); | ||
| 21 | has 'is_require' => ( is => 'ro', writer => '_set_is_require', isa => 'Bool' ); | ||
| 22 | has 'hints' => ( is => 'ro', writer => '_set_hints', isa => 'Num' ); | ||
| 23 | has 'bitmask' => ( is => 'ro', writer => '_set_bitmask', isa => 'Str' ); | ||
| 24 | has 'hinthash' => ( is => 'ro', writer => '_set_hinthash', isa => 'Maybe[HashRef]' ); | ||
| 25 | has 'args' => ( is => 'ro', writer => '_set_args', isa => 'Maybe[ArrayRef]' ); | ||
| 26 | |||
| 27 | around BUILDARGS => sub { | ||
| 28 | my $orig = shift; | ||
| 29 | my $class = shift; | ||
| 30 | |||
| 31 | if( @_ == 1 ) { | ||
| 32 | return $class->$orig( depth => $_[0] ) if $_[0] =~ /^\d+$/; | ||
| 33 | croak 'Unvalid depth was assigned'; | ||
| 34 | }elsif( @_ > 2 ) { | ||
| 35 | croak 'Too many arguments for caller'; | ||
| 36 | }elsif( @_ == 2 and not exists $_{depth} ) { | ||
| 37 | croak 'Unvalid arguments for caller'; | ||
| 38 | }else{ | ||
| 39 | return $class->$orig(@_); | ||
| 40 | } | ||
| 41 | }; | ||
| 42 | |||
| 43 | sub BUILD { | ||
| 44 | my $self = shift; | ||
| 45 | my $depth = $self->depth(); | ||
| 46 | |||
| 47 | my ( | ||
| 48 | $package, $filename, $line, | ||
| 49 | $subroutine, $hasargs, $wantarray, $evaltext, | ||
| 50 | $is_require, $hints, $bitmask, $hinthash | ||
| 51 | ); | ||
| 52 | |||
| 53 | |||
| 54 | if( defined $depth and $depth =~ /^\d+$/ ) { | ||
| 55 | package DB { | ||
| 56 | our @args; | ||
| 57 | my $i = 1; | ||
| 58 | do { | ||
| 59 | ( $package, $filename, $line ) = CORE::caller($i++); | ||
| 60 | } while( $package =~ /^Test::/ or $package =~ /^Caller::Easy/ ); | ||
| 61 | |||
| 62 | ( | ||
| 63 | undef, undef, undef, | ||
| 64 | $subroutine, $hasargs, $wantarray, $evaltext, | ||
| 65 | $is_require, $hints, $bitmask, $hinthash | ||
| 66 | ) = CORE::caller( $depth + $i++ ); | ||
| 67 | } | ||
| 68 | }else{ | ||
| 69 | my $i = 1; | ||
| 70 | do { | ||
| 71 | ( $package, $filename, $line ) = CORE::caller($i++); | ||
| 72 | } while( $package =~ /^Test::/ or $package =~ /^Caller::Easy/ ); | ||
| 73 | |||
| 74 | $self->_set_package($package) if $package; | ||
| 75 | $self->_set_filename($filename) if $filename; | ||
| 76 | $self->_set_line($line) if $line; | ||
| 77 | |||
| 78 | return $self unless wantarray; | ||
| 79 | return ( $package, $filename, $line ); | ||
| 80 | } | ||
| 81 | |||
| 82 | $self->_set_package($package) if $package; | ||
| 83 | $self->_set_filename($filename) if $filename; | ||
| 84 | $self->_set_line($line) if $line; | ||
| 85 | $self->_set_subroutine($subroutine) if $subroutine; | ||
| 86 | $self->_set_hasargs($hasargs) if defined $hasargs; | ||
| 87 | $self->_set_wantarray($wantarray) if defined $wantarray; | ||
| 88 | $self->_set_evaltext($evaltext) if $evaltext; | ||
| 89 | $self->_set_is_require($is_require) if defined $is_require; | ||
| 90 | $self->_set_hints($hints) if $hints; | ||
| 91 | $self->_set_bitmask($bitmask) if $bitmask; | ||
| 92 | $self->_set_hinthash($hinthash) if $hinthash; | ||
| 93 | $self->_set_args(\@DB::args); | ||
| 94 | |||
| 95 | return $self unless wantarray; | ||
| 96 | return ( | ||
| 97 | $package, $filename, $line, | ||
| 98 | $subroutine, $hasargs, $wantarray, $evaltext, | ||
| 99 | $is_require, $hints, $bitmask, $hinthash | ||
| 100 | ); | ||
| 101 | } | ||
| 102 | |||
| 103 | __PACKAGE__->meta->make_immutable; | ||
| 104 | no Moose; | ||
| 105 | |||
| 106 | sub caller { | ||
| 107 | __PACKAGE__->new(@_); | ||
| 108 | } | ||
| 109 | |||
| 110 | sub import { | ||
| 111 | my $packagename = CORE::caller; | ||
| 112 | no strict 'refs'; | ||
| 113 | *{"$packagename\::caller"} = \&caller; | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; | ||
| 117 | __END__ | ||
| 118 | |||
| 119 | =encoding utf-8 | ||
| 120 | |||
| 121 | =head1 NAME | ||
| 122 | |||
| 123 | Caller::Easy - less stress to remind returned list from CORE::caller() | ||
| 124 | |||
| 125 | =head1 SYNOPSIS | ||
| 126 | |||
| 127 | use Caller::Easy; # Module name is temporal | ||
| 128 | |||
| 129 | # the way up to now | ||
| 130 | sub foo { | ||
| 131 | my $subname = (caller(0))[3]; | ||
| 132 | } | ||
| 133 | |||
| 134 | # with OO | ||
| 135 | sub foo { | ||
| 136 | my $subname = Caller::Easy->new(0)->subroutine(); | ||
| 137 | } | ||
| 138 | |||
| 139 | # like a function imported | ||
| 140 | sub foo { | ||
| 141 | my $subname = caller(0)->subroutine(); | ||
| 142 | } | ||
| 143 | |||
| 144 | All the above will return 'main::foo' | ||
| 145 | |||
| 146 | Now you can choise the way you much prefer | ||
| 147 | |||
| 148 | =head1 DESCRIPTION | ||
| 149 | |||
| 150 | Caller::Easy is the easiest way for using functions of C<CORE::caller()> | ||
| 151 | |||
| 152 | it produces the easier way to get some info from C<caller()> with no having to care about namespace. | ||
| 153 | |||
| 154 | =head2 Constructor and initialization | ||
| 155 | |||
| 156 | =head3 new() | ||
| 157 | |||
| 158 | You can set no argument then it returns the object reference in scalar context. | ||
| 159 | |||
| 160 | In list context, you can get just only ( $package, $filename, $line ). | ||
| 161 | |||
| 162 | if you set depth(level) like C<new(1)>, you can get more info from caller | ||
| 163 | ( $package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, | ||
| 164 | $is_require, $hints, $bitmask, $hinthash ) | ||
| 165 | directly with same term for C<CORE::caller()> | ||
| 166 | |||
| 167 | To be strictly, you can set C<depth> parameter like C<new( depth =E<gt> 1 )> | ||
| 168 | but we can forget it, just set the natural number you want to set. | ||
| 169 | |||
| 170 | =head2 Methods (All of them is read-only) | ||
| 171 | |||
| 172 | =head3 caller() | ||
| 173 | |||
| 174 | It is implemented to be alias of C<new()> but it doesn't matter. | ||
| 175 | |||
| 176 | this method is imported to your packages automatically when you C<use Caller::Easy;> | ||
| 177 | |||
| 178 | So you can use much freely this method like if there is no module imported. | ||
| 179 | |||
| 180 | =head3 package() | ||
| 181 | |||
| 182 | You can get package name instead of C<(caller(n))[0]> | ||
| 183 | |||
| 184 | =head3 filename() | ||
| 185 | |||
| 186 | You can get file name instead of C<(caller(n))[1]> | ||
| 187 | |||
| 188 | =head3 line() | ||
| 189 | |||
| 190 | You can get the line called instead of C<(caller(n))[2]> | ||
| 191 | |||
| 192 | =head3 subroutine() | ||
| 193 | |||
| 194 | You can get the name of subroutine instead of C<(caller(n))[3]> | ||
| 195 | |||
| 196 | =head3 hasargs(), wantarray(), evaltext(), is_require(), hints(), bitmask(), hinthash() | ||
| 197 | |||
| 198 | Please read L<CORE::caller|http://perldoc.perl.org/functions/caller.html> | ||
| 199 | |||
| 200 | B<Don't ask me> | ||
| 201 | |||
| 202 | =head3 args() | ||
| 203 | |||
| 204 | You can get the arguments of targeted subroutine instead of C<@DB::args> | ||
| 205 | |||
| 206 | This method is the B<unique> point of this module. | ||
| 207 | |||
| 208 | =head3 depth() | ||
| 209 | |||
| 210 | You can get what you set. | ||
| 211 | |||
| 212 | =head1 TODO | ||
| 213 | |||
| 214 | =over | ||
| 215 | |||
| 216 | =item using Moose is a bottle-neck | ||
| 217 | |||
| 218 | I made this module in a few days with Moose because it's the easiest way. | ||
| 219 | It will be too heavy for some environments. | ||
| 220 | |||
| 221 | To abolish Moose is a TODO if this module will be popular. | ||
| 222 | |||
| 223 | =item rewite the tests | ||
| 224 | |||
| 225 | I don't know well about L<CORE::caller|http://perldoc.perl.org/functions/caller.html>! | ||
| 226 | |||
| 227 | Why I have written this module is | ||
| 228 | Just only I can't remember what I wanna get with something number from caller() | ||
| 229 | without some reference. | ||
| 230 | |||
| 231 | So some of tests may not be appropriate. | ||
| 232 | |||
| 233 | =item rename the module | ||
| 234 | |||
| 235 | I have to find the name that deserve it. | ||
| 236 | |||
| 237 | =item rewite the POD | ||
| 238 | |||
| 239 | I know well that my English is awful. | ||
| 240 | |||
| 241 | =back | ||
| 242 | |||
| 243 | =head1 SEE ALSO | ||
| 244 | |||
| 245 | =over | ||
| 246 | |||
| 247 | =item L<CORE::caller|http://perldoc.perl.org/functions/caller.html> | ||
| 248 | |||
| 249 | If you are confused with this module, Please read this carefully. | ||
| 250 | |||
| 251 | =item L<Perl6::Caller|http://search.cpan.org/~ovid/Perl6-Caller/lib/Perl6/Caller.pm> | ||
| 252 | |||
| 253 | One of better implements for using something like this module. | ||
| 254 | |||
| 255 | The reason why I reinvent the wheel is that this module has no github repository. | ||
| 256 | |||
| 257 | =item L<Safe::Caller|https://github.com/stsc/Safe-Caller> | ||
| 258 | |||
| 259 | The newest implement for using something like this module. | ||
| 260 | |||
| 261 | It has github repository but usage is limited. | ||
| 262 | |||
| 263 | =back | ||
| 264 | |||
| 265 | =head1 LICENSE | ||
| 266 | |||
| 267 | Copyright (C) Yuki Yoshida. | ||
| 268 | |||
| 269 | This library is free software; you can redistribute it and/or modify | ||
| 270 | it under the same terms as Perl itself. | ||
| 271 | |||
| 272 | =head1 AUTHOR | ||
| 273 | |||
| 274 | Yuki Yoshida E<lt>worthmine@gmail.comE<gt> | ||
| 275 | |||
| 276 | =cut |