Skip to content

Latest commit

 

History

History
73 lines (49 loc) · 2.11 KB

automatic_dereferencing.pod

File metadata and controls

73 lines (49 loc) · 2.11 KB

Automatic Dereferencing

Perl can automatically dereference certain references on your behalf. Given an array reference in $arrayref, you can write:

Given an expression which returns an array reference, you can do the same:

The same goes for the array operators pop, shift, unshift, splice, keys, values, and each and the hash operators keys, values, and each. If the reference provided is not of the proper type--if it does not dereference properly--Perl will throw an exception. While this may seem more dangerous than explicitly dereferencing references directly, it is in fact the same behavior:

Unfortunately, this automatic dereferencing has two problems. First, it only works on plain variables. If you have a blessed array or hash, a tied hash, or an object with array or hash overloading, Perl will throw a runtime exception instead of dereferencing the reference.

Second, remember that each, keys, and values can operate on both arrays and hashes. You can't look at:

... and tell whether @items contains a list of key/value pairs or index/value pairs, because you don't know whether you should expect $ref to refer to a hash or an array. Yes, choosing good variable names will help, but this code is intrinsically confusing.

Neither of these drawbacks make this syntax unusable in general, but its rough edges and potential for confusing readers make it less useful than it could be.

POD ERRORS

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

Around line 3:

A non-empty Z<>