Coercion
Learn about coercion and its types.
We'll cover the following...
Throughout the lifetime of a Perl variable, it may contain values of different types—strings, integers, rational numbers, and more. Instead of attaching type information to variables, Perl relies on the context provided by operators to determine how to handle values. Perl attempts to do what we mean (we may hear this referred to as DWIM for “Do what I mean” or “Dwimmery”), though we must be specific about our intentions. If we treat a value as a string, Perl will do its best to coerce that value into a string.
Boolean coercion
Boolean coercion occurs when we test the truthiness of a value, such as in an if
or while
condition. Numeric 0
, undef
, the empty string, and the ...