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 #ifdef DEBUG_LEAKING_SCALARS
171 # define FREE_SV_DEBUG_FILE(sv) PerlMemfree((sv)->sv_debug_file)
173 # define FREE_SV_DEBUG_FILE(sv) PerlMemShared_free((sv)->sv_debug_file)
176 # define FREE_SV_DEBUG_FILE(sv)
179 #define plant_SV(p) \
181 FREE_SV_DEBUG_FILE(p); \
182 SvANY(p) = (void *)PL_sv_root; \
183 SvFLAGS(p) = SVTYPEMASK; \
188 /* sv_mutex must be held while calling uproot_SV() */
189 #define uproot_SV(p) \
192 PL_sv_root = (SV*)SvANY(p); \
197 /* make some more SVs by adding another arena */
199 /* sv_mutex must be held while calling more_sv() */
206 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
207 PL_nice_chunk = Nullch;
208 PL_nice_chunk_size = 0;
211 char *chunk; /* must use New here to match call to */
212 New(704,chunk,PERL_ARENA_SIZE,char); /* Safefree() in sv_free_arenas() */
213 sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
219 /* new_SV(): return a new, empty SV head */
221 #ifdef DEBUG_LEAKING_SCALARS
222 /* provide a real function for a debugger to play with */
232 sv = S_more_sv(aTHX);
237 sv->sv_debug_optype = PL_op ? PL_op->op_type : 0;
238 sv->sv_debug_line = (U16) ((PL_copline == NOLINE) ?
239 (PL_curcop ? CopLINE(PL_curcop) : 0) : PL_copline);
240 sv->sv_debug_inpad = 0;
241 sv->sv_debug_cloned = 0;
243 sv->sv_debug_file = PL_curcop ? savepv(CopFILE(PL_curcop)): NULL;
245 sv->sv_debug_file = PL_curcop ? savesharedpv(CopFILE(PL_curcop)): NULL;
250 # define new_SV(p) (p)=S_new_SV(aTHX)
259 (p) = S_more_sv(aTHX); \
268 /* del_SV(): return an empty SV head to the free list */
283 S_del_sv(pTHX_ SV *p)
288 for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
289 const SV * const sv = sva + 1;
290 const SV * const svend = &sva[SvREFCNT(sva)];
291 if (p >= sv && p < svend) {
297 if (ckWARN_d(WARN_INTERNAL))
298 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
299 "Attempt to free non-arena SV: 0x%"UVxf
300 pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
307 #else /* ! DEBUGGING */
309 #define del_SV(p) plant_SV(p)
311 #endif /* DEBUGGING */
315 =head1 SV Manipulation Functions
317 =for apidoc sv_add_arena
319 Given a chunk of memory, link it to the head of the list of arenas,
320 and split it into a list of free SVs.
326 Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
332 /* The first SV in an arena isn't an SV. */
333 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
334 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
335 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
337 PL_sv_arenaroot = sva;
338 PL_sv_root = sva + 1;
340 svend = &sva[SvREFCNT(sva) - 1];
343 SvANY(sv) = (void *)(SV*)(sv + 1);
347 /* Must always set typemask because it's awlays checked in on cleanup
348 when the arenas are walked looking for objects. */
349 SvFLAGS(sv) = SVTYPEMASK;
356 SvFLAGS(sv) = SVTYPEMASK;
359 /* visit(): call the named function for each non-free SV in the arenas
360 * whose flags field matches the flags/mask args. */
363 S_visit(pTHX_ SVFUNC_t f, U32 flags, U32 mask)
368 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
369 register const SV * const svend = &sva[SvREFCNT(sva)];
371 for (sv = sva + 1; sv < svend; ++sv) {
372 if (SvTYPE(sv) != SVTYPEMASK
373 && (sv->sv_flags & mask) == flags
386 /* called by sv_report_used() for each live SV */
389 do_report_used(pTHX_ SV *sv)
391 if (SvTYPE(sv) != SVTYPEMASK) {
392 PerlIO_printf(Perl_debug_log, "****\n");
399 =for apidoc sv_report_used
401 Dump the contents of all SVs not yet freed. (Debugging aid).
407 Perl_sv_report_used(pTHX)
410 visit(do_report_used, 0, 0);
414 /* called by sv_clean_objs() for each live SV */
417 do_clean_objs(pTHX_ SV *sv)
421 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
422 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv)));
434 /* XXX Might want to check arrays, etc. */
437 /* called by sv_clean_objs() for each live SV */
439 #ifndef DISABLE_DESTRUCTOR_KLUDGE
441 do_clean_named_objs(pTHX_ SV *sv)
443 if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) {
444 if ( SvOBJECT(GvSV(sv)) ||
445 (GvAV(sv) && SvOBJECT(GvAV(sv))) ||
446 (GvHV(sv) && SvOBJECT(GvHV(sv))) ||
447 (GvIO(sv) && SvOBJECT(GvIO(sv))) ||
448 (GvCV(sv) && SvOBJECT(GvCV(sv))) )
450 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv)));
451 SvFLAGS(sv) |= SVf_BREAK;
459 =for apidoc sv_clean_objs
461 Attempt to destroy all objects not yet freed
467 Perl_sv_clean_objs(pTHX)
469 PL_in_clean_objs = TRUE;
470 visit(do_clean_objs, SVf_ROK, SVf_ROK);
471 #ifndef DISABLE_DESTRUCTOR_KLUDGE
472 /* some barnacles may yet remain, clinging to typeglobs */
473 visit(do_clean_named_objs, SVt_PVGV, SVTYPEMASK);
475 PL_in_clean_objs = FALSE;
478 /* called by sv_clean_all() for each live SV */
481 do_clean_all(pTHX_ SV *sv)
483 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
484 SvFLAGS(sv) |= SVf_BREAK;
485 if (PL_comppad == (AV*)sv) {
487 PL_curpad = Null(SV**);
493 =for apidoc sv_clean_all
495 Decrement the refcnt of each remaining SV, possibly triggering a
496 cleanup. This function may have to be called multiple times to free
497 SVs which are in complex self-referential hierarchies.
503 Perl_sv_clean_all(pTHX)
506 PL_in_clean_all = TRUE;
507 cleaned = visit(do_clean_all, 0,0);
508 PL_in_clean_all = FALSE;
513 S_free_arena(pTHX_ void **root) {
515 void **next = *(void **)root;
522 =for apidoc sv_free_arenas
524 Deallocate the memory used by all arenas. Note that all the individual SV
525 heads and bodies within the arenas must already have been freed.
530 #define free_arena(name) \
532 S_free_arena(aTHX_ (void**) PL_ ## name ## _arenaroot); \
533 PL_ ## name ## _arenaroot = 0; \
534 PL_ ## name ## _root = 0; \
538 Perl_sv_free_arenas(pTHX)
543 /* Free arenas here, but be careful about fake ones. (We assume
544 contiguity of the fake ones with the corresponding real ones.) */
546 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
547 svanext = (SV*) SvANY(sva);
548 while (svanext && SvFAKE(svanext))
549 svanext = (SV*) SvANY(svanext);
567 #if defined(USE_ITHREADS)
572 Safefree(PL_nice_chunk);
573 PL_nice_chunk = Nullch;
574 PL_nice_chunk_size = 0;
579 /* ---------------------------------------------------------------------
581 * support functions for report_uninit()
584 /* the maxiumum size of array or hash where we will scan looking
585 * for the undefined element that triggered the warning */
587 #define FUV_MAX_SEARCH_SIZE 1000
589 /* Look for an entry in the hash whose value has the same SV as val;
590 * If so, return a mortal copy of the key. */
593 S_find_hash_subscript(pTHX_ HV *hv, SV* val)
599 if (!hv || SvMAGICAL(hv) || !HvARRAY(hv) ||
600 (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
605 for (i=HvMAX(hv); i>0; i--) {
607 for (entry = array[i]; entry; entry = HeNEXT(entry)) {
608 if (HeVAL(entry) != val)
610 if ( HeVAL(entry) == &PL_sv_undef ||
611 HeVAL(entry) == &PL_sv_placeholder)
615 if (HeKLEN(entry) == HEf_SVKEY)
616 return sv_mortalcopy(HeKEY_sv(entry));
617 return sv_2mortal(newSVpvn(HeKEY(entry), HeKLEN(entry)));
623 /* Look for an entry in the array whose value has the same SV as val;
624 * If so, return the index, otherwise return -1. */
627 S_find_array_subscript(pTHX_ AV *av, SV* val)
631 if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
632 (AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
636 for (i=AvFILLp(av); i>=0; i--) {
637 if (svp[i] == val && svp[i] != &PL_sv_undef)
643 /* S_varname(): return the name of a variable, optionally with a subscript.
644 * If gv is non-zero, use the name of that global, along with gvtype (one
645 * of "$", "@", "%"); otherwise use the name of the lexical at pad offset
646 * targ. Depending on the value of the subscript_type flag, return:
649 #define FUV_SUBSCRIPT_NONE 1 /* "@foo" */
650 #define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */
651 #define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */
652 #define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */
655 S_varname(pTHX_ GV *gv, const char *gvtype, PADOFFSET targ,
656 SV* keyname, I32 aindex, int subscript_type)
659 SV * const name = sv_newmortal();
662 /* simulate gv_fullname4(), but add literal '^' for $^FOO names
663 * XXX get rid of all this if gv_fullnameX() ever supports this
667 HV * const hv = GvSTASH(gv);
668 sv_setpv(name, gvtype);
671 else if (!(p=HvNAME_get(hv)))
673 if (strNE(p, "main")) {
675 sv_catpvn(name,"::", 2);
677 if (GvNAMELEN(gv)>= 1 &&
678 ((unsigned int)*GvNAME(gv)) <= 26)
680 Perl_sv_catpvf(aTHX_ name,"^%c", *GvNAME(gv) + 'A' - 1);
681 sv_catpvn(name,GvNAME(gv)+1,GvNAMELEN(gv)-1);
684 sv_catpvn(name,GvNAME(gv),GvNAMELEN(gv));
688 CV * const cv = find_runcv(&unused);
692 if (!cv || !CvPADLIST(cv))
694 av = (AV*)(*av_fetch(CvPADLIST(cv), 0, FALSE));
695 sv = *av_fetch(av, targ, FALSE);
696 /* SvLEN in a pad name is not to be trusted */
697 sv_setpv(name, SvPV_nolen_const(sv));
700 if (subscript_type == FUV_SUBSCRIPT_HASH) {
704 Perl_sv_catpvf(aTHX_ name, "{%s}",
705 pv_display(sv,SvPVX_const(keyname), SvCUR(keyname), 0, 32));
708 else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
710 Perl_sv_catpvf(aTHX_ name, "[%"IVdf"]", (IV)aindex);
712 else if (subscript_type == FUV_SUBSCRIPT_WITHIN)
713 sv_insert(name, 0, 0, "within ", 7);
720 =for apidoc find_uninit_var
722 Find the name of the undefined variable (if any) that caused the operator o
723 to issue a "Use of uninitialized value" warning.
724 If match is true, only return a name if it's value matches uninit_sv.
725 So roughly speaking, if a unary operator (such as OP_COS) generates a
726 warning, then following the direct child of the op may yield an
727 OP_PADSV or OP_GV that gives the name of the undefined variable. On the
728 other hand, with OP_ADD there are two branches to follow, so we only print
729 the variable name if we get an exact match.
731 The name is returned as a mortal SV.
733 Assumes that PL_op is the op that originally triggered the error, and that
734 PL_comppad/PL_curpad points to the currently executing pad.
740 S_find_uninit_var(pTHX_ OP* obase, SV* uninit_sv, bool match)
749 if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
750 uninit_sv == &PL_sv_placeholder)))
753 switch (obase->op_type) {
760 const bool pad = (obase->op_type == OP_PADAV || obase->op_type == OP_PADHV);
761 const bool hash = (obase->op_type == OP_PADHV || obase->op_type == OP_RV2HV);
764 int subscript_type = FUV_SUBSCRIPT_WITHIN;
766 if (pad) { /* @lex, %lex */
767 sv = PAD_SVl(obase->op_targ);
771 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
772 /* @global, %global */
773 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
776 sv = hash ? (SV*)GvHV(gv): (SV*)GvAV(gv);
778 else /* @{expr}, %{expr} */
779 return find_uninit_var(cUNOPx(obase)->op_first,
783 /* attempt to find a match within the aggregate */
785 keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
787 subscript_type = FUV_SUBSCRIPT_HASH;
790 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
792 subscript_type = FUV_SUBSCRIPT_ARRAY;
795 if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
798 return S_varname(aTHX_ gv, hash ? "%" : "@", obase->op_targ,
799 keysv, index, subscript_type);
803 if (match && PAD_SVl(obase->op_targ) != uninit_sv)
805 return S_varname(aTHX_ Nullgv, "$", obase->op_targ,
806 Nullsv, 0, FUV_SUBSCRIPT_NONE);
809 gv = cGVOPx_gv(obase);
810 if (!gv || (match && GvSV(gv) != uninit_sv))
812 return S_varname(aTHX_ gv, "$", 0, Nullsv, 0, FUV_SUBSCRIPT_NONE);
815 if (obase->op_flags & OPf_SPECIAL) { /* lexical array */
817 av = (AV*)PAD_SV(obase->op_targ);
818 if (!av || SvRMAGICAL(av))
820 svp = av_fetch(av, (I32)obase->op_private, FALSE);
821 if (!svp || *svp != uninit_sv)
824 return S_varname(aTHX_ Nullgv, "$", obase->op_targ,
825 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
828 gv = cGVOPx_gv(obase);
833 if (!av || SvRMAGICAL(av))
835 svp = av_fetch(av, (I32)obase->op_private, FALSE);
836 if (!svp || *svp != uninit_sv)
839 return S_varname(aTHX_ gv, "$", 0,
840 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
845 o = cUNOPx(obase)->op_first;
846 if (!o || o->op_type != OP_NULL ||
847 ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
849 return find_uninit_var(cBINOPo->op_last, uninit_sv, match);
854 /* $a[uninit_expr] or $h{uninit_expr} */
855 return find_uninit_var(cBINOPx(obase)->op_last, uninit_sv, match);
858 o = cBINOPx(obase)->op_first;
859 kid = cBINOPx(obase)->op_last;
861 /* get the av or hv, and optionally the gv */
863 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
864 sv = PAD_SV(o->op_targ);
866 else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
867 && cUNOPo->op_first->op_type == OP_GV)
869 gv = cGVOPx_gv(cUNOPo->op_first);
872 sv = o->op_type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)GvAV(gv);
877 if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
878 /* index is constant */
882 if (obase->op_type == OP_HELEM) {
883 HE* he = hv_fetch_ent((HV*)sv, cSVOPx_sv(kid), 0, 0);
884 if (!he || HeVAL(he) != uninit_sv)
888 svp = av_fetch((AV*)sv, SvIV(cSVOPx_sv(kid)), FALSE);
889 if (!svp || *svp != uninit_sv)
893 if (obase->op_type == OP_HELEM)
894 return S_varname(aTHX_ gv, "%", o->op_targ,
895 cSVOPx_sv(kid), 0, FUV_SUBSCRIPT_HASH);
897 return S_varname(aTHX_ gv, "@", o->op_targ, Nullsv,
898 SvIV(cSVOPx_sv(kid)), FUV_SUBSCRIPT_ARRAY);
902 /* index is an expression;
903 * attempt to find a match within the aggregate */
904 if (obase->op_type == OP_HELEM) {
905 SV * const keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
907 return S_varname(aTHX_ gv, "%", o->op_targ,
908 keysv, 0, FUV_SUBSCRIPT_HASH);
911 const I32 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
913 return S_varname(aTHX_ gv, "@", o->op_targ,
914 Nullsv, index, FUV_SUBSCRIPT_ARRAY);
918 return S_varname(aTHX_ gv,
919 (o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
921 o->op_targ, Nullsv, 0, FUV_SUBSCRIPT_WITHIN);
927 /* only examine RHS */
928 return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv, match);
931 o = cUNOPx(obase)->op_first;
932 if (o->op_type == OP_PUSHMARK)
935 if (!o->op_sibling) {
936 /* one-arg version of open is highly magical */
938 if (o->op_type == OP_GV) { /* open FOO; */
940 if (match && GvSV(gv) != uninit_sv)
942 return S_varname(aTHX_ gv, "$", 0,
943 Nullsv, 0, FUV_SUBSCRIPT_NONE);
945 /* other possibilities not handled are:
946 * open $x; or open my $x; should return '${*$x}'
947 * open expr; should return '$'.expr ideally
953 /* ops where $_ may be an implicit arg */
957 if ( !(obase->op_flags & OPf_STACKED)) {
958 if (uninit_sv == ((obase->op_private & OPpTARGET_MY)
959 ? PAD_SVl(obase->op_targ)
963 sv_setpvn(sv, "$_", 2);
971 /* skip filehandle as it can't produce 'undef' warning */
972 o = cUNOPx(obase)->op_first;
973 if ((obase->op_flags & OPf_STACKED) && o->op_type == OP_PUSHMARK)
974 o = o->op_sibling->op_sibling;
981 match = 1; /* XS or custom code could trigger random warnings */
986 if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs))
987 return sv_2mortal(newSVpv("${$/}", 0));
992 if (!(obase->op_flags & OPf_KIDS))
994 o = cUNOPx(obase)->op_first;
1000 /* if all except one arg are constant, or have no side-effects,
1001 * or are optimized away, then it's unambiguous */
1003 for (kid=o; kid; kid = kid->op_sibling) {
1005 ( (kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid)))
1006 || (kid->op_type == OP_NULL && ! (kid->op_flags & OPf_KIDS))
1007 || (kid->op_type == OP_PUSHMARK)
1011 if (o2) { /* more than one found */
1018 return find_uninit_var(o2, uninit_sv, match);
1022 sv = find_uninit_var(o, uninit_sv, 1);
1034 =for apidoc report_uninit
1036 Print appropriate "Use of uninitialized variable" warning
1042 Perl_report_uninit(pTHX_ SV* uninit_sv)
1045 SV* varname = Nullsv;
1047 varname = find_uninit_var(PL_op, uninit_sv,0);
1049 sv_insert(varname, 0, 0, " ", 1);
1051 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
1052 varname ? SvPV_nolen_const(varname) : "",
1053 " in ", OP_DESC(PL_op));
1056 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
1061 S_more_bodies (pTHX_ void **arena_root, void **root, size_t size)
1065 const size_t count = PERL_ARENA_SIZE/size;
1066 New(0, start, count*size, char);
1067 *((void **) start) = *arena_root;
1068 *arena_root = (void *)start;
1070 end = start + (count-1) * size;
1072 /* The initial slot is used to link the arenas together, so it isn't to be
1073 linked into the list of ready-to-use bodies. */
1077 *root = (void *)start;
1079 while (start < end) {
1080 char * const next = start + size;
1081 *(void**) start = (void *)next;
1084 *(void **)start = 0;
1089 /* grab a new thing from the free list, allocating more if necessary */
1092 S_new_body(pTHX_ void **arena_root, void **root, size_t size)
1096 xpv = *root ? *root : S_more_bodies(aTHX_ arena_root, root, size);
1097 *root = *(void**)xpv;
1102 /* return a thing to the free list */
1104 #define del_body(thing, root) \
1107 *(void **)thing = *root; \
1108 *root = (void*)thing; \
1112 /* Conventionally we simply malloc() a big block of memory, then divide it
1113 up into lots of the thing that we're allocating.
1115 This macro will expand to call to S_new_body. So for XPVBM (with ithreads),
1118 S_new_body(my_perl, (void**)&(my_perl->Ixpvbm_arenaroot),
1119 (void**)&(my_perl->Ixpvbm_root), sizeof(XPVBM), 0)
1122 #define new_body(TYPE,lctype) \
1123 S_new_body(aTHX_ (void**)&PL_ ## lctype ## _arenaroot, \
1124 (void**)&PL_ ## lctype ## _root, \
1127 #define del_body_type(p,TYPE,lctype) \
1128 del_body((void*)p, (void**)&PL_ ## lctype ## _root)
1130 /* But for some types, we cheat. The type starts with some members that are
1131 never accessed. So we allocate the substructure, starting at the first used
1132 member, then adjust the pointer back in memory by the size of the bit not
1133 allocated, so it's as if we allocated the full structure.
1134 (But things will all go boom if you write to the part that is "not there",
1135 because you'll be overwriting the last members of the preceding structure
1138 We calculate the correction using the STRUCT_OFFSET macro. For example, if
1139 xpv_allocated is the same structure as XPV then the two OFFSETs sum to zero,
1140 and the pointer is unchanged. If the allocated structure is smaller (no
1141 initial NV actually allocated) then the net effect is to subtract the size
1142 of the NV from the pointer, to return a new pointer as if an initial NV were
1145 This is the same trick as was used for NV and IV bodies. Ironically it
1146 doesn't need to be used for NV bodies any more, because NV is now at the
1147 start of the structure. IV bodies don't need it either, because they are
1148 no longer allocated. */
1150 #define new_body_allocated(TYPE,lctype,member) \
1151 (void*)((char*)S_new_body(aTHX_ (void**)&PL_ ## lctype ## _arenaroot, \
1152 (void**)&PL_ ## lctype ## _root, \
1153 sizeof(lctype ## _allocated)) - \
1154 STRUCT_OFFSET(TYPE, member) \
1155 + STRUCT_OFFSET(lctype ## _allocated, member))
1158 #define del_body_allocated(p,TYPE,lctype,member) \
1159 del_body((void*)((char*)p + STRUCT_OFFSET(TYPE, member) \
1160 - STRUCT_OFFSET(lctype ## _allocated, member)), \
1161 (void**)&PL_ ## lctype ## _root)
1163 #define my_safemalloc(s) (void*)safemalloc(s)
1164 #define my_safefree(p) safefree((char*)p)
1168 #define new_XNV() my_safemalloc(sizeof(XPVNV))
1169 #define del_XNV(p) my_safefree(p)
1171 #define new_XPV() my_safemalloc(sizeof(XPV))
1172 #define del_XPV(p) my_safefree(p)
1174 #define new_XPVIV() my_safemalloc(sizeof(XPVIV))
1175 #define del_XPVIV(p) my_safefree(p)
1177 #define new_XPVNV() my_safemalloc(sizeof(XPVNV))
1178 #define del_XPVNV(p) my_safefree(p)
1180 #define new_XPVCV() my_safemalloc(sizeof(XPVCV))
1181 #define del_XPVCV(p) my_safefree(p)
1183 #define new_XPVAV() my_safemalloc(sizeof(XPVAV))
1184 #define del_XPVAV(p) my_safefree(p)
1186 #define new_XPVHV() my_safemalloc(sizeof(XPVHV))
1187 #define del_XPVHV(p) my_safefree(p)
1189 #define new_XPVMG() my_safemalloc(sizeof(XPVMG))
1190 #define del_XPVMG(p) my_safefree(p)
1192 #define new_XPVGV() my_safemalloc(sizeof(XPVGV))
1193 #define del_XPVGV(p) my_safefree(p)
1195 #define new_XPVLV() my_safemalloc(sizeof(XPVLV))
1196 #define del_XPVLV(p) my_safefree(p)
1198 #define new_XPVBM() my_safemalloc(sizeof(XPVBM))
1199 #define del_XPVBM(p) my_safefree(p)
1203 #define new_XNV() new_body(NV, xnv)
1204 #define del_XNV(p) del_body_type(p, NV, xnv)
1206 #define new_XPV() new_body_allocated(XPV, xpv, xpv_cur)
1207 #define del_XPV(p) del_body_allocated(p, XPV, xpv, xpv_cur)
1209 #define new_XPVIV() new_body_allocated(XPVIV, xpviv, xpv_cur)
1210 #define del_XPVIV(p) del_body_allocated(p, XPVIV, xpviv, xpv_cur)
1212 #define new_XPVNV() new_body(XPVNV, xpvnv)
1213 #define del_XPVNV(p) del_body_type(p, XPVNV, xpvnv)
1215 #define new_XPVCV() new_body(XPVCV, xpvcv)
1216 #define del_XPVCV(p) del_body_type(p, XPVCV, xpvcv)
1218 #define new_XPVAV() new_body_allocated(XPVAV, xpvav, xav_fill)
1219 #define del_XPVAV(p) del_body_allocated(p, XPVAV, xpvav, xav_fill)
1221 #define new_XPVHV() new_body_allocated(XPVHV, xpvhv, xhv_fill)
1222 #define del_XPVHV(p) del_body_allocated(p, XPVHV, xpvhv, xhv_fill)
1224 #define new_XPVMG() new_body(XPVMG, xpvmg)
1225 #define del_XPVMG(p) del_body_type(p, XPVMG, xpvmg)
1227 #define new_XPVGV() new_body(XPVGV, xpvgv)
1228 #define del_XPVGV(p) del_body_type(p, XPVGV, xpvgv)
1230 #define new_XPVLV() new_body(XPVLV, xpvlv)
1231 #define del_XPVLV(p) del_body_type(p, XPVLV, xpvlv)
1233 #define new_XPVBM() new_body(XPVBM, xpvbm)
1234 #define del_XPVBM(p) del_body_type(p, XPVBM, xpvbm)
1238 #define new_XPVFM() my_safemalloc(sizeof(XPVFM))
1239 #define del_XPVFM(p) my_safefree(p)
1241 #define new_XPVIO() my_safemalloc(sizeof(XPVIO))
1242 #define del_XPVIO(p) my_safefree(p)
1245 =for apidoc sv_upgrade
1247 Upgrade an SV to a more complex form. Generally adds a new body type to the
1248 SV, then copies across as much information as possible from the old body.
1249 You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>.
1255 Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
1257 void** old_body_arena;
1258 size_t old_body_offset;
1259 size_t old_body_length; /* Well, the length to copy. */
1261 #ifndef NV_ZERO_IS_ALLBITS_ZERO
1262 /* If NV 0.0 is store as all bits 0 then Zero() already creates a correct
1264 bool zero_nv = TRUE;
1267 size_t new_body_length;
1268 size_t new_body_offset;
1269 void** new_body_arena;
1270 void** new_body_arenaroot;
1271 const U32 old_type = SvTYPE(sv);
1273 if (mt != SVt_PV && SvIsCOW(sv)) {
1274 sv_force_normal_flags(sv, 0);
1277 if (SvTYPE(sv) == mt)
1280 if (SvTYPE(sv) > mt)
1281 Perl_croak(aTHX_ "sv_upgrade from type %d down to type %d",
1282 (int)SvTYPE(sv), (int)mt);
1285 old_body = SvANY(sv);
1287 old_body_offset = 0;
1288 old_body_length = 0;
1289 new_body_offset = 0;
1290 new_body_length = ~0;
1292 /* Copying structures onto other structures that have been neatly zeroed
1293 has a subtle gotcha. Consider XPVMG
1295 +------+------+------+------+------+-------+-------+
1296 | NV | CUR | LEN | IV | MAGIC | STASH |
1297 +------+------+------+------+------+-------+-------+
1298 0 4 8 12 16 20 24 28
1300 where NVs are aligned to 8 bytes, so that sizeof that structure is
1301 actually 32 bytes long, with 4 bytes of padding at the end:
1303 +------+------+------+------+------+-------+-------+------+
1304 | NV | CUR | LEN | IV | MAGIC | STASH | ??? |
1305 +------+------+------+------+------+-------+-------+------+
1306 0 4 8 12 16 20 24 28 32
1308 so what happens if you allocate memory for this structure:
1310 +------+------+------+------+------+-------+-------+------+------+...
1311 | NV | CUR | LEN | IV | MAGIC | STASH | GP | NAME |
1312 +------+------+------+------+------+-------+-------+------+------+...
1313 0 4 8 12 16 20 24 28 32 36
1315 zero it, then copy sizeof(XPVMG) bytes on top of it? Not quite what you
1316 expect, because you copy the area marked ??? onto GP. Now, ??? may have
1317 started out as zero once, but it's quite possible that it isn't. So now,
1318 rather than a nicely zeroed GP, you have it pointing somewhere random.
1321 (In fact, GP ends up pointing at a previous GP structure, because the
1322 principle cause of the padding in XPVMG getting garbage is a copy of
1323 sizeof(XPVMG) bytes from a XPVGV structure in sv_unglob)
1325 So we are careful and work out the size of used parts of all the
1328 switch (SvTYPE(sv)) {
1334 else if (mt < SVt_PVIV)
1336 old_body_offset = STRUCT_OFFSET(XPVIV, xiv_iv);
1337 old_body_length = sizeof(IV);
1340 old_body_arena = (void **) &PL_xnv_root;
1341 old_body_length = sizeof(NV);
1342 #ifndef NV_ZERO_IS_ALLBITS_ZERO
1351 old_body_arena = (void **) &PL_xpv_root;
1352 old_body_offset = STRUCT_OFFSET(XPV, xpv_cur)
1353 - STRUCT_OFFSET(xpv_allocated, xpv_cur);
1354 old_body_length = STRUCT_OFFSET(XPV, xpv_len)
1355 + sizeof (((XPV*)SvANY(sv))->xpv_len)
1359 else if (mt == SVt_NV)
1363 old_body_arena = (void **) &PL_xpviv_root;
1364 old_body_offset = STRUCT_OFFSET(XPVIV, xpv_cur)
1365 - STRUCT_OFFSET(xpviv_allocated, xpv_cur);
1366 old_body_length = STRUCT_OFFSET(XPVIV, xiv_u)
1367 + sizeof (((XPVIV*)SvANY(sv))->xiv_u)
1371 old_body_arena = (void **) &PL_xpvnv_root;
1372 old_body_length = STRUCT_OFFSET(XPVNV, xiv_u)
1373 + sizeof (((XPVNV*)SvANY(sv))->xiv_u);
1374 #ifndef NV_ZERO_IS_ALLBITS_ZERO
1379 /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
1380 there's no way that it can be safely upgraded, because perl.c
1381 expects to Safefree(SvANY(PL_mess_sv)) */
1382 assert(sv != PL_mess_sv);
1383 /* This flag bit is used to mean other things in other scalar types.
1384 Given that it only has meaning inside the pad, it shouldn't be set
1385 on anything that can get upgraded. */
1386 assert((SvFLAGS(sv) & SVpad_TYPED) == 0);
1387 old_body_arena = (void **) &PL_xpvmg_root;
1388 old_body_length = STRUCT_OFFSET(XPVMG, xmg_stash)
1389 + sizeof (((XPVMG*)SvANY(sv))->xmg_stash);
1390 #ifndef NV_ZERO_IS_ALLBITS_ZERO
1395 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
1398 SvFLAGS(sv) &= ~SVTYPEMASK;
1403 Perl_croak(aTHX_ "Can't upgrade to undef");
1405 assert(old_type == SVt_NULL);
1406 SvANY(sv) = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
1410 assert(old_type == SVt_NULL);
1411 SvANY(sv) = new_XNV();
1415 assert(old_type == SVt_NULL);
1416 SvANY(sv) = &sv->sv_u.svu_rv;
1420 SvANY(sv) = new_XPVHV();
1423 HvTOTALKEYS(sv) = 0;
1428 SvANY(sv) = new_XPVAV();
1435 /* SVt_NULL isn't the only thing upgraded to AV or HV.
1436 The target created by newSVrv also is, and it can have magic.
1437 However, it never has SvPVX set.
1439 if (old_type >= SVt_RV) {
1440 assert(SvPVX_const(sv) == 0);
1443 /* Could put this in the else clause below, as PVMG must have SvPVX
1444 0 already (the assertion above) */
1445 SvPV_set(sv, (char*)0);
1447 if (old_type >= SVt_PVMG) {
1448 SvMAGIC_set(sv, ((XPVMG*)old_body)->xmg_magic);
1449 SvSTASH_set(sv, ((XPVMG*)old_body)->xmg_stash);
1457 new_body = new_XPVIO();
1458 new_body_length = sizeof(XPVIO);
1461 new_body = new_XPVFM();
1462 new_body_length = sizeof(XPVFM);
1466 new_body_length = sizeof(XPVBM);
1467 new_body_arena = (void **) &PL_xpvbm_root;
1468 new_body_arenaroot = (void **) &PL_xpvbm_arenaroot;
1471 new_body_length = sizeof(XPVGV);
1472 new_body_arena = (void **) &PL_xpvgv_root;
1473 new_body_arenaroot = (void **) &PL_xpvgv_arenaroot;
1476 new_body_length = sizeof(XPVCV);
1477 new_body_arena = (void **) &PL_xpvcv_root;
1478 new_body_arenaroot = (void **) &PL_xpvcv_arenaroot;
1481 new_body_length = sizeof(XPVLV);
1482 new_body_arena = (void **) &PL_xpvlv_root;
1483 new_body_arenaroot = (void **) &PL_xpvlv_arenaroot;
1486 new_body_length = sizeof(XPVMG);
1487 new_body_arena = (void **) &PL_xpvmg_root;
1488 new_body_arenaroot = (void **) &PL_xpvmg_arenaroot;
1491 new_body_length = sizeof(XPVNV);
1492 new_body_arena = (void **) &PL_xpvnv_root;
1493 new_body_arenaroot = (void **) &PL_xpvnv_arenaroot;
1496 new_body_offset = STRUCT_OFFSET(XPVIV, xpv_cur)
1497 - STRUCT_OFFSET(xpviv_allocated, xpv_cur);
1498 new_body_length = sizeof(XPVIV) - new_body_offset;
1499 new_body_arena = (void **) &PL_xpviv_root;
1500 new_body_arenaroot = (void **) &PL_xpviv_arenaroot;
1501 /* XXX Is this still needed? Was it ever needed? Surely as there is
1502 no route from NV to PVIV, NOK can never be true */
1506 goto new_body_no_NV;
1508 new_body_offset = STRUCT_OFFSET(XPV, xpv_cur)
1509 - STRUCT_OFFSET(xpv_allocated, xpv_cur);
1510 new_body_length = sizeof(XPV) - new_body_offset;
1511 new_body_arena = (void **) &PL_xpv_root;
1512 new_body_arenaroot = (void **) &PL_xpv_arenaroot;
1514 /* PV and PVIV don't have an NV slot. */
1515 #ifndef NV_ZERO_IS_ALLBITS_ZERO
1520 assert(new_body_length);
1522 /* This points to the start of the allocated area. */
1523 new_body = S_new_body(aTHX_ new_body_arenaroot, new_body_arena,
1526 /* We always allocated the full length item with PURIFY */
1527 new_body_length += new_body_offset;
1528 new_body_offset = 0;
1529 new_body = my_safemalloc(new_body_length);
1533 Zero(new_body, new_body_length, char);
1534 new_body = ((char *)new_body) - new_body_offset;
1535 SvANY(sv) = new_body;
1537 if (old_body_length) {
1538 Copy((char *)old_body + old_body_offset,
1539 (char *)new_body + old_body_offset,
1540 old_body_length, char);
1543 #ifndef NV_ZERO_IS_ALLBITS_ZERO
1549 IoPAGE_LEN(sv) = 60;
1550 if (old_type < SVt_RV)
1554 Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu", mt);
1558 if (old_body_arena) {
1560 my_safefree(old_body);
1562 del_body((void*)((char*)old_body + old_body_offset),
1569 =for apidoc sv_backoff
1571 Remove any string offset. You should normally use the C<SvOOK_off> macro
1578 Perl_sv_backoff(pTHX_ register SV *sv)
1581 assert(SvTYPE(sv) != SVt_PVHV);
1582 assert(SvTYPE(sv) != SVt_PVAV);
1584 const char * const s = SvPVX_const(sv);
1585 SvLEN_set(sv, SvLEN(sv) + SvIVX(sv));
1586 SvPV_set(sv, SvPVX(sv) - SvIVX(sv));
1588 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
1590 SvFLAGS(sv) &= ~SVf_OOK;
1597 Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
1598 upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1599 Use the C<SvGROW> wrapper instead.
1605 Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
1609 #ifdef HAS_64K_LIMIT
1610 if (newlen >= 0x10000) {
1611 PerlIO_printf(Perl_debug_log,
1612 "Allocation too large: %"UVxf"\n", (UV)newlen);
1615 #endif /* HAS_64K_LIMIT */
1618 if (SvTYPE(sv) < SVt_PV) {
1619 sv_upgrade(sv, SVt_PV);
1620 s = SvPVX_mutable(sv);
1622 else if (SvOOK(sv)) { /* pv is offset? */
1624 s = SvPVX_mutable(sv);
1625 if (newlen > SvLEN(sv))
1626 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
1627 #ifdef HAS_64K_LIMIT
1628 if (newlen >= 0x10000)
1633 s = SvPVX_mutable(sv);
1635 if (newlen > SvLEN(sv)) { /* need more room? */
1636 newlen = PERL_STRLEN_ROUNDUP(newlen);
1637 if (SvLEN(sv) && s) {
1639 const STRLEN l = malloced_size((void*)SvPVX_const(sv));
1645 s = saferealloc(s, newlen);
1648 s = safemalloc(newlen);
1649 if (SvPVX_const(sv) && SvCUR(sv)) {
1650 Move(SvPVX_const(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
1654 SvLEN_set(sv, newlen);
1660 =for apidoc sv_setiv
1662 Copies an integer into the given SV, upgrading first if necessary.
1663 Does not handle 'set' magic. See also C<sv_setiv_mg>.
1669 Perl_sv_setiv(pTHX_ register SV *sv, IV i)
1671 SV_CHECK_THINKFIRST_COW_DROP(sv);
1672 switch (SvTYPE(sv)) {
1674 sv_upgrade(sv, SVt_IV);
1677 sv_upgrade(sv, SVt_PVNV);
1681 sv_upgrade(sv, SVt_PVIV);
1690 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
1693 (void)SvIOK_only(sv); /* validate number */
1699 =for apidoc sv_setiv_mg
1701 Like C<sv_setiv>, but also handles 'set' magic.
1707 Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
1714 =for apidoc sv_setuv
1716 Copies an unsigned integer into the given SV, upgrading first if necessary.
1717 Does not handle 'set' magic. See also C<sv_setuv_mg>.
1723 Perl_sv_setuv(pTHX_ register SV *sv, UV u)
1725 /* With these two if statements:
1726 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
1729 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
1731 If you wish to remove them, please benchmark to see what the effect is
1733 if (u <= (UV)IV_MAX) {
1734 sv_setiv(sv, (IV)u);
1743 =for apidoc sv_setuv_mg
1745 Like C<sv_setuv>, but also handles 'set' magic.
1751 Perl_sv_setuv_mg(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);
1772 =for apidoc sv_setnv
1774 Copies a double into the given SV, upgrading first if necessary.
1775 Does not handle 'set' magic. See also C<sv_setnv_mg>.
1781 Perl_sv_setnv(pTHX_ register SV *sv, NV num)
1783 SV_CHECK_THINKFIRST_COW_DROP(sv);
1784 switch (SvTYPE(sv)) {
1787 sv_upgrade(sv, SVt_NV);
1792 sv_upgrade(sv, SVt_PVNV);
1801 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
1805 (void)SvNOK_only(sv); /* validate number */
1810 =for apidoc sv_setnv_mg
1812 Like C<sv_setnv>, but also handles 'set' magic.
1818 Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
1824 /* Print an "isn't numeric" warning, using a cleaned-up,
1825 * printable version of the offending string
1829 S_not_a_number(pTHX_ SV *sv)
1836 dsv = sv_2mortal(newSVpv("", 0));
1837 pv = sv_uni_display(dsv, sv, 10, 0);
1840 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
1841 /* each *s can expand to 4 chars + "...\0",
1842 i.e. need room for 8 chars */
1844 const char *s, *end;
1845 for (s = SvPVX_const(sv), end = s + SvCUR(sv); s < end && d < limit;
1848 if (ch & 128 && !isPRINT_LC(ch)) {
1857 else if (ch == '\r') {
1861 else if (ch == '\f') {
1865 else if (ch == '\\') {
1869 else if (ch == '\0') {
1873 else if (isPRINT_LC(ch))
1890 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1891 "Argument \"%s\" isn't numeric in %s", pv,
1894 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1895 "Argument \"%s\" isn't numeric", pv);
1899 =for apidoc looks_like_number
1901 Test if the content of an SV looks like a number (or is a number).
1902 C<Inf> and C<Infinity> are treated as numbers (so will not issue a
1903 non-numeric warning), even if your atof() doesn't grok them.
1909 Perl_looks_like_number(pTHX_ SV *sv)
1911 register const char *sbegin;
1915 sbegin = SvPVX_const(sv);
1918 else if (SvPOKp(sv))
1919 sbegin = SvPV_const(sv, len);
1921 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
1922 return grok_number(sbegin, len, NULL);
1925 /* Actually, ISO C leaves conversion of UV to IV undefined, but
1926 until proven guilty, assume that things are not that bad... */
1931 As 64 bit platforms often have an NV that doesn't preserve all bits of
1932 an IV (an assumption perl has been based on to date) it becomes necessary
1933 to remove the assumption that the NV always carries enough precision to
1934 recreate the IV whenever needed, and that the NV is the canonical form.
1935 Instead, IV/UV and NV need to be given equal rights. So as to not lose
1936 precision as a side effect of conversion (which would lead to insanity
1937 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1938 1) to distinguish between IV/UV/NV slots that have cached a valid
1939 conversion where precision was lost and IV/UV/NV slots that have a
1940 valid conversion which has lost no precision
1941 2) to ensure that if a numeric conversion to one form is requested that
1942 would lose precision, the precise conversion (or differently
1943 imprecise conversion) is also performed and cached, to prevent
1944 requests for different numeric formats on the same SV causing
1945 lossy conversion chains. (lossless conversion chains are perfectly
1950 SvIOKp is true if the IV slot contains a valid value
1951 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
1952 SvNOKp is true if the NV slot contains a valid value
1953 SvNOK is true only if the NV value is accurate
1956 while converting from PV to NV, check to see if converting that NV to an
1957 IV(or UV) would lose accuracy over a direct conversion from PV to
1958 IV(or UV). If it would, cache both conversions, return NV, but mark
1959 SV as IOK NOKp (ie not NOK).
1961 While converting from PV to IV, check to see if converting that IV to an
1962 NV would lose accuracy over a direct conversion from PV to NV. If it
1963 would, cache both conversions, flag similarly.
1965 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
1966 correctly because if IV & NV were set NV *always* overruled.
1967 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
1968 changes - now IV and NV together means that the two are interchangeable:
1969 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
1971 The benefit of this is that operations such as pp_add know that if
1972 SvIOK is true for both left and right operands, then integer addition
1973 can be used instead of floating point (for cases where the result won't
1974 overflow). Before, floating point was always used, which could lead to
1975 loss of precision compared with integer addition.
1977 * making IV and NV equal status should make maths accurate on 64 bit
1979 * may speed up maths somewhat if pp_add and friends start to use
1980 integers when possible instead of fp. (Hopefully the overhead in
1981 looking for SvIOK and checking for overflow will not outweigh the
1982 fp to integer speedup)
1983 * will slow down integer operations (callers of SvIV) on "inaccurate"
1984 values, as the change from SvIOK to SvIOKp will cause a call into
1985 sv_2iv each time rather than a macro access direct to the IV slot
1986 * should speed up number->string conversion on integers as IV is
1987 favoured when IV and NV are equally accurate
1989 ####################################################################
1990 You had better be using SvIOK_notUV if you want an IV for arithmetic:
1991 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
1992 On the other hand, SvUOK is true iff UV.
1993 ####################################################################
1995 Your mileage will vary depending your CPU's relative fp to integer
1999 #ifndef NV_PRESERVES_UV
2000 # define IS_NUMBER_UNDERFLOW_IV 1
2001 # define IS_NUMBER_UNDERFLOW_UV 2
2002 # define IS_NUMBER_IV_AND_UV 2
2003 # define IS_NUMBER_OVERFLOW_IV 4
2004 # define IS_NUMBER_OVERFLOW_UV 5
2006 /* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
2008 /* For sv_2nv these three cases are "SvNOK and don't bother casting" */
2010 S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype)
2012 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));
2013 if (SvNVX(sv) < (NV)IV_MIN) {
2014 (void)SvIOKp_on(sv);
2016 SvIV_set(sv, IV_MIN);
2017 return IS_NUMBER_UNDERFLOW_IV;
2019 if (SvNVX(sv) > (NV)UV_MAX) {
2020 (void)SvIOKp_on(sv);
2023 SvUV_set(sv, UV_MAX);
2024 return IS_NUMBER_OVERFLOW_UV;
2026 (void)SvIOKp_on(sv);
2028 /* Can't use strtol etc to convert this string. (See truth table in
2030 if (SvNVX(sv) <= (UV)IV_MAX) {
2031 SvIV_set(sv, I_V(SvNVX(sv)));
2032 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2033 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
2035 /* Integer is imprecise. NOK, IOKp */
2037 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
2040 SvUV_set(sv, U_V(SvNVX(sv)));
2041 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2042 if (SvUVX(sv) == UV_MAX) {
2043 /* As we know that NVs don't preserve UVs, UV_MAX cannot
2044 possibly be preserved by NV. Hence, it must be overflow.
2046 return IS_NUMBER_OVERFLOW_UV;
2048 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
2050 /* Integer is imprecise. NOK, IOKp */
2052 return IS_NUMBER_OVERFLOW_IV;
2054 #endif /* !NV_PRESERVES_UV*/
2056 /* sv_2iv() is now a macro using Perl_sv_2iv_flags();
2057 * this function provided for binary compatibility only
2061 Perl_sv_2iv(pTHX_ register SV *sv)
2063 return sv_2iv_flags(sv, SV_GMAGIC);
2067 =for apidoc sv_2iv_flags
2069 Return the integer value of an SV, doing any necessary string
2070 conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2071 Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
2077 Perl_sv_2iv_flags(pTHX_ register SV *sv, I32 flags)
2081 if (SvGMAGICAL(sv)) {
2082 if (flags & SV_GMAGIC)
2087 return I_V(SvNVX(sv));
2089 if (SvPOKp(sv) && SvLEN(sv))
2092 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2093 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2099 if (SvTHINKFIRST(sv)) {
2102 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
2103 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
2104 return SvIV(tmpstr);
2105 return PTR2IV(SvRV(sv));
2108 sv_force_normal_flags(sv, 0);
2110 if (SvREADONLY(sv) && !SvOK(sv)) {
2111 if (ckWARN(WARN_UNINITIALIZED))
2118 return (IV)(SvUVX(sv));
2125 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2126 * without also getting a cached IV/UV from it at the same time
2127 * (ie PV->NV conversion should detect loss of accuracy and cache
2128 * IV or UV at same time to avoid this. NWC */
2130 if (SvTYPE(sv) == SVt_NV)
2131 sv_upgrade(sv, SVt_PVNV);
2133 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2134 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2135 certainly cast into the IV range at IV_MAX, whereas the correct
2136 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2138 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2139 SvIV_set(sv, I_V(SvNVX(sv)));
2140 if (SvNVX(sv) == (NV) SvIVX(sv)
2141 #ifndef NV_PRESERVES_UV
2142 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2143 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2144 /* Don't flag it as "accurately an integer" if the number
2145 came from a (by definition imprecise) NV operation, and
2146 we're outside the range of NV integer precision */
2149 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2150 DEBUG_c(PerlIO_printf(Perl_debug_log,
2151 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
2157 /* IV not precise. No need to convert from PV, as NV
2158 conversion would already have cached IV if it detected
2159 that PV->IV would be better than PV->NV->IV
2160 flags already correct - don't set public IOK. */
2161 DEBUG_c(PerlIO_printf(Perl_debug_log,
2162 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
2167 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2168 but the cast (NV)IV_MIN rounds to a the value less (more
2169 negative) than IV_MIN which happens to be equal to SvNVX ??
2170 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2171 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2172 (NV)UVX == NVX are both true, but the values differ. :-(
2173 Hopefully for 2s complement IV_MIN is something like
2174 0x8000000000000000 which will be exact. NWC */
2177 SvUV_set(sv, U_V(SvNVX(sv)));
2179 (SvNVX(sv) == (NV) SvUVX(sv))
2180 #ifndef NV_PRESERVES_UV
2181 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2182 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2183 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2184 /* Don't flag it as "accurately an integer" if the number
2185 came from a (by definition imprecise) NV operation, and
2186 we're outside the range of NV integer precision */
2192 DEBUG_c(PerlIO_printf(Perl_debug_log,
2193 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
2197 return (IV)SvUVX(sv);
2200 else if (SvPOKp(sv) && SvLEN(sv)) {
2202 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2203 /* We want to avoid a possible problem when we cache an IV which
2204 may be later translated to an NV, and the resulting NV is not
2205 the same as the direct translation of the initial string
2206 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2207 be careful to ensure that the value with the .456 is around if the
2208 NV value is requested in the future).
2210 This means that if we cache such an IV, we need to cache the
2211 NV as well. Moreover, we trade speed for space, and do not
2212 cache the NV if we are sure it's not needed.
2215 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2216 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2217 == IS_NUMBER_IN_UV) {
2218 /* It's definitely an integer, only upgrade to PVIV */
2219 if (SvTYPE(sv) < SVt_PVIV)
2220 sv_upgrade(sv, SVt_PVIV);
2222 } else if (SvTYPE(sv) < SVt_PVNV)
2223 sv_upgrade(sv, SVt_PVNV);
2225 /* If NV preserves UV then we only use the UV value if we know that
2226 we aren't going to call atof() below. If NVs don't preserve UVs
2227 then the value returned may have more precision than atof() will
2228 return, even though value isn't perfectly accurate. */
2229 if ((numtype & (IS_NUMBER_IN_UV
2230 #ifdef NV_PRESERVES_UV
2233 )) == IS_NUMBER_IN_UV) {
2234 /* This won't turn off the public IOK flag if it was set above */
2235 (void)SvIOKp_on(sv);
2237 if (!(numtype & IS_NUMBER_NEG)) {
2239 if (value <= (UV)IV_MAX) {
2240 SvIV_set(sv, (IV)value);
2242 SvUV_set(sv, value);
2246 /* 2s complement assumption */
2247 if (value <= (UV)IV_MIN) {
2248 SvIV_set(sv, -(IV)value);
2250 /* Too negative for an IV. This is a double upgrade, but
2251 I'm assuming it will be rare. */
2252 if (SvTYPE(sv) < SVt_PVNV)
2253 sv_upgrade(sv, SVt_PVNV);
2257 SvNV_set(sv, -(NV)value);
2258 SvIV_set(sv, IV_MIN);
2262 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2263 will be in the previous block to set the IV slot, and the next
2264 block to set the NV slot. So no else here. */
2266 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2267 != IS_NUMBER_IN_UV) {
2268 /* It wasn't an (integer that doesn't overflow the UV). */
2269 SvNV_set(sv, Atof(SvPVX_const(sv)));
2271 if (! numtype && ckWARN(WARN_NUMERIC))
2274 #if defined(USE_LONG_DOUBLE)
2275 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
2276 PTR2UV(sv), SvNVX(sv)));
2278 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
2279 PTR2UV(sv), SvNVX(sv)));
2283 #ifdef NV_PRESERVES_UV
2284 (void)SvIOKp_on(sv);
2286 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2287 SvIV_set(sv, I_V(SvNVX(sv)));
2288 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2291 /* Integer is imprecise. NOK, IOKp */
2293 /* UV will not work better than IV */
2295 if (SvNVX(sv) > (NV)UV_MAX) {
2297 /* Integer is inaccurate. NOK, IOKp, is UV */
2298 SvUV_set(sv, UV_MAX);
2301 SvUV_set(sv, U_V(SvNVX(sv)));
2302 /* 0xFFFFFFFFFFFFFFFF not an issue in here */
2303 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2307 /* Integer is imprecise. NOK, IOKp, is UV */
2313 #else /* NV_PRESERVES_UV */
2314 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2315 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2316 /* The IV slot will have been set from value returned by
2317 grok_number above. The NV slot has just been set using
2320 assert (SvIOKp(sv));
2322 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2323 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2324 /* Small enough to preserve all bits. */
2325 (void)SvIOKp_on(sv);
2327 SvIV_set(sv, I_V(SvNVX(sv)));
2328 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2330 /* Assumption: first non-preserved integer is < IV_MAX,
2331 this NV is in the preserved range, therefore: */
2332 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2334 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);
2338 0 0 already failed to read UV.
2339 0 1 already failed to read UV.
2340 1 0 you won't get here in this case. IV/UV
2341 slot set, public IOK, Atof() unneeded.
2342 1 1 already read UV.
2343 so there's no point in sv_2iuv_non_preserve() attempting
2344 to use atol, strtol, strtoul etc. */
2345 if (sv_2iuv_non_preserve (sv, numtype)
2346 >= IS_NUMBER_OVERFLOW_IV)
2350 #endif /* NV_PRESERVES_UV */
2353 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
2355 if (SvTYPE(sv) < SVt_IV)
2356 /* Typically the caller expects that sv_any is not NULL now. */
2357 sv_upgrade(sv, SVt_IV);
2360 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2361 PTR2UV(sv),SvIVX(sv)));
2362 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
2365 /* sv_2uv() is now a macro using Perl_sv_2uv_flags();
2366 * this function provided for binary compatibility only
2370 Perl_sv_2uv(pTHX_ register SV *sv)
2372 return sv_2uv_flags(sv, SV_GMAGIC);
2376 =for apidoc sv_2uv_flags
2378 Return the unsigned integer value of an SV, doing any necessary string
2379 conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2380 Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
2386 Perl_sv_2uv_flags(pTHX_ register SV *sv, I32 flags)
2390 if (SvGMAGICAL(sv)) {
2391 if (flags & SV_GMAGIC)
2396 return U_V(SvNVX(sv));
2397 if (SvPOKp(sv) && SvLEN(sv))
2400 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2401 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2407 if (SvTHINKFIRST(sv)) {
2410 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
2411 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
2412 return SvUV(tmpstr);
2413 return PTR2UV(SvRV(sv));
2416 sv_force_normal_flags(sv, 0);
2418 if (SvREADONLY(sv) && !SvOK(sv)) {
2419 if (ckWARN(WARN_UNINITIALIZED))
2429 return (UV)SvIVX(sv);
2433 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2434 * without also getting a cached IV/UV from it at the same time
2435 * (ie PV->NV conversion should detect loss of accuracy and cache
2436 * IV or UV at same time to avoid this. */
2437 /* IV-over-UV optimisation - choose to cache IV if possible */
2439 if (SvTYPE(sv) == SVt_NV)
2440 sv_upgrade(sv, SVt_PVNV);
2442 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2443 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2444 SvIV_set(sv, I_V(SvNVX(sv)));
2445 if (SvNVX(sv) == (NV) SvIVX(sv)
2446 #ifndef NV_PRESERVES_UV
2447 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2448 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2449 /* Don't flag it as "accurately an integer" if the number
2450 came from a (by definition imprecise) NV operation, and
2451 we're outside the range of NV integer precision */
2454 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2455 DEBUG_c(PerlIO_printf(Perl_debug_log,
2456 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (precise)\n",
2462 /* IV not precise. No need to convert from PV, as NV
2463 conversion would already have cached IV if it detected
2464 that PV->IV would be better than PV->NV->IV
2465 flags already correct - don't set public IOK. */
2466 DEBUG_c(PerlIO_printf(Perl_debug_log,
2467 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (imprecise)\n",
2472 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2473 but the cast (NV)IV_MIN rounds to a the value less (more
2474 negative) than IV_MIN which happens to be equal to SvNVX ??
2475 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2476 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2477 (NV)UVX == NVX are both true, but the values differ. :-(
2478 Hopefully for 2s complement IV_MIN is something like
2479 0x8000000000000000 which will be exact. NWC */
2482 SvUV_set(sv, U_V(SvNVX(sv)));
2484 (SvNVX(sv) == (NV) SvUVX(sv))
2485 #ifndef NV_PRESERVES_UV
2486 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2487 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2488 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2489 /* Don't flag it as "accurately an integer" if the number
2490 came from a (by definition imprecise) NV operation, and
2491 we're outside the range of NV integer precision */
2496 DEBUG_c(PerlIO_printf(Perl_debug_log,
2497 "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n",
2503 else if (SvPOKp(sv) && SvLEN(sv)) {
2505 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2507 /* We want to avoid a possible problem when we cache a UV which
2508 may be later translated to an NV, and the resulting NV is not
2509 the translation of the initial data.
2511 This means that if we cache such a UV, we need to cache the
2512 NV as well. Moreover, we trade speed for space, and do not
2513 cache the NV if not needed.
2516 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2517 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2518 == IS_NUMBER_IN_UV) {
2519 /* It's definitely an integer, only upgrade to PVIV */
2520 if (SvTYPE(sv) < SVt_PVIV)
2521 sv_upgrade(sv, SVt_PVIV);
2523 } else if (SvTYPE(sv) < SVt_PVNV)
2524 sv_upgrade(sv, SVt_PVNV);
2526 /* If NV preserves UV then we only use the UV value if we know that
2527 we aren't going to call atof() below. If NVs don't preserve UVs
2528 then the value returned may have more precision than atof() will
2529 return, even though it isn't accurate. */
2530 if ((numtype & (IS_NUMBER_IN_UV
2531 #ifdef NV_PRESERVES_UV
2534 )) == IS_NUMBER_IN_UV) {
2535 /* This won't turn off the public IOK flag if it was set above */
2536 (void)SvIOKp_on(sv);
2538 if (!(numtype & IS_NUMBER_NEG)) {
2540 if (value <= (UV)IV_MAX) {
2541 SvIV_set(sv, (IV)value);
2543 /* it didn't overflow, and it was positive. */
2544 SvUV_set(sv, value);
2548 /* 2s complement assumption */
2549 if (value <= (UV)IV_MIN) {
2550 SvIV_set(sv, -(IV)value);
2552 /* Too negative for an IV. This is a double upgrade, but
2553 I'm assuming it will be rare. */
2554 if (SvTYPE(sv) < SVt_PVNV)
2555 sv_upgrade(sv, SVt_PVNV);
2559 SvNV_set(sv, -(NV)value);
2560 SvIV_set(sv, IV_MIN);
2565 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2566 != IS_NUMBER_IN_UV) {
2567 /* It wasn't an integer, or it overflowed the UV. */
2568 SvNV_set(sv, Atof(SvPVX_const(sv)));
2570 if (! numtype && ckWARN(WARN_NUMERIC))
2573 #if defined(USE_LONG_DOUBLE)
2574 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n",
2575 PTR2UV(sv), SvNVX(sv)));
2577 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"NVgf")\n",
2578 PTR2UV(sv), SvNVX(sv)));
2581 #ifdef NV_PRESERVES_UV
2582 (void)SvIOKp_on(sv);
2584 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2585 SvIV_set(sv, I_V(SvNVX(sv)));
2586 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2589 /* Integer is imprecise. NOK, IOKp */
2591 /* UV will not work better than IV */
2593 if (SvNVX(sv) > (NV)UV_MAX) {
2595 /* Integer is inaccurate. NOK, IOKp, is UV */
2596 SvUV_set(sv, UV_MAX);
2599 SvUV_set(sv, U_V(SvNVX(sv)));
2600 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
2601 NV preservse UV so can do correct comparison. */
2602 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2606 /* Integer is imprecise. NOK, IOKp, is UV */
2611 #else /* NV_PRESERVES_UV */
2612 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2613 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2614 /* The UV slot will have been set from value returned by
2615 grok_number above. The NV slot has just been set using
2618 assert (SvIOKp(sv));
2620 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2621 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2622 /* Small enough to preserve all bits. */
2623 (void)SvIOKp_on(sv);
2625 SvIV_set(sv, I_V(SvNVX(sv)));
2626 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2628 /* Assumption: first non-preserved integer is < IV_MAX,
2629 this NV is in the preserved range, therefore: */
2630 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2632 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);
2635 sv_2iuv_non_preserve (sv, numtype);
2637 #endif /* NV_PRESERVES_UV */
2641 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2642 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2645 if (SvTYPE(sv) < SVt_IV)
2646 /* Typically the caller expects that sv_any is not NULL now. */
2647 sv_upgrade(sv, SVt_IV);
2651 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
2652 PTR2UV(sv),SvUVX(sv)));
2653 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
2659 Return the num value of an SV, doing any necessary string or integer
2660 conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
2667 Perl_sv_2nv(pTHX_ register SV *sv)
2671 if (SvGMAGICAL(sv)) {
2675 if (SvPOKp(sv) && SvLEN(sv)) {
2676 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) &&
2677 !grok_number(SvPVX_const(sv), SvCUR(sv), NULL))
2679 return Atof(SvPVX_const(sv));
2683 return (NV)SvUVX(sv);
2685 return (NV)SvIVX(sv);
2688 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2689 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2695 if (SvTHINKFIRST(sv)) {
2698 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
2699 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
2700 return SvNV(tmpstr);
2701 return PTR2NV(SvRV(sv));
2704 sv_force_normal_flags(sv, 0);
2706 if (SvREADONLY(sv) && !SvOK(sv)) {
2707 if (ckWARN(WARN_UNINITIALIZED))
2712 if (SvTYPE(sv) < SVt_NV) {
2713 if (SvTYPE(sv) == SVt_IV)
2714 sv_upgrade(sv, SVt_PVNV);
2716 sv_upgrade(sv, SVt_NV);
2717 #ifdef USE_LONG_DOUBLE
2719 STORE_NUMERIC_LOCAL_SET_STANDARD();
2720 PerlIO_printf(Perl_debug_log,
2721 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
2722 PTR2UV(sv), SvNVX(sv));
2723 RESTORE_NUMERIC_LOCAL();
2727 STORE_NUMERIC_LOCAL_SET_STANDARD();
2728 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
2729 PTR2UV(sv), SvNVX(sv));
2730 RESTORE_NUMERIC_LOCAL();
2734 else if (SvTYPE(sv) < SVt_PVNV)
2735 sv_upgrade(sv, SVt_PVNV);
2740 SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
2741 #ifdef NV_PRESERVES_UV
2744 /* Only set the public NV OK flag if this NV preserves the IV */
2745 /* Check it's not 0xFFFFFFFFFFFFFFFF */
2746 if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
2747 : (SvIVX(sv) == I_V(SvNVX(sv))))
2753 else if (SvPOKp(sv) && SvLEN(sv)) {
2755 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2756 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !numtype)
2758 #ifdef NV_PRESERVES_UV
2759 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2760 == IS_NUMBER_IN_UV) {
2761 /* It's definitely an integer */
2762 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
2764 SvNV_set(sv, Atof(SvPVX_const(sv)));
2767 SvNV_set(sv, Atof(SvPVX_const(sv)));
2768 /* Only set the public NV OK flag if this NV preserves the value in
2769 the PV at least as well as an IV/UV would.
2770 Not sure how to do this 100% reliably. */
2771 /* if that shift count is out of range then Configure's test is
2772 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2774 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2775 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2776 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
2777 } else if (!(numtype & IS_NUMBER_IN_UV)) {
2778 /* Can't use strtol etc to convert this string, so don't try.
2779 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
2782 /* value has been set. It may not be precise. */
2783 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
2784 /* 2s complement assumption for (UV)IV_MIN */
2785 SvNOK_on(sv); /* Integer is too negative. */
2790 if (numtype & IS_NUMBER_NEG) {
2791 SvIV_set(sv, -(IV)value);
2792 } else if (value <= (UV)IV_MAX) {
2793 SvIV_set(sv, (IV)value);
2795 SvUV_set(sv, value);
2799 if (numtype & IS_NUMBER_NOT_INT) {
2800 /* I believe that even if the original PV had decimals,
2801 they are lost beyond the limit of the FP precision.
2802 However, neither is canonical, so both only get p
2803 flags. NWC, 2000/11/25 */
2804 /* Both already have p flags, so do nothing */
2806 const NV nv = SvNVX(sv);
2807 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2808 if (SvIVX(sv) == I_V(nv)) {
2813 /* It had no "." so it must be integer. */
2816 /* between IV_MAX and NV(UV_MAX).
2817 Could be slightly > UV_MAX */
2819 if (numtype & IS_NUMBER_NOT_INT) {
2820 /* UV and NV both imprecise. */
2822 const UV nv_as_uv = U_V(nv);
2824 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
2835 #endif /* NV_PRESERVES_UV */
2838 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
2840 if (SvTYPE(sv) < SVt_NV)
2841 /* Typically the caller expects that sv_any is not NULL now. */
2842 /* XXX Ilya implies that this is a bug in callers that assume this
2843 and ideally should be fixed. */
2844 sv_upgrade(sv, SVt_NV);
2847 #if defined(USE_LONG_DOUBLE)
2849 STORE_NUMERIC_LOCAL_SET_STANDARD();
2850 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
2851 PTR2UV(sv), SvNVX(sv));
2852 RESTORE_NUMERIC_LOCAL();
2856 STORE_NUMERIC_LOCAL_SET_STANDARD();
2857 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
2858 PTR2UV(sv), SvNVX(sv));
2859 RESTORE_NUMERIC_LOCAL();
2865 /* asIV(): extract an integer from the string value of an SV.
2866 * Caller must validate PVX */
2869 S_asIV(pTHX_ SV *sv)
2872 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2874 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2875 == IS_NUMBER_IN_UV) {
2876 /* It's definitely an integer */
2877 if (numtype & IS_NUMBER_NEG) {
2878 if (value < (UV)IV_MIN)
2881 if (value < (UV)IV_MAX)
2886 if (ckWARN(WARN_NUMERIC))
2889 return I_V(Atof(SvPVX_const(sv)));
2892 /* asUV(): extract an unsigned integer from the string value of an SV
2893 * Caller must validate PVX */
2896 S_asUV(pTHX_ SV *sv)
2899 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2901 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2902 == IS_NUMBER_IN_UV) {
2903 /* It's definitely an integer */
2904 if (!(numtype & IS_NUMBER_NEG))
2908 if (ckWARN(WARN_NUMERIC))
2911 return U_V(Atof(SvPVX_const(sv)));
2915 =for apidoc sv_2pv_nolen
2917 Like C<sv_2pv()>, but doesn't return the length too. You should usually
2918 use the macro wrapper C<SvPV_nolen(sv)> instead.
2923 Perl_sv_2pv_nolen(pTHX_ register SV *sv)
2925 return sv_2pv(sv, 0);
2928 /* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
2929 * UV as a string towards the end of buf, and return pointers to start and
2932 * We assume that buf is at least TYPE_CHARS(UV) long.
2936 uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
2938 char *ptr = buf + TYPE_CHARS(UV);
2952 *--ptr = '0' + (char)(uv % 10);
2960 /* sv_2pv() is now a macro using Perl_sv_2pv_flags();
2961 * this function provided for binary compatibility only
2965 Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
2967 return sv_2pv_flags(sv, lp, SV_GMAGIC);
2971 =for apidoc sv_2pv_flags
2973 Returns a pointer to the string value of an SV, and sets *lp to its length.
2974 If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
2976 Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
2977 usually end up here too.
2983 Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
2988 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
2989 char *tmpbuf = tbuf;
2996 if (SvGMAGICAL(sv)) {
2997 if (flags & SV_GMAGIC)
3002 if (flags & SV_MUTABLE_RETURN)
3003 return SvPVX_mutable(sv);
3004 if (flags & SV_CONST_RETURN)
3005 return (char *)SvPVX_const(sv);
3010 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
3012 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
3017 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
3022 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
3023 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
3031 if (SvTHINKFIRST(sv)) {
3034 register const char *typestr;
3035 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
3036 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
3038 /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr); */
3041 if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) {
3042 if (flags & SV_CONST_RETURN) {
3043 pv = (char *) SvPVX_const(tmpstr);
3045 pv = (flags & SV_MUTABLE_RETURN)
3046 ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr);
3049 *lp = SvCUR(tmpstr);
3051 pv = sv_2pv_flags(tmpstr, lp, flags);
3062 typestr = "NULLREF";
3066 switch (SvTYPE(sv)) {
3068 if ( ((SvFLAGS(sv) &
3069 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
3070 == (SVs_OBJECT|SVs_SMG))
3071 && (mg = mg_find(sv, PERL_MAGIC_qr))) {
3072 const regexp *re = (regexp *)mg->mg_obj;
3075 const char *fptr = "msix";
3080 char need_newline = 0;
3081 U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12);
3083 while((ch = *fptr++)) {
3085 reflags[left++] = ch;
3088 reflags[right--] = ch;
3093 reflags[left] = '-';
3097 mg->mg_len = re->prelen + 4 + left;
3099 * If /x was used, we have to worry about a regex
3100 * ending with a comment later being embedded
3101 * within another regex. If so, we don't want this
3102 * regex's "commentization" to leak out to the
3103 * right part of the enclosing regex, we must cap
3104 * it with a newline.
3106 * So, if /x was used, we scan backwards from the
3107 * end of the regex. If we find a '#' before we
3108 * find a newline, we need to add a newline
3109 * ourself. If we find a '\n' first (or if we
3110 * don't find '#' or '\n'), we don't need to add
3111 * anything. -jfriedl
3113 if (PMf_EXTENDED & re->reganch)
3115 const char *endptr = re->precomp + re->prelen;
3116 while (endptr >= re->precomp)
3118 const char c = *(endptr--);
3120 break; /* don't need another */
3122 /* we end while in a comment, so we
3124 mg->mg_len++; /* save space for it */
3125 need_newline = 1; /* note to add it */
3131 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
3132 Copy("(?", mg->mg_ptr, 2, char);
3133 Copy(reflags, mg->mg_ptr+2, left, char);
3134 Copy(":", mg->mg_ptr+left+2, 1, char);
3135 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
3137 mg->mg_ptr[mg->mg_len - 2] = '\n';
3138 mg->mg_ptr[mg->mg_len - 1] = ')';
3139 mg->mg_ptr[mg->mg_len] = 0;
3141 PL_reginterp_cnt += re->program[0].next_off;
3143 if (re->reganch & ROPT_UTF8)
3159 case SVt_PVBM: typestr = SvROK(sv) ? "REF" : "SCALAR"; break;
3160 case SVt_PVLV: typestr = SvROK(sv) ? "REF"
3161 /* tied lvalues should appear to be
3162 * scalars for backwards compatitbility */
3163 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
3164 ? "SCALAR" : "LVALUE"; break;
3165 case SVt_PVAV: typestr = "ARRAY"; break;
3166 case SVt_PVHV: typestr = "HASH"; break;
3167 case SVt_PVCV: typestr = "CODE"; break;
3168 case SVt_PVGV: typestr = "GLOB"; break;
3169 case SVt_PVFM: typestr = "FORMAT"; break;
3170 case SVt_PVIO: typestr = "IO"; break;
3171 default: typestr = "UNKNOWN"; break;
3175 const char *name = HvNAME_get(SvSTASH(sv));
3176 Perl_sv_setpvf(aTHX_ tsv, "%s=%s(0x%"UVxf")",
3177 name ? name : "__ANON__" , typestr, PTR2UV(sv));
3180 Perl_sv_setpvf(aTHX_ tsv, "%s(0x%"UVxf")", typestr, PTR2UV(sv));
3184 *lp = strlen(typestr);
3185 return (char *)typestr;
3187 if (SvREADONLY(sv) && !SvOK(sv)) {
3188 if (ckWARN(WARN_UNINITIALIZED))
3195 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
3196 /* I'm assuming that if both IV and NV are equally valid then
3197 converting the IV is going to be more efficient */
3198 const U32 isIOK = SvIOK(sv);
3199 const U32 isUIOK = SvIsUV(sv);
3200 char buf[TYPE_CHARS(UV)];
3203 if (SvTYPE(sv) < SVt_PVIV)
3204 sv_upgrade(sv, SVt_PVIV);
3206 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
3208 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
3209 /* inlined from sv_setpvn */
3210 SvGROW_mutable(sv, (STRLEN)(ebuf - ptr + 1));
3211 Move(ptr,SvPVX_mutable(sv),ebuf - ptr,char);
3212 SvCUR_set(sv, ebuf - ptr);
3222 else if (SvNOKp(sv)) {
3223 if (SvTYPE(sv) < SVt_PVNV)
3224 sv_upgrade(sv, SVt_PVNV);
3225 /* The +20 is pure guesswork. Configure test needed. --jhi */
3226 s = SvGROW_mutable(sv, NV_DIG + 20);
3227 olderrno = errno; /* some Xenix systems wipe out errno here */
3229 if (SvNVX(sv) == 0.0)
3230 (void)strcpy(s,"0");
3234 Gconvert(SvNVX(sv), NV_DIG, 0, s);
3237 #ifdef FIXNEGATIVEZERO
3238 if (*s == '-' && s[1] == '0' && !s[2])
3248 if (ckWARN(WARN_UNINITIALIZED)
3249 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
3253 if (SvTYPE(sv) < SVt_PV)
3254 /* Typically the caller expects that sv_any is not NULL now. */
3255 sv_upgrade(sv, SVt_PV);
3259 STRLEN len = s - SvPVX_const(sv);
3265 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3266 PTR2UV(sv),SvPVX_const(sv)));
3267 if (flags & SV_CONST_RETURN)
3268 return (char *)SvPVX_const(sv);
3269 if (flags & SV_MUTABLE_RETURN)
3270 return SvPVX_mutable(sv);
3274 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
3275 /* Sneaky stuff here */
3279 tsv = newSVpv(tmpbuf, 0);
3292 t = SvPVX_const(tsv);
3297 len = strlen(tmpbuf);
3299 #ifdef FIXNEGATIVEZERO
3300 if (len == 2 && t[0] == '-' && t[1] == '0') {
3305 SvUPGRADE(sv, SVt_PV);
3308 s = SvGROW_mutable(sv, len + 1);
3311 return strcpy(s, t);
3316 =for apidoc sv_copypv
3318 Copies a stringified representation of the source SV into the
3319 destination SV. Automatically performs any necessary mg_get and
3320 coercion of numeric values into strings. Guaranteed to preserve
3321 UTF-8 flag even from overloaded objects. Similar in nature to
3322 sv_2pv[_flags] but operates directly on an SV instead of just the
3323 string. Mostly uses sv_2pv_flags to do its work, except when that
3324 would lose the UTF-8'ness of the PV.
3330 Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv)
3333 const char * const s = SvPV_const(ssv,len);
3334 sv_setpvn(dsv,s,len);
3342 =for apidoc sv_2pvbyte_nolen
3344 Return a pointer to the byte-encoded representation of the SV.
3345 May cause the SV to be downgraded from UTF-8 as a side-effect.
3347 Usually accessed via the C<SvPVbyte_nolen> macro.
3353 Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
3355 return sv_2pvbyte(sv, 0);
3359 =for apidoc sv_2pvbyte
3361 Return a pointer to the byte-encoded representation of the SV, and set *lp
3362 to its length. May cause the SV to be downgraded from UTF-8 as a
3365 Usually accessed via the C<SvPVbyte> macro.
3371 Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
3373 sv_utf8_downgrade(sv,0);
3374 return lp ? SvPV(sv,*lp) : SvPV_nolen(sv);
3378 =for apidoc sv_2pvutf8_nolen
3380 Return a pointer to the UTF-8-encoded representation of the SV.
3381 May cause the SV to be upgraded to UTF-8 as a side-effect.
3383 Usually accessed via the C<SvPVutf8_nolen> macro.
3389 Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
3391 return sv_2pvutf8(sv, 0);
3395 =for apidoc sv_2pvutf8
3397 Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
3398 to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
3400 Usually accessed via the C<SvPVutf8> macro.
3406 Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
3408 sv_utf8_upgrade(sv);
3409 return SvPV(sv,*lp);
3413 =for apidoc sv_2bool
3415 This function is only called on magical items, and is only used by
3416 sv_true() or its macro equivalent.
3422 Perl_sv_2bool(pTHX_ register SV *sv)
3431 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
3432 (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
3433 return (bool)SvTRUE(tmpsv);
3434 return SvRV(sv) != 0;
3437 register XPV* const Xpvtmp = (XPV*)SvANY(sv);
3439 (*sv->sv_u.svu_pv > '0' ||
3440 Xpvtmp->xpv_cur > 1 ||
3441 (Xpvtmp->xpv_cur && *sv->sv_u.svu_pv != '0')))
3448 return SvIVX(sv) != 0;
3451 return SvNVX(sv) != 0.0;
3458 /* sv_utf8_upgrade() is now a macro using sv_utf8_upgrade_flags();
3459 * this function provided for binary compatibility only
3464 Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
3466 return sv_utf8_upgrade_flags(sv, SV_GMAGIC);
3470 =for apidoc sv_utf8_upgrade
3472 Converts the PV of an SV to its UTF-8-encoded form.
3473 Forces the SV to string form if it is not already.
3474 Always sets the SvUTF8 flag to avoid future validity checks even
3475 if all the bytes have hibit clear.
3477 This is not as a general purpose byte encoding to Unicode interface:
3478 use the Encode extension for that.
3480 =for apidoc sv_utf8_upgrade_flags
3482 Converts the PV of an SV to its UTF-8-encoded form.
3483 Forces the SV to string form if it is not already.
3484 Always sets the SvUTF8 flag to avoid future validity checks even
3485 if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
3486 will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
3487 C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
3489 This is not as a general purpose byte encoding to Unicode interface:
3490 use the Encode extension for that.
3496 Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags)
3498 if (sv == &PL_sv_undef)
3502 if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
3503 (void) sv_2pv_flags(sv,&len, flags);
3507 (void) SvPV_force(sv,len);
3516 sv_force_normal_flags(sv, 0);
3519 if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING))
3520 sv_recode_to_utf8(sv, PL_encoding);
3521 else { /* Assume Latin-1/EBCDIC */
3522 /* This function could be much more efficient if we
3523 * had a FLAG in SVs to signal if there are any hibit
3524 * chars in the PV. Given that there isn't such a flag
3525 * make the loop as fast as possible. */
3526 const U8 *s = (U8 *) SvPVX_const(sv);
3527 const U8 *e = (U8 *) SvEND(sv);
3533 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
3537 STRLEN len = SvCUR(sv) + 1; /* Plus the \0 */
3538 U8 * const recoded = bytes_to_utf8((U8*)s, &len);
3540 SvPV_free(sv); /* No longer using what was there before. */
3542 SvPV_set(sv, (char*)recoded);
3543 SvCUR_set(sv, len - 1);
3544 SvLEN_set(sv, len); /* No longer know the real size. */
3546 /* Mark as UTF-8 even if no hibit - saves scanning loop */
3553 =for apidoc sv_utf8_downgrade
3555 Attempts to convert the PV of an SV from characters to bytes.
3556 If the PV contains a character beyond byte, this conversion will fail;
3557 in this case, either returns false or, if C<fail_ok> is not
3560 This is not as a general purpose Unicode to byte encoding interface:
3561 use the Encode extension for that.
3567 Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
3569 if (SvPOKp(sv) && SvUTF8(sv)) {
3575 sv_force_normal_flags(sv, 0);
3577 s = (U8 *) SvPV(sv, len);
3578 if (!utf8_to_bytes(s, &len)) {
3583 Perl_croak(aTHX_ "Wide character in %s",
3586 Perl_croak(aTHX_ "Wide character");
3597 =for apidoc sv_utf8_encode
3599 Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
3600 flag off so that it looks like octets again.
3606 Perl_sv_utf8_encode(pTHX_ register SV *sv)
3608 (void) sv_utf8_upgrade(sv);
3610 sv_force_normal_flags(sv, 0);
3612 if (SvREADONLY(sv)) {
3613 Perl_croak(aTHX_ PL_no_modify);
3619 =for apidoc sv_utf8_decode
3621 If the PV of the SV is an octet sequence in UTF-8
3622 and contains a multiple-byte character, the C<SvUTF8> flag is turned on
3623 so that it looks like a character. If the PV contains only single-byte
3624 characters, the C<SvUTF8> flag stays being off.
3625 Scans PV for validity and returns false if the PV is invalid UTF-8.
3631 Perl_sv_utf8_decode(pTHX_ register SV *sv)
3637 /* The octets may have got themselves encoded - get them back as
3640 if (!sv_utf8_downgrade(sv, TRUE))
3643 /* it is actually just a matter of turning the utf8 flag on, but
3644 * we want to make sure everything inside is valid utf8 first.
3646 c = (const U8 *) SvPVX_const(sv);
3647 if (!is_utf8_string(c, SvCUR(sv)+1))
3649 e = (const U8 *) SvEND(sv);
3652 if (!UTF8_IS_INVARIANT(ch)) {
3661 /* sv_setsv() is now a macro using Perl_sv_setsv_flags();
3662 * this function provided for binary compatibility only
3666 Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
3668 sv_setsv_flags(dstr, sstr, SV_GMAGIC);
3672 =for apidoc sv_setsv
3674 Copies the contents of the source SV C<ssv> into the destination SV
3675 C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3676 function if the source SV needs to be reused. Does not handle 'set' magic.
3677 Loosely speaking, it performs a copy-by-value, obliterating any previous
3678 content of the destination.
3680 You probably want to use one of the assortment of wrappers, such as
3681 C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3682 C<SvSetMagicSV_nosteal>.
3684 =for apidoc sv_setsv_flags
3686 Copies the contents of the source SV C<ssv> into the destination SV
3687 C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3688 function if the source SV needs to be reused. Does not handle 'set' magic.
3689 Loosely speaking, it performs a copy-by-value, obliterating any previous
3690 content of the destination.
3691 If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
3692 C<ssv> if appropriate, else not. If the C<flags> parameter has the
3693 C<NOSTEAL> bit set then the buffers of temps will not be stolen. <sv_setsv>
3694 and C<sv_setsv_nomg> are implemented in terms of this function.
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 This is the primary function for copying scalars, and most other
3701 copy-ish functions and macros use this underneath.
3707 Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags)
3709 register U32 sflags;
3715 SV_CHECK_THINKFIRST_COW_DROP(dstr);
3717 sstr = &PL_sv_undef;
3718 stype = SvTYPE(sstr);
3719 dtype = SvTYPE(dstr);
3724 /* need to nuke the magic */
3726 SvRMAGICAL_off(dstr);
3729 /* There's a lot of redundancy below but we're going for speed here */
3734 if (dtype != SVt_PVGV) {
3735 (void)SvOK_off(dstr);
3743 sv_upgrade(dstr, SVt_IV);
3746 sv_upgrade(dstr, SVt_PVNV);
3750 sv_upgrade(dstr, SVt_PVIV);
3753 (void)SvIOK_only(dstr);
3754 SvIV_set(dstr, SvIVX(sstr));
3757 if (SvTAINTED(sstr))
3768 sv_upgrade(dstr, SVt_NV);
3773 sv_upgrade(dstr, SVt_PVNV);
3776 SvNV_set(dstr, SvNVX(sstr));
3777 (void)SvNOK_only(dstr);
3778 if (SvTAINTED(sstr))
3786 sv_upgrade(dstr, SVt_RV);
3787 else if (dtype == SVt_PVGV &&
3788 SvROK(sstr) && SvTYPE(SvRV(sstr)) == SVt_PVGV) {
3791 if (GvIMPORTED(dstr) != GVf_IMPORTED
3792 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3794 GvIMPORTED_on(dstr);
3803 #ifdef PERL_OLD_COPY_ON_WRITE
3804 if ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS) {
3805 if (dtype < SVt_PVIV)
3806 sv_upgrade(dstr, SVt_PVIV);
3813 sv_upgrade(dstr, SVt_PV);
3816 if (dtype < SVt_PVIV)
3817 sv_upgrade(dstr, SVt_PVIV);
3820 if (dtype < SVt_PVNV)
3821 sv_upgrade(dstr, SVt_PVNV);
3828 const char * const type = sv_reftype(sstr,0);
3830 Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_NAME(PL_op));
3832 Perl_croak(aTHX_ "Bizarre copy of %s", type);
3837 if (dtype <= SVt_PVGV) {
3839 if (dtype != SVt_PVGV) {
3840 const char * const name = GvNAME(sstr);
3841 const STRLEN len = GvNAMELEN(sstr);
3842 /* don't upgrade SVt_PVLV: it can hold a glob */
3843 if (dtype != SVt_PVLV)
3844 sv_upgrade(dstr, SVt_PVGV);
3845 sv_magic(dstr, dstr, PERL_MAGIC_glob, Nullch, 0);
3846 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
3847 GvNAME(dstr) = savepvn(name, len);
3848 GvNAMELEN(dstr) = len;
3849 SvFAKE_on(dstr); /* can coerce to non-glob */
3851 /* ahem, death to those who redefine active sort subs */
3852 else if (PL_curstackinfo->si_type == PERLSI_SORT
3853 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
3854 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
3857 #ifdef GV_UNIQUE_CHECK
3858 if (GvUNIQUE((GV*)dstr)) {
3859 Perl_croak(aTHX_ PL_no_modify);
3863 (void)SvOK_off(dstr);
3864 GvINTRO_off(dstr); /* one-shot flag */
3866 GvGP(dstr) = gp_ref(GvGP(sstr));
3867 if (SvTAINTED(sstr))
3869 if (GvIMPORTED(dstr) != GVf_IMPORTED
3870 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3872 GvIMPORTED_on(dstr);
3880 if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
3882 if ((int)SvTYPE(sstr) != stype) {
3883 stype = SvTYPE(sstr);
3884 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
3888 if (stype == SVt_PVLV)
3889 SvUPGRADE(dstr, SVt_PVNV);
3891 SvUPGRADE(dstr, (U32)stype);
3894 sflags = SvFLAGS(sstr);
3896 if (sflags & SVf_ROK) {
3897 if (dtype >= SVt_PV) {
3898 if (dtype == SVt_PVGV) {
3899 SV *sref = SvREFCNT_inc(SvRV(sstr));
3901 const int intro = GvINTRO(dstr);
3903 #ifdef GV_UNIQUE_CHECK
3904 if (GvUNIQUE((GV*)dstr)) {
3905 Perl_croak(aTHX_ PL_no_modify);
3910 GvINTRO_off(dstr); /* one-shot flag */
3911 GvLINE(dstr) = CopLINE(PL_curcop);
3912 GvEGV(dstr) = (GV*)dstr;
3915 switch (SvTYPE(sref)) {
3918 SAVEGENERICSV(GvAV(dstr));
3920 dref = (SV*)GvAV(dstr);
3921 GvAV(dstr) = (AV*)sref;
3922 if (!GvIMPORTED_AV(dstr)
3923 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3925 GvIMPORTED_AV_on(dstr);
3930 SAVEGENERICSV(GvHV(dstr));
3932 dref = (SV*)GvHV(dstr);
3933 GvHV(dstr) = (HV*)sref;
3934 if (!GvIMPORTED_HV(dstr)
3935 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3937 GvIMPORTED_HV_on(dstr);
3942 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
3943 SvREFCNT_dec(GvCV(dstr));
3944 GvCV(dstr) = Nullcv;
3945 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3946 PL_sub_generation++;
3948 SAVEGENERICSV(GvCV(dstr));
3951 dref = (SV*)GvCV(dstr);
3952 if (GvCV(dstr) != (CV*)sref) {
3953 CV* cv = GvCV(dstr);
3955 if (!GvCVGEN((GV*)dstr) &&
3956 (CvROOT(cv) || CvXSUB(cv)))
3958 /* ahem, death to those who redefine
3959 * active sort subs */
3960 if (PL_curstackinfo->si_type == PERLSI_SORT &&
3961 PL_sortcop == CvSTART(cv))
3963 "Can't redefine active sort subroutine %s",
3964 GvENAME((GV*)dstr));
3965 /* Redefining a sub - warning is mandatory if
3966 it was a const and its value changed. */
3967 if (ckWARN(WARN_REDEFINE)
3969 && (!CvCONST((CV*)sref)
3970 || sv_cmp(cv_const_sv(cv),
3971 cv_const_sv((CV*)sref)))))
3973 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
3975 ? "Constant subroutine %s::%s redefined"
3976 : "Subroutine %s::%s redefined",
3977 HvNAME_get(GvSTASH((GV*)dstr)),
3978 GvENAME((GV*)dstr));
3982 cv_ckproto(cv, (GV*)dstr,
3984 ? SvPVX_const(sref) : Nullch);
3986 GvCV(dstr) = (CV*)sref;
3987 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3988 GvASSUMECV_on(dstr);
3989 PL_sub_generation++;
3991 if (!GvIMPORTED_CV(dstr)
3992 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3994 GvIMPORTED_CV_on(dstr);
3999 SAVEGENERICSV(GvIOp(dstr));
4001 dref = (SV*)GvIOp(dstr);
4002 GvIOp(dstr) = (IO*)sref;
4006 SAVEGENERICSV(GvFORM(dstr));
4008 dref = (SV*)GvFORM(dstr);
4009 GvFORM(dstr) = (CV*)sref;
4013 SAVEGENERICSV(GvSV(dstr));
4015 dref = (SV*)GvSV(dstr);
4017 if (!GvIMPORTED_SV(dstr)
4018 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4020 GvIMPORTED_SV_on(dstr);
4026 if (SvTAINTED(sstr))
4030 if (SvPVX_const(dstr)) {
4036 (void)SvOK_off(dstr);
4037 SvRV_set(dstr, SvREFCNT_inc(SvRV(sstr)));
4039 if (sflags & SVp_NOK) {
4041 /* Only set the public OK flag if the source has public OK. */
4042 if (sflags & SVf_NOK)
4043 SvFLAGS(dstr) |= SVf_NOK;
4044 SvNV_set(dstr, SvNVX(sstr));
4046 if (sflags & SVp_IOK) {
4047 (void)SvIOKp_on(dstr);
4048 if (sflags & SVf_IOK)
4049 SvFLAGS(dstr) |= SVf_IOK;
4050 if (sflags & SVf_IVisUV)
4052 SvIV_set(dstr, SvIVX(sstr));
4054 if (SvAMAGIC(sstr)) {
4058 else if (sflags & SVp_POK) {
4062 * Check to see if we can just swipe the string. If so, it's a
4063 * possible small lose on short strings, but a big win on long ones.
4064 * It might even be a win on short strings if SvPVX_const(dstr)
4065 * has to be allocated and SvPVX_const(sstr) has to be freed.
4068 /* Whichever path we take through the next code, we want this true,
4069 and doing it now facilitates the COW check. */
4070 (void)SvPOK_only(dstr);
4073 /* We're not already COW */
4074 ((sflags & (SVf_FAKE | SVf_READONLY)) != (SVf_FAKE | SVf_READONLY)
4075 #ifndef PERL_OLD_COPY_ON_WRITE
4076 /* or we are, but dstr isn't a suitable target. */
4077 || (SvFLAGS(dstr) & CAN_COW_MASK) != CAN_COW_FLAGS
4082 (sflags & SVs_TEMP) && /* slated for free anyway? */
4083 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
4084 (!(flags & SV_NOSTEAL)) &&
4085 /* and we're allowed to steal temps */
4086 SvREFCNT(sstr) == 1 && /* and no other references to it? */
4087 SvLEN(sstr) && /* and really is a string */
4088 /* and won't be needed again, potentially */
4089 !(PL_op && PL_op->op_type == OP_AASSIGN))
4090 #ifdef PERL_OLD_COPY_ON_WRITE
4091 && !((sflags & CAN_COW_MASK) == CAN_COW_FLAGS
4092 && (SvFLAGS(dstr) & CAN_COW_MASK) == CAN_COW_FLAGS
4093 && SvTYPE(sstr) >= SVt_PVIV)
4096 /* Failed the swipe test, and it's not a shared hash key either.
4097 Have to copy the string. */
4098 STRLEN len = SvCUR(sstr);
4099 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
4100 Move(SvPVX_const(sstr),SvPVX(dstr),len,char);
4101 SvCUR_set(dstr, len);
4102 *SvEND(dstr) = '\0';
4104 /* If PERL_OLD_COPY_ON_WRITE is not defined, then isSwipe will always
4106 /* Either it's a shared hash key, or it's suitable for
4107 copy-on-write or we can swipe the string. */
4109 PerlIO_printf(Perl_debug_log, "Copy on write: sstr --> dstr\n");
4113 #ifdef PERL_OLD_COPY_ON_WRITE
4115 /* I believe I should acquire a global SV mutex if
4116 it's a COW sv (not a shared hash key) to stop
4117 it going un copy-on-write.
4118 If the source SV has gone un copy on write between up there
4119 and down here, then (assert() that) it is of the correct
4120 form to make it copy on write again */
4121 if ((sflags & (SVf_FAKE | SVf_READONLY))
4122 != (SVf_FAKE | SVf_READONLY)) {
4123 SvREADONLY_on(sstr);
4125 /* Make the source SV into a loop of 1.
4126 (about to become 2) */
4127 SV_COW_NEXT_SV_SET(sstr, sstr);
4131 /* Initial code is common. */
4132 if (SvPVX_const(dstr)) { /* we know that dtype >= SVt_PV */
4134 SvFLAGS(dstr) &= ~SVf_OOK;
4135 Safefree(SvPVX_const(dstr) - SvIVX(dstr));
4137 else if (SvLEN(dstr))
4138 Safefree(SvPVX_const(dstr));
4142 /* making another shared SV. */
4143 STRLEN cur = SvCUR(sstr);
4144 STRLEN len = SvLEN(sstr);
4145 #ifdef PERL_OLD_COPY_ON_WRITE
4147 assert (SvTYPE(dstr) >= SVt_PVIV);
4148 /* SvIsCOW_normal */
4149 /* splice us in between source and next-after-source. */
4150 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4151 SV_COW_NEXT_SV_SET(sstr, dstr);
4152 SvPV_set(dstr, SvPVX_mutable(sstr));
4156 /* SvIsCOW_shared_hash */
4157 DEBUG_C(PerlIO_printf(Perl_debug_log,
4158 "Copy on write: Sharing hash\n"));
4160 assert (SvTYPE(dstr) >= SVt_PV);
4162 HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr)))));
4164 SvLEN_set(dstr, len);
4165 SvCUR_set(dstr, cur);
4166 SvREADONLY_on(dstr);
4168 /* Relesase a global SV mutex. */
4171 { /* Passes the swipe test. */
4172 SvPV_set(dstr, SvPVX_mutable(sstr));
4173 SvLEN_set(dstr, SvLEN(sstr));
4174 SvCUR_set(dstr, SvCUR(sstr));
4177 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
4178 SvPV_set(sstr, Nullch);
4184 if (sflags & SVf_UTF8)
4186 if (sflags & SVp_NOK) {
4188 if (sflags & SVf_NOK)
4189 SvFLAGS(dstr) |= SVf_NOK;
4190 SvNV_set(dstr, SvNVX(sstr));
4192 if (sflags & SVp_IOK) {
4193 (void)SvIOKp_on(dstr);
4194 if (sflags & SVf_IOK)
4195 SvFLAGS(dstr) |= SVf_IOK;
4196 if (sflags & SVf_IVisUV)
4198 SvIV_set(dstr, SvIVX(sstr));
4201 MAGIC *smg = mg_find(sstr,PERL_MAGIC_vstring);
4202 sv_magic(dstr, NULL, PERL_MAGIC_vstring,
4203 smg->mg_ptr, smg->mg_len);
4204 SvRMAGICAL_on(dstr);
4207 else if (sflags & SVp_IOK) {
4208 if (sflags & SVf_IOK)
4209 (void)SvIOK_only(dstr);
4211 (void)SvOK_off(dstr);
4212 (void)SvIOKp_on(dstr);
4214 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
4215 if (sflags & SVf_IVisUV)
4217 SvIV_set(dstr, SvIVX(sstr));
4218 if (sflags & SVp_NOK) {
4219 if (sflags & SVf_NOK)
4220 (void)SvNOK_on(dstr);
4222 (void)SvNOKp_on(dstr);
4223 SvNV_set(dstr, SvNVX(sstr));
4226 else if (sflags & SVp_NOK) {
4227 if (sflags & SVf_NOK)
4228 (void)SvNOK_only(dstr);
4230 (void)SvOK_off(dstr);
4233 SvNV_set(dstr, SvNVX(sstr));
4236 if (dtype == SVt_PVGV) {
4237 if (ckWARN(WARN_MISC))
4238 Perl_warner(aTHX_ packWARN(WARN_MISC), "Undefined value assigned to typeglob");
4241 (void)SvOK_off(dstr);
4243 if (SvTAINTED(sstr))
4248 =for apidoc sv_setsv_mg
4250 Like C<sv_setsv>, but also handles 'set' magic.
4256 Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
4258 sv_setsv(dstr,sstr);
4262 #ifdef PERL_OLD_COPY_ON_WRITE
4264 Perl_sv_setsv_cow(pTHX_ SV *dstr, SV *sstr)
4266 STRLEN cur = SvCUR(sstr);
4267 STRLEN len = SvLEN(sstr);
4268 register char *new_pv;
4271 PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n",
4279 if (SvTHINKFIRST(dstr))
4280 sv_force_normal_flags(dstr, SV_COW_DROP_PV);
4281 else if (SvPVX_const(dstr))
4282 Safefree(SvPVX_const(dstr));
4286 SvUPGRADE(dstr, SVt_PVIV);
4288 assert (SvPOK(sstr));
4289 assert (SvPOKp(sstr));
4290 assert (!SvIOK(sstr));
4291 assert (!SvIOKp(sstr));
4292 assert (!SvNOK(sstr));
4293 assert (!SvNOKp(sstr));
4295 if (SvIsCOW(sstr)) {
4297 if (SvLEN(sstr) == 0) {
4298 /* source is a COW shared hash key. */
4299 DEBUG_C(PerlIO_printf(Perl_debug_log,
4300 "Fast copy on write: Sharing hash\n"));
4301 new_pv = HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr))));
4304 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4306 assert ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS);
4307 SvUPGRADE(sstr, SVt_PVIV);
4308 SvREADONLY_on(sstr);
4310 DEBUG_C(PerlIO_printf(Perl_debug_log,
4311 "Fast copy on write: Converting sstr to COW\n"));
4312 SV_COW_NEXT_SV_SET(dstr, sstr);
4314 SV_COW_NEXT_SV_SET(sstr, dstr);
4315 new_pv = SvPVX_mutable(sstr);
4318 SvPV_set(dstr, new_pv);
4319 SvFLAGS(dstr) = (SVt_PVIV|SVf_POK|SVp_POK|SVf_FAKE|SVf_READONLY);
4322 SvLEN_set(dstr, len);
4323 SvCUR_set(dstr, cur);
4332 =for apidoc sv_setpvn
4334 Copies a string into an SV. The C<len> parameter indicates the number of
4335 bytes to be copied. If the C<ptr> argument is NULL the SV will become
4336 undefined. Does not handle 'set' magic. See C<sv_setpvn_mg>.
4342 Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
4344 register char *dptr;
4346 SV_CHECK_THINKFIRST_COW_DROP(sv);
4352 /* len is STRLEN which is unsigned, need to copy to signed */
4355 Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen");
4357 SvUPGRADE(sv, SVt_PV);
4359 dptr = SvGROW(sv, len + 1);
4360 Move(ptr,dptr,len,char);
4363 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4368 =for apidoc sv_setpvn_mg
4370 Like C<sv_setpvn>, but also handles 'set' magic.
4376 Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
4378 sv_setpvn(sv,ptr,len);
4383 =for apidoc sv_setpv
4385 Copies a string into an SV. The string must be null-terminated. Does not
4386 handle 'set' magic. See C<sv_setpv_mg>.
4392 Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
4394 register STRLEN len;
4396 SV_CHECK_THINKFIRST_COW_DROP(sv);
4402 SvUPGRADE(sv, SVt_PV);
4404 SvGROW(sv, len + 1);
4405 Move(ptr,SvPVX(sv),len+1,char);
4407 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4412 =for apidoc sv_setpv_mg
4414 Like C<sv_setpv>, but also handles 'set' magic.
4420 Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
4427 =for apidoc sv_usepvn
4429 Tells an SV to use C<ptr> to find its string value. Normally the string is
4430 stored inside the SV but sv_usepvn allows the SV to use an outside string.
4431 The C<ptr> should point to memory that was allocated by C<malloc>. The
4432 string length, C<len>, must be supplied. This function will realloc the
4433 memory pointed to by C<ptr>, so that pointer should not be freed or used by
4434 the programmer after giving it to sv_usepvn. Does not handle 'set' magic.
4435 See C<sv_usepvn_mg>.
4441 Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
4444 SV_CHECK_THINKFIRST_COW_DROP(sv);
4445 SvUPGRADE(sv, SVt_PV);
4450 if (SvPVX_const(sv))
4453 allocate = PERL_STRLEN_ROUNDUP(len + 1);
4454 ptr = saferealloc (ptr, allocate);
4457 SvLEN_set(sv, allocate);
4459 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4464 =for apidoc sv_usepvn_mg
4466 Like C<sv_usepvn>, but also handles 'set' magic.
4472 Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
4474 sv_usepvn(sv,ptr,len);
4478 #ifdef PERL_OLD_COPY_ON_WRITE
4479 /* Need to do this *after* making the SV normal, as we need the buffer
4480 pointer to remain valid until after we've copied it. If we let go too early,
4481 another thread could invalidate it by unsharing last of the same hash key
4482 (which it can do by means other than releasing copy-on-write Svs)
4483 or by changing the other copy-on-write SVs in the loop. */
4485 S_sv_release_COW(pTHX_ register SV *sv, const char *pvx, STRLEN len, SV *after)
4487 if (len) { /* this SV was SvIsCOW_normal(sv) */
4488 /* we need to find the SV pointing to us. */
4489 SV *current = SV_COW_NEXT_SV(after);
4491 if (current == sv) {
4492 /* The SV we point to points back to us (there were only two of us
4494 Hence other SV is no longer copy on write either. */
4496 SvREADONLY_off(after);
4498 /* We need to follow the pointers around the loop. */
4500 while ((next = SV_COW_NEXT_SV(current)) != sv) {
4503 /* don't loop forever if the structure is bust, and we have
4504 a pointer into a closed loop. */
4505 assert (current != after);
4506 assert (SvPVX_const(current) == pvx);
4508 /* Make the SV before us point to the SV after us. */
4509 SV_COW_NEXT_SV_SET(current, after);
4512 unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
4517 Perl_sv_release_IVX(pTHX_ register SV *sv)
4520 sv_force_normal_flags(sv, 0);
4526 =for apidoc sv_force_normal_flags
4528 Undo various types of fakery on an SV: if the PV is a shared string, make
4529 a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
4530 an xpvmg; if we're a copy-on-write scalar, this is the on-write time when
4531 we do the copy, and is also used locally. If C<SV_COW_DROP_PV> is set
4532 then a copy-on-write scalar drops its PV buffer (if any) and becomes
4533 SvPOK_off rather than making a copy. (Used where this scalar is about to be
4534 set to some other value.) In addition, the C<flags> parameter gets passed to
4535 C<sv_unref_flags()> when unrefing. C<sv_force_normal> calls this function
4536 with flags set to 0.
4542 Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags)
4544 #ifdef PERL_OLD_COPY_ON_WRITE
4545 if (SvREADONLY(sv)) {
4546 /* At this point I believe I should acquire a global SV mutex. */
4548 const char *pvx = SvPVX_const(sv);
4549 const STRLEN len = SvLEN(sv);
4550 const STRLEN cur = SvCUR(sv);
4551 SV * const next = SV_COW_NEXT_SV(sv); /* next COW sv in the loop. */
4553 PerlIO_printf(Perl_debug_log,
4554 "Copy on write: Force normal %ld\n",
4560 /* This SV doesn't own the buffer, so need to New() a new one: */
4561 SvPV_set(sv, (char*)0);
4563 if (flags & SV_COW_DROP_PV) {
4564 /* OK, so we don't need to copy our buffer. */
4567 SvGROW(sv, cur + 1);
4568 Move(pvx,SvPVX(sv),cur,char);
4572 sv_release_COW(sv, pvx, len, next);
4577 else if (IN_PERL_RUNTIME)
4578 Perl_croak(aTHX_ PL_no_modify);
4579 /* At this point I believe that I can drop the global SV mutex. */
4582 if (SvREADONLY(sv)) {
4584 const char *pvx = SvPVX_const(sv);
4585 const STRLEN len = SvCUR(sv);
4588 SvPV_set(sv, Nullch);
4590 SvGROW(sv, len + 1);
4591 Move(pvx,SvPVX_const(sv),len,char);
4593 unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
4595 else if (IN_PERL_RUNTIME)
4596 Perl_croak(aTHX_ PL_no_modify);
4600 sv_unref_flags(sv, flags);
4601 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
4606 =for apidoc sv_force_normal
4608 Undo various types of fakery on an SV: if the PV is a shared string, make
4609 a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
4610 an xpvmg. See also C<sv_force_normal_flags>.
4616 Perl_sv_force_normal(pTHX_ register SV *sv)
4618 sv_force_normal_flags(sv, 0);
4624 Efficient removal of characters from the beginning of the string buffer.
4625 SvPOK(sv) must be true and