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

Hi Monks,

I'm a newbie to Perl (though with years of PHP experience) and am having the following problem.

I'm building a data structure to send using SOAP functions, example:

$param = SOAP::Data->name("ListOfContact" => \SOAP::Data->value( SOAP::Data->name("Contact" => \SOAP::Data->value( SOAP::Data->name("ContactId" => $contact->{ContactId}), SOAP::Data->name("ContactLastName" => $contact->{LASTNAME}), )) ));

This works as I want and expect. I'd like to add some more Data->name sections however I'd like to do this dynamically based on a database query. So in each row returned from the database I'm constructing a string variable in the foreach like:

"SOAP::Data->name("DynamicFieldName" => DynamicValue),"

I'd then like to somehow include these new lines into the SOAP param constructor, but not sure how to get it out of the string variable without it just printing the string itself. I also tried building the whole param variable into one string var and trying some sort of eval on it.

I hope that makes sense and any help would be really appreciated.

Thanks very much, Mark.

Replies are listed 'Best First'.
Re: Using a dynamic variable within a SOAP data structure
by Khen1950fx (Canon) on Apr 20, 2011 at 12:29 UTC
    You can put all of your new lines into an array of parameters like this:
    #!/usr/bin/perl use strict; use warnings; use SOAP::Lite +autodispatch => uri => 'http://www.soaplite.com/My/Parameters', proxy => 'http://localhost/', on_fault => sub { my($soap, $res) = @_; die ref $res ? $res->faultdetail : $soap->transport->status, " +\n"; }; my @parameters = ( SOAP::Data->name(list_contact => 'ListOfContact'), SOAP::Data->name(contact => 'Contact'), SOAP::Data->name(contact_id => 'ContactID'), SOAP::Data->name(contact_lastname => 'LASTNAME') ); print "Parameters: ", join(' ', map {$_->value} @parameters), "\n"; print "By order: ", byorder(@parameters), "\n"; print "By name: ", byname(@parameters), "\n";
Re: Using a dynamic variable within a SOAP data structure
by firstaspect (Initiate) on Apr 20, 2011 at 12:58 UTC
    Thanks for the replies. Khen1950fx, your approach looks like it would do what I want, apart from if you take a look at my original code then it's actually building a hierachy so ListOfContact contains Contact which then contains all the other values, so I'm not sure it's as simple as just creating an array of each. Unless perhaps I can create an array of the parameters under Contact and then use that array as the Data->value of Contact.
Re: Using a dynamic variable within a SOAP data structure
by InfiniteSilence (Curate) on Apr 20, 2011 at 12:23 UTC
    It has been a while since I've used SOAP::Lite but by taking a quick look at your code I'm thinking perhaps you should be simply building your content as XML and passing it as shown in the perldoc: $elem = SOAP::Data->type('xml' => $xml_content);

    Celebrate Intellectual Diversity