in reply to perl if statement

Quite aside from the if issue, that code smells bad. There are many techniques that will will clean it up, but the end point is to move the HTML stuff out of the source altogether into a template. The following code generates the template file as a convenience for testing (just run the script). Usually the "create file" stuff isn't required!

#!/usr/bin/perl use strict; use warnings; use HTML::Template; # Create a test template file my $tmplFileName = "template.htmlt"; open my $tmplOut, '>', $tmplFileName or die "Can't create $tmplFileNam +e: $!\n"; print $tmplOut <<TMPL; Content-type:text/html <html> <head> <title>Account/title> </head> <body> <h2><TMPL_VAR message /></h2> </body> </html> TMPL close $tmplOut; # "production" code my $tmpl = HTML::Template->new(filename => $tmplFileName); 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(); }

Prints:

Content-type:text/html <html> <head> <title>Account/title> </head> <body> <h2>Hello Mr. Foo, your account - ready</h2> </body> </html>

Note too that the test file content is generated using a HEREDOC to inline the file contents. If you really need to include a fragment of HTML in your source code that's a much nicer way to do it than multiple print statements!

Also note the use of strictures (use strict; use warnings;) and the open statement using lexical file handle and result testing.

Premature optimization is the root of all job security

Replies are listed 'Best First'.
Re^2: perl if statement
by 1nickt (Canon) on Mar 01, 2016 at 14:20 UTC

    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.

      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