Skip to content

Latest commit

 

History

History
149 lines (107 loc) · 5.12 KB

coercion.pod

File metadata and controls

149 lines (107 loc) · 5.12 KB

Coercion

A Perl variable can hold at various times values of different types--strings, integers, rational numbers, and more. Rather than attaching type information to variables, Perl relies on the context provided by operators (value_contexts) to know what to do with values. By design, Perl attempts to do what you meanCalled DWIM for do what I mean or dwimmery., though you must be specific about your intentions. If you treat a variable which happens to contain a number as a string, Perl will do its best to coerce that number into a string.

Boolean Coercion

Boolean coercion occurs when you test the truthiness of a value, such as in an if or while condition. Numeric 0, undef, the empty string, and the string '0' all evaluate as false. All other values--including strings which may be numerically equal to zero (such as '0.0', '0e', and '0 but true')--evaluate as true.

When a scalar has both string and numeric components (dualvars), Perl 5 prefers to check the string component for boolean truth. '0 but true' evaluates to zero numerically, but it is not an empty string, thus it evaluates to a true value in boolean context.

String Coercion

String coercion occurs when using string operators such as comparisons (eq and cmp), concatenation, split, substr, and regular expressions, as well as when using a value as a hash key. The undefined value stringifies to an empty string, produces a "use of uninitialized value" warning. Numbers stringify to strings containing their values, such that the value 10 stringifies to the string 10. You can even split a number into individual digits with:

Numeric Coercion

Numeric coercion occurs when using numeric comparison operators (such as == and <=>), when performing mathematic operations, and when using a value as an array or list index. The undefined value numifies to zero and produces a "Use of uninitialized value" warning. Strings which do not begin with numeric portions also numify to zero and produce an "Argument isn't numeric" warning. Strings which begin with characters allowed in numeric literals numify to those values and produce no warnings, such that 10 leptons leaping numifies to 10 and 6.022e23 moles marauding numifies to 6.022e23.

The core module Scalar::Util contains a looks_like_number() function which uses the same parsing rules as the Perl 5 grammar to extract a number from a string.

Reference Coercion

Using a dereferencing operation on a non-reference turns that value into a reference. This process of autovivification (autovivification) is handy when manipulating nested data structures (nested_data_structures):

Although the hash never contained values for Brad and Jack, Perl helpfully created hash references for them, then assigned each a key/value pair keyed on id.

Cached Coercions

Perl 5's internal representation of values stores both string and numeric values. Stringifying a numeric value does not replace the numeric value. Instead, it attaches a stringified value, so that the representation contains both components. Similarly, numifying a string value populates the numeric component while leaving the string component untouched.

Certain Perl operations prefer to use one component of a value over another--boolean checks prefer strings, for example. If a value has a cached representation in a form you do not expect, relying on an implicit conversion may produce surprising results. You almost never need to be explicit about what you expectYour author can recall doing so twice in over a decade of programming Perl 5, but knowing that this caching occurs may someday help you diagnose an odd situation.

Dualvars

The multi-component nature of Perl values is available to users in the form of dualvars. The core module Scalar::Util provides a function dualvar() which allows you to bypass Perl coercion and manipulate the string and numeric components of a value separately:

POD ERRORS

Hey! The above document had some coding errors, which are explained below:

Around line 3:

A non-empty Z<>

Around line 9:

Deleting unknown formatting code N<>

Around line 20:

A non-empty Z<>

Around line 56:

A non-empty Z<>

Around line 108:

A non-empty Z<>

Around line 118:

Deleting unknown formatting code N<>

Around line 128:

A non-empty Z<>