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 are approximately
67 1K chunks of memory parcelled up into N heads or bodies. The first slot
68 in each arena is reserved, and is used to hold a link to the next arena.
69 In the case of heads, the unused first slot also contains some flags and
70 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
75 The following global variables are associated with arenas:
77 PL_sv_arenaroot pointer to list of SV arenas
78 PL_sv_root pointer to list of free SV structures
80 PL_foo_arenaroot pointer to list of foo arenas,
81 PL_foo_root pointer to list of free foo bodies
82 ... for foo in xiv, xnv, xrv, xpv etc.
84 Note that some of the larger and more rarely used body types (eg xpvio)
85 are not allocated using arenas, but are instead just malloc()/free()ed as
86 required. Also, if PURIFY is defined, arenas are abandoned altogether,
87 with all items individually malloc()ed. In addition, a few SV heads are
88 not allocated from an arena, but are instead directly created as static
89 or auto variables, eg PL_sv_undef.
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)
274 if (ckWARN_d(WARN_INTERNAL))
275 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
276 "Attempt to free non-arena SV: 0x%"UVxf
277 pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
284 #else /* ! DEBUGGING */
286 #define del_SV(p) plant_SV(p)
288 #endif /* DEBUGGING */
292 =head1 SV Manipulation Functions
294 =for apidoc sv_add_arena
296 Given a chunk of memory, link it to the head of the list of arenas,
297 and split it into a list of free SVs.
303 Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
309 /* The first SV in an arena isn't an SV. */
310 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
311 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
312 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
314 PL_sv_arenaroot = sva;
315 PL_sv_root = sva + 1;
317 svend = &sva[SvREFCNT(sva) - 1];
320 SvANY(sv) = (void *)(SV*)(sv + 1);
322 SvFLAGS(sv) = SVTYPEMASK;
326 SvFLAGS(sv) = SVTYPEMASK;
329 /* make some more SVs by adding another arena */
331 /* sv_mutex must be held while calling more_sv() */
338 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
339 PL_nice_chunk = Nullch;
340 PL_nice_chunk_size = 0;
343 char *chunk; /* must use New here to match call to */
344 New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */
345 sv_add_arena(chunk, 1008, 0);
351 /* visit(): call the named function for each non-free SV in the arenas
352 * whose flags field matches the flags/mask args. */
355 S_visit(pTHX_ SVFUNC_t f, U32 flags, U32 mask)
362 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
363 svend = &sva[SvREFCNT(sva)];
364 for (sv = sva + 1; sv < svend; ++sv) {
365 if (SvTYPE(sv) != SVTYPEMASK
366 && (sv->sv_flags & mask) == flags
379 /* called by sv_report_used() for each live SV */
382 do_report_used(pTHX_ SV *sv)
384 if (SvTYPE(sv) != SVTYPEMASK) {
385 PerlIO_printf(Perl_debug_log, "****\n");
392 =for apidoc sv_report_used
394 Dump the contents of all SVs not yet freed. (Debugging aid).
400 Perl_sv_report_used(pTHX)
403 visit(do_report_used, 0, 0);
407 /* called by sv_clean_objs() for each live SV */
410 do_clean_objs(pTHX_ SV *sv)
414 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
415 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv)));
427 /* XXX Might want to check arrays, etc. */
430 /* called by sv_clean_objs() for each live SV */
432 #ifndef DISABLE_DESTRUCTOR_KLUDGE
434 do_clean_named_objs(pTHX_ SV *sv)
436 if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) {
437 if ( SvOBJECT(GvSV(sv)) ||
438 (GvAV(sv) && SvOBJECT(GvAV(sv))) ||
439 (GvHV(sv) && SvOBJECT(GvHV(sv))) ||
440 (GvIO(sv) && SvOBJECT(GvIO(sv))) ||
441 (GvCV(sv) && SvOBJECT(GvCV(sv))) )
443 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv)));
444 SvFLAGS(sv) |= SVf_BREAK;
452 =for apidoc sv_clean_objs
454 Attempt to destroy all objects not yet freed
460 Perl_sv_clean_objs(pTHX)
462 PL_in_clean_objs = TRUE;
463 visit(do_clean_objs, SVf_ROK, SVf_ROK);
464 #ifndef DISABLE_DESTRUCTOR_KLUDGE
465 /* some barnacles may yet remain, clinging to typeglobs */
466 visit(do_clean_named_objs, SVt_PVGV, SVTYPEMASK);
468 PL_in_clean_objs = FALSE;
471 /* called by sv_clean_all() for each live SV */
474 do_clean_all(pTHX_ SV *sv)
476 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
477 SvFLAGS(sv) |= SVf_BREAK;
478 if (PL_comppad == (AV*)sv) {
480 PL_curpad = Null(SV**);
486 =for apidoc sv_clean_all
488 Decrement the refcnt of each remaining SV, possibly triggering a
489 cleanup. This function may have to be called multiple times to free
490 SVs which are in complex self-referential hierarchies.
496 Perl_sv_clean_all(pTHX)
499 PL_in_clean_all = TRUE;
500 cleaned = visit(do_clean_all, 0,0);
501 PL_in_clean_all = FALSE;
506 =for apidoc sv_free_arenas
508 Deallocate the memory used by all arenas. Note that all the individual SV
509 heads and bodies within the arenas must already have been freed.
515 Perl_sv_free_arenas(pTHX)
519 XPV *arena, *arenanext;
521 /* Free arenas here, but be careful about fake ones. (We assume
522 contiguity of the fake ones with the corresponding real ones.) */
524 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
525 svanext = (SV*) SvANY(sva);
526 while (svanext && SvFAKE(svanext))
527 svanext = (SV*) SvANY(svanext);
530 Safefree((void *)sva);
533 for (arena = PL_xiv_arenaroot; arena; arena = arenanext) {
534 arenanext = (XPV*)arena->xpv_pv;
537 PL_xiv_arenaroot = 0;
540 for (arena = PL_xnv_arenaroot; arena; arena = arenanext) {
541 arenanext = (XPV*)arena->xpv_pv;
544 PL_xnv_arenaroot = 0;
547 for (arena = PL_xrv_arenaroot; arena; arena = arenanext) {
548 arenanext = (XPV*)arena->xpv_pv;
551 PL_xrv_arenaroot = 0;
554 for (arena = PL_xpv_arenaroot; arena; arena = arenanext) {
555 arenanext = (XPV*)arena->xpv_pv;
558 PL_xpv_arenaroot = 0;
561 for (arena = (XPV*)PL_xpviv_arenaroot; arena; arena = arenanext) {
562 arenanext = (XPV*)arena->xpv_pv;
565 PL_xpviv_arenaroot = 0;
568 for (arena = (XPV*)PL_xpvnv_arenaroot; arena; arena = arenanext) {
569 arenanext = (XPV*)arena->xpv_pv;
572 PL_xpvnv_arenaroot = 0;
575 for (arena = (XPV*)PL_xpvcv_arenaroot; arena; arena = arenanext) {
576 arenanext = (XPV*)arena->xpv_pv;
579 PL_xpvcv_arenaroot = 0;
582 for (arena = (XPV*)PL_xpvav_arenaroot; arena; arena = arenanext) {
583 arenanext = (XPV*)arena->xpv_pv;
586 PL_xpvav_arenaroot = 0;
589 for (arena = (XPV*)PL_xpvhv_arenaroot; arena; arena = arenanext) {
590 arenanext = (XPV*)arena->xpv_pv;
593 PL_xpvhv_arenaroot = 0;
596 for (arena = (XPV*)PL_xpvmg_arenaroot; arena; arena = arenanext) {
597 arenanext = (XPV*)arena->xpv_pv;
600 PL_xpvmg_arenaroot = 0;
603 for (arena = (XPV*)PL_xpvlv_arenaroot; arena; arena = arenanext) {
604 arenanext = (XPV*)arena->xpv_pv;
607 PL_xpvlv_arenaroot = 0;
610 for (arena = (XPV*)PL_xpvbm_arenaroot; arena; arena = arenanext) {
611 arenanext = (XPV*)arena->xpv_pv;
614 PL_xpvbm_arenaroot = 0;
617 for (arena = (XPV*)PL_he_arenaroot; arena; arena = arenanext) {
618 arenanext = (XPV*)arena->xpv_pv;
625 Safefree(PL_nice_chunk);
626 PL_nice_chunk = Nullch;
627 PL_nice_chunk_size = 0;
632 /* ---------------------------------------------------------------------
634 * support functions for report_uninit()
637 /* the maxiumum size of array or hash where we will scan looking
638 * for the undefined element that triggered the warning */
640 #define FUV_MAX_SEARCH_SIZE 1000
642 /* Look for an entry in the hash whose value has the same SV as val;
643 * If so, return a mortal copy of the key. */
646 S_find_hash_subscript(pTHX_ HV *hv, SV* val)
652 if (!hv || SvMAGICAL(hv) || !HvARRAY(hv) ||
653 (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
658 for (i=HvMAX(hv); i>0; i--) {
659 for (entry = array[i]; entry; entry = HeNEXT(entry)) {
660 if (HeVAL(entry) != val)
662 if ( HeVAL(entry) == &PL_sv_undef ||
663 HeVAL(entry) == &PL_sv_placeholder)
667 if (HeKLEN(entry) == HEf_SVKEY)
668 return sv_mortalcopy(HeKEY_sv(entry));
669 return sv_2mortal(newSVpvn(HeKEY(entry), HeKLEN(entry)));
675 /* Look for an entry in the array whose value has the same SV as val;
676 * If so, return the index, otherwise return -1. */
679 S_find_array_subscript(pTHX_ AV *av, SV* val)
683 if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
684 (AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
688 for (i=AvFILLp(av); i>=0; i--) {
689 if (svp[i] == val && svp[i] != &PL_sv_undef)
695 /* S_varname(): return the name of a variable, optionally with a subscript.
696 * If gv is non-zero, use the name of that global, along with gvtype (one
697 * of "$", "@", "%"); otherwise use the name of the lexical at pad offset
698 * targ. Depending on the value of the subscript_type flag, return:
701 #define FUV_SUBSCRIPT_NONE 1 /* "@foo" */
702 #define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */
703 #define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */
704 #define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */
707 S_varname(pTHX_ GV *gv, const char *gvtype, PADOFFSET targ,
708 SV* keyname, I32 aindex, int subscript_type)
714 name = sv_newmortal();
717 /* simulate gv_fullname4(), but add literal '^' for $^FOO names
718 * XXX get rid of all this if gv_fullnameX() ever supports this
722 HV *hv = GvSTASH(gv);
723 sv_setpv(name, gvtype);
726 else if (!(p=HvNAME(hv)))
728 if (strNE(p, "main")) {
730 sv_catpvn(name,"::", 2);
732 if (GvNAMELEN(gv)>= 1 &&
733 ((unsigned int)*GvNAME(gv)) <= 26)
735 Perl_sv_catpvf(aTHX_ name,"^%c", *GvNAME(gv) + 'A' - 1);
736 sv_catpvn(name,GvNAME(gv)+1,GvNAMELEN(gv)-1);
739 sv_catpvn(name,GvNAME(gv),GvNAMELEN(gv));
743 CV *cv = find_runcv(&u);
744 if (!cv || !CvPADLIST(cv))
746 av = (AV*)(*av_fetch(CvPADLIST(cv), 0, FALSE));
747 sv = *av_fetch(av, targ, FALSE);
748 /* SvLEN in a pad name is not to be trusted */
749 sv_setpv(name, SvPV_nolen(sv));
752 if (subscript_type == FUV_SUBSCRIPT_HASH) {
755 Perl_sv_catpvf(aTHX_ name, "{%s}",
756 pv_display(sv,SvPVX(keyname), SvCUR(keyname), 0, 32));
759 else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
761 Perl_sv_catpvf(aTHX_ name, "[%"IVdf"]", (IV)aindex);
763 else if (subscript_type == FUV_SUBSCRIPT_WITHIN)
764 sv_insert(name, 0, 0, "within ", 7);
771 =for apidoc find_uninit_var
773 Find the name of the undefined variable (if any) that caused the operator o
774 to issue a "Use of uninitialized value" warning.
775 If match is true, only return a name if it's value matches uninit_sv.
776 So roughly speaking, if a unary operator (such as OP_COS) generates a
777 warning, then following the direct child of the op may yield an
778 OP_PADSV or OP_GV that gives the name of the undefined variable. On the
779 other hand, with OP_ADD there are two branches to follow, so we only print
780 the variable name if we get an exact match.
782 The name is returned as a mortal SV.
784 Assumes that PL_op is the op that originally triggered the error, and that
785 PL_comppad/PL_curpad points to the currently executing pad.
791 S_find_uninit_var(pTHX_ OP* obase, SV* uninit_sv, bool match)
799 if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
800 uninit_sv == &PL_sv_placeholder)))
803 switch (obase->op_type) {
810 bool pad = (obase->op_type == OP_PADAV || obase->op_type == OP_PADHV);
811 bool hash = (obase->op_type == OP_PADHV || obase->op_type == OP_RV2HV);
814 int subscript_type = FUV_SUBSCRIPT_WITHIN;
816 if (pad) { /* @lex, %lex */
817 sv = PAD_SVl(obase->op_targ);
821 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
822 /* @global, %global */
823 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
826 sv = hash ? (SV*)GvHV(gv): (SV*)GvAV(gv);
828 else /* @{expr}, %{expr} */
829 return find_uninit_var(cUNOPx(obase)->op_first,
833 /* attempt to find a match within the aggregate */
835 keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
837 subscript_type = FUV_SUBSCRIPT_HASH;
840 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
842 subscript_type = FUV_SUBSCRIPT_ARRAY;
845 if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
848 return S_varname(aTHX_ gv, hash ? "%" : "@", obase->op_targ,
849 keysv, index, subscript_type);
853 if (match && PAD_SVl(obase->op_targ) != uninit_sv)
855 return S_varname(aTHX_ Nullgv, "$", obase->op_targ,
856 Nullsv, 0, FUV_SUBSCRIPT_NONE);
859 gv = cGVOPx_gv(obase);
860 if (!gv || (match && GvSV(gv) != uninit_sv))
862 return S_varname(aTHX_ gv, "$", 0, Nullsv, 0, FUV_SUBSCRIPT_NONE);
865 if (obase->op_flags & OPf_SPECIAL) { /* lexical array */
867 av = (AV*)PAD_SV(obase->op_targ);
868 if (!av || SvRMAGICAL(av))
870 svp = av_fetch(av, (I32)obase->op_private, FALSE);
871 if (!svp || *svp != uninit_sv)
874 return S_varname(aTHX_ Nullgv, "$", obase->op_targ,
875 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
878 gv = cGVOPx_gv(obase);
883 if (!av || SvRMAGICAL(av))
885 svp = av_fetch(av, (I32)obase->op_private, FALSE);
886 if (!svp || *svp != uninit_sv)
889 return S_varname(aTHX_ gv, "$", 0,
890 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
895 o = cUNOPx(obase)->op_first;
896 if (!o || o->op_type != OP_NULL ||
897 ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
899 return find_uninit_var(cBINOPo->op_last, uninit_sv, match);
904 /* $a[uninit_expr] or $h{uninit_expr} */
905 return find_uninit_var(cBINOPx(obase)->op_last, uninit_sv, match);
908 o = cBINOPx(obase)->op_first;
909 kid = cBINOPx(obase)->op_last;
911 /* get the av or hv, and optionally the gv */
913 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
914 sv = PAD_SV(o->op_targ);
916 else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
917 && cUNOPo->op_first->op_type == OP_GV)
919 gv = cGVOPx_gv(cUNOPo->op_first);
922 sv = o->op_type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)GvAV(gv);
927 if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
928 /* index is constant */
932 if (obase->op_type == OP_HELEM) {
933 HE* he = hv_fetch_ent((HV*)sv, cSVOPx_sv(kid), 0, 0);
934 if (!he || HeVAL(he) != uninit_sv)
938 svp = av_fetch((AV*)sv, SvIV(cSVOPx_sv(kid)), FALSE);
939 if (!svp || *svp != uninit_sv)
943 if (obase->op_type == OP_HELEM)
944 return S_varname(aTHX_ gv, "%", o->op_targ,
945 cSVOPx_sv(kid), 0, FUV_SUBSCRIPT_HASH);
947 return S_varname(aTHX_ gv, "@", o->op_targ, Nullsv,
948 SvIV(cSVOPx_sv(kid)), FUV_SUBSCRIPT_ARRAY);
952 /* index is an expression;
953 * attempt to find a match within the aggregate */
954 if (obase->op_type == OP_HELEM) {
955 SV *keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
957 return S_varname(aTHX_ gv, "%", o->op_targ,
958 keysv, 0, FUV_SUBSCRIPT_HASH);
961 I32 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
963 return S_varname(aTHX_ gv, "@", o->op_targ,
964 Nullsv, index, FUV_SUBSCRIPT_ARRAY);
968 return S_varname(aTHX_ gv,
969 (o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
971 o->op_targ, Nullsv, 0, FUV_SUBSCRIPT_WITHIN);
977 /* only examine RHS */
978 return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv, match);
981 o = cUNOPx(obase)->op_first;
982 if (o->op_type == OP_PUSHMARK)
985 if (!o->op_sibling) {
986 /* one-arg version of open is highly magical */
988 if (o->op_type == OP_GV) { /* open FOO; */
990 if (match && GvSV(gv) != uninit_sv)
992 return S_varname(aTHX_ gv, "$", 0,
993 Nullsv, 0, FUV_SUBSCRIPT_NONE);
995 /* other possibilities not handled are:
996 * open $x; or open my $x; should return '${*$x}'
997 * open expr; should return '$'.expr ideally
1003 /* ops where $_ may be an implicit arg */
1007 if ( !(obase->op_flags & OPf_STACKED)) {
1008 if (uninit_sv == ((obase->op_private & OPpTARGET_MY)
1009 ? PAD_SVl(obase->op_targ)
1012 sv = sv_newmortal();
1021 /* skip filehandle as it can't produce 'undef' warning */
1022 o = cUNOPx(obase)->op_first;
1023 if ((obase->op_flags & OPf_STACKED) && o->op_type == OP_PUSHMARK)
1024 o = o->op_sibling->op_sibling;
1031 match = 1; /* XS or custom code could trigger random warnings */
1036 if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs))
1037 return sv_2mortal(newSVpv("${$/}", 0));
1042 if (!(obase->op_flags & OPf_KIDS))
1044 o = cUNOPx(obase)->op_first;
1050 /* if all except one arg are constant, or have no side-effects,
1051 * or are optimized away, then it's unambiguous */
1053 for (kid=o; kid; kid = kid->op_sibling) {
1055 ( (kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid)))
1056 || (kid->op_type == OP_NULL && ! (kid->op_flags & OPf_KIDS))
1057 || (kid->op_type == OP_PUSHMARK)
1061 if (o2) { /* more than one found */
1068 return find_uninit_var(o2, uninit_sv, match);
1072 sv = find_uninit_var(o, uninit_sv, 1);
1084 =for apidoc report_uninit
1086 Print appropriate "Use of uninitialized variable" warning
1092 Perl_report_uninit(pTHX_ SV* uninit_sv)
1095 SV* varname = Nullsv;
1097 varname = find_uninit_var(PL_op, uninit_sv,0);
1099 sv_insert(varname, 0, 0, " ", 1);
1101 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
1102 varname ? SvPV_nolen(varname) : "",
1103 " in ", OP_DESC(PL_op));
1106 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
1110 /* grab a new IV body from the free list, allocating more if necessary */
1121 * See comment in more_xiv() -- RAM.
1123 PL_xiv_root = *(IV**)xiv;
1125 return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv));
1128 /* return an IV body to the free list */
1131 S_del_xiv(pTHX_ XPVIV *p)
1133 IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv));
1135 *(IV**)xiv = PL_xiv_root;
1140 /* allocate another arena's worth of IV bodies */
1146 register IV* xivend;
1148 New(705, ptr, 1008/sizeof(XPV), XPV);
1149 ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */
1150 PL_xiv_arenaroot = ptr; /* to keep Purify happy */
1153 xivend = &xiv[1008 / sizeof(IV) - 1];
1154 xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */
1156 while (xiv < xivend) {
1157 *(IV**)xiv = (IV *)(xiv + 1);
1163 /* grab a new NV body from the free list, allocating more if necessary */
1173 PL_xnv_root = *(NV**)xnv;
1175 return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv));
1178 /* return an NV body to the free list */
1181 S_del_xnv(pTHX_ XPVNV *p)
1183 NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv));
1185 *(NV**)xnv = PL_xnv_root;
1190 /* allocate another arena's worth of NV bodies */
1196 register NV* xnvend;
1198 New(711, ptr, 1008/sizeof(XPV), XPV);
1199 ptr->xpv_pv = (char*)PL_xnv_arenaroot;
1200 PL_xnv_arenaroot = ptr;
1203 xnvend = &xnv[1008 / sizeof(NV) - 1];
1204 xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */
1206 while (xnv < xnvend) {
1207 *(NV**)xnv = (NV*)(xnv + 1);
1213 /* grab a new struct xrv from the free list, allocating more if necessary */
1223 PL_xrv_root = (XRV*)xrv->xrv_rv;
1228 /* return a struct xrv to the free list */
1231 S_del_xrv(pTHX_ XRV *p)
1234 p->xrv_rv = (SV*)PL_xrv_root;
1239 /* allocate another arena's worth of struct xrv */
1245 register XRV* xrvend;
1247 New(712, ptr, 1008/sizeof(XPV), XPV);
1248 ptr->xpv_pv = (char*)PL_xrv_arenaroot;
1249 PL_xrv_arenaroot = ptr;
1252 xrvend = &xrv[1008 / sizeof(XRV) - 1];
1253 xrv += (sizeof(XPV) - 1) / sizeof(XRV) + 1;
1255 while (xrv < xrvend) {
1256 xrv->xrv_rv = (SV*)(xrv + 1);
1262 /* grab a new struct xpv from the free list, allocating more if necessary */
1272 PL_xpv_root = (XPV*)xpv->xpv_pv;
1277 /* return a struct xpv to the free list */
1280 S_del_xpv(pTHX_ XPV *p)
1283 p->xpv_pv = (char*)PL_xpv_root;
1288 /* allocate another arena's worth of struct xpv */
1294 register XPV* xpvend;
1295 New(713, xpv, 1008/sizeof(XPV), XPV);
1296 xpv->xpv_pv = (char*)PL_xpv_arenaroot;
1297 PL_xpv_arenaroot = xpv;
1299 xpvend = &xpv[1008 / sizeof(XPV) - 1];
1300 PL_xpv_root = ++xpv;
1301 while (xpv < xpvend) {
1302 xpv->xpv_pv = (char*)(xpv + 1);
1308 /* grab a new struct xpviv from the free list, allocating more if necessary */
1317 xpviv = PL_xpviv_root;
1318 PL_xpviv_root = (XPVIV*)xpviv->xpv_pv;
1323 /* return a struct xpviv to the free list */
1326 S_del_xpviv(pTHX_ XPVIV *p)
1329 p->xpv_pv = (char*)PL_xpviv_root;
1334 /* allocate another arena's worth of struct xpviv */
1339 register XPVIV* xpviv;
1340 register XPVIV* xpvivend;
1341 New(714, xpviv, 1008/sizeof(XPVIV), XPVIV);
1342 xpviv->xpv_pv = (char*)PL_xpviv_arenaroot;
1343 PL_xpviv_arenaroot = xpviv;
1345 xpvivend = &xpviv[1008 / sizeof(XPVIV) - 1];
1346 PL_xpviv_root = ++xpviv;
1347 while (xpviv < xpvivend) {
1348 xpviv->xpv_pv = (char*)(xpviv + 1);
1354 /* grab a new struct xpvnv from the free list, allocating more if necessary */
1363 xpvnv = PL_xpvnv_root;
1364 PL_xpvnv_root = (XPVNV*)xpvnv->xpv_pv;
1369 /* return a struct xpvnv to the free list */
1372 S_del_xpvnv(pTHX_ XPVNV *p)
1375 p->xpv_pv = (char*)PL_xpvnv_root;
1380 /* allocate another arena's worth of struct xpvnv */
1385 register XPVNV* xpvnv;
1386 register XPVNV* xpvnvend;
1387 New(715, xpvnv, 1008/sizeof(XPVNV), XPVNV);
1388 xpvnv->xpv_pv = (char*)PL_xpvnv_arenaroot;
1389 PL_xpvnv_arenaroot = xpvnv;
1391 xpvnvend = &xpvnv[1008 / sizeof(XPVNV) - 1];
1392 PL_xpvnv_root = ++xpvnv;
1393 while (xpvnv < xpvnvend) {
1394 xpvnv->xpv_pv = (char*)(xpvnv + 1);
1400 /* grab a new struct xpvcv from the free list, allocating more if necessary */
1409 xpvcv = PL_xpvcv_root;
1410 PL_xpvcv_root = (XPVCV*)xpvcv->xpv_pv;
1415 /* return a struct xpvcv to the free list */
1418 S_del_xpvcv(pTHX_ XPVCV *p)
1421 p->xpv_pv = (char*)PL_xpvcv_root;
1426 /* allocate another arena's worth of struct xpvcv */
1431 register XPVCV* xpvcv;
1432 register XPVCV* xpvcvend;
1433 New(716, xpvcv, 1008/sizeof(XPVCV), XPVCV);
1434 xpvcv->xpv_pv = (char*)PL_xpvcv_arenaroot;
1435 PL_xpvcv_arenaroot = xpvcv;
1437 xpvcvend = &xpvcv[1008 / sizeof(XPVCV) - 1];
1438 PL_xpvcv_root = ++xpvcv;
1439 while (xpvcv < xpvcvend) {
1440 xpvcv->xpv_pv = (char*)(xpvcv + 1);
1446 /* grab a new struct xpvav from the free list, allocating more if necessary */
1455 xpvav = PL_xpvav_root;
1456 PL_xpvav_root = (XPVAV*)xpvav->xav_array;
1461 /* return a struct xpvav to the free list */
1464 S_del_xpvav(pTHX_ XPVAV *p)
1467 p->xav_array = (char*)PL_xpvav_root;
1472 /* allocate another arena's worth of struct xpvav */
1477 register XPVAV* xpvav;
1478 register XPVAV* xpvavend;
1479 New(717, xpvav, 1008/sizeof(XPVAV), XPVAV);
1480 xpvav->xav_array = (char*)PL_xpvav_arenaroot;
1481 PL_xpvav_arenaroot = xpvav;
1483 xpvavend = &xpvav[1008 / sizeof(XPVAV) - 1];
1484 PL_xpvav_root = ++xpvav;
1485 while (xpvav < xpvavend) {
1486 xpvav->xav_array = (char*)(xpvav + 1);
1489 xpvav->xav_array = 0;
1492 /* grab a new struct xpvhv from the free list, allocating more if necessary */
1501 xpvhv = PL_xpvhv_root;
1502 PL_xpvhv_root = (XPVHV*)xpvhv->xhv_array;
1507 /* return a struct xpvhv to the free list */
1510 S_del_xpvhv(pTHX_ XPVHV *p)
1513 p->xhv_array = (char*)PL_xpvhv_root;
1518 /* allocate another arena's worth of struct xpvhv */
1523 register XPVHV* xpvhv;
1524 register XPVHV* xpvhvend;
1525 New(718, xpvhv, 1008/sizeof(XPVHV), XPVHV);
1526 xpvhv->xhv_array = (char*)PL_xpvhv_arenaroot;
1527 PL_xpvhv_arenaroot = xpvhv;
1529 xpvhvend = &xpvhv[1008 / sizeof(XPVHV) - 1];
1530 PL_xpvhv_root = ++xpvhv;
1531 while (xpvhv < xpvhvend) {
1532 xpvhv->xhv_array = (char*)(xpvhv + 1);
1535 xpvhv->xhv_array = 0;
1538 /* grab a new struct xpvmg from the free list, allocating more if necessary */
1547 xpvmg = PL_xpvmg_root;
1548 PL_xpvmg_root = (XPVMG*)xpvmg->xpv_pv;
1553 /* return a struct xpvmg to the free list */
1556 S_del_xpvmg(pTHX_ XPVMG *p)
1559 p->xpv_pv = (char*)PL_xpvmg_root;
1564 /* allocate another arena's worth of struct xpvmg */
1569 register XPVMG* xpvmg;
1570 register XPVMG* xpvmgend;
1571 New(719, xpvmg, 1008/sizeof(XPVMG), XPVMG);
1572 xpvmg->xpv_pv = (char*)PL_xpvmg_arenaroot;
1573 PL_xpvmg_arenaroot = xpvmg;
1575 xpvmgend = &xpvmg[1008 / sizeof(XPVMG) - 1];
1576 PL_xpvmg_root = ++xpvmg;
1577 while (xpvmg < xpvmgend) {
1578 xpvmg->xpv_pv = (char*)(xpvmg + 1);
1584 /* grab a new struct xpvlv from the free list, allocating more if necessary */
1593 xpvlv = PL_xpvlv_root;
1594 PL_xpvlv_root = (XPVLV*)xpvlv->xpv_pv;
1599 /* return a struct xpvlv to the free list */
1602 S_del_xpvlv(pTHX_ XPVLV *p)
1605 p->xpv_pv = (char*)PL_xpvlv_root;
1610 /* allocate another arena's worth of struct xpvlv */
1615 register XPVLV* xpvlv;
1616 register XPVLV* xpvlvend;
1617 New(720, xpvlv, 1008/sizeof(XPVLV), XPVLV);
1618 xpvlv->xpv_pv = (char*)PL_xpvlv_arenaroot;
1619 PL_xpvlv_arenaroot = xpvlv;
1621 xpvlvend = &xpvlv[1008 / sizeof(XPVLV) - 1];
1622 PL_xpvlv_root = ++xpvlv;
1623 while (xpvlv < xpvlvend) {
1624 xpvlv->xpv_pv = (char*)(xpvlv + 1);
1630 /* grab a new struct xpvbm from the free list, allocating more if necessary */
1639 xpvbm = PL_xpvbm_root;
1640 PL_xpvbm_root = (XPVBM*)xpvbm->xpv_pv;
1645 /* return a struct xpvbm to the free list */
1648 S_del_xpvbm(pTHX_ XPVBM *p)
1651 p->xpv_pv = (char*)PL_xpvbm_root;
1656 /* allocate another arena's worth of struct xpvbm */
1661 register XPVBM* xpvbm;
1662 register XPVBM* xpvbmend;
1663 New(721, xpvbm, 1008/sizeof(XPVBM), XPVBM);
1664 xpvbm->xpv_pv = (char*)PL_xpvbm_arenaroot;
1665 PL_xpvbm_arenaroot = xpvbm;
1667 xpvbmend = &xpvbm[1008 / sizeof(XPVBM) - 1];
1668 PL_xpvbm_root = ++xpvbm;
1669 while (xpvbm < xpvbmend) {
1670 xpvbm->xpv_pv = (char*)(xpvbm + 1);
1676 #define my_safemalloc(s) (void*)safemalloc(s)
1677 #define my_safefree(p) safefree((char*)p)
1681 #define new_XIV() my_safemalloc(sizeof(XPVIV))
1682 #define del_XIV(p) my_safefree(p)
1684 #define new_XNV() my_safemalloc(sizeof(XPVNV))
1685 #define del_XNV(p) my_safefree(p)
1687 #define new_XRV() my_safemalloc(sizeof(XRV))
1688 #define del_XRV(p) my_safefree(p)
1690 #define new_XPV() my_safemalloc(sizeof(XPV))
1691 #define del_XPV(p) my_safefree(p)
1693 #define new_XPVIV() my_safemalloc(sizeof(XPVIV))
1694 #define del_XPVIV(p) my_safefree(p)
1696 #define new_XPVNV() my_safemalloc(sizeof(XPVNV))
1697 #define del_XPVNV(p) my_safefree(p)
1699 #define new_XPVCV() my_safemalloc(sizeof(XPVCV))
1700 #define del_XPVCV(p) my_safefree(p)
1702 #define new_XPVAV() my_safemalloc(sizeof(XPVAV))
1703 #define del_XPVAV(p) my_safefree(p)
1705 #define new_XPVHV() my_safemalloc(sizeof(XPVHV))
1706 #define del_XPVHV(p) my_safefree(p)
1708 #define new_XPVMG() my_safemalloc(sizeof(XPVMG))
1709 #define del_XPVMG(p) my_safefree(p)
1711 #define new_XPVLV() my_safemalloc(sizeof(XPVLV))
1712 #define del_XPVLV(p) my_safefree(p)
1714 #define new_XPVBM() my_safemalloc(sizeof(XPVBM))
1715 #define del_XPVBM(p) my_safefree(p)
1719 #define new_XIV() (void*)new_xiv()
1720 #define del_XIV(p) del_xiv((XPVIV*) p)
1722 #define new_XNV() (void*)new_xnv()
1723 #define del_XNV(p) del_xnv((XPVNV*) p)
1725 #define new_XRV() (void*)new_xrv()
1726 #define del_XRV(p) del_xrv((XRV*) p)
1728 #define new_XPV() (void*)new_xpv()
1729 #define del_XPV(p) del_xpv((XPV *)p)
1731 #define new_XPVIV() (void*)new_xpviv()
1732 #define del_XPVIV(p) del_xpviv((XPVIV *)p)
1734 #define new_XPVNV() (void*)new_xpvnv()
1735 #define del_XPVNV(p) del_xpvnv((XPVNV *)p)
1737 #define new_XPVCV() (void*)new_xpvcv()
1738 #define del_XPVCV(p) del_xpvcv((XPVCV *)p)
1740 #define new_XPVAV() (void*)new_xpvav()
1741 #define del_XPVAV(p) del_xpvav((XPVAV *)p)
1743 #define new_XPVHV() (void*)new_xpvhv()
1744 #define del_XPVHV(p) del_xpvhv((XPVHV *)p)
1746 #define new_XPVMG() (void*)new_xpvmg()
1747 #define del_XPVMG(p) del_xpvmg((XPVMG *)p)
1749 #define new_XPVLV() (void*)new_xpvlv()
1750 #define del_XPVLV(p) del_xpvlv((XPVLV *)p)
1752 #define new_XPVBM() (void*)new_xpvbm()
1753 #define del_XPVBM(p) del_xpvbm((XPVBM *)p)
1757 #define new_XPVGV() my_safemalloc(sizeof(XPVGV))
1758 #define del_XPVGV(p) my_safefree(p)
1760 #define new_XPVFM() my_safemalloc(sizeof(XPVFM))
1761 #define del_XPVFM(p) my_safefree(p)
1763 #define new_XPVIO() my_safemalloc(sizeof(XPVIO))
1764 #define del_XPVIO(p) my_safefree(p)
1767 =for apidoc sv_upgrade
1769 Upgrade an SV to a more complex form. Generally adds a new body type to the
1770 SV, then copies across as much information as possible from the old body.
1771 You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>.
1777 Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
1785 MAGIC* magic = NULL;
1788 if (mt != SVt_PV && SvIsCOW(sv)) {
1789 sv_force_normal_flags(sv, 0);
1792 if (SvTYPE(sv) == mt)
1796 (void)SvOOK_off(sv);
1798 switch (SvTYPE(sv)) {
1819 else if (mt < SVt_PVIV)
1836 pv = (char*)SvRV(sv);
1856 else if (mt == SVt_NV)
1867 del_XPVIV(SvANY(sv));
1877 del_XPVNV(SvANY(sv));
1885 magic = SvMAGIC(sv);
1886 stash = SvSTASH(sv);
1887 del_XPVMG(SvANY(sv));
1890 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
1893 SvFLAGS(sv) &= ~SVTYPEMASK;
1898 Perl_croak(aTHX_ "Can't upgrade to undef");
1900 SvANY(sv) = new_XIV();
1904 SvANY(sv) = new_XNV();
1908 SvANY(sv) = new_XRV();
1909 SvRV_set(sv, (SV*)pv);
1912 SvANY(sv) = new_XPV();
1918 SvANY(sv) = new_XPVIV();
1928 SvANY(sv) = new_XPVNV();
1936 SvANY(sv) = new_XPVMG();
1942 SvMAGIC_set(sv, magic);
1943 SvSTASH_set(sv, stash);
1946 SvANY(sv) = new_XPVLV();
1952 SvMAGIC_set(sv, magic);
1953 SvSTASH_set(sv, stash);
1965 SvANY(sv) = new_XPVAV();
1968 SvPV_set(sv, (char*)0);
1973 SvMAGIC_set(sv, magic);
1974 SvSTASH_set(sv, stash);
1977 AvFLAGS(sv) = AVf_REAL;
1980 SvANY(sv) = new_XPVHV();
1983 SvPV_set(sv, (char*)0);
1986 HvTOTALKEYS(sv) = 0;
1987 HvPLACEHOLDERS(sv) = 0;
1988 SvMAGIC_set(sv, magic);
1989 SvSTASH_set(sv, stash);
1996 SvANY(sv) = new_XPVCV();
1997 Zero(SvANY(sv), 1, XPVCV);
2003 SvMAGIC_set(sv, magic);
2004 SvSTASH_set(sv, stash);
2007 SvANY(sv) = new_XPVGV();
2013 SvMAGIC_set(sv, magic);
2014 SvSTASH_set(sv, stash);
2022 SvANY(sv) = new_XPVBM();
2028 SvMAGIC_set(sv, magic);
2029 SvSTASH_set(sv, stash);
2035 SvANY(sv) = new_XPVFM();
2036 Zero(SvANY(sv), 1, XPVFM);
2042 SvMAGIC_set(sv, magic);
2043 SvSTASH_set(sv, stash);
2046 SvANY(sv) = new_XPVIO();
2047 Zero(SvANY(sv), 1, XPVIO);
2053 SvMAGIC_set(sv, magic);
2054 SvSTASH_set(sv, stash);
2055 IoPAGE_LEN(sv) = 60;
2062 =for apidoc sv_backoff
2064 Remove any string offset. You should normally use the C<SvOOK_off> macro
2071 Perl_sv_backoff(pTHX_ register SV *sv)
2075 char *s = SvPVX(sv);
2076 SvLEN_set(sv, SvLEN(sv) + SvIVX(sv));
2077 SvPV_set(sv, SvPVX(sv) - SvIVX(sv));
2079 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
2081 SvFLAGS(sv) &= ~SVf_OOK;
2088 Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
2089 upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
2090 Use the C<SvGROW> wrapper instead.
2096 Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
2100 #ifdef HAS_64K_LIMIT
2101 if (newlen >= 0x10000) {
2102 PerlIO_printf(Perl_debug_log,
2103 "Allocation too large: %"UVxf"\n", (UV)newlen);
2106 #endif /* HAS_64K_LIMIT */
2109 if (SvTYPE(sv) < SVt_PV) {
2110 sv_upgrade(sv, SVt_PV);
2113 else if (SvOOK(sv)) { /* pv is offset? */
2116 if (newlen > SvLEN(sv))
2117 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
2118 #ifdef HAS_64K_LIMIT
2119 if (newlen >= 0x10000)
2126 if (newlen > SvLEN(sv)) { /* need more room? */
2127 if (SvLEN(sv) && s) {
2129 STRLEN l = malloced_size((void*)SvPVX(sv));
2135 Renew(s,newlen,char);
2138 New(703, s, newlen, char);
2139 if (SvPVX(sv) && SvCUR(sv)) {
2140 Move(SvPVX(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
2144 SvLEN_set(sv, newlen);
2150 =for apidoc sv_setiv
2152 Copies an integer into the given SV, upgrading first if necessary.
2153 Does not handle 'set' magic. See also C<sv_setiv_mg>.
2159 Perl_sv_setiv(pTHX_ register SV *sv, IV i)
2161 SV_CHECK_THINKFIRST_COW_DROP(sv);
2162 switch (SvTYPE(sv)) {
2164 sv_upgrade(sv, SVt_IV);
2167 sv_upgrade(sv, SVt_PVNV);
2171 sv_upgrade(sv, SVt_PVIV);
2180 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
2183 (void)SvIOK_only(sv); /* validate number */
2189 =for apidoc sv_setiv_mg
2191 Like C<sv_setiv>, but also handles 'set' magic.
2197 Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
2204 =for apidoc sv_setuv
2206 Copies an unsigned integer into the given SV, upgrading first if necessary.
2207 Does not handle 'set' magic. See also C<sv_setuv_mg>.
2213 Perl_sv_setuv(pTHX_ register SV *sv, UV u)
2215 /* With these two if statements:
2216 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
2219 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
2221 If you wish to remove them, please benchmark to see what the effect is
2223 if (u <= (UV)IV_MAX) {
2224 sv_setiv(sv, (IV)u);
2233 =for apidoc sv_setuv_mg
2235 Like C<sv_setuv>, but also handles 'set' magic.
2241 Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
2243 /* With these two if statements:
2244 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
2247 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
2249 If you wish to remove them, please benchmark to see what the effect is
2251 if (u <= (UV)IV_MAX) {
2252 sv_setiv(sv, (IV)u);
2262 =for apidoc sv_setnv
2264 Copies a double into the given SV, upgrading first if necessary.
2265 Does not handle 'set' magic. See also C<sv_setnv_mg>.
2271 Perl_sv_setnv(pTHX_ register SV *sv, NV num)
2273 SV_CHECK_THINKFIRST_COW_DROP(sv);
2274 switch (SvTYPE(sv)) {
2277 sv_upgrade(sv, SVt_NV);
2282 sv_upgrade(sv, SVt_PVNV);
2291 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
2295 (void)SvNOK_only(sv); /* validate number */
2300 =for apidoc sv_setnv_mg
2302 Like C<sv_setnv>, but also handles 'set' magic.
2308 Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
2314 /* Print an "isn't numeric" warning, using a cleaned-up,
2315 * printable version of the offending string
2319 S_not_a_number(pTHX_ SV *sv)
2326 dsv = sv_2mortal(newSVpv("", 0));
2327 pv = sv_uni_display(dsv, sv, 10, 0);
2330 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
2331 /* each *s can expand to 4 chars + "...\0",
2332 i.e. need room for 8 chars */
2335 for (s = SvPVX(sv), end = s + SvCUR(sv); s < end && d < limit; s++) {
2337 if (ch & 128 && !isPRINT_LC(ch)) {
2346 else if (ch == '\r') {
2350 else if (ch == '\f') {
2354 else if (ch == '\\') {
2358 else if (ch == '\0') {
2362 else if (isPRINT_LC(ch))
2379 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
2380 "Argument \"%s\" isn't numeric in %s", pv,
2383 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
2384 "Argument \"%s\" isn't numeric", pv);
2388 =for apidoc looks_like_number
2390 Test if the content of an SV looks like a number (or is a number).
2391 C<Inf> and C<Infinity> are treated as numbers (so will not issue a
2392 non-numeric warning), even if your atof() doesn't grok them.
2398 Perl_looks_like_number(pTHX_ SV *sv)
2400 register char *sbegin;
2407 else if (SvPOKp(sv))
2408 sbegin = SvPV(sv, len);
2410 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
2411 return grok_number(sbegin, len, NULL);
2414 /* Actually, ISO C leaves conversion of UV to IV undefined, but
2415 until proven guilty, assume that things are not that bad... */
2420 As 64 bit platforms often have an NV that doesn't preserve all bits of
2421 an IV (an assumption perl has been based on to date) it becomes necessary
2422 to remove the assumption that the NV always carries enough precision to
2423 recreate the IV whenever needed, and that the NV is the canonical form.
2424 Instead, IV/UV and NV need to be given equal rights. So as to not lose
2425 precision as a side effect of conversion (which would lead to insanity
2426 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
2427 1) to distinguish between IV/UV/NV slots that have cached a valid
2428 conversion where precision was lost and IV/UV/NV slots that have a
2429 valid conversion which has lost no precision
2430 2) to ensure that if a numeric conversion to one form is requested that
2431 would lose precision, the precise conversion (or differently
2432 imprecise conversion) is also performed and cached, to prevent
2433 requests for different numeric formats on the same SV causing
2434 lossy conversion chains. (lossless conversion chains are perfectly
2439 SvIOKp is true if the IV slot contains a valid value
2440 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
2441 SvNOKp is true if the NV slot contains a valid value
2442 SvNOK is true only if the NV value is accurate
2445 while converting from PV to NV, check to see if converting that NV to an
2446 IV(or UV) would lose accuracy over a direct conversion from PV to
2447 IV(or UV). If it would, cache both conversions, return NV, but mark
2448 SV as IOK NOKp (ie not NOK).
2450 While converting from PV to IV, check to see if converting that IV to an
2451 NV would lose accuracy over a direct conversion from PV to NV. If it
2452 would, cache both conversions, flag similarly.
2454 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
2455 correctly because if IV & NV were set NV *always* overruled.
2456 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
2457 changes - now IV and NV together means that the two are interchangeable:
2458 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
2460 The benefit of this is that operations such as pp_add know that if
2461 SvIOK is true for both left and right operands, then integer addition
2462 can be used instead of floating point (for cases where the result won't
2463 overflow). Before, floating point was always used, which could lead to
2464 loss of precision compared with integer addition.
2466 * making IV and NV equal status should make maths accurate on 64 bit
2468 * may speed up maths somewhat if pp_add and friends start to use
2469 integers when possible instead of fp. (Hopefully the overhead in
2470 looking for SvIOK and checking for overflow will not outweigh the
2471 fp to integer speedup)
2472 * will slow down integer operations (callers of SvIV) on "inaccurate"
2473 values, as the change from SvIOK to SvIOKp will cause a call into
2474 sv_2iv each time rather than a macro access direct to the IV slot
2475 * should speed up number->string conversion on integers as IV is
2476 favoured when IV and NV are equally accurate
2478 ####################################################################
2479 You had better be using SvIOK_notUV if you want an IV for arithmetic:
2480 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
2481 On the other hand, SvUOK is true iff UV.
2482 ####################################################################
2484 Your mileage will vary depending your CPU's relative fp to integer
2488 #ifndef NV_PRESERVES_UV
2489 # define IS_NUMBER_UNDERFLOW_IV 1
2490 # define IS_NUMBER_UNDERFLOW_UV 2
2491 # define IS_NUMBER_IV_AND_UV 2
2492 # define IS_NUMBER_OVERFLOW_IV 4
2493 # define IS_NUMBER_OVERFLOW_UV 5
2495 /* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
2497 /* For sv_2nv these three cases are "SvNOK and don't bother casting" */
2499 S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype)
2501 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));
2502 if (SvNVX(sv) < (NV)IV_MIN) {
2503 (void)SvIOKp_on(sv);
2505 SvIV_set(sv, IV_MIN);
2506 return IS_NUMBER_UNDERFLOW_IV;
2508 if (SvNVX(sv) > (NV)UV_MAX) {
2509 (void)SvIOKp_on(sv);
2512 SvUV_set(sv, UV_MAX);
2513 return IS_NUMBER_OVERFLOW_UV;
2515 (void)SvIOKp_on(sv);
2517 /* Can't use strtol etc to convert this string. (See truth table in
2519 if (SvNVX(sv) <= (UV)IV_MAX) {
2520 SvIV_set(sv, I_V(SvNVX(sv)));
2521 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2522 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
2524 /* Integer is imprecise. NOK, IOKp */
2526 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
2529 SvUV_set(sv, U_V(SvNVX(sv)));
2530 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2531 if (SvUVX(sv) == UV_MAX) {
2532 /* As we know that NVs don't preserve UVs, UV_MAX cannot
2533 possibly be preserved by NV. Hence, it must be overflow.
2535 return IS_NUMBER_OVERFLOW_UV;
2537 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
2539 /* Integer is imprecise. NOK, IOKp */
2541 return IS_NUMBER_OVERFLOW_IV;
2543 #endif /* !NV_PRESERVES_UV*/
2545 /* sv_2iv() is now a macro using Perl_sv_2iv_flags();
2546 * this function provided for binary compatibility only
2550 Perl_sv_2iv(pTHX_ register SV *sv)
2552 return sv_2iv_flags(sv, SV_GMAGIC);
2556 =for apidoc sv_2iv_flags
2558 Return the integer value of an SV, doing any necessary string
2559 conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2560 Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
2566 Perl_sv_2iv_flags(pTHX_ register SV *sv, I32 flags)
2570 if (SvGMAGICAL(sv)) {
2571 if (flags & SV_GMAGIC)
2576 return I_V(SvNVX(sv));
2578 if (SvPOKp(sv) && SvLEN(sv))
2581 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2582 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2588 if (SvTHINKFIRST(sv)) {
2591 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
2592 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
2593 return SvIV(tmpstr);
2594 return PTR2IV(SvRV(sv));
2597 sv_force_normal_flags(sv, 0);
2599 if (SvREADONLY(sv) && !SvOK(sv)) {
2600 if (ckWARN(WARN_UNINITIALIZED))
2607 return (IV)(SvUVX(sv));
2614 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2615 * without also getting a cached IV/UV from it at the same time
2616 * (ie PV->NV conversion should detect loss of accuracy and cache
2617 * IV or UV at same time to avoid this. NWC */
2619 if (SvTYPE(sv) == SVt_NV)
2620 sv_upgrade(sv, SVt_PVNV);
2622 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2623 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2624 certainly cast into the IV range at IV_MAX, whereas the correct
2625 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2627 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2628 SvIV_set(sv, I_V(SvNVX(sv)));
2629 if (SvNVX(sv) == (NV) SvIVX(sv)
2630 #ifndef NV_PRESERVES_UV
2631 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2632 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2633 /* Don't flag it as "accurately an integer" if the number
2634 came from a (by definition imprecise) NV operation, and
2635 we're outside the range of NV integer precision */
2638 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2639 DEBUG_c(PerlIO_printf(Perl_debug_log,
2640 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
2646 /* IV not precise. No need to convert from PV, as NV
2647 conversion would already have cached IV if it detected
2648 that PV->IV would be better than PV->NV->IV
2649 flags already correct - don't set public IOK. */
2650 DEBUG_c(PerlIO_printf(Perl_debug_log,
2651 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
2656 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2657 but the cast (NV)IV_MIN rounds to a the value less (more
2658 negative) than IV_MIN which happens to be equal to SvNVX ??
2659 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2660 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2661 (NV)UVX == NVX are both true, but the values differ. :-(
2662 Hopefully for 2s complement IV_MIN is something like
2663 0x8000000000000000 which will be exact. NWC */
2666 SvUV_set(sv, U_V(SvNVX(sv)));
2668 (SvNVX(sv) == (NV) SvUVX(sv))
2669 #ifndef NV_PRESERVES_UV
2670 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2671 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2672 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2673 /* Don't flag it as "accurately an integer" if the number
2674 came from a (by definition imprecise) NV operation, and
2675 we're outside the range of NV integer precision */
2681 DEBUG_c(PerlIO_printf(Perl_debug_log,
2682 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
2686 return (IV)SvUVX(sv);
2689 else if (SvPOKp(sv) && SvLEN(sv)) {
2691 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2692 /* We want to avoid a possible problem when we cache an IV which
2693 may be later translated to an NV, and the resulting NV is not
2694 the same as the direct translation of the initial string
2695 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2696 be careful to ensure that the value with the .456 is around if the
2697 NV value is requested in the future).
2699 This means that if we cache such an IV, we need to cache the
2700 NV as well. Moreover, we trade speed for space, and do not
2701 cache the NV if we are sure it's not needed.
2704 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2705 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2706 == IS_NUMBER_IN_UV) {
2707 /* It's definitely an integer, only upgrade to PVIV */
2708 if (SvTYPE(sv) < SVt_PVIV)
2709 sv_upgrade(sv, SVt_PVIV);
2711 } else if (SvTYPE(sv) < SVt_PVNV)
2712 sv_upgrade(sv, SVt_PVNV);
2714 /* If NV preserves UV then we only use the UV value if we know that
2715 we aren't going to call atof() below. If NVs don't preserve UVs
2716 then the value returned may have more precision than atof() will
2717 return, even though value isn't perfectly accurate. */
2718 if ((numtype & (IS_NUMBER_IN_UV
2719 #ifdef NV_PRESERVES_UV
2722 )) == IS_NUMBER_IN_UV) {
2723 /* This won't turn off the public IOK flag if it was set above */
2724 (void)SvIOKp_on(sv);
2726 if (!(numtype & IS_NUMBER_NEG)) {
2728 if (value <= (UV)IV_MAX) {
2729 SvIV_set(sv, (IV)value);
2731 SvUV_set(sv, value);
2735 /* 2s complement assumption */
2736 if (value <= (UV)IV_MIN) {
2737 SvIV_set(sv, -(IV)value);
2739 /* Too negative for an IV. This is a double upgrade, but
2740 I'm assuming it will be rare. */
2741 if (SvTYPE(sv) < SVt_PVNV)
2742 sv_upgrade(sv, SVt_PVNV);
2746 SvNV_set(sv, -(NV)value);
2747 SvIV_set(sv, IV_MIN);
2751 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2752 will be in the previous block to set the IV slot, and the next
2753 block to set the NV slot. So no else here. */
2755 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2756 != IS_NUMBER_IN_UV) {
2757 /* It wasn't an (integer that doesn't overflow the UV). */
2758 SvNV_set(sv, Atof(SvPVX(sv)));
2760 if (! numtype && ckWARN(WARN_NUMERIC))
2763 #if defined(USE_LONG_DOUBLE)
2764 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
2765 PTR2UV(sv), SvNVX(sv)));
2767 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
2768 PTR2UV(sv), SvNVX(sv)));
2772 #ifdef NV_PRESERVES_UV
2773 (void)SvIOKp_on(sv);
2775 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2776 SvIV_set(sv, I_V(SvNVX(sv)));
2777 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2780 /* Integer is imprecise. NOK, IOKp */
2782 /* UV will not work better than IV */
2784 if (SvNVX(sv) > (NV)UV_MAX) {
2786 /* Integer is inaccurate. NOK, IOKp, is UV */
2787 SvUV_set(sv, UV_MAX);
2790 SvUV_set(sv, U_V(SvNVX(sv)));
2791 /* 0xFFFFFFFFFFFFFFFF not an issue in here */
2792 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2796 /* Integer is imprecise. NOK, IOKp, is UV */
2802 #else /* NV_PRESERVES_UV */
2803 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2804 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2805 /* The IV slot will have been set from value returned by
2806 grok_number above. The NV slot has just been set using
2809 assert (SvIOKp(sv));
2811 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2812 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2813 /* Small enough to preserve all bits. */
2814 (void)SvIOKp_on(sv);
2816 SvIV_set(sv, I_V(SvNVX(sv)));
2817 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2819 /* Assumption: first non-preserved integer is < IV_MAX,
2820 this NV is in the preserved range, therefore: */
2821 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2823 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);
2827 0 0 already failed to read UV.
2828 0 1 already failed to read UV.
2829 1 0 you won't get here in this case. IV/UV
2830 slot set, public IOK, Atof() unneeded.
2831 1 1 already read UV.
2832 so there's no point in sv_2iuv_non_preserve() attempting
2833 to use atol, strtol, strtoul etc. */
2834 if (sv_2iuv_non_preserve (sv, numtype)
2835 >= IS_NUMBER_OVERFLOW_IV)
2839 #endif /* NV_PRESERVES_UV */
2842 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
2844 if (SvTYPE(sv) < SVt_IV)
2845 /* Typically the caller expects that sv_any is not NULL now. */
2846 sv_upgrade(sv, SVt_IV);
2849 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2850 PTR2UV(sv),SvIVX(sv)));
2851 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
2854 /* sv_2uv() is now a macro using Perl_sv_2uv_flags();
2855 * this function provided for binary compatibility only
2859 Perl_sv_2uv(pTHX_ register SV *sv)
2861 return sv_2uv_flags(sv, SV_GMAGIC);
2865 =for apidoc sv_2uv_flags
2867 Return the unsigned integer value of an SV, doing any necessary string
2868 conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2869 Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
2875 Perl_sv_2uv_flags(pTHX_ register SV *sv, I32 flags)
2879 if (SvGMAGICAL(sv)) {
2880 if (flags & SV_GMAGIC)
2885 return U_V(SvNVX(sv));
2886 if (SvPOKp(sv) && SvLEN(sv))
2889 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2890 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2896 if (SvTHINKFIRST(sv)) {
2899 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
2900 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
2901 return SvUV(tmpstr);
2902 return PTR2UV(SvRV(sv));
2905 sv_force_normal_flags(sv, 0);
2907 if (SvREADONLY(sv) && !SvOK(sv)) {
2908 if (ckWARN(WARN_UNINITIALIZED))
2918 return (UV)SvIVX(sv);
2922 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2923 * without also getting a cached IV/UV from it at the same time
2924 * (ie PV->NV conversion should detect loss of accuracy and cache
2925 * IV or UV at same time to avoid this. */
2926 /* IV-over-UV optimisation - choose to cache IV if possible */
2928 if (SvTYPE(sv) == SVt_NV)
2929 sv_upgrade(sv, SVt_PVNV);
2931 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2932 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2933 SvIV_set(sv, I_V(SvNVX(sv)));
2934 if (SvNVX(sv) == (NV) SvIVX(sv)
2935 #ifndef NV_PRESERVES_UV
2936 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2937 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2938 /* Don't flag it as "accurately an integer" if the number
2939 came from a (by definition imprecise) NV operation, and
2940 we're outside the range of NV integer precision */
2943 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2944 DEBUG_c(PerlIO_printf(Perl_debug_log,
2945 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (precise)\n",
2951 /* IV not precise. No need to convert from PV, as NV
2952 conversion would already have cached IV if it detected
2953 that PV->IV would be better than PV->NV->IV
2954 flags already correct - don't set public IOK. */
2955 DEBUG_c(PerlIO_printf(Perl_debug_log,
2956 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (imprecise)\n",
2961 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2962 but the cast (NV)IV_MIN rounds to a the value less (more
2963 negative) than IV_MIN which happens to be equal to SvNVX ??
2964 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2965 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2966 (NV)UVX == NVX are both true, but the values differ. :-(
2967 Hopefully for 2s complement IV_MIN is something like
2968 0x8000000000000000 which will be exact. NWC */
2971 SvUV_set(sv, U_V(SvNVX(sv)));
2973 (SvNVX(sv) == (NV) SvUVX(sv))
2974 #ifndef NV_PRESERVES_UV
2975 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2976 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2977 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2978 /* Don't flag it as "accurately an integer" if the number
2979 came from a (by definition imprecise) NV operation, and
2980 we're outside the range of NV integer precision */
2985 DEBUG_c(PerlIO_printf(Perl_debug_log,
2986 "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n",
2992 else if (SvPOKp(sv) && SvLEN(sv)) {
2994 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2996 /* We want to avoid a possible problem when we cache a UV which
2997 may be later translated to an NV, and the resulting NV is not
2998 the translation of the initial data.
3000 This means that if we cache such a UV, we need to cache the
3001 NV as well. Moreover, we trade speed for space, and do not
3002 cache the NV if not needed.
3005 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
3006 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3007 == IS_NUMBER_IN_UV) {
3008 /* It's definitely an integer, only upgrade to PVIV */
3009 if (SvTYPE(sv) < SVt_PVIV)
3010 sv_upgrade(sv, SVt_PVIV);
3012 } else if (SvTYPE(sv) < SVt_PVNV)
3013 sv_upgrade(sv, SVt_PVNV);
3015 /* If NV preserves UV then we only use the UV value if we know that
3016 we aren't going to call atof() below. If NVs don't preserve UVs
3017 then the value returned may have more precision than atof() will
3018 return, even though it isn't accurate. */
3019 if ((numtype & (IS_NUMBER_IN_UV
3020 #ifdef NV_PRESERVES_UV
3023 )) == IS_NUMBER_IN_UV) {
3024 /* This won't turn off the public IOK flag if it was set above */
3025 (void)SvIOKp_on(sv);
3027 if (!(numtype & IS_NUMBER_NEG)) {
3029 if (value <= (UV)IV_MAX) {
3030 SvIV_set(sv, (IV)value);
3032 /* it didn't overflow, and it was positive. */
3033 SvUV_set(sv, value);
3037 /* 2s complement assumption */
3038 if (value <= (UV)IV_MIN) {
3039 SvIV_set(sv, -(IV)value);
3041 /* Too negative for an IV. This is a double upgrade, but
3042 I'm assuming it will be rare. */
3043 if (SvTYPE(sv) < SVt_PVNV)
3044 sv_upgrade(sv, SVt_PVNV);
3048 SvNV_set(sv, -(NV)value);
3049 SvIV_set(sv, IV_MIN);
3054 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3055 != IS_NUMBER_IN_UV) {
3056 /* It wasn't an integer, or it overflowed the UV. */
3057 SvNV_set(sv, Atof(SvPVX(sv)));
3059 if (! numtype && ckWARN(WARN_NUMERIC))
3062 #if defined(USE_LONG_DOUBLE)
3063 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n",
3064 PTR2UV(sv), SvNVX(sv)));
3066 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"NVgf")\n",
3067 PTR2UV(sv), SvNVX(sv)));
3070 #ifdef NV_PRESERVES_UV
3071 (void)SvIOKp_on(sv);
3073 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
3074 SvIV_set(sv, I_V(SvNVX(sv)));
3075 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
3078 /* Integer is imprecise. NOK, IOKp */
3080 /* UV will not work better than IV */
3082 if (SvNVX(sv) > (NV)UV_MAX) {
3084 /* Integer is inaccurate. NOK, IOKp, is UV */
3085 SvUV_set(sv, UV_MAX);
3088 SvUV_set(sv, U_V(SvNVX(sv)));
3089 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
3090 NV preservse UV so can do correct comparison. */
3091 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
3095 /* Integer is imprecise. NOK, IOKp, is UV */
3100 #else /* NV_PRESERVES_UV */
3101 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3102 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
3103 /* The UV slot will have been set from value returned by
3104 grok_number above. The NV slot has just been set using
3107 assert (SvIOKp(sv));
3109 if (((UV)1 << NV_PRESERVES_UV_BITS) >
3110 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
3111 /* Small enough to preserve all bits. */
3112 (void)SvIOKp_on(sv);
3114 SvIV_set(sv, I_V(SvNVX(sv)));
3115 if ((NV)(SvIVX(sv)) == SvNVX(sv))
3117 /* Assumption: first non-preserved integer is < IV_MAX,
3118 this NV is in the preserved range, therefore: */
3119 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
3121 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);
3124 sv_2iuv_non_preserve (sv, numtype);
3126 #endif /* NV_PRESERVES_UV */
3130 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
3131 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
3134 if (SvTYPE(sv) < SVt_IV)
3135 /* Typically the caller expects that sv_any is not NULL now. */
3136 sv_upgrade(sv, SVt_IV);
3140 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
3141 PTR2UV(sv),SvUVX(sv)));
3142 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
3148 Return the num value of an SV, doing any necessary string or integer
3149 conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
3156 Perl_sv_2nv(pTHX_ register SV *sv)
3160 if (SvGMAGICAL(sv)) {
3164 if (SvPOKp(sv) && SvLEN(sv)) {
3165 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) &&
3166 !grok_number(SvPVX(sv), SvCUR(sv), NULL))
3168 return Atof(SvPVX(sv));
3172 return (NV)SvUVX(sv);
3174 return (NV)SvIVX(sv);
3177 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
3178 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
3184 if (SvTHINKFIRST(sv)) {
3187 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
3188 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
3189 return SvNV(tmpstr);
3190 return PTR2NV(SvRV(sv));
3193 sv_force_normal_flags(sv, 0);
3195 if (SvREADONLY(sv) && !SvOK(sv)) {
3196 if (ckWARN(WARN_UNINITIALIZED))
3201 if (SvTYPE(sv) < SVt_NV) {
3202 if (SvTYPE(sv) == SVt_IV)
3203 sv_upgrade(sv, SVt_PVNV);
3205 sv_upgrade(sv, SVt_NV);
3206 #ifdef USE_LONG_DOUBLE
3208 STORE_NUMERIC_LOCAL_SET_STANDARD();
3209 PerlIO_printf(Perl_debug_log,
3210 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
3211 PTR2UV(sv), SvNVX(sv));
3212 RESTORE_NUMERIC_LOCAL();
3216 STORE_NUMERIC_LOCAL_SET_STANDARD();
3217 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
3218 PTR2UV(sv), SvNVX(sv));
3219 RESTORE_NUMERIC_LOCAL();
3223 else if (SvTYPE(sv) < SVt_PVNV)
3224 sv_upgrade(sv, SVt_PVNV);
3229 SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
3230 #ifdef NV_PRESERVES_UV
3233 /* Only set the public NV OK flag if this NV preserves the IV */
3234 /* Check it's not 0xFFFFFFFFFFFFFFFF */
3235 if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
3236 : (SvIVX(sv) == I_V(SvNVX(sv))))
3242 else if (SvPOKp(sv) && SvLEN(sv)) {
3244 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
3245 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !numtype)
3247 #ifdef NV_PRESERVES_UV
3248 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3249 == IS_NUMBER_IN_UV) {
3250 /* It's definitely an integer */
3251 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
3253 SvNV_set(sv, Atof(SvPVX(sv)));
3256 SvNV_set(sv, Atof(SvPVX(sv)));
3257 /* Only set the public NV OK flag if this NV preserves the value in
3258 the PV at least as well as an IV/UV would.
3259 Not sure how to do this 100% reliably. */
3260 /* if that shift count is out of range then Configure's test is
3261 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
3263 if (((UV)1 << NV_PRESERVES_UV_BITS) >
3264 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
3265 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
3266 } else if (!(numtype & IS_NUMBER_IN_UV)) {
3267 /* Can't use strtol etc to convert this string, so don't try.
3268 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
3271 /* value has been set. It may not be precise. */
3272 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
3273 /* 2s complement assumption for (UV)IV_MIN */
3274 SvNOK_on(sv); /* Integer is too negative. */
3279 if (numtype & IS_NUMBER_NEG) {
3280 SvIV_set(sv, -(IV)value);
3281 } else if (value <= (UV)IV_MAX) {
3282 SvIV_set(sv, (IV)value);
3284 SvUV_set(sv, value);
3288 if (numtype & IS_NUMBER_NOT_INT) {
3289 /* I believe that even if the original PV had decimals,
3290 they are lost beyond the limit of the FP precision.
3291 However, neither is canonical, so both only get p
3292 flags. NWC, 2000/11/25 */
3293 /* Both already have p flags, so do nothing */
3296 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
3297 if (SvIVX(sv) == I_V(nv)) {
3302 /* It had no "." so it must be integer. */
3305 /* between IV_MAX and NV(UV_MAX).
3306 Could be slightly > UV_MAX */
3308 if (numtype & IS_NUMBER_NOT_INT) {
3309 /* UV and NV both imprecise. */
3311 UV nv_as_uv = U_V(nv);
3313 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
3324 #endif /* NV_PRESERVES_UV */
3327 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
3329 if (SvTYPE(sv) < SVt_NV)
3330 /* Typically the caller expects that sv_any is not NULL now. */
3331 /* XXX Ilya implies that this is a bug in callers that assume this
3332 and ideally should be fixed. */
3333 sv_upgrade(sv, SVt_NV);
3336 #if defined(USE_LONG_DOUBLE)
3338 STORE_NUMERIC_LOCAL_SET_STANDARD();
3339 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
3340 PTR2UV(sv), SvNVX(sv));
3341 RESTORE_NUMERIC_LOCAL();
3345 STORE_NUMERIC_LOCAL_SET_STANDARD();
3346 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
3347 PTR2UV(sv), SvNVX(sv));
3348 RESTORE_NUMERIC_LOCAL();
3354 /* asIV(): extract an integer from the string value of an SV.
3355 * Caller must validate PVX */
3358 S_asIV(pTHX_ SV *sv)
3361 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
3363 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3364 == IS_NUMBER_IN_UV) {
3365 /* It's definitely an integer */
3366 if (numtype & IS_NUMBER_NEG) {
3367 if (value < (UV)IV_MIN)
3370 if (value < (UV)IV_MAX)
3375 if (ckWARN(WARN_NUMERIC))
3378 return I_V(Atof(SvPVX(sv)));
3381 /* asUV(): extract an unsigned integer from the string value of an SV
3382 * Caller must validate PVX */
3385 S_asUV(pTHX_ SV *sv)
3388 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
3390 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3391 == IS_NUMBER_IN_UV) {
3392 /* It's definitely an integer */
3393 if (!(numtype & IS_NUMBER_NEG))
3397 if (ckWARN(WARN_NUMERIC))
3400 return U_V(Atof(SvPVX(sv)));
3404 =for apidoc sv_2pv_nolen
3406 Like C<sv_2pv()>, but doesn't return the length too. You should usually
3407 use the macro wrapper C<SvPV_nolen(sv)> instead.
3412 Perl_sv_2pv_nolen(pTHX_ register SV *sv)
3415 return sv_2pv(sv, &n_a);
3418 /* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
3419 * UV as a string towards the end of buf, and return pointers to start and
3422 * We assume that buf is at least TYPE_CHARS(UV) long.
3426 uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
3428 char *ptr = buf + TYPE_CHARS(UV);
3442 *--ptr = '0' + (char)(uv % 10);
3450 /* sv_2pv() is now a macro using Perl_sv_2pv_flags();
3451 * this function provided for binary compatibility only
3455 Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
3457 return sv_2pv_flags(sv, lp, SV_GMAGIC);
3461 =for apidoc sv_2pv_flags
3463 Returns a pointer to the string value of an SV, and sets *lp to its length.
3464 If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
3466 Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
3467 usually end up here too.
3473 Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
3478 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
3479 char *tmpbuf = tbuf;
3485 if (SvGMAGICAL(sv)) {
3486 if (flags & SV_GMAGIC)
3494 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
3496 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
3501 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
3506 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
3507 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
3514 if (SvTHINKFIRST(sv)) {
3517 register const char *typestr;
3518 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
3519 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
3520 char *pv = SvPV(tmpstr, *lp);
3530 typestr = "NULLREF";
3534 switch (SvTYPE(sv)) {
3536 if ( ((SvFLAGS(sv) &
3537 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
3538 == (SVs_OBJECT|SVs_SMG))
3539 && (mg = mg_find(sv, PERL_MAGIC_qr))) {
3540 const regexp *re = (regexp *)mg->mg_obj;
3543 const char *fptr = "msix";
3548 char need_newline = 0;
3549 U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12);
3551 while((ch = *fptr++)) {
3553 reflags[left++] = ch;
3556 reflags[right--] = ch;
3561 reflags[left] = '-';
3565 mg->mg_len = re->prelen + 4 + left;
3567 * If /x was used, we have to worry about a regex
3568 * ending with a comment later being embedded
3569 * within another regex. If so, we don't want this
3570 * regex's "commentization" to leak out to the
3571 * right part of the enclosing regex, we must cap
3572 * it with a newline.
3574 * So, if /x was used, we scan backwards from the
3575 * end of the regex. If we find a '#' before we
3576 * find a newline, we need to add a newline
3577 * ourself. If we find a '\n' first (or if we
3578 * don't find '#' or '\n'), we don't need to add
3579 * anything. -jfriedl
3581 if (PMf_EXTENDED & re->reganch)
3583 const char *endptr = re->precomp + re->prelen;
3584 while (endptr >= re->precomp)
3586 const char c = *(endptr--);
3588 break; /* don't need another */
3590 /* we end while in a comment, so we
3592 mg->mg_len++; /* save space for it */
3593 need_newline = 1; /* note to add it */
3599 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
3600 Copy("(?", mg->mg_ptr, 2, char);
3601 Copy(reflags, mg->mg_ptr+2, left, char);
3602 Copy(":", mg->mg_ptr+left+2, 1, char);
3603 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
3605 mg->mg_ptr[mg->mg_len - 2] = '\n';
3606 mg->mg_ptr[mg->mg_len - 1] = ')';
3607 mg->mg_ptr[mg->mg_len] = 0;
3609 PL_reginterp_cnt += re->program[0].next_off;
3611 if (re->reganch & ROPT_UTF8)
3626 case SVt_PVBM: typestr = SvROK(sv) ? "REF" : "SCALAR"; break;
3627 case SVt_PVLV: typestr = SvROK(sv) ? "REF"
3628 /* tied lvalues should appear to be
3629 * scalars for backwards compatitbility */
3630 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
3631 ? "SCALAR" : "LVALUE"; break;
3632 case SVt_PVAV: typestr = "ARRAY"; break;
3633 case SVt_PVHV: typestr = "HASH"; break;
3634 case SVt_PVCV: typestr = "CODE"; break;
3635 case SVt_PVGV: typestr = "GLOB"; break;
3636 case SVt_PVFM: typestr = "FORMAT"; break;
3637 case SVt_PVIO: typestr = "IO"; break;
3638 default: typestr = "UNKNOWN"; break;
3642 const char *name = HvNAME(SvSTASH(sv));
3643 Perl_sv_setpvf(aTHX_ tsv, "%s=%s(0x%"UVxf")",
3644 name ? name : "__ANON__" , typestr, PTR2UV(sv));
3647 Perl_sv_setpvf(aTHX_ tsv, "%s(0x%"UVxf")", typestr, PTR2UV(sv));
3650 *lp = strlen(typestr);
3651 return (char *)typestr;
3653 if (SvREADONLY(sv) && !SvOK(sv)) {
3654 if (ckWARN(WARN_UNINITIALIZED))
3660 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
3661 /* I'm assuming that if both IV and NV are equally valid then
3662 converting the IV is going to be more efficient */
3663 const U32 isIOK = SvIOK(sv);
3664 const U32 isUIOK = SvIsUV(sv);
3665 char buf[TYPE_CHARS(UV)];
3668 if (SvTYPE(sv) < SVt_PVIV)
3669 sv_upgrade(sv, SVt_PVIV);
3671 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
3673 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
3674 SvGROW(sv, (STRLEN)(ebuf - ptr + 1)); /* inlined from sv_setpvn */
3675 Move(ptr,SvPVX(sv),ebuf - ptr,char);
3676 SvCUR_set(sv, ebuf - ptr);
3686 else if (SvNOKp(sv)) {
3687 if (SvTYPE(sv) < SVt_PVNV)
3688 sv_upgrade(sv, SVt_PVNV);
3689 /* The +20 is pure guesswork. Configure test needed. --jhi */
3690 SvGROW(sv, NV_DIG + 20);
3692 olderrno = errno; /* some Xenix systems wipe out errno here */
3694 if (SvNVX(sv) == 0.0)
3695 (void)strcpy(s,"0");
3699 Gconvert(SvNVX(sv), NV_DIG, 0, s);
3702 #ifdef FIXNEGATIVEZERO
3703 if (*s == '-' && s[1] == '0' && !s[2])
3713 if (ckWARN(WARN_UNINITIALIZED)
3714 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
3717 if (SvTYPE(sv) < SVt_PV)
3718 /* Typically the caller expects that sv_any is not NULL now. */
3719 sv_upgrade(sv, SVt_PV);
3722 *lp = s - SvPVX(sv);
3725 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3726 PTR2UV(sv),SvPVX(sv)));
3730 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
3731 /* Sneaky stuff here */
3735 tsv = newSVpv(tmpbuf, 0);
3751 len = strlen(tmpbuf);
3753 #ifdef FIXNEGATIVEZERO
3754 if (len == 2 && t[0] == '-' && t[1] == '0') {
3759 (void)SvUPGRADE(sv, SVt_PV);
3761 s = SvGROW(sv, len + 1);
3764 return strcpy(s, t);
3769 =for apidoc sv_copypv
3771 Copies a stringified representation of the source SV into the
3772 destination SV. Automatically performs any necessary mg_get and
3773 coercion of numeric values into strings. Guaranteed to preserve
3774 UTF-8 flag even from overloaded objects. Similar in nature to
3775 sv_2pv[_flags] but operates directly on an SV instead of just the
3776 string. Mostly uses sv_2pv_flags to do its work, except when that
3777 would lose the UTF-8'ness of the PV.
3783 Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv)
3788 sv_setpvn(dsv,s,len);
3796 =for apidoc sv_2pvbyte_nolen
3798 Return a pointer to the byte-encoded representation of the SV.
3799 May cause the SV to be downgraded from UTF-8 as a side-effect.
3801 Usually accessed via the C<SvPVbyte_nolen> macro.
3807 Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
3810 return sv_2pvbyte(sv, &n_a);
3814 =for apidoc sv_2pvbyte
3816 Return a pointer to the byte-encoded representation of the SV, and set *lp
3817 to its length. May cause the SV to be downgraded from UTF-8 as a
3820 Usually accessed via the C<SvPVbyte> macro.
3826 Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
3828 sv_utf8_downgrade(sv,0);
3829 return SvPV(sv,*lp);
3833 =for apidoc sv_2pvutf8_nolen
3835 Return a pointer to the UTF-8-encoded representation of the SV.
3836 May cause the SV to be upgraded to UTF-8 as a side-effect.
3838 Usually accessed via the C<SvPVutf8_nolen> macro.
3844 Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
3847 return sv_2pvutf8(sv, &n_a);
3851 =for apidoc sv_2pvutf8
3853 Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
3854 to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
3856 Usually accessed via the C<SvPVutf8> macro.
3862 Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
3864 sv_utf8_upgrade(sv);
3865 return SvPV(sv,*lp);
3869 =for apidoc sv_2bool
3871 This function is only called on magical items, and is only used by
3872 sv_true() or its macro equivalent.
3878 Perl_sv_2bool(pTHX_ register SV *sv)
3887 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
3888 (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
3889 return (bool)SvTRUE(tmpsv);
3890 return SvRV(sv) != 0;
3893 register XPV* Xpvtmp;
3894 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
3895 (*Xpvtmp->xpv_pv > '0' ||
3896 Xpvtmp->xpv_cur > 1 ||
3897 (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0')))
3904 return SvIVX(sv) != 0;
3907 return SvNVX(sv) != 0.0;
3914 /* sv_utf8_upgrade() is now a macro using sv_utf8_upgrade_flags();
3915 * this function provided for binary compatibility only
3920 Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
3922 return sv_utf8_upgrade_flags(sv, SV_GMAGIC);
3926 =for apidoc sv_utf8_upgrade
3928 Converts the PV of an SV to its UTF-8-encoded form.
3929 Forces the SV to string form if it is not already.
3930 Always sets the SvUTF8 flag to avoid future validity checks even
3931 if all the bytes have hibit clear.
3933 This is not as a general purpose byte encoding to Unicode interface:
3934 use the Encode extension for that.
3936 =for apidoc sv_utf8_upgrade_flags
3938 Converts the PV of an SV to its UTF-8-encoded form.
3939 Forces the SV to string form if it is not already.
3940 Always sets the SvUTF8 flag to avoid future validity checks even
3941 if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
3942 will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
3943 C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
3945 This is not as a general purpose byte encoding to Unicode interface:
3946 use the Encode extension for that.
3952 Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags)
3957 if (sv == &PL_sv_undef)
3961 if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
3962 (void) sv_2pv_flags(sv,&len, flags);
3966 (void) SvPV_force(sv,len);
3975 sv_force_normal_flags(sv, 0);
3978 if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING))
3979 sv_recode_to_utf8(sv, PL_encoding);
3980 else { /* Assume Latin-1/EBCDIC */
3981 /* This function could be much more efficient if we
3982 * had a FLAG in SVs to signal if there are any hibit
3983 * chars in the PV. Given that there isn't such a flag
3984 * make the loop as fast as possible. */
3985 s = (U8 *) SvPVX(sv);
3986 e = (U8 *) SvEND(sv);
3990 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
3995 (void)SvOOK_off(sv);
3997 len = SvCUR(sv) + 1; /* Plus the \0 */
3998 SvPV_set(sv, (char*)bytes_to_utf8((U8*)s, &len));
3999 SvCUR_set(sv, len - 1);
4001 Safefree(s); /* No longer using what was there before. */
4002 SvLEN_set(sv, len); /* No longer know the real size. */
4004 /* Mark as UTF-8 even if no hibit - saves scanning loop */
4011 =for apidoc sv_utf8_downgrade
4013 Attempts to convert the PV of an SV from characters to bytes.
4014 If the PV contains a character beyond byte, this conversion will fail;
4015 in this case, either returns false or, if C<fail_ok> is not
4018 This is not as a general purpose Unicode to byte encoding interface:
4019 use the Encode extension for that.
4025 Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
4027 if (SvPOKp(sv) && SvUTF8(sv)) {
4033 sv_force_normal_flags(sv, 0);
4035 s = (U8 *) SvPV(sv, len);
4036 if (!utf8_to_bytes(s, &len)) {
4041 Perl_croak(aTHX_ "Wide character in %s",
4044 Perl_croak(aTHX_ "Wide character");
4055 =for apidoc sv_utf8_encode
4057 Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
4058 flag off so that it looks like octets again.
4064 Perl_sv_utf8_encode(pTHX_ register SV *sv)
4066 (void) sv_utf8_upgrade(sv);
4068 sv_force_normal_flags(sv, 0);
4070 if (SvREADONLY(sv)) {
4071 Perl_croak(aTHX_ PL_no_modify);
4077 =for apidoc sv_utf8_decode
4079 If the PV of the SV is an octet sequence in UTF-8
4080 and contains a multiple-byte character, the C<SvUTF8> flag is turned on
4081 so that it looks like a character. If the PV contains only single-byte
4082 characters, the C<SvUTF8> flag stays being off.
4083 Scans PV for validity and returns false if the PV is invalid UTF-8.
4089 Perl_sv_utf8_decode(pTHX_ register SV *sv)
4095 /* The octets may have got themselves encoded - get them back as
4098 if (!sv_utf8_downgrade(sv, TRUE))
4101 /* it is actually just a matter of turning the utf8 flag on, but
4102 * we want to make sure everything inside is valid utf8 first.
4104 c = (U8 *) SvPVX(sv);
4105 if (!is_utf8_string(c, SvCUR(sv)+1))
4107 e = (U8 *) SvEND(sv);
4110 if (!UTF8_IS_INVARIANT(ch)) {
4119 /* sv_setsv() is now a macro using Perl_sv_setsv_flags();
4120 * this function provided for binary compatibility only
4124 Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
4126 sv_setsv_flags(dstr, sstr, SV_GMAGIC);
4130 =for apidoc sv_setsv
4132 Copies the contents of the source SV C<ssv> into the destination SV
4133 C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
4134 function if the source SV needs to be reused. Does not handle 'set' magic.
4135 Loosely speaking, it performs a copy-by-value, obliterating any previous
4136 content of the destination.
4138 You probably want to use one of the assortment of wrappers, such as
4139 C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
4140 C<SvSetMagicSV_nosteal>.
4142 =for apidoc sv_setsv_flags
4144 Copies the contents of the source SV C<ssv> into the destination SV
4145 C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
4146 function if the source SV needs to be reused. Does not handle 'set' magic.
4147 Loosely speaking, it performs a copy-by-value, obliterating any previous
4148 content of the destination.
4149 If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
4150 C<ssv> if appropriate, else not. If the C<flags> parameter has the
4151 C<NOSTEAL> bit set then the buffers of temps will not be stolen. <sv_setsv>
4152 and C<sv_setsv_nomg> are implemented in terms of this function.
4154 You probably want to use one of the assortment of wrappers, such as
4155 C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
4156 C<SvSetMagicSV_nosteal>.
4158 This is the primary function for copying scalars, and most other
4159 copy-ish functions and macros use this underneath.
4165 Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags)
4167 register U32 sflags;
4173 SV_CHECK_THINKFIRST_COW_DROP(dstr);
4175 sstr = &PL_sv_undef;
4176 stype = SvTYPE(sstr);
4177 dtype = SvTYPE(dstr);
4182 /* need to nuke the magic */
4184 SvRMAGICAL_off(dstr);
4187 /* There's a lot of redundancy below but we're going for speed here */
4192 if (dtype != SVt_PVGV) {
4193 (void)SvOK_off(dstr);
4201 sv_upgrade(dstr, SVt_IV);
4204 sv_upgrade(dstr, SVt_PVNV);
4208 sv_upgrade(dstr, SVt_PVIV);
4211 (void)SvIOK_only(dstr);
4212 SvIV_set(dstr, SvIVX(sstr));
4215 if (SvTAINTED(sstr))
4226 sv_upgrade(dstr, SVt_NV);
4231 sv_upgrade(dstr, SVt_PVNV);
4234 SvNV_set(dstr, SvNVX(sstr));
4235 (void)SvNOK_only(dstr);
4236 if (SvTAINTED(sstr))
4244 sv_upgrade(dstr, SVt_RV);
4245 else if (dtype == SVt_PVGV &&
4246 SvROK(sstr) && SvTYPE(SvRV(sstr)) == SVt_PVGV) {
4249 if (GvIMPORTED(dstr) != GVf_IMPORTED
4250 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4252 GvIMPORTED_on(dstr);
4261 #ifdef PERL_COPY_ON_WRITE
4262 if ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS) {
4263 if (dtype < SVt_PVIV)
4264 sv_upgrade(dstr, SVt_PVIV);
4271 sv_upgrade(dstr, SVt_PV);
4274 if (dtype < SVt_PVIV)
4275 sv_upgrade(dstr, SVt_PVIV);
4278 if (dtype < SVt_PVNV)
4279 sv_upgrade(dstr, SVt_PVNV);
4286 Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0),
4289 Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0));
4293 if (dtype <= SVt_PVGV) {
4295 if (dtype != SVt_PVGV) {
4296 char *name = GvNAME(sstr);
4297 STRLEN len = GvNAMELEN(sstr);
4298 /* don't upgrade SVt_PVLV: it can hold a glob */
4299 if (dtype != SVt_PVLV)
4300 sv_upgrade(dstr, SVt_PVGV);
4301 sv_magic(dstr, dstr, PERL_MAGIC_glob, Nullch, 0);
4302 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
4303 GvNAME(dstr) = savepvn(name, len);
4304 GvNAMELEN(dstr) = len;
4305 SvFAKE_on(dstr); /* can coerce to non-glob */
4307 /* ahem, death to those who redefine active sort subs */
4308 else if (PL_curstackinfo->si_type == PERLSI_SORT
4309 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
4310 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
4313 #ifdef GV_UNIQUE_CHECK
4314 if (GvUNIQUE((GV*)dstr)) {
4315 Perl_croak(aTHX_ PL_no_modify);
4319 (void)SvOK_off(dstr);
4320 GvINTRO_off(dstr); /* one-shot flag */
4322 GvGP(dstr) = gp_ref(GvGP(sstr));
4323 if (SvTAINTED(sstr))
4325 if (GvIMPORTED(dstr) != GVf_IMPORTED
4326 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4328 GvIMPORTED_on(dstr);
4336 if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
4338 if ((int)SvTYPE(sstr) != stype) {
4339 stype = SvTYPE(sstr);
4340 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
4344 if (stype == SVt_PVLV)
4345 (void)SvUPGRADE(dstr, SVt_PVNV);
4347 (void)SvUPGRADE(dstr, (U32)stype);
4350 sflags = SvFLAGS(sstr);
4352 if (sflags & SVf_ROK) {
4353 if (dtype >= SVt_PV) {
4354 if (dtype == SVt_PVGV) {
4355 SV *sref = SvREFCNT_inc(SvRV(sstr));
4357 int intro = GvINTRO(dstr);
4359 #ifdef GV_UNIQUE_CHECK
4360 if (GvUNIQUE((GV*)dstr)) {
4361 Perl_croak(aTHX_ PL_no_modify);
4366 GvINTRO_off(dstr); /* one-shot flag */
4367 GvLINE(dstr) = CopLINE(PL_curcop);
4368 GvEGV(dstr) = (GV*)dstr;
4371 switch (SvTYPE(sref)) {
4374 SAVEGENERICSV(GvAV(dstr));
4376 dref = (SV*)GvAV(dstr);
4377 GvAV(dstr) = (AV*)sref;
4378 if (!GvIMPORTED_AV(dstr)
4379 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4381 GvIMPORTED_AV_on(dstr);
4386 SAVEGENERICSV(GvHV(dstr));
4388 dref = (SV*)GvHV(dstr);
4389 GvHV(dstr) = (HV*)sref;
4390 if (!GvIMPORTED_HV(dstr)
4391 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4393 GvIMPORTED_HV_on(dstr);
4398 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
4399 SvREFCNT_dec(GvCV(dstr));
4400 GvCV(dstr) = Nullcv;
4401 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
4402 PL_sub_generation++;
4404 SAVEGENERICSV(GvCV(dstr));
4407 dref = (SV*)GvCV(dstr);
4408 if (GvCV(dstr) != (CV*)sref) {
4409 CV* cv = GvCV(dstr);
4411 if (!GvCVGEN((GV*)dstr) &&
4412 (CvROOT(cv) || CvXSUB(cv)))
4414 /* ahem, death to those who redefine
4415 * active sort subs */
4416 if (PL_curstackinfo->si_type == PERLSI_SORT &&
4417 PL_sortcop == CvSTART(cv))
4419 "Can't redefine active sort subroutine %s",
4420 GvENAME((GV*)dstr));
4421 /* Redefining a sub - warning is mandatory if
4422 it was a const and its value changed. */
4423 if (ckWARN(WARN_REDEFINE)
4425 && (!CvCONST((CV*)sref)
4426 || sv_cmp(cv_const_sv(cv),
4427 cv_const_sv((CV*)sref)))))
4429 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
4431 ? "Constant subroutine %s::%s redefined"
4432 : "Subroutine %s::%s redefined",
4433 HvNAME(GvSTASH((GV*)dstr)),
4434 GvENAME((GV*)dstr));
4438 cv_ckproto(cv, (GV*)dstr,
4439 SvPOK(sref) ? SvPVX(sref) : Nullch);
4441 GvCV(dstr) = (CV*)sref;
4442 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
4443 GvASSUMECV_on(dstr);
4444 PL_sub_generation++;
4446 if (!GvIMPORTED_CV(dstr)
4447 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4449 GvIMPORTED_CV_on(dstr);
4454 SAVEGENERICSV(GvIOp(dstr));
4456 dref = (SV*)GvIOp(dstr);
4457 GvIOp(dstr) = (IO*)sref;
4461 SAVEGENERICSV(GvFORM(dstr));
4463 dref = (SV*)GvFORM(dstr);
4464 GvFORM(dstr) = (CV*)sref;
4468 SAVEGENERICSV(GvSV(dstr));
4470 dref = (SV*)GvSV(dstr);
4472 if (!GvIMPORTED_SV(dstr)
4473 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4475 GvIMPORTED_SV_on(dstr);
4481 if (SvTAINTED(sstr))
4486 (void)SvOOK_off(dstr); /* backoff */
4488 Safefree(SvPVX(dstr));
4493 (void)SvOK_off(dstr);
4494 SvRV_set(dstr, SvREFCNT_inc(SvRV(sstr)));
4496 if (sflags & SVp_NOK) {
4498 /* Only set the public OK flag if the source has public OK. */
4499 if (sflags & SVf_NOK)
4500 SvFLAGS(dstr) |= SVf_NOK;
4501 SvNV_set(dstr, SvNVX(sstr));
4503 if (sflags & SVp_IOK) {
4504 (void)SvIOKp_on(dstr);
4505 if (sflags & SVf_IOK)
4506 SvFLAGS(dstr) |= SVf_IOK;
4507 if (sflags & SVf_IVisUV)
4509 SvIV_set(dstr, SvIVX(sstr));
4511 if (SvAMAGIC(sstr)) {
4515 else if (sflags & SVp_POK) {
4519 * Check to see if we can just swipe the string. If so, it's a
4520 * possible small lose on short strings, but a big win on long ones.
4521 * It might even be a win on short strings if SvPVX(dstr)
4522 * has to be allocated and SvPVX(sstr) has to be freed.
4525 /* Whichever path we take through the next code, we want this true,
4526 and doing it now facilitates the COW check. */
4527 (void)SvPOK_only(dstr);
4530 #ifdef PERL_COPY_ON_WRITE
4531 (sflags & (SVf_FAKE | SVf_READONLY)) != (SVf_FAKE | SVf_READONLY)
4535 (sflags & SVs_TEMP) && /* slated for free anyway? */
4536 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
4537 (!(flags & SV_NOSTEAL)) &&
4538 /* and we're allowed to steal temps */
4539 SvREFCNT(sstr) == 1 && /* and no other references to it? */
4540 SvLEN(sstr) && /* and really is a string */
4541 /* and won't be needed again, potentially */
4542 !(PL_op && PL_op->op_type == OP_AASSIGN))
4543 #ifdef PERL_COPY_ON_WRITE
4544 && !((sflags & CAN_COW_MASK) == CAN_COW_FLAGS
4545 && (SvFLAGS(dstr) & CAN_COW_MASK) == CAN_COW_FLAGS
4546 && SvTYPE(sstr) >= SVt_PVIV)
4549 /* Failed the swipe test, and it's not a shared hash key either.
4550 Have to copy the string. */
4551 STRLEN len = SvCUR(sstr);
4552 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
4553 Move(SvPVX(sstr),SvPVX(dstr),len,char);
4554 SvCUR_set(dstr, len);
4555 *SvEND(dstr) = '\0';
4557 /* If PERL_COPY_ON_WRITE is not defined, then isSwipe will always
4559 #ifdef PERL_COPY_ON_WRITE
4560 /* Either it's a shared hash key, or it's suitable for
4561 copy-on-write or we can swipe the string. */
4563 PerlIO_printf(Perl_debug_log, "Copy on write: sstr --> dstr\n");
4568 /* I believe I should acquire a global SV mutex if
4569 it's a COW sv (not a shared hash key) to stop
4570 it going un copy-on-write.
4571 If the source SV has gone un copy on write between up there
4572 and down here, then (assert() that) it is of the correct
4573 form to make it copy on write again */
4574 if ((sflags & (SVf_FAKE | SVf_READONLY))
4575 != (SVf_FAKE | SVf_READONLY)) {
4576 SvREADONLY_on(sstr);
4578 /* Make the source SV into a loop of 1.
4579 (about to become 2) */
4580 SV_COW_NEXT_SV_SET(sstr, sstr);
4584 /* Initial code is common. */
4585 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
4587 SvFLAGS(dstr) &= ~SVf_OOK;
4588 Safefree(SvPVX(dstr) - SvIVX(dstr));
4590 else if (SvLEN(dstr))
4591 Safefree(SvPVX(dstr));
4594 #ifdef PERL_COPY_ON_WRITE
4596 /* making another shared SV. */
4597 STRLEN cur = SvCUR(sstr);
4598 STRLEN len = SvLEN(sstr);
4599 assert (SvTYPE(dstr) >= SVt_PVIV);
4601 /* SvIsCOW_normal */
4602 /* splice us in between source and next-after-source. */
4603 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4604 SV_COW_NEXT_SV_SET(sstr, dstr);
4605 SvPV_set(dstr, SvPVX(sstr));
4607 /* SvIsCOW_shared_hash */
4608 UV hash = SvUVX(sstr);
4609 DEBUG_C(PerlIO_printf(Perl_debug_log,
4610 "Copy on write: Sharing hash\n"));
4612 sharepvn(SvPVX(sstr),
4613 (sflags & SVf_UTF8?-cur:cur), hash));
4614 SvUV_set(dstr, hash);
4618 SvREADONLY_on(dstr);
4620 /* Relesase a global SV mutex. */
4624 { /* Passes the swipe test. */
4625 SvPV_set(dstr, SvPVX(sstr));
4626 SvLEN_set(dstr, SvLEN(sstr));
4627 SvCUR_set(dstr, SvCUR(sstr));
4630 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
4631 SvPV_set(sstr, Nullch);
4637 if (sflags & SVf_UTF8)
4640 if (sflags & SVp_NOK) {
4642 if (sflags & SVf_NOK)
4643 SvFLAGS(dstr) |= SVf_NOK;
4644 SvNV_set(dstr, SvNVX(sstr));
4646 if (sflags & SVp_IOK) {
4647 (void)SvIOKp_on(dstr);
4648 if (sflags & SVf_IOK)
4649 SvFLAGS(dstr) |= SVf_IOK;
4650 if (sflags & SVf_IVisUV)
4652 SvIV_set(dstr, SvIVX(sstr));
4655 MAGIC *smg = mg_find(sstr,PERL_MAGIC_vstring);
4656 sv_magic(dstr, NULL, PERL_MAGIC_vstring,
4657 smg->mg_ptr, smg->mg_len);
4658 SvRMAGICAL_on(dstr);
4661 else if (sflags & SVp_IOK) {
4662 if (sflags & SVf_IOK)
4663 (void)SvIOK_only(dstr);
4665 (void)SvOK_off(dstr);
4666 (void)SvIOKp_on(dstr);
4668 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
4669 if (sflags & SVf_IVisUV)
4671 SvIV_set(dstr, SvIVX(sstr));
4672 if (sflags & SVp_NOK) {
4673 if (sflags & SVf_NOK)
4674 (void)SvNOK_on(dstr);
4676 (void)SvNOKp_on(dstr);
4677 SvNV_set(dstr, SvNVX(sstr));
4680 else if (sflags & SVp_NOK) {
4681 if (sflags & SVf_NOK)
4682 (void)SvNOK_only(dstr);
4684 (void)SvOK_off(dstr);
4687 SvNV_set(dstr, SvNVX(sstr));
4690 if (dtype == SVt_PVGV) {
4691 if (ckWARN(WARN_MISC))
4692 Perl_warner(aTHX_ packWARN(WARN_MISC), "Undefined value assigned to typeglob");
4695 (void)SvOK_off(dstr);
4697 if (SvTAINTED(sstr))
4702 =for apidoc sv_setsv_mg
4704 Like C<sv_setsv>, but also handles 'set' magic.
4710 Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
4712 sv_setsv(dstr,sstr);
4716 #ifdef PERL_COPY_ON_WRITE
4718 Perl_sv_setsv_cow(pTHX_ SV *dstr, SV *sstr)
4720 STRLEN cur = SvCUR(sstr);
4721 STRLEN len = SvLEN(sstr);
4722 register char *new_pv;
4725 PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n",
4733 if (SvTHINKFIRST(dstr))
4734 sv_force_normal_flags(dstr, SV_COW_DROP_PV);
4735 else if (SvPVX(dstr))
4736 Safefree(SvPVX(dstr));
4740 (void)SvUPGRADE (dstr, SVt_PVIV);
4742 assert (SvPOK(sstr));
4743 assert (SvPOKp(sstr));
4744 assert (!SvIOK(sstr));
4745 assert (!SvIOKp(sstr));
4746 assert (!SvNOK(sstr));
4747 assert (!SvNOKp(sstr));
4749 if (SvIsCOW(sstr)) {
4751 if (SvLEN(sstr) == 0) {
4752 /* source is a COW shared hash key. */
4753 UV hash = SvUVX(sstr);
4754 DEBUG_C(PerlIO_printf(Perl_debug_log,