# perldoc > SOAP::Data

---
type: CommandReference
command: SOAP::Data
mode: perldoc
section: ""
source: perldoc
---

## Quick Reference

- `SOAP::Data->new(name => 'idx', value => 5)` — create a data object
- `SOAP::Data->name('idx' => 5)` — shorthand for name and value
- `SOAP::Data->name('idx')->value(5)` — chained method
- `SOAP::Data->name('foo' => \SOAP::Data->value( SOAP::Data->name('bar' => '123') ))` — nested XML
- `SOAP::Data->type('xml' => $xml_content)` — raw XML insertion
- `SOAP::Data->name("myElement" => "myValue")->attr({ 'xmlns:foo2' => 'urn:Foo2', 'xmlns:foo3' => 'urn:Foo3' })` — multiple namespaces

## Name

SOAP::Data - manipulate and control Perl data expressed as SOAP data entities.

## Synopsis

perl
use SOAP::Data;
$obj = SOAP::Data->new(key => value, ...);
# or chained accessors:
$obj = SOAP::Data->name('idx')->value(5);
## Methods

- `new(name => 'idx', value => 5)` — constructor; accepts key/value pairs for attributes.
- `name([$new_name [, $new_value]])` — gets/sets the XML tag name; optionally updates value.
- `type('int')` — gets/sets the XML Schema type; useful when Perl type detection is wrong.
- `uri('http://www.perl.com/SOAP')` — gets/sets the namespace URI for the element.
- `prefix('perl')` — gets/sets the namespace prefix (label).
- `attr({ attr1 => 'value' })` — sets arbitrary XML attributes (must be namespace-qualified if not native to SOAP).
- `value([$new_value])` — gets/sets the encapsulated value.
- `actor($new_actor)` — gets/sets the SOAP actor attribute (for headers).
- `mustUnderstand([0|1])` — gets/sets the `mustUnderstand` attribute.
- `encodingStyle($soap_11_encoding)` — gets/sets an alternative encoding style.
- `root([1])` — explicitly marks the element as the root element.

All accessors (except `value`) accept a new value as an optional second argument to update the object's value.

## Type Detection

SOAP::Lite's serializer detects the type of scalar values using regular expressions. If detection is incorrect, explicitly set the type with `type()`:

perl
# default serializes as integer
$elem = SOAP::Data->name('idx')->value(5);
# force type 'long'
$elem = SOAP::Data->name('idx')->value(5)->type('long');
## Examples

### Simple Types

The following produce identical XML:

perl
$elem1 = SOAP::Data->new(name => 'idx', value => 5);
$elem2 = SOAP::Data->name('idx' => 5);
$elem3 = SOAP::Data->name('idx')->value(5);
### Complex Types (Nested XML)

perl
SOAP::Data->name('foo' => \SOAP::Data->value(
    SOAP::Data->name('bar' => '123')
));
Produces:
xml
<foo>
  <bar>123</bar>
</foo>
### Arrays

perl
$elem1 = SOAP::Data->name('item' => 123)->type('SomeObject');
$elem2 = SOAP::Data->name('item' => 456)->type('SomeObject');
push(@array, $elem1, $elem2);

my $client = SOAP::Lite
    ->readable(1)
    ->uri($NS)
    ->proxy($HOST);

$temp_elements = SOAP::Data
    ->name("CallDetails" => \SOAP::Data->value(
        SOAP::Data->name("elem1" => 'foo'),
        SOAP::Data->name("elem2" => 'baz'),
        SOAP::Data->name("someArray" => \SOAP::Data->value(
            SOAP::Data->name("someArrayItem" => @array)
                ->type("SomeObject"))
        )->type("ArrayOf_SomeObject") ))
    ->type("SomeObject");

$response = $client->someMethod($temp_elements);
### Raw XML

perl
$xml_content = "<foo><bar>123</bar></foo>";
$elem = SOAP::Data->type('xml' => $xml_content);
The XML is inserted verbatim; no validation is performed.

### Multiple Namespaces

perl
$elem = SOAP::Data->name("myElement" => "myValue")
                  ->attr({ 'xmlns:foo2' => 'urn:Foo2',
                           'xmlns:foo3' => 'urn:Foo3' });
Produces:
xml
<myElement xmlns:foo2="urn:Foo2" xmlns:foo3="urn:Foo3">myValue</myElement>
## See Also

- [SOAP::Header](https://metacpan.org/pod/SOAP::Header)
- [SOAP::SOM](https://metacpan.org/pod/SOAP::SOM)
- [SOAP::Serializer](https://metacpan.org/pod/SOAP::Serializer)