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 Safefree() */
353 New(704,chunk,PERL_ARENA_SIZE,char); /* in sv_free_arenas() */
354 sv_add_arena(chunk, PERL_ARENA_SIZE, 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)
679 if (!hv || SvMAGICAL(hv) || !HvARRAY(hv) ||
680 (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
685 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 bool pad = (obase->op_type == OP_PADAV || obase->op_type == OP_PADHV);
839 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 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 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 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 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 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 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)
5121 dstr = SvPV_force_flags(dsv, dlen, flags);
5122 SvGROW(dsv, dlen + slen + 1);
5125 Move(sstr, SvPVX(dsv) + dlen, slen, char);
5126 SvCUR_set(dsv, SvCUR(dsv) + slen);
5128 (void)SvPOK_only_UTF8(dsv); /* validate pointer */
5133 =for apidoc sv_catpvn_mg
5135 Like C<sv_catpvn>, but also handles 'set' magic.
5141 Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
5143 sv_catpvn(sv,ptr,len);
5147 /* sv_catsv() is now a macro using Perl_sv_catsv_flags();
5148 * this function provided for binary compatibility only
5152 Perl_sv_catsv(pTHX_ SV *dstr, register SV *sstr)
5154 sv_catsv_flags(dstr, sstr, SV_GMAGIC);
5158 =for apidoc sv_catsv
5160 Concatenates the string from SV C<ssv> onto the end of the string in
5161 SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but
5162 not 'set' magic. See C<sv_catsv_mg>.
5164 =for apidoc sv_catsv_flags
5166 Concatenates the string from SV C<ssv> onto the end of the string in
5167 SV C<dsv>. Modifies C<dsv> but not C<ssv>. If C<flags> has C<SV_GMAGIC>
5168 bit set, will C<mg_get> on the SVs if appropriate, else not. C<sv_catsv>
5169 and C<sv_catsv_nomg> are implemented in terms of this function.
5174 Perl_sv_catsv_flags(pTHX_ SV *dsv, register SV *ssv, I32 flags)
5180 if ((spv = SvPV(ssv, slen))) {
5181 /* sutf8 and dutf8 were type bool, but under USE_ITHREADS,
5182 gcc version 2.95.2 20000220 (Debian GNU/Linux) for
5183 Linux xxx 2.2.17 on sparc64 with gcc -O2, we erroneously
5184 get dutf8 = 0x20000000, (i.e. SVf_UTF8) even though
5185 dsv->sv_flags doesn't have that bit set.
5186 Andy Dougherty 12 Oct 2001
5188 I32 sutf8 = DO_UTF8(ssv);
5191 if (SvGMAGICAL(dsv) && (flags & SV_GMAGIC))
5193 dutf8 = DO_UTF8(dsv);
5195 if (dutf8 != sutf8) {
5197 /* Not modifying source SV, so taking a temporary copy. */
5198 SV* csv = sv_2mortal(newSVpvn(spv, slen));
5200 sv_utf8_upgrade(csv);
5201 spv = SvPV(csv, slen);
5204 sv_utf8_upgrade_nomg(dsv);
5206 sv_catpvn_nomg(dsv, spv, slen);
5211 =for apidoc sv_catsv_mg
5213 Like C<sv_catsv>, but also handles 'set' magic.
5219 Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv)
5226 =for apidoc sv_catpv
5228 Concatenates the string onto the end of the string which is in the SV.
5229 If the SV has the UTF-8 status set, then the bytes appended should be
5230 valid UTF-8. Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>.
5235 Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr)
5237 register STRLEN len;
5243 junk = SvPV_force(sv, tlen);
5245 SvGROW(sv, tlen + len + 1);
5248 Move(ptr,SvPVX(sv)+tlen,len+1,char);
5249 SvCUR_set(sv, SvCUR(sv) + len);
5250 (void)SvPOK_only_UTF8(sv); /* validate pointer */
5255 =for apidoc sv_catpv_mg
5257 Like C<sv_catpv>, but also handles 'set' magic.
5263 Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr)
5272 Create a new null SV, or if len > 0, create a new empty SVt_PV type SV
5273 with an initial PV allocation of len+1. Normally accessed via the C<NEWSV>
5280 Perl_newSV(pTHX_ STRLEN len)
5286 sv_upgrade(sv, SVt_PV);
5287 SvGROW(sv, len + 1);
5292 =for apidoc sv_magicext
5294 Adds magic to an SV, upgrading it if necessary. Applies the
5295 supplied vtable and returns a pointer to the magic added.
5297 Note that C<sv_magicext> will allow things that C<sv_magic> will not.
5298 In particular, you can add magic to SvREADONLY SVs, and add more than
5299 one instance of the same 'how'.
5301 If C<namlen> is greater than zero then a C<savepvn> I<copy> of C<name> is
5302 stored, if C<namlen> is zero then C<name> is stored as-is and - as another
5303 special case - if C<(name && namlen == HEf_SVKEY)> then C<name> is assumed
5304 to contain an C<SV*> and is stored as-is with its REFCNT incremented.
5306 (This is now used as a subroutine by C<sv_magic>.)
5311 Perl_sv_magicext(pTHX_ SV* sv, SV* obj, int how, const MGVTBL *vtable,
5312 const char* name, I32 namlen)
5316 if (SvTYPE(sv) < SVt_PVMG) {
5317 (void)SvUPGRADE(sv, SVt_PVMG);
5319 Newz(702,mg, 1, MAGIC);
5320 mg->mg_moremagic = SvMAGIC(sv);
5321 SvMAGIC_set(sv, mg);
5323 /* Sometimes a magic contains a reference loop, where the sv and
5324 object refer to each other. To prevent a reference loop that
5325 would prevent such objects being freed, we look for such loops
5326 and if we find one we avoid incrementing the object refcount.
5328 Note we cannot do this to avoid self-tie loops as intervening RV must
5329 have its REFCNT incremented to keep it in existence.
5332 if (!obj || obj == sv ||
5333 how == PERL_MAGIC_arylen ||
5334 how == PERL_MAGIC_qr ||
5335 (SvTYPE(obj) == SVt_PVGV &&
5336 (GvSV(obj) == sv || GvHV(obj) == (HV*)sv || GvAV(obj) == (AV*)sv ||
5337 GvCV(obj) == (CV*)sv || GvIOp(obj) == (IO*)sv ||
5338 GvFORM(obj) == (CV*)sv)))
5343 mg->mg_obj = SvREFCNT_inc(obj);
5344 mg->mg_flags |= MGf_REFCOUNTED;
5347 /* Normal self-ties simply pass a null object, and instead of
5348 using mg_obj directly, use the SvTIED_obj macro to produce a
5349 new RV as needed. For glob "self-ties", we are tieing the PVIO
5350 with an RV obj pointing to the glob containing the PVIO. In
5351 this case, to avoid a reference loop, we need to weaken the
5355 if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO &&
5356 obj && SvROK(obj) && GvIO(SvRV(obj)) == (IO*)sv)
5362 mg->mg_len = namlen;
5365 mg->mg_ptr = savepvn(name, namlen);
5366 else if (namlen == HEf_SVKEY)
5367 mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
5369 mg->mg_ptr = (char *) name;
5371 mg->mg_virtual = vtable;
5375 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
5380 =for apidoc sv_magic
5382 Adds magic to an SV. First upgrades C<sv> to type C<SVt_PVMG> if necessary,
5383 then adds a new magic item of type C<how> to the head of the magic list.
5385 See C<sv_magicext> (which C<sv_magic> now calls) for a description of the
5386 handling of the C<name> and C<namlen> arguments.
5388 You need to use C<sv_magicext> to add magic to SvREADONLY SVs and also
5389 to add more than one instance of the same 'how'.
5395 Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen)
5397 const MGVTBL *vtable = 0;
5400 #ifdef PERL_COPY_ON_WRITE
5402 sv_force_normal_flags(sv, 0);
5404 if (SvREADONLY(sv)) {
5406 && how != PERL_MAGIC_regex_global
5407 && how != PERL_MAGIC_bm
5408 && how != PERL_MAGIC_fm
5409 && how != PERL_MAGIC_sv
5410 && how != PERL_MAGIC_backref
5413 Perl_croak(aTHX_ PL_no_modify);
5416 if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) {
5417 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
5418 /* sv_magic() refuses to add a magic of the same 'how' as an
5421 if (how == PERL_MAGIC_taint)
5429 vtable = &PL_vtbl_sv;
5431 case PERL_MAGIC_overload:
5432 vtable = &PL_vtbl_amagic;
5434 case PERL_MAGIC_overload_elem:
5435 vtable = &PL_vtbl_amagicelem;
5437 case PERL_MAGIC_overload_table:
5438 vtable = &PL_vtbl_ovrld;
5441 vtable = &PL_vtbl_bm;
5443 case PERL_MAGIC_regdata:
5444 vtable = &PL_vtbl_regdata;
5446 case PERL_MAGIC_regdatum:
5447 vtable = &PL_vtbl_regdatum;
5449 case PERL_MAGIC_env:
5450 vtable = &PL_vtbl_env;
5453 vtable = &PL_vtbl_fm;
5455 case PERL_MAGIC_envelem:
5456 vtable = &PL_vtbl_envelem;
5458 case PERL_MAGIC_regex_global:
5459 vtable = &PL_vtbl_mglob;
5461 case PERL_MAGIC_isa:
5462 vtable = &PL_vtbl_isa;
5464 case PERL_MAGIC_isaelem:
5465 vtable = &PL_vtbl_isaelem;
5467 case PERL_MAGIC_nkeys:
5468 vtable = &PL_vtbl_nkeys;
5470 case PERL_MAGIC_dbfile:
5473 case PERL_MAGIC_dbline:
5474 vtable = &PL_vtbl_dbline;
5476 #ifdef USE_LOCALE_COLLATE
5477 case PERL_MAGIC_collxfrm:
5478 vtable = &PL_vtbl_collxfrm;
5480 #endif /* USE_LOCALE_COLLATE */
5481 case PERL_MAGIC_tied:
5482 vtable = &PL_vtbl_pack;
5484 case PERL_MAGIC_tiedelem:
5485 case PERL_MAGIC_tiedscalar:
5486 vtable = &PL_vtbl_packelem;
5489 vtable = &PL_vtbl_regexp;
5491 case PERL_MAGIC_sig:
5492 vtable = &PL_vtbl_sig;
5494 case PERL_MAGIC_sigelem:
5495 vtable = &PL_vtbl_sigelem;
5497 case PERL_MAGIC_taint:
5498 vtable = &PL_vtbl_taint;
5500 case PERL_MAGIC_uvar:
5501 vtable = &PL_vtbl_uvar;
5503 case PERL_MAGIC_vec:
5504 vtable = &PL_vtbl_vec;
5506 case PERL_MAGIC_vstring:
5509 case PERL_MAGIC_utf8:
5510 vtable = &PL_vtbl_utf8;
5512 case PERL_MAGIC_substr:
5513 vtable = &PL_vtbl_substr;
5515 case PERL_MAGIC_defelem:
5516 vtable = &PL_vtbl_defelem;
5518 case PERL_MAGIC_glob:
5519 vtable = &PL_vtbl_glob;
5521 case PERL_MAGIC_arylen:
5522 vtable = &PL_vtbl_arylen;
5524 case PERL_MAGIC_pos:
5525 vtable = &PL_vtbl_pos;
5527 case PERL_MAGIC_backref:
5528 vtable = &PL_vtbl_backref;
5530 case PERL_MAGIC_ext:
5531 /* Reserved for use by extensions not perl internals. */
5532 /* Useful for attaching extension internal data to perl vars. */
5533 /* Note that multiple extensions may clash if magical scalars */
5534 /* etc holding private data from one are passed to another. */
5537 Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how);
5540 /* Rest of work is done else where */
5541 mg = sv_magicext(sv,obj,how,(MGVTBL*)vtable,name,namlen);
5544 case PERL_MAGIC_taint:
5547 case PERL_MAGIC_ext:
5548 case PERL_MAGIC_dbfile:
5555 =for apidoc sv_unmagic
5557 Removes all magic of type C<type> from an SV.
5563 Perl_sv_unmagic(pTHX_ SV *sv, int type)
5567 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
5570 for (mg = *mgp; mg; mg = *mgp) {
5571 if (mg->mg_type == type) {
5572 const MGVTBL* const vtbl = mg->mg_virtual;
5573 *mgp = mg->mg_moremagic;
5574 if (vtbl && vtbl->svt_free)
5575 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
5576 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
5578 Safefree(mg->mg_ptr);
5579 else if (mg->mg_len == HEf_SVKEY)
5580 SvREFCNT_dec((SV*)mg->mg_ptr);
5581 else if (mg->mg_type == PERL_MAGIC_utf8 && mg->mg_ptr)
5582 Safefree(mg->mg_ptr);
5584 if (mg->mg_flags & MGf_REFCOUNTED)
5585 SvREFCNT_dec(mg->mg_obj);
5589 mgp = &mg->mg_moremagic;
5593 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
5600 =for apidoc sv_rvweaken
5602 Weaken a reference: set the C<SvWEAKREF> flag on this RV; give the
5603 referred-to SV C<PERL_MAGIC_backref> magic if it hasn't already; and
5604 push a back-reference to this RV onto the array of backreferences
5605 associated with that magic.
5611 Perl_sv_rvweaken(pTHX_ SV *sv)
5614 if (!SvOK(sv)) /* let undefs pass */
5617 Perl_croak(aTHX_ "Can't weaken a nonreference");
5618 else if (SvWEAKREF(sv)) {
5619 if (ckWARN(WARN_MISC))
5620 Perl_warner(aTHX_ packWARN(WARN_MISC), "Reference is already weak");
5624 sv_add_backref(tsv, sv);
5630 /* Give tsv backref magic if it hasn't already got it, then push a
5631 * back-reference to sv onto the array associated with the backref magic.
5635 S_sv_add_backref(pTHX_ SV *tsv, SV *sv)
5639 if (SvMAGICAL(tsv) && (mg = mg_find(tsv, PERL_MAGIC_backref)))
5640 av = (AV*)mg->mg_obj;
5643 sv_magic(tsv, (SV*)av, PERL_MAGIC_backref, NULL, 0);
5644 /* av now has a refcnt of 2, which avoids it getting freed
5645 * before us during global cleanup. The extra ref is removed
5646 * by magic_killbackrefs() when tsv is being freed */
5648 if (AvFILLp(av) >= AvMAX(av)) {
5650 SV **svp = AvARRAY(av);
5651 for (i = AvFILLp(av); i >= 0; i--)
5653 svp[i] = sv; /* reuse the slot */
5656 av_extend(av, AvFILLp(av)+1);
5658 AvARRAY(av)[++AvFILLp(av)] = sv; /* av_push() */
5661 /* delete a back-reference to ourselves from the backref magic associated
5662 * with the SV we point to.
5666 S_sv_del_backref(pTHX_ SV *sv)
5673 if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, PERL_MAGIC_backref)))
5674 Perl_croak(aTHX_ "panic: del_backref");
5675 av = (AV *)mg->mg_obj;
5677 for (i = AvFILLp(av); i >= 0; i--)
5678 if (svp[i] == sv) svp[i] = Nullsv;
5682 =for apidoc sv_insert
5684 Inserts a string at the specified offset/length within the SV. Similar to
5685 the Perl substr() function.
5691 Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, const char *little, STRLEN littlelen)
5695 register char *midend;
5696 register char *bigend;
5702 Perl_croak(aTHX_ "Can't modify non-existent substring");
5703 SvPV_force(bigstr, curlen);
5704 (void)SvPOK_only_UTF8(bigstr);
5705 if (offset + len > curlen) {
5706 SvGROW(bigstr, offset+len+1);
5707 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
5708 SvCUR_set(bigstr, offset+len);
5712 i = littlelen - len;
5713 if (i > 0) { /* string might grow */
5714 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
5715 mid = big + offset + len;
5716 midend = bigend = big + SvCUR(bigstr);
5719 while (midend > mid) /* shove everything down */
5720 *--bigend = *--midend;
5721 Move(little,big+offset,littlelen,char);
5722 SvCUR_set(bigstr, SvCUR(bigstr) + i);
5727 Move(little,SvPVX(bigstr)+offset,len,char);
5732 big = SvPVX(bigstr);
5735 bigend = big + SvCUR(bigstr);
5737 if (midend > bigend)
5738 Perl_croak(aTHX_ "panic: sv_insert");
5740 if (mid - big > bigend - midend) { /* faster to shorten from end */
5742 Move(little, mid, littlelen,char);
5745 i = bigend - midend;
5747 Move(midend, mid, i,char);
5751 SvCUR_set(bigstr, mid - big);
5754 else if ((i = mid - big)) { /* faster from front */
5755 midend -= littlelen;
5757 sv_chop(bigstr,midend-i);
5762 Move(little, mid, littlelen,char);
5764 else if (littlelen) {
5765 midend -= littlelen;
5766 sv_chop(bigstr,midend);
5767 Move(little,midend,littlelen,char);
5770 sv_chop(bigstr,midend);
5776 =for apidoc sv_replace
5778 Make the first argument a copy of the second, then delete the original.
5779 The target SV physically takes over ownership of the body of the source SV
5780 and inherits its flags; however, the target keeps any magic it owns,
5781 and any magic in the source is discarded.
5782 Note that this is a rather specialist SV copying operation; most of the
5783 time you'll want to use C<sv_setsv> or one of its many macro front-ends.
5789 Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv)
5791 U32 refcnt = SvREFCNT(sv);
5792 SV_CHECK_THINKFIRST_COW_DROP(sv);
5793 if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL))
5794 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "Reference miscount in sv_replace()");
5795 if (SvMAGICAL(sv)) {
5799 sv_upgrade(nsv, SVt_PVMG);
5800 SvMAGIC_set(nsv, SvMAGIC(sv));
5801 SvFLAGS(nsv) |= SvMAGICAL(sv);
5803 SvMAGIC_set(sv, NULL);
5807 assert(!SvREFCNT(sv));
5808 #ifdef DEBUG_LEAKING_SCALARS
5809 sv->sv_flags = nsv->sv_flags;
5810 sv->sv_any = nsv->sv_any;
5811 sv->sv_refcnt = nsv->sv_refcnt;
5813 StructCopy(nsv,sv,SV);
5816 #ifdef PERL_COPY_ON_WRITE
5817 if (SvIsCOW_normal(nsv)) {
5818 /* We need to follow the pointers around the loop to make the
5819 previous SV point to sv, rather than nsv. */
5822 while ((next = SV_COW_NEXT_SV(current)) != nsv) {
5825 assert(SvPVX(current) == SvPVX(nsv));
5827 /* Make the SV before us point to the SV after us. */
5829 PerlIO_printf(Perl_debug_log, "previous is\n");
5831 PerlIO_printf(Perl_debug_log,
5832 "move it from 0x%"UVxf" to 0x%"UVxf"\n",
5833 (UV) SV_COW_NEXT_SV(current), (UV) sv);
5835 SV_COW_NEXT_SV_SET(current, sv);
5838 SvREFCNT(sv) = refcnt;
5839 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
5845 =for apidoc sv_clear
5847 Clear an SV: call any destructors, free up any memory used by the body,
5848 and free the body itself. The SV's head is I<not> freed, although
5849 its type is set to all 1's so that it won't inadvertently be assumed
5850 to be live during global destruction etc.
5851 This function should only be called when REFCNT is zero. Most of the time
5852 you'll want to call C<sv_free()> (or its macro wrapper C<SvREFCNT_dec>)
5859 Perl_sv_clear(pTHX_ register SV *sv)
5864 assert(SvREFCNT(sv) == 0);
5867 if (PL_defstash) { /* Still have a symbol table? */
5874 stash = SvSTASH(sv);
5875 destructor = StashHANDLER(stash,DESTROY);
5877 SV* tmpref = newRV(sv);
5878 SvREADONLY_on(tmpref); /* DESTROY() could be naughty */
5880 PUSHSTACKi(PERLSI_DESTROY);
5885 call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR|G_VOID);
5891 if(SvREFCNT(tmpref) < 2) {
5892 /* tmpref is not kept alive! */
5894 SvRV_set(tmpref, NULL);
5897 SvREFCNT_dec(tmpref);
5899 } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
5903 if (PL_in_clean_objs)
5904 Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'",
5906 /* DESTROY gave object new lease on life */
5912 SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */
5913 SvOBJECT_off(sv); /* Curse the object. */
5914 if (SvTYPE(sv) != SVt_PVIO)
5915 --PL_sv_objcount; /* XXX Might want something more general */
5918 if (SvTYPE(sv) >= SVt_PVMG) {
5921 if (SvFLAGS(sv) & SVpad_TYPED)
5922 SvREFCNT_dec(SvSTASH(sv));
5925 switch (SvTYPE(sv)) {
5928 IoIFP(sv) != PerlIO_stdin() &&
5929 IoIFP(sv) != PerlIO_stdout() &&
5930 IoIFP(sv) != PerlIO_stderr())
5932 io_close((IO*)sv, FALSE);
5934 if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP))
5935 PerlDir_close(IoDIRP(sv));
5936 IoDIRP(sv) = (DIR*)NULL;
5937 Safefree(IoTOP_NAME(sv));
5938 Safefree(IoFMT_NAME(sv));
5939 Safefree(IoBOTTOM_NAME(sv));
5954 if (LvTYPE(sv) == 'T') { /* for tie: return HE to pool */
5955 SvREFCNT_dec(HeKEY_sv((HE*)LvTARG(sv)));
5956 HeNEXT((HE*)LvTARG(sv)) = PL_hv_fetch_ent_mh;
5957 PL_hv_fetch_ent_mh = (HE*)LvTARG(sv);
5959 else if (LvTYPE(sv) != 't') /* unless tie: unrefcnted fake SV** */
5960 SvREFCNT_dec(LvTARG(sv));
5964 Safefree(GvNAME(sv));
5965 /* cannot decrease stash refcount yet, as we might recursively delete
5966 ourselves when the refcnt drops to zero. Delay SvREFCNT_dec
5967 of stash until current sv is completely gone.
5968 -- JohnPC, 27 Mar 1998 */
5969 stash = GvSTASH(sv);
5975 /* Don't bother with SvOOK_off(sv); as we're only going to free it. */
5977 SvPV_set(sv, SvPVX(sv) - SvIVX(sv));
5978 /* Don't even bother with turning off the OOK flag. */
5987 SvREFCNT_dec(SvRV(sv));
5989 #ifdef PERL_COPY_ON_WRITE
5990 else if (SvPVX(sv)) {
5992 /* I believe I need to grab the global SV mutex here and
5993 then recheck the COW status. */
5995 PerlIO_printf(Perl_debug_log, "Copy on write: clear\n");
5998 sv_release_COW(sv, SvPVX(sv), SvCUR(sv), SvLEN(sv),
5999 SvUVX(sv), SV_COW_NEXT_SV(sv));
6000 /* And drop it here. */
6002 } else if (SvLEN(sv)) {
6003 Safefree(SvPVX(sv));
6007 else if (SvPVX(sv) && SvLEN(sv))
6008 Safefree(SvPVX(sv));
6009 else if (SvPVX(sv) && SvREADONLY(sv) && SvFAKE(sv)) {
6010 unsharepvn(SvPVX(sv),
6011 SvUTF8(sv) ? -(I32)SvCUR(sv) : SvCUR(sv),
6025 switch (SvTYPE(sv)) {
6041 del_XPVIV(SvANY(sv));
6044 del_XPVNV(SvANY(sv));
6047 del_XPVMG(SvANY(sv));
6050 del_XPVLV(SvANY(sv));
6053 del_XPVAV(SvANY(sv));
6056 del_XPVHV(SvANY(sv));
6059 del_XPVCV(SvANY(sv));
6062 del_XPVGV(SvANY(sv));
6063 /* code duplication for increased performance. */
6064 SvFLAGS(sv) &= SVf_BREAK;
6065 SvFLAGS(sv) |= SVTYPEMASK;
6066 /* decrease refcount of the stash that owns this GV, if any */
6068 SvREFCNT_dec(stash);
6069 return; /* not break, SvFLAGS reset already happened */
6071 del_XPVBM(SvANY(sv));
6074 del_XPVFM(SvANY(sv));
6077 del_XPVIO(SvANY(sv));
6080 SvFLAGS(sv) &= SVf_BREAK;
6081 SvFLAGS(sv) |= SVTYPEMASK;
6085 =for apidoc sv_newref
6087 Increment an SV's reference count. Use the C<SvREFCNT_inc()> wrapper
6094 Perl_sv_newref(pTHX_ SV *sv)
6104 Decrement an SV's reference count, and if it drops to zero, call
6105 C<sv_clear> to invoke destructors and free up any memory used by
6106 the body; finally, deallocate the SV's head itself.
6107 Normally called via a wrapper macro C<SvREFCNT_dec>.
6113 Perl_sv_free(pTHX_ SV *sv)
6118 if (SvREFCNT(sv) == 0) {
6119 if (SvFLAGS(sv) & SVf_BREAK)
6120 /* this SV's refcnt has been artificially decremented to
6121 * trigger cleanup */
6123 if (PL_in_clean_all) /* All is fair */
6125 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
6126 /* make sure SvREFCNT(sv)==0 happens very seldom */
6127 SvREFCNT(sv) = (~(U32)0)/2;
6130 if (ckWARN_d(WARN_INTERNAL))
6131 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
6132 "Attempt to free unreferenced scalar: SV 0x%"UVxf
6133 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
6136 if (--(SvREFCNT(sv)) > 0)
6138 Perl_sv_free2(aTHX_ sv);
6142 Perl_sv_free2(pTHX_ SV *sv)
6147 if (ckWARN_d(WARN_DEBUGGING))
6148 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
6149 "Attempt to free temp prematurely: SV 0x%"UVxf
6150 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
6154 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
6155 /* make sure SvREFCNT(sv)==0 happens very seldom */
6156 SvREFCNT(sv) = (~(U32)0)/2;
6167 Returns the length of the string in the SV. Handles magic and type
6168 coercion. See also C<SvCUR>, which gives raw access to the xpv_cur slot.
6174 Perl_sv_len(pTHX_ register SV *sv)
6182 len = mg_length(sv);
6184 (void)SvPV(sv, len);
6189 =for apidoc sv_len_utf8
6191 Returns the number of characters in the string in an SV, counting wide
6192 UTF-8 bytes as a single character. Handles magic and type coercion.
6198 * The length is cached in PERL_UTF8_magic, in the mg_len field. Also the
6199 * mg_ptr is used, by sv_pos_u2b(), see the comments of S_utf8_mg_pos_init().
6200 * (Note that the mg_len is not the length of the mg_ptr field.)
6205 Perl_sv_len_utf8(pTHX_ register SV *sv)
6211 return mg_length(sv);
6215 U8 *s = (U8*)SvPV(sv, len);
6216 MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : 0;
6218 if (mg && mg->mg_len != -1 && (mg->mg_len > 0 || len == 0)) {
6220 #ifdef PERL_UTF8_CACHE_ASSERT
6221 assert(ulen == Perl_utf8_length(aTHX_ s, s + len));
6225 ulen = Perl_utf8_length(aTHX_ s, s + len);
6226 if (!mg && !SvREADONLY(sv)) {
6227 sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0);
6228 mg = mg_find(sv, PERL_MAGIC_utf8);
6238 /* S_utf8_mg_pos_init() is used to initialize the mg_ptr field of
6239 * a PERL_UTF8_magic. The mg_ptr is used to store the mapping
6240 * between UTF-8 and byte offsets. There are two (substr offset and substr
6241 * length, the i offset, PERL_MAGIC_UTF8_CACHESIZE) times two (UTF-8 offset
6242 * and byte offset) cache positions.
6244 * The mg_len field is used by sv_len_utf8(), see its comments.
6245 * Note that the mg_len is not the length of the mg_ptr field.
6249 S_utf8_mg_pos_init(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i, I32 *offsetp, U8 *s, U8 *start)
6253 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
6255 *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, (MGVTBL*)&PL_vtbl_utf8, 0, 0);
6259 *cachep = (STRLEN *) (*mgp)->mg_ptr;
6261 Newz(0, *cachep, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
6262 (*mgp)->mg_ptr = (char *) *cachep;
6266 (*cachep)[i] = *offsetp;
6267 (*cachep)[i+1] = s - start;
6275 * S_utf8_mg_pos() is used to query and update mg_ptr field of
6276 * a PERL_UTF8_magic. The mg_ptr is used to store the mapping
6277 * between UTF-8 and byte offsets. See also the comments of
6278 * S_utf8_mg_pos_init().
6282 S_utf8_mg_pos(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i, I32 *offsetp, I32 uoff, U8 **sp, U8 *start, U8 *send)
6286 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
6288 *mgp = mg_find(sv, PERL_MAGIC_utf8);
6289 if (*mgp && (*mgp)->mg_ptr) {
6290 *cachep = (STRLEN *) (*mgp)->mg_ptr;
6291 ASSERT_UTF8_CACHE(*cachep);
6292 if ((*cachep)[i] == (STRLEN)uoff) /* An exact match. */
6294 else { /* We will skip to the right spot. */
6299 /* The assumption is that going backward is half
6300 * the speed of going forward (that's where the
6301 * 2 * backw in the below comes from). (The real
6302 * figure of course depends on the UTF-8 data.) */
6304 if ((*cachep)[i] > (STRLEN)uoff) {
6306 backw = (*cachep)[i] - (STRLEN)uoff;
6308 if (forw < 2 * backw)
6311 p = start + (*cachep)[i+1];
6313 /* Try this only for the substr offset (i == 0),
6314 * not for the substr length (i == 2). */
6315 else if (i == 0) { /* (*cachep)[i] < uoff */
6316 STRLEN ulen = sv_len_utf8(sv);
6318 if ((STRLEN)uoff < ulen) {
6319 forw = (STRLEN)uoff - (*cachep)[i];
6320 backw = ulen - (STRLEN)uoff;
6322 if (forw < 2 * backw)
6323 p = start + (*cachep)[i+1];
6328 /* If the string is not long enough for uoff,
6329 * we could extend it, but not at this low a level. */
6333 if (forw < 2 * backw) {
6340 while (UTF8_IS_CONTINUATION(*p))
6345 /* Update the cache. */
6346 (*cachep)[i] = (STRLEN)uoff;
6347 (*cachep)[i+1] = p - start;
6349 /* Drop the stale "length" cache */
6358 if (found) { /* Setup the return values. */
6359 *offsetp = (*cachep)[i+1];
6360 *sp = start + *offsetp;
6363 *offsetp = send - start;
6365 else if (*sp < start) {
6371 #ifdef PERL_UTF8_CACHE_ASSERT
6376 while (n-- && s < send)
6380 assert(*offsetp == s - start);
6381 assert((*cachep)[0] == (STRLEN)uoff);
6382 assert((*cachep)[1] == *offsetp);
6384 ASSERT_UTF8_CACHE(*cachep);
6393 =for apidoc sv_pos_u2b
6395 Converts the value pointed to by offsetp from a count of UTF-8 chars from
6396 the start of the string, to a count of the equivalent number of bytes; if
6397 lenp is non-zero, it does the same to lenp, but this time starting from
6398 the offset, rather than from the start of the string. Handles magic and
6405 * sv_pos_u2b() uses, like sv_pos_b2u(), the mg_ptr of the potential
6406 * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and
6407 * byte offsets. See also the comments of S_utf8_mg_pos().
6412 Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp)
6423 start = s = (U8*)SvPV(sv, len);
6425 I32 uoffset = *offsetp;
6430 if (utf8_mg_pos(sv, &mg, &cache, 0, offsetp, *offsetp, &s, start, send))
6432 if (!found && uoffset > 0) {
6433 while (s < send && uoffset--)
6437 if (utf8_mg_pos_init(sv, &mg, &cache, 0, offsetp, s, start))
6439 *offsetp = s - start;
6444 if (utf8_mg_pos(sv, &mg, &cache, 2, lenp, *lenp, &s, start, send)) {
6448 if (!found && *lenp > 0) {
6451 while (s < send && ulen--)
6455 utf8_mg_pos_init(sv, &mg, &cache, 2, lenp, s, start);
6459 ASSERT_UTF8_CACHE(cache);
6471 =for apidoc sv_pos_b2u
6473 Converts the value pointed to by offsetp from a count of bytes from the
6474 start of the string, to a count of the equivalent number of UTF-8 chars.
6475 Handles magic and type coercion.
6481 * sv_pos_b2u() uses, like sv_pos_u2b(), the mg_ptr of the potential
6482 * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and
6483 * byte offsets. See also the comments of S_utf8_mg_pos().
6488 Perl_sv_pos_b2u(pTHX_ register SV* sv, I32* offsetp)
6496 s = (U8*)SvPV(sv, len);
6497 if ((I32)len < *offsetp)
6498 Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset");
6500 U8* send = s + *offsetp;
6502 STRLEN *cache = NULL;
6506 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
6507 mg = mg_find(sv, PERL_MAGIC_utf8);
6508 if (mg && mg->mg_ptr) {
6509 cache = (STRLEN *) mg->mg_ptr;
6510 if (cache[1] == (STRLEN)*offsetp) {
6511 /* An exact match. */
6512 *offsetp = cache[0];
6516 else if (cache[1] < (STRLEN)*offsetp) {
6517 /* We already know part of the way. */
6520 /* Let the below loop do the rest. */
6522 else { /* cache[1] > *offsetp */
6523 /* We already know all of the way, now we may
6524 * be able to walk back. The same assumption
6525 * is made as in S_utf8_mg_pos(), namely that
6526 * walking backward is twice slower than
6527 * walking forward. */
6528 STRLEN forw = *offsetp;
6529 STRLEN backw = cache[1] - *offsetp;
6531 if (!(forw < 2 * backw)) {
6532 U8 *p = s + cache[1];
6539 while (UTF8_IS_CONTINUATION(*p)) {
6547 *offsetp = cache[0];
6549 /* Drop the stale "length" cache */
6557 ASSERT_UTF8_CACHE(cache);
6563 /* Call utf8n_to_uvchr() to validate the sequence
6564 * (unless a simple non-UTF character) */
6565 if (!UTF8_IS_INVARIANT(*s))
6566 utf8n_to_uvchr(s, UTF8SKIP(s), &n, 0);
6575 if (!SvREADONLY(sv)) {
6577 sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0);
6578 mg = mg_find(sv, PERL_MAGIC_utf8);
6583 Newz(0, cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
6584 mg->mg_ptr = (char *) cache;
6589 cache[1] = *offsetp;
6590 /* Drop the stale "length" cache */
6603 Returns a boolean indicating whether the strings in the two SVs are
6604 identical. Is UTF-8 and 'use bytes' aware, handles get magic, and will
6605 coerce its args to strings if necessary.
6611 Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2)
6619 SV* svrecode = Nullsv;
6626 pv1 = SvPV(sv1, cur1);
6633 pv2 = SvPV(sv2, cur2);
6635 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
6636 /* Differing utf8ness.
6637 * Do not UTF8size the comparands as a side-effect. */
6640 svrecode = newSVpvn(pv2, cur2);
6641 sv_recode_to_utf8(svrecode, PL_encoding);
6642 pv2 = SvPV(svrecode, cur2);
6645 svrecode = newSVpvn(pv1, cur1);
6646 sv_recode_to_utf8(svrecode, PL_encoding);
6647 pv1 = SvPV(svrecode, cur1);
6649 /* Now both are in UTF-8. */
6651 SvREFCNT_dec(svrecode);
6656 bool is_utf8 = TRUE;
6659 /* sv1 is the UTF-8 one,
6660 * if is equal it must be downgrade-able */
6661 char *pv = (char*)bytes_from_utf8((const U8*)pv1,
6667 /* sv2 is the UTF-8 one,
6668 * if is equal it must be downgrade-able */
6669 char *pv = (char *)bytes_from_utf8((const U8*)pv2,
6675 /* Downgrade not possible - cannot be eq */
6683 eq = (pv1 == pv2) || memEQ(pv1, pv2, cur1);
6686 SvREFCNT_dec(svrecode);
6697 Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
6698 string in C<sv1> is less than, equal to, or greater than the string in
6699 C<sv2>. Is UTF-8 and 'use bytes' aware, handles get magic, and will
6700 coerce its args to strings if necessary. See also C<sv_cmp_locale>.
6706 Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2)
6709 const char *pv1, *pv2;
6712 SV *svrecode = Nullsv;
6719 pv1 = SvPV(sv1, cur1);
6726 pv2 = SvPV(sv2, cur2);
6728 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
6729 /* Differing utf8ness.
6730 * Do not UTF8size the comparands as a side-effect. */
6733 svrecode = newSVpvn(pv2, cur2);
6734 sv_recode_to_utf8(svrecode, PL_encoding);
6735 pv2 = SvPV(svrecode, cur2);
6738 pv2 = tpv = (char*)bytes_to_utf8((const U8*)pv2, &cur2);
6743 svrecode = newSVpvn(pv1, cur1);
6744 sv_recode_to_utf8(svrecode, PL_encoding);
6745 pv1 = SvPV(svrecode, cur1);
6748 pv1 = tpv = (char*)bytes_to_utf8((const U8*)pv1, &cur1);
6754 cmp = cur2 ? -1 : 0;
6758 const I32 retval = memcmp((const void*)pv1, (const void*)pv2, cur1 < cur2 ? cur1 : cur2);
6761 cmp = retval < 0 ? -1 : 1;
6762 } else if (cur1 == cur2) {
6765 cmp = cur1 < cur2 ? -1 : 1;
6770 SvREFCNT_dec(svrecode);
6779 =for apidoc sv_cmp_locale
6781 Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
6782 'use bytes' aware, handles get magic, and will coerce its args to strings
6783 if necessary. See also C<sv_cmp_locale>. See also C<sv_cmp>.
6789 Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2)
6791 #ifdef USE_LOCALE_COLLATE
6797 if (PL_collation_standard)
6801 pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL;
6803 pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL;
6805 if (!pv1 || !len1) {
6816 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
6819 return retval < 0 ? -1 : 1;
6822 * When the result of collation is equality, that doesn't mean
6823 * that there are no differences -- some locales exclude some
6824 * characters from consideration. So to avoid false equalities,
6825 * we use the raw string as a tiebreaker.
6831 #endif /* USE_LOCALE_COLLATE */
6833 return sv_cmp(sv1, sv2);
6837 #ifdef USE_LOCALE_COLLATE
6840 =for apidoc sv_collxfrm
6842 Add Collate Transform magic to an SV if it doesn't already have it.
6844 Any scalar variable may carry PERL_MAGIC_collxfrm magic that contains the
6845 scalar data of the variable, but transformed to such a format that a normal
6846 memory comparison can be used to compare the data according to the locale
6853 Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp)
6857 mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL;
6858 if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
6863 Safefree(mg->mg_ptr);
6865 if ((xf = mem_collxfrm(s, len, &xlen))) {
6866 if (SvREADONLY(sv)) {
6869 return xf + sizeof(PL_collation_ix);
6872 sv_magic(sv, 0, PERL_MAGIC_collxfrm, 0, 0);
6873 mg = mg_find(sv, PERL_MAGIC_collxfrm);
6886 if (mg && mg->mg_ptr) {
6888 return mg->mg_ptr + sizeof(PL_collation_ix);
6896 #endif /* USE_LOCALE_COLLATE */
6901 Get a line from the filehandle and store it into the SV, optionally
6902 appending to the currently-stored string.
6908 Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append)
6912 register STDCHAR rslast;
6913 register STDCHAR *bp;
6919 if (SvTHINKFIRST(sv))
6920 sv_force_normal_flags(sv, append ? 0 : SV_COW_DROP_PV);
6921 /* XXX. If you make this PVIV, then copy on write can copy scalars read
6923 However, perlbench says it's slower, because the existing swipe code
6924 is faster than copy on write.
6925 Swings and roundabouts. */
6926 (void)SvUPGRADE(sv, SVt_PV);
6931 if (PerlIO_isutf8(fp)) {
6933 sv_utf8_upgrade_nomg(sv);
6934 sv_pos_u2b(sv,&append,0);
6936 } else if (SvUTF8(sv)) {
6937 SV *tsv = NEWSV(0,0);
6938 sv_gets(tsv, fp, 0);
6939 sv_utf8_upgrade_nomg(tsv);
6940 SvCUR_set(sv,append);
6943 goto return_string_or_null;
6948 if (PerlIO_isutf8(fp))
6951 if (IN_PERL_COMPILETIME) {
6952 /* we always read code in line mode */
6956 else if (RsSNARF(PL_rs)) {
6957 /* If it is a regular disk file use size from stat() as estimate
6958 of amount we are going to read - may result in malloc-ing
6959 more memory than we realy need if layers bellow reduce
6960 size we read (e.g. CRLF or a gzip layer)
6963 if (!PerlLIO_fstat(PerlIO_fileno(fp), &st) && S_ISREG(st.st_mode)) {
6964 Off_t offset = PerlIO_tell(fp);
6965 if (offset != (Off_t) -1 && st.st_size + append > offset) {
6966 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1));
6972 else if (RsRECORD(PL_rs)) {
6976 /* Grab the size of the record we're getting */
6977 recsize = SvIV(SvRV(PL_rs));
6978 buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append;
6981 /* VMS wants read instead of fread, because fread doesn't respect */
6982 /* RMS record boundaries. This is not necessarily a good thing to be */
6983 /* doing, but we've got no other real choice - except avoid stdio
6984 as implementation - perhaps write a :vms layer ?
6986 bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize);
6988 bytesread = PerlIO_read(fp, buffer, recsize);
6992 SvCUR_set(sv, bytesread += append);
6993 buffer[bytesread] = '\0';
6994 goto return_string_or_null;
6996 else if (RsPARA(PL_rs)) {
7002 /* Get $/ i.e. PL_rs into same encoding as stream wants */
7003 if (PerlIO_isutf8(fp)) {
7004 rsptr = SvPVutf8(PL_rs, rslen);
7007 if (SvUTF8(PL_rs)) {
7008 if (!sv_utf8_downgrade(PL_rs, TRUE)) {
7009 Perl_croak(aTHX_ "Wide character in $/");
7012 rsptr = SvPV(PL_rs, rslen);
7016 rslast = rslen ? rsptr[rslen - 1] : '\0';
7018 if (rspara) { /* have to do this both before and after */
7019 do { /* to make sure file boundaries work right */
7022 i = PerlIO_getc(fp);
7026 PerlIO_ungetc(fp,i);
7032 /* See if we know enough about I/O mechanism to cheat it ! */
7034 /* This used to be #ifdef test - it is made run-time test for ease
7035 of abstracting out stdio interface. One call should be cheap
7036 enough here - and may even be a macro allowing compile
7040 if (PerlIO_fast_gets(fp)) {
7043 * We're going to steal some values from the stdio struct
7044 * and put EVERYTHING in the innermost loop into registers.
7046 register STDCHAR *ptr;
7050 #if defined(VMS) && defined(PERLIO_IS_STDIO)
7051 /* An ungetc()d char is handled separately from the regular
7052 * buffer, so we getc() it back out and stuff it in the buffer.
7054 i = PerlIO_getc(fp);
7055 if (i == EOF) return 0;
7056 *(--((*fp)->_ptr)) = (unsigned char) i;
7060 /* Here is some breathtakingly efficient cheating */
7062 cnt = PerlIO_get_cnt(fp); /* get count into register */
7063 /* make sure we have the room */
7064 if ((I32)(SvLEN(sv) - append) <= cnt + 1) {
7065 /* Not room for all of it
7066 if we are looking for a separator and room for some
7068 if (rslen && cnt > 80 && (I32)SvLEN(sv) > append) {
7069 /* just process what we have room for */
7070 shortbuffered = cnt - SvLEN(sv) + append + 1;
7071 cnt -= shortbuffered;
7075 /* remember that cnt can be negative */
7076 SvGROW(sv, (STRLEN)(append + (cnt <= 0 ? 2 : (cnt + 1))));
7081 bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */
7082 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
7083 DEBUG_P(PerlIO_printf(Perl_debug_log,
7084 "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
7085 DEBUG_P(PerlIO_printf(Perl_debug_log,
7086 "Screamer: entering: PerlIO * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
7087 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
7088 PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
7093 while (cnt > 0) { /* this | eat */
7095 if ((*bp++ = *ptr++) == rslast) /* really | dust */
7096 goto thats_all_folks; /* screams | sed :-) */
7100 Copy(ptr, bp, cnt, char); /* this | eat */
7101 bp += cnt; /* screams | dust */
7102 ptr += cnt; /* louder | sed :-) */
7107 if (shortbuffered) { /* oh well, must extend */
7108 cnt = shortbuffered;
7110 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
7112 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
7113 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
7117 DEBUG_P(PerlIO_printf(Perl_debug_log,
7118 "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n",
7119 PTR2UV(ptr),(long)cnt));
7120 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* deregisterize cnt and ptr */
7122 DEBUG_P(PerlIO_printf(Perl_debug_log,
7123 "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
7124 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
7125 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
7127 /* This used to call 'filbuf' in stdio form, but as that behaves like
7128 getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
7129 another abstraction. */
7130 i = PerlIO_getc(fp); /* get more characters */
7132 DEBUG_P(PerlIO_printf(Perl_debug_log,
7133 "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
7134 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
7135 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
7137 cnt = PerlIO_get_cnt(fp);
7138 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
7139 DEBUG_P(PerlIO_printf(Perl_debug_log,
7140 "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
7142 if (i == EOF) /* all done for ever? */
7143 goto thats_really_all_folks;
7145 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
7147 SvGROW(sv, bpx + cnt + 2);
7148 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
7150 *bp++ = (STDCHAR)i; /* store character from PerlIO_getc */
7152 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
7153 goto thats_all_folks;
7157 if ((rslen > 1 && (STRLEN)(bp - (STDCHAR*)SvPVX(sv)) < rslen) ||
7158 memNE((char*)bp - rslen, rsptr, rslen))
7159 goto screamer; /* go back to the fray */
7160 thats_really_all_folks:
7162 cnt += shortbuffered;
7163 DEBUG_P(PerlIO_printf(Perl_debug_log,
7164 "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
7165 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* put these back or we're in trouble */
7166 DEBUG_P(PerlIO_printf(Perl_debug_log,
7167 "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
7168 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
7169 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
7171 SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */
7172 DEBUG_P(PerlIO_printf(Perl_debug_log,
7173 "Screamer: done, len=%ld, string=|%.*s|\n",
7174 (long)SvCUR(sv),(int)SvCUR(sv),SvPVX(sv)));
7178 /*The big, slow, and stupid way. */
7179 #ifdef USE_HEAP_INSTEAD_OF_STACK /* Even slower way. */
7181 New(0, buf, 8192, STDCHAR);
7189 const register STDCHAR *bpe = buf + sizeof(buf);
7191 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = (STDCHAR)i) != rslast && bp < bpe)
7192 ; /* keep reading */
7196 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
7197 /* Accomodate broken VAXC compiler, which applies U8 cast to
7198 * both args of ?: operator, causing EOF to change into 255
7201 i = (U8)buf[cnt - 1];
7207 cnt = 0; /* we do need to re-set the sv even when cnt <= 0 */
7209 sv_catpvn(sv, (char *) buf, cnt);
7211 sv_setpvn(sv, (char *) buf, cnt);
7213 if (i != EOF && /* joy */
7215 SvCUR(sv) < rslen ||
7216 memNE(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
7220 * If we're reading from a TTY and we get a short read,
7221 * indicating that the user hit his EOF character, we need
7222 * to notice it now, because if we try to read from the TTY
7223 * again, the EOF condition will disappear.
7225 * The comparison of cnt to sizeof(buf) is an optimization
7226 * that prevents unnecessary calls to feof().
7230 if (!(cnt < sizeof(buf) && PerlIO_eof(fp)))
7234 #ifdef USE_HEAP_INSTEAD_OF_STACK
7239 if (rspara) { /* have to do this both before and after */
7240 while (i != EOF) { /* to make sure file boundaries work right */
7241 i = PerlIO_getc(fp);
7243 PerlIO_ungetc(fp,i);
7249 return_string_or_null:
7250 return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch;
7256 Auto-increment of the value in the SV, doing string to numeric conversion
7257 if necessary. Handles 'get' magic.
7263 Perl_sv_inc(pTHX_ register SV *sv)
7272 if (SvTHINKFIRST(sv)) {
7274 sv_force_normal_flags(sv, 0);
7275 if (SvREADONLY(sv)) {
7276 if (IN_PERL_RUNTIME)
7277 Perl_croak(aTHX_ PL_no_modify);
7281 if (SvAMAGIC(sv) && AMG_CALLun(sv,inc))
7283 i = PTR2IV(SvRV(sv));
7288 flags = SvFLAGS(sv);
7289 if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) {
7290 /* It's (privately or publicly) a float, but not tested as an
7291 integer, so test it to see. */
7293 flags = SvFLAGS(sv);
7295 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
7296 /* It's publicly an integer, or privately an integer-not-float */
7297 #ifdef PERL_PRESERVE_IVUV
7301 if (SvUVX(sv) == UV_MAX)
7302 sv_setnv(sv, UV_MAX_P1);
7304 (void)SvIOK_only_UV(sv);
7305 SvUV_set(sv, SvUVX(sv) + 1);
7307 if (SvIVX(sv) == IV_MAX)
7308 sv_setuv(sv, (UV)IV_MAX + 1);
7310 (void)SvIOK_only(sv);
7311 SvIV_set(sv, SvIVX(sv) + 1);
7316 if (flags & SVp_NOK) {
7317 (void)SvNOK_only(sv);
7318 SvNV_set(sv, SvNVX(sv) + 1.0);
7322 if (!(flags & SVp_POK) || !*SvPVX(sv)) {
7323 if ((flags & SVTYPEMASK) < SVt_PVIV)
7324 sv_upgrade(sv, SVt_IV);
7325 (void)SvIOK_only(sv);
7330 while (isALPHA(*d)) d++;
7331 while (isDIGIT(*d)) d++;
7333 #ifdef PERL_PRESERVE_IVUV
7334 /* Got to punt this as an integer if needs be, but we don't issue
7335 warnings. Probably ought to make the sv_iv_please() that does
7336 the conversion if possible, and silently. */
7337 int numtype = grok_number(SvPVX(sv), SvCUR(sv), NULL);
7338 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
7339 /* Need to try really hard to see if it's an integer.
7340 9.22337203685478e+18 is an integer.
7341 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
7342 so $a="9.22337203685478e+18"; $a+0; $a++
7343 needs to be the same as $a="9.22337203685478e+18"; $a++
7350 /* sv_2iv *should* have made this an NV */
7351 if (flags & SVp_NOK) {
7352 (void)SvNOK_only(sv);
7353 SvNV_set(sv, SvNVX(sv) + 1.0);
7356 /* I don't think we can get here. Maybe I should assert this
7357 And if we do get here I suspect that sv_setnv will croak. NWC
7359 #if defined(USE_LONG_DOUBLE)
7360 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",
7361 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
7363 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
7364 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
7367 #endif /* PERL_PRESERVE_IVUV */
7368 sv_setnv(sv,Atof(SvPVX(sv)) + 1.0);
7372 while (d >= SvPVX(sv)) {
7380 /* MKS: The original code here died if letters weren't consecutive.
7381 * at least it didn't have to worry about non-C locales. The
7382 * new code assumes that ('z'-'a')==('Z'-'A'), letters are
7383 * arranged in order (although not consecutively) and that only
7384 * [A-Za-z] are accepted by isALPHA in the C locale.
7386 if (*d != 'z' && *d != 'Z') {
7387 do { ++*d; } while (!isALPHA(*d));
7390 *(d--) -= 'z' - 'a';
7395 *(d--) -= 'z' - 'a' + 1;
7399 /* oh,oh, the number grew */
7400 SvGROW(sv, SvCUR(sv) + 2);
7401 SvCUR_set(sv, SvCUR(sv) + 1);
7402 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--)
7413 Auto-decrement of the value in the SV, doing string to numeric conversion
7414 if necessary. Handles 'get' magic.
7420 Perl_sv_dec(pTHX_ register SV *sv)
7428 if (SvTHINKFIRST(sv)) {
7430 sv_force_normal_flags(sv, 0);
7431 if (SvREADONLY(sv)) {
7432 if (IN_PERL_RUNTIME)
7433 Perl_croak(aTHX_ PL_no_modify);
7437 if (SvAMAGIC(sv) && AMG_CALLun(sv,dec))
7439 i = PTR2IV(SvRV(sv));
7444 /* Unlike sv_inc we don't have to worry about string-never-numbers
7445 and keeping them magic. But we mustn't warn on punting */
7446 flags = SvFLAGS(sv);
7447 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
7448 /* It's publicly an integer, or privately an integer-not-float */
7449 #ifdef PERL_PRESERVE_IVUV
7453 if (SvUVX(sv) == 0) {
7454 (void)SvIOK_only(sv);
7458 (void)SvIOK_only_UV(sv);
7459 SvUV_set(sv, SvUVX(sv) + 1);
7462 if (SvIVX(sv) == IV_MIN)
7463 sv_setnv(sv, (NV)IV_MIN - 1.0);
7465 (void)SvIOK_only(sv);
7466 SvIV_set(sv, SvIVX(sv) - 1);
7471 if (flags & SVp_NOK) {
7472 SvNV_set(sv, SvNVX(sv) - 1.0);
7473 (void)SvNOK_only(sv);
7476 if (!(flags & SVp_POK)) {
7477 if ((flags & SVTYPEMASK) < SVt_PVNV)
7478 sv_upgrade(sv, SVt_NV);
7480 (void)SvNOK_only(sv);
7483 #ifdef PERL_PRESERVE_IVUV
7485 int numtype = grok_number(SvPVX(sv), SvCUR(sv), NULL);
7486 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
7487 /* Need to try really hard to see if it's an integer.
7488 9.22337203685478e+18 is an integer.
7489 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
7490 so $a="9.22337203685478e+18"; $a+0; $a--
7491 needs to be the same as $a="9.22337203685478e+18"; $a--
7498 /* sv_2iv *should* have made this an NV */
7499 if (flags & SVp_NOK) {
7500 (void)SvNOK_only(sv);
7501 SvNV_set(sv, SvNVX(sv) - 1.0);
7504 /* I don't think we can get here. Maybe I should assert this
7505 And if we do get here I suspect that sv_setnv will croak. NWC
7507 #if defined(USE_LONG_DOUBLE)
7508 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",
7509 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
7511 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
7512 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
7516 #endif /* PERL_PRESERVE_IVUV */
7517 sv_setnv(sv,Atof(SvPVX(sv)) - 1.0); /* punt */
7521 =for apidoc sv_mortalcopy
7523 Creates a new SV which is a copy of the original SV (using C<sv_setsv>).
7524 The new SV is marked as mortal. It will be destroyed "soon", either by an
7525 explicit call to FREETMPS, or by an implicit call at places such as
7526 statement boundaries. See also C<sv_newmortal> and C<sv_2mortal>.
7531 /* Make a string that will exist for the duration of the expression
7532 * evaluation. Actually, it may have to last longer than that, but
7533 * hopefully we won't free it until it has been assigned to a
7534 * permanent location. */
7537 Perl_sv_mortalcopy(pTHX_ SV *oldstr)
7542 sv_setsv(sv,oldstr);
7544 PL_tmps_stack[++PL_tmps_ix] = sv;
7550 =for apidoc sv_newmortal
7552 Creates a new null SV which is mortal. The reference count of the SV is
7553 set to 1. It will be destroyed "soon", either by an explicit call to
7554 FREETMPS, or by an implicit call at places such as statement boundaries.
7555 See also C<sv_mortalcopy> and C<sv_2mortal>.
7561 Perl_sv_newmortal(pTHX)
7566 SvFLAGS(sv) = SVs_TEMP;
7568 PL_tmps_stack[++PL_tmps_ix] = sv;
7573 =for apidoc sv_2mortal
7575 Marks an existing SV as mortal. The SV will be destroyed "soon", either
7576 by an explicit call to FREETMPS, or by an implicit call at places such as
7577 statement boundaries. SvTEMP() is turned on which means that the SV's
7578 string buffer can be "stolen" if this SV is copied. See also C<sv_newmortal>
7579 and C<sv_mortalcopy>.
7585 Perl_sv_2mortal(pTHX_ register SV *sv)
7590 if (SvREADONLY(sv) && SvIMMORTAL(sv))
7593 PL_tmps_stack[++PL_tmps_ix] = sv;
7601 Creates a new SV and copies a string into it. The reference count for the
7602 SV is set to 1. If C<len> is zero, Perl will compute the length using
7603 strlen(). For efficiency, consider using C<newSVpvn> instead.
7609 Perl_newSVpv(pTHX_ const char *s, STRLEN len)
7616 sv_setpvn(sv,s,len);
7621 =for apidoc newSVpvn
7623 Creates a new SV and copies a string into it. The reference count for the
7624 SV is set to 1. Note that if C<len> is zero, Perl will create a zero length
7625 string. You are responsible for ensuring that the source string is at least
7626 C<len> bytes long. If the C<s> argument is NULL the new SV will be undefined.
7632 Perl_newSVpvn(pTHX_ const char *s, STRLEN len)
7637 sv_setpvn(sv,s,len);
7642 =for apidoc newSVpvn_share
7644 Creates a new SV with its SvPVX pointing to a shared string in the string
7645 table. If the string does not already exist in the table, it is created
7646 first. Turns on READONLY and FAKE. The string's hash is stored in the UV
7647 slot of the SV; if the C<hash> parameter is non-zero, that value is used;
7648 otherwise the hash is computed. The idea here is that as the string table
7649 is used for shared hash keys these strings will have SvPVX == HeKEY and
7650 hash lookup will avoid string compare.
7656 Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
7659 bool is_utf8 = FALSE;
7661 STRLEN tmplen = -len;
7663 /* See the note in hv.c:hv_fetch() --jhi */
7664 src = (char*)bytes_from_utf8((const U8*)src, &tmplen, &is_utf8);
7668 PERL_HASH(hash, src, len);
7670 sv_upgrade(sv, SVt_PVIV);
7671 SvPV_set(sv, sharepvn(src, is_utf8?-len:len, hash));
7684 #if defined(PERL_IMPLICIT_CONTEXT)
7686 /* pTHX_ magic can't cope with varargs, so this is a no-context
7687 * version of the main function, (which may itself be aliased to us).
7688 * Don't access this version directly.
7692 Perl_newSVpvf_nocontext(const char* pat, ...)
7697 va_start(args, pat);
7698 sv = vnewSVpvf(pat, &args);
7705 =for apidoc newSVpvf
7707 Creates a new SV and initializes it with the string formatted like
7714 Perl_newSVpvf(pTHX_ const char* pat, ...)
7718 va_start(args, pat);
7719 sv = vnewSVpvf(pat, &args);
7724 /* backend for newSVpvf() and newSVpvf_nocontext() */
7727 Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args)
7731 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
7738 Creates a new SV and copies a floating point value into it.
7739 The reference count for the SV is set to 1.
7745 Perl_newSVnv(pTHX_ NV n)
7757 Creates a new SV and copies an integer into it. The reference count for the
7764 Perl_newSViv(pTHX_ IV i)
7776 Creates a new SV and copies an unsigned integer into it.
7777 The reference count for the SV is set to 1.
7783 Perl_newSVuv(pTHX_ UV u)
7793 =for apidoc newRV_noinc
7795 Creates an RV wrapper for an SV. The reference count for the original
7796 SV is B<not> incremented.
7802 Perl_newRV_noinc(pTHX_ SV *tmpRef)
7807 sv_upgrade(sv, SVt_RV);
7809 SvRV_set(sv, tmpRef);
7814 /* newRV_inc is the official function name to use now.
7815 * newRV_inc is in fact #defined to newRV in sv.h
7819 Perl_newRV(pTHX_ SV *tmpRef)
7821 return newRV_noinc(SvREFCNT_inc(tmpRef));
7827 Creates a new SV which is an exact duplicate of the original SV.
7834 Perl_newSVsv(pTHX_ register SV *old)
7840 if (SvTYPE(old) == SVTYPEMASK) {
7841 if (ckWARN_d(WARN_INTERNAL))
7842 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "semi-panic: attempt to dup freed string");
7846 /* SV_GMAGIC is the default for sv_setv()
7847 SV_NOSTEAL prevents TEMP buffers being, well, stolen, and saves games
7848 with SvTEMP_off and SvTEMP_on round a call to sv_setsv. */
7849 sv_setsv_flags(sv, old, SV_GMAGIC | SV_NOSTEAL);
7854 =for apidoc sv_reset
7856 Underlying implementation for the C<reset> Perl function.
7857 Note that the perl-level function is vaguely deprecated.
7863 Perl_sv_reset(pTHX_ register const char *s, HV *stash)
7872 char todo[PERL_UCHAR_MAX+1];
7877 if (!*s) { /* reset ?? searches */
7878 for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) {
7879 pm->op_pmdynflags &= ~PMdf_USED;
7884 /* reset variables */
7886 if (!HvARRAY(stash))
7889 Zero(todo, 256, char);
7891 i = (unsigned char)*s;
7895 max = (unsigned char)*s++;
7896 for ( ; i <= max; i++) {
7899 for (i = 0; i <= (I32) HvMAX(stash); i++) {
7900 for (entry = HvARRAY(stash)[i];
7902 entry = HeNEXT(entry))
7904 if (!todo[(U8)*HeKEY(entry)])
7906 gv = (GV*)HeVAL(entry);
7908 if (SvTHINKFIRST(sv)) {
7909 if (!SvREADONLY(sv) && SvROK(sv))
7914 if (SvTYPE(sv) >= SVt_PV) {
7916 if (SvPVX(sv) != Nullch)
7923 if (GvHV(gv) && !HvNAME(GvHV(gv))) {
7926 #ifdef USE_ENVIRON_ARRAY
7928 # ifdef USE_ITHREADS
7929 && PL_curinterp == aTHX
7933 environ[0] = Nullch;
7936 #endif /* !PERL_MICRO */
7946 Using various gambits, try to get an IO from an SV: the IO slot if its a
7947 GV; or the recursive result if we're an RV; or the IO slot of the symbol
7948 named after the PV if we're a string.
7954 Perl_sv_2io(pTHX_ SV *sv)
7959 switch (SvTYPE(sv)) {
7967 Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv));
7971 Perl_croak(aTHX_ PL_no_usym, "filehandle");
7973 return sv_2io(SvRV(sv));
7974 gv = gv_fetchsv(sv, FALSE, SVt_PVIO);
7980 Perl_croak(aTHX_ "Bad filehandle: %"SVf, sv);
7989 Using various gambits, try to get a CV from an SV; in addition, try if
7990 possible to set C<*st> and C<*gvp> to the stash and GV associated with it.
7996 Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref)
8003 return *gvp = Nullgv, Nullcv;
8004 switch (SvTYPE(sv)) {
8023 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
8024 tryAMAGICunDEREF(to_cv);
8027 if (SvTYPE(sv) == SVt_PVCV) {
8036 Perl_croak(aTHX_ "Not a subroutine reference");
8041 gv = gv_fetchsv(sv, lref, SVt_PVCV);
8047 if (lref && !GvCVu(gv)) {
8050 tmpsv = NEWSV(704,0);
8051 gv_efullname3(tmpsv, gv, Nullch);
8052 /* XXX this is probably not what they think they're getting.
8053 * It has the same effect as "sub name;", i.e. just a forward
8055 newSUB(start_subparse(FALSE, 0),
8056 newSVOP(OP_CONST, 0, tmpsv),
8061 Perl_croak(aTHX_ "Unable to create sub named \"%"SVf"\"",
8071 Returns true if the SV has a true value by Perl's rules.
8072 Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may
8073 instead use an in-line version.
8079 Perl_sv_true(pTHX_ register SV *sv)
8084 const register XPV* tXpv;
8085 if ((tXpv = (XPV*)SvANY(sv)) &&
8086 (tXpv->xpv_cur > 1 ||
8087 (tXpv->xpv_cur && *tXpv->xpv_pv != '0')))
8094 return SvIVX(sv) != 0;
8097 return SvNVX(sv) != 0.0;
8099 return sv_2bool(sv);
8107 A private implementation of the C<SvIVx> macro for compilers which can't
8108 cope with complex macro expressions. Always use the macro instead.
8114 Perl_sv_iv(pTHX_ register SV *sv)
8118 return (IV)SvUVX(sv);
8127 A private implementation of the C<SvUVx> macro for compilers which can't
8128 cope with complex macro expressions. Always use the macro instead.
8134 Perl_sv_uv(pTHX_ register SV *sv)
8139 return (UV)SvIVX(sv);
8147 A private implementation of the C<SvNVx> macro for compilers which can't
8148 cope with complex macro expressions. Always use the macro instead.
8154 Perl_sv_nv(pTHX_ register SV *sv)
8161 /* sv_pv() is now a macro using SvPV_nolen();
8162 * this function provided for binary compatibility only
8166 Perl_sv_pv(pTHX_ SV *sv)
8173 return sv_2pv(sv, &n_a);
8179 Use the C<SvPV_nolen> macro instead
8183 A private implementation of the C<SvPV> macro for compilers which can't
8184 cope with complex macro expressions. Always use the macro instead.
8190 Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp)
8196 return sv_2pv(sv, lp);
8201 Perl_sv_pvn_nomg(pTHX_ register SV *sv, STRLEN *lp)
8207 return sv_2pv_flags(sv, lp, 0);
8210 /* sv_pvn_force() is now a macro using Perl_sv_pvn_force_flags();
8211 * this function provided for binary compatibility only
8215 Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp)
8217 return sv_pvn_force_flags(sv, lp, SV_GMAGIC);
8221 =for apidoc sv_pvn_force
8223 Get a sensible string out of the SV somehow.
8224 A private implementation of the C<SvPV_force> macro for compilers which
8225 can't cope with complex macro expressions. Always use the macro instead.
8227 =for apidoc sv_pvn_force_flags
8229 Get a sensible string out of the SV somehow.
8230 If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<sv> if
8231 appropriate, else not. C<sv_pvn_force> and C<sv_pvn_force_nomg> are
8232 implemented in terms of this function.
8233 You normally want to use the various wrapper macros instead: see
8234 C<SvPV_force> and C<SvPV_force_nomg>
8240 Perl_sv_pvn_force_flags(pTHX_ SV *sv, STRLEN *lp, I32 flags)
8244 if (SvTHINKFIRST(sv) && !SvROK(sv))
8245 sv_force_normal_flags(sv, 0);
8251 if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) {
8252 Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
8256 s = sv_2pv_flags(sv, lp, flags);
8257 if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */
8262 (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */
8263 SvGROW(sv, len + 1);
8264 Move(s,SvPVX(sv),len,char);
8269 SvPOK_on(sv); /* validate pointer */
8271 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
8272 PTR2UV(sv),SvPVX(sv)));
8278 /* sv_pvbyte () is now a macro using Perl_sv_2pv_flags();
8279 * this function provided for binary compatibility only
8283 Perl_sv_pvbyte(pTHX_ SV *sv)
8285 sv_utf8_downgrade(sv,0);
8290 =for apidoc sv_pvbyte
8292 Use C<SvPVbyte_nolen> instead.
8294 =for apidoc sv_pvbyten
8296 A private implementation of the C<SvPVbyte> macro for compilers
8297 which can't cope with complex macro expressions. Always use the macro
8304 Perl_sv_pvbyten(pTHX_ SV *sv, STRLEN *lp)
8306 sv_utf8_downgrade(sv,0);
8307 return sv_pvn(sv,lp);
8311 =for apidoc sv_pvbyten_force
8313 A private implementation of the C<SvPVbytex_force> macro for compilers
8314 which can't cope with complex macro expressions. Always use the macro
8321 Perl_sv_pvbyten_force(pTHX_ SV *sv, STRLEN *lp)
8323 sv_pvn_force(sv,lp);
8324 sv_utf8_downgrade(sv,0);
8329 /* sv_pvutf8 () is now a macro using Perl_sv_2pv_flags();
8330 * this function provided for binary compatibility only
8334 Perl_sv_pvutf8(pTHX_ SV *sv)
8336 sv_utf8_upgrade(sv);
8341 =for apidoc sv_pvutf8
8343 Use the C<SvPVutf8_nolen> macro instead
8345 =for apidoc sv_pvutf8n
8347 A private implementation of the C<SvPVutf8> macro for compilers
8348 which can't cope with complex macro expressions. Always use the macro
8355 Perl_sv_pvutf8n(pTHX_ SV *sv, STRLEN *lp)
8357 sv_utf8_upgrade(sv);
8358 return sv_pvn(sv,lp);
8362 =for apidoc sv_pvutf8n_force
8364 A private implementation of the C<SvPVutf8_force> macro for compilers
8365 which can't cope with complex macro expressions. Always use the macro
8372 Perl_sv_pvutf8n_force(pTHX_ SV *sv, STRLEN *lp)
8374 sv_pvn_force(sv,lp);
8375 sv_utf8_upgrade(sv);
8381 =for apidoc sv_reftype
8383 Returns a string describing what the SV is a reference to.
8389 Perl_sv_reftype(pTHX_ const SV *sv, int ob)
8391 /* The fact that I don't need to downcast to char * everywhere, only in ?:
8392 inside return suggests a const propagation bug in g++. */
8393 if (ob && SvOBJECT(sv)) {
8394 char *name = HvNAME(SvSTASH(sv));
8395 return name ? name : (char *) "__ANON__";
8398 switch (SvTYPE(sv)) {
8415 case SVt_PVLV: return (char *) (SvROK(sv) ? "REF"
8416 /* tied lvalues should appear to be
8417 * scalars for backwards compatitbility */
8418 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
8419 ? "SCALAR" : "LVALUE");
8420 case SVt_PVAV: return "ARRAY";
8421 case SVt_PVHV: return "HASH";
8422 case SVt_PVCV: return "CODE";
8423 case SVt_PVGV: return "GLOB";
8424 case SVt_PVFM: return "FORMAT";
8425 case SVt_PVIO: return "IO";
8426 default: return "UNKNOWN";
8432 =for apidoc sv_isobject
8434 Returns a boolean indicating whether the SV is an RV pointing to a blessed
8435 object. If the SV is not an RV, or if the object is not blessed, then this
8442 Perl_sv_isobject(pTHX_ SV *sv)
8459 Returns a boolean indicating whether the SV is blessed into the specified
8460 class. This does not check for subtypes; use C<sv_derived_from> to verify
8461 an inheritance relationship.
8467 Perl_sv_isa(pTHX_ SV *sv, const char *name)
8478 if (!HvNAME(SvSTASH(sv)))
8481 return strEQ(HvNAME(SvSTASH(sv)), name);
8487 Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then
8488 it will be upgraded to one. If C<classname> is non-null then the new SV will
8489 be blessed in the specified package. The new SV is returned and its
8490 reference count is 1.
8496 Perl_newSVrv(pTHX_ SV *rv, const char *classname)
8502 SV_CHECK_THINKFIRST_COW_DROP(rv);
8505 if (SvTYPE(rv) >= SVt_PVMG) {
8506 U32 refcnt = SvREFCNT(rv);
8510 SvREFCNT(rv) = refcnt;
8513 if (SvTYPE(rv) < SVt_RV)
8514 sv_upgrade(rv, SVt_RV);
8515 else if (SvTYPE(rv) > SVt_RV) {
8526 HV* stash = gv_stashpv(classname, TRUE);
8527 (void)sv_bless(rv, stash);
8533 =for apidoc sv_setref_pv
8535 Copies a pointer into a new SV, optionally blessing the SV. The C<rv>
8536 argument will be upgraded to an RV. That RV will be modified to point to
8537 the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed
8538 into the SV. The C<classname> argument indicates the package for the
8539 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
8540 will have a reference count of 1, and the RV will be returned.
8542 Do not use with other Perl types such as HV, AV, SV, CV, because those
8543 objects will become corrupted by the pointer copy process.
8545 Note that C<sv_setref_pvn> copies the string while this copies the pointer.
8551 Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv)
8554 sv_setsv(rv, &PL_sv_undef);
8558 sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
8563 =for apidoc sv_setref_iv
8565 Copies an integer into a new SV, optionally blessing the SV. The C<rv>
8566 argument will be upgraded to an RV. That RV will be modified to point to
8567 the new SV. The C<classname> argument indicates the package for the
8568 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
8569 will have a reference count of 1, and the RV will be returned.
8575 Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv)
8577 sv_setiv(newSVrv(rv,classname), iv);
8582 =for apidoc sv_setref_uv
8584 Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv>
8585 argument will be upgraded to an RV. That RV will be modified to point to
8586 the new SV. The C<classname> argument indicates the package for the
8587 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
8588 will have a reference count of 1, and the RV will be returned.
8594 Perl_sv_setref_uv(pTHX_ SV *rv, const char *classname, UV uv)
8596 sv_setuv(newSVrv(rv,classname), uv);
8601 =for apidoc sv_setref_nv
8603 Copies a double into a new SV, optionally blessing the SV. The C<rv>
8604 argument will be upgraded to an RV. That RV will be modified to point to
8605 the new SV. The C<classname> argument indicates the package for the
8606 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
8607 will have a reference count of 1, and the RV will be returned.
8613 Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv)
8615 sv_setnv(newSVrv(rv,classname), nv);
8620 =for apidoc sv_setref_pvn
8622 Copies a string into a new SV, optionally blessing the SV. The length of the
8623 string must be specified with C<n>. The C<rv> argument will be upgraded to
8624 an RV. That RV will be modified to point to the new SV. The C<classname>
8625 argument indicates the package for the blessing. Set C<classname> to
8626 C<Nullch> to avoid the blessing. The new SV will have a reference count
8627 of 1, and the RV will be returned.
8629 Note that C<sv_setref_pv> copies the pointer while this copies the string.
8635 Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, char *pv, STRLEN n)
8637 sv_setpvn(newSVrv(rv,classname), pv, n);
8642 =for apidoc sv_bless
8644 Blesses an SV into a specified package. The SV must be an RV. The package
8645 must be designated by its stash (see C<gv_stashpv()>). The reference count
8646 of the SV is unaffected.
8652 Perl_sv_bless(pTHX_ SV *sv, HV *stash)
8656 Perl_croak(aTHX_ "Can't bless non-reference value");
8658 if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) {
8659 if (SvREADONLY(tmpRef))
8660 Perl_croak(aTHX_ PL_no_modify);
8661 if (SvOBJECT(tmpRef)) {
8662 if (SvTYPE(tmpRef) != SVt_PVIO)
8664 SvREFCNT_dec(SvSTASH(tmpRef));
8667 SvOBJECT_on(tmpRef);
8668 if (SvTYPE(tmpRef) != SVt_PVIO)
8670 (void)SvUPGRADE(tmpRef, SVt_PVMG);
8671 SvSTASH_set(tmpRef, (HV*)SvREFCNT_inc(stash));
8678 if(SvSMAGICAL(tmpRef))
8679 if(mg_find(tmpRef, PERL_MAGIC_ext) || mg_find(tmpRef, PERL_MAGIC_uvar))
8687 /* Downgrades a PVGV to a PVMG.
8691 S_sv_unglob(pTHX_ SV *sv)
8695 assert(SvTYPE(sv) == SVt_PVGV);
8700 SvREFCNT_dec(GvSTASH(sv));
8701 GvSTASH(sv) = Nullhv;
8703 sv_unmagic(sv, PERL_MAGIC_glob);
8704 Safefree(GvNAME(sv));
8707 /* need to keep SvANY(sv) in the right arena */
8708 xpvmg = new_XPVMG();
8709 StructCopy(SvANY(sv), xpvmg, XPVMG);
8710 del_XPVGV(SvANY(sv));
8713 SvFLAGS(sv) &= ~SVTYPEMASK;
8714 SvFLAGS(sv) |= SVt_PVMG;
8718 =for apidoc sv_unref_flags
8720 Unsets the RV status of the SV, and decrements the reference count of
8721 whatever was being referenced by the RV. This can almost be thought of
8722 as a reversal of C<newSVrv>. The C<cflags> argument can contain
8723 C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented
8724 (otherwise the decrementing is conditional on the reference count being
8725 different from one or the reference being a readonly SV).
8732 Perl_sv_unref_flags(pTHX_ SV *sv, U32 flags)
8736 if (SvWEAKREF(sv)) {
8744 /* You can't have a || SvREADONLY(rv) here, as $a = $$a, where $a was
8745 assigned to as BEGIN {$a = \"Foo"} will fail. */
8746 if (SvREFCNT(rv) != 1 || (flags & SV_IMMEDIATE_UNREF))
8748 else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */
8749 sv_2mortal(rv); /* Schedule for freeing later */
8753 =for apidoc sv_unref
8755 Unsets the RV status of the SV, and decrements the reference count of
8756 whatever was being referenced by the RV. This can almost be thought of
8757 as a reversal of C<newSVrv>. This is C<sv_unref_flags> with the C<flag>
8758 being zero. See C<SvROK_off>.
8764 Perl_sv_unref(pTHX_ SV *sv)
8766 sv_unref_flags(sv, 0);
8770 =for apidoc sv_taint
8772 Taint an SV. Use C<SvTAINTED_on> instead.
8777 Perl_sv_taint(pTHX_ SV *sv)
8779 sv_magic((sv), Nullsv, PERL_MAGIC_taint, Nullch, 0);
8783 =for apidoc sv_untaint
8785 Untaint an SV. Use C<SvTAINTED_off> instead.
8790 Perl_sv_untaint(pTHX_ SV *sv)
8792 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
8793 MAGIC *mg = mg_find(sv, PERL_MAGIC_taint);
8800 =for apidoc sv_tainted
8802 Test an SV for taintedness. Use C<SvTAINTED> instead.
8807 Perl_sv_tainted(pTHX_ SV *sv)
8809 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
8810 MAGIC *mg = mg_find(sv, PERL_MAGIC_taint);
8811 if (mg && ((mg->mg_len & 1) || ((mg->mg_len & 2) && mg->mg_obj == sv)))
8818 =for apidoc sv_setpviv
8820 Copies an integer into the given SV, also updating its string value.
8821 Does not handle 'set' magic. See C<sv_setpviv_mg>.
8827 Perl_sv_setpviv(pTHX_ SV *sv, IV iv)
8829 char buf[TYPE_CHARS(UV)];
8831 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
8833 sv_setpvn(sv, ptr, ebuf - ptr);
8837 =for apidoc sv_setpviv_mg
8839 Like C<sv_setpviv>, but also handles 'set' magic.
8845 Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv)
8847 char buf[TYPE_CHARS(UV)];
8849 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
8851 sv_setpvn(sv, ptr, ebuf - ptr);
8855 #if defined(PERL_IMPLICIT_CONTEXT)
8857 /* pTHX_ magic can't cope with varargs, so this is a no-context
8858 * version of the main function, (which may itself be aliased to us).
8859 * Don't access this version directly.
8863 Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...)
8867 va_start(args, pat);
8868 sv_vsetpvf(sv, pat, &args);
8872 /* pTHX_ magic can't cope with varargs, so this is a no-context
8873 * version of the main function, (which may itself be aliased to us).
8874 * Don't access this version directly.
8878 Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...)
8882 va_start(args, pat);
8883 sv_vsetpvf_mg(sv, pat, &args);
8889 =for apidoc sv_setpvf
8891 Works like C<sv_catpvf> but copies the text into the SV instead of
8892 appending it. Does not handle 'set' magic. See C<sv_setpvf_mg>.
8898 Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...)
8901 va_start(args, pat);
8902 sv_vsetpvf(sv, pat, &args);
8907 =for apidoc sv_vsetpvf
8909 Works like C<sv_vcatpvf> but copies the text into the SV instead of
8910 appending it. Does not handle 'set' magic. See C<sv_vsetpvf_mg>.
8912 Usually used via its frontend C<sv_setpvf>.
8918 Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args)
8920 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
8924 =for apidoc sv_setpvf_mg
8926 Like C<sv_setpvf>, but also handles 'set' magic.
8932 Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...)
8935 va_start(args, pat);
8936 sv_vsetpvf_mg(sv, pat, &args);
8941 =for apidoc sv_vsetpvf_mg
8943 Like C<sv_vsetpvf>, but also handles 'set' magic.
8945 Usually used via its frontend C<sv_setpvf_mg>.
8951 Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
8953 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
8957 #if defined(PERL_IMPLICIT_CONTEXT)
8959 /* pTHX_ magic can't cope with varargs, so this is a no-context
8960 * version of the main function, (which may itself be aliased to us).
8961 * Don't access this version directly.
8965 Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...)
8969 va_start(args, pat);
8970 sv_vcatpvf(sv, pat, &args);
8974 /* pTHX_ magic can't cope with varargs, so this is a no-context
8975 * version of the main function, (which may itself be aliased to us).
8976 * Don't access this version directly.
8980 Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...)
8984 va_start(args, pat);
8985 sv_vcatpvf_mg(sv, pat, &args);
8991 =for apidoc sv_catpvf
8993 Processes its arguments like C<sprintf> and appends the formatted
8994 output to an SV. If the appended data contains "wide" characters
8995 (including, but not limited to, SVs with a UTF-8 PV formatted with %s,
8996 and characters >255 formatted with %c), the original SV might get
8997 upgraded to UTF-8. Handles 'get' magic, but not 'set' magic. See
8998 C<sv_catpvf_mg>. If the original SV was UTF-8, the pattern should be
8999 valid UTF-8; if the original SV was bytes, the pattern should be too.
9004 Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...)
9007 va_start(args, pat);
9008 sv_vcatpvf(sv, pat, &args);
9013 =for apidoc sv_vcatpvf
9015 Processes its arguments like C<vsprintf> and appends the formatted output
9016 to an SV. Does not handle 'set' magic. See C<sv_vcatpvf_mg>.
9018 Usually used via its frontend C<sv_catpvf>.
9024 Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args)
9026 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
9030 =for apidoc sv_catpvf_mg
9032 Like C<sv_catpvf>, but also handles 'set' magic.
9038 Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...)
9041 va_start(args, pat);
9042 sv_vcatpvf_mg(sv, pat, &args);
9047 =for apidoc sv_vcatpvf_mg
9049 Like C<sv_vcatpvf>, but also handles 'set' magic.
9051 Usually used via its frontend C<sv_catpvf_mg>.
9057 Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
9059 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
9064 =for apidoc sv_vsetpvfn
9066 Works like C<sv_vcatpvfn> but copies the text into the SV instead of
9069 Usually used via one of its frontends C<sv_vsetpvf> and C<sv_vsetpvf_mg>.
9075 Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
9077 sv_setpvn(sv, "", 0);
9078 sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted);
9081 /* private function for use in sv_vcatpvfn via the EXPECT_NUMBER macro */
9084 S_expect_number(pTHX_ char** pattern)
9087 switch (**pattern) {
9088 case '1': case '2': case '3':
9089 case '4': case '5': case '6':
9090 case '7': case '8': case '9':
9091 while (isDIGIT(**pattern))
9092 var = var * 10 + (*(*pattern)++ - '0');
9096 #define EXPECT_NUMBER(pattern, var) (var = S_expect_number(aTHX_ &pattern))
9099 F0convert(NV nv, char *endbuf, STRLEN *len)
9110 if (uv & 1 && uv == nv)
9111 uv--; /* Round to even */
9113 unsigned dig = uv % 10;
9126 =for apidoc sv_vcatpvfn
9128 Processes its arguments like C<vsprintf> and appends the formatted output
9129 to an SV. Uses an array of SVs if the C style variable argument list is
9130 missing (NULL). When running with taint checks enabled, indicates via
9131 C<maybe_tainted> if results are untrustworthy (often due to the use of
9134 Usually used via one of its frontends C<sv_vcatpvf> and C<sv_vcatpvf_mg>.
9139 /* XXX maybe_tainted is never assigned to, so the doc above is lying. */
9142 Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
9149 static const char nullstr[] = "(null)";
9151 bool has_utf8; /* has the result utf8? */
9152 bool pat_utf8; /* the pattern is in utf8? */
9154 /* Times 4: a decimal digit takes more than 3 binary digits.
9155 * NV_DIG: mantissa takes than many decimal digits.
9156 * Plus 32: Playing safe. */
9157 char ebuf[IV_DIG * 4 + NV_DIG + 32];
9158 /* large enough for "%#.#f" --chip */
9159 /* what about long double NVs? --jhi */
9161 has_utf8 = pat_utf8 = DO_UTF8(sv);
9163 /* no matter what, this is a string now */
9164 (void)SvPV_force(sv, origlen);
9166 /* special-case "", "%s", and "%_" */
9169 if (patlen == 2 && pat[0] == '%') {
9173 const char *s = va_arg(*args, char*);
9174 sv_catpv(sv, s ? s : nullstr);
9176 else if (svix < svmax) {
9177 sv_catsv(sv, *svargs);
9178 if (DO_UTF8(*svargs))
9184 argsv = va_arg(*args, SV*);
9185 sv_catsv(sv, argsv);
9190 /* See comment on '_' below */
9195 #ifndef USE_LONG_DOUBLE
9196 /* special-case "%.<number>[gf]" */
9197 if ( patlen <= 5 && pat[0] == '%' && pat[1] == '.'
9198 && (pat[patlen-1] == 'g' || pat[patlen-1] == 'f') ) {
9199 unsigned digits = 0;
9203 while (*pp >= '0' && *pp <= '9')
9204 digits = 10 * digits + (*pp++ - '0');
9205 if (pp - pat == (int)patlen - 1) {
9209 nv = (NV)va_arg(*args, double);
9210 else if (svix < svmax)
9215 /* Add check for digits != 0 because it seems that some
9216 gconverts are buggy in this case, and we don't yet have
9217 a Configure test for this. */
9218 if (digits && digits < sizeof(ebuf) - NV_DIG - 10) {
9219 /* 0, point, slack */
9220 Gconvert(nv, (int)digits, 0, ebuf);
9222 if (*ebuf) /* May return an empty string for digits==0 */
9225 } else if (!digits) {
9228 if ((p = F0convert(nv, ebuf + sizeof ebuf, &l))) {
9229 sv_catpvn(sv, p, l);
9235 #endif /* !USE_LONG_DOUBLE */
9237 if (!args && svix < svmax && DO_UTF8(*svargs))
9240 patend = (char*)pat + patlen;
9241 for (p = (char*)pat; p < patend; p = q) {
9244 bool vectorize = FALSE;
9245 bool vectorarg = FALSE;
9246 bool vec_utf8 = FALSE;
9252 bool has_precis = FALSE;
9255 bool is_utf8 = FALSE; /* is this item utf8? */
9256 #ifdef HAS_LDBL_SPRINTF_BUG
9257 /* This is to try to fix a bug with irix/nonstop-ux/powerux and
9258 with sfio - Allen <allens@cpan.org> */
9259 bool fix_ldbl_sprintf_bug = FALSE;
9263 U8 utf8buf[UTF8_MAXBYTES+1];
9264 STRLEN esignlen = 0;
9266 char *eptr = Nullch;
9269 U8 *vecstr = Null(U8*);
9276 /* we need a long double target in case HAS_LONG_DOUBLE but
9279 #if defined(HAS_LONG_DOUBLE) && LONG_DOUBLESIZE > DOUBLESIZE
9287 const char *dotstr = ".";
9288 STRLEN dotstrlen = 1;
9289 I32 efix = 0; /* explicit format parameter index */
9290 I32 ewix = 0; /* explicit width index */
9291 I32 epix = 0; /* explicit precision index */
9292 I32 evix = 0; /* explicit vector index */
9293 bool asterisk = FALSE;
9295 /* echo everything up to the next format specification */
9296 for (q = p; q < patend && *q != '%'; ++q) ;
9298 if (has_utf8 && !pat_utf8)
9299 sv_catpvn_utf8_upgrade(sv, p, q - p, nsv);
9301 sv_catpvn(sv, p, q - p);
9308 We allow format specification elements in this order:
9309 \d+\$ explicit format parameter index
9311 v|\*(\d+\$)?v vector with optional (optionally specified) arg
9312 0 flag (as above): repeated to allow "v02"
9313 \d+|\*(\d+\$)? width using optional (optionally specified) arg
9314 \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg
9316 [%bcdefginopsux_DFOUX] format (mandatory)
9318 if (EXPECT_NUMBER(q, width)) {
9359 if (EXPECT_NUMBER(q, ewix))
9368 if ((vectorarg = asterisk)) {
9380 EXPECT_NUMBER(q, width);
9385 vecsv = va_arg(*args, SV*);
9387 vecsv = (evix ? evix <= svmax : svix < svmax) ?
9388 svargs[evix ? evix-1 : svix++] : &PL_sv_undef;
9389 dotstr = SvPVx(vecsv, dotstrlen);
9394 vecsv = va_arg(*args, SV*);
9395 vecstr = (U8*)SvPVx(vecsv,veclen);
9396 vec_utf8 = DO_UTF8(vecsv);
9398 else if (efix ? efix <= svmax : svix < svmax) {
9399 vecsv = svargs[efix ? efix-1 : svix++];
9400 vecstr = (U8*)SvPVx(vecsv,veclen);
9401 vec_utf8 = DO_UTF8(vecsv);
9402 /* if this is a version object, we need to return the
9403 * stringified representation (which the SvPVX has
9404 * already done for us), but not vectorize the args
9406 if ( *q == 'd' && sv_derived_from(vecsv,"version") )
9408 q++; /* skip past the rest of the %vd format */
9409 eptr = (char *) vecstr;
9410 elen = strlen(eptr);
9423 i = va_arg(*args, int);
9425 i = (ewix ? ewix <= svmax : svix < svmax) ?
9426 SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
9428 width = (i < 0) ? -i : i;
9438 if (EXPECT_NUMBER(q, epix) && *q++ != '$')
9440 /* XXX: todo, support specified precision parameter */
9444 i = va_arg(*args, int);
9446 i = (ewix ? ewix <= svmax : svix < svmax)
9447 ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
9448 precis = (i < 0) ? 0 : i;
9453 precis = precis * 10 + (*q++ - '0');
9462 case 'I': /* Ix, I32x, and I64x */
9464 if (q[1] == '6' && q[2] == '4') {
9470 if (q[1] == '3' && q[2] == '2') {
9480 #if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE)
9491 #if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE)
9492 if (*(q + 1) == 'l') { /* lld, llf */
9517 argsv = (efix ? efix <= svmax : svix < svmax) ?
9518 svargs[efix ? efix-1 : svix++] : &PL_sv_undef;
9525 uv = (args && !vectorize) ? va_arg(*args, int) : SvIVx(argsv);
9527 (!UNI_IS_INVARIANT(uv) && SvUTF8(sv)))
9529 eptr = (char*)utf8buf;
9530 elen = uvchr_to_utf8((U8*)eptr, uv) - utf8buf;
9541 if (args && !vectorize) {
9542 eptr = va_arg(*args, char*);
9544 #ifdef MACOS_TRADITIONAL
9545 /* On MacOS, %#s format is used for Pascal strings */
9550 elen = strlen(eptr);
9552 eptr = (char *)nullstr;
9553 elen = sizeof nullstr - 1;
9557 eptr = SvPVx(argsv, elen);
9558 if (DO_UTF8(argsv)) {
9559 if (has_precis && precis < elen) {
9561 sv_pos_u2b(argsv, &p, 0); /* sticks at end */
9564 if (width) { /* fudge width (can't fudge elen) */
9565 width += elen - sv_len_utf8(argsv);
9577 * The "%_" hack might have to be changed someday,
9578 * if ISO or ANSI decide to use '_' for something.
9579 * So we keep it hidden from users' code.
9581 if (!args || vectorize)
9583 argsv = va_arg(*args, SV*);
9584 eptr = SvPVx(argsv, elen);
9590 if (has_precis && elen > precis)
9601 goto format_sv; /* %-p -> %_ */
9605 goto format_sv; /* %-Np -> %.N_ */
9608 if (alt || vectorize)
9610 uv = PTR2UV(args ? va_arg(*args, void*) : argsv);
9628 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
9637 esignbuf[esignlen++] = plus;
9641 case 'h': iv = (short)va_arg(*args, int); break;
9642 case 'l': iv = va_arg(*args, long); break;
9643 case 'V': iv = va_arg(*args, IV); break;
9644 default: iv = va_arg(*args, int); break;
9646 case 'q': iv = va_arg(*args, Quad_t); break;
9651 IV tiv = SvIVx(argsv); /* work around GCC bug #13488 */
9653 case 'h': iv = (short)tiv; break;
9654 case 'l': iv = (long)tiv; break;
9656 default: iv = tiv; break;
9658 case 'q': iv = (Quad_t)tiv; break;
9662 if ( !vectorize ) /* we already set uv above */
9667 esignbuf[esignlen++] = plus;
9671 esignbuf[esignlen++] = '-';
9714 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
9725 case 'h': uv = (unsigned short)va_arg(*args, unsigned); break;
9726 case 'l': uv = va_arg(*args, unsigned long); break;
9727 case 'V': uv = va_arg(*args, UV); break;
9728 default: uv = va_arg(*args, unsigned); break;
9730 case 'q': uv = va_arg(*args, Uquad_t); break;
9735 UV tuv = SvUVx(argsv); /* work around GCC bug #13488 */
9737 case 'h': uv = (unsigned short)tuv; break;
9738 case 'l': uv = (unsigned long)tuv; break;
9740 default: uv = tuv; break;
9742 case 'q': uv = (Uquad_t)tuv; break;
9748 eptr = ebuf + sizeof ebuf;
9754 p = (char*)((c == 'X')
9755 ? "0123456789ABCDEF" : "0123456789abcdef");
9761 esignbuf[esignlen++] = '0';
9762 esignbuf[esignlen++] = c; /* 'x' or 'X' */
9768 *--eptr = '0' + dig;
9770 if (alt && *eptr != '0')
9776 *--eptr = '0' + dig;
9779 esignbuf[esignlen++] = '0';
9780 esignbuf[esignlen++] = 'b';
9783 default: /* it had better be ten or less */
9786 *--eptr = '0' + dig;
9787 } while (uv /= base);
9790 elen = (ebuf + sizeof ebuf) - eptr;
9793 zeros = precis - elen;
9794 else if (precis == 0 && elen == 1 && *eptr == '0')
9799 /* FLOATING POINT */
9802 c = 'f'; /* maybe %F isn't supported here */
9808 /* This is evil, but floating point is even more evil */
9810 /* for SV-style calling, we can only get NV
9811 for C-style calling, we assume %f is double;
9812 for simplicity we allow any of %Lf, %llf, %qf for long double
9816 #if defined(USE_LONG_DOUBLE)
9820 /* [perl #20339] - we should accept and ignore %lf rather than die */
9824 #if defined(USE_LONG_DOUBLE)
9825 intsize = args ? 0 : 'q';
9829 #if defined(HAS_LONG_DOUBLE)
9838 /* now we need (long double) if intsize == 'q', else (double) */
9839 nv = (args && !vectorize) ?
9840 #if LONG_DOUBLESIZE > DOUBLESIZE
9842 va_arg(*args, long double) :
9843 va_arg(*args, double)
9845 va_arg(*args, double)
9851 if (c != 'e' && c != 'E') {
9853 /* FIXME: if HAS_LONG_DOUBLE but not USE_LONG_DOUBLE this
9854 will cast our (long double) to (double) */
9855 (void)Perl_frexp(nv, &i);
9856 if (i == PERL_INT_MIN)
9857 Perl_die(aTHX_ "panic: frexp");
9859 need = BIT_DIGITS(i);
9861 need += has_precis ? precis : 6; /* known default */
9866 #ifdef HAS_LDBL_SPRINTF_BUG
9867 /* This is to try to fix a bug with irix/nonstop-ux/powerux and
9868 with sfio - Allen <allens@cpan.org> */
9871 # define MY_DBL_MAX DBL_MAX
9872 # else /* XXX guessing! HUGE_VAL may be defined as infinity, so not using */
9873 # if DOUBLESIZE >= 8
9874 # define MY_DBL_MAX 1.7976931348623157E+308L
9876 # define MY_DBL_MAX 3.40282347E+38L
9880 # ifdef HAS_LDBL_SPRINTF_BUG_LESS1 /* only between -1L & 1L - Allen */
9881 # define MY_DBL_MAX_BUG 1L
9883 # define MY_DBL_MAX_BUG MY_DBL_MAX
9887 # define MY_DBL_MIN DBL_MIN
9888 # else /* XXX guessing! -Allen */
9889 # if DOUBLESIZE >= 8
9890 # define MY_DBL_MIN 2.2250738585072014E-308L
9892 # define MY_DBL_MIN 1.17549435E-38L
9896 if ((intsize == 'q') && (c == 'f') &&
9897 ((nv < MY_DBL_MAX_BUG) && (nv > -MY_DBL_MAX_BUG)) &&
9899 /* it's going to be short enough that
9900 * long double precision is not needed */
9902 if ((nv <= 0L) && (nv >= -0L))
9903 fix_ldbl_sprintf_bug = TRUE; /* 0 is 0 - easiest */
9905 /* would use Perl_fp_class as a double-check but not
9906 * functional on IRIX - see perl.h comments */
9908 if ((nv >= MY_DBL_MIN) || (nv <= -MY_DBL_MIN)) {
9909 /* It's within the range that a double can represent */
9910 #if defined(DBL_MAX) && !defined(DBL_MIN)
9911 if ((nv >= ((long double)1/DBL_MAX)) ||
9912 (nv <= (-(long double)1/DBL_MAX)))
9914 fix_ldbl_sprintf_bug = TRUE;
9917 if (fix_ldbl_sprintf_bug == TRUE) {
9927 # undef MY_DBL_MAX_BUG
9930 #endif /* HAS_LDBL_SPRINTF_BUG */
9932 need += 20; /* fudge factor */
9933 if (PL_efloatsize < need) {
9934 Safefree(PL_efloatbuf);
9935 PL_efloatsize = need + 20; /* more fudge */
9936 New(906, PL_efloatbuf, PL_efloatsize, char);
9937 PL_efloatbuf[0] = '\0';
9940 if ( !(width || left || plus || alt) && fill != '0'
9941 && has_precis && intsize != 'q' ) { /* Shortcuts */
9942 /* See earlier comment about buggy Gconvert when digits,
9944 if ( c == 'g' && precis) {
9945 Gconvert((NV)nv, (int)precis, 0, PL_efloatbuf);
9946 if (*PL_efloatbuf) /* May return an empty string for digits==0 */
9947 goto float_converted;
9948 } else if ( c == 'f' && !precis) {
9949 if ((eptr = F0convert(nv, ebuf + sizeof ebuf, &elen)))
9953 eptr = ebuf + sizeof ebuf;
9956 /* FIXME: what to do if HAS_LONG_DOUBLE but not PERL_PRIfldbl? */
9957 #if defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl)
9958 if (intsize == 'q') {
9959 /* Copy the one or more characters in a long double
9960 * format before the 'base' ([efgEFG]) character to
9961 * the format string. */
9962 static char const prifldbl[] = PERL_PRIfldbl;
9963 char const *p = prifldbl + sizeof(prifldbl) - 3;
9964 while (p >= prifldbl) { *--eptr = *p--; }
9969 do { *--eptr = '0' + (base % 10); } while (base /= 10);
9974 do { *--eptr = '0' + (base % 10); } while (base /= 10);
9986 /* No taint. Otherwise we are in the strange situation
9987 * where printf() taints but print($float) doesn't.
9989 #if defined(HAS_LONG_DOUBLE)
9991 (void)sprintf(PL_efloatbuf, eptr, nv);
9993 (void)sprintf(PL_efloatbuf, eptr, (double)nv);
9995 (void)sprintf(PL_efloatbuf, eptr, nv);
9998 eptr = PL_efloatbuf;
9999 elen = strlen(PL_efloatbuf);
10005 i = SvCUR(sv) - origlen;
10006 if (args && !vectorize) {
10008 case 'h': *(va_arg(*args, short*)) = i; break;
10009 default: *(va_arg(*args, int*)) = i; break;
10010 case 'l': *(va_arg(*args, long*)) = i; break;
10011 case 'V': *(va_arg(*args, IV*)) = i; break;
10013 case 'q': *(va_arg(*args, Quad_t*)) = i; break;
10018 sv_setuv_mg(argsv, (UV)i);
10020 continue; /* not "break" */
10026 if (!args && ckWARN(WARN_PRINTF) &&
10027 (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) {
10028 SV *msg = sv_newmortal();
10029 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %sprintf: ",
10030 (PL_op->op_type == OP_PRTF) ? "" : "s");
10033 Perl_sv_catpvf(aTHX_ msg,
10034 "\"%%%c\"", c & 0xFF);
10036 Perl_sv_catpvf(aTHX_ msg,
10037 "\"%%\\%03"UVof"\"",
10040 sv_catpv(msg, "end of string");
10041 Perl_warner(aTHX_ packWARN(WARN_PRINTF), "%"SVf, msg); /* yes, this is reentrant */
10044 /* output mangled stuff ... */
10050 /* ... right here, because formatting flags should not apply */
10051 SvGROW(sv, SvCUR(sv) + elen + 1);
10053 Copy(eptr, p, elen, char);
10056 SvCUR_set(sv, p - SvPVX(sv));
10058 continue; /* not "break" */
10061 /* calculate width before utf8_upgrade changes it */
10062 have = esignlen + zeros + elen;
10064 if (is_utf8 != has_utf8) {
10067 sv_utf8_upgrade(sv);
10070 SV *nsv = sv_2mortal(newSVpvn(eptr, elen));
10071 sv_utf8_upgrade(nsv);
10075 SvGROW(sv, SvCUR(sv) + elen + 1);
10080 need = (have > width ? have : width);
10083 SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1);
10085 if (esignlen && fill == '0') {
10086 for (i = 0; i < (int)esignlen; i++)
10087 *p++ = esignbuf[i];
10089 if (gap && !left) {
10090 memset(p, fill, gap);
10093 if (esignlen && fill != '0') {
10094 for (i = 0; i < (int)esignlen; i++)
10095 *p++ = esignbuf[i];
10098 for (i = zeros; i; i--)
10102 Copy(eptr, p, elen, char);
10106 memset(p, ' ', gap);
10111 Copy(dotstr, p, dotstrlen, char);
10115 vectorize = FALSE; /* done iterating over vecstr */
10122 SvCUR_set(sv, p - SvPVX(sv));
10130 /* =========================================================================
10132 =head1 Cloning an interpreter
10134 All the macros and functions in this section are for the private use of
10135 the main function, perl_clone().
10137 The foo_dup() functions make an exact copy of an existing foo thinngy.
10138 During the course of a cloning, a hash table is used to map old addresses
10139 to new addresses. The table is created and manipulated with the
10140 ptr_table_* functions.
10144 ============================================================================*/
10147 #if defined(USE_ITHREADS)
10149 #ifndef GpREFCNT_inc
10150 # define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL)
10154 #define sv_dup_inc(s,t) SvREFCNT_inc(sv_dup(s,t))
10155 #define av_dup(s,t) (AV*)sv_dup((SV*)s,t)
10156 #define av_dup_inc(s,t) (AV*)SvREFCNT_inc(sv_dup((SV*)s,t))
10157 #define hv_dup(s,t) (HV*)sv_dup((SV*)s,t)
10158 #define hv_dup_inc(s,t) (HV*)SvREFCNT_inc(sv_dup((SV*)s,t))
10159 #define cv_dup(s,t) (CV*)sv_dup((SV*)s,t)
10160 #define cv_dup_inc(s,t) (CV*)SvREFCNT_inc(sv_dup((SV*)s,t))
10161 #define io_dup(s,t) (IO*)sv_dup((SV*)s,t)
10162 #define io_dup_inc(s,t) (IO*)SvREFCNT_inc(sv_dup((SV*)s,t))
10163 #define gv_dup(s,t) (GV*)sv_dup((SV*)s,t)
10164 #define gv_dup_inc(s,t) (GV*)SvREFCNT_inc(sv_dup((SV*)s,t))
10165 #define SAVEPV(p) (p ? savepv(p) : Nullch)
10166 #define SAVEPVN(p,n) (p ? savepvn(p,n) : Nullch)
10169 /* Duplicate a regexp. Required reading: pregcomp() and pregfree() in
10170 regcomp.c. AMS 20010712 */
10173 Perl_re_dup(pTHX_ REGEXP *r, CLONE_PARAMS *param)
10178 struct reg_substr_datum *s;
10181 return (REGEXP *)NULL;
10183 if ((ret = (REGEXP *)ptr_table_fetch(PL_ptr_table, r)))
10186 len = r->offsets[0];
10187 npar = r->nparens+1;
10189 Newc(0, ret, sizeof(regexp) + (len+1)*sizeof(regnode), char, regexp);
10190 Copy(r->program, ret->program, len+1, regnode);
10192 New(0, ret->startp, npar, I32);
10193 Copy(r->startp, ret->startp, npar, I32);
10194 New(0, ret->endp, npar, I32);
10195 Copy(r->startp, ret->startp, npar, I32);
10197 New(0, ret->substrs, 1, struct reg_substr_data);
10198 for (s = ret->substrs->data, i = 0; i < 3; i++, s++) {
10199 s->min_offset = r->substrs->data[i].min_offset;
10200 s->max_offset = r->substrs->data[i].max_offset;
10201 s->substr = sv_dup_inc(r->substrs->data[i].substr, param);
10202 s->utf8_substr = sv_dup_inc(r->substrs->data[i].utf8_substr, param);
10205 ret->regstclass = NULL;
10207 struct reg_data *d;
10208 const int count = r->data->count;
10210 Newc(0, d, sizeof(struct reg_data) + count*sizeof(void *),
10211 char, struct reg_data);
10212 New(0, d->what, count, U8);
10215 for (i = 0; i < count; i++) {
10216 d->what[i] = r->data->what[i];
10217 switch (d->what[i]) {
10218 /* legal options are one of: sfpont
10219 see also regcomp.h and pregfree() */
10221 d->data[i] = sv_dup_inc((SV *)r->data->data[i], param);
10224 d->data[i] = av_dup_inc((AV *)r->data->data[i], param);
10227 /* This is cheating. */
10228 New(0, d->data[i], 1, struct regnode_charclass_class);
10229 StructCopy(r->data->data[i], d->data[i],
10230 struct regnode_charclass_class);
10231 ret->regstclass = (regnode*)d->data[i];
10234 /* Compiled op trees are readonly, and can thus be
10235 shared without duplication. */
10237 d->data[i] = (void*)OpREFCNT_inc((OP*)r->data->data[i]);
10241 d->data[i] = r->data->data[i];
10244 d->data[i] = r->data->data[i];
10246 ((reg_trie_data*)d->data[i])->refcount++;
10250 Perl_croak(aTHX_ "panic: re_dup unknown data code '%c'", r->data->what[i]);
10259 New(0, ret->offsets, 2*len+1, U32);
10260 Copy(r->offsets, ret->offsets, 2*len+1, U32);
10262 ret->precomp = SAVEPVN(r->precomp, r->prelen);
10263 ret->refcnt = r->refcnt;
10264 ret->minlen = r->minlen;
10265 ret->prelen = r->prelen;
10266 ret->nparens = r->nparens;
10267 ret->lastparen = r->lastparen;
10268 ret->lastcloseparen = r->lastcloseparen;
10269 ret->reganch = r->reganch;
10271 ret->sublen = r->sublen;
10273 if (RX_MATCH_COPIED(ret))
10274 ret->subbeg = SAVEPVN(r->subbeg, r->sublen);
10276 ret->subbeg = Nullch;
10277 #ifdef PERL_COPY_ON_WRITE
10278 ret->saved_copy = Nullsv;
10281 ptr_table_store(PL_ptr_table, r, ret);
10285 /* duplicate a file handle */
10288 Perl_fp_dup(pTHX_ PerlIO *fp, char type, CLONE_PARAMS *param)
10294 return (PerlIO*)NULL;
10296 /* look for it in the table first */
10297 ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp);
10301 /* create anew and remember what it is */
10302 ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE);
10303 ptr_table_store(PL_ptr_table, fp, ret);
10307 /* duplicate a directory handle */
10310 Perl_dirp_dup(pTHX_ DIR *dp)
10318 /* duplicate a typeglob */
10321 Perl_gp_dup(pTHX_ GP *gp, CLONE_PARAMS* param)
10326 /* look for it in the table first */
10327 ret = (GP*)ptr_table_fetch(PL_ptr_table, gp);
10331 /* create anew and remember what it is */
10332 Newz(0, ret, 1, GP);
10333 ptr_table_store(PL_ptr_table, gp, ret);
10336 ret->gp_refcnt = 0; /* must be before any other dups! */
10337 ret->gp_sv = sv_dup_inc(gp->gp_sv, param);
10338 ret->gp_io = io_dup_inc(gp->gp_io, param);
10339 ret->gp_form = cv_dup_inc(gp->gp_form, param);
10340 ret->gp_av = av_dup_inc(gp->gp_av, param);
10341 ret->gp_hv = hv_dup_inc(gp->gp_hv, param);
10342 ret->gp_egv = gv_dup(gp->gp_egv, param);/* GvEGV is not refcounted */
10343 ret->gp_cv = cv_dup_inc(gp->gp_cv, param);
10344 ret->gp_cvgen = gp->gp_cvgen;
10345 ret->gp_flags = gp->gp_flags;
10346 ret->gp_line = gp->gp_line;
10347 ret->gp_file = gp->gp_file; /* points to COP.cop_file */
10351 /* duplicate a chain of magic */
10354 Perl_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS* param)
10356 MAGIC *mgprev = (MAGIC*)NULL;
10359 return (MAGIC*)NULL;
10360 /* look for it in the table first */
10361 mgret = (MAGIC*)ptr_table_fetch(PL_ptr_table, mg);
10365 for (; mg; mg = mg->mg_moremagic) {
10367 Newz(0, nmg, 1, MAGIC);
10369 mgprev->mg_moremagic = nmg;
10372 nmg->mg_virtual = mg->mg_virtual; /* XXX copy dynamic vtable? */
10373 nmg->mg_private = mg->mg_private;
10374 nmg->mg_type = mg->mg_type;
10375 nmg->mg_flags = mg->mg_flags;
10376 if (mg->mg_type == PERL_MAGIC_qr) {
10377 nmg->mg_obj = (SV*)re_dup((REGEXP*)mg->mg_obj, param);
10379 else if(mg->mg_type == PERL_MAGIC_backref) {
10380 const AV * const av = (AV*) mg->mg_obj;
10383 (void)SvREFCNT_inc(nmg->mg_obj = (SV*)newAV());
10385 for (i = AvFILLp(av); i >= 0; i--) {
10386 if (!svp[i]) continue;
10387 av_push((AV*)nmg->mg_obj,sv_dup(svp[i],param));
10391 nmg->mg_obj = (mg->mg_flags & MGf_REFCOUNTED)
10392 ? sv_dup_inc(mg->mg_obj, param)
10393 : sv_dup(mg->mg_obj, param);
10395 nmg->mg_len = mg->mg_len;
10396 nmg->mg_ptr = mg->mg_ptr; /* XXX random ptr? */
10397 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
10398 if (mg->mg_len > 0) {
10399 nmg->mg_ptr = SAVEPVN(mg->mg_ptr, mg->mg_len);
10400 if (mg->mg_type == PERL_MAGIC_overload_table &&
10401 AMT_AMAGIC((AMT*)mg->mg_ptr))
10403 AMT *amtp = (AMT*)mg->mg_ptr;
10404 AMT *namtp = (AMT*)nmg->mg_ptr;
10406 for (i = 1; i < NofAMmeth; i++) {
10407 namtp->table[i] = cv_dup_inc(amtp->table[i], param);
10411 else if (mg->mg_len == HEf_SVKEY)
10412 nmg->mg_ptr = (char*)sv_dup_inc((SV*)mg->mg_ptr, param);
10414 if ((mg->mg_flags & MGf_DUP) && mg->mg_virtual && mg->mg_virtual->svt_dup) {
10415 CALL_FPTR(nmg->mg_virtual->svt_dup)(aTHX_ nmg, param);
10422 /* create a new pointer-mapping table */
10425 Perl_ptr_table_new(pTHX)
10428 Newz(0, tbl, 1, PTR_TBL_t);
10429 tbl->tbl_max = 511;
10430 tbl->tbl_items = 0;
10431 Newz(0, tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
10436 # define PTR_TABLE_HASH(ptr) (PTR2UV(ptr) >> 3)
10438 # define PTR_TABLE_HASH(ptr) (PTR2UV(ptr) >> 2)
10446 register struct ptr_tbl_ent* pte;
10447 register struct ptr_tbl_ent* pteend;
10448 New(0, pte, PERL_ARENA_SIZE/sizeof(struct ptr_tbl_ent), struct ptr_tbl_ent);
10449 pte->next = PL_pte_arenaroot;
10450 PL_pte_arenaroot = pte;
10452 pteend = &pte[PERL_ARENA_SIZE / sizeof(struct ptr_tbl_ent) - 1];
10453 PL_pte_root = ++pte;
10454 while (pte < pteend) {
10455 pte->next = pte + 1;
10461 STATIC struct ptr_tbl_ent*
10464 struct ptr_tbl_ent* pte;
10468 PL_pte_root = pte->next;
10473 S_del_pte(pTHX_ struct ptr_tbl_ent*p)
10475 p->next = PL_pte_root;
10479 /* map an existing pointer using a table */
10482 Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, void *sv)
10484 PTR_TBL_ENT_t *tblent;
10485 const UV hash = PTR_TABLE_HASH(sv);
10487 tblent = tbl->tbl_ary[hash & tbl->tbl_max];
10488 for (; tblent; tblent = tblent->next) {
10489 if (tblent->oldval == sv)
10490 return tblent->newval;
10492 return (void*)NULL;
10495 /* add a new entry to a pointer-mapping table */
10498 Perl_ptr_table_store(pTHX_ PTR_TBL_t *tbl, void *oldv, void *newv)
10500 PTR_TBL_ENT_t *tblent, **otblent;
10501 /* XXX this may be pessimal on platforms where pointers aren't good
10502 * hash values e.g. if they grow faster in the most significant
10504 const UV hash = PTR_TABLE_HASH(oldv);
10508 otblent = &tbl->tbl_ary[hash & tbl->tbl_max];
10509 for (tblent = *otblent; tblent; empty=0, tblent = tblent->next) {
10510 if (tblent->oldval == oldv) {
10511 tblent->newval = newv;
10515 tblent = S_new_pte(aTHX);
10516 tblent->oldval = oldv;
10517 tblent->newval = newv;
10518 tblent->next = *otblent;
10521 if (!empty && tbl->tbl_items > tbl->tbl_max)
10522 ptr_table_split(tbl);
10525 /* double the hash bucket size of an existing ptr table */
10528 Perl_ptr_table_split(pTHX_ PTR_TBL_t *tbl)
10530 PTR_TBL_ENT_t **ary = tbl->tbl_ary;
10531 const UV oldsize = tbl->tbl_max + 1;
10532 UV newsize = oldsize * 2;
10535 Renew(ary, newsize, PTR_TBL_ENT_t*);
10536 Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
10537 tbl->tbl_max = --newsize;
10538 tbl->tbl_ary = ary;
10539 for (i=0; i < oldsize; i++, ary++) {
10540 PTR_TBL_ENT_t **curentp, **entp, *ent;
10543 curentp = ary + oldsize;
10544 for (entp = ary, ent = *ary; ent; ent = *entp) {
10545 if ((newsize & PTR_TABLE_HASH(ent->oldval)) != i) {
10547 ent->next = *curentp;
10557 /* remove all the entries from a ptr table */
10560 Perl_ptr_table_clear(pTHX_ PTR_TBL_t *tbl)
10562 register PTR_TBL_ENT_t **array;
10563 register PTR_TBL_ENT_t *entry;
10567 if (!tbl || !tbl->tbl_items) {
10571 array = tbl->tbl_ary;
10573 max = tbl->tbl_max;
10577 PTR_TBL_ENT_t *oentry = entry;
10578 entry = entry->next;
10579 S_del_pte(aTHX_ oentry);
10582 if (++riter > max) {
10585 entry = array[riter];
10589 tbl->tbl_items = 0;
10592 /* clear and free a ptr table */
10595 Perl_ptr_table_free(pTHX_ PTR_TBL_t *tbl)
10600 ptr_table_clear(tbl);
10601 Safefree(tbl->tbl_ary);
10605 /* attempt to make everything in the typeglob readonly */
10608 S_gv_share(pTHX_ SV *sstr, CLONE_PARAMS *param)
10610 GV *gv = (GV*)sstr;
10611 SV *sv = ¶m->proto_perl->Isv_no; /* just need SvREADONLY-ness */
10613 if (GvIO(gv) || GvFORM(gv)) {
10614 GvUNIQUE_off(gv); /* GvIOs cannot be shared. nor can GvFORMs */
10616 else if (!GvCV(gv)) {
10617 GvCV(gv) = (CV*)sv;
10620 /* CvPADLISTs cannot be shared */
10621 if (!SvREADONLY(GvCV(gv)) && !CvXSUB(GvCV(gv))) {
10626 if (!GvUNIQUE(gv)) {
10628 PerlIO_printf(Perl_debug_log, "gv_share: unable to share %s::%s\n",
10629 HvNAME(GvSTASH(gv)), GvNAME(gv));
10635 * write attempts will die with
10636 * "Modification of a read-only value attempted"
10642 SvREADONLY_on(GvSV(gv));
10646 GvAV(gv) = (AV*)sv;
10649 SvREADONLY_on(GvAV(gv));
10653 GvHV(gv) = (HV*)sv;
10656 SvREADONLY_on(GvHV(gv));
10659 return sstr; /* he_dup() will SvREFCNT_inc() */
10662 /* duplicate an SV of any type (including AV, HV etc) */
10665 Perl_rvpv_dup(pTHX_ SV *dstr, SV *sstr, CLONE_PARAMS* param)
10668 SvRV_set(dstr, SvWEAKREF(sstr)
10669 ? sv_dup(SvRV(sstr), param)
10670 : sv_dup_inc(SvRV(sstr), param));
10673 else if (SvPVX(sstr)) {
10674 /* Has something there */
10676 /* Normal PV - clone whole allocated space */
10677 SvPV_set(dstr, SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1));
10678 if (SvREADONLY(sstr) && SvFAKE(sstr)) {
10679 /* Not that normal - actually sstr is copy on write.
10680 But we are a true, independant SV, so: */
10681 SvREADONLY_off(dstr);
10686 /* Special case - not normally malloced for some reason */
10687 if (SvREADONLY(sstr) && SvFAKE(sstr)) {
10688 /* A "shared" PV - clone it as unshared string */
10689 if(SvPADTMP(sstr)) {
10690 /* However, some of them live in the pad
10691 and they should not have these flags
10694 SvPV_set(dstr, sharepvn(SvPVX(sstr), SvCUR(sstr),
10696 SvUV_set(dstr, SvUVX(sstr));
10699 SvPV_set(dstr, SAVEPVN(SvPVX(sstr), SvCUR(sstr)));
10701 SvREADONLY_off(dstr);
10705 /* Some other special case - random pointer */
10706 SvPV_set(dstr, SvPVX(sstr));
10711 /* Copy the Null */
10712 if (SvTYPE(dstr) == SVt_RV)
10713 SvRV_set(dstr, NULL);
10720 Perl_sv_dup(pTHX_ SV *sstr, CLONE_PARAMS* param)
10725 if (!sstr || SvTYPE(sstr) == SVTYPEMASK)
10727 /* look for it in the table first */
10728 dstr = (SV*)ptr_table_fetch(PL_ptr_table, sstr);
10732 if(param->flags & CLONEf_JOIN_IN) {
10733 /** We are joining here so we don't want do clone
10734 something that is bad **/
10736 if(SvTYPE(sstr) == SVt_PVHV &&
10738 /** don't clone stashes if they already exist **/
10739 HV* old_stash = gv_stashpv(HvNAME(sstr),0);
10740 return (SV*) old_stash;
10744 /* create anew and remember what it is */
10747 #ifdef DEBUG_LEAKING_SCALARS
10748 dstr->sv_debug_optype = sstr->sv_debug_optype;
10749 dstr->sv_debug_line = sstr->sv_debug_line;
10750 dstr->sv_debug_inpad = sstr->sv_debug_inpad;
10751 dstr->sv_debug_cloned = 1;
10753 dstr->sv_debug_file = savepv(sstr->sv_debug_file);
10755 dstr->sv_debug_file = savesharedpv(sstr->sv_debug_file);
10759 ptr_table_store(PL_ptr_table, sstr, dstr);
10762 SvFLAGS(dstr) = SvFLAGS(sstr);
10763 SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */
10764 SvREFCNT(dstr) = 0; /* must be before any other dups! */
10767 if (SvANY(sstr) && PL_watch_pvx && SvPVX(sstr) == PL_watch_pvx)
10768 PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n",
10769 PL_watch_pvx, SvPVX(sstr));
10772 /* don't clone objects whose class has asked us not to */
10773 if (SvOBJECT(sstr) && ! (SvFLAGS(SvSTASH(sstr)) & SVphv_CLONEABLE)) {
10774 SvFLAGS(dstr) &= ~SVTYPEMASK;
10775 SvOBJECT_off(dstr);
10779 switch (SvTYPE(sstr)) {
10781 SvANY(dstr) = NULL;
10784 SvANY(dstr) = new_XIV();
10785 SvIV_set(dstr, SvIVX(sstr));
10788 SvANY(dstr) = new_XNV();
10789 SvNV_set(dstr, SvNVX(sstr));
10792 SvANY(dstr) = new_XRV();
10793 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10796 SvANY(dstr) = new_XPV();
10797 SvCUR_set(dstr, SvCUR(sstr));
10798 SvLEN_set(dstr, SvLEN(sstr));
10799 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10802 SvANY(dstr) = new_XPVIV();
10803 SvCUR_set(dstr, SvCUR(sstr));
10804 SvLEN_set(dstr, SvLEN(sstr));
10805 SvIV_set(dstr, SvIVX(sstr));
10806 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10809 SvANY(dstr) = new_XPVNV();
10810 SvCUR_set(dstr, SvCUR(sstr));
10811 SvLEN_set(dstr, SvLEN(sstr));
10812 SvIV_set(dstr, SvIVX(sstr));
10813 SvNV_set(dstr, SvNVX(sstr));
10814 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10817 SvANY(dstr) = new_XPVMG();
10818 SvCUR_set(dstr, SvCUR(sstr));
10819 SvLEN_set(dstr, SvLEN(sstr));
10820 SvIV_set(dstr, SvIVX(sstr));
10821 SvNV_set(dstr, SvNVX(sstr));
10822 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10823 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
10824 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10827 SvANY(dstr) = new_XPVBM();
10828 SvCUR_set(dstr, SvCUR(sstr));
10829 SvLEN_set(dstr, SvLEN(sstr));
10830 SvIV_set(dstr, SvIVX(sstr));
10831 SvNV_set(dstr, SvNVX(sstr));
10832 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10833 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
10834 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10835 BmRARE(dstr) = BmRARE(sstr);
10836 BmUSEFUL(dstr) = BmUSEFUL(sstr);
10837 BmPREVIOUS(dstr)= BmPREVIOUS(sstr);
10840 SvANY(dstr) = new_XPVLV();
10841 SvCUR_set(dstr, SvCUR(sstr));
10842 SvLEN_set(dstr, SvLEN(sstr));
10843 SvIV_set(dstr, SvIVX(sstr));
10844 SvNV_set(dstr, SvNVX(sstr));
10845 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10846 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
10847 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10848 LvTARGOFF(dstr) = LvTARGOFF(sstr); /* XXX sometimes holds PMOP* when DEBUGGING */
10849 LvTARGLEN(dstr) = LvTARGLEN(sstr);
10850 if (LvTYPE(sstr) == 't') /* for tie: unrefcnted fake (SV**) */
10851 LvTARG(dstr) = dstr;
10852 else if (LvTYPE(sstr) == 'T') /* for tie: fake HE */
10853 LvTARG(dstr) = (SV*)he_dup((HE*)LvTARG(sstr), 0, param);
10855 LvTARG(dstr) = sv_dup_inc(LvTARG(sstr), param);
10856 LvTYPE(dstr) = LvTYPE(sstr);
10859 if (GvUNIQUE((GV*)sstr)) {
10861 if ((share = gv_share(sstr, param))) {
10864 ptr_table_store(PL_ptr_table, sstr, dstr);
10866 PerlIO_printf(Perl_debug_log, "sv_dup: sharing %s::%s\n",
10867 HvNAME(GvSTASH(share)), GvNAME(share));
10872 SvANY(dstr) = new_XPVGV();
10873 SvCUR_set(dstr, SvCUR(sstr));
10874 SvLEN_set(dstr, SvLEN(sstr));
10875 SvIV_set(dstr, SvIVX(sstr));
10876 SvNV_set(dstr, SvNVX(sstr));
10877 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10878 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
10879 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10880 GvNAMELEN(dstr) = GvNAMELEN(sstr);
10881 GvNAME(dstr) = SAVEPVN(GvNAME(sstr), GvNAMELEN(sstr));
10882 GvSTASH(dstr) = hv_dup_inc(GvSTASH(sstr), param);
10883 GvFLAGS(dstr) = GvFLAGS(sstr);
10884 GvGP(dstr) = gp_dup(GvGP(sstr), param);
10885 (void)GpREFCNT_inc(GvGP(dstr));
10888 SvANY(dstr) = new_XPVIO();
10889 SvCUR_set(dstr, SvCUR(sstr));
10890 SvLEN_set(dstr, SvLEN(sstr));
10891 SvIV_set(dstr, SvIVX(sstr));
10892 SvNV_set(dstr, SvNVX(sstr));
10893 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10894 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
10895 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10896 IoIFP(dstr) = fp_dup(IoIFP(sstr), IoTYPE(sstr), param);
10897 if (IoOFP(sstr) == IoIFP(sstr))
10898 IoOFP(dstr) = IoIFP(dstr);
10900 IoOFP(dstr) = fp_dup(IoOFP(sstr), IoTYPE(sstr), param);
10901 /* PL_rsfp_filters entries have fake IoDIRP() */
10902 if (IoDIRP(sstr) && !(IoFLAGS(sstr) & IOf_FAKE_DIRP))
10903 IoDIRP(dstr) = dirp_dup(IoDIRP(sstr));
10905 IoDIRP(dstr) = IoDIRP(sstr);
10906 IoLINES(dstr) = IoLINES(sstr);
10907 IoPAGE(dstr) = IoPAGE(sstr);
10908 IoPAGE_LEN(dstr) = IoPAGE_LEN(sstr);
10909 IoLINES_LEFT(dstr) = IoLINES_LEFT(sstr);
10910 if(IoFLAGS(sstr) & IOf_FAKE_DIRP) {
10911 /* I have no idea why fake dirp (rsfps)
10912 should be treaded differently but otherwise
10913 we end up with leaks -- sky*/
10914 IoTOP_GV(dstr) = gv_dup_inc(IoTOP_GV(sstr), param);
10915 IoFMT_GV(dstr) = gv_dup_inc(IoFMT_GV(sstr), param);
10916 IoBOTTOM_GV(dstr) = gv_dup_inc(IoBOTTOM_GV(sstr), param);
10918 IoTOP_GV(dstr) = gv_dup(IoTOP_GV(sstr), param);
10919 IoFMT_GV(dstr) = gv_dup(IoFMT_GV(sstr), param);
10920 IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(sstr), param);
10922 IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(sstr));
10923 IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(sstr));
10924 IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(sstr));
10925 IoSUBPROCESS(dstr) = IoSUBPROCESS(sstr);
10926 IoTYPE(dstr) = IoTYPE(sstr);
10927 IoFLAGS(dstr) = IoFLAGS(sstr);
10930 SvANY(dstr) = new_XPVAV();
10931 SvCUR_set(dstr, SvCUR(sstr));
10932 SvLEN_set(dstr, SvLEN(sstr));
10933 SvIV_set(dstr, SvIVX(sstr));
10934 SvNV_set(dstr, SvNVX(sstr));
10935 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10936 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
10937 AvARYLEN((AV*)dstr) = sv_dup_inc(AvARYLEN((AV*)sstr), param);
10938 AvFLAGS((AV*)dstr) = AvFLAGS((AV*)sstr);
10939 if (AvARRAY((AV*)sstr)) {
10940 SV **dst_ary, **src_ary;
10941 SSize_t items = AvFILLp((AV*)sstr) + 1;
10943 src_ary = AvARRAY((AV*)sstr);
10944 Newz(0, dst_ary, AvMAX((AV*)sstr)+1, SV*);
10945 ptr_table_store(PL_ptr_table, src_ary, dst_ary);
10946 SvPV_set(dstr, (char*)dst_ary);
10947 AvALLOC((AV*)dstr) = dst_ary;
10948 if (AvREAL((AV*)sstr)) {
10949 while (items-- > 0)
10950 *dst_ary++ = sv_dup_inc(*src_ary++, param);
10953 while (items-- > 0)
10954 *dst_ary++ = sv_dup(*src_ary++, param);
10956 items = AvMAX((AV*)sstr) - AvFILLp((AV*)sstr);
10957 while (items-- > 0) {
10958 *dst_ary++ = &PL_sv_undef;
10962 SvPV_set(dstr, Nullch);
10963 AvALLOC((AV*)dstr) = (SV**)NULL;
10967 SvANY(dstr) = new_XPVHV();
10968 SvCUR_set(dstr, SvCUR(sstr));
10969 SvLEN_set(dstr, SvLEN(sstr));
10970 SvIV_set(dstr, SvIVX(sstr));
10971 SvNV_set(dstr, SvNVX(sstr));
10972 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10973 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
10974 HvRITER((HV*)dstr) = HvRITER((HV*)sstr);
10975 if (HvARRAY((HV*)sstr)) {
10977 XPVHV *dxhv = (XPVHV*)SvANY(dstr);
10978 XPVHV *sxhv = (XPVHV*)SvANY(sstr);
10979 Newz(0, dxhv->xhv_array,
10980 PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1), char);
10981 while (i <= sxhv->xhv_max) {
10982 ((HE**)dxhv->xhv_array)[i] = he_dup(((HE**)sxhv->xhv_array)[i],
10983 (bool)!!HvSHAREKEYS(sstr),
10987 dxhv->xhv_eiter = he_dup(sxhv->xhv_eiter,
10988 (bool)!!HvSHAREKEYS(sstr), param);
10991 SvPV_set(dstr, Nullch);
10992 HvEITER((HV*)dstr) = (HE*)NULL;
10994 HvPMROOT((HV*)dstr) = HvPMROOT((HV*)sstr); /* XXX */
10995 HvNAME((HV*)dstr) = SAVEPV(HvNAME((HV*)sstr));
10996 /* Record stashes for possible cloning in Perl_clone(). */
10997 if(HvNAME((HV*)dstr))
10998 av_push(param->stashes, dstr);
11001 SvANY(dstr) = new_XPVFM();
11002 FmLINES(dstr) = FmLINES(sstr);
11006 SvANY(dstr) = new_XPVCV();
11008 SvCUR_set(dstr, SvCUR(sstr));
11009 SvLEN_set(dstr, SvLEN(sstr));
11010 SvIV_set(dstr, SvIVX(sstr));
11011 SvNV_set(dstr, SvNVX(sstr));
11012 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
11013 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
11014 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
11015 CvSTASH(dstr) = hv_dup(CvSTASH(sstr), param); /* NOTE: not refcounted */
11016 CvSTART(dstr) = CvSTART(sstr);
11018 CvROOT(dstr) = OpREFCNT_inc(CvROOT(sstr));
11020 CvXSUB(dstr) = CvXSUB(sstr);
11021 CvXSUBANY(dstr) = CvXSUBANY(sstr);
11022 if (CvCONST(sstr)) {
11023 CvXSUBANY(dstr).any_ptr = GvUNIQUE(CvGV(sstr)) ?
11024 SvREFCNT_inc(CvXSUBANY(sstr).any_ptr) :
11025 sv_dup_inc((SV *)CvXSUBANY(sstr).any_ptr, param);
11027 /* don't dup if copying back - CvGV isn't refcounted, so the
11028 * duped GV may never be freed. A bit of a hack! DAPM */
11029 CvGV(dstr) = (param->flags & CLONEf_JOIN_IN) ?
11030 Nullgv : gv_dup(CvGV(sstr), param) ;
11031 if (param->flags & CLONEf_COPY_STACKS) {
11032 CvDEPTH(dstr) = CvDEPTH(sstr);
11036 PAD_DUP(CvPADLIST(dstr), CvPADLIST(sstr), param);
11037 CvOUTSIDE_SEQ(dstr) = CvOUTSIDE_SEQ(sstr);
11039 CvWEAKOUTSIDE(sstr)
11040 ? cv_dup( CvOUTSIDE(sstr), param)
11041 : cv_dup_inc(CvOUTSIDE(sstr), param);
11042 CvFLAGS(dstr) = CvFLAGS(sstr);
11043 CvFILE(dstr) = CvXSUB(sstr) ? CvFILE(sstr) : SAVEPV(CvFILE(sstr));
11046 Perl_croak(aTHX_ "Bizarre SvTYPE [%" IVdf "]", (IV)SvTYPE(sstr));
11050 if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO)
11056 /* duplicate a context */
11059 Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max, CLONE_PARAMS* param)
11061 PERL_CONTEXT *ncxs;
11064 return (PERL_CONTEXT*)NULL;
11066 /* look for it in the table first */
11067 ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs);
11071 /* create anew and remember what it is */
11072 Newz(56, ncxs, max + 1, PERL_CONTEXT);
11073 ptr_table_store(PL_ptr_table, cxs, ncxs);
11076 PERL_CONTEXT *cx = &cxs[ix];
11077 PERL_CONTEXT *ncx = &ncxs[ix];
11078 ncx->cx_type = cx->cx_type;
11079 if (CxTYPE(cx) == CXt_SUBST) {
11080 Perl_croak(aTHX_ "Cloning substitution context is unimplemented");
11083 ncx->blk_oldsp = cx->blk_oldsp;
11084 ncx->blk_oldcop = cx->blk_oldcop;
11085 ncx->blk_oldmarksp = cx->blk_oldmarksp;
11086 ncx->blk_oldscopesp = cx->blk_oldscopesp;
11087 ncx->blk_oldpm = cx->blk_oldpm;
11088 ncx->blk_gimme = cx->blk_gimme;
11089 switch (CxTYPE(cx)) {
11091 ncx->blk_sub.cv = (cx->blk_sub.olddepth == 0
11092 ? cv_dup_inc(cx->blk_sub.cv, param)
11093 : cv_dup(cx->blk_sub.cv,param));
11094 ncx->blk_sub.argarray = (cx->blk_sub.hasargs
11095 ? av_dup_inc(cx->blk_sub.argarray, param)
11097 ncx->blk_sub.savearray = av_dup_inc(cx->blk_sub.savearray, param);
11098 ncx->blk_sub.olddepth = cx->blk_sub.olddepth;
11099 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
11100 ncx->blk_sub.lval = cx->blk_sub.lval;
11101 ncx->blk_sub.retop = cx->blk_sub.retop;
11104 ncx->blk_eval.old_in_eval = cx->blk_eval.old_in_eval;
11105 ncx->blk_eval.old_op_type = cx->blk_eval.old_op_type;
11106 ncx->blk_eval.old_namesv = sv_dup_inc(cx->blk_eval.old_namesv, param);
11107 ncx->blk_eval.old_eval_root = cx->blk_eval.old_eval_root;
11108 ncx->blk_eval.cur_text = sv_dup(cx->blk_eval.cur_text, param);
11109 ncx->blk_eval.retop = cx->blk_eval.retop;
11112 ncx->blk_loop.label = cx->blk_loop.label;
11113 ncx->blk_loop.resetsp = cx->blk_loop.resetsp;
11114 ncx->blk_loop.redo_op = cx->blk_loop.redo_op;
11115 ncx->blk_loop.next_op = cx->blk_loop.next_op;
11116 ncx->blk_loop.last_op = cx->blk_loop.last_op;
11117 ncx->blk_loop.iterdata = (CxPADLOOP(cx)
11118 ? cx->blk_loop.iterdata
11119 : gv_dup((GV*)cx->blk_loop.iterdata, param));
11120 ncx->blk_loop.oldcomppad
11121 = (PAD*)ptr_table_fetch(PL_ptr_table,
11122 cx->blk_loop.oldcomppad);
11123 ncx->blk_loop.itersave = sv_dup_inc(cx->blk_loop.itersave, param);
11124 ncx->blk_loop.iterlval = sv_dup_inc(cx->blk_loop.iterlval, param);
11125 ncx->blk_loop.iterary = av_dup_inc(cx->blk_loop.iterary, param);
11126 ncx->blk_loop.iterix = cx->blk_loop.iterix;
11127 ncx->blk_loop.itermax = cx->blk_loop.itermax;
11130 ncx->blk_sub.cv = cv_dup(cx->blk_sub.cv, param);
11131 ncx->blk_sub.gv = gv_dup(cx->blk_sub.gv, param);
11132 ncx->blk_sub.dfoutgv = gv_dup_inc(cx->blk_sub.dfoutgv, param);
11133 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
11134 ncx->blk_sub.retop = cx->blk_sub.retop;
11146 /* duplicate a stack info structure */
11149 Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param)
11154 return (PERL_SI*)NULL;
11156 /* look for it in the table first */
11157 nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si);
11161 /* create anew and remember what it is */
11162 Newz(56, nsi, 1, PERL_SI);
11163 ptr_table_store(PL_ptr_table, si, nsi);
11165 nsi->si_stack = av_dup_inc(si->si_stack, param);
11166 nsi->si_cxix = si->si_cxix;
11167 nsi->si_cxmax = si->si_cxmax;
11168 nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax, param);
11169 nsi->si_type = si->si_type;
11170 nsi->si_prev = si_dup(si->si_prev, param);
11171 nsi->si_next = si_dup(si->si_next, param);
11172 nsi->si_markoff = si->si_markoff;
11177 #define POPINT(ss,ix) ((ss)[--(ix)].any_i32)
11178 #define TOPINT(ss,ix) ((ss)[ix].any_i32)
11179 #define POPLONG(ss,ix) ((ss)[--(ix)].any_long)
11180 #define TOPLONG(ss,ix) ((ss)[ix].any_long)
11181 #define POPIV(ss,ix) ((ss)[--(ix)].any_iv)
11182 #define TOPIV(ss,ix) ((ss)[ix].any_iv)
11183 #define POPBOOL(ss,ix) ((ss)[--(ix)].any_bool)
11184 #define TOPBOOL(ss,ix) ((ss)[ix].any_bool)
11185 #define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr)
11186 #define TOPPTR(ss,ix) ((ss)[ix].any_ptr)
11187 #define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr)
11188 #define TOPDPTR(ss,ix) ((ss)[ix].any_dptr)
11189 #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr)
11190 #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr)
11193 #define pv_dup_inc(p) SAVEPV(p)
11194 #define pv_dup(p) SAVEPV(p)
11195 #define svp_dup_inc(p,pp) any_dup(p,pp)
11197 /* map any object to the new equivent - either something in the
11198 * ptr table, or something in the interpreter structure
11202 Perl_any_dup(pTHX_ void *v, PerlInterpreter *proto_perl)
11207 return (void*)NULL;
11209 /* look for it in the table first */
11210 ret = ptr_table_fetch(PL_ptr_table, v);
11214 /* see if it is part of the interpreter structure */
11215 if (v >= (void*)proto_perl && v < (void*)(proto_perl+1))
11216 ret = (void*)(((char*)aTHX) + (((char*)v) - (char*)proto_perl));
11224 /* duplicate the save stack */
11227 Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param)
11229 ANY *ss = proto_perl->Tsavestack;
11230 I32 ix = proto_perl->Tsavestack_ix;
11231 I32 max = proto_perl->Tsavestack_max;
11244 void (*dptr) (void*);
11245 void (*dxptr) (pTHX_ void*);
11248 Newz(54, nss, max, ANY);
11252 TOPINT(nss,ix) = i;
11254 case SAVEt_ITEM: /* normal string */
11255 sv = (SV*)POPPTR(ss,ix);
11256 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
11257 sv = (SV*)POPPTR(ss,ix);
11258 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
11260 case SAVEt_SV: /* scalar reference */
11261 sv = (SV*)POPPTR(ss,ix);
11262 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
11263 gv = (GV*)POPPTR(ss,ix);
11264 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
11266 case SAVEt_GENERIC_PVREF: /* generic char* */
11267 c = (char*)POPPTR(ss,ix);
11268 TOPPTR(nss,ix) = pv_dup(c);
11269 ptr = POPPTR(ss,ix);
11270 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11272 case SAVEt_SHARED_PVREF: /* char* in shared space */
11273 c = (char*)POPPTR(ss,ix);
11274 TOPPTR(nss,ix) = savesharedpv(c);
11275 ptr = POPPTR(ss,ix);
11276 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11278 case SAVEt_GENERIC_SVREF: /* generic sv */
11279 case SAVEt_SVREF: /* scalar reference */
11280 sv = (SV*)POPPTR(ss,ix);
11281 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
11282 ptr = POPPTR(ss,ix);
11283 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
11285 case SAVEt_AV: /* array reference */
11286 av = (AV*)POPPTR(ss,ix);
11287 TOPPTR(nss,ix) = av_dup_inc(av, param);
11288 gv = (GV*)POPPTR(ss,ix);
11289 TOPPTR(nss,ix) = gv_dup(gv, param);
11291 case SAVEt_HV: /* hash reference */
11292 hv = (HV*)POPPTR(ss,ix);
11293 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
11294 gv = (GV*)POPPTR(ss,ix);
11295 TOPPTR(nss,ix) = gv_dup(gv, param);
11297 case SAVEt_INT: /* int reference */
11298 ptr = POPPTR(ss,ix);
11299 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11300 intval = (int)POPINT(ss,ix);
11301 TOPINT(nss,ix) = intval;
11303 case SAVEt_LONG: /* long reference */
11304 ptr = POPPTR(ss,ix);
11305 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11306 longval = (long)POPLONG(ss,ix);
11307 TOPLONG(nss,ix) = longval;
11309 case SAVEt_I32: /* I32 reference */
11310 case SAVEt_I16: /* I16 reference */
11311 case SAVEt_I8: /* I8 reference */
11312 ptr = POPPTR(ss,ix);
11313 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11315 TOPINT(nss,ix) = i;
11317 case SAVEt_IV: /* IV reference */
11318 ptr = POPPTR(ss,ix);
11319 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11321 TOPIV(nss,ix) = iv;
11323 case SAVEt_SPTR: /* SV* reference */
11324 ptr = POPPTR(ss,ix);
11325 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11326 sv = (SV*)POPPTR(ss,ix);
11327 TOPPTR(nss,ix) = sv_dup(sv, param);
11329 case SAVEt_VPTR: /* random* reference */
11330 ptr = POPPTR(ss,ix);
11331 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11332 ptr = POPPTR(ss,ix);
11333 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11335 case SAVEt_PPTR: /* char* reference */
11336 ptr = POPPTR(ss,ix);
11337 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11338 c = (char*)POPPTR(ss,ix);
11339 TOPPTR(nss,ix) = pv_dup(c);
11341 case SAVEt_HPTR: /* HV* reference */
11342 ptr = POPPTR(ss,ix);
11343 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11344 hv = (HV*)POPPTR(ss,ix);
11345 TOPPTR(nss,ix) = hv_dup(hv, param);
11347 case SAVEt_APTR: /* AV* reference */
11348 ptr = POPPTR(ss,ix);
11349 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11350 av = (AV*)POPPTR(ss,ix);
11351 TOPPTR(nss,ix) = av_dup(av, param);
11354 gv = (GV*)POPPTR(ss,ix);
11355 TOPPTR(nss,ix) = gv_dup(gv, param);
11357 case SAVEt_GP: /* scalar reference */
11358 gp = (GP*)POPPTR(ss,ix);
11359 TOPPTR(nss,ix) = gp = gp_dup(gp, param);
11360 (void)GpREFCNT_inc(gp);
11361 gv = (GV*)POPPTR(ss,ix);
11362 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
11363 c = (char*)POPPTR(ss,ix);
11364 TOPPTR(nss,ix) = pv_dup(c);
11366 TOPIV(nss,ix) = iv;
11368 TOPIV(nss,ix) = iv;
11371 case SAVEt_MORTALIZESV:
11372 sv = (SV*)POPPTR(ss,ix);
11373 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
11376 ptr = POPPTR(ss,ix);
11377 if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) {
11378 /* these are assumed to be refcounted properly */
11379 switch (((OP*)ptr)->op_type) {
11381 case OP_LEAVESUBLV:
11385 case OP_LEAVEWRITE:
11386 TOPPTR(nss,ix) = ptr;
11391 TOPPTR(nss,ix) = Nullop;
11396 TOPPTR(nss,ix) = Nullop;
11399 c = (char*)POPPTR(ss,ix);
11400 TOPPTR(nss,ix) = pv_dup_inc(c);
11402 case SAVEt_CLEARSV:
11403 longval = POPLONG(ss,ix);
11404 TOPLONG(nss,ix) = longval;
11407 hv = (HV*)POPPTR(ss,ix);
11408 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
11409 c = (char*)POPPTR(ss,ix);
11410 TOPPTR(nss,ix) = pv_dup_inc(c);
11412 TOPINT(nss,ix) = i;
11414 case SAVEt_DESTRUCTOR:
11415 ptr = POPPTR(ss,ix);
11416 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
11417 dptr = POPDPTR(ss,ix);
11418 TOPDPTR(nss,ix) = (void (*)(void*))any_dup((void *)dptr, proto_perl);
11420 case SAVEt_DESTRUCTOR_X:
11421 ptr = POPPTR(ss,ix);
11422 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
11423 dxptr = POPDXPTR(ss,ix);
11424 TOPDXPTR(nss,ix) = (void (*)(pTHX_ void*))any_dup((void *)dxptr, proto_perl);
11426 case SAVEt_REGCONTEXT:
11429 TOPINT(nss,ix) = i;
11432 case SAVEt_STACK_POS: /* Position on Perl stack */
11434 TOPINT(nss,ix) = i;
11436 case SAVEt_AELEM: /* array element */
11437 sv = (SV*)POPPTR(ss,ix);
11438 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
11440 TOPINT(nss,ix) = i;
11441 av = (AV*)POPPTR(ss,ix);
11442 TOPPTR(nss,ix) = av_dup_inc(av, param);
11444 case SAVEt_HELEM: /* hash element */
11445 sv = (SV*)POPPTR(ss,ix);
11446 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
11447 sv = (SV*)POPPTR(ss,ix);
11448 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
11449 hv = (HV*)POPPTR(ss,ix);
11450 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
11453 ptr = POPPTR(ss,ix);
11454 TOPPTR(nss,ix) = ptr;
11458 TOPINT(nss,ix) = i;
11460 case SAVEt_COMPPAD:
11461 av = (AV*)POPPTR(ss,ix);
11462 TOPPTR(nss,ix) = av_dup(av, param);
11465 longval = (long)POPLONG(ss,ix);
11466 TOPLONG(nss,ix) = longval;
11467 ptr = POPPTR(ss,ix);
11468 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11469 sv = (SV*)POPPTR(ss,ix);
11470 TOPPTR(nss,ix) = sv_dup(sv, param);
11473 ptr = POPPTR(ss,ix);
11474 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11475 longval = (long)POPBOOL(ss,ix);
11476 TOPBOOL(nss,ix) = (bool)longval;
11478 case SAVEt_SET_SVFLAGS:
11480 TOPINT(nss,ix) = i;
11482 TOPINT(nss,ix) = i;
11483 sv = (SV*)POPPTR(ss,ix);
11484 TOPPTR(nss,ix) = sv_dup(sv, param);
11487 Perl_croak(aTHX_ "panic: ss_dup inconsistency");
11495 /* if sv is a stash, call $class->CLONE_SKIP(), and set the SVphv_CLONEABLE
11496 * flag to the result. This is done for each stash before cloning starts,
11497 * so we know which stashes want their objects cloned */
11500 do_mark_cloneable_stash(pTHX_ SV *sv)
11502 if (HvNAME((HV*)sv)) {
11503 GV* cloner = gv_fetchmethod_autoload((HV*)sv, "CLONE_SKIP", 0);
11504 SvFLAGS(sv) |= SVphv_CLONEABLE; /* clone objects by default */
11505 if (cloner && GvCV(cloner)) {
11512 XPUSHs(sv_2mortal(newSVpv(HvNAME((HV*)sv), 0)));
11514 call_sv((SV*)GvCV(cloner), G_SCALAR);
11521 SvFLAGS(sv) &= ~SVphv_CLONEABLE;
11529 =for apidoc perl_clone
11531 Create and return a new interpreter by cloning the current one.
11533 perl_clone takes these flags as parameters:
11535 CLONEf_COPY_STACKS - is used to, well, copy the stacks also,
11536 without it we only clone the data and zero the stacks,
11537 with it we copy the stacks and the new perl interpreter is
11538 ready to run at the exact same point as the previous one.
11539 The pseudo-fork code uses COPY_STACKS while the
11540 threads->new doesn't.
11542 CLONEf_KEEP_PTR_TABLE
11543 perl_clone keeps a ptr_table with the pointer of the old
11544 variable as a key and the new variable as a value,
11545 this allows it to check if something has been cloned and not
11546 clone it again but rather just use the value and increase the
11547 refcount. If KEEP_PTR_TABLE is not set then perl_clone will kill
11548 the ptr_table using the function
11549 C<ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;>,
11550 reason to keep it around is if you want to dup some of your own
11551 variable who are outside the graph perl scans, example of this
11552 code is in threads.xs create
11555 This is a win32 thing, it is ignored on unix, it tells perls
11556 win32host code (which is c++) to clone itself, this is needed on
11557 win32 if you want to run two threads at the same time,
11558 if you just want to do some stuff in a separate perl interpreter
11559 and then throw it away and return to the original one,
11560 you don't need to do anything.
11565 /* XXX the above needs expanding by someone who actually understands it ! */
11566 EXTERN_C PerlInterpreter *
11567 perl_clone_host(PerlInterpreter* proto_perl, UV flags);
11570 perl_clone(PerlInterpreter *proto_perl, UV flags)
11573 #ifdef PERL_IMPLICIT_SYS
11575 /* perlhost.h so we need to call into it
11576 to clone the host, CPerlHost should have a c interface, sky */
11578 if (flags & CLONEf_CLONE_HOST) {
11579 return perl_clone_host(proto_perl,flags);
11581 return perl_clone_using(proto_perl, flags,
11583 proto_perl->IMemShared,
11584 proto_perl->IMemParse,
11586 proto_perl->IStdIO,
11590 proto_perl->IProc);
11594 perl_clone_using(PerlInterpreter *proto_perl, UV flags,
11595 struct IPerlMem* ipM, struct IPerlMem* ipMS,
11596 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
11597 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
11598 struct IPerlDir* ipD, struct IPerlSock* ipS,
11599 struct IPerlProc* ipP)
11601 /* XXX many of the string copies here can be optimized if they're
11602 * constants; they need to be allocated as common memory and just
11603 * their pointers copied. */
11606 CLONE_PARAMS clone_params;
11607 CLONE_PARAMS* param = &clone_params;
11609 PerlInterpreter *my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
11610 /* for each stash, determine whether its objects should be cloned */
11611 S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
11612 PERL_SET_THX(my_perl);
11615 Poison(my_perl, 1, PerlInterpreter);
11617 PL_curcop = (COP *)Nullop;
11621 PL_savestack_ix = 0;
11622 PL_savestack_max = -1;
11623 PL_sig_pending = 0;
11624 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
11625 # else /* !DEBUGGING */
11626 Zero(my_perl, 1, PerlInterpreter);
11627 # endif /* DEBUGGING */
11629 /* host pointers */
11631 PL_MemShared = ipMS;
11632 PL_MemParse = ipMP;
11639 #else /* !PERL_IMPLICIT_SYS */
11641 CLONE_PARAMS clone_params;
11642 CLONE_PARAMS* param = &clone_params;
11643 PerlInterpreter *my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
11644 /* for each stash, determine whether its objects should be cloned */
11645 S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
11646 PERL_SET_THX(my_perl);
11649 Poison(my_perl, 1, PerlInterpreter);
11651 PL_curcop = (COP *)Nullop;
11655 PL_savestack_ix = 0;
11656 PL_savestack_max = -1;
11657 PL_sig_pending = 0;
11658 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
11659 # else /* !DEBUGGING */
11660 Zero(my_perl, 1, PerlInterpreter);
11661 # endif /* DEBUGGING */
11662 #endif /* PERL_IMPLICIT_SYS */
11663 param->flags = flags;
11664 param->proto_perl = proto_perl;
11667 PL_xiv_arenaroot = NULL;
11668 PL_xiv_root = NULL;
11669 PL_xnv_arenaroot = NULL;
11670 PL_xnv_root = NULL;
11671 PL_xrv_arenaroot = NULL;
11672 PL_xrv_root = NULL;
11673 PL_xpv_arenaroot = NULL;
11674 PL_xpv_root = NULL;
11675 PL_xpviv_arenaroot = NULL;
11676 PL_xpviv_root = NULL;
11677 PL_xpvnv_arenaroot = NULL;
11678 PL_xpvnv_root = NULL;
11679 PL_xpvcv_arenaroot = NULL;
11680 PL_xpvcv_root = NULL;
11681 PL_xpvav_arenaroot = NULL;
11682 PL_xpvav_root = NULL;
11683 PL_xpvhv_arenaroot = NULL;
11684 PL_xpvhv_root = NULL;
11685 PL_xpvmg_arenaroot = NULL;
11686 PL_xpvmg_root = NULL;
11687 PL_xpvlv_arenaroot = NULL;
11688 PL_xpvlv_root = NULL;
11689 PL_xpvbm_arenaroot = NULL;
11690 PL_xpvbm_root = NULL;
11691 PL_he_arenaroot = NULL;
11693 #if defined(USE_ITHREADS)
11694 PL_pte_arenaroot = NULL;
11695 PL_pte_root = NULL;
11697 PL_nice_chunk = NULL;
11698 PL_nice_chunk_size = 0;
11700 PL_sv_objcount = 0;
11701 PL_sv_root = Nullsv;
11702 PL_sv_arenaroot = Nullsv;
11704 PL_debug = proto_perl->Idebug;
11706 #ifdef USE_REENTRANT_API
11707 /* XXX: things like -Dm will segfault here in perlio, but doing
11708 * PERL_SET_CONTEXT(proto_perl);
11709 * breaks too many other things
11711 Perl_reentrant_init(aTHX);
11714 /* create SV map for pointer relocation */
11715 PL_ptr_table = ptr_table_new();
11717 /* initialize these special pointers as early as possible */
11718 SvANY(&PL_sv_undef) = NULL;
11719 SvREFCNT(&PL_sv_undef) = (~(U32)0)/2;
11720 SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL;
11721 ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef);
11723 SvANY(&PL_sv_no) = new_XPVNV();
11724 SvREFCNT(&PL_sv_no) = (~(U32)0)/2;
11725 SvFLAGS(&PL_sv_no) = SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
11726 |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
11727 SvPV_set(&PL_sv_no, SAVEPVN(PL_No, 0));
11728 SvCUR_set(&PL_sv_no, 0);
11729 SvLEN_set(&PL_sv_no, 1);
11730 SvIV_set(&PL_sv_no, 0);
11731 SvNV_set(&PL_sv_no, 0);
11732 ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no);
11734 SvANY(&PL_sv_yes) = new_XPVNV();
11735 SvREFCNT(&PL_sv_yes) = (~(U32)0)/2;
11736 SvFLAGS(&PL_sv_yes) = SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
11737 |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
11738 SvPV_set(&PL_sv_yes, SAVEPVN(PL_Yes, 1));
11739 SvCUR_set(&PL_sv_yes, 1);
11740 SvLEN_set(&PL_sv_yes, 2);
11741 SvIV_set(&PL_sv_yes, 1);
11742 SvNV_set(&PL_sv_yes, 1);
11743 ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes);
11745 /* create (a non-shared!) shared string table */
11746 PL_strtab = newHV();
11747 HvSHAREKEYS_off(PL_strtab);
11748 hv_ksplit(PL_strtab, 512);
11749 ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab);
11751 PL_compiling = proto_perl->Icompiling;
11753 /* These two PVs will be free'd special way so must set them same way op.c does */
11754 PL_compiling.cop_stashpv = savesharedpv(PL_compiling.cop_stashpv);
11755 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_stashpv, PL_compiling.cop_stashpv);
11757 PL_compiling.cop_file = savesharedpv(PL_compiling.cop_file);
11758 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_file, PL_compiling.cop_file);
11760 ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling);
11761 if (!specialWARN(PL_compiling.cop_warnings))
11762 PL_compiling.cop_warnings = sv_dup_inc(PL_compiling.cop_warnings, param);
11763 if (!specialCopIO(PL_compiling.cop_io))
11764 PL_compiling.cop_io = sv_dup_inc(PL_compiling.cop_io, param);
11765 PL_curcop = (COP*)any_dup(proto_perl->Tcurcop, proto_perl);
11767 /* pseudo environmental stuff */
11768 PL_origargc = proto_perl->Iorigargc;
11769 PL_origargv = proto_perl->Iorigargv;
11771 param->stashes = newAV(); /* Setup array of objects to call clone on */
11773 #ifdef PERLIO_LAYERS
11774 /* Clone PerlIO tables as soon as we can handle general xx_dup() */
11775 PerlIO_clone(aTHX_ proto_perl, param);
11778 PL_envgv = gv_dup(proto_perl->Ienvgv, param);
11779 PL_incgv = gv_dup(proto_perl->Iincgv, param);
11780 PL_hintgv = gv_dup(proto_perl->Ihintgv, param);
11781 PL_origfilename = SAVEPV(proto_perl->Iorigfilename);
11782 PL_diehook = sv_dup_inc(proto_perl->Idiehook, param);
11783 PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook, param);
11786 PL_minus_c = proto_perl->Iminus_c;
11787 PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel, param);
11788 PL_localpatches = proto_perl->Ilocalpatches;
11789 PL_splitstr = proto_perl->Isplitstr;
11790 PL_preprocess = proto_perl->Ipreprocess;
11791 PL_minus_n = proto_perl->Iminus_n;
11792 PL_minus_p = proto_perl->Iminus_p;
11793 PL_minus_l = proto_perl->Iminus_l;
11794 PL_minus_a = proto_perl->Iminus_a;
11795 PL_minus_F = proto_perl->Iminus_F;
11796 PL_doswitches = proto_perl->Idoswitches;
11797 PL_dowarn = proto_perl->Idowarn;
11798 PL_doextract = proto_perl->Idoextract;
11799 PL_sawampersand = proto_perl->Isawampersand;
11800 PL_unsafe = proto_perl->Iunsafe;
11801 PL_inplace = SAVEPV(proto_perl->Iinplace);
11802 PL_e_script = sv_dup_inc(proto_perl->Ie_script, param);
11803 PL_perldb = proto_perl->Iperldb;
11804 PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
11805 PL_exit_flags = proto_perl->Iexit_flags;
11807 /* magical thingies */
11808 /* XXX time(&PL_basetime) when asked for? */
11809 PL_basetime = proto_perl->Ibasetime;
11810 PL_formfeed = sv_dup(proto_perl->Iformfeed, param);
11812 PL_maxsysfd = proto_perl->Imaxsysfd;
11813 PL_multiline = proto_perl->Imultiline;
11814 PL_statusvalue = proto_perl->Istatusvalue;
11816 PL_statusvalue_vms = proto_perl->Istatusvalue_vms;
11818 PL_encoding = sv_dup(proto_perl->Iencoding, param);
11820 sv_setpvn(PERL_DEBUG_PAD(0), "", 0); /* For regex debugging. */
11821 sv_setpvn(PERL_DEBUG_PAD(1), "", 0); /* ext/re needs these */
11822 sv_setpvn(PERL_DEBUG_PAD(2), "", 0); /* even without DEBUGGING. */
11824 /* Clone the regex array */
11825 PL_regex_padav = newAV();
11827 I32 len = av_len((AV*)proto_perl->Iregex_padav);
11828 SV** regexen = AvARRAY((AV*)proto_perl->Iregex_padav);
11829 av_push(PL_regex_padav,
11830 sv_dup_inc(regexen[0],param));
11831 for(i = 1; i <= len; i++) {
11832 if(SvREPADTMP(regexen[i])) {
11833 av_push(PL_regex_padav, sv_dup_inc(regexen[i], param));
11835 av_push(PL_regex_padav,
11837 newSViv(PTR2IV(re_dup(INT2PTR(REGEXP *,
11838 SvIVX(regexen[i])), param)))
11843 PL_regex_pad = AvARRAY(PL_regex_padav);
11845 /* shortcuts to various I/O objects */
11846 PL_stdingv = gv_dup(proto_perl->Istdingv, param);
11847 PL_stderrgv = gv_dup(proto_perl->Istderrgv, param);
11848 PL_defgv = gv_dup(proto_perl->Idefgv, param);
11849 PL_argvgv = gv_dup(proto_perl->Iargvgv, param);
11850 PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv, param);
11851 PL_argvout_stack = av_dup_inc(proto_perl->Iargvout_stack, param);
11853 /* shortcuts to regexp stuff */
11854 PL_replgv = gv_dup(proto_perl->Ireplgv, param);
11856 /* shortcuts to misc objects */
11857 PL_errgv = gv_dup(proto_perl->Ierrgv, param);
11859 /* shortcuts to debugging objects */
11860 PL_DBgv = gv_dup(proto_perl->IDBgv, param);
11861 PL_DBline = gv_dup(proto_perl->IDBline, param);
11862 PL_DBsub = gv_dup(proto_perl->IDBsub, param);
11863 PL_DBsingle = sv_dup(proto_perl->IDBsingle, param);
11864 PL_DBtrace = sv_dup(proto_perl->IDBtrace, param);
11865 PL_DBsignal = sv_dup(proto_perl->IDBsignal, param);
11866 PL_DBassertion = sv_dup(proto_perl->IDBassertion, param);
11867 PL_lineary = av_dup(proto_perl->Ilineary, param);
11868 PL_dbargs = av_dup(proto_perl->Idbargs, param);
11870 /* symbol tables */
11871 PL_defstash = hv_dup_inc(proto_perl->Tdefstash, param);
11872 PL_curstash = hv_dup(proto_perl->Tcurstash, param);
11873 PL_debstash = hv_dup(proto_perl->Idebstash, param);
11874 PL_globalstash = hv_dup(proto_perl->Iglobalstash, param);
11875 PL_curstname = sv_dup_inc(proto_perl->Icurstname, param);
11877 PL_beginav = av_dup_inc(proto_perl->Ibeginav, param);
11878 PL_beginav_save = av_dup_inc(proto_perl->Ibeginav_save, param);
11879 PL_checkav_save = av_dup_inc(proto_perl->Icheckav_save, param);
11880 PL_endav = av_dup_inc(proto_perl->Iendav, param);
11881 PL_checkav = av_dup_inc(proto_perl->Icheckav, param);
11882 PL_initav = av_dup_inc(proto_perl->Iinitav, param);
11884 PL_sub_generation = proto_perl->Isub_generation;
11886 /* funky return mechanisms */
11887 PL_forkprocess = proto_perl->Iforkprocess;
11889 /* subprocess state */
11890 PL_fdpid = av_dup_inc(proto_perl->Ifdpid, param);
11892 /* internal state */
11893 PL_tainting = proto_perl->Itainting;
11894 PL_taint_warn = proto_perl->Itaint_warn;
11895 PL_maxo = proto_perl->Imaxo;
11896 if (proto_perl->Iop_mask)
11897 PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo);
11899 PL_op_mask = Nullch;
11900 /* PL_asserting = proto_perl->Iasserting; */
11902 /* current interpreter roots */
11903 PL_main_cv = cv_dup_inc(proto_perl->Imain_cv, param);
11904 PL_main_root = OpREFCNT_inc(proto_perl->Imain_root);
11905 PL_main_start = proto_perl->Imain_start;
11906 PL_eval_root = proto_perl->Ieval_root;
11907 PL_eval_start = proto_perl->Ieval_start;
11909 /* runtime control stuff */
11910 PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl);
11911 PL_copline = proto_perl->Icopline;
11913 PL_filemode = proto_perl->Ifilemode;
11914 PL_lastfd = proto_perl->Ilastfd;
11915 PL_oldname = proto_perl->Ioldname; /* XXX not quite right */
11918 PL_gensym = proto_perl->Igensym;
11919 PL_preambled = proto_perl->Ipreambled;
11920 PL_preambleav = av_dup_inc(proto_perl->Ipreambleav, param);
11921 PL_laststatval = proto_perl->Ilaststatval;
11922 PL_laststype = proto_perl->Ilaststype;
11923 PL_mess_sv = Nullsv;
11925 PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv, param);
11926 PL_ofmt = SAVEPV(proto_perl->Iofmt);
11928 /* interpreter atexit processing */
11929 PL_exitlistlen = proto_perl->Iexitlistlen;
11930 if (PL_exitlistlen) {
11931 New(0, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
11932 Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
11935 PL_exitlist = (PerlExitListEntry*)NULL;
11936 PL_modglobal = hv_dup_inc(proto_perl->Imodglobal, param);
11937 PL_custom_op_names = hv_dup_inc(proto_perl->Icustom_op_names,param);
11938 PL_custom_op_descs = hv_dup_inc(proto_perl->Icustom_op_descs,param);
11940 PL_profiledata = NULL;
11941 PL_rsfp = fp_dup(proto_perl->Irsfp, '<', param);
11942 /* PL_rsfp_filters entries have fake IoDIRP() */
11943 PL_rsfp_filters = av_dup_inc(proto_perl->Irsfp_filters, param);
11945 PL_compcv = cv_dup(proto_perl->Icompcv, param);
11947 PAD_CLONE_VARS(proto_perl, param);
11949 #ifdef HAVE_INTERP_INTERN
11950 sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern);
11953 /* more statics moved here */
11954 PL_generation = proto_perl->Igeneration;
11955 PL_DBcv = cv_dup(proto_perl->IDBcv, param);
11957 PL_in_clean_objs = proto_perl->Iin_clean_objs;
11958 PL_in_clean_all = proto_perl->Iin_clean_all;
11960 PL_uid = proto_perl->Iuid;
11961 PL_euid = proto_perl->Ieuid;
11962 PL_gid = proto_perl->Igid;
11963 PL_egid = proto_perl->Iegid;
11964 PL_nomemok = proto_perl->Inomemok;
11965 PL_an = proto_perl->Ian;
11966 PL_evalseq = proto_perl->Ievalseq;
11967 PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */
11968 PL_origalen = proto_perl->Iorigalen;
11969 PL_pidstatus = newHV(); /* XXX flag for cloning? */
11970 PL_osname = SAVEPV(proto_perl->Iosname);
11971 PL_sh_path_compat = proto_perl->Ish_path_compat; /* XXX never deallocated */
11972 PL_sighandlerp = proto_perl->Isighandlerp;
11975 PL_runops = proto_perl->Irunops;
11977 Copy(proto_perl->Itokenbuf, PL_tokenbuf, 256, char);
11980 PL_cshlen = proto_perl->Icshlen;
11981 PL_cshname = proto_perl->Icshname; /* XXX never deallocated */
11984 PL_lex_state = proto_perl->Ilex_state;
11985 PL_lex_defer = proto_perl->Ilex_defer;
11986 PL_lex_expect = proto_perl->Ilex_expect;
11987 PL_lex_formbrack = proto_perl->Ilex_formbrack;
11988 PL_lex_dojoin = proto_perl->Ilex_dojoin;
11989 PL_lex_starts = proto_perl->Ilex_starts;
11990 PL_lex_stuff = sv_dup_inc(proto_perl->Ilex_stuff, param);
11991 PL_lex_repl = sv_dup_inc(proto_perl->Ilex_repl, param);
11992 PL_lex_op = proto_perl->Ilex_op;
11993 PL_lex_inpat = proto_perl->Ilex_inpat;
11994 PL_lex_inwhat = proto_perl->Ilex_inwhat;
11995 PL_lex_brackets = proto_perl->Ilex_brackets;
11996 i = (PL_lex_brackets < 120 ? 120 : PL_lex_brackets);
11997 PL_lex_brackstack = SAVEPVN(proto_perl->Ilex_brackstack,i);
11998 PL_lex_casemods = proto_perl->Ilex_casemods;
11999 i = (PL_lex_casemods < 12 ? 12 : PL_lex_casemods);
12000 PL_lex_casestack = SAVEPVN(proto_perl->Ilex_casestack,i);
12002 Copy(proto_perl->Inextval, PL_nextval, 5, YYSTYPE);
12003 Copy(proto_perl->Inexttype, PL_nexttype, 5, I32);
12004 PL_nexttoke = proto_perl->Inexttoke;
12006 /* XXX This is probably masking the deeper issue of why
12007 * SvANY(proto_perl->Ilinestr) can be NULL at this point. For test case:
12008 * http://archive.develooper.com/perl5-porters%40perl.org/msg83298.html
12009 * (A little debugging with a watchpoint on it may help.)
12011 if (SvANY(proto_perl->Ilinestr)) {
12012 PL_linestr = sv_dup_inc(proto_perl->Ilinestr, param);
12013 i = proto_perl->Ibufptr - SvPVX(proto_perl->Ilinestr);
12014 PL_bufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
12015 i = proto_perl->Ioldbufptr - SvPVX(proto_perl->Ilinestr);
12016 PL_oldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
12017 i = proto_perl->Ioldoldbufptr - SvPVX(proto_perl->Ilinestr);
12018 PL_oldoldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
12019 i = proto_perl->Ilinestart - SvPVX(proto_perl->Ilinestr);
12020 PL_linestart = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
12023 PL_linestr = NEWSV(65,79);
12024 sv_upgrade(PL_linestr,SVt_PVIV);
12025 sv_setpvn(PL_linestr,"",0);
12026 PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart = SvPVX(PL_linestr);
12028 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
12029 PL_pending_ident = proto_perl->Ipending_ident;
12030 PL_sublex_info = proto_perl->Isublex_info; /* XXX not quite right */
12032 PL_expect = proto_perl->Iexpect;
12034 PL_multi_start = proto_perl->Imulti_start;
12035 PL_multi_end = proto_perl->Imulti_end;
12036 PL_multi_open = proto_perl->Imulti_open;
12037 PL_multi_close = proto_perl->Imulti_close;
12039 PL_error_count = proto_perl->Ierror_count;
12040 PL_subline = proto_perl->Isubline;
12041 PL_subname = sv_dup_inc(proto_perl->Isubname, param);
12043 /* XXX See comment on SvANY(proto_perl->Ilinestr) above */
12044 if (SvANY(proto_perl->Ilinestr)) {
12045 i = proto_perl->Ilast_uni - SvPVX(proto_perl->Ilinestr);
12046 PL_last_uni = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
12047 i = proto_perl->Ilast_lop - SvPVX(proto_perl->Ilinestr);
12048 PL_last_lop = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
12049 PL_last_lop_op = proto_perl->Ilast_lop_op;
12052 PL_last_uni = SvPVX(PL_linestr);
12053 PL_last_lop = SvPVX(PL_linestr);
12054 PL_last_lop_op = 0;
12056 PL_in_my = proto_perl->Iin_my;
12057 PL_in_my_stash = hv_dup(proto_perl->Iin_my_stash, param);
12059 PL_cryptseen = proto_perl->Icryptseen;
12062 PL_hints = proto_perl->Ihints;
12064 PL_amagic_generation = proto_perl->Iamagic_generation;
12066 #ifdef USE_LOCALE_COLLATE
12067 PL_collation_ix = proto_perl->Icollation_ix;
12068 PL_collation_name = SAVEPV(proto_perl->Icollation_name);
12069 PL_collation_standard = proto_perl->Icollation_standard;
12070 PL_collxfrm_base = proto_perl->Icollxfrm_base;
12071 PL_collxfrm_mult = proto_perl->Icollxfrm_mult;
12072 #endif /* USE_LOCALE_COLLATE */
12074 #ifdef USE_LOCALE_NUMERIC
12075 PL_numeric_name = SAVEPV(proto_perl->Inumeric_name);
12076 PL_numeric_standard = proto_perl->Inumeric_standard;
12077 PL_numeric_local = proto_perl->Inumeric_local;
12078 PL_numeric_radix_sv = sv_dup_inc(proto_perl->Inumeric_radix_sv, param);
12079 #endif /* !USE_LOCALE_NUMERIC */
12081 /* utf8 character classes */
12082 PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum, param);
12083 PL_utf8_alnumc = sv_dup_inc(proto_perl->Iutf8_alnumc, param);
12084 PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii, param);
12085 PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha, param);
12086 PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space, param);
12087 PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl, param);
12088 PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph, param);
12089 PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit, param);
12090 PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper, param);
12091 PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower, param);
12092 PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print, param);
12093 PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct, param);
12094 PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit, param);
12095 PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark, param);
12096 PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper, param);
12097 PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle, param);
12098 PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower, param);
12099 PL_utf8_tofold = sv_dup_inc(proto_perl->Iutf8_tofold, param);
12100 PL_utf8_idstart = sv_dup_inc(proto_perl->Iutf8_idstart, param);
12101 PL_utf8_idcont = sv_dup_inc(proto_perl->Iutf8_idcont, param);
12103 /* Did the locale setup indicate UTF-8? */
12104 PL_utf8locale = proto_perl->Iutf8locale;
12105 /* Unicode features (see perlrun/-C) */
12106 PL_unicode = proto_perl->Iunicode;
12108 /* Pre-5.8 signals control */
12109 PL_signals = proto_perl->Isignals;
12111 /* times() ticks per second */
12112 PL_clocktick = proto_perl->Iclocktick;
12114 /* Recursion stopper for PerlIO_find_layer */
12115 PL_in_load_module = proto_perl->Iin_load_module;
12117 /* sort() routine */
12118 PL_sort_RealCmp = proto_perl->Isort_RealCmp;
12120 /* Not really needed/useful since the reenrant_retint is "volatile",
12121 * but do it for consistency's sake. */
12122 PL_reentrant_retint = proto_perl->Ireentrant_retint;
12124 /* Hooks to shared SVs and locks. */
12125 PL_sharehook = proto_perl->Isharehook;
12126 PL_lockhook = proto_perl->Ilockhook;
12127 PL_unlockhook = proto_perl->Iunlockhook;
12128 PL_threadhook = proto_perl->Ithreadhook;
12130 PL_runops_std = proto_perl->Irunops_std;
12131 PL_runops_dbg = proto_perl->Irunops_dbg;
12133 #ifdef THREADS_HAVE_PIDS
12134 PL_ppid = proto_perl->Ippid;
12138 PL_last_swash_hv = Nullhv; /* reinits on demand */
12139 PL_last_swash_klen = 0;
12140 PL_last_swash_key[0]= '\0';
12141 PL_last_swash_tmps = (U8*)NULL;
12142 PL_last_swash_slen = 0;
12144 PL_glob_index = proto_perl->Iglob_index;
12145 PL_srand_called = proto_perl->Isrand_called;
12146 PL_hash_seed = proto_perl->Ihash_seed;
12147 PL_rehash_seed = proto_perl->Irehash_seed;
12148 PL_uudmap['M'] = 0; /* reinits on demand */
12149 PL_bitcount = Nullch; /* reinits on demand */
12151 if (proto_perl->Ipsig_pend) {
12152 Newz(0, PL_psig_pend, SIG_SIZE, int);
12155 PL_psig_pend = (int*)NULL;
12158 if (proto_perl->Ipsig_ptr) {
12159 Newz(0, PL_psig_ptr, SIG_SIZE, SV*);
12160 Newz(0, PL_psig_name, SIG_SIZE, SV*);
12161 for (i = 1; i < SIG_SIZE; i++) {
12162 PL_psig_ptr[i] = sv_dup_inc(proto_perl->Ipsig_ptr[i], param);
12163 PL_psig_name[i] = sv_dup_inc(proto_perl->Ipsig_name[i], param);
12167 PL_psig_ptr = (SV**)NULL;
12168 PL_psig_name = (SV**)NULL;
12171 /* thrdvar.h stuff */
12173 if (flags & CLONEf_COPY_STACKS) {
12174 /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */
12175 PL_tmps_ix = proto_perl->Ttmps_ix;
12176 PL_tmps_max = proto_perl->Ttmps_max;
12177 PL_tmps_floor = proto_perl->Ttmps_floor;
12178 Newz(50, PL_tmps_stack, PL_tmps_max, SV*);
12180 while (i <= PL_tmps_ix) {
12181 PL_tmps_stack[i] = sv_dup_inc(proto_perl->Ttmps_stack[i], param);
12185 /* next PUSHMARK() sets *(PL_markstack_ptr+1) */
12186 i = proto_perl->Tmarkstack_max - proto_perl->Tmarkstack;
12187 Newz(54, PL_markstack, i, I32);
12188 PL_markstack_max = PL_markstack + (proto_perl->Tmarkstack_max
12189 - proto_perl->Tmarkstack);
12190 PL_markstack_ptr = PL_markstack + (proto_perl->Tmarkstack_ptr
12191 - proto_perl->Tmarkstack);
12192 Copy(proto_perl->Tmarkstack, PL_markstack,
12193 PL_markstack_ptr - PL_markstack + 1, I32);
12195 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
12196 * NOTE: unlike the others! */
12197 PL_scopestack_ix = proto_perl->Tscopestack_ix;
12198 PL_scopestack_max = proto_perl->Tscopestack_max;
12199 Newz(54, PL_scopestack, PL_scopestack_max, I32);
12200 Copy(proto_perl->Tscopestack, PL_scopestack, PL_scopestack_ix, I32);
12202 /* NOTE: si_dup() looks at PL_markstack */
12203 PL_curstackinfo = si_dup(proto_perl->Tcurstackinfo, param);
12205 /* PL_curstack = PL_curstackinfo->si_stack; */
12206 PL_curstack = av_dup(proto_perl->Tcurstack, param);
12207 PL_mainstack = av_dup(proto_perl->Tmainstack, param);
12209 /* next PUSHs() etc. set *(PL_stack_sp+1) */
12210 PL_stack_base = AvARRAY(PL_curstack);
12211 PL_stack_sp = PL_stack_base + (proto_perl->Tstack_sp
12212 - proto_perl->Tstack_base);
12213 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
12215 /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix]
12216 * NOTE: unlike the others! */
12217 PL_savestack_ix = proto_perl->Tsavestack_ix;
12218 PL_savestack_max = proto_perl->Tsavestack_max;
12219 /*Newz(54, PL_savestack, PL_savestack_max, ANY);*/
12220 PL_savestack = ss_dup(proto_perl, param);
12224 ENTER; /* perl_destruct() wants to LEAVE; */
12227 PL_start_env = proto_perl->Tstart_env; /* XXXXXX */
12228 PL_top_env = &PL_start_env;
12230 PL_op = proto_perl->Top;
12233 PL_Xpv = (XPV*)NULL;
12234 PL_na = proto_perl->Tna;
12236 PL_statbuf = proto_perl->Tstatbuf;
12237 PL_statcache = proto_perl->Tstatcache;
12238 PL_statgv = gv_dup(proto_perl->Tstatgv, param);
12239 PL_statname = sv_dup_inc(proto_perl->Tstatname, param);
12241 PL_timesbuf = proto_perl->Ttimesbuf;
12244 PL_tainted = proto_perl->Ttainted;
12245 PL_curpm = proto_perl->Tcurpm; /* XXX No PMOP ref count */
12246 PL_rs = sv_dup_inc(proto_perl->Trs, param);
12247 PL_last_in_gv = gv_dup(proto_perl->Tlast_in_gv, param);
12248 PL_ofs_sv = sv_dup_inc(proto_perl->Tofs_sv, param);
12249 PL_defoutgv = gv_dup_inc(proto_perl->Tdefoutgv, param);
12250 PL_chopset = proto_perl->Tchopset; /* XXX never deallocated */
12251 PL_toptarget = sv_dup_inc(proto_perl->Ttoptarget, param);
12252 PL_bodytarget = sv_dup_inc(proto_perl->Tbodytarget, param);
12253 PL_formtarget = sv_dup(proto_perl->Tformtarget, param);
12255 PL_restartop = proto_perl->Trestartop;
12256 PL_in_eval = proto_perl->Tin_eval;
12257 PL_delaymagic = proto_perl->Tdelaymagic;
12258 PL_dirty = proto_perl->Tdirty;
12259 PL_localizing = proto_perl->Tlocalizing;
12261 PL_errors = sv_dup_inc(proto_perl->Terrors, param);
12262 PL_hv_fetch_ent_mh = Nullhe;
12263 PL_modcount = proto_perl->Tmodcount;
12264 PL_lastgotoprobe = Nullop;
12265 PL_dumpindent = proto_perl->Tdumpindent;
12267 PL_sortcop = (OP*)any_dup(proto_perl->Tsortcop, proto_perl);
12268 PL_sortstash = hv_dup(proto_perl->Tsortstash, param);
12269 PL_firstgv = gv_dup(proto_perl->Tfirstgv, param);
12270 PL_secondgv = gv_dup(proto_perl->Tsecondgv, param);
12271 PL_sortcxix = proto_perl->Tsortcxix;
12272 PL_efloatbuf = Nullch; /* reinits on demand */
12273 PL_efloatsize = 0; /* reinits on demand */
12277 PL_screamfirst = NULL;
12278 PL_screamnext = NULL;
12279 PL_maxscream = -1; /* reinits on demand */
12280 PL_lastscream = Nullsv;
12282 PL_watchaddr = NULL;
12283 PL_watchok = Nullch;
12285 PL_regdummy = proto_perl->Tregdummy;
12286 PL_regprecomp = Nullch;
12289 PL_colorset = 0; /* reinits PL_colors[] */
12290 /*PL_colors[6] = {0,0,0,0,0,0};*/
12291 PL_reginput = Nullch;
12292 PL_regbol = Nullch;
12293 PL_regeol = Nullch;
12294 PL_regstartp = (I32*)NULL;
12295 PL_regendp = (I32*)NULL;
12296 PL_reglastparen = (U32*)NULL;
12297 PL_reglastcloseparen = (U32*)NULL;
12298 PL_regtill = Nullch;
12299 PL_reg_start_tmp = (char**)NULL;
12300 PL_reg_start_tmpl = 0;
12301 PL_regdata = (struct reg_data*)NULL;
12304 PL_reg_eval_set = 0;
12306 PL_regprogram = (regnode*)NULL;
12308 PL_regcc = (CURCUR*)NULL;
12309 PL_reg_call_cc = (struct re_cc_state*)NULL;
12310 PL_reg_re = (regexp*)NULL;
12311 PL_reg_ganch = Nullch;
12312 PL_reg_sv = Nullsv;
12313 PL_reg_match_utf8 = FALSE;
12314 PL_reg_magic = (MAGIC*)NULL;
12316 PL_reg_oldcurpm = (PMOP*)NULL;
12317 PL_reg_curpm = (PMOP*)NULL;
12318 PL_reg_oldsaved = Nullch;
12319 PL_reg_oldsavedlen = 0;
12320 #ifdef PERL_COPY_ON_WRITE
12323 PL_reg_maxiter = 0;
12324 PL_reg_leftiter = 0;
12325 PL_reg_poscache = Nullch;
12326 PL_reg_poscache_size= 0;
12328 /* RE engine - function pointers */
12329 PL_regcompp = proto_perl->Tregcompp;
12330 PL_regexecp = proto_perl->Tregexecp;
12331 PL_regint_start = proto_perl->Tregint_start;
12332 PL_regint_string = proto_perl->Tregint_string;
12333 PL_regfree = proto_perl->Tregfree;
12335 PL_reginterp_cnt = 0;
12336 PL_reg_starttry = 0;
12338 /* Pluggable optimizer */
12339 PL_peepp = proto_perl->Tpeepp;
12341 PL_stashcache = newHV();
12343 if (!(flags & CLONEf_KEEP_PTR_TABLE)) {
12344 ptr_table_free(PL_ptr_table);
12345 PL_ptr_table = NULL;
12348 /* Call the ->CLONE method, if it exists, for each of the stashes
12349 identified by sv_dup() above.
12351 while(av_len(param->stashes) != -1) {
12352 HV* stash = (HV*) av_shift(param->stashes);
12353 GV* cloner = gv_fetchmethod_autoload(stash, "CLONE", 0);
12354 if (cloner && GvCV(cloner)) {
12359 XPUSHs(sv_2mortal(newSVpv(HvNAME(stash), 0)));
12361 call_sv((SV*)GvCV(cloner), G_DISCARD);
12367 SvREFCNT_dec(param->stashes);
12372 #endif /* USE_ITHREADS */
12375 =head1 Unicode Support
12377 =for apidoc sv_recode_to_utf8
12379 The encoding is assumed to be an Encode object, on entry the PV
12380 of the sv is assumed to be octets in that encoding, and the sv
12381 will be converted into Unicode (and UTF-8).
12383 If the sv already is UTF-8 (or if it is not POK), or if the encoding
12384 is not a reference, nothing is done to the sv. If the encoding is not
12385 an C<Encode::XS> Encoding object, bad things will happen.
12386 (See F<lib/encoding.pm> and L<Encode>).
12388 The PV of the sv is returned.
12393 Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding)
12396 if (SvPOK(sv) && !SvUTF8(sv) && !IN_BYTES && SvROK(encoding)) {
12410 Passing sv_yes is wrong - it needs to be or'ed set of constants
12411 for Encode::XS, while UTf-8 decode (currently) assumes a true value means
12412 remove converted chars from source.
12414 Both will default the value - let them.
12416 XPUSHs(&PL_sv_yes);
12419 call_method("decode", G_SCALAR);
12423 s = SvPV(uni, len);
12424 if (s != SvPVX(sv)) {
12425 SvGROW(sv, len + 1);
12426 Move(s, SvPVX(sv), len, char);
12427 SvCUR_set(sv, len);
12428 SvPVX(sv)[len] = 0;
12435 return SvPOKp(sv) ? SvPVX(sv) : NULL;
12439 =for apidoc sv_cat_decode
12441 The encoding is assumed to be an Encode object, the PV of the ssv is
12442 assumed to be octets in that encoding and decoding the input starts
12443 from the position which (PV + *offset) pointed to. The dsv will be
12444 concatenated the decoded UTF-8 string from ssv. Decoding will terminate
12445 when the string tstr appears in decoding output or the input ends on
12446 the PV of the ssv. The value which the offset points will be modified
12447 to the last input position on the ssv.
12449 Returns TRUE if the terminator was found, else returns FALSE.
12454 Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding,
12455 SV *ssv, int *offset, char *tstr, int tlen)
12459 if (SvPOK(ssv) && SvPOK(dsv) && SvROK(encoding) && offset) {
12470 XPUSHs(offsv = sv_2mortal(newSViv(*offset)));
12471 XPUSHs(sv_2mortal(newSVpvn(tstr, tlen)));
12473 call_method("cat_decode", G_SCALAR);
12475 ret = SvTRUE(TOPs);
12476 *offset = SvIV(offsv);
12482 Perl_croak(aTHX_ "Invalid argument to sv_cat_decode");
12488 * c-indentation-style: bsd
12489 * c-basic-offset: 4
12490 * indent-tabs-mode: t
12493 * vim: shiftwidth=4: