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_OLD_COPY_ON_WRITE
51 #define SV_COW_NEXT_SV(sv) INT2PTR(SV *,SvUVX(sv))
52 #define SV_COW_NEXT_SV_SET(current,next) SvUV_set(current, PTR2UV(next))
53 /* This is a pessimistic view. Scalar must be purely a read-write PV to copy-
57 /* ============================================================================
59 =head1 Allocation and deallocation of SVs.
61 An SV (or AV, HV, etc.) is allocated in two parts: the head (struct sv,
62 av, hv...) contains type and reference count information, as well as a
63 pointer to the body (struct xrv, xpv, xpviv...), which contains fields
64 specific to each type.
66 Normally, this allocation is done using arenas, which by default are
67 approximately 4K chunks of memory parcelled up into N heads or bodies. The
68 first slot in each arena is reserved, and is used to hold a link to the next
69 arena. In the case of heads, the unused first slot also contains some flags
70 and a note of the number of slots. Snaked through each arena chain is a
71 linked list of free items; when this becomes empty, an extra arena is
72 allocated and divided up into N items which are threaded into the free list.
74 The following global variables are associated with arenas:
76 PL_sv_arenaroot pointer to list of SV arenas
77 PL_sv_root pointer to list of free SV structures
79 PL_foo_arenaroot pointer to list of foo arenas,
80 PL_foo_root pointer to list of free foo bodies
81 ... for foo in xiv, xnv, xrv, xpv etc.
83 Note that some of the larger and more rarely used body types (eg xpvio)
84 are not allocated using arenas, but are instead just malloc()/free()ed as
85 required. Also, if PURIFY is defined, arenas are abandoned altogether,
86 with all items individually malloc()ed. In addition, a few SV heads are
87 not allocated from an arena, but are instead directly created as static
88 or auto variables, eg PL_sv_undef. The size of arenas can be changed from
89 the default by setting PERL_ARENA_SIZE appropriately at compile time.
91 The SV arena serves the secondary purpose of allowing still-live SVs
92 to be located and destroyed during final cleanup.
94 At the lowest level, the macros new_SV() and del_SV() grab and free
95 an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
96 to return the SV to the free list with error checking.) new_SV() calls
97 more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
98 SVs in the free list have their SvTYPE field set to all ones.
100 Similarly, there are macros new_XIV()/del_XIV(), new_XNV()/del_XNV() etc
101 that allocate and return individual body types. Normally these are mapped
102 to the arena-manipulating functions new_xiv()/del_xiv() etc, but may be
103 instead mapped directly to malloc()/free() if PURIFY is defined. The
104 new/del functions remove from, or add to, the appropriate PL_foo_root
105 list, and call more_xiv() etc to add a new arena if the list is empty.
107 At the time of very final cleanup, sv_free_arenas() is called from
108 perl_destruct() to physically free all the arenas allocated since the
109 start of the interpreter. Note that this also clears PL_he_arenaroot,
110 which is otherwise dealt with in hv.c.
112 Manipulation of any of the PL_*root pointers is protected by enclosing
113 LOCK_SV_MUTEX; ... UNLOCK_SV_MUTEX calls which should Do the Right Thing
114 if threads are enabled.
116 The function visit() scans the SV arenas list, and calls a specified
117 function for each SV it finds which is still live - ie which has an SvTYPE
118 other than all 1's, and a non-zero SvREFCNT. visit() is used by the
119 following functions (specified as [function that calls visit()] / [function
120 called by visit() for each SV]):
122 sv_report_used() / do_report_used()
123 dump all remaining SVs (debugging aid)
125 sv_clean_objs() / do_clean_objs(),do_clean_named_objs()
126 Attempt to free all objects pointed to by RVs,
127 and, unless DISABLE_DESTRUCTOR_KLUDGE is defined,
128 try to do the same for all objects indirectly
129 referenced by typeglobs too. Called once from
130 perl_destruct(), prior to calling sv_clean_all()
133 sv_clean_all() / do_clean_all()
134 SvREFCNT_dec(sv) each remaining SV, possibly
135 triggering an sv_free(). It also sets the
136 SVf_BREAK flag on the SV to indicate that the
137 refcnt has been artificially lowered, and thus
138 stopping sv_free() from giving spurious warnings
139 about SVs which unexpectedly have a refcnt
140 of zero. called repeatedly from perl_destruct()
141 until there are no SVs left.
145 Private API to rest of sv.c
149 new_XIV(), del_XIV(),
150 new_XNV(), del_XNV(),
155 sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
160 ============================================================================ */
165 * "A time to plant, and a time to uproot what was planted..."
169 * nice_chunk and nice_chunk size need to be set
170 * and queried under the protection of sv_mutex
173 Perl_offer_nice_chunk(pTHX_ void *chunk, U32 chunk_size)
178 new_chunk = (void *)(chunk);
179 new_chunk_size = (chunk_size);
180 if (new_chunk_size > PL_nice_chunk_size) {
181 Safefree(PL_nice_chunk);
182 PL_nice_chunk = (char *) new_chunk;
183 PL_nice_chunk_size = new_chunk_size;
190 #ifdef DEBUG_LEAKING_SCALARS
191 # define FREE_SV_DEBUG_FILE(sv) Safefree((sv)->sv_debug_file)
193 # define FREE_SV_DEBUG_FILE(sv)
196 #define plant_SV(p) \
198 FREE_SV_DEBUG_FILE(p); \
199 SvANY(p) = (void *)PL_sv_root; \
200 SvFLAGS(p) = SVTYPEMASK; \
205 /* sv_mutex must be held while calling uproot_SV() */
206 #define uproot_SV(p) \
209 PL_sv_root = (SV*)SvANY(p); \
214 /* make some more SVs by adding another arena */
216 /* sv_mutex must be held while calling more_sv() */
223 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
224 PL_nice_chunk = Nullch;
225 PL_nice_chunk_size = 0;
228 char *chunk; /* must use New here to match call to */
229 Newx(chunk,PERL_ARENA_SIZE,char); /* Safefree() in sv_free_arenas() */
230 sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
236 /* new_SV(): return a new, empty SV head */
238 #ifdef DEBUG_LEAKING_SCALARS
239 /* provide a real function for a debugger to play with */
249 sv = S_more_sv(aTHX);
254 sv->sv_debug_optype = PL_op ? PL_op->op_type : 0;
255 sv->sv_debug_line = (U16) ((PL_copline == NOLINE) ?
256 (PL_curcop ? CopLINE(PL_curcop) : 0) : PL_copline);
257 sv->sv_debug_inpad = 0;
258 sv->sv_debug_cloned = 0;
259 sv->sv_debug_file = PL_curcop ? savepv(CopFILE(PL_curcop)): NULL;
263 # define new_SV(p) (p)=S_new_SV(aTHX)
272 (p) = S_more_sv(aTHX); \
281 /* del_SV(): return an empty SV head to the free list */
296 S_del_sv(pTHX_ SV *p)
301 for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
302 const SV * const sv = sva + 1;
303 const SV * const svend = &sva[SvREFCNT(sva)];
304 if (p >= sv && p < svend) {
310 if (ckWARN_d(WARN_INTERNAL))
311 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
312 "Attempt to free non-arena SV: 0x%"UVxf
313 pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
320 #else /* ! DEBUGGING */
322 #define del_SV(p) plant_SV(p)
324 #endif /* DEBUGGING */
328 =head1 SV Manipulation Functions
330 =for apidoc sv_add_arena
332 Given a chunk of memory, link it to the head of the list of arenas,
333 and split it into a list of free SVs.
339 Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
345 /* The first SV in an arena isn't an SV. */
346 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
347 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
348 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
350 PL_sv_arenaroot = sva;
351 PL_sv_root = sva + 1;
353 svend = &sva[SvREFCNT(sva) - 1];
356 SvANY(sv) = (void *)(SV*)(sv + 1);
360 /* Must always set typemask because it's awlays checked in on cleanup
361 when the arenas are walked looking for objects. */
362 SvFLAGS(sv) = SVTYPEMASK;
369 SvFLAGS(sv) = SVTYPEMASK;
372 /* visit(): call the named function for each non-free SV in the arenas
373 * whose flags field matches the flags/mask args. */
376 S_visit(pTHX_ SVFUNC_t f, U32 flags, U32 mask)
381 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
382 register const SV * const svend = &sva[SvREFCNT(sva)];
384 for (sv = sva + 1; sv < svend; ++sv) {
385 if (SvTYPE(sv) != SVTYPEMASK
386 && (sv->sv_flags & mask) == flags
399 /* called by sv_report_used() for each live SV */
402 do_report_used(pTHX_ SV *sv)
404 if (SvTYPE(sv) != SVTYPEMASK) {
405 PerlIO_printf(Perl_debug_log, "****\n");
412 =for apidoc sv_report_used
414 Dump the contents of all SVs not yet freed. (Debugging aid).
420 Perl_sv_report_used(pTHX)
423 visit(do_report_used, 0, 0);
427 /* called by sv_clean_objs() for each live SV */
430 do_clean_objs(pTHX_ SV *ref)
433 SV * const target = SvRV(ref);
434 if (SvOBJECT(target)) {
435 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(ref)));
436 if (SvWEAKREF(ref)) {
437 sv_del_backref(target, ref);
443 SvREFCNT_dec(target);
448 /* XXX Might want to check arrays, etc. */
451 /* called by sv_clean_objs() for each live SV */
453 #ifndef DISABLE_DESTRUCTOR_KLUDGE
455 do_clean_named_objs(pTHX_ SV *sv)
457 if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) {
459 #ifdef PERL_DONT_CREATE_GVSV
462 SvOBJECT(GvSV(sv))) ||
463 (GvAV(sv) && SvOBJECT(GvAV(sv))) ||
464 (GvHV(sv) && SvOBJECT(GvHV(sv))) ||
465 (GvIO(sv) && SvOBJECT(GvIO(sv))) ||
466 (GvCV(sv) && SvOBJECT(GvCV(sv))) )
468 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv)));
469 SvFLAGS(sv) |= SVf_BREAK;
477 =for apidoc sv_clean_objs
479 Attempt to destroy all objects not yet freed
485 Perl_sv_clean_objs(pTHX)
487 PL_in_clean_objs = TRUE;
488 visit(do_clean_objs, SVf_ROK, SVf_ROK);
489 #ifndef DISABLE_DESTRUCTOR_KLUDGE
490 /* some barnacles may yet remain, clinging to typeglobs */
491 visit(do_clean_named_objs, SVt_PVGV, SVTYPEMASK);
493 PL_in_clean_objs = FALSE;
496 /* called by sv_clean_all() for each live SV */
499 do_clean_all(pTHX_ SV *sv)
501 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
502 SvFLAGS(sv) |= SVf_BREAK;
503 if (PL_comppad == (AV*)sv) {
505 PL_curpad = Null(SV**);
511 =for apidoc sv_clean_all
513 Decrement the refcnt of each remaining SV, possibly triggering a
514 cleanup. This function may have to be called multiple times to free
515 SVs which are in complex self-referential hierarchies.
521 Perl_sv_clean_all(pTHX)
524 PL_in_clean_all = TRUE;
525 cleaned = visit(do_clean_all, 0,0);
526 PL_in_clean_all = FALSE;
531 S_free_arena(pTHX_ void **root) {
533 void ** const next = *(void **)root;
540 =for apidoc sv_free_arenas
542 Deallocate the memory used by all arenas. Note that all the individual SV
543 heads and bodies within the arenas must already have been freed.
548 #define free_arena(name) \
550 S_free_arena(aTHX_ (void**) PL_ ## name ## _arenaroot); \
551 PL_ ## name ## _arenaroot = 0; \
552 PL_ ## name ## _root = 0; \
556 Perl_sv_free_arenas(pTHX)
561 /* Free arenas here, but be careful about fake ones. (We assume
562 contiguity of the fake ones with the corresponding real ones.) */
564 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
565 svanext = (SV*) SvANY(sva);
566 while (svanext && SvFAKE(svanext))
567 svanext = (SV*) SvANY(svanext);
585 #if defined(USE_ITHREADS)
589 Safefree(PL_nice_chunk);
590 PL_nice_chunk = Nullch;
591 PL_nice_chunk_size = 0;
596 /* ---------------------------------------------------------------------
598 * support functions for report_uninit()
601 /* the maxiumum size of array or hash where we will scan looking
602 * for the undefined element that triggered the warning */
604 #define FUV_MAX_SEARCH_SIZE 1000
606 /* Look for an entry in the hash whose value has the same SV as val;
607 * If so, return a mortal copy of the key. */
610 S_find_hash_subscript(pTHX_ HV *hv, SV* val)
616 if (!hv || SvMAGICAL(hv) || !HvARRAY(hv) ||
617 (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
622 for (i=HvMAX(hv); i>0; i--) {
624 for (entry = array[i]; entry; entry = HeNEXT(entry)) {
625 if (HeVAL(entry) != val)
627 if ( HeVAL(entry) == &PL_sv_undef ||
628 HeVAL(entry) == &PL_sv_placeholder)
632 if (HeKLEN(entry) == HEf_SVKEY)
633 return sv_mortalcopy(HeKEY_sv(entry));
634 return sv_2mortal(newSVpvn(HeKEY(entry), HeKLEN(entry)));
640 /* Look for an entry in the array whose value has the same SV as val;
641 * If so, return the index, otherwise return -1. */
644 S_find_array_subscript(pTHX_ AV *av, SV* val)
648 if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
649 (AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
653 for (i=AvFILLp(av); i>=0; i--) {
654 if (svp[i] == val && svp[i] != &PL_sv_undef)
660 /* S_varname(): return the name of a variable, optionally with a subscript.
661 * If gv is non-zero, use the name of that global, along with gvtype (one
662 * of "$", "@", "%"); otherwise use the name of the lexical at pad offset
663 * targ. Depending on the value of the subscript_type flag, return:
666 #define FUV_SUBSCRIPT_NONE 1 /* "@foo" */
667 #define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */
668 #define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */
669 #define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */
672 S_varname(pTHX_ GV *gv, const char gvtype, PADOFFSET targ,
673 SV* keyname, I32 aindex, int subscript_type)
676 SV * const name = sv_newmortal();
679 /* simulate gv_fullname4(), but add literal '^' for $^FOO names
680 * XXX get rid of all this if gv_fullnameX() ever supports this
684 HV * const hv = GvSTASH(gv);
687 else if (!(p=HvNAME_get(hv)))
689 if (strEQ(p, "main"))
690 sv_setpvn(name, &gvtype, 1);
692 Perl_sv_setpvf(aTHX_ name, "%c%s::", gvtype, p);
694 if (GvNAMELEN(gv)>= 1 &&
695 ((unsigned int)*GvNAME(gv)) <= 26)
697 Perl_sv_catpvf(aTHX_ name,"^%c", *GvNAME(gv) + 'A' - 1);
698 sv_catpvn(name,GvNAME(gv)+1,GvNAMELEN(gv)-1);
701 sv_catpvn(name,GvNAME(gv),GvNAMELEN(gv));
705 CV * const cv = find_runcv(&unused);
709 if (!cv || !CvPADLIST(cv))
711 av = (AV*)(*av_fetch(CvPADLIST(cv), 0, FALSE));
712 sv = *av_fetch(av, targ, FALSE);
713 /* SvLEN in a pad name is not to be trusted */
714 sv_setpv(name, SvPV_nolen_const(sv));
717 if (subscript_type == FUV_SUBSCRIPT_HASH) {
718 SV * const sv = NEWSV(0,0);
720 Perl_sv_catpvf(aTHX_ name, "{%s}",
721 pv_display(sv,SvPVX_const(keyname), SvCUR(keyname), 0, 32));
724 else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
726 Perl_sv_catpvf(aTHX_ name, "[%"IVdf"]", (IV)aindex);
728 else if (subscript_type == FUV_SUBSCRIPT_WITHIN)
729 sv_insert(name, 0, 0, "within ", 7);
736 =for apidoc find_uninit_var
738 Find the name of the undefined variable (if any) that caused the operator o
739 to issue a "Use of uninitialized value" warning.
740 If match is true, only return a name if it's value matches uninit_sv.
741 So roughly speaking, if a unary operator (such as OP_COS) generates a
742 warning, then following the direct child of the op may yield an
743 OP_PADSV or OP_GV that gives the name of the undefined variable. On the
744 other hand, with OP_ADD there are two branches to follow, so we only print
745 the variable name if we get an exact match.
747 The name is returned as a mortal SV.
749 Assumes that PL_op is the op that originally triggered the error, and that
750 PL_comppad/PL_curpad points to the currently executing pad.
756 S_find_uninit_var(pTHX_ OP* obase, SV* uninit_sv, bool match)
764 if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
765 uninit_sv == &PL_sv_placeholder)))
768 switch (obase->op_type) {
775 const bool pad = (obase->op_type == OP_PADAV || obase->op_type == OP_PADHV);
776 const bool hash = (obase->op_type == OP_PADHV || obase->op_type == OP_RV2HV);
779 int subscript_type = FUV_SUBSCRIPT_WITHIN;
781 if (pad) { /* @lex, %lex */
782 sv = PAD_SVl(obase->op_targ);
786 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
787 /* @global, %global */
788 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
791 sv = hash ? (SV*)GvHV(gv): (SV*)GvAV(gv);
793 else /* @{expr}, %{expr} */
794 return find_uninit_var(cUNOPx(obase)->op_first,
798 /* attempt to find a match within the aggregate */
800 keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
802 subscript_type = FUV_SUBSCRIPT_HASH;
805 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
807 subscript_type = FUV_SUBSCRIPT_ARRAY;
810 if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
813 return varname(gv, hash ? '%' : '@', obase->op_targ,
814 keysv, index, subscript_type);
818 if (match && PAD_SVl(obase->op_targ) != uninit_sv)
820 return varname(Nullgv, '$', obase->op_targ,
821 Nullsv, 0, FUV_SUBSCRIPT_NONE);
824 gv = cGVOPx_gv(obase);
825 if (!gv || (match && GvSV(gv) != uninit_sv))
827 return varname(gv, '$', 0, Nullsv, 0, FUV_SUBSCRIPT_NONE);
830 if (obase->op_flags & OPf_SPECIAL) { /* lexical array */
833 av = (AV*)PAD_SV(obase->op_targ);
834 if (!av || SvRMAGICAL(av))
836 svp = av_fetch(av, (I32)obase->op_private, FALSE);
837 if (!svp || *svp != uninit_sv)
840 return varname(Nullgv, '$', obase->op_targ,
841 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
844 gv = cGVOPx_gv(obase);
850 if (!av || SvRMAGICAL(av))
852 svp = av_fetch(av, (I32)obase->op_private, FALSE);
853 if (!svp || *svp != uninit_sv)
856 return varname(gv, '$', 0,
857 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
862 o = cUNOPx(obase)->op_first;
863 if (!o || o->op_type != OP_NULL ||
864 ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
866 return find_uninit_var(cBINOPo->op_last, uninit_sv, match);
871 /* $a[uninit_expr] or $h{uninit_expr} */
872 return find_uninit_var(cBINOPx(obase)->op_last, uninit_sv, match);
875 o = cBINOPx(obase)->op_first;
876 kid = cBINOPx(obase)->op_last;
878 /* get the av or hv, and optionally the gv */
880 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
881 sv = PAD_SV(o->op_targ);
883 else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
884 && cUNOPo->op_first->op_type == OP_GV)
886 gv = cGVOPx_gv(cUNOPo->op_first);
889 sv = o->op_type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)GvAV(gv);
894 if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
895 /* index is constant */
899 if (obase->op_type == OP_HELEM) {
900 HE* he = hv_fetch_ent((HV*)sv, cSVOPx_sv(kid), 0, 0);
901 if (!he || HeVAL(he) != uninit_sv)
905 SV ** const svp = av_fetch((AV*)sv, SvIV(cSVOPx_sv(kid)), FALSE);
906 if (!svp || *svp != uninit_sv)
910 if (obase->op_type == OP_HELEM)
911 return varname(gv, '%', o->op_targ,
912 cSVOPx_sv(kid), 0, FUV_SUBSCRIPT_HASH);
914 return varname(gv, '@', o->op_targ, Nullsv,
915 SvIV(cSVOPx_sv(kid)), FUV_SUBSCRIPT_ARRAY);
919 /* index is an expression;
920 * attempt to find a match within the aggregate */
921 if (obase->op_type == OP_HELEM) {
922 SV * const keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
924 return varname(gv, '%', o->op_targ,
925 keysv, 0, FUV_SUBSCRIPT_HASH);
928 const I32 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
930 return varname(gv, '@', o->op_targ,
931 Nullsv, index, FUV_SUBSCRIPT_ARRAY);
936 (o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
938 o->op_targ, Nullsv, 0, FUV_SUBSCRIPT_WITHIN);
944 /* only examine RHS */
945 return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv, match);
948 o = cUNOPx(obase)->op_first;
949 if (o->op_type == OP_PUSHMARK)
952 if (!o->op_sibling) {
953 /* one-arg version of open is highly magical */
955 if (o->op_type == OP_GV) { /* open FOO; */
957 if (match && GvSV(gv) != uninit_sv)
959 return varname(gv, '$', 0,
960 Nullsv, 0, FUV_SUBSCRIPT_NONE);
962 /* other possibilities not handled are:
963 * open $x; or open my $x; should return '${*$x}'
964 * open expr; should return '$'.expr ideally
970 /* ops where $_ may be an implicit arg */
974 if ( !(obase->op_flags & OPf_STACKED)) {
975 if (uninit_sv == ((obase->op_private & OPpTARGET_MY)
976 ? PAD_SVl(obase->op_targ)
980 sv_setpvn(sv, "$_", 2);
988 /* skip filehandle as it can't produce 'undef' warning */
989 o = cUNOPx(obase)->op_first;
990 if ((obase->op_flags & OPf_STACKED) && o->op_type == OP_PUSHMARK)
991 o = o->op_sibling->op_sibling;
998 match = 1; /* XS or custom code could trigger random warnings */
1003 if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs))
1004 return sv_2mortal(newSVpvn("${$/}", 5));
1009 if (!(obase->op_flags & OPf_KIDS))
1011 o = cUNOPx(obase)->op_first;
1017 /* if all except one arg are constant, or have no side-effects,
1018 * or are optimized away, then it's unambiguous */
1020 for (kid=o; kid; kid = kid->op_sibling) {
1022 ( (kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid)))
1023 || (kid->op_type == OP_NULL && ! (kid->op_flags & OPf_KIDS))
1024 || (kid->op_type == OP_PUSHMARK)
1028 if (o2) { /* more than one found */
1035 return find_uninit_var(o2, uninit_sv, match);
1039 sv = find_uninit_var(o, uninit_sv, 1);
1051 =for apidoc report_uninit
1053 Print appropriate "Use of uninitialized variable" warning
1059 Perl_report_uninit(pTHX_ SV* uninit_sv)
1062 SV* varname = Nullsv;
1064 varname = find_uninit_var(PL_op, uninit_sv,0);
1066 sv_insert(varname, 0, 0, " ", 1);
1068 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
1069 varname ? SvPV_nolen_const(varname) : "",
1070 " in ", OP_DESC(PL_op));
1073 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
1078 S_more_bodies (pTHX_ void **arena_root, void **root, size_t size)
1082 const size_t count = PERL_ARENA_SIZE/size;
1083 Newx(start, count*size, char);
1084 *((void **) start) = *arena_root;
1085 *arena_root = (void *)start;
1087 end = start + (count-1) * size;
1089 /* The initial slot is used to link the arenas together, so it isn't to be
1090 linked into the list of ready-to-use bodies. */
1094 *root = (void *)start;
1096 while (start < end) {
1097 char * const next = start + size;
1098 *(void**) start = (void *)next;
1101 *(void **)start = 0;
1106 /* grab a new thing from the free list, allocating more if necessary */
1108 /* 1st, the inline version */
1110 #define new_body_inline(xpv, arena_root, root, size) \
1113 xpv = *((void **)(root)) \
1114 ? *((void **)(root)) : S_more_bodies(aTHX_ arena_root, root, size); \
1115 *(root) = *(void**)(xpv); \
1119 /* now use the inline version in the proper function */
1122 S_new_body(pTHX_ void **arena_root, void **root, size_t size)
1125 new_body_inline(xpv, arena_root, root, size);
1129 /* return a thing to the free list */
1131 #define del_body(thing, root) \
1133 void **thing_copy = (void **)thing; \
1135 *thing_copy = *root; \
1136 *root = (void*)thing_copy; \
1140 /* Conventionally we simply malloc() a big block of memory, then divide it
1141 up into lots of the thing that we're allocating.
1143 This macro will expand to call to S_new_body. So for XPVBM (with ithreads),
1146 S_new_body(my_perl, (void**)&(my_perl->Ixpvbm_arenaroot),
1147 (void**)&(my_perl->Ixpvbm_root), sizeof(XPVBM), 0)
1150 #define new_body_type(TYPE,lctype) \
1151 S_new_body(aTHX_ (void**)&PL_ ## lctype ## _arenaroot, \
1152 (void**)&PL_ ## lctype ## _root, \
1155 #define del_body_type(p,TYPE,lctype) \
1156 del_body((void*)p, (void**)&PL_ ## lctype ## _root)
1158 /* But for some types, we cheat. The type starts with some members that are
1159 never accessed. So we allocate the substructure, starting at the first used
1160 member, then adjust the pointer back in memory by the size of the bit not
1161 allocated, so it's as if we allocated the full structure.
1162 (But things will all go boom if you write to the part that is "not there",
1163 because you'll be overwriting the last members of the preceding structure
1166 We calculate the correction using the STRUCT_OFFSET macro. For example, if
1167 xpv_allocated is the same structure as XPV then the two OFFSETs sum to zero,
1168 and the pointer is unchanged. If the allocated structure is smaller (no
1169 initial NV actually allocated) then the net effect is to subtract the size
1170 of the NV from the pointer, to return a new pointer as if an initial NV were
1173 This is the same trick as was used for NV and IV bodies. Ironically it
1174 doesn't need to be used for NV bodies any more, because NV is now at the
1175 start of the structure. IV bodies don't need it either, because they are
1176 no longer allocated. */
1178 #define new_body_allocated(TYPE,lctype,member) \
1179 (void*)((char*)S_new_body(aTHX_ (void**)&PL_ ## lctype ## _arenaroot, \
1180 (void**)&PL_ ## lctype ## _root, \
1181 sizeof(lctype ## _allocated)) - \
1182 STRUCT_OFFSET(TYPE, member) \
1183 + STRUCT_OFFSET(lctype ## _allocated, member))
1186 #define del_body_allocated(p,TYPE,lctype,member) \
1187 del_body((void*)((char*)p + STRUCT_OFFSET(TYPE, member) \
1188 - STRUCT_OFFSET(lctype ## _allocated, member)), \
1189 (void**)&PL_ ## lctype ## _root)
1191 #define my_safemalloc(s) (void*)safemalloc(s)
1192 #define my_safefree(p) safefree((char*)p)
1196 #define new_XNV() my_safemalloc(sizeof(XPVNV))
1197 #define del_XNV(p) my_safefree(p)
1199 #define new_XPV() my_safemalloc(sizeof(XPV))
1200 #define del_XPV(p) my_safefree(p)
1202 #define new_XPVIV() my_safemalloc(sizeof(XPVIV))
1203 #define del_XPVIV(p) my_safefree(p)
1205 #define new_XPVNV() my_safemalloc(sizeof(XPVNV))
1206 #define del_XPVNV(p) my_safefree(p)
1208 #define new_XPVCV() my_safemalloc(sizeof(XPVCV))
1209 #define del_XPVCV(p) my_safefree(p)
1211 #define new_XPVAV() my_safemalloc(sizeof(XPVAV))
1212 #define del_XPVAV(p) my_safefree(p)
1214 #define new_XPVHV() my_safemalloc(sizeof(XPVHV))
1215 #define del_XPVHV(p) my_safefree(p)
1217 #define new_XPVMG() my_safemalloc(sizeof(XPVMG))
1218 #define del_XPVMG(p) my_safefree(p)
1220 #define new_XPVGV() my_safemalloc(sizeof(XPVGV))
1221 #define del_XPVGV(p) my_safefree(p)
1223 #define new_XPVLV() my_safemalloc(sizeof(XPVLV))
1224 #define del_XPVLV(p) my_safefree(p)
1226 #define new_XPVBM() my_safemalloc(sizeof(XPVBM))
1227 #define del_XPVBM(p) my_safefree(p)
1231 #define new_XNV() new_body_type(NV, xnv)
1232 #define del_XNV(p) del_body_type(p, NV, xnv)
1234 #define new_XPV() new_body_allocated(XPV, xpv, xpv_cur)
1235 #define del_XPV(p) del_body_allocated(p, XPV, xpv, xpv_cur)
1237 #define new_XPVIV() new_body_allocated(XPVIV, xpviv, xpv_cur)
1238 #define del_XPVIV(p) del_body_allocated(p, XPVIV, xpviv, xpv_cur)
1240 #define new_XPVNV() new_body_type(XPVNV, xpvnv)
1241 #define del_XPVNV(p) del_body_type(p, XPVNV, xpvnv)
1243 #define new_XPVCV() new_body_type(XPVCV, xpvcv)
1244 #define del_XPVCV(p) del_body_type(p, XPVCV, xpvcv)
1246 #define new_XPVAV() new_body_allocated(XPVAV, xpvav, xav_fill)
1247 #define del_XPVAV(p) del_body_allocated(p, XPVAV, xpvav, xav_fill)
1249 #define new_XPVHV() new_body_allocated(XPVHV, xpvhv, xhv_fill)
1250 #define del_XPVHV(p) del_body_allocated(p, XPVHV, xpvhv, xhv_fill)
1252 #define new_XPVMG() new_body_type(XPVMG, xpvmg)
1253 #define del_XPVMG(p) del_body_type(p, XPVMG, xpvmg)
1255 #define new_XPVGV() new_body_type(XPVGV, xpvgv)
1256 #define del_XPVGV(p) del_body_type(p, XPVGV, xpvgv)
1258 #define new_XPVLV() new_body_type(XPVLV, xpvlv)
1259 #define del_XPVLV(p) del_body_type(p, XPVLV, xpvlv)
1261 #define new_XPVBM() new_body_type(XPVBM, xpvbm)
1262 #define del_XPVBM(p) del_body_type(p, XPVBM, xpvbm)
1266 #define new_XPVFM() my_safemalloc(sizeof(XPVFM))
1267 #define del_XPVFM(p) my_safefree(p)
1269 #define new_XPVIO() my_safemalloc(sizeof(XPVIO))
1270 #define del_XPVIO(p) my_safefree(p)
1273 =for apidoc sv_upgrade
1275 Upgrade an SV to a more complex form. Generally adds a new body type to the
1276 SV, then copies across as much information as possible from the old body.
1277 You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>.
1283 Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
1285 void** old_body_arena;
1286 size_t old_body_offset;
1287 size_t old_body_length; /* Well, the length to copy. */
1289 #ifndef NV_ZERO_IS_ALLBITS_ZERO
1290 /* If NV 0.0 is store as all bits 0 then Zero() already creates a correct
1292 bool zero_nv = TRUE;
1295 size_t new_body_length;
1296 size_t new_body_offset;
1297 void** new_body_arena;
1298 void** new_body_arenaroot;
1299 const U32 old_type = SvTYPE(sv);
1301 if (mt != SVt_PV && SvIsCOW(sv)) {
1302 sv_force_normal_flags(sv, 0);
1305 if (SvTYPE(sv) == mt)
1308 if (SvTYPE(sv) > mt)
1309 Perl_croak(aTHX_ "sv_upgrade from type %d down to type %d",
1310 (int)SvTYPE(sv), (int)mt);
1313 old_body = SvANY(sv);
1315 old_body_offset = 0;
1316 old_body_length = 0;
1317 new_body_offset = 0;
1318 new_body_length = ~0;
1320 /* Copying structures onto other structures that have been neatly zeroed
1321 has a subtle gotcha. Consider XPVMG
1323 +------+------+------+------+------+-------+-------+
1324 | NV | CUR | LEN | IV | MAGIC | STASH |
1325 +------+------+------+------+------+-------+-------+
1326 0 4 8 12 16 20 24 28
1328 where NVs are aligned to 8 bytes, so that sizeof that structure is
1329 actually 32 bytes long, with 4 bytes of padding at the end:
1331 +------+------+------+------+------+-------+-------+------+
1332 | NV | CUR | LEN | IV | MAGIC | STASH | ??? |
1333 +------+------+------+------+------+-------+-------+------+
1334 0 4 8 12 16 20 24 28 32
1336 so what happens if you allocate memory for this structure:
1338 +------+------+------+------+------+-------+-------+------+------+...
1339 | NV | CUR | LEN | IV | MAGIC | STASH | GP | NAME |
1340 +------+------+------+------+------+-------+-------+------+------+...
1341 0 4 8 12 16 20 24 28 32 36
1343 zero it, then copy sizeof(XPVMG) bytes on top of it? Not quite what you
1344 expect, because you copy the area marked ??? onto GP. Now, ??? may have
1345 started out as zero once, but it's quite possible that it isn't. So now,
1346 rather than a nicely zeroed GP, you have it pointing somewhere random.
1349 (In fact, GP ends up pointing at a previous GP structure, because the
1350 principle cause of the padding in XPVMG getting garbage is a copy of
1351 sizeof(XPVMG) bytes from a XPVGV structure in sv_unglob)
1353 So we are careful and work out the size of used parts of all the
1356 switch (SvTYPE(sv)) {
1362 else if (mt < SVt_PVIV)
1364 old_body_offset = STRUCT_OFFSET(XPVIV, xiv_iv);
1365 old_body_length = sizeof(IV);
1368 old_body_arena = (void **) &PL_xnv_root;
1369 old_body_length = sizeof(NV);
1370 #ifndef NV_ZERO_IS_ALLBITS_ZERO
1379 old_body_arena = (void **) &PL_xpv_root;
1380 old_body_offset = STRUCT_OFFSET(XPV, xpv_cur)
1381 - STRUCT_OFFSET(xpv_allocated, xpv_cur);
1382 old_body_length = STRUCT_OFFSET(XPV, xpv_len)
1383 + sizeof (((XPV*)SvANY(sv))->xpv_len)
1387 else if (mt == SVt_NV)
1391 old_body_arena = (void **) &PL_xpviv_root;
1392 old_body_offset = STRUCT_OFFSET(XPVIV, xpv_cur)
1393 - STRUCT_OFFSET(xpviv_allocated, xpv_cur);
1394 old_body_length = STRUCT_OFFSET(XPVIV, xiv_u)
1395 + sizeof (((XPVIV*)SvANY(sv))->xiv_u)
1399 old_body_arena = (void **) &PL_xpvnv_root;
1400 old_body_length = STRUCT_OFFSET(XPVNV, xiv_u)
1401 + sizeof (((XPVNV*)SvANY(sv))->xiv_u);
1402 #ifndef NV_ZERO_IS_ALLBITS_ZERO
1407 /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
1408 there's no way that it can be safely upgraded, because perl.c
1409 expects to Safefree(SvANY(PL_mess_sv)) */
1410 assert(sv != PL_mess_sv);
1411 /* This flag bit is used to mean other things in other scalar types.
1412 Given that it only has meaning inside the pad, it shouldn't be set
1413 on anything that can get upgraded. */
1414 assert((SvFLAGS(sv) & SVpad_TYPED) == 0);
1415 old_body_arena = (void **) &PL_xpvmg_root;
1416 old_body_length = STRUCT_OFFSET(XPVMG, xmg_stash)
1417 + sizeof (((XPVMG*)SvANY(sv))->xmg_stash);
1418 #ifndef NV_ZERO_IS_ALLBITS_ZERO
1423 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
1426 SvFLAGS(sv) &= ~SVTYPEMASK;
1431 Perl_croak(aTHX_ "Can't upgrade to undef");
1433 assert(old_type == SVt_NULL);
1434 SvANY(sv) = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
1438 assert(old_type == SVt_NULL);
1439 SvANY(sv) = new_XNV();
1443 assert(old_type == SVt_NULL);
1444 SvANY(sv) = &sv->sv_u.svu_rv;
1448 SvANY(sv) = new_XPVHV();
1451 HvTOTALKEYS(sv) = 0;
1456 SvANY(sv) = new_XPVAV();
1463 /* SVt_NULL isn't the only thing upgraded to AV or HV.
1464 The target created by newSVrv also is, and it can have magic.
1465 However, it never has SvPVX set.
1467 if (old_type >= SVt_RV) {
1468 assert(SvPVX_const(sv) == 0);
1471 /* Could put this in the else clause below, as PVMG must have SvPVX
1472 0 already (the assertion above) */
1473 SvPV_set(sv, (char*)0);
1475 if (old_type >= SVt_PVMG) {
1476 SvMAGIC_set(sv, ((XPVMG*)old_body)->xmg_magic);
1477 SvSTASH_set(sv, ((XPVMG*)old_body)->xmg_stash);
1485 new_body = new_XPVIO();
1486 new_body_length = sizeof(XPVIO);
1489 new_body = new_XPVFM();
1490 new_body_length = sizeof(XPVFM);
1494 new_body_length = sizeof(XPVBM);
1495 new_body_arena = (void **) &PL_xpvbm_root;
1496 new_body_arenaroot = (void **) &PL_xpvbm_arenaroot;
1499 new_body_length = sizeof(XPVGV);
1500 new_body_arena = (void **) &PL_xpvgv_root;
1501 new_body_arenaroot = (void **) &PL_xpvgv_arenaroot;
1504 new_body_length = sizeof(XPVCV);
1505 new_body_arena = (void **) &PL_xpvcv_root;
1506 new_body_arenaroot = (void **) &PL_xpvcv_arenaroot;
1509 new_body_length = sizeof(XPVLV);
1510 new_body_arena = (void **) &PL_xpvlv_root;
1511 new_body_arenaroot = (void **) &PL_xpvlv_arenaroot;
1514 new_body_length = sizeof(XPVMG);
1515 new_body_arena = (void **) &PL_xpvmg_root;
1516 new_body_arenaroot = (void **) &PL_xpvmg_arenaroot;
1519 new_body_length = sizeof(XPVNV);
1520 new_body_arena = (void **) &PL_xpvnv_root;
1521 new_body_arenaroot = (void **) &PL_xpvnv_arenaroot;
1524 new_body_offset = STRUCT_OFFSET(XPVIV, xpv_cur)
1525 - STRUCT_OFFSET(xpviv_allocated, xpv_cur);
1526 new_body_length = sizeof(XPVIV) - new_body_offset;
1527 new_body_arena = (void **) &PL_xpviv_root;
1528 new_body_arenaroot = (void **) &PL_xpviv_arenaroot;
1529 /* XXX Is this still needed? Was it ever needed? Surely as there is
1530 no route from NV to PVIV, NOK can never be true */
1534 goto new_body_no_NV;
1536 new_body_offset = STRUCT_OFFSET(XPV, xpv_cur)
1537 - STRUCT_OFFSET(xpv_allocated, xpv_cur);
1538 new_body_length = sizeof(XPV) - new_body_offset;
1539 new_body_arena = (void **) &PL_xpv_root;
1540 new_body_arenaroot = (void **) &PL_xpv_arenaroot;
1542 /* PV and PVIV don't have an NV slot. */
1543 #ifndef NV_ZERO_IS_ALLBITS_ZERO
1548 assert(new_body_length);
1550 /* This points to the start of the allocated area. */
1551 new_body_inline(new_body, new_body_arenaroot, new_body_arena,
1554 /* We always allocated the full length item with PURIFY */
1555 new_body_length += new_body_offset;
1556 new_body_offset = 0;
1557 new_body = my_safemalloc(new_body_length);
1561 Zero(new_body, new_body_length, char);
1562 new_body = ((char *)new_body) - new_body_offset;
1563 SvANY(sv) = new_body;
1565 if (old_body_length) {
1566 Copy((char *)old_body + old_body_offset,
1567 (char *)new_body + old_body_offset,
1568 old_body_length, char);
1571 #ifndef NV_ZERO_IS_ALLBITS_ZERO
1577 IoPAGE_LEN(sv) = 60;
1578 if (old_type < SVt_RV)
1582 Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu", mt);
1586 if (old_body_arena) {
1588 my_safefree(old_body);
1590 del_body((void*)((char*)old_body + old_body_offset),
1597 =for apidoc sv_backoff
1599 Remove any string offset. You should normally use the C<SvOOK_off> macro
1606 Perl_sv_backoff(pTHX_ register SV *sv)
1609 assert(SvTYPE(sv) != SVt_PVHV);
1610 assert(SvTYPE(sv) != SVt_PVAV);
1612 const char * const s = SvPVX_const(sv);
1613 SvLEN_set(sv, SvLEN(sv) + SvIVX(sv));
1614 SvPV_set(sv, SvPVX(sv) - SvIVX(sv));
1616 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
1618 SvFLAGS(sv) &= ~SVf_OOK;
1625 Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
1626 upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1627 Use the C<SvGROW> wrapper instead.
1633 Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
1637 #ifdef HAS_64K_LIMIT
1638 if (newlen >= 0x10000) {
1639 PerlIO_printf(Perl_debug_log,
1640 "Allocation too large: %"UVxf"\n", (UV)newlen);
1643 #endif /* HAS_64K_LIMIT */
1646 if (SvTYPE(sv) < SVt_PV) {
1647 sv_upgrade(sv, SVt_PV);
1648 s = SvPVX_mutable(sv);
1650 else if (SvOOK(sv)) { /* pv is offset? */
1652 s = SvPVX_mutable(sv);
1653 if (newlen > SvLEN(sv))
1654 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
1655 #ifdef HAS_64K_LIMIT
1656 if (newlen >= 0x10000)
1661 s = SvPVX_mutable(sv);
1663 if (newlen > SvLEN(sv)) { /* need more room? */
1664 newlen = PERL_STRLEN_ROUNDUP(newlen);
1665 if (SvLEN(sv) && s) {
1667 const STRLEN l = malloced_size((void*)SvPVX_const(sv));
1673 s = saferealloc(s, newlen);
1676 s = safemalloc(newlen);
1677 if (SvPVX_const(sv) && SvCUR(sv)) {
1678 Move(SvPVX_const(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
1682 SvLEN_set(sv, newlen);
1688 =for apidoc sv_setiv
1690 Copies an integer into the given SV, upgrading first if necessary.
1691 Does not handle 'set' magic. See also C<sv_setiv_mg>.
1697 Perl_sv_setiv(pTHX_ register SV *sv, IV i)
1699 SV_CHECK_THINKFIRST_COW_DROP(sv);
1700 switch (SvTYPE(sv)) {
1702 sv_upgrade(sv, SVt_IV);
1705 sv_upgrade(sv, SVt_PVNV);
1709 sv_upgrade(sv, SVt_PVIV);
1718 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
1721 (void)SvIOK_only(sv); /* validate number */
1727 =for apidoc sv_setiv_mg
1729 Like C<sv_setiv>, but also handles 'set' magic.
1735 Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
1742 =for apidoc sv_setuv
1744 Copies an unsigned integer into the given SV, upgrading first if necessary.
1745 Does not handle 'set' magic. See also C<sv_setuv_mg>.
1751 Perl_sv_setuv(pTHX_ register SV *sv, UV u)
1753 /* With these two if statements:
1754 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
1757 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
1759 If you wish to remove them, please benchmark to see what the effect is
1761 if (u <= (UV)IV_MAX) {
1762 sv_setiv(sv, (IV)u);
1771 =for apidoc sv_setuv_mg
1773 Like C<sv_setuv>, but also handles 'set' magic.
1779 Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
1788 =for apidoc sv_setnv
1790 Copies a double into the given SV, upgrading first if necessary.
1791 Does not handle 'set' magic. See also C<sv_setnv_mg>.
1797 Perl_sv_setnv(pTHX_ register SV *sv, NV num)
1799 SV_CHECK_THINKFIRST_COW_DROP(sv);
1800 switch (SvTYPE(sv)) {
1803 sv_upgrade(sv, SVt_NV);
1808 sv_upgrade(sv, SVt_PVNV);
1817 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
1821 (void)SvNOK_only(sv); /* validate number */
1826 =for apidoc sv_setnv_mg
1828 Like C<sv_setnv>, but also handles 'set' magic.
1834 Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
1840 /* Print an "isn't numeric" warning, using a cleaned-up,
1841 * printable version of the offending string
1845 S_not_a_number(pTHX_ SV *sv)
1852 dsv = sv_2mortal(newSVpvn("", 0));
1853 pv = sv_uni_display(dsv, sv, 10, 0);
1856 const char * const limit = tmpbuf + sizeof(tmpbuf) - 8;
1857 /* each *s can expand to 4 chars + "...\0",
1858 i.e. need room for 8 chars */
1860 const char *s, *end;
1861 for (s = SvPVX_const(sv), end = s + SvCUR(sv); s < end && d < limit;
1864 if (ch & 128 && !isPRINT_LC(ch)) {
1873 else if (ch == '\r') {
1877 else if (ch == '\f') {
1881 else if (ch == '\\') {
1885 else if (ch == '\0') {
1889 else if (isPRINT_LC(ch))
1906 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1907 "Argument \"%s\" isn't numeric in %s", pv,
1910 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1911 "Argument \"%s\" isn't numeric", pv);
1915 =for apidoc looks_like_number
1917 Test if the content of an SV looks like a number (or is a number).
1918 C<Inf> and C<Infinity> are treated as numbers (so will not issue a
1919 non-numeric warning), even if your atof() doesn't grok them.
1925 Perl_looks_like_number(pTHX_ SV *sv)
1927 register const char *sbegin;
1931 sbegin = SvPVX_const(sv);
1934 else if (SvPOKp(sv))
1935 sbegin = SvPV_const(sv, len);
1937 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
1938 return grok_number(sbegin, len, NULL);
1941 /* Actually, ISO C leaves conversion of UV to IV undefined, but
1942 until proven guilty, assume that things are not that bad... */
1947 As 64 bit platforms often have an NV that doesn't preserve all bits of
1948 an IV (an assumption perl has been based on to date) it becomes necessary
1949 to remove the assumption that the NV always carries enough precision to
1950 recreate the IV whenever needed, and that the NV is the canonical form.
1951 Instead, IV/UV and NV need to be given equal rights. So as to not lose
1952 precision as a side effect of conversion (which would lead to insanity
1953 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1954 1) to distinguish between IV/UV/NV slots that have cached a valid
1955 conversion where precision was lost and IV/UV/NV slots that have a
1956 valid conversion which has lost no precision
1957 2) to ensure that if a numeric conversion to one form is requested that
1958 would lose precision, the precise conversion (or differently
1959 imprecise conversion) is also performed and cached, to prevent
1960 requests for different numeric formats on the same SV causing
1961 lossy conversion chains. (lossless conversion chains are perfectly
1966 SvIOKp is true if the IV slot contains a valid value
1967 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
1968 SvNOKp is true if the NV slot contains a valid value
1969 SvNOK is true only if the NV value is accurate
1972 while converting from PV to NV, check to see if converting that NV to an
1973 IV(or UV) would lose accuracy over a direct conversion from PV to
1974 IV(or UV). If it would, cache both conversions, return NV, but mark
1975 SV as IOK NOKp (ie not NOK).
1977 While converting from PV to IV, check to see if converting that IV to an
1978 NV would lose accuracy over a direct conversion from PV to NV. If it
1979 would, cache both conversions, flag similarly.
1981 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
1982 correctly because if IV & NV were set NV *always* overruled.
1983 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
1984 changes - now IV and NV together means that the two are interchangeable:
1985 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
1987 The benefit of this is that operations such as pp_add know that if
1988 SvIOK is true for both left and right operands, then integer addition
1989 can be used instead of floating point (for cases where the result won't
1990 overflow). Before, floating point was always used, which could lead to
1991 loss of precision compared with integer addition.
1993 * making IV and NV equal status should make maths accurate on 64 bit
1995 * may speed up maths somewhat if pp_add and friends start to use
1996 integers when possible instead of fp. (Hopefully the overhead in
1997 looking for SvIOK and checking for overflow will not outweigh the
1998 fp to integer speedup)
1999 * will slow down integer operations (callers of SvIV) on "inaccurate"
2000 values, as the change from SvIOK to SvIOKp will cause a call into
2001 sv_2iv each time rather than a macro access direct to the IV slot
2002 * should speed up number->string conversion on integers as IV is
2003 favoured when IV and NV are equally accurate
2005 ####################################################################
2006 You had better be using SvIOK_notUV if you want an IV for arithmetic:
2007 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
2008 On the other hand, SvUOK is true iff UV.
2009 ####################################################################
2011 Your mileage will vary depending your CPU's relative fp to integer
2015 #ifndef NV_PRESERVES_UV
2016 # define IS_NUMBER_UNDERFLOW_IV 1
2017 # define IS_NUMBER_UNDERFLOW_UV 2
2018 # define IS_NUMBER_IV_AND_UV 2
2019 # define IS_NUMBER_OVERFLOW_IV 4
2020 # define IS_NUMBER_OVERFLOW_UV 5
2022 /* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
2024 /* For sv_2nv these three cases are "SvNOK and don't bother casting" */
2026 S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype)
2028 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%"UVxf" NV=%"NVgf" inttype=%"UVXf"\n", SvPVX_const(sv), SvIVX(sv), SvNVX(sv), (UV)numtype));
2029 if (SvNVX(sv) < (NV)IV_MIN) {
2030 (void)SvIOKp_on(sv);
2032 SvIV_set(sv, IV_MIN);
2033 return IS_NUMBER_UNDERFLOW_IV;
2035 if (SvNVX(sv) > (NV)UV_MAX) {
2036 (void)SvIOKp_on(sv);
2039 SvUV_set(sv, UV_MAX);
2040 return IS_NUMBER_OVERFLOW_UV;
2042 (void)SvIOKp_on(sv);
2044 /* Can't use strtol etc to convert this string. (See truth table in
2046 if (SvNVX(sv) <= (UV)IV_MAX) {
2047 SvIV_set(sv, I_V(SvNVX(sv)));
2048 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2049 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
2051 /* Integer is imprecise. NOK, IOKp */
2053 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
2056 SvUV_set(sv, U_V(SvNVX(sv)));
2057 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2058 if (SvUVX(sv) == UV_MAX) {
2059 /* As we know that NVs don't preserve UVs, UV_MAX cannot
2060 possibly be preserved by NV. Hence, it must be overflow.
2062 return IS_NUMBER_OVERFLOW_UV;
2064 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
2066 /* Integer is imprecise. NOK, IOKp */
2068 return IS_NUMBER_OVERFLOW_IV;
2070 #endif /* !NV_PRESERVES_UV*/
2072 /* sv_2iv() is now a macro using Perl_sv_2iv_flags();
2073 * this function provided for binary compatibility only
2077 Perl_sv_2iv(pTHX_ register SV *sv)
2079 return sv_2iv_flags(sv, SV_GMAGIC);
2083 =for apidoc sv_2iv_flags
2085 Return the integer value of an SV, doing any necessary string
2086 conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2087 Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
2093 Perl_sv_2iv_flags(pTHX_ register SV *sv, I32 flags)
2097 if (SvGMAGICAL(sv)) {
2098 if (flags & SV_GMAGIC)
2103 return I_V(SvNVX(sv));
2105 if (SvPOKp(sv) && SvLEN(sv))
2108 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2109 if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
2115 if (SvTHINKFIRST(sv)) {
2118 SV * const tmpstr=AMG_CALLun(sv,numer);
2119 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2120 return SvIV(tmpstr);
2123 return PTR2IV(SvRV(sv));
2126 sv_force_normal_flags(sv, 0);
2128 if (SvREADONLY(sv) && !SvOK(sv)) {
2129 if (ckWARN(WARN_UNINITIALIZED))
2136 return (IV)(SvUVX(sv));
2143 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2144 * without also getting a cached IV/UV from it at the same time
2145 * (ie PV->NV conversion should detect loss of accuracy and cache
2146 * IV or UV at same time to avoid this. NWC */
2148 if (SvTYPE(sv) == SVt_NV)
2149 sv_upgrade(sv, SVt_PVNV);
2151 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2152 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2153 certainly cast into the IV range at IV_MAX, whereas the correct
2154 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2156 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2157 SvIV_set(sv, I_V(SvNVX(sv)));
2158 if (SvNVX(sv) == (NV) SvIVX(sv)
2159 #ifndef NV_PRESERVES_UV
2160 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2161 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2162 /* Don't flag it as "accurately an integer" if the number
2163 came from a (by definition imprecise) NV operation, and
2164 we're outside the range of NV integer precision */
2167 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2168 DEBUG_c(PerlIO_printf(Perl_debug_log,
2169 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
2175 /* IV not precise. No need to convert from PV, as NV
2176 conversion would already have cached IV if it detected
2177 that PV->IV would be better than PV->NV->IV
2178 flags already correct - don't set public IOK. */
2179 DEBUG_c(PerlIO_printf(Perl_debug_log,
2180 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
2185 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2186 but the cast (NV)IV_MIN rounds to a the value less (more
2187 negative) than IV_MIN which happens to be equal to SvNVX ??
2188 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2189 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2190 (NV)UVX == NVX are both true, but the values differ. :-(
2191 Hopefully for 2s complement IV_MIN is something like
2192 0x8000000000000000 which will be exact. NWC */
2195 SvUV_set(sv, U_V(SvNVX(sv)));
2197 (SvNVX(sv) == (NV) SvUVX(sv))
2198 #ifndef NV_PRESERVES_UV
2199 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2200 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2201 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2202 /* Don't flag it as "accurately an integer" if the number
2203 came from a (by definition imprecise) NV operation, and
2204 we're outside the range of NV integer precision */
2210 DEBUG_c(PerlIO_printf(Perl_debug_log,
2211 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
2215 return (IV)SvUVX(sv);
2218 else if (SvPOKp(sv) && SvLEN(sv)) {
2220 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2221 /* We want to avoid a possible problem when we cache an IV which
2222 may be later translated to an NV, and the resulting NV is not
2223 the same as the direct translation of the initial string
2224 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2225 be careful to ensure that the value with the .456 is around if the
2226 NV value is requested in the future).
2228 This means that if we cache such an IV, we need to cache the
2229 NV as well. Moreover, we trade speed for space, and do not
2230 cache the NV if we are sure it's not needed.
2233 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2234 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2235 == IS_NUMBER_IN_UV) {
2236 /* It's definitely an integer, only upgrade to PVIV */
2237 if (SvTYPE(sv) < SVt_PVIV)
2238 sv_upgrade(sv, SVt_PVIV);
2240 } else if (SvTYPE(sv) < SVt_PVNV)
2241 sv_upgrade(sv, SVt_PVNV);
2243 /* If NV preserves UV then we only use the UV value if we know that
2244 we aren't going to call atof() below. If NVs don't preserve UVs
2245 then the value returned may have more precision than atof() will
2246 return, even though value isn't perfectly accurate. */
2247 if ((numtype & (IS_NUMBER_IN_UV
2248 #ifdef NV_PRESERVES_UV
2251 )) == IS_NUMBER_IN_UV) {
2252 /* This won't turn off the public IOK flag if it was set above */
2253 (void)SvIOKp_on(sv);
2255 if (!(numtype & IS_NUMBER_NEG)) {
2257 if (value <= (UV)IV_MAX) {
2258 SvIV_set(sv, (IV)value);
2260 SvUV_set(sv, value);
2264 /* 2s complement assumption */
2265 if (value <= (UV)IV_MIN) {
2266 SvIV_set(sv, -(IV)value);
2268 /* Too negative for an IV. This is a double upgrade, but
2269 I'm assuming it will be rare. */
2270 if (SvTYPE(sv) < SVt_PVNV)
2271 sv_upgrade(sv, SVt_PVNV);
2275 SvNV_set(sv, -(NV)value);
2276 SvIV_set(sv, IV_MIN);
2280 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2281 will be in the previous block to set the IV slot, and the next
2282 block to set the NV slot. So no else here. */
2284 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2285 != IS_NUMBER_IN_UV) {
2286 /* It wasn't an (integer that doesn't overflow the UV). */
2287 SvNV_set(sv, Atof(SvPVX_const(sv)));
2289 if (! numtype && ckWARN(WARN_NUMERIC))
2292 #if defined(USE_LONG_DOUBLE)
2293 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
2294 PTR2UV(sv), SvNVX(sv)));
2296 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
2297 PTR2UV(sv), SvNVX(sv)));
2301 #ifdef NV_PRESERVES_UV
2302 (void)SvIOKp_on(sv);
2304 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2305 SvIV_set(sv, I_V(SvNVX(sv)));
2306 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2309 /* Integer is imprecise. NOK, IOKp */
2311 /* UV will not work better than IV */
2313 if (SvNVX(sv) > (NV)UV_MAX) {
2315 /* Integer is inaccurate. NOK, IOKp, is UV */
2316 SvUV_set(sv, UV_MAX);
2319 SvUV_set(sv, U_V(SvNVX(sv)));
2320 /* 0xFFFFFFFFFFFFFFFF not an issue in here */
2321 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2325 /* Integer is imprecise. NOK, IOKp, is UV */
2331 #else /* NV_PRESERVES_UV */
2332 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2333 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2334 /* The IV slot will have been set from value returned by
2335 grok_number above. The NV slot has just been set using
2338 assert (SvIOKp(sv));
2340 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2341 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2342 /* Small enough to preserve all bits. */
2343 (void)SvIOKp_on(sv);
2345 SvIV_set(sv, I_V(SvNVX(sv)));
2346 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2348 /* Assumption: first non-preserved integer is < IV_MAX,
2349 this NV is in the preserved range, therefore: */
2350 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2352 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);
2356 0 0 already failed to read UV.
2357 0 1 already failed to read UV.
2358 1 0 you won't get here in this case. IV/UV
2359 slot set, public IOK, Atof() unneeded.
2360 1 1 already read UV.
2361 so there's no point in sv_2iuv_non_preserve() attempting
2362 to use atol, strtol, strtoul etc. */
2363 if (sv_2iuv_non_preserve (sv, numtype)
2364 >= IS_NUMBER_OVERFLOW_IV)
2368 #endif /* NV_PRESERVES_UV */
2371 if (!PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP) && ckWARN(WARN_UNINITIALIZED))
2373 if (SvTYPE(sv) < SVt_IV)
2374 /* Typically the caller expects that sv_any is not NULL now. */
2375 sv_upgrade(sv, SVt_IV);
2378 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2379 PTR2UV(sv),SvIVX(sv)));
2380 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
2383 /* sv_2uv() is now a macro using Perl_sv_2uv_flags();
2384 * this function provided for binary compatibility only
2388 Perl_sv_2uv(pTHX_ register SV *sv)
2390 return sv_2uv_flags(sv, SV_GMAGIC);
2394 =for apidoc sv_2uv_flags
2396 Return the unsigned integer value of an SV, doing any necessary string
2397 conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2398 Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
2404 Perl_sv_2uv_flags(pTHX_ register SV *sv, I32 flags)
2408 if (SvGMAGICAL(sv)) {
2409 if (flags & SV_GMAGIC)
2414 return U_V(SvNVX(sv));
2415 if (SvPOKp(sv) && SvLEN(sv))
2418 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2419 if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
2425 if (SvTHINKFIRST(sv)) {
2428 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
2429 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
2430 return SvUV(tmpstr);
2431 return PTR2UV(SvRV(sv));
2434 sv_force_normal_flags(sv, 0);
2436 if (SvREADONLY(sv) && !SvOK(sv)) {
2437 if (ckWARN(WARN_UNINITIALIZED))
2447 return (UV)SvIVX(sv);
2451 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2452 * without also getting a cached IV/UV from it at the same time
2453 * (ie PV->NV conversion should detect loss of accuracy and cache
2454 * IV or UV at same time to avoid this. */
2455 /* IV-over-UV optimisation - choose to cache IV if possible */
2457 if (SvTYPE(sv) == SVt_NV)
2458 sv_upgrade(sv, SVt_PVNV);
2460 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2461 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2462 SvIV_set(sv, I_V(SvNVX(sv)));
2463 if (SvNVX(sv) == (NV) SvIVX(sv)
2464 #ifndef NV_PRESERVES_UV
2465 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2466 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2467 /* Don't flag it as "accurately an integer" if the number
2468 came from a (by definition imprecise) NV operation, and
2469 we're outside the range of NV integer precision */
2472 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2473 DEBUG_c(PerlIO_printf(Perl_debug_log,
2474 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (precise)\n",
2480 /* IV not precise. No need to convert from PV, as NV
2481 conversion would already have cached IV if it detected
2482 that PV->IV would be better than PV->NV->IV
2483 flags already correct - don't set public IOK. */
2484 DEBUG_c(PerlIO_printf(Perl_debug_log,
2485 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (imprecise)\n",
2490 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2491 but the cast (NV)IV_MIN rounds to a the value less (more
2492 negative) than IV_MIN which happens to be equal to SvNVX ??
2493 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2494 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2495 (NV)UVX == NVX are both true, but the values differ. :-(
2496 Hopefully for 2s complement IV_MIN is something like
2497 0x8000000000000000 which will be exact. NWC */
2500 SvUV_set(sv, U_V(SvNVX(sv)));
2502 (SvNVX(sv) == (NV) SvUVX(sv))
2503 #ifndef NV_PRESERVES_UV
2504 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2505 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2506 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2507 /* Don't flag it as "accurately an integer" if the number
2508 came from a (by definition imprecise) NV operation, and
2509 we're outside the range of NV integer precision */
2514 DEBUG_c(PerlIO_printf(Perl_debug_log,
2515 "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n",
2521 else if (SvPOKp(sv) && SvLEN(sv)) {
2523 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2525 /* We want to avoid a possible problem when we cache a UV which
2526 may be later translated to an NV, and the resulting NV is not
2527 the translation of the initial data.
2529 This means that if we cache such a UV, we need to cache the
2530 NV as well. Moreover, we trade speed for space, and do not
2531 cache the NV if not needed.
2534 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2535 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2536 == IS_NUMBER_IN_UV) {
2537 /* It's definitely an integer, only upgrade to PVIV */
2538 if (SvTYPE(sv) < SVt_PVIV)
2539 sv_upgrade(sv, SVt_PVIV);
2541 } else if (SvTYPE(sv) < SVt_PVNV)
2542 sv_upgrade(sv, SVt_PVNV);
2544 /* If NV preserves UV then we only use the UV value if we know that
2545 we aren't going to call atof() below. If NVs don't preserve UVs
2546 then the value returned may have more precision than atof() will
2547 return, even though it isn't accurate. */
2548 if ((numtype & (IS_NUMBER_IN_UV
2549 #ifdef NV_PRESERVES_UV
2552 )) == IS_NUMBER_IN_UV) {
2553 /* This won't turn off the public IOK flag if it was set above */
2554 (void)SvIOKp_on(sv);
2556 if (!(numtype & IS_NUMBER_NEG)) {
2558 if (value <= (UV)IV_MAX) {
2559 SvIV_set(sv, (IV)value);
2561 /* it didn't overflow, and it was positive. */
2562 SvUV_set(sv, value);
2566 /* 2s complement assumption */
2567 if (value <= (UV)IV_MIN) {
2568 SvIV_set(sv, -(IV)value);
2570 /* Too negative for an IV. This is a double upgrade, but
2571 I'm assuming it will be rare. */
2572 if (SvTYPE(sv) < SVt_PVNV)
2573 sv_upgrade(sv, SVt_PVNV);
2577 SvNV_set(sv, -(NV)value);
2578 SvIV_set(sv, IV_MIN);
2583 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2584 != IS_NUMBER_IN_UV) {
2585 /* It wasn't an integer, or it overflowed the UV. */
2586 SvNV_set(sv, Atof(SvPVX_const(sv)));
2588 if (! numtype && ckWARN(WARN_NUMERIC))
2591 #if defined(USE_LONG_DOUBLE)
2592 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n",
2593 PTR2UV(sv), SvNVX(sv)));
2595 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"NVgf")\n",
2596 PTR2UV(sv), SvNVX(sv)));
2599 #ifdef NV_PRESERVES_UV
2600 (void)SvIOKp_on(sv);
2602 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2603 SvIV_set(sv, I_V(SvNVX(sv)));
2604 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2607 /* Integer is imprecise. NOK, IOKp */
2609 /* UV will not work better than IV */
2611 if (SvNVX(sv) > (NV)UV_MAX) {
2613 /* Integer is inaccurate. NOK, IOKp, is UV */
2614 SvUV_set(sv, UV_MAX);
2617 SvUV_set(sv, U_V(SvNVX(sv)));
2618 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
2619 NV preservse UV so can do correct comparison. */
2620 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2624 /* Integer is imprecise. NOK, IOKp, is UV */
2629 #else /* NV_PRESERVES_UV */
2630 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2631 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2632 /* The UV slot will have been set from value returned by
2633 grok_number above. The NV slot has just been set using
2636 assert (SvIOKp(sv));
2638 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2639 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2640 /* Small enough to preserve all bits. */
2641 (void)SvIOKp_on(sv);
2643 SvIV_set(sv, I_V(SvNVX(sv)));
2644 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2646 /* Assumption: first non-preserved integer is < IV_MAX,
2647 this NV is in the preserved range, therefore: */
2648 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2650 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);
2653 sv_2iuv_non_preserve (sv, numtype);
2655 #endif /* NV_PRESERVES_UV */
2659 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2660 if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
2663 if (SvTYPE(sv) < SVt_IV)
2664 /* Typically the caller expects that sv_any is not NULL now. */
2665 sv_upgrade(sv, SVt_IV);
2669 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
2670 PTR2UV(sv),SvUVX(sv)));
2671 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
2677 Return the num value of an SV, doing any necessary string or integer
2678 conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
2685 Perl_sv_2nv(pTHX_ register SV *sv)
2689 if (SvGMAGICAL(sv)) {
2693 if (SvPOKp(sv) && SvLEN(sv)) {
2694 if (!SvIOKp(sv) && ckWARN(WARN_NUMERIC) &&
2695 !grok_number(SvPVX_const(sv), SvCUR(sv), NULL))
2697 return Atof(SvPVX_const(sv));
2701 return (NV)SvUVX(sv);
2703 return (NV)SvIVX(sv);
2706 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2707 if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
2713 if (SvTHINKFIRST(sv)) {
2716 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
2717 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
2718 return SvNV(tmpstr);
2719 return PTR2NV(SvRV(sv));
2722 sv_force_normal_flags(sv, 0);
2724 if (SvREADONLY(sv) && !SvOK(sv)) {
2725 if (ckWARN(WARN_UNINITIALIZED))
2730 if (SvTYPE(sv) < SVt_NV) {
2731 if (SvTYPE(sv) == SVt_IV)
2732 sv_upgrade(sv, SVt_PVNV);
2734 sv_upgrade(sv, SVt_NV);
2735 #ifdef USE_LONG_DOUBLE
2737 STORE_NUMERIC_LOCAL_SET_STANDARD();
2738 PerlIO_printf(Perl_debug_log,
2739 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
2740 PTR2UV(sv), SvNVX(sv));
2741 RESTORE_NUMERIC_LOCAL();
2745 STORE_NUMERIC_LOCAL_SET_STANDARD();
2746 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
2747 PTR2UV(sv), SvNVX(sv));
2748 RESTORE_NUMERIC_LOCAL();
2752 else if (SvTYPE(sv) < SVt_PVNV)
2753 sv_upgrade(sv, SVt_PVNV);
2758 SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
2759 #ifdef NV_PRESERVES_UV
2762 /* Only set the public NV OK flag if this NV preserves the IV */
2763 /* Check it's not 0xFFFFFFFFFFFFFFFF */
2764 if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
2765 : (SvIVX(sv) == I_V(SvNVX(sv))))
2771 else if (SvPOKp(sv) && SvLEN(sv)) {
2773 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2774 if (!SvIOKp(sv) && !numtype && ckWARN(WARN_NUMERIC))
2776 #ifdef NV_PRESERVES_UV
2777 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2778 == IS_NUMBER_IN_UV) {
2779 /* It's definitely an integer */
2780 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
2782 SvNV_set(sv, Atof(SvPVX_const(sv)));
2785 SvNV_set(sv, Atof(SvPVX_const(sv)));
2786 /* Only set the public NV OK flag if this NV preserves the value in
2787 the PV at least as well as an IV/UV would.
2788 Not sure how to do this 100% reliably. */
2789 /* if that shift count is out of range then Configure's test is
2790 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2792 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2793 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2794 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
2795 } else if (!(numtype & IS_NUMBER_IN_UV)) {
2796 /* Can't use strtol etc to convert this string, so don't try.
2797 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
2800 /* value has been set. It may not be precise. */
2801 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
2802 /* 2s complement assumption for (UV)IV_MIN */
2803 SvNOK_on(sv); /* Integer is too negative. */
2808 if (numtype & IS_NUMBER_NEG) {
2809 SvIV_set(sv, -(IV)value);
2810 } else if (value <= (UV)IV_MAX) {
2811 SvIV_set(sv, (IV)value);
2813 SvUV_set(sv, value);
2817 if (numtype & IS_NUMBER_NOT_INT) {
2818 /* I believe that even if the original PV had decimals,
2819 they are lost beyond the limit of the FP precision.
2820 However, neither is canonical, so both only get p
2821 flags. NWC, 2000/11/25 */
2822 /* Both already have p flags, so do nothing */
2824 const NV nv = SvNVX(sv);
2825 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2826 if (SvIVX(sv) == I_V(nv)) {
2831 /* It had no "." so it must be integer. */
2834 /* between IV_MAX and NV(UV_MAX).
2835 Could be slightly > UV_MAX */
2837 if (numtype & IS_NUMBER_NOT_INT) {
2838 /* UV and NV both imprecise. */
2840 const UV nv_as_uv = U_V(nv);
2842 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
2853 #endif /* NV_PRESERVES_UV */
2856 if (!PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP) && ckWARN(WARN_UNINITIALIZED))
2858 if (SvTYPE(sv) < SVt_NV)
2859 /* Typically the caller expects that sv_any is not NULL now. */
2860 /* XXX Ilya implies that this is a bug in callers that assume this
2861 and ideally should be fixed. */
2862 sv_upgrade(sv, SVt_NV);
2865 #if defined(USE_LONG_DOUBLE)
2867 STORE_NUMERIC_LOCAL_SET_STANDARD();
2868 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
2869 PTR2UV(sv), SvNVX(sv));
2870 RESTORE_NUMERIC_LOCAL();
2874 STORE_NUMERIC_LOCAL_SET_STANDARD();
2875 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
2876 PTR2UV(sv), SvNVX(sv));
2877 RESTORE_NUMERIC_LOCAL();
2883 /* asIV(): extract an integer from the string value of an SV.
2884 * Caller must validate PVX */
2887 S_asIV(pTHX_ SV *sv)
2890 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2892 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2893 == IS_NUMBER_IN_UV) {
2894 /* It's definitely an integer */
2895 if (numtype & IS_NUMBER_NEG) {
2896 if (value < (UV)IV_MIN)
2899 if (value < (UV)IV_MAX)
2904 if (ckWARN(WARN_NUMERIC))
2907 return I_V(Atof(SvPVX_const(sv)));
2910 /* asUV(): extract an unsigned integer from the string value of an SV
2911 * Caller must validate PVX */
2914 S_asUV(pTHX_ SV *sv)
2917 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2919 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2920 == IS_NUMBER_IN_UV) {
2921 /* It's definitely an integer */
2922 if (!(numtype & IS_NUMBER_NEG))
2926 if (ckWARN(WARN_NUMERIC))
2929 return U_V(Atof(SvPVX_const(sv)));
2933 =for apidoc sv_2pv_nolen
2935 Like C<sv_2pv()>, but doesn't return the length too. You should usually
2936 use the macro wrapper C<SvPV_nolen(sv)> instead.
2941 Perl_sv_2pv_nolen(pTHX_ register SV *sv)
2943 return sv_2pv(sv, 0);
2946 /* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
2947 * UV as a string towards the end of buf, and return pointers to start and
2950 * We assume that buf is at least TYPE_CHARS(UV) long.
2954 S_uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
2956 char *ptr = buf + TYPE_CHARS(UV);
2957 char * const ebuf = ptr;
2970 *--ptr = '0' + (char)(uv % 10);
2978 /* sv_2pv() is now a macro using Perl_sv_2pv_flags();
2979 * this function provided for binary compatibility only
2983 Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
2985 return sv_2pv_flags(sv, lp, SV_GMAGIC);
2989 =for apidoc sv_2pv_flags
2991 Returns a pointer to the string value of an SV, and sets *lp to its length.
2992 If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
2994 Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
2995 usually end up here too.
3001 Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
3006 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
3007 char *tmpbuf = tbuf;
3014 if (SvGMAGICAL(sv)) {
3015 if (flags & SV_GMAGIC)
3020 if (flags & SV_MUTABLE_RETURN)
3021 return SvPVX_mutable(sv);
3022 if (flags & SV_CONST_RETURN)
3023 return (char *)SvPVX_const(sv);
3028 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
3030 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
3035 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
3040 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
3041 if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
3049 if (SvTHINKFIRST(sv)) {
3052 register const char *typestr;
3053 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
3054 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
3056 /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr); */
3059 if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) {
3060 if (flags & SV_CONST_RETURN) {
3061 pv = (char *) SvPVX_const(tmpstr);
3063 pv = (flags & SV_MUTABLE_RETURN)
3064 ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr);
3067 *lp = SvCUR(tmpstr);
3069 pv = sv_2pv_flags(tmpstr, lp, flags);
3080 typestr = "NULLREF";
3084 switch (SvTYPE(sv)) {
3086 if ( ((SvFLAGS(sv) &
3087 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
3088 == (SVs_OBJECT|SVs_SMG))
3089 && (mg = mg_find(sv, PERL_MAGIC_qr))) {
3090 const regexp *re = (regexp *)mg->mg_obj;
3093 const char *fptr = "msix";
3098 char need_newline = 0;
3099 U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12);
3101 while((ch = *fptr++)) {
3103 reflags[left++] = ch;
3106 reflags[right--] = ch;
3111 reflags[left] = '-';
3115 mg->mg_len = re->prelen + 4 + left;
3117 * If /x was used, we have to worry about a regex
3118 * ending with a comment later being embedded
3119 * within another regex. If so, we don't want this
3120 * regex's "commentization" to leak out to the
3121 * right part of the enclosing regex, we must cap
3122 * it with a newline.
3124 * So, if /x was used, we scan backwards from the
3125 * end of the regex. If we find a '#' before we
3126 * find a newline, we need to add a newline
3127 * ourself. If we find a '\n' first (or if we
3128 * don't find '#' or '\n'), we don't need to add
3129 * anything. -jfriedl
3131 if (PMf_EXTENDED & re->reganch)
3133 const char *endptr = re->precomp + re->prelen;
3134 while (endptr >= re->precomp)
3136 const char c = *(endptr--);
3138 break; /* don't need another */
3140 /* we end while in a comment, so we
3142 mg->mg_len++; /* save space for it */
3143 need_newline = 1; /* note to add it */
3149 Newx(mg->mg_ptr, mg->mg_len + 1 + left, char);
3150 Copy("(?", mg->mg_ptr, 2, char);
3151 Copy(reflags, mg->mg_ptr+2, left, char);
3152 Copy(":", mg->mg_ptr+left+2, 1, char);
3153 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
3155 mg->mg_ptr[mg->mg_len - 2] = '\n';
3156 mg->mg_ptr[mg->mg_len - 1] = ')';
3157 mg->mg_ptr[mg->mg_len] = 0;
3159 PL_reginterp_cnt += re->program[0].next_off;
3161 if (re->reganch & ROPT_UTF8)
3177 case SVt_PVBM: typestr = SvROK(sv) ? "REF" : "SCALAR"; break;
3178 case SVt_PVLV: typestr = SvROK(sv) ? "REF"
3179 /* tied lvalues should appear to be
3180 * scalars for backwards compatitbility */
3181 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
3182 ? "SCALAR" : "LVALUE"; break;
3183 case SVt_PVAV: typestr = "ARRAY"; break;
3184 case SVt_PVHV: typestr = "HASH"; break;
3185 case SVt_PVCV: typestr = "CODE"; break;
3186 case SVt_PVGV: typestr = "GLOB"; break;
3187 case SVt_PVFM: typestr = "FORMAT"; break;
3188 case SVt_PVIO: typestr = "IO"; break;
3189 default: typestr = "UNKNOWN"; break;
3193 const char *name = HvNAME_get(SvSTASH(sv));
3194 Perl_sv_setpvf(aTHX_ tsv, "%s=%s(0x%"UVxf")",
3195 name ? name : "__ANON__" , typestr, PTR2UV(sv));
3198 Perl_sv_setpvf(aTHX_ tsv, "%s(0x%"UVxf")", typestr, PTR2UV(sv));
3202 *lp = strlen(typestr);
3203 return (char *)typestr;
3205 if (SvREADONLY(sv) && !SvOK(sv)) {
3206 if (ckWARN(WARN_UNINITIALIZED))
3213 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
3214 /* I'm assuming that if both IV and NV are equally valid then
3215 converting the IV is going to be more efficient */
3216 const U32 isIOK = SvIOK(sv);
3217 const U32 isUIOK = SvIsUV(sv);
3218 char buf[TYPE_CHARS(UV)];
3221 if (SvTYPE(sv) < SVt_PVIV)
3222 sv_upgrade(sv, SVt_PVIV);
3224 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
3226 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
3227 /* inlined from sv_setpvn */
3228 SvGROW_mutable(sv, (STRLEN)(ebuf - ptr + 1));
3229 Move(ptr,SvPVX_mutable(sv),ebuf - ptr,char);
3230 SvCUR_set(sv, ebuf - ptr);
3240 else if (SvNOKp(sv)) {
3241 if (SvTYPE(sv) < SVt_PVNV)
3242 sv_upgrade(sv, SVt_PVNV);
3243 /* The +20 is pure guesswork. Configure test needed. --jhi */
3244 s = SvGROW_mutable(sv, NV_DIG + 20);
3245 olderrno = errno; /* some Xenix systems wipe out errno here */
3247 if (SvNVX(sv) == 0.0)
3248 (void)strcpy(s,"0");
3252 Gconvert(SvNVX(sv), NV_DIG, 0, s);
3255 #ifdef FIXNEGATIVEZERO
3256 if (*s == '-' && s[1] == '0' && !s[2])
3266 if (!PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP) && ckWARN(WARN_UNINITIALIZED))
3270 if (SvTYPE(sv) < SVt_PV)
3271 /* Typically the caller expects that sv_any is not NULL now. */
3272 sv_upgrade(sv, SVt_PV);
3276 const STRLEN len = s - SvPVX_const(sv);
3282 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3283 PTR2UV(sv),SvPVX_const(sv)));
3284 if (flags & SV_CONST_RETURN)
3285 return (char *)SvPVX_const(sv);
3286 if (flags & SV_MUTABLE_RETURN)
3287 return SvPVX_mutable(sv);
3291 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
3292 /* Sneaky stuff here */
3296 tsv = newSVpv(tmpbuf, 0);
3309 t = SvPVX_const(tsv);
3314 len = strlen(tmpbuf);
3316 #ifdef FIXNEGATIVEZERO
3317 if (len == 2 && t[0] == '-' && t[1] == '0') {
3322 SvUPGRADE(sv, SVt_PV);
3325 s = SvGROW_mutable(sv, len + 1);
3328 return memcpy(s, t, len + 1);
3333 =for apidoc sv_copypv
3335 Copies a stringified representation of the source SV into the
3336 destination SV. Automatically performs any necessary mg_get and
3337 coercion of numeric values into strings. Guaranteed to preserve
3338 UTF-8 flag even from overloaded objects. Similar in nature to
3339 sv_2pv[_flags] but operates directly on an SV instead of just the
3340 string. Mostly uses sv_2pv_flags to do its work, except when that
3341 would lose the UTF-8'ness of the PV.
3347 Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv)
3350 const char * const s = SvPV_const(ssv,len);
3351 sv_setpvn(dsv,s,len);
3359 =for apidoc sv_2pvbyte_nolen
3361 Return a pointer to the byte-encoded representation of the SV.
3362 May cause the SV to be downgraded from UTF-8 as a side-effect.
3364 Usually accessed via the C<SvPVbyte_nolen> macro.
3370 Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
3372 return sv_2pvbyte(sv, 0);
3376 =for apidoc sv_2pvbyte
3378 Return a pointer to the byte-encoded representation of the SV, and set *lp
3379 to its length. May cause the SV to be downgraded from UTF-8 as a
3382 Usually accessed via the C<SvPVbyte> macro.
3388 Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
3390 sv_utf8_downgrade(sv,0);
3391 return lp ? SvPV(sv,*lp) : SvPV_nolen(sv);
3395 =for apidoc sv_2pvutf8_nolen
3397 Return a pointer to the UTF-8-encoded representation of the SV.
3398 May cause the SV to be upgraded to UTF-8 as a side-effect.
3400 Usually accessed via the C<SvPVutf8_nolen> macro.
3406 Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
3408 return sv_2pvutf8(sv, 0);
3412 =for apidoc sv_2pvutf8
3414 Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
3415 to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
3417 Usually accessed via the C<SvPVutf8> macro.
3423 Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
3425 sv_utf8_upgrade(sv);
3426 return lp ? SvPV(sv,*lp) : SvPV_nolen(sv);
3430 =for apidoc sv_2bool
3432 This function is only called on magical items, and is only used by
3433 sv_true() or its macro equivalent.
3439 Perl_sv_2bool(pTHX_ register SV *sv)
3447 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
3448 (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
3449 return (bool)SvTRUE(tmpsv);
3450 return SvRV(sv) != 0;
3453 register XPV* const Xpvtmp = (XPV*)SvANY(sv);
3455 (*sv->sv_u.svu_pv > '0' ||
3456 Xpvtmp->xpv_cur > 1 ||
3457 (Xpvtmp->xpv_cur && *sv->sv_u.svu_pv != '0')))
3464 return SvIVX(sv) != 0;
3467 return SvNVX(sv) != 0.0;
3474 /* sv_utf8_upgrade() is now a macro using sv_utf8_upgrade_flags();
3475 * this function provided for binary compatibility only
3480 Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
3482 return sv_utf8_upgrade_flags(sv, SV_GMAGIC);
3486 =for apidoc sv_utf8_upgrade
3488 Converts the PV of an SV to its UTF-8-encoded form.
3489 Forces the SV to string form if it is not already.
3490 Always sets the SvUTF8 flag to avoid future validity checks even
3491 if all the bytes have hibit clear.
3493 This is not as a general purpose byte encoding to Unicode interface:
3494 use the Encode extension for that.
3496 =for apidoc sv_utf8_upgrade_flags
3498 Converts the PV of an SV to its UTF-8-encoded form.
3499 Forces the SV to string form if it is not already.
3500 Always sets the SvUTF8 flag to avoid future validity checks even
3501 if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
3502 will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
3503 C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
3505 This is not as a general purpose byte encoding to Unicode interface:
3506 use the Encode extension for that.
3512 Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags)
3514 if (sv == &PL_sv_undef)
3518 if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
3519 (void) sv_2pv_flags(sv,&len, flags);
3523 (void) SvPV_force(sv,len);
3532 sv_force_normal_flags(sv, 0);
3535 if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING))
3536 sv_recode_to_utf8(sv, PL_encoding);
3537 else { /* Assume Latin-1/EBCDIC */
3538 /* This function could be much more efficient if we
3539 * had a FLAG in SVs to signal if there are any hibit
3540 * chars in the PV. Given that there isn't such a flag
3541 * make the loop as fast as possible. */
3542 const U8 *s = (U8 *) SvPVX_const(sv);
3543 const U8 *e = (U8 *) SvEND(sv);
3549 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
3553 STRLEN len = SvCUR(sv) + 1; /* Plus the \0 */
3554 U8 * const recoded = bytes_to_utf8((U8*)s, &len);
3556 SvPV_free(sv); /* No longer using what was there before. */
3558 SvPV_set(sv, (char*)recoded);
3559 SvCUR_set(sv, len - 1);
3560 SvLEN_set(sv, len); /* No longer know the real size. */
3562 /* Mark as UTF-8 even if no hibit - saves scanning loop */
3569 =for apidoc sv_utf8_downgrade
3571 Attempts to convert the PV of an SV from characters to bytes.
3572 If the PV contains a character beyond byte, this conversion will fail;
3573 in this case, either returns false or, if C<fail_ok> is not
3576 This is not as a general purpose Unicode to byte encoding interface:
3577 use the Encode extension for that.
3583 Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
3585 if (SvPOKp(sv) && SvUTF8(sv)) {
3591 sv_force_normal_flags(sv, 0);
3593 s = (U8 *) SvPV(sv, len);
3594 if (!utf8_to_bytes(s, &len)) {
3599 Perl_croak(aTHX_ "Wide character in %s",
3602 Perl_croak(aTHX_ "Wide character");
3613 =for apidoc sv_utf8_encode
3615 Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
3616 flag off so that it looks like octets again.
3622 Perl_sv_utf8_encode(pTHX_ register SV *sv)
3624 (void) sv_utf8_upgrade(sv);
3626 sv_force_normal_flags(sv, 0);
3628 if (SvREADONLY(sv)) {
3629 Perl_croak(aTHX_ PL_no_modify);
3635 =for apidoc sv_utf8_decode
3637 If the PV of the SV is an octet sequence in UTF-8
3638 and contains a multiple-byte character, the C<SvUTF8> flag is turned on
3639 so that it looks like a character. If the PV contains only single-byte
3640 characters, the C<SvUTF8> flag stays being off.
3641 Scans PV for validity and returns false if the PV is invalid UTF-8.
3647 Perl_sv_utf8_decode(pTHX_ register SV *sv)
3653 /* The octets may have got themselves encoded - get them back as
3656 if (!sv_utf8_downgrade(sv, TRUE))
3659 /* it is actually just a matter of turning the utf8 flag on, but
3660 * we want to make sure everything inside is valid utf8 first.
3662 c = (const U8 *) SvPVX_const(sv);
3663 if (!is_utf8_string(c, SvCUR(sv)+1))
3665 e = (const U8 *) SvEND(sv);
3668 if (!UTF8_IS_INVARIANT(ch)) {
3677 /* sv_setsv() is now a macro using Perl_sv_setsv_flags();
3678 * this function provided for binary compatibility only
3682 Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
3684 sv_setsv_flags(dstr, sstr, SV_GMAGIC);
3688 =for apidoc sv_setsv
3690 Copies the contents of the source SV C<ssv> into the destination SV
3691 C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3692 function if the source SV needs to be reused. Does not handle 'set' magic.
3693 Loosely speaking, it performs a copy-by-value, obliterating any previous
3694 content of the destination.
3696 You probably want to use one of the assortment of wrappers, such as
3697 C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3698 C<SvSetMagicSV_nosteal>.
3700 =for apidoc sv_setsv_flags
3702 Copies the contents of the source SV C<ssv> into the destination SV
3703 C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3704 function if the source SV needs to be reused. Does not handle 'set' magic.
3705 Loosely speaking, it performs a copy-by-value, obliterating any previous
3706 content of the destination.
3707 If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
3708 C<ssv> if appropriate, else not. If the C<flags> parameter has the
3709 C<NOSTEAL> bit set then the buffers of temps will not be stolen. <sv_setsv>
3710 and C<sv_setsv_nomg> are implemented in terms of this function.
3712 You probably want to use one of the assortment of wrappers, such as
3713 C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3714 C<SvSetMagicSV_nosteal>.
3716 This is the primary function for copying scalars, and most other
3717 copy-ish functions and macros use this underneath.
3723 Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags)
3725 register U32 sflags;
3731 SV_CHECK_THINKFIRST_COW_DROP(dstr);
3733 sstr = &PL_sv_undef;
3734 stype = SvTYPE(sstr);
3735 dtype = SvTYPE(dstr);
3740 /* need to nuke the magic */
3742 SvRMAGICAL_off(dstr);
3745 /* There's a lot of redundancy below but we're going for speed here */
3750 if (dtype != SVt_PVGV) {
3751 (void)SvOK_off(dstr);
3759 sv_upgrade(dstr, SVt_IV);
3762 sv_upgrade(dstr, SVt_PVNV);
3766 sv_upgrade(dstr, SVt_PVIV);
3769 (void)SvIOK_only(dstr);
3770 SvIV_set(dstr, SvIVX(sstr));
3773 if (SvTAINTED(sstr))
3784 sv_upgrade(dstr, SVt_NV);
3789 sv_upgrade(dstr, SVt_PVNV);
3792 SvNV_set(dstr, SvNVX(sstr));
3793 (void)SvNOK_only(dstr);
3794 if (SvTAINTED(sstr))
3802 sv_upgrade(dstr, SVt_RV);
3803 else if (dtype == SVt_PVGV &&
3804 SvROK(sstr) && SvTYPE(SvRV(sstr)) == SVt_PVGV) {
3807 if (GvIMPORTED(dstr) != GVf_IMPORTED
3808 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3810 GvIMPORTED_on(dstr);
3819 #ifdef PERL_OLD_COPY_ON_WRITE
3820 if ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS) {
3821 if (dtype < SVt_PVIV)
3822 sv_upgrade(dstr, SVt_PVIV);
3829 sv_upgrade(dstr, SVt_PV);
3832 if (dtype < SVt_PVIV)
3833 sv_upgrade(dstr, SVt_PVIV);
3836 if (dtype < SVt_PVNV)
3837 sv_upgrade(dstr, SVt_PVNV);
3844 const char * const type = sv_reftype(sstr,0);
3846 Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_NAME(PL_op));
3848 Perl_croak(aTHX_ "Bizarre copy of %s", type);
3853 if (dtype <= SVt_PVGV) {
3855 if (dtype != SVt_PVGV) {
3856 const char * const name = GvNAME(sstr);
3857 const STRLEN len = GvNAMELEN(sstr);
3858 /* don't upgrade SVt_PVLV: it can hold a glob */
3859 if (dtype != SVt_PVLV)
3860 sv_upgrade(dstr, SVt_PVGV);
3861 sv_magic(dstr, dstr, PERL_MAGIC_glob, Nullch, 0);
3862 GvSTASH(dstr) = GvSTASH(sstr);
3864 Perl_sv_add_backref(aTHX_ (SV*)GvSTASH(dstr), dstr);
3865 GvNAME(dstr) = savepvn(name, len);
3866 GvNAMELEN(dstr) = len;
3867 SvFAKE_on(dstr); /* can coerce to non-glob */
3869 /* ahem, death to those who redefine active sort subs */
3870 else if (PL_curstackinfo->si_type == PERLSI_SORT
3871 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
3872 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
3875 #ifdef GV_UNIQUE_CHECK
3876 if (GvUNIQUE((GV*)dstr)) {
3877 Perl_croak(aTHX_ PL_no_modify);
3881 (void)SvOK_off(dstr);
3882 GvINTRO_off(dstr); /* one-shot flag */
3884 GvGP(dstr) = gp_ref(GvGP(sstr));
3885 if (SvTAINTED(sstr))
3887 if (GvIMPORTED(dstr) != GVf_IMPORTED
3888 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3890 GvIMPORTED_on(dstr);
3898 if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
3900 if ((int)SvTYPE(sstr) != stype) {
3901 stype = SvTYPE(sstr);
3902 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
3906 if (stype == SVt_PVLV)
3907 SvUPGRADE(dstr, SVt_PVNV);
3909 SvUPGRADE(dstr, (U32)stype);
3912 sflags = SvFLAGS(sstr);
3914 if (sflags & SVf_ROK) {
3915 if (dtype >= SVt_PV) {
3916 if (dtype == SVt_PVGV) {
3917 SV * const sref = SvREFCNT_inc(SvRV(sstr));
3919 const int intro = GvINTRO(dstr);
3921 #ifdef GV_UNIQUE_CHECK
3922 if (GvUNIQUE((GV*)dstr)) {
3923 Perl_croak(aTHX_ PL_no_modify);
3928 GvINTRO_off(dstr); /* one-shot flag */
3929 GvLINE(dstr) = CopLINE(PL_curcop);
3930 GvEGV(dstr) = (GV*)dstr;
3933 switch (SvTYPE(sref)) {
3936 SAVEGENERICSV(GvAV(dstr));
3938 dref = (SV*)GvAV(dstr);
3939 GvAV(dstr) = (AV*)sref;
3940 if (!GvIMPORTED_AV(dstr)
3941 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3943 GvIMPORTED_AV_on(dstr);
3948 SAVEGENERICSV(GvHV(dstr));
3950 dref = (SV*)GvHV(dstr);
3951 GvHV(dstr) = (HV*)sref;
3952 if (!GvIMPORTED_HV(dstr)
3953 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3955 GvIMPORTED_HV_on(dstr);
3960 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
3961 SvREFCNT_dec(GvCV(dstr));
3962 GvCV(dstr) = Nullcv;
3963 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3964 PL_sub_generation++;
3966 SAVEGENERICSV(GvCV(dstr));
3969 dref = (SV*)GvCV(dstr);
3970 if (GvCV(dstr) != (CV*)sref) {
3971 CV* const cv = GvCV(dstr);
3973 if (!GvCVGEN((GV*)dstr) &&
3974 (CvROOT(cv) || CvXSUB(cv)))
3976 /* ahem, death to those who redefine
3977 * active sort subs */
3978 if (PL_curstackinfo->si_type == PERLSI_SORT &&
3979 PL_sortcop == CvSTART(cv))
3981 "Can't redefine active sort subroutine %s",
3982 GvENAME((GV*)dstr));
3983 /* Redefining a sub - warning is mandatory if
3984 it was a const and its value changed. */
3985 if (ckWARN(WARN_REDEFINE)
3987 && (!CvCONST((CV*)sref)
3988 || sv_cmp(cv_const_sv(cv),
3989 cv_const_sv((CV*)sref)))))
3991 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
3993 ? "Constant subroutine %s::%s redefined"
3994 : "Subroutine %s::%s redefined",
3995 HvNAME_get(GvSTASH((GV*)dstr)),
3996 GvENAME((GV*)dstr));
4000 cv_ckproto(cv, (GV*)dstr,
4002 ? SvPVX_const(sref) : Nullch);
4004 GvCV(dstr) = (CV*)sref;
4005 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
4006 GvASSUMECV_on(dstr);
4007 PL_sub_generation++;
4009 if (!GvIMPORTED_CV(dstr)
4010 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4012 GvIMPORTED_CV_on(dstr);
4017 SAVEGENERICSV(GvIOp(dstr));
4019 dref = (SV*)GvIOp(dstr);
4020 GvIOp(dstr) = (IO*)sref;
4024 SAVEGENERICSV(GvFORM(dstr));
4026 dref = (SV*)GvFORM(dstr);
4027 GvFORM(dstr) = (CV*)sref;
4031 SAVEGENERICSV(GvSV(dstr));
4033 dref = (SV*)GvSV(dstr);
4035 if (!GvIMPORTED_SV(dstr)
4036 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4038 GvIMPORTED_SV_on(dstr);
4044 if (SvTAINTED(sstr))
4048 if (SvPVX_const(dstr)) {
4054 (void)SvOK_off(dstr);
4055 SvRV_set(dstr, SvREFCNT_inc(SvRV(sstr)));
4057 if (sflags & SVp_NOK) {
4059 /* Only set the public OK flag if the source has public OK. */
4060 if (sflags & SVf_NOK)
4061 SvFLAGS(dstr) |= SVf_NOK;
4062 SvNV_set(dstr, SvNVX(sstr));
4064 if (sflags & SVp_IOK) {
4065 (void)SvIOKp_on(dstr);
4066 if (sflags & SVf_IOK)
4067 SvFLAGS(dstr) |= SVf_IOK;
4068 if (sflags & SVf_IVisUV)
4070 SvIV_set(dstr, SvIVX(sstr));
4072 if (SvAMAGIC(sstr)) {
4076 else if (sflags & SVp_POK) {
4080 * Check to see if we can just swipe the string. If so, it's a
4081 * possible small lose on short strings, but a big win on long ones.
4082 * It might even be a win on short strings if SvPVX_const(dstr)
4083 * has to be allocated and SvPVX_const(sstr) has to be freed.
4086 /* Whichever path we take through the next code, we want this true,
4087 and doing it now facilitates the COW check. */
4088 (void)SvPOK_only(dstr);
4091 /* We're not already COW */
4092 ((sflags & (SVf_FAKE | SVf_READONLY)) != (SVf_FAKE | SVf_READONLY)
4093 #ifndef PERL_OLD_COPY_ON_WRITE
4094 /* or we are, but dstr isn't a suitable target. */
4095 || (SvFLAGS(dstr) & CAN_COW_MASK) != CAN_COW_FLAGS
4100 (sflags & SVs_TEMP) && /* slated for free anyway? */
4101 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
4102 (!(flags & SV_NOSTEAL)) &&
4103 /* and we're allowed to steal temps */
4104 SvREFCNT(sstr) == 1 && /* and no other references to it? */
4105 SvLEN(sstr) && /* and really is a string */
4106 /* and won't be needed again, potentially */
4107 !(PL_op && PL_op->op_type == OP_AASSIGN))
4108 #ifdef PERL_OLD_COPY_ON_WRITE
4109 && !((sflags & CAN_COW_MASK) == CAN_COW_FLAGS
4110 && (SvFLAGS(dstr) & CAN_COW_MASK) == CAN_COW_FLAGS
4111 && SvTYPE(sstr) >= SVt_PVIV)
4114 /* Failed the swipe test, and it's not a shared hash key either.
4115 Have to copy the string. */
4116 STRLEN len = SvCUR(sstr);
4117 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
4118 Move(SvPVX_const(sstr),SvPVX(dstr),len,char);
4119 SvCUR_set(dstr, len);
4120 *SvEND(dstr) = '\0';
4122 /* If PERL_OLD_COPY_ON_WRITE is not defined, then isSwipe will always
4124 /* Either it's a shared hash key, or it's suitable for
4125 copy-on-write or we can swipe the string. */
4127 PerlIO_printf(Perl_debug_log, "Copy on write: sstr --> dstr\n");
4131 #ifdef PERL_OLD_COPY_ON_WRITE
4133 /* I believe I should acquire a global SV mutex if
4134 it's a COW sv (not a shared hash key) to stop
4135 it going un copy-on-write.
4136 If the source SV has gone un copy on write between up there
4137 and down here, then (assert() that) it is of the correct
4138 form to make it copy on write again */
4139 if ((sflags & (SVf_FAKE | SVf_READONLY))
4140 != (SVf_FAKE | SVf_READONLY)) {
4141 SvREADONLY_on(sstr);
4143 /* Make the source SV into a loop of 1.
4144 (about to become 2) */
4145 SV_COW_NEXT_SV_SET(sstr, sstr);
4149 /* Initial code is common. */
4150 if (SvPVX_const(dstr)) { /* we know that dtype >= SVt_PV */
4155 /* making another shared SV. */
4156 STRLEN cur = SvCUR(sstr);
4157 STRLEN len = SvLEN(sstr);
4158 #ifdef PERL_OLD_COPY_ON_WRITE
4160 assert (SvTYPE(dstr) >= SVt_PVIV);
4161 /* SvIsCOW_normal */
4162 /* splice us in between source and next-after-source. */
4163 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4164 SV_COW_NEXT_SV_SET(sstr, dstr);
4165 SvPV_set(dstr, SvPVX_mutable(sstr));
4169 /* SvIsCOW_shared_hash */
4170 DEBUG_C(PerlIO_printf(Perl_debug_log,
4171 "Copy on write: Sharing hash\n"));
4173 assert (SvTYPE(dstr) >= SVt_PV);
4175 HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr)))));
4177 SvLEN_set(dstr, len);
4178 SvCUR_set(dstr, cur);
4179 SvREADONLY_on(dstr);
4181 /* Relesase a global SV mutex. */
4184 { /* Passes the swipe test. */
4185 SvPV_set(dstr, SvPVX_mutable(sstr));
4186 SvLEN_set(dstr, SvLEN(sstr));
4187 SvCUR_set(dstr, SvCUR(sstr));
4190 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
4191 SvPV_set(sstr, Nullch);
4197 if (sflags & SVf_UTF8)
4199 if (sflags & SVp_NOK) {
4201 if (sflags & SVf_NOK)
4202 SvFLAGS(dstr) |= SVf_NOK;
4203 SvNV_set(dstr, SvNVX(sstr));
4205 if (sflags & SVp_IOK) {
4206 (void)SvIOKp_on(dstr);
4207 if (sflags & SVf_IOK)
4208 SvFLAGS(dstr) |= SVf_IOK;
4209 if (sflags & SVf_IVisUV)
4211 SvIV_set(dstr, SvIVX(sstr));
4214 MAGIC *smg = mg_find(sstr,PERL_MAGIC_vstring);
4215 sv_magic(dstr, NULL, PERL_MAGIC_vstring,
4216 smg->mg_ptr, smg->mg_len);
4217 SvRMAGICAL_on(dstr);
4220 else if (sflags & SVp_IOK) {
4221 if (sflags & SVf_IOK)
4222 (void)SvIOK_only(dstr);
4224 (void)SvOK_off(dstr);
4225 (void)SvIOKp_on(dstr);
4227 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
4228 if (sflags & SVf_IVisUV)
4230 SvIV_set(dstr, SvIVX(sstr));
4231 if (sflags & SVp_NOK) {
4232 if (sflags & SVf_NOK)
4233 (void)SvNOK_on(dstr);
4235 (void)SvNOKp_on(dstr);
4236 SvNV_set(dstr, SvNVX(sstr));
4239 else if (sflags & SVp_NOK) {
4240 if (sflags & SVf_NOK)
4241 (void)SvNOK_only(dstr);
4243 (void)SvOK_off(dstr);
4246 SvNV_set(dstr, SvNVX(sstr));
4249 if (dtype == SVt_PVGV) {
4250 if (ckWARN(WARN_MISC))
4251 Perl_warner(aTHX_ packWARN(WARN_MISC), "Undefined value assigned to typeglob");
4254 (void)SvOK_off(dstr);
4256 if (SvTAINTED(sstr))
4261 =for apidoc sv_setsv_mg
4263 Like C<sv_setsv>, but also handles 'set' magic.
4269 Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
4271 sv_setsv(dstr,sstr);
4275 #ifdef PERL_OLD_COPY_ON_WRITE
4277 Perl_sv_setsv_cow(pTHX_ SV *dstr, SV *sstr)
4279 STRLEN cur = SvCUR(sstr);
4280 STRLEN len = SvLEN(sstr);
4281 register char *new_pv;
4284 PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n",
4292 if (SvTHINKFIRST(dstr))
4293 sv_force_normal_flags(dstr, SV_COW_DROP_PV);
4294 else if (SvPVX_const(dstr))
4295 Safefree(SvPVX_const(dstr));
4299 SvUPGRADE(dstr, SVt_PVIV);
4301 assert (SvPOK(sstr));
4302 assert (SvPOKp(sstr));
4303 assert (!SvIOK(sstr));
4304 assert (!SvIOKp(sstr));
4305 assert (!SvNOK(sstr));
4306 assert (!SvNOKp(sstr));
4308 if (SvIsCOW(sstr)) {
4310 if (SvLEN(sstr) == 0) {
4311 /* source is a COW shared hash key. */
4312 DEBUG_C(PerlIO_printf(Perl_debug_log,
4313 "Fast copy on write: Sharing hash\n"));
4314 new_pv = HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr))));
4317 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4319 assert ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS);
4320 SvUPGRADE(sstr, SVt_PVIV);
4321 SvREADONLY_on(sstr);
4323 DEBUG_C(PerlIO_printf(Perl_debug_log,
4324 "Fast copy on write: Converting sstr to COW\n"));
4325 SV_COW_NEXT_SV_SET(dstr, sstr);
4327 SV_COW_NEXT_SV_SET(sstr, dstr);
4328 new_pv = SvPVX_mutable(sstr);
4331 SvPV_set(dstr, new_pv);
4332 SvFLAGS(dstr) = (SVt_PVIV|SVf_POK|SVp_POK|SVf_FAKE|SVf_READONLY);
4335 SvLEN_set(dstr, len);
4336 SvCUR_set(dstr, cur);
4345 =for apidoc sv_setpvn
4347 Copies a string into an SV. The C<len> parameter indicates the number of
4348 bytes to be copied. If the C<ptr> argument is NULL the SV will become
4349 undefined. Does not handle 'set' magic. See C<sv_setpvn_mg>.
4355 Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
4357 register char *dptr;
4359 SV_CHECK_THINKFIRST_COW_DROP(sv);
4365 /* len is STRLEN which is unsigned, need to copy to signed */
4368 Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen");
4370 SvUPGRADE(sv, SVt_PV);
4372 dptr = SvGROW(sv, len + 1);
4373 Move(ptr,dptr,len,char);
4376 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4381 =for apidoc sv_setpvn_mg
4383 Like C<sv_setpvn>, but also handles 'set' magic.
4389 Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
4391 sv_setpvn(sv,ptr,len);
4396 =for apidoc sv_setpv
4398 Copies a string into an SV. The string must be null-terminated. Does not
4399 handle 'set' magic. See C<sv_setpv_mg>.
4405 Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
4407 register STRLEN len;
4409 SV_CHECK_THINKFIRST_COW_DROP(sv);
4415 SvUPGRADE(sv, SVt_PV);
4417 SvGROW(sv, len + 1);
4418 Move(ptr,SvPVX(sv),len+1,char);
4420 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4425 =for apidoc sv_setpv_mg
4427 Like C<sv_setpv>, but also handles 'set' magic.
4433 Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
4440 =for apidoc sv_usepvn
4442 Tells an SV to use C<ptr> to find its string value. Normally the string is
4443 stored inside the SV but sv_usepvn allows the SV to use an outside string.
4444 The C<ptr> should point to memory that was allocated by C<malloc>. The
4445 string length, C<len>, must be supplied. This function will realloc the
4446 memory pointed to by C<ptr>, so that pointer should not be freed or used by
4447 the programmer after giving it to sv_usepvn. Does not handle 'set' magic.
4448 See C<sv_usepvn_mg>.
4454 Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
4457 SV_CHECK_THINKFIRST_COW_DROP(sv);
4458 SvUPGRADE(sv, SVt_PV);
4463 if (SvPVX_const(sv))
4466 allocate = PERL_STRLEN_ROUNDUP(len + 1);
4467 ptr = saferealloc (ptr, allocate);
4470 SvLEN_set(sv, allocate);
4472 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4477 =for apidoc sv_usepvn_mg
4479 Like C<sv_usepvn>, but also handles 'set' magic.
4485 Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
4487 sv_usepvn(sv,ptr,len);
4491 #ifdef PERL_OLD_COPY_ON_WRITE
4492 /* Need to do this *after* making the SV normal, as we need the buffer
4493 pointer to remain valid until after we've copied it. If we let go too early,
4494 another thread could invalidate it by unsharing last of the same hash key
4495 (which it can do by means other than releasing copy-on-write Svs)
4496 or by changing the other copy-on-write SVs in the loop. */
4498 S_sv_release_COW(pTHX_ register SV *sv, const char *pvx, STRLEN len, SV *after)
4500 if (len) { /* this SV was SvIsCOW_normal(sv) */
4501 /* we need to find the SV pointing to us. */
4502 SV * const current = SV_COW_NEXT_SV(after);
4504 if (current == sv) {
4505 /* The SV we point to points back to us (there were only two of us
4507 Hence other SV is no longer copy on write either. */
4509 SvREADONLY_off(after);
4511 /* We need to follow the pointers around the loop. */
4513 while ((next = SV_COW_NEXT_SV(current)) != sv) {
4516 /* don't loop forever if the structure is bust, and we have
4517 a pointer into a closed loop. */
4518 assert (current != after);
4519 assert (SvPVX_const(current) == pvx);
4521 /* Make the SV before us point to the SV after us. */
4522 SV_COW_NEXT_SV_SET(current, after);
4525 unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
4530 Perl_sv_release_IVX(pTHX_ register SV *sv)
4533 sv_force_normal_flags(sv, 0);
4539 =for apidoc sv_force_normal_flags
4541 Undo various types of fakery on an SV: if the PV is a shared string, make
4542 a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
4543 an xpvmg; if we're a copy-on-write scalar, this is the on-write time when
4544 we do the copy, and is also used locally. If C<SV_COW_DROP_PV> is set
4545 then a copy-on-write scalar drops its PV buffer (if any) and becomes
4546 SvPOK_off rather than making a copy. (Used where this scalar is about to be
4547 set to some other value.) In addition, the C<flags> parameter gets passed to
4548 C<sv_unref_flags()> when unrefing. C<sv_force_normal> calls this function
4549 with flags set to 0.
4555 Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags)
4557 #ifdef PERL_OLD_COPY_ON_WRITE
4558 if (SvREADONLY(sv)) {
4559 /* At this point I believe I should acquire a global SV mutex. */
4561 const char * const pvx = SvPVX_const(sv);
4562 const STRLEN len = SvLEN(sv);
4563 const STRLEN cur = SvCUR(sv);
4564 SV * const next = SV_COW_NEXT_SV(sv); /* next COW sv in the loop. */
4566 PerlIO_printf(Perl_debug_log,
4567 "Copy on write: Force normal %ld\n",
4573 /* This SV doesn't own the buffer, so need to Newx() a new one: */
4574 SvPV_set(sv, (char*)0);
4576 if (flags & SV_COW_DROP_PV) {
4577 /* OK, so we don't need to copy our buffer. */
4580 SvGROW(sv, cur + 1);
4581 Move(pvx,SvPVX(sv),cur,char);
4585 sv_release_COW(sv, pvx, len, next);
4590 else if (IN_PERL_RUNTIME)
4591 Perl_croak(aTHX_ PL_no_modify);
4592 /* At this point I believe that I can drop the global SV mutex. */
4595 if (SvREADONLY(sv)) {
4597 const char * const pvx = SvPVX_const(sv);
4598 const STRLEN len = SvCUR(sv);
4601 SvPV_set(sv, Nullch);
4603 SvGROW(sv, len + 1);
4604 Move(pvx,SvPVX(sv),len,char);
4606 unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
4608 else if (IN_PERL_RUNTIME)
4609 Perl_croak(aTHX_ PL_no_modify);
4613 sv_unref_flags(sv, flags);
4614 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
4619 =for apidoc sv_force_normal
4621 Undo various types of fakery on an SV: if the PV is a shared string, make
4622 a private copy; if we're a ref, stop refing; if we're a&