Skip to content

Commit

Permalink
Merge 9df512f into 3756ac5
Browse files Browse the repository at this point in the history
  • Loading branch information
kfly8 committed Jul 4, 2019
2 parents 3756ac5 + 9df512f commit a39313d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ A subroutine stash name, e.g. `main`

Setter for subroutine stash name.

## subinfo

A subroutine information, e.g. `['main', 'hello']`

## set\_subinfo(\[$stashname, $subname\])

Setter for subroutine information.

## file

A filename where subroutine is defined, e.g. `path/to/main.pl`.
Expand Down
42 changes: 31 additions & 11 deletions lib/Sub/Meta.pm
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ sub new {
}

sub sub() { $_[0]{sub} }
sub subname() { $_[0]{subname} ||= $_[0]->_build_subname }
sub fullname() { $_[0]{fullname} ||= $_[0]->_build_fullname }
sub stashname() { $_[0]{stashname} ||= $_[0]->_build_stashname }
sub subname() { $_[0]->subinfo->[1] || '' }
sub stashname() { $_[0]->subinfo->[0] || '' }
sub fullname() { join '::', @{$_[0]->subinfo} }
sub subinfo() { $_[0]{subinfo} ||= $_[0]->_build_subinfo }
sub file() { $_[0]{file} ||= $_[0]->_build_file }
sub line() { $_[0]{line} ||= $_[0]->_build_line }
sub is_constant() { $_[0]{is_constant} ||= $_[0]->_build_is_constant }
Expand All @@ -40,10 +41,23 @@ sub is_method() { $_[0]{is_method} }
sub parameters() { $_[0]{parameters} }
sub returns() { $_[0]{returns} }

sub set_sub($) { $_[0]{sub} = $_[1]; $_[0] }
sub set_subname($) { $_[0]{subname} = $_[1]; $_[0] }
sub set_fullname($) { $_[0]{fullname} = $_[1]; $_[0] }
sub set_stashname($) { $_[0]{stashname} = $_[1]; $_[0] }
sub set_sub($) {
$_[0]{sub} = $_[1];
$_[0]->subinfo; # rebuild subinfo
$_[0];
}

sub set_subname($) { $_[0]{subinfo}[1] = $_[1]; $_[0] }
sub set_stashname($) { $_[0]{subinfo}[0] = $_[1]; $_[0] }
sub set_fullname($) {
$_[0]{subinfo} = $_[1] =~ m!^(.+)::([^:]+)$! ? [$1, $2] : [];
$_[0];
}
sub set_subinfo($) {
$_[0]{subinfo} = @_ > 2 ? [ $_[1], $_[2] ] : $_[1];
$_[0];
}

sub set_file($) { $_[0]{file} = $_[1]; $_[0] }
sub set_line($) { $_[0]{line} = $_[1]; $_[0] }
sub set_is_constant($) { $_[0]{is_constant} = $_[1]; $_[0] }
Expand All @@ -63,9 +77,7 @@ sub set_returns($) {
return $self
}

sub _build_subname() { $_[0]->sub ? Sub::Identify::sub_name($_[0]->sub) : '' }
sub _build_fullname() { $_[0]->sub ? Sub::Identify::sub_fullname($_[0]->sub) : '' }
sub _build_stashname() { $_[0]->sub ? Sub::Identify::stash_name($_[0]->sub) : '' }
sub _build_subinfo() { $_[0]->sub ? [ Sub::Identify::get_code_info($_[0]->sub) ] : [] }
sub _build_file() { $_[0]->sub ? (Sub::Identify::get_code_location($_[0]->sub))[0] : '' }
sub _build_line() { $_[0]->sub ? (Sub::Identify::get_code_location($_[0]->sub))[1] : undef }
sub _build_is_constant() { $_[0]->sub ? Sub::Identify::is_sub_constant($_[0]->sub) : undef }
Expand All @@ -75,8 +87,8 @@ sub _build_attribute() { $_[0]->sub ? [ attributes::get($_[0]->sub) ] : undef
sub apply_subname($) {
my ($self, $subname) = @_;
_croak 'apply_subname requires subroutine reference' unless $self->sub;
Sub::Util::set_subname($subname, $self->sub);
$self->set_subname($subname);
Sub::Util::set_subname($self->fullname, $self->sub);
return $self;
}

Expand Down Expand Up @@ -181,6 +193,14 @@ A subroutine stash name, e.g. C<main>
Setter for subroutine stash name.
=head2 subinfo
A subroutine information, e.g. C<['main', 'hello']>
=head2 set_subinfo([$stashname, $subname])
Setter for subroutine information.
=head2 file
A filename where subroutine is defined, e.g. C<path/to/main.pl>.
Expand Down
22 changes: 18 additions & 4 deletions t/01_meta.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use Test2::V0;

use Sub::Meta;
use Sub::Identify;

subtest 'non sub' => sub {
my $meta = Sub::Meta->new;
Expand Down Expand Up @@ -29,7 +30,7 @@ subtest 'has sub' => sub {
is $meta->fullname, 'main::hello', 'fullname';
is $meta->stashname, 'main', 'stashname';
is $meta->file, 't/01_meta.t', 'file';
is $meta->line, 23, 'line';
is $meta->line, 24, 'line';
ok !$meta->is_constant, 'is_constant';
is $meta->prototype, '$$', 'prototype';
is $meta->attribute, ['method'], 'attribute';
Expand All @@ -44,12 +45,25 @@ subtest 'has sub' => sub {
my $meta = Sub::Meta->new(sub => \&hello);
is $meta->set_sub(\&hello2), $meta, 'set_sub';
is $meta->sub, \&hello2, 'subname';

is $meta->set_subname('world'), $meta, 'set_subname';
is $meta->subname, 'world', 'subname';
is $meta->set_fullname('test::world'), $meta, 'set_fullname';
is $meta->fullname, 'test::world', 'fullname';
is $meta->stashname, 'main', 'stashname';
is $meta->fullname, 'main::world', 'fullname';
is $meta->subinfo, ['main', 'world'], 'subinfo';

is $meta->set_fullname('foo::bar::baz'), $meta, 'set_fullname';
is $meta->subname, 'baz', 'subname';
is $meta->fullname, 'foo::bar::baz', 'fullname';
is $meta->stashname, 'foo::bar', 'stashname';
is $meta->subinfo, ['foo::bar', 'baz'], 'subinfo';

is $meta->set_stashname('test'), $meta, 'set_stashname';
is $meta->subname, 'baz', 'subname';
is $meta->fullname, 'test::baz', 'fullname';
is $meta->stashname, 'test', 'stashname';
is $meta->subinfo, ['test', 'baz'], 'subinfo';

is $meta->set_file('test/file.t'), $meta, 'set_file';
is $meta->file, 'test/file.t', 'file';
is $meta->set_line(999), $meta, 'set_line';
Expand All @@ -73,7 +87,7 @@ subtest 'has sub' => sub {
my $meta = Sub::Meta->new(sub => \&hello3);
is $meta->apply_subname('HELLO'), $meta, 'apply_subname';
is $meta->subname, 'HELLO', 'subname';
is $meta->_build_subname, 'HELLO', 'build_subname';
is [ Sub::Identify::get_code_info(\&hello3) ], ['main','HELLO'], 'build_subinfo';

is $meta->apply_prototype('$'), $meta, 'apply_prototype';
is $meta->prototype, '$', 'prototype';
Expand Down

0 comments on commit a39313d

Please sign in to comment.