3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 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.
9 * "I wonder what the Entish is for 'yes' and 'no'," he thought.
12 * This file contains the code that creates, manipulates and destroys
13 * scalar values (SVs). The other types (AV, HV, GV, etc.) reuse the
14 * structure of an SV, so their creation and destruction is handled
15 * here; higher-level functions are in av.c, hv.c, and so on. Opcode
16 * level functions (eg. substr, split, join) for each of the types are
28 /* Missing proto on LynxOS */
29 char *gconvert(double, int, int, char *);
32 #ifdef PERL_UTF8_CACHE_ASSERT
33 /* The cache element 0 is the Unicode offset;
34 * the cache element 1 is the byte offset of the element 0;
35 * the cache element 2 is the Unicode length of the substring;
36 * the cache element 3 is the byte length of the substring;
37 * The checking of the substring side would be good
38 * but substr() has enough code paths to make my head spin;
39 * if adding more checks watch out for the following tests:
40 * t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t
41 * lib/utf8.t lib/Unicode/Collate/t/index.t
44 #define ASSERT_UTF8_CACHE(cache) \
45 STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); } } STMT_END
47 #define ASSERT_UTF8_CACHE(cache) NOOP
50 /* ============================================================================
52 =head1 Allocation and deallocation of SVs.
54 An SV (or AV, HV, etc.) is allocated in two parts: the head (struct sv,
55 av, hv...) contains type and reference count information, as well as a
56 pointer to the body (struct xrv, xpv, xpviv...), which contains fields
57 specific to each type.
59 Normally, this allocation is done using arenas, which are approximately
60 1K chunks of memory parcelled up into N heads or bodies. The first slot
61 in each arena is reserved, and is used to hold a link to the next arena.
62 In the case of heads, the unused first slot also contains some flags and
63 a note of the number of slots. Snaked through each arena chain is a
64 linked list of free items; when this becomes empty, an extra arena is
65 allocated and divided up into N items which are threaded into the free
68 The following global variables are associated with arenas:
70 PL_sv_arenaroot pointer to list of SV arenas
71 PL_sv_root pointer to list of free SV structures
73 PL_foo_arenaroot pointer to list of foo arenas,
74 PL_foo_root pointer to list of free foo bodies
75 ... for foo in xiv, xnv, xrv, xpv etc.
77 Note that some of the larger and more rarely used body types (eg xpvio)
78 are not allocated using arenas, but are instead just malloc()/free()ed as
79 required. Also, if PURIFY is defined, arenas are abandoned altogether,
80 with all items individually malloc()ed. In addition, a few SV heads are
81 not allocated from an arena, but are instead directly created as static
82 or auto variables, eg PL_sv_undef.
84 The SV arena serves the secondary purpose of allowing still-live SVs
85 to be located and destroyed during final cleanup.
87 At the lowest level, the macros new_SV() and del_SV() grab and free
88 an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
89 to return the SV to the free list with error checking.) new_SV() calls
90 more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
91 SVs in the free list have their SvTYPE field set to all ones.
93 Similarly, there are macros new_XIV()/del_XIV(), new_XNV()/del_XNV() etc
94 that allocate and return individual body types. Normally these are mapped
95 to the arena-manipulating functions new_xiv()/del_xiv() etc, but may be
96 instead mapped directly to malloc()/free() if PURIFY is defined. The
97 new/del functions remove from, or add to, the appropriate PL_foo_root
98 list, and call more_xiv() etc to add a new arena if the list is empty.
100 At the time of very final cleanup, sv_free_arenas() is called from
101 perl_destruct() to physically free all the arenas allocated since the
102 start of the interpreter. Note that this also clears PL_he_arenaroot,
103 which is otherwise dealt with in hv.c.
105 Manipulation of any of the PL_*root pointers is protected by enclosing
106 LOCK_SV_MUTEX; ... UNLOCK_SV_MUTEX calls which should Do the Right Thing
107 if threads are enabled.
109 The function visit() scans the SV arenas list, and calls a specified
110 function for each SV it finds which is still live - ie which has an SvTYPE
111 other than all 1's, and a non-zero SvREFCNT. visit() is used by the
112 following functions (specified as [function that calls visit()] / [function
113 called by visit() for each SV]):
115 sv_report_used() / do_report_used()
116 dump all remaining SVs (debugging aid)
118 sv_clean_objs() / do_clean_objs(),do_clean_named_objs()
119 Attempt to free all objects pointed to by RVs,
120 and, unless DISABLE_DESTRUCTOR_KLUDGE is defined,
121 try to do the same for all objects indirectly
122 referenced by typeglobs too. Called once from
123 perl_destruct(), prior to calling sv_clean_all()
126 sv_clean_all() / do_clean_all()
127 SvREFCNT_dec(sv) each remaining SV, possibly
128 triggering an sv_free(). It also sets the
129 SVf_BREAK flag on the SV to indicate that the
130 refcnt has been artificially lowered, and thus
131 stopping sv_free() from giving spurious warnings
132 about SVs which unexpectedly have a refcnt
133 of zero. called repeatedly from perl_destruct()
134 until there are no SVs left.
138 Private API to rest of sv.c
142 new_XIV(), del_XIV(),
143 new_XNV(), del_XNV(),
148 sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
153 ============================================================================ */
158 * "A time to plant, and a time to uproot what was planted..."
161 #define plant_SV(p) \
163 SvANY(p) = (void *)PL_sv_root; \
164 SvFLAGS(p) = SVTYPEMASK; \
169 /* sv_mutex must be held while calling uproot_SV() */
170 #define uproot_SV(p) \
173 PL_sv_root = (SV*)SvANY(p); \
178 /* new_SV(): return a new, empty SV head */
180 #ifdef DEBUG_LEAKING_SCALARS
181 /* provide a real function for a debugger to play with */
198 # define new_SV(p) (p)=S_new_SV(aTHX)
216 /* del_SV(): return an empty SV head to the free list */
231 S_del_sv(pTHX_ SV *p)
238 for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
240 svend = &sva[SvREFCNT(sva)];
241 if (p >= sv && p < svend)
245 if (ckWARN_d(WARN_INTERNAL))
246 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
247 "Attempt to free non-arena SV: 0x%"UVxf
248 pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
255 #else /* ! DEBUGGING */
257 #define del_SV(p) plant_SV(p)
259 #endif /* DEBUGGING */
263 =head1 SV Manipulation Functions
265 =for apidoc sv_add_arena
267 Given a chunk of memory, link it to the head of the list of arenas,
268 and split it into a list of free SVs.
274 Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
280 /* The first SV in an arena isn't an SV. */
281 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
282 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
283 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
285 PL_sv_arenaroot = sva;
286 PL_sv_root = sva + 1;
288 svend = &sva[SvREFCNT(sva) - 1];
291 SvANY(sv) = (void *)(SV*)(sv + 1);
293 SvFLAGS(sv) = SVTYPEMASK;
297 SvFLAGS(sv) = SVTYPEMASK;
300 /* make some more SVs by adding another arena */
302 /* sv_mutex must be held while calling more_sv() */
309 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
310 PL_nice_chunk = Nullch;
311 PL_nice_chunk_size = 0;
314 char *chunk; /* must use New here to match call to */
315 New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */
316 sv_add_arena(chunk, 1008, 0);
322 /* visit(): call the named function for each non-free SV in the arenas
323 * whose flags field matches the flags/mask args. */
326 S_visit(pTHX_ SVFUNC_t f, U32 flags, U32 mask)
333 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
334 svend = &sva[SvREFCNT(sva)];
335 for (sv = sva + 1; sv < svend; ++sv) {
336 if (SvTYPE(sv) != SVTYPEMASK
337 && (sv->sv_flags & mask) == flags
350 /* called by sv_report_used() for each live SV */
353 do_report_used(pTHX_ SV *sv)
355 if (SvTYPE(sv) != SVTYPEMASK) {
356 PerlIO_printf(Perl_debug_log, "****\n");
363 =for apidoc sv_report_used
365 Dump the contents of all SVs not yet freed. (Debugging aid).
371 Perl_sv_report_used(pTHX)
374 visit(do_report_used, 0, 0);
378 /* called by sv_clean_objs() for each live SV */
381 do_clean_objs(pTHX_ SV *sv)
385 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
386 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv)));
398 /* XXX Might want to check arrays, etc. */
401 /* called by sv_clean_objs() for each live SV */
403 #ifndef DISABLE_DESTRUCTOR_KLUDGE
405 do_clean_named_objs(pTHX_ SV *sv)
407 if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) {
408 if ( SvOBJECT(GvSV(sv)) ||
409 (GvAV(sv) && SvOBJECT(GvAV(sv))) ||
410 (GvHV(sv) && SvOBJECT(GvHV(sv))) ||
411 (GvIO(sv) && SvOBJECT(GvIO(sv))) ||
412 (GvCV(sv) && SvOBJECT(GvCV(sv))) )
414 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv)));
415 SvFLAGS(sv) |= SVf_BREAK;
423 =for apidoc sv_clean_objs
425 Attempt to destroy all objects not yet freed
431 Perl_sv_clean_objs(pTHX)
433 PL_in_clean_objs = TRUE;
434 visit(do_clean_objs, SVf_ROK, SVf_ROK);
435 #ifndef DISABLE_DESTRUCTOR_KLUDGE
436 /* some barnacles may yet remain, clinging to typeglobs */
437 visit(do_clean_named_objs, SVt_PVGV, SVTYPEMASK);
439 PL_in_clean_objs = FALSE;
442 /* called by sv_clean_all() for each live SV */
445 do_clean_all(pTHX_ SV *sv)
447 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
448 SvFLAGS(sv) |= SVf_BREAK;
453 =for apidoc sv_clean_all
455 Decrement the refcnt of each remaining SV, possibly triggering a
456 cleanup. This function may have to be called multiple times to free
457 SVs which are in complex self-referential hierarchies.
463 Perl_sv_clean_all(pTHX)
466 PL_in_clean_all = TRUE;
467 cleaned = visit(do_clean_all, 0,0);
468 PL_in_clean_all = FALSE;
473 =for apidoc sv_free_arenas
475 Deallocate the memory used by all arenas. Note that all the individual SV
476 heads and bodies within the arenas must already have been freed.
482 Perl_sv_free_arenas(pTHX)
486 XPV *arena, *arenanext;
488 /* Free arenas here, but be careful about fake ones. (We assume
489 contiguity of the fake ones with the corresponding real ones.) */
491 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
492 svanext = (SV*) SvANY(sva);
493 while (svanext && SvFAKE(svanext))
494 svanext = (SV*) SvANY(svanext);
497 Safefree((void *)sva);
500 for (arena = PL_xiv_arenaroot; arena; arena = arenanext) {
501 arenanext = (XPV*)arena->xpv_pv;
504 PL_xiv_arenaroot = 0;
507 for (arena = PL_xnv_arenaroot; arena; arena = arenanext) {
508 arenanext = (XPV*)arena->xpv_pv;
511 PL_xnv_arenaroot = 0;
514 for (arena = PL_xrv_arenaroot; arena; arena = arenanext) {
515 arenanext = (XPV*)arena->xpv_pv;
518 PL_xrv_arenaroot = 0;
521 for (arena = PL_xpv_arenaroot; arena; arena = arenanext) {
522 arenanext = (XPV*)arena->xpv_pv;
525 PL_xpv_arenaroot = 0;
528 for (arena = (XPV*)PL_xpviv_arenaroot; arena; arena = arenanext) {
529 arenanext = (XPV*)arena->xpv_pv;
532 PL_xpviv_arenaroot = 0;
535 for (arena = (XPV*)PL_xpvnv_arenaroot; arena; arena = arenanext) {
536 arenanext = (XPV*)arena->xpv_pv;
539 PL_xpvnv_arenaroot = 0;
542 for (arena = (XPV*)PL_xpvcv_arenaroot; arena; arena = arenanext) {
543 arenanext = (XPV*)arena->xpv_pv;
546 PL_xpvcv_arenaroot = 0;
549 for (arena = (XPV*)PL_xpvav_arenaroot; arena; arena = arenanext) {
550 arenanext = (XPV*)arena->xpv_pv;
553 PL_xpvav_arenaroot = 0;
556 for (arena = (XPV*)PL_xpvhv_arenaroot; arena; arena = arenanext) {
557 arenanext = (XPV*)arena->xpv_pv;
560 PL_xpvhv_arenaroot = 0;
563 for (arena = (XPV*)PL_xpvmg_arenaroot; arena; arena = arenanext) {
564 arenanext = (XPV*)arena->xpv_pv;
567 PL_xpvmg_arenaroot = 0;
570 for (arena = (XPV*)PL_xpvlv_arenaroot; arena; arena = arenanext) {
571 arenanext = (XPV*)arena->xpv_pv;
574 PL_xpvlv_arenaroot = 0;
577 for (arena = (XPV*)PL_xpvbm_arenaroot; arena; arena = arenanext) {
578 arenanext = (XPV*)arena->xpv_pv;
581 PL_xpvbm_arenaroot = 0;
584 for (arena = (XPV*)PL_he_arenaroot; arena; arena = arenanext) {
585 arenanext = (XPV*)arena->xpv_pv;
592 Safefree(PL_nice_chunk);
593 PL_nice_chunk = Nullch;
594 PL_nice_chunk_size = 0;
600 =for apidoc report_uninit
602 Print appropriate "Use of uninitialized variable" warning
608 Perl_report_uninit(pTHX)
611 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
612 " in ", OP_DESC(PL_op));
614 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit, "", "");
617 /* grab a new IV body from the free list, allocating more if necessary */
628 * See comment in more_xiv() -- RAM.
630 PL_xiv_root = *(IV**)xiv;
632 return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv));
635 /* return an IV body to the free list */
638 S_del_xiv(pTHX_ XPVIV *p)
640 IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv));
642 *(IV**)xiv = PL_xiv_root;
647 /* allocate another arena's worth of IV bodies */
655 New(705, ptr, 1008/sizeof(XPV), XPV);
656 ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */
657 PL_xiv_arenaroot = ptr; /* to keep Purify happy */
660 xivend = &xiv[1008 / sizeof(IV) - 1];
661 xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */
663 while (xiv < xivend) {
664 *(IV**)xiv = (IV *)(xiv + 1);
670 /* grab a new NV body from the free list, allocating more if necessary */
680 PL_xnv_root = *(NV**)xnv;
682 return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv));
685 /* return an NV body to the free list */
688 S_del_xnv(pTHX_ XPVNV *p)
690 NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv));
692 *(NV**)xnv = PL_xnv_root;
697 /* allocate another arena's worth of NV bodies */
705 New(711, ptr, 1008/sizeof(XPV), XPV);
706 ptr->xpv_pv = (char*)PL_xnv_arenaroot;
707 PL_xnv_arenaroot = ptr;
710 xnvend = &xnv[1008 / sizeof(NV) - 1];
711 xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */
713 while (xnv < xnvend) {
714 *(NV**)xnv = (NV*)(xnv + 1);
720 /* grab a new struct xrv from the free list, allocating more if necessary */
730 PL_xrv_root = (XRV*)xrv->xrv_rv;
735 /* return a struct xrv to the free list */
738 S_del_xrv(pTHX_ XRV *p)
741 p->xrv_rv = (SV*)PL_xrv_root;
746 /* allocate another arena's worth of struct xrv */
752 register XRV* xrvend;
754 New(712, ptr, 1008/sizeof(XPV), XPV);
755 ptr->xpv_pv = (char*)PL_xrv_arenaroot;
756 PL_xrv_arenaroot = ptr;
759 xrvend = &xrv[1008 / sizeof(XRV) - 1];
760 xrv += (sizeof(XPV) - 1) / sizeof(XRV) + 1;
762 while (xrv < xrvend) {
763 xrv->xrv_rv = (SV*)(xrv + 1);
769 /* grab a new struct xpv from the free list, allocating more if necessary */
779 PL_xpv_root = (XPV*)xpv->xpv_pv;
784 /* return a struct xpv to the free list */
787 S_del_xpv(pTHX_ XPV *p)
790 p->xpv_pv = (char*)PL_xpv_root;
795 /* allocate another arena's worth of struct xpv */
801 register XPV* xpvend;
802 New(713, xpv, 1008/sizeof(XPV), XPV);
803 xpv->xpv_pv = (char*)PL_xpv_arenaroot;
804 PL_xpv_arenaroot = xpv;
806 xpvend = &xpv[1008 / sizeof(XPV) - 1];
808 while (xpv < xpvend) {
809 xpv->xpv_pv = (char*)(xpv + 1);
815 /* grab a new struct xpviv from the free list, allocating more if necessary */
824 xpviv = PL_xpviv_root;
825 PL_xpviv_root = (XPVIV*)xpviv->xpv_pv;
830 /* return a struct xpviv to the free list */
833 S_del_xpviv(pTHX_ XPVIV *p)
836 p->xpv_pv = (char*)PL_xpviv_root;
841 /* allocate another arena's worth of struct xpviv */
846 register XPVIV* xpviv;
847 register XPVIV* xpvivend;
848 New(714, xpviv, 1008/sizeof(XPVIV), XPVIV);
849 xpviv->xpv_pv = (char*)PL_xpviv_arenaroot;
850 PL_xpviv_arenaroot = xpviv;
852 xpvivend = &xpviv[1008 / sizeof(XPVIV) - 1];
853 PL_xpviv_root = ++xpviv;
854 while (xpviv < xpvivend) {
855 xpviv->xpv_pv = (char*)(xpviv + 1);
861 /* grab a new struct xpvnv from the free list, allocating more if necessary */
870 xpvnv = PL_xpvnv_root;
871 PL_xpvnv_root = (XPVNV*)xpvnv->xpv_pv;
876 /* return a struct xpvnv to the free list */
879 S_del_xpvnv(pTHX_ XPVNV *p)
882 p->xpv_pv = (char*)PL_xpvnv_root;
887 /* allocate another arena's worth of struct xpvnv */
892 register XPVNV* xpvnv;
893 register XPVNV* xpvnvend;
894 New(715, xpvnv, 1008/sizeof(XPVNV), XPVNV);
895 xpvnv->xpv_pv = (char*)PL_xpvnv_arenaroot;
896 PL_xpvnv_arenaroot = xpvnv;
898 xpvnvend = &xpvnv[1008 / sizeof(XPVNV) - 1];
899 PL_xpvnv_root = ++xpvnv;
900 while (xpvnv < xpvnvend) {
901 xpvnv->xpv_pv = (char*)(xpvnv + 1);
907 /* grab a new struct xpvcv from the free list, allocating more if necessary */
916 xpvcv = PL_xpvcv_root;
917 PL_xpvcv_root = (XPVCV*)xpvcv->xpv_pv;
922 /* return a struct xpvcv to the free list */
925 S_del_xpvcv(pTHX_ XPVCV *p)
928 p->xpv_pv = (char*)PL_xpvcv_root;
933 /* allocate another arena's worth of struct xpvcv */
938 register XPVCV* xpvcv;
939 register XPVCV* xpvcvend;
940 New(716, xpvcv, 1008/sizeof(XPVCV), XPVCV);
941 xpvcv->xpv_pv = (char*)PL_xpvcv_arenaroot;
942 PL_xpvcv_arenaroot = xpvcv;
944 xpvcvend = &xpvcv[1008 / sizeof(XPVCV) - 1];
945 PL_xpvcv_root = ++xpvcv;
946 while (xpvcv < xpvcvend) {
947 xpvcv->xpv_pv = (char*)(xpvcv + 1);
953 /* grab a new struct xpvav from the free list, allocating more if necessary */
962 xpvav = PL_xpvav_root;
963 PL_xpvav_root = (XPVAV*)xpvav->xav_array;
968 /* return a struct xpvav to the free list */
971 S_del_xpvav(pTHX_ XPVAV *p)
974 p->xav_array = (char*)PL_xpvav_root;
979 /* allocate another arena's worth of struct xpvav */
984 register XPVAV* xpvav;
985 register XPVAV* xpvavend;
986 New(717, xpvav, 1008/sizeof(XPVAV), XPVAV);
987 xpvav->xav_array = (char*)PL_xpvav_arenaroot;
988 PL_xpvav_arenaroot = xpvav;
990 xpvavend = &xpvav[1008 / sizeof(XPVAV) - 1];
991 PL_xpvav_root = ++xpvav;
992 while (xpvav < xpvavend) {
993 xpvav->xav_array = (char*)(xpvav + 1);
996 xpvav->xav_array = 0;
999 /* grab a new struct xpvhv from the free list, allocating more if necessary */
1008 xpvhv = PL_xpvhv_root;
1009 PL_xpvhv_root = (XPVHV*)xpvhv->xhv_array;
1014 /* return a struct xpvhv to the free list */
1017 S_del_xpvhv(pTHX_ XPVHV *p)
1020 p->xhv_array = (char*)PL_xpvhv_root;
1025 /* allocate another arena's worth of struct xpvhv */
1030 register XPVHV* xpvhv;
1031 register XPVHV* xpvhvend;
1032 New(718, xpvhv, 1008/sizeof(XPVHV), XPVHV);
1033 xpvhv->xhv_array = (char*)PL_xpvhv_arenaroot;
1034 PL_xpvhv_arenaroot = xpvhv;
1036 xpvhvend = &xpvhv[1008 / sizeof(XPVHV) - 1];
1037 PL_xpvhv_root = ++xpvhv;
1038 while (xpvhv < xpvhvend) {
1039 xpvhv->xhv_array = (char*)(xpvhv + 1);
1042 xpvhv->xhv_array = 0;
1045 /* grab a new struct xpvmg from the free list, allocating more if necessary */
1054 xpvmg = PL_xpvmg_root;
1055 PL_xpvmg_root = (XPVMG*)xpvmg->xpv_pv;
1060 /* return a struct xpvmg to the free list */
1063 S_del_xpvmg(pTHX_ XPVMG *p)
1066 p->xpv_pv = (char*)PL_xpvmg_root;
1071 /* allocate another arena's worth of struct xpvmg */
1076 register XPVMG* xpvmg;
1077 register XPVMG* xpvmgend;
1078 New(719, xpvmg, 1008/sizeof(XPVMG), XPVMG);
1079 xpvmg->xpv_pv = (char*)PL_xpvmg_arenaroot;
1080 PL_xpvmg_arenaroot = xpvmg;
1082 xpvmgend = &xpvmg[1008 / sizeof(XPVMG) - 1];
1083 PL_xpvmg_root = ++xpvmg;
1084 while (xpvmg < xpvmgend) {
1085 xpvmg->xpv_pv = (char*)(xpvmg + 1);
1091 /* grab a new struct xpvlv from the free list, allocating more if necessary */
1100 xpvlv = PL_xpvlv_root;
1101 PL_xpvlv_root = (XPVLV*)xpvlv->xpv_pv;
1106 /* return a struct xpvlv to the free list */
1109 S_del_xpvlv(pTHX_ XPVLV *p)
1112 p->xpv_pv = (char*)PL_xpvlv_root;
1117 /* allocate another arena's worth of struct xpvlv */
1122 register XPVLV* xpvlv;
1123 register XPVLV* xpvlvend;
1124 New(720, xpvlv, 1008/sizeof(XPVLV), XPVLV);
1125 xpvlv->xpv_pv = (char*)PL_xpvlv_arenaroot;
1126 PL_xpvlv_arenaroot = xpvlv;
1128 xpvlvend = &xpvlv[1008 / sizeof(XPVLV) - 1];
1129 PL_xpvlv_root = ++xpvlv;
1130 while (xpvlv < xpvlvend) {
1131 xpvlv->xpv_pv = (char*)(xpvlv + 1);
1137 /* grab a new struct xpvbm from the free list, allocating more if necessary */
1146 xpvbm = PL_xpvbm_root;
1147 PL_xpvbm_root = (XPVBM*)xpvbm->xpv_pv;
1152 /* return a struct xpvbm to the free list */
1155 S_del_xpvbm(pTHX_ XPVBM *p)
1158 p->xpv_pv = (char*)PL_xpvbm_root;
1163 /* allocate another arena's worth of struct xpvbm */
1168 register XPVBM* xpvbm;
1169 register XPVBM* xpvbmend;
1170 New(721, xpvbm, 1008/sizeof(XPVBM), XPVBM);
1171 xpvbm->xpv_pv = (char*)PL_xpvbm_arenaroot;
1172 PL_xpvbm_arenaroot = xpvbm;
1174 xpvbmend = &xpvbm[1008 / sizeof(XPVBM) - 1];
1175 PL_xpvbm_root = ++xpvbm;
1176 while (xpvbm < xpvbmend) {
1177 xpvbm->xpv_pv = (char*)(xpvbm + 1);
1183 #define my_safemalloc(s) (void*)safemalloc(s)
1184 #define my_safefree(p) safefree((char*)p)
1188 #define new_XIV() my_safemalloc(sizeof(XPVIV))
1189 #define del_XIV(p) my_safefree(p)
1191 #define new_XNV() my_safemalloc(sizeof(XPVNV))
1192 #define del_XNV(p) my_safefree(p)
1194 #define new_XRV() my_safemalloc(sizeof(XRV))
1195 #define del_XRV(p) my_safefree(p)
1197 #define new_XPV() my_safemalloc(sizeof(XPV))
1198 #define del_XPV(p) my_safefree(p)
1200 #define new_XPVIV() my_safemalloc(sizeof(XPVIV))
1201 #define del_XPVIV(p) my_safefree(p)
1203 #define new_XPVNV() my_safemalloc(sizeof(XPVNV))
1204 #define del_XPVNV(p) my_safefree(p)
1206 #define new_XPVCV() my_safemalloc(sizeof(XPVCV))
1207 #define del_XPVCV(p) my_safefree(p)
1209 #define new_XPVAV() my_safemalloc(sizeof(XPVAV))
1210 #define del_XPVAV(p) my_safefree(p)
1212 #define new_XPVHV() my_safemalloc(sizeof(XPVHV))
1213 #define del_XPVHV(p) my_safefree(p)
1215 #define new_XPVMG() my_safemalloc(sizeof(XPVMG))
1216 #define del_XPVMG(p) my_safefree(p)
1218 #define new_XPVLV() my_safemalloc(sizeof(XPVLV))
1219 #define del_XPVLV(p) my_safefree(p)
1221 #define new_XPVBM() my_safemalloc(sizeof(XPVBM))
1222 #define del_XPVBM(p) my_safefree(p)
1226 #define new_XIV() (void*)new_xiv()
1227 #define del_XIV(p) del_xiv((XPVIV*) p)
1229 #define new_XNV() (void*)new_xnv()
1230 #define del_XNV(p) del_xnv((XPVNV*) p)
1232 #define new_XRV() (void*)new_xrv()
1233 #define del_XRV(p) del_xrv((XRV*) p)
1235 #define new_XPV() (void*)new_xpv()
1236 #define del_XPV(p) del_xpv((XPV *)p)
1238 #define new_XPVIV() (void*)new_xpviv()
1239 #define del_XPVIV(p) del_xpviv((XPVIV *)p)
1241 #define new_XPVNV() (void*)new_xpvnv()
1242 #define del_XPVNV(p) del_xpvnv((XPVNV *)p)
1244 #define new_XPVCV() (void*)new_xpvcv()
1245 #define del_XPVCV(p) del_xpvcv((XPVCV *)p)
1247 #define new_XPVAV() (void*)new_xpvav()
1248 #define del_XPVAV(p) del_xpvav((XPVAV *)p)
1250 #define new_XPVHV() (void*)new_xpvhv()
1251 #define del_XPVHV(p) del_xpvhv((XPVHV *)p)
1253 #define new_XPVMG() (void*)new_xpvmg()
1254 #define del_XPVMG(p) del_xpvmg((XPVMG *)p)
1256 #define new_XPVLV() (void*)new_xpvlv()
1257 #define del_XPVLV(p) del_xpvlv((XPVLV *)p)
1259 #define new_XPVBM() (void*)new_xpvbm()
1260 #define del_XPVBM(p) del_xpvbm((XPVBM *)p)
1264 #define new_XPVGV() my_safemalloc(sizeof(XPVGV))
1265 #define del_XPVGV(p) my_safefree(p)
1267 #define new_XPVFM() my_safemalloc(sizeof(XPVFM))
1268 #define del_XPVFM(p) my_safefree(p)
1270 #define new_XPVIO() my_safemalloc(sizeof(XPVIO))
1271 #define del_XPVIO(p) my_safefree(p)
1274 =for apidoc sv_upgrade
1276 Upgrade an SV to a more complex form. Generally adds a new body type to the
1277 SV, then copies across as much information as possible from the old body.
1278 You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>.
1284 Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
1292 MAGIC* magic = NULL;
1295 if (mt != SVt_PV && SvREADONLY(sv) && SvFAKE(sv)) {
1296 sv_force_normal(sv);
1299 if (SvTYPE(sv) == mt)
1303 (void)SvOOK_off(sv);
1305 switch (SvTYPE(sv)) {
1326 else if (mt < SVt_PVIV)
1343 pv = (char*)SvRV(sv);
1363 else if (mt == SVt_NV)
1374 del_XPVIV(SvANY(sv));
1384 del_XPVNV(SvANY(sv));
1392 magic = SvMAGIC(sv);
1393 stash = SvSTASH(sv);
1394 del_XPVMG(SvANY(sv));
1397 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
1402 Perl_croak(aTHX_ "Can't upgrade to undef");
1404 SvANY(sv) = new_XIV();
1408 SvANY(sv) = new_XNV();
1412 SvANY(sv) = new_XRV();
1416 SvANY(sv) = new_XPV();
1422 SvANY(sv) = new_XPVIV();
1432 SvANY(sv) = new_XPVNV();
1440 SvANY(sv) = new_XPVMG();
1446 SvMAGIC(sv) = magic;
1447 SvSTASH(sv) = stash;
1450 SvANY(sv) = new_XPVLV();
1456 SvMAGIC(sv) = magic;
1457 SvSTASH(sv) = stash;
1464 SvANY(sv) = new_XPVAV();
1472 SvMAGIC(sv) = magic;
1473 SvSTASH(sv) = stash;
1476 AvFLAGS(sv) = AVf_REAL;
1479 SvANY(sv) = new_XPVHV();
1485 HvTOTALKEYS(sv) = 0;
1486 HvPLACEHOLDERS(sv) = 0;
1487 SvMAGIC(sv) = magic;
1488 SvSTASH(sv) = stash;
1495 SvANY(sv) = new_XPVCV();
1496 Zero(SvANY(sv), 1, XPVCV);
1502 SvMAGIC(sv) = magic;
1503 SvSTASH(sv) = stash;
1506 SvANY(sv) = new_XPVGV();
1512 SvMAGIC(sv) = magic;
1513 SvSTASH(sv) = stash;
1521 SvANY(sv) = new_XPVBM();
1527 SvMAGIC(sv) = magic;
1528 SvSTASH(sv) = stash;
1534 SvANY(sv) = new_XPVFM();
1535 Zero(SvANY(sv), 1, XPVFM);
1541 SvMAGIC(sv) = magic;
1542 SvSTASH(sv) = stash;
1545 SvANY(sv) = new_XPVIO();
1546 Zero(SvANY(sv), 1, XPVIO);
1552 SvMAGIC(sv) = magic;
1553 SvSTASH(sv) = stash;
1554 IoPAGE_LEN(sv) = 60;
1557 SvFLAGS(sv) &= ~SVTYPEMASK;
1563 =for apidoc sv_backoff
1565 Remove any string offset. You should normally use the C<SvOOK_off> macro
1572 Perl_sv_backoff(pTHX_ register SV *sv)
1576 char *s = SvPVX(sv);
1577 SvLEN(sv) += SvIVX(sv);
1578 SvPVX(sv) -= SvIVX(sv);
1580 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
1582 SvFLAGS(sv) &= ~SVf_OOK;
1589 Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
1590 upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1591 Use the C<SvGROW> wrapper instead.
1597 Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
1603 #ifdef HAS_64K_LIMIT
1604 if (newlen >= 0x10000) {
1605 PerlIO_printf(Perl_debug_log,
1606 "Allocation too large: %"UVxf"\n", (UV)newlen);
1609 #endif /* HAS_64K_LIMIT */
1612 if (SvTYPE(sv) < SVt_PV) {
1613 sv_upgrade(sv, SVt_PV);
1616 else if (SvOOK(sv)) { /* pv is offset? */
1619 if (newlen > SvLEN(sv))
1620 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
1621 #ifdef HAS_64K_LIMIT
1622 if (newlen >= 0x10000)
1629 if (newlen > SvLEN(sv)) { /* need more room? */
1630 if (SvLEN(sv) && s) {
1632 STRLEN l = malloced_size((void*)SvPVX(sv));
1638 Renew(s,newlen,char);
1641 /* sv_force_normal_flags() must not try to unshare the new
1642 PVX we allocate below. AMS 20010713 */
1643 if (SvREADONLY(sv) && SvFAKE(sv)) {
1647 New(703, s, newlen, char);
1648 if (SvPVX(sv) && SvCUR(sv)) {
1649 Move(SvPVX(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
1653 SvLEN_set(sv, newlen);
1659 =for apidoc sv_setiv
1661 Copies an integer into the given SV, upgrading first if necessary.
1662 Does not handle 'set' magic. See also C<sv_setiv_mg>.
1668 Perl_sv_setiv(pTHX_ register SV *sv, IV i)
1670 SV_CHECK_THINKFIRST(sv);
1671 switch (SvTYPE(sv)) {
1673 sv_upgrade(sv, SVt_IV);
1676 sv_upgrade(sv, SVt_PVNV);
1680 sv_upgrade(sv, SVt_PVIV);
1689 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
1692 (void)SvIOK_only(sv); /* validate number */
1698 =for apidoc sv_setiv_mg
1700 Like C<sv_setiv>, but also handles 'set' magic.
1706 Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
1713 =for apidoc sv_setuv
1715 Copies an unsigned integer into the given SV, upgrading first if necessary.
1716 Does not handle 'set' magic. See also C<sv_setuv_mg>.
1722 Perl_sv_setuv(pTHX_ register SV *sv, UV u)
1724 /* With these two if statements:
1725 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
1728 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
1730 If you wish to remove them, please benchmark to see what the effect is
1732 if (u <= (UV)IV_MAX) {
1733 sv_setiv(sv, (IV)u);
1742 =for apidoc sv_setuv_mg
1744 Like C<sv_setuv>, but also handles 'set' magic.
1750 Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
1752 /* With these two if statements:
1753 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
1756 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
1758 If you wish to remove them, please benchmark to see what the effect is
1760 if (u <= (UV)IV_MAX) {
1761 sv_setiv(sv, (IV)u);
1771 =for apidoc sv_setnv
1773 Copies a double into the given SV, upgrading first if necessary.
1774 Does not handle 'set' magic. See also C<sv_setnv_mg>.
1780 Perl_sv_setnv(pTHX_ register SV *sv, NV num)
1782 SV_CHECK_THINKFIRST(sv);
1783 switch (SvTYPE(sv)) {
1786 sv_upgrade(sv, SVt_NV);
1791 sv_upgrade(sv, SVt_PVNV);
1800 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
1804 (void)SvNOK_only(sv); /* validate number */
1809 =for apidoc sv_setnv_mg
1811 Like C<sv_setnv>, but also handles 'set' magic.
1817 Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
1823 /* Print an "isn't numeric" warning, using a cleaned-up,
1824 * printable version of the offending string
1828 S_not_a_number(pTHX_ SV *sv)
1835 dsv = sv_2mortal(newSVpv("", 0));
1836 pv = sv_uni_display(dsv, sv, 10, 0);
1839 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
1840 /* each *s can expand to 4 chars + "...\0",
1841 i.e. need room for 8 chars */
1844 for (s = SvPVX(sv), end = s + SvCUR(sv); s < end && d < limit; s++) {
1846 if (ch & 128 && !isPRINT_LC(ch)) {
1855 else if (ch == '\r') {
1859 else if (ch == '\f') {
1863 else if (ch == '\\') {
1867 else if (ch == '\0') {
1871 else if (isPRINT_LC(ch))
1888 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1889 "Argument \"%s\" isn't numeric in %s", pv,
1892 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1893 "Argument \"%s\" isn't numeric", pv);
1897 =for apidoc looks_like_number
1899 Test if the content of an SV looks like a number (or is a number).
1900 C<Inf> and C<Infinity> are treated as numbers (so will not issue a
1901 non-numeric warning), even if your atof() doesn't grok them.
1907 Perl_looks_like_number(pTHX_ SV *sv)
1909 register char *sbegin;
1916 else if (SvPOKp(sv))
1917 sbegin = SvPV(sv, len);
1919 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
1920 return grok_number(sbegin, len, NULL);
1923 /* Actually, ISO C leaves conversion of UV to IV undefined, but
1924 until proven guilty, assume that things are not that bad... */
1929 As 64 bit platforms often have an NV that doesn't preserve all bits of
1930 an IV (an assumption perl has been based on to date) it becomes necessary
1931 to remove the assumption that the NV always carries enough precision to
1932 recreate the IV whenever needed, and that the NV is the canonical form.
1933 Instead, IV/UV and NV need to be given equal rights. So as to not lose
1934 precision as a side effect of conversion (which would lead to insanity
1935 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1936 1) to distinguish between IV/UV/NV slots that have cached a valid
1937 conversion where precision was lost and IV/UV/NV slots that have a
1938 valid conversion which has lost no precision
1939 2) to ensure that if a numeric conversion to one form is requested that
1940 would lose precision, the precise conversion (or differently
1941 imprecise conversion) is also performed and cached, to prevent
1942 requests for different numeric formats on the same SV causing
1943 lossy conversion chains. (lossless conversion chains are perfectly
1948 SvIOKp is true if the IV slot contains a valid value
1949 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
1950 SvNOKp is true if the NV slot contains a valid value
1951 SvNOK is true only if the NV value is accurate
1954 while converting from PV to NV, check to see if converting that NV to an
1955 IV(or UV) would lose accuracy over a direct conversion from PV to
1956 IV(or UV). If it would, cache both conversions, return NV, but mark
1957 SV as IOK NOKp (ie not NOK).
1959 While converting from PV to IV, check to see if converting that IV to an
1960 NV would lose accuracy over a direct conversion from PV to NV. If it
1961 would, cache both conversions, flag similarly.
1963 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
1964 correctly because if IV & NV were set NV *always* overruled.
1965 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
1966 changes - now IV and NV together means that the two are interchangeable:
1967 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
1969 The benefit of this is that operations such as pp_add know that if
1970 SvIOK is true for both left and right operands, then integer addition
1971 can be used instead of floating point (for cases where the result won't
1972 overflow). Before, floating point was always used, which could lead to
1973 loss of precision compared with integer addition.
1975 * making IV and NV equal status should make maths accurate on 64 bit
1977 * may speed up maths somewhat if pp_add and friends start to use
1978 integers when possible instead of fp. (Hopefully the overhead in
1979 looking for SvIOK and checking for overflow will not outweigh the
1980 fp to integer speedup)
1981 * will slow down integer operations (callers of SvIV) on "inaccurate"
1982 values, as the change from SvIOK to SvIOKp will cause a call into
1983 sv_2iv each time rather than a macro access direct to the IV slot
1984 * should speed up number->string conversion on integers as IV is
1985 favoured when IV and NV are equally accurate
1987 ####################################################################
1988 You had better be using SvIOK_notUV if you want an IV for arithmetic:
1989 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
1990 On the other hand, SvUOK is true iff UV.
1991 ####################################################################
1993 Your mileage will vary depending your CPU's relative fp to integer
1997 #ifndef NV_PRESERVES_UV
1998 # define IS_NUMBER_UNDERFLOW_IV 1
1999 # define IS_NUMBER_UNDERFLOW_UV 2
2000 # define IS_NUMBER_IV_AND_UV 2
2001 # define IS_NUMBER_OVERFLOW_IV 4
2002 # define IS_NUMBER_OVERFLOW_UV 5
2004 /* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
2006 /* For sv_2nv these three cases are "SvNOK and don't bother casting" */
2008 S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype)
2010 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%"UVxf" NV=%"NVgf" inttype=%"UVXf"\n", SvPVX(sv), SvIVX(sv), SvNVX(sv), (UV)numtype));
2011 if (SvNVX(sv) < (NV)IV_MIN) {
2012 (void)SvIOKp_on(sv);
2015 return IS_NUMBER_UNDERFLOW_IV;
2017 if (SvNVX(sv) > (NV)UV_MAX) {
2018 (void)SvIOKp_on(sv);
2022 return IS_NUMBER_OVERFLOW_UV;
2024 (void)SvIOKp_on(sv);
2026 /* Can't use strtol etc to convert this string. (See truth table in
2028 if (SvNVX(sv) <= (UV)IV_MAX) {
2029 SvIVX(sv) = I_V(SvNVX(sv));
2030 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2031 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
2033 /* Integer is imprecise. NOK, IOKp */
2035 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
2038 SvUVX(sv) = U_V(SvNVX(sv));
2039 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2040 if (SvUVX(sv) == UV_MAX) {
2041 /* As we know that NVs don't preserve UVs, UV_MAX cannot
2042 possibly be preserved by NV. Hence, it must be overflow.
2044 return IS_NUMBER_OVERFLOW_UV;
2046 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
2048 /* Integer is imprecise. NOK, IOKp */
2050 return IS_NUMBER_OVERFLOW_IV;
2052 #endif /* !NV_PRESERVES_UV*/
2057 Return the integer value of an SV, doing any necessary string conversion,
2058 magic etc. Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
2064 Perl_sv_2iv(pTHX_ register SV *sv)
2068 if (SvGMAGICAL(sv)) {
2073 return I_V(SvNVX(sv));
2075 if (SvPOKp(sv) && SvLEN(sv))
2078 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2079 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2085 if (SvTHINKFIRST(sv)) {
2088 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
2089 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
2090 return SvIV(tmpstr);
2091 return PTR2IV(SvRV(sv));
2093 if (SvREADONLY(sv) && SvFAKE(sv)) {
2094 sv_force_normal(sv);
2096 if (SvREADONLY(sv) && !SvOK(sv)) {
2097 if (ckWARN(WARN_UNINITIALIZED))
2104 return (IV)(SvUVX(sv));
2111 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2112 * without also getting a cached IV/UV from it at the same time
2113 * (ie PV->NV conversion should detect loss of accuracy and cache
2114 * IV or UV at same time to avoid this. NWC */
2116 if (SvTYPE(sv) == SVt_NV)
2117 sv_upgrade(sv, SVt_PVNV);
2119 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2120 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2121 certainly cast into the IV range at IV_MAX, whereas the correct
2122 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2124 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2125 SvIVX(sv) = I_V(SvNVX(sv));
2126 if (SvNVX(sv) == (NV) SvIVX(sv)
2127 #ifndef NV_PRESERVES_UV
2128 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2129 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2130 /* Don't flag it as "accurately an integer" if the number
2131 came from a (by definition imprecise) NV operation, and
2132 we're outside the range of NV integer precision */
2135 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2136 DEBUG_c(PerlIO_printf(Perl_debug_log,
2137 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
2143 /* IV not precise. No need to convert from PV, as NV
2144 conversion would already have cached IV if it detected
2145 that PV->IV would be better than PV->NV->IV
2146 flags already correct - don't set public IOK. */
2147 DEBUG_c(PerlIO_printf(Perl_debug_log,
2148 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
2153 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2154 but the cast (NV)IV_MIN rounds to a the value less (more
2155 negative) than IV_MIN which happens to be equal to SvNVX ??
2156 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2157 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2158 (NV)UVX == NVX are both true, but the values differ. :-(
2159 Hopefully for 2s complement IV_MIN is something like
2160 0x8000000000000000 which will be exact. NWC */
2163 SvUVX(sv) = U_V(SvNVX(sv));
2165 (SvNVX(sv) == (NV) SvUVX(sv))
2166 #ifndef NV_PRESERVES_UV
2167 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2168 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2169 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2170 /* Don't flag it as "accurately an integer" if the number
2171 came from a (by definition imprecise) NV operation, and
2172 we're outside the range of NV integer precision */
2178 DEBUG_c(PerlIO_printf(Perl_debug_log,
2179 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
2183 return (IV)SvUVX(sv);
2186 else if (SvPOKp(sv) && SvLEN(sv)) {
2188 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2189 /* We want to avoid a possible problem when we cache an IV which
2190 may be later translated to an NV, and the resulting NV is not
2191 the same as the direct translation of the initial string
2192 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2193 be careful to ensure that the value with the .456 is around if the
2194 NV value is requested in the future).
2196 This means that if we cache such an IV, we need to cache the
2197 NV as well. Moreover, we trade speed for space, and do not
2198 cache the NV if we are sure it's not needed.
2201 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2202 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2203 == IS_NUMBER_IN_UV) {
2204 /* It's definitely an integer, only upgrade to PVIV */
2205 if (SvTYPE(sv) < SVt_PVIV)
2206 sv_upgrade(sv, SVt_PVIV);
2208 } else if (SvTYPE(sv) < SVt_PVNV)
2209 sv_upgrade(sv, SVt_PVNV);
2211 /* If NV preserves UV then we only use the UV value if we know that
2212 we aren't going to call atof() below. If NVs don't preserve UVs
2213 then the value returned may have more precision than atof() will
2214 return, even though value isn't perfectly accurate. */
2215 if ((numtype & (IS_NUMBER_IN_UV
2216 #ifdef NV_PRESERVES_UV
2219 )) == IS_NUMBER_IN_UV) {
2220 /* This won't turn off the public IOK flag if it was set above */
2221 (void)SvIOKp_on(sv);
2223 if (!(numtype & IS_NUMBER_NEG)) {
2225 if (value <= (UV)IV_MAX) {
2226 SvIVX(sv) = (IV)value;
2232 /* 2s complement assumption */
2233 if (value <= (UV)IV_MIN) {
2234 SvIVX(sv) = -(IV)value;
2236 /* Too negative for an IV. This is a double upgrade, but
2237 I'm assuming it will be rare. */
2238 if (SvTYPE(sv) < SVt_PVNV)
2239 sv_upgrade(sv, SVt_PVNV);
2243 SvNVX(sv) = -(NV)value;
2248 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2249 will be in the previous block to set the IV slot, and the next
2250 block to set the NV slot. So no else here. */
2252 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2253 != IS_NUMBER_IN_UV) {
2254 /* It wasn't an (integer that doesn't overflow the UV). */
2255 SvNVX(sv) = Atof(SvPVX(sv));
2257 if (! numtype && ckWARN(WARN_NUMERIC))
2260 #if defined(USE_LONG_DOUBLE)
2261 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
2262 PTR2UV(sv), SvNVX(sv)));
2264 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
2265 PTR2UV(sv), SvNVX(sv)));
2269 #ifdef NV_PRESERVES_UV
2270 (void)SvIOKp_on(sv);
2272 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2273 SvIVX(sv) = I_V(SvNVX(sv));
2274 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2277 /* Integer is imprecise. NOK, IOKp */
2279 /* UV will not work better than IV */
2281 if (SvNVX(sv) > (NV)UV_MAX) {
2283 /* Integer is inaccurate. NOK, IOKp, is UV */
2287 SvUVX(sv) = U_V(SvNVX(sv));
2288 /* 0xFFFFFFFFFFFFFFFF not an issue in here */
2289 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2293 /* Integer is imprecise. NOK, IOKp, is UV */
2299 #else /* NV_PRESERVES_UV */
2300 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2301 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2302 /* The IV slot will have been set from value returned by
2303 grok_number above. The NV slot has just been set using
2306 assert (SvIOKp(sv));
2308 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2309 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2310 /* Small enough to preserve all bits. */
2311 (void)SvIOKp_on(sv);
2313 SvIVX(sv) = I_V(SvNVX(sv));
2314 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2316 /* Assumption: first non-preserved integer is < IV_MAX,
2317 this NV is in the preserved range, therefore: */
2318 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2320 Perl_croak(aTHX_ "sv_2iv assumed (U_V(fabs((double)SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%"NVgf" U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX);
2324 0 0 already failed to read UV.
2325 0 1 already failed to read UV.
2326 1 0 you won't get here in this case. IV/UV
2327 slot set, public IOK, Atof() unneeded.
2328 1 1 already read UV.
2329 so there's no point in sv_2iuv_non_preserve() attempting
2330 to use atol, strtol, strtoul etc. */
2331 if (sv_2iuv_non_preserve (sv, numtype)
2332 >= IS_NUMBER_OVERFLOW_IV)
2336 #endif /* NV_PRESERVES_UV */
2339 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
2341 if (SvTYPE(sv) < SVt_IV)
2342 /* Typically the caller expects that sv_any is not NULL now. */
2343 sv_upgrade(sv, SVt_IV);
2346 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2347 PTR2UV(sv),SvIVX(sv)));
2348 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
2354 Return the unsigned integer value of an SV, doing any necessary string
2355 conversion, magic etc. Normally used via the C<SvUV(sv)> and C<SvUVx(sv)>
2362 Perl_sv_2uv(pTHX_ register SV *sv)
2366 if (SvGMAGICAL(sv)) {
2371 return U_V(SvNVX(sv));
2372 if (SvPOKp(sv) && SvLEN(sv))
2375 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2376 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2382 if (SvTHINKFIRST(sv)) {
2385 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
2386 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
2387 return SvUV(tmpstr);
2388 return PTR2UV(SvRV(sv));
2390 if (SvREADONLY(sv) && SvFAKE(sv)) {
2391 sv_force_normal(sv);
2393 if (SvREADONLY(sv) && !SvOK(sv)) {
2394 if (ckWARN(WARN_UNINITIALIZED))
2404 return (UV)SvIVX(sv);
2408 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2409 * without also getting a cached IV/UV from it at the same time
2410 * (ie PV->NV conversion should detect loss of accuracy and cache
2411 * IV or UV at same time to avoid this. */
2412 /* IV-over-UV optimisation - choose to cache IV if possible */
2414 if (SvTYPE(sv) == SVt_NV)
2415 sv_upgrade(sv, SVt_PVNV);
2417 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2418 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2419 SvIVX(sv) = I_V(SvNVX(sv));
2420 if (SvNVX(sv) == (NV) SvIVX(sv)
2421 #ifndef NV_PRESERVES_UV
2422 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2423 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2424 /* Don't flag it as "accurately an integer" if the number
2425 came from a (by definition imprecise) NV operation, and
2426 we're outside the range of NV integer precision */
2429 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2430 DEBUG_c(PerlIO_printf(Perl_debug_log,
2431 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (precise)\n",
2437 /* IV not precise. No need to convert from PV, as NV
2438 conversion would already have cached IV if it detected
2439 that PV->IV would be better than PV->NV->IV
2440 flags already correct - don't set public IOK. */
2441 DEBUG_c(PerlIO_printf(Perl_debug_log,
2442 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (imprecise)\n",
2447 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2448 but the cast (NV)IV_MIN rounds to a the value less (more
2449 negative) than IV_MIN which happens to be equal to SvNVX ??
2450 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2451 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2452 (NV)UVX == NVX are both true, but the values differ. :-(
2453 Hopefully for 2s complement IV_MIN is something like
2454 0x8000000000000000 which will be exact. NWC */
2457 SvUVX(sv) = U_V(SvNVX(sv));
2459 (SvNVX(sv) == (NV) SvUVX(sv))
2460 #ifndef NV_PRESERVES_UV
2461 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2462 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2463 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2464 /* Don't flag it as "accurately an integer" if the number
2465 came from a (by definition imprecise) NV operation, and
2466 we're outside the range of NV integer precision */
2471 DEBUG_c(PerlIO_printf(Perl_debug_log,
2472 "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n",
2478 else if (SvPOKp(sv) && SvLEN(sv)) {
2480 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2482 /* We want to avoid a possible problem when we cache a UV which
2483 may be later translated to an NV, and the resulting NV is not
2484 the translation of the initial data.
2486 This means that if we cache such a UV, we need to cache the
2487 NV as well. Moreover, we trade speed for space, and do not
2488 cache the NV if not needed.
2491 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2492 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2493 == IS_NUMBER_IN_UV) {
2494 /* It's definitely an integer, only upgrade to PVIV */
2495 if (SvTYPE(sv) < SVt_PVIV)
2496 sv_upgrade(sv, SVt_PVIV);
2498 } else if (SvTYPE(sv) < SVt_PVNV)
2499 sv_upgrade(sv, SVt_PVNV);
2501 /* If NV preserves UV then we only use the UV value if we know that
2502 we aren't going to call atof() below. If NVs don't preserve UVs
2503 then the value returned may have more precision than atof() will
2504 return, even though it isn't accurate. */
2505 if ((numtype & (IS_NUMBER_IN_UV
2506 #ifdef NV_PRESERVES_UV
2509 )) == IS_NUMBER_IN_UV) {
2510 /* This won't turn off the public IOK flag if it was set above */
2511 (void)SvIOKp_on(sv);
2513 if (!(numtype & IS_NUMBER_NEG)) {
2515 if (value <= (UV)IV_MAX) {
2516 SvIVX(sv) = (IV)value;
2518 /* it didn't overflow, and it was positive. */
2523 /* 2s complement assumption */
2524 if (value <= (UV)IV_MIN) {
2525 SvIVX(sv) = -(IV)value;
2527 /* Too negative for an IV. This is a double upgrade, but
2528 I'm assuming it will be rare. */
2529 if (SvTYPE(sv) < SVt_PVNV)
2530 sv_upgrade(sv, SVt_PVNV);
2534 SvNVX(sv) = -(NV)value;
2540 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2541 != IS_NUMBER_IN_UV) {
2542 /* It wasn't an integer, or it overflowed the UV. */
2543 SvNVX(sv) = Atof(SvPVX(sv));
2545 if (! numtype && ckWARN(WARN_NUMERIC))
2548 #if defined(USE_LONG_DOUBLE)
2549 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n",
2550 PTR2UV(sv), SvNVX(sv)));
2552 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"NVgf")\n",
2553 PTR2UV(sv), SvNVX(sv)));
2556 #ifdef NV_PRESERVES_UV
2557 (void)SvIOKp_on(sv);
2559 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2560 SvIVX(sv) = I_V(SvNVX(sv));
2561 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2564 /* Integer is imprecise. NOK, IOKp */
2566 /* UV will not work better than IV */
2568 if (SvNVX(sv) > (NV)UV_MAX) {
2570 /* Integer is inaccurate. NOK, IOKp, is UV */
2574 SvUVX(sv) = U_V(SvNVX(sv));
2575 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
2576 NV preservse UV so can do correct comparison. */
2577 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2581 /* Integer is imprecise. NOK, IOKp, is UV */
2586 #else /* NV_PRESERVES_UV */
2587 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2588 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2589 /* The UV slot will have been set from value returned by
2590 grok_number above. The NV slot has just been set using
2593 assert (SvIOKp(sv));
2595 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2596 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2597 /* Small enough to preserve all bits. */
2598 (void)SvIOKp_on(sv);
2600 SvIVX(sv) = I_V(SvNVX(sv));
2601 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2603 /* Assumption: first non-preserved integer is < IV_MAX,
2604 this NV is in the preserved range, therefore: */
2605 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2607 Perl_croak(aTHX_ "sv_2uv assumed (U_V(fabs((double)SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%"NVgf" U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX);
2610 sv_2iuv_non_preserve (sv, numtype);
2612 #endif /* NV_PRESERVES_UV */
2616 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2617 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2620 if (SvTYPE(sv) < SVt_IV)
2621 /* Typically the caller expects that sv_any is not NULL now. */
2622 sv_upgrade(sv, SVt_IV);
2626 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
2627 PTR2UV(sv),SvUVX(sv)));
2628 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
2634 Return the num value of an SV, doing any necessary string or integer
2635 conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
2642 Perl_sv_2nv(pTHX_ register SV *sv)
2646 if (SvGMAGICAL(sv)) {
2650 if (SvPOKp(sv) && SvLEN(sv)) {
2651 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) &&
2652 !grok_number(SvPVX(sv), SvCUR(sv), NULL))
2654 return Atof(SvPVX(sv));
2658 return (NV)SvUVX(sv);
2660 return (NV)SvIVX(sv);
2663 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2664 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2670 if (SvTHINKFIRST(sv)) {
2673 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
2674 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
2675 return SvNV(tmpstr);
2676 return PTR2NV(SvRV(sv));
2678 if (SvREADONLY(sv) && SvFAKE(sv)) {
2679 sv_force_normal(sv);
2681 if (SvREADONLY(sv) && !SvOK(sv)) {
2682 if (ckWARN(WARN_UNINITIALIZED))
2687 if (SvTYPE(sv) < SVt_NV) {
2688 if (SvTYPE(sv) == SVt_IV)
2689 sv_upgrade(sv, SVt_PVNV);
2691 sv_upgrade(sv, SVt_NV);
2692 #ifdef USE_LONG_DOUBLE
2694 STORE_NUMERIC_LOCAL_SET_STANDARD();
2695 PerlIO_printf(Perl_debug_log,
2696 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
2697 PTR2UV(sv), SvNVX(sv));
2698 RESTORE_NUMERIC_LOCAL();
2702 STORE_NUMERIC_LOCAL_SET_STANDARD();
2703 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
2704 PTR2UV(sv), SvNVX(sv));
2705 RESTORE_NUMERIC_LOCAL();
2709 else if (SvTYPE(sv) < SVt_PVNV)
2710 sv_upgrade(sv, SVt_PVNV);
2715 SvNVX(sv) = SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv);
2716 #ifdef NV_PRESERVES_UV
2719 /* Only set the public NV OK flag if this NV preserves the IV */
2720 /* Check it's not 0xFFFFFFFFFFFFFFFF */
2721 if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
2722 : (SvIVX(sv) == I_V(SvNVX(sv))))
2728 else if (SvPOKp(sv) && SvLEN(sv)) {
2730 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2731 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !numtype)
2733 #ifdef NV_PRESERVES_UV
2734 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2735 == IS_NUMBER_IN_UV) {
2736 /* It's definitely an integer */
2737 SvNVX(sv) = (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value;
2739 SvNVX(sv) = Atof(SvPVX(sv));
2742 SvNVX(sv) = Atof(SvPVX(sv));
2743 /* Only set the public NV OK flag if this NV preserves the value in
2744 the PV at least as well as an IV/UV would.
2745 Not sure how to do this 100% reliably. */
2746 /* if that shift count is out of range then Configure's test is
2747 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2749 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2750 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2751 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
2752 } else if (!(numtype & IS_NUMBER_IN_UV)) {
2753 /* Can't use strtol etc to convert this string, so don't try.
2754 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
2757 /* value has been set. It may not be precise. */
2758 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
2759 /* 2s complement assumption for (UV)IV_MIN */
2760 SvNOK_on(sv); /* Integer is too negative. */
2765 if (numtype & IS_NUMBER_NEG) {
2766 SvIVX(sv) = -(IV)value;
2767 } else if (value <= (UV)IV_MAX) {
2768 SvIVX(sv) = (IV)value;
2774 if (numtype & IS_NUMBER_NOT_INT) {
2775 /* I believe that even if the original PV had decimals,
2776 they are lost beyond the limit of the FP precision.
2777 However, neither is canonical, so both only get p
2778 flags. NWC, 2000/11/25 */
2779 /* Both already have p flags, so do nothing */
2782 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2783 if (SvIVX(sv) == I_V(nv)) {
2788 /* It had no "." so it must be integer. */
2791 /* between IV_MAX and NV(UV_MAX).
2792 Could be slightly > UV_MAX */
2794 if (numtype & IS_NUMBER_NOT_INT) {
2795 /* UV and NV both imprecise. */
2797 UV nv_as_uv = U_V(nv);
2799 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
2810 #endif /* NV_PRESERVES_UV */
2813 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
2815 if (SvTYPE(sv) < SVt_NV)
2816 /* Typically the caller expects that sv_any is not NULL now. */
2817 /* XXX Ilya implies that this is a bug in callers that assume this
2818 and ideally should be fixed. */
2819 sv_upgrade(sv, SVt_NV);
2822 #if defined(USE_LONG_DOUBLE)
2824 STORE_NUMERIC_LOCAL_SET_STANDARD();
2825 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
2826 PTR2UV(sv), SvNVX(sv));
2827 RESTORE_NUMERIC_LOCAL();
2831 STORE_NUMERIC_LOCAL_SET_STANDARD();
2832 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
2833 PTR2UV(sv), SvNVX(sv));
2834 RESTORE_NUMERIC_LOCAL();
2840 /* asIV(): extract an integer from the string value of an SV.
2841 * Caller must validate PVX */
2844 S_asIV(pTHX_ SV *sv)
2847 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2849 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2850 == IS_NUMBER_IN_UV) {
2851 /* It's definitely an integer */
2852 if (numtype & IS_NUMBER_NEG) {
2853 if (value < (UV)IV_MIN)
2856 if (value < (UV)IV_MAX)
2861 if (ckWARN(WARN_NUMERIC))
2864 return I_V(Atof(SvPVX(sv)));
2867 /* asUV(): extract an unsigned integer from the string value of an SV
2868 * Caller must validate PVX */
2871 S_asUV(pTHX_ SV *sv)
2874 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2876 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2877 == IS_NUMBER_IN_UV) {
2878 /* It's definitely an integer */
2879 if (!(numtype & IS_NUMBER_NEG))
2883 if (ckWARN(WARN_NUMERIC))
2886 return U_V(Atof(SvPVX(sv)));
2890 =for apidoc sv_2pv_nolen
2892 Like C<sv_2pv()>, but doesn't return the length too. You should usually
2893 use the macro wrapper C<SvPV_nolen(sv)> instead.
2898 Perl_sv_2pv_nolen(pTHX_ register SV *sv)
2901 return sv_2pv(sv, &n_a);
2904 /* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
2905 * UV as a string towards the end of buf, and return pointers to start and
2908 * We assume that buf is at least TYPE_CHARS(UV) long.
2912 uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
2914 char *ptr = buf + TYPE_CHARS(UV);
2928 *--ptr = '0' + (char)(uv % 10);
2936 /* sv_2pv() is now a macro using Perl_sv_2pv_flags();
2937 * this function provided for binary compatibility only
2941 Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
2943 return sv_2pv_flags(sv, lp, SV_GMAGIC);
2947 =for apidoc sv_2pv_flags
2949 Returns a pointer to the string value of an SV, and sets *lp to its length.
2950 If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
2952 Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
2953 usually end up here too.
2959 Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
2964 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
2965 char *tmpbuf = tbuf;
2971 if (SvGMAGICAL(sv)) {
2972 if (flags & SV_GMAGIC)
2980 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
2982 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
2987 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
2992 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2993 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
3000 if (SvTHINKFIRST(sv)) {
3003 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
3004 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
3005 char *pv = SvPV(tmpstr, *lp);
3019 switch (SvTYPE(sv)) {
3021 if ( ((SvFLAGS(sv) &
3022 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
3023 == (SVs_OBJECT|SVs_SMG))
3024 && (mg = mg_find(sv, PERL_MAGIC_qr))) {
3025 regexp *re = (regexp *)mg->mg_obj;
3028 char *fptr = "msix";
3033 char need_newline = 0;
3034 U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12);
3036 while((ch = *fptr++)) {
3038 reflags[left++] = ch;
3041 reflags[right--] = ch;
3046 reflags[left] = '-';
3050 mg->mg_len = re->prelen + 4 + left;
3052 * If /x was used, we have to worry about a regex
3053 * ending with a comment later being embedded
3054 * within another regex. If so, we don't want this
3055 * regex's "commentization" to leak out to the
3056 * right part of the enclosing regex, we must cap
3057 * it with a newline.
3059 * So, if /x was used, we scan backwards from the
3060 * end of the regex. If we find a '#' before we
3061 * find a newline, we need to add a newline
3062 * ourself. If we find a '\n' first (or if we
3063 * don't find '#' or '\n'), we don't need to add
3064 * anything. -jfriedl
3066 if (PMf_EXTENDED & re->reganch)
3068 char *endptr = re->precomp + re->prelen;
3069 while (endptr >= re->precomp)
3071 char c = *(endptr--);
3073 break; /* don't need another */
3075 /* we end while in a comment, so we
3077 mg->mg_len++; /* save space for it */
3078 need_newline = 1; /* note to add it */
3084 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
3085 Copy("(?", mg->mg_ptr, 2, char);
3086 Copy(reflags, mg->mg_ptr+2, left, char);
3087 Copy(":", mg->mg_ptr+left+2, 1, char);
3088 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
3090 mg->mg_ptr[mg->mg_len - 2] = '\n';
3091 mg->mg_ptr[mg->mg_len - 1] = ')';
3092 mg->mg_ptr[mg->mg_len] = 0;
3094 PL_reginterp_cnt += re->program[0].next_off;
3096 if (re->reganch & ROPT_UTF8)
3111 case SVt_PVBM: if (SvROK(sv))
3114 s = "SCALAR"; break;
3115 case SVt_PVLV: s = SvROK(sv) ? "REF"
3116 /* tied lvalues should appear to be
3117 * scalars for backwards compatitbility */
3118 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
3119 ? "SCALAR" : "LVALUE"; break;
3120 case SVt_PVAV: s = "ARRAY"; break;
3121 case SVt_PVHV: s = "HASH"; break;
3122 case SVt_PVCV: s = "CODE"; break;
3123 case SVt_PVGV: s = "GLOB"; break;
3124 case SVt_PVFM: s = "FORMAT"; break;
3125 case SVt_PVIO: s = "IO"; break;
3126 default: s = "UNKNOWN"; break;
3130 HV *svs = SvSTASH(sv);
3133 /* [20011101.072] This bandaid for C<package;>
3134 should eventually be removed. AMS 20011103 */
3135 (svs ? HvNAME(svs) : "<none>"), s
3140 Perl_sv_catpvf(aTHX_ tsv, "(0x%"UVxf")", PTR2UV(sv));
3146 if (SvREADONLY(sv) && !SvOK(sv)) {
3147 if (ckWARN(WARN_UNINITIALIZED))
3153 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
3154 /* I'm assuming that if both IV and NV are equally valid then
3155 converting the IV is going to be more efficient */
3156 U32 isIOK = SvIOK(sv);
3157 U32 isUIOK = SvIsUV(sv);
3158 char buf[TYPE_CHARS(UV)];
3161 if (SvTYPE(sv) < SVt_PVIV)
3162 sv_upgrade(sv, SVt_PVIV);
3164 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
3166 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
3167 SvGROW(sv, (STRLEN)(ebuf - ptr + 1)); /* inlined from sv_setpvn */
3168 Move(ptr,SvPVX(sv),ebuf - ptr,char);
3169 SvCUR_set(sv, ebuf - ptr);
3179 else if (SvNOKp(sv)) {
3180 if (SvTYPE(sv) < SVt_PVNV)
3181 sv_upgrade(sv, SVt_PVNV);
3182 /* The +20 is pure guesswork. Configure test needed. --jhi */
3183 SvGROW(sv, NV_DIG + 20);
3185 olderrno = errno; /* some Xenix systems wipe out errno here */
3187 if (SvNVX(sv) == 0.0)
3188 (void)strcpy(s,"0");
3192 Gconvert(SvNVX(sv), NV_DIG, 0, s);
3195 #ifdef FIXNEGATIVEZERO
3196 if (*s == '-' && s[1] == '0' && !s[2])
3206 if (ckWARN(WARN_UNINITIALIZED)
3207 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
3210 if (SvTYPE(sv) < SVt_PV)
3211 /* Typically the caller expects that sv_any is not NULL now. */
3212 sv_upgrade(sv, SVt_PV);
3215 *lp = s - SvPVX(sv);
3218 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3219 PTR2UV(sv),SvPVX(sv)));
3223 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
3224 /* Sneaky stuff here */
3228 tsv = newSVpv(tmpbuf, 0);
3244 len = strlen(tmpbuf);
3246 #ifdef FIXNEGATIVEZERO
3247 if (len == 2 && t[0] == '-' && t[1] == '0') {
3252 (void)SvUPGRADE(sv, SVt_PV);
3254 s = SvGROW(sv, len + 1);
3257 return strcpy(s, t);
3262 =for apidoc sv_copypv
3264 Copies a stringified representation of the source SV into the
3265 destination SV. Automatically performs any necessary mg_get and
3266 coercion of numeric values into strings. Guaranteed to preserve
3267 UTF-8 flag even from overloaded objects. Similar in nature to
3268 sv_2pv[_flags] but operates directly on an SV instead of just the
3269 string. Mostly uses sv_2pv_flags to do its work, except when that
3270 would lose the UTF-8'ness of the PV.
3276 Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv)
3281 sv_setpvn(dsv,s,len);
3289 =for apidoc sv_2pvbyte_nolen
3291 Return a pointer to the byte-encoded representation of the SV.
3292 May cause the SV to be downgraded from UTF-8 as a side-effect.
3294 Usually accessed via the C<SvPVbyte_nolen> macro.
3300 Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
3303 return sv_2pvbyte(sv, &n_a);
3307 =for apidoc sv_2pvbyte
3309 Return a pointer to the byte-encoded representation of the SV, and set *lp
3310 to its length. May cause the SV to be downgraded from UTF-8 as a
3313 Usually accessed via the C<SvPVbyte> macro.
3319 Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
3321 sv_utf8_downgrade(sv,0);
3322 return SvPV(sv,*lp);
3326 =for apidoc sv_2pvutf8_nolen
3328 Return a pointer to the UTF-8-encoded representation of the SV.
3329 May cause the SV to be upgraded to UTF-8 as a side-effect.
3331 Usually accessed via the C<SvPVutf8_nolen> macro.
3337 Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
3340 return sv_2pvutf8(sv, &n_a);
3344 =for apidoc sv_2pvutf8
3346 Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
3347 to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
3349 Usually accessed via the C<SvPVutf8> macro.
3355 Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
3357 sv_utf8_upgrade(sv);
3358 return SvPV(sv,*lp);
3362 =for apidoc sv_2bool
3364 This function is only called on magical items, and is only used by
3365 sv_true() or its macro equivalent.
3371 Perl_sv_2bool(pTHX_ register SV *sv)
3380 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
3381 (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
3382 return (bool)SvTRUE(tmpsv);
3383 return SvRV(sv) != 0;
3386 register XPV* Xpvtmp;
3387 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
3388 (*Xpvtmp->xpv_pv > '0' ||
3389 Xpvtmp->xpv_cur > 1 ||
3390 (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0')))
3397 return SvIVX(sv) != 0;
3400 return SvNVX(sv) != 0.0;
3407 /* sv_utf8_upgrade() is now a macro using sv_utf8_upgrade_flags();
3408 * this function provided for binary compatibility only
3413 Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
3415 return sv_utf8_upgrade_flags(sv, SV_GMAGIC);
3419 =for apidoc sv_utf8_upgrade
3421 Converts the PV of an SV to its UTF-8-encoded form.
3422 Forces the SV to string form if it is not already.
3423 Always sets the SvUTF8 flag to avoid future validity checks even
3424 if all the bytes have hibit clear.
3426 This is not as a general purpose byte encoding to Unicode interface:
3427 use the Encode extension for that.
3429 =for apidoc sv_utf8_upgrade_flags
3431 Converts the PV of an SV to its UTF-8-encoded form.
3432 Forces the SV to string form if it is not already.
3433 Always sets the SvUTF8 flag to avoid future validity checks even
3434 if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
3435 will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
3436 C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
3438 This is not as a general purpose byte encoding to Unicode interface:
3439 use the Encode extension for that.
3445 Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags)
3450 if (sv == &PL_sv_undef)
3454 if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
3455 (void) sv_2pv_flags(sv,&len, flags);
3459 (void) SvPV_force(sv,len);
3467 if (SvREADONLY(sv) && SvFAKE(sv)) {
3468 sv_force_normal(sv);
3471 if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING))
3472 sv_recode_to_utf8(sv, PL_encoding);
3473 else { /* Assume Latin-1/EBCDIC */
3474 /* This function could be much more efficient if we
3475 * had a FLAG in SVs to signal if there are any hibit
3476 * chars in the PV. Given that there isn't such a flag
3477 * make the loop as fast as possible. */
3478 s = (U8 *) SvPVX(sv);
3479 e = (U8 *) SvEND(sv);
3483 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
3488 (void)SvOOK_off(sv);
3490 len = SvCUR(sv) + 1; /* Plus the \0 */
3491 SvPVX(sv) = (char*)bytes_to_utf8((U8*)s, &len);
3492 SvCUR(sv) = len - 1;
3494 Safefree(s); /* No longer using what was there before. */
3495 SvLEN(sv) = len; /* No longer know the real size. */
3497 /* Mark as UTF-8 even if no hibit - saves scanning loop */
3504 =for apidoc sv_utf8_downgrade
3506 Attempts to convert the PV of an SV from characters to bytes.
3507 If the PV contains a character beyond byte, this conversion will fail;
3508 in this case, either returns false or, if C<fail_ok> is not
3511 This is not as a general purpose Unicode to byte encoding interface:
3512 use the Encode extension for that.
3518 Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
3520 if (SvPOKp(sv) && SvUTF8(sv)) {
3525 if (SvREADONLY(sv) && SvFAKE(sv))
3526 sv_force_normal(sv);
3527 s = (U8 *) SvPV(sv, len);
3528 if (!utf8_to_bytes(s, &len)) {
3533 Perl_croak(aTHX_ "Wide character in %s",
3536 Perl_croak(aTHX_ "Wide character");
3547 =for apidoc sv_utf8_encode
3549 Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
3550 flag off so that it looks like octets again.
3556 Perl_sv_utf8_encode(pTHX_ register SV *sv)
3558 (void) sv_utf8_upgrade(sv);
3560 sv_force_normal_flags(sv, 0);
3562 if (SvREADONLY(sv)) {
3563 Perl_croak(aTHX_ PL_no_modify);
3569 =for apidoc sv_utf8_decode
3571 If the PV of the SV is an octet sequence in UTF-8
3572 and contains a multiple-byte character, the C<SvUTF8> flag is turned on
3573 so that it looks like a character. If the PV contains only single-byte
3574 characters, the C<SvUTF8> flag stays being off.
3575 Scans PV for validity and returns false if the PV is invalid UTF-8.
3581 Perl_sv_utf8_decode(pTHX_ register SV *sv)
3587 /* The octets may have got themselves encoded - get them back as
3590 if (!sv_utf8_downgrade(sv, TRUE))
3593 /* it is actually just a matter of turning the utf8 flag on, but
3594 * we want to make sure everything inside is valid utf8 first.
3596 c = (U8 *) SvPVX(sv);
3597 if (!is_utf8_string(c, SvCUR(sv)+1))
3599 e = (U8 *) SvEND(sv);
3602 if (!UTF8_IS_INVARIANT(ch)) {
3611 /* sv_setsv() is now a macro using Perl_sv_setsv_flags();
3612 * this function provided for binary compatibility only
3616 Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
3618 sv_setsv_flags(dstr, sstr, SV_GMAGIC);
3622 =for apidoc sv_setsv
3624 Copies the contents of the source SV C<ssv> into the destination SV
3625 C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3626 function if the source SV needs to be reused. Does not handle 'set' magic.
3627 Loosely speaking, it performs a copy-by-value, obliterating any previous
3628 content of the destination.
3630 You probably want to use one of the assortment of wrappers, such as
3631 C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3632 C<SvSetMagicSV_nosteal>.
3634 =for apidoc sv_setsv_flags
3636 Copies the contents of the source SV C<ssv> into the destination SV
3637 C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3638 function if the source SV needs to be reused. Does not handle 'set' magic.
3639 Loosely speaking, it performs a copy-by-value, obliterating any previous
3640 content of the destination.
3641 If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
3642 C<ssv> if appropriate, else not. If the C<flags> parameter has the
3643 C<NOSTEAL> bit set then the buffers of temps will not be stolen. <sv_setsv>
3644 and C<sv_setsv_nomg> are implemented in terms of this function.
3646 You probably want to use one of the assortment of wrappers, such as
3647 C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3648 C<SvSetMagicSV_nosteal>.
3650 This is the primary function for copying scalars, and most other
3651 copy-ish functions and macros use this underneath.
3657 Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags)
3659 register U32 sflags;
3665 SV_CHECK_THINKFIRST(dstr);
3667 sstr = &PL_sv_undef;
3668 stype = SvTYPE(sstr);
3669 dtype = SvTYPE(dstr);
3674 /* need to nuke the magic */
3676 SvRMAGICAL_off(dstr);
3679 /* There's a lot of redundancy below but we're going for speed here */
3684 if (dtype != SVt_PVGV) {
3685 (void)SvOK_off(dstr);
3693 sv_upgrade(dstr, SVt_IV);
3696 sv_upgrade(dstr, SVt_PVNV);
3700 sv_upgrade(dstr, SVt_PVIV);
3703 (void)SvIOK_only(dstr);
3704 SvIVX(dstr) = SvIVX(sstr);
3707 if (SvTAINTED(sstr))
3718 sv_upgrade(dstr, SVt_NV);
3723 sv_upgrade(dstr, SVt_PVNV);
3726 SvNVX(dstr) = SvNVX(sstr);
3727 (void)SvNOK_only(dstr);
3728 if (SvTAINTED(sstr))
3736 sv_upgrade(dstr, SVt_RV);
3737 else if (dtype == SVt_PVGV &&
3738 SvROK(sstr) && SvTYPE(SvRV(sstr)) == SVt_PVGV) {
3741 if (GvIMPORTED(dstr) != GVf_IMPORTED
3742 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3744 GvIMPORTED_on(dstr);
3755 sv_upgrade(dstr, SVt_PV);
3758 if (dtype < SVt_PVIV)
3759 sv_upgrade(dstr, SVt_PVIV);
3762 if (dtype < SVt_PVNV)
3763 sv_upgrade(dstr, SVt_PVNV);
3770 Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0),
3773 Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0));
3777 if (dtype <= SVt_PVGV) {
3779 if (dtype != SVt_PVGV) {
3780 char *name = GvNAME(sstr);
3781 STRLEN len = GvNAMELEN(sstr);
3782 sv_upgrade(dstr, SVt_PVGV);
3783 sv_magic(dstr, dstr, PERL_MAGIC_glob, Nullch, 0);
3784 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
3785 GvNAME(dstr) = savepvn(name, len);
3786 GvNAMELEN(dstr) = len;
3787 SvFAKE_on(dstr); /* can coerce to non-glob */
3789 /* ahem, death to those who redefine active sort subs */
3790 else if (PL_curstackinfo->si_type == PERLSI_SORT
3791 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
3792 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
3795 #ifdef GV_UNIQUE_CHECK
3796 if (GvUNIQUE((GV*)dstr)) {
3797 Perl_croak(aTHX_ PL_no_modify);
3801 (void)SvOK_off(dstr);
3802 GvINTRO_off(dstr); /* one-shot flag */
3804 GvGP(dstr) = gp_ref(GvGP(sstr));
3805 if (SvTAINTED(sstr))
3807 if (GvIMPORTED(dstr) != GVf_IMPORTED
3808 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3810 GvIMPORTED_on(dstr);
3818 if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
3820 if ((int)SvTYPE(sstr) != stype) {
3821 stype = SvTYPE(sstr);
3822 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
3826 if (stype == SVt_PVLV)
3827 (void)SvUPGRADE(dstr, SVt_PVNV);
3829 (void)SvUPGRADE(dstr, (U32)stype);
3832 sflags = SvFLAGS(sstr);
3834 if (sflags & SVf_ROK) {
3835 if (dtype >= SVt_PV) {
3836 if (dtype == SVt_PVGV) {
3837 SV *sref = SvREFCNT_inc(SvRV(sstr));
3839 int intro = GvINTRO(dstr);
3841 #ifdef GV_UNIQUE_CHECK
3842 if (GvUNIQUE((GV*)dstr)) {
3843 Perl_croak(aTHX_ PL_no_modify);
3848 GvINTRO_off(dstr); /* one-shot flag */
3849 GvLINE(dstr) = CopLINE(PL_curcop);
3850 GvEGV(dstr) = (GV*)dstr;
3853 switch (SvTYPE(sref)) {
3856 SAVEGENERICSV(GvAV(dstr));
3858 dref = (SV*)GvAV(dstr);
3859 GvAV(dstr) = (AV*)sref;
3860 if (!GvIMPORTED_AV(dstr)
3861 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3863 GvIMPORTED_AV_on(dstr);
3868 SAVEGENERICSV(GvHV(dstr));
3870 dref = (SV*)GvHV(dstr);
3871 GvHV(dstr) = (HV*)sref;
3872 if (!GvIMPORTED_HV(dstr)
3873 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3875 GvIMPORTED_HV_on(dstr);
3880 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
3881 SvREFCNT_dec(GvCV(dstr));
3882 GvCV(dstr) = Nullcv;
3883 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3884 PL_sub_generation++;
3886 SAVEGENERICSV(GvCV(dstr));
3889 dref = (SV*)GvCV(dstr);
3890 if (GvCV(dstr) != (CV*)sref) {
3891 CV* cv = GvCV(dstr);
3893 if (!GvCVGEN((GV*)dstr) &&
3894 (CvROOT(cv) || CvXSUB(cv)))
3896 /* ahem, death to those who redefine
3897 * active sort subs */
3898 if (PL_curstackinfo->si_type == PERLSI_SORT &&
3899 PL_sortcop == CvSTART(cv))
3901 "Can't redefine active sort subroutine %s",
3902 GvENAME((GV*)dstr));
3903 /* Redefining a sub - warning is mandatory if
3904 it was a const and its value changed. */
3905 if (ckWARN(WARN_REDEFINE)
3907 && (!CvCONST((CV*)sref)
3908 || sv_cmp(cv_const_sv(cv),
3909 cv_const_sv((CV*)sref)))))
3911 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
3913 ? "Constant subroutine %s::%s redefined"
3914 : "Subroutine %s::%s redefined",
3915 HvNAME(GvSTASH((GV*)dstr)),
3916 GvENAME((GV*)dstr));
3920 cv_ckproto(cv, (GV*)dstr,
3921 SvPOK(sref) ? SvPVX(sref) : Nullch);
3923 GvCV(dstr) = (CV*)sref;
3924 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3925 GvASSUMECV_on(dstr);
3926 PL_sub_generation++;
3928 if (!GvIMPORTED_CV(dstr)
3929 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3931 GvIMPORTED_CV_on(dstr);
3936 SAVEGENERICSV(GvIOp(dstr));
3938 dref = (SV*)GvIOp(dstr);
3939 GvIOp(dstr) = (IO*)sref;
3943 SAVEGENERICSV(GvFORM(dstr));
3945 dref = (SV*)GvFORM(dstr);
3946 GvFORM(dstr) = (CV*)sref;
3950 SAVEGENERICSV(GvSV(dstr));
3952 dref = (SV*)GvSV(dstr);
3954 if (!GvIMPORTED_SV(dstr)
3955 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3957 GvIMPORTED_SV_on(dstr);
3963 if (SvTAINTED(sstr))
3968 (void)SvOOK_off(dstr); /* backoff */
3970 Safefree(SvPVX(dstr));
3971 SvLEN(dstr)=SvCUR(dstr)=0;
3974 (void)SvOK_off(dstr);
3975 SvRV(dstr) = SvREFCNT_inc(SvRV(sstr));
3977 if (sflags & SVp_NOK) {
3979 /* Only set the public OK flag if the source has public OK. */
3980 if (sflags & SVf_NOK)
3981 SvFLAGS(dstr) |= SVf_NOK;
3982 SvNVX(dstr) = SvNVX(sstr);
3984 if (sflags & SVp_IOK) {
3985 (void)SvIOKp_on(dstr);
3986 if (sflags & SVf_IOK)
3987 SvFLAGS(dstr) |= SVf_IOK;
3988 if (sflags & SVf_IVisUV)
3990 SvIVX(dstr) = SvIVX(sstr);
3992 if (SvAMAGIC(sstr)) {
3996 else if (sflags & SVp_POK) {
3999 * Check to see if we can just swipe the string. If so, it's a
4000 * possible small lose on short strings, but a big win on long ones.
4001 * It might even be a win on short strings if SvPVX(dstr)
4002 * has to be allocated and SvPVX(sstr) has to be freed.
4005 if (SvTEMP(sstr) && /* slated for free anyway? */
4006 SvREFCNT(sstr) == 1 && /* and no other references to it? */
4007 (!(flags & SV_NOSTEAL)) && /* and we're allowed to steal temps */
4008 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
4009 SvLEN(sstr) && /* and really is a string */
4010 /* and won't be needed again, potentially */
4011 !(PL_op && PL_op->op_type == OP_AASSIGN))
4013 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
4015 SvFLAGS(dstr) &= ~SVf_OOK;
4016 Safefree(SvPVX(dstr) - SvIVX(dstr));
4018 else if (SvLEN(dstr))
4019 Safefree(SvPVX(dstr));
4021 (void)SvPOK_only(dstr);
4022 SvPV_set(dstr, SvPVX(sstr));
4023 SvLEN_set(dstr, SvLEN(sstr));
4024 SvCUR_set(dstr, SvCUR(sstr));
4027 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
4028 SvPV_set(sstr, Nullch);
4033 else { /* have to copy actual string */
4034 STRLEN len = SvCUR(sstr);
4035 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
4036 Move(SvPVX(sstr),SvPVX(dstr),len,char);
4037 SvCUR_set(dstr, len);
4038 *SvEND(dstr) = '\0';
4039 (void)SvPOK_only(dstr);
4041 if (sflags & SVf_UTF8)
4044 if (sflags & SVp_NOK) {
4046 if (sflags & SVf_NOK)
4047 SvFLAGS(dstr) |= SVf_NOK;
4048 SvNVX(dstr) = SvNVX(sstr);
4050 if (sflags & SVp_IOK) {
4051 (void)SvIOKp_on(dstr);
4052 if (sflags & SVf_IOK)
4053 SvFLAGS(dstr) |= SVf_IOK;
4054 if (sflags & SVf_IVisUV)
4056 SvIVX(dstr) = SvIVX(sstr);
4058 if ( SvVOK(sstr) ) {
4059 MAGIC *smg = mg_find(sstr,PERL_MAGIC_vstring);
4060 sv_magic(dstr, NULL, PERL_MAGIC_vstring,
4061 smg->mg_ptr, smg->mg_len);
4062 SvRMAGICAL_on(dstr);
4065 else if (sflags & SVp_IOK) {
4066 if (sflags & SVf_IOK)
4067 (void)SvIOK_only(dstr);
4069 (void)SvOK_off(dstr);
4070 (void)SvIOKp_on(dstr);
4072 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
4073 if (sflags & SVf_IVisUV)
4075 SvIVX(dstr) = SvIVX(sstr);
4076 if (sflags & SVp_NOK) {
4077 if (sflags & SVf_NOK)
4078 (void)SvNOK_on(dstr);
4080 (void)SvNOKp_on(dstr);
4081 SvNVX(dstr) = SvNVX(sstr);
4084 else if (sflags & SVp_NOK) {
4085 if (sflags & SVf_NOK)
4086 (void)SvNOK_only(dstr);
4088 (void)SvOK_off(dstr);
4091 SvNVX(dstr) = SvNVX(sstr);
4094 if (dtype == SVt_PVGV) {
4095 if (ckWARN(WARN_MISC))
4096 Perl_warner(aTHX_ packWARN(WARN_MISC), "Undefined value assigned to typeglob");
4099 (void)SvOK_off(dstr);
4101 if (SvTAINTED(sstr))
4106 =for apidoc sv_setsv_mg
4108 Like C<sv_setsv>, but also handles 'set' magic.
4114 Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
4116 sv_setsv(dstr,sstr);
4121 =for apidoc sv_setpvn
4123 Copies a string into an SV. The C<len> parameter indicates the number of
4124 bytes to be copied. If the C<ptr> argument is NULL the SV will become
4125 undefined. Does not handle 'set' magic. See C<sv_setpvn_mg>.
4131 Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
4133 register char *dptr;
4135 SV_CHECK_THINKFIRST(sv);
4141 /* len is STRLEN which is unsigned, need to copy to signed */
4144 Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen");
4146 (void)SvUPGRADE(sv, SVt_PV);
4148 SvGROW(sv, len + 1);
4150 Move(ptr,dptr,len,char);
4153 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4158 =for apidoc sv_setpvn_mg
4160 Like C<sv_setpvn>, but also handles 'set' magic.
4166 Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
4168 sv_setpvn(sv,ptr,len);
4173 =for apidoc sv_setpv
4175 Copies a string into an SV. The string must be null-terminated. Does not
4176 handle 'set' magic. See C<sv_setpv_mg>.
4182 Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
4184 register STRLEN len;
4186 SV_CHECK_THINKFIRST(sv);
4192 (void)SvUPGRADE(sv, SVt_PV);
4194 SvGROW(sv, len + 1);
4195 Move(ptr,SvPVX(sv),len+1,char);
4197 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4202 =for apidoc sv_setpv_mg
4204 Like C<sv_setpv>, but also handles 'set' magic.
4210 Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
4217 =for apidoc sv_usepvn
4219 Tells an SV to use C<ptr> to find its string value. Normally the string is
4220 stored inside the SV but sv_usepvn allows the SV to use an outside string.
4221 The C<ptr> should point to memory that was allocated by C<malloc>. The
4222 string length, C<len>, must be supplied. This function will realloc the
4223 memory pointed to by C<ptr>, so that pointer should not be freed or used by
4224 the programmer after giving it to sv_usepvn. Does not handle 'set' magic.
4225 See C<sv_usepvn_mg>.
4231 Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
4233 SV_CHECK_THINKFIRST(sv);
4234 (void)SvUPGRADE(sv, SVt_PV);
4239 (void)SvOOK_off(sv);
4240 if (SvPVX(sv) && SvLEN(sv))
4241 Safefree(SvPVX(sv));
4242 Renew(ptr, len+1, char);
4245 SvLEN_set(sv, len+1);
4247 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4252 =for apidoc sv_usepvn_mg
4254 Like C<sv_usepvn>, but also handles 'set' magic.
4260 Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
4262 sv_usepvn(sv,ptr,len);
4267 =for apidoc sv_force_normal_flags
4269 Undo various types of fakery on an SV: if the PV is a shared string, make
4270 a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
4271 an xpvmg. The C<flags> parameter gets passed to C<sv_unref_flags()>
4272 when unrefing. C<sv_force_normal> calls this function with flags set to 0.
4278 Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags)
4280 if (SvREADONLY(sv)) {
4282 char *pvx = SvPVX(sv);
4283 STRLEN len = SvCUR(sv);
4284 U32 hash = SvUVX(sv);
4287 SvGROW(sv, len + 1);
4288 Move(pvx,SvPVX(sv),len,char);
4290 unsharepvn(pvx, SvUTF8(sv) ? -(I32)len : len, hash);
4292 else if (IN_PERL_RUNTIME)
4293 Perl_croak(aTHX_ PL_no_modify);
4296 sv_unref_flags(sv, flags);
4297 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
4302 =for apidoc sv_force_normal
4304 Undo various types of fakery on an SV: if the PV is a shared string, make
4305 a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
4306 an xpvmg. See also C<sv_force_normal_flags>.
4312 Perl_sv_force_normal(pTHX_ register SV *sv)
4314 sv_force_normal_flags(sv, 0);
4320 Efficient removal of characters from the beginning of the string buffer.
4321 SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside
4322 the string buffer. The C<ptr> becomes the first character of the adjusted
4323 string. Uses the "OOK hack".
4324 Beware: after this function returns, C<ptr> and SvPVX(sv) may no longer
4325 refer to the same chunk of data.
4331 Perl_sv_chop(pTHX_ register SV *sv, register char *ptr)
4333 register STRLEN delta;
4334 if (!ptr || !SvPOKp(sv))
4336 delta = ptr - SvPVX(sv);
4337 SV_CHECK_THINKFIRST(sv);
4338 if (SvTYPE(sv) < SVt_PVIV)
4339 sv_upgrade(sv,SVt_PVIV);
4342 if (!SvLEN(sv)) { /* make copy of shared string */
4343 char *pvx = SvPVX(sv);
4344 STRLEN len = SvCUR(sv);
4345 SvGROW(sv, len + 1);
4346 Move(pvx,SvPVX(sv),len,char);
4350 /* Same SvOOK_on but SvOOK_on does a SvIOK_off
4351 and we do that anyway inside the SvNIOK_off
4353 SvFLAGS(sv) |= SVf_OOK;
4362 /* sv_catpvn() is now a macro using Perl_sv_catpvn_flags();
4363 * this function provided for binary compatibility only
4367 Perl_sv_catpvn(pTHX_ SV *dsv, const char* sstr, STRLEN slen)
4369 sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC);
4373 =for apidoc sv_catpvn
4375 Concatenates the string onto the end of the string which is in the SV. The
4376 C<len> indicates number of bytes to copy. If the SV has the UTF-8
4377 status set, then the bytes appended should be valid UTF-8.
4378 Handles 'get' magic, but not 'set' magic. See C<sv_catpvn_mg>.
4380 =for apidoc sv_catpvn_flags
4382 Concatenates the string onto the end of the string which is in the SV. The
4383 C<len> indicates number of bytes to copy. If the SV has the UTF-8
4384 status set, then the bytes appended should be valid UTF-8.
4385 If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<dsv> if
4386 appropriate, else not. C<sv_catpvn> and C<sv_catpvn_nomg> are implemented
4387 in terms of this function.
4393 Perl_sv_catpvn_flags(pTHX_ register SV *dsv, register const char *sstr, register STRLEN slen, I32 flags)
4398 dstr = SvPV_force_flags(dsv, dlen, flags);
4399 SvGROW(dsv, dlen + slen + 1);
4402 Move(sstr, SvPVX(dsv) + dlen, slen, char);
4405 (void)SvPOK_only_UTF8(dsv); /* validate pointer */
4410 =for apidoc sv_catpvn_mg
4412 Like C<sv_catpvn>, but also handles 'set' magic.
4418 Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
4420 sv_catpvn(sv,ptr,len);
4424 /* sv_catsv() is now a macro using Perl_sv_catsv_flags();
4425 * this function provided for binary compatibility only
4429 Perl_sv_catsv(pTHX_ SV *dstr, register SV *sstr)
4431 sv_catsv_flags(dstr, sstr, SV_GMAGIC);
4435 =for apidoc sv_catsv
4437 Concatenates the string from SV C<ssv> onto the end of the string in
4438 SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but
4439 not 'set' magic. See C<sv_catsv_mg>.
4441 =for apidoc sv_catsv_flags
4443 Concatenates the string from SV C<ssv> onto the end of the string in
4444 SV C<dsv>. Modifies C<dsv> but not C<ssv>. If C<flags> has C<SV_GMAGIC>
4445 bit set, will C<mg_get> on the SVs if appropriate, else not. C<sv_catsv>
4446 and C<sv_catsv_nomg> are implemented in terms of this function.
4451 Perl_sv_catsv_flags(pTHX_ SV *dsv, register SV *ssv, I32 flags)
4457 if ((spv = SvPV(ssv, slen))) {
4458 /* sutf8 and dutf8 were type bool, but under USE_ITHREADS,
4459 gcc version 2.95.2 20000220 (Debian GNU/Linux) for
4460 Linux xxx 2.2.17 on sparc64 with gcc -O2, we erroneously
4461 get dutf8 = 0x20000000, (i.e. SVf_UTF8) even though
4462 dsv->sv_flags doesn't have that bit set.
4463 Andy Dougherty 12 Oct 2001
4465 I32 sutf8 = DO_UTF8(ssv);
4468 if (SvGMAGICAL(dsv) && (flags & SV_GMAGIC))
4470 dutf8 = DO_UTF8(dsv);
4472 if (dutf8 != sutf8) {
4474 /* Not modifying source SV, so taking a temporary copy. */
4475 SV* csv = sv_2mortal(newSVpvn(spv, slen));
4477 sv_utf8_upgrade(csv);
4478 spv = SvPV(csv, slen);
4481 sv_utf8_upgrade_nomg(dsv);
4483 sv_catpvn_nomg(dsv, spv, slen);
4488 =for apidoc sv_catsv_mg
4490 Like C<sv_catsv>, but also handles 'set' magic.
4496 Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv)
4503 =for apidoc sv_catpv
4505 Concatenates the string onto the end of the string which is in the SV.
4506 If the SV has the UTF-8 status set, then the bytes appended should be
4507 valid UTF-8. Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>.
4512 Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr)
4514 register STRLEN len;
4520 junk = SvPV_force(sv, tlen);
4522 SvGROW(sv, tlen + len + 1);
4525 Move(ptr,SvPVX(sv)+tlen,len+1,char);
4527 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4532 =for apidoc sv_catpv_mg
4534 Like C<sv_catpv>, but also handles 'set' magic.
4540 Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr)
4549 Create a new null SV, or if len > 0, create a new empty SVt_PV type SV
4550 with an initial PV allocation of len+1. Normally accessed via the C<NEWSV>
4557 Perl_newSV(pTHX_ STRLEN len)
4563 sv_upgrade(sv, SVt_PV);
4564 SvGROW(sv, len + 1);
4569 =for apidoc sv_magicext
4571 Adds magic to an SV, upgrading it if necessary. Applies the
4572 supplied vtable and returns a pointer to the magic added.
4574 Note that C<sv_magicext> will allow things that C<sv_magic> will not.
4575 In particular, you can add magic to SvREADONLY SVs, and add more than
4576 one instance of the same 'how'.
4578 If C<namlen> is greater than zero then a C<savepvn> I<copy> of C<name> is
4579 stored, if C<namlen> is zero then C<name> is stored as-is and - as another
4580 special case - if C<(name && namlen == HEf_SVKEY)> then C<name> is assumed
4581 to contain an C<SV*> and is stored as-is with its REFCNT incremented.
4583 (This is now used as a subroutine by C<sv_magic>.)
4588 Perl_sv_magicext(pTHX_ SV* sv, SV* obj, int how, MGVTBL *vtable,
4589 const char* name, I32 namlen)
4593 if (SvTYPE(sv) < SVt_PVMG) {
4594 (void)SvUPGRADE(sv, SVt_PVMG);
4596 Newz(702,mg, 1, MAGIC);
4597 mg->mg_moremagic = SvMAGIC(sv);
4600 /* Some magic sontains a reference loop, where the sv and object refer to
4601 each other. To prevent a reference loop that would prevent such
4602 objects being freed, we look for such loops and if we find one we
4603 avoid incrementing the object refcount.
4605 Note we cannot do this to avoid self-tie loops as intervening RV must
4606 have its REFCNT incremented to keep it in existence.
4609 if (!obj || obj == sv ||
4610 how == PERL_MAGIC_arylen ||
4611 how == PERL_MAGIC_qr ||
4612 (SvTYPE(obj) == SVt_PVGV &&
4613 (GvSV(obj) == sv || GvHV(obj) == (HV*)sv || GvAV(obj) == (AV*)sv ||
4614 GvCV(obj) == (CV*)sv || GvIOp(obj) == (IO*)sv ||
4615 GvFORM(obj) == (CV*)sv)))
4620 mg->mg_obj = SvREFCNT_inc(obj);
4621 mg->mg_flags |= MGf_REFCOUNTED;
4624 /* Normal self-ties simply pass a null object, and instead of
4625 using mg_obj directly, use the SvTIED_obj macro to produce a
4626 new RV as needed. For glob "self-ties", we are tieing the PVIO
4627 with an RV obj pointing to the glob containing the PVIO. In
4628 this case, to avoid a reference loop, we need to weaken the
4632 if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO &&
4633 obj && SvROK(obj) && GvIO(SvRV(obj)) == (IO*)sv)
4639 mg->mg_len = namlen;
4642 mg->mg_ptr = savepvn(name, namlen);
4643 else if (namlen == HEf_SVKEY)
4644 mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
4646 mg->mg_ptr = (char *) name;
4648 mg->mg_virtual = vtable;
4652 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
4657 =for apidoc sv_magic
4659 Adds magic to an SV. First upgrades C<sv> to type C<SVt_PVMG> if necessary,
4660 then adds a new magic item of type C<how> to the head of the magic list.
4662 See C<sv_magicext> (which C<sv_magic> now calls) for a description of the
4663 handling of the C<name> and C<namlen> arguments.
4669 Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen)
4674 if (SvREADONLY(sv)) {
4676 && how != PERL_MAGIC_regex_global
4677 && how != PERL_MAGIC_bm
4678 && how != PERL_MAGIC_fm
4679 && how != PERL_MAGIC_sv
4680 && how != PERL_MAGIC_backref
4683 Perl_croak(aTHX_ PL_no_modify);
4686 if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) {
4687 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
4688 /* sv_magic() refuses to add a magic of the same 'how' as an
4691 if (how == PERL_MAGIC_taint)
4699 vtable = &PL_vtbl_sv;
4701 case PERL_MAGIC_overload:
4702 vtable = &PL_vtbl_amagic;
4704 case PERL_MAGIC_overload_elem:
4705 vtable = &PL_vtbl_amagicelem;
4707 case PERL_MAGIC_overload_table:
4708 vtable = &PL_vtbl_ovrld;
4711 vtable = &PL_vtbl_bm;
4713 case PERL_MAGIC_regdata:
4714 vtable = &PL_vtbl_regdata;
4716 case PERL_MAGIC_regdatum:
4717 vtable = &PL_vtbl_regdatum;
4719 case PERL_MAGIC_env:
4720 vtable = &PL_vtbl_env;
4723 vtable = &PL_vtbl_fm;
4725 case PERL_MAGIC_envelem:
4726 vtable = &PL_vtbl_envelem;
4728 case PERL_MAGIC_regex_global:
4729 vtable = &PL_vtbl_mglob;
4731 case PERL_MAGIC_isa:
4732 vtable = &PL_vtbl_isa;
4734 case PERL_MAGIC_isaelem:
4735 vtable = &PL_vtbl_isaelem;
4737 case PERL_MAGIC_nkeys:
4738 vtable = &PL_vtbl_nkeys;
4740 case PERL_MAGIC_dbfile:
4743 case PERL_MAGIC_dbline:
4744 vtable = &PL_vtbl_dbline;
4746 #ifdef USE_5005THREADS
4747 case PERL_MAGIC_mutex:
4748 vtable = &PL_vtbl_mutex;
4750 #endif /* USE_5005THREADS */
4751 #ifdef USE_LOCALE_COLLATE
4752 case PERL_MAGIC_collxfrm:
4753 vtable = &PL_vtbl_collxfrm;
4755 #endif /* USE_LOCALE_COLLATE */
4756 case PERL_MAGIC_tied:
4757 vtable = &PL_vtbl_pack;
4759 case PERL_MAGIC_tiedelem:
4760 case PERL_MAGIC_tiedscalar:
4761 vtable = &PL_vtbl_packelem;
4764 vtable = &PL_vtbl_regexp;
4766 case PERL_MAGIC_sig:
4767 vtable = &PL_vtbl_sig;
4769 case PERL_MAGIC_sigelem:
4770 vtable = &PL_vtbl_sigelem;
4772 case PERL_MAGIC_taint:
4773 vtable = &PL_vtbl_taint;
4775 case PERL_MAGIC_uvar:
4776 vtable = &PL_vtbl_uvar;
4778 case PERL_MAGIC_vec:
4779 vtable = &PL_vtbl_vec;
4781 case PERL_MAGIC_vstring:
4784 case PERL_MAGIC_utf8:
4785 vtable = &PL_vtbl_utf8;
4787 case PERL_MAGIC_substr:
4788 vtable = &PL_vtbl_substr;
4790 case PERL_MAGIC_defelem:
4791 vtable = &PL_vtbl_defelem;
4793 case PERL_MAGIC_glob:
4794 vtable = &PL_vtbl_glob;
4796 case PERL_MAGIC_arylen:
4797 vtable = &PL_vtbl_arylen;
4799 case PERL_MAGIC_pos:
4800 vtable = &PL_vtbl_pos;
4802 case PERL_MAGIC_backref:
4803 vtable = &PL_vtbl_backref;
4805 case PERL_MAGIC_ext:
4806 /* Reserved for use by extensions not perl internals. */
4807 /* Useful for attaching extension internal data to perl vars. */
4808 /* Note that multiple extensions may clash if magical scalars */
4809 /* etc holding private data from one are passed to another. */
4812 Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how);
4815 /* Rest of work is done else where */
4816 mg = sv_magicext(sv,obj,how,vtable,name,namlen);
4819 case PERL_MAGIC_taint:
4822 case PERL_MAGIC_ext:
4823 case PERL_MAGIC_dbfile:
4830 =for apidoc sv_unmagic
4832 Removes all magic of type C<type> from an SV.
4838 Perl_sv_unmagic(pTHX_ SV *sv, int type)
4842 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
4845 for (mg = *mgp; mg; mg = *mgp) {
4846 if (mg->mg_type == type) {
4847 MGVTBL* vtbl = mg->mg_virtual;
4848 *mgp = mg->mg_moremagic;
4849 if (vtbl && vtbl->svt_free)
4850 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
4851 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
4853 Safefree(mg->mg_ptr);
4854 else if (mg->mg_len == HEf_SVKEY)
4855 SvREFCNT_dec((SV*)mg->mg_ptr);
4856 else if (mg->mg_type == PERL_MAGIC_utf8 && mg->mg_ptr)
4857 Safefree(mg->mg_ptr);
4859 if (mg->mg_flags & MGf_REFCOUNTED)
4860 SvREFCNT_dec(mg->mg_obj);
4864 mgp = &mg->mg_moremagic;
4868 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
4875 =for apidoc sv_rvweaken
4877 Weaken a reference: set the C<SvWEAKREF> flag on this RV; give the
4878 referred-to SV C<PERL_MAGIC_backref> magic if it hasn't already; and
4879 push a back-reference to this RV onto the array of backreferences
4880 associated with that magic.
4886 Perl_sv_rvweaken(pTHX_ SV *sv)
4889 if (!SvOK(sv)) /* let undefs pass */
4892 Perl_croak(aTHX_ "Can't weaken a nonreference");
4893 else if (SvWEAKREF(sv)) {
4894 if (ckWARN(WARN_MISC))
4895 Perl_warner(aTHX_ packWARN(WARN_MISC), "Reference is already weak");
4899 sv_add_backref(tsv, sv);
4905 /* Give tsv backref magic if it hasn't already got it, then push a
4906 * back-reference to sv onto the array associated with the backref magic.
4910 S_sv_add_backref(pTHX_ SV *tsv, SV *sv)
4914 if (SvMAGICAL(tsv) && (mg = mg_find(tsv, PERL_MAGIC_backref)))
4915 av = (AV*)mg->mg_obj;
4918 sv_magic(tsv, (SV*)av, PERL_MAGIC_backref, NULL, 0);
4919 /* av now has a refcnt of 2, which avoids it getting freed
4920 * before us during global cleanup. The extra ref is removed
4921 * by magic_killbackrefs() when tsv is being freed */
4923 if (AvFILLp(av) >= AvMAX(av)) {
4925 SV **svp = AvARRAY(av);
4926 for (i = AvFILLp(av); i >= 0; i--)
4928 svp[i] = sv; /* reuse the slot */
4931 av_extend(av, AvFILLp(av)+1);
4933 AvARRAY(av)[++AvFILLp(av)] = sv; /* av_push() */
4936 /* delete a back-reference to ourselves from the backref magic associated
4937 * with the SV we point to.
4941 S_sv_del_backref(pTHX_ SV *sv)
4948 if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, PERL_MAGIC_backref)))
4949 Perl_croak(aTHX_ "panic: del_backref");
4950 av = (AV *)mg->mg_obj;
4952 for (i = AvFILLp(av); i >= 0; i--)
4953 if (svp[i] == sv) svp[i] = Nullsv;
4957 =for apidoc sv_insert
4959 Inserts a string at the specified offset/length within the SV. Similar to
4960 the Perl substr() function.
4966 Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, char *little, STRLEN littlelen)
4970 register char *midend;
4971 register char *bigend;
4977 Perl_croak(aTHX_ "Can't modify non-existent substring");
4978 SvPV_force(bigstr, curlen);
4979 (void)SvPOK_only_UTF8(bigstr);
4980 if (offset + len > curlen) {
4981 SvGROW(bigstr, offset+len+1);
4982 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
4983 SvCUR_set(bigstr, offset+len);
4987 i = littlelen - len;
4988 if (i > 0) { /* string might grow */
4989 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
4990 mid = big + offset + len;
4991 midend = bigend = big + SvCUR(bigstr);
4994 while (midend > mid) /* shove everything down */
4995 *--bigend = *--midend;
4996 Move(little,big+offset,littlelen,char);
5002 Move(little,SvPVX(bigstr)+offset,len,char);
5007 big = SvPVX(bigstr);
5010 bigend = big + SvCUR(bigstr);
5012 if (midend > bigend)
5013 Perl_croak(aTHX_ "panic: sv_insert");
5015 if (mid - big > bigend - midend) { /* faster to shorten from end */
5017 Move(little, mid, littlelen,char);
5020 i = bigend - midend;
5022 Move(midend, mid, i,char);
5026 SvCUR_set(bigstr, mid - big);
5029 else if ((i = mid - big)) { /* faster from front */
5030 midend -= littlelen;
5032 sv_chop(bigstr,midend-i);
5037 Move(little, mid, littlelen,char);
5039 else if (littlelen) {
5040 midend -= littlelen;
5041 sv_chop(bigstr,midend);
5042 Move(little,midend,littlelen,char);
5045 sv_chop(bigstr,midend);
5051 =for apidoc sv_replace
5053 Make the first argument a copy of the second, then delete the original.
5054 The target SV physically takes over ownership of the body of the source SV
5055 and inherits its flags; however, the target keeps any magic it owns,
5056 and any magic in the source is discarded.
5057 Note that this is a rather specialist SV copying operation; most of the
5058 time you'll want to use C<sv_setsv> or one of its many macro front-ends.
5064 Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv)
5066 U32 refcnt = SvREFCNT(sv);
5067 SV_CHECK_THINKFIRST(sv);
5068 if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL))
5069 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "Reference miscount in sv_replace()");
5070 if (SvMAGICAL(sv)) {
5074 sv_upgrade(nsv, SVt_PVMG);
5075 SvMAGIC(nsv) = SvMAGIC(sv);
5076 SvFLAGS(nsv) |= SvMAGICAL(sv);
5082 assert(!SvREFCNT(sv));
5083 StructCopy(nsv,sv,SV);
5084 SvREFCNT(sv) = refcnt;
5085 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
5091 =for apidoc sv_clear
5093 Clear an SV: call any destructors, free up any memory used by the body,
5094 and free the body itself. The SV's head is I<not> freed, although
5095 its type is set to all 1's so that it won't inadvertently be assumed
5096 to be live during global destruction etc.
5097 This function should only be called when REFCNT is zero. Most of the time
5098 you'll want to call C<sv_free()> (or its macro wrapper C<SvREFCNT_dec>)
5105 Perl_sv_clear(pTHX_ register SV *sv)
5109 assert(SvREFCNT(sv) == 0);
5112 if (PL_defstash) { /* Still have a symbol table? */
5119 stash = SvSTASH(sv);
5120 destructor = StashHANDLER(stash,DESTROY);
5122 SV* tmpref = newRV(sv);
5123 SvREADONLY_on(tmpref); /* DESTROY() could be naughty */
5125 PUSHSTACKi(PERLSI_DESTROY);
5130 call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR|G_VOID);
5136 if(SvREFCNT(tmpref) < 2) {
5137 /* tmpref is not kept alive! */
5142 SvREFCNT_dec(tmpref);
5144 } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
5148 if (PL_in_clean_objs)
5149 Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'",
5151 /* DESTROY gave object new lease on life */
5157 SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */
5158 SvOBJECT_off(sv); /* Curse the object. */
5159 if (SvTYPE(sv) != SVt_PVIO)
5160 --PL_sv_objcount; /* XXX Might want something more general */
5163 if (SvTYPE(sv) >= SVt_PVMG) {
5166 if (SvFLAGS(sv) & SVpad_TYPED)
5167 SvREFCNT_dec(SvSTASH(sv));
5170 switch (SvTYPE(sv)) {
5173 IoIFP(sv) != PerlIO_stdin() &&
5174 IoIFP(sv) != PerlIO_stdout() &&
5175 IoIFP(sv) != PerlIO_stderr())
5177 io_close((IO*)sv, FALSE);
5179 if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP))
5180 PerlDir_close(IoDIRP(sv));
5181 IoDIRP(sv) = (DIR*)NULL;
5182 Safefree(IoTOP_NAME(sv));
5183 Safefree(IoFMT_NAME(sv));
5184 Safefree(IoBOTTOM_NAME(sv));
5199 if (LvTYPE(sv) == 'T') { /* for tie: return HE to pool */
5200 SvREFCNT_dec(HeKEY_sv((HE*)LvTARG(sv)));
5201 HeNEXT((HE*)LvTARG(sv)) = PL_hv_fetch_ent_mh;
5202 PL_hv_fetch_ent_mh = (HE*)LvTARG(sv);
5204 else if (LvTYPE(sv) != 't') /* unless tie: unrefcnted fake SV** */
5205 SvREFCNT_dec(LvTARG(sv));
5209 Safefree(GvNAME(sv));
5210 /* cannot decrease stash refcount yet, as we might recursively delete
5211 ourselves when the refcnt drops to zero. Delay SvREFCNT_dec
5212 of stash until current sv is completely gone.
5213 -- JohnPC, 27 Mar 1998 */
5214 stash = GvSTASH(sv);
5228 SvREFCNT_dec(SvRV(sv));
5230 else if (SvPVX(sv) && SvLEN(sv))
5231 Safefree(SvPVX(sv));
5232 else if (SvPVX(sv) && SvREADONLY(sv) && SvFAKE(sv)) {
5233 unsharepvn(SvPVX(sv),
5234 SvUTF8(sv) ? -(I32)SvCUR(sv) : SvCUR(sv),
5247 switch (SvTYPE(sv)) {
5263 del_XPVIV(SvANY(sv));
5266 del_XPVNV(SvANY(sv));
5269 del_XPVMG(SvANY(sv));
5272 del_XPVLV(SvANY(sv));
5275 del_XPVAV(SvANY(sv));
5278 del_XPVHV(SvANY(sv));
5281 del_XPVCV(SvANY(sv));
5284 del_XPVGV(SvANY(sv));
5285 /* code duplication for increased performance. */
5286 SvFLAGS(sv) &= SVf_BREAK;
5287 SvFLAGS(sv) |= SVTYPEMASK;
5288 /* decrease refcount of the stash that owns this GV, if any */
5290 SvREFCNT_dec(stash);
5291 return; /* not break, SvFLAGS reset already happened */
5293 del_XPVBM(SvANY(sv));
5296 del_XPVFM(SvANY(sv));
5299 del_XPVIO(SvANY(sv));
5302 SvFLAGS(sv) &= SVf_BREAK;
5303 SvFLAGS(sv) |= SVTYPEMASK;
5307 =for apidoc sv_newref
5309 Increment an SV's reference count. Use the C<SvREFCNT_inc()> wrapper
5316 Perl_sv_newref(pTHX_ SV *sv)
5319 ATOMIC_INC(SvREFCNT(sv));
5326 Decrement an SV's reference count, and if it drops to zero, call
5327 C<sv_clear> to invoke destructors and free up any memory used by
5328 the body; finally, deallocate the SV's head itself.
5329 Normally called via a wrapper macro C<SvREFCNT_dec>.
5335 Perl_sv_free(pTHX_ SV *sv)
5337 int refcount_is_zero;
5341 if (SvREFCNT(sv) == 0) {
5342 if (SvFLAGS(sv) & SVf_BREAK)
5343 /* this SV's refcnt has been artificially decremented to
5344 * trigger cleanup */
5346 if (PL_in_clean_all) /* All is fair */
5348 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
5349 /* make sure SvREFCNT(sv)==0 happens very seldom */
5350 SvREFCNT(sv) = (~(U32)0)/2;
5353 if (ckWARN_d(WARN_INTERNAL))
5354 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
5355 "Attempt to free unreferenced scalar: SV 0x%"UVxf
5356 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
5359 ATOMIC_DEC_AND_TEST(refcount_is_zero, SvREFCNT(sv));
5360 if (!refcount_is_zero)
5364 if (ckWARN_d(WARN_DEBUGGING))
5365 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
5366 "Attempt to free temp prematurely: SV 0x%"UVxf
5367 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
5371 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
5372 /* make sure SvREFCNT(sv)==0 happens very seldom */
5373 SvREFCNT(sv) = (~(U32)0)/2;
5384 Returns the length of the string in the SV. Handles magic and type
5385 coercion. See also C<SvCUR>, which gives raw access to the xpv_cur slot.
5391 Perl_sv_len(pTHX_ register SV *sv)
5399 len = mg_length(sv);
5401 (void)SvPV(sv, len);
5406 =for apidoc sv_len_utf8
5408 Returns the number of characters in the string in an SV, counting wide
5409 UTF-8 bytes as a single character. Handles magic and type coercion.
5415 * The length is cached in PERL_UTF8_magic, in the mg_len field. Also the
5416 * mg_ptr is used, by sv_pos_u2b(), see the comments of S_utf8_mg_pos_init().
5417 * (Note that the mg_len is not the length of the mg_ptr field.)
5422 Perl_sv_len_utf8(pTHX_ register SV *sv)
5428 return mg_length(sv);
5432 U8 *s = (U8*)SvPV(sv, len);
5433 MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : 0;
5435 if (mg && mg->mg_len != -1 && (mg->mg_len > 0 || len == 0)) {
5437 #ifdef PERL_UTF8_CACHE_ASSERT
5438 assert(ulen == Perl_utf8_length(aTHX_ s, s + len));
5442 ulen = Perl_utf8_length(aTHX_ s, s + len);
5443 if (!mg && !SvREADONLY(sv)) {
5444 sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0);
5445 mg = mg_find(sv, PERL_MAGIC_utf8);
5455 /* S_utf8_mg_pos_init() is used to initialize the mg_ptr field of
5456 * a PERL_UTF8_magic. The mg_ptr is used to store the mapping
5457 * between UTF-8 and byte offsets. There are two (substr offset and substr
5458 * length, the i offset, PERL_MAGIC_UTF8_CACHESIZE) times two (UTF-8 offset
5459 * and byte offset) cache positions.
5461 * The mg_len field is used by sv_len_utf8(), see its comments.
5462 * Note that the mg_len is not the length of the mg_ptr field.
5466 S_utf8_mg_pos_init(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i, I32 *offsetp, U8 *s, U8 *start)
5470 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
5472 *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, &PL_vtbl_utf8, 0, 0);
5476 *cachep = (STRLEN *) (*mgp)->mg_ptr;
5478 Newz(0, *cachep, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
5479 (*mgp)->mg_ptr = (char *) *cachep;
5483 (*cachep)[i] = *offsetp;
5484 (*cachep)[i+1] = s - start;
5492 * S_utf8_mg_pos() is used to query and update mg_ptr field of
5493 * a PERL_UTF8_magic. The mg_ptr is used to store the mapping
5494 * between UTF-8 and byte offsets. See also the comments of
5495 * S_utf8_mg_pos_init().
5499 S_utf8_mg_pos(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i, I32 *offsetp, I32 uoff, U8 **sp, U8 *start, U8 *send)
5503 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
5505 *mgp = mg_find(sv, PERL_MAGIC_utf8);
5506 if (*mgp && (*mgp)->mg_ptr) {
5507 *cachep = (STRLEN *) (*mgp)->mg_ptr;
5508 ASSERT_UTF8_CACHE(*cachep);
5509 if ((*cachep)[i] == (STRLEN)uoff) /* An exact match. */
5511 else { /* We will skip to the right spot. */
5516 /* The assumption is that going backward is half
5517 * the speed of going forward (that's where the
5518 * 2 * backw in the below comes from). (The real
5519 * figure of course depends on the UTF-8 data.) */
5521 if ((*cachep)[i] > (STRLEN)uoff) {
5523 backw = (*cachep)[i] - (STRLEN)uoff;
5525 if (forw < 2 * backw)
5528 p = start + (*cachep)[i+1];
5530 /* Try this only for the substr offset (i == 0),
5531 * not for the substr length (i == 2). */
5532 else if (i == 0) { /* (*cachep)[i] < uoff */
5533 STRLEN ulen = sv_len_utf8(sv);
5535 if ((STRLEN)uoff < ulen) {
5536 forw = (STRLEN)uoff - (*cachep)[i];
5537 backw = ulen - (STRLEN)uoff;
5539 if (forw < 2 * backw)
5540 p = start + (*cachep)[i+1];
5545 /* If the string is not long enough for uoff,
5546 * we could extend it, but not at this low a level. */
5550 if (forw < 2 * backw) {
5557 while (UTF8_IS_CONTINUATION(*p))
5562 /* Update the cache. */
5563 (*cachep)[i] = (STRLEN)uoff;
5564 (*cachep)[i+1] = p - start;
5566 /* Drop the stale "length" cache */
5575 if (found) { /* Setup the return values. */
5576 *offsetp = (*cachep)[i+1];
5577 *sp = start + *offsetp;
5580 *offsetp = send - start;
5582 else if (*sp < start) {
5588 #ifdef PERL_UTF8_CACHE_ASSERT
5593 while (n-- && s < send)
5597 assert(*offsetp == s - start);
5598 assert((*cachep)[0] == (STRLEN)uoff);
5599 assert((*cachep)[1] == *offsetp);
5601 ASSERT_UTF8_CACHE(*cachep);
5610 =for apidoc sv_pos_u2b
5612 Converts the value pointed to by offsetp from a count of UTF-8 chars from
5613 the start of the string, to a count of the equivalent number of bytes; if
5614 lenp is non-zero, it does the same to lenp, but this time starting from
5615 the offset, rather than from the start of the string. Handles magic and
5622 * sv_pos_u2b() uses, like sv_pos_b2u(), the mg_ptr of the potential
5623 * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and
5624 * byte offsets. See also the comments of S_utf8_mg_pos().
5629 Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp)
5640 start = s = (U8*)SvPV(sv, len);
5642 I32 uoffset = *offsetp;
5647 if (utf8_mg_pos(sv, &mg, &cache, 0, offsetp, *offsetp, &s, start, send))
5649 if (!found && uoffset > 0) {
5650 while (s < send && uoffset--)
5654 if (utf8_mg_pos_init(sv, &mg, &cache, 0, offsetp, s, start))
5656 *offsetp = s - start;
5661 if (utf8_mg_pos(sv, &mg, &cache, 2, lenp, *lenp + *offsetp, &s, start, send)) {
5665 if (!found && *lenp > 0) {
5668 while (s < send && ulen--)
5672 utf8_mg_pos_init(sv, &mg, &cache, 2, lenp, s, start);
5676 ASSERT_UTF8_CACHE(cache);
5688 =for apidoc sv_pos_b2u
5690 Converts the value pointed to by offsetp from a count of bytes from the
5691 start of the string, to a count of the equivalent number of UTF-8 chars.
5692 Handles magic and type coercion.
5698 * sv_pos_b2u() uses, like sv_pos_u2b(), the mg_ptr of the potential
5699 * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and
5700 * byte offsets. See also the comments of S_utf8_mg_pos().
5705 Perl_sv_pos_b2u(pTHX_ register SV* sv, I32* offsetp)
5713 s = (U8*)SvPV(sv, len);
5714 if ((I32)len < *offsetp)
5715 Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset");
5717 U8* send = s + *offsetp;
5719 STRLEN *cache = NULL;
5723 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
5724 mg = mg_find(sv, PERL_MAGIC_utf8);
5725 if (mg && mg->mg_ptr) {
5726 cache = (STRLEN *) mg->mg_ptr;
5727 if (cache[1] == (STRLEN)*offsetp) {
5728 /* An exact match. */
5729 *offsetp = cache[0];
5733 else if (cache[1] < (STRLEN)*offsetp) {
5734 /* We already know part of the way. */
5737 /* Let the below loop do the rest. */
5739 else { /* cache[1] > *offsetp */
5740 /* We already know all of the way, now we may
5741 * be able to walk back. The same assumption
5742 * is made as in S_utf8_mg_pos(), namely that
5743 * walking backward is twice slower than
5744 * walking forward. */
5745 STRLEN forw = *offsetp;
5746 STRLEN backw = cache[1] - *offsetp;
5748 if (!(forw < 2 * backw)) {
5749 U8 *p = s + cache[1];
5756 while (UTF8_IS_CONTINUATION(*p)) {
5764 *offsetp = cache[0];
5766 /* Drop the stale "length" cache */
5774 ASSERT_UTF8_CACHE(cache);
5780 /* Call utf8n_to_uvchr() to validate the sequence
5781 * (unless a simple non-UTF character) */
5782 if (!UTF8_IS_INVARIANT(*s))
5783 utf8n_to_uvchr(s, UTF8SKIP(s), &n, 0);
5792 if (!SvREADONLY(sv)) {
5794 sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0);
5795 mg = mg_find(sv, PERL_MAGIC_utf8);
5800 Newz(0, cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
5801 mg->mg_ptr = (char *) cache;
5806 cache[1] = *offsetp;
5807 /* Drop the stale "length" cache */
5821 Returns a boolean indicating whether the strings in the two SVs are
5822 identical. Is UTF-8 and 'use bytes' aware, handles get magic, and will
5823 coerce its args to strings if necessary.
5829 Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2)
5837 SV* svrecode = Nullsv;
5844 pv1 = SvPV(sv1, cur1);
5851 pv2 = SvPV(sv2, cur2);
5853 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
5854 /* Differing utf8ness.
5855 * Do not UTF8size the comparands as a side-effect. */
5858 svrecode = newSVpvn(pv2, cur2);
5859 sv_recode_to_utf8(svrecode, PL_encoding);
5860 pv2 = SvPV(svrecode, cur2);
5863 svrecode = newSVpvn(pv1, cur1);
5864 sv_recode_to_utf8(svrecode, PL_encoding);
5865 pv1 = SvPV(svrecode, cur1);
5867 /* Now both are in UTF-8. */
5869 SvREFCNT_dec(svrecode);
5874 bool is_utf8 = TRUE;
5877 /* sv1 is the UTF-8 one,
5878 * if is equal it must be downgrade-able */
5879 char *pv = (char*)bytes_from_utf8((U8*)pv1,
5885 /* sv2 is the UTF-8 one,
5886 * if is equal it must be downgrade-able */
5887 char *pv = (char *)bytes_from_utf8((U8*)pv2,
5893 /* Downgrade not possible - cannot be eq */
5900 eq = memEQ(pv1, pv2, cur1);
5903 SvREFCNT_dec(svrecode);
5914 Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
5915 string in C<sv1> is less than, equal to, or greater than the string in
5916 C<sv2>. Is UTF-8 and 'use bytes' aware, handles get magic, and will
5917 coerce its args to strings if necessary. See also C<sv_cmp_locale>.
5923 Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2)
5926 char *pv1, *pv2, *tpv = Nullch;
5928 SV *svrecode = Nullsv;
5935 pv1 = SvPV(sv1, cur1);
5942 pv2 = SvPV(sv2, cur2);
5944 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
5945 /* Differing utf8ness.
5946 * Do not UTF8size the comparands as a side-effect. */
5949 svrecode = newSVpvn(pv2, cur2);
5950 sv_recode_to_utf8(svrecode, PL_encoding);
5951 pv2 = SvPV(svrecode, cur2);
5954 pv2 = tpv = (char*)bytes_to_utf8((U8*)pv2, &cur2);
5959 svrecode = newSVpvn(pv1, cur1);
5960 sv_recode_to_utf8(svrecode, PL_encoding);
5961 pv1 = SvPV(svrecode, cur1);
5964 pv1 = tpv = (char*)bytes_to_utf8((U8*)pv1, &cur1);
5970 cmp = cur2 ? -1 : 0;
5974 I32 retval = memcmp((void*)pv1, (void*)pv2, cur1 < cur2 ? cur1 : cur2);
5977 cmp = retval < 0 ? -1 : 1;
5978 } else if (cur1 == cur2) {
5981 cmp = cur1 < cur2 ? -1 : 1;
5986 SvREFCNT_dec(svrecode);
5995 =for apidoc sv_cmp_locale
5997 Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
5998 'use bytes' aware, handles get magic, and will coerce its args to strings
5999 if necessary. See also C<sv_cmp_locale>. See also C<sv_cmp>.
6005 Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2)
6007 #ifdef USE_LOCALE_COLLATE
6013 if (PL_collation_standard)
6017 pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL;
6019 pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL;
6021 if (!pv1 || !len1) {
6032 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
6035 return retval < 0 ? -1 : 1;
6038 * When the result of collation is equality, that doesn't mean
6039 * that there are no differences -- some locales exclude some
6040 * characters from consideration. So to avoid false equalities,
6041 * we use the raw string as a tiebreaker.
6047 #endif /* USE_LOCALE_COLLATE */
6049 return sv_cmp(sv1, sv2);
6053 #ifdef USE_LOCALE_COLLATE
6056 =for apidoc sv_collxfrm
6058 Add Collate Transform magic to an SV if it doesn't already have it.
6060 Any scalar variable may carry PERL_MAGIC_collxfrm magic that contains the
6061 scalar data of the variable, but transformed to such a format that a normal
6062 memory comparison can be used to compare the data according to the locale
6069 Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp)
6073 mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL;
6074 if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
6079 Safefree(mg->mg_ptr);
6081 if ((xf = mem_collxfrm(s, len, &xlen))) {
6082 if (SvREADONLY(sv)) {
6085 return xf + sizeof(PL_collation_ix);
6088 sv_magic(sv, 0, PERL_MAGIC_collxfrm, 0, 0);
6089 mg = mg_find(sv, PERL_MAGIC_collxfrm);
6102 if (mg && mg->mg_ptr) {
6104 return mg->mg_ptr + sizeof(PL_collation_ix);
6112 #endif /* USE_LOCALE_COLLATE */
6117 Get a line from the filehandle and store it into the SV, optionally
6118 appending to the currently-stored string.
6124 Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append)
6128 register STDCHAR rslast;
6129 register STDCHAR *bp;
6135 if (SvTHINKFIRST(sv))
6136 sv_force_normal_flags(sv, append ? 0 : SV_COW_DROP_PV);
6137 /* XXX. If you make this PVIV, then copy on write can copy scalars read
6139 However, perlbench says it's slower, because the existing swipe code
6140 is faster than copy on write.
6141 Swings and roundabouts. */
6142 (void)SvUPGRADE(sv, SVt_PV);
6147 if (PerlIO_isutf8(fp)) {
6149 sv_utf8_upgrade_nomg(sv);
6150 sv_pos_u2b(sv,&append,0);
6152 } else if (SvUTF8(sv)) {
6153 SV *tsv = NEWSV(0,0);
6154 sv_gets(tsv, fp, 0);
6155 sv_utf8_upgrade_nomg(tsv);
6156 SvCUR_set(sv,append);
6159 goto return_string_or_null;
6164 if (PerlIO_isutf8(fp))
6167 if (IN_PERL_COMPILETIME) {
6168 /* we always read code in line mode */
6172 else if (RsSNARF(PL_rs)) {
6173 /* If it is a regular disk file use size from stat() as estimate
6174 of amount we are going to read - may result in malloc-ing
6175 more memory than we realy need if layers bellow reduce
6176 size we read (e.g. CRLF or a gzip layer)
6179 if (!PerlLIO_fstat(PerlIO_fileno(fp), &st) && S_ISREG(st.st_mode)) {
6180 Off_t offset = PerlIO_tell(fp);
6181 if (offset != (Off_t) -1 && st.st_size + append > offset) {
6182 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1));
6188 else if (RsRECORD(PL_rs)) {
6192 /* Grab the size of the record we're getting */
6193 recsize = SvIV(SvRV(PL_rs));
6194 buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append;
6197 /* VMS wants read instead of fread, because fread doesn't respect */
6198 /* RMS record boundaries. This is not necessarily a good thing to be */
6199 /* doing, but we've got no other real choice - except avoid stdio
6200 as implementation - perhaps write a :vms layer ?
6202 bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize);
6204 bytesread = PerlIO_read(fp, buffer, recsize);
6208 SvCUR_set(sv, bytesread += append);
6209 buffer[bytesread] = '\0';
6210 goto return_string_or_null;
6212 else if (RsPARA(PL_rs)) {
6218 /* Get $/ i.e. PL_rs into same encoding as stream wants */
6219 if (PerlIO_isutf8(fp)) {
6220 rsptr = SvPVutf8(PL_rs, rslen);
6223 if (SvUTF8(PL_rs)) {
6224 if (!sv_utf8_downgrade(PL_rs, TRUE)) {
6225 Perl_croak(aTHX_ "Wide character in $/");
6228 rsptr = SvPV(PL_rs, rslen);
6232 rslast = rslen ? rsptr[rslen - 1] : '\0';
6234 if (rspara) { /* have to do this both before and after */
6235 do { /* to make sure file boundaries work right */
6238 i = PerlIO_getc(fp);
6242 PerlIO_ungetc(fp,i);
6248 /* See if we know enough about I/O mechanism to cheat it ! */
6250 /* This used to be #ifdef test - it is made run-time test for ease
6251 of abstracting out stdio interface. One call should be cheap
6252 enough here - and may even be a macro allowing compile
6256 if (PerlIO_fast_gets(fp)) {
6259 * We're going to steal some values from the stdio struct
6260 * and put EVERYTHING in the innermost loop into registers.
6262 register STDCHAR *ptr;
6266 #if defined(VMS) && defined(PERLIO_IS_STDIO)
6267 /* An ungetc()d char is handled separately from the regular
6268 * buffer, so we getc() it back out and stuff it in the buffer.
6270 i = PerlIO_getc(fp);
6271 if (i == EOF) return 0;
6272 *(--((*fp)->_ptr)) = (unsigned char) i;
6276 /* Here is some breathtakingly efficient cheating */
6278 cnt = PerlIO_get_cnt(fp); /* get count into register */
6279 /* make sure we have the room */
6280 if ((I32)(SvLEN(sv) - append) <= cnt + 1) {
6281 /* Not room for all of it
6282 if we are looking for a separator and room for some
6284 if (rslen && cnt > 80 && (I32)SvLEN(sv) > append) {
6285 /* just process what we have room for */
6286 shortbuffered = cnt - SvLEN(sv) + append + 1;
6287 cnt -= shortbuffered;
6291 /* remember that cnt can be negative */
6292 SvGROW(sv, (STRLEN)(append + (cnt <= 0 ? 2 : (cnt + 1))));
6297 bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */
6298 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
6299 DEBUG_P(PerlIO_printf(Perl_debug_log,
6300 "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
6301 DEBUG_P(PerlIO_printf(Perl_debug_log,
6302 "Screamer: entering: PerlIO * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
6303 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
6304 PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
6309 while (cnt > 0) { /* this | eat */
6311 if ((*bp++ = *ptr++) == rslast) /* really | dust */
6312 goto thats_all_folks; /* screams | sed :-) */
6316 Copy(ptr, bp, cnt, char); /* this | eat */
6317 bp += cnt; /* screams | dust */
6318 ptr += cnt; /* louder | sed :-) */
6323 if (shortbuffered) { /* oh well, must extend */
6324 cnt = shortbuffered;
6326 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
6328 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
6329 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
6333 DEBUG_P(PerlIO_printf(Perl_debug_log,
6334 "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n",
6335 PTR2UV(ptr),(long)cnt));
6336 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* deregisterize cnt and ptr */
6338 DEBUG_P(PerlIO_printf(Perl_debug_log,
6339 "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
6340 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
6341 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
6343 /* This used to call 'filbuf' in stdio form, but as that behaves like
6344 getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
6345 another abstraction. */
6346 i = PerlIO_getc(fp); /* get more characters */
6348 DEBUG_P(PerlIO_printf(Perl_debug_log,
6349 "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
6350 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
6351 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
6353 cnt = PerlIO_get_cnt(fp);
6354 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
6355 DEBUG_P(PerlIO_printf(Perl_debug_log,
6356 "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
6358 if (i == EOF) /* all done for ever? */
6359 goto thats_really_all_folks;
6361 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
6363 SvGROW(sv, bpx + cnt + 2);
6364 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
6366 *bp++ = (STDCHAR)i; /* store character from PerlIO_getc */
6368 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
6369 goto thats_all_folks;
6373 if ((rslen > 1 && (STRLEN)(bp - (STDCHAR*)SvPVX(sv)) < rslen) ||
6374 memNE((char*)bp - rslen, rsptr, rslen))
6375 goto screamer; /* go back to the fray */
6376 thats_really_all_folks:
6378 cnt += shortbuffered;
6379 DEBUG_P(PerlIO_printf(Perl_debug_log,
6380 "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
6381 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* put these back or we're in trouble */
6382 DEBUG_P(PerlIO_printf(Perl_debug_log,
6383 "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
6384 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
6385 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
6387 SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */
6388 DEBUG_P(PerlIO_printf(Perl_debug_log,
6389 "Screamer: done, len=%ld, string=|%.*s|\n",
6390 (long)SvCUR(sv),(int)SvCUR(sv),SvPVX(sv)));
6394 /*The big, slow, and stupid way. */
6396 /* Any stack-challenged places. */
6398 /* EPOC: need to work around SDK features. *
6399 * On WINS: MS VC5 generates calls to _chkstk, *
6400 * if a "large" stack frame is allocated. *
6401 * gcc on MARM does not generate calls like these. */
6402 # define USEHEAPINSTEADOFSTACK
6405 #ifdef USEHEAPINSTEADOFSTACK
6407 New(0, buf, 8192, STDCHAR);
6415 register STDCHAR *bpe = buf + sizeof(buf);
6417 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = (STDCHAR)i) != rslast && bp < bpe)
6418 ; /* keep reading */
6422 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
6423 /* Accomodate broken VAXC compiler, which applies U8 cast to
6424 * both args of ?: operator, causing EOF to change into 255
6427 i = (U8)buf[cnt - 1];
6433 cnt = 0; /* we do need to re-set the sv even when cnt <= 0 */
6435 sv_catpvn(sv, (char *) buf, cnt);
6437 sv_setpvn(sv, (char *) buf, cnt);
6439 if (i != EOF && /* joy */
6441 SvCUR(sv) < rslen ||
6442 memNE(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
6446 * If we're reading from a TTY and we get a short read,
6447 * indicating that the user hit his EOF character, we need
6448 * to notice it now, because if we try to read from the TTY
6449 * again, the EOF condition will disappear.
6451 * The comparison of cnt to sizeof(buf) is an optimization
6452 * that prevents unnecessary calls to feof().
6456 if (!(cnt < sizeof(buf) && PerlIO_eof(fp)))
6460 #ifdef USEHEAPINSTEADOFSTACK
6465 if (rspara) { /* have to do this both before and after */
6466 while (i != EOF) { /* to make sure file boundaries work right */
6467 i = PerlIO_getc(fp);
6469 PerlIO_ungetc(fp,i);
6475 return_string_or_null:
6476 return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch;
6482 Auto-increment of the value in the SV, doing string to numeric conversion
6483 if necessary. Handles 'get' magic.
6489 Perl_sv_inc(pTHX_ register SV *sv)
6498 if (SvTHINKFIRST(sv)) {
6499 if (SvREADONLY(sv) && SvFAKE(sv))
6500 sv_force_normal(sv);
6501 if (SvREADONLY(sv)) {
6502 if (IN_PERL_RUNTIME)
6503 Perl_croak(aTHX_ PL_no_modify);
6507 if (SvAMAGIC(sv) && AMG_CALLun(sv,inc))
6509 i = PTR2IV(SvRV(sv));
6514 flags = SvFLAGS(sv);
6515 if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) {
6516 /* It's (privately or publicly) a float, but not tested as an
6517 integer, so test it to see. */
6519 flags = SvFLAGS(sv);
6521 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
6522 /* It's publicly an integer, or privately an integer-not-float */
6523 #ifdef PERL_PRESERVE_IVUV
6527 if (SvUVX(sv) == UV_MAX)
6528 sv_setnv(sv, UV_MAX_P1);
6530 (void)SvIOK_only_UV(sv);
6533 if (SvIVX(sv) == IV_MAX)
6534 sv_setuv(sv, (UV)IV_MAX + 1);
6536 (void)SvIOK_only(sv);
6542 if (flags & SVp_NOK) {
6543 (void)SvNOK_only(sv);
6548 if (!(flags & SVp_POK) || !*SvPVX(sv)) {
6549 if ((flags & SVTYPEMASK) < SVt_PVIV)
6550 sv_upgrade(sv, SVt_IV);
6551 (void)SvIOK_only(sv);
6556 while (isALPHA(*d)) d++;
6557 while (isDIGIT(*d)) d++;
6559 #ifdef PERL_PRESERVE_IVUV
6560 /* Got to punt this as an integer if needs be, but we don't issue
6561 warnings. Probably ought to make the sv_iv_please() that does
6562 the conversion if possible, and silently. */
6563 int numtype = grok_number(SvPVX(sv), SvCUR(sv), NULL);
6564 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
6565 /* Need to try really hard to see if it's an integer.
6566 9.22337203685478e+18 is an integer.
6567 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
6568 so $a="9.22337203685478e+18"; $a+0; $a++
6569 needs to be the same as $a="9.22337203685478e+18"; $a++
6576 /* sv_2iv *should* have made this an NV */
6577 if (flags & SVp_NOK) {
6578 (void)SvNOK_only(sv);
6582 /* I don't think we can get here. Maybe I should assert this
6583 And if we do get here I suspect that sv_setnv will croak. NWC
6585 #if defined(USE_LONG_DOUBLE)
6586 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"PERL_PRIgldbl"\n",
6587 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
6589 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
6590 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
6593 #endif /* PERL_PRESERVE_IVUV */
6594 sv_setnv(sv,Atof(SvPVX(sv)) + 1.0);
6598 while (d >= SvPVX(sv)) {
6606 /* MKS: The original code here died if letters weren't consecutive.
6607 * at least it didn't have to worry about non-C locales. The
6608 * new code assumes that ('z'-'a')==('Z'-'A'), letters are
6609 * arranged in order (although not consecutively) and that only
6610 * [A-Za-z] are accepted by isALPHA in the C locale.
6612 if (*d != 'z' && *d != 'Z') {
6613 do { ++*d; } while (!isALPHA(*d));
6616 *(d--) -= 'z' - 'a';
6621 *(d--) -= 'z' - 'a' + 1;
6625 /* oh,oh, the number grew */
6626 SvGROW(sv, SvCUR(sv) + 2);
6628 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--)
6639 Auto-decrement of the value in the SV, doing string to numeric conversion
6640 if necessary. Handles 'get' magic.
6646 Perl_sv_dec(pTHX_ register SV *sv)
6654 if (SvTHINKFIRST(sv)) {
6655 if (SvREADONLY(sv) && SvFAKE(sv))
6656 sv_force_normal(sv);
6657 if (SvREADONLY(sv)) {
6658 if (IN_PERL_RUNTIME)
6659 Perl_croak(aTHX_ PL_no_modify);
6663 if (SvAMAGIC(sv) && AMG_CALLun(sv,dec))
6665 i = PTR2IV(SvRV(sv));
6670 /* Unlike sv_inc we don't have to worry about string-never-numbers
6671 and keeping them magic. But we mustn't warn on punting */
6672 flags = SvFLAGS(sv);
6673 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
6674 /* It's publicly an integer, or privately an integer-not-float */
6675 #ifdef PERL_PRESERVE_IVUV
6679 if (SvUVX(sv) == 0) {
6680 (void)SvIOK_only(sv);
6684 (void)SvIOK_only_UV(sv);
6688 if (SvIVX(sv) == IV_MIN)
6689 sv_setnv(sv, (NV)IV_MIN - 1.0);
6691 (void)SvIOK_only(sv);
6697 if (flags & SVp_NOK) {
6699 (void)SvNOK_only(sv);
6702 if (!(flags & SVp_POK)) {
6703 if ((flags & SVTYPEMASK) < SVt_PVNV)
6704 sv_upgrade(sv, SVt_NV);
6706 (void)SvNOK_only(sv);
6709 #ifdef PERL_PRESERVE_IVUV
6711 int numtype = grok_number(SvPVX(sv), SvCUR(sv), NULL);
6712 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
6713 /* Need to try really hard to see if it's an integer.
6714 9.22337203685478e+18 is an integer.
6715 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
6716 so $a="9.22337203685478e+18"; $a+0; $a--
6717 needs to be the same as $a="9.22337203685478e+18"; $a--
6724 /* sv_2iv *should* have made this an NV */
6725 if (flags & SVp_NOK) {
6726 (void)SvNOK_only(sv);
6730 /* I don't think we can get here. Maybe I should assert this
6731 And if we do get here I suspect that sv_setnv will croak. NWC
6733 #if defined(USE_LONG_DOUBLE)
6734 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"PERL_PRIgldbl"\n",
6735 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
6737 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
6738 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
6742 #endif /* PERL_PRESERVE_IVUV */
6743 sv_setnv(sv,Atof(SvPVX(sv)) - 1.0); /* punt */
6747 =for apidoc sv_mortalcopy
6749 Creates a new SV which is a copy of the original SV (using C<sv_setsv>).
6750 The new SV is marked as mortal. It will be destroyed "soon", either by an
6751 explicit call to FREETMPS, or by an implicit call at places such as
6752 statement boundaries. See also C<sv_newmortal> and C<sv_2mortal>.
6757 /* Make a string that will exist for the duration of the expression
6758 * evaluation. Actually, it may have to last longer than that, but
6759 * hopefully we won't free it until it has been assigned to a
6760 * permanent location. */
6763 Perl_sv_mortalcopy(pTHX_ SV *oldstr)
6768 sv_setsv(sv,oldstr);
6770 PL_tmps_stack[++PL_tmps_ix] = sv;
6776 =for apidoc sv_newmortal
6778 Creates a new null SV which is mortal. The reference count of the SV is
6779 set to 1. It will be destroyed "soon", either by an explicit call to
6780 FREETMPS, or by an implicit call at places such as statement boundaries.
6781 See also C<sv_mortalcopy> and C<sv_2mortal>.
6787 Perl_sv_newmortal(pTHX)
6792 SvFLAGS(sv) = SVs_TEMP;
6794 PL_tmps_stack[++PL_tmps_ix] = sv;
6799 =for apidoc sv_2mortal
6801 Marks an existing SV as mortal. The SV will be destroyed "soon", either
6802 by an explicit call to FREETMPS, or by an implicit call at places such as
6803 statement boundaries. SvTEMP() is turned on which means that the SV's
6804 string buffer can be "stolen" if this SV is copied. See also C<sv_newmortal>
6805 and C<sv_mortalcopy>.
6811 Perl_sv_2mortal(pTHX_ register SV *sv)
6815 if (SvREADONLY(sv) && SvIMMORTAL(sv))
6818 PL_tmps_stack[++PL_tmps_ix] = sv;
6826 Creates a new SV and copies a string into it. The reference count for the
6827 SV is set to 1. If C<len> is zero, Perl will compute the length using
6828 strlen(). For efficiency, consider using C<newSVpvn> instead.
6834 Perl_newSVpv(pTHX_ const char *s, STRLEN len)
6841 sv_setpvn(sv,s,len);
6846 =for apidoc newSVpvn
6848 Creates a new SV and copies a string into it. The reference count for the
6849 SV is set to 1. Note that if C<len> is zero, Perl will create a zero length
6850 string. You are responsible for ensuring that the source string is at least
6851 C<len> bytes long. If the C<s> argument is NULL the new SV will be undefined.
6857 Perl_newSVpvn(pTHX_ const char *s, STRLEN len)
6862 sv_setpvn(sv,s,len);
6867 =for apidoc newSVpvn_share
6869 Creates a new SV with its SvPVX pointing to a shared string in the string
6870 table. If the string does not already exist in the table, it is created
6871 first. Turns on READONLY and FAKE. The string's hash is stored in the UV
6872 slot of the SV; if the C<hash> parameter is non-zero, that value is used;
6873 otherwise the hash is computed. The idea here is that as the string table
6874 is used for shared hash keys these strings will have SvPVX == HeKEY and
6875 hash lookup will avoid string compare.
6881 Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
6884 bool is_utf8 = FALSE;
6886 STRLEN tmplen = -len;
6888 /* See the note in hv.c:hv_fetch() --jhi */
6889 src = (char*)bytes_from_utf8((U8*)src, &tmplen, &is_utf8);
6893 PERL_HASH(hash, src, len);
6895 sv_upgrade(sv, SVt_PVIV);
6896 SvPVX(sv) = sharepvn(src, is_utf8?-len:len, hash);
6909 #if defined(PERL_IMPLICIT_CONTEXT)
6911 /* pTHX_ magic can't cope with varargs, so this is a no-context
6912 * version of the main function, (which may itself be aliased to us).
6913 * Don't access this version directly.
6917 Perl_newSVpvf_nocontext(const char* pat, ...)
6922 va_start(args, pat);
6923 sv = vnewSVpvf(pat, &args);
6930 =for apidoc newSVpvf
6932 Creates a new SV and initializes it with the string formatted like
6939 Perl_newSVpvf(pTHX_ const char* pat, ...)
6943 va_start(args, pat);
6944 sv = vnewSVpvf(pat, &args);
6949 /* backend for newSVpvf() and newSVpvf_nocontext() */
6952 Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args)
6956 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
6963 Creates a new SV and copies a floating point value into it.
6964 The reference count for the SV is set to 1.
6970 Perl_newSVnv(pTHX_ NV n)
6982 Creates a new SV and copies an integer into it. The reference count for the
6989 Perl_newSViv(pTHX_ IV i)
7001 Creates a new SV and copies an unsigned integer into it.
7002 The reference count for the SV is set to 1.
7008 Perl_newSVuv(pTHX_ UV u)
7018 =for apidoc newRV_noinc
7020 Creates an RV wrapper for an SV. The reference count for the original
7021 SV is B<not> incremented.
7027 Perl_newRV_noinc(pTHX_ SV *tmpRef)
7032 sv_upgrade(sv, SVt_RV);
7039 /* newRV_inc is the official function name to use now.
7040 * newRV_inc is in fact #defined to newRV in sv.h
7044 Perl_newRV(pTHX_ SV *tmpRef)
7046 return newRV_noinc(SvREFCNT_inc(tmpRef));
7052 Creates a new SV which is an exact duplicate of the original SV.
7059 Perl_newSVsv(pTHX_ register SV *old)
7065 if (SvTYPE(old) == SVTYPEMASK) {
7066 if (ckWARN_d(WARN_INTERNAL))
7067 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "semi-panic: attempt to dup freed string");
7082 =for apidoc sv_reset
7084 Underlying implementation for the C<reset> Perl function.
7085 Note that the perl-level function is vaguely deprecated.
7091 Perl_sv_reset(pTHX_ register char *s, HV *stash)
7099 char todo[PERL_UCHAR_MAX+1];
7104 if (!*s) { /* reset ?? searches */
7105 for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) {
7106 pm->op_pmdynflags &= ~PMdf_USED;
7111 /* reset variables */
7113 if (!HvARRAY(stash))
7116 Zero(todo, 256, char);
7118 i = (unsigned char)*s;
7122 max = (unsigned char)*s++;
7123 for ( ; i <= max; i++) {
7126 for (i = 0; i <= (I32) HvMAX(stash); i++) {
7127 for (entry = HvARRAY(stash)[i];
7129 entry = HeNEXT(entry))
7131 if (!todo[(U8)*HeKEY(entry)])
7133 gv = (GV*)HeVAL(entry);
7135 if (SvTHINKFIRST(sv)) {
7136 if (!SvREADONLY(sv) && SvROK(sv))
7141 if (SvTYPE(sv) >= SVt_PV) {
7143 if (SvPVX(sv) != Nullch)
7150 if (GvHV(gv) && !HvNAME(GvHV(gv))) {
7153 #ifdef USE_ENVIRON_ARRAY
7155 # ifdef USE_ITHREADS
7156 && PL_curinterp == aTHX
7160 environ[0] = Nullch;
7163 #endif /* !PERL_MICRO */
7173 Using various gambits, try to get an IO from an SV: the IO slot if its a
7174 GV; or the recursive result if we're an RV; or the IO slot of the symbol
7175 named after the PV if we're a string.
7181 Perl_sv_2io(pTHX_ SV *sv)
7187 switch (SvTYPE(sv)) {
7195 Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv));
7199 Perl_croak(aTHX_ PL_no_usym, "filehandle");
7201 return sv_2io(SvRV(sv));
7202 gv = gv_fetchpv(SvPV(sv,n_a), FALSE, SVt_PVIO);
7208 Perl_croak(aTHX_ "Bad filehandle: %"SVf, sv);
7217 Using various gambits, try to get a CV from an SV; in addition, try if
7218 possible to set C<*st> and C<*gvp> to the stash and GV associated with it.
7224 Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref)
7231 return *gvp = Nullgv, Nullcv;
7232 switch (SvTYPE(sv)) {
7251 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
7252 tryAMAGICunDEREF(to_cv);
7255 if (SvTYPE(sv) == SVt_PVCV) {
7264 Perl_croak(aTHX_ "Not a subroutine reference");
7269 gv = gv_fetchpv(SvPV(sv, n_a), lref, SVt_PVCV);
7275 if (lref && !GvCVu(gv)) {
7278 tmpsv = NEWSV(704,0);
7279 gv_efullname3(tmpsv, gv, Nullch);
7280 /* XXX this is probably not what they think they're getting.
7281 * It has the same effect as "sub name;", i.e. just a forward
7283 newSUB(start_subparse(FALSE, 0),
7284 newSVOP(OP_CONST, 0, tmpsv),
7289 Perl_croak(aTHX_ "Unable to create sub named \"%"SVf"\"",
7299 Returns true if the SV has a true value by Perl's rules.
7300 Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may
7301 instead use an in-line version.
7307 Perl_sv_true(pTHX_ register SV *sv)
7313 if ((tXpv = (XPV*)SvANY(sv)) &&
7314 (tXpv->xpv_cur > 1 ||
7315 (tXpv->xpv_cur && *tXpv->xpv_pv != '0')))
7322 return SvIVX(sv) != 0;
7325 return SvNVX(sv) != 0.0;
7327 return sv_2bool(sv);
7335 A private implementation of the C<SvIVx> macro for compilers which can't
7336 cope with complex macro expressions. Always use the macro instead.
7342 Perl_sv_iv(pTHX_ register SV *sv)
7346 return (IV)SvUVX(sv);
7355 A private implementation of the C<SvUVx> macro for compilers which can't
7356 cope with complex macro expressions. Always use the macro instead.
7362 Perl_sv_uv(pTHX_ register SV *sv)
7367 return (UV)SvIVX(sv);
7375 A private implementation of the C<SvNVx> macro for compilers which can't
7376 cope with complex macro expressions. Always use the macro instead.
7382 Perl_sv_nv(pTHX_ register SV *sv)
7389 /* sv_pv() is now a macro using SvPV_nolen();
7390 * this function provided for binary compatibility only
7394 Perl_sv_pv(pTHX_ SV *sv)
7401 return sv_2pv(sv, &n_a);
7407 Use the C<SvPV_nolen> macro instead
7411 A private implementation of the C<SvPV> macro for compilers which can't
7412 cope with complex macro expressions. Always use the macro instead.
7418 Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp)
7424 return sv_2pv(sv, lp);
7429 Perl_sv_pvn_nomg(pTHX_ register SV *sv, STRLEN *lp)
7435 return sv_2pv_flags(sv, lp, 0);
7438 /* sv_pvn_force() is now a macro using Perl_sv_pvn_force_flags();
7439 * this function provided for binary compatibility only
7443 Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp)
7445 return sv_pvn_force_flags(sv, lp, SV_GMAGIC);
7449 =for apidoc sv_pvn_force
7451 Get a sensible string out of the SV somehow.
7452 A private implementation of the C<SvPV_force> macro for compilers which
7453 can't cope with complex macro expressions. Always use the macro instead.
7455 =for apidoc sv_pvn_force_flags
7457 Get a sensible string out of the SV somehow.
7458 If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<sv> if
7459 appropriate, else not. C<sv_pvn_force> and C<sv_pvn_force_nomg> are
7460 implemented in terms of this function.
7461 You normally want to use the various wrapper macros instead: see
7462 C<SvPV_force> and C<SvPV_force_nomg>
7468 Perl_sv_pvn_force_flags(pTHX_ SV *sv, STRLEN *lp, I32 flags)
7472 if (SvTHINKFIRST(sv) && !SvROK(sv))
7473 sv_force_normal(sv);
7479 if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) {
7480 Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
7484 s = sv_2pv_flags(sv, lp, flags);
7485 if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */
7490 (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */
7491 SvGROW(sv, len + 1);
7492 Move(s,SvPVX(sv),len,char);
7497 SvPOK_on(sv); /* validate pointer */
7499 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
7500 PTR2UV(sv),SvPVX(sv)));
7506 /* sv_pvbyte () is now a macro using Perl_sv_2pv_flags();
7507 * this function provided for binary compatibility only
7511 Perl_sv_pvbyte(pTHX_ SV *sv)
7513 sv_utf8_downgrade(sv,0);
7518 =for apidoc sv_pvbyte
7520 Use C<SvPVbyte_nolen> instead.
7522 =for apidoc sv_pvbyten
7524 A private implementation of the C<SvPVbyte> macro for compilers
7525 which can't cope with complex macro expressions. Always use the macro
7532 Perl_sv_pvbyten(pTHX_ SV *sv, STRLEN *lp)
7534 sv_utf8_downgrade(sv,0);
7535 return sv_pvn(sv,lp);
7539 =for apidoc sv_pvbyten_force
7541 A private implementation of the C<SvPVbytex_force> macro for compilers
7542 which can't cope with complex macro expressions. Always use the macro
7549 Perl_sv_pvbyten_force(pTHX_ SV *sv, STRLEN *lp)
7551 sv_pvn_force(sv,lp);
7552 sv_utf8_downgrade(sv,0);
7557 /* sv_pvutf8 () is now a macro using Perl_sv_2pv_flags();
7558 * this function provided for binary compatibility only
7562 Perl_sv_pvutf8(pTHX_ SV *sv)
7564 sv_utf8_upgrade(sv);
7569 =for apidoc sv_pvutf8
7571 Use the C<SvPVutf8_nolen> macro instead
7573 =for apidoc sv_pvutf8n
7575 A private implementation of the C<SvPVutf8> macro for compilers
7576 which can't cope with complex macro expressions. Always use the macro
7583 Perl_sv_pvutf8n(pTHX_ SV *sv, STRLEN *lp)
7585 sv_utf8_upgrade(sv);
7586 return sv_pvn(sv,lp);
7590 =for apidoc sv_pvutf8n_force
7592 A private implementation of the C<SvPVutf8_force> macro for compilers
7593 which can't cope with complex macro expressions. Always use the macro
7600 Perl_sv_pvutf8n_force(pTHX_ SV *sv, STRLEN *lp)
7602 sv_pvn_force(sv,lp);
7603 sv_utf8_upgrade(sv);
7609 =for apidoc sv_reftype
7611 Returns a string describing what the SV is a reference to.
7617 Perl_sv_reftype(pTHX_ SV *sv, int ob)
7619 if (ob && SvOBJECT(sv)) {
7620 HV *svs = SvSTASH(sv);
7621 /* [20011101.072] This bandaid for C<package;> should eventually
7622 be removed. AMS 20011103 */
7623 return (svs ? HvNAME(svs) : "<none>");
7626 switch (SvTYPE(sv)) {
7641 case SVt_PVLV: return SvROK(sv) ? "REF"
7642 /* tied lvalues should appear to be
7643 * scalars for backwards compatitbility */
7644 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
7645 ? "SCALAR" : "LVALUE";
7646 case SVt_PVAV: return "ARRAY";
7647 case SVt_PVHV: return "HASH";
7648 case SVt_PVCV: return "CODE";
7649 case SVt_PVGV: return "GLOB";
7650 case SVt_PVFM: return "FORMAT";
7651 case SVt_PVIO: return "IO";
7652 default: return "UNKNOWN";
7658 =for apidoc sv_isobject
7660 Returns a boolean indicating whether the SV is an RV pointing to a blessed
7661 object. If the SV is not an RV, or if the object is not blessed, then this
7668 Perl_sv_isobject(pTHX_ SV *sv)
7685 Returns a boolean indicating whether the SV is blessed into the specified
7686 class. This does not check for subtypes; use C<sv_derived_from> to verify
7687 an inheritance relationship.
7693 Perl_sv_isa(pTHX_ SV *sv, const char *name)
7705 return strEQ(HvNAME(SvSTASH(sv)), name);
7711 Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then
7712 it will be upgraded to one. If C<classname> is non-null then the new SV will
7713 be blessed in the specified package. The new SV is returned and its
7714 reference count is 1.
7720 Perl_newSVrv(pTHX_ SV *rv, const char *classname)
7726 SV_CHECK_THINKFIRST(rv);
7729 if (SvTYPE(rv) >= SVt_PVMG) {
7730 U32 refcnt = SvREFCNT(rv);
7734 SvREFCNT(rv) = refcnt;
7737 if (SvTYPE(rv) < SVt_RV)
7738 sv_upgrade(rv, SVt_RV);
7739 else if (SvTYPE(rv) > SVt_RV) {
7741 if (SvPVX(rv) && SvLEN(rv))
7742 Safefree(SvPVX(rv));
7752 HV* stash = gv_stashpv(classname, TRUE);
7753 (void)sv_bless(rv, stash);
7759 =for apidoc sv_setref_pv
7761 Copies a pointer into a new SV, optionally blessing the SV. The C<rv>
7762 argument will be upgraded to an RV. That RV will be modified to point to
7763 the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed
7764 into the SV. The C<classname> argument indicates the package for the
7765 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
7766 will have a reference count of 1, and the RV will be returned.
7768 Do not use with other Perl types such as HV, AV, SV, CV, because those
7769 objects will become corrupted by the pointer copy process.
7771 Note that C<sv_setref_pvn> copies the string while this copies the pointer.
7777 Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv)
7780 sv_setsv(rv, &PL_sv_undef);
7784 sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
7789 =for apidoc sv_setref_iv
7791 Copies an integer into a new SV, optionally blessing the SV. The C<rv>
7792 argument will be upgraded to an RV. That RV will be modified to point to
7793 the new SV. The C<classname> argument indicates the package for the
7794 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
7795 will have a reference count of 1, and the RV will be returned.
7801 Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv)
7803 sv_setiv(newSVrv(rv,classname), iv);
7808 =for apidoc sv_setref_uv
7810 Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv>
7811 argument will be upgraded to an RV. That RV will be modified to point to
7812 the new SV. The C<classname> argument indicates the package for the
7813 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
7814 will have a reference count of 1, and the RV will be returned.
7820 Perl_sv_setref_uv(pTHX_ SV *rv, const char *classname, UV uv)
7822 sv_setuv(newSVrv(rv,classname), uv);
7827 =for apidoc sv_setref_nv
7829 Copies a double into a new SV, optionally blessing the SV. The C<rv>
7830 argument will be upgraded to an RV. That RV will be modified to point to
7831 the new SV. The C<classname> argument indicates the package for the
7832 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
7833 will have a reference count of 1, and the RV will be returned.
7839 Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv)
7841 sv_setnv(newSVrv(rv,classname), nv);
7846 =for apidoc sv_setref_pvn
7848 Copies a string into a new SV, optionally blessing the SV. The length of the
7849 string must be specified with C<n>. The C<rv> argument will be upgraded to
7850 an RV. That RV will be modified to point to the new SV. The C<classname>
7851 argument indicates the package for the blessing. Set C<classname> to
7852 C<Nullch> to avoid the blessing. The new SV will have a reference count
7853 of 1, and the RV will be returned.
7855 Note that C<sv_setref_pv> copies the pointer while this copies the string.
7861 Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, char *pv, STRLEN n)
7863 sv_setpvn(newSVrv(rv,classname), pv, n);
7868 =for apidoc sv_bless
7870 Blesses an SV into a specified package. The SV must be an RV. The package
7871 must be designated by its stash (see C<gv_stashpv()>). The reference count
7872 of the SV is unaffected.
7878 Perl_sv_bless(pTHX_ SV *sv, HV *stash)
7882 Perl_croak(aTHX_ "Can't bless non-reference value");
7884 if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) {
7885 if (SvREADONLY(tmpRef))
7886 Perl_croak(aTHX_ PL_no_modify);
7887 if (SvOBJECT(tmpRef)) {
7888 if (SvTYPE(tmpRef) != SVt_PVIO)
7890 SvREFCNT_dec(SvSTASH(tmpRef));
7893 SvOBJECT_on(tmpRef);
7894 if (SvTYPE(tmpRef) != SVt_PVIO)
7896 (void)SvUPGRADE(tmpRef, SVt_PVMG);
7897 SvSTASH(tmpRef) = (HV*)SvREFCNT_inc(stash);
7904 if(SvSMAGICAL(tmpRef))
7905 if(mg_find(tmpRef, PERL_MAGIC_ext) || mg_find(tmpRef, PERL_MAGIC_uvar))
7913 /* Downgrades a PVGV to a PVMG.
7917 S_sv_unglob(pTHX_ SV *sv)
7921 assert(SvTYPE(sv) == SVt_PVGV);
7926 SvREFCNT_dec(GvSTASH(sv));
7927 GvSTASH(sv) = Nullhv;
7929 sv_unmagic(sv, PERL_MAGIC_glob);
7930 Safefree(GvNAME(sv));
7933 /* need to keep SvANY(sv) in the right arena */
7934 xpvmg = new_XPVMG();
7935 StructCopy(SvANY(sv), xpvmg, XPVMG);
7936 del_XPVGV(SvANY(sv));
7939 SvFLAGS(sv) &= ~SVTYPEMASK;
7940 SvFLAGS(sv) |= SVt_PVMG;
7944 =for apidoc sv_unref_flags
7946 Unsets the RV status of the SV, and decrements the reference count of
7947 whatever was being referenced by the RV. This can almost be thought of
7948 as a reversal of C<newSVrv>. The C<cflags> argument can contain
7949 C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented
7950 (otherwise the decrementing is conditional on the reference count being
7951 different from one or the reference being a readonly SV).
7958 Perl_sv_unref_flags(pTHX_ SV *sv, U32 flags)
7962 if (SvWEAKREF(sv)) {
7970 /* You can't have a || SvREADONLY(rv) here, as $a = $$a, where $a was
7971 assigned to as BEGIN {$a = \"Foo"} will fail. */
7972 if (SvREFCNT(rv) != 1 || (flags & SV_IMMEDIATE_UNREF))
7974 else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */
7975 sv_2mortal(rv); /* Schedule for freeing later */
7979 =for apidoc sv_unref
7981 Unsets the RV status of the SV, and decrements the reference count of
7982 whatever was being referenced by the RV. This can almost be thought of
7983 as a reversal of C<newSVrv>. This is C<sv_unref_flags> with the C<flag>
7984 being zero. See C<SvROK_off>.
7990 Perl_sv_unref(pTHX_ SV *sv)
7992 sv_unref_flags(sv, 0);
7996 =for apidoc sv_taint
7998 Taint an SV. Use C<SvTAINTED_on> instead.
8003 Perl_sv_taint(pTHX_ SV *sv)
8005 sv_magic((sv), Nullsv, PERL_MAGIC_taint, Nullch, 0);
8009 =for apidoc sv_untaint
8011 Untaint an SV. Use C<SvTAINTED_off> instead.
8016 Perl_sv_untaint(pTHX_ SV *sv)
8018 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
8019 MAGIC *mg = mg_find(sv, PERL_MAGIC_taint);
8026 =for apidoc sv_tainted
8028 Test an SV for taintedness. Use C<SvTAINTED> instead.
8033 Perl_sv_tainted(pTHX_ SV *sv)
8035 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
8036 MAGIC *mg = mg_find(sv, PERL_MAGIC_taint);
8037 if (mg && ((mg->mg_len & 1) || ((mg->mg_len & 2) && mg->mg_obj == sv)))
8044 =for apidoc sv_setpviv
8046 Copies an integer into the given SV, also updating its string value.
8047 Does not handle 'set' magic. See C<sv_setpviv_mg>.
8053 Perl_sv_setpviv(pTHX_ SV *sv, IV iv)
8055 char buf[TYPE_CHARS(UV)];
8057 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
8059 sv_setpvn(sv, ptr, ebuf - ptr);
8063 =for apidoc sv_setpviv_mg
8065 Like C<sv_setpviv>, but also handles 'set' magic.
8071 Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv)
8073 char buf[TYPE_CHARS(UV)];
8075 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
8077 sv_setpvn(sv, ptr, ebuf - ptr);
8081 #if defined(PERL_IMPLICIT_CONTEXT)
8083 /* pTHX_ magic can't cope with varargs, so this is a no-context
8084 * version of the main function, (which may itself be aliased to us).
8085 * Don't access this version directly.
8089 Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...)
8093 va_start(args, pat);
8094 sv_vsetpvf(sv, pat, &args);
8098 /* pTHX_ magic can't cope with varargs, so this is a no-context
8099 * version of the main function, (which may itself be aliased to us).
8100 * Don't access this version directly.
8104 Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...)
8108 va_start(args, pat);
8109 sv_vsetpvf_mg(sv, pat, &args);
8115 =for apidoc sv_setpvf
8117 Works like C<sv_catpvf> but copies the text into the SV instead of
8118 appending it. Does not handle 'set' magic. See C<sv_setpvf_mg>.
8124 Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...)
8127 va_start(args, pat);
8128 sv_vsetpvf(sv, pat, &args);
8133 =for apidoc sv_vsetpvf
8135 Works like C<sv_vcatpvf> but copies the text into the SV instead of
8136 appending it. Does not handle 'set' magic. See C<sv_vsetpvf_mg>.
8138 Usually used via its frontend C<sv_setpvf>.
8144 Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args)
8146 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
8150 =for apidoc sv_setpvf_mg
8152 Like C<sv_setpvf>, but also handles 'set' magic.
8158 Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...)
8161 va_start(args, pat);
8162 sv_vsetpvf_mg(sv, pat, &args);
8167 =for apidoc sv_vsetpvf_mg
8169 Like C<sv_vsetpvf>, but also handles 'set' magic.
8171 Usually used via its frontend C<sv_setpvf_mg>.
8177 Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
8179 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
8183 #if defined(PERL_IMPLICIT_CONTEXT)
8185 /* pTHX_ magic can't cope with varargs, so this is a no-context
8186 * version of the main function, (which may itself be aliased to us).
8187 * Don't access this version directly.
8191 Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...)
8195 va_start(args, pat);
8196 sv_vcatpvf(sv, pat, &args);
8200 /* pTHX_ magic can't cope with varargs, so this is a no-context
8201 * version of the main function, (which may itself be aliased to us).
8202 * Don't access this version directly.
8206 Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...)
8210 va_start(args, pat);
8211 sv_vcatpvf_mg(sv, pat, &args);
8217 =for apidoc sv_catpvf
8219 Processes its arguments like C<sprintf> and appends the formatted
8220 output to an SV. If the appended data contains "wide" characters
8221 (including, but not limited to, SVs with a UTF-8 PV formatted with %s,
8222 and characters >255 formatted with %c), the original SV might get
8223 upgraded to UTF-8. Handles 'get' magic, but not 'set' magic. See
8229 Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...)
8232 va_start(args, pat);
8233 sv_vcatpvf(sv, pat, &args);
8238 =for apidoc sv_vcatpvf
8240 Processes its arguments like C<vsprintf> and appends the formatted output
8241 to an SV. Does not handle 'set' magic. See C<sv_vcatpvf_mg>.
8243 Usually used via its frontend C<sv_catpvf>.
8249 Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args)
8251 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
8255 =for apidoc sv_catpvf_mg
8257 Like C<sv_catpvf>, but also handles 'set' magic.
8263 Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...)
8266 va_start(args, pat);
8267 sv_vcatpvf_mg(sv, pat, &args);
8272 =for apidoc sv_vcatpvf_mg
8274 Like C<sv_vcatpvf>, but also handles 'set' magic.
8276 Usually used via its frontend C<sv_catpvf_mg>.
8282 Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
8284 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
8289 =for apidoc sv_vsetpvfn
8291 Works like C<sv_vcatpvfn> but copies the text into the SV instead of
8294 Usually used via one of its frontends C<sv_vsetpvf> and C<sv_vsetpvf_mg>.
8300 Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
8302 sv_setpvn(sv, "", 0);
8303 sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted);
8306 /* private function for use in sv_vcatpvfn via the EXPECT_NUMBER macro */
8309 S_expect_number(pTHX_ char** pattern)
8312 switch (**pattern) {
8313 case '1': case '2': case '3':
8314 case '4': case '5': case '6':
8315 case '7': case '8': case '9':
8316 while (isDIGIT(**pattern))
8317 var = var * 10 + (*(*pattern)++ - '0');
8321 #define EXPECT_NUMBER(pattern, var) (var = S_expect_number(aTHX_ &pattern))
8324 F0convert(NV nv, char *endbuf, STRLEN *len)
8335 if (uv & 1 && uv == nv)
8336 uv--; /* Round to even */
8338 unsigned dig = uv % 10;
8351 =for apidoc sv_vcatpvfn
8353 Processes its arguments like C<vsprintf> and appends the formatted output
8354 to an SV. Uses an array of SVs if the C style variable argument list is
8355 missing (NULL). When running with taint checks enabled, indicates via
8356 C<maybe_tainted> if results are untrustworthy (often due to the use of
8359 Usually used via one of its frontends C<sv_vcatpvf> and C<sv_vcatpvf_mg>.
8365 Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
8372 static char nullstr[] = "(null)";
8374 bool has_utf8; /* has the result utf8? */
8375 bool pat_utf8; /* the pattern is in utf8? */
8377 /* Times 4: a decimal digit takes more than 3 binary digits.
8378 * NV_DIG: mantissa takes than many decimal digits.
8379 * Plus 32: Playing safe. */
8380 char ebuf[IV_DIG * 4 + NV_DIG + 32];
8381 /* large enough for "%#.#f" --chip */
8382 /* what about long double NVs? --jhi */
8384 has_utf8 = pat_utf8 = DO_UTF8(sv);
8386 /* no matter what, this is a string now */
8387 (void)SvPV_force(sv, origlen);
8389 /* special-case "", "%s", and "%_" */
8392 if (patlen == 2 && pat[0] == '%') {
8396 char *s = va_arg(*args, char*);
8397 sv_catpv(sv, s ? s : nullstr);
8399 else if (svix < svmax) {
8400 sv_catsv(sv, *svargs);
8401 if (DO_UTF8(*svargs))
8407 argsv = va_arg(*args, SV*);
8408 sv_catsv(sv, argsv);
8413 /* See comment on '_' below */
8418 #ifndef USE_LONG_DOUBLE
8419 /* special-case "%.<number>[gf]" */
8420 if ( patlen <= 5 && pat[0] == '%' && pat[1] == '.'
8421 && (pat[patlen-1] == 'g' || pat[patlen-1] == 'f') ) {
8422 unsigned digits = 0;
8426 while (*pp >= '0' && *pp <= '9')
8427 digits = 10 * digits + (*pp++ - '0');
8428 if (pp - pat == (int)patlen - 1) {
8432 nv = (NV)va_arg(*args, double);
8433 else if (svix < svmax)
8438 /* Add check for digits != 0 because it seems that some
8439 gconverts are buggy in this case, and we don't yet have
8440 a Configure test for this. */
8441 if (digits && digits < sizeof(ebuf) - NV_DIG - 10) {
8442 /* 0, point, slack */
8443 Gconvert(nv, (int)digits, 0, ebuf);
8445 if (*ebuf) /* May return an empty string for digits==0 */
8448 } else if (!digits) {
8451 if ((p = F0convert(nv, ebuf + sizeof ebuf, &l))) {
8452 sv_catpvn(sv, p, l);
8458 #endif /* !USE_LONG_DOUBLE */
8460 if (!args && svix < svmax && DO_UTF8(*svargs))
8463 patend = (char*)pat + patlen;
8464 for (p = (char*)pat; p < patend; p = q) {
8467 bool vectorize = FALSE;
8468 bool vectorarg = FALSE;
8469 bool vec_utf8 = FALSE;
8475 bool has_precis = FALSE;
8478 bool is_utf8 = FALSE; /* is this item utf8? */
8479 #ifdef HAS_LDBL_SPRINTF_BUG
8480 /* This is to try to fix a bug with irix/nonstop-ux/powerux and
8481 with sfio - Allen <allens@cpan.org> */
8482 bool fix_ldbl_sprintf_bug = FALSE;
8486 U8 utf8buf[UTF8_MAXLEN+1];
8487 STRLEN esignlen = 0;
8489 char *eptr = Nullch;
8492 U8 *vecstr = Null(U8*);
8499 /* we need a long double target in case HAS_LONG_DOUBLE but
8502 #if defined(HAS_LONG_DOUBLE) && LONG_DOUBLESIZE > DOUBLESIZE
8511 STRLEN dotstrlen = 1;
8512 I32 efix = 0; /* explicit format parameter index */
8513 I32 ewix = 0; /* explicit width index */
8514 I32 epix = 0; /* explicit precision index */
8515 I32 evix = 0; /* explicit vector index */
8516 bool asterisk = FALSE;
8518 /* echo everything up to the next format specification */
8519 for (q = p; q < patend && *q != '%'; ++q) ;
8521 if (has_utf8 && !pat_utf8)
8522 sv_catpvn_utf8_upgrade(sv, p, q - p, nsv);
8524 sv_catpvn(sv, p, q - p);
8531 We allow format specification elements in this order:
8532 \d+\$ explicit format parameter index
8534 v|\*(\d+\$)?v vector with optional (optionally specified) arg
8535 0 flag (as above): repeated to allow "v02"
8536 \d+|\*(\d+\$)? width using optional (optionally specified) arg
8537 \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg
8539 [%bcdefginopsux_DFOUX] format (mandatory)
8541 if (EXPECT_NUMBER(q, width)) {
8582 if (EXPECT_NUMBER(q, ewix))
8591 if ((vectorarg = asterisk)) {
8603 EXPECT_NUMBER(q, width);
8608 vecsv = va_arg(*args, SV*);
8610 vecsv = (evix ? evix <= svmax : svix < svmax) ?
8611 svargs[evix ? evix-1 : svix++] : &PL_sv_undef;
8612 dotstr = SvPVx(vecsv, dotstrlen);
8617 vecsv = va_arg(*args, SV*);
8618 vecstr = (U8*)SvPVx(vecsv,veclen);
8619 vec_utf8 = DO_UTF8(vecsv);
8621 else if (efix ? efix <= svmax : svix < svmax) {
8622 vecsv = svargs[efix ? efix-1 : svix++];
8623 vecstr = (U8*)SvPVx(vecsv,veclen);
8624 vec_utf8 = DO_UTF8(vecsv);
8634 i = va_arg(*args, int);
8636 i = (ewix ? ewix <= svmax : svix < svmax) ?
8637 SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
8639 width = (i < 0) ? -i : i;
8649 if (EXPECT_NUMBER(q, epix) && *q++ != '$')
8651 /* XXX: todo, support specified precision parameter */
8655 i = va_arg(*args, int);
8657 i = (ewix ? ewix <= svmax : svix < svmax)
8658 ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
8659 precis = (i < 0) ? 0 : i;
8664 precis = precis * 10 + (*q++ - '0');
8673 case 'I': /* Ix, I32x, and I64x */
8675 if (q[1] == '6' && q[2] == '4') {
8681 if (q[1] == '3' && q[2] == '2') {
8691 #if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE)
8702 #if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE)
8703 if (*(q + 1) == 'l') { /* lld, llf */
8728 argsv = (efix ? efix <= svmax : svix < svmax) ?
8729 svargs[efix ? efix-1 : svix++] : &PL_sv_undef;
8736 uv = (args && !vectorize) ? va_arg(*args, int) : SvIVx(argsv);
8738 (!UNI_IS_INVARIANT(uv) && SvUTF8(sv)))
8740 eptr = (char*)utf8buf;
8741 elen = uvchr_to_utf8((U8*)eptr, uv) - utf8buf;
8752 if (args && !vectorize) {
8753 eptr = va_arg(*args, char*);
8755 #ifdef MACOS_TRADITIONAL
8756 /* On MacOS, %#s format is used for Pascal strings */
8761 elen = strlen(eptr);
8764 elen = sizeof nullstr - 1;
8768 eptr = SvPVx(argsv, elen);
8769 if (DO_UTF8(argsv)) {
8770 if (has_precis && precis < elen) {
8772 sv_pos_u2b(argsv, &p, 0); /* sticks at end */
8775 if (width) { /* fudge width (can't fudge elen) */
8776 width += elen - sv_len_utf8(argsv);
8785 * The "%_" hack might have to be changed someday,
8786 * if ISO or ANSI decide to use '_' for something.
8787 * So we keep it hidden from users' code.
8789 if (!args || vectorize)
8791 argsv = va_arg(*args, SV*);
8792 eptr = SvPVx(argsv, elen);
8798 if (has_precis && elen > precis)
8805 if (alt || vectorize)
8807 uv = PTR2UV(args ? va_arg(*args, void*) : argsv);
8825 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
8834 esignbuf[esignlen++] = plus;
8838 case 'h': iv = (short)va_arg(*args, int); break;
8839 case 'l': iv = va_arg(*args, long); break;
8840 case 'V': iv = va_arg(*args, IV); break;
8841 default: iv = va_arg(*args, int); break;
8843 case 'q': iv = va_arg(*args, Quad_t); break;
8848 IV tiv = SvIVx(argsv); /* work around GCC bug #13488 */
8850 case 'h': iv = (short)tiv; break;
8851 case 'l': iv = (long)tiv; break;
8853 default: iv = tiv; break;
8855 case 'q': iv = (Quad_t)tiv; break;
8859 if ( !vectorize ) /* we already set uv above */
8864 esignbuf[esignlen++] = plus;
8868 esignbuf[esignlen++] = '-';
8911 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
8922 case 'h': uv = (unsigned short)va_arg(*args, unsigned); break;
8923 case 'l': uv = va_arg(*args, unsigned long); break;
8924 case 'V': uv = va_arg(*args, UV); break;
8925 default: uv = va_arg(*args, unsigned); break;
8927 case 'q': uv = va_arg(*args, Uquad_t); break;
8932 UV tuv = SvUVx(argsv); /* work around GCC bug #13488 */
8934 case 'h': uv = (unsigned short)tuv; break;
8935 case 'l': uv = (unsigned long)tuv; break;
8937 default: uv = tuv; break;
8939 case 'q': uv = (Uquad_t)tuv; break;
8945 eptr = ebuf + sizeof ebuf;
8951 p = (char*)((c == 'X')
8952 ? "0123456789ABCDEF" : "0123456789abcdef");
8958 esignbuf[esignlen++] = '0';
8959 esignbuf[esignlen++] = c; /* 'x' or 'X' */
8965 *--eptr = '0' + dig;
8967 if (alt && *eptr != '0')
8973 *--eptr = '0' + dig;
8976 esignbuf[esignlen++] = '0';
8977 esignbuf[esignlen++] = 'b';
8980 default: /* it had better be ten or less */
8981 #if defined(PERL_Y2KWARN)
8982 if (ckWARN(WARN_Y2K)) {
8984 char *s = SvPV(sv,n);
8985 if (n >= 2 && s[n-2] == '1' && s[n-1] == '9'
8986 && (n == 2 || !isDIGIT(s[n-3])))
8988 Perl_warner(aTHX_ packWARN(WARN_Y2K),
8989 "Possible Y2K bug: %%%c %s",
8990 c, "format string following '19'");
8996 *--eptr = '0' + dig;
8997 } while (uv /= base);
9000 elen = (ebuf + sizeof ebuf) - eptr;
9003 zeros = precis - elen;
9004 else if (precis == 0 && elen == 1 && *eptr == '0')
9009 /* FLOATING POINT */
9012 c = 'f'; /* maybe %F isn't supported here */
9018 /* This is evil, but floating point is even more evil */
9020 /* for SV-style calling, we can only get NV
9021 for C-style calling, we assume %f is double;
9022 for simplicity we allow any of %Lf, %llf, %qf for long double
9026 #if defined(USE_LONG_DOUBLE)
9030 /* [perl #20339] - we should accept and ignore %lf rather than die */
9034 #if defined(USE_LONG_DOUBLE)
9035 intsize = args ? 0 : 'q';
9039 #if defined(HAS_LONG_DOUBLE)
9048 /* now we need (long double) if intsize == 'q', else (double) */
9049 nv = (args && !vectorize) ?
9050 #if LONG_DOUBLESIZE > DOUBLESIZE
9052 va_arg(*args, long double) :
9053 va_arg(*args, double)
9055 va_arg(*args, double)
9061 if (c != 'e' && c != 'E') {
9063 /* FIXME: if HAS_LONG_DOUBLE but not USE_LONG_DOUBLE this
9064 will cast our (long double) to (double) */
9065 (void)Perl_frexp(nv, &i);
9066 if (i == PERL_INT_MIN)
9067 Perl_die(aTHX_ "panic: frexp");
9069 need = BIT_DIGITS(i);
9071 need += has_precis ? precis : 6; /* known default */
9076 #ifdef HAS_LDBL_SPRINTF_BUG
9077 /* This is to try to fix a bug with irix/nonstop-ux/powerux and
9078 with sfio - Allen <allens@cpan.org> */
9081 # define MY_DBL_MAX DBL_MAX
9082 # else /* XXX guessing! HUGE_VAL may be defined as infinity, so not using */
9083 # if DOUBLESIZE >= 8
9084 # define MY_DBL_MAX 1.7976931348623157E+308L
9086 # define MY_DBL_MAX 3.40282347E+38L
9090 # ifdef HAS_LDBL_SPRINTF_BUG_LESS1 /* only between -1L & 1L - Allen */
9091 # define MY_DBL_MAX_BUG 1L
9093 # define MY_DBL_MAX_BUG MY_DBL_MAX
9097 # define MY_DBL_MIN DBL_MIN
9098 # else /* XXX guessing! -Allen */
9099 # if DOUBLESIZE >= 8
9100 # define MY_DBL_MIN 2.2250738585072014E-308L
9102 # define MY_DBL_MIN 1.17549435E-38L
9106 if ((intsize == 'q') && (c == 'f') &&
9107 ((nv < MY_DBL_MAX_BUG) && (nv > -MY_DBL_MAX_BUG)) &&
9109 /* it's going to be short enough that
9110 * long double precision is not needed */
9112 if ((nv <= 0L) && (nv >= -0L))
9113 fix_ldbl_sprintf_bug = TRUE; /* 0 is 0 - easiest */
9115 /* would use Perl_fp_class as a double-check but not
9116 * functional on IRIX - see perl.h comments */
9118 if ((nv >= MY_DBL_MIN) || (nv <= -MY_DBL_MIN)) {
9119 /* It's within the range that a double can represent */
9120 #if defined(DBL_MAX) && !defined(DBL_MIN)
9121 if ((nv >= ((long double)1/DBL_MAX)) ||
9122 (nv <= (-(long double)1/DBL_MAX)))
9124 fix_ldbl_sprintf_bug = TRUE;
9127 if (fix_ldbl_sprintf_bug == TRUE) {
9137 # undef MY_DBL_MAX_BUG
9140 #endif /* HAS_LDBL_SPRINTF_BUG */
9142 need += 20; /* fudge factor */
9143 if (PL_efloatsize < need) {
9144 Safefree(PL_efloatbuf);
9145 PL_efloatsize = need + 20; /* more fudge */
9146 New(906, PL_efloatbuf, PL_efloatsize, char);
9147 PL_efloatbuf[0] = '\0';
9150 if ( !(width || left || plus || alt) && fill != '0'
9151 && has_precis && intsize != 'q' ) { /* Shortcuts */
9152 /* See earlier comment about buggy Gconvert when digits,
9154 if ( c == 'g' && precis) {
9155 Gconvert((NV)nv, (int)precis, 0, PL_efloatbuf);
9156 if (*PL_efloatbuf) /* May return an empty string for digits==0 */
9157 goto float_converted;
9158 } else if ( c == 'f' && !precis) {
9159 if ((eptr = F0convert(nv, ebuf + sizeof ebuf, &elen)))
9163 eptr = ebuf + sizeof ebuf;
9166 /* FIXME: what to do if HAS_LONG_DOUBLE but not PERL_PRIfldbl? */
9167 #if defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl)
9168 if (intsize == 'q') {
9169 /* Copy the one or more characters in a long double
9170 * format before the 'base' ([efgEFG]) character to
9171 * the format string. */
9172 static char const prifldbl[] = PERL_PRIfldbl;
9173 char const *p = prifldbl + sizeof(prifldbl) - 3;
9174 while (p >= prifldbl) { *--eptr = *p--; }
9179 do { *--eptr = '0' + (base % 10); } while (base /= 10);
9184 do { *--eptr = '0' + (base % 10); } while (base /= 10);
9196 /* No taint. Otherwise we are in the strange situation
9197 * where printf() taints but print($float) doesn't.
9199 #if defined(HAS_LONG_DOUBLE)
9201 (void)sprintf(PL_efloatbuf, eptr, nv);
9203 (void)sprintf(PL_efloatbuf, eptr, (double)nv);
9205 (void)sprintf(PL_efloatbuf, eptr, nv);
9208 eptr = PL_efloatbuf;
9209 elen = strlen(PL_efloatbuf);
9215 i = SvCUR(sv) - origlen;
9216 if (args && !vectorize) {
9218 case 'h': *(va_arg(*args, short*)) = i; break;
9219 default: *(va_arg(*args, int*)) = i; break;
9220 case 'l': *(va_arg(*args, long*)) = i; break;
9221 case 'V': *(va_arg(*args, IV*)) = i; break;
9223 case 'q': *(va_arg(*args, Quad_t*)) = i; break;
9228 sv_setuv_mg(argsv, (UV)i);
9230 continue; /* not "break" */
9236 if (!args && ckWARN(WARN_PRINTF) &&
9237 (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) {
9238 SV *msg = sv_newmortal();
9239 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %sprintf: ",
9240 (PL_op->op_type == OP_PRTF) ? "" : "s");
9243 Perl_sv_catpvf(aTHX_ msg,
9244 "\"%%%c\"", c & 0xFF);
9246 Perl_sv_catpvf(aTHX_ msg,
9247 "\"%%\\%03"UVof"\"",
9250 sv_catpv(msg, "end of string");
9251 Perl_warner(aTHX_ packWARN(WARN_PRINTF), "%"SVf, msg); /* yes, this is reentrant */
9254 /* output mangled stuff ... */
9260 /* ... right here, because formatting flags should not apply */
9261 SvGROW(sv, SvCUR(sv) + elen + 1);
9263 Copy(eptr, p, elen, char);
9266 SvCUR(sv) = p - SvPVX(sv);
9268 continue; /* not "break" */
9271 /* calculate width before utf8_upgrade changes it */
9272 have = esignlen + zeros + elen;
9274 if (is_utf8 != has_utf8) {
9277 sv_utf8_upgrade(sv);
9280 SV *nsv = sv_2mortal(newSVpvn(eptr, elen));
9281 sv_utf8_upgrade(nsv);
9285 SvGROW(sv, SvCUR(sv) + elen + 1);
9289 /* Use memchr() instead of strchr(), as eptr is not guaranteed */
9290 /* to point to a null-terminated string. */
9291 if (left && ckWARN(WARN_PRINTF) && memchr(eptr, '\n', elen) &&
9292 (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF))
9293 Perl_warner(aTHX_ packWARN(WARN_PRINTF),
9294 "Newline in left-justified string for %sprintf",
9295 (PL_op->op_type == OP_PRTF) ? "" : "s");
9297 need = (have > width ? have : width);
9300 SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1);
9302 if (esignlen && fill == '0') {
9303 for (i = 0; i < (int)esignlen; i++)
9307 memset(p, fill, gap);
9310 if (esignlen && fill != '0') {
9311 for (i = 0; i < (int)esignlen; i++)
9315 for (i = zeros; i; i--)
9319 Copy(eptr, p, elen, char);
9323 memset(p, ' ', gap);
9328 Copy(dotstr, p, dotstrlen, char);
9332 vectorize = FALSE; /* done iterating over vecstr */
9339 SvCUR(sv) = p - SvPVX(sv);
9347 /* =========================================================================
9349 =head1 Cloning an interpreter
9351 All the macros and functions in this section are for the private use of
9352 the main function, perl_clone().
9354 The foo_dup() functions make an exact copy of an existing foo thinngy.
9355 During the course of a cloning, a hash table is used to map old addresses
9356 to new addresses. The table is created and manipulated with the
9357 ptr_table_* functions.
9361 ============================================================================*/
9364 #if defined(USE_ITHREADS)
9366 #if defined(USE_5005THREADS)
9367 # include "error: USE_5005THREADS and USE_ITHREADS are incompatible"
9370 #ifndef GpREFCNT_inc
9371 # define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL)
9375 #define sv_dup_inc(s,t) SvREFCNT_inc(sv_dup(s,t))
9376 #define av_dup(s,t) (AV*)sv_dup((SV*)s,t)
9377 #define av_dup_inc(s,t) (AV*)SvREFCNT_inc(sv_dup((SV*)s,t))
9378 #define hv_dup(s,t) (HV*)sv_dup((SV*)s,t)
9379 #define hv_dup_inc(s,t) (HV*)SvREFCNT_inc(sv_dup((SV*)s,t))
9380 #define cv_dup(s,t) (CV*)sv_dup((SV*)s,t)
9381 #define cv_dup_inc(s,t) (CV*)SvREFCNT_inc(sv_dup((SV*)s,t))
9382 #define io_dup(s,t) (IO*)sv_dup((SV*)s,t)
9383 #define io_dup_inc(s,t) (IO*)SvREFCNT_inc(sv_dup((SV*)s,t))
9384 #define gv_dup(s,t) (GV*)sv_dup((SV*)s,t)
9385 #define gv_dup_inc(s,t) (GV*)SvREFCNT_inc(sv_dup((SV*)s,t))
9386 #define SAVEPV(p) (p ? savepv(p) : Nullch)
9387 #define SAVEPVN(p,n) (p ? savepvn(p,n) : Nullch)
9390 /* Duplicate a regexp. Required reading: pregcomp() and pregfree() in
9391 regcomp.c. AMS 20010712 */
9394 Perl_re_dup(pTHX_ REGEXP *r, CLONE_PARAMS *param)
9398 struct reg_substr_datum *s;
9401 return (REGEXP *)NULL;
9403 if ((ret = (REGEXP *)ptr_table_fetch(PL_ptr_table, r)))
9406 len = r->offsets[0];
9407 npar = r->nparens+1;
9409 Newc(0, ret, sizeof(regexp) + (len+1)*sizeof(regnode), char, regexp);
9410 Copy(r->program, ret->program, len+1, regnode);
9412 New(0, ret->startp, npar, I32);
9413 Copy(r->startp, ret->startp, npar, I32);
9414 New(0, ret->endp, npar, I32);
9415 Copy(r->startp, ret->startp, npar, I32);
9417 New(0, ret->substrs, 1, struct reg_substr_data);
9418 for (s = ret->substrs->data, i = 0; i < 3; i++, s++) {
9419 s->min_offset = r->substrs->data[i].min_offset;
9420 s->max_offset = r->substrs->data[i].max_offset;
9421 s->substr = sv_dup_inc(r->substrs->data[i].substr, param);
9422 s->utf8_substr = sv_dup_inc(r->substrs->data[i].utf8_substr, param);
9425 ret->regstclass = NULL;
9428 int count = r->data->count;
9430 Newc(0, d, sizeof(struct reg_data) + count*sizeof(void *),
9431 char, struct reg_data);
9432 New(0, d->what, count, U8);
9435 for (i = 0; i < count; i++) {
9436 d->what[i] = r->data->what[i];
9437 switch (d->what[i]) {
9439 d->data[i] = sv_dup_inc((SV *)r->data->data[i], param);
9442 d->data[i] = av_dup_inc((AV *)r->data->data[i], param);
9445 /* This is cheating. */
9446 New(0, d->data[i], 1, struct regnode_charclass_class);
9447 StructCopy(r->data->data[i], d->data[i],
9448 struct regnode_charclass_class);
9449 ret->regstclass = (regnode*)d->data[i];
9452 /* Compiled op trees are readonly, and can thus be
9453 shared without duplication. */
9455 d->data[i] = (void*)OpREFCNT_inc((OP*)r->data->data[i]);
9459 d->data[i] = r->data->data[i];
9469 New(0, ret->offsets, 2*len+1, U32);
9470 Copy(r->offsets, ret->offsets, 2*len+1, U32);
9472 ret->precomp = SAVEPVN(r->precomp, r->prelen);
9473 ret->refcnt = r->refcnt;
9474 ret->minlen = r->minlen;
9475 ret->prelen = r->prelen;
9476 ret->nparens = r->nparens;
9477 ret->lastparen = r->lastparen;
9478 ret->lastcloseparen = r->lastcloseparen;
9479 ret->reganch = r->reganch;
9481 ret->sublen = r->sublen;
9483 if (RX_MATCH_COPIED(ret))
9484 ret->subbeg = SAVEPVN(r->subbeg, r->sublen);
9486 ret->subbeg = Nullch;
9488 ptr_table_store(PL_ptr_table, r, ret);
9492 /* duplicate a file handle */
9495 Perl_fp_dup(pTHX_ PerlIO *fp, char type, CLONE_PARAMS *param)
9499 return (PerlIO*)NULL;
9501 /* look for it in the table first */
9502 ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp);
9506 /* create anew and remember what it is */
9507 ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE);
9508 ptr_table_store(PL_ptr_table, fp, ret);
9512 /* duplicate a directory handle */
9515 Perl_dirp_dup(pTHX_ DIR *dp)
9523 /* duplicate a typeglob */
9526 Perl_gp_dup(pTHX_ GP *gp, CLONE_PARAMS* param)
9531 /* look for it in the table first */
9532 ret = (GP*)ptr_table_fetch(PL_ptr_table, gp);
9536 /* create anew and remember what it is */
9537 Newz(0, ret, 1, GP);
9538 ptr_table_store(PL_ptr_table, gp, ret);
9541 ret->gp_refcnt = 0; /* must be before any other dups! */
9542 ret->gp_sv = sv_dup_inc(gp->gp_sv, param);
9543 ret->gp_io = io_dup_inc(gp->gp_io, param);
9544 ret->gp_form = cv_dup_inc(gp->gp_form, param);
9545 ret->gp_av = av_dup_inc(gp->gp_av, param);
9546 ret->gp_hv = hv_dup_inc(gp->gp_hv, param);
9547 ret->gp_egv = gv_dup(gp->gp_egv, param);/* GvEGV is not refcounted */
9548 ret->gp_cv = cv_dup_inc(gp->gp_cv, param);
9549 ret->gp_cvgen = gp->gp_cvgen;
9550 ret->gp_flags = gp->gp_flags;
9551 ret->gp_line = gp->gp_line;
9552 ret->gp_file = gp->gp_file; /* points to COP.cop_file */
9556 /* duplicate a chain of magic */
9559 Perl_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS* param)
9561 MAGIC *mgprev = (MAGIC*)NULL;
9564 return (MAGIC*)NULL;
9565 /* look for it in the table first */
9566 mgret = (MAGIC*)ptr_table_fetch(PL_ptr_table, mg);
9570 for (; mg; mg = mg->mg_moremagic) {
9572 Newz(0, nmg, 1, MAGIC);
9574 mgprev->mg_moremagic = nmg;
9577 nmg->mg_virtual = mg->mg_virtual; /* XXX copy dynamic vtable? */
9578 nmg->mg_private = mg->mg_private;
9579 nmg->mg_type = mg->mg_type;
9580 nmg->mg_flags = mg->mg_flags;
9581 if (mg->mg_type == PERL_MAGIC_qr) {
9582 nmg->mg_obj = (SV*)re_dup((REGEXP*)mg->mg_obj, param);
9584 else if(mg->mg_type == PERL_MAGIC_backref) {
9585 AV *av = (AV*) mg->mg_obj;
9588 SvREFCNT_inc(nmg->mg_obj = (SV*)newAV());
9590 for (i = AvFILLp(av); i >= 0; i--) {
9591 if (!svp[i]) continue;
9592 av_push((AV*)nmg->mg_obj,sv_dup(svp[i],param));
9596 nmg->mg_obj = (mg->mg_flags & MGf_REFCOUNTED)
9597 ? sv_dup_inc(mg->mg_obj, param)
9598 : sv_dup(mg->mg_obj, param);
9600 nmg->mg_len = mg->mg_len;
9601 nmg->mg_ptr = mg->mg_ptr; /* XXX random ptr? */
9602 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
9603 if (mg->mg_len > 0) {
9604 nmg->mg_ptr = SAVEPVN(mg->mg_ptr, mg->mg_len);
9605 if (mg->mg_type == PERL_MAGIC_overload_table &&
9606 AMT_AMAGIC((AMT*)mg->mg_ptr))
9608 AMT *amtp = (AMT*)mg->mg_ptr;
9609 AMT *namtp = (AMT*)nmg->mg_ptr;
9611 for (i = 1; i < NofAMmeth; i++) {
9612 namtp->table[i] = cv_dup_inc(amtp->table[i], param);
9616 else if (mg->mg_len == HEf_SVKEY)
9617 nmg->mg_ptr = (char*)sv_dup_inc((SV*)mg->mg_ptr, param);
9619 if ((mg->mg_flags & MGf_DUP) && mg->mg_virtual && mg->mg_virtual->svt_dup) {
9620 CALL_FPTR(nmg->mg_virtual->svt_dup)(aTHX_ nmg, param);
9627 /* create a new pointer-mapping table */
9630 Perl_ptr_table_new(pTHX)
9633 Newz(0, tbl, 1, PTR_TBL_t);
9636 Newz(0, tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
9641 # define PTR_TABLE_HASH(ptr) (PTR2UV(ptr) >> 3)
9643 # define PTR_TABLE_HASH(ptr) (PTR2UV(ptr) >> 2)
9646 /* map an existing pointer using a table */
9649 Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, void *sv)
9651 PTR_TBL_ENT_t *tblent;
9652 UV hash = PTR_TABLE_HASH(sv);
9654 tblent = tbl->tbl_ary[hash & tbl->tbl_max];
9655 for (; tblent; tblent = tblent->next) {
9656 if (tblent->oldval == sv)
9657 return tblent->newval;
9662 /* add a new entry to a pointer-mapping table */
9665 Perl_ptr_table_store(pTHX_ PTR_TBL_t *tbl, void *oldv, void *newv)
9667 PTR_TBL_ENT_t *tblent, **otblent;
9668 /* XXX this may be pessimal on platforms where pointers aren't good
9669 * hash values e.g. if they grow faster in the most significant
9671 UV hash = PTR_TABLE_HASH(oldv);
9675 otblent = &tbl->tbl_ary[hash & tbl->tbl_max];
9676 for (tblent = *otblent; tblent; empty=0, tblent = tblent->next) {
9677 if (tblent->oldval == oldv) {
9678 tblent->newval = newv;
9682 Newz(0, tblent, 1, PTR_TBL_ENT_t);
9683 tblent->oldval = oldv;
9684 tblent->newval = newv;
9685 tblent->next = *otblent;
9688 if (!empty && tbl->tbl_items > tbl->tbl_max)
9689 ptr_table_split(tbl);
9692 /* double the hash bucket size of an existing ptr table */
9695 Perl_ptr_table_split(pTHX_ PTR_TBL_t *tbl)
9697 PTR_TBL_ENT_t **ary = tbl->tbl_ary;
9698 UV oldsize = tbl->tbl_max + 1;
9699 UV newsize = oldsize * 2;
9702 Renew(ary, newsize, PTR_TBL_ENT_t*);
9703 Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
9704 tbl->tbl_max = --newsize;
9706 for (i=0; i < oldsize; i++, ary++) {
9707 PTR_TBL_ENT_t **curentp, **entp, *ent;
9710 curentp = ary + oldsize;
9711 for (entp = ary, ent = *ary; ent; ent = *entp) {
9712 if ((newsize & PTR_TABLE_HASH(ent->oldval)) != i) {
9714 ent->next = *curentp;
9724 /* remove all the entries from a ptr table */
9727 Perl_ptr_table_clear(pTHX_ PTR_TBL_t *tbl)
9729 register PTR_TBL_ENT_t **array;
9730 register PTR_TBL_ENT_t *entry;
9731 register PTR_TBL_ENT_t *oentry = Null(PTR_TBL_ENT_t*);
9735 if (!tbl || !tbl->tbl_items) {
9739 array = tbl->tbl_ary;
9746 entry = entry->next;
9750 if (++riter > max) {
9753 entry = array[riter];
9760 /* clear and free a ptr table */
9763 Perl_ptr_table_free(pTHX_ PTR_TBL_t *tbl)
9768 ptr_table_clear(tbl);
9769 Safefree(tbl->tbl_ary);
9777 /* attempt to make everything in the typeglob readonly */
9780 S_gv_share(pTHX_ SV *sstr, CLONE_PARAMS *param)
9783 SV *sv = ¶m->proto_perl->Isv_no; /* just need SvREADONLY-ness */
9785 if (GvIO(gv) || GvFORM(gv)) {
9786 GvUNIQUE_off(gv); /* GvIOs cannot be shared. nor can GvFORMs */
9788 else if (!GvCV(gv)) {
9792 /* CvPADLISTs cannot be shared */
9793 if (!SvREADONLY(GvCV(gv)) && !CvXSUB(GvCV(gv))) {
9798 if (!GvUNIQUE(gv)) {
9800 PerlIO_printf(Perl_debug_log, "gv_share: unable to share %s::%s\n",
9801 HvNAME(GvSTASH(gv)), GvNAME(gv));
9807 * write attempts will die with
9808 * "Modification of a read-only value attempted"
9814 SvREADONLY_on(GvSV(gv));
9821 SvREADONLY_on(GvAV(gv));
9828 SvREADONLY_on(GvHV(gv));
9831 return sstr; /* he_dup() will SvREFCNT_inc() */
9834 /* duplicate an SV of any type (including AV, HV etc) */
9837 Perl_rvpv_dup(pTHX_ SV *dstr, SV *sstr, CLONE_PARAMS* param)
9840 SvRV(dstr) = SvWEAKREF(sstr)
9841 ? sv_dup(SvRV(sstr), param)
9842 : sv_dup_inc(SvRV(sstr), param);
9844 else if (SvPVX(sstr)) {
9845 /* Has something there */
9847 /* Normal PV - clone whole allocated space */
9848 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
9851 /* Special case - not normally malloced for some reason */
9852 if (SvREADONLY(sstr) && SvFAKE(sstr)) {
9853 /* A "shared" PV - clone it as unshared string */
9854 if(SvPADTMP(sstr)) {
9855 /* However, some of them live in the pad
9856 and they should not have these flags
9859 SvPVX(dstr) = sharepvn(SvPVX(sstr), SvCUR(sstr),
9861 SvUVX(dstr) = SvUVX(sstr);
9864 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvCUR(sstr));
9866 SvREADONLY_off(dstr);
9870 /* Some other special case - random pointer */
9871 SvPVX(dstr) = SvPVX(sstr);
9877 SvPVX(dstr) = SvPVX(sstr);
9882 Perl_sv_dup(pTHX_ SV *sstr, CLONE_PARAMS* param)
9886 if (!sstr || SvTYPE(sstr) == SVTYPEMASK)
9888 /* look for it in the table first */
9889 dstr = (SV*)ptr_table_fetch(PL_ptr_table, sstr);
9893 if(param->flags & CLONEf_JOIN_IN) {
9894 /** We are joining here so we don't want do clone
9895 something that is bad **/
9897 if(SvTYPE(sstr) == SVt_PVHV &&
9899 /** don't clone stashes if they already exist **/
9900 HV* old_stash = gv_stashpv(HvNAME(sstr),0);
9901 return (SV*) old_stash;
9905 /* create anew and remember what it is */
9907 ptr_table_store(PL_ptr_table, sstr, dstr);
9910 SvFLAGS(dstr) = SvFLAGS(sstr);
9911 SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */
9912 SvREFCNT(dstr) = 0; /* must be before any other dups! */
9915 if (SvANY(sstr) && PL_watch_pvx && SvPVX(sstr) == PL_watch_pvx)
9916 PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n",
9917 PL_watch_pvx, SvPVX(sstr));
9920 switch (SvTYPE(sstr)) {
9925 SvANY(dstr) = new_XIV();
9926 SvIVX(dstr) = SvIVX(sstr);
9929 SvANY(dstr) = new_XNV();
9930 SvNVX(dstr) = SvNVX(sstr);
9933 SvANY(dstr) = new_XRV();
9934 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
9937 SvANY(dstr) = new_XPV();
9938 SvCUR(dstr) = SvCUR(sstr);
9939 SvLEN(dstr) = SvLEN(sstr);
9940 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
9943 SvANY(dstr) = new_XPVIV();
9944 SvCUR(dstr) = SvCUR(sstr);
9945 SvLEN(dstr) = SvLEN(sstr);
9946 SvIVX(dstr) = SvIVX(sstr);
9947 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
9950 SvANY(dstr) = new_XPVNV();
9951 SvCUR(dstr) = SvCUR(sstr);
9952 SvLEN(dstr) = SvLEN(sstr);
9953 SvIVX(dstr) = SvIVX(sstr);
9954 SvNVX(dstr) = SvNVX(sstr);
9955 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
9958 SvANY(dstr) = new_XPVMG();
9959 SvCUR(dstr) = SvCUR(sstr);
9960 SvLEN(dstr) = SvLEN(sstr);
9961 SvIVX(dstr) = SvIVX(sstr);
9962 SvNVX(dstr) = SvNVX(sstr);
9963 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param);
9964 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param);
9965 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
9968 SvANY(dstr) = new_XPVBM();
9969 SvCUR(dstr) = SvCUR(sstr);
9970 SvLEN(dstr) = SvLEN(sstr);
9971 SvIVX(dstr) = SvIVX(sstr);
9972 SvNVX(dstr) = SvNVX(sstr);
9973 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param);
9974 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param);
9975 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
9976 BmRARE(dstr) = BmRARE(sstr);
9977 BmUSEFUL(dstr) = BmUSEFUL(sstr);
9978 BmPREVIOUS(dstr)= BmPREVIOUS(sstr);
9981 SvANY(dstr) = new_XPVLV();
9982 SvCUR(dstr) = SvCUR(sstr);
9983 SvLEN(dstr) = SvLEN(sstr);
9984 SvIVX(dstr) = SvIVX(sstr);
9985 SvNVX(dstr) = SvNVX(sstr);
9986 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param);
9987 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param);
9988 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
9989 LvTARGOFF(dstr) = LvTARGOFF(sstr); /* XXX sometimes holds PMOP* when DEBUGGING */
9990 LvTARGLEN(dstr) = LvTARGLEN(sstr);
9991 if (LvTYPE(sstr) == 't') /* for tie: unrefcnted fake (SV**) */
9992 LvTARG(dstr) = dstr;
9993 else if (LvTYPE(sstr) == 'T') /* for tie: fake HE */
9994 LvTARG(dstr) = (SV*)he_dup((HE*)LvTARG(sstr), 0, param);
9996 LvTARG(dstr) = sv_dup_inc(LvTARG(sstr), param);
9997 LvTYPE(dstr) = LvTYPE(sstr);
10000 if (GvUNIQUE((GV*)sstr)) {
10002 if ((share = gv_share(sstr, param))) {
10005 ptr_table_store(PL_ptr_table, sstr, dstr);
10007 PerlIO_printf(Perl_debug_log, "sv_dup: sharing %s::%s\n",
10008 HvNAME(GvSTASH(share)), GvNAME(share));
10013 SvANY(dstr) = new_XPVGV();
10014 SvCUR(dstr) = SvCUR(sstr);
10015 SvLEN(dstr) = SvLEN(sstr);
10016 SvIVX(dstr) = SvIVX(sstr);
10017 SvNVX(dstr) = SvNVX(sstr);
10018 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param);
10019 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param);
10020 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10021 GvNAMELEN(dstr) = GvNAMELEN(sstr);
10022 GvNAME(dstr) = SAVEPVN(GvNAME(sstr), GvNAMELEN(sstr));
10023 GvSTASH(dstr) = hv_dup_inc(GvSTASH(sstr), param);
10024 GvFLAGS(dstr) = GvFLAGS(sstr);
10025 GvGP(dstr) = gp_dup(GvGP(sstr), param);
10026 (void)GpREFCNT_inc(GvGP(dstr));
10029 SvANY(dstr) = new_XPVIO();
10030 SvCUR(dstr) = SvCUR(sstr);
10031 SvLEN(dstr) = SvLEN(sstr);
10032 SvIVX(dstr) = SvIVX(sstr);
10033 SvNVX(dstr) = SvNVX(sstr);
10034 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param);
10035 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param);
10036 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10037 IoIFP(dstr) = fp_dup(IoIFP(sstr), IoTYPE(sstr), param);
10038 if (IoOFP(sstr) == IoIFP(sstr))
10039 IoOFP(dstr) = IoIFP(dstr);
10041 IoOFP(dstr) = fp_dup(IoOFP(sstr), IoTYPE(sstr), param);
10042 /* PL_rsfp_filters entries have fake IoDIRP() */
10043 if (IoDIRP(sstr) && !(IoFLAGS(sstr) & IOf_FAKE_DIRP))
10044 IoDIRP(dstr) = dirp_dup(IoDIRP(sstr));
10046 IoDIRP(dstr) = IoDIRP(sstr);
10047 IoLINES(dstr) = IoLINES(sstr);
10048 IoPAGE(dstr) = IoPAGE(sstr);
10049 IoPAGE_LEN(dstr) = IoPAGE_LEN(sstr);
10050 IoLINES_LEFT(dstr) = IoLINES_LEFT(sstr);
10051 if(IoFLAGS(sstr) & IOf_FAKE_DIRP) {
10052 /* I have no idea why fake dirp (rsfps)
10053 should be treaded differently but otherwise
10054 we end up with leaks -- sky*/
10055 IoTOP_GV(dstr) = gv_dup_inc(IoTOP_GV(sstr), param);
10056 IoFMT_GV(dstr) = gv_dup_inc(IoFMT_GV(sstr), param);
10057 IoBOTTOM_GV(dstr) = gv_dup_inc(IoBOTTOM_GV(sstr), param);
10059 IoTOP_GV(dstr) = gv_dup(IoTOP_GV(sstr), param);
10060 IoFMT_GV(dstr) = gv_dup(IoFMT_GV(sstr), param);
10061 IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(sstr), param);
10063 IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(sstr));
10064 IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(sstr));
10065 IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(sstr));
10066 IoSUBPROCESS(dstr) = IoSUBPROCESS(sstr);
10067 IoTYPE(dstr) = IoTYPE(sstr);
10068 IoFLAGS(dstr) = IoFLAGS(sstr);
10071 SvANY(dstr) = new_XPVAV();
10072 SvCUR(dstr) = SvCUR(sstr);
10073 SvLEN(dstr) = SvLEN(sstr);
10074 SvIVX(dstr) = SvIVX(sstr);
10075 SvNVX(dstr) = SvNVX(sstr);
10076 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param);
10077 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param);
10078 AvARYLEN((AV*)dstr) = sv_dup_inc(AvARYLEN((AV*)sstr), param);
10079 AvFLAGS((AV*)dstr) = AvFLAGS((AV*)sstr);
10080 if (AvARRAY((AV*)sstr)) {
10081 SV **dst_ary, **src_ary;
10082 SSize_t items = AvFILLp((AV*)sstr) + 1;
10084 src_ary = AvARRAY((AV*)sstr);
10085 Newz(0, dst_ary, AvMAX((AV*)sstr)+1, SV*);
10086 ptr_table_store(PL_ptr_table, src_ary, dst_ary);
10087 SvPVX(dstr) = (char*)dst_ary;
10088 AvALLOC((AV*)dstr) = dst_ary;
10089 if (AvREAL((AV*)sstr)) {
10090 while (items-- > 0)
10091 *dst_ary++ = sv_dup_inc(*src_ary++, param);
10094 while (items-- > 0)
10095 *dst_ary++ = sv_dup(*src_ary++, param);
10097 items = AvMAX((AV*)sstr) - AvFILLp((AV*)sstr);
10098 while (items-- > 0) {
10099 *dst_ary++ = &PL_sv_undef;
10103 SvPVX(dstr) = Nullch;
10104 AvALLOC((AV*)dstr) = (SV**)NULL;
10108 SvANY(dstr) = new_XPVHV();
10109 SvCUR(dstr) = SvCUR(sstr);
10110 SvLEN(dstr) = SvLEN(sstr);
10111 SvIVX(dstr) = SvIVX(sstr);
10112 SvNVX(dstr) = SvNVX(sstr);
10113 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param);
10114 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param);
10115 HvRITER((HV*)dstr) = HvRITER((HV*)sstr);
10116 if (HvARRAY((HV*)sstr)) {
10118 XPVHV *dxhv = (XPVHV*)SvANY(dstr);
10119 XPVHV *sxhv = (XPVHV*)SvANY(sstr);
10120 Newz(0, dxhv->xhv_array,
10121 PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1), char);
10122 while (i <= sxhv->xhv_max) {
10123 ((HE**)dxhv->xhv_array)[i] = he_dup(((HE**)sxhv->xhv_array)[i],
10124 (bool)!!HvSHAREKEYS(sstr),
10128 dxhv->xhv_eiter = he_dup(sxhv->xhv_eiter,
10129 (bool)!!HvSHAREKEYS(sstr), param);
10132 SvPVX(dstr) = Nullch;
10133 HvEITER((HV*)dstr) = (HE*)NULL;
10135 HvPMROOT((HV*)dstr) = HvPMROOT((HV*)sstr); /* XXX */
10136 HvNAME((HV*)dstr) = SAVEPV(HvNAME((HV*)sstr));
10137 /* Record stashes for possible cloning in Perl_clone(). */
10138 if(HvNAME((HV*)dstr))
10139 av_push(param->stashes, dstr);
10142 SvANY(dstr) = new_XPVFM();
10143 FmLINES(dstr) = FmLINES(sstr);
10147 SvANY(dstr) = new_XPVCV();
10149 SvCUR(dstr) = SvCUR(sstr);
10150 SvLEN(dstr) = SvLEN(sstr);
10151 SvIVX(dstr) = SvIVX(sstr);
10152 SvNVX(dstr) = SvNVX(sstr);
10153 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param);
10154 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param);
10155 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10156 CvSTASH(dstr) = hv_dup(CvSTASH(sstr), param); /* NOTE: not refcounted */
10157 CvSTART(dstr) = CvSTART(sstr);
10159 CvROOT(dstr) = OpREFCNT_inc(CvROOT(sstr));
10161 CvXSUB(dstr) = CvXSUB(sstr);
10162 CvXSUBANY(dstr) = CvXSUBANY(sstr);
10163 if (CvCONST(sstr)) {
10164 CvXSUBANY(dstr).any_ptr = GvUNIQUE(CvGV(sstr)) ?
10165 SvREFCNT_inc(CvXSUBANY(sstr).any_ptr) :
10166 sv_dup_inc(CvXSUBANY(sstr).any_ptr, param);
10168 /* don't dup if copying back - CvGV isn't refcounted, so the
10169 * duped GV may never be freed. A bit of a hack! DAPM */
10170 CvGV(dstr) = (param->flags & CLONEf_JOIN_IN) ?
10171 Nullgv : gv_dup(CvGV(sstr), param) ;
10172 if (param->flags & CLONEf_COPY_STACKS) {
10173 CvDEPTH(dstr) = CvDEPTH(sstr);
10177 PAD_DUP(CvPADLIST(dstr), CvPADLIST(sstr), param);
10178 CvOUTSIDE_SEQ(dstr) = CvOUTSIDE_SEQ(sstr);
10180 CvWEAKOUTSIDE(sstr)
10181 ? cv_dup( CvOUTSIDE(sstr), param)
10182 : cv_dup_inc(CvOUTSIDE(sstr), param);
10183 CvFLAGS(dstr) = CvFLAGS(sstr);
10184 CvFILE(dstr) = CvXSUB(sstr) ? CvFILE(sstr) : SAVEPV(CvFILE(sstr));
10187 Perl_croak(aTHX_ "Bizarre SvTYPE [%" IVdf "]", (IV)SvTYPE(sstr));
10191 if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO)
10197 /* duplicate a context */
10200 Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max, CLONE_PARAMS* param)
10202 PERL_CONTEXT *ncxs;
10205 return (PERL_CONTEXT*)NULL;
10207 /* look for it in the table first */
10208 ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs);
10212 /* create anew and remember what it is */
10213 Newz(56, ncxs, max + 1, PERL_CONTEXT);
10214 ptr_table_store(PL_ptr_table, cxs, ncxs);
10217 PERL_CONTEXT *cx = &cxs[ix];
10218 PERL_CONTEXT *ncx = &ncxs[ix];
10219 ncx->cx_type = cx->cx_type;
10220 if (CxTYPE(cx) == CXt_SUBST) {
10221 Perl_croak(aTHX_ "Cloning substitution context is unimplemented");
10224 ncx->blk_oldsp = cx->blk_oldsp;
10225 ncx->blk_oldcop = cx->blk_oldcop;
10226 ncx->blk_oldretsp = cx->blk_oldretsp;
10227 ncx->blk_oldmarksp = cx->blk_oldmarksp;
10228 ncx->blk_oldscopesp = cx->blk_oldscopesp;
10229 ncx->blk_oldpm = cx->blk_oldpm;
10230 ncx->blk_gimme = cx->blk_gimme;
10231 switch (CxTYPE(cx)) {
10233 ncx->blk_sub.cv = (cx->blk_sub.olddepth == 0
10234 ? cv_dup_inc(cx->blk_sub.cv, param)
10235 : cv_dup(cx->blk_sub.cv,param));
10236 ncx->blk_sub.argarray = (cx->blk_sub.hasargs
10237 ? av_dup_inc(cx->blk_sub.argarray, param)
10239 ncx->blk_sub.savearray = av_dup_inc(cx->blk_sub.savearray, param);
10240 ncx->blk_sub.olddepth = cx->blk_sub.olddepth;
10241 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
10242 ncx->blk_sub.lval = cx->blk_sub.lval;
10245 ncx->blk_eval.old_in_eval = cx->blk_eval.old_in_eval;
10246 ncx->blk_eval.old_op_type = cx->blk_eval.old_op_type;
10247 ncx->blk_eval.old_namesv = sv_dup_inc(cx->blk_eval.old_namesv, param);
10248 ncx->blk_eval.old_eval_root = cx->blk_eval.old_eval_root;
10249 ncx->blk_eval.cur_text = sv_dup(cx->blk_eval.cur_text, param);
10252 ncx->blk_loop.label = cx->blk_loop.label;
10253 ncx->blk_loop.resetsp = cx->blk_loop.resetsp;
10254 ncx->blk_loop.redo_op = cx->blk_loop.redo_op;
10255 ncx->blk_loop.next_op = cx->blk_loop.next_op;
10256 ncx->blk_loop.last_op = cx->blk_loop.last_op;
10257 ncx->blk_loop.iterdata = (CxPADLOOP(cx)
10258 ? cx->blk_loop.iterdata
10259 : gv_dup((GV*)cx->blk_loop.iterdata, param));
10260 ncx->blk_loop.oldcomppad
10261 = (PAD*)ptr_table_fetch(PL_ptr_table,
10262 cx->blk_loop.oldcomppad);
10263 ncx->blk_loop.itersave = sv_dup_inc(cx->blk_loop.itersave, param);
10264 ncx->blk_loop.iterlval = sv_dup_inc(cx->blk_loop.iterlval, param);
10265 ncx->blk_loop.iterary = av_dup_inc(cx->blk_loop.iterary, param);
10266 ncx->blk_loop.iterix = cx->blk_loop.iterix;
10267 ncx->blk_loop.itermax = cx->blk_loop.itermax;
10270 ncx->blk_sub.cv = cv_dup(cx->blk_sub.cv, param);
10271 ncx->blk_sub.gv = gv_dup(cx->blk_sub.gv, param);
10272 ncx->blk_sub.dfoutgv = gv_dup_inc(cx->blk_sub.dfoutgv, param);
10273 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
10285 /* duplicate a stack info structure */
10288 Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param)
10293 return (PERL_SI*)NULL;
10295 /* look for it in the table first */
10296 nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si);
10300 /* create anew and remember what it is */
10301 Newz(56, nsi, 1, PERL_SI);
10302 ptr_table_store(PL_ptr_table, si, nsi);
10304 nsi->si_stack = av_dup_inc(si->si_stack, param);
10305 nsi->si_cxix = si->si_cxix;
10306 nsi->si_cxmax = si->si_cxmax;
10307 nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax, param);
10308 nsi->si_type = si->si_type;
10309 nsi->si_prev = si_dup(si->si_prev, param);
10310 nsi->si_next = si_dup(si->si_next, param);
10311 nsi->si_markoff = si->si_markoff;
10316 #define POPINT(ss,ix) ((ss)[--(ix)].any_i32)
10317 #define TOPINT(ss,ix) ((ss)[ix].any_i32)
10318 #define POPLONG(ss,ix) ((ss)[--(ix)].any_long)
10319 #define TOPLONG(ss,ix) ((ss)[ix].any_long)
10320 #define POPIV(ss,ix) ((ss)[--(ix)].any_iv)
10321 #define TOPIV(ss,ix) ((ss)[ix].any_iv)
10322 #define POPBOOL(ss,ix) ((ss)[--(ix)].any_bool)
10323 #define TOPBOOL(ss,ix) ((ss)[ix].any_bool)
10324 #define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr)
10325 #define TOPPTR(ss,ix) ((ss)[ix].any_ptr)
10326 #define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr)
10327 #define TOPDPTR(ss,ix) ((ss)[ix].any_dptr)
10328 #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr)
10329 #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr)
10332 #define pv_dup_inc(p) SAVEPV(p)
10333 #define pv_dup(p) SAVEPV(p)
10334 #define svp_dup_inc(p,pp) any_dup(p,pp)
10336 /* map any object to the new equivent - either something in the
10337 * ptr table, or something in the interpreter structure
10341 Perl_any_dup(pTHX_ void *v, PerlInterpreter *proto_perl)
10346 return (void*)NULL;
10348 /* look for it in the table first */
10349 ret = ptr_table_fetch(PL_ptr_table, v);
10353 /* see if it is part of the interpreter structure */
10354 if (v >= (void*)proto_perl && v < (void*)(proto_perl+1))
10355 ret = (void*)(((char*)aTHX) + (((char*)v) - (char*)proto_perl));
10363 /* duplicate the save stack */
10366 Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param)
10368 ANY *ss = proto_perl->Tsavestack;
10369 I32 ix = proto_perl->Tsavestack_ix;
10370 I32 max = proto_perl->Tsavestack_max;
10383 void (*dptr) (void*);
10384 void (*dxptr) (pTHX_ void*);
10387 Newz(54, nss, max, ANY);
10391 TOPINT(nss,ix) = i;
10393 case SAVEt_ITEM: /* normal string */
10394 sv = (SV*)POPPTR(ss,ix);
10395 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
10396 sv = (SV*)POPPTR(ss,ix);
10397 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
10399 case SAVEt_SV: /* scalar reference */
10400 sv = (SV*)POPPTR(ss,ix);
10401 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
10402 gv = (GV*)POPPTR(ss,ix);
10403 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
10405 case SAVEt_GENERIC_PVREF: /* generic char* */
10406 c = (char*)POPPTR(ss,ix);
10407 TOPPTR(nss,ix) = pv_dup(c);
10408 ptr = POPPTR(ss,ix);
10409 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10411 case SAVEt_SHARED_PVREF: /* char* in shared space */
10412 c = (char*)POPPTR(ss,ix);
10413 TOPPTR(nss,ix) = savesharedpv(c);
10414 ptr = POPPTR(ss,ix);
10415 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10417 case SAVEt_GENERIC_SVREF: /* generic sv */
10418 case SAVEt_SVREF: /* scalar reference */
10419 sv = (SV*)POPPTR(ss,ix);
10420 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
10421 ptr = POPPTR(ss,ix);
10422 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
10424 case SAVEt_AV: /* array reference */
10425 av = (AV*)POPPTR(ss,ix);
10426 TOPPTR(nss,ix) = av_dup_inc(av, param);
10427 gv = (GV*)POPPTR(ss,ix);
10428 TOPPTR(nss,ix) = gv_dup(gv, param);
10430 case SAVEt_HV: /* hash reference */
10431 hv = (HV*)POPPTR(ss,ix);
10432 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
10433 gv = (GV*)POPPTR(ss,ix);
10434 TOPPTR(nss,ix) = gv_dup(gv, param);
10436 case SAVEt_INT: /* int reference */
10437 ptr = POPPTR(ss,ix);
10438 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10439 intval = (int)POPINT(ss,ix);
10440 TOPINT(nss,ix) = intval;
10442 case SAVEt_LONG: /* long reference */
10443 ptr = POPPTR(ss,ix);
10444 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10445 longval = (long)POPLONG(ss,ix);
10446 TOPLONG(nss,ix) = longval;
10448 case SAVEt_I32: /* I32 reference */
10449 case SAVEt_I16: /* I16 reference */
10450 case SAVEt_I8: /* I8 reference */
10451 ptr = POPPTR(ss,ix);
10452 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10454 TOPINT(nss,ix) = i;
10456 case SAVEt_IV: /* IV reference */
10457 ptr = POPPTR(ss,ix);
10458 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10460 TOPIV(nss,ix) = iv;
10462 case SAVEt_SPTR: /* SV* reference */
10463 ptr = POPPTR(ss,ix);
10464 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10465 sv = (SV*)POPPTR(ss,ix);
10466 TOPPTR(nss,ix) = sv_dup(sv, param);
10468 case SAVEt_VPTR: /* random* reference */
10469 ptr = POPPTR(ss,ix);
10470 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10471 ptr = POPPTR(ss,ix);
10472 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10474 case SAVEt_PPTR: /* char* reference */
10475 ptr = POPPTR(ss,ix);
10476 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10477 c = (char*)POPPTR(ss,ix);
10478 TOPPTR(nss,ix) = pv_dup(c);
10480 case SAVEt_HPTR: /* HV* reference */
10481 ptr = POPPTR(ss,ix);
10482 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10483 hv = (HV*)POPPTR(ss,ix);
10484 TOPPTR(nss,ix) = hv_dup(hv, param);
10486 case SAVEt_APTR: /* AV* reference */
10487 ptr = POPPTR(ss,ix);
10488 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10489 av = (AV*)POPPTR(ss,ix);
10490 TOPPTR(nss,ix) = av_dup(av, param);
10493 gv = (GV*)POPPTR(ss,ix);
10494 TOPPTR(nss,ix) = gv_dup(gv, param);
10496 case SAVEt_GP: /* scalar reference */
10497 gp = (GP*)POPPTR(ss,ix);
10498 TOPPTR(nss,ix) = gp = gp_dup(gp, param);
10499 (void)GpREFCNT_inc(gp);
10500 gv = (GV*)POPPTR(ss,ix);
10501 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
10502 c = (char*)POPPTR(ss,ix);
10503 TOPPTR(nss,ix) = pv_dup(c);
10505 TOPIV(nss,ix) = iv;
10507 TOPIV(nss,ix) = iv;
10510 case SAVEt_MORTALIZESV:
10511 sv = (SV*)POPPTR(ss,ix);
10512 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
10515 ptr = POPPTR(ss,ix);
10516 if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) {
10517 /* these are assumed to be refcounted properly */
10518 switch (((OP*)ptr)->op_type) {
10520 case OP_LEAVESUBLV:
10524 case OP_LEAVEWRITE:
10525 TOPPTR(nss,ix) = ptr;
10530 TOPPTR(nss,ix) = Nullop;
10535 TOPPTR(nss,ix) = Nullop;
10538 c = (char*)POPPTR(ss,ix);
10539 TOPPTR(nss,ix) = pv_dup_inc(c);
10541 case SAVEt_CLEARSV:
10542 longval = POPLONG(ss,ix);
10543 TOPLONG(nss,ix) = longval;
10546 hv = (HV*)POPPTR(ss,ix);
10547 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
10548 c = (char*)POPPTR(ss,ix);
10549 TOPPTR(nss,ix) = pv_dup_inc(c);
10551 TOPINT(nss,ix) = i;
10553 case SAVEt_DESTRUCTOR:
10554 ptr = POPPTR(ss,ix);
10555 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
10556 dptr = POPDPTR(ss,ix);
10557 TOPDPTR(nss,ix) = (void (*)(void*))any_dup((void *)dptr, proto_perl);
10559 case SAVEt_DESTRUCTOR_X:
10560 ptr = POPPTR(ss,ix);
10561 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
10562 dxptr = POPDXPTR(ss,ix);
10563 TOPDXPTR(nss,ix) = (void (*)(pTHX_ void*))any_dup((void *)dxptr, proto_perl);
10565 case SAVEt_REGCONTEXT:
10568 TOPINT(nss,ix) = i;
10571 case SAVEt_STACK_POS: /* Position on Perl stack */
10573 TOPINT(nss,ix) = i;
10575 case SAVEt_AELEM: /* array element */
10576 sv = (SV*)POPPTR(ss,ix);
10577 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
10579 TOPINT(nss,ix) = i;
10580 av = (AV*)POPPTR(ss,ix);
10581 TOPPTR(nss,ix) = av_dup_inc(av, param);
10583 case SAVEt_HELEM: /* hash element */
10584 sv = (SV*)POPPTR(ss,ix);
10585 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
10586 sv = (SV*)POPPTR(ss,ix);
10587 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
10588 hv = (HV*)POPPTR(ss,ix);
10589 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
10592 ptr = POPPTR(ss,ix);
10593 TOPPTR(nss,ix) = ptr;
10597 TOPINT(nss,ix) = i;
10599 case SAVEt_COMPPAD:
10600 av = (AV*)POPPTR(ss,ix);
10601 TOPPTR(nss,ix) = av_dup(av, param);
10604 longval = (long)POPLONG(ss,ix);
10605 TOPLONG(nss,ix) = longval;
10606 ptr = POPPTR(ss,ix);
10607 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10608 sv = (SV*)POPPTR(ss,ix);
10609 TOPPTR(nss,ix) = sv_dup(sv, param);
10612 ptr = POPPTR(ss,ix);
10613 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10614 longval = (long)POPBOOL(ss,ix);
10615 TOPBOOL(nss,ix) = (bool)longval;
10618 Perl_croak(aTHX_ "panic: ss_dup inconsistency");
10626 =for apidoc perl_clone
10628 Create and return a new interpreter by cloning the current one.
10630 perl_clone takes these flags as parameters:
10632 CLONEf_COPY_STACKS - is used to, well, copy the stacks also,
10633 without it we only clone the data and zero the stacks,
10634 with it we copy the stacks and the new perl interpreter is
10635 ready to run at the exact same point as the previous one.
10636 The pseudo-fork code uses COPY_STACKS while the
10637 threads->new doesn't.
10639 CLONEf_KEEP_PTR_TABLE
10640 perl_clone keeps a ptr_table with the pointer of the old
10641 variable as a key and the new variable as a value,
10642 this allows it to check if something has been cloned and not
10643 clone it again but rather just use the value and increase the
10644 refcount. If KEEP_PTR_TABLE is not set then perl_clone will kill
10645 the ptr_table using the function
10646 C<ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;>,
10647 reason to keep it around is if you want to dup some of your own
10648 variable who are outside the graph perl scans, example of this
10649 code is in threads.xs create
10652 This is a win32 thing, it is ignored on unix, it tells perls
10653 win32host code (which is c++) to clone itself, this is needed on
10654 win32 if you want to run two threads at the same time,
10655 if you just want to do some stuff in a separate perl interpreter
10656 and then throw it away and return to the original one,
10657 you don't need to do anything.
10662 /* XXX the above needs expanding by someone who actually understands it ! */
10663 EXTERN_C PerlInterpreter *
10664 perl_clone_host(PerlInterpreter* proto_perl, UV flags);
10667 perl_clone(PerlInterpreter *proto_perl, UV flags)
10669 #ifdef PERL_IMPLICIT_SYS
10671 /* perlhost.h so we need to call into it
10672 to clone the host, CPerlHost should have a c interface, sky */
10674 if (flags & CLONEf_CLONE_HOST) {
10675 return perl_clone_host(proto_perl,flags);
10677 return perl_clone_using(proto_perl, flags,
10679 proto_perl->IMemShared,
10680 proto_perl->IMemParse,
10682 proto_perl->IStdIO,
10686 proto_perl->IProc);
10690 perl_clone_using(PerlInterpreter *proto_perl, UV flags,
10691 struct IPerlMem* ipM, struct IPerlMem* ipMS,
10692 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
10693 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
10694 struct IPerlDir* ipD, struct IPerlSock* ipS,
10695 struct IPerlProc* ipP)
10697 /* XXX many of the string copies here can be optimized if they're
10698 * constants; they need to be allocated as common memory and just
10699 * their pointers copied. */
10702 CLONE_PARAMS clone_params;
10703 CLONE_PARAMS* param = &clone_params;
10705 PerlInterpreter *my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
10706 PERL_SET_THX(my_perl);
10709 Poison(my_perl, 1, PerlInterpreter);
10713 PL_savestack_ix = 0;
10714 PL_savestack_max = -1;
10716 PL_sig_pending = 0;
10717 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
10718 # else /* !DEBUGGING */
10719 Zero(my_perl, 1, PerlInterpreter);
10720 # endif /* DEBUGGING */
10722 /* host pointers */
10724 PL_MemShared = ipMS;
10725 PL_MemParse = ipMP;
10732 #else /* !PERL_IMPLICIT_SYS */
10734 CLONE_PARAMS clone_params;
10735 CLONE_PARAMS* param = &clone_params;
10736 PerlInterpreter *my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
10737 PERL_SET_THX(my_perl);
10742 Poison(my_perl, 1, PerlInterpreter);
10746 PL_savestack_ix = 0;
10747 PL_savestack_max = -1;
10749 PL_sig_pending = 0;
10750 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
10751 # else /* !DEBUGGING */
10752 Zero(my_perl, 1, PerlInterpreter);
10753 # endif /* DEBUGGING */
10754 #endif /* PERL_IMPLICIT_SYS */
10755 param->flags = flags;
10756 param->proto_perl = proto_perl;
10759 PL_xiv_arenaroot = NULL;
10760 PL_xiv_root = NULL;
10761 PL_xnv_arenaroot = NULL;
10762 PL_xnv_root = NULL;
10763 PL_xrv_arenaroot = NULL;
10764 PL_xrv_root = NULL;
10765 PL_xpv_arenaroot = NULL;
10766 PL_xpv_root = NULL;
10767 PL_xpviv_arenaroot = NULL;
10768 PL_xpviv_root = NULL;
10769 PL_xpvnv_arenaroot = NULL;
10770 PL_xpvnv_root = NULL;
10771 PL_xpvcv_arenaroot = NULL;
10772 PL_xpvcv_root = NULL;
10773 PL_xpvav_arenaroot = NULL;
10774 PL_xpvav_root = NULL;
10775 PL_xpvhv_arenaroot = NULL;
10776 PL_xpvhv_root = NULL;
10777 PL_xpvmg_arenaroot = NULL;
10778 PL_xpvmg_root = NULL;
10779 PL_xpvlv_arenaroot = NULL;
10780 PL_xpvlv_root = NULL;
10781 PL_xpvbm_arenaroot = NULL;
10782 PL_xpvbm_root = NULL;
10783 PL_he_arenaroot = NULL;
10785 PL_nice_chunk = NULL;
10786 PL_nice_chunk_size = 0;
10788 PL_sv_objcount = 0;
10789 PL_sv_root = Nullsv;
10790 PL_sv_arenaroot = Nullsv;
10792 PL_debug = proto_perl->Idebug;
10794 #ifdef USE_REENTRANT_API
10795 /* XXX: things like -Dm will segfault here in perlio, but doing
10796 * PERL_SET_CONTEXT(proto_perl);
10797 * breaks too many other things
10799 Perl_reentrant_init(aTHX);
10802 /* create SV map for pointer relocation */
10803 PL_ptr_table = ptr_table_new();
10805 /* initialize these special pointers as early as possible */
10806 SvANY(&PL_sv_undef) = NULL;
10807 SvREFCNT(&PL_sv_undef) = (~(U32)0)/2;
10808 SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL;
10809 ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef);
10811 SvANY(&PL_sv_no) = new_XPVNV();
10812 SvREFCNT(&PL_sv_no) = (~(U32)0)/2;
10813 SvFLAGS(&PL_sv_no) = SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
10814 |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
10815 SvPVX(&PL_sv_no) = SAVEPVN(PL_No, 0);
10816 SvCUR(&PL_sv_no) = 0;
10817 SvLEN(&PL_sv_no) = 1;
10818 SvIVX(&PL_sv_no) = 0;
10819 SvNVX(&PL_sv_no) = 0;
10820 ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no);
10822 SvANY(&PL_sv_yes) = new_XPVNV();
10823 SvREFCNT(&PL_sv_yes) = (~(U32)0)/2;
10824 SvFLAGS(&PL_sv_yes) = SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
10825 |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
10826 SvPVX(&PL_sv_yes) = SAVEPVN(PL_Yes, 1);
10827 SvCUR(&PL_sv_yes) = 1;
10828 SvLEN(&PL_sv_yes) = 2;
10829 SvIVX(&PL_sv_yes) = 1;
10830 SvNVX(&PL_sv_yes) = 1;
10831 ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes);
10833 /* create (a non-shared!) shared string table */
10834 PL_strtab = newHV();
10835 HvSHAREKEYS_off(PL_strtab);
10836 hv_ksplit(PL_strtab, 512);
10837 ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab);
10839 PL_compiling = proto_perl->Icompiling;
10841 /* These two PVs will be free'd special way so must set them same way op.c does */
10842 PL_compiling.cop_stashpv = savesharedpv(PL_compiling.cop_stashpv);
10843 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_stashpv, PL_compiling.cop_stashpv);
10845 PL_compiling.cop_file = savesharedpv(PL_compiling.cop_file);
10846 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_file, PL_compiling.cop_file);
10848 ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling);
10849 if (!specialWARN(PL_compiling.cop_warnings))
10850 PL_compiling.cop_warnings = sv_dup_inc(PL_compiling.cop_warnings, param);
10851 if (!specialCopIO(PL_compiling.cop_io))
10852 PL_compiling.cop_io = sv_dup_inc(PL_compiling.cop_io, param);
10853 PL_curcop = (COP*)any_dup(proto_perl->Tcurcop, proto_perl);
10855 /* pseudo environmental stuff */
10856 PL_origargc = proto_perl->Iorigargc;
10857 PL_origargv = proto_perl->Iorigargv;
10859 param->stashes = newAV(); /* Setup array of objects to call clone on */
10861 #ifdef PERLIO_LAYERS
10862 /* Clone PerlIO tables as soon as we can handle general xx_dup() */
10863 PerlIO_clone(aTHX_ proto_perl, param);
10866 PL_envgv = gv_dup(proto_perl->Ienvgv, param);
10867 PL_incgv = gv_dup(proto_perl->Iincgv, param);
10868 PL_hintgv = gv_dup(proto_perl->Ihintgv, param);
10869 PL_origfilename = SAVEPV(proto_perl->Iorigfilename);
10870 PL_diehook = sv_dup_inc(proto_perl->Idiehook, param);
10871 PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook, param);
10874 PL_minus_c = proto_perl->Iminus_c;
10875 PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel, param);
10876 PL_localpatches = proto_perl->Ilocalpatches;
10877 PL_splitstr = proto_perl->Isplitstr;
10878 PL_preprocess = proto_perl->Ipreprocess;
10879 PL_minus_n = proto_perl->Iminus_n;
10880 PL_minus_p = proto_perl->Iminus_p;
10881 PL_minus_l = proto_perl->Iminus_l;
10882 PL_minus_a = proto_perl->Iminus_a;
10883 PL_minus_F = proto_perl->Iminus_F;
10884 PL_doswitches = proto_perl->Idoswitches;
10885 PL_dowarn = proto_perl->Idowarn;
10886 PL_doextract = proto_perl->Idoextract;
10887 PL_sawampersand = proto_perl->Isawampersand;
10888 PL_unsafe = proto_perl->Iunsafe;
10889 PL_inplace = SAVEPV(proto_perl->Iinplace);
10890 PL_e_script = sv_dup_inc(proto_perl->Ie_script, param);
10891 PL_perldb = proto_perl->Iperldb;
10892 PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
10893 PL_exit_flags = proto_perl->Iexit_flags;
10895 /* magical thingies */
10896 /* XXX time(&PL_basetime) when asked for? */
10897 PL_basetime = proto_perl->Ibasetime;
10898 PL_formfeed = sv_dup(proto_perl->Iformfeed, param);
10900 PL_maxsysfd = proto_perl->Imaxsysfd;
10901 PL_multiline = proto_perl->Imultiline;
10902 PL_statusvalue = proto_perl->Istatusvalue;
10904 PL_statusvalue_vms = proto_perl->Istatusvalue_vms;
10906 PL_encoding = sv_dup(proto_perl->Iencoding, param);
10908 sv_setpvn(PERL_DEBUG_PAD(0), "", 0); /* For regex debugging. */
10909 sv_setpvn(PERL_DEBUG_PAD(1), "", 0); /* ext/re needs these */
10910 sv_setpvn(PERL_DEBUG_PAD(2), "", 0); /* even without DEBUGGING. */
10912 /* Clone the regex array */
10913 PL_regex_padav = newAV();
10915 I32 len = av_len((AV*)proto_perl->Iregex_padav);
10916 SV** regexen = AvARRAY((AV*)proto_perl->Iregex_padav);
10917 av_push(PL_regex_padav,
10918 sv_dup_inc(regexen[0],param));
10919 for(i = 1; i <= len; i++) {
10920 if(SvREPADTMP(regexen[i])) {
10921 av_push(PL_regex_padav, sv_dup_inc(regexen[i], param));
10923 av_push(PL_regex_padav,
10925 newSViv(PTR2IV(re_dup(INT2PTR(REGEXP *,
10926 SvIVX(regexen[i])), param)))
10931 PL_regex_pad = AvARRAY(PL_regex_padav);
10933 /* shortcuts to various I/O objects */
10934 PL_stdingv = gv_dup(proto_perl->Istdingv, param);
10935 PL_stderrgv = gv_dup(proto_perl->Istderrgv, param);
10936 PL_defgv = gv_dup(proto_perl->Idefgv, param);
10937 PL_argvgv = gv_dup(proto_perl->Iargvgv, param);
10938 PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv, param);
10939 PL_argvout_stack = av_dup_inc(proto_perl->Iargvout_stack, param);
10941 /* shortcuts to regexp stuff */
10942 PL_replgv = gv_dup(proto_perl->Ireplgv, param);
10944 /* shortcuts to misc objects */
10945 PL_errgv = gv_dup(proto_perl->Ierrgv, param);
10947 /* shortcuts to debugging objects */
10948 PL_DBgv = gv_dup(proto_perl->IDBgv, param);
10949 PL_DBline = gv_dup(proto_perl->IDBline, param);
10950 PL_DBsub = gv_dup(proto_perl->IDBsub, param);
10951 PL_DBsingle = sv_dup(proto_perl->IDBsingle, param);
10952 PL_DBtrace = sv_dup(proto_perl->IDBtrace, param);
10953 PL_DBsignal = sv_dup(proto_perl->IDBsignal, param);
10954 PL_lineary = av_dup(proto_perl->Ilineary, param);
10955 PL_dbargs = av_dup(proto_perl->Idbargs, param);
10957 /* symbol tables */
10958 PL_defstash = hv_dup_inc(proto_perl->Tdefstash, param);
10959 PL_curstash = hv_dup(proto_perl->Tcurstash, param);
10960 PL_nullstash = hv_dup(proto_perl->Inullstash, param);
10961 PL_debstash = hv_dup(proto_perl->Idebstash, param);
10962 PL_globalstash = hv_dup(proto_perl->Iglobalstash, param);
10963 PL_curstname = sv_dup_inc(proto_perl->Icurstname, param);
10965 PL_beginav = av_dup_inc(proto_perl->Ibeginav, param);
10966 PL_beginav_save = av_dup_inc(proto_perl->Ibeginav_save, param);
10967 PL_checkav_save = av_dup_inc(proto_perl->Icheckav_save, param);
10968 PL_endav = av_dup_inc(proto_perl->Iendav, param);
10969 PL_checkav = av_dup_inc(proto_perl->Icheckav, param);
10970 PL_initav = av_dup_inc(proto_perl->Iinitav, param);
10972 PL_sub_generation = proto_perl->Isub_generation;
10974 /* funky return mechanisms */
10975 PL_forkprocess = proto_perl->Iforkprocess;
10977 /* subprocess state */
10978 PL_fdpid = av_dup_inc(proto_perl->Ifdpid, param);
10980 /* internal state */
10981 PL_tainting = proto_perl->Itainting;
10982 PL_taint_warn = proto_perl->Itaint_warn;
10983 PL_maxo = proto_perl->Imaxo;
10984 if (proto_perl->Iop_mask)
10985 PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo);
10987 PL_op_mask = Nullch;
10989 /* current interpreter roots */
10990 PL_main_cv = cv_dup_inc(proto_perl->Imain_cv, param);
10991 PL_main_root = OpREFCNT_inc(proto_perl->Imain_root);
10992 PL_main_start = proto_perl->Imain_start;
10993 PL_eval_root = proto_perl->Ieval_root;
10994 PL_eval_start = proto_perl->Ieval_start;
10996 /* runtime control stuff */
10997 PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl);
10998 PL_copline = proto_perl->Icopline;
11000 PL_filemode = proto_perl->Ifilemode;
11001 PL_lastfd = proto_perl->Ilastfd;
11002 PL_oldname = proto_perl->Ioldname; /* XXX not quite right */
11005 PL_gensym = proto_perl->Igensym;
11006 PL_preambled = proto_perl->Ipreambled;
11007 PL_preambleav = av_dup_inc(proto_perl->Ipreambleav, param);
11008 PL_laststatval = proto_perl->Ilaststatval;
11009 PL_laststype = proto_perl->Ilaststype;
11010 PL_mess_sv = Nullsv;
11012 PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv, param);
11013 PL_ofmt = SAVEPV(proto_perl->Iofmt);
11015 /* interpreter atexit processing */
11016 PL_exitlistlen = proto_perl->Iexitlistlen;
11017 if (PL_exitlistlen) {
11018 New(0, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
11019 Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
11022 PL_exitlist = (PerlExitListEntry*)NULL;
11023 PL_modglobal = hv_dup_inc(proto_perl->Imodglobal, param);
11024 PL_custom_op_names = hv_dup_inc(proto_perl->Icustom_op_names,param);
11025 PL_custom_op_descs = hv_dup_inc(proto_perl->Icustom_op_descs,param);
11027 PL_profiledata = NULL;
11028 PL_rsfp = fp_dup(proto_perl->Irsfp, '<', param);
11029 /* PL_rsfp_filters entries have fake IoDIRP() */
11030 PL_rsfp_filters = av_dup_inc(proto_perl->Irsfp_filters, param);
11032 PL_compcv = cv_dup(proto_perl->Icompcv, param);
11034 PAD_CLONE_VARS(proto_perl, param);
11036 #ifdef HAVE_INTERP_INTERN
11037 sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern);
11040 /* more statics moved here */
11041 PL_generation = proto_perl->Igeneration;
11042 PL_DBcv = cv_dup(proto_perl->IDBcv, param);
11044 PL_in_clean_objs = proto_perl->Iin_clean_objs;
11045 PL_in_clean_all = proto_perl->Iin_clean_all;
11047 PL_uid = proto_perl->Iuid;
11048 PL_euid = proto_perl->Ieuid;
11049 PL_gid = proto_perl->Igid;
11050 PL_egid = proto_perl->Iegid;
11051 PL_nomemok = proto_perl->Inomemok;
11052 PL_an = proto_perl->Ian;
11053 PL_op_seqmax = proto_perl->Iop_seqmax;
11054 PL_evalseq = proto_perl->Ievalseq;
11055 PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */
11056 PL_origalen = proto_perl->Iorigalen;
11057 PL_pidstatus = newHV(); /* XXX flag for cloning? */
11058 PL_osname = SAVEPV(proto_perl->Iosname);
11059 PL_sh_path_compat = proto_perl->Ish_path_compat; /* XXX never deallocated */
11060 PL_sighandlerp = proto_perl->Isighandlerp;
11063 PL_runops = proto_perl->Irunops;
11065 Copy(proto_perl->Itokenbuf, PL_tokenbuf, 256, char);
11068 PL_cshlen = proto_perl->Icshlen;
11069 PL_cshname = proto_perl->Icshname; /* XXX never deallocated */
11072 PL_lex_state = proto_perl->Ilex_state;
11073 PL_lex_defer = proto_perl->Ilex_defer;
11074 PL_lex_expect = proto_perl->Ilex_expect;
11075 PL_lex_formbrack = proto_perl->Ilex_formbrack;
11076 PL_lex_dojoin = proto_perl->Ilex_dojoin;
11077 PL_lex_starts = proto_perl->Ilex_starts;
11078 PL_lex_stuff = sv_dup_inc(proto_perl->Ilex_stuff, param);
11079 PL_lex_repl = sv_dup_inc(proto_perl->Ilex_repl, param);
11080 PL_lex_op = proto_perl->Ilex_op;
11081 PL_lex_inpat = proto_perl->Ilex_inpat;
11082 PL_lex_inwhat = proto_perl->Ilex_inwhat;
11083 PL_lex_brackets = proto_perl->Ilex_brackets;
11084 i = (PL_lex_brackets < 120 ? 120 : PL_lex_brackets);
11085 PL_lex_brackstack = SAVEPVN(proto_perl->Ilex_brackstack,i);
11086 PL_lex_casemods = proto_perl->Ilex_casemods;
11087 i = (PL_lex_casemods < 12 ? 12 : PL_lex_casemods);
11088 PL_lex_casestack = SAVEPVN(proto_perl->Ilex_casestack,i);
11090 Copy(proto_perl->Inextval, PL_nextval, 5, YYSTYPE);
11091 Copy(proto_perl->Inexttype, PL_nexttype, 5, I32);
11092 PL_nexttoke = proto_perl->Inexttoke;
11094 /* XXX This is probably masking the deeper issue of why
11095 * SvANY(proto_perl->Ilinestr) can be NULL at this point. For test case:
11096 * http://archive.develooper.com/perl5-porters%40perl.org/msg83298.html
11097 * (A little debugging with a watchpoint on it may help.)
11099 if (SvANY(proto_perl->Ilinestr)) {
11100 PL_linestr = sv_dup_inc(proto_perl->Ilinestr, param);
11101 i = proto_perl->Ibufptr - SvPVX(proto_perl->Ilinestr);
11102 PL_bufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
11103 i = proto_perl->Ioldbufptr - SvPVX(proto_perl->Ilinestr);
11104 PL_oldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
11105 i = proto_perl->Ioldoldbufptr - SvPVX(proto_perl->Ilinestr);
11106 PL_oldoldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
11107 i = proto_perl->Ilinestart - SvPVX(proto_perl->Ilinestr);
11108 PL_linestart = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
11111 PL_linestr = NEWSV(65,79);
11112 sv_upgrade(PL_linestr,SVt_PVIV);
11113 sv_setpvn(PL_linestr,"",0);
11114 PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart = SvPVX(PL_linestr);
11116 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
11117 PL_pending_ident = proto_perl->Ipending_ident;
11118 PL_sublex_info = proto_perl->Isublex_info; /* XXX not quite right */
11120 PL_expect = proto_perl->Iexpect;
11122 PL_multi_start = proto_perl->Imulti_start;
11123 PL_multi_end = proto_perl->Imulti_end;
11124 PL_multi_open = proto_perl->Imulti_open;
11125 PL_multi_close = proto_perl->Imulti_close;
11127 PL_error_count = proto_perl->Ierror_count;
11128 PL_subline = proto_perl->Isubline;
11129 PL_subname = sv_dup_inc(proto_perl->Isubname, param);
11131 /* XXX See comment on SvANY(proto_perl->Ilinestr) above */
11132 if (SvANY(proto_perl->Ilinestr)) {
11133 i = proto_perl->Ilast_uni - SvPVX(proto_perl->Ilinestr);
11134 PL_last_uni = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
11135 i = proto_perl->Ilast_lop - SvPVX(proto_perl->Ilinestr);
11136 PL_last_lop = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
11137 PL_last_lop_op = proto_perl->Ilast_lop_op;
11140 PL_last_uni = SvPVX(PL_linestr);
11141 PL_last_lop = SvPVX(PL_linestr);
11142 PL_last_lop_op = 0;
11144 PL_in_my = proto_perl->Iin_my;
11145 PL_in_my_stash = hv_dup(proto_perl->Iin_my_stash, param);
11147 PL_cryptseen = proto_perl->Icryptseen;
11150 PL_hints = proto_perl->Ihints;
11152 PL_amagic_generation = proto_perl->Iamagic_generation;
11154 #ifdef USE_LOCALE_COLLATE
11155 PL_collation_ix = proto_perl->Icollation_ix;
11156 PL_collation_name = SAVEPV(proto_perl->Icollation_name);
11157 PL_collation_standard = proto_perl->Icollation_standard;
11158 PL_collxfrm_base = proto_perl->Icollxfrm_base;
11159 PL_collxfrm_mult = proto_perl->Icollxfrm_mult;
11160 #endif /* USE_LOCALE_COLLATE */
11162 #ifdef USE_LOCALE_NUMERIC
11163 PL_numeric_name = SAVEPV(proto_perl->Inumeric_name);
11164 PL_numeric_standard = proto_perl->Inumeric_standard;
11165 PL_numeric_local = proto_perl->Inumeric_local;
11166 PL_numeric_radix_sv = sv_dup_inc(proto_perl->Inumeric_radix_sv, param);
11167 #endif /* !USE_LOCALE_NUMERIC */
11169 /* utf8 character classes */
11170 PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum, param);
11171 PL_utf8_alnumc = sv_dup_inc(proto_perl->Iutf8_alnumc, param);
11172 PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii, param);
11173 PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha, param);
11174 PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space, param);
11175 PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl, param);
11176 PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph, param);
11177 PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit, param);
11178 PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper, param);
11179 PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower, param);
11180 PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print, param);
11181 PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct, param);
11182 PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit, param);
11183 PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark, param);
11184 PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper, param);
11185 PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle, param);
11186 PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower, param);
11187 PL_utf8_tofold = sv_dup_inc(proto_perl->Iutf8_tofold, param);
11188 PL_utf8_idstart = sv_dup_inc(proto_perl->Iutf8_idstart, param);
11189 PL_utf8_idcont = sv_dup_inc(proto_perl->Iutf8_idcont, param);
11191 /* Did the locale setup indicate UTF-8? */
11192 PL_utf8locale = proto_perl->Iutf8locale;
11193 /* Unicode features (see perlrun/-C) */
11194 PL_unicode = proto_perl->Iunicode;
11196 /* Pre-5.8 signals control */
11197 PL_signals = proto_perl->Isignals;
11199 /* times() ticks per second */
11200 PL_clocktick = proto_perl->Iclocktick;
11202 /* Recursion stopper for PerlIO_find_layer */
11203 PL_in_load_module = proto_perl->Iin_load_module;
11205 /* sort() routine */
11206 PL_sort_RealCmp = proto_perl->Isort_RealCmp;
11208 /* Not really needed/useful since the reenrant_retint is "volatile",
11209 * but do it for consistency's sake. */
11210 PL_reentrant_retint = proto_perl->Ireentrant_retint;
11212 /* Hooks to shared SVs and locks. */
11213 PL_sharehook = proto_perl->Isharehook;
11214 PL_lockhook = proto_perl->Ilockhook;
11215 PL_unlockhook = proto_perl->Iunlockhook;
11216 PL_threadhook = proto_perl->Ithreadhook;
11218 PL_runops_std = proto_perl->Irunops_std;
11219 PL_runops_dbg = proto_perl->Irunops_dbg;
11221 #ifdef THREADS_HAVE_PIDS
11222 PL_ppid = proto_perl->Ippid;
11226 PL_last_swash_hv = Nullhv; /* reinits on demand */
11227 PL_last_swash_klen = 0;
11228 PL_last_swash_key[0]= '\0';
11229 PL_last_swash_tmps = (U8*)NULL;
11230 PL_last_swash_slen = 0;
11232 /* perly.c globals */
11233 PL_yydebug = proto_perl->Iyydebug;
11234 PL_yynerrs = proto_perl->Iyynerrs;
11235 PL_yyerrflag = proto_perl->Iyyerrflag;
11236 PL_yychar = proto_perl->Iyychar;
11237 PL_yyval = proto_perl->Iyyval;
11238 PL_yylval = proto_perl->Iyylval;
11240 PL_glob_index = proto_perl->Iglob_index;
11241 PL_srand_called = proto_perl->Isrand_called;
11242 PL_hash_seed = proto_perl->Ihash_seed;
11243 PL_rehash_seed = proto_perl->Irehash_seed;
11244 PL_uudmap['M'] = 0; /* reinits on demand */
11245 PL_bitcount = Nullch; /* reinits on demand */
11247 if (proto_perl->Ipsig_pend) {
11248 Newz(0, PL_psig_pend, SIG_SIZE, int);
11251 PL_psig_pend = (int*)NULL;
11254 if (proto_perl->Ipsig_ptr) {
11255 Newz(0, PL_psig_ptr, SIG_SIZE, SV*);
11256 Newz(0, PL_psig_name, SIG_SIZE, SV*);
11257 for (i = 1; i < SIG_SIZE; i++) {
11258 PL_psig_ptr[i] = sv_dup_inc(proto_perl->Ipsig_ptr[i], param);
11259 PL_psig_name[i] = sv_dup_inc(proto_perl->Ipsig_name[i], param);
11263 PL_psig_ptr = (SV**)NULL;
11264 PL_psig_name = (SV**)NULL;
11267 /* thrdvar.h stuff */
11269 if (flags & CLONEf_COPY_STACKS) {
11270 /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */
11271 PL_tmps_ix = proto_perl->Ttmps_ix;
11272 PL_tmps_max = proto_perl->Ttmps_max;
11273 PL_tmps_floor = proto_perl->Ttmps_floor;
11274 Newz(50, PL_tmps_stack, PL_tmps_max, SV*);
11276 while (i <= PL_tmps_ix) {
11277 PL_tmps_stack[i] = sv_dup_inc(proto_perl->Ttmps_stack[i], param);
11281 /* next PUSHMARK() sets *(PL_markstack_ptr+1) */
11282 i = proto_perl->Tmarkstack_max - proto_perl->Tmarkstack;
11283 Newz(54, PL_markstack, i, I32);
11284 PL_markstack_max = PL_markstack + (proto_perl->Tmarkstack_max
11285 - proto_perl->Tmarkstack);
11286 PL_markstack_ptr = PL_markstack + (proto_perl->Tmarkstack_ptr
11287 - proto_perl->Tmarkstack);
11288 Copy(proto_perl->Tmarkstack, PL_markstack,
11289 PL_markstack_ptr - PL_markstack + 1, I32);
11291 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
11292 * NOTE: unlike the others! */
11293 PL_scopestack_ix = proto_perl->Tscopestack_ix;
11294 PL_scopestack_max = proto_perl->Tscopestack_max;
11295 Newz(54, PL_scopestack, PL_scopestack_max, I32);
11296 Copy(proto_perl->Tscopestack, PL_scopestack, PL_scopestack_ix, I32);
11298 /* next push_return() sets PL_retstack[PL_retstack_ix]
11299 * NOTE: unlike the others! */
11300 PL_retstack_ix = proto_perl->Tretstack_ix;
11301 PL_retstack_max = proto_perl->Tretstack_max;
11302 Newz(54, PL_retstack, PL_retstack_max, OP*);
11303 Copy(proto_perl->Tretstack, PL_retstack, PL_retstack_ix, OP*);
11305 /* NOTE: si_dup() looks at PL_markstack */
11306 PL_curstackinfo = si_dup(proto_perl->Tcurstackinfo, param);
11308 /* PL_curstack = PL_curstackinfo->si_stack; */
11309 PL_curstack = av_dup(proto_perl->Tcurstack, param);
11310 PL_mainstack = av_dup(proto_perl->Tmainstack, param);
11312 /* next PUSHs() etc. set *(PL_stack_sp+1) */
11313 PL_stack_base = AvARRAY(PL_curstack);
11314 PL_stack_sp = PL_stack_base + (proto_perl->Tstack_sp
11315 - proto_perl->Tstack_base);
11316 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
11318 /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix]
11319 * NOTE: unlike the others! */
11320 PL_savestack_ix = proto_perl->Tsavestack_ix;
11321 PL_savestack_max = proto_perl->Tsavestack_max;
11322 /*Newz(54, PL_savestack, PL_savestack_max, ANY);*/
11323 PL_savestack = ss_dup(proto_perl, param);
11327 ENTER; /* perl_destruct() wants to LEAVE; */
11330 PL_start_env = proto_perl->Tstart_env; /* XXXXXX */
11331 PL_top_env = &PL_start_env;
11333 PL_op = proto_perl->Top;
11336 PL_Xpv = (XPV*)NULL;
11337 PL_na = proto_perl->Tna;
11339 PL_statbuf = proto_perl->Tstatbuf;
11340 PL_statcache = proto_perl->Tstatcache;
11341 PL_statgv = gv_dup(proto_perl->Tstatgv, param);
11342 PL_statname = sv_dup_inc(proto_perl->Tstatname, param);
11344 PL_timesbuf = proto_perl->Ttimesbuf;
11347 PL_tainted = proto_perl->Ttainted;
11348 PL_curpm = proto_perl->Tcurpm; /* XXX No PMOP ref count */
11349 PL_rs = sv_dup_inc(proto_perl->Trs, param);
11350 PL_last_in_gv = gv_dup(proto_perl->Tlast_in_gv, param);
11351 PL_ofs_sv = sv_dup_inc(proto_perl->Tofs_sv, param);
11352 PL_defoutgv = gv_dup_inc(proto_perl->Tdefoutgv, param);
11353 PL_chopset = proto_perl->Tchopset; /* XXX never deallocated */
11354 PL_toptarget = sv_dup_inc(proto_perl->Ttoptarget, param);
11355 PL_bodytarget = sv_dup_inc(proto_perl->Tbodytarget, param);
11356 PL_formtarget = sv_dup(proto_perl->Tformtarget, param);
11358 PL_restartop = proto_perl->Trestartop;
11359 PL_in_eval = proto_perl->Tin_eval;
11360 PL_delaymagic = proto_perl->Tdelaymagic;
11361 PL_dirty = proto_perl->Tdirty;
11362 PL_localizing = proto_perl->Tlocalizing;
11364 #ifdef PERL_FLEXIBLE_EXCEPTIONS
11365 PL_protect = proto_perl->Tprotect;
11367 PL_errors = sv_dup_inc(proto_perl->Terrors, param);
11368 PL_hv_fetch_ent_mh = Nullhe;
11369 PL_modcount = proto_perl->Tmodcount;
11370 PL_lastgotoprobe = Nullop;
11371 PL_dumpindent = proto_perl->Tdumpindent;
11373 PL_sortcop = (OP*)any_dup(proto_perl->Tsortcop, proto_perl);
11374 PL_sortstash = hv_dup(proto_perl->Tsortstash, param);
11375 PL_firstgv = gv_dup(proto_perl->Tfirstgv, param);
11376 PL_secondgv = gv_dup(proto_perl->Tsecondgv, param);
11377 PL_sortcxix = proto_perl->Tsortcxix;
11378 PL_efloatbuf = Nullch; /* reinits on demand */
11379 PL_efloatsize = 0; /* reinits on demand */
11383 PL_screamfirst = NULL;
11384 PL_screamnext = NULL;
11385 PL_maxscream = -1; /* reinits on demand */
11386 PL_lastscream = Nullsv;
11388 PL_watchaddr = NULL;
11389 PL_watchok = Nullch;
11391 PL_regdummy = proto_perl->Tregdummy;
11392 PL_regcomp_parse = Nullch;
11393 PL_regxend = Nullch;
11394 PL_regcode = (regnode*)NULL;
11397 PL_regprecomp = Nullch;
11402 PL_seen_zerolen = 0;
11404 PL_regcomp_rx = (regexp*)NULL;
11406 PL_colorset = 0; /* reinits PL_colors[] */
11407 /*PL_colors[6] = {0,0,0,0,0,0};*/
11408 PL_reg_whilem_seen = 0;
11409 PL_reginput = Nullch;
11410 PL_regbol = Nullch;
11411 PL_regeol = Nullch;
11412 PL_regstartp = (I32*)NULL;
11413 PL_regendp = (I32*)NULL;
11414 PL_reglastparen = (U32*)NULL;
11415 PL_reglastcloseparen = (U32*)NULL;
11416 PL_regtill = Nullch;
11417 PL_reg_start_tmp = (char**)NULL;
11418 PL_reg_start_tmpl = 0;
11419 PL_regdata = (struct reg_data*)NULL;
11422 PL_reg_eval_set = 0;
11424 PL_regprogram = (regnode*)NULL;
11426 PL_regcc = (CURCUR*)NULL;
11427 PL_reg_call_cc = (struct re_cc_state*)NULL;
11428 PL_reg_re = (regexp*)NULL;
11429 PL_reg_ganch = Nullch;
11430 PL_reg_sv = Nullsv;
11431 PL_reg_match_utf8 = FALSE;
11432 PL_reg_magic = (MAGIC*)NULL;
11434 PL_reg_oldcurpm = (PMOP*)NULL;
11435 PL_reg_curpm = (PMOP*)NULL;
11436 PL_reg_oldsaved = Nullch;
11437 PL_reg_oldsavedlen = 0;
11438 PL_reg_maxiter = 0;
11439 PL_reg_leftiter = 0;
11440 PL_reg_poscache = Nullch;
11441 PL_reg_poscache_size= 0;
11443 /* RE engine - function pointers */
11444 PL_regcompp = proto_perl->Tregcompp;
11445 PL_regexecp = proto_perl->Tregexecp;
11446 PL_regint_start = proto_perl->Tregint_start;
11447 PL_regint_string = proto_perl->Tregint_string;
11448 PL_regfree = proto_perl->Tregfree;
11450 PL_reginterp_cnt = 0;
11451 PL_reg_starttry = 0;
11453 /* Pluggable optimizer */
11454 PL_peepp = proto_perl->Tpeepp;
11456 PL_stashcache = newHV();
11458 if (!(flags & CLONEf_KEEP_PTR_TABLE)) {
11459 ptr_table_free(PL_ptr_table);
11460 PL_ptr_table = NULL;
11463 /* Call the ->CLONE method, if it exists, for each of the stashes
11464 identified by sv_dup() above.
11466 while(av_len(param->stashes) != -1) {
11467 HV* stash = (HV*) av_shift(param->stashes);
11468 GV* cloner = gv_fetchmethod_autoload(stash, "CLONE", 0);
11469 if (cloner && GvCV(cloner)) {
11474 XPUSHs(sv_2mortal(newSVpv(HvNAME(stash), 0)));
11476 call_sv((SV*)GvCV(cloner), G_DISCARD);
11482 SvREFCNT_dec(param->stashes);
11487 #endif /* USE_ITHREADS */
11490 =head1 Unicode Support
11492 =for apidoc sv_recode_to_utf8
11494 The encoding is assumed to be an Encode object, on entry the PV
11495 of the sv is assumed to be octets in that encoding, and the sv
11496 will be converted into Unicode (and UTF-8).
11498 If the sv already is UTF-8 (or if it is not POK), or if the encoding
11499 is not a reference, nothing is done to the sv. If the encoding is not
11500 an C<Encode::XS> Encoding object, bad things will happen.
11501 (See F<lib/encoding.pm> and L<Encode>).
11503 The PV of the sv is returned.
11508 Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding)
11510 if (SvPOK(sv) && !SvUTF8(sv) && !IN_BYTES && SvROK(encoding)) {
11524 Passing sv_yes is wrong - it needs to be or'ed set of constants
11525 for Encode::XS, while UTf-8 decode (currently) assumes a true value means
11526 remove converted chars from source.
11528 Both will default the value - let them.
11530 XPUSHs(&PL_sv_yes);
11533 call_method("decode", G_SCALAR);
11537 s = SvPV(uni, len);
11538 if (s != SvPVX(sv)) {
11539 SvGROW(sv, len + 1);
11540 Move(s, SvPVX(sv), len, char);
11541 SvCUR_set(sv, len);
11542 SvPVX(sv)[len] = 0;
11549 return SvPOKp(sv) ? SvPVX(sv) : NULL;
11553 =for apidoc sv_cat_decode
11555 The encoding is assumed to be an Encode object, the PV of the ssv is
11556 assumed to be octets in that encoding and decoding the input starts
11557 from the position which (PV + *offset) pointed to. The dsv will be
11558 concatenated the decoded UTF-8 string from ssv. Decoding will terminate
11559 when the string tstr appears in decoding output or the input ends on
11560 the PV of the ssv. The value which the offset points will be modified
11561 to the last input position on the ssv.
11563 Returns TRUE if the terminator was found, else returns FALSE.
11568 Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding,
11569 SV *ssv, int *offset, char *tstr, int tlen)
11572 if (SvPOK(ssv) && SvPOK(dsv) && SvROK(encoding) && offset) {
11583 XPUSHs(offsv = sv_2mortal(newSViv(*offset)));
11584 XPUSHs(sv_2mortal(newSVpvn(tstr, tlen)));
11586 call_method("cat_decode", G_SCALAR);
11588 ret = SvTRUE(TOPs);
11589 *offset = SvIV(offsv);
11595 Perl_croak(aTHX_ "Invalid argument to sv_cat_decode");