home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2000 May
/
Chip_2000-05_cd1.bin
/
zkuste
/
Perl
/
ActivePerl-5.6.0.613.msi
/
䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥
/
_d15d1c825844a676502fe84f7795e219
< prev
next >
Wrap
Text File
|
2000-03-23
|
7KB
|
167 lines
<HTML>
<HEAD>
<TITLE>sscanf - emulate the sscanf of the C<C> stdio library</TITLE>
<LINK REL="stylesheet" HREF="../../../Active.css" TYPE="text/css">
<LINK REV="made" HREF="mailto:">
</HEAD>
<BODY>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> sscanf - emulate the sscanf of the C<C> stdio library</P></STRONG>
</TD></TR>
</TABLE>
<A NAME="__index__"></A>
<!-- INDEX BEGIN -->
<UL>
<LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
<LI><A HREF="#synopsis">SYNOPSIS</A></LI>
<LI><A HREF="#description">DESCRIPTION</A></LI>
<LI><A HREF="#features">FEATURES</A></LI>
<LI><A HREF="#limitations">LIMITATIONS</A></LI>
<LI><A HREF="#examples">EXAMPLES</A></LI>
<LI><A HREF="#internals">INTERNALS</A></LI>
<LI><A HREF="#version">VERSION</A></LI>
<LI><A HREF="#author">AUTHOR</A></LI>
</UL>
<!-- INDEX END -->
<HR>
<P>
<H1><A NAME="name">NAME</A></H1>
<P>sscanf - emulate the <CODE>sscanf()</CODE> of the <CODE>C</CODE> stdio library</P>
<P>
<HR>
<H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
<UL>
<LI>Linux</LI>
<LI>Solaris</LI>
<LI>Windows</LI>
</UL>
<HR>
<H1><A NAME="synopsis">SYNOPSIS</A></H1>
<PRE>
use String::Scanf; # this will import sscanf() into the
# current namespace</PRE>
<PRE>
@values = sscanf($scanf_format_string, $scalar_to_scan);</PRE>
<PRE>
# the default scan target is the $_
@values = sscanf($scanf_format_string);</PRE>
<PRE>
# converting scanf formats to regexps (::format_to_re
# is never exported to the current namespace)</PRE>
<PRE>
$regexp_string = String::Scanf::format_to_re($scanf_format_string);</PRE>
<P>
<HR>
<H1><A NAME="description">DESCRIPTION</A></H1>
<P>Perl <CODE>sscanf()</CODE> can be used very much like the <CODE>C</CODE> stdio sscanf(), for
detailed <CODE>sscanf()</CODE> documentation please refer to your usual
documentation resources. The supported formats are: <CODE>[diuoxefgsc]</CODE>
and the character class <CODE>[]</CODE>.</P>
<P><STRONG>All</STRONG> of the format must match. If not, an empty list is returned
and all the values end up empty.</P>
<P>The <CODE>c</CODE> format returns an anonymous list (see perlref)
containing the numeric values of the characters it matched.</P>
<P>The ::format_to_re() function may be helpful if one wants to
develop her own parsing routines.</P>
<P>
<HR>
<H1><A NAME="features">FEATURES</A></H1>
<P>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 <STRONG>embedded</STRONG>, not leading or trailing.</P>
<P>If the <CODE>oh</CODE> formats are used, the octal/hexadecimal interpretation
is forced even without the leading <CODE>0</CODE> or <CODE>0x</CODE>.</P>
<P>
<HR>
<H1><A NAME="limitations">LIMITATIONS</A></H1>
<P>Certain features of the C <CODE>sscanf()</CODE> are unsupported:</P>
<PRE>
* the formats C<[npSC]>
* in the C<[efg]> formats the C<INF> and various C<NaN>s</PRE>
<P>The numeric formats are scanned in as strings, this meaning that
numeric overflows may occur. For example: <CODE>1.2345e67890</CODE> will match
the <CODE>%g</CODE> format but in most machines Perl cannot handle that large
floating point numbers and bizarre values may end up in the Perl
variable. Similar caveats apply for integer-type numbers. Results of
such huge numbers (or very tiny numbers, say, <CODE>1.24345e-67890</CODE>) are
implementation-defined, which translates quite often as <EM>garbage</EM>.
<STRONG>NOTE</STRONG>: if you really want <STRONG>Big</STRONG> numbers please consider
using the <CODE>Math::BigInt</CODE> and <CODE>Math::BigFloat</CODE>, these packages come
standard with Perl 5, or the <CODE>Math::Pari</CODE> package, available from
<CODE>CPAN</CODE>.</P>
<P>For Perl <integers> and <EM>floating point numbers</EM> are the same thing.
Also, the possible <CODE>hl</CODE> modifiers for the <EM>integers</EM> mean nothing:
they are accepted but still they do nothing because Perl does not care
about short/long integer differences.</P>
<P>The character class format is not so rigorously checked for
correctness that an illegal character class definition could
not be sneaked in. For example <CODE>[z-a,X]</CODE> is a <CODE>bad</CODE> example:
perfectly illegal as a character class but <CODE>String::Scanf</CODE> will
happily accept it. Beware.</P>
<P>The ::format_to_re() only does the scanf format -> regular expression
conversion. It ignores tricky things like the <CODE>c</CODE> format (see above)
and the %n$ argument reordering. If you want these, you may as well use
the full ::sscanf().</P>
<P>
<HR>
<H1><A NAME="examples">EXAMPLES</A></H1>
<PRE>
# business as usual</PRE>
<PRE>
($i, $s, $x) = sscanf('%d %3s %g', ' -5_678 abc 3.14e-99 9');</PRE>
<PRE>
# '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)</PRE>
<PRE>
($x, $y, $z) = sscanf('%i%3[a-e]%2c', ' 42acxde');</PRE>
<PRE>
# reordering the arguments: $a becomes 34, $b becomes 12</PRE>
<PRE>
($a, $b) = sscanf('%2$d %1$d', '12 34');</PRE>
<PRE>
# converting scanf formats to regexps</PRE>
<PRE>
$re = String::Scanf::format_to_re('%x');</PRE>
<P>More examples in the test set <CODE>t/scanf.t</CODE>.</P>
<P>
<HR>
<H1><A NAME="internals">INTERNALS</A></H1>
<P>The Perl <CODE>sscanf()</CODE> turns the <CODE>C</CODE>-<CODE>stdio</CODE> style <CODE>sscanf()</CODE> format
string into a Perl regexp (see perlre) which captures the wanted
values into submatches and returns the submatches as a list.</P>
<P>Originally written for purposes of debugging but also useful
for educational purposes:</P>
<PRE>
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</PRE>
<P>
<HR>
<H1><A NAME="version">VERSION</A></H1>
<P>v1.1, $Id: Scanf.pm,v 1.8 1995/12/27 08:32:28 jhi Exp $</P>
<P>
<HR>
<H1><A NAME="author">AUTHOR</A></H1>
<P>Jarkko Hietaniemi, <CODE>Jarkko.Hietaniemi@iki.fi</CODE></P>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> sscanf - emulate the sscanf of the C<C> stdio library</P></STRONG>
</TD></TR>
</TABLE>
</BODY>
</HTML>