Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit

  • Loading branch information...
commit d17b798611483bdb4f9ea19dd488b0dd4ebba6d6 0 parents
@miyagawa authored
5 .gitignore
@@ -0,0 +1,5 @@
+META.yml
+Makefile
+inc/
+pm_to_blib
+*~
2  .shipit
@@ -0,0 +1,2 @@
+steps = FindVersion, ChangeVersion, CheckChangeLog, DistTest, Commit, Tag, MakeDist, UploadCPAN
+git.push_to = origin
4 Changes
@@ -0,0 +1,4 @@
+Revision history for Perl extension Data::Section::Simple
+
+0.01 Thu Mar 18 01:06:44 2010
+ - original version
22 MANIFEST
@@ -0,0 +1,22 @@
+.gitignore
+Changes
+inc/Module/Install.pm
+inc/Module/Install/Base.pm
+inc/Module/Install/Can.pm
+inc/Module/Install/Fetch.pm
+inc/Module/Install/Makefile.pm
+inc/Module/Install/Metadata.pm
+inc/Module/Install/ReadmeFromPod.pm
+inc/Module/Install/Repository.pm
+inc/Module/Install/Win32.pm
+inc/Module/Install/WriteAll.pm
+lib/Data/Section/Simple.pm
+Makefile.PL
+MANIFEST This list of files
+META.yml
+README
+t/00_compile.t
+t/basic.t
+t/Foo.pm
+t/pkg_oo.t
+xt/pod.t
14 MANIFEST.SKIP
@@ -0,0 +1,14 @@
+\bRCS\b
+\bCVS\b
+\.svn/
+\.git/
+^MANIFEST\.
+^Makefile$
+~$
+\.old$
+^blib/
+^pm_to_blib
+^MakeMaker-\d
+\.gz$
+\.cvsignore
+\.shipit
7 Makefile.PL
@@ -0,0 +1,7 @@
+use inc::Module::Install;
+all_from 'lib/Data/Section/Simple.pm';
+readme_from('lib/Data/Section/Simple.pm');
+build_requires 'Test::More', 0.88;
+test_requires 'Test::Requires';
+auto_set_repository();
+WriteAll;
50 README
@@ -0,0 +1,50 @@
+NAME
+ Data::Section::Simple - Read data from __DATA__
+
+SYNOPSIS
+ use Data::Section::Simple qw(get_data_section);
+
+ # Functional interface -- reads from caller package __DATA__
+ my $all = get_data_section; # All data in hash reference
+ my $foo = get_data_section('foo.html');
+
+ # OO - allows reading from other packages
+ my $reader = Data::Section::Simple->new($package);
+ my $all = $reader->get_data_section;
+
+ __DATA__
+
+ @@ foo.html
+ <html>
+ <body>Hello</body>
+ </html>
+
+ @@ bar.tt
+ [% IF true %]
+ Foo
+ [% END %]
+
+DESCRIPTION
+ Data::Section::Simple is a simple module to extract data drom "__DATA__"
+ section of the file.
+
+ As the name suggests, it's a very simpler version of what Data::Section
+ does. If you want more functionalities such as merging data section, use
+ Data::Section instead.
+
+AUTHOR
+ Tatsuhiko Miyagawa <miyagawa@bulknews.net>
+
+COPYRIGHT
+ Copyright 2010- Tatsuhiko Miyagawa
+
+ The code to read DATA section is based on Mojo::Command get_all_data:
+ Copyright 2008-2010 Sebastian Riedel
+
+LICENSE
+ This library is free software; you can redistribute it and/or modify it
+ under the same terms as Perl itself.
+
+SEE ALSO
+ Data::Section Inline::Files
+
106 lib/Data/Section/Simple.pm
@@ -0,0 +1,106 @@
+package Data::Section::Simple;
+
+use strict;
+use 5.008_001;
+our $VERSION = '0.01';
+
+use base qw(Exporter);
+our @EXPORT_OK = qw(get_data_section);
+
+sub new {
+ my($class, $pkg) = @_;
+ bless { package => $pkg || caller }, $class;
+}
+
+sub get_data_section {
+ my $self = ref $_[0] ? shift : __PACKAGE__->new(scalar caller);
+
+ if (@_) {
+ return $self->get_data_section->{$_[0]};
+ } else {
+ my $d = do { no strict 'refs'; \*{$self->{package}."::DATA"} };
+ return unless fileno $d;
+
+ seek $d, 0, 0;
+ my $content = join '', <$d>;
+ $content =~ s/^.*\n__DATA__\n/\n/s; # for win32
+ $content =~ s/\n__END__\n.*$/\n/s;
+
+ my @data = split /^@@\s+(.+?)\s*\r?\n/m, $content;
+ shift @data; # trailing whitespaces
+
+ my $all = {};
+ while (@data) {
+ my ($name, $content) = splice @data, 0, 2;
+ $all->{$name} = $content;
+ }
+
+ return $all;
+ }
+}
+
+1;
+__END__
+
+=encoding utf-8
+
+=for stopwords
+
+=head1 NAME
+
+Data::Section::Simple - Read data from __DATA__
+
+=head1 SYNOPSIS
+
+ use Data::Section::Simple qw(get_data_section);
+
+ # Functional interface -- reads from caller package __DATA__
+ my $all = get_data_section; # All data in hash reference
+ my $foo = get_data_section('foo.html');
+
+ # OO - allows reading from other packages
+ my $reader = Data::Section::Simple->new($package);
+ my $all = $reader->get_data_section;
+
+ __DATA__
+
+ @@ foo.html
+ <html>
+ <body>Hello</body>
+ </html>
+
+ @@ bar.tt
+ [% IF true %]
+ Foo
+ [% END %]
+
+=head1 DESCRIPTION
+
+Data::Section::Simple is a simple module to extract data drom
+C<__DATA__> section of the file.
+
+As the name suggests, it's a very simpler version of what
+L<Data::Section> does. If you want more functionalities such as
+merging data section, use L<Data::Section> instead.
+
+=head1 AUTHOR
+
+Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
+
+=head1 COPYRIGHT
+
+Copyright 2010- Tatsuhiko Miyagawa
+
+The code to read DATA section is based on Mojo::Command get_all_data:
+Copyright 2008-2010 Sebastian Riedel
+
+=head1 LICENSE
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=head1 SEE ALSO
+
+L<Data::Section> L<Inline::Files>
+
+=cut
4 t/00_compile.t
@@ -0,0 +1,4 @@
+use strict;
+use Test::More tests => 1;
+
+BEGIN { use_ok 'Data::Section::Simple' }
23 t/Foo.pm
@@ -0,0 +1,23 @@
+package Foo;
+
+1;
+
+__DATA__
+
+@@ foo.html
+<html>
+<body>Foo</body>
+</html>
+
+@@ bar.tt
+[% IF foo %]
+bar
+[% END %]
+
+__END__
+
+=head1 NAME
+
+Foo
+
+=cut
42 t/basic.t
@@ -0,0 +1,42 @@
+use strict;
+use Data::Section::Simple qw(get_data_section);
+use Test::More;
+
+my $x = get_data_section();
+is_deeply [ sort keys %$x ], [ qw(bar.tt foo.html) ];
+
+is get_data_section('foo.html'), <<HTML;
+<html>
+<body>Foo</body>
+</html>
+
+HTML
+
+is get_data_section('bar.tt'), <<TT;
+[% IF foo %]
+bar
+[% END %]
+
+TT
+
+done_testing;
+
+__DATA__
+
+@@ foo.html
+<html>
+<body>Foo</body>
+</html>
+
+@@ bar.tt
+[% IF foo %]
+bar
+[% END %]
+
+__END__
+
+=head1 NAME
+
+basic.t
+
+=cut
28 t/pkg_oo.t
@@ -0,0 +1,28 @@
+use strict;
+use Data::Section::Simple;
+
+use lib "t";
+use Foo;
+
+use Test::More;
+
+my $d = Data::Section::Simple->new('Foo');
+my $x = $d->get_data_section();
+is_deeply [ sort keys %$x ], [ qw(bar.tt foo.html) ];
+
+is $d->get_data_section('foo.html'), <<HTML;
+<html>
+<body>Foo</body>
+</html>
+
+HTML
+
+is $d->get_data_section('bar.tt'), <<TT;
+[% IF foo %]
+bar
+[% END %]
+
+TT
+
+done_testing;
+
4 xt/pod.t
@@ -0,0 +1,4 @@
+use Test::More;
+eval "use Test::Pod 1.00";
+plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;
+all_pod_files_ok();
Please sign in to comment.
Something went wrong with that request. Please try again.