strtol#

Parse a signed integer from the start of a string, in a given base.

Synopsis#

use POSIX qw(strtol);
my ($n, $unparsed) = strtol("0x2a rest", 0);   # (42, 5)

What you get back#

A two-element list: the parsed integer and the number of bytes that were not consumed. Base 0 auto-detects from prefix (0x, 0b, leading 0); otherwise $base must be 2-36.

Edge cases#

  • Invalid base: returns (undef, undef) and sets $! to EINVAL.

  • Overflow: returns LONG_MIN / LONG_MAX and sets $! to ERANGE.

Differences from upstream#

Fully compatible with upstream POSIX.

See also#

  • strtoul — unsigned variant.

  • strtod — floating-point variant.