This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate:
[perl5.git] / sv.h
CommitLineData
a0d0e21e 1/* sv.h
79072805 2 *
e6906430
JH
3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, by Larry Wall and others
79072805
LW
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
79072805
LW
9 */
10
a0d0e21e
LW
11#ifdef sv_flags
12#undef sv_flags /* Convex has this in <signal.h> for sigvec() */
13#endif
14
954c1994 15/*
ccfc67b7
JH
16=head1 SV Flags
17
954c1994 18=for apidoc AmU||svtype
8cf8f3d1 19An enum of flags for Perl types. These are found in the file B<sv.h>
954c1994
GS
20in the C<svtype> enum. Test these flags with the C<SvTYPE> macro.
21
22=for apidoc AmU||SVt_PV
23Pointer type flag for scalars. See C<svtype>.
24
25=for apidoc AmU||SVt_IV
26Integer type flag for scalars. See C<svtype>.
27
28=for apidoc AmU||SVt_NV
29Double type flag for scalars. See C<svtype>.
30
31=for apidoc AmU||SVt_PVMG
32Type flag for blessed scalars. See C<svtype>.
33
34=for apidoc AmU||SVt_PVAV
35Type flag for arrays. See C<svtype>.
36
37=for apidoc AmU||SVt_PVHV
38Type flag for hashes. See C<svtype>.
39
40=for apidoc AmU||SVt_PVCV
41Type flag for code refs. See C<svtype>.
42
43=cut
44*/
45
79072805 46typedef enum {
a0d0e21e
LW
47 SVt_NULL, /* 0 */
48 SVt_IV, /* 1 */
49 SVt_NV, /* 2 */
50 SVt_RV, /* 3 */
51 SVt_PV, /* 4 */
52 SVt_PVIV, /* 5 */
53 SVt_PVNV, /* 6 */
54 SVt_PVMG, /* 7 */
55 SVt_PVBM, /* 8 */
56 SVt_PVLV, /* 9 */
57 SVt_PVAV, /* 10 */
58 SVt_PVHV, /* 11 */
59 SVt_PVCV, /* 12 */
60 SVt_PVGV, /* 13 */
61 SVt_PVFM, /* 14 */
62 SVt_PVIO /* 15 */
79072805
LW
63} svtype;
64
79072805
LW
65/* Using C's structural equivalence to help emulate C++ inheritance here... */
66
5e045b90 67struct STRUCT_SV { /* struct sv { */
463ee0b2 68 void* sv_any; /* pointer to something */
79072805 69 U32 sv_refcnt; /* how many references to us */
8990e307 70 U32 sv_flags; /* what we are */
79072805
LW
71};
72
73struct gv {
463ee0b2 74 XPVGV* sv_any; /* pointer to something */
79072805 75 U32 sv_refcnt; /* how many references to us */
8990e307 76 U32 sv_flags; /* what we are */
79072805
LW
77};
78
79struct cv {
232e078e 80 XPVCV* sv_any; /* pointer to something */
79072805 81 U32 sv_refcnt; /* how many references to us */
8990e307 82 U32 sv_flags; /* what we are */
79072805
LW
83};
84
85struct av {
463ee0b2 86 XPVAV* sv_any; /* pointer to something */
79072805 87 U32 sv_refcnt; /* how many references to us */
8990e307 88 U32 sv_flags; /* what we are */
79072805
LW
89};
90
91struct hv {
463ee0b2 92 XPVHV* sv_any; /* pointer to something */
79072805 93 U32 sv_refcnt; /* how many references to us */
8990e307
LW
94 U32 sv_flags; /* what we are */
95};
96
97struct io {
98 XPVIO* sv_any; /* pointer to something */
99 U32 sv_refcnt; /* how many references to us */
100 U32 sv_flags; /* what we are */
79072805
LW
101};
102
954c1994 103/*
ccfc67b7
JH
104=head1 SV Manipulation Functions
105
954c1994
GS
106=for apidoc Am|U32|SvREFCNT|SV* sv
107Returns the value of the object's reference count.
108
109=for apidoc Am|SV*|SvREFCNT_inc|SV* sv
110Increments the reference count of the given SV.
111
112=for apidoc Am|void|SvREFCNT_dec|SV* sv
113Decrements the reference count of the given SV.
114
115=for apidoc Am|svtype|SvTYPE|SV* sv
116Returns the type of the SV. See C<svtype>.
117
118=for apidoc Am|void|SvUPGRADE|SV* sv|svtype type
119Used to upgrade an SV to a more complex form. Uses C<sv_upgrade> to
120perform the upgrade if necessary. See C<svtype>.
121
122=cut
123*/
124
463ee0b2 125#define SvANY(sv) (sv)->sv_any
79072805 126#define SvFLAGS(sv) (sv)->sv_flags
aeea060c 127#define SvREFCNT(sv) (sv)->sv_refcnt
a863c7d1 128
4d1ff10f 129#ifdef USE_5005THREADS
dce16143 130
3d35f11b
GS
131# if defined(VMS)
132# define ATOMIC_INC(count) __ATOMIC_INCREMENT_LONG(&count)
133# define ATOMIC_DEC_AND_TEST(res,count) res=(1==__ATOMIC_DECREMENT_LONG(&count))
134 # else
135# ifdef EMULATE_ATOMIC_REFCOUNTS
136 # define ATOMIC_INC(count) STMT_START { \
137 MUTEX_LOCK(&PL_svref_mutex); \
138 ++count; \
139 MUTEX_UNLOCK(&PL_svref_mutex); \
140 } STMT_END
141# define ATOMIC_DEC_AND_TEST(res,count) STMT_START { \
142 MUTEX_LOCK(&PL_svref_mutex); \
143 res = (--count == 0); \
144 MUTEX_UNLOCK(&PL_svref_mutex); \
145 } STMT_END
146# else
147# define ATOMIC_INC(count) atomic_inc(&count)
148# define ATOMIC_DEC_AND_TEST(res,count) (res = atomic_dec_and_test(&count))
149# endif /* EMULATE_ATOMIC_REFCOUNTS */
150# endif /* VMS */
dce16143
MB
151#else
152# define ATOMIC_INC(count) (++count)
83b0a010 153# define ATOMIC_DEC_AND_TEST(res, count) (res = (--count == 0))
4d1ff10f 154#endif /* USE_5005THREADS */
dce16143 155
5b7ea690 156#if defined(__GNUC__) && !defined(__STRICT_ANSI__) && !defined(PERL_GCC_PEDANTIC)
dce16143
MB
157# define SvREFCNT_inc(sv) \
158 ({ \
159 SV *nsv = (SV*)(sv); \
160 if (nsv) \
161 ATOMIC_INC(SvREFCNT(nsv)); \
162 nsv; \
163 })
8990e307 164#else
9ede5bc8 165# ifdef USE_5005THREADS
3d35f11b
GS
166# if defined(VMS) && defined(__ALPHA)
167# define SvREFCNT_inc(sv) \
168 (PL_Sv=(SV*)(sv), (PL_Sv && __ATOMIC_INCREMENT_LONG(&(SvREFCNT(PL_Sv)))), (SV *)PL_Sv)
169# else
170# define SvREFCNT_inc(sv) sv_newref((SV*)sv)
171# endif
a863c7d1 172# else
dce16143 173# define SvREFCNT_inc(sv) \
6b88bc9c 174 ((PL_Sv=(SV*)(sv)), (PL_Sv && ATOMIC_INC(SvREFCNT(PL_Sv))), (SV*)PL_Sv)
a863c7d1 175# endif
8990e307
LW
176#endif
177
91f3b821 178#define SvREFCNT_dec(sv) sv_free((SV*)(sv))
a863c7d1 179
8990e307
LW
180#define SVTYPEMASK 0xff
181#define SvTYPE(sv) ((sv)->sv_flags & SVTYPEMASK)
79072805
LW
182
183#define SvUPGRADE(sv, mt) (SvTYPE(sv) >= mt || sv_upgrade(sv, mt))
184
8990e307
LW
185#define SVs_PADBUSY 0x00000100 /* reserved for tmp or my already */
186#define SVs_PADTMP 0x00000200 /* in use as tmp */
187#define SVs_PADMY 0x00000400 /* in use a "my" variable */
188#define SVs_TEMP 0x00000800 /* string is stealable? */
189#define SVs_OBJECT 0x00001000 /* is "blessed" */
190#define SVs_GMG 0x00002000 /* has magical get method */
191#define SVs_SMG 0x00004000 /* has magical set method */
192#define SVs_RMG 0x00008000 /* has random magical methods */
193
194#define SVf_IOK 0x00010000 /* has valid public integer value */
195#define SVf_NOK 0x00020000 /* has valid public numeric value */
196#define SVf_POK 0x00040000 /* has valid public pointer value */
197#define SVf_ROK 0x00080000 /* has a valid reference pointer */
a0d0e21e 198
748a9306 199#define SVf_FAKE 0x00100000 /* glob or lexical is just a copy */
8990e307 200#define SVf_OOK 0x00200000 /* has valid offset value */
645c22ef
DM
201#define SVf_BREAK 0x00400000 /* refcnt is artificially low - used
202 * by SV's in final arena cleanup */
8990e307
LW
203#define SVf_READONLY 0x00800000 /* may not be modified */
204
a0d0e21e 205
8990e307
LW
206#define SVp_IOK 0x01000000 /* has valid non-public integer value */
207#define SVp_NOK 0x02000000 /* has valid non-public numeric value */
208#define SVp_POK 0x04000000 /* has valid non-public pointer value */
209#define SVp_SCREAM 0x08000000 /* has been studied? */
210
5b7ea690 211#define SVf_UTF8 0x20000000 /* SvPV is UTF-8 encoded */
5bc28da9 212
430eacda 213#define SVf_THINKFIRST (SVf_READONLY|SVf_ROK|SVf_FAKE)
5bc28da9 214
a0d0e21e
LW
215#define SVf_OK (SVf_IOK|SVf_NOK|SVf_POK|SVf_ROK| \
216 SVp_IOK|SVp_NOK|SVp_POK)
217
9e7bc3e8 218#define SVf_AMAGIC 0x10000000 /* has magical overloaded methods */
a0d0e21e 219
c240c76d 220#define PRIVSHIFT 8 /* (SVp_?OK >> PRIVSHIFT) == SVf_?OK */
8990e307
LW
221
222/* Some private flags. */
223
f472eb5c 224/* SVpad_OUR may be set on SVt_PV{NV,MG,GV} types */
032d6771 225#define SVpad_OUR 0x80000000 /* pad name is "our" instead of "my" */
524189f1 226#define SVpad_TYPED 0x40000000 /* Typed Lexical */
032d6771 227
25da4f38
IZ
228#define SVf_IVisUV 0x80000000 /* use XPVUV instead of XPVIV */
229
230#define SVpfm_COMPILED 0x80000000 /* FORMLINE is compiled */
8990e307
LW
231
232#define SVpbm_VALID 0x80000000
bbce6d69 233#define SVpbm_TAIL 0x40000000
8990e307 234
25da4f38
IZ
235#define SVrepl_EVAL 0x40000000 /* Replacement part of s///e */
236
6bfc225d 237#define SVphv_SHAREKEYS 0x20000000 /* keys live on shared string table */
51594c39 238#define SVphv_LAZYDEL 0x40000000 /* entry in xhv_eiter must be deleted */
19692e8d 239#define SVphv_HASKFLAGS 0x80000000 /* keys have flag byte after hash */
bf6bd887 240
810b8aa5
GS
241#define SVprv_WEAKREF 0x80000000 /* Weak reference */
242
ed6116ce
LW
243struct xrv {
244 SV * xrv_rv; /* pointer to another SV */
245};
246
79072805 247struct xpv {
ed6116ce
LW
248 char * xpv_pv; /* pointer to malloced string */
249 STRLEN xpv_cur; /* length of xpv_pv as a C string */
250 STRLEN xpv_len; /* allocated size */
79072805
LW
251};
252
253struct xpviv {
ed6116ce
LW
254 char * xpv_pv; /* pointer to malloced string */
255 STRLEN xpv_cur; /* length of xpv_pv as a C string */
256 STRLEN xpv_len; /* allocated size */
a0d0e21e 257 IV xiv_iv; /* integer value or pv offset */
79072805
LW
258};
259
ff68c719 260struct xpvuv {
261 char * xpv_pv; /* pointer to malloced string */
262 STRLEN xpv_cur; /* length of xpv_pv as a C string */
263 STRLEN xpv_len; /* allocated size */
264 UV xuv_uv; /* unsigned value or pv offset */
265};
266
79072805 267struct xpvnv {
ed6116ce
LW
268 char * xpv_pv; /* pointer to malloced string */
269 STRLEN xpv_cur; /* length of xpv_pv as a C string */
270 STRLEN xpv_len; /* allocated size */
a0d0e21e 271 IV xiv_iv; /* integer value or pv offset */
65202027 272 NV xnv_nv; /* numeric value, if any */
79072805
LW
273};
274
6ee623d5 275/* These structure must match the beginning of struct xpvhv in hv.h. */
79072805 276struct xpvmg {
ed6116ce
LW
277 char * xpv_pv; /* pointer to malloced string */
278 STRLEN xpv_cur; /* length of xpv_pv as a C string */
279 STRLEN xpv_len; /* allocated size */
a0d0e21e 280 IV xiv_iv; /* integer value or pv offset */
65202027 281 NV xnv_nv; /* numeric value, if any */
79072805
LW
282 MAGIC* xmg_magic; /* linked list of magicalness */
283 HV* xmg_stash; /* class package */
284};
285
286struct xpvlv {
ed6116ce
LW
287 char * xpv_pv; /* pointer to malloced string */
288 STRLEN xpv_cur; /* length of xpv_pv as a C string */
289 STRLEN xpv_len; /* allocated size */
a0d0e21e 290 IV xiv_iv; /* integer value or pv offset */
65202027 291 NV xnv_nv; /* numeric value, if any */
79072805
LW
292 MAGIC* xmg_magic; /* linked list of magicalness */
293 HV* xmg_stash; /* class package */
8990e307 294
79072805
LW
295 STRLEN xlv_targoff;
296 STRLEN xlv_targlen;
297 SV* xlv_targ;
73c86719
JH
298 char xlv_type; /* k=keys .=pos x=substr v=vec /=join/re
299 * y=alem/helem/iter t=tie T=tied HE */
79072805
LW
300};
301
302struct xpvgv {
ed6116ce
LW
303 char * xpv_pv; /* pointer to malloced string */
304 STRLEN xpv_cur; /* length of xpv_pv as a C string */
305 STRLEN xpv_len; /* allocated size */
a0d0e21e 306 IV xiv_iv; /* integer value or pv offset */
65202027 307 NV xnv_nv; /* numeric value, if any */
79072805
LW
308 MAGIC* xmg_magic; /* linked list of magicalness */
309 HV* xmg_stash; /* class package */
8990e307 310
79072805
LW
311 GP* xgv_gp;
312 char* xgv_name;
313 STRLEN xgv_namelen;
314 HV* xgv_stash;
a5f75d66 315 U8 xgv_flags;
79072805
LW
316};
317
318struct xpvbm {
ed6116ce
LW
319 char * xpv_pv; /* pointer to malloced string */
320 STRLEN xpv_cur; /* length of xpv_pv as a C string */
321 STRLEN xpv_len; /* allocated size */
a0d0e21e 322 IV xiv_iv; /* integer value or pv offset */
65202027 323 NV xnv_nv; /* numeric value, if any */
79072805
LW
324 MAGIC* xmg_magic; /* linked list of magicalness */
325 HV* xmg_stash; /* class package */
8990e307 326
79072805
LW
327 I32 xbm_useful; /* is this constant pattern being useful? */
328 U16 xbm_previous; /* how many characters in string before rare? */
329 U8 xbm_rare; /* rarest character in string */
330};
331
a77f7f8b 332/* This structure must match XPVCV in cv.h */
44a8e56a 333
77a005ab
MB
334typedef U16 cv_flags_t;
335
79072805 336struct xpvfm {
ed6116ce
LW
337 char * xpv_pv; /* pointer to malloced string */
338 STRLEN xpv_cur; /* length of xpv_pv as a C string */
339 STRLEN xpv_len; /* allocated size */
a0d0e21e 340 IV xiv_iv; /* integer value or pv offset */
65202027 341 NV xnv_nv; /* numeric value, if any */
79072805
LW
342 MAGIC* xmg_magic; /* linked list of magicalness */
343 HV* xmg_stash; /* class package */
8990e307 344
79072805
LW
345 HV * xcv_stash;
346 OP * xcv_start;
347 OP * xcv_root;
acfe0abc 348 void (*xcv_xsub)(pTHX_ CV*);
a0d0e21e
LW
349 ANY xcv_xsubany;
350 GV * xcv_gv;
57843af0 351 char * xcv_file;
b195d487 352 long xcv_depth; /* >= 2 indicates recursive call */
79072805 353 AV * xcv_padlist;
748a9306 354 CV * xcv_outside;
4d1ff10f 355#ifdef USE_5005THREADS
f6d98b14 356 perl_mutex *xcv_mutexp; /* protects xcv_owner */
52e1cb5e 357 struct perl_thread *xcv_owner; /* current owner thread */
4d1ff10f 358#endif /* USE_5005THREADS */
77a005ab 359 cv_flags_t xcv_flags;
d7afa7f5
JH
360 U32 xcv_outside_seq; /* the COP sequence (at the point of our
361 * compilation) in the lexically enclosing
362 * sub */
11a7ac70 363 IV xfm_lines;
79072805
LW
364};
365
8990e307
LW
366struct xpvio {
367 char * xpv_pv; /* pointer to malloced string */
368 STRLEN xpv_cur; /* length of xpv_pv as a C string */
369 STRLEN xpv_len; /* allocated size */
a0d0e21e 370 IV xiv_iv; /* integer value or pv offset */
65202027 371 NV xnv_nv; /* numeric value, if any */
8990e307
LW
372 MAGIC* xmg_magic; /* linked list of magicalness */
373 HV* xmg_stash; /* class package */
374
760ac839
LW
375 PerlIO * xio_ifp; /* ifp and ofp are normally the same */
376 PerlIO * xio_ofp; /* but sockets need separate streams */
4755096e
GS
377 /* Cray addresses everything by word boundaries (64 bits) and
378 * code and data pointers cannot be mixed (which is exactly what
379 * Perl_filter_add() tries to do with the dirp), hence the following
380 * union trick (as suggested by Gurusamy Sarathy).
381 * For further information see Geir Johansen's problem report titled
382 [ID 20000612.002] Perl problem on Cray system
383 * The any pointer (known as IoANY()) will also be a good place
384 * to hang any IO disciplines to.
385 */
386 union {
387 DIR * xiou_dirp; /* for opendir, readdir, etc */
388 void * xiou_any; /* for alignment */
389 } xio_dirpu;
11a7ac70
JH
390 IV xio_lines; /* $. */
391 IV xio_page; /* $% */
392 IV xio_page_len; /* $= */
393 IV xio_lines_left; /* $- */
8990e307
LW
394 char * xio_top_name; /* $^ */
395 GV * xio_top_gv; /* $^ */
396 char * xio_fmt_name; /* $~ */
397 GV * xio_fmt_gv; /* $~ */
398 char * xio_bottom_name;/* $^B */
399 GV * xio_bottom_gv; /* $^B */
400 short xio_subprocess; /* -| or |- */
401 char xio_type;
402 char xio_flags;
403};
4755096e
GS
404#define xio_dirp xio_dirpu.xiou_dirp
405#define xio_any xio_dirpu.xiou_any
8990e307 406
e0c19803
GS
407#define IOf_ARGV 1 /* this fp iterates over ARGV */
408#define IOf_START 2 /* check for null ARGV and substitute '-' */
409#define IOf_FLUSH 4 /* this fp wants a flush after write op */
410#define IOf_DIDTOP 8 /* just did top of form */
411#define IOf_UNTAINT 16 /* consider this fp (and its data) "safe" */
412#define IOf_NOLINE 32 /* slurped a pseudo-line from empty file */
413#define IOf_FAKE_DIRP 64 /* xio_dirp is fake (source filters kludge) */
8990e307 414
ed6116ce
LW
415/* The following macros define implementation-independent predicates on SVs. */
416
954c1994
GS
417/*
418=for apidoc Am|bool|SvNIOK|SV* sv
419Returns a boolean indicating whether the SV contains a number, integer or
420double.
421
422=for apidoc Am|bool|SvNIOKp|SV* sv
423Returns a boolean indicating whether the SV contains a number, integer or
424double. Checks the B<private> setting. Use C<SvNIOK>.
425
426=for apidoc Am|void|SvNIOK_off|SV* sv
427Unsets the NV/IV status of an SV.
428
429=for apidoc Am|bool|SvOK|SV* sv
430Returns a boolean indicating whether the value is an SV.
431
432=for apidoc Am|bool|SvIOKp|SV* sv
433Returns a boolean indicating whether the SV contains an integer. Checks
434the B<private> setting. Use C<SvIOK>.
435
436=for apidoc Am|bool|SvNOKp|SV* sv
437Returns a boolean indicating whether the SV contains a double. Checks the
438B<private> setting. Use C<SvNOK>.
439
440=for apidoc Am|bool|SvPOKp|SV* sv
441Returns a boolean indicating whether the SV contains a character string.
442Checks the B<private> setting. Use C<SvPOK>.
443
444=for apidoc Am|bool|SvIOK|SV* sv
445Returns a boolean indicating whether the SV contains an integer.
446
447=for apidoc Am|void|SvIOK_on|SV* sv
448Tells an SV that it is an integer.
449
450=for apidoc Am|void|SvIOK_off|SV* sv
451Unsets the IV status of an SV.
452
453=for apidoc Am|void|SvIOK_only|SV* sv
454Tells an SV that it is an integer and disables all other OK bits.
455
e331fc52
JH
456=for apidoc Am|void|SvIOK_only_UV|SV* sv
457Tells and SV that it is an unsigned integer and disables all other OK bits.
458
459=for apidoc Am|void|SvIOK_UV|SV* sv
460Returns a boolean indicating whether the SV contains an unsigned integer.
461
28e5dec8
JH
462=for apidoc Am|void|SvUOK|SV* sv
463Returns a boolean indicating whether the SV contains an unsigned integer.
464
e331fc52 465=for apidoc Am|void|SvIOK_notUV|SV* sv
d1be9408 466Returns a boolean indicating whether the SV contains a signed integer.
e331fc52 467
954c1994
GS
468=for apidoc Am|bool|SvNOK|SV* sv
469Returns a boolean indicating whether the SV contains a double.
470
471=for apidoc Am|void|SvNOK_on|SV* sv
472Tells an SV that it is a double.
473
474=for apidoc Am|void|SvNOK_off|SV* sv
475Unsets the NV status of an SV.
476
477=for apidoc Am|void|SvNOK_only|SV* sv
478Tells an SV that it is a double and disables all other OK bits.
479
480=for apidoc Am|bool|SvPOK|SV* sv
481Returns a boolean indicating whether the SV contains a character
482string.
483
484=for apidoc Am|void|SvPOK_on|SV* sv
485Tells an SV that it is a string.
486
487=for apidoc Am|void|SvPOK_off|SV* sv
488Unsets the PV status of an SV.
489
490=for apidoc Am|void|SvPOK_only|SV* sv
491Tells an SV that it is a string and disables all other OK bits.
d5ce4a7c 492Will also turn off the UTF8 status.
954c1994
GS
493
494=for apidoc Am|bool|SvOOK|SV* sv
495Returns a boolean indicating whether the SvIVX is a valid offset value for
496the SvPVX. This hack is used internally to speed up removal of characters
497from the beginning of a SvPV. When SvOOK is true, then the start of the
498allocated string buffer is really (SvPVX - SvIVX).
499
500=for apidoc Am|bool|SvROK|SV* sv
501Tests if the SV is an RV.
502
503=for apidoc Am|void|SvROK_on|SV* sv
504Tells an SV that it is an RV.
505
506=for apidoc Am|void|SvROK_off|SV* sv
507Unsets the RV status of an SV.
508
509=for apidoc Am|SV*|SvRV|SV* sv
510Dereferences an RV to return the SV.
511
512=for apidoc Am|IV|SvIVX|SV* sv
645c22ef
DM
513Returns the raw value in the SV's IV slot, without checks or conversions.
514Only use when you are sure SvIOK is true. See also C<SvIV()>.
954c1994
GS
515
516=for apidoc Am|UV|SvUVX|SV* sv
645c22ef
DM
517Returns the raw value in the SV's UV slot, without checks or conversions.
518Only use when you are sure SvIOK is true. See also C<SvUV()>.
954c1994
GS
519
520=for apidoc Am|NV|SvNVX|SV* sv
645c22ef
DM
521Returns the raw value in the SV's NV slot, without checks or conversions.
522Only use when you are sure SvNOK is true. See also C<SvNV()>.
954c1994
GS
523
524=for apidoc Am|char*|SvPVX|SV* sv
645c22ef 525Returns a pointer to the physical string in the SV. The SV must contain a
954c1994
GS
526string.
527
528=for apidoc Am|STRLEN|SvCUR|SV* sv
529Returns the length of the string which is in the SV. See C<SvLEN>.
530
531=for apidoc Am|STRLEN|SvLEN|SV* sv
659239a4
MG
532Returns the size of the string buffer in the SV, not including any part
533attributable to C<SvOOK>. See C<SvCUR>.
954c1994
GS
534
535=for apidoc Am|char*|SvEND|SV* sv
536Returns a pointer to the last character in the string which is in the SV.
537See C<SvCUR>. Access the character as *(SvEND(sv)).
538
539=for apidoc Am|HV*|SvSTASH|SV* sv
540Returns the stash of the SV.
541
542=for apidoc Am|void|SvCUR_set|SV* sv|STRLEN len
543Set the length of the string which is in the SV. See C<SvCUR>.
544
545=cut
546*/
547
79072805 548#define SvNIOK(sv) (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
748a9306 549#define SvNIOKp(sv) (SvFLAGS(sv) & (SVp_IOK|SVp_NOK))
a0d0e21e 550#define SvNIOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK| \
25da4f38 551 SVp_IOK|SVp_NOK|SVf_IVisUV))
79072805 552
137365c5 553#ifdef __GNUC__
ecde6dd6 554#define assert_not_ROK(sv) ({assert(!SvROK(sv) || !SvRV(sv))}),
137365c5 555#else
ecde6dd6 556#define assert_not_ROK(sv)
137365c5
JH
557#endif
558
463ee0b2 559#define SvOK(sv) (SvFLAGS(sv) & SVf_OK)
ecde6dd6 560#define SvOK_off(sv) (assert_not_ROK(sv) \
137365c5 561 SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC| \
d41ff1b8 562 SVf_IVisUV|SVf_UTF8), \
25da4f38 563 SvOOK_off(sv))
ecde6dd6 564#define SvOK_off_exc_UV(sv) (assert_not_ROK(sv) \
137365c5 565 SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC| \
d41ff1b8 566 SVf_UTF8), \
a0d0e21e 567 SvOOK_off(sv))
79072805 568
8990e307
LW
569#define SvOKp(sv) (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
570#define SvIOKp(sv) (SvFLAGS(sv) & SVp_IOK)
155aba94 571#define SvIOKp_on(sv) ((void)SvOOK_off(sv), SvFLAGS(sv) |= SVp_IOK)
8990e307
LW
572#define SvNOKp(sv) (SvFLAGS(sv) & SVp_NOK)
573#define SvNOKp_on(sv) (SvFLAGS(sv) |= SVp_NOK)
574#define SvPOKp(sv) (SvFLAGS(sv) & SVp_POK)
ecde6dd6 575#define SvPOKp_on(sv) (assert_not_ROK(sv) \
137365c5 576 SvFLAGS(sv) |= SVp_POK)
463ee0b2 577
79072805 578#define SvIOK(sv) (SvFLAGS(sv) & SVf_IOK)
155aba94 579#define SvIOK_on(sv) ((void)SvOOK_off(sv), \
a0d0e21e 580 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
25da4f38 581#define SvIOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV))
155aba94 582#define SvIOK_only(sv) ((void)SvOK_off(sv), \
a0d0e21e 583 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
155aba94 584#define SvIOK_only_UV(sv) ((void)SvOK_off_exc_UV(sv), \
25da4f38 585 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
70401c6b 586
25da4f38
IZ
587#define SvIOK_UV(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV)) \
588 == (SVf_IOK|SVf_IVisUV))
28e5dec8 589#define SvUOK(sv) SvIOK_UV(sv)
25da4f38
IZ
590#define SvIOK_notUV(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV)) \
591 == SVf_IOK)
592
7d7ce6cc 593#define SvVOK(sv) (SvMAGICAL(sv) && mg_find(sv,'V'))
25da4f38
IZ
594#define SvIsUV(sv) (SvFLAGS(sv) & SVf_IVisUV)
595#define SvIsUV_on(sv) (SvFLAGS(sv) |= SVf_IVisUV)
596#define SvIsUV_off(sv) (SvFLAGS(sv) &= ~SVf_IVisUV)
79072805
LW
597
598#define SvNOK(sv) (SvFLAGS(sv) & SVf_NOK)
a0d0e21e 599#define SvNOK_on(sv) (SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
8990e307 600#define SvNOK_off(sv) (SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
155aba94 601#define SvNOK_only(sv) ((void)SvOK_off(sv), \
a0d0e21e 602 SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
79072805 603
914184e1
JH
604/*
605=for apidoc Am|void|SvUTF8|SV* sv
606Returns a boolean indicating whether the SV contains UTF-8 encoded data.
607
608=for apidoc Am|void|SvUTF8_on|SV *sv
d5ce4a7c
GA
609Turn on the UTF8 status of an SV (the data is not changed, just the flag).
610Do not use frivolously.
914184e1
JH
611
612=for apidoc Am|void|SvUTF8_off|SV *sv
613Unsets the UTF8 status of an SV.
614
615=for apidoc Am|void|SvPOK_only_UTF8|SV* sv
d5ce4a7c
GA
616Tells an SV that it is a string and disables all other OK bits,
617and leaves the UTF8 status as it was.
f1a1024e 618
914184e1
JH
619=cut
620 */
621
a7cb1f99
GS
622#define SvUTF8(sv) (SvFLAGS(sv) & SVf_UTF8)
623#define SvUTF8_on(sv) (SvFLAGS(sv) |= (SVf_UTF8))
624#define SvUTF8_off(sv) (SvFLAGS(sv) &= ~(SVf_UTF8))
5bc28da9 625
79072805 626#define SvPOK(sv) (SvFLAGS(sv) & SVf_POK)
ecde6dd6 627#define SvPOK_on(sv) (assert_not_ROK(sv) \
137365c5 628 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
8990e307 629#define SvPOK_off(sv) (SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
ecde6dd6 630#define SvPOK_only(sv) (assert_not_ROK(sv) \
137365c5 631 SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC| \
d41ff1b8
GS
632 SVf_IVisUV|SVf_UTF8), \
633 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
ecde6dd6 634#define SvPOK_only_UTF8(sv) (assert_not_ROK(sv) \
137365c5 635 SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC| \
d41ff1b8 636 SVf_IVisUV), \
a0d0e21e 637 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
79072805
LW
638
639#define SvOOK(sv) (SvFLAGS(sv) & SVf_OOK)
155aba94 640#define SvOOK_on(sv) ((void)SvIOK_off(sv), SvFLAGS(sv) |= SVf_OOK)
79072805 641#define SvOOK_off(sv) (SvOOK(sv) && sv_backoff(sv))
79072805 642
a0d0e21e
LW
643#define SvFAKE(sv) (SvFLAGS(sv) & SVf_FAKE)
644#define SvFAKE_on(sv) (SvFLAGS(sv) |= SVf_FAKE)
645#define SvFAKE_off(sv) (SvFLAGS(sv) &= ~SVf_FAKE)
646
ed6116ce 647#define SvROK(sv) (SvFLAGS(sv) & SVf_ROK)
a0d0e21e 648#define SvROK_on(sv) (SvFLAGS(sv) |= SVf_ROK)
a0d0e21e 649#define SvROK_off(sv) (SvFLAGS(sv) &= ~(SVf_ROK|SVf_AMAGIC))
79072805 650
8990e307
LW
651#define SvMAGICAL(sv) (SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG))
652#define SvMAGICAL_on(sv) (SvFLAGS(sv) |= (SVs_GMG|SVs_SMG|SVs_RMG))
653#define SvMAGICAL_off(sv) (SvFLAGS(sv) &= ~(SVs_GMG|SVs_SMG|SVs_RMG))
79072805 654
8990e307
LW
655#define SvGMAGICAL(sv) (SvFLAGS(sv) & SVs_GMG)
656#define SvGMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_GMG)
657#define SvGMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_GMG)
ed6116ce 658
8990e307
LW
659#define SvSMAGICAL(sv) (SvFLAGS(sv) & SVs_SMG)
660#define SvSMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_SMG)
661#define SvSMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_SMG)
ed6116ce 662
8990e307
LW
663#define SvRMAGICAL(sv) (SvFLAGS(sv) & SVs_RMG)
664#define SvRMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_RMG)
665#define SvRMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_RMG)
ed6116ce 666
9e7bc3e8
JD
667#define SvAMAGIC(sv) (SvFLAGS(sv) & SVf_AMAGIC)
668#define SvAMAGIC_on(sv) (SvFLAGS(sv) |= SVf_AMAGIC)
669#define SvAMAGIC_off(sv) (SvFLAGS(sv) &= ~SVf_AMAGIC)
a0d0e21e 670
8cf8f3d1 671#define SvGAMAGIC(sv) (SvFLAGS(sv) & (SVs_GMG|SVf_AMAGIC))
1426bbf4 672
a0d0e21e
LW
673/*
674#define Gv_AMG(stash) \
675 (HV_AMAGICmb(stash) && \
676 ((!HV_AMAGICbad(stash) && HV_AMAGIC(stash)) || Gv_AMupdate(stash)))
677*/
3280af22 678#define Gv_AMG(stash) (PL_amagic_generation && Gv_AMupdate(stash))
a0d0e21e 679
810b8aa5
GS
680#define SvWEAKREF(sv) ((SvFLAGS(sv) & (SVf_ROK|SVprv_WEAKREF)) \
681 == (SVf_ROK|SVprv_WEAKREF))
682#define SvWEAKREF_on(sv) (SvFLAGS(sv) |= (SVf_ROK|SVprv_WEAKREF))
683#define SvWEAKREF_off(sv) (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_WEAKREF))
684
a0d0e21e 685#define SvTHINKFIRST(sv) (SvFLAGS(sv) & SVf_THINKFIRST)
ed6116ce 686
8990e307 687#define SvPADBUSY(sv) (SvFLAGS(sv) & SVs_PADBUSY)
ed6116ce 688
8990e307
LW
689#define SvPADTMP(sv) (SvFLAGS(sv) & SVs_PADTMP)
690#define SvPADTMP_on(sv) (SvFLAGS(sv) |= SVs_PADTMP|SVs_PADBUSY)
691#define SvPADTMP_off(sv) (SvFLAGS(sv) &= ~SVs_PADTMP)
ed6116ce 692
8990e307
LW
693#define SvPADMY(sv) (SvFLAGS(sv) & SVs_PADMY)
694#define SvPADMY_on(sv) (SvFLAGS(sv) |= SVs_PADMY|SVs_PADBUSY)
ed6116ce 695
8990e307
LW
696#define SvTEMP(sv) (SvFLAGS(sv) & SVs_TEMP)
697#define SvTEMP_on(sv) (SvFLAGS(sv) |= SVs_TEMP)
698#define SvTEMP_off(sv) (SvFLAGS(sv) &= ~SVs_TEMP)
79072805 699
8990e307
LW
700#define SvOBJECT(sv) (SvFLAGS(sv) & SVs_OBJECT)
701#define SvOBJECT_on(sv) (SvFLAGS(sv) |= SVs_OBJECT)
702#define SvOBJECT_off(sv) (SvFLAGS(sv) &= ~SVs_OBJECT)
79072805 703
8990e307
LW
704#define SvREADONLY(sv) (SvFLAGS(sv) & SVf_READONLY)
705#define SvREADONLY_on(sv) (SvFLAGS(sv) |= SVf_READONLY)
706#define SvREADONLY_off(sv) (SvFLAGS(sv) &= ~SVf_READONLY)
79072805 707
8990e307
LW
708#define SvSCREAM(sv) (SvFLAGS(sv) & SVp_SCREAM)
709#define SvSCREAM_on(sv) (SvFLAGS(sv) |= SVp_SCREAM)
710#define SvSCREAM_off(sv) (SvFLAGS(sv) &= ~SVp_SCREAM)
79072805 711
8990e307
LW
712#define SvCOMPILED(sv) (SvFLAGS(sv) & SVpfm_COMPILED)
713#define SvCOMPILED_on(sv) (SvFLAGS(sv) |= SVpfm_COMPILED)
714#define SvCOMPILED_off(sv) (SvFLAGS(sv) &= ~SVpfm_COMPILED)
79072805 715
25da4f38
IZ
716#define SvEVALED(sv) (SvFLAGS(sv) & SVrepl_EVAL)
717#define SvEVALED_on(sv) (SvFLAGS(sv) |= SVrepl_EVAL)
718#define SvEVALED_off(sv) (SvFLAGS(sv) &= ~SVrepl_EVAL)
719
8990e307
LW
720#define SvTAIL(sv) (SvFLAGS(sv) & SVpbm_TAIL)
721#define SvTAIL_on(sv) (SvFLAGS(sv) |= SVpbm_TAIL)
722#define SvTAIL_off(sv) (SvFLAGS(sv) &= ~SVpbm_TAIL)
723
8990e307
LW
724#define SvVALID(sv) (SvFLAGS(sv) & SVpbm_VALID)
725#define SvVALID_on(sv) (SvFLAGS(sv) |= SVpbm_VALID)
726#define SvVALID_off(sv) (SvFLAGS(sv) &= ~SVpbm_VALID)
727
1cc8b4c5
AB
728#ifdef USE_ITHREADS
729/* The following uses the FAKE flag to show that a regex pointer is infact
8ca6097c 730 its own offset in the regexpad for ithreads */
1cc8b4c5
AB
731#define SvREPADTMP(sv) (SvFLAGS(sv) & SVf_FAKE)
732#define SvREPADTMP_on(sv) (SvFLAGS(sv) |= SVf_FAKE)
733#define SvREPADTMP_off(sv) (SvFLAGS(sv) &= ~SVf_FAKE)
734#endif
735
ed6116ce
LW
736#define SvRV(sv) ((XRV*) SvANY(sv))->xrv_rv
737#define SvRVx(sv) SvRV(sv)
738
463ee0b2
LW
739#define SvIVX(sv) ((XPVIV*) SvANY(sv))->xiv_iv
740#define SvIVXx(sv) SvIVX(sv)
ff68c719 741#define SvUVX(sv) ((XPVUV*) SvANY(sv))->xuv_uv
742#define SvUVXx(sv) SvUVX(sv)
463ee0b2
LW
743#define SvNVX(sv) ((XPVNV*)SvANY(sv))->xnv_nv
744#define SvNVXx(sv) SvNVX(sv)
745#define SvPVX(sv) ((XPV*) SvANY(sv))->xpv_pv
746#define SvPVXx(sv) SvPVX(sv)
79072805 747#define SvCUR(sv) ((XPV*) SvANY(sv))->xpv_cur
79072805
LW
748#define SvLEN(sv) ((XPV*) SvANY(sv))->xpv_len
749#define SvLENx(sv) SvLEN(sv)
750#define SvEND(sv)(((XPV*) SvANY(sv))->xpv_pv + ((XPV*)SvANY(sv))->xpv_cur)
6b88bc9c 751#define SvENDx(sv) ((PL_Sv = (sv)), SvEND(PL_Sv))
79072805
LW
752#define SvMAGIC(sv) ((XPVMG*) SvANY(sv))->xmg_magic
753#define SvSTASH(sv) ((XPVMG*) SvANY(sv))->xmg_stash
754
28e5dec8
JH
755/* Ask a scalar nicely to try to become an IV, if possible.
756 Not guaranteed to stay returning void */
757/* Macro won't actually call sv_2iv if already IOK */
758#define SvIV_please(sv) \
759 STMT_START {if (!SvIOKp(sv) && (SvNOK(sv) || SvPOK(sv))) \
760 (void) SvIV(sv); } STMT_END
79072805 761#define SvIV_set(sv, val) \
80b92232 762 STMT_START { assert(SvTYPE(sv) == SVt_IV || SvTYPE(sv) >= SVt_PVIV); \
763 (((XPVIV*) SvANY(sv))->xiv_iv = val); } STMT_END
79072805 764#define SvNV_set(sv, val) \
80b92232 765 STMT_START { assert(SvTYPE(sv) == SVt_NV || SvTYPE(sv) >= SVt_PVNV); \
766 (((XPVNV*) SvANY(sv))->xnv_nv = val); } STMT_END
79072805 767#define SvPV_set(sv, val) \
80b92232 768 STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
769 (((XPV*) SvANY(sv))->xpv_pv = val); } STMT_END
79072805 770#define SvCUR_set(sv, val) \
80b92232 771 STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
772 (((XPV*) SvANY(sv))->xpv_cur = val); } STMT_END
79072805 773#define SvLEN_set(sv, val) \
80b92232 774 STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
775 (((XPV*) SvANY(sv))->xpv_len = val); } STMT_END
79072805 776#define SvEND_set(sv, val) \
80b92232 777 STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
778 (((XPV*) SvANY(sv))->xpv_cur = val - SvPVX(sv)); } STMT_END
79072805
LW
779
780#define BmRARE(sv) ((XPVBM*) SvANY(sv))->xbm_rare
781#define BmUSEFUL(sv) ((XPVBM*) SvANY(sv))->xbm_useful
782#define BmPREVIOUS(sv) ((XPVBM*) SvANY(sv))->xbm_previous
783
784#define FmLINES(sv) ((XPVFM*) SvANY(sv))->xfm_lines
785
786#define LvTYPE(sv) ((XPVLV*) SvANY(sv))->xlv_type
787#define LvTARG(sv) ((XPVLV*) SvANY(sv))->xlv_targ
788#define LvTARGOFF(sv) ((XPVLV*) SvANY(sv))->xlv_targoff
789#define LvTARGLEN(sv) ((XPVLV*) SvANY(sv))->xlv_targlen
790
8990e307
LW
791#define IoIFP(sv) ((XPVIO*) SvANY(sv))->xio_ifp
792#define IoOFP(sv) ((XPVIO*) SvANY(sv))->xio_ofp
793#define IoDIRP(sv) ((XPVIO*) SvANY(sv))->xio_dirp
4755096e 794#define IoANY(sv) ((XPVIO*) SvANY(sv))->xio_any
8990e307
LW
795#define IoLINES(sv) ((XPVIO*) SvANY(sv))->xio_lines
796#define IoPAGE(sv) ((XPVIO*) SvANY(sv))->xio_page
797#define IoPAGE_LEN(sv) ((XPVIO*) SvANY(sv))->xio_page_len
798#define IoLINES_LEFT(sv)((XPVIO*) SvANY(sv))->xio_lines_left
799#define IoTOP_NAME(sv) ((XPVIO*) SvANY(sv))->xio_top_name
800#define IoTOP_GV(sv) ((XPVIO*) SvANY(sv))->xio_top_gv
801#define IoFMT_NAME(sv) ((XPVIO*) SvANY(sv))->xio_fmt_name
802#define IoFMT_GV(sv) ((XPVIO*) SvANY(sv))->xio_fmt_gv
803#define IoBOTTOM_NAME(sv)((XPVIO*) SvANY(sv))->xio_bottom_name
804#define IoBOTTOM_GV(sv) ((XPVIO*) SvANY(sv))->xio_bottom_gv
805#define IoSUBPROCESS(sv)((XPVIO*) SvANY(sv))->xio_subprocess
806#define IoTYPE(sv) ((XPVIO*) SvANY(sv))->xio_type
807#define IoFLAGS(sv) ((XPVIO*) SvANY(sv))->xio_flags
808
50952442
JH
809/* IoTYPE(sv) is a single character telling the type of I/O connection. */
810#define IoTYPE_RDONLY '<'
811#define IoTYPE_WRONLY '>'
812#define IoTYPE_RDWR '+'
813#define IoTYPE_APPEND 'a'
814#define IoTYPE_PIPE '|'
815#define IoTYPE_STD '-' /* stdin or stdout */
816#define IoTYPE_SOCKET 's'
817#define IoTYPE_CLOSED ' '
03fcf2fc
GS
818
819/*
954c1994
GS
820=for apidoc Am|bool|SvTAINTED|SV* sv
821Checks to see if an SV is tainted. Returns TRUE if it is, FALSE if
822not.
823
824=for apidoc Am|void|SvTAINTED_on|SV* sv
825Marks an SV as tainted.
826
827=for apidoc Am|void|SvTAINTED_off|SV* sv
828Untaints an SV. Be I<very> careful with this routine, as it short-circuits
829some of Perl's fundamental security features. XS module authors should not
830use this function unless they fully understand all the implications of
831unconditionally untainting the value. Untainting should be done in the
832standard perl fashion, via a carefully crafted regexp, rather than directly
833untainting variables.
834
835=for apidoc Am|void|SvTAINT|SV* sv
836Taints an SV if tainting is enabled
837
838=cut
839*/
840
bbce6d69 841#define SvTAINTED(sv) (SvMAGICAL(sv) && sv_tainted(sv))
3280af22
NIS
842#define SvTAINTED_on(sv) STMT_START{ if(PL_tainting){sv_taint(sv);} }STMT_END
843#define SvTAINTED_off(sv) STMT_START{ if(PL_tainting){sv_untaint(sv);} }STMT_END
bbce6d69 844
c6ee37c5
MB
845#define SvTAINT(sv) \
846 STMT_START { \
3280af22 847 if (PL_tainting) { \
3280af22 848 if (PL_tainted) \
c6ee37c5
MB
849 SvTAINTED_on(sv); \
850 } \
851 } STMT_END
79072805 852
954c1994
GS
853/*
854=for apidoc Am|char*|SvPV_force|SV* sv|STRLEN len
5f08bad4
ST
855Like C<SvPV> but will force the SV into containing just a string
856(C<SvPOK_only>). You want force if you are going to update the C<SvPVX>
857directly.
954c1994 858
645c22ef 859=for apidoc Am|char*|SvPV_force_nomg|SV* sv|STRLEN len
5f08bad4
ST
860Like C<SvPV> but will force the SV into containing just a string
861(C<SvPOK_only>). You want force if you are going to update the C<SvPVX>
862directly. Doesn't process magic.
645c22ef 863
954c1994 864=for apidoc Am|char*|SvPV|SV* sv|STRLEN len
5f08bad4
ST
865Returns a pointer to the string in the SV, or a stringified form of
866the SV if the SV does not contain a string. The SV may cache the
867stringified version becoming C<SvPOK>. Handles 'get' magic. See also
645c22ef
DM
868C<SvPVx> for a version which guarantees to evaluate sv only once.
869
870=for apidoc Am|char*|SvPVx|SV* sv|STRLEN len
871A version of C<SvPV> which guarantees to evaluate sv only once.
954c1994
GS
872
873=for apidoc Am|char*|SvPV_nolen|SV* sv
5f08bad4
ST
874Returns a pointer to the string in the SV, or a stringified form of
875the SV if the SV does not contain a string. The SV may cache the
876stringified form becoming C<SvPOK>. Handles 'get' magic.
954c1994
GS
877
878=for apidoc Am|IV|SvIV|SV* sv
645c22ef
DM
879Coerces the given SV to an integer and returns it. See C<SvIVx> for a
880version which guarantees to evaluate sv only once.
881
882=for apidoc Am|IV|SvIVx|SV* sv
883Coerces the given SV to an integer and returns it. Guarantees to evaluate
d1be9408 884sv only once. Use the more efficient C<SvIV> otherwise.
954c1994
GS
885
886=for apidoc Am|NV|SvNV|SV* sv
645c22ef
DM
887Coerce the given SV to a double and return it. See C<SvNVx> for a version
888which guarantees to evaluate sv only once.
889
890=for apidoc Am|NV|SvNVx|SV* sv
891Coerces the given SV to a double and returns it. Guarantees to evaluate
d1be9408 892sv only once. Use the more efficient C<SvNV> otherwise.
954c1994
GS
893
894=for apidoc Am|UV|SvUV|SV* sv
645c22ef
DM
895Coerces the given SV to an unsigned integer and returns it. See C<SvUVx>
896for a version which guarantees to evaluate sv only once.
897
898=for apidoc Am|UV|SvUVx|SV* sv
899Coerces the given SV to an unsigned integer and returns it. Guarantees to
d1be9408 900evaluate sv only once. Use the more efficient C<SvUV> otherwise.
954c1994
GS
901
902=for apidoc Am|bool|SvTRUE|SV* sv
903Returns a boolean indicating whether Perl would evaluate the SV as true or
904false, defined or undefined. Does not handle 'get' magic.
905
645c22ef 906=for apidoc Am|char*|SvPVutf8_force|SV* sv|STRLEN len
b70b15d2 907Like C<SvPV_force>, but converts sv to utf8 first if necessary.
645c22ef
DM
908
909=for apidoc Am|char*|SvPVutf8|SV* sv|STRLEN len
b70b15d2 910Like C<SvPV>, but converts sv to utf8 first if necessary.
645c22ef 911
b70b15d2
TJ
912=for apidoc Am|char*|SvPVutf8_nolen|SV* sv
913Like C<SvPV_nolen>, but converts sv to utf8 first if necessary.
645c22ef
DM
914
915=for apidoc Am|char*|SvPVbyte_force|SV* sv|STRLEN len
916Like C<SvPV_force>, but converts sv to byte representation first if necessary.
917
918=for apidoc Am|char*|SvPVbyte|SV* sv|STRLEN len
919Like C<SvPV>, but converts sv to byte representation first if necessary.
920
b70b15d2 921=for apidoc Am|char*|SvPVbyte_nolen|SV* sv
645c22ef
DM
922Like C<SvPV_nolen>, but converts sv to byte representation first if necessary.
923
924=for apidoc Am|char*|SvPVutf8x_force|SV* sv|STRLEN len
b70b15d2 925Like C<SvPV_force>, but converts sv to utf8 first if necessary.
d1be9408 926Guarantees to evaluate sv only once; use the more efficient C<SvPVutf8_force>
645c22ef
DM
927otherwise.
928
929=for apidoc Am|char*|SvPVutf8x|SV* sv|STRLEN len
b70b15d2 930Like C<SvPV>, but converts sv to utf8 first if necessary.
d1be9408 931Guarantees to evaluate sv only once; use the more efficient C<SvPVutf8>
645c22ef
DM
932otherwise.
933
934=for apidoc Am|char*|SvPVbytex_force|SV* sv|STRLEN len
935Like C<SvPV_force>, but converts sv to byte representation first if necessary.
d1be9408 936Guarantees to evaluate sv only once; use the more efficient C<SvPVbyte_force>
645c22ef
DM
937otherwise.
938
939=for apidoc Am|char*|SvPVbytex|SV* sv|STRLEN len
940Like C<SvPV>, but converts sv to byte representation first if necessary.
d1be9408 941Guarantees to evaluate sv only once; use the more efficient C<SvPVbyte>
645c22ef
DM
942otherwise.
943
fd4f854d
NC
944=for apidoc Am|bool|SvIsCOW|SV* sv
945Returns a boolean indicating whether the SV is Copy-On-Write. (either shared
946hash key scalars, or full Copy On Write scalars if 5.9.0 is configured for
947COW)
948
949=for apidoc Am|bool|SvIsCOW_shared_hash|SV* sv
950Returns a boolean indicating whether the SV is Copy-On-Write shared hash key
951scalar.
645c22ef 952
954c1994
GS
953=cut
954*/
955
25da4f38 956/* Let us hope that bitmaps for UV and IV are the same */
463ee0b2 957#define SvIV(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv))
ff68c719 958#define SvUV(sv) (SvIOK(sv) ? SvUVX(sv) : sv_2uv(sv))
463ee0b2 959#define SvNV(sv) (SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv))
79072805 960
baca2b92 961/* ----*/
8d6d96c1 962
8d6d96c1
HS
963#define SvPV(sv, lp) SvPV_flags(sv, lp, SV_GMAGIC)
964
8d6d96c1
HS
965#define SvPV_flags(sv, lp, flags) \
966 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
967 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv_flags(sv, &lp, flags))
79072805 968
8d6d96c1 969#define SvPV_force(sv, lp) SvPV_force_flags(sv, lp, SV_GMAGIC)
baca2b92 970
8d6d96c1
HS
971#define SvPV_force_nomg(sv, lp) SvPV_force_flags(sv, lp, 0)
972
8d6d96c1 973#define SvPV_force_flags(sv, lp, flags) \
ff68c719 974 ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
8d6d96c1 975 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvn_force_flags(sv, &lp, flags))
a0d0e21e 976
1fa8b10d 977#define SvPV_nolen(sv) \
5bc28da9
NIS
978 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
979 ? SvPVX(sv) : sv_2pv_nolen(sv))
70401c6b 980
baca2b92 981#define SvPV_nomg(sv, lp) SvPV_flags(sv, lp, 0)
5bc28da9 982
baca2b92 983/* ----*/
70401c6b 984
5bc28da9
NIS
985#define SvPVutf8(sv, lp) \
986 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK|SVf_UTF8) \
987 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvutf8(sv, &lp))
70401c6b 988
5bc28da9 989#define SvPVutf8_force(sv, lp) \
70401c6b 990 ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == (SVf_POK|SVf_UTF8) \
5bc28da9
NIS
991 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvutf8n_force(sv, &lp))
992
baca2b92 993
5bc28da9
NIS
994#define SvPVutf8_nolen(sv) \
995 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK|SVf_UTF8)\
996 ? SvPVX(sv) : sv_2pvutf8_nolen(sv))
70401c6b 997
baca2b92
DM
998/* ----*/
999
5bc28da9
NIS
1000#define SvPVbyte(sv, lp) \
1001 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK) \
1002 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvbyte(sv, &lp))
70401c6b 1003
5bc28da9
NIS
1004#define SvPVbyte_force(sv, lp) \
1005 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST)) == (SVf_POK) \
1006 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvbyte_force(sv, &lp))
1007
5bc28da9
NIS
1008#define SvPVbyte_nolen(sv) \
1009 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK)\
1010 ? SvPVX(sv) : sv_2pvbyte_nolen(sv))
70401c6b 1011
1fa8b10d 1012
baca2b92
DM
1013
1014/* define FOOx(): idempotent versions of FOO(). If possible, use a local
1015 * var to evaluate the arg once; failing that, use a global if possible;
1016 * failing that, call a function to do the work
1017 */
1018
1019#define SvPVx_force(sv, lp) sv_pvn_force(sv, &lp)
1020#define SvPVutf8x_force(sv, lp) sv_pvutf8n_force(sv, &lp)
1021#define SvPVbytex_force(sv, lp) sv_pvbyten_force(sv, &lp)
1022
5b7ea690 1023#if defined(__GNUC__) && !defined(__STRICT_ANSI__) && !defined(PERL_GCC_PEDANTIC)
baca2b92 1024
a80f87c4
GS
1025# define SvIVx(sv) ({SV *nsv = (SV*)(sv); SvIV(nsv); })
1026# define SvUVx(sv) ({SV *nsv = (SV*)(sv); SvUV(nsv); })
1027# define SvNVx(sv) ({SV *nsv = (SV*)(sv); SvNV(nsv); })
1028# define SvPVx(sv, lp) ({SV *nsv = (sv); SvPV(nsv, lp); })
5bc28da9
NIS
1029# define SvPVutf8x(sv, lp) ({SV *nsv = (sv); SvPVutf8(nsv, lp); })
1030# define SvPVbytex(sv, lp) ({SV *nsv = (sv); SvPVbyte(nsv, lp); })
a80f87c4 1031# define SvTRUE(sv) ( \
8990e307
LW
1032 !sv \
1033 ? 0 \
1034 : SvPOK(sv) \
a80f87c4
GS
1035 ? (({XPV *nxpv = (XPV*)SvANY(sv); \
1036 nxpv && \
c2f1de04 1037 (nxpv->xpv_cur > 1 || \
a80f87c4 1038 (nxpv->xpv_cur && *nxpv->xpv_pv != '0')); }) \
79072805
LW
1039 ? 1 \
1040 : 0) \
1041 : \
1042 SvIOK(sv) \
463ee0b2 1043 ? SvIVX(sv) != 0 \
79072805 1044 : SvNOK(sv) \
463ee0b2
LW
1045 ? SvNVX(sv) != 0.0 \
1046 : sv_2bool(sv) )
a80f87c4 1047# define SvTRUEx(sv) ({SV *nsv = (sv); SvTRUE(nsv); })
baca2b92 1048
a80f87c4 1049#else /* __GNUC__ */
baca2b92
DM
1050
1051# ifdef USE_5005THREADS
1052# define SvIVx(sv) sv_iv(sv)
1053# define SvUVx(sv) sv_uv(sv)
1054# define SvNVx(sv) sv_nv(sv)
1055# define SvPVx(sv, lp) sv_pvn(sv, &lp)
1056# define SvPVutf8x(sv, lp) sv_pvutf8n(sv, &lp)
1057# define SvPVbytex(sv, lp) sv_pvbyten(sv, &lp)
1058# define SvTRUE(sv) SvTRUEx(sv)
1059# define SvTRUEx(sv) sv_true(sv)
1060
1061# else /* USE_5005THREADS */
1062
a80f87c4
GS
1063/* These inlined macros use globals, which will require a thread
1064 * declaration in user code, so we avoid them under threads */
1065
baca2b92
DM
1066# define SvIVx(sv) ((PL_Sv = (sv)), SvIV(PL_Sv))
1067# define SvUVx(sv) ((PL_Sv = (sv)), SvUV(PL_Sv))
1068# define SvNVx(sv) ((PL_Sv = (sv)), SvNV(PL_Sv))
1069# define SvPVx(sv, lp) ((PL_Sv = (sv)), SvPV(PL_Sv, lp))
1070# define SvPVutf8x(sv, lp) ((PL_Sv = (sv)), SvPVutf8(PL_Sv, lp))
1071# define SvPVbytex(sv, lp) ((PL_Sv = (sv)), SvPVbyte(PL_Sv, lp))
1072# define SvTRUE(sv) ( \
a80f87c4
GS
1073 !sv \
1074 ? 0 \
1075 : SvPOK(sv) \
6b88bc9c 1076 ? ((PL_Xpv = (XPV*)SvANY(sv)) && \
c2f1de04 1077 (PL_Xpv->xpv_cur > 1 || \
6b88bc9c 1078 (PL_Xpv->xpv_cur && *PL_Xpv->xpv_pv != '0')) \
a80f87c4
GS
1079 ? 1 \
1080 : 0) \
1081 : \
1082 SvIOK(sv) \
1083 ? SvIVX(sv) != 0 \
1084 : SvNOK(sv) \
1085 ? SvNVX(sv) != 0.0 \
1086 : sv_2bool(sv) )
baca2b92
DM
1087# define SvTRUEx(sv) ((PL_Sv = (sv)), SvTRUE(PL_Sv))
1088# endif /* USE_5005THREADS */
1089#endif /* __GNU__ */
1090
1091
1092/* flag values for sv_*_flags functions */
1093#define SV_IMMEDIATE_UNREF 1
1094#define SV_GMAGIC 2
6563884d
JH
1095#define SV_COW_DROP_PV 4 /* Unused in Perl 5.8.x */
1096#define SV_UTF8_NO_ENCODING 8
baca2b92
DM
1097
1098/* all these 'functions' are now just macros */
1099
1100#define sv_pv(sv) SvPV_nolen(sv)
1101#define sv_pvutf8(sv) SvPVutf8_nolen(sv)
1102#define sv_pvbyte(sv) SvPVbyte_nolen(sv)
1103
1104#define sv_pvn_force_nomg(sv, lp) sv_pvn_force_flags(sv, lp, 0)
1105#define sv_utf8_upgrade_nomg(sv) sv_utf8_upgrade_flags(sv, 0)
1106#define sv_catpvn_nomg(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, 0)
1107#define sv_setsv(dsv, ssv) sv_setsv_flags(dsv, ssv, SV_GMAGIC)
1108#define sv_setsv_nomg(dsv, ssv) sv_setsv_flags(dsv, ssv, 0)
1109#define sv_catsv(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC)
1110#define sv_catsv_nomg(dsv, ssv) sv_catsv_flags(dsv, ssv, 0)
1111#define sv_catpvn(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC)
1112#define sv_2pv(sv, lp) sv_2pv_flags(sv, lp, SV_GMAGIC)
1113#define sv_2pv_nomg(sv, lp) sv_2pv_flags(sv, lp, 0)
1114#define sv_pvn_force(sv, lp) sv_pvn_force_flags(sv, lp, SV_GMAGIC)
1115#define sv_utf8_upgrade(sv) sv_utf8_upgrade_flags(sv, SV_GMAGIC)
1116
5835a535
JH
1117/* Should be named SvCatPVN_utf8_upgrade? */
1118#define sv_catpvn_utf8_upgrade(dsv, sstr, slen, nsv) \
1119 STMT_START { \
1120 if (!(nsv)) \
1121 nsv = sv_2mortal(newSVpvn(sstr, slen)); \
1122 else \
1123 sv_setpvn(nsv, sstr, slen); \
1124 SvUTF8_off(nsv); \
1125 sv_utf8_upgrade(nsv); \
1126 sv_catsv(dsv, nsv); \
1127 } STMT_END
79072805 1128
954c1994
GS
1129/*
1130=for apidoc Am|SV*|newRV_inc|SV* sv
1131
1132Creates an RV wrapper for an SV. The reference count for the original SV is
1133incremented.
1134
1135=cut
1136*/
1137
5f05dabc 1138#define newRV_inc(sv) newRV(sv)
5f05dabc 1139
ef50df4b 1140/* the following macros update any magic values this sv is associated with */
79072805 1141
954c1994 1142/*
ccfc67b7
JH
1143=head1 Magical Functions
1144
954c1994
GS
1145=for apidoc Am|void|SvGETMAGIC|SV* sv
1146Invokes C<mg_get> on an SV if it has 'get' magic. This macro evaluates its
1147argument more than once.
1148
1149=for apidoc Am|void|SvSETMAGIC|SV* sv
1150Invokes C<mg_set> on an SV if it has 'set' magic. This macro evaluates its
1151argument more than once.
1152
1153=for apidoc Am|void|SvSetSV|SV* dsb|SV* ssv
1154Calls C<sv_setsv> if dsv is not the same as ssv. May evaluate arguments
1155more than once.
1156
1157=for apidoc Am|void|SvSetSV_nosteal|SV* dsv|SV* ssv
1158Calls a non-destructive version of C<sv_setsv> if dsv is not the same as
1159ssv. May evaluate arguments more than once.
1160
645c22ef
DM
1161=for apidoc Am|void|SvSetMagicSV|SV* dsb|SV* ssv
1162Like C<SvSetSV>, but does any set magic required afterwards.
1163
1164=for apidoc Am|void|SvSetMagicSV_nosteal|SV* dsv|SV* ssv
1165Like C<SvSetMagicSV>, but does any set magic required afterwards.
1166
68795e93
NIS
1167=for apidoc Am|void|SvSHARE|SV* sv
1168Arranges for sv to be shared between threads if a suitable module
1169has been loaded.
1170
1171=for apidoc Am|void|SvLOCK|SV* sv
1172Arranges for a mutual exclusion lock to be obtained on sv if a suitable module
1173has been loaded.
1174
1175=for apidoc Am|void|SvUNLOCK|SV* sv
1176Releases a mutual exclusion lock on sv if a suitable module
1177has been loaded.
1178
ccfc67b7
JH
1179=head1 SV Manipulation Functions
1180
679ac26e 1181=for apidoc Am|char *|SvGROW|SV* sv|STRLEN len
954c1994
GS
1182Expands the character buffer in the SV so that it has room for the
1183indicated number of bytes (remember to reserve space for an extra trailing
8cf8f3d1 1184NUL character). Calls C<sv_grow> to perform the expansion if necessary.
954c1994
GS
1185Returns a pointer to the character buffer.
1186
1187=cut
1188*/
1189
68795e93
NIS
1190#define SvSHARE(sv) CALL_FPTR(PL_sharehook)(aTHX_ sv)
1191#define SvLOCK(sv) CALL_FPTR(PL_lockhook)(aTHX_ sv)
1192#define SvUNLOCK(sv) CALL_FPTR(PL_unlockhook)(aTHX_ sv)
1193
189b2af5
GS
1194#define SvGETMAGIC(x) STMT_START { if (SvGMAGICAL(x)) mg_get(x); } STMT_END
1195#define SvSETMAGIC(x) STMT_START { if (SvSMAGICAL(x)) mg_set(x); } STMT_END
79072805 1196
54310121 1197#define SvSetSV_and(dst,src,finally) \
189b2af5 1198 STMT_START { \
54310121 1199 if ((dst) != (src)) { \
1200 sv_setsv(dst, src); \
1201 finally; \
189b2af5
GS
1202 } \
1203 } STMT_END
54310121 1204#define SvSetSV_nosteal_and(dst,src,finally) \
189b2af5 1205 STMT_START { \
8ebc5c01 1206 if ((dst) != (src)) { \
1207 U32 tMpF = SvFLAGS(src) & SVs_TEMP; \
1208 SvTEMP_off(src); \
1209 sv_setsv(dst, src); \
1210 SvFLAGS(src) |= tMpF; \
54310121 1211 finally; \
189b2af5
GS
1212 } \
1213 } STMT_END
79072805 1214
54310121 1215#define SvSetSV(dst,src) \
c9d716f7 1216 SvSetSV_and(dst,src,/*nothing*/;)
54310121 1217#define SvSetSV_nosteal(dst,src) \
c9d716f7 1218 SvSetSV_nosteal_and(dst,src,/*nothing*/;)
54310121 1219
1220#define SvSetMagicSV(dst,src) \
1221 SvSetSV_and(dst,src,SvSETMAGIC(dst))
1222#define SvSetMagicSV_nosteal(dst,src) \
1223 SvSetSV_nosteal_and(dst,src,SvSETMAGIC(dst))
1224
5835a535 1225
1045810a 1226#if !defined(SKIP_DEBUGGING)
79072805 1227#define SvPEEK(sv) sv_peek(sv)
3967c732
JD
1228#else
1229#define SvPEEK(sv) ""
1230#endif
79072805 1231
3280af22 1232#define SvIMMORTAL(sv) ((sv)==&PL_sv_undef || (sv)==&PL_sv_yes || (sv)==&PL_sv_no)
36477c24 1233
3280af22 1234#define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
54310121 1235
79072805
LW
1236#define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
1237
933fea7f
GS
1238#define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
1239#define Sv_Grow sv_grow
3d35f11b 1240
a0739874
DM
1241#define CLONEf_COPY_STACKS 1
1242#define CLONEf_KEEP_PTR_TABLE 2
c43294b8 1243#define CLONEf_CLONE_HOST 4
457d3ae8 1244#define CLONEf_JOIN_IN 8
a0739874 1245
8cf8f3d1 1246struct clone_params {
d2d73c3e
AB
1247 AV* stashes;
1248 UV flags;
59b40662 1249 PerlInterpreter *proto_perl;
8cf8f3d1 1250};
b4cc2f95
JH
1251
1252#define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) sv_force_normal(sv)