Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Trying to create an XML file that <a href="http://search.cpan.org/~grantm/XML-Simple-2.18/lib/XML/Simple.pm" rel="nofollow noreferrer">XML::Simple</a> will parse into a specific data structure can be a real pain. I find the easiest way to handle this is to start with the data structure you want, run it through <a href="http://search.cpan.org/~grantm/XML-Simple-2.18/lib/XML/Simple.pm#XMLout()" rel="nofollow noreferrer">XMLout</a>, then modify the resulting XML as you see fit.</p> <pre><code>use strict; use warnings; use XML::Simple; my $config = { 'indicator' =&gt; 'edit', 'elements' =&gt; [ { 'name' =&gt; 'overrides', 'label' =&gt; 'Over Rides', 'type' =&gt; 'Fieldset', 'attributes' =&gt; { 'title' =&gt; 'Use these fields to override the csv value with this constant value', }, 'elements' =&gt; [ { 'type' =&gt; 'text', 'name' =&gt; 'client', 'label' =&gt; 'Client', }, { 'type' =&gt; 'Select', 'name' =&gt; 'bidy_type', 'label' =&gt; 'Bid Type', 'options' =&gt; [ [ 'bid' =&gt; 'Bid' ], [ 'approved' =&gt; 'Approved' ], ], }, { 'type' =&gt; 'text', 'name' =&gt; 'client_pay', 'label' =&gt; 'Client Pay', }, { 'type' =&gt; 'text', 'name' =&gt; 'due_date', 'label' =&gt; 'Due Date', }, { 'type' =&gt; 'text', 'name' =&gt; 'start_date', 'label' =&gt; 'Start Date', }, { 'type' =&gt; 'Radiogroup', 'name' =&gt; 'category', 'label' =&gt; 'Category', 'options' =&gt; [ [ 'grass_cut_initial' =&gt; 'Grass Cut - Initial' ], [ 'grass_cut_recut' =&gt; 'Grass Cut - Recut' ], [ 'secure' =&gt; 'Secure' ], [ 'winterization' =&gt; 'Winterization' ], [ 'rehab' =&gt; 'Rehab' ], [ 'custom' =&gt; 'Custom' ], ], }, { 'type' =&gt; 'text', 'name' =&gt; 'contractor', 'label' =&gt; 'Contractor', }, { 'type' =&gt; 'text', 'name' =&gt; 'contractor_pay', 'label' =&gt; 'Contractor Pay', }, ], }, ], }; my $xml = XMLout($config, 'KeyAttr' =&gt; []); print "$xml\n"; </code></pre> <p><strong>Result</strong></p> <pre><code>&lt;opt indicator="edit"&gt; &lt;elements label="Over Rides" name="overrides" type="Fieldset"&gt; &lt;attributes title="Use these fields to override the csv value with this constant value" /&gt; &lt;elements label="Client" name="client" type="text" /&gt; &lt;elements label="Bid Type" name="bidy_type" type="Select"&gt; &lt;options&gt; &lt;anon&gt;bid&lt;/anon&gt; &lt;anon&gt;Bid&lt;/anon&gt; &lt;/options&gt; &lt;options&gt; &lt;anon&gt;approved&lt;/anon&gt; &lt;anon&gt;Approved&lt;/anon&gt; &lt;/options&gt; &lt;/elements&gt; &lt;elements label="Client Pay" name="client_pay" type="text" /&gt; &lt;elements label="Due Date" name="due_date" type="text" /&gt; &lt;elements label="Start Date" name="start_date" type="text" /&gt; &lt;elements label="Category" name="category" type="Radiogroup"&gt; &lt;options&gt; &lt;anon&gt;grass_cut_initial&lt;/anon&gt; &lt;anon&gt;Grass Cut - Initial&lt;/anon&gt; &lt;/options&gt; &lt;options&gt; &lt;anon&gt;grass_cut_recut&lt;/anon&gt; &lt;anon&gt;Grass Cut - Recut&lt;/anon&gt; &lt;/options&gt; &lt;options&gt; &lt;anon&gt;secure&lt;/anon&gt; &lt;anon&gt;Secure&lt;/anon&gt; &lt;/options&gt; &lt;options&gt; &lt;anon&gt;winterization&lt;/anon&gt; &lt;anon&gt;Winterization&lt;/anon&gt; &lt;/options&gt; &lt;options&gt; &lt;anon&gt;rehab&lt;/anon&gt; &lt;anon&gt;Rehab&lt;/anon&gt; &lt;/options&gt; &lt;options&gt; &lt;anon&gt;custom&lt;/anon&gt; &lt;anon&gt;Custom&lt;/anon&gt; &lt;/options&gt; &lt;/elements&gt; &lt;elements label="Contractor" name="contractor" type="text" /&gt; &lt;elements label="Contractor Pay" name="contractor_pay" type="text" /&gt; &lt;/elements&gt; &lt;/opt&gt; </code></pre> <p>Not exactly the XML you would expect, but it gets the job done. You can also double check that this works by running it back through <a href="http://search.cpan.org/~grantm/XML-Simple-2.18/lib/XML/Simple.pm#XMLin()" rel="nofollow noreferrer">XMLin</a> and examining the resulting data structure:</p> <pre><code>use strict; use warnings; use XML::Simple; use Data::Dumper; my $xml = '...'; my $config = XMLin($xml, 'KeyAttr' =&gt; []); print Dumper($config); </code></pre> <p>The reason I use the <code>KeyAttr</code> option is because of this <a href="http://search.cpan.org/~grantm/XML-Simple-2.18/lib/XML/Simple.pm#Caveats" rel="nofollow noreferrer">caveat</a>:</p> <blockquote> <p>If you wish to 'round-trip' arbitrary data structures from Perl to XML and back to Perl, then you should probably disable array folding (using the KeyAttr option) both with XMLout() and with XMLin().</p> </blockquote> <p>Also, I can't seem to find a way to pass options to <a href="http://search.cpan.org/~bricas/Config-Any-0.18/lib/Config/Any.pm" rel="nofollow noreferrer">Config::Any</a> through <a href="http://search.cpan.org/~cfranks/HTML-FormFu-0.06000/lib/HTML/FormFu.pm#load_config_file" rel="nofollow noreferrer">load_config_file</a> (I haven't spent much time hunting through the docs for <a href="http://search.cpan.org/~cfranks/HTML-FormFu-0.06000/lib/HTML/FormFu.pm" rel="nofollow noreferrer">HTML::FormFu</a> though). This means you might have to use <a href="http://search.cpan.org/~grantm/XML-Simple-2.18/lib/XML/Simple.pm" rel="nofollow noreferrer">XML::Simple</a> yourself to get the data structure to pass to <a href="http://search.cpan.org/~cfranks/HTML-FormFu-0.06000/lib/HTML/FormFu.pm#populate" rel="nofollow noreferrer">populate</a>.</p> <hr> <p>As you can see, an XML config file really isn't the easiest approach when working with HTML::FormFu. If you're open to other approaches, I would suggest using something that has a much better mapping to Perl data structures, like YAML (probably one of the reasons why it's used in the <a href="http://search.cpan.org/~cfranks/HTML-FormFu-0.06000/lib/HTML/FormFu/Manual/Cookbook.pod#YAML" rel="nofollow noreferrer">documentation examples</a>). Personally, I just use Perl to create my forms and stick the code/config in a module.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload