lecture
in color
use, BEGIN when found (by calling the interpreter)
BEGIN: executed even before compilation is finished, when encountered.
CHECK: executed when compilation is finished, before running the program.
INIT: executed just before your program starts.
END: executed after program is done.
AUTOLOAD, DESTROY, etc. BEGIN, CHECK, INIT, END blocks.
BEGIN and INIT blocks are executed in occurrence order (FIFO).
CHECK and END blocks are executed in the opposite
of occurrence order (LIFO). contents of perl_int/special.pl...
#! /var/local/couch/bin/perl
BEGIN { print "begin01\n"; }
BEGIN { print "begin02\n"; }
CHECK { print "check01\n"; }
CHECK { print "check02\n"; }
INIT { print "init01\n"; }
INIT { print "init02\n"; }
END { print "end01\n"; }
END { print "end02\n"; }
print "hello!\n";
...end of perl_int/special.pl
This prints
contents of perl_int/special.pl.out... begin01 begin02 check02 check01 init01 init02 hello! end02 end01 ...end of perl_int/special.pl.out
BEGIN blocks) eval blocks). END blocks. $a=$a+1 as $a+=1). SV: scalar value.
AV: array value.
HV: hash value.
IV: integer value (see also I32, I16, UV, U32, U16)
RV: reference value (special kind of SV).
GV: glob value.
SV* newSViv(IV);
SV* newSVnv(double);
SV* newSVpv(const char*, int);
SV* newSVpvn(const char*, int);
sprintf)
SV* newSVpvf(const char*, ...);
SV* newSVsv(SV*);
void sv_setiv(SV*, IV);
void sv_setuv(SV*, UV);
void sv_setnv(SV*, double);
void sv_setpv(SV*, const char*);
void sv_setpvn(SV*, const char*, int)
void sv_setpvf(SV*, const char*, ...)
void sv_setpvfn(SV*, const char*, STRLEN, va_list *, SV **, I32, bool);
void sv_setsv(SV*, SV*);
SvIV(SV*)
SvUV(SV*)
SvNV(SV*)
SvPV(SV*, STRLEN len)
SvPV_nolen(SV*)
SvTRUE(SV*)
SvIOK(SV*)
SvNOK(SV*)
SvPOK(SV*)
void sv_catpv(SV*, const char*);
void sv_catpvn(SV*, const char*, STRLEN);
void sv_catpvf(SV*, const char*, ...);
void sv_catpvfn(SV*, const char*, STRLEN, va_list *, SV **, I32, bool);
.= operator)
void sv_catsv(SV*, SV*);
SV* get_sv("package::varname", FALSE);
SvOK(SV*)
SvREFCNT_dec(SV*);
SV, the SV's reference count
is updated.
AV* newAV();
SV's
AV* av_make(I32 num, SV **ptr);
push, pop, shift, unshift
void av_push(AV*, SV*); SV* av_pop(AV*); SV* av_shift(AV*); void av_unshift(AV*, I32 num);
I32 av_len(AV*);
SV** av_fetch(AV*, I32 key, I32 lval);(
lval != 0 means to store an undef at key as well) SV** av_store(AV*, I32 key, SV* val);
void av_clear(AV*);
void av_undef(AV*);
void av_extend(AV*, I32 key);
HV* newHV();
SV** hv_store(HV*, const char* key, U32 klen, SV* val, U32 hash);
key is a string, and you must specify its length klen.
hash is a precomputed hash value for the key
(0 to make hv_store calculate it).SV** hv_fetch(HV*, const char* key, U32 klen, I32 lval);
lval!=0 means initialize value to undef even if it doesn't exist.
bool hv_exists(HV*, const char* key, U32 klen);
SV* hv_delete(HV*, const char* key, U32 klen, I32 flags);
void hv_clear(HV*);
void hv_undef(HV*);
each)
I32 hv_iterinit(HV*);
HE* hv_iternext(HV*);
char* hv_iterkey(HE* entry, I32* retlen);
SV* hv_iterval(HV*, HE* entry);
hv_iternext,
hv_iterkey, and hv_iterval. The key and retlen arguments are return values for the key and its
length. The value is returned in the SV* argument SV* hv_iternextsv(HV*, char* key, I32* retlen);
So, to iterate over a hash h, one would write in C:
char key[1024]; int keylen; SV* v;
hv_iterinit(h);
while ((v=hv_iternextsv(h,key,&keylen))!=0) {
# do something with v
}
(note: this code may be in error; checking on it)
lecture
in color