3 * Copyright (c) 1991-2000, 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 (SvTYPE(sv) == mt)
922 switch (SvTYPE(sv)) {
943 else if (mt < SVt_PVIV)
960 pv = (char*)SvRV(sv);
980 else if (mt == SVt_NV)
991 del_XPVIV(SvANY(sv));
1001 del_XPVNV(SvANY(sv));
1009 magic = SvMAGIC(sv);
1010 stash = SvSTASH(sv);
1011 del_XPVMG(SvANY(sv));
1014 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
1019 Perl_croak(aTHX_ "Can't upgrade to undef");
1021 SvANY(sv) = new_XIV();
1025 SvANY(sv) = new_XNV();
1029 SvANY(sv) = new_XRV();
1033 SvANY(sv) = new_XPV();
1039 SvANY(sv) = new_XPVIV();
1049 SvANY(sv) = new_XPVNV();
1057 SvANY(sv) = new_XPVMG();
1063 SvMAGIC(sv) = magic;
1064 SvSTASH(sv) = stash;
1067 SvANY(sv) = new_XPVLV();
1073 SvMAGIC(sv) = magic;
1074 SvSTASH(sv) = stash;
1081 SvANY(sv) = new_XPVAV();
1089 SvMAGIC(sv) = magic;
1090 SvSTASH(sv) = stash;
1096 SvANY(sv) = new_XPVHV();
1104 SvMAGIC(sv) = magic;
1105 SvSTASH(sv) = stash;
1112 SvANY(sv) = new_XPVCV();
1113 Zero(SvANY(sv), 1, XPVCV);
1119 SvMAGIC(sv) = magic;
1120 SvSTASH(sv) = stash;
1123 SvANY(sv) = new_XPVGV();
1129 SvMAGIC(sv) = magic;
1130 SvSTASH(sv) = stash;
1138 SvANY(sv) = new_XPVBM();
1144 SvMAGIC(sv) = magic;
1145 SvSTASH(sv) = stash;
1151 SvANY(sv) = new_XPVFM();
1152 Zero(SvANY(sv), 1, XPVFM);
1158 SvMAGIC(sv) = magic;
1159 SvSTASH(sv) = stash;
1162 SvANY(sv) = new_XPVIO();
1163 Zero(SvANY(sv), 1, XPVIO);
1169 SvMAGIC(sv) = magic;
1170 SvSTASH(sv) = stash;
1171 IoPAGE_LEN(sv) = 60;
1174 SvFLAGS(sv) &= ~SVTYPEMASK;
1180 Perl_sv_backoff(pTHX_ register SV *sv)
1184 char *s = SvPVX(sv);
1185 SvLEN(sv) += SvIVX(sv);
1186 SvPVX(sv) -= SvIVX(sv);
1188 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
1190 SvFLAGS(sv) &= ~SVf_OOK;
1197 Expands the character buffer in the SV. This will use C<sv_unref> and will
1198 upgrade the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1205 Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
1209 #ifdef HAS_64K_LIMIT
1210 if (newlen >= 0x10000) {
1211 PerlIO_printf(Perl_debug_log,
1212 "Allocation too large: %"UVxf"\n", (UV)newlen);
1215 #endif /* HAS_64K_LIMIT */
1218 if (SvTYPE(sv) < SVt_PV) {
1219 sv_upgrade(sv, SVt_PV);
1222 else if (SvOOK(sv)) { /* pv is offset? */
1225 if (newlen > SvLEN(sv))
1226 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
1227 #ifdef HAS_64K_LIMIT
1228 if (newlen >= 0x10000)
1234 if (newlen > SvLEN(sv)) { /* need more room? */
1235 if (SvLEN(sv) && s) {
1236 #if defined(MYMALLOC) && !defined(LEAKTEST)
1237 STRLEN l = malloced_size((void*)SvPVX(sv));
1243 Renew(s,newlen,char);
1246 New(703,s,newlen,char);
1248 SvLEN_set(sv, newlen);
1254 =for apidoc sv_setiv
1256 Copies an integer into the given SV. Does not handle 'set' magic. See
1263 Perl_sv_setiv(pTHX_ register SV *sv, IV i)
1265 SV_CHECK_THINKFIRST(sv);
1266 switch (SvTYPE(sv)) {
1268 sv_upgrade(sv, SVt_IV);
1271 sv_upgrade(sv, SVt_PVNV);
1275 sv_upgrade(sv, SVt_PVIV);
1286 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
1287 PL_op_desc[PL_op->op_type]);
1290 (void)SvIOK_only(sv); /* validate number */
1296 =for apidoc sv_setiv_mg
1298 Like C<sv_setiv>, but also handles 'set' magic.
1304 Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
1311 =for apidoc sv_setuv
1313 Copies an unsigned integer into the given SV. Does not handle 'set' magic.
1320 Perl_sv_setuv(pTHX_ register SV *sv, UV u)
1328 =for apidoc sv_setuv_mg
1330 Like C<sv_setuv>, but also handles 'set' magic.
1336 Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
1343 =for apidoc sv_setnv
1345 Copies a double into the given SV. Does not handle 'set' magic. See
1352 Perl_sv_setnv(pTHX_ register SV *sv, NV num)
1354 SV_CHECK_THINKFIRST(sv);
1355 switch (SvTYPE(sv)) {
1358 sv_upgrade(sv, SVt_NV);
1363 sv_upgrade(sv, SVt_PVNV);
1374 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
1375 PL_op_name[PL_op->op_type]);
1379 (void)SvNOK_only(sv); /* validate number */
1384 =for apidoc sv_setnv_mg
1386 Like C<sv_setnv>, but also handles 'set' magic.
1392 Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
1399 S_not_a_number(pTHX_ SV *sv)
1405 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
1406 /* each *s can expand to 4 chars + "...\0",
1407 i.e. need room for 8 chars */
1409 for (s = SvPVX(sv); *s && d < limit; s++) {
1411 if (ch & 128 && !isPRINT_LC(ch)) {
1420 else if (ch == '\r') {
1424 else if (ch == '\f') {
1428 else if (ch == '\\') {
1432 else if (isPRINT_LC(ch))
1447 Perl_warner(aTHX_ WARN_NUMERIC,
1448 "Argument \"%s\" isn't numeric in %s", tmpbuf,
1449 PL_op_desc[PL_op->op_type]);
1451 Perl_warner(aTHX_ WARN_NUMERIC,
1452 "Argument \"%s\" isn't numeric", tmpbuf);
1455 /* the number can be converted to integer with atol() or atoll() */
1456 #define IS_NUMBER_TO_INT_BY_ATOL 0x01
1457 #define IS_NUMBER_TO_INT_BY_ATOF 0x02 /* atol() may be != atof() */
1458 #define IS_NUMBER_NOT_IV 0x04 /* (IV)atof() may be != atof() */
1459 #define IS_NUMBER_NEG 0x08 /* not good to cache UV */
1460 #define IS_NUMBER_INFINITY 0x10 /* this is big */
1462 /* Actually, ISO C leaves conversion of UV to IV undefined, but
1463 until proven guilty, assume that things are not that bad... */
1466 Perl_sv_2iv(pTHX_ register SV *sv)
1470 if (SvGMAGICAL(sv)) {
1475 return I_V(SvNVX(sv));
1477 if (SvPOKp(sv) && SvLEN(sv))
1480 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1482 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1488 if (SvTHINKFIRST(sv)) {
1491 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv, numer)))
1492 return SvIV(tmpstr);
1493 return PTR2IV(SvRV(sv));
1495 if (SvREADONLY(sv) && !SvOK(sv)) {
1497 if (ckWARN(WARN_UNINITIALIZED))
1504 return (IV)(SvUVX(sv));
1511 /* We can cache the IV/UV value even if it not good enough
1512 * to reconstruct NV, since the conversion to PV will prefer
1516 if (SvTYPE(sv) == SVt_NV)
1517 sv_upgrade(sv, SVt_PVNV);
1520 if (SvNVX(sv) < (NV)IV_MAX + 0.5)
1521 SvIVX(sv) = I_V(SvNVX(sv));
1523 SvUVX(sv) = U_V(SvNVX(sv));
1526 DEBUG_c(PerlIO_printf(Perl_debug_log,
1527 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
1531 return (IV)SvUVX(sv);
1534 else if (SvPOKp(sv) && SvLEN(sv)) {
1535 I32 numtype = looks_like_number(sv);
1537 /* We want to avoid a possible problem when we cache an IV which
1538 may be later translated to an NV, and the resulting NV is not
1539 the translation of the initial data.
1541 This means that if we cache such an IV, we need to cache the
1542 NV as well. Moreover, we trade speed for space, and do not
1543 cache the NV if not needed.
1545 if (numtype & IS_NUMBER_NOT_IV) {
1546 /* May be not an integer. Need to cache NV if we cache IV
1547 * - otherwise future conversion to NV will be wrong. */
1550 d = Atof(SvPVX(sv));
1552 if (SvTYPE(sv) < SVt_PVNV)
1553 sv_upgrade(sv, SVt_PVNV);
1557 #if defined(USE_LONG_DOUBLE)
1558 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
1559 PTR2UV(sv), SvNVX(sv)));
1561 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%g)\n",
1562 PTR2UV(sv), SvNVX(sv)));
1564 if (SvNVX(sv) < (NV)IV_MAX + 0.5)
1565 SvIVX(sv) = I_V(SvNVX(sv));
1567 SvUVX(sv) = U_V(SvNVX(sv));
1572 else { /* The NV may be reconstructed from IV - safe to cache IV,
1573 which may be calculated by atol(). */
1574 if (SvTYPE(sv) < SVt_PVIV)
1575 sv_upgrade(sv, SVt_PVIV);
1577 SvIVX(sv) = Atol(SvPVX(sv));
1578 if (! numtype && ckWARN(WARN_NUMERIC))
1584 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1586 if (SvTYPE(sv) < SVt_IV)
1587 /* Typically the caller expects that sv_any is not NULL now. */
1588 sv_upgrade(sv, SVt_IV);
1591 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
1592 PTR2UV(sv),SvIVX(sv)));
1593 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
1597 Perl_sv_2uv(pTHX_ register SV *sv)
1601 if (SvGMAGICAL(sv)) {
1606 return U_V(SvNVX(sv));
1607 if (SvPOKp(sv) && SvLEN(sv))
1610 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1612 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1618 if (SvTHINKFIRST(sv)) {
1621 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv, numer)))
1622 return SvUV(tmpstr);
1623 return PTR2UV(SvRV(sv));
1625 if (SvREADONLY(sv) && !SvOK(sv)) {
1627 if (ckWARN(WARN_UNINITIALIZED))
1637 return (UV)SvIVX(sv);
1641 /* We can cache the IV/UV value even if it not good enough
1642 * to reconstruct NV, since the conversion to PV will prefer
1645 if (SvTYPE(sv) == SVt_NV)
1646 sv_upgrade(sv, SVt_PVNV);
1648 if (SvNVX(sv) >= -0.5) {
1650 SvUVX(sv) = U_V(SvNVX(sv));
1653 SvIVX(sv) = I_V(SvNVX(sv));
1655 DEBUG_c(PerlIO_printf(Perl_debug_log,
1656 "0x%"UVxf" 2uv(%"IVdf" => %"IVdf") (as signed)\n",
1659 (IV)(UV)SvIVX(sv)));
1660 return (UV)SvIVX(sv);
1663 else if (SvPOKp(sv) && SvLEN(sv)) {
1664 I32 numtype = looks_like_number(sv);
1666 /* We want to avoid a possible problem when we cache a UV which
1667 may be later translated to an NV, and the resulting NV is not
1668 the translation of the initial data.
1670 This means that if we cache such a UV, we need to cache the
1671 NV as well. Moreover, we trade speed for space, and do not
1672 cache the NV if not needed.
1674 if (numtype & IS_NUMBER_NOT_IV) {
1675 /* May be not an integer. Need to cache NV if we cache IV
1676 * - otherwise future conversion to NV will be wrong. */
1679 d = Atof(SvPVX(sv));
1681 if (SvTYPE(sv) < SVt_PVNV)
1682 sv_upgrade(sv, SVt_PVNV);
1686 #if defined(USE_LONG_DOUBLE)
1687 DEBUG_c(PerlIO_printf(Perl_debug_log,
1688 "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
1689 PTR2UV(sv), SvNVX(sv)));
1691 DEBUG_c(PerlIO_printf(Perl_debug_log,
1692 "0x%"UVxf" 2nv(%g)\n",
1693 PTR2UV(sv), SvNVX(sv)));
1695 if (SvNVX(sv) < -0.5) {
1696 SvIVX(sv) = I_V(SvNVX(sv));
1699 SvUVX(sv) = U_V(SvNVX(sv));
1703 else if (numtype & IS_NUMBER_NEG) {
1704 /* The NV may be reconstructed from IV - safe to cache IV,
1705 which may be calculated by atol(). */
1706 if (SvTYPE(sv) == SVt_PV)
1707 sv_upgrade(sv, SVt_PVIV);
1709 SvIVX(sv) = (IV)Atol(SvPVX(sv));
1711 else if (numtype) { /* Non-negative */
1712 /* The NV may be reconstructed from UV - safe to cache UV,
1713 which may be calculated by strtoul()/atol. */
1714 if (SvTYPE(sv) == SVt_PV)
1715 sv_upgrade(sv, SVt_PVIV);
1717 (void)SvIsUV_on(sv);
1719 SvUVX(sv) = Strtoul(SvPVX(sv), Null(char**), 10);
1720 #else /* no atou(), but we know the number fits into IV... */
1721 /* The only problem may be if it is negative... */
1722 SvUVX(sv) = (UV)Atol(SvPVX(sv));
1725 else { /* Not a number. Cache 0. */
1728 if (SvTYPE(sv) < SVt_PVIV)
1729 sv_upgrade(sv, SVt_PVIV);
1731 (void)SvIsUV_on(sv);
1732 SvUVX(sv) = 0; /* We assume that 0s have the
1733 same bitmap in IV and UV. */
1734 if (ckWARN(WARN_NUMERIC))
1739 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1741 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1744 if (SvTYPE(sv) < SVt_IV)
1745 /* Typically the caller expects that sv_any is not NULL now. */
1746 sv_upgrade(sv, SVt_IV);
1750 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
1751 PTR2UV(sv),SvUVX(sv)));
1752 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
1756 Perl_sv_2nv(pTHX_ register SV *sv)
1760 if (SvGMAGICAL(sv)) {
1764 if (SvPOKp(sv) && SvLEN(sv)) {
1766 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv))
1768 return Atof(SvPVX(sv));
1772 return (NV)SvUVX(sv);
1774 return (NV)SvIVX(sv);
1777 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1779 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1785 if (SvTHINKFIRST(sv)) {
1788 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)))
1789 return SvNV(tmpstr);
1790 return PTR2NV(SvRV(sv));
1792 if (SvREADONLY(sv) && !SvOK(sv)) {
1794 if (ckWARN(WARN_UNINITIALIZED))
1799 if (SvTYPE(sv) < SVt_NV) {
1800 if (SvTYPE(sv) == SVt_IV)
1801 sv_upgrade(sv, SVt_PVNV);
1803 sv_upgrade(sv, SVt_NV);
1804 #if defined(USE_LONG_DOUBLE)
1806 RESTORE_NUMERIC_STANDARD();
1807 PerlIO_printf(Perl_debug_log,
1808 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
1809 PTR2UV(sv), SvNVX(sv));
1810 RESTORE_NUMERIC_LOCAL();
1814 RESTORE_NUMERIC_STANDARD();
1815 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%g)\n",
1816 PTR2UV(sv), SvNVX(sv));
1817 RESTORE_NUMERIC_LOCAL();
1821 else if (SvTYPE(sv) < SVt_PVNV)
1822 sv_upgrade(sv, SVt_PVNV);
1824 (!SvPOKp(sv) || !strchr(SvPVX(sv),'.') || !looks_like_number(sv)))
1826 SvNVX(sv) = SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv);
1828 else if (SvPOKp(sv) && SvLEN(sv)) {
1830 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv))
1832 SvNVX(sv) = Atof(SvPVX(sv));
1836 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1838 if (SvTYPE(sv) < SVt_NV)
1839 /* Typically the caller expects that sv_any is not NULL now. */
1840 sv_upgrade(sv, SVt_NV);
1844 #if defined(USE_LONG_DOUBLE)
1846 RESTORE_NUMERIC_STANDARD();
1847 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
1848 PTR2UV(sv), SvNVX(sv));
1849 RESTORE_NUMERIC_LOCAL();
1853 RESTORE_NUMERIC_STANDARD();
1854 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%g)\n",
1855 PTR2UV(sv), SvNVX(sv));
1856 RESTORE_NUMERIC_LOCAL();
1863 S_asIV(pTHX_ SV *sv)
1865 I32 numtype = looks_like_number(sv);
1868 if (numtype & IS_NUMBER_TO_INT_BY_ATOL)
1869 return Atol(SvPVX(sv));
1872 if (ckWARN(WARN_NUMERIC))
1875 d = Atof(SvPVX(sv));
1880 S_asUV(pTHX_ SV *sv)
1882 I32 numtype = looks_like_number(sv);
1885 if (numtype & IS_NUMBER_TO_INT_BY_ATOL)
1886 return Strtoul(SvPVX(sv), Null(char**), 10);
1890 if (ckWARN(WARN_NUMERIC))
1893 return U_V(Atof(SvPVX(sv)));
1897 * Returns a combination of (advisory only - can get false negatives)
1898 * IS_NUMBER_TO_INT_BY_ATOL, IS_NUMBER_TO_INT_BY_ATOF, IS_NUMBER_NOT_IV,
1900 * 0 if does not look like number.
1902 * In fact possible values are 0 and
1903 * IS_NUMBER_TO_INT_BY_ATOL 123
1904 * IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_NOT_IV 123.1
1905 * IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_IV 123e0
1906 * IS_NUMBER_INFINITY
1907 * with a possible addition of IS_NUMBER_NEG.
1911 =for apidoc looks_like_number
1913 Test if an the content of an SV looks like a number (or is a
1920 Perl_looks_like_number(pTHX_ SV *sv)
1923 register char *send;
1924 register char *sbegin;
1925 register char *nbegin;
1934 else if (SvPOKp(sv))
1935 sbegin = SvPV(sv, len);
1938 send = sbegin + len;
1945 numtype = IS_NUMBER_NEG;
1952 * we return IS_NUMBER_TO_INT_BY_ATOL if the number can be converted
1953 * to _integer_ with atol() and IS_NUMBER_TO_INT_BY_ATOF if you need
1957 /* next must be digit or the radix separator or beginning of infinity */
1961 } while (isDIGIT(*s));
1963 if (s - nbegin >= TYPE_DIGITS(IV)) /* Cannot cache ato[ul]() */
1964 numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_IV;
1966 numtype |= IS_NUMBER_TO_INT_BY_ATOL;
1969 #ifdef USE_LOCALE_NUMERIC
1970 || IS_NUMERIC_RADIX(*s)
1974 numtype |= IS_NUMBER_NOT_IV;
1975 while (isDIGIT(*s)) /* optional digits after the radix */
1980 #ifdef USE_LOCALE_NUMERIC
1981 || IS_NUMERIC_RADIX(*s)
1985 numtype |= IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_NOT_IV;
1986 /* no digits before the radix means we need digits after it */
1990 } while (isDIGIT(*s));
1995 else if (*s == 'I' || *s == 'i') {
1996 s++; if (*s != 'N' && *s != 'n') return 0;
1997 s++; if (*s != 'F' && *s != 'f') return 0;
1998 s++; if (*s == 'I' || *s == 'i') {
1999 s++; if (*s != 'N' && *s != 'n') return 0;
2000 s++; if (*s != 'I' && *s != 'i') return 0;
2001 s++; if (*s != 'T' && *s != 't') return 0;
2002 s++; if (*s != 'Y' && *s != 'y') return 0;
2010 numtype = IS_NUMBER_INFINITY;
2012 /* we can have an optional exponent part */
2013 if (*s == 'e' || *s == 'E') {
2014 numtype &= ~IS_NUMBER_NEG;
2015 numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_IV;
2017 if (*s == '+' || *s == '-')
2022 } while (isDIGIT(*s));
2032 if (len == 10 && memEQ(sbegin, "0 but true", 10))
2033 return IS_NUMBER_TO_INT_BY_ATOL;
2038 Perl_sv_2pv_nolen(pTHX_ register SV *sv)
2041 return sv_2pv(sv, &n_a);
2044 /* We assume that buf is at least TYPE_CHARS(UV) long. */
2046 uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
2048 char *ptr = buf + TYPE_CHARS(UV);
2062 *--ptr = '0' + (uv % 10);
2071 Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
2076 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
2077 char *tmpbuf = tbuf;
2083 if (SvGMAGICAL(sv)) {
2091 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
2093 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
2098 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
2103 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2105 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2112 if (SvTHINKFIRST(sv)) {
2115 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)))
2116 return SvPV(tmpstr,*lp);
2123 switch (SvTYPE(sv)) {
2125 if ( ((SvFLAGS(sv) &
2126 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
2127 == (SVs_OBJECT|SVs_RMG))
2128 && strEQ(s=HvNAME(SvSTASH(sv)), "Regexp")
2129 && (mg = mg_find(sv, 'r'))) {
2131 regexp *re = (regexp *)mg->mg_obj;
2134 char *fptr = "msix";
2139 U16 reganch = (re->reganch & PMf_COMPILETIME) >> 12;
2141 while((ch = *fptr++)) {
2143 reflags[left++] = ch;
2146 reflags[right--] = ch;
2151 reflags[left] = '-';
2155 mg->mg_len = re->prelen + 4 + left;
2156 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
2157 Copy("(?", mg->mg_ptr, 2, char);
2158 Copy(reflags, mg->mg_ptr+2, left, char);
2159 Copy(":", mg->mg_ptr+left+2, 1, char);
2160 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
2161 mg->mg_ptr[mg->mg_len - 1] = ')';
2162 mg->mg_ptr[mg->mg_len] = 0;
2164 PL_reginterp_cnt += re->program[0].next_off;
2176 case SVt_PVBM: if (SvROK(sv))
2179 s = "SCALAR"; break;
2180 case SVt_PVLV: s = "LVALUE"; break;
2181 case SVt_PVAV: s = "ARRAY"; break;
2182 case SVt_PVHV: s = "HASH"; break;
2183 case SVt_PVCV: s = "CODE"; break;
2184 case SVt_PVGV: s = "GLOB"; break;
2185 case SVt_PVFM: s = "FORMAT"; break;
2186 case SVt_PVIO: s = "IO"; break;
2187 default: s = "UNKNOWN"; break;
2191 Perl_sv_setpvf(aTHX_ tsv, "%s=%s", HvNAME(SvSTASH(sv)), s);
2194 Perl_sv_catpvf(aTHX_ tsv, "(0x%"UVxf")", PTR2UV(sv));
2200 if (SvREADONLY(sv) && !SvOK(sv)) {
2202 if (ckWARN(WARN_UNINITIALIZED))
2208 if (SvNOKp(sv)) { /* See note in sv_2uv() */
2209 /* XXXX 64-bit? IV may have better precision... */
2210 /* I tried changing this for to be 64-bit-aware and
2211 * the t/op/numconvert.t became very, very, angry.
2213 if (SvTYPE(sv) < SVt_PVNV)
2214 sv_upgrade(sv, SVt_PVNV);
2217 olderrno = errno; /* some Xenix systems wipe out errno here */
2219 if (SvNVX(sv) == 0.0)
2220 (void)strcpy(s,"0");
2224 Gconvert(SvNVX(sv), NV_DIG, 0, s);
2227 #ifdef FIXNEGATIVEZERO
2228 if (*s == '-' && s[1] == '0' && !s[2])
2237 else if (SvIOKp(sv)) {
2238 U32 isIOK = SvIOK(sv);
2239 U32 isUIOK = SvIsUV(sv);
2240 char buf[TYPE_CHARS(UV)];
2243 if (SvTYPE(sv) < SVt_PVIV)
2244 sv_upgrade(sv, SVt_PVIV);
2246 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
2248 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
2249 SvGROW(sv, ebuf - ptr + 1); /* inlined from sv_setpvn */
2250 Move(ptr,SvPVX(sv),ebuf - ptr,char);
2251 SvCUR_set(sv, ebuf - ptr);
2264 if (ckWARN(WARN_UNINITIALIZED)
2265 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
2270 if (SvTYPE(sv) < SVt_PV)
2271 /* Typically the caller expects that sv_any is not NULL now. */
2272 sv_upgrade(sv, SVt_PV);
2275 *lp = s - SvPVX(sv);
2278 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
2279 PTR2UV(sv),SvPVX(sv)));
2283 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
2284 /* Sneaky stuff here */
2288 tsv = newSVpv(tmpbuf, 0);
2304 len = strlen(tmpbuf);
2306 #ifdef FIXNEGATIVEZERO
2307 if (len == 2 && t[0] == '-' && t[1] == '0') {
2312 (void)SvUPGRADE(sv, SVt_PV);
2314 s = SvGROW(sv, len + 1);
2323 Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
2326 return sv_2pvbyte(sv, &n_a);
2330 Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
2332 return sv_2pv(sv,lp);
2336 Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
2339 return sv_2pvutf8(sv, &n_a);
2343 Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
2345 sv_utf8_upgrade(sv);
2346 return sv_2pv(sv,lp);
2349 /* This function is only called on magical items */
2351 Perl_sv_2bool(pTHX_ register SV *sv)
2361 if (SvAMAGIC(sv) && (tmpsv = AMG_CALLun(sv,bool_)))
2362 return SvTRUE(tmpsv);
2363 return SvRV(sv) != 0;
2366 register XPV* Xpvtmp;
2367 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
2368 (*Xpvtmp->xpv_pv > '0' ||
2369 Xpvtmp->xpv_cur > 1 ||
2370 (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0')))
2377 return SvIVX(sv) != 0;
2380 return SvNVX(sv) != 0.0;
2388 =for apidoc sv_utf8_upgrade
2390 Convert the PV of an SV to its UTF8-encoded form.
2396 Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
2401 if (!sv || !SvPOK(sv) || SvUTF8(sv))
2404 /* This function could be much more efficient if we had a FLAG
2405 * to signal if there are any hibit chars in the string
2408 for (c = SvPVX(sv); c < SvEND(sv); c++) {
2415 SvGROW(sv, SvCUR(sv) + hicount + 1);
2417 src = SvEND(sv) - 1;
2418 SvCUR_set(sv, SvCUR(sv) + hicount);
2419 dst = SvEND(sv) - 1;
2424 uv_to_utf8((U8*)dst, (U8)*src--);
2437 =for apidoc sv_utf8_downgrade
2439 Attempt to convert the PV of an SV from UTF8-encoded to byte encoding.
2440 This may not be possible if the PV contains non-byte encoding characters;
2441 if this is the case, either returns false or, if C<fail_ok> is not
2448 Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
2450 if (SvPOK(sv) && SvUTF8(sv)) {
2451 char *c = SvPVX(sv);
2453 /* need to figure out if this is possible at all first */
2454 while (c < SvEND(sv)) {
2457 UV uv = utf8_to_uv((U8*)c, &len);
2462 /* XXX might want to make a callback here instead */
2463 Perl_croak(aTHX_ "Big byte");
2476 char *src = first_hi;
2477 char *dst = first_hi;
2478 while (src < SvEND(sv)) {
2481 U8 u = (U8)utf8_to_uv((U8*)src, &len);
2489 SvCUR_set(sv, dst - SvPVX(sv));
2497 =for apidoc sv_utf8_encode
2499 Convert the PV of an SV to UTF8-encoded, but then turn off the C<SvUTF8>
2500 flag so that it looks like bytes again. Nothing calls this.
2506 Perl_sv_utf8_encode(pTHX_ register SV *sv)
2508 sv_utf8_upgrade(sv);
2513 Perl_sv_utf8_decode(pTHX_ register SV *sv)
2517 bool has_utf = FALSE;
2518 if (!sv_utf8_downgrade(sv, TRUE))
2521 /* it is actually just a matter of turning the utf8 flag on, but
2522 * we want to make sure everything inside is valid utf8 first.
2525 while (c < SvEND(sv)) {
2528 (void)utf8_to_uv((U8*)c, &len);
2548 /* Note: sv_setsv() should not be called with a source string that needs
2549 * to be reused, since it may destroy the source string if it is marked
2554 =for apidoc sv_setsv
2556 Copies the contents of the source SV C<ssv> into the destination SV C<dsv>.
2557 The source SV may be destroyed if it is mortal. Does not handle 'set'
2558 magic. See the macro forms C<SvSetSV>, C<SvSetSV_nosteal> and
2565 Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
2568 register U32 sflags;
2574 SV_CHECK_THINKFIRST(dstr);
2576 sstr = &PL_sv_undef;
2577 stype = SvTYPE(sstr);
2578 dtype = SvTYPE(dstr);
2582 /* There's a lot of redundancy below but we're going for speed here */
2587 if (dtype != SVt_PVGV) {
2588 (void)SvOK_off(dstr);
2596 sv_upgrade(dstr, SVt_IV);
2599 sv_upgrade(dstr, SVt_PVNV);
2603 sv_upgrade(dstr, SVt_PVIV);
2606 (void)SvIOK_only(dstr);
2607 SvIVX(dstr) = SvIVX(sstr);
2620 sv_upgrade(dstr, SVt_NV);
2625 sv_upgrade(dstr, SVt_PVNV);
2628 SvNVX(dstr) = SvNVX(sstr);
2629 (void)SvNOK_only(dstr);
2637 sv_upgrade(dstr, SVt_RV);
2638 else if (dtype == SVt_PVGV &&
2639 SvTYPE(SvRV(sstr)) == SVt_PVGV) {
2642 if (GvIMPORTED(dstr) != GVf_IMPORTED
2643 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
2645 GvIMPORTED_on(dstr);
2656 sv_upgrade(dstr, SVt_PV);
2659 if (dtype < SVt_PVIV)
2660 sv_upgrade(dstr, SVt_PVIV);
2663 if (dtype < SVt_PVNV)
2664 sv_upgrade(dstr, SVt_PVNV);
2671 Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0),
2672 PL_op_name[PL_op->op_type]);
2674 Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0));
2678 if (dtype <= SVt_PVGV) {
2680 if (dtype != SVt_PVGV) {
2681 char *name = GvNAME(sstr);
2682 STRLEN len = GvNAMELEN(sstr);
2683 sv_upgrade(dstr, SVt_PVGV);
2684 sv_magic(dstr, dstr, '*', Nullch, 0);
2685 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
2686 GvNAME(dstr) = savepvn(name, len);
2687 GvNAMELEN(dstr) = len;
2688 SvFAKE_on(dstr); /* can coerce to non-glob */
2690 /* ahem, death to those who redefine active sort subs */
2691 else if (PL_curstackinfo->si_type == PERLSI_SORT
2692 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
2693 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
2695 (void)SvOK_off(dstr);
2696 GvINTRO_off(dstr); /* one-shot flag */
2698 GvGP(dstr) = gp_ref(GvGP(sstr));
2700 if (GvIMPORTED(dstr) != GVf_IMPORTED
2701 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
2703 GvIMPORTED_on(dstr);
2711 if (SvGMAGICAL(sstr)) {
2713 if (SvTYPE(sstr) != stype) {
2714 stype = SvTYPE(sstr);
2715 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
2719 if (stype == SVt_PVLV)
2720 (void)SvUPGRADE(dstr, SVt_PVNV);
2722 (void)SvUPGRADE(dstr, stype);
2725 sflags = SvFLAGS(sstr);
2727 if (sflags & SVf_ROK) {
2728 if (dtype >= SVt_PV) {
2729 if (dtype == SVt_PVGV) {
2730 SV *sref = SvREFCNT_inc(SvRV(sstr));
2732 int intro = GvINTRO(dstr);
2737 GvINTRO_off(dstr); /* one-shot flag */
2738 Newz(602,gp, 1, GP);
2739 GvGP(dstr) = gp_ref(gp);
2740 GvSV(dstr) = NEWSV(72,0);
2741 GvLINE(dstr) = CopLINE(PL_curcop);
2742 GvEGV(dstr) = (GV*)dstr;
2745 switch (SvTYPE(sref)) {
2748 SAVESPTR(GvAV(dstr));
2750 dref = (SV*)GvAV(dstr);
2751 GvAV(dstr) = (AV*)sref;
2752 if (!GvIMPORTED_AV(dstr)
2753 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
2755 GvIMPORTED_AV_on(dstr);
2760 SAVESPTR(GvHV(dstr));
2762 dref = (SV*)GvHV(dstr);
2763 GvHV(dstr) = (HV*)sref;
2764 if (!GvIMPORTED_HV(dstr)
2765 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
2767 GvIMPORTED_HV_on(dstr);
2772 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
2773 SvREFCNT_dec(GvCV(dstr));
2774 GvCV(dstr) = Nullcv;
2775 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
2776 PL_sub_generation++;
2778 SAVESPTR(GvCV(dstr));
2781 dref = (SV*)GvCV(dstr);
2782 if (GvCV(dstr) != (CV*)sref) {
2783 CV* cv = GvCV(dstr);
2785 if (!GvCVGEN((GV*)dstr) &&
2786 (CvROOT(cv) || CvXSUB(cv)))
2788 SV *const_sv = cv_const_sv(cv);
2789 bool const_changed = TRUE;
2791 const_changed = sv_cmp(const_sv,
2792 op_const_sv(CvSTART((CV*)sref),
2794 /* ahem, death to those who redefine
2795 * active sort subs */
2796 if (PL_curstackinfo->si_type == PERLSI_SORT &&
2797 PL_sortcop == CvSTART(cv))
2799 "Can't redefine active sort subroutine %s",
2800 GvENAME((GV*)dstr));
2801 if ((const_changed && const_sv) || ckWARN(WARN_REDEFINE))
2802 Perl_warner(aTHX_ WARN_REDEFINE, const_sv ?
2803 "Constant subroutine %s redefined"
2804 : "Subroutine %s redefined",
2805 GvENAME((GV*)dstr));
2807 cv_ckproto(cv, (GV*)dstr,
2808 SvPOK(sref) ? SvPVX(sref) : Nullch);
2810 GvCV(dstr) = (CV*)sref;
2811 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
2812 GvASSUMECV_on(dstr);
2813 PL_sub_generation++;
2815 if (!GvIMPORTED_CV(dstr)
2816 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
2818 GvIMPORTED_CV_on(dstr);
2823 SAVESPTR(GvIOp(dstr));
2825 dref = (SV*)GvIOp(dstr);
2826 GvIOp(dstr) = (IO*)sref;
2830 SAVESPTR(GvFORM(dstr));
2832 dref = (SV*)GvFORM(dstr);
2833 GvFORM(dstr) = (CV*)sref;
2837 SAVESPTR(GvSV(dstr));
2839 dref = (SV*)GvSV(dstr);
2841 if (!GvIMPORTED_SV(dstr)
2842 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
2844 GvIMPORTED_SV_on(dstr);
2856 (void)SvOOK_off(dstr); /* backoff */
2858 Safefree(SvPVX(dstr));
2859 SvLEN(dstr)=SvCUR(dstr)=0;
2862 (void)SvOK_off(dstr);
2863 SvRV(dstr) = SvREFCNT_inc(SvRV(sstr));
2865 if (sflags & SVp_NOK) {
2867 SvNVX(dstr) = SvNVX(sstr);
2869 if (sflags & SVp_IOK) {
2870 (void)SvIOK_on(dstr);
2871 SvIVX(dstr) = SvIVX(sstr);
2872 if (sflags & SVf_IVisUV)
2875 if (SvAMAGIC(sstr)) {
2879 else if (sflags & SVp_POK) {
2882 * Check to see if we can just swipe the string. If so, it's a
2883 * possible small lose on short strings, but a big win on long ones.
2884 * It might even be a win on short strings if SvPVX(dstr)
2885 * has to be allocated and SvPVX(sstr) has to be freed.
2888 if (SvTEMP(sstr) && /* slated for free anyway? */
2889 SvREFCNT(sstr) == 1 && /* and no other references to it? */
2890 !(sflags & SVf_OOK)) /* and not involved in OOK hack? */
2892 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
2894 SvFLAGS(dstr) &= ~SVf_OOK;
2895 Safefree(SvPVX(dstr) - SvIVX(dstr));
2897 else if (SvLEN(dstr))
2898 Safefree(SvPVX(dstr));
2900 (void)SvPOK_only(dstr);
2901 SvPV_set(dstr, SvPVX(sstr));
2902 SvLEN_set(dstr, SvLEN(sstr));
2903 SvCUR_set(dstr, SvCUR(sstr));
2906 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
2907 SvPV_set(sstr, Nullch);
2912 else { /* have to copy actual string */
2913 STRLEN len = SvCUR(sstr);
2915 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
2916 Move(SvPVX(sstr),SvPVX(dstr),len,char);
2917 SvCUR_set(dstr, len);
2918 *SvEND(dstr) = '\0';
2919 (void)SvPOK_only(dstr);
2921 if ((sflags & SVf_UTF8) && !IN_BYTE)
2924 if (sflags & SVp_NOK) {
2926 SvNVX(dstr) = SvNVX(sstr);
2928 if (sflags & SVp_IOK) {
2929 (void)SvIOK_on(dstr);
2930 SvIVX(dstr) = SvIVX(sstr);
2931 if (sflags & SVf_IVisUV)
2935 else if (sflags & SVp_NOK) {
2936 SvNVX(dstr) = SvNVX(sstr);
2937 (void)SvNOK_only(dstr);
2938 if (sflags & SVf_IOK) {
2939 (void)SvIOK_on(dstr);
2940 SvIVX(dstr) = SvIVX(sstr);
2941 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
2942 if (sflags & SVf_IVisUV)
2946 else if (sflags & SVp_IOK) {
2947 (void)SvIOK_only(dstr);
2948 SvIVX(dstr) = SvIVX(sstr);
2949 if (sflags & SVf_IVisUV)
2953 if (dtype == SVt_PVGV) {
2954 if (ckWARN(WARN_MISC))
2955 Perl_warner(aTHX_ WARN_MISC, "Undefined value assigned to typeglob");
2958 (void)SvOK_off(dstr);
2964 =for apidoc sv_setsv_mg
2966 Like C<sv_setsv>, but also handles 'set' magic.
2972 Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
2974 sv_setsv(dstr,sstr);
2979 =for apidoc sv_setpvn
2981 Copies a string into an SV. The C<len> parameter indicates the number of
2982 bytes to be copied. Does not handle 'set' magic. See C<sv_setpvn_mg>.
2988 Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
2990 register char *dptr;
2991 assert(len >= 0); /* STRLEN is probably unsigned, so this may
2992 elicit a warning, but it won't hurt. */
2993 SV_CHECK_THINKFIRST(sv);
2998 (void)SvUPGRADE(sv, SVt_PV);
3000 SvGROW(sv, len + 1);
3002 Move(ptr,dptr,len,char);
3005 (void)SvPOK_only(sv); /* validate pointer */
3010 =for apidoc sv_setpvn_mg
3012 Like C<sv_setpvn>, but also handles 'set' magic.
3018 Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
3020 sv_setpvn(sv,ptr,len);
3025 =for apidoc sv_setpv
3027 Copies a string into an SV. The string must be null-terminated. Does not
3028 handle 'set' magic. See C<sv_setpv_mg>.
3034 Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
3036 register STRLEN len;
3038 SV_CHECK_THINKFIRST(sv);
3044 (void)SvUPGRADE(sv, SVt_PV);
3046 SvGROW(sv, len + 1);
3047 Move(ptr,SvPVX(sv),len+1,char);
3049 (void)SvPOK_only(sv); /* validate pointer */
3054 =for apidoc sv_setpv_mg
3056 Like C<sv_setpv>, but also handles 'set' magic.
3062 Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
3069 =for apidoc sv_usepvn
3071 Tells an SV to use C<ptr> to find its string value. Normally the string is
3072 stored inside the SV but sv_usepvn allows the SV to use an outside string.
3073 The C<ptr> should point to memory that was allocated by C<malloc>. The
3074 string length, C<len>, must be supplied. This function will realloc the
3075 memory pointed to by C<ptr>, so that pointer should not be freed or used by
3076 the programmer after giving it to sv_usepvn. Does not handle 'set' magic.
3077 See C<sv_usepvn_mg>.
3083 Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
3085 SV_CHECK_THINKFIRST(sv);
3086 (void)SvUPGRADE(sv, SVt_PV);
3091 (void)SvOOK_off(sv);
3092 if (SvPVX(sv) && SvLEN(sv))
3093 Safefree(SvPVX(sv));
3094 Renew(ptr, len+1, char);
3097 SvLEN_set(sv, len+1);
3099 (void)SvPOK_only(sv); /* validate pointer */
3104 =for apidoc sv_usepvn_mg
3106 Like C<sv_usepvn>, but also handles 'set' magic.
3112 Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
3114 sv_usepvn(sv,ptr,len);
3119 Perl_sv_force_normal(pTHX_ register SV *sv)
3121 if (SvREADONLY(sv)) {
3123 if (PL_curcop != &PL_compiling)
3124 Perl_croak(aTHX_ PL_no_modify);
3128 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
3135 Efficient removal of characters from the beginning of the string buffer.
3136 SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside
3137 the string buffer. The C<ptr> becomes the first character of the adjusted
3144 Perl_sv_chop(pTHX_ register SV *sv, register char *ptr) /* like set but assuming ptr is in sv */
3148 register STRLEN delta;
3150 if (!ptr || !SvPOKp(sv))
3152 SV_CHECK_THINKFIRST(sv);
3153 if (SvTYPE(sv) < SVt_PVIV)
3154 sv_upgrade(sv,SVt_PVIV);
3157 if (!SvLEN(sv)) { /* make copy of shared string */
3158 char *pvx = SvPVX(sv);
3159 STRLEN len = SvCUR(sv);
3160 SvGROW(sv, len + 1);
3161 Move(pvx,SvPVX(sv),len,char);
3165 SvFLAGS(sv) |= SVf_OOK;
3167 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK|SVf_IVisUV);
3168 delta = ptr - SvPVX(sv);
3176 =for apidoc sv_catpvn
3178 Concatenates the string onto the end of the string which is in the SV. The
3179 C<len> indicates number of bytes to copy. Handles 'get' magic, but not
3180 'set' magic. See C<sv_catpvn_mg>.
3186 Perl_sv_catpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
3191 junk = SvPV_force(sv, tlen);
3192 SvGROW(sv, tlen + len + 1);
3195 Move(ptr,SvPVX(sv)+tlen,len,char);
3198 (void)SvPOK_only_UTF8(sv); /* validate pointer */
3203 =for apidoc sv_catpvn_mg
3205 Like C<sv_catpvn>, but also handles 'set' magic.
3211 Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
3213 sv_catpvn(sv,ptr,len);
3218 =for apidoc sv_catsv
3220 Concatenates the string from SV C<ssv> onto the end of the string in SV
3221 C<dsv>. Handles 'get' magic, but not 'set' magic. See C<sv_catsv_mg>.
3227 Perl_sv_catsv(pTHX_ SV *dstr, register SV *sstr)
3233 if ((s = SvPV(sstr, len))) {
3234 if (DO_UTF8(sstr)) {
3235 sv_utf8_upgrade(dstr);
3236 sv_catpvn(dstr,s,len);
3240 sv_catpvn(dstr,s,len);
3245 =for apidoc sv_catsv_mg
3247 Like C<sv_catsv>, but also handles 'set' magic.
3253 Perl_sv_catsv_mg(pTHX_ SV *dstr, register SV *sstr)
3255 sv_catsv(dstr,sstr);
3260 =for apidoc sv_catpv
3262 Concatenates the string onto the end of the string which is in the SV.
3263 Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>.
3269 Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr)
3271 register STRLEN len;
3277 junk = SvPV_force(sv, tlen);
3279 SvGROW(sv, tlen + len + 1);
3282 Move(ptr,SvPVX(sv)+tlen,len+1,char);
3284 (void)SvPOK_only_UTF8(sv); /* validate pointer */
3289 =for apidoc sv_catpv_mg
3291 Like C<sv_catpv>, but also handles 'set' magic.
3297 Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr)
3304 Perl_newSV(pTHX_ STRLEN len)
3310 sv_upgrade(sv, SVt_PV);
3311 SvGROW(sv, len + 1);
3316 /* name is assumed to contain an SV* if (name && namelen == HEf_SVKEY) */
3319 =for apidoc sv_magic
3321 Adds magic to an SV.
3327 Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen)
3331 if (SvREADONLY(sv)) {
3333 if (PL_curcop != &PL_compiling && !strchr("gBf", how))
3334 Perl_croak(aTHX_ PL_no_modify);
3336 if (SvMAGICAL(sv) || (how == 't' && SvTYPE(sv) >= SVt_PVMG)) {
3337 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
3344 (void)SvUPGRADE(sv, SVt_PVMG);
3346 Newz(702,mg, 1, MAGIC);
3347 mg->mg_moremagic = SvMAGIC(sv);
3350 if (!obj || obj == sv || how == '#' || how == 'r')
3354 mg->mg_obj = SvREFCNT_inc(obj);
3355 mg->mg_flags |= MGf_REFCOUNTED;
3358 mg->mg_len = namlen;
3361 mg->mg_ptr = savepvn(name, namlen);
3362 else if (namlen == HEf_SVKEY)
3363 mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
3367 mg->mg_virtual = &PL_vtbl_sv;
3370 mg->mg_virtual = &PL_vtbl_amagic;
3373 mg->mg_virtual = &PL_vtbl_amagicelem;
3379 mg->mg_virtual = &PL_vtbl_bm;
3382 mg->mg_virtual = &PL_vtbl_regdata;
3385 mg->mg_virtual = &PL_vtbl_regdatum;
3388 mg->mg_virtual = &PL_vtbl_env;
3391 mg->mg_virtual = &PL_vtbl_fm;
3394 mg->mg_virtual = &PL_vtbl_envelem;
3397 mg->mg_virtual = &PL_vtbl_mglob;
3400 mg->mg_virtual = &PL_vtbl_isa;
3403 mg->mg_virtual = &PL_vtbl_isaelem;
3406 mg->mg_virtual = &PL_vtbl_nkeys;
3413 mg->mg_virtual = &PL_vtbl_dbline;
3417 mg->mg_virtual = &PL_vtbl_mutex;
3419 #endif /* USE_THREADS */
3420 #ifdef USE_LOCALE_COLLATE
3422 mg->mg_virtual = &PL_vtbl_collxfrm;
3424 #endif /* USE_LOCALE_COLLATE */
3426 mg->mg_virtual = &PL_vtbl_pack;
3430 mg->mg_virtual = &PL_vtbl_packelem;
3433 mg->mg_virtual = &PL_vtbl_regexp;
3436 mg->mg_virtual = &PL_vtbl_sig;
3439 mg->mg_virtual = &PL_vtbl_sigelem;
3442 mg->mg_virtual = &PL_vtbl_taint;
3446 mg->mg_virtual = &PL_vtbl_uvar;
3449 mg->mg_virtual = &PL_vtbl_vec;
3452 mg->mg_virtual = &PL_vtbl_substr;
3455 mg->mg_virtual = &PL_vtbl_defelem;
3458 mg->mg_virtual = &PL_vtbl_glob;
3461 mg->mg_virtual = &PL_vtbl_arylen;
3464 mg->mg_virtual = &PL_vtbl_pos;
3467 mg->mg_virtual = &PL_vtbl_backref;
3469 case '~': /* Reserved for use by extensions not perl internals. */
3470 /* Useful for attaching extension internal data to perl vars. */
3471 /* Note that multiple extensions may clash if magical scalars */
3472 /* etc holding private data from one are passed to another. */
3476 Perl_croak(aTHX_ "Don't know how to handle magic of type '%c'", how);
3480 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
3484 =for apidoc sv_unmagic
3486 Removes magic from an SV.
3492 Perl_sv_unmagic(pTHX_ SV *sv, int type)
3496 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
3499 for (mg = *mgp; mg; mg = *mgp) {
3500 if (mg->mg_type == type) {
3501 MGVTBL* vtbl = mg->mg_virtual;
3502 *mgp = mg->mg_moremagic;
3503 if (vtbl && vtbl->svt_free)
3504 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
3505 if (mg->mg_ptr && mg->mg_type != 'g')
3506 if (mg->mg_len >= 0)
3507 Safefree(mg->mg_ptr);
3508 else if (mg->mg_len == HEf_SVKEY)
3509 SvREFCNT_dec((SV*)mg->mg_ptr);
3510 if (mg->mg_flags & MGf_REFCOUNTED)
3511 SvREFCNT_dec(mg->mg_obj);
3515 mgp = &mg->mg_moremagic;
3519 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
3526 =for apidoc sv_rvweaken
3534 Perl_sv_rvweaken(pTHX_ SV *sv)
3537 if (!SvOK(sv)) /* let undefs pass */
3540 Perl_croak(aTHX_ "Can't weaken a nonreference");
3541 else if (SvWEAKREF(sv)) {
3543 if (ckWARN(WARN_MISC))
3544 Perl_warner(aTHX_ WARN_MISC, "Reference is already weak");
3548 sv_add_backref(tsv, sv);
3555 S_sv_add_backref(pTHX_ SV *tsv, SV *sv)
3559 if (SvMAGICAL(tsv) && (mg = mg_find(tsv, '<')))
3560 av = (AV*)mg->mg_obj;
3563 sv_magic(tsv, (SV*)av, '<', NULL, 0);
3564 SvREFCNT_dec(av); /* for sv_magic */
3570 S_sv_del_backref(pTHX_ SV *sv)
3577 if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, '<')))
3578 Perl_croak(aTHX_ "panic: del_backref");
3579 av = (AV *)mg->mg_obj;
3584 svp[i] = &PL_sv_undef; /* XXX */
3591 =for apidoc sv_insert
3593 Inserts a string at the specified offset/length within the SV. Similar to
3594 the Perl substr() function.
3600 Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, char *little, STRLEN littlelen)
3604 register char *midend;
3605 register char *bigend;
3611 Perl_croak(aTHX_ "Can't modify non-existent substring");
3612 SvPV_force(bigstr, curlen);
3613 (void)SvPOK_only_UTF8(bigstr);
3614 if (offset + len > curlen) {
3615 SvGROW(bigstr, offset+len+1);
3616 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
3617 SvCUR_set(bigstr, offset+len);
3621 i = littlelen - len;
3622 if (i > 0) { /* string might grow */
3623 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
3624 mid = big + offset + len;
3625 midend = bigend = big + SvCUR(bigstr);
3628 while (midend > mid) /* shove everything down */
3629 *--bigend = *--midend;
3630 Move(little,big+offset,littlelen,char);
3636 Move(little,SvPVX(bigstr)+offset,len,char);
3641 big = SvPVX(bigstr);
3644 bigend = big + SvCUR(bigstr);
3646 if (midend > bigend)
3647 Perl_croak(aTHX_ "panic: sv_insert");
3649 if (mid - big > bigend - midend) { /* faster to shorten from end */
3651 Move(little, mid, littlelen,char);
3654 i = bigend - midend;
3656 Move(midend, mid, i,char);
3660 SvCUR_set(bigstr, mid - big);
3663 else if ((i = mid - big)) { /* faster from front */
3664 midend -= littlelen;
3666 sv_chop(bigstr,midend-i);
3671 Move(little, mid, littlelen,char);
3673 else if (littlelen) {
3674 midend -= littlelen;
3675 sv_chop(bigstr,midend);
3676 Move(little,midend,littlelen,char);
3679 sv_chop(bigstr,midend);
3685 =for apidoc sv_replace
3687 Make the first argument a copy of the second, then delete the original.
3693 Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv)
3696 U32 refcnt = SvREFCNT(sv);
3697 SV_CHECK_THINKFIRST(sv);
3698 if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL))
3699 Perl_warner(aTHX_ WARN_INTERNAL, "Reference miscount in sv_replace()");
3700 if (SvMAGICAL(sv)) {
3704 sv_upgrade(nsv, SVt_PVMG);
3705 SvMAGIC(nsv) = SvMAGIC(sv);
3706 SvFLAGS(nsv) |= SvMAGICAL(sv);
3712 assert(!SvREFCNT(sv));
3713 StructCopy(nsv,sv,SV);
3714 SvREFCNT(sv) = refcnt;
3715 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
3720 =for apidoc sv_clear
3722 Clear an SV, making it empty. Does not free the memory used by the SV
3729 Perl_sv_clear(pTHX_ register SV *sv)
3733 assert(SvREFCNT(sv) == 0);
3737 if (PL_defstash) { /* Still have a symbol table? */
3742 Zero(&tmpref, 1, SV);
3743 sv_upgrade(&tmpref, SVt_RV);
3745 SvREADONLY_on(&tmpref); /* DESTROY() could be naughty */
3746 SvREFCNT(&tmpref) = 1;
3749 stash = SvSTASH(sv);
3750 destructor = gv_fetchmethod(SvSTASH(sv), "DESTROY");
3753 PUSHSTACKi(PERLSI_DESTROY);
3754 SvRV(&tmpref) = SvREFCNT_inc(sv);
3759 call_sv((SV*)GvCV(destructor),
3760 G_DISCARD|G_EVAL|G_KEEPERR);
3766 } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
3768 del_XRV(SvANY(&tmpref));
3771 if (PL_in_clean_objs)
3772 Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'",
3774 /* DESTROY gave object new lease on life */
3780 SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */
3781 SvOBJECT_off(sv); /* Curse the object. */
3782 if (SvTYPE(sv) != SVt_PVIO)
3783 --PL_sv_objcount; /* XXX Might want something more general */
3786 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
3789 switch (SvTYPE(sv)) {
3792 IoIFP(sv) != PerlIO_stdin() &&
3793 IoIFP(sv) != PerlIO_stdout() &&
3794 IoIFP(sv) != PerlIO_stderr())
3796 io_close((IO*)sv, FALSE);
3798 if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP))
3799 PerlDir_close(IoDIRP(sv));
3800 IoDIRP(sv) = (DIR*)NULL;
3801 Safefree(IoTOP_NAME(sv));
3802 Safefree(IoFMT_NAME(sv));
3803 Safefree(IoBOTTOM_NAME(sv));
3818 SvREFCNT_dec(LvTARG(sv));
3822 Safefree(GvNAME(sv));
3823 /* cannot decrease stash refcount yet, as we might recursively delete
3824 ourselves when the refcnt drops to zero. Delay SvREFCNT_dec
3825 of stash until current sv is completely gone.
3826 -- JohnPC, 27 Mar 1998 */
3827 stash = GvSTASH(sv);
3833 (void)SvOOK_off(sv);
3841 SvREFCNT_dec(SvRV(sv));
3843 else if (SvPVX(sv) && SvLEN(sv))
3844 Safefree(SvPVX(sv));
3854 switch (SvTYPE(sv)) {
3870 del_XPVIV(SvANY(sv));
3873 del_XPVNV(SvANY(sv));
3876 del_XPVMG(SvANY(sv));
3879 del_XPVLV(SvANY(sv));
3882 del_XPVAV(SvANY(sv));
3885 del_XPVHV(SvANY(sv));
3888 del_XPVCV(SvANY(sv));
3891 del_XPVGV(SvANY(sv));
3892 /* code duplication for increased performance. */
3893 SvFLAGS(sv) &= SVf_BREAK;
3894 SvFLAGS(sv) |= SVTYPEMASK;
3895 /* decrease refcount of the stash that owns this GV, if any */
3897 SvREFCNT_dec(stash);
3898 return; /* not break, SvFLAGS reset already happened */
3900 del_XPVBM(SvANY(sv));
3903 del_XPVFM(SvANY(sv));
3906 del_XPVIO(SvANY(sv));
3909 SvFLAGS(sv) &= SVf_BREAK;
3910 SvFLAGS(sv) |= SVTYPEMASK;
3914 Perl_sv_newref(pTHX_ SV *sv)
3917 ATOMIC_INC(SvREFCNT(sv));
3924 Free the memory used by an SV.
3930 Perl_sv_free(pTHX_ SV *sv)
3933 int refcount_is_zero;
3937 if (SvREFCNT(sv) == 0) {
3938 if (SvFLAGS(sv) & SVf_BREAK)
3940 if (PL_in_clean_all) /* All is fair */
3942 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
3943 /* make sure SvREFCNT(sv)==0 happens very seldom */
3944 SvREFCNT(sv) = (~(U32)0)/2;
3947 if (ckWARN_d(WARN_INTERNAL))
3948 Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free unreferenced scalar");
3951 ATOMIC_DEC_AND_TEST(refcount_is_zero, SvREFCNT(sv));
3952 if (!refcount_is_zero)
3956 if (ckWARN_d(WARN_DEBUGGING))
3957 Perl_warner(aTHX_ WARN_DEBUGGING,
3958 "Attempt to free temp prematurely: SV 0x%"UVxf,
3963 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
3964 /* make sure SvREFCNT(sv)==0 happens very seldom */
3965 SvREFCNT(sv) = (~(U32)0)/2;
3976 Returns the length of the string in the SV. See also C<SvCUR>.
3982 Perl_sv_len(pTHX_ register SV *sv)
3991 len = mg_length(sv);
3993 junk = SvPV(sv, len);
3998 =for apidoc sv_len_utf8
4000 Returns the number of characters in the string in an SV, counting wide
4001 UTF8 bytes as a single character.
4007 Perl_sv_len_utf8(pTHX_ register SV *sv)
4018 len = mg_length(sv);
4021 s = (U8*)SvPV(sv, len);
4032 Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp)
4037 I32 uoffset = *offsetp;
4043 start = s = (U8*)SvPV(sv, len);
4045 while (s < send && uoffset--)
4049 *offsetp = s - start;
4053 while (s < send && ulen--)
4063 Perl_sv_pos_b2u(pTHX_ register SV *sv, I32* offsetp)
4072 s = (U8*)SvPV(sv, len);
4074 Perl_croak(aTHX_ "panic: bad byte offset");
4075 send = s + *offsetp;
4083 if (ckWARN_d(WARN_UTF8))
4084 Perl_warner(aTHX_ WARN_UTF8, "Malformed UTF-8 character");
4094 Returns a boolean indicating whether the strings in the two SVs are
4101 Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2)
4108 bool pv1tmp = FALSE;
4109 bool pv2tmp = FALSE;
4116 pv1 = SvPV(sv1, cur1);
4123 pv2 = SvPV(sv2, cur2);
4125 /* do not utf8ize the comparands as a side-effect */
4126 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTE && 0) {
4128 pv2 = (char*)bytes_to_utf8((U8*)pv2, &cur2);
4132 pv1 = (char*)bytes_to_utf8((U8*)pv1, &cur1);
4138 eq = memEQ(pv1, pv2, cur1);
4151 Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
4152 string in C<sv1> is less than, equal to, or greater than the string in
4159 Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2)
4164 bool pv1tmp = FALSE;
4165 bool pv2tmp = FALSE;
4172 pv1 = SvPV(sv1, cur1);
4179 pv2 = SvPV(sv2, cur2);
4181 /* do not utf8ize the comparands as a side-effect */
4182 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTE) {
4184 pv2 = (char*)bytes_to_utf8((U8*)pv2, &cur2);
4188 pv1 = (char*)bytes_to_utf8((U8*)pv1, &cur1);
4194 cmp = cur2 ? -1 : 0;
4198 I32 retval = memcmp((void*)pv1, (void*)pv2, cur1 < cur2 ? cur1 : cur2);
4201 cmp = retval < 0 ? -1 : 1;
4202 } else if (cur1 == cur2) {
4205 cmp = cur1 < cur2 ? -1 : 1;
4218 =for apidoc sv_cmp_locale
4220 Compares the strings in two SVs in a locale-aware manner. See
4227 Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2)
4229 #ifdef USE_LOCALE_COLLATE
4235 if (PL_collation_standard)
4239 pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL;
4241 pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL;
4243 if (!pv1 || !len1) {
4254 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
4257 return retval < 0 ? -1 : 1;
4260 * When the result of collation is equality, that doesn't mean
4261 * that there are no differences -- some locales exclude some
4262 * characters from consideration. So to avoid false equalities,
4263 * we use the raw string as a tiebreaker.
4269 #endif /* USE_LOCALE_COLLATE */
4271 return sv_cmp(sv1, sv2);
4274 #ifdef USE_LOCALE_COLLATE
4276 * Any scalar variable may carry an 'o' magic that contains the
4277 * scalar data of the variable transformed to such a format that
4278 * a normal memory comparison can be used to compare the data
4279 * according to the locale settings.
4282 Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp)
4286 mg = SvMAGICAL(sv) ? mg_find(sv, 'o') : (MAGIC *) NULL;
4287 if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
4292 Safefree(mg->mg_ptr);
4294 if ((xf = mem_collxfrm(s, len, &xlen))) {
4295 if (SvREADONLY(sv)) {
4298 return xf + sizeof(PL_collation_ix);
4301 sv_magic(sv, 0, 'o', 0, 0);
4302 mg = mg_find(sv, 'o');
4315 if (mg && mg->mg_ptr) {
4317 return mg->mg_ptr + sizeof(PL_collation_ix);
4325 #endif /* USE_LOCALE_COLLATE */
4330 Get a line from the filehandle and store it into the SV, optionally
4331 appending to the currently-stored string.
4337 Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append)
4342 register STDCHAR rslast;
4343 register STDCHAR *bp;
4347 SV_CHECK_THINKFIRST(sv);
4348 (void)SvUPGRADE(sv, SVt_PV);
4352 if (RsSNARF(PL_rs)) {
4356 else if (RsRECORD(PL_rs)) {
4357 I32 recsize, bytesread;
4360 /* Grab the size of the record we're getting */
4361 recsize = SvIV(SvRV(PL_rs));
4362 (void)SvPOK_only(sv); /* Validate pointer */
4363 buffer = SvGROW(sv, recsize + 1);
4366 /* VMS wants read instead of fread, because fread doesn't respect */
4367 /* RMS record boundaries. This is not necessarily a good thing to be */
4368 /* doing, but we've got no other real choice */
4369 bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize);
4371 bytesread = PerlIO_read(fp, buffer, recsize);
4373 SvCUR_set(sv, bytesread);
4374 buffer[bytesread] = '\0';
4375 return(SvCUR(sv) ? SvPVX(sv) : Nullch);
4377 else if (RsPARA(PL_rs)) {
4382 rsptr = SvPV(PL_rs, rslen);
4383 rslast = rslen ? rsptr[rslen - 1] : '\0';
4385 if (RsPARA(PL_rs)) { /* have to do this both before and after */
4386 do { /* to make sure file boundaries work right */
4389 i = PerlIO_getc(fp);
4393 PerlIO_ungetc(fp,i);
4399 /* See if we know enough about I/O mechanism to cheat it ! */
4401 /* This used to be #ifdef test - it is made run-time test for ease
4402 of abstracting out stdio interface. One call should be cheap
4403 enough here - and may even be a macro allowing compile
4407 if (PerlIO_fast_gets(fp)) {
4410 * We're going to steal some values from the stdio struct
4411 * and put EVERYTHING in the innermost loop into registers.
4413 register STDCHAR *ptr;
4417 #if defined(VMS) && defined(PERLIO_IS_STDIO)
4418 /* An ungetc()d char is handled separately from the regular
4419 * buffer, so we getc() it back out and stuff it in the buffer.
4421 i = PerlIO_getc(fp);
4422 if (i == EOF) return 0;
4423 *(--((*fp)->_ptr)) = (unsigned char) i;
4427 /* Here is some breathtakingly efficient cheating */
4429 cnt = PerlIO_get_cnt(fp); /* get count into register */
4430 (void)SvPOK_only(sv); /* validate pointer */
4431 if (SvLEN(sv) - append <= cnt + 1) { /* make sure we have the room */
4432 if (cnt > 80 && SvLEN(sv) > append) {
4433 shortbuffered = cnt - SvLEN(sv) + append + 1;
4434 cnt -= shortbuffered;
4438 /* remember that cnt can be negative */
4439 SvGROW(sv, append + (cnt <= 0 ? 2 : (cnt + 1)));
4444 bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */
4445 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
4446 DEBUG_P(PerlIO_printf(Perl_debug_log,
4447 "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
4448 DEBUG_P(PerlIO_printf(Perl_debug_log,
4449 "Screamer: entering: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
4450 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
4451 PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
4456 while (cnt > 0) { /* this | eat */
4458 if ((*bp++ = *ptr++) == rslast) /* really | dust */
4459 goto thats_all_folks; /* screams | sed :-) */
4463 Copy(ptr, bp, cnt, char); /* this | eat */
4464 bp += cnt; /* screams | dust */
4465 ptr += cnt; /* louder | sed :-) */
4470 if (shortbuffered) { /* oh well, must extend */
4471 cnt = shortbuffered;
4473 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
4475 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
4476 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
4480 DEBUG_P(PerlIO_printf(Perl_debug_log,
4481 "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n",
4482 PTR2UV(ptr),(long)cnt));
4483 PerlIO_set_ptrcnt(fp, ptr, cnt); /* deregisterize cnt and ptr */
4484 DEBUG_P(PerlIO_printf(Perl_debug_log,
4485 "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
4486 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
4487 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
4488 /* This used to call 'filbuf' in stdio form, but as that behaves like
4489 getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
4490 another abstraction. */
4491 i = PerlIO_getc(fp); /* get more characters */
4492 DEBUG_P(PerlIO_printf(Perl_debug_log,
4493 "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
4494 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
4495 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
4496 cnt = PerlIO_get_cnt(fp);
4497 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
4498 DEBUG_P(PerlIO_printf(Perl_debug_log,
4499 "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
4501 if (i == EOF) /* all done for ever? */
4502 goto thats_really_all_folks;
4504 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
4506 SvGROW(sv, bpx + cnt + 2);
4507 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
4509 *bp++ = i; /* store character from PerlIO_getc */
4511 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
4512 goto thats_all_folks;
4516 if ((rslen > 1 && (bp - (STDCHAR*)SvPVX(sv) < rslen)) ||
4517 memNE((char*)bp - rslen, rsptr, rslen))
4518 goto screamer; /* go back to the fray */
4519 thats_really_all_folks:
4521 cnt += shortbuffered;
4522 DEBUG_P(PerlIO_printf(Perl_debug_log,
4523 "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
4524 PerlIO_set_ptrcnt(fp, ptr, cnt); /* put these back or we're in trouble */
4525 DEBUG_P(PerlIO_printf(Perl_debug_log,
4526 "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
4527 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
4528 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
4530 SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */
4531 DEBUG_P(PerlIO_printf(Perl_debug_log,
4532 "Screamer: done, len=%ld, string=|%.*s|\n",
4533 (long)SvCUR(sv),(int)SvCUR(sv),SvPVX(sv)));
4538 /*The big, slow, and stupid way */
4541 /* Need to work around EPOC SDK features */
4542 /* On WINS: MS VC5 generates calls to _chkstk, */
4543 /* if a `large' stack frame is allocated */
4544 /* gcc on MARM does not generate calls like these */
4550 register STDCHAR *bpe = buf + sizeof(buf);
4552 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = i) != rslast && bp < bpe)
4553 ; /* keep reading */
4557 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
4558 /* Accomodate broken VAXC compiler, which applies U8 cast to
4559 * both args of ?: operator, causing EOF to change into 255
4561 if (cnt) { i = (U8)buf[cnt - 1]; } else { i = EOF; }
4565 sv_catpvn(sv, (char *) buf, cnt);
4567 sv_setpvn(sv, (char *) buf, cnt);
4569 if (i != EOF && /* joy */
4571 SvCUR(sv) < rslen ||
4572 memNE(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
4576 * If we're reading from a TTY and we get a short read,
4577 * indicating that the user hit his EOF character, we need
4578 * to notice it now, because if we try to read from the TTY
4579 * again, the EOF condition will disappear.
4581 * The comparison of cnt to sizeof(buf) is an optimization
4582 * that prevents unnecessary calls to feof().
4586 if (!(cnt < sizeof(buf) && PerlIO_eof(fp)))
4591 if (RsPARA(PL_rs)) { /* have to do this both before and after */
4592 while (i != EOF) { /* to make sure file boundaries work right */
4593 i = PerlIO_getc(fp);
4595 PerlIO_ungetc(fp,i);
4601 return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch;
4608 Auto-increment of the value in the SV.
4614 Perl_sv_inc(pTHX_ register SV *sv)
4623 if (SvTHINKFIRST(sv)) {
4624 if (SvREADONLY(sv)) {
4626 if (PL_curcop != &PL_compiling)
4627 Perl_croak(aTHX_ PL_no_modify);
4631 if (SvAMAGIC(sv) && AMG_CALLun(sv,inc))
4633 i = PTR2IV(SvRV(sv));
4638 flags = SvFLAGS(sv);
4639 if (flags & SVp_NOK) {
4640 (void)SvNOK_only(sv);
4644 if (flags & SVp_IOK) {
4646 if (SvUVX(sv) == UV_MAX)
4647 sv_setnv(sv, (NV)UV_MAX + 1.0);
4649 (void)SvIOK_only_UV(sv);
4652 if (SvIVX(sv) == IV_MAX)
4653 sv_setnv(sv, (NV)IV_MAX + 1.0);
4655 (void)SvIOK_only(sv);
4661 if (!(flags & SVp_POK) || !*SvPVX(sv)) {
4662 if ((flags & SVTYPEMASK) < SVt_PVNV)
4663 sv_upgrade(sv, SVt_NV);
4665 (void)SvNOK_only(sv);
4669 while (isALPHA(*d)) d++;
4670 while (isDIGIT(*d)) d++;
4672 sv_setnv(sv,Atof(SvPVX(sv)) + 1.0); /* punt */
4676 while (d >= SvPVX(sv)) {
4684 /* MKS: The original code here died if letters weren't consecutive.
4685 * at least it didn't have to worry about non-C locales. The
4686 * new code assumes that ('z'-'a')==('Z'-'A'), letters are
4687 * arranged in order (although not consecutively) and that only
4688 * [A-Za-z] are accepted by isALPHA in the C locale.
4690 if (*d != 'z' && *d != 'Z') {
4691 do { ++*d; } while (!isALPHA(*d));
4694 *(d--) -= 'z' - 'a';
4699 *(d--) -= 'z' - 'a' + 1;
4703 /* oh,oh, the number grew */
4704 SvGROW(sv, SvCUR(sv) + 2);
4706 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--)
4717 Auto-decrement of the value in the SV.
4723 Perl_sv_dec(pTHX_ register SV *sv)
4731 if (SvTHINKFIRST(sv)) {
4732 if (SvREADONLY(sv)) {
4734 if (PL_curcop != &PL_compiling)
4735 Perl_croak(aTHX_ PL_no_modify);
4739 if (SvAMAGIC(sv) && AMG_CALLun(sv,dec))
4741 i = PTR2IV(SvRV(sv));
4746 flags = SvFLAGS(sv);
4747 if (flags & SVp_NOK) {
4749 (void)SvNOK_only(sv);
4752 if (flags & SVp_IOK) {
4754 if (SvUVX(sv) == 0) {
4755 (void)SvIOK_only(sv);
4759 (void)SvIOK_only_UV(sv);
4763 if (SvIVX(sv) == IV_MIN)
4764 sv_setnv(sv, (NV)IV_MIN - 1.0);
4766 (void)SvIOK_only(sv);
4772 if (!(flags & SVp_POK)) {
4773 if ((flags & SVTYPEMASK) < SVt_PVNV)
4774 sv_upgrade(sv, SVt_NV);
4776 (void)SvNOK_only(sv);
4779 sv_setnv(sv,Atof(SvPVX(sv)) - 1.0); /* punt */
4783 =for apidoc sv_mortalcopy
4785 Creates a new SV which is a copy of the original SV. The new SV is marked
4791 /* Make a string that will exist for the duration of the expression
4792 * evaluation. Actually, it may have to last longer than that, but
4793 * hopefully we won't free it until it has been assigned to a
4794 * permanent location. */
4797 Perl_sv_mortalcopy(pTHX_ SV *oldstr)
4803 sv_setsv(sv,oldstr);
4805 PL_tmps_stack[++PL_tmps_ix] = sv;
4811 =for apidoc sv_newmortal
4813 Creates a new SV which is mortal. The reference count of the SV is set to 1.
4819 Perl_sv_newmortal(pTHX)
4825 SvFLAGS(sv) = SVs_TEMP;
4827 PL_tmps_stack[++PL_tmps_ix] = sv;
4832 =for apidoc sv_2mortal
4834 Marks an SV as mortal. The SV will be destroyed when the current context
4840 /* same thing without the copying */
4843 Perl_sv_2mortal(pTHX_ register SV *sv)
4848 if (SvREADONLY(sv) && SvIMMORTAL(sv))
4851 PL_tmps_stack[++PL_tmps_ix] = sv;
4859 Creates a new SV and copies a string into it. The reference count for the
4860 SV is set to 1. If C<len> is zero, Perl will compute the length using
4861 strlen(). For efficiency, consider using C<newSVpvn> instead.
4867 Perl_newSVpv(pTHX_ const char *s, STRLEN len)
4874 sv_setpvn(sv,s,len);
4879 =for apidoc newSVpvn
4881 Creates a new SV and copies a string into it. The reference count for the
4882 SV is set to 1. Note that if C<len> is zero, Perl will create a zero length
4883 string. You are responsible for ensuring that the source string is at least
4890 Perl_newSVpvn(pTHX_ const char *s, STRLEN len)
4895 sv_setpvn(sv,s,len);
4899 #if defined(PERL_IMPLICIT_CONTEXT)
4901 Perl_newSVpvf_nocontext(const char* pat, ...)
4906 va_start(args, pat);
4907 sv = vnewSVpvf(pat, &args);
4914 =for apidoc newSVpvf
4916 Creates a new SV an initialize it with the string formatted like
4923 Perl_newSVpvf(pTHX_ const char* pat, ...)
4927 va_start(args, pat);
4928 sv = vnewSVpvf(pat, &args);
4934 Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args)
4938 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
4945 Creates a new SV and copies a floating point value into it.
4946 The reference count for the SV is set to 1.
4952 Perl_newSVnv(pTHX_ NV n)
4964 Creates a new SV and copies an integer into it. The reference count for the
4971 Perl_newSViv(pTHX_ IV i)
4983 Creates a new SV and copies an unsigned integer into it.
4984 The reference count for the SV is set to 1.
4990 Perl_newSVuv(pTHX_ UV u)
5000 =for apidoc newRV_noinc
5002 Creates an RV wrapper for an SV. The reference count for the original
5003 SV is B<not> incremented.
5009 Perl_newRV_noinc(pTHX_ SV *tmpRef)
5015 sv_upgrade(sv, SVt_RV);
5022 /* newRV_inc is #defined to newRV in sv.h */
5024 Perl_newRV(pTHX_ SV *tmpRef)
5026 return newRV_noinc(SvREFCNT_inc(tmpRef));
5032 Creates a new SV which is an exact duplicate of the original SV.
5037 /* make an exact duplicate of old */
5040 Perl_newSVsv(pTHX_ register SV *old)
5047 if (SvTYPE(old) == SVTYPEMASK) {
5048 if (ckWARN_d(WARN_INTERNAL))
5049 Perl_warner(aTHX_ WARN_INTERNAL, "semi-panic: attempt to dup freed string");
5064 Perl_sv_reset(pTHX_ register char *s, HV *stash)
5072 char todo[PERL_UCHAR_MAX+1];
5077 if (!*s) { /* reset ?? searches */
5078 for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) {
5079 pm->op_pmdynflags &= ~PMdf_USED;
5084 /* reset variables */
5086 if (!HvARRAY(stash))
5089 Zero(todo, 256, char);
5091 i = (unsigned char)*s;
5095 max = (unsigned char)*s++;
5096 for ( ; i <= max; i++) {
5099 for (i = 0; i <= (I32) HvMAX(stash); i++) {
5100 for (entry = HvARRAY(stash)[i];
5102 entry = HeNEXT(entry))
5104 if (!todo[(U8)*HeKEY(entry)])
5106 gv = (GV*)HeVAL(entry);
5108 if (SvTHINKFIRST(sv)) {
5109 if (!SvREADONLY(sv) && SvROK(sv))
5114 if (SvTYPE(sv) >= SVt_PV) {
5116 if (SvPVX(sv) != Nullch)
5123 if (GvHV(gv) && !HvNAME(GvHV(gv))) {
5125 #ifndef VMS /* VMS has no environ array */
5127 environ[0] = Nullch;
5136 Perl_sv_2io(pTHX_ SV *sv)
5142 switch (SvTYPE(sv)) {
5150 Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv));
5154 Perl_croak(aTHX_ PL_no_usym, "filehandle");
5156 return sv_2io(SvRV(sv));
5157 gv = gv_fetchpv(SvPV(sv,n_a), FALSE, SVt_PVIO);
5163 Perl_croak(aTHX_ "Bad filehandle: %s", SvPV(sv,n_a));
5170 Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref)
5177 return *gvp = Nullgv, Nullcv;
5178 switch (SvTYPE(sv)) {
5198 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
5199 tryAMAGICunDEREF(to_cv);
5202 if (SvTYPE(sv) == SVt_PVCV) {
5211 Perl_croak(aTHX_ "Not a subroutine reference");
5216 gv = gv_fetchpv(SvPV(sv, n_a), lref, SVt_PVCV);
5222 if (lref && !GvCVu(gv)) {
5225 tmpsv = NEWSV(704,0);
5226 gv_efullname3(tmpsv, gv, Nullch);
5227 /* XXX this is probably not what they think they're getting.
5228 * It has the same effect as "sub name;", i.e. just a forward
5230 newSUB(start_subparse(FALSE, 0),
5231 newSVOP(OP_CONST, 0, tmpsv),
5236 Perl_croak(aTHX_ "Unable to create sub named \"%s\"", SvPV(sv,n_a));
5245 Returns true if the SV has a true value by Perl's rules.
5251 Perl_sv_true(pTHX_ register SV *sv)
5258 if ((tXpv = (XPV*)SvANY(sv)) &&
5259 (tXpv->xpv_cur > 1 ||
5260 (tXpv->xpv_cur && *tXpv->xpv_pv != '0')))
5267 return SvIVX(sv) != 0;
5270 return SvNVX(sv) != 0.0;
5272 return sv_2bool(sv);
5278 Perl_sv_iv(pTHX_ register SV *sv)
5282 return (IV)SvUVX(sv);
5289 Perl_sv_uv(pTHX_ register SV *sv)
5294 return (UV)SvIVX(sv);
5300 Perl_sv_nv(pTHX_ register SV *sv)
5308 Perl_sv_pv(pTHX_ SV *sv)
5315 return sv_2pv(sv, &n_a);
5319 Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp)
5325 return sv_2pv(sv, lp);
5329 =for apidoc sv_pvn_force
5331 Get a sensible string out of the SV somehow.
5337 Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp)
5341 if (SvTHINKFIRST(sv) && !SvROK(sv))
5342 sv_force_normal(sv);
5348 if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) {
5350 Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
5351 PL_op_name[PL_op->op_type]);
5355 if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */
5360 (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */
5361 SvGROW(sv, len + 1);
5362 Move(s,SvPVX(sv),len,char);
5367 SvPOK_on(sv); /* validate pointer */
5369 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
5370 PTR2UV(sv),SvPVX(sv)));
5377 Perl_sv_pvbyte(pTHX_ SV *sv)
5383 Perl_sv_pvbyten(pTHX_ SV *sv, STRLEN *lp)
5385 return sv_pvn(sv,lp);
5389 Perl_sv_pvbyten_force(pTHX_ SV *sv, STRLEN *lp)
5391 return sv_pvn_force(sv,lp);
5395 Perl_sv_pvutf8(pTHX_ SV *sv)
5397 sv_utf8_upgrade(sv);
5402 Perl_sv_pvutf8n(pTHX_ SV *sv, STRLEN *lp)
5404 sv_utf8_upgrade(sv);
5405 return sv_pvn(sv,lp);
5409 =for apidoc sv_pvutf8n_force
5411 Get a sensible UTF8-encoded string out of the SV somehow. See
5418 Perl_sv_pvutf8n_force(pTHX_ SV *sv, STRLEN *lp)
5420 sv_utf8_upgrade(sv);
5421 return sv_pvn_force(sv,lp);
5425 =for apidoc sv_reftype
5427 Returns a string describing what the SV is a reference to.
5433 Perl_sv_reftype(pTHX_ SV *sv, int ob)
5435 if (ob && SvOBJECT(sv))
5436 return HvNAME(SvSTASH(sv));
5438 switch (SvTYPE(sv)) {
5452 case SVt_PVLV: return "LVALUE";
5453 case SVt_PVAV: return "ARRAY";
5454 case SVt_PVHV: return "HASH";
5455 case SVt_PVCV: return "CODE";
5456 case SVt_PVGV: return "GLOB";
5457 case SVt_PVFM: return "FORMAT";
5458 case SVt_PVIO: return "IO";
5459 default: return "UNKNOWN";
5465 =for apidoc sv_isobject
5467 Returns a boolean indicating whether the SV is an RV pointing to a blessed
5468 object. If the SV is not an RV, or if the object is not blessed, then this
5475 Perl_sv_isobject(pTHX_ SV *sv)
5492 Returns a boolean indicating whether the SV is blessed into the specified
5493 class. This does not check for subtypes; use C<sv_derived_from> to verify
5494 an inheritance relationship.
5500 Perl_sv_isa(pTHX_ SV *sv, const char *name)
5512 return strEQ(HvNAME(SvSTASH(sv)), name);
5518 Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then
5519 it will be upgraded to one. If C<classname> is non-null then the new SV will
5520 be blessed in the specified package. The new SV is returned and its
5521 reference count is 1.
5527 Perl_newSVrv(pTHX_ SV *rv, const char *classname)
5534 SV_CHECK_THINKFIRST(rv);
5537 if (SvTYPE(rv) < SVt_RV)
5538 sv_upgrade(rv, SVt_RV);
5545 HV* stash = gv_stashpv(classname, TRUE);
5546 (void)sv_bless(rv, stash);
5552 =for apidoc sv_setref_pv
5554 Copies a pointer into a new SV, optionally blessing the SV. The C<rv>
5555 argument will be upgraded to an RV. That RV will be modified to point to
5556 the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed
5557 into the SV. The C<classname> argument indicates the package for the
5558 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
5559 will be returned and will have a reference count of 1.
5561 Do not use with other Perl types such as HV, AV, SV, CV, because those
5562 objects will become corrupted by the pointer copy process.
5564 Note that C<sv_setref_pvn> copies the string while this copies the pointer.
5570 Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv)
5573 sv_setsv(rv, &PL_sv_undef);
5577 sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
5582 =for apidoc sv_setref_iv
5584 Copies an integer into a new SV, optionally blessing the SV. The C<rv>
5585 argument will be upgraded to an RV. That RV will be modified to point to
5586 the new SV. The C<classname> argument indicates the package for the
5587 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
5588 will be returned and will have a reference count of 1.
5594 Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv)
5596 sv_setiv(newSVrv(rv,classname), iv);
5601 =for apidoc sv_setref_nv
5603 Copies a double into a new SV, optionally blessing the SV. The C<rv>
5604 argument will be upgraded to an RV. That RV will be modified to point to
5605 the new SV. The C<classname> argument indicates the package for the
5606 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
5607 will be returned and will have a reference count of 1.
5613 Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv)
5615 sv_setnv(newSVrv(rv,classname), nv);
5620 =for apidoc sv_setref_pvn
5622 Copies a string into a new SV, optionally blessing the SV. The length of the
5623 string must be specified with C<n>. The C<rv> argument will be upgraded to
5624 an RV. That RV will be modified to point to the new SV. The C<classname>
5625 argument indicates the package for the blessing. Set C<classname> to
5626 C<Nullch> to avoid the blessing. The new SV will be returned and will have
5627 a reference count of 1.
5629 Note that C<sv_setref_pv> copies the pointer while this copies the string.
5635 Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, char *pv, STRLEN n)
5637 sv_setpvn(newSVrv(rv,classname), pv, n);
5642 =for apidoc sv_bless
5644 Blesses an SV into a specified package. The SV must be an RV. The package
5645 must be designated by its stash (see C<gv_stashpv()>). The reference count
5646 of the SV is unaffected.
5652 Perl_sv_bless(pTHX_ SV *sv, HV *stash)
5657 Perl_croak(aTHX_ "Can't bless non-reference value");
5659 if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) {
5660 if (SvREADONLY(tmpRef))
5661 Perl_croak(aTHX_ PL_no_modify);
5662 if (SvOBJECT(tmpRef)) {
5663 if (SvTYPE(tmpRef) != SVt_PVIO)
5665 SvREFCNT_dec(SvSTASH(tmpRef));
5668 SvOBJECT_on(tmpRef);
5669 if (SvTYPE(tmpRef) != SVt_PVIO)
5671 (void)SvUPGRADE(tmpRef, SVt_PVMG);
5672 SvSTASH(tmpRef) = (HV*)SvREFCNT_inc(stash);
5683 S_sv_unglob(pTHX_ SV *sv)
5687 assert(SvTYPE(sv) == SVt_PVGV);
5692 SvREFCNT_dec(GvSTASH(sv));
5693 GvSTASH(sv) = Nullhv;
5695 sv_unmagic(sv, '*');
5696 Safefree(GvNAME(sv));
5699 /* need to keep SvANY(sv) in the right arena */
5700 xpvmg = new_XPVMG();
5701 StructCopy(SvANY(sv), xpvmg, XPVMG);
5702 del_XPVGV(SvANY(sv));
5705 SvFLAGS(sv) &= ~SVTYPEMASK;
5706 SvFLAGS(sv) |= SVt_PVMG;
5710 =for apidoc sv_unref
5712 Unsets the RV status of the SV, and decrements the reference count of
5713 whatever was being referenced by the RV. This can almost be thought of
5714 as a reversal of C<newSVrv>. See C<SvROK_off>.
5720 Perl_sv_unref(pTHX_ SV *sv)
5724 if (SvWEAKREF(sv)) {
5732 if (SvREFCNT(rv) != 1 || SvREADONLY(rv))
5735 sv_2mortal(rv); /* Schedule for freeing later */
5739 Perl_sv_taint(pTHX_ SV *sv)
5741 sv_magic((sv), Nullsv, 't', Nullch, 0);
5745 Perl_sv_untaint(pTHX_ SV *sv)
5747 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
5748 MAGIC *mg = mg_find(sv, 't');
5755 Perl_sv_tainted(pTHX_ SV *sv)
5757 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
5758 MAGIC *mg = mg_find(sv, 't');
5759 if (mg && ((mg->mg_len & 1) || ((mg->mg_len & 2) && mg->mg_obj == sv)))
5766 =for apidoc sv_setpviv
5768 Copies an integer into the given SV, also updating its string value.
5769 Does not handle 'set' magic. See C<sv_setpviv_mg>.
5775 Perl_sv_setpviv(pTHX_ SV *sv, IV iv)
5777 char buf[TYPE_CHARS(UV)];
5779 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
5781 sv_setpvn(sv, ptr, ebuf - ptr);
5786 =for apidoc sv_setpviv_mg
5788 Like C<sv_setpviv>, but also handles 'set' magic.
5794 Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv)
5796 char buf[TYPE_CHARS(UV)];
5798 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
5800 sv_setpvn(sv, ptr, ebuf - ptr);
5804 #if defined(PERL_IMPLICIT_CONTEXT)
5806 Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...)
5810 va_start(args, pat);
5811 sv_vsetpvf(sv, pat, &args);
5817 Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...)
5821 va_start(args, pat);
5822 sv_vsetpvf_mg(sv, pat, &args);
5828 =for apidoc sv_setpvf
5830 Processes its arguments like C<sprintf> and sets an SV to the formatted
5831 output. Does not handle 'set' magic. See C<sv_setpvf_mg>.
5837 Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...)
5840 va_start(args, pat);
5841 sv_vsetpvf(sv, pat, &args);
5846 Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args)
5848 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
5852 =for apidoc sv_setpvf_mg
5854 Like C<sv_setpvf>, but also handles 'set' magic.
5860 Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...)
5863 va_start(args, pat);
5864 sv_vsetpvf_mg(sv, pat, &args);
5869 Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
5871 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
5875 #if defined(PERL_IMPLICIT_CONTEXT)
5877 Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...)
5881 va_start(args, pat);
5882 sv_vcatpvf(sv, pat, &args);
5887 Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...)
5891 va_start(args, pat);
5892 sv_vcatpvf_mg(sv, pat, &args);
5898 =for apidoc sv_catpvf
5900 Processes its arguments like C<sprintf> and appends the formatted output
5901 to an SV. Handles 'get' magic, but not 'set' magic. C<SvSETMAGIC()> must
5902 typically be called after calling this function to handle 'set' magic.
5908 Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...)
5911 va_start(args, pat);
5912 sv_vcatpvf(sv, pat, &args);
5917 Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args)
5919 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
5923 =for apidoc sv_catpvf_mg
5925 Like C<sv_catpvf>, but also handles 'set' magic.
5931 Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...)
5934 va_start(args, pat);
5935 sv_vcatpvf_mg(sv, pat, &args);
5940 Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
5942 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
5947 =for apidoc sv_vsetpvfn
5949 Works like C<vcatpvfn> but copies the text into the SV instead of
5956 Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
5958 sv_setpvn(sv, "", 0);
5959 sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted);
5963 =for apidoc sv_vcatpvfn
5965 Processes its arguments like C<vsprintf> and appends the formatted output
5966 to an SV. Uses an array of SVs if the C style variable argument list is
5967 missing (NULL). When running with taint checks enabled, indicates via
5968 C<maybe_tainted> if results are untrustworthy (often due to the use of
5975 Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
5983 static char nullstr[] = "(null)";
5986 /* no matter what, this is a string now */
5987 (void)SvPV_force(sv, origlen);
5989 /* special-case "", "%s", and "%_" */
5992 if (patlen == 2 && pat[0] == '%') {
5996 char *s = va_arg(*args, char*);
5997 sv_catpv(sv, s ? s : nullstr);
5999 else if (svix < svmax) {
6000 sv_catsv(sv, *svargs);
6001 if (DO_UTF8(*svargs))
6007 argsv = va_arg(*args, SV*);
6008 sv_catsv(sv, argsv);
6013 /* See comment on '_' below */
6018 patend = (char*)pat + patlen;
6019 for (p = (char*)pat; p < patend; p = q) {
6022 bool vectorize = FALSE;
6029 bool has_precis = FALSE;
6031 bool is_utf = FALSE;
6034 U8 utf8buf[UTF8_MAXLEN];
6035 STRLEN esignlen = 0;
6037 char *eptr = Nullch;
6039 /* Times 4: a decimal digit takes more than 3 binary digits.
6040 * NV_DIG: mantissa takes than many decimal digits.
6041 * Plus 32: Playing safe. */
6042 char ebuf[IV_DIG * 4 + NV_DIG + 32];
6043 /* large enough for "%#.#f" --chip */
6044 /* what about long double NVs? --jhi */
6047 U8 *vecstr = Null(U8*);
6059 STRLEN dotstrlen = 1;
6061 for (q = p; q < patend && *q != '%'; ++q) ;
6063 sv_catpvn(sv, p, q - p);
6092 case '*': /* printf("%*vX",":",$ipv6addr) */
6097 vecsv = va_arg(*args, SV*);
6098 else if (svix < svmax)
6099 vecsv = svargs[svix++];
6102 dotstr = SvPVx(vecsv,dotstrlen);
6121 case '1': case '2': case '3':
6122 case '4': case '5': case '6':
6123 case '7': case '8': case '9':
6126 width = width * 10 + (*q++ - '0');
6131 i = va_arg(*args, int);
6133 i = (svix < svmax) ? SvIVx(svargs[svix++]) : 0;
6135 width = (i < 0) ? -i : i;
6146 i = va_arg(*args, int);
6148 i = (svix < svmax) ? SvIVx(svargs[svix++]) : 0;
6149 precis = (i < 0) ? 0 : i;
6155 precis = precis * 10 + (*q++ - '0');
6162 vecsv = va_arg(*args, SV*);
6163 vecstr = (U8*)SvPVx(vecsv,veclen);
6164 utf = DO_UTF8(vecsv);
6166 else if (svix < svmax) {
6167 vecsv = svargs[svix++];
6168 vecstr = (U8*)SvPVx(vecsv,veclen);
6169 utf = DO_UTF8(vecsv);
6189 if (*(q + 1) == 'l') { /* lld */
6216 uv = va_arg(*args, int);
6218 uv = (svix < svmax) ? SvIVx(svargs[svix++]) : 0;
6219 if ((uv > 255 || (uv > 127 && SvUTF8(sv))) && !IN_BYTE) {
6220 eptr = (char*)utf8buf;
6221 elen = uv_to_utf8((U8*)eptr, uv) - utf8buf;
6233 eptr = va_arg(*args, char*);
6235 #ifdef MACOS_TRADITIONAL
6236 /* On MacOS, %#s format is used for Pascal strings */
6241 elen = strlen(eptr);
6244 elen = sizeof nullstr - 1;
6247 else if (svix < svmax) {
6248 argsv = svargs[svix++];
6249 eptr = SvPVx(argsv, elen);
6250 if (DO_UTF8(argsv)) {
6251 if (has_precis && precis < elen) {
6253 sv_pos_u2b(argsv, &p, 0); /* sticks at end */
6256 if (width) { /* fudge width (can't fudge elen) */
6257 width += elen - sv_len_utf8(argsv);
6266 * The "%_" hack might have to be changed someday,
6267 * if ISO or ANSI decide to use '_' for something.
6268 * So we keep it hidden from users' code.
6272 argsv = va_arg(*args,SV*);
6273 eptr = SvPVx(argsv, elen);
6279 if (has_precis && elen > precis)
6289 uv = PTR2UV(va_arg(*args, void*));
6291 uv = (svix < svmax) ? PTR2UV(svargs[svix++]) : 0;
6311 iv = (IV)utf8_to_uv(vecstr, &ulen);
6321 case 'h': iv = (short)va_arg(*args, int); break;
6322 default: iv = va_arg(*args, int); break;
6323 case 'l': iv = va_arg(*args, long); break;
6324 case 'V': iv = va_arg(*args, IV); break;
6326 case 'q': iv = va_arg(*args, Quad_t); break;
6331 iv = (svix < svmax) ? SvIVx(svargs[svix++]) : 0;
6333 case 'h': iv = (short)iv; break;
6335 case 'l': iv = (long)iv; break;
6338 case 'q': iv = (Quad_t)iv; break;
6345 esignbuf[esignlen++] = plus;
6349 esignbuf[esignlen++] = '-';
6393 uv = utf8_to_uv(vecstr, &ulen);
6403 case 'h': uv = (unsigned short)va_arg(*args, unsigned); break;
6404 default: uv = va_arg(*args, unsigned); break;
6405 case 'l': uv = va_arg(*args, unsigned long); break;
6406 case 'V': uv = va_arg(*args, UV); break;
6408 case 'q': uv = va_arg(*args, Quad_t); break;
6413 uv = (svix < svmax) ? SvUVx(svargs[svix++]) : 0;
6415 case 'h': uv = (unsigned short)uv; break;
6417 case 'l': uv = (unsigned long)uv; break;
6420 case 'q': uv = (Quad_t)uv; break;
6426 eptr = ebuf + sizeof ebuf;
6432 p = (char*)((c == 'X')
6433 ? "0123456789ABCDEF" : "0123456789abcdef");
6439 esignbuf[esignlen++] = '0';
6440 esignbuf[esignlen++] = c; /* 'x' or 'X' */
6446 *--eptr = '0' + dig;
6448 if (alt && *eptr != '0')
6454 *--eptr = '0' + dig;
6457 esignbuf[esignlen++] = '0';
6458 esignbuf[esignlen++] = 'b';
6461 default: /* it had better be ten or less */
6462 #if defined(PERL_Y2KWARN)
6463 if (ckWARN(WARN_Y2K)) {
6465 char *s = SvPV(sv,n);
6466 if (n >= 2 && s[n-2] == '1' && s[n-1] == '9'
6467 && (n == 2 || !isDIGIT(s[n-3])))
6469 Perl_warner(aTHX_ WARN_Y2K,
6470 "Possible Y2K bug: %%%c %s",
6471 c, "format string following '19'");
6477 *--eptr = '0' + dig;
6478 } while (uv /= base);
6481 elen = (ebuf + sizeof ebuf) - eptr;
6484 zeros = precis - elen;
6485 else if (precis == 0 && elen == 1 && *eptr == '0')
6490 /* FLOATING POINT */
6493 c = 'f'; /* maybe %F isn't supported here */
6499 /* This is evil, but floating point is even more evil */
6503 nv = va_arg(*args, NV);
6505 nv = (svix < svmax) ? SvNVx(svargs[svix++]) : 0.0;
6508 if (c != 'e' && c != 'E') {
6510 (void)Perl_frexp(nv, &i);
6511 if (i == PERL_INT_MIN)
6512 Perl_die(aTHX_ "panic: frexp");
6514 need = BIT_DIGITS(i);
6516 need += has_precis ? precis : 6; /* known default */
6520 need += 20; /* fudge factor */
6521 if (PL_efloatsize < need) {
6522 Safefree(PL_efloatbuf);
6523 PL_efloatsize = need + 20; /* more fudge */
6524 New(906, PL_efloatbuf, PL_efloatsize, char);
6525 PL_efloatbuf[0] = '\0';
6528 eptr = ebuf + sizeof ebuf;
6531 #ifdef USE_LONG_DOUBLE
6533 static char const my_prifldbl[] = PERL_PRIfldbl;
6534 char const *p = my_prifldbl + sizeof my_prifldbl - 3;
6535 while (p >= my_prifldbl) { *--eptr = *p--; }
6540 do { *--eptr = '0' + (base % 10); } while (base /= 10);
6545 do { *--eptr = '0' + (base % 10); } while (base /= 10);
6558 RESTORE_NUMERIC_STANDARD();
6559 (void)sprintf(PL_efloatbuf, eptr, nv);
6560 RESTORE_NUMERIC_LOCAL();
6563 eptr = PL_efloatbuf;
6564 elen = strlen(PL_efloatbuf);
6571 i = SvCUR(sv) - origlen;
6574 case 'h': *(va_arg(*args, short*)) = i; break;
6575 default: *(va_arg(*args, int*)) = i; break;
6576 case 'l': *(va_arg(*args, long*)) = i; break;
6577 case 'V': *(va_arg(*args, IV*)) = i; break;
6579 case 'q': *(va_arg(*args, Quad_t*)) = i; break;
6583 else if (svix < svmax)
6584 sv_setuv_mg(svargs[svix++], (UV)i);
6585 continue; /* not "break" */
6592 if (!args && ckWARN(WARN_PRINTF) &&
6593 (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) {
6594 SV *msg = sv_newmortal();
6595 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %s: ",
6596 (PL_op->op_type == OP_PRTF) ? "printf" : "sprintf");
6599 Perl_sv_catpvf(aTHX_ msg,
6600 "\"%%%c\"", c & 0xFF);
6602 Perl_sv_catpvf(aTHX_ msg,
6603 "\"%%\\%03"UVof"\"",
6606 sv_catpv(msg, "end of string");
6607 Perl_warner(aTHX_ WARN_PRINTF, "%"SVf, msg); /* yes, this is reentrant */
6610 /* output mangled stuff ... */
6616 /* ... right here, because formatting flags should not apply */
6617 SvGROW(sv, SvCUR(sv) + elen + 1);
6619 memcpy(p, eptr, elen);
6622 SvCUR(sv) = p - SvPVX(sv);
6623 continue; /* not "break" */
6626 have = esignlen + zeros + elen;
6627 need = (have > width ? have : width);
6630 SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1);
6632 if (esignlen && fill == '0') {
6633 for (i = 0; i < esignlen; i++)
6637 memset(p, fill, gap);
6640 if (esignlen && fill != '0') {
6641 for (i = 0; i < esignlen; i++)
6645 for (i = zeros; i; i--)
6649 memcpy(p, eptr, elen);
6653 memset(p, ' ', gap);
6658 memcpy(p, dotstr, dotstrlen);
6662 vectorize = FALSE; /* done iterating over vecstr */
6667 SvCUR(sv) = p - SvPVX(sv);
6675 #if defined(USE_ITHREADS)
6677 #if defined(USE_THREADS)
6678 # include "error: USE_THREADS and USE_ITHREADS are incompatible"
6681 #ifndef GpREFCNT_inc
6682 # define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL)
6686 #define sv_dup_inc(s) SvREFCNT_inc(sv_dup(s))
6687 #define av_dup(s) (AV*)sv_dup((SV*)s)
6688 #define av_dup_inc(s) (AV*)SvREFCNT_inc(sv_dup((SV*)s))
6689 #define hv_dup(s) (HV*)sv_dup((SV*)s)
6690 #define hv_dup_inc(s) (HV*)SvREFCNT_inc(sv_dup((SV*)s))
6691 #define cv_dup(s) (CV*)sv_dup((SV*)s)
6692 #define cv_dup_inc(s) (CV*)SvREFCNT_inc(sv_dup((SV*)s))
6693 #define io_dup(s) (IO*)sv_dup((SV*)s)
6694 #define io_dup_inc(s) (IO*)SvREFCNT_inc(sv_dup((SV*)s))
6695 #define gv_dup(s) (GV*)sv_dup((SV*)s)
6696 #define gv_dup_inc(s) (GV*)SvREFCNT_inc(sv_dup((SV*)s))
6697 #define SAVEPV(p) (p ? savepv(p) : Nullch)
6698 #define SAVEPVN(p,n) (p ? savepvn(p,n) : Nullch)
6701 Perl_re_dup(pTHX_ REGEXP *r)
6703 /* XXX fix when pmop->op_pmregexp becomes shared */
6704 return ReREFCNT_inc(r);
6708 Perl_fp_dup(pTHX_ PerlIO *fp, char type)
6712 return (PerlIO*)NULL;
6714 /* look for it in the table first */
6715 ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp);
6719 /* create anew and remember what it is */
6720 ret = PerlIO_fdupopen(fp);
6721 ptr_table_store(PL_ptr_table, fp, ret);
6726 Perl_dirp_dup(pTHX_ DIR *dp)
6735 Perl_gp_dup(pTHX_ GP *gp)
6740 /* look for it in the table first */
6741 ret = (GP*)ptr_table_fetch(PL_ptr_table, gp);
6745 /* create anew and remember what it is */
6746 Newz(0, ret, 1, GP);
6747 ptr_table_store(PL_ptr_table, gp, ret);
6750 ret->gp_refcnt = 0; /* must be before any other dups! */
6751 ret->gp_sv = sv_dup_inc(gp->gp_sv);
6752 ret->gp_io = io_dup_inc(gp->gp_io);
6753 ret->gp_form = cv_dup_inc(gp->gp_form);
6754 ret->gp_av = av_dup_inc(gp->gp_av);
6755 ret->gp_hv = hv_dup_inc(gp->gp_hv);
6756 ret->gp_egv = gv_dup(gp->gp_egv); /* GvEGV is not refcounted */
6757 ret->gp_cv = cv_dup_inc(gp->gp_cv);
6758 ret->gp_cvgen = gp->gp_cvgen;
6759 ret->gp_flags = gp->gp_flags;
6760 ret->gp_line = gp->gp_line;
6761 ret->gp_file = gp->gp_file; /* points to COP.cop_file */
6766 Perl_mg_dup(pTHX_ MAGIC *mg)
6768 MAGIC *mgret = (MAGIC*)NULL;
6771 return (MAGIC*)NULL;
6772 /* look for it in the table first */
6773 mgret = (MAGIC*)ptr_table_fetch(PL_ptr_table, mg);
6777 for (; mg; mg = mg->mg_moremagic) {
6779 Newz(0, nmg, 1, MAGIC);
6783 mgprev->mg_moremagic = nmg;
6784 nmg->mg_virtual = mg->mg_virtual; /* XXX copy dynamic vtable? */
6785 nmg->mg_private = mg->mg_private;
6786 nmg->mg_type = mg->mg_type;
6787 nmg->mg_flags = mg->mg_flags;
6788 if (mg->mg_type == 'r') {
6789 nmg->mg_obj = (SV*)re_dup((REGEXP*)mg->mg_obj);
6792 nmg->mg_obj = (mg->mg_flags & MGf_REFCOUNTED)
6793 ? sv_dup_inc(mg->mg_obj)
6794 : sv_dup(mg->mg_obj);
6796 nmg->mg_len = mg->mg_len;
6797 nmg->mg_ptr = mg->mg_ptr; /* XXX random ptr? */
6798 if (mg->mg_ptr && mg->mg_type != 'g') {
6799 if (mg->mg_len >= 0) {
6800 nmg->mg_ptr = SAVEPVN(mg->mg_ptr, mg->mg_len);
6801 if (mg->mg_type == 'c' && AMT_AMAGIC((AMT*)mg->mg_ptr)) {
6802 AMT *amtp = (AMT*)mg->mg_ptr;
6803 AMT *namtp = (AMT*)nmg->mg_ptr;
6805 for (i = 1; i < NofAMmeth; i++) {
6806 namtp->table[i] = cv_dup_inc(amtp->table[i]);
6810 else if (mg->mg_len == HEf_SVKEY)
6811 nmg->mg_ptr = (char*)sv_dup_inc((SV*)mg->mg_ptr);
6819 Perl_ptr_table_new(pTHX)
6822 Newz(0, tbl, 1, PTR_TBL_t);
6825 Newz(0, tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
6830 Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, void *sv)
6832 PTR_TBL_ENT_t *tblent;
6833 UV hash = PTR2UV(sv);
6835 tblent = tbl->tbl_ary[hash & tbl->tbl_max];
6836 for (; tblent; tblent = tblent->next) {
6837 if (tblent->oldval == sv)
6838 return tblent->newval;
6844 Perl_ptr_table_store(pTHX_ PTR_TBL_t *tbl, void *oldv, void *newv)
6846 PTR_TBL_ENT_t *tblent, **otblent;
6847 /* XXX this may be pessimal on platforms where pointers aren't good
6848 * hash values e.g. if they grow faster in the most significant
6850 UV hash = PTR2UV(oldv);
6854 otblent = &tbl->tbl_ary[hash & tbl->tbl_max];
6855 for (tblent = *otblent; tblent; i=0, tblent = tblent->next) {
6856 if (tblent->oldval == oldv) {
6857 tblent->newval = newv;
6862 Newz(0, tblent, 1, PTR_TBL_ENT_t);
6863 tblent->oldval = oldv;
6864 tblent->newval = newv;
6865 tblent->next = *otblent;
6868 if (i && tbl->tbl_items > tbl->tbl_max)
6869 ptr_table_split(tbl);
6873 Perl_ptr_table_split(pTHX_ PTR_TBL_t *tbl)
6875 PTR_TBL_ENT_t **ary = tbl->tbl_ary;
6876 UV oldsize = tbl->tbl_max + 1;
6877 UV newsize = oldsize * 2;
6880 Renew(ary, newsize, PTR_TBL_ENT_t*);
6881 Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
6882 tbl->tbl_max = --newsize;
6884 for (i=0; i < oldsize; i++, ary++) {
6885 PTR_TBL_ENT_t **curentp, **entp, *ent;
6888 curentp = ary + oldsize;
6889 for (entp = ary, ent = *ary; ent; ent = *entp) {
6890 if ((newsize & PTR2UV(ent->oldval)) != i) {
6892 ent->next = *curentp;
6907 Perl_sv_dup(pTHX_ SV *sstr)
6911 if (!sstr || SvTYPE(sstr) == SVTYPEMASK)
6913 /* look for it in the table first */
6914 dstr = (SV*)ptr_table_fetch(PL_ptr_table, sstr);
6918 /* create anew and remember what it is */
6920 ptr_table_store(PL_ptr_table, sstr, dstr);
6923 SvFLAGS(dstr) = SvFLAGS(sstr);
6924 SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */
6925 SvREFCNT(dstr) = 0; /* must be before any other dups! */
6928 if (SvANY(sstr) && PL_watch_pvx && SvPVX(sstr) == PL_watch_pvx)
6929 PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n",
6930 PL_watch_pvx, SvPVX(sstr));
6933 switch (SvTYPE(sstr)) {
6938 SvANY(dstr) = new_XIV();
6939 SvIVX(dstr) = SvIVX(sstr);
6942 SvANY(dstr) = new_XNV();
6943 SvNVX(dstr) = SvNVX(sstr);
6946 SvANY(dstr) = new_XRV();
6947 SvRV(dstr) = sv_dup_inc(SvRV(sstr));
6950 SvANY(dstr) = new_XPV();
6951 SvCUR(dstr) = SvCUR(sstr);
6952 SvLEN(dstr) = SvLEN(sstr);
6954 SvRV(dstr) = sv_dup_inc(SvRV(sstr));
6955 else if (SvPVX(sstr) && SvLEN(sstr))
6956 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
6958 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
6961 SvANY(dstr) = new_XPVIV();
6962 SvCUR(dstr) = SvCUR(sstr);
6963 SvLEN(dstr) = SvLEN(sstr);
6964 SvIVX(dstr) = SvIVX(sstr);
6966 SvRV(dstr) = sv_dup_inc(SvRV(sstr));
6967 else if (SvPVX(sstr) && SvLEN(sstr))
6968 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
6970 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
6973 SvANY(dstr) = new_XPVNV();
6974 SvCUR(dstr) = SvCUR(sstr);
6975 SvLEN(dstr) = SvLEN(sstr);
6976 SvIVX(dstr) = SvIVX(sstr);
6977 SvNVX(dstr) = SvNVX(sstr);
6979 SvRV(dstr) = sv_dup_inc(SvRV(sstr));
6980 else if (SvPVX(sstr) && SvLEN(sstr))
6981 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
6983 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
6986 SvANY(dstr) = new_XPVMG();
6987 SvCUR(dstr) = SvCUR(sstr);
6988 SvLEN(dstr) = SvLEN(sstr);
6989 SvIVX(dstr) = SvIVX(sstr);
6990 SvNVX(dstr) = SvNVX(sstr);
6991 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
6992 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
6994 SvRV(dstr) = sv_dup_inc(SvRV(sstr));
6995 else if (SvPVX(sstr) && SvLEN(sstr))
6996 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
6998 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
7001 SvANY(dstr) = new_XPVBM();
7002 SvCUR(dstr) = SvCUR(sstr);
7003 SvLEN(dstr) = SvLEN(sstr);
7004 SvIVX(dstr) = SvIVX(sstr);
7005 SvNVX(dstr) = SvNVX(sstr);
7006 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
7007 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
7009 SvRV(dstr) = sv_dup_inc(SvRV(sstr));
7010 else if (SvPVX(sstr) && SvLEN(sstr))
7011 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
7013 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
7014 BmRARE(dstr) = BmRARE(sstr);
7015 BmUSEFUL(dstr) = BmUSEFUL(sstr);
7016 BmPREVIOUS(dstr)= BmPREVIOUS(sstr);
7019 SvANY(dstr) = new_XPVLV();
7020 SvCUR(dstr) = SvCUR(sstr);
7021 SvLEN(dstr) = SvLEN(sstr);
7022 SvIVX(dstr) = SvIVX(sstr);
7023 SvNVX(dstr) = SvNVX(sstr);
7024 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
7025 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
7027 SvRV(dstr) = sv_dup_inc(SvRV(sstr));
7028 else if (SvPVX(sstr) && SvLEN(sstr))
7029 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
7031 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
7032 LvTARGOFF(dstr) = LvTARGOFF(sstr); /* XXX sometimes holds PMOP* when DEBUGGING */
7033 LvTARGLEN(dstr) = LvTARGLEN(sstr);
7034 LvTARG(dstr) = sv_dup_inc(LvTARG(sstr));
7035 LvTYPE(dstr) = LvTYPE(sstr);
7038 SvANY(dstr) = new_XPVGV();
7039 SvCUR(dstr) = SvCUR(sstr);
7040 SvLEN(dstr) = SvLEN(sstr);
7041 SvIVX(dstr) = SvIVX(sstr);
7042 SvNVX(dstr) = SvNVX(sstr);
7043 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
7044 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
7046 SvRV(dstr) = sv_dup_inc(SvRV(sstr));
7047 else if (SvPVX(sstr) && SvLEN(sstr))
7048 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
7050 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
7051 GvNAMELEN(dstr) = GvNAMELEN(sstr);
7052 GvNAME(dstr) = SAVEPVN(GvNAME(sstr), GvNAMELEN(sstr));
7053 GvSTASH(dstr) = hv_dup_inc(GvSTASH(sstr));
7054 GvFLAGS(dstr) = GvFLAGS(sstr);
7055 GvGP(dstr) = gp_dup(GvGP(sstr));
7056 (void)GpREFCNT_inc(GvGP(dstr));
7059 SvANY(dstr) = new_XPVIO();
7060 SvCUR(dstr) = SvCUR(sstr);
7061 SvLEN(dstr) = SvLEN(sstr);
7062 SvIVX(dstr) = SvIVX(sstr);
7063 SvNVX(dstr) = SvNVX(sstr);
7064 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
7065 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
7067 SvRV(dstr) = sv_dup_inc(SvRV(sstr));
7068 else if (SvPVX(sstr) && SvLEN(sstr))
7069 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
7071 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
7072 IoIFP(dstr) = fp_dup(IoIFP(sstr), IoTYPE(sstr));
7073 if (IoOFP(sstr) == IoIFP(sstr))
7074 IoOFP(dstr) = IoIFP(dstr);
7076 IoOFP(dstr) = fp_dup(IoOFP(sstr), IoTYPE(sstr));
7077 /* PL_rsfp_filters entries have fake IoDIRP() */
7078 if (IoDIRP(sstr) && !(IoFLAGS(sstr) & IOf_FAKE_DIRP))
7079 IoDIRP(dstr) = dirp_dup(IoDIRP(sstr));
7081 IoDIRP(dstr) = IoDIRP(sstr);
7082 IoLINES(dstr) = IoLINES(sstr);
7083 IoPAGE(dstr) = IoPAGE(sstr);
7084 IoPAGE_LEN(dstr) = IoPAGE_LEN(sstr);
7085 IoLINES_LEFT(dstr) = IoLINES_LEFT(sstr);
7086 IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(sstr));
7087 IoTOP_GV(dstr) = gv_dup(IoTOP_GV(sstr));
7088 IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(sstr));
7089 IoFMT_GV(dstr) = gv_dup(IoFMT_GV(sstr));
7090 IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(sstr));
7091 IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(sstr));
7092 IoSUBPROCESS(dstr) = IoSUBPROCESS(sstr);
7093 IoTYPE(dstr) = IoTYPE(sstr);
7094 IoFLAGS(dstr) = IoFLAGS(sstr);
7097 SvANY(dstr) = new_XPVAV();
7098 SvCUR(dstr) = SvCUR(sstr);
7099 SvLEN(dstr) = SvLEN(sstr);
7100 SvIVX(dstr) = SvIVX(sstr);
7101 SvNVX(dstr) = SvNVX(sstr);
7102 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
7103 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
7104 AvARYLEN((AV*)dstr) = sv_dup_inc(AvARYLEN((AV*)sstr));
7105 AvFLAGS((AV*)dstr) = AvFLAGS((AV*)sstr);
7106 if (AvARRAY((AV*)sstr)) {
7107 SV **dst_ary, **src_ary;
7108 SSize_t items = AvFILLp((AV*)sstr) + 1;
7110 src_ary = AvARRAY((AV*)sstr);
7111 Newz(0, dst_ary, AvMAX((AV*)sstr)+1, SV*);
7112 ptr_table_store(PL_ptr_table, src_ary, dst_ary);
7113 SvPVX(dstr) = (char*)dst_ary;
7114 AvALLOC((AV*)dstr) = dst_ary;
7115 if (AvREAL((AV*)sstr)) {
7117 *dst_ary++ = sv_dup_inc(*src_ary++);
7121 *dst_ary++ = sv_dup(*src_ary++);
7123 items = AvMAX((AV*)sstr) - AvFILLp((AV*)sstr);
7124 while (items-- > 0) {
7125 *dst_ary++ = &PL_sv_undef;
7129 SvPVX(dstr) = Nullch;
7130 AvALLOC((AV*)dstr) = (SV**)NULL;
7134 SvANY(dstr) = new_XPVHV();
7135 SvCUR(dstr) = SvCUR(sstr);
7136 SvLEN(dstr) = SvLEN(sstr);
7137 SvIVX(dstr) = SvIVX(sstr);
7138 SvNVX(dstr) = SvNVX(sstr);
7139 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
7140 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
7141 HvRITER((HV*)dstr) = HvRITER((HV*)sstr);
7142 if (HvARRAY((HV*)sstr)) {
7144 XPVHV *dxhv = (XPVHV*)SvANY(dstr);
7145 XPVHV *sxhv = (XPVHV*)SvANY(sstr);
7146 Newz(0, dxhv->xhv_array,
7147 PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1), char);
7148 while (i <= sxhv->xhv_max) {
7149 ((HE**)dxhv->xhv_array)[i] = he_dup(((HE**)sxhv->xhv_array)[i],
7150 !!HvSHAREKEYS(sstr));
7153 dxhv->xhv_eiter = he_dup(sxhv->xhv_eiter, !!HvSHAREKEYS(sstr));
7156 SvPVX(dstr) = Nullch;
7157 HvEITER((HV*)dstr) = (HE*)NULL;
7159 HvPMROOT((HV*)dstr) = HvPMROOT((HV*)sstr); /* XXX */
7160 HvNAME((HV*)dstr) = SAVEPV(HvNAME((HV*)sstr));
7163 SvANY(dstr) = new_XPVFM();
7164 FmLINES(dstr) = FmLINES(sstr);
7168 SvANY(dstr) = new_XPVCV();
7170 SvCUR(dstr) = SvCUR(sstr);
7171 SvLEN(dstr) = SvLEN(sstr);
7172 SvIVX(dstr) = SvIVX(sstr);
7173 SvNVX(dstr) = SvNVX(sstr);
7174 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
7175 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
7176 if (SvPVX(sstr) && SvLEN(sstr))
7177 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
7179 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
7180 CvSTASH(dstr) = hv_dup(CvSTASH(sstr));/* NOTE: not refcounted */
7181 CvSTART(dstr) = CvSTART(sstr);
7182 CvROOT(dstr) = OpREFCNT_inc(CvROOT(sstr));
7183 CvXSUB(dstr) = CvXSUB(sstr);
7184 CvXSUBANY(dstr) = CvXSUBANY(sstr);
7185 CvGV(dstr) = gv_dup_inc(CvGV(sstr));
7186 CvDEPTH(dstr) = CvDEPTH(sstr);
7187 if (CvPADLIST(sstr) && !AvREAL(CvPADLIST(sstr))) {
7188 /* XXX padlists are real, but pretend to be not */
7189 AvREAL_on(CvPADLIST(sstr));
7190 CvPADLIST(dstr) = av_dup_inc(CvPADLIST(sstr));
7191 AvREAL_off(CvPADLIST(sstr));
7192 AvREAL_off(CvPADLIST(dstr));
7195 CvPADLIST(dstr) = av_dup_inc(CvPADLIST(sstr));
7196 CvOUTSIDE(dstr) = cv_dup_inc(CvOUTSIDE(sstr));
7197 CvFLAGS(dstr) = CvFLAGS(sstr);
7200 Perl_croak(aTHX_ "Bizarre SvTYPE [%d]", SvTYPE(sstr));
7204 if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO)
7211 Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max)
7216 return (PERL_CONTEXT*)NULL;
7218 /* look for it in the table first */
7219 ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs);
7223 /* create anew and remember what it is */
7224 Newz(56, ncxs, max + 1, PERL_CONTEXT);
7225 ptr_table_store(PL_ptr_table, cxs, ncxs);
7228 PERL_CONTEXT *cx = &cxs[ix];
7229 PERL_CONTEXT *ncx = &ncxs[ix];
7230 ncx->cx_type = cx->cx_type;
7231 if (CxTYPE(cx) == CXt_SUBST) {
7232 Perl_croak(aTHX_ "Cloning substitution context is unimplemented");
7235 ncx->blk_oldsp = cx->blk_oldsp;
7236 ncx->blk_oldcop = cx->blk_oldcop;
7237 ncx->blk_oldretsp = cx->blk_oldretsp;
7238 ncx->blk_oldmarksp = cx->blk_oldmarksp;
7239 ncx->blk_oldscopesp = cx->blk_oldscopesp;
7240 ncx->blk_oldpm = cx->blk_oldpm;
7241 ncx->blk_gimme = cx->blk_gimme;
7242 switch (CxTYPE(cx)) {
7244 ncx->blk_sub.cv = (cx->blk_sub.olddepth == 0
7245 ? cv_dup_inc(cx->blk_sub.cv)
7246 : cv_dup(cx->blk_sub.cv));
7247 ncx->blk_sub.argarray = (cx->blk_sub.hasargs
7248 ? av_dup_inc(cx->blk_sub.argarray)
7250 ncx->blk_sub.savearray = av_dup(cx->blk_sub.savearray);
7251 ncx->blk_sub.olddepth = cx->blk_sub.olddepth;
7252 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
7253 ncx->blk_sub.lval = cx->blk_sub.lval;
7256 ncx->blk_eval.old_in_eval = cx->blk_eval.old_in_eval;
7257 ncx->blk_eval.old_op_type = cx->blk_eval.old_op_type;
7258 ncx->blk_eval.old_namesv = sv_dup_inc(cx->blk_eval.old_namesv);
7259 ncx->blk_eval.old_eval_root = cx->blk_eval.old_eval_root;
7260 ncx->blk_eval.cur_text = sv_dup(cx->blk_eval.cur_text);
7263 ncx->blk_loop.label = cx->blk_loop.label;
7264 ncx->blk_loop.resetsp = cx->blk_loop.resetsp;
7265 ncx->blk_loop.redo_op = cx->blk_loop.redo_op;
7266 ncx->blk_loop.next_op = cx->blk_loop.next_op;
7267 ncx->blk_loop.last_op = cx->blk_loop.last_op;
7268 ncx->blk_loop.iterdata = (CxPADLOOP(cx)
7269 ? cx->blk_loop.iterdata
7270 : gv_dup((GV*)cx->blk_loop.iterdata));
7271 ncx->blk_loop.oldcurpad
7272 = (SV**)ptr_table_fetch(PL_ptr_table,
7273 cx->blk_loop.oldcurpad);
7274 ncx->blk_loop.itersave = sv_dup_inc(cx->blk_loop.itersave);
7275 ncx->blk_loop.iterlval = sv_dup_inc(cx->blk_loop.iterlval);
7276 ncx->blk_loop.iterary = av_dup_inc(cx->blk_loop.iterary);
7277 ncx->blk_loop.iterix = cx->blk_loop.iterix;
7278 ncx->blk_loop.itermax = cx->blk_loop.itermax;
7281 ncx->blk_sub.cv = cv_dup(cx->blk_sub.cv);
7282 ncx->blk_sub.gv = gv_dup(cx->blk_sub.gv);
7283 ncx->blk_sub.dfoutgv = gv_dup_inc(cx->blk_sub.dfoutgv);
7284 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
7297 Perl_si_dup(pTHX_ PERL_SI *si)
7302 return (PERL_SI*)NULL;
7304 /* look for it in the table first */
7305 nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si);
7309 /* create anew and remember what it is */
7310 Newz(56, nsi, 1, PERL_SI);
7311 ptr_table_store(PL_ptr_table, si, nsi);
7313 nsi->si_stack = av_dup_inc(si->si_stack);
7314 nsi->si_cxix = si->si_cxix;
7315 nsi->si_cxmax = si->si_cxmax;
7316 nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax);
7317 nsi->si_type = si->si_type;
7318 nsi->si_prev = si_dup(si->si_prev);
7319 nsi->si_next = si_dup(si->si_next);
7320 nsi->si_markoff = si->si_markoff;
7325 #define POPINT(ss,ix) ((ss)[--(ix)].any_i32)
7326 #define TOPINT(ss,ix) ((ss)[ix].any_i32)
7327 #define POPLONG(ss,ix) ((ss)[--(ix)].any_long)
7328 #define TOPLONG(ss,ix) ((ss)[ix].any_long)
7329 #define POPIV(ss,ix) ((ss)[--(ix)].any_iv)
7330 #define TOPIV(ss,ix) ((ss)[ix].any_iv)
7331 #define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr)
7332 #define TOPPTR(ss,ix) ((ss)[ix].any_ptr)
7333 #define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr)
7334 #define TOPDPTR(ss,ix) ((ss)[ix].any_dptr)
7335 #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr)
7336 #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr)
7339 #define pv_dup_inc(p) SAVEPV(p)
7340 #define pv_dup(p) SAVEPV(p)
7341 #define svp_dup_inc(p,pp) any_dup(p,pp)
7344 Perl_any_dup(pTHX_ void *v, PerlInterpreter *proto_perl)
7351 /* look for it in the table first */
7352 ret = ptr_table_fetch(PL_ptr_table, v);
7356 /* see if it is part of the interpreter structure */
7357 if (v >= (void*)proto_perl && v < (void*)(proto_perl+1))
7358 ret = (void*)(((char*)aTHXo) + (((char*)v) - (char*)proto_perl));
7366 Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl)
7368 ANY *ss = proto_perl->Tsavestack;
7369 I32 ix = proto_perl->Tsavestack_ix;
7370 I32 max = proto_perl->Tsavestack_max;
7383 void (*dptr) (void*);
7384 void (*dxptr) (pTHXo_ void*);
7387 Newz(54, nss, max, ANY);
7393 case SAVEt_ITEM: /* normal string */
7394 sv = (SV*)POPPTR(ss,ix);
7395 TOPPTR(nss,ix) = sv_dup_inc(sv);
7396 sv = (SV*)POPPTR(ss,ix);
7397 TOPPTR(nss,ix) = sv_dup_inc(sv);
7399 case SAVEt_SV: /* scalar reference */
7400 sv = (SV*)POPPTR(ss,ix);
7401 TOPPTR(nss,ix) = sv_dup_inc(sv);
7402 gv = (GV*)POPPTR(ss,ix);
7403 TOPPTR(nss,ix) = gv_dup_inc(gv);
7405 case SAVEt_GENERIC_PVREF: /* generic char* */
7406 c = (char*)POPPTR(ss,ix);
7407 TOPPTR(nss,ix) = pv_dup(c);
7408 ptr = POPPTR(ss,ix);
7409 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
7411 case SAVEt_GENERIC_SVREF: /* generic sv */
7412 case SAVEt_SVREF: /* scalar reference */
7413 sv = (SV*)POPPTR(ss,ix);
7414 TOPPTR(nss,ix) = sv_dup_inc(sv);
7415 ptr = POPPTR(ss,ix);
7416 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
7418 case SAVEt_AV: /* array reference */
7419 av = (AV*)POPPTR(ss,ix);
7420 TOPPTR(nss,ix) = av_dup_inc(av);
7421 gv = (GV*)POPPTR(ss,ix);
7422 TOPPTR(nss,ix) = gv_dup(gv);
7424 case SAVEt_HV: /* hash reference */
7425 hv = (HV*)POPPTR(ss,ix);
7426 TOPPTR(nss,ix) = hv_dup_inc(hv);
7427 gv = (GV*)POPPTR(ss,ix);
7428 TOPPTR(nss,ix) = gv_dup(gv);
7430 case SAVEt_INT: /* int reference */
7431 ptr = POPPTR(ss,ix);
7432 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
7433 intval = (int)POPINT(ss,ix);
7434 TOPINT(nss,ix) = intval;
7436 case SAVEt_LONG: /* long reference */
7437 ptr = POPPTR(ss,ix);
7438 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
7439 longval = (long)POPLONG(ss,ix);
7440 TOPLONG(nss,ix) = longval;
7442 case SAVEt_I32: /* I32 reference */
7443 case SAVEt_I16: /* I16 reference */
7444 case SAVEt_I8: /* I8 reference */
7445 ptr = POPPTR(ss,ix);
7446 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
7450 case SAVEt_IV: /* IV reference */
7451 ptr = POPPTR(ss,ix);
7452 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
7456 case SAVEt_SPTR: /* SV* reference */
7457 ptr = POPPTR(ss,ix);
7458 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
7459 sv = (SV*)POPPTR(ss,ix);
7460 TOPPTR(nss,ix) = sv_dup(sv);
7462 case SAVEt_VPTR: /* random* reference */
7463 ptr = POPPTR(ss,ix);
7464 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
7465 ptr = POPPTR(ss,ix);
7466 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
7468 case SAVEt_PPTR: /* char* reference */
7469 ptr = POPPTR(ss,ix);
7470 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
7471 c = (char*)POPPTR(ss,ix);
7472 TOPPTR(nss,ix) = pv_dup(c);
7474 case SAVEt_HPTR: /* HV* reference */
7475 ptr = POPPTR(ss,ix);
7476 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
7477 hv = (HV*)POPPTR(ss,ix);
7478 TOPPTR(nss,ix) = hv_dup(hv);
7480 case SAVEt_APTR: /* AV* reference */
7481 ptr = POPPTR(ss,ix);
7482 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
7483 av = (AV*)POPPTR(ss,ix);
7484 TOPPTR(nss,ix) = av_dup(av);
7487 gv = (GV*)POPPTR(ss,ix);
7488 TOPPTR(nss,ix) = gv_dup(gv);
7490 case SAVEt_GP: /* scalar reference */
7491 gp = (GP*)POPPTR(ss,ix);
7492 TOPPTR(nss,ix) = gp = gp_dup(gp);
7493 (void)GpREFCNT_inc(gp);
7494 gv = (GV*)POPPTR(ss,ix);
7495 TOPPTR(nss,ix) = gv_dup_inc(c);
7496 c = (char*)POPPTR(ss,ix);
7497 TOPPTR(nss,ix) = pv_dup(c);
7504 sv = (SV*)POPPTR(ss,ix);
7505 TOPPTR(nss,ix) = sv_dup_inc(sv);
7508 ptr = POPPTR(ss,ix);
7509 if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) {
7510 /* these are assumed to be refcounted properly */
7511 switch (((OP*)ptr)->op_type) {
7518 TOPPTR(nss,ix) = ptr;
7523 TOPPTR(nss,ix) = Nullop;
7528 TOPPTR(nss,ix) = Nullop;
7531 c = (char*)POPPTR(ss,ix);
7532 TOPPTR(nss,ix) = pv_dup_inc(c);
7535 longval = POPLONG(ss,ix);
7536 TOPLONG(nss,ix) = longval;
7539 hv = (HV*)POPPTR(ss,ix);
7540 TOPPTR(nss,ix) = hv_dup_inc(hv);
7541 c = (char*)POPPTR(ss,ix);
7542 TOPPTR(nss,ix) = pv_dup_inc(c);
7546 case SAVEt_DESTRUCTOR:
7547 ptr = POPPTR(ss,ix);
7548 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
7549 dptr = POPDPTR(ss,ix);
7550 TOPDPTR(nss,ix) = (void (*)(void*))any_dup((void *)dptr, proto_perl);
7552 case SAVEt_DESTRUCTOR_X:
7553 ptr = POPPTR(ss,ix);
7554 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
7555 dxptr = POPDXPTR(ss,ix);
7556 TOPDXPTR(nss,ix) = (void (*)(pTHXo_ void*))any_dup((void *)dxptr, proto_perl);
7558 case SAVEt_REGCONTEXT:
7564 case SAVEt_STACK_POS: /* Position on Perl stack */
7568 case SAVEt_AELEM: /* array element */
7569 sv = (SV*)POPPTR(ss,ix);
7570 TOPPTR(nss,ix) = sv_dup_inc(sv);
7573 av = (AV*)POPPTR(ss,ix);
7574 TOPPTR(nss,ix) = av_dup_inc(av);
7576 case SAVEt_HELEM: /* hash element */
7577 sv = (SV*)POPPTR(ss,ix);
7578 TOPPTR(nss,ix) = sv_dup_inc(sv);
7579 sv = (SV*)POPPTR(ss,ix);
7580 TOPPTR(nss,ix) = sv_dup_inc(sv);
7581 hv = (HV*)POPPTR(ss,ix);
7582 TOPPTR(nss,ix) = hv_dup_inc(hv);
7585 ptr = POPPTR(ss,ix);
7586 TOPPTR(nss,ix) = ptr;
7593 av = (AV*)POPPTR(ss,ix);
7594 TOPPTR(nss,ix) = av_dup(av);
7597 Perl_croak(aTHX_ "panic: ss_dup inconsistency");
7609 perl_clone(PerlInterpreter *proto_perl, UV flags)
7612 CPerlObj *pPerl = (CPerlObj*)proto_perl;
7615 #ifdef PERL_IMPLICIT_SYS
7616 return perl_clone_using(proto_perl, flags,
7618 proto_perl->IMemShared,
7619 proto_perl->IMemParse,
7629 perl_clone_using(PerlInterpreter *proto_perl, UV flags,
7630 struct IPerlMem* ipM, struct IPerlMem* ipMS,
7631 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
7632 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
7633 struct IPerlDir* ipD, struct IPerlSock* ipS,
7634 struct IPerlProc* ipP)
7636 /* XXX many of the string copies here can be optimized if they're
7637 * constants; they need to be allocated as common memory and just
7638 * their pointers copied. */
7642 CPerlObj *pPerl = new(ipM) CPerlObj(ipM, ipMS, ipMP, ipE, ipStd, ipLIO,
7644 PERL_SET_THX(pPerl);
7645 # else /* !PERL_OBJECT */
7646 PerlInterpreter *my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
7647 PERL_SET_THX(my_perl);
7650 memset(my_perl, 0xab, sizeof(PerlInterpreter));
7655 # else /* !DEBUGGING */
7656 Zero(my_perl, 1, PerlInterpreter);
7657 # endif /* DEBUGGING */
7661 PL_MemShared = ipMS;
7669 # endif /* PERL_OBJECT */
7670 #else /* !PERL_IMPLICIT_SYS */
7672 PerlInterpreter *my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
7673 PERL_SET_THX(my_perl);
7676 memset(my_perl, 0xab, sizeof(PerlInterpreter));
7681 # else /* !DEBUGGING */
7682 Zero(my_perl, 1, PerlInterpreter);
7683 # endif /* DEBUGGING */
7684 #endif /* PERL_IMPLICIT_SYS */
7687 PL_xiv_arenaroot = NULL;
7689 PL_xnv_arenaroot = NULL;
7691 PL_xrv_arenaroot = NULL;
7693 PL_xpv_arenaroot = NULL;
7695 PL_xpviv_arenaroot = NULL;
7696 PL_xpviv_root = NULL;
7697 PL_xpvnv_arenaroot = NULL;
7698 PL_xpvnv_root = NULL;
7699 PL_xpvcv_arenaroot = NULL;
7700 PL_xpvcv_root = NULL;
7701 PL_xpvav_arenaroot = NULL;
7702 PL_xpvav_root = NULL;
7703 PL_xpvhv_arenaroot = NULL;
7704 PL_xpvhv_root = NULL;
7705 PL_xpvmg_arenaroot = NULL;
7706 PL_xpvmg_root = NULL;
7707 PL_xpvlv_arenaroot = NULL;
7708 PL_xpvlv_root = NULL;
7709 PL_xpvbm_arenaroot = NULL;
7710 PL_xpvbm_root = NULL;
7711 PL_he_arenaroot = NULL;
7713 PL_nice_chunk = NULL;
7714 PL_nice_chunk_size = 0;
7717 PL_sv_root = Nullsv;
7718 PL_sv_arenaroot = Nullsv;
7720 PL_debug = proto_perl->Idebug;
7722 /* create SV map for pointer relocation */
7723 PL_ptr_table = ptr_table_new();
7725 /* initialize these special pointers as early as possible */
7726 SvANY(&PL_sv_undef) = NULL;
7727 SvREFCNT(&PL_sv_undef) = (~(U32)0)/2;
7728 SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL;
7729 ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef);
7732 SvUPGRADE(&PL_sv_no, SVt_PVNV);
7734 SvANY(&PL_sv_no) = new_XPVNV();
7736 SvREFCNT(&PL_sv_no) = (~(U32)0)/2;
7737 SvFLAGS(&PL_sv_no) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
7738 SvPVX(&PL_sv_no) = SAVEPVN(PL_No, 0);
7739 SvCUR(&PL_sv_no) = 0;
7740 SvLEN(&PL_sv_no) = 1;
7741 SvNVX(&PL_sv_no) = 0;
7742 ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no);
7745 SvUPGRADE(&PL_sv_yes, SVt_PVNV);
7747 SvANY(&PL_sv_yes) = new_XPVNV();
7749 SvREFCNT(&PL_sv_yes) = (~(U32)0)/2;
7750 SvFLAGS(&PL_sv_yes) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
7751 SvPVX(&PL_sv_yes) = SAVEPVN(PL_Yes, 1);
7752 SvCUR(&PL_sv_yes) = 1;
7753 SvLEN(&PL_sv_yes) = 2;
7754 SvNVX(&PL_sv_yes) = 1;
7755 ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes);
7757 /* create shared string table */
7758 PL_strtab = newHV();
7759 HvSHAREKEYS_off(PL_strtab);
7760 hv_ksplit(PL_strtab, 512);
7761 ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab);
7763 PL_compiling = proto_perl->Icompiling;
7764 PL_compiling.cop_stashpv = SAVEPV(PL_compiling.cop_stashpv);
7765 PL_compiling.cop_file = SAVEPV(PL_compiling.cop_file);
7766 ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling);
7767 if (!specialWARN(PL_compiling.cop_warnings))
7768 PL_compiling.cop_warnings = sv_dup_inc(PL_compiling.cop_warnings);
7769 PL_curcop = (COP*)any_dup(proto_perl->Tcurcop, proto_perl);
7771 /* pseudo environmental stuff */
7772 PL_origargc = proto_perl->Iorigargc;
7774 New(0, PL_origargv, i+1, char*);
7775 PL_origargv[i] = '\0';
7777 PL_origargv[i] = SAVEPV(proto_perl->Iorigargv[i]);
7779 PL_envgv = gv_dup(proto_perl->Ienvgv);
7780 PL_incgv = gv_dup(proto_perl->Iincgv);
7781 PL_hintgv = gv_dup(proto_perl->Ihintgv);
7782 PL_origfilename = SAVEPV(proto_perl->Iorigfilename);
7783 PL_diehook = sv_dup_inc(proto_perl->Idiehook);
7784 PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook);
7787 PL_minus_c = proto_perl->Iminus_c;
7788 PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel);
7789 PL_localpatches = proto_perl->Ilocalpatches;
7790 PL_splitstr = proto_perl->Isplitstr;
7791 PL_preprocess = proto_perl->Ipreprocess;
7792 PL_minus_n = proto_perl->Iminus_n;
7793 PL_minus_p = proto_perl->Iminus_p;
7794 PL_minus_l = proto_perl->Iminus_l;
7795 PL_minus_a = proto_perl->Iminus_a;
7796 PL_minus_F = proto_perl->Iminus_F;
7797 PL_doswitches = proto_perl->Idoswitches;
7798 PL_dowarn = proto_perl->Idowarn;
7799 PL_doextract = proto_perl->Idoextract;
7800 PL_sawampersand = proto_perl->Isawampersand;
7801 PL_unsafe = proto_perl->Iunsafe;
7802 PL_inplace = SAVEPV(proto_perl->Iinplace);
7803 PL_e_script = sv_dup_inc(proto_perl->Ie_script);
7804 PL_perldb = proto_perl->Iperldb;
7805 PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
7807 /* magical thingies */
7808 /* XXX time(&PL_basetime) when asked for? */
7809 PL_basetime = proto_perl->Ibasetime;
7810 PL_formfeed = sv_dup(proto_perl->Iformfeed);
7812 PL_maxsysfd = proto_perl->Imaxsysfd;
7813 PL_multiline = proto_perl->Imultiline;
7814 PL_statusvalue = proto_perl->Istatusvalue;
7816 PL_statusvalue_vms = proto_perl->Istatusvalue_vms;
7819 /* shortcuts to various I/O objects */
7820 PL_stdingv = gv_dup(proto_perl->Istdingv);
7821 PL_stderrgv = gv_dup(proto_perl->Istderrgv);
7822 PL_defgv = gv_dup(proto_perl->Idefgv);
7823 PL_argvgv = gv_dup(proto_perl->Iargvgv);
7824 PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv);
7825 PL_argvout_stack = av_dup(proto_perl->Iargvout_stack);
7827 /* shortcuts to regexp stuff */
7828 PL_replgv = gv_dup(proto_perl->Ireplgv);
7830 /* shortcuts to misc objects */
7831 PL_errgv = gv_dup(proto_perl->Ierrgv);
7833 /* shortcuts to debugging objects */
7834 PL_DBgv = gv_dup(proto_perl->IDBgv);
7835 PL_DBline = gv_dup(proto_perl->IDBline);
7836 PL_DBsub = gv_dup(proto_perl->IDBsub);
7837 PL_DBsingle = sv_dup(proto_perl->IDBsingle);
7838 PL_DBtrace = sv_dup(proto_perl->IDBtrace);
7839 PL_DBsignal = sv_dup(proto_perl->IDBsignal);
7840 PL_lineary = av_dup(proto_perl->Ilineary);
7841 PL_dbargs = av_dup(proto_perl->Idbargs);
7844 PL_defstash = hv_dup_inc(proto_perl->Tdefstash);
7845 PL_curstash = hv_dup(proto_perl->Tcurstash);
7846 PL_debstash = hv_dup(proto_perl->Idebstash);
7847 PL_globalstash = hv_dup(proto_perl->Iglobalstash);
7848 PL_curstname = sv_dup_inc(proto_perl->Icurstname);
7850 PL_beginav = av_dup_inc(proto_perl->Ibeginav);
7851 PL_endav = av_dup_inc(proto_perl->Iendav);
7852 PL_checkav = av_dup_inc(proto_perl->Icheckav);
7853 PL_initav = av_dup_inc(proto_perl->Iinitav);
7855 PL_sub_generation = proto_perl->Isub_generation;
7857 /* funky return mechanisms */
7858 PL_forkprocess = proto_perl->Iforkprocess;
7860 /* subprocess state */
7861 PL_fdpid = av_dup_inc(proto_perl->Ifdpid);
7863 /* internal state */
7864 PL_tainting = proto_perl->Itainting;
7865 PL_maxo = proto_perl->Imaxo;
7866 if (proto_perl->Iop_mask)
7867 PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo);
7869 PL_op_mask = Nullch;
7871 /* current interpreter roots */
7872 PL_main_cv = cv_dup_inc(proto_perl->Imain_cv);
7873 PL_main_root = OpREFCNT_inc(proto_perl->Imain_root);
7874 PL_main_start = proto_perl->Imain_start;
7875 PL_eval_root = proto_perl->Ieval_root;
7876 PL_eval_start = proto_perl->Ieval_start;
7878 /* runtime control stuff */
7879 PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl);
7880 PL_copline = proto_perl->Icopline;
7882 PL_filemode = proto_perl->Ifilemode;
7883 PL_lastfd = proto_perl->Ilastfd;
7884 PL_oldname = proto_perl->Ioldname; /* XXX not quite right */
7887 PL_gensym = proto_perl->Igensym;
7888 PL_preambled = proto_perl->Ipreambled;
7889 PL_preambleav = av_dup_inc(proto_perl->Ipreambleav);
7890 PL_laststatval = proto_perl->Ilaststatval;
7891 PL_laststype = proto_perl->Ilaststype;
7892 PL_mess_sv = Nullsv;
7894 PL_orslen = proto_perl->Iorslen;
7895 PL_ors = SAVEPVN(proto_perl->Iors, PL_orslen);
7896 PL_ofmt = SAVEPV(proto_perl->Iofmt);
7898 /* interpreter atexit processing */
7899 PL_exitlistlen = proto_perl->Iexitlistlen;
7900 if (PL_exitlistlen) {
7901 New(0, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
7902 Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
7905 PL_exitlist = (PerlExitListEntry*)NULL;
7906 PL_modglobal = hv_dup_inc(proto_perl->Imodglobal);
7908 PL_profiledata = NULL;
7909 PL_rsfp = fp_dup(proto_perl->Irsfp, '<');
7910 /* PL_rsfp_filters entries have fake IoDIRP() */
7911 PL_rsfp_filters = av_dup_inc(proto_perl->Irsfp_filters);
7913 PL_compcv = cv_dup(proto_perl->Icompcv);
7914 PL_comppad = av_dup(proto_perl->Icomppad);
7915 PL_comppad_name = av_dup(proto_perl->Icomppad_name);
7916 PL_comppad_name_fill = proto_perl->Icomppad_name_fill;
7917 PL_comppad_name_floor = proto_perl->Icomppad_name_floor;
7918 PL_curpad = (SV**)ptr_table_fetch(PL_ptr_table,
7919 proto_perl->Tcurpad);
7921 #ifdef HAVE_INTERP_INTERN
7922 sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern);
7925 /* more statics moved here */
7926 PL_generation = proto_perl->Igeneration;
7927 PL_DBcv = cv_dup(proto_perl->IDBcv);
7929 PL_in_clean_objs = proto_perl->Iin_clean_objs;
7930 PL_in_clean_all = proto_perl->Iin_clean_all;
7932 PL_uid = proto_perl->Iuid;
7933 PL_euid = proto_perl->Ieuid;
7934 PL_gid = proto_perl->Igid;
7935 PL_egid = proto_perl->Iegid;
7936 PL_nomemok = proto_perl->Inomemok;
7937 PL_an = proto_perl->Ian;
7938 PL_cop_seqmax = proto_perl->Icop_seqmax;
7939 PL_op_seqmax = proto_perl->Iop_seqmax;
7940 PL_evalseq = proto_perl->Ievalseq;
7941 PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */
7942 PL_origalen = proto_perl->Iorigalen;
7943 PL_pidstatus = newHV(); /* XXX flag for cloning? */
7944 PL_osname = SAVEPV(proto_perl->Iosname);
7945 PL_sh_path = SAVEPV(proto_perl->Ish_path);
7946 PL_sighandlerp = proto_perl->Isighandlerp;
7949 PL_runops = proto_perl->Irunops;
7951 Copy(proto_perl->Itokenbuf, PL_tokenbuf, 256, char);
7954 PL_cshlen = proto_perl->Icshlen;
7955 PL_cshname = SAVEPVN(proto_perl->Icshname, PL_cshlen);
7958 PL_lex_state = proto_perl->Ilex_state;
7959 PL_lex_defer = proto_perl->Ilex_defer;
7960 PL_lex_expect = proto_perl->Ilex_expect;
7961 PL_lex_formbrack = proto_perl->Ilex_formbrack;
7962 PL_lex_dojoin = proto_perl->Ilex_dojoin;
7963 PL_lex_starts = proto_perl->Ilex_starts;
7964 PL_lex_stuff = sv_dup_inc(proto_perl->Ilex_stuff);
7965 PL_lex_repl = sv_dup_inc(proto_perl->Ilex_repl);
7966 PL_lex_op = proto_perl->Ilex_op;
7967 PL_lex_inpat = proto_perl->Ilex_inpat;
7968 PL_lex_inwhat = proto_perl->Ilex_inwhat;
7969 PL_lex_brackets = proto_perl->Ilex_brackets;
7970 i = (PL_lex_brackets < 120 ? 120 : PL_lex_brackets);
7971 PL_lex_brackstack = SAVEPVN(proto_perl->Ilex_brackstack,i);
7972 PL_lex_casemods = proto_perl->Ilex_casemods;
7973 i = (PL_lex_casemods < 12 ? 12 : PL_lex_casemods);
7974 PL_lex_casestack = SAVEPVN(proto_perl->Ilex_casestack,i);
7976 Copy(proto_perl->Inextval, PL_nextval, 5, YYSTYPE);
7977 Copy(proto_perl->Inexttype, PL_nexttype, 5, I32);
7978 PL_nexttoke = proto_perl->Inexttoke;
7980 PL_linestr = sv_dup_inc(proto_perl->Ilinestr);
7981 i = proto_perl->Ibufptr - SvPVX(proto_perl->Ilinestr);
7982 PL_bufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
7983 i = proto_perl->Ioldbufptr - SvPVX(proto_perl->Ilinestr);
7984 PL_oldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
7985 i = proto_perl->Ioldoldbufptr - SvPVX(proto_perl->Ilinestr);
7986 PL_oldoldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
7987 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
7988 i = proto_perl->Ilinestart - SvPVX(proto_perl->Ilinestr);
7989 PL_linestart = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
7990 PL_pending_ident = proto_perl->Ipending_ident;
7991 PL_sublex_info = proto_perl->Isublex_info; /* XXX not quite right */
7993 PL_expect = proto_perl->Iexpect;
7995 PL_multi_start = proto_perl->Imulti_start;
7996 PL_multi_end = proto_perl->Imulti_end;
7997 PL_multi_open = proto_perl->Imulti_open;
7998 PL_multi_close = proto_perl->Imulti_close;
8000 PL_error_count = proto_perl->Ierror_count;
8001 PL_subline = proto_perl->Isubline;
8002 PL_subname = sv_dup_inc(proto_perl->Isubname);
8004 PL_min_intro_pending = proto_perl->Imin_intro_pending;
8005 PL_max_intro_pending = proto_perl->Imax_intro_pending;
8006 PL_padix = proto_perl->Ipadix;
8007 PL_padix_floor = proto_perl->Ipadix_floor;
8008 PL_pad_reset_pending = proto_perl->Ipad_reset_pending;
8010 i = proto_perl->Ilast_uni - SvPVX(proto_perl->Ilinestr);
8011 PL_last_uni = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
8012 i = proto_perl->Ilast_lop - SvPVX(proto_perl->Ilinestr);
8013 PL_last_lop = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
8014 PL_last_lop_op = proto_perl->Ilast_lop_op;
8015 PL_in_my = proto_perl->Iin_my;
8016 PL_in_my_stash = hv_dup(proto_perl->Iin_my_stash);
8018 PL_cryptseen = proto_perl->Icryptseen;
8021 PL_hints = proto_perl->Ihints;
8023 PL_amagic_generation = proto_perl->Iamagic_generation;
8025 #ifdef USE_LOCALE_COLLATE
8026 PL_collation_ix = proto_perl->Icollation_ix;
8027 PL_collation_name = SAVEPV(proto_perl->Icollation_name);
8028 PL_collation_standard = proto_perl->Icollation_standard;
8029 PL_collxfrm_base = proto_perl->Icollxfrm_base;
8030 PL_collxfrm_mult = proto_perl->Icollxfrm_mult;
8031 #endif /* USE_LOCALE_COLLATE */
8033 #ifdef USE_LOCALE_NUMERIC
8034 PL_numeric_name = SAVEPV(proto_perl->Inumeric_name);
8035 PL_numeric_standard = proto_perl->Inumeric_standard;
8036 PL_numeric_local = proto_perl->Inumeric_local;
8037 PL_numeric_radix = proto_perl->Inumeric_radix;
8038 #endif /* !USE_LOCALE_NUMERIC */
8040 /* utf8 character classes */
8041 PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum);
8042 PL_utf8_alnumc = sv_dup_inc(proto_perl->Iutf8_alnumc);
8043 PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii);
8044 PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha);
8045 PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space);
8046 PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl);
8047 PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph);
8048 PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit);
8049 PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper);
8050 PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower);
8051 PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print);
8052 PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct);
8053 PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit);
8054 PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark);
8055 PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper);
8056 PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle);
8057 PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower);
8060 PL_last_swash_hv = Nullhv; /* reinits on demand */
8061 PL_last_swash_klen = 0;
8062 PL_last_swash_key[0]= '\0';
8063 PL_last_swash_tmps = (U8*)NULL;
8064 PL_last_swash_slen = 0;
8066 /* perly.c globals */
8067 PL_yydebug = proto_perl->Iyydebug;
8068 PL_yynerrs = proto_perl->Iyynerrs;
8069 PL_yyerrflag = proto_perl->Iyyerrflag;
8070 PL_yychar = proto_perl->Iyychar;
8071 PL_yyval = proto_perl->Iyyval;
8072 PL_yylval = proto_perl->Iyylval;
8074 PL_glob_index = proto_perl->Iglob_index;
8075 PL_srand_called = proto_perl->Isrand_called;
8076 PL_uudmap['M'] = 0; /* reinits on demand */
8077 PL_bitcount = Nullch; /* reinits on demand */
8079 if (proto_perl->Ipsig_ptr) {
8080 int sig_num[] = { SIG_NUM };
8081 Newz(0, PL_psig_ptr, sizeof(sig_num)/sizeof(*sig_num), SV*);
8082 Newz(0, PL_psig_name, sizeof(sig_num)/sizeof(*sig_num), SV*);
8083 for (i = 1; PL_sig_name[i]; i++) {
8084 PL_psig_ptr[i] = sv_dup_inc(proto_perl->Ipsig_ptr[i]);
8085 PL_psig_name[i] = sv_dup_inc(proto_perl->Ipsig_name[i]);
8089 PL_psig_ptr = (SV**)NULL;
8090 PL_psig_name = (SV**)NULL;
8093 /* thrdvar.h stuff */
8096 /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */
8097 PL_tmps_ix = proto_perl->Ttmps_ix;
8098 PL_tmps_max = proto_perl->Ttmps_max;
8099 PL_tmps_floor = proto_perl->Ttmps_floor;
8100 Newz(50, PL_tmps_stack, PL_tmps_max, SV*);
8102 while (i <= PL_tmps_ix) {
8103 PL_tmps_stack[i] = sv_dup_inc(proto_perl->Ttmps_stack[i]);
8107 /* next PUSHMARK() sets *(PL_markstack_ptr+1) */
8108 i = proto_perl->Tmarkstack_max - proto_perl->Tmarkstack;
8109 Newz(54, PL_markstack, i, I32);
8110 PL_markstack_max = PL_markstack + (proto_perl->Tmarkstack_max
8111 - proto_perl->Tmarkstack);
8112 PL_markstack_ptr = PL_markstack + (proto_perl->Tmarkstack_ptr
8113 - proto_perl->Tmarkstack);
8114 Copy(proto_perl->Tmarkstack, PL_markstack,
8115 PL_markstack_ptr - PL_markstack + 1, I32);
8117 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
8118 * NOTE: unlike the others! */
8119 PL_scopestack_ix = proto_perl->Tscopestack_ix;
8120 PL_scopestack_max = proto_perl->Tscopestack_max;
8121 Newz(54, PL_scopestack, PL_scopestack_max, I32);
8122 Copy(proto_perl->Tscopestack, PL_scopestack, PL_scopestack_ix, I32);
8124 /* next push_return() sets PL_retstack[PL_retstack_ix]
8125 * NOTE: unlike the others! */
8126 PL_retstack_ix = proto_perl->Tretstack_ix;
8127 PL_retstack_max = proto_perl->Tretstack_max;
8128 Newz(54, PL_retstack, PL_retstack_max, OP*);
8129 Copy(proto_perl->Tretstack, PL_retstack, PL_retstack_ix, I32);
8131 /* NOTE: si_dup() looks at PL_markstack */
8132 PL_curstackinfo = si_dup(proto_perl->Tcurstackinfo);
8134 /* PL_curstack = PL_curstackinfo->si_stack; */
8135 PL_curstack = av_dup(proto_perl->Tcurstack);
8136 PL_mainstack = av_dup(proto_perl->Tmainstack);
8138 /* next PUSHs() etc. set *(PL_stack_sp+1) */
8139 PL_stack_base = AvARRAY(PL_curstack);
8140 PL_stack_sp = PL_stack_base + (proto_perl->Tstack_sp
8141 - proto_perl->Tstack_base);
8142 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
8144 /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix]
8145 * NOTE: unlike the others! */
8146 PL_savestack_ix = proto_perl->Tsavestack_ix;
8147 PL_savestack_max = proto_perl->Tsavestack_max;
8148 /*Newz(54, PL_savestack, PL_savestack_max, ANY);*/
8149 PL_savestack = ss_dup(proto_perl);
8153 ENTER; /* perl_destruct() wants to LEAVE; */
8156 PL_start_env = proto_perl->Tstart_env; /* XXXXXX */
8157 PL_top_env = &PL_start_env;
8159 PL_op = proto_perl->Top;
8162 PL_Xpv = (XPV*)NULL;
8163 PL_na = proto_perl->Tna;
8165 PL_statbuf = proto_perl->Tstatbuf;
8166 PL_statcache = proto_perl->Tstatcache;
8167 PL_statgv = gv_dup(proto_perl->Tstatgv);
8168 PL_statname = sv_dup_inc(proto_perl->Tstatname);
8170 PL_timesbuf = proto_perl->Ttimesbuf;
8173 PL_tainted = proto_perl->Ttainted;
8174 PL_curpm = proto_perl->Tcurpm; /* XXX No PMOP ref count */
8175 PL_nrs = sv_dup_inc(proto_perl->Tnrs);
8176 PL_rs = sv_dup_inc(proto_perl->Trs);
8177 PL_last_in_gv = gv_dup(proto_perl->Tlast_in_gv);
8178 PL_ofslen = proto_perl->Tofslen;
8179 PL_ofs = SAVEPVN(proto_perl->Tofs, PL_ofslen);
8180 PL_defoutgv = gv_dup_inc(proto_perl->Tdefoutgv);
8181 PL_chopset = proto_perl->Tchopset; /* XXX never deallocated */
8182 PL_toptarget = sv_dup_inc(proto_perl->Ttoptarget);
8183 PL_bodytarget = sv_dup_inc(proto_perl->Tbodytarget);
8184 PL_formtarget = sv_dup(proto_perl->Tformtarget);
8186 PL_restartop = proto_perl->Trestartop;
8187 PL_in_eval = proto_perl->Tin_eval;
8188 PL_delaymagic = proto_perl->Tdelaymagic;
8189 PL_dirty = proto_perl->Tdirty;
8190 PL_localizing = proto_perl->Tlocalizing;
8192 #ifdef PERL_FLEXIBLE_EXCEPTIONS
8193 PL_protect = proto_perl->Tprotect;
8195 PL_errors = sv_dup_inc(proto_perl->Terrors);
8196 PL_av_fetch_sv = Nullsv;
8197 PL_hv_fetch_sv = Nullsv;
8198 Zero(&PL_hv_fetch_ent_mh, 1, HE); /* XXX */
8199 PL_modcount = proto_perl->Tmodcount;
8200 PL_lastgotoprobe = Nullop;
8201 PL_dumpindent = proto_perl->Tdumpindent;
8203 PL_sortcop = (OP*)any_dup(proto_perl->Tsortcop, proto_perl);
8204 PL_sortstash = hv_dup(proto_perl->Tsortstash);
8205 PL_firstgv = gv_dup(proto_perl->Tfirstgv);
8206 PL_secondgv = gv_dup(proto_perl->Tsecondgv);
8207 PL_sortcxix = proto_perl->Tsortcxix;
8208 PL_efloatbuf = Nullch; /* reinits on demand */
8209 PL_efloatsize = 0; /* reinits on demand */
8213 PL_screamfirst = NULL;
8214 PL_screamnext = NULL;
8215 PL_maxscream = -1; /* reinits on demand */
8216 PL_lastscream = Nullsv;
8218 PL_watchaddr = NULL;
8219 PL_watchok = Nullch;
8221 PL_regdummy = proto_perl->Tregdummy;
8222 PL_regcomp_parse = Nullch;
8223 PL_regxend = Nullch;
8224 PL_regcode = (regnode*)NULL;
8227 PL_regprecomp = Nullch;
8232 PL_seen_zerolen = 0;
8234 PL_regcomp_rx = (regexp*)NULL;
8236 PL_colorset = 0; /* reinits PL_colors[] */
8237 /*PL_colors[6] = {0,0,0,0,0,0};*/
8238 PL_reg_whilem_seen = 0;
8239 PL_reginput = Nullch;
8242 PL_regstartp = (I32*)NULL;
8243 PL_regendp = (I32*)NULL;
8244 PL_reglastparen = (U32*)NULL;
8245 PL_regtill = Nullch;
8247 PL_reg_start_tmp = (char**)NULL;
8248 PL_reg_start_tmpl = 0;
8249 PL_regdata = (struct reg_data*)NULL;
8252 PL_reg_eval_set = 0;
8254 PL_regprogram = (regnode*)NULL;
8256 PL_regcc = (CURCUR*)NULL;
8257 PL_reg_call_cc = (struct re_cc_state*)NULL;
8258 PL_reg_re = (regexp*)NULL;
8259 PL_reg_ganch = Nullch;
8261 PL_reg_magic = (MAGIC*)NULL;
8263 PL_reg_oldcurpm = (PMOP*)NULL;
8264 PL_reg_curpm = (PMOP*)NULL;
8265 PL_reg_oldsaved = Nullch;
8266 PL_reg_oldsavedlen = 0;
8268 PL_reg_leftiter = 0;
8269 PL_reg_poscache = Nullch;
8270 PL_reg_poscache_size= 0;
8272 /* RE engine - function pointers */
8273 PL_regcompp = proto_perl->Tregcompp;
8274 PL_regexecp = proto_perl->Tregexecp;
8275 PL_regint_start = proto_perl->Tregint_start;
8276 PL_regint_string = proto_perl->Tregint_string;
8277 PL_regfree = proto_perl->Tregfree;
8279 PL_reginterp_cnt = 0;
8280 PL_reg_starttry = 0;
8283 return (PerlInterpreter*)pPerl;
8289 #else /* !USE_ITHREADS */
8295 #endif /* USE_ITHREADS */
8298 do_report_used(pTHXo_ SV *sv)
8300 if (SvTYPE(sv) != SVTYPEMASK) {
8301 PerlIO_printf(Perl_debug_log, "****\n");
8307 do_clean_objs(pTHXo_ SV *sv)
8311 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
8312 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv));)
8318 /* XXX Might want to check arrays, etc. */
8321 #ifndef DISABLE_DESTRUCTOR_KLUDGE
8323 do_clean_named_objs(pTHXo_ SV *sv)
8325 if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) {
8326 if ( SvOBJECT(GvSV(sv)) ||
8327 (GvAV(sv) && SvOBJECT(GvAV(sv))) ||
8328 (GvHV(sv) && SvOBJECT(GvHV(sv))) ||
8329 (GvIO(sv) && SvOBJECT(GvIO(sv))) ||
8330 (GvCV(sv) && SvOBJECT(GvCV(sv))) )
8332 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv));)
8340 do_clean_all(pTHXo_ SV *sv)
8342 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) );)
8343 SvFLAGS(sv) |= SVf_BREAK;