3 * Copyright (c) 1991-1999, 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);
34 (p) = (SV*)safemalloc(sizeof(SV)); \
46 Safefree((char*)(p)); \
51 static I32 registry_size;
53 #define REGHASH(sv,size) ((((U32)(sv)) >> 2) % (size))
55 #define REG_REPLACE(sv,a,b) \
57 void* p = sv->sv_any; \
58 I32 h = REGHASH(sv, registry_size); \
60 while (registry[i] != (a)) { \
61 if (++i >= registry_size) \
64 Perl_die(aTHX_ "SV registry bug"); \
69 #define REG_ADD(sv) REG_REPLACE(sv,Nullsv,sv)
70 #define REG_REMOVE(sv) REG_REPLACE(sv,sv,Nullsv)
73 S_reg_add(pTHX_ SV *sv)
75 if (PL_sv_count >= (registry_size >> 1))
77 SV **oldreg = registry;
78 I32 oldsize = registry_size;
80 registry_size = registry_size ? ((registry_size << 2) + 1) : 2037;
81 Newz(707, registry, registry_size, SV*);
86 for (i = 0; i < oldsize; ++i) {
87 SV* oldsv = oldreg[i];
100 S_reg_remove(pTHX_ SV *sv)
107 S_visit(pTHX_ SVFUNC_t f)
111 for (i = 0; i < registry_size; ++i) {
112 SV* sv = registry[i];
113 if (sv && SvTYPE(sv) != SVTYPEMASK)
119 Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
121 if (!(flags & SVf_FAKE))
128 * "A time to plant, and a time to uproot what was planted..."
131 #define plant_SV(p) \
133 SvANY(p) = (void *)PL_sv_root; \
134 SvFLAGS(p) = SVTYPEMASK; \
139 /* sv_mutex must be held while calling uproot_SV() */
140 #define uproot_SV(p) \
143 PL_sv_root = (SV*)SvANY(p); \
165 if (PL_debug & 32768) \
173 S_del_sv(pTHX_ SV *p)
175 if (PL_debug & 32768) {
180 for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
182 svend = &sva[SvREFCNT(sva)];
183 if (p >= sv && p < svend)
187 if (ckWARN_d(WARN_INTERNAL))
188 Perl_warner(aTHX_ WARN_INTERNAL,
189 "Attempt to free non-arena SV: 0x%lx", (unsigned long)p);
196 #else /* ! DEBUGGING */
198 #define del_SV(p) plant_SV(p)
200 #endif /* DEBUGGING */
203 Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
208 Zero(sva, size, char);
210 /* The first SV in an arena isn't an SV. */
211 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
212 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
213 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
215 PL_sv_arenaroot = sva;
216 PL_sv_root = sva + 1;
218 svend = &sva[SvREFCNT(sva) - 1];
221 SvANY(sv) = (void *)(SV*)(sv + 1);
222 SvFLAGS(sv) = SVTYPEMASK;
226 SvFLAGS(sv) = SVTYPEMASK;
229 /* sv_mutex must be held while calling more_sv() */
236 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
237 PL_nice_chunk = Nullch;
240 char *chunk; /* must use New here to match call to */
241 New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */
242 sv_add_arena(chunk, 1008, 0);
249 S_visit(pTHX_ SVFUNC_t f)
255 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
256 svend = &sva[SvREFCNT(sva)];
257 for (sv = sva + 1; sv < svend; ++sv) {
258 if (SvTYPE(sv) != SVTYPEMASK)
267 Perl_sv_report_used(pTHX)
269 visit(do_report_used);
273 Perl_sv_clean_objs(pTHX)
275 PL_in_clean_objs = TRUE;
276 visit(do_clean_objs);
277 #ifndef DISABLE_DESTRUCTOR_KLUDGE
278 /* some barnacles may yet remain, clinging to typeglobs */
279 visit(do_clean_named_objs);
281 PL_in_clean_objs = FALSE;
285 Perl_sv_clean_all(pTHX)
287 PL_in_clean_all = TRUE;
289 PL_in_clean_all = FALSE;
293 Perl_sv_free_arenas(pTHX)
298 /* Free arenas here, but be careful about fake ones. (We assume
299 contiguity of the fake ones with the corresponding real ones.) */
301 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
302 svanext = (SV*) SvANY(sva);
303 while (svanext && SvFAKE(svanext))
304 svanext = (SV*) SvANY(svanext);
307 Safefree((void *)sva);
311 Safefree(PL_nice_chunk);
312 PL_nice_chunk = Nullch;
313 PL_nice_chunk_size = 0;
327 * See comment in more_xiv() -- RAM.
329 PL_xiv_root = *(IV**)xiv;
331 return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv));
335 S_del_xiv(pTHX_ XPVIV *p)
337 IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv));
339 *(IV**)xiv = PL_xiv_root;
350 New(705, ptr, 1008/sizeof(XPV), XPV);
351 ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */
352 PL_xiv_arenaroot = ptr; /* to keep Purify happy */
355 xivend = &xiv[1008 / sizeof(IV) - 1];
356 xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */
358 while (xiv < xivend) {
359 *(IV**)xiv = (IV *)(xiv + 1);
373 PL_xnv_root = *(NV**)xnv;
375 return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv));
379 S_del_xnv(pTHX_ XPVNV *p)
381 NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv));
383 *(NV**)xnv = PL_xnv_root;
393 New(711, xnv, 1008/sizeof(NV), NV);
394 xnvend = &xnv[1008 / sizeof(NV) - 1];
395 xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */
397 while (xnv < xnvend) {
398 *(NV**)xnv = (NV*)(xnv + 1);
412 PL_xrv_root = (XRV*)xrv->xrv_rv;
418 S_del_xrv(pTHX_ XRV *p)
421 p->xrv_rv = (SV*)PL_xrv_root;
430 register XRV* xrvend;
431 New(712, PL_xrv_root, 1008/sizeof(XRV), XRV);
433 xrvend = &xrv[1008 / 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, PL_xpv_root, 1008/sizeof(XPV), XPV);
470 xpvend = &xpv[1008 / sizeof(XPV) - 1];
471 while (xpv < xpvend) {
472 xpv->xpv_pv = (char*)(xpv + 1);
485 xpviv = PL_xpviv_root;
486 PL_xpviv_root = (XPVIV*)xpviv->xpv_pv;
492 S_del_xpviv(pTHX_ XPVIV *p)
495 p->xpv_pv = (char*)PL_xpviv_root;
504 register XPVIV* xpviv;
505 register XPVIV* xpvivend;
506 New(714, PL_xpviv_root, 1008/sizeof(XPVIV), XPVIV);
507 xpviv = PL_xpviv_root;
508 xpvivend = &xpviv[1008 / sizeof(XPVIV) - 1];
509 while (xpviv < xpvivend) {
510 xpviv->xpv_pv = (char*)(xpviv + 1);
524 xpvnv = PL_xpvnv_root;
525 PL_xpvnv_root = (XPVNV*)xpvnv->xpv_pv;
531 S_del_xpvnv(pTHX_ XPVNV *p)
534 p->xpv_pv = (char*)PL_xpvnv_root;
543 register XPVNV* xpvnv;
544 register XPVNV* xpvnvend;
545 New(715, PL_xpvnv_root, 1008/sizeof(XPVNV), XPVNV);
546 xpvnv = PL_xpvnv_root;
547 xpvnvend = &xpvnv[1008 / sizeof(XPVNV) - 1];
548 while (xpvnv < xpvnvend) {
549 xpvnv->xpv_pv = (char*)(xpvnv + 1);
564 xpvcv = PL_xpvcv_root;
565 PL_xpvcv_root = (XPVCV*)xpvcv->xpv_pv;
571 S_del_xpvcv(pTHX_ XPVCV *p)
574 p->xpv_pv = (char*)PL_xpvcv_root;
583 register XPVCV* xpvcv;
584 register XPVCV* xpvcvend;
585 New(716, PL_xpvcv_root, 1008/sizeof(XPVCV), XPVCV);
586 xpvcv = PL_xpvcv_root;
587 xpvcvend = &xpvcv[1008 / sizeof(XPVCV) - 1];
588 while (xpvcv < xpvcvend) {
589 xpvcv->xpv_pv = (char*)(xpvcv + 1);
604 xpvav = PL_xpvav_root;
605 PL_xpvav_root = (XPVAV*)xpvav->xav_array;
611 S_del_xpvav(pTHX_ XPVAV *p)
614 p->xav_array = (char*)PL_xpvav_root;
623 register XPVAV* xpvav;
624 register XPVAV* xpvavend;
625 New(717, PL_xpvav_root, 1008/sizeof(XPVAV), XPVAV);
626 xpvav = PL_xpvav_root;
627 xpvavend = &xpvav[1008 / sizeof(XPVAV) - 1];
628 while (xpvav < xpvavend) {
629 xpvav->xav_array = (char*)(xpvav + 1);
632 xpvav->xav_array = 0;
644 xpvhv = PL_xpvhv_root;
645 PL_xpvhv_root = (XPVHV*)xpvhv->xhv_array;
651 S_del_xpvhv(pTHX_ XPVHV *p)
654 p->xhv_array = (char*)PL_xpvhv_root;
663 register XPVHV* xpvhv;
664 register XPVHV* xpvhvend;
665 New(718, PL_xpvhv_root, 1008/sizeof(XPVHV), XPVHV);
666 xpvhv = PL_xpvhv_root;
667 xpvhvend = &xpvhv[1008 / sizeof(XPVHV) - 1];
668 while (xpvhv < xpvhvend) {
669 xpvhv->xhv_array = (char*)(xpvhv + 1);
672 xpvhv->xhv_array = 0;
683 xpvmg = PL_xpvmg_root;
684 PL_xpvmg_root = (XPVMG*)xpvmg->xpv_pv;
690 S_del_xpvmg(pTHX_ XPVMG *p)
693 p->xpv_pv = (char*)PL_xpvmg_root;
702 register XPVMG* xpvmg;
703 register XPVMG* xpvmgend;
704 New(719, PL_xpvmg_root, 1008/sizeof(XPVMG), XPVMG);
705 xpvmg = PL_xpvmg_root;
706 xpvmgend = &xpvmg[1008 / sizeof(XPVMG) - 1];
707 while (xpvmg < xpvmgend) {
708 xpvmg->xpv_pv = (char*)(xpvmg + 1);
723 xpvlv = PL_xpvlv_root;
724 PL_xpvlv_root = (XPVLV*)xpvlv->xpv_pv;
730 S_del_xpvlv(pTHX_ XPVLV *p)
733 p->xpv_pv = (char*)PL_xpvlv_root;
742 register XPVLV* xpvlv;
743 register XPVLV* xpvlvend;
744 New(720, PL_xpvlv_root, 1008/sizeof(XPVLV), XPVLV);
745 xpvlv = PL_xpvlv_root;
746 xpvlvend = &xpvlv[1008 / sizeof(XPVLV) - 1];
747 while (xpvlv < xpvlvend) {
748 xpvlv->xpv_pv = (char*)(xpvlv + 1);
762 xpvbm = PL_xpvbm_root;
763 PL_xpvbm_root = (XPVBM*)xpvbm->xpv_pv;
769 S_del_xpvbm(pTHX_ XPVBM *p)
772 p->xpv_pv = (char*)PL_xpvbm_root;
781 register XPVBM* xpvbm;
782 register XPVBM* xpvbmend;
783 New(721, PL_xpvbm_root, 1008/sizeof(XPVBM), XPVBM);
784 xpvbm = PL_xpvbm_root;
785 xpvbmend = &xpvbm[1008 / sizeof(XPVBM) - 1];
786 while (xpvbm < xpvbmend) {
787 xpvbm->xpv_pv = (char*)(xpvbm + 1);
794 #define new_XIV() (void*)safemalloc(sizeof(XPVIV))
795 #define del_XIV(p) Safefree((char*)p)
797 #define new_XIV() (void*)new_xiv()
798 #define del_XIV(p) del_xiv((XPVIV*) p)
802 #define new_XNV() (void*)safemalloc(sizeof(XPVNV))
803 #define del_XNV(p) Safefree((char*)p)
805 #define new_XNV() (void*)new_xnv()
806 #define del_XNV(p) del_xnv((XPVNV*) p)
810 #define new_XRV() (void*)safemalloc(sizeof(XRV))
811 #define del_XRV(p) Safefree((char*)p)
813 #define new_XRV() (void*)new_xrv()
814 #define del_XRV(p) del_xrv((XRV*) p)
818 #define new_XPV() (void*)safemalloc(sizeof(XPV))
819 #define del_XPV(p) Safefree((char*)p)
821 #define new_XPV() (void*)new_xpv()
822 #define del_XPV(p) del_xpv((XPV *)p)
826 # define my_safemalloc(s) safemalloc(s)
827 # define my_safefree(s) safefree(s)
830 S_my_safemalloc(MEM_SIZE size)
833 New(717, p, size, char);
836 # define my_safefree(s) Safefree(s)
840 #define new_XPVIV() (void*)safemalloc(sizeof(XPVIV))
841 #define del_XPVIV(p) Safefree((char*)p)
843 #define new_XPVIV() (void*)new_xpviv()
844 #define del_XPVIV(p) del_xpviv((XPVIV *)p)
848 #define new_XPVNV() (void*)safemalloc(sizeof(XPVNV))
849 #define del_XPVNV(p) Safefree((char*)p)
851 #define new_XPVNV() (void*)new_xpvnv()
852 #define del_XPVNV(p) del_xpvnv((XPVNV *)p)
857 #define new_XPVCV() (void*)safemalloc(sizeof(XPVCV))
858 #define del_XPVCV(p) Safefree((char*)p)
860 #define new_XPVCV() (void*)new_xpvcv()
861 #define del_XPVCV(p) del_xpvcv((XPVCV *)p)
865 #define new_XPVAV() (void*)safemalloc(sizeof(XPVAV))
866 #define del_XPVAV(p) Safefree((char*)p)
868 #define new_XPVAV() (void*)new_xpvav()
869 #define del_XPVAV(p) del_xpvav((XPVAV *)p)
873 #define new_XPVHV() (void*)safemalloc(sizeof(XPVHV))
874 #define del_XPVHV(p) Safefree((char*)p)
876 #define new_XPVHV() (void*)new_xpvhv()
877 #define del_XPVHV(p) del_xpvhv((XPVHV *)p)
881 #define new_XPVMG() (void*)safemalloc(sizeof(XPVMG))
882 #define del_XPVMG(p) Safefree((char*)p)
884 #define new_XPVMG() (void*)new_xpvmg()
885 #define del_XPVMG(p) del_xpvmg((XPVMG *)p)
889 #define new_XPVLV() (void*)safemalloc(sizeof(XPVLV))
890 #define del_XPVLV(p) Safefree((char*)p)
892 #define new_XPVLV() (void*)new_xpvlv()
893 #define del_XPVLV(p) del_xpvlv((XPVLV *)p)
896 #define new_XPVGV() (void*)my_safemalloc(sizeof(XPVGV))
897 #define del_XPVGV(p) my_safefree((char*)p)
900 #define new_XPVBM() (void*)safemalloc(sizeof(XPVBM))
901 #define del_XPVBM(p) Safefree((char*)p)
903 #define new_XPVBM() (void*)new_xpvbm()
904 #define del_XPVBM(p) del_xpvbm((XPVBM *)p)
907 #define new_XPVFM() (void*)my_safemalloc(sizeof(XPVFM))
908 #define del_XPVFM(p) my_safefree((char*)p)
910 #define new_XPVIO() (void*)my_safemalloc(sizeof(XPVIO))
911 #define del_XPVIO(p) my_safefree((char*)p)
914 Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
924 if (SvTYPE(sv) == mt)
930 switch (SvTYPE(sv)) {
951 else if (mt < SVt_PVIV)
968 pv = (char*)SvRV(sv);
988 else if (mt == SVt_NV)
999 del_XPVIV(SvANY(sv));
1009 del_XPVNV(SvANY(sv));
1017 magic = SvMAGIC(sv);
1018 stash = SvSTASH(sv);
1019 del_XPVMG(SvANY(sv));
1022 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
1027 Perl_croak(aTHX_ "Can't upgrade to undef");
1029 SvANY(sv) = new_XIV();
1033 SvANY(sv) = new_XNV();
1037 SvANY(sv) = new_XRV();
1041 SvANY(sv) = new_XPV();
1047 SvANY(sv) = new_XPVIV();
1057 SvANY(sv) = new_XPVNV();
1065 SvANY(sv) = new_XPVMG();
1071 SvMAGIC(sv) = magic;
1072 SvSTASH(sv) = stash;
1075 SvANY(sv) = new_XPVLV();
1081 SvMAGIC(sv) = magic;
1082 SvSTASH(sv) = stash;
1089 SvANY(sv) = new_XPVAV();
1097 SvMAGIC(sv) = magic;
1098 SvSTASH(sv) = stash;
1104 SvANY(sv) = new_XPVHV();
1112 SvMAGIC(sv) = magic;
1113 SvSTASH(sv) = stash;
1120 SvANY(sv) = new_XPVCV();
1121 Zero(SvANY(sv), 1, XPVCV);
1127 SvMAGIC(sv) = magic;
1128 SvSTASH(sv) = stash;
1131 SvANY(sv) = new_XPVGV();
1137 SvMAGIC(sv) = magic;
1138 SvSTASH(sv) = stash;
1146 SvANY(sv) = new_XPVBM();
1152 SvMAGIC(sv) = magic;
1153 SvSTASH(sv) = stash;
1159 SvANY(sv) = new_XPVFM();
1160 Zero(SvANY(sv), 1, XPVFM);
1166 SvMAGIC(sv) = magic;
1167 SvSTASH(sv) = stash;
1170 SvANY(sv) = new_XPVIO();
1171 Zero(SvANY(sv), 1, XPVIO);
1177 SvMAGIC(sv) = magic;
1178 SvSTASH(sv) = stash;
1179 IoPAGE_LEN(sv) = 60;
1182 SvFLAGS(sv) &= ~SVTYPEMASK;
1188 Perl_sv_backoff(pTHX_ register SV *sv)
1192 char *s = SvPVX(sv);
1193 SvLEN(sv) += SvIVX(sv);
1194 SvPVX(sv) -= SvIVX(sv);
1196 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
1198 SvFLAGS(sv) &= ~SVf_OOK;
1203 Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
1207 #ifdef HAS_64K_LIMIT
1208 if (newlen >= 0x10000) {
1209 PerlIO_printf(Perl_debug_log, "Allocation too large: %lx\n", newlen);
1212 #endif /* HAS_64K_LIMIT */
1215 if (SvTYPE(sv) < SVt_PV) {
1216 sv_upgrade(sv, SVt_PV);
1219 else if (SvOOK(sv)) { /* pv is offset? */
1222 if (newlen > SvLEN(sv))
1223 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
1224 #ifdef HAS_64K_LIMIT
1225 if (newlen >= 0x10000)
1231 if (newlen > SvLEN(sv)) { /* need more room? */
1232 if (SvLEN(sv) && s) {
1233 #if defined(MYMALLOC) && !defined(PURIFY) && !defined(LEAKTEST)
1234 STRLEN l = malloced_size((void*)SvPVX(sv));
1240 Renew(s,newlen,char);
1243 New(703,s,newlen,char);
1245 SvLEN_set(sv, newlen);
1251 Perl_sv_setiv(pTHX_ register SV *sv, IV i)
1253 SV_CHECK_THINKFIRST(sv);
1254 switch (SvTYPE(sv)) {
1256 sv_upgrade(sv, SVt_IV);
1259 sv_upgrade(sv, SVt_PVNV);
1263 sv_upgrade(sv, SVt_PVIV);
1274 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
1275 PL_op_desc[PL_op->op_type]);
1278 (void)SvIOK_only(sv); /* validate number */
1284 Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
1291 Perl_sv_setuv(pTHX_ register SV *sv, UV u)
1299 Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
1306 Perl_sv_setnv(pTHX_ register SV *sv, NV num)
1308 SV_CHECK_THINKFIRST(sv);
1309 switch (SvTYPE(sv)) {
1312 sv_upgrade(sv, SVt_NV);
1317 sv_upgrade(sv, SVt_PVNV);
1328 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
1329 PL_op_name[PL_op->op_type]);
1333 (void)SvNOK_only(sv); /* validate number */
1338 Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
1345 S_not_a_number(pTHX_ SV *sv)
1351 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
1352 /* each *s can expand to 4 chars + "...\0",
1353 i.e. need room for 8 chars */
1355 for (s = SvPVX(sv); *s && d < limit; s++) {
1357 if (ch & 128 && !isPRINT_LC(ch)) {
1366 else if (ch == '\r') {
1370 else if (ch == '\f') {
1374 else if (ch == '\\') {
1378 else if (isPRINT_LC(ch))
1393 Perl_warner(aTHX_ WARN_NUMERIC,
1394 "Argument \"%s\" isn't numeric in %s", tmpbuf,
1395 PL_op_desc[PL_op->op_type]);
1397 Perl_warner(aTHX_ WARN_NUMERIC,
1398 "Argument \"%s\" isn't numeric", tmpbuf);
1401 /* the number can be converted to integer with atol() or atoll() */
1402 #define IS_NUMBER_TO_INT_BY_ATOL 0x01
1403 #define IS_NUMBER_TO_INT_BY_ATOF 0x02 /* atol() may be != atof() */
1404 #define IS_NUMBER_NOT_IV 0x04 /* (IV)atof() may be != atof() */
1405 #define IS_NUMBER_NEG 0x08 /* not good to cache UV */
1407 /* Actually, ISO C leaves conversion of UV to IV undefined, but
1408 until proven guilty, assume that things are not that bad... */
1411 Perl_sv_2iv(pTHX_ register SV *sv)
1415 if (SvGMAGICAL(sv)) {
1420 return I_V(SvNVX(sv));
1422 if (SvPOKp(sv) && SvLEN(sv))
1425 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1427 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1428 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1433 if (SvTHINKFIRST(sv)) {
1436 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv, numer)))
1437 return SvIV(tmpstr);
1438 return PTR2IV(SvRV(sv));
1440 if (SvREADONLY(sv) && !SvOK(sv)) {
1442 if (ckWARN(WARN_UNINITIALIZED))
1443 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1449 return (IV)(SvUVX(sv));
1456 /* We can cache the IV/UV value even if it not good enough
1457 * to reconstruct NV, since the conversion to PV will prefer
1461 if (SvTYPE(sv) == SVt_NV)
1462 sv_upgrade(sv, SVt_PVNV);
1465 if (SvNVX(sv) < (NV)IV_MAX + 0.5)
1466 SvIVX(sv) = I_V(SvNVX(sv));
1468 SvUVX(sv) = U_V(SvNVX(sv));
1472 DEBUG_c(PerlIO_printf(Perl_debug_log,
1473 "0x%" PERL_PRIx64 " 2iv(%" PERL_PRIu64 " => %" PERL_PRId64 ") (as unsigned)\n",
1475 (UV)SvUVX(sv), (IV)SvUVX(sv)));
1477 DEBUG_c(PerlIO_printf(Perl_debug_log,
1478 "0x%lx 2iv(%lu => %ld) (as unsigned)\n",
1480 (unsigned long)SvUVX(sv), (long)(IV)SvUVX(sv)));
1482 return (IV)SvUVX(sv);
1485 else if (SvPOKp(sv) && SvLEN(sv)) {
1486 I32 numtype = looks_like_number(sv);
1488 /* We want to avoid a possible problem when we cache an IV which
1489 may be later translated to an NV, and the resulting NV is not
1490 the translation of the initial data.
1492 This means that if we cache such an IV, we need to cache the
1493 NV as well. Moreover, we trade speed for space, and do not
1494 cache the NV if not needed.
1496 if (numtype & IS_NUMBER_NOT_IV) {
1497 /* May be not an integer. Need to cache NV if we cache IV
1498 * - otherwise future conversion to NV will be wrong. */
1501 d = Atof(SvPVX(sv));
1503 if (SvTYPE(sv) < SVt_PVNV)
1504 sv_upgrade(sv, SVt_PVNV);
1508 #if defined(USE_LONG_DOUBLE)
1509 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2nv(%" PERL_PRIgldbl ")\n",
1510 (unsigned long)sv, SvNVX(sv)));
1512 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2nv(%g)\n",
1513 (unsigned long)sv, SvNVX(sv)));
1515 if (SvNVX(sv) < (NV)IV_MAX + 0.5)
1516 SvIVX(sv) = I_V(SvNVX(sv));
1518 SvUVX(sv) = U_V(SvNVX(sv));
1524 /* The NV may be reconstructed from IV - safe to cache IV,
1525 which may be calculated by atol(). */
1526 if (SvTYPE(sv) == SVt_PV)
1527 sv_upgrade(sv, SVt_PVIV);
1529 SvIVX(sv) = Atol(SvPVX(sv));
1531 else { /* Not a number. Cache 0. */
1534 if (SvTYPE(sv) < SVt_PVIV)
1535 sv_upgrade(sv, SVt_PVIV);
1538 if (ckWARN(WARN_NUMERIC))
1544 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1545 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1546 if (SvTYPE(sv) < SVt_IV)
1547 /* Typically the caller expects that sv_any is not NULL now. */
1548 sv_upgrade(sv, SVt_IV);
1551 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2iv(%ld)\n",
1552 (unsigned long)sv,(long)SvIVX(sv)));
1553 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
1557 Perl_sv_2uv(pTHX_ register SV *sv)
1561 if (SvGMAGICAL(sv)) {
1566 return U_V(SvNVX(sv));
1567 if (SvPOKp(sv) && SvLEN(sv))
1570 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1572 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1573 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1578 if (SvTHINKFIRST(sv)) {
1581 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv, numer)))
1582 return SvUV(tmpstr);
1583 return PTR2UV(SvRV(sv));
1585 if (SvREADONLY(sv) && !SvOK(sv)) {
1587 if (ckWARN(WARN_UNINITIALIZED))
1588 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1597 return (UV)SvIVX(sv);
1601 /* We can cache the IV/UV value even if it not good enough
1602 * to reconstruct NV, since the conversion to PV will prefer
1605 if (SvTYPE(sv) == SVt_NV)
1606 sv_upgrade(sv, SVt_PVNV);
1608 if (SvNVX(sv) >= -0.5) {
1610 SvUVX(sv) = U_V(SvNVX(sv));
1613 SvIVX(sv) = I_V(SvNVX(sv));
1616 DEBUG_c(PerlIO_printf(Perl_debug_log,
1617 "0x%" PERL_PRIx64 " 2uv(%" PERL_PRId64 " => %" PERL_PRIu64 ") (as signed)\n",
1618 (unsigned long)sv,(long)SvIVX(sv),
1619 (long)(UV)SvIVX(sv)));
1621 DEBUG_c(PerlIO_printf(Perl_debug_log,
1622 "0x%lx 2uv(%ld => %lu) (as signed)\n",
1623 (unsigned long)sv,(long)SvIVX(sv),
1624 (long)(UV)SvIVX(sv)));
1626 return (UV)SvIVX(sv);
1629 else if (SvPOKp(sv) && SvLEN(sv)) {
1630 I32 numtype = looks_like_number(sv);
1632 /* We want to avoid a possible problem when we cache a UV which
1633 may be later translated to an NV, and the resulting NV is not
1634 the translation of the initial data.
1636 This means that if we cache such a UV, we need to cache the
1637 NV as well. Moreover, we trade speed for space, and do not
1638 cache the NV if not needed.
1640 if (numtype & IS_NUMBER_NOT_IV) {
1641 /* May be not an integer. Need to cache NV if we cache IV
1642 * - otherwise future conversion to NV will be wrong. */
1645 d = Atof(SvPVX(sv));
1647 if (SvTYPE(sv) < SVt_PVNV)
1648 sv_upgrade(sv, SVt_PVNV);
1652 #if defined(USE_LONG_DOUBLE)
1653 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2nv(%" PERL_PRIgldbl ")\n",
1654 (unsigned long)sv, SvNVX(sv)));
1656 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2nv(%g)\n",
1657 (unsigned long)sv, SvNVX(sv)));
1659 if (SvNVX(sv) < -0.5) {
1660 SvIVX(sv) = I_V(SvNVX(sv));
1663 SvUVX(sv) = U_V(SvNVX(sv));
1667 else if (numtype & IS_NUMBER_NEG) {
1668 /* The NV may be reconstructed from IV - safe to cache IV,
1669 which may be calculated by atol(). */
1670 if (SvTYPE(sv) == SVt_PV)
1671 sv_upgrade(sv, SVt_PVIV);
1673 SvIVX(sv) = (IV)Atol(SvPVX(sv));
1675 else if (numtype) { /* Non-negative */
1676 /* The NV may be reconstructed from UV - safe to cache UV,
1677 which may be calculated by strtoul()/atol. */
1678 if (SvTYPE(sv) == SVt_PV)
1679 sv_upgrade(sv, SVt_PVIV);
1681 (void)SvIsUV_on(sv);
1683 SvUVX(sv) = Strtoul(SvPVX(sv), Null(char**), 10);
1684 #else /* no atou(), but we know the number fits into IV... */
1685 /* The only problem may be if it is negative... */
1686 SvUVX(sv) = (UV)Atol(SvPVX(sv));
1689 else { /* Not a number. Cache 0. */
1692 if (SvTYPE(sv) < SVt_PVIV)
1693 sv_upgrade(sv, SVt_PVIV);
1694 SvUVX(sv) = 0; /* We assume that 0s have the
1695 same bitmap in IV and UV. */
1697 (void)SvIsUV_on(sv);
1698 if (ckWARN(WARN_NUMERIC))
1703 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1705 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1706 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1708 if (SvTYPE(sv) < SVt_IV)
1709 /* Typically the caller expects that sv_any is not NULL now. */
1710 sv_upgrade(sv, SVt_IV);
1714 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2uv(%lu)\n",
1715 (unsigned long)sv,SvUVX(sv)));
1716 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
1720 Perl_sv_2nv(pTHX_ register SV *sv)
1724 if (SvGMAGICAL(sv)) {
1728 if (SvPOKp(sv) && SvLEN(sv)) {
1730 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv))
1732 return Atof(SvPVX(sv));
1736 return (NV)SvUVX(sv);
1738 return (NV)SvIVX(sv);
1741 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1743 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1744 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1749 if (SvTHINKFIRST(sv)) {
1752 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)))
1753 return SvNV(tmpstr);
1754 return PTR2NV(SvRV(sv));
1756 if (SvREADONLY(sv) && !SvOK(sv)) {
1758 if (ckWARN(WARN_UNINITIALIZED))
1759 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1763 if (SvTYPE(sv) < SVt_NV) {
1764 if (SvTYPE(sv) == SVt_IV)
1765 sv_upgrade(sv, SVt_PVNV);
1767 sv_upgrade(sv, SVt_NV);
1768 #if defined(USE_LONG_DOUBLE)
1770 RESTORE_NUMERIC_STANDARD();
1771 PerlIO_printf(Perl_debug_log, "0x%lx num(%" PERL_PRIgldbl ")\n",
1772 (unsigned long)sv, SvNVX(sv));
1773 RESTORE_NUMERIC_LOCAL();
1777 RESTORE_NUMERIC_STANDARD();
1778 PerlIO_printf(Perl_debug_log, "0x%lx num(%g)\n",
1779 (unsigned long)sv, SvNVX(sv));
1780 RESTORE_NUMERIC_LOCAL();
1784 else if (SvTYPE(sv) < SVt_PVNV)
1785 sv_upgrade(sv, SVt_PVNV);
1787 (!SvPOKp(sv) || !strchr(SvPVX(sv),'.') || !looks_like_number(sv)))
1789 SvNVX(sv) = SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv);
1791 else if (SvPOKp(sv) && SvLEN(sv)) {
1793 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv))
1795 SvNVX(sv) = Atof(SvPVX(sv));
1799 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1800 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1801 if (SvTYPE(sv) < SVt_NV)
1802 /* Typically the caller expects that sv_any is not NULL now. */
1803 sv_upgrade(sv, SVt_NV);
1807 #if defined(USE_LONG_DOUBLE)
1809 RESTORE_NUMERIC_STANDARD();
1810 PerlIO_printf(Perl_debug_log, "0x%lx 2nv(%" PERL_PRIgldbl ")\n",
1811 (unsigned long)sv, SvNVX(sv));
1812 RESTORE_NUMERIC_LOCAL();
1816 RESTORE_NUMERIC_STANDARD();
1817 PerlIO_printf(Perl_debug_log, "0x%lx 1nv(%g)\n",
1818 (unsigned long)sv, SvNVX(sv));
1819 RESTORE_NUMERIC_LOCAL();
1826 S_asIV(pTHX_ SV *sv)
1828 I32 numtype = looks_like_number(sv);
1831 if (numtype & IS_NUMBER_TO_INT_BY_ATOL)
1832 return Atol(SvPVX(sv));
1835 if (ckWARN(WARN_NUMERIC))
1838 d = Atof(SvPVX(sv));
1843 S_asUV(pTHX_ SV *sv)
1845 I32 numtype = looks_like_number(sv);
1848 if (numtype & IS_NUMBER_TO_INT_BY_ATOL)
1849 return Strtoul(SvPVX(sv), Null(char**), 10);
1853 if (ckWARN(WARN_NUMERIC))
1856 return U_V(Atof(SvPVX(sv)));
1860 * Returns a combination of (advisory only - can get false negatives)
1861 * IS_NUMBER_TO_INT_BY_ATOL, IS_NUMBER_TO_INT_BY_ATOF, IS_NUMBER_NOT_IV,
1863 * 0 if does not look like number.
1865 * In fact possible values are 0 and
1866 * IS_NUMBER_TO_INT_BY_ATOL 123
1867 * IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_NOT_IV 123.1
1868 * IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_IV 123e0
1869 * with a possible addition of IS_NUMBER_NEG.
1873 Perl_looks_like_number(pTHX_ SV *sv)
1876 register char *send;
1877 register char *sbegin;
1878 register char *nbegin;
1886 else if (SvPOKp(sv))
1887 sbegin = SvPV(sv, len);
1890 send = sbegin + len;
1897 numtype = IS_NUMBER_NEG;
1904 * we return IS_NUMBER_TO_INT_BY_ATOL if the number can be converted
1905 * to _integer_ with atol() and IS_NUMBER_TO_INT_BY_ATOF if you need
1909 /* next must be digit or the radix separator */
1913 } while (isDIGIT(*s));
1915 if (s - nbegin >= TYPE_DIGITS(IV)) /* Cannot cache ato[ul]() */
1916 numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_IV;
1918 numtype |= IS_NUMBER_TO_INT_BY_ATOL;
1921 #ifdef USE_LOCALE_NUMERIC
1922 || IS_NUMERIC_RADIX(*s)
1926 numtype |= IS_NUMBER_NOT_IV;
1927 while (isDIGIT(*s)) /* optional digits after the radix */
1932 #ifdef USE_LOCALE_NUMERIC
1933 || IS_NUMERIC_RADIX(*s)
1937 numtype |= IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_NOT_IV;
1938 /* no digits before the radix means we need digits after it */
1942 } while (isDIGIT(*s));
1950 /* we can have an optional exponent part */
1951 if (*s == 'e' || *s == 'E') {
1952 numtype &= ~IS_NUMBER_NEG;
1953 numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_IV;
1955 if (*s == '+' || *s == '-')
1960 } while (isDIGIT(*s));
1969 if (len == 10 && memEQ(sbegin, "0 but true", 10))
1970 return IS_NUMBER_TO_INT_BY_ATOL;
1975 Perl_sv_2pv_nolen(pTHX_ register SV *sv)
1978 return sv_2pv(sv, &n_a);
1981 /* We assume that buf is at least TYPE_CHARS(UV) long. */
1983 uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
1986 char *ptr = buf + TYPE_CHARS(UV);
2001 *--ptr = '0' + (uv % 10);
2010 Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
2015 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
2016 char *tmpbuf = tbuf;
2022 if (SvGMAGICAL(sv)) {
2031 (void)sprintf(tmpbuf,"%" PERL_PRIu64,(UV)SvUVX(sv));
2033 (void)sprintf(tmpbuf,"%" PERL_PRId64,(IV)SvIVX(sv));
2036 (void)sprintf(tmpbuf,"%lu",(unsigned long)SvUVX(sv));
2038 (void)sprintf(tmpbuf,"%ld",(long)SvIVX(sv));
2044 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
2049 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2051 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2052 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
2058 if (SvTHINKFIRST(sv)) {
2061 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)))
2062 return SvPV(tmpstr,*lp);
2069 switch (SvTYPE(sv)) {
2071 if ( ((SvFLAGS(sv) &
2072 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
2073 == (SVs_OBJECT|SVs_RMG))
2074 && strEQ(s=HvNAME(SvSTASH(sv)), "Regexp")
2075 && (mg = mg_find(sv, 'r'))) {
2077 regexp *re = (regexp *)mg->mg_obj;
2080 char *fptr = "msix";
2085 U16 reganch = (re->reganch & PMf_COMPILETIME) >> 12;
2087 while(ch = *fptr++) {
2089 reflags[left++] = ch;
2092 reflags[right--] = ch;
2097 reflags[left] = '-';
2101 mg->mg_len = re->prelen + 4 + left;
2102 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
2103 Copy("(?", mg->mg_ptr, 2, char);
2104 Copy(reflags, mg->mg_ptr+2, left, char);
2105 Copy(":", mg->mg_ptr+left+2, 1, char);
2106 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
2107 mg->mg_ptr[mg->mg_len - 1] = ')';
2108 mg->mg_ptr[mg->mg_len] = 0;
2110 PL_reginterp_cnt += re->program[0].next_off;
2122 case SVt_PVBM: s = "SCALAR"; break;
2123 case SVt_PVLV: s = "LVALUE"; break;
2124 case SVt_PVAV: s = "ARRAY"; break;
2125 case SVt_PVHV: s = "HASH"; break;
2126 case SVt_PVCV: s = "CODE"; break;
2127 case SVt_PVGV: s = "GLOB"; break;
2128 case SVt_PVFM: s = "FORMAT"; break;
2129 case SVt_PVIO: s = "IO"; break;
2130 default: s = "UNKNOWN"; break;
2134 Perl_sv_setpvf(aTHX_ tsv, "%s=%s", HvNAME(SvSTASH(sv)), s);
2138 Perl_sv_catpvf(aTHX_ tsv, "(0x%" PERL_PRIx64")", PTR2UV(sv));
2140 Perl_sv_catpvf(aTHX_ tsv, "(0x%lx)", (unsigned long)sv);
2147 if (SvREADONLY(sv) && !SvOK(sv)) {
2149 if (ckWARN(WARN_UNINITIALIZED))
2150 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
2155 if (SvNOKp(sv)) { /* See note in sv_2uv() */
2156 /* XXXX 64-bit? IV may have better precision... */
2157 /* I tried changing this for to be 64-bit-aware and
2158 * the t/op/numconvert.t became very, very, angry.
2160 if (SvTYPE(sv) < SVt_PVNV)
2161 sv_upgrade(sv, SVt_PVNV);
2164 olderrno = errno; /* some Xenix systems wipe out errno here */
2166 if (SvNVX(sv) == 0.0)
2167 (void)strcpy(s,"0");
2171 Gconvert(SvNVX(sv), NV_DIG, 0, s);
2174 #ifdef FIXNEGATIVEZERO
2175 if (*s == '-' && s[1] == '0' && !s[2])
2184 else if (SvIOKp(sv)) {
2185 U32 isIOK = SvIOK(sv);
2186 U32 isUIOK = SvIsUV(sv);
2187 char buf[TYPE_CHARS(UV)];
2190 if (SvTYPE(sv) < SVt_PVIV)
2191 sv_upgrade(sv, SVt_PVIV);
2193 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
2195 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
2196 SvGROW(sv, ebuf - ptr + 1); /* inlined from sv_setpvn */
2197 Move(ptr,SvPVX(sv),ebuf - ptr,char);
2198 SvCUR_set(sv, ebuf - ptr);
2211 if (ckWARN(WARN_UNINITIALIZED)
2212 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
2214 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
2217 if (SvTYPE(sv) < SVt_PV)
2218 /* Typically the caller expects that sv_any is not NULL now. */
2219 sv_upgrade(sv, SVt_PV);
2222 *lp = s - SvPVX(sv);
2225 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2pv(%s)\n",
2226 (unsigned long)sv,SvPVX(sv)));
2230 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
2231 /* Sneaky stuff here */
2235 tsv = newSVpv(tmpbuf, 0);
2251 len = strlen(tmpbuf);
2253 #ifdef FIXNEGATIVEZERO
2254 if (len == 2 && t[0] == '-' && t[1] == '0') {
2259 (void)SvUPGRADE(sv, SVt_PV);
2261 s = SvGROW(sv, len + 1);
2269 /* This function is only called on magical items */
2271 Perl_sv_2bool(pTHX_ register SV *sv)
2281 if (SvAMAGIC(sv) && (tmpsv = AMG_CALLun(sv,bool_)))
2282 return SvTRUE(tmpsv);
2283 return SvRV(sv) != 0;
2286 register XPV* Xpvtmp;
2287 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
2288 (*Xpvtmp->xpv_pv > '0' ||
2289 Xpvtmp->xpv_cur > 1 ||
2290 (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0')))
2297 return SvIVX(sv) != 0;
2300 return SvNVX(sv) != 0.0;
2307 /* Note: sv_setsv() should not be called with a source string that needs
2308 * to be reused, since it may destroy the source string if it is marked
2313 Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
2316 register U32 sflags;
2322 SV_CHECK_THINKFIRST(dstr);
2324 sstr = &PL_sv_undef;
2325 stype = SvTYPE(sstr);
2326 dtype = SvTYPE(dstr);
2330 /* There's a lot of redundancy below but we're going for speed here */
2335 if (dtype != SVt_PVGV) {
2336 (void)SvOK_off(dstr);
2344 sv_upgrade(dstr, SVt_IV);
2347 sv_upgrade(dstr, SVt_PVNV);
2351 sv_upgrade(dstr, SVt_PVIV);
2354 (void)SvIOK_only(dstr);
2355 SvIVX(dstr) = SvIVX(sstr);
2368 sv_upgrade(dstr, SVt_NV);
2373 sv_upgrade(dstr, SVt_PVNV);
2376 SvNVX(dstr) = SvNVX(sstr);
2377 (void)SvNOK_only(dstr);
2385 sv_upgrade(dstr, SVt_RV);
2386 else if (dtype == SVt_PVGV &&
2387 SvTYPE(SvRV(sstr)) == SVt_PVGV) {
2390 if (PL_curcop->cop_stash != GvSTASH(dstr))
2391 GvIMPORTED_on(dstr);
2401 sv_upgrade(dstr, SVt_PV);
2404 if (dtype < SVt_PVIV)
2405 sv_upgrade(dstr, SVt_PVIV);
2408 if (dtype < SVt_PVNV)
2409 sv_upgrade(dstr, SVt_PVNV);
2416 Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0),
2417 PL_op_name[PL_op->op_type]);
2419 Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0));
2423 if (dtype <= SVt_PVGV) {
2425 if (dtype != SVt_PVGV) {
2426 char *name = GvNAME(sstr);
2427 STRLEN len = GvNAMELEN(sstr);
2428 sv_upgrade(dstr, SVt_PVGV);
2429 sv_magic(dstr, dstr, '*', name, len);
2430 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
2431 GvNAME(dstr) = savepvn(name, len);
2432 GvNAMELEN(dstr) = len;
2433 SvFAKE_on(dstr); /* can coerce to non-glob */
2435 /* ahem, death to those who redefine active sort subs */
2436 else if (PL_curstackinfo->si_type == PERLSI_SORT
2437 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
2438 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
2440 (void)SvOK_off(dstr);
2441 GvINTRO_off(dstr); /* one-shot flag */
2443 GvGP(dstr) = gp_ref(GvGP(sstr));
2445 if (PL_curcop->cop_stash != GvSTASH(dstr))
2446 GvIMPORTED_on(dstr);
2453 if (SvGMAGICAL(sstr)) {
2455 if (SvTYPE(sstr) != stype) {
2456 stype = SvTYPE(sstr);
2457 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
2461 if (stype == SVt_PVLV)
2462 (void)SvUPGRADE(dstr, SVt_PVNV);
2464 (void)SvUPGRADE(dstr, stype);
2467 sflags = SvFLAGS(sstr);
2469 if (sflags & SVf_ROK) {
2470 if (dtype >= SVt_PV) {
2471 if (dtype == SVt_PVGV) {
2472 SV *sref = SvREFCNT_inc(SvRV(sstr));
2474 int intro = GvINTRO(dstr);
2478 GvGP(dstr)->gp_refcnt--;
2479 GvINTRO_off(dstr); /* one-shot flag */
2480 Newz(602,gp, 1, GP);
2481 GvGP(dstr) = gp_ref(gp);
2482 GvSV(dstr) = NEWSV(72,0);
2483 GvLINE(dstr) = PL_curcop->cop_line;
2484 GvEGV(dstr) = (GV*)dstr;
2487 switch (SvTYPE(sref)) {
2490 SAVESPTR(GvAV(dstr));
2492 dref = (SV*)GvAV(dstr);
2493 GvAV(dstr) = (AV*)sref;
2494 if (PL_curcop->cop_stash != GvSTASH(dstr))
2495 GvIMPORTED_AV_on(dstr);
2499 SAVESPTR(GvHV(dstr));
2501 dref = (SV*)GvHV(dstr);
2502 GvHV(dstr) = (HV*)sref;
2503 if (PL_curcop->cop_stash != GvSTASH(dstr))
2504 GvIMPORTED_HV_on(dstr);
2508 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
2509 SvREFCNT_dec(GvCV(dstr));
2510 GvCV(dstr) = Nullcv;
2511 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
2512 PL_sub_generation++;
2514 SAVESPTR(GvCV(dstr));
2517 dref = (SV*)GvCV(dstr);
2518 if (GvCV(dstr) != (CV*)sref) {
2519 CV* cv = GvCV(dstr);
2521 if (!GvCVGEN((GV*)dstr) &&
2522 (CvROOT(cv) || CvXSUB(cv)))
2524 SV *const_sv = cv_const_sv(cv);
2525 bool const_changed = TRUE;
2527 const_changed = sv_cmp(const_sv,
2528 op_const_sv(CvSTART((CV*)sref),
2530 /* ahem, death to those who redefine
2531 * active sort subs */
2532 if (PL_curstackinfo->si_type == PERLSI_SORT &&
2533 PL_sortcop == CvSTART(cv))
2535 "Can't redefine active sort subroutine %s",
2536 GvENAME((GV*)dstr));
2537 if (ckWARN(WARN_REDEFINE) || (const_changed && const_sv)) {
2538 if (!(CvGV(cv) && GvSTASH(CvGV(cv))
2539 && HvNAME(GvSTASH(CvGV(cv)))
2540 && strEQ(HvNAME(GvSTASH(CvGV(cv))),
2542 Perl_warner(aTHX_ WARN_REDEFINE, const_sv ?
2543 "Constant subroutine %s redefined"
2544 : "Subroutine %s redefined",
2545 GvENAME((GV*)dstr));
2548 cv_ckproto(cv, (GV*)dstr,
2549 SvPOK(sref) ? SvPVX(sref) : Nullch);
2551 GvCV(dstr) = (CV*)sref;
2552 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
2553 GvASSUMECV_on(dstr);
2554 PL_sub_generation++;
2556 if (PL_curcop->cop_stash != GvSTASH(dstr))
2557 GvIMPORTED_CV_on(dstr);
2561 SAVESPTR(GvIOp(dstr));
2563 dref = (SV*)GvIOp(dstr);
2564 GvIOp(dstr) = (IO*)sref;
2568 SAVESPTR(GvSV(dstr));
2570 dref = (SV*)GvSV(dstr);
2572 if (PL_curcop->cop_stash != GvSTASH(dstr))
2573 GvIMPORTED_SV_on(dstr);
2584 (void)SvOOK_off(dstr); /* backoff */
2586 Safefree(SvPVX(dstr));
2587 SvLEN(dstr)=SvCUR(dstr)=0;
2590 (void)SvOK_off(dstr);
2591 SvRV(dstr) = SvREFCNT_inc(SvRV(sstr));
2593 if (sflags & SVp_NOK) {
2595 SvNVX(dstr) = SvNVX(sstr);
2597 if (sflags & SVp_IOK) {
2598 (void)SvIOK_on(dstr);
2599 SvIVX(dstr) = SvIVX(sstr);
2603 if (SvAMAGIC(sstr)) {
2607 else if (sflags & SVp_POK) {
2610 * Check to see if we can just swipe the string. If so, it's a
2611 * possible small lose on short strings, but a big win on long ones.
2612 * It might even be a win on short strings if SvPVX(dstr)
2613 * has to be allocated and SvPVX(sstr) has to be freed.
2616 if (SvTEMP(sstr) && /* slated for free anyway? */
2617 SvREFCNT(sstr) == 1 && /* and no other references to it? */
2618 !(sflags & SVf_OOK)) /* and not involved in OOK hack? */
2620 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
2622 SvFLAGS(dstr) &= ~SVf_OOK;
2623 Safefree(SvPVX(dstr) - SvIVX(dstr));
2625 else if (SvLEN(dstr))
2626 Safefree(SvPVX(dstr));
2628 (void)SvPOK_only(dstr);
2629 SvPV_set(dstr, SvPVX(sstr));
2630 SvLEN_set(dstr, SvLEN(sstr));
2631 SvCUR_set(dstr, SvCUR(sstr));
2633 (void)SvOK_off(sstr);
2634 SvPV_set(sstr, Nullch);
2639 else { /* have to copy actual string */
2640 STRLEN len = SvCUR(sstr);
2642 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
2643 Move(SvPVX(sstr),SvPVX(dstr),len,char);
2644 SvCUR_set(dstr, len);
2645 *SvEND(dstr) = '\0';
2646 (void)SvPOK_only(dstr);
2649 if (sflags & SVp_NOK) {
2651 SvNVX(dstr) = SvNVX(sstr);
2653 if (sflags & SVp_IOK) {
2654 (void)SvIOK_on(dstr);
2655 SvIVX(dstr) = SvIVX(sstr);
2660 else if (sflags & SVp_NOK) {
2661 SvNVX(dstr) = SvNVX(sstr);
2662 (void)SvNOK_only(dstr);
2664 (void)SvIOK_on(dstr);
2665 SvIVX(dstr) = SvIVX(sstr);
2666 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
2671 else if (sflags & SVp_IOK) {
2672 (void)SvIOK_only(dstr);
2673 SvIVX(dstr) = SvIVX(sstr);
2678 if (dtype == SVt_PVGV) {
2679 if (ckWARN(WARN_UNSAFE))
2680 Perl_warner(aTHX_ WARN_UNSAFE, "Undefined value assigned to typeglob");
2683 (void)SvOK_off(dstr);
2689 Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
2691 sv_setsv(dstr,sstr);
2696 Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
2698 register char *dptr;
2699 assert(len >= 0); /* STRLEN is probably unsigned, so this may
2700 elicit a warning, but it won't hurt. */
2701 SV_CHECK_THINKFIRST(sv);
2706 (void)SvUPGRADE(sv, SVt_PV);
2708 SvGROW(sv, len + 1);
2710 Move(ptr,dptr,len,char);
2713 (void)SvPOK_only(sv); /* validate pointer */
2718 Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
2720 sv_setpvn(sv,ptr,len);
2725 Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
2727 register STRLEN len;
2729 SV_CHECK_THINKFIRST(sv);
2735 (void)SvUPGRADE(sv, SVt_PV);
2737 SvGROW(sv, len + 1);
2738 Move(ptr,SvPVX(sv),len+1,char);
2740 (void)SvPOK_only(sv); /* validate pointer */
2745 Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
2752 Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
2754 SV_CHECK_THINKFIRST(sv);
2755 (void)SvUPGRADE(sv, SVt_PV);
2760 (void)SvOOK_off(sv);
2761 if (SvPVX(sv) && SvLEN(sv))
2762 Safefree(SvPVX(sv));
2763 Renew(ptr, len+1, char);
2766 SvLEN_set(sv, len+1);
2768 (void)SvPOK_only(sv); /* validate pointer */
2773 Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
2775 sv_usepvn(sv,ptr,len);
2780 Perl_sv_force_normal(pTHX_ register SV *sv)
2782 if (SvREADONLY(sv)) {
2784 if (PL_curcop != &PL_compiling)
2785 Perl_croak(aTHX_ PL_no_modify);
2789 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
2794 Perl_sv_chop(pTHX_ register SV *sv, register char *ptr) /* like set but assuming ptr is in sv */
2798 register STRLEN delta;
2800 if (!ptr || !SvPOKp(sv))
2802 SV_CHECK_THINKFIRST(sv);
2803 if (SvTYPE(sv) < SVt_PVIV)
2804 sv_upgrade(sv,SVt_PVIV);
2807 if (!SvLEN(sv)) { /* make copy of shared string */
2808 char *pvx = SvPVX(sv);
2809 STRLEN len = SvCUR(sv);
2810 SvGROW(sv, len + 1);
2811 Move(pvx,SvPVX(sv),len,char);
2815 SvFLAGS(sv) |= SVf_OOK;
2817 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK|SVf_IVisUV);
2818 delta = ptr - SvPVX(sv);
2826 Perl_sv_catpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
2831 junk = SvPV_force(sv, tlen);
2832 SvGROW(sv, tlen + len + 1);
2835 Move(ptr,SvPVX(sv)+tlen,len,char);
2838 (void)SvPOK_only(sv); /* validate pointer */
2843 Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
2845 sv_catpvn(sv,ptr,len);
2850 Perl_sv_catsv(pTHX_ SV *dstr, register SV *sstr)
2856 if (s = SvPV(sstr, len))
2857 sv_catpvn(dstr,s,len);
2861 Perl_sv_catsv_mg(pTHX_ SV *dstr, register SV *sstr)
2863 sv_catsv(dstr,sstr);
2868 Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr)
2870 register STRLEN len;
2876 junk = SvPV_force(sv, tlen);
2878 SvGROW(sv, tlen + len + 1);
2881 Move(ptr,SvPVX(sv)+tlen,len+1,char);
2883 (void)SvPOK_only(sv); /* validate pointer */
2888 Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr)
2895 Perl_newSV(pTHX_ STRLEN len)
2901 sv_upgrade(sv, SVt_PV);
2902 SvGROW(sv, len + 1);
2907 /* name is assumed to contain an SV* if (name && namelen == HEf_SVKEY) */
2910 Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen)
2914 if (SvREADONLY(sv)) {
2916 if (PL_curcop != &PL_compiling && !strchr("gBf", how))
2917 Perl_croak(aTHX_ PL_no_modify);
2919 if (SvMAGICAL(sv) || (how == 't' && SvTYPE(sv) >= SVt_PVMG)) {
2920 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
2927 (void)SvUPGRADE(sv, SVt_PVMG);
2929 Newz(702,mg, 1, MAGIC);
2930 mg->mg_moremagic = SvMAGIC(sv);
2933 if (!obj || obj == sv || how == '#' || how == 'r')
2937 mg->mg_obj = SvREFCNT_inc(obj);
2938 mg->mg_flags |= MGf_REFCOUNTED;
2941 mg->mg_len = namlen;
2944 mg->mg_ptr = savepvn(name, namlen);
2945 else if (namlen == HEf_SVKEY)
2946 mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
2950 mg->mg_virtual = &PL_vtbl_sv;
2953 mg->mg_virtual = &PL_vtbl_amagic;
2956 mg->mg_virtual = &PL_vtbl_amagicelem;
2962 mg->mg_virtual = &PL_vtbl_bm;
2965 mg->mg_virtual = &PL_vtbl_regdata;
2968 mg->mg_virtual = &PL_vtbl_regdatum;
2971 mg->mg_virtual = &PL_vtbl_env;
2974 mg->mg_virtual = &PL_vtbl_fm;
2977 mg->mg_virtual = &PL_vtbl_envelem;
2980 mg->mg_virtual = &PL_vtbl_mglob;
2983 mg->mg_virtual = &PL_vtbl_isa;
2986 mg->mg_virtual = &PL_vtbl_isaelem;
2989 mg->mg_virtual = &PL_vtbl_nkeys;
2996 mg->mg_virtual = &PL_vtbl_dbline;
3000 mg->mg_virtual = &PL_vtbl_mutex;
3002 #endif /* USE_THREADS */
3003 #ifdef USE_LOCALE_COLLATE
3005 mg->mg_virtual = &PL_vtbl_collxfrm;
3007 #endif /* USE_LOCALE_COLLATE */
3009 mg->mg_virtual = &PL_vtbl_pack;
3013 mg->mg_virtual = &PL_vtbl_packelem;
3016 mg->mg_virtual = &PL_vtbl_regexp;
3019 mg->mg_virtual = &PL_vtbl_sig;
3022 mg->mg_virtual = &PL_vtbl_sigelem;
3025 mg->mg_virtual = &PL_vtbl_taint;
3029 mg->mg_virtual = &PL_vtbl_uvar;
3032 mg->mg_virtual = &PL_vtbl_vec;
3035 mg->mg_virtual = &PL_vtbl_substr;
3038 mg->mg_virtual = &PL_vtbl_defelem;
3041 mg->mg_virtual = &PL_vtbl_glob;
3044 mg->mg_virtual = &PL_vtbl_arylen;
3047 mg->mg_virtual = &PL_vtbl_pos;
3050 mg->mg_virtual = &PL_vtbl_backref;
3052 case '~': /* Reserved for use by extensions not perl internals. */
3053 /* Useful for attaching extension internal data to perl vars. */
3054 /* Note that multiple extensions may clash if magical scalars */
3055 /* etc holding private data from one are passed to another. */
3059 Perl_croak(aTHX_ "Don't know how to handle magic of type '%c'", how);
3063 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
3067 Perl_sv_unmagic(pTHX_ SV *sv, int type)
3071 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
3074 for (mg = *mgp; mg; mg = *mgp) {
3075 if (mg->mg_type == type) {
3076 MGVTBL* vtbl = mg->mg_virtual;
3077 *mgp = mg->mg_moremagic;
3078 if (vtbl && (vtbl->svt_free != NULL))
3079 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
3080 if (mg->mg_ptr && mg->mg_type != 'g')
3081 if (mg->mg_len >= 0)
3082 Safefree(mg->mg_ptr);
3083 else if (mg->mg_len == HEf_SVKEY)
3084 SvREFCNT_dec((SV*)mg->mg_ptr);
3085 if (mg->mg_flags & MGf_REFCOUNTED)
3086 SvREFCNT_dec(mg->mg_obj);
3090 mgp = &mg->mg_moremagic;
3094 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
3101 Perl_sv_rvweaken(pTHX_ SV *sv)
3104 if (!SvOK(sv)) /* let undefs pass */
3107 Perl_croak(aTHX_ "Can't weaken a nonreference");
3108 else if (SvWEAKREF(sv)) {
3110 if (ckWARN(WARN_MISC))
3111 Perl_warner(aTHX_ WARN_MISC, "Reference is already weak");
3115 sv_add_backref(tsv, sv);
3122 S_sv_add_backref(pTHX_ SV *tsv, SV *sv)
3126 if (SvMAGICAL(tsv) && (mg = mg_find(tsv, '<')))
3127 av = (AV*)mg->mg_obj;
3130 sv_magic(tsv, (SV*)av, '<', NULL, 0);
3131 SvREFCNT_dec(av); /* for sv_magic */
3137 S_sv_del_backref(pTHX_ SV *sv)
3144 if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, '<')))
3145 Perl_croak(aTHX_ "panic: del_backref");
3146 av = (AV *)mg->mg_obj;
3151 svp[i] = &PL_sv_undef; /* XXX */
3158 Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, char *little, STRLEN littlelen)
3162 register char *midend;
3163 register char *bigend;
3169 Perl_croak(aTHX_ "Can't modify non-existent substring");
3170 SvPV_force(bigstr, curlen);
3171 if (offset + len > curlen) {
3172 SvGROW(bigstr, offset+len+1);
3173 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
3174 SvCUR_set(bigstr, offset+len);
3177 i = littlelen - len;
3178 if (i > 0) { /* string might grow */
3179 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
3180 mid = big + offset + len;
3181 midend = bigend = big + SvCUR(bigstr);
3184 while (midend > mid) /* shove everything down */
3185 *--bigend = *--midend;
3186 Move(little,big+offset,littlelen,char);
3192 Move(little,SvPVX(bigstr)+offset,len,char);
3197 big = SvPVX(bigstr);
3200 bigend = big + SvCUR(bigstr);
3202 if (midend > bigend)
3203 Perl_croak(aTHX_ "panic: sv_insert");
3205 if (mid - big > bigend - midend) { /* faster to shorten from end */
3207 Move(little, mid, littlelen,char);
3210 i = bigend - midend;
3212 Move(midend, mid, i,char);
3216 SvCUR_set(bigstr, mid - big);
3219 else if (i = mid - big) { /* faster from front */
3220 midend -= littlelen;
3222 sv_chop(bigstr,midend-i);
3227 Move(little, mid, littlelen,char);
3229 else if (littlelen) {
3230 midend -= littlelen;
3231 sv_chop(bigstr,midend);
3232 Move(little,midend,littlelen,char);
3235 sv_chop(bigstr,midend);
3240 /* make sv point to what nstr did */
3243 Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv)
3246 U32 refcnt = SvREFCNT(sv);
3247 SV_CHECK_THINKFIRST(sv);
3248 if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL))
3249 Perl_warner(aTHX_ WARN_INTERNAL, "Reference miscount in sv_replace()");
3250 if (SvMAGICAL(sv)) {
3254 sv_upgrade(nsv, SVt_PVMG);
3255 SvMAGIC(nsv) = SvMAGIC(sv);
3256 SvFLAGS(nsv) |= SvMAGICAL(sv);
3262 assert(!SvREFCNT(sv));
3263 StructCopy(nsv,sv,SV);
3264 SvREFCNT(sv) = refcnt;
3265 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
3270 Perl_sv_clear(pTHX_ register SV *sv)
3274 assert(SvREFCNT(sv) == 0);
3278 if (PL_defstash) { /* Still have a symbol table? */
3283 Zero(&tmpref, 1, SV);
3284 sv_upgrade(&tmpref, SVt_RV);
3286 SvREADONLY_on(&tmpref); /* DESTROY() could be naughty */
3287 SvREFCNT(&tmpref) = 1;
3290 stash = SvSTASH(sv);
3291 destructor = gv_fetchmethod(SvSTASH(sv), "DESTROY");
3294 PUSHSTACKi(PERLSI_DESTROY);
3295 SvRV(&tmpref) = SvREFCNT_inc(sv);
3300 call_sv((SV*)GvCV(destructor),
3301 G_DISCARD|G_EVAL|G_KEEPERR);
3307 } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
3309 del_XRV(SvANY(&tmpref));
3312 if (PL_in_clean_objs)
3313 Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'",
3315 /* DESTROY gave object new lease on life */
3321 SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */
3322 SvOBJECT_off(sv); /* Curse the object. */
3323 if (SvTYPE(sv) != SVt_PVIO)
3324 --PL_sv_objcount; /* XXX Might want something more general */
3327 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
3330 switch (SvTYPE(sv)) {
3333 IoIFP(sv) != PerlIO_stdin() &&
3334 IoIFP(sv) != PerlIO_stdout() &&
3335 IoIFP(sv) != PerlIO_stderr())
3337 io_close((IO*)sv, FALSE);
3340 PerlDir_close(IoDIRP(sv));
3343 Safefree(IoTOP_NAME(sv));
3344 Safefree(IoFMT_NAME(sv));
3345 Safefree(IoBOTTOM_NAME(sv));
3360 SvREFCNT_dec(LvTARG(sv));
3364 Safefree(GvNAME(sv));
3365 /* cannot decrease stash refcount yet, as we might recursively delete
3366 ourselves when the refcnt drops to zero. Delay SvREFCNT_dec
3367 of stash until current sv is completely gone.
3368 -- JohnPC, 27 Mar 1998 */
3369 stash = GvSTASH(sv);
3375 (void)SvOOK_off(sv);
3383 SvREFCNT_dec(SvRV(sv));
3385 else if (SvPVX(sv) && SvLEN(sv))
3386 Safefree(SvPVX(sv));
3396 switch (SvTYPE(sv)) {
3412 del_XPVIV(SvANY(sv));
3415 del_XPVNV(SvANY(sv));
3418 del_XPVMG(SvANY(sv));
3421 del_XPVLV(SvANY(sv));
3424 del_XPVAV(SvANY(sv));
3427 del_XPVHV(SvANY(sv));
3430 del_XPVCV(SvANY(sv));
3433 del_XPVGV(SvANY(sv));
3434 /* code duplication for increased performance. */
3435 SvFLAGS(sv) &= SVf_BREAK;
3436 SvFLAGS(sv) |= SVTYPEMASK;
3437 /* decrease refcount of the stash that owns this GV, if any */
3439 SvREFCNT_dec(stash);
3440 return; /* not break, SvFLAGS reset already happened */
3442 del_XPVBM(SvANY(sv));
3445 del_XPVFM(SvANY(sv));
3448 del_XPVIO(SvANY(sv));
3451 SvFLAGS(sv) &= SVf_BREAK;
3452 SvFLAGS(sv) |= SVTYPEMASK;
3456 Perl_sv_newref(pTHX_ SV *sv)
3459 ATOMIC_INC(SvREFCNT(sv));
3464 Perl_sv_free(pTHX_ SV *sv)
3467 int refcount_is_zero;
3471 if (SvREFCNT(sv) == 0) {
3472 if (SvFLAGS(sv) & SVf_BREAK)
3474 if (PL_in_clean_all) /* All is fair */
3476 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
3477 /* make sure SvREFCNT(sv)==0 happens very seldom */
3478 SvREFCNT(sv) = (~(U32)0)/2;
3481 if (ckWARN_d(WARN_INTERNAL))
3482 Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free unreferenced scalar");
3485 ATOMIC_DEC_AND_TEST(refcount_is_zero, SvREFCNT(sv));
3486 if (!refcount_is_zero)
3490 if (ckWARN_d(WARN_DEBUGGING))
3491 Perl_warner(aTHX_ WARN_DEBUGGING,
3492 "Attempt to free temp prematurely: SV 0x%lx", (unsigned long)sv);
3496 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
3497 /* make sure SvREFCNT(sv)==0 happens very seldom */
3498 SvREFCNT(sv) = (~(U32)0)/2;
3507 Perl_sv_len(pTHX_ register SV *sv)
3516 len = mg_length(sv);
3518 junk = SvPV(sv, len);
3523 Perl_sv_len_utf8(pTHX_ register SV *sv)
3534 len = mg_length(sv);
3537 s = (U8*)SvPV(sv, len);
3548 Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp)
3553 I32 uoffset = *offsetp;
3559 start = s = (U8*)SvPV(sv, len);
3561 while (s < send && uoffset--)
3565 *offsetp = s - start;
3569 while (s < send && ulen--)
3579 Perl_sv_pos_b2u(pTHX_ register SV *sv, I32* offsetp)
3588 s = (U8*)SvPV(sv, len);
3590 Perl_croak(aTHX_ "panic: bad byte offset");
3591 send = s + *offsetp;
3599 if (ckWARN_d(WARN_UTF8))
3600 Perl_warner(aTHX_ WARN_UTF8, "Malformed UTF-8 character");
3608 Perl_sv_eq(pTHX_ register SV *str1, register SV *str2)
3620 pv1 = SvPV(str1, cur1);
3625 pv2 = SvPV(str2, cur2);
3630 return memEQ(pv1, pv2, cur1);
3634 Perl_sv_cmp(pTHX_ register SV *str1, register SV *str2)
3637 char *pv1 = str1 ? SvPV(str1, cur1) : (char *) NULL;
3639 char *pv2 = str2 ? SvPV(str2, cur2) : (char *) NULL;
3643 return cur2 ? -1 : 0;
3648 retval = memcmp((void*)pv1, (void*)pv2, cur1 < cur2 ? cur1 : cur2);
3651 return retval < 0 ? -1 : 1;
3656 return cur1 < cur2 ? -1 : 1;
3660 Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2)
3662 #ifdef USE_LOCALE_COLLATE
3668 if (PL_collation_standard)
3672 pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL;
3674 pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL;
3676 if (!pv1 || !len1) {
3687 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
3690 return retval < 0 ? -1 : 1;
3693 * When the result of collation is equality, that doesn't mean
3694 * that there are no differences -- some locales exclude some
3695 * characters from consideration. So to avoid false equalities,
3696 * we use the raw string as a tiebreaker.
3702 #endif /* USE_LOCALE_COLLATE */
3704 return sv_cmp(sv1, sv2);
3707 #ifdef USE_LOCALE_COLLATE
3709 * Any scalar variable may carry an 'o' magic that contains the
3710 * scalar data of the variable transformed to such a format that
3711 * a normal memory comparison can be used to compare the data
3712 * according to the locale settings.
3715 Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp)
3719 mg = SvMAGICAL(sv) ? mg_find(sv, 'o') : (MAGIC *) NULL;
3720 if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
3725 Safefree(mg->mg_ptr);
3727 if ((xf = mem_collxfrm(s, len, &xlen))) {
3728 if (SvREADONLY(sv)) {
3731 return xf + sizeof(PL_collation_ix);
3734 sv_magic(sv, 0, 'o', 0, 0);
3735 mg = mg_find(sv, 'o');
3748 if (mg && mg->mg_ptr) {
3750 return mg->mg_ptr + sizeof(PL_collation_ix);
3758 #endif /* USE_LOCALE_COLLATE */
3761 Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append)
3766 register STDCHAR rslast;
3767 register STDCHAR *bp;
3771 SV_CHECK_THINKFIRST(sv);
3772 (void)SvUPGRADE(sv, SVt_PV);
3776 if (RsSNARF(PL_rs)) {
3780 else if (RsRECORD(PL_rs)) {
3781 I32 recsize, bytesread;
3784 /* Grab the size of the record we're getting */
3785 recsize = SvIV(SvRV(PL_rs));
3786 (void)SvPOK_only(sv); /* Validate pointer */
3787 buffer = SvGROW(sv, recsize + 1);
3790 /* VMS wants read instead of fread, because fread doesn't respect */
3791 /* RMS record boundaries. This is not necessarily a good thing to be */
3792 /* doing, but we've got no other real choice */
3793 bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize);
3795 bytesread = PerlIO_read(fp, buffer, recsize);
3797 SvCUR_set(sv, bytesread);
3798 buffer[bytesread] = '\0';
3799 return(SvCUR(sv) ? SvPVX(sv) : Nullch);
3801 else if (RsPARA(PL_rs)) {
3806 rsptr = SvPV(PL_rs, rslen);
3807 rslast = rslen ? rsptr[rslen - 1] : '\0';
3809 if (RsPARA(PL_rs)) { /* have to do this both before and after */
3810 do { /* to make sure file boundaries work right */
3813 i = PerlIO_getc(fp);
3817 PerlIO_ungetc(fp,i);
3823 /* See if we know enough about I/O mechanism to cheat it ! */
3825 /* This used to be #ifdef test - it is made run-time test for ease
3826 of abstracting out stdio interface. One call should be cheap
3827 enough here - and may even be a macro allowing compile
3831 if (PerlIO_fast_gets(fp)) {
3834 * We're going to steal some values from the stdio struct
3835 * and put EVERYTHING in the innermost loop into registers.
3837 register STDCHAR *ptr;
3841 #if defined(VMS) && defined(PERLIO_IS_STDIO)
3842 /* An ungetc()d char is handled separately from the regular
3843 * buffer, so we getc() it back out and stuff it in the buffer.
3845 i = PerlIO_getc(fp);
3846 if (i == EOF) return 0;
3847 *(--((*fp)->_ptr)) = (unsigned char) i;
3851 /* Here is some breathtakingly efficient cheating */
3853 cnt = PerlIO_get_cnt(fp); /* get count into register */
3854 (void)SvPOK_only(sv); /* validate pointer */
3855 if (SvLEN(sv) - append <= cnt + 1) { /* make sure we have the room */
3856 if (cnt > 80 && SvLEN(sv) > append) {
3857 shortbuffered = cnt - SvLEN(sv) + append + 1;
3858 cnt -= shortbuffered;
3862 /* remember that cnt can be negative */
3863 SvGROW(sv, append + (cnt <= 0 ? 2 : (cnt + 1)));
3868 bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */
3869 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
3870 DEBUG_P(PerlIO_printf(Perl_debug_log,
3871 "Screamer: entering, ptr=%ld, cnt=%ld\n",(long)ptr,(long)cnt));
3872 DEBUG_P(PerlIO_printf(Perl_debug_log,
3873 "Screamer: entering: FILE * thinks ptr=%ld, cnt=%ld, base=%ld\n",
3874 (long)PerlIO_get_ptr(fp), (long)PerlIO_get_cnt(fp),
3875 (long)(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
3880 while (cnt > 0) { /* this | eat */
3882 if ((*bp++ = *ptr++) == rslast) /* really | dust */
3883 goto thats_all_folks; /* screams | sed :-) */
3887 Copy(ptr, bp, cnt, char); /* this | eat */
3888 bp += cnt; /* screams | dust */
3889 ptr += cnt; /* louder | sed :-) */
3894 if (shortbuffered) { /* oh well, must extend */
3895 cnt = shortbuffered;
3897 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
3899 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
3900 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
3904 DEBUG_P(PerlIO_printf(Perl_debug_log,
3905 "Screamer: going to getc, ptr=%ld, cnt=%ld\n",(long)ptr,(long)cnt));
3906 PerlIO_set_ptrcnt(fp, ptr, cnt); /* deregisterize cnt and ptr */
3907 DEBUG_P(PerlIO_printf(Perl_debug_log,
3908 "Screamer: pre: FILE * thinks ptr=%ld, cnt=%ld, base=%ld\n",
3909 (long)PerlIO_get_ptr(fp), (long)PerlIO_get_cnt(fp),
3910 (long)(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
3911 /* This used to call 'filbuf' in stdio form, but as that behaves like
3912 getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
3913 another abstraction. */
3914 i = PerlIO_getc(fp); /* get more characters */
3915 DEBUG_P(PerlIO_printf(Perl_debug_log,
3916 "Screamer: post: FILE * thinks ptr=%ld, cnt=%ld, base=%ld\n",
3917 (long)PerlIO_get_ptr(fp), (long)PerlIO_get_cnt(fp),
3918 (long)(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
3919 cnt = PerlIO_get_cnt(fp);
3920 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
3921 DEBUG_P(PerlIO_printf(Perl_debug_log,
3922 "Screamer: after getc, ptr=%ld, cnt=%ld\n",(long)ptr,(long)cnt));
3924 if (i == EOF) /* all done for ever? */
3925 goto thats_really_all_folks;
3927 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
3929 SvGROW(sv, bpx + cnt + 2);
3930 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
3932 *bp++ = i; /* store character from PerlIO_getc */
3934 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
3935 goto thats_all_folks;
3939 if ((rslen > 1 && (bp - (STDCHAR*)SvPVX(sv) < rslen)) ||
3940 memNE((char*)bp - rslen, rsptr, rslen))
3941 goto screamer; /* go back to the fray */
3942 thats_really_all_folks:
3944 cnt += shortbuffered;
3945 DEBUG_P(PerlIO_printf(Perl_debug_log,
3946 "Screamer: quitting, ptr=%ld, cnt=%ld\n",(long)ptr,(long)cnt));
3947 PerlIO_set_ptrcnt(fp, ptr, cnt); /* put these back or we're in trouble */
3948 DEBUG_P(PerlIO_printf(Perl_debug_log,
3949 "Screamer: end: FILE * thinks ptr=%ld, cnt=%ld, base=%ld\n",
3950 (long)PerlIO_get_ptr(fp), (long)PerlIO_get_cnt(fp),
3951 (long)(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
3953 SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */
3954 DEBUG_P(PerlIO_printf(Perl_debug_log,
3955 "Screamer: done, len=%ld, string=|%.*s|\n",
3956 (long)SvCUR(sv),(int)SvCUR(sv),SvPVX(sv)));
3961 /*The big, slow, and stupid way */
3964 /* Need to work around EPOC SDK features */
3965 /* On WINS: MS VC5 generates calls to _chkstk, */
3966 /* if a `large' stack frame is allocated */
3967 /* gcc on MARM does not generate calls like these */
3973 register STDCHAR *bpe = buf + sizeof(buf);
3975 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = i) != rslast && bp < bpe)
3976 ; /* keep reading */
3980 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
3981 /* Accomodate broken VAXC compiler, which applies U8 cast to
3982 * both args of ?: operator, causing EOF to change into 255
3984 if (cnt) { i = (U8)buf[cnt - 1]; } else { i = EOF; }
3988 sv_catpvn(sv, (char *) buf, cnt);
3990 sv_setpvn(sv, (char *) buf, cnt);
3992 if (i != EOF && /* joy */
3994 SvCUR(sv) < rslen ||
3995 memNE(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
3999 * If we're reading from a TTY and we get a short read,
4000 * indicating that the user hit his EOF character, we need
4001 * to notice it now, because if we try to read from the TTY
4002 * again, the EOF condition will disappear.
4004 * The comparison of cnt to sizeof(buf) is an optimization
4005 * that prevents unnecessary calls to feof().
4009 if (!(cnt < sizeof(buf) && PerlIO_eof(fp)))
4014 if (RsPARA(PL_rs)) { /* have to do this both before and after */
4015 while (i != EOF) { /* to make sure file boundaries work right */
4016 i = PerlIO_getc(fp);
4018 PerlIO_ungetc(fp,i);
4025 win32_strip_return(sv);
4028 return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch;
4033 Perl_sv_inc(pTHX_ register SV *sv)
4042 if (SvTHINKFIRST(sv)) {
4043 if (SvREADONLY(sv)) {
4045 if (PL_curcop != &PL_compiling)
4046 Perl_croak(aTHX_ PL_no_modify);
4050 if (SvAMAGIC(sv) && AMG_CALLun(sv,inc))
4052 i = PTR2IV(SvRV(sv));
4057 flags = SvFLAGS(sv);
4058 if (flags & SVp_NOK) {
4059 (void)SvNOK_only(sv);
4063 if (flags & SVp_IOK) {
4065 if (SvUVX(sv) == UV_MAX)
4066 sv_setnv(sv, (NV)UV_MAX + 1.0);
4068 (void)SvIOK_only_UV(sv);
4071 if (SvIVX(sv) == IV_MAX)
4072 sv_setnv(sv, (NV)IV_MAX + 1.0);
4074 (void)SvIOK_only(sv);
4080 if (!(flags & SVp_POK) || !*SvPVX(sv)) {
4081 if ((flags & SVTYPEMASK) < SVt_PVNV)
4082 sv_upgrade(sv, SVt_NV);
4084 (void)SvNOK_only(sv);
4088 while (isALPHA(*d)) d++;
4089 while (isDIGIT(*d)) d++;
4091 sv_setnv(sv,Atof(SvPVX(sv)) + 1.0); /* punt */
4095 while (d >= SvPVX(sv)) {
4103 /* MKS: The original code here died if letters weren't consecutive.
4104 * at least it didn't have to worry about non-C locales. The
4105 * new code assumes that ('z'-'a')==('Z'-'A'), letters are
4106 * arranged in order (although not consecutively) and that only
4107 * [A-Za-z] are accepted by isALPHA in the C locale.
4109 if (*d != 'z' && *d != 'Z') {
4110 do { ++*d; } while (!isALPHA(*d));
4113 *(d--) -= 'z' - 'a';
4118 *(d--) -= 'z' - 'a' + 1;
4122 /* oh,oh, the number grew */
4123 SvGROW(sv, SvCUR(sv) + 2);
4125 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--)
4134 Perl_sv_dec(pTHX_ register SV *sv)
4142 if (SvTHINKFIRST(sv)) {
4143 if (SvREADONLY(sv)) {
4145 if (PL_curcop != &PL_compiling)
4146 Perl_croak(aTHX_ PL_no_modify);
4150 if (SvAMAGIC(sv) && AMG_CALLun(sv,dec))
4152 i = PTR2IV(SvRV(sv));
4157 flags = SvFLAGS(sv);
4158 if (flags & SVp_NOK) {
4160 (void)SvNOK_only(sv);
4163 if (flags & SVp_IOK) {
4165 if (SvUVX(sv) == 0) {
4166 (void)SvIOK_only(sv);
4170 (void)SvIOK_only_UV(sv);
4174 if (SvIVX(sv) == IV_MIN)
4175 sv_setnv(sv, (NV)IV_MIN - 1.0);
4177 (void)SvIOK_only(sv);
4183 if (!(flags & SVp_POK)) {
4184 if ((flags & SVTYPEMASK) < SVt_PVNV)
4185 sv_upgrade(sv, SVt_NV);
4187 (void)SvNOK_only(sv);
4190 sv_setnv(sv,Atof(SvPVX(sv)) - 1.0); /* punt */
4193 /* Make a string that will exist for the duration of the expression
4194 * evaluation. Actually, it may have to last longer than that, but
4195 * hopefully we won't free it until it has been assigned to a
4196 * permanent location. */
4199 Perl_sv_mortalcopy(pTHX_ SV *oldstr)
4205 sv_setsv(sv,oldstr);
4207 PL_tmps_stack[++PL_tmps_ix] = sv;
4213 Perl_sv_newmortal(pTHX)
4219 SvFLAGS(sv) = SVs_TEMP;
4221 PL_tmps_stack[++PL_tmps_ix] = sv;
4225 /* same thing without the copying */
4228 Perl_sv_2mortal(pTHX_ register SV *sv)
4233 if (SvREADONLY(sv) && SvIMMORTAL(sv))
4236 PL_tmps_stack[++PL_tmps_ix] = sv;
4242 Perl_newSVpv(pTHX_ const char *s, STRLEN len)
4249 sv_setpvn(sv,s,len);
4254 Perl_newSVpvn(pTHX_ const char *s, STRLEN len)
4259 sv_setpvn(sv,s,len);
4263 #if defined(PERL_IMPLICIT_CONTEXT)
4265 Perl_newSVpvf_nocontext(const char* pat, ...)
4270 va_start(args, pat);
4271 sv = vnewSVpvf(pat, &args);
4278 Perl_newSVpvf(pTHX_ const char* pat, ...)
4282 va_start(args, pat);
4283 sv = vnewSVpvf(pat, &args);
4289 Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args)
4293 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
4298 Perl_newSVnv(pTHX_ NV n)
4308 Perl_newSViv(pTHX_ IV i)
4318 Perl_newRV_noinc(pTHX_ SV *tmpRef)
4324 sv_upgrade(sv, SVt_RV);
4332 Perl_newRV(pTHX_ SV *tmpRef)
4334 return newRV_noinc(SvREFCNT_inc(tmpRef));
4337 /* make an exact duplicate of old */
4340 Perl_newSVsv(pTHX_ register SV *old)
4347 if (SvTYPE(old) == SVTYPEMASK) {
4348 if (ckWARN_d(WARN_INTERNAL))
4349 Perl_warner(aTHX_ WARN_INTERNAL, "semi-panic: attempt to dup freed string");
4364 Perl_sv_reset(pTHX_ register char *s, HV *stash)
4372 char todo[PERL_UCHAR_MAX+1];
4377 if (!*s) { /* reset ?? searches */
4378 for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) {
4379 pm->op_pmdynflags &= ~PMdf_USED;
4384 /* reset variables */
4386 if (!HvARRAY(stash))
4389 Zero(todo, 256, char);
4391 i = (unsigned char)*s;
4395 max = (unsigned char)*s++;
4396 for ( ; i <= max; i++) {
4399 for (i = 0; i <= (I32) HvMAX(stash); i++) {
4400 for (entry = HvARRAY(stash)[i];
4402 entry = HeNEXT(entry))
4404 if (!todo[(U8)*HeKEY(entry)])
4406 gv = (GV*)HeVAL(entry);
4408 if (SvTHINKFIRST(sv)) {
4409 if (!SvREADONLY(sv) && SvROK(sv))
4414 if (SvTYPE(sv) >= SVt_PV) {
4416 if (SvPVX(sv) != Nullch)
4423 if (GvHV(gv) && !HvNAME(GvHV(gv))) {
4425 #ifndef VMS /* VMS has no environ array */
4427 environ[0] = Nullch;
4436 Perl_sv_2io(pTHX_ SV *sv)
4442 switch (SvTYPE(sv)) {
4450 Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv));
4454 Perl_croak(aTHX_ PL_no_usym, "filehandle");
4456 return sv_2io(SvRV(sv));
4457 gv = gv_fetchpv(SvPV(sv,n_a), FALSE, SVt_PVIO);
4463 Perl_croak(aTHX_ "Bad filehandle: %s", SvPV(sv,n_a));
4470 Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref)
4477 return *gvp = Nullgv, Nullcv;
4478 switch (SvTYPE(sv)) {
4498 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
4499 tryAMAGICunDEREF(to_cv);
4502 if (SvTYPE(sv) == SVt_PVCV) {
4511 Perl_croak(aTHX_ "Not a subroutine reference");
4516 gv = gv_fetchpv(SvPV(sv, n_a), lref, SVt_PVCV);
4522 if (lref && !GvCVu(gv)) {
4525 tmpsv = NEWSV(704,0);
4526 gv_efullname3(tmpsv, gv, Nullch);
4527 /* XXX this is probably not what they think they're getting.
4528 * It has the same effect as "sub name;", i.e. just a forward
4530 newSUB(start_subparse(FALSE, 0),
4531 newSVOP(OP_CONST, 0, tmpsv),
4536 Perl_croak(aTHX_ "Unable to create sub named \"%s\"", SvPV(sv,n_a));
4543 Perl_sv_true(pTHX_ register SV *sv)
4550 if ((tXpv = (XPV*)SvANY(sv)) &&
4551 (*tXpv->xpv_pv > '0' ||
4552 tXpv->xpv_cur > 1 ||
4553 (tXpv->xpv_cur && *tXpv->xpv_pv != '0')))
4560 return SvIVX(sv) != 0;
4563 return SvNVX(sv) != 0.0;
4565 return sv_2bool(sv);
4571 Perl_sv_iv(pTHX_ register SV *sv)
4575 return (IV)SvUVX(sv);
4582 Perl_sv_uv(pTHX_ register SV *sv)
4587 return (UV)SvIVX(sv);
4593 Perl_sv_nv(pTHX_ register SV *sv)
4601 Perl_sv_pv(pTHX_ SV *sv)
4608 return sv_2pv(sv, &n_a);
4612 Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp)
4618 return sv_2pv(sv, lp);
4622 Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp)
4626 if (SvTHINKFIRST(sv) && !SvROK(sv))
4627 sv_force_normal(sv);
4633 if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) {
4635 Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
4636 PL_op_name[PL_op->op_type]);
4640 if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */
4645 (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */
4646 SvGROW(sv, len + 1);
4647 Move(s,SvPVX(sv),len,char);
4652 SvPOK_on(sv); /* validate pointer */
4654 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2pv(%s)\n",
4655 (unsigned long)sv,SvPVX(sv)));
4662 Perl_sv_reftype(pTHX_ SV *sv, int ob)
4664 if (ob && SvOBJECT(sv))
4665 return HvNAME(SvSTASH(sv));
4667 switch (SvTYPE(sv)) {
4681 case SVt_PVLV: return "LVALUE";
4682 case SVt_PVAV: return "ARRAY";
4683 case SVt_PVHV: return "HASH";
4684 case SVt_PVCV: return "CODE";
4685 case SVt_PVGV: return "GLOB";
4686 case SVt_PVFM: return "FORMAT";
4687 default: return "UNKNOWN";
4693 Perl_sv_isobject(pTHX_ SV *sv)
4708 Perl_sv_isa(pTHX_ SV *sv, const char *name)
4720 return strEQ(HvNAME(SvSTASH(sv)), name);
4724 Perl_newSVrv(pTHX_ SV *rv, const char *classname)
4731 SV_CHECK_THINKFIRST(rv);
4734 if (SvTYPE(rv) < SVt_RV)
4735 sv_upgrade(rv, SVt_RV);
4742 HV* stash = gv_stashpv(classname, TRUE);
4743 (void)sv_bless(rv, stash);
4749 Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv)
4752 sv_setsv(rv, &PL_sv_undef);
4756 sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
4761 Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv)
4763 sv_setiv(newSVrv(rv,classname), iv);
4768 Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv)
4770 sv_setnv(newSVrv(rv,classname), nv);
4775 Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, char *pv, STRLEN n)
4777 sv_setpvn(newSVrv(rv,classname), pv, n);
4782 Perl_sv_bless(pTHX_ SV *sv, HV *stash)
4787 Perl_croak(aTHX_ "Can't bless non-reference value");
4789 if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) {
4790 if (SvREADONLY(tmpRef))
4791 Perl_croak(aTHX_ PL_no_modify);
4792 if (SvOBJECT(tmpRef)) {
4793 if (SvTYPE(tmpRef) != SVt_PVIO)
4795 SvREFCNT_dec(SvSTASH(tmpRef));
4798 SvOBJECT_on(tmpRef);
4799 if (SvTYPE(tmpRef) != SVt_PVIO)
4801 (void)SvUPGRADE(tmpRef, SVt_PVMG);
4802 SvSTASH(tmpRef) = (HV*)SvREFCNT_inc(stash);
4813 S_sv_unglob(pTHX_ SV *sv)
4815 assert(SvTYPE(sv) == SVt_PVGV);
4820 SvREFCNT_dec(GvSTASH(sv));
4821 GvSTASH(sv) = Nullhv;
4823 sv_unmagic(sv, '*');
4824 Safefree(GvNAME(sv));
4826 SvFLAGS(sv) &= ~SVTYPEMASK;
4827 SvFLAGS(sv) |= SVt_PVMG;
4831 Perl_sv_unref(pTHX_ SV *sv)
4835 if (SvWEAKREF(sv)) {
4843 if (SvREFCNT(rv) != 1 || SvREADONLY(rv))
4846 sv_2mortal(rv); /* Schedule for freeing later */
4850 Perl_sv_taint(pTHX_ SV *sv)
4852 sv_magic((sv), Nullsv, 't', Nullch, 0);
4856 Perl_sv_untaint(pTHX_ SV *sv)
4858 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
4859 MAGIC *mg = mg_find(sv, 't');
4866 Perl_sv_tainted(pTHX_ SV *sv)
4868 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
4869 MAGIC *mg = mg_find(sv, 't');
4870 if (mg && ((mg->mg_len & 1) || (mg->mg_len & 2) && mg->mg_obj == sv))
4877 Perl_sv_setpviv(pTHX_ SV *sv, IV iv)
4879 char buf[TYPE_CHARS(UV)];
4881 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
4883 sv_setpvn(sv, ptr, ebuf - ptr);
4888 Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv)
4890 char buf[TYPE_CHARS(UV)];
4892 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
4894 sv_setpvn(sv, ptr, ebuf - ptr);
4898 #if defined(PERL_IMPLICIT_CONTEXT)
4900 Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...)
4904 va_start(args, pat);
4905 sv_vsetpvf(sv, pat, &args);
4911 Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...)
4915 va_start(args, pat);
4916 sv_vsetpvf_mg(sv, pat, &args);
4922 Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...)
4925 va_start(args, pat);
4926 sv_vsetpvf(sv, pat, &args);
4931 Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args)
4933 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
4937 Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...)
4940 va_start(args, pat);
4941 sv_vsetpvf_mg(sv, pat, &args);
4946 Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
4948 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
4952 #if defined(PERL_IMPLICIT_CONTEXT)
4954 Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...)
4958 va_start(args, pat);
4959 sv_vcatpvf(sv, pat, &args);
4964 Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...)
4968 va_start(args, pat);
4969 sv_vcatpvf_mg(sv, pat, &args);
4975 Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...)
4978 va_start(args, pat);
4979 sv_vcatpvf(sv, pat, &args);
4984 Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args)
4986 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
4990 Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...)
4993 va_start(args, pat);
4994 sv_vcatpvf_mg(sv, pat, &args);
4999 Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
5001 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
5006 Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
5008 sv_setpvn(sv, "", 0);
5009 sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted);
5013 Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
5021 static char nullstr[] = "(null)";
5023 /* no matter what, this is a string now */
5024 (void)SvPV_force(sv, origlen);
5026 /* special-case "", "%s", and "%_" */
5029 if (patlen == 2 && pat[0] == '%') {
5033 char *s = va_arg(*args, char*);
5034 sv_catpv(sv, s ? s : nullstr);
5036 else if (svix < svmax)
5037 sv_catsv(sv, *svargs);
5041 sv_catsv(sv, va_arg(*args, SV*));
5044 /* See comment on '_' below */
5049 patend = (char*)pat + patlen;
5050 for (p = (char*)pat; p < patend; p = q) {
5058 bool has_precis = FALSE;
5063 STRLEN esignlen = 0;
5065 char *eptr = Nullch;
5067 /* Times 4: a decimal digit takes more than 3 binary digits.
5068 * NV_DIG: mantissa takes than many decimal digits.
5069 * Plus 32: Playing safe. */
5070 char ebuf[IV_DIG * 4 + NV_DIG + 32];
5071 /* large enough for "%#.#f" --chip */
5072 /* what about long double NVs? --jhi */
5083 for (q = p; q < patend && *q != '%'; ++q) ;
5085 sv_catpvn(sv, p, q - p);
5123 case '1': case '2': case '3':
5124 case '4': case '5': case '6':
5125 case '7': case '8': case '9':
5128 width = width * 10 + (*q++ - '0');
5133 i = va_arg(*args, int);
5135 i = (svix < svmax) ? SvIVx(svargs[svix++]) : 0;
5137 width = (i < 0) ? -i : i;
5148 i = va_arg(*args, int);
5150 i = (svix < svmax) ? SvIVx(svargs[svix++]) : 0;
5151 precis = (i < 0) ? 0 : i;
5157 precis = precis * 10 + (*q++ - '0');
5167 if (*(q + 1) == 'l') { /* lld */
5199 uv = va_arg(*args, int);
5201 uv = (svix < svmax) ? SvIVx(svargs[svix++]) : 0;
5203 eptr = (char*)utf8buf;
5204 elen = uv_to_utf8((U8*)eptr, uv) - utf8buf;
5208 c = va_arg(*args, int);
5210 c = (svix < svmax) ? SvIVx(svargs[svix++]) : 0;
5217 eptr = va_arg(*args, char*);
5219 elen = strlen(eptr);
5222 elen = sizeof nullstr - 1;
5225 else if (svix < svmax) {
5226 eptr = SvPVx(svargs[svix++], elen);
5228 if (has_precis && precis < elen) {
5230 sv_pos_u2b(svargs[svix - 1], &p, 0); /* sticks at end */
5233 if (width) { /* fudge width (can't fudge elen) */
5234 width += elen - sv_len_utf8(svargs[svix - 1]);
5242 * The "%_" hack might have to be changed someday,
5243 * if ISO or ANSI decide to use '_' for something.
5244 * So we keep it hidden from users' code.
5248 eptr = SvPVx(va_arg(*args, SV*), elen);
5251 if (has_precis && elen > precis)
5259 uv = PTR2UV(va_arg(*args, void*));
5261 uv = (svix < svmax) ? PTR2UV(svargs[svix++]) : 0;
5276 case 'h': iv = (short)va_arg(*args, int); break;
5277 default: iv = va_arg(*args, int); break;
5278 case 'l': iv = va_arg(*args, long); break;
5279 case 'V': iv = va_arg(*args, IV); break;
5281 case 'q': iv = va_arg(*args, Quad_t); break;
5286 iv = (svix < svmax) ? SvIVx(svargs[svix++]) : 0;
5288 case 'h': iv = (short)iv; break;
5289 default: iv = (int)iv; break;
5290 case 'l': iv = (long)iv; break;
5293 case 'q': iv = (Quad_t)iv; break;
5300 esignbuf[esignlen++] = plus;
5304 esignbuf[esignlen++] = '-';
5342 case 'h': uv = (unsigned short)va_arg(*args, unsigned); break;
5343 default: uv = va_arg(*args, unsigned); break;
5344 case 'l': uv = va_arg(*args, unsigned long); break;
5345 case 'V': uv = va_arg(*args, UV); break;
5347 case 'q': uv = va_arg(*args, Quad_t); break;
5352 uv = (svix < svmax) ? SvUVx(svargs[svix++]) : 0;
5354 case 'h': uv = (unsigned short)uv; break;
5355 default: uv = (unsigned)uv; break;
5356 case 'l': uv = (unsigned long)uv; break;
5359 case 'q': uv = (Quad_t)uv; break;
5365 eptr = ebuf + sizeof ebuf;
5371 p = (c == 'X') ? "0123456789ABCDEF" : "0123456789abcdef";
5377 esignbuf[esignlen++] = '0';
5378 esignbuf[esignlen++] = c; /* 'x' or 'X' */
5384 *--eptr = '0' + dig;
5386 if (alt && *eptr != '0')
5392 *--eptr = '0' + dig;
5395 esignbuf[esignlen++] = '0';
5396 esignbuf[esignlen++] = 'b';
5399 default: /* it had better be ten or less */
5400 #if defined(PERL_Y2KWARN)
5401 if (ckWARN(WARN_MISC)) {
5403 char *s = SvPV(sv,n);
5404 if (n >= 2 && s[n-2] == '1' && s[n-1] == '9'
5405 && (n == 2 || !isDIGIT(s[n-3])))
5407 Perl_warner(aTHX_ WARN_MISC,
5408 "Possible Y2K bug: %%%c %s",
5409 c, "format string following '19'");
5415 *--eptr = '0' + dig;
5416 } while (uv /= base);
5419 elen = (ebuf + sizeof ebuf) - eptr;
5422 zeros = precis - elen;
5423 else if (precis == 0 && elen == 1 && *eptr == '0')
5428 /* FLOATING POINT */
5431 c = 'f'; /* maybe %F isn't supported here */
5437 /* This is evil, but floating point is even more evil */
5440 nv = va_arg(*args, NV);
5442 nv = (svix < svmax) ? SvNVx(svargs[svix++]) : 0.0;
5445 if (c != 'e' && c != 'E') {
5447 (void)frexp(nv, &i);
5448 if (i == PERL_INT_MIN)
5449 Perl_die(aTHX_ "panic: frexp");
5451 need = BIT_DIGITS(i);
5453 need += has_precis ? precis : 6; /* known default */
5457 need += 20; /* fudge factor */
5458 if (PL_efloatsize < need) {
5459 Safefree(PL_efloatbuf);
5460 PL_efloatsize = need + 20; /* more fudge */
5461 New(906, PL_efloatbuf, PL_efloatsize, char);
5462 PL_efloatbuf[0] = '\0';
5465 eptr = ebuf + sizeof ebuf;
5468 #ifdef USE_LONG_DOUBLE
5470 char* p = PERL_PRIfldbl + sizeof(PERL_PRIfldbl) - 3;
5471 while (p >= PERL_PRIfldbl) { *--eptr = *p--; }
5476 do { *--eptr = '0' + (base % 10); } while (base /= 10);
5481 do { *--eptr = '0' + (base % 10); } while (base /= 10);
5494 RESTORE_NUMERIC_STANDARD();
5495 (void)sprintf(PL_efloatbuf, eptr, nv);
5496 RESTORE_NUMERIC_LOCAL();
5499 eptr = PL_efloatbuf;
5500 elen = strlen(PL_efloatbuf);
5502 #ifdef USE_LOCALE_NUMERIC
5504 * User-defined locales may include arbitrary characters.
5505 * And, unfortunately, some (broken) systems may allow the
5506 * "C" locale to be overridden by a malicious user.
5507 * XXX This is an extreme way to cope with broken systems.
5509 if (maybe_tainted && PL_tainting) {
5510 /* safe if it matches /[-+]?\d*(\.\d*)?([eE][-+]?\d*)?/ */
5511 if (*eptr == '-' || *eptr == '+')
5513 while (isDIGIT(*eptr))
5517 while (isDIGIT(*eptr))
5520 if (*eptr == 'e' || *eptr == 'E') {
5522 if (*eptr == '-' || *eptr == '+')
5524 while (isDIGIT(*eptr))
5528 *maybe_tainted = TRUE; /* results are suspect */
5529 eptr = PL_efloatbuf;
5531 #endif /* USE_LOCALE_NUMERIC */
5538 i = SvCUR(sv) - origlen;
5541 case 'h': *(va_arg(*args, short*)) = i; break;
5542 default: *(va_arg(*args, int*)) = i; break;
5543 case 'l': *(va_arg(*args, long*)) = i; break;
5544 case 'V': *(va_arg(*args, IV*)) = i; break;
5546 case 'q': *(va_arg(*args, Quad_t*)) = i; break;
5550 else if (svix < svmax)
5551 sv_setuv(svargs[svix++], (UV)i);
5552 continue; /* not "break" */
5558 if (!args && ckWARN(WARN_PRINTF) &&
5559 (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) {
5560 SV *msg = sv_newmortal();
5561 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %s: ",
5562 (PL_op->op_type == OP_PRTF) ? "printf" : "sprintf");
5566 Perl_sv_catpvf(aTHX_ msg,
5567 "\"%%%c\"", c & 0xFF);
5569 Perl_sv_catpvf(aTHX_ msg,
5570 "\"%%\\%03" PERL_PRIo64 "\"",
5573 Perl_sv_catpvf(aTHX_ msg, isPRINT(c) ?
5574 "\"%%%c\"" : "\"%%\\%03o\"",
5578 sv_catpv(msg, "end of string");
5579 Perl_warner(aTHX_ WARN_PRINTF, "%_", msg); /* yes, this is reentrant */
5582 /* output mangled stuff ... */
5588 /* ... right here, because formatting flags should not apply */
5589 SvGROW(sv, SvCUR(sv) + elen + 1);
5591 memcpy(p, eptr, elen);
5594 SvCUR(sv) = p - SvPVX(sv);
5595 continue; /* not "break" */
5598 have = esignlen + zeros + elen;
5599 need = (have > width ? have : width);
5602 SvGROW(sv, SvCUR(sv) + need + 1);
5604 if (esignlen && fill == '0') {
5605 for (i = 0; i < esignlen; i++)
5609 memset(p, fill, gap);
5612 if (esignlen && fill != '0') {
5613 for (i = 0; i < esignlen; i++)
5617 for (i = zeros; i; i--)
5621 memcpy(p, eptr, elen);
5625 memset(p, ' ', gap);
5629 SvCUR(sv) = p - SvPVX(sv);
5640 do_report_used(pTHXo_ SV *sv)
5642 if (SvTYPE(sv) != SVTYPEMASK) {
5643 /* XXX Perhaps this ought to go to Perl_debug_log, if DEBUGGING. */
5644 PerlIO_printf(PerlIO_stderr(), "****\n");
5650 do_clean_objs(pTHXo_ SV *sv)
5654 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
5655 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv));)
5661 /* XXX Might want to check arrays, etc. */
5664 #ifndef DISABLE_DESTRUCTOR_KLUDGE
5666 do_clean_named_objs(pTHXo_ SV *sv)
5668 if (SvTYPE(sv) == SVt_PVGV) {
5669 if ( SvOBJECT(GvSV(sv)) ||
5670 GvAV(sv) && SvOBJECT(GvAV(sv)) ||
5671 GvHV(sv) && SvOBJECT(GvHV(sv)) ||
5672 GvIO(sv) && SvOBJECT(GvIO(sv)) ||
5673 GvCV(sv) && SvOBJECT(GvCV(sv)) )
5675 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv));)
5683 do_clean_all(pTHXo_ SV *sv)
5685 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%lx\n", sv) );)
5686 SvFLAGS(sv) |= SVf_BREAK;