in reply to Re: perl if statement
in thread perl if statement

Note that you can also simply place your template code into the DATA file (which begins with the token "__END__" or "__DATA__" at the end of your script), loading it with the filehandle parameter to HTML::Template::new() instead of the filename parameter. I like to use this technique for very simple scripts/short templates, as it keeps your HTML in the same physical file but separate from your Perl program:

#!/usr/bin/perl use strict; use warnings; use HTML::Template; my $tmpl = HTML::Template->new( filehandle => \*DATA ); my $user = 'Mr. Foo'; if (1) { $tmpl->param( message => "Hello $user, your account - ready" ); print $tmpl->output(); exit 0; } else { $tmpl->param( message => "Your user/pin incorrect - Use correct in +put" ); print $tmpl->output(); } __END__ Content-type:text/html <html> <head> <title>Account/title> </head> <body> <h2><TMPL_VAR message /></h2> </body> </html>

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^3: perl if statement
by GrandFather (Saint) on Mar 01, 2016 at 19:55 UTC

    Sure, Perl provides lots of ways of providing inline data that are better than the string of print statements used by the OP. I use DATA a lot for one off scripts that filter data I've copied and pasted from somewhere for example (Clipboard saves the need for paste and DATA though). If I need a fragment of HTML inlined then a HEREDOC is a really good choice because it makes the HTML clear and puts it right where it's used.

    However, the big win with using a template system (there are may btw - HTML::Template is a really good starting point) is being able to spin off the HTML into something that is very close to a static page file that a page designer can work on without having to have any understanding of the business code that uses it. Encouraging this usage pattern is why I went to the trouble of creating a test file in the example.

    The problem with the DATA section is that it doesn't scale. There is only one of them. Inline data seems rather like peanuts, it's hard to stop at one!

    Premature optimization is the root of all job security