File: PropertyModule.pm

package info (click to toggle)
libperinci-sub-util-propertymodule-perl 0.46-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 140 kB
  • sloc: perl: 52; makefile: 2
file content (144 lines) | stat: -rw-r--r-- 3,969 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package Perinci::Sub::Util::PropertyModule;

our $DATE = '2016-05-15'; # DATE
our $VERSION = '0.46'; # VERSION

use 5.010001;
use strict;
use warnings;

use Exporter qw(import);
our @EXPORT_OK = qw(
                       get_required_property_modules
               );

sub get_required_property_modules {
    no warnings 'once';
    no warnings 'redefine';

    my $meta = shift;

    # Here's how it works: first we reset the
    # $Sah::Schema::rinci::functio_meta::schema structure which contains the
    # list of supported properties. This is done by emptying it and
    # force-reloading the Sah::Schema::rinci::function_meta module, which is the
    # module responsible for declaring the structure.
    #
    # We also delete Perinci::Sub::Property::* entries from %INC to force-reload
    # them. We then record %INC at this point (1).
    #
    # Then we run $meta to normalize_function_data(), which will load additional
    # Perinci::Sub::Property::* modules that are needed.
    #
    # Finally we compare the previous content %INC (at point (1)) with the
    # current %INC. We now get the list of required Perinci::Sub::Property::*
    # modules.

    %Sah::Schema::rinci::function_meta::schema = ();
    delete $INC{'Sah/Schema/rinci/function_meta.pm'};
    require Sah::Schema::rinci::function_meta;

    for (grep {m!^Perinci/Sub/Property/!} keys %INC) {
        delete $INC{$_};
    }

    require Perinci::Sub::Normalize;

    my %inc_before = %INC;
    Perinci::Sub::Normalize::normalize_function_metadata($meta);

    my %res;
    for (keys %INC) {
        next unless m!^Perinci/Sub/Property/!;
        next if $inc_before{$_};
        $res{$_} = 1;
    }

    [map {my $mod = $_; $mod =~ s!/!::!g; $mod =~ s/\.pm\z//; $mod}
         sort keys %res];
}

1;
# ABSTRACT: Given a Rinci function metadata, find what property modules are required

__END__

=pod

=encoding UTF-8

=head1 NAME

Perinci::Sub::Util::PropertyModule - Given a Rinci function metadata, find what property modules are required

=head1 VERSION

This document describes version 0.46 of Perinci::Sub::Util::PropertyModule (from Perl distribution Perinci-Sub-Util-PropertyModule), released on 2016-05-15.

=head1 SYNOPSIS

 use Perinci::Sub::Util::PropertyModule qw(get_required_property_modules);

 my $meta = {
     v => 1.1,
     args => {
         foo => {
             ...
             'form.widget' => '...',
         },
         bar => {},
     },
     'cmdline.skip_format' => 1,
     result => {
         table => { ... },
     },
 };
 my $mods = get_required_property_modules($meta);

Result:

 ['Perinci::Sub::Property::arg::form',
  'Perinci::Sub::Property::cmdline',
  'Perinci::Sub::Property::result::table']

=head1 FUNCTIONS

=head2 get_required_property_modules($meta) => array

Since the Perinci framework is modular, additional properties can be introduced
by additional property modules (C<Perinci::Sub::Property::*>). These properties
might be experimental, 3rd party, etc.

This function can detect which modules are used.

This function can be used during distribution building to automatically add
those modules as prerequisites.

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/Perinci-Sub-Util-PropertyModule>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-Perinci-Sub-Util-PropertyModule>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Perinci-Sub-Util-PropertyModule>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by perlancar@cpan.org.

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

=cut