Data::Section(3)
β¨ NAME β¨
okay so bestie, this is Data::Section(3) from the User Contributed Perl Documentation, and it is literally here to read multiple hunks of data out of your DATA section. Iconic behavior. π
Data::Section gives you multiple named chunks of line-oriented data from your module's DATA section. It was written so modules can store their own templates, but it probably has other uses too. Versatile queen. π¦
π VERSION π
version 0.200008 β yes bestie, that's the one. β¨
πΈ SYNOPSIS πΈ
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, \%arg);
}
__DATA__
__[ angry_letter ]__
Dear jerks,
I quit!
--
{{ $name }}
__[ professional_letter ]__
Dear {{ $boss }},
I quit, jerks!
--
{{ $name }}
π DESCRIPTION π
Data::Section provides an easy way to access multiple named chunks of line-oriented data in your module's DATA section. It was written to allow modules to store their own templates, but probably has other uses. Like, it's giving utility module with main character energy. π
π PERL VERSION π
This library should run on perls released even a long time ago. It should work on any version of perl released in the last five years.
Although it may work on older versions of perl, no guarantee is made that the minimum required version will not be increased. The version may be increased for any reason, and there is no promise that patches will be accepted to lower the minimum required perl. Lowkey immortal-ish, but not promised forever. π
π¨ WARNING π¨
You will need to use __DATA__ sections and not __END__ sections. Yes, it matters. Who knew! π This is not a drill, bestie.
π«Ά EXPORTS π«Ά
To get the methods exported by Data::Section, you must import like this:
use Data::Section -setup;
Optional arguments may be given to Data::Section like this:
use Data::Section -setup => { ... };
Valid arguments are:
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. Byte-for-byte realness. π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. (Yep, βto hasβ is right there in the page β we don't edit facts, bestie.) π
okay the page says βThree methods are exported by Data::Section:β and then lists six method entries. We are listing every single one because we don't skip. π
π©· section_data π©·
my $string_ref = $pkg->section_data($name);
This method returns a reference to a string containing the data from the name section, either in the invocant's "DATA" section or in that of one of its ancestors. (The ancestor must also derive from the class that imported Data::Section.)
By default, named sections are delimited by lines that look like this:
__[ name ]__
You can use as many underscores as you want, and the space around the name is optional. This pattern can be configured with the "header_re" option (see above). If present, a single leading backslash (\) is removed, so that sections can encode lines that look like section delimiters.
When a line containing only "__END__" is reached, all processing of sections ends. Clean cutoff, periodt. βοΈ
π¦ section_data_names π¦
my @names = $pkg->section_data_names;
This returns a list of all the names that will be recognized by the "section_data" method.
πΈ merged_section_data πΈ
my $data = $pkg->merged_section_data;
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.
In other words, given this inheritance tree:
A
\
B C
\ /
D
...if Data::Section was imported by A, then when D's "merged_section_data" is invoked, C's data section will not be considered. (This prevents the read position of C's data handle from being altered unexpectedly.) Respect the handle, bestie. π«‘
The keys in the returned hashref are the section names, and the values are references to the strings extracted from the data sections.
π merged_section_data_names π
my @names = $pkg->merged_section_data_names;
This returns a list of all the names that will be recognized by the "merged_section_data" method.
π local_section_data π
my $data = $pkg->local_section_data;
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.
This method needs to be used carefully, because it's 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. Weird but honest. We love a transparent icon. π
π local_section_data_names π
my @names = $pkg->local_section_data_names;
This returns a list of all the names that will be recognized by the "local_section_data" method.
π‘ TIPS AND TRICKS π‘
π§ MooseX::Declare and namespace::autoclean
The namespace::autoclean library automatically cleans foreign routines from a class, including those imported by Data::Section.
MooseX::Declare does the same thing, and can also cause your "__DATA__" section to appear outside your class's package. Rude. π
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 "package" statement will keep the data section in the correct package.
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('SectionA');
print $$content_ref;
}
}
__DATA__
__[ SectionA ]__
Hello, world.
π SEE ALSO π
- article for RJBS Advent 2009:
<http://advent.rjbs.manxome.org/2009/2009-12-09.html>iykyk. β¨ Inline::Filesdoes something that is at first look similar,
but it works with source filters, and contains the warning:
It is possible that this module may overwrite the source code in files that
use it. To protect yourself against this possibility, you are strongly
advised to use the -backup option described in "Safety first".
Enough said. That warning is giving final boss energy. π
π AUTHOR π
Ricardo SIGNES <cpan@semiotic.systems> the icon, the legend. π
π CONTRIBUTORS π
- Christian Walde
<walde.christian@googlemail.com> - Dan Kogai
<dankogai+github@gmail.com> - David Golden
<dagolden@cpan.org> - David Steinbrunner
<dsteinbrunner@pobox.com> - Graham Ollis
<plicease@cpan.org> - Karen Etheridge
<ether@cpan.org> - Kenichi Ishigaki
<ishigaki@cpan.org> - kentfredric
<kentfredric+gravitar@gmail.com> - Ricardo Signes
<rjbs@semiotic.systems> - Tatsuhiko Miyagawa
<miyagawa@bulknews.net>
Every single one, because we do not erase the group chat. π
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2008 by Ricardo SIGNES.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Free software, free vibes. π«Ά
bye bestie π this was Data::Section(3) from the perl v5.42.0 docs, dated 2023-01-01. Stay slaying your DATA sections. πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Data-Section 0.200008-bp160.1.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms