Programming Perl in C
Comparison Functions
The Perl API contains a set of calls to make comparing SVs easier. First, there are
functions to test whether an SV is true in the Perl sense:
if (sv_true(sv)) warn("sv is true!");
Tests for equality can be expressed using these two functions:
if (sv_eq(sv1, sv2)) warn("The SVs are equal");
if (sv_cmp(sv1, sv2) == 0) warn("The SVs are equal");
The Perl API also comes with a full set of normal string comparison functions.
These are useful when you have an SV and a normal C string to compare. You might
be tempted to upgrade the string to an SV and use sv_eq(), but that's generally
not an efficient solution.
char *string = SvPV(sv, len); // extract string from an SV
if (strEQ(string, "foo")) warn("SV contains foo");
if (strNE(string, "foo")) warn("SV does not contain foo");
if (strGT(string, "foo")) warn("SV is greater than foo");
if (strGE(string, "foo")) warn("SV is greater than or equal to foo");
if (strLT(string, "foo")) warn("SV is less than foo");
if (strLE(string, "foo")) warn("SV is less than or equal to foo");
if (strnEQ(string, "foo", 3)) warn("SV starts with foo");
You can test for undef by comparing the SV* to the globally defined PL_sv_undef:
if (sv == &PL_sv_undef) warn("sv is undefined!");
Notice that the preceding test uses the & operator to get the address of PL_sv_undef
and compares it to the address of the SV since SVs are always handled using point
ers. A common mistake is to leave off the & on PL_sv_undef and end up with confus
ing compiler errors about type mismatches.
Array Values (AV)
Perl's arrays are represented in C by the AV type. Underneath the covers an AV is
simply a normal C array of SVs with some bookkeeping information to make
certain operations faster. However, just like SVs, AVs are opaque types, and you
must work with them through the supplied set of functions.
18
183
3
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved