bigup401 has asked for the wisdom of the Perl Monks concerning the following question:

i want to output

<li> <div class=inforno /> <img src='' </div> </li>

my script

use CGI::Pretty ":standard"; print li ( div({"class=class=inforno"}), div("<img <img src=''"), );

Replies are listed 'Best First'.
Re: div in cgi
by marto (Cardinal) on Sep 13, 2018 at 14:56 UTC

    Continuing to make life difficult for yourself I see. Your script prints what you tell it:

    CGI::Pretty is DEPRECATED and will be removed in a future release. Ple +ase see https://github.com/leejo/CGI.pm/issues/162 for more informati +on at /home/marto/perl5/perlbrew/perls/perl-5.26.1/lib/site_perl/5.26 +.1/CGI/Pretty.pm line 21. <li><div class=class=inforno /> <div><img <img src=''</div></li>

    You ask it to print invalid HTML, which it does.

    use CGI::Pretty ":standard"; print li ( div({"class=inforno"}, "<img src=''>") );

    Produces:

    CGI::Pretty is DEPRECATED and will be removed in a future release. Ple +ase see https://github.com/leejo/CGI.pm/issues/162 for more informati +on at /home/marto/perl5/perlbrew/perls/perl-5.26.1/lib/site_perl/5.26 +.1/CGI/Pretty.pm line 21. <li><div class=inforno><img src=''></div></li>

    You seem to be ignoring good advice, choosing to use modules which are deprecated, even when this module was supported it was ~10x slower than just using CGI, and recommended for debugging purposes only.

      I don't understand the wording "DEPRECATED" here.

      The fact that it will be removed from CORE doesn't mean it will cease to exist in CPAN, right?

      Mind you large parts of this monastery is using such functions from CGI, a hypothetical update to a newer Perl would be impossible without such a module for backwards compatibility.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        In so much that it no longer does what it's supposed to, notice that the output isn't 'pretty', from the docs:

        'It will be removed from the CGI distribution in a future release, so you should no longer use it and remove it from any code that currently uses it.

        For now it has been reduced to a shell to prevent your code breaking, but the "pretty" functions will no longer output "pretty" HTML.'

        It then continues to give examples of better ways of doing this. Regardless OP has already had templating systems and other frameworks suggested to them.

      thanks

Re: div in cgi
by davido (Cardinal) on Sep 13, 2018 at 16:35 UTC

    Using CGI.pm's helpers to generate your output is nothing short of unwieldy. Sure, it works, and it's Perl, and you end up with mostly well-formed HTML, but it's just a lot of work. Use Template::Toolkit instead. There's a little learning curve at first, but then your life becomes easier once you catch on to it.

    The code (mycgi.pl):

    #!/usr/bin/env perl use strict; use warnings; use CGI; use Template; use FindBin qw($Bin); use constant TEMPLATE_PATH => "$Bin/"; (my $template = __FILE__) =~ s/\.pl$/.tt/; # Derive the template from +the script name. my $data = {list => [qw(foo bar baz)]}; render($template, $data); sub render { my ($temp, $data) = @_; my $t = Template->new({INCLUDE_PATH => TEMPLATE_PATH}) || die "$Template::ERROR\n"; $t->process($temp, $data) || die $t->error, "\n"; }

    The template (mycgi.tt):

    <!DOCTYPE html> <html> <head> <title>Hello world</title> </head> <body> <ul> [%- FOR string IN list %] <li> <div class inforno /> ----[% string %]---- </div> </li> [%- END %] </ul> </body> </html>

    The output:

    <!DOCTYPE html> <html> <head> <title>Hello world</title> </head> <body> <ul> <li> <div class inforno /> ----foo---- </div> </li> <li> <div class inforno /> ----bar---- </div> </li> <li> <div class inforno /> ----baz---- </div> </li> </ul> </body> </html>

    I see that you've already received recommendations for Template::Toolkit and Mojo::Template.

    Here is the same code adapted for Mojo::Template:

    use strict; use warnings; use CGI; use Mojo::Template; use FindBin qw($Bin); use constant TEMPLATE_PATH => "$Bin/"; (my $template = __FILE__) =~ s/\.pl$/.tt/; # Derive the template from +the script name my $data = {list => [qw(foo bar baz)]}; render($template, $data); sub render { my ($temp, $data) = @_; print Mojo::Template ->new(vars => 1) ->render_file(TEMPLATE_PATH . $temp, $data); }

    And the Mojo::Template based template:

    <!DOCTYPE html> <html> <head> <title>Hello world</title> </head> <body> <ul> % for my $string (@$list) { <li> <div class inforno /> ----<%= $string %>---- </div> </li> % } </ul> </body> </html>

    The output remains the same. I find Mojo::Template to be pretty simple to use since its tags are based on Perl rather than on a DSL that takes time to learn.

    By contrast, let's look at the code to generate similar (but with worse formatting) output using only CGI.pm's helpers.

    #!/usr/bin/env perl use strict; use warnings; use CGI qw(:standard); my $data = {list => [qw(foo bar baz)]}; print start_html(-title => "Hello world"); print ul( map{ li( div({class => 'inforno'}, "----$_----") ) . "\n" } @{$data->{'list'}} ); print end_html();

    I find that harder to skim through and reason about. We all should be looking for ways to simplify our code in ways that make it easier to reason about, regardless of our level of proficiency.


    Dave

Re: div in cgi
by hippo (Archbishop) on Sep 13, 2018 at 14:56 UTC
    print <<EOT; <li> <div class=inforno /> <img src='' </div> </li> EOT

    That's some seriously broken HTML you'll be printing, mind.

Re: div in cgi
by cavac (Prior) on Sep 13, 2018 at 16:24 UTC

    Other people have given you already the correct answer. But just as a side node, to your really, REALLY want to generate the HTML code from within your script? At least in my experience (12+ years of writing webserver stuff in Perl), splitting code and HTML into different files is much more maintainable. You really should look into using something like Template Toolkit.

    "For me, programming in Perl is like my cooking. The result may not always taste nice, but it's quick, painless and it get's food on the table."
Re: div in cgi
by Anonymous Monk on Sep 13, 2018 at 16:30 UTC
    #!/usr/bin/perl # DISABLE LEE JOHNSON'S CRIPPLEWARE BEGIN { $SIG{__WARN__} = sub {} } use strict; use warnings; # IGNORE MARTO'S OBSESSIVE COMPULSION use CGI::Pretty ":standard"; # PRINT OUTPUT DESIRED BY THE SEEKER print li("\n\t", div({"class=inforno"},"\n\t\t<img src=''\n\t"),"\n" ),"\n"; __END__ https://www.perlmonks.org/?node_id=1222286 div in cgi. i want to output: <li> <div class=inforno> <img src='' </div> </li>
      Using CGI::Pretty when you don't need to (should have just used CGI, since the Pretty part does nothing), generating broken HTML. Your mid 90s code style and crappy attitude are don't have a place in the world post 1996.
        > Using CGI::Pretty when you don't need to

        The OP asked about CGI::Pretty, silly.

        > (should have just used CGI, since the Pretty part does nothing),

        It's not my fault the new dictator of CGI::Pretty is too incompetent to maintain it and so hostile as to deliberately break it and spew obnoxious error messages instead of well-formatted HTML!

        > generating broken HTML.

        It's not broken until a browser tries to render it. You make too many false assumptions. The OP asked for incomplete HTML and may have a good reason.

        > Your mid 90s code style

        What are you talking about?

        > and crappy attitude are don't have a place in the world post 1996.

        Who put you in charge of deciding who has a place in the world? Mr. President? You pushed the wrong button, again!