3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 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 #ifdef PERL_COPY_ON_WRITE
51 #define SV_COW_NEXT_SV(sv) INT2PTR(SV *,SvUVX(sv))
52 #define SV_COW_NEXT_SV_SET(current,next) SvUV_set(current, PTR2UV(next))
53 /* This is a pessimistic view. Scalar must be purely a read-write PV to copy-
57 /* ============================================================================
59 =head1 Allocation and deallocation of SVs.
61 An SV (or AV, HV, etc.) is allocated in two parts: the head (struct sv,
62 av, hv...) contains type and reference count information, as well as a
63 pointer to the body (struct xrv, xpv, xpviv...), which contains fields
64 specific to each type.
66 Normally, this allocation is done using arenas, which by default are
67 approximately 4K chunks of memory parcelled up into N heads or bodies. The
68 first slot in each arena is reserved, and is used to hold a link to the next
69 arena. In the case of heads, the unused first slot also contains some flags
70 and a note of the number of slots. Snaked through each arena chain is a
71 linked list of free items; when this becomes empty, an extra arena is
72 allocated and divided up into N items which are threaded into the free list.
74 The following global variables are associated with arenas:
76 PL_sv_arenaroot pointer to list of SV arenas
77 PL_sv_root pointer to list of free SV structures
79 PL_foo_arenaroot pointer to list of foo arenas,
80 PL_foo_root pointer to list of free foo bodies
81 ... for foo in xiv, xnv, xrv, xpv etc.
83 Note that some of the larger and more rarely used body types (eg xpvio)
84 are not allocated using arenas, but are instead just malloc()/free()ed as
85 required. Also, if PURIFY is defined, arenas are abandoned altogether,
86 with all items individually malloc()ed. In addition, a few SV heads are
87 not allocated from an arena, but are instead directly created as static
88 or auto variables, eg PL_sv_undef. The size of arenas can be changed from
89 the default by setting PERL_ARENA_SIZE appropriately at compile time.
91 The SV arena serves the secondary purpose of allowing still-live SVs
92 to be located and destroyed during final cleanup.
94 At the lowest level, the macros new_SV() and del_SV() grab and free
95 an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
96 to return the SV to the free list with error checking.) new_SV() calls
97 more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
98 SVs in the free list have their SvTYPE field set to all ones.
100 Similarly, there are macros new_XIV()/del_XIV(), new_XNV()/del_XNV() etc
101 that allocate and return individual body types. Normally these are mapped
102 to the arena-manipulating functions new_xiv()/del_xiv() etc, but may be
103 instead mapped directly to malloc()/free() if PURIFY is defined. The
104 new/del functions remove from, or add to, the appropriate PL_foo_root
105 list, and call more_xiv() etc to add a new arena if the list is empty.
107 At the time of very final cleanup, sv_free_arenas() is called from
108 perl_destruct() to physically free all the arenas allocated since the
109 start of the interpreter. Note that this also clears PL_he_arenaroot,
110 which is otherwise dealt with in hv.c.
112 Manipulation of any of the PL_*root pointers is protected by enclosing
113 LOCK_SV_MUTEX; ... UNLOCK_SV_MUTEX calls which should Do the Right Thing
114 if threads are enabled.
116 The function visit() scans the SV arenas list, and calls a specified
117 function for each SV it finds which is still live - ie which has an SvTYPE
118 other than all 1's, and a non-zero SvREFCNT. visit() is used by the
119 following functions (specified as [function that calls visit()] / [function
120 called by visit() for each SV]):
122 sv_report_used() / do_report_used()
123 dump all remaining SVs (debugging aid)
125 sv_clean_objs() / do_clean_objs(),do_clean_named_objs()
126 Attempt to free all objects pointed to by RVs,
127 and, unless DISABLE_DESTRUCTOR_KLUDGE is defined,
128 try to do the same for all objects indirectly
129 referenced by typeglobs too. Called once from
130 perl_destruct(), prior to calling sv_clean_all()
133 sv_clean_all() / do_clean_all()
134 SvREFCNT_dec(sv) each remaining SV, possibly
135 triggering an sv_free(). It also sets the
136 SVf_BREAK flag on the SV to indicate that the
137 refcnt has been artificially lowered, and thus
138 stopping sv_free() from giving spurious warnings
139 about SVs which unexpectedly have a refcnt
140 of zero. called repeatedly from perl_destruct()
141 until there are no SVs left.
145 Private API to rest of sv.c
149 new_XIV(), del_XIV(),
150 new_XNV(), del_XNV(),
155 sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
160 ============================================================================ */
165 * "A time to plant, and a time to uproot what was planted..."
168 #ifdef DEBUG_LEAKING_SCALARS
170 # define FREE_SV_DEBUG_FILE(sv) PerlMemfree((sv)->sv_debug_file)
172 # define FREE_SV_DEBUG_FILE(sv) PerlMemShared_free((sv)->sv_debug_file)
175 # define FREE_SV_DEBUG_FILE(sv)
178 #define plant_SV(p) \
180 FREE_SV_DEBUG_FILE(p); \
181 SvANY(p) = (void *)PL_sv_root; \
182 SvFLAGS(p) = SVTYPEMASK; \
187 /* sv_mutex must be held while calling uproot_SV() */
188 #define uproot_SV(p) \
191 PL_sv_root = (SV*)SvANY(p); \
196 /* new_SV(): return a new, empty SV head */
198 #ifdef DEBUG_LEAKING_SCALARS
199 /* provide a real function for a debugger to play with */
214 sv->sv_debug_optype = PL_op ? PL_op->op_type : 0;
215 sv->sv_debug_line = (U16) ((PL_copline == NOLINE) ?
216 (PL_curcop ? CopLINE(PL_curcop) : 0) : PL_copline);
217 sv->sv_debug_inpad = 0;
218 sv->sv_debug_cloned = 0;
220 sv->sv_debug_file = PL_curcop ? savepv(CopFILE(PL_curcop)): NULL;
222 sv->sv_debug_file = PL_curcop ? savesharedpv(CopFILE(PL_curcop)): NULL;
227 # define new_SV(p) (p)=S_new_SV(aTHX)
245 /* del_SV(): return an empty SV head to the free list */
260 S_del_sv(pTHX_ SV *p)
267 for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
269 svend = &sva[SvREFCNT(sva)];
270 if (p >= sv && p < svend) {
276 if (ckWARN_d(WARN_INTERNAL))
277 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
278 "Attempt to free non-arena SV: 0x%"UVxf
279 pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
286 #else /* ! DEBUGGING */
288 #define del_SV(p) plant_SV(p)
290 #endif /* DEBUGGING */
294 =head1 SV Manipulation Functions
296 =for apidoc sv_add_arena
298 Given a chunk of memory, link it to the head of the list of arenas,
299 and split it into a list of free SVs.
305 Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
311 /* The first SV in an arena isn't an SV. */
312 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
313 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
314 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
316 PL_sv_arenaroot = sva;
317 PL_sv_root = sva + 1;
319 svend = &sva[SvREFCNT(sva) - 1];
322 SvANY(sv) = (void *)(SV*)(sv + 1);
326 /* Must always set typemask because it's awlays checked in on cleanup
327 when the arenas are walked looking for objects. */
328 SvFLAGS(sv) = SVTYPEMASK;
335 SvFLAGS(sv) = SVTYPEMASK;
338 /* make some more SVs by adding another arena */
340 /* sv_mutex must be held while calling more_sv() */
347 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
348 PL_nice_chunk = Nullch;
349 PL_nice_chunk_size = 0;
352 char *chunk; /* must use New here to match call to */
353 New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */
354 sv_add_arena(chunk, 1008, 0);
360 /* visit(): call the named function for each non-free SV in the arenas
361 * whose flags field matches the flags/mask args. */
364 S_visit(pTHX_ SVFUNC_t f, U32 flags, U32 mask)
371 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
372 svend = &sva[SvREFCNT(sva)];
373 for (sv = sva + 1; sv < svend; ++sv) {
374 if (SvTYPE(sv) != SVTYPEMASK
375 && (sv->sv_flags & mask) == flags
388 /* called by sv_report_used() for each live SV */
391 do_report_used(pTHX_ SV *sv)
393 if (SvTYPE(sv) != SVTYPEMASK) {
394 PerlIO_printf(Perl_debug_log, "****\n");
401 =for apidoc sv_report_used
403 Dump the contents of all SVs not yet freed. (Debugging aid).
409 Perl_sv_report_used(pTHX)
412 visit(do_report_used, 0, 0);
416 /* called by sv_clean_objs() for each live SV */
419 do_clean_objs(pTHX_ SV *sv)
423 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
424 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv)));
436 /* XXX Might want to check arrays, etc. */
439 /* called by sv_clean_objs() for each live SV */
441 #ifndef DISABLE_DESTRUCTOR_KLUDGE
443 do_clean_named_objs(pTHX_ SV *sv)
445 if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) {
446 if ( SvOBJECT(GvSV(sv)) ||
447 (GvAV(sv) && SvOBJECT(GvAV(sv))) ||
448 (GvHV(sv) && SvOBJECT(GvHV(sv))) ||
449 (GvIO(sv) && SvOBJECT(GvIO(sv))) ||
450 (GvCV(sv) && SvOBJECT(GvCV(sv))) )
452 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv)));
453 SvFLAGS(sv) |= SVf_BREAK;
461 =for apidoc sv_clean_objs
463 Attempt to destroy all objects not yet freed
469 Perl_sv_clean_objs(pTHX)
471 PL_in_clean_objs = TRUE;
472 visit(do_clean_objs, SVf_ROK, SVf_ROK);
473 #ifndef DISABLE_DESTRUCTOR_KLUDGE
474 /* some barnacles may yet remain, clinging to typeglobs */
475 visit(do_clean_named_objs, SVt_PVGV, SVTYPEMASK);
477 PL_in_clean_objs = FALSE;
480 /* called by sv_clean_all() for each live SV */
483 do_clean_all(pTHX_ SV *sv)
485 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
486 SvFLAGS(sv) |= SVf_BREAK;
487 if (PL_comppad == (AV*)sv) {
489 PL_curpad = Null(SV**);
495 =for apidoc sv_clean_all
497 Decrement the refcnt of each remaining SV, possibly triggering a
498 cleanup. This function may have to be called multiple times to free
499 SVs which are in complex self-referential hierarchies.
505 Perl_sv_clean_all(pTHX)
508 PL_in_clean_all = TRUE;
509 cleaned = visit(do_clean_all, 0,0);
510 PL_in_clean_all = FALSE;
515 =for apidoc sv_free_arenas
517 Deallocate the memory used by all arenas. Note that all the individual SV
518 heads and bodies within the arenas must already have been freed.
524 Perl_sv_free_arenas(pTHX)
528 XPV *arena, *arenanext;
530 /* Free arenas here, but be careful about fake ones. (We assume
531 contiguity of the fake ones with the corresponding real ones.) */
533 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
534 svanext = (SV*) SvANY(sva);
535 while (svanext && SvFAKE(svanext))
536 svanext = (SV*) SvANY(svanext);
539 Safefree((void *)sva);
542 for (arena = PL_xiv_arenaroot; arena; arena = arenanext) {
543 arenanext = (XPV*)arena->xpv_pv;
546 PL_xiv_arenaroot = 0;
549 for (arena = PL_xnv_arenaroot; arena; arena = arenanext) {
550 arenanext = (XPV*)arena->xpv_pv;
553 PL_xnv_arenaroot = 0;
556 for (arena = PL_xrv_arenaroot; arena; arena = arenanext) {
557 arenanext = (XPV*)arena->xpv_pv;
560 PL_xrv_arenaroot = 0;
563 for (arena = PL_xpv_arenaroot; arena; arena = arenanext) {
564 arenanext = (XPV*)arena->xpv_pv;
567 PL_xpv_arenaroot = 0;
570 for (arena = (XPV*)PL_xpviv_arenaroot; arena; arena = arenanext) {
571 arenanext = (XPV*)arena->xpv_pv;
574 PL_xpviv_arenaroot = 0;
577 for (arena = (XPV*)PL_xpvnv_arenaroot; arena; arena = arenanext) {
578 arenanext = (XPV*)arena->xpv_pv;
581 PL_xpvnv_arenaroot = 0;
584 for (arena = (XPV*)PL_xpvcv_arenaroot; arena; arena = arenanext) {
585 arenanext = (XPV*)arena->xpv_pv;
588 PL_xpvcv_arenaroot = 0;
591 for (arena = (XPV*)PL_xpvav_arenaroot; arena; arena = arenanext) {
592 arenanext = (XPV*)arena->xpv_pv;
595 PL_xpvav_arenaroot = 0;
598 for (arena = (XPV*)PL_xpvhv_arenaroot; arena; arena = arenanext) {
599 arenanext = (XPV*)arena->xpv_pv;
602 PL_xpvhv_arenaroot = 0;
605 for (arena = (XPV*)PL_xpvmg_arenaroot; arena; arena = arenanext) {
606 arenanext = (XPV*)arena->xpv_pv;
609 PL_xpvmg_arenaroot = 0;
612 for (arena = (XPV*)PL_xpvlv_arenaroot; arena; arena = arenanext) {
613 arenanext = (XPV*)arena->xpv_pv;
616 PL_xpvlv_arenaroot = 0;
619 for (arena = (XPV*)PL_xpvbm_arenaroot; arena; arena = arenanext) {
620 arenanext = (XPV*)arena->xpv_pv;
623 PL_xpvbm_arenaroot = 0;
629 for (he = PL_he_arenaroot; he; he = he_next) {
630 he_next = HeNEXT(he);
637 #if defined(USE_ITHREADS)
639 struct ptr_tbl_ent *pte;
640 struct ptr_tbl_ent *pte_next;
641 for (pte = PL_pte_arenaroot; pte; pte = pte_next) {
642 pte_next = pte->next;
646 PL_pte_arenaroot = 0;
651 Safefree(PL_nice_chunk);
652 PL_nice_chunk = Nullch;
653 PL_nice_chunk_size = 0;
658 /* ---------------------------------------------------------------------
660 * support functions for report_uninit()
663 /* the maxiumum size of array or hash where we will scan looking
664 * for the undefined element that triggered the warning */
666 #define FUV_MAX_SEARCH_SIZE 1000
668 /* Look for an entry in the hash whose value has the same SV as val;
669 * If so, return a mortal copy of the key. */
672 S_find_hash_subscript(pTHX_ HV *hv, SV* val)
678 if (!hv || SvMAGICAL(hv) || !HvARRAY(hv) ||
679 (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
684 for (i=HvMAX(hv); i>0; i--) {
686 for (entry = array[i]; entry; entry = HeNEXT(entry)) {
687 if (HeVAL(entry) != val)
689 if ( HeVAL(entry) == &PL_sv_undef ||
690 HeVAL(entry) == &PL_sv_placeholder)
694 if (HeKLEN(entry) == HEf_SVKEY)
695 return sv_mortalcopy(HeKEY_sv(entry));
696 return sv_2mortal(newSVpvn(HeKEY(entry), HeKLEN(entry)));
702 /* Look for an entry in the array whose value has the same SV as val;
703 * If so, return the index, otherwise return -1. */
706 S_find_array_subscript(pTHX_ AV *av, SV* val)
710 if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
711 (AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
715 for (i=AvFILLp(av); i>=0; i--) {
716 if (svp[i] == val && svp[i] != &PL_sv_undef)
722 /* S_varname(): return the name of a variable, optionally with a subscript.
723 * If gv is non-zero, use the name of that global, along with gvtype (one
724 * of "$", "@", "%"); otherwise use the name of the lexical at pad offset
725 * targ. Depending on the value of the subscript_type flag, return:
728 #define FUV_SUBSCRIPT_NONE 1 /* "@foo" */
729 #define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */
730 #define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */
731 #define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */
734 S_varname(pTHX_ GV *gv, const char *gvtype, PADOFFSET targ,
735 SV* keyname, I32 aindex, int subscript_type)
741 name = sv_newmortal();
744 /* simulate gv_fullname4(), but add literal '^' for $^FOO names
745 * XXX get rid of all this if gv_fullnameX() ever supports this
749 HV *hv = GvSTASH(gv);
750 sv_setpv(name, gvtype);
753 else if (!(p=HvNAME(hv)))
755 if (strNE(p, "main")) {
757 sv_catpvn(name,"::", 2);
759 if (GvNAMELEN(gv)>= 1 &&
760 ((unsigned int)*GvNAME(gv)) <= 26)
762 Perl_sv_catpvf(aTHX_ name,"^%c", *GvNAME(gv) + 'A' - 1);
763 sv_catpvn(name,GvNAME(gv)+1,GvNAMELEN(gv)-1);
766 sv_catpvn(name,GvNAME(gv),GvNAMELEN(gv));
770 CV *cv = find_runcv(&u);
771 if (!cv || !CvPADLIST(cv))
773 av = (AV*)(*av_fetch(CvPADLIST(cv), 0, FALSE));
774 sv = *av_fetch(av, targ, FALSE);
775 /* SvLEN in a pad name is not to be trusted */
776 sv_setpv(name, SvPV_nolen(sv));
779 if (subscript_type == FUV_SUBSCRIPT_HASH) {
782 Perl_sv_catpvf(aTHX_ name, "{%s}",
783 pv_display(sv,SvPVX(keyname), SvCUR(keyname), 0, 32));
786 else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
788 Perl_sv_catpvf(aTHX_ name, "[%"IVdf"]", (IV)aindex);
790 else if (subscript_type == FUV_SUBSCRIPT_WITHIN)
791 sv_insert(name, 0, 0, "within ", 7);
798 =for apidoc find_uninit_var
800 Find the name of the undefined variable (if any) that caused the operator o
801 to issue a "Use of uninitialized value" warning.
802 If match is true, only return a name if it's value matches uninit_sv.
803 So roughly speaking, if a unary operator (such as OP_COS) generates a
804 warning, then following the direct child of the op may yield an
805 OP_PADSV or OP_GV that gives the name of the undefined variable. On the
806 other hand, with OP_ADD there are two branches to follow, so we only print
807 the variable name if we get an exact match.
809 The name is returned as a mortal SV.
811 Assumes that PL_op is the op that originally triggered the error, and that
812 PL_comppad/PL_curpad points to the currently executing pad.
818 S_find_uninit_var(pTHX_ OP* obase, SV* uninit_sv, bool match)
827 if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
828 uninit_sv == &PL_sv_placeholder)))
831 switch (obase->op_type) {
838 const bool pad = (obase->op_type == OP_PADAV || obase->op_type == OP_PADHV);
839 const bool hash = (obase->op_type == OP_PADHV || obase->op_type == OP_RV2HV);
842 int subscript_type = FUV_SUBSCRIPT_WITHIN;
844 if (pad) { /* @lex, %lex */
845 sv = PAD_SVl(obase->op_targ);
849 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
850 /* @global, %global */
851 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
854 sv = hash ? (SV*)GvHV(gv): (SV*)GvAV(gv);
856 else /* @{expr}, %{expr} */
857 return find_uninit_var(cUNOPx(obase)->op_first,
861 /* attempt to find a match within the aggregate */
863 keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
865 subscript_type = FUV_SUBSCRIPT_HASH;
868 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
870 subscript_type = FUV_SUBSCRIPT_ARRAY;
873 if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
876 return S_varname(aTHX_ gv, hash ? "%" : "@", obase->op_targ,
877 keysv, index, subscript_type);
881 if (match && PAD_SVl(obase->op_targ) != uninit_sv)
883 return S_varname(aTHX_ Nullgv, "$", obase->op_targ,
884 Nullsv, 0, FUV_SUBSCRIPT_NONE);
887 gv = cGVOPx_gv(obase);
888 if (!gv || (match && GvSV(gv) != uninit_sv))
890 return S_varname(aTHX_ gv, "$", 0, Nullsv, 0, FUV_SUBSCRIPT_NONE);
893 if (obase->op_flags & OPf_SPECIAL) { /* lexical array */
895 av = (AV*)PAD_SV(obase->op_targ);
896 if (!av || SvRMAGICAL(av))
898 svp = av_fetch(av, (I32)obase->op_private, FALSE);
899 if (!svp || *svp != uninit_sv)
902 return S_varname(aTHX_ Nullgv, "$", obase->op_targ,
903 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
906 gv = cGVOPx_gv(obase);
911 if (!av || SvRMAGICAL(av))
913 svp = av_fetch(av, (I32)obase->op_private, FALSE);
914 if (!svp || *svp != uninit_sv)
917 return S_varname(aTHX_ gv, "$", 0,
918 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
923 o = cUNOPx(obase)->op_first;
924 if (!o || o->op_type != OP_NULL ||
925 ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
927 return find_uninit_var(cBINOPo->op_last, uninit_sv, match);
932 /* $a[uninit_expr] or $h{uninit_expr} */
933 return find_uninit_var(cBINOPx(obase)->op_last, uninit_sv, match);
936 o = cBINOPx(obase)->op_first;
937 kid = cBINOPx(obase)->op_last;
939 /* get the av or hv, and optionally the gv */
941 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
942 sv = PAD_SV(o->op_targ);
944 else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
945 && cUNOPo->op_first->op_type == OP_GV)
947 gv = cGVOPx_gv(cUNOPo->op_first);
950 sv = o->op_type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)GvAV(gv);
955 if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
956 /* index is constant */
960 if (obase->op_type == OP_HELEM) {
961 HE* he = hv_fetch_ent((HV*)sv, cSVOPx_sv(kid), 0, 0);
962 if (!he || HeVAL(he) != uninit_sv)
966 svp = av_fetch((AV*)sv, SvIV(cSVOPx_sv(kid)), FALSE);
967 if (!svp || *svp != uninit_sv)
971 if (obase->op_type == OP_HELEM)
972 return S_varname(aTHX_ gv, "%", o->op_targ,
973 cSVOPx_sv(kid), 0, FUV_SUBSCRIPT_HASH);
975 return S_varname(aTHX_ gv, "@", o->op_targ, Nullsv,
976 SvIV(cSVOPx_sv(kid)), FUV_SUBSCRIPT_ARRAY);
980 /* index is an expression;
981 * attempt to find a match within the aggregate */
982 if (obase->op_type == OP_HELEM) {
983 SV *keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
985 return S_varname(aTHX_ gv, "%", o->op_targ,
986 keysv, 0, FUV_SUBSCRIPT_HASH);
989 const I32 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
991 return S_varname(aTHX_ gv, "@", o->op_targ,
992 Nullsv, index, FUV_SUBSCRIPT_ARRAY);
996 return S_varname(aTHX_ gv,
997 (o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
999 o->op_targ, Nullsv, 0, FUV_SUBSCRIPT_WITHIN);
1005 /* only examine RHS */
1006 return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv, match);
1009 o = cUNOPx(obase)->op_first;
1010 if (o->op_type == OP_PUSHMARK)
1013 if (!o->op_sibling) {
1014 /* one-arg version of open is highly magical */
1016 if (o->op_type == OP_GV) { /* open FOO; */
1018 if (match && GvSV(gv) != uninit_sv)
1020 return S_varname(aTHX_ gv, "$", 0,
1021 Nullsv, 0, FUV_SUBSCRIPT_NONE);
1023 /* other possibilities not handled are:
1024 * open $x; or open my $x; should return '${*$x}'
1025 * open expr; should return '$'.expr ideally
1031 /* ops where $_ may be an implicit arg */
1035 if ( !(obase->op_flags & OPf_STACKED)) {
1036 if (uninit_sv == ((obase->op_private & OPpTARGET_MY)
1037 ? PAD_SVl(obase->op_targ)
1040 sv = sv_newmortal();
1049 /* skip filehandle as it can't produce 'undef' warning */
1050 o = cUNOPx(obase)->op_first;
1051 if ((obase->op_flags & OPf_STACKED) && o->op_type == OP_PUSHMARK)
1052 o = o->op_sibling->op_sibling;
1059 match = 1; /* XS or custom code could trigger random warnings */
1064 if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs))
1065 return sv_2mortal(newSVpv("${$/}", 0));
1070 if (!(obase->op_flags & OPf_KIDS))
1072 o = cUNOPx(obase)->op_first;
1078 /* if all except one arg are constant, or have no side-effects,
1079 * or are optimized away, then it's unambiguous */
1081 for (kid=o; kid; kid = kid->op_sibling) {
1083 ( (kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid)))
1084 || (kid->op_type == OP_NULL && ! (kid->op_flags & OPf_KIDS))
1085 || (kid->op_type == OP_PUSHMARK)
1089 if (o2) { /* more than one found */
1096 return find_uninit_var(o2, uninit_sv, match);
1100 sv = find_uninit_var(o, uninit_sv, 1);
1112 =for apidoc report_uninit
1114 Print appropriate "Use of uninitialized variable" warning
1120 Perl_report_uninit(pTHX_ SV* uninit_sv)
1123 SV* varname = Nullsv;
1125 varname = find_uninit_var(PL_op, uninit_sv,0);
1127 sv_insert(varname, 0, 0, " ", 1);
1129 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
1130 varname ? SvPV_nolen(varname) : "",
1131 " in ", OP_DESC(PL_op));
1134 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
1138 /* grab a new IV body from the free list, allocating more if necessary */
1149 * See comment in more_xiv() -- RAM.
1151 PL_xiv_root = *(IV**)xiv;
1153 return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv));
1156 /* return an IV body to the free list */
1159 S_del_xiv(pTHX_ XPVIV *p)
1161 IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv));
1163 *(IV**)xiv = PL_xiv_root;
1168 /* allocate another arena's worth of IV bodies */
1174 register IV* xivend;
1176 New(705, ptr, PERL_ARENA_SIZE/sizeof(XPV), XPV);
1177 ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */
1178 PL_xiv_arenaroot = ptr; /* to keep Purify happy */
1181 xivend = &xiv[PERL_ARENA_SIZE / sizeof(IV) - 1];
1182 xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */
1184 while (xiv < xivend) {
1185 *(IV**)xiv = (IV *)(xiv + 1);
1191 /* grab a new NV body from the free list, allocating more if necessary */
1201 PL_xnv_root = *(NV**)xnv;
1203 return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv));
1206 /* return an NV body to the free list */
1209 S_del_xnv(pTHX_ XPVNV *p)
1211 NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv));
1213 *(NV**)xnv = PL_xnv_root;
1218 /* allocate another arena's worth of NV bodies */
1224 register NV* xnvend;
1226 New(711, ptr, PERL_ARENA_SIZE/sizeof(XPV), XPV);
1227 ptr->xpv_pv = (char*)PL_xnv_arenaroot;
1228 PL_xnv_arenaroot = ptr;
1231 xnvend = &xnv[PERL_ARENA_SIZE / sizeof(NV) - 1];
1232 xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */
1234 while (xnv < xnvend) {
1235 *(NV**)xnv = (NV*)(xnv + 1);
1241 /* grab a new struct xrv from the free list, allocating more if necessary */
1251 PL_xrv_root = (XRV*)xrv->xrv_rv;
1256 /* return a struct xrv to the free list */
1259 S_del_xrv(pTHX_ XRV *p)
1262 p->xrv_rv = (SV*)PL_xrv_root;
1267 /* allocate another arena's worth of struct xrv */
1273 register XRV* xrvend;
1275 New(712, ptr, PERL_ARENA_SIZE/sizeof(XPV), XPV);
1276 ptr->xpv_pv = (char*)PL_xrv_arenaroot;
1277 PL_xrv_arenaroot = ptr;
1280 xrvend = &xrv[PERL_ARENA_SIZE / sizeof(XRV) - 1];
1281 xrv += (sizeof(XPV) - 1) / sizeof(XRV) + 1;
1283 while (xrv < xrvend) {
1284 xrv->xrv_rv = (SV*)(xrv + 1);
1290 /* grab a new struct xpv from the free list, allocating more if necessary */
1300 PL_xpv_root = (XPV*)xpv->xpv_pv;
1305 /* return a struct xpv to the free list */
1308 S_del_xpv(pTHX_ XPV *p)
1311 p->xpv_pv = (char*)PL_xpv_root;
1316 /* allocate another arena's worth of struct xpv */
1322 register XPV* xpvend;
1323 New(713, xpv, PERL_ARENA_SIZE/sizeof(XPV), XPV);
1324 xpv->xpv_pv = (char*)PL_xpv_arenaroot;
1325 PL_xpv_arenaroot = xpv;
1327 xpvend = &xpv[PERL_ARENA_SIZE / sizeof(XPV) - 1];
1328 PL_xpv_root = ++xpv;
1329 while (xpv < xpvend) {
1330 xpv->xpv_pv = (char*)(xpv + 1);
1336 /* grab a new struct xpviv from the free list, allocating more if necessary */
1345 xpviv = PL_xpviv_root;
1346 PL_xpviv_root = (XPVIV*)xpviv->xpv_pv;
1351 /* return a struct xpviv to the free list */
1354 S_del_xpviv(pTHX_ XPVIV *p)
1357 p->xpv_pv = (char*)PL_xpviv_root;
1362 /* allocate another arena's worth of struct xpviv */
1367 register XPVIV* xpviv;
1368 register XPVIV* xpvivend;
1369 New(714, xpviv, PERL_ARENA_SIZE/sizeof(XPVIV), XPVIV);
1370 xpviv->xpv_pv = (char*)PL_xpviv_arenaroot;
1371 PL_xpviv_arenaroot = xpviv;
1373 xpvivend = &xpviv[PERL_ARENA_SIZE / sizeof(XPVIV) - 1];
1374 PL_xpviv_root = ++xpviv;
1375 while (xpviv < xpvivend) {
1376 xpviv->xpv_pv = (char*)(xpviv + 1);
1382 /* grab a new struct xpvnv from the free list, allocating more if necessary */
1391 xpvnv = PL_xpvnv_root;
1392 PL_xpvnv_root = (XPVNV*)xpvnv->xpv_pv;
1397 /* return a struct xpvnv to the free list */
1400 S_del_xpvnv(pTHX_ XPVNV *p)
1403 p->xpv_pv = (char*)PL_xpvnv_root;
1408 /* allocate another arena's worth of struct xpvnv */
1413 register XPVNV* xpvnv;
1414 register XPVNV* xpvnvend;
1415 New(715, xpvnv, PERL_ARENA_SIZE/sizeof(XPVNV), XPVNV);
1416 xpvnv->xpv_pv = (char*)PL_xpvnv_arenaroot;
1417 PL_xpvnv_arenaroot = xpvnv;
1419 xpvnvend = &xpvnv[PERL_ARENA_SIZE / sizeof(XPVNV) - 1];
1420 PL_xpvnv_root = ++xpvnv;
1421 while (xpvnv < xpvnvend) {
1422 xpvnv->xpv_pv = (char*)(xpvnv + 1);
1428 /* grab a new struct xpvcv from the free list, allocating more if necessary */
1437 xpvcv = PL_xpvcv_root;
1438 PL_xpvcv_root = (XPVCV*)xpvcv->xpv_pv;
1443 /* return a struct xpvcv to the free list */
1446 S_del_xpvcv(pTHX_ XPVCV *p)
1449 p->xpv_pv = (char*)PL_xpvcv_root;
1454 /* allocate another arena's worth of struct xpvcv */
1459 register XPVCV* xpvcv;
1460 register XPVCV* xpvcvend;
1461 New(716, xpvcv, PERL_ARENA_SIZE/sizeof(XPVCV), XPVCV);
1462 xpvcv->xpv_pv = (char*)PL_xpvcv_arenaroot;
1463 PL_xpvcv_arenaroot = xpvcv;
1465 xpvcvend = &xpvcv[PERL_ARENA_SIZE / sizeof(XPVCV) - 1];
1466 PL_xpvcv_root = ++xpvcv;
1467 while (xpvcv < xpvcvend) {
1468 xpvcv->xpv_pv = (char*)(xpvcv + 1);
1474 /* grab a new struct xpvav from the free list, allocating more if necessary */
1483 xpvav = PL_xpvav_root;
1484 PL_xpvav_root = (XPVAV*)xpvav->xav_array;
1489 /* return a struct xpvav to the free list */
1492 S_del_xpvav(pTHX_ XPVAV *p)
1495 p->xav_array = (char*)PL_xpvav_root;
1500 /* allocate another arena's worth of struct xpvav */
1505 register XPVAV* xpvav;
1506 register XPVAV* xpvavend;
1507 New(717, xpvav, PERL_ARENA_SIZE/sizeof(XPVAV), XPVAV);
1508 xpvav->xav_array = (char*)PL_xpvav_arenaroot;
1509 PL_xpvav_arenaroot = xpvav;
1511 xpvavend = &xpvav[PERL_ARENA_SIZE / sizeof(XPVAV) - 1];
1512 PL_xpvav_root = ++xpvav;
1513 while (xpvav < xpvavend) {
1514 xpvav->xav_array = (char*)(xpvav + 1);
1517 xpvav->xav_array = 0;
1520 /* grab a new struct xpvhv from the free list, allocating more if necessary */
1529 xpvhv = PL_xpvhv_root;
1530 PL_xpvhv_root = (XPVHV*)xpvhv->xhv_array;
1535 /* return a struct xpvhv to the free list */
1538 S_del_xpvhv(pTHX_ XPVHV *p)
1541 p->xhv_array = (char*)PL_xpvhv_root;
1546 /* allocate another arena's worth of struct xpvhv */
1551 register XPVHV* xpvhv;
1552 register XPVHV* xpvhvend;
1553 New(718, xpvhv, PERL_ARENA_SIZE/sizeof(XPVHV), XPVHV);
1554 xpvhv->xhv_array = (char*)PL_xpvhv_arenaroot;
1555 PL_xpvhv_arenaroot = xpvhv;
1557 xpvhvend = &xpvhv[PERL_ARENA_SIZE / sizeof(XPVHV) - 1];
1558 PL_xpvhv_root = ++xpvhv;
1559 while (xpvhv < xpvhvend) {
1560 xpvhv->xhv_array = (char*)(xpvhv + 1);
1563 xpvhv->xhv_array = 0;
1566 /* grab a new struct xpvmg from the free list, allocating more if necessary */
1575 xpvmg = PL_xpvmg_root;
1576 PL_xpvmg_root = (XPVMG*)xpvmg->xpv_pv;
1581 /* return a struct xpvmg to the free list */
1584 S_del_xpvmg(pTHX_ XPVMG *p)
1587 p->xpv_pv = (char*)PL_xpvmg_root;
1592 /* allocate another arena's worth of struct xpvmg */
1597 register XPVMG* xpvmg;
1598 register XPVMG* xpvmgend;
1599 New(719, xpvmg, PERL_ARENA_SIZE/sizeof(XPVMG), XPVMG);
1600 xpvmg->xpv_pv = (char*)PL_xpvmg_arenaroot;
1601 PL_xpvmg_arenaroot = xpvmg;
1603 xpvmgend = &xpvmg[PERL_ARENA_SIZE / sizeof(XPVMG) - 1];
1604 PL_xpvmg_root = ++xpvmg;
1605 while (xpvmg < xpvmgend) {
1606 xpvmg->xpv_pv = (char*)(xpvmg + 1);
1612 /* grab a new struct xpvlv from the free list, allocating more if necessary */
1621 xpvlv = PL_xpvlv_root;
1622 PL_xpvlv_root = (XPVLV*)xpvlv->xpv_pv;
1627 /* return a struct xpvlv to the free list */
1630 S_del_xpvlv(pTHX_ XPVLV *p)
1633 p->xpv_pv = (char*)PL_xpvlv_root;
1638 /* allocate another arena's worth of struct xpvlv */
1643 register XPVLV* xpvlv;
1644 register XPVLV* xpvlvend;
1645 New(720, xpvlv, PERL_ARENA_SIZE/sizeof(XPVLV), XPVLV);
1646 xpvlv->xpv_pv = (char*)PL_xpvlv_arenaroot;
1647 PL_xpvlv_arenaroot = xpvlv;
1649 xpvlvend = &xpvlv[PERL_ARENA_SIZE / sizeof(XPVLV) - 1];
1650 PL_xpvlv_root = ++xpvlv;
1651 while (xpvlv < xpvlvend) {
1652 xpvlv->xpv_pv = (char*)(xpvlv + 1);
1658 /* grab a new struct xpvbm from the free list, allocating more if necessary */
1667 xpvbm = PL_xpvbm_root;
1668 PL_xpvbm_root = (XPVBM*)xpvbm->xpv_pv;
1673 /* return a struct xpvbm to the free list */
1676 S_del_xpvbm(pTHX_ XPVBM *p)
1679 p->xpv_pv = (char*)PL_xpvbm_root;
1684 /* allocate another arena's worth of struct xpvbm */
1689 register XPVBM* xpvbm;
1690 register XPVBM* xpvbmend;
1691 New(721, xpvbm, PERL_ARENA_SIZE/sizeof(XPVBM), XPVBM);
1692 xpvbm->xpv_pv = (char*)PL_xpvbm_arenaroot;
1693 PL_xpvbm_arenaroot = xpvbm;
1695 xpvbmend = &xpvbm[PERL_ARENA_SIZE / sizeof(XPVBM) - 1];
1696 PL_xpvbm_root = ++xpvbm;
1697 while (xpvbm < xpvbmend) {
1698 xpvbm->xpv_pv = (char*)(xpvbm + 1);
1704 #define my_safemalloc(s) (void*)safemalloc(s)
1705 #define my_safefree(p) safefree((char*)p)
1709 #define new_XIV() my_safemalloc(sizeof(XPVIV))
1710 #define del_XIV(p) my_safefree(p)
1712 #define new_XNV() my_safemalloc(sizeof(XPVNV))
1713 #define del_XNV(p) my_safefree(p)
1715 #define new_XRV() my_safemalloc(sizeof(XRV))
1716 #define del_XRV(p) my_safefree(p)
1718 #define new_XPV() my_safemalloc(sizeof(XPV))
1719 #define del_XPV(p) my_safefree(p)
1721 #define new_XPVIV() my_safemalloc(sizeof(XPVIV))
1722 #define del_XPVIV(p) my_safefree(p)
1724 #define new_XPVNV() my_safemalloc(sizeof(XPVNV))
1725 #define del_XPVNV(p) my_safefree(p)
1727 #define new_XPVCV() my_safemalloc(sizeof(XPVCV))
1728 #define del_XPVCV(p) my_safefree(p)
1730 #define new_XPVAV() my_safemalloc(sizeof(XPVAV))
1731 #define del_XPVAV(p) my_safefree(p)
1733 #define new_XPVHV() my_safemalloc(sizeof(XPVHV))
1734 #define del_XPVHV(p) my_safefree(p)
1736 #define new_XPVMG() my_safemalloc(sizeof(XPVMG))
1737 #define del_XPVMG(p) my_safefree(p)
1739 #define new_XPVLV() my_safemalloc(sizeof(XPVLV))
1740 #define del_XPVLV(p) my_safefree(p)
1742 #define new_XPVBM() my_safemalloc(sizeof(XPVBM))
1743 #define del_XPVBM(p) my_safefree(p)
1747 #define new_XIV() (void*)new_xiv()
1748 #define del_XIV(p) del_xiv((XPVIV*) p)
1750 #define new_XNV() (void*)new_xnv()
1751 #define del_XNV(p) del_xnv((XPVNV*) p)
1753 #define new_XRV() (void*)new_xrv()
1754 #define del_XRV(p) del_xrv((XRV*) p)
1756 #define new_XPV() (void*)new_xpv()
1757 #define del_XPV(p) del_xpv((XPV *)p)
1759 #define new_XPVIV() (void*)new_xpviv()
1760 #define del_XPVIV(p) del_xpviv((XPVIV *)p)
1762 #define new_XPVNV() (void*)new_xpvnv()
1763 #define del_XPVNV(p) del_xpvnv((XPVNV *)p)
1765 #define new_XPVCV() (void*)new_xpvcv()
1766 #define del_XPVCV(p) del_xpvcv((XPVCV *)p)
1768 #define new_XPVAV() (void*)new_xpvav()
1769 #define del_XPVAV(p) del_xpvav((XPVAV *)p)
1771 #define new_XPVHV() (void*)new_xpvhv()
1772 #define del_XPVHV(p) del_xpvhv((XPVHV *)p)
1774 #define new_XPVMG() (void*)new_xpvmg()
1775 #define del_XPVMG(p) del_xpvmg((XPVMG *)p)
1777 #define new_XPVLV() (void*)new_xpvlv()
1778 #define del_XPVLV(p) del_xpvlv((XPVLV *)p)
1780 #define new_XPVBM() (void*)new_xpvbm()
1781 #define del_XPVBM(p) del_xpvbm((XPVBM *)p)
1785 #define new_XPVGV() my_safemalloc(sizeof(XPVGV))
1786 #define del_XPVGV(p) my_safefree(p)
1788 #define new_XPVFM() my_safemalloc(sizeof(XPVFM))
1789 #define del_XPVFM(p) my_safefree(p)
1791 #define new_XPVIO() my_safemalloc(sizeof(XPVIO))
1792 #define del_XPVIO(p) my_safefree(p)
1795 =for apidoc sv_upgrade
1797 Upgrade an SV to a more complex form. Generally adds a new body type to the
1798 SV, then copies across as much information as possible from the old body.
1799 You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>.
1805 Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
1816 if (mt != SVt_PV && SvIsCOW(sv)) {
1817 sv_force_normal_flags(sv, 0);
1820 if (SvTYPE(sv) == mt)
1831 switch (SvTYPE(sv)) {
1839 else if (mt < SVt_PVIV)
1849 pv = (char*)SvRV(sv);
1859 else if (mt == SVt_NV)
1867 del_XPVIV(SvANY(sv));
1875 del_XPVNV(SvANY(sv));
1878 /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
1879 there's no way that it can be safely upgraded, because perl.c
1880 expects to Safefree(SvANY(PL_mess_sv)) */
1881 assert(sv != PL_mess_sv);
1887 magic = SvMAGIC(sv);
1888 stash = SvSTASH(sv);
1889 del_XPVMG(SvANY(sv));
1892 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
1895 SvFLAGS(sv) &= ~SVTYPEMASK;
1900 Perl_croak(aTHX_ "Can't upgrade to undef");
1902 SvANY(sv) = new_XIV();
1906 SvANY(sv) = new_XNV();
1910 SvANY(sv) = new_XRV();
1911 SvRV_set(sv, (SV*)pv);
1914 SvANY(sv) = new_XPVHV();
1921 HvTOTALKEYS(sv) = 0;
1922 HvPLACEHOLDERS(sv) = 0;
1924 /* Fall through... */
1927 SvANY(sv) = new_XPVAV();
1932 AvFLAGS(sv) = AVf_REAL;
1937 /* XXX? Only SVt_NULL is ever upgraded to AV or HV? */
1939 /* FIXME. Should be able to remove all this if()... if the above
1940 assertion is genuinely always true. */
1943 SvFLAGS(sv) &= ~SVf_OOK;
1946 SvPV_set(sv, (char*)0);
1947 SvMAGIC_set(sv, magic);
1948 SvSTASH_set(sv, stash);
1952 SvANY(sv) = new_XPVIO();
1953 Zero(SvANY(sv), 1, XPVIO);
1954 IoPAGE_LEN(sv) = 60;
1955 goto set_magic_common;
1957 SvANY(sv) = new_XPVFM();
1958 Zero(SvANY(sv), 1, XPVFM);
1959 goto set_magic_common;
1961 SvANY(sv) = new_XPVBM();
1965 goto set_magic_common;
1967 SvANY(sv) = new_XPVGV();
1973 goto set_magic_common;
1975 SvANY(sv) = new_XPVCV();
1976 Zero(SvANY(sv), 1, XPVCV);
1977 goto set_magic_common;
1979 SvANY(sv) = new_XPVLV();
1992 SvANY(sv) = new_XPVMG();
1995 SvMAGIC_set(sv, magic);
1996 SvSTASH_set(sv, stash);
2000 SvANY(sv) = new_XPVNV();
2006 SvANY(sv) = new_XPVIV();
2015 SvANY(sv) = new_XPV();
2026 =for apidoc sv_backoff
2028 Remove any string offset. You should normally use the C<SvOOK_off> macro
2035 Perl_sv_backoff(pTHX_ register SV *sv)
2039 char *s = SvPVX(sv);
2040 SvLEN_set(sv, SvLEN(sv) + SvIVX(sv));
2041 SvPV_set(sv, SvPVX(sv) - SvIVX(sv));
2043 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
2045 SvFLAGS(sv) &= ~SVf_OOK;
2052 Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
2053 upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
2054 Use the C<SvGROW> wrapper instead.
2060 Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
2064 #ifdef HAS_64K_LIMIT
2065 if (newlen >= 0x10000) {
2066 PerlIO_printf(Perl_debug_log,
2067 "Allocation too large: %"UVxf"\n", (UV)newlen);
2070 #endif /* HAS_64K_LIMIT */
2073 if (SvTYPE(sv) < SVt_PV) {
2074 sv_upgrade(sv, SVt_PV);
2077 else if (SvOOK(sv)) { /* pv is offset? */
2080 if (newlen > SvLEN(sv))
2081 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
2082 #ifdef HAS_64K_LIMIT
2083 if (newlen >= 0x10000)
2090 if (newlen > SvLEN(sv)) { /* need more room? */
2091 if (SvLEN(sv) && s) {
2093 STRLEN l = malloced_size((void*)SvPVX(sv));
2099 Renew(s,newlen,char);
2102 New(703, s, newlen, char);
2103 if (SvPVX(sv) && SvCUR(sv)) {
2104 Move(SvPVX(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
2108 SvLEN_set(sv, newlen);
2114 =for apidoc sv_setiv
2116 Copies an integer into the given SV, upgrading first if necessary.
2117 Does not handle 'set' magic. See also C<sv_setiv_mg>.
2123 Perl_sv_setiv(pTHX_ register SV *sv, IV i)
2125 SV_CHECK_THINKFIRST_COW_DROP(sv);
2126 switch (SvTYPE(sv)) {
2128 sv_upgrade(sv, SVt_IV);
2131 sv_upgrade(sv, SVt_PVNV);
2135 sv_upgrade(sv, SVt_PVIV);
2144 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
2147 (void)SvIOK_only(sv); /* validate number */
2153 =for apidoc sv_setiv_mg
2155 Like C<sv_setiv>, but also handles 'set' magic.
2161 Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
2168 =for apidoc sv_setuv
2170 Copies an unsigned integer into the given SV, upgrading first if necessary.
2171 Does not handle 'set' magic. See also C<sv_setuv_mg>.
2177 Perl_sv_setuv(pTHX_ register SV *sv, UV u)
2179 /* With these two if statements:
2180 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
2183 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
2185 If you wish to remove them, please benchmark to see what the effect is
2187 if (u <= (UV)IV_MAX) {
2188 sv_setiv(sv, (IV)u);
2197 =for apidoc sv_setuv_mg
2199 Like C<sv_setuv>, but also handles 'set' magic.
2205 Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
2207 /* With these two if statements:
2208 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
2211 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
2213 If you wish to remove them, please benchmark to see what the effect is
2215 if (u <= (UV)IV_MAX) {
2216 sv_setiv(sv, (IV)u);
2226 =for apidoc sv_setnv
2228 Copies a double into the given SV, upgrading first if necessary.
2229 Does not handle 'set' magic. See also C<sv_setnv_mg>.
2235 Perl_sv_setnv(pTHX_ register SV *sv, NV num)
2237 SV_CHECK_THINKFIRST_COW_DROP(sv);
2238 switch (SvTYPE(sv)) {
2241 sv_upgrade(sv, SVt_NV);
2246 sv_upgrade(sv, SVt_PVNV);
2255 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
2259 (void)SvNOK_only(sv); /* validate number */
2264 =for apidoc sv_setnv_mg
2266 Like C<sv_setnv>, but also handles 'set' magic.
2272 Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
2278 /* Print an "isn't numeric" warning, using a cleaned-up,
2279 * printable version of the offending string
2283 S_not_a_number(pTHX_ SV *sv)
2290 dsv = sv_2mortal(newSVpv("", 0));
2291 pv = sv_uni_display(dsv, sv, 10, 0);
2294 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
2295 /* each *s can expand to 4 chars + "...\0",
2296 i.e. need room for 8 chars */
2299 for (s = SvPVX(sv), end = s + SvCUR(sv); s < end && d < limit; s++) {
2301 if (ch & 128 && !isPRINT_LC(ch)) {
2310 else if (ch == '\r') {
2314 else if (ch == '\f') {
2318 else if (ch == '\\') {
2322 else if (ch == '\0') {
2326 else if (isPRINT_LC(ch))
2343 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
2344 "Argument \"%s\" isn't numeric in %s", pv,
2347 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
2348 "Argument \"%s\" isn't numeric", pv);
2352 =for apidoc looks_like_number
2354 Test if the content of an SV looks like a number (or is a number).
2355 C<Inf> and C<Infinity> are treated as numbers (so will not issue a
2356 non-numeric warning), even if your atof() doesn't grok them.
2362 Perl_looks_like_number(pTHX_ SV *sv)
2364 register char *sbegin;
2371 else if (SvPOKp(sv))
2372 sbegin = SvPV(sv, len);
2374 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
2375 return grok_number(sbegin, len, NULL);
2378 /* Actually, ISO C leaves conversion of UV to IV undefined, but
2379 until proven guilty, assume that things are not that bad... */
2384 As 64 bit platforms often have an NV that doesn't preserve all bits of
2385 an IV (an assumption perl has been based on to date) it becomes necessary
2386 to remove the assumption that the NV always carries enough precision to
2387 recreate the IV whenever needed, and that the NV is the canonical form.
2388 Instead, IV/UV and NV need to be given equal rights. So as to not lose
2389 precision as a side effect of conversion (which would lead to insanity
2390 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
2391 1) to distinguish between IV/UV/NV slots that have cached a valid
2392 conversion where precision was lost and IV/UV/NV slots that have a
2393 valid conversion which has lost no precision
2394 2) to ensure that if a numeric conversion to one form is requested that
2395 would lose precision, the precise conversion (or differently
2396 imprecise conversion) is also performed and cached, to prevent
2397 requests for different numeric formats on the same SV causing
2398 lossy conversion chains. (lossless conversion chains are perfectly
2403 SvIOKp is true if the IV slot contains a valid value
2404 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
2405 SvNOKp is true if the NV slot contains a valid value
2406 SvNOK is true only if the NV value is accurate
2409 while converting from PV to NV, check to see if converting that NV to an
2410 IV(or UV) would lose accuracy over a direct conversion from PV to
2411 IV(or UV). If it would, cache both conversions, return NV, but mark
2412 SV as IOK NOKp (ie not NOK).
2414 While converting from PV to IV, check to see if converting that IV to an
2415 NV would lose accuracy over a direct conversion from PV to NV. If it
2416 would, cache both conversions, flag similarly.
2418 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
2419 correctly because if IV & NV were set NV *always* overruled.
2420 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
2421 changes - now IV and NV together means that the two are interchangeable:
2422 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
2424 The benefit of this is that operations such as pp_add know that if
2425 SvIOK is true for both left and right operands, then integer addition
2426 can be used instead of floating point (for cases where the result won't
2427 overflow). Before, floating point was always used, which could lead to
2428 loss of precision compared with integer addition.
2430 * making IV and NV equal status should make maths accurate on 64 bit
2432 * may speed up maths somewhat if pp_add and friends start to use
2433 integers when possible instead of fp. (Hopefully the overhead in
2434 looking for SvIOK and checking for overflow will not outweigh the
2435 fp to integer speedup)
2436 * will slow down integer operations (callers of SvIV) on "inaccurate"
2437 values, as the change from SvIOK to SvIOKp will cause a call into
2438 sv_2iv each time rather than a macro access direct to the IV slot
2439 * should speed up number->string conversion on integers as IV is
2440 favoured when IV and NV are equally accurate
2442 ####################################################################
2443 You had better be using SvIOK_notUV if you want an IV for arithmetic:
2444 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
2445 On the other hand, SvUOK is true iff UV.
2446 ####################################################################
2448 Your mileage will vary depending your CPU's relative fp to integer
2452 #ifndef NV_PRESERVES_UV
2453 # define IS_NUMBER_UNDERFLOW_IV 1
2454 # define IS_NUMBER_UNDERFLOW_UV 2
2455 # define IS_NUMBER_IV_AND_UV 2
2456 # define IS_NUMBER_OVERFLOW_IV 4
2457 # define IS_NUMBER_OVERFLOW_UV 5
2459 /* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
2461 /* For sv_2nv these three cases are "SvNOK and don't bother casting" */
2463 S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype)
2465 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));
2466 if (SvNVX(sv) < (NV)IV_MIN) {
2467 (void)SvIOKp_on(sv);
2469 SvIV_set(sv, IV_MIN);
2470 return IS_NUMBER_UNDERFLOW_IV;
2472 if (SvNVX(sv) > (NV)UV_MAX) {
2473 (void)SvIOKp_on(sv);
2476 SvUV_set(sv, UV_MAX);
2477 return IS_NUMBER_OVERFLOW_UV;
2479 (void)SvIOKp_on(sv);
2481 /* Can't use strtol etc to convert this string. (See truth table in
2483 if (SvNVX(sv) <= (UV)IV_MAX) {
2484 SvIV_set(sv, I_V(SvNVX(sv)));
2485 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2486 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
2488 /* Integer is imprecise. NOK, IOKp */
2490 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
2493 SvUV_set(sv, U_V(SvNVX(sv)));
2494 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2495 if (SvUVX(sv) == UV_MAX) {
2496 /* As we know that NVs don't preserve UVs, UV_MAX cannot
2497 possibly be preserved by NV. Hence, it must be overflow.
2499 return IS_NUMBER_OVERFLOW_UV;
2501 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
2503 /* Integer is imprecise. NOK, IOKp */
2505 return IS_NUMBER_OVERFLOW_IV;
2507 #endif /* !NV_PRESERVES_UV*/
2509 /* sv_2iv() is now a macro using Perl_sv_2iv_flags();
2510 * this function provided for binary compatibility only
2514 Perl_sv_2iv(pTHX_ register SV *sv)
2516 return sv_2iv_flags(sv, SV_GMAGIC);
2520 =for apidoc sv_2iv_flags
2522 Return the integer value of an SV, doing any necessary string
2523 conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2524 Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
2530 Perl_sv_2iv_flags(pTHX_ register SV *sv, I32 flags)
2534 if (SvGMAGICAL(sv)) {
2535 if (flags & SV_GMAGIC)
2540 return I_V(SvNVX(sv));
2542 if (SvPOKp(sv) && SvLEN(sv))
2545 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2546 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2552 if (SvTHINKFIRST(sv)) {
2555 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
2556 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
2557 return SvIV(tmpstr);
2558 return PTR2IV(SvRV(sv));
2561 sv_force_normal_flags(sv, 0);
2563 if (SvREADONLY(sv) && !SvOK(sv)) {
2564 if (ckWARN(WARN_UNINITIALIZED))
2571 return (IV)(SvUVX(sv));
2578 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2579 * without also getting a cached IV/UV from it at the same time
2580 * (ie PV->NV conversion should detect loss of accuracy and cache
2581 * IV or UV at same time to avoid this. NWC */
2583 if (SvTYPE(sv) == SVt_NV)
2584 sv_upgrade(sv, SVt_PVNV);
2586 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2587 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2588 certainly cast into the IV range at IV_MAX, whereas the correct
2589 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2591 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2592 SvIV_set(sv, I_V(SvNVX(sv)));
2593 if (SvNVX(sv) == (NV) SvIVX(sv)
2594 #ifndef NV_PRESERVES_UV
2595 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2596 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2597 /* Don't flag it as "accurately an integer" if the number
2598 came from a (by definition imprecise) NV operation, and
2599 we're outside the range of NV integer precision */
2602 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2603 DEBUG_c(PerlIO_printf(Perl_debug_log,
2604 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
2610 /* IV not precise. No need to convert from PV, as NV
2611 conversion would already have cached IV if it detected
2612 that PV->IV would be better than PV->NV->IV
2613 flags already correct - don't set public IOK. */
2614 DEBUG_c(PerlIO_printf(Perl_debug_log,
2615 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
2620 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2621 but the cast (NV)IV_MIN rounds to a the value less (more
2622 negative) than IV_MIN which happens to be equal to SvNVX ??
2623 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2624 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2625 (NV)UVX == NVX are both true, but the values differ. :-(
2626 Hopefully for 2s complement IV_MIN is something like
2627 0x8000000000000000 which will be exact. NWC */
2630 SvUV_set(sv, U_V(SvNVX(sv)));
2632 (SvNVX(sv) == (NV) SvUVX(sv))
2633 #ifndef NV_PRESERVES_UV
2634 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2635 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2636 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2637 /* Don't flag it as "accurately an integer" if the number
2638 came from a (by definition imprecise) NV operation, and
2639 we're outside the range of NV integer precision */
2645 DEBUG_c(PerlIO_printf(Perl_debug_log,
2646 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
2650 return (IV)SvUVX(sv);
2653 else if (SvPOKp(sv) && SvLEN(sv)) {
2655 const int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2656 /* We want to avoid a possible problem when we cache an IV which
2657 may be later translated to an NV, and the resulting NV is not
2658 the same as the direct translation of the initial string
2659 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2660 be careful to ensure that the value with the .456 is around if the
2661 NV value is requested in the future).
2663 This means that if we cache such an IV, we need to cache the
2664 NV as well. Moreover, we trade speed for space, and do not
2665 cache the NV if we are sure it's not needed.
2668 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2669 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2670 == IS_NUMBER_IN_UV) {
2671 /* It's definitely an integer, only upgrade to PVIV */
2672 if (SvTYPE(sv) < SVt_PVIV)
2673 sv_upgrade(sv, SVt_PVIV);
2675 } else if (SvTYPE(sv) < SVt_PVNV)
2676 sv_upgrade(sv, SVt_PVNV);
2678 /* If NV preserves UV then we only use the UV value if we know that
2679 we aren't going to call atof() below. If NVs don't preserve UVs
2680 then the value returned may have more precision than atof() will
2681 return, even though value isn't perfectly accurate. */
2682 if ((numtype & (IS_NUMBER_IN_UV
2683 #ifdef NV_PRESERVES_UV
2686 )) == IS_NUMBER_IN_UV) {
2687 /* This won't turn off the public IOK flag if it was set above */
2688 (void)SvIOKp_on(sv);
2690 if (!(numtype & IS_NUMBER_NEG)) {
2692 if (value <= (UV)IV_MAX) {
2693 SvIV_set(sv, (IV)value);
2695 SvUV_set(sv, value);
2699 /* 2s complement assumption */
2700 if (value <= (UV)IV_MIN) {
2701 SvIV_set(sv, -(IV)value);
2703 /* Too negative for an IV. This is a double upgrade, but
2704 I'm assuming it will be rare. */
2705 if (SvTYPE(sv) < SVt_PVNV)
2706 sv_upgrade(sv, SVt_PVNV);
2710 SvNV_set(sv, -(NV)value);
2711 SvIV_set(sv, IV_MIN);
2715 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2716 will be in the previous block to set the IV slot, and the next
2717 block to set the NV slot. So no else here. */
2719 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2720 != IS_NUMBER_IN_UV) {
2721 /* It wasn't an (integer that doesn't overflow the UV). */
2722 SvNV_set(sv, Atof(SvPVX(sv)));
2724 if (! numtype && ckWARN(WARN_NUMERIC))
2727 #if defined(USE_LONG_DOUBLE)
2728 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
2729 PTR2UV(sv), SvNVX(sv)));
2731 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
2732 PTR2UV(sv), SvNVX(sv)));
2736 #ifdef NV_PRESERVES_UV
2737 (void)SvIOKp_on(sv);
2739 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2740 SvIV_set(sv, I_V(SvNVX(sv)));
2741 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2744 /* Integer is imprecise. NOK, IOKp */
2746 /* UV will not work better than IV */
2748 if (SvNVX(sv) > (NV)UV_MAX) {
2750 /* Integer is inaccurate. NOK, IOKp, is UV */
2751 SvUV_set(sv, UV_MAX);
2754 SvUV_set(sv, U_V(SvNVX(sv)));
2755 /* 0xFFFFFFFFFFFFFFFF not an issue in here */
2756 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2760 /* Integer is imprecise. NOK, IOKp, is UV */
2766 #else /* NV_PRESERVES_UV */
2767 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2768 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2769 /* The IV slot will have been set from value returned by
2770 grok_number above. The NV slot has just been set using
2773 assert (SvIOKp(sv));
2775 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2776 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2777 /* Small enough to preserve all bits. */
2778 (void)SvIOKp_on(sv);
2780 SvIV_set(sv, I_V(SvNVX(sv)));
2781 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2783 /* Assumption: first non-preserved integer is < IV_MAX,
2784 this NV is in the preserved range, therefore: */
2785 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2787 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);
2791 0 0 already failed to read UV.
2792 0 1 already failed to read UV.
2793 1 0 you won't get here in this case. IV/UV
2794 slot set, public IOK, Atof() unneeded.
2795 1 1 already read UV.
2796 so there's no point in sv_2iuv_non_preserve() attempting
2797 to use atol, strtol, strtoul etc. */
2798 if (sv_2iuv_non_preserve (sv, numtype)
2799 >= IS_NUMBER_OVERFLOW_IV)
2803 #endif /* NV_PRESERVES_UV */
2806 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
2808 if (SvTYPE(sv) < SVt_IV)
2809 /* Typically the caller expects that sv_any is not NULL now. */
2810 sv_upgrade(sv, SVt_IV);
2813 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2814 PTR2UV(sv),SvIVX(sv)));
2815 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
2818 /* sv_2uv() is now a macro using Perl_sv_2uv_flags();
2819 * this function provided for binary compatibility only
2823 Perl_sv_2uv(pTHX_ register SV *sv)
2825 return sv_2uv_flags(sv, SV_GMAGIC);
2829 =for apidoc sv_2uv_flags
2831 Return the unsigned integer value of an SV, doing any necessary string
2832 conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2833 Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
2839 Perl_sv_2uv_flags(pTHX_ register SV *sv, I32 flags)
2843 if (SvGMAGICAL(sv)) {
2844 if (flags & SV_GMAGIC)
2849 return U_V(SvNVX(sv));
2850 if (SvPOKp(sv) && SvLEN(sv))
2853 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2854 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2860 if (SvTHINKFIRST(sv)) {
2863 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
2864 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
2865 return SvUV(tmpstr);
2866 return PTR2UV(SvRV(sv));
2869 sv_force_normal_flags(sv, 0);
2871 if (SvREADONLY(sv) && !SvOK(sv)) {
2872 if (ckWARN(WARN_UNINITIALIZED))
2882 return (UV)SvIVX(sv);
2886 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2887 * without also getting a cached IV/UV from it at the same time
2888 * (ie PV->NV conversion should detect loss of accuracy and cache
2889 * IV or UV at same time to avoid this. */
2890 /* IV-over-UV optimisation - choose to cache IV if possible */
2892 if (SvTYPE(sv) == SVt_NV)
2893 sv_upgrade(sv, SVt_PVNV);
2895 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2896 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2897 SvIV_set(sv, I_V(SvNVX(sv)));
2898 if (SvNVX(sv) == (NV) SvIVX(sv)
2899 #ifndef NV_PRESERVES_UV
2900 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2901 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2902 /* Don't flag it as "accurately an integer" if the number
2903 came from a (by definition imprecise) NV operation, and
2904 we're outside the range of NV integer precision */
2907 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2908 DEBUG_c(PerlIO_printf(Perl_debug_log,
2909 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (precise)\n",
2915 /* IV not precise. No need to convert from PV, as NV
2916 conversion would already have cached IV if it detected
2917 that PV->IV would be better than PV->NV->IV
2918 flags already correct - don't set public IOK. */
2919 DEBUG_c(PerlIO_printf(Perl_debug_log,
2920 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (imprecise)\n",
2925 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2926 but the cast (NV)IV_MIN rounds to a the value less (more
2927 negative) than IV_MIN which happens to be equal to SvNVX ??
2928 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2929 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2930 (NV)UVX == NVX are both true, but the values differ. :-(
2931 Hopefully for 2s complement IV_MIN is something like
2932 0x8000000000000000 which will be exact. NWC */
2935 SvUV_set(sv, U_V(SvNVX(sv)));
2937 (SvNVX(sv) == (NV) SvUVX(sv))
2938 #ifndef NV_PRESERVES_UV
2939 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2940 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2941 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2942 /* Don't flag it as "accurately an integer" if the number
2943 came from a (by definition imprecise) NV operation, and
2944 we're outside the range of NV integer precision */
2949 DEBUG_c(PerlIO_printf(Perl_debug_log,
2950 "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n",
2956 else if (SvPOKp(sv) && SvLEN(sv)) {
2958 const int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2960 /* We want to avoid a possible problem when we cache a UV which
2961 may be later translated to an NV, and the resulting NV is not
2962 the translation of the initial data.
2964 This means that if we cache such a UV, we need to cache the
2965 NV as well. Moreover, we trade speed for space, and do not
2966 cache the NV if not needed.
2969 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2970 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2971 == IS_NUMBER_IN_UV) {
2972 /* It's definitely an integer, only upgrade to PVIV */
2973 if (SvTYPE(sv) < SVt_PVIV)
2974 sv_upgrade(sv, SVt_PVIV);
2976 } else if (SvTYPE(sv) < SVt_PVNV)
2977 sv_upgrade(sv, SVt_PVNV);
2979 /* If NV preserves UV then we only use the UV value if we know that
2980 we aren't going to call atof() below. If NVs don't preserve UVs
2981 then the value returned may have more precision than atof() will
2982 return, even though it isn't accurate. */
2983 if ((numtype & (IS_NUMBER_IN_UV
2984 #ifdef NV_PRESERVES_UV
2987 )) == IS_NUMBER_IN_UV) {
2988 /* This won't turn off the public IOK flag if it was set above */
2989 (void)SvIOKp_on(sv);
2991 if (!(numtype & IS_NUMBER_NEG)) {
2993 if (value <= (UV)IV_MAX) {
2994 SvIV_set(sv, (IV)value);
2996 /* it didn't overflow, and it was positive. */
2997 SvUV_set(sv, value);
3001 /* 2s complement assumption */
3002 if (value <= (UV)IV_MIN) {
3003 SvIV_set(sv, -(IV)value);
3005 /* Too negative for an IV. This is a double upgrade, but
3006 I'm assuming it will be rare. */
3007 if (SvTYPE(sv) < SVt_PVNV)
3008 sv_upgrade(sv, SVt_PVNV);
3012 SvNV_set(sv, -(NV)value);
3013 SvIV_set(sv, IV_MIN);
3018 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3019 != IS_NUMBER_IN_UV) {
3020 /* It wasn't an integer, or it overflowed the UV. */
3021 SvNV_set(sv, Atof(SvPVX(sv)));
3023 if (! numtype && ckWARN(WARN_NUMERIC))
3026 #if defined(USE_LONG_DOUBLE)
3027 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n",
3028 PTR2UV(sv), SvNVX(sv)));
3030 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"NVgf")\n",
3031 PTR2UV(sv), SvNVX(sv)));
3034 #ifdef NV_PRESERVES_UV
3035 (void)SvIOKp_on(sv);
3037 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
3038 SvIV_set(sv, I_V(SvNVX(sv)));
3039 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
3042 /* Integer is imprecise. NOK, IOKp */
3044 /* UV will not work better than IV */
3046 if (SvNVX(sv) > (NV)UV_MAX) {
3048 /* Integer is inaccurate. NOK, IOKp, is UV */
3049 SvUV_set(sv, UV_MAX);
3052 SvUV_set(sv, U_V(SvNVX(sv)));
3053 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
3054 NV preservse UV so can do correct comparison. */
3055 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
3059 /* Integer is imprecise. NOK, IOKp, is UV */
3064 #else /* NV_PRESERVES_UV */
3065 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3066 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
3067 /* The UV slot will have been set from value returned by
3068 grok_number above. The NV slot has just been set using
3071 assert (SvIOKp(sv));
3073 if (((UV)1 << NV_PRESERVES_UV_BITS) >
3074 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
3075 /* Small enough to preserve all bits. */
3076 (void)SvIOKp_on(sv);
3078 SvIV_set(sv, I_V(SvNVX(sv)));
3079 if ((NV)(SvIVX(sv)) == SvNVX(sv))
3081 /* Assumption: first non-preserved integer is < IV_MAX,
3082 this NV is in the preserved range, therefore: */
3083 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
3085 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);
3088 sv_2iuv_non_preserve (sv, numtype);
3090 #endif /* NV_PRESERVES_UV */
3094 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
3095 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
3098 if (SvTYPE(sv) < SVt_IV)
3099 /* Typically the caller expects that sv_any is not NULL now. */
3100 sv_upgrade(sv, SVt_IV);
3104 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
3105 PTR2UV(sv),SvUVX(sv)));
3106 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
3112 Return the num value of an SV, doing any necessary string or integer
3113 conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
3120 Perl_sv_2nv(pTHX_ register SV *sv)
3124 if (SvGMAGICAL(sv)) {
3128 if (SvPOKp(sv) && SvLEN(sv)) {
3129 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) &&
3130 !grok_number(SvPVX(sv), SvCUR(sv), NULL))
3132 return Atof(SvPVX(sv));
3136 return (NV)SvUVX(sv);
3138 return (NV)SvIVX(sv);
3141 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
3142 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
3148 if (SvTHINKFIRST(sv)) {
3151 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
3152 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
3153 return SvNV(tmpstr);
3154 return PTR2NV(SvRV(sv));
3157 sv_force_normal_flags(sv, 0);
3159 if (SvREADONLY(sv) && !SvOK(sv)) {
3160 if (ckWARN(WARN_UNINITIALIZED))
3165 if (SvTYPE(sv) < SVt_NV) {
3166 if (SvTYPE(sv) == SVt_IV)
3167 sv_upgrade(sv, SVt_PVNV);
3169 sv_upgrade(sv, SVt_NV);
3170 #ifdef USE_LONG_DOUBLE
3172 STORE_NUMERIC_LOCAL_SET_STANDARD();
3173 PerlIO_printf(Perl_debug_log,
3174 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
3175 PTR2UV(sv), SvNVX(sv));
3176 RESTORE_NUMERIC_LOCAL();
3180 STORE_NUMERIC_LOCAL_SET_STANDARD();
3181 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
3182 PTR2UV(sv), SvNVX(sv));
3183 RESTORE_NUMERIC_LOCAL();
3187 else if (SvTYPE(sv) < SVt_PVNV)
3188 sv_upgrade(sv, SVt_PVNV);
3193 SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
3194 #ifdef NV_PRESERVES_UV
3197 /* Only set the public NV OK flag if this NV preserves the IV */
3198 /* Check it's not 0xFFFFFFFFFFFFFFFF */
3199 if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
3200 : (SvIVX(sv) == I_V(SvNVX(sv))))
3206 else if (SvPOKp(sv) && SvLEN(sv)) {
3208 const int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
3209 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !numtype)
3211 #ifdef NV_PRESERVES_UV
3212 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3213 == IS_NUMBER_IN_UV) {
3214 /* It's definitely an integer */
3215 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
3217 SvNV_set(sv, Atof(SvPVX(sv)));
3220 SvNV_set(sv, Atof(SvPVX(sv)));
3221 /* Only set the public NV OK flag if this NV preserves the value in
3222 the PV at least as well as an IV/UV would.
3223 Not sure how to do this 100% reliably. */
3224 /* if that shift count is out of range then Configure's test is
3225 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
3227 if (((UV)1 << NV_PRESERVES_UV_BITS) >
3228 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
3229 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
3230 } else if (!(numtype & IS_NUMBER_IN_UV)) {
3231 /* Can't use strtol etc to convert this string, so don't try.
3232 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
3235 /* value has been set. It may not be precise. */
3236 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
3237 /* 2s complement assumption for (UV)IV_MIN */
3238 SvNOK_on(sv); /* Integer is too negative. */
3243 if (numtype & IS_NUMBER_NEG) {
3244 SvIV_set(sv, -(IV)value);
3245 } else if (value <= (UV)IV_MAX) {
3246 SvIV_set(sv, (IV)value);
3248 SvUV_set(sv, value);
3252 if (numtype & IS_NUMBER_NOT_INT) {
3253 /* I believe that even if the original PV had decimals,
3254 they are lost beyond the limit of the FP precision.
3255 However, neither is canonical, so both only get p
3256 flags. NWC, 2000/11/25 */
3257 /* Both already have p flags, so do nothing */
3260 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
3261 if (SvIVX(sv) == I_V(nv)) {
3266 /* It had no "." so it must be integer. */
3269 /* between IV_MAX and NV(UV_MAX).
3270 Could be slightly > UV_MAX */
3272 if (numtype & IS_NUMBER_NOT_INT) {
3273 /* UV and NV both imprecise. */
3275 UV nv_as_uv = U_V(nv);
3277 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
3288 #endif /* NV_PRESERVES_UV */
3291 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
3293 if (SvTYPE(sv) < SVt_NV)
3294 /* Typically the caller expects that sv_any is not NULL now. */
3295 /* XXX Ilya implies that this is a bug in callers that assume this
3296 and ideally should be fixed. */
3297 sv_upgrade(sv, SVt_NV);
3300 #if defined(USE_LONG_DOUBLE)
3302 STORE_NUMERIC_LOCAL_SET_STANDARD();
3303 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
3304 PTR2UV(sv), SvNVX(sv));
3305 RESTORE_NUMERIC_LOCAL();
3309 STORE_NUMERIC_LOCAL_SET_STANDARD();
3310 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
3311 PTR2UV(sv), SvNVX(sv));
3312 RESTORE_NUMERIC_LOCAL();
3318 /* asIV(): extract an integer from the string value of an SV.
3319 * Caller must validate PVX */
3322 S_asIV(pTHX_ SV *sv)
3325 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
3327 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3328 == IS_NUMBER_IN_UV) {
3329 /* It's definitely an integer */
3330 if (numtype & IS_NUMBER_NEG) {
3331 if (value < (UV)IV_MIN)
3334 if (value < (UV)IV_MAX)
3339 if (ckWARN(WARN_NUMERIC))
3342 return I_V(Atof(SvPVX(sv)));
3345 /* asUV(): extract an unsigned integer from the string value of an SV
3346 * Caller must validate PVX */
3349 S_asUV(pTHX_ SV *sv)
3352 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
3354 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3355 == IS_NUMBER_IN_UV) {
3356 /* It's definitely an integer */
3357 if (!(numtype & IS_NUMBER_NEG))
3361 if (ckWARN(WARN_NUMERIC))
3364 return U_V(Atof(SvPVX(sv)));
3368 =for apidoc sv_2pv_nolen
3370 Like C<sv_2pv()>, but doesn't return the length too. You should usually
3371 use the macro wrapper C<SvPV_nolen(sv)> instead.
3376 Perl_sv_2pv_nolen(pTHX_ register SV *sv)
3379 return sv_2pv(sv, &n_a);
3382 /* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
3383 * UV as a string towards the end of buf, and return pointers to start and
3386 * We assume that buf is at least TYPE_CHARS(UV) long.
3390 uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
3392 char *ptr = buf + TYPE_CHARS(UV);
3406 *--ptr = '0' + (char)(uv % 10);
3414 /* sv_2pv() is now a macro using Perl_sv_2pv_flags();
3415 * this function provided for binary compatibility only
3419 Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
3421 return sv_2pv_flags(sv, lp, SV_GMAGIC);
3425 =for apidoc sv_2pv_flags
3427 Returns a pointer to the string value of an SV, and sets *lp to its length.
3428 If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
3430 Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
3431 usually end up here too.
3437 Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
3442 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
3443 char *tmpbuf = tbuf;
3449 if (SvGMAGICAL(sv)) {
3450 if (flags & SV_GMAGIC)
3458 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
3460 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
3465 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
3470 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
3471 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
3478 if (SvTHINKFIRST(sv)) {
3481 register const char *typestr;
3482 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
3483 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
3484 char *pv = SvPV(tmpstr, *lp);
3494 typestr = "NULLREF";
3498 switch (SvTYPE(sv)) {
3500 if ( ((SvFLAGS(sv) &
3501 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
3502 == (SVs_OBJECT|SVs_SMG))
3503 && (mg = mg_find(sv, PERL_MAGIC_qr))) {
3504 const regexp *re = (regexp *)mg->mg_obj;
3507 const char *fptr = "msix";
3512 char need_newline = 0;
3513 U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12);
3515 while((ch = *fptr++)) {
3517 reflags[left++] = ch;
3520 reflags[right--] = ch;
3525 reflags[left] = '-';
3529 mg->mg_len = re->prelen + 4 + left;
3531 * If /x was used, we have to worry about a regex
3532 * ending with a comment later being embedded
3533 * within another regex. If so, we don't want this
3534 * regex's "commentization" to leak out to the
3535 * right part of the enclosing regex, we must cap
3536 * it with a newline.
3538 * So, if /x was used, we scan backwards from the
3539 * end of the regex. If we find a '#' before we
3540 * find a newline, we need to add a newline
3541 * ourself. If we find a '\n' first (or if we
3542 * don't find '#' or '\n'), we don't need to add
3543 * anything. -jfriedl
3545 if (PMf_EXTENDED & re->reganch)
3547 const char *endptr = re->precomp + re->prelen;
3548 while (endptr >= re->precomp)
3550 const char c = *(endptr--);
3552 break; /* don't need another */
3554 /* we end while in a comment, so we
3556 mg->mg_len++; /* save space for it */
3557 need_newline = 1; /* note to add it */
3563 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
3564 Copy("(?", mg->mg_ptr, 2, char);
3565 Copy(reflags, mg->mg_ptr+2, left, char);
3566 Copy(":", mg->mg_ptr+left+2, 1, char);
3567 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
3569 mg->mg_ptr[mg->mg_len - 2] = '\n';
3570 mg->mg_ptr[mg->mg_len - 1] = ')';
3571 mg->mg_ptr[mg->mg_len] = 0;
3573 PL_reginterp_cnt += re->program[0].next_off;
3575 if (re->reganch & ROPT_UTF8)
3590 case SVt_PVBM: typestr = SvROK(sv) ? "REF" : "SCALAR"; break;
3591 case SVt_PVLV: typestr = SvROK(sv) ? "REF"
3592 /* tied lvalues should appear to be
3593 * scalars for backwards compatitbility */
3594 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
3595 ? "SCALAR" : "LVALUE"; break;
3596 case SVt_PVAV: typestr = "ARRAY"; break;
3597 case SVt_PVHV: typestr = "HASH"; break;
3598 case SVt_PVCV: typestr = "CODE"; break;
3599 case SVt_PVGV: typestr = "GLOB"; break;
3600 case SVt_PVFM: typestr = "FORMAT"; break;
3601 case SVt_PVIO: typestr = "IO"; break;
3602 default: typestr = "UNKNOWN"; break;
3606 const char *name = HvNAME(SvSTASH(sv));
3607 Perl_sv_setpvf(aTHX_ tsv, "%s=%s(0x%"UVxf")",
3608 name ? name : "__ANON__" , typestr, PTR2UV(sv));
3611 Perl_sv_setpvf(aTHX_ tsv, "%s(0x%"UVxf")", typestr, PTR2UV(sv));
3614 *lp = strlen(typestr);
3615 return (char *)typestr;
3617 if (SvREADONLY(sv) && !SvOK(sv)) {
3618 if (ckWARN(WARN_UNINITIALIZED))
3624 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
3625 /* I'm assuming that if both IV and NV are equally valid then
3626 converting the IV is going to be more efficient */
3627 const U32 isIOK = SvIOK(sv);
3628 const U32 isUIOK = SvIsUV(sv);
3629 char buf[TYPE_CHARS(UV)];
3632 if (SvTYPE(sv) < SVt_PVIV)
3633 sv_upgrade(sv, SVt_PVIV);
3635 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
3637 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
3638 SvGROW(sv, (STRLEN)(ebuf - ptr + 1)); /* inlined from sv_setpvn */
3639 Move(ptr,SvPVX(sv),ebuf - ptr,char);
3640 SvCUR_set(sv, ebuf - ptr);
3650 else if (SvNOKp(sv)) {
3651 if (SvTYPE(sv) < SVt_PVNV)
3652 sv_upgrade(sv, SVt_PVNV);
3653 /* The +20 is pure guesswork. Configure test needed. --jhi */
3654 SvGROW(sv, NV_DIG + 20);
3656 olderrno = errno; /* some Xenix systems wipe out errno here */
3658 if (SvNVX(sv) == 0.0)
3659 (void)strcpy(s,"0");
3663 Gconvert(SvNVX(sv), NV_DIG, 0, s);
3666 #ifdef FIXNEGATIVEZERO
3667 if (*s == '-' && s[1] == '0' && !s[2])
3677 if (ckWARN(WARN_UNINITIALIZED)
3678 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
3681 if (SvTYPE(sv) < SVt_PV)
3682 /* Typically the caller expects that sv_any is not NULL now. */
3683 sv_upgrade(sv, SVt_PV);
3686 *lp = s - SvPVX(sv);
3689 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3690 PTR2UV(sv),SvPVX(sv)));
3694 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
3695 /* Sneaky stuff here */
3699 tsv = newSVpv(tmpbuf, 0);
3716 len = strlen(tmpbuf);
3718 #ifdef FIXNEGATIVEZERO
3719 if (len == 2 && t[0] == '-' && t[1] == '0') {
3724 (void)SvUPGRADE(sv, SVt_PV);
3726 s = SvGROW(sv, len + 1);
3729 return strcpy(s, t);
3734 =for apidoc sv_copypv
3736 Copies a stringified representation of the source SV into the
3737 destination SV. Automatically performs any necessary mg_get and
3738 coercion of numeric values into strings. Guaranteed to preserve
3739 UTF-8 flag even from overloaded objects. Similar in nature to
3740 sv_2pv[_flags] but operates directly on an SV instead of just the
3741 string. Mostly uses sv_2pv_flags to do its work, except when that
3742 would lose the UTF-8'ness of the PV.
3748 Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv)
3753 sv_setpvn(dsv,s,len);
3761 =for apidoc sv_2pvbyte_nolen
3763 Return a pointer to the byte-encoded representation of the SV.
3764 May cause the SV to be downgraded from UTF-8 as a side-effect.
3766 Usually accessed via the C<SvPVbyte_nolen> macro.
3772 Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
3775 return sv_2pvbyte(sv, &n_a);
3779 =for apidoc sv_2pvbyte
3781 Return a pointer to the byte-encoded representation of the SV, and set *lp
3782 to its length. May cause the SV to be downgraded from UTF-8 as a
3785 Usually accessed via the C<SvPVbyte> macro.
3791 Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
3793 sv_utf8_downgrade(sv,0);
3794 return SvPV(sv,*lp);
3798 =for apidoc sv_2pvutf8_nolen
3800 Return a pointer to the UTF-8-encoded representation of the SV.
3801 May cause the SV to be upgraded to UTF-8 as a side-effect.
3803 Usually accessed via the C<SvPVutf8_nolen> macro.
3809 Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
3812 return sv_2pvutf8(sv, &n_a);
3816 =for apidoc sv_2pvutf8
3818 Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
3819 to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
3821 Usually accessed via the C<SvPVutf8> macro.
3827 Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
3829 sv_utf8_upgrade(sv);
3830 return SvPV(sv,*lp);
3834 =for apidoc sv_2bool
3836 This function is only called on magical items, and is only used by
3837 sv_true() or its macro equivalent.
3843 Perl_sv_2bool(pTHX_ register SV *sv)
3852 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
3853 (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
3854 return (bool)SvTRUE(tmpsv);
3855 return SvRV(sv) != 0;
3858 register XPV* Xpvtmp;
3859 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
3860 (*Xpvtmp->xpv_pv > '0' ||
3861 Xpvtmp->xpv_cur > 1 ||
3862 (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0')))
3869 return SvIVX(sv) != 0;
3872 return SvNVX(sv) != 0.0;
3879 /* sv_utf8_upgrade() is now a macro using sv_utf8_upgrade_flags();
3880 * this function provided for binary compatibility only
3885 Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
3887 return sv_utf8_upgrade_flags(sv, SV_GMAGIC);
3891 =for apidoc sv_utf8_upgrade
3893 Converts the PV of an SV to its UTF-8-encoded form.
3894 Forces the SV to string form if it is not already.
3895 Always sets the SvUTF8 flag to avoid future validity checks even
3896 if all the bytes have hibit clear.
3898 This is not as a general purpose byte encoding to Unicode interface:
3899 use the Encode extension for that.
3901 =for apidoc sv_utf8_upgrade_flags
3903 Converts the PV of an SV to its UTF-8-encoded form.
3904 Forces the SV to string form if it is not already.
3905 Always sets the SvUTF8 flag to avoid future validity checks even
3906 if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
3907 will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
3908 C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
3910 This is not as a general purpose byte encoding to Unicode interface:
3911 use the Encode extension for that.
3917 Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags)
3919 if (sv == &PL_sv_undef)
3923 if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
3924 (void) sv_2pv_flags(sv,&len, flags);
3928 (void) SvPV_force(sv,len);
3937 sv_force_normal_flags(sv, 0);
3940 if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING))
3941 sv_recode_to_utf8(sv, PL_encoding);
3942 else { /* Assume Latin-1/EBCDIC */
3943 /* This function could be much more efficient if we
3944 * had a FLAG in SVs to signal if there are any hibit
3945 * chars in the PV. Given that there isn't such a flag
3946 * make the loop as fast as possible. */
3947 U8 *s = (U8 *) SvPVX(sv);
3948 U8 *e = (U8 *) SvEND(sv);
3954 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
3958 STRLEN len = SvCUR(sv) + 1; /* Plus the \0 */
3959 s = bytes_to_utf8((U8*)s, &len);
3961 SvPV_free(sv); /* No longer using what was there before. */
3963 SvPV_set(sv, (char*)s);
3964 SvCUR_set(sv, len - 1);
3965 SvLEN_set(sv, len); /* No longer know the real size. */
3967 /* Mark as UTF-8 even if no hibit - saves scanning loop */
3974 =for apidoc sv_utf8_downgrade
3976 Attempts to convert the PV of an SV from characters to bytes.
3977 If the PV contains a character beyond byte, this conversion will fail;
3978 in this case, either returns false or, if C<fail_ok> is not
3981 This is not as a general purpose Unicode to byte encoding interface:
3982 use the Encode extension for that.
3988 Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
3990 if (SvPOKp(sv) && SvUTF8(sv)) {
3996 sv_force_normal_flags(sv, 0);
3998 s = (U8 *) SvPV(sv, len);
3999 if (!utf8_to_bytes(s, &len)) {
4004 Perl_croak(aTHX_ "Wide character in %s",
4007 Perl_croak(aTHX_ "Wide character");
4018 =for apidoc sv_utf8_encode
4020 Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
4021 flag off so that it looks like octets again.
4027 Perl_sv_utf8_encode(pTHX_ register SV *sv)
4029 (void) sv_utf8_upgrade(sv);
4031 sv_force_normal_flags(sv, 0);
4033 if (SvREADONLY(sv)) {
4034 Perl_croak(aTHX_ PL_no_modify);
4040 =for apidoc sv_utf8_decode
4042 If the PV of the SV is an octet sequence in UTF-8
4043 and contains a multiple-byte character, the C<SvUTF8> flag is turned on
4044 so that it looks like a character. If the PV contains only single-byte
4045 characters, the C<SvUTF8> flag stays being off.
4046 Scans PV for validity and returns false if the PV is invalid UTF-8.
4052 Perl_sv_utf8_decode(pTHX_ register SV *sv)
4058 /* The octets may have got themselves encoded - get them back as
4061 if (!sv_utf8_downgrade(sv, TRUE))
4064 /* it is actually just a matter of turning the utf8 flag on, but
4065 * we want to make sure everything inside is valid utf8 first.
4067 c = (U8 *) SvPVX(sv);
4068 if (!is_utf8_string(c, SvCUR(sv)+1))
4070 e = (U8 *) SvEND(sv);
4073 if (!UTF8_IS_INVARIANT(ch)) {
4082 /* sv_setsv() is now a macro using Perl_sv_setsv_flags();
4083 * this function provided for binary compatibility only
4087 Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
4089 sv_setsv_flags(dstr, sstr, SV_GMAGIC);
4093 =for apidoc sv_setsv
4095 Copies the contents of the source SV C<ssv> into the destination SV
4096 C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
4097 function if the source SV needs to be reused. Does not handle 'set' magic.
4098 Loosely speaking, it performs a copy-by-value, obliterating any previous
4099 content of the destination.
4101 You probably want to use one of the assortment of wrappers, such as
4102 C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
4103 C<SvSetMagicSV_nosteal>.
4105 =for apidoc sv_setsv_flags
4107 Copies the contents of the source SV C<ssv> into the destination SV
4108 C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
4109 function if the source SV needs to be reused. Does not handle 'set' magic.
4110 Loosely speaking, it performs a copy-by-value, obliterating any previous
4111 content of the destination.
4112 If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
4113 C<ssv> if appropriate, else not. If the C<flags> parameter has the
4114 C<NOSTEAL> bit set then the buffers of temps will not be stolen. <sv_setsv>
4115 and C<sv_setsv_nomg> are implemented in terms of this function.
4117 You probably want to use one of the assortment of wrappers, such as
4118 C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
4119 C<SvSetMagicSV_nosteal>.
4121 This is the primary function for copying scalars, and most other
4122 copy-ish functions and macros use this underneath.
4128 Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags)
4130 register U32 sflags;
4136 SV_CHECK_THINKFIRST_COW_DROP(dstr);
4138 sstr = &PL_sv_undef;
4139 stype = SvTYPE(sstr);
4140 dtype = SvTYPE(dstr);
4145 /* need to nuke the magic */
4147 SvRMAGICAL_off(dstr);
4150 /* There's a lot of redundancy below but we're going for speed here */
4155 if (dtype != SVt_PVGV) {
4156 (void)SvOK_off(dstr);
4164 sv_upgrade(dstr, SVt_IV);
4167 sv_upgrade(dstr, SVt_PVNV);
4171 sv_upgrade(dstr, SVt_PVIV);
4174 (void)SvIOK_only(dstr);
4175 SvIV_set(dstr, SvIVX(sstr));
4178 if (SvTAINTED(sstr))
4189 sv_upgrade(dstr, SVt_NV);
4194 sv_upgrade(dstr, SVt_PVNV);
4197 SvNV_set(dstr, SvNVX(sstr));
4198 (void)SvNOK_only(dstr);
4199 if (SvTAINTED(sstr))
4207 sv_upgrade(dstr, SVt_RV);
4208 else if (dtype == SVt_PVGV &&
4209 SvROK(sstr) && SvTYPE(SvRV(sstr)) == SVt_PVGV) {
4212 if (GvIMPORTED(dstr) != GVf_IMPORTED
4213 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4215 GvIMPORTED_on(dstr);
4224 #ifdef PERL_COPY_ON_WRITE
4225 if ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS) {
4226 if (dtype < SVt_PVIV)
4227 sv_upgrade(dstr, SVt_PVIV);
4234 sv_upgrade(dstr, SVt_PV);
4237 if (dtype < SVt_PVIV)
4238 sv_upgrade(dstr, SVt_PVIV);
4241 if (dtype < SVt_PVNV)
4242 sv_upgrade(dstr, SVt_PVNV);
4249 Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0),
4252 Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0));
4256 if (dtype <= SVt_PVGV) {
4258 if (dtype != SVt_PVGV) {
4259 char *name = GvNAME(sstr);
4260 STRLEN len = GvNAMELEN(sstr);
4261 /* don't upgrade SVt_PVLV: it can hold a glob */
4262 if (dtype != SVt_PVLV)
4263 sv_upgrade(dstr, SVt_PVGV);
4264 sv_magic(dstr, dstr, PERL_MAGIC_glob, Nullch, 0);
4265 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
4266 GvNAME(dstr) = savepvn(name, len);
4267 GvNAMELEN(dstr) = len;
4268 SvFAKE_on(dstr); /* can coerce to non-glob */
4270 /* ahem, death to those who redefine active sort subs */
4271 else if (PL_curstackinfo->si_type == PERLSI_SORT
4272 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
4273 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
4276 #ifdef GV_UNIQUE_CHECK
4277 if (GvUNIQUE((GV*)dstr)) {
4278 Perl_croak(aTHX_ PL_no_modify);
4282 (void)SvOK_off(dstr);
4283 GvINTRO_off(dstr); /* one-shot flag */
4285 GvGP(dstr) = gp_ref(GvGP(sstr));
4286 if (SvTAINTED(sstr))
4288 if (GvIMPORTED(dstr) != GVf_IMPORTED
4289 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4291 GvIMPORTED_on(dstr);
4299 if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
4301 if ((int)SvTYPE(sstr) != stype) {
4302 stype = SvTYPE(sstr);
4303 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
4307 if (stype == SVt_PVLV)
4308 (void)SvUPGRADE(dstr, SVt_PVNV);
4310 (void)SvUPGRADE(dstr, (U32)stype);
4313 sflags = SvFLAGS(sstr);
4315 if (sflags & SVf_ROK) {
4316 if (dtype >= SVt_PV) {
4317 if (dtype == SVt_PVGV) {
4318 SV *sref = SvREFCNT_inc(SvRV(sstr));
4320 int intro = GvINTRO(dstr);
4322 #ifdef GV_UNIQUE_CHECK
4323 if (GvUNIQUE((GV*)dstr)) {
4324 Perl_croak(aTHX_ PL_no_modify);
4329 GvINTRO_off(dstr); /* one-shot flag */
4330 GvLINE(dstr) = CopLINE(PL_curcop);
4331 GvEGV(dstr) = (GV*)dstr;
4334 switch (SvTYPE(sref)) {
4337 SAVEGENERICSV(GvAV(dstr));
4339 dref = (SV*)GvAV(dstr);
4340 GvAV(dstr) = (AV*)sref;
4341 if (!GvIMPORTED_AV(dstr)
4342 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4344 GvIMPORTED_AV_on(dstr);
4349 SAVEGENERICSV(GvHV(dstr));
4351 dref = (SV*)GvHV(dstr);
4352 GvHV(dstr) = (HV*)sref;
4353 if (!GvIMPORTED_HV(dstr)
4354 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4356 GvIMPORTED_HV_on(dstr);
4361 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
4362 SvREFCNT_dec(GvCV(dstr));
4363 GvCV(dstr) = Nullcv;
4364 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
4365 PL_sub_generation++;
4367 SAVEGENERICSV(GvCV(dstr));
4370 dref = (SV*)GvCV(dstr);
4371 if (GvCV(dstr) != (CV*)sref) {
4372 CV* cv = GvCV(dstr);
4374 if (!GvCVGEN((GV*)dstr) &&
4375 (CvROOT(cv) || CvXSUB(cv)))
4377 /* ahem, death to those who redefine
4378 * active sort subs */
4379 if (PL_curstackinfo->si_type == PERLSI_SORT &&
4380 PL_sortcop == CvSTART(cv))
4382 "Can't redefine active sort subroutine %s",
4383 GvENAME((GV*)dstr));
4384 /* Redefining a sub - warning is mandatory if
4385 it was a const and its value changed. */
4386 if (ckWARN(WARN_REDEFINE)
4388 && (!CvCONST((CV*)sref)
4389 || sv_cmp(cv_const_sv(cv),
4390 cv_const_sv((CV*)sref)))))
4392 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
4394 ? "Constant subroutine %s::%s redefined"
4395 : "Subroutine %s::%s redefined",
4396 HvNAME(GvSTASH((GV*)dstr)),
4397 GvENAME((GV*)dstr));
4401 cv_ckproto(cv, (GV*)dstr,
4402 SvPOK(sref) ? SvPVX(sref) : Nullch);
4404 GvCV(dstr) = (CV*)sref;
4405 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
4406 GvASSUMECV_on(dstr);
4407 PL_sub_generation++;
4409 if (!GvIMPORTED_CV(dstr)
4410 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4412 GvIMPORTED_CV_on(dstr);
4417 SAVEGENERICSV(GvIOp(dstr));
4419 dref = (SV*)GvIOp(dstr);
4420 GvIOp(dstr) = (IO*)sref;
4424 SAVEGENERICSV(GvFORM(dstr));
4426 dref = (SV*)GvFORM(dstr);
4427 GvFORM(dstr) = (CV*)sref;
4431 SAVEGENERICSV(GvSV(dstr));
4433 dref = (SV*)GvSV(dstr);
4435 if (!GvIMPORTED_SV(dstr)
4436 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4438 GvIMPORTED_SV_on(dstr);
4444 if (SvTAINTED(sstr))
4454 (void)SvOK_off(dstr);
4455 SvRV_set(dstr, SvREFCNT_inc(SvRV(sstr)));
4457 if (sflags & SVp_NOK) {
4459 /* Only set the public OK flag if the source has public OK. */
4460 if (sflags & SVf_NOK)
4461 SvFLAGS(dstr) |= SVf_NOK;
4462 SvNV_set(dstr, SvNVX(sstr));
4464 if (sflags & SVp_IOK) {
4465 (void)SvIOKp_on(dstr);
4466 if (sflags & SVf_IOK)
4467 SvFLAGS(dstr) |= SVf_IOK;
4468 if (sflags & SVf_IVisUV)
4470 SvIV_set(dstr, SvIVX(sstr));
4472 if (SvAMAGIC(sstr)) {
4476 else if (sflags & SVp_POK) {
4480 * Check to see if we can just swipe the string. If so, it's a
4481 * possible small lose on short strings, but a big win on long ones.
4482 * It might even be a win on short strings if SvPVX(dstr)
4483 * has to be allocated and SvPVX(sstr) has to be freed.
4486 /* Whichever path we take through the next code, we want this true,
4487 and doing it now facilitates the COW check. */
4488 (void)SvPOK_only(dstr);
4491 #ifdef PERL_COPY_ON_WRITE
4492 (sflags & (SVf_FAKE | SVf_READONLY)) != (SVf_FAKE | SVf_READONLY)
4496 (sflags & SVs_TEMP) && /* slated for free anyway? */
4497 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
4498 (!(flags & SV_NOSTEAL)) &&
4499 /* and we're allowed to steal temps */
4500 SvREFCNT(sstr) == 1 && /* and no other references to it? */
4501 SvLEN(sstr) && /* and really is a string */
4502 /* and won't be needed again, potentially */
4503 !(PL_op && PL_op->op_type == OP_AASSIGN))
4504 #ifdef PERL_COPY_ON_WRITE
4505 && !((sflags & CAN_COW_MASK) == CAN_COW_FLAGS
4506 && (SvFLAGS(dstr) & CAN_COW_MASK) == CAN_COW_FLAGS
4507 && SvTYPE(sstr) >= SVt_PVIV)
4510 /* Failed the swipe test, and it's not a shared hash key either.
4511 Have to copy the string. */
4512 STRLEN len = SvCUR(sstr);
4513 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
4514 Move(SvPVX(sstr),SvPVX(dstr),len,char);
4515 SvCUR_set(dstr, len);
4516 *SvEND(dstr) = '\0';
4518 /* If PERL_COPY_ON_WRITE is not defined, then isSwipe will always
4520 #ifdef PERL_COPY_ON_WRITE
4521 /* Either it's a shared hash key, or it's suitable for
4522 copy-on-write or we can swipe the string. */
4524 PerlIO_printf(Perl_debug_log, "Copy on write: sstr --> dstr\n");
4529 /* I believe I should acquire a global SV mutex if
4530 it's a COW sv (not a shared hash key) to stop
4531 it going un copy-on-write.
4532 If the source SV has gone un copy on write between up there
4533 and down here, then (assert() that) it is of the correct
4534 form to make it copy on write again */
4535 if ((sflags & (SVf_FAKE | SVf_READONLY))
4536 != (SVf_FAKE | SVf_READONLY)) {
4537 SvREADONLY_on(sstr);
4539 /* Make the source SV into a loop of 1.
4540 (about to become 2) */
4541 SV_COW_NEXT_SV_SET(sstr, sstr);
4545 /* Initial code is common. */
4546 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
4548 SvFLAGS(dstr) &= ~SVf_OOK;
4549 Safefree(SvPVX(dstr) - SvIVX(dstr));
4551 else if (SvLEN(dstr))
4552 Safefree(SvPVX(dstr));
4555 #ifdef PERL_COPY_ON_WRITE
4557 /* making another shared SV. */
4558 STRLEN cur = SvCUR(sstr);
4559 STRLEN len = SvLEN(sstr);
4560 assert (SvTYPE(dstr) >= SVt_PVIV);
4562 /* SvIsCOW_normal */
4563 /* splice us in between source and next-after-source. */
4564 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4565 SV_COW_NEXT_SV_SET(sstr, dstr);
4566 SvPV_set(dstr, SvPVX(sstr));
4568 /* SvIsCOW_shared_hash */
4569 UV hash = SvUVX(sstr);
4570 DEBUG_C(PerlIO_printf(Perl_debug_log,
4571 "Copy on write: Sharing hash\n"));
4573 sharepvn(SvPVX(sstr),
4574 (sflags & SVf_UTF8?-cur:cur), hash));
4575 SvUV_set(dstr, hash);
4577 SvLEN_set(dstr, len);
4578 SvCUR_set(dstr, cur);
4579 SvREADONLY_on(dstr);
4581 /* Relesase a global SV mutex. */
4585 { /* Passes the swipe test. */
4586 SvPV_set(dstr, SvPVX(sstr));
4587 SvLEN_set(dstr, SvLEN(sstr));
4588 SvCUR_set(dstr, SvCUR(sstr));
4591 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
4592 SvPV_set(sstr, Nullch);
4598 if (sflags & SVf_UTF8)
4601 if (sflags & SVp_NOK) {
4603 if (sflags & SVf_NOK)
4604 SvFLAGS(dstr) |= SVf_NOK;
4605 SvNV_set(dstr, SvNVX(sstr));
4607 if (sflags & SVp_IOK) {
4608 (void)SvIOKp_on(dstr);
4609 if (sflags & SVf_IOK)
4610 SvFLAGS(dstr) |= SVf_IOK;
4611 if (sflags & SVf_IVisUV)
4613 SvIV_set(dstr, SvIVX(sstr));
4616 MAGIC *smg = mg_find(sstr,PERL_MAGIC_vstring);
4617 sv_magic(dstr, NULL, PERL_MAGIC_vstring,
4618 smg->mg_ptr, smg->mg_len);
4619 SvRMAGICAL_on(dstr);
4622 else if (sflags & SVp_IOK) {
4623 if (sflags & SVf_IOK)
4624 (void)SvIOK_only(dstr);
4626 (void)SvOK_off(dstr);
4627 (void)SvIOKp_on(dstr);
4629 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
4630 if (sflags & SVf_IVisUV)
4632 SvIV_set(dstr, SvIVX(sstr));
4633 if (sflags & SVp_NOK) {
4634 if (sflags & SVf_NOK)
4635 (void)SvNOK_on(dstr);
4637 (void)SvNOKp_on(dstr);
4638 SvNV_set(dstr, SvNVX(sstr));
4641 else if (sflags & SVp_NOK) {
4642 if (sflags & SVf_NOK)
4643 (void)SvNOK_only(dstr);
4645 (void)SvOK_off(dstr);
4648 SvNV_set(dstr, SvNVX(sstr));
4651 if (dtype == SVt_PVGV) {
4652 if (ckWARN(WARN_MISC))
4653 Perl_warner(aTHX_ packWARN(WARN_MISC), "Undefined value assigned to typeglob");
4656 (void)SvOK_off(dstr);
4658 if (SvTAINTED(sstr))
4663 =for apidoc sv_setsv_mg
4665 Like C<sv_setsv>, but also handles 'set' magic.
4671 Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
4673 sv_setsv(dstr,sstr);
4677 #ifdef PERL_COPY_ON_WRITE
4679 Perl_sv_setsv_cow(pTHX_ SV *dstr, SV *sstr)
4681 STRLEN cur = SvCUR(sstr);
4682 STRLEN len = SvLEN(sstr);
4683 register char *new_pv;
4686 PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n",
4694 if (SvTHINKFIRST(dstr))
4695 sv_force_normal_flags(dstr, SV_COW_DROP_PV);
4696 else if (SvPVX(dstr))
4697 Safefree(SvPVX(dstr));
4701 (void)SvUPGRADE (dstr, SVt_PVIV);
4703 assert (SvPOK(sstr));
4704 assert (SvPOKp(sstr));
4705 assert (!SvIOK(sstr));
4706 assert (!SvIOKp(sstr));
4707 assert (!SvNOK(sstr));
4708 assert (!SvNOKp(sstr));
4710 if (SvIsCOW(sstr)) {
4712 if (SvLEN(sstr) == 0) {
4713 /* source is a COW shared hash key. */
4714 UV hash = SvUVX(sstr);
4715 DEBUG_C(PerlIO_printf(Perl_debug_log,
4716 "Fast copy on write: Sharing hash\n"));
4717 SvUV_set(dstr, hash);
4718 new_pv = sharepvn(SvPVX(sstr), (SvUTF8(sstr)?-cur:cur), hash);
4721 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4723 assert ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS);
4724 (void)SvUPGRADE (sstr, SVt_PVIV);
4725 SvREADONLY_on(sstr);
4727 DEBUG_C(PerlIO_printf(Perl_debug_log,
4728 "Fast copy on write: Converting sstr to COW\n"));
4729 SV_COW_NEXT_SV_SET(dstr, sstr);
4731 SV_COW_NEXT_SV_SET(sstr, dstr);
4732 new_pv = SvPVX(sstr);
4735 SvPV_set(dstr, new_pv);
4736 SvFLAGS(dstr) = (SVt_PVIV|SVf_POK|SVp_POK|SVf_FAKE|SVf_READONLY);
4739 SvLEN_set(dstr, len);
4740 SvCUR_set(dstr, cur);
4749 =for apidoc sv_setpvn
4751 Copies a string into an SV. The C<len> parameter indicates the number of
4752 bytes to be copied. If the C<ptr> argument is NULL the SV will become
4753 undefined. Does not handle 'set' magic. See C<sv_setpvn_mg>.
4759 Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
4761 register char *dptr;
4763 SV_CHECK_THINKFIRST_COW_DROP(sv);
4769 /* len is STRLEN which is unsigned, need to copy to signed */
4772 Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen");
4774 (void)SvUPGRADE(sv, SVt_PV);
4776 SvGROW(sv, len + 1);
4778 Move(ptr,dptr,len,char);
4781 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4786 =for apidoc sv_setpvn_mg
4788 Like C<sv_setpvn>, but also handles 'set' magic.
4794 Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
4796 sv_setpvn(sv,ptr,len);
4801 =for apidoc sv_setpv
4803 Copies a string into an SV. The string must be null-terminated. Does not
4804 handle 'set' magic. See C<sv_setpv_mg>.
4810 Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
4812 register STRLEN len;
4814 SV_CHECK_THINKFIRST_COW_DROP(sv);
4820 (void)SvUPGRADE(sv, SVt_PV);
4822 SvGROW(sv, len + 1);
4823 Move(ptr,SvPVX(sv),len+1,char);
4825 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4830 =for apidoc sv_setpv_mg
4832 Like C<sv_setpv>, but also handles 'set' magic.
4838 Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
4845 =for apidoc sv_usepvn
4847 Tells an SV to use C<ptr> to find its string value. Normally the string is
4848 stored inside the SV but sv_usepvn allows the SV to use an outside string.
4849 The C<ptr> should point to memory that was allocated by C<malloc>. The
4850 string length, C<len>, must be supplied. This function will realloc the
4851 memory pointed to by C<ptr>, so that pointer should not be freed or used by
4852 the programmer after giving it to sv_usepvn. Does not handle 'set' magic.
4853 See C<sv_usepvn_mg>.
4859 Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
4861 SV_CHECK_THINKFIRST_COW_DROP(sv);
4862 (void)SvUPGRADE(sv, SVt_PV);
4869 Renew(ptr, len+1, char);
4872 SvLEN_set(sv, len+1);
4874 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4879 =for apidoc sv_usepvn_mg
4881 Like C<sv_usepvn>, but also handles 'set' magic.
4887 Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
4889 sv_usepvn(sv,ptr,len);
4893 #ifdef PERL_COPY_ON_WRITE
4894 /* Need to do this *after* making the SV normal, as we need the buffer
4895 pointer to remain valid until after we've copied it. If we let go too early,
4896 another thread could invalidate it by unsharing last of the same hash key
4897 (which it can do by means other than releasing copy-on-write Svs)
4898 or by changing the other copy-on-write SVs in the loop. */
4900 S_sv_release_COW(pTHX_ register SV *sv, char *pvx, STRLEN cur, STRLEN len,
4901 U32 hash, SV *after)
4903 if (len) { /* this SV was SvIsCOW_normal(sv) */
4904 /* we need to find the SV pointing to us. */
4905 SV *current = SV_COW_NEXT_SV(after);
4907 if (current == sv) {
4908 /* The SV we point to points back to us (there were only two of us
4910 Hence other SV is no longer copy on write either. */
4912 SvREADONLY_off(after);
4914 /* We need to follow the pointers around the loop. */
4916 while ((next = SV_COW_NEXT_SV(current)) != sv) {
4919 /* don't loop forever if the structure is bust, and we have
4920 a pointer into a closed loop. */
4921 assert (current != after);
4922 assert (SvPVX(current) == pvx);
4924 /* Make the SV before us point to the SV after us. */
4925 SV_COW_NEXT_SV_SET(current, after);
4928 unsharepvn(pvx, SvUTF8(sv) ? -(I32)cur : cur, hash);
4933 Perl_sv_release_IVX(pTHX_ register SV *sv)
4936 sv_force_normal_flags(sv, 0);
4942 =for apidoc sv_force_normal_flags
4944 Undo various types of fakery on an SV: if the PV is a shared string, make
4945 a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
4946 an xpvmg; if we're a copy-on-write scalar, this is the on-write time when
4947 we do the copy, and is also used locally. If C<SV_COW_DROP_PV> is set
4948 then a copy-on-write scalar drops its PV buffer (if any) and becomes
4949 SvPOK_off rather than making a copy. (Used where this scalar is about to be
4950 set to some other value.) In addition, the C<flags> parameter gets passed to
4951 C<sv_unref_flags()> when unrefing. C<sv_force_normal> calls this function
4952 with flags set to 0.
4958 Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags)
4960 #ifdef PERL_COPY_ON_WRITE
4961 if (SvREADONLY(sv)) {
4962 /* At this point I believe I should acquire a global SV mutex. */
4964 char *pvx = SvPVX(sv);
4965 STRLEN len = SvLEN(sv);
4966 STRLEN cur = SvCUR(sv);
4967 U32 hash = SvUVX(sv);
4968 SV *next = SV_COW_NEXT_SV(sv); /* next COW sv in the loop. */
4970 PerlIO_printf(Perl_debug_log,
4971 "Copy on write: Force normal %ld\n",
4977 /* This SV doesn't own the buffer, so need to New() a new one: */
4978 SvPV_set(sv, (char*)0);
4980 if (flags & SV_COW_DROP_PV) {
4981 /* OK, so we don't need to copy our buffer. */
4984 SvGROW(sv, cur + 1);
4985 Move(pvx,SvPVX(sv),cur,char);
4989 sv_release_COW(sv, pvx, cur, len, hash, next);
4994 else if (IN_PERL_RUNTIME)
4995 Perl_croak(aTHX_ PL_no_modify);
4996 /* At this point I believe that I can drop the global SV mutex. */
4999 if (SvREADONLY(sv)) {
5001 char *pvx = SvPVX(sv);
5002 int is_utf8 = SvUTF8(sv);
5003 STRLEN len = SvCUR(sv);
5004 U32 hash = SvUVX(sv);
5007 SvPV_set(sv, (char*)0);
5009 SvGROW(sv, len + 1);
5010 Move(pvx,SvPVX(sv),len,char);
5012 unsharepvn(pvx, is_utf8 ? -(I32)len : len, hash);
5014 else if (IN_PERL_RUNTIME)
5015 Perl_croak(aTHX_ PL_no_modify);
5019 sv_unref_flags(sv, flags);
5020 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
5025 =for apidoc sv_force_normal
5027 Undo various types of fakery on an SV: if the PV is a shared string, make
5028 a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
5029 an xpvmg. See also C<sv_force_normal_flags>.
5035 Perl_sv_force_normal(pTHX_ register SV *sv)
5037 sv_force_normal_flags(sv, 0);
5043 Efficient removal of characters from the beginning of the string buffer.
5044 SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside
5045 the string buffer. The C<ptr> becomes the first character of the adjusted
5046 string. Uses the "OOK hack".
5047 Beware: after this function returns, C<ptr> and SvPVX(sv) may no longer
5048 refer to the same chunk of data.
5054 Perl_sv_chop(pTHX_ register SV *sv, register const char *ptr)
5056 register STRLEN delta;
5057 if (!ptr || !SvPOKp(sv))
5059 delta = ptr - SvPVX(sv);
5060 SV_CHECK_THINKFIRST(sv);
5061 if (SvTYPE(sv) < SVt_PVIV)
5062 sv_upgrade(sv,SVt_PVIV);
5065 if (!SvLEN(sv)) { /* make copy of shared string */
5066 const char *pvx = SvPVX(sv);
5067 STRLEN len = SvCUR(sv);
5068 SvGROW(sv, len + 1);
5069 Move(pvx,SvPVX(sv),len,char);
5073 /* Same SvOOK_on but SvOOK_on does a SvIOK_off
5074 and we do that anyway inside the SvNIOK_off
5076 SvFLAGS(sv) |= SVf_OOK;
5079 SvLEN_set(sv, SvLEN(sv) - delta);
5080 SvCUR_set(sv, SvCUR(sv) - delta);
5081 SvPV_set(sv, SvPVX(sv) + delta);
5082 SvIV_set(sv, SvIVX(sv) + delta);
5085 /* sv_catpvn() is now a macro using Perl_sv_catpvn_flags();
5086 * this function provided for binary compatibility only
5090 Perl_sv_catpvn(pTHX_ SV *dsv, const char* sstr, STRLEN slen)
5092 sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC);
5096 =for apidoc sv_catpvn
5098 Concatenates the string onto the end of the string which is in the SV. The
5099 C<len> indicates number of bytes to copy. If the SV has the UTF-8
5100 status set, then the bytes appended should be valid UTF-8.
5101 Handles 'get' magic, but not 'set' magic. See C<sv_catpvn_mg>.
5103 =for apidoc sv_catpvn_flags
5105 Concatenates the string onto the end of the string which is in the SV. The
5106 C<len> indicates number of bytes to copy. If the SV has the UTF-8
5107 status set, then the bytes appended should be valid UTF-8.
5108 If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<dsv> if
5109 appropriate, else not. C<sv_catpvn> and C<sv_catpvn_nomg> are implemented
5110 in terms of this function.
5116 Perl_sv_catpvn_flags(pTHX_ register SV *dsv, register const char *sstr, register STRLEN slen, I32 flags)
5119 const char *dstr = SvPV_force_flags(dsv, dlen, flags);
5121 SvGROW(dsv, dlen + slen + 1);
5124 Move(sstr, SvPVX(dsv) + dlen, slen, char);
5125 SvCUR_set(dsv, SvCUR(dsv) + slen);
5127 (void)SvPOK_only_UTF8(dsv); /* validate pointer */
5132 =for apidoc sv_catpvn_mg
5134 Like C<sv_catpvn>, but also handles 'set' magic.
5140 Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
5142 sv_catpvn(sv,ptr,len);
5146 /* sv_catsv() is now a macro using Perl_sv_catsv_flags();
5147 * this function provided for binary compatibility only
5151 Perl_sv_catsv(pTHX_ SV *dstr, register SV *sstr)
5153 sv_catsv_flags(dstr, sstr, SV_GMAGIC);
5157 =for apidoc sv_catsv
5159 Concatenates the string from SV C<ssv> onto the end of the string in
5160 SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but
5161 not 'set' magic. See C<sv_catsv_mg>.
5163 =for apidoc sv_catsv_flags
5165 Concatenates the string from SV C<ssv> onto the end of the string in
5166 SV C<dsv>. Modifies C<dsv> but not C<ssv>. If C<flags> has C<SV_GMAGIC>
5167 bit set, will C<mg_get> on the SVs if appropriate, else not. C<sv_catsv>
5168 and C<sv_catsv_nomg> are implemented in terms of this function.
5173 Perl_sv_catsv_flags(pTHX_ SV *dsv, register SV *ssv, I32 flags)
5179 if ((spv = SvPV(ssv, slen))) {
5180 /* sutf8 and dutf8 were type bool, but under USE_ITHREADS,
5181 gcc version 2.95.2 20000220 (Debian GNU/Linux) for
5182 Linux xxx 2.2.17 on sparc64 with gcc -O2, we erroneously
5183 get dutf8 = 0x20000000, (i.e. SVf_UTF8) even though
5184 dsv->sv_flags doesn't have that bit set.
5185 Andy Dougherty 12 Oct 2001
5187 I32 sutf8 = DO_UTF8(ssv);
5190 if (SvGMAGICAL(dsv) && (flags & SV_GMAGIC))
5192 dutf8 = DO_UTF8(dsv);
5194 if (dutf8 != sutf8) {
5196 /* Not modifying source SV, so taking a temporary copy. */
5197 SV* csv = sv_2mortal(newSVpvn(spv, slen));
5199 sv_utf8_upgrade(csv);
5200 spv = SvPV(csv, slen);
5203 sv_utf8_upgrade_nomg(dsv);
5205 sv_catpvn_nomg(dsv, spv, slen);
5210 =for apidoc sv_catsv_mg
5212 Like C<sv_catsv>, but also handles 'set' magic.
5218 Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv)
5225 =for apidoc sv_catpv
5227 Concatenates the string onto the end of the string which is in the SV.
5228 If the SV has the UTF-8 status set, then the bytes appended should be
5229 valid UTF-8. Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>.
5234 Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr)
5236 register STRLEN len;
5242 junk = SvPV_force(sv, tlen);
5244 SvGROW(sv, tlen + len + 1);
5247 Move(ptr,SvPVX(sv)+tlen,len+1,char);
5248 SvCUR_set(sv, SvCUR(sv) + len);
5249 (void)SvPOK_only_UTF8(sv); /* validate pointer */
5254 =for apidoc sv_catpv_mg
5256 Like C<sv_catpv>, but also handles 'set' magic.
5262 Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr)
5271 Create a new null SV, or if len > 0, create a new empty SVt_PV type SV
5272 with an initial PV allocation of len+1. Normally accessed via the C<NEWSV>
5279 Perl_newSV(pTHX_ STRLEN len)
5285 sv_upgrade(sv, SVt_PV);
5286 SvGROW(sv, len + 1);
5291 =for apidoc sv_magicext
5293 Adds magic to an SV, upgrading it if necessary. Applies the
5294 supplied vtable and returns a pointer to the magic added.
5296 Note that C<sv_magicext> will allow things that C<sv_magic> will not.
5297 In particular, you can add magic to SvREADONLY SVs, and add more than
5298 one instance of the same 'how'.
5300 If C<namlen> is greater than zero then a C<savepvn> I<copy> of C<name> is
5301 stored, if C<namlen> is zero then C<name> is stored as-is and - as another
5302 special case - if C<(name && namlen == HEf_SVKEY)> then C<name> is assumed
5303 to contain an C<SV*> and is stored as-is with its REFCNT incremented.
5305 (This is now used as a subroutine by C<sv_magic>.)
5310 Perl_sv_magicext(pTHX_ SV* sv, SV* obj, int how, const MGVTBL *vtable,
5311 const char* name, I32 namlen)
5315 if (SvTYPE(sv) < SVt_PVMG) {
5316 (void)SvUPGRADE(sv, SVt_PVMG);
5318 Newz(702,mg, 1, MAGIC);
5319 mg->mg_moremagic = SvMAGIC(sv);
5320 SvMAGIC_set(sv, mg);
5322 /* Sometimes a magic contains a reference loop, where the sv and
5323 object refer to each other. To prevent a reference loop that
5324 would prevent such objects being freed, we look for such loops
5325 and if we find one we avoid incrementing the object refcount.
5327 Note we cannot do this to avoid self-tie loops as intervening RV must
5328 have its REFCNT incremented to keep it in existence.
5331 if (!obj || obj == sv ||
5332 how == PERL_MAGIC_arylen ||
5333 how == PERL_MAGIC_qr ||
5334 (SvTYPE(obj) == SVt_PVGV &&
5335 (GvSV(obj) == sv || GvHV(obj) == (HV*)sv || GvAV(obj) == (AV*)sv ||
5336 GvCV(obj) == (CV*)sv || GvIOp(obj) == (IO*)sv ||
5337 GvFORM(obj) == (CV*)sv)))
5342 mg->mg_obj = SvREFCNT_inc(obj);
5343 mg->mg_flags |= MGf_REFCOUNTED;
5346 /* Normal self-ties simply pass a null object, and instead of
5347 using mg_obj directly, use the SvTIED_obj macro to produce a
5348 new RV as needed. For glob "self-ties", we are tieing the PVIO
5349 with an RV obj pointing to the glob containing the PVIO. In
5350 this case, to avoid a reference loop, we need to weaken the
5354 if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO &&
5355 obj && SvROK(obj) && GvIO(SvRV(obj)) == (IO*)sv)
5361 mg->mg_len = namlen;
5364 mg->mg_ptr = savepvn(name, namlen);
5365 else if (namlen == HEf_SVKEY)
5366 mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
5368 mg->mg_ptr = (char *) name;
5370 mg->mg_virtual = vtable;
5374 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
5379 =for apidoc sv_magic
5381 Adds magic to an SV. First upgrades C<sv> to type C<SVt_PVMG> if necessary,
5382 then adds a new magic item of type C<how> to the head of the magic list.
5384 See C<sv_magicext> (which C<sv_magic> now calls) for a description of the
5385 handling of the C<name> and C<namlen> arguments.
5387 You need to use C<sv_magicext> to add magic to SvREADONLY SVs and also
5388 to add more than one instance of the same 'how'.
5394 Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen)
5396 const MGVTBL *vtable = 0;
5399 #ifdef PERL_COPY_ON_WRITE
5401 sv_force_normal_flags(sv, 0);
5403 if (SvREADONLY(sv)) {
5405 && how != PERL_MAGIC_regex_global
5406 && how != PERL_MAGIC_bm
5407 && how != PERL_MAGIC_fm
5408 && how != PERL_MAGIC_sv
5409 && how != PERL_MAGIC_backref
5412 Perl_croak(aTHX_ PL_no_modify);
5415 if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) {
5416 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
5417 /* sv_magic() refuses to add a magic of the same 'how' as an
5420 if (how == PERL_MAGIC_taint)
5428 vtable = &PL_vtbl_sv;
5430 case PERL_MAGIC_overload:
5431 vtable = &PL_vtbl_amagic;
5433 case PERL_MAGIC_overload_elem:
5434 vtable = &PL_vtbl_amagicelem;
5436 case PERL_MAGIC_overload_table:
5437 vtable = &PL_vtbl_ovrld;
5440 vtable = &PL_vtbl_bm;
5442 case PERL_MAGIC_regdata:
5443 vtable = &PL_vtbl_regdata;
5445 case PERL_MAGIC_regdatum:
5446 vtable = &PL_vtbl_regdatum;
5448 case PERL_MAGIC_env:
5449 vtable = &PL_vtbl_env;
5452 vtable = &PL_vtbl_fm;
5454 case PERL_MAGIC_envelem:
5455 vtable = &PL_vtbl_envelem;
5457 case PERL_MAGIC_regex_global:
5458 vtable = &PL_vtbl_mglob;
5460 case PERL_MAGIC_isa:
5461 vtable = &PL_vtbl_isa;
5463 case PERL_MAGIC_isaelem:
5464 vtable = &PL_vtbl_isaelem;
5466 case PERL_MAGIC_nkeys:
5467 vtable = &PL_vtbl_nkeys;
5469 case PERL_MAGIC_dbfile:
5472 case PERL_MAGIC_dbline:
5473 vtable = &PL_vtbl_dbline;
5475 #ifdef USE_LOCALE_COLLATE
5476 case PERL_MAGIC_collxfrm:
5477 vtable = &PL_vtbl_collxfrm;
5479 #endif /* USE_LOCALE_COLLATE */
5480 case PERL_MAGIC_tied:
5481 vtable = &PL_vtbl_pack;
5483 case PERL_MAGIC_tiedelem:
5484 case PERL_MAGIC_tiedscalar:
5485 vtable = &PL_vtbl_packelem;
5488 vtable = &PL_vtbl_regexp;
5490 case PERL_MAGIC_sig:
5491 vtable = &PL_vtbl_sig;
5493 case PERL_MAGIC_sigelem:
5494 vtable = &PL_vtbl_sigelem;
5496 case PERL_MAGIC_taint:
5497 vtable = &PL_vtbl_taint;
5499 case PERL_MAGIC_uvar:
5500 vtable = &PL_vtbl_uvar;
5502 case PERL_MAGIC_vec:
5503 vtable = &PL_vtbl_vec;
5505 case PERL_MAGIC_vstring:
5508 case PERL_MAGIC_utf8:
5509 vtable = &PL_vtbl_utf8;
5511 case PERL_MAGIC_substr:
5512 vtable = &PL_vtbl_substr;
5514 case PERL_MAGIC_defelem:
5515 vtable = &PL_vtbl_defelem;
5517 case PERL_MAGIC_glob:
5518 vtable = &PL_vtbl_glob;
5520 case PERL_MAGIC_arylen:
5521 vtable = &PL_vtbl_arylen;
5523 case PERL_MAGIC_pos:
5524 vtable = &PL_vtbl_pos;
5526 case PERL_MAGIC_backref:
5527 vtable = &PL_vtbl_backref;
5529 case PERL_MAGIC_ext:
5530 /* Reserved for use by extensions not perl internals. */
5531 /* Useful for attaching extension internal data to perl vars. */
5532 /* Note that multiple extensions may clash if magical scalars */
5533 /* etc holding private data from one are passed to another. */
5536 Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how);
5539 /* Rest of work is done else where */
5540 mg = sv_magicext(sv,obj,how,(MGVTBL*)vtable,name,namlen);
5543 case PERL_MAGIC_taint:
5546 case PERL_MAGIC_ext:
5547 case PERL_MAGIC_dbfile:
5554 =for apidoc sv_unmagic
5556 Removes all magic of type C<type> from an SV.
5562 Perl_sv_unmagic(pTHX_ SV *sv, int type)
5566 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
5569 for (mg = *mgp; mg; mg = *mgp) {
5570 if (mg->mg_type == type) {
5571 const MGVTBL* const vtbl = mg->mg_virtual;
5572 *mgp = mg->mg_moremagic;
5573 if (vtbl && vtbl->svt_free)
5574 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
5575 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
5577 Safefree(mg->mg_ptr);
5578 else if (mg->mg_len == HEf_SVKEY)
5579 SvREFCNT_dec((SV*)mg->mg_ptr);
5580 else if (mg->mg_type == PERL_MAGIC_utf8 && mg->mg_ptr)
5581 Safefree(mg->mg_ptr);
5583 if (mg->mg_flags & MGf_REFCOUNTED)
5584 SvREFCNT_dec(mg->mg_obj);
5588 mgp = &mg->mg_moremagic;
5592 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
5599 =for apidoc sv_rvweaken
5601 Weaken a reference: set the C<SvWEAKREF> flag on this RV; give the
5602 referred-to SV C<PERL_MAGIC_backref> magic if it hasn't already; and
5603 push a back-reference to this RV onto the array of backreferences
5604 associated with that magic.
5610 Perl_sv_rvweaken(pTHX_ SV *sv)
5613 if (!SvOK(sv)) /* let undefs pass */
5616 Perl_croak(aTHX_ "Can't weaken a nonreference");
5617 else if (SvWEAKREF(sv)) {
5618 if (ckWARN(WARN_MISC))
5619 Perl_warner(aTHX_ packWARN(WARN_MISC), "Reference is already weak");
5623 sv_add_backref(tsv, sv);
5629 /* Give tsv backref magic if it hasn't already got it, then push a
5630 * back-reference to sv onto the array associated with the backref magic.
5634 S_sv_add_backref(pTHX_ SV *tsv, SV *sv)
5638 if (SvMAGICAL(tsv) && (mg = mg_find(tsv, PERL_MAGIC_backref)))
5639 av = (AV*)mg->mg_obj;
5642 sv_magic(tsv, (SV*)av, PERL_MAGIC_backref, NULL, 0);
5643 /* av now has a refcnt of 2, which avoids it getting freed
5644 * before us during global cleanup. The extra ref is removed
5645 * by magic_killbackrefs() when tsv is being freed */
5647 if (AvFILLp(av) >= AvMAX(av)) {
5649 SV **svp = AvARRAY(av);
5650 for (i = AvFILLp(av); i >= 0; i--)
5652 svp[i] = sv; /* reuse the slot */
5655 av_extend(av, AvFILLp(av)+1);
5657 AvARRAY(av)[++AvFILLp(av)] = sv; /* av_push() */
5660 /* delete a back-reference to ourselves from the backref magic associated
5661 * with the SV we point to.
5665 S_sv_del_backref(pTHX_ SV *sv)
5672 if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, PERL_MAGIC_backref)))
5673 Perl_croak(aTHX_ "panic: del_backref");
5674 av = (AV *)mg->mg_obj;
5676 for (i = AvFILLp(av); i >= 0; i--)
5677 if (svp[i] == sv) svp[i] = Nullsv;
5681 =for apidoc sv_insert
5683 Inserts a string at the specified offset/length within the SV. Similar to
5684 the Perl substr() function.
5690 Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, const char *little, STRLEN littlelen)
5694 register char *midend;
5695 register char *bigend;
5701 Perl_croak(aTHX_ "Can't modify non-existent substring");
5702 SvPV_force(bigstr, curlen);
5703 (void)SvPOK_only_UTF8(bigstr);
5704 if (offset + len > curlen) {
5705 SvGROW(bigstr, offset+len+1);
5706 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
5707 SvCUR_set(bigstr, offset+len);
5711 i = littlelen - len;
5712 if (i > 0) { /* string might grow */
5713 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
5714 mid = big + offset + len;
5715 midend = bigend = big + SvCUR(bigstr);
5718 while (midend > mid) /* shove everything down */
5719 *--bigend = *--midend;
5720 Move(little,big+offset,littlelen,char);
5721 SvCUR_set(bigstr, SvCUR(bigstr) + i);
5726 Move(little,SvPVX(bigstr)+offset,len,char);
5731 big = SvPVX(bigstr);
5734 bigend = big + SvCUR(bigstr);
5736 if (midend > bigend)
5737 Perl_croak(aTHX_ "panic: sv_insert");
5739 if (mid - big > bigend - midend) { /* faster to shorten from end */
5741 Move(little, mid, littlelen,char);
5744 i = bigend - midend;
5746 Move(midend, mid, i,char);
5750 SvCUR_set(bigstr, mid - big);
5753 else if ((i = mid - big)) { /* faster from front */
5754 midend -= littlelen;
5756 sv_chop(bigstr,midend-i);
5761 Move(little, mid, littlelen,char);
5763 else if (littlelen) {
5764 midend -= littlelen;
5765 sv_chop(bigstr,midend);
5766 Move(little,midend,littlelen,char);
5769 sv_chop(bigstr,midend);
5775 =for apidoc sv_replace
5777 Make the first argument a copy of the second, then delete the original.
5778 The target SV physically takes over ownership of the body of the source SV
5779 and inherits its flags; however, the target keeps any magic it owns,
5780 and any magic in the source is discarded.
5781 Note that this is a rather specialist SV copying operation; most of the
5782 time you'll want to use C<sv_setsv> or one of its many macro front-ends.
5788 Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv)
5790 U32 refcnt = SvREFCNT(sv);
5791 SV_CHECK_THINKFIRST_COW_DROP(sv);
5792 if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL))
5793 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "Reference miscount in sv_replace()");
5794 if (SvMAGICAL(sv)) {
5798 sv_upgrade(nsv, SVt_PVMG);
5799 SvMAGIC_set(nsv, SvMAGIC(sv));
5800 SvFLAGS(nsv) |= SvMAGICAL(sv);
5802 SvMAGIC_set(sv, NULL);
5806 assert(!SvREFCNT(sv));
5807 #ifdef DEBUG_LEAKING_SCALARS
5808 sv->sv_flags = nsv->sv_flags;
5809 sv->sv_any = nsv->sv_any;
5810 sv->sv_refcnt = nsv->sv_refcnt;
5812 StructCopy(nsv,sv,SV);
5815 #ifdef PERL_COPY_ON_WRITE
5816 if (SvIsCOW_normal(nsv)) {
5817 /* We need to follow the pointers around the loop to make the
5818 previous SV point to sv, rather than nsv. */
5821 while ((next = SV_COW_NEXT_SV(current)) != nsv) {
5824 assert(SvPVX(current) == SvPVX(nsv));
5826 /* Make the SV before us point to the SV after us. */
5828 PerlIO_printf(Perl_debug_log, "previous is\n");
5830 PerlIO_printf(Perl_debug_log,
5831 "move it from 0x%"UVxf" to 0x%"UVxf"\n",
5832 (UV) SV_COW_NEXT_SV(current), (UV) sv);
5834 SV_COW_NEXT_SV_SET(current, sv);
5837 SvREFCNT(sv) = refcnt;
5838 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
5844 =for apidoc sv_clear
5846 Clear an SV: call any destructors, free up any memory used by the body,
5847 and free the body itself. The SV's head is I<not> freed, although
5848 its type is set to all 1's so that it won't inadvertently be assumed
5849 to be live during global destruction etc.
5850 This function should only be called when REFCNT is zero. Most of the time
5851 you'll want to call C<sv_free()> (or its macro wrapper C<SvREFCNT_dec>)
5858 Perl_sv_clear(pTHX_ register SV *sv)
5863 assert(SvREFCNT(sv) == 0);
5866 if (PL_defstash) { /* Still have a symbol table? */
5873 stash = SvSTASH(sv);
5874 destructor = StashHANDLER(stash,DESTROY);
5876 SV* tmpref = newRV(sv);
5877 SvREADONLY_on(tmpref); /* DESTROY() could be naughty */
5879 PUSHSTACKi(PERLSI_DESTROY);
5884 call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR|G_VOID);
5890 if(SvREFCNT(tmpref) < 2) {
5891 /* tmpref is not kept alive! */
5893 SvRV_set(tmpref, NULL);
5896 SvREFCNT_dec(tmpref);
5898 } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
5902 if (PL_in_clean_objs)
5903 Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'",
5905 /* DESTROY gave object new lease on life */
5911 SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */
5912 SvOBJECT_off(sv); /* Curse the object. */
5913 if (SvTYPE(sv) != SVt_PVIO)
5914 --PL_sv_objcount; /* XXX Might want something more general */
5917 if (SvTYPE(sv) >= SVt_PVMG) {
5920 if (SvFLAGS(sv) & SVpad_TYPED)
5921 SvREFCNT_dec(SvSTASH(sv));
5924 switch (SvTYPE(sv)) {
5927 IoIFP(sv) != PerlIO_stdin() &&
5928 IoIFP(sv) != PerlIO_stdout() &&
5929 IoIFP(sv) != PerlIO_stderr())
5931 io_close((IO*)sv, FALSE);
5933 if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP))
5934 PerlDir_close(IoDIRP(sv));
5935 IoDIRP(sv) = (DIR*)NULL;
5936 Safefree(IoTOP_NAME(sv));
5937 Safefree(IoFMT_NAME(sv));
5938 Safefree(IoBOTTOM_NAME(sv));
5953 if (LvTYPE(sv) == 'T') { /* for tie: return HE to pool */
5954 SvREFCNT_dec(HeKEY_sv((HE*)LvTARG(sv)));
5955 HeNEXT((HE*)LvTARG(sv)) = PL_hv_fetch_ent_mh;
5956 PL_hv_fetch_ent_mh = (HE*)LvTARG(sv);
5958 else if (LvTYPE(sv) != 't') /* unless tie: unrefcnted fake SV** */
5959 SvREFCNT_dec(LvTARG(sv));
5963 Safefree(GvNAME(sv));
5964 /* cannot decrease stash refcount yet, as we might recursively delete
5965 ourselves when the refcnt drops to zero. Delay SvREFCNT_dec
5966 of stash until current sv is completely gone.
5967 -- JohnPC, 27 Mar 1998 */
5968 stash = GvSTASH(sv);
5974 /* Don't bother with SvOOK_off(sv); as we're only going to free it. */
5976 SvPV_set(sv, SvPVX(sv) - SvIVX(sv));
5977 /* Don't even bother with turning off the OOK flag. */
5986 SvREFCNT_dec(SvRV(sv));
5988 #ifdef PERL_COPY_ON_WRITE
5989 else if (SvPVX(sv)) {
5991 /* I believe I need to grab the global SV mutex here and
5992 then recheck the COW status. */
5994 PerlIO_printf(Perl_debug_log, "Copy on write: clear\n");
5997 sv_release_COW(sv, SvPVX(sv), SvCUR(sv), SvLEN(sv),
5998 SvUVX(sv), SV_COW_NEXT_SV(sv));
5999 /* And drop it here. */
6001 } else if (SvLEN(sv)) {
6002 Safefree(SvPVX(sv));
6006 else if (SvPVX(sv) && SvLEN(sv))
6007 Safefree(SvPVX(sv));
6008 else if (SvPVX(sv) && SvREADONLY(sv) && SvFAKE(sv)) {
6009 unsharepvn(SvPVX(sv),
6010 SvUTF8(sv) ? -(I32)SvCUR(sv) : SvCUR(sv),
6024 switch (SvTYPE(sv)) {
6040 del_XPVIV(SvANY(sv));
6043 del_XPVNV(SvANY(sv));
6046 del_XPVMG(SvANY(sv));
6049 del_XPVLV(SvANY(sv));
6052 del_XPVAV(SvANY(sv));
6055 del_XPVHV(SvANY(sv));
6058 del_XPVCV(SvANY(sv));
6061 del_XPVGV(SvANY(sv));
6062 /* code duplication for increased performance. */
6063 SvFLAGS(sv) &= SVf_BREAK;
6064 SvFLAGS(sv) |= SVTYPEMASK;
6065 /* decrease refcount of the stash that owns this GV, if any */
6067 SvREFCNT_dec(stash);
6068 return; /* not break, SvFLAGS reset already happened */
6070 del_XPVBM(SvANY(sv));
6073 del_XPVFM(SvANY(sv));
6076 del_XPVIO(SvANY(sv));
6079 SvFLAGS(sv) &= SVf_BREAK;
6080 SvFLAGS(sv) |= SVTYPEMASK;
6084 =for apidoc sv_newref
6086 Increment an SV's reference count. Use the C<SvREFCNT_inc()> wrapper
6093 Perl_sv_newref(pTHX_ SV *sv)
6103 Decrement an SV's reference count, and if it drops to zero, call
6104 C<sv_clear> to invoke destructors and free up any memory used by
6105 the body; finally, deallocate the SV's head itself.
6106 Normally called via a wrapper macro C<SvREFCNT_dec>.
6112 Perl_sv_free(pTHX_ SV *sv)
6117 if (SvREFCNT(sv) == 0) {
6118 if (SvFLAGS(sv) & SVf_BREAK)
6119 /* this SV's refcnt has been artificially decremented to
6120 * trigger cleanup */
6122 if (PL_in_clean_all) /* All is fair */
6124 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
6125 /* make sure SvREFCNT(sv)==0 happens very seldom */
6126 SvREFCNT(sv) = (~(U32)0)/2;
6129 if (ckWARN_d(WARN_INTERNAL))
6130 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
6131 "Attempt to free unreferenced scalar: SV 0x%"UVxf
6132 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
6135 if (--(SvREFCNT(sv)) > 0)
6137 Perl_sv_free2(aTHX_ sv);
6141 Perl_sv_free2(pTHX_ SV *sv)
6146 if (ckWARN_d(WARN_DEBUGGING))
6147 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
6148 "Attempt to free temp prematurely: SV 0x%"UVxf
6149 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
6153 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
6154 /* make sure SvREFCNT(sv)==0 happens very seldom */
6155 SvREFCNT(sv) = (~(U32)0)/2;
6166 Returns the length of the string in the SV. Handles magic and type
6167 coercion. See also C<SvCUR>, which gives raw access to the xpv_cur slot.
6173 Perl_sv_len(pTHX_ register SV *sv)
6181 len = mg_length(sv);
6183 (void)SvPV(sv, len);
6188 =for apidoc sv_len_utf8
6190 Returns the number of characters in the string in an SV, counting wide
6191 UTF-8 bytes as a single character. Handles magic and type coercion.
6197 * The length is cached in PERL_UTF8_magic, in the mg_len field. Also the
6198 * mg_ptr is used, by sv_pos_u2b(), see the comments of S_utf8_mg_pos_init().
6199 * (Note that the mg_len is not the length of the mg_ptr field.)
6204 Perl_sv_len_utf8(pTHX_ register SV *sv)
6210 return mg_length(sv);
6214 U8 *s = (U8*)SvPV(sv, len);
6215 MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : 0;
6217 if (mg && mg->mg_len != -1 && (mg->mg_len > 0 || len == 0)) {
6219 #ifdef PERL_UTF8_CACHE_ASSERT
6220 assert(ulen == Perl_utf8_length(aTHX_ s, s + len));
6224 ulen = Perl_utf8_length(aTHX_ s, s + len);
6225 if (!mg && !SvREADONLY(sv)) {
6226 sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0);
6227 mg = mg_find(sv, PERL_MAGIC_utf8);
6237 /* S_utf8_mg_pos_init() is used to initialize the mg_ptr field of
6238 * a PERL_UTF8_magic. The mg_ptr is used to store the mapping
6239 * between UTF-8 and byte offsets. There are two (substr offset and substr
6240 * length, the i offset, PERL_MAGIC_UTF8_CACHESIZE) times two (UTF-8 offset
6241 * and byte offset) cache positions.
6243 * The mg_len field is used by sv_len_utf8(), see its comments.
6244 * Note that the mg_len is not the length of the mg_ptr field.
6248 S_utf8_mg_pos_init(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i, I32 *offsetp, U8 *s, U8 *start)
6252 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
6254 *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, (MGVTBL*)&PL_vtbl_utf8, 0, 0);
6258 *cachep = (STRLEN *) (*mgp)->mg_ptr;
6260 Newz(0, *cachep, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
6261 (*mgp)->mg_ptr = (char *) *cachep;
6265 (*cachep)[i] = *offsetp;
6266 (*cachep)[i+1] = s - start;
6274 * S_utf8_mg_pos() is used to query and update mg_ptr field of
6275 * a PERL_UTF8_magic. The mg_ptr is used to store the mapping
6276 * between UTF-8 and byte offsets. See also the comments of
6277 * S_utf8_mg_pos_init().
6281 S_utf8_mg_pos(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i, I32 *offsetp, I32 uoff, U8 **sp, U8 *start, U8 *send)
6285 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
6287 *mgp = mg_find(sv, PERL_MAGIC_utf8);
6288 if (*mgp && (*mgp)->mg_ptr) {
6289 *cachep = (STRLEN *) (*mgp)->mg_ptr;
6290 ASSERT_UTF8_CACHE(*cachep);
6291 if ((*cachep)[i] == (STRLEN)uoff) /* An exact match. */
6293 else { /* We will skip to the right spot. */
6298 /* The assumption is that going backward is half
6299 * the speed of going forward (that's where the
6300 * 2 * backw in the below comes from). (The real
6301 * figure of course depends on the UTF-8 data.) */
6303 if ((*cachep)[i] > (STRLEN)uoff) {
6305 backw = (*cachep)[i] - (STRLEN)uoff;
6307 if (forw < 2 * backw)
6310 p = start + (*cachep)[i+1];
6312 /* Try this only for the substr offset (i == 0),
6313 * not for the substr length (i == 2). */
6314 else if (i == 0) { /* (*cachep)[i] < uoff */
6315 STRLEN ulen = sv_len_utf8(sv);
6317 if ((STRLEN)uoff < ulen) {
6318 forw = (STRLEN)uoff - (*cachep)[i];
6319 backw = ulen - (STRLEN)uoff;
6321 if (forw < 2 * backw)
6322 p = start + (*cachep)[i+1];
6327 /* If the string is not long enough for uoff,
6328 * we could extend it, but not at this low a level. */
6332 if (forw < 2 * backw) {
6339 while (UTF8_IS_CONTINUATION(*p))
6344 /* Update the cache. */
6345 (*cachep)[i] = (STRLEN)uoff;
6346 (*cachep)[i+1] = p - start;
6348 /* Drop the stale "length" cache */
6357 if (found) { /* Setup the return values. */
6358 *offsetp = (*cachep)[i+1];
6359 *sp = start + *offsetp;
6362 *offsetp = send - start;
6364 else if (*sp < start) {
6370 #ifdef PERL_UTF8_CACHE_ASSERT
6375 while (n-- && s < send)
6379 assert(*offsetp == s - start);
6380 assert((*cachep)[0] == (STRLEN)uoff);
6381 assert((*cachep)[1] == *offsetp);
6383 ASSERT_UTF8_CACHE(*cachep);
6392 =for apidoc sv_pos_u2b
6394 Converts the value pointed to by offsetp from a count of UTF-8 chars from
6395 the start of the string, to a count of the equivalent number of bytes; if
6396 lenp is non-zero, it does the same to lenp, but this time starting from
6397 the offset, rather than from the start of the string. Handles magic and
6404 * sv_pos_u2b() uses, like sv_pos_b2u(), the mg_ptr of the potential
6405 * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and
6406 * byte offsets. See also the comments of S_utf8_mg_pos().
6411 Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp)
6422 start = s = (U8*)SvPV(sv, len);
6424 I32 uoffset = *offsetp;
6429 if (utf8_mg_pos(sv, &mg, &cache, 0, offsetp, *offsetp, &s, start, send))
6431 if (!found && uoffset > 0) {
6432 while (s < send && uoffset--)
6436 if (utf8_mg_pos_init(sv, &mg, &cache, 0, offsetp, s, start))
6438 *offsetp = s - start;
6443 if (utf8_mg_pos(sv, &mg, &cache, 2, lenp, *lenp, &s, start, send)) {
6447 if (!found && *lenp > 0) {
6450 while (s < send && ulen--)
6454 utf8_mg_pos_init(sv, &mg, &cache, 2, lenp, s, start);
6458 ASSERT_UTF8_CACHE(cache);
6470 =for apidoc sv_pos_b2u
6472 Converts the value pointed to by offsetp from a count of bytes from the
6473 start of the string, to a count of the equivalent number of UTF-8 chars.
6474 Handles magic and type coercion.
6480 * sv_pos_b2u() uses, like sv_pos_u2b(), the mg_ptr of the potential
6481 * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and
6482 * byte offsets. See also the comments of S_utf8_mg_pos().
6487 Perl_sv_pos_b2u(pTHX_ register SV* sv, I32* offsetp)
6495 s = (U8*)SvPV(sv, len);
6496 if ((I32)len < *offsetp)
6497 Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset");
6499 U8* send = s + *offsetp;
6501 STRLEN *cache = NULL;
6505 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
6506 mg = mg_find(sv, PERL_MAGIC_utf8);
6507 if (mg && mg->mg_ptr) {
6508 cache = (STRLEN *) mg->mg_ptr;
6509 if (cache[1] == (STRLEN)*offsetp) {
6510 /* An exact match. */
6511 *offsetp = cache[0];
6515 else if (cache[1] < (STRLEN)*offsetp) {
6516 /* We already know part of the way. */
6519 /* Let the below loop do the rest. */
6521 else { /* cache[1] > *offsetp */
6522 /* We already know all of the way, now we may
6523 * be able to walk back. The same assumption
6524 * is made as in S_utf8_mg_pos(), namely that
6525 * walking backward is twice slower than
6526 * walking forward. */
6527 STRLEN forw = *offsetp;
6528 STRLEN backw = cache[1] - *offsetp;
6530 if (!(forw < 2 * backw)) {
6531 U8 *p = s + cache[1];
6538 while (UTF8_IS_CONTINUATION(*p)) {
6546 *offsetp = cache[0];
6548 /* Drop the stale "length" cache */
6556 ASSERT_UTF8_CACHE(cache);
6562 /* Call utf8n_to_uvchr() to validate the sequence
6563 * (unless a simple non-UTF character) */
6564 if (!UTF8_IS_INVARIANT(*s))
6565 utf8n_to_uvchr(s, UTF8SKIP(s), &n, 0);
6574 if (!SvREADONLY(sv)) {
6576 sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0);
6577 mg = mg_find(sv, PERL_MAGIC_utf8);
6582 Newz(0, cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
6583 mg->mg_ptr = (char *) cache;
6588 cache[1] = *offsetp;
6589 /* Drop the stale "length" cache */
6602 Returns a boolean indicating whether the strings in the two SVs are
6603 identical. Is UTF-8 and 'use bytes' aware, handles get magic, and will
6604 coerce its args to strings if necessary.
6610 Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2)
6618 SV* svrecode = Nullsv;
6625 pv1 = SvPV(sv1, cur1);
6632 pv2 = SvPV(sv2, cur2);
6634 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
6635 /* Differing utf8ness.
6636 * Do not UTF8size the comparands as a side-effect. */
6639 svrecode = newSVpvn(pv2, cur2);
6640 sv_recode_to_utf8(svrecode, PL_encoding);
6641 pv2 = SvPV(svrecode, cur2);
6644 svrecode = newSVpvn(pv1, cur1);
6645 sv_recode_to_utf8(svrecode, PL_encoding);
6646 pv1 = SvPV(svrecode, cur1);
6648 /* Now both are in UTF-8. */
6650 SvREFCNT_dec(svrecode);
6655 bool is_utf8 = TRUE;
6658 /* sv1 is the UTF-8 one,
6659 * if is equal it must be downgrade-able */
6660 char *pv = (char*)bytes_from_utf8((const U8*)pv1,
6666 /* sv2 is the UTF-8 one,
6667 * if is equal it must be downgrade-able */
6668 char *pv = (char *)bytes_from_utf8((const U8*)pv2,
6674 /* Downgrade not possible - cannot be eq */
6682 eq = (pv1 == pv2) || memEQ(pv1, pv2, cur1);
6685 SvREFCNT_dec(svrecode);
6696 Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
6697 string in C<sv1> is less than, equal to, or greater than the string in
6698 C<sv2>. Is UTF-8 and 'use bytes' aware, handles get magic, and will
6699 coerce its args to strings if necessary. See also C<sv_cmp_locale>.
6705 Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2)
6708 const char *pv1, *pv2;
6711 SV *svrecode = Nullsv;
6718 pv1 = SvPV(sv1, cur1);
6725 pv2 = SvPV(sv2, cur2);
6727 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
6728 /* Differing utf8ness.
6729 * Do not UTF8size the comparands as a side-effect. */
6732 svrecode = newSVpvn(pv2, cur2);
6733 sv_recode_to_utf8(svrecode, PL_encoding);
6734 pv2 = SvPV(svrecode, cur2);
6737 pv2 = tpv = (char*)bytes_to_utf8((const U8*)pv2, &cur2);
6742 svrecode = newSVpvn(pv1, cur1);
6743 sv_recode_to_utf8(svrecode, PL_encoding);
6744 pv1 = SvPV(svrecode, cur1);
6747 pv1 = tpv = (char*)bytes_to_utf8((const U8*)pv1, &cur1);
6753 cmp = cur2 ? -1 : 0;
6757 const I32 retval = memcmp((const void*)pv1, (const void*)pv2, cur1 < cur2 ? cur1 : cur2);
6760 cmp = retval < 0 ? -1 : 1;
6761 } else if (cur1 == cur2) {
6764 cmp = cur1 < cur2 ? -1 : 1;
6769 SvREFCNT_dec(svrecode);
6778 =for apidoc sv_cmp_locale
6780 Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
6781 'use bytes' aware, handles get magic, and will coerce its args to strings
6782 if necessary. See also C<sv_cmp_locale>. See also C<sv_cmp>.
6788 Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2)
6790 #ifdef USE_LOCALE_COLLATE
6796 if (PL_collation_standard)
6800 pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL;
6802 pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL;
6804 if (!pv1 || !len1) {
6815 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
6818 return retval < 0 ? -1 : 1;
6821 * When the result of collation is equality, that doesn't mean
6822 * that there are no differences -- some locales exclude some
6823 * characters from consideration. So to avoid false equalities,
6824 * we use the raw string as a tiebreaker.
6830 #endif /* USE_LOCALE_COLLATE */
6832 return sv_cmp(sv1, sv2);
6836 #ifdef USE_LOCALE_COLLATE
6839 =for apidoc sv_collxfrm
6841 Add Collate Transform magic to an SV if it doesn't already have it.
6843 Any scalar variable may carry PERL_MAGIC_collxfrm magic that contains the
6844 scalar data of the variable, but transformed to such a format that a normal
6845 memory comparison can be used to compare the data according to the locale
6852 Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp)
6856 mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL;
6857 if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
6862 Safefree(mg->mg_ptr);
6864 if ((xf = mem_collxfrm(s, len, &xlen))) {
6865 if (SvREADONLY(sv)) {
6868 return xf + sizeof(PL_collation_ix);
6871 sv_magic(sv, 0, PERL_MAGIC_collxfrm, 0, 0);
6872 mg = mg_find(sv, PERL_MAGIC_collxfrm);
6885 if (mg && mg->mg_ptr) {
6887 return mg->mg_ptr + sizeof(PL_collation_ix);
6895 #endif /* USE_LOCALE_COLLATE */
6900 Get a line from the filehandle and store it into the SV, optionally
6901 appending to the currently-stored string.
6907 Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append)
6911 register STDCHAR rslast;
6912 register STDCHAR *bp;
6918 if (SvTHINKFIRST(sv))
6919 sv_force_normal_flags(sv, append ? 0 : SV_COW_DROP_PV);
6920 /* XXX. If you make this PVIV, then copy on write can copy scalars read
6922 However, perlbench says it's slower, because the existing swipe code
6923 is faster than copy on write.
6924 Swings and roundabouts. */
6925 (void)SvUPGRADE(sv, SVt_PV);
6930 if (PerlIO_isutf8(fp)) {
6932 sv_utf8_upgrade_nomg(sv);
6933 sv_pos_u2b(sv,&append,0);
6935 } else if (SvUTF8(sv)) {
6936 SV *tsv = NEWSV(0,0);
6937 sv_gets(tsv, fp, 0);
6938 sv_utf8_upgrade_nomg(tsv);
6939 SvCUR_set(sv,append);
6942 goto return_string_or_null;
6947 if (PerlIO_isutf8(fp))
6950 if (IN_PERL_COMPILETIME) {
6951 /* we always read code in line mode */
6955 else if (RsSNARF(PL_rs)) {
6956 /* If it is a regular disk file use size from stat() as estimate
6957 of amount we are going to read - may result in malloc-ing
6958 more memory than we realy need if layers bellow reduce
6959 size we read (e.g. CRLF or a gzip layer)
6962 if (!PerlLIO_fstat(PerlIO_fileno(fp), &st) && S_ISREG(st.st_mode)) {
6963 const Off_t offset = PerlIO_tell(fp);
6964 if (offset != (Off_t) -1 && st.st_size + append > offset) {
6965 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1));
6971 else if (RsRECORD(PL_rs)) {
6975 /* Grab the size of the record we're getting */
6976 recsize = SvIV(SvRV(PL_rs));
6977 buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append;
6980 /* VMS wants read instead of fread, because fread doesn't respect */
6981 /* RMS record boundaries. This is not necessarily a good thing to be */
6982 /* doing, but we've got no other real choice - except avoid stdio
6983 as implementation - perhaps write a :vms layer ?
6985 bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize);
6987 bytesread = PerlIO_read(fp, buffer, recsize);
6991 SvCUR_set(sv, bytesread += append);
6992 buffer[bytesread] = '\0';
6993 goto return_string_or_null;
6995 else if (RsPARA(PL_rs)) {
7001 /* Get $/ i.e. PL_rs into same encoding as stream wants */
7002 if (PerlIO_isutf8(fp)) {
7003 rsptr = SvPVutf8(PL_rs, rslen);
7006 if (SvUTF8(PL_rs)) {
7007 if (!sv_utf8_downgrade(PL_rs, TRUE)) {
7008 Perl_croak(aTHX_ "Wide character in $/");
7011 rsptr = SvPV(PL_rs, rslen);
7015 rslast = rslen ? rsptr[rslen - 1] : '\0';
7017 if (rspara) { /* have to do this both before and after */
7018 do { /* to make sure file boundaries work right */
7021 i = PerlIO_getc(fp);
7025 PerlIO_ungetc(fp,i);
7031 /* See if we know enough about I/O mechanism to cheat it ! */
7033 /* This used to be #ifdef test - it is made run-time test for ease
7034 of abstracting out stdio interface. One call should be cheap
7035 enough here - and may even be a macro allowing compile
7039 if (PerlIO_fast_gets(fp)) {
7042 * We're going to steal some values from the stdio struct
7043 * and put EVERYTHING in the innermost loop into registers.
7045 register STDCHAR *ptr;
7049 #if defined(VMS) && defined(PERLIO_IS_STDIO)
7050 /* An ungetc()d char is handled separately from the regular
7051 * buffer, so we getc() it back out and stuff it in the buffer.
7053 i = PerlIO_getc(fp);
7054 if (i == EOF) return 0;
7055 *(--((*fp)->_ptr)) = (unsigned char) i;
7059 /* Here is some breathtakingly efficient cheating */
7061 cnt = PerlIO_get_cnt(fp); /* get count into register */
7062 /* make sure we have the room */
7063 if ((I32)(SvLEN(sv) - append) <= cnt + 1) {
7064 /* Not room for all of it
7065 if we are looking for a separator and room for some
7067 if (rslen && cnt > 80 && (I32)SvLEN(sv) > append) {
7068 /* just process what we have room for */
7069 shortbuffered = cnt - SvLEN(sv) + append + 1;
7070 cnt -= shortbuffered;
7074 /* remember that cnt can be negative */
7075 SvGROW(sv, (STRLEN)(append + (cnt <= 0 ? 2 : (cnt + 1))));
7080 bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */
7081 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
7082 DEBUG_P(PerlIO_printf(Perl_debug_log,
7083 "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
7084 DEBUG_P(PerlIO_printf(Perl_debug_log,
7085 "Screamer: entering: PerlIO * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
7086 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
7087 PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
7092 while (cnt > 0) { /* this | eat */
7094 if ((*bp++ = *ptr++) == rslast) /* really | dust */
7095 goto thats_all_folks; /* screams | sed :-) */
7099 Copy(ptr, bp, cnt, char); /* this | eat */
7100 bp += cnt; /* screams | dust */
7101 ptr += cnt; /* louder | sed :-) */
7106 if (shortbuffered) { /* oh well, must extend */
7107 cnt = shortbuffered;
7109 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
7111 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
7112 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
7116 DEBUG_P(PerlIO_printf(Perl_debug_log,
7117 "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n",
7118 PTR2UV(ptr),(long)cnt));
7119 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* deregisterize cnt and ptr */
7121 DEBUG_P(PerlIO_printf(Perl_debug_log,
7122 "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
7123 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
7124 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
7126 /* This used to call 'filbuf' in stdio form, but as that behaves like
7127 getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
7128 another abstraction. */
7129 i = PerlIO_getc(fp); /* get more characters */
7131 DEBUG_P(PerlIO_printf(Perl_debug_log,
7132 "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
7133 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
7134 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
7136 cnt = PerlIO_get_cnt(fp);
7137 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
7138 DEBUG_P(PerlIO_printf(Perl_debug_log,
7139 "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
7141 if (i == EOF) /* all done for ever? */
7142 goto thats_really_all_folks;
7144 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
7146 SvGROW(sv, bpx + cnt + 2);
7147 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
7149 *bp++ = (STDCHAR)i; /* store character from PerlIO_getc */
7151 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
7152 goto thats_all_folks;
7156 if ((rslen > 1 && (STRLEN)(bp - (STDCHAR*)SvPVX(sv)) < rslen) ||
7157 memNE((char*)bp - rslen, rsptr, rslen))
7158 goto screamer; /* go back to the fray */
7159 thats_really_all_folks:
7161 cnt += shortbuffered;
7162 DEBUG_P(PerlIO_printf(Perl_debug_log,
7163 "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
7164 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* put these back or we're in trouble */
7165 DEBUG_P(PerlIO_printf(Perl_debug_log,
7166 "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
7167 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
7168 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
7170 SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */
7171 DEBUG_P(PerlIO_printf(Perl_debug_log,
7172 "Screamer: done, len=%ld, string=|%.*s|\n",
7173 (long)SvCUR(sv),(int)SvCUR(sv),SvPVX(sv)));
7177 /*The big, slow, and stupid way. */
7178 #ifdef USE_HEAP_INSTEAD_OF_STACK /* Even slower way. */
7180 New(0, buf, 8192, STDCHAR);
7188 const register STDCHAR *bpe = buf + sizeof(buf);
7190 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = (STDCHAR)i) != rslast && bp < bpe)
7191 ; /* keep reading */
7195 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
7196 /* Accomodate broken VAXC compiler, which applies U8 cast to
7197 * both args of ?: operator, causing EOF to change into 255
7200 i = (U8)buf[cnt - 1];
7206 cnt = 0; /* we do need to re-set the sv even when cnt <= 0 */
7208 sv_catpvn(sv, (char *) buf, cnt);
7210 sv_setpvn(sv, (char *) buf, cnt);
7212 if (i != EOF && /* joy */
7214 SvCUR(sv) < rslen ||
7215 memNE(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
7219 * If we're reading from a TTY and we get a short read,
7220 * indicating that the user hit his EOF character, we need
7221 * to notice it now, because if we try to read from the TTY
7222 * again, the EOF condition will disappear.
7224 * The comparison of cnt to sizeof(buf) is an optimization
7225 * that prevents unnecessary calls to feof().
7229 if (!(cnt < sizeof(buf) && PerlIO_eof(fp)))
7233 #ifdef USE_HEAP_INSTEAD_OF_STACK
7238 if (rspara) { /* have to do this both before and after */
7239 while (i != EOF) { /* to make sure file boundaries work right */
7240 i = PerlIO_getc(fp);
7242 PerlIO_ungetc(fp,i);
7248 return_string_or_null:
7249 return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch;
7255 Auto-increment of the value in the SV, doing string to numeric conversion
7256 if necessary. Handles 'get' magic.
7262 Perl_sv_inc(pTHX_ register SV *sv)
7271 if (SvTHINKFIRST(sv)) {
7273 sv_force_normal_flags(sv, 0);
7274 if (SvREADONLY(sv)) {
7275 if (IN_PERL_RUNTIME)
7276 Perl_croak(aTHX_ PL_no_modify);
7280 if (SvAMAGIC(sv) && AMG_CALLun(sv,inc))
7282 i = PTR2IV(SvRV(sv));
7287 flags = SvFLAGS(sv);
7288 if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) {
7289 /* It's (privately or publicly) a float, but not tested as an
7290 integer, so test it to see. */
7292 flags = SvFLAGS(sv);
7294 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
7295 /* It's publicly an integer, or privately an integer-not-float */
7296 #ifdef PERL_PRESERVE_IVUV
7300 if (SvUVX(sv) == UV_MAX)
7301 sv_setnv(sv, UV_MAX_P1);
7303 (void)SvIOK_only_UV(sv);
7304 SvUV_set(sv, SvUVX(sv) + 1);
7306 if (SvIVX(sv) == IV_MAX)
7307 sv_setuv(sv, (UV)IV_MAX + 1);
7309 (void)SvIOK_only(sv);
7310 SvIV_set(sv, SvIVX(sv) + 1);
7315 if (flags & SVp_NOK) {
7316 (void)SvNOK_only(sv);
7317 SvNV_set(sv, SvNVX(sv) + 1.0);
7321 if (!(flags & SVp_POK) || !*SvPVX(sv)) {
7322 if ((flags & SVTYPEMASK) < SVt_PVIV)
7323 sv_upgrade(sv, SVt_IV);
7324 (void)SvIOK_only(sv);
7329 while (isALPHA(*d)) d++;
7330 while (isDIGIT(*d)) d++;
7332 #ifdef PERL_PRESERVE_IVUV
7333 /* Got to punt this as an integer if needs be, but we don't issue
7334 warnings. Probably ought to make the sv_iv_please() that does
7335 the conversion if possible, and silently. */
7336 int numtype = grok_number(SvPVX(sv), SvCUR(sv), NULL);
7337 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
7338 /* Need to try really hard to see if it's an integer.
7339 9.22337203685478e+18 is an integer.
7340 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
7341 so $a="9.22337203685478e+18"; $a+0; $a++
7342 needs to be the same as $a="9.22337203685478e+18"; $a++
7349 /* sv_2iv *should* have made this an NV */
7350 if (flags & SVp_NOK) {
7351 (void)SvNOK_only(sv);
7352 SvNV_set(sv, SvNVX(sv) + 1.0);
7355 /* I don't think we can get here. Maybe I should assert this
7356 And if we do get here I suspect that sv_setnv will croak. NWC
7358 #if defined(USE_LONG_DOUBLE)
7359 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",
7360 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
7362 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
7363 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
7366 #endif /* PERL_PRESERVE_IVUV */
7367 sv_setnv(sv,Atof(SvPVX(sv)) + 1.0);
7371 while (d >= SvPVX(sv)) {
7379 /* MKS: The original code here died if letters weren't consecutive.
7380 * at least it didn't have to worry about non-C locales. The
7381 * new code assumes that ('z'-'a')==('Z'-'A'), letters are
7382 * arranged in order (although not consecutively) and that only
7383 * [A-Za-z] are accepted by isALPHA in the C locale.
7385 if (*d != 'z' && *d != 'Z') {
7386 do { ++*d; } while (!isALPHA(*d));
7389 *(d--) -= 'z' - 'a';
7394 *(d--) -= 'z' - 'a' + 1;
7398 /* oh,oh, the number grew */
7399 SvGROW(sv, SvCUR(sv) + 2);
7400 SvCUR_set(sv, SvCUR(sv) + 1);
7401 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--)
7412 Auto-decrement of the value in the SV, doing string to numeric conversion
7413 if necessary. Handles 'get' magic.
7419 Perl_sv_dec(pTHX_ register SV *sv)
7427 if (SvTHINKFIRST(sv)) {
7429 sv_force_normal_flags(sv, 0);
7430 if (SvREADONLY(sv)) {
7431 if (IN_PERL_RUNTIME)
7432 Perl_croak(aTHX_ PL_no_modify);
7436 if (SvAMAGIC(sv) && AMG_CALLun(sv,dec))
7438 i = PTR2IV(SvRV(sv));
7443 /* Unlike sv_inc we don't have to worry about string-never-numbers
7444 and keeping them magic. But we mustn't warn on punting */
7445 flags = SvFLAGS(sv);
7446 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
7447 /* It's publicly an integer, or privately an integer-not-float */
7448 #ifdef PERL_PRESERVE_IVUV
7452 if (SvUVX(sv) == 0) {
7453 (void)SvIOK_only(sv);
7457 (void)SvIOK_only_UV(sv);
7458 SvUV_set(sv, SvUVX(sv) + 1);
7461 if (SvIVX(sv) == IV_MIN)
7462 sv_setnv(sv, (NV)IV_MIN - 1.0);
7464 (void)SvIOK_only(sv);
7465 SvIV_set(sv, SvIVX(sv) - 1);
7470 if (flags & SVp_NOK) {
7471 SvNV_set(sv, SvNVX(sv) - 1.0);
7472 (void)SvNOK_only(sv);
7475 if (!(flags & SVp_POK)) {
7476 if ((flags & SVTYPEMASK) < SVt_PVNV)
7477 sv_upgrade(sv, SVt_NV);
7479 (void)SvNOK_only(sv);
7482 #ifdef PERL_PRESERVE_IVUV
7484 int numtype = grok_number(SvPVX(sv), SvCUR(sv), NULL);
7485 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
7486 /* Need to try really hard to see if it's an integer.
7487 9.22337203685478e+18 is an integer.
7488 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
7489 so $a="9.22337203685478e+18"; $a+0; $a--
7490 needs to be the same as $a="9.22337203685478e+18"; $a--
7497 /* sv_2iv *should* have made this an NV */
7498 if (flags & SVp_NOK) {
7499 (void)SvNOK_only(sv);
7500 SvNV_set(sv, SvNVX(sv) - 1.0);
7503 /* I don't think we can get here. Maybe I should assert this
7504 And if we do get here I suspect that sv_setnv will croak. NWC
7506 #if defined(USE_LONG_DOUBLE)
7507 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",
7508 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
7510 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
7511 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
7515 #endif /* PERL_PRESERVE_IVUV */
7516 sv_setnv(sv,Atof(SvPVX(sv)) - 1.0); /* punt */
7520 =for apidoc sv_mortalcopy
7522 Creates a new SV which is a copy of the original SV (using C<sv_setsv>).
7523 The new SV is marked as mortal. It will be destroyed "soon", either by an
7524 explicit call to FREETMPS, or by an implicit call at places such as
7525 statement boundaries. See also C<sv_newmortal> and C<sv_2mortal>.
7530 /* Make a string that will exist for the duration of the expression
7531 * evaluation. Actually, it may have to last longer than that, but
7532 * hopefully we won't free it until it has been assigned to a
7533 * permanent location. */
7536 Perl_sv_mortalcopy(pTHX_ SV *oldstr)
7541 sv_setsv(sv,oldstr);
7543 PL_tmps_stack[++PL_tmps_ix] = sv;
7549 =for apidoc sv_newmortal
7551 Creates a new null SV which is mortal. The reference count of the SV is
7552 set to 1. It will be destroyed "soon", either by an explicit call to
7553 FREETMPS, or by an implicit call at places such as statement boundaries.
7554 See also C<sv_mortalcopy> and C<sv_2mortal>.
7560 Perl_sv_newmortal(pTHX)
7565 SvFLAGS(sv) = SVs_TEMP;
7567 PL_tmps_stack[++PL_tmps_ix] = sv;
7572 =for apidoc sv_2mortal
7574 Marks an existing SV as mortal. The SV will be destroyed "soon", either
7575 by an explicit call to FREETMPS, or by an implicit call at places such as
7576 statement boundaries. SvTEMP() is turned on which means that the SV's
7577 string buffer can be "stolen" if this SV is copied. See also C<sv_newmortal>
7578 and C<sv_mortalcopy>.
7584 Perl_sv_2mortal(pTHX_ register SV *sv)
7589 if (SvREADONLY(sv) && SvIMMORTAL(sv))
7592 PL_tmps_stack[++PL_tmps_ix] = sv;
7600 Creates a new SV and copies a string into it. The reference count for the
7601 SV is set to 1. If C<len> is zero, Perl will compute the length using
7602 strlen(). For efficiency, consider using C<newSVpvn> instead.
7608 Perl_newSVpv(pTHX_ const char *s, STRLEN len)
7615 sv_setpvn(sv,s,len);
7620 =for apidoc newSVpvn
7622 Creates a new SV and copies a string into it. The reference count for the
7623 SV is set to 1. Note that if C<len> is zero, Perl will create a zero length
7624 string. You are responsible for ensuring that the source string is at least
7625 C<len> bytes long. If the C<s> argument is NULL the new SV will be undefined.
7631 Perl_newSVpvn(pTHX_ const char *s, STRLEN len)
7636 sv_setpvn(sv,s,len);
7641 =for apidoc newSVpvn_share
7643 Creates a new SV with its SvPVX pointing to a shared string in the string
7644 table. If the string does not already exist in the table, it is created
7645 first. Turns on READONLY and FAKE. The string's hash is stored in the UV
7646 slot of the SV; if the C<hash> parameter is non-zero, that value is used;
7647 otherwise the hash is computed. The idea here is that as the string table
7648 is used for shared hash keys these strings will have SvPVX == HeKEY and
7649 hash lookup will avoid string compare.
7655 Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
7658 bool is_utf8 = FALSE;
7660 STRLEN tmplen = -len;
7662 /* See the note in hv.c:hv_fetch() --jhi */
7663 src = (char*)bytes_from_utf8((const U8*)src, &tmplen, &is_utf8);
7667 PERL_HASH(hash, src, len);
7669 sv_upgrade(sv, SVt_PVIV);
7670 SvPV_set(sv, sharepvn(src, is_utf8?-len:len, hash));
7683 #if defined(PERL_IMPLICIT_CONTEXT)
7685 /* pTHX_ magic can't cope with varargs, so this is a no-context
7686 * version of the main function, (which may itself be aliased to us).
7687 * Don't access this version directly.
7691 Perl_newSVpvf_nocontext(const char* pat, ...)
7696 va_start(args, pat);
7697 sv = vnewSVpvf(pat, &args);
7704 =for apidoc newSVpvf
7706 Creates a new SV and initializes it with the string formatted like
7713 Perl_newSVpvf(pTHX_ const char* pat, ...)
7717 va_start(args, pat);
7718 sv = vnewSVpvf(pat, &args);
7723 /* backend for newSVpvf() and newSVpvf_nocontext() */
7726 Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args)
7730 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
7737 Creates a new SV and copies a floating point value into it.
7738 The reference count for the SV is set to 1.
7744 Perl_newSVnv(pTHX_ NV n)
7756 Creates a new SV and copies an integer into it. The reference count for the
7763 Perl_newSViv(pTHX_ IV i)
7775 Creates a new SV and copies an unsigned integer into it.
7776 The reference count for the SV is set to 1.
7782 Perl_newSVuv(pTHX_ UV u)
7792 =for apidoc newRV_noinc
7794 Creates an RV wrapper for an SV. The reference count for the original
7795 SV is B<not> incremented.
7801 Perl_newRV_noinc(pTHX_ SV *tmpRef)
7806 sv_upgrade(sv, SVt_RV);
7808 SvRV_set(sv, tmpRef);
7813 /* newRV_inc is the official function name to use now.
7814 * newRV_inc is in fact #defined to newRV in sv.h
7818 Perl_newRV(pTHX_ SV *tmpRef)
7820 return newRV_noinc(SvREFCNT_inc(tmpRef));
7826 Creates a new SV which is an exact duplicate of the original SV.
7833 Perl_newSVsv(pTHX_ register SV *old)
7839 if (SvTYPE(old) == SVTYPEMASK) {
7840 if (ckWARN_d(WARN_INTERNAL))
7841 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "semi-panic: attempt to dup freed string");
7845 /* SV_GMAGIC is the default for sv_setv()
7846 SV_NOSTEAL prevents TEMP buffers being, well, stolen, and saves games
7847 with SvTEMP_off and SvTEMP_on round a call to sv_setsv. */
7848 sv_setsv_flags(sv, old, SV_GMAGIC | SV_NOSTEAL);
7853 =for apidoc sv_reset
7855 Underlying implementation for the C<reset> Perl function.
7856 Note that the perl-level function is vaguely deprecated.
7862 Perl_sv_reset(pTHX_ register const char *s, HV *stash)
7871 char todo[PERL_UCHAR_MAX+1];
7876 if (!*s) { /* reset ?? searches */
7877 for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) {
7878 pm->op_pmdynflags &= ~PMdf_USED;
7883 /* reset variables */
7885 if (!HvARRAY(stash))
7888 Zero(todo, 256, char);
7890 i = (unsigned char)*s;
7894 max = (unsigned char)*s++;
7895 for ( ; i <= max; i++) {
7898 for (i = 0; i <= (I32) HvMAX(stash); i++) {
7899 for (entry = HvARRAY(stash)[i];
7901 entry = HeNEXT(entry))
7903 if (!todo[(U8)*HeKEY(entry)])
7905 gv = (GV*)HeVAL(entry);
7907 if (SvTHINKFIRST(sv)) {
7908 if (!SvREADONLY(sv) && SvROK(sv))
7913 if (SvTYPE(sv) >= SVt_PV) {
7915 if (SvPVX(sv) != Nullch)
7922 if (GvHV(gv) && !HvNAME(GvHV(gv))) {
7925 #ifdef USE_ENVIRON_ARRAY
7927 # ifdef USE_ITHREADS
7928 && PL_curinterp == aTHX
7932 environ[0] = Nullch;
7935 #endif /* !PERL_MICRO */
7945 Using various gambits, try to get an IO from an SV: the IO slot if its a
7946 GV; or the recursive result if we're an RV; or the IO slot of the symbol
7947 named after the PV if we're a string.
7953 Perl_sv_2io(pTHX_ SV *sv)
7958 switch (SvTYPE(sv)) {
7966 Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv));
7970 Perl_croak(aTHX_ PL_no_usym, "filehandle");
7972 return sv_2io(SvRV(sv));
7973 gv = gv_fetchsv(sv, FALSE, SVt_PVIO);
7979 Perl_croak(aTHX_ "Bad filehandle: %"SVf, sv);
7988 Using various gambits, try to get a CV from an SV; in addition, try if
7989 possible to set C<*st> and C<*gvp> to the stash and GV associated with it.
7995 Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref)
8002 return *gvp = Nullgv, Nullcv;
8003 switch (SvTYPE(sv)) {
8022 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
8023 tryAMAGICunDEREF(to_cv);
8026 if (SvTYPE(sv) == SVt_PVCV) {
8035 Perl_croak(aTHX_ "Not a subroutine reference");
8040 gv = gv_fetchsv(sv, lref, SVt_PVCV);
8046 if (lref && !GvCVu(gv)) {
8049 tmpsv = NEWSV(704,0);
8050 gv_efullname3(tmpsv, gv, Nullch);
8051 /* XXX this is probably not what they think they're getting.
8052 * It has the same effect as "sub name;", i.e. just a forward
8054 newSUB(start_subparse(FALSE, 0),
8055 newSVOP(OP_CONST, 0, tmpsv),
8060 Perl_croak(aTHX_ "Unable to create sub named \"%"SVf"\"",
8070 Returns true if the SV has a true value by Perl's rules.
8071 Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may
8072 instead use an in-line version.
8078 Perl_sv_true(pTHX_ register SV *sv)
8083 const register XPV* tXpv;
8084 if ((tXpv = (XPV*)SvANY(sv)) &&
8085 (tXpv->xpv_cur > 1 ||
8086 (tXpv->xpv_cur && *tXpv->xpv_pv != '0')))
8093 return SvIVX(sv) != 0;
8096 return SvNVX(sv) != 0.0;
8098 return sv_2bool(sv);
8106 A private implementation of the C<SvIVx> macro for compilers which can't
8107 cope with complex macro expressions. Always use the macro instead.
8113 Perl_sv_iv(pTHX_ register SV *sv)
8117 return (IV)SvUVX(sv);
8126 A private implementation of the C<SvUVx> macro for compilers which can't
8127 cope with complex macro expressions. Always use the macro instead.
8133 Perl_sv_uv(pTHX_ register SV *sv)
8138 return (UV)SvIVX(sv);
8146 A private implementation of the C<SvNVx> macro for compilers which can't
8147 cope with complex macro expressions. Always use the macro instead.
8153 Perl_sv_nv(pTHX_ register SV *sv)
8160 /* sv_pv() is now a macro using SvPV_nolen();
8161 * this function provided for binary compatibility only
8165 Perl_sv_pv(pTHX_ SV *sv)
8172 return sv_2pv(sv, &n_a);
8178 Use the C<SvPV_nolen> macro instead
8182 A private implementation of the C<SvPV> macro for compilers which can't
8183 cope with complex macro expressions. Always use the macro instead.
8189 Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp)
8195 return sv_2pv(sv, lp);
8200 Perl_sv_pvn_nomg(pTHX_ register SV *sv, STRLEN *lp)
8206 return sv_2pv_flags(sv, lp, 0);
8209 /* sv_pvn_force() is now a macro using Perl_sv_pvn_force_flags();
8210 * this function provided for binary compatibility only
8214 Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp)
8216 return sv_pvn_force_flags(sv, lp, SV_GMAGIC);
8220 =for apidoc sv_pvn_force
8222 Get a sensible string out of the SV somehow.
8223 A private implementation of the C<SvPV_force> macro for compilers which
8224 can't cope with complex macro expressions. Always use the macro instead.
8226 =for apidoc sv_pvn_force_flags
8228 Get a sensible string out of the SV somehow.
8229 If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<sv> if
8230 appropriate, else not. C<sv_pvn_force> and C<sv_pvn_force_nomg> are
8231 implemented in terms of this function.
8232 You normally want to use the various wrapper macros instead: see
8233 C<SvPV_force> and C<SvPV_force_nomg>
8239 Perl_sv_pvn_force_flags(pTHX_ SV *sv, STRLEN *lp, I32 flags)
8243 if (SvTHINKFIRST(sv) && !SvROK(sv))
8244 sv_force_normal_flags(sv, 0);
8250 if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) {
8251 Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
8255 s = sv_2pv_flags(sv, lp, flags);
8256 if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */
8261 (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */
8262 SvGROW(sv, len + 1);
8263 Move(s,SvPVX(sv),len,char);
8268 SvPOK_on(sv); /* validate pointer */
8270 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
8271 PTR2UV(sv),SvPVX(sv)));
8277 /* sv_pvbyte () is now a macro using Perl_sv_2pv_flags();
8278 * this function provided for binary compatibility only
8282 Perl_sv_pvbyte(pTHX_ SV *sv)
8284 sv_utf8_downgrade(sv,0);
8289 =for apidoc sv_pvbyte
8291 Use C<SvPVbyte_nolen> instead.
8293 =for apidoc sv_pvbyten
8295 A private implementation of the C<SvPVbyte> macro for compilers
8296 which can't cope with complex macro expressions. Always use the macro
8303 Perl_sv_pvbyten(pTHX_ SV *sv, STRLEN *lp)
8305 sv_utf8_downgrade(sv,0);
8306 return sv_pvn(sv,lp);
8310 =for apidoc sv_pvbyten_force
8312 A private implementation of the C<SvPVbytex_force> macro for compilers
8313 which can't cope with complex macro expressions. Always use the macro
8320 Perl_sv_pvbyten_force(pTHX_ SV *sv, STRLEN *lp)
8322 sv_pvn_force(sv,lp);
8323 sv_utf8_downgrade(sv,0);
8328 /* sv_pvutf8 () is now a macro using Perl_sv_2pv_flags();
8329 * this function provided for binary compatibility only
8333 Perl_sv_pvutf8(pTHX_ SV *sv)
8335 sv_utf8_upgrade(sv);
8340 =for apidoc sv_pvutf8
8342 Use the C<SvPVutf8_nolen> macro instead
8344 =for apidoc sv_pvutf8n
8346 A private implementation of the C<SvPVutf8> macro for compilers
8347 which can't cope with complex macro expressions. Always use the macro
8354 Perl_sv_pvutf8n(pTHX_ SV *sv, STRLEN *lp)
8356 sv_utf8_upgrade(sv);
8357 return sv_pvn(sv,lp);
8361 =for apidoc sv_pvutf8n_force
8363 A private implementation of the C<SvPVutf8_force> macro for compilers
8364 which can't cope with complex macro expressions. Always use the macro
8371 Perl_sv_pvutf8n_force(pTHX_ SV *sv, STRLEN *lp)
8373 sv_pvn_force(sv,lp);
8374 sv_utf8_upgrade(sv);
8380 =for apidoc sv_reftype
8382 Returns a string describing what the SV is a reference to.
8388 Perl_sv_reftype(pTHX_ const SV *sv, int ob)
8390 /* The fact that I don't need to downcast to char * everywhere, only in ?:
8391 inside return suggests a const propagation bug in g++. */
8392 if (ob && SvOBJECT(sv)) {
8393 char *name = HvNAME(SvSTASH(sv));
8394 return name ? name : (char *) "__ANON__";
8397 switch (SvTYPE(sv)) {
8414 case SVt_PVLV: return (char *) (SvROK(sv) ? "REF"
8415 /* tied lvalues should appear to be
8416 * scalars for backwards compatitbility */
8417 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
8418 ? "SCALAR" : "LVALUE");
8419 case SVt_PVAV: return "ARRAY";
8420 case SVt_PVHV: return "HASH";
8421 case SVt_PVCV: return "CODE";
8422 case SVt_PVGV: return "GLOB";
8423 case SVt_PVFM: return "FORMAT";
8424 case SVt_PVIO: return "IO";
8425 default: return "UNKNOWN";
8431 =for apidoc sv_isobject
8433 Returns a boolean indicating whether the SV is an RV pointing to a blessed
8434 object. If the SV is not an RV, or if the object is not blessed, then this
8441 Perl_sv_isobject(pTHX_ SV *sv)
8458 Returns a boolean indicating whether the SV is blessed into the specified
8459 class. This does not check for subtypes; use C<sv_derived_from> to verify
8460 an inheritance relationship.
8466 Perl_sv_isa(pTHX_ SV *sv, const char *name)
8477 if (!HvNAME(SvSTASH(sv)))
8480 return strEQ(HvNAME(SvSTASH(sv)), name);
8486 Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then
8487 it will be upgraded to one. If C<classname> is non-null then the new SV will
8488 be blessed in the specified package. The new SV is returned and its
8489 reference count is 1.
8495 Perl_newSVrv(pTHX_ SV *rv, const char *classname)
8501 SV_CHECK_THINKFIRST_COW_DROP(rv);
8504 if (SvTYPE(rv) >= SVt_PVMG) {
8505 U32 refcnt = SvREFCNT(rv);
8509 SvREFCNT(rv) = refcnt;
8512 if (SvTYPE(rv) < SVt_RV)
8513 sv_upgrade(rv, SVt_RV);
8514 else if (SvTYPE(rv) > SVt_RV) {
8525 HV* stash = gv_stashpv(classname, TRUE);
8526 (void)sv_bless(rv, stash);
8532 =for apidoc sv_setref_pv
8534 Copies a pointer into a new SV, optionally blessing the SV. The C<rv>
8535 argument will be upgraded to an RV. That RV will be modified to point to
8536 the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed
8537 into the SV. The C<classname> argument indicates the package for the
8538 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
8539 will have a reference count of 1, and the RV will be returned.
8541 Do not use with other Perl types such as HV, AV, SV, CV, because those
8542 objects will become corrupted by the pointer copy process.
8544 Note that C<sv_setref_pvn> copies the string while this copies the pointer.
8550 Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv)
8553 sv_setsv(rv, &PL_sv_undef);
8557 sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
8562 =for apidoc sv_setref_iv
8564 Copies an integer into a new SV, optionally blessing the SV. The C<rv>
8565 argument will be upgraded to an RV. That RV will be modified to point to
8566 the new SV. The C<classname> argument indicates the package for the
8567 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
8568 will have a reference count of 1, and the RV will be returned.
8574 Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv)
8576 sv_setiv(newSVrv(rv,classname), iv);
8581 =for apidoc sv_setref_uv
8583 Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv>
8584 argument will be upgraded to an RV. That RV will be modified to point to
8585 the new SV. The C<classname> argument indicates the package for the
8586 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
8587 will have a reference count of 1, and the RV will be returned.
8593 Perl_sv_setref_uv(pTHX_ SV *rv, const char *classname, UV uv)
8595 sv_setuv(newSVrv(rv,classname), uv);
8600 =for apidoc sv_setref_nv
8602 Copies a double into a new SV, optionally blessing the SV. The C<rv>
8603 argument will be upgraded to an RV. That RV will be modified to point to
8604 the new SV. The C<classname> argument indicates the package for the
8605 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
8606 will have a reference count of 1, and the RV will be returned.
8612 Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv)
8614 sv_setnv(newSVrv(rv,classname), nv);
8619 =for apidoc sv_setref_pvn
8621 Copies a string into a new SV, optionally blessing the SV. The length of the
8622 string must be specified with C<n>. The C<rv> argument will be upgraded to
8623 an RV. That RV will be modified to point to the new SV. The C<classname>
8624 argument indicates the package for the blessing. Set C<classname> to
8625 C<Nullch> to avoid the blessing. The new SV will have a reference count
8626 of 1, and the RV will be returned.
8628 Note that C<sv_setref_pv> copies the pointer while this copies the string.
8634 Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, char *pv, STRLEN n)
8636 sv_setpvn(newSVrv(rv,classname), pv, n);
8641 =for apidoc sv_bless
8643 Blesses an SV into a specified package. The SV must be an RV. The package
8644 must be designated by its stash (see C<gv_stashpv()>). The reference count
8645 of the SV is unaffected.
8651 Perl_sv_bless(pTHX_ SV *sv, HV *stash)
8655 Perl_croak(aTHX_ "Can't bless non-reference value");
8657 if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) {
8658 if (SvREADONLY(tmpRef))
8659 Perl_croak(aTHX_ PL_no_modify);
8660 if (SvOBJECT(tmpRef)) {
8661 if (SvTYPE(tmpRef) != SVt_PVIO)
8663 SvREFCNT_dec(SvSTASH(tmpRef));
8666 SvOBJECT_on(tmpRef);
8667 if (SvTYPE(tmpRef) != SVt_PVIO)
8669 (void)SvUPGRADE(tmpRef, SVt_PVMG);
8670 SvSTASH_set(tmpRef, (HV*)SvREFCNT_inc(stash));
8677 if(SvSMAGICAL(tmpRef))
8678 if(mg_find(tmpRef, PERL_MAGIC_ext) || mg_find(tmpRef, PERL_MAGIC_uvar))
8686 /* Downgrades a PVGV to a PVMG.
8690 S_sv_unglob(pTHX_ SV *sv)
8694 assert(SvTYPE(sv) == SVt_PVGV);
8699 SvREFCNT_dec(GvSTASH(sv));
8700 GvSTASH(sv) = Nullhv;
8702 sv_unmagic(sv, PERL_MAGIC_glob);
8703 Safefree(GvNAME(sv));
8706 /* need to keep SvANY(sv) in the right arena */
8707 xpvmg = new_XPVMG();
8708 StructCopy(SvANY(sv), xpvmg, XPVMG);
8709 del_XPVGV(SvANY(sv));
8712 SvFLAGS(sv) &= ~SVTYPEMASK;
8713 SvFLAGS(sv) |= SVt_PVMG;
8717 =for apidoc sv_unref_flags
8719 Unsets the RV status of the SV, and decrements the reference count of
8720 whatever was being referenced by the RV. This can almost be thought of
8721 as a reversal of C<newSVrv>. The C<cflags> argument can contain
8722 C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented
8723 (otherwise the decrementing is conditional on the reference count being
8724 different from one or the reference being a readonly SV).
8731 Perl_sv_unref_flags(pTHX_ SV *sv, U32 flags)
8735 if (SvWEAKREF(sv)) {
8743 /* You can't have a || SvREADONLY(rv) here, as $a = $$a, where $a was
8744 assigned to as BEGIN {$a = \"Foo"} will fail. */
8745 if (SvREFCNT(rv) != 1 || (flags & SV_IMMEDIATE_UNREF))
8747 else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */
8748 sv_2mortal(rv); /* Schedule for freeing later */
8752 =for apidoc sv_unref
8754 Unsets the RV status of the SV, and decrements the reference count of
8755 whatever was being referenced by the RV. This can almost be thought of
8756 as a reversal of C<newSVrv>. This is C<sv_unref_flags> with the C<flag>
8757 being zero. See C<SvROK_off>.
8763 Perl_sv_unref(pTHX_ SV *sv)
8765 sv_unref_flags(sv, 0);
8769 =for apidoc sv_taint
8771 Taint an SV. Use C<SvTAINTED_on> instead.
8776 Perl_sv_taint(pTHX_ SV *sv)
8778 sv_magic((sv), Nullsv, PERL_MAGIC_taint, Nullch, 0);
8782 =for apidoc sv_untaint
8784 Untaint an SV. Use C<SvTAINTED_off> instead.
8789 Perl_sv_untaint(pTHX_ SV *sv)
8791 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
8792 MAGIC *mg = mg_find(sv, PERL_MAGIC_taint);
8799 =for apidoc sv_tainted
8801 Test an SV for taintedness. Use C<SvTAINTED> instead.
8806 Perl_sv_tainted(pTHX_ SV *sv)
8808 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
8809 MAGIC *mg = mg_find(sv, PERL_MAGIC_taint);
8810 if (mg && ((mg->mg_len & 1) || ((mg->mg_len & 2) && mg->mg_obj == sv)))
8817 =for apidoc sv_setpviv
8819 Copies an integer into the given SV, also updating its string value.
8820 Does not handle 'set' magic. See C<sv_setpviv_mg>.
8826 Perl_sv_setpviv(pTHX_ SV *sv, IV iv)
8828 char buf[TYPE_CHARS(UV)];
8830 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
8832 sv_setpvn(sv, ptr, ebuf - ptr);
8836 =for apidoc sv_setpviv_mg
8838 Like C<sv_setpviv>, but also handles 'set' magic.
8844 Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv)
8846 char buf[TYPE_CHARS(UV)];
8848 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
8850 sv_setpvn(sv, ptr, ebuf - ptr);
8854 #if defined(PERL_IMPLICIT_CONTEXT)
8856 /* pTHX_ magic can't cope with varargs, so this is a no-context
8857 * version of the main function, (which may itself be aliased to us).
8858 * Don't access this version directly.
8862 Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...)
8866 va_start(args, pat);
8867 sv_vsetpvf(sv, pat, &args);
8871 /* pTHX_ magic can't cope with varargs, so this is a no-context
8872 * version of the main function, (which may itself be aliased to us).
8873 * Don't access this version directly.
8877 Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...)
8881 va_start(args, pat);
8882 sv_vsetpvf_mg(sv, pat, &args);
8888 =for apidoc sv_setpvf
8890 Works like C<sv_catpvf> but copies the text into the SV instead of
8891 appending it. Does not handle 'set' magic. See C<sv_setpvf_mg>.
8897 Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...)
8900 va_start(args, pat);
8901 sv_vsetpvf(sv, pat, &args);
8906 =for apidoc sv_vsetpvf
8908 Works like C<sv_vcatpvf> but copies the text into the SV instead of
8909 appending it. Does not handle 'set' magic. See C<sv_vsetpvf_mg>.
8911 Usually used via its frontend C<sv_setpvf>.
8917 Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args)
8919 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
8923 =for apidoc sv_setpvf_mg
8925 Like C<sv_setpvf>, but also handles 'set' magic.
8931 Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...)
8934 va_start(args, pat);
8935 sv_vsetpvf_mg(sv, pat, &args);
8940 =for apidoc sv_vsetpvf_mg
8942 Like C<sv_vsetpvf>, but also handles 'set' magic.
8944 Usually used via its frontend C<sv_setpvf_mg>.
8950 Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
8952 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
8956 #if defined(PERL_IMPLICIT_CONTEXT)
8958 /* pTHX_ magic can't cope with varargs, so this is a no-context
8959 * version of the main function, (which may itself be aliased to us).
8960 * Don't access this version directly.
8964 Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...)
8968 va_start(args, pat);
8969 sv_vcatpvf(sv, pat, &args);
8973 /* pTHX_ magic can't cope with varargs, so this is a no-context
8974 * version of the main function, (which may itself be aliased to us).
8975 * Don't access this version directly.
8979 Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...)
8983 va_start(args, pat);
8984 sv_vcatpvf_mg(sv, pat, &args);
8990 =for apidoc sv_catpvf
8992 Processes its arguments like C<sprintf> and appends the formatted
8993 output to an SV. If the appended data contains "wide" characters
8994 (including, but not limited to, SVs with a UTF-8 PV formatted with %s,
8995 and characters >255 formatted with %c), the original SV might get
8996 upgraded to UTF-8. Handles 'get' magic, but not 'set' magic. See
8997 C<sv_catpvf_mg>. If the original SV was UTF-8, the pattern should be
8998 valid UTF-8; if the original SV was bytes, the pattern should be too.
9003 Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...)
9006 va_start(args, pat);
9007 sv_vcatpvf(sv, pat, &args);
9012 =for apidoc sv_vcatpvf
9014 Processes its arguments like C<vsprintf> and appends the formatted output
9015 to an SV. Does not handle 'set' magic. See C<sv_vcatpvf_mg>.
9017 Usually used via its frontend C<sv_catpvf>.
9023 Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args)
9025 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
9029 =for apidoc sv_catpvf_mg
9031 Like C<sv_catpvf>, but also handles 'set' magic.
9037 Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...)
9040 va_start(args, pat);
9041 sv_vcatpvf_mg(sv, pat, &args);
9046 =for apidoc sv_vcatpvf_mg
9048 Like C<sv_vcatpvf>, but also handles 'set' magic.
9050 Usually used via its frontend C<sv_catpvf_mg>.
9056 Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
9058 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
9063 =for apidoc sv_vsetpvfn
9065 Works like C<sv_vcatpvfn> but copies the text into the SV instead of
9068 Usually used via one of its frontends C<sv_vsetpvf> and C<sv_vsetpvf_mg>.
9074 Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
9076 sv_setpvn(sv, "", 0);
9077 sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted);
9080 /* private function for use in sv_vcatpvfn via the EXPECT_NUMBER macro */
9083 S_expect_number(pTHX_ char** pattern)
9086 switch (**pattern) {
9087 case '1': case '2': case '3':
9088 case '4': case '5': case '6':
9089 case '7': case '8': case '9':
9090 while (isDIGIT(**pattern))
9091 var = var * 10 + (*(*pattern)++ - '0');
9095 #define EXPECT_NUMBER(pattern, var) (var = S_expect_number(aTHX_ &pattern))
9098 F0convert(NV nv, char *endbuf, STRLEN *len)
9109 if (uv & 1 && uv == nv)
9110 uv--; /* Round to even */
9112 unsigned dig = uv % 10;
9125 =for apidoc sv_vcatpvfn
9127 Processes its arguments like C<vsprintf> and appends the formatted output
9128 to an SV. Uses an array of SVs if the C style variable argument list is
9129 missing (NULL). When running with taint checks enabled, indicates via
9130 C<maybe_tainted> if results are untrustworthy (often due to the use of
9133 Usually used via one of its frontends C<sv_vcatpvf> and C<sv_vcatpvf_mg>.
9138 /* XXX maybe_tainted is never assigned to, so the doc above is lying. */
9141 Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
9148 static const char nullstr[] = "(null)";
9150 bool has_utf8; /* has the result utf8? */
9151 bool pat_utf8; /* the pattern is in utf8? */
9153 /* Times 4: a decimal digit takes more than 3 binary digits.
9154 * NV_DIG: mantissa takes than many decimal digits.
9155 * Plus 32: Playing safe. */
9156 char ebuf[IV_DIG * 4 + NV_DIG + 32];
9157 /* large enough for "%#.#f" --chip */
9158 /* what about long double NVs? --jhi */
9160 has_utf8 = pat_utf8 = DO_UTF8(sv);
9162 /* no matter what, this is a string now */
9163 (void)SvPV_force(sv, origlen);
9165 /* special-case "", "%s", and "%_" */
9168 if (patlen == 2 && pat[0] == '%') {
9172 const char *s = va_arg(*args, char*);
9173 sv_catpv(sv, s ? s : nullstr);
9175 else if (svix < svmax) {
9176 sv_catsv(sv, *svargs);
9177 if (DO_UTF8(*svargs))
9183 argsv = va_arg(*args, SV*);
9184 sv_catsv(sv, argsv);
9189 /* See comment on '_' below */
9194 #ifndef USE_LONG_DOUBLE
9195 /* special-case "%.<number>[gf]" */
9196 if ( patlen <= 5 && pat[0] == '%' && pat[1] == '.'
9197 && (pat[patlen-1] == 'g' || pat[patlen-1] == 'f') ) {
9198 unsigned digits = 0;
9202 while (*pp >= '0' && *pp <= '9')
9203 digits = 10 * digits + (*pp++ - '0');
9204 if (pp - pat == (int)patlen - 1) {
9208 nv = (NV)va_arg(*args, double);
9209 else if (svix < svmax)
9214 /* Add check for digits != 0 because it seems that some
9215 gconverts are buggy in this case, and we don't yet have
9216 a Configure test for this. */
9217 if (digits && digits < sizeof(ebuf) - NV_DIG - 10) {
9218 /* 0, point, slack */
9219 Gconvert(nv, (int)digits, 0, ebuf);
9221 if (*ebuf) /* May return an empty string for digits==0 */
9224 } else if (!digits) {
9227 if ((p = F0convert(nv, ebuf + sizeof ebuf, &l))) {
9228 sv_catpvn(sv, p, l);
9234 #endif /* !USE_LONG_DOUBLE */
9236 if (!args && svix < svmax && DO_UTF8(*svargs))
9239 patend = (char*)pat + patlen;
9240 for (p = (char*)pat; p < patend; p = q) {
9243 bool vectorize = FALSE;
9244 bool vectorarg = FALSE;
9245 bool vec_utf8 = FALSE;
9251 bool has_precis = FALSE;
9254 bool is_utf8 = FALSE; /* is this item utf8? */
9255 #ifdef HAS_LDBL_SPRINTF_BUG
9256 /* This is to try to fix a bug with irix/nonstop-ux/powerux and
9257 with sfio - Allen <allens@cpan.org> */
9258 bool fix_ldbl_sprintf_bug = FALSE;
9262 U8 utf8buf[UTF8_MAXBYTES+1];
9263 STRLEN esignlen = 0;
9265 char *eptr = Nullch;
9268 U8 *vecstr = Null(U8*);
9275 /* we need a long double target in case HAS_LONG_DOUBLE but
9278 #if defined(HAS_LONG_DOUBLE) && LONG_DOUBLESIZE > DOUBLESIZE
9286 const char *dotstr = ".";
9287 STRLEN dotstrlen = 1;
9288 I32 efix = 0; /* explicit format parameter index */
9289 I32 ewix = 0; /* explicit width index */
9290 I32 epix = 0; /* explicit precision index */
9291 I32 evix = 0; /* explicit vector index */
9292 bool asterisk = FALSE;
9294 /* echo everything up to the next format specification */
9295 for (q = p; q < patend && *q != '%'; ++q) ;
9297 if (has_utf8 && !pat_utf8)
9298 sv_catpvn_utf8_upgrade(sv, p, q - p, nsv);
9300 sv_catpvn(sv, p, q - p);
9307 We allow format specification elements in this order:
9308 \d+\$ explicit format parameter index
9310 v|\*(\d+\$)?v vector with optional (optionally specified) arg
9311 0 flag (as above): repeated to allow "v02"
9312 \d+|\*(\d+\$)? width using optional (optionally specified) arg
9313 \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg
9315 [%bcdefginopsux_DFOUX] format (mandatory)
9317 if (EXPECT_NUMBER(q, width)) {
9358 if (EXPECT_NUMBER(q, ewix))
9367 if ((vectorarg = asterisk)) {
9379 EXPECT_NUMBER(q, width);
9384 vecsv = va_arg(*args, SV*);
9386 vecsv = (evix ? evix <= svmax : svix < svmax) ?
9387 svargs[evix ? evix-1 : svix++] : &PL_sv_undef;
9388 dotstr = SvPVx(vecsv, dotstrlen);
9393 vecsv = va_arg(*args, SV*);
9394 vecstr = (U8*)SvPVx(vecsv,veclen);
9395 vec_utf8 = DO_UTF8(vecsv);
9397 else if (efix ? efix <= svmax : svix < svmax) {
9398 vecsv = svargs[efix ? efix-1 : svix++];
9399 vecstr = (U8*)SvPVx(vecsv,veclen);
9400 vec_utf8 = DO_UTF8(vecsv);
9401 /* if this is a version object, we need to return the
9402 * stringified representation (which the SvPVX has
9403 * already done for us), but not vectorize the args
9405 if ( *q == 'd' && sv_derived_from(vecsv,"version") )
9407 q++; /* skip past the rest of the %vd format */
9408 eptr = (char *) vecstr;
9409 elen = strlen(eptr);
9422 i = va_arg(*args, int);
9424 i = (ewix ? ewix <= svmax : svix < svmax) ?
9425 SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
9427 width = (i < 0) ? -i : i;
9437 if (EXPECT_NUMBER(q, epix) && *q++ != '$')
9439 /* XXX: todo, support specified precision parameter */
9443 i = va_arg(*args, int);
9445 i = (ewix ? ewix <= svmax : svix < svmax)
9446 ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
9447 precis = (i < 0) ? 0 : i;
9452 precis = precis * 10 + (*q++ - '0');
9461 case 'I': /* Ix, I32x, and I64x */
9463 if (q[1] == '6' && q[2] == '4') {
9469 if (q[1] == '3' && q[2] == '2') {
9479 #if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE)
9490 #if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE)
9491 if (*(q + 1) == 'l') { /* lld, llf */
9516 argsv = (efix ? efix <= svmax : svix < svmax) ?
9517 svargs[efix ? efix-1 : svix++] : &PL_sv_undef;
9524 uv = (args && !vectorize) ? va_arg(*args, int) : SvIVx(argsv);
9526 (!UNI_IS_INVARIANT(uv) && SvUTF8(sv)))
9528 eptr = (char*)utf8buf;
9529 elen = uvchr_to_utf8((U8*)eptr, uv) - utf8buf;
9540 if (args && !vectorize) {
9541 eptr = va_arg(*args, char*);
9543 #ifdef MACOS_TRADITIONAL
9544 /* On MacOS, %#s format is used for Pascal strings */
9549 elen = strlen(eptr);
9551 eptr = (char *)nullstr;
9552 elen = sizeof nullstr - 1;
9556 eptr = SvPVx(argsv, elen);
9557 if (DO_UTF8(argsv)) {
9558 if (has_precis && precis < elen) {
9560 sv_pos_u2b(argsv, &p, 0); /* sticks at end */
9563 if (width) { /* fudge width (can't fudge elen) */
9564 width += elen - sv_len_utf8(argsv);
9576 * The "%_" hack might have to be changed someday,
9577 * if ISO or ANSI decide to use '_' for something.
9578 * So we keep it hidden from users' code.
9580 if (!args || vectorize)
9582 argsv = va_arg(*args, SV*);
9583 eptr = SvPVx(argsv, elen);
9589 if (has_precis && elen > precis)
9600 goto format_sv; /* %-p -> %_ */
9604 goto format_sv; /* %-Np -> %.N_ */
9607 if (alt || vectorize)
9609 uv = PTR2UV(args ? va_arg(*args, void*) : argsv);
9627 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
9636 esignbuf[esignlen++] = plus;
9640 case 'h': iv = (short)va_arg(*args, int); break;
9641 case 'l': iv = va_arg(*args, long); break;
9642 case 'V': iv = va_arg(*args, IV); break;
9643 default: iv = va_arg(*args, int); break;
9645 case 'q': iv = va_arg(*args, Quad_t); break;
9650 IV tiv = SvIVx(argsv); /* work around GCC bug #13488 */
9652 case 'h': iv = (short)tiv; break;
9653 case 'l': iv = (long)tiv; break;
9655 default: iv = tiv; break;
9657 case 'q': iv = (Quad_t)tiv; break;
9661 if ( !vectorize ) /* we already set uv above */
9666 esignbuf[esignlen++] = plus;
9670 esignbuf[esignlen++] = '-';
9713 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
9724 case 'h': uv = (unsigned short)va_arg(*args, unsigned); break;
9725 case 'l': uv = va_arg(*args, unsigned long); break;
9726 case 'V': uv = va_arg(*args, UV); break;
9727 default: uv = va_arg(*args, unsigned); break;
9729 case 'q': uv = va_arg(*args, Uquad_t); break;
9734 UV tuv = SvUVx(argsv); /* work around GCC bug #13488 */
9736 case 'h': uv = (unsigned short)tuv; break;
9737 case 'l': uv = (unsigned long)tuv; break;
9739 default: uv = tuv; break;
9741 case 'q': uv = (Uquad_t)tuv; break;
9747 eptr = ebuf + sizeof ebuf;
9753 p = (char*)((c == 'X')
9754 ? "0123456789ABCDEF" : "0123456789abcdef");
9760 esignbuf[esignlen++] = '0';
9761 esignbuf[esignlen++] = c; /* 'x' or 'X' */
9767 *--eptr = '0' + dig;
9769 if (alt && *eptr != '0')
9775 *--eptr = '0' + dig;
9778 esignbuf[esignlen++] = '0';
9779 esignbuf[esignlen++] = 'b';
9782 default: /* it had better be ten or less */
9785 *--eptr = '0' + dig;
9786 } while (uv /= base);
9789 elen = (ebuf + sizeof ebuf) - eptr;
9792 zeros = precis - elen;
9793 else if (precis == 0 && elen == 1 && *eptr == '0')
9798 /* FLOATING POINT */
9801 c = 'f'; /* maybe %F isn't supported here */
9807 /* This is evil, but floating point is even more evil */
9809 /* for SV-style calling, we can only get NV
9810 for C-style calling, we assume %f is double;
9811 for simplicity we allow any of %Lf, %llf, %qf for long double
9815 #if defined(USE_LONG_DOUBLE)
9819 /* [perl #20339] - we should accept and ignore %lf rather than die */
9823 #if defined(USE_LONG_DOUBLE)
9824 intsize = args ? 0 : 'q';
9828 #if defined(HAS_LONG_DOUBLE)
9837 /* now we need (long double) if intsize == 'q', else (double) */
9838 nv = (args && !vectorize) ?
9839 #if LONG_DOUBLESIZE > DOUBLESIZE
9841 va_arg(*args, long double) :
9842 va_arg(*args, double)
9844 va_arg(*args, double)
9850 if (c != 'e' && c != 'E') {
9852 /* FIXME: if HAS_LONG_DOUBLE but not USE_LONG_DOUBLE this
9853 will cast our (long double) to (double) */
9854 (void)Perl_frexp(nv, &i);
9855 if (i == PERL_INT_MIN)
9856 Perl_die(aTHX_ "panic: frexp");
9858 need = BIT_DIGITS(i);
9860 need += has_precis ? precis : 6; /* known default */
9865 #ifdef HAS_LDBL_SPRINTF_BUG
9866 /* This is to try to fix a bug with irix/nonstop-ux/powerux and
9867 with sfio - Allen <allens@cpan.org> */
9870 # define MY_DBL_MAX DBL_MAX
9871 # else /* XXX guessing! HUGE_VAL may be defined as infinity, so not using */
9872 # if DOUBLESIZE >= 8
9873 # define MY_DBL_MAX 1.7976931348623157E+308L
9875 # define MY_DBL_MAX 3.40282347E+38L
9879 # ifdef HAS_LDBL_SPRINTF_BUG_LESS1 /* only between -1L & 1L - Allen */
9880 # define MY_DBL_MAX_BUG 1L
9882 # define MY_DBL_MAX_BUG MY_DBL_MAX
9886 # define MY_DBL_MIN DBL_MIN
9887 # else /* XXX guessing! -Allen */
9888 # if DOUBLESIZE >= 8
9889 # define MY_DBL_MIN 2.2250738585072014E-308L
9891 # define MY_DBL_MIN 1.17549435E-38L
9895 if ((intsize == 'q') && (c == 'f') &&
9896 ((nv < MY_DBL_MAX_BUG) && (nv > -MY_DBL_MAX_BUG)) &&
9898 /* it's going to be short enough that
9899 * long double precision is not needed */
9901 if ((nv <= 0L) && (nv >= -0L))
9902 fix_ldbl_sprintf_bug = TRUE; /* 0 is 0 - easiest */
9904 /* would use Perl_fp_class as a double-check but not
9905 * functional on IRIX - see perl.h comments */
9907 if ((nv >= MY_DBL_MIN) || (nv <= -MY_DBL_MIN)) {
9908 /* It's within the range that a double can represent */
9909 #if defined(DBL_MAX) && !defined(DBL_MIN)
9910 if ((nv >= ((long double)1/DBL_MAX)) ||
9911 (nv <= (-(long double)1/DBL_MAX)))
9913 fix_ldbl_sprintf_bug = TRUE;
9916 if (fix_ldbl_sprintf_bug == TRUE) {
9926 # undef MY_DBL_MAX_BUG
9929 #endif /* HAS_LDBL_SPRINTF_BUG */
9931 need += 20; /* fudge factor */
9932 if (PL_efloatsize < need) {
9933 Safefree(PL_efloatbuf);
9934 PL_efloatsize = need + 20; /* more fudge */
9935 New(906, PL_efloatbuf, PL_efloatsize, char);
9936 PL_efloatbuf[0] = '\0';
9939 if ( !(width || left || plus || alt) && fill != '0'
9940 && has_precis && intsize != 'q' ) { /* Shortcuts */
9941 /* See earlier comment about buggy Gconvert when digits,
9943 if ( c == 'g' && precis) {
9944 Gconvert((NV)nv, (int)precis, 0, PL_efloatbuf);
9945 if (*PL_efloatbuf) /* May return an empty string for digits==0 */
9946 goto float_converted;
9947 } else if ( c == 'f' && !precis) {
9948 if ((eptr = F0convert(nv, ebuf + sizeof ebuf, &elen)))
9952 eptr = ebuf + sizeof ebuf;
9955 /* FIXME: what to do if HAS_LONG_DOUBLE but not PERL_PRIfldbl? */
9956 #if defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl)
9957 if (intsize == 'q') {
9958 /* Copy the one or more characters in a long double
9959 * format before the 'base' ([efgEFG]) character to
9960 * the format string. */
9961 static char const prifldbl[] = PERL_PRIfldbl;
9962 char const *p = prifldbl + sizeof(prifldbl) - 3;
9963 while (p >= prifldbl) { *--eptr = *p--; }
9968 do { *--eptr = '0' + (base % 10); } while (base /= 10);
9973 do { *--eptr = '0' + (base % 10); } while (base /= 10);
9985 /* No taint. Otherwise we are in the strange situation
9986 * where printf() taints but print($float) doesn't.
9988 #if defined(HAS_LONG_DOUBLE)
9990 (void)sprintf(PL_efloatbuf, eptr, nv);
9992 (void)sprintf(PL_efloatbuf, eptr, (double)nv);
9994 (void)sprintf(PL_efloatbuf, eptr, nv);
9997 eptr = PL_efloatbuf;
9998 elen = strlen(PL_efloatbuf);
10004 i = SvCUR(sv) - origlen;
10005 if (args && !vectorize) {
10007 case 'h': *(va_arg(*args, short*)) = i; break;
10008 default: *(va_arg(*args, int*)) = i; break;
10009 case 'l': *(va_arg(*args, long*)) = i; break;
10010 case 'V': *(va_arg(*args, IV*)) = i; break;
10012 case 'q': *(va_arg(*args, Quad_t*)) = i; break;
10017 sv_setuv_mg(argsv, (UV)i);
10019 continue; /* not "break" */
10025 if (!args && ckWARN(WARN_PRINTF) &&
10026 (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) {
10027 SV *msg = sv_newmortal();
10028 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %sprintf: ",
10029 (PL_op->op_type == OP_PRTF) ? "" : "s");
10032 Perl_sv_catpvf(aTHX_ msg,
10033 "\"%%%c\"", c & 0xFF);
10035 Perl_sv_catpvf(aTHX_ msg,
10036 "\"%%\\%03"UVof"\"",
10039 sv_catpv(msg, "end of string");
10040 Perl_warner(aTHX_ packWARN(WARN_PRINTF), "%"SVf, msg); /* yes, this is reentrant */
10043 /* output mangled stuff ... */
10049 /* ... right here, because formatting flags should not apply */
10050 SvGROW(sv, SvCUR(sv) + elen + 1);
10052 Copy(eptr, p, elen, char);
10055 SvCUR_set(sv, p - SvPVX(sv));
10057 continue; /* not "break" */
10060 /* calculate width before utf8_upgrade changes it */
10061 have = esignlen + zeros + elen;
10063 if (is_utf8 != has_utf8) {
10066 sv_utf8_upgrade(sv);
10069 SV *nsv = sv_2mortal(newSVpvn(eptr, elen));
10070 sv_utf8_upgrade(nsv);
10074 SvGROW(sv, SvCUR(sv) + elen + 1);
10079 need = (have > width ? have : width);
10082 SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1);
10084 if (esignlen && fill == '0') {
10085 for (i = 0; i < (int)esignlen; i++)
10086 *p++ = esignbuf[i];
10088 if (gap && !left) {
10089 memset(p, fill, gap);
10092 if (esignlen && fill != '0') {
10093 for (i = 0; i < (int)esignlen; i++)
10094 *p++ = esignbuf[i];
10097 for (i = zeros; i; i--)
10101 Copy(eptr, p, elen, char);
10105 memset(p, ' ', gap);
10110 Copy(dotstr, p, dotstrlen, char);
10114 vectorize = FALSE; /* done iterating over vecstr */
10121 SvCUR_set(sv, p - SvPVX(sv));
10129 /* =========================================================================
10131 =head1 Cloning an interpreter
10133 All the macros and functions in this section are for the private use of
10134 the main function, perl_clone().
10136 The foo_dup() functions make an exact copy of an existing foo thinngy.
10137 During the course of a cloning, a hash table is used to map old addresses
10138 to new addresses. The table is created and manipulated with the
10139 ptr_table_* functions.
10143 ============================================================================*/
10146 #if defined(USE_ITHREADS)
10148 #ifndef GpREFCNT_inc
10149 # define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL)
10153 #define sv_dup_inc(s,t) SvREFCNT_inc(sv_dup(s,t))
10154 #define av_dup(s,t) (AV*)sv_dup((SV*)s,t)
10155 #define av_dup_inc(s,t) (AV*)SvREFCNT_inc(sv_dup((SV*)s,t))
10156 #define hv_dup(s,t) (HV*)sv_dup((SV*)s,t)
10157 #define hv_dup_inc(s,t) (HV*)SvREFCNT_inc(sv_dup((SV*)s,t))
10158 #define cv_dup(s,t) (CV*)sv_dup((SV*)s,t)
10159 #define cv_dup_inc(s,t) (CV*)SvREFCNT_inc(sv_dup((SV*)s,t))
10160 #define io_dup(s,t) (IO*)sv_dup((SV*)s,t)
10161 #define io_dup_inc(s,t) (IO*)SvREFCNT_inc(sv_dup((SV*)s,t))
10162 #define gv_dup(s,t) (GV*)sv_dup((SV*)s,t)
10163 #define gv_dup_inc(s,t) (GV*)SvREFCNT_inc(sv_dup((SV*)s,t))
10164 #define SAVEPV(p) (p ? savepv(p) : Nullch)
10165 #define SAVEPVN(p,n) (p ? savepvn(p,n) : Nullch)
10168 /* Duplicate a regexp. Required reading: pregcomp() and pregfree() in
10169 regcomp.c. AMS 20010712 */
10172 Perl_re_dup(pTHX_ REGEXP *r, CLONE_PARAMS *param)
10177 struct reg_substr_datum *s;
10180 return (REGEXP *)NULL;
10182 if ((ret = (REGEXP *)ptr_table_fetch(PL_ptr_table, r)))
10185 len = r->offsets[0];
10186 npar = r->nparens+1;
10188 Newc(0, ret, sizeof(regexp) + (len+1)*sizeof(regnode), char, regexp);
10189 Copy(r->program, ret->program, len+1, regnode);
10191 New(0, ret->startp, npar, I32);
10192 Copy(r->startp, ret->startp, npar, I32);
10193 New(0, ret->endp, npar, I32);
10194 Copy(r->startp, ret->startp, npar, I32);
10196 New(0, ret->substrs, 1, struct reg_substr_data);
10197 for (s = ret->substrs->data, i = 0; i < 3; i++, s++) {
10198 s->min_offset = r->substrs->data[i].min_offset;
10199 s->max_offset = r->substrs->data[i].max_offset;
10200 s->substr = sv_dup_inc(r->substrs->data[i].substr, param);
10201 s->utf8_substr = sv_dup_inc(r->substrs->data[i].utf8_substr, param);
10204 ret->regstclass = NULL;
10206 struct reg_data *d;
10207 const int count = r->data->count;
10209 Newc(0, d, sizeof(struct reg_data) + count*sizeof(void *),
10210 char, struct reg_data);
10211 New(0, d->what, count, U8);
10214 for (i = 0; i < count; i++) {
10215 d->what[i] = r->data->what[i];
10216 switch (d->what[i]) {
10217 /* legal options are one of: sfpont
10218 see also regcomp.h and pregfree() */
10220 d->data[i] = sv_dup_inc((SV *)r->data->data[i], param);
10223 d->data[i] = av_dup_inc((AV *)r->data->data[i], param);
10226 /* This is cheating. */
10227 New(0, d->data[i], 1, struct regnode_charclass_class);
10228 StructCopy(r->data->data[i], d->data[i],
10229 struct regnode_charclass_class);
10230 ret->regstclass = (regnode*)d->data[i];
10233 /* Compiled op trees are readonly, and can thus be
10234 shared without duplication. */
10236 d->data[i] = (void*)OpREFCNT_inc((OP*)r->data->data[i]);
10240 d->data[i] = r->data->data[i];
10243 d->data[i] = r->data->data[i];
10245 ((reg_trie_data*)d->data[i])->refcount++;
10249 Perl_croak(aTHX_ "panic: re_dup unknown data code '%c'", r->data->what[i]);
10258 New(0, ret->offsets, 2*len+1, U32);
10259 Copy(r->offsets, ret->offsets, 2*len+1, U32);
10261 ret->precomp = SAVEPVN(r->precomp, r->prelen);
10262 ret->refcnt = r->refcnt;
10263 ret->minlen = r->minlen;
10264 ret->prelen = r->prelen;
10265 ret->nparens = r->nparens;
10266 ret->lastparen = r->lastparen;
10267 ret->lastcloseparen = r->lastcloseparen;
10268 ret->reganch = r->reganch;
10270 ret->sublen = r->sublen;
10272 if (RX_MATCH_COPIED(ret))
10273 ret->subbeg = SAVEPVN(r->subbeg, r->sublen);
10275 ret->subbeg = Nullch;
10276 #ifdef PERL_COPY_ON_WRITE
10277 ret->saved_copy = Nullsv;
10280 ptr_table_store(PL_ptr_table, r, ret);
10284 /* duplicate a file handle */
10287 Perl_fp_dup(pTHX_ PerlIO *fp, char type, CLONE_PARAMS *param)
10293 return (PerlIO*)NULL;
10295 /* look for it in the table first */
10296 ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp);
10300 /* create anew and remember what it is */
10301 ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE);
10302 ptr_table_store(PL_ptr_table, fp, ret);
10306 /* duplicate a directory handle */
10309 Perl_dirp_dup(pTHX_ DIR *dp)
10317 /* duplicate a typeglob */
10320 Perl_gp_dup(pTHX_ GP *gp, CLONE_PARAMS* param)
10325 /* look for it in the table first */
10326 ret = (GP*)ptr_table_fetch(PL_ptr_table, gp);
10330 /* create anew and remember what it is */
10331 Newz(0, ret, 1, GP);
10332 ptr_table_store(PL_ptr_table, gp, ret);
10335 ret->gp_refcnt = 0; /* must be before any other dups! */
10336 ret->gp_sv = sv_dup_inc(gp->gp_sv, param);
10337 ret->gp_io = io_dup_inc(gp->gp_io, param);
10338 ret->gp_form = cv_dup_inc(gp->gp_form, param);
10339 ret->gp_av = av_dup_inc(gp->gp_av, param);
10340 ret->gp_hv = hv_dup_inc(gp->gp_hv, param);
10341 ret->gp_egv = gv_dup(gp->gp_egv, param);/* GvEGV is not refcounted */
10342 ret->gp_cv = cv_dup_inc(gp->gp_cv, param);
10343 ret->gp_cvgen = gp->gp_cvgen;
10344 ret->gp_flags = gp->gp_flags;
10345 ret->gp_line = gp->gp_line;
10346 ret->gp_file = gp->gp_file; /* points to COP.cop_file */
10350 /* duplicate a chain of magic */
10353 Perl_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS* param)
10355 MAGIC *mgprev = (MAGIC*)NULL;
10358 return (MAGIC*)NULL;
10359 /* look for it in the table first */
10360 mgret = (MAGIC*)ptr_table_fetch(PL_ptr_table, mg);
10364 for (; mg; mg = mg->mg_moremagic) {
10366 Newz(0, nmg, 1, MAGIC);
10368 mgprev->mg_moremagic = nmg;
10371 nmg->mg_virtual = mg->mg_virtual; /* XXX copy dynamic vtable? */
10372 nmg->mg_private = mg->mg_private;
10373 nmg->mg_type = mg->mg_type;
10374 nmg->mg_flags = mg->mg_flags;
10375 if (mg->mg_type == PERL_MAGIC_qr) {
10376 nmg->mg_obj = (SV*)re_dup((REGEXP*)mg->mg_obj, param);
10378 else if(mg->mg_type == PERL_MAGIC_backref) {
10379 const AV * const av = (AV*) mg->mg_obj;
10382 (void)SvREFCNT_inc(nmg->mg_obj = (SV*)newAV());
10384 for (i = AvFILLp(av); i >= 0; i--) {
10385 if (!svp[i]) continue;
10386 av_push((AV*)nmg->mg_obj,sv_dup(svp[i],param));
10390 nmg->mg_obj = (mg->mg_flags & MGf_REFCOUNTED)
10391 ? sv_dup_inc(mg->mg_obj, param)
10392 : sv_dup(mg->mg_obj, param);
10394 nmg->mg_len = mg->mg_len;
10395 nmg->mg_ptr = mg->mg_ptr; /* XXX random ptr? */
10396 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
10397 if (mg->mg_len > 0) {
10398 nmg->mg_ptr = SAVEPVN(mg->mg_ptr, mg->mg_len);
10399 if (mg->mg_type == PERL_MAGIC_overload_table &&
10400 AMT_AMAGIC((AMT*)mg->mg_ptr))
10402 AMT *amtp = (AMT*)mg->mg_ptr;
10403 AMT *namtp = (AMT*)nmg->mg_ptr;
10405 for (i = 1; i < NofAMmeth; i++) {
10406 namtp->table[i] = cv_dup_inc(amtp->table[i], param);
10410 else if (mg->mg_len == HEf_SVKEY)
10411 nmg->mg_ptr = (char*)sv_dup_inc((SV*)mg->mg_ptr, param);
10413 if ((mg->mg_flags & MGf_DUP) && mg->mg_virtual && mg->mg_virtual->svt_dup) {
10414 CALL_FPTR(nmg->mg_virtual->svt_dup)(aTHX_ nmg, param);
10421 /* create a new pointer-mapping table */
10424 Perl_ptr_table_new(pTHX)
10427 Newz(0, tbl, 1, PTR_TBL_t);
10428 tbl->tbl_max = 511;
10429 tbl->tbl_items = 0;
10430 Newz(0, tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
10435 # define PTR_TABLE_HASH(ptr) (PTR2UV(ptr) >> 3)
10437 # define PTR_TABLE_HASH(ptr) (PTR2UV(ptr) >> 2)
10445 register struct ptr_tbl_ent* pte;
10446 register struct ptr_tbl_ent* pteend;
10447 New(0, pte, PERL_ARENA_SIZE/sizeof(struct ptr_tbl_ent), struct ptr_tbl_ent);
10448 pte->next = PL_pte_arenaroot;
10449 PL_pte_arenaroot = pte;
10451 pteend = &pte[PERL_ARENA_SIZE / sizeof(struct ptr_tbl_ent) - 1];
10452 PL_pte_root = ++pte;
10453 while (pte < pteend) {
10454 pte->next = pte + 1;
10460 STATIC struct ptr_tbl_ent*
10463 struct ptr_tbl_ent* pte;
10467 PL_pte_root = pte->next;
10472 S_del_pte(pTHX_ struct ptr_tbl_ent*p)
10474 p->next = PL_pte_root;
10478 /* map an existing pointer using a table */
10481 Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, void *sv)
10483 PTR_TBL_ENT_t *tblent;
10484 const UV hash = PTR_TABLE_HASH(sv);
10486 tblent = tbl->tbl_ary[hash & tbl->tbl_max];
10487 for (; tblent; tblent = tblent->next) {
10488 if (tblent->oldval == sv)
10489 return tblent->newval;
10491 return (void*)NULL;
10494 /* add a new entry to a pointer-mapping table */
10497 Perl_ptr_table_store(pTHX_ PTR_TBL_t *tbl, void *oldv, void *newv)
10499 PTR_TBL_ENT_t *tblent, **otblent;
10500 /* XXX this may be pessimal on platforms where pointers aren't good
10501 * hash values e.g. if they grow faster in the most significant
10503 const UV hash = PTR_TABLE_HASH(oldv);
10507 otblent = &tbl->tbl_ary[hash & tbl->tbl_max];
10508 for (tblent = *otblent; tblent; empty=0, tblent = tblent->next) {
10509 if (tblent->oldval == oldv) {
10510 tblent->newval = newv;
10514 tblent = S_new_pte(aTHX);
10515 tblent->oldval = oldv;
10516 tblent->newval = newv;
10517 tblent->next = *otblent;
10520 if (!empty && tbl->tbl_items > tbl->tbl_max)
10521 ptr_table_split(tbl);
10524 /* double the hash bucket size of an existing ptr table */
10527 Perl_ptr_table_split(pTHX_ PTR_TBL_t *tbl)
10529 PTR_TBL_ENT_t **ary = tbl->tbl_ary;
10530 const UV oldsize = tbl->tbl_max + 1;
10531 UV newsize = oldsize * 2;
10534 Renew(ary, newsize, PTR_TBL_ENT_t*);
10535 Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
10536 tbl->tbl_max = --newsize;
10537 tbl->tbl_ary = ary;
10538 for (i=0; i < oldsize; i++, ary++) {
10539 PTR_TBL_ENT_t **curentp, **entp, *ent;
10542 curentp = ary + oldsize;
10543 for (entp = ary, ent = *ary; ent; ent = *entp) {
10544 if ((newsize & PTR_TABLE_HASH(ent->oldval)) != i) {
10546 ent->next = *curentp;
10556 /* remove all the entries from a ptr table */
10559 Perl_ptr_table_clear(pTHX_ PTR_TBL_t *tbl)
10561 register PTR_TBL_ENT_t **array;
10562 register PTR_TBL_ENT_t *entry;
10566 if (!tbl || !tbl->tbl_items) {
10570 array = tbl->tbl_ary;
10572 max = tbl->tbl_max;
10576 PTR_TBL_ENT_t *oentry = entry;
10577 entry = entry->next;
10578 S_del_pte(aTHX_ oentry);
10581 if (++riter > max) {
10584 entry = array[riter];
10588 tbl->tbl_items = 0;
10591 /* clear and free a ptr table */
10594 Perl_ptr_table_free(pTHX_ PTR_TBL_t *tbl)
10599 ptr_table_clear(tbl);
10600 Safefree(tbl->tbl_ary);
10604 /* attempt to make everything in the typeglob readonly */
10607 S_gv_share(pTHX_ SV *sstr, CLONE_PARAMS *param)
10609 GV *gv = (GV*)sstr;
10610 SV *sv = ¶m->proto_perl->Isv_no; /* just need SvREADONLY-ness */
10612 if (GvIO(gv) || GvFORM(gv)) {
10613 GvUNIQUE_off(gv); /* GvIOs cannot be shared. nor can GvFORMs */
10615 else if (!GvCV(gv)) {
10616 GvCV(gv) = (CV*)sv;
10619 /* CvPADLISTs cannot be shared */
10620 if (!SvREADONLY(GvCV(gv)) && !CvXSUB(GvCV(gv))) {
10625 if (!GvUNIQUE(gv)) {
10627 PerlIO_printf(Perl_debug_log, "gv_share: unable to share %s::%s\n",
10628 HvNAME(GvSTASH(gv)), GvNAME(gv));
10634 * write attempts will die with
10635 * "Modification of a read-only value attempted"
10641 SvREADONLY_on(GvSV(gv));
10645 GvAV(gv) = (AV*)sv;
10648 SvREADONLY_on(GvAV(gv));
10652 GvHV(gv) = (HV*)sv;
10655 SvREADONLY_on(GvHV(gv));
10658 return sstr; /* he_dup() will SvREFCNT_inc() */
10661 /* duplicate an SV of any type (including AV, HV etc) */
10664 Perl_rvpv_dup(pTHX_ SV *dstr, SV *sstr, CLONE_PARAMS* param)
10667 SvRV_set(dstr, SvWEAKREF(sstr)
10668 ? sv_dup(SvRV(sstr), param)
10669 : sv_dup_inc(SvRV(sstr), param));
10672 else if (SvPVX(sstr)) {
10673 /* Has something there */
10675 /* Normal PV - clone whole allocated space */
10676 SvPV_set(dstr, SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1));
10677 if (SvREADONLY(sstr) && SvFAKE(sstr)) {
10678 /* Not that normal - actually sstr is copy on write.
10679 But we are a true, independant SV, so: */
10680 SvREADONLY_off(dstr);
10685 /* Special case - not normally malloced for some reason */
10686 if (SvREADONLY(sstr) && SvFAKE(sstr)) {
10687 /* A "shared" PV - clone it as unshared string */
10688 if(SvPADTMP(sstr)) {
10689 /* However, some of them live in the pad
10690 and they should not have these flags
10693 SvPV_set(dstr, sharepvn(SvPVX(sstr), SvCUR(sstr),
10695 SvUV_set(dstr, SvUVX(sstr));
10698 SvPV_set(dstr, SAVEPVN(SvPVX(sstr), SvCUR(sstr)));
10700 SvREADONLY_off(dstr);
10704 /* Some other special case - random pointer */
10705 SvPV_set(dstr, SvPVX(sstr));
10710 /* Copy the Null */
10711 if (SvTYPE(dstr) == SVt_RV)
10712 SvRV_set(dstr, NULL);
10719 Perl_sv_dup(pTHX_ SV *sstr, CLONE_PARAMS* param)
10724 if (!sstr || SvTYPE(sstr) == SVTYPEMASK)
10726 /* look for it in the table first */
10727 dstr = (SV*)ptr_table_fetch(PL_ptr_table, sstr);
10731 if(param->flags & CLONEf_JOIN_IN) {
10732 /** We are joining here so we don't want do clone
10733 something that is bad **/
10735 if(SvTYPE(sstr) == SVt_PVHV &&
10737 /** don't clone stashes if they already exist **/
10738 HV* old_stash = gv_stashpv(HvNAME(sstr),0);
10739 return (SV*) old_stash;
10743 /* create anew and remember what it is */
10746 #ifdef DEBUG_LEAKING_SCALARS
10747 dstr->sv_debug_optype = sstr->sv_debug_optype;
10748 dstr->sv_debug_line = sstr->sv_debug_line;
10749 dstr->sv_debug_inpad = sstr->sv_debug_inpad;
10750 dstr->sv_debug_cloned = 1;
10752 dstr->sv_debug_file = savepv(sstr->sv_debug_file);
10754 dstr->sv_debug_file = savesharedpv(sstr->sv_debug_file);
10758 ptr_table_store(PL_ptr_table, sstr, dstr);
10761 SvFLAGS(dstr) = SvFLAGS(sstr);
10762 SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */
10763 SvREFCNT(dstr) = 0; /* must be before any other dups! */
10766 if (SvANY(sstr) && PL_watch_pvx && SvPVX(sstr) == PL_watch_pvx)
10767 PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n",
10768 PL_watch_pvx, SvPVX(sstr));
10771 /* don't clone objects whose class has asked us not to */
10772 if (SvOBJECT(sstr) && ! (SvFLAGS(SvSTASH(sstr)) & SVphv_CLONEABLE)) {
10773 SvFLAGS(dstr) &= ~SVTYPEMASK;
10774 SvOBJECT_off(dstr);
10778 switch (SvTYPE(sstr)) {
10780 SvANY(dstr) = NULL;
10783 SvANY(dstr) = new_XIV();
10784 SvIV_set(dstr, SvIVX(sstr));
10787 SvANY(dstr) = new_XNV();
10788 SvNV_set(dstr, SvNVX(sstr));
10791 SvANY(dstr) = new_XRV();
10792 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10795 SvANY(dstr) = new_XPV();
10796 SvCUR_set(dstr, SvCUR(sstr));
10797 SvLEN_set(dstr, SvLEN(sstr));
10798 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10801 SvANY(dstr) = new_XPVIV();
10802 SvCUR_set(dstr, SvCUR(sstr));
10803 SvLEN_set(dstr, SvLEN(sstr));
10804 SvIV_set(dstr, SvIVX(sstr));
10805 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10808 SvANY(dstr) = new_XPVNV();
10809 SvCUR_set(dstr, SvCUR(sstr));
10810 SvLEN_set(dstr, SvLEN(sstr));
10811 SvIV_set(dstr, SvIVX(sstr));
10812 SvNV_set(dstr, SvNVX(sstr));
10813 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10816 SvANY(dstr) = new_XPVMG();
10817 SvCUR_set(dstr, SvCUR(sstr));
10818 SvLEN_set(dstr, SvLEN(sstr));
10819 SvIV_set(dstr, SvIVX(sstr));
10820 SvNV_set(dstr, SvNVX(sstr));
10821 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10822 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
10823 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10826 SvANY(dstr) = new_XPVBM();
10827 SvCUR_set(dstr, SvCUR(sstr));
10828 SvLEN_set(dstr, SvLEN(sstr));
10829 SvIV_set(dstr, SvIVX(sstr));
10830 SvNV_set(dstr, SvNVX(sstr));
10831 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10832 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
10833 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10834 BmRARE(dstr) = BmRARE(sstr);
10835 BmUSEFUL(dstr) = BmUSEFUL(sstr);
10836 BmPREVIOUS(dstr)= BmPREVIOUS(sstr);
10839 SvANY(dstr) = new_XPVLV();
10840 SvCUR_set(dstr, SvCUR(sstr));
10841 SvLEN_set(dstr, SvLEN(sstr));
10842 SvIV_set(dstr, SvIVX(sstr));
10843 SvNV_set(dstr, SvNVX(sstr));
10844 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10845 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
10846 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10847 LvTARGOFF(dstr) = LvTARGOFF(sstr); /* XXX sometimes holds PMOP* when DEBUGGING */
10848 LvTARGLEN(dstr) = LvTARGLEN(sstr);
10849 if (LvTYPE(sstr) == 't') /* for tie: unrefcnted fake (SV**) */
10850 LvTARG(dstr) = dstr;
10851 else if (LvTYPE(sstr) == 'T') /* for tie: fake HE */
10852 LvTARG(dstr) = (SV*)he_dup((HE*)LvTARG(sstr), 0, param);
10854 LvTARG(dstr) = sv_dup_inc(LvTARG(sstr), param);
10855 LvTYPE(dstr) = LvTYPE(sstr);
10858 if (GvUNIQUE((GV*)sstr)) {
10860 if ((share = gv_share(sstr, param))) {
10863 ptr_table_store(PL_ptr_table, sstr, dstr);
10865 PerlIO_printf(Perl_debug_log, "sv_dup: sharing %s::%s\n",
10866 HvNAME(GvSTASH(share)), GvNAME(share));
10871 SvANY(dstr) = new_XPVGV();
10872 SvCUR_set(dstr, SvCUR(sstr));
10873 SvLEN_set(dstr, SvLEN(sstr));
10874 SvIV_set(dstr, SvIVX(sstr));
10875 SvNV_set(dstr, SvNVX(sstr));
10876 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10877 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
10878 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10879 GvNAMELEN(dstr) = GvNAMELEN(sstr);
10880 GvNAME(dstr) = SAVEPVN(GvNAME(sstr), GvNAMELEN(sstr));
10881 GvSTASH(dstr) = hv_dup_inc(GvSTASH(sstr), param);
10882 GvFLAGS(dstr) = GvFLAGS(sstr);
10883 GvGP(dstr) = gp_dup(GvGP(sstr), param);
10884 (void)GpREFCNT_inc(GvGP(dstr));
10887 SvANY(dstr) = new_XPVIO();
10888 SvCUR_set(dstr, SvCUR(sstr));
10889 SvLEN_set(dstr, SvLEN(sstr));
10890 SvIV_set(dstr, SvIVX(sstr));
10891 SvNV_set(dstr, SvNVX(sstr));
10892 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10893 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
10894 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10895 IoIFP(dstr) = fp_dup(IoIFP(sstr), IoTYPE(sstr), param);
10896 if (IoOFP(sstr) == IoIFP(sstr))
10897 IoOFP(dstr) = IoIFP(dstr);
10899 IoOFP(dstr) = fp_dup(IoOFP(sstr), IoTYPE(sstr), param);
10900 /* PL_rsfp_filters entries have fake IoDIRP() */
10901 if (IoDIRP(sstr) && !(IoFLAGS(sstr) & IOf_FAKE_DIRP))
10902 IoDIRP(dstr) = dirp_dup(IoDIRP(sstr));
10904 IoDIRP(dstr) = IoDIRP(sstr);
10905 IoLINES(dstr) = IoLINES(sstr);
10906 IoPAGE(dstr) = IoPAGE(sstr);
10907 IoPAGE_LEN(dstr) = IoPAGE_LEN(sstr);
10908 IoLINES_LEFT(dstr) = IoLINES_LEFT(sstr);
10909 if(IoFLAGS(sstr) & IOf_FAKE_DIRP) {
10910 /* I have no idea why fake dirp (rsfps)
10911 should be treaded differently but otherwise
10912 we end up with leaks -- sky*/
10913 IoTOP_GV(dstr) = gv_dup_inc(IoTOP_GV(sstr), param);
10914 IoFMT_GV(dstr) = gv_dup_inc(IoFMT_GV(sstr), param);
10915 IoBOTTOM_GV(dstr) = gv_dup_inc(IoBOTTOM_GV(sstr), param);
10917 IoTOP_GV(dstr) = gv_dup(IoTOP_GV(sstr), param);
10918 IoFMT_GV(dstr) = gv_dup(IoFMT_GV(sstr), param);
10919 IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(sstr), param);
10921 IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(sstr));
10922 IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(sstr));
10923 IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(sstr));
10924 IoSUBPROCESS(dstr) = IoSUBPROCESS(sstr);
10925 IoTYPE(dstr) = IoTYPE(sstr);
10926 IoFLAGS(dstr) = IoFLAGS(sstr);
10929 SvANY(dstr) = new_XPVAV();
10930 SvCUR_set(dstr, SvCUR(sstr));
10931 SvLEN_set(dstr, SvLEN(sstr));
10932 SvIV_set(dstr, SvIVX(sstr));
10933 SvNV_set(dstr, SvNVX(sstr));
10934 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10935 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
10936 AvARYLEN((AV*)dstr) = sv_dup_inc(AvARYLEN((AV*)sstr), param);
10937 AvFLAGS((AV*)dstr) = AvFLAGS((AV*)sstr);
10938 if (AvARRAY((AV*)sstr)) {
10939 SV **dst_ary, **src_ary;
10940 SSize_t items = AvFILLp((AV*)sstr) + 1;
10942 src_ary = AvARRAY((AV*)sstr);
10943 Newz(0, dst_ary, AvMAX((AV*)sstr)+1, SV*);
10944 ptr_table_store(PL_ptr_table, src_ary, dst_ary);
10945 SvPV_set(dstr, (char*)dst_ary);
10946 AvALLOC((AV*)dstr) = dst_ary;
10947 if (AvREAL((AV*)sstr)) {
10948 while (items-- > 0)
10949 *dst_ary++ = sv_dup_inc(*src_ary++, param);
10952 while (items-- > 0)
10953 *dst_ary++ = sv_dup(*src_ary++, param);
10955 items = AvMAX((AV*)sstr) - AvFILLp((AV*)sstr);
10956 while (items-- > 0) {
10957 *dst_ary++ = &PL_sv_undef;
10961 SvPV_set(dstr, Nullch);
10962 AvALLOC((AV*)dstr) = (SV**)NULL;
10966 SvANY(dstr) = new_XPVHV();
10967 SvCUR_set(dstr, SvCUR(sstr));
10968 SvLEN_set(dstr, SvLEN(sstr));
10969 SvIV_set(dstr, SvIVX(sstr));
10970 SvNV_set(dstr, SvNVX(sstr));
10971 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10972 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
10973 HvRITER((HV*)dstr) = HvRITER((HV*)sstr);
10974 if (HvARRAY((HV*)sstr)) {
10976 XPVHV *dxhv = (XPVHV*)SvANY(dstr);
10977 XPVHV *sxhv = (XPVHV*)SvANY(sstr);
10978 Newz(0, dxhv->xhv_array,
10979 PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1), char);
10980 while (i <= sxhv->xhv_max) {
10981 ((HE**)dxhv->xhv_array)[i] = he_dup(((HE**)sxhv->xhv_array)[i],
10982 (bool)!!HvSHAREKEYS(sstr),
10986 dxhv->xhv_eiter = he_dup(sxhv->xhv_eiter,
10987 (bool)!!HvSHAREKEYS(sstr), param);
10990 SvPV_set(dstr, Nullch);
10991 HvEITER((HV*)dstr) = (HE*)NULL;
10993 HvPMROOT((HV*)dstr) = HvPMROOT((HV*)sstr); /* XXX */
10994 HvNAME((HV*)dstr) = SAVEPV(HvNAME((HV*)sstr));
10995 /* Record stashes for possible cloning in Perl_clone(). */
10996 if(HvNAME((HV*)dstr))
10997 av_push(param->stashes, dstr);
11000 SvANY(dstr) = new_XPVFM();
11001 FmLINES(dstr) = FmLINES(sstr);
11005 SvANY(dstr) = new_XPVCV();
11007 SvCUR_set(dstr, SvCUR(sstr));
11008 SvLEN_set(dstr, SvLEN(sstr));
11009 SvIV_set(dstr, SvIVX(sstr));
11010 SvNV_set(dstr, SvNVX(sstr));
11011 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
11012 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
11013 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
11014 CvSTASH(dstr) = hv_dup(CvSTASH(sstr), param); /* NOTE: not refcounted */
11015 CvSTART(dstr) = CvSTART(sstr);
11017 CvROOT(dstr) = OpREFCNT_inc(CvROOT(sstr));
11019 CvXSUB(dstr) = CvXSUB(sstr);
11020 CvXSUBANY(dstr) = CvXSUBANY(sstr);
11021 if (CvCONST(sstr)) {
11022 CvXSUBANY(dstr).any_ptr = GvUNIQUE(CvGV(sstr)) ?
11023 SvREFCNT_inc(CvXSUBANY(sstr).any_ptr) :
11024 sv_dup_inc((SV *)CvXSUBANY(sstr).any_ptr, param);
11026 /* don't dup if copying back - CvGV isn't refcounted, so the
11027 * duped GV may never be freed. A bit of a hack! DAPM */
11028 CvGV(dstr) = (param->flags & CLONEf_JOIN_IN) ?
11029 Nullgv : gv_dup(CvGV(sstr), param) ;
11030 if (param->flags & CLONEf_COPY_STACKS) {
11031 CvDEPTH(dstr) = CvDEPTH(sstr);
11035 PAD_DUP(CvPADLIST(dstr), CvPADLIST(sstr), param);
11036 CvOUTSIDE_SEQ(dstr) = CvOUTSIDE_SEQ(sstr);
11038 CvWEAKOUTSIDE(sstr)
11039 ? cv_dup( CvOUTSIDE(sstr), param)
11040 : cv_dup_inc(CvOUTSIDE(sstr), param);
11041 CvFLAGS(dstr) = CvFLAGS(sstr);
11042 CvFILE(dstr) = CvXSUB(sstr) ? CvFILE(sstr) : SAVEPV(CvFILE(sstr));
11045 Perl_croak(aTHX_ "Bizarre SvTYPE [%" IVdf "]", (IV)SvTYPE(sstr));
11049 if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO)
11055 /* duplicate a context */
11058 Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max, CLONE_PARAMS* param)
11060 PERL_CONTEXT *ncxs;
11063 return (PERL_CONTEXT*)NULL;
11065 /* look for it in the table first */
11066 ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs);
11070 /* create anew and remember what it is */
11071 Newz(56, ncxs, max + 1, PERL_CONTEXT);
11072 ptr_table_store(PL_ptr_table, cxs, ncxs);
11075 PERL_CONTEXT *cx = &cxs[ix];
11076 PERL_CONTEXT *ncx = &ncxs[ix];
11077 ncx->cx_type = cx->cx_type;
11078 if (CxTYPE(cx) == CXt_SUBST) {
11079 Perl_croak(aTHX_ "Cloning substitution context is unimplemented");
11082 ncx->blk_oldsp = cx->blk_oldsp;
11083 ncx->blk_oldcop = cx->blk_oldcop;
11084 ncx->blk_oldmarksp = cx->blk_oldmarksp;
11085 ncx->blk_oldscopesp = cx->blk_oldscopesp;
11086 ncx->blk_oldpm = cx->blk_oldpm;
11087 ncx->blk_gimme = cx->blk_gimme;
11088 switch (CxTYPE(cx)) {
11090 ncx->blk_sub.cv = (cx->blk_sub.olddepth == 0
11091 ? cv_dup_inc(cx->blk_sub.cv, param)
11092 : cv_dup(cx->blk_sub.cv,param));
11093 ncx->blk_sub.argarray = (cx->blk_sub.hasargs
11094 ? av_dup_inc(cx->blk_sub.argarray, param)
11096 ncx->blk_sub.savearray = av_dup_inc(cx->blk_sub.savearray, param);
11097 ncx->blk_sub.olddepth = cx->blk_sub.olddepth;
11098 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
11099 ncx->blk_sub.lval = cx->blk_sub.lval;
11100 ncx->blk_sub.retop = cx->blk_sub.retop;
11103 ncx->blk_eval.old_in_eval = cx->blk_eval.old_in_eval;
11104 ncx->blk_eval.old_op_type = cx->blk_eval.old_op_type;
11105 ncx->blk_eval.old_namesv = sv_dup_inc(cx->blk_eval.old_namesv, param);
11106 ncx->blk_eval.old_eval_root = cx->blk_eval.old_eval_root;
11107 ncx->blk_eval.cur_text = sv_dup(cx->blk_eval.cur_text, param);
11108 ncx->blk_eval.retop = cx->blk_eval.retop;
11111 ncx->blk_loop.label = cx->blk_loop.label;
11112 ncx->blk_loop.resetsp = cx->blk_loop.resetsp;
11113 ncx->blk_loop.redo_op = cx->blk_loop.redo_op;
11114 ncx->blk_loop.next_op = cx->blk_loop.next_op;
11115 ncx->blk_loop.last_op = cx->blk_loop.last_op;
11116 ncx->blk_loop.iterdata = (CxPADLOOP(cx)
11117 ? cx->blk_loop.iterdata
11118 : gv_dup((GV*)cx->blk_loop.iterdata, param));
11119 ncx->blk_loop.oldcomppad
11120 = (PAD*)ptr_table_fetch(PL_ptr_table,
11121 cx->blk_loop.oldcomppad);
11122 ncx->blk_loop.itersave = sv_dup_inc(cx->blk_loop.itersave, param);
11123 ncx->blk_loop.iterlval = sv_dup_inc(cx->blk_loop.iterlval, param);
11124 ncx->blk_loop.iterary = av_dup_inc(cx->blk_loop.iterary, param);
11125 ncx->blk_loop.iterix = cx->blk_loop.iterix;
11126 ncx->blk_loop.itermax = cx->blk_loop.itermax;
11129 ncx->blk_sub.cv = cv_dup(cx->blk_sub.cv, param);
11130 ncx->blk_sub.gv = gv_dup(cx->blk_sub.gv, param);
11131 ncx->blk_sub.dfoutgv = gv_dup_inc(cx->blk_sub.dfoutgv, param);
11132 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
11133 ncx->blk_sub.retop = cx->blk_sub.retop;
11145 /* duplicate a stack info structure */
11148 Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param)
11153 return (PERL_SI*)NULL;
11155 /* look for it in the table first */
11156 nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si);
11160 /* create anew and remember what it is */
11161 Newz(56, nsi, 1, PERL_SI);
11162 ptr_table_store(PL_ptr_table, si, nsi);
11164 nsi->si_stack = av_dup_inc(si->si_stack, param);
11165 nsi->si_cxix = si->si_cxix;
11166 nsi->si_cxmax = si->si_cxmax;
11167 nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax, param);
11168 nsi->si_type = si->si_type;
11169 nsi->si_prev = si_dup(si->si_prev, param);
11170 nsi->si_next = si_dup(si->si_next, param);
11171 nsi->si_markoff = si->si_markoff;
11176 #define POPINT(ss,ix) ((ss)[--(ix)].any_i32)
11177 #define TOPINT(ss,ix) ((ss)[ix].any_i32)
11178 #define POPLONG(ss,ix) ((ss)[--(ix)].any_long)
11179 #define TOPLONG(ss,ix) ((ss)[ix].any_long)
11180 #define POPIV(ss,ix) ((ss)[--(ix)].any_iv)
11181 #define TOPIV(ss,ix) ((ss)[ix].any_iv)
11182 #define POPBOOL(ss,ix) ((ss)[--(ix)].any_bool)
11183 #define TOPBOOL(ss,ix) ((ss)[ix].any_bool)
11184 #define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr)
11185 #define TOPPTR(ss,ix) ((ss)[ix].any_ptr)
11186 #define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr)
11187 #define TOPDPTR(ss,ix) ((ss)[ix].any_dptr)
11188 #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr)
11189 #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr)
11192 #define pv_dup_inc(p) SAVEPV(p)
11193 #define pv_dup(p) SAVEPV(p)
11194 #define svp_dup_inc(p,pp) any_dup(p,pp)
11196 /* map any object to the new equivent - either something in the
11197 * ptr table, or something in the interpreter structure
11201 Perl_any_dup(pTHX_ void *v, PerlInterpreter *proto_perl)
11206 return (void*)NULL;
11208 /* look for it in the table first */
11209 ret = ptr_table_fetch(PL_ptr_table, v);
11213 /* see if it is part of the interpreter structure */
11214 if (v >= (void*)proto_perl && v < (void*)(proto_perl+1))
11215 ret = (void*)(((char*)aTHX) + (((char*)v) - (char*)proto_perl));
11223 /* duplicate the save stack */
11226 Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param)
11228 ANY *ss = proto_perl->Tsavestack;
11229 I32 ix = proto_perl->Tsavestack_ix;
11230 I32 max = proto_perl->Tsavestack_max;
11243 void (*dptr) (void*);
11244 void (*dxptr) (pTHX_ void*);
11247 Newz(54, nss, max, ANY);
11251 TOPINT(nss,ix) = i;
11253 case SAVEt_ITEM: /* normal string */
11254 sv = (SV*)POPPTR(ss,ix);
11255 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
11256 sv = (SV*)POPPTR(ss,ix);
11257 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
11259 case SAVEt_SV: /* scalar reference */
11260 sv = (SV*)POPPTR(ss,ix);
11261 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
11262 gv = (GV*)POPPTR(ss,ix);
11263 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
11265 case SAVEt_GENERIC_PVREF: /* generic char* */
11266 c = (char*)POPPTR(ss,ix);
11267 TOPPTR(nss,ix) = pv_dup(c);
11268 ptr = POPPTR(ss,ix);
11269 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11271 case SAVEt_SHARED_PVREF: /* char* in shared space */
11272 c = (char*)POPPTR(ss,ix);
11273 TOPPTR(nss,ix) = savesharedpv(c);
11274 ptr = POPPTR(ss,ix);
11275 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11277 case SAVEt_GENERIC_SVREF: /* generic sv */
11278 case SAVEt_SVREF: /* scalar reference */
11279 sv = (SV*)POPPTR(ss,ix);
11280 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
11281 ptr = POPPTR(ss,ix);
11282 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
11284 case SAVEt_AV: /* array reference */
11285 av = (AV*)POPPTR(ss,ix);
11286 TOPPTR(nss,ix) = av_dup_inc(av, param);
11287 gv = (GV*)POPPTR(ss,ix);
11288 TOPPTR(nss,ix) = gv_dup(gv, param);
11290 case SAVEt_HV: /* hash reference */
11291 hv = (HV*)POPPTR(ss,ix);
11292 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
11293 gv = (GV*)POPPTR(ss,ix);
11294 TOPPTR(nss,ix) = gv_dup(gv, param);
11296 case SAVEt_INT: /* int reference */
11297 ptr = POPPTR(ss,ix);
11298 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11299 intval = (int)POPINT(ss,ix);
11300 TOPINT(nss,ix) = intval;
11302 case SAVEt_LONG: /* long reference */
11303 ptr = POPPTR(ss,ix);
11304 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11305 longval = (long)POPLONG(ss,ix);
11306 TOPLONG(nss,ix) = longval;
11308 case SAVEt_I32: /* I32 reference */
11309 case SAVEt_I16: /* I16 reference */
11310 case SAVEt_I8: /* I8 reference */
11311 ptr = POPPTR(ss,ix);
11312 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11314 TOPINT(nss,ix) = i;
11316 case SAVEt_IV: /* IV reference */
11317 ptr = POPPTR(ss,ix);
11318 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11320 TOPIV(nss,ix) = iv;
11322 case SAVEt_SPTR: /* SV* reference */
11323 ptr = POPPTR(ss,ix);
11324 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11325 sv = (SV*)POPPTR(ss,ix);
11326 TOPPTR(nss,ix) = sv_dup(sv, param);
11328 case SAVEt_VPTR: /* random* reference */
11329 ptr = POPPTR(ss,ix);
11330 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11331 ptr = POPPTR(ss,ix);
11332 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11334 case SAVEt_PPTR: /* char* reference */
11335 ptr = POPPTR(ss,ix);
11336 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11337 c = (char*)POPPTR(ss,ix);
11338 TOPPTR(nss,ix) = pv_dup(c);
11340 case SAVEt_HPTR: /* HV* reference */
11341 ptr = POPPTR(ss,ix);
11342 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11343 hv = (HV*)POPPTR(ss,ix);
11344 TOPPTR(nss,ix) = hv_dup(hv, param);
11346 case SAVEt_APTR: /* AV* reference */
11347 ptr = POPPTR(ss,ix);
11348 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11349 av = (AV*)POPPTR(ss,ix);
11350 TOPPTR(nss,ix) = av_dup(av, param);
11353 gv = (GV*)POPPTR(ss,ix);
11354 TOPPTR(nss,ix) = gv_dup(gv, param);
11356 case SAVEt_GP: /* scalar reference */
11357 gp = (GP*)POPPTR(ss,ix);
11358 TOPPTR(nss,ix) = gp = gp_dup(gp, param);
11359 (void)GpREFCNT_inc(gp);
11360 gv = (GV*)POPPTR(ss,ix);
11361 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
11362 c = (char*)POPPTR(ss,ix);
11363 TOPPTR(nss,ix) = pv_dup(c);
11365 TOPIV(nss,ix) = iv;
11367 TOPIV(nss,ix) = iv;
11370 case SAVEt_MORTALIZESV:
11371 sv = (SV*)POPPTR(ss,ix);
11372 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
11375 ptr = POPPTR(ss,ix);
11376 if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) {
11377 /* these are assumed to be refcounted properly */
11378 switch (((OP*)ptr)->op_type) {
11380 case OP_LEAVESUBLV:
11384 case OP_LEAVEWRITE:
11385 TOPPTR(nss,ix) = ptr;
11390 TOPPTR(nss,ix) = Nullop;
11395 TOPPTR(nss,ix) = Nullop;
11398 c = (char*)POPPTR(ss,ix);
11399 TOPPTR(nss,ix) = pv_dup_inc(c);
11401 case SAVEt_CLEARSV:
11402 longval = POPLONG(ss,ix);
11403 TOPLONG(nss,ix) = longval;
11406 hv = (HV*)POPPTR(ss,ix);
11407 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
11408 c = (char*)POPPTR(ss,ix);
11409 TOPPTR(nss,ix) = pv_dup_inc(c);
11411 TOPINT(nss,ix) = i;
11413 case SAVEt_DESTRUCTOR:
11414 ptr = POPPTR(ss,ix);
11415 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
11416 dptr = POPDPTR(ss,ix);
11417 TOPDPTR(nss,ix) = (void (*)(void*))any_dup((void *)dptr, proto_perl);
11419 case SAVEt_DESTRUCTOR_X:
11420 ptr = POPPTR(ss,ix);
11421 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
11422 dxptr = POPDXPTR(ss,ix);
11423 TOPDXPTR(nss,ix) = (void (*)(pTHX_ void*))any_dup((void *)dxptr, proto_perl);
11425 case SAVEt_REGCONTEXT:
11428 TOPINT(nss,ix) = i;
11431 case SAVEt_STACK_POS: /* Position on Perl stack */
11433 TOPINT(nss,ix) = i;
11435 case SAVEt_AELEM: /* array element */
11436 sv = (SV*)POPPTR(ss,ix);
11437 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
11439 TOPINT(nss,ix) = i;
11440 av = (AV*)POPPTR(ss,ix);
11441 TOPPTR(nss,ix) = av_dup_inc(av, param);
11443 case SAVEt_HELEM: /* hash element */
11444 sv = (SV*)POPPTR(ss,ix);
11445 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
11446 sv = (SV*)POPPTR(ss,ix);
11447 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
11448 hv = (HV*)POPPTR(ss,ix);
11449 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
11452 ptr = POPPTR(ss,ix);
11453 TOPPTR(nss,ix) = ptr;
11457 TOPINT(nss,ix) = i;
11459 case SAVEt_COMPPAD:
11460 av = (AV*)POPPTR(ss,ix);
11461 TOPPTR(nss,ix) = av_dup(av, param);
11464 longval = (long)POPLONG(ss,ix);
11465 TOPLONG(nss,ix) = longval;
11466 ptr = POPPTR(ss,ix);
11467 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11468 sv = (SV*)POPPTR(ss,ix);
11469 TOPPTR(nss,ix) = sv_dup(sv, param);
11472 ptr = POPPTR(ss,ix);
11473 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11474 longval = (long)POPBOOL(ss,ix);
11475 TOPBOOL(nss,ix) = (bool)longval;
11477 case SAVEt_SET_SVFLAGS:
11479 TOPINT(nss,ix) = i;
11481 TOPINT(nss,ix) = i;
11482 sv = (SV*)POPPTR(ss,ix);
11483 TOPPTR(nss,ix) = sv_dup(sv, param);
11486 Perl_croak(aTHX_ "panic: ss_dup inconsistency");
11494 /* if sv is a stash, call $class->CLONE_SKIP(), and set the SVphv_CLONEABLE
11495 * flag to the result. This is done for each stash before cloning starts,
11496 * so we know which stashes want their objects cloned */
11499 do_mark_cloneable_stash(pTHX_ SV *sv)
11501 if (HvNAME((HV*)sv)) {
11502 GV* cloner = gv_fetchmethod_autoload((HV*)sv, "CLONE_SKIP", 0);
11503 SvFLAGS(sv) |= SVphv_CLONEABLE; /* clone objects by default */
11504 if (cloner && GvCV(cloner)) {
11511 XPUSHs(sv_2mortal(newSVpv(HvNAME((HV*)sv), 0)));
11513 call_sv((SV*)GvCV(cloner), G_SCALAR);
11520 SvFLAGS(sv) &= ~SVphv_CLONEABLE;
11528 =for apidoc perl_clone
11530 Create and return a new interpreter by cloning the current one.
11532 perl_clone takes these flags as parameters:
11534 CLONEf_COPY_STACKS - is used to, well, copy the stacks also,
11535 without it we only clone the data and zero the stacks,
11536 with it we copy the stacks and the new perl interpreter is
11537 ready to run at the exact same point as the previous one.
11538 The pseudo-fork code uses COPY_STACKS while the
11539 threads->new doesn't.
11541 CLONEf_KEEP_PTR_TABLE
11542 perl_clone keeps a ptr_table with the pointer of the old
11543 variable as a key and the new variable as a value,
11544 this allows it to check if something has been cloned and not
11545 clone it again but rather just use the value and increase the
11546 refcount. If KEEP_PTR_TABLE is not set then perl_clone will kill
11547 the ptr_table using the function
11548 C<ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;>,
11549 reason to keep it around is if you want to dup some of your own
11550 variable who are outside the graph perl scans, example of this
11551 code is in threads.xs create
11554 This is a win32 thing, it is ignored on unix, it tells perls
11555 win32host code (which is c++) to clone itself, this is needed on
11556 win32 if you want to run two threads at the same time,
11557 if you just want to do some stuff in a separate perl interpreter
11558 and then throw it away and return to the original one,
11559 you don't need to do anything.
11564 /* XXX the above needs expanding by someone who actually understands it ! */
11565 EXTERN_C PerlInterpreter *
11566 perl_clone_host(PerlInterpreter* proto_perl, UV flags);
11569 perl_clone(PerlInterpreter *proto_perl, UV flags)
11572 #ifdef PERL_IMPLICIT_SYS
11574 /* perlhost.h so we need to call into it
11575 to clone the host, CPerlHost should have a c interface, sky */
11577 if (flags & CLONEf_CLONE_HOST) {
11578 return perl_clone_host(proto_perl,flags);
11580 return perl_clone_using(proto_perl, flags,
11582 proto_perl->IMemShared,
11583 proto_perl->IMemParse,
11585 proto_perl->IStdIO,
11589 proto_perl->IProc);
11593 perl_clone_using(PerlInterpreter *proto_perl, UV flags,
11594 struct IPerlMem* ipM, struct IPerlMem* ipMS,
11595 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
11596 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
11597 struct IPerlDir* ipD, struct IPerlSock* ipS,
11598 struct IPerlProc* ipP)
11600 /* XXX many of the string copies here can be optimized if they're
11601 * constants; they need to be allocated as common memory and just
11602 * their pointers copied. */
11605 CLONE_PARAMS clone_params;
11606 CLONE_PARAMS* param = &clone_params;
11608 PerlInterpreter *my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
11609 /* for each stash, determine whether its objects should be cloned */
11610 S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
11611 PERL_SET_THX(my_perl);
11614 Poison(my_perl, 1, PerlInterpreter);
11616 PL_curcop = (COP *)Nullop;
11620 PL_savestack_ix = 0;
11621 PL_savestack_max = -1;
11622 PL_sig_pending = 0;
11623 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
11624 # else /* !DEBUGGING */
11625 Zero(my_perl, 1, PerlInterpreter);
11626 # endif /* DEBUGGING */
11628 /* host pointers */
11630 PL_MemShared = ipMS;
11631 PL_MemParse = ipMP;
11638 #else /* !PERL_IMPLICIT_SYS */
11640 CLONE_PARAMS clone_params;
11641 CLONE_PARAMS* param = &clone_params;
11642 PerlInterpreter *my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
11643 /* for each stash, determine whether its objects should be cloned */
11644 S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
11645 PERL_SET_THX(my_perl);
11648 Poison(my_perl, 1, PerlInterpreter);
11650 PL_curcop = (COP *)Nullop;
11654 PL_savestack_ix = 0;
11655 PL_savestack_max = -1;
11656 PL_sig_pending = 0;
11657 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
11658 # else /* !DEBUGGING */
11659 Zero(my_perl, 1, PerlInterpreter);
11660 # endif /* DEBUGGING */
11661 #endif /* PERL_IMPLICIT_SYS */
11662 param->flags = flags;
11663 param->proto_perl = proto_perl;
11666 PL_xiv_arenaroot = NULL;
11667 PL_xiv_root = NULL;
11668 PL_xnv_arenaroot = NULL;
11669 PL_xnv_root = NULL;
11670 PL_xrv_arenaroot = NULL;
11671 PL_xrv_root = NULL;
11672 PL_xpv_arenaroot = NULL;
11673 PL_xpv_root = NULL;
11674 PL_xpviv_arenaroot = NULL;
11675 PL_xpviv_root = NULL;
11676 PL_xpvnv_arenaroot = NULL;
11677 PL_xpvnv_root = NULL;
11678 PL_xpvcv_arenaroot = NULL;
11679 PL_xpvcv_root = NULL;
11680 PL_xpvav_arenaroot = NULL;
11681 PL_xpvav_root = NULL;
11682 PL_xpvhv_arenaroot = NULL;
11683 PL_xpvhv_root = NULL;
11684 PL_xpvmg_arenaroot = NULL;
11685 PL_xpvmg_root = NULL;
11686 PL_xpvlv_arenaroot = NULL;
11687 PL_xpvlv_root = NULL;
11688 PL_xpvbm_arenaroot = NULL;
11689 PL_xpvbm_root = NULL;
11690 PL_he_arenaroot = NULL;
11692 #if defined(USE_ITHREADS)
11693 PL_pte_arenaroot = NULL;
11694 PL_pte_root = NULL;
11696 PL_nice_chunk = NULL;
11697 PL_nice_chunk_size = 0;
11699 PL_sv_objcount = 0;
11700 PL_sv_root = Nullsv;
11701 PL_sv_arenaroot = Nullsv;
11703 PL_debug = proto_perl->Idebug;
11705 #ifdef USE_REENTRANT_API
11706 /* XXX: things like -Dm will segfault here in perlio, but doing
11707 * PERL_SET_CONTEXT(proto_perl);
11708 * breaks too many other things
11710 Perl_reentrant_init(aTHX);
11713 /* create SV map for pointer relocation */
11714 PL_ptr_table = ptr_table_new();
11716 /* initialize these special pointers as early as possible */
11717 SvANY(&PL_sv_undef) = NULL;
11718 SvREFCNT(&PL_sv_undef) = (~(U32)0)/2;
11719 SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL;
11720 ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef);
11722 SvANY(&PL_sv_no) = new_XPVNV();
11723 SvREFCNT(&PL_sv_no) = (~(U32)0)/2;
11724 SvFLAGS(&PL_sv_no) = SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
11725 |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
11726 SvPV_set(&PL_sv_no, SAVEPVN(PL_No, 0));
11727 SvCUR_set(&PL_sv_no, 0);
11728 SvLEN_set(&PL_sv_no, 1);
11729 SvIV_set(&PL_sv_no, 0);
11730 SvNV_set(&PL_sv_no, 0);
11731 ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no);
11733 SvANY(&PL_sv_yes) = new_XPVNV();
11734 SvREFCNT(&PL_sv_yes) = (~(U32)0)/2;
11735 SvFLAGS(&PL_sv_yes) = SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
11736 |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
11737 SvPV_set(&PL_sv_yes, SAVEPVN(PL_Yes, 1));
11738 SvCUR_set(&PL_sv_yes, 1);
11739 SvLEN_set(&PL_sv_yes, 2);
11740 SvIV_set(&PL_sv_yes, 1);
11741 SvNV_set(&PL_sv_yes, 1);
11742 ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes);
11744 /* create (a non-shared!) shared string table */
11745 PL_strtab = newHV();
11746 HvSHAREKEYS_off(PL_strtab);
11747 hv_ksplit(PL_strtab, 512);
11748 ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab);
11750 PL_compiling = proto_perl->Icompiling;
11752 /* These two PVs will be free'd special way so must set them same way op.c does */
11753 PL_compiling.cop_stashpv = savesharedpv(PL_compiling.cop_stashpv);
11754 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_stashpv, PL_compiling.cop_stashpv);
11756 PL_compiling.cop_file = savesharedpv(PL_compiling.cop_file);
11757 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_file, PL_compiling.cop_file);
11759 ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling);
11760 if (!specialWARN(PL_compiling.cop_warnings))
11761 PL_compiling.cop_warnings = sv_dup_inc(PL_compiling.cop_warnings, param);
11762 if (!specialCopIO(PL_compiling.cop_io))
11763 PL_compiling.cop_io = sv_dup_inc(PL_compiling.cop_io, param);
11764 PL_curcop = (COP*)any_dup(proto_perl->Tcurcop, proto_perl);
11766 /* pseudo environmental stuff */
11767 PL_origargc = proto_perl->Iorigargc;
11768 PL_origargv = proto_perl->Iorigargv;
11770 param->stashes = newAV(); /* Setup array of objects to call clone on */
11772 #ifdef PERLIO_LAYERS
11773 /* Clone PerlIO tables as soon as we can handle general xx_dup() */
11774 PerlIO_clone(aTHX_ proto_perl, param);
11777 PL_envgv = gv_dup(proto_perl->Ienvgv, param);
11778 PL_incgv = gv_dup(proto_perl->Iincgv, param);
11779 PL_hintgv = gv_dup(proto_perl->Ihintgv, param);
11780 PL_origfilename = SAVEPV(proto_perl->Iorigfilename);
11781 PL_diehook = sv_dup_inc(proto_perl->Idiehook, param);
11782 PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook, param);
11785 PL_minus_c = proto_perl->Iminus_c;
11786 PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel, param);
11787 PL_localpatches = proto_perl->Ilocalpatches;
11788 PL_splitstr = proto_perl->Isplitstr;
11789 PL_preprocess = proto_perl->Ipreprocess;
11790 PL_minus_n = proto_perl->Iminus_n;
11791 PL_minus_p = proto_perl->Iminus_p;
11792 PL_minus_l = proto_perl->Iminus_l;
11793 PL_minus_a = proto_perl->Iminus_a;
11794 PL_minus_F = proto_perl->Iminus_F;
11795 PL_doswitches = proto_perl->Idoswitches;
11796 PL_dowarn = proto_perl->Idowarn;
11797 PL_doextract = proto_perl->Idoextract;
11798 PL_sawampersand = proto_perl->Isawampersand;
11799 PL_unsafe = proto_perl->Iunsafe;
11800 PL_inplace = SAVEPV(proto_perl->Iinplace);
11801 PL_e_script = sv_dup_inc(proto_perl->Ie_script, param);
11802 PL_perldb = proto_perl->Iperldb;
11803 PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
11804 PL_exit_flags = proto_perl->Iexit_flags;
11806 /* magical thingies */
11807 /* XXX time(&PL_basetime) when asked for? */
11808 PL_basetime = proto_perl->Ibasetime;
11809 PL_formfeed = sv_dup(proto_perl->Iformfeed, param);
11811 PL_maxsysfd = proto_perl->Imaxsysfd;
11812 PL_multiline = proto_perl->Imultiline;
11813 PL_statusvalue = proto_perl->Istatusvalue;
11815 PL_statusvalue_vms = proto_perl->Istatusvalue_vms;
11817 PL_encoding = sv_dup(proto_perl->Iencoding, param);
11819 sv_setpvn(PERL_DEBUG_PAD(0), "", 0); /* For regex debugging. */
11820 sv_setpvn(PERL_DEBUG_PAD(1), "", 0); /* ext/re needs these */
11821 sv_setpvn(PERL_DEBUG_PAD(2), "", 0); /* even without DEBUGGING. */
11823 /* Clone the regex array */
11824 PL_regex_padav = newAV();
11826 I32 len = av_len((AV*)proto_perl->Iregex_padav);
11827 SV** regexen = AvARRAY((AV*)proto_perl->Iregex_padav);
11828 av_push(PL_regex_padav,
11829 sv_dup_inc(regexen[0],param));
11830 for(i = 1; i <= len; i++) {
11831 if(SvREPADTMP(regexen[i])) {
11832 av_push(PL_regex_padav, sv_dup_inc(regexen[i], param));
11834 av_push(PL_regex_padav,
11836 newSViv(PTR2IV(re_dup(INT2PTR(REGEXP *,
11837 SvIVX(regexen[i])), param)))
11842 PL_regex_pad = AvARRAY(PL_regex_padav);
11844 /* shortcuts to various I/O objects */
11845 PL_stdingv = gv_dup(proto_perl->Istdingv, param);
11846 PL_stderrgv = gv_dup(proto_perl->Istderrgv, param);
11847 PL_defgv = gv_dup(proto_perl->Idefgv, param);
11848 PL_argvgv = gv_dup(proto_perl->Iargvgv, param);
11849 PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv, param);
11850 PL_argvout_stack = av_dup_inc(proto_perl->Iargvout_stack, param);
11852 /* shortcuts to regexp stuff */
11853 PL_replgv = gv_dup(proto_perl->Ireplgv, param);
11855 /* shortcuts to misc objects */
11856 PL_errgv = gv_dup(proto_perl->Ierrgv, param);
11858 /* shortcuts to debugging objects */
11859 PL_DBgv = gv_dup(proto_perl->IDBgv, param);
11860 PL_DBline = gv_dup(proto_perl->IDBline, param);
11861 PL_DBsub = gv_dup(proto_perl->IDBsub, param);
11862 PL_DBsingle = sv_dup(proto_perl->IDBsingle, param);
11863 PL_DBtrace = sv_dup(proto_perl->IDBtrace, param);
11864 PL_DBsignal = sv_dup(proto_perl->IDBsignal, param);
11865 PL_DBassertion = sv_dup(proto_perl->IDBassertion, param);
11866 PL_lineary = av_dup(proto_perl->Ilineary, param);
11867 PL_dbargs = av_dup(proto_perl->Idbargs, param);
11869 /* symbol tables */
11870 PL_defstash = hv_dup_inc(proto_perl->Tdefstash, param);
11871 PL_curstash = hv_dup(proto_perl->Tcurstash, param);
11872 PL_debstash = hv_dup(proto_perl->Idebstash, param);
11873 PL_globalstash = hv_dup(proto_perl->Iglobalstash, param);
11874 PL_curstname = sv_dup_inc(proto_perl->Icurstname, param);
11876 PL_beginav = av_dup_inc(proto_perl->Ibeginav, param);
11877 PL_beginav_save = av_dup_inc(proto_perl->Ibeginav_save, param);
11878 PL_checkav_save = av_dup_inc(proto_perl->Icheckav_save, param);
11879 PL_endav = av_dup_inc(proto_perl->Iendav, param);
11880 PL_checkav = av_dup_inc(proto_perl->Icheckav, param);
11881 PL_initav = av_dup_inc(proto_perl->Iinitav, param);
11883 PL_sub_generation = proto_perl->Isub_generation;
11885 /* funky return mechanisms */
11886 PL_forkprocess = proto_perl->Iforkprocess;
11888 /* subprocess state */
11889 PL_fdpid = av_dup_inc(proto_perl->Ifdpid, param);
11891 /* internal state */
11892 PL_tainting = proto_perl->Itainting;
11893 PL_taint_warn = proto_perl->Itaint_warn;
11894 PL_maxo = proto_perl->Imaxo;
11895 if (proto_perl->Iop_mask)
11896 PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo);
11898 PL_op_mask = Nullch;
11899 /* PL_asserting = proto_perl->Iasserting; */
11901 /* current interpreter roots */
11902 PL_main_cv = cv_dup_inc(proto_perl->Imain_cv, param);
11903 PL_main_root = OpREFCNT_inc(proto_perl->Imain_root);
11904 PL_main_start = proto_perl->Imain_start;
11905 PL_eval_root = proto_perl->Ieval_root;
11906 PL_eval_start = proto_perl->Ieval_start;
11908 /* runtime control stuff */
11909 PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl);
11910 PL_copline = proto_perl->Icopline;
11912 PL_filemode = proto_perl->Ifilemode;
11913 PL_lastfd = proto_perl->Ilastfd;
11914 PL_oldname = proto_perl->Ioldname; /* XXX not quite right */
11917 PL_gensym = proto_perl->Igensym;
11918 PL_preambled = proto_perl->Ipreambled;
11919 PL_preambleav = av_dup_inc(proto_perl->Ipreambleav, param);
11920 PL_laststatval = proto_perl->Ilaststatval;
11921 PL_laststype = proto_perl->Ilaststype;
11922 PL_mess_sv = Nullsv;
11924 PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv, param);
11925 PL_ofmt = SAVEPV(proto_perl->Iofmt);
11927 /* interpreter atexit processing */
11928 PL_exitlistlen = proto_perl->Iexitlistlen;
11929 if (PL_exitlistlen) {
11930 New(0, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
11931 Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
11934 PL_exitlist = (PerlExitListEntry*)NULL;
11935 PL_modglobal = hv_dup_inc(proto_perl->Imodglobal, param);
11936 PL_custom_op_names = hv_dup_inc(proto_perl->Icustom_op_names,param);
11937 PL_custom_op_descs = hv_dup_inc(proto_perl->Icustom_op_descs,param);
11939 PL_profiledata = NULL;
11940 PL_rsfp = fp_dup(proto_perl->Irsfp, '<', param);
11941 /* PL_rsfp_filters entries have fake IoDIRP() */
11942 PL_rsfp_filters = av_dup_inc(proto_perl->Irsfp_filters, param);
11944 PL_compcv = cv_dup(proto_perl->Icompcv, param);
11946 PAD_CLONE_VARS(proto_perl, param);
11948 #ifdef HAVE_INTERP_INTERN
11949 sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern);
11952 /* more statics moved here */
11953 PL_generation = proto_perl->Igeneration;
11954 PL_DBcv = cv_dup(proto_perl->IDBcv, param);
11956 PL_in_clean_objs = proto_perl->Iin_clean_objs;
11957 PL_in_clean_all = proto_perl->Iin_clean_all;
11959 PL_uid = proto_perl->Iuid;
11960 PL_euid = proto_perl->Ieuid;
11961 PL_gid = proto_perl->Igid;
11962 PL_egid = proto_perl->Iegid;
11963 PL_nomemok = proto_perl->Inomemok;
11964 PL_an = proto_perl->Ian;
11965 PL_evalseq = proto_perl->Ievalseq;
11966 PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */
11967 PL_origalen = proto_perl->Iorigalen;
11968 PL_pidstatus = newHV(); /* XXX flag for cloning? */
11969 PL_osname = SAVEPV(proto_perl->Iosname);
11970 PL_sh_path_compat = proto_perl->Ish_path_compat; /* XXX never deallocated */
11971 PL_sighandlerp = proto_perl->Isighandlerp;
11974 PL_runops = proto_perl->Irunops;
11976 Copy(proto_perl->Itokenbuf, PL_tokenbuf, 256, char);
11979 PL_cshlen = proto_perl->Icshlen;
11980 PL_cshname = proto_perl->Icshname; /* XXX never deallocated */
11983 PL_lex_state = proto_perl->Ilex_state;
11984 PL_lex_defer = proto_perl->Ilex_defer;
11985 PL_lex_expect = proto_perl->Ilex_expect;
11986 PL_lex_formbrack = proto_perl->Ilex_formbrack;
11987 PL_lex_dojoin = proto_perl->Ilex_dojoin;
11988 PL_lex_starts = proto_perl->Ilex_starts;
11989 PL_lex_stuff = sv_dup_inc(proto_perl->Ilex_stuff, param);
11990 PL_lex_repl = sv_dup_inc(proto_perl->Ilex_repl, param);
11991 PL_lex_op = proto_perl->Ilex_op;
11992 PL_lex_inpat = proto_perl->Ilex_inpat;
11993 PL_lex_inwhat = proto_perl->Ilex_inwhat;
11994 PL_lex_brackets = proto_perl->Ilex_brackets;
11995 i = (PL_lex_brackets < 120 ? 120 : PL_lex_brackets);
11996 PL_lex_brackstack = SAVEPVN(proto_perl->Ilex_brackstack,i);
11997 PL_lex_casemods = proto_perl->Ilex_casemods;
11998 i = (PL_lex_casemods < 12 ? 12 : PL_lex_casemods);
11999 PL_lex_casestack = SAVEPVN(proto_perl->Ilex_casestack,i);
12001 Copy(proto_perl->Inextval, PL_nextval, 5, YYSTYPE);
12002 Copy(proto_perl->Inexttype, PL_nexttype, 5, I32);
12003 PL_nexttoke = proto_perl->Inexttoke;
12005 /* XXX This is probably masking the deeper issue of why
12006 * SvANY(proto_perl->Ilinestr) can be NULL at this point. For test case:
12007 * http://archive.develooper.com/perl5-porters%40perl.org/msg83298.html
12008 * (A little debugging with a watchpoint on it may help.)
12010 if (SvANY(proto_perl->Ilinestr)) {
12011 PL_linestr = sv_dup_inc(proto_perl->Ilinestr, param);
12012 i = proto_perl->Ibufptr - SvPVX(proto_perl->Ilinestr);
12013 PL_bufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
12014 i = proto_perl->Ioldbufptr - SvPVX(proto_perl->Ilinestr);
12015 PL_oldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
12016 i = proto_perl->Ioldoldbufptr - SvPVX(proto_perl->Ilinestr);
12017 PL_oldoldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
12018 i = proto_perl->Ilinestart - SvPVX(proto_perl->Ilinestr);
12019 PL_linestart = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
12022 PL_linestr = NEWSV(65,79);
12023 sv_upgrade(PL_linestr,SVt_PVIV);
12024 sv_setpvn(PL_linestr,"",0);
12025 PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart = SvPVX(PL_linestr);
12027 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
12028 PL_pending_ident = proto_perl->Ipending_ident;
12029 PL_sublex_info = proto_perl->Isublex_info; /* XXX not quite right */
12031 PL_expect = proto_perl->Iexpect;
12033 PL_multi_start = proto_perl->Imulti_start;
12034 PL_multi_end = proto_perl->Imulti_end;
12035 PL_multi_open = proto_perl->Imulti_open;
12036 PL_multi_close = proto_perl->Imulti_close;
12038 PL_error_count = proto_perl->Ierror_count;
12039 PL_subline = proto_perl->Isubline;
12040 PL_subname = sv_dup_inc(proto_perl->Isubname, param);
12042 /* XXX See comment on SvANY(proto_perl->Ilinestr) above */
12043 if (SvANY(proto_perl->Ilinestr)) {
12044 i = proto_perl->Ilast_uni - SvPVX(proto_perl->Ilinestr);
12045 PL_last_uni = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
12046 i = proto_perl->Ilast_lop - SvPVX(proto_perl->Ilinestr);
12047 PL_last_lop = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
12048 PL_last_lop_op = proto_perl->Ilast_lop_op;
12051 PL_last_uni = SvPVX(PL_linestr);
12052 PL_last_lop = SvPVX(PL_linestr);
12053 PL_last_lop_op = 0;
12055 PL_in_my = proto_perl->Iin_my;
12056 PL_in_my_stash = hv_dup(proto_perl->Iin_my_stash, param);
12058 PL_cryptseen = proto_perl->Icryptseen;
12061 PL_hints = proto_perl->Ihints;
12063 PL_amagic_generation = proto_perl->Iamagic_generation;
12065 #ifdef USE_LOCALE_COLLATE
12066 PL_collation_ix = proto_perl->Icollation_ix;
12067 PL_collation_name = SAVEPV(proto_perl->Icollation_name);
12068 PL_collation_standard = proto_perl->Icollation_standard;
12069 PL_collxfrm_base = proto_perl->Icollxfrm_base;
12070 PL_collxfrm_mult = proto_perl->Icollxfrm_mult;
12071 #endif /* USE_LOCALE_COLLATE */
12073 #ifdef USE_LOCALE_NUMERIC
12074 PL_numeric_name = SAVEPV(proto_perl->Inumeric_name);
12075 PL_numeric_standard = proto_perl->Inumeric_standard;
12076 PL_numeric_local = proto_perl->Inumeric_local;
12077 PL_numeric_radix_sv = sv_dup_inc(proto_perl->Inumeric_radix_sv, param);
12078 #endif /* !USE_LOCALE_NUMERIC */
12080 /* utf8 character classes */
12081 PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum, param);
12082 PL_utf8_alnumc = sv_dup_inc(proto_perl->Iutf8_alnumc, param);
12083 PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii, param);
12084 PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha, param);
12085 PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space, param);
12086 PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl, param);
12087 PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph, param);
12088 PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit, param);
12089 PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper, param);
12090 PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower, param);
12091 PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print, param);
12092 PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct, param);
12093 PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit, param);
12094 PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark, param);
12095 PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper, param);
12096 PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle, param);
12097 PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower, param);
12098 PL_utf8_tofold = sv_dup_inc(proto_perl->Iutf8_tofold, param);
12099 PL_utf8_idstart = sv_dup_inc(proto_perl->Iutf8_idstart, param);
12100 PL_utf8_idcont = sv_dup_inc(proto_perl->Iutf8_idcont, param);
12102 /* Did the locale setup indicate UTF-8? */
12103 PL_utf8locale = proto_perl->Iutf8locale;
12104 /* Unicode features (see perlrun/-C) */
12105 PL_unicode = proto_perl->Iunicode;
12107 /* Pre-5.8 signals control */
12108 PL_signals = proto_perl->Isignals;
12110 /* times() ticks per second */
12111 PL_clocktick = proto_perl->Iclocktick;
12113 /* Recursion stopper for PerlIO_find_layer */
12114 PL_in_load_module = proto_perl->Iin_load_module;
12116 /* sort() routine */
12117 PL_sort_RealCmp = proto_perl->Isort_RealCmp;
12119 /* Not really needed/useful since the reenrant_retint is "volatile",
12120 * but do it for consistency's sake. */
12121 PL_reentrant_retint = proto_perl->Ireentrant_retint;
12123 /* Hooks to shared SVs and locks. */
12124 PL_sharehook = proto_perl->Isharehook;
12125 PL_lockhook = proto_perl->Ilockhook;
12126 PL_unlockhook = proto_perl->Iunlockhook;
12127 PL_threadhook = proto_perl->Ithreadhook;
12129 PL_runops_std = proto_perl->Irunops_std;
12130 PL_runops_dbg = proto_perl->Irunops_dbg;
12132 #ifdef THREADS_HAVE_PIDS
12133 PL_ppid = proto_perl->Ippid;
12137 PL_last_swash_hv = Nullhv; /* reinits on demand */
12138 PL_last_swash_klen = 0;
12139 PL_last_swash_key[0]= '\0';
12140 PL_last_swash_tmps = (U8*)NULL;
12141 PL_last_swash_slen = 0;
12143 PL_glob_index = proto_perl->Iglob_index;
12144 PL_srand_called = proto_perl->Isrand_called;
12145 PL_hash_seed = proto_perl->Ihash_seed;
12146 PL_rehash_seed = proto_perl->Irehash_seed;
12147 PL_uudmap['M'] = 0; /* reinits on demand */
12148 PL_bitcount = Nullch; /* reinits on demand */
12150 if (proto_perl->Ipsig_pend) {
12151 Newz(0, PL_psig_pend, SIG_SIZE, int);
12154 PL_psig_pend = (int*)NULL;
12157 if (proto_perl->Ipsig_ptr) {
12158 Newz(0, PL_psig_ptr, SIG_SIZE, SV*);
12159 Newz(0, PL_psig_name, SIG_SIZE, SV*);
12160 for (i = 1; i < SIG_SIZE; i++) {
12161 PL_psig_ptr[i] = sv_dup_inc(proto_perl->Ipsig_ptr[i], param);
12162 PL_psig_name[i] = sv_dup_inc(proto_perl->Ipsig_name[i], param);
12166 PL_psig_ptr = (SV**)NULL;
12167 PL_psig_name = (SV**)NULL;
12170 /* thrdvar.h stuff */
12172 if (flags & CLONEf_COPY_STACKS) {
12173 /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */
12174 PL_tmps_ix = proto_perl->Ttmps_ix;
12175 PL_tmps_max = proto_perl->Ttmps_max;
12176 PL_tmps_floor = proto_perl->Ttmps_floor;
12177 Newz(50, PL_tmps_stack, PL_tmps_max, SV*);
12179 while (i <= PL_tmps_ix) {
12180 PL_tmps_stack[i] = sv_dup_inc(proto_perl->Ttmps_stack[i], param);
12184 /* next PUSHMARK() sets *(PL_markstack_ptr+1) */
12185 i = proto_perl->Tmarkstack_max - proto_perl->Tmarkstack;
12186 Newz(54, PL_markstack, i, I32);
12187 PL_markstack_max = PL_markstack + (proto_perl->Tmarkstack_max
12188 - proto_perl->Tmarkstack);
12189 PL_markstack_ptr = PL_markstack + (proto_perl->Tmarkstack_ptr
12190 - proto_perl->Tmarkstack);
12191 Copy(proto_perl->Tmarkstack, PL_markstack,
12192 PL_markstack_ptr - PL_markstack + 1, I32);
12194 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
12195 * NOTE: unlike the others! */
12196 PL_scopestack_ix = proto_perl->Tscopestack_ix;
12197 PL_scopestack_max = proto_perl->Tscopestack_max;
12198 Newz(54, PL_scopestack, PL_scopestack_max, I32);
12199 Copy(proto_perl->Tscopestack, PL_scopestack, PL_scopestack_ix, I32);
12201 /* NOTE: si_dup() looks at PL_markstack */
12202 PL_curstackinfo = si_dup(proto_perl->Tcurstackinfo, param);
12204 /* PL_curstack = PL_curstackinfo->si_stack; */
12205 PL_curstack = av_dup(proto_perl->Tcurstack, param);
12206 PL_mainstack = av_dup(proto_perl->Tmainstack, param);
12208 /* next PUSHs() etc. set *(PL_stack_sp+1) */
12209 PL_stack_base = AvARRAY(PL_curstack);
12210 PL_stack_sp = PL_stack_base + (proto_perl->Tstack_sp
12211 - proto_perl->Tstack_base);
12212 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
12214 /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix]
12215 * NOTE: unlike the others! */
12216 PL_savestack_ix = proto_perl->Tsavestack_ix;
12217 PL_savestack_max = proto_perl->Tsavestack_max;
12218 /*Newz(54, PL_savestack, PL_savestack_max, ANY);*/
12219 PL_savestack = ss_dup(proto_perl, param);
12223 ENTER; /* perl_destruct() wants to LEAVE; */
12226 PL_start_env = proto_perl->Tstart_env; /* XXXXXX */
12227 PL_top_env = &PL_start_env;
12229 PL_op = proto_perl->Top;
12232 PL_Xpv = (XPV*)NULL;
12233 PL_na = proto_perl->Tna;
12235 PL_statbuf = proto_perl->Tstatbuf;
12236 PL_statcache = proto_perl->Tstatcache;
12237 PL_statgv = gv_dup(proto_perl->Tstatgv, param);
12238 PL_statname = sv_dup_inc(proto_perl->Tstatname, param);
12240 PL_timesbuf = proto_perl->Ttimesbuf;
12243 PL_tainted = proto_perl->Ttainted;
12244 PL_curpm = proto_perl->Tcurpm; /* XXX No PMOP ref count */
12245 PL_rs = sv_dup_inc(proto_perl->Trs, param);
12246 PL_last_in_gv = gv_dup(proto_perl->Tlast_in_gv, param);
12247 PL_ofs_sv = sv_dup_inc(proto_perl->Tofs_sv, param);
12248 PL_defoutgv = gv_dup_inc(proto_perl->Tdefoutgv, param);
12249 PL_chopset = proto_perl->Tchopset; /* XXX never deallocated */
12250 PL_toptarget = sv_dup_inc(proto_perl->Ttoptarget, param);
12251 PL_bodytarget = sv_dup_inc(proto_perl->Tbodytarget, param);
12252 PL_formtarget = sv_dup(proto_perl->Tformtarget, param);
12254 PL_restartop = proto_perl->Trestartop;
12255 PL_in_eval = proto_perl->Tin_eval;
12256 PL_delaymagic = proto_perl->Tdelaymagic;
12257 PL_dirty = proto_perl->Tdirty;
12258 PL_localizing = proto_perl->Tlocalizing;
12260 PL_errors = sv_dup_inc(proto_perl->Terrors, param);
12261 PL_hv_fetch_ent_mh = Nullhe;
12262 PL_modcount = proto_perl->Tmodcount;
12263 PL_lastgotoprobe = Nullop;
12264 PL_dumpindent = proto_perl->Tdumpindent;
12266 PL_sortcop = (OP*)any_dup(proto_perl->Tsortcop, proto_perl);
12267 PL_sortstash = hv_dup(proto_perl->Tsortstash, param);
12268 PL_firstgv = gv_dup(proto_perl->Tfirstgv, param);
12269 PL_secondgv = gv_dup(proto_perl->Tsecondgv, param);
12270 PL_sortcxix = proto_perl->Tsortcxix;
12271 PL_efloatbuf = Nullch; /* reinits on demand */
12272 PL_efloatsize = 0; /* reinits on demand */
12276 PL_screamfirst = NULL;
12277 PL_screamnext = NULL;
12278 PL_maxscream = -1; /* reinits on demand */
12279 PL_lastscream = Nullsv;
12281 PL_watchaddr = NULL;
12282 PL_watchok = Nullch;
12284 PL_regdummy = proto_perl->Tregdummy;
12285 PL_regprecomp = Nullch;
12288 PL_colorset = 0; /* reinits PL_colors[] */
12289 /*PL_colors[6] = {0,0,0,0,0,0};*/
12290 PL_reginput = Nullch;
12291 PL_regbol = Nullch;
12292 PL_regeol = Nullch;
12293 PL_regstartp = (I32*)NULL;
12294 PL_regendp = (I32*)NULL;
12295 PL_reglastparen = (U32*)NULL;
12296 PL_reglastcloseparen = (U32*)NULL;
12297 PL_regtill = Nullch;
12298 PL_reg_start_tmp = (char**)NULL;
12299 PL_reg_start_tmpl = 0;
12300 PL_regdata = (struct reg_data*)NULL;
12303 PL_reg_eval_set = 0;
12305 PL_regprogram = (regnode*)NULL;
12307 PL_regcc = (CURCUR*)NULL;
12308 PL_reg_call_cc = (struct re_cc_state*)NULL;
12309 PL_reg_re = (regexp*)NULL;
12310 PL_reg_ganch = Nullch;
12311 PL_reg_sv = Nullsv;
12312 PL_reg_match_utf8 = FALSE;
12313 PL_reg_magic = (MAGIC*)NULL;
12315 PL_reg_oldcurpm = (PMOP*)NULL;
12316 PL_reg_curpm = (PMOP*)NULL;
12317 PL_reg_oldsaved = Nullch;
12318 PL_reg_oldsavedlen = 0;
12319 #ifdef PERL_COPY_ON_WRITE
12322 PL_reg_maxiter = 0;
12323 PL_reg_leftiter = 0;
12324 PL_reg_poscache = Nullch;
12325 PL_reg_poscache_size= 0;
12327 /* RE engine - function pointers */
12328 PL_regcompp = proto_perl->Tregcompp;
12329 PL_regexecp = proto_perl->Tregexecp;
12330 PL_regint_start = proto_perl->Tregint_start;
12331 PL_regint_string = proto_perl->Tregint_string;
12332 PL_regfree = proto_perl->Tregfree;
12334 PL_reginterp_cnt = 0;
12335 PL_reg_starttry = 0;
12337 /* Pluggable optimizer */
12338 PL_peepp = proto_perl->Tpeepp;
12340 PL_stashcache = newHV();
12342 if (!(flags & CLONEf_KEEP_PTR_TABLE)) {
12343 ptr_table_free(PL_ptr_table);
12344 PL_ptr_table = NULL;
12347 /* Call the ->CLONE method, if it exists, for each of the stashes
12348 identified by sv_dup() above.
12350 while(av_len(param->stashes) != -1) {
12351 HV* stash = (HV*) av_shift(param->stashes);
12352 GV* cloner = gv_fetchmethod_autoload(stash, "CLONE", 0);
12353 if (cloner && GvCV(cloner)) {
12358 XPUSHs(sv_2mortal(newSVpv(HvNAME(stash), 0)));
12360 call_sv((SV*)GvCV(cloner), G_DISCARD);
12366 SvREFCNT_dec(param->stashes);
12371 #endif /* USE_ITHREADS */
12374 =head1 Unicode Support
12376 =for apidoc sv_recode_to_utf8
12378 The encoding is assumed to be an Encode object, on entry the PV
12379 of the sv is assumed to be octets in that encoding, and the sv
12380 will be converted into Unicode (and UTF-8).
12382 If the sv already is UTF-8 (or if it is not POK), or if the encoding
12383 is not a reference, nothing is done to the sv. If the encoding is not
12384 an C<Encode::XS> Encoding object, bad things will happen.
12385 (See F<lib/encoding.pm> and L<Encode>).
12387 The PV of the sv is returned.
12392 Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding)
12395 if (SvPOK(sv) && !SvUTF8(sv) && !IN_BYTES && SvROK(encoding)) {
12409 Passing sv_yes is wrong - it needs to be or'ed set of constants
12410 for Encode::XS, while UTf-8 decode (currently) assumes a true value means
12411 remove converted chars from source.
12413 Both will default the value - let them.
12415 XPUSHs(&PL_sv_yes);
12418 call_method("decode", G_SCALAR);
12422 s = SvPV(uni, len);
12423 if (s != SvPVX(sv)) {
12424 SvGROW(sv, len + 1);
12425 Move(s, SvPVX(sv), len, char);
12426 SvCUR_set(sv, len);
12427 SvPVX(sv)[len] = 0;
12434 return SvPOKp(sv) ? SvPVX(sv) : NULL;
12438 =for apidoc sv_cat_decode
12440 The encoding is assumed to be an Encode object, the PV of the ssv is
12441 assumed to be octets in that encoding and decoding the input starts
12442 from the position which (PV + *offset) pointed to. The dsv will be
12443 concatenated the decoded UTF-8 string from ssv. Decoding will terminate
12444 when the string tstr appears in decoding output or the input ends on
12445 the PV of the ssv. The value which the offset points will be modified
12446 to the last input position on the ssv.
12448 Returns TRUE if the terminator was found, else returns FALSE.
12453 Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding,
12454 SV *ssv, int *offset, char *tstr, int tlen)
12458 if (SvPOK(ssv) && SvPOK(dsv) && SvROK(encoding) && offset) {
12469 XPUSHs(offsv = sv_2mortal(newSViv(*offset)));
12470 XPUSHs(sv_2mortal(newSVpvn(tstr, tlen)));
12472 call_method("cat_decode", G_SCALAR);
12474 ret = SvTRUE(TOPs);
12475 *offset = SvIV(offsv);
12481 Perl_croak(aTHX_ "Invalid argument to sv_cat_decode");
12487 * c-indentation-style: bsd
12488 * c-basic-offset: 4
12489 * indent-tabs-mode: t
12492 * vim: shiftwidth=4: