home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BURKS 2
/
BURKS_AUG97.ISO
/
SLAKWARE
/
D13
/
PERL2.TGZ
/
perl2.tar
/
usr
/
lib
/
perl5
/
pod
/
perlguts.pod
< prev
next >
Wrap
Text File
|
1996-06-28
|
59KB
|
2,195 lines
=head1 NAME
perlguts - Perl's Internal Functions
=head1 DESCRIPTION
This document attempts to describe some of the internal functions of the
Perl executable. It is far from complete and probably contains many errors.
Please refer any questions or comments to the author below.
=head1 Datatypes
Perl has three typedefs that handle Perl's three main data types:
SV Scalar Value
AV Array Value
HV Hash Value
Each typedef has specific routines that manipulate the various data types.
=head2 What is an "IV"?
Perl uses a special typedef IV which is large enough to hold either an
integer or a pointer.
Perl also uses two special typedefs, I32 and I16, which will always be at
least 32-bits and 16-bits long, respectively.
=head2 Working with SV's
An SV can be created and loaded with one command. There are four types of
values that can be loaded: an integer value (IV), a double (NV), a string,
(PV), and another scalar (SV).
The four routines are:
SV* newSViv(IV);
SV* newSVnv(double);
SV* newSVpv(char*, int);
SV* newSVsv(SV*);
To change the value of an *already-existing* SV, there are five routines:
void sv_setiv(SV*, IV);
void sv_setnv(SV*, double);
void sv_setpvn(SV*, char*, int)
void sv_setpv(SV*, char*);
void sv_setsv(SV*, SV*);
Notice that you can choose to specify the length of the string to be
assigned by using C<sv_setpvn> or C<newSVpv>, or you may allow Perl to
calculate the length by using C<sv_setpv> or by specifying 0 as the second
argument to C<newSVpv>. Be warned, though, that Perl will determine the
string's length by using C<strlen>, which depends on the string terminating
with a NUL character.
To access the actual value that an SV points to, you can use the macros:
SvIV(SV*)
SvNV(SV*)
SvPV(SV*, STRLEN len)
which will automatically coerce the actual scalar type into an IV, double,
or string.
In the C<SvPV> macro, the length of the string returned is placed into the
variable C<len> (this is a macro, so you do I<not> use C<&len>). If you do not
care what the length of the data is, use the global variable C<na>. Remember,
however, that Perl allows arbitrary strings of data that may both contain
NUL's and not be terminated by a NUL.
If you simply want to know if the scalar value is TRUE, you can use:
SvTRUE(SV*)
Although Perl will automatically grow strings for you, if you need to force
Perl to allocate more memory for your SV, you can use the macro
SvGROW(SV*, STRLEN newlen)
which will determine if more memory needs to be allocated. If so, it will
call the function C<sv_grow>. Note that C<SvGROW> can only increase, not
decrease, the allocated memory of an SV.
If you have an SV and want to know what kind of data Perl thinks is stored
in it, you can use the following macros to check the type of SV you have.
SvIOK(SV*)
SvNOK(SV*)
SvPOK(SV*)
You can get and set the current length of the string stored in an SV with
the following macros:
SvCUR(SV*)
SvCUR_set(SV*, I32 val)
You can also get a pointer to the end of the string stored in the SV
with the macro:
SvEND(SV*)
But note that these last three macros are valid only if C<SvPOK()> is true.
If you want to append something to the end of string stored in an C<SV*>,
you can use the following functions:
void sv_catpv(SV*, char*);
void sv_catpvn(SV*, char*, int);
void sv_catsv(SV*, SV*);
The first function calculates the length of the string to be appended by
using C<strlen>. In the second, you specify the length of the string
yourself. The third function extends the string stored in the first SV
with the string stored in the second SV. It also forces the second SV to
be interpreted as a string.
If you know the name of a scalar variable, you can get a pointer to its SV
by using the following:
SV* perl_get_sv("varname", FALSE);
This returns NULL if the variable does not exist.
If you want to know if this variable (or any other SV) is actually C<defined>,
you can call:
SvOK(SV*)
The scalar C<undef> value is stored in an SV instance called C<sv_undef>. Its
address can be used whenever an C<SV*> is needed.
There are also the two values C<sv_yes> and C<sv_no>, which contain Boolean
TRUE and FALSE values, respectively. Like C<sv_undef>, their addresses can
be used whenever an C<SV*> is needed.
Do not be fooled into thinking that C<(SV *) 0> is the same as C<&sv_undef>.
Take this code:
SV* sv = (SV*) 0;
if (I-am-to-return-a-real-value) {
sv = sv_2mortal(newSViv(42));
}
sv_setsv(ST(0), sv);
This code tries to return a new SV (which contains the value 42) if it should
return a real value, or undef otherwise. Instead it has returned a null
pointer which, somewhere down the line, will cause a segmentation violation,
or just weird results. Change the zero to C<&sv_undef> in the first line and
all will be well.
To free an SV that you've created, call C<SvREFCNT_dec(SV*)>. Normally this
call is not necessary. See the section on B<MORTALITY>.
=head2 What's Really Stored in an SV?
Recall that the usual method of determining the type of scalar you have is
to use C<Sv*OK> macros. Since a scalar can be both a number and a string,
usually these macros will always return TRUE and calling the C<Sv*V>
macros will do the appropriate conversion of string to integer/double or
integer/double to string.
If you I<really> need to know if you have an integer, double, or string
pointer in an SV, you can use the following three macros instead:
SvIOKp(SV*)
SvNOKp(SV*)
SvPOKp(SV*)
These will tell you if you truly have an integer, double, or string pointer
stored in your SV. The "p" stands for private.
In general, though, it's best to just use the C<Sv*V> macros.
=head2 Working with AV's
There are two ways to create and load an AV. The first method just creates
an empty AV:
AV* newAV();
The second method both creates the AV and initially populates it with SV's:
AV* av_make(I32 num, SV **ptr);
The second argument points to an array containing C<num> C<SV*>'s. Once the
AV has been created, the SV's can be destroyed, if so desired.
Once the AV has been created, the following operations are possible on AV's:
void av_push(AV*, SV*);
SV* av_pop(AV*);
SV* av_shift(AV*);
void av_unshift(AV*, I32 num);
These should be familiar operations, with the exception of C<av_unshift>.
This routine adds C<num> elements at the front of the array with the C<undef>
value. You must then use C<av_store> (described below) to assign values
to these new elements.
Here are some other functions:
I32 av_len(AV*); /* Returns highest index value in array */
SV** av_fetch(AV*, I32 key, I32 lval);
/* Fetches value at key offset, but it stores an undef value
at the offset if lval is non-zero */
SV** av_store(AV*, I32 key, SV* val);
/* Stores val at offset key */
Take note that C<av_fetch> and C<av_store> return C<SV**>'s, not C<SV*>'s.
void av_clear(AV*);
/* Clear out all elements, but leave the array */
void av_undef(AV*);
/* Undefines the array, removing all elements */
void av_extend(AV*, I32 key);
/* Extend the array to a total of key elements */
If you know the name of an array variable, you can get a pointer to its AV
by using the following:
AV* perl_get_av("varname", FALSE);
This returns NULL if the variable does not exist.
=head2 Working with HV's
To create an HV, you use the following routine:
HV* newHV();
Once the HV has been created, the following operations are possible on HV's:
SV** hv_store(HV*, char* key, U32 klen, SV* val, U32 hash);
SV** hv_fetch(HV*, char* key, U32 klen, I32 lval);
The C<klen> parameter is the length of the key being passed in. The C<val>
argument contains the SV pointer to the scalar being stored, and C<hash> is
the pre-computed hash value (zero if you want C<hv_store> to calculate it
for you). The C<lval> parameter indicates whether this fetch is actually a
part of a store operation.
Remember that C<hv_store> and C<hv