Re: Two questions about XML::Generator

Re: Two questions about XML::Generator

am 18.12.2007 01:51:44 von Ben Morrow

Quoth nospam :
> Please help with the two questions below regarding XML::Generator. Each
> question has an example:

I'm sure we've had this question, or one very like it, not so long
ago...


> # 1. Is it possible to insert conditional logic similar to the
> # $var eq 'mystr', which would only output TAG2 if true?
>
>
> print $xml->TAG1(
> $xml->TAG1A("AAA"),
> $xml->TAG1B("BBB"),
>
>
> ($var eq 'mystring' and
>
> $xml->TAG2(
> $xml->TAG2A("CCC"),
> $xml->TAG2B("DDD"),
> )

One way would be

(
$var eq 'mystring'
? $xml->TAG2(...)
: ()
),

which inserts an empty list when the condition is false. It's slightly
annoying 'and' doesn't behave like this.

> # 2. Is it possible to loop iterate over TAG2 using a foreach loop?
>

> (foreach (@array) {
>
> $xml->TAG2(
> $xml->TAG2A("CCC"),
> $xml->TAG2B("DDD"),
> }
>
> ),

No, this is what map is for.

map {
$xml->TAG2($_)
} @array,

There is no way to change the variable map uses: whenever I write a map
the extends over more than a couple of lines, I start it like

map {
my $entry = $_;
...
} ...

so I get a variable with a real name.

Ben