3 * Copyright (c) 1991-2001, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "I wonder what the Entish is for 'yes' and 'no'," he thought.
19 #define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) sv_force_normal(sv)
21 static void do_report_used(pTHXo_ SV *sv);
22 static void do_clean_objs(pTHXo_ SV *sv);
23 #ifndef DISABLE_DESTRUCTOR_KLUDGE
24 static void do_clean_named_objs(pTHXo_ SV *sv);
26 static void do_clean_all(pTHXo_ SV *sv);
29 * "A time to plant, and a time to uproot what was planted..."
34 SvANY(p) = (void *)PL_sv_root; \
35 SvFLAGS(p) = SVTYPEMASK; \
40 /* sv_mutex must be held while calling uproot_SV() */
41 #define uproot_SV(p) \
44 PL_sv_root = (SV*)SvANY(p); \
66 if (PL_debug & 32768) \
76 if (PL_debug & 32768) {
81 for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
83 svend = &sva[SvREFCNT(sva)];
84 if (p >= sv && p < svend)
88 if (ckWARN_d(WARN_INTERNAL))
89 Perl_warner(aTHX_ WARN_INTERNAL,
90 "Attempt to free non-arena SV: 0x%"UVxf,
98 #else /* ! DEBUGGING */
100 #define del_SV(p) plant_SV(p)
102 #endif /* DEBUGGING */
105 Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
110 Zero(ptr, size, char);
112 /* The first SV in an arena isn't an SV. */
113 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
114 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
115 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
117 PL_sv_arenaroot = sva;
118 PL_sv_root = sva + 1;
120 svend = &sva[SvREFCNT(sva) - 1];
123 SvANY(sv) = (void *)(SV*)(sv + 1);
124 SvFLAGS(sv) = SVTYPEMASK;
128 SvFLAGS(sv) = SVTYPEMASK;
131 /* sv_mutex must be held while calling more_sv() */
138 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
139 PL_nice_chunk = Nullch;
142 char *chunk; /* must use New here to match call to */
143 New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */
144 sv_add_arena(chunk, 1008, 0);
151 S_visit(pTHX_ SVFUNC_t f)
157 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
158 svend = &sva[SvREFCNT(sva)];
159 for (sv = sva + 1; sv < svend; ++sv) {
160 if (SvTYPE(sv) != SVTYPEMASK)
167 Perl_sv_report_used(pTHX)
169 visit(do_report_used);
173 Perl_sv_clean_objs(pTHX)
175 PL_in_clean_objs = TRUE;
176 visit(do_clean_objs);
177 #ifndef DISABLE_DESTRUCTOR_KLUDGE
178 /* some barnacles may yet remain, clinging to typeglobs */
179 visit(do_clean_named_objs);
181 PL_in_clean_objs = FALSE;
185 Perl_sv_clean_all(pTHX)
187 PL_in_clean_all = TRUE;
189 PL_in_clean_all = FALSE;
193 Perl_sv_free_arenas(pTHX)
197 XPV *arena, *arenanext;
199 /* Free arenas here, but be careful about fake ones. (We assume
200 contiguity of the fake ones with the corresponding real ones.) */
202 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
203 svanext = (SV*) SvANY(sva);
204 while (svanext && SvFAKE(svanext))
205 svanext = (SV*) SvANY(svanext);
208 Safefree((void *)sva);
211 for (arena = PL_xiv_arenaroot; arena; arena = arenanext) {
212 arenanext = (XPV*)arena->xpv_pv;
215 PL_xiv_arenaroot = 0;
217 for (arena = PL_xnv_arenaroot; arena; arena = arenanext) {
218 arenanext = (XPV*)arena->xpv_pv;
221 PL_xnv_arenaroot = 0;
223 for (arena = PL_xrv_arenaroot; arena; arena = arenanext) {
224 arenanext = (XPV*)arena->xpv_pv;
227 PL_xrv_arenaroot = 0;
229 for (arena = PL_xpv_arenaroot; arena; arena = arenanext) {
230 arenanext = (XPV*)arena->xpv_pv;
233 PL_xpv_arenaroot = 0;
235 for (arena = (XPV*)PL_xpviv_arenaroot; arena; arena = arenanext) {
236 arenanext = (XPV*)arena->xpv_pv;
239 PL_xpviv_arenaroot = 0;
241 for (arena = (XPV*)PL_xpvnv_arenaroot; arena; arena = arenanext) {
242 arenanext = (XPV*)arena->xpv_pv;
245 PL_xpvnv_arenaroot = 0;
247 for (arena = (XPV*)PL_xpvcv_arenaroot; arena; arena = arenanext) {
248 arenanext = (XPV*)arena->xpv_pv;
251 PL_xpvcv_arenaroot = 0;
253 for (arena = (XPV*)PL_xpvav_arenaroot; arena; arena = arenanext) {
254 arenanext = (XPV*)arena->xpv_pv;
257 PL_xpvav_arenaroot = 0;
259 for (arena = (XPV*)PL_xpvhv_arenaroot; arena; arena = arenanext) {
260 arenanext = (XPV*)arena->xpv_pv;
263 PL_xpvhv_arenaroot = 0;
265 for (arena = (XPV*)PL_xpvmg_arenaroot; arena; arena = arenanext) {
266 arenanext = (XPV*)arena->xpv_pv;
269 PL_xpvmg_arenaroot = 0;
271 for (arena = (XPV*)PL_xpvlv_arenaroot; arena; arena = arenanext) {
272 arenanext = (XPV*)arena->xpv_pv;
275 PL_xpvlv_arenaroot = 0;
277 for (arena = (XPV*)PL_xpvbm_arenaroot; arena; arena = arenanext) {
278 arenanext = (XPV*)arena->xpv_pv;
281 PL_xpvbm_arenaroot = 0;
283 for (arena = (XPV*)PL_he_arenaroot; arena; arena = arenanext) {
284 arenanext = (XPV*)arena->xpv_pv;
290 Safefree(PL_nice_chunk);
291 PL_nice_chunk = Nullch;
292 PL_nice_chunk_size = 0;
298 Perl_report_uninit(pTHX)
301 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit,
302 " in ", PL_op_desc[PL_op->op_type]);
304 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit, "", "");
316 * See comment in more_xiv() -- RAM.
318 PL_xiv_root = *(IV**)xiv;
320 return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv));
324 S_del_xiv(pTHX_ XPVIV *p)
326 IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv));
328 *(IV**)xiv = PL_xiv_root;
339 New(705, ptr, 1008/sizeof(XPV), XPV);
340 ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */
341 PL_xiv_arenaroot = ptr; /* to keep Purify happy */
344 xivend = &xiv[1008 / sizeof(IV) - 1];
345 xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */
347 while (xiv < xivend) {
348 *(IV**)xiv = (IV *)(xiv + 1);
362 PL_xnv_root = *(NV**)xnv;
364 return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv));
368 S_del_xnv(pTHX_ XPVNV *p)
370 NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv));
372 *(NV**)xnv = PL_xnv_root;
383 New(711, ptr, 1008/sizeof(XPV), XPV);
384 ptr->xpv_pv = (char*)PL_xnv_arenaroot;
385 PL_xnv_arenaroot = ptr;
388 xnvend = &xnv[1008 / sizeof(NV) - 1];
389 xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */
391 while (xnv < xnvend) {
392 *(NV**)xnv = (NV*)(xnv + 1);
406 PL_xrv_root = (XRV*)xrv->xrv_rv;
412 S_del_xrv(pTHX_ XRV *p)
415 p->xrv_rv = (SV*)PL_xrv_root;
424 register XRV* xrvend;
426 New(712, ptr, 1008/sizeof(XPV), XPV);
427 ptr->xpv_pv = (char*)PL_xrv_arenaroot;
428 PL_xrv_arenaroot = ptr;
431 xrvend = &xrv[1008 / sizeof(XRV) - 1];
432 xrv += (sizeof(XPV) - 1) / sizeof(XRV) + 1;
434 while (xrv < xrvend) {
435 xrv->xrv_rv = (SV*)(xrv + 1);
449 PL_xpv_root = (XPV*)xpv->xpv_pv;
455 S_del_xpv(pTHX_ XPV *p)
458 p->xpv_pv = (char*)PL_xpv_root;
467 register XPV* xpvend;
468 New(713, xpv, 1008/sizeof(XPV), XPV);
469 xpv->xpv_pv = (char*)PL_xpv_arenaroot;
470 PL_xpv_arenaroot = xpv;
472 xpvend = &xpv[1008 / sizeof(XPV) - 1];
474 while (xpv < xpvend) {
475 xpv->xpv_pv = (char*)(xpv + 1);
488 xpviv = PL_xpviv_root;
489 PL_xpviv_root = (XPVIV*)xpviv->xpv_pv;
495 S_del_xpviv(pTHX_ XPVIV *p)
498 p->xpv_pv = (char*)PL_xpviv_root;
506 register XPVIV* xpviv;
507 register XPVIV* xpvivend;
508 New(714, xpviv, 1008/sizeof(XPVIV), XPVIV);
509 xpviv->xpv_pv = (char*)PL_xpviv_arenaroot;
510 PL_xpviv_arenaroot = xpviv;
512 xpvivend = &xpviv[1008 / sizeof(XPVIV) - 1];
513 PL_xpviv_root = ++xpviv;
514 while (xpviv < xpvivend) {
515 xpviv->xpv_pv = (char*)(xpviv + 1);
528 xpvnv = PL_xpvnv_root;
529 PL_xpvnv_root = (XPVNV*)xpvnv->xpv_pv;
535 S_del_xpvnv(pTHX_ XPVNV *p)
538 p->xpv_pv = (char*)PL_xpvnv_root;
546 register XPVNV* xpvnv;
547 register XPVNV* xpvnvend;
548 New(715, xpvnv, 1008/sizeof(XPVNV), XPVNV);
549 xpvnv->xpv_pv = (char*)PL_xpvnv_arenaroot;
550 PL_xpvnv_arenaroot = xpvnv;
552 xpvnvend = &xpvnv[1008 / sizeof(XPVNV) - 1];
553 PL_xpvnv_root = ++xpvnv;
554 while (xpvnv < xpvnvend) {
555 xpvnv->xpv_pv = (char*)(xpvnv + 1);
568 xpvcv = PL_xpvcv_root;
569 PL_xpvcv_root = (XPVCV*)xpvcv->xpv_pv;
575 S_del_xpvcv(pTHX_ XPVCV *p)
578 p->xpv_pv = (char*)PL_xpvcv_root;
586 register XPVCV* xpvcv;
587 register XPVCV* xpvcvend;
588 New(716, xpvcv, 1008/sizeof(XPVCV), XPVCV);
589 xpvcv->xpv_pv = (char*)PL_xpvcv_arenaroot;
590 PL_xpvcv_arenaroot = xpvcv;
592 xpvcvend = &xpvcv[1008 / sizeof(XPVCV) - 1];
593 PL_xpvcv_root = ++xpvcv;
594 while (xpvcv < xpvcvend) {
595 xpvcv->xpv_pv = (char*)(xpvcv + 1);
608 xpvav = PL_xpvav_root;
609 PL_xpvav_root = (XPVAV*)xpvav->xav_array;
615 S_del_xpvav(pTHX_ XPVAV *p)
618 p->xav_array = (char*)PL_xpvav_root;
626 register XPVAV* xpvav;
627 register XPVAV* xpvavend;
628 New(717, xpvav, 1008/sizeof(XPVAV), XPVAV);
629 xpvav->xav_array = (char*)PL_xpvav_arenaroot;
630 PL_xpvav_arenaroot = xpvav;
632 xpvavend = &xpvav[1008 / sizeof(XPVAV) - 1];
633 PL_xpvav_root = ++xpvav;
634 while (xpvav < xpvavend) {
635 xpvav->xav_array = (char*)(xpvav + 1);
638 xpvav->xav_array = 0;
648 xpvhv = PL_xpvhv_root;
649 PL_xpvhv_root = (XPVHV*)xpvhv->xhv_array;
655 S_del_xpvhv(pTHX_ XPVHV *p)
658 p->xhv_array = (char*)PL_xpvhv_root;
666 register XPVHV* xpvhv;
667 register XPVHV* xpvhvend;
668 New(718, xpvhv, 1008/sizeof(XPVHV), XPVHV);
669 xpvhv->xhv_array = (char*)PL_xpvhv_arenaroot;
670 PL_xpvhv_arenaroot = xpvhv;
672 xpvhvend = &xpvhv[1008 / sizeof(XPVHV) - 1];
673 PL_xpvhv_root = ++xpvhv;
674 while (xpvhv < xpvhvend) {
675 xpvhv->xhv_array = (char*)(xpvhv + 1);
678 xpvhv->xhv_array = 0;
688 xpvmg = PL_xpvmg_root;
689 PL_xpvmg_root = (XPVMG*)xpvmg->xpv_pv;
695 S_del_xpvmg(pTHX_ XPVMG *p)
698 p->xpv_pv = (char*)PL_xpvmg_root;
706 register XPVMG* xpvmg;
707 register XPVMG* xpvmgend;
708 New(719, xpvmg, 1008/sizeof(XPVMG), XPVMG);
709 xpvmg->xpv_pv = (char*)PL_xpvmg_arenaroot;
710 PL_xpvmg_arenaroot = xpvmg;
712 xpvmgend = &xpvmg[1008 / sizeof(XPVMG) - 1];
713 PL_xpvmg_root = ++xpvmg;
714 while (xpvmg < xpvmgend) {
715 xpvmg->xpv_pv = (char*)(xpvmg + 1);
728 xpvlv = PL_xpvlv_root;
729 PL_xpvlv_root = (XPVLV*)xpvlv->xpv_pv;
735 S_del_xpvlv(pTHX_ XPVLV *p)
738 p->xpv_pv = (char*)PL_xpvlv_root;
746 register XPVLV* xpvlv;
747 register XPVLV* xpvlvend;
748 New(720, xpvlv, 1008/sizeof(XPVLV), XPVLV);
749 xpvlv->xpv_pv = (char*)PL_xpvlv_arenaroot;
750 PL_xpvlv_arenaroot = xpvlv;
752 xpvlvend = &xpvlv[1008 / sizeof(XPVLV) - 1];
753 PL_xpvlv_root = ++xpvlv;
754 while (xpvlv < xpvlvend) {
755 xpvlv->xpv_pv = (char*)(xpvlv + 1);
768 xpvbm = PL_xpvbm_root;
769 PL_xpvbm_root = (XPVBM*)xpvbm->xpv_pv;
775 S_del_xpvbm(pTHX_ XPVBM *p)
778 p->xpv_pv = (char*)PL_xpvbm_root;
786 register XPVBM* xpvbm;
787 register XPVBM* xpvbmend;
788 New(721, xpvbm, 1008/sizeof(XPVBM), XPVBM);
789 xpvbm->xpv_pv = (char*)PL_xpvbm_arenaroot;
790 PL_xpvbm_arenaroot = xpvbm;
792 xpvbmend = &xpvbm[1008 / sizeof(XPVBM) - 1];
793 PL_xpvbm_root = ++xpvbm;
794 while (xpvbm < xpvbmend) {
795 xpvbm->xpv_pv = (char*)(xpvbm + 1);
802 # define my_safemalloc(s) (void*)safexmalloc(717,s)
803 # define my_safefree(p) safexfree((char*)p)
805 # define my_safemalloc(s) (void*)safemalloc(s)
806 # define my_safefree(p) safefree((char*)p)
811 #define new_XIV() my_safemalloc(sizeof(XPVIV))
812 #define del_XIV(p) my_safefree(p)
814 #define new_XNV() my_safemalloc(sizeof(XPVNV))
815 #define del_XNV(p) my_safefree(p)
817 #define new_XRV() my_safemalloc(sizeof(XRV))
818 #define del_XRV(p) my_safefree(p)
820 #define new_XPV() my_safemalloc(sizeof(XPV))
821 #define del_XPV(p) my_safefree(p)
823 #define new_XPVIV() my_safemalloc(sizeof(XPVIV))
824 #define del_XPVIV(p) my_safefree(p)
826 #define new_XPVNV() my_safemalloc(sizeof(XPVNV))
827 #define del_XPVNV(p) my_safefree(p)
829 #define new_XPVCV() my_safemalloc(sizeof(XPVCV))
830 #define del_XPVCV(p) my_safefree(p)
832 #define new_XPVAV() my_safemalloc(sizeof(XPVAV))
833 #define del_XPVAV(p) my_safefree(p)
835 #define new_XPVHV() my_safemalloc(sizeof(XPVHV))
836 #define del_XPVHV(p) my_safefree(p)
838 #define new_XPVMG() my_safemalloc(sizeof(XPVMG))
839 #define del_XPVMG(p) my_safefree(p)
841 #define new_XPVLV() my_safemalloc(sizeof(XPVLV))
842 #define del_XPVLV(p) my_safefree(p)
844 #define new_XPVBM() my_safemalloc(sizeof(XPVBM))
845 #define del_XPVBM(p) my_safefree(p)
849 #define new_XIV() (void*)new_xiv()
850 #define del_XIV(p) del_xiv((XPVIV*) p)
852 #define new_XNV() (void*)new_xnv()
853 #define del_XNV(p) del_xnv((XPVNV*) p)
855 #define new_XRV() (void*)new_xrv()
856 #define del_XRV(p) del_xrv((XRV*) p)
858 #define new_XPV() (void*)new_xpv()
859 #define del_XPV(p) del_xpv((XPV *)p)
861 #define new_XPVIV() (void*)new_xpviv()
862 #define del_XPVIV(p) del_xpviv((XPVIV *)p)
864 #define new_XPVNV() (void*)new_xpvnv()
865 #define del_XPVNV(p) del_xpvnv((XPVNV *)p)
867 #define new_XPVCV() (void*)new_xpvcv()
868 #define del_XPVCV(p) del_xpvcv((XPVCV *)p)
870 #define new_XPVAV() (void*)new_xpvav()
871 #define del_XPVAV(p) del_xpvav((XPVAV *)p)
873 #define new_XPVHV() (void*)new_xpvhv()
874 #define del_XPVHV(p) del_xpvhv((XPVHV *)p)
876 #define new_XPVMG() (void*)new_xpvmg()
877 #define del_XPVMG(p) del_xpvmg((XPVMG *)p)
879 #define new_XPVLV() (void*)new_xpvlv()
880 #define del_XPVLV(p) del_xpvlv((XPVLV *)p)
882 #define new_XPVBM() (void*)new_xpvbm()
883 #define del_XPVBM(p) del_xpvbm((XPVBM *)p)
887 #define new_XPVGV() my_safemalloc(sizeof(XPVGV))
888 #define del_XPVGV(p) my_safefree(p)
890 #define new_XPVFM() my_safemalloc(sizeof(XPVFM))
891 #define del_XPVFM(p) my_safefree(p)
893 #define new_XPVIO() my_safemalloc(sizeof(XPVIO))
894 #define del_XPVIO(p) my_safefree(p)
897 =for apidoc sv_upgrade
899 Upgrade an SV to a more complex form. Use C<SvUPGRADE>. See
906 Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
916 if (mt != SVt_PV && SvREADONLY(sv) && SvFAKE(sv)) {
920 if (SvTYPE(sv) == mt)
926 switch (SvTYPE(sv)) {
947 else if (mt < SVt_PVIV)
964 pv = (char*)SvRV(sv);
984 else if (mt == SVt_NV)
995 del_XPVIV(SvANY(sv));
1005 del_XPVNV(SvANY(sv));
1013 magic = SvMAGIC(sv);
1014 stash = SvSTASH(sv);
1015 del_XPVMG(SvANY(sv));
1018 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
1023 Perl_croak(aTHX_ "Can't upgrade to undef");
1025 SvANY(sv) = new_XIV();
1029 SvANY(sv) = new_XNV();
1033 SvANY(sv) = new_XRV();
1037 SvANY(sv) = new_XPV();
1043 SvANY(sv) = new_XPVIV();
1053 SvANY(sv) = new_XPVNV();
1061 SvANY(sv) = new_XPVMG();
1067 SvMAGIC(sv) = magic;
1068 SvSTASH(sv) = stash;
1071 SvANY(sv) = new_XPVLV();
1077 SvMAGIC(sv) = magic;
1078 SvSTASH(sv) = stash;
1085 SvANY(sv) = new_XPVAV();
1093 SvMAGIC(sv) = magic;
1094 SvSTASH(sv) = stash;
1100 SvANY(sv) = new_XPVHV();
1108 SvMAGIC(sv) = magic;
1109 SvSTASH(sv) = stash;
1116 SvANY(sv) = new_XPVCV();
1117 Zero(SvANY(sv), 1, XPVCV);
1123 SvMAGIC(sv) = magic;
1124 SvSTASH(sv) = stash;
1127 SvANY(sv) = new_XPVGV();
1133 SvMAGIC(sv) = magic;
1134 SvSTASH(sv) = stash;
1142 SvANY(sv) = new_XPVBM();
1148 SvMAGIC(sv) = magic;
1149 SvSTASH(sv) = stash;
1155 SvANY(sv) = new_XPVFM();
1156 Zero(SvANY(sv), 1, XPVFM);
1162 SvMAGIC(sv) = magic;
1163 SvSTASH(sv) = stash;
1166 SvANY(sv) = new_XPVIO();
1167 Zero(SvANY(sv), 1, XPVIO);
1173 SvMAGIC(sv) = magic;
1174 SvSTASH(sv) = stash;
1175 IoPAGE_LEN(sv) = 60;
1178 SvFLAGS(sv) &= ~SVTYPEMASK;
1184 Perl_sv_backoff(pTHX_ register SV *sv)
1188 char *s = SvPVX(sv);
1189 SvLEN(sv) += SvIVX(sv);
1190 SvPVX(sv) -= SvIVX(sv);
1192 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
1194 SvFLAGS(sv) &= ~SVf_OOK;
1201 Expands the character buffer in the SV. This will use C<sv_unref> and will
1202 upgrade the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1209 Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
1213 #ifdef HAS_64K_LIMIT
1214 if (newlen >= 0x10000) {
1215 PerlIO_printf(Perl_debug_log,
1216 "Allocation too large: %"UVxf"\n", (UV)newlen);
1219 #endif /* HAS_64K_LIMIT */
1222 if (SvTYPE(sv) < SVt_PV) {
1223 sv_upgrade(sv, SVt_PV);
1226 else if (SvOOK(sv)) { /* pv is offset? */
1229 if (newlen > SvLEN(sv))
1230 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
1231 #ifdef HAS_64K_LIMIT
1232 if (newlen >= 0x10000)
1238 if (newlen > SvLEN(sv)) { /* need more room? */
1239 if (SvLEN(sv) && s) {
1240 #if defined(MYMALLOC) && !defined(LEAKTEST)
1241 STRLEN l = malloced_size((void*)SvPVX(sv));
1247 Renew(s,newlen,char);
1250 New(703,s,newlen,char);
1252 SvLEN_set(sv, newlen);
1258 =for apidoc sv_setiv
1260 Copies an integer into the given SV. Does not handle 'set' magic. See
1267 Perl_sv_setiv(pTHX_ register SV *sv, IV i)
1269 SV_CHECK_THINKFIRST(sv);
1270 switch (SvTYPE(sv)) {
1272 sv_upgrade(sv, SVt_IV);
1275 sv_upgrade(sv, SVt_PVNV);
1279 sv_upgrade(sv, SVt_PVIV);
1288 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
1289 PL_op_desc[PL_op->op_type]);
1291 (void)SvIOK_only(sv); /* validate number */
1297 =for apidoc sv_setiv_mg
1299 Like C<sv_setiv>, but also handles 'set' magic.
1305 Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
1312 =for apidoc sv_setuv
1314 Copies an unsigned integer into the given SV. Does not handle 'set' magic.
1321 Perl_sv_setuv(pTHX_ register SV *sv, UV u)
1323 /* With these two if statements:
1324 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
1327 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
1329 If you wish to remove them, please benchmark to see what the effect is
1331 if (u <= (UV)IV_MAX) {
1332 sv_setiv(sv, (IV)u);
1341 =for apidoc sv_setuv_mg
1343 Like C<sv_setuv>, but also handles 'set' magic.
1349 Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
1351 /* With these two if statements:
1352 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
1355 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
1357 If you wish to remove them, please benchmark to see what the effect is
1359 if (u <= (UV)IV_MAX) {
1360 sv_setiv(sv, (IV)u);
1370 =for apidoc sv_setnv
1372 Copies a double into the given SV. Does not handle 'set' magic. See
1379 Perl_sv_setnv(pTHX_ register SV *sv, NV num)
1381 SV_CHECK_THINKFIRST(sv);
1382 switch (SvTYPE(sv)) {
1385 sv_upgrade(sv, SVt_NV);
1390 sv_upgrade(sv, SVt_PVNV);
1399 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
1400 PL_op_name[PL_op->op_type]);
1403 (void)SvNOK_only(sv); /* validate number */
1408 =for apidoc sv_setnv_mg
1410 Like C<sv_setnv>, but also handles 'set' magic.
1416 Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
1423 S_not_a_number(pTHX_ SV *sv)
1428 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
1429 /* each *s can expand to 4 chars + "...\0",
1430 i.e. need room for 8 chars */
1432 for (s = SvPVX(sv); *s && d < limit; s++) {
1434 if (ch & 128 && !isPRINT_LC(ch)) {
1443 else if (ch == '\r') {
1447 else if (ch == '\f') {
1451 else if (ch == '\\') {
1455 else if (isPRINT_LC(ch))
1470 Perl_warner(aTHX_ WARN_NUMERIC,
1471 "Argument \"%s\" isn't numeric in %s", tmpbuf,
1472 PL_op_desc[PL_op->op_type]);
1474 Perl_warner(aTHX_ WARN_NUMERIC,
1475 "Argument \"%s\" isn't numeric", tmpbuf);
1478 /* the number can be converted to integer with atol() or atoll() although */
1479 #define IS_NUMBER_TO_INT_BY_ATOL 0x01 /* integer (may have decimals) */
1480 #define IS_NUMBER_TO_INT_BY_STRTOL 0x02 /* it may exceed IV_MAX */
1481 #define IS_NUMBER_TO_INT_BY_ATOF 0x04 /* seen something like 123e4 */
1482 #define IS_NUMBER_LONGER_THAN_IV_MAX 0x08 /* more digits than IV_MAX */
1483 #define IS_NUMBER_AS_LONG_AS_IV_MAX 0x10 /* may(be not) larger than IV_MAX */
1484 #define IS_NUMBER_NOT_INT 0x20 /* seen a decimal point or e */
1485 #define IS_NUMBER_NEG 0x40 /* seen a leading - */
1486 #define IS_NUMBER_INFINITY 0x80 /* /^\s*-?Infinity\s*$/i */
1488 /* Actually, ISO C leaves conversion of UV to IV undefined, but
1489 until proven guilty, assume that things are not that bad... */
1491 /* As 64 bit platforms often have an NV that doesn't preserve all bits of
1492 an IV (an assumption perl has been based on to date) it becomes necessary
1493 to remove the assumption that the NV always carries enough precision to
1494 recreate the IV whenever needed, and that the NV is the canonical form.
1495 Instead, IV/UV and NV need to be given equal rights. So as to not lose
1496 precision as an side effect of conversion (which would lead to insanity
1497 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1498 1) to distinguish between IV/UV/NV slots that have cached a valid
1499 conversion where precision was lost and IV/UV/NV slots that have a
1500 valid conversion which has lost no precision
1501 2) to ensure that if a numeric conversion to one form is request that
1502 would lose precision, the precise conversion (or differently
1503 imprecise conversion) is also performed and cached, to prevent
1504 requests for different numeric formats on the same SV causing
1505 lossy conversion chains. (lossless conversion chains are perfectly
1510 SvIOKp is true if the IV slot contains a valid value
1511 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
1512 SvNOKp is true if the NV slot contains a valid value
1513 SvNOK is true only if the NV value is accurate
1516 while converting from PV to NV check to see if converting that NV to an
1517 IV(or UV) would lose accuracy over a direct conversion from PV to
1518 IV(or UV). If it would, cache both conversions, return NV, but mark
1519 SV as IOK NOKp (ie not NOK).
1521 while converting from PV to IV check to see if converting that IV to an
1522 NV would lose accuracy over a direct conversion from PV to NV. If it
1523 would, cache both conversions, flag similarly.
1525 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
1526 correctly because if IV & NV were set NV *always* overruled.
1527 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flags meaning
1528 changes - now IV and NV together means that the two are interchangeable
1529 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
1531 The benefit of this is operations such as pp_add know that if SvIOK is
1532 true for both left and right operands, then integer addition can be
1533 used instead of floating point. (for cases where the result won't
1534 overflow) Before, floating point was always used, which could lead to
1535 loss of precision compared with integer addition.
1537 * making IV and NV equal status should make maths accurate on 64 bit
1539 * may speed up maths somewhat if pp_add and friends start to use
1540 integers when possible instead of fp. (hopefully the overhead in
1541 looking for SvIOK and checking for overflow will not outweigh the
1542 fp to integer speedup)
1543 * will slow down integer operations (callers of SvIV) on "inaccurate"
1544 values, as the change from SvIOK to SvIOKp will cause a call into
1545 sv_2iv each time rather than a macro access direct to the IV slot
1546 * should speed up number->string conversion on integers as IV is
1547 favoured when IV and NV equally accurate
1549 ####################################################################
1550 You had better be using SvIOK_notUV if you want an IV for arithmetic
1551 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV
1552 SvUOK is true iff UV.
1553 ####################################################################
1555 Your mileage will vary depending your CPUs relative fp to integer
1559 #ifndef NV_PRESERVES_UV
1560 #define IS_NUMBER_UNDERFLOW_IV 1
1561 #define IS_NUMBER_UNDERFLOW_UV 2
1562 #define IS_NUMBER_IV_AND_UV 2
1563 #define IS_NUMBER_OVERFLOW_IV 4
1564 #define IS_NUMBER_OVERFLOW_UV 5
1565 /* Hopefully your optimiser will consider inlining these two functions. */
1567 S_sv_2inuv_non_preserve (pTHX_ register SV *sv, I32 numtype) {
1568 NV nv = SvNVX(sv); /* Code simpler and had compiler problems if */
1569 UV nv_as_uv = U_V(nv); /* these are not in simple variables. */
1570 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2inuv_non '%s', IV=0x%"UVxf" NV=%g inttype=%"UVXf"\n", SvPVX(sv), SvIVX(sv), nv, (UV)numtype));
1571 if (nv_as_uv <= (UV)IV_MAX) {
1572 (void)SvIOKp_on(sv);
1573 (void)SvNOKp_on(sv);
1574 /* Within suitable range to fit in an IV, atol won't overflow */
1575 /* XXX quite sure? Is that your final answer? not really, I'm
1576 trusting that nv_as_uv to round down if NV is (IV_MAX + 1) */
1577 SvIVX(sv) = (IV)Atol(SvPVX(sv));
1578 if (numtype & IS_NUMBER_NOT_INT) {
1579 /* I believe that even if the original PV had decimals, they
1580 are lost beyond the limit of the FP precision.
1581 However, neither is canonical, so both only get p flags.
1583 /* Both already have p flags, so do nothing */
1584 } else if (SvIVX(sv) == I_V(nv)) {
1589 /* It had no "." so it must be integer. assert (get in here from
1590 sv_2iv and sv_2uv only for ndef HAS_STRTOL and
1591 IS_NUMBER_AS_LONG_AS_IV_MAX) or my logic is faulty and all
1592 conversion routines need audit. */
1594 return nv < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
1596 /* between IV_MAX and NV(UV_MAX). Could be slightly> UV_MAX */
1597 (void)SvIOKp_on(sv);
1598 (void)SvNOKp_on(sv);
1601 int save_errno = errno;
1603 SvUVX(sv) = Strtoul(SvPVX(sv), Null(char**), 10);
1605 if (numtype & IS_NUMBER_NOT_INT) {
1606 /* UV and NV both imprecise. */
1608 } else if (SvUVX(sv) == nv_as_uv && SvUVX(sv) != UV_MAX) {
1617 return IS_NUMBER_OVERFLOW_IV;
1621 /* Must have just overflowed UV, but not enough that an NV could spot
1623 return IS_NUMBER_OVERFLOW_UV;
1626 /* We've just lost integer precision, nothing we could do. */
1627 SvUVX(sv) = nv_as_uv;
1628 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2niuv_non UV? '%s', UV=0x%"UVxf" NV=%g U_V(NV)=0x%"UVxf" inttype=%"UVXf"\n", SvPVX(sv), SvIVX(sv), nv, nv_as_uv, (UV)numtype));
1629 /* UV and NV slots equally valid only if we have casting symmetry. */
1630 if (numtype & IS_NUMBER_NOT_INT) {
1632 } else if (SvUVX(sv) == nv_as_uv && SvUVX(sv) != UV_MAX) {
1633 /* UV_MAX can cast up to NV (UV_MAX+1), that NV casts down to UV_MAX
1634 UV_MAX ought to be 0xFF...FFF which won't preserve (We only
1635 get to this point if NVs don't preserve UVs) */
1640 /* As above, I believe UV at least as good as NV */
1643 #endif /* HAS_STRTOUL */
1644 return IS_NUMBER_OVERFLOW_IV;
1647 /* For sv_2nv these three cases are "SvNOK and don't bother casting" */
1649 S_sv_2iuv_non_preserve (pTHX_ register SV *sv, I32 numtype)
1651 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%"UVxf" NV=%g inttype=%"UVXf"\n", SvPVX(sv), SvIVX(sv), SvNVX(sv), (UV)numtype));
1652 if (SvNVX(sv) < (NV)IV_MIN) {
1653 (void)SvIOKp_on(sv);
1656 return IS_NUMBER_UNDERFLOW_IV;
1658 if (SvNVX(sv) > (NV)UV_MAX) {
1659 (void)SvIOKp_on(sv);
1663 return IS_NUMBER_OVERFLOW_UV;
1665 if (!(numtype & (IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_TO_INT_BY_STRTOL))) {
1666 (void)SvIOKp_on(sv);
1668 /* Can't use strtol etc to convert this string */
1669 if (SvNVX(sv) <= (UV)IV_MAX) {
1670 SvIVX(sv) = I_V(SvNVX(sv));
1671 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
1672 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
1674 /* Integer is imprecise. NOK, IOKp */
1676 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
1679 SvUVX(sv) = U_V(SvNVX(sv));
1680 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
1681 if (SvUVX(sv) == UV_MAX) {
1682 /* As we know that NVs don't preserve UVs, UV_MAX cannot
1683 possibly be preserved by NV. Hence, it must be overflow.
1685 return IS_NUMBER_OVERFLOW_UV;
1687 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
1689 /* Integer is imprecise. NOK, IOKp */
1691 return IS_NUMBER_OVERFLOW_IV;
1693 return S_sv_2inuv_non_preserve(aTHX_ sv, numtype);
1695 #endif /* NV_PRESERVES_UV*/
1698 Perl_sv_2iv(pTHX_ register SV *sv)
1702 if (SvGMAGICAL(sv)) {
1707 return I_V(SvNVX(sv));
1709 if (SvPOKp(sv) && SvLEN(sv))
1712 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1713 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1719 if (SvTHINKFIRST(sv)) {
1722 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
1723 (SvRV(tmpstr) != SvRV(sv)))
1724 return SvIV(tmpstr);
1725 return PTR2IV(SvRV(sv));
1727 if (SvREADONLY(sv) && SvFAKE(sv)) {
1728 sv_force_normal(sv);
1730 if (SvREADONLY(sv) && !SvOK(sv)) {
1731 if (ckWARN(WARN_UNINITIALIZED))
1738 return (IV)(SvUVX(sv));
1745 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
1746 * without also getting a cached IV/UV from it at the same time
1747 * (ie PV->NV conversion should detect loss of accuracy and cache
1748 * IV or UV at same time to avoid this. NWC */
1750 if (SvTYPE(sv) == SVt_NV)
1751 sv_upgrade(sv, SVt_PVNV);
1753 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
1754 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
1755 certainly cast into the IV range at IV_MAX, whereas the correct
1756 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
1758 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
1759 SvIVX(sv) = I_V(SvNVX(sv));
1760 if (SvNVX(sv) == (NV) SvIVX(sv)
1761 #ifndef NV_PRESERVES_UV
1762 && (((UV)1 << NV_PRESERVES_UV_BITS) >
1763 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
1764 /* Don't flag it as "accurately an integer" if the number
1765 came from a (by definition imprecise) NV operation, and
1766 we're outside the range of NV integer precision */
1769 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
1770 DEBUG_c(PerlIO_printf(Perl_debug_log,
1771 "0x%"UVxf" iv(%g => %"IVdf") (precise)\n",
1777 /* IV not precise. No need to convert from PV, as NV
1778 conversion would already have cached IV if it detected
1779 that PV->IV would be better than PV->NV->IV
1780 flags already correct - don't set public IOK. */
1781 DEBUG_c(PerlIO_printf(Perl_debug_log,
1782 "0x%"UVxf" iv(%g => %"IVdf") (imprecise)\n",
1787 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
1788 but the cast (NV)IV_MIN rounds to a the value less (more
1789 negative) than IV_MIN which happens to be equal to SvNVX ??
1790 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
1791 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
1792 (NV)UVX == NVX are both true, but the values differ. :-(
1793 Hopefully for 2s complement IV_MIN is something like
1794 0x8000000000000000 which will be exact. NWC */
1797 SvUVX(sv) = U_V(SvNVX(sv));
1799 (SvNVX(sv) == (NV) SvUVX(sv))
1800 #ifndef NV_PRESERVES_UV
1801 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
1802 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
1803 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
1804 /* Don't flag it as "accurately an integer" if the number
1805 came from a (by definition imprecise) NV operation, and
1806 we're outside the range of NV integer precision */
1812 DEBUG_c(PerlIO_printf(Perl_debug_log,
1813 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
1817 return (IV)SvUVX(sv);
1820 else if (SvPOKp(sv) && SvLEN(sv)) {
1821 I32 numtype = looks_like_number(sv);
1823 /* We want to avoid a possible problem when we cache an IV which
1824 may be later translated to an NV, and the resulting NV is not
1825 the translation of the initial data.
1827 This means that if we cache such an IV, we need to cache the
1828 NV as well. Moreover, we trade speed for space, and do not
1829 cache the NV if we are sure it's not needed.
1832 if ((numtype & ~IS_NUMBER_NEG) == IS_NUMBER_TO_INT_BY_ATOL) {
1833 /* The NV may be reconstructed from IV - safe to cache IV,
1834 which may be calculated by atol(). */
1835 if (SvTYPE(sv) < SVt_PVIV)
1836 sv_upgrade(sv, SVt_PVIV);
1838 SvIVX(sv) = Atol(SvPVX(sv));
1842 int save_errno = errno;
1843 /* Is it an integer that we could convert with strtol?
1844 So try it, and if it doesn't set errno then it's pukka.
1845 This should be faster than going atof and then thinking. */
1846 if (((numtype & (IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_NOT_INT))
1847 == IS_NUMBER_TO_INT_BY_STRTOL)
1848 /* && is a sequence point. Without it not sure if I'm trying
1849 to do too much between sequence points and hence going
1851 && ((errno = 0), 1) /* , 1 so always true */
1852 && ((i = Strtol(SvPVX(sv), Null(char**), 10)), 1)
1854 if (SvTYPE(sv) < SVt_PVIV)
1855 sv_upgrade(sv, SVt_PVIV);
1864 /* Hopefully trace flow will optimise this away where possible
1868 /* It wasn't an integer, or it overflowed, or we don't have
1869 strtol. Do things the slow way - check if it's a UV etc. */
1870 d = Atof(SvPVX(sv));
1872 if (SvTYPE(sv) < SVt_PVNV)
1873 sv_upgrade(sv, SVt_PVNV);
1876 if (! numtype && ckWARN(WARN_NUMERIC))
1879 #if defined(USE_LONG_DOUBLE)
1880 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
1881 PTR2UV(sv), SvNVX(sv)));
1883 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%g)\n",
1884 PTR2UV(sv), SvNVX(sv)));
1888 #ifdef NV_PRESERVES_UV
1889 (void)SvIOKp_on(sv);
1891 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
1892 SvIVX(sv) = I_V(SvNVX(sv));
1893 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
1896 /* Integer is imprecise. NOK, IOKp */
1898 /* UV will not work better than IV */
1900 if (SvNVX(sv) > (NV)UV_MAX) {
1902 /* Integer is inaccurate. NOK, IOKp, is UV */
1906 SvUVX(sv) = U_V(SvNVX(sv));
1907 /* 0xFFFFFFFFFFFFFFFF not an issue in here */
1908 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
1912 /* Integer is imprecise. NOK, IOKp, is UV */
1918 #else /* NV_PRESERVES_UV */
1919 if (((UV)1 << NV_PRESERVES_UV_BITS) >
1920 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
1921 /* Small enough to preserve all bits. */
1922 (void)SvIOKp_on(sv);
1924 SvIVX(sv) = I_V(SvNVX(sv));
1925 if ((NV)(SvIVX(sv)) == SvNVX(sv))
1927 /* Assumption: first non-preserved integer is < IV_MAX,
1928 this NV is in the preserved range, therefore: */
1929 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
1931 Perl_croak(aTHX_ "sv_2iv assumed (U_V(fabs(SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%g U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX);
1933 } else if (sv_2iuv_non_preserve (sv, numtype)
1934 >= IS_NUMBER_OVERFLOW_IV)
1936 #endif /* NV_PRESERVES_UV */
1940 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1942 if (SvTYPE(sv) < SVt_IV)
1943 /* Typically the caller expects that sv_any is not NULL now. */
1944 sv_upgrade(sv, SVt_IV);
1947 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
1948 PTR2UV(sv),SvIVX(sv)));
1949 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
1953 Perl_sv_2uv(pTHX_ register SV *sv)
1957 if (SvGMAGICAL(sv)) {
1962 return U_V(SvNVX(sv));
1963 if (SvPOKp(sv) && SvLEN(sv))
1966 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1967 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1973 if (SvTHINKFIRST(sv)) {
1976 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
1977 (SvRV(tmpstr) != SvRV(sv)))
1978 return SvUV(tmpstr);
1979 return PTR2UV(SvRV(sv));
1981 if (SvREADONLY(sv) && SvFAKE(sv)) {
1982 sv_force_normal(sv);
1984 if (SvREADONLY(sv) && !SvOK(sv)) {
1985 if (ckWARN(WARN_UNINITIALIZED))
1995 return (UV)SvIVX(sv);
1999 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2000 * without also getting a cached IV/UV from it at the same time
2001 * (ie PV->NV conversion should detect loss of accuracy and cache
2002 * IV or UV at same time to avoid this. */
2003 /* IV-over-UV optimisation - choose to cache IV if possible */
2005 if (SvTYPE(sv) == SVt_NV)
2006 sv_upgrade(sv, SVt_PVNV);
2008 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2009 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2010 SvIVX(sv) = I_V(SvNVX(sv));
2011 if (SvNVX(sv) == (NV) SvIVX(sv)
2012 #ifndef NV_PRESERVES_UV
2013 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2014 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2015 /* Don't flag it as "accurately an integer" if the number
2016 came from a (by definition imprecise) NV operation, and
2017 we're outside the range of NV integer precision */
2020 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2021 DEBUG_c(PerlIO_printf(Perl_debug_log,
2022 "0x%"UVxf" uv(%g => %"IVdf") (precise)\n",
2028 /* IV not precise. No need to convert from PV, as NV
2029 conversion would already have cached IV if it detected
2030 that PV->IV would be better than PV->NV->IV
2031 flags already correct - don't set public IOK. */
2032 DEBUG_c(PerlIO_printf(Perl_debug_log,
2033 "0x%"UVxf" uv(%g => %"IVdf") (imprecise)\n",
2038 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2039 but the cast (NV)IV_MIN rounds to a the value less (more
2040 negative) than IV_MIN which happens to be equal to SvNVX ??
2041 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2042 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2043 (NV)UVX == NVX are both true, but the values differ. :-(
2044 Hopefully for 2s complement IV_MIN is something like
2045 0x8000000000000000 which will be exact. NWC */
2048 SvUVX(sv) = U_V(SvNVX(sv));
2050 (SvNVX(sv) == (NV) SvUVX(sv))
2051 #ifndef NV_PRESERVES_UV
2052 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2053 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2054 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2055 /* Don't flag it as "accurately an integer" if the number
2056 came from a (by definition imprecise) NV operation, and
2057 we're outside the range of NV integer precision */
2062 DEBUG_c(PerlIO_printf(Perl_debug_log,
2063 "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n",
2069 else if (SvPOKp(sv) && SvLEN(sv)) {
2070 I32 numtype = looks_like_number(sv);
2072 /* We want to avoid a possible problem when we cache a UV which
2073 may be later translated to an NV, and the resulting NV is not
2074 the translation of the initial data.
2076 This means that if we cache such a UV, we need to cache the
2077 NV as well. Moreover, we trade speed for space, and do not
2078 cache the NV if not needed.
2081 if ((numtype & ~IS_NUMBER_NEG) == IS_NUMBER_TO_INT_BY_ATOL) {
2082 /* The NV may be reconstructed from IV - safe to cache IV,
2083 which may be calculated by atol(). */
2084 if (SvTYPE(sv) < SVt_PVIV)
2085 sv_upgrade(sv, SVt_PVIV);
2087 SvIVX(sv) = Atol(SvPVX(sv));
2091 char *num_begin = SvPVX(sv);
2092 int save_errno = errno;
2094 /* seems that strtoul taking numbers that start with - is
2095 implementation dependant, and can't be relied upon. */
2096 if (numtype & IS_NUMBER_NEG) {
2097 /* Not totally defensive. assumine that looks_like_num
2098 didn't lie about a - sign */
2099 while (isSPACE(*num_begin))
2101 if (*num_begin == '-')
2105 /* Is it an integer that we could convert with strtoul?
2106 So try it, and if it doesn't set errno then it's pukka.
2107 This should be faster than going atof and then thinking. */
2108 if (((numtype & (IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_NOT_INT))
2109 == IS_NUMBER_TO_INT_BY_STRTOL)
2110 && ((errno = 0), 1) /* always true */
2111 && ((u = Strtoul(num_begin, Null(char**), 10)), 1) /* ditto */
2113 /* If known to be negative, check it didn't undeflow IV
2114 XXX possibly we should put more negative values as NVs
2115 direct rather than go via atof below */
2116 && ((numtype & IS_NUMBER_NEG) ? (u <= (UV)IV_MIN) : 1)) {
2119 if (SvTYPE(sv) < SVt_PVIV)
2120 sv_upgrade(sv, SVt_PVIV);
2123 /* If it's negative must use IV.
2124 IV-over-UV optimisation */
2125 if (numtype & IS_NUMBER_NEG) {
2127 } else if (u <= (UV) IV_MAX) {
2130 /* it didn't overflow, and it was positive. */
2139 /* Hopefully trace flow will optimise this away where possible
2143 /* It wasn't an integer, or it overflowed, or we don't have
2144 strtol. Do things the slow way - check if it's a IV etc. */
2145 d = Atof(SvPVX(sv));
2147 if (SvTYPE(sv) < SVt_PVNV)
2148 sv_upgrade(sv, SVt_PVNV);
2151 if (! numtype && ckWARN(WARN_NUMERIC))
2154 #if defined(USE_LONG_DOUBLE)
2155 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n",
2156 PTR2UV(sv), SvNVX(sv)));
2158 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%g)\n",
2159 PTR2UV(sv), SvNVX(sv)));
2162 #ifdef NV_PRESERVES_UV
2163 (void)SvIOKp_on(sv);
2165 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2166 SvIVX(sv) = I_V(SvNVX(sv));
2167 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2170 /* Integer is imprecise. NOK, IOKp */
2172 /* UV will not work better than IV */
2174 if (SvNVX(sv) > (NV)UV_MAX) {
2176 /* Integer is inaccurate. NOK, IOKp, is UV */
2180 SvUVX(sv) = U_V(SvNVX(sv));
2181 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
2182 NV preservse UV so can do correct comparison. */
2183 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2187 /* Integer is imprecise. NOK, IOKp, is UV */
2192 #else /* NV_PRESERVES_UV */
2193 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2194 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2195 /* Small enough to preserve all bits. */
2196 (void)SvIOKp_on(sv);
2198 SvIVX(sv) = I_V(SvNVX(sv));
2199 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2201 /* Assumption: first non-preserved integer is < IV_MAX,
2202 this NV is in the preserved range, therefore: */
2203 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2205 Perl_croak(aTHX_ "sv_2uv assumed (U_V(fabs(SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%g U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX);
2208 sv_2iuv_non_preserve (sv, numtype);
2209 #endif /* NV_PRESERVES_UV */
2214 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2215 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2218 if (SvTYPE(sv) < SVt_IV)
2219 /* Typically the caller expects that sv_any is not NULL now. */
2220 sv_upgrade(sv, SVt_IV);
2224 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
2225 PTR2UV(sv),SvUVX(sv)));
2226 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
2230 Perl_sv_2nv(pTHX_ register SV *sv)
2234 if (SvGMAGICAL(sv)) {
2238 if (SvPOKp(sv) && SvLEN(sv)) {
2239 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv))
2241 return Atof(SvPVX(sv));
2245 return (NV)SvUVX(sv);
2247 return (NV)SvIVX(sv);
2250 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2251 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2257 if (SvTHINKFIRST(sv)) {
2260 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
2261 (SvRV(tmpstr) != SvRV(sv)))
2262 return SvNV(tmpstr);
2263 return PTR2NV(SvRV(sv));
2265 if (SvREADONLY(sv) && SvFAKE(sv)) {
2266 sv_force_normal(sv);
2268 if (SvREADONLY(sv) && !SvOK(sv)) {
2269 if (ckWARN(WARN_UNINITIALIZED))
2274 if (SvTYPE(sv) < SVt_NV) {
2275 if (SvTYPE(sv) == SVt_IV)
2276 sv_upgrade(sv, SVt_PVNV);
2278 sv_upgrade(sv, SVt_NV);
2279 #if defined(USE_LONG_DOUBLE)
2281 STORE_NUMERIC_LOCAL_SET_STANDARD();
2282 PerlIO_printf(Perl_debug_log,
2283 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
2284 PTR2UV(sv), SvNVX(sv));
2285 RESTORE_NUMERIC_LOCAL();
2289 STORE_NUMERIC_LOCAL_SET_STANDARD();
2290 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%g)\n",
2291 PTR2UV(sv), SvNVX(sv));
2292 RESTORE_NUMERIC_LOCAL();
2296 else if (SvTYPE(sv) < SVt_PVNV)
2297 sv_upgrade(sv, SVt_PVNV);
2299 (!SvPOKp(sv) || !strchr(SvPVX(sv),'.') || !looks_like_number(sv)))
2301 SvNVX(sv) = SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv);
2302 #ifdef NV_PRESERVES_UV
2305 /* Only set the public NV OK flag if this NV preserves the IV */
2306 /* Check it's not 0xFFFFFFFFFFFFFFFF */
2307 if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
2308 : (SvIVX(sv) == I_V(SvNVX(sv))))
2314 else if (SvPOKp(sv) && SvLEN(sv)) {
2315 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv))
2317 SvNVX(sv) = Atof(SvPVX(sv));
2318 #ifdef NV_PRESERVES_UV
2321 /* Only set the public NV OK flag if this NV preserves the value in
2322 the PV at least as well as an IV/UV would.
2323 Not sure how to do this 100% reliably. */
2324 /* if that shift count is out of range then Configure's test is
2325 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2327 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2328 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)))
2329 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
2330 else if (SvNVX(sv) < (NV)IV_MIN || SvNVX(sv) > (NV)UV_MAX) {
2331 /* Definitely too large/small to fit in an integer, so no loss
2332 of precision going to integer in the future via NV */
2335 /* Is it something we can run through strtol etc (ie no
2336 trailing exponent part)? */
2337 int numtype = looks_like_number(sv);
2338 /* XXX probably should cache this if called above */
2341 (IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_TO_INT_BY_STRTOL))) {
2342 /* Can't use strtol etc to convert this string, so don't try */
2345 sv_2inuv_non_preserve (sv, numtype);
2347 #endif /* NV_PRESERVES_UV */
2350 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
2352 if (SvTYPE(sv) < SVt_NV)
2353 /* Typically the caller expects that sv_any is not NULL now. */
2354 /* XXX Ilya implies that this is a bug in callers that assume this
2355 and ideally should be fixed. */
2356 sv_upgrade(sv, SVt_NV);
2359 #if defined(USE_LONG_DOUBLE)
2361 STORE_NUMERIC_LOCAL_SET_STANDARD();
2362 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
2363 PTR2UV(sv), SvNVX(sv));
2364 RESTORE_NUMERIC_LOCAL();
2368 STORE_NUMERIC_LOCAL_SET_STANDARD();
2369 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%g)\n",
2370 PTR2UV(sv), SvNVX(sv));
2371 RESTORE_NUMERIC_LOCAL();
2378 S_asIV(pTHX_ SV *sv)
2380 I32 numtype = looks_like_number(sv);
2383 if (numtype & IS_NUMBER_TO_INT_BY_ATOL)
2384 return Atol(SvPVX(sv));
2386 if (ckWARN(WARN_NUMERIC))
2389 d = Atof(SvPVX(sv));
2394 S_asUV(pTHX_ SV *sv)
2396 I32 numtype = looks_like_number(sv);
2399 if (numtype & IS_NUMBER_TO_INT_BY_ATOL)
2400 return Strtoul(SvPVX(sv), Null(char**), 10);
2403 if (ckWARN(WARN_NUMERIC))
2406 return U_V(Atof(SvPVX(sv)));
2410 * Returns a combination of (advisory only - can get false negatives)
2411 * IS_NUMBER_TO_INT_BY_ATOL, IS_NUMBER_TO_INT_BY_ATOF
2412 * IS_NUMBER_LONGER_THAN_IV_MAX, IS_NUMBER_AS_LONG_AS_IV_MAX
2413 * IS_NUMBER_NOT_INT, IS_NUMBER_NEG, IS_NUMBER_INFINITY
2414 * 0 if does not look like number.
2416 * (atol and strtol stop when they hit a decimal point. strtol will return
2417 * LONG_MAX and LONG_MIN when given out of range values. ANSI says they should
2418 * do this, and vendors have had 11 years to get it right.
2419 * However, will try to make it still work with only atol
2421 * IS_NUMBER_TO_INT_BY_ATOL 123456789 or 123456789.3 definitely < IV_MAX
2422 * IS_NUMBER_TO_INT_BY_STRTOL 123456789 or 123456789.3 if digits = IV_MAX
2423 * IS_NUMBER_TO_INT_BY_ATOF 123456789e0 or >> IV_MAX
2424 * IS_NUMBER_LONGER_THAN_IV_MAX lots of digits, don't bother with atol
2425 * IS_NUMBER_AS_LONG_AS_IV_MAX atol might hit LONG_MAX, might not.
2426 * IS_NUMBER_NOT_INT saw "." or "e"
2428 * IS_NUMBER_INFINITY
2432 =for apidoc looks_like_number
2434 Test if an the content of an SV looks like a number (or is a
2435 number). C<Inf> and C<Infinity> are treated as numbers (so will not
2436 issue a non-numeric warning), even if your atof() doesn't grok them.
2442 Perl_looks_like_number(pTHX_ SV *sv)
2445 register char *send;
2446 register char *sbegin;
2447 register char *nbegin;
2451 #ifdef USE_LOCALE_NUMERIC
2452 bool specialradix = FALSE;
2459 else if (SvPOKp(sv))
2460 sbegin = SvPV(sv, len);
2463 send = sbegin + len;
2470 numtype = IS_NUMBER_NEG;
2477 * we return IS_NUMBER_TO_INT_BY_ATOL if the number can converted to
2478 * integer with atol() without overflow, IS_NUMBER_TO_INT_BY_STRTOL if
2479 * possibly slightly larger than max int, IS_NUMBER_TO_INT_BY_ATOF if you
2480 * will need (int)atof().
2483 /* next must be digit or the radix separator or beginning of infinity */
2487 } while (isDIGIT(*s));
2489 /* Aaargh. long long really is irritating.
2490 In the gospel according to ANSI 1989, it is an axiom that "long"
2491 is the longest integer type, and that if you don't know how long
2492 something is you can cast it to long, and nothing will be lost
2493 (except possibly speed of execution if long is slower than the
2495 Now, one can't be sure if the old rules apply, or long long
2496 (or some other newfangled thing) is actually longer than the
2497 (formerly) longest thing.
2499 /* This lot will work for 64 bit *as long as* either
2500 either long is 64 bit
2501 or we can find both strtol/strtoq and strtoul/strtouq
2502 If not, we really should refuse to let the user use 64 bit IVs
2503 By "64 bit" I really mean IVs that don't get preserved by NVs
2504 It also should work for 128 bit IVs. Can any lend me a machine to
2507 if (s - nbegin > TYPE_DIGITS(UV)) /* Cannot cache ato[ul]() */
2508 numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_LONGER_THAN_IV_MAX;
2509 else if (s - nbegin < BIT_DIGITS(((sizeof (IV)>sizeof (long))
2510 ? sizeof(long) : sizeof (IV))*8-1))
2511 numtype |= IS_NUMBER_TO_INT_BY_ATOL;
2513 /* Can't be sure either way. (For 64 bit UV, 63 bit IV is 1 decimal
2514 digit less (IV_MAX= 9223372036854775807,
2515 UV_MAX= 18446744073709551615) so be cautious */
2516 numtype |= IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_AS_LONG_AS_IV_MAX;
2519 #ifdef USE_LOCALE_NUMERIC
2520 || (specialradix = IS_NUMERIC_RADIX(s))
2523 #ifdef USE_LOCALE_NUMERIC
2525 s += SvCUR(PL_numeric_radix);
2529 numtype |= IS_NUMBER_NOT_INT;
2530 while (isDIGIT(*s)) /* optional digits after the radix */
2535 #ifdef USE_LOCALE_NUMERIC
2536 || (specialradix = IS_NUMERIC_RADIX(s))
2539 #ifdef USE_LOCALE_NUMERIC
2541 s += SvCUR(PL_numeric_radix);
2545 numtype |= IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_NOT_INT;
2546 /* no digits before the radix means we need digits after it */
2550 } while (isDIGIT(*s));
2555 else if (*s == 'I' || *s == 'i') {
2556 s++; if (*s != 'N' && *s != 'n') return 0;
2557 s++; if (*s != 'F' && *s != 'f') return 0;
2558 s++; if (*s == 'I' || *s == 'i') {
2559 s++; if (*s != 'N' && *s != 'n') return 0;
2560 s++; if (*s != 'I' && *s != 'i') return 0;
2561 s++; if (*s != 'T' && *s != 't') return 0;
2562 s++; if (*s != 'Y' && *s != 'y') return 0;
2571 numtype = (numtype & IS_NUMBER_NEG) /* Keep track of sign */
2572 | IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT;
2574 /* we can have an optional exponent part */
2575 if (*s == 'e' || *s == 'E') {
2576 numtype &= IS_NUMBER_NEG;
2577 numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_INT;
2579 if (*s == '+' || *s == '-')
2584 } while (isDIGIT(*s));
2594 if (len == 10 && memEQ(sbegin, "0 but true", 10))
2595 return IS_NUMBER_TO_INT_BY_ATOL;
2600 Perl_sv_2pv_nolen(pTHX_ register SV *sv)
2603 return sv_2pv(sv, &n_a);
2606 /* We assume that buf is at least TYPE_CHARS(UV) long. */
2608 uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
2610 char *ptr = buf + TYPE_CHARS(UV);
2624 *--ptr = '0' + (uv % 10);
2633 Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
2638 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
2639 char *tmpbuf = tbuf;
2645 if (SvGMAGICAL(sv)) {
2653 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
2655 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
2660 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
2665 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2666 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2673 if (SvTHINKFIRST(sv)) {
2676 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
2677 (SvRV(tmpstr) != SvRV(sv)))
2678 return SvPV(tmpstr,*lp);
2685 switch (SvTYPE(sv)) {
2687 if ( ((SvFLAGS(sv) &
2688 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
2689 == (SVs_OBJECT|SVs_RMG))
2690 && strEQ(s=HvNAME(SvSTASH(sv)), "Regexp")
2691 && (mg = mg_find(sv, 'r'))) {
2692 regexp *re = (regexp *)mg->mg_obj;
2695 char *fptr = "msix";
2700 U16 reganch = (re->reganch & PMf_COMPILETIME) >> 12;
2702 while((ch = *fptr++)) {
2704 reflags[left++] = ch;
2707 reflags[right--] = ch;
2712 reflags[left] = '-';
2716 mg->mg_len = re->prelen + 4 + left;
2717 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
2718 Copy("(?", mg->mg_ptr, 2, char);
2719 Copy(reflags, mg->mg_ptr+2, left, char);
2720 Copy(":", mg->mg_ptr+left+2, 1, char);
2721 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
2722 mg->mg_ptr[mg->mg_len - 1] = ')';
2723 mg->mg_ptr[mg->mg_len] = 0;
2725 PL_reginterp_cnt += re->program[0].next_off;
2737 case SVt_PVBM: if (SvROK(sv))
2740 s = "SCALAR"; break;
2741 case SVt_PVLV: s = "LVALUE"; break;
2742 case SVt_PVAV: s = "ARRAY"; break;
2743 case SVt_PVHV: s = "HASH"; break;
2744 case SVt_PVCV: s = "CODE"; break;
2745 case SVt_PVGV: s = "GLOB"; break;
2746 case SVt_PVFM: s = "FORMAT"; break;
2747 case SVt_PVIO: s = "IO"; break;
2748 default: s = "UNKNOWN"; break;
2752 Perl_sv_setpvf(aTHX_ tsv, "%s=%s", HvNAME(SvSTASH(sv)), s);
2755 Perl_sv_catpvf(aTHX_ tsv, "(0x%"UVxf")", PTR2UV(sv));
2761 if (SvREADONLY(sv) && !SvOK(sv)) {
2762 if (ckWARN(WARN_UNINITIALIZED))
2768 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
2769 /* I'm assuming that if both IV and NV are equally valid then
2770 converting the IV is going to be more efficient */
2771 U32 isIOK = SvIOK(sv);
2772 U32 isUIOK = SvIsUV(sv);
2773 char buf[TYPE_CHARS(UV)];
2776 if (SvTYPE(sv) < SVt_PVIV)
2777 sv_upgrade(sv, SVt_PVIV);
2779 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
2781 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
2782 SvGROW(sv, ebuf - ptr + 1); /* inlined from sv_setpvn */
2783 Move(ptr,SvPVX(sv),ebuf - ptr,char);
2784 SvCUR_set(sv, ebuf - ptr);
2794 else if (SvNOKp(sv)) {
2795 if (SvTYPE(sv) < SVt_PVNV)
2796 sv_upgrade(sv, SVt_PVNV);
2797 /* The +20 is pure guesswork. Configure test needed. --jhi */
2798 SvGROW(sv, NV_DIG + 20);
2800 olderrno = errno; /* some Xenix systems wipe out errno here */
2802 if (SvNVX(sv) == 0.0)
2803 (void)strcpy(s,"0");
2807 Gconvert(SvNVX(sv), NV_DIG, 0, s);
2810 #ifdef FIXNEGATIVEZERO
2811 if (*s == '-' && s[1] == '0' && !s[2])
2821 if (ckWARN(WARN_UNINITIALIZED)
2822 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
2825 if (SvTYPE(sv) < SVt_PV)
2826 /* Typically the caller expects that sv_any is not NULL now. */
2827 sv_upgrade(sv, SVt_PV);
2830 *lp = s - SvPVX(sv);
2833 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
2834 PTR2UV(sv),SvPVX(sv)));
2838 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
2839 /* Sneaky stuff here */
2843 tsv = newSVpv(tmpbuf, 0);
2859 len = strlen(tmpbuf);
2861 #ifdef FIXNEGATIVEZERO
2862 if (len == 2 && t[0] == '-' && t[1] == '0') {
2867 (void)SvUPGRADE(sv, SVt_PV);
2869 s = SvGROW(sv, len + 1);
2878 Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
2881 return sv_2pvbyte(sv, &n_a);
2885 Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
2887 return sv_2pv(sv,lp);
2891 Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
2894 return sv_2pvutf8(sv, &n_a);
2898 Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
2900 sv_utf8_upgrade(sv);
2901 return SvPV(sv,*lp);
2904 /* This function is only called on magical items */
2906 Perl_sv_2bool(pTHX_ register SV *sv)
2915 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
2916 (SvRV(tmpsv) != SvRV(sv)))
2917 return SvTRUE(tmpsv);
2918 return SvRV(sv) != 0;
2921 register XPV* Xpvtmp;
2922 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
2923 (*Xpvtmp->xpv_pv > '0' ||
2924 Xpvtmp->xpv_cur > 1 ||
2925 (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0')))
2932 return SvIVX(sv) != 0;
2935 return SvNVX(sv) != 0.0;
2943 =for apidoc sv_utf8_upgrade
2945 Convert the PV of an SV to its UTF8-encoded form.
2951 Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
2956 if (!sv || !SvPOK(sv) || SvUTF8(sv))
2959 /* This function could be much more efficient if we had a FLAG in SVs
2960 * to signal if there are any hibit chars in the PV.
2961 * Given that there isn't make loop fast as possible
2967 if ((hibit = UTF8_IS_CONTINUED(*t++)))
2974 if (SvREADONLY(sv) && SvFAKE(sv)) {
2975 sv_force_normal(sv);
2978 len = SvCUR(sv) + 1; /* Plus the \0 */
2979 SvPVX(sv) = (char*)bytes_to_utf8((U8*)s, &len);
2980 SvCUR(sv) = len - 1;
2982 Safefree(s); /* No longer using what was there before. */
2983 SvLEN(sv) = len; /* No longer know the real size. */
2989 =for apidoc sv_utf8_downgrade
2991 Attempt to convert the PV of an SV from UTF8-encoded to byte encoding.
2992 This may not be possible if the PV contains non-byte encoding characters;
2993 if this is the case, either returns false or, if C<fail_ok> is not
3000 Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
3002 if (SvPOK(sv) && SvUTF8(sv)) {
3007 if (SvREADONLY(sv) && SvFAKE(sv))
3008 sv_force_normal(sv);
3010 if (!utf8_to_bytes((U8*)s, &len)) {
3015 Perl_croak(aTHX_ "Wide character in %s",
3016 PL_op_desc[PL_op->op_type]);
3018 Perl_croak(aTHX_ "Wide character");
3030 =for apidoc sv_utf8_encode
3032 Convert the PV of an SV to UTF8-encoded, but then turn off the C<SvUTF8>
3033 flag so that it looks like bytes again. Nothing calls this.
3039 Perl_sv_utf8_encode(pTHX_ register SV *sv)
3041 sv_utf8_upgrade(sv);
3046 Perl_sv_utf8_decode(pTHX_ register SV *sv)
3051 bool has_utf = FALSE;
3052 if (!sv_utf8_downgrade(sv, TRUE))
3055 /* it is actually just a matter of turning the utf8 flag on, but
3056 * we want to make sure everything inside is valid utf8 first.
3059 if (!is_utf8_string((U8*)c, SvCUR(sv)+1))
3063 if (UTF8_IS_CONTINUED(*c++)) {
3073 /* Note: sv_setsv() should not be called with a source string that needs
3074 * to be reused, since it may destroy the source string if it is marked
3079 =for apidoc sv_setsv
3081 Copies the contents of the source SV C<ssv> into the destination SV C<dsv>.
3082 The source SV may be destroyed if it is mortal. Does not handle 'set'
3083 magic. See the macro forms C<SvSetSV>, C<SvSetSV_nosteal> and
3090 Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
3092 register U32 sflags;
3098 SV_CHECK_THINKFIRST(dstr);
3100 sstr = &PL_sv_undef;
3101 stype = SvTYPE(sstr);
3102 dtype = SvTYPE(dstr);
3106 /* There's a lot of redundancy below but we're going for speed here */
3111 if (dtype != SVt_PVGV) {
3112 (void)SvOK_off(dstr);
3120 sv_upgrade(dstr, SVt_IV);
3123 sv_upgrade(dstr, SVt_PVNV);
3127 sv_upgrade(dstr, SVt_PVIV);
3130 (void)SvIOK_only(dstr);
3131 SvIVX(dstr) = SvIVX(sstr);
3134 if (SvTAINTED(sstr))
3145 sv_upgrade(dstr, SVt_NV);
3150 sv_upgrade(dstr, SVt_PVNV);
3153 SvNVX(dstr) = SvNVX(sstr);
3154 (void)SvNOK_only(dstr);
3155 if (SvTAINTED(sstr))
3163 sv_upgrade(dstr, SVt_RV);
3164 else if (dtype == SVt_PVGV &&
3165 SvTYPE(SvRV(sstr)) == SVt_PVGV) {
3168 if (GvIMPORTED(dstr) != GVf_IMPORTED
3169 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3171 GvIMPORTED_on(dstr);
3182 sv_upgrade(dstr, SVt_PV);
3185 if (dtype < SVt_PVIV)
3186 sv_upgrade(dstr, SVt_PVIV);
3189 if (dtype < SVt_PVNV)
3190 sv_upgrade(dstr, SVt_PVNV);
3197 Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0),
3198 PL_op_name[PL_op->op_type]);
3200 Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0));
3204 if (dtype <= SVt_PVGV) {
3206 if (dtype != SVt_PVGV) {
3207 char *name = GvNAME(sstr);
3208 STRLEN len = GvNAMELEN(sstr);
3209 sv_upgrade(dstr, SVt_PVGV);
3210 sv_magic(dstr, dstr, '*', Nullch, 0);
3211 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
3212 GvNAME(dstr) = savepvn(name, len);
3213 GvNAMELEN(dstr) = len;
3214 SvFAKE_on(dstr); /* can coerce to non-glob */
3216 /* ahem, death to those who redefine active sort subs */
3217 else if (PL_curstackinfo->si_type == PERLSI_SORT
3218 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
3219 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
3222 #ifdef GV_SHARED_CHECK
3223 if (GvSHARED((GV*)dstr)) {
3224 Perl_croak(aTHX_ PL_no_modify);
3228 (void)SvOK_off(dstr);
3229 GvINTRO_off(dstr); /* one-shot flag */
3231 GvGP(dstr) = gp_ref(GvGP(sstr));
3232 if (SvTAINTED(sstr))
3234 if (GvIMPORTED(dstr) != GVf_IMPORTED
3235 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3237 GvIMPORTED_on(dstr);
3245 if (SvGMAGICAL(sstr)) {
3247 if (SvTYPE(sstr) != stype) {
3248 stype = SvTYPE(sstr);
3249 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
3253 if (stype == SVt_PVLV)
3254 (void)SvUPGRADE(dstr, SVt_PVNV);
3256 (void)SvUPGRADE(dstr, stype);
3259 sflags = SvFLAGS(sstr);
3261 if (sflags & SVf_ROK) {
3262 if (dtype >= SVt_PV) {
3263 if (dtype == SVt_PVGV) {
3264 SV *sref = SvREFCNT_inc(SvRV(sstr));
3266 int intro = GvINTRO(dstr);
3268 #ifdef GV_SHARED_CHECK
3269 if (GvSHARED((GV*)dstr)) {
3270 Perl_croak(aTHX_ PL_no_modify);
3277 GvINTRO_off(dstr); /* one-shot flag */
3278 Newz(602,gp, 1, GP);
3279 GvGP(dstr) = gp_ref(gp);
3280 GvSV(dstr) = NEWSV(72,0);
3281 GvLINE(dstr) = CopLINE(PL_curcop);
3282 GvEGV(dstr) = (GV*)dstr;
3285 switch (SvTYPE(sref)) {
3288 SAVESPTR(GvAV(dstr));
3290 dref = (SV*)GvAV(dstr);
3291 GvAV(dstr) = (AV*)sref;
3292 if (!GvIMPORTED_AV(dstr)
3293 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3295 GvIMPORTED_AV_on(dstr);
3300 SAVESPTR(GvHV(dstr));
3302 dref = (SV*)GvHV(dstr);
3303 GvHV(dstr) = (HV*)sref;
3304 if (!GvIMPORTED_HV(dstr)
3305 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3307 GvIMPORTED_HV_on(dstr);
3312 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
3313 SvREFCNT_dec(GvCV(dstr));
3314 GvCV(dstr) = Nullcv;
3315 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3316 PL_sub_generation++;
3318 SAVESPTR(GvCV(dstr));
3321 dref = (SV*)GvCV(dstr);
3322 if (GvCV(dstr) != (CV*)sref) {
3323 CV* cv = GvCV(dstr);
3325 if (!GvCVGEN((GV*)dstr) &&
3326 (CvROOT(cv) || CvXSUB(cv)))
3329 /* ahem, death to those who redefine
3330 * active sort subs */
3331 if (PL_curstackinfo->si_type == PERLSI_SORT &&
3332 PL_sortcop == CvSTART(cv))
3334 "Can't redefine active sort subroutine %s",
3335 GvENAME((GV*)dstr));
3336 /* Redefining a sub - warning is mandatory if
3337 it was a const and its value changed. */
3338 if (ckWARN(WARN_REDEFINE)
3340 && (!CvCONST((CV*)sref)
3341 || sv_cmp(cv_const_sv(cv),
3342 cv_const_sv((CV*)sref)))))
3344 Perl_warner(aTHX_ WARN_REDEFINE,
3346 ? "Constant subroutine %s redefined"
3347 : "Subroutine %s redefined",
3348 GvENAME((GV*)dstr));
3351 cv_ckproto(cv, (GV*)dstr,
3352 SvPOK(sref) ? SvPVX(sref) : Nullch);
3354 GvCV(dstr) = (CV*)sref;
3355 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3356 GvASSUMECV_on(dstr);
3357 PL_sub_generation++;
3359 if (!GvIMPORTED_CV(dstr)
3360 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3362 GvIMPORTED_CV_on(dstr);
3367 SAVESPTR(GvIOp(dstr));
3369 dref = (SV*)GvIOp(dstr);
3370 GvIOp(dstr) = (IO*)sref;
3374 SAVESPTR(GvFORM(dstr));
3376 dref = (SV*)GvFORM(dstr);
3377 GvFORM(dstr) = (CV*)sref;
3381 SAVESPTR(GvSV(dstr));
3383 dref = (SV*)GvSV(dstr);
3385 if (!GvIMPORTED_SV(dstr)
3386 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3388 GvIMPORTED_SV_on(dstr);
3396 if (SvTAINTED(sstr))
3401 (void)SvOOK_off(dstr); /* backoff */
3403 Safefree(SvPVX(dstr));
3404 SvLEN(dstr)=SvCUR(dstr)=0;
3407 (void)SvOK_off(dstr);
3408 SvRV(dstr) = SvREFCNT_inc(SvRV(sstr));
3410 if (sflags & SVp_NOK) {
3412 /* Only set the public OK flag if the source has public OK. */
3413 if (sflags & SVf_NOK)
3414 SvFLAGS(dstr) |= SVf_NOK;
3415 SvNVX(dstr) = SvNVX(sstr);
3417 if (sflags & SVp_IOK) {
3418 (void)SvIOKp_on(dstr);
3419 if (sflags & SVf_IOK)
3420 SvFLAGS(dstr) |= SVf_IOK;
3421 if (sflags & SVf_IVisUV)
3423 SvIVX(dstr) = SvIVX(sstr);
3425 if (SvAMAGIC(sstr)) {
3429 else if (sflags & SVp_POK) {
3432 * Check to see if we can just swipe the string. If so, it's a
3433 * possible small lose on short strings, but a big win on long ones.
3434 * It might even be a win on short strings if SvPVX(dstr)
3435 * has to be allocated and SvPVX(sstr) has to be freed.
3438 if (SvTEMP(sstr) && /* slated for free anyway? */
3439 SvREFCNT(sstr) == 1 && /* and no other references to it? */
3440 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
3441 SvLEN(sstr) && /* and really is a string */
3442 !(PL_op && PL_op->op_type == OP_AASSIGN)) /* and won't be needed again, potentially */
3444 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
3446 SvFLAGS(dstr) &= ~SVf_OOK;
3447 Safefree(SvPVX(dstr) - SvIVX(dstr));
3449 else if (SvLEN(dstr))
3450 Safefree(SvPVX(dstr));
3452 (void)SvPOK_only(dstr);
3453 SvPV_set(dstr, SvPVX(sstr));
3454 SvLEN_set(dstr, SvLEN(sstr));
3455 SvCUR_set(dstr, SvCUR(sstr));
3458 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
3459 SvPV_set(sstr, Nullch);
3464 else { /* have to copy actual string */
3465 STRLEN len = SvCUR(sstr);
3467 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
3468 Move(SvPVX(sstr),SvPVX(dstr),len,char);
3469 SvCUR_set(dstr, len);
3470 *SvEND(dstr) = '\0';
3471 (void)SvPOK_only(dstr);
3473 if (sflags & SVf_UTF8)
3476 if (sflags & SVp_NOK) {
3478 if (sflags & SVf_NOK)
3479 SvFLAGS(dstr) |= SVf_NOK;
3480 SvNVX(dstr) = SvNVX(sstr);
3482 if (sflags & SVp_IOK) {
3483 (void)SvIOKp_on(dstr);
3484 if (sflags & SVf_IOK)
3485 SvFLAGS(dstr) |= SVf_IOK;
3486 if (sflags & SVf_IVisUV)
3488 SvIVX(dstr) = SvIVX(sstr);
3491 else if (sflags & SVp_IOK) {
3492 if (sflags & SVf_IOK)
3493 (void)SvIOK_only(dstr);
3498 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
3499 if (sflags & SVf_IVisUV)
3501 SvIVX(dstr) = SvIVX(sstr);
3502 if (sflags & SVp_NOK) {
3503 if (sflags & SVf_NOK)
3504 (void)SvNOK_on(dstr);
3506 (void)SvNOKp_on(dstr);
3507 SvNVX(dstr) = SvNVX(sstr);
3510 else if (sflags & SVp_NOK) {
3511 if (sflags & SVf_NOK)
3512 (void)SvNOK_only(dstr);
3517 SvNVX(dstr) = SvNVX(sstr);
3520 if (dtype == SVt_PVGV) {
3521 if (ckWARN(WARN_MISC))
3522 Perl_warner(aTHX_ WARN_MISC, "Undefined value assigned to typeglob");
3525 (void)SvOK_off(dstr);
3527 if (SvTAINTED(sstr))
3532 =for apidoc sv_setsv_mg
3534 Like C<sv_setsv>, but also handles 'set' magic.
3540 Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
3542 sv_setsv(dstr,sstr);
3547 =for apidoc sv_setpvn
3549 Copies a string into an SV. The C<len> parameter indicates the number of
3550 bytes to be copied. Does not handle 'set' magic. See C<sv_setpvn_mg>.
3556 Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
3558 register char *dptr;
3560 SV_CHECK_THINKFIRST(sv);
3566 /* len is STRLEN which is unsigned, need to copy to signed */
3570 (void)SvUPGRADE(sv, SVt_PV);
3572 SvGROW(sv, len + 1);
3574 Move(ptr,dptr,len,char);
3577 (void)SvPOK_only_UTF8(sv); /* validate pointer */
3582 =for apidoc sv_setpvn_mg
3584 Like C<sv_setpvn>, but also handles 'set' magic.
3590 Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
3592 sv_setpvn(sv,ptr,len);
3597 =for apidoc sv_setpv
3599 Copies a string into an SV. The string must be null-terminated. Does not
3600 handle 'set' magic. See C<sv_setpv_mg>.
3606 Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
3608 register STRLEN len;
3610 SV_CHECK_THINKFIRST(sv);
3616 (void)SvUPGRADE(sv, SVt_PV);
3618 SvGROW(sv, len + 1);
3619 Move(ptr,SvPVX(sv),len+1,char);
3621 (void)SvPOK_only_UTF8(sv); /* validate pointer */
3626 =for apidoc sv_setpv_mg
3628 Like C<sv_setpv>, but also handles 'set' magic.
3634 Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
3641 =for apidoc sv_usepvn
3643 Tells an SV to use C<ptr> to find its string value. Normally the string is
3644 stored inside the SV but sv_usepvn allows the SV to use an outside string.
3645 The C<ptr> should point to memory that was allocated by C<malloc>. The
3646 string length, C<len>, must be supplied. This function will realloc the
3647 memory pointed to by C<ptr>, so that pointer should not be freed or used by
3648 the programmer after giving it to sv_usepvn. Does not handle 'set' magic.
3649 See C<sv_usepvn_mg>.
3655 Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
3657 SV_CHECK_THINKFIRST(sv);
3658 (void)SvUPGRADE(sv, SVt_PV);
3663 (void)SvOOK_off(sv);
3664 if (SvPVX(sv) && SvLEN(sv))
3665 Safefree(SvPVX(sv));
3666 Renew(ptr, len+1, char);
3669 SvLEN_set(sv, len+1);
3671 (void)SvPOK_only_UTF8(sv); /* validate pointer */
3676 =for apidoc sv_usepvn_mg
3678 Like C<sv_usepvn>, but also handles 'set' magic.
3684 Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
3686 sv_usepvn(sv,ptr,len);
3691 Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags)
3693 if (SvREADONLY(sv)) {
3695 char *pvx = SvPVX(sv);
3696 STRLEN len = SvCUR(sv);
3697 U32 hash = SvUVX(sv);
3698 SvGROW(sv, len + 1);
3699 Move(pvx,SvPVX(sv),len,char);
3703 unsharepvn(pvx,SvUTF8(sv)?-len:len,hash);
3705 else if (PL_curcop != &PL_compiling)
3706 Perl_croak(aTHX_ PL_no_modify);
3709 sv_unref_flags(sv, flags);
3710 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
3715 Perl_sv_force_normal(pTHX_ register SV *sv)
3717 sv_force_normal_flags(sv, 0);
3723 Efficient removal of characters from the beginning of the string buffer.
3724 SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside
3725 the string buffer. The C<ptr> becomes the first character of the adjusted
3732 Perl_sv_chop(pTHX_ register SV *sv, register char *ptr) /* like set but assuming ptr is in sv */
3736 register STRLEN delta;
3738 if (!ptr || !SvPOKp(sv))
3740 SV_CHECK_THINKFIRST(sv);
3741 if (SvTYPE(sv) < SVt_PVIV)
3742 sv_upgrade(sv,SVt_PVIV);
3745 if (!SvLEN(sv)) { /* make copy of shared string */
3746 char *pvx = SvPVX(sv);
3747 STRLEN len = SvCUR(sv);
3748 SvGROW(sv, len + 1);
3749 Move(pvx,SvPVX(sv),len,char);
3753 SvFLAGS(sv) |= SVf_OOK;
3755 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK|SVf_IVisUV);
3756 delta = ptr - SvPVX(sv);
3764 =for apidoc sv_catpvn
3766 Concatenates the string onto the end of the string which is in the SV. The
3767 C<len> indicates number of bytes to copy. Handles 'get' magic, but not
3768 'set' magic. See C<sv_catpvn_mg>.
3774 Perl_sv_catpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
3779 junk = SvPV_force(sv, tlen);
3780 SvGROW(sv, tlen + len + 1);
3783 Move(ptr,SvPVX(sv)+tlen,len,char);
3786 (void)SvPOK_only_UTF8(sv); /* validate pointer */
3791 =for apidoc sv_catpvn_mg
3793 Like C<sv_catpvn>, but also handles 'set' magic.
3799 Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
3801 sv_catpvn(sv,ptr,len);
3806 =for apidoc sv_catsv
3808 Concatenates the string from SV C<ssv> onto the end of the string in
3809 SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but
3810 not 'set' magic. See C<sv_catsv_mg>.
3815 Perl_sv_catsv(pTHX_ SV *dsv, register SV *ssv)
3821 if ((spv = SvPV(ssv, slen))) {
3822 bool dutf8 = DO_UTF8(dsv);
3823 bool sutf8 = DO_UTF8(ssv);
3826 sv_catpvn(dsv,spv,slen);
3829 /* Not modifying source SV, so taking a temporary copy. */
3830 SV* csv = sv_2mortal(newSVsv(ssv));
3834 sv_utf8_upgrade(csv);
3835 cpv = SvPV(csv,clen);
3836 sv_catpvn(dsv,cpv,clen);
3839 sv_utf8_upgrade(dsv);
3840 sv_catpvn(dsv,spv,slen);
3841 SvUTF8_on(dsv); /* If dsv has no wide characters. */
3848 =for apidoc sv_catsv_mg
3850 Like C<sv_catsv>, but also handles 'set' magic.
3856 Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv)
3863 =for apidoc sv_catpv
3865 Concatenates the string onto the end of the string which is in the SV.
3866 Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>.
3872 Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr)
3874 register STRLEN len;
3880 junk = SvPV_force(sv, tlen);
3882 SvGROW(sv, tlen + len + 1);
3885 Move(ptr,SvPVX(sv)+tlen,len+1,char);
3887 (void)SvPOK_only_UTF8(sv); /* validate pointer */
3892 =for apidoc sv_catpv_mg
3894 Like C<sv_catpv>, but also handles 'set' magic.
3900 Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr)
3907 Perl_newSV(pTHX_ STRLEN len)
3913 sv_upgrade(sv, SVt_PV);
3914 SvGROW(sv, len + 1);
3919 /* name is assumed to contain an SV* if (name && namelen == HEf_SVKEY) */
3922 =for apidoc sv_magic
3924 Adds magic to an SV.
3930 Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen)
3934 if (SvREADONLY(sv)) {
3935 if (PL_curcop != &PL_compiling && !strchr("gBf", how))
3936 Perl_croak(aTHX_ PL_no_modify);
3938 if (SvMAGICAL(sv) || (how == 't' && SvTYPE(sv) >= SVt_PVMG)) {
3939 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
3946 (void)SvUPGRADE(sv, SVt_PVMG);
3948 Newz(702,mg, 1, MAGIC);
3949 mg->mg_moremagic = SvMAGIC(sv);
3952 if (!obj || obj == sv || how == '#' || how == 'r')
3955 mg->mg_obj = SvREFCNT_inc(obj);
3956 mg->mg_flags |= MGf_REFCOUNTED;
3959 mg->mg_len = namlen;
3962 mg->mg_ptr = savepvn(name, namlen);
3963 else if (namlen == HEf_SVKEY)
3964 mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
3968 mg->mg_virtual = &PL_vtbl_sv;
3971 mg->mg_virtual = &PL_vtbl_amagic;
3974 mg->mg_virtual = &PL_vtbl_amagicelem;
3977 mg->mg_virtual = &PL_vtbl_ovrld;
3980 mg->mg_virtual = &PL_vtbl_bm;
3983 mg->mg_virtual = &PL_vtbl_regdata;
3986 mg->mg_virtual = &PL_vtbl_regdatum;
3989 mg->mg_virtual = &PL_vtbl_env;
3992 mg->mg_virtual = &PL_vtbl_fm;
3995 mg->mg_virtual = &PL_vtbl_envelem;
3998 mg->mg_virtual = &PL_vtbl_mglob;
4001 mg->mg_virtual = &PL_vtbl_isa;
4004 mg->mg_virtual = &PL_vtbl_isaelem;
4007 mg->mg_virtual = &PL_vtbl_nkeys;
4014 mg->mg_virtual = &PL_vtbl_dbline;
4018 mg->mg_virtual = &PL_vtbl_mutex;
4020 #endif /* USE_THREADS */
4021 #ifdef USE_LOCALE_COLLATE
4023 mg->mg_virtual = &PL_vtbl_collxfrm;
4025 #endif /* USE_LOCALE_COLLATE */
4027 mg->mg_virtual = &PL_vtbl_pack;
4031 mg->mg_virtual = &PL_vtbl_packelem;
4034 mg->mg_virtual = &PL_vtbl_regexp;
4037 mg->mg_virtual = &PL_vtbl_sig;
4040 mg->mg_virtual = &PL_vtbl_sigelem;
4043 mg->mg_virtual = &PL_vtbl_taint;
4047 mg->mg_virtual = &PL_vtbl_uvar;
4050 mg->mg_virtual = &PL_vtbl_vec;
4053 mg->mg_virtual = &PL_vtbl_substr;
4056 mg->mg_virtual = &PL_vtbl_defelem;
4059 mg->mg_virtual = &PL_vtbl_glob;
4062 mg->mg_virtual = &PL_vtbl_arylen;
4065 mg->mg_virtual = &PL_vtbl_pos;
4068 mg->mg_virtual = &PL_vtbl_backref;
4070 case '~': /* Reserved for use by extensions not perl internals. */
4071 /* Useful for attaching extension internal data to perl vars. */
4072 /* Note that multiple extensions may clash if magical scalars */
4073 /* etc holding private data from one are passed to another. */
4077 Perl_croak(aTHX_ "Don't know how to handle magic of type '%c'", how);
4081 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
4085 =for apidoc sv_unmagic
4087 Removes magic from an SV.
4093 Perl_sv_unmagic(pTHX_ SV *sv, int type)
4097 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
4100 for (mg = *mgp; mg; mg = *mgp) {
4101 if (mg->mg_type == type) {
4102 MGVTBL* vtbl = mg->mg_virtual;
4103 *mgp = mg->mg_moremagic;
4104 if (vtbl && vtbl->svt_free)
4105 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
4106 if (mg->mg_ptr && mg->mg_type != 'g')
4107 if (mg->mg_len >= 0)
4108 Safefree(mg->mg_ptr);
4109 else if (mg->mg_len == HEf_SVKEY)
4110 SvREFCNT_dec((SV*)mg->mg_ptr);
4111 if (mg->mg_flags & MGf_REFCOUNTED)
4112 SvREFCNT_dec(mg->mg_obj);
4116 mgp = &mg->mg_moremagic;
4120 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
4127 =for apidoc sv_rvweaken
4135 Perl_sv_rvweaken(pTHX_ SV *sv)
4138 if (!SvOK(sv)) /* let undefs pass */
4141 Perl_croak(aTHX_ "Can't weaken a nonreference");
4142 else if (SvWEAKREF(sv)) {
4143 if (ckWARN(WARN_MISC))
4144 Perl_warner(aTHX_ WARN_MISC, "Reference is already weak");
4148 sv_add_backref(tsv, sv);
4155 S_sv_add_backref(pTHX_ SV *tsv, SV *sv)
4159 if (SvMAGICAL(tsv) && (mg = mg_find(tsv, '<')))
4160 av = (AV*)mg->mg_obj;
4163 sv_magic(tsv, (SV*)av, '<', NULL, 0);
4164 SvREFCNT_dec(av); /* for sv_magic */
4170 S_sv_del_backref(pTHX_ SV *sv)
4177 if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, '<')))
4178 Perl_croak(aTHX_ "panic: del_backref");
4179 av = (AV *)mg->mg_obj;
4184 svp[i] = &PL_sv_undef; /* XXX */
4191 =for apidoc sv_insert
4193 Inserts a string at the specified offset/length within the SV. Similar to
4194 the Perl substr() function.
4200 Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, char *little, STRLEN littlelen)
4204 register char *midend;
4205 register char *bigend;
4211 Perl_croak(aTHX_ "Can't modify non-existent substring");
4212 SvPV_force(bigstr, curlen);
4213 (void)SvPOK_only_UTF8(bigstr);
4214 if (offset + len > curlen) {
4215 SvGROW(bigstr, offset+len+1);
4216 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
4217 SvCUR_set(bigstr, offset+len);
4221 i = littlelen - len;
4222 if (i > 0) { /* string might grow */
4223 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
4224 mid = big + offset + len;
4225 midend = bigend = big + SvCUR(bigstr);
4228 while (midend > mid) /* shove everything down */
4229 *--bigend = *--midend;
4230 Move(little,big+offset,littlelen,char);
4236 Move(little,SvPVX(bigstr)+offset,len,char);
4241 big = SvPVX(bigstr);
4244 bigend = big + SvCUR(bigstr);
4246 if (midend > bigend)
4247 Perl_croak(aTHX_ "panic: sv_insert");
4249 if (mid - big > bigend - midend) { /* faster to shorten from end */
4251 Move(little, mid, littlelen,char);
4254 i = bigend - midend;
4256 Move(midend, mid, i,char);
4260 SvCUR_set(bigstr, mid - big);
4263 else if ((i = mid - big)) { /* faster from front */
4264 midend -= littlelen;
4266 sv_chop(bigstr,midend-i);
4271 Move(little, mid, littlelen,char);
4273 else if (littlelen) {
4274 midend -= littlelen;
4275 sv_chop(bigstr,midend);
4276 Move(little,midend,littlelen,char);
4279 sv_chop(bigstr,midend);
4285 =for apidoc sv_replace
4287 Make the first argument a copy of the second, then delete the original.
4293 Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv)
4295 U32 refcnt = SvREFCNT(sv);
4296 SV_CHECK_THINKFIRST(sv);
4297 if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL))
4298 Perl_warner(aTHX_ WARN_INTERNAL, "Reference miscount in sv_replace()");
4299 if (SvMAGICAL(sv)) {
4303 sv_upgrade(nsv, SVt_PVMG);
4304 SvMAGIC(nsv) = SvMAGIC(sv);
4305 SvFLAGS(nsv) |= SvMAGICAL(sv);
4311 assert(!SvREFCNT(sv));
4312 StructCopy(nsv,sv,SV);
4313 SvREFCNT(sv) = refcnt;
4314 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
4319 =for apidoc sv_clear
4321 Clear an SV, making it empty. Does not free the memory used by the SV
4328 Perl_sv_clear(pTHX_ register SV *sv)
4332 assert(SvREFCNT(sv) == 0);
4335 if (PL_defstash) { /* Still have a symbol table? */
4340 Zero(&tmpref, 1, SV);
4341 sv_upgrade(&tmpref, SVt_RV);
4343 SvREADONLY_on(&tmpref); /* DESTROY() could be naughty */
4344 SvREFCNT(&tmpref) = 1;
4347 stash = SvSTASH(sv);
4348 destructor = StashHANDLER(stash,DESTROY);
4351 PUSHSTACKi(PERLSI_DESTROY);
4352 SvRV(&tmpref) = SvREFCNT_inc(sv);
4357 call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR);
4363 } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
4365 del_XRV(SvANY(&tmpref));
4368 if (PL_in_clean_objs)
4369 Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'",
4371 /* DESTROY gave object new lease on life */
4377 SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */
4378 SvOBJECT_off(sv); /* Curse the object. */
4379 if (SvTYPE(sv) != SVt_PVIO)
4380 --PL_sv_objcount; /* XXX Might want something more general */
4383 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
4386 switch (SvTYPE(sv)) {
4389 IoIFP(sv) != PerlIO_stdin() &&
4390 IoIFP(sv) != PerlIO_stdout() &&
4391 IoIFP(sv) != PerlIO_stderr())
4393 io_close((IO*)sv, FALSE);
4395 if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP))
4396 PerlDir_close(IoDIRP(sv));
4397 IoDIRP(sv) = (DIR*)NULL;
4398 Safefree(IoTOP_NAME(sv));
4399 Safefree(IoFMT_NAME(sv));
4400 Safefree(IoBOTTOM_NAME(sv));
4415 SvREFCNT_dec(LvTARG(sv));
4419 Safefree(GvNAME(sv));
4420 /* cannot decrease stash refcount yet, as we might recursively delete
4421 ourselves when the refcnt drops to zero. Delay SvREFCNT_dec
4422 of stash until current sv is completely gone.
4423 -- JohnPC, 27 Mar 1998 */
4424 stash = GvSTASH(sv);
4430 (void)SvOOK_off(sv);
4438 SvREFCNT_dec(SvRV(sv));
4440 else if (SvPVX(sv) && SvLEN(sv))
4441 Safefree(SvPVX(sv));
4442 else if (SvPVX(sv) && SvREADONLY(sv) && SvFAKE(sv)) {
4443 unsharepvn(SvPVX(sv),SvUTF8(sv)?-SvCUR(sv):SvCUR(sv),SvUVX(sv));
4455 switch (SvTYPE(sv)) {
4471 del_XPVIV(SvANY(sv));
4474 del_XPVNV(SvANY(sv));
4477 del_XPVMG(SvANY(sv));
4480 del_XPVLV(SvANY(sv));
4483 del_XPVAV(SvANY(sv));
4486 del_XPVHV(SvANY(sv));
4489 del_XPVCV(SvANY(sv));
4492 del_XPVGV(SvANY(sv));
4493 /* code duplication for increased performance. */
4494 SvFLAGS(sv) &= SVf_BREAK;
4495 SvFLAGS(sv) |= SVTYPEMASK;
4496 /* decrease refcount of the stash that owns this GV, if any */
4498 SvREFCNT_dec(stash);
4499 return; /* not break, SvFLAGS reset already happened */
4501 del_XPVBM(SvANY(sv));
4504 del_XPVFM(SvANY(sv));
4507 del_XPVIO(SvANY(sv));
4510 SvFLAGS(sv) &= SVf_BREAK;
4511 SvFLAGS(sv) |= SVTYPEMASK;
4515 Perl_sv_newref(pTHX_ SV *sv)
4518 ATOMIC_INC(SvREFCNT(sv));
4525 Free the memory used by an SV.
4531 Perl_sv_free(pTHX_ SV *sv)
4533 int refcount_is_zero;
4537 if (SvREFCNT(sv) == 0) {
4538 if (SvFLAGS(sv) & SVf_BREAK)
4540 if (PL_in_clean_all) /* All is fair */
4542 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
4543 /* make sure SvREFCNT(sv)==0 happens very seldom */
4544 SvREFCNT(sv) = (~(U32)0)/2;
4547 if (ckWARN_d(WARN_INTERNAL))
4548 Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free unreferenced scalar");
4551 ATOMIC_DEC_AND_TEST(refcount_is_zero, SvREFCNT(sv));
4552 if (!refcount_is_zero)
4556 if (ckWARN_d(WARN_DEBUGGING))
4557 Perl_warner(aTHX_ WARN_DEBUGGING,
4558 "Attempt to free temp prematurely: SV 0x%"UVxf,
4563 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
4564 /* make sure SvREFCNT(sv)==0 happens very seldom */
4565 SvREFCNT(sv) = (~(U32)0)/2;
4576 Returns the length of the string in the SV. See also C<SvCUR>.
4582 Perl_sv_len(pTHX_ register SV *sv)
4591 len = mg_length(sv);
4593 junk = SvPV(sv, len);
4598 =for apidoc sv_len_utf8
4600 Returns the number of characters in the string in an SV, counting wide
4601 UTF8 bytes as a single character.
4607 Perl_sv_len_utf8(pTHX_ register SV *sv)
4613 return mg_length(sv);
4617 U8 *s = (U8*)SvPV(sv, len);
4619 return Perl_utf8_length(aTHX_ s, s + len);
4624 Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp)
4629 I32 uoffset = *offsetp;
4635 start = s = (U8*)SvPV(sv, len);
4637 while (s < send && uoffset--)
4641 *offsetp = s - start;
4645 while (s < send && ulen--)
4655 Perl_sv_pos_b2u(pTHX_ register SV *sv, I32* offsetp)
4664 s = (U8*)SvPV(sv, len);
4666 Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset");
4667 send = s + *offsetp;
4672 if (utf8_to_uv(s, UTF8SKIP(s), &n, 0)) {
4686 Returns a boolean indicating whether the strings in the two SVs are
4693 Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2)
4700 bool pv1tmp = FALSE;
4701 bool pv2tmp = FALSE;
4708 pv1 = SvPV(sv1, cur1);
4715 pv2 = SvPV(sv2, cur2);
4717 /* do not utf8ize the comparands as a side-effect */
4718 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTE) {
4719 bool is_utf8 = TRUE;
4721 if (PL_hints & HINT_UTF8_DISTINCT)
4725 char *pv = (char*)bytes_from_utf8((U8*)pv1, &cur1, &is_utf8);
4728 pv1tmp = (pv != pv1);
4732 char *pv = (char *)bytes_from_utf8((U8*)pv2, &cur2, &is_utf8);
4735 pv2tmp = (pv != pv2);
4741 eq = memEQ(pv1, pv2, cur1);
4754 Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
4755 string in C<sv1> is less than, equal to, or greater than the string in
4762 Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2)
4767 bool pv1tmp = FALSE;
4768 bool pv2tmp = FALSE;
4775 pv1 = SvPV(sv1, cur1);
4782 pv2 = SvPV(sv2, cur2);
4784 /* do not utf8ize the comparands as a side-effect */
4785 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTE) {
4786 if (PL_hints & HINT_UTF8_DISTINCT)
4787 return SvUTF8(sv1) ? 1 : -1;
4790 pv2 = (char*)bytes_to_utf8((U8*)pv2, &cur2);
4794 pv1 = (char*)bytes_to_utf8((U8*)pv1, &cur1);
4800 cmp = cur2 ? -1 : 0;
4804 I32 retval = memcmp((void*)pv1, (void*)pv2, cur1 < cur2 ? cur1 : cur2);
4807 cmp = retval < 0 ? -1 : 1;
4808 } else if (cur1 == cur2) {
4811 cmp = cur1 < cur2 ? -1 : 1;
4824 =for apidoc sv_cmp_locale
4826 Compares the strings in two SVs in a locale-aware manner. See
4833 Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2)
4835 #ifdef USE_LOCALE_COLLATE
4841 if (PL_collation_standard)
4845 pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL;
4847 pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL;
4849 if (!pv1 || !len1) {
4860 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
4863 return retval < 0 ? -1 : 1;
4866 * When the result of collation is equality, that doesn't mean
4867 * that there are no differences -- some locales exclude some
4868 * characters from consideration. So to avoid false equalities,
4869 * we use the raw string as a tiebreaker.
4875 #endif /* USE_LOCALE_COLLATE */
4877 return sv_cmp(sv1, sv2);
4880 #ifdef USE_LOCALE_COLLATE
4882 * Any scalar variable may carry an 'o' magic that contains the
4883 * scalar data of the variable transformed to such a format that
4884 * a normal memory comparison can be used to compare the data
4885 * according to the locale settings.
4888 Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp)
4892 mg = SvMAGICAL(sv) ? mg_find(sv, 'o') : (MAGIC *) NULL;
4893 if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
4898 Safefree(mg->mg_ptr);
4900 if ((xf = mem_collxfrm(s, len, &xlen))) {
4901 if (SvREADONLY(sv)) {
4904 return xf + sizeof(PL_collation_ix);
4907 sv_magic(sv, 0, 'o', 0, 0);
4908 mg = mg_find(sv, 'o');
4921 if (mg && mg->mg_ptr) {
4923 return mg->mg_ptr + sizeof(PL_collation_ix);
4931 #endif /* USE_LOCALE_COLLATE */
4936 Get a line from the filehandle and store it into the SV, optionally
4937 appending to the currently-stored string.
4943 Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append)
4947 register STDCHAR rslast;
4948 register STDCHAR *bp;
4952 SV_CHECK_THINKFIRST(sv);
4953 (void)SvUPGRADE(sv, SVt_PV);
4957 if (RsSNARF(PL_rs)) {
4961 else if (RsRECORD(PL_rs)) {
4962 I32 recsize, bytesread;
4965 /* Grab the size of the record we're getting */
4966 recsize = SvIV(SvRV(PL_rs));
4967 (void)SvPOK_only(sv); /* Validate pointer */
4968 buffer = SvGROW(sv, recsize + 1);
4971 /* VMS wants read instead of fread, because fread doesn't respect */
4972 /* RMS record boundaries. This is not necessarily a good thing to be */
4973 /* doing, but we've got no other real choice */
4974 bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize);
4976 bytesread = PerlIO_read(fp, buffer, recsize);
4978 SvCUR_set(sv, bytesread);
4979 buffer[bytesread] = '\0';
4980 if (PerlIO_isutf8(fp))
4984 return(SvCUR(sv) ? SvPVX(sv) : Nullch);
4986 else if (RsPARA(PL_rs)) {
4991 /* Get $/ i.e. PL_rs into same encoding as stream wants */
4992 if (PerlIO_isutf8(fp)) {
4993 rsptr = SvPVutf8(PL_rs, rslen);
4996 if (SvUTF8(PL_rs)) {
4997 if (!sv_utf8_downgrade(PL_rs, TRUE)) {
4998 Perl_croak(aTHX_ "Wide character in $/");
5001 rsptr = SvPV(PL_rs, rslen);
5005 rslast = rslen ? rsptr[rslen - 1] : '\0';
5007 if (RsPARA(PL_rs)) { /* have to do this both before and after */
5008 do { /* to make sure file boundaries work right */
5011 i = PerlIO_getc(fp);
5015 PerlIO_ungetc(fp,i);
5021 /* See if we know enough about I/O mechanism to cheat it ! */
5023 /* This used to be #ifdef test - it is made run-time test for ease
5024 of abstracting out stdio interface. One call should be cheap
5025 enough here - and may even be a macro allowing compile
5029 if (PerlIO_fast_gets(fp)) {
5032 * We're going to steal some values from the stdio struct
5033 * and put EVERYTHING in the innermost loop into registers.
5035 register STDCHAR *ptr;
5039 #if defined(VMS) && defined(PERLIO_IS_STDIO)
5040 /* An ungetc()d char is handled separately from the regular
5041 * buffer, so we getc() it back out and stuff it in the buffer.
5043 i = PerlIO_getc(fp);
5044 if (i == EOF) return 0;
5045 *(--((*fp)->_ptr)) = (unsigned char) i;
5049 /* Here is some breathtakingly efficient cheating */
5051 cnt = PerlIO_get_cnt(fp); /* get count into register */
5052 (void)SvPOK_only(sv); /* validate pointer */
5053 if (SvLEN(sv) - append <= cnt + 1) { /* make sure we have the room */
5054 if (cnt > 80 && SvLEN(sv) > append) {
5055 shortbuffered = cnt - SvLEN(sv) + append + 1;
5056 cnt -= shortbuffered;
5060 /* remember that cnt can be negative */
5061 SvGROW(sv, append + (cnt <= 0 ? 2 : (cnt + 1)));
5066 bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */
5067 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
5068 DEBUG_P(PerlIO_printf(Perl_debug_log,
5069 "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
5070 DEBUG_P(PerlIO_printf(Perl_debug_log,
5071 "Screamer: entering: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
5072 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
5073 PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
5078 while (cnt > 0) { /* this | eat */
5080 if ((*bp++ = *ptr++) == rslast) /* really | dust */
5081 goto thats_all_folks; /* screams | sed :-) */
5085 Copy(ptr, bp, cnt, char); /* this | eat */
5086 bp += cnt; /* screams | dust */
5087 ptr += cnt; /* louder | sed :-) */
5092 if (shortbuffered) { /* oh well, must extend */
5093 cnt = shortbuffered;
5095 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
5097 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
5098 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
5102 DEBUG_P(PerlIO_printf(Perl_debug_log,
5103 "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n",
5104 PTR2UV(ptr),(long)cnt));
5105 PerlIO_set_ptrcnt(fp, ptr, cnt); /* deregisterize cnt and ptr */
5106 DEBUG_P(PerlIO_printf(Perl_debug_log,
5107 "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
5108 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
5109 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
5110 /* This used to call 'filbuf' in stdio form, but as that behaves like
5111 getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
5112 another abstraction. */
5113 i = PerlIO_getc(fp); /* get more characters */
5114 DEBUG_P(PerlIO_printf(Perl_debug_log,
5115 "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
5116 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
5117 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
5118 cnt = PerlIO_get_cnt(fp);
5119 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
5120 DEBUG_P(PerlIO_printf(Perl_debug_log,
5121 "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
5123 if (i == EOF) /* all done for ever? */
5124 goto thats_really_all_folks;
5126 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
5128 SvGROW(sv, bpx + cnt + 2);
5129 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
5131 *bp++ = i; /* store character from PerlIO_getc */
5133 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
5134 goto thats_all_folks;
5138 if ((rslen > 1 && (bp - (STDCHAR*)SvPVX(sv) < rslen)) ||
5139 memNE((char*)bp - rslen, rsptr, rslen))
5140 goto screamer; /* go back to the fray */
5141 thats_really_all_folks:
5143 cnt += shortbuffered;
5144 DEBUG_P(PerlIO_printf(Perl_debug_log,
5145 "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
5146 PerlIO_set_ptrcnt(fp, ptr, cnt); /* put these back or we're in trouble */
5147 DEBUG_P(PerlIO_printf(Perl_debug_log,
5148 "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
5149 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
5150 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
5152 SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */
5153 DEBUG_P(PerlIO_printf(Perl_debug_log,
5154 "Screamer: done, len=%ld, string=|%.*s|\n",
5155 (long)SvCUR(sv),(int)SvCUR(sv),SvPVX(sv)));
5160 /*The big, slow, and stupid way */
5163 /* Need to work around EPOC SDK features */
5164 /* On WINS: MS VC5 generates calls to _chkstk, */
5165 /* if a `large' stack frame is allocated */
5166 /* gcc on MARM does not generate calls like these */
5172 register STDCHAR *bpe = buf + sizeof(buf);
5174 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = i) != rslast && bp < bpe)
5175 ; /* keep reading */
5179 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
5180 /* Accomodate broken VAXC compiler, which applies U8 cast to
5181 * both args of ?: operator, causing EOF to change into 255
5183 if (cnt) { i = (U8)buf[cnt - 1]; } else { i = EOF; }
5187 sv_catpvn(sv, (char *) buf, cnt);
5189 sv_setpvn(sv, (char *) buf, cnt);
5191 if (i != EOF && /* joy */
5193 SvCUR(sv) < rslen ||
5194 memNE(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
5198 * If we're reading from a TTY and we get a short read,
5199 * indicating that the user hit his EOF character, we need
5200 * to notice it now, because if we try to read from the TTY
5201 * again, the EOF condition will disappear.
5203 * The comparison of cnt to sizeof(buf) is an optimization
5204 * that prevents unnecessary calls to feof().
5208 if (!(cnt < sizeof(buf) && PerlIO_eof(fp)))
5213 if (RsPARA(PL_rs)) { /* have to do this both before and after */
5214 while (i != EOF) { /* to make sure file boundaries work right */
5215 i = PerlIO_getc(fp);
5217 PerlIO_ungetc(fp,i);
5223 if (PerlIO_isutf8(fp))
5228 return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch;
5235 Auto-increment of the value in the SV.
5241 Perl_sv_inc(pTHX_ register SV *sv)
5250 if (SvTHINKFIRST(sv)) {
5251 if (SvREADONLY(sv)) {
5252 if (PL_curcop != &PL_compiling)
5253 Perl_croak(aTHX_ PL_no_modify);
5257 if (SvAMAGIC(sv) && AMG_CALLun(sv,inc))
5259 i = PTR2IV(SvRV(sv));
5264 flags = SvFLAGS(sv);
5265 if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) {
5266 /* It's (privately or publicly) a float, but not tested as an
5267 integer, so test it to see. */
5269 flags = SvFLAGS(sv);
5271 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
5272 /* It's publicly an integer, or privately an integer-not-float */
5275 if (SvUVX(sv) == UV_MAX)
5276 sv_setnv(sv, (NV)UV_MAX + 1.0);
5278 (void)SvIOK_only_UV(sv);
5281 if (SvIVX(sv) == IV_MAX)
5282 sv_setuv(sv, (UV)IV_MAX + 1);
5284 (void)SvIOK_only(sv);
5290 if (flags & SVp_NOK) {
5291 (void)SvNOK_only(sv);
5296 if (!(flags & SVp_POK) || !*SvPVX(sv)) {
5297 if ((flags & SVTYPEMASK) < SVt_PVIV)
5298 sv_upgrade(sv, SVt_IV);
5299 (void)SvIOK_only(sv);
5304 while (isALPHA(*d)) d++;
5305 while (isDIGIT(*d)) d++;
5307 #ifdef PERL_PRESERVE_IVUV
5308 /* Got to punt this an an integer if needs be, but we don't issue
5309 warnings. Probably ought to make the sv_iv_please() that does
5310 the conversion if possible, and silently. */
5311 I32 numtype = looks_like_number(sv);
5312 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
5313 /* Need to try really hard to see if it's an integer.
5314 9.22337203685478e+18 is an integer.
5315 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
5316 so $a="9.22337203685478e+18"; $a+0; $a++
5317 needs to be the same as $a="9.22337203685478e+18"; $a++
5324 /* sv_2iv *should* have made this an NV */
5325 if (flags & SVp_NOK) {
5326 (void)SvNOK_only(sv);
5330 /* I don't think we can get here. Maybe I should assert this
5331 And if we do get here I suspect that sv_setnv will croak. NWC
5333 #if defined(USE_LONG_DOUBLE)
5334 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"PERL_PRIgldbl"\n",
5335 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
5337 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%g\n",
5338 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
5341 #endif /* PERL_PRESERVE_IVUV */
5342 sv_setnv(sv,Atof(SvPVX(sv)) + 1.0);
5346 while (d >= SvPVX(sv)) {
5354 /* MKS: The original code here died if letters weren't consecutive.
5355 * at least it didn't have to worry about non-C locales. The
5356 * new code assumes that ('z'-'a')==('Z'-'A'), letters are
5357 * arranged in order (although not consecutively) and that only
5358 * [A-Za-z] are accepted by isALPHA in the C locale.
5360 if (*d != 'z' && *d != 'Z') {
5361 do { ++*d; } while (!isALPHA(*d));
5364 *(d--) -= 'z' - 'a';
5369 *(d--) -= 'z' - 'a' + 1;
5373 /* oh,oh, the number grew */
5374 SvGROW(sv, SvCUR(sv) + 2);
5376 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--)
5387 Auto-decrement of the value in the SV.
5393 Perl_sv_dec(pTHX_ register SV *sv)
5401 if (SvTHINKFIRST(sv)) {
5402 if (SvREADONLY(sv)) {
5403 if (PL_curcop != &PL_compiling)
5404 Perl_croak(aTHX_ PL_no_modify);
5408 if (SvAMAGIC(sv) && AMG_CALLun(sv,dec))
5410 i = PTR2IV(SvRV(sv));
5415 /* Unlike sv_inc we don't have to worry about string-never-numbers
5416 and keeping them magic. But we mustn't warn on punting */
5417 flags = SvFLAGS(sv);
5418 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
5419 /* It's publicly an integer, or privately an integer-not-float */
5422 if (SvUVX(sv) == 0) {
5423 (void)SvIOK_only(sv);
5427 (void)SvIOK_only_UV(sv);
5431 if (SvIVX(sv) == IV_MIN)
5432 sv_setnv(sv, (NV)IV_MIN - 1.0);
5434 (void)SvIOK_only(sv);
5440 if (flags & SVp_NOK) {
5442 (void)SvNOK_only(sv);
5445 if (!(flags & SVp_POK)) {
5446 if ((flags & SVTYPEMASK) < SVt_PVNV)
5447 sv_upgrade(sv, SVt_NV);
5449 (void)SvNOK_only(sv);
5452 #ifdef PERL_PRESERVE_IVUV
5454 I32 numtype = looks_like_number(sv);
5455 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
5456 /* Need to try really hard to see if it's an integer.
5457 9.22337203685478e+18 is an integer.
5458 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
5459 so $a="9.22337203685478e+18"; $a+0; $a--
5460 needs to be the same as $a="9.22337203685478e+18"; $a--
5467 /* sv_2iv *should* have made this an NV */
5468 if (flags & SVp_NOK) {
5469 (void)SvNOK_only(sv);
5473 /* I don't think we can get here. Maybe I should assert this
5474 And if we do get here I suspect that sv_setnv will croak. NWC
5476 #if defined(USE_LONG_DOUBLE)
5477 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"PERL_PRIgldbl"\n",
5478 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
5480 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%g\n",
5481 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
5485 #endif /* PERL_PRESERVE_IVUV */
5486 sv_setnv(sv,Atof(SvPVX(sv)) - 1.0); /* punt */
5490 =for apidoc sv_mortalcopy
5492 Creates a new SV which is a copy of the original SV. The new SV is marked
5498 /* Make a string that will exist for the duration of the expression
5499 * evaluation. Actually, it may have to last longer than that, but
5500 * hopefully we won't free it until it has been assigned to a
5501 * permanent location. */
5504 Perl_sv_mortalcopy(pTHX_ SV *oldstr)
5509 sv_setsv(sv,oldstr);
5511 PL_tmps_stack[++PL_tmps_ix] = sv;
5517 =for apidoc sv_newmortal
5519 Creates a new SV which is mortal. The reference count of the SV is set to 1.
5525 Perl_sv_newmortal(pTHX)
5530 SvFLAGS(sv) = SVs_TEMP;
5532 PL_tmps_stack[++PL_tmps_ix] = sv;
5537 =for apidoc sv_2mortal
5539 Marks an SV as mortal. The SV will be destroyed when the current context
5545 /* same thing without the copying */
5548 Perl_sv_2mortal(pTHX_ register SV *sv)
5552 if (SvREADONLY(sv) && SvIMMORTAL(sv))
5555 PL_tmps_stack[++PL_tmps_ix] = sv;
5563 Creates a new SV and copies a string into it. The reference count for the
5564 SV is set to 1. If C<len> is zero, Perl will compute the length using
5565 strlen(). For efficiency, consider using C<newSVpvn> instead.
5571 Perl_newSVpv(pTHX_ const char *s, STRLEN len)
5578 sv_setpvn(sv,s,len);
5583 =for apidoc newSVpvn
5585 Creates a new SV and copies a string into it. The reference count for the
5586 SV is set to 1. Note that if C<len> is zero, Perl will create a zero length
5587 string. You are responsible for ensuring that the source string is at least
5594 Perl_newSVpvn(pTHX_ const char *s, STRLEN len)
5599 sv_setpvn(sv,s,len);
5604 =for apidoc newSVpvn_share
5606 Creates a new SV and populates it with a string from
5607 the string table. Turns on READONLY and FAKE.
5608 The idea here is that as string table is used for shared hash
5609 keys these strings will have SvPVX == HeKEY and hash lookup
5610 will avoid string compare.
5616 Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
5619 bool is_utf8 = FALSE;
5624 if (is_utf8 && !(PL_hints & HINT_UTF8_DISTINCT))
5625 src = (char*)bytes_from_utf8((U8*)src, (STRLEN*)&len, &is_utf8);
5627 PERL_HASH(hash, src, len);
5629 sv_upgrade(sv, SVt_PVIV);
5630 SvPVX(sv) = sharepvn(src, is_utf8?-len:len, hash);
5642 #if defined(PERL_IMPLICIT_CONTEXT)
5644 Perl_newSVpvf_nocontext(const char* pat, ...)
5649 va_start(args, pat);
5650 sv = vnewSVpvf(pat, &args);
5657 =for apidoc newSVpvf
5659 Creates a new SV an initialize it with the string formatted like
5666 Perl_newSVpvf(pTHX_ const char* pat, ...)
5670 va_start(args, pat);
5671 sv = vnewSVpvf(pat, &args);
5677 Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args)
5681 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
5688 Creates a new SV and copies a floating point value into it.
5689 The reference count for the SV is set to 1.
5695 Perl_newSVnv(pTHX_ NV n)
5707 Creates a new SV and copies an integer into it. The reference count for the
5714 Perl_newSViv(pTHX_ IV i)
5726 Creates a new SV and copies an unsigned integer into it.
5727 The reference count for the SV is set to 1.
5733 Perl_newSVuv(pTHX_ UV u)
5743 =for apidoc newRV_noinc
5745 Creates an RV wrapper for an SV. The reference count for the original
5746 SV is B<not> incremented.
5752 Perl_newRV_noinc(pTHX_ SV *tmpRef)
5757 sv_upgrade(sv, SVt_RV);
5764 /* newRV_inc is #defined to newRV in sv.h */
5766 Perl_newRV(pTHX_ SV *tmpRef)
5768 return newRV_noinc(SvREFCNT_inc(tmpRef));
5774 Creates a new SV which is an exact duplicate of the original SV.
5779 /* make an exact duplicate of old */
5782 Perl_newSVsv(pTHX_ register SV *old)
5788 if (SvTYPE(old) == SVTYPEMASK) {
5789 if (ckWARN_d(WARN_INTERNAL))
5790 Perl_warner(aTHX_ WARN_INTERNAL, "semi-panic: attempt to dup freed string");
5805 Perl_sv_reset(pTHX_ register char *s, HV *stash)
5813 char todo[PERL_UCHAR_MAX+1];
5818 if (!*s) { /* reset ?? searches */
5819 for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) {
5820 pm->op_pmdynflags &= ~PMdf_USED;
5825 /* reset variables */
5827 if (!HvARRAY(stash))
5830 Zero(todo, 256, char);
5832 i = (unsigned char)*s;
5836 max = (unsigned char)*s++;
5837 for ( ; i <= max; i++) {
5840 for (i = 0; i <= (I32) HvMAX(stash); i++) {
5841 for (entry = HvARRAY(stash)[i];
5843 entry = HeNEXT(entry))
5845 if (!todo[(U8)*HeKEY(entry)])
5847 gv = (GV*)HeVAL(entry);
5849 if (SvTHINKFIRST(sv)) {
5850 if (!SvREADONLY(sv) && SvROK(sv))
5855 if (SvTYPE(sv) >= SVt_PV) {
5857 if (SvPVX(sv) != Nullch)
5864 if (GvHV(gv) && !HvNAME(GvHV(gv))) {
5866 #ifdef USE_ENVIRON_ARRAY
5868 environ[0] = Nullch;
5877 Perl_sv_2io(pTHX_ SV *sv)
5883 switch (SvTYPE(sv)) {
5891 Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv));
5895 Perl_croak(aTHX_ PL_no_usym, "filehandle");
5897 return sv_2io(SvRV(sv));
5898 gv = gv_fetchpv(SvPV(sv,n_a), FALSE, SVt_PVIO);
5904 Perl_croak(aTHX_ "Bad filehandle: %s", SvPV(sv,n_a));
5911 Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref)
5918 return *gvp = Nullgv, Nullcv;
5919 switch (SvTYPE(sv)) {
5938 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
5939 tryAMAGICunDEREF(to_cv);
5942 if (SvTYPE(sv) == SVt_PVCV) {
5951 Perl_croak(aTHX_ "Not a subroutine reference");
5956 gv = gv_fetchpv(SvPV(sv, n_a), lref, SVt_PVCV);
5962 if (lref && !GvCVu(gv)) {
5965 tmpsv = NEWSV(704,0);
5966 gv_efullname3(tmpsv, gv, Nullch);
5967 /* XXX this is probably not what they think they're getting.
5968 * It has the same effect as "sub name;", i.e. just a forward
5970 newSUB(start_subparse(FALSE, 0),
5971 newSVOP(OP_CONST, 0, tmpsv),
5976 Perl_croak(aTHX_ "Unable to create sub named \"%s\"", SvPV(sv,n_a));
5985 Returns true if the SV has a true value by Perl's rules.
5991 Perl_sv_true(pTHX_ register SV *sv)
5997 if ((tXpv = (XPV*)SvANY(sv)) &&
5998 (tXpv->xpv_cur > 1 ||
5999 (tXpv->xpv_cur && *tXpv->xpv_pv != '0')))
6006 return SvIVX(sv) != 0;
6009 return SvNVX(sv) != 0.0;
6011 return sv_2bool(sv);
6017 Perl_sv_iv(pTHX_ register SV *sv)
6021 return (IV)SvUVX(sv);
6028 Perl_sv_uv(pTHX_ register SV *sv)
6033 return (UV)SvIVX(sv);
6039 Perl_sv_nv(pTHX_ register SV *sv)
6047 Perl_sv_pv(pTHX_ SV *sv)
6054 return sv_2pv(sv, &n_a);
6058 Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp)
6064 return sv_2pv(sv, lp);
6068 =for apidoc sv_pvn_force
6070 Get a sensible string out of the SV somehow.
6076 Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp)
6080 if (SvTHINKFIRST(sv) && !SvROK(sv))
6081 sv_force_normal(sv);
6087 if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) {
6088 Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
6089 PL_op_name[PL_op->op_type]);
6093 if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */
6098 (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */
6099 SvGROW(sv, len + 1);
6100 Move(s,SvPVX(sv),len,char);
6105 SvPOK_on(sv); /* validate pointer */
6107 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
6108 PTR2UV(sv),SvPVX(sv)));
6115 Perl_sv_pvbyte(pTHX_ SV *sv)
6121 Perl_sv_pvbyten(pTHX_ SV *sv, STRLEN *lp)
6123 return sv_pvn(sv,lp);
6127 Perl_sv_pvbyten_force(pTHX_ SV *sv, STRLEN *lp)
6129 return sv_pvn_force(sv,lp);
6133 Perl_sv_pvutf8(pTHX_ SV *sv)
6135 sv_utf8_upgrade(sv);
6140 Perl_sv_pvutf8n(pTHX_ SV *sv, STRLEN *lp)
6142 sv_utf8_upgrade(sv);
6143 return sv_pvn(sv,lp);
6147 =for apidoc sv_pvutf8n_force
6149 Get a sensible UTF8-encoded string out of the SV somehow. See
6156 Perl_sv_pvutf8n_force(pTHX_ SV *sv, STRLEN *lp)
6158 sv_utf8_upgrade(sv);
6159 return sv_pvn_force(sv,lp);
6163 =for apidoc sv_reftype
6165 Returns a string describing what the SV is a reference to.
6171 Perl_sv_reftype(pTHX_ SV *sv, int ob)
6173 if (ob && SvOBJECT(sv))
6174 return HvNAME(SvSTASH(sv));
6176 switch (SvTYPE(sv)) {
6190 case SVt_PVLV: return "LVALUE";
6191 case SVt_PVAV: return "ARRAY";
6192 case SVt_PVHV: return "HASH";
6193 case SVt_PVCV: return "CODE";
6194 case SVt_PVGV: return "GLOB";
6195 case SVt_PVFM: return "FORMAT";
6196 case SVt_PVIO: return "IO";
6197 default: return "UNKNOWN";
6203 =for apidoc sv_isobject
6205 Returns a boolean indicating whether the SV is an RV pointing to a blessed
6206 object. If the SV is not an RV, or if the object is not blessed, then this
6213 Perl_sv_isobject(pTHX_ SV *sv)
6230 Returns a boolean indicating whether the SV is blessed into the specified
6231 class. This does not check for subtypes; use C<sv_derived_from> to verify
6232 an inheritance relationship.
6238 Perl_sv_isa(pTHX_ SV *sv, const char *name)
6250 return strEQ(HvNAME(SvSTASH(sv)), name);
6256 Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then
6257 it will be upgraded to one. If C<classname> is non-null then the new SV will
6258 be blessed in the specified package. The new SV is returned and its
6259 reference count is 1.
6265 Perl_newSVrv(pTHX_ SV *rv, const char *classname)
6271 SV_CHECK_THINKFIRST(rv);
6274 if (SvTYPE(rv) >= SVt_PVMG) {
6275 U32 refcnt = SvREFCNT(rv);
6279 SvREFCNT(rv) = refcnt;
6282 if (SvTYPE(rv) < SVt_RV)
6283 sv_upgrade(rv, SVt_RV);
6284 else if (SvTYPE(rv) > SVt_RV) {
6285 (void)SvOOK_off(rv);
6286 if (SvPVX(rv) && SvLEN(rv))
6287 Safefree(SvPVX(rv));
6297 HV* stash = gv_stashpv(classname, TRUE);
6298 (void)sv_bless(rv, stash);
6304 =for apidoc sv_setref_pv
6306 Copies a pointer into a new SV, optionally blessing the SV. The C<rv>
6307 argument will be upgraded to an RV. That RV will be modified to point to
6308 the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed
6309 into the SV. The C<classname> argument indicates the package for the
6310 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
6311 will be returned and will have a reference count of 1.
6313 Do not use with other Perl types such as HV, AV, SV, CV, because those
6314 objects will become corrupted by the pointer copy process.
6316 Note that C<sv_setref_pvn> copies the string while this copies the pointer.
6322 Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv)
6325 sv_setsv(rv, &PL_sv_undef);
6329 sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
6334 =for apidoc sv_setref_iv
6336 Copies an integer into a new SV, optionally blessing the SV. The C<rv>
6337 argument will be upgraded to an RV. That RV will be modified to point to
6338 the new SV. The C<classname> argument indicates the package for the
6339 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
6340 will be returned and will have a reference count of 1.
6346 Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv)
6348 sv_setiv(newSVrv(rv,classname), iv);
6353 =for apidoc sv_setref_nv
6355 Copies a double into a new SV, optionally blessing the SV. The C<rv>
6356 argument will be upgraded to an RV. That RV will be modified to point to
6357 the new SV. The C<classname> argument indicates the package for the
6358 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
6359 will be returned and will have a reference count of 1.
6365 Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv)
6367 sv_setnv(newSVrv(rv,classname), nv);
6372 =for apidoc sv_setref_pvn
6374 Copies a string into a new SV, optionally blessing the SV. The length of the
6375 string must be specified with C<n>. The C<rv> argument will be upgraded to
6376 an RV. That RV will be modified to point to the new SV. The C<classname>
6377 argument indicates the package for the blessing. Set C<classname> to
6378 C<Nullch> to avoid the blessing. The new SV will be returned and will have
6379 a reference count of 1.
6381 Note that C<sv_setref_pv> copies the pointer while this copies the string.
6387 Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, char *pv, STRLEN n)
6389 sv_setpvn(newSVrv(rv,classname), pv, n);
6394 =for apidoc sv_bless
6396 Blesses an SV into a specified package. The SV must be an RV. The package
6397 must be designated by its stash (see C<gv_stashpv()>). The reference count
6398 of the SV is unaffected.
6404 Perl_sv_bless(pTHX_ SV *sv, HV *stash)
6408 Perl_croak(aTHX_ "Can't bless non-reference value");
6410 if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) {
6411 if (SvREADONLY(tmpRef))
6412 Perl_croak(aTHX_ PL_no_modify);
6413 if (SvOBJECT(tmpRef)) {
6414 if (SvTYPE(tmpRef) != SVt_PVIO)
6416 SvREFCNT_dec(SvSTASH(tmpRef));
6419 SvOBJECT_on(tmpRef);
6420 if (SvTYPE(tmpRef) != SVt_PVIO)
6422 (void)SvUPGRADE(tmpRef, SVt_PVMG);
6423 SvSTASH(tmpRef) = (HV*)SvREFCNT_inc(stash);
6434 S_sv_unglob(pTHX_ SV *sv)
6438 assert(SvTYPE(sv) == SVt_PVGV);
6443 SvREFCNT_dec(GvSTASH(sv));
6444 GvSTASH(sv) = Nullhv;
6446 sv_unmagic(sv, '*');
6447 Safefree(GvNAME(sv));
6450 /* need to keep SvANY(sv) in the right arena */
6451 xpvmg = new_XPVMG();
6452 StructCopy(SvANY(sv), xpvmg, XPVMG);
6453 del_XPVGV(SvANY(sv));
6456 SvFLAGS(sv) &= ~SVTYPEMASK;
6457 SvFLAGS(sv) |= SVt_PVMG;
6461 =for apidoc sv_unref_flags
6463 Unsets the RV status of the SV, and decrements the reference count of
6464 whatever was being referenced by the RV. This can almost be thought of
6465 as a reversal of C<newSVrv>. The C<cflags> argument can contain
6466 C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented
6467 (otherwise the decrementing is conditional on the reference count being
6468 different from one or the reference being a readonly SV).
6475 Perl_sv_unref_flags(pTHX_ SV *sv, U32 flags)
6479 if (SvWEAKREF(sv)) {
6487 if (SvREFCNT(rv) != 1 || SvREADONLY(rv) || flags) /* SV_IMMEDIATE_UNREF */
6489 else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */
6490 sv_2mortal(rv); /* Schedule for freeing later */
6494 =for apidoc sv_unref
6496 Unsets the RV status of the SV, and decrements the reference count of
6497 whatever was being referenced by the RV. This can almost be thought of
6498 as a reversal of C<newSVrv>. This is C<sv_unref_flags> with the C<flag>
6499 being zero. See C<SvROK_off>.
6505 Perl_sv_unref(pTHX_ SV *sv)
6507 sv_unref_flags(sv, 0);
6511 Perl_sv_taint(pTHX_ SV *sv)
6513 sv_magic((sv), Nullsv, 't', Nullch, 0);
6517 Perl_sv_untaint(pTHX_ SV *sv)
6519 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
6520 MAGIC *mg = mg_find(sv, 't');
6527 Perl_sv_tainted(pTHX_ SV *sv)
6529 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
6530 MAGIC *mg = mg_find(sv, 't');
6531 if (mg && ((mg->mg_len & 1) || ((mg->mg_len & 2) && mg->mg_obj == sv)))
6538 =for apidoc sv_setpviv
6540 Copies an integer into the given SV, also updating its string value.
6541 Does not handle 'set' magic. See C<sv_setpviv_mg>.
6547 Perl_sv_setpviv(pTHX_ SV *sv, IV iv)
6549 char buf[TYPE_CHARS(UV)];
6551 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
6553 sv_setpvn(sv, ptr, ebuf - ptr);
6558 =for apidoc sv_setpviv_mg
6560 Like C<sv_setpviv>, but also handles 'set' magic.
6566 Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv)
6568 char buf[TYPE_CHARS(UV)];
6570 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
6572 sv_setpvn(sv, ptr, ebuf - ptr);
6576 #if defined(PERL_IMPLICIT_CONTEXT)
6578 Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...)
6582 va_start(args, pat);
6583 sv_vsetpvf(sv, pat, &args);
6589 Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...)
6593 va_start(args, pat);
6594 sv_vsetpvf_mg(sv, pat, &args);
6600 =for apidoc sv_setpvf
6602 Processes its arguments like C<sprintf> and sets an SV to the formatted
6603 output. Does not handle 'set' magic. See C<sv_setpvf_mg>.
6609 Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...)
6612 va_start(args, pat);
6613 sv_vsetpvf(sv, pat, &args);
6618 Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args)
6620 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
6624 =for apidoc sv_setpvf_mg
6626 Like C<sv_setpvf>, but also handles 'set' magic.
6632 Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...)
6635 va_start(args, pat);
6636 sv_vsetpvf_mg(sv, pat, &args);
6641 Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
6643 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
6647 #if defined(PERL_IMPLICIT_CONTEXT)
6649 Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...)
6653 va_start(args, pat);
6654 sv_vcatpvf(sv, pat, &args);
6659 Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...)
6663 va_start(args, pat);
6664 sv_vcatpvf_mg(sv, pat, &args);
6670 =for apidoc sv_catpvf
6672 Processes its arguments like C<sprintf> and appends the formatted output
6673 to an SV. Handles 'get' magic, but not 'set' magic. C<SvSETMAGIC()> must
6674 typically be called after calling this function to handle 'set' magic.
6680 Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...)
6683 va_start(args, pat);
6684 sv_vcatpvf(sv, pat, &args);
6689 Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args)
6691 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
6695 =for apidoc sv_catpvf_mg
6697 Like C<sv_catpvf>, but also handles 'set' magic.
6703 Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...)
6706 va_start(args, pat);
6707 sv_vcatpvf_mg(sv, pat, &args);
6712 Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
6714 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
6719 =for apidoc sv_vsetpvfn
6721 Works like C<vcatpvfn> but copies the text into the SV instead of
6728 Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
6730 sv_setpvn(sv, "", 0);
6731 sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted);
6735 S_expect_number(pTHX_ char** pattern)
6738 switch (**pattern) {
6739 case '1': case '2': case '3':
6740 case '4': case '5': case '6':
6741 case '7': case '8': case '9':
6742 while (isDIGIT(**pattern))
6743 var = var * 10 + (*(*pattern)++ - '0');
6747 #define EXPECT_NUMBER(pattern, var) (var = S_expect_number(aTHX_ &pattern))
6750 =for apidoc sv_vcatpvfn
6752 Processes its arguments like C<vsprintf> and appends the formatted output
6753 to an SV. Uses an array of SVs if the C style variable argument list is
6754 missing (NULL). When running with taint checks enabled, indicates via
6755 C<maybe_tainted> if results are untrustworthy (often due to the use of
6762 Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
6769 static char nullstr[] = "(null)";
6772 /* no matter what, this is a string now */
6773 (void)SvPV_force(sv, origlen);
6775 /* special-case "", "%s", and "%_" */
6778 if (patlen == 2 && pat[0] == '%') {
6782 char *s = va_arg(*args, char*);
6783 sv_catpv(sv, s ? s : nullstr);
6785 else if (svix < svmax) {
6786 sv_catsv(sv, *svargs);
6787 if (DO_UTF8(*svargs))
6793 argsv = va_arg(*args, SV*);
6794 sv_catsv(sv, argsv);
6799 /* See comment on '_' below */
6804 patend = (char*)pat + patlen;
6805 for (p = (char*)pat; p < patend; p = q) {
6808 bool vectorize = FALSE;
6809 bool vectorarg = FALSE;
6810 bool vec_utf = FALSE;
6816 bool has_precis = FALSE;
6818 bool is_utf = FALSE;
6821 U8 utf8buf[UTF8_MAXLEN+1];
6822 STRLEN esignlen = 0;
6824 char *eptr = Nullch;
6826 /* Times 4: a decimal digit takes more than 3 binary digits.
6827 * NV_DIG: mantissa takes than many decimal digits.
6828 * Plus 32: Playing safe. */
6829 char ebuf[IV_DIG * 4 + NV_DIG + 32];
6830 /* large enough for "%#.#f" --chip */
6831 /* what about long double NVs? --jhi */
6834 U8 *vecstr = Null(U8*);
6846 STRLEN dotstrlen = 1;
6847 I32 efix = 0; /* explicit format parameter index */
6848 I32 ewix = 0; /* explicit width index */
6849 I32 epix = 0; /* explicit precision index */
6850 I32 evix = 0; /* explicit vector index */
6851 bool asterisk = FALSE;
6853 /* echo everything up to the next format specification */
6854 for (q = p; q < patend && *q != '%'; ++q) ;
6856 sv_catpvn(sv, p, q - p);
6863 We allow format specification elements in this order:
6864 \d+\$ explicit format parameter index
6866 \*?(\d+\$)?v vector with optional (optionally specified) arg
6867 \d+|\*(\d+\$)? width using optional (optionally specified) arg
6868 \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg
6870 [%bcdefginopsux_DFOUX] format (mandatory)
6872 if (EXPECT_NUMBER(q, width)) {
6913 if (EXPECT_NUMBER(q, ewix))
6922 if (vectorarg = asterisk) {
6932 EXPECT_NUMBER(q, width);
6937 vecsv = va_arg(*args, SV*);
6939 vecsv = (evix ? evix <= svmax : svix < svmax) ?
6940 svargs[ewix ? ewix-1 : svix++] : &PL_sv_undef;
6941 dotstr = SvPVx(vecsv, dotstrlen);
6946 vecsv = va_arg(*args, SV*);
6947 vecstr = (U8*)SvPVx(vecsv,veclen);
6948 vec_utf = DO_UTF8(vecsv);
6950 else if (efix ? efix <= svmax : svix < svmax) {
6951 vecsv = svargs[efix ? efix-1 : svix++];
6952 vecstr = (U8*)SvPVx(vecsv,veclen);
6953 vec_utf = DO_UTF8(vecsv);
6963 i = va_arg(*args, int);
6965 i = (ewix ? ewix <= svmax : svix < svmax) ?
6966 SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
6968 width = (i < 0) ? -i : i;
6978 if (EXPECT_NUMBER(q, epix) && *q++ != '$')
6981 i = va_arg(*args, int);
6983 i = (ewix ? ewix <= svmax : svix < svmax)
6984 ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
6985 precis = (i < 0) ? 0 : i;
6990 precis = precis * 10 + (*q++ - '0');
6998 #if defined(HAS_QUAD) || (defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE))
7009 #if defined(HAS_QUAD) || (defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE))
7010 if (*(q + 1) == 'l') { /* lld, llf */
7033 argsv = (efix ? efix <= svmax : svix < svmax) ?
7034 svargs[efix ? efix-1 : svix++] : &PL_sv_undef;
7041 uv = args ? va_arg(*args, int) : SvIVx(argsv);
7042 if ((uv > 255 || (uv > 127 && SvUTF8(sv))) && !IN_BYTE) {
7043 eptr = (char*)utf8buf;
7044 elen = uv_to_utf8((U8*)eptr, uv) - utf8buf;
7056 eptr = va_arg(*args, char*);
7058 #ifdef MACOS_TRADITIONAL
7059 /* On MacOS, %#s format is used for Pascal strings */
7064 elen = strlen(eptr);
7067 elen = sizeof nullstr - 1;
7071 eptr = SvPVx(argsv, elen);
7072 if (DO_UTF8(argsv)) {
7073 if (has_precis && precis < elen) {
7075 sv_pos_u2b(argsv, &p, 0); /* sticks at end */
7078 if (width) { /* fudge width (can't fudge elen) */
7079 width += elen - sv_len_utf8(argsv);
7088 * The "%_" hack might have to be changed someday,
7089 * if ISO or ANSI decide to use '_' for something.
7090 * So we keep it hidden from users' code.
7094 argsv = va_arg(*args, SV*);
7095 eptr = SvPVx(argsv, elen);
7101 if (has_precis && elen > precis)
7110 uv = PTR2UV(args ? va_arg(*args, void*) : argsv);
7128 iv = (IV)utf8_to_uv(vecstr, veclen, &ulen, 0);
7138 case 'h': iv = (short)va_arg(*args, int); break;
7139 default: iv = va_arg(*args, int); break;
7140 case 'l': iv = va_arg(*args, long); break;
7141 case 'V': iv = va_arg(*args, IV); break;
7143 case 'q': iv = va_arg(*args, Quad_t); break;
7150 case 'h': iv = (short)iv; break;
7152 case 'l': iv = (long)iv; break;
7155 case 'q': iv = (Quad_t)iv; break;
7162 esignbuf[esignlen++] = plus;
7166 esignbuf[esignlen++] = '-';
7208 uv = utf8_to_uv(vecstr, veclen, &ulen, 0);
7218 case 'h': uv = (unsigned short)va_arg(*args, unsigned); break;
7219 default: uv = va_arg(*args, unsigned); break;
7220 case 'l': uv = va_arg(*args, unsigned long); break;
7221 case 'V': uv = va_arg(*args, UV); break;
7223 case 'q': uv = va_arg(*args, Quad_t); break;
7230 case 'h': uv = (unsigned short)uv; break;
7232 case 'l': uv = (unsigned long)uv; break;
7235 case 'q': uv = (Quad_t)uv; break;
7241 eptr = ebuf + sizeof ebuf;
7247 p = (char*)((c == 'X')
7248 ? "0123456789ABCDEF" : "0123456789abcdef");
7254 esignbuf[esignlen++] = '0';
7255 esignbuf[esignlen++] = c; /* 'x' or 'X' */
7261 *--eptr = '0' + dig;
7263 if (alt && *eptr != '0')
7269 *--eptr = '0' + dig;
7272 esignbuf[esignlen++] = '0';
7273 esignbuf[esignlen++] = 'b';
7276 default: /* it had better be ten or less */
7277 #if defined(PERL_Y2KWARN)
7278 if (ckWARN(WARN_Y2K)) {
7280 char *s = SvPV(sv,n);
7281 if (n >= 2 && s[n-2] == '1' && s[n-1] == '9'
7282 && (n == 2 || !isDIGIT(s[n-3])))
7284 Perl_warner(aTHX_ WARN_Y2K,
7285 "Possible Y2K bug: %%%c %s",
7286 c, "format string following '19'");
7292 *--eptr = '0' + dig;
7293 } while (uv /= base);
7296 elen = (ebuf + sizeof ebuf) - eptr;
7299 zeros = precis - elen;
7300 else if (precis == 0 && elen == 1 && *eptr == '0')
7305 /* FLOATING POINT */
7308 c = 'f'; /* maybe %F isn't supported here */
7314 /* This is evil, but floating point is even more evil */
7317 nv = args ? va_arg(*args, NV) : SvNVx(argsv);
7320 if (c != 'e' && c != 'E') {
7322 (void)Perl_frexp(nv, &i);
7323 if (i == PERL_INT_MIN)
7324 Perl_die(aTHX_ "panic: frexp");
7326 need = BIT_DIGITS(i);
7328 need += has_precis ? precis : 6; /* known default */
7332 need += 20; /* fudge factor */
7333 if (PL_efloatsize < need) {
7334 Safefree(PL_efloatbuf);
7335 PL_efloatsize = need + 20; /* more fudge */
7336 New(906, PL_efloatbuf, PL_efloatsize, char);
7337 PL_efloatbuf[0] = '\0';
7340 eptr = ebuf + sizeof ebuf;
7343 #if defined(USE_LONG_DOUBLE) && defined(PERL_PRIfldbl)
7345 /* Copy the one or more characters in a long double
7346 * format before the 'base' ([efgEFG]) character to
7347 * the format string. */
7348 static char const prifldbl[] = PERL_PRIfldbl;
7349 char const *p = prifldbl + sizeof(prifldbl) - 3;
7350 while (p >= prifldbl) { *--eptr = *p--; }
7355 do { *--eptr = '0' + (base % 10); } while (base /= 10);
7360 do { *--eptr = '0' + (base % 10); } while (base /= 10);
7372 /* No taint. Otherwise we are in the strange situation
7373 * where printf() taints but print($float) doesn't.
7375 (void)sprintf(PL_efloatbuf, eptr, nv);
7377 eptr = PL_efloatbuf;
7378 elen = strlen(PL_efloatbuf);
7385 i = SvCUR(sv) - origlen;
7388 case 'h': *(va_arg(*args, short*)) = i; break;
7389 default: *(va_arg(*args, int*)) = i; break;
7390 case 'l': *(va_arg(*args, long*)) = i; break;
7391 case 'V': *(va_arg(*args, IV*)) = i; break;
7393 case 'q': *(va_arg(*args, Quad_t*)) = i; break;
7398 sv_setuv_mg(argsv, (UV)i);
7399 continue; /* not "break" */
7406 if (!args && ckWARN(WARN_PRINTF) &&
7407 (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) {
7408 SV *msg = sv_newmortal();
7409 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %s: ",
7410 (PL_op->op_type == OP_PRTF) ? "printf" : "sprintf");
7413 Perl_sv_catpvf(aTHX_ msg,
7414 "\"%%%c\"", c & 0xFF);
7416 Perl_sv_catpvf(aTHX_ msg,
7417 "\"%%\\%03"UVof"\"",
7420 sv_catpv(msg, "end of string");
7421 Perl_warner(aTHX_ WARN_PRINTF, "%"SVf, msg); /* yes, this is reentrant */
7424 /* output mangled stuff ... */
7430 /* ... right here, because formatting flags should not apply */
7431 SvGROW(sv, SvCUR(sv) + elen + 1);
7433 Copy(eptr, p, elen, char);
7436 SvCUR(sv) = p - SvPVX(sv);
7437 continue; /* not "break" */
7440 have = esignlen + zeros + elen;
7441 need = (have > width ? have : width);
7444 SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1);
7446 if (esignlen && fill == '0') {
7447 for (i = 0; i < esignlen; i++)
7451 memset(p, fill, gap);
7454 if (esignlen && fill != '0') {
7455 for (i = 0; i < esignlen; i++)
7459 for (i = zeros; i; i--)
7463 Copy(eptr, p, elen, char);
7467 memset(p, ' ', gap);
7472 Copy(dotstr, p, dotstrlen, char);
7476 vectorize = FALSE; /* done iterating over vecstr */
7481 SvCUR(sv) = p - SvPVX(sv);
7489 #if defined(USE_ITHREADS)
7491 #if defined(USE_THREADS)
7492 # include "error: USE_THREADS and USE_ITHREADS are incompatible"
7495 #ifndef GpREFCNT_inc
7496 # define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL)
7500 #define sv_dup_inc(s) SvREFCNT_inc(sv_dup(s))
7501 #define av_dup(s) (AV*)sv_dup((SV*)s)
7502 #define av_dup_inc(s) (AV*)SvREFCNT_inc(sv_dup((SV*)s))
7503 #define hv_dup(s) (HV*)sv_dup((SV*)s)
7504 #define hv_dup_inc(s) (HV*)SvREFCNT_inc(sv_dup((SV*)s))
7505 #define cv_dup(s) (CV*)sv_dup((SV*)s)
7506 #define cv_dup_inc(s) (CV*)SvREFCNT_inc(sv_dup((SV*)s))
7507 #define io_dup(s) (IO*)sv_dup((SV*)s)
7508 #define io_dup_inc(s) (IO*)SvREFCNT_inc(sv_dup((SV*)s))
7509 #define gv_dup(s) (GV*)sv_dup((SV*)s)
7510 #define gv_dup_inc(s) (GV*)SvREFCNT_inc(sv_dup((SV*)s))
7511 #define SAVEPV(p) (p ? savepv(p) : Nullch)
7512 #define SAVEPVN(p,n) (p ? savepvn(p,n) : Nullch)
7515 Perl_re_dup(pTHX_ REGEXP *r)
7517 /* XXX fix when pmop->op_pmregexp becomes shared */
7518 return ReREFCNT_inc(r);
7522 Perl_fp_dup(pTHX_ PerlIO *fp, char type)
7526 return (PerlIO*)NULL;
7528 /* look for it in the table first */
7529 ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp);
7533 /* create anew and remember what it is */
7534 ret = PerlIO_fdupopen(aTHX_ fp);
7535 ptr_table_store(PL_ptr_table, fp, ret);
7540 Perl_dirp_dup(pTHX_ DIR *dp)
7549 Perl_gp_dup(pTHX_ GP *gp)
7554 /* look for it in the table first */
7555 ret = (GP*)ptr_table_fetch(PL_ptr_table, gp);
7559 /* create anew and remember what it is */
7560 Newz(0, ret, 1, GP);
7561 ptr_table_store(PL_ptr_table, gp, ret);
7564 ret->gp_refcnt = 0; /* must be before any other dups! */
7565 ret->gp_sv = sv_dup_inc(gp->gp_sv);
7566 ret->gp_io = io_dup_inc(gp->gp_io);
7567 ret->gp_form = cv_dup_inc(gp->gp_form);
7568 ret->gp_av = av_dup_inc(gp->gp_av);
7569 ret->gp_hv = hv_dup_inc(gp->gp_hv);
7570 ret->gp_egv = gv_dup(gp->gp_egv); /* GvEGV is not refcounted */
7571 ret->gp_cv = cv_dup_inc(gp->gp_cv);
7572 ret->gp_cvgen = gp->gp_cvgen;
7573 ret->gp_flags = gp->gp_flags;
7574 ret->gp_line = gp->gp_line;
7575 ret->gp_file = gp->gp_file; /* points to COP.cop_file */
7580 Perl_mg_dup(pTHX_ MAGIC *mg)
7582 MAGIC *mgret = (MAGIC*)NULL;
7585 return (MAGIC*)NULL;
7586 /* look for it in the table first */
7587 mgret = (MAGIC*)ptr_table_fetch(PL_ptr_table, mg);
7591 for (; mg; mg = mg->mg_moremagic) {
7593 Newz(0, nmg, 1, MAGIC);
7597 mgprev->mg_moremagic = nmg;
7598 nmg->mg_virtual = mg->mg_virtual; /* XXX copy dynamic vtable? */
7599 nmg->mg_private = mg->mg_private;
7600 nmg->mg_type = mg->mg_type;
7601 nmg->mg_flags = mg->mg_flags;
7602 if (mg->mg_type == 'r') {
7603 nmg->mg_obj = (SV*)re_dup((REGEXP*)mg->mg_obj);
7606 nmg->mg_obj = (mg->mg_flags & MGf_REFCOUNTED)
7607 ? sv_dup_inc(mg->mg_obj)
7608 : sv_dup(mg->mg_obj);
7610 nmg->mg_len = mg->mg_len;
7611 nmg->mg_ptr = mg->mg_ptr; /* XXX random ptr? */
7612 if (mg->mg_ptr && mg->mg_type != 'g') {
7613 if (mg->mg_len >= 0) {
7614 nmg->mg_ptr = SAVEPVN(mg->mg_ptr, mg->mg_len);
7615 if (mg->mg_type == 'c' && AMT_AMAGIC((AMT*)mg->mg_ptr)) {
7616 AMT *amtp = (AMT*)mg->mg_ptr;
7617 AMT *namtp = (AMT*)nmg->mg_ptr;
7619 for (i = 1; i < NofAMmeth; i++) {
7620 namtp->table[i] = cv_dup_inc(amtp->table[i]);
7624 else if (mg->mg_len == HEf_SVKEY)
7625 nmg->mg_ptr = (char*)sv_dup_inc((SV*)mg->mg_ptr);
7633 Perl_ptr_table_new(pTHX)
7636 Newz(0, tbl, 1, PTR_TBL_t);
7639 Newz(0, tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
7644 Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, void *sv)
7646 PTR_TBL_ENT_t *tblent;
7647 UV hash = PTR2UV(sv);
7649 tblent = tbl->tbl_ary[hash & tbl->tbl_max];
7650 for (; tblent; tblent = tblent->next) {
7651 if (tblent->oldval == sv)
7652 return tblent->newval;
7658 Perl_ptr_table_store(pTHX_ PTR_TBL_t *tbl, void *oldv, void *newv)
7660 PTR_TBL_ENT_t *tblent, **otblent;
7661 /* XXX this may be pessimal on platforms where pointers aren't good
7662 * hash values e.g. if they grow faster in the most significant
7664 UV hash = PTR2UV(oldv);
7668 otblent = &tbl->tbl_ary[hash & tbl->tbl_max];
7669 for (tblent = *otblent; tblent; i=0, tblent = tblent->next) {
7670 if (tblent->oldval == oldv) {
7671 tblent->newval = newv;
7676 Newz(0, tblent, 1, PTR_TBL_ENT_t);
7677 tblent->oldval = oldv;
7678 tblent->newval = newv;
7679 tblent->next = *otblent;
7682 if (i && tbl->tbl_items > tbl->tbl_max)
7683 ptr_table_split(tbl);
7687 Perl_ptr_table_split(pTHX_ PTR_TBL_t *tbl)
7689 PTR_TBL_ENT_t **ary = tbl->tbl_ary;
7690 UV oldsize = tbl->tbl_max + 1;
7691 UV newsize = oldsize * 2;
7694 Renew(ary, newsize, PTR_TBL_ENT_t*);
7695 Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
7696 tbl->tbl_max = --newsize;
7698 for (i=0; i < oldsize; i++, ary++) {
7699 PTR_TBL_ENT_t **curentp, **entp, *ent;
7702 curentp = ary + oldsize;
7703 for (entp = ary, ent = *ary; ent; ent = *entp) {
7704 if ((newsize & PTR2UV(ent->oldval)) != i) {
7706 ent->next = *curentp;
7717 Perl_ptr_table_clear(pTHX_ PTR_TBL_t *tbl)
7719 register PTR_TBL_ENT_t **array;
7720 register PTR_TBL_ENT_t *entry;
7721 register PTR_TBL_ENT_t *oentry = Null(PTR_TBL_ENT_t*);
7725 if (!tbl || !tbl->tbl_items) {
7729 array = tbl->tbl_ary;
7736 entry = entry->next;
7740 if (++riter > max) {
7743 entry = array[riter];
7751 Perl_ptr_table_free(pTHX_ PTR_TBL_t *tbl)
7756 ptr_table_clear(tbl);
7757 Safefree(tbl->tbl_ary);
7766 S_gv_share(pTHX_ SV *sstr)
7769 SV *sv = &PL_sv_no; /* just need SvREADONLY-ness */
7771 if (GvIO(gv) || GvFORM(gv)) {
7772 GvSHARED_off(gv); /* GvIOs cannot be shared. nor can GvFORMs */
7774 else if (!GvCV(gv)) {
7778 /* CvPADLISTs cannot be shared */
7779 if (!CvXSUB(GvCV(gv))) {
7784 if (!GvSHARED(gv)) {
7786 PerlIO_printf(Perl_debug_log, "gv_share: unable to share %s::%s\n",
7787 HvNAME(GvSTASH(gv)), GvNAME(gv));
7793 * write attempts will die with
7794 * "Modification of a read-only value attempted"
7800 SvREADONLY_on(GvSV(gv));
7807 SvREADONLY_on(GvAV(gv));
7814 SvREADONLY_on(GvAV(gv));
7817 return sstr; /* he_dup() will SvREFCNT_inc() */
7821 Perl_sv_dup(pTHX_ SV *sstr)
7825 if (!sstr || SvTYPE(sstr) == SVTYPEMASK)
7827 /* look for it in the table first */
7828 dstr = (SV*)ptr_table_fetch(PL_ptr_table, sstr);
7832 /* create anew and remember what it is */
7834 ptr_table_store(PL_ptr_table, sstr, dstr);
7837 SvFLAGS(dstr) = SvFLAGS(sstr);
7838 SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */
7839 SvREFCNT(dstr) = 0; /* must be before any other dups! */
7842 if (SvANY(sstr) && PL_watch_pvx && SvPVX(sstr) == PL_watch_pvx)
7843 PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n",
7844 PL_watch_pvx, SvPVX(sstr));
7847 switch (SvTYPE(sstr)) {
7852 SvANY(dstr) = new_XIV();
7853 SvIVX(dstr) = SvIVX(sstr);
7856 SvANY(dstr) = new_XNV();
7857 SvNVX(dstr) = SvNVX(sstr);
7860 SvANY(dstr) = new_XRV();
7861 SvRV(dstr) = sv_dup_inc(SvRV(sstr));
7864 SvANY(dstr) = new_XPV();
7865 SvCUR(dstr) = SvCUR(sstr);
7866 SvLEN(dstr) = SvLEN(sstr);
7868 SvRV(dstr) = sv_dup_inc(SvRV(sstr));
7869 else if (SvPVX(sstr) && SvLEN(sstr))
7870 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
7872 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
7875 SvANY(dstr) = new_XPVIV();
7876 SvCUR(dstr) = SvCUR(sstr);
7877 SvLEN(dstr) = SvLEN(sstr);
7878 SvIVX(dstr) = SvIVX(sstr);
7880 SvRV(dstr) = sv_dup_inc(SvRV(sstr));
7881 else if (SvPVX(sstr) && SvLEN(sstr))
7882 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
7884 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
7887 SvANY(dstr) = new_XPVNV();
7888 SvCUR(dstr) = SvCUR(sstr);
7889 SvLEN(dstr) = SvLEN(sstr);
7890 SvIVX(dstr) = SvIVX(sstr);
7891 SvNVX(dstr) = SvNVX(sstr);
7893 SvRV(dstr) = sv_dup_inc(SvRV(sstr));
7894 else if (SvPVX(sstr) && SvLEN(sstr))
7895 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
7897 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
7900 SvANY(dstr) = new_XPVMG();
7901 SvCUR(dstr) = SvCUR(sstr);
7902 SvLEN(dstr) = SvLEN(sstr);
7903 SvIVX(dstr) = SvIVX(sstr);
7904 SvNVX(dstr) = SvNVX(sstr);
7905 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
7906 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
7908 SvRV(dstr) = sv_dup_inc(SvRV(sstr));
7909 else if (SvPVX(sstr) && SvLEN(sstr))
7910 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
7912 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
7915 SvANY(dstr) = new_XPVBM();
7916 SvCUR(dstr) = SvCUR(sstr);
7917 SvLEN(dstr) = SvLEN(sstr);
7918 SvIVX(dstr) = SvIVX(sstr);
7919 SvNVX(dstr) = SvNVX(sstr);
7920 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
7921 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
7923 SvRV(dstr) = sv_dup_inc(SvRV(sstr));
7924 else if (SvPVX(sstr) && SvLEN(sstr))
7925 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
7927 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
7928 BmRARE(dstr) = BmRARE(sstr);
7929 BmUSEFUL(dstr) = BmUSEFUL(sstr);
7930 BmPREVIOUS(dstr)= BmPREVIOUS(sstr);
7933 SvANY(dstr) = new_XPVLV();
7934 SvCUR(dstr) = SvCUR(sstr);
7935 SvLEN(dstr) = SvLEN(sstr);
7936 SvIVX(dstr) = SvIVX(sstr);
7937 SvNVX(dstr) = SvNVX(sstr);
7938 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
7939 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
7941 SvRV(dstr) = sv_dup_inc(SvRV(sstr));
7942 else if (SvPVX(sstr) && SvLEN(sstr))
7943 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
7945 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
7946 LvTARGOFF(dstr) = LvTARGOFF(sstr); /* XXX sometimes holds PMOP* when DEBUGGING */
7947 LvTARGLEN(dstr) = LvTARGLEN(sstr);
7948 LvTARG(dstr) = sv_dup_inc(LvTARG(sstr));
7949 LvTYPE(dstr) = LvTYPE(sstr);
7952 if (GvSHARED((GV*)sstr)) {
7954 if ((share = gv_share(sstr))) {
7958 PerlIO_printf(Perl_debug_log, "sv_dup: sharing %s::%s\n",
7959 HvNAME(GvSTASH(share)), GvNAME(share));
7964 SvANY(dstr) = new_XPVGV();
7965 SvCUR(dstr) = SvCUR(sstr);
7966 SvLEN(dstr) = SvLEN(sstr);
7967 SvIVX(dstr) = SvIVX(sstr);
7968 SvNVX(dstr) = SvNVX(sstr);
7969 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
7970 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
7972 SvRV(dstr) = sv_dup_inc(SvRV(sstr));
7973 else if (SvPVX(sstr) && SvLEN(sstr))
7974 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
7976 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
7977 GvNAMELEN(dstr) = GvNAMELEN(sstr);
7978 GvNAME(dstr) = SAVEPVN(GvNAME(sstr), GvNAMELEN(sstr));
7979 GvSTASH(dstr) = hv_dup_inc(GvSTASH(sstr));
7980 GvFLAGS(dstr) = GvFLAGS(sstr);
7981 GvGP(dstr) = gp_dup(GvGP(sstr));
7982 (void)GpREFCNT_inc(GvGP(dstr));
7985 SvANY(dstr) = new_XPVIO();
7986 SvCUR(dstr) = SvCUR(sstr);
7987 SvLEN(dstr) = SvLEN(sstr);
7988 SvIVX(dstr) = SvIVX(sstr);
7989 SvNVX(dstr) = SvNVX(sstr);
7990 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
7991 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
7993 SvRV(dstr) = sv_dup_inc(SvRV(sstr));
7994 else if (SvPVX(sstr) && SvLEN(sstr))
7995 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
7997 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
7998 IoIFP(dstr) = fp_dup(IoIFP(sstr), IoTYPE(sstr));
7999 if (IoOFP(sstr) == IoIFP(sstr))
8000 IoOFP(dstr) = IoIFP(dstr);
8002 IoOFP(dstr) = fp_dup(IoOFP(sstr), IoTYPE(sstr));
8003 /* PL_rsfp_filters entries have fake IoDIRP() */
8004 if (IoDIRP(sstr) && !(IoFLAGS(sstr) & IOf_FAKE_DIRP))
8005 IoDIRP(dstr) = dirp_dup(IoDIRP(sstr));
8007 IoDIRP(dstr) = IoDIRP(sstr);
8008 IoLINES(dstr) = IoLINES(sstr);
8009 IoPAGE(dstr) = IoPAGE(sstr);
8010 IoPAGE_LEN(dstr) = IoPAGE_LEN(sstr);
8011 IoLINES_LEFT(dstr) = IoLINES_LEFT(sstr);
8012 IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(sstr));
8013 IoTOP_GV(dstr) = gv_dup(IoTOP_GV(sstr));
8014 IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(sstr));
8015 IoFMT_GV(dstr) = gv_dup(IoFMT_GV(sstr));
8016 IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(sstr));
8017 IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(sstr));
8018 IoSUBPROCESS(dstr) = IoSUBPROCESS(sstr);
8019 IoTYPE(dstr) = IoTYPE(sstr);
8020 IoFLAGS(dstr) = IoFLAGS(sstr);
8023 SvANY(dstr) = new_XPVAV();
8024 SvCUR(dstr) = SvCUR(sstr);
8025 SvLEN(dstr) = SvLEN(sstr);
8026 SvIVX(dstr) = SvIVX(sstr);
8027 SvNVX(dstr) = SvNVX(sstr);
8028 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
8029 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
8030 AvARYLEN((AV*)dstr) = sv_dup_inc(AvARYLEN((AV*)sstr));
8031 AvFLAGS((AV*)dstr) = AvFLAGS((AV*)sstr);
8032 if (AvARRAY((AV*)sstr)) {
8033 SV **dst_ary, **src_ary;
8034 SSize_t items = AvFILLp((AV*)sstr) + 1;
8036 src_ary = AvARRAY((AV*)sstr);
8037 Newz(0, dst_ary, AvMAX((AV*)sstr)+1, SV*);
8038 ptr_table_store(PL_ptr_table, src_ary, dst_ary);
8039 SvPVX(dstr) = (char*)dst_ary;
8040 AvALLOC((AV*)dstr) = dst_ary;
8041 if (AvREAL((AV*)sstr)) {
8043 *dst_ary++ = sv_dup_inc(*src_ary++);
8047 *dst_ary++ = sv_dup(*src_ary++);
8049 items = AvMAX((AV*)sstr) - AvFILLp((AV*)sstr);
8050 while (items-- > 0) {
8051 *dst_ary++ = &PL_sv_undef;
8055 SvPVX(dstr) = Nullch;
8056 AvALLOC((AV*)dstr) = (SV**)NULL;
8060 SvANY(dstr) = new_XPVHV();
8061 SvCUR(dstr) = SvCUR(sstr);
8062 SvLEN(dstr) = SvLEN(sstr);
8063 SvIVX(dstr) = SvIVX(sstr);
8064 SvNVX(dstr) = SvNVX(sstr);
8065 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
8066 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
8067 HvRITER((HV*)dstr) = HvRITER((HV*)sstr);
8068 if (HvARRAY((HV*)sstr)) {
8070 XPVHV *dxhv = (XPVHV*)SvANY(dstr);
8071 XPVHV *sxhv = (XPVHV*)SvANY(sstr);
8072 Newz(0, dxhv->xhv_array,
8073 PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1), char);
8074 while (i <= sxhv->xhv_max) {
8075 ((HE**)dxhv->xhv_array)[i] = he_dup(((HE**)sxhv->xhv_array)[i],
8076 !!HvSHAREKEYS(sstr));
8079 dxhv->xhv_eiter = he_dup(sxhv->xhv_eiter, !!HvSHAREKEYS(sstr));
8082 SvPVX(dstr) = Nullch;
8083 HvEITER((HV*)dstr) = (HE*)NULL;
8085 HvPMROOT((HV*)dstr) = HvPMROOT((HV*)sstr); /* XXX */
8086 HvNAME((HV*)dstr) = SAVEPV(HvNAME((HV*)sstr));
8089 SvANY(dstr) = new_XPVFM();
8090 FmLINES(dstr) = FmLINES(sstr);
8094 SvANY(dstr) = new_XPVCV();
8096 SvCUR(dstr) = SvCUR(sstr);
8097 SvLEN(dstr) = SvLEN(sstr);
8098 SvIVX(dstr) = SvIVX(sstr);
8099 SvNVX(dstr) = SvNVX(sstr);
8100 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
8101 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
8102 if (SvPVX(sstr) && SvLEN(sstr))
8103 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
8105 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
8106 CvSTASH(dstr) = hv_dup(CvSTASH(sstr));/* NOTE: not refcounted */
8107 CvSTART(dstr) = CvSTART(sstr);
8108 CvROOT(dstr) = OpREFCNT_inc(CvROOT(sstr));
8109 CvXSUB(dstr) = CvXSUB(sstr);
8110 CvXSUBANY(dstr) = CvXSUBANY(sstr);
8111 CvGV(dstr) = gv_dup_inc(CvGV(sstr));
8112 CvDEPTH(dstr) = CvDEPTH(sstr);
8113 if (CvPADLIST(sstr) && !AvREAL(CvPADLIST(sstr))) {
8114 /* XXX padlists are real, but pretend to be not */
8115 AvREAL_on(CvPADLIST(sstr));
8116 CvPADLIST(dstr) = av_dup_inc(CvPADLIST(sstr));
8117 AvREAL_off(CvPADLIST(sstr));
8118 AvREAL_off(CvPADLIST(dstr));
8121 CvPADLIST(dstr) = av_dup_inc(CvPADLIST(sstr));
8122 CvOUTSIDE(dstr) = cv_dup_inc(CvOUTSIDE(sstr));
8123 CvFLAGS(dstr) = CvFLAGS(sstr);
8126 Perl_croak(aTHX_ "Bizarre SvTYPE [%d]", SvTYPE(sstr));
8130 if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO)
8137 Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max)
8142 return (PERL_CONTEXT*)NULL;
8144 /* look for it in the table first */
8145 ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs);
8149 /* create anew and remember what it is */
8150 Newz(56, ncxs, max + 1, PERL_CONTEXT);
8151 ptr_table_store(PL_ptr_table, cxs, ncxs);
8154 PERL_CONTEXT *cx = &cxs[ix];
8155 PERL_CONTEXT *ncx = &ncxs[ix];
8156 ncx->cx_type = cx->cx_type;
8157 if (CxTYPE(cx) == CXt_SUBST) {
8158 Perl_croak(aTHX_ "Cloning substitution context is unimplemented");
8161 ncx->blk_oldsp = cx->blk_oldsp;
8162 ncx->blk_oldcop = cx->blk_oldcop;
8163 ncx->blk_oldretsp = cx->blk_oldretsp;
8164 ncx->blk_oldmarksp = cx->blk_oldmarksp;
8165 ncx->blk_oldscopesp = cx->blk_oldscopesp;
8166 ncx->blk_oldpm = cx->blk_oldpm;
8167 ncx->blk_gimme = cx->blk_gimme;
8168 switch (CxTYPE(cx)) {
8170 ncx->blk_sub.cv = (cx->blk_sub.olddepth == 0
8171 ? cv_dup_inc(cx->blk_sub.cv)
8172 : cv_dup(cx->blk_sub.cv));
8173 ncx->blk_sub.argarray = (cx->blk_sub.hasargs
8174 ? av_dup_inc(cx->blk_sub.argarray)
8176 ncx->blk_sub.savearray = av_dup(cx->blk_sub.savearray);
8177 ncx->blk_sub.olddepth = cx->blk_sub.olddepth;
8178 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
8179 ncx->blk_sub.lval = cx->blk_sub.lval;
8182 ncx->blk_eval.old_in_eval = cx->blk_eval.old_in_eval;
8183 ncx->blk_eval.old_op_type = cx->blk_eval.old_op_type;
8184 ncx->blk_eval.old_namesv = sv_dup_inc(cx->blk_eval.old_namesv);
8185 ncx->blk_eval.old_eval_root = cx->blk_eval.old_eval_root;
8186 ncx->blk_eval.cur_text = sv_dup(cx->blk_eval.cur_text);
8189 ncx->blk_loop.label = cx->blk_loop.label;
8190 ncx->blk_loop.resetsp = cx->blk_loop.resetsp;
8191 ncx->blk_loop.redo_op = cx->blk_loop.redo_op;
8192 ncx->blk_loop.next_op = cx->blk_loop.next_op;
8193 ncx->blk_loop.last_op = cx->blk_loop.last_op;
8194 ncx->blk_loop.iterdata = (CxPADLOOP(cx)
8195 ? cx->blk_loop.iterdata
8196 : gv_dup((GV*)cx->blk_loop.iterdata));
8197 ncx->blk_loop.oldcurpad
8198 = (SV**)ptr_table_fetch(PL_ptr_table,
8199 cx->blk_loop.oldcurpad);
8200 ncx->blk_loop.itersave = sv_dup_inc(cx->blk_loop.itersave);
8201 ncx->blk_loop.iterlval = sv_dup_inc(cx->blk_loop.iterlval);
8202 ncx->blk_loop.iterary = av_dup_inc(cx->blk_loop.iterary);
8203 ncx->blk_loop.iterix = cx->blk_loop.iterix;
8204 ncx->blk_loop.itermax = cx->blk_loop.itermax;
8207 ncx->blk_sub.cv = cv_dup(cx->blk_sub.cv);
8208 ncx->blk_sub.gv = gv_dup(cx->blk_sub.gv);
8209 ncx->blk_sub.dfoutgv = gv_dup_inc(cx->blk_sub.dfoutgv);
8210 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
8223 Perl_si_dup(pTHX_ PERL_SI *si)
8228 return (PERL_SI*)NULL;
8230 /* look for it in the table first */
8231 nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si);
8235 /* create anew and remember what it is */
8236 Newz(56, nsi, 1, PERL_SI);
8237 ptr_table_store(PL_ptr_table, si, nsi);
8239 nsi->si_stack = av_dup_inc(si->si_stack);
8240 nsi->si_cxix = si->si_cxix;
8241 nsi->si_cxmax = si->si_cxmax;
8242 nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax);
8243 nsi->si_type = si->si_type;
8244 nsi->si_prev = si_dup(si->si_prev);
8245 nsi->si_next = si_dup(si->si_next);
8246 nsi->si_markoff = si->si_markoff;
8251 #define POPINT(ss,ix) ((ss)[--(ix)].any_i32)
8252 #define TOPINT(ss,ix) ((ss)[ix].any_i32)
8253 #define POPLONG(ss,ix) ((ss)[--(ix)].any_long)
8254 #define TOPLONG(ss,ix) ((ss)[ix].any_long)
8255 #define POPIV(ss,ix) ((ss)[--(ix)].any_iv)
8256 #define TOPIV(ss,ix) ((ss)[ix].any_iv)
8257 #define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr)
8258 #define TOPPTR(ss,ix) ((ss)[ix].any_ptr)
8259 #define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr)
8260 #define TOPDPTR(ss,ix) ((ss)[ix].any_dptr)
8261 #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr)
8262 #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr)
8265 #define pv_dup_inc(p) SAVEPV(p)
8266 #define pv_dup(p) SAVEPV(p)
8267 #define svp_dup_inc(p,pp) any_dup(p,pp)
8270 Perl_any_dup(pTHX_ void *v, PerlInterpreter *proto_perl)
8277 /* look for it in the table first */
8278 ret = ptr_table_fetch(PL_ptr_table, v);
8282 /* see if it is part of the interpreter structure */
8283 if (v >= (void*)proto_perl && v < (void*)(proto_perl+1))
8284 ret = (void*)(((char*)aTHXo) + (((char*)v) - (char*)proto_perl));
8292 Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl)
8294 ANY *ss = proto_perl->Tsavestack;
8295 I32 ix = proto_perl->Tsavestack_ix;
8296 I32 max = proto_perl->Tsavestack_max;
8309 void (*dptr) (void*);
8310 void (*dxptr) (pTHXo_ void*);
8313 Newz(54, nss, max, ANY);
8319 case SAVEt_ITEM: /* normal string */
8320 sv = (SV*)POPPTR(ss,ix);
8321 TOPPTR(nss,ix) = sv_dup_inc(sv);
8322 sv = (SV*)POPPTR(ss,ix);
8323 TOPPTR(nss,ix) = sv_dup_inc(sv);
8325 case SAVEt_SV: /* scalar reference */
8326 sv = (SV*)POPPTR(ss,ix);
8327 TOPPTR(nss,ix) = sv_dup_inc(sv);
8328 gv = (GV*)POPPTR(ss,ix);
8329 TOPPTR(nss,ix) = gv_dup_inc(gv);
8331 case SAVEt_GENERIC_PVREF: /* generic char* */
8332 c = (char*)POPPTR(ss,ix);
8333 TOPPTR(nss,ix) = pv_dup(c);
8334 ptr = POPPTR(ss,ix);
8335 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
8337 case SAVEt_GENERIC_SVREF: /* generic sv */
8338 case SAVEt_SVREF: /* scalar reference */
8339 sv = (SV*)POPPTR(ss,ix);
8340 TOPPTR(nss,ix) = sv_dup_inc(sv);
8341 ptr = POPPTR(ss,ix);
8342 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
8344 case SAVEt_AV: /* array reference */
8345 av = (AV*)POPPTR(ss,ix);
8346 TOPPTR(nss,ix) = av_dup_inc(av);
8347 gv = (GV*)POPPTR(ss,ix);
8348 TOPPTR(nss,ix) = gv_dup(gv);
8350 case SAVEt_HV: /* hash reference */
8351 hv = (HV*)POPPTR(ss,ix);
8352 TOPPTR(nss,ix) = hv_dup_inc(hv);
8353 gv = (GV*)POPPTR(ss,ix);
8354 TOPPTR(nss,ix) = gv_dup(gv);
8356 case SAVEt_INT: /* int reference */
8357 ptr = POPPTR(ss,ix);
8358 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
8359 intval = (int)POPINT(ss,ix);
8360 TOPINT(nss,ix) = intval;
8362 case SAVEt_LONG: /* long reference */
8363 ptr = POPPTR(ss,ix);
8364 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
8365 longval = (long)POPLONG(ss,ix);
8366 TOPLONG(nss,ix) = longval;
8368 case SAVEt_I32: /* I32 reference */
8369 case SAVEt_I16: /* I16 reference */
8370 case SAVEt_I8: /* I8 reference */
8371 ptr = POPPTR(ss,ix);
8372 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
8376 case SAVEt_IV: /* IV reference */
8377 ptr = POPPTR(ss,ix);
8378 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
8382 case SAVEt_SPTR: /* SV* reference */
8383 ptr = POPPTR(ss,ix);
8384 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
8385 sv = (SV*)POPPTR(ss,ix);
8386 TOPPTR(nss,ix) = sv_dup(sv);
8388 case SAVEt_VPTR: /* random* reference */
8389 ptr = POPPTR(ss,ix);
8390 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
8391 ptr = POPPTR(ss,ix);
8392 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
8394 case SAVEt_PPTR: /* char* reference */
8395 ptr = POPPTR(ss,ix);
8396 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
8397 c = (char*)POPPTR(ss,ix);
8398 TOPPTR(nss,ix) = pv_dup(c);
8400 case SAVEt_HPTR: /* HV* reference */
8401 ptr = POPPTR(ss,ix);
8402 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
8403 hv = (HV*)POPPTR(ss,ix);
8404 TOPPTR(nss,ix) = hv_dup(hv);
8406 case SAVEt_APTR: /* AV* reference */
8407 ptr = POPPTR(ss,ix);
8408 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
8409 av = (AV*)POPPTR(ss,ix);
8410 TOPPTR(nss,ix) = av_dup(av);
8413 gv = (GV*)POPPTR(ss,ix);
8414 TOPPTR(nss,ix) = gv_dup(gv);
8416 case SAVEt_GP: /* scalar reference */
8417 gp = (GP*)POPPTR(ss,ix);
8418 TOPPTR(nss,ix) = gp = gp_dup(gp);
8419 (void)GpREFCNT_inc(gp);
8420 gv = (GV*)POPPTR(ss,ix);
8421 TOPPTR(nss,ix) = gv_dup_inc(c);
8422 c = (char*)POPPTR(ss,ix);
8423 TOPPTR(nss,ix) = pv_dup(c);
8430 sv = (SV*)POPPTR(ss,ix);
8431 TOPPTR(nss,ix) = sv_dup_inc(sv);
8434 ptr = POPPTR(ss,ix);
8435 if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) {
8436 /* these are assumed to be refcounted properly */
8437 switch (((OP*)ptr)->op_type) {
8444 TOPPTR(nss,ix) = ptr;
8449 TOPPTR(nss,ix) = Nullop;
8454 TOPPTR(nss,ix) = Nullop;
8457 c = (char*)POPPTR(ss,ix);
8458 TOPPTR(nss,ix) = pv_dup_inc(c);
8461 longval = POPLONG(ss,ix);
8462 TOPLONG(nss,ix) = longval;
8465 hv = (HV*)POPPTR(ss,ix);
8466 TOPPTR(nss,ix) = hv_dup_inc(hv);
8467 c = (char*)POPPTR(ss,ix);
8468 TOPPTR(nss,ix) = pv_dup_inc(c);
8472 case SAVEt_DESTRUCTOR:
8473 ptr = POPPTR(ss,ix);
8474 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
8475 dptr = POPDPTR(ss,ix);
8476 TOPDPTR(nss,ix) = (void (*)(void*))any_dup((void *)dptr, proto_perl);
8478 case SAVEt_DESTRUCTOR_X:
8479 ptr = POPPTR(ss,ix);
8480 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
8481 dxptr = POPDXPTR(ss,ix);
8482 TOPDXPTR(nss,ix) = (void (*)(pTHXo_ void*))any_dup((void *)dxptr, proto_perl);
8484 case SAVEt_REGCONTEXT:
8490 case SAVEt_STACK_POS: /* Position on Perl stack */
8494 case SAVEt_AELEM: /* array element */
8495 sv = (SV*)POPPTR(ss,ix);
8496 TOPPTR(nss,ix) = sv_dup_inc(sv);
8499 av = (AV*)POPPTR(ss,ix);
8500 TOPPTR(nss,ix) = av_dup_inc(av);
8502 case SAVEt_HELEM: /* hash element */
8503 sv = (SV*)POPPTR(ss,ix);
8504 TOPPTR(nss,ix) = sv_dup_inc(sv);
8505 sv = (SV*)POPPTR(ss,ix);
8506 TOPPTR(nss,ix) = sv_dup_inc(sv);
8507 hv = (HV*)POPPTR(ss,ix);
8508 TOPPTR(nss,ix) = hv_dup_inc(hv);
8511 ptr = POPPTR(ss,ix);
8512 TOPPTR(nss,ix) = ptr;
8519 av = (AV*)POPPTR(ss,ix);
8520 TOPPTR(nss,ix) = av_dup(av);
8523 longval = (long)POPLONG(ss,ix);
8524 TOPLONG(nss,ix) = longval;
8525 ptr = POPPTR(ss,ix);
8526 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
8527 sv = (SV*)POPPTR(ss,ix);
8528 TOPPTR(nss,ix) = sv_dup(sv);
8531 Perl_croak(aTHX_ "panic: ss_dup inconsistency");
8543 perl_clone(PerlInterpreter *proto_perl, UV flags)
8546 CPerlObj *pPerl = (CPerlObj*)proto_perl;
8549 #ifdef PERL_IMPLICIT_SYS
8550 return perl_clone_using(proto_perl, flags,
8552 proto_perl->IMemShared,
8553 proto_perl->IMemParse,
8563 perl_clone_using(PerlInterpreter *proto_perl, UV flags,
8564 struct IPerlMem* ipM, struct IPerlMem* ipMS,
8565 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
8566 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
8567 struct IPerlDir* ipD, struct IPerlSock* ipS,
8568 struct IPerlProc* ipP)
8570 /* XXX many of the string copies here can be optimized if they're
8571 * constants; they need to be allocated as common memory and just
8572 * their pointers copied. */
8576 CPerlObj *pPerl = new(ipM) CPerlObj(ipM, ipMS, ipMP, ipE, ipStd, ipLIO,
8578 PERL_SET_THX(pPerl);
8579 # else /* !PERL_OBJECT */
8580 PerlInterpreter *my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
8581 PERL_SET_THX(my_perl);
8584 memset(my_perl, 0xab, sizeof(PerlInterpreter));
8590 # else /* !DEBUGGING */
8591 Zero(my_perl, 1, PerlInterpreter);
8592 # endif /* DEBUGGING */
8596 PL_MemShared = ipMS;
8604 # endif /* PERL_OBJECT */
8605 #else /* !PERL_IMPLICIT_SYS */
8607 PerlInterpreter *my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
8608 PERL_SET_THX(my_perl);
8611 memset(my_perl, 0xab, sizeof(PerlInterpreter));
8617 # else /* !DEBUGGING */
8618 Zero(my_perl, 1, PerlInterpreter);
8619 # endif /* DEBUGGING */
8620 #endif /* PERL_IMPLICIT_SYS */
8623 PL_xiv_arenaroot = NULL;
8625 PL_xnv_arenaroot = NULL;
8627 PL_xrv_arenaroot = NULL;
8629 PL_xpv_arenaroot = NULL;
8631 PL_xpviv_arenaroot = NULL;
8632 PL_xpviv_root = NULL;
8633 PL_xpvnv_arenaroot = NULL;
8634 PL_xpvnv_root = NULL;
8635 PL_xpvcv_arenaroot = NULL;
8636 PL_xpvcv_root = NULL;
8637 PL_xpvav_arenaroot = NULL;
8638 PL_xpvav_root = NULL;
8639 PL_xpvhv_arenaroot = NULL;
8640 PL_xpvhv_root = NULL;
8641 PL_xpvmg_arenaroot = NULL;
8642 PL_xpvmg_root = NULL;
8643 PL_xpvlv_arenaroot = NULL;
8644 PL_xpvlv_root = NULL;
8645 PL_xpvbm_arenaroot = NULL;
8646 PL_xpvbm_root = NULL;
8647 PL_he_arenaroot = NULL;
8649 PL_nice_chunk = NULL;
8650 PL_nice_chunk_size = 0;
8653 PL_sv_root = Nullsv;
8654 PL_sv_arenaroot = Nullsv;
8656 PL_debug = proto_perl->Idebug;
8658 /* create SV map for pointer relocation */
8659 PL_ptr_table = ptr_table_new();
8661 /* initialize these special pointers as early as possible */
8662 SvANY(&PL_sv_undef) = NULL;
8663 SvREFCNT(&PL_sv_undef) = (~(U32)0)/2;
8664 SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL;
8665 ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef);
8668 SvUPGRADE(&PL_sv_no, SVt_PVNV);
8670 SvANY(&PL_sv_no) = new_XPVNV();
8672 SvREFCNT(&PL_sv_no) = (~(U32)0)/2;
8673 SvFLAGS(&PL_sv_no) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
8674 SvPVX(&PL_sv_no) = SAVEPVN(PL_No, 0);
8675 SvCUR(&PL_sv_no) = 0;
8676 SvLEN(&PL_sv_no) = 1;
8677 SvNVX(&PL_sv_no) = 0;
8678 ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no);
8681 SvUPGRADE(&PL_sv_yes, SVt_PVNV);
8683 SvANY(&PL_sv_yes) = new_XPVNV();
8685 SvREFCNT(&PL_sv_yes) = (~(U32)0)/2;
8686 SvFLAGS(&PL_sv_yes) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
8687 SvPVX(&PL_sv_yes) = SAVEPVN(PL_Yes, 1);
8688 SvCUR(&PL_sv_yes) = 1;
8689 SvLEN(&PL_sv_yes) = 2;
8690 SvNVX(&PL_sv_yes) = 1;
8691 ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes);
8693 /* create shared string table */
8694 PL_strtab = newHV();
8695 HvSHAREKEYS_off(PL_strtab);
8696 hv_ksplit(PL_strtab, 512);
8697 ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab);
8699 PL_compiling = proto_perl->Icompiling;
8700 PL_compiling.cop_stashpv = SAVEPV(PL_compiling.cop_stashpv);
8701 PL_compiling.cop_file = SAVEPV(PL_compiling.cop_file);
8702 ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling);
8703 if (!specialWARN(PL_compiling.cop_warnings))
8704 PL_compiling.cop_warnings = sv_dup_inc(PL_compiling.cop_warnings);
8705 if (!specialCopIO(PL_compiling.cop_io))
8706 PL_compiling.cop_io = sv_dup_inc(PL_compiling.cop_io);
8707 PL_curcop = (COP*)any_dup(proto_perl->Tcurcop, proto_perl);
8709 /* pseudo environmental stuff */
8710 PL_origargc = proto_perl->Iorigargc;
8712 New(0, PL_origargv, i+1, char*);
8713 PL_origargv[i] = '\0';
8715 PL_origargv[i] = SAVEPV(proto_perl->Iorigargv[i]);
8717 PL_envgv = gv_dup(proto_perl->Ienvgv);
8718 PL_incgv = gv_dup(proto_perl->Iincgv);
8719 PL_hintgv = gv_dup(proto_perl->Ihintgv);
8720 PL_origfilename = SAVEPV(proto_perl->Iorigfilename);
8721 PL_diehook = sv_dup_inc(proto_perl->Idiehook);
8722 PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook);
8725 PL_minus_c = proto_perl->Iminus_c;
8726 PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel);
8727 PL_localpatches = proto_perl->Ilocalpatches;
8728 PL_splitstr = proto_perl->Isplitstr;
8729 PL_preprocess = proto_perl->Ipreprocess;
8730 PL_minus_n = proto_perl->Iminus_n;
8731 PL_minus_p = proto_perl->Iminus_p;
8732 PL_minus_l = proto_perl->Iminus_l;
8733 PL_minus_a = proto_perl->Iminus_a;
8734 PL_minus_F = proto_perl->Iminus_F;
8735 PL_doswitches = proto_perl->Idoswitches;
8736 PL_dowarn = proto_perl->Idowarn;
8737 PL_doextract = proto_perl->Idoextract;
8738 PL_sawampersand = proto_perl->Isawampersand;
8739 PL_unsafe = proto_perl->Iunsafe;
8740 PL_inplace = SAVEPV(proto_perl->Iinplace);
8741 PL_e_script = sv_dup_inc(proto_perl->Ie_script);
8742 PL_perldb = proto_perl->Iperldb;
8743 PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
8745 /* magical thingies */
8746 /* XXX time(&PL_basetime) when asked for? */
8747 PL_basetime = proto_perl->Ibasetime;
8748 PL_formfeed = sv_dup(proto_perl->Iformfeed);
8750 PL_maxsysfd = proto_perl->Imaxsysfd;
8751 PL_multiline = proto_perl->Imultiline;
8752 PL_statusvalue = proto_perl->Istatusvalue;
8754 PL_statusvalue_vms = proto_perl->Istatusvalue_vms;
8757 /* shortcuts to various I/O objects */
8758 PL_stdingv = gv_dup(proto_perl->Istdingv);
8759 PL_stderrgv = gv_dup(proto_perl->Istderrgv);
8760 PL_defgv = gv_dup(proto_perl->Idefgv);
8761 PL_argvgv = gv_dup(proto_perl->Iargvgv);
8762 PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv);
8763 PL_argvout_stack = av_dup(proto_perl->Iargvout_stack);
8765 /* shortcuts to regexp stuff */
8766 PL_replgv = gv_dup(proto_perl->Ireplgv);
8768 /* shortcuts to misc objects */
8769 PL_errgv = gv_dup(proto_perl->Ierrgv);
8771 /* shortcuts to debugging objects */
8772 PL_DBgv = gv_dup(proto_perl->IDBgv);
8773 PL_DBline = gv_dup(proto_perl->IDBline);
8774 PL_DBsub = gv_dup(proto_perl->IDBsub);
8775 PL_DBsingle = sv_dup(proto_perl->IDBsingle);
8776 PL_DBtrace = sv_dup(proto_perl->IDBtrace);
8777 PL_DBsignal = sv_dup(proto_perl->IDBsignal);
8778 PL_lineary = av_dup(proto_perl->Ilineary);
8779 PL_dbargs = av_dup(proto_perl->Idbargs);
8782 PL_defstash = hv_dup_inc(proto_perl->Tdefstash);
8783 PL_curstash = hv_dup(proto_perl->Tcurstash);
8784 PL_debstash = hv_dup(proto_perl->Idebstash);
8785 PL_globalstash = hv_dup(proto_perl->Iglobalstash);
8786 PL_curstname = sv_dup_inc(proto_perl->Icurstname);
8788 PL_beginav = av_dup_inc(proto_perl->Ibeginav);
8789 PL_endav = av_dup_inc(proto_perl->Iendav);
8790 PL_checkav = av_dup_inc(proto_perl->Icheckav);
8791 PL_initav = av_dup_inc(proto_perl->Iinitav);
8793 PL_sub_generation = proto_perl->Isub_generation;
8795 /* funky return mechanisms */
8796 PL_forkprocess = proto_perl->Iforkprocess;
8798 /* subprocess state */
8799 PL_fdpid = av_dup_inc(proto_perl->Ifdpid);
8801 /* internal state */
8802 PL_tainting = proto_perl->Itainting;
8803 PL_maxo = proto_perl->Imaxo;
8804 if (proto_perl->Iop_mask)
8805 PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo);
8807 PL_op_mask = Nullch;
8809 /* current interpreter roots */
8810 PL_main_cv = cv_dup_inc(proto_perl->Imain_cv);
8811 PL_main_root = OpREFCNT_inc(proto_perl->Imain_root);
8812 PL_main_start = proto_perl->Imain_start;
8813 PL_eval_root = proto_perl->Ieval_root;
8814 PL_eval_start = proto_perl->Ieval_start;
8816 /* runtime control stuff */
8817 PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl);
8818 PL_copline = proto_perl->Icopline;
8820 PL_filemode = proto_perl->Ifilemode;
8821 PL_lastfd = proto_perl->Ilastfd;
8822 PL_oldname = proto_perl->Ioldname; /* XXX not quite right */
8825 PL_gensym = proto_perl->Igensym;
8826 PL_preambled = proto_perl->Ipreambled;
8827 PL_preambleav = av_dup_inc(proto_perl->Ipreambleav);
8828 PL_laststatval = proto_perl->Ilaststatval;
8829 PL_laststype = proto_perl->Ilaststype;
8830 PL_mess_sv = Nullsv;
8832 PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv);
8833 PL_ofmt = SAVEPV(proto_perl->Iofmt);
8835 /* interpreter atexit processing */
8836 PL_exitlistlen = proto_perl->Iexitlistlen;
8837 if (PL_exitlistlen) {
8838 New(0, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
8839 Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
8842 PL_exitlist = (PerlExitListEntry*)NULL;
8843 PL_modglobal = hv_dup_inc(proto_perl->Imodglobal);
8845 PL_profiledata = NULL;
8846 PL_rsfp = fp_dup(proto_perl->Irsfp, '<');
8847 /* PL_rsfp_filters entries have fake IoDIRP() */
8848 PL_rsfp_filters = av_dup_inc(proto_perl->Irsfp_filters);
8850 PL_compcv = cv_dup(proto_perl->Icompcv);
8851 PL_comppad = av_dup(proto_perl->Icomppad);
8852 PL_comppad_name = av_dup(proto_perl->Icomppad_name);
8853 PL_comppad_name_fill = proto_perl->Icomppad_name_fill;
8854 PL_comppad_name_floor = proto_perl->Icomppad_name_floor;
8855 PL_curpad = (SV**)ptr_table_fetch(PL_ptr_table,
8856 proto_perl->Tcurpad);
8858 #ifdef HAVE_INTERP_INTERN
8859 sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern);
8862 /* more statics moved here */
8863 PL_generation = proto_perl->Igeneration;
8864 PL_DBcv = cv_dup(proto_perl->IDBcv);
8866 PL_in_clean_objs = proto_perl->Iin_clean_objs;
8867 PL_in_clean_all = proto_perl->Iin_clean_all;
8869 PL_uid = proto_perl->Iuid;
8870 PL_euid = proto_perl->Ieuid;
8871 PL_gid = proto_perl->Igid;
8872 PL_egid = proto_perl->Iegid;
8873 PL_nomemok = proto_perl->Inomemok;
8874 PL_an = proto_perl->Ian;
8875 PL_cop_seqmax = proto_perl->Icop_seqmax;
8876 PL_op_seqmax = proto_perl->Iop_seqmax;
8877 PL_evalseq = proto_perl->Ievalseq;
8878 PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */
8879 PL_origalen = proto_perl->Iorigalen;
8880 PL_pidstatus = newHV(); /* XXX flag for cloning? */
8881 PL_osname = SAVEPV(proto_perl->Iosname);
8882 PL_sh_path = SAVEPV(proto_perl->Ish_path);
8883 PL_sighandlerp = proto_perl->Isighandlerp;
8886 PL_runops = proto_perl->Irunops;
8888 Copy(proto_perl->Itokenbuf, PL_tokenbuf, 256, char);
8891 PL_cshlen = proto_perl->Icshlen;
8892 PL_cshname = SAVEPVN(proto_perl->Icshname, PL_cshlen);
8895 PL_lex_state = proto_perl->Ilex_state;
8896 PL_lex_defer = proto_perl->Ilex_defer;
8897 PL_lex_expect = proto_perl->Ilex_expect;
8898 PL_lex_formbrack = proto_perl->Ilex_formbrack;
8899 PL_lex_dojoin = proto_perl->Ilex_dojoin;
8900 PL_lex_starts = proto_perl->Ilex_starts;
8901 PL_lex_stuff = sv_dup_inc(proto_perl->Ilex_stuff);
8902 PL_lex_repl = sv_dup_inc(proto_perl->Ilex_repl);
8903 PL_lex_op = proto_perl->Ilex_op;
8904 PL_lex_inpat = proto_perl->Ilex_inpat;
8905 PL_lex_inwhat = proto_perl->Ilex_inwhat;
8906 PL_lex_brackets = proto_perl->Ilex_brackets;
8907 i = (PL_lex_brackets < 120 ? 120 : PL_lex_brackets);
8908 PL_lex_brackstack = SAVEPVN(proto_perl->Ilex_brackstack,i);
8909 PL_lex_casemods = proto_perl->Ilex_casemods;
8910 i = (PL_lex_casemods < 12 ? 12 : PL_lex_casemods);
8911 PL_lex_casestack = SAVEPVN(proto_perl->Ilex_casestack,i);
8913 Copy(proto_perl->Inextval, PL_nextval, 5, YYSTYPE);
8914 Copy(proto_perl->Inexttype, PL_nexttype, 5, I32);
8915 PL_nexttoke = proto_perl->Inexttoke;
8917 PL_linestr = sv_dup_inc(proto_perl->Ilinestr);
8918 i = proto_perl->Ibufptr - SvPVX(proto_perl->Ilinestr);
8919 PL_bufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
8920 i = proto_perl->Ioldbufptr - SvPVX(proto_perl->Ilinestr);
8921 PL_oldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
8922 i = proto_perl->Ioldoldbufptr - SvPVX(proto_perl->Ilinestr);
8923 PL_oldoldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
8924 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
8925 i = proto_perl->Ilinestart - SvPVX(proto_perl->Ilinestr);
8926 PL_linestart = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
8927 PL_pending_ident = proto_perl->Ipending_ident;
8928 PL_sublex_info = proto_perl->Isublex_info; /* XXX not quite right */
8930 PL_expect = proto_perl->Iexpect;
8932 PL_multi_start = proto_perl->Imulti_start;
8933 PL_multi_end = proto_perl->Imulti_end;
8934 PL_multi_open = proto_perl->Imulti_open;
8935 PL_multi_close = proto_perl->Imulti_close;
8937 PL_error_count = proto_perl->Ierror_count;
8938 PL_subline = proto_perl->Isubline;
8939 PL_subname = sv_dup_inc(proto_perl->Isubname);
8941 PL_min_intro_pending = proto_perl->Imin_intro_pending;
8942 PL_max_intro_pending = proto_perl->Imax_intro_pending;
8943 PL_padix = proto_perl->Ipadix;
8944 PL_padix_floor = proto_perl->Ipadix_floor;
8945 PL_pad_reset_pending = proto_perl->Ipad_reset_pending;
8947 i = proto_perl->Ilast_uni - SvPVX(proto_perl->Ilinestr);
8948 PL_last_uni = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
8949 i = proto_perl->Ilast_lop - SvPVX(proto_perl->Ilinestr);
8950 PL_last_lop = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
8951 PL_last_lop_op = proto_perl->Ilast_lop_op;
8952 PL_in_my = proto_perl->Iin_my;
8953 PL_in_my_stash = hv_dup(proto_perl->Iin_my_stash);
8955 PL_cryptseen = proto_perl->Icryptseen;
8958 PL_hints = proto_perl->Ihints;
8960 PL_amagic_generation = proto_perl->Iamagic_generation;
8962 #ifdef USE_LOCALE_COLLATE
8963 PL_collation_ix = proto_perl->Icollation_ix;
8964 PL_collation_name = SAVEPV(proto_perl->Icollation_name);
8965 PL_collation_standard = proto_perl->Icollation_standard;
8966 PL_collxfrm_base = proto_perl->Icollxfrm_base;
8967 PL_collxfrm_mult = proto_perl->Icollxfrm_mult;
8968 #endif /* USE_LOCALE_COLLATE */
8970 #ifdef USE_LOCALE_NUMERIC
8971 PL_numeric_name = SAVEPV(proto_perl->Inumeric_name);
8972 PL_numeric_standard = proto_perl->Inumeric_standard;
8973 PL_numeric_local = proto_perl->Inumeric_local;
8974 PL_numeric_radix = sv_dup_inc(proto_perl->Inumeric_radix);
8975 #endif /* !USE_LOCALE_NUMERIC */
8977 /* utf8 character classes */
8978 PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum);
8979 PL_utf8_alnumc = sv_dup_inc(proto_perl->Iutf8_alnumc);
8980 PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii);
8981 PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha);
8982 PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space);
8983 PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl);
8984 PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph);
8985 PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit);
8986 PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper);
8987 PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower);
8988 PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print);
8989 PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct);
8990 PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit);
8991 PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark);
8992 PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper);
8993 PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle);
8994 PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower);
8997 PL_last_swash_hv = Nullhv; /* reinits on demand */
8998 PL_last_swash_klen = 0;
8999 PL_last_swash_key[0]= '\0';
9000 PL_last_swash_tmps = (U8*)NULL;
9001 PL_last_swash_slen = 0;
9003 /* perly.c globals */
9004 PL_yydebug = proto_perl->Iyydebug;
9005 PL_yynerrs = proto_perl->Iyynerrs;
9006 PL_yyerrflag = proto_perl->Iyyerrflag;
9007 PL_yychar = proto_perl->Iyychar;
9008 PL_yyval = proto_perl->Iyyval;
9009 PL_yylval = proto_perl->Iyylval;
9011 PL_glob_index = proto_perl->Iglob_index;
9012 PL_srand_called = proto_perl->Isrand_called;
9013 PL_uudmap['M'] = 0; /* reinits on demand */
9014 PL_bitcount = Nullch; /* reinits on demand */
9016 if (proto_perl->Ipsig_pend) {
9017 Newz(0, PL_psig_pend, SIG_SIZE, int);
9020 PL_psig_pend = (int*)NULL;
9023 if (proto_perl->Ipsig_ptr) {
9024 Newz(0, PL_psig_ptr, SIG_SIZE, SV*);
9025 Newz(0, PL_psig_name, SIG_SIZE, SV*);
9026 for (i = 1; i < SIG_SIZE; i++) {
9027 PL_psig_ptr[i] = sv_dup_inc(proto_perl->Ipsig_ptr[i]);
9028 PL_psig_name[i] = sv_dup_inc(proto_perl->Ipsig_name[i]);
9032 PL_psig_ptr = (SV**)NULL;
9033 PL_psig_name = (SV**)NULL;
9036 /* thrdvar.h stuff */
9038 if (flags & CLONEf_COPY_STACKS) {
9039 /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */
9040 PL_tmps_ix = proto_perl->Ttmps_ix;
9041 PL_tmps_max = proto_perl->Ttmps_max;
9042 PL_tmps_floor = proto_perl->Ttmps_floor;
9043 Newz(50, PL_tmps_stack, PL_tmps_max, SV*);
9045 while (i <= PL_tmps_ix) {
9046 PL_tmps_stack[i] = sv_dup_inc(proto_perl->Ttmps_stack[i]);
9050 /* next PUSHMARK() sets *(PL_markstack_ptr+1) */
9051 i = proto_perl->Tmarkstack_max - proto_perl->Tmarkstack;
9052 Newz(54, PL_markstack, i, I32);
9053 PL_markstack_max = PL_markstack + (proto_perl->Tmarkstack_max
9054 - proto_perl->Tmarkstack);
9055 PL_markstack_ptr = PL_markstack + (proto_perl->Tmarkstack_ptr
9056 - proto_perl->Tmarkstack);
9057 Copy(proto_perl->Tmarkstack, PL_markstack,
9058 PL_markstack_ptr - PL_markstack + 1, I32);
9060 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
9061 * NOTE: unlike the others! */
9062 PL_scopestack_ix = proto_perl->Tscopestack_ix;
9063 PL_scopestack_max = proto_perl->Tscopestack_max;
9064 Newz(54, PL_scopestack, PL_scopestack_max, I32);
9065 Copy(proto_perl->Tscopestack, PL_scopestack, PL_scopestack_ix, I32);
9067 /* next push_return() sets PL_retstack[PL_retstack_ix]
9068 * NOTE: unlike the others! */
9069 PL_retstack_ix = proto_perl->Tretstack_ix;
9070 PL_retstack_max = proto_perl->Tretstack_max;
9071 Newz(54, PL_retstack, PL_retstack_max, OP*);
9072 Copy(proto_perl->Tretstack, PL_retstack, PL_retstack_ix, I32);
9074 /* NOTE: si_dup() looks at PL_markstack */
9075 PL_curstackinfo = si_dup(proto_perl->Tcurstackinfo);
9077 /* PL_curstack = PL_curstackinfo->si_stack; */
9078 PL_curstack = av_dup(proto_perl->Tcurstack);
9079 PL_mainstack = av_dup(proto_perl->Tmainstack);
9081 /* next PUSHs() etc. set *(PL_stack_sp+1) */
9082 PL_stack_base = AvARRAY(PL_curstack);
9083 PL_stack_sp = PL_stack_base + (proto_perl->Tstack_sp
9084 - proto_perl->Tstack_base);
9085 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
9087 /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix]
9088 * NOTE: unlike the others! */
9089 PL_savestack_ix = proto_perl->Tsavestack_ix;
9090 PL_savestack_max = proto_perl->Tsavestack_max;
9091 /*Newz(54, PL_savestack, PL_savestack_max, ANY);*/
9092 PL_savestack = ss_dup(proto_perl);
9096 ENTER; /* perl_destruct() wants to LEAVE; */
9099 PL_start_env = proto_perl->Tstart_env; /* XXXXXX */
9100 PL_top_env = &PL_start_env;
9102 PL_op = proto_perl->Top;
9105 PL_Xpv = (XPV*)NULL;
9106 PL_na = proto_perl->Tna;
9108 PL_statbuf = proto_perl->Tstatbuf;
9109 PL_statcache = proto_perl->Tstatcache;
9110 PL_statgv = gv_dup(proto_perl->Tstatgv);
9111 PL_statname = sv_dup_inc(proto_perl->Tstatname);
9113 PL_timesbuf = proto_perl->Ttimesbuf;
9116 PL_tainted = proto_perl->Ttainted;
9117 PL_curpm = proto_perl->Tcurpm; /* XXX No PMOP ref count */
9118 PL_nrs = sv_dup_inc(proto_perl->Tnrs);
9119 PL_rs = sv_dup_inc(proto_perl->Trs);
9120 PL_last_in_gv = gv_dup(proto_perl->Tlast_in_gv);
9121 PL_ofs_sv = sv_dup_inc(proto_perl->Tofs_sv);
9122 PL_defoutgv = gv_dup_inc(proto_perl->Tdefoutgv);
9123 PL_chopset = proto_perl->Tchopset; /* XXX never deallocated */
9124 PL_toptarget = sv_dup_inc(proto_perl->Ttoptarget);
9125 PL_bodytarget = sv_dup_inc(proto_perl->Tbodytarget);
9126 PL_formtarget = sv_dup(proto_perl->Tformtarget);
9128 PL_restartop = proto_perl->Trestartop;
9129 PL_in_eval = proto_perl->Tin_eval;
9130 PL_delaymagic = proto_perl->Tdelaymagic;
9131 PL_dirty = proto_perl->Tdirty;
9132 PL_localizing = proto_perl->Tlocalizing;
9134 #ifdef PERL_FLEXIBLE_EXCEPTIONS
9135 PL_protect = proto_perl->Tprotect;
9137 PL_errors = sv_dup_inc(proto_perl->Terrors);
9138 PL_av_fetch_sv = Nullsv;
9139 PL_hv_fetch_sv = Nullsv;
9140 Zero(&PL_hv_fetch_ent_mh, 1, HE); /* XXX */
9141 PL_modcount = proto_perl->Tmodcount;
9142 PL_lastgotoprobe = Nullop;
9143 PL_dumpindent = proto_perl->Tdumpindent;
9145 PL_sortcop = (OP*)any_dup(proto_perl->Tsortcop, proto_perl);
9146 PL_sortstash = hv_dup(proto_perl->Tsortstash);
9147 PL_firstgv = gv_dup(proto_perl->Tfirstgv);
9148 PL_secondgv = gv_dup(proto_perl->Tsecondgv);
9149 PL_sortcxix = proto_perl->Tsortcxix;
9150 PL_efloatbuf = Nullch; /* reinits on demand */
9151 PL_efloatsize = 0; /* reinits on demand */
9155 PL_screamfirst = NULL;
9156 PL_screamnext = NULL;
9157 PL_maxscream = -1; /* reinits on demand */
9158 PL_lastscream = Nullsv;
9160 PL_watchaddr = NULL;
9161 PL_watchok = Nullch;
9163 PL_regdummy = proto_perl->Tregdummy;
9164 PL_regcomp_parse = Nullch;
9165 PL_regxend = Nullch;
9166 PL_regcode = (regnode*)NULL;
9169 PL_regprecomp = Nullch;
9174 PL_seen_zerolen = 0;
9176 PL_regcomp_rx = (regexp*)NULL;
9178 PL_colorset = 0; /* reinits PL_colors[] */
9179 /*PL_colors[6] = {0,0,0,0,0,0};*/
9180 PL_reg_whilem_seen = 0;
9181 PL_reginput = Nullch;
9184 PL_regstartp = (I32*)NULL;
9185 PL_regendp = (I32*)NULL;
9186 PL_reglastparen = (U32*)NULL;
9187 PL_regtill = Nullch;
9189 PL_reg_start_tmp = (char**)NULL;
9190 PL_reg_start_tmpl = 0;
9191 PL_regdata = (struct reg_data*)NULL;
9194 PL_reg_eval_set = 0;
9196 PL_regprogram = (regnode*)NULL;
9198 PL_regcc = (CURCUR*)NULL;
9199 PL_reg_call_cc = (struct re_cc_state*)NULL;
9200 PL_reg_re = (regexp*)NULL;
9201 PL_reg_ganch = Nullch;
9203 PL_reg_magic = (MAGIC*)NULL;
9205 PL_reg_oldcurpm = (PMOP*)NULL;
9206 PL_reg_curpm = (PMOP*)NULL;
9207 PL_reg_oldsaved = Nullch;
9208 PL_reg_oldsavedlen = 0;
9210 PL_reg_leftiter = 0;
9211 PL_reg_poscache = Nullch;
9212 PL_reg_poscache_size= 0;
9214 /* RE engine - function pointers */
9215 PL_regcompp = proto_perl->Tregcompp;
9216 PL_regexecp = proto_perl->Tregexecp;
9217 PL_regint_start = proto_perl->Tregint_start;
9218 PL_regint_string = proto_perl->Tregint_string;
9219 PL_regfree = proto_perl->Tregfree;
9221 PL_reginterp_cnt = 0;
9222 PL_reg_starttry = 0;
9224 if (!(flags & CLONEf_KEEP_PTR_TABLE)) {
9225 ptr_table_free(PL_ptr_table);
9226 PL_ptr_table = NULL;
9230 return (PerlInterpreter*)pPerl;
9236 #else /* !USE_ITHREADS */
9242 #endif /* USE_ITHREADS */
9245 do_report_used(pTHXo_ SV *sv)
9247 if (SvTYPE(sv) != SVTYPEMASK) {
9248 PerlIO_printf(Perl_debug_log, "****\n");
9254 do_clean_objs(pTHXo_ SV *sv)
9258 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
9259 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv));)
9260 if (SvWEAKREF(sv)) {
9271 /* XXX Might want to check arrays, etc. */
9274 #ifndef DISABLE_DESTRUCTOR_KLUDGE
9276 do_clean_named_objs(pTHXo_ SV *sv)
9278 if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) {
9279 if ( SvOBJECT(GvSV(sv)) ||
9280 (GvAV(sv) && SvOBJECT(GvAV(sv))) ||
9281 (GvHV(sv) && SvOBJECT(GvHV(sv))) ||
9282 (GvIO(sv) && SvOBJECT(GvIO(sv))) ||
9283 (GvCV(sv) && SvOBJECT(GvCV(sv))) )
9285 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv));)
9293 do_clean_all(pTHXo_ SV *sv)
9295 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) );)
9296 SvFLAGS(sv) |= SVf_BREAK;