Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Calling functions in middle of CGI HTML declarations

by Your Mother (Bishop)
on Aug 26, 2012 at 16:52 UTC ( #989845=note: print w/replies, xml ) Need Help??

in reply to Calling functions in middle of CGI HTML declarations

Most (all?) block functions will take an array ref and do what you want–

use CGI::Pretty ":standard"; my @fields = qw( Adios Amigo ); print div ({ -id => "div-test" }, div( \@fields ) ); __END__ <div id="div-test"> <div> Adios </div> <div> Amigo </div> </div>

I use this the most in li constructions but it works fine with div, as you can see.

Replies are listed 'Best First'.
Re^2: Calling functions in middle of CGI HTML declarations
by Don Coyote (Pilgrim) on Aug 26, 2012 at 18:17 UTC

    The functions tend to take arguments on a per element basis. If the function is going to it will generally ask if it is recieveing an array or hash reference first then go on to process the code accordingly.

    The important thing is to provide the method with what it is expecting or is set up to handle. Here it is important to determine to the method it is being provided with an array reference rather than an expanded list. As it will determine whether the list is going to be concatenated and used as content as in the case where the array actual is used and ends up as one chunk in the div, or split into the lists components and then used as content, where the ref is used and creates the individual divs.

    The square brackets provide the method with an anonymous array built from the list provided in the array, whereas the reference to the array provides, a reference to the array. Providing the reference being the more efficient way to move the data through.

    I think that building an anonymous array out of a named array (what I did by adding square brackets)is probably more expensive than needs be, but was a workaround to get the desired result.

    Basically if you provide the array as a reference, the method does the foreach for you. But you have to inspect each method via the docs to determine what each will or wont accept. There can be differnces in output depending on which markup or version you are using and such.

    To note, while hacking through this I noticed that closed div beginning elements were produced which is some effect of the code being processed for xhtml and not html, hence the start_div/end_div in the foreach statement to explicitly inform cgi what I wanted. This also prevented the divs being built recursively within themselves without any closing elements.

      Did you silently update your original reply or did I just completely miss the parts that are similar to mine? If I did miss it, sorry about that.

        YM, no, no silent update. I kind of felt like mentioning that using the backslash ref notation as in your example is most likely a better way to use a ref overall. Though without proper tests I cannot really tell as I do not know.

        I replied also to make the point the method is looking for a ref to produce the op requirements, whether a ref to an anonymous array or an actual array ref. And is also why I replied to your thread rather than again to the ot

        hope this clears up what I was blabbering on about

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://989845]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2018-04-20 11:10 GMT
Find Nodes?
    Voting Booth?