.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.48) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Required to disable full justification in groff 1.23.0. .if n .ds AD l .\" ======================================================================== .\" .IX Title "Data::Section 3" .TH Data::Section 3 2026-08-26 "perl v5.44.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 2 \& package Letter::Resignation; \& use Data::Section \-setup; \& \& sub quit { \& my ($class, $angry, %arg) = @_; \& \& my $template = $class\->section_data( \& ($angry ? "angry_" : "professional_") . "letter" \& ); \& \& return fill_in($$template, \e%arg); \& } \& \& _\|_DATA_\|_ \& _\|_[ angry_letter ]_\|_ \& Dear jerks, \& \& I quit! \& \& \-\- \& {{ $name }} \& _\|_[ professional_letter ]_\|_ \& Dear {{ $boss }}, \& \& I quit, jerks! \& \& \& \-\- \& {{ $name }} .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Data::Section provides an easy way to access multiple named chunks of line\-oriented data in your module\*(Aqs DATA section. It was written to allow modules to store their own templates, but probably has other uses. .SH WARNING .IX Header "WARNING" You will need to use \f(CW\*(C`_\|_DATA_\|_\*(C'\fR sections and not \f(CW\*(C`_\|_END_\|_\*(C'\fR sections. Yes, it matters. Who knew! .SH EXPORTS .IX Header "EXPORTS" To get the methods exported by Data::Section, you must import like this: .PP .Vb 1 \& use Data::Section \-setup; .Ve .PP Optional arguments may be given to Data::Section like this: .PP .Vb 1 \& use Data::Section \-setup => { ... }; .Ve .PP Valid arguments are: .PP .Vb 2 \& encoding \- if given, gives the encoding needed to decode bytes in \& data sections; default; UTF\-8 \& \& the special value "bytes" will leave the bytes in the string \& verbatim \& \& inherit \- if true, allow packages to inherit the data of the packages \& from which they inherit; default: true \& \& header_re \- if given, changes the regex used to find section headers \& in the data section; it should leave the section name in $1 \& \& default_name \- if given, allows the first section to has no header and set \& its name .Ve .PP Three methods are exported by Data::Section: .SS section_data .IX Subsection "section_data" .Vb 1 \& my $string_ref = $pkg\->section_data($name); .Ve .PP This method returns a reference to a string containing the data from the name section, either in the invocant\*(Aqs \f(CW\*(C`DATA\*(C'\fR section or in that of one of its ancestors. (The ancestor must also derive from the class that imported Data::Section.) .PP By default, named sections are delimited by lines that look like this: .PP .Vb 1 \& _\|_[ name ]_\|_ .Ve .PP You can use as many underscores as you want, and the space around the name is optional. This pattern can be configured with the \f(CW\*(C`header_re\*(C'\fR option (see above). If present, a single leading \f(CW\*(C`\e\*(C'\fR is removed, so that sections can encode lines that look like section delimiters. .PP When a line containing only \f(CW\*(C`_\|_END_\|_\*(C'\fR is reached, all processing of sections ends. .SS section_data_names .IX Subsection "section_data_names" .Vb 1 \& my @names = $pkg\->section_data_names; .Ve .PP This returns a list of all the names that will be recognized by the \&\f(CW\*(C`section_data\*(C'\fR method. .SS merged_section_data .IX Subsection "merged_section_data" .Vb 1 \& my $data = $pkg\->merged_section_data; .Ve .PP This method returns a hashref containing all the data extracted from the package data for all the classes from which the invocant inherits \-\- as long as those classes also inherit from the package into which Data::Section was imported. .PP In other words, given this inheritance tree: .PP .Vb 5 \& A \& \e \& B C \& \e / \& D .Ve .PP \&...if Data::Section was imported by A, then when D\*(Aqs \f(CW\*(C`merged_section_data\*(C'\fR is invoked, C\*(Aqs data section will not be considered. (This prevents the read position of C\*(Aqs data handle from being altered unexpectedly.) .PP The keys in the returned hashref are the section names, and the values are \&\fBreferences to\fR the strings extracted from the data sections. .SS merged_section_data_names .IX Subsection "merged_section_data_names" .Vb 1 \& my @names = $pkg\->merged_section_data_names; .Ve .PP This returns a list of all the names that will be recognized by the \&\f(CW\*(C`merged_section_data\*(C'\fR method. .SS local_section_data .IX Subsection "local_section_data" .Vb 1 \& my $data = $pkg\->local_section_data; .Ve .PP This method returns a hashref containing all the data extracted from the package on which the method was invoked. If called on an object, it will operate on the package into which the object was blessed. .PP This method needs to be used carefully, because it\*(Aqs weird. It returns only the data for the package on which it was invoked. If the package on which it was invoked has no data sections, it returns an empty hashref. .SS local_section_data_names .IX Subsection "local_section_data_names" .Vb 1 \& my @names = $pkg\->local_section_data_names; .Ve .PP This returns a list of all the names that will be recognized by the \&\f(CW\*(C`local_section_data\*(C'\fR method. .SH "TIPS AND TRICKS" .IX Header "TIPS AND TRICKS" .SS "MooseX::Declare and namespace::autoclean" .IX Subsection "MooseX::Declare and namespace::autoclean" The namespace::autoclean library automatically cleans foreign routines from a class, including those imported by Data::Section. .PP MooseX::Declare does the same thing, and can also cause your \&\f(CW\*(C`_\|_DATA_\|_\*(C'\fR section to appear outside your class\*(Aqs package. .PP These are easy to address. The Sub::Exporter::ForMethods library provides an installer that will cause installed methods to appear to come from the class and avoid autocleaning. Using an explicit \f(CW\*(C`package\*(C'\fR statement will keep the data section in the correct package. .PP .Vb 1 \& package Foo; \& \& use MooseX::Declare; \& class Foo { \& \& # Utility to tell Sub::Exporter modules to export methods. \& use Sub::Exporter::ForMethods qw( method_installer ); \& \& # method_installer returns a sub. \& use Data::Section { installer => method_installer }, \-setup; \& \& method my_method { \& my $content_ref = $self\->section_data(\*(AqSectionA\*(Aq); \& \& print $$content_ref; \& } \& } \& \& _\|_DATA_\|_ \& _\|_[ SectionA ]_\|_ \& Hello, world. .Ve .SH "SEE ALSO" .IX Header "SEE ALSO"