DATA templates

38 views
Skip to first unread message

Stefan Adams

unread,
Feb 22, 2020, 10:12:21 AM2/22/20
to mojolicious
I just spent more time than I should admit about figuring this out.

Templates in the DATA section must come before the END section.

Is this common to all of Perl that DATA must come before END? Can anyone point me to a document resource for this, so that I can read it and beat it into my head? This is not the first time I've gotten tripped up on this.

Mike Lieman

unread,
Feb 22, 2020, 10:41:20 AM2/22/20
to mojol...@googlegroups.com
>The two control characters ^D and ^Z, and the tokens __END__ and __DATA__ may be used to indicate the logical end of the script before the actual end of file. Any following text is ignored.

https://perldoc.perl.org/perldata.html#Special-Literals

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mojolicious/CACyQ%2BFQZykiSrmiFxoEdRYB-KdOw%3DeDPTQ86zC%3DNu428EuOJEA%40mail.gmail.com.

Илья

unread,
Feb 22, 2020, 10:45:02 AM2/22/20
to mojol...@googlegroups.com

To be honest, perl documentation doesn't shed any light on case where __END__ and __DATA__ tokens coexists in one file. But I'm sure that it is not mojolicious problem, but perl behaviour in that case.

Dan Book

unread,
Feb 22, 2020, 11:56:46 AM2/22/20
to mojol...@googlegroups.com
It is not well defined. __END__ and __DATA__ even serve the same purpose within the main script file (but not in module files). I recommend not combining them.

-Dan

Stefan Adams

unread,
Feb 22, 2020, 11:59:31 AM2/22/20
to mojolicious
Thank you!  This link was very helpful and it provided a link to -- I think -- a solid description explaining this behavior.

but for other modules data after __END__ is not automatically retrievable

@mojocore: What do you think about adding this tip to one or both of these sections?

Mojo::Loader#data_section
Extract embedded file from the C<DATA> section of a class, all files will be
cached once they have been accessed for the first time. C<DATA> sections in
classes other than C<main> should be placed before C<END> sections.


Mojolicious::Guides##Renderer
All templates should be in the C<templates> directories of the application,
which can be customized with L<Mojolicious::Renderer/"paths">, or one of the
the C<DATA> sections from L<Mojolicious::Renderer/"classes">. C<DATA> sections
in classes other than C<main> should be placed before C<END> sections.


Here's a Perl test demonstrating this behavior:

$ cat end_data.t
package EndData;
use Test::More;

{
  local $.;
  my $class = __PACKAGE__;
  my $handle = do { no strict 'refs'; \*{"${class}::DATA"} };
  my $fileno = ok !fileno($handle);
  SKIP: {
    skip "DATA filehandle could not be opened", 2 if $fileno;
    ok seek($handle, 0, 0);
    like join('', <$handle>), qr(__END__.*?__DATA__);
  }
}

done_testing;

__END__
abc

__DATA__
def
$ cat data_end.t
package DataEnd;
use Test::More;

{
  local $.;
  my $class = __PACKAGE__;
  my $handle = do { no strict 'refs'; \*{"${class}::DATA"} };
  my $fileno = ok fileno($handle);
  SKIP: {
    skip "DATA filehandle could not be opened", 2 unless $fileno;
    ok seek($handle, 0, 0);
    like join('', <$handle>), qr(__END__.*?__DATA__);
  }
}

done_testing;

__DATA__
def

__END__
abc
$ prove -v end_data.t data_end.t 
end_data.t ..
ok 1
ok 2 # skip DATA filehandle could not be opened
ok 3 # skip DATA filehandle could not be opened

1..3
ok
data_end.t ..
ok 1
ok 2
ok 3
1..3
ok
All tests successful.
Files=2, Tests=6,  0 wallclock secs ( 0.01 usr  0.01 sys +  0.05 cusr  0.00 csys =  0.07 CPU)
Result: PASS


Stefan Adams

unread,
Feb 22, 2020, 12:03:15 PM2/22/20
to mojolicious
On Sat, Feb 22, 2020 at 10:56 AM Dan Book <gri...@gmail.com> wrote:
It is not well defined. __END__ and __DATA__ even serve the same purpose within the main script file (but not in module files). I recommend not combining them.

Interesting!  I did `mojo generate plugin` and it added perldoc after __END__ so I didn't think to remove it, but I wanted to add __DATA__.  What do you think about removing __END__ from the generator?

Dan Book

unread,
Feb 22, 2020, 12:05:24 PM2/22/20
to mojol...@googlegroups.com
I believe it is required in that configuration to allow POD after the DATA section. If you moved the POD before DATA (what I usually do) it wouldn't be needed.

-Dan

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.

Dan Book

unread,
Feb 22, 2020, 12:06:52 PM2/22/20
to mojol...@googlegroups.com
Actually the DATA section there is not part of the template so I believe it could be removed.

-Dan
Reply all
Reply to author
Forward
0 new messages