NAME

Data::Str2int - int, int str to int; else undef. No warnings

SYNOPSIS

int, int str to int; else undef. No warnings.

#####
# Subroutine interface
#  
use Data::Str2Num qw(str2int);

$integer = str2int($string);

DESCRIPTION

The Data::SecsPack program module supercedes this program module. The Data::SecsPack::str2int subroutine, in a scalar context, behaves the same and supercedes C&<Data::StrInt::str2int>. In time, this module will vanish.

The str2int subroutine translates an scalar numeric string and a scalar number to a scalar integer; otherwsie it returns an undef.

Perl itself has a documented function, '0+$x', that converts a number scalar so that its internal storage is an integer (See p.351, 3rd Edition of Programming Perl). "If it cannot perform the conversion, it leaves the integer 0." In addition the 0 + also produces a warning.

So how do you tell a conversion failure from the number 0? Compare the output to the input? Trap the warning? Surprising not all Perls, some Microsoft Perls in particular, may leave the internal storage as a scalar string and do not do numeric strings. Perl 5.6 under Microsoft has a broken '0+' and is no longer actively supported. It is still very popular and widely used on web hosting computers.

What is $x for the following, Perl 5.6, Microsoft:

my $x = 0 + '0x100';  # $x is 0 with a warning  

The str2int provides a different behavior that is more usefull in many situations as follows:

$x = str2int('033');   # $x is 27
$x = str2int('0xFF');  # $x is 255
$x = str2int('255');   # $x is 255
$x = str2int('hello'); # $x is undef no warning
$x = str2int(0.5);     # $x is undef no warning
$x = str2int(1E0);     # $x is 1 
$x = str2int(0xf);     # $x is 15
$x = str2int(1E30);    # $x is undef no warning

The str2int pulls out anything that resembles an integer; otherwise it returns undef with no warning. This makes the str2int subroutine not only useful for forcing an integer conversion but also for parsing scalars from strings.

The Perl code is a few lines without starting any whatevers with a Perl eval and attempting to trap all the warnings and dies, and without the regular expression engine with its overhead. The code works on broken Microsoft 5.6 Perls.

SEE ALSO

Data::SecsPack