NAME

Twilio::TwiML::TwiML - Base class of every TwiML element

SYNOPSIS

use Twilio::TwiML::GenericNode;

my $response = Twilio::TwiML::GenericNode->new('Response');
my $say      = $response->add_child('Say', 'Hello', { voice => 'alice' });
$say->set_attribute(language => 'es-MX');

print $response->as_xml;

DESCRIPTION

Builds a TwiML document as a tree of elements, and writes it the way twilio-php does. An element converts to its document in string context, so it can be printed directly.

Attributes are written in alphabetical order, because a Perl hash has no order of its own. Twilio does not depend on the order. An attribute with an undefined value is left out.

Text can be given as characters or as UTF-8 bytes. The document is returned as UTF-8 bytes, ready to send, so do not add an encoding layer when printing it.

METHODS

new

my $element = Twilio::TwiML::TwiML->new($name, $value, \%attributes);

append

$element->append('Some text');
$element->append($other_element);

Adds text or an element at the end, and returns $element.

nest

my $child = $element->nest($other_element);

Adds an element at the end, and returns that element.

add_child

my $child = $element->add_child($name, $value, \%attributes);

Adds a new element with any name, for TwiML this library does not know about, and returns it.

set_attribute

$element->set_attribute(name => 'value');

Sets an attribute and returns $element. An array reference is written as a list separated by spaces, and a reference to 1 or 0 as true or false. A name the element does not know is written as it is.

attribute_table

Returns what each element knows about its attributes. Generated elements use it to turn send_digits into sendDigits, and a Perl true value into true.

as_xml

Returns the document, with the XML declaration.

LICENSE

Copyright (c) 2026 Francisco Zarabozo. Released under the MIT License.