sscanf - emulate the
use String::Scanf; # this will import sscanf() into the # current namespace
@values = sscanf($scanf_format_string, $scalar_to_scan);
# the default scan target is the $_ @values = sscanf($scanf_format_string);
# converting scanf formats to regexps (::format_to_re # is never exported to the current namespace)
$regexp_string = String::Scanf::format_to_re($scanf_format_string);
All of the format must match. If not, an empty list is returned and all the values end up empty.
The ::format_to_re() function may be helpful if one wants to develop her own parsing routines.
Embedded underscores are accepted in numbers just like in Perl, even in octal/hexadecimal numbers (Perl does not currently support this). Please note the word embedded, not leading or trailing.
Certain features of the C
* the formats C<[npSC]> * in the C<[efg]> formats the C<INF> and various C<NaN>s
The numeric formats are scanned in as strings, this meaning that
numeric overflows may occur. For example:
For Perl <integers> and floating point numbers are the same thing.
Also, the possible
The character class format is not so rigorously checked for
correctness that an illegal character class definition could
not be sneaked in. For example
The ::format_to_re() only does the scanf format -> regular expression
conversion. It ignores tricky things like the
# business as usual
($i, $s, $x) = sscanf('%d %3s %g', ' -5_678 abc 3.14e-99 9');
# 'skip leading whitespace': $x becomes 42 despite the leading space # 'the illegal character': $y becomes 'ab' despite the '3' # 'c' format: $z becomes [120 100], the numeric values of 'x' # and 'd' (assuming ASCII or ISO Latin 1)
($x, $y, $z) = sscanf('%i%3[a-e]%2c', ' 42acxde');
# reordering the arguments: $a becomes 34, $b becomes 12
($a, $b) = sscanf('%2$d %1$d', '12 34');
# converting scanf formats to regexps
$re = String::Scanf::format_to_re('%x');
More examples in the test set
Originally written for purposes of debugging but also useful for educational purposes:
String::Scanf::debug(1); # turn on debugging: shows the regexps # used and the possible reordering list # and the character (%c) conversion targets String::Scanf::debug(0); # turn off debugging print String::Scanf::debug(), "\n"; # the current debug status
v1.1, $Id: Scanf.pm,v 1.8 1995/12/27 08:32:28 jhi Exp $