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.
18 /* Use an overridden DBL_DIG */
22 # define DBL_DIG OVR_DBL_DIG
24 /* The following is all to get DBL_DIG, in order to pick a nice
25 default value for printing floating point numbers in Gconvert.
35 #define DBL_DIG 15 /* A guess that works lots of places */
40 #define FCALL this->*f
41 #define VTBL this->*vtbl
43 #else /* !PERL_OBJECT */
45 static IV asIV _((SV* sv));
46 static UV asUV _((SV* sv));
47 static SV *more_sv _((void));
48 static void more_xiv _((void));
49 static void more_xnv _((void));
50 static void more_xpv _((void));
51 static void more_xrv _((void));
52 static XPVIV *new_xiv _((void));
53 static XPVNV *new_xnv _((void));
54 static XPV *new_xpv _((void));
55 static XRV *new_xrv _((void));
56 static void del_xiv _((XPVIV* p));
57 static void del_xnv _((XPVNV* p));
58 static void del_xpv _((XPV* p));
59 static void del_xrv _((XRV* p));
60 static void sv_unglob _((SV* sv));
63 static void *my_safemalloc(MEM_SIZE size);
66 typedef void (*SVFUNC) _((SV*));
70 #endif /* PERL_OBJECT */
72 #define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) sv_force_normal(sv)
79 (p) = (SV*)safemalloc(sizeof(SV)); \
91 Safefree((char*)(p)); \
96 static I32 registry_size;
98 #define REGHASH(sv,size) ((((U32)(sv)) >> 2) % (size))
100 #define REG_REPLACE(sv,a,b) \
102 void* p = sv->sv_any; \
103 I32 h = REGHASH(sv, registry_size); \
105 while (registry[i] != (a)) { \
106 if (++i >= registry_size) \
109 die("SV registry bug"); \
114 #define REG_ADD(sv) REG_REPLACE(sv,Nullsv,sv)
115 #define REG_REMOVE(sv) REG_REPLACE(sv,sv,Nullsv)
121 if (PL_sv_count >= (registry_size >> 1))
123 SV **oldreg = registry;
124 I32 oldsize = registry_size;
126 registry_size = registry_size ? ((registry_size << 2) + 1) : 2037;
127 Newz(707, registry, registry_size, SV*);
132 for (i = 0; i < oldsize; ++i) {
133 SV* oldsv = oldreg[i];
159 for (i = 0; i < registry_size; ++i) {
160 SV* sv = registry[i];
161 if (sv && SvTYPE(sv) != SVTYPEMASK)
167 sv_add_arena(ptr, size, flags)
172 if (!(flags & SVf_FAKE))
179 * "A time to plant, and a time to uproot what was planted..."
182 #define plant_SV(p) \
184 SvANY(p) = (void *)PL_sv_root; \
185 SvFLAGS(p) = SVTYPEMASK; \
190 /* sv_mutex must be held while calling uproot_SV() */
191 #define uproot_SV(p) \
194 PL_sv_root = (SV*)SvANY(p); \
216 if (PL_debug & 32768) \
226 if (PL_debug & 32768) {
231 for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
233 svend = &sva[SvREFCNT(sva)];
234 if (p >= sv && p < svend)
238 warn("Attempt to free non-arena SV: 0x%lx", (unsigned long)p);
245 #else /* ! DEBUGGING */
247 #define del_SV(p) plant_SV(p)
249 #endif /* DEBUGGING */
252 sv_add_arena(char *ptr, U32 size, U32 flags)
257 Zero(sva, size, char);
259 /* The first SV in an arena isn't an SV. */
260 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
261 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
262 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
264 PL_sv_arenaroot = sva;
265 PL_sv_root = sva + 1;
267 svend = &sva[SvREFCNT(sva) - 1];
270 SvANY(sv) = (void *)(SV*)(sv + 1);
271 SvFLAGS(sv) = SVTYPEMASK;
275 SvFLAGS(sv) = SVTYPEMASK;
278 /* sv_mutex must be held while calling more_sv() */
285 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
286 PL_nice_chunk = Nullch;
289 char *chunk; /* must use New here to match call to */
290 New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */
291 sv_add_arena(chunk, 1008, 0);
304 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
305 svend = &sva[SvREFCNT(sva)];
306 for (sv = sva + 1; sv < svend; ++sv) {
307 if (SvTYPE(sv) != SVTYPEMASK)
316 do_report_used(SV *sv)
318 if (SvTYPE(sv) != SVTYPEMASK) {
319 /* XXX Perhaps this ought to go to Perl_debug_log, if DEBUGGING. */
320 PerlIO_printf(PerlIO_stderr(), "****\n");
328 visit(FUNC_NAME_TO_PTR(do_report_used));
332 do_clean_objs(SV *sv)
336 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
337 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv));)
343 /* XXX Might want to check arrays, etc. */
346 #ifndef DISABLE_DESTRUCTOR_KLUDGE
348 do_clean_named_objs(SV *sv)
350 if (SvTYPE(sv) == SVt_PVGV) {
351 if ( SvOBJECT(GvSV(sv)) ||
352 GvAV(sv) && SvOBJECT(GvAV(sv)) ||
353 GvHV(sv) && SvOBJECT(GvHV(sv)) ||
354 GvIO(sv) && SvOBJECT(GvIO(sv)) ||
355 GvCV(sv) && SvOBJECT(GvCV(sv)) )
357 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv));)
367 PL_in_clean_objs = TRUE;
368 visit(FUNC_NAME_TO_PTR(do_clean_objs));
369 #ifndef DISABLE_DESTRUCTOR_KLUDGE
370 /* some barnacles may yet remain, clinging to typeglobs */
371 visit(FUNC_NAME_TO_PTR(do_clean_named_objs));
373 PL_in_clean_objs = FALSE;
379 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%lx\n", sv) );)
380 SvFLAGS(sv) |= SVf_BREAK;
387 PL_in_clean_all = TRUE;
388 visit(FUNC_NAME_TO_PTR(do_clean_all));
389 PL_in_clean_all = FALSE;
398 /* Free arenas here, but be careful about fake ones. (We assume
399 contiguity of the fake ones with the corresponding real ones.) */
401 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
402 svanext = (SV*) SvANY(sva);
403 while (svanext && SvFAKE(svanext))
404 svanext = (SV*) SvANY(svanext);
407 Safefree((void *)sva);
411 Safefree(PL_nice_chunk);
412 PL_nice_chunk = Nullch;
413 PL_nice_chunk_size = 0;
427 * See comment in more_xiv() -- RAM.
429 PL_xiv_root = *(IV**)xiv;
431 return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv));
437 IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv));
439 *(IV**)xiv = PL_xiv_root;
450 New(705, ptr, 1008/sizeof(XPV), XPV);
451 ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */
452 PL_xiv_arenaroot = ptr; /* to keep Purify happy */
455 xivend = &xiv[1008 / sizeof(IV) - 1];
456 xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */
458 while (xiv < xivend) {
459 *(IV**)xiv = (IV *)(xiv + 1);
473 PL_xnv_root = *(double**)xnv;
475 return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv));
481 double* xnv = (double*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv));
483 *(double**)xnv = PL_xnv_root;
491 register double* xnv;
492 register double* xnvend;
493 New(711, xnv, 1008/sizeof(double), double);
494 xnvend = &xnv[1008 / sizeof(double) - 1];
495 xnv += (sizeof(XPVIV) - 1) / sizeof(double) + 1; /* fudge by sizeof XPVIV */
497 while (xnv < xnvend) {
498 *(double**)xnv = (double*)(xnv + 1);
512 PL_xrv_root = (XRV*)xrv->xrv_rv;
521 p->xrv_rv = (SV*)PL_xrv_root;
530 register XRV* xrvend;
531 New(712, PL_xrv_root, 1008/sizeof(XRV), XRV);
533 xrvend = &xrv[1008 / sizeof(XRV) - 1];
534 while (xrv < xrvend) {
535 xrv->xrv_rv = (SV*)(xrv + 1);
549 PL_xpv_root = (XPV*)xpv->xpv_pv;
558 p->xpv_pv = (char*)PL_xpv_root;
567 register XPV* xpvend;
568 New(713, PL_xpv_root, 1008/sizeof(XPV), XPV);
570 xpvend = &xpv[1008 / sizeof(XPV) - 1];
571 while (xpv < xpvend) {
572 xpv->xpv_pv = (char*)(xpv + 1);
579 #define new_XIV() (void*)safemalloc(sizeof(XPVIV))
580 #define del_XIV(p) Safefree((char*)p)
582 #define new_XIV() (void*)new_xiv()
583 #define del_XIV(p) del_xiv((XPVIV*) p)
587 #define new_XNV() (void*)safemalloc(sizeof(XPVNV))
588 #define del_XNV(p) Safefree((char*)p)
590 #define new_XNV() (void*)new_xnv()
591 #define del_XNV(p) del_xnv((XPVNV*) p)
595 #define new_XRV() (void*)safemalloc(sizeof(XRV))
596 #define del_XRV(p) Safefree((char*)p)
598 #define new_XRV() (void*)new_xrv()
599 #define del_XRV(p) del_xrv((XRV*) p)
603 #define new_XPV() (void*)safemalloc(sizeof(XPV))
604 #define del_XPV(p) Safefree((char*)p)
606 #define new_XPV() (void*)new_xpv()
607 #define del_XPV(p) del_xpv((XPV *)p)
611 # define my_safemalloc(s) safemalloc(s)
612 # define my_safefree(s) safefree(s)
615 my_safemalloc(MEM_SIZE size)
618 New(717, p, size, char);
621 # define my_safefree(s) Safefree(s)
624 #define new_XPVIV() (void*)my_safemalloc(sizeof(XPVIV))
625 #define del_XPVIV(p) my_safefree((char*)p)
627 #define new_XPVNV() (void*)my_safemalloc(sizeof(XPVNV))
628 #define del_XPVNV(p) my_safefree((char*)p)
630 #define new_XPVMG() (void*)my_safemalloc(sizeof(XPVMG))
631 #define del_XPVMG(p) my_safefree((char*)p)
633 #define new_XPVLV() (void*)my_safemalloc(sizeof(XPVLV))
634 #define del_XPVLV(p) my_safefree((char*)p)
636 #define new_XPVAV() (void*)my_safemalloc(sizeof(XPVAV))
637 #define del_XPVAV(p) my_safefree((char*)p)
639 #define new_XPVHV() (void*)my_safemalloc(sizeof(XPVHV))
640 #define del_XPVHV(p) my_safefree((char*)p)
642 #define new_XPVCV() (void*)my_safemalloc(sizeof(XPVCV))
643 #define del_XPVCV(p) my_safefree((char*)p)
645 #define new_XPVGV() (void*)my_safemalloc(sizeof(XPVGV))
646 #define del_XPVGV(p) my_safefree((char*)p)
648 #define new_XPVBM() (void*)my_safemalloc(sizeof(XPVBM))
649 #define del_XPVBM(p) my_safefree((char*)p)
651 #define new_XPVFM() (void*)my_safemalloc(sizeof(XPVFM))
652 #define del_XPVFM(p) my_safefree((char*)p)
654 #define new_XPVIO() (void*)my_safemalloc(sizeof(XPVIO))
655 #define del_XPVIO(p) my_safefree((char*)p)
658 sv_upgrade(register SV *sv, U32 mt)
668 if (SvTYPE(sv) == mt)
674 switch (SvTYPE(sv)) {
689 nv = (double)SvIVX(sv);
695 else if (mt < SVt_PVIV)
712 pv = (char*)SvRV(sv);
716 nv = (double)(unsigned long)pv;
732 else if (mt == SVt_NV)
743 del_XPVIV(SvANY(sv));
753 del_XPVNV(SvANY(sv));
763 del_XPVMG(SvANY(sv));
766 croak("Can't upgrade that kind of scalar");
771 croak("Can't upgrade to undef");
773 SvANY(sv) = new_XIV();
777 SvANY(sv) = new_XNV();
781 SvANY(sv) = new_XRV();
785 SvANY(sv) = new_XPV();
791 SvANY(sv) = new_XPVIV();
801 SvANY(sv) = new_XPVNV();
809 SvANY(sv) = new_XPVMG();
819 SvANY(sv) = new_XPVLV();
833 SvANY(sv) = new_XPVAV();
848 SvANY(sv) = new_XPVHV();
864 SvANY(sv) = new_XPVCV();
865 Zero(SvANY(sv), 1, XPVCV);
875 SvANY(sv) = new_XPVGV();
890 SvANY(sv) = new_XPVBM();
903 SvANY(sv) = new_XPVFM();
904 Zero(SvANY(sv), 1, XPVFM);
914 SvANY(sv) = new_XPVIO();
915 Zero(SvANY(sv), 1, XPVIO);
926 SvFLAGS(sv) &= ~SVTYPEMASK;
932 sv_backoff(register SV *sv)
937 SvLEN(sv) += SvIVX(sv);
938 SvPVX(sv) -= SvIVX(sv);
940 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
942 SvFLAGS(sv) &= ~SVf_OOK;
947 sv_grow(register SV *sv, register STRLEN newlen)
952 if (newlen >= 0x10000) {
953 PerlIO_printf(Perl_debug_log, "Allocation too large: %lx\n", newlen);
956 #endif /* HAS_64K_LIMIT */
959 if (SvTYPE(sv) < SVt_PV) {
960 sv_upgrade(sv, SVt_PV);
963 else if (SvOOK(sv)) { /* pv is offset? */
966 if (newlen > SvLEN(sv))
967 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
969 if (newlen >= 0x10000)
975 if (newlen > SvLEN(sv)) { /* need more room? */
976 if (SvLEN(sv) && s) {
977 #if defined(MYMALLOC) && !defined(PURIFY) && !defined(LEAKTEST)
978 STRLEN l = malloced_size((void*)SvPVX(sv));
984 Renew(s,newlen,char);
987 New(703,s,newlen,char);
989 SvLEN_set(sv, newlen);
995 sv_setiv(register SV *sv, IV i)
997 SV_CHECK_THINKFIRST(sv);
998 switch (SvTYPE(sv)) {
1000 sv_upgrade(sv, SVt_IV);
1003 sv_upgrade(sv, SVt_PVNV);
1007 sv_upgrade(sv, SVt_PVIV);
1018 croak("Can't coerce %s to integer in %s", sv_reftype(sv,0),
1019 PL_op_desc[PL_op->op_type]);
1022 (void)SvIOK_only(sv); /* validate number */
1028 sv_setiv_mg(register SV *sv, IV i)
1035 sv_setuv(register SV *sv, UV u)
1040 sv_setnv(sv, (double)u);
1044 sv_setuv_mg(register SV *sv, UV u)
1051 sv_setnv(register SV *sv, double num)
1053 SV_CHECK_THINKFIRST(sv);
1054 switch (SvTYPE(sv)) {
1057 sv_upgrade(sv, SVt_NV);
1062 sv_upgrade(sv, SVt_PVNV);
1073 croak("Can't coerce %s to number in %s", sv_reftype(sv,0),
1074 PL_op_name[PL_op->op_type]);
1078 (void)SvNOK_only(sv); /* validate number */
1083 sv_setnv_mg(register SV *sv, double num)
1090 not_a_number(SV *sv)
1096 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
1097 /* each *s can expand to 4 chars + "...\0",
1098 i.e. need room for 8 chars */
1100 for (s = SvPVX(sv); *s && d < limit; s++) {
1102 if (ch & 128 && !isPRINT_LC(ch)) {
1111 else if (ch == '\r') {
1115 else if (ch == '\f') {
1119 else if (ch == '\\') {
1123 else if (isPRINT_LC(ch))
1138 warner(WARN_NUMERIC, "Argument \"%s\" isn't numeric in %s", tmpbuf,
1139 PL_op_name[PL_op->op_type]);
1141 warner(WARN_NUMERIC, "Argument \"%s\" isn't numeric", tmpbuf);
1145 sv_2iv(register SV *sv)
1149 if (SvGMAGICAL(sv)) {
1154 if (SvNVX(sv) < 0.0)
1155 return I_V(SvNVX(sv));
1157 return (IV) U_V(SvNVX(sv));
1159 if (SvPOKp(sv) && SvLEN(sv))
1162 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1164 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1165 warner(WARN_UNINITIALIZED, PL_warn_uninit);
1170 if (SvTHINKFIRST(sv)) {
1173 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv, numer)))
1174 return SvIV(tmpstr);
1175 return (IV)SvRV(sv);
1177 if (SvREADONLY(sv)) {
1179 if (SvNVX(sv) < 0.0)
1180 return I_V(SvNVX(sv));
1182 return (IV) U_V(SvNVX(sv));
1184 if (SvPOKp(sv) && SvLEN(sv))
1188 if (ckWARN(WARN_UNINITIALIZED))
1189 warner(WARN_UNINITIALIZED, PL_warn_uninit);
1194 switch (SvTYPE(sv)) {
1196 sv_upgrade(sv, SVt_IV);
1199 sv_upgrade(sv, SVt_PVIV);
1202 sv_upgrade(sv, SVt_PVNV);
1207 if (SvNVX(sv) < 0.0)
1208 SvIVX(sv) = I_V(SvNVX(sv));
1210 SvUVX(sv) = U_V(SvNVX(sv));
1212 else if (SvPOKp(sv) && SvLEN(sv)) {
1214 SvIVX(sv) = asIV(sv);
1218 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1219 warner(WARN_UNINITIALIZED, PL_warn_uninit);
1222 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2iv(%ld)\n",
1223 (unsigned long)sv,(long)SvIVX(sv)));
1228 sv_2uv(register SV *sv)
1232 if (SvGMAGICAL(sv)) {
1237 return U_V(SvNVX(sv));
1238 if (SvPOKp(sv) && SvLEN(sv))
1241 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1243 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1244 warner(WARN_UNINITIALIZED, PL_warn_uninit);
1249 if (SvTHINKFIRST(sv)) {
1252 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv, numer)))
1253 return SvUV(tmpstr);
1254 return (UV)SvRV(sv);
1256 if (SvREADONLY(sv)) {
1258 return U_V(SvNVX(sv));
1260 if (SvPOKp(sv) && SvLEN(sv))
1264 if (ckWARN(WARN_UNINITIALIZED))
1265 warner(WARN_UNINITIALIZED, PL_warn_uninit);
1270 switch (SvTYPE(sv)) {
1272 sv_upgrade(sv, SVt_IV);
1275 sv_upgrade(sv, SVt_PVIV);
1278 sv_upgrade(sv, SVt_PVNV);
1283 SvUVX(sv) = U_V(SvNVX(sv));
1285 else if (SvPOKp(sv) && SvLEN(sv)) {
1287 SvUVX(sv) = asUV(sv);
1290 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1292 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1293 warner(WARN_UNINITIALIZED, PL_warn_uninit);
1297 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2uv(%lu)\n",
1298 (unsigned long)sv,SvUVX(sv)));
1303 sv_2nv(register SV *sv)
1307 if (SvGMAGICAL(sv)) {
1311 if (SvPOKp(sv) && SvLEN(sv)) {
1313 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv))
1315 SET_NUMERIC_STANDARD();
1316 return atof(SvPVX(sv));
1319 return (double)SvIVX(sv);
1321 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1323 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1324 warner(WARN_UNINITIALIZED, PL_warn_uninit);
1329 if (SvTHINKFIRST(sv)) {
1332 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)))
1333 return SvNV(tmpstr);
1334 return (double)(unsigned long)SvRV(sv);
1336 if (SvREADONLY(sv)) {
1338 if (SvPOKp(sv) && SvLEN(sv)) {
1339 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv))
1341 SET_NUMERIC_STANDARD();
1342 return atof(SvPVX(sv));
1345 return (double)SvIVX(sv);
1346 if (ckWARN(WARN_UNINITIALIZED))
1347 warner(WARN_UNINITIALIZED, PL_warn_uninit);
1351 if (SvTYPE(sv) < SVt_NV) {
1352 if (SvTYPE(sv) == SVt_IV)
1353 sv_upgrade(sv, SVt_PVNV);
1355 sv_upgrade(sv, SVt_NV);
1356 DEBUG_c(SET_NUMERIC_STANDARD());
1357 DEBUG_c(PerlIO_printf(Perl_debug_log,
1358 "0x%lx num(%g)\n",(unsigned long)sv,SvNVX(sv)));
1360 else if (SvTYPE(sv) < SVt_PVNV)
1361 sv_upgrade(sv, SVt_PVNV);
1363 (!SvPOKp(sv) || !strchr(SvPVX(sv),'.') || !looks_like_number(sv)))
1365 SvNVX(sv) = (double)SvIVX(sv);
1367 else if (SvPOKp(sv) && SvLEN(sv)) {
1369 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv))
1371 SET_NUMERIC_STANDARD();
1372 SvNVX(sv) = atof(SvPVX(sv));
1376 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1377 warner(WARN_UNINITIALIZED, PL_warn_uninit);
1381 DEBUG_c(SET_NUMERIC_STANDARD());
1382 DEBUG_c(PerlIO_printf(Perl_debug_log,
1383 "0x%lx 2nv(%g)\n",(unsigned long)sv,SvNVX(sv)));
1390 I32 numtype = looks_like_number(sv);
1394 return atol(SvPVX(sv));
1397 if (ckWARN(WARN_NUMERIC))
1400 SET_NUMERIC_STANDARD();
1401 d = atof(SvPVX(sv));
1411 I32 numtype = looks_like_number(sv);
1415 return strtoul(SvPVX(sv), Null(char**), 10);
1419 if (ckWARN(WARN_NUMERIC))
1422 SET_NUMERIC_STANDARD();
1423 return U_V(atof(SvPVX(sv)));
1427 looks_like_number(SV *sv)
1430 register char *send;
1431 register char *sbegin;
1439 else if (SvPOKp(sv))
1440 sbegin = SvPV(sv, len);
1443 send = sbegin + len;
1448 if (*s == '+' || *s == '-')
1451 /* next must be digit or '.' */
1455 } while (isDIGIT(*s));
1458 while (isDIGIT(*s)) /* optional digits after "." */
1462 else if (*s == '.') {
1464 /* no digits before '.' means we need digits after it */
1468 } while (isDIGIT(*s));
1477 * we return 1 if the number can be converted to _integer_ with atol()
1478 * and 2 if you need (int)atof().
1482 /* we can have an optional exponent part */
1483 if (*s == 'e' || *s == 'E') {
1486 if (*s == '+' || *s == '-')
1491 } while (isDIGIT(*s));
1500 if (len == 10 && memEQ(sbegin, "0 but true", 10))
1506 sv_2pv_nolen(register SV *sv)
1509 return sv_2pv(sv, &n_a);
1513 sv_2pv(register SV *sv, STRLEN *lp)
1518 char tmpbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
1524 if (SvGMAGICAL(sv)) {
1531 (void)sprintf(tmpbuf,"%ld",(long)SvIVX(sv));
1536 SET_NUMERIC_STANDARD();
1537 Gconvert(SvNVX(sv), DBL_DIG, 0, tmpbuf);
1542 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1544 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1545 warner(WARN_UNINITIALIZED, PL_warn_uninit);
1551 if (SvTHINKFIRST(sv)) {
1554 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)))
1555 return SvPV(tmpstr,*lp);
1562 switch (SvTYPE(sv)) {
1564 if ( ((SvFLAGS(sv) &
1565 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
1566 == (SVs_OBJECT|SVs_RMG))
1567 && strEQ(s=HvNAME(SvSTASH(sv)), "Regexp")
1568 && (mg = mg_find(sv, 'r'))) {
1570 regexp *re = (regexp *)mg->mg_obj;
1573 char *fptr = "msix";
1578 U16 reganch = (re->reganch & PMf_COMPILETIME) >> 12;
1580 while(ch = *fptr++) {
1582 reflags[left++] = ch;
1585 reflags[right--] = ch;
1590 reflags[left] = '-';
1594 mg->mg_len = re->prelen + 4 + left;
1595 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
1596 Copy("(?", mg->mg_ptr, 2, char);
1597 Copy(reflags, mg->mg_ptr+2, left, char);
1598 Copy(":", mg->mg_ptr+left+2, 1, char);
1599 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
1600 mg->mg_ptr[mg->mg_len - 1] = ')';
1601 mg->mg_ptr[mg->mg_len] = 0;
1603 PL_reginterp_cnt += re->program[0].next_off;
1615 case SVt_PVBM: s = "SCALAR"; break;
1616 case SVt_PVLV: s = "LVALUE"; break;
1617 case SVt_PVAV: s = "ARRAY"; break;
1618 case SVt_PVHV: s = "HASH"; break;
1619 case SVt_PVCV: s = "CODE"; break;
1620 case SVt_PVGV: s = "GLOB"; break;
1621 case SVt_PVFM: s = "FORMAT"; break;
1622 case SVt_PVIO: s = "IO"; break;
1623 default: s = "UNKNOWN"; break;
1627 sv_setpvf(tsv, "%s=%s", HvNAME(SvSTASH(sv)), s);
1630 sv_catpvf(tsv, "(0x%lx)", (unsigned long)sv);
1636 if (SvREADONLY(sv)) {
1638 SET_NUMERIC_STANDARD();
1639 Gconvert(SvNVX(sv), DBL_DIG, 0, tmpbuf);
1644 (void)sprintf(tmpbuf,"%ld",(long)SvIVX(sv));
1650 if (ckWARN(WARN_UNINITIALIZED))
1651 warner(WARN_UNINITIALIZED, PL_warn_uninit);
1657 (void)SvUPGRADE(sv, SVt_PV);
1659 if (SvTYPE(sv) < SVt_PVNV)
1660 sv_upgrade(sv, SVt_PVNV);
1663 olderrno = errno; /* some Xenix systems wipe out errno here */
1665 if (SvNVX(sv) == 0.0)
1666 (void)strcpy(s,"0");
1670 SET_NUMERIC_STANDARD();
1671 Gconvert(SvNVX(sv), DBL_DIG, 0, s);
1674 #ifdef FIXNEGATIVEZERO
1675 if (*s == '-' && s[1] == '0' && !s[2])
1684 else if (SvIOKp(sv)) {
1685 U32 oldIOK = SvIOK(sv);
1686 if (SvTYPE(sv) < SVt_PVIV)
1687 sv_upgrade(sv, SVt_PVIV);
1688 olderrno = errno; /* some Xenix systems wipe out errno here */
1689 sv_setpviv(sv, SvIVX(sv));
1699 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1700 warner(WARN_UNINITIALIZED, PL_warn_uninit);
1704 *lp = s - SvPVX(sv);
1707 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2pv(%s)\n",(unsigned long)sv,SvPVX(sv)));
1711 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
1712 /* Sneaky stuff here */
1716 tsv = newSVpv(tmpbuf, 0);
1732 len = strlen(tmpbuf);
1734 #ifdef FIXNEGATIVEZERO
1735 if (len == 2 && t[0] == '-' && t[1] == '0') {
1740 (void)SvUPGRADE(sv, SVt_PV);
1742 s = SvGROW(sv, len + 1);
1750 /* This function is only called on magical items */
1752 sv_2bool(register SV *sv)
1762 if (SvAMAGIC(sv) && (tmpsv = AMG_CALLun(sv,bool_)))
1763 return SvTRUE(tmpsv);
1764 return SvRV(sv) != 0;
1767 register XPV* Xpvtmp;
1768 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
1769 (*Xpvtmp->xpv_pv > '0' ||
1770 Xpvtmp->xpv_cur > 1 ||
1771 (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0')))
1778 return SvIVX(sv) != 0;
1781 return SvNVX(sv) != 0.0;
1788 /* Note: sv_setsv() should not be called with a source string that needs
1789 * to be reused, since it may destroy the source string if it is marked
1794 sv_setsv(SV *dstr, register SV *sstr)
1797 register U32 sflags;
1803 SV_CHECK_THINKFIRST(dstr);
1805 sstr = &PL_sv_undef;
1806 stype = SvTYPE(sstr);
1807 dtype = SvTYPE(dstr);
1811 /* There's a lot of redundancy below but we're going for speed here */
1816 if (dtype != SVt_PVGV) {
1817 (void)SvOK_off(dstr);
1825 sv_upgrade(dstr, SVt_IV);
1828 sv_upgrade(dstr, SVt_PVNV);
1832 sv_upgrade(dstr, SVt_PVIV);
1835 (void)SvIOK_only(dstr);
1836 SvIVX(dstr) = SvIVX(sstr);
1847 sv_upgrade(dstr, SVt_NV);
1852 sv_upgrade(dstr, SVt_PVNV);
1855 SvNVX(dstr) = SvNVX(sstr);
1856 (void)SvNOK_only(dstr);
1864 sv_upgrade(dstr, SVt_RV);
1865 else if (dtype == SVt_PVGV &&
1866 SvTYPE(SvRV(sstr)) == SVt_PVGV) {
1869 if (PL_curcop->cop_stash != GvSTASH(dstr))
1870 GvIMPORTED_on(dstr);
1880 sv_upgrade(dstr, SVt_PV);
1883 if (dtype < SVt_PVIV)
1884 sv_upgrade(dstr, SVt_PVIV);
1887 if (dtype < SVt_PVNV)
1888 sv_upgrade(dstr, SVt_PVNV);
1895 croak("Bizarre copy of %s in %s", sv_reftype(sstr, 0),
1896 PL_op_name[PL_op->op_type]);
1898 croak("Bizarre copy of %s", sv_reftype(sstr, 0));
1902 if (dtype <= SVt_PVGV) {
1904 if (dtype != SVt_PVGV) {
1905 char *name = GvNAME(sstr);
1906 STRLEN len = GvNAMELEN(sstr);
1907 sv_upgrade(dstr, SVt_PVGV);
1908 sv_magic(dstr, dstr, '*', name, len);
1909 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
1910 GvNAME(dstr) = savepvn(name, len);
1911 GvNAMELEN(dstr) = len;
1912 SvFAKE_on(dstr); /* can coerce to non-glob */
1914 /* ahem, death to those who redefine active sort subs */
1915 else if (PL_curstackinfo->si_type == PERLSI_SORT
1916 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
1917 croak("Can't redefine active sort subroutine %s",
1919 (void)SvOK_off(dstr);
1920 GvINTRO_off(dstr); /* one-shot flag */
1922 GvGP(dstr) = gp_ref(GvGP(sstr));
1924 if (PL_curcop->cop_stash != GvSTASH(dstr))
1925 GvIMPORTED_on(dstr);
1932 if (SvGMAGICAL(sstr)) {
1934 if (SvTYPE(sstr) != stype) {
1935 stype = SvTYPE(sstr);
1936 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
1940 if (stype == SVt_PVLV)
1941 (void)SvUPGRADE(dstr, SVt_PVNV);
1943 (void)SvUPGRADE(dstr, stype);
1946 sflags = SvFLAGS(sstr);
1948 if (sflags & SVf_ROK) {
1949 if (dtype >= SVt_PV) {
1950 if (dtype == SVt_PVGV) {
1951 SV *sref = SvREFCNT_inc(SvRV(sstr));
1953 int intro = GvINTRO(dstr);
1957 GvGP(dstr)->gp_refcnt--;
1958 GvINTRO_off(dstr); /* one-shot flag */
1959 Newz(602,gp, 1, GP);
1960 GvGP(dstr) = gp_ref(gp);
1961 GvSV(dstr) = NEWSV(72,0);
1962 GvLINE(dstr) = PL_curcop->cop_line;
1963 GvEGV(dstr) = (GV*)dstr;
1966 switch (SvTYPE(sref)) {
1969 SAVESPTR(GvAV(dstr));
1971 dref = (SV*)GvAV(dstr);
1972 GvAV(dstr) = (AV*)sref;
1973 if (PL_curcop->cop_stash != GvSTASH(dstr))
1974 GvIMPORTED_AV_on(dstr);
1978 SAVESPTR(GvHV(dstr));
1980 dref = (SV*)GvHV(dstr);
1981 GvHV(dstr) = (HV*)sref;
1982 if (PL_curcop->cop_stash != GvSTASH(dstr))
1983 GvIMPORTED_HV_on(dstr);
1987 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
1988 SvREFCNT_dec(GvCV(dstr));
1989 GvCV(dstr) = Nullcv;
1990 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
1991 PL_sub_generation++;
1993 SAVESPTR(GvCV(dstr));
1996 dref = (SV*)GvCV(dstr);
1997 if (GvCV(dstr) != (CV*)sref) {
1998 CV* cv = GvCV(dstr);
2000 if (!GvCVGEN((GV*)dstr) &&
2001 (CvROOT(cv) || CvXSUB(cv)))
2003 SV *const_sv = cv_const_sv(cv);
2004 bool const_changed = TRUE;
2006 const_changed = sv_cmp(const_sv,
2007 op_const_sv(CvSTART((CV*)sref),
2009 /* ahem, death to those who redefine
2010 * active sort subs */
2011 if (PL_curstackinfo->si_type == PERLSI_SORT &&
2012 PL_sortcop == CvSTART(cv))
2014 "Can't redefine active sort subroutine %s",
2015 GvENAME((GV*)dstr));
2016 if (ckWARN(WARN_REDEFINE) || (const_changed && const_sv)) {
2017 if (!(CvGV(cv) && GvSTASH(CvGV(cv))
2018 && HvNAME(GvSTASH(CvGV(cv)))
2019 && strEQ(HvNAME(GvSTASH(CvGV(cv))),
2021 warner(WARN_REDEFINE, const_sv ?
2022 "Constant subroutine %s redefined"
2023 : "Subroutine %s redefined",
2024 GvENAME((GV*)dstr));
2027 cv_ckproto(cv, (GV*)dstr,
2028 SvPOK(sref) ? SvPVX(sref) : Nullch);
2030 GvCV(dstr) = (CV*)sref;
2031 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
2032 GvASSUMECV_on(dstr);
2033 PL_sub_generation++;
2035 if (PL_curcop->cop_stash != GvSTASH(dstr))
2036 GvIMPORTED_CV_on(dstr);
2040 SAVESPTR(GvIOp(dstr));
2042 dref = (SV*)GvIOp(dstr);
2043 GvIOp(dstr) = (IO*)sref;
2047 SAVESPTR(GvSV(dstr));
2049 dref = (SV*)GvSV(dstr);
2051 if (PL_curcop->cop_stash != GvSTASH(dstr))
2052 GvIMPORTED_SV_on(dstr);
2063 (void)SvOOK_off(dstr); /* backoff */
2064 Safefree(SvPVX(dstr));
2065 SvLEN(dstr)=SvCUR(dstr)=0;
2068 (void)SvOK_off(dstr);
2069 SvRV(dstr) = SvREFCNT_inc(SvRV(sstr));
2071 if (sflags & SVp_NOK) {
2073 SvNVX(dstr) = SvNVX(sstr);
2075 if (sflags & SVp_IOK) {
2076 (void)SvIOK_on(dstr);
2077 SvIVX(dstr) = SvIVX(sstr);
2079 if (SvAMAGIC(sstr)) {
2083 else if (sflags & SVp_POK) {
2086 * Check to see if we can just swipe the string. If so, it's a
2087 * possible small lose on short strings, but a big win on long ones.
2088 * It might even be a win on short strings if SvPVX(dstr)
2089 * has to be allocated and SvPVX(sstr) has to be freed.
2092 if (SvTEMP(sstr) && /* slated for free anyway? */
2093 SvREFCNT(sstr) == 1 && /* and no other references to it? */
2094 !(sflags & SVf_OOK)) /* and not involved in OOK hack? */
2096 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
2098 SvFLAGS(dstr) &= ~SVf_OOK;
2099 Safefree(SvPVX(dstr) - SvIVX(dstr));
2102 Safefree(SvPVX(dstr));
2104 (void)SvPOK_only(dstr);
2105 SvPV_set(dstr, SvPVX(sstr));
2106 SvLEN_set(dstr, SvLEN(sstr));
2107 SvCUR_set(dstr, SvCUR(sstr));
2109 (void)SvOK_off(sstr);
2110 SvPV_set(sstr, Nullch);
2115 else { /* have to copy actual string */
2116 STRLEN len = SvCUR(sstr);
2118 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
2119 Move(SvPVX(sstr),SvPVX(dstr),len,char);
2120 SvCUR_set(dstr, len);
2121 *SvEND(dstr) = '\0';
2122 (void)SvPOK_only(dstr);
2125 if (sflags & SVp_NOK) {
2127 SvNVX(dstr) = SvNVX(sstr);
2129 if (sflags & SVp_IOK) {
2130 (void)SvIOK_on(dstr);
2131 SvIVX(dstr) = SvIVX(sstr);
2134 else if (sflags & SVp_NOK) {
2135 SvNVX(dstr) = SvNVX(sstr);
2136 (void)SvNOK_only(dstr);
2138 (void)SvIOK_on(dstr);
2139 SvIVX(dstr) = SvIVX(sstr);
2142 else if (sflags & SVp_IOK) {
2143 (void)SvIOK_only(dstr);
2144 SvIVX(dstr) = SvIVX(sstr);
2147 if (dtype == SVt_PVGV) {
2148 if (ckWARN(WARN_UNSAFE))
2149 warner(WARN_UNSAFE, "Undefined value assigned to typeglob");
2152 (void)SvOK_off(dstr);
2158 sv_setsv_mg(SV *dstr, register SV *sstr)
2160 sv_setsv(dstr,sstr);
2165 sv_setpvn(register SV *sv, register const char *ptr, register STRLEN len)
2167 register char *dptr;
2168 assert(len >= 0); /* STRLEN is probably unsigned, so this may
2169 elicit a warning, but it won't hurt. */
2170 SV_CHECK_THINKFIRST(sv);
2175 (void)SvUPGRADE(sv, SVt_PV);
2177 SvGROW(sv, len + 1);
2179 Move(ptr,dptr,len,char);
2182 (void)SvPOK_only(sv); /* validate pointer */
2187 sv_setpvn_mg(register SV *sv, register const char *ptr, register STRLEN len)
2189 sv_setpvn(sv,ptr,len);
2194 sv_setpv(register SV *sv, register const char *ptr)
2196 register STRLEN len;
2198 SV_CHECK_THINKFIRST(sv);
2204 (void)SvUPGRADE(sv, SVt_PV);
2206 SvGROW(sv, len + 1);
2207 Move(ptr,SvPVX(sv),len+1,char);
2209 (void)SvPOK_only(sv); /* validate pointer */
2214 sv_setpv_mg(register SV *sv, register const char *ptr)
2221 sv_usepvn(register SV *sv, register char *ptr, register STRLEN len)
2223 SV_CHECK_THINKFIRST(sv);
2224 (void)SvUPGRADE(sv, SVt_PV);
2229 (void)SvOOK_off(sv);
2231 Safefree(SvPVX(sv));
2232 Renew(ptr, len+1, char);
2235 SvLEN_set(sv, len+1);
2237 (void)SvPOK_only(sv); /* validate pointer */
2242 sv_usepvn_mg(register SV *sv, register char *ptr, register STRLEN len)
2244 sv_usepvn(sv,ptr,len);
2249 sv_force_normal(register SV *sv)
2251 if (SvREADONLY(sv)) {
2253 if (PL_curcop != &PL_compiling)
2254 croak(PL_no_modify);
2258 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
2263 sv_chop(register SV *sv, register char *ptr) /* like set but assuming ptr is in sv */
2267 register STRLEN delta;
2269 if (!ptr || !SvPOKp(sv))
2271 SV_CHECK_THINKFIRST(sv);
2272 if (SvTYPE(sv) < SVt_PVIV)
2273 sv_upgrade(sv,SVt_PVIV);
2277 SvFLAGS(sv) |= SVf_OOK;
2279 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK);
2280 delta = ptr - SvPVX(sv);
2288 sv_catpvn(register SV *sv, register const char *ptr, register STRLEN len)
2293 junk = SvPV_force(sv, tlen);
2294 SvGROW(sv, tlen + len + 1);
2297 Move(ptr,SvPVX(sv)+tlen,len,char);
2300 (void)SvPOK_only(sv); /* validate pointer */
2305 sv_catpvn_mg(register SV *sv, register const char *ptr, register STRLEN len)
2307 sv_catpvn(sv,ptr,len);
2312 sv_catsv(SV *dstr, register SV *sstr)
2318 if (s = SvPV(sstr, len))
2319 sv_catpvn(dstr,s,len);
2323 sv_catsv_mg(SV *dstr, register SV *sstr)
2325 sv_catsv(dstr,sstr);
2330 sv_catpv(register SV *sv, register const char *ptr)
2332 register STRLEN len;
2338 junk = SvPV_force(sv, tlen);
2340 SvGROW(sv, tlen + len + 1);
2343 Move(ptr,SvPVX(sv)+tlen,len+1,char);
2345 (void)SvPOK_only(sv); /* validate pointer */
2350 sv_catpv_mg(register SV *sv, register const char *ptr)
2363 sv_upgrade(sv, SVt_PV);
2364 SvGROW(sv, len + 1);
2369 /* name is assumed to contain an SV* if (name && namelen == HEf_SVKEY) */
2372 sv_magic(register SV *sv, SV *obj, int how, const char *name, I32 namlen)
2376 if (SvREADONLY(sv)) {
2378 if (PL_curcop != &PL_compiling && !strchr("gBf", how))
2379 croak(PL_no_modify);
2381 if (SvMAGICAL(sv) || (how == 't' && SvTYPE(sv) >= SVt_PVMG)) {
2382 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
2389 (void)SvUPGRADE(sv, SVt_PVMG);
2391 Newz(702,mg, 1, MAGIC);
2392 mg->mg_moremagic = SvMAGIC(sv);
2395 if (!obj || obj == sv || how == '#' || how == 'r')
2399 mg->mg_obj = SvREFCNT_inc(obj);
2400 mg->mg_flags |= MGf_REFCOUNTED;
2403 mg->mg_len = namlen;
2406 mg->mg_ptr = savepvn(name, namlen);
2407 else if (namlen == HEf_SVKEY)
2408 mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
2412 mg->mg_virtual = &PL_vtbl_sv;
2415 mg->mg_virtual = &PL_vtbl_amagic;
2418 mg->mg_virtual = &PL_vtbl_amagicelem;
2424 mg->mg_virtual = &PL_vtbl_bm;
2427 mg->mg_virtual = &PL_vtbl_regdata;
2430 mg->mg_virtual = &PL_vtbl_regdatum;
2433 mg->mg_virtual = &PL_vtbl_env;
2436 mg->mg_virtual = &PL_vtbl_fm;
2439 mg->mg_virtual = &PL_vtbl_envelem;
2442 mg->mg_virtual = &PL_vtbl_mglob;
2445 mg->mg_virtual = &PL_vtbl_isa;
2448 mg->mg_virtual = &PL_vtbl_isaelem;
2451 mg->mg_virtual = &PL_vtbl_nkeys;
2458 mg->mg_virtual = &PL_vtbl_dbline;
2462 mg->mg_virtual = &PL_vtbl_mutex;
2464 #endif /* USE_THREADS */
2465 #ifdef USE_LOCALE_COLLATE
2467 mg->mg_virtual = &PL_vtbl_collxfrm;
2469 #endif /* USE_LOCALE_COLLATE */
2471 mg->mg_virtual = &PL_vtbl_pack;
2475 mg->mg_virtual = &PL_vtbl_packelem;
2478 mg->mg_virtual = &PL_vtbl_regexp;
2481 mg->mg_virtual = &PL_vtbl_sig;
2484 mg->mg_virtual = &PL_vtbl_sigelem;
2487 mg->mg_virtual = &PL_vtbl_taint;
2491 mg->mg_virtual = &PL_vtbl_uvar;
2494 mg->mg_virtual = &PL_vtbl_vec;
2497 mg->mg_virtual = &PL_vtbl_substr;
2500 mg->mg_virtual = &PL_vtbl_defelem;
2503 mg->mg_virtual = &PL_vtbl_glob;
2506 mg->mg_virtual = &PL_vtbl_arylen;
2509 mg->mg_virtual = &PL_vtbl_pos;
2511 case '~': /* Reserved for use by extensions not perl internals. */
2512 /* Useful for attaching extension internal data to perl vars. */
2513 /* Note that multiple extensions may clash if magical scalars */
2514 /* etc holding private data from one are passed to another. */
2518 croak("Don't know how to handle magic of type '%c'", how);
2522 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2526 sv_unmagic(SV *sv, int type)
2530 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
2533 for (mg = *mgp; mg; mg = *mgp) {
2534 if (mg->mg_type == type) {
2535 MGVTBL* vtbl = mg->mg_virtual;
2536 *mgp = mg->mg_moremagic;
2537 if (vtbl && (vtbl->svt_free != NULL))
2538 (VTBL->svt_free)(sv, mg);
2539 if (mg->mg_ptr && mg->mg_type != 'g')
2540 if (mg->mg_len >= 0)
2541 Safefree(mg->mg_ptr);
2542 else if (mg->mg_len == HEf_SVKEY)
2543 SvREFCNT_dec((SV*)mg->mg_ptr);
2544 if (mg->mg_flags & MGf_REFCOUNTED)
2545 SvREFCNT_dec(mg->mg_obj);
2549 mgp = &mg->mg_moremagic;
2553 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
2560 sv_insert(SV *bigstr, STRLEN offset, STRLEN len, char *little, STRLEN littlelen)
2564 register char *midend;
2565 register char *bigend;
2571 croak("Can't modify non-existent substring");
2572 SvPV_force(bigstr, curlen);
2573 if (offset + len > curlen) {
2574 SvGROW(bigstr, offset+len+1);
2575 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
2576 SvCUR_set(bigstr, offset+len);
2579 i = littlelen - len;
2580 if (i > 0) { /* string might grow */
2581 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
2582 mid = big + offset + len;
2583 midend = bigend = big + SvCUR(bigstr);
2586 while (midend > mid) /* shove everything down */
2587 *--bigend = *--midend;
2588 Move(little,big+offset,littlelen,char);
2594 Move(little,SvPVX(bigstr)+offset,len,char);
2599 big = SvPVX(bigstr);
2602 bigend = big + SvCUR(bigstr);
2604 if (midend > bigend)
2605 croak("panic: sv_insert");
2607 if (mid - big > bigend - midend) { /* faster to shorten from end */
2609 Move(little, mid, littlelen,char);
2612 i = bigend - midend;
2614 Move(midend, mid, i,char);
2618 SvCUR_set(bigstr, mid - big);
2621 else if (i = mid - big) { /* faster from front */
2622 midend -= littlelen;
2624 sv_chop(bigstr,midend-i);
2629 Move(little, mid, littlelen,char);
2631 else if (littlelen) {
2632 midend -= littlelen;
2633 sv_chop(bigstr,midend);
2634 Move(little,midend,littlelen,char);
2637 sv_chop(bigstr,midend);
2642 /* make sv point to what nstr did */
2645 sv_replace(register SV *sv, register SV *nsv)
2647 U32 refcnt = SvREFCNT(sv);
2648 SV_CHECK_THINKFIRST(sv);
2649 if (SvREFCNT(nsv) != 1)
2650 warn("Reference miscount in sv_replace()");
2651 if (SvMAGICAL(sv)) {
2655 sv_upgrade(nsv, SVt_PVMG);
2656 SvMAGIC(nsv) = SvMAGIC(sv);
2657 SvFLAGS(nsv) |= SvMAGICAL(sv);
2663 assert(!SvREFCNT(sv));
2664 StructCopy(nsv,sv,SV);
2665 SvREFCNT(sv) = refcnt;
2666 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
2671 sv_clear(register SV *sv)
2675 assert(SvREFCNT(sv) == 0);
2679 if (PL_defstash) { /* Still have a symbol table? */
2684 Zero(&tmpref, 1, SV);
2685 sv_upgrade(&tmpref, SVt_RV);
2687 SvREADONLY_on(&tmpref); /* DESTROY() could be naughty */
2688 SvREFCNT(&tmpref) = 1;
2691 stash = SvSTASH(sv);
2692 destructor = gv_fetchmethod(SvSTASH(sv), "DESTROY");
2695 PUSHSTACKi(PERLSI_DESTROY);
2696 SvRV(&tmpref) = SvREFCNT_inc(sv);
2701 perl_call_sv((SV*)GvCV(destructor),
2702 G_DISCARD|G_EVAL|G_KEEPERR);
2708 } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
2710 del_XRV(SvANY(&tmpref));
2713 if (PL_in_clean_objs)
2714 croak("DESTROY created new reference to dead object '%s'",
2716 /* DESTROY gave object new lease on life */
2722 SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */
2723 SvOBJECT_off(sv); /* Curse the object. */
2724 if (SvTYPE(sv) != SVt_PVIO)
2725 --PL_sv_objcount; /* XXX Might want something more general */
2728 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2731 switch (SvTYPE(sv)) {
2734 IoIFP(sv) != PerlIO_stdin() &&
2735 IoIFP(sv) != PerlIO_stdout() &&
2736 IoIFP(sv) != PerlIO_stderr())
2741 PerlDir_close(IoDIRP(sv));
2744 Safefree(IoTOP_NAME(sv));
2745 Safefree(IoFMT_NAME(sv));
2746 Safefree(IoBOTTOM_NAME(sv));
2761 SvREFCNT_dec(LvTARG(sv));
2765 Safefree(GvNAME(sv));
2766 /* cannot decrease stash refcount yet, as we might recursively delete
2767 ourselves when the refcnt drops to zero. Delay SvREFCNT_dec
2768 of stash until current sv is completely gone.
2769 -- JohnPC, 27 Mar 1998 */
2770 stash = GvSTASH(sv);
2776 (void)SvOOK_off(sv);
2781 SvREFCNT_dec(SvRV(sv));
2782 else if (SvPVX(sv) && SvLEN(sv))
2783 Safefree(SvPVX(sv));
2793 switch (SvTYPE(sv)) {
2809 del_XPVIV(SvANY(sv));
2812 del_XPVNV(SvANY(sv));
2815 del_XPVMG(SvANY(sv));
2818 del_XPVLV(SvANY(sv));
2821 del_XPVAV(SvANY(sv));
2824 del_XPVHV(SvANY(sv));
2827 del_XPVCV(SvANY(sv));
2830 del_XPVGV(SvANY(sv));
2831 /* code duplication for increased performance. */
2832 SvFLAGS(sv) &= SVf_BREAK;
2833 SvFLAGS(sv) |= SVTYPEMASK;
2834 /* decrease refcount of the stash that owns this GV, if any */
2836 SvREFCNT_dec(stash);
2837 return; /* not break, SvFLAGS reset already happened */
2839 del_XPVBM(SvANY(sv));
2842 del_XPVFM(SvANY(sv));
2845 del_XPVIO(SvANY(sv));
2848 SvFLAGS(sv) &= SVf_BREAK;
2849 SvFLAGS(sv) |= SVTYPEMASK;
2856 ATOMIC_INC(SvREFCNT(sv));
2863 int refcount_is_zero;
2867 if (SvREFCNT(sv) == 0) {
2868 if (SvFLAGS(sv) & SVf_BREAK)
2870 if (PL_in_clean_all) /* All is fair */
2872 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
2873 /* make sure SvREFCNT(sv)==0 happens very seldom */
2874 SvREFCNT(sv) = (~(U32)0)/2;
2877 warn("Attempt to free unreferenced scalar");
2880 ATOMIC_DEC_AND_TEST(refcount_is_zero, SvREFCNT(sv));
2881 if (!refcount_is_zero)
2885 warn("Attempt to free temp prematurely: SV 0x%lx", (unsigned long)sv);
2889 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
2890 /* make sure SvREFCNT(sv)==0 happens very seldom */
2891 SvREFCNT(sv) = (~(U32)0)/2;
2900 sv_len(register SV *sv)
2909 len = mg_length(sv);
2911 junk = SvPV(sv, len);
2916 sv_len_utf8(register SV *sv)
2927 len = mg_length(sv);
2930 s = (U8*)SvPV(sv, len);
2941 sv_pos_u2b(register SV *sv, I32* offsetp, I32* lenp)
2946 I32 uoffset = *offsetp;
2952 start = s = (U8*)SvPV(sv, len);
2954 while (s < send && uoffset--)
2958 *offsetp = s - start;
2962 while (s < send && ulen--)
2972 sv_pos_b2u(register SV *sv, I32* offsetp)
2981 s = (U8*)SvPV(sv, len);
2983 croak("panic: bad byte offset");
2984 send = s + *offsetp;
2991 warn("Malformed UTF-8 character");
2999 sv_eq(register SV *str1, register SV *str2)
3011 pv1 = SvPV(str1, cur1);
3016 pv2 = SvPV(str2, cur2);
3021 return memEQ(pv1, pv2, cur1);
3025 sv_cmp(register SV *str1, register SV *str2)
3028 char *pv1 = str1 ? SvPV(str1, cur1) : (char *) NULL;
3030 char *pv2 = str2 ? SvPV(str2, cur2) : (char *) NULL;
3034 return cur2 ? -1 : 0;
3039 retval = memcmp((void*)pv1, (void*)pv2, cur1 < cur2 ? cur1 : cur2);
3042 return retval < 0 ? -1 : 1;
3047 return cur1 < cur2 ? -1 : 1;
3051 sv_cmp_locale(register SV *sv1, register SV *sv2)
3053 #ifdef USE_LOCALE_COLLATE
3059 if (PL_collation_standard)
3063 pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL;
3065 pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL;
3067 if (!pv1 || !len1) {
3078 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
3081 return retval < 0 ? -1 : 1;
3084 * When the result of collation is equality, that doesn't mean
3085 * that there are no differences -- some locales exclude some
3086 * characters from consideration. So to avoid false equalities,
3087 * we use the raw string as a tiebreaker.
3093 #endif /* USE_LOCALE_COLLATE */
3095 return sv_cmp(sv1, sv2);
3098 #ifdef USE_LOCALE_COLLATE
3100 * Any scalar variable may carry an 'o' magic that contains the
3101 * scalar data of the variable transformed to such a format that
3102 * a normal memory comparison can be used to compare the data
3103 * according to the locale settings.
3106 sv_collxfrm(SV *sv, STRLEN *nxp)
3110 mg = SvMAGICAL(sv) ? mg_find(sv, 'o') : (MAGIC *) NULL;
3111 if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
3116 Safefree(mg->mg_ptr);
3118 if ((xf = mem_collxfrm(s, len, &xlen))) {
3119 if (SvREADONLY(sv)) {
3122 return xf + sizeof(PL_collation_ix);
3125 sv_magic(sv, 0, 'o', 0, 0);
3126 mg = mg_find(sv, 'o');
3139 if (mg && mg->mg_ptr) {
3141 return mg->mg_ptr + sizeof(PL_collation_ix);
3149 #endif /* USE_LOCALE_COLLATE */
3152 sv_gets(register SV *sv, register PerlIO *fp, I32 append)
3157 register STDCHAR rslast;
3158 register STDCHAR *bp;
3162 SV_CHECK_THINKFIRST(sv);
3163 (void)SvUPGRADE(sv, SVt_PV);
3167 if (RsSNARF(PL_rs)) {
3171 else if (RsRECORD(PL_rs)) {
3172 I32 recsize, bytesread;
3175 /* Grab the size of the record we're getting */
3176 recsize = SvIV(SvRV(PL_rs));
3177 (void)SvPOK_only(sv); /* Validate pointer */
3178 buffer = SvGROW(sv, recsize + 1);
3181 /* VMS wants read instead of fread, because fread doesn't respect */
3182 /* RMS record boundaries. This is not necessarily a good thing to be */
3183 /* doing, but we've got no other real choice */
3184 bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize);
3186 bytesread = PerlIO_read(fp, buffer, recsize);
3188 SvCUR_set(sv, bytesread);
3189 buffer[bytesread] = '\0';
3190 return(SvCUR(sv) ? SvPVX(sv) : Nullch);
3192 else if (RsPARA(PL_rs)) {
3197 rsptr = SvPV(PL_rs, rslen);
3198 rslast = rslen ? rsptr[rslen - 1] : '\0';
3200 if (RsPARA(PL_rs)) { /* have to do this both before and after */
3201 do { /* to make sure file boundaries work right */
3204 i = PerlIO_getc(fp);
3208 PerlIO_ungetc(fp,i);
3214 /* See if we know enough about I/O mechanism to cheat it ! */
3216 /* This used to be #ifdef test - it is made run-time test for ease
3217 of abstracting out stdio interface. One call should be cheap
3218 enough here - and may even be a macro allowing compile
3222 if (PerlIO_fast_gets(fp)) {
3225 * We're going to steal some values from the stdio struct
3226 * and put EVERYTHING in the innermost loop into registers.
3228 register STDCHAR *ptr;
3232 #if defined(VMS) && defined(PERLIO_IS_STDIO)
3233 /* An ungetc()d char is handled separately from the regular
3234 * buffer, so we getc() it back out and stuff it in the buffer.
3236 i = PerlIO_getc(fp);
3237 if (i == EOF) return 0;
3238 *(--((*fp)->_ptr)) = (unsigned char) i;
3242 /* Here is some breathtakingly efficient cheating */
3244 cnt = PerlIO_get_cnt(fp); /* get count into register */
3245 (void)SvPOK_only(sv); /* validate pointer */
3246 if (SvLEN(sv) - append <= cnt + 1) { /* make sure we have the room */
3247 if (cnt > 80 && SvLEN(sv) > append) {
3248 shortbuffered = cnt - SvLEN(sv) + append + 1;
3249 cnt -= shortbuffered;
3253 /* remember that cnt can be negative */
3254 SvGROW(sv, append + (cnt <= 0 ? 2 : (cnt + 1)));
3259 bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */
3260 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
3261 DEBUG_P(PerlIO_printf(Perl_debug_log,
3262 "Screamer: entering, ptr=%ld, cnt=%ld\n",(long)ptr,(long)cnt));
3263 DEBUG_P(PerlIO_printf(Perl_debug_log,
3264 "Screamer: entering: FILE * thinks ptr=%ld, cnt=%ld, base=%ld\n",
3265 (long)PerlIO_get_ptr(fp), (long)PerlIO_get_cnt(fp),
3266 (long)(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
3271 while (cnt > 0) { /* this | eat */
3273 if ((*bp++ = *ptr++) == rslast) /* really | dust */
3274 goto thats_all_folks; /* screams | sed :-) */
3278 Copy(ptr, bp, cnt, char); /* this | eat */
3279 bp += cnt; /* screams | dust */
3280 ptr += cnt; /* louder | sed :-) */
3285 if (shortbuffered) { /* oh well, must extend */
3286 cnt = shortbuffered;
3288 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
3290 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
3291 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
3295 DEBUG_P(PerlIO_printf(Perl_debug_log,
3296 "Screamer: going to getc, ptr=%ld, cnt=%ld\n",(long)ptr,(long)cnt));
3297 PerlIO_set_ptrcnt(fp, ptr, cnt); /* deregisterize cnt and ptr */
3298 DEBUG_P(PerlIO_printf(Perl_debug_log,
3299 "Screamer: pre: FILE * thinks ptr=%ld, cnt=%ld, base=%ld\n",
3300 (long)PerlIO_get_ptr(fp), (long)PerlIO_get_cnt(fp),
3301 (long)(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
3302 /* This used to call 'filbuf' in stdio form, but as that behaves like
3303 getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
3304 another abstraction. */
3305 i = PerlIO_getc(fp); /* get more characters */
3306 DEBUG_P(PerlIO_printf(Perl_debug_log,
3307 "Screamer: post: FILE * thinks ptr=%ld, cnt=%ld, base=%ld\n",
3308 (long)PerlIO_get_ptr(fp), (long)PerlIO_get_cnt(fp),
3309 (long)(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
3310 cnt = PerlIO_get_cnt(fp);
3311 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
3312 DEBUG_P(PerlIO_printf(Perl_debug_log,
3313 "Screamer: after getc, ptr=%ld, cnt=%ld\n",(long)ptr,(long)cnt));
3315 if (i == EOF) /* all done for ever? */
3316 goto thats_really_all_folks;
3318 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
3320 SvGROW(sv, bpx + cnt + 2);
3321 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
3323 *bp++ = i; /* store character from PerlIO_getc */
3325 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
3326 goto thats_all_folks;
3330 if ((rslen > 1 && (bp - (STDCHAR*)SvPVX(sv) < rslen)) ||
3331 memNE((char*)bp - rslen, rsptr, rslen))
3332 goto screamer; /* go back to the fray */
3333 thats_really_all_folks:
3335 cnt += shortbuffered;
3336 DEBUG_P(PerlIO_printf(Perl_debug_log,
3337 "Screamer: quitting, ptr=%ld, cnt=%ld\n",(long)ptr,(long)cnt));
3338 PerlIO_set_ptrcnt(fp, ptr, cnt); /* put these back or we're in trouble */
3339 DEBUG_P(PerlIO_printf(Perl_debug_log,
3340 "Screamer: end: FILE * thinks ptr=%ld, cnt=%ld, base=%ld\n",
3341 (long)PerlIO_get_ptr(fp), (long)PerlIO_get_cnt(fp),
3342 (long)(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
3344 SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */
3345 DEBUG_P(PerlIO_printf(Perl_debug_log,
3346 "Screamer: done, len=%ld, string=|%.*s|\n",
3347 (long)SvCUR(sv),(int)SvCUR(sv),SvPVX(sv)));
3351 /*The big, slow, and stupid way */
3356 register STDCHAR *bpe = buf + sizeof(buf);
3358 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = i) != rslast && bp < bpe)
3359 ; /* keep reading */
3363 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
3364 /* Accomodate broken VAXC compiler, which applies U8 cast to
3365 * both args of ?: operator, causing EOF to change into 255
3367 if (cnt) { i = (U8)buf[cnt - 1]; } else { i = EOF; }
3371 sv_catpvn(sv, (char *) buf, cnt);
3373 sv_setpvn(sv, (char *) buf, cnt);
3375 if (i != EOF && /* joy */
3377 SvCUR(sv) < rslen ||
3378 memNE(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
3382 * If we're reading from a TTY and we get a short read,
3383 * indicating that the user hit his EOF character, we need
3384 * to notice it now, because if we try to read from the TTY
3385 * again, the EOF condition will disappear.
3387 * The comparison of cnt to sizeof(buf) is an optimization
3388 * that prevents unnecessary calls to feof().
3392 if (!(cnt < sizeof(buf) && PerlIO_eof(fp)))
3397 if (RsPARA(PL_rs)) { /* have to do this both before and after */
3398 while (i != EOF) { /* to make sure file boundaries work right */
3399 i = PerlIO_getc(fp);
3401 PerlIO_ungetc(fp,i);
3408 win32_strip_return(sv);
3411 return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch;
3416 sv_inc(register SV *sv)
3425 if (SvTHINKFIRST(sv)) {
3426 if (SvREADONLY(sv)) {
3428 if (PL_curcop != &PL_compiling)
3429 croak(PL_no_modify);
3433 if (SvAMAGIC(sv) && AMG_CALLun(sv,inc))
3440 flags = SvFLAGS(sv);
3441 if (flags & SVp_NOK) {
3442 (void)SvNOK_only(sv);
3446 if (flags & SVp_IOK) {
3447 if (SvIVX(sv) == IV_MAX)
3448 sv_setnv(sv, (double)IV_MAX + 1.0);
3450 (void)SvIOK_only(sv);
3455 if (!(flags & SVp_POK) || !*SvPVX(sv)) {
3456 if ((flags & SVTYPEMASK) < SVt_PVNV)
3457 sv_upgrade(sv, SVt_NV);
3459 (void)SvNOK_only(sv);
3463 while (isALPHA(*d)) d++;
3464 while (isDIGIT(*d)) d++;
3466 SET_NUMERIC_STANDARD();
3467 sv_setnv(sv,atof(SvPVX(sv)) + 1.0); /* punt */
3471 while (d >= SvPVX(sv)) {
3479 /* MKS: The original code here died if letters weren't consecutive.
3480 * at least it didn't have to worry about non-C locales. The
3481 * new code assumes that ('z'-'a')==('Z'-'A'), letters are
3482 * arranged in order (although not consecutively) and that only
3483 * [A-Za-z] are accepted by isALPHA in the C locale.
3485 if (*d != 'z' && *d != 'Z') {
3486 do { ++*d; } while (!isALPHA(*d));
3489 *(d--) -= 'z' - 'a';
3494 *(d--) -= 'z' - 'a' + 1;
3498 /* oh,oh, the number grew */
3499 SvGROW(sv, SvCUR(sv) + 2);
3501 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--)
3510 sv_dec(register SV *sv)
3518 if (SvTHINKFIRST(sv)) {
3519 if (SvREADONLY(sv)) {
3521 if (PL_curcop != &PL_compiling)
3522 croak(PL_no_modify);
3526 if (SvAMAGIC(sv) && AMG_CALLun(sv,dec))
3533 flags = SvFLAGS(sv);
3534 if (flags & SVp_NOK) {
3536 (void)SvNOK_only(sv);
3539 if (flags & SVp_IOK) {
3540 if (SvIVX(sv) == IV_MIN)
3541 sv_setnv(sv, (double)IV_MIN - 1.0);
3543 (void)SvIOK_only(sv);
3548 if (!(flags & SVp_POK)) {
3549 if ((flags & SVTYPEMASK) < SVt_PVNV)
3550 sv_upgrade(sv, SVt_NV);
3552 (void)SvNOK_only(sv);
3555 SET_NUMERIC_STANDARD();
3556 sv_setnv(sv,atof(SvPVX(sv)) - 1.0); /* punt */
3559 /* Make a string that will exist for the duration of the expression
3560 * evaluation. Actually, it may have to last longer than that, but
3561 * hopefully we won't free it until it has been assigned to a
3562 * permanent location. */
3565 sv_mortalcopy(SV *oldstr)
3571 sv_setsv(sv,oldstr);
3573 PL_tmps_stack[++PL_tmps_ix] = sv;
3585 SvFLAGS(sv) = SVs_TEMP;
3587 PL_tmps_stack[++PL_tmps_ix] = sv;
3591 /* same thing without the copying */
3594 sv_2mortal(register SV *sv)
3599 if (SvREADONLY(sv) && SvIMMORTAL(sv))
3602 PL_tmps_stack[++PL_tmps_ix] = sv;
3608 newSVpv(const char *s, STRLEN len)
3615 sv_setpvn(sv,s,len);
3620 newSVpvn(const char *s, STRLEN len)
3625 sv_setpvn(sv,s,len);
3630 newSVpvf(const char* pat, ...)
3636 va_start(args, pat);
3637 sv_vsetpvfn(sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
3664 newRV_noinc(SV *tmpRef)
3670 sv_upgrade(sv, SVt_RV);
3680 return newRV_noinc(SvREFCNT_inc(tmpRef));
3683 /* make an exact duplicate of old */
3686 newSVsv(register SV *old)
3692 if (SvTYPE(old) == SVTYPEMASK) {
3693 warn("semi-panic: attempt to dup freed string");
3708 sv_reset(register char *s, HV *stash)
3721 if (!*s) { /* reset ?? searches */
3722 for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) {
3723 pm->op_pmdynflags &= ~PMdf_USED;
3728 /* reset variables */
3730 if (!HvARRAY(stash))
3733 Zero(todo, 256, char);
3740 for ( ; i <= max; i++) {
3743 for (i = 0; i <= (I32) HvMAX(stash); i++) {
3744 for (entry = HvARRAY(stash)[i];
3746 entry = HeNEXT(entry))
3748 if (!todo[(U8)*HeKEY(entry)])
3750 gv = (GV*)HeVAL(entry);
3752 if (SvTHINKFIRST(sv)) {
3753 if (!SvREADONLY(sv) && SvROK(sv))
3758 if (SvTYPE(sv) >= SVt_PV) {
3760 if (SvPVX(sv) != Nullch)
3767 if (GvHV(gv) && !HvNAME(GvHV(gv))) {
3769 #ifndef VMS /* VMS has no environ array */
3771 environ[0] = Nullch;
3786 switch (SvTYPE(sv)) {
3794 croak("Bad filehandle: %s", GvNAME(gv));
3798 croak(PL_no_usym, "filehandle");
3800 return sv_2io(SvRV(sv));
3801 gv = gv_fetchpv(SvPV(sv,n_a), FALSE, SVt_PVIO);
3807 croak("Bad filehandle: %s", SvPV(sv,n_a));
3814 sv_2cv(SV *sv, HV **st, GV **gvp, I32 lref)
3821 return *gvp = Nullgv, Nullcv;
3822 switch (SvTYPE(sv)) {
3842 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
3843 tryAMAGICunDEREF(to_cv);
3846 if (SvTYPE(sv) == SVt_PVCV) {
3855 croak("Not a subroutine reference");
3860 gv = gv_fetchpv(SvPV(sv, n_a), lref, SVt_PVCV);
3866 if (lref && !GvCVu(gv)) {
3869 tmpsv = NEWSV(704,0);
3870 gv_efullname3(tmpsv, gv, Nullch);
3871 newSUB(start_subparse(FALSE, 0),
3872 newSVOP(OP_CONST, 0, tmpsv),
3877 croak("Unable to create sub named \"%s\"", SvPV(sv,n_a));
3884 sv_true(register SV *sv)
3891 if ((tXpv = (XPV*)SvANY(sv)) &&
3892 (*tXpv->xpv_pv > '0' ||
3893 tXpv->xpv_cur > 1 ||
3894 (tXpv->xpv_cur && *tXpv->xpv_pv != '0')))
3901 return SvIVX(sv) != 0;
3904 return SvNVX(sv) != 0.0;
3906 return sv_2bool(sv);
3912 sv_iv(register SV *sv)
3920 sv_uv(register SV *sv)
3928 sv_nv(register SV *sv)
3943 return sv_2pv(sv, &n_a);
3947 sv_pvn(SV *sv, STRLEN *lp)
3953 return sv_2pv(sv, lp);
3957 sv_pvn_force(SV *sv, STRLEN *lp)
3961 if (SvTHINKFIRST(sv) && !SvROK(sv))
3962 sv_force_normal(sv);
3968 if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) {
3970 croak("Can't coerce %s to string in %s", sv_reftype(sv,0),
3971 PL_op_name[PL_op->op_type]);
3975 if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */
3980 (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */
3981 SvGROW(sv, len + 1);
3982 Move(s,SvPVX(sv),len,char);
3987 SvPOK_on(sv); /* validate pointer */
3989 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2pv(%s)\n",
3990 (unsigned long)sv,SvPVX(sv)));
3997 sv_reftype(SV *sv, int ob)
3999 if (ob && SvOBJECT(sv))
4000 return HvNAME(SvSTASH(sv));
4002 switch (SvTYPE(sv)) {
4016 case SVt_PVLV: return "LVALUE";
4017 case SVt_PVAV: return "ARRAY";
4018 case SVt_PVHV: return "HASH";
4019 case SVt_PVCV: return "CODE";
4020 case SVt_PVGV: return "GLOB";
4021 case SVt_PVFM: return "FORMAT";
4022 default: return "UNKNOWN";
4043 sv_isa(SV *sv, const char *name)
4055 return strEQ(HvNAME(SvSTASH(sv)), name);
4059 newSVrv(SV *rv, const char *classname)
4066 SV_CHECK_THINKFIRST(rv);
4069 if (SvTYPE(rv) < SVt_RV)
4070 sv_upgrade(rv, SVt_RV);
4077 HV* stash = gv_stashpv(classname, TRUE);
4078 (void)sv_bless(rv, stash);
4084 sv_setref_pv(SV *rv, const char *classname, void *pv)
4087 sv_setsv(rv, &PL_sv_undef);
4091 sv_setiv(newSVrv(rv,classname), (IV)pv);
4096 sv_setref_iv(SV *rv, const char *classname, IV iv)
4098 sv_setiv(newSVrv(rv,classname), iv);
4103 sv_setref_nv(SV *rv, const char *classname, double nv)
4105 sv_setnv(newSVrv(rv,classname), nv);
4110 sv_setref_pvn(SV *rv, const char *classname, char *pv, STRLEN n)
4112 sv_setpvn(newSVrv(rv,classname), pv, n);
4117 sv_bless(SV *sv, HV *stash)
4122 croak("Can't bless non-reference value");
4124 if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) {
4125 if (SvREADONLY(tmpRef))
4126 croak(PL_no_modify);
4127 if (SvOBJECT(tmpRef)) {
4128 if (SvTYPE(tmpRef) != SVt_PVIO)
4130 SvREFCNT_dec(SvSTASH(tmpRef));
4133 SvOBJECT_on(tmpRef);
4134 if (SvTYPE(tmpRef) != SVt_PVIO)
4136 (void)SvUPGRADE(tmpRef, SVt_PVMG);
4137 SvSTASH(tmpRef) = (HV*)SvREFCNT_inc(stash);
4150 assert(SvTYPE(sv) == SVt_PVGV);
4155 SvREFCNT_dec(GvSTASH(sv));
4156 GvSTASH(sv) = Nullhv;
4158 sv_unmagic(sv, '*');
4159 Safefree(GvNAME(sv));
4161 SvFLAGS(sv) &= ~SVTYPEMASK;
4162 SvFLAGS(sv) |= SVt_PVMG;
4172 if (SvREFCNT(rv) != 1 || SvREADONLY(rv))
4175 sv_2mortal(rv); /* Schedule for freeing later */
4181 sv_magic((sv), Nullsv, 't', Nullch, 0);
4187 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
4188 MAGIC *mg = mg_find(sv, 't');
4197 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
4198 MAGIC *mg = mg_find(sv, 't');
4199 if (mg && ((mg->mg_len & 1) || (mg->mg_len & 2) && mg->mg_obj == sv))
4206 sv_setpviv(SV *sv, IV iv)
4209 char buf[TYPE_DIGITS(UV)];
4210 char *ptr = buf + sizeof(buf);
4215 sv_setpvn(sv, "", 0);
4224 *--ptr = '0' + (uv % 10);
4226 len = (buf + sizeof(buf)) - ptr;
4227 /* taking advantage of SvCUR(sv) == 0 */
4228 SvGROW(sv, sign + len + 1);
4232 memcpy(p, ptr, len);
4235 SvCUR(sv) = p - SvPVX(sv);
4240 sv_setpviv_mg(SV *sv, IV iv)
4247 sv_setpvf(SV *sv, const char* pat, ...)
4250 va_start(args, pat);
4251 sv_vsetpvfn(sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
4257 sv_setpvf_mg(SV *sv, const char* pat, ...)
4260 va_start(args, pat);
4261 sv_vsetpvfn(sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
4267 sv_catpvf(SV *sv, const char* pat, ...)
4270 va_start(args, pat);
4271 sv_vcatpvfn(sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
4276 sv_catpvf_mg(SV *sv, const char* pat, ...)
4279 va_start(args, pat);
4280 sv_vcatpvfn(sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
4286 sv_vsetpvfn(SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *used_locale)
4288 sv_setpvn(sv, "", 0);
4289 sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, used_locale);
4293 sv_vcatpvfn(SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *used_locale)
4301 static char nullstr[] = "(null)";
4303 /* no matter what, this is a string now */
4304 (void)SvPV_force(sv, origlen);
4306 /* special-case "", "%s", and "%_" */
4309 if (patlen == 2 && pat[0] == '%') {
4313 char *s = va_arg(*args, char*);
4314 sv_catpv(sv, s ? s : nullstr);
4316 else if (svix < svmax)
4317 sv_catsv(sv, *svargs);
4321 sv_catsv(sv, va_arg(*args, SV*));
4324 /* See comment on '_' below */
4329 patend = (char*)pat + patlen;
4330 for (p = (char*)pat; p < patend; p = q) {
4338 bool has_precis = FALSE;
4343 STRLEN esignlen = 0;
4345 char *eptr = Nullch;
4347 char ebuf[TYPE_DIGITS(int) * 2 + 16]; /* large enough for "%#.#f" */
4358 for (q = p; q < patend && *q != '%'; ++q) ;
4360 sv_catpvn(sv, p, q - p);
4398 case '1': case '2': case '3':
4399 case '4': case '5': case '6':
4400 case '7': case '8': case '9':
4403 width = width * 10 + (*q++ - '0');
4408 i = va_arg(*args, int);
4410 i = (svix < svmax) ? SvIVx(svargs[svix++]) : 0;
4412 width = (i < 0) ? -i : i;
4423 i = va_arg(*args, int);
4425 i = (svix < svmax) ? SvIVx(svargs[svix++]) : 0;
4426 precis = (i < 0) ? 0 : i;
4432 precis = precis * 10 + (*q++ - '0');
4441 #if 0 /* when quads have better support within Perl */
4442 if (*(q + 1) == 'l') {
4469 uv = va_arg(*args, int);
4471 uv = (svix < svmax) ? SvIVx(svargs[svix++]) : 0;
4473 eptr = (char*)utf8buf;
4474 elen = uv_to_utf8((U8*)eptr, uv) - utf8buf;
4478 c = va_arg(*args, int);
4480 c = (svix < svmax) ? SvIVx(svargs[svix++]) : 0;
4487 eptr = va_arg(*args, char*);
4489 elen = strlen(eptr);
4492 elen = sizeof nullstr - 1;
4495 else if (svix < svmax) {
4496 eptr = SvPVx(svargs[svix++], elen);
4498 if (has_precis && precis < elen) {
4500 sv_pos_u2b(svargs[svix - 1], &p, 0); /* sticks at end */
4503 if (width) { /* fudge width (can't fudge elen) */
4504 width += elen - sv_len_utf8(svargs[svix - 1]);
4512 * The "%_" hack might have to be changed someday,
4513 * if ISO or ANSI decide to use '_' for something.
4514 * So we keep it hidden from users' code.
4518 eptr = SvPVx(va_arg(*args, SV*), elen);
4521 if (has_precis && elen > precis)
4529 uv = (UV)va_arg(*args, void*);
4531 uv = (svix < svmax) ? (UV)svargs[svix++] : 0;
4542 case 'h': iv = (short)va_arg(*args, int); break;
4543 default: iv = va_arg(*args, int); break;
4544 case 'l': iv = va_arg(*args, long); break;
4545 case 'V': iv = va_arg(*args, IV); break;
4549 iv = (svix < svmax) ? SvIVx(svargs[svix++]) : 0;
4551 case 'h': iv = (short)iv; break;
4552 default: iv = (int)iv; break;
4553 case 'l': iv = (long)iv; break;
4560 esignbuf[esignlen++] = plus;
4564 esignbuf[esignlen++] = '-';
4594 case 'h': uv = (unsigned short)va_arg(*args, unsigned); break;
4595 default: uv = va_arg(*args, unsigned); break;
4596 case 'l': uv = va_arg(*args, unsigned long); break;
4597 case 'V': uv = va_arg(*args, UV); break;
4601 uv = (svix < svmax) ? SvUVx(svargs[svix++]) : 0;
4603 case 'h': uv = (unsigned short)uv; break;
4604 default: uv = (unsigned)uv; break;
4605 case 'l': uv = (unsigned long)uv; break;
4611 eptr = ebuf + sizeof ebuf;
4617 p = (c == 'X') ? "0123456789ABCDEF" : "0123456789abcdef";
4623 esignbuf[esignlen++] = '0';
4624 esignbuf[esignlen++] = c; /* 'x' or 'X' */
4630 *--eptr = '0' + dig;
4632 if (alt && *eptr != '0')
4638 *--eptr = '0' + dig;
4640 if (alt && *eptr != '0')
4643 default: /* it had better be ten or less */
4646 *--eptr = '0' + dig;
4647 } while (uv /= base);
4650 elen = (ebuf + sizeof ebuf) - eptr;
4653 zeros = precis - elen;
4654 else if (precis == 0 && elen == 1 && *eptr == '0')
4659 /* FLOATING POINT */
4662 c = 'f'; /* maybe %F isn't supported here */
4668 /* This is evil, but floating point is even more evil */
4671 nv = va_arg(*args, double);
4673 nv = (svix < svmax) ? SvNVx(svargs[svix++]) : 0.0;
4676 if (c != 'e' && c != 'E') {
4678 (void)frexp(nv, &i);
4679 if (i == PERL_INT_MIN)
4680 die("panic: frexp");
4682 need = BIT_DIGITS(i);
4684 need += has_precis ? precis : 6; /* known default */
4688 need += 20; /* fudge factor */
4689 if (PL_efloatsize < need) {
4690 Safefree(PL_efloatbuf);
4691 PL_efloatsize = need + 20; /* more fudge */
4692 New(906, PL_efloatbuf, PL_efloatsize, char);
4695 eptr = ebuf + sizeof ebuf;
4700 do { *--eptr = '0' + (base % 10); } while (base /= 10);
4705 do { *--eptr = '0' + (base % 10); } while (base /= 10);
4717 (void)sprintf(PL_efloatbuf, eptr, nv);
4719 eptr = PL_efloatbuf;
4720 elen = strlen(PL_efloatbuf);
4724 * User-defined locales may include arbitrary characters.
4725 * And, unfortunately, some system may alloc the "C" locale
4726 * to be overridden by a malicious user.
4729 *used_locale = TRUE;
4730 #endif /* LC_NUMERIC */
4737 i = SvCUR(sv) - origlen;
4740 case 'h': *(va_arg(*args, short*)) = i; break;
4741 default: *(va_arg(*args, int*)) = i; break;
4742 case 'l': *(va_arg(*args, long*)) = i; break;
4743 case 'V': *(va_arg(*args, IV*)) = i; break;
4746 else if (svix < svmax)
4747 sv_setuv(svargs[svix++], (UV)i);
4748 continue; /* not "break" */
4754 if (!args && ckWARN(WARN_PRINTF) &&
4755 (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) {
4756 SV *msg = sv_newmortal();
4757 sv_setpvf(msg, "Invalid conversion in %s: ",
4758 (PL_op->op_type == OP_PRTF) ? "printf" : "sprintf");
4760 sv_catpvf(msg, isPRINT(c) ? "\"%%%c\"" : "\"%%\\%03o\"",
4763 sv_catpv(msg, "end of string");
4764 warner(WARN_PRINTF, "%_", msg); /* yes, this is reentrant */
4767 /* output mangled stuff ... */
4773 /* ... right here, because formatting flags should not apply */
4774 SvGROW(sv, SvCUR(sv) + elen + 1);
4776 memcpy(p, eptr, elen);
4779 SvCUR(sv) = p - SvPVX(sv);
4780 continue; /* not "break" */
4783 have = esignlen + zeros + elen;
4784 need = (have > width ? have : width);
4787 SvGROW(sv, SvCUR(sv) + need + 1);
4789 if (esignlen && fill == '0') {
4790 for (i = 0; i < esignlen; i++)
4794 memset(p, fill, gap);
4797 if (esignlen && fill != '0') {
4798 for (i = 0; i < esignlen; i++)
4802 for (i = zeros; i; i--)
4806 memcpy(p, eptr, elen);
4810 memset(p, ' ', gap);
4814 SvCUR(sv) = p - SvPVX(sv);