NAME

Sub::Meta - handle subroutine meta information

SYNOPSIS

use Sub::Meta;

sub hello { }
my $meta = Sub::Meta->new(\&hello);
$meta->subname; # => hello
$meta->apply_subname('world'); # rename subroutine name

# specify parameters types ( without validation )
$meta->set_parameters( Sub::Meta::Parameters->new(args => [ { type => 'Str' }]) );
$meta->parameters->args; # => Sub::Meta::Param->new({ type => 'Str' })

# specify returns types ( without validation )
$meta->set_returns( Sub::Meta::Returns->new('Str') );
$meta->returns->scalar; # => 'Str'

DESCRIPTION

Sub::Meta provides methods to handle subroutine meta information. In addition to information that can be obtained from subroutines using module B etc., subroutines can have meta information such as arguments and return values.

METHODS

Constructor

new

Constructor of Sub::Meta.

Getter

sub

A subroutine reference

subname

A subroutine name, e.g. hello

fullname

A subroutine full name, e.g. main::hello

stashname

A subroutine stash name, e.g. main

file

A filename where subroutine is defined, e.g. path/to/main.pl.

line

A line where the definition of subroutine started.

is_constant

A boolean value indicating whether the subroutine is a constant or not.

prototype

A prototype of subroutine reference.

attribute

A attribute of subroutine reference.

is_method

A boolean value indicating whether the subroutine is a method or not.

parameters

Parameters object of Sub::Meta::Parameters.

returns

Returns object of Sub::Meta::Returns.

Setter

You can set meta information of subroutine. set_xxx sets xxx and does not affect subroutine reference. On the other hands, apply_xxx sets xxx and apply xxx to subroutine reference.

Setter methods of Sub::Meta returns meta object. So you can chain setting:

$meta->set_subname('foo')
     ->set_stashname('Some')

set_xxx

set_sub($)

set_subname($)

set_fullname($)

set_stashname($)

set_file($)

set_line($)

set_is_constant($)

set_prototype($)

set_attribute($)

set_is_method($)

set_parameters($)

Sets the parameters object of Sub::Meta::Parameters or any object:

my $meta = Sub::Meta->new;
$meta->set_parameters({ type => 'Type'});
$meta->parameters; # => Sub::Meta::Parameters->new({type => 'Type'});

# or
$meta->set_parameters(Sub::Meta::Parameters->new(type => 'Foo'));
$meta->set_parameters(MyParamters->new)

set_returns($)

Sets the returns object of Sub::Meta::Returns or any object.

my $meta = Sub::Meta->new;
$meta->set_returns({ type => 'Type'});
$meta->returns; # => Sub::Meta::Returns->new({type => 'Type'});

# or
$meta->set_returns(Sub::Meta::Returns->new(type => 'Foo'));
$meta->set_returns(MyReturns->new)

apply_xxx

apply_subname($)

apply_prototype($)

apply_attribute(@)

NOTE

Pure-Perl version

By default Sub::Meta tries to load an XS implementation for speed. If that fails, or if the environment variable PERL_SUB_META_PP is defined to a true value, it will fall back to a pure perl implementation.

SEE ALSO

Sub::Identify, Sub::Util, Sub::Info, Function::Paramters::Info, Function::Return::Info

LICENSE

Copyright (C) kfly8.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

kfly8 <kfly@cpan.org>