This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix a2p translation of '{print "a" "b" "c"}'
[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 }
a0d0e21e 1003 else if (SvNOKp(sv))
463ee0b2 1004 sprintf(t,"(%g)",SvNVX(sv));
a0d0e21e 1005 else if (SvIOKp(sv))
463ee0b2 1006 sprintf(t,"(%ld)",(long)SvIVX(sv));
79072805
LW
1007 else
1008 strcpy(t,"()");
a0d0e21e
LW
1009
1010 finish:
1011 if (unref) {
1012 t += strlen(t);
1013 while (unref--)
1014 *t++ = ')';
1015 *t = '\0';
1016 }
79072805
LW
1017 return tokenbuf;
1018}
a0d0e21e 1019#endif
79072805
LW
1020
1021int
1022sv_backoff(sv)
1023register SV *sv;
1024{
1025 assert(SvOOK(sv));
463ee0b2
LW
1026 if (SvIVX(sv)) {
1027 char *s = SvPVX(sv);
1028 SvLEN(sv) += SvIVX(sv);
1029 SvPVX(sv) -= SvIVX(sv);
79072805 1030 SvIV_set(sv, 0);
463ee0b2 1031 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
79072805
LW
1032 }
1033 SvFLAGS(sv) &= ~SVf_OOK;
a0d0e21e 1034 return 0;
79072805
LW
1035}
1036
1037char *
1038sv_grow(sv,newlen)
1039register SV *sv;
1040#ifndef DOSISH
1041register I32 newlen;
1042#else
1043unsigned long newlen;
1044#endif
1045{
1046 register char *s;
1047
1048#ifdef MSDOS
1049 if (newlen >= 0x10000) {
d1bf51dd 1050 PerlIO_printf(Perl_debug_log, "Allocation too large: %lx\n", newlen);
79072805
LW
1051 my_exit(1);
1052 }
1053#endif /* MSDOS */
a0d0e21e
LW
1054 if (SvROK(sv))
1055 sv_unref(sv);
79072805
LW
1056 if (SvTYPE(sv) < SVt_PV) {
1057 sv_upgrade(sv, SVt_PV);
463ee0b2 1058 s = SvPVX(sv);
79072805
LW
1059 }
1060 else if (SvOOK(sv)) { /* pv is offset? */
1061 sv_backoff(sv);
463ee0b2 1062 s = SvPVX(sv);
79072805
LW
1063 if (newlen > SvLEN(sv))
1064 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
1065 }
1066 else
463ee0b2 1067 s = SvPVX(sv);
79072805 1068 if (newlen > SvLEN(sv)) { /* need more room? */
85e6fe83 1069 if (SvLEN(sv) && s)
79072805
LW
1070 Renew(s,newlen,char);
1071 else
1072 New(703,s,newlen,char);
1073 SvPV_set(sv, s);
1074 SvLEN_set(sv, newlen);
1075 }
1076 return s;
1077}
1078
1079void
1080sv_setiv(sv,i)
1081register SV *sv;
a0d0e21e 1082IV i;
79072805 1083{
ed6116ce 1084 if (SvTHINKFIRST(sv)) {
8990e307 1085 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
1086 croak(no_modify);
1087 if (SvROK(sv))
1088 sv_unref(sv);
1089 }
463ee0b2
LW
1090 switch (SvTYPE(sv)) {
1091 case SVt_NULL:
79072805 1092 sv_upgrade(sv, SVt_IV);
463ee0b2
LW
1093 break;
1094 case SVt_NV:
1095 sv_upgrade(sv, SVt_PVNV);
1096 break;
ed6116ce 1097 case SVt_RV:
463ee0b2 1098 case SVt_PV:
79072805 1099 sv_upgrade(sv, SVt_PVIV);
463ee0b2 1100 break;
a0d0e21e
LW
1101
1102 case SVt_PVGV:
1103 if (SvFAKE(sv)) {
1104 sv_unglob(sv);
1105 break;
1106 }
1107 /* FALL THROUGH */
1108 case SVt_PVAV:
1109 case SVt_PVHV:
1110 case SVt_PVCV:
1111 case SVt_PVFM:
1112 case SVt_PVIO:
1113 croak("Can't coerce %s to integer in %s", sv_reftype(sv,0),
1114 op_name[op->op_type]);
463ee0b2 1115 }
a0d0e21e 1116 (void)SvIOK_only(sv); /* validate number */
a5f75d66 1117 SvIVX(sv) = i;
463ee0b2 1118 SvTAINT(sv);
79072805
LW
1119}
1120
1121void
1122sv_setnv(sv,num)
1123register SV *sv;
1124double num;
1125{
ed6116ce 1126 if (SvTHINKFIRST(sv)) {
8990e307 1127 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
1128 croak(no_modify);
1129 if (SvROK(sv))
1130 sv_unref(sv);
1131 }
a0d0e21e
LW
1132 switch (SvTYPE(sv)) {
1133 case SVt_NULL:
1134 case SVt_IV:
79072805 1135 sv_upgrade(sv, SVt_NV);
a0d0e21e
LW
1136 break;
1137 case SVt_NV:
1138 case SVt_RV:
1139 case SVt_PV:
1140 case SVt_PVIV:
79072805 1141 sv_upgrade(sv, SVt_PVNV);
a0d0e21e
LW
1142 /* FALL THROUGH */
1143 case SVt_PVNV:
1144 case SVt_PVMG:
1145 case SVt_PVBM:
1146 case SVt_PVLV:
1147 if (SvOOK(sv))
1148 (void)SvOOK_off(sv);
1149 break;
1150 case SVt_PVGV:
1151 if (SvFAKE(sv)) {
1152 sv_unglob(sv);
1153 break;
1154 }
1155 /* FALL THROUGH */
1156 case SVt_PVAV:
1157 case SVt_PVHV:
1158 case SVt_PVCV:
1159 case SVt_PVFM:
1160 case SVt_PVIO:
1161 croak("Can't coerce %s to number in %s", sv_reftype(sv,0),
1162 op_name[op->op_type]);
79072805 1163 }
463ee0b2 1164 SvNVX(sv) = num;
a0d0e21e 1165 (void)SvNOK_only(sv); /* validate number */
463ee0b2 1166 SvTAINT(sv);
79072805
LW
1167}
1168
a0d0e21e
LW
1169static void
1170not_a_number(sv)
1171SV *sv;
1172{
1173 char tmpbuf[64];
1174 char *d = tmpbuf;
1175 char *s;
1176 int i;
1177
1178 for (s = SvPVX(sv), i = 50; *s && i; s++,i--) {
1179 int ch = *s;
1180 if (ch & 128 && !isprint(ch)) {
1181 *d++ = 'M';
1182 *d++ = '-';
1183 ch &= 127;
1184 }
1185 if (isprint(ch))
1186 *d++ = ch;
1187 else {
1188 *d++ = '^';
1189 *d++ = ch ^ 64;
1190 }
1191 }
1192 if (*s) {
1193 *d++ = '.';
1194 *d++ = '.';
1195 *d++ = '.';
1196 }
1197 *d = '\0';
1198
1199 if (op)
c07a80fd 1200 warn("Argument \"%s\" isn't numeric in %s", tmpbuf,
a0d0e21e
LW
1201 op_name[op->op_type]);
1202 else
1203 warn("Argument \"%s\" isn't numeric", tmpbuf);
1204}
1205
1206IV
79072805
LW
1207sv_2iv(sv)
1208register SV *sv;
1209{
1210 if (!sv)
1211 return 0;
8990e307 1212 if (SvGMAGICAL(sv)) {
463ee0b2
LW
1213 mg_get(sv);
1214 if (SvIOKp(sv))
1215 return SvIVX(sv);
748a9306
LW
1216 if (SvNOKp(sv)) {
1217 if (SvNVX(sv) < 0.0)
1218 return I_V(SvNVX(sv));
1219 else
5d94fbed 1220 return (IV) U_V(SvNVX(sv));
748a9306 1221 }
a0d0e21e
LW
1222 if (SvPOKp(sv) && SvLEN(sv)) {
1223 if (dowarn && !looks_like_number(sv))
1224 not_a_number(sv);
1225 return (IV)atol(SvPVX(sv));
1226 }
16d20bd9
AD
1227 if (!SvROK(sv)) {
1228 return 0;
1229 }
463ee0b2 1230 }
ed6116ce 1231 if (SvTHINKFIRST(sv)) {
a0d0e21e
LW
1232 if (SvROK(sv)) {
1233#ifdef OVERLOAD
1234 SV* tmpstr;
1235 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv, numer)))
1236 return SvIV(tmpstr);
1237#endif /* OVERLOAD */
1238 return (IV)SvRV(sv);
1239 }
ed6116ce 1240 if (SvREADONLY(sv)) {
748a9306
LW
1241 if (SvNOKp(sv)) {
1242 if (SvNVX(sv) < 0.0)
1243 return I_V(SvNVX(sv));
1244 else
5d94fbed 1245 return (IV) U_V(SvNVX(sv));
748a9306
LW
1246 }
1247 if (SvPOKp(sv) && SvLEN(sv)) {
a0d0e21e
LW
1248 if (dowarn && !looks_like_number(sv))
1249 not_a_number(sv);
1250 return (IV)atol(SvPVX(sv));
1251 }
ed6116ce 1252 if (dowarn)
8990e307 1253 warn(warn_uninit);
ed6116ce
LW
1254 return 0;
1255 }
79072805 1256 }
463ee0b2 1257 switch (SvTYPE(sv)) {
463ee0b2 1258 case SVt_NULL:
79072805 1259 sv_upgrade(sv, SVt_IV);
463ee0b2
LW
1260 return SvIVX(sv);
1261 case SVt_PV:
79072805 1262 sv_upgrade(sv, SVt_PVIV);
463ee0b2
LW
1263 break;
1264 case SVt_NV:
1265 sv_upgrade(sv, SVt_PVNV);
1266 break;
1267 }
748a9306 1268 if (SvNOKp(sv)) {
a5f75d66 1269 (void)SvIOK_on(sv);
748a9306
LW
1270 if (SvNVX(sv) < 0.0)
1271 SvIVX(sv) = I_V(SvNVX(sv));
1272 else
5d94fbed 1273 SvIVX(sv) = (IV) U_V(SvNVX(sv));
748a9306
LW
1274 }
1275 else if (SvPOKp(sv) && SvLEN(sv)) {
a0d0e21e
LW
1276 if (dowarn && !looks_like_number(sv))
1277 not_a_number(sv);
a5f75d66 1278 (void)SvIOK_on(sv);
a0d0e21e 1279 SvIVX(sv) = (IV)atol(SvPVX(sv));
93a17b20 1280 }
79072805 1281 else {
91bba347 1282 if (dowarn && !localizing && !(SvFLAGS(sv) & SVs_PADTMP))
8990e307 1283 warn(warn_uninit);
a0d0e21e 1284 return 0;
79072805 1285 }
1edc1566 1286 (void)SvIOK_on(sv);
760ac839 1287 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2iv(%ld)\n",
a0d0e21e 1288 (unsigned long)sv,(long)SvIVX(sv)));
463ee0b2 1289 return SvIVX(sv);
79072805
LW
1290}
1291
1292double
1293sv_2nv(sv)
1294register SV *sv;
1295{
1296 if (!sv)
1297 return 0.0;
8990e307 1298 if (SvGMAGICAL(sv)) {
463ee0b2
LW
1299 mg_get(sv);
1300 if (SvNOKp(sv))
1301 return SvNVX(sv);
a0d0e21e 1302 if (SvPOKp(sv) && SvLEN(sv)) {
748a9306 1303 if (dowarn && !SvIOKp(sv) && !looks_like_number(sv))
a0d0e21e 1304 not_a_number(sv);
463ee0b2 1305 return atof(SvPVX(sv));
a0d0e21e 1306 }
463ee0b2
LW
1307 if (SvIOKp(sv))
1308 return (double)SvIVX(sv);
16d20bd9
AD
1309 if (!SvROK(sv)) {
1310 return 0;
1311 }
463ee0b2 1312 }
ed6116ce 1313 if (SvTHINKFIRST(sv)) {
a0d0e21e
LW
1314 if (SvROK(sv)) {
1315#ifdef OVERLOAD
1316 SV* tmpstr;
1317 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)))
1318 return SvNV(tmpstr);
1319#endif /* OVERLOAD */
1320 return (double)(unsigned long)SvRV(sv);
1321 }
ed6116ce 1322 if (SvREADONLY(sv)) {
748a9306
LW
1323 if (SvPOKp(sv) && SvLEN(sv)) {
1324 if (dowarn && !SvIOKp(sv) && !looks_like_number(sv))
a0d0e21e 1325 not_a_number(sv);
ed6116ce 1326 return atof(SvPVX(sv));
a0d0e21e 1327 }
748a9306 1328 if (SvIOKp(sv))
8990e307 1329 return (double)SvIVX(sv);
ed6116ce 1330 if (dowarn)
8990e307 1331 warn(warn_uninit);
ed6116ce
LW
1332 return 0.0;
1333 }
79072805
LW
1334 }
1335 if (SvTYPE(sv) < SVt_NV) {
463ee0b2
LW
1336 if (SvTYPE(sv) == SVt_IV)
1337 sv_upgrade(sv, SVt_PVNV);
1338 else
1339 sv_upgrade(sv, SVt_NV);
760ac839 1340 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx num(%g)\n",(unsigned long)sv,SvNVX(sv)));
79072805
LW
1341 }
1342 else if (SvTYPE(sv) < SVt_PVNV)
1343 sv_upgrade(sv, SVt_PVNV);
748a9306
LW
1344 if (SvIOKp(sv) &&
1345 (!SvPOKp(sv) || !strchr(SvPVX(sv),'.') || !looks_like_number(sv)))
93a17b20 1346 {
463ee0b2 1347 SvNVX(sv) = (double)SvIVX(sv);
93a17b20 1348 }
748a9306
LW
1349 else if (SvPOKp(sv) && SvLEN(sv)) {
1350 if (dowarn && !SvIOKp(sv) && !looks_like_number(sv))
a0d0e21e 1351 not_a_number(sv);
463ee0b2 1352 SvNVX(sv) = atof(SvPVX(sv));
93a17b20 1353 }
79072805 1354 else {
91bba347 1355 if (dowarn && !localizing && !(SvFLAGS(sv) & SVs_PADTMP))
8990e307 1356 warn(warn_uninit);
a0d0e21e 1357 return 0.0;
79072805
LW
1358 }
1359 SvNOK_on(sv);
760ac839 1360 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2nv(%g)\n",(unsigned long)sv,SvNVX(sv)));
463ee0b2 1361 return SvNVX(sv);
79072805
LW
1362}
1363
1364char *
463ee0b2 1365sv_2pv(sv, lp)
79072805 1366register SV *sv;
463ee0b2 1367STRLEN *lp;
79072805
LW
1368{
1369 register char *s;
1370 int olderrno;
1371
463ee0b2
LW
1372 if (!sv) {
1373 *lp = 0;
1374 return "";
1375 }
8990e307 1376 if (SvGMAGICAL(sv)) {
463ee0b2
LW
1377 mg_get(sv);
1378 if (SvPOKp(sv)) {
1379 *lp = SvCUR(sv);
1380 return SvPVX(sv);
1381 }
1382 if (SvIOKp(sv)) {
a0d0e21e
LW
1383 (void)sprintf(tokenbuf,"%ld",(long)SvIVX(sv));
1384 goto tokensave;
463ee0b2
LW
1385 }
1386 if (SvNOKp(sv)) {
a0d0e21e
LW
1387 Gconvert(SvNVX(sv), DBL_DIG, 0, tokenbuf);
1388 goto tokensave;
463ee0b2 1389 }
16d20bd9
AD
1390 if (!SvROK(sv)) {
1391 *lp = 0;
1392 return "";
1393 }
463ee0b2 1394 }
ed6116ce
LW
1395 if (SvTHINKFIRST(sv)) {
1396 if (SvROK(sv)) {
a0d0e21e
LW
1397#ifdef OVERLOAD
1398 SV* tmpstr;
1399 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)))
1400 return SvPV(tmpstr,*lp);
1401#endif /* OVERLOAD */
ed6116ce
LW
1402 sv = (SV*)SvRV(sv);
1403 if (!sv)
1404 s = "NULLREF";
1405 else {
1406 switch (SvTYPE(sv)) {
1407 case SVt_NULL:
1408 case SVt_IV:
1409 case SVt_NV:
1410 case SVt_RV:
1411 case SVt_PV:
1412 case SVt_PVIV:
1413 case SVt_PVNV:
1414 case SVt_PVBM:
1415 case SVt_PVMG: s = "SCALAR"; break;
1416 case SVt_PVLV: s = "LVALUE"; break;
1417 case SVt_PVAV: s = "ARRAY"; break;
1418 case SVt_PVHV: s = "HASH"; break;
1419 case SVt_PVCV: s = "CODE"; break;
1420 case SVt_PVGV: s = "GLOB"; break;
1421 case SVt_PVFM: s = "FORMATLINE"; break;
8990e307 1422 case SVt_PVIO: s = "FILEHANDLE"; break;
ed6116ce
LW
1423 default: s = "UNKNOWN"; break;
1424 }
1425 if (SvOBJECT(sv))
1426 sprintf(tokenbuf, "%s=%s(0x%lx)",
1427 HvNAME(SvSTASH(sv)), s, (unsigned long)sv);
1428 else
1429 sprintf(tokenbuf, "%s(0x%lx)", s, (unsigned long)sv);
a0d0e21e 1430 goto tokensaveref;
463ee0b2 1431 }
ed6116ce
LW
1432 *lp = strlen(s);
1433 return s;
79072805 1434 }
ed6116ce 1435 if (SvREADONLY(sv)) {
748a9306 1436 if (SvNOKp(sv)) {
a0d0e21e
LW
1437 Gconvert(SvNVX(sv), DBL_DIG, 0, tokenbuf);
1438 goto tokensave;
ed6116ce 1439 }
8bb9dbe4
LW
1440 if (SvIOKp(sv)) {
1441 (void)sprintf(tokenbuf,"%ld",(long)SvIVX(sv));
1442 goto tokensave;
1443 }
ed6116ce 1444 if (dowarn)
8990e307 1445 warn(warn_uninit);
ed6116ce
LW
1446 *lp = 0;
1447 return "";
79072805 1448 }
79072805
LW
1449 }
1450 if (!SvUPGRADE(sv, SVt_PV))
1451 return 0;
748a9306 1452 if (SvNOKp(sv)) {
79072805
LW
1453 if (SvTYPE(sv) < SVt_PVNV)
1454 sv_upgrade(sv, SVt_PVNV);
1455 SvGROW(sv, 28);
463ee0b2 1456 s = SvPVX(sv);
79072805 1457 olderrno = errno; /* some Xenix systems wipe out errno here */
79072805 1458#ifdef apollo
463ee0b2 1459 if (SvNVX(sv) == 0.0)
79072805
LW
1460 (void)strcpy(s,"0");
1461 else
1462#endif /*apollo*/
a0d0e21e 1463 Gconvert(SvNVX(sv), DBL_DIG, 0, s);
79072805 1464 errno = olderrno;
a0d0e21e
LW
1465#ifdef FIXNEGATIVEZERO
1466 if (*s == '-' && s[1] == '0' && !s[2])
1467 strcpy(s,"0");
1468#endif
79072805
LW
1469 while (*s) s++;
1470#ifdef hcx
1471 if (s[-1] == '.')
1472 s--;
1473#endif
1474 }
748a9306 1475 else if (SvIOKp(sv)) {
79072805
LW
1476 if (SvTYPE(sv) < SVt_PVIV)
1477 sv_upgrade(sv, SVt_PVIV);
1478 SvGROW(sv, 11);
463ee0b2 1479 s = SvPVX(sv);
79072805 1480 olderrno = errno; /* some Xenix systems wipe out errno here */
a0d0e21e 1481 (void)sprintf(s,"%ld",(long)SvIVX(sv));
79072805
LW
1482 errno = olderrno;
1483 while (*s) s++;
1484 }
1485 else {
91bba347 1486 if (dowarn && !localizing && !(SvFLAGS(sv) & SVs_PADTMP))
8990e307 1487 warn(warn_uninit);
a0d0e21e
LW
1488 *lp = 0;
1489 return "";
79072805
LW
1490 }
1491 *s = '\0';
463ee0b2
LW
1492 *lp = s - SvPVX(sv);
1493 SvCUR_set(sv, *lp);
79072805 1494 SvPOK_on(sv);
760ac839 1495 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2pv(%s)\n",(unsigned long)sv,SvPVX(sv)));
463ee0b2 1496 return SvPVX(sv);
a0d0e21e
LW
1497
1498 tokensave:
1499 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
1500 /* Sneaky stuff here */
1501
1502 tokensaveref:
1503 sv = sv_newmortal();
1504 *lp = strlen(tokenbuf);
1505 sv_setpvn(sv, tokenbuf, *lp);
1506 return SvPVX(sv);
1507 }
1508 else {
1509 STRLEN len;
1510
1511#ifdef FIXNEGATIVEZERO
1512 if (*tokenbuf == '-' && tokenbuf[1] == '0' && !tokenbuf[2])
1513 strcpy(tokenbuf,"0");
1514#endif
1515 (void)SvUPGRADE(sv, SVt_PV);
1516 len = *lp = strlen(tokenbuf);
1517 s = SvGROW(sv, len + 1);
1518 SvCUR_set(sv, len);
1519 (void)strcpy(s, tokenbuf);
6bf554b4 1520 SvPOKp_on(sv);
a0d0e21e
LW
1521 return s;
1522 }
463ee0b2
LW
1523}
1524
1525/* This function is only called on magical items */
1526bool
1527sv_2bool(sv)
1528register SV *sv;
1529{
8990e307 1530 if (SvGMAGICAL(sv))
463ee0b2
LW
1531 mg_get(sv);
1532
a0d0e21e
LW
1533 if (!SvOK(sv))
1534 return 0;
1535 if (SvROK(sv)) {
1536#ifdef OVERLOAD
1537 {
1538 SV* tmpsv;
1539 if (SvAMAGIC(sv) && (tmpsv = AMG_CALLun(sv,bool_)))
1540 return SvTRUE(tmpsv);
1541 }
1542#endif /* OVERLOAD */
1543 return SvRV(sv) != 0;
1544 }
463ee0b2
LW
1545 if (SvPOKp(sv)) {
1546 register XPV* Xpv;
1547 if ((Xpv = (XPV*)SvANY(sv)) &&
1548 (*Xpv->xpv_pv > '0' ||
1549 Xpv->xpv_cur > 1 ||
1550 (Xpv->xpv_cur && *Xpv->xpv_pv != '0')))
1551 return 1;
1552 else
1553 return 0;
1554 }
1555 else {
1556 if (SvIOKp(sv))
1557 return SvIVX(sv) != 0;
1558 else {
1559 if (SvNOKp(sv))
1560 return SvNVX(sv) != 0.0;
1561 else
1562 return FALSE;
1563 }
1564 }
79072805
LW
1565}
1566
1567/* Note: sv_setsv() should not be called with a source string that needs
463ee0b2 1568 * to be reused, since it may destroy the source string if it is marked
79072805
LW
1569 * as temporary.
1570 */
1571
1572void
1573sv_setsv(dstr,sstr)
1574SV *dstr;
1575register SV *sstr;
1576{
8990e307
LW
1577 register U32 sflags;
1578 register int dtype;
1579 register int stype;
463ee0b2 1580
79072805
LW
1581 if (sstr == dstr)
1582 return;
ed6116ce 1583 if (SvTHINKFIRST(dstr)) {
8990e307 1584 if (SvREADONLY(dstr) && curcop != &compiling)
ed6116ce
LW
1585 croak(no_modify);
1586 if (SvROK(dstr))
1587 sv_unref(dstr);
1588 }
79072805
LW
1589 if (!sstr)
1590 sstr = &sv_undef;
8990e307
LW
1591 stype = SvTYPE(sstr);
1592 dtype = SvTYPE(dstr);
79072805 1593
8e07c86e
AD
1594 if (dtype == SVt_PVGV && (SvFLAGS(dstr) & SVf_FAKE)) {
1595 sv_unglob(dstr); /* so fake GLOB won't perpetuate */
4633a7c4
LW
1596 sv_setpvn(dstr, "", 0);
1597 (void)SvPOK_only(dstr);
8e07c86e
AD
1598 dtype = SvTYPE(dstr);
1599 }
1600
a0d0e21e
LW
1601#ifdef OVERLOAD
1602 SvAMAGIC_off(dstr);
1603#endif /* OVERLOAD */
463ee0b2 1604 /* There's a lot of redundancy below but we're going for speed here */
79072805 1605
8990e307 1606 switch (stype) {
79072805 1607 case SVt_NULL:
a0d0e21e 1608 (void)SvOK_off(dstr);
79072805 1609 return;
463ee0b2 1610 case SVt_IV:
8990e307
LW
1611 if (dtype <= SVt_PV) {
1612 if (dtype < SVt_IV)
1613 sv_upgrade(dstr, SVt_IV);
8990e307
LW
1614 else if (dtype == SVt_NV)
1615 sv_upgrade(dstr, SVt_PVNV);
a0d0e21e
LW
1616 else if (dtype <= SVt_PV)
1617 sv_upgrade(dstr, SVt_PVIV);
8990e307 1618 }
463ee0b2
LW
1619 break;
1620 case SVt_NV:
8990e307
LW
1621 if (dtype <= SVt_PVIV) {
1622 if (dtype < SVt_NV)
1623 sv_upgrade(dstr, SVt_NV);
8990e307
LW
1624 else if (dtype == SVt_PVIV)
1625 sv_upgrade(dstr, SVt_PVNV);
a0d0e21e
LW
1626 else if (dtype <= SVt_PV)
1627 sv_upgrade(dstr, SVt_PVNV);
8990e307 1628 }
463ee0b2 1629 break;
ed6116ce 1630 case SVt_RV:
8990e307 1631 if (dtype < SVt_RV)
ed6116ce 1632 sv_upgrade(dstr, SVt_RV);
c07a80fd
PP
1633 else if (dtype == SVt_PVGV &&
1634 SvTYPE(SvRV(sstr)) == SVt_PVGV) {
1635 sstr = SvRV(sstr);
a5f75d66
AD
1636 if (sstr == dstr) {
1637 if (curcop->cop_stash != GvSTASH(dstr))
1638 GvIMPORTED_on(dstr);
1639 GvMULTI_on(dstr);
1640 return;
1641 }
c07a80fd
PP
1642 goto glob_assign;
1643 }
ed6116ce 1644 break;
463ee0b2 1645 case SVt_PV:
8990e307 1646 if (dtype < SVt_PV)
463ee0b2 1647 sv_upgrade(dstr, SVt_PV);
463ee0b2
LW
1648 break;
1649 case SVt_PVIV:
8990e307 1650 if (dtype < SVt_PVIV)
463ee0b2 1651 sv_upgrade(dstr, SVt_PVIV);
463ee0b2
LW
1652 break;
1653 case SVt_PVNV:
8990e307 1654 if (dtype < SVt_PVNV)
463ee0b2 1655 sv_upgrade(dstr, SVt_PVNV);
463ee0b2 1656 break;
4633a7c4
LW
1657
1658 case SVt_PVLV:
4561caa4 1659 sv_upgrade(dstr, SVt_PVLV);
4633a7c4
LW
1660 break;
1661
1662 case SVt_PVAV:
1663 case SVt_PVHV:
1664 case SVt_PVCV:
4633a7c4
LW
1665 case SVt_PVIO:
1666 if (op)
1667 croak("Bizarre copy of %s in %s", sv_reftype(sstr, 0),
1668 op_name[op->op_type]);
1669 else
1670 croak("Bizarre copy of %s", sv_reftype(sstr, 0));
1671 break;
1672
79072805 1673 case SVt_PVGV:
8990e307 1674 if (dtype <= SVt_PVGV) {
c07a80fd 1675 glob_assign:
a5f75d66 1676 if (dtype != SVt_PVGV) {
a0d0e21e
LW
1677 char *name = GvNAME(sstr);
1678 STRLEN len = GvNAMELEN(sstr);
463ee0b2 1679 sv_upgrade(dstr, SVt_PVGV);
a0d0e21e
LW
1680 sv_magic(dstr, dstr, '*', name, len);
1681 GvSTASH(dstr) = GvSTASH(sstr);
1682 GvNAME(dstr) = savepvn(name, len);
1683 GvNAMELEN(dstr) = len;
1684 SvFAKE_on(dstr); /* can coerce to non-glob */
1685 }
1686 (void)SvOK_off(dstr);
a5f75d66 1687 GvINTRO_off(dstr); /* one-shot flag */
1edc1566 1688 gp_free((GV*)dstr);
79072805 1689 GvGP(dstr) = gp_ref(GvGP(sstr));
8990e307 1690 SvTAINT(dstr);
a5f75d66
AD
1691 if (curcop->cop_stash != GvSTASH(dstr))
1692 GvIMPORTED_on(dstr);
1693 GvMULTI_on(dstr);
79072805
LW
1694 return;
1695 }
1696 /* FALL THROUGH */
1697
1698 default:
8990e307
LW
1699 if (dtype < stype)
1700 sv_upgrade(dstr, stype);
1701 if (SvGMAGICAL(sstr))
79072805 1702 mg_get(sstr);
79072805
LW
1703 }
1704
8990e307
LW
1705 sflags = SvFLAGS(sstr);
1706
1707 if (sflags & SVf_ROK) {
1708 if (dtype >= SVt_PV) {
1709 if (dtype == SVt_PVGV) {
1710 SV *sref = SvREFCNT_inc(SvRV(sstr));
1711 SV *dref = 0;
a5f75d66 1712 int intro = GvINTRO(dstr);
a0d0e21e
LW
1713
1714 if (intro) {
1715 GP *gp;
1716 GvGP(dstr)->gp_refcnt--;
a5f75d66 1717 GvINTRO_off(dstr); /* one-shot flag */
a0d0e21e
LW
1718 Newz(602,gp, 1, GP);
1719 GvGP(dstr) = gp;
1720 GvREFCNT(dstr) = 1;
1721 GvSV(dstr) = NEWSV(72,0);
1722 GvLINE(dstr) = curcop->cop_line;
1edc1566 1723 GvEGV(dstr) = (GV*)dstr;
a0d0e21e 1724 }
a5f75d66 1725 GvMULTI_on(dstr);
8990e307
LW
1726 switch (SvTYPE(sref)) {
1727 case SVt_PVAV:
a0d0e21e
LW
1728 if (intro)
1729 SAVESPTR(GvAV(dstr));
1730 else
1731 dref = (SV*)GvAV(dstr);
8990e307 1732 GvAV(dstr) = (AV*)sref;
a5f75d66
AD
1733 if (curcop->cop_stash != GvSTASH(dstr))
1734 GvIMPORTED_AV_on(dstr);
8990e307
LW
1735 break;
1736 case SVt_PVHV:
a0d0e21e
LW
1737 if (intro)
1738 SAVESPTR(GvHV(dstr));
1739 else
1740 dref = (SV*)GvHV(dstr);
8990e307 1741 GvHV(dstr) = (HV*)sref;
a5f75d66
AD
1742 if (curcop->cop_stash != GvSTASH(dstr))
1743 GvIMPORTED_HV_on(dstr);
8990e307
LW
1744 break;
1745 case SVt_PVCV:
a0d0e21e
LW
1746 if (intro)
1747 SAVESPTR(GvCV(dstr));
748a9306
LW
1748 else {
1749 CV* cv = GvCV(dstr);
4633a7c4
LW
1750 if (cv) {
1751 dref = (SV*)cv;
1752 if (dowarn && sref != dref &&
1753 !GvCVGEN((GV*)dstr) &&
1754 (CvROOT(cv) || CvXSUB(cv)) )
1755 warn("Subroutine %s redefined",
1756 GvENAME((GV*)dstr));
1757 SvFAKE_on(cv);
1758 }
748a9306 1759 }
a5f75d66
AD
1760 if (GvCV(dstr) != (CV*)sref) {
1761 GvCV(dstr) = (CV*)sref;
1762 GvASSUMECV_on(dstr);
1763 }
1764 if (curcop->cop_stash != GvSTASH(dstr))
1765 GvIMPORTED_CV_on(dstr);
8990e307 1766 break;
91bba347
LW
1767 case SVt_PVIO:
1768 if (intro)
1769 SAVESPTR(GvIOp(dstr));
1770 else
1771 dref = (SV*)GvIOp(dstr);
1772 GvIOp(dstr) = (IO*)sref;
1773 break;
8990e307 1774 default:
a0d0e21e
LW
1775 if (intro)
1776 SAVESPTR(GvSV(dstr));
1777 else
1778 dref = (SV*)GvSV(dstr);
8990e307 1779 GvSV(dstr) = sref;
a5f75d66
AD
1780 if (curcop->cop_stash != GvSTASH(dstr))
1781 GvIMPORTED_SV_on(dstr);
8990e307
LW
1782 break;
1783 }
1784 if (dref)
1785 SvREFCNT_dec(dref);
a0d0e21e
LW
1786 if (intro)
1787 SAVEFREESV(sref);
8990e307
LW
1788 SvTAINT(dstr);
1789 return;
1790 }
a0d0e21e 1791 if (SvPVX(dstr)) {
760ac839 1792 (void)SvOOK_off(dstr); /* backoff */
8990e307 1793 Safefree(SvPVX(dstr));
a0d0e21e
LW
1794 SvLEN(dstr)=SvCUR(dstr)=0;
1795 }
8990e307 1796 }
a0d0e21e 1797 (void)SvOK_off(dstr);
8990e307 1798 SvRV(dstr) = SvREFCNT_inc(SvRV(sstr));
ed6116ce 1799 SvROK_on(dstr);
8990e307 1800 if (sflags & SVp_NOK) {
ed6116ce
LW
1801 SvNOK_on(dstr);
1802 SvNVX(dstr) = SvNVX(sstr);
1803 }
8990e307 1804 if (sflags & SVp_IOK) {
a0d0e21e 1805 (void)SvIOK_on(dstr);
ed6116ce
LW
1806 SvIVX(dstr) = SvIVX(sstr);
1807 }
a0d0e21e
LW
1808#ifdef OVERLOAD
1809 if (SvAMAGIC(sstr)) {
1810 SvAMAGIC_on(dstr);
1811 }
1812#endif /* OVERLOAD */
ed6116ce 1813 }
8990e307 1814 else if (sflags & SVp_POK) {
79072805
LW
1815
1816 /*
1817 * Check to see if we can just swipe the string. If so, it's a
1818 * possible small lose on short strings, but a big win on long ones.
463ee0b2
LW
1819 * It might even be a win on short strings if SvPVX(dstr)
1820 * has to be allocated and SvPVX(sstr) has to be freed.
79072805
LW
1821 */
1822
a5f75d66
AD
1823 if (SvTEMP(sstr) && /* slated for free anyway? */
1824 !(sflags & SVf_OOK)) /* and not involved in OOK hack? */
1825 {
adbc6bb1 1826 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
a5f75d66
AD
1827 if (SvOOK(dstr)) {
1828 SvFLAGS(dstr) &= ~SVf_OOK;
1829 Safefree(SvPVX(dstr) - SvIVX(dstr));
1830 }
1831 else
1832 Safefree(SvPVX(dstr));
79072805 1833 }
a5f75d66 1834 (void)SvPOK_only(dstr);
463ee0b2 1835 SvPV_set(dstr, SvPVX(sstr));
79072805
LW
1836 SvLEN_set(dstr, SvLEN(sstr));
1837 SvCUR_set(dstr, SvCUR(sstr));
79072805 1838 SvTEMP_off(dstr);
a5f75d66 1839 (void)SvOK_off(sstr);
79072805
LW
1840 SvPV_set(sstr, Nullch);
1841 SvLEN_set(sstr, 0);
a5f75d66
AD
1842 SvCUR_set(sstr, 0);
1843 SvTEMP_off(sstr);
79072805
LW
1844 }
1845 else { /* have to copy actual string */
8990e307
LW
1846 STRLEN len = SvCUR(sstr);
1847
1848 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
1849 Move(SvPVX(sstr),SvPVX(dstr),len,char);
1850 SvCUR_set(dstr, len);
1851 *SvEND(dstr) = '\0';
a0d0e21e 1852 (void)SvPOK_only(dstr);
79072805
LW
1853 }
1854 /*SUPPRESS 560*/
8990e307 1855 if (sflags & SVp_NOK) {
79072805 1856 SvNOK_on(dstr);
463ee0b2 1857 SvNVX(dstr) = SvNVX(sstr);
79072805 1858 }
8990e307 1859 if (sflags & SVp_IOK) {
a0d0e21e 1860 (void)SvIOK_on(dstr);
463ee0b2 1861 SvIVX(dstr) = SvIVX(sstr);
79072805
LW
1862 }
1863 }
8990e307 1864 else if (sflags & SVp_NOK) {
463ee0b2 1865 SvNVX(dstr) = SvNVX(sstr);
a0d0e21e 1866 (void)SvNOK_only(dstr);
79072805 1867 if (SvIOK(sstr)) {
a0d0e21e 1868 (void)SvIOK_on(dstr);
463ee0b2 1869 SvIVX(dstr) = SvIVX(sstr);
79072805
LW
1870 }
1871 }
8990e307 1872 else if (sflags & SVp_IOK) {
a0d0e21e 1873 (void)SvIOK_only(dstr);
463ee0b2 1874 SvIVX(dstr) = SvIVX(sstr);
79072805
LW
1875 }
1876 else {
a0d0e21e
LW
1877 (void)SvOK_off(dstr);
1878 }
463ee0b2 1879 SvTAINT(dstr);
79072805
LW
1880}
1881
1882void
1883sv_setpvn(sv,ptr,len)
1884register SV *sv;
1885register char *ptr;
1886register STRLEN len;
1887{
4561caa4
CS
1888 assert(len >= 0); /* STRLEN is probably unsigned, so this may
1889 elicit a warning, but it won't hurt. */
ed6116ce 1890 if (SvTHINKFIRST(sv)) {
8990e307 1891 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
1892 croak(no_modify);
1893 if (SvROK(sv))
1894 sv_unref(sv);
1895 }
463ee0b2 1896 if (!ptr) {
a0d0e21e 1897 (void)SvOK_off(sv);
463ee0b2
LW
1898 return;
1899 }
c07a80fd
PP
1900 if (SvTYPE(sv) >= SVt_PV) {
1901 if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
1902 sv_unglob(sv);
1903 }
1904 else if (!sv_upgrade(sv, SVt_PV))
79072805
LW
1905 return;
1906 SvGROW(sv, len + 1);
a0d0e21e 1907 Move(ptr,SvPVX(sv),len,char);
79072805
LW
1908 SvCUR_set(sv, len);
1909 *SvEND(sv) = '\0';
a0d0e21e 1910 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2 1911 SvTAINT(sv);
79072805
LW
1912}
1913
1914void
1915sv_setpv(sv,ptr)
1916register SV *sv;
1917register char *ptr;
1918{
1919 register STRLEN len;
1920
ed6116ce 1921 if (SvTHINKFIRST(sv)) {
8990e307 1922 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
1923 croak(no_modify);
1924 if (SvROK(sv))
1925 sv_unref(sv);
1926 }
463ee0b2 1927 if (!ptr) {
a0d0e21e 1928 (void)SvOK_off(sv);
463ee0b2
LW
1929 return;
1930 }
79072805 1931 len = strlen(ptr);
c07a80fd
PP
1932 if (SvTYPE(sv) >= SVt_PV) {
1933 if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
1934 sv_unglob(sv);
1935 }
1936 else if (!sv_upgrade(sv, SVt_PV))
79072805
LW
1937 return;
1938 SvGROW(sv, len + 1);
463ee0b2 1939 Move(ptr,SvPVX(sv),len+1,char);
79072805 1940 SvCUR_set(sv, len);
a0d0e21e 1941 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2
LW
1942 SvTAINT(sv);
1943}
1944
1945void
1946sv_usepvn(sv,ptr,len)
1947register SV *sv;
1948register char *ptr;
1949register STRLEN len;
1950{
ed6116ce 1951 if (SvTHINKFIRST(sv)) {
8990e307 1952 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
1953 croak(no_modify);
1954 if (SvROK(sv))
1955 sv_unref(sv);
1956 }
463ee0b2
LW
1957 if (!SvUPGRADE(sv, SVt_PV))
1958 return;
1959 if (!ptr) {
a0d0e21e 1960 (void)SvOK_off(sv);
463ee0b2
LW
1961 return;
1962 }
1963 if (SvPVX(sv))
1964 Safefree(SvPVX(sv));
1965 Renew(ptr, len+1, char);
1966 SvPVX(sv) = ptr;
1967 SvCUR_set(sv, len);
1968 SvLEN_set(sv, len+1);
1969 *SvEND(sv) = '\0';
a0d0e21e 1970 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2 1971 SvTAINT(sv);
79072805
LW
1972}
1973
1974void
1975sv_chop(sv,ptr) /* like set but assuming ptr is in sv */
1976register SV *sv;
1977register char *ptr;
1978{
1979 register STRLEN delta;
1980
a0d0e21e 1981 if (!ptr || !SvPOKp(sv))
79072805 1982 return;
ed6116ce 1983 if (SvTHINKFIRST(sv)) {
8990e307 1984 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
1985 croak(no_modify);
1986 if (SvROK(sv))
1987 sv_unref(sv);
1988 }
79072805
LW
1989 if (SvTYPE(sv) < SVt_PVIV)
1990 sv_upgrade(sv,SVt_PVIV);
1991
1992 if (!SvOOK(sv)) {
463ee0b2 1993 SvIVX(sv) = 0;
79072805
LW
1994 SvFLAGS(sv) |= SVf_OOK;
1995 }
8990e307 1996 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK);
463ee0b2 1997 delta = ptr - SvPVX(sv);
79072805
LW
1998 SvLEN(sv) -= delta;
1999 SvCUR(sv) -= delta;
463ee0b2
LW
2000 SvPVX(sv) += delta;
2001 SvIVX(sv) += delta;
79072805
LW
2002}
2003
2004void
2005sv_catpvn(sv,ptr,len)
2006register SV *sv;
2007register char *ptr;
2008register STRLEN len;
2009{
463ee0b2 2010 STRLEN tlen;
748a9306 2011 char *junk;
a0d0e21e 2012
748a9306 2013 junk = SvPV_force(sv, tlen);
463ee0b2 2014 SvGROW(sv, tlen + len + 1);
4633a7c4
LW
2015 if (ptr == junk)
2016 ptr = SvPVX(sv);
463ee0b2 2017 Move(ptr,SvPVX(sv)+tlen,len,char);
79072805
LW
2018 SvCUR(sv) += len;
2019 *SvEND(sv) = '\0';
a0d0e21e 2020 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2 2021 SvTAINT(sv);
79072805
LW
2022}
2023
2024void
2025sv_catsv(dstr,sstr)
2026SV *dstr;
2027register SV *sstr;
2028{
2029 char *s;
463ee0b2 2030 STRLEN len;
79072805
LW
2031 if (!sstr)
2032 return;
463ee0b2
LW
2033 if (s = SvPV(sstr, len))
2034 sv_catpvn(dstr,s,len);
79072805
LW
2035}
2036
2037void
2038sv_catpv(sv,ptr)
2039register SV *sv;
2040register char *ptr;
2041{
2042 register STRLEN len;
463ee0b2 2043 STRLEN tlen;
748a9306 2044 char *junk;
79072805 2045
79072805
LW
2046 if (!ptr)
2047 return;
748a9306 2048 junk = SvPV_force(sv, tlen);
79072805 2049 len = strlen(ptr);
463ee0b2 2050 SvGROW(sv, tlen + len + 1);
4633a7c4
LW
2051 if (ptr == junk)
2052 ptr = SvPVX(sv);
463ee0b2 2053 Move(ptr,SvPVX(sv)+tlen,len+1,char);
79072805 2054 SvCUR(sv) += len;
a0d0e21e 2055 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2 2056 SvTAINT(sv);
79072805
LW
2057}
2058
79072805
LW
2059SV *
2060#ifdef LEAKTEST
2061newSV(x,len)
2062I32 x;
2063#else
2064newSV(len)
2065#endif
2066STRLEN len;
2067{
2068 register SV *sv;
2069
4561caa4 2070 new_SV(sv);
8990e307
LW
2071 SvANY(sv) = 0;
2072 SvREFCNT(sv) = 1;
2073 SvFLAGS(sv) = 0;
79072805
LW
2074 if (len) {
2075 sv_upgrade(sv, SVt_PV);
2076 SvGROW(sv, len + 1);
2077 }
2078 return sv;
2079}
2080
1edc1566
PP
2081/* name is assumed to contain an SV* if (name && namelen == HEf_SVKEY) */
2082
79072805
LW
2083void
2084sv_magic(sv, obj, how, name, namlen)
2085register SV *sv;
2086SV *obj;
a0d0e21e 2087int how;
79072805 2088char *name;
463ee0b2 2089I32 namlen;
79072805
LW
2090{
2091 MAGIC* mg;
2092
a0d0e21e
LW
2093 if (SvREADONLY(sv) && curcop != &compiling && !strchr("gB", how))
2094 croak(no_modify);
4633a7c4 2095 if (SvMAGICAL(sv) || (how == 't' && SvTYPE(sv) >= SVt_PVMG)) {
748a9306
LW
2096 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
2097 if (how == 't')
2098 mg->mg_len |= 1;
463ee0b2 2099 return;
748a9306 2100 }
463ee0b2
LW
2101 }
2102 else {
2103 if (!SvUPGRADE(sv, SVt_PVMG))
2104 return;
463ee0b2 2105 }
79072805
LW
2106 Newz(702,mg, 1, MAGIC);
2107 mg->mg_moremagic = SvMAGIC(sv);
463ee0b2 2108
79072805 2109 SvMAGIC(sv) = mg;
748a9306 2110 if (!obj || obj == sv || how == '#')
8990e307 2111 mg->mg_obj = obj;
85e6fe83 2112 else {
8990e307 2113 mg->mg_obj = SvREFCNT_inc(obj);
85e6fe83
LW
2114 mg->mg_flags |= MGf_REFCOUNTED;
2115 }
79072805 2116 mg->mg_type = how;
463ee0b2 2117 mg->mg_len = namlen;
1edc1566
PP
2118 if (name)
2119 if (namlen >= 0)
2120 mg->mg_ptr = savepvn(name, namlen);
2121 else if (namlen == HEf_SVKEY)
2122 mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
2123
79072805
LW
2124 switch (how) {
2125 case 0:
2126 mg->mg_virtual = &vtbl_sv;
2127 break;
a0d0e21e
LW
2128#ifdef OVERLOAD
2129 case 'A':
2130 mg->mg_virtual = &vtbl_amagic;
2131 break;
2132 case 'a':
2133 mg->mg_virtual = &vtbl_amagicelem;
2134 break;
2135 case 'c':
2136 mg->mg_virtual = 0;
2137 break;
2138#endif /* OVERLOAD */
79072805
LW
2139 case 'B':
2140 mg->mg_virtual = &vtbl_bm;
2141 break;
79072805
LW
2142 case 'E':
2143 mg->mg_virtual = &vtbl_env;
2144 break;
2145 case 'e':
2146 mg->mg_virtual = &vtbl_envelem;
2147 break;
93a17b20
LW
2148 case 'g':
2149 mg->mg_virtual = &vtbl_mglob;
2150 break;
463ee0b2
LW
2151 case 'I':
2152 mg->mg_virtual = &vtbl_isa;
2153 break;
2154 case 'i':
2155 mg->mg_virtual = &vtbl_isaelem;
2156 break;
16660edb
PP
2157 case 'k':
2158 mg->mg_virtual = &vtbl_nkeys;
2159 break;
79072805 2160 case 'L':
a0d0e21e 2161 SvRMAGICAL_on(sv);
93a17b20
LW
2162 mg->mg_virtual = 0;
2163 break;
2164 case 'l':
79072805
LW
2165 mg->mg_virtual = &vtbl_dbline;
2166 break;
463ee0b2
LW
2167 case 'P':
2168 mg->mg_virtual = &vtbl_pack;
2169 break;
2170 case 'p':
a0d0e21e 2171 case 'q':
463ee0b2
LW
2172 mg->mg_virtual = &vtbl_packelem;
2173 break;
79072805
LW
2174 case 'S':
2175 mg->mg_virtual = &vtbl_sig;
2176 break;
2177 case 's':
2178 mg->mg_virtual = &vtbl_sigelem;
2179 break;
463ee0b2
LW
2180 case 't':
2181 mg->mg_virtual = &vtbl_taint;
748a9306 2182 mg->mg_len = 1;
463ee0b2 2183 break;
79072805
LW
2184 case 'U':
2185 mg->mg_virtual = &vtbl_uvar;
2186 break;
2187 case 'v':
2188 mg->mg_virtual = &vtbl_vec;
2189 break;
2190 case 'x':
2191 mg->mg_virtual = &vtbl_substr;
2192 break;
2193 case '*':
2194 mg->mg_virtual = &vtbl_glob;
2195 break;
2196 case '#':
2197 mg->mg_virtual = &vtbl_arylen;
2198 break;
a0d0e21e
LW
2199 case '.':
2200 mg->mg_virtual = &vtbl_pos;
2201 break;
4633a7c4
LW
2202 case '~': /* Reserved for use by extensions not perl internals. */
2203 /* Useful for attaching extension internal data to perl vars. */
2204 /* Note that multiple extensions may clash if magical scalars */
2205 /* etc holding private data from one are passed to another. */
2206 SvRMAGICAL_on(sv);
a0d0e21e 2207 break;
79072805 2208 default:
463ee0b2
LW
2209 croak("Don't know how to handle magic of type '%c'", how);
2210 }
8990e307
LW
2211 mg_magical(sv);
2212 if (SvGMAGICAL(sv))
2213 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
463ee0b2
LW
2214}
2215
2216int
2217sv_unmagic(sv, type)
2218SV* sv;
a0d0e21e 2219int type;
463ee0b2
LW
2220{
2221 MAGIC* mg;
2222 MAGIC** mgp;
91bba347 2223 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
463ee0b2
LW
2224 return 0;
2225 mgp = &SvMAGIC(sv);
2226 for (mg = *mgp; mg; mg = *mgp) {
2227 if (mg->mg_type == type) {
2228 MGVTBL* vtbl = mg->mg_virtual;
2229 *mgp = mg->mg_moremagic;
2230 if (vtbl && vtbl->svt_free)
2231 (*vtbl->svt_free)(sv, mg);
2232 if (mg->mg_ptr && mg->mg_type != 'g')
1edc1566
PP
2233 if (mg->mg_len >= 0)
2234 Safefree(mg->mg_ptr);
2235 else if (mg->mg_len == HEf_SVKEY)
2236 SvREFCNT_dec((SV*)mg->mg_ptr);
a0d0e21e
LW
2237 if (mg->mg_flags & MGf_REFCOUNTED)
2238 SvREFCNT_dec(mg->mg_obj);
463ee0b2
LW
2239 Safefree(mg);
2240 }
2241 else
2242 mgp = &mg->mg_moremagic;
79072805 2243 }
91bba347 2244 if (!SvMAGIC(sv)) {
463ee0b2 2245 SvMAGICAL_off(sv);
8990e307 2246 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
463ee0b2
LW
2247 }
2248
2249 return 0;
79072805
LW
2250}
2251
2252void
2253sv_insert(bigstr,offset,len,little,littlelen)
2254SV *bigstr;
2255STRLEN offset;
2256STRLEN len;
2257char *little;
2258STRLEN littlelen;
2259{
2260 register char *big;
2261 register char *mid;
2262 register char *midend;
2263 register char *bigend;
2264 register I32 i;
2265
8990e307
LW
2266 if (!bigstr)
2267 croak("Can't modify non-existent substring");
a0d0e21e 2268 SvPV_force(bigstr, na);
79072805
LW
2269
2270 i = littlelen - len;
2271 if (i > 0) { /* string might grow */
a0d0e21e 2272 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
79072805
LW
2273 mid = big + offset + len;
2274 midend = bigend = big + SvCUR(bigstr);
2275 bigend += i;
2276 *bigend = '\0';
2277 while (midend > mid) /* shove everything down */
2278 *--bigend = *--midend;
2279 Move(little,big+offset,littlelen,char);
2280 SvCUR(bigstr) += i;
2281 SvSETMAGIC(bigstr);
2282 return;
2283 }
2284 else if (i == 0) {
463ee0b2 2285 Move(little,SvPVX(bigstr)+offset,len,char);
79072805
LW
2286 SvSETMAGIC(bigstr);
2287 return;
2288 }
2289
463ee0b2 2290 big = SvPVX(bigstr);
79072805
LW
2291 mid = big + offset;
2292 midend = mid + len;
2293 bigend = big + SvCUR(bigstr);
2294
2295 if (midend > bigend)
463ee0b2 2296 croak("panic: sv_insert");
79072805
LW
2297
2298 if (mid - big > bigend - midend) { /* faster to shorten from end */
2299 if (littlelen) {
2300 Move(little, mid, littlelen,char);
2301 mid += littlelen;
2302 }
2303 i = bigend - midend;
2304 if (i > 0) {
2305 Move(midend, mid, i,char);
2306 mid += i;
2307 }
2308 *mid = '\0';
2309 SvCUR_set(bigstr, mid - big);
2310 }
2311 /*SUPPRESS 560*/
2312 else if (i = mid - big) { /* faster from front */
2313 midend -= littlelen;
2314 mid = midend;
2315 sv_chop(bigstr,midend-i);
2316 big += i;
2317 while (i--)
2318 *--midend = *--big;
2319 if (littlelen)
2320 Move(little, mid, littlelen,char);
2321 }
2322 else if (littlelen) {
2323 midend -= littlelen;
2324 sv_chop(bigstr,midend);
2325 Move(little,midend,littlelen,char);
2326 }
2327 else {
2328 sv_chop(bigstr,midend);
2329 }
2330 SvSETMAGIC(bigstr);
2331}
2332
2333/* make sv point to what nstr did */
2334
2335void
2336sv_replace(sv,nsv)
2337register SV *sv;
2338register SV *nsv;
2339{
2340 U32 refcnt = SvREFCNT(sv);
ed6116ce 2341 if (SvTHINKFIRST(sv)) {
8990e307 2342 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
2343 croak(no_modify);
2344 if (SvROK(sv))
2345 sv_unref(sv);
2346 }
79072805
LW
2347 if (SvREFCNT(nsv) != 1)
2348 warn("Reference miscount in sv_replace()");
93a17b20 2349 if (SvMAGICAL(sv)) {
a0d0e21e
LW
2350 if (SvMAGICAL(nsv))
2351 mg_free(nsv);
2352 else
2353 sv_upgrade(nsv, SVt_PVMG);
93a17b20 2354 SvMAGIC(nsv) = SvMAGIC(sv);
a0d0e21e 2355 SvFLAGS(nsv) |= SvMAGICAL(sv);
93a17b20
LW
2356 SvMAGICAL_off(sv);
2357 SvMAGIC(sv) = 0;
2358 }
79072805
LW
2359 SvREFCNT(sv) = 0;
2360 sv_clear(sv);
2361 StructCopy(nsv,sv,SV);
2362 SvREFCNT(sv) = refcnt;
1edc1566 2363 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
463ee0b2 2364 del_SV(nsv);
79072805
LW
2365}
2366
2367void
2368sv_clear(sv)
2369register SV *sv;
2370{
2371 assert(sv);
2372 assert(SvREFCNT(sv) == 0);
2373
ed6116ce 2374 if (SvOBJECT(sv)) {
463ee0b2 2375 dSP;
463ee0b2
LW
2376 GV* destructor;
2377
a0d0e21e
LW
2378 if (defstash) { /* Still have a symbol table? */
2379 destructor = gv_fetchmethod(SvSTASH(sv), "DESTROY");
2380
2381 ENTER;
2382 SAVEFREESV(SvSTASH(sv));
2383 if (destructor && GvCV(destructor)) {
2384 SV ref;
2385
2386 Zero(&ref, 1, SV);
2387 sv_upgrade(&ref, SVt_RV);
a0d0e21e
LW
2388 SvRV(&ref) = SvREFCNT_inc(sv);
2389 SvROK_on(&ref);
2390
2391 EXTEND(SP, 2);
2392 PUSHMARK(SP);
2393 PUSHs(&ref);
2394 PUTBACK;
4633a7c4 2395 perl_call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR);
748a9306 2396 del_XRV(SvANY(&ref));
1edc1566 2397 SvREFCNT(sv)--;
a0d0e21e
LW
2398 }
2399 LEAVE;
2400 }
4633a7c4
LW
2401 else
2402 SvREFCNT_dec(SvSTASH(sv));
a0d0e21e
LW
2403 if (SvOBJECT(sv)) {
2404 SvOBJECT_off(sv); /* Curse the object. */
2405 if (SvTYPE(sv) != SVt_PVIO)
2406 --sv_objcount; /* XXX Might want something more general */
2407 }
1edc1566
PP
2408 if (SvREFCNT(sv)) {
2409 SV *ret;
2410 if ( perldb
2411 && (ret = perl_get_sv("DB::ret", FALSE))
2412 && SvROK(ret) && SvRV(ret) == sv && SvREFCNT(sv) == 1) {
2413 /* Debugger is prone to dangling references. */
2414 SvRV(ret) = 0;
2415 SvROK_off(ret);
2416 SvREFCNT(sv) = 0;
2417 } else {
2418 croak("panic: dangling references in DESTROY");
2419 }
2420 }
463ee0b2 2421 }
c07a80fd 2422 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
a0d0e21e 2423 mg_free(sv);
79072805 2424 switch (SvTYPE(sv)) {
8990e307 2425 case SVt_PVIO:
91bba347 2426 io_close((IO*)sv);
8990e307
LW
2427 Safefree(IoTOP_NAME(sv));
2428 Safefree(IoFMT_NAME(sv));
2429 Safefree(IoBOTTOM_NAME(sv));
a0d0e21e 2430 /* FALL THROUGH */
79072805 2431 case SVt_PVBM:
a0d0e21e 2432 goto freescalar;
79072805 2433 case SVt_PVCV:
748a9306 2434 case SVt_PVFM:
85e6fe83 2435 cv_undef((CV*)sv);
a0d0e21e 2436 goto freescalar;
79072805 2437 case SVt_PVHV:
85e6fe83 2438 hv_undef((HV*)sv);
a0d0e21e 2439 break;
79072805 2440 case SVt_PVAV:
85e6fe83 2441 av_undef((AV*)sv);
a0d0e21e
LW
2442 break;
2443 case SVt_PVGV:
1edc1566 2444 gp_free((GV*)sv);
a0d0e21e
LW
2445 Safefree(GvNAME(sv));
2446 /* FALL THROUGH */
79072805 2447 case SVt_PVLV:
79072805 2448 case SVt_PVMG:
79072805
LW
2449 case SVt_PVNV:
2450 case SVt_PVIV:
a0d0e21e
LW
2451 freescalar:
2452 (void)SvOOK_off(sv);
79072805
LW
2453 /* FALL THROUGH */
2454 case SVt_PV:
a0d0e21e 2455 case SVt_RV:
8990e307
LW
2456 if (SvROK(sv))
2457 SvREFCNT_dec(SvRV(sv));
1edc1566 2458 else if (SvPVX(sv) && SvLEN(sv))
463ee0b2 2459 Safefree(SvPVX(sv));
79072805 2460 break;
a0d0e21e 2461/*
79072805 2462 case SVt_NV:
79072805 2463 case SVt_IV:
79072805
LW
2464 case SVt_NULL:
2465 break;
a0d0e21e 2466*/
79072805
LW
2467 }
2468
2469 switch (SvTYPE(sv)) {
2470 case SVt_NULL:
2471 break;
79072805
LW
2472 case SVt_IV:
2473 del_XIV(SvANY(sv));
2474 break;
2475 case SVt_NV:
2476 del_XNV(SvANY(sv));
2477 break;
ed6116ce
LW
2478 case SVt_RV:
2479 del_XRV(SvANY(sv));
2480 break;
79072805
LW
2481 case SVt_PV:
2482 del_XPV(SvANY(sv));
2483 break;
2484 case SVt_PVIV:
2485 del_XPVIV(SvANY(sv));
2486 break;
2487 case SVt_PVNV:
2488 del_XPVNV(SvANY(sv));
2489 break;
2490 case SVt_PVMG:
2491 del_XPVMG(SvANY(sv));
2492 break;
2493 case SVt_PVLV:
2494 del_XPVLV(SvANY(sv));
2495 break;
2496 case SVt_PVAV:
2497 del_XPVAV(SvANY(sv));
2498 break;
2499 case SVt_PVHV:
2500 del_XPVHV(SvANY(sv));
2501 break;
2502 case SVt_PVCV:
2503 del_XPVCV(SvANY(sv));
2504 break;
2505 case SVt_PVGV:
2506 del_XPVGV(SvANY(sv));
2507 break;
2508 case SVt_PVBM:
2509 del_XPVBM(SvANY(sv));
2510 break;
2511 case SVt_PVFM:
2512 del_XPVFM(SvANY(sv));
2513 break;
8990e307
LW
2514 case SVt_PVIO:
2515 del_XPVIO(SvANY(sv));
2516 break;
79072805 2517 }
a0d0e21e 2518 SvFLAGS(sv) &= SVf_BREAK;
8990e307 2519 SvFLAGS(sv) |= SVTYPEMASK;
79072805
LW
2520}
2521
2522SV *
8990e307 2523sv_newref(sv)
79072805
LW
2524SV* sv;
2525{
463ee0b2
LW
2526 if (sv)
2527 SvREFCNT(sv)++;
79072805
LW
2528 return sv;
2529}
2530
2531void
2532sv_free(sv)
2533SV *sv;
2534{
2535 if (!sv)
2536 return;
a0d0e21e
LW
2537 if (SvREADONLY(sv)) {
2538 if (sv == &sv_undef || sv == &sv_yes || sv == &sv_no)
2539 return;
79072805 2540 }
a0d0e21e
LW
2541 if (SvREFCNT(sv) == 0) {
2542 if (SvFLAGS(sv) & SVf_BREAK)
2543 return;
1edc1566
PP
2544 if (in_clean_all) /* All is fair */
2545 return;
79072805
LW
2546 warn("Attempt to free unreferenced scalar");
2547 return;
2548 }
8990e307
LW
2549 if (--SvREFCNT(sv) > 0)
2550 return;
463ee0b2
LW
2551#ifdef DEBUGGING
2552 if (SvTEMP(sv)) {
2553 warn("Attempt to free temp prematurely");
79072805 2554 return;
79072805 2555 }
463ee0b2 2556#endif
79072805 2557 sv_clear(sv);
79072805
LW
2558 del_SV(sv);
2559}
2560
2561STRLEN
2562sv_len(sv)
2563register SV *sv;
2564{
748a9306 2565 char *junk;
463ee0b2 2566 STRLEN len;
79072805
LW
2567
2568 if (!sv)
2569 return 0;
2570
8990e307
LW
2571 if (SvGMAGICAL(sv))
2572 len = mg_len(sv);
2573 else
748a9306 2574 junk = SvPV(sv, len);
463ee0b2 2575 return len;
79072805
LW
2576}
2577
2578I32
2579sv_eq(str1,str2)
2580register SV *str1;
2581register SV *str2;
2582{
2583 char *pv1;
463ee0b2 2584 STRLEN cur1;
79072805 2585 char *pv2;
463ee0b2 2586 STRLEN cur2;
79072805
LW
2587
2588 if (!str1) {
2589 pv1 = "";
2590 cur1 = 0;
2591 }
463ee0b2
LW
2592 else
2593 pv1 = SvPV(str1, cur1);
79072805
LW
2594
2595 if (!str2)
2596 return !cur1;
463ee0b2
LW
2597 else
2598 pv2 = SvPV(str2, cur2);
79072805
LW
2599
2600 if (cur1 != cur2)
2601 return 0;
2602
a026971d 2603 return !memcmp(pv1, pv2, cur1);
79072805
LW
2604}
2605
2606I32
2607sv_cmp(str1,str2)
2608register SV *str1;
2609register SV *str2;
2610{
2611 I32 retval;
2612 char *pv1;
463ee0b2 2613 STRLEN cur1;
79072805 2614 char *pv2;
463ee0b2 2615 STRLEN cur2;
79072805 2616
16660edb
PP
2617 if (lc_collate_active) { /* NOTE: this is the LC_COLLATE branch */
2618
79072805
LW
2619 if (!str1) {
2620 pv1 = "";
2621 cur1 = 0;
16660edb 2622 } else {
463ee0b2 2623 pv1 = SvPV(str1, cur1);
79072805 2624
16660edb
PP
2625 {
2626 STRLEN cur1x;
2627 char * pv1x = mem_collxfrm(pv1, cur1, &cur1x);
2628
2629 pv1 = pv1x;
2630 cur1 = cur1x;
2631 }
2632 }
2633
79072805
LW
2634 if (!str2) {
2635 pv2 = "";
2636 cur2 = 0;
16660edb
PP
2637 } else {
2638 pv2 = SvPV(str2, cur2);
2639
2640 {
2641 STRLEN cur2x;
2642 char * pv2x = mem_collxfrm(pv2, cur2, &cur2x);
2643
2644 pv2 = pv2x;
2645 cur2 = cur2x;
2646 }
79072805 2647 }
16660edb
PP
2648
2649 if (!cur1) {
2650 Safefree(pv2);
2651 return cur2 ? -1 : 0;
2652 }
2653
2654 if (!cur2) {
2655 Safefree(pv1);
2656 return 1;
2657 }
2658
2659 retval = memcmp((void*)pv1, (void*)pv2, cur1 < cur2 ? cur1 : cur2);
2660
2661 Safefree(pv1);
2662 Safefree(pv2);
2663
2664 if (retval)
2665 return retval < 0 ? -1 : 1;
2666
2667 if (cur1 == cur2)
2668 return 0;
463ee0b2 2669 else
16660edb
PP
2670 return cur1 < cur2 ? -1 : 1;
2671
2672 } else { /* NOTE: this is the non-LC_COLLATE branch */
2673
2674 if (!str1) {
2675 pv1 = "";
2676 cur1 = 0;
2677 } else
2678 pv1 = SvPV(str1, cur1);
2679
2680 if (!str2) {
2681 pv2 = "";
2682 cur2 = 0;
2683 } else
463ee0b2 2684 pv2 = SvPV(str2, cur2);
79072805
LW
2685
2686 if (!cur1)
2687 return cur2 ? -1 : 0;
16660edb 2688
79072805
LW
2689 if (!cur2)
2690 return 1;
2691
16660edb
PP
2692 retval = memcmp((void*)pv1, (void*)pv2, cur1 < cur2 ? cur1 : cur2);
2693
2694 if (retval)
79072805 2695 return retval < 0 ? -1 : 1;
16660edb
PP
2696
2697 if (cur1 == cur2)
79072805
LW
2698 return 0;
2699 else
16660edb
PP
2700 return cur1 < cur2 ? -1 : 1;
2701 }
79072805
LW
2702}
2703
2704char *
2705sv_gets(sv,fp,append)
2706register SV *sv;
760ac839 2707register PerlIO *fp;
79072805
LW
2708I32 append;
2709{
c07a80fd
PP
2710 char *rsptr;
2711 STRLEN rslen;
2712 register STDCHAR rslast;
2713 register STDCHAR *bp;
2714 register I32 cnt;
2715 I32 i;
2716
ed6116ce 2717 if (SvTHINKFIRST(sv)) {
8990e307 2718 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
2719 croak(no_modify);
2720 if (SvROK(sv))
2721 sv_unref(sv);
2722 }
79072805 2723 if (!SvUPGRADE(sv, SVt_PV))
a0d0e21e 2724 return 0;
c07a80fd
PP
2725
2726 if (RsSNARF(rs)) {
2727 rsptr = NULL;
2728 rslen = 0;
2729 }
2730 else if (RsPARA(rs)) {
2731 rsptr = "\n\n";
2732 rslen = 2;
2733 }
2734 else
2735 rsptr = SvPV(rs, rslen);
2736 rslast = rslen ? rsptr[rslen - 1] : '\0';
2737
2738 if (RsPARA(rs)) { /* have to do this both before and after */
79072805 2739 do { /* to make sure file boundaries work right */
760ac839 2740 if (PerlIO_eof(fp))
a0d0e21e 2741 return 0;
760ac839 2742 i = PerlIO_getc(fp);
79072805 2743 if (i != '\n') {
a0d0e21e
LW
2744 if (i == -1)
2745 return 0;
760ac839 2746 PerlIO_ungetc(fp,i);
79072805
LW
2747 break;
2748 }
2749 } while (i != EOF);
2750 }
c07a80fd 2751
760ac839
LW
2752 /* See if we know enough about I/O mechanism to cheat it ! */
2753
2754 /* This used to be #ifdef test - it is made run-time test for ease
2755 of abstracting out stdio interface. One call should be cheap
2756 enough here - and may even be a macro allowing compile
2757 time optimization.
2758 */
2759
2760 if (PerlIO_fast_gets(fp)) {
2761
2762 /*
2763 * We're going to steal some values from the stdio struct
2764 * and put EVERYTHING in the innermost loop into registers.
2765 */
2766 register STDCHAR *ptr;
2767 STRLEN bpx;
2768 I32 shortbuffered;
2769
16660edb
PP
2770#if defined(VMS) && defined(PERLIO_IS_STDIO)
2771 /* An ungetc()d char is handled separately from the regular
2772 * buffer, so we getc() it back out and stuff it in the buffer.
2773 */
2774 i = PerlIO_getc(fp);
2775 if (i == EOF) return 0;
2776 *(--((*fp)->_ptr)) = (unsigned char) i;
2777 (*fp)->_cnt++;
2778#endif
c07a80fd 2779
c2960299 2780 /* Here is some breathtakingly efficient cheating */
c07a80fd 2781
760ac839 2782 cnt = PerlIO_get_cnt(fp); /* get count into register */
a0d0e21e 2783 (void)SvPOK_only(sv); /* validate pointer */
79072805
LW
2784 if (SvLEN(sv) - append <= cnt + 1) { /* make sure we have the room */
2785 if (cnt > 80 && SvLEN(sv) > append) {
2786 shortbuffered = cnt - SvLEN(sv) + append + 1;
2787 cnt -= shortbuffered;
2788 }
2789 else {
2790 shortbuffered = 0;
2791 SvGROW(sv, append+cnt+2);/* (remembering cnt can be -1) */
2792 }
2793 }
2794 else
2795 shortbuffered = 0;
c07a80fd 2796 bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */
760ac839 2797 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
16660edb
PP
2798 DEBUG_P(PerlIO_printf(Perl_debug_log,
2799 "Screamer: entering, ptr=%d, cnt=%d\n",ptr,cnt));
2800 DEBUG_P(PerlIO_printf(Perl_debug_log,
2801 "Screamer: entering: FILE * thinks ptr=%d, cnt=%d, base=%d\n",
2802 PerlIO_get_ptr(fp), PerlIO_get_cnt(fp), PerlIO_get_base(fp)));
79072805
LW
2803 for (;;) {
2804 screamer:
93a17b20 2805 if (cnt > 0) {
c07a80fd 2806 if (rslen) {
760ac839
LW
2807 while (cnt > 0) { /* this | eat */
2808 cnt--;
c07a80fd
PP
2809 if ((*bp++ = *ptr++) == rslast) /* really | dust */
2810 goto thats_all_folks; /* screams | sed :-) */
2811 }
2812 }
2813 else {
2814 memcpy((char*)bp, (char*)ptr, cnt); /* this | eat */
2815 bp += cnt; /* screams | dust */
2816 ptr += cnt; /* louder | sed :-) */
a5f75d66 2817 cnt = 0;
93a17b20 2818 }
79072805
LW
2819 }
2820
748a9306 2821 if (shortbuffered) { /* oh well, must extend */
79072805
LW
2822 cnt = shortbuffered;
2823 shortbuffered = 0;
c07a80fd 2824 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
79072805
LW
2825 SvCUR_set(sv, bpx);
2826 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
c07a80fd 2827 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
79072805
LW
2828 continue;
2829 }
2830
16660edb
PP
2831 DEBUG_P(PerlIO_printf(Perl_debug_log,
2832 "Screamer: going to getc, ptr=%d, cnt=%d\n",ptr,cnt));
d1bf51dd 2833 PerlIO_set_ptrcnt(fp, ptr, cnt); /* deregisterize cnt and ptr */
16660edb
PP
2834 DEBUG_P(PerlIO_printf(Perl_debug_log,
2835 "Screamer: pre: FILE * thinks ptr=%d, cnt=%d, base=%d\n",
2836 PerlIO_get_ptr(fp), PerlIO_get_cnt(fp), PerlIO_get_base(fp)));
2837 /* This used to call 'filbuf' in stdio form, but as that behaves like
2838 getc when cnt <= 0 we use PerlIO_getc here to avoid another
2839 abstraction. This may also avoid issues with different named
2840 'filbuf' equivalents, though Configure tries to handle them now
2841 anyway.
760ac839
LW
2842 */
2843 i = PerlIO_getc(fp); /* get more characters */
16660edb
PP
2844 DEBUG_P(PerlIO_printf(Perl_debug_log,
2845 "Screamer: post: FILE * thinks ptr=%d, cnt=%d, base=%d\n",
2846 PerlIO_get_ptr(fp), PerlIO_get_cnt(fp), PerlIO_get_base(fp)));
760ac839
LW
2847 cnt = PerlIO_get_cnt(fp);
2848 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
16660edb
PP
2849 DEBUG_P(PerlIO_printf(Perl_debug_log,
2850 "Screamer: after getc, ptr=%d, cnt=%d\n",ptr,cnt));
79072805 2851
748a9306
LW
2852 if (i == EOF) /* all done for ever? */
2853 goto thats_really_all_folks;
2854
c07a80fd 2855 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
79072805
LW
2856 SvCUR_set(sv, bpx);
2857 SvGROW(sv, bpx + cnt + 2);
c07a80fd
PP
2858 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
2859
760ac839 2860 *bp++ = i; /* store character from PerlIO_getc */
79072805 2861
c07a80fd 2862 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
79072805 2863 goto thats_all_folks;
79072805
LW
2864 }
2865
2866thats_all_folks:
c07a80fd 2867 if ((rslen > 1 && (bp - (STDCHAR*)SvPVX(sv) < rslen)) ||
a026971d 2868 memcmp((char*)bp - rslen, rsptr, rslen))
760ac839 2869 goto screamer; /* go back to the fray */
79072805
LW
2870thats_really_all_folks:
2871 if (shortbuffered)
2872 cnt += shortbuffered;
16660edb
PP
2873 DEBUG_P(PerlIO_printf(Perl_debug_log,
2874 "Screamer: quitting, ptr=%d, cnt=%d\n",ptr,cnt));
d1bf51dd 2875 PerlIO_set_ptrcnt(fp, ptr, cnt); /* put these back or we're in trouble */
16660edb
PP
2876 DEBUG_P(PerlIO_printf(Perl_debug_log,
2877 "Screamer: end: FILE * thinks ptr=%d, cnt=%d, base=%d\n",
2878 PerlIO_get_ptr(fp), PerlIO_get_cnt(fp), PerlIO_get_base(fp)));
79072805 2879 *bp = '\0';
760ac839 2880 SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */
16660edb
PP
2881 DEBUG_P(PerlIO_printf(Perl_debug_log,
2882 "Screamer: done, len=%d, string=|%.*s|\n",
2883 SvCUR(sv),SvCUR(sv),SvPVX(sv)));
760ac839
LW
2884 }
2885 else
79072805 2886 {
760ac839 2887 /*The big, slow, and stupid way */
c07a80fd 2888 STDCHAR buf[8192];
79072805 2889
760ac839 2890screamer2:
c07a80fd 2891 if (rslen) {
760ac839
LW
2892 register STDCHAR *bpe = buf + sizeof(buf);
2893 bp = buf;
2894 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = i) != rslast && bp < bpe)
2895 ; /* keep reading */
2896 cnt = bp - buf;
c07a80fd
PP
2897 }
2898 else {
760ac839 2899 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
16660edb
PP
2900 /* Accomodate broken VAXC compiler, which applies U8 cast to
2901 * both args of ?: operator, causing EOF to change into 255
2902 */
2903 if (cnt) { i = (U8)buf[cnt - 1]; } else { i = EOF; }
c07a80fd 2904 }
79072805
LW
2905
2906 if (append)
760ac839 2907 sv_catpvn(sv, (char *) buf, cnt);
79072805 2908 else
760ac839 2909 sv_setpvn(sv, (char *) buf, cnt);
c07a80fd
PP
2910
2911 if (i != EOF && /* joy */
2912 (!rslen ||
2913 SvCUR(sv) < rslen ||
a026971d 2914 memcmp(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
79072805
LW
2915 {
2916 append = -1;
760ac839 2917 goto screamer2;
79072805
LW
2918 }
2919 }
2920
c07a80fd
PP
2921 if (RsPARA(rs)) { /* have to do this both before and after */
2922 while (i != EOF) { /* to make sure file boundaries work right */
760ac839 2923 i = PerlIO_getc(fp);
79072805 2924 if (i != '\n') {
760ac839 2925 PerlIO_ungetc(fp,i);
79072805
LW
2926 break;
2927 }
2928 }
2929 }
c07a80fd
PP
2930
2931 return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch;
79072805
LW
2932}
2933
760ac839 2934
79072805
LW
2935void
2936sv_inc(sv)
2937register SV *sv;
2938{
2939 register char *d;
463ee0b2 2940 int flags;
79072805
LW
2941
2942 if (!sv)
2943 return;
ed6116ce 2944 if (SvTHINKFIRST(sv)) {
8990e307 2945 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce 2946 croak(no_modify);
a0d0e21e
LW
2947 if (SvROK(sv)) {
2948#ifdef OVERLOAD
2949 if (SvAMAGIC(sv) && AMG_CALLun(sv,inc)) return;
2950#endif /* OVERLOAD */
2951 sv_unref(sv);
2952 }
ed6116ce 2953 }
8990e307 2954 if (SvGMAGICAL(sv))
79072805 2955 mg_get(sv);
8990e307
LW
2956 flags = SvFLAGS(sv);
2957 if (flags & SVp_IOK) {
a0d0e21e 2958 (void)SvIOK_only(sv);
a5f75d66 2959 ++SvIVX(sv);
79072805
LW
2960 return;
2961 }
8990e307 2962 if (flags & SVp_NOK) {
463ee0b2 2963 SvNVX(sv) += 1.0;
a0d0e21e 2964 (void)SvNOK_only(sv);
79072805
LW
2965 return;
2966 }
8990e307 2967 if (!(flags & SVp_POK) || !*SvPVX(sv)) {
4633a7c4
LW
2968 if ((flags & SVTYPEMASK) < SVt_PVNV)
2969 sv_upgrade(sv, SVt_NV);
463ee0b2 2970 SvNVX(sv) = 1.0;
a0d0e21e 2971 (void)SvNOK_only(sv);
79072805
LW
2972 return;
2973 }
463ee0b2 2974 d = SvPVX(sv);
79072805
LW
2975 while (isALPHA(*d)) d++;
2976 while (isDIGIT(*d)) d++;
2977 if (*d) {
463ee0b2 2978 sv_setnv(sv,atof(SvPVX(sv)) + 1.0); /* punt */
79072805
LW
2979 return;
2980 }
2981 d--;
463ee0b2 2982 while (d >= SvPVX(sv)) {
79072805
LW
2983 if (isDIGIT(*d)) {
2984 if (++*d <= '9')
2985 return;
2986 *(d--) = '0';
2987 }
2988 else {
2989 ++*d;
2990 if (isALPHA(*d))
2991 return;
2992 *(d--) -= 'z' - 'a' + 1;
2993 }
2994 }
2995 /* oh,oh, the number grew */
2996 SvGROW(sv, SvCUR(sv) + 2);
2997 SvCUR(sv)++;
463ee0b2 2998 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--)
79072805
LW
2999 *d = d[-1];
3000 if (isDIGIT(d[1]))
3001 *d = '1';
3002 else
3003 *d = d[1];
3004}
3005
3006void
3007sv_dec(sv)
3008register SV *sv;
3009{
463ee0b2
LW
3010 int flags;
3011
79072805
LW
3012 if (!sv)
3013 return;
ed6116ce 3014 if (SvTHINKFIRST(sv)) {
8990e307 3015 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce 3016 croak(no_modify);
a0d0e21e
LW
3017 if (SvROK(sv)) {
3018#ifdef OVERLOAD
3019 if (SvAMAGIC(sv) && AMG_CALLun(sv,dec)) return;
3020#endif /* OVERLOAD */
3021 sv_unref(sv);
3022 }
ed6116ce 3023 }
8990e307 3024 if (SvGMAGICAL(sv))
79072805 3025 mg_get(sv);
8990e307
LW
3026 flags = SvFLAGS(sv);
3027 if (flags & SVp_IOK) {
a0d0e21e 3028 (void)SvIOK_only(sv);
a5f75d66 3029 --SvIVX(sv);
79072805
LW
3030 return;
3031 }
8990e307 3032 if (flags & SVp_NOK) {
463ee0b2 3033 SvNVX(sv) -= 1.0;
a0d0e21e 3034 (void)SvNOK_only(sv);
79072805
LW
3035 return;
3036 }
8990e307 3037 if (!(flags & SVp_POK)) {
4633a7c4
LW
3038 if ((flags & SVTYPEMASK) < SVt_PVNV)
3039 sv_upgrade(sv, SVt_NV);
463ee0b2 3040 SvNVX(sv) = -1.0;
a0d0e21e 3041 (void)SvNOK_only(sv);
79072805
LW
3042 return;
3043 }
463ee0b2 3044 sv_setnv(sv,atof(SvPVX(sv)) - 1.0);
79072805
LW
3045}
3046
3047/* Make a string that will exist for the duration of the expression
3048 * evaluation. Actually, it may have to last longer than that, but
3049 * hopefully we won't free it until it has been assigned to a
3050 * permanent location. */
3051
8990e307
LW
3052static void
3053sv_mortalgrow()
3054{
3055 tmps_max += 128;
3056 Renew(tmps_stack, tmps_max, SV*);
3057}
3058
79072805
LW
3059SV *
3060sv_mortalcopy(oldstr)
3061SV *oldstr;
3062{
463ee0b2 3063 register SV *sv;
79072805 3064
4561caa4 3065 new_SV(sv);
8990e307
LW
3066 SvANY(sv) = 0;
3067 SvREFCNT(sv) = 1;
3068 SvFLAGS(sv) = 0;
79072805 3069 sv_setsv(sv,oldstr);
8990e307
LW
3070 if (++tmps_ix >= tmps_max)
3071 sv_mortalgrow();
3072 tmps_stack[tmps_ix] = sv;
3073 SvTEMP_on(sv);
3074 return sv;
3075}
3076
3077SV *
3078sv_newmortal()
3079{
3080 register SV *sv;
3081
4561caa4 3082 new_SV(sv);
8990e307
LW
3083 SvANY(sv) = 0;
3084 SvREFCNT(sv) = 1;
3085 SvFLAGS(sv) = SVs_TEMP;
3086 if (++tmps_ix >= tmps_max)
3087 sv_mortalgrow();
79072805 3088 tmps_stack[tmps_ix] = sv;
79072805
LW
3089 return sv;
3090}
3091
3092/* same thing without the copying */
3093
3094SV *
3095sv_2mortal(sv)
3096register SV *sv;
3097{
3098 if (!sv)
3099 return sv;
a0d0e21e
LW
3100 if (SvREADONLY(sv) && curcop != &compiling)
3101 croak(no_modify);
8990e307
LW
3102 if (++tmps_ix >= tmps_max)
3103 sv_mortalgrow();
79072805 3104 tmps_stack[tmps_ix] = sv;
8990e307 3105 SvTEMP_on(sv);
79072805
LW
3106 return sv;
3107}
3108
3109SV *
3110newSVpv(s,len)
3111char *s;
3112STRLEN len;
3113{
463ee0b2 3114 register SV *sv;
79072805 3115
4561caa4 3116 new_SV(sv);
8990e307
LW
3117 SvANY(sv) = 0;
3118 SvREFCNT(sv) = 1;
3119 SvFLAGS(sv) = 0;
79072805
LW
3120 if (!len)
3121 len = strlen(s);
3122 sv_setpvn(sv,s,len);
3123 return sv;
3124}
3125
3126SV *
3127newSVnv(n)
3128double n;
3129{
463ee0b2 3130 register SV *sv;
79072805 3131
4561caa4 3132 new_SV(sv);
8990e307
LW
3133 SvANY(sv) = 0;
3134 SvREFCNT(sv) = 1;
3135 SvFLAGS(sv) = 0;
79072805
LW
3136 sv_setnv(sv,n);
3137 return sv;
3138}
3139
3140SV *
3141newSViv(i)
a0d0e21e 3142IV i;
79072805 3143{
463ee0b2 3144 register SV *sv;
79072805 3145
4561caa4 3146 new_SV(sv);
8990e307
LW
3147 SvANY(sv) = 0;
3148 SvREFCNT(sv) = 1;
3149 SvFLAGS(sv) = 0;
79072805
LW
3150 sv_setiv(sv,i);
3151 return sv;
3152}
3153
2304df62
AD
3154SV *
3155newRV(ref)
3156SV *ref;
3157{
3158 register SV *sv;
3159
4561caa4 3160 new_SV(sv);
2304df62
AD
3161 SvANY(sv) = 0;
3162 SvREFCNT(sv) = 1;
3163 SvFLAGS(sv) = 0;
3164 sv_upgrade(sv, SVt_RV);
a0d0e21e 3165 SvTEMP_off(ref);
2304df62
AD
3166 SvRV(sv) = SvREFCNT_inc(ref);
3167 SvROK_on(sv);
2304df62
AD
3168 return sv;
3169}
3170
79072805
LW
3171/* make an exact duplicate of old */
3172
3173SV *
3174newSVsv(old)
3175register SV *old;
3176{
463ee0b2 3177 register SV *sv;
79072805
LW
3178
3179 if (!old)
3180 return Nullsv;
8990e307 3181 if (SvTYPE(old) == SVTYPEMASK) {
79072805
LW
3182 warn("semi-panic: attempt to dup freed string");
3183 return Nullsv;
3184 }
4561caa4 3185 new_SV(sv);
8990e307
LW
3186 SvANY(sv) = 0;
3187 SvREFCNT(sv) = 1;
3188 SvFLAGS(sv) = 0;
79072805
LW
3189 if (SvTEMP(old)) {
3190 SvTEMP_off(old);
463ee0b2 3191 sv_setsv(sv,old);
79072805
LW
3192 SvTEMP_on(old);
3193 }
3194 else
463ee0b2
LW
3195 sv_setsv(sv,old);
3196 return sv;
79072805
LW
3197}
3198
3199void
3200sv_reset(s,stash)
3201register char *s;
3202HV *stash;
3203{
3204 register HE *entry;
3205 register GV *gv;
3206 register SV *sv;
3207 register I32 i;
3208 register PMOP *pm;
3209 register I32 max;
463ee0b2 3210 char todo[256];
79072805
LW
3211
3212 if (!*s) { /* reset ?? searches */
3213 for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) {
3214 pm->op_pmflags &= ~PMf_USED;
3215 }
3216 return;
3217 }
3218
3219 /* reset variables */
3220
3221 if (!HvARRAY(stash))
3222 return;
463ee0b2
LW
3223
3224 Zero(todo, 256, char);
79072805
LW
3225 while (*s) {
3226 i = *s;
3227 if (s[1] == '-') {
3228 s += 2;
3229 }
3230 max = *s++;
3231 for ( ; i <= max; i++) {
463ee0b2
LW
3232 todo[i] = 1;
3233 }
a0d0e21e 3234 for (i = 0; i <= (I32) HvMAX(stash); i++) {
79072805
LW
3235 for (entry = HvARRAY(stash)[i];
3236 entry;
1edc1566
PP
3237 entry = HeNEXT(entry)) {
3238 if (!todo[(U8)*HeKEY(entry)])
463ee0b2 3239 continue;
1edc1566 3240 gv = (GV*)HeVAL(entry);
79072805 3241 sv = GvSV(gv);
a0d0e21e 3242 (void)SvOK_off(sv);
79072805
LW
3243 if (SvTYPE(sv) >= SVt_PV) {
3244 SvCUR_set(sv, 0);
463ee0b2
LW
3245 SvTAINT(sv);
3246 if (SvPVX(sv) != Nullch)
3247 *SvPVX(sv) = '\0';
79072805
LW
3248 }
3249 if (GvAV(gv)) {
3250 av_clear(GvAV(gv));
3251 }
3252 if (GvHV(gv)) {
a0d0e21e
LW
3253 if (HvNAME(GvHV(gv)))
3254 continue;
463ee0b2 3255 hv_clear(GvHV(gv));
a0d0e21e 3256#ifndef VMS /* VMS has no environ array */
79072805
LW
3257 if (gv == envgv)
3258 environ[0] = Nullch;
a0d0e21e 3259#endif
79072805
LW
3260 }
3261 }
3262 }
3263 }
3264}
3265
79072805
LW
3266CV *
3267sv_2cv(sv, st, gvp, lref)
3268SV *sv;
3269HV **st;
3270GV **gvp;
3271I32 lref;
3272{
3273 GV *gv;
3274 CV *cv;
3275
3276 if (!sv)
93a17b20 3277 return *gvp = Nullgv, Nullcv;
79072805 3278 switch (SvTYPE(sv)) {
79072805
LW
3279 case SVt_PVCV:
3280 *st = CvSTASH(sv);
3281 *gvp = Nullgv;
3282 return (CV*)sv;
3283 case SVt_PVHV:
3284 case SVt_PVAV:
3285 *gvp = Nullgv;
3286 return Nullcv;
8990e307
LW
3287 case SVt_PVGV:
3288 gv = (GV*)sv;
a0d0e21e 3289 *gvp = gv;
8990e307
LW
3290 *st = GvESTASH(gv);
3291 goto fix_gv;
3292
79072805 3293 default:
a0d0e21e
LW
3294 if (SvGMAGICAL(sv))
3295 mg_get(sv);
3296 if (SvROK(sv)) {
3297 cv = (CV*)SvRV(sv);
3298 if (SvTYPE(cv) != SVt_PVCV)
3299 croak("Not a subroutine reference");
3300 *gvp = Nullgv;
3301 *st = CvSTASH(cv);
3302 return cv;
3303 }
79072805
LW
3304 if (isGV(sv))
3305 gv = (GV*)sv;
3306 else
85e6fe83 3307 gv = gv_fetchpv(SvPV(sv, na), lref, SVt_PVCV);
79072805
LW
3308 *gvp = gv;
3309 if (!gv)
3310 return Nullcv;
3311 *st = GvESTASH(gv);
8990e307
LW
3312 fix_gv:
3313 if (lref && !GvCV(gv)) {
4633a7c4 3314 SV *tmpsv;
748a9306 3315 ENTER;
4633a7c4 3316 tmpsv = NEWSV(704,0);
16660edb 3317 gv_efullname3(tmpsv, gv, Nullch);
748a9306 3318 newSUB(start_subparse(),
4633a7c4
LW
3319 newSVOP(OP_CONST, 0, tmpsv),
3320 Nullop,
8990e307 3321 Nullop);
748a9306 3322 LEAVE;
4633a7c4
LW
3323 if (!GvCV(gv))
3324 croak("Unable to create sub named \"%s\"", SvPV(sv,na));
8990e307 3325 }
79072805
LW
3326 return GvCV(gv);
3327 }
3328}
3329
3330#ifndef SvTRUE
3331I32
3332SvTRUE(sv)
3333register SV *sv;
3334{
8990e307
LW
3335 if (!sv)
3336 return 0;
3337 if (SvGMAGICAL(sv))
79072805
LW
3338 mg_get(sv);
3339 if (SvPOK(sv)) {
3340 register XPV* Xpv;
3341 if ((Xpv = (XPV*)SvANY(sv)) &&
3342 (*Xpv->xpv_pv > '0' ||
3343 Xpv->xpv_cur > 1 ||
3344 (Xpv->xpv_cur && *Xpv->xpv_pv != '0')))
3345 return 1;
3346 else
3347 return 0;
3348 }
3349 else {
3350 if (SvIOK(sv))
463ee0b2 3351 return SvIVX(sv) != 0;
79072805
LW
3352 else {
3353 if (SvNOK(sv))
463ee0b2 3354 return SvNVX(sv) != 0.0;
79072805 3355 else
463ee0b2 3356 return sv_2bool(sv);
79072805
LW
3357 }
3358 }
3359}
3360#endif /* SvTRUE */
3361
85e6fe83 3362#ifndef SvIV
a0d0e21e 3363IV SvIV(Sv)
85e6fe83
LW
3364register SV *Sv;
3365{
3366 if (SvIOK(Sv))
3367 return SvIVX(Sv);
3368 return sv_2iv(Sv);
3369}
3370#endif /* SvIV */
3371
3372
463ee0b2
LW
3373#ifndef SvNV
3374double SvNV(Sv)
79072805
LW
3375register SV *Sv;
3376{
79072805 3377 if (SvNOK(Sv))
463ee0b2 3378 return SvNVX(Sv);
79072805 3379 if (SvIOK(Sv))
463ee0b2 3380 return (double)SvIVX(Sv);
79072805
LW
3381 return sv_2nv(Sv);
3382}
463ee0b2 3383#endif /* SvNV */
79072805 3384
463ee0b2 3385#ifdef CRIPPLED_CC
79072805 3386char *
463ee0b2 3387sv_pvn(sv, lp)
79072805 3388SV *sv;
463ee0b2 3389STRLEN *lp;
79072805 3390{
85e6fe83
LW
3391 if (SvPOK(sv)) {
3392 *lp = SvCUR(sv);
a0d0e21e 3393 return SvPVX(sv);
85e6fe83 3394 }
463ee0b2 3395 return sv_2pv(sv, lp);
79072805
LW
3396}
3397#endif
3398
a0d0e21e
LW
3399char *
3400sv_pvn_force(sv, lp)
3401SV *sv;
3402STRLEN *lp;
3403{
3404 char *s;
3405
3406 if (SvREADONLY(sv) && curcop != &compiling)
3407 croak(no_modify);
3408
3409 if (SvPOK(sv)) {
3410 *lp = SvCUR(sv);
3411 }
3412 else {
748a9306 3413 if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) {
4633a7c4 3414 if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV) {
a0d0e21e 3415 sv_unglob(sv);
4633a7c4
LW
3416 s = SvPVX(sv);
3417 *lp = SvCUR(sv);
3418 }
a0d0e21e
LW
3419 else
3420 croak("Can't coerce %s to string in %s", sv_reftype(sv,0),
3421 op_name[op->op_type]);
3422 }
4633a7c4
LW
3423 else
3424 s = sv_2pv(sv, lp);
a0d0e21e
LW
3425 if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */
3426 STRLEN len = *lp;
3427
3428 if (SvROK(sv))
3429 sv_unref(sv);
3430 (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */
3431 SvGROW(sv, len + 1);
3432 Move(s,SvPVX(sv),len,char);
3433 SvCUR_set(sv, len);
3434 *SvEND(sv) = '\0';
3435 }
3436 if (!SvPOK(sv)) {
3437 SvPOK_on(sv); /* validate pointer */
3438 SvTAINT(sv);
760ac839 3439 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%lx 2pv(%s)\n",
a0d0e21e
LW
3440 (unsigned long)sv,SvPVX(sv)));
3441 }
3442 }
3443 return SvPVX(sv);
3444}
3445
3446char *
3447sv_reftype(sv, ob)
3448SV* sv;
3449int ob;
3450{
3451 if (ob && SvOBJECT(sv))
3452 return HvNAME(SvSTASH(sv));
3453 else {
3454 switch (SvTYPE(sv)) {
3455 case SVt_NULL:
3456 case SVt_IV:
3457 case SVt_NV:
3458 case SVt_RV:
3459 case SVt_PV:
3460 case SVt_PVIV:
3461 case SVt_PVNV:
3462 case SVt_PVMG:
3463 case SVt_PVBM:
3464 if (SvROK(sv))
3465 return "REF";
3466 else
3467 return "SCALAR";
3468 case SVt_PVLV: return "LVALUE";
3469 case SVt_PVAV: return "ARRAY";
3470 case SVt_PVHV: return "HASH";
3471 case SVt_PVCV: return "CODE";
3472 case SVt_PVGV: return "GLOB";
3473 case SVt_PVFM: return "FORMLINE";
3474 default: return "UNKNOWN";
3475 }
3476 }
3477}
3478
463ee0b2 3479int
85e6fe83
LW
3480sv_isobject(sv)
3481SV *sv;
3482{
3483 if (!SvROK(sv))
3484 return 0;
3485 sv = (SV*)SvRV(sv);
3486 if (!SvOBJECT(sv))
3487 return 0;
3488 return 1;
3489}
3490
3491int
463ee0b2
LW
3492sv_isa(sv, name)
3493SV *sv;
3494char *name;
3495{
ed6116ce 3496 if (!SvROK(sv))
463ee0b2 3497 return 0;
ed6116ce
LW
3498 sv = (SV*)SvRV(sv);
3499 if (!SvOBJECT(sv))
463ee0b2
LW
3500 return 0;
3501
3502 return strEQ(HvNAME(SvSTASH(sv)), name);
3503}
3504
3505SV*
a0d0e21e 3506newSVrv(rv, classname)
463ee0b2 3507SV *rv;
a0d0e21e 3508char *classname;
463ee0b2 3509{
463ee0b2
LW
3510 SV *sv;
3511
4561caa4 3512 new_SV(sv);
8990e307 3513 SvANY(sv) = 0;
a0d0e21e 3514 SvREFCNT(sv) = 0;
8990e307 3515 SvFLAGS(sv) = 0;
ed6116ce 3516 sv_upgrade(rv, SVt_RV);
8990e307 3517 SvRV(rv) = SvREFCNT_inc(sv);
ed6116ce 3518 SvROK_on(rv);
463ee0b2 3519
a0d0e21e
LW
3520 if (classname) {
3521 HV* stash = gv_stashpv(classname, TRUE);
3522 (void)sv_bless(rv, stash);
3523 }
3524 return sv;
3525}
3526
3527SV*
3528sv_setref_pv(rv, classname, pv)
3529SV *rv;
3530char *classname;
3531void* pv;
3532{
3533 if (!pv)
3534 sv_setsv(rv, &sv_undef);
3535 else
3536 sv_setiv(newSVrv(rv,classname), (IV)pv);
3537 return rv;
3538}
3539
3540SV*
3541sv_setref_iv(rv, classname, iv)
3542SV *rv;
3543char *classname;
3544IV iv;
3545{
3546 sv_setiv(newSVrv(rv,classname), iv);
3547 return rv;
3548}
3549
3550SV*
3551sv_setref_nv(rv, classname, nv)
3552SV *rv;
3553char *classname;
3554double nv;
3555{
3556 sv_setnv(newSVrv(rv,classname), nv);
3557 return rv;
3558}
463ee0b2 3559
a0d0e21e
LW
3560SV*
3561sv_setref_pvn(rv, classname, pv, n)
3562SV *rv;
3563char *classname;
3564char* pv;
3565I32 n;
3566{
3567 sv_setpvn(newSVrv(rv,classname), pv, n);
463ee0b2
LW
3568 return rv;
3569}
3570
a0d0e21e
LW
3571SV*
3572sv_bless(sv,stash)
3573SV* sv;
3574HV* stash;
3575{
3576 SV *ref;
3577 if (!SvROK(sv))
3578 croak("Can't bless non-reference value");
3579 ref = SvRV(sv);
3580 if (SvFLAGS(ref) & (SVs_OBJECT|SVf_READONLY)) {
3581 if (SvREADONLY(ref))
3582 croak(no_modify);
3583 if (SvOBJECT(ref) && SvTYPE(ref) != SVt_PVIO)
3584 --sv_objcount;
3585 }
3586 SvOBJECT_on(ref);
3587 ++sv_objcount;
3588 (void)SvUPGRADE(ref, SVt_PVMG);
3589 SvSTASH(ref) = (HV*)SvREFCNT_inc(stash);
3590
3591#ifdef OVERLOAD
748a9306 3592 SvAMAGIC_off(sv);
a0d0e21e
LW
3593 if (Gv_AMG(stash)) {
3594 SvAMAGIC_on(sv);
3595 }
3596#endif /* OVERLOAD */
3597
3598 return sv;
3599}
3600
3601static void
3602sv_unglob(sv)
3603SV* sv;
3604{
3605 assert(SvTYPE(sv) == SVt_PVGV);
3606 SvFAKE_off(sv);
3607 if (GvGP(sv))
1edc1566 3608 gp_free((GV*)sv);
a0d0e21e
LW
3609 sv_unmagic(sv, '*');
3610 Safefree(GvNAME(sv));
a5f75d66 3611 GvMULTI_off(sv);
a0d0e21e
LW
3612 SvFLAGS(sv) &= ~SVTYPEMASK;
3613 SvFLAGS(sv) |= SVt_PVMG;
3614}
3615
ed6116ce
LW
3616void
3617sv_unref(sv)
3618SV* sv;
3619{
a0d0e21e
LW
3620 SV* rv = SvRV(sv);
3621
ed6116ce
LW
3622 SvRV(sv) = 0;
3623 SvROK_off(sv);
4633a7c4
LW
3624 if (SvREFCNT(rv) != 1 || SvREADONLY(rv))
3625 SvREFCNT_dec(rv);
8e07c86e 3626 else
4633a7c4 3627 sv_2mortal(rv); /* Schedule for freeing later */
ed6116ce 3628}
8990e307
LW
3629
3630#ifdef DEBUGGING
3631void
3632sv_dump(sv)
3633SV* sv;
3634{
3635 char tmpbuf[1024];
3636 char *d = tmpbuf;
3637 U32 flags;
3638 U32 type;
3639
3640 if (!sv) {
760ac839 3641 PerlIO_printf(Perl_debug_log, "SV = 0\n");
8990e307
LW
3642 return;
3643 }
3644
3645 flags = SvFLAGS(sv);
3646 type = SvTYPE(sv);
3647
3648 sprintf(d, "(0x%lx)\n REFCNT = %ld\n FLAGS = (",
3649 (unsigned long)SvANY(sv), (long)SvREFCNT(sv));
3650 d += strlen(d);
3651 if (flags & SVs_PADBUSY) strcat(d, "PADBUSY,");
3652 if (flags & SVs_PADTMP) strcat(d, "PADTMP,");
3653 if (flags & SVs_PADMY) strcat(d, "PADMY,");
3654 if (flags & SVs_TEMP) strcat(d, "TEMP,");
3655 if (flags & SVs_OBJECT) strcat(d, "OBJECT,");
3656 if (flags & SVs_GMG) strcat(d, "GMG,");
3657 if (flags & SVs_SMG) strcat(d, "SMG,");
3658 if (flags & SVs_RMG) strcat(d, "RMG,");
3659 d += strlen(d);
3660
3661 if (flags & SVf_IOK) strcat(d, "IOK,");
3662 if (flags & SVf_NOK) strcat(d, "NOK,");
3663 if (flags & SVf_POK) strcat(d, "POK,");
3664 if (flags & SVf_ROK) strcat(d, "ROK,");
8990e307 3665 if (flags & SVf_OOK) strcat(d, "OOK,");
a0d0e21e 3666 if (flags & SVf_FAKE) strcat(d, "FAKE,");
8990e307
LW
3667 if (flags & SVf_READONLY) strcat(d, "READONLY,");
3668 d += strlen(d);
3669
1edc1566
PP
3670#ifdef OVERLOAD
3671 if (flags & SVf_AMAGIC) strcat(d, "OVERLOAD,");
3672#endif /* OVERLOAD */
8990e307
LW
3673 if (flags & SVp_IOK) strcat(d, "pIOK,");
3674 if (flags & SVp_NOK) strcat(d, "pNOK,");
3675 if (flags & SVp_POK) strcat(d, "pPOK,");
3676 if (flags & SVp_SCREAM) strcat(d, "SCREAM,");
1edc1566
PP
3677
3678 switch (type) {
3679 case SVt_PVCV:
3680 if (CvANON(sv)) strcat(d, "ANON,");
3681 if (CvCLONE(sv)) strcat(d, "CLONE,");
3682 if (CvCLONED(sv)) strcat(d, "CLONED,");
3683 break;
3684 case SVt_PVGV:
3685 if (GvMULTI(sv)) strcat(d, "MULTI,");
3686#ifdef OVERLOAD
3687 if (flags & SVpgv_AM) strcat(d, "withOVERLOAD,");
3688#endif /* OVERLOAD */
3689 }
3690
8990e307
LW
3691 d += strlen(d);
3692 if (d[-1] == ',')
3693 d--;
3694 *d++ = ')';
3695 *d = '\0';
3696
760ac839 3697 PerlIO_printf(Perl_debug_log, "SV = ");
8990e307
LW
3698 switch (type) {
3699 case SVt_NULL:
760ac839 3700 PerlIO_printf(Perl_debug_log, "NULL%s\n", tmpbuf);
8990e307
LW
3701 return;
3702 case SVt_IV:
760ac839 3703 PerlIO_printf(Perl_debug_log, "IV%s\n", tmpbuf);
8990e307
LW
3704 break;
3705 case SVt_NV:
760ac839 3706 PerlIO_printf(Perl_debug_log, "NV%s\n", tmpbuf);
8990e307
LW
3707 break;
3708 case SVt_RV:
760ac839 3709 PerlIO_printf(Perl_debug_log, "RV%s\n", tmpbuf);
8990e307
LW
3710 break;
3711 case SVt_PV:
760ac839 3712 PerlIO_printf(Perl_debug_log, "PV%s\n", tmpbuf);
8990e307
LW
3713 break;
3714 case SVt_PVIV:
760ac839 3715 PerlIO_printf(Perl_debug_log, "PVIV%s\n", tmpbuf);
8990e307
LW
3716 break;
3717 case SVt_PVNV:
760ac839 3718 PerlIO_printf(Perl_debug_log, "PVNV%s\n", tmpbuf);
8990e307
LW
3719 break;
3720 case SVt_PVBM:
760ac839 3721 PerlIO_printf(Perl_debug_log, "PVBM%s\n", tmpbuf);
8990e307
LW
3722 break;
3723 case SVt_PVMG:
760ac839 3724 PerlIO_printf(Perl_debug_log, "PVMG%s\n", tmpbuf);
8990e307
LW
3725 break;
3726 case SVt_PVLV:
760ac839 3727 PerlIO_printf(Perl_debug_log, "PVLV%s\n", tmpbuf);
8990e307
LW
3728 break;
3729 case SVt_PVAV:
760ac839 3730 PerlIO_printf(Perl_debug_log, "PVAV%s\n", tmpbuf);
8990e307
LW
3731 break;
3732 case SVt_PVHV:
760ac839 3733 PerlIO_printf(Perl_debug_log, "PVHV%s\n", tmpbuf);
8990e307
LW
3734 break;
3735 case SVt_PVCV:
760ac839 3736 PerlIO_printf(Perl_debug_log, "PVCV%s\n", tmpbuf);
8990e307
LW
3737 break;
3738 case SVt_PVGV:
760ac839 3739 PerlIO_printf(Perl_debug_log, "PVGV%s\n", tmpbuf);
8990e307
LW
3740 break;
3741 case SVt_PVFM:
760ac839 3742 PerlIO_printf(Perl_debug_log, "PVFM%s\n", tmpbuf);
8990e307
LW
3743 break;
3744 case SVt_PVIO:
760ac839 3745 PerlIO_printf(Perl_debug_log, "PVIO%s\n", tmpbuf);
8990e307
LW
3746 break;
3747 default:
760ac839 3748 PerlIO_printf(Perl_debug_log, "UNKNOWN%s\n", tmpbuf);
8990e307
LW
3749 return;
3750 }
3751 if (type >= SVt_PVIV || type == SVt_IV)
760ac839 3752 PerlIO_printf(Perl_debug_log, " IV = %ld\n", (long)SvIVX(sv));
8990e307 3753 if (type >= SVt_PVNV || type == SVt_NV)
760ac839 3754 PerlIO_printf(Perl_debug_log, " NV = %.*g\n", DBL_DIG, SvNVX(sv));
8990e307 3755 if (SvROK(sv)) {
760ac839 3756 PerlIO_printf(Perl_debug_log, " RV = 0x%lx\n", (long)SvRV(sv));
8990e307
LW
3757 sv_dump(SvRV(sv));
3758 return;
3759 }
3760 if (type < SVt_PV)
3761 return;
3762 if (type <= SVt_PVLV) {
3763 if (SvPVX(sv))
760ac839 3764 PerlIO_printf(Perl_debug_log, " PV = 0x%lx \"%s\"\n CUR = %ld\n LEN = %ld\n",
a0d0e21e 3765 (long)SvPVX(sv), SvPVX(sv), (long)SvCUR(sv), (long)SvLEN(sv));
8990e307 3766 else
760ac839 3767 PerlIO_printf(Perl_debug_log, " PV = 0\n");
8990e307
LW
3768 }
3769 if (type >= SVt_PVMG) {
3770 if (SvMAGIC(sv)) {
760ac839 3771 PerlIO_printf(Perl_debug_log, " MAGIC = 0x%lx\n", (long)SvMAGIC(sv));
8990e307
LW
3772 }
3773 if (SvSTASH(sv))
760ac839 3774 PerlIO_printf(Perl_debug_log, " STASH = \"%s\"\n", HvNAME(SvSTASH(sv)));
8990e307
LW
3775 }
3776 switch (type) {
3777 case SVt_PVLV:
760ac839
LW
3778 PerlIO_printf(Perl_debug_log, " TYPE = %c\n", LvTYPE(sv));
3779 PerlIO_printf(Perl_debug_log, " TARGOFF = %ld\n", (long)LvTARGOFF(sv));
3780 PerlIO_printf(Perl_debug_log, " TARGLEN = %ld\n", (long)LvTARGLEN(sv));
3781 PerlIO_printf(Perl_debug_log, " TARG = 0x%lx\n", (long)LvTARG(sv));
8990e307
LW
3782 sv_dump(LvTARG(sv));
3783 break;
3784 case SVt_PVAV:
760ac839
LW
3785 PerlIO_printf(Perl_debug_log, " ARRAY = 0x%lx\n", (long)AvARRAY(sv));
3786 PerlIO_printf(Perl_debug_log, " ALLOC = 0x%lx\n", (long)AvALLOC(sv));
3787 PerlIO_printf(Perl_debug_log, " FILL = %ld\n", (long)AvFILL(sv));
3788 PerlIO_printf(Perl_debug_log, " MAX = %ld\n", (long)AvMAX(sv));
3789 PerlIO_printf(Perl_debug_log, " ARYLEN = 0x%lx\n", (long)AvARYLEN(sv));
4633a7c4
LW
3790 flags = AvFLAGS(sv);
3791 d = tmpbuf;
1edc1566 3792 *d = '\0';
4633a7c4
LW
3793 if (flags & AVf_REAL) strcat(d, "REAL,");
3794 if (flags & AVf_REIFY) strcat(d, "REIFY,");
3795 if (flags & AVf_REUSED) strcat(d, "REUSED,");
3796 if (*d)
3797 d[strlen(d)-1] = '\0';
760ac839 3798 PerlIO_printf(Perl_debug_log, " FLAGS = (%s)\n", d);
8990e307
LW
3799 break;
3800 case SVt_PVHV:
760ac839
LW
3801 PerlIO_printf(Perl_debug_log, " ARRAY = 0x%lx\n",(long)HvARRAY(sv));
3802 PerlIO_printf(Perl_debug_log, " KEYS = %ld\n", (long)HvKEYS(sv));
3803 PerlIO_printf(Perl_debug_log, " FILL = %ld\n", (long)HvFILL(sv));
3804 PerlIO_printf(Perl_debug_log, " MAX = %ld\n", (long)HvMAX(sv));
3805 PerlIO_printf(Perl_debug_log, " RITER = %ld\n", (long)HvRITER(sv));
3806 PerlIO_printf(Perl_debug_log, " EITER = 0x%lx\n",(long) HvEITER(sv));
8990e307 3807 if (HvPMROOT(sv))
760ac839 3808 PerlIO_printf(Perl_debug_log, " PMROOT = 0x%lx\n",(long)HvPMROOT(sv));
8990e307 3809 if (HvNAME(sv))
760ac839 3810 PerlIO_printf(Perl_debug_log, " NAME = \"%s\"\n", HvNAME(sv));
8990e307
LW
3811 break;
3812 case SVt_PVFM:
3813 case SVt_PVCV:
1edc1566 3814 if (SvPOK(sv))
760ac839
LW
3815 PerlIO_printf(Perl_debug_log, " PROTOTYPE = \"%s\"\n", SvPV(sv,na));
3816 PerlIO_printf(Perl_debug_log, " STASH = 0x%lx\n", (long)CvSTASH(sv));
3817 PerlIO_printf(Perl_debug_log, " START = 0x%lx\n", (long)CvSTART(sv));
3818 PerlIO_printf(Perl_debug_log, " ROOT = 0x%lx\n", (long)CvROOT(sv));
3819 PerlIO_printf(Perl_debug_log, " XSUB = 0x%lx\n", (long)CvXSUB(sv));
3820 PerlIO_printf(Perl_debug_log, " XSUBANY = %ld\n", (long)CvXSUBANY(sv).any_i32);
d1bf51dd 3821 PerlIO_printf(Perl_debug_log, " GV = 0x%lx", (long)CvGV(sv));
1edc1566 3822 if (CvGV(sv) && GvNAME(CvGV(sv))) {
d1bf51dd 3823 PerlIO_printf(Perl_debug_log, " \"%s\"\n", GvNAME(CvGV(sv)));
1edc1566 3824 } else {
d1bf51dd 3825 PerlIO_printf(Perl_debug_log, "\n");
1edc1566 3826 }
760ac839
LW
3827 PerlIO_printf(Perl_debug_log, " FILEGV = 0x%lx\n", (long)CvFILEGV(sv));
3828 PerlIO_printf(Perl_debug_log, " DEPTH = %ld\n", (long)CvDEPTH(sv));
3829 PerlIO_printf(Perl_debug_log, " PADLIST = 0x%lx\n", (long)CvPADLIST(sv));
3830 PerlIO_printf(Perl_debug_log, " OUTSIDE = 0x%lx\n", (long)CvOUTSIDE(sv));
8990e307 3831 if (type == SVt_PVFM)
760ac839 3832 PerlIO_printf(Perl_debug_log, " LINES = %ld\n", (long)FmLINES(sv));
8990e307
LW
3833 break;
3834 case SVt_PVGV:
760ac839
LW
3835 PerlIO_printf(Perl_debug_log, " NAME = \"%s\"\n", GvNAME(sv));
3836 PerlIO_printf(Perl_debug_log, " NAMELEN = %ld\n", (long)GvNAMELEN(sv));
3837 PerlIO_printf(Perl_debug_log, " STASH = \"%s\"\n", HvNAME(GvSTASH(sv)));
3838 PerlIO_printf(Perl_debug_log, " GP = 0x%lx\n", (long)GvGP(sv));
3839 PerlIO_printf(Perl_debug_log, " SV = 0x%lx\n", (long)GvSV(sv));
3840 PerlIO_printf(Perl_debug_log, " REFCNT = %ld\n", (long)GvREFCNT(sv));
3841 PerlIO_printf(Perl_debug_log, " IO = 0x%lx\n", (long)GvIOp(sv));
3842 PerlIO_printf(Perl_debug_log, " FORM = 0x%lx\n", (long)GvFORM(sv));
3843 PerlIO_printf(Perl_debug_log, " AV = 0x%lx\n", (long)GvAV(sv));
3844 PerlIO_printf(Perl_debug_log, " HV = 0x%lx\n", (long)GvHV(sv));
3845 PerlIO_printf(Perl_debug_log, " CV = 0x%lx\n", (long)GvCV(sv));
3846 PerlIO_printf(Perl_debug_log, " CVGEN = 0x%lx\n", (long)GvCVGEN(sv));
3847 PerlIO_printf(Perl_debug_log, " LASTEXPR = %ld\n", (long)GvLASTEXPR(sv));
3848 PerlIO_printf(Perl_debug_log, " LINE = %ld\n", (long)GvLINE(sv));
3849 PerlIO_printf(Perl_debug_log, " FLAGS = 0x%x\n", (int)GvFLAGS(sv));
3850 PerlIO_printf(Perl_debug_log, " STASH = \"%s\"\n", HvNAME(GvSTASH(sv)));
3851 PerlIO_printf(Perl_debug_log, " EGV = 0x%lx\n", (long)GvEGV(sv));
8990e307
LW
3852 break;
3853 case SVt_PVIO:
760ac839
LW
3854 PerlIO_printf(Perl_debug_log, " IFP = 0x%lx\n", (long)IoIFP(sv));
3855 PerlIO_printf(Perl_debug_log, " OFP = 0x%lx\n", (long)IoOFP(sv));
3856 PerlIO_printf(Perl_debug_log, " DIRP = 0x%lx\n", (long)IoDIRP(sv));
3857 PerlIO_printf(Perl_debug_log, " LINES = %ld\n", (long)IoLINES(sv));
3858 PerlIO_printf(Perl_debug_log, " PAGE = %ld\n", (long)IoPAGE(sv));
3859 PerlIO_printf(Perl_debug_log, " PAGE_LEN = %ld\n", (long)IoPAGE_LEN(sv));
3860 PerlIO_printf(Perl_debug_log, " LINES_LEFT = %ld\n", (long)IoLINES_LEFT(sv));
3861 PerlIO_printf(Perl_debug_log, " TOP_NAME = \"%s\"\n", IoTOP_NAME(sv));
3862 PerlIO_printf(Perl_debug_log, " TOP_GV = 0x%lx\n", (long)IoTOP_GV(sv));
3863 PerlIO_printf(Perl_debug_log, " FMT_NAME = \"%s\"\n", IoFMT_NAME(sv));
3864 PerlIO_printf(Perl_debug_log, " FMT_GV = 0x%lx\n", (long)IoFMT_GV(sv));
3865 PerlIO_printf(Perl_debug_log, " BOTTOM_NAME = \"%s\"\n", IoBOTTOM_NAME(sv));
3866 PerlIO_printf(Perl_debug_log, " BOTTOM_GV = 0x%lx\n", (long)IoBOTTOM_GV(sv));
3867 PerlIO_printf(Perl_debug_log, " SUBPROCESS = %ld\n", (long)IoSUBPROCESS(sv));
3868 PerlIO_printf(Perl_debug_log, " TYPE = %c\n", IoTYPE(sv));
3869 PerlIO_printf(Perl_debug_log, " FLAGS = 0x%lx\n", (long)IoFLAGS(sv));
8990e307
LW
3870 break;
3871 }
3872}
2304df62
AD
3873#else
3874void
3875sv_dump(sv)
3876SV* sv;
3877{
3878}
8990e307 3879#endif
a0d0e21e
LW
3880
3881IO*
3882sv_2io(sv)
3883SV *sv;
3884{
3885 IO* io;
3886 GV* gv;
3887
3888 switch (SvTYPE(sv)) {
3889 case SVt_PVIO:
3890 io = (IO*)sv;
3891 break;
3892 case SVt_PVGV:
3893 gv = (GV*)sv;
3894 io = GvIO(gv);
3895 if (!io)
3896 croak("Bad filehandle: %s", GvNAME(gv));
3897 break;
3898 default:
3899 if (!SvOK(sv))
3900 croak(no_usym, "filehandle");
3901 if (SvROK(sv))
3902 return sv_2io(SvRV(sv));
3903 gv = gv_fetchpv(SvPV(sv,na), FALSE, SVt_PVIO);
3904 if (gv)
3905 io = GvIO(gv);
3906 else
3907 io = 0;
3908 if (!io)
3909 croak("Bad filehandle: %s", SvPV(sv,na));
3910 break;
3911 }
3912 return io;
3913}
3914