The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Sub::Meta::Creator - creator of Sub::Meta by code reference

SYNOPSIS

    use Sub::Meta::Creator;

    sub finder {
        my $sub = shift;
        return +{ sub => $sub }
    }

    my $creator = Sub::Meta::Creator->new(
        finders => [ \&finder ],
    );

    sub foo { }
    my $meta = $creator->create(\&foo);

DESCRIPTION

This module provides convenient ways to create Sub::Meta. The purpose of this module is to make it easier to associate Sub::Meta with information of code references. For example, Function::Parameters can retrieve not only subroutine names and packages from code references, but also argument type information, etc. Sub::Meta::Creator can be generated Sub::Meta with such information:

    use Sub::Meta::Creator;
    use Sub::Meta::Finder::FunctionParameters;

    my $creator = Sub::Meta::Creator->new(
        finders => [ \&Sub::Meta::Finder::FunctionParameters::find_materials ]
    );

    use Function::Parameters;
    use Types::Standard -types;

    fun hello(Str $msg) { }
    my $meta = $creator->create(\&hello);
    my $args = $meta->args; # [ Sub::Meta::Param->new(name => '$msg', type => Str) ]

METHODS

new

Constructor of Sub::Meta::Creator. This constructor requires finders:

    my $creator = Sub::Meta::Creator->new(
        finders => [ sub { my $sub = shift; +{ sub => $sub } } ]
    );

finders

Return elements of finder. The type of finders is ArrayRef[CodeRef]. CodeRef, an element of finders, finds information from the code reference of the first argument, processes the information to become the argument of Sub::Meta#new, and returns it.

find_materials($sub)

From the code reference, find the material for Sub::Meta#new.

create($sub)

From the code reference, create the instance of Sub::Meta.

sub_meta_class

Returns class name of Sub::Meta. default: Sub::Meta Please override for customization.

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>