This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: Namespace cleanup: Does SDBM need binary compatibility?
[perl5.git] / sv.c
<
CommitLineData
a0d0e21e 1/* sv.c
79072805 2 *
a0d0e21e 3 * Copyright (c) 1991-1994, Larry Wall
79072805
LW
4 *
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.
7 *
a0d0e21e
LW
8 */
9
10/*
11 * "I wonder what the Entish is for 'yes' and 'no'," he thought.
79072805
LW
12 */
13
14#include "EXTERN.h"
15#include "perl.h"
79072805 16
c07a80fd
PP
17#ifdef OVR_DBL_DIG
18/* Use an overridden DBL_DIG */
19# ifdef DBL_DIG
20# undef DBL_DIG
21# endif
22# define DBL_DIG OVR_DBL_DIG
23#else
a0d0e21e
LW
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.
26 (see config.h)
27*/
28#ifdef I_LIMITS
29#include <limits.h>
30#endif
31#ifdef I_FLOAT
32#include <float.h>
33#endif
34#ifndef HAS_DBL_DIG
35#define DBL_DIG 15 /* A guess that works lots of places */
36#endif
c07a80fd
PP
37#endif
38
1edc1566 39#if defined(USE_STDIO_PTR) && defined(STDIO_PTR_LVALUE) && defined(STDIO_CNT_LVALUE) && !defined(__QNX__)
c07a80fd
PP
40# define FAST_SV_GETS
41#endif
a0d0e21e
LW
42
43static SV *more_sv _((void));
44static XPVIV *more_xiv _((void));
45static XPVNV *more_xnv _((void));
46static XPV *more_xpv _((void));
47static XRV *more_xrv _((void));
a0d0e21e
LW
48static XPVIV *new_xiv _((void));
49static XPVNV *new_xnv _((void));
50static XPV *new_xpv _((void));
51static XRV *new_xrv _((void));
52static void del_xiv _((XPVIV* p));
53static void del_xnv _((XPVNV* p));
54static void del_xpv _((XPV* p));
55static void del_xrv _((XRV* p));
56static void sv_mortalgrow _((void));
a0d0e21e
LW
57static void sv_unglob _((SV* sv));
58
4561caa4
CS
59typedef void (*SVFUNC) _((SV*));
60
a0d0e21e 61#ifdef PURIFY
79072805 62
4561caa4
CS
63#define new_SV(p) \
64 do { \
65 (p) = (SV*)safemalloc(sizeof(SV)); \
66 reg_add(p); \
67 } while (0)
68
69#define del_SV(p) \
70 do { \
71 reg_remove(p); \
72 free((char*)(p)); \
73 } while (0)
74
75static SV **registry;
76static I32 regsize;
77
78#define REGHASH(sv,size) ((((U32)(sv)) >> 2) % (size))
79
80#define REG_REPLACE(sv,a,b) \
81 do { \
82 void* p = sv->sv_any; \
83 I32 h = REGHASH(sv, regsize); \
84 I32 i = h; \
85 while (registry[i] != (a)) { \
86 if (++i >= regsize) \
87 i = 0; \
88 if (i == h) \
89 die("SV registry bug"); \
90 } \
91 registry[i] = (b); \
92 } while (0)
93
94#define REG_ADD(sv) REG_REPLACE(sv,Nullsv,sv)
95#define REG_REMOVE(sv) REG_REPLACE(sv,sv,Nullsv)
96
97static void
98reg_add(sv)
99SV* sv;
100{
101 if (sv_count >= (regsize >> 1))
102 {
103 SV **oldreg = registry;
104 I32 oldsize = regsize;
105
106 regsize = regsize ? ((regsize << 2) + 1) : 2037;
107 registry = (SV**)safemalloc(regsize * sizeof(SV*));
108 memzero(registry, regsize * sizeof(SV*));
109
110 if (oldreg) {
111 I32 i;
112
113 for (i = 0; i < oldsize; ++i) {
114 SV* oldsv = oldreg[i];
115 if (oldsv)
116 REG_ADD(oldsv);
117 }
118 Safefree(oldreg);
119 }
120 }
121
122 REG_ADD(sv);
123 ++sv_count;
124}
125
126static void
127reg_remove(sv)
128SV* sv;
129{
130 REG_REMOVE(sv);
131 --sv_count;
132}
133
134static void
135visit(f)
136SVFUNC f;
137{
138 I32 i;
139
140 for (i = 0; i < regsize; ++i) {
141 SV* sv = registry[i];
142 if (sv)
143 (*f)(sv);
144 }
145}
a0d0e21e 146
4633a7c4
LW
147void
148sv_add_arena(ptr, size, flags)
149char* ptr;
150U32 size;
151U32 flags;
152{
153 if (!(flags & SVf_FAKE))
154 free(ptr);
155}
156
4561caa4
CS
157#else /* ! PURIFY */
158
159/*
160 * "A time to plant, and a time to uproot what was planted..."
161 */
162
163#define plant_SV(p) \
164 do { \
165 SvANY(p) = (void *)sv_root; \
166 SvFLAGS(p) = SVTYPEMASK; \
167 sv_root = (p); \
168 --sv_count; \
169 } while (0)
a0d0e21e 170
4561caa4
CS
171#define uproot_SV(p) \
172 do { \
173 (p) = sv_root; \
174 sv_root = (SV*)SvANY(p); \
a0d0e21e 175 ++sv_count; \
4561caa4 176 } while (0)
463ee0b2 177
4561caa4
CS
178#define new_SV(p) \
179 if (sv_root) \
180 uproot_SV(p); \
181 else \
182 (p) = more_sv()
463ee0b2 183
a0d0e21e 184#ifdef DEBUGGING
4561caa4 185
a0d0e21e
LW
186#define del_SV(p) \
187 if (debug & 32768) \
188 del_sv(p); \
4561caa4
CS
189 else \
190 plant_SV(p)
a0d0e21e 191
463ee0b2
LW
192static void
193del_sv(p)
194SV* p;
195{
a0d0e21e 196 if (debug & 32768) {
4633a7c4 197 SV* sva;
a0d0e21e
LW
198 SV* sv;
199 SV* svend;
200 int ok = 0;
4633a7c4
LW
201 for (sva = sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
202 sv = sva + 1;
203 svend = &sva[SvREFCNT(sva)];
a0d0e21e
LW
204 if (p >= sv && p < svend)
205 ok = 1;
206 }
207 if (!ok) {
208 warn("Attempt to free non-arena SV: 0x%lx", (unsigned long)p);
209 return;
210 }
211 }
4561caa4 212 plant_SV(p);
463ee0b2 213}
a0d0e21e 214
4561caa4
CS
215#else /* ! DEBUGGING */
216
217#define del_SV(p) plant_SV(p)
218
219#endif /* DEBUGGING */
463ee0b2 220
4633a7c4
LW
221void
222sv_add_arena(ptr, size, flags)
223char* ptr;
224U32 size;
225U32 flags;
463ee0b2 226{
4633a7c4 227 SV* sva = (SV*)ptr;
463ee0b2
LW
228 register SV* sv;
229 register SV* svend;
4633a7c4
LW
230 Zero(sva, size, char);
231
232 /* The first SV in an arena isn't an SV. */
233 SvANY(sva) = (void *) sv_arenaroot; /* ptr to next arena */
234 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
235 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
236
237 sv_arenaroot = sva;
238 sv_root = sva + 1;
239
240 svend = &sva[SvREFCNT(sva) - 1];
241 sv = sva + 1;
463ee0b2 242 while (sv < svend) {
a0d0e21e 243 SvANY(sv) = (void *)(SV*)(sv + 1);
8990e307 244 SvFLAGS(sv) = SVTYPEMASK;
463ee0b2
LW
245 sv++;
246 }
247 SvANY(sv) = 0;
4633a7c4
LW
248 SvFLAGS(sv) = SVTYPEMASK;
249}
250
251static SV*
252more_sv()
253{
4561caa4
CS
254 register SV* sv;
255
c07a80fd
PP
256 if (nice_chunk) {
257 sv_add_arena(nice_chunk, nice_chunk_size, 0);
258 nice_chunk = Nullch;
259 }
1edc1566
PP
260 else {
261 char *chunk; /* must use New here to match call to */
262 New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */
263 sv_add_arena(chunk, 1008, 0);
264 }
4561caa4
CS
265 uproot_SV(sv);
266 return sv;
463ee0b2
LW
267}
268
4561caa4
CS
269static void
270visit(f)
271SVFUNC f;
8990e307 272{
4633a7c4 273 SV* sva;
8990e307
LW
274 SV* sv;
275 register SV* svend;
276
4561caa4 277 for (sva = sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
4633a7c4 278 svend = &sva[SvREFCNT(sva)];
4561caa4
CS
279 for (sv = sva + 1; sv < svend; ++sv) {
280 if (SvTYPE(sv) != SVTYPEMASK)
281 (*f)(sv);
8990e307
LW
282 }
283 }
284}
285
4561caa4
CS
286#endif /* PURIFY */
287
288static void
289do_report_used(sv)
290SV* sv;
291{
292 if (SvTYPE(sv) != SVTYPEMASK) {
d1bf51dd 293 /* XXX Perhaps this ought to go to Perl_debug_log, if DEBUGGING. */
4561caa4
CS
294 PerlIO_printf(PerlIO_stderr(), "****\n");
295 sv_dump(sv);
296 }
297}
298
8990e307 299void
4561caa4
CS
300sv_report_used()
301{
302 visit(do_report_used);
303}
304
305static void
306do_clean_objs(sv)
307SV* sv;
8990e307 308{
a0d0e21e 309 SV* rv;
8990e307 310
4561caa4 311 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
d1bf51dd 312 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv));)
4561caa4
CS
313 SvROK_off(sv);
314 SvRV(sv) = 0;
315 SvREFCNT_dec(rv);
a5f75d66 316 }
4561caa4
CS
317
318 /* XXX Might want to check arrays, etc. */
319}
320
321#ifndef DISABLE_DESTRUCTOR_KLUDGE
322static void
323do_clean_named_objs(sv)
324SV* sv;
325{
326 if (SvTYPE(sv) == SVt_PVGV && GvSV(sv))
327 do_clean_objs(GvSV(sv));
328}
a5f75d66 329#endif
4561caa4
CS
330
331void
332sv_clean_objs()
333{
334#ifndef DISABLE_DESTRUCTOR_KLUDGE
335 visit(do_clean_named_objs);
336#endif
337 visit(do_clean_objs);
338}
339
340static void
341do_clean_all(sv)
342SV* sv;
343{
d1bf51dd 344 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops:\n "), sv_dump(sv));)
4561caa4
CS
345 SvFLAGS(sv) |= SVf_BREAK;
346 SvREFCNT_dec(sv);
8990e307
LW
347}
348
1edc1566
PP
349static int in_clean_all = 0;
350
8990e307 351void
8990e307
LW
352sv_clean_all()
353{
1edc1566 354 in_clean_all = 1;
4561caa4 355 visit(do_clean_all);
1edc1566 356 in_clean_all = 0;
8990e307 357}
463ee0b2 358
4633a7c4
LW
359void
360sv_free_arenas()
361{
362 SV* sva;
363 SV* svanext;
364
365 /* Free arenas here, but be careful about fake ones. (We assume
366 contiguity of the fake ones with the corresponding real ones.) */
367
368 for (sva = sv_arenaroot; sva; sva = svanext) {
369 svanext = (SV*) SvANY(sva);
370 while (svanext && SvFAKE(svanext))
371 svanext = (SV*) SvANY(svanext);
372
373 if (!SvFAKE(sva))
1edc1566 374 Safefree((void *)sva);
4633a7c4
LW
375 }
376}
377
463ee0b2
LW
378static XPVIV*
379new_xiv()
380{
a0d0e21e 381 IV** xiv;
463ee0b2
LW
382 if (xiv_root) {
383 xiv = xiv_root;
85e6fe83
LW
384 /*
385 * See comment in more_xiv() -- RAM.
386 */
a0d0e21e 387 xiv_root = (IV**)*xiv;
463ee0b2
LW
388 return (XPVIV*)((char*)xiv - sizeof(XPV));
389 }
390 return more_xiv();
391}
392
393static void
394del_xiv(p)
395XPVIV* p;
396{
a0d0e21e
LW
397 IV** xiv = (IV**)((char*)(p) + sizeof(XPV));
398 *xiv = (IV *)xiv_root;
463ee0b2
LW
399 xiv_root = xiv;
400}
401
402static XPVIV*
403more_xiv()
404{
a0d0e21e
LW
405 register IV** xiv;
406 register IV** xivend;
407 XPV* ptr = (XPV*)safemalloc(1008);
408 ptr->xpv_pv = (char*)xiv_arenaroot; /* linked list of xiv arenas */
409 xiv_arenaroot = ptr; /* to keep Purify happy */
410
411 xiv = (IV**) ptr;
412 xivend = &xiv[1008 / sizeof(IV *) - 1];
413 xiv += (sizeof(XPV) - 1) / sizeof(IV *) + 1; /* fudge by size of XPV */
463ee0b2
LW
414 xiv_root = xiv;
415 while (xiv < xivend) {
a0d0e21e 416 *xiv = (IV *)(xiv + 1);
463ee0b2
LW
417 xiv++;
418 }
85e6fe83 419 *xiv = 0;
463ee0b2
LW
420 return new_xiv();
421}
422
463ee0b2
LW
423static XPVNV*
424new_xnv()
425{
426 double* xnv;
427 if (xnv_root) {
428 xnv = xnv_root;
429 xnv_root = *(double**)xnv;
430 return (XPVNV*)((char*)xnv - sizeof(XPVIV));
431 }
432 return more_xnv();
433}
434
435static void
436del_xnv(p)
437XPVNV* p;
438{
439 double* xnv = (double*)((char*)(p) + sizeof(XPVIV));
440 *(double**)xnv = xnv_root;
441 xnv_root = xnv;
442}
443
444static XPVNV*
445more_xnv()
446{
463ee0b2
LW
447 register double* xnv;
448 register double* xnvend;
8990e307 449 xnv = (double*)safemalloc(1008);
463ee0b2
LW
450 xnvend = &xnv[1008 / sizeof(double) - 1];
451 xnv += (sizeof(XPVIV) - 1) / sizeof(double) + 1; /* fudge by sizeof XPVIV */
452 xnv_root = xnv;
453 while (xnv < xnvend) {
454 *(double**)xnv = (double*)(xnv + 1);
455 xnv++;
456 }
457 *(double**)xnv = 0;
458 return new_xnv();
459}
460
ed6116ce
LW
461static XRV*
462new_xrv()
463{
464 XRV* xrv;
465 if (xrv_root) {
466 xrv = xrv_root;
467 xrv_root = (XRV*)xrv->xrv_rv;
468 return xrv;
469 }
470 return more_xrv();
471}
472
473static void
474del_xrv(p)
475XRV* p;
476{
477 p->xrv_rv = (SV*)xrv_root;
478 xrv_root = p;
479}
480
481static XRV*
482more_xrv()
483{
ed6116ce
LW
484 register XRV* xrv;
485 register XRV* xrvend;
8990e307 486 xrv_root = (XRV*)safemalloc(1008);
ed6116ce
LW
487 xrv = xrv_root;
488 xrvend = &xrv[1008 / sizeof(XRV) - 1];
489 while (xrv < xrvend) {
490 xrv->xrv_rv = (SV*)(xrv + 1);
491 xrv++;
492 }
493 xrv->xrv_rv = 0;
494 return new_xrv();
495}
496
463ee0b2
LW
497static XPV*
498new_xpv()
499{
500 XPV* xpv;
501 if (xpv_root) {
502 xpv = xpv_root;
503 xpv_root = (XPV*)xpv->xpv_pv;
504 return xpv;
505 }
506 return more_xpv();
507}
508
509static void
510del_xpv(p)
511XPV* p;
512{
513 p->xpv_pv = (char*)xpv_root;
514 xpv_root = p;
515}
516
517static XPV*
518more_xpv()
519{
463ee0b2
LW
520 register XPV* xpv;
521 register XPV* xpvend;
8990e307 522 xpv_root = (XPV*)safemalloc(1008);
463ee0b2
LW
523 xpv = xpv_root;
524 xpvend = &xpv[1008 / sizeof(XPV) - 1];
525 while (xpv < xpvend) {
526 xpv->xpv_pv = (char*)(xpv + 1);
527 xpv++;
528 }
529 xpv->xpv_pv = 0;
530 return new_xpv();
531}
532
533#ifdef PURIFY
8990e307 534#define new_XIV() (void*)safemalloc(sizeof(XPVIV))
463ee0b2
LW
535#define del_XIV(p) free((char*)p)
536#else
85e6fe83 537#define new_XIV() (void*)new_xiv()
463ee0b2
LW
538#define del_XIV(p) del_xiv(p)
539#endif
540
541#ifdef PURIFY
8990e307 542#define new_XNV() (void*)safemalloc(sizeof(XPVNV))
463ee0b2
LW
543#define del_XNV(p) free((char*)p)
544#else
85e6fe83 545#define new_XNV() (void*)new_xnv()
463ee0b2
LW
546#define del_XNV(p) del_xnv(p)
547#endif
548
549#ifdef PURIFY
8990e307 550#define new_XRV() (void*)safemalloc(sizeof(XRV))
ed6116ce
LW
551#define del_XRV(p) free((char*)p)
552#else
85e6fe83 553#define new_XRV() (void*)new_xrv()
ed6116ce
LW
554#define del_XRV(p) del_xrv(p)
555#endif
556
557#ifdef PURIFY
8990e307 558#define new_XPV() (void*)safemalloc(sizeof(XPV))
463ee0b2
LW
559#define del_XPV(p) free((char*)p)
560#else
85e6fe83 561#define new_XPV() (void*)new_xpv()
463ee0b2
LW
562#define del_XPV(p) del_xpv(p)
563#endif
564
8990e307 565#define new_XPVIV() (void*)safemalloc(sizeof(XPVIV))
463ee0b2
LW
566#define del_XPVIV(p) free((char*)p)
567
8990e307 568#define new_XPVNV() (void*)safemalloc(sizeof(XPVNV))
463ee0b2
LW
569#define del_XPVNV(p) free((char*)p)
570
8990e307 571#define new_XPVMG() (void*)safemalloc(sizeof(XPVMG))
463ee0b2
LW
572#define del_XPVMG(p) free((char*)p)
573
8990e307 574#define new_XPVLV() (void*)safemalloc(sizeof(XPVLV))
463ee0b2
LW
575#define del_XPVLV(p) free((char*)p)
576
8990e307 577#define new_XPVAV() (void*)safemalloc(sizeof(XPVAV))
463ee0b2
LW
578#define del_XPVAV(p) free((char*)p)
579
8990e307 580#define new_XPVHV() (void*)safemalloc(sizeof(XPVHV))
463ee0b2
LW
581#define del_XPVHV(p) free((char*)p)
582
8990e307 583#define new_XPVCV() (void*)safemalloc(sizeof(XPVCV))
463ee0b2
LW
584#define del_XPVCV(p) free((char*)p)
585
8990e307 586#define new_XPVGV() (void*)safemalloc(sizeof(XPVGV))
463ee0b2
LW
587#define del_XPVGV(p) free((char*)p)
588
8990e307 589#define new_XPVBM() (void*)safemalloc(sizeof(XPVBM))
463ee0b2
LW
590#define del_XPVBM(p) free((char*)p)
591
8990e307 592#define new_XPVFM() (void*)safemalloc(sizeof(XPVFM))
463ee0b2
LW
593#define del_XPVFM(p) free((char*)p)
594
8990e307
LW
595#define new_XPVIO() (void*)safemalloc(sizeof(XPVIO))
596#define del_XPVIO(p) free((char*)p)
597
79072805
LW
598bool
599sv_upgrade(sv, mt)
600register SV* sv;
601U32 mt;
602{
603 char* pv;
604 U32 cur;
605 U32 len;
a0d0e21e 606 IV iv;
79072805
LW
607 double nv;
608 MAGIC* magic;
609 HV* stash;
610
611 if (SvTYPE(sv) == mt)
612 return TRUE;
613
a5f75d66
AD
614 if (mt < SVt_PVIV)
615 (void)SvOOK_off(sv);
616
79072805
LW
617 switch (SvTYPE(sv)) {
618 case SVt_NULL:
619 pv = 0;
620 cur = 0;
621 len = 0;
622 iv = 0;
623 nv = 0.0;
624 magic = 0;
625 stash = 0;
626 break;
79072805
LW
627 case SVt_IV:
628 pv = 0;
629 cur = 0;
630 len = 0;
463ee0b2
LW
631 iv = SvIVX(sv);
632 nv = (double)SvIVX(sv);
79072805
LW
633 del_XIV(SvANY(sv));
634 magic = 0;
635 stash = 0;
ed6116ce 636 if (mt == SVt_NV)
463ee0b2 637 mt = SVt_PVNV;
ed6116ce
LW
638 else if (mt < SVt_PVIV)
639 mt = SVt_PVIV;
79072805
LW
640 break;
641 case SVt_NV:
642 pv = 0;
643 cur = 0;
644 len = 0;
463ee0b2 645 nv = SvNVX(sv);
ed6116ce 646 iv = I_32(nv);
79072805
LW
647 magic = 0;
648 stash = 0;
649 del_XNV(SvANY(sv));
650 SvANY(sv) = 0;
ed6116ce 651 if (mt < SVt_PVNV)
79072805
LW
652 mt = SVt_PVNV;
653 break;
ed6116ce
LW
654 case SVt_RV:
655 pv = (char*)SvRV(sv);
656 cur = 0;
657 len = 0;
a0d0e21e 658 iv = (IV)pv;
ed6116ce
LW
659 nv = (double)(unsigned long)pv;
660 del_XRV(SvANY(sv));
661 magic = 0;
662 stash = 0;
663 break;
79072805 664 case SVt_PV:
463ee0b2 665 pv = SvPVX(sv);
79072805
LW
666 cur = SvCUR(sv);
667 len = SvLEN(sv);
668 iv = 0;
669 nv = 0.0;
670 magic = 0;
671 stash = 0;
672 del_XPV(SvANY(sv));
748a9306
LW
673 if (mt <= SVt_IV)
674 mt = SVt_PVIV;
675 else if (mt == SVt_NV)
676 mt = SVt_PVNV;
79072805
LW
677 break;
678 case SVt_PVIV:
463ee0b2 679 pv = SvPVX(sv);
79072805
LW
680 cur = SvCUR(sv);
681 len = SvLEN(sv);
463ee0b2 682 iv = SvIVX(sv);
79072805
LW
683 nv = 0.0;
684 magic = 0;
685 stash = 0;
686 del_XPVIV(SvANY(sv));
687 break;
688 case SVt_PVNV:
463ee0b2 689 pv = SvPVX(sv);
79072805
LW
690 cur = SvCUR(sv);
691 len = SvLEN(sv);
463ee0b2
LW
692 iv = SvIVX(sv);
693 nv = SvNVX(sv);
79072805
LW
694 magic = 0;
695 stash = 0;
696 del_XPVNV(SvANY(sv));
697 break;
698 case SVt_PVMG:
463ee0b2 699 pv = SvPVX(sv);
79072805
LW
700 cur = SvCUR(sv);
701 len = SvLEN(sv);
463ee0b2
LW
702 iv = SvIVX(sv);
703 nv = SvNVX(sv);
79072805
LW
704 magic = SvMAGIC(sv);
705 stash = SvSTASH(sv);
706 del_XPVMG(SvANY(sv));
707 break;
708 default:
463ee0b2 709 croak("Can't upgrade that kind of scalar");
79072805
LW
710 }
711
712 switch (mt) {
713 case SVt_NULL:
463ee0b2 714 croak("Can't upgrade to undef");
79072805
LW
715 case SVt_IV:
716 SvANY(sv) = new_XIV();
463ee0b2 717 SvIVX(sv) = iv;
79072805
LW
718 break;
719 case SVt_NV:
720 SvANY(sv) = new_XNV();
463ee0b2 721 SvNVX(sv) = nv;
79072805 722 break;
ed6116ce
LW
723 case SVt_RV:
724 SvANY(sv) = new_XRV();
725 SvRV(sv) = (SV*)pv;
ed6116ce 726 break;
79072805
LW
727 case SVt_PV:
728 SvANY(sv) = new_XPV();
463ee0b2 729 SvPVX(sv) = pv;
79072805
LW
730 SvCUR(sv) = cur;
731 SvLEN(sv) = len;
732 break;
733 case SVt_PVIV:
734 SvANY(sv) = new_XPVIV();
463ee0b2 735 SvPVX(sv) = pv;
79072805
LW
736 SvCUR(sv) = cur;
737 SvLEN(sv) = len;
463ee0b2 738 SvIVX(sv) = iv;
79072805 739 if (SvNIOK(sv))
a0d0e21e 740 (void)SvIOK_on(sv);
79072805
LW
741 SvNOK_off(sv);
742 break;
743 case SVt_PVNV:
744 SvANY(sv) = new_XPVNV();
463ee0b2 745 SvPVX(sv) = pv;
79072805
LW
746 SvCUR(sv) = cur;
747 SvLEN(sv) = len;
463ee0b2
LW
748 SvIVX(sv) = iv;
749 SvNVX(sv) = nv;
79072805
LW
750 break;
751 case SVt_PVMG:
752 SvANY(sv) = new_XPVMG();
463ee0b2 753 SvPVX(sv) = pv;
79072805
LW
754 SvCUR(sv) = cur;
755 SvLEN(sv) = len;
463ee0b2
LW
756 SvIVX(sv) = iv;
757 SvNVX(sv) = nv;
79072805
LW
758 SvMAGIC(sv) = magic;
759 SvSTASH(sv) = stash;
760 break;
761 case SVt_PVLV:
762 SvANY(sv) = new_XPVLV();
463ee0b2 763 SvPVX(sv) = pv;
79072805
LW
764 SvCUR(sv) = cur;
765 SvLEN(sv) = len;
463ee0b2
LW
766 SvIVX(sv) = iv;
767 SvNVX(sv) = nv;
79072805
LW
768 SvMAGIC(sv) = magic;
769 SvSTASH(sv) = stash;
770 LvTARGOFF(sv) = 0;
771 LvTARGLEN(sv) = 0;
772 LvTARG(sv) = 0;
773 LvTYPE(sv) = 0;
774 break;
775 case SVt_PVAV:
776 SvANY(sv) = new_XPVAV();
463ee0b2
LW
777 if (pv)
778 Safefree(pv);
2304df62 779 SvPVX(sv) = 0;
d1bf51dd
CS
780 AvMAX(sv) = -1;
781 AvFILL(sv) = -1;
463ee0b2
LW
782 SvIVX(sv) = 0;
783 SvNVX(sv) = 0.0;
784 SvMAGIC(sv) = magic;
785 SvSTASH(sv) = stash;
786 AvALLOC(sv) = 0;
79072805
LW
787 AvARYLEN(sv) = 0;
788 AvFLAGS(sv) = 0;
789 break;
790 case SVt_PVHV:
791 SvANY(sv) = new_XPVHV();
463ee0b2
LW
792 if (pv)
793 Safefree(pv);
794 SvPVX(sv) = 0;
795 HvFILL(sv) = 0;
796 HvMAX(sv) = 0;
797 HvKEYS(sv) = 0;
798 SvNVX(sv) = 0.0;
79072805
LW
799 SvMAGIC(sv) = magic;
800 SvSTASH(sv) = stash;
79072805
LW
801 HvRITER(sv) = 0;
802 HvEITER(sv) = 0;
803 HvPMROOT(sv) = 0;
804 HvNAME(sv) = 0;
79072805
LW
805 break;
806 case SVt_PVCV:
807 SvANY(sv) = new_XPVCV();
748a9306 808 Zero(SvANY(sv), 1, XPVCV);
463ee0b2 809 SvPVX(sv) = pv;
79072805
LW
810 SvCUR(sv) = cur;
811 SvLEN(sv) = len;
463ee0b2
LW
812 SvIVX(sv) = iv;
813 SvNVX(sv) = nv;
79072805
LW
814 SvMAGIC(sv) = magic;
815 SvSTASH(sv) = stash;
79072805
LW
816 break;
817 case SVt_PVGV:
818 SvANY(sv) = new_XPVGV();
463ee0b2 819 SvPVX(sv) = pv;
79072805
LW
820 SvCUR(sv) = cur;
821 SvLEN(sv) = len;
463ee0b2
LW
822 SvIVX(sv) = iv;
823 SvNVX(sv) = nv;
79072805
LW
824 SvMAGIC(sv) = magic;
825 SvSTASH(sv) = stash;
93a17b20 826 GvGP(sv) = 0;
79072805
LW
827 GvNAME(sv) = 0;
828 GvNAMELEN(sv) = 0;
829 GvSTASH(sv) = 0;
a5f75d66 830 GvFLAGS(sv) = 0;
79072805
LW
831 break;
832 case SVt_PVBM:
833 SvANY(sv) = new_XPVBM();
463ee0b2 834 SvPVX(sv) = pv;
79072805
LW
835 SvCUR(sv) = cur;
836 SvLEN(sv) = len;
463ee0b2
LW
837 SvIVX(sv) = iv;
838 SvNVX(sv) = nv;
79072805
LW
839 SvMAGIC(sv) = magic;
840 SvSTASH(sv) = stash;
841 BmRARE(sv) = 0;
842 BmUSEFUL(sv) = 0;
843 BmPREVIOUS(sv) = 0;
844 break;
845 case SVt_PVFM:
846 SvANY(sv) = new_XPVFM();
748a9306 847 Zero(SvANY(sv), 1, XPVFM);
463ee0b2 848 SvPVX(sv) = pv;
79072805
LW
849 SvCUR(sv) = cur;
850 SvLEN(sv) = len;
463ee0b2
LW
851 SvIVX(sv) = iv;
852 SvNVX(sv) = nv;
79072805
LW
853 SvMAGIC(sv) = magic;
854 SvSTASH(sv) = stash;
79072805 855 break;
8990e307
LW
856 case SVt_PVIO:
857 SvANY(sv) = new_XPVIO();
748a9306 858 Zero(SvANY(sv), 1, XPVIO);
8990e307
LW
859 SvPVX(sv) = pv;
860 SvCUR(sv) = cur;
861 SvLEN(sv) = len;
862 SvIVX(sv) = iv;
863 SvNVX(sv) = nv;
864 SvMAGIC(sv) = magic;
865 SvSTASH(sv) = stash;
85e6fe83 866 IoPAGE_LEN(sv) = 60;
8990e307
LW
867 break;
868 }
869 SvFLAGS(sv) &= ~SVTYPEMASK;
870 SvFLAGS(sv) |= mt;
79072805
LW
871 return TRUE;
872}
873
a0d0e21e 874#ifdef DEBUGGING
79072805
LW
875char *
876sv_peek(sv)
877register SV *sv;
878{
879 char *t = tokenbuf;
a0d0e21e 880 int unref = 0;
79072805
LW
881
882 retry:
883 if (!sv) {
884 strcpy(t, "VOID");
a0d0e21e 885 goto finish;
79072805
LW
886 }
887 else if (sv == (SV*)0x55555555 || SvTYPE(sv) == 'U') {
888 strcpy(t, "WILD");
a0d0e21e
LW
889 goto finish;
890 }
891 else if (sv == &sv_undef || sv == &sv_no || sv == &sv_yes) {
892 if (sv == &sv_undef) {
893 strcpy(t, "SV_UNDEF");
894 if (!(SvFLAGS(sv) & (SVf_OK|SVf_OOK|SVs_OBJECT|
895 SVs_GMG|SVs_SMG|SVs_RMG)) &&
896 SvREADONLY(sv))
897 goto finish;
898 }
899 else if (sv == &sv_no) {
900 strcpy(t, "SV_NO");
901 if (!(SvFLAGS(sv) & (SVf_ROK|SVf_OOK|SVs_OBJECT|
902 SVs_GMG|SVs_SMG|SVs_RMG)) &&
903 !(~SvFLAGS(sv) & (SVf_POK|SVf_NOK|SVf_READONLY|
904 SVp_POK|SVp_NOK)) &&
905 SvCUR(sv) == 0 &&
906 SvNVX(sv) == 0.0)
907 goto finish;
908 }
909 else {
910 strcpy(t, "SV_YES");
911 if (!(SvFLAGS(sv) & (SVf_ROK|SVf_OOK|SVs_OBJECT|
912 SVs_GMG|SVs_SMG|SVs_RMG)) &&
913 !(~SvFLAGS(sv) & (SVf_POK|SVf_NOK|SVf_READONLY|
914 SVp_POK|SVp_NOK)) &&
915 SvCUR(sv) == 1 &&
916 SvPVX(sv) && *SvPVX(sv) == '1' &&
917 SvNVX(sv) == 1.0)
918 goto finish;
919 }
920 t += strlen(t);
921 *t++ = ':';
79072805 922 }
a0d0e21e
LW
923 else if (SvREFCNT(sv) == 0) {
924 *t++ = '(';
925 unref++;
79072805 926 }
a0d0e21e
LW
927 if (SvROK(sv)) {
928 *t++ = '\\';
929 if (t - tokenbuf + unref > 10) {
930 strcpy(tokenbuf + unref + 3,"...");
931 goto finish;
79072805 932 }
a0d0e21e
LW
933 sv = (SV*)SvRV(sv);
934 goto retry;
935 }
936 switch (SvTYPE(sv)) {
937 default:
938 strcpy(t,"FREED");
939 goto finish;
940
941 case SVt_NULL:
942 strcpy(t,"UNDEF");
943 return tokenbuf;
944 case SVt_IV:
945 strcpy(t,"IV");
946 break;
947 case SVt_NV:
948 strcpy(t,"NV");
949 break;
950 case SVt_RV:
951 strcpy(t,"RV");
952 break;
953 case SVt_PV:
954 strcpy(t,"PV");
955 break;
956 case SVt_PVIV:
957 strcpy(t,"PVIV");
958 break;
959 case SVt_PVNV:
960 strcpy(t,"PVNV");
961 break;
962 case SVt_PVMG:
963 strcpy(t,"PVMG");
964 break;
965 case SVt_PVLV:
966 strcpy(t,"PVLV");
967 break;
968 case SVt_PVAV:
969 strcpy(t,"AV");
970 break;
971 case SVt_PVHV:
972 strcpy(t,"HV");
973 break;
974 case SVt_PVCV:
975 if (CvGV(sv))
976 sprintf(t, "CV(%s)", GvNAME(CvGV(sv)));
977 else
978 strcpy(t, "CV()");
979 goto finish;
980 case SVt_PVGV:
981 strcpy(t,"GV");
982 break;
983 case SVt_PVBM:
984 strcpy(t,"BM");
985 break;
986 case SVt_PVFM:
987 strcpy(t,"FM");
988 break;
989 case SVt_PVIO:
990 strcpy(t,"IO");
991 break;
79072805
LW
992 }
993 t += strlen(t);
994
a0d0e21e 995 if (SvPOKp(sv)) {
463ee0b2 996 if (!SvPVX(sv))
a0d0e21e 997 strcpy(t, "(null)");
79072805 998 if (SvOOK(sv))
2304df62 999 sprintf(t,"(%ld+\"%.127s\")",(long)SvIVX(sv),SvPVX(sv));
79072805 1000 else
2304df62 1001 sprintf(t,"(\"%.127s\")",SvPVX(sv));
79072805 1002 }
bbce6d69
PP
1003 else if (SvNOKp(sv)) {
1004 NUMERIC_STANDARD();
463ee0b2 1005 sprintf(t,"(%g)",SvNVX(sv));
bbce6d69 1006 }
a0d0e21e 1007 else if (SvIOKp(sv))
463ee0b2 1008 sprintf(t,"(%ld)",(long)SvIVX(sv));
79072805
LW
1009 else
1010 strcpy(t,"()");
a0d0e21e
LW
1011
1012 finish:
1013 if (unref) {
1014 t += strlen(t);
1015 while (unref--)
1016 *t++ = ')';
1017 *t = '\0';
1018 }
79072805
LW
1019 return tokenbuf;
1020}
a0d0e21e 1021#endif
79072805
LW
1022
1023int
1024sv_backoff(sv)
1025register SV *sv;
1026{
1027 assert(SvOOK(sv));
463ee0b2
LW
1028 if (SvIVX(sv)) {
1029 char *s = SvPVX(sv);
1030 SvLEN(sv) += SvIVX(sv);
1031 SvPVX(sv) -= SvIVX(sv);
79072805 1032 SvIV_set(sv, 0);
463ee0b2 1033 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
79072805
LW
1034 }
1035 SvFLAGS(sv) &= ~SVf_OOK;
a0d0e21e 1036 return 0;
79072805
LW
1037}
1038
1039char *
1040sv_grow(sv,newlen)
1041register SV *sv;
1042#ifndef DOSISH
1043register I32 newlen;
1044#else
1045unsigned long newlen;
1046#endif
1047{
1048 register char *s;
1049
55497cff 1050#ifdef HAS_64K_LIMIT
79072805 1051 if (newlen >= 0x10000) {
d1bf51dd 1052 PerlIO_printf(Perl_debug_log, "Allocation too large: %lx\n", newlen);
79072805
LW
1053 my_exit(1);
1054 }
55497cff 1055#endif /* HAS_64K_LIMIT */
a0d0e21e
LW
1056 if (SvROK(sv))
1057 sv_unref(sv);
79072805
LW
1058 if (SvTYPE(sv) < SVt_PV) {
1059 sv_upgrade(sv, SVt_PV);
463ee0b2 1060 s = SvPVX(sv);
79072805
LW
1061 }
1062 else if (SvOOK(sv)) { /* pv is offset? */
1063 sv_backoff(sv);
463ee0b2 1064 s = SvPVX(sv);
79072805
LW
1065 if (newlen > SvLEN(sv))
1066 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
1067 }
1068 else
463ee0b2 1069 s = SvPVX(sv);
79072805 1070 if (newlen > SvLEN(sv)) { /* need more room? */
85e6fe83 1071 if (SvLEN(sv) && s)
79072805
LW
1072 Renew(s,newlen,char);
1073 else
1074 New(703,s,newlen,char);
1075 SvPV_set(sv, s);
1076 SvLEN_set(sv, newlen);
1077 }
1078 return s;
1079}
1080
1081void
1082sv_setiv(sv,i)
1083register SV *sv;
a0d0e21e 1084IV i;
79072805 1085{
ed6116ce 1086 if (SvTHINKFIRST(sv)) {
8990e307 1087 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
1088 croak(no_modify);
1089 if (SvROK(sv))
1090 sv_unref(sv);
1091 }
463ee0b2
LW
1092 switch (SvTYPE(sv)) {
1093 case SVt_NULL:
79072805 1094 sv_upgrade(sv, SVt_IV);
463ee0b2
LW
1095 break;
1096 case SVt_NV:
1097 sv_upgrade(sv, SVt_PVNV);
1098 break;
ed6116ce 1099 case SVt_RV:
463ee0b2 1100 case SVt_PV:
79072805 1101 sv_upgrade(sv, SVt_PVIV);
463ee0b2 1102 break;
a0d0e21e
LW
1103
1104 case SVt_PVGV:
1105 if (SvFAKE(sv)) {
1106 sv_unglob(sv);
1107 break;
1108 }
1109 /* FALL THROUGH */
1110 case SVt_PVAV:
1111 case SVt_PVHV:
1112 case SVt_PVCV:
1113 case SVt_PVFM:
1114 case SVt_PVIO:
1115 croak("Can't coerce %s to integer in %s", sv_reftype(sv,0),
1116 op_name[op->op_type]);
463ee0b2 1117 }
a0d0e21e 1118 (void)SvIOK_only(sv); /* validate number */
a5f75d66 1119 SvIVX(sv) = i;
463ee0b2 1120 SvTAINT(sv);
79072805
LW
1121}
1122
1123void
55497cff
PP
1124sv_setuv(sv,u)
1125register SV *sv;
1126UV u;
1127{
1128 if (u <= IV_MAX)
1129 sv_setiv(sv, u);
1130 else
1131 sv_setnv(sv, (double)u);
1132}
1133
1134void
79072805
LW
1135sv_setnv(sv,num)
1136register SV *sv;
1137double num;
1138{
ed6116ce 1139 if (SvTHINKFIRST(sv)) {
8990e307 1140 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
1141 croak(no_modify);
1142 if (SvROK(sv))
1143 sv_unref(sv);
1144 }
a0d0e21e
LW
1145 switch (SvTYPE(sv)) {
1146 case SVt_NULL:
1147 case SVt_IV:
79072805 1148 sv_upgrade(sv, SVt_NV);
a0d0e21e
LW
1149 break;
1150 case SVt_NV:
1151 case SVt_RV:
1152 case SVt_PV:
1153 case SVt_PVIV:
79072805 1154 sv_upgrade(sv, SVt_PVNV);
a0d0e21e
LW
1155 /* FALL THROUGH */
1156 case SVt_PVNV:
1157 case SVt_PVMG:
1158 case SVt_PVBM:
1159 case SVt_PVLV:
1160 if (SvOOK(sv))
1161 (void)SvOOK_off(sv);
1162 break;
1163 case SVt_PVGV:
1164 if (SvFAKE(sv)) {
1165 sv_unglob(sv);
1166 break;
1167 }
1168 /* FALL THROUGH */
1169 case SVt_PVAV:
1170 case SVt_PVHV:
1171 case SVt_PVCV:
1172 case SVt_PVFM:
1173 case SVt_PVIO:
1174 croak("Can't coerce %s to number in %s", sv_reftype(sv,0),
1175 op_name[op->op_type]);
79072805 1176 }
463ee0b2 1177 SvNVX(sv) = num;
a0d0e21e 1178 (void)SvNOK_only(sv); /* validate number */
463ee0b2 1179 SvTAINT(sv);
79072805
LW
1180}
1181
a0d0e21e
LW
1182static void
1183not_a_number(sv)
1184SV *sv;
1185{
1186 char tmpbuf[64];
1187 char *d = tmpbuf;
1188 char *s;
1189 int i;
1190
1191 for (s = SvPVX(sv), i = 50; *s && i; s++,i--) {
bbce6d69
PP
1192 int ch = *s & 0xFF;
1193 if (ch & 128 && !isPRINT_LC(ch)) {
a0d0e21e
LW
1194 *d++ = 'M';
1195 *d++ = '-';
1196 ch &= 127;
1197 }
bbce6d69
PP
1198 if (ch == '\n') {
1199 *d++ = '\\';
1200 *d++ = 'n';
1201 }
1202 else if (ch == '\r') {
1203 *d++ = '\\';
1204 *d++ = 'r';
1205 }
1206 else if (ch == '\f') {
1207 *d++ = '\\';
1208 *d++ = 'f';
1209 }
1210 else if (ch == '\\') {
1211 *d++ = '\\';
1212 *d++ = '\\';
1213 }
1214 else if (isPRINT_LC(ch))
a0d0e21e
LW
1215 *d++ = ch;
1216 else {
1217 *d++ = '^';
bbce6d69 1218 *d++ = toCTRL(ch);
a0d0e21e
LW
1219 }
1220 }
1221 if (*s) {
1222 *d++ = '.';
1223 *d++ = '.';
1224 *d++ = '.';
1225 }
1226 *d = '\0';
1227
1228 if (op)
c07a80fd 1229 warn("Argument \"%s\" isn't numeric in %s", tmpbuf,
a0d0e21e
LW
1230 op_name[op->op_type]);
1231 else
1232 warn("Argument \"%s\" isn't numeric", tmpbuf);
1233}
1234
1235IV
79072805
LW
1236sv_2iv(sv)
1237register SV *sv;
1238{
1239 if (!sv)
1240 return 0;
8990e307 1241 if (SvGMAGICAL(sv)) {
463ee0b2
LW
1242 mg_get(sv);
1243 if (SvIOKp(sv))
1244 return SvIVX(sv);
748a9306
LW
1245 if (SvNOKp(sv)) {
1246 if (SvNVX(sv) < 0.0)
1247 return I_V(SvNVX(sv));
1248 else
5d94fbed 1249 return (IV) U_V(SvNVX(sv));
748a9306 1250 }
a0d0e21e
LW
1251 if (SvPOKp(sv) && SvLEN(sv)) {
1252 if (dowarn && !looks_like_number(sv))
1253 not_a_number(sv);
1254 return (IV)atol(SvPVX(sv));
1255 }
16d20bd9
AD
1256 if (!SvROK(sv)) {
1257 return 0;
1258 }
463ee0b2 1259 }
ed6116ce 1260 if (SvTHINKFIRST(sv)) {
a0d0e21e
LW
1261 if (SvROK(sv)) {
1262#ifdef OVERLOAD
1263 SV* tmpstr;
1264 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv, numer)))
1265 return SvIV(tmpstr);
1266#endif /* OVERLOAD */
1267 return (IV)SvRV(sv);
1268 }
ed6116ce 1269 if (SvREADONLY(sv)) {
748a9306
LW
1270 if (SvNOKp(sv)) {
1271 if (SvNVX(sv) < 0.0)
1272 return I_V(SvNVX(sv));
1273 else
5d94fbed 1274 return (IV) U_V(SvNVX(sv));
748a9306
LW
1275 }
1276 if (SvPOKp(sv) && SvLEN(sv)) {
a0d0e21e
LW
1277 if (dowarn && !looks_like_number(sv))
1278 not_a_number(sv);
1279 return (IV)atol(SvPVX(sv));
1280 }
ed6116ce 1281 if (dowarn)
8990e307 1282 warn(warn_uninit);
ed6116ce
LW
1283 return 0;
1284 }
79072805 1285 }
463ee0b2 1286 switch (SvTYPE(sv)) {
463ee0b2 1287 case SVt_NULL:
79072805 1288 sv_upgrade(sv, SVt_IV);
463ee0b2
LW
1289 return SvIVX(sv);
1290 case SVt_PV:
79072805 1291 sv_upgrade(sv, SVt_PVIV);
463ee0b2
LW
1292 break;
1293 case SVt_NV:
1294 sv_upgrade(sv, SVt_PVNV);
1295 break;
1296 }
748a9306 1297 if (SvNOKp(sv)) {
a5f75d66 1298 (void)SvIOK_on(sv);
748a9306
LW
1299 if (SvNVX(sv) < 0.0)
1300 SvIVX(sv) = I_V(SvNVX(sv));
1301 else
ff68c719 1302 SvUVX(sv) = U_V(SvNVX(sv));
748a9306
LW
1303 }
1304 else if (SvPOKp(sv) && SvLEN(sv)) {
a0d0e21e
LW
1305 if (dowarn && !looks_like_number(sv))
1306 not_a_number(sv);
a5f75d66 1307 (void)SvIOK_on(sv);
a0d0e21e 1308 SvIVX(sv) = (IV)atol(SvPVX(sv));
93a17b20 1309 }
79072805 1310 else {
91bba347 1311 if (dowarn && !localizing && !(SvFLAGS(sv) & SVs_PADTMP))
8990e307 1312 warn(warn_uninit);
a0d0e21e 1313 return 0;
79072805 1314 }
760ac839 1315 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2iv(%ld)\n",
a0d0e21e 1316 (unsigned long)sv,(long)SvIVX(sv)));
463ee0b2 1317 return SvIVX(sv);
79072805
LW
1318}
1319
ff68c719
PP
1320UV
1321sv_2uv(sv)
1322register SV *sv;
1323{
1324 if (!sv)
1325 return 0;
1326 if (SvGMAGICAL(sv)) {
1327 mg_get(sv);
1328 if (SvIOKp(sv))
1329 return SvUVX(sv);
1330 if (SvNOKp(sv))
1331 return U_V(SvNVX(sv));
1332 if (SvPOKp(sv) && SvLEN(sv)) {
1333 if (dowarn && !looks_like_number(sv))
1334 not_a_number(sv);
1335 return (UV)atol(SvPVX(sv));
1336 }
1337 if (!SvROK(sv)) {
1338 return 0;
1339 }
1340 }
1341 if (SvTHINKFIRST(sv)) {
1342 if (SvROK(sv)) {
1343#ifdef OVERLOAD
1344 SV* tmpstr;
1345 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv, numer)))
1346 return SvUV(tmpstr);
1347#endif /* OVERLOAD */
1348 return (UV)SvRV(sv);
1349 }
1350 if (SvREADONLY(sv)) {
1351 if (SvNOKp(sv)) {
1352 return U_V(SvNVX(sv));
1353 }
1354 if (SvPOKp(sv) && SvLEN(sv)) {
1355 if (dowarn && !looks_like_number(sv))
1356 not_a_number(sv);
1357 return (UV)atol(SvPVX(sv));
1358 }
1359 if (dowarn)
1360 warn(warn_uninit);
1361 return 0;
1362 }
1363 }
1364 switch (SvTYPE(sv)) {
1365 case SVt_NULL:
1366 sv_upgrade(sv, SVt_IV);
1367 return SvUVX(sv);
1368 case SVt_PV:
1369 sv_upgrade(sv, SVt_PVIV);
1370 break;
1371 case SVt_NV:
1372 sv_upgrade(sv, SVt_PVNV);
1373 break;
1374 }
1375 if (SvNOKp(sv)) {
1376 (void)SvIOK_on(sv);
1377 SvUVX(sv) = U_V(SvNVX(sv));
1378 }
1379 else if (SvPOKp(sv) && SvLEN(sv)) {
1380 if (dowarn && !looks_like_number(sv))
1381 not_a_number(sv);
1382 (void)SvIOK_on(sv);
1383 SvUVX(sv) = (UV)atol(SvPVX(sv));
1384 }
1385 else {
1386 if (dowarn && !localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1387 warn(warn_uninit);
1388 return 0;
1389 }
1390 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2uv(%lu)\n",
1391 (unsigned long)sv,SvUVX(sv)));
1392 return SvUVX(sv);
1393}
1394
79072805
LW
1395double
1396sv_2nv(sv)
1397register SV *sv;
1398{
1399 if (!sv)
1400 return 0.0;
8990e307 1401 if (SvGMAGICAL(sv)) {
463ee0b2
LW
1402 mg_get(sv);
1403 if (SvNOKp(sv))
1404 return SvNVX(sv);
a0d0e21e 1405 if (SvPOKp(sv) && SvLEN(sv)) {
748a9306 1406 if (dowarn && !SvIOKp(sv) && !looks_like_number(sv))
a0d0e21e 1407 not_a_number(sv);
bbce6d69 1408 NUMERIC_STANDARD();
463ee0b2 1409 return atof(SvPVX(sv));
a0d0e21e 1410 }
463ee0b2
LW
1411 if (SvIOKp(sv))
1412 return (double)SvIVX(sv);
16d20bd9
AD
1413 if (!SvROK(sv)) {
1414 return 0;
1415 }
463ee0b2 1416 }
ed6116ce 1417 if (SvTHINKFIRST(sv)) {
a0d0e21e
LW
1418 if (SvROK(sv)) {
1419#ifdef OVERLOAD
1420 SV* tmpstr;
1421 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)))
1422 return SvNV(tmpstr);
1423#endif /* OVERLOAD */
1424 return (double)(unsigned long)SvRV(sv);
1425 }
ed6116ce 1426 if (SvREADONLY(sv)) {
748a9306
LW
1427 if (SvPOKp(sv) && SvLEN(sv)) {
1428 if (dowarn && !SvIOKp(sv) && !looks_like_number(sv))
a0d0e21e 1429 not_a_number(sv);
bbce6d69 1430 NUMERIC_STANDARD();
ed6116ce 1431 return atof(SvPVX(sv));
a0d0e21e 1432 }
748a9306 1433 if (SvIOKp(sv))
8990e307 1434 return (double)SvIVX(sv);
ed6116ce 1435 if (dowarn)
8990e307 1436 warn(warn_uninit);
ed6116ce
LW
1437 return 0.0;
1438 }
79072805
LW
1439 }
1440 if (SvTYPE(sv) < SVt_NV) {
463ee0b2
LW
1441 if (SvTYPE(sv) == SVt_IV)
1442 sv_upgrade(sv, SVt_PVNV);
1443 else
1444 sv_upgrade(sv, SVt_NV);
bbce6d69
PP
1445 DEBUG_c(NUMERIC_STANDARD());
1446 DEBUG_c(PerlIO_printf(Perl_debug_log,
1447 "0x%lx num(%g)\n",(unsigned long)sv,SvNVX(sv)));
79072805
LW
1448 }
1449 else if (SvTYPE(sv) < SVt_PVNV)
1450 sv_upgrade(sv, SVt_PVNV);
748a9306
LW
1451 if (SvIOKp(sv) &&
1452 (!SvPOKp(sv) || !strchr(SvPVX(sv),'.') || !looks_like_number(sv)))
93a17b20 1453 {
463ee0b2 1454 SvNVX(sv) = (double)SvIVX(sv);
93a17b20 1455 }
748a9306
LW
1456 else if (SvPOKp(sv) && SvLEN(sv)) {
1457 if (dowarn && !SvIOKp(sv) && !looks_like_number(sv))
a0d0e21e 1458 not_a_number(sv);
bbce6d69 1459 NUMERIC_STANDARD();
463ee0b2 1460 SvNVX(sv) = atof(SvPVX(sv));
93a17b20 1461 }
79072805 1462 else {
91bba347 1463 if (dowarn && !localizing && !(SvFLAGS(sv) & SVs_PADTMP))
8990e307 1464 warn(warn_uninit);
a0d0e21e 1465 return 0.0;
79072805
LW
1466 }
1467 SvNOK_on(sv);
bbce6d69
PP
1468 DEBUG_c(NUMERIC_STANDARD());
1469 DEBUG_c(PerlIO_printf(Perl_debug_log,
1470 "0x%lx 2nv(%g)\n",(unsigned long)sv,SvNVX(sv)));
463ee0b2 1471 return SvNVX(sv);
79072805
LW
1472}
1473
1474char *
463ee0b2 1475sv_2pv(sv, lp)
79072805 1476register SV *sv;
463ee0b2 1477STRLEN *lp;
79072805
LW
1478{
1479 register char *s;
1480 int olderrno;
1481
463ee0b2
LW
1482 if (!sv) {
1483 *lp = 0;
1484 return "";
1485 }
8990e307 1486 if (SvGMAGICAL(sv)) {
463ee0b2
LW
1487 mg_get(sv);
1488 if (SvPOKp(sv)) {
1489 *lp = SvCUR(sv);
1490 return SvPVX(sv);
1491 }
1492 if (SvIOKp(sv)) {
a0d0e21e
LW
1493 (void)sprintf(tokenbuf,"%ld",(long)SvIVX(sv));
1494 goto tokensave;
463ee0b2
LW
1495 }
1496 if (SvNOKp(sv)) {
bbce6d69 1497 NUMERIC_STANDARD();
a0d0e21e
LW
1498 Gconvert(SvNVX(sv), DBL_DIG, 0, tokenbuf);
1499 goto tokensave;
463ee0b2 1500 }
16d20bd9
AD
1501 if (!SvROK(sv)) {
1502 *lp = 0;
1503 return "";
1504 }
463ee0b2 1505 }
ed6116ce
LW
1506 if (SvTHINKFIRST(sv)) {
1507 if (SvROK(sv)) {
a0d0e21e
LW
1508#ifdef OVERLOAD
1509 SV* tmpstr;
1510 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)))
1511 return SvPV(tmpstr,*lp);
1512#endif /* OVERLOAD */
ed6116ce
LW
1513 sv = (SV*)SvRV(sv);
1514 if (!sv)
1515 s = "NULLREF";
1516 else {
1517 switch (SvTYPE(sv)) {
1518 case SVt_NULL:
1519 case SVt_IV:
1520 case SVt_NV:
1521 case SVt_RV:
1522 case SVt_PV:
1523 case SVt_PVIV:
1524 case SVt_PVNV:
1525 case SVt_PVBM:
1526 case SVt_PVMG: s = "SCALAR"; break;
1527 case SVt_PVLV: s = "LVALUE"; break;
1528 case SVt_PVAV: s = "ARRAY"; break;
1529 case SVt_PVHV: s = "HASH"; break;
1530 case SVt_PVCV: s = "CODE"; break;
1531 case SVt_PVGV: s = "GLOB"; break;
1532 case SVt_PVFM: s = "FORMATLINE"; break;
8990e307 1533 case SVt_PVIO: s = "FILEHANDLE"; break;
ed6116ce
LW
1534 default: s = "UNKNOWN"; break;
1535 }
1536 if (SvOBJECT(sv))
1537 sprintf(tokenbuf, "%s=%s(0x%lx)",
1538 HvNAME(SvSTASH(sv)), s, (unsigned long)sv);
1539 else
1540 sprintf(tokenbuf, "%s(0x%lx)", s, (unsigned long)sv);
a0d0e21e 1541 goto tokensaveref;
463ee0b2 1542 }
ed6116ce
LW
1543 *lp = strlen(s);
1544 return s;
79072805 1545 }
ed6116ce 1546 if (SvREADONLY(sv)) {
748a9306 1547 if (SvNOKp(sv)) {
bbce6d69 1548 NUMERIC_STANDARD();
a0d0e21e
LW
1549 Gconvert(SvNVX(sv), DBL_DIG, 0, tokenbuf);
1550 goto tokensave;
ed6116ce 1551 }
8bb9dbe4
LW
1552 if (SvIOKp(sv)) {
1553 (void)sprintf(tokenbuf,"%ld",(long)SvIVX(sv));
1554 goto tokensave;
1555 }
ed6116ce 1556 if (dowarn)
8990e307 1557 warn(warn_uninit);
ed6116ce
LW
1558 *lp = 0;
1559 return "";
79072805 1560 }
79072805
LW
1561 }
1562 if (!SvUPGRADE(sv, SVt_PV))
1563 return 0;
748a9306 1564 if (SvNOKp(sv)) {
79072805
LW
1565 if (SvTYPE(sv) < SVt_PVNV)
1566 sv_upgrade(sv, SVt_PVNV);
1567 SvGROW(sv, 28);
463ee0b2 1568 s = SvPVX(sv);
79072805 1569 olderrno = errno; /* some Xenix systems wipe out errno here */
79072805 1570#ifdef apollo
463ee0b2 1571 if (SvNVX(sv) == 0.0)
79072805
LW
1572 (void)strcpy(s,"0");
1573 else
1574#endif /*apollo*/
bbce6d69
PP
1575 {
1576 NUMERIC_STANDARD();
a0d0e21e 1577 Gconvert(SvNVX(sv), DBL_DIG, 0, s);
bbce6d69 1578 }
79072805 1579 errno = olderrno;
a0d0e21e
LW
1580#ifdef FIXNEGATIVEZERO
1581 if (*s == '-' && s[1] == '0' && !s[2])
1582 strcpy(s,"0");
1583#endif
79072805
LW
1584 while (*s) s++;
1585#ifdef hcx
1586 if (s[-1] == '.')
1587 s--;
1588#endif
1589 }
748a9306 1590 else if (SvIOKp(sv)) {
79072805
LW
1591 if (SvTYPE(sv) < SVt_PVIV)
1592 sv_upgrade(sv, SVt_PVIV);
1593 SvGROW(sv, 11);
463ee0b2 1594 s = SvPVX(sv);
79072805 1595 olderrno = errno; /* some Xenix systems wipe out errno here */
a0d0e21e 1596 (void)sprintf(s,"%ld",(long)SvIVX(sv));
79072805
LW
1597 errno = olderrno;
1598 while (*s) s++;
1599 }
1600 else {
91bba347 1601 if (dowarn && !localizing && !(SvFLAGS(sv) & SVs_PADTMP))
8990e307 1602 warn(warn_uninit);
a0d0e21e
LW
1603 *lp = 0;
1604 return "";
79072805
LW
1605 }
1606 *s = '\0';
463ee0b2
LW
1607 *lp = s - SvPVX(sv);
1608 SvCUR_set(sv, *lp);
79072805 1609 SvPOK_on(sv);
760ac839 1610 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2pv(%s)\n",(unsigned long)sv,SvPVX(sv)));
463ee0b2 1611 return SvPVX(sv);
a0d0e21e
LW
1612
1613 tokensave:
1614 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
1615 /* Sneaky stuff here */
1616
1617 tokensaveref:
1618 sv = sv_newmortal();
1619 *lp = strlen(tokenbuf);
1620 sv_setpvn(sv, tokenbuf, *lp);
1621 return SvPVX(sv);
1622 }
1623 else {
1624 STRLEN len;
1625
1626#ifdef FIXNEGATIVEZERO
1627 if (*tokenbuf == '-' && tokenbuf[1] == '0' && !tokenbuf[2])
1628 strcpy(tokenbuf,"0");
1629#endif
1630 (void)SvUPGRADE(sv, SVt_PV);
1631 len = *lp = strlen(tokenbuf);
1632 s = SvGROW(sv, len + 1);
1633 SvCUR_set(sv, len);
1634 (void)strcpy(s, tokenbuf);
6bf554b4 1635 SvPOKp_on(sv);
a0d0e21e
LW
1636 return s;
1637 }
463ee0b2
LW
1638}
1639
1640/* This function is only called on magical items */
1641bool
1642sv_2bool(sv)
1643register SV *sv;
1644{
8990e307 1645 if (SvGMAGICAL(sv))
463ee0b2
LW
1646 mg_get(sv);
1647
a0d0e21e
LW
1648 if (!SvOK(sv))
1649 return 0;
1650 if (SvROK(sv)) {
1651#ifdef OVERLOAD
1652 {
1653 SV* tmpsv;
1654 if (SvAMAGIC(sv) && (tmpsv = AMG_CALLun(sv,bool_)))
1655 return SvTRUE(tmpsv);
1656 }
1657#endif /* OVERLOAD */
1658 return SvRV(sv) != 0;
1659 }
463ee0b2
LW
1660 if (SvPOKp(sv)) {
1661 register XPV* Xpv;
1662 if ((Xpv = (XPV*)SvANY(sv)) &&
1663 (*Xpv->xpv_pv > '0' ||
1664 Xpv->xpv_cur > 1 ||
1665 (Xpv->xpv_cur && *Xpv->xpv_pv != '0')))
1666 return 1;
1667 else
1668 return 0;
1669 }
1670 else {
1671 if (SvIOKp(sv))
1672 return SvIVX(sv) != 0;
1673 else {
1674 if (SvNOKp(sv))
1675 return SvNVX(sv) != 0.0;
1676 else
1677 return FALSE;
1678 }
1679 }
79072805
LW
1680}
1681
1682/* Note: sv_setsv() should not be called with a source string that needs
463ee0b2 1683 * to be reused, since it may destroy the source string if it is marked
79072805
LW
1684 * as temporary.
1685 */
1686
1687void
1688sv_setsv(dstr,sstr)
1689SV *dstr;
1690register SV *sstr;
1691{
8990e307
LW
1692 register U32 sflags;
1693 register int dtype;
1694 register int stype;
463ee0b2 1695
79072805
LW
1696 if (sstr == dstr)
1697 return;
ed6116ce 1698 if (SvTHINKFIRST(dstr)) {
8990e307 1699 if (SvREADONLY(dstr) && curcop != &compiling)
ed6116ce
LW
1700 croak(no_modify);
1701 if (SvROK(dstr))
1702 sv_unref(dstr);
1703 }
79072805
LW
1704 if (!sstr)
1705 sstr = &sv_undef;
8990e307
LW
1706 stype = SvTYPE(sstr);
1707 dtype = SvTYPE(dstr);
79072805 1708
8e07c86e
AD
1709 if (dtype == SVt_PVGV && (SvFLAGS(dstr) & SVf_FAKE)) {
1710 sv_unglob(dstr); /* so fake GLOB won't perpetuate */
4633a7c4
LW
1711 sv_setpvn(dstr, "", 0);
1712 (void)SvPOK_only(dstr);
8e07c86e
AD
1713 dtype = SvTYPE(dstr);
1714 }
1715
a0d0e21e
LW
1716#ifdef OVERLOAD
1717 SvAMAGIC_off(dstr);
1718#endif /* OVERLOAD */
463ee0b2 1719 /* There's a lot of redundancy below but we're going for speed here */
79072805 1720
8990e307 1721 switch (stype) {
79072805 1722 case SVt_NULL:
a0d0e21e 1723 (void)SvOK_off(dstr);
79072805 1724 return;
463ee0b2 1725 case SVt_IV:
ff68c719 1726 if (dtype != SVt_IV && dtype < SVt_PVIV) {
8990e307
LW
1727 if (dtype < SVt_IV)
1728 sv_upgrade(dstr, SVt_IV);
8990e307
LW
1729 else if (dtype == SVt_NV)
1730 sv_upgrade(dstr, SVt_PVNV);
ff68c719 1731 else
a0d0e21e 1732 sv_upgrade(dstr, SVt_PVIV);
8990e307 1733 }
463ee0b2
LW
1734 break;
1735 case SVt_NV:
ff68c719 1736 if (dtype != SVt_NV && dtype < SVt_PVNV) {
8990e307
LW
1737 if (dtype < SVt_NV)
1738 sv_upgrade(dstr, SVt_NV);
ff68c719 1739 else
a0d0e21e 1740 sv_upgrade(dstr, SVt_PVNV);
8990e307 1741 }
463ee0b2 1742 break;
ed6116ce 1743 case SVt_RV:
8990e307 1744 if (dtype < SVt_RV)
ed6116ce 1745 sv_upgrade(dstr, SVt_RV);
c07a80fd
PP
1746 else if (dtype == SVt_PVGV &&
1747 SvTYPE(SvRV(sstr)) == SVt_PVGV) {
1748 sstr = SvRV(sstr);
a5f75d66
AD
1749 if (sstr == dstr) {
1750 if (curcop->cop_stash != GvSTASH(dstr))
1751 GvIMPORTED_on(dstr);
1752 GvMULTI_on(dstr);
1753 return;
1754 }
c07a80fd
PP
1755 goto glob_assign;
1756 }
ed6116ce 1757 break;
463ee0b2 1758 case SVt_PV:
8990e307 1759 if (dtype < SVt_PV)
463ee0b2 1760 sv_upgrade(dstr, SVt_PV);
463ee0b2
LW
1761 break;
1762 case SVt_PVIV:
8990e307 1763 if (dtype < SVt_PVIV)
463ee0b2 1764 sv_upgrade(dstr, SVt_PVIV);
463ee0b2
LW
1765 break;
1766 case SVt_PVNV:
8990e307 1767 if (dtype < SVt_PVNV)
463ee0b2 1768 sv_upgrade(dstr, SVt_PVNV);
463ee0b2 1769 break;
4633a7c4
LW
1770
1771 case SVt_PVLV:
4561caa4 1772 sv_upgrade(dstr, SVt_PVLV);
4633a7c4
LW
1773 break;
1774
1775 case SVt_PVAV:
1776 case SVt_PVHV:
1777 case SVt_PVCV:
4633a7c4
LW
1778 case SVt_PVIO:
1779 if (op)
1780 croak("Bizarre copy of %s in %s", sv_reftype(sstr, 0),
1781 op_name[op->op_type]);
1782 else
1783 croak("Bizarre copy of %s", sv_reftype(sstr, 0));
1784 break;
1785
79072805 1786 case SVt_PVGV:
8990e307 1787 if (dtype <= SVt_PVGV) {
c07a80fd 1788 glob_assign:
a5f75d66 1789 if (dtype != SVt_PVGV) {
a0d0e21e
LW
1790 char *name = GvNAME(sstr);
1791 STRLEN len = GvNAMELEN(sstr);
463ee0b2 1792 sv_upgrade(dstr, SVt_PVGV);
a0d0e21e
LW
1793 sv_magic(dstr, dstr, '*', name, len);
1794 GvSTASH(dstr) = GvSTASH(sstr);
1795 GvNAME(dstr) = savepvn(name, len);
1796 GvNAMELEN(dstr) = len;
1797 SvFAKE_on(dstr); /* can coerce to non-glob */
1798 }
1799 (void)SvOK_off(dstr);
a5f75d66 1800 GvINTRO_off(dstr); /* one-shot flag */
1edc1566 1801 gp_free((GV*)dstr);
79072805 1802 GvGP(dstr) = gp_ref(GvGP(sstr));
8990e307 1803 SvTAINT(dstr);
a5f75d66
AD
1804 if (curcop->cop_stash != GvSTASH(dstr))
1805 GvIMPORTED_on(dstr);
1806 GvMULTI_on(dstr);
79072805
LW
1807 return;
1808 }
1809 /* FALL THROUGH */
1810
1811 default:
8990e307
LW
1812 if (dtype < stype)
1813 sv_upgrade(dstr, stype);
1814 if (SvGMAGICAL(sstr))
79072805 1815 mg_get(sstr);
79072805
LW
1816 }
1817
8990e307
LW
1818 sflags = SvFLAGS(sstr);
1819
1820 if (sflags & SVf_ROK) {
1821 if (dtype >= SVt_PV) {
1822 if (dtype == SVt_PVGV) {
1823 SV *sref = SvREFCNT_inc(SvRV(sstr));
1824 SV *dref = 0;
a5f75d66 1825 int intro = GvINTRO(dstr);
a0d0e21e
LW
1826
1827 if (intro) {
1828 GP *gp;
1829 GvGP(dstr)->gp_refcnt--;
a5f75d66 1830 GvINTRO_off(dstr); /* one-shot flag */
a0d0e21e
LW
1831 Newz(602,gp, 1, GP);
1832 GvGP(dstr) = gp;
1833 GvREFCNT(dstr) = 1;
1834 GvSV(dstr) = NEWSV(72,0);
1835 GvLINE(dstr) = curcop->cop_line;
1edc1566 1836 GvEGV(dstr) = (GV*)dstr;
a0d0e21e 1837 }
a5f75d66 1838 GvMULTI_on(dstr);
8990e307
LW
1839 switch (SvTYPE(sref)) {
1840 case SVt_PVAV:
a0d0e21e
LW
1841 if (intro)
1842 SAVESPTR(GvAV(dstr));
1843 else
1844 dref = (SV*)GvAV(dstr);
8990e307 1845 GvAV(dstr) = (AV*)sref;
a5f75d66
AD
1846 if (curcop->cop_stash != GvSTASH(dstr))
1847 GvIMPORTED_AV_on(dstr);
8990e307
LW
1848 break;
1849 case SVt_PVHV:
a0d0e21e
LW
1850 if (intro)
1851 SAVESPTR(GvHV(dstr));
1852 else
1853 dref = (SV*)GvHV(dstr);
8990e307 1854 GvHV(dstr) = (HV*)sref;
a5f75d66
AD
1855 if (curcop->cop_stash != GvSTASH(dstr))
1856 GvIMPORTED_HV_on(dstr);
8990e307
LW
1857 break;
1858 case SVt_PVCV:
a0d0e21e
LW
1859 if (intro)
1860 SAVESPTR(GvCV(dstr));
748a9306
LW
1861 else {
1862 CV* cv = GvCV(dstr);
4633a7c4
LW
1863 if (cv) {
1864 dref = (SV*)cv;
1865 if (dowarn && sref != dref &&
1866 !GvCVGEN((GV*)dstr) &&
1867 (CvROOT(cv) || CvXSUB(cv)) )
1868 warn("Subroutine %s redefined",
1869 GvENAME((GV*)dstr));
1870 SvFAKE_on(cv);
1871 }
748a9306 1872 }
a5f75d66
AD
1873 if (GvCV(dstr) != (CV*)sref) {
1874 GvCV(dstr) = (CV*)sref;
1875 GvASSUMECV_on(dstr);
1876 }
1877 if (curcop->cop_stash != GvSTASH(dstr))
1878 GvIMPORTED_CV_on(dstr);
8990e307 1879 break;
91bba347
LW
1880 case SVt_PVIO:
1881 if (intro)
1882 SAVESPTR(GvIOp(dstr));
1883 else
1884 dref = (SV*)GvIOp(dstr);
1885 GvIOp(dstr) = (IO*)sref;
1886 break;
8990e307 1887 default:
a0d0e21e
LW
1888 if (intro)
1889 SAVESPTR(GvSV(dstr));
1890 else
1891 dref = (SV*)GvSV(dstr);
8990e307 1892 GvSV(dstr) = sref;
a5f75d66
AD
1893 if (curcop->cop_stash != GvSTASH(dstr))
1894 GvIMPORTED_SV_on(dstr);
8990e307
LW
1895 break;
1896 }
1897 if (dref)
1898 SvREFCNT_dec(dref);
a0d0e21e
LW
1899 if (intro)
1900 SAVEFREESV(sref);
8990e307
LW
1901 SvTAINT(dstr);
1902 return;
1903 }
a0d0e21e 1904 if (SvPVX(dstr)) {
760ac839 1905 (void)SvOOK_off(dstr); /* backoff */
8990e307 1906 Safefree(SvPVX(dstr));
a0d0e21e
LW
1907 SvLEN(dstr)=SvCUR(dstr)=0;
1908 }
8990e307 1909 }
a0d0e21e 1910 (void)SvOK_off(dstr);
8990e307 1911 SvRV(dstr) = SvREFCNT_inc(SvRV(sstr));
ed6116ce 1912 SvROK_on(dstr);
8990e307 1913 if (sflags & SVp_NOK) {
ed6116ce
LW
1914 SvNOK_on(dstr);
1915 SvNVX(dstr) = SvNVX(sstr);
1916 }
8990e307 1917 if (sflags & SVp_IOK) {
a0d0e21e 1918 (void)SvIOK_on(dstr);
ed6116ce
LW
1919 SvIVX(dstr) = SvIVX(sstr);
1920 }
a0d0e21e
LW
1921#ifdef OVERLOAD
1922 if (SvAMAGIC(sstr)) {
1923 SvAMAGIC_on(dstr);
1924 }
1925#endif /* OVERLOAD */
ed6116ce 1926 }
8990e307 1927 else if (sflags & SVp_POK) {
79072805
LW
1928
1929 /*
1930 * Check to see if we can just swipe the string. If so, it's a
1931 * possible small lose on short strings, but a big win on long ones.
463ee0b2
LW
1932 * It might even be a win on short strings if SvPVX(dstr)
1933 * has to be allocated and SvPVX(sstr) has to be freed.
79072805
LW
1934 */
1935
ff68c719 1936 if (SvTEMP(sstr) && /* slated for free anyway? */
a5f75d66
AD
1937 !(sflags & SVf_OOK)) /* and not involved in OOK hack? */
1938 {
adbc6bb1 1939 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
a5f75d66
AD
1940 if (SvOOK(dstr)) {
1941 SvFLAGS(dstr) &= ~SVf_OOK;
1942 Safefree(SvPVX(dstr) - SvIVX(dstr));
1943 }
1944 else
1945 Safefree(SvPVX(dstr));
79072805 1946 }
a5f75d66 1947 (void)SvPOK_only(dstr);
463ee0b2 1948 SvPV_set(dstr, SvPVX(sstr));
79072805
LW
1949 SvLEN_set(dstr, SvLEN(sstr));
1950 SvCUR_set(dstr, SvCUR(sstr));
79072805 1951 SvTEMP_off(dstr);
a5f75d66 1952 (void)SvOK_off(sstr);
79072805
LW
1953 SvPV_set(sstr, Nullch);
1954 SvLEN_set(sstr, 0);
a5f75d66
AD
1955 SvCUR_set(sstr, 0);
1956 SvTEMP_off(sstr);
79072805
LW
1957 }
1958 else { /* have to copy actual string */
8990e307
LW
1959 STRLEN len = SvCUR(sstr);
1960
1961 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
1962 Move(SvPVX(sstr),SvPVX(dstr),len,char);
1963 SvCUR_set(dstr, len);
1964 *SvEND(dstr) = '\0';
a0d0e21e 1965 (void)SvPOK_only(dstr);
79072805
LW
1966 }
1967 /*SUPPRESS 560*/
8990e307 1968 if (sflags & SVp_NOK) {
79072805 1969 SvNOK_on(dstr);
463ee0b2 1970 SvNVX(dstr) = SvNVX(sstr);
79072805 1971 }
8990e307 1972 if (sflags & SVp_IOK) {
a0d0e21e 1973 (void)SvIOK_on(dstr);
463ee0b2 1974 SvIVX(dstr) = SvIVX(sstr);
79072805
LW
1975 }
1976 }
8990e307 1977 else if (sflags & SVp_NOK) {
463ee0b2 1978 SvNVX(dstr) = SvNVX(sstr);
a0d0e21e 1979 (void)SvNOK_only(dstr);
79072805 1980 if (SvIOK(sstr)) {
a0d0e21e 1981 (void)SvIOK_on(dstr);
463ee0b2 1982 SvIVX(dstr) = SvIVX(sstr);
79072805
LW
1983 }
1984 }
8990e307 1985 else if (sflags & SVp_IOK) {
a0d0e21e 1986 (void)SvIOK_only(dstr);
463ee0b2 1987 SvIVX(dstr) = SvIVX(sstr);
79072805
LW
1988 }
1989 else {
a0d0e21e
LW
1990 (void)SvOK_off(dstr);
1991 }
463ee0b2 1992 SvTAINT(dstr);
79072805
LW
1993}
1994
1995void
1996sv_setpvn(sv,ptr,len)
1997register SV *sv;
1998register char *ptr;
1999register STRLEN len;
2000{
4561caa4
CS
2001 assert(len >= 0); /* STRLEN is probably unsigned, so this may
2002 elicit a warning, but it won't hurt. */
ed6116ce 2003 if (SvTHINKFIRST(sv)) {
8990e307 2004 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
2005 croak(no_modify);
2006 if (SvROK(sv))
2007 sv_unref(sv);
2008 }
463ee0b2 2009 if (!ptr) {
a0d0e21e 2010 (void)SvOK_off(sv);
463ee0b2
LW
2011 return;
2012 }
c07a80fd
PP
2013 if (SvTYPE(sv) >= SVt_PV) {
2014 if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
2015 sv_unglob(sv);
2016 }
2017 else if (!sv_upgrade(sv, SVt_PV))
79072805
LW
2018 return;
2019 SvGROW(sv, len + 1);
a0d0e21e 2020 Move(ptr,SvPVX(sv),len,char);
79072805
LW
2021 SvCUR_set(sv, len);
2022 *SvEND(sv) = '\0';
a0d0e21e 2023 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2 2024 SvTAINT(sv);
79072805
LW
2025}
2026
2027void
2028sv_setpv(sv,ptr)
2029register SV *sv;
2030register char *ptr;
2031{
2032 register STRLEN len;
2033
ed6116ce 2034 if (SvTHINKFIRST(sv)) {
8990e307 2035 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
2036 croak(no_modify);
2037 if (SvROK(sv))
2038 sv_unref(sv);
2039 }
463ee0b2 2040 if (!ptr) {
a0d0e21e 2041 (void)SvOK_off(sv);
463ee0b2
LW
2042 return;
2043 }
79072805 2044 len = strlen(ptr);
c07a80fd
PP
2045 if (SvTYPE(sv) >= SVt_PV) {
2046 if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
2047 sv_unglob(sv);
2048 }
2049 else if (!sv_upgrade(sv, SVt_PV))
79072805
LW
2050 return;
2051 SvGROW(sv, len + 1);
463ee0b2 2052 Move(ptr,SvPVX(sv),len+1,char);
79072805 2053 SvCUR_set(sv, len);
a0d0e21e 2054 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2
LW
2055 SvTAINT(sv);
2056}
2057
2058void
2059sv_usepvn(sv,ptr,len)
2060register SV *sv;
2061register char *ptr;
2062register STRLEN len;
2063{
ed6116ce 2064 if (SvTHINKFIRST(sv)) {
8990e307 2065 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
2066 croak(no_modify);
2067 if (SvROK(sv))
2068 sv_unref(sv);
2069 }
463ee0b2
LW
2070 if (!SvUPGRADE(sv, SVt_PV))
2071 return;
2072 if (!ptr) {
a0d0e21e 2073 (void)SvOK_off(sv);
463ee0b2
LW
2074 return;
2075 }
2076 if (SvPVX(sv))
2077 Safefree(SvPVX(sv));
2078 Renew(ptr, len+1, char);
2079 SvPVX(sv) = ptr;
2080 SvCUR_set(sv, len);
2081 SvLEN_set(sv, len+1);
2082 *SvEND(sv) = '\0';
a0d0e21e 2083 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2 2084 SvTAINT(sv);
79072805
LW
2085}
2086
2087void
2088sv_chop(sv,ptr) /* like set but assuming ptr is in sv */
2089register SV *sv;
2090register char *ptr;
2091{
2092 register STRLEN delta;
2093
a0d0e21e 2094 if (!ptr || !SvPOKp(sv))
79072805 2095 return;
ed6116ce 2096 if (SvTHINKFIRST(sv)) {
8990e307 2097 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
2098 croak(no_modify);
2099 if (SvROK(sv))
2100 sv_unref(sv);
2101 }
79072805
LW
2102 if (SvTYPE(sv) < SVt_PVIV)
2103 sv_upgrade(sv,SVt_PVIV);
2104
2105 if (!SvOOK(sv)) {
463ee0b2 2106 SvIVX(sv) = 0;
79072805
LW
2107 SvFLAGS(sv) |= SVf_OOK;
2108 }
8990e307 2109 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK);
463ee0b2 2110 delta = ptr - SvPVX(sv);
79072805
LW
2111 SvLEN(sv) -= delta;
2112 SvCUR(sv) -= delta;
463ee0b2
LW
2113 SvPVX(sv) += delta;
2114 SvIVX(sv) += delta;
79072805
LW
2115}
2116
2117void
2118sv_catpvn(sv,ptr,len)
2119register SV *sv;
2120register char *ptr;
2121register STRLEN len;
2122{
463ee0b2 2123 STRLEN tlen;
748a9306 2124 char *junk;
a0d0e21e 2125
748a9306 2126 junk = SvPV_force(sv, tlen);
463ee0b2 2127 SvGROW(sv, tlen + len + 1);
4633a7c4
LW
2128 if (ptr == junk)
2129 ptr = SvPVX(sv);
463ee0b2 2130 Move(ptr,SvPVX(sv)+tlen,len,char);
79072805
LW
2131 SvCUR(sv) += len;
2132 *SvEND(sv) = '\0';
a0d0e21e 2133 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2 2134 SvTAINT(sv);
79072805
LW
2135}
2136
2137void
2138sv_catsv(dstr,sstr)
2139SV *dstr;
2140register SV *sstr;
2141{
2142 char *s;
463ee0b2 2143 STRLEN len;
79072805
LW
2144 if (!sstr)
2145 return;
463ee0b2
LW
2146 if (s = SvPV(sstr, len))
2147 sv_catpvn(dstr,s,len);
79072805
LW
2148}
2149
2150void
2151sv_catpv(sv,ptr)
2152register SV *sv;
2153register char *ptr;
2154{
2155 register STRLEN len;
463ee0b2 2156 STRLEN tlen;
748a9306 2157 char *junk;
79072805 2158
79072805
LW
2159 if (!ptr)
2160 return;
748a9306 2161 junk = SvPV_force(sv, tlen);
79072805 2162 len = strlen(ptr);
463ee0b2 2163 SvGROW(sv, tlen + len + 1);
4633a7c4
LW
2164 if (ptr == junk)
2165 ptr = SvPVX(sv);
463ee0b2 2166 Move(ptr,SvPVX(sv)+tlen,len+1,char);
79072805 2167 SvCUR(sv) += len;
a0d0e21e 2168 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2 2169 SvTAINT(sv);
79072805
LW
2170}
2171
79072805
LW
2172SV *
2173#ifdef LEAKTEST
2174newSV(x,len)
2175I32 x;
2176#else
2177newSV(len)
2178#endif
2179STRLEN len;
2180{
2181 register SV *sv;
2182
4561caa4 2183 new_SV(sv);
8990e307
LW
2184 SvANY(sv) = 0;
2185 SvREFCNT(sv) = 1;
2186 SvFLAGS(sv) = 0;
79072805
LW
2187 if (len) {
2188 sv_upgrade(sv, SVt_PV);
2189 SvGROW(sv, len + 1);
2190 }
2191 return sv;
2192}
2193
1edc1566
PP
2194/* name is assumed to contain an SV* if (name && namelen == HEf_SVKEY) */
2195
79072805
LW
2196void
2197sv_magic(sv, obj, how, name, namlen)
2198register SV *sv;
2199SV *obj;
a0d0e21e 2200int how;
79072805 2201char *name;
463ee0b2 2202I32 namlen;
79072805
LW
2203{
2204 MAGIC* mg;
2205
55497cff 2206 if (SvREADONLY(sv) && curcop != &compiling && !strchr("gBf", how))
a0d0e21e 2207 croak(no_modify);
4633a7c4 2208 if (SvMAGICAL(sv) || (how == 't' && SvTYPE(sv) >= SVt_PVMG)) {
748a9306
LW
2209 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
2210 if (how == 't')
2211 mg->mg_len |= 1;
463ee0b2 2212 return;
748a9306 2213 }
463ee0b2
LW
2214 }
2215 else {
2216 if (!SvUPGRADE(sv, SVt_PVMG))
2217 return;
463ee0b2 2218 }
79072805
LW
2219 Newz(702,mg, 1, MAGIC);
2220 mg->mg_moremagic = SvMAGIC(sv);
463ee0b2 2221
79072805 2222 SvMAGIC(sv) = mg;
748a9306 2223 if (!obj || obj == sv || how == '#')
8990e307 2224 mg->mg_obj = obj;
85e6fe83 2225 else {
8990e307 2226 mg->mg_obj = SvREFCNT_inc(obj);
85e6fe83
LW
2227 mg->mg_flags |= MGf_REFCOUNTED;
2228 }
79072805 2229 mg->mg_type = how;
463ee0b2 2230 mg->mg_len = namlen;
1edc1566
PP
2231 if (name)
2232 if (namlen >= 0)
2233 mg->mg_ptr = savepvn(name, namlen);
2234 else if (namlen == HEf_SVKEY)
2235 mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
2236
79072805
LW
2237 switch (how) {
2238 case 0:
2239 mg->mg_virtual = &vtbl_sv;
2240 break;
a0d0e21e
LW
2241#ifdef OVERLOAD
2242 case 'A':
2243 mg->mg_virtual = &vtbl_amagic;
2244 break;
2245 case 'a':
2246 mg->mg_virtual = &vtbl_amagicelem;
2247 break;
2248 case 'c':
2249 mg->mg_virtual = 0;
2250 break;
2251#endif /* OVERLOAD */
79072805
LW
2252 case 'B':
2253 mg->mg_virtual = &vtbl_bm;
2254 break;
79072805
LW
2255 case 'E':
2256 mg->mg_virtual = &vtbl_env;
2257 break;
55497cff
PP
2258 case 'f':
2259 mg->mg_virtual = &vtbl_fm;
2260 break;
79072805
LW
2261 case 'e':
2262 mg->mg_virtual = &vtbl_envelem;
2263 break;
93a17b20
LW
2264 case 'g':
2265 mg->mg_virtual = &vtbl_mglob;
2266 break;
463ee0b2
LW
2267 case 'I':
2268 mg->mg_virtual = &vtbl_isa;
2269 break;
2270 case 'i':
2271 mg->mg_virtual = &vtbl_isaelem;
2272 break;
16660edb
PP
2273 case 'k':
2274 mg->mg_virtual = &vtbl_nkeys;
2275 break;
79072805 2276 case 'L':
a0d0e21e 2277 SvRMAGICAL_on(sv);
93a17b20
LW
2278 mg->mg_virtual = 0;
2279 break;
2280 case 'l':
79072805
LW
2281 mg->mg_virtual = &vtbl_dbline;
2282 break;
bbce6d69
PP
2283#ifdef HAS_STRXFRM
2284 case 'o':
2285 mg->mg_virtual = &vtbl_collxfrm;
2286 break;
2287#endif
463ee0b2
LW
2288 case 'P':
2289 mg->mg_virtual = &vtbl_pack;
2290 break;
2291 case 'p':
a0d0e21e 2292 case 'q':
463ee0b2
LW
2293 mg->mg_virtual = &vtbl_packelem;
2294 break;
79072805
LW
2295 case 'S':
2296 mg->mg_virtual = &vtbl_sig;
2297 break;
2298 case 's':
2299 mg->mg_virtual = &vtbl_sigelem;
2300 break;
463ee0b2
LW
2301 case 't':
2302 mg->mg_virtual = &vtbl_taint;
748a9306 2303 mg->mg_len = 1;
463ee0b2 2304 break;
79072805
LW
2305 case 'U':
2306 mg->mg_virtual = &vtbl_uvar;
2307 break;
2308 case 'v':
2309 mg->mg_virtual = &vtbl_vec;
2310 break;
2311 case 'x':
2312 mg->mg_virtual = &vtbl_substr;
2313 break;
2314 case '*':
2315 mg->mg_virtual = &vtbl_glob;
2316 break;
2317 case '#':
2318 mg->mg_virtual = &vtbl_arylen;
2319 break;
a0d0e21e
LW
2320 case '.':
2321 mg->mg_virtual = &vtbl_pos;
2322 break;
4633a7c4
LW
2323 case '~': /* Reserved for use by extensions not perl internals. */
2324 /* Useful for attaching extension internal data to perl vars. */
2325 /* Note that multiple extensions may clash if magical scalars */
2326 /* etc holding private data from one are passed to another. */
2327 SvRMAGICAL_on(sv);
a0d0e21e 2328 break;
79072805 2329 default:
463ee0b2
LW
2330 croak("Don't know how to handle magic of type '%c'", how);
2331 }
8990e307
LW
2332 mg_magical(sv);
2333 if (SvGMAGICAL(sv))
2334 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
463ee0b2
LW
2335}
2336
2337int
2338sv_unmagic(sv, type)
2339SV* sv;
a0d0e21e 2340int type;
463ee0b2
LW
2341{
2342 MAGIC* mg;
2343 MAGIC** mgp;
91bba347 2344 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
463ee0b2
LW
2345 return 0;
2346 mgp = &SvMAGIC(sv);
2347 for (mg = *mgp; mg; mg = *mgp) {
2348 if (mg->mg_type == type) {
2349 MGVTBL* vtbl = mg->mg_virtual;
2350 *mgp = mg->mg_moremagic;
2351 if (vtbl && vtbl->svt_free)
2352 (*vtbl->svt_free)(sv, mg);
2353 if (mg->mg_ptr && mg->mg_type != 'g')
1edc1566
PP
2354 if (mg->mg_len >= 0)
2355 Safefree(mg->mg_ptr);
2356 else if (mg->mg_len == HEf_SVKEY)
2357 SvREFCNT_dec((SV*)mg->mg_ptr);
a0d0e21e
LW
2358 if (mg->mg_flags & MGf_REFCOUNTED)
2359 SvREFCNT_dec(mg->mg_obj);
463ee0b2
LW
2360 Safefree(mg);
2361 }
2362 else
2363 mgp = &mg->mg_moremagic;
79072805 2364 }
91bba347 2365 if (!SvMAGIC(sv)) {
463ee0b2 2366 SvMAGICAL_off(sv);
8990e307 2367 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
463ee0b2
LW
2368 }
2369
2370 return 0;
79072805
LW
2371}
2372
2373void
2374sv_insert(bigstr,offset,len,little,littlelen)
2375SV *bigstr;
2376STRLEN offset;
2377STRLEN len;
2378char *little;
2379STRLEN littlelen;
2380{
2381 register char *big;
2382 register char *mid;
2383 register char *midend;
2384 register char *bigend;
2385 register I32 i;
2386
8990e307
LW
2387 if (!bigstr)
2388 croak("Can't modify non-existent substring");
a0d0e21e 2389 SvPV_force(bigstr, na);
79072805
LW
2390
2391 i = littlelen - len;
2392 if (i > 0) { /* string might grow */
a0d0e21e 2393 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
79072805
LW
2394 mid = big + offset + len;
2395 midend = bigend = big + SvCUR(bigstr);
2396 bigend += i;
2397 *bigend = '\0';
2398 while (midend > mid) /* shove everything down */
2399 *--bigend = *--midend;
2400 Move(little,big+offset,littlelen,char);
2401 SvCUR(bigstr) += i;
2402 SvSETMAGIC(bigstr);
2403 return;
2404 }
2405 else if (i == 0) {
463ee0b2 2406 Move(little,SvPVX(bigstr)+offset,len,char);
79072805
LW
2407 SvSETMAGIC(bigstr);
2408 return;
2409 }
2410
463ee0b2 2411 big = SvPVX(bigstr);
79072805
LW
2412 mid = big + offset;
2413 midend = mid + len;
2414 bigend = big + SvCUR(bigstr);
2415
2416 if (midend > bigend)
463ee0b2 2417 croak("panic: sv_insert");
79072805
LW
2418
2419 if (mid - big > bigend - midend) { /* faster to shorten from end */
2420 if (littlelen) {
2421 Move(little, mid, littlelen,char);
2422 mid += littlelen;
2423 }
2424 i = bigend - midend;
2425 if (i > 0) {
2426 Move(midend, mid, i,char);
2427 mid += i;
2428 }
2429 *mid = '\0';
2430 SvCUR_set(bigstr, mid - big);
2431 }
2432 /*SUPPRESS 560*/
2433 else if (i = mid - big) { /* faster from front */
2434 midend -= littlelen;
2435 mid = midend;
2436 sv_chop(bigstr,midend-i);
2437 big += i;
2438 while (i--)
2439 *--midend = *--big;
2440 if (littlelen)
2441 Move(little, mid, littlelen,char);
2442 }
2443 else if (littlelen) {
2444 midend -= littlelen;
2445 sv_chop(bigstr,midend);
2446 Move(little,midend,littlelen,char);
2447 }
2448 else {
2449 sv_chop(bigstr,midend);
2450 }
2451 SvSETMAGIC(bigstr);
2452}
2453
2454/* make sv point to what nstr did */
2455
2456void
2457sv_replace(sv,nsv)
2458register SV *sv;
2459register SV *nsv;
2460{
2461 U32 refcnt = SvREFCNT(sv);
ed6116ce 2462 if (SvTHINKFIRST(sv)) {
8990e307 2463 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
2464 croak(no_modify);
2465 if (SvROK(sv))
2466 sv_unref(sv);
2467 }
79072805
LW
2468 if (SvREFCNT(nsv) != 1)
2469 warn("Reference miscount in sv_replace()");
93a17b20 2470 if (SvMAGICAL(sv)) {
a0d0e21e
LW
2471 if (SvMAGICAL(nsv))
2472 mg_free(nsv);
2473 else
2474 sv_upgrade(nsv, SVt_PVMG);
93a17b20 2475 SvMAGIC(nsv) = SvMAGIC(sv);
a0d0e21e 2476 SvFLAGS(nsv) |= SvMAGICAL(sv);
93a17b20
LW
2477 SvMAGICAL_off(sv);
2478 SvMAGIC(sv) = 0;
2479 }
79072805
LW
2480 SvREFCNT(sv) = 0;
2481 sv_clear(sv);
2482 StructCopy(nsv,sv,SV);
2483 SvREFCNT(sv) = refcnt;
1edc1566 2484 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
463ee0b2 2485 del_SV(nsv);
79072805
LW
2486}
2487
2488void
2489sv_clear(sv)
2490register SV *sv;
2491{
2492 assert(sv);
2493 assert(SvREFCNT(sv) == 0);
2494
ed6116ce 2495 if (SvOBJECT(sv)) {
463ee0b2 2496 dSP;
463ee0b2
LW
2497 GV* destructor;
2498
a0d0e21e
LW
2499 if (defstash) { /* Still have a symbol table? */
2500 destructor = gv_fetchmethod(SvSTASH(sv), "DESTROY");
2501
2502 ENTER;
2503 SAVEFREESV(SvSTASH(sv));
2504 if (destructor && GvCV(destructor)) {
2505 SV ref;
2506
2507 Zero(&ref, 1, SV);
2508 sv_upgrade(&ref, SVt_RV);
a0d0e21e
LW
2509 SvRV(&ref) = SvREFCNT_inc(sv);
2510 SvROK_on(&ref);
2511
2512 EXTEND(SP, 2);
2513 PUSHMARK(SP);
2514 PUSHs(&ref);
2515 PUTBACK;
4633a7c4 2516 perl_call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR);
748a9306 2517 del_XRV(SvANY(&ref));
1edc1566 2518 SvREFCNT(sv)--;
a0d0e21e
LW
2519 }
2520 LEAVE;
2521 }
4633a7c4
LW
2522 else
2523 SvREFCNT_dec(SvSTASH(sv));
a0d0e21e
LW
2524 if (SvOBJECT(sv)) {
2525 SvOBJECT_off(sv); /* Curse the object. */
2526 if (SvTYPE(sv) != SVt_PVIO)
2527 --sv_objcount; /* XXX Might want something more general */
2528 }
1edc1566
PP
2529 if (SvREFCNT(sv)) {
2530 SV *ret;
2531 if ( perldb
2532 && (ret = perl_get_sv("DB::ret", FALSE))
2533 && SvROK(ret) && SvRV(ret) == sv && SvREFCNT(sv) == 1) {
2534 /* Debugger is prone to dangling references. */
2535 SvRV(ret) = 0;
2536 SvROK_off(ret);
2537 SvREFCNT(sv) = 0;
2538 } else {
2539 croak("panic: dangling references in DESTROY");
2540 }
2541 }
463ee0b2 2542 }
c07a80fd 2543 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
a0d0e21e 2544 mg_free(sv);
79072805 2545 switch (SvTYPE(sv)) {
8990e307 2546 case SVt_PVIO:
91bba347 2547 io_close((IO*)sv);
8990e307
LW
2548 Safefree(IoTOP_NAME(sv));
2549 Safefree(IoFMT_NAME(sv));
2550 Safefree(IoBOTTOM_NAME(sv));
a0d0e21e 2551 /* FALL THROUGH */
79072805 2552 case SVt_PVBM:
a0d0e21e 2553 goto freescalar;
79072805 2554 case SVt_PVCV:
748a9306 2555 case SVt_PVFM:
85e6fe83 2556 cv_undef((CV*)sv);
a0d0e21e 2557 goto freescalar;
79072805 2558 case SVt_PVHV:
85e6fe83 2559 hv_undef((HV*)sv);
a0d0e21e 2560 break;
79072805 2561 case SVt_PVAV:
85e6fe83 2562 av_undef((AV*)sv);
a0d0e21e
LW
2563 break;
2564 case SVt_PVGV:
1edc1566 2565 gp_free((GV*)sv);
a0d0e21e
LW
2566 Safefree(GvNAME(sv));
2567 /* FALL THROUGH */
79072805 2568 case SVt_PVLV:
79072805 2569 case SVt_PVMG:
79072805
LW
2570 case SVt_PVNV:
2571 case SVt_PVIV:
a0d0e21e
LW
2572 freescalar:
2573 (void)SvOOK_off(sv);
79072805
LW
2574 /* FALL THROUGH */
2575 case SVt_PV:
a0d0e21e 2576 case SVt_RV:
8990e307
LW
2577 if (SvROK(sv))
2578 SvREFCNT_dec(SvRV(sv));
1edc1566 2579 else if (SvPVX(sv) && SvLEN(sv))
463ee0b2 2580 Safefree(SvPVX(sv));
79072805 2581 break;
a0d0e21e 2582/*
79072805 2583 case SVt_NV:
79072805 2584 case SVt_IV:
79072805
LW
2585 case SVt_NULL:
2586 break;
a0d0e21e 2587*/
79072805
LW
2588 }
2589
2590 switch (SvTYPE(sv)) {
2591 case SVt_NULL:
2592 break;
79072805
LW
2593 case SVt_IV:
2594 del_XIV(SvANY(sv));
2595 break;
2596 case SVt_NV:
2597 del_XNV(SvANY(sv));
2598 break;
ed6116ce
LW
2599 case SVt_RV:
2600 del_XRV(SvANY(sv));
2601 break;
79072805
LW
2602 case SVt_PV:
2603 del_XPV(SvANY(sv));
2604 break;
2605 case SVt_PVIV:
2606 del_XPVIV(SvANY(sv));
2607 break;
2608 case SVt_PVNV:
2609 del_XPVNV(SvANY(sv));
2610 break;
2611 case SVt_PVMG:
2612 del_XPVMG(SvANY(sv));
2613 break;
2614 case SVt_PVLV:
2615 del_XPVLV(SvANY(sv));
2616 break;
2617 case SVt_PVAV:
2618 del_XPVAV(SvANY(sv));
2619 break;
2620 case SVt_PVHV:
2621 del_XPVHV(SvANY(sv));
2622 break;
2623 case SVt_PVCV:
2624 del_XPVCV(SvANY(sv));
2625 break;
2626 case SVt_PVGV:
2627 del_XPVGV(SvANY(sv));
2628 break;
2629 case SVt_PVBM:
2630 del_XPVBM(SvANY(sv));
2631 break;
2632 case SVt_PVFM:
2633 del_XPVFM(SvANY(sv));
2634 break;
8990e307
LW
2635 case SVt_PVIO:
2636 del_XPVIO(SvANY(sv));
2637 break;
79072805 2638 }
a0d0e21e 2639 SvFLAGS(sv) &= SVf_BREAK;
8990e307 2640 SvFLAGS(sv) |= SVTYPEMASK;
79072805
LW
2641}
2642
2643SV *
8990e307 2644sv_newref(sv)
79072805
LW
2645SV* sv;
2646{
463ee0b2
LW
2647 if (sv)
2648 SvREFCNT(sv)++;
79072805
LW
2649 return sv;
2650}
2651
2652void
2653sv_free(sv)
2654SV *sv;
2655{
2656 if (!sv)
2657 return;
a0d0e21e
LW
2658 if (SvREADONLY(sv)) {
2659 if (sv == &sv_undef || sv == &sv_yes || sv == &sv_no)
2660 return;
79072805 2661 }
a0d0e21e
LW
2662 if (SvREFCNT(sv) == 0) {
2663 if (SvFLAGS(sv) & SVf_BREAK)
2664 return;
1edc1566
PP
2665 if (in_clean_all) /* All is fair */
2666 return;
79072805
LW
2667 warn("Attempt to free unreferenced scalar");
2668 return;
2669 }
8990e307
LW
2670 if (--SvREFCNT(sv) > 0)
2671 return;
463ee0b2
LW
2672#ifdef DEBUGGING
2673 if (SvTEMP(sv)) {
2674 warn("Attempt to free temp prematurely");
79072805 2675 return;
79072805 2676 }
463ee0b2 2677#endif
79072805 2678 sv_clear(sv);
79072805
LW
2679 del_SV(sv);
2680}
2681
2682STRLEN
2683sv_len(sv)
2684register SV *sv;
2685{
748a9306 2686 char *junk;
463ee0b2 2687 STRLEN len;
79072805
LW
2688
2689 if (!sv)
2690 return 0;
2691
8990e307
LW
2692 if (SvGMAGICAL(sv))
2693 len = mg_len(sv);
2694 else
748a9306 2695 junk = SvPV(sv, len);
463ee0b2 2696 return len;
79072805
LW
2697}
2698
2699I32
2700sv_eq(str1,str2)
2701register SV *str1;
2702register SV *str2;
2703{
2704 char *pv1;
463ee0b2 2705 STRLEN cur1;
79072805 2706 char *pv2;
463ee0b2 2707 STRLEN cur2;
79072805
LW
2708
2709 if (!str1) {
2710 pv1 = "";
2711 cur1 = 0;
2712 }
463ee0b2
LW
2713 else
2714 pv1 = SvPV(str1, cur1);
79072805
LW
2715
2716 if (!str2)
2717 return !cur1;
463ee0b2
LW
2718 else
2719 pv2 = SvPV(str2, cur2);
79072805
LW
2720
2721 if (cur1 != cur2)
2722 return 0;
2723
a026971d 2724 return !memcmp(pv1, pv2, cur1);
79072805
LW
2725}
2726
2727I32
bbce6d69 2728sv_cmp(str1, str2)
79072805
LW
2729register SV *str1;
2730register SV *str2;
2731{
bbce6d69
PP
2732 STRLEN cur1 = 0;
2733 char *pv1 = str1 ? SvPV(str1, cur1) : NULL;
2734 STRLEN cur2 = 0;
2735 char *pv2 = str2 ? SvPV(str2, cur2) : NULL;
79072805 2736 I32 retval;
79072805 2737
bbce6d69
PP
2738 if (!cur1)
2739 return cur2 ? -1 : 0;
16660edb 2740
bbce6d69
PP
2741 if (!cur2)
2742 return 1;
79072805 2743
bbce6d69 2744 retval = memcmp((void*)pv1, (void*)pv2, cur1 < cur2 ? cur1 : cur2);
16660edb 2745
bbce6d69
PP
2746 if (retval)
2747 return retval < 0 ? -1 : 1;
16660edb 2748
bbce6d69
PP
2749 if (cur1 == cur2)
2750 return 0;
2751 else
2752 return cur1 < cur2 ? -1 : 1;
2753}
16660edb 2754
bbce6d69
PP
2755I32
2756sv_cmp_locale(sv1, sv2)
2757register SV *sv1;
2758register SV *sv2;
2759{
2760#ifdef LC_COLLATE
16660edb 2761
bbce6d69
PP
2762 char *pv1, *pv2;
2763 STRLEN len1, len2;
2764 I32 retval;
16660edb 2765
bbce6d69
PP
2766 if (collation_standard)
2767 goto raw_compare;
16660edb 2768
bbce6d69
PP
2769 len1 = 0;
2770 pv1 = sv1 ? sv_collxfrm(sv1, &len1) : NULL;
2771 len2 = 0;
2772 pv2 = sv2 ? sv_collxfrm(sv2, &len2) : NULL;
16660edb 2773
bbce6d69
PP
2774 if (!pv1 || !len1) {
2775 if (pv2 && len2)
2776 return -1;
2777 else
2778 goto raw_compare;
2779 }
2780 else {
2781 if (!pv2 || !len2)
2782 return 1;
2783 }
16660edb 2784
bbce6d69 2785 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
16660edb 2786
bbce6d69 2787 if (retval)
16660edb
PP
2788 return retval < 0 ? -1 : 1;
2789
bbce6d69
PP
2790 /*
2791 * When the result of collation is equality, that doesn't mean
2792 * that there are no differences -- some locales exclude some
2793 * characters from consideration. So to avoid false equalities,
2794 * we use the raw string as a tiebreaker.
2795 */
16660edb 2796
bbce6d69
PP
2797 raw_compare:
2798 /* FALL THROUGH */
16660edb 2799
bbce6d69 2800#endif /* LC_COLLATE */
16660edb 2801
bbce6d69
PP
2802 return sv_cmp(sv1, sv2);
2803}
79072805 2804
bbce6d69 2805#ifdef LC_COLLATE
16660edb 2806
bbce6d69
PP
2807char *
2808sv_collxfrm(sv, nxp)
2809 SV *sv;
2810 STRLEN *nxp;
2811{
2812 /* Any scalar variable may carry an 'o' magic that contains the
2813 * scalar data of the variable transformed to such a format that
2814 * a normal memcmp() can be used to compare the data according
2815 * to the locale settings. */
79072805 2816
bbce6d69 2817 MAGIC *mg = NULL;
16660edb 2818
bbce6d69
PP
2819 if (SvMAGICAL(sv)) {
2820 mg = mg_find(sv, 'o');
2821 if (mg && *(U32*)mg->mg_ptr != collation_ix)
2822 mg = NULL;
2823 }
16660edb 2824
bbce6d69
PP
2825 if (! mg) {
2826 char *s, *xf;
2827 STRLEN len, xlen;
2828
2829 s = SvPV(sv, len);
2830 if ((xf = mem_collxfrm(s, len, &xlen))) {
2831 sv_magic(sv, 0, 'o', 0, 0);
2832 if ((mg = mg_find(sv, 'o'))) {
2833 mg->mg_ptr = xf;
2834 mg->mg_len = xlen;
2835 }
2836 }
2837 }
2838
2839 if (mg) {
2840 *nxp = mg->mg_len;
2841 return mg->mg_ptr + sizeof(collation_ix);
2842 }
2843 else {
2844 *nxp = 0;
2845 return NULL;
16660edb 2846 }
79072805
LW
2847}
2848
bbce6d69
PP
2849#endif /* LC_COLLATE */
2850
79072805
LW
2851char *
2852sv_gets(sv,fp,append)
2853register SV *sv;
760ac839 2854register PerlIO *fp;
79072805
LW
2855I32 append;
2856{
c07a80fd
PP
2857 char *rsptr;
2858 STRLEN rslen;
2859 register STDCHAR rslast;
2860 register STDCHAR *bp;
2861 register I32 cnt;
2862 I32 i;
2863
ed6116ce 2864 if (SvTHINKFIRST(sv)) {
8990e307 2865 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
2866 croak(no_modify);
2867 if (SvROK(sv))
2868 sv_unref(sv);
2869 }
79072805 2870 if (!SvUPGRADE(sv, SVt_PV))
a0d0e21e 2871 return 0;
ff68c719 2872 SvSCREAM_off(sv);
c07a80fd
PP
2873
2874 if (RsSNARF(rs)) {
2875 rsptr = NULL;
2876 rslen = 0;
2877 }
2878 else if (RsPARA(rs)) {
2879 rsptr = "\n\n";
2880 rslen = 2;
2881 }
2882 else
2883 rsptr = SvPV(rs, rslen);
2884 rslast = rslen ? rsptr[rslen - 1] : '\0';
2885
2886 if (RsPARA(rs)) { /* have to do this both before and after */
79072805 2887 do { /* to make sure file boundaries work right */
760ac839 2888 if (PerlIO_eof(fp))
a0d0e21e 2889 return 0;
760ac839 2890 i = PerlIO_getc(fp);
79072805 2891 if (i != '\n') {
a0d0e21e
LW
2892 if (i == -1)
2893 return 0;
760ac839 2894 PerlIO_ungetc(fp,i);
79072805
LW
2895 break;
2896 }
2897 } while (i != EOF);
2898 }
c07a80fd 2899
760ac839
LW
2900 /* See if we know enough about I/O mechanism to cheat it ! */
2901
2902 /* This used to be #ifdef test - it is made run-time test for ease
2903 of abstracting out stdio interface. One call should be cheap
2904 enough here - and may even be a macro allowing compile
2905 time optimization.
2906 */
2907
2908 if (PerlIO_fast_gets(fp)) {
2909
2910 /*
2911 * We're going to steal some values from the stdio struct
2912 * and put EVERYTHING in the innermost loop into registers.
2913 */
2914 register STDCHAR *ptr;
2915 STRLEN bpx;
2916 I32 shortbuffered;
2917
16660edb
PP
2918#if defined(VMS) && defined(PERLIO_IS_STDIO)
2919 /* An ungetc()d char is handled separately from the regular
2920 * buffer, so we getc() it back out and stuff it in the buffer.
2921 */
2922 i = PerlIO_getc(fp);
2923 if (i == EOF) return 0;
2924 *(--((*fp)->_ptr)) = (unsigned char) i;
2925 (*fp)->_cnt++;
2926#endif
c07a80fd 2927
c2960299 2928 /* Here is some breathtakingly efficient cheating */
c07a80fd 2929
760ac839 2930 cnt = PerlIO_get_cnt(fp); /* get count into register */
a0d0e21e 2931 (void)SvPOK_only(sv); /* validate pointer */
79072805
LW
2932 if (SvLEN(sv) - append <= cnt + 1) { /* make sure we have the room */
2933 if (cnt > 80 && SvLEN(sv) > append) {
2934 shortbuffered = cnt - SvLEN(sv) + append + 1;
2935 cnt -= shortbuffered;
2936 }
2937 else {
2938 shortbuffered = 0;
bbce6d69
PP
2939 /* remember that cnt can be negative */
2940 SvGROW(sv, append + (cnt <= 0 ? 2 : (cnt + 1)));
79072805
LW
2941 }
2942 }
2943 else
2944 shortbuffered = 0;
c07a80fd 2945 bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */
760ac839 2946 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
16660edb
PP
2947 DEBUG_P(PerlIO_printf(Perl_debug_log,
2948 "Screamer: entering, ptr=%d, cnt=%d\n",ptr,cnt));
2949 DEBUG_P(PerlIO_printf(Perl_debug_log,
2950 "Screamer: entering: FILE * thinks ptr=%d, cnt=%d, base=%d\n",
bbce6d69
PP
2951 PerlIO_get_ptr(fp), PerlIO_get_cnt(fp),
2952 PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0));
79072805
LW
2953 for (;;) {
2954 screamer:
93a17b20 2955 if (cnt > 0) {
c07a80fd 2956 if (rslen) {
760ac839
LW
2957 while (cnt > 0) { /* this | eat */
2958 cnt--;
c07a80fd
PP
2959 if ((*bp++ = *ptr++) == rslast) /* really | dust */
2960 goto thats_all_folks; /* screams | sed :-) */
2961 }
2962 }
2963 else {
2964 memcpy((char*)bp, (char*)ptr, cnt); /* this | eat */
2965 bp += cnt; /* screams | dust */
2966 ptr += cnt; /* louder | sed :-) */
a5f75d66 2967 cnt = 0;
93a17b20 2968 }
79072805
LW
2969 }
2970
748a9306 2971 if (shortbuffered) { /* oh well, must extend */
79072805
LW
2972 cnt = shortbuffered;
2973 shortbuffered = 0;
c07a80fd 2974 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
79072805
LW
2975 SvCUR_set(sv, bpx);
2976 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
c07a80fd 2977 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
79072805
LW
2978 continue;
2979 }
2980
16660edb
PP
2981 DEBUG_P(PerlIO_printf(Perl_debug_log,
2982 "Screamer: going to getc, ptr=%d, cnt=%d\n",ptr,cnt));
d1bf51dd 2983 PerlIO_set_ptrcnt(fp, ptr, cnt); /* deregisterize cnt and ptr */
16660edb
PP
2984 DEBUG_P(PerlIO_printf(Perl_debug_log,
2985 "Screamer: pre: FILE * thinks ptr=%d, cnt=%d, base=%d\n",
bbce6d69
PP
2986 PerlIO_get_ptr(fp), PerlIO_get_cnt(fp),
2987 PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0));
16660edb
PP
2988 /* This used to call 'filbuf' in stdio form, but as that behaves like
2989 getc when cnt <= 0 we use PerlIO_getc here to avoid another
2990 abstraction. This may also avoid issues with different named
2991 'filbuf' equivalents, though Configure tries to handle them now
2992 anyway.
760ac839
LW
2993 */
2994 i = PerlIO_getc(fp); /* get more characters */
16660edb
PP
2995 DEBUG_P(PerlIO_printf(Perl_debug_log,
2996 "Screamer: post: FILE * thinks ptr=%d, cnt=%d, base=%d\n",
bbce6d69
PP
2997 PerlIO_get_ptr(fp), PerlIO_get_cnt(fp),
2998 PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0));
760ac839
LW
2999 cnt = PerlIO_get_cnt(fp);
3000 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
16660edb
PP
3001 DEBUG_P(PerlIO_printf(Perl_debug_log,
3002 "Screamer: after getc, ptr=%d, cnt=%d\n",ptr,cnt));
79072805 3003
748a9306
LW
3004 if (i == EOF) /* all done for ever? */
3005 goto thats_really_all_folks;
3006
c07a80fd 3007 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
79072805
LW
3008 SvCUR_set(sv, bpx);
3009 SvGROW(sv, bpx + cnt + 2);
c07a80fd
PP
3010 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
3011
760ac839 3012 *bp++ = i; /* store character from PerlIO_getc */
79072805 3013
c07a80fd 3014 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
79072805 3015 goto thats_all_folks;
79072805
LW
3016 }
3017
3018thats_all_folks:
c07a80fd 3019 if ((rslen > 1 && (bp - (STDCHAR*)SvPVX(sv) < rslen)) ||
a026971d 3020 memcmp((char*)bp - rslen, rsptr, rslen))
760ac839 3021 goto screamer; /* go back to the fray */
79072805
LW
3022thats_really_all_folks:
3023 if (shortbuffered)
3024 cnt += shortbuffered;
16660edb
PP
3025 DEBUG_P(PerlIO_printf(Perl_debug_log,
3026 "Screamer: quitting, ptr=%d, cnt=%d\n",ptr,cnt));
d1bf51dd 3027 PerlIO_set_ptrcnt(fp, ptr, cnt); /* put these back or we're in trouble */
16660edb
PP
3028 DEBUG_P(PerlIO_printf(Perl_debug_log,
3029 "Screamer: end: FILE * thinks ptr=%d, cnt=%d, base=%d\n",
bbce6d69
PP
3030 PerlIO_get_ptr(fp), PerlIO_get_cnt(fp),
3031 PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0));
79072805 3032 *bp = '\0';
760ac839 3033 SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */
16660edb
PP
3034 DEBUG_P(PerlIO_printf(Perl_debug_log,
3035 "Screamer: done, len=%d, string=|%.*s|\n",
3036 SvCUR(sv),SvCUR(sv),SvPVX(sv)));
760ac839
LW
3037 }
3038 else
79072805 3039 {
760ac839 3040 /*The big, slow, and stupid way */
c07a80fd 3041 STDCHAR buf[8192];
79072805 3042
760ac839 3043screamer2:
c07a80fd 3044 if (rslen) {
760ac839
LW
3045 register STDCHAR *bpe = buf + sizeof(buf);
3046 bp = buf;
3047 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = i) != rslast && bp < bpe)
3048 ; /* keep reading */
3049 cnt = bp - buf;
c07a80fd
PP
3050 }
3051 else {
760ac839 3052 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
16660edb
PP
3053 /* Accomodate broken VAXC compiler, which applies U8 cast to
3054 * both args of ?: operator, causing EOF to change into 255
3055 */
3056 if (cnt) { i = (U8)buf[cnt - 1]; } else { i = EOF; }
c07a80fd 3057 }
79072805
LW
3058
3059 if (append)
760ac839 3060 sv_catpvn(sv, (char *) buf, cnt);
79072805 3061 else
760ac839 3062 sv_setpvn(sv, (char *) buf, cnt);
c07a80fd
PP
3063
3064 if (i != EOF && /* joy */
3065 (!rslen ||
3066 SvCUR(sv) < rslen ||
a026971d 3067 memcmp(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
79072805
LW
3068 {
3069 append = -1;
760ac839 3070 goto screamer2;
79072805
LW
3071 }
3072 }
3073
c07a80fd
PP
3074 if (RsPARA(rs)) { /* have to do this both before and after */
3075 while (i != EOF) { /* to make sure file boundaries work right */
760ac839 3076 i = PerlIO_getc(fp);
79072805 3077 if (i != '\n') {
760ac839 3078 PerlIO_ungetc(fp,i);
79072805
LW
3079 break;
3080 }
3081 }
3082 }
c07a80fd
PP
3083
3084 return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch;
79072805
LW
3085}
3086
760ac839 3087
79072805
LW
3088void
3089sv_inc(sv)
3090register SV *sv;
3091{
3092 register char *d;
463ee0b2 3093 int flags;
79072805
LW
3094
3095 if (!sv)
3096 return;
ed6116ce 3097 if (SvTHINKFIRST(sv)) {
8990e307 3098 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce 3099 croak(no_modify);
a0d0e21e
LW
3100 if (SvROK(sv)) {
3101#ifdef OVERLOAD
3102 if (SvAMAGIC(sv) && AMG_CALLun(sv,inc)) return;
3103#endif /* OVERLOAD */
3104 sv_unref(sv);
3105 }
ed6116ce 3106 }
8990e307 3107 if (SvGMAGICAL(sv))
79072805 3108 mg_get(sv);
8990e307 3109 flags = SvFLAGS(sv);
8990e307 3110 if (flags & SVp_NOK) {
a0d0e21e 3111 (void)SvNOK_only(sv);
55497cff
PP
3112 SvNVX(sv) += 1.0;
3113 return;
3114 }
3115 if (flags & SVp_IOK) {
3116 if (SvIVX(sv) == IV_MAX)
3117 sv_setnv(sv, (double)IV_MAX + 1.0);
3118 else {
3119 (void)SvIOK_only(sv);
3120 ++SvIVX(sv);
3121 }
79072805
LW
3122 return;
3123 }
8990e307 3124 if (!(flags & SVp_POK) || !*SvPVX(sv)) {
4633a7c4
LW
3125 if ((flags & SVTYPEMASK) < SVt_PVNV)
3126 sv_upgrade(sv, SVt_NV);
463ee0b2 3127 SvNVX(sv) = 1.0;
a0d0e21e 3128 (void)SvNOK_only(sv);
79072805
LW
3129 return;
3130 }
463ee0b2 3131 d = SvPVX(sv);
79072805
LW
3132 while (isALPHA(*d)) d++;
3133 while (isDIGIT(*d)) d++;
3134 if (*d) {
bbce6d69
PP
3135 NUMERIC_STANDARD();
3136 sv_setnv(sv,atof(SvPVX(sv)) + 1.0); /* punt */
79072805
LW
3137 return;
3138 }
3139 d--;
463ee0b2 3140 while (d >= SvPVX(sv)) {
79072805
LW
3141 if (isDIGIT(*d)) {
3142 if (++*d <= '9')
3143 return;
3144 *(d--) = '0';
3145 }
3146 else {
3147 ++*d;
3148 if (isALPHA(*d))
3149 return;
3150 *(d--) -= 'z' - 'a' + 1;
3151 }
3152 }
3153 /* oh,oh, the number grew */
3154 SvGROW(sv, SvCUR(sv) + 2);
3155 SvCUR(sv)++;
463ee0b2 3156 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--)
79072805
LW
3157 *d = d[-1];
3158 if (isDIGIT(d[1]))
3159 *d = '1';
3160 else
3161 *d = d[1];
3162}
3163
3164void
3165sv_dec(sv)
3166register SV *sv;
3167{
463ee0b2
LW
3168 int flags;
3169
79072805
LW
3170 if (!sv)
3171 return;
ed6116ce 3172 if (SvTHINKFIRST(sv)) {
8990e307 3173 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce 3174 croak(no_modify);
a0d0e21e
LW
3175 if (SvROK(sv)) {
3176#ifdef OVERLOAD
3177 if (SvAMAGIC(sv) && AMG_CALLun(sv,dec)) return;
3178#endif /* OVERLOAD */
3179 sv_unref(sv);
3180 }
ed6116ce 3181 }
8990e307 3182 if (SvGMAGICAL(sv))
79072805 3183 mg_get(sv);
8990e307 3184 flags = SvFLAGS(sv);
8990e307 3185 if (flags & SVp_NOK) {
463ee0b2 3186 SvNVX(sv) -= 1.0;
a0d0e21e 3187 (void)SvNOK_only(sv);
79072805
LW
3188 return;
3189 }
55497cff
PP
3190 if (flags & SVp_IOK) {
3191 if (SvIVX(sv) == IV_MIN)
3192 sv_setnv(sv, (double)IV_MIN - 1.0);
3193 else {
3194 (void)SvIOK_only(sv);
3195 --SvIVX(sv);
3196 }
3197 return;
3198 }
8990e307 3199 if (!(flags & SVp_POK)) {
4633a7c4
LW
3200 if ((flags & SVTYPEMASK) < SVt_PVNV)
3201 sv_upgrade(sv, SVt_NV);
463ee0b2 3202 SvNVX(sv) = -1.0;
a0d0e21e 3203 (void)SvNOK_only(sv);
79072805
LW
3204 return;
3205 }
bbce6d69
PP
3206 NUMERIC_STANDARD();
3207 sv_setnv(sv,atof(SvPVX(sv)) - 1.0); /* punt */
79072805
LW
3208}
3209
3210/* Make a string that will exist for the duration of the expression
3211 * evaluation. Actually, it may have to last longer than that, but
3212 * hopefully we won't free it until it has been assigned to a
3213 * permanent location. */
3214
8990e307
LW
3215static void
3216sv_mortalgrow()
3217{
55497cff 3218 tmps_max += (tmps_max < 512) ? 128 : 512;
8990e307
LW
3219 Renew(tmps_stack, tmps_max, SV*);
3220}
3221
79072805
LW
3222SV *
3223sv_mortalcopy(oldstr)
3224SV *oldstr;
3225{
463ee0b2 3226 register SV *sv;
79072805 3227
4561caa4 3228 new_SV(sv);
8990e307
LW
3229 SvANY(sv) = 0;
3230 SvREFCNT(sv) = 1;
3231 SvFLAGS(sv) = 0;
79072805 3232 sv_setsv(sv,oldstr);
8990e307
LW
3233 if (++tmps_ix >= tmps_max)
3234 sv_mortalgrow();
3235 tmps_stack[tmps_ix] = sv;
3236 SvTEMP_on(sv);
3237 return sv;
3238}
3239
3240SV *
3241sv_newmortal()
3242{
3243 register SV *sv;
3244
4561caa4 3245 new_SV(sv);
8990e307
LW
3246 SvANY(sv) = 0;
3247 SvREFCNT(sv) = 1;
3248 SvFLAGS(sv) = SVs_TEMP;
3249 if (++tmps_ix >= tmps_max)
3250 sv_mortalgrow();
79072805 3251 tmps_stack[tmps_ix] = sv;
79072805
LW
3252 return sv;
3253}
3254
3255/* same thing without the copying */
3256
3257SV *
3258sv_2mortal(sv)
3259register SV *sv;
3260{
3261 if (!sv)
3262 return sv;
a0d0e21e
LW
3263 if (SvREADONLY(sv) && curcop != &compiling)
3264 croak(no_modify);
8990e307
LW
3265 if (++tmps_ix >= tmps_max)
3266 sv_mortalgrow();
79072805 3267 tmps_stack[tmps_ix] = sv;
8990e307 3268 SvTEMP_on(sv);
79072805
LW
3269 return sv;
3270}
3271
3272SV *
3273newSVpv(s,len)
3274char *s;
3275STRLEN len;
3276{
463ee0b2 3277 register SV *sv;
79072805 3278
4561caa4 3279 new_SV(sv);
8990e307
LW
3280 SvANY(sv) = 0;
3281 SvREFCNT(sv) = 1;
3282 SvFLAGS(sv) = 0;
79072805
LW
3283 if (!len)
3284 len = strlen(s);
3285 sv_setpvn(sv,s,len);
3286 return sv;
3287}
3288
3289SV *
3290newSVnv(n)
3291double n;
3292{
463ee0b2 3293 register SV *sv;
79072805 3294
4561caa4 3295 new_SV(sv);
8990e307
LW
3296 SvANY(sv) = 0;
3297 SvREFCNT(sv) = 1;
3298 SvFLAGS(sv) = 0;
79072805
LW
3299 sv_setnv(sv,n);
3300 return sv;
3301}
3302
3303SV *
3304newSViv(i)
a0d0e21e 3305IV i;
79072805 3306{
463ee0b2 3307 register SV *sv;
79072805 3308
4561caa4 3309 new_SV(sv);
8990e307
LW
3310 SvANY(sv) = 0;
3311 SvREFCNT(sv) = 1;
3312 SvFLAGS(sv) = 0;
79072805
LW
3313 sv_setiv(sv,i);
3314 return sv;
3315}
3316
2304df62
AD
3317SV *
3318newRV(ref)
3319SV *ref;
3320{
3321 register SV *sv;
3322
4561caa4 3323 new_SV(sv);
2304df62
AD
3324 SvANY(sv) = 0;
3325 SvREFCNT(sv) = 1;
3326 SvFLAGS(sv) = 0;
3327 sv_upgrade(sv, SVt_RV);
a0d0e21e 3328 SvTEMP_off(ref);
2304df62
AD
3329 SvRV(sv) = SvREFCNT_inc(ref);
3330 SvROK_on(sv);
2304df62
AD
3331 return sv;
3332}
3333
79072805
LW
3334/* make an exact duplicate of old */
3335
3336SV *
3337newSVsv(old)
3338register SV *old;
3339{
463ee0b2 3340 register SV *sv;
79072805
LW
3341
3342 if (!old)
3343 return Nullsv;
8990e307 3344 if (SvTYPE(old) == SVTYPEMASK) {
79072805
LW
3345 warn("semi-panic: attempt to dup freed string");
3346 return Nullsv;
3347 }
4561caa4 3348 new_SV(sv);
8990e307
LW
3349 SvANY(sv) = 0;
3350 SvREFCNT(sv) = 1;
3351 SvFLAGS(sv) = 0;
ff68c719
PP
3352 if (SvTEMP(old)) {
3353 SvTEMP_off(old);
463ee0b2 3354 sv_setsv(sv,old);
ff68c719 3355 SvTEMP_on(old);
79072805
LW
3356 }
3357 else
463ee0b2
LW
3358 sv_setsv(sv,old);
3359 return sv;
79072805
LW
3360}
3361
3362void
3363sv_reset(s,stash)
3364register char *s;
3365HV *stash;
3366{
3367 register HE *entry;
3368 register GV *gv;
3369 register SV *sv;
3370 register I32 i;
3371 register PMOP *pm;
3372 register I32 max;
463ee0b2 3373 char todo[256];
79072805
LW
3374
3375 if (!*s) { /* reset ?? searches */
3376 for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) {
3377 pm->op_pmflags &= ~PMf_USED;
3378 }
3379 return;
3380 }
3381
3382 /* reset variables */
3383
3384 if (!HvARRAY(stash))
3385 return;
463ee0b2
LW
3386
3387 Zero(todo, 256, char);
79072805
LW
3388 while (*s) {
3389 i = *s;
3390 if (s[1] == '-') {
3391 s += 2;
3392 }
3393 max = *s++;
3394 for ( ; i <= max; i++) {
463ee0b2
LW
3395 todo[i] = 1;
3396 }
a0d0e21e 3397 for (i = 0; i <= (I32) HvMAX(stash); i++) {
79072805
LW
3398 for (entry = HvARRAY(stash)[i];
3399 entry;
1edc1566
PP
3400 entry = HeNEXT(entry)) {
3401 if (!todo[(U8)*HeKEY(entry)])
463ee0b2 3402 continue;
1edc1566 3403 gv = (GV*)HeVAL(entry);
79072805 3404 sv = GvSV(gv);
a0d0e21e 3405 (void)SvOK_off(sv);
79072805
LW
3406 if (SvTYPE(sv) >= SVt_PV) {
3407 SvCUR_set(sv, 0);
463ee0b2
LW
3408 SvTAINT(sv);
3409 if (SvPVX(sv) != Nullch)
3410 *SvPVX(sv) = '\0';
79072805
LW
3411 }
3412 if (GvAV(gv)) {
3413 av_clear(GvAV(gv));
3414 }
3415 if (GvHV(gv)) {
a0d0e21e
LW
3416 if (HvNAME(GvHV(gv)))
3417 continue;
463ee0b2 3418 hv_clear(GvHV(gv));
a0d0e21e 3419#ifndef VMS /* VMS has no environ array */
79072805
LW
3420 if (gv == envgv)
3421 environ[0] = Nullch;
a0d0e21e 3422#endif
79072805
LW
3423 }
3424 }
3425 }
3426 }
3427}
3428
79072805
LW
3429CV *
3430sv_2cv(sv, st, gvp, lref)
3431SV *sv;
3432HV **st;
3433GV **gvp;
3434I32 lref;
3435{
3436 GV *gv;
3437 CV *cv;
3438
3439 if (!sv)
93a17b20 3440 return *gvp = Nullgv, Nullcv;
79072805 3441 switch (SvTYPE(sv)) {
79072805
LW
3442 case SVt_PVCV:
3443 *st = CvSTASH(sv);
3444 *gvp = Nullgv;
3445 return (CV*)sv;
3446 case SVt_PVHV:
3447 case SVt_PVAV:
3448 *gvp = Nullgv;
3449 return Nullcv;
8990e307
LW
3450 case SVt_PVGV:
3451 gv = (GV*)sv;
a0d0e21e 3452 *gvp = gv;
8990e307
LW
3453 *st = GvESTASH(gv);
3454 goto fix_gv;
3455
79072805 3456 default:
a0d0e21e
LW
3457 if (SvGMAGICAL(sv))
3458 mg_get(sv);
3459 if (SvROK(sv)) {
3460 cv = (CV*)SvRV(sv);
3461 if (SvTYPE(cv) != SVt_PVCV)
3462 croak("Not a subroutine reference");
3463 *gvp = Nullgv;
3464 *st = CvSTASH(cv);
3465 return cv;
3466 }
79072805
LW
3467 if (isGV(sv))
3468 gv = (GV*)sv;
3469 else
85e6fe83 3470 gv = gv_fetchpv(SvPV(sv, na), lref, SVt_PVCV);
79072805
LW
3471 *gvp = gv;
3472 if (!gv)
3473 return Nullcv;
3474 *st = GvESTASH(gv);
8990e307
LW
3475 fix_gv:
3476 if (lref && !GvCV(gv)) {
4633a7c4 3477 SV *tmpsv;
748a9306 3478 ENTER;
4633a7c4 3479 tmpsv = NEWSV(704,0);
16660edb 3480 gv_efullname3(tmpsv, gv, Nullch);
748a9306 3481 newSUB(start_subparse(),
4633a7c4
LW
3482 newSVOP(OP_CONST, 0, tmpsv),
3483 Nullop,
8990e307 3484 Nullop);
748a9306 3485 LEAVE;
4633a7c4
LW
3486 if (!GvCV(gv))
3487 croak("Unable to create sub named \"%s\"", SvPV(sv,na));
8990e307 3488 }
79072805
LW
3489 return GvCV(gv);
3490 }
3491}
3492
3493#ifndef SvTRUE
3494I32
3495SvTRUE(sv)
3496register SV *sv;
3497{
8990e307
LW
3498 if (!sv)
3499 return 0;
3500 if (SvGMAGICAL(sv))
79072805
LW
3501 mg_get(sv);
3502 if (SvPOK(sv)) {
3503 register XPV* Xpv;
3504 if ((Xpv = (XPV*)SvANY(sv)) &&
3505 (*Xpv->xpv_pv > '0' ||
3506 Xpv->xpv_cur > 1 ||
3507 (Xpv->xpv_cur && *Xpv->xpv_pv != '0')))
3508 return 1;
3509 else
3510 return 0;
3511 }
3512 else {
3513 if (SvIOK(sv))
463ee0b2 3514 return SvIVX(sv) != 0;
79072805
LW
3515 else {
3516 if (SvNOK(sv))
463ee0b2 3517 return SvNVX(sv) != 0.0;
79072805 3518 else
463ee0b2 3519 return sv_2bool(sv);
79072805
LW
3520 }
3521 }
3522}
ff68c719 3523#endif /* !SvTRUE */
79072805 3524
85e6fe83 3525#ifndef SvIV
ff68c719
PP
3526IV
3527SvIV(sv)
3528register SV *sv;
85e6fe83 3529{
ff68c719
PP
3530 if (SvIOK(sv))
3531 return SvIVX(sv);
3532 return sv_2iv(sv);
85e6fe83 3533}
ff68c719 3534#endif /* !SvIV */
85e6fe83 3535
ff68c719
PP
3536#ifndef SvUV
3537UV
3538SvUV(sv)
3539register SV *sv;
3540{
3541 if (SvIOK(sv))
3542 return SvUVX(sv);
3543 return sv_2uv(sv);
3544}
3545#endif /* !SvUV */
85e6fe83 3546
463ee0b2 3547#ifndef SvNV
ff68c719
PP
3548double
3549SvNV(sv)
3550register SV *sv;
79072805 3551{
ff68c719
PP
3552 if (SvNOK(sv))
3553 return SvNVX(sv);
3554 return sv_2nv(sv);
79072805 3555}
ff68c719 3556#endif /* !SvNV */
79072805 3557
463ee0b2 3558#ifdef CRIPPLED_CC
79072805 3559char *
463ee0b2 3560sv_pvn(sv, lp)
79072805 3561SV *sv;
463ee0b2 3562STRLEN *lp;
79072805 3563{
85e6fe83
LW
3564 if (SvPOK(sv)) {
3565 *lp = SvCUR(sv);
a0d0e21e 3566 return SvPVX(sv);
85e6fe83 3567 }
463ee0b2 3568 return sv_2pv(sv, lp);
79072805
LW
3569}
3570#endif
3571
a0d0e21e
LW
3572char *
3573sv_pvn_force(sv, lp)
3574SV *sv;
3575STRLEN *lp;
3576{
3577 char *s;
3578
3579 if (SvREADONLY(sv) && curcop != &compiling)
3580 croak(no_modify);
3581
3582 if (SvPOK(sv)) {
3583 *lp = SvCUR(sv);
3584 }
3585 else {
748a9306 3586 if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) {
4633a7c4 3587 if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV) {
a0d0e21e 3588 sv_unglob(sv);
4633a7c4
LW
3589 s = SvPVX(sv);
3590 *lp = SvCUR(sv);
3591 }
a0d0e21e
LW
3592 else
3593 croak("Can't coerce %s to string in %s", sv_reftype(sv,0),
3594 op_name[op->op_type]);
3595 }
4633a7c4
LW
3596 else
3597 s = sv_2pv(sv, lp);
a0d0e21e
LW
3598 if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */
3599 STRLEN len = *lp;
3600
3601 if (SvROK(sv))
3602 sv_unref(sv);
3603 (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */
3604 SvGROW(sv, len + 1);
3605 Move(s,SvPVX(sv),len,char);
3606 SvCUR_set(sv, len);
3607 *SvEND(sv) = '\0';
3608 }
3609 if (!SvPOK(sv)) {
3610 SvPOK_on(sv); /* validate pointer */
3611 SvTAINT(sv);
760ac839 3612 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2pv(%s)\n",
a0d0e21e
LW
3613 (unsigned long)sv,SvPVX(sv)));
3614 }
3615 }
3616 return SvPVX(sv);
3617}
3618
3619char *
3620sv_reftype(sv, ob)
3621SV* sv;
3622int ob;
3623{
3624 if (ob && SvOBJECT(sv))
3625 return HvNAME(SvSTASH(sv));
3626 else {
3627 switch (SvTYPE(sv)) {
3628 case SVt_NULL:
3629 case SVt_IV:
3630 case SVt_NV:
3631 case SVt_RV:
3632 case SVt_PV:
3633 case SVt_PVIV:
3634 case SVt_PVNV:
3635 case SVt_PVMG:
3636 case SVt_PVBM:
3637 if (SvROK(sv))
3638 return "REF";
3639 else
3640 return "SCALAR";
3641 case SVt_PVLV: return "LVALUE";
3642 case SVt_PVAV: return "ARRAY";
3643 case SVt_PVHV: return "HASH";
3644 case SVt_PVCV: return "CODE";
3645 case SVt_PVGV: return "GLOB";
3646 case SVt_PVFM: return "FORMLINE";
3647 default: return "UNKNOWN";
3648 }
3649 }
3650}
3651
463ee0b2 3652int
85e6fe83
LW
3653sv_isobject(sv)
3654SV *sv;
3655{
3656 if (!SvROK(sv))
3657 return 0;
3658 sv = (SV*)SvRV(sv);
3659 if (!SvOBJECT(sv))
3660 return 0;
3661 return 1;
3662}
3663
3664int
463ee0b2
LW
3665sv_isa(sv, name)
3666SV *sv;
3667char *name;
3668{
ed6116ce 3669 if (!SvROK(sv))
463ee0b2 3670 return 0;
ed6116ce
LW
3671 sv = (SV*)SvRV(sv);
3672 if (!SvOBJECT(sv))
463ee0b2
LW
3673 return 0;
3674
3675 return strEQ(HvNAME(SvSTASH(sv)), name);
3676}
3677
3678SV*
a0d0e21e 3679newSVrv(rv, classname)
463ee0b2 3680SV *rv;
a0d0e21e 3681char *classname;
463ee0b2 3682{
463ee0b2
LW
3683 SV *sv;
3684
4561caa4 3685 new_SV(sv);
8990e307 3686 SvANY(sv) = 0;
a0d0e21e 3687 SvREFCNT(sv) = 0;
8990e307 3688 SvFLAGS(sv) = 0;
ed6116ce 3689 sv_upgrade(rv, SVt_RV);
8990e307 3690 SvRV(rv) = SvREFCNT_inc(sv);
ed6116ce 3691 SvROK_on(rv);
463ee0b2 3692
a0d0e21e
LW
3693 if (classname) {
3694 HV* stash = gv_stashpv(classname, TRUE);
3695 (void)sv_bless(rv, stash);
3696 }
3697 return sv;
3698}
3699
3700SV*
3701sv_setref_pv(rv, classname, pv)
3702SV *rv;
3703char *classname;
3704void* pv;
3705{
3706 if (!pv)
3707 sv_setsv(rv, &sv_undef);
3708 else
3709 sv_setiv(newSVrv(rv,classname), (IV)pv);
3710 return rv;
3711}
3712
3713SV*
3714sv_setref_iv(rv, classname, iv)
3715SV *rv;
3716char *classname;
3717IV iv;
3718{
3719 sv_setiv(newSVrv(rv,classname), iv);
3720 return rv;
3721}
3722
3723SV*
3724sv_setref_nv(rv, classname, nv)
3725SV *rv;
3726char *classname;
3727double nv;
3728{
3729 sv_setnv(newSVrv(rv,classname), nv);
3730 return rv;
3731}
463ee0b2 3732
a0d0e21e
LW
3733SV*
3734sv_setref_pvn(rv, classname, pv, n)
3735SV *rv;
3736char *classname;
3737char* pv;
3738I32 n;
3739{
3740 sv_setpvn(newSVrv(rv,classname), pv, n);
463ee0b2
LW
3741 return rv;
3742}
3743
a0d0e21e
LW
3744SV*
3745sv_bless(sv,stash)
3746SV* sv;
3747HV* stash;
3748{
3749 SV *ref;
3750 if (!SvROK(sv))
3751 croak("Can't bless non-reference value");
3752 ref = SvRV(sv);
3753 if (SvFLAGS(ref) & (SVs_OBJECT|SVf_READONLY)) {
3754 if (SvREADONLY(ref))
3755 croak(no_modify);
3756 if (SvOBJECT(ref) && SvTYPE(ref) != SVt_PVIO)
3757 --sv_objcount;
3758 }
3759 SvOBJECT_on(ref);
3760 ++sv_objcount;
3761 (void)SvUPGRADE(ref, SVt_PVMG);
3762 SvSTASH(ref) = (HV*)SvREFCNT_inc(stash);
3763
3764#ifdef OVERLOAD
748a9306 3765 SvAMAGIC_off(sv);
a0d0e21e
LW
3766 if (Gv_AMG(stash)) {
3767 SvAMAGIC_on(sv);
3768 }
3769#endif /* OVERLOAD */
3770
3771 return sv;
3772}
3773
3774static void
3775sv_unglob(sv)
3776SV* sv;
3777{
3778 assert(SvTYPE(sv) == SVt_PVGV);
3779 SvFAKE_off(sv);
3780 if (GvGP(sv))
1edc1566 3781 gp_free((GV*)sv);
a0d0e21e
LW
3782 sv_unmagic(sv, '*');
3783 Safefree(GvNAME(sv));
a5f75d66 3784 GvMULTI_off(sv);
a0d0e21e
LW
3785 SvFLAGS(sv) &= ~SVTYPEMASK;
3786 SvFLAGS(sv) |= SVt_PVMG;
3787}
3788
ed6116ce
LW
3789void
3790sv_unref(sv)
3791SV* sv;
3792{
a0d0e21e
LW
3793 SV* rv = SvRV(sv);
3794
ed6116ce
LW
3795 SvRV(sv) = 0;
3796 SvROK_off(sv);
4633a7c4
LW
3797 if (SvREFCNT(rv) != 1 || SvREADONLY(rv))
3798 SvREFCNT_dec(rv);
8e07c86e 3799 else
4633a7c4 3800 sv_2mortal(rv); /* Schedule for freeing later */
ed6116ce 3801}
8990e307 3802
bbce6d69
PP
3803IO*
3804sv_2io(sv)
3805SV *sv;
3806{
3807 IO* io;
3808 GV* gv;
3809
3810 switch (SvTYPE(sv)) {
3811 case SVt_PVIO:
3812 io = (IO*)sv;
3813 break;
3814 case SVt_PVGV:
3815 gv = (GV*)sv;
3816 io = GvIO(gv);
3817 if (!io)
3818 croak("Bad filehandle: %s", GvNAME(gv));
3819 break;
3820 default:
3821 if (!SvOK(sv))
3822 croak(no_usym, "filehandle");
3823 if (SvROK(sv))
3824 return sv_2io(SvRV(sv));
3825 gv = gv_fetchpv(SvPV(sv,na), FALSE, SVt_PVIO);
3826 if (gv)
3827 io = GvIO(gv);
3828 else
3829 io = 0;
3830 if (!io)
3831 croak("Bad filehandle: %s", SvPV(sv,na));
3832 break;
3833 }
3834 return io;
3835}
3836
3837void
3838sv_taint(sv)
3839SV *sv;
3840{
3841 sv_magic((sv), Nullsv, 't', Nullch, 0);
3842}
3843
3844void
3845sv_untaint(sv)
3846SV *sv;
3847{
3848 MAGIC *mg = mg_find(sv, 't');
3849 if (mg)
3850 mg->mg_len &= ~1;
3851}
3852
3853bool
3854sv_tainted(sv)
3855SV *sv;
3856{
3857 MAGIC *mg = mg_find(sv, 't');
3858 return (mg && ((mg->mg_len & 1)
3859 || (mg->mg_len & 2) && mg->mg_obj == sv));
3860}
3861
8990e307
LW
3862#ifdef DEBUGGING
3863void
3864sv_dump(sv)
3865SV* sv;
3866{
3867 char tmpbuf[1024];
3868 char *d = tmpbuf;
3869 U32 flags;
3870 U32 type;
3871
3872 if (!sv) {
760ac839 3873 PerlIO_printf(Perl_debug_log, "SV = 0\n");
8990e307
LW
3874 return;
3875 }
3876
3877 flags = SvFLAGS(sv);
3878 type = SvTYPE(sv);
3879
3880 sprintf(d, "(0x%lx)\n REFCNT = %ld\n FLAGS = (",
3881 (unsigned long)SvANY(sv), (long)SvREFCNT(sv));
3882 d += strlen(d);
3883 if (flags & SVs_PADBUSY) strcat(d, "PADBUSY,");
3884 if (flags & SVs_PADTMP) strcat(d, "PADTMP,");
3885 if (flags & SVs_PADMY) strcat(d, "PADMY,");
3886 if (flags & SVs_TEMP) strcat(d, "TEMP,");
3887 if (flags & SVs_OBJECT) strcat(d, "OBJECT,");
3888 if (flags & SVs_GMG) strcat(d, "GMG,");
3889 if (flags & SVs_SMG) strcat(d, "SMG,");
3890 if (flags & SVs_RMG) strcat(d, "RMG,");
3891 d += strlen(d);
3892
3893 if (flags & SVf_IOK) strcat(d, "IOK,");
3894 if (flags & SVf_NOK) strcat(d, "NOK,");
3895 if (flags & SVf_POK) strcat(d, "POK,");
3896 if (flags & SVf_ROK) strcat(d, "ROK,");
8990e307 3897 if (flags & SVf_OOK) strcat(d, "OOK,");
a0d0e21e 3898 if (flags & SVf_FAKE) strcat(d, "FAKE,");
8990e307
LW
3899 if (flags & SVf_READONLY) strcat(d, "READONLY,");
3900 d += strlen(d);
3901
1edc1566
PP
3902#ifdef OVERLOAD
3903 if (flags & SVf_AMAGIC) strcat(d, "OVERLOAD,");
3904#endif /* OVERLOAD */
8990e307
LW
3905 if (flags & SVp_IOK) strcat(d, "pIOK,");
3906 if (flags & SVp_NOK) strcat(d, "pNOK,");
3907 if (flags & SVp_POK) strcat(d, "pPOK,");
3908 if (flags & SVp_SCREAM) strcat(d, "SCREAM,");
1edc1566
PP
3909
3910 switch (type) {
3911 case SVt_PVCV:
3912 if (CvANON(sv)) strcat(d, "ANON,");
3913 if (CvCLONE(sv)) strcat(d, "CLONE,");
3914 if (CvCLONED(sv)) strcat(d, "CLONED,");
3915 break;
55497cff
PP
3916 case SVt_PVHV:
3917 if (HvSHAREKEYS(sv)) strcat(d, "SHAREKEYS,");
3918 if (HvLAZYDEL(sv)) strcat(d, "LAZYDEL,");
3919 break;
1edc1566 3920 case SVt_PVGV:
55497cff
PP
3921 if (GvINTRO(sv)) strcat(d, "INTRO,");
3922 if (GvMULTI(sv)) strcat(d, "MULTI,");
3923 if (GvASSUMECV(sv)) strcat(d, "ASSUMECV,");
3924 if (GvIMPORTED(sv)) {
3925 strcat(d, "IMPORT");
3926 if (GvIMPORTED(sv) == GVf_IMPORTED)
3927 strcat(d, "ALL,");
3928 else {
3929 strcat(d, "(");
3930 if (GvIMPORTED_SV(sv)) strcat(d, " SV");
3931 if (GvIMPORTED_AV(sv)) strcat(d, " AV");
3932 if (GvIMPORTED_HV(sv)) strcat(d, " HV");
3933 if (GvIMPORTED_CV(sv)) strcat(d, " CV");
3934 strcat(d, " ),");
3935 }
3936 }
1edc1566
PP
3937#ifdef OVERLOAD
3938 if (flags & SVpgv_AM) strcat(d, "withOVERLOAD,");
3939#endif /* OVERLOAD */
3940 }
3941
8990e307
LW
3942 d += strlen(d);
3943 if (d[-1] == ',')
3944 d--;
3945 *d++ = ')';
3946 *d = '\0';
3947
760ac839 3948 PerlIO_printf(Perl_debug_log, "SV = ");
8990e307
LW
3949 switch (type) {
3950 case SVt_NULL:
760ac839 3951 PerlIO_printf(Perl_debug_log, "NULL%s\n", tmpbuf);
8990e307
LW
3952 return;
3953 case SVt_IV:
760ac839 3954 PerlIO_printf(Perl_debug_log, "IV%s\n", tmpbuf);
8990e307
LW
3955 break;
3956 case SVt_NV:
760ac839 3957 PerlIO_printf(Perl_debug_log, "NV%s\n", tmpbuf);
8990e307
LW
3958 break;
3959 case SVt_RV:
760ac839 3960 PerlIO_printf(Perl_debug_log, "RV%s\n", tmpbuf);