3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
9 * "I wonder what the Entish is for 'yes' and 'no'," he thought.
12 * This file contains the code that creates, manipulates and destroys
13 * scalar values (SVs). The other types (AV, HV, GV, etc.) reuse the
14 * structure of an SV, so their creation and destruction is handled
15 * here; higher-level functions are in av.c, hv.c, and so on. Opcode
16 * level functions (eg. substr, split, join) for each of the types are
28 /* Missing proto on LynxOS */
29 char *gconvert(double, int, int, char *);
32 #ifdef PERL_UTF8_CACHE_ASSERT
33 /* The cache element 0 is the Unicode offset;
34 * the cache element 1 is the byte offset of the element 0;
35 * the cache element 2 is the Unicode length of the substring;
36 * the cache element 3 is the byte length of the substring;
37 * The checking of the substring side would be good
38 * but substr() has enough code paths to make my head spin;
39 * if adding more checks watch out for the following tests:
40 * t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t
41 * lib/utf8.t lib/Unicode/Collate/t/index.t
44 #define ASSERT_UTF8_CACHE(cache) \
45 STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); } } STMT_END
47 #define ASSERT_UTF8_CACHE(cache) NOOP
50 #ifdef PERL_COPY_ON_WRITE
51 #define SV_COW_NEXT_SV(sv) INT2PTR(SV *,SvUVX(sv))
52 #define SV_COW_NEXT_SV_SET(current,next) SvUV_set(current, PTR2UV(next))
53 /* This is a pessimistic view. Scalar must be purely a read-write PV to copy-
57 /* ============================================================================
59 =head1 Allocation and deallocation of SVs.
61 An SV (or AV, HV, etc.) is allocated in two parts: the head (struct sv,
62 av, hv...) contains type and reference count information, as well as a
63 pointer to the body (struct xrv, xpv, xpviv...), which contains fields
64 specific to each type.
66 Normally, this allocation is done using arenas, which by default are
67 approximately 4K chunks of memory parcelled up into N heads or bodies. The
68 first slot in each arena is reserved, and is used to hold a link to the next
69 arena. In the case of heads, the unused first slot also contains some flags
70 and a note of the number of slots. Snaked through each arena chain is a
71 linked list of free items; when this becomes empty, an extra arena is
72 allocated and divided up into N items which are threaded into the free list.
74 The following global variables are associated with arenas:
76 PL_sv_arenaroot pointer to list of SV arenas
77 PL_sv_root pointer to list of free SV structures
79 PL_foo_arenaroot pointer to list of foo arenas,
80 PL_foo_root pointer to list of free foo bodies
81 ... for foo in xiv, xnv, xrv, xpv etc.
83 Note that some of the larger and more rarely used body types (eg xpvio)
84 are not allocated using arenas, but are instead just malloc()/free()ed as
85 required. Also, if PURIFY is defined, arenas are abandoned altogether,
86 with all items individually malloc()ed. In addition, a few SV heads are
87 not allocated from an arena, but are instead directly created as static
88 or auto variables, eg PL_sv_undef. The size of arenas can be changed from
89 the default by setting PERL_ARENA_SIZE appropriately at compile time.
91 The SV arena serves the secondary purpose of allowing still-live SVs
92 to be located and destroyed during final cleanup.
94 At the lowest level, the macros new_SV() and del_SV() grab and free
95 an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
96 to return the SV to the free list with error checking.) new_SV() calls
97 more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
98 SVs in the free list have their SvTYPE field set to all ones.
100 Similarly, there are macros new_XIV()/del_XIV(), new_XNV()/del_XNV() etc
101 that allocate and return individual body types. Normally these are mapped
102 to the arena-manipulating functions new_xiv()/del_xiv() etc, but may be
103 instead mapped directly to malloc()/free() if PURIFY is defined. The
104 new/del functions remove from, or add to, the appropriate PL_foo_root
105 list, and call more_xiv() etc to add a new arena if the list is empty.
107 At the time of very final cleanup, sv_free_arenas() is called from
108 perl_destruct() to physically free all the arenas allocated since the
109 start of the interpreter. Note that this also clears PL_he_arenaroot,
110 which is otherwise dealt with in hv.c.
112 Manipulation of any of the PL_*root pointers is protected by enclosing
113 LOCK_SV_MUTEX; ... UNLOCK_SV_MUTEX calls which should Do the Right Thing
114 if threads are enabled.
116 The function visit() scans the SV arenas list, and calls a specified
117 function for each SV it finds which is still live - ie which has an SvTYPE
118 other than all 1's, and a non-zero SvREFCNT. visit() is used by the
119 following functions (specified as [function that calls visit()] / [function
120 called by visit() for each SV]):
122 sv_report_used() / do_report_used()
123 dump all remaining SVs (debugging aid)
125 sv_clean_objs() / do_clean_objs(),do_clean_named_objs()
126 Attempt to free all objects pointed to by RVs,
127 and, unless DISABLE_DESTRUCTOR_KLUDGE is defined,
128 try to do the same for all objects indirectly
129 referenced by typeglobs too. Called once from
130 perl_destruct(), prior to calling sv_clean_all()
133 sv_clean_all() / do_clean_all()
134 SvREFCNT_dec(sv) each remaining SV, possibly
135 triggering an sv_free(). It also sets the
136 SVf_BREAK flag on the SV to indicate that the
137 refcnt has been artificially lowered, and thus
138 stopping sv_free() from giving spurious warnings
139 about SVs which unexpectedly have a refcnt
140 of zero. called repeatedly from perl_destruct()
141 until there are no SVs left.
145 Private API to rest of sv.c
149 new_XIV(), del_XIV(),
150 new_XNV(), del_XNV(),
155 sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
160 ============================================================================ */
165 * "A time to plant, and a time to uproot what was planted..."
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)) {
290 SV *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 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 =for apidoc sv_free_arenas
515 Deallocate the memory used by all arenas. Note that all the individual SV
516 heads and bodies within the arenas must already have been freed.
522 Perl_sv_free_arenas(pTHX)
526 void *arena, *arenanext;
528 /* Free arenas here, but be careful about fake ones. (We assume
529 contiguity of the fake ones with the corresponding real ones.) */
531 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
532 svanext = (SV*) SvANY(sva);
533 while (svanext && SvFAKE(svanext))
534 svanext = (SV*) SvANY(svanext);
537 Safefree((void *)sva);
540 for (arena = PL_xnv_arenaroot; arena; arena = arenanext) {
541 arenanext = *(void **)arena;
544 PL_xnv_arenaroot = 0;
547 for (arena = PL_xpv_arenaroot; arena; arena = arenanext) {
548 arenanext = *(void **)arena;
551 PL_xpv_arenaroot = 0;
554 for (arena = PL_xpviv_arenaroot; arena; arena = arenanext) {
555 arenanext = *(void **)arena;
558 PL_xpviv_arenaroot = 0;
561 for (arena = PL_xpvnv_arenaroot; arena; arena = arenanext) {
562 arenanext = *(void **)arena;
565 PL_xpvnv_arenaroot = 0;
568 for (arena = PL_xpvcv_arenaroot; arena; arena = arenanext) {
569 arenanext = *(void **)arena;
572 PL_xpvcv_arenaroot = 0;
575 for (arena = PL_xpvav_arenaroot; arena; arena = arenanext) {
576 arenanext = *(void **)arena;
579 PL_xpvav_arenaroot = 0;
582 for (arena = PL_xpvhv_arenaroot; arena; arena = arenanext) {
583 arenanext = *(void **)arena;
586 PL_xpvhv_arenaroot = 0;
589 for (arena = PL_xpvmg_arenaroot; arena; arena = arenanext) {
590 arenanext = *(void **)arena;
593 PL_xpvmg_arenaroot = 0;
596 for (arena = PL_xpvgv_arenaroot; arena; arena = arenanext) {
597 arenanext = *(void **)arena;
600 PL_xpvgv_arenaroot = 0;
603 for (arena = PL_xpvlv_arenaroot; arena; arena = arenanext) {
604 arenanext = *(void **)arena;
607 PL_xpvlv_arenaroot = 0;
610 for (arena = PL_xpvbm_arenaroot; arena; arena = arenanext) {
611 arenanext = *(void **)arena;
614 PL_xpvbm_arenaroot = 0;
620 for (he = PL_he_arenaroot; he; he = he_next) {
621 he_next = HeNEXT(he);
628 #if defined(USE_ITHREADS)
630 struct ptr_tbl_ent *pte;
631 struct ptr_tbl_ent *pte_next;
632 for (pte = PL_pte_arenaroot; pte; pte = pte_next) {
633 pte_next = pte->next;
637 PL_pte_arenaroot = 0;
642 Safefree(PL_nice_chunk);
643 PL_nice_chunk = Nullch;
644 PL_nice_chunk_size = 0;
649 /* ---------------------------------------------------------------------
651 * support functions for report_uninit()
654 /* the maxiumum size of array or hash where we will scan looking
655 * for the undefined element that triggered the warning */
657 #define FUV_MAX_SEARCH_SIZE 1000
659 /* Look for an entry in the hash whose value has the same SV as val;
660 * If so, return a mortal copy of the key. */
663 S_find_hash_subscript(pTHX_ HV *hv, SV* val)
669 if (!hv || SvMAGICAL(hv) || !HvARRAY(hv) ||
670 (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
675 for (i=HvMAX(hv); i>0; i--) {
677 for (entry = array[i]; entry; entry = HeNEXT(entry)) {
678 if (HeVAL(entry) != val)
680 if ( HeVAL(entry) == &PL_sv_undef ||
681 HeVAL(entry) == &PL_sv_placeholder)
685 if (HeKLEN(entry) == HEf_SVKEY)
686 return sv_mortalcopy(HeKEY_sv(entry));
687 return sv_2mortal(newSVpvn(HeKEY(entry), HeKLEN(entry)));
693 /* Look for an entry in the array whose value has the same SV as val;
694 * If so, return the index, otherwise return -1. */
697 S_find_array_subscript(pTHX_ AV *av, SV* val)
701 if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
702 (AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
706 for (i=AvFILLp(av); i>=0; i--) {
707 if (svp[i] == val && svp[i] != &PL_sv_undef)
713 /* S_varname(): return the name of a variable, optionally with a subscript.
714 * If gv is non-zero, use the name of that global, along with gvtype (one
715 * of "$", "@", "%"); otherwise use the name of the lexical at pad offset
716 * targ. Depending on the value of the subscript_type flag, return:
719 #define FUV_SUBSCRIPT_NONE 1 /* "@foo" */
720 #define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */
721 #define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */
722 #define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */
725 S_varname(pTHX_ GV *gv, const char *gvtype, PADOFFSET targ,
726 SV* keyname, I32 aindex, int subscript_type)
731 SV * const name = sv_newmortal();
734 /* simulate gv_fullname4(), but add literal '^' for $^FOO names
735 * XXX get rid of all this if gv_fullnameX() ever supports this
739 HV *hv = GvSTASH(gv);
740 sv_setpv(name, gvtype);
743 else if (!(p=HvNAME_get(hv)))
745 if (strNE(p, "main")) {
747 sv_catpvn(name,"::", 2);
749 if (GvNAMELEN(gv)>= 1 &&
750 ((unsigned int)*GvNAME(gv)) <= 26)
752 Perl_sv_catpvf(aTHX_ name,"^%c", *GvNAME(gv) + 'A' - 1);
753 sv_catpvn(name,GvNAME(gv)+1,GvNAMELEN(gv)-1);
756 sv_catpvn(name,GvNAME(gv),GvNAMELEN(gv));
760 CV *cv = find_runcv(&u);
761 if (!cv || !CvPADLIST(cv))
763 av = (AV*)(*av_fetch(CvPADLIST(cv), 0, FALSE));
764 sv = *av_fetch(av, targ, FALSE);
765 /* SvLEN in a pad name is not to be trusted */
766 sv_setpv(name, SvPV_nolen(sv));
769 if (subscript_type == FUV_SUBSCRIPT_HASH) {
772 Perl_sv_catpvf(aTHX_ name, "{%s}",
773 pv_display(sv,SvPVX(keyname), SvCUR(keyname), 0, 32));
776 else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
778 Perl_sv_catpvf(aTHX_ name, "[%"IVdf"]", (IV)aindex);
780 else if (subscript_type == FUV_SUBSCRIPT_WITHIN)
781 sv_insert(name, 0, 0, "within ", 7);
788 =for apidoc find_uninit_var
790 Find the name of the undefined variable (if any) that caused the operator o
791 to issue a "Use of uninitialized value" warning.
792 If match is true, only return a name if it's value matches uninit_sv.
793 So roughly speaking, if a unary operator (such as OP_COS) generates a
794 warning, then following the direct child of the op may yield an
795 OP_PADSV or OP_GV that gives the name of the undefined variable. On the
796 other hand, with OP_ADD there are two branches to follow, so we only print
797 the variable name if we get an exact match.
799 The name is returned as a mortal SV.
801 Assumes that PL_op is the op that originally triggered the error, and that
802 PL_comppad/PL_curpad points to the currently executing pad.
808 S_find_uninit_var(pTHX_ OP* obase, SV* uninit_sv, bool match)
817 if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
818 uninit_sv == &PL_sv_placeholder)))
821 switch (obase->op_type) {
828 const bool pad = (obase->op_type == OP_PADAV || obase->op_type == OP_PADHV);
829 const bool hash = (obase->op_type == OP_PADHV || obase->op_type == OP_RV2HV);
832 int subscript_type = FUV_SUBSCRIPT_WITHIN;
834 if (pad) { /* @lex, %lex */
835 sv = PAD_SVl(obase->op_targ);
839 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
840 /* @global, %global */
841 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
844 sv = hash ? (SV*)GvHV(gv): (SV*)GvAV(gv);
846 else /* @{expr}, %{expr} */
847 return find_uninit_var(cUNOPx(obase)->op_first,
851 /* attempt to find a match within the aggregate */
853 keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
855 subscript_type = FUV_SUBSCRIPT_HASH;
858 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
860 subscript_type = FUV_SUBSCRIPT_ARRAY;
863 if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
866 return S_varname(aTHX_ gv, hash ? "%" : "@", obase->op_targ,
867 keysv, index, subscript_type);
871 if (match && PAD_SVl(obase->op_targ) != uninit_sv)
873 return S_varname(aTHX_ Nullgv, "$", obase->op_targ,
874 Nullsv, 0, FUV_SUBSCRIPT_NONE);
877 gv = cGVOPx_gv(obase);
878 if (!gv || (match && GvSV(gv) != uninit_sv))
880 return S_varname(aTHX_ gv, "$", 0, Nullsv, 0, FUV_SUBSCRIPT_NONE);
883 if (obase->op_flags & OPf_SPECIAL) { /* lexical array */
885 av = (AV*)PAD_SV(obase->op_targ);
886 if (!av || SvRMAGICAL(av))
888 svp = av_fetch(av, (I32)obase->op_private, FALSE);
889 if (!svp || *svp != uninit_sv)
892 return S_varname(aTHX_ Nullgv, "$", obase->op_targ,
893 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
896 gv = cGVOPx_gv(obase);
901 if (!av || SvRMAGICAL(av))
903 svp = av_fetch(av, (I32)obase->op_private, FALSE);
904 if (!svp || *svp != uninit_sv)
907 return S_varname(aTHX_ gv, "$", 0,
908 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
913 o = cUNOPx(obase)->op_first;
914 if (!o || o->op_type != OP_NULL ||
915 ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
917 return find_uninit_var(cBINOPo->op_last, uninit_sv, match);
922 /* $a[uninit_expr] or $h{uninit_expr} */
923 return find_uninit_var(cBINOPx(obase)->op_last, uninit_sv, match);
926 o = cBINOPx(obase)->op_first;
927 kid = cBINOPx(obase)->op_last;
929 /* get the av or hv, and optionally the gv */
931 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
932 sv = PAD_SV(o->op_targ);
934 else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
935 && cUNOPo->op_first->op_type == OP_GV)
937 gv = cGVOPx_gv(cUNOPo->op_first);
940 sv = o->op_type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)GvAV(gv);
945 if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
946 /* index is constant */
950 if (obase->op_type == OP_HELEM) {
951 HE* he = hv_fetch_ent((HV*)sv, cSVOPx_sv(kid), 0, 0);
952 if (!he || HeVAL(he) != uninit_sv)
956 svp = av_fetch((AV*)sv, SvIV(cSVOPx_sv(kid)), FALSE);
957 if (!svp || *svp != uninit_sv)
961 if (obase->op_type == OP_HELEM)
962 return S_varname(aTHX_ gv, "%", o->op_targ,
963 cSVOPx_sv(kid), 0, FUV_SUBSCRIPT_HASH);
965 return S_varname(aTHX_ gv, "@", o->op_targ, Nullsv,
966 SvIV(cSVOPx_sv(kid)), FUV_SUBSCRIPT_ARRAY);
970 /* index is an expression;
971 * attempt to find a match within the aggregate */
972 if (obase->op_type == OP_HELEM) {
973 SV *keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
975 return S_varname(aTHX_ gv, "%", o->op_targ,
976 keysv, 0, FUV_SUBSCRIPT_HASH);
979 const I32 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
981 return S_varname(aTHX_ gv, "@", o->op_targ,
982 Nullsv, index, FUV_SUBSCRIPT_ARRAY);
986 return S_varname(aTHX_ gv,
987 (o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
989 o->op_targ, Nullsv, 0, FUV_SUBSCRIPT_WITHIN);
995 /* only examine RHS */
996 return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv, match);
999 o = cUNOPx(obase)->op_first;
1000 if (o->op_type == OP_PUSHMARK)
1003 if (!o->op_sibling) {
1004 /* one-arg version of open is highly magical */
1006 if (o->op_type == OP_GV) { /* open FOO; */
1008 if (match && GvSV(gv) != uninit_sv)
1010 return S_varname(aTHX_ gv, "$", 0,
1011 Nullsv, 0, FUV_SUBSCRIPT_NONE);
1013 /* other possibilities not handled are:
1014 * open $x; or open my $x; should return '${*$x}'
1015 * open expr; should return '$'.expr ideally
1021 /* ops where $_ may be an implicit arg */
1025 if ( !(obase->op_flags & OPf_STACKED)) {
1026 if (uninit_sv == ((obase->op_private & OPpTARGET_MY)
1027 ? PAD_SVl(obase->op_targ)
1030 sv = sv_newmortal();
1039 /* skip filehandle as it can't produce 'undef' warning */
1040 o = cUNOPx(obase)->op_first;
1041 if ((obase->op_flags & OPf_STACKED) && o->op_type == OP_PUSHMARK)
1042 o = o->op_sibling->op_sibling;
1049 match = 1; /* XS or custom code could trigger random warnings */
1054 if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs))
1055 return sv_2mortal(newSVpv("${$/}", 0));
1060 if (!(obase->op_flags & OPf_KIDS))
1062 o = cUNOPx(obase)->op_first;
1068 /* if all except one arg are constant, or have no side-effects,
1069 * or are optimized away, then it's unambiguous */
1071 for (kid=o; kid; kid = kid->op_sibling) {
1073 ( (kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid)))
1074 || (kid->op_type == OP_NULL && ! (kid->op_flags & OPf_KIDS))
1075 || (kid->op_type == OP_PUSHMARK)
1079 if (o2) { /* more than one found */
1086 return find_uninit_var(o2, uninit_sv, match);
1090 sv = find_uninit_var(o, uninit_sv, 1);
1102 =for apidoc report_uninit
1104 Print appropriate "Use of uninitialized variable" warning
1110 Perl_report_uninit(pTHX_ SV* uninit_sv)
1113 SV* varname = Nullsv;
1115 varname = find_uninit_var(PL_op, uninit_sv,0);
1117 sv_insert(varname, 0, 0, " ", 1);
1119 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
1120 varname ? SvPV_nolen(varname) : "",
1121 " in ", OP_DESC(PL_op));
1124 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
1128 /* allocate another arena's worth of NV bodies */
1136 New(711, ptr, PERL_ARENA_SIZE/sizeof(NV), NV);
1137 *((void **) ptr) = (void *)PL_xnv_arenaroot;
1138 PL_xnv_arenaroot = ptr;
1141 xnvend = &xnv[PERL_ARENA_SIZE / sizeof(NV) - 1];
1142 xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */
1144 while (xnv < xnvend) {
1145 *(NV**)xnv = (NV*)(xnv + 1);
1151 /* allocate another arena's worth of struct xpv */
1157 xpv_allocated* xpvend;
1158 New(713, xpv, PERL_ARENA_SIZE/sizeof(xpv_allocated), xpv_allocated);
1159 *((xpv_allocated**)xpv) = PL_xpv_arenaroot;
1160 PL_xpv_arenaroot = xpv;
1162 xpvend = &xpv[PERL_ARENA_SIZE / sizeof(xpv_allocated) - 1];
1163 PL_xpv_root = ++xpv;
1164 while (xpv < xpvend) {
1165 *((xpv_allocated**)xpv) = xpv + 1;
1168 *((xpv_allocated**)xpv) = 0;
1171 /* allocate another arena's worth of struct xpviv */
1176 xpviv_allocated* xpviv;
1177 xpviv_allocated* xpvivend;
1178 New(713, xpviv, PERL_ARENA_SIZE/sizeof(xpviv_allocated), xpviv_allocated);
1179 *((xpviv_allocated**)xpviv) = PL_xpviv_arenaroot;
1180 PL_xpviv_arenaroot = xpviv;
1182 xpvivend = &xpviv[PERL_ARENA_SIZE / sizeof(xpviv_allocated) - 1];
1183 PL_xpviv_root = ++xpviv;
1184 while (xpviv < xpvivend) {
1185 *((xpviv_allocated**)xpviv) = xpviv + 1;
1188 *((xpviv_allocated**)xpviv) = 0;
1191 /* allocate another arena's worth of struct xpvnv */
1198 New(715, xpvnv, PERL_ARENA_SIZE/sizeof(XPVNV), XPVNV);
1199 *((XPVNV**)xpvnv) = PL_xpvnv_arenaroot;
1200 PL_xpvnv_arenaroot = xpvnv;
1202 xpvnvend = &xpvnv[PERL_ARENA_SIZE / sizeof(XPVNV) - 1];
1203 PL_xpvnv_root = ++xpvnv;
1204 while (xpvnv < xpvnvend) {
1205 *((XPVNV**)xpvnv) = xpvnv + 1;
1208 *((XPVNV**)xpvnv) = 0;
1211 /* allocate another arena's worth of struct xpvcv */
1218 New(716, xpvcv, PERL_ARENA_SIZE/sizeof(XPVCV), XPVCV);
1219 *((XPVCV**)xpvcv) = PL_xpvcv_arenaroot;
1220 PL_xpvcv_arenaroot = xpvcv;
1222 xpvcvend = &xpvcv[PERL_ARENA_SIZE / sizeof(XPVCV) - 1];
1223 PL_xpvcv_root = ++xpvcv;
1224 while (xpvcv < xpvcvend) {
1225 *((XPVCV**)xpvcv) = xpvcv + 1;
1228 *((XPVCV**)xpvcv) = 0;
1231 /* allocate another arena's worth of struct xpvav */
1236 xpvav_allocated* xpvav;
1237 xpvav_allocated* xpvavend;
1238 New(717, xpvav, PERL_ARENA_SIZE/sizeof(xpvav_allocated),
1240 *((xpvav_allocated**)xpvav) = PL_xpvav_arenaroot;
1241 PL_xpvav_arenaroot = xpvav;
1243 xpvavend = &xpvav[PERL_ARENA_SIZE / sizeof(xpvav_allocated) - 1];
1244 PL_xpvav_root = ++xpvav;
1245 while (xpvav < xpvavend) {
1246 *((xpvav_allocated**)xpvav) = xpvav + 1;
1249 *((xpvav_allocated**)xpvav) = 0;
1252 /* allocate another arena's worth of struct xpvhv */
1257 xpvhv_allocated* xpvhv;
1258 xpvhv_allocated* xpvhvend;
1259 New(718, xpvhv, PERL_ARENA_SIZE/sizeof(xpvhv_allocated),
1261 *((xpvhv_allocated**)xpvhv) = PL_xpvhv_arenaroot;
1262 PL_xpvhv_arenaroot = xpvhv;
1264 xpvhvend = &xpvhv[PERL_ARENA_SIZE / sizeof(xpvhv_allocated) - 1];
1265 PL_xpvhv_root = ++xpvhv;
1266 while (xpvhv < xpvhvend) {
1267 *((xpvhv_allocated**)xpvhv) = xpvhv + 1;
1270 *((xpvhv_allocated**)xpvhv) = 0;
1273 /* allocate another arena's worth of struct xpvmg */
1280 New(719, xpvmg, PERL_ARENA_SIZE/sizeof(XPVMG), XPVMG);
1281 *((XPVMG**)xpvmg) = PL_xpvmg_arenaroot;
1282 PL_xpvmg_arenaroot = xpvmg;
1284 xpvmgend = &xpvmg[PERL_ARENA_SIZE / sizeof(XPVMG) - 1];
1285 PL_xpvmg_root = ++xpvmg;
1286 while (xpvmg < xpvmgend) {
1287 *((XPVMG**)xpvmg) = xpvmg + 1;
1290 *((XPVMG**)xpvmg) = 0;
1293 /* allocate another arena's worth of struct xpvgv */
1300 New(720, xpvgv, PERL_ARENA_SIZE/sizeof(XPVGV), XPVGV);
1301 *((XPVGV**)xpvgv) = PL_xpvgv_arenaroot;
1302 PL_xpvgv_arenaroot = xpvgv;
1304 xpvgvend = &xpvgv[PERL_ARENA_SIZE / sizeof(XPVGV) - 1];
1305 PL_xpvgv_root = ++xpvgv;
1306 while (xpvgv < xpvgvend) {
1307 *((XPVGV**)xpvgv) = xpvgv + 1;
1310 *((XPVGV**)xpvgv) = 0;
1313 /* allocate another arena's worth of struct xpvlv */
1320 New(720, xpvlv, PERL_ARENA_SIZE/sizeof(XPVLV), XPVLV);
1321 *((XPVLV**)xpvlv) = PL_xpvlv_arenaroot;
1322 PL_xpvlv_arenaroot = xpvlv;
1324 xpvlvend = &xpvlv[PERL_ARENA_SIZE / sizeof(XPVLV) - 1];
1325 PL_xpvlv_root = ++xpvlv;
1326 while (xpvlv < xpvlvend) {
1327 *((XPVLV**)xpvlv) = xpvlv + 1;
1330 *((XPVLV**)xpvlv) = 0;
1333 /* allocate another arena's worth of struct xpvbm */
1340 New(721, xpvbm, PERL_ARENA_SIZE/sizeof(XPVBM), XPVBM);
1341 *((XPVBM**)xpvbm) = PL_xpvbm_arenaroot;
1342 PL_xpvbm_arenaroot = xpvbm;
1344 xpvbmend = &xpvbm[PERL_ARENA_SIZE / sizeof(XPVBM) - 1];
1345 PL_xpvbm_root = ++xpvbm;
1346 while (xpvbm < xpvbmend) {
1347 *((XPVBM**)xpvbm) = xpvbm + 1;
1350 *((XPVBM**)xpvbm) = 0;
1353 /* grab a new NV body from the free list, allocating more if necessary */
1363 PL_xnv_root = *(NV**)xnv;
1365 return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv));
1368 /* return an NV body to the free list */
1371 S_del_xnv(pTHX_ XPVNV *p)
1373 NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv));
1375 *(NV**)xnv = PL_xnv_root;
1380 /* grab a new struct xpv from the free list, allocating more if necessary */
1390 PL_xpv_root = *(xpv_allocated**)xpv;
1392 /* If xpv_allocated is the same structure as XPV then the two OFFSETs
1393 sum to zero, and the pointer is unchanged. If the allocated structure
1394 is smaller (no initial IV actually allocated) then the net effect is
1395 to subtract the size of the IV from the pointer, to return a new pointer
1396 as if an initial IV were actually allocated. */
1397 return (XPV*)((char*)xpv - STRUCT_OFFSET(XPV, xpv_cur)
1398 + STRUCT_OFFSET(xpv_allocated, xpv_cur));
1401 /* return a struct xpv to the free list */
1404 S_del_xpv(pTHX_ XPV *p)
1407 = (xpv_allocated*)((char*)(p) + STRUCT_OFFSET(XPV, xpv_cur)
1408 - STRUCT_OFFSET(xpv_allocated, xpv_cur));
1410 *(xpv_allocated**)xpv = PL_xpv_root;
1415 /* grab a new struct xpviv from the free list, allocating more if necessary */
1420 xpviv_allocated* xpviv;
1424 xpviv = PL_xpviv_root;
1425 PL_xpviv_root = *(xpviv_allocated**)xpviv;
1427 /* If xpviv_allocated is the same structure as XPVIV then the two OFFSETs
1428 sum to zero, and the pointer is unchanged. If the allocated structure
1429 is smaller (no initial IV actually allocated) then the net effect is
1430 to subtract the size of the IV from the pointer, to return a new pointer
1431 as if an initial IV were actually allocated. */
1432 return (XPVIV*)((char*)xpviv - STRUCT_OFFSET(XPVIV, xpv_cur)
1433 + STRUCT_OFFSET(xpviv_allocated, xpv_cur));
1436 /* return a struct xpviv to the free list */
1439 S_del_xpviv(pTHX_ XPVIV *p)
1441 xpviv_allocated* xpviv
1442 = (xpviv_allocated*)((char*)(p) + STRUCT_OFFSET(XPVIV, xpv_cur)
1443 - STRUCT_OFFSET(xpviv_allocated, xpv_cur));
1445 *(xpviv_allocated**)xpviv = PL_xpviv_root;
1446 PL_xpviv_root = xpviv;
1450 /* grab a new struct xpvnv from the free list, allocating more if necessary */
1459 xpvnv = PL_xpvnv_root;
1460 PL_xpvnv_root = *(XPVNV**)xpvnv;
1465 /* return a struct xpvnv to the free list */
1468 S_del_xpvnv(pTHX_ XPVNV *p)
1471 *(XPVNV**)p = PL_xpvnv_root;
1476 /* grab a new struct xpvcv from the free list, allocating more if necessary */
1485 xpvcv = PL_xpvcv_root;
1486 PL_xpvcv_root = *(XPVCV**)xpvcv;
1491 /* return a struct xpvcv to the free list */
1494 S_del_xpvcv(pTHX_ XPVCV *p)
1497 *(XPVCV**)p = PL_xpvcv_root;
1502 /* grab a new struct xpvav from the free list, allocating more if necessary */
1507 xpvav_allocated* xpvav;
1511 xpvav = PL_xpvav_root;
1512 PL_xpvav_root = *(xpvav_allocated**)xpvav;
1514 return (XPVAV*)((char*)xpvav - STRUCT_OFFSET(XPVAV, xav_fill)
1515 + STRUCT_OFFSET(xpvav_allocated, xav_fill));
1518 /* return a struct xpvav to the free list */
1521 S_del_xpvav(pTHX_ XPVAV *p)
1523 xpvav_allocated* xpvav
1524 = (xpvav_allocated*)((char*)(p) + STRUCT_OFFSET(XPVAV, xav_fill)
1525 - STRUCT_OFFSET(xpvav_allocated, xav_fill));
1527 *(xpvav_allocated**)xpvav = PL_xpvav_root;
1528 PL_xpvav_root = xpvav;
1532 /* grab a new struct xpvhv from the free list, allocating more if necessary */
1537 xpvhv_allocated* xpvhv;
1541 xpvhv = PL_xpvhv_root;
1542 PL_xpvhv_root = *(xpvhv_allocated**)xpvhv;
1544 return (XPVHV*)((char*)xpvhv - STRUCT_OFFSET(XPVHV, xhv_fill)
1545 + STRUCT_OFFSET(xpvhv_allocated, xhv_fill));
1548 /* return a struct xpvhv to the free list */
1551 S_del_xpvhv(pTHX_ XPVHV *p)
1553 xpvhv_allocated* xpvhv
1554 = (xpvhv_allocated*)((char*)(p) + STRUCT_OFFSET(XPVHV, xhv_fill)
1555 - STRUCT_OFFSET(xpvhv_allocated, xhv_fill));
1557 *(xpvhv_allocated**)xpvhv = PL_xpvhv_root;
1558 PL_xpvhv_root = xpvhv;
1562 /* grab a new struct xpvmg from the free list, allocating more if necessary */
1571 xpvmg = PL_xpvmg_root;
1572 PL_xpvmg_root = *(XPVMG**)xpvmg;
1577 /* return a struct xpvmg to the free list */
1580 S_del_xpvmg(pTHX_ XPVMG *p)
1583 *(XPVMG**)p = PL_xpvmg_root;
1588 /* grab a new struct xpvgv from the free list, allocating more if necessary */
1597 xpvgv = PL_xpvgv_root;
1598 PL_xpvgv_root = *(XPVGV**)xpvgv;
1603 /* return a struct xpvgv to the free list */
1606 S_del_xpvgv(pTHX_ XPVGV *p)
1609 *(XPVGV**)p = PL_xpvgv_root;
1614 /* grab a new struct xpvlv from the free list, allocating more if necessary */
1623 xpvlv = PL_xpvlv_root;
1624 PL_xpvlv_root = *(XPVLV**)xpvlv;
1629 /* return a struct xpvlv to the free list */
1632 S_del_xpvlv(pTHX_ XPVLV *p)
1635 *(XPVLV**)p = PL_xpvlv_root;
1640 /* grab a new struct xpvbm from the free list, allocating more if necessary */
1649 xpvbm = PL_xpvbm_root;
1650 PL_xpvbm_root = *(XPVBM**)xpvbm;
1655 /* return a struct xpvbm to the free list */
1658 S_del_xpvbm(pTHX_ XPVBM *p)
1661 *(XPVBM**)p = PL_xpvbm_root;
1666 #define my_safemalloc(s) (void*)safemalloc(s)
1667 #define my_safefree(p) safefree((char*)p)
1671 #define new_XNV() my_safemalloc(sizeof(XPVNV))
1672 #define del_XNV(p) my_safefree(p)
1674 #define new_XPV() my_safemalloc(sizeof(XPV))
1675 #define del_XPV(p) my_safefree(p)
1677 #define new_XPVIV() my_safemalloc(sizeof(XPVIV))
1678 #define del_XPVIV(p) my_safefree(p)
1680 #define new_XPVNV() my_safemalloc(sizeof(XPVNV))
1681 #define del_XPVNV(p) my_safefree(p)
1683 #define new_XPVCV() my_safemalloc(sizeof(XPVCV))
1684 #define del_XPVCV(p) my_safefree(p)
1686 #define new_XPVAV() my_safemalloc(sizeof(XPVAV))
1687 #define del_XPVAV(p) my_safefree(p)
1689 #define new_XPVHV() my_safemalloc(sizeof(XPVHV))
1690 #define del_XPVHV(p) my_safefree(p)
1692 #define new_XPVMG() my_safemalloc(sizeof(XPVMG))
1693 #define del_XPVMG(p) my_safefree(p)
1695 #define new_XPVGV() my_safemalloc(sizeof(XPVGV))
1696 #define del_XPVGV(p) my_safefree(p)
1698 #define new_XPVLV() my_safemalloc(sizeof(XPVLV))
1699 #define del_XPVLV(p) my_safefree(p)
1701 #define new_XPVBM() my_safemalloc(sizeof(XPVBM))
1702 #define del_XPVBM(p) my_safefree(p)
1706 #define new_XNV() (void*)new_xnv()
1707 #define del_XNV(p) del_xnv((XPVNV*) p)
1709 #define new_XPV() (void*)new_xpv()
1710 #define del_XPV(p) del_xpv((XPV *)p)
1712 #define new_XPVIV() (void*)new_xpviv()
1713 #define del_XPVIV(p) del_xpviv((XPVIV *)p)
1715 #define new_XPVNV() (void*)new_xpvnv()
1716 #define del_XPVNV(p) del_xpvnv((XPVNV *)p)
1718 #define new_XPVCV() (void*)new_xpvcv()
1719 #define del_XPVCV(p) del_xpvcv((XPVCV *)p)
1721 #define new_XPVAV() (void*)new_xpvav()
1722 #define del_XPVAV(p) del_xpvav((XPVAV *)p)
1724 #define new_XPVHV() (void*)new_xpvhv()
1725 #define del_XPVHV(p) del_xpvhv((XPVHV *)p)
1727 #define new_XPVMG() (void*)new_xpvmg()
1728 #define del_XPVMG(p) del_xpvmg((XPVMG *)p)
1730 #define new_XPVGV() (void*)new_xpvgv()
1731 #define del_XPVGV(p) del_xpvgv((XPVGV *)p)
1733 #define new_XPVLV() (void*)new_xpvlv()
1734 #define del_XPVLV(p) del_xpvlv((XPVLV *)p)
1736 #define new_XPVBM() (void*)new_xpvbm()
1737 #define del_XPVBM(p) del_xpvbm((XPVBM *)p)
1741 #define new_XPVFM() my_safemalloc(sizeof(XPVFM))
1742 #define del_XPVFM(p) my_safefree(p)
1744 #define new_XPVIO() my_safemalloc(sizeof(XPVIO))
1745 #define del_XPVIO(p) my_safefree(p)
1748 =for apidoc sv_upgrade
1750 Upgrade an SV to a more complex form. Generally adds a new body type to the
1751 SV, then copies across as much information as possible from the old body.
1752 You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>.
1758 Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
1769 if (mt != SVt_PV && SvIsCOW(sv)) {
1770 sv_force_normal_flags(sv, 0);
1773 if (SvTYPE(sv) == mt)
1784 switch (SvTYPE(sv)) {
1791 else if (mt < SVt_PVIV)
1801 pv = (char*)SvRV(sv);
1810 else if (mt == SVt_NV)
1818 del_XPVIV(SvANY(sv));
1826 del_XPVNV(SvANY(sv));
1829 /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
1830 there's no way that it can be safely upgraded, because perl.c
1831 expects to Safefree(SvANY(PL_mess_sv)) */
1832 assert(sv != PL_mess_sv);
1833 /* This flag bit is used to mean other things in other scalar types.
1834 Given that it only has meaning inside the pad, it shouldn't be set
1835 on anything that can get upgraded. */
1836 assert((SvFLAGS(sv) & SVpad_TYPED) == 0);
1842 magic = SvMAGIC(sv);
1843 stash = SvSTASH(sv);
1844 del_XPVMG(SvANY(sv));
1847 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
1850 SvFLAGS(sv) &= ~SVTYPEMASK;
1855 Perl_croak(aTHX_ "Can't upgrade to undef");
1857 SvANY(sv) = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
1861 SvANY(sv) = new_XNV();
1865 SvANY(sv) = &sv->sv_u.svu_rv;
1866 SvRV_set(sv, (SV*)pv);
1869 SvANY(sv) = new_XPVHV();
1870 ((XPVHV*) SvANY(sv))->xhv_aux = 0;
1873 HvTOTALKEYS(sv) = 0;
1875 /* Fall through... */
1878 SvANY(sv) = new_XPVAV();
1886 /* XXX? Only SVt_NULL is ever upgraded to AV or HV? */
1888 /* FIXME. Should be able to remove all this if()... if the above
1889 assertion is genuinely always true. */
1892 SvFLAGS(sv) &= ~SVf_OOK;
1895 SvPV_set(sv, (char*)0);
1896 SvMAGIC_set(sv, magic);
1897 SvSTASH_set(sv, stash);
1901 SvANY(sv) = new_XPVIO();
1902 Zero(SvANY(sv), 1, XPVIO);
1903 IoPAGE_LEN(sv) = 60;
1904 goto set_magic_common;
1906 SvANY(sv) = new_XPVFM();
1907 Zero(SvANY(sv), 1, XPVFM);
1908 goto set_magic_common;
1910 SvANY(sv) = new_XPVBM();
1914 goto set_magic_common;
1916 SvANY(sv) = new_XPVGV();
1922 goto set_magic_common;
1924 SvANY(sv) = new_XPVCV();
1925 Zero(SvANY(sv), 1, XPVCV);
1926 goto set_magic_common;
1928 SvANY(sv) = new_XPVLV();
1941 SvANY(sv) = new_XPVMG();
1944 SvMAGIC_set(sv, magic);
1945 SvSTASH_set(sv, stash);
1949 SvANY(sv) = new_XPVNV();
1955 SvANY(sv) = new_XPVIV();
1964 SvANY(sv) = new_XPV();
1975 =for apidoc sv_backoff
1977 Remove any string offset. You should normally use the C<SvOOK_off> macro
1984 Perl_sv_backoff(pTHX_ register SV *sv)
1988 char *s = SvPVX(sv);
1989 SvLEN_set(sv, SvLEN(sv) + SvIVX(sv));
1990 SvPV_set(sv, SvPVX(sv) - SvIVX(sv));
1992 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
1994 SvFLAGS(sv) &= ~SVf_OOK;
2001 Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
2002 upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
2003 Use the C<SvGROW> wrapper instead.
2009 Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
2013 #ifdef HAS_64K_LIMIT
2014 if (newlen >= 0x10000) {
2015 PerlIO_printf(Perl_debug_log,
2016 "Allocation too large: %"UVxf"\n", (UV)newlen);
2019 #endif /* HAS_64K_LIMIT */
2022 if (SvTYPE(sv) < SVt_PV) {
2023 sv_upgrade(sv, SVt_PV);
2026 else if (SvOOK(sv)) { /* pv is offset? */
2029 if (newlen > SvLEN(sv))
2030 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
2031 #ifdef HAS_64K_LIMIT
2032 if (newlen >= 0x10000)
2039 if (newlen > SvLEN(sv)) { /* need more room? */
2040 if (SvLEN(sv) && s) {
2042 const STRLEN l = malloced_size((void*)SvPVX(sv));
2048 Renew(s,newlen,char);
2051 New(703, s, newlen, char);
2052 if (SvPVX(sv) && SvCUR(sv)) {
2053 Move(SvPVX(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
2057 SvLEN_set(sv, newlen);
2063 =for apidoc sv_setiv
2065 Copies an integer into the given SV, upgrading first if necessary.
2066 Does not handle 'set' magic. See also C<sv_setiv_mg>.
2072 Perl_sv_setiv(pTHX_ register SV *sv, IV i)
2074 SV_CHECK_THINKFIRST_COW_DROP(sv);
2075 switch (SvTYPE(sv)) {
2077 sv_upgrade(sv, SVt_IV);
2080 sv_upgrade(sv, SVt_PVNV);
2084 sv_upgrade(sv, SVt_PVIV);
2093 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
2096 (void)SvIOK_only(sv); /* validate number */
2102 =for apidoc sv_setiv_mg
2104 Like C<sv_setiv>, but also handles 'set' magic.
2110 Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
2117 =for apidoc sv_setuv
2119 Copies an unsigned integer into the given SV, upgrading first if necessary.
2120 Does not handle 'set' magic. See also C<sv_setuv_mg>.
2126 Perl_sv_setuv(pTHX_ register SV *sv, UV u)
2128 /* With these two if statements:
2129 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
2132 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
2134 If you wish to remove them, please benchmark to see what the effect is
2136 if (u <= (UV)IV_MAX) {
2137 sv_setiv(sv, (IV)u);
2146 =for apidoc sv_setuv_mg
2148 Like C<sv_setuv>, but also handles 'set' magic.
2154 Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
2156 /* With these two if statements:
2157 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
2160 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
2162 If you wish to remove them, please benchmark to see what the effect is
2164 if (u <= (UV)IV_MAX) {
2165 sv_setiv(sv, (IV)u);
2175 =for apidoc sv_setnv
2177 Copies a double into the given SV, upgrading first if necessary.
2178 Does not handle 'set' magic. See also C<sv_setnv_mg>.
2184 Perl_sv_setnv(pTHX_ register SV *sv, NV num)
2186 SV_CHECK_THINKFIRST_COW_DROP(sv);
2187 switch (SvTYPE(sv)) {
2190 sv_upgrade(sv, SVt_NV);
2195 sv_upgrade(sv, SVt_PVNV);
2204 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
2208 (void)SvNOK_only(sv); /* validate number */
2213 =for apidoc sv_setnv_mg
2215 Like C<sv_setnv>, but also handles 'set' magic.
2221 Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
2227 /* Print an "isn't numeric" warning, using a cleaned-up,
2228 * printable version of the offending string
2232 S_not_a_number(pTHX_ SV *sv)
2239 dsv = sv_2mortal(newSVpv("", 0));
2240 pv = sv_uni_display(dsv, sv, 10, 0);
2243 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
2244 /* each *s can expand to 4 chars + "...\0",
2245 i.e. need room for 8 chars */
2248 for (s = SvPVX(sv), end = s + SvCUR(sv); s < end && d < limit; s++) {
2250 if (ch & 128 && !isPRINT_LC(ch)) {
2259 else if (ch == '\r') {
2263 else if (ch == '\f') {
2267 else if (ch == '\\') {
2271 else if (ch == '\0') {
2275 else if (isPRINT_LC(ch))
2292 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
2293 "Argument \"%s\" isn't numeric in %s", pv,
2296 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
2297 "Argument \"%s\" isn't numeric", pv);
2301 =for apidoc looks_like_number
2303 Test if the content of an SV looks like a number (or is a number).
2304 C<Inf> and C<Infinity> are treated as numbers (so will not issue a
2305 non-numeric warning), even if your atof() doesn't grok them.
2311 Perl_looks_like_number(pTHX_ SV *sv)
2313 register const char *sbegin;
2320 else if (SvPOKp(sv))
2321 sbegin = SvPV(sv, len);
2323 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
2324 return grok_number(sbegin, len, NULL);
2327 /* Actually, ISO C leaves conversion of UV to IV undefined, but
2328 until proven guilty, assume that things are not that bad... */
2333 As 64 bit platforms often have an NV that doesn't preserve all bits of
2334 an IV (an assumption perl has been based on to date) it becomes necessary
2335 to remove the assumption that the NV always carries enough precision to
2336 recreate the IV whenever needed, and that the NV is the canonical form.
2337 Instead, IV/UV and NV need to be given equal rights. So as to not lose
2338 precision as a side effect of conversion (which would lead to insanity
2339 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
2340 1) to distinguish between IV/UV/NV slots that have cached a valid
2341 conversion where precision was lost and IV/UV/NV slots that have a
2342 valid conversion which has lost no precision
2343 2) to ensure that if a numeric conversion to one form is requested that
2344 would lose precision, the precise conversion (or differently
2345 imprecise conversion) is also performed and cached, to prevent
2346 requests for different numeric formats on the same SV causing
2347 lossy conversion chains. (lossless conversion chains are perfectly
2352 SvIOKp is true if the IV slot contains a valid value
2353 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
2354 SvNOKp is true if the NV slot contains a valid value
2355 SvNOK is true only if the NV value is accurate
2358 while converting from PV to NV, check to see if converting that NV to an
2359 IV(or UV) would lose accuracy over a direct conversion from PV to
2360 IV(or UV). If it would, cache both conversions, return NV, but mark
2361 SV as IOK NOKp (ie not NOK).
2363 While converting from PV to IV, check to see if converting that IV to an
2364 NV would lose accuracy over a direct conversion from PV to NV. If it
2365 would, cache both conversions, flag similarly.
2367 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
2368 correctly because if IV & NV were set NV *always* overruled.
2369 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
2370 changes - now IV and NV together means that the two are interchangeable:
2371 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
2373 The benefit of this is that operations such as pp_add know that if
2374 SvIOK is true for both left and right operands, then integer addition
2375 can be used instead of floating point (for cases where the result won't
2376 overflow). Before, floating point was always used, which could lead to
2377 loss of precision compared with integer addition.
2379 * making IV and NV equal status should make maths accurate on 64 bit
2381 * may speed up maths somewhat if pp_add and friends start to use
2382 integers when possible instead of fp. (Hopefully the overhead in
2383 looking for SvIOK and checking for overflow will not outweigh the
2384 fp to integer speedup)
2385 * will slow down integer operations (callers of SvIV) on "inaccurate"
2386 values, as the change from SvIOK to SvIOKp will cause a call into
2387 sv_2iv each time rather than a macro access direct to the IV slot
2388 * should speed up number->string conversion on integers as IV is
2389 favoured when IV and NV are equally accurate
2391 ####################################################################
2392 You had better be using SvIOK_notUV if you want an IV for arithmetic:
2393 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
2394 On the other hand, SvUOK is true iff UV.
2395 ####################################################################
2397 Your mileage will vary depending your CPU's relative fp to integer
2401 #ifndef NV_PRESERVES_UV
2402 # define IS_NUMBER_UNDERFLOW_IV 1
2403 # define IS_NUMBER_UNDERFLOW_UV 2
2404 # define IS_NUMBER_IV_AND_UV 2
2405 # define IS_NUMBER_OVERFLOW_IV 4
2406 # define IS_NUMBER_OVERFLOW_UV 5
2408 /* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
2410 /* For sv_2nv these three cases are "SvNOK and don't bother casting" */
2412 S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype)
2414 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%"UVxf" NV=%"NVgf" inttype=%"UVXf"\n", SvPVX(sv), SvIVX(sv), SvNVX(sv), (UV)numtype));
2415 if (SvNVX(sv) < (NV)IV_MIN) {
2416 (void)SvIOKp_on(sv);
2418 SvIV_set(sv, IV_MIN);
2419 return IS_NUMBER_UNDERFLOW_IV;
2421 if (SvNVX(sv) > (NV)UV_MAX) {
2422 (void)SvIOKp_on(sv);
2425 SvUV_set(sv, UV_MAX);
2426 return IS_NUMBER_OVERFLOW_UV;
2428 (void)SvIOKp_on(sv);
2430 /* Can't use strtol etc to convert this string. (See truth table in
2432 if (SvNVX(sv) <= (UV)IV_MAX) {
2433 SvIV_set(sv, I_V(SvNVX(sv)));
2434 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2435 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
2437 /* Integer is imprecise. NOK, IOKp */
2439 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
2442 SvUV_set(sv, U_V(SvNVX(sv)));
2443 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2444 if (SvUVX(sv) == UV_MAX) {
2445 /* As we know that NVs don't preserve UVs, UV_MAX cannot
2446 possibly be preserved by NV. Hence, it must be overflow.
2448 return IS_NUMBER_OVERFLOW_UV;
2450 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
2452 /* Integer is imprecise. NOK, IOKp */
2454 return IS_NUMBER_OVERFLOW_IV;
2456 #endif /* !NV_PRESERVES_UV*/
2458 /* sv_2iv() is now a macro using Perl_sv_2iv_flags();
2459 * this function provided for binary compatibility only
2463 Perl_sv_2iv(pTHX_ register SV *sv)
2465 return sv_2iv_flags(sv, SV_GMAGIC);
2469 =for apidoc sv_2iv_flags
2471 Return the integer value of an SV, doing any necessary string
2472 conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2473 Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
2479 Perl_sv_2iv_flags(pTHX_ register SV *sv, I32 flags)
2483 if (SvGMAGICAL(sv)) {
2484 if (flags & SV_GMAGIC)
2489 return I_V(SvNVX(sv));
2491 if (SvPOKp(sv) && SvLEN(sv))
2494 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2495 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2501 if (SvTHINKFIRST(sv)) {
2504 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
2505 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
2506 return SvIV(tmpstr);
2507 return PTR2IV(SvRV(sv));
2510 sv_force_normal_flags(sv, 0);
2512 if (SvREADONLY(sv) && !SvOK(sv)) {
2513 if (ckWARN(WARN_UNINITIALIZED))
2520 return (IV)(SvUVX(sv));
2527 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2528 * without also getting a cached IV/UV from it at the same time
2529 * (ie PV->NV conversion should detect loss of accuracy and cache
2530 * IV or UV at same time to avoid this. NWC */
2532 if (SvTYPE(sv) == SVt_NV)
2533 sv_upgrade(sv, SVt_PVNV);
2535 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2536 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2537 certainly cast into the IV range at IV_MAX, whereas the correct
2538 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2540 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2541 SvIV_set(sv, I_V(SvNVX(sv)));
2542 if (SvNVX(sv) == (NV) SvIVX(sv)
2543 #ifndef NV_PRESERVES_UV
2544 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2545 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2546 /* Don't flag it as "accurately an integer" if the number
2547 came from a (by definition imprecise) NV operation, and
2548 we're outside the range of NV integer precision */
2551 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2552 DEBUG_c(PerlIO_printf(Perl_debug_log,
2553 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
2559 /* IV not precise. No need to convert from PV, as NV
2560 conversion would already have cached IV if it detected
2561 that PV->IV would be better than PV->NV->IV
2562 flags already correct - don't set public IOK. */
2563 DEBUG_c(PerlIO_printf(Perl_debug_log,
2564 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
2569 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2570 but the cast (NV)IV_MIN rounds to a the value less (more
2571 negative) than IV_MIN which happens to be equal to SvNVX ??
2572 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2573 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2574 (NV)UVX == NVX are both true, but the values differ. :-(
2575 Hopefully for 2s complement IV_MIN is something like
2576 0x8000000000000000 which will be exact. NWC */
2579 SvUV_set(sv, U_V(SvNVX(sv)));
2581 (SvNVX(sv) == (NV) SvUVX(sv))
2582 #ifndef NV_PRESERVES_UV
2583 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2584 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2585 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2586 /* Don't flag it as "accurately an integer" if the number
2587 came from a (by definition imprecise) NV operation, and
2588 we're outside the range of NV integer precision */
2594 DEBUG_c(PerlIO_printf(Perl_debug_log,
2595 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
2599 return (IV)SvUVX(sv);
2602 else if (SvPOKp(sv) && SvLEN(sv)) {
2604 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2605 /* We want to avoid a possible problem when we cache an IV which
2606 may be later translated to an NV, and the resulting NV is not
2607 the same as the direct translation of the initial string
2608 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2609 be careful to ensure that the value with the .456 is around if the
2610 NV value is requested in the future).
2612 This means that if we cache such an IV, we need to cache the
2613 NV as well. Moreover, we trade speed for space, and do not
2614 cache the NV if we are sure it's not needed.
2617 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2618 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2619 == IS_NUMBER_IN_UV) {
2620 /* It's definitely an integer, only upgrade to PVIV */
2621 if (SvTYPE(sv) < SVt_PVIV)
2622 sv_upgrade(sv, SVt_PVIV);
2624 } else if (SvTYPE(sv) < SVt_PVNV)
2625 sv_upgrade(sv, SVt_PVNV);
2627 /* If NV preserves UV then we only use the UV value if we know that
2628 we aren't going to call atof() below. If NVs don't preserve UVs
2629 then the value returned may have more precision than atof() will
2630 return, even though value isn't perfectly accurate. */
2631 if ((numtype & (IS_NUMBER_IN_UV
2632 #ifdef NV_PRESERVES_UV
2635 )) == IS_NUMBER_IN_UV) {
2636 /* This won't turn off the public IOK flag if it was set above */
2637 (void)SvIOKp_on(sv);
2639 if (!(numtype & IS_NUMBER_NEG)) {
2641 if (value <= (UV)IV_MAX) {
2642 SvIV_set(sv, (IV)value);
2644 SvUV_set(sv, value);
2648 /* 2s complement assumption */
2649 if (value <= (UV)IV_MIN) {
2650 SvIV_set(sv, -(IV)value);
2652 /* Too negative for an IV. This is a double upgrade, but
2653 I'm assuming it will be rare. */
2654 if (SvTYPE(sv) < SVt_PVNV)
2655 sv_upgrade(sv, SVt_PVNV);
2659 SvNV_set(sv, -(NV)value);
2660 SvIV_set(sv, IV_MIN);
2664 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2665 will be in the previous block to set the IV slot, and the next
2666 block to set the NV slot. So no else here. */
2668 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2669 != IS_NUMBER_IN_UV) {
2670 /* It wasn't an (integer that doesn't overflow the UV). */
2671 SvNV_set(sv, Atof(SvPVX(sv)));
2673 if (! numtype && ckWARN(WARN_NUMERIC))
2676 #if defined(USE_LONG_DOUBLE)
2677 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
2678 PTR2UV(sv), SvNVX(sv)));
2680 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
2681 PTR2UV(sv), SvNVX(sv)));
2685 #ifdef NV_PRESERVES_UV
2686 (void)SvIOKp_on(sv);
2688 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2689 SvIV_set(sv, I_V(SvNVX(sv)));
2690 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2693 /* Integer is imprecise. NOK, IOKp */
2695 /* UV will not work better than IV */
2697 if (SvNVX(sv) > (NV)UV_MAX) {
2699 /* Integer is inaccurate. NOK, IOKp, is UV */
2700 SvUV_set(sv, UV_MAX);
2703 SvUV_set(sv, U_V(SvNVX(sv)));
2704 /* 0xFFFFFFFFFFFFFFFF not an issue in here */
2705 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2709 /* Integer is imprecise. NOK, IOKp, is UV */
2715 #else /* NV_PRESERVES_UV */
2716 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2717 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2718 /* The IV slot will have been set from value returned by
2719 grok_number above. The NV slot has just been set using
2722 assert (SvIOKp(sv));
2724 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2725 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2726 /* Small enough to preserve all bits. */
2727 (void)SvIOKp_on(sv);
2729 SvIV_set(sv, I_V(SvNVX(sv)));
2730 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2732 /* Assumption: first non-preserved integer is < IV_MAX,
2733 this NV is in the preserved range, therefore: */
2734 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2736 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);
2740 0 0 already failed to read UV.
2741 0 1 already failed to read UV.
2742 1 0 you won't get here in this case. IV/UV
2743 slot set, public IOK, Atof() unneeded.
2744 1 1 already read UV.
2745 so there's no point in sv_2iuv_non_preserve() attempting
2746 to use atol, strtol, strtoul etc. */
2747 if (sv_2iuv_non_preserve (sv, numtype)
2748 >= IS_NUMBER_OVERFLOW_IV)
2752 #endif /* NV_PRESERVES_UV */
2755 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
2757 if (SvTYPE(sv) < SVt_IV)
2758 /* Typically the caller expects that sv_any is not NULL now. */
2759 sv_upgrade(sv, SVt_IV);
2762 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2763 PTR2UV(sv),SvIVX(sv)));
2764 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
2767 /* sv_2uv() is now a macro using Perl_sv_2uv_flags();
2768 * this function provided for binary compatibility only
2772 Perl_sv_2uv(pTHX_ register SV *sv)
2774 return sv_2uv_flags(sv, SV_GMAGIC);
2778 =for apidoc sv_2uv_flags
2780 Return the unsigned integer value of an SV, doing any necessary string
2781 conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2782 Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
2788 Perl_sv_2uv_flags(pTHX_ register SV *sv, I32 flags)
2792 if (SvGMAGICAL(sv)) {
2793 if (flags & SV_GMAGIC)
2798 return U_V(SvNVX(sv));
2799 if (SvPOKp(sv) && SvLEN(sv))
2802 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2803 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2809 if (SvTHINKFIRST(sv)) {
2812 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
2813 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
2814 return SvUV(tmpstr);
2815 return PTR2UV(SvRV(sv));
2818 sv_force_normal_flags(sv, 0);
2820 if (SvREADONLY(sv) && !SvOK(sv)) {
2821 if (ckWARN(WARN_UNINITIALIZED))
2831 return (UV)SvIVX(sv);
2835 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2836 * without also getting a cached IV/UV from it at the same time
2837 * (ie PV->NV conversion should detect loss of accuracy and cache
2838 * IV or UV at same time to avoid this. */
2839 /* IV-over-UV optimisation - choose to cache IV if possible */
2841 if (SvTYPE(sv) == SVt_NV)
2842 sv_upgrade(sv, SVt_PVNV);
2844 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2845 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2846 SvIV_set(sv, I_V(SvNVX(sv)));
2847 if (SvNVX(sv) == (NV) SvIVX(sv)
2848 #ifndef NV_PRESERVES_UV
2849 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2850 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2851 /* Don't flag it as "accurately an integer" if the number
2852 came from a (by definition imprecise) NV operation, and
2853 we're outside the range of NV integer precision */
2856 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2857 DEBUG_c(PerlIO_printf(Perl_debug_log,
2858 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (precise)\n",
2864 /* IV not precise. No need to convert from PV, as NV
2865 conversion would already have cached IV if it detected
2866 that PV->IV would be better than PV->NV->IV
2867 flags already correct - don't set public IOK. */
2868 DEBUG_c(PerlIO_printf(Perl_debug_log,
2869 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (imprecise)\n",
2874 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2875 but the cast (NV)IV_MIN rounds to a the value less (more
2876 negative) than IV_MIN which happens to be equal to SvNVX ??
2877 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2878 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2879 (NV)UVX == NVX are both true, but the values differ. :-(
2880 Hopefully for 2s complement IV_MIN is something like
2881 0x8000000000000000 which will be exact. NWC */
2884 SvUV_set(sv, U_V(SvNVX(sv)));
2886 (SvNVX(sv) == (NV) SvUVX(sv))
2887 #ifndef NV_PRESERVES_UV
2888 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2889 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2890 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2891 /* Don't flag it as "accurately an integer" if the number
2892 came from a (by definition imprecise) NV operation, and
2893 we're outside the range of NV integer precision */
2898 DEBUG_c(PerlIO_printf(Perl_debug_log,
2899 "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n",
2905 else if (SvPOKp(sv) && SvLEN(sv)) {
2907 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2909 /* We want to avoid a possible problem when we cache a UV which
2910 may be later translated to an NV, and the resulting NV is not
2911 the translation of the initial data.
2913 This means that if we cache such a UV, we need to cache the
2914 NV as well. Moreover, we trade speed for space, and do not
2915 cache the NV if not needed.
2918 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2919 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2920 == IS_NUMBER_IN_UV) {
2921 /* It's definitely an integer, only upgrade to PVIV */
2922 if (SvTYPE(sv) < SVt_PVIV)
2923 sv_upgrade(sv, SVt_PVIV);
2925 } else if (SvTYPE(sv) < SVt_PVNV)
2926 sv_upgrade(sv, SVt_PVNV);
2928 /* If NV preserves UV then we only use the UV value if we know that
2929 we aren't going to call atof() below. If NVs don't preserve UVs
2930 then the value returned may have more precision than atof() will
2931 return, even though it isn't accurate. */
2932 if ((numtype & (IS_NUMBER_IN_UV
2933 #ifdef NV_PRESERVES_UV
2936 )) == IS_NUMBER_IN_UV) {
2937 /* This won't turn off the public IOK flag if it was set above */
2938 (void)SvIOKp_on(sv);
2940 if (!(numtype & IS_NUMBER_NEG)) {
2942 if (value <= (UV)IV_MAX) {
2943 SvIV_set(sv, (IV)value);
2945 /* it didn't overflow, and it was positive. */
2946 SvUV_set(sv, value);
2950 /* 2s complement assumption */
2951 if (value <= (UV)IV_MIN) {
2952 SvIV_set(sv, -(IV)value);
2954 /* Too negative for an IV. This is a double upgrade, but
2955 I'm assuming it will be rare. */
2956 if (SvTYPE(sv) < SVt_PVNV)
2957 sv_upgrade(sv, SVt_PVNV);
2961 SvNV_set(sv, -(NV)value);
2962 SvIV_set(sv, IV_MIN);
2967 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2968 != IS_NUMBER_IN_UV) {
2969 /* It wasn't an integer, or it overflowed the UV. */
2970 SvNV_set(sv, Atof(SvPVX(sv)));
2972 if (! numtype && ckWARN(WARN_NUMERIC))
2975 #if defined(USE_LONG_DOUBLE)
2976 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n",
2977 PTR2UV(sv), SvNVX(sv)));
2979 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"NVgf")\n",
2980 PTR2UV(sv), SvNVX(sv)));
2983 #ifdef NV_PRESERVES_UV
2984 (void)SvIOKp_on(sv);
2986 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2987 SvIV_set(sv, I_V(SvNVX(sv)));
2988 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2991 /* Integer is imprecise. NOK, IOKp */
2993 /* UV will not work better than IV */
2995 if (SvNVX(sv) > (NV)UV_MAX) {
2997 /* Integer is inaccurate. NOK, IOKp, is UV */
2998 SvUV_set(sv, UV_MAX);
3001 SvUV_set(sv, U_V(SvNVX(sv)));
3002 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
3003 NV preservse UV so can do correct comparison. */
3004 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
3008 /* Integer is imprecise. NOK, IOKp, is UV */
3013 #else /* NV_PRESERVES_UV */
3014 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3015 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
3016 /* The UV slot will have been set from value returned by
3017 grok_number above. The NV slot has just been set using
3020 assert (SvIOKp(sv));
3022 if (((UV)1 << NV_PRESERVES_UV_BITS) >
3023 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
3024 /* Small enough to preserve all bits. */
3025 (void)SvIOKp_on(sv);
3027 SvIV_set(sv, I_V(SvNVX(sv)));
3028 if ((NV)(SvIVX(sv)) == SvNVX(sv))
3030 /* Assumption: first non-preserved integer is < IV_MAX,
3031 this NV is in the preserved range, therefore: */
3032 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
3034 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);
3037 sv_2iuv_non_preserve (sv, numtype);
3039 #endif /* NV_PRESERVES_UV */
3043 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
3044 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
3047 if (SvTYPE(sv) < SVt_IV)
3048 /* Typically the caller expects that sv_any is not NULL now. */
3049 sv_upgrade(sv, SVt_IV);
3053 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
3054 PTR2UV(sv),SvUVX(sv)));
3055 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
3061 Return the num value of an SV, doing any necessary string or integer
3062 conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
3069 Perl_sv_2nv(pTHX_ register SV *sv)
3073 if (SvGMAGICAL(sv)) {
3077 if (SvPOKp(sv) && SvLEN(sv)) {
3078 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) &&
3079 !grok_number(SvPVX_const(sv), SvCUR(sv), NULL))
3081 return Atof(SvPVX(sv));
3085 return (NV)SvUVX(sv);
3087 return (NV)SvIVX(sv);
3090 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
3091 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
3097 if (SvTHINKFIRST(sv)) {
3100 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
3101 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
3102 return SvNV(tmpstr);
3103 return PTR2NV(SvRV(sv));
3106 sv_force_normal_flags(sv, 0);
3108 if (SvREADONLY(sv) && !SvOK(sv)) {
3109 if (ckWARN(WARN_UNINITIALIZED))
3114 if (SvTYPE(sv) < SVt_NV) {
3115 if (SvTYPE(sv) == SVt_IV)
3116 sv_upgrade(sv, SVt_PVNV);
3118 sv_upgrade(sv, SVt_NV);
3119 #ifdef USE_LONG_DOUBLE
3121 STORE_NUMERIC_LOCAL_SET_STANDARD();
3122 PerlIO_printf(Perl_debug_log,
3123 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
3124 PTR2UV(sv), SvNVX(sv));
3125 RESTORE_NUMERIC_LOCAL();
3129 STORE_NUMERIC_LOCAL_SET_STANDARD();
3130 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
3131 PTR2UV(sv), SvNVX(sv));
3132 RESTORE_NUMERIC_LOCAL();
3136 else if (SvTYPE(sv) < SVt_PVNV)
3137 sv_upgrade(sv, SVt_PVNV);
3142 SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
3143 #ifdef NV_PRESERVES_UV
3146 /* Only set the public NV OK flag if this NV preserves the IV */
3147 /* Check it's not 0xFFFFFFFFFFFFFFFF */
3148 if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
3149 : (SvIVX(sv) == I_V(SvNVX(sv))))
3155 else if (SvPOKp(sv) && SvLEN(sv)) {
3157 const int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
3158 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !numtype)
3160 #ifdef NV_PRESERVES_UV
3161 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3162 == IS_NUMBER_IN_UV) {
3163 /* It's definitely an integer */
3164 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
3166 SvNV_set(sv, Atof(SvPVX(sv)));
3169 SvNV_set(sv, Atof(SvPVX(sv)));
3170 /* Only set the public NV OK flag if this NV preserves the value in
3171 the PV at least as well as an IV/UV would.
3172 Not sure how to do this 100% reliably. */
3173 /* if that shift count is out of range then Configure's test is
3174 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
3176 if (((UV)1 << NV_PRESERVES_UV_BITS) >
3177 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
3178 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
3179 } else if (!(numtype & IS_NUMBER_IN_UV)) {
3180 /* Can't use strtol etc to convert this string, so don't try.
3181 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
3184 /* value has been set. It may not be precise. */
3185 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
3186 /* 2s complement assumption for (UV)IV_MIN */
3187 SvNOK_on(sv); /* Integer is too negative. */
3192 if (numtype & IS_NUMBER_NEG) {
3193 SvIV_set(sv, -(IV)value);
3194 } else if (value <= (UV)IV_MAX) {
3195 SvIV_set(sv, (IV)value);
3197 SvUV_set(sv, value);
3201 if (numtype & IS_NUMBER_NOT_INT) {
3202 /* I believe that even if the original PV had decimals,
3203 they are lost beyond the limit of the FP precision.
3204 However, neither is canonical, so both only get p
3205 flags. NWC, 2000/11/25 */
3206 /* Both already have p flags, so do nothing */
3209 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
3210 if (SvIVX(sv) == I_V(nv)) {
3215 /* It had no "." so it must be integer. */
3218 /* between IV_MAX and NV(UV_MAX).
3219 Could be slightly > UV_MAX */
3221 if (numtype & IS_NUMBER_NOT_INT) {
3222 /* UV and NV both imprecise. */
3224 UV nv_as_uv = U_V(nv);
3226 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
3237 #endif /* NV_PRESERVES_UV */
3240 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
3242 if (SvTYPE(sv) < SVt_NV)
3243 /* Typically the caller expects that sv_any is not NULL now. */
3244 /* XXX Ilya implies that this is a bug in callers that assume this
3245 and ideally should be fixed. */
3246 sv_upgrade(sv, SVt_NV);
3249 #if defined(USE_LONG_DOUBLE)
3251 STORE_NUMERIC_LOCAL_SET_STANDARD();
3252 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
3253 PTR2UV(sv), SvNVX(sv));
3254 RESTORE_NUMERIC_LOCAL();
3258 STORE_NUMERIC_LOCAL_SET_STANDARD();
3259 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
3260 PTR2UV(sv), SvNVX(sv));
3261 RESTORE_NUMERIC_LOCAL();
3267 /* asIV(): extract an integer from the string value of an SV.
3268 * Caller must validate PVX */
3271 S_asIV(pTHX_ SV *sv)
3274 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
3276 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3277 == IS_NUMBER_IN_UV) {
3278 /* It's definitely an integer */
3279 if (numtype & IS_NUMBER_NEG) {
3280 if (value < (UV)IV_MIN)
3283 if (value < (UV)IV_MAX)
3288 if (ckWARN(WARN_NUMERIC))
3291 return I_V(Atof(SvPVX(sv)));
3294 /* asUV(): extract an unsigned integer from the string value of an SV
3295 * Caller must validate PVX */
3298 S_asUV(pTHX_ SV *sv)
3301 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
3303 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3304 == IS_NUMBER_IN_UV) {
3305 /* It's definitely an integer */
3306 if (!(numtype & IS_NUMBER_NEG))
3310 if (ckWARN(WARN_NUMERIC))
3313 return U_V(Atof(SvPVX(sv)));
3317 =for apidoc sv_2pv_nolen
3319 Like C<sv_2pv()>, but doesn't return the length too. You should usually
3320 use the macro wrapper C<SvPV_nolen(sv)> instead.
3325 Perl_sv_2pv_nolen(pTHX_ register SV *sv)
3328 return sv_2pv(sv, &n_a);
3331 /* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
3332 * UV as a string towards the end of buf, and return pointers to start and
3335 * We assume that buf is at least TYPE_CHARS(UV) long.
3339 uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
3341 char *ptr = buf + TYPE_CHARS(UV);
3355 *--ptr = '0' + (char)(uv % 10);
3363 /* sv_2pv() is now a macro using Perl_sv_2pv_flags();
3364 * this function provided for binary compatibility only
3368 Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
3370 return sv_2pv_flags(sv, lp, SV_GMAGIC);
3374 =for apidoc sv_2pv_flags
3376 Returns a pointer to the string value of an SV, and sets *lp to its length.
3377 If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
3379 Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
3380 usually end up here too.
3386 Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
3391 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
3392 char *tmpbuf = tbuf;
3398 if (SvGMAGICAL(sv)) {
3399 if (flags & SV_GMAGIC)
3407 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
3409 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
3414 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
3419 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
3420 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
3427 if (SvTHINKFIRST(sv)) {
3430 register const char *typestr;
3431 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
3432 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
3433 char *pv = SvPV(tmpstr, *lp);
3443 typestr = "NULLREF";
3447 switch (SvTYPE(sv)) {
3449 if ( ((SvFLAGS(sv) &
3450 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
3451 == (SVs_OBJECT|SVs_SMG))
3452 && (mg = mg_find(sv, PERL_MAGIC_qr))) {
3453 const regexp *re = (regexp *)mg->mg_obj;
3456 const char *fptr = "msix";
3461 char need_newline = 0;
3462 U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12);
3464 while((ch = *fptr++)) {
3466 reflags[left++] = ch;
3469 reflags[right--] = ch;
3474 reflags[left] = '-';
3478 mg->mg_len = re->prelen + 4 + left;
3480 * If /x was used, we have to worry about a regex
3481 * ending with a comment later being embedded
3482 * within another regex. If so, we don't want this
3483 * regex's "commentization" to leak out to the
3484 * right part of the enclosing regex, we must cap
3485 * it with a newline.
3487 * So, if /x was used, we scan backwards from the
3488 * end of the regex. If we find a '#' before we
3489 * find a newline, we need to add a newline
3490 * ourself. If we find a '\n' first (or if we
3491 * don't find '#' or '\n'), we don't need to add
3492 * anything. -jfriedl
3494 if (PMf_EXTENDED & re->reganch)
3496 const char *endptr = re->precomp + re->prelen;
3497 while (endptr >= re->precomp)
3499 const char c = *(endptr--);
3501 break; /* don't need another */
3503 /* we end while in a comment, so we
3505 mg->mg_len++; /* save space for it */
3506 need_newline = 1; /* note to add it */
3512 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
3513 Copy("(?", mg->mg_ptr, 2, char);
3514 Copy(reflags, mg->mg_ptr+2, left, char);
3515 Copy(":", mg->mg_ptr+left+2, 1, char);
3516 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
3518 mg->mg_ptr[mg->mg_len - 2] = '\n';
3519 mg->mg_ptr[mg->mg_len - 1] = ')';
3520 mg->mg_ptr[mg->mg_len] = 0;
3522 PL_reginterp_cnt += re->program[0].next_off;
3524 if (re->reganch & ROPT_UTF8)
3539 case SVt_PVBM: typestr = SvROK(sv) ? "REF" : "SCALAR"; break;
3540 case SVt_PVLV: typestr = SvROK(sv) ? "REF"
3541 /* tied lvalues should appear to be
3542 * scalars for backwards compatitbility */
3543 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
3544 ? "SCALAR" : "LVALUE"; break;
3545 case SVt_PVAV: typestr = "ARRAY"; break;
3546 case SVt_PVHV: typestr = "HASH"; break;
3547 case SVt_PVCV: typestr = "CODE"; break;
3548 case SVt_PVGV: typestr = "GLOB"; break;
3549 case SVt_PVFM: typestr = "FORMAT"; break;
3550 case SVt_PVIO: typestr = "IO"; break;
3551 default: typestr = "UNKNOWN"; break;
3555 const char *name = HvNAME_get(SvSTASH(sv));
3556 Perl_sv_setpvf(aTHX_ tsv, "%s=%s(0x%"UVxf")",
3557 name ? name : "__ANON__" , typestr, PTR2UV(sv));
3560 Perl_sv_setpvf(aTHX_ tsv, "%s(0x%"UVxf")", typestr, PTR2UV(sv));
3563 *lp = strlen(typestr);
3564 return (char *)typestr;
3566 if (SvREADONLY(sv) && !SvOK(sv)) {
3567 if (ckWARN(WARN_UNINITIALIZED))
3573 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
3574 /* I'm assuming that if both IV and NV are equally valid then
3575 converting the IV is going to be more efficient */
3576 const U32 isIOK = SvIOK(sv);
3577 const U32 isUIOK = SvIsUV(sv);
3578 char buf[TYPE_CHARS(UV)];
3581 if (SvTYPE(sv) < SVt_PVIV)
3582 sv_upgrade(sv, SVt_PVIV);
3584 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
3586 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
3587 SvGROW(sv, (STRLEN)(ebuf - ptr + 1)); /* inlined from sv_setpvn */
3588 Move(ptr,SvPVX(sv),ebuf - ptr,char);
3589 SvCUR_set(sv, ebuf - ptr);
3599 else if (SvNOKp(sv)) {
3600 if (SvTYPE(sv) < SVt_PVNV)
3601 sv_upgrade(sv, SVt_PVNV);
3602 /* The +20 is pure guesswork. Configure test needed. --jhi */
3603 SvGROW(sv, NV_DIG + 20);
3605 olderrno = errno; /* some Xenix systems wipe out errno here */
3607 if (SvNVX(sv) == 0.0)
3608 (void)strcpy(s,"0");
3612 Gconvert(SvNVX(sv), NV_DIG, 0, s);
3615 #ifdef FIXNEGATIVEZERO
3616 if (*s == '-' && s[1] == '0' && !s[2])
3626 if (ckWARN(WARN_UNINITIALIZED)
3627 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
3630 if (SvTYPE(sv) < SVt_PV)
3631 /* Typically the caller expects that sv_any is not NULL now. */
3632 sv_upgrade(sv, SVt_PV);
3635 *lp = s - SvPVX(sv);
3638 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3639 PTR2UV(sv),SvPVX(sv)));
3643 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
3644 /* Sneaky stuff here */
3648 tsv = newSVpv(tmpbuf, 0);
3665 len = strlen(tmpbuf);
3667 #ifdef FIXNEGATIVEZERO
3668 if (len == 2 && t[0] == '-' && t[1] == '0') {
3673 (void)SvUPGRADE(sv, SVt_PV);
3675 s = SvGROW(sv, len + 1);
3678 return strcpy(s, t);
3683 =for apidoc sv_copypv
3685 Copies a stringified representation of the source SV into the
3686 destination SV. Automatically performs any necessary mg_get and
3687 coercion of numeric values into strings. Guaranteed to preserve
3688 UTF-8 flag even from overloaded objects. Similar in nature to
3689 sv_2pv[_flags] but operates directly on an SV instead of just the
3690 string. Mostly uses sv_2pv_flags to do its work, except when that
3691 would lose the UTF-8'ness of the PV.
3697 Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv)
3702 sv_setpvn(dsv,s,len);
3710 =for apidoc sv_2pvbyte_nolen
3712 Return a pointer to the byte-encoded representation of the SV.
3713 May cause the SV to be downgraded from UTF-8 as a side-effect.
3715 Usually accessed via the C<SvPVbyte_nolen> macro.
3721 Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
3724 return sv_2pvbyte(sv, &n_a);
3728 =for apidoc sv_2pvbyte
3730 Return a pointer to the byte-encoded representation of the SV, and set *lp
3731 to its length. May cause the SV to be downgraded from UTF-8 as a
3734 Usually accessed via the C<SvPVbyte> macro.
3740 Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
3742 sv_utf8_downgrade(sv,0);
3743 return SvPV(sv,*lp);
3747 =for apidoc sv_2pvutf8_nolen
3749 Return a pointer to the UTF-8-encoded representation of the SV.
3750 May cause the SV to be upgraded to UTF-8 as a side-effect.
3752 Usually accessed via the C<SvPVutf8_nolen> macro.
3758 Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
3761 return sv_2pvutf8(sv, &n_a);
3765 =for apidoc sv_2pvutf8
3767 Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
3768 to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
3770 Usually accessed via the C<SvPVutf8> macro.
3776 Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
3778 sv_utf8_upgrade(sv);
3779 return SvPV(sv,*lp);
3783 =for apidoc sv_2bool
3785 This function is only called on magical items, and is only used by
3786 sv_true() or its macro equivalent.
3792 Perl_sv_2bool(pTHX_ register SV *sv)
3801 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
3802 (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
3803 return (bool)SvTRUE(tmpsv);
3804 return SvRV(sv) != 0;
3807 register XPV* Xpvtmp;
3808 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
3809 (*sv->sv_u.svu_pv > '0' ||
3810 Xpvtmp->xpv_cur > 1 ||
3811 (Xpvtmp->xpv_cur && *sv->sv_u.svu_pv != '0')))
3818 return SvIVX(sv) != 0;
3821 return SvNVX(sv) != 0.0;
3828 /* sv_utf8_upgrade() is now a macro using sv_utf8_upgrade_flags();
3829 * this function provided for binary compatibility only
3834 Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
3836 return sv_utf8_upgrade_flags(sv, SV_GMAGIC);
3840 =for apidoc sv_utf8_upgrade
3842 Converts the PV of an SV to its UTF-8-encoded form.
3843 Forces the SV to string form if it is not already.
3844 Always sets the SvUTF8 flag to avoid future validity checks even
3845 if all the bytes have hibit clear.
3847 This is not as a general purpose byte encoding to Unicode interface:
3848 use the Encode extension for that.
3850 =for apidoc sv_utf8_upgrade_flags
3852 Converts the PV of an SV to its UTF-8-encoded form.
3853 Forces the SV to string form if it is not already.
3854 Always sets the SvUTF8 flag to avoid future validity checks even
3855 if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
3856 will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
3857 C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
3859 This is not as a general purpose byte encoding to Unicode interface:
3860 use the Encode extension for that.
3866 Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags)
3868 if (sv == &PL_sv_undef)
3872 if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
3873 (void) sv_2pv_flags(sv,&len, flags);
3877 (void) SvPV_force(sv,len);
3886 sv_force_normal_flags(sv, 0);
3889 if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING))
3890 sv_recode_to_utf8(sv, PL_encoding);
3891 else { /* Assume Latin-1/EBCDIC */
3892 /* This function could be much more efficient if we
3893 * had a FLAG in SVs to signal if there are any hibit
3894 * chars in the PV. Given that there isn't such a flag
3895 * make the loop as fast as possible. */
3896 U8 *s = (U8 *) SvPVX(sv);
3897 U8 *e = (U8 *) SvEND(sv);
3903 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
3907 STRLEN len = SvCUR(sv) + 1; /* Plus the \0 */
3908 s = bytes_to_utf8((U8*)s, &len);
3910 SvPV_free(sv); /* No longer using what was there before. */
3912 SvPV_set(sv, (char*)s);
3913 SvCUR_set(sv, len - 1);
3914 SvLEN_set(sv, len); /* No longer know the real size. */
3916 /* Mark as UTF-8 even if no hibit - saves scanning loop */
3923 =for apidoc sv_utf8_downgrade
3925 Attempts to convert the PV of an SV from characters to bytes.
3926 If the PV contains a character beyond byte, this conversion will fail;
3927 in this case, either returns false or, if C<fail_ok> is not
3930 This is not as a general purpose Unicode to byte encoding interface:
3931 use the Encode extension for that.
3937 Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
3939 if (SvPOKp(sv) && SvUTF8(sv)) {
3945 sv_force_normal_flags(sv, 0);
3947 s = (U8 *) SvPV(sv, len);
3948 if (!utf8_to_bytes(s, &len)) {
3953 Perl_croak(aTHX_ "Wide character in %s",
3956 Perl_croak(aTHX_ "Wide character");
3967 =for apidoc sv_utf8_encode
3969 Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
3970 flag off so that it looks like octets again.
3976 Perl_sv_utf8_encode(pTHX_ register SV *sv)
3978 (void) sv_utf8_upgrade(sv);
3980 sv_force_normal_flags(sv, 0);
3982 if (SvREADONLY(sv)) {
3983 Perl_croak(aTHX_ PL_no_modify);
3989 =for apidoc sv_utf8_decode
3991 If the PV of the SV is an octet sequence in UTF-8
3992 and contains a multiple-byte character, the C<SvUTF8> flag is turned on
3993 so that it looks like a character. If the PV contains only single-byte
3994 characters, the C<SvUTF8> flag stays being off.
3995 Scans PV for validity and returns false if the PV is invalid UTF-8.
4001 Perl_sv_utf8_decode(pTHX_ register SV *sv)
4007 /* The octets may have got themselves encoded - get them back as
4010 if (!sv_utf8_downgrade(sv, TRUE))
4013 /* it is actually just a matter of turning the utf8 flag on, but
4014 * we want to make sure everything inside is valid utf8 first.
4016 c = (U8 *) SvPVX(sv);
4017 if (!is_utf8_string(c, SvCUR(sv)+1))
4019 e = (U8 *) SvEND(sv);
4022 if (!UTF8_IS_INVARIANT(ch)) {
4031 /* sv_setsv() is now a macro using Perl_sv_setsv_flags();
4032 * this function provided for binary compatibility only
4036 Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
4038 sv_setsv_flags(dstr, sstr, SV_GMAGIC);
4042 =for apidoc sv_setsv
4044 Copies the contents of the source SV C<ssv> into the destination SV
4045 C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
4046 function if the source SV needs to be reused. Does not handle 'set' magic.
4047 Loosely speaking, it performs a copy-by-value, obliterating any previous
4048 content of the destination.
4050 You probably want to use one of the assortment of wrappers, such as
4051 C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
4052 C<SvSetMagicSV_nosteal>.
4054 =for apidoc sv_setsv_flags
4056 Copies the contents of the source SV C<ssv> into the destination SV
4057 C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
4058 function if the source SV needs to be reused. Does not handle 'set' magic.
4059 Loosely speaking, it performs a copy-by-value, obliterating any previous
4060 content of the destination.
4061 If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
4062 C<ssv> if appropriate, else not. If the C<flags> parameter has the
4063 C<NOSTEAL> bit set then the buffers of temps will not be stolen. <sv_setsv>
4064 and C<sv_setsv_nomg> are implemented in terms of this function.
4066 You probably want to use one of the assortment of wrappers, such as
4067 C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
4068 C<SvSetMagicSV_nosteal>.
4070 This is the primary function for copying scalars, and most other
4071 copy-ish functions and macros use this underneath.
4077 Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags)
4079 register U32 sflags;
4085 SV_CHECK_THINKFIRST_COW_DROP(dstr);
4087 sstr = &PL_sv_undef;
4088 stype = SvTYPE(sstr);
4089 dtype = SvTYPE(dstr);
4094 /* need to nuke the magic */
4096 SvRMAGICAL_off(dstr);
4099 /* There's a lot of redundancy below but we're going for speed here */
4104 if (dtype != SVt_PVGV) {
4105 (void)SvOK_off(dstr);
4113 sv_upgrade(dstr, SVt_IV);
4116 sv_upgrade(dstr, SVt_PVNV);
4120 sv_upgrade(dstr, SVt_PVIV);
4123 (void)SvIOK_only(dstr);
4124 SvIV_set(dstr, SvIVX(sstr));
4127 if (SvTAINTED(sstr))
4138 sv_upgrade(dstr, SVt_NV);
4143 sv_upgrade(dstr, SVt_PVNV);
4146 SvNV_set(dstr, SvNVX(sstr));
4147 (void)SvNOK_only(dstr);
4148 if (SvTAINTED(sstr))
4156 sv_upgrade(dstr, SVt_RV);
4157 else if (dtype == SVt_PVGV &&
4158 SvROK(sstr) && SvTYPE(SvRV(sstr)) == SVt_PVGV) {
4161 if (GvIMPORTED(dstr) != GVf_IMPORTED
4162 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4164 GvIMPORTED_on(dstr);
4173 #ifdef PERL_COPY_ON_WRITE
4174 if ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS) {
4175 if (dtype < SVt_PVIV)
4176 sv_upgrade(dstr, SVt_PVIV);
4183 sv_upgrade(dstr, SVt_PV);
4186 if (dtype < SVt_PVIV)
4187 sv_upgrade(dstr, SVt_PVIV);
4190 if (dtype < SVt_PVNV)
4191 sv_upgrade(dstr, SVt_PVNV);
4198 const char * const type = sv_reftype(sstr,0);
4200 Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_NAME(PL_op));
4202 Perl_croak(aTHX_ "Bizarre copy of %s", type);
4207 if (dtype <= SVt_PVGV) {
4209 if (dtype != SVt_PVGV) {
4210 const char * const name = GvNAME(sstr);
4211 const STRLEN len = GvNAMELEN(sstr);
4212 /* don't upgrade SVt_PVLV: it can hold a glob */
4213 if (dtype != SVt_PVLV)
4214 sv_upgrade(dstr, SVt_PVGV);
4215 sv_magic(dstr, dstr, PERL_MAGIC_glob, Nullch, 0);
4216 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
4217 GvNAME(dstr) = savepvn(name, len);
4218 GvNAMELEN(dstr) = len;
4219 SvFAKE_on(dstr); /* can coerce to non-glob */
4221 /* ahem, death to those who redefine active sort subs */
4222 else if (PL_curstackinfo->si_type == PERLSI_SORT
4223 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
4224 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
4227 #ifdef GV_UNIQUE_CHECK
4228 if (GvUNIQUE((GV*)dstr)) {
4229 Perl_croak(aTHX_ PL_no_modify);
4233 (void)SvOK_off(dstr);
4234 GvINTRO_off(dstr); /* one-shot flag */
4236 GvGP(dstr) = gp_ref(GvGP(sstr));
4237 if (SvTAINTED(sstr))
4239 if (GvIMPORTED(dstr) != GVf_IMPORTED
4240 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4242 GvIMPORTED_on(dstr);
4250 if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
4252 if ((int)SvTYPE(sstr) != stype) {
4253 stype = SvTYPE(sstr);
4254 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
4258 if (stype == SVt_PVLV)
4259 (void)SvUPGRADE(dstr, SVt_PVNV);
4261 (void)SvUPGRADE(dstr, (U32)stype);
4264 sflags = SvFLAGS(sstr);
4266 if (sflags & SVf_ROK) {
4267 if (dtype >= SVt_PV) {
4268 if (dtype == SVt_PVGV) {
4269 SV *sref = SvREFCNT_inc(SvRV(sstr));
4271 const int intro = GvINTRO(dstr);
4273 #ifdef GV_UNIQUE_CHECK
4274 if (GvUNIQUE((GV*)dstr)) {
4275 Perl_croak(aTHX_ PL_no_modify);
4280 GvINTRO_off(dstr); /* one-shot flag */
4281 GvLINE(dstr) = CopLINE(PL_curcop);
4282 GvEGV(dstr) = (GV*)dstr;
4285 switch (SvTYPE(sref)) {
4288 SAVEGENERICSV(GvAV(dstr));
4290 dref = (SV*)GvAV(dstr);
4291 GvAV(dstr) = (AV*)sref;
4292 if (!GvIMPORTED_AV(dstr)
4293 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4295 GvIMPORTED_AV_on(dstr);
4300 SAVEGENERICSV(GvHV(dstr));
4302 dref = (SV*)GvHV(dstr);
4303 GvHV(dstr) = (HV*)sref;
4304 if (!GvIMPORTED_HV(dstr)
4305 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4307 GvIMPORTED_HV_on(dstr);
4312 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
4313 SvREFCNT_dec(GvCV(dstr));
4314 GvCV(dstr) = Nullcv;
4315 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
4316 PL_sub_generation++;
4318 SAVEGENERICSV(GvCV(dstr));
4321 dref = (SV*)GvCV(dstr);
4322 if (GvCV(dstr) != (CV*)sref) {
4323 CV* cv = GvCV(dstr);
4325 if (!GvCVGEN((GV*)dstr) &&
4326 (CvROOT(cv) || CvXSUB(cv)))
4328 /* ahem, death to those who redefine
4329 * active sort subs */
4330 if (PL_curstackinfo->si_type == PERLSI_SORT &&
4331 PL_sortcop == CvSTART(cv))
4333 "Can't redefine active sort subroutine %s",
4334 GvENAME((GV*)dstr));
4335 /* Redefining a sub - warning is mandatory if
4336 it was a const and its value changed. */
4337 if (ckWARN(WARN_REDEFINE)
4339 && (!CvCONST((CV*)sref)
4340 || sv_cmp(cv_const_sv(cv),
4341 cv_const_sv((CV*)sref)))))
4343 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
4345 ? "Constant subroutine %s::%s redefined"
4346 : "Subroutine %s::%s redefined",
4347 HvNAME_get(GvSTASH((GV*)dstr)),
4348 GvENAME((GV*)dstr));
4352 cv_ckproto(cv, (GV*)dstr,
4353 SvPOK(sref) ? SvPVX(sref) : Nullch);
4355 GvCV(dstr) = (CV*)sref;
4356 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
4357 GvASSUMECV_on(dstr);
4358 PL_sub_generation++;
4360 if (!GvIMPORTED_CV(dstr)
4361 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4363 GvIMPORTED_CV_on(dstr);
4368 SAVEGENERICSV(GvIOp(dstr));
4370 dref = (SV*)GvIOp(dstr);
4371 GvIOp(dstr) = (IO*)sref;
4375 SAVEGENERICSV(GvFORM(dstr));
4377 dref = (SV*)GvFORM(dstr);
4378 GvFORM(dstr) = (CV*)sref;
4382 SAVEGENERICSV(GvSV(dstr));
4384 dref = (SV*)GvSV(dstr);
4386 if (!GvIMPORTED_SV(dstr)
4387 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4389 GvIMPORTED_SV_on(dstr);
4395 if (SvTAINTED(sstr))
4405 (void)SvOK_off(dstr);
4406 SvRV_set(dstr, SvREFCNT_inc(SvRV(sstr)));
4408 if (sflags & SVp_NOK) {
4410 /* Only set the public OK flag if the source has public OK. */
4411 if (sflags & SVf_NOK)
4412 SvFLAGS(dstr) |= SVf_NOK;
4413 SvNV_set(dstr, SvNVX(sstr));
4415 if (sflags & SVp_IOK) {
4416 (void)SvIOKp_on(dstr);
4417 if (sflags & SVf_IOK)
4418 SvFLAGS(dstr) |= SVf_IOK;
4419 if (sflags & SVf_IVisUV)
4421 SvIV_set(dstr, SvIVX(sstr));
4423 if (SvAMAGIC(sstr)) {
4427 else if (sflags & SVp_POK) {
4431 * Check to see if we can just swipe the string. If so, it's a
4432 * possible small lose on short strings, but a big win on long ones.
4433 * It might even be a win on short strings if SvPVX(dstr)
4434 * has to be allocated and SvPVX(sstr) has to be freed.
4437 /* Whichever path we take through the next code, we want this true,
4438 and doing it now facilitates the COW check. */
4439 (void)SvPOK_only(dstr);
4442 #ifdef PERL_COPY_ON_WRITE
4443 (sflags & (SVf_FAKE | SVf_READONLY)) != (SVf_FAKE | SVf_READONLY)
4447 (sflags & SVs_TEMP) && /* slated for free anyway? */
4448 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
4449 (!(flags & SV_NOSTEAL)) &&
4450 /* and we're allowed to steal temps */
4451 SvREFCNT(sstr) == 1 && /* and no other references to it? */
4452 SvLEN(sstr) && /* and really is a string */
4453 /* and won't be needed again, potentially */
4454 !(PL_op && PL_op->op_type == OP_AASSIGN))
4455 #ifdef PERL_COPY_ON_WRITE
4456 && !((sflags & CAN_COW_MASK) == CAN_COW_FLAGS
4457 && (SvFLAGS(dstr) & CAN_COW_MASK) == CAN_COW_FLAGS
4458 && SvTYPE(sstr) >= SVt_PVIV)
4461 /* Failed the swipe test, and it's not a shared hash key either.
4462 Have to copy the string. */
4463 STRLEN len = SvCUR(sstr);
4464 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
4465 Move(SvPVX(sstr),SvPVX(dstr),len,char);
4466 SvCUR_set(dstr, len);
4467 *SvEND(dstr) = '\0';
4469 /* If PERL_COPY_ON_WRITE is not defined, then isSwipe will always
4471 #ifdef PERL_COPY_ON_WRITE
4472 /* Either it's a shared hash key, or it's suitable for
4473 copy-on-write or we can swipe the string. */
4475 PerlIO_printf(Perl_debug_log, "Copy on write: sstr --> dstr\n");
4480 /* I believe I should acquire a global SV mutex if
4481 it's a COW sv (not a shared hash key) to stop
4482 it going un copy-on-write.
4483 If the source SV has gone un copy on write between up there
4484 and down here, then (assert() that) it is of the correct
4485 form to make it copy on write again */
4486 if ((sflags & (SVf_FAKE | SVf_READONLY))
4487 != (SVf_FAKE | SVf_READONLY)) {
4488 SvREADONLY_on(sstr);
4490 /* Make the source SV into a loop of 1.
4491 (about to become 2) */
4492 SV_COW_NEXT_SV_SET(sstr, sstr);
4496 /* Initial code is common. */
4497 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
4499 SvFLAGS(dstr) &= ~SVf_OOK;
4500 Safefree(SvPVX(dstr) - SvIVX(dstr));
4502 else if (SvLEN(dstr))
4503 Safefree(SvPVX(dstr));
4506 #ifdef PERL_COPY_ON_WRITE
4508 /* making another shared SV. */
4509 STRLEN cur = SvCUR(sstr);
4510 STRLEN len = SvLEN(sstr);
4511 assert (SvTYPE(dstr) >= SVt_PVIV);
4513 /* SvIsCOW_normal */
4514 /* splice us in between source and next-after-source. */
4515 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4516 SV_COW_NEXT_SV_SET(sstr, dstr);
4517 SvPV_set(dstr, SvPVX(sstr));
4519 /* SvIsCOW_shared_hash */
4520 UV hash = SvUVX(sstr);
4521 DEBUG_C(PerlIO_printf(Perl_debug_log,
4522 "Copy on write: Sharing hash\n"));
4524 sharepvn(SvPVX(sstr),
4525 (sflags & SVf_UTF8?-cur:cur), hash));
4526 SvUV_set(dstr, hash);
4528 SvLEN_set(dstr, len);
4529 SvCUR_set(dstr, cur);
4530 SvREADONLY_on(dstr);
4532 /* Relesase a global SV mutex. */
4536 { /* Passes the swipe test. */
4537 SvPV_set(dstr, SvPVX(sstr));
4538 SvLEN_set(dstr, SvLEN(sstr));
4539 SvCUR_set(dstr, SvCUR(sstr));
4542 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
4543 SvPV_set(sstr, Nullch);
4549 if (sflags & SVf_UTF8)
4552 if (sflags & SVp_NOK) {
4554 if (sflags & SVf_NOK)
4555 SvFLAGS(dstr) |= SVf_NOK;
4556 SvNV_set(dstr, SvNVX(sstr));
4558 if (sflags & SVp_IOK) {
4559 (void)SvIOKp_on(dstr);
4560 if (sflags & SVf_IOK)
4561 SvFLAGS(dstr) |= SVf_IOK;
4562 if (sflags & SVf_IVisUV)
4564 SvIV_set(dstr, SvIVX(sstr));
4567 MAGIC *smg = mg_find(sstr,PERL_MAGIC_vstring);
4568 sv_magic(dstr, NULL, PERL_MAGIC_vstring,
4569 smg->mg_ptr, smg->mg_len);
4570 SvRMAGICAL_on(dstr);
4573 else if (sflags & SVp_IOK) {
4574 if (sflags & SVf_IOK)
4575 (void)SvIOK_only(dstr);
4577 (void)SvOK_off(dstr);
4578 (void)SvIOKp_on(dstr);
4580 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
4581 if (sflags & SVf_IVisUV)
4583 SvIV_set(dstr, SvIVX(sstr));
4584 if (sflags & SVp_NOK) {
4585 if (sflags & SVf_NOK)
4586 (void)SvNOK_on(dstr);
4588 (void)SvNOKp_on(dstr);
4589 SvNV_set(dstr, SvNVX(sstr));
4592 else if (sflags & SVp_NOK) {
4593 if (sflags & SVf_NOK)
4594 (void)SvNOK_only(dstr);
4596 (void)SvOK_off(dstr);
4599 SvNV_set(dstr, SvNVX(sstr));
4602 if (dtype == SVt_PVGV) {
4603 if (ckWARN(WARN_MISC))
4604 Perl_warner(aTHX_ packWARN(WARN_MISC), "Undefined value assigned to typeglob");
4607 (void)SvOK_off(dstr);
4609 if (SvTAINTED(sstr))
4614 =for apidoc sv_setsv_mg
4616 Like C<sv_setsv>, but also handles 'set' magic.
4622 Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
4624 sv_setsv(dstr,sstr);
4628 #ifdef PERL_COPY_ON_WRITE
4630 Perl_sv_setsv_cow(pTHX_ SV *dstr, SV *sstr)
4632 STRLEN cur = SvCUR(sstr);
4633 STRLEN len = SvLEN(sstr);
4634 register char *new_pv;
4637 PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n",
4645 if (SvTHINKFIRST(dstr))
4646 sv_force_normal_flags(dstr, SV_COW_DROP_PV);
4647 else if (SvPVX(dstr))
4648 Safefree(SvPVX(dstr));
4652 (void)SvUPGRADE (dstr, SVt_PVIV);
4654 assert (SvPOK(sstr));
4655 assert (SvPOKp(sstr));
4656 assert (!SvIOK(sstr));
4657 assert (!SvIOKp(sstr));
4658 assert (!SvNOK(sstr));
4659 assert (!SvNOKp(sstr));
4661 if (SvIsCOW(sstr)) {
4663 if (SvLEN(sstr) == 0) {
4664 /* source is a COW shared hash key. */
4665 UV hash = SvUVX(sstr);
4666 DEBUG_C(PerlIO_printf(Perl_debug_log,
4667 "Fast copy on write: Sharing hash\n"));
4668 SvUV_set(dstr, hash);
4669 new_pv = sharepvn(SvPVX(sstr), (SvUTF8(sstr)?-cur:cur), hash);
4672 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4674 assert ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS);
4675 (void)SvUPGRADE (sstr, SVt_PVIV);
4676 SvREADONLY_on(sstr);
4678 DEBUG_C(PerlIO_printf(Perl_debug_log,
4679 "Fast copy on write: Converting sstr to COW\n"));
4680 SV_COW_NEXT_SV_SET(dstr, sstr);
4682 SV_COW_NEXT_SV_SET(sstr, dstr);
4683 new_pv = SvPVX(sstr);
4686 SvPV_set(dstr, new_pv);
4687 SvFLAGS(dstr) = (SVt_PVIV|SVf_POK|SVp_POK|SVf_FAKE|SVf_READONLY);
4690 SvLEN_set(dstr, len);
4691 SvCUR_set(dstr, cur);
4700 =for apidoc sv_setpvn
4702 Copies a string into an SV. The C<len> parameter indicates the number of
4703 bytes to be copied. If the C<ptr> argument is NULL the SV will become
4704 undefined. Does not handle 'set' magic. See C<sv_setpvn_mg>.
4710 Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
4712 register char *dptr;
4714 SV_CHECK_THINKFIRST_COW_DROP(sv);
4720 /* len is STRLEN which is unsigned, need to copy to signed */
4723 Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen");
4725 (void)SvUPGRADE(sv, SVt_PV);
4727 SvGROW(sv, len + 1);
4729 Move(ptr,dptr,len,char);
4732 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4737 =for apidoc sv_setpvn_mg
4739 Like C<sv_setpvn>, but also handles 'set' magic.