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);
479 #define new_XIV() (void*)safemalloc(sizeof(XPVIV))
480 #define del_XIV(p) Safefree((char*)p)
482 #define new_XIV() (void*)new_xiv()
483 #define del_XIV(p) del_xiv((XPVIV*) p)
487 #define new_XNV() (void*)safemalloc(sizeof(XPVNV))
488 #define del_XNV(p) Safefree((char*)p)
490 #define new_XNV() (void*)new_xnv()
491 #define del_XNV(p) del_xnv((XPVNV*) p)
495 #define new_XRV() (void*)safemalloc(sizeof(XRV))
496 #define del_XRV(p) Safefree((char*)p)
498 #define new_XRV() (void*)new_xrv()
499 #define del_XRV(p) del_xrv((XRV*) p)
503 #define new_XPV() (void*)safemalloc(sizeof(XPV))
504 #define del_XPV(p) Safefree((char*)p)
506 #define new_XPV() (void*)new_xpv()
507 #define del_XPV(p) del_xpv((XPV *)p)
511 # define my_safemalloc(s) safemalloc(s)
512 # define my_safefree(s) safefree(s)
515 S_my_safemalloc(MEM_SIZE size)
518 New(717, p, size, char);
521 # define my_safefree(s) Safefree(s)
524 #define new_XPVIV() (void*)my_safemalloc(sizeof(XPVIV))
525 #define del_XPVIV(p) my_safefree((char*)p)
527 #define new_XPVNV() (void*)my_safemalloc(sizeof(XPVNV))
528 #define del_XPVNV(p) my_safefree((char*)p)
530 #define new_XPVMG() (void*)my_safemalloc(sizeof(XPVMG))
531 #define del_XPVMG(p) my_safefree((char*)p)
533 #define new_XPVLV() (void*)my_safemalloc(sizeof(XPVLV))
534 #define del_XPVLV(p) my_safefree((char*)p)
536 #define new_XPVAV() (void*)my_safemalloc(sizeof(XPVAV))
537 #define del_XPVAV(p) my_safefree((char*)p)
539 #define new_XPVHV() (void*)my_safemalloc(sizeof(XPVHV))
540 #define del_XPVHV(p) my_safefree((char*)p)
542 #define new_XPVCV() (void*)my_safemalloc(sizeof(XPVCV))
543 #define del_XPVCV(p) my_safefree((char*)p)
545 #define new_XPVGV() (void*)my_safemalloc(sizeof(XPVGV))
546 #define del_XPVGV(p) my_safefree((char*)p)
548 #define new_XPVBM() (void*)my_safemalloc(sizeof(XPVBM))
549 #define del_XPVBM(p) my_safefree((char*)p)
551 #define new_XPVFM() (void*)my_safemalloc(sizeof(XPVFM))
552 #define del_XPVFM(p) my_safefree((char*)p)
554 #define new_XPVIO() (void*)my_safemalloc(sizeof(XPVIO))
555 #define del_XPVIO(p) my_safefree((char*)p)
558 Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
568 if (SvTYPE(sv) == mt)
574 switch (SvTYPE(sv)) {
595 else if (mt < SVt_PVIV)
612 pv = (char*)SvRV(sv);
615 iv = (IV)PTR_CAST pv;
632 else if (mt == SVt_NV)
643 del_XPVIV(SvANY(sv));
653 del_XPVNV(SvANY(sv));
663 del_XPVMG(SvANY(sv));
666 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
671 Perl_croak(aTHX_ "Can't upgrade to undef");
673 SvANY(sv) = new_XIV();
677 SvANY(sv) = new_XNV();
681 SvANY(sv) = new_XRV();
685 SvANY(sv) = new_XPV();
691 SvANY(sv) = new_XPVIV();
701 SvANY(sv) = new_XPVNV();
709 SvANY(sv) = new_XPVMG();
719 SvANY(sv) = new_XPVLV();
733 SvANY(sv) = new_XPVAV();
748 SvANY(sv) = new_XPVHV();
764 SvANY(sv) = new_XPVCV();
765 Zero(SvANY(sv), 1, XPVCV);
775 SvANY(sv) = new_XPVGV();
790 SvANY(sv) = new_XPVBM();
803 SvANY(sv) = new_XPVFM();
804 Zero(SvANY(sv), 1, XPVFM);
814 SvANY(sv) = new_XPVIO();
815 Zero(SvANY(sv), 1, XPVIO);
826 SvFLAGS(sv) &= ~SVTYPEMASK;
832 Perl_sv_backoff(pTHX_ register SV *sv)
837 SvLEN(sv) += SvIVX(sv);
838 SvPVX(sv) -= SvIVX(sv);
840 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
842 SvFLAGS(sv) &= ~SVf_OOK;
847 Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
852 if (newlen >= 0x10000) {
853 PerlIO_printf(Perl_debug_log, "Allocation too large: %lx\n", newlen);
856 #endif /* HAS_64K_LIMIT */
859 if (SvTYPE(sv) < SVt_PV) {
860 sv_upgrade(sv, SVt_PV);
863 else if (SvOOK(sv)) { /* pv is offset? */
866 if (newlen > SvLEN(sv))
867 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
869 if (newlen >= 0x10000)
875 if (newlen > SvLEN(sv)) { /* need more room? */
876 if (SvLEN(sv) && s) {
877 #if defined(MYMALLOC) && !defined(PURIFY) && !defined(LEAKTEST)
878 STRLEN l = malloced_size((void*)SvPVX(sv));
884 Renew(s,newlen,char);
887 New(703,s,newlen,char);
889 SvLEN_set(sv, newlen);
895 Perl_sv_setiv(pTHX_ register SV *sv, IV i)
897 SV_CHECK_THINKFIRST(sv);
898 switch (SvTYPE(sv)) {
900 sv_upgrade(sv, SVt_IV);
903 sv_upgrade(sv, SVt_PVNV);
907 sv_upgrade(sv, SVt_PVIV);
918 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
919 PL_op_desc[PL_op->op_type]);
922 (void)SvIOK_only(sv); /* validate number */
928 Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
935 Perl_sv_setuv(pTHX_ register SV *sv, UV u)
943 Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
950 Perl_sv_setnv(pTHX_ register SV *sv, NV num)
952 SV_CHECK_THINKFIRST(sv);
953 switch (SvTYPE(sv)) {
956 sv_upgrade(sv, SVt_NV);
961 sv_upgrade(sv, SVt_PVNV);
972 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
973 PL_op_name[PL_op->op_type]);
977 (void)SvNOK_only(sv); /* validate number */
982 Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
989 S_not_a_number(pTHX_ SV *sv)
995 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
996 /* each *s can expand to 4 chars + "...\0",
997 i.e. need room for 8 chars */
999 for (s = SvPVX(sv); *s && d < limit; s++) {
1001 if (ch & 128 && !isPRINT_LC(ch)) {
1010 else if (ch == '\r') {
1014 else if (ch == '\f') {
1018 else if (ch == '\\') {
1022 else if (isPRINT_LC(ch))
1037 Perl_warner(aTHX_ WARN_NUMERIC, "Argument \"%s\" isn't numeric in %s", tmpbuf,
1038 PL_op_name[PL_op->op_type]);
1040 Perl_warner(aTHX_ WARN_NUMERIC, "Argument \"%s\" isn't numeric", tmpbuf);
1043 /* the number can be converted to integer with atol() or atoll() */
1044 #define IS_NUMBER_TO_INT_BY_ATOL 0x01
1045 #define IS_NUMBER_TO_INT_BY_ATOF 0x02 /* atol() may be != atof() */
1046 #define IS_NUMBER_NOT_IV 0x04 /* (IV)atof() may be != atof() */
1047 #define IS_NUMBER_NEG 0x08 /* not good to cache UV */
1049 /* Actually, ISO C leaves conversion of UV to IV undefined, but
1050 until proven guilty, assume that things are not that bad... */
1053 Perl_sv_2iv(pTHX_ register SV *sv)
1057 if (SvGMAGICAL(sv)) {
1062 return I_V(SvNVX(sv));
1064 if (SvPOKp(sv) && SvLEN(sv))
1067 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1069 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1070 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1075 if (SvTHINKFIRST(sv)) {
1078 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv, numer)))
1079 return SvIV(tmpstr);
1080 return (IV)PTR_CAST SvRV(sv);
1082 if (SvREADONLY(sv) && !SvOK(sv)) {
1084 if (ckWARN(WARN_UNINITIALIZED))
1085 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1091 return (IV)(SvUVX(sv));
1098 /* We can cache the IV/UV value even if it not good enough
1099 * to reconstruct NV, since the conversion to PV will prefer
1103 if (SvTYPE(sv) == SVt_NV)
1104 sv_upgrade(sv, SVt_PVNV);
1107 if (SvNVX(sv) < (NV)IV_MAX + 0.5)
1108 SvIVX(sv) = I_V(SvNVX(sv));
1110 SvUVX(sv) = U_V(SvNVX(sv));
1114 DEBUG_c(PerlIO_printf(Perl_debug_log,
1115 "0x%" PERL_PRIx64 " 2iv(%" PERL_PRIu64 " => %" PERL_PRId64 ") (as unsigned)\n",
1117 (UV)SvUVX(sv), (IV)SvUVX(sv)));
1119 DEBUG_c(PerlIO_printf(Perl_debug_log,
1120 "0x%lx 2iv(%lu => %ld) (as unsigned)\n",
1122 (unsigned long)SvUVX(sv), (long)(IV)SvUVX(sv)));
1124 return (IV)SvUVX(sv);
1127 else if (SvPOKp(sv) && SvLEN(sv)) {
1128 I32 numtype = looks_like_number(sv);
1130 /* We want to avoid a possible problem when we cache an IV which
1131 may be later translated to an NV, and the resulting NV is not
1132 the translation of the initial data.
1134 This means that if we cache such an IV, we need to cache the
1135 NV as well. Moreover, we trade speed for space, and do not
1136 cache the NV if not needed.
1138 if (numtype & IS_NUMBER_NOT_IV) {
1139 /* May be not an integer. Need to cache NV if we cache IV
1140 * - otherwise future conversion to NV will be wrong. */
1143 d = Atof(SvPVX(sv));
1145 if (SvTYPE(sv) < SVt_PVNV)
1146 sv_upgrade(sv, SVt_PVNV);
1150 #if defined(USE_LONG_DOUBLE)
1151 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2nv(%" PERL_PRIgldbl ")\n",
1152 (unsigned long)sv, SvNVX(sv)));
1154 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2nv(%g)\n",
1155 (unsigned long)sv, SvNVX(sv)));
1157 if (SvNVX(sv) < (NV)IV_MAX + 0.5)
1158 SvIVX(sv) = I_V(SvNVX(sv));
1160 SvUVX(sv) = U_V(SvNVX(sv));
1166 /* The NV may be reconstructed from IV - safe to cache IV,
1167 which may be calculated by atol(). */
1168 if (SvTYPE(sv) == SVt_PV)
1169 sv_upgrade(sv, SVt_PVIV);
1171 SvIVX(sv) = Atol(SvPVX(sv));
1173 else { /* Not a number. Cache 0. */
1176 if (SvTYPE(sv) < SVt_PVIV)
1177 sv_upgrade(sv, SVt_PVIV);
1180 if (ckWARN(WARN_NUMERIC))
1186 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1187 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1188 if (SvTYPE(sv) < SVt_IV)
1189 /* Typically the caller expects that sv_any is not NULL now. */
1190 sv_upgrade(sv, SVt_IV);
1193 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2iv(%ld)\n",
1194 (unsigned long)sv,(long)SvIVX(sv)));
1195 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
1199 Perl_sv_2uv(pTHX_ register SV *sv)
1203 if (SvGMAGICAL(sv)) {
1208 return U_V(SvNVX(sv));
1209 if (SvPOKp(sv) && SvLEN(sv))
1212 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1214 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1215 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1220 if (SvTHINKFIRST(sv)) {
1223 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv, numer)))
1224 return SvUV(tmpstr);
1225 return (UV)PTR_CAST SvRV(sv);
1227 if (SvREADONLY(sv) && !SvOK(sv)) {
1229 if (ckWARN(WARN_UNINITIALIZED))
1230 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1239 return (UV)SvIVX(sv);
1243 /* We can cache the IV/UV value even if it not good enough
1244 * to reconstruct NV, since the conversion to PV will prefer
1247 if (SvTYPE(sv) == SVt_NV)
1248 sv_upgrade(sv, SVt_PVNV);
1250 if (SvNVX(sv) >= -0.5) {
1252 SvUVX(sv) = U_V(SvNVX(sv));
1255 SvIVX(sv) = I_V(SvNVX(sv));
1258 DEBUG_c(PerlIO_printf(Perl_debug_log,
1259 "0x%" PERL_PRIx64 " 2uv(%" PERL_PRId64 " => %" PERL_PRIu64 ") (as signed)\n",
1260 (unsigned long)sv,(long)SvIVX(sv),
1261 (long)(UV)SvIVX(sv)));
1263 DEBUG_c(PerlIO_printf(Perl_debug_log,
1264 "0x%lx 2uv(%ld => %lu) (as signed)\n",
1265 (unsigned long)sv,(long)SvIVX(sv),
1266 (long)(UV)SvIVX(sv)));
1268 return (UV)SvIVX(sv);
1271 else if (SvPOKp(sv) && SvLEN(sv)) {
1272 I32 numtype = looks_like_number(sv);
1274 /* We want to avoid a possible problem when we cache a UV which
1275 may be later translated to an NV, and the resulting NV is not
1276 the translation of the initial data.
1278 This means that if we cache such a UV, we need to cache the
1279 NV as well. Moreover, we trade speed for space, and do not
1280 cache the NV if not needed.
1282 if (numtype & IS_NUMBER_NOT_IV) {
1283 /* May be not an integer. Need to cache NV if we cache IV
1284 * - otherwise future conversion to NV will be wrong. */
1287 d = Atof(SvPVX(sv));
1289 if (SvTYPE(sv) < SVt_PVNV)
1290 sv_upgrade(sv, SVt_PVNV);
1294 #if defined(USE_LONG_DOUBLE)
1295 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2nv(%" PERL_PRIgldbl ")\n",
1296 (unsigned long)sv, SvNVX(sv)));
1298 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2nv(%g)\n",
1299 (unsigned long)sv, SvNVX(sv)));
1301 if (SvNVX(sv) < -0.5) {
1302 SvIVX(sv) = I_V(SvNVX(sv));
1305 SvUVX(sv) = U_V(SvNVX(sv));
1309 else if (numtype & IS_NUMBER_NEG) {
1310 /* The NV may be reconstructed from IV - safe to cache IV,
1311 which may be calculated by atol(). */
1312 if (SvTYPE(sv) == SVt_PV)
1313 sv_upgrade(sv, SVt_PVIV);
1315 SvIVX(sv) = (IV)Atol(SvPVX(sv));
1317 else if (numtype) { /* Non-negative */
1318 /* The NV may be reconstructed from UV - safe to cache UV,
1319 which may be calculated by strtoul()/atol. */
1320 if (SvTYPE(sv) == SVt_PV)
1321 sv_upgrade(sv, SVt_PVIV);
1323 (void)SvIsUV_on(sv);
1325 SvUVX(sv) = Strtoul(SvPVX(sv), Null(char**), 10);
1326 #else /* no atou(), but we know the number fits into IV... */
1327 /* The only problem may be if it is negative... */
1328 SvUVX(sv) = (UV)Atol(SvPVX(sv));
1331 else { /* Not a number. Cache 0. */
1334 if (SvTYPE(sv) < SVt_PVIV)
1335 sv_upgrade(sv, SVt_PVIV);
1336 SvUVX(sv) = 0; /* We assume that 0s have the
1337 same bitmap in IV and UV. */
1339 (void)SvIsUV_on(sv);
1340 if (ckWARN(WARN_NUMERIC))
1345 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1347 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1348 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1350 if (SvTYPE(sv) < SVt_IV)
1351 /* Typically the caller expects that sv_any is not NULL now. */
1352 sv_upgrade(sv, SVt_IV);
1356 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2uv(%lu)\n",
1357 (unsigned long)sv,SvUVX(sv)));
1358 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
1362 Perl_sv_2nv(pTHX_ register SV *sv)
1366 if (SvGMAGICAL(sv)) {
1370 if (SvPOKp(sv) && SvLEN(sv)) {
1372 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv))
1374 return Atof(SvPVX(sv));
1378 return (NV)SvUVX(sv);
1380 return (NV)SvIVX(sv);
1383 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1385 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1386 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1391 if (SvTHINKFIRST(sv)) {
1394 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)))
1395 return SvNV(tmpstr);
1396 return (NV)(PTRV)SvRV(sv);
1398 if (SvREADONLY(sv) && !SvOK(sv)) {
1400 if (ckWARN(WARN_UNINITIALIZED))
1401 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1405 if (SvTYPE(sv) < SVt_NV) {
1406 if (SvTYPE(sv) == SVt_IV)
1407 sv_upgrade(sv, SVt_PVNV);
1409 sv_upgrade(sv, SVt_NV);
1410 #if defined(USE_LONG_DOUBLE)
1412 RESTORE_NUMERIC_STANDARD();
1413 PerlIO_printf(Perl_debug_log, "0x%lx num(%" PERL_PRIgldbl ")\n",
1414 (unsigned long)sv, SvNVX(sv));
1415 RESTORE_NUMERIC_LOCAL();
1419 RESTORE_NUMERIC_STANDARD();
1420 PerlIO_printf(Perl_debug_log, "0x%lx num(%g)\n",
1421 (unsigned long)sv, SvNVX(sv));
1422 RESTORE_NUMERIC_LOCAL();
1426 else if (SvTYPE(sv) < SVt_PVNV)
1427 sv_upgrade(sv, SVt_PVNV);
1429 (!SvPOKp(sv) || !strchr(SvPVX(sv),'.') || !looks_like_number(sv)))
1431 SvNVX(sv) = SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv);
1433 else if (SvPOKp(sv) && SvLEN(sv)) {
1435 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv))
1437 SvNVX(sv) = Atof(SvPVX(sv));
1441 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1442 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1443 if (SvTYPE(sv) < SVt_NV)
1444 /* Typically the caller expects that sv_any is not NULL now. */
1445 sv_upgrade(sv, SVt_NV);
1449 #if defined(USE_LONG_DOUBLE)
1451 RESTORE_NUMERIC_STANDARD();
1452 PerlIO_printf(Perl_debug_log, "0x%lx 2nv(%" PERL_PRIgldbl ")\n",
1453 (unsigned long)sv, SvNVX(sv));
1454 RESTORE_NUMERIC_LOCAL();
1458 RESTORE_NUMERIC_STANDARD();
1459 PerlIO_printf(Perl_debug_log, "0x%lx 1nv(%g)\n",
1460 (unsigned long)sv, SvNVX(sv));
1461 RESTORE_NUMERIC_LOCAL();
1468 S_asIV(pTHX_ SV *sv)
1470 I32 numtype = looks_like_number(sv);
1473 if (numtype & IS_NUMBER_TO_INT_BY_ATOL)
1474 return Atol(SvPVX(sv));
1477 if (ckWARN(WARN_NUMERIC))
1480 d = Atof(SvPVX(sv));
1485 S_asUV(pTHX_ SV *sv)
1487 I32 numtype = looks_like_number(sv);
1490 if (numtype & IS_NUMBER_TO_INT_BY_ATOL)
1491 return Strtoul(SvPVX(sv), Null(char**), 10);
1495 if (ckWARN(WARN_NUMERIC))
1498 return U_V(Atof(SvPVX(sv)));
1502 * Returns a combination of (advisory only - can get false negatives)
1503 * IS_NUMBER_TO_INT_BY_ATOL, IS_NUMBER_TO_INT_BY_ATOF, IS_NUMBER_NOT_IV,
1505 * 0 if does not look like number.
1507 * In fact possible values are 0 and
1508 * IS_NUMBER_TO_INT_BY_ATOL 123
1509 * IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_NOT_IV 123.1
1510 * IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_IV 123e0
1511 * with a possible addition of IS_NUMBER_NEG.
1515 Perl_looks_like_number(pTHX_ SV *sv)
1518 register char *send;
1519 register char *sbegin;
1520 register char *nbegin;
1528 else if (SvPOKp(sv))
1529 sbegin = SvPV(sv, len);
1532 send = sbegin + len;
1539 numtype = IS_NUMBER_NEG;
1546 * we return IS_NUMBER_TO_INT_BY_ATOL if the number can be converted
1547 * to _integer_ with atol() and IS_NUMBER_TO_INT_BY_ATOF if you need
1551 /* next must be digit or the radix separator */
1555 } while (isDIGIT(*s));
1557 if (s - nbegin >= TYPE_DIGITS(IV)) /* Cannot cache ato[ul]() */
1558 numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_IV;
1560 numtype |= IS_NUMBER_TO_INT_BY_ATOL;
1563 #ifdef USE_LOCALE_NUMERIC
1564 || IS_NUMERIC_RADIX(*s)
1568 numtype |= IS_NUMBER_NOT_IV;
1569 while (isDIGIT(*s)) /* optional digits after the radix */
1574 #ifdef USE_LOCALE_NUMERIC
1575 || IS_NUMERIC_RADIX(*s)
1579 numtype |= IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_NOT_IV;
1580 /* no digits before the radix means we need digits after it */
1584 } while (isDIGIT(*s));
1592 /* we can have an optional exponent part */
1593 if (*s == 'e' || *s == 'E') {
1594 numtype &= ~IS_NUMBER_NEG;
1595 numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_IV;
1597 if (*s == '+' || *s == '-')
1602 } while (isDIGIT(*s));
1611 if (len == 10 && memEQ(sbegin, "0 but true", 10))
1612 return IS_NUMBER_TO_INT_BY_ATOL;
1617 Perl_sv_2pv_nolen(pTHX_ register SV *sv)
1620 return sv_2pv(sv, &n_a);
1623 /* We assume that buf is at least TYPE_CHARS(UV) long. */
1625 uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
1628 char *ptr = buf + TYPE_CHARS(UV);
1643 *--ptr = '0' + (uv % 10);
1652 Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
1657 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
1658 char *tmpbuf = tbuf;
1664 if (SvGMAGICAL(sv)) {
1673 (void)sprintf(tmpbuf,"%" PERL_PRIu64,(UV)SvUVX(sv));
1675 (void)sprintf(tmpbuf,"%" PERL_PRId64,(IV)SvIVX(sv));
1678 (void)sprintf(tmpbuf,"%lu",(unsigned long)SvUVX(sv));
1680 (void)sprintf(tmpbuf,"%ld",(long)SvIVX(sv));
1686 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
1691 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1693 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1694 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1700 if (SvTHINKFIRST(sv)) {
1703 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)))
1704 return SvPV(tmpstr,*lp);
1711 switch (SvTYPE(sv)) {
1713 if ( ((SvFLAGS(sv) &
1714 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
1715 == (SVs_OBJECT|SVs_RMG))
1716 && strEQ(s=HvNAME(SvSTASH(sv)), "Regexp")
1717 && (mg = mg_find(sv, 'r'))) {
1719 regexp *re = (regexp *)mg->mg_obj;
1722 char *fptr = "msix";
1727 U16 reganch = (re->reganch & PMf_COMPILETIME) >> 12;
1729 while(ch = *fptr++) {
1731 reflags[left++] = ch;
1734 reflags[right--] = ch;
1739 reflags[left] = '-';
1743 mg->mg_len = re->prelen + 4 + left;
1744 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
1745 Copy("(?", mg->mg_ptr, 2, char);
1746 Copy(reflags, mg->mg_ptr+2, left, char);
1747 Copy(":", mg->mg_ptr+left+2, 1, char);
1748 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
1749 mg->mg_ptr[mg->mg_len - 1] = ')';
1750 mg->mg_ptr[mg->mg_len] = 0;
1752 PL_reginterp_cnt += re->program[0].next_off;
1764 case SVt_PVBM: s = "SCALAR"; break;
1765 case SVt_PVLV: s = "LVALUE"; break;
1766 case SVt_PVAV: s = "ARRAY"; break;
1767 case SVt_PVHV: s = "HASH"; break;
1768 case SVt_PVCV: s = "CODE"; break;
1769 case SVt_PVGV: s = "GLOB"; break;
1770 case SVt_PVFM: s = "FORMAT"; break;
1771 case SVt_PVIO: s = "IO"; break;
1772 default: s = "UNKNOWN"; break;
1776 Perl_sv_setpvf(aTHX_ tsv, "%s=%s", HvNAME(SvSTASH(sv)), s);
1780 Perl_sv_catpvf(aTHX_ tsv, "(0x%" PERL_PRIx64")", (UV)PTR_CAST sv);
1782 Perl_sv_catpvf(aTHX_ tsv, "(0x%lx)", (unsigned long)sv);
1789 if (SvREADONLY(sv) && !SvOK(sv)) {
1791 if (ckWARN(WARN_UNINITIALIZED))
1792 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1797 if (SvNOKp(sv)) { /* See note in sv_2uv() */
1798 /* XXXX 64-bit? IV may have better precision... */
1799 /* I tried changing this for to be 64-bit-aware and
1800 * the t/op/numconvert.t became very, very, angry.
1802 if (SvTYPE(sv) < SVt_PVNV)
1803 sv_upgrade(sv, SVt_PVNV);
1806 olderrno = errno; /* some Xenix systems wipe out errno here */
1808 if (SvNVX(sv) == 0.0)
1809 (void)strcpy(s,"0");
1813 Gconvert(SvNVX(sv), NV_DIG, 0, s);
1816 #ifdef FIXNEGATIVEZERO
1817 if (*s == '-' && s[1] == '0' && !s[2])
1826 else if (SvIOKp(sv)) {
1827 U32 isIOK = SvIOK(sv);
1828 U32 isUIOK = SvIsUV(sv);
1829 char buf[TYPE_CHARS(UV)];
1832 if (SvTYPE(sv) < SVt_PVIV)
1833 sv_upgrade(sv, SVt_PVIV);
1835 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
1837 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
1838 SvGROW(sv, ebuf - ptr + 1); /* inlined from sv_setpvn */
1839 Move(ptr,SvPVX(sv),ebuf - ptr,char);
1840 SvCUR_set(sv, ebuf - ptr);
1853 if (ckWARN(WARN_UNINITIALIZED)
1854 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1856 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
1859 if (SvTYPE(sv) < SVt_PV)
1860 /* Typically the caller expects that sv_any is not NULL now. */
1861 sv_upgrade(sv, SVt_PV);
1864 *lp = s - SvPVX(sv);
1867 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2pv(%s)\n",
1868 (unsigned long)sv,SvPVX(sv)));
1872 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
1873 /* Sneaky stuff here */
1877 tsv = newSVpv(tmpbuf, 0);
1893 len = strlen(tmpbuf);
1895 #ifdef FIXNEGATIVEZERO
1896 if (len == 2 && t[0] == '-' && t[1] == '0') {
1901 (void)SvUPGRADE(sv, SVt_PV);
1903 s = SvGROW(sv, len + 1);
1911 /* This function is only called on magical items */
1913 Perl_sv_2bool(pTHX_ register SV *sv)
1923 if (SvAMAGIC(sv) && (tmpsv = AMG_CALLun(sv,bool_)))
1924 return SvTRUE(tmpsv);
1925 return SvRV(sv) != 0;
1928 register XPV* Xpvtmp;
1929 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
1930 (*Xpvtmp->xpv_pv > '0' ||
1931 Xpvtmp->xpv_cur > 1 ||
1932 (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0')))
1939 return SvIVX(sv) != 0;
1942 return SvNVX(sv) != 0.0;
1949 /* Note: sv_setsv() should not be called with a source string that needs
1950 * to be reused, since it may destroy the source string if it is marked
1955 Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
1958 register U32 sflags;
1964 SV_CHECK_THINKFIRST(dstr);
1966 sstr = &PL_sv_undef;
1967 stype = SvTYPE(sstr);
1968 dtype = SvTYPE(dstr);
1972 /* There's a lot of redundancy below but we're going for speed here */
1977 if (dtype != SVt_PVGV) {
1978 (void)SvOK_off(dstr);
1986 sv_upgrade(dstr, SVt_IV);
1989 sv_upgrade(dstr, SVt_PVNV);
1993 sv_upgrade(dstr, SVt_PVIV);
1996 (void)SvIOK_only(dstr);
1997 SvIVX(dstr) = SvIVX(sstr);
2010 sv_upgrade(dstr, SVt_NV);
2015 sv_upgrade(dstr, SVt_PVNV);
2018 SvNVX(dstr) = SvNVX(sstr);
2019 (void)SvNOK_only(dstr);
2027 sv_upgrade(dstr, SVt_RV);
2028 else if (dtype == SVt_PVGV &&
2029 SvTYPE(SvRV(sstr)) == SVt_PVGV) {
2032 if (PL_curcop->cop_stash != GvSTASH(dstr))
2033 GvIMPORTED_on(dstr);
2043 sv_upgrade(dstr, SVt_PV);
2046 if (dtype < SVt_PVIV)
2047 sv_upgrade(dstr, SVt_PVIV);
2050 if (dtype < SVt_PVNV)
2051 sv_upgrade(dstr, SVt_PVNV);
2058 Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0),
2059 PL_op_name[PL_op->op_type]);
2061 Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0));
2065 if (dtype <= SVt_PVGV) {
2067 if (dtype != SVt_PVGV) {
2068 char *name = GvNAME(sstr);
2069 STRLEN len = GvNAMELEN(sstr);
2070 sv_upgrade(dstr, SVt_PVGV);
2071 sv_magic(dstr, dstr, '*', name, len);
2072 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
2073 GvNAME(dstr) = savepvn(name, len);
2074 GvNAMELEN(dstr) = len;
2075 SvFAKE_on(dstr); /* can coerce to non-glob */
2077 /* ahem, death to those who redefine active sort subs */
2078 else if (PL_curstackinfo->si_type == PERLSI_SORT
2079 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
2080 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
2082 (void)SvOK_off(dstr);
2083 GvINTRO_off(dstr); /* one-shot flag */
2085 GvGP(dstr) = gp_ref(GvGP(sstr));
2087 if (PL_curcop->cop_stash != GvSTASH(dstr))
2088 GvIMPORTED_on(dstr);
2095 if (SvGMAGICAL(sstr)) {
2097 if (SvTYPE(sstr) != stype) {
2098 stype = SvTYPE(sstr);
2099 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
2103 if (stype == SVt_PVLV)
2104 (void)SvUPGRADE(dstr, SVt_PVNV);
2106 (void)SvUPGRADE(dstr, stype);
2109 sflags = SvFLAGS(sstr);
2111 if (sflags & SVf_ROK) {
2112 if (dtype >= SVt_PV) {
2113 if (dtype == SVt_PVGV) {
2114 SV *sref = SvREFCNT_inc(SvRV(sstr));
2116 int intro = GvINTRO(dstr);
2120 GvGP(dstr)->gp_refcnt--;
2121 GvINTRO_off(dstr); /* one-shot flag */
2122 Newz(602,gp, 1, GP);
2123 GvGP(dstr) = gp_ref(gp);
2124 GvSV(dstr) = NEWSV(72,0);
2125 GvLINE(dstr) = PL_curcop->cop_line;
2126 GvEGV(dstr) = (GV*)dstr;
2129 switch (SvTYPE(sref)) {
2132 SAVESPTR(GvAV(dstr));
2134 dref = (SV*)GvAV(dstr);
2135 GvAV(dstr) = (AV*)sref;
2136 if (PL_curcop->cop_stash != GvSTASH(dstr))
2137 GvIMPORTED_AV_on(dstr);
2141 SAVESPTR(GvHV(dstr));
2143 dref = (SV*)GvHV(dstr);
2144 GvHV(dstr) = (HV*)sref;
2145 if (PL_curcop->cop_stash != GvSTASH(dstr))
2146 GvIMPORTED_HV_on(dstr);
2150 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
2151 SvREFCNT_dec(GvCV(dstr));
2152 GvCV(dstr) = Nullcv;
2153 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
2154 PL_sub_generation++;
2156 SAVESPTR(GvCV(dstr));
2159 dref = (SV*)GvCV(dstr);
2160 if (GvCV(dstr) != (CV*)sref) {
2161 CV* cv = GvCV(dstr);
2163 if (!GvCVGEN((GV*)dstr) &&
2164 (CvROOT(cv) || CvXSUB(cv)))
2166 SV *const_sv = cv_const_sv(cv);
2167 bool const_changed = TRUE;
2169 const_changed = sv_cmp(const_sv,
2170 op_const_sv(CvSTART((CV*)sref),
2172 /* ahem, death to those who redefine
2173 * active sort subs */
2174 if (PL_curstackinfo->si_type == PERLSI_SORT &&
2175 PL_sortcop == CvSTART(cv))
2177 "Can't redefine active sort subroutine %s",
2178 GvENAME((GV*)dstr));
2179 if (ckWARN(WARN_REDEFINE) || (const_changed && const_sv)) {
2180 if (!(CvGV(cv) && GvSTASH(CvGV(cv))
2181 && HvNAME(GvSTASH(CvGV(cv)))
2182 && strEQ(HvNAME(GvSTASH(CvGV(cv))),
2184 Perl_warner(aTHX_ WARN_REDEFINE, const_sv ?
2185 "Constant subroutine %s redefined"
2186 : "Subroutine %s redefined",
2187 GvENAME((GV*)dstr));
2190 cv_ckproto(cv, (GV*)dstr,
2191 SvPOK(sref) ? SvPVX(sref) : Nullch);
2193 GvCV(dstr) = (CV*)sref;
2194 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
2195 GvASSUMECV_on(dstr);
2196 PL_sub_generation++;
2198 if (PL_curcop->cop_stash != GvSTASH(dstr))
2199 GvIMPORTED_CV_on(dstr);
2203 SAVESPTR(GvIOp(dstr));
2205 dref = (SV*)GvIOp(dstr);
2206 GvIOp(dstr) = (IO*)sref;
2210 SAVESPTR(GvSV(dstr));
2212 dref = (SV*)GvSV(dstr);
2214 if (PL_curcop->cop_stash != GvSTASH(dstr))
2215 GvIMPORTED_SV_on(dstr);
2226 (void)SvOOK_off(dstr); /* backoff */
2228 Safefree(SvPVX(dstr));
2229 SvLEN(dstr)=SvCUR(dstr)=0;
2232 (void)SvOK_off(dstr);
2233 SvRV(dstr) = SvREFCNT_inc(SvRV(sstr));
2235 if (sflags & SVp_NOK) {
2237 SvNVX(dstr) = SvNVX(sstr);
2239 if (sflags & SVp_IOK) {
2240 (void)SvIOK_on(dstr);
2241 SvIVX(dstr) = SvIVX(sstr);
2245 if (SvAMAGIC(sstr)) {
2249 else if (sflags & SVp_POK) {
2252 * Check to see if we can just swipe the string. If so, it's a
2253 * possible small lose on short strings, but a big win on long ones.
2254 * It might even be a win on short strings if SvPVX(dstr)
2255 * has to be allocated and SvPVX(sstr) has to be freed.
2258 if (SvTEMP(sstr) && /* slated for free anyway? */
2259 SvREFCNT(sstr) == 1 && /* and no other references to it? */
2260 !(sflags & SVf_OOK)) /* and not involved in OOK hack? */
2262 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
2264 SvFLAGS(dstr) &= ~SVf_OOK;
2265 Safefree(SvPVX(dstr) - SvIVX(dstr));
2267 else if (SvLEN(dstr))
2268 Safefree(SvPVX(dstr));
2270 (void)SvPOK_only(dstr);
2271 SvPV_set(dstr, SvPVX(sstr));
2272 SvLEN_set(dstr, SvLEN(sstr));
2273 SvCUR_set(dstr, SvCUR(sstr));
2275 (void)SvOK_off(sstr);
2276 SvPV_set(sstr, Nullch);
2281 else { /* have to copy actual string */
2282 STRLEN len = SvCUR(sstr);
2284 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
2285 Move(SvPVX(sstr),SvPVX(dstr),len,char);
2286 SvCUR_set(dstr, len);
2287 *SvEND(dstr) = '\0';
2288 (void)SvPOK_only(dstr);
2291 if (sflags & SVp_NOK) {
2293 SvNVX(dstr) = SvNVX(sstr);
2295 if (sflags & SVp_IOK) {
2296 (void)SvIOK_on(dstr);
2297 SvIVX(dstr) = SvIVX(sstr);
2302 else if (sflags & SVp_NOK) {
2303 SvNVX(dstr) = SvNVX(sstr);
2304 (void)SvNOK_only(dstr);
2306 (void)SvIOK_on(dstr);
2307 SvIVX(dstr) = SvIVX(sstr);
2308 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
2313 else if (sflags & SVp_IOK) {
2314 (void)SvIOK_only(dstr);
2315 SvIVX(dstr) = SvIVX(sstr);
2320 if (dtype == SVt_PVGV) {
2321 if (ckWARN(WARN_UNSAFE))
2322 Perl_warner(aTHX_ WARN_UNSAFE, "Undefined value assigned to typeglob");
2325 (void)SvOK_off(dstr);
2331 Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
2333 sv_setsv(dstr,sstr);
2338 Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
2340 register char *dptr;
2341 assert(len >= 0); /* STRLEN is probably unsigned, so this may
2342 elicit a warning, but it won't hurt. */
2343 SV_CHECK_THINKFIRST(sv);
2348 (void)SvUPGRADE(sv, SVt_PV);
2350 SvGROW(sv, len + 1);
2352 Move(ptr,dptr,len,char);
2355 (void)SvPOK_only(sv); /* validate pointer */
2360 Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
2362 sv_setpvn(sv,ptr,len);
2367 Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
2369 register STRLEN len;
2371 SV_CHECK_THINKFIRST(sv);
2377 (void)SvUPGRADE(sv, SVt_PV);
2379 SvGROW(sv, len + 1);
2380 Move(ptr,SvPVX(sv),len+1,char);
2382 (void)SvPOK_only(sv); /* validate pointer */
2387 Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
2394 Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
2396 SV_CHECK_THINKFIRST(sv);
2397 (void)SvUPGRADE(sv, SVt_PV);
2402 (void)SvOOK_off(sv);
2403 if (SvPVX(sv) && SvLEN(sv))
2404 Safefree(SvPVX(sv));
2405 Renew(ptr, len+1, char);
2408 SvLEN_set(sv, len+1);
2410 (void)SvPOK_only(sv); /* validate pointer */
2415 Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
2417 sv_usepvn(sv,ptr,len);
2422 Perl_sv_force_normal(pTHX_ register SV *sv)
2424 if (SvREADONLY(sv)) {
2426 if (PL_curcop != &PL_compiling)
2427 Perl_croak(aTHX_ PL_no_modify);
2431 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
2436 Perl_sv_chop(pTHX_ register SV *sv, register char *ptr) /* like set but assuming ptr is in sv */
2440 register STRLEN delta;
2442 if (!ptr || !SvPOKp(sv))
2444 SV_CHECK_THINKFIRST(sv);
2445 if (SvTYPE(sv) < SVt_PVIV)
2446 sv_upgrade(sv,SVt_PVIV);
2449 if (!SvLEN(sv)) { /* make copy of shared string */
2450 char *pvx = SvPVX(sv);
2451 STRLEN len = SvCUR(sv);
2452 SvGROW(sv, len + 1);
2453 Move(pvx,SvPVX(sv),len,char);
2457 SvFLAGS(sv) |= SVf_OOK;
2459 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK|SVf_IVisUV);
2460 delta = ptr - SvPVX(sv);
2468 Perl_sv_catpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
2473 junk = SvPV_force(sv, tlen);
2474 SvGROW(sv, tlen + len + 1);
2477 Move(ptr,SvPVX(sv)+tlen,len,char);
2480 (void)SvPOK_only(sv); /* validate pointer */
2485 Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
2487 sv_catpvn(sv,ptr,len);
2492 Perl_sv_catsv(pTHX_ SV *dstr, register SV *sstr)
2498 if (s = SvPV(sstr, len))
2499 sv_catpvn(dstr,s,len);
2503 Perl_sv_catsv_mg(pTHX_ SV *dstr, register SV *sstr)
2505 sv_catsv(dstr,sstr);
2510 Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr)
2512 register STRLEN len;
2518 junk = SvPV_force(sv, tlen);
2520 SvGROW(sv, tlen + len + 1);
2523 Move(ptr,SvPVX(sv)+tlen,len+1,char);
2525 (void)SvPOK_only(sv); /* validate pointer */
2530 Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr)
2537 Perl_newSV(pTHX_ STRLEN len)
2543 sv_upgrade(sv, SVt_PV);
2544 SvGROW(sv, len + 1);
2549 /* name is assumed to contain an SV* if (name && namelen == HEf_SVKEY) */
2552 Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen)
2556 if (SvREADONLY(sv)) {
2558 if (PL_curcop != &PL_compiling && !strchr("gBf", how))
2559 Perl_croak(aTHX_ PL_no_modify);
2561 if (SvMAGICAL(sv) || (how == 't' && SvTYPE(sv) >= SVt_PVMG)) {
2562 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
2569 (void)SvUPGRADE(sv, SVt_PVMG);
2571 Newz(702,mg, 1, MAGIC);
2572 mg->mg_moremagic = SvMAGIC(sv);
2575 if (!obj || obj == sv || how == '#' || how == 'r')
2579 mg->mg_obj = SvREFCNT_inc(obj);
2580 mg->mg_flags |= MGf_REFCOUNTED;
2583 mg->mg_len = namlen;
2586 mg->mg_ptr = savepvn(name, namlen);
2587 else if (namlen == HEf_SVKEY)
2588 mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
2592 mg->mg_virtual = &PL_vtbl_sv;
2595 mg->mg_virtual = &PL_vtbl_amagic;
2598 mg->mg_virtual = &PL_vtbl_amagicelem;
2604 mg->mg_virtual = &PL_vtbl_bm;
2607 mg->mg_virtual = &PL_vtbl_regdata;
2610 mg->mg_virtual = &PL_vtbl_regdatum;
2613 mg->mg_virtual = &PL_vtbl_env;
2616 mg->mg_virtual = &PL_vtbl_fm;
2619 mg->mg_virtual = &PL_vtbl_envelem;
2622 mg->mg_virtual = &PL_vtbl_mglob;
2625 mg->mg_virtual = &PL_vtbl_isa;
2628 mg->mg_virtual = &PL_vtbl_isaelem;
2631 mg->mg_virtual = &PL_vtbl_nkeys;
2638 mg->mg_virtual = &PL_vtbl_dbline;
2642 mg->mg_virtual = &PL_vtbl_mutex;
2644 #endif /* USE_THREADS */
2645 #ifdef USE_LOCALE_COLLATE
2647 mg->mg_virtual = &PL_vtbl_collxfrm;
2649 #endif /* USE_LOCALE_COLLATE */
2651 mg->mg_virtual = &PL_vtbl_pack;
2655 mg->mg_virtual = &PL_vtbl_packelem;
2658 mg->mg_virtual = &PL_vtbl_regexp;
2661 mg->mg_virtual = &PL_vtbl_sig;
2664 mg->mg_virtual = &PL_vtbl_sigelem;
2667 mg->mg_virtual = &PL_vtbl_taint;
2671 mg->mg_virtual = &PL_vtbl_uvar;
2674 mg->mg_virtual = &PL_vtbl_vec;
2677 mg->mg_virtual = &PL_vtbl_substr;
2680 mg->mg_virtual = &PL_vtbl_defelem;
2683 mg->mg_virtual = &PL_vtbl_glob;
2686 mg->mg_virtual = &PL_vtbl_arylen;
2689 mg->mg_virtual = &PL_vtbl_pos;
2692 mg->mg_virtual = &PL_vtbl_backref;
2694 case '~': /* Reserved for use by extensions not perl internals. */
2695 /* Useful for attaching extension internal data to perl vars. */
2696 /* Note that multiple extensions may clash if magical scalars */
2697 /* etc holding private data from one are passed to another. */
2701 Perl_croak(aTHX_ "Don't know how to handle magic of type '%c'", how);
2705 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2709 Perl_sv_unmagic(pTHX_ SV *sv, int type)
2713 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
2716 for (mg = *mgp; mg; mg = *mgp) {
2717 if (mg->mg_type == type) {
2718 MGVTBL* vtbl = mg->mg_virtual;
2719 *mgp = mg->mg_moremagic;
2720 if (vtbl && (vtbl->svt_free != NULL))
2721 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
2722 if (mg->mg_ptr && mg->mg_type != 'g')
2723 if (mg->mg_len >= 0)
2724 Safefree(mg->mg_ptr);
2725 else if (mg->mg_len == HEf_SVKEY)
2726 SvREFCNT_dec((SV*)mg->mg_ptr);
2727 if (mg->mg_flags & MGf_REFCOUNTED)
2728 SvREFCNT_dec(mg->mg_obj);
2732 mgp = &mg->mg_moremagic;
2736 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
2743 Perl_sv_rvweaken(pTHX_ SV *sv)
2746 if (!SvOK(sv)) /* let undefs pass */
2749 Perl_croak(aTHX_ "Can't weaken a nonreference");
2750 else if (SvWEAKREF(sv)) {
2752 if (ckWARN(WARN_MISC))
2753 Perl_warner(aTHX_ WARN_MISC, "Reference is already weak");
2757 sv_add_backref(tsv, sv);
2764 S_sv_add_backref(pTHX_ SV *tsv, SV *sv)
2768 if (SvMAGICAL(tsv) && (mg = mg_find(tsv, '<')))
2769 av = (AV*)mg->mg_obj;
2772 sv_magic(tsv, (SV*)av, '<', NULL, 0);
2773 SvREFCNT_dec(av); /* for sv_magic */
2779 S_sv_del_backref(pTHX_ SV *sv)
2786 if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, '<')))
2787 Perl_croak(aTHX_ "panic: del_backref");
2788 av = (AV *)mg->mg_obj;
2793 svp[i] = &PL_sv_undef; /* XXX */
2800 Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, char *little, STRLEN littlelen)
2804 register char *midend;
2805 register char *bigend;
2811 Perl_croak(aTHX_ "Can't modify non-existent substring");
2812 SvPV_force(bigstr, curlen);
2813 if (offset + len > curlen) {
2814 SvGROW(bigstr, offset+len+1);
2815 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
2816 SvCUR_set(bigstr, offset+len);
2819 i = littlelen - len;
2820 if (i > 0) { /* string might grow */
2821 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
2822 mid = big + offset + len;
2823 midend = bigend = big + SvCUR(bigstr);
2826 while (midend > mid) /* shove everything down */
2827 *--bigend = *--midend;
2828 Move(little,big+offset,littlelen,char);
2834 Move(little,SvPVX(bigstr)+offset,len,char);
2839 big = SvPVX(bigstr);
2842 bigend = big + SvCUR(bigstr);
2844 if (midend > bigend)
2845 Perl_croak(aTHX_ "panic: sv_insert");
2847 if (mid - big > bigend - midend) { /* faster to shorten from end */
2849 Move(little, mid, littlelen,char);
2852 i = bigend - midend;
2854 Move(midend, mid, i,char);
2858 SvCUR_set(bigstr, mid - big);
2861 else if (i = mid - big) { /* faster from front */
2862 midend -= littlelen;
2864 sv_chop(bigstr,midend-i);
2869 Move(little, mid, littlelen,char);
2871 else if (littlelen) {
2872 midend -= littlelen;
2873 sv_chop(bigstr,midend);
2874 Move(little,midend,littlelen,char);
2877 sv_chop(bigstr,midend);
2882 /* make sv point to what nstr did */
2885 Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv)
2888 U32 refcnt = SvREFCNT(sv);
2889 SV_CHECK_THINKFIRST(sv);
2890 if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL))
2891 Perl_warner(aTHX_ WARN_INTERNAL, "Reference miscount in sv_replace()");
2892 if (SvMAGICAL(sv)) {
2896 sv_upgrade(nsv, SVt_PVMG);
2897 SvMAGIC(nsv) = SvMAGIC(sv);
2898 SvFLAGS(nsv) |= SvMAGICAL(sv);
2904 assert(!SvREFCNT(sv));
2905 StructCopy(nsv,sv,SV);
2906 SvREFCNT(sv) = refcnt;
2907 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
2912 Perl_sv_clear(pTHX_ register SV *sv)
2916 assert(SvREFCNT(sv) == 0);
2920 if (PL_defstash) { /* Still have a symbol table? */
2925 Zero(&tmpref, 1, SV);
2926 sv_upgrade(&tmpref, SVt_RV);
2928 SvREADONLY_on(&tmpref); /* DESTROY() could be naughty */
2929 SvREFCNT(&tmpref) = 1;
2932 stash = SvSTASH(sv);
2933 destructor = gv_fetchmethod(SvSTASH(sv), "DESTROY");
2936 PUSHSTACKi(PERLSI_DESTROY);
2937 SvRV(&tmpref) = SvREFCNT_inc(sv);
2942 call_sv((SV*)GvCV(destructor),
2943 G_DISCARD|G_EVAL|G_KEEPERR);
2949 } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
2951 del_XRV(SvANY(&tmpref));
2954 if (PL_in_clean_objs)
2955 Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'",
2957 /* DESTROY gave object new lease on life */
2963 SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */
2964 SvOBJECT_off(sv); /* Curse the object. */
2965 if (SvTYPE(sv) != SVt_PVIO)
2966 --PL_sv_objcount; /* XXX Might want something more general */
2969 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2972 switch (SvTYPE(sv)) {
2975 IoIFP(sv) != PerlIO_stdin() &&
2976 IoIFP(sv) != PerlIO_stdout() &&
2977 IoIFP(sv) != PerlIO_stderr())
2979 io_close((IO*)sv, FALSE);
2982 PerlDir_close(IoDIRP(sv));
2985 Safefree(IoTOP_NAME(sv));
2986 Safefree(IoFMT_NAME(sv));
2987 Safefree(IoBOTTOM_NAME(sv));
3002 SvREFCNT_dec(LvTARG(sv));
3006 Safefree(GvNAME(sv));
3007 /* cannot decrease stash refcount yet, as we might recursively delete
3008 ourselves when the refcnt drops to zero. Delay SvREFCNT_dec
3009 of stash until current sv is completely gone.
3010 -- JohnPC, 27 Mar 1998 */
3011 stash = GvSTASH(sv);
3017 (void)SvOOK_off(sv);
3025 SvREFCNT_dec(SvRV(sv));
3027 else if (SvPVX(sv) && SvLEN(sv))
3028 Safefree(SvPVX(sv));
3038 switch (SvTYPE(sv)) {
3054 del_XPVIV(SvANY(sv));
3057 del_XPVNV(SvANY(sv));
3060 del_XPVMG(SvANY(sv));
3063 del_XPVLV(SvANY(sv));
3066 del_XPVAV(SvANY(sv));
3069 del_XPVHV(SvANY(sv));
3072 del_XPVCV(SvANY(sv));
3075 del_XPVGV(SvANY(sv));
3076 /* code duplication for increased performance. */
3077 SvFLAGS(sv) &= SVf_BREAK;
3078 SvFLAGS(sv) |= SVTYPEMASK;
3079 /* decrease refcount of the stash that owns this GV, if any */
3081 SvREFCNT_dec(stash);
3082 return; /* not break, SvFLAGS reset already happened */
3084 del_XPVBM(SvANY(sv));
3087 del_XPVFM(SvANY(sv));
3090 del_XPVIO(SvANY(sv));
3093 SvFLAGS(sv) &= SVf_BREAK;
3094 SvFLAGS(sv) |= SVTYPEMASK;
3098 Perl_sv_newref(pTHX_ SV *sv)
3101 ATOMIC_INC(SvREFCNT(sv));
3106 Perl_sv_free(pTHX_ SV *sv)
3109 int refcount_is_zero;
3113 if (SvREFCNT(sv) == 0) {
3114 if (SvFLAGS(sv) & SVf_BREAK)
3116 if (PL_in_clean_all) /* All is fair */
3118 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
3119 /* make sure SvREFCNT(sv)==0 happens very seldom */
3120 SvREFCNT(sv) = (~(U32)0)/2;
3123 if (ckWARN_d(WARN_INTERNAL))
3124 Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free unreferenced scalar");
3127 ATOMIC_DEC_AND_TEST(refcount_is_zero, SvREFCNT(sv));
3128 if (!refcount_is_zero)
3132 if (ckWARN_d(WARN_DEBUGGING))
3133 Perl_warner(aTHX_ WARN_DEBUGGING,
3134 "Attempt to free temp prematurely: SV 0x%lx", (unsigned long)sv);
3138 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
3139 /* make sure SvREFCNT(sv)==0 happens very seldom */
3140 SvREFCNT(sv) = (~(U32)0)/2;
3149 Perl_sv_len(pTHX_ register SV *sv)
3158 len = mg_length(sv);
3160 junk = SvPV(sv, len);
3165 Perl_sv_len_utf8(pTHX_ register SV *sv)
3176 len = mg_length(sv);
3179 s = (U8*)SvPV(sv, len);
3190 Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp)
3195 I32 uoffset = *offsetp;
3201 start = s = (U8*)SvPV(sv, len);
3203 while (s < send && uoffset--)
3207 *offsetp = s - start;
3211 while (s < send && ulen--)
3221 Perl_sv_pos_b2u(pTHX_ register SV *sv, I32* offsetp)
3230 s = (U8*)SvPV(sv, len);
3232 Perl_croak(aTHX_ "panic: bad byte offset");
3233 send = s + *offsetp;
3241 if (ckWARN_d(WARN_UTF8))
3242 Perl_warner(aTHX_ WARN_UTF8, "Malformed UTF-8 character");
3250 Perl_sv_eq(pTHX_ register SV *str1, register SV *str2)
3262 pv1 = SvPV(str1, cur1);
3267 pv2 = SvPV(str2, cur2);
3272 return memEQ(pv1, pv2, cur1);
3276 Perl_sv_cmp(pTHX_ register SV *str1, register SV *str2)
3279 char *pv1 = str1 ? SvPV(str1, cur1) : (char *) NULL;
3281 char *pv2 = str2 ? SvPV(str2, cur2) : (char *) NULL;
3285 return cur2 ? -1 : 0;
3290 retval = memcmp((void*)pv1, (void*)pv2, cur1 < cur2 ? cur1 : cur2);
3293 return retval < 0 ? -1 : 1;
3298 return cur1 < cur2 ? -1 : 1;
3302 Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2)
3304 #ifdef USE_LOCALE_COLLATE
3310 if (PL_collation_standard)
3314 pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL;
3316 pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL;
3318 if (!pv1 || !len1) {
3329 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
3332 return retval < 0 ? -1 : 1;
3335 * When the result of collation is equality, that doesn't mean
3336 * that there are no differences -- some locales exclude some
3337 * characters from consideration. So to avoid false equalities,
3338 * we use the raw string as a tiebreaker.
3344 #endif /* USE_LOCALE_COLLATE */
3346 return sv_cmp(sv1, sv2);
3349 #ifdef USE_LOCALE_COLLATE
3351 * Any scalar variable may carry an 'o' magic that contains the
3352 * scalar data of the variable transformed to such a format that
3353 * a normal memory comparison can be used to compare the data
3354 * according to the locale settings.
3357 Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp)
3361 mg = SvMAGICAL(sv) ? mg_find(sv, 'o') : (MAGIC *) NULL;
3362 if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
3367 Safefree(mg->mg_ptr);
3369 if ((xf = mem_collxfrm(s, len, &xlen))) {
3370 if (SvREADONLY(sv)) {
3373 return xf + sizeof(PL_collation_ix);
3376 sv_magic(sv, 0, 'o', 0, 0);
3377 mg = mg_find(sv, 'o');
3390 if (mg && mg->mg_ptr) {
3392 return mg->mg_ptr + sizeof(PL_collation_ix);
3400 #endif /* USE_LOCALE_COLLATE */
3403 Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append)
3408 register STDCHAR rslast;
3409 register STDCHAR *bp;
3413 SV_CHECK_THINKFIRST(sv);
3414 (void)SvUPGRADE(sv, SVt_PV);
3418 if (RsSNARF(PL_rs)) {
3422 else if (RsRECORD(PL_rs)) {
3423 I32 recsize, bytesread;
3426 /* Grab the size of the record we're getting */
3427 recsize = SvIV(SvRV(PL_rs));
3428 (void)SvPOK_only(sv); /* Validate pointer */
3429 buffer = SvGROW(sv, recsize + 1);
3432 /* VMS wants read instead of fread, because fread doesn't respect */
3433 /* RMS record boundaries. This is not necessarily a good thing to be */
3434 /* doing, but we've got no other real choice */
3435 bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize);
3437 bytesread = PerlIO_read(fp, buffer, recsize);
3439 SvCUR_set(sv, bytesread);
3440 buffer[bytesread] = '\0';
3441 return(SvCUR(sv) ? SvPVX(sv) : Nullch);
3443 else if (RsPARA(PL_rs)) {
3448 rsptr = SvPV(PL_rs, rslen);
3449 rslast = rslen ? rsptr[rslen - 1] : '\0';
3451 if (RsPARA(PL_rs)) { /* have to do this both before and after */
3452 do { /* to make sure file boundaries work right */
3455 i = PerlIO_getc(fp);
3459 PerlIO_ungetc(fp,i);
3465 /* See if we know enough about I/O mechanism to cheat it ! */
3467 /* This used to be #ifdef test - it is made run-time test for ease
3468 of abstracting out stdio interface. One call should be cheap
3469 enough here - and may even be a macro allowing compile
3473 if (PerlIO_fast_gets(fp)) {
3476 * We're going to steal some values from the stdio struct
3477 * and put EVERYTHING in the innermost loop into registers.
3479 register STDCHAR *ptr;
3483 #if defined(VMS) && defined(PERLIO_IS_STDIO)
3484 /* An ungetc()d char is handled separately from the regular
3485 * buffer, so we getc() it back out and stuff it in the buffer.
3487 i = PerlIO_getc(fp);
3488 if (i == EOF) return 0;
3489 *(--((*fp)->_ptr)) = (unsigned char) i;
3493 /* Here is some breathtakingly efficient cheating */
3495 cnt = PerlIO_get_cnt(fp); /* get count into register */
3496 (void)SvPOK_only(sv); /* validate pointer */
3497 if (SvLEN(sv) - append <= cnt + 1) { /* make sure we have the room */
3498 if (cnt > 80 && SvLEN(sv) > append) {
3499 shortbuffered = cnt - SvLEN(sv) + append + 1;
3500 cnt -= shortbuffered;
3504 /* remember that cnt can be negative */
3505 SvGROW(sv, append + (cnt <= 0 ? 2 : (cnt + 1)));
3510 bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */
3511 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
3512 DEBUG_P(PerlIO_printf(Perl_debug_log,
3513 "Screamer: entering, ptr=%ld, cnt=%ld\n",(long)ptr,(long)cnt));
3514 DEBUG_P(PerlIO_printf(Perl_debug_log,
3515 "Screamer: entering: FILE * thinks ptr=%ld, cnt=%ld, base=%ld\n",
3516 (long)PerlIO_get_ptr(fp), (long)PerlIO_get_cnt(fp),
3517 (long)(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
3522 while (cnt > 0) { /* this | eat */
3524 if ((*bp++ = *ptr++) == rslast) /* really | dust */
3525 goto thats_all_folks; /* screams | sed :-) */
3529 Copy(ptr, bp, cnt, char); /* this | eat */
3530 bp += cnt; /* screams | dust */
3531 ptr += cnt; /* louder | sed :-) */
3536 if (shortbuffered) { /* oh well, must extend */
3537 cnt = shortbuffered;
3539 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
3541 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
3542 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
3546 DEBUG_P(PerlIO_printf(Perl_debug_log,
3547 "Screamer: going to getc, ptr=%ld, cnt=%ld\n",(long)ptr,(long)cnt));
3548 PerlIO_set_ptrcnt(fp, ptr, cnt); /* deregisterize cnt and ptr */
3549 DEBUG_P(PerlIO_printf(Perl_debug_log,
3550 "Screamer: pre: FILE * thinks ptr=%ld, cnt=%ld, base=%ld\n",
3551 (long)PerlIO_get_ptr(fp), (long)PerlIO_get_cnt(fp),
3552 (long)(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
3553 /* This used to call 'filbuf' in stdio form, but as that behaves like
3554 getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
3555 another abstraction. */
3556 i = PerlIO_getc(fp); /* get more characters */
3557 DEBUG_P(PerlIO_printf(Perl_debug_log,
3558 "Screamer: post: FILE * thinks ptr=%ld, cnt=%ld, base=%ld\n",
3559 (long)PerlIO_get_ptr(fp), (long)PerlIO_get_cnt(fp),
3560 (long)(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
3561 cnt = PerlIO_get_cnt(fp);
3562 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
3563 DEBUG_P(PerlIO_printf(Perl_debug_log,
3564 "Screamer: after getc, ptr=%ld, cnt=%ld\n",(long)ptr,(long)cnt));
3566 if (i == EOF) /* all done for ever? */
3567 goto thats_really_all_folks;
3569 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
3571 SvGROW(sv, bpx + cnt + 2);
3572 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
3574 *bp++ = i; /* store character from PerlIO_getc */
3576 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
3577 goto thats_all_folks;
3581 if ((rslen > 1 && (bp - (STDCHAR*)SvPVX(sv) < rslen)) ||
3582 memNE((char*)bp - rslen, rsptr, rslen))
3583 goto screamer; /* go back to the fray */
3584 thats_really_all_folks:
3586 cnt += shortbuffered;
3587 DEBUG_P(PerlIO_printf(Perl_debug_log,
3588 "Screamer: quitting, ptr=%ld, cnt=%ld\n",(long)ptr,(long)cnt));
3589 PerlIO_set_ptrcnt(fp, ptr, cnt); /* put these back or we're in trouble */
3590 DEBUG_P(PerlIO_printf(Perl_debug_log,
3591 "Screamer: end: FILE * thinks ptr=%ld, cnt=%ld, base=%ld\n",
3592 (long)PerlIO_get_ptr(fp), (long)PerlIO_get_cnt(fp),
3593 (long)(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
3595 SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */
3596 DEBUG_P(PerlIO_printf(Perl_debug_log,
3597 "Screamer: done, len=%ld, string=|%.*s|\n",
3598 (long)SvCUR(sv),(int)SvCUR(sv),SvPVX(sv)));
3603 /*The big, slow, and stupid way */
3606 /* Need to work around EPOC SDK features */
3607 /* On WINS: MS VC5 generates calls to _chkstk, */
3608 /* if a `large' stack frame is allocated */
3609 /* gcc on MARM does not generate calls like these */
3615 register STDCHAR *bpe = buf + sizeof(buf);
3617 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = i) != rslast && bp < bpe)
3618 ; /* keep reading */
3622 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
3623 /* Accomodate broken VAXC compiler, which applies U8 cast to
3624 * both args of ?: operator, causing EOF to change into 255
3626 if (cnt) { i = (U8)buf[cnt - 1]; } else { i = EOF; }
3630 sv_catpvn(sv, (char *) buf, cnt);
3632 sv_setpvn(sv, (char *) buf, cnt);
3634 if (i != EOF && /* joy */
3636 SvCUR(sv) < rslen ||
3637 memNE(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
3641 * If we're reading from a TTY and we get a short read,
3642 * indicating that the user hit his EOF character, we need
3643 * to notice it now, because if we try to read from the TTY
3644 * again, the EOF condition will disappear.
3646 * The comparison of cnt to sizeof(buf) is an optimization
3647 * that prevents unnecessary calls to feof().
3651 if (!(cnt < sizeof(buf) && PerlIO_eof(fp)))
3656 if (RsPARA(PL_rs)) { /* have to do this both before and after */
3657 while (i != EOF) { /* to make sure file boundaries work right */
3658 i = PerlIO_getc(fp);
3660 PerlIO_ungetc(fp,i);
3667 win32_strip_return(sv);
3670 return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch;
3675 Perl_sv_inc(pTHX_ register SV *sv)
3684 if (SvTHINKFIRST(sv)) {
3685 if (SvREADONLY(sv)) {
3687 if (PL_curcop != &PL_compiling)
3688 Perl_croak(aTHX_ PL_no_modify);
3692 if (SvAMAGIC(sv) && AMG_CALLun(sv,inc))
3694 i = (IV)PTR_CAST SvRV(sv);
3699 flags = SvFLAGS(sv);
3700 if (flags & SVp_NOK) {
3701 (void)SvNOK_only(sv);
3705 if (flags & SVp_IOK) {
3707 if (SvUVX(sv) == UV_MAX)
3708 sv_setnv(sv, (NV)UV_MAX + 1.0);
3710 (void)SvIOK_only_UV(sv);
3713 if (SvIVX(sv) == IV_MAX)
3714 sv_setnv(sv, (NV)IV_MAX + 1.0);
3716 (void)SvIOK_only(sv);
3722 if (!(flags & SVp_POK) || !*SvPVX(sv)) {
3723 if ((flags & SVTYPEMASK) < SVt_PVNV)
3724 sv_upgrade(sv, SVt_NV);
3726 (void)SvNOK_only(sv);
3730 while (isALPHA(*d)) d++;
3731 while (isDIGIT(*d)) d++;
3733 sv_setnv(sv,Atof(SvPVX(sv)) + 1.0); /* punt */
3737 while (d >= SvPVX(sv)) {
3745 /* MKS: The original code here died if letters weren't consecutive.
3746 * at least it didn't have to worry about non-C locales. The
3747 * new code assumes that ('z'-'a')==('Z'-'A'), letters are
3748 * arranged in order (although not consecutively) and that only
3749 * [A-Za-z] are accepted by isALPHA in the C locale.
3751 if (*d != 'z' && *d != 'Z') {
3752 do { ++*d; } while (!isALPHA(*d));
3755 *(d--) -= 'z' - 'a';
3760 *(d--) -= 'z' - 'a' + 1;
3764 /* oh,oh, the number grew */
3765 SvGROW(sv, SvCUR(sv) + 2);
3767 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--)
3776 Perl_sv_dec(pTHX_ register SV *sv)
3784 if (SvTHINKFIRST(sv)) {
3785 if (SvREADONLY(sv)) {
3787 if (PL_curcop != &PL_compiling)
3788 Perl_croak(aTHX_ PL_no_modify);
3792 if (SvAMAGIC(sv) && AMG_CALLun(sv,dec))
3794 i = (IV)PTR_CAST SvRV(sv);
3799 flags = SvFLAGS(sv);
3800 if (flags & SVp_NOK) {
3802 (void)SvNOK_only(sv);
3805 if (flags & SVp_IOK) {
3807 if (SvUVX(sv) == 0) {
3808 (void)SvIOK_only(sv);
3812 (void)SvIOK_only_UV(sv);
3816 if (SvIVX(sv) == IV_MIN)
3817 sv_setnv(sv, (NV)IV_MIN - 1.0);
3819 (void)SvIOK_only(sv);
3825 if (!(flags & SVp_POK)) {
3826 if ((flags & SVTYPEMASK) < SVt_PVNV)
3827 sv_upgrade(sv, SVt_NV);
3829 (void)SvNOK_only(sv);
3832 sv_setnv(sv,Atof(SvPVX(sv)) - 1.0); /* punt */
3835 /* Make a string that will exist for the duration of the expression
3836 * evaluation. Actually, it may have to last longer than that, but
3837 * hopefully we won't free it until it has been assigned to a
3838 * permanent location. */
3841 Perl_sv_mortalcopy(pTHX_ SV *oldstr)
3847 sv_setsv(sv,oldstr);
3849 PL_tmps_stack[++PL_tmps_ix] = sv;
3855 Perl_sv_newmortal(pTHX)
3861 SvFLAGS(sv) = SVs_TEMP;
3863 PL_tmps_stack[++PL_tmps_ix] = sv;
3867 /* same thing without the copying */
3870 Perl_sv_2mortal(pTHX_ register SV *sv)
3875 if (SvREADONLY(sv) && SvIMMORTAL(sv))
3878 PL_tmps_stack[++PL_tmps_ix] = sv;
3884 Perl_newSVpv(pTHX_ const char *s, STRLEN len)
3891 sv_setpvn(sv,s,len);
3896 Perl_newSVpvn(pTHX_ const char *s, STRLEN len)
3901 sv_setpvn(sv,s,len);
3905 #if defined(PERL_IMPLICIT_CONTEXT)
3907 Perl_newSVpvf_nocontext(const char* pat, ...)
3912 va_start(args, pat);
3913 sv = vnewSVpvf(pat, &args);
3920 Perl_newSVpvf(pTHX_ const char* pat, ...)
3924 va_start(args, pat);
3925 sv = vnewSVpvf(pat, &args);
3931 Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args)
3935 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
3940 Perl_newSVnv(pTHX_ NV n)
3950 Perl_newSViv(pTHX_ IV i)
3960 Perl_newRV_noinc(pTHX_ SV *tmpRef)
3966 sv_upgrade(sv, SVt_RV);
3974 Perl_newRV(pTHX_ SV *tmpRef)
3976 return newRV_noinc(SvREFCNT_inc(tmpRef));
3979 /* make an exact duplicate of old */
3982 Perl_newSVsv(pTHX_ register SV *old)
3989 if (SvTYPE(old) == SVTYPEMASK) {
3990 if (ckWARN_d(WARN_INTERNAL))
3991 Perl_warner(aTHX_ WARN_INTERNAL, "semi-panic: attempt to dup freed string");
4006 Perl_sv_reset(pTHX_ register char *s, HV *stash)
4014 char todo[PERL_UCHAR_MAX+1];
4019 if (!*s) { /* reset ?? searches */
4020 for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) {
4021 pm->op_pmdynflags &= ~PMdf_USED;
4026 /* reset variables */
4028 if (!HvARRAY(stash))
4031 Zero(todo, 256, char);
4033 i = (unsigned char)*s;
4037 max = (unsigned char)*s++;
4038 for ( ; i <= max; i++) {
4041 for (i = 0; i <= (I32) HvMAX(stash); i++) {
4042 for (entry = HvARRAY(stash)[i];
4044 entry = HeNEXT(entry))
4046 if (!todo[(U8)*HeKEY(entry)])
4048 gv = (GV*)HeVAL(entry);
4050 if (SvTHINKFIRST(sv)) {
4051 if (!SvREADONLY(sv) && SvROK(sv))
4056 if (SvTYPE(sv) >= SVt_PV) {
4058 if (SvPVX(sv) != Nullch)
4065 if (GvHV(gv) && !HvNAME(GvHV(gv))) {
4067 #ifndef VMS /* VMS has no environ array */
4069 environ[0] = Nullch;
4078 Perl_sv_2io(pTHX_ SV *sv)
4084 switch (SvTYPE(sv)) {
4092 Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv));
4096 Perl_croak(aTHX_ PL_no_usym, "filehandle");
4098 return sv_2io(SvRV(sv));
4099 gv = gv_fetchpv(SvPV(sv,n_a), FALSE, SVt_PVIO);
4105 Perl_croak(aTHX_ "Bad filehandle: %s", SvPV(sv,n_a));
4112 Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref)
4119 return *gvp = Nullgv, Nullcv;
4120 switch (SvTYPE(sv)) {
4140 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
4141 tryAMAGICunDEREF(to_cv);
4144 if (SvTYPE(sv) == SVt_PVCV) {
4153 Perl_croak(aTHX_ "Not a subroutine reference");
4158 gv = gv_fetchpv(SvPV(sv, n_a), lref, SVt_PVCV);
4164 if (lref && !GvCVu(gv)) {
4167 tmpsv = NEWSV(704,0);
4168 gv_efullname3(tmpsv, gv, Nullch);
4169 /* XXX this is probably not what they think they're getting.
4170 * It has the same effect as "sub name;", i.e. just a forward
4172 newSUB(start_subparse(FALSE, 0),
4173 newSVOP(OP_CONST, 0, tmpsv),
4178 Perl_croak(aTHX_ "Unable to create sub named \"%s\"", SvPV(sv,n_a));
4185 Perl_sv_true(pTHX_ register SV *sv)
4192 if ((tXpv = (XPV*)SvANY(sv)) &&
4193 (*tXpv->xpv_pv > '0' ||
4194 tXpv->xpv_cur > 1 ||
4195 (tXpv->xpv_cur && *tXpv->xpv_pv != '0')))
4202 return SvIVX(sv) != 0;
4205 return SvNVX(sv) != 0.0;
4207 return sv_2bool(sv);
4213 Perl_sv_iv(pTHX_ register SV *sv)
4217 return (IV)SvUVX(sv);
4224 Perl_sv_uv(pTHX_ register SV *sv)
4229 return (UV)SvIVX(sv);
4235 Perl_sv_nv(pTHX_ register SV *sv)
4243 Perl_sv_pv(pTHX_ SV *sv)
4250 return sv_2pv(sv, &n_a);
4254 Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp)
4260 return sv_2pv(sv, lp);
4264 Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp)
4268 if (SvTHINKFIRST(sv) && !SvROK(sv))
4269 sv_force_normal(sv);
4275 if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) {
4277 Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
4278 PL_op_name[PL_op->op_type]);
4282 if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */
4287 (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */
4288 SvGROW(sv, len + 1);
4289 Move(s,SvPVX(sv),len,char);
4294 SvPOK_on(sv); /* validate pointer */
4296 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2pv(%s)\n",
4297 (unsigned long)sv,SvPVX(sv)));
4304 Perl_sv_reftype(pTHX_ SV *sv, int ob)
4306 if (ob && SvOBJECT(sv))
4307 return HvNAME(SvSTASH(sv));
4309 switch (SvTYPE(sv)) {
4323 case SVt_PVLV: return "LVALUE";
4324 case SVt_PVAV: return "ARRAY";
4325 case SVt_PVHV: return "HASH";
4326 case SVt_PVCV: return "CODE";
4327 case SVt_PVGV: return "GLOB";
4328 case SVt_PVFM: return "FORMAT";
4329 default: return "UNKNOWN";
4335 Perl_sv_isobject(pTHX_ SV *sv)
4350 Perl_sv_isa(pTHX_ SV *sv, const char *name)
4362 return strEQ(HvNAME(SvSTASH(sv)), name);
4366 Perl_newSVrv(pTHX_ SV *rv, const char *classname)
4373 SV_CHECK_THINKFIRST(rv);
4376 if (SvTYPE(rv) < SVt_RV)
4377 sv_upgrade(rv, SVt_RV);
4384 HV* stash = gv_stashpv(classname, TRUE);
4385 (void)sv_bless(rv, stash);
4391 Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv)
4394 sv_setsv(rv, &PL_sv_undef);
4398 sv_setiv(newSVrv(rv,classname), (IV)PTR_CAST pv);
4403 Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv)
4405 sv_setiv(newSVrv(rv,classname), iv);
4410 Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv)
4412 sv_setnv(newSVrv(rv,classname), nv);
4417 Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, char *pv, STRLEN n)
4419 sv_setpvn(newSVrv(rv,classname), pv, n);
4424 Perl_sv_bless(pTHX_ SV *sv, HV *stash)
4429 Perl_croak(aTHX_ "Can't bless non-reference value");
4431 if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) {
4432 if (SvREADONLY(tmpRef))
4433 Perl_croak(aTHX_ PL_no_modify);
4434 if (SvOBJECT(tmpRef)) {
4435 if (SvTYPE(tmpRef) != SVt_PVIO)
4437 SvREFCNT_dec(SvSTASH(tmpRef));
4440 SvOBJECT_on(tmpRef);
4441 if (SvTYPE(tmpRef) != SVt_PVIO)
4443 (void)SvUPGRADE(tmpRef, SVt_PVMG);
4444 SvSTASH(tmpRef) = (HV*)SvREFCNT_inc(stash);
4455 S_sv_unglob(pTHX_ SV *sv)
4457 assert(SvTYPE(sv) == SVt_PVGV);
4462 SvREFCNT_dec(GvSTASH(sv));
4463 GvSTASH(sv) = Nullhv;
4465 sv_unmagic(sv, '*');
4466 Safefree(GvNAME(sv));
4468 SvFLAGS(sv) &= ~SVTYPEMASK;
4469 SvFLAGS(sv) |= SVt_PVMG;
4473 Perl_sv_unref(pTHX_ SV *sv)
4477 if (SvWEAKREF(sv)) {
4485 if (SvREFCNT(rv) != 1 || SvREADONLY(rv))
4488 sv_2mortal(rv); /* Schedule for freeing later */
4492 Perl_sv_taint(pTHX_ SV *sv)
4494 sv_magic((sv), Nullsv, 't', Nullch, 0);
4498 Perl_sv_untaint(pTHX_ SV *sv)
4500 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
4501 MAGIC *mg = mg_find(sv, 't');
4508 Perl_sv_tainted(pTHX_ SV *sv)
4510 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
4511 MAGIC *mg = mg_find(sv, 't');
4512 if (mg && ((mg->mg_len & 1) || (mg->mg_len & 2) && mg->mg_obj == sv))
4519 Perl_sv_setpviv(pTHX_ SV *sv, IV iv)
4521 char buf[TYPE_CHARS(UV)];
4523 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
4525 sv_setpvn(sv, ptr, ebuf - ptr);
4530 Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv)
4532 char buf[TYPE_CHARS(UV)];
4534 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
4536 sv_setpvn(sv, ptr, ebuf - ptr);
4540 #if defined(PERL_IMPLICIT_CONTEXT)
4542 Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...)
4546 va_start(args, pat);
4547 sv_vsetpvf(sv, pat, &args);
4553 Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...)
4557 va_start(args, pat);
4558 sv_vsetpvf_mg(sv, pat, &args);
4564 Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...)
4567 va_start(args, pat);
4568 sv_vsetpvf(sv, pat, &args);
4573 Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args)
4575 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
4579 Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...)
4582 va_start(args, pat);
4583 sv_vsetpvf_mg(sv, pat, &args);
4588 Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
4590 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
4594 #if defined(PERL_IMPLICIT_CONTEXT)
4596 Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...)
4600 va_start(args, pat);
4601 sv_vcatpvf(sv, pat, &args);
4606 Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...)
4610 va_start(args, pat);
4611 sv_vcatpvf_mg(sv, pat, &args);
4617 Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...)
4620 va_start(args, pat);
4621 sv_vcatpvf(sv, pat, &args);
4626 Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args)
4628 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
4632 Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...)
4635 va_start(args, pat);
4636 sv_vcatpvf_mg(sv, pat, &args);
4641 Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
4643 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
4648 Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *used_locale)
4650 sv_setpvn(sv, "", 0);
4651 sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, used_locale);
4655 Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *used_locale)
4663 static char nullstr[] = "(null)";
4665 /* no matter what, this is a string now */
4666 (void)SvPV_force(sv, origlen);
4668 /* special-case "", "%s", and "%_" */
4671 if (patlen == 2 && pat[0] == '%') {
4675 char *s = va_arg(*args, char*);
4676 sv_catpv(sv, s ? s : nullstr);
4678 else if (svix < svmax)
4679 sv_catsv(sv, *svargs);
4683 sv_catsv(sv, va_arg(*args, SV*));
4686 /* See comment on '_' below */
4691 patend = (char*)pat + patlen;
4692 for (p = (char*)pat; p < patend; p = q) {
4700 bool has_precis = FALSE;
4705 STRLEN esignlen = 0;
4707 char *eptr = Nullch;
4709 /* Times 4: a decimal digit takes more than 3 binary digits.
4710 * NV_DIG: mantissa takes than many decimal digits.
4711 * Plus 32: Playing safe. */
4712 char ebuf[IV_DIG * 4 + NV_DIG + 32];
4713 /* large enough for "%#.#f" --chip */
4714 /* what about long double NVs? --jhi */
4725 for (q = p; q < patend && *q != '%'; ++q) ;
4727 sv_catpvn(sv, p, q - p);
4765 case '1': case '2': case '3':
4766 case '4': case '5': case '6':
4767 case '7': case '8': case '9':
4770 width = width * 10 + (*q++ - '0');
4775 i = va_arg(*args, int);
4777 i = (svix < svmax) ? SvIVx(svargs[svix++]) : 0;
4779 width = (i < 0) ? -i : i;
4790 i = va_arg(*args, int);
4792 i = (svix < svmax) ? SvIVx(svargs[svix++]) : 0;
4793 precis = (i < 0) ? 0 : i;
4799 precis = precis * 10 + (*q++ - '0');
4809 if (*(q + 1) == 'l') { /* lld */
4841 uv = va_arg(*args, int);
4843 uv = (svix < svmax) ? SvIVx(svargs[svix++]) : 0;
4845 eptr = (char*)utf8buf;
4846 elen = uv_to_utf8((U8*)eptr, uv) - utf8buf;
4850 c = va_arg(*args, int);
4852 c = (svix < svmax) ? SvIVx(svargs[svix++]) : 0;
4859 eptr = va_arg(*args, char*);
4861 elen = strlen(eptr);
4864 elen = sizeof nullstr - 1;
4867 else if (svix < svmax) {
4868 eptr = SvPVx(svargs[svix++], elen);
4870 if (has_precis && precis < elen) {
4872 sv_pos_u2b(svargs[svix - 1], &p, 0); /* sticks at end */
4875 if (width) { /* fudge width (can't fudge elen) */
4876 width += elen - sv_len_utf8(svargs[svix - 1]);
4884 * The "%_" hack might have to be changed someday,
4885 * if ISO or ANSI decide to use '_' for something.
4886 * So we keep it hidden from users' code.
4890 eptr = SvPVx(va_arg(*args, SV*), elen);
4893 if (has_precis && elen > precis)
4901 uv = (UV)PTR_CAST va_arg(*args, void*);
4903 uv = (svix < svmax) ? (UV)PTR_CAST svargs[svix++] : 0;
4918 case 'h': iv = (short)va_arg(*args, int); break;
4919 default: iv = va_arg(*args, int); break;
4920 case 'l': iv = va_arg(*args, long); break;
4921 case 'V': iv = va_arg(*args, IV); break;
4923 case 'q': iv = va_arg(*args, Quad_t); break;
4928 iv = (svix < svmax) ? SvIVx(svargs[svix++]) : 0;
4930 case 'h': iv = (short)iv; break;
4931 default: iv = (int)iv; break;
4932 case 'l': iv = (long)iv; break;
4935 case 'q': iv = (Quad_t)iv; break;
4942 esignbuf[esignlen++] = plus;
4946 esignbuf[esignlen++] = '-';
4984 case 'h': uv = (unsigned short)va_arg(*args, unsigned); break;
4985 default: uv = va_arg(*args, unsigned); break;
4986 case 'l': uv = va_arg(*args, unsigned long); break;
4987 case 'V': uv = va_arg(*args, UV); break;
4989 case 'q': uv = va_arg(*args, Quad_t); break;
4994 uv = (svix < svmax) ? SvUVx(svargs[svix++]) : 0;
4996 case 'h': uv = (unsigned short)uv; break;
4997 default: uv = (unsigned)uv; break;
4998 case 'l': uv = (unsigned long)uv; break;
5001 case 'q': uv = (Quad_t)uv; break;
5007 eptr = ebuf + sizeof ebuf;
5013 p = (c == 'X') ? "0123456789ABCDEF" : "0123456789abcdef";
5019 esignbuf[esignlen++] = '0';
5020 esignbuf[esignlen++] = c; /* 'x' or 'X' */
5026 *--eptr = '0' + dig;
5028 if (alt && *eptr != '0')
5034 *--eptr = '0' + dig;
5037 esignbuf[esignlen++] = '0';
5038 esignbuf[esignlen++] = 'b';
5041 default: /* it had better be ten or less */
5044 *--eptr = '0' + dig;
5045 } while (uv /= base);
5048 elen = (ebuf + sizeof ebuf) - eptr;
5051 zeros = precis - elen;
5052 else if (precis == 0 && elen == 1 && *eptr == '0')
5057 /* FLOATING POINT */
5060 c = 'f'; /* maybe %F isn't supported here */
5066 /* This is evil, but floating point is even more evil */
5069 nv = va_arg(*args, NV);
5071 nv = (svix < svmax) ? SvNVx(svargs[svix++]) : 0.0;
5074 if (c != 'e' && c != 'E') {
5076 (void)frexp(nv, &i);
5077 if (i == PERL_INT_MIN)
5078 Perl_die(aTHX_ "panic: frexp");
5080 need = BIT_DIGITS(i);
5082 need += has_precis ? precis : 6; /* known default */
5086 need += 20; /* fudge factor */
5087 if (PL_efloatsize < need) {
5088 Safefree(PL_efloatbuf);
5089 PL_efloatsize = need + 20; /* more fudge */
5090 New(906, PL_efloatbuf, PL_efloatsize, char);
5093 eptr = ebuf + sizeof ebuf;
5096 #ifdef USE_LONG_DOUBLE
5098 char* p = PERL_PRIfldbl + sizeof(PERL_PRIfldbl) - 3;
5099 while (p >= PERL_PRIfldbl) { *--eptr = *p--; }
5104 do { *--eptr = '0' + (base % 10); } while (base /= 10);
5109 do { *--eptr = '0' + (base % 10); } while (base /= 10);
5122 RESTORE_NUMERIC_STANDARD();
5123 (void)sprintf(PL_efloatbuf, eptr, nv);
5124 RESTORE_NUMERIC_LOCAL();
5127 eptr = PL_efloatbuf;
5128 elen = strlen(PL_efloatbuf);
5132 * User-defined locales may include arbitrary characters.
5133 * And, unfortunately, some system may alloc the "C" locale
5134 * to be overridden by a malicious user.
5137 *used_locale = TRUE;
5138 #endif /* LC_NUMERIC */
5145 i = SvCUR(sv) - origlen;
5148 case 'h': *(va_arg(*args, short*)) = i; break;
5149 default: *(va_arg(*args, int*)) = i; break;
5150 case 'l': *(va_arg(*args, long*)) = i; break;
5151 case 'V': *(va_arg(*args, IV*)) = i; break;
5153 case 'q': *(va_arg(*args, Quad_t*)) = i; break;
5157 else if (svix < svmax)
5158 sv_setuv(svargs[svix++], (UV)i);
5159 continue; /* not "break" */
5165 if (!args && ckWARN(WARN_PRINTF) &&
5166 (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) {
5167 SV *msg = sv_newmortal();
5168 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %s: ",
5169 (PL_op->op_type == OP_PRTF) ? "printf" : "sprintf");
5173 Perl_sv_catpvf(aTHX_ msg,
5174 "\"%%%c\"", c & 0xFF);
5176 Perl_sv_catpvf(aTHX_ msg,
5177 "\"%%\\%03" PERL_PRIo64 "\"",
5180 Perl_sv_catpvf(aTHX_ msg, isPRINT(c) ?
5181 "\"%%%c\"" : "\"%%\\%03o\"",
5185 sv_catpv(msg, "end of string");
5186 Perl_warner(aTHX_ WARN_PRINTF, "%_", msg); /* yes, this is reentrant */
5189 /* output mangled stuff ... */
5195 /* ... right here, because formatting flags should not apply */
5196 SvGROW(sv, SvCUR(sv) + elen + 1);
5198 memcpy(p, eptr, elen);
5201 SvCUR(sv) = p - SvPVX(sv);
5202 continue; /* not "break" */
5205 have = esignlen + zeros + elen;
5206 need = (have > width ? have : width);
5209 SvGROW(sv, SvCUR(sv) + need + 1);
5211 if (esignlen && fill == '0') {
5212 for (i = 0; i < esignlen; i++)
5216 memset(p, fill, gap);
5219 if (esignlen && fill != '0') {
5220 for (i = 0; i < esignlen; i++)
5224 for (i = zeros; i; i--)
5228 memcpy(p, eptr, elen);
5232 memset(p, ' ', gap);
5236 SvCUR(sv) = p - SvPVX(sv);
5247 do_report_used(pTHXo_ SV *sv)
5249 if (SvTYPE(sv) != SVTYPEMASK) {
5250 /* XXX Perhaps this ought to go to Perl_debug_log, if DEBUGGING. */
5251 PerlIO_printf(PerlIO_stderr(), "****\n");
5257 do_clean_objs(pTHXo_ SV *sv)
5261 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
5262 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv));)
5268 /* XXX Might want to check arrays, etc. */
5271 #ifndef DISABLE_DESTRUCTOR_KLUDGE
5273 do_clean_named_objs(pTHXo_ SV *sv)
5275 if (SvTYPE(sv) == SVt_PVGV) {
5276 if ( SvOBJECT(GvSV(sv)) ||
5277 GvAV(sv) && SvOBJECT(GvAV(sv)) ||
5278 GvHV(sv) && SvOBJECT(GvHV(sv)) ||
5279 GvIO(sv) && SvOBJECT(GvIO(sv)) ||
5280 GvCV(sv) && SvOBJECT(GvCV(sv)) )
5282 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv));)
5290 do_clean_all(pTHXo_ SV *sv)
5292 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%lx\n", sv) );)
5293 SvFLAGS(sv) |= SVf_BREAK;