Value and type errors#
These diagnostics fire when a value is not the kind of thing the operation needs: a plain scalar where a reference was expected, a zero where a divisor was expected, a read-only value where a writable one was expected. All are fatal (F) unless noted.
Arithmetic#
`Illegal division by zero`#
(F) The right-hand side of / or % evaluated to zero. Numeric division and modulus have no result at zero, so Perl dies rather than returning infinity or NaN. Guard the divisor:
my $rate = $count ? $total / $count : 0;
`Illegal modulus zero`#
(F) The right operand of % was zero. Same cause and same fix as division by zero, for the modulus operator.
References#
`Not a reference`#
(F) A dereference (@$x, %$x, $$x, $x->[0], $x->{k}) was attempted on a value that is not a reference. Most often $x is undef or a plain string. Check it first:
die "expected an arrayref" unless ref $x eq 'ARRAY';
`Not a subroutine reference`#
(F) A value used where a code reference was required (for example the second argument to a higher-order function) is not a CODE ref. `set_subname: not a code reference` and `set_prototype: not a code reference` are the same error reported by Sub::Util, and `PadWalker: cv is not a code reference` is the PadWalker form.
Restricted hashes#
A hash locked with Hash::Util’s lock_keys rejects access to keys outside its allowed set.
`Attempt to access disallowed key '%s' in a restricted hash`#
(F) You read or wrote a key that the restricted hash does not permit. Either add the key to the allowed set, or unlock the hash.
`Attempt to delete readonly key '%s' from a restricted hash`#
(F) You tried to delete a key that the hash has locked against deletion.
Read-only values#
`Modification of a read-only value attempted`#
(F) An assignment or in-place operation targeted something that cannot be changed: a literal, a constant, a value passed by the caller and made read-only, or a Readonly/const value. A common surprise is modifying $_ inside a map/grep block when the source list elements are read-only.
Filehandles#
`Bad filehandle: %s`#
(F) An I/O operation was handed something that is not a usable filehandle - an undefined value, a closed handle, or a bareword that names no open handle. Open the handle (and check the result) before using it:
open my $fh, '<', $path or die "open $path: $!";
Memory#
`Out of memory`#
(F) An allocation failed. Usually the program asked for an implausibly large string, array, or hash - check the size you are requesting before assuming the machine is at fault.
See also#
References - what a reference is and how dereferencing works; the background for the “Not a reference” errors.
ref- test what kind of reference a value holds before you dereference it.Hash::Util-lock_keys,unlock_keys, and the rest of the restricted-hash machinery.General fatal errors - method-resolution and inheritance errors that are also about what a value is.