Chapter 17. Dynamic Languages and XML

Although much attention is currently directed to Java and .NET, many developers work with dynamic programming languages such as Perl, Python, and Ruby. Dynamic languages differ from languages such as Java or C# in that the variables are usually not fixed to a particular type. For example, the following code fragment is perfectly valid in a dynamic language:

var x;
x = 5;
x = "testing";

Notice that the variable declaration does not identify the type of the variable, and that it may be used to store numeric, string or even object values. In most dynamic languages, the variable declaration is not even needed: You can simply begin to use a variable. Some languages, such as Python and Ruby, go even further, supporting what has become known as duck typing. With duck typing, a variable is treated as a particular type as long as it supports the same method and property calls as the desired type does. That is, if a block of code is expecting a method called toString, then any object may be used as long as it supports a toString method. This demonstrates a powerful feature in dynamic languages, the ability to rapidly prototype and test a block of code. (The term duck typing derives from the old expression, "If it walks like a duck, and quacks like a duck, it must be a duck.")

Dynamic languages are frequently also known as scripting languages, as many of them are interpreted languages that may be used directly from the command-line, without requiring a separate ...

Get Professional XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.