Another way would be to place the array as an argument to the div method directly. You can get different results from slightly different constructs. Please note I am using the object orientated syntax. I have updated this reply with the syntax for the method orientated syntax. See end of post.

Placing the array directly as an argument will result in the list being expanded as content.

print $q->div( { -id => "div-test" }, $q->div(@fields), ); <body> <div id="div-test"> <div>Hello Goodbye Friend Adios Amigo</div> </div> </body>

To reproduce a singular div for every item in the list then let the method know you are supplying a list by placing square brackets around the array.

print $q->div( { -id => "div-test" }, $q->div([@fields]), ); <body> <div id="div-test"> <div>Hello</div> <div>Goodbye</div> <div>Friend</div> <div>Adios</div> <div>Amigo</div> </div><!--end div-test div --> </body>

Using a foreach loop may become necessary if you perhaps wanted the id of each div to be entitled as per each item of the array. But while in the first two instances you would be placing the array into the content compartment of the argument to div, this would be a standalone statement for cgi to process. That is, placed outside of any arguments to div, if you wanted to you could write a quick div method to wrap any arrays of divs.

print $q->div( { -id => "div-test" }, $q->div([@fields]), ); print ( $q->start_div({-id=>$_}), $q->end_div ) foreach @fields; <body> <div id="div-test"> <div>Hello</div> <div>Goodbye</div> <div>Friend</div> <div>Adios</div> <div>Amigo</div> </div><!--end div-test div --> <div id="Hello"></div> <div id="Goodbye"></div> <div id="Friend"></div> <div id="Adios"></div> <div id="Amigo"></div> </body>

Have a play around with those and you should be able to get what you want.

Update UTC 19:55 27 08 2012

I have revisited my reply to adjust for using the method calling approach. Most of the previous works by simply removing the $q calls. However to enable the final foreach statement to work you will also need to import the div element methods as well as :standard.

use CGI qw/:standard *div/;

This allows you to use start_div and end_div methods. The glob in front of element allows you to call any start_element, end_element pair from your script. I have also included the reference syntax as shown by Your Mother in reply

#! /usr/bin/perl use strict; use warnings; use CGI qw /:standard *div/; use CGI::Pretty; my $q = CGI->new(); my @fields = qw(Hello World); print header(), start_html(), div( @fields ), div( [@fields] ), div( { -id => "div-test" }, div(\@fields) ); print ( start_div({-id=>$_}), end_div() ) foreach @fields; print end_html(); exit 0;

Prints

Content-Type: text/html; charset=ISO-8859-1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-U +S"> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> </head> <body> <div> Hello World </div> <div> Hello </div> <div> World </div> <div id="div-test"> <div> Hello </div> <div> World </div> </div> <div id="Hello"> </div> <div id="World"> </div> </body> </html>

Coyote


In reply to Re: Calling functions in middle of CGI HTML declarations by Don Coyote
in thread Calling functions in middle of CGI HTML declarations by dr.jekyllandme

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.