3 * Copyright (c) 1991-1994, 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 */
39 #if defined(USE_STDIO_PTR) && defined(STDIO_PTR_LVALUE) && defined(STDIO_CNT_LVALUE)
43 static SV *more_sv _((void));
44 static XPVIV *more_xiv _((void));
45 static XPVNV *more_xnv _((void));
46 static XPV *more_xpv _((void));
47 static XRV *more_xrv _((void));
48 static SV *new_sv _((void));
49 static XPVIV *new_xiv _((void));
50 static XPVNV *new_xnv _((void));
51 static XPV *new_xpv _((void));
52 static XRV *new_xrv _((void));
53 static void del_xiv _((XPVIV* p));
54 static void del_xnv _((XPVNV* p));
55 static void del_xpv _((XPV* p));
56 static void del_xrv _((XRV* p));
57 static void sv_mortalgrow _((void));
59 static void sv_unglob _((SV* sv));
63 #define new_SV() sv = (SV*)safemalloc(sizeof(SV))
64 #define del_SV(p) free((char*)p)
67 sv_add_arena(ptr, size, flags)
72 if (!(flags & SVf_FAKE))
81 sv_root = (SV*)SvANY(sv); \
93 sv_root = (SV*)SvANY(sv);
105 SvANY(p) = (void *)sv_root; \
119 for (sva = sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
121 svend = &sva[SvREFCNT(sva)];
122 if (p >= sv && p < svend)
126 warn("Attempt to free non-arena SV: 0x%lx", (unsigned long)p);
130 SvANY(p) = (void *) sv_root;
136 SvANY(p) = (void *)sv_root; \
143 sv_add_arena(ptr, size, flags)
151 Zero(sva, size, char);
153 /* The first SV in an arena isn't an SV. */
154 SvANY(sva) = (void *) sv_arenaroot; /* ptr to next arena */
155 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
156 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
161 svend = &sva[SvREFCNT(sva) - 1];
164 SvANY(sv) = (void *)(SV*)(sv + 1);
165 SvFLAGS(sv) = SVTYPEMASK;
169 SvFLAGS(sv) = SVTYPEMASK;
176 sv_add_arena(nice_chunk, nice_chunk_size, 0);
180 sv_add_arena(safemalloc(1008), 1008, 0);
192 for (sva = sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
194 svend = &sva[SvREFCNT(sva)];
196 if (SvTYPE(sv) != SVTYPEMASK) {
197 fprintf(stderr, "****\n");
213 #ifndef DISABLE_DESTRUCTOR_KLUDGE
215 for (sva = sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
217 svend = &sva[SvREFCNT(sva)];
219 if (SvTYPE(gv) == SVt_PVGV && (sv = GvSV(gv)) &&
220 SvROK(sv) && SvOBJECT(rv = SvRV(sv)))
222 DEBUG_D((fprintf(stderr, "Cleaning object ref:\n "),
234 for (sva = sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
236 svend = &sva[SvREFCNT(sva)];
238 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
239 DEBUG_D((fprintf(stderr, "Cleaning object ref:\n "),
245 /* XXX Might want to check arrays, etc. */
258 for (sva = sv_arenaroot; sva; sva = (SV*) SvANY(sva)) {
260 svend = &sva[SvREFCNT(sva)];
262 if (SvTYPE(sv) != SVTYPEMASK) {
263 DEBUG_D((fprintf(stderr, "Cleaning loops:\n "), sv_dump(sv));)
264 SvFLAGS(sv) |= SVf_BREAK;
278 /* Free arenas here, but be careful about fake ones. (We assume
279 contiguity of the fake ones with the corresponding real ones.) */
281 for (sva = sv_arenaroot; sva; sva = svanext) {
282 svanext = (SV*) SvANY(sva);
283 while (svanext && SvFAKE(svanext))
284 svanext = (SV*) SvANY(svanext);
298 * See comment in more_xiv() -- RAM.
300 xiv_root = (IV**)*xiv;
301 return (XPVIV*)((char*)xiv - sizeof(XPV));
310 IV** xiv = (IV**)((char*)(p) + sizeof(XPV));
311 *xiv = (IV *)xiv_root;
319 register IV** xivend;
320 XPV* ptr = (XPV*)safemalloc(1008);
321 ptr->xpv_pv = (char*)xiv_arenaroot; /* linked list of xiv arenas */
322 xiv_arenaroot = ptr; /* to keep Purify happy */
325 xivend = &xiv[1008 / sizeof(IV *) - 1];
326 xiv += (sizeof(XPV) - 1) / sizeof(IV *) + 1; /* fudge by size of XPV */
328 while (xiv < xivend) {
329 *xiv = (IV *)(xiv + 1);
342 xnv_root = *(double**)xnv;
343 return (XPVNV*)((char*)xnv - sizeof(XPVIV));
352 double* xnv = (double*)((char*)(p) + sizeof(XPVIV));
353 *(double**)xnv = xnv_root;
360 register double* xnv;
361 register double* xnvend;
362 xnv = (double*)safemalloc(1008);
363 xnvend = &xnv[1008 / sizeof(double) - 1];
364 xnv += (sizeof(XPVIV) - 1) / sizeof(double) + 1; /* fudge by sizeof XPVIV */
366 while (xnv < xnvend) {
367 *(double**)xnv = (double*)(xnv + 1);
380 xrv_root = (XRV*)xrv->xrv_rv;
390 p->xrv_rv = (SV*)xrv_root;
398 register XRV* xrvend;
399 xrv_root = (XRV*)safemalloc(1008);
401 xrvend = &xrv[1008 / sizeof(XRV) - 1];
402 while (xrv < xrvend) {
403 xrv->xrv_rv = (SV*)(xrv + 1);
416 xpv_root = (XPV*)xpv->xpv_pv;
426 p->xpv_pv = (char*)xpv_root;
434 register XPV* xpvend;
435 xpv_root = (XPV*)safemalloc(1008);
437 xpvend = &xpv[1008 / sizeof(XPV) - 1];
438 while (xpv < xpvend) {
439 xpv->xpv_pv = (char*)(xpv + 1);
447 #define new_XIV() (void*)safemalloc(sizeof(XPVIV))
448 #define del_XIV(p) free((char*)p)
450 #define new_XIV() (void*)new_xiv()
451 #define del_XIV(p) del_xiv(p)
455 #define new_XNV() (void*)safemalloc(sizeof(XPVNV))
456 #define del_XNV(p) free((char*)p)
458 #define new_XNV() (void*)new_xnv()
459 #define del_XNV(p) del_xnv(p)
463 #define new_XRV() (void*)safemalloc(sizeof(XRV))
464 #define del_XRV(p) free((char*)p)
466 #define new_XRV() (void*)new_xrv()
467 #define del_XRV(p) del_xrv(p)
471 #define new_XPV() (void*)safemalloc(sizeof(XPV))
472 #define del_XPV(p) free((char*)p)
474 #define new_XPV() (void*)new_xpv()
475 #define del_XPV(p) del_xpv(p)
478 #define new_XPVIV() (void*)safemalloc(sizeof(XPVIV))
479 #define del_XPVIV(p) free((char*)p)
481 #define new_XPVNV() (void*)safemalloc(sizeof(XPVNV))
482 #define del_XPVNV(p) free((char*)p)
484 #define new_XPVMG() (void*)safemalloc(sizeof(XPVMG))
485 #define del_XPVMG(p) free((char*)p)
487 #define new_XPVLV() (void*)safemalloc(sizeof(XPVLV))
488 #define del_XPVLV(p) free((char*)p)
490 #define new_XPVAV() (void*)safemalloc(sizeof(XPVAV))
491 #define del_XPVAV(p) free((char*)p)
493 #define new_XPVHV() (void*)safemalloc(sizeof(XPVHV))
494 #define del_XPVHV(p) free((char*)p)
496 #define new_XPVCV() (void*)safemalloc(sizeof(XPVCV))
497 #define del_XPVCV(p) free((char*)p)
499 #define new_XPVGV() (void*)safemalloc(sizeof(XPVGV))
500 #define del_XPVGV(p) free((char*)p)
502 #define new_XPVBM() (void*)safemalloc(sizeof(XPVBM))
503 #define del_XPVBM(p) free((char*)p)
505 #define new_XPVFM() (void*)safemalloc(sizeof(XPVFM))
506 #define del_XPVFM(p) free((char*)p)
508 #define new_XPVIO() (void*)safemalloc(sizeof(XPVIO))
509 #define del_XPVIO(p) free((char*)p)
524 if (SvTYPE(sv) == mt)
530 switch (SvTYPE(sv)) {
545 nv = (double)SvIVX(sv);
551 else if (mt < SVt_PVIV)
568 pv = (char*)SvRV(sv);
572 nv = (double)(unsigned long)pv;
589 else if (mt == SVt_NV)
601 del_XPVIV(SvANY(sv));
612 del_XPVNV(SvANY(sv));
622 del_XPVMG(SvANY(sv));
625 croak("Can't upgrade that kind of scalar");
630 croak("Can't upgrade to undef");
632 SvANY(sv) = new_XIV();
636 SvANY(sv) = new_XNV();
640 SvANY(sv) = new_XRV();
644 SvANY(sv) = new_XPV();
650 SvANY(sv) = new_XPVIV();
660 SvANY(sv) = new_XPVNV();
668 SvANY(sv) = new_XPVMG();
678 SvANY(sv) = new_XPVLV();
692 SvANY(sv) = new_XPVAV();
707 SvANY(sv) = new_XPVHV();
723 SvANY(sv) = new_XPVCV();
724 Zero(SvANY(sv), 1, XPVCV);
734 SvANY(sv) = new_XPVGV();
749 SvANY(sv) = new_XPVBM();
762 SvANY(sv) = new_XPVFM();
763 Zero(SvANY(sv), 1, XPVFM);
773 SvANY(sv) = new_XPVIO();
774 Zero(SvANY(sv), 1, XPVIO);
785 SvFLAGS(sv) &= ~SVTYPEMASK;
803 else if (sv == (SV*)0x55555555 || SvTYPE(sv) == 'U') {
807 else if (sv == &sv_undef || sv == &sv_no || sv == &sv_yes) {
808 if (sv == &sv_undef) {
809 strcpy(t, "SV_UNDEF");
810 if (!(SvFLAGS(sv) & (SVf_OK|SVf_OOK|SVs_OBJECT|
811 SVs_GMG|SVs_SMG|SVs_RMG)) &&
815 else if (sv == &sv_no) {
817 if (!(SvFLAGS(sv) & (SVf_ROK|SVf_OOK|SVs_OBJECT|
818 SVs_GMG|SVs_SMG|SVs_RMG)) &&
819 !(~SvFLAGS(sv) & (SVf_POK|SVf_NOK|SVf_READONLY|
827 if (!(SvFLAGS(sv) & (SVf_ROK|SVf_OOK|SVs_OBJECT|
828 SVs_GMG|SVs_SMG|SVs_RMG)) &&
829 !(~SvFLAGS(sv) & (SVf_POK|SVf_NOK|SVf_READONLY|
832 SvPVX(sv) && *SvPVX(sv) == '1' &&
839 else if (SvREFCNT(sv) == 0) {
845 if (t - tokenbuf + unref > 10) {
846 strcpy(tokenbuf + unref + 3,"...");
852 switch (SvTYPE(sv)) {
892 sprintf(t, "CV(%s)", GvNAME(CvGV(sv)));
915 sprintf(t,"(%ld+\"%.127s\")",(long)SvIVX(sv),SvPVX(sv));
917 sprintf(t,"(\"%.127s\")",SvPVX(sv));
920 sprintf(t,"(%g)",SvNVX(sv));
922 sprintf(t,"(%ld)",(long)SvIVX(sv));
944 SvLEN(sv) += SvIVX(sv);
945 SvPVX(sv) -= SvIVX(sv);
947 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
949 SvFLAGS(sv) &= ~SVf_OOK;
959 unsigned long newlen;
965 if (newlen >= 0x10000) {
966 fprintf(stderr, "Allocation too large: %lx\n", newlen);
972 if (SvTYPE(sv) < SVt_PV) {
973 sv_upgrade(sv, SVt_PV);
976 else if (SvOOK(sv)) { /* pv is offset? */
979 if (newlen > SvLEN(sv))
980 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
984 if (newlen > SvLEN(sv)) { /* need more room? */
986 Renew(s,newlen,char);
988 New(703,s,newlen,char);
990 SvLEN_set(sv, newlen);
1000 if (SvTHINKFIRST(sv)) {
1001 if (SvREADONLY(sv) && curcop != &compiling)
1006 switch (SvTYPE(sv)) {
1008 sv_upgrade(sv, SVt_IV);
1011 sv_upgrade(sv, SVt_PVNV);
1015 sv_upgrade(sv, SVt_PVIV);
1029 croak("Can't coerce %s to integer in %s", sv_reftype(sv,0),
1030 op_name[op->op_type]);
1032 (void)SvIOK_only(sv); /* validate number */
1042 if (SvTHINKFIRST(sv)) {
1043 if (SvREADONLY(sv) && curcop != &compiling)
1048 switch (SvTYPE(sv)) {
1051 sv_upgrade(sv, SVt_NV);
1057 sv_upgrade(sv, SVt_PVNV);
1064 (void)SvOOK_off(sv);
1077 croak("Can't coerce %s to number in %s", sv_reftype(sv,0),
1078 op_name[op->op_type]);
1081 (void)SvNOK_only(sv); /* validate number */
1094 for (s = SvPVX(sv), i = 50; *s && i; s++,i--) {
1096 if (ch & 128 && !isprint(ch)) {
1116 warn("Argument \"%s\" isn't numeric in %s", tmpbuf,
1117 op_name[op->op_type]);
1119 warn("Argument \"%s\" isn't numeric", tmpbuf);
1128 if (SvGMAGICAL(sv)) {
1133 if (SvNVX(sv) < 0.0)
1134 return I_V(SvNVX(sv));
1136 return (IV) U_V(SvNVX(sv));
1138 if (SvPOKp(sv) && SvLEN(sv)) {
1139 if (dowarn && !looks_like_number(sv))
1141 return (IV)atol(SvPVX(sv));
1147 if (SvTHINKFIRST(sv)) {
1151 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv, numer)))
1152 return SvIV(tmpstr);
1153 #endif /* OVERLOAD */
1154 return (IV)SvRV(sv);
1156 if (SvREADONLY(sv)) {
1158 if (SvNVX(sv) < 0.0)
1159 return I_V(SvNVX(sv));
1161 return (IV) U_V(SvNVX(sv));
1163 if (SvPOKp(sv) && SvLEN(sv)) {
1164 if (dowarn && !looks_like_number(sv))
1166 return (IV)atol(SvPVX(sv));
1173 switch (SvTYPE(sv)) {
1175 sv_upgrade(sv, SVt_IV);
1178 sv_upgrade(sv, SVt_PVIV);
1181 sv_upgrade(sv, SVt_PVNV);
1186 if (SvNVX(sv) < 0.0)
1187 SvIVX(sv) = I_V(SvNVX(sv));
1189 SvIVX(sv) = (IV) U_V(SvNVX(sv));
1191 else if (SvPOKp(sv) && SvLEN(sv)) {
1192 if (dowarn && !looks_like_number(sv))
1195 SvIVX(sv) = (IV)atol(SvPVX(sv));
1198 if (dowarn && !localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1202 DEBUG_c(fprintf(stderr,"0x%lx 2iv(%ld)\n",
1203 (unsigned long)sv,(long)SvIVX(sv)));
1213 if (SvGMAGICAL(sv)) {
1217 if (SvPOKp(sv) && SvLEN(sv)) {
1218 if (dowarn && !SvIOKp(sv) && !looks_like_number(sv))
1220 return atof(SvPVX(sv));
1223 return (double)SvIVX(sv);
1228 if (SvTHINKFIRST(sv)) {
1232 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)))
1233 return SvNV(tmpstr);
1234 #endif /* OVERLOAD */
1235 return (double)(unsigned long)SvRV(sv);
1237 if (SvREADONLY(sv)) {
1238 if (SvPOKp(sv) && SvLEN(sv)) {
1239 if (dowarn && !SvIOKp(sv) && !looks_like_number(sv))
1241 return atof(SvPVX(sv));
1244 return (double)SvIVX(sv);
1250 if (SvTYPE(sv) < SVt_NV) {
1251 if (SvTYPE(sv) == SVt_IV)
1252 sv_upgrade(sv, SVt_PVNV);
1254 sv_upgrade(sv, SVt_NV);
1255 DEBUG_c(fprintf(stderr,"0x%lx num(%g)\n",(unsigned long)sv,SvNVX(sv)));
1257 else if (SvTYPE(sv) < SVt_PVNV)
1258 sv_upgrade(sv, SVt_PVNV);
1260 (!SvPOKp(sv) || !strchr(SvPVX(sv),'.') || !looks_like_number(sv)))
1262 SvNVX(sv) = (double)SvIVX(sv);
1264 else if (SvPOKp(sv) && SvLEN(sv)) {
1265 if (dowarn && !SvIOKp(sv) && !looks_like_number(sv))
1267 SvNVX(sv) = atof(SvPVX(sv));
1270 if (dowarn && !localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1275 DEBUG_c(fprintf(stderr,"0x%lx 2nv(%g)\n",(unsigned long)sv,SvNVX(sv)));
1291 if (SvGMAGICAL(sv)) {
1298 (void)sprintf(tokenbuf,"%ld",(long)SvIVX(sv));
1302 Gconvert(SvNVX(sv), DBL_DIG, 0, tokenbuf);
1310 if (SvTHINKFIRST(sv)) {
1314 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)))
1315 return SvPV(tmpstr,*lp);
1316 #endif /* OVERLOAD */
1321 switch (SvTYPE(sv)) {
1330 case SVt_PVMG: s = "SCALAR"; break;
1331 case SVt_PVLV: s = "LVALUE"; break;
1332 case SVt_PVAV: s = "ARRAY"; break;
1333 case SVt_PVHV: s = "HASH"; break;
1334 case SVt_PVCV: s = "CODE"; break;
1335 case SVt_PVGV: s = "GLOB"; break;
1336 case SVt_PVFM: s = "FORMATLINE"; break;
1337 case SVt_PVIO: s = "FILEHANDLE"; break;
1338 default: s = "UNKNOWN"; break;
1341 sprintf(tokenbuf, "%s=%s(0x%lx)",
1342 HvNAME(SvSTASH(sv)), s, (unsigned long)sv);
1344 sprintf(tokenbuf, "%s(0x%lx)", s, (unsigned long)sv);
1350 if (SvREADONLY(sv)) {
1352 Gconvert(SvNVX(sv), DBL_DIG, 0, tokenbuf);
1356 (void)sprintf(tokenbuf,"%ld",(long)SvIVX(sv));
1365 if (!SvUPGRADE(sv, SVt_PV))
1368 if (SvTYPE(sv) < SVt_PVNV)
1369 sv_upgrade(sv, SVt_PVNV);
1372 olderrno = errno; /* some Xenix systems wipe out errno here */
1374 if (SvNVX(sv) == 0.0)
1375 (void)strcpy(s,"0");
1378 Gconvert(SvNVX(sv), DBL_DIG, 0, s);
1380 #ifdef FIXNEGATIVEZERO
1381 if (*s == '-' && s[1] == '0' && !s[2])
1390 else if (SvIOKp(sv)) {
1391 if (SvTYPE(sv) < SVt_PVIV)
1392 sv_upgrade(sv, SVt_PVIV);
1395 olderrno = errno; /* some Xenix systems wipe out errno here */
1396 (void)sprintf(s,"%ld",(long)SvIVX(sv));
1401 if (dowarn && !localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1407 *lp = s - SvPVX(sv);
1410 DEBUG_c(fprintf(stderr,"0x%lx 2pv(%s)\n",(unsigned long)sv,SvPVX(sv)));
1414 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
1415 /* Sneaky stuff here */
1418 sv = sv_newmortal();
1419 *lp = strlen(tokenbuf);
1420 sv_setpvn(sv, tokenbuf, *lp);
1426 #ifdef FIXNEGATIVEZERO
1427 if (*tokenbuf == '-' && tokenbuf[1] == '0' && !tokenbuf[2])
1428 strcpy(tokenbuf,"0");
1430 (void)SvUPGRADE(sv, SVt_PV);
1431 len = *lp = strlen(tokenbuf);
1432 s = SvGROW(sv, len + 1);
1434 (void)strcpy(s, tokenbuf);
1435 /* NO SvPOK_on(sv) here! */
1440 /* This function is only called on magical items */
1454 if (SvAMAGIC(sv) && (tmpsv = AMG_CALLun(sv,bool_)))
1455 return SvTRUE(tmpsv);
1457 #endif /* OVERLOAD */
1458 return SvRV(sv) != 0;
1462 if ((Xpv = (XPV*)SvANY(sv)) &&
1463 (*Xpv->xpv_pv > '0' ||
1465 (Xpv->xpv_cur && *Xpv->xpv_pv != '0')))
1472 return SvIVX(sv) != 0;
1475 return SvNVX(sv) != 0.0;
1482 /* Note: sv_setsv() should not be called with a source string that needs
1483 * to be reused, since it may destroy the source string if it is marked
1492 register U32 sflags;
1498 if (SvTHINKFIRST(dstr)) {
1499 if (SvREADONLY(dstr) && curcop != &compiling)
1506 stype = SvTYPE(sstr);
1507 dtype = SvTYPE(dstr);
1509 if (dtype == SVt_PVGV && (SvFLAGS(dstr) & SVf_FAKE)) {
1510 sv_unglob(dstr); /* so fake GLOB won't perpetuate */
1511 sv_setpvn(dstr, "", 0);
1512 (void)SvPOK_only(dstr);
1513 dtype = SvTYPE(dstr);
1518 #endif /* OVERLOAD */
1519 /* There's a lot of redundancy below but we're going for speed here */
1523 (void)SvOK_off(dstr);
1526 if (dtype <= SVt_PV) {
1528 sv_upgrade(dstr, SVt_IV);
1529 else if (dtype == SVt_NV)
1530 sv_upgrade(dstr, SVt_PVNV);
1531 else if (dtype <= SVt_PV)
1532 sv_upgrade(dstr, SVt_PVIV);
1536 if (dtype <= SVt_PVIV) {
1538 sv_upgrade(dstr, SVt_NV);
1539 else if (dtype == SVt_PVIV)
1540 sv_upgrade(dstr, SVt_PVNV);
1541 else if (dtype <= SVt_PV)
1542 sv_upgrade(dstr, SVt_PVNV);
1547 sv_upgrade(dstr, SVt_RV);
1548 else if (dtype == SVt_PVGV &&
1549 SvTYPE(SvRV(sstr)) == SVt_PVGV) {
1552 if (curcop->cop_stash != GvSTASH(dstr))
1553 GvIMPORTED_on(dstr);
1562 sv_upgrade(dstr, SVt_PV);
1565 if (dtype < SVt_PVIV)
1566 sv_upgrade(dstr, SVt_PVIV);
1569 if (dtype < SVt_PVNV)
1570 sv_upgrade(dstr, SVt_PVNV);
1574 sv_upgrade(dstr, SVt_PVNV);
1582 croak("Bizarre copy of %s in %s", sv_reftype(sstr, 0),
1583 op_name[op->op_type]);
1585 croak("Bizarre copy of %s", sv_reftype(sstr, 0));
1589 if (dtype <= SVt_PVGV) {
1591 if (dtype != SVt_PVGV) {
1592 char *name = GvNAME(sstr);
1593 STRLEN len = GvNAMELEN(sstr);
1594 sv_upgrade(dstr, SVt_PVGV);
1595 sv_magic(dstr, dstr, '*', name, len);
1596 GvSTASH(dstr) = GvSTASH(sstr);
1597 GvNAME(dstr) = savepvn(name, len);
1598 GvNAMELEN(dstr) = len;
1599 SvFAKE_on(dstr); /* can coerce to non-glob */
1601 (void)SvOK_off(dstr);
1602 GvINTRO_off(dstr); /* one-shot flag */
1604 GvGP(dstr) = gp_ref(GvGP(sstr));
1606 if (curcop->cop_stash != GvSTASH(dstr))
1607 GvIMPORTED_on(dstr);
1615 sv_upgrade(dstr, stype);
1616 if (SvGMAGICAL(sstr))
1620 sflags = SvFLAGS(sstr);
1622 if (sflags & SVf_ROK) {
1623 if (dtype >= SVt_PV) {
1624 if (dtype == SVt_PVGV) {
1625 SV *sref = SvREFCNT_inc(SvRV(sstr));
1627 int intro = GvINTRO(dstr);
1631 GvGP(dstr)->gp_refcnt--;
1632 GvINTRO_off(dstr); /* one-shot flag */
1633 Newz(602,gp, 1, GP);
1636 GvSV(dstr) = NEWSV(72,0);
1637 GvLINE(dstr) = curcop->cop_line;
1641 switch (SvTYPE(sref)) {
1644 SAVESPTR(GvAV(dstr));
1646 dref = (SV*)GvAV(dstr);
1647 GvAV(dstr) = (AV*)sref;
1648 if (curcop->cop_stash != GvSTASH(dstr))
1649 GvIMPORTED_AV_on(dstr);
1653 SAVESPTR(GvHV(dstr));
1655 dref = (SV*)GvHV(dstr);
1656 GvHV(dstr) = (HV*)sref;
1657 if (curcop->cop_stash != GvSTASH(dstr))
1658 GvIMPORTED_HV_on(dstr);
1662 SAVESPTR(GvCV(dstr));
1664 CV* cv = GvCV(dstr);
1667 if (dowarn && sref != dref &&
1668 !GvCVGEN((GV*)dstr) &&
1669 (CvROOT(cv) || CvXSUB(cv)) )
1670 warn("Subroutine %s redefined",
1671 GvENAME((GV*)dstr));
1675 if (GvCV(dstr) != (CV*)sref) {
1676 GvCV(dstr) = (CV*)sref;
1677 GvASSUMECV_on(dstr);
1679 if (curcop->cop_stash != GvSTASH(dstr))
1680 GvIMPORTED_CV_on(dstr);
1684 SAVESPTR(GvIOp(dstr));
1686 dref = (SV*)GvIOp(dstr);
1687 GvIOp(dstr) = (IO*)sref;
1691 SAVESPTR(GvSV(dstr));
1693 dref = (SV*)GvSV(dstr);
1695 if (curcop->cop_stash != GvSTASH(dstr))
1696 GvIMPORTED_SV_on(dstr);
1707 Safefree(SvPVX(dstr));
1708 SvLEN(dstr)=SvCUR(dstr)=0;
1711 (void)SvOK_off(dstr);
1712 SvRV(dstr) = SvREFCNT_inc(SvRV(sstr));
1714 if (sflags & SVp_NOK) {
1716 SvNVX(dstr) = SvNVX(sstr);
1718 if (sflags & SVp_IOK) {
1719 (void)SvIOK_on(dstr);
1720 SvIVX(dstr) = SvIVX(sstr);
1723 if (SvAMAGIC(sstr)) {
1726 #endif /* OVERLOAD */
1728 else if (sflags & SVp_POK) {
1731 * Check to see if we can just swipe the string. If so, it's a
1732 * possible small lose on short strings, but a big win on long ones.
1733 * It might even be a win on short strings if SvPVX(dstr)
1734 * has to be allocated and SvPVX(sstr) has to be freed.
1737 if (SvTEMP(sstr) && /* slated for free anyway? */
1738 !(sflags & SVf_OOK)) /* and not involved in OOK hack? */
1740 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
1742 SvFLAGS(dstr) &= ~SVf_OOK;
1743 Safefree(SvPVX(dstr) - SvIVX(dstr));
1746 Safefree(SvPVX(dstr));
1748 (void)SvPOK_only(dstr);
1749 SvPV_set(dstr, SvPVX(sstr));
1750 SvLEN_set(dstr, SvLEN(sstr));
1751 SvCUR_set(dstr, SvCUR(sstr));
1753 (void)SvOK_off(sstr);
1754 SvPV_set(sstr, Nullch);
1759 else { /* have to copy actual string */
1760 STRLEN len = SvCUR(sstr);
1762 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
1763 Move(SvPVX(sstr),SvPVX(dstr),len,char);
1764 SvCUR_set(dstr, len);
1765 *SvEND(dstr) = '\0';
1766 (void)SvPOK_only(dstr);
1769 if (sflags & SVp_NOK) {
1771 SvNVX(dstr) = SvNVX(sstr);
1773 if (sflags & SVp_IOK) {
1774 (void)SvIOK_on(dstr);
1775 SvIVX(dstr) = SvIVX(sstr);
1778 else if (sflags & SVp_NOK) {
1779 SvNVX(dstr) = SvNVX(sstr);
1780 (void)SvNOK_only(dstr);
1782 (void)SvIOK_on(dstr);
1783 SvIVX(dstr) = SvIVX(sstr);
1786 else if (sflags & SVp_IOK) {
1787 (void)SvIOK_only(dstr);
1788 SvIVX(dstr) = SvIVX(sstr);
1791 (void)SvOK_off(dstr);
1797 sv_setpvn(sv,ptr,len)
1800 register STRLEN len;
1803 if (SvTHINKFIRST(sv)) {
1804 if (SvREADONLY(sv) && curcop != &compiling)
1813 if (SvTYPE(sv) >= SVt_PV) {
1814 if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
1817 else if (!sv_upgrade(sv, SVt_PV))
1819 SvGROW(sv, len + 1);
1820 Move(ptr,SvPVX(sv),len,char);
1823 (void)SvPOK_only(sv); /* validate pointer */
1832 register STRLEN len;
1834 if (SvTHINKFIRST(sv)) {
1835 if (SvREADONLY(sv) && curcop != &compiling)
1845 if (SvTYPE(sv) >= SVt_PV) {
1846 if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
1849 else if (!sv_upgrade(sv, SVt_PV))
1851 SvGROW(sv, len + 1);
1852 Move(ptr,SvPVX(sv),len+1,char);
1854 (void)SvPOK_only(sv); /* validate pointer */
1859 sv_usepvn(sv,ptr,len)
1862 register STRLEN len;
1864 if (SvTHINKFIRST(sv)) {
1865 if (SvREADONLY(sv) && curcop != &compiling)
1870 if (!SvUPGRADE(sv, SVt_PV))
1877 Safefree(SvPVX(sv));
1878 Renew(ptr, len+1, char);
1881 SvLEN_set(sv, len+1);
1883 (void)SvPOK_only(sv); /* validate pointer */
1888 sv_chop(sv,ptr) /* like set but assuming ptr is in sv */
1892 register STRLEN delta;
1894 if (!ptr || !SvPOKp(sv))
1896 if (SvTHINKFIRST(sv)) {
1897 if (SvREADONLY(sv) && curcop != &compiling)
1902 if (SvTYPE(sv) < SVt_PVIV)
1903 sv_upgrade(sv,SVt_PVIV);
1907 SvFLAGS(sv) |= SVf_OOK;
1909 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK);
1910 delta = ptr - SvPVX(sv);
1918 sv_catpvn(sv,ptr,len)
1921 register STRLEN len;
1926 junk = SvPV_force(sv, tlen);
1927 SvGROW(sv, tlen + len + 1);
1930 Move(ptr,SvPVX(sv)+tlen,len,char);
1933 (void)SvPOK_only(sv); /* validate pointer */
1946 if (s = SvPV(sstr, len))
1947 sv_catpvn(dstr,s,len);
1955 register STRLEN len;
1961 junk = SvPV_force(sv, tlen);
1963 SvGROW(sv, tlen + len + 1);
1966 Move(ptr,SvPVX(sv)+tlen,len+1,char);
1968 (void)SvPOK_only(sv); /* validate pointer */
1988 sv_upgrade(sv, SVt_PV);
1989 SvGROW(sv, len + 1);
1995 sv_magic(sv, obj, how, name, namlen)
2004 if (SvREADONLY(sv) && curcop != &compiling && !strchr("gB", how))
2006 if (SvMAGICAL(sv) || (how == 't' && SvTYPE(sv) >= SVt_PVMG)) {
2007 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
2014 if (!SvUPGRADE(sv, SVt_PVMG))
2017 Newz(702,mg, 1, MAGIC);
2018 mg->mg_moremagic = SvMAGIC(sv);
2021 if (!obj || obj == sv || how == '#')
2024 mg->mg_obj = SvREFCNT_inc(obj);
2025 mg->mg_flags |= MGf_REFCOUNTED;
2028 mg->mg_len = namlen;
2029 if (name && namlen >= 0)
2030 mg->mg_ptr = savepvn(name, namlen);
2033 mg->mg_virtual = &vtbl_sv;
2037 mg->mg_virtual = &vtbl_amagic;
2040 mg->mg_virtual = &vtbl_amagicelem;
2045 #endif /* OVERLOAD */
2047 mg->mg_virtual = &vtbl_bm;
2050 mg->mg_virtual = &vtbl_env;
2053 mg->mg_virtual = &vtbl_envelem;
2056 mg->mg_virtual = &vtbl_mglob;
2059 mg->mg_virtual = &vtbl_isa;
2062 mg->mg_virtual = &vtbl_isaelem;
2069 mg->mg_virtual = &vtbl_dbline;
2072 mg->mg_virtual = &vtbl_pack;
2076 mg->mg_virtual = &vtbl_packelem;
2079 mg->mg_virtual = &vtbl_sig;
2082 mg->mg_virtual = &vtbl_sigelem;
2085 mg->mg_virtual = &vtbl_taint;
2089 mg->mg_virtual = &vtbl_uvar;
2092 mg->mg_virtual = &vtbl_vec;
2095 mg->mg_virtual = &vtbl_substr;
2098 mg->mg_virtual = &vtbl_glob;
2101 mg->mg_virtual = &vtbl_arylen;
2104 mg->mg_virtual = &vtbl_pos;
2106 case '~': /* Reserved for use by extensions not perl internals. */
2107 /* Useful for attaching extension internal data to perl vars. */
2108 /* Note that multiple extensions may clash if magical scalars */
2109 /* etc holding private data from one are passed to another. */
2113 croak("Don't know how to handle magic of type '%c'", how);
2117 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2121 sv_unmagic(sv, type)
2127 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
2130 for (mg = *mgp; mg; mg = *mgp) {
2131 if (mg->mg_type == type) {
2132 MGVTBL* vtbl = mg->mg_virtual;
2133 *mgp = mg->mg_moremagic;
2134 if (vtbl && vtbl->svt_free)
2135 (*vtbl->svt_free)(sv, mg);
2136 if (mg->mg_ptr && mg->mg_type != 'g')
2137 Safefree(mg->mg_ptr);
2138 if (mg->mg_flags & MGf_REFCOUNTED)
2139 SvREFCNT_dec(mg->mg_obj);
2143 mgp = &mg->mg_moremagic;
2147 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
2154 sv_insert(bigstr,offset,len,little,littlelen)
2163 register char *midend;
2164 register char *bigend;
2168 croak("Can't modify non-existent substring");
2169 SvPV_force(bigstr, na);
2171 i = littlelen - len;
2172 if (i > 0) { /* string might grow */
2173 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
2174 mid = big + offset + len;
2175 midend = bigend = big + SvCUR(bigstr);
2178 while (midend > mid) /* shove everything down */
2179 *--bigend = *--midend;
2180 Move(little,big+offset,littlelen,char);
2186 Move(little,SvPVX(bigstr)+offset,len,char);
2191 big = SvPVX(bigstr);
2194 bigend = big + SvCUR(bigstr);
2196 if (midend > bigend)
2197 croak("panic: sv_insert");
2199 if (mid - big > bigend - midend) { /* faster to shorten from end */
2201 Move(little, mid, littlelen,char);
2204 i = bigend - midend;
2206 Move(midend, mid, i,char);
2210 SvCUR_set(bigstr, mid - big);
2213 else if (i = mid - big) { /* faster from front */
2214 midend -= littlelen;
2216 sv_chop(bigstr,midend-i);
2221 Move(little, mid, littlelen,char);
2223 else if (littlelen) {
2224 midend -= littlelen;
2225 sv_chop(bigstr,midend);
2226 Move(little,midend,littlelen,char);
2229 sv_chop(bigstr,midend);
2234 /* make sv point to what nstr did */
2241 U32 refcnt = SvREFCNT(sv);
2242 if (SvTHINKFIRST(sv)) {
2243 if (SvREADONLY(sv) && curcop != &compiling)
2248 if (SvREFCNT(nsv) != 1)
2249 warn("Reference miscount in sv_replace()");
2250 if (SvMAGICAL(sv)) {
2254 sv_upgrade(nsv, SVt_PVMG);
2255 SvMAGIC(nsv) = SvMAGIC(sv);
2256 SvFLAGS(nsv) |= SvMAGICAL(sv);
2262 StructCopy(nsv,sv,SV);
2263 SvREFCNT(sv) = refcnt;
2272 assert(SvREFCNT(sv) == 0);
2278 if (defstash) { /* Still have a symbol table? */
2279 destructor = gv_fetchmethod(SvSTASH(sv), "DESTROY");
2282 SAVEFREESV(SvSTASH(sv));
2283 if (destructor && GvCV(destructor)) {
2287 sv_upgrade(&ref, SVt_RV);
2288 SAVEI32(SvREFCNT(sv));
2289 SvRV(&ref) = SvREFCNT_inc(sv);
2296 perl_call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR);
2297 del_XRV(SvANY(&ref));
2302 SvREFCNT_dec(SvSTASH(sv));
2304 SvOBJECT_off(sv); /* Curse the object. */
2305 if (SvTYPE(sv) != SVt_PVIO)
2306 --sv_objcount; /* XXX Might want something more general */
2309 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2311 switch (SvTYPE(sv)) {
2314 Safefree(IoTOP_NAME(sv));
2315 Safefree(IoFMT_NAME(sv));
2316 Safefree(IoBOTTOM_NAME(sv));
2332 Safefree(GvNAME(sv));
2339 (void)SvOOK_off(sv);
2344 SvREFCNT_dec(SvRV(sv));
2346 Safefree(SvPVX(sv));
2356 switch (SvTYPE(sv)) {
2372 del_XPVIV(SvANY(sv));
2375 del_XPVNV(SvANY(sv));
2378 del_XPVMG(SvANY(sv));
2381 del_XPVLV(SvANY(sv));
2384 del_XPVAV(SvANY(sv));
2387 del_XPVHV(SvANY(sv));
2390 del_XPVCV(SvANY(sv));
2393 del_XPVGV(SvANY(sv));
2396 del_XPVBM(SvANY(sv));
2399 del_XPVFM(SvANY(sv));
2402 del_XPVIO(SvANY(sv));
2405 SvFLAGS(sv) &= SVf_BREAK;
2406 SvFLAGS(sv) |= SVTYPEMASK;
2424 if (SvREADONLY(sv)) {
2425 if (sv == &sv_undef || sv == &sv_yes || sv == &sv_no)
2428 if (SvREFCNT(sv) == 0) {
2429 if (SvFLAGS(sv) & SVf_BREAK)
2431 warn("Attempt to free unreferenced scalar");
2434 if (--SvREFCNT(sv) > 0)
2438 warn("Attempt to free temp prematurely");
2459 junk = SvPV(sv, len);
2478 pv1 = SvPV(str1, cur1);
2483 pv2 = SvPV(str2, cur2);
2488 return !bcmp(pv1, pv2, cur1);
2507 pv1 = SvPV(str1, cur1);
2514 pv2 = SvPV(str2, cur2);
2517 return cur2 ? -1 : 0;
2523 if (retval = memcmp((void*)pv1, (void*)pv2, cur1))
2524 return retval < 0 ? -1 : 1;
2529 else if (retval = memcmp((void*)pv1, (void*)pv2, cur2))
2530 return retval < 0 ? -1 : 1;
2531 else if (cur1 == cur2)
2538 sv_gets(sv,fp,append)
2545 register STDCHAR rslast;
2546 register STDCHAR *bp;
2552 * We're going to steal some values from the stdio struct
2553 * and put EVERYTHING in the innermost loop into registers.
2555 register STDCHAR *ptr;
2560 if (SvTHINKFIRST(sv)) {
2561 if (SvREADONLY(sv) && curcop != &compiling)
2566 if (!SvUPGRADE(sv, SVt_PV))
2573 else if (RsPARA(rs)) {
2578 rsptr = SvPV(rs, rslen);
2579 rslast = rslen ? rsptr[rslen - 1] : '\0';
2581 if (RsPARA(rs)) { /* have to do this both before and after */
2582 do { /* to make sure file boundaries work right */
2597 /* Here is some breathtakingly efficient cheating */
2599 cnt = FILE_cnt(fp); /* get count into register */
2600 (void)SvPOK_only(sv); /* validate pointer */
2601 if (SvLEN(sv) - append <= cnt + 1) { /* make sure we have the room */
2602 if (cnt > 80 && SvLEN(sv) > append) {
2603 shortbuffered = cnt - SvLEN(sv) + append + 1;
2604 cnt -= shortbuffered;
2608 SvGROW(sv, append+cnt+2);/* (remembering cnt can be -1) */
2613 bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */
2619 while (--cnt >= 0) { /* this | eat */
2620 if ((*bp++ = *ptr++) == rslast) /* really | dust */
2621 goto thats_all_folks; /* screams | sed :-) */
2625 memcpy((char*)bp, (char*)ptr, cnt); /* this | eat */
2626 bp += cnt; /* screams | dust */
2627 ptr += cnt; /* louder | sed :-) */
2632 if (shortbuffered) { /* oh well, must extend */
2633 cnt = shortbuffered;
2635 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
2637 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
2638 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
2642 FILE_cnt(fp) = cnt; /* deregisterize cnt and ptr */
2644 i = _filbuf(fp); /* get more characters */
2646 ptr = FILE_ptr(fp); /* reregisterize cnt and ptr */
2648 if (i == EOF) /* all done for ever? */
2649 goto thats_really_all_folks;
2651 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
2653 SvGROW(sv, bpx + cnt + 2);
2654 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
2656 *bp++ = i; /* store character from _filbuf */
2658 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
2659 goto thats_all_folks;
2663 if ((rslen > 1 && (bp - (STDCHAR*)SvPVX(sv) < rslen)) ||
2664 bcmp((char*)bp - rslen, rsptr, rslen))
2665 goto screamer; /* go back to the fray */
2666 thats_really_all_folks:
2668 cnt += shortbuffered;
2669 FILE_cnt(fp) = cnt; /* put these back or we're in trouble */
2672 SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */
2674 #else /* SV_FAST_GETS */
2676 /*The big, slow, and stupid way */
2683 register STDCHAR *bpe = buf + sizeof(buf);
2685 while ((i = getc(fp)) != EOF && (*bp++ = i) != rslast && bp < bpe)
2686 ; /* keep reading */
2690 cnt = fread((char*)buf, 1, sizeof(buf), fp);
2691 i = cnt ? (U8)buf[cnt - 1] : EOF;
2695 sv_catpvn(sv, buf, cnt);
2697 sv_setpvn(sv, buf, cnt);
2699 if (i != EOF && /* joy */
2701 SvCUR(sv) < rslen ||
2702 bcmp(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
2709 #endif /* SV_FAST_GETS */
2711 if (RsPARA(rs)) { /* have to do this both before and after */
2712 while (i != EOF) { /* to make sure file boundaries work right */
2721 return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch;
2733 if (SvTHINKFIRST(sv)) {
2734 if (SvREADONLY(sv) && curcop != &compiling)
2738 if (SvAMAGIC(sv) && AMG_CALLun(sv,inc)) return;
2739 #endif /* OVERLOAD */
2745 flags = SvFLAGS(sv);
2746 if (flags & SVp_IOK) {
2747 (void)SvIOK_only(sv);
2751 if (flags & SVp_NOK) {
2753 (void)SvNOK_only(sv);
2756 if (!(flags & SVp_POK) || !*SvPVX(sv)) {
2757 if ((flags & SVTYPEMASK) < SVt_PVNV)
2758 sv_upgrade(sv, SVt_NV);
2760 (void)SvNOK_only(sv);
2764 while (isALPHA(*d)) d++;
2765 while (isDIGIT(*d)) d++;
2767 sv_setnv(sv,atof(SvPVX(sv)) + 1.0); /* punt */
2771 while (d >= SvPVX(sv)) {
2781 *(d--) -= 'z' - 'a' + 1;
2784 /* oh,oh, the number grew */
2785 SvGROW(sv, SvCUR(sv) + 2);
2787 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--)
2803 if (SvTHINKFIRST(sv)) {
2804 if (SvREADONLY(sv) && curcop != &compiling)
2808 if (SvAMAGIC(sv) && AMG_CALLun(sv,dec)) return;
2809 #endif /* OVERLOAD */
2815 flags = SvFLAGS(sv);
2816 if (flags & SVp_IOK) {
2817 (void)SvIOK_only(sv);
2821 if (flags & SVp_NOK) {
2823 (void)SvNOK_only(sv);
2826 if (!(flags & SVp_POK)) {
2827 if ((flags & SVTYPEMASK) < SVt_PVNV)
2828 sv_upgrade(sv, SVt_NV);
2830 (void)SvNOK_only(sv);
2833 sv_setnv(sv,atof(SvPVX(sv)) - 1.0);
2836 /* Make a string that will exist for the duration of the expression
2837 * evaluation. Actually, it may have to last longer than that, but
2838 * hopefully we won't free it until it has been assigned to a
2839 * permanent location. */
2845 Renew(tmps_stack, tmps_max, SV*);
2849 sv_mortalcopy(oldstr)
2858 sv_setsv(sv,oldstr);
2859 if (++tmps_ix >= tmps_max)
2861 tmps_stack[tmps_ix] = sv;
2874 SvFLAGS(sv) = SVs_TEMP;
2875 if (++tmps_ix >= tmps_max)
2877 tmps_stack[tmps_ix] = sv;
2881 /* same thing without the copying */
2889 if (SvREADONLY(sv) && curcop != &compiling)
2891 if (++tmps_ix >= tmps_max)
2893 tmps_stack[tmps_ix] = sv;
2911 sv_setpvn(sv,s,len);
2953 sv_upgrade(sv, SVt_RV);
2955 SvRV(sv) = SvREFCNT_inc(ref);
2960 /* make an exact duplicate of old */
2970 if (SvTYPE(old) == SVTYPEMASK) {
2971 warn("semi-panic: attempt to dup freed string");
3001 if (!*s) { /* reset ?? searches */
3002 for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) {
3003 pm->op_pmflags &= ~PMf_USED;
3008 /* reset variables */
3010 if (!HvARRAY(stash))
3013 Zero(todo, 256, char);
3020 for ( ; i <= max; i++) {
3023 for (i = 0; i <= (I32) HvMAX(stash); i++) {
3024 for (entry = HvARRAY(stash)[i];
3026 entry = entry->hent_next) {
3027 if (!todo[(U8)*entry->hent_key])
3029 gv = (GV*)entry->hent_val;
3032 if (SvTYPE(sv) >= SVt_PV) {
3035 if (SvPVX(sv) != Nullch)
3042 if (HvNAME(GvHV(gv)))
3045 #ifndef VMS /* VMS has no environ array */
3047 environ[0] = Nullch;
3056 sv_2cv(sv, st, gvp, lref)
3066 return *gvp = Nullgv, Nullcv;
3067 switch (SvTYPE(sv)) {
3087 if (SvTYPE(cv) != SVt_PVCV)
3088 croak("Not a subroutine reference");
3096 gv = gv_fetchpv(SvPV(sv, na), lref, SVt_PVCV);
3102 if (lref && !GvCV(gv)) {
3105 tmpsv = NEWSV(704,0);
3106 gv_efullname(tmpsv, gv);
3107 newSUB(start_subparse(),
3108 newSVOP(OP_CONST, 0, tmpsv),
3113 croak("Unable to create sub named \"%s\"", SvPV(sv,na));
3130 if ((Xpv = (XPV*)SvANY(sv)) &&
3131 (*Xpv->xpv_pv > '0' ||
3133 (Xpv->xpv_cur && *Xpv->xpv_pv != '0')))
3140 return SvIVX(sv) != 0;
3143 return SvNVX(sv) != 0.0;
3145 return sv_2bool(sv);
3169 return (double)SvIVX(Sv);
3184 return sv_2pv(sv, lp);
3189 sv_pvn_force(sv, lp)
3195 if (SvREADONLY(sv) && curcop != &compiling)
3202 if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) {
3203 if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV) {
3209 croak("Can't coerce %s to string in %s", sv_reftype(sv,0),
3210 op_name[op->op_type]);
3214 if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */
3219 (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */
3220 SvGROW(sv, len + 1);
3221 Move(s,SvPVX(sv),len,char);
3226 SvPOK_on(sv); /* validate pointer */
3228 DEBUG_c(fprintf(stderr,"0x%lx 2pv(%s)\n",
3229 (unsigned long)sv,SvPVX(sv)));
3240 if (ob && SvOBJECT(sv))
3241 return HvNAME(SvSTASH(sv));
3243 switch (SvTYPE(sv)) {
3257 case SVt_PVLV: return "LVALUE";
3258 case SVt_PVAV: return "ARRAY";
3259 case SVt_PVHV: return "HASH";
3260 case SVt_PVCV: return "CODE";
3261 case SVt_PVGV: return "GLOB";
3262 case SVt_PVFM: return "FORMLINE";
3263 default: return "UNKNOWN";
3291 return strEQ(HvNAME(SvSTASH(sv)), name);
3295 newSVrv(rv, classname)
3305 sv_upgrade(rv, SVt_RV);
3306 SvRV(rv) = SvREFCNT_inc(sv);
3310 HV* stash = gv_stashpv(classname, TRUE);
3311 (void)sv_bless(rv, stash);
3317 sv_setref_pv(rv, classname, pv)
3323 sv_setsv(rv, &sv_undef);
3325 sv_setiv(newSVrv(rv,classname), (IV)pv);
3330 sv_setref_iv(rv, classname, iv)
3335 sv_setiv(newSVrv(rv,classname), iv);
3340 sv_setref_nv(rv, classname, nv)
3345 sv_setnv(newSVrv(rv,classname), nv);
3350 sv_setref_pvn(rv, classname, pv, n)
3356 sv_setpvn(newSVrv(rv,classname), pv, n);
3367 croak("Can't bless non-reference value");
3369 if (SvFLAGS(ref) & (SVs_OBJECT|SVf_READONLY)) {
3370 if (SvREADONLY(ref))
3372 if (SvOBJECT(ref) && SvTYPE(ref) != SVt_PVIO)
3377 (void)SvUPGRADE(ref, SVt_PVMG);
3378 SvSTASH(ref) = (HV*)SvREFCNT_inc(stash);
3382 if (Gv_AMG(stash)) {
3385 #endif /* OVERLOAD */
3394 assert(SvTYPE(sv) == SVt_PVGV);
3398 sv_unmagic(sv, '*');
3399 Safefree(GvNAME(sv));
3401 SvFLAGS(sv) &= ~SVTYPEMASK;
3402 SvFLAGS(sv) |= SVt_PVMG;
3413 if (SvREFCNT(rv) != 1 || SvREADONLY(rv))
3416 sv_2mortal(rv); /* Schedule for freeing later */
3430 fprintf(stderr, "SV = 0\n");
3434 flags = SvFLAGS(sv);
3437 sprintf(d, "(0x%lx)\n REFCNT = %ld\n FLAGS = (",
3438 (unsigned long)SvANY(sv), (long)SvREFCNT(sv));
3440 if (flags & SVs_PADBUSY) strcat(d, "PADBUSY,");
3441 if (flags & SVs_PADTMP) strcat(d, "PADTMP,");
3442 if (flags & SVs_PADMY) strcat(d, "PADMY,");
3443 if (flags & SVs_TEMP) strcat(d, "TEMP,");
3444 if (flags & SVs_OBJECT) strcat(d, "OBJECT,");
3445 if (flags & SVs_GMG) strcat(d, "GMG,");
3446 if (flags & SVs_SMG) strcat(d, "SMG,");
3447 if (flags & SVs_RMG) strcat(d, "RMG,");
3450 if (flags & SVf_IOK) strcat(d, "IOK,");
3451 if (flags & SVf_NOK) strcat(d, "NOK,");
3452 if (flags & SVf_POK) strcat(d, "POK,");
3453 if (flags & SVf_ROK) strcat(d, "ROK,");
3454 if (flags & SVf_OOK) strcat(d, "OOK,");
3455 if (flags & SVf_FAKE) strcat(d, "FAKE,");
3456 if (flags & SVf_READONLY) strcat(d, "READONLY,");
3459 if (flags & SVp_IOK) strcat(d, "pIOK,");
3460 if (flags & SVp_NOK) strcat(d, "pNOK,");
3461 if (flags & SVp_POK) strcat(d, "pPOK,");
3462 if (flags & SVp_SCREAM) strcat(d, "SCREAM,");
3469 fprintf(stderr, "SV = ");
3472 fprintf(stderr,"NULL%s\n", tmpbuf);
3475 fprintf(stderr,"IV%s\n", tmpbuf);
3478 fprintf(stderr,"NV%s\n", tmpbuf);
3481 fprintf(stderr,"RV%s\n", tmpbuf);
3484 fprintf(stderr,"PV%s\n", tmpbuf);
3487 fprintf(stderr,"PVIV%s\n", tmpbuf);
3490 fprintf(stderr,"PVNV%s\n", tmpbuf);
3493 fprintf(stderr,"PVBM%s\n", tmpbuf);
3496 fprintf(stderr,"PVMG%s\n", tmpbuf);
3499 fprintf(stderr,"PVLV%s\n", tmpbuf);
3502 fprintf(stderr,"PVAV%s\n", tmpbuf);
3505 fprintf(stderr,"PVHV%s\n", tmpbuf);
3508 fprintf(stderr,"PVCV%s\n", tmpbuf);
3511 fprintf(stderr,"PVGV%s\n", tmpbuf);
3514 fprintf(stderr,"PVFM%s\n", tmpbuf);
3517 fprintf(stderr,"PVIO%s\n", tmpbuf);
3520 fprintf(stderr,"UNKNOWN%s\n", tmpbuf);
3523 if (type >= SVt_PVIV || type == SVt_IV)
3524 fprintf(stderr, " IV = %ld\n", (long)SvIVX(sv));
3525 if (type >= SVt_PVNV || type == SVt_NV)
3526 fprintf(stderr, " NV = %.*g\n", DBL_DIG, SvNVX(sv));
3528 fprintf(stderr, " RV = 0x%lx\n", (long)SvRV(sv));
3534 if (type <= SVt_PVLV) {
3536 fprintf(stderr, " PV = 0x%lx \"%s\"\n CUR = %ld\n LEN = %ld\n",
3537 (long)SvPVX(sv), SvPVX(sv), (long)SvCUR(sv), (long)SvLEN(sv));
3539 fprintf(stderr, " PV = 0\n");
3541 if (type >= SVt_PVMG) {
3543 fprintf(stderr, " MAGIC = 0x%lx\n", (long)SvMAGIC(sv));
3546 fprintf(stderr, " STASH = %s\n", HvNAME(SvSTASH(sv)));
3550 fprintf(stderr, " TYPE = %c\n", LvTYPE(sv));
3551 fprintf(stderr, " TARGOFF = %ld\n", (long)LvTARGOFF(sv));
3552 fprintf(stderr, " TARGLEN = %ld\n", (long)LvTARGLEN(sv));
3553 fprintf(stderr, " TARG = 0x%lx\n", (long)LvTARG(sv));
3554 sv_dump(LvTARG(sv));
3557 fprintf(stderr, " ARRAY = 0x%lx\n", (long)AvARRAY(sv));
3558 fprintf(stderr, " ALLOC = 0x%lx\n", (long)AvALLOC(sv));
3559 fprintf(stderr, " FILL = %ld\n", (long)AvFILL(sv));
3560 fprintf(stderr, " MAX = %ld\n", (long)AvMAX(sv));
3561 fprintf(stderr, " ARYLEN = 0x%lx\n", (long)AvARYLEN(sv));
3562 flags = AvFLAGS(sv);
3564 if (flags & AVf_REAL) strcat(d, "REAL,");
3565 if (flags & AVf_REIFY) strcat(d, "REIFY,");
3566 if (flags & AVf_REUSED) strcat(d, "REUSED,");
3568 d[strlen(d)-1] = '\0';
3569 fprintf(stderr, " FLAGS = (%s)\n", d);
3572 fprintf(stderr, " ARRAY = 0x%lx\n",(long)HvARRAY(sv));
3573 fprintf(stderr, " KEYS = %ld\n", (long)HvKEYS(sv));
3574 fprintf(stderr, " FILL = %ld\n", (long)HvFILL(sv));
3575 fprintf(stderr, " MAX = %ld\n", (long)HvMAX(sv));
3576 fprintf(stderr, " RITER = %ld\n", (long)HvRITER(sv));
3577 fprintf(stderr, " EITER = 0x%lx\n",(long) HvEITER(sv));
3579 fprintf(stderr, " PMROOT = 0x%lx\n",(long)HvPMROOT(sv));
3581 fprintf(stderr, " NAME = \"%s\"\n", HvNAME(sv));
3585 fprintf(stderr, " STASH = 0x%lx\n", (long)CvSTASH(sv));
3586 fprintf(stderr, " START = 0x%lx\n", (long)CvSTART(sv));
3587 fprintf(stderr, " ROOT = 0x%lx\n", (long)CvROOT(sv));
3588 fprintf(stderr, " XSUB = 0x%lx\n", (long)CvXSUB(sv));
3589 fprintf(stderr, " XSUBANY = %ld\n", (long)CvXSUBANY(sv).any_i32);
3590 fprintf(stderr, " FILEGV = 0x%lx\n", (long)CvFILEGV(sv));
3591 fprintf(stderr, " DEPTH = %ld\n", (long)CvDEPTH(sv));
3592 fprintf(stderr, " PADLIST = 0x%lx\n", (long)CvPADLIST(sv));
3593 fprintf(stderr, " OUTSIDE = 0x%lx\n", (long)CvOUTSIDE(sv));
3594 if (type == SVt_PVFM)
3595 fprintf(stderr, " LINES = %ld\n", (long)FmLINES(sv));
3598 fprintf(stderr, " NAME = %s\n", GvNAME(sv));
3599 fprintf(stderr, " NAMELEN = %ld\n", (long)GvNAMELEN(sv));
3600 fprintf(stderr, " STASH = 0x%lx\n", (long)GvSTASH(sv));
3601 fprintf(stderr, " GP = 0x%lx\n", (long)GvGP(sv));
3602 fprintf(stderr, " SV = 0x%lx\n", (long)GvSV(sv));
3603 fprintf(stderr, " REFCNT = %ld\n", (long)GvREFCNT(sv));
3604 fprintf(stderr, " IO = 0x%lx\n", (long)GvIOp(sv));
3605 fprintf(stderr, " FORM = 0x%lx\n", (long)GvFORM(sv));
3606 fprintf(stderr, " AV = 0x%lx\n", (long)GvAV(sv));
3607 fprintf(stderr, " HV = 0x%lx\n", (long)GvHV(sv));
3608 fprintf(stderr, " CV = 0x%lx\n", (long)GvCV(sv));
3609 fprintf(stderr, " CVGEN = 0x%lx\n", (long)GvCVGEN(sv));
3610 fprintf(stderr, " LASTEXPR = %ld\n", (long)GvLASTEXPR(sv));
3611 fprintf(stderr, " LINE = %ld\n", (long)GvLINE(sv));
3612 fprintf(stderr, " FLAGS = 0x%x\n", (int)GvFLAGS(sv));
3613 fprintf(stderr, " STASH = 0x%lx\n", (long)GvSTASH(sv));
3614 fprintf(stderr, " EGV = 0x%lx\n", (long)GvEGV(sv));
3617 fprintf(stderr, " IFP = 0x%lx\n", (long)IoIFP(sv));
3618 fprintf(stderr, " OFP = 0x%lx\n", (long)IoOFP(sv));
3619 fprintf(stderr, " DIRP = 0x%lx\n", (long)IoDIRP(sv));
3620 fprintf(stderr, " LINES = %ld\n", (long)IoLINES(sv));
3621 fprintf(stderr, " PAGE = %ld\n", (long)IoPAGE(sv));
3622 fprintf(stderr, " PAGE_LEN = %ld\n", (long)IoPAGE_LEN(sv));
3623 fprintf(stderr, " LINES_LEFT = %ld\n", (long)IoLINES_LEFT(sv));
3624 fprintf(stderr, " TOP_NAME = %s\n", IoTOP_NAME(sv));
3625 fprintf(stderr, " TOP_GV = 0x%lx\n", (long)IoTOP_GV(sv));
3626 fprintf(stderr, " FMT_NAME = %s\n", IoFMT_NAME(sv));
3627 fprintf(stderr, " FMT_GV = 0x%lx\n", (long)IoFMT_GV(sv));
3628 fprintf(stderr, " BOTTOM_NAME = %s\n", IoBOTTOM_NAME(sv));
3629 fprintf(stderr, " BOTTOM_GV = 0x%lx\n", (long)IoBOTTOM_GV(sv));
3630 fprintf(stderr, " SUBPROCESS = %ld\n", (long)IoSUBPROCESS(sv));
3631 fprintf(stderr, " TYPE = %c\n", IoTYPE(sv));
3632 fprintf(stderr, " FLAGS = 0x%lx\n", (long)IoFLAGS(sv));
3651 switch (SvTYPE(sv)) {
3659 croak("Bad filehandle: %s", GvNAME(gv));
3663 croak(no_usym, "filehandle");
3665 return sv_2io(SvRV(sv));
3666 gv = gv_fetchpv(SvPV(sv,na), FALSE, SVt_PVIO);
3672 croak("Bad filehandle: %s", SvPV(sv,na));