1Data::Section(3)      User Contributed Perl Documentation     Data::Section(3)
2
3
4

NAME

6       Data::Section - read multiple hunks of data out of your DATA section
7

VERSION

9       version 0.200008
10

SYNOPSIS

12         package Letter::Resignation;
13         use Data::Section -setup;
14
15         sub quit {
16           my ($class, $angry, %arg) = @_;
17
18           my $template = $class->section_data(
19             ($angry ? "angry_" : "professional_") . "letter"
20           );
21
22           return fill_in($$template, \%arg);
23         }
24
25         __DATA__
26         __[ angry_letter ]__
27         Dear jerks,
28
29           I quit!
30
31         --
32         {{ $name }}
33         __[ professional_letter ]__
34         Dear {{ $boss }},
35
36           I quit, jerks!
37
38
39         --
40         {{ $name }}
41

DESCRIPTION

43       Data::Section provides an easy way to access multiple named chunks of
44       line-oriented data in your module's DATA section.  It was written to
45       allow modules to store their own templates, but probably has other
46       uses.
47

PERL VERSION

49       This library should run on perls released even a long time ago.  It
50       should work on any version of perl released in the last five years.
51
52       Although it may work on older versions of perl, no guarantee is made
53       that the minimum required version will not be increased.  The version
54       may be increased for any reason, and there is no promise that patches
55       will be accepted to lower the minimum required perl.
56

WARNING

58       You will need to use "__DATA__" sections and not "__END__" sections.
59       Yes, it matters.  Who knew!
60

EXPORTS

62       To get the methods exported by Data::Section, you must import like
63       this:
64
65         use Data::Section -setup;
66
67       Optional arguments may be given to Data::Section like this:
68
69         use Data::Section -setup => { ... };
70
71       Valid arguments are:
72
73         encoding     - if given, gives the encoding needed to decode bytes in
74                        data sections; default; UTF-8
75
76                        the special value "bytes" will leave the bytes in the string
77                        verbatim
78
79         inherit      - if true, allow packages to inherit the data of the packages
80                        from which they inherit; default: true
81
82         header_re    - if given, changes the regex used to find section headers
83                        in the data section; it should leave the section name in $1
84
85         default_name - if given, allows the first section to has no header and set
86                        its name
87
88       Three methods are exported by Data::Section:
89
90   section_data
91         my $string_ref = $pkg->section_data($name);
92
93       This method returns a reference to a string containing the data from
94       the name section, either in the invocant's "DATA" section or in that of
95       one of its ancestors.  (The ancestor must also derive from the class
96       that imported Data::Section.)
97
98       By default, named sections are delimited by lines that look like this:
99
100         __[ name ]__
101
102       You can use as many underscores as you want, and the space around the
103       name is optional.  This pattern can be configured with the "header_re"
104       option (see above).  If present, a single leading "\" is removed, so
105       that sections can encode lines that look like section delimiters.
106
107       When a line containing only "__END__" is reached, all processing of
108       sections ends.
109
110   section_data_names
111         my @names = $pkg->section_data_names;
112
113       This returns a list of all the names that will be recognized by the
114       "section_data" method.
115
116   merged_section_data
117         my $data = $pkg->merged_section_data;
118
119       This method returns a hashref containing all the data extracted from
120       the package data for all the classes from which the invocant inherits
121       -- as long as those classes also inherit from the package into which
122       Data::Section was imported.
123
124       In other words, given this inheritance tree:
125
126         A
127          \
128           B   C
129            \ /
130             D
131
132       ...if Data::Section was imported by A, then when D's
133       "merged_section_data" is invoked, C's data section will not be
134       considered.  (This prevents the read position of C's data handle from
135       being altered unexpectedly.)
136
137       The keys in the returned hashref are the section names, and the values
138       are references to the strings extracted from the data sections.
139
140   merged_section_data_names
141         my @names = $pkg->merged_section_data_names;
142
143       This returns a list of all the names that will be recognized by the
144       "merged_section_data" method.
145
146   local_section_data
147         my $data = $pkg->local_section_data;
148
149       This method returns a hashref containing all the data extracted from
150       the package on which the method was invoked.  If called on an object,
151       it will operate on the package into which the object was blessed.
152
153       This method needs to be used carefully, because it's weird.  It returns
154       only the data for the package on which it was invoked.  If the package
155       on which it was invoked has no data sections, it returns an empty
156       hashref.
157
158   local_section_data_names
159         my @names = $pkg->local_section_data_names;
160
161       This returns a list of all the names that will be recognized by the
162       "local_section_data" method.
163

TIPS AND TRICKS

165   MooseX::Declare and namespace::autoclean
166       The namespace::autoclean library automatically cleans foreign routines
167       from a class, including those imported by Data::Section.
168
169       MooseX::Declare does the same thing, and can also cause your "__DATA__"
170       section to appear outside your class's package.
171
172       These are easy to address.  The Sub::Exporter::ForMethods library
173       provides an installer that will cause installed methods to appear to
174       come from the class and avoid autocleaning.  Using an explicit
175       "package" statement will keep the data section in the correct package.
176
177          package Foo;
178
179          use MooseX::Declare;
180          class Foo {
181
182            # Utility to tell Sub::Exporter modules to export methods.
183            use Sub::Exporter::ForMethods qw( method_installer );
184
185            # method_installer returns a sub.
186            use Data::Section { installer => method_installer }, -setup;
187
188            method my_method {
189               my $content_ref = $self->section_data('SectionA');
190
191               print $$content_ref;
192            }
193          }
194
195          __DATA__
196          __[ SectionA ]__
197          Hello, world.
198

SEE ALSO

200       •   article for RJBS Advent 2009
201           <http://advent.rjbs.manxome.org/2009/2009-12-09.html>
202
203       •   Inline::Files does something that is at first look similar,
204
205           but it works with source filters, and contains the warning:
206
207             It is possible that this module may overwrite the source code in files that
208             use it. To protect yourself against this possibility, you are strongly
209             advised to use the -backup option described in "Safety first".
210
211           Enough said.
212

AUTHOR

214       Ricardo SIGNES <cpan@semiotic.systems>
215

CONTRIBUTORS

217       •   Christian Walde <walde.christian@googlemail.com>
218
219       •   Dan Kogai <dankogai+github@gmail.com>
220
221       •   David Golden <dagolden@cpan.org>
222
223       •   David Steinbrunner <dsteinbrunner@pobox.com>
224
225       •   Graham Ollis <plicease@cpan.org>
226
227       •   Karen Etheridge <ether@cpan.org>
228
229       •   Kenichi Ishigaki <ishigaki@cpan.org>
230
231       •   kentfredric <kentfredric+gravitar@gmail.com>
232
233       •   Ricardo Signes <rjbs@semiotic.systems>
234
235       •   Tatsuhiko Miyagawa <miyagawa@bulknews.net>
236
238       This software is copyright (c) 2008 by Ricardo SIGNES.
239
240       This is free software; you can redistribute it and/or modify it under
241       the same terms as the Perl 5 programming language system itself.
242
243
244
245perl v5.36.0                      2023-01-20                  Data::Section(3)
Impressum