3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006 by Larry Wall and others
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.
12 #undef sv_flags /* Convex has this in <signal.h> for sigvec() */
18 =for apidoc AmU||svtype
19 An enum of flags for Perl types. These are found in the file B<sv.h>
20 in the C<svtype> enum. Test these flags with the C<SvTYPE> macro.
22 =for apidoc AmU||SVt_PV
23 Pointer type flag for scalars. See C<svtype>.
25 =for apidoc AmU||SVt_IV
26 Integer type flag for scalars. See C<svtype>.
28 =for apidoc AmU||SVt_NV
29 Double type flag for scalars. See C<svtype>.
31 =for apidoc AmU||SVt_PVMG
32 Type flag for blessed scalars. See C<svtype>.
34 =for apidoc AmU||SVt_PVAV
35 Type flag for arrays. See C<svtype>.
37 =for apidoc AmU||SVt_PVHV
38 Type flag for hashes. See C<svtype>.
40 =for apidoc AmU||SVt_PVCV
41 Type flag for code refs. See C<svtype>.
63 SVt_LAST /* keep last in enum. used to size arrays */
67 #define PTE_SVSLOT SVt_LAST
69 #if defined(PERL_IN_HV_C) || defined(PERL_IN_XS_APITEST)
70 #define HE_SVSLOT (SVt_LAST + 1)
72 #define PERL_ARENA_ROOTS_SIZE (SVt_LAST + 2)
74 /* Using C's structural equivalence to help emulate C++ inheritance here... */
76 struct STRUCT_SV { /* struct sv { */
77 void* sv_any; /* pointer to something */
78 U32 sv_refcnt; /* how many references to us */
79 U32 sv_flags; /* what we are */
83 XPVGV* sv_any; /* pointer to something */
84 U32 sv_refcnt; /* how many references to us */
85 U32 sv_flags; /* what we are */
89 XPVCV* sv_any; /* pointer to something */
90 U32 sv_refcnt; /* how many references to us */
91 U32 sv_flags; /* what we are */
95 XPVAV* sv_any; /* pointer to something */
96 U32 sv_refcnt; /* how many references to us */
97 U32 sv_flags; /* what we are */
101 XPVHV* sv_any; /* pointer to something */
102 U32 sv_refcnt; /* how many references to us */
103 U32 sv_flags; /* what we are */
107 XPVIO* sv_any; /* pointer to something */
108 U32 sv_refcnt; /* how many references to us */
109 U32 sv_flags; /* what we are */
113 =head1 SV Manipulation Functions
115 =for apidoc Am|U32|SvREFCNT|SV* sv
116 Returns the value of the object's reference count.
118 =for apidoc Am|SV*|SvREFCNT_inc|SV* sv
119 Increments the reference count of the given SV.
121 All of the following SvREFCNT_inc* macros are optimized versions of
122 SvREFCNT_inc, and can be replaced with SvREFCNT_inc.
124 =for apidoc Am|SV*|SvREFCNT_inc_NN|SV* sv
125 Same as SvREFCNT_inc, but can only be used if you know I<sv>
126 is not NULL. Since we don't have to check the NULLness, it's faster
129 =for apidoc Am|SV*|SvREFCNT_inc_void|SV* sv
130 Same as SvREFCNT_inc, but can only be used if you don't need the
131 return value. The macro doesn't need to return a meaningful value.
133 =for apidoc Am|SV*|SvREFCNT_inc_void_NN|SV* sv
134 Same as SvREFCNT_inc, but can only be used if you don't need the return
135 value, and you know that I<sv> is not NULL. The macro doesn't need
136 to return a meaningful value, or check for NULLness, so it's smaller
139 =for apidoc Am|SV*|SvREFCNT_inc_simple|SV* sv
140 Same as SvREFCNT_inc, but can only be used with simple variables, not
141 expressions or pointer dereferences. Since we don't have to store a
142 temporary value, it's faster.
144 =for apidoc Am|SV*|SvREFCNT_inc_simple_NN|SV* sv
145 Same as SvREFCNT_inc_simple, but can only be used if you know I<sv>
146 is not NULL. Since we don't have to check the NULLness, it's faster
149 =for apidoc Am|SV*|SvREFCNT_inc_simple_void|SV* sv
150 Same as SvREFCNT_inc_simple, but can only be used if you don't need the
151 return value. The macro doesn't need to return a meaningful value.
153 =for apidoc Am|SV*|SvREFCNT_inc|SV* sv
154 Increments the reference count of the given SV.
156 =for apidoc Am|void|SvREFCNT_dec|SV* sv
157 Decrements the reference count of the given SV.
159 =for apidoc Am|svtype|SvTYPE|SV* sv
160 Returns the type of the SV. See C<svtype>.
162 =for apidoc Am|void|SvUPGRADE|SV* sv|svtype type
163 Used to upgrade an SV to a more complex form. Uses C<sv_upgrade> to
164 perform the upgrade if necessary. See C<svtype>.
169 #define SvANY(sv) (sv)->sv_any
170 #define SvFLAGS(sv) (sv)->sv_flags
171 #define SvREFCNT(sv) (sv)->sv_refcnt
173 #ifdef USE_5005THREADS
176 # define ATOMIC_INC(count) __ATOMIC_INCREMENT_LONG(&count)
177 # define ATOMIC_DEC_AND_TEST(res,count) res=(1==__ATOMIC_DECREMENT_LONG(&count))
179 # ifdef EMULATE_ATOMIC_REFCOUNTS
180 # define ATOMIC_INC(count) STMT_START { \
181 MUTEX_LOCK(&PL_svref_mutex); \
183 MUTEX_UNLOCK(&PL_svref_mutex); \
185 # define ATOMIC_DEC_AND_TEST(res,count) STMT_START { \
186 MUTEX_LOCK(&PL_svref_mutex); \
187 res = (--count == 0); \
188 MUTEX_UNLOCK(&PL_svref_mutex); \
191 # define ATOMIC_INC(count) atomic_inc(&count)
192 # define ATOMIC_DEC_AND_TEST(res,count) (res = atomic_dec_and_test(&count))
193 # endif /* EMULATE_ATOMIC_REFCOUNTS */
196 # define ATOMIC_INC(count) (++count)
197 # define ATOMIC_DEC_AND_TEST(res, count) (res = (--count == 0))
198 #endif /* USE_5005THREADS */
200 #if defined(__GNUC__) && !defined(__STRICT_ANSI__) && !defined(PERL_GCC_PEDANTIC)
201 # define SvREFCNT_inc(sv) \
203 SV * const _sv = (SV*)(sv); \
205 ATOMIC_INC(SvREFCNT(_sv)); \
208 # define SvREFCNT_inc_simple(sv) \
214 # define SvREFCNT_inc_NN(sv) \
216 SV * const _sv = (SV*)(sv); \
220 # define SvREFCNT_inc_void(sv) \
222 SV * const _sv = (SV*)(sv); \
224 (void)(SvREFCNT(_sv)++); \
227 # ifdef USE_5005THREADS
228 # if defined(VMS) && defined(__ALPHA)
229 # define SvREFCNT_inc(sv) \
230 (PL_Sv=(SV*)(sv), (PL_Sv && __ATOMIC_INCREMENT_LONG(&(SvREFCNT(PL_Sv)))), (SV *)PL_Sv)
232 # define SvREFCNT_inc(sv) sv_newref((SV*)sv)
234 # define SvREFCNT_inc_simple(sv) SvREFCNT_inc(sv)
235 # define SvREFCNT_inc_NN(sv) SvREFCNT_inc(sv)
236 # define SvREFCNT_inc_void(sv) SvREFCNT_inc(sv)
238 # define SvREFCNT_inc(sv) \
239 ((PL_Sv=(SV*)(sv)) ? (ATOMIC_INC(SvREFCNT(PL_Sv)), (SV*)PL_Sv) : NULL)
240 # define SvREFCNT_inc_simple(sv) \
241 ((sv) ? (ATOMIC_INC(SvREFCNT(sv)),(SV*)(sv)) : NULL)
242 # define SvREFCNT_inc_NN(sv) \
243 (PL_Sv=(SV*)(sv),ATOMIC_INC(SvREFCNT(PL_Sv)),PL_Sv)
244 # define SvREFCNT_inc_void(sv) \
245 (void)((PL_Sv=(SV*)(sv)) ? ATOMIC_INC(SvREFCNT(PL_Sv)) : 0)
249 /* These guys don't need the curly blocks */
250 #define SvREFCNT_inc_simple_void(sv) if (sv) (SvREFCNT(sv)++);
251 #define SvREFCNT_inc_simple_NN(sv) (++(SvREFCNT(sv)),(SV*)(sv))
252 #define SvREFCNT_inc_void_NN(sv) (void)(++SvREFCNT((SV*)(sv)))
253 #define SvREFCNT_inc_simple_void_NN(sv) (void)(++SvREFCNT((SV*)(sv)))
255 #define SvREFCNT_dec(sv) sv_free((SV*)(sv))
257 #define SVTYPEMASK 0xff
258 #define SvTYPE(sv) ((sv)->sv_flags & SVTYPEMASK)
260 /* Sadly there are some parts of the core that have pointers to already-freed
261 SV heads, and rely on being able to tell that they are now free. So mark
262 them all by using a consistent macro. */
263 #define SvIS_FREED(sv) ((sv)->sv_flags == SVTYPEMASK)
265 #define SvUPGRADE(sv, mt) (SvTYPE(sv) >= mt || sv_upgrade(sv, mt))
267 #define SVs_PADBUSY 0x00000100 /* reserved for tmp or my already */
268 #define SVs_PADTMP 0x00000200 /* in use as tmp */
269 #define SVs_PADMY 0x00000400 /* in use a "my" variable */
270 #define SVs_TEMP 0x00000800 /* string is stealable? */
271 #define SVs_OBJECT 0x00001000 /* is "blessed" */
272 #define SVs_GMG 0x00002000 /* has magical get method */
273 #define SVs_SMG 0x00004000 /* has magical set method */
274 #define SVs_RMG 0x00008000 /* has random magical methods */
276 #define SVf_IOK 0x00010000 /* has valid public integer value */
277 #define SVf_NOK 0x00020000 /* has valid public numeric value */
278 #define SVf_POK 0x00040000 /* has valid public pointer value */
279 #define SVf_ROK 0x00080000 /* has a valid reference pointer */
281 #define SVf_FAKE 0x00100000 /* glob or lexical is just a copy */
282 #define SVf_OOK 0x00200000 /* has valid offset value */
283 #define SVf_BREAK 0x00400000 /* refcnt is artificially low - used
284 * by SV's in final arena cleanup */
285 #define SVf_READONLY 0x00800000 /* may not be modified */
288 #define SVp_IOK 0x01000000 /* has valid non-public integer value */
289 #define SVp_NOK 0x02000000 /* has valid non-public numeric value */
290 #define SVp_POK 0x04000000 /* has valid non-public pointer value */
291 #define SVp_SCREAM 0x08000000 /* has been studied? */
293 #define SVf_UTF8 0x20000000 /* SvPV is UTF-8 encoded */
294 /* Ensure this value does not clash with the GV_ADD* flags in gv.h */
296 #define SVf_THINKFIRST (SVf_READONLY|SVf_ROK|SVf_FAKE)
298 #define SVf_OK (SVf_IOK|SVf_NOK|SVf_POK|SVf_ROK| \
299 SVp_IOK|SVp_NOK|SVp_POK)
301 #define SVf_AMAGIC 0x10000000 /* has magical overloaded methods */
303 #define PRIVSHIFT 8 /* (SVp_?OK >> PRIVSHIFT) == SVf_?OK */
305 /* Some private flags. */
307 /* SVpad_OUR may be set on SVt_PV{NV,MG,GV} types */
308 #define SVpad_OUR 0x80000000 /* pad name is "our" instead of "my" */
309 #define SVpad_TYPED 0x40000000 /* Typed Lexical */
311 #define SVf_IVisUV 0x80000000 /* use XPVUV instead of XPVIV */
313 #define SVpfm_COMPILED 0x80000000 /* FORMLINE is compiled */
315 #define SVpbm_VALID 0x80000000
316 #define SVpbm_TAIL 0x40000000
318 #define SVrepl_EVAL 0x40000000 /* Replacement part of s///e */
320 #define SVphv_CLONEABLE 0x08000000 /* for stashes: clone its objects */
321 #define SVphv_REHASH 0x10000000 /* HV is recalculating hash values */
322 #define SVphv_SHAREKEYS 0x20000000 /* keys live on shared string table */
323 #define SVphv_LAZYDEL 0x40000000 /* entry in xhv_eiter must be deleted */
324 #define SVphv_HASKFLAGS 0x80000000 /* keys have flag byte after hash */
326 #define SVprv_WEAKREF 0x80000000 /* Weak reference */
329 IV xiv_iv; /* integer value or pv offset */
333 NV xnv_nv; /* numeric value, if any */
337 SV * xrv_rv; /* pointer to another SV */
341 char * xpv_pv; /* pointer to malloced string */
342 STRLEN xpv_cur; /* length of xpv_pv as a C string */
343 STRLEN xpv_len; /* allocated size */
346 typedef struct xpv xpv_allocated;
349 char * xpv_pv; /* pointer to malloced string */
350 STRLEN xpv_cur; /* length of xpv_pv as a C string */
351 STRLEN xpv_len; /* allocated size */
352 IV xiv_iv; /* integer value or pv offset */
355 typedef struct xpviv xpviv_allocated;
358 char * xpv_pv; /* pointer to malloced string */
359 STRLEN xpv_cur; /* length of xpv_pv as a C string */
360 STRLEN xpv_len; /* allocated size */
361 UV xuv_uv; /* unsigned value or pv offset */
365 char * xpv_pv; /* pointer to malloced string */
366 STRLEN xpv_cur; /* length of xpv_pv as a C string */
367 STRLEN xpv_len; /* allocated size */
368 IV xiv_iv; /* integer value or pv offset */
369 NV xnv_nv; /* numeric value, if any */
372 /* These structure must match the beginning of struct xpvhv in hv.h. */
374 char * xpv_pv; /* pointer to malloced string */
375 STRLEN xpv_cur; /* length of xpv_pv as a C string */
376 STRLEN xpv_len; /* allocated size */
377 IV xiv_iv; /* integer value or pv offset */
378 NV xnv_nv; /* numeric value, if any */
379 MAGIC* xmg_magic; /* linked list of magicalness */
380 HV* xmg_stash; /* class package */
384 char * xpv_pv; /* pointer to malloced string */
385 STRLEN xpv_cur; /* length of xpv_pv as a C string */
386 STRLEN xpv_len; /* allocated size */
387 IV xiv_iv; /* integer value or pv offset */
388 NV xnv_nv; /* numeric value, if any */
389 MAGIC* xmg_magic; /* linked list of magicalness */
390 HV* xmg_stash; /* class package */
395 char xlv_type; /* k=keys .=pos x=substr v=vec /=join/re
396 * y=alem/helem/iter t=tie T=tied HE */
400 char * xpv_pv; /* pointer to malloced string */
401 STRLEN xpv_cur; /* length of xpv_pv as a C string */
402 STRLEN xpv_len; /* allocated size */
403 IV xiv_iv; /* integer value or pv offset */
404 NV xnv_nv; /* numeric value, if any */
405 MAGIC* xmg_magic; /* linked list of magicalness */
406 HV* xmg_stash; /* class package */
416 char * xpv_pv; /* pointer to malloced string */
417 STRLEN xpv_cur; /* length of xpv_pv as a C string */
418 STRLEN xpv_len; /* allocated size */
419 IV xiv_iv; /* integer value or pv offset */
420 NV xnv_nv; /* numeric value, if any */
421 MAGIC* xmg_magic; /* linked list of magicalness */
422 HV* xmg_stash; /* class package */
424 I32 xbm_useful; /* is this constant pattern being useful? */
425 U16 xbm_previous; /* how many characters in string before rare? */
426 U8 xbm_rare; /* rarest character in string */
429 /* This structure must match XPVCV in cv.h */
431 typedef U16 cv_flags_t;
434 char * xpv_pv; /* pointer to malloced string */
435 STRLEN xpv_cur; /* length of xpv_pv as a C string */
436 STRLEN xpv_len; /* allocated size */
437 IV xiv_iv; /* integer value or pv offset */
438 NV xnv_nv; /* numeric value, if any */
439 MAGIC* xmg_magic; /* linked list of magicalness */
440 HV* xmg_stash; /* class package */
445 void (*xcv_xsub)(pTHX_ CV*);
449 long xcv_depth; /* >= 2 indicates recursive call */
452 #ifdef USE_5005THREADS
453 perl_mutex *xcv_mutexp; /* protects xcv_owner */
454 struct perl_thread *xcv_owner; /* current owner thread */
455 #endif /* USE_5005THREADS */
456 cv_flags_t xcv_flags;
457 U32 xcv_outside_seq; /* the COP sequence (at the point of our
458 * compilation) in the lexically enclosing
464 char * xpv_pv; /* pointer to malloced string */
465 STRLEN xpv_cur; /* length of xpv_pv as a C string */
466 STRLEN xpv_len; /* allocated size */
467 IV xiv_iv; /* integer value or pv offset */
468 NV xnv_nv; /* numeric value, if any */
469 MAGIC* xmg_magic; /* linked list of magicalness */
470 HV* xmg_stash; /* class package */
472 PerlIO * xio_ifp; /* ifp and ofp are normally the same */
473 PerlIO * xio_ofp; /* but sockets need separate streams */
474 /* Cray addresses everything by word boundaries (64 bits) and
475 * code and data pointers cannot be mixed (which is exactly what
476 * Perl_filter_add() tries to do with the dirp), hence the following
477 * union trick (as suggested by Gurusamy Sarathy).
478 * For further information see Geir Johansen's problem report titled
479 [ID 20000612.002] Perl problem on Cray system
480 * The any pointer (known as IoANY()) will also be a good place
481 * to hang any IO disciplines to.
484 DIR * xiou_dirp; /* for opendir, readdir, etc */
485 void * xiou_any; /* for alignment */
487 IV xio_lines; /* $. */
488 IV xio_page; /* $% */
489 IV xio_page_len; /* $= */
490 IV xio_lines_left; /* $- */
491 char * xio_top_name; /* $^ */
492 GV * xio_top_gv; /* $^ */
493 char * xio_fmt_name; /* $~ */
494 GV * xio_fmt_gv; /* $~ */
495 char * xio_bottom_name;/* $^B */
496 GV * xio_bottom_gv; /* $^B */
497 short xio_subprocess; /* -| or |- */
501 #define xio_dirp xio_dirpu.xiou_dirp
502 #define xio_any xio_dirpu.xiou_any
504 #define IOf_ARGV 1 /* this fp iterates over ARGV */
505 #define IOf_START 2 /* check for null ARGV and substitute '-' */
506 #define IOf_FLUSH 4 /* this fp wants a flush after write op */
507 #define IOf_DIDTOP 8 /* just did top of form */
508 #define IOf_UNTAINT 16 /* consider this fp (and its data) "safe" */
509 #define IOf_NOLINE 32 /* slurped a pseudo-line from empty file */
510 #define IOf_FAKE_DIRP 64 /* xio_dirp is fake (source filters kludge) */
512 /* The following macros define implementation-independent predicates on SVs. */
515 =for apidoc Am|bool|SvNIOK|SV* sv
516 Returns a boolean indicating whether the SV contains a number, integer or
519 =for apidoc Am|bool|SvNIOKp|SV* sv
520 Returns a boolean indicating whether the SV contains a number, integer or
521 double. Checks the B<private> setting. Use C<SvNIOK>.
523 =for apidoc Am|void|SvNIOK_off|SV* sv
524 Unsets the NV/IV status of an SV.
526 =for apidoc Am|bool|SvOK|SV* sv
527 Returns a boolean indicating whether the value is an SV. It also tells
528 whether the value is defined or not.
530 =for apidoc Am|bool|SvIOKp|SV* sv
531 Returns a boolean indicating whether the SV contains an integer. Checks
532 the B<private> setting. Use C<SvIOK>.
534 =for apidoc Am|bool|SvNOKp|SV* sv
535 Returns a boolean indicating whether the SV contains a double. Checks the
536 B<private> setting. Use C<SvNOK>.
538 =for apidoc Am|bool|SvPOKp|SV* sv
539 Returns a boolean indicating whether the SV contains a character string.
540 Checks the B<private> setting. Use C<SvPOK>.
542 =for apidoc Am|bool|SvIOK|SV* sv
543 Returns a boolean indicating whether the SV contains an integer.
545 =for apidoc Am|void|SvIOK_on|SV* sv
546 Tells an SV that it is an integer.
548 =for apidoc Am|void|SvIOK_off|SV* sv
549 Unsets the IV status of an SV.
551 =for apidoc Am|void|SvIOK_only|SV* sv
552 Tells an SV that it is an integer and disables all other OK bits.
554 =for apidoc Am|void|SvIOK_only_UV|SV* sv
555 Tells and SV that it is an unsigned integer and disables all other OK bits.
557 =for apidoc Am|bool|SvIOK_UV|SV* sv
558 Returns a boolean indicating whether the SV contains an unsigned integer.
560 =for apidoc Am|void|SvUOK|SV* sv
561 Returns a boolean indicating whether the SV contains an unsigned integer.
563 =for apidoc Am|bool|SvIOK_notUV|SV* sv
564 Returns a boolean indicating whether the SV contains a signed integer.
566 =for apidoc Am|bool|SvNOK|SV* sv
567 Returns a boolean indicating whether the SV contains a double.
569 =for apidoc Am|void|SvNOK_on|SV* sv
570 Tells an SV that it is a double.
572 =for apidoc Am|void|SvNOK_off|SV* sv
573 Unsets the NV status of an SV.
575 =for apidoc Am|void|SvNOK_only|SV* sv
576 Tells an SV that it is a double and disables all other OK bits.
578 =for apidoc Am|bool|SvPOK|SV* sv
579 Returns a boolean indicating whether the SV contains a character
582 =for apidoc Am|void|SvPOK_on|SV* sv
583 Tells an SV that it is a string.
585 =for apidoc Am|void|SvPOK_off|SV* sv
586 Unsets the PV status of an SV.
588 =for apidoc Am|void|SvPOK_only|SV* sv
589 Tells an SV that it is a string and disables all other OK bits.
590 Will also turn off the UTF-8 status.
592 =for apidoc Am|bool|SvOOK|SV* sv
593 Returns a boolean indicating whether the SvIVX is a valid offset value for
594 the SvPVX. This hack is used internally to speed up removal of characters
595 from the beginning of a SvPV. When SvOOK is true, then the start of the
596 allocated string buffer is really (SvPVX - SvIVX).
598 =for apidoc Am|bool|SvROK|SV* sv
599 Tests if the SV is an RV.
601 =for apidoc Am|void|SvROK_on|SV* sv
602 Tells an SV that it is an RV.
604 =for apidoc Am|void|SvROK_off|SV* sv
605 Unsets the RV status of an SV.
607 =for apidoc Am|SV*|SvRV|SV* sv
608 Dereferences an RV to return the SV.
610 =for apidoc Am|IV|SvIVX|SV* sv
611 Returns the raw value in the SV's IV slot, without checks or conversions.
612 Only use when you are sure SvIOK is true. See also C<SvIV()>.
614 =for apidoc Am|UV|SvUVX|SV* sv
615 Returns the raw value in the SV's UV slot, without checks or conversions.
616 Only use when you are sure SvIOK is true. See also C<SvUV()>.
618 =for apidoc Am|NV|SvNVX|SV* sv
619 Returns the raw value in the SV's NV slot, without checks or conversions.
620 Only use when you are sure SvNOK is true. See also C<SvNV()>.
622 =for apidoc Am|char*|SvPVX|SV* sv
623 Returns a pointer to the physical string in the SV. The SV must contain a
626 =for apidoc Am|STRLEN|SvCUR|SV* sv
627 Returns the length of the string which is in the SV. See C<SvLEN>.
629 =for apidoc Am|STRLEN|SvLEN|SV* sv
630 Returns the size of the string buffer in the SV, not including any part
631 attributable to C<SvOOK>. See C<SvCUR>.
633 =for apidoc Am|char*|SvEND|SV* sv
634 Returns a pointer to the last character in the string which is in the SV.
635 See C<SvCUR>. Access the character as *(SvEND(sv)).
637 =for apidoc Am|HV*|SvSTASH|SV* sv
638 Returns the stash of the SV.
640 =for apidoc Am|void|SvIV_set|SV* sv|IV val
641 Set the value of the IV pointer in sv to val. It is possible to perform
642 the same function of this macro with an lvalue assignment to C<SvIVX>.
643 With future Perls, however, it will be more efficient to use
644 C<SvIV_set> instead of the lvalue assignment to C<SvIVX>.
646 =for apidoc Am|void|SvNV_set|SV* sv|NV val
647 Set the value of the NV pointer in sv to val. See C<SvIV_set>.
649 =for apidoc Am|void|SvPV_set|SV* sv|char* val
650 Set the value of the PV pointer in sv to val. See C<SvIV_set>.
652 =for apidoc Am|void|SvUV_set|SV* sv|UV val
653 Set the value of the UV pointer in sv to val. See C<SvIV_set>.
655 =for apidoc Am|void|SvRV_set|SV* sv|SV* val
656 Set the value of the RV pointer in sv to val. See C<SvIV_set>.
658 =for apidoc Am|void|SvMAGIC_set|SV* sv|MAGIC* val
659 Set the value of the MAGIC pointer in sv to val. See C<SvIV_set>.
661 =for apidoc Am|void|SvSTASH_set|SV* sv|STASH* val
662 Set the value of the STASH pointer in sv to val. See C<SvIV_set>.
664 =for apidoc Am|void|SvCUR_set|SV* sv|STRLEN len
665 Set the current length of the string which is in the SV. See C<SvCUR>
668 =for apidoc Am|void|SvLEN_set|SV* sv|STRLEN len
669 Set the actual length of the string which is in the SV. See C<SvIV_set>.
674 #define SvNIOK(sv) (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
675 #define SvNIOKp(sv) (SvFLAGS(sv) & (SVp_IOK|SVp_NOK))
676 #define SvNIOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK| \
677 SVp_IOK|SVp_NOK|SVf_IVisUV))
679 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
680 #define assert_not_ROK(sv) ({assert(!SvROK(sv) || !SvRV(sv));}),
682 #define assert_not_ROK(sv)
685 #define SvOK(sv) (SvFLAGS(sv) & SVf_OK)
686 #define SvOK_off(sv) (assert_not_ROK(sv) \
687 SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC| \
688 SVf_IVisUV|SVf_UTF8), \
690 #define SvOK_off_exc_UV(sv) (assert_not_ROK(sv) \
691 SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC| \
695 #define SvOKp(sv) (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
696 #define SvIOKp(sv) (SvFLAGS(sv) & SVp_IOK)
697 #define SvIOKp_on(sv) ((void)SvOOK_off(sv), SvFLAGS(sv) |= SVp_IOK)
698 #define SvNOKp(sv) (SvFLAGS(sv) & SVp_NOK)
699 #define SvNOKp_on(sv) (SvFLAGS(sv) |= SVp_NOK)
700 #define SvPOKp(sv) (SvFLAGS(sv) & SVp_POK)
701 #define SvPOKp_on(sv) (assert_not_ROK(sv) \
702 SvFLAGS(sv) |= SVp_POK)
704 #define SvIOK(sv) (SvFLAGS(sv) & SVf_IOK)
705 #define SvIOK_on(sv) ((void)SvOOK_off(sv), \
706 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
707 #define SvIOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV))
708 #define SvIOK_only(sv) (SvOK_off(sv), \
709 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
710 #define SvIOK_only_UV(sv) (SvOK_off_exc_UV(sv), \
711 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
713 #define SvIOK_UV(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV)) \
714 == (SVf_IOK|SVf_IVisUV))
715 #define SvUOK(sv) SvIOK_UV(sv)
716 #define SvIOK_notUV(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV)) \
719 #define SvVOK(sv) (SvMAGICAL(sv) && mg_find(sv,'V'))
720 #define SvIsUV(sv) (SvFLAGS(sv) & SVf_IVisUV)
721 #define SvIsUV_on(sv) (SvFLAGS(sv) |= SVf_IVisUV)
722 #define SvIsUV_off(sv) (SvFLAGS(sv) &= ~SVf_IVisUV)
724 #define SvNOK(sv) (SvFLAGS(sv) & SVf_NOK)
725 #define SvNOK_on(sv) (SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
726 #define SvNOK_off(sv) (SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
727 #define SvNOK_only(sv) (SvOK_off(sv), \
728 SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
731 =for apidoc Am|bool|SvUTF8|SV* sv
732 Returns a boolean indicating whether the SV contains UTF-8 encoded data.
734 =for apidoc Am|void|SvUTF8_on|SV *sv
735 Turn on the UTF-8 status of an SV (the data is not changed, just the flag).
736 Do not use frivolously.
738 =for apidoc Am|void|SvUTF8_off|SV *sv
739 Unsets the UTF-8 status of an SV.
741 =for apidoc Am|void|SvPOK_only_UTF8|SV* sv
742 Tells an SV that it is a string and disables all other OK bits,
743 and leaves the UTF-8 status as it was.
748 /* Ensure the return value of this macro does not clash with the GV_ADD* flags
750 #define SvUTF8(sv) (SvFLAGS(sv) & SVf_UTF8)
751 #define SvUTF8_on(sv) (SvFLAGS(sv) |= (SVf_UTF8))
752 #define SvUTF8_off(sv) (SvFLAGS(sv) &= ~(SVf_UTF8))
754 #define SvPOK(sv) (SvFLAGS(sv) & SVf_POK)
755 #define SvPOK_on(sv) (assert_not_ROK(sv) \
756 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
757 #define SvPOK_off(sv) (SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
758 #define SvPOK_only(sv) (assert_not_ROK(sv) \
759 SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC| \
760 SVf_IVisUV|SVf_UTF8), \
761 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
762 #define SvPOK_only_UTF8(sv) (assert_not_ROK(sv) \
763 SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC| \
765 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
767 #define SvOOK(sv) (SvFLAGS(sv) & SVf_OOK)
768 #define SvOOK_on(sv) ((void)SvIOK_off(sv), SvFLAGS(sv) |= SVf_OOK)
769 #define SvOOK_off(sv) ((void)(SvOOK(sv) && sv_backoff(sv)))
771 #define SvFAKE(sv) (SvFLAGS(sv) & SVf_FAKE)
772 #define SvFAKE_on(sv) (SvFLAGS(sv) |= SVf_FAKE)
773 #define SvFAKE_off(sv) (SvFLAGS(sv) &= ~SVf_FAKE)
775 #define SvROK(sv) (SvFLAGS(sv) & SVf_ROK)
776 #define SvROK_on(sv) (SvFLAGS(sv) |= SVf_ROK)
777 #define SvROK_off(sv) (SvFLAGS(sv) &= ~(SVf_ROK|SVf_AMAGIC))
779 #define SvMAGICAL(sv) (SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG))
780 #define SvMAGICAL_on(sv) (SvFLAGS(sv) |= (SVs_GMG|SVs_SMG|SVs_RMG))
781 #define SvMAGICAL_off(sv) (SvFLAGS(sv) &= ~(SVs_GMG|SVs_SMG|SVs_RMG))
783 #define SvGMAGICAL(sv) (SvFLAGS(sv) & SVs_GMG)
784 #define SvGMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_GMG)
785 #define SvGMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_GMG)
787 #define SvSMAGICAL(sv) (SvFLAGS(sv) & SVs_SMG)
788 #define SvSMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_SMG)
789 #define SvSMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_SMG)
791 #define SvRMAGICAL(sv) (SvFLAGS(sv) & SVs_RMG)
792 #define SvRMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_RMG)
793 #define SvRMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_RMG)
795 #define SvAMAGIC(sv) (SvFLAGS(sv) & SVf_AMAGIC)
796 #define SvAMAGIC_on(sv) (SvFLAGS(sv) |= SVf_AMAGIC)
797 #define SvAMAGIC_off(sv) (SvFLAGS(sv) &= ~SVf_AMAGIC)
799 #define SvGAMAGIC(sv) (SvFLAGS(sv) & (SVs_GMG|SVf_AMAGIC))
802 #define Gv_AMG(stash) \
803 (HV_AMAGICmb(stash) && \
804 ((!HV_AMAGICbad(stash) && HV_AMAGIC(stash)) || Gv_AMupdate(stash)))
806 #define Gv_AMG(stash) (PL_amagic_generation && Gv_AMupdate(stash))
808 #define SvWEAKREF(sv) ((SvFLAGS(sv) & (SVf_ROK|SVprv_WEAKREF)) \
809 == (SVf_ROK|SVprv_WEAKREF))
810 #define SvWEAKREF_on(sv) (SvFLAGS(sv) |= (SVf_ROK|SVprv_WEAKREF))
811 #define SvWEAKREF_off(sv) (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_WEAKREF))
813 #define SvTHINKFIRST(sv) (SvFLAGS(sv) & SVf_THINKFIRST)
815 #define SvPADBUSY(sv) (SvFLAGS(sv) & SVs_PADBUSY)
817 #define SvPADTMP(sv) (SvFLAGS(sv) & SVs_PADTMP)
818 #define SvPADTMP_on(sv) (SvFLAGS(sv) |= SVs_PADTMP|SVs_PADBUSY)
819 #define SvPADTMP_off(sv) (SvFLAGS(sv) &= ~SVs_PADTMP)
821 #define SvPADMY(sv) (SvFLAGS(sv) & SVs_PADMY)
822 #define SvPADMY_on(sv) (SvFLAGS(sv) |= SVs_PADMY|SVs_PADBUSY)
824 #define SvTEMP(sv) (SvFLAGS(sv) & SVs_TEMP)
825 #define SvTEMP_on(sv) (SvFLAGS(sv) |= SVs_TEMP)
826 #define SvTEMP_off(sv) (SvFLAGS(sv) &= ~SVs_TEMP)
828 #define SvOBJECT(sv) (SvFLAGS(sv) & SVs_OBJECT)
829 #define SvOBJECT_on(sv) (SvFLAGS(sv) |= SVs_OBJECT)
830 #define SvOBJECT_off(sv) (SvFLAGS(sv) &= ~SVs_OBJECT)
832 #define SvREADONLY(sv) (SvFLAGS(sv) & SVf_READONLY)
833 #define SvREADONLY_on(sv) (SvFLAGS(sv) |= SVf_READONLY)
834 #define SvREADONLY_off(sv) (SvFLAGS(sv) &= ~SVf_READONLY)
836 #define SvSCREAM(sv) (SvFLAGS(sv) & SVp_SCREAM)
837 #define SvSCREAM_on(sv) (SvFLAGS(sv) |= SVp_SCREAM)
838 #define SvSCREAM_off(sv) (SvFLAGS(sv) &= ~SVp_SCREAM)
840 #define SvCOMPILED(sv) (SvFLAGS(sv) & SVpfm_COMPILED)
841 #define SvCOMPILED_on(sv) (SvFLAGS(sv) |= SVpfm_COMPILED)
842 #define SvCOMPILED_off(sv) (SvFLAGS(sv) &= ~SVpfm_COMPILED)
844 #define SvEVALED(sv) (SvFLAGS(sv) & SVrepl_EVAL)
845 #define SvEVALED_on(sv) (SvFLAGS(sv) |= SVrepl_EVAL)
846 #define SvEVALED_off(sv) (SvFLAGS(sv) &= ~SVrepl_EVAL)
848 #define SvTAIL(sv) (SvFLAGS(sv) & SVpbm_TAIL)
849 #define SvTAIL_on(sv) (SvFLAGS(sv) |= SVpbm_TAIL)
850 #define SvTAIL_off(sv) (SvFLAGS(sv) &= ~SVpbm_TAIL)
852 #define SvVALID(sv) (SvFLAGS(sv) & SVpbm_VALID)
853 #define SvVALID_on(sv) (SvFLAGS(sv) |= SVpbm_VALID)
854 #define SvVALID_off(sv) (SvFLAGS(sv) &= ~SVpbm_VALID)
857 /* The following uses the FAKE flag to show that a regex pointer is infact
858 its own offset in the regexpad for ithreads */
859 #define SvREPADTMP(sv) (SvFLAGS(sv) & SVf_FAKE)
860 #define SvREPADTMP_on(sv) (SvFLAGS(sv) |= SVf_FAKE)
861 #define SvREPADTMP_off(sv) (SvFLAGS(sv) &= ~SVf_FAKE)
864 #define SvRV(sv) ((XRV*) SvANY(sv))->xrv_rv
865 #define SvRVx(sv) SvRV(sv)
867 #define SvIVX(sv) ((XPVIV*) SvANY(sv))->xiv_iv
868 #define SvUVX(sv) ((XPVUV*) SvANY(sv))->xuv_uv
869 #define SvNVX(sv) ((XPVNV*)SvANY(sv))->xnv_nv
870 #define SvPVX(sv) ((XPV*) SvANY(sv))->xpv_pv
872 /* Given that these two are new, there can't be any existing code using them
874 # define SvPVX_mutable(sv) (0 + SvPVX(sv))
875 # define SvPVX_const(sv) ((const char*) (0 + SvPVX(sv)))
877 /* Except for the poison code, which uses & to scribble over the pointer after
879 # define SvPVX_mutable(sv) (SvPVX(sv))
880 # define SvPVX_const(sv) ((const char*) (SvPVX(sv)))
882 #define SvCUR(sv) ((XPV*) SvANY(sv))->xpv_cur
883 #define SvLEN(sv) ((XPV*) SvANY(sv))->xpv_len
884 #define SvEND(sv)(((XPV*) SvANY(sv))->xpv_pv + ((XPV*)SvANY(sv))->xpv_cur)
886 #define SvIVXx(sv) SvIVX(sv)
887 #define SvUVXx(sv) SvUVX(sv)
888 #define SvNVXx(sv) SvNVX(sv)
889 #define SvPVXx(sv) SvPVX(sv)
890 #define SvLENx(sv) SvLEN(sv)
891 #define SvENDx(sv) ((PL_Sv = (sv)), SvEND(PL_Sv))
892 #define SvMAGIC(sv) ((XPVMG*) SvANY(sv))->xmg_magic
893 #define SvSTASH(sv) ((XPVMG*) SvANY(sv))->xmg_stash
895 /* Ask a scalar nicely to try to become an IV, if possible.
896 Not guaranteed to stay returning void */
897 /* Macro won't actually call sv_2iv if already IOK */
898 #define SvIV_please(sv) \
899 STMT_START {if (!SvIOKp(sv) && (SvNOK(sv) || SvPOK(sv))) \
900 (void) SvIV(sv); } STMT_END
901 /* Put the asserts back at some point and figure out where they reveal bugs
903 #define SvIV_set(sv, val) \
904 STMT_START { assert(SvTYPE(sv) == SVt_IV || SvTYPE(sv) >= SVt_PVIV); \
905 (((XPVIV*) SvANY(sv))->xiv_iv = (val)); } STMT_END
906 #define SvNV_set(sv, val) \
907 STMT_START { assert(SvTYPE(sv) == SVt_NV || SvTYPE(sv) >= SVt_PVNV); \
908 (((XPVNV*)SvANY(sv))->xnv_nv = (val)); } STMT_END
909 /* assert(SvTYPE(sv) >= SVt_PV); */
910 #define SvPV_set(sv, val) \
912 (((XPV*) SvANY(sv))->xpv_pv = (val)); } STMT_END
913 /* assert(SvTYPE(sv) == SVt_IV || SvTYPE(sv) >= SVt_PVIV); */
914 #define SvUV_set(sv, val) \
916 (((XPVUV*)SvANY(sv))->xuv_uv = (val)); } STMT_END
917 /* assert(SvTYPE(sv) >= SVt_RV); */
918 #define SvRV_set(sv, val) \
920 (((XRV*)SvANY(sv))->xrv_rv = (val)); } STMT_END
921 /* assert(SvTYPE(sv) >= SVt_PVMG); */
922 #define SvMAGIC_set(sv, val) \
924 (((XPVMG*)SvANY(sv))->xmg_magic = (val)); } STMT_END
925 /* assert(SvTYPE(sv) >= SVt_PVMG); */
926 #define SvSTASH_set(sv, val) \
928 (((XPVMG*) SvANY(sv))->xmg_stash = (val)); } STMT_END
929 /* assert(SvTYPE(sv) >= SVt_PV); */
930 #define SvCUR_set(sv, val) \
932 (((XPV*) SvANY(sv))->xpv_cur = (val)); } STMT_END
933 /* assert(SvTYPE(sv) >= SVt_PV); */
934 #define SvLEN_set(sv, val) \
936 (((XPV*) SvANY(sv))->xpv_len = (val)); } STMT_END
937 #define SvEND_set(sv, val) \
938 STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
939 (SvCUR(sv) = (val) - SvPVX(sv)); } STMT_END
941 #define SvPV_renew(sv,n) \
942 STMT_START { SvLEN_set(sv, n); \
943 SvPV_set((sv), (MEM_WRAP_CHECK_(n,char) \
944 (char*)saferealloc((Malloc_t)SvPVX(sv), \
945 (MEM_SIZE)((n))))); \
948 #define SvPV_shrink_to_cur(sv) STMT_START { \
949 const STRLEN _lEnGtH = SvCUR(sv) + 1; \
950 SvPV_renew(sv, _lEnGtH); \
953 #define SvPV_free(sv) \
955 assert(SvTYPE(sv) >= SVt_PV); \
958 SvPV_set(sv, SvPVX_mutable(sv) - SvIVX(sv)); \
959 SvFLAGS(sv) &= ~SVf_OOK; \
961 Safefree(SvPVX(sv)); \
965 #define BmRARE(sv) ((XPVBM*) SvANY(sv))->xbm_rare
966 #define BmUSEFUL(sv) ((XPVBM*) SvANY(sv))->xbm_useful
967 #define BmPREVIOUS(sv) ((XPVBM*) SvANY(sv))->xbm_previous
969 #define FmLINES(sv) ((XPVFM*) SvANY(sv))->xfm_lines
971 #define LvTYPE(sv) ((XPVLV*) SvANY(sv))->xlv_type
972 #define LvTARG(sv) ((XPVLV*) SvANY(sv))->xlv_targ
973 #define LvTARGOFF(sv) ((XPVLV*) SvANY(sv))->xlv_targoff
974 #define LvTARGLEN(sv) ((XPVLV*) SvANY(sv))->xlv_targlen
976 #define IoIFP(sv) ((XPVIO*) SvANY(sv))->xio_ifp
977 #define IoOFP(sv) ((XPVIO*) SvANY(sv))->xio_ofp
978 #define IoDIRP(sv) ((XPVIO*) SvANY(sv))->xio_dirp
979 #define IoANY(sv) ((XPVIO*) SvANY(sv))->xio_any
980 #define IoLINES(sv) ((XPVIO*) SvANY(sv))->xio_lines
981 #define IoPAGE(sv) ((XPVIO*) SvANY(sv))->xio_page
982 #define IoPAGE_LEN(sv) ((XPVIO*) SvANY(sv))->xio_page_len
983 #define IoLINES_LEFT(sv)((XPVIO*) SvANY(sv))->xio_lines_left
984 #define IoTOP_NAME(sv) ((XPVIO*) SvANY(sv))->xio_top_name
985 #define IoTOP_GV(sv) ((XPVIO*) SvANY(sv))->xio_top_gv
986 #define IoFMT_NAME(sv) ((XPVIO*) SvANY(sv))->xio_fmt_name
987 #define IoFMT_GV(sv) ((XPVIO*) SvANY(sv))->xio_fmt_gv
988 #define IoBOTTOM_NAME(sv)((XPVIO*) SvANY(sv))->xio_bottom_name
989 #define IoBOTTOM_GV(sv) ((XPVIO*) SvANY(sv))->xio_bottom_gv
990 #define IoSUBPROCESS(sv)((XPVIO*) SvANY(sv))->xio_subprocess
991 #define IoTYPE(sv) ((XPVIO*) SvANY(sv))->xio_type
992 #define IoFLAGS(sv) ((XPVIO*) SvANY(sv))->xio_flags
994 /* IoTYPE(sv) is a single character telling the type of I/O connection. */
995 #define IoTYPE_RDONLY '<'
996 #define IoTYPE_WRONLY '>'
997 #define IoTYPE_RDWR '+'
998 #define IoTYPE_APPEND 'a'
999 #define IoTYPE_PIPE '|'
1000 #define IoTYPE_STD '-' /* stdin or stdout */
1001 #define IoTYPE_SOCKET 's'
1002 #define IoTYPE_CLOSED ' '
1003 #define IoTYPE_IMPLICIT 'I' /* stdin or stdout or stderr */
1004 #define IoTYPE_NUMERIC '#' /* fdopen */
1007 =for apidoc Am|bool|SvTAINTED|SV* sv
1008 Checks to see if an SV is tainted. Returns TRUE if it is, FALSE if
1011 =for apidoc Am|void|SvTAINTED_on|SV* sv
1012 Marks an SV as tainted if tainting is enabled.
1014 =for apidoc Am|void|SvTAINTED_off|SV* sv
1015 Untaints an SV. Be I<very> careful with this routine, as it short-circuits
1016 some of Perl's fundamental security features. XS module authors should not
1017 use this function unless they fully understand all the implications of
1018 unconditionally untainting the value. Untainting should be done in the
1019 standard perl fashion, via a carefully crafted regexp, rather than directly
1020 untainting variables.
1022 =for apidoc Am|void|SvTAINT|SV* sv
1023 Taints an SV if tainting is enabled.
1028 #define sv_taint(sv) sv_magic((sv), NULL, PERL_MAGIC_taint, NULL, 0)
1030 #define SvTAINTED(sv) (SvMAGICAL(sv) && sv_tainted(sv))
1031 #define SvTAINTED_on(sv) STMT_START{ if(PL_tainting){sv_taint(sv);} }STMT_END
1032 #define SvTAINTED_off(sv) STMT_START{ if(PL_tainting){sv_untaint(sv);} }STMT_END
1034 #define SvTAINT(sv) \
1036 if (PL_tainting) { \
1043 =for apidoc Am|char*|SvPV_force|SV* sv|STRLEN len
1044 Like C<SvPV> but will force the SV into containing just a string
1045 (C<SvPOK_only>). You want force if you are going to update the C<SvPVX>
1048 =for apidoc Am|char*|SvPV_force_nomg|SV* sv|STRLEN len
1049 Like C<SvPV> but will force the SV into containing just a string
1050 (C<SvPOK_only>). You want force if you are going to update the C<SvPVX>
1051 directly. Doesn't process magic.
1053 =for apidoc Am|char*|SvPV|SV* sv|STRLEN len
1054 Returns a pointer to the string in the SV, or a stringified form of
1055 the SV if the SV does not contain a string. The SV may cache the
1056 stringified version becoming C<SvPOK>. Handles 'get' magic. See also
1057 C<SvPVx> for a version which guarantees to evaluate sv only once.
1059 =for apidoc Am|char*|SvPVx|SV* sv|STRLEN len
1060 A version of C<SvPV> which guarantees to evaluate sv only once.
1062 =for apidoc Am|char*|SvPV_nomg|SV* sv|STRLEN len
1063 Like C<SvPV> but doesn't process magic.
1065 =for apidoc Am|char*|SvPV_nolen|SV* sv
1066 Returns a pointer to the string in the SV, or a stringified form of
1067 the SV if the SV does not contain a string. The SV may cache the
1068 stringified form becoming C<SvPOK>. Handles 'get' magic.
1070 =for apidoc Am|IV|SvIV|SV* sv
1071 Coerces the given SV to an integer and returns it. See C<SvIVx> for a
1072 version which guarantees to evaluate sv only once.
1074 =for apidoc Am|IV|SvIV_nomg|SV* sv
1075 Like C<SvIV> but doesn't process magic.
1077 =for apidoc Am|IV|SvIVx|SV* sv
1078 Coerces the given SV to an integer and returns it. Guarantees to evaluate
1079 sv only once. Use the more efficient C<SvIV> otherwise.
1081 =for apidoc Am|NV|SvNV|SV* sv
1082 Coerce the given SV to a double and return it. See C<SvNVx> for a version
1083 which guarantees to evaluate sv only once.
1085 =for apidoc Am|NV|SvNVx|SV* sv
1086 Coerces the given SV to a double and returns it. Guarantees to evaluate
1087 sv only once. Use the more efficient C<SvNV> otherwise.
1089 =for apidoc Am|UV|SvUV|SV* sv
1090 Coerces the given SV to an unsigned integer and returns it. See C<SvUVx>
1091 for a version which guarantees to evaluate sv only once.
1093 =for apidoc Am|UV|SvUV_nomg|SV* sv
1094 Like C<SvUV> but doesn't process magic.
1096 =for apidoc Am|UV|SvUVx|SV* sv
1097 Coerces the given SV to an unsigned integer and returns it. Guarantees to
1098 evaluate sv only once. Use the more efficient C<SvUV> otherwise.
1100 =for apidoc Am|bool|SvTRUE|SV* sv
1101 Returns a boolean indicating whether Perl would evaluate the SV as true or
1102 false, defined or undefined. Does not handle 'get' magic.
1104 =for apidoc Am|char*|SvPVutf8_force|SV* sv|STRLEN len
1105 Like C<SvPV_force>, but converts sv to utf8 first if necessary.
1107 =for apidoc Am|char*|SvPVutf8|SV* sv|STRLEN len
1108 Like C<SvPV>, but converts sv to utf8 first if necessary.
1110 =for apidoc Am|char*|SvPVutf8_nolen|SV* sv
1111 Like C<SvPV_nolen>, but converts sv to utf8 first if necessary.
1113 =for apidoc Am|char*|SvPVbyte_force|SV* sv|STRLEN len
1114 Like C<SvPV_force>, but converts sv to byte representation first if necessary.
1116 =for apidoc Am|char*|SvPVbyte|SV* sv|STRLEN len
1117 Like C<SvPV>, but converts sv to byte representation first if necessary.
1119 =for apidoc Am|char*|SvPVbyte_nolen|SV* sv
1120 Like C<SvPV_nolen>, but converts sv to byte representation first if necessary.
1122 =for apidoc Am|char*|SvPVutf8x_force|SV* sv|STRLEN len
1123 Like C<SvPV_force>, but converts sv to utf8 first if necessary.
1124 Guarantees to evaluate sv only once; use the more efficient C<SvPVutf8_force>
1127 =for apidoc Am|char*|SvPVutf8x|SV* sv|STRLEN len
1128 Like C<SvPV>, but converts sv to utf8 first if necessary.
1129 Guarantees to evaluate sv only once; use the more efficient C<SvPVutf8>
1132 =for apidoc Am|char*|SvPVbytex_force|SV* sv|STRLEN len
1133 Like C<SvPV_force>, but converts sv to byte representation first if necessary.
1134 Guarantees to evaluate sv only once; use the more efficient C<SvPVbyte_force>
1137 =for apidoc Am|char*|SvPVbytex|SV* sv|STRLEN len
1138 Like C<SvPV>, but converts sv to byte representation first if necessary.
1139 Guarantees to evaluate sv only once; use the more efficient C<SvPVbyte>
1142 =for apidoc Am|bool|SvIsCOW|SV* sv
1143 Returns a boolean indicating whether the SV is Copy-On-Write. (either shared
1144 hash key scalars, or full Copy On Write scalars if 5.9.0 is configured for
1147 =for apidoc Am|bool|SvIsCOW_shared_hash|SV* sv
1148 Returns a boolean indicating whether the SV is Copy-On-Write shared hash key
1151 =for apidoc Am|void|sv_catpvn_nomg|SV* sv|const char* ptr|STRLEN len
1152 Like C<sv_catpvn> but doesn't process magic.
1154 =for apidoc Am|void|sv_setsv_nomg|SV* dsv|SV* ssv
1155 Like C<sv_setsv> but doesn't process magic.
1157 =for apidoc Am|void|sv_catsv_nomg|SV* dsv|SV* ssv
1158 Like C<sv_catsv> but doesn't process magic.
1163 /* Let us hope that bitmaps for UV and IV are the same */
1164 #define SvIV(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv))
1165 #define SvUV(sv) (SvIOK(sv) ? SvUVX(sv) : sv_2uv(sv))
1166 #define SvNV(sv) (SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv))
1168 #define SvIV_nomg(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv_flags(sv, 0))
1169 #define SvUV_nomg(sv) (SvIOK(sv) ? SvUVX(sv) : sv_2uv_flags(sv, 0))
1173 #define SvPV(sv, lp) SvPV_flags(sv, lp, SV_GMAGIC)
1174 #define SvPV_const(sv, lp) SvPV_flags_const(sv, lp, SV_GMAGIC)
1175 #define SvPV_mutable(sv, lp) SvPV_flags_mutable(sv, lp, SV_GMAGIC)
1177 #define SvPV_flags(sv, lp, flags) \
1178 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
1179 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv_flags(sv, &lp, flags))
1180 #define SvPV_flags_const(sv, lp, flags) \
1181 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
1182 ? ((lp = SvCUR(sv)), SvPVX_const(sv)) : \
1183 (const char*) sv_2pv_flags(sv, &lp, flags|SV_CONST_RETURN))
1184 #define SvPV_flags_const_nolen(sv, flags) \
1185 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
1186 ? SvPVX_const(sv) : \
1187 (const char*) sv_2pv_flags(sv, 0, flags|SV_CONST_RETURN))
1188 #define SvPV_flags_mutable(sv, lp, flags) \
1189 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
1190 ? ((lp = SvCUR(sv)), SvPVX_mutable(sv)) : \
1191 sv_2pv_flags(sv, &lp, flags|SV_MUTABLE_RETURN))
1193 #define SvPV_force(sv, lp) SvPV_force_flags(sv, lp, SV_GMAGIC)
1194 #define SvPV_force_nolen(sv) SvPV_force_flags_nolen(sv, SV_GMAGIC)
1195 #define SvPV_force_mutable(sv, lp) SvPV_force_flags_mutable(sv, lp, SV_GMAGIC)
1197 #define SvPV_force_nomg(sv, lp) SvPV_force_flags(sv, lp, 0)
1198 #define SvPV_force_nomg_nolen(sv) SvPV_force_flags_nolen(sv, 0)
1200 #define SvPV_force_flags(sv, lp, flags) \
1201 ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
1202 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvn_force_flags(sv, &lp, flags))
1203 #define SvPV_force_flags_nolen(sv, flags) \
1204 ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
1205 ? SvPVX(sv) : sv_pvn_force_flags(sv, 0, flags))
1206 #define SvPV_force_flags_mutable(sv, lp, flags) \
1207 ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
1208 ? ((lp = SvCUR(sv)), SvPVX_mutable(sv)) \
1209 : sv_pvn_force_flags(sv, &lp, flags|SV_MUTABLE_RETURN))
1211 #define SvPV_nolen(sv) \
1212 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
1213 ? SvPVX(sv) : sv_2pv_flags(sv, 0, SV_GMAGIC))
1215 #define SvPV_nolen_const(sv) \
1216 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
1217 ? SvPVX_const(sv) : sv_2pv_flags(sv, 0, SV_GMAGIC|SV_CONST_RETURN))
1219 #define SvPV_nomg(sv, lp) SvPV_flags(sv, lp, 0)
1220 #define SvPV_nomg_const(sv, lp) SvPV_flags_const(sv, lp, 0)
1221 #define SvPV_nomg_const_nolen(sv) SvPV_flags_const_nolen(sv, 0)
1225 #define SvPVutf8(sv, lp) \
1226 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK|SVf_UTF8) \
1227 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvutf8(sv, &lp))
1229 #define SvPVutf8_force(sv, lp) \
1230 ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == (SVf_POK|SVf_UTF8) \
1231 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvutf8n_force(sv, &lp))
1234 #define SvPVutf8_nolen(sv) \
1235 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK|SVf_UTF8)\
1236 ? SvPVX(sv) : sv_2pvutf8(sv, 0))
1240 #define SvPVbyte(sv, lp) \
1241 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK) \
1242 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvbyte(sv, &lp))
1244 #define SvPVbyte_force(sv, lp) \
1245 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST)) == (SVf_POK) \
1246 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvbyten_force(sv, &lp))
1248 #define SvPVbyte_nolen(sv) \
1249 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK)\
1250 ? SvPVX(sv) : sv_2pvbyte(sv, 0))
1254 /* define FOOx(): idempotent versions of FOO(). If possible, use a local
1255 * var to evaluate the arg once; failing that, use a global if possible;
1256 * failing that, call a function to do the work
1259 #define SvPVx_force(sv, lp) sv_pvn_force(sv, &lp)
1260 #define SvPVutf8x_force(sv, lp) sv_pvutf8n_force(sv, &lp)
1261 #define SvPVbytex_force(sv, lp) sv_pvbyten_force(sv, &lp)
1263 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
1265 # define SvIVx(sv) ({SV *_sv = (SV*)(sv); SvIV(_sv); })
1266 # define SvUVx(sv) ({SV *_sv = (SV*)(sv); SvUV(_sv); })
1267 # define SvNVx(sv) ({SV *_sv = (SV*)(sv); SvNV(_sv); })
1268 # define SvPVx(sv, lp) ({SV *_sv = (sv); SvPV(_sv, lp); })
1269 # define SvPVx_const(sv, lp) ({SV *_sv = (sv); SvPV_const(_sv, lp); })
1270 # define SvPVx_nolen(sv) ({SV *_sv = (sv); SvPV_nolen(_sv); })
1271 # define SvPVx_nolen_const(sv) ({SV *_sv = (sv); SvPV_nolen_const(_sv); })
1272 # define SvPVutf8x(sv, lp) ({SV *_sv = (sv); SvPVutf8(_sv, lp); })
1273 # define SvPVbytex(sv, lp) ({SV *_sv = (sv); SvPVbyte(_sv, lp); })
1274 # define SvPVbytex_nolen(sv) ({SV *_sv = (sv); SvPVbyte_nolen(_sv); })
1275 # define SvTRUE(sv) ( \
1279 ? (({XPV *nxpv = (XPV*)SvANY(sv); \
1281 (nxpv->xpv_cur > 1 || \
1282 (nxpv->xpv_cur && *nxpv->xpv_pv != '0')); }) \
1289 ? SvNVX(sv) != 0.0 \
1291 # define SvTRUEx(sv) ({SV *_sv = (sv); SvTRUE(_sv); })
1293 #else /* __GNUC__ */
1295 # ifdef USE_5005THREADS
1296 # define SvIVx(sv) sv_iv(sv)
1297 # define SvUVx(sv) sv_uv(sv)
1298 # define SvNVx(sv) sv_nv(sv)
1299 # define SvPVx(sv, lp) sv_pvn(sv, &lp)
1300 # define SvPVutf8x(sv, lp) sv_pvutf8n(sv, &lp)
1301 # define SvPVbytex(sv, lp) sv_pvbyten(sv, &lp)
1302 # define SvTRUE(sv) SvTRUEx(sv)
1303 # define SvTRUEx(sv) sv_true(sv)
1305 # else /* USE_5005THREADS */
1307 /* These inlined macros use globals, which will require a thread
1308 * declaration in user code, so we avoid them under threads */
1310 # define SvIVx(sv) ((PL_Sv = (sv)), SvIV(PL_Sv))
1311 # define SvUVx(sv) ((PL_Sv = (sv)), SvUV(PL_Sv))
1312 # define SvNVx(sv) ((PL_Sv = (sv)), SvNV(PL_Sv))
1313 # define SvPVx(sv, lp) ((PL_Sv = (sv)), SvPV(PL_Sv, lp))
1314 # define SvPVx_const(sv, lp) ((PL_Sv = (sv)), SvPV_const(PL_Sv, lp))
1315 # define SvPVx_nolen(sv) ((PL_Sv = (sv)), SvPV_nolen(PL_Sv))
1316 # define SvPVx_nolen_const(sv) ((PL_Sv = (sv)), SvPV_nolen_const(PL_Sv))
1317 # define SvPVutf8x(sv, lp) ((PL_Sv = (sv)), SvPVutf8(PL_Sv, lp))
1318 # define SvPVbytex(sv, lp) ((PL_Sv = (sv)), SvPVbyte(PL_Sv, lp))
1319 # define SvPVbytex_nolen(sv) ((PL_Sv = (sv)), SvPVbyte_nolen(PL_Sv))
1320 # define SvTRUE(sv) ( \
1324 ? ((PL_Xpv = (XPV*)SvANY(sv)) && \
1325 (PL_Xpv->xpv_cur > 1 || \
1326 (PL_Xpv->xpv_cur && *PL_Xpv->xpv_pv != '0')) \
1333 ? SvNVX(sv) != 0.0 \
1335 # define SvTRUEx(sv) ((PL_Sv = (sv)), SvTRUE(PL_Sv))
1336 # endif /* USE_5005THREADS */
1337 #endif /* __GNU__ */
1339 #define SvIsCOW(sv) ((SvFLAGS(sv) & (SVf_FAKE | SVf_READONLY)) == \
1340 (SVf_FAKE | SVf_READONLY))
1341 #define SvIsCOW_shared_hash(sv) (SvIsCOW(sv) && SvLEN(sv) == 0)
1343 #define SvSHARED_HASH(sv) (0 + SvUVX(sv))
1345 /* flag values for sv_*_flags functions */
1346 #define SV_IMMEDIATE_UNREF 1
1348 #define SV_COW_DROP_PV 4 /* Unused in Perl 5.8.x */
1349 #define SV_UTF8_NO_ENCODING 8
1350 #define SV_NOSTEAL 16
1351 #define SV_CONST_RETURN 32
1352 #define SV_MUTABLE_RETURN 64
1353 #define SV_SMAGIC 128
1355 #define sv_unref(sv) sv_unref_flags(sv, 0)
1356 #define sv_force_normal(sv) sv_force_normal_flags(sv, 0)
1358 /* all these 'functions' are now just macros */
1360 #define sv_pv(sv) SvPV_nolen(sv)
1361 #define sv_pvutf8(sv) SvPVutf8_nolen(sv)
1362 #define sv_pvbyte(sv) SvPVbyte_nolen(sv)
1364 #define sv_pvn_force_nomg(sv, lp) sv_pvn_force_flags(sv, lp, 0)
1365 #define sv_utf8_upgrade_nomg(sv) sv_utf8_upgrade_flags(sv, 0)
1366 #define sv_catpvn_nomg(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, 0)
1367 #define sv_setsv(dsv, ssv) sv_setsv_flags(dsv, ssv, SV_GMAGIC)
1368 #define sv_setsv_nomg(dsv, ssv) sv_setsv_flags(dsv, ssv, 0)
1369 #define sv_catsv(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC)
1370 #define sv_catsv_nomg(dsv, ssv) sv_catsv_flags(dsv, ssv, 0)
1371 #define sv_catsv_mg(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC|SV_SMAGIC)
1372 #define sv_catpvn(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC)
1373 #define sv_catpvn_mg(sv, sstr, slen) \
1374 sv_catpvn_flags(sv, sstr, slen, SV_GMAGIC|SV_SMAGIC);
1375 #define sv_2pv(sv, lp) sv_2pv_flags(sv, lp, SV_GMAGIC)
1376 #define sv_2pv_nolen(sv) sv_2pv(sv, 0)
1377 #define sv_2pvbyte_nolen(sv) sv_2pvbyte(sv, 0)
1378 #define sv_2pvutf8_nolen(sv) sv_2pvutf8(sv, 0)
1379 #define sv_2pv_nomg(sv, lp) sv_2pv_flags(sv, lp, 0)
1380 #define sv_pvn_force(sv, lp) sv_pvn_force_flags(sv, lp, SV_GMAGIC)
1381 #define sv_utf8_upgrade(sv) sv_utf8_upgrade_flags(sv, SV_GMAGIC)
1382 #define sv_2iv(sv) sv_2iv_flags(sv, SV_GMAGIC)
1383 #define sv_2uv(sv) sv_2uv_flags(sv, SV_GMAGIC)
1385 /* Should be named SvCatPVN_utf8_upgrade? */
1386 #define sv_catpvn_utf8_upgrade(dsv, sstr, slen, nsv) \
1389 nsv = sv_2mortal(newSVpvn(sstr, slen)); \
1391 sv_setpvn(nsv, sstr, slen); \
1393 sv_utf8_upgrade(nsv); \
1394 sv_catsv(dsv, nsv); \
1398 =for apidoc Am|SV*|newRV_inc|SV* sv
1400 Creates an RV wrapper for an SV. The reference count for the original SV is
1406 #define newRV_inc(sv) newRV(sv)
1408 /* the following macros update any magic values this sv is associated with */
1411 =head1 Magical Functions
1413 =for apidoc Am|void|SvGETMAGIC|SV* sv
1414 Invokes C<mg_get> on an SV if it has 'get' magic. This macro evaluates its
1415 argument more than once.
1417 =for apidoc Am|void|SvSETMAGIC|SV* sv
1418 Invokes C<mg_set> on an SV if it has 'set' magic. This macro evaluates its
1419 argument more than once.
1421 =for apidoc Am|void|SvSetSV|SV* dsb|SV* ssv
1422 Calls C<sv_setsv> if dsv is not the same as ssv. May evaluate arguments
1425 =for apidoc Am|void|SvSetSV_nosteal|SV* dsv|SV* ssv
1426 Calls a non-destructive version of C<sv_setsv> if dsv is not the same as
1427 ssv. May evaluate arguments more than once.
1429 =for apidoc Am|void|SvSetMagicSV|SV* dsb|SV* ssv
1430 Like C<SvSetSV>, but does any set magic required afterwards.
1432 =for apidoc Am|void|SvSetMagicSV_nosteal|SV* dsv|SV* ssv
1433 Like C<SvSetSV_nosteal>, but does any set magic required afterwards.
1435 =for apidoc Am|void|SvSHARE|SV* sv
1436 Arranges for sv to be shared between threads if a suitable module
1439 =for apidoc Am|void|SvLOCK|SV* sv
1440 Arranges for a mutual exclusion lock to be obtained on sv if a suitable module
1443 =for apidoc Am|void|SvUNLOCK|SV* sv
1444 Releases a mutual exclusion lock on sv if a suitable module
1447 =head1 SV Manipulation Functions
1449 =for apidoc Am|char *|SvGROW|SV* sv|STRLEN len
1450 Expands the character buffer in the SV so that it has room for the
1451 indicated number of bytes (remember to reserve space for an extra trailing
1452 NUL character). Calls C<sv_grow> to perform the expansion if necessary.
1453 Returns a pointer to the character buffer.
1458 #define SvSHARE(sv) CALL_FPTR(PL_sharehook)(aTHX_ sv)
1459 #define SvLOCK(sv) CALL_FPTR(PL_lockhook)(aTHX_ sv)
1460 #define SvUNLOCK(sv) CALL_FPTR(PL_unlockhook)(aTHX_ sv)
1462 #define SvGETMAGIC(x) STMT_START { if (SvGMAGICAL(x)) mg_get(x); } STMT_END
1463 #define SvSETMAGIC(x) STMT_START { if (SvSMAGICAL(x)) mg_set(x); } STMT_END
1465 #define SvSetSV_and(dst,src,finally) \
1467 if ((dst) != (src)) { \
1468 sv_setsv(dst, src); \
1472 #define SvSetSV_nosteal_and(dst,src,finally) \
1474 if ((dst) != (src)) { \
1475 sv_setsv_flags(dst, src, SV_GMAGIC | SV_NOSTEAL); \
1480 #define SvSetSV(dst,src) \
1481 SvSetSV_and(dst,src,/*nothing*/;)
1482 #define SvSetSV_nosteal(dst,src) \
1483 SvSetSV_nosteal_and(dst,src,/*nothing*/;)
1485 #define SvSetMagicSV(dst,src) \
1486 SvSetSV_and(dst,src,SvSETMAGIC(dst))
1487 #define SvSetMagicSV_nosteal(dst,src) \
1488 SvSetSV_nosteal_and(dst,src,SvSETMAGIC(dst))
1491 #if !defined(SKIP_DEBUGGING)
1492 #define SvPEEK(sv) sv_peek(sv)
1494 #define SvPEEK(sv) ""
1497 #define SvIMMORTAL(sv) ((sv)==&PL_sv_undef || (sv)==&PL_sv_yes || (sv)==&PL_sv_no || (sv)==&PL_sv_placeholder)
1499 #define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
1501 #define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
1502 /* If I give every macro argument a different name, then there won't be bugs
1503 where nested macros get confused. Been there, done that. */
1504 #define isGV_with_GP(pwadak) \
1505 ((SvTYPE(pwadak) == SVt_PVGV || SvTYPE(pwadak) == SVt_PVLV))
1507 #define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
1508 #define SvGROW_mutable(sv,len) \
1509 (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX_mutable(sv))
1510 #define Sv_Grow sv_grow
1512 #define CLONEf_COPY_STACKS 1
1513 #define CLONEf_KEEP_PTR_TABLE 2
1514 #define CLONEf_CLONE_HOST 4
1515 #define CLONEf_JOIN_IN 8
1517 struct clone_params {
1520 PerlInterpreter *proto_perl;
1523 #define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) sv_force_normal(sv)