This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Lexical use open ... support:
[perl5.git] / sv.c
CommitLineData
a0d0e21e 1/* sv.c
79072805 2 *
3818b22b 3 * Copyright (c) 1991-2000, 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"
864dbfa3 15#define PERL_IN_SV_C
79072805 16#include "perl.h"
79072805 17
51371543 18#define FCALL *f
6fc92669 19#define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) sv_force_normal(sv)
2c5424a7 20
51371543
GS
21static void do_report_used(pTHXo_ SV *sv);
22static void do_clean_objs(pTHXo_ SV *sv);
23#ifndef DISABLE_DESTRUCTOR_KLUDGE
24static void do_clean_named_objs(pTHXo_ SV *sv);
25#endif
26static void do_clean_all(pTHXo_ SV *sv);
27
4561caa4
CS
28/*
29 * "A time to plant, and a time to uproot what was planted..."
30 */
31
053fc874
GS
32#define plant_SV(p) \
33 STMT_START { \
34 SvANY(p) = (void *)PL_sv_root; \
35 SvFLAGS(p) = SVTYPEMASK; \
36 PL_sv_root = (p); \
37 --PL_sv_count; \
38 } STMT_END
a0d0e21e 39
fba3b22e 40/* sv_mutex must be held while calling uproot_SV() */
053fc874
GS
41#define uproot_SV(p) \
42 STMT_START { \
43 (p) = PL_sv_root; \
44 PL_sv_root = (SV*)SvANY(p); \
45 ++PL_sv_count; \
46 } STMT_END
47
48#define new_SV(p) \
49 STMT_START { \
50 LOCK_SV_MUTEX; \
51 if (PL_sv_root) \
52 uproot_SV(p); \
53 else \
54 (p) = more_sv(); \
55 UNLOCK_SV_MUTEX; \
56 SvANY(p) = 0; \
57 SvREFCNT(p) = 1; \
58 SvFLAGS(p) = 0; \
59 } STMT_END
463ee0b2 60
a0d0e21e 61#ifdef DEBUGGING
4561caa4 62
053fc874
GS
63#define del_SV(p) \
64 STMT_START { \
65 LOCK_SV_MUTEX; \
66 if (PL_debug & 32768) \
67 del_sv(p); \
68 else \
69 plant_SV(p); \
70 UNLOCK_SV_MUTEX; \
71 } STMT_END
a0d0e21e 72
76e3520e 73STATIC void
cea2e8a9 74S_del_sv(pTHX_ SV *p)
463ee0b2 75{
3280af22 76 if (PL_debug & 32768) {
4633a7c4 77 SV* sva;
a0d0e21e
LW
78 SV* sv;
79 SV* svend;
80 int ok = 0;
3280af22 81 for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
4633a7c4
LW
82 sv = sva + 1;
83 svend = &sva[SvREFCNT(sva)];
a0d0e21e
LW
84 if (p >= sv && p < svend)
85 ok = 1;
86 }
87 if (!ok) {
0453d815
PM
88 if (ckWARN_d(WARN_INTERNAL))
89 Perl_warner(aTHX_ WARN_INTERNAL,
1d7c1841
GS
90 "Attempt to free non-arena SV: 0x%"UVxf,
91 PTR2UV(p));
a0d0e21e
LW
92 return;
93 }
94 }
4561caa4 95 plant_SV(p);
463ee0b2 96}
a0d0e21e 97
4561caa4
CS
98#else /* ! DEBUGGING */
99
100#define del_SV(p) plant_SV(p)
101
102#endif /* DEBUGGING */
463ee0b2 103
4633a7c4 104void
864dbfa3 105Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
463ee0b2 106{
4633a7c4 107 SV* sva = (SV*)ptr;
463ee0b2
LW
108 register SV* sv;
109 register SV* svend;
14dd3ad8 110 Zero(ptr, size, char);
4633a7c4
LW
111
112 /* The first SV in an arena isn't an SV. */
3280af22 113 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
4633a7c4
LW
114 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
115 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
116
3280af22
NIS
117 PL_sv_arenaroot = sva;
118 PL_sv_root = sva + 1;
4633a7c4
LW
119
120 svend = &sva[SvREFCNT(sva) - 1];
121 sv = sva + 1;
463ee0b2 122 while (sv < svend) {
a0d0e21e 123 SvANY(sv) = (void *)(SV*)(sv + 1);
8990e307 124 SvFLAGS(sv) = SVTYPEMASK;
463ee0b2
LW
125 sv++;
126 }
127 SvANY(sv) = 0;
4633a7c4
LW
128 SvFLAGS(sv) = SVTYPEMASK;
129}
130
fba3b22e 131/* sv_mutex must be held while calling more_sv() */
76e3520e 132STATIC SV*
cea2e8a9 133S_more_sv(pTHX)
4633a7c4 134{
4561caa4
CS
135 register SV* sv;
136
3280af22
NIS
137 if (PL_nice_chunk) {
138 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
139 PL_nice_chunk = Nullch;
c07a80fd 140 }
1edc1566
PP
141 else {
142 char *chunk; /* must use New here to match call to */
143 New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */
144 sv_add_arena(chunk, 1008, 0);
145 }
4561caa4
CS
146 uproot_SV(sv);
147 return sv;
463ee0b2
LW
148}
149
76e3520e 150STATIC void
cea2e8a9 151S_visit(pTHX_ SVFUNC_t f)
8990e307 152{
4633a7c4 153 SV* sva;
8990e307
LW
154 SV* sv;
155 register SV* svend;
156
3280af22 157 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
4633a7c4 158 svend = &sva[SvREFCNT(sva)];
4561caa4
CS
159 for (sv = sva + 1; sv < svend; ++sv) {
160 if (SvTYPE(sv) != SVTYPEMASK)
51371543 161 (FCALL)(aTHXo_ sv);
8990e307
LW
162 }
163 }
164}
165
166void
864dbfa3 167Perl_sv_report_used(pTHX)
4561caa4 168{
0b94c7bb 169 visit(do_report_used);
4561caa4
CS
170}
171
4561caa4 172void
864dbfa3 173Perl_sv_clean_objs(pTHX)
4561caa4 174{
3280af22 175 PL_in_clean_objs = TRUE;
0b94c7bb 176 visit(do_clean_objs);
4561caa4 177#ifndef DISABLE_DESTRUCTOR_KLUDGE
2d0f3c12 178 /* some barnacles may yet remain, clinging to typeglobs */
0b94c7bb 179 visit(do_clean_named_objs);
4561caa4 180#endif
3280af22 181 PL_in_clean_objs = FALSE;
4561caa4
CS
182}
183
8990e307 184void
864dbfa3 185Perl_sv_clean_all(pTHX)
8990e307 186{
3280af22 187 PL_in_clean_all = TRUE;
0b94c7bb 188 visit(do_clean_all);
3280af22 189 PL_in_clean_all = FALSE;
8990e307 190}
463ee0b2 191
4633a7c4 192void
864dbfa3 193Perl_sv_free_arenas(pTHX)
4633a7c4
LW
194{
195 SV* sva;
196 SV* svanext;
612f20c3 197 XPV *arena, *arenanext;
4633a7c4
LW
198
199 /* Free arenas here, but be careful about fake ones. (We assume
200 contiguity of the fake ones with the corresponding real ones.) */
201
3280af22 202 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
4633a7c4
LW
203 svanext = (SV*) SvANY(sva);
204 while (svanext && SvFAKE(svanext))
205 svanext = (SV*) SvANY(svanext);
206
207 if (!SvFAKE(sva))
1edc1566 208 Safefree((void *)sva);
4633a7c4 209 }
5f05dabc 210
612f20c3
GS
211 for (arena = PL_xiv_arenaroot; arena; arena = arenanext) {
212 arenanext = (XPV*)arena->xpv_pv;
213 Safefree(arena);
214 }
215 PL_xiv_arenaroot = 0;
216
217 for (arena = PL_xnv_arenaroot; arena; arena = arenanext) {
218 arenanext = (XPV*)arena->xpv_pv;
219 Safefree(arena);
220 }
221 PL_xnv_arenaroot = 0;
222
223 for (arena = PL_xrv_arenaroot; arena; arena = arenanext) {
224 arenanext = (XPV*)arena->xpv_pv;
225 Safefree(arena);
226 }
227 PL_xrv_arenaroot = 0;
228
229 for (arena = PL_xpv_arenaroot; arena; arena = arenanext) {
230 arenanext = (XPV*)arena->xpv_pv;
231 Safefree(arena);
232 }
233 PL_xpv_arenaroot = 0;
234
235 for (arena = (XPV*)PL_xpviv_arenaroot; arena; arena = arenanext) {
236 arenanext = (XPV*)arena->xpv_pv;
237 Safefree(arena);
238 }
239 PL_xpviv_arenaroot = 0;
240
241 for (arena = (XPV*)PL_xpvnv_arenaroot; arena; arena = arenanext) {
242 arenanext = (XPV*)arena->xpv_pv;
243 Safefree(arena);
244 }
245 PL_xpvnv_arenaroot = 0;
246
247 for (arena = (XPV*)PL_xpvcv_arenaroot; arena; arena = arenanext) {
248 arenanext = (XPV*)arena->xpv_pv;
249 Safefree(arena);
250 }
251 PL_xpvcv_arenaroot = 0;
252
253 for (arena = (XPV*)PL_xpvav_arenaroot; arena; arena = arenanext) {
254 arenanext = (XPV*)arena->xpv_pv;
255 Safefree(arena);
256 }
257 PL_xpvav_arenaroot = 0;
258
259 for (arena = (XPV*)PL_xpvhv_arenaroot; arena; arena = arenanext) {
260 arenanext = (XPV*)arena->xpv_pv;
261 Safefree(arena);
262 }
263 PL_xpvhv_arenaroot = 0;
264
265 for (arena = (XPV*)PL_xpvmg_arenaroot; arena; arena = arenanext) {
266 arenanext = (XPV*)arena->xpv_pv;
267 Safefree(arena);
268 }
269 PL_xpvmg_arenaroot = 0;
270
271 for (arena = (XPV*)PL_xpvlv_arenaroot; arena; arena = arenanext) {
272 arenanext = (XPV*)arena->xpv_pv;
273 Safefree(arena);
274 }
275 PL_xpvlv_arenaroot = 0;
276
277 for (arena = (XPV*)PL_xpvbm_arenaroot; arena; arena = arenanext) {
278 arenanext = (XPV*)arena->xpv_pv;
279 Safefree(arena);
280 }
281 PL_xpvbm_arenaroot = 0;
282
283 for (arena = (XPV*)PL_he_arenaroot; arena; arena = arenanext) {
284 arenanext = (XPV*)arena->xpv_pv;
285 Safefree(arena);
286 }
287 PL_he_arenaroot = 0;
288
3280af22
NIS
289 if (PL_nice_chunk)
290 Safefree(PL_nice_chunk);
291 PL_nice_chunk = Nullch;
292 PL_nice_chunk_size = 0;
293 PL_sv_arenaroot = 0;
294 PL_sv_root = 0;
4633a7c4
LW
295}
296
1d7c1841
GS
297void
298Perl_report_uninit(pTHX)
299{
300 if (PL_op)
301 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit,
302 " in ", PL_op_desc[PL_op->op_type]);
303 else
304 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit, "", "");
305}
306
76e3520e 307STATIC XPVIV*
cea2e8a9 308S_new_xiv(pTHX)
463ee0b2 309{
ea7c11a3 310 IV* xiv;
cbe51380
GS
311 LOCK_SV_MUTEX;
312 if (!PL_xiv_root)
313 more_xiv();
314 xiv = PL_xiv_root;
315 /*
316 * See comment in more_xiv() -- RAM.
317 */
318 PL_xiv_root = *(IV**)xiv;
319 UNLOCK_SV_MUTEX;
320 return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv));
463ee0b2
LW
321}
322
76e3520e 323STATIC void
cea2e8a9 324S_del_xiv(pTHX_ XPVIV *p)
463ee0b2 325{
23e6a22f 326 IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv));
cbe51380 327 LOCK_SV_MUTEX;
3280af22
NIS
328 *(IV**)xiv = PL_xiv_root;
329 PL_xiv_root = xiv;
cbe51380 330 UNLOCK_SV_MUTEX;
463ee0b2
LW
331}
332
cbe51380 333STATIC void
cea2e8a9 334S_more_xiv(pTHX)
463ee0b2 335{
ea7c11a3
SM
336 register IV* xiv;
337 register IV* xivend;
8c52afec
IZ
338 XPV* ptr;
339 New(705, ptr, 1008/sizeof(XPV), XPV);
3280af22
NIS
340 ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */
341 PL_xiv_arenaroot = ptr; /* to keep Purify happy */
a0d0e21e 342
ea7c11a3
SM
343 xiv = (IV*) ptr;
344 xivend = &xiv[1008 / sizeof(IV) - 1];
345 xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */
3280af22 346 PL_xiv_root = xiv;
463ee0b2 347 while (xiv < xivend) {
ea7c11a3 348 *(IV**)xiv = (IV *)(xiv + 1);
463ee0b2
LW
349 xiv++;
350 }
ea7c11a3 351 *(IV**)xiv = 0;
463ee0b2
LW
352}
353
76e3520e 354STATIC XPVNV*
cea2e8a9 355S_new_xnv(pTHX)
463ee0b2 356{
65202027 357 NV* xnv;
cbe51380
GS
358 LOCK_SV_MUTEX;
359 if (!PL_xnv_root)
360 more_xnv();
361 xnv = PL_xnv_root;
65202027 362 PL_xnv_root = *(NV**)xnv;
cbe51380
GS
363 UNLOCK_SV_MUTEX;
364 return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv));
463ee0b2
LW
365}
366
76e3520e 367STATIC void
cea2e8a9 368S_del_xnv(pTHX_ XPVNV *p)
463ee0b2 369{
65202027 370 NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv));
cbe51380 371 LOCK_SV_MUTEX;
65202027 372 *(NV**)xnv = PL_xnv_root;
3280af22 373 PL_xnv_root = xnv;
cbe51380 374 UNLOCK_SV_MUTEX;
463ee0b2
LW
375}
376
cbe51380 377STATIC void
cea2e8a9 378S_more_xnv(pTHX)
463ee0b2 379{
65202027
DS
380 register NV* xnv;
381 register NV* xnvend;
612f20c3
GS
382 XPV *ptr;
383 New(711, ptr, 1008/sizeof(XPV), XPV);
384 ptr->xpv_pv = (char*)PL_xnv_arenaroot;
385 PL_xnv_arenaroot = ptr;
386
387 xnv = (NV*) ptr;
65202027
DS
388 xnvend = &xnv[1008 / sizeof(NV) - 1];
389 xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */
3280af22 390 PL_xnv_root = xnv;
463ee0b2 391 while (xnv < xnvend) {
65202027 392 *(NV**)xnv = (NV*)(xnv + 1);
463ee0b2
LW
393 xnv++;
394 }
65202027 395 *(NV**)xnv = 0;
463ee0b2
LW
396}
397
76e3520e 398STATIC XRV*
cea2e8a9 399S_new_xrv(pTHX)
ed6116ce
LW
400{
401 XRV* xrv;
cbe51380
GS
402 LOCK_SV_MUTEX;
403 if (!PL_xrv_root)
404 more_xrv();
405 xrv = PL_xrv_root;
406 PL_xrv_root = (XRV*)xrv->xrv_rv;
407 UNLOCK_SV_MUTEX;
408 return xrv;
ed6116ce
LW
409}
410
76e3520e 411STATIC void
cea2e8a9 412S_del_xrv(pTHX_ XRV *p)
ed6116ce 413{
cbe51380 414 LOCK_SV_MUTEX;
3280af22
NIS
415 p->xrv_rv = (SV*)PL_xrv_root;
416 PL_xrv_root = p;
cbe51380 417 UNLOCK_SV_MUTEX;
ed6116ce
LW
418}
419
cbe51380 420STATIC void
cea2e8a9 421S_more_xrv(pTHX)
ed6116ce 422{
ed6116ce
LW
423 register XRV* xrv;
424 register XRV* xrvend;
612f20c3
GS
425 XPV *ptr;
426 New(712, ptr, 1008/sizeof(XPV), XPV);
427 ptr->xpv_pv = (char*)PL_xrv_arenaroot;
428 PL_xrv_arenaroot = ptr;
429
430 xrv = (XRV*) ptr;
ed6116ce 431 xrvend = &xrv[1008 / sizeof(XRV) - 1];
612f20c3
GS
432 xrv += (sizeof(XPV) - 1) / sizeof(XRV) + 1;
433 PL_xrv_root = xrv;
ed6116ce
LW
434 while (xrv < xrvend) {
435 xrv->xrv_rv = (SV*)(xrv + 1);
436 xrv++;
437 }
438 xrv->xrv_rv = 0;
ed6116ce
LW
439}
440
76e3520e 441STATIC XPV*
cea2e8a9 442S_new_xpv(pTHX)
463ee0b2
LW
443{
444 XPV* xpv;
cbe51380
GS
445 LOCK_SV_MUTEX;
446 if (!PL_xpv_root)
447 more_xpv();
448 xpv = PL_xpv_root;
449 PL_xpv_root = (XPV*)xpv->xpv_pv;
450 UNLOCK_SV_MUTEX;
451 return xpv;
463ee0b2
LW
452}
453
76e3520e 454STATIC void
cea2e8a9 455S_del_xpv(pTHX_ XPV *p)
463ee0b2 456{
cbe51380 457 LOCK_SV_MUTEX;
3280af22
NIS
458 p->xpv_pv = (char*)PL_xpv_root;
459 PL_xpv_root = p;
cbe51380 460 UNLOCK_SV_MUTEX;
463ee0b2
LW
461}
462
cbe51380 463STATIC void
cea2e8a9 464S_more_xpv(pTHX)
463ee0b2 465{
463ee0b2
LW
466 register XPV* xpv;
467 register XPV* xpvend;
612f20c3
GS
468 New(713, xpv, 1008/sizeof(XPV), XPV);
469 xpv->xpv_pv = (char*)PL_xpv_arenaroot;
470 PL_xpv_arenaroot = xpv;
471
463ee0b2 472 xpvend = &xpv[1008 / sizeof(XPV) - 1];
612f20c3 473 PL_xpv_root = ++xpv;
463ee0b2
LW
474 while (xpv < xpvend) {
475 xpv->xpv_pv = (char*)(xpv + 1);
476 xpv++;
477 }
478 xpv->xpv_pv = 0;
463ee0b2
LW
479}
480
932e9ff9
VB
481STATIC XPVIV*
482S_new_xpviv(pTHX)
483{
484 XPVIV* xpviv;
485 LOCK_SV_MUTEX;
486 if (!PL_xpviv_root)
487 more_xpviv();
488 xpviv = PL_xpviv_root;
489 PL_xpviv_root = (XPVIV*)xpviv->xpv_pv;
490 UNLOCK_SV_MUTEX;
491 return xpviv;
492}
493
494STATIC void
495S_del_xpviv(pTHX_ XPVIV *p)
496{
497 LOCK_SV_MUTEX;
498 p->xpv_pv = (char*)PL_xpviv_root;
499 PL_xpviv_root = p;
500 UNLOCK_SV_MUTEX;
501}
502
932e9ff9
VB
503STATIC void
504S_more_xpviv(pTHX)
505{
506 register XPVIV* xpviv;
507 register XPVIV* xpvivend;
612f20c3
GS
508 New(714, xpviv, 1008/sizeof(XPVIV), XPVIV);
509 xpviv->xpv_pv = (char*)PL_xpviv_arenaroot;
510 PL_xpviv_arenaroot = xpviv;
511
932e9ff9 512 xpvivend = &xpviv[1008 / sizeof(XPVIV) - 1];
612f20c3 513 PL_xpviv_root = ++xpviv;
932e9ff9
VB
514 while (xpviv < xpvivend) {
515 xpviv->xpv_pv = (char*)(xpviv + 1);
516 xpviv++;
517 }
518 xpviv->xpv_pv = 0;
519}
520
932e9ff9
VB
521STATIC XPVNV*
522S_new_xpvnv(pTHX)
523{
524 XPVNV* xpvnv;
525 LOCK_SV_MUTEX;
526 if (!PL_xpvnv_root)
527 more_xpvnv();
528 xpvnv = PL_xpvnv_root;
529 PL_xpvnv_root = (XPVNV*)xpvnv->xpv_pv;
530 UNLOCK_SV_MUTEX;
531 return xpvnv;
532}
533
534STATIC void
535S_del_xpvnv(pTHX_ XPVNV *p)
536{
537 LOCK_SV_MUTEX;
538 p->xpv_pv = (char*)PL_xpvnv_root;
539 PL_xpvnv_root = p;
540 UNLOCK_SV_MUTEX;
541}
542
932e9ff9
VB
543STATIC void
544S_more_xpvnv(pTHX)
545{
546 register XPVNV* xpvnv;
547 register XPVNV* xpvnvend;
612f20c3
GS
548 New(715, xpvnv, 1008/sizeof(XPVNV), XPVNV);
549 xpvnv->xpv_pv = (char*)PL_xpvnv_arenaroot;
550 PL_xpvnv_arenaroot = xpvnv;
551
932e9ff9 552 xpvnvend = &xpvnv[1008 / sizeof(XPVNV) - 1];
612f20c3 553 PL_xpvnv_root = ++xpvnv;
932e9ff9
VB
554 while (xpvnv < xpvnvend) {
555 xpvnv->xpv_pv = (char*)(xpvnv + 1);
556 xpvnv++;
557 }
558 xpvnv->xpv_pv = 0;
559}
560
932e9ff9
VB
561STATIC XPVCV*
562S_new_xpvcv(pTHX)
563{
564 XPVCV* xpvcv;
565 LOCK_SV_MUTEX;
566 if (!PL_xpvcv_root)
567 more_xpvcv();
568 xpvcv = PL_xpvcv_root;
569 PL_xpvcv_root = (XPVCV*)xpvcv->xpv_pv;
570 UNLOCK_SV_MUTEX;
571 return xpvcv;
572}
573
574STATIC void
575S_del_xpvcv(pTHX_ XPVCV *p)
576{
577 LOCK_SV_MUTEX;
578 p->xpv_pv = (char*)PL_xpvcv_root;
579 PL_xpvcv_root = p;
580 UNLOCK_SV_MUTEX;
581}
582
932e9ff9
VB
583STATIC void
584S_more_xpvcv(pTHX)
585{
586 register XPVCV* xpvcv;
587 register XPVCV* xpvcvend;
612f20c3
GS
588 New(716, xpvcv, 1008/sizeof(XPVCV), XPVCV);
589 xpvcv->xpv_pv = (char*)PL_xpvcv_arenaroot;
590 PL_xpvcv_arenaroot = xpvcv;
591
932e9ff9 592 xpvcvend = &xpvcv[1008 / sizeof(XPVCV) - 1];
612f20c3 593 PL_xpvcv_root = ++xpvcv;
932e9ff9
VB
594 while (xpvcv < xpvcvend) {
595 xpvcv->xpv_pv = (char*)(xpvcv + 1);
596 xpvcv++;
597 }
598 xpvcv->xpv_pv = 0;
599}
600
932e9ff9
VB
601STATIC XPVAV*
602S_new_xpvav(pTHX)
603{
604 XPVAV* xpvav;
605 LOCK_SV_MUTEX;
606 if (!PL_xpvav_root)
607 more_xpvav();
608 xpvav = PL_xpvav_root;
609 PL_xpvav_root = (XPVAV*)xpvav->xav_array;
610 UNLOCK_SV_MUTEX;
611 return xpvav;
612}
613
614STATIC void
615S_del_xpvav(pTHX_ XPVAV *p)
616{
617 LOCK_SV_MUTEX;
618 p->xav_array = (char*)PL_xpvav_root;
619 PL_xpvav_root = p;
620 UNLOCK_SV_MUTEX;
621}
622
932e9ff9
VB
623STATIC void
624S_more_xpvav(pTHX)
625{
626 register XPVAV* xpvav;
627 register XPVAV* xpvavend;
612f20c3
GS
628 New(717, xpvav, 1008/sizeof(XPVAV), XPVAV);
629 xpvav->xav_array = (char*)PL_xpvav_arenaroot;
630 PL_xpvav_arenaroot = xpvav;
631
932e9ff9 632 xpvavend = &xpvav[1008 / sizeof(XPVAV) - 1];
612f20c3 633 PL_xpvav_root = ++xpvav;
932e9ff9
VB
634 while (xpvav < xpvavend) {
635 xpvav->xav_array = (char*)(xpvav + 1);
636 xpvav++;
637 }
638 xpvav->xav_array = 0;
639}
640
932e9ff9
VB
641STATIC XPVHV*
642S_new_xpvhv(pTHX)
643{
644 XPVHV* xpvhv;
645 LOCK_SV_MUTEX;
646 if (!PL_xpvhv_root)
647 more_xpvhv();
648 xpvhv = PL_xpvhv_root;
649 PL_xpvhv_root = (XPVHV*)xpvhv->xhv_array;
650 UNLOCK_SV_MUTEX;
651 return xpvhv;
652}
653
654STATIC void
655S_del_xpvhv(pTHX_ XPVHV *p)
656{
657 LOCK_SV_MUTEX;
658 p->xhv_array = (char*)PL_xpvhv_root;
659 PL_xpvhv_root = p;
660 UNLOCK_SV_MUTEX;
661}
662
932e9ff9
VB
663STATIC void
664S_more_xpvhv(pTHX)
665{
666 register XPVHV* xpvhv;
667 register XPVHV* xpvhvend;
612f20c3
GS
668 New(718, xpvhv, 1008/sizeof(XPVHV), XPVHV);
669 xpvhv->xhv_array = (char*)PL_xpvhv_arenaroot;
670 PL_xpvhv_arenaroot = xpvhv;
671
932e9ff9 672 xpvhvend = &xpvhv[1008 / sizeof(XPVHV) - 1];
612f20c3 673 PL_xpvhv_root = ++xpvhv;
932e9ff9
VB
674 while (xpvhv < xpvhvend) {
675 xpvhv->xhv_array = (char*)(xpvhv + 1);
676 xpvhv++;
677 }
678 xpvhv->xhv_array = 0;
679}
680
932e9ff9
VB
681STATIC XPVMG*
682S_new_xpvmg(pTHX)
683{
684 XPVMG* xpvmg;
685 LOCK_SV_MUTEX;
686 if (!PL_xpvmg_root)
687 more_xpvmg();
688 xpvmg = PL_xpvmg_root;
689 PL_xpvmg_root = (XPVMG*)xpvmg->xpv_pv;
690 UNLOCK_SV_MUTEX;
691 return xpvmg;
692}
693
694STATIC void
695S_del_xpvmg(pTHX_ XPVMG *p)
696{
697 LOCK_SV_MUTEX;
698 p->xpv_pv = (char*)PL_xpvmg_root;
699 PL_xpvmg_root = p;
700 UNLOCK_SV_MUTEX;
701}
702
932e9ff9
VB
703STATIC void
704S_more_xpvmg(pTHX)
705{
706 register XPVMG* xpvmg;
707 register XPVMG* xpvmgend;
612f20c3
GS
708 New(719, xpvmg, 1008/sizeof(XPVMG), XPVMG);
709 xpvmg->xpv_pv = (char*)PL_xpvmg_arenaroot;
710 PL_xpvmg_arenaroot = xpvmg;
711
932e9ff9 712 xpvmgend = &xpvmg[1008 / sizeof(XPVMG) - 1];
612f20c3 713 PL_xpvmg_root = ++xpvmg;
932e9ff9
VB
714 while (xpvmg < xpvmgend) {
715 xpvmg->xpv_pv = (char*)(xpvmg + 1);
716 xpvmg++;
717 }
718 xpvmg->xpv_pv = 0;
719}
720
932e9ff9
VB
721STATIC XPVLV*
722S_new_xpvlv(pTHX)
723{
724 XPVLV* xpvlv;
725 LOCK_SV_MUTEX;
726 if (!PL_xpvlv_root)
727 more_xpvlv();
728 xpvlv = PL_xpvlv_root;
729 PL_xpvlv_root = (XPVLV*)xpvlv->xpv_pv;
730 UNLOCK_SV_MUTEX;
731 return xpvlv;
732}
733
734STATIC void
735S_del_xpvlv(pTHX_ XPVLV *p)
736{
737 LOCK_SV_MUTEX;
738 p->xpv_pv = (char*)PL_xpvlv_root;
739 PL_xpvlv_root = p;
740 UNLOCK_SV_MUTEX;
741}
742
932e9ff9
VB
743STATIC void
744S_more_xpvlv(pTHX)
745{
746 register XPVLV* xpvlv;
747 register XPVLV* xpvlvend;
612f20c3
GS
748 New(720, xpvlv, 1008/sizeof(XPVLV), XPVLV);
749 xpvlv->xpv_pv = (char*)PL_xpvlv_arenaroot;
750 PL_xpvlv_arenaroot = xpvlv;
751
932e9ff9 752 xpvlvend = &xpvlv[1008 / sizeof(XPVLV) - 1];
612f20c3 753 PL_xpvlv_root = ++xpvlv;
932e9ff9
VB
754 while (xpvlv < xpvlvend) {
755 xpvlv->xpv_pv = (char*)(xpvlv + 1);
756 xpvlv++;
757 }
758 xpvlv->xpv_pv = 0;
759}
760
932e9ff9
VB
761STATIC XPVBM*
762S_new_xpvbm(pTHX)
763{
764 XPVBM* xpvbm;
765 LOCK_SV_MUTEX;
766 if (!PL_xpvbm_root)
767 more_xpvbm();
768 xpvbm = PL_xpvbm_root;
769 PL_xpvbm_root = (XPVBM*)xpvbm->xpv_pv;
770 UNLOCK_SV_MUTEX;
771 return xpvbm;
772}
773
774STATIC void
775S_del_xpvbm(pTHX_ XPVBM *p)
776{
777 LOCK_SV_MUTEX;
778 p->xpv_pv = (char*)PL_xpvbm_root;
779 PL_xpvbm_root = p;
780 UNLOCK_SV_MUTEX;
781}
782
932e9ff9
VB
783STATIC void
784S_more_xpvbm(pTHX)
785{
786 register XPVBM* xpvbm;
787 register XPVBM* xpvbmend;
612f20c3
GS
788 New(721, xpvbm, 1008/sizeof(XPVBM), XPVBM);
789 xpvbm->xpv_pv = (char*)PL_xpvbm_arenaroot;
790 PL_xpvbm_arenaroot = xpvbm;
791
932e9ff9 792 xpvbmend = &xpvbm[1008 / sizeof(XPVBM) - 1];
612f20c3 793 PL_xpvbm_root = ++xpvbm;
932e9ff9
VB
794 while (xpvbm < xpvbmend) {
795 xpvbm->xpv_pv = (char*)(xpvbm + 1);
796 xpvbm++;
797 }
798 xpvbm->xpv_pv = 0;
799}
800
d33b2eba
GS
801#ifdef LEAKTEST
802# define my_safemalloc(s) (void*)safexmalloc(717,s)
803# define my_safefree(p) safexfree((char*)p)
804#else
805# define my_safemalloc(s) (void*)safemalloc(s)
806# define my_safefree(p) safefree((char*)p)
807#endif
463ee0b2 808
d33b2eba 809#ifdef PURIFY
463ee0b2 810
d33b2eba
GS
811#define new_XIV() my_safemalloc(sizeof(XPVIV))
812#define del_XIV(p) my_safefree(p)
ed6116ce 813
d33b2eba
GS
814#define new_XNV() my_safemalloc(sizeof(XPVNV))
815#define del_XNV(p) my_safefree(p)
463ee0b2 816
d33b2eba
GS
817#define new_XRV() my_safemalloc(sizeof(XRV))
818#define del_XRV(p) my_safefree(p)
8c52afec 819
d33b2eba
GS
820#define new_XPV() my_safemalloc(sizeof(XPV))
821#define del_XPV(p) my_safefree(p)
9b94d1dd 822
d33b2eba
GS
823#define new_XPVIV() my_safemalloc(sizeof(XPVIV))
824#define del_XPVIV(p) my_safefree(p)
932e9ff9 825
d33b2eba
GS
826#define new_XPVNV() my_safemalloc(sizeof(XPVNV))
827#define del_XPVNV(p) my_safefree(p)
932e9ff9 828
d33b2eba
GS
829#define new_XPVCV() my_safemalloc(sizeof(XPVCV))
830#define del_XPVCV(p) my_safefree(p)
932e9ff9 831
d33b2eba
GS
832#define new_XPVAV() my_safemalloc(sizeof(XPVAV))
833#define del_XPVAV(p) my_safefree(p)
834
835#define new_XPVHV() my_safemalloc(sizeof(XPVHV))
836#define del_XPVHV(p) my_safefree(p)
1c846c1f 837
d33b2eba
GS
838#define new_XPVMG() my_safemalloc(sizeof(XPVMG))
839#define del_XPVMG(p) my_safefree(p)
840
841#define new_XPVLV() my_safemalloc(sizeof(XPVLV))
842#define del_XPVLV(p) my_safefree(p)
843
844#define new_XPVBM() my_safemalloc(sizeof(XPVBM))
845#define del_XPVBM(p) my_safefree(p)
846
847#else /* !PURIFY */
848
849#define new_XIV() (void*)new_xiv()
850#define del_XIV(p) del_xiv((XPVIV*) p)
851
852#define new_XNV() (void*)new_xnv()
853#define del_XNV(p) del_xnv((XPVNV*) p)
9b94d1dd 854
d33b2eba
GS
855#define new_XRV() (void*)new_xrv()
856#define del_XRV(p) del_xrv((XRV*) p)
9b94d1dd 857
d33b2eba
GS
858#define new_XPV() (void*)new_xpv()
859#define del_XPV(p) del_xpv((XPV *)p)
860
861#define new_XPVIV() (void*)new_xpviv()
862#define del_XPVIV(p) del_xpviv((XPVIV *)p)
863
864#define new_XPVNV() (void*)new_xpvnv()
865#define del_XPVNV(p) del_xpvnv((XPVNV *)p)
866
867#define new_XPVCV() (void*)new_xpvcv()
868#define del_XPVCV(p) del_xpvcv((XPVCV *)p)
869
870#define new_XPVAV() (void*)new_xpvav()
871#define del_XPVAV(p) del_xpvav((XPVAV *)p)
872
873#define new_XPVHV() (void*)new_xpvhv()
874#define del_XPVHV(p) del_xpvhv((XPVHV *)p)
1c846c1f 875
d33b2eba
GS
876#define new_XPVMG() (void*)new_xpvmg()
877#define del_XPVMG(p) del_xpvmg((XPVMG *)p)
878
879#define new_XPVLV() (void*)new_xpvlv()
880#define del_XPVLV(p) del_xpvlv((XPVLV *)p)
881
882#define new_XPVBM() (void*)new_xpvbm()
883#define del_XPVBM(p) del_xpvbm((XPVBM *)p)
884
885#endif /* PURIFY */
9b94d1dd 886
d33b2eba
GS
887#define new_XPVGV() my_safemalloc(sizeof(XPVGV))
888#define del_XPVGV(p) my_safefree(p)
1c846c1f 889
d33b2eba
GS
890#define new_XPVFM() my_safemalloc(sizeof(XPVFM))
891#define del_XPVFM(p) my_safefree(p)
1c846c1f 892
d33b2eba
GS
893#define new_XPVIO() my_safemalloc(sizeof(XPVIO))
894#define del_XPVIO(p) my_safefree(p)
8990e307 895
954c1994
GS
896/*
897=for apidoc sv_upgrade
898
899Upgrade an SV to a more complex form. Use C<SvUPGRADE>. See
900C<svtype>.
901
902=cut
903*/
904
79072805 905bool
864dbfa3 906Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
79072805
LW
907{
908 char* pv;
909 U32 cur;
910 U32 len;
a0d0e21e 911 IV iv;
65202027 912 NV nv;
79072805
LW
913 MAGIC* magic;
914 HV* stash;
915
f130fd45
NIS
916 if (mt != SVt_PV && SvREADONLY(sv) && SvFAKE(sv)) {
917 sv_force_normal(sv);
918 }
919
79072805
LW
920 if (SvTYPE(sv) == mt)
921 return TRUE;
922
a5f75d66
AD
923 if (mt < SVt_PVIV)
924 (void)SvOOK_off(sv);
925
79072805
LW
926 switch (SvTYPE(sv)) {
927 case SVt_NULL:
928 pv = 0;
929 cur = 0;
930 len = 0;
931 iv = 0;
932 nv = 0.0;
933 magic = 0;
934 stash = 0;
935 break;
79072805
LW
936 case SVt_IV:
937 pv = 0;
938 cur = 0;
939 len = 0;
463ee0b2 940 iv = SvIVX(sv);
65202027 941 nv = (NV)SvIVX(sv);
79072805
LW
942 del_XIV(SvANY(sv));
943 magic = 0;
944 stash = 0;
ed6116ce 945 if (mt == SVt_NV)
463ee0b2 946 mt = SVt_PVNV;
ed6116ce
LW
947 else if (mt < SVt_PVIV)
948 mt = SVt_PVIV;
79072805
LW
949 break;
950 case SVt_NV:
951 pv = 0;
952 cur = 0;
953 len = 0;
463ee0b2 954 nv = SvNVX(sv);
1bd302c3 955 iv = I_V(nv);
79072805
LW
956 magic = 0;
957 stash = 0;
958 del_XNV(SvANY(sv));
959 SvANY(sv) = 0;
ed6116ce 960 if (mt < SVt_PVNV)
79072805
LW
961 mt = SVt_PVNV;
962 break;
ed6116ce
LW
963 case SVt_RV:
964 pv = (char*)SvRV(sv);
965 cur = 0;
966 len = 0;
56431972
RB
967 iv = PTR2IV(pv);
968 nv = PTR2NV(pv);
ed6116ce
LW
969 del_XRV(SvANY(sv));
970 magic = 0;
971 stash = 0;
972 break;
79072805 973 case SVt_PV:
463ee0b2 974 pv = SvPVX(sv);
79072805
LW
975 cur = SvCUR(sv);
976 len = SvLEN(sv);
977 iv = 0;
978 nv = 0.0;
979 magic = 0;
980 stash = 0;
981 del_XPV(SvANY(sv));
748a9306
LW
982 if (mt <= SVt_IV)
983 mt = SVt_PVIV;
984 else if (mt == SVt_NV)
985 mt = SVt_PVNV;
79072805
LW
986 break;
987 case SVt_PVIV:
463ee0b2 988 pv = SvPVX(sv);
79072805
LW
989 cur = SvCUR(sv);
990 len = SvLEN(sv);
463ee0b2 991 iv = SvIVX(sv);
79072805
LW
992 nv = 0.0;
993 magic = 0;
994 stash = 0;
995 del_XPVIV(SvANY(sv));
996 break;
997 case SVt_PVNV:
463ee0b2 998 pv = SvPVX(sv);
79072805
LW
999 cur = SvCUR(sv);
1000 len = SvLEN(sv);
463ee0b2
LW
1001 iv = SvIVX(sv);
1002 nv = SvNVX(sv);
79072805
LW
1003 magic = 0;
1004 stash = 0;
1005 del_XPVNV(SvANY(sv));
1006 break;
1007 case SVt_PVMG:
463ee0b2 1008 pv = SvPVX(sv);
79072805
LW
1009 cur = SvCUR(sv);
1010 len = SvLEN(sv);
463ee0b2
LW
1011 iv = SvIVX(sv);
1012 nv = SvNVX(sv);
79072805
LW
1013 magic = SvMAGIC(sv);
1014 stash = SvSTASH(sv);
1015 del_XPVMG(SvANY(sv));
1016 break;
1017 default:
cea2e8a9 1018 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
79072805
LW
1019 }
1020
1021 switch (mt) {
1022 case SVt_NULL:
cea2e8a9 1023 Perl_croak(aTHX_ "Can't upgrade to undef");
79072805
LW
1024 case SVt_IV:
1025 SvANY(sv) = new_XIV();
463ee0b2 1026 SvIVX(sv) = iv;
79072805
LW
1027 break;
1028 case SVt_NV:
1029 SvANY(sv) = new_XNV();
463ee0b2 1030 SvNVX(sv) = nv;
79072805 1031 break;
ed6116ce
LW
1032 case SVt_RV:
1033 SvANY(sv) = new_XRV();
1034 SvRV(sv) = (SV*)pv;
ed6116ce 1035 break;
79072805
LW
1036 case SVt_PV:
1037 SvANY(sv) = new_XPV();
463ee0b2 1038 SvPVX(sv) = pv;
79072805
LW
1039 SvCUR(sv) = cur;
1040 SvLEN(sv) = len;
1041 break;
1042 case SVt_PVIV:
1043 SvANY(sv) = new_XPVIV();
463ee0b2 1044 SvPVX(sv) = pv;
79072805
LW
1045 SvCUR(sv) = cur;
1046 SvLEN(sv) = len;
463ee0b2 1047 SvIVX(sv) = iv;
79072805 1048 if (SvNIOK(sv))
a0d0e21e 1049 (void)SvIOK_on(sv);
79072805
LW
1050 SvNOK_off(sv);
1051 break;
1052 case SVt_PVNV:
1053 SvANY(sv) = new_XPVNV();
463ee0b2 1054 SvPVX(sv) = pv;
79072805
LW
1055 SvCUR(sv) = cur;
1056 SvLEN(sv) = len;
463ee0b2
LW
1057 SvIVX(sv) = iv;
1058 SvNVX(sv) = nv;
79072805
LW
1059 break;
1060 case SVt_PVMG:
1061 SvANY(sv) = new_XPVMG();
463ee0b2 1062 SvPVX(sv) = pv;
79072805
LW
1063 SvCUR(sv) = cur;
1064 SvLEN(sv) = len;
463ee0b2
LW
1065 SvIVX(sv) = iv;
1066 SvNVX(sv) = nv;
79072805
LW
1067 SvMAGIC(sv) = magic;
1068 SvSTASH(sv) = stash;
1069 break;
1070 case SVt_PVLV:
1071 SvANY(sv) = new_XPVLV();
463ee0b2 1072 SvPVX(sv) = pv;
79072805
LW
1073 SvCUR(sv) = cur;
1074 SvLEN(sv) = len;
463ee0b2
LW
1075 SvIVX(sv) = iv;
1076 SvNVX(sv) = nv;
79072805
LW
1077 SvMAGIC(sv) = magic;
1078 SvSTASH(sv) = stash;
1079 LvTARGOFF(sv) = 0;
1080 LvTARGLEN(sv) = 0;
1081 LvTARG(sv) = 0;
1082 LvTYPE(sv) = 0;
1083 break;
1084 case SVt_PVAV:
1085 SvANY(sv) = new_XPVAV();
463ee0b2
LW
1086 if (pv)
1087 Safefree(pv);
2304df62 1088 SvPVX(sv) = 0;
d1bf51dd 1089 AvMAX(sv) = -1;
93965878 1090 AvFILLp(sv) = -1;
463ee0b2
LW
1091 SvIVX(sv) = 0;
1092 SvNVX(sv) = 0.0;
1093 SvMAGIC(sv) = magic;
1094 SvSTASH(sv) = stash;
1095 AvALLOC(sv) = 0;
79072805
LW
1096 AvARYLEN(sv) = 0;
1097 AvFLAGS(sv) = 0;
1098 break;
1099 case SVt_PVHV:
1100 SvANY(sv) = new_XPVHV();
463ee0b2
LW
1101 if (pv)
1102 Safefree(pv);
1103 SvPVX(sv) = 0;
1104 HvFILL(sv) = 0;
1105 HvMAX(sv) = 0;
1106 HvKEYS(sv) = 0;
1107 SvNVX(sv) = 0.0;
79072805
LW
1108 SvMAGIC(sv) = magic;
1109 SvSTASH(sv) = stash;
79072805
LW
1110 HvRITER(sv) = 0;
1111 HvEITER(sv) = 0;
1112 HvPMROOT(sv) = 0;
1113 HvNAME(sv) = 0;
79072805
LW
1114 break;
1115 case SVt_PVCV:
1116 SvANY(sv) = new_XPVCV();
748a9306 1117 Zero(SvANY(sv), 1, XPVCV);
463ee0b2 1118 SvPVX(sv) = pv;
79072805
LW
1119 SvCUR(sv) = cur;
1120 SvLEN(sv) = len;
463ee0b2
LW
1121 SvIVX(sv) = iv;
1122 SvNVX(sv) = nv;
79072805
LW
1123 SvMAGIC(sv) = magic;
1124 SvSTASH(sv) = stash;
79072805
LW
1125 break;
1126 case SVt_PVGV:
1127 SvANY(sv) = new_XPVGV();
463ee0b2 1128 SvPVX(sv) = pv;
79072805
LW
1129 SvCUR(sv) = cur;
1130 SvLEN(sv) = len;
463ee0b2
LW
1131 SvIVX(sv) = iv;
1132 SvNVX(sv) = nv;
79072805
LW
1133 SvMAGIC(sv) = magic;
1134 SvSTASH(sv) = stash;
93a17b20 1135 GvGP(sv) = 0;
79072805
LW
1136 GvNAME(sv) = 0;
1137 GvNAMELEN(sv) = 0;
1138 GvSTASH(sv) = 0;
a5f75d66 1139 GvFLAGS(sv) = 0;
79072805
LW
1140 break;
1141 case SVt_PVBM:
1142 SvANY(sv) = new_XPVBM();
463ee0b2 1143 SvPVX(sv) = pv;
79072805
LW
1144 SvCUR(sv) = cur;
1145 SvLEN(sv) = len;
463ee0b2
LW
1146 SvIVX(sv) = iv;
1147 SvNVX(sv) = nv;
79072805
LW
1148 SvMAGIC(sv) = magic;
1149 SvSTASH(sv) = stash;
1150 BmRARE(sv) = 0;
1151 BmUSEFUL(sv) = 0;
1152 BmPREVIOUS(sv) = 0;
1153 break;
1154 case SVt_PVFM:
1155 SvANY(sv) = new_XPVFM();
748a9306 1156 Zero(SvANY(sv), 1, XPVFM);
463ee0b2 1157 SvPVX(sv) = pv;
79072805
LW
1158 SvCUR(sv) = cur;
1159 SvLEN(sv) = len;
463ee0b2
LW
1160 SvIVX(sv) = iv;
1161 SvNVX(sv) = nv;
79072805
LW
1162 SvMAGIC(sv) = magic;
1163 SvSTASH(sv) = stash;
79072805 1164 break;
8990e307
LW
1165 case SVt_PVIO:
1166 SvANY(sv) = new_XPVIO();
748a9306 1167 Zero(SvANY(sv), 1, XPVIO);
8990e307
LW
1168 SvPVX(sv) = pv;
1169 SvCUR(sv) = cur;
1170 SvLEN(sv) = len;
1171 SvIVX(sv) = iv;
1172 SvNVX(sv) = nv;
1173 SvMAGIC(sv) = magic;
1174 SvSTASH(sv) = stash;
85e6fe83 1175 IoPAGE_LEN(sv) = 60;
8990e307
LW
1176 break;
1177 }
1178 SvFLAGS(sv) &= ~SVTYPEMASK;
1179 SvFLAGS(sv) |= mt;
79072805
LW
1180 return TRUE;
1181}
1182
79072805 1183int
864dbfa3 1184Perl_sv_backoff(pTHX_ register SV *sv)
79072805
LW
1185{
1186 assert(SvOOK(sv));
463ee0b2
LW
1187 if (SvIVX(sv)) {
1188 char *s = SvPVX(sv);
1189 SvLEN(sv) += SvIVX(sv);
1190 SvPVX(sv) -= SvIVX(sv);
79072805 1191 SvIV_set(sv, 0);
463ee0b2 1192 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
79072805
LW
1193 }
1194 SvFLAGS(sv) &= ~SVf_OOK;
a0d0e21e 1195 return 0;
79072805
LW
1196}
1197
954c1994
GS
1198/*
1199=for apidoc sv_grow
1200
1201Expands the character buffer in the SV. This will use C<sv_unref> and will
1202upgrade the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1203Use C<SvGROW>.
1204
1205=cut
1206*/
1207
79072805 1208char *
864dbfa3 1209Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
79072805
LW
1210{
1211 register char *s;
1212
55497cff 1213#ifdef HAS_64K_LIMIT
79072805 1214 if (newlen >= 0x10000) {
1d7c1841
GS
1215 PerlIO_printf(Perl_debug_log,
1216 "Allocation too large: %"UVxf"\n", (UV)newlen);
79072805
LW
1217 my_exit(1);
1218 }
55497cff 1219#endif /* HAS_64K_LIMIT */
a0d0e21e
LW
1220 if (SvROK(sv))
1221 sv_unref(sv);
79072805
LW
1222 if (SvTYPE(sv) < SVt_PV) {
1223 sv_upgrade(sv, SVt_PV);
463ee0b2 1224 s = SvPVX(sv);
79072805
LW
1225 }
1226 else if (SvOOK(sv)) { /* pv is offset? */
1227 sv_backoff(sv);
463ee0b2 1228 s = SvPVX(sv);
79072805
LW
1229 if (newlen > SvLEN(sv))
1230 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
c6f8c383
GA
1231#ifdef HAS_64K_LIMIT
1232 if (newlen >= 0x10000)
1233 newlen = 0xFFFF;
1234#endif
79072805
LW
1235 }
1236 else
463ee0b2 1237 s = SvPVX(sv);
79072805 1238 if (newlen > SvLEN(sv)) { /* need more room? */
8d6dde3e 1239 if (SvLEN(sv) && s) {
f5a32c7f 1240#if defined(MYMALLOC) && !defined(LEAKTEST)
8d6dde3e
IZ
1241 STRLEN l = malloced_size((void*)SvPVX(sv));
1242 if (newlen <= l) {
1243 SvLEN_set(sv, l);
1244 return s;
1245 } else
c70c8a0a 1246#endif
79072805 1247 Renew(s,newlen,char);
8d6dde3e 1248 }
79072805
LW
1249 else
1250 New(703,s,newlen,char);
1251 SvPV_set(sv, s);
1252 SvLEN_set(sv, newlen);
1253 }
1254 return s;
1255}
1256
954c1994
GS
1257/*
1258=for apidoc sv_setiv
1259
1260Copies an integer into the given SV. Does not handle 'set' magic. See
1261C<sv_setiv_mg>.
1262
1263=cut
1264*/
1265
79072805 1266void
864dbfa3 1267Perl_sv_setiv(pTHX_ register SV *sv, IV i)
79072805 1268{
2213622d 1269 SV_CHECK_THINKFIRST(sv);
463ee0b2
LW
1270 switch (SvTYPE(sv)) {
1271 case SVt_NULL:
79072805 1272 sv_upgrade(sv, SVt_IV);
463ee0b2
LW
1273 break;
1274 case SVt_NV:
1275 sv_upgrade(sv, SVt_PVNV);
1276 break;
ed6116ce 1277 case SVt_RV:
463ee0b2 1278 case SVt_PV:
79072805 1279 sv_upgrade(sv, SVt_PVIV);
463ee0b2 1280 break;
a0d0e21e
LW
1281
1282 case SVt_PVGV:
a0d0e21e
LW
1283 case SVt_PVAV:
1284 case SVt_PVHV:
1285 case SVt_PVCV:
1286 case SVt_PVFM:
1287 case SVt_PVIO:
11343788
MB
1288 {
1289 dTHR;
cea2e8a9 1290 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
22c35a8c 1291 PL_op_desc[PL_op->op_type]);
11343788 1292 }
463ee0b2 1293 }
a0d0e21e 1294 (void)SvIOK_only(sv); /* validate number */
a5f75d66 1295 SvIVX(sv) = i;
463ee0b2 1296 SvTAINT(sv);
79072805
LW
1297}
1298
954c1994
GS
1299/*
1300=for apidoc sv_setiv_mg
1301
1302Like C<sv_setiv>, but also handles 'set' magic.
1303
1304=cut
1305*/
1306
79072805 1307void
864dbfa3 1308Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
ef50df4b
GS
1309{
1310 sv_setiv(sv,i);
1311 SvSETMAGIC(sv);
1312}
1313
954c1994
GS
1314/*
1315=for apidoc sv_setuv
1316
1317Copies an unsigned integer into the given SV. Does not handle 'set' magic.
1318See C<sv_setuv_mg>.
1319
1320=cut
1321*/
1322
ef50df4b 1323void
864dbfa3 1324Perl_sv_setuv(pTHX_ register SV *sv, UV u)
55497cff 1325{
25da4f38
IZ
1326 sv_setiv(sv, 0);
1327 SvIsUV_on(sv);
1328 SvUVX(sv) = u;
55497cff
PP
1329}
1330
954c1994
GS
1331/*
1332=for apidoc sv_setuv_mg
1333
1334Like C<sv_setuv>, but also handles 'set' magic.
1335
1336=cut
1337*/
1338
55497cff 1339void
864dbfa3 1340Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
ef50df4b
GS
1341{
1342 sv_setuv(sv,u);
1343 SvSETMAGIC(sv);
1344}
1345
954c1994
GS
1346/*
1347=for apidoc sv_setnv
1348
1349Copies a double into the given SV. Does not handle 'set' magic. See
1350C<sv_setnv_mg>.
1351
1352=cut
1353*/
1354
ef50df4b 1355void
65202027 1356Perl_sv_setnv(pTHX_ register SV *sv, NV num)
79072805 1357{
2213622d 1358 SV_CHECK_THINKFIRST(sv);
a0d0e21e
LW
1359 switch (SvTYPE(sv)) {
1360 case SVt_NULL:
1361 case SVt_IV:
79072805 1362 sv_upgrade(sv, SVt_NV);
a0d0e21e 1363 break;
a0d0e21e
LW
1364 case SVt_RV:
1365 case SVt_PV:
1366 case SVt_PVIV:
79072805 1367 sv_upgrade(sv, SVt_PVNV);
a0d0e21e 1368 break;
827b7e14 1369
a0d0e21e 1370 case SVt_PVGV:
a0d0e21e
LW
1371 case SVt_PVAV:
1372 case SVt_PVHV:
1373 case SVt_PVCV:
1374 case SVt_PVFM:
1375 case SVt_PVIO:
11343788
MB
1376 {
1377 dTHR;
cea2e8a9 1378 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
22c35a8c 1379 PL_op_name[PL_op->op_type]);
11343788 1380 }
79072805 1381 }
463ee0b2 1382 SvNVX(sv) = num;
a0d0e21e 1383 (void)SvNOK_only(sv); /* validate number */
463ee0b2 1384 SvTAINT(sv);
79072805
LW
1385}
1386
954c1994
GS
1387/*
1388=for apidoc sv_setnv_mg
1389
1390Like C<sv_setnv>, but also handles 'set' magic.
1391
1392=cut
1393*/
1394
ef50df4b 1395void
65202027 1396Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
ef50df4b
GS
1397{
1398 sv_setnv(sv,num);
1399 SvSETMAGIC(sv);
1400}
1401
76e3520e 1402STATIC void
cea2e8a9 1403S_not_a_number(pTHX_ SV *sv)
a0d0e21e 1404{
11343788 1405 dTHR;
a0d0e21e
LW
1406 char tmpbuf[64];
1407 char *d = tmpbuf;
1408 char *s;
dc28f22b
GA
1409 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
1410 /* each *s can expand to 4 chars + "...\0",
1411 i.e. need room for 8 chars */
a0d0e21e 1412
dc28f22b 1413 for (s = SvPVX(sv); *s && d < limit; s++) {
bbce6d69
PP
1414 int ch = *s & 0xFF;
1415 if (ch & 128 && !isPRINT_LC(ch)) {
a0d0e21e
LW
1416 *d++ = 'M';
1417 *d++ = '-';
1418 ch &= 127;
1419 }
bbce6d69
PP
1420 if (ch == '\n') {
1421 *d++ = '\\';
1422 *d++ = 'n';
1423 }
1424 else if (ch == '\r') {
1425 *d++ = '\\';
1426 *d++ = 'r';
1427 }
1428 else if (ch == '\f') {
1429 *d++ = '\\';
1430 *d++ = 'f';
1431 }
1432 else if (ch == '\\') {
1433 *d++ = '\\';
1434 *d++ = '\\';
1435 }
1436 else if (isPRINT_LC(ch))
a0d0e21e
LW
1437 *d++ = ch;
1438 else {
1439 *d++ = '^';
bbce6d69 1440 *d++ = toCTRL(ch);
a0d0e21e
LW
1441 }
1442 }
1443 if (*s) {
1444 *d++ = '.';
1445 *d++ = '.';
1446 *d++ = '.';
1447 }
1448 *d = '\0';
1449
533c011a 1450 if (PL_op)
42d38218
MS
1451 Perl_warner(aTHX_ WARN_NUMERIC,
1452 "Argument \"%s\" isn't numeric in %s", tmpbuf,
1453 PL_op_desc[PL_op->op_type]);
a0d0e21e 1454 else
42d38218
MS
1455 Perl_warner(aTHX_ WARN_NUMERIC,
1456 "Argument \"%s\" isn't numeric", tmpbuf);
a0d0e21e
LW
1457}
1458
cf2093f6 1459/* the number can be converted to integer with atol() or atoll() */
25da4f38
IZ
1460#define IS_NUMBER_TO_INT_BY_ATOL 0x01
1461#define IS_NUMBER_TO_INT_BY_ATOF 0x02 /* atol() may be != atof() */
1462#define IS_NUMBER_NOT_IV 0x04 /* (IV)atof() may be != atof() */
1463#define IS_NUMBER_NEG 0x08 /* not good to cache UV */
300aed98 1464#define IS_NUMBER_INFINITY 0x10 /* this is big */
25da4f38
IZ
1465
1466/* Actually, ISO C leaves conversion of UV to IV undefined, but
1467 until proven guilty, assume that things are not that bad... */
1468
a0d0e21e 1469IV
864dbfa3 1470Perl_sv_2iv(pTHX_ register SV *sv)
79072805
LW
1471{
1472 if (!sv)
1473 return 0;
8990e307 1474 if (SvGMAGICAL(sv)) {
463ee0b2
LW
1475 mg_get(sv);
1476 if (SvIOKp(sv))
1477 return SvIVX(sv);
748a9306 1478 if (SvNOKp(sv)) {
25da4f38 1479 return I_V(SvNVX(sv));
748a9306 1480 }
36477c24
PP
1481 if (SvPOKp(sv) && SvLEN(sv))
1482 return asIV(sv);
3fe9a6f1 1483 if (!SvROK(sv)) {
d008e5eb 1484 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
c6ee37c5 1485 dTHR;
d008e5eb 1486 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 1487 report_uninit();
c6ee37c5 1488 }
36477c24 1489 return 0;
3fe9a6f1 1490 }
463ee0b2 1491 }
ed6116ce 1492 if (SvTHINKFIRST(sv)) {
a0d0e21e 1493 if (SvROK(sv)) {
a0d0e21e 1494 SV* tmpstr;
1554e226
DC
1495 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
1496 (SvRV(tmpstr) != SvRV(sv)))
9e7bc3e8 1497 return SvIV(tmpstr);
56431972 1498 return PTR2IV(SvRV(sv));
a0d0e21e 1499 }
47deb5e7
NIS
1500 if (SvREADONLY(sv) && SvFAKE(sv)) {
1501 sv_force_normal(sv);
1502 }
0336b60e
IZ
1503 if (SvREADONLY(sv) && !SvOK(sv)) {
1504 dTHR;
1505 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 1506 report_uninit();
ed6116ce
LW
1507 return 0;
1508 }
79072805 1509 }
25da4f38
IZ
1510 if (SvIOKp(sv)) {
1511 if (SvIsUV(sv)) {
1512 return (IV)(SvUVX(sv));
1513 }
1514 else {
1515 return SvIVX(sv);
1516 }
463ee0b2 1517 }
748a9306 1518 if (SvNOKp(sv)) {
25da4f38
IZ
1519 /* We can cache the IV/UV value even if it not good enough
1520 * to reconstruct NV, since the conversion to PV will prefer
cf2093f6 1521 * NV over IV/UV.
25da4f38
IZ
1522 */
1523
1524 if (SvTYPE(sv) == SVt_NV)
1525 sv_upgrade(sv, SVt_PVNV);
1526
a5f75d66 1527 (void)SvIOK_on(sv);
65202027 1528 if (SvNVX(sv) < (NV)IV_MAX + 0.5)
748a9306 1529 SvIVX(sv) = I_V(SvNVX(sv));
25da4f38 1530 else {
ff68c719 1531 SvUVX(sv) = U_V(SvNVX(sv));
25da4f38
IZ
1532 SvIsUV_on(sv);
1533 ret_iv_max:
1c846c1f 1534 DEBUG_c(PerlIO_printf(Perl_debug_log,
57def98f 1535 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
56431972 1536 PTR2UV(sv),
57def98f
JH
1537 SvUVX(sv),
1538 SvUVX(sv)));
25da4f38
IZ
1539 return (IV)SvUVX(sv);
1540 }
748a9306
LW
1541 }
1542 else if (SvPOKp(sv) && SvLEN(sv)) {
25da4f38
IZ
1543 I32 numtype = looks_like_number(sv);
1544
1545 /* We want to avoid a possible problem when we cache an IV which
1546 may be later translated to an NV, and the resulting NV is not
1547 the translation of the initial data.
1c846c1f 1548
25da4f38
IZ
1549 This means that if we cache such an IV, we need to cache the
1550 NV as well. Moreover, we trade speed for space, and do not
1551 cache the NV if not needed.
1552 */
1553 if (numtype & IS_NUMBER_NOT_IV) {
1554 /* May be not an integer. Need to cache NV if we cache IV
1555 * - otherwise future conversion to NV will be wrong. */
65202027 1556 NV d;
25da4f38 1557
097ee67d 1558 d = Atof(SvPVX(sv));
25da4f38
IZ
1559
1560 if (SvTYPE(sv) < SVt_PVNV)
1561 sv_upgrade(sv, SVt_PVNV);
1562 SvNVX(sv) = d;
1563 (void)SvNOK_on(sv);
1564 (void)SvIOK_on(sv);
65202027 1565#if defined(USE_LONG_DOUBLE)
1d7c1841
GS
1566 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
1567 PTR2UV(sv), SvNVX(sv)));
65202027 1568#else
1d7c1841
GS
1569 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%g)\n",
1570 PTR2UV(sv), SvNVX(sv)));
65202027 1571#endif
65202027 1572 if (SvNVX(sv) < (NV)IV_MAX + 0.5)
25da4f38
IZ
1573 SvIVX(sv) = I_V(SvNVX(sv));
1574 else {
1575 SvUVX(sv) = U_V(SvNVX(sv));
1576 SvIsUV_on(sv);
1577 goto ret_iv_max;
1578 }
1579 }
0a3ece55
MG
1580 else { /* The NV may be reconstructed from IV - safe to cache IV,
1581 which may be calculated by atol(). */
25da4f38
IZ
1582 if (SvTYPE(sv) < SVt_PVIV)
1583 sv_upgrade(sv, SVt_PVIV);
25da4f38 1584 (void)SvIOK_on(sv);
0a3ece55
MG
1585 SvIVX(sv) = Atol(SvPVX(sv));
1586 if (! numtype && ckWARN(WARN_NUMERIC))
25da4f38
IZ
1587 not_a_number(sv);
1588 }
93a17b20 1589 }
79072805 1590 else {
11343788 1591 dTHR;
599cee73 1592 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1d7c1841 1593 report_uninit();
25da4f38
IZ
1594 if (SvTYPE(sv) < SVt_IV)
1595 /* Typically the caller expects that sv_any is not NULL now. */
1596 sv_upgrade(sv, SVt_IV);
a0d0e21e 1597 return 0;
79072805 1598 }
1d7c1841
GS
1599 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
1600 PTR2UV(sv),SvIVX(sv)));
25da4f38 1601 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
79072805
LW
1602}
1603
ff68c719 1604UV
864dbfa3 1605Perl_sv_2uv(pTHX_ register SV *sv)
ff68c719
PP
1606{
1607 if (!sv)
1608 return 0;
1609 if (SvGMAGICAL(sv)) {
1610 mg_get(sv);
1611 if (SvIOKp(sv))
1612 return SvUVX(sv);
1613 if (SvNOKp(sv))
1614 return U_V(SvNVX(sv));
36477c24
PP
1615 if (SvPOKp(sv) && SvLEN(sv))
1616 return asUV(sv);
3fe9a6f1 1617 if (!SvROK(sv)) {
d008e5eb 1618 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
c6ee37c5 1619 dTHR;
d008e5eb 1620 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 1621 report_uninit();
c6ee37c5 1622 }
36477c24 1623 return 0;
3fe9a6f1 1624 }
ff68c719
PP
1625 }
1626 if (SvTHINKFIRST(sv)) {
1627 if (SvROK(sv)) {
ff68c719 1628 SV* tmpstr;
1554e226
DC
1629 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
1630 (SvRV(tmpstr) != SvRV(sv)))
9e7bc3e8 1631 return SvUV(tmpstr);
56431972 1632 return PTR2UV(SvRV(sv));
ff68c719 1633 }
0336b60e
IZ
1634 if (SvREADONLY(sv) && !SvOK(sv)) {
1635 dTHR;
1636 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 1637 report_uninit();
ff68c719
PP
1638 return 0;
1639 }
1640 }
25da4f38
IZ
1641 if (SvIOKp(sv)) {
1642 if (SvIsUV(sv)) {
1643 return SvUVX(sv);
1644 }
1645 else {
1646 return (UV)SvIVX(sv);
1647 }
ff68c719
PP
1648 }
1649 if (SvNOKp(sv)) {
25da4f38
IZ
1650 /* We can cache the IV/UV value even if it not good enough
1651 * to reconstruct NV, since the conversion to PV will prefer
cf2093f6 1652 * NV over IV/UV.
25da4f38
IZ
1653 */
1654 if (SvTYPE(sv) == SVt_NV)
1655 sv_upgrade(sv, SVt_PVNV);
ff68c719 1656 (void)SvIOK_on(sv);
25da4f38
IZ
1657 if (SvNVX(sv) >= -0.5) {
1658 SvIsUV_on(sv);
1659 SvUVX(sv) = U_V(SvNVX(sv));
1660 }
1661 else {
1662 SvIVX(sv) = I_V(SvNVX(sv));
1663 ret_zero:
1c846c1f 1664 DEBUG_c(PerlIO_printf(Perl_debug_log,
07270b1a 1665 "0x%"UVxf" 2uv(%"IVdf" => %"IVdf") (as signed)\n",
57def98f
JH
1666 PTR2UV(sv),
1667 SvIVX(sv),
1668 (IV)(UV)SvIVX(sv)));
25da4f38
IZ
1669 return (UV)SvIVX(sv);
1670 }
ff68c719
PP
1671 }
1672 else if (SvPOKp(sv) && SvLEN(sv)) {
25da4f38
IZ
1673 I32 numtype = looks_like_number(sv);
1674
1675 /* We want to avoid a possible problem when we cache a UV which
1676 may be later translated to an NV, and the resulting NV is not
1677 the translation of the initial data.
1c846c1f 1678
25da4f38
IZ
1679 This means that if we cache such a UV, we need to cache the
1680 NV as well. Moreover, we trade speed for space, and do not
1681 cache the NV if not needed.
1682 */
1683 if (numtype & IS_NUMBER_NOT_IV) {
1684 /* May be not an integer. Need to cache NV if we cache IV
1685 * - otherwise future conversion to NV will be wrong. */
65202027 1686 NV d;
25da4f38 1687
cf2093f6 1688 d = Atof(SvPVX(sv));
25da4f38
IZ
1689
1690 if (SvTYPE(sv) < SVt_PVNV)
1691 sv_upgrade(sv, SVt_PVNV);
1692 SvNVX(sv) = d;
1693 (void)SvNOK_on(sv);
1694 (void)SvIOK_on(sv);
65202027 1695#if defined(USE_LONG_DOUBLE)
1d7c1841
GS
1696 DEBUG_c(PerlIO_printf(Perl_debug_log,
1697 "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
1698 PTR2UV(sv), SvNVX(sv)));
65202027 1699#else
1d7c1841
GS
1700 DEBUG_c(PerlIO_printf(Perl_debug_log,
1701 "0x%"UVxf" 2nv(%g)\n",
1702 PTR2UV(sv), SvNVX(sv)));
65202027 1703#endif
25da4f38
IZ
1704 if (SvNVX(sv) < -0.5) {
1705 SvIVX(sv) = I_V(SvNVX(sv));
1706 goto ret_zero;
1707 } else {
1708 SvUVX(sv) = U_V(SvNVX(sv));
1709 SvIsUV_on(sv);
1710 }
1711 }
1712 else if (numtype & IS_NUMBER_NEG) {
1713 /* The NV may be reconstructed from IV - safe to cache IV,
1714 which may be calculated by atol(). */
1715 if (SvTYPE(sv) == SVt_PV)
1716 sv_upgrade(sv, SVt_PVIV);
1717 (void)SvIOK_on(sv);
cf2093f6 1718 SvIVX(sv) = (IV)Atol(SvPVX(sv));
25da4f38
IZ
1719 }
1720 else if (numtype) { /* Non-negative */
1721 /* The NV may be reconstructed from UV - safe to cache UV,
1722 which may be calculated by strtoul()/atol. */
1723 if (SvTYPE(sv) == SVt_PV)
1724 sv_upgrade(sv, SVt_PVIV);
1725 (void)SvIOK_on(sv);
1726 (void)SvIsUV_on(sv);
1727#ifdef HAS_STRTOUL
cf2093f6 1728 SvUVX(sv) = Strtoul(SvPVX(sv), Null(char**), 10);
25da4f38
IZ
1729#else /* no atou(), but we know the number fits into IV... */
1730 /* The only problem may be if it is negative... */
cf2093f6 1731 SvUVX(sv) = (UV)Atol(SvPVX(sv));
25da4f38
IZ
1732#endif
1733 }
1734 else { /* Not a number. Cache 0. */
1735 dTHR;
1736
1737 if (SvTYPE(sv) < SVt_PVIV)
1738 sv_upgrade(sv, SVt_PVIV);
25da4f38
IZ
1739 (void)SvIOK_on(sv);
1740 (void)SvIsUV_on(sv);
7b9e3c00
GS
1741 SvUVX(sv) = 0; /* We assume that 0s have the
1742 same bitmap in IV and UV. */
25da4f38
IZ
1743 if (ckWARN(WARN_NUMERIC))
1744 not_a_number(sv);
1745 }
ff68c719
PP
1746 }
1747 else {
d008e5eb 1748 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
c6ee37c5 1749 dTHR;
d008e5eb 1750 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 1751 report_uninit();
c6ee37c5 1752 }
25da4f38
IZ
1753 if (SvTYPE(sv) < SVt_IV)
1754 /* Typically the caller expects that sv_any is not NULL now. */
1755 sv_upgrade(sv, SVt_IV);
ff68c719
PP
1756 return 0;
1757 }
25da4f38 1758
1d7c1841
GS
1759 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
1760 PTR2UV(sv),SvUVX(sv)));
25da4f38 1761 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
ff68c719
PP
1762}
1763
65202027 1764NV
864dbfa3 1765Perl_sv_2nv(pTHX_ register SV *sv)
79072805
LW
1766{
1767 if (!sv)
1768 return 0.0;
8990e307 1769 if (SvGMAGICAL(sv)) {
463ee0b2
LW
1770 mg_get(sv);
1771 if (SvNOKp(sv))
1772 return SvNVX(sv);
a0d0e21e 1773 if (SvPOKp(sv) && SvLEN(sv)) {
d008e5eb 1774 dTHR;
599cee73 1775 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv))
a0d0e21e 1776 not_a_number(sv);
097ee67d 1777 return Atof(SvPVX(sv));
a0d0e21e 1778 }
25da4f38 1779 if (SvIOKp(sv)) {
1c846c1f 1780 if (SvIsUV(sv))
65202027 1781 return (NV)SvUVX(sv);
25da4f38 1782 else
65202027 1783 return (NV)SvIVX(sv);
25da4f38 1784 }
16d20bd9 1785 if (!SvROK(sv)) {
d008e5eb 1786 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
c6ee37c5 1787 dTHR;
d008e5eb 1788 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 1789 report_uninit();
c6ee37c5 1790 }
16d20bd9
AD
1791 return 0;
1792 }
463ee0b2 1793 }
ed6116ce 1794 if (SvTHINKFIRST(sv)) {
a0d0e21e 1795 if (SvROK(sv)) {
a0d0e21e 1796 SV* tmpstr;
1554e226
DC
1797 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
1798 (SvRV(tmpstr) != SvRV(sv)))
9e7bc3e8 1799 return SvNV(tmpstr);
56431972 1800 return PTR2NV(SvRV(sv));
a0d0e21e 1801 }
0336b60e 1802 if (SvREADONLY(sv) && !SvOK(sv)) {
d008e5eb 1803 dTHR;
599cee73 1804 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 1805 report_uninit();
ed6116ce
LW
1806 return 0.0;
1807 }
79072805
LW
1808 }
1809 if (SvTYPE(sv) < SVt_NV) {
463ee0b2
LW
1810 if (SvTYPE(sv) == SVt_IV)
1811 sv_upgrade(sv, SVt_PVNV);
1812 else
1813 sv_upgrade(sv, SVt_NV);
572bbb43 1814#if defined(USE_LONG_DOUBLE)
097ee67d 1815 DEBUG_c({
f93f4e46 1816 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
1817 PerlIO_printf(Perl_debug_log,
1818 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
1819 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
1820 RESTORE_NUMERIC_LOCAL();
1821 });
65202027 1822#else
572bbb43 1823 DEBUG_c({
f93f4e46 1824 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
1825 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%g)\n",
1826 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
1827 RESTORE_NUMERIC_LOCAL();
1828 });
572bbb43 1829#endif
79072805
LW
1830 }
1831 else if (SvTYPE(sv) < SVt_PVNV)
1832 sv_upgrade(sv, SVt_PVNV);
748a9306
LW
1833 if (SvIOKp(sv) &&
1834 (!SvPOKp(sv) || !strchr(SvPVX(sv),'.') || !looks_like_number(sv)))
93a17b20 1835 {
65202027 1836 SvNVX(sv) = SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv);
93a17b20 1837 }
748a9306 1838 else if (SvPOKp(sv) && SvLEN(sv)) {
d008e5eb 1839 dTHR;
599cee73 1840 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv))
a0d0e21e 1841 not_a_number(sv);
097ee67d 1842 SvNVX(sv) = Atof(SvPVX(sv));
93a17b20 1843 }
79072805 1844 else {
11343788 1845 dTHR;
599cee73 1846 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1d7c1841 1847 report_uninit();
25da4f38
IZ
1848 if (SvTYPE(sv) < SVt_NV)
1849 /* Typically the caller expects that sv_any is not NULL now. */
1850 sv_upgrade(sv, SVt_NV);
a0d0e21e 1851 return 0.0;
79072805
LW
1852 }
1853 SvNOK_on(sv);
572bbb43 1854#if defined(USE_LONG_DOUBLE)
097ee67d 1855 DEBUG_c({
f93f4e46 1856 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
1857 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
1858 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
1859 RESTORE_NUMERIC_LOCAL();
1860 });
65202027 1861#else
572bbb43 1862 DEBUG_c({
f93f4e46 1863 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
1864 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%g)\n",
1865 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
1866 RESTORE_NUMERIC_LOCAL();
1867 });
572bbb43 1868#endif
463ee0b2 1869 return SvNVX(sv);
79072805
LW
1870}
1871
76e3520e 1872STATIC IV
cea2e8a9 1873S_asIV(pTHX_ SV *sv)
36477c24
PP
1874{
1875 I32 numtype = looks_like_number(sv);
65202027 1876 NV d;
36477c24 1877
25da4f38 1878 if (numtype & IS_NUMBER_TO_INT_BY_ATOL)
cf2093f6 1879 return Atol(SvPVX(sv));
d008e5eb
GS
1880 if (!numtype) {
1881 dTHR;
1882 if (ckWARN(WARN_NUMERIC))
1883 not_a_number(sv);
1884 }
097ee67d 1885 d = Atof(SvPVX(sv));
25da4f38 1886 return I_V(d);
36477c24
PP
1887}
1888
76e3520e 1889STATIC UV
cea2e8a9 1890S_asUV(pTHX_ SV *sv)
36477c24
PP
1891{
1892 I32 numtype = looks_like_number(sv);
1893
84902520 1894#ifdef HAS_STRTOUL
25da4f38 1895 if (numtype & IS_NUMBER_TO_INT_BY_ATOL)
cf2093f6 1896 return Strtoul(SvPVX(sv), Null(char**), 10);
84902520 1897#endif
d008e5eb
GS
1898 if (!numtype) {
1899 dTHR;
1900 if (ckWARN(WARN_NUMERIC))
1901 not_a_number(sv);
1902 }
097ee67d 1903 return U_V(Atof(SvPVX(sv)));
36477c24
PP
1904}
1905
25da4f38
IZ
1906/*
1907 * Returns a combination of (advisory only - can get false negatives)
1908 * IS_NUMBER_TO_INT_BY_ATOL, IS_NUMBER_TO_INT_BY_ATOF, IS_NUMBER_NOT_IV,
1909 * IS_NUMBER_NEG
1910 * 0 if does not look like number.
1911 *
1912 * In fact possible values are 0 and
1913 * IS_NUMBER_TO_INT_BY_ATOL 123
1914 * IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_NOT_IV 123.1
1915 * IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_IV 123e0
300aed98 1916 * IS_NUMBER_INFINITY
25da4f38
IZ
1917 * with a possible addition of IS_NUMBER_NEG.
1918 */
1919
954c1994
GS
1920/*
1921=for apidoc looks_like_number
1922
1923Test if an the content of an SV looks like a number (or is a
1924number).
1925
1926=cut
1927*/
1928
36477c24 1929I32
864dbfa3 1930Perl_looks_like_number(pTHX_ SV *sv)
36477c24
PP
1931{
1932 register char *s;
1933 register char *send;
1934 register char *sbegin;
25da4f38
IZ
1935 register char *nbegin;
1936 I32 numtype = 0;
300aed98 1937 I32 sawinf = 0;
36477c24
PP
1938 STRLEN len;
1939
1940 if (SvPOK(sv)) {
1c846c1f 1941 sbegin = SvPVX(sv);
36477c24
PP
1942 len = SvCUR(sv);
1943 }
1944 else if (SvPOKp(sv))
1945 sbegin = SvPV(sv, len);
1946 else
1947 return 1;
1948 send = sbegin + len;
1949
1950 s = sbegin;
1951 while (isSPACE(*s))
1952 s++;
25da4f38
IZ
1953 if (*s == '-') {
1954 s++;
1955 numtype = IS_NUMBER_NEG;
1956 }
1957 else if (*s == '+')
36477c24 1958 s++;
ff0cee69 1959
25da4f38
IZ
1960 nbegin = s;
1961 /*
097ee67d
JH
1962 * we return IS_NUMBER_TO_INT_BY_ATOL if the number can be converted
1963 * to _integer_ with atol() and IS_NUMBER_TO_INT_BY_ATOF if you need
1964 * (int)atof().
25da4f38
IZ
1965 */
1966
300aed98 1967 /* next must be digit or the radix separator or beginning of infinity */
ff0cee69
PP
1968 if (isDIGIT(*s)) {
1969 do {
1970 s++;
1971 } while (isDIGIT(*s));
25da4f38
IZ
1972
1973 if (s - nbegin >= TYPE_DIGITS(IV)) /* Cannot cache ato[ul]() */
1974 numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_IV;
1975 else
1976 numtype |= IS_NUMBER_TO_INT_BY_ATOL;
1977
097ee67d 1978 if (*s == '.'
1c846c1f 1979#ifdef USE_LOCALE_NUMERIC
097ee67d
JH
1980 || IS_NUMERIC_RADIX(*s)
1981#endif
1982 ) {
ff0cee69 1983 s++;
25da4f38 1984 numtype |= IS_NUMBER_NOT_IV;
097ee67d 1985 while (isDIGIT(*s)) /* optional digits after the radix */
ff0cee69
PP
1986 s++;
1987 }
36477c24 1988 }
097ee67d 1989 else if (*s == '.'
1c846c1f 1990#ifdef USE_LOCALE_NUMERIC
097ee67d
JH
1991 || IS_NUMERIC_RADIX(*s)
1992#endif
1993 ) {
ff0cee69 1994 s++;
25da4f38 1995 numtype |= IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_NOT_IV;
097ee67d 1996 /* no digits before the radix means we need digits after it */
ff0cee69
PP
1997 if (isDIGIT(*s)) {
1998 do {
1999 s++;
2000 } while (isDIGIT(*s));
2001 }
2002 else
2003 return 0;
2004 }
300aed98
JH
2005 else if (*s == 'I' || *s == 'i') {
2006 s++; if (*s != 'N' && *s != 'n') return 0;
2007 s++; if (*s != 'F' && *s != 'f') return 0;
2008 s++; if (*s == 'I' || *s == 'i') {
2009 s++; if (*s != 'N' && *s != 'n') return 0;
2010 s++; if (*s != 'I' && *s != 'i') return 0;
2011 s++; if (*s != 'T' && *s != 't') return 0;
2012 s++; if (*s != 'Y' && *s != 'y') return 0;
2013 }
2014 sawinf = 1;
2015 }
ff0cee69
PP
2016 else
2017 return 0;
2018
300aed98
JH
2019 if (sawinf)
2020 numtype = IS_NUMBER_INFINITY;
2021 else {
2022 /* we can have an optional exponent part */
2023 if (*s == 'e' || *s == 'E') {
2024 numtype &= ~IS_NUMBER_NEG;
2025 numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_IV;
36477c24 2026 s++;
300aed98
JH
2027 if (*s == '+' || *s == '-')
2028 s++;
2029 if (isDIGIT(*s)) {
2030 do {
2031 s++;
2032 } while (isDIGIT(*s));
2033 }
2034 else
2035 return 0;
2036 }
36477c24
PP
2037 }
2038 while (isSPACE(*s))
2039 s++;
2040 if (s >= send)
2041 return numtype;
2042 if (len == 10 && memEQ(sbegin, "0 but true", 10))
25da4f38 2043 return IS_NUMBER_TO_INT_BY_ATOL;
36477c24
PP
2044 return 0;
2045}
2046
79072805 2047char *
864dbfa3 2048Perl_sv_2pv_nolen(pTHX_ register SV *sv)
1fa8b10d
JD
2049{
2050 STRLEN n_a;
2051 return sv_2pv(sv, &n_a);
2052}
2053
25da4f38 2054/* We assume that buf is at least TYPE_CHARS(UV) long. */
864dbfa3 2055static char *
25da4f38
IZ
2056uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
2057{
25da4f38
IZ
2058 char *ptr = buf + TYPE_CHARS(UV);
2059 char *ebuf = ptr;
2060 int sign;
25da4f38
IZ
2061
2062 if (is_uv)
2063 sign = 0;
2064 else if (iv >= 0) {
2065 uv = iv;
2066 sign = 0;
2067 } else {
2068 uv = -iv;
2069 sign = 1;
2070 }
2071 do {
2072 *--ptr = '0' + (uv % 10);
2073 } while (uv /= 10);
2074 if (sign)
2075 *--ptr = '-';
2076 *peob = ebuf;
2077 return ptr;
2078}
2079
1fa8b10d 2080char *
864dbfa3 2081Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
79072805
LW
2082{
2083 register char *s;
2084 int olderrno;
46fc3d4c 2085 SV *tsv;
25da4f38
IZ
2086 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
2087 char *tmpbuf = tbuf;
79072805 2088
463ee0b2
LW
2089 if (!sv) {
2090 *lp = 0;
2091 return "";
2092 }
8990e307 2093 if (SvGMAGICAL(sv)) {
463ee0b2
LW
2094 mg_get(sv);
2095 if (SvPOKp(sv)) {
2096 *lp = SvCUR(sv);
2097 return SvPVX(sv);
2098 }
cf2093f6 2099 if (SvIOKp(sv)) {
1c846c1f 2100 if (SvIsUV(sv))
57def98f 2101 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
cf2093f6 2102 else
57def98f 2103 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
46fc3d4c 2104 tsv = Nullsv;
a0d0e21e 2105 goto tokensave;
463ee0b2
LW
2106 }
2107 if (SvNOKp(sv)) {
2d4389e4 2108 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
46fc3d4c 2109 tsv = Nullsv;
a0d0e21e 2110 goto tokensave;
463ee0b2 2111 }
16d20bd9 2112 if (!SvROK(sv)) {
d008e5eb 2113 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
c6ee37c5 2114 dTHR;
d008e5eb 2115 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 2116 report_uninit();
c6ee37c5 2117 }
16d20bd9
AD
2118 *lp = 0;
2119 return "";
2120 }
463ee0b2 2121 }
ed6116ce
LW
2122 if (SvTHINKFIRST(sv)) {
2123 if (SvROK(sv)) {
a0d0e21e 2124 SV* tmpstr;
1554e226
DC
2125 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
2126 (SvRV(tmpstr) != SvRV(sv)))
9e7bc3e8 2127 return SvPV(tmpstr,*lp);
ed6116ce
LW
2128 sv = (SV*)SvRV(sv);
2129 if (!sv)
2130 s = "NULLREF";
2131 else {
f9277f47
IZ
2132 MAGIC *mg;
2133
ed6116ce 2134 switch (SvTYPE(sv)) {
f9277f47
IZ
2135 case SVt_PVMG:
2136 if ( ((SvFLAGS(sv) &
1c846c1f 2137 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
3149a8e4 2138 == (SVs_OBJECT|SVs_RMG))
57668c4d 2139 && strEQ(s=HvNAME(SvSTASH(sv)), "Regexp")
f9277f47 2140 && (mg = mg_find(sv, 'r'))) {
5c0ca799 2141 dTHR;
2cd61cdb 2142 regexp *re = (regexp *)mg->mg_obj;
1bd3ad17 2143
2cd61cdb 2144 if (!mg->mg_ptr) {
8782bef2
GB
2145 char *fptr = "msix";
2146 char reflags[6];
2147 char ch;
2148 int left = 0;
2149 int right = 4;
2150 U16 reganch = (re->reganch & PMf_COMPILETIME) >> 12;
2151
155aba94 2152 while((ch = *fptr++)) {
8782bef2
GB
2153 if(reganch & 1) {
2154 reflags[left++] = ch;
2155 }
2156 else {
2157 reflags[right--] = ch;
2158 }
2159 reganch >>= 1;
2160 }
2161 if(left != 4) {
2162 reflags[left] = '-';
2163 left = 5;
2164 }
2165
2166 mg->mg_len = re->prelen + 4 + left;
2167 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
2168 Copy("(?", mg->mg_ptr, 2, char);
2169 Copy(reflags, mg->mg_ptr+2, left, char);
2170 Copy(":", mg->mg_ptr+left+2, 1, char);
2171 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
1bd3ad17
IZ
2172 mg->mg_ptr[mg->mg_len - 1] = ')';
2173 mg->mg_ptr[mg->mg_len] = 0;
2174 }
3280af22 2175 PL_reginterp_cnt += re->program[0].next_off;
1bd3ad17
IZ
2176 *lp = mg->mg_len;
2177 return mg->mg_ptr;
f9277f47
IZ
2178 }
2179 /* Fall through */
ed6116ce
LW
2180 case SVt_NULL:
2181 case SVt_IV:
2182 case SVt_NV:
2183 case SVt_RV:
2184 case SVt_PV:
2185 case SVt_PVIV:
2186 case SVt_PVNV:
81689caa
HS
2187 case SVt_PVBM: if (SvROK(sv))
2188 s = "REF";
2189 else
2190 s = "SCALAR"; break;
ed6116ce
LW
2191 case SVt_PVLV: s = "LVALUE"; break;
2192 case SVt_PVAV: s = "ARRAY"; break;
2193 case SVt_PVHV: s = "HASH"; break;
2194 case SVt_PVCV: s = "CODE"; break;
2195 case SVt_PVGV: s = "GLOB"; break;
1d2dff63 2196 case SVt_PVFM: s = "FORMAT"; break;
36477c24 2197 case SVt_PVIO: s = "IO"; break;
ed6116ce
LW
2198 default: s = "UNKNOWN"; break;
2199 }
46fc3d4c 2200 tsv = NEWSV(0,0);
ed6116ce 2201 if (SvOBJECT(sv))
cea2e8a9 2202 Perl_sv_setpvf(aTHX_ tsv, "%s=%s", HvNAME(SvSTASH(sv)), s);
ed6116ce 2203 else
46fc3d4c 2204 sv_setpv(tsv, s);
57def98f 2205 Perl_sv_catpvf(aTHX_ tsv, "(0x%"UVxf")", PTR2UV(sv));
a0d0e21e 2206 goto tokensaveref;
463ee0b2 2207 }
ed6116ce
LW
2208 *lp = strlen(s);
2209 return s;
79072805 2210 }
0336b60e
IZ
2211 if (SvREADONLY(sv) && !SvOK(sv)) {
2212 dTHR;
2213 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 2214 report_uninit();
ed6116ce
LW
2215 *lp = 0;
2216 return "";
79072805 2217 }
79072805 2218 }
25da4f38
IZ
2219 if (SvNOKp(sv)) { /* See note in sv_2uv() */
2220 /* XXXX 64-bit? IV may have better precision... */
8aad8eb3 2221 /* I tried changing this to be 64-bit-aware and
34d861e4
JH
2222 * the t/op/numconvert.t became very, very, angry.
2223 * --jhi Sep 1999 */
79072805
LW
2224 if (SvTYPE(sv) < SVt_PVNV)
2225 sv_upgrade(sv, SVt_PVNV);
1c846c1f 2226 /* The +20 is pure guesswork. Configure test needed. --jhi */
59155cc0 2227 SvGROW(sv, NV_DIG + 20);
463ee0b2 2228 s = SvPVX(sv);
79072805 2229 olderrno = errno; /* some Xenix systems wipe out errno here */
79072805 2230#ifdef apollo
463ee0b2 2231 if (SvNVX(sv) == 0.0)
79072805
LW
2232 (void)strcpy(s,"0");
2233 else
2234#endif /*apollo*/
bbce6d69 2235 {
2d4389e4 2236 Gconvert(SvNVX(sv), NV_DIG, 0, s);
bbce6d69 2237 }
79072805 2238 errno = olderrno;
a0d0e21e
LW
2239#ifdef FIXNEGATIVEZERO
2240 if (*s == '-' && s[1] == '0' && !s[2])
2241 strcpy(s,"0");
2242#endif
79072805
LW
2243 while (*s) s++;
2244#ifdef hcx
2245 if (s[-1] == '.')
46fc3d4c 2246 *--s = '\0';
79072805
LW
2247#endif
2248 }
748a9306 2249 else if (SvIOKp(sv)) {
25da4f38 2250 U32 isIOK = SvIOK(sv);
0336b60e 2251 U32 isUIOK = SvIsUV(sv);
25da4f38
IZ
2252 char buf[TYPE_CHARS(UV)];
2253 char *ebuf, *ptr;
2254
79072805
LW
2255 if (SvTYPE(sv) < SVt_PVIV)
2256 sv_upgrade(sv, SVt_PVIV);
0336b60e 2257 if (isUIOK)
25da4f38 2258 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
0336b60e 2259 else
25da4f38 2260 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
0336b60e
IZ
2261 SvGROW(sv, ebuf - ptr + 1); /* inlined from sv_setpvn */
2262 Move(ptr,SvPVX(sv),ebuf - ptr,char);
2263 SvCUR_set(sv, ebuf - ptr);
46fc3d4c 2264 s = SvEND(sv);
0336b60e 2265 *s = '\0';
25da4f38 2266 if (isIOK)
64f14228
GA
2267 SvIOK_on(sv);
2268 else
2269 SvIOKp_on(sv);
0336b60e
IZ
2270 if (isUIOK)
2271 SvIsUV_on(sv);
2272 SvPOK_on(sv);
79072805
LW
2273 }
2274 else {
11343788 2275 dTHR;
0336b60e
IZ
2276 if (ckWARN(WARN_UNINITIALIZED)
2277 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
2278 {
1d7c1841 2279 report_uninit();
0336b60e 2280 }
a0d0e21e 2281 *lp = 0;
25da4f38
IZ
2282 if (SvTYPE(sv) < SVt_PV)
2283 /* Typically the caller expects that sv_any is not NULL now. */
2284 sv_upgrade(sv, SVt_PV);
a0d0e21e 2285 return "";
79072805 2286 }
463ee0b2
LW
2287 *lp = s - SvPVX(sv);
2288 SvCUR_set(sv, *lp);
79072805 2289 SvPOK_on(sv);
1d7c1841
GS
2290 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
2291 PTR2UV(sv),SvPVX(sv)));
463ee0b2 2292 return SvPVX(sv);
a0d0e21e
LW
2293
2294 tokensave:
2295 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
2296 /* Sneaky stuff here */
2297
2298 tokensaveref:
46fc3d4c 2299 if (!tsv)
96827780 2300 tsv = newSVpv(tmpbuf, 0);
46fc3d4c
PP
2301 sv_2mortal(tsv);
2302 *lp = SvCUR(tsv);
2303 return SvPVX(tsv);
a0d0e21e
LW
2304 }
2305 else {
2306 STRLEN len;
46fc3d4c
PP
2307 char *t;
2308
2309 if (tsv) {
2310 sv_2mortal(tsv);
2311 t = SvPVX(tsv);
2312 len = SvCUR(tsv);
2313 }
2314 else {
96827780
MB
2315 t = tmpbuf;
2316 len = strlen(tmpbuf);
46fc3d4c 2317 }
a0d0e21e 2318#ifdef FIXNEGATIVEZERO
46fc3d4c
PP
2319 if (len == 2 && t[0] == '-' && t[1] == '0') {
2320 t = "0";
2321 len = 1;
2322 }
a0d0e21e
LW
2323#endif
2324 (void)SvUPGRADE(sv, SVt_PV);
46fc3d4c 2325 *lp = len;
a0d0e21e
LW
2326 s = SvGROW(sv, len + 1);
2327 SvCUR_set(sv, len);
46fc3d4c 2328 (void)strcpy(s, t);
6bf554b4 2329 SvPOKp_on(sv);
a0d0e21e
LW
2330 return s;
2331 }
463ee0b2
LW
2332}
2333
7340a771
GS
2334char *
2335Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
2336{
560a288e
GS
2337 STRLEN n_a;
2338 return sv_2pvbyte(sv, &n_a);
7340a771
GS
2339}
2340
2341char *
2342Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
2343{
2344 return sv_2pv(sv,lp);
2345}
2346
2347char *
2348Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
2349{
560a288e
GS
2350 STRLEN n_a;
2351 return sv_2pvutf8(sv, &n_a);
7340a771
GS
2352}
2353
2354char *
2355Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
2356{
560a288e 2357 sv_utf8_upgrade(sv);
7340a771
GS
2358 return sv_2pv(sv,lp);
2359}
1c846c1f 2360
463ee0b2
LW
2361/* This function is only called on magical items */
2362bool
864dbfa3 2363Perl_sv_2bool(pTHX_ register SV *sv)
463ee0b2 2364{
8990e307 2365 if (SvGMAGICAL(sv))
463ee0b2
LW
2366 mg_get(sv);
2367
a0d0e21e
LW
2368 if (!SvOK(sv))
2369 return 0;
2370 if (SvROK(sv)) {
11343788 2371 dTHR;
a0d0e21e 2372 SV* tmpsv;
1554e226
DC
2373 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
2374 (SvRV(tmpsv) != SvRV(sv)))
9e7bc3e8 2375 return SvTRUE(tmpsv);
a0d0e21e
LW
2376 return SvRV(sv) != 0;
2377 }
463ee0b2 2378 if (SvPOKp(sv)) {
11343788
MB
2379 register XPV* Xpvtmp;
2380 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
2381 (*Xpvtmp->xpv_pv > '0' ||
2382 Xpvtmp->xpv_cur > 1 ||
2383 (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0')))
463ee0b2
LW
2384 return 1;
2385 else
2386 return 0;
2387 }
2388 else {
2389 if (SvIOKp(sv))
2390 return SvIVX(sv) != 0;
2391 else {
2392 if (SvNOKp(sv))
2393 return SvNVX(sv) != 0.0;
2394 else
2395 return FALSE;
2396 }
2397 }
79072805
LW
2398}
2399
c461cf8f
JH
2400/*
2401=for apidoc sv_utf8_upgrade
2402
2403Convert the PV of an SV to its UTF8-encoded form.
2404
2405=cut
2406*/
2407
560a288e
GS
2408void
2409Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
2410{
40826f67
JH
2411 char *s, *t;
2412 bool hibit;
560a288e
GS
2413
2414 if (!sv || !SvPOK(sv) || SvUTF8(sv))
2415 return;
2416
40826f67
JH
2417 /* This function could be much more efficient if we had a FLAG in SVs
2418 * to signal if there are any hibit chars in the PV.
560a288e 2419 */
40826f67
JH
2420 for (s = t = SvPVX(sv), hibit = FALSE; t < SvEND(sv) && !hibit; t++)
2421 if (*t & 0x80)
2422 hibit = TRUE;
560a288e 2423
40826f67 2424 if (hibit) {
67e989fb 2425 STRLEN len = SvCUR(sv) + 1; /* Plus the \0 */
00df9076 2426 SvPVX(sv) = (char*)bytes_to_utf8((U8*)s, &len);
841d7a39
JH
2427 SvCUR(sv) = len - 1;
2428 SvLEN(sv) = len; /* No longer know the real size. */
560a288e 2429 SvUTF8_on(sv);
d2f449dd 2430 Safefree(s); /* No longer using what was there before. */
560a288e
GS
2431 }
2432}
2433
c461cf8f
JH
2434/*
2435=for apidoc sv_utf8_downgrade
2436
2437Attempt to convert the PV of an SV from UTF8-encoded to byte encoding.
2438This may not be possible if the PV contains non-byte encoding characters;
2439if this is the case, either returns false or, if C<fail_ok> is not
2440true, croaks.
2441
2442=cut
2443*/
2444
560a288e
GS
2445bool
2446Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
2447{
2448 if (SvPOK(sv) && SvUTF8(sv)) {
2449 char *c = SvPVX(sv);
9f9ab905 2450 STRLEN len = SvCUR(sv) + 1; /* include trailing NUL */
00df9076 2451 if (!utf8_to_bytes((U8*)c, &len)) {
67e989fb
JH
2452 if (fail_ok)
2453 return FALSE;
4b3603a4
JH
2454 else {
2455 if (PL_op)
2456 Perl_croak(aTHX_ "Wide character in %s",
2457 PL_op_desc[PL_op->op_type]);
2458 else
2459 Perl_croak(aTHX_ "Wide character");
2460 }
67e989fb
JH
2461 }
2462 SvCUR(sv) = len - 1;
9f9ab905 2463 SvUTF8_off(sv);
560a288e
GS
2464 }
2465 return TRUE;
2466}
2467
c461cf8f
JH
2468/*
2469=for apidoc sv_utf8_encode
2470
2471Convert the PV of an SV to UTF8-encoded, but then turn off the C<SvUTF8>
1c846c1f 2472flag so that it looks like bytes again. Nothing calls this.
c461cf8f
JH
2473
2474=cut
2475*/
2476
560a288e
GS
2477void
2478Perl_sv_utf8_encode(pTHX_ register SV *sv)
2479{
2480 sv_utf8_upgrade(sv);
2481 SvUTF8_off(sv);
2482}
2483
2484bool
2485Perl_sv_utf8_decode(pTHX_ register SV *sv)
2486{
2487 if (SvPOK(sv)) {
2488 char *c;
2489 bool has_utf = FALSE;
2490 if (!sv_utf8_downgrade(sv, TRUE))
2491 return FALSE;
2492
2493 /* it is actually just a matter of turning the utf8 flag on, but
2494 * we want to make sure everything inside is valid utf8 first.
2495 */
2496 c = SvPVX(sv);
00df9076 2497 if (!is_utf8_string((U8*)c, SvCUR(sv)+1))
67e989fb
JH
2498 return FALSE;
2499
560a288e 2500 while (c < SvEND(sv)) {
67e989fb
JH
2501 if (*c++ & 0x80) {
2502 SvUTF8_on(sv);
2503 break;
2504 }
560a288e 2505 }
560a288e
GS
2506 }
2507 return TRUE;
2508}
2509
2510
79072805 2511/* Note: sv_setsv() should not be called with a source string that needs
463ee0b2 2512 * to be reused, since it may destroy the source string if it is marked
79072805
LW
2513 * as temporary.
2514 */
2515
954c1994
GS
2516/*
2517=for apidoc sv_setsv
2518
2519Copies the contents of the source SV C<ssv> into the destination SV C<dsv>.
2520The source SV may be destroyed if it is mortal. Does not handle 'set'
2521magic. See the macro forms C<SvSetSV>, C<SvSetSV_nosteal> and
2522C<sv_setsv_mg>.
2523
2524=cut
2525*/
2526
79072805 2527void
864dbfa3 2528Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
79072805 2529{
11343788 2530 dTHR;
8990e307
LW
2531 register U32 sflags;
2532 register int dtype;
2533 register int stype;
463ee0b2 2534
79072805
LW
2535 if (sstr == dstr)
2536 return;
2213622d 2537 SV_CHECK_THINKFIRST(dstr);
79072805 2538 if (!sstr)
3280af22 2539 sstr = &PL_sv_undef;
8990e307
LW
2540 stype = SvTYPE(sstr);
2541 dtype = SvTYPE(dstr);
79072805 2542
a0d0e21e 2543 SvAMAGIC_off(dstr);
9e7bc3e8 2544
463ee0b2 2545 /* There's a lot of redundancy below but we're going for speed here */
79072805 2546
8990e307 2547 switch (stype) {
79072805 2548 case SVt_NULL:
aece5585 2549 undef_sstr:
20408e3c
GS
2550 if (dtype != SVt_PVGV) {
2551 (void)SvOK_off(dstr);
2552 return;
2553 }
2554 break;
463ee0b2 2555 case SVt_IV:
aece5585
GA
2556 if (SvIOK(sstr)) {
2557 switch (dtype) {
2558 case SVt_NULL:
8990e307 2559 sv_upgrade(dstr, SVt_IV);
aece5585
GA
2560 break;
2561 case SVt_NV:
8990e307 2562 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
2563 break;
2564 case SVt_RV:
2565 case SVt_PV:
a0d0e21e 2566 sv_upgrade(dstr, SVt_PVIV);
aece5585
GA
2567 break;
2568 }
2569 (void)SvIOK_only(dstr);
2570 SvIVX(dstr) = SvIVX(sstr);
25da4f38
IZ
2571 if (SvIsUV(sstr))
2572 SvIsUV_on(dstr);
27c9684d
AP
2573 if (SvTAINTED(sstr))
2574 SvTAINT(dstr);
aece5585 2575 return;
8990e307 2576 }
aece5585
GA
2577 goto undef_sstr;
2578
463ee0b2 2579 case SVt_NV:
aece5585
GA
2580 if (SvNOK(sstr)) {
2581 switch (dtype) {
2582 case SVt_NULL:
2583 case SVt_IV:
8990e307 2584 sv_upgrade(dstr, SVt_NV);
aece5585
GA
2585 break;
2586 case SVt_RV:
2587 case SVt_PV:
2588 case SVt_PVIV:
a0d0e21e 2589 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
2590 break;
2591 }
2592 SvNVX(dstr) = SvNVX(sstr);
2593 (void)SvNOK_only(dstr);
27c9684d
AP
2594 if (SvTAINTED(sstr))
2595 SvTAINT(dstr);
aece5585 2596 return;
8990e307 2597 }
aece5585
GA
2598 goto undef_sstr;
2599
ed6116ce 2600 case SVt_RV:
8990e307 2601 if (dtype < SVt_RV)
ed6116ce 2602 sv_upgrade(dstr, SVt_RV);
c07a80fd
PP
2603 else if (dtype == SVt_PVGV &&
2604 SvTYPE(SvRV(sstr)) == SVt_PVGV) {
2605 sstr = SvRV(sstr);
a5f75d66 2606 if (sstr == dstr) {
1d7c1841
GS
2607 if (GvIMPORTED(dstr) != GVf_IMPORTED
2608 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
2609 {
a5f75d66 2610 GvIMPORTED_on(dstr);
1d7c1841 2611 }
a5f75d66
AD
2612 GvMULTI_on(dstr);
2613 return;
2614 }
c07a80fd
PP
2615 goto glob_assign;
2616 }
ed6116ce 2617 break;
463ee0b2 2618 case SVt_PV:
fc36a67e 2619 case SVt_PVFM:
8990e307 2620 if (dtype < SVt_PV)
463ee0b2 2621 sv_upgrade(dstr, SVt_PV);
463ee0b2
LW
2622 break;
2623 case SVt_PVIV:
8990e307 2624 if (dtype < SVt_PVIV)
463ee0b2 2625 sv_upgrade(dstr, SVt_PVIV);
463ee0b2
LW
2626 break;
2627 case SVt_PVNV:
8990e307 2628 if (dtype < SVt_PVNV)
463ee0b2 2629 sv_upgrade(dstr, SVt_PVNV);
463ee0b2 2630 break;
4633a7c4
LW
2631 case SVt_PVAV:
2632 case SVt_PVHV:
2633 case SVt_PVCV:
4633a7c4 2634 case SVt_PVIO:
533c011a 2635 if (PL_op)
cea2e8a9 2636 Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0),
22c35a8c 2637 PL_op_name[PL_op->op_type]);
4633a7c4 2638 else
cea2e8a9 2639 Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0));
4633a7c4
LW
2640 break;
2641
79072805 2642 case SVt_PVGV:
8990e307 2643 if (dtype <= SVt_PVGV) {
c07a80fd 2644 glob_assign:
a5f75d66 2645 if (dtype != SVt_PVGV) {
a0d0e21e
LW
2646 char *name = GvNAME(sstr);
2647 STRLEN len = GvNAMELEN(sstr);
463ee0b2 2648 sv_upgrade(dstr, SVt_PVGV);
6662521e 2649 sv_magic(dstr, dstr, '*', Nullch, 0);
85aff577 2650 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
a0d0e21e
LW
2651 GvNAME(dstr) = savepvn(name, len);
2652 GvNAMELEN(dstr) = len;
2653 SvFAKE_on(dstr); /* can coerce to non-glob */
2654 }
7bac28a0 2655 /* ahem, death to those who redefine active sort subs */
3280af22
NIS
2656 else if (PL_curstackinfo->si_type == PERLSI_SORT
2657 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
cea2e8a9 2658 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
7bac28a0 2659 GvNAME(dstr));
a0d0e21e 2660 (void)SvOK_off(dstr);
a5f75d66 2661 GvINTRO_off(dstr); /* one-shot flag */
1edc1566 2662 gp_free((GV*)dstr);
79072805 2663 GvGP(dstr) = gp_ref(GvGP(sstr));
27c9684d
AP
2664 if (SvTAINTED(sstr))
2665 SvTAINT(dstr);
1d7c1841
GS
2666 if (GvIMPORTED(dstr) != GVf_IMPORTED
2667 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
2668 {
a5f75d66 2669 GvIMPORTED_on(dstr);
1d7c1841 2670 }
a5f75d66 2671 GvMULTI_on(dstr);
79072805
LW
2672 return;
2673 }
2674 /* FALL THROUGH */
2675
2676 default:
973f89ab
CS
2677 if (SvGMAGICAL(sstr)) {
2678 mg_get(sstr);
2679 if (SvTYPE(sstr) != stype) {
2680 stype = SvTYPE(sstr);
2681 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
2682 goto glob_assign;
2683 }
2684 }
ded42b9f 2685 if (stype == SVt_PVLV)
6fc92669 2686 (void)SvUPGRADE(dstr, SVt_PVNV);
ded42b9f 2687 else
6fc92669 2688 (void)SvUPGRADE(dstr, stype);
79072805
LW
2689 }
2690
8990e307
LW
2691 sflags = SvFLAGS(sstr);
2692
2693 if (sflags & SVf_ROK) {
2694 if (dtype >= SVt_PV) {
2695 if (dtype == SVt_PVGV) {
2696 SV *sref = SvREFCNT_inc(SvRV(sstr));
2697 SV *dref = 0;
a5f75d66 2698 int intro = GvINTRO(dstr);
a0d0e21e
LW
2699
2700 if (intro) {
2701 GP *gp;
1d7c1841 2702 gp_free((GV*)dstr);
a5f75d66 2703 GvINTRO_off(dstr); /* one-shot flag */
a0d0e21e 2704 Newz(602,gp, 1, GP);
44a8e56a 2705 GvGP(dstr) = gp_ref(gp);
a0d0e21e 2706 GvSV(dstr) = NEWSV(72,0);
1d7c1841 2707 GvLINE(dstr) = CopLINE(PL_curcop);
1edc1566 2708 GvEGV(dstr) = (GV*)dstr;
a0d0e21e 2709 }
a5f75d66 2710 GvMULTI_on(dstr);
8990e307
LW
2711 switch (SvTYPE(sref)) {
2712 case SVt_PVAV:
a0d0e21e
LW
2713 if (intro)
2714 SAVESPTR(GvAV(dstr));
2715 else
2716 dref = (SV*)GvAV(dstr);
8990e307 2717 GvAV(dstr) = (AV*)sref;
39bac7f7 2718 if (!GvIMPORTED_AV(dstr)
1d7c1841
GS
2719 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
2720 {
a5f75d66 2721 GvIMPORTED_AV_on(dstr);
1d7c1841 2722 }
8990e307
LW
2723 break;
2724 case SVt_PVHV:
a0d0e21e
LW
2725 if (intro)
2726 SAVESPTR(GvHV(dstr));
2727 else
2728 dref = (SV*)GvHV(dstr);
8990e307 2729 GvHV(dstr) = (HV*)sref;
39bac7f7 2730 if (!GvIMPORTED_HV(dstr)
1d7c1841
GS
2731 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
2732 {
a5f75d66 2733 GvIMPORTED_HV_on(dstr);
1d7c1841 2734 }
8990e307
LW
2735 break;
2736 case SVt_PVCV:
8ebc5c01
PP
2737 if (intro) {
2738 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
2739 SvREFCNT_dec(GvCV(dstr));
2740 GvCV(dstr) = Nullcv;
68dc0745 2741 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3280af22 2742 PL_sub_generation++;
8ebc5c01 2743 }
a0d0e21e 2744 SAVESPTR(GvCV(dstr));
8ebc5c01 2745 }
68dc0745
PP
2746 else
2747 dref = (SV*)GvCV(dstr);
2748 if (GvCV(dstr) != (CV*)sref) {
748a9306 2749 CV* cv = GvCV(dstr);
4633a7c4 2750 if (cv) {
68dc0745
PP
2751 if (!GvCVGEN((GV*)dstr) &&
2752 (CvROOT(cv) || CvXSUB(cv)))
2753 {
beab0874 2754 SV *const_sv;
7bac28a0
PP
2755 /* ahem, death to those who redefine
2756 * active sort subs */
3280af22
NIS
2757 if (PL_curstackinfo->si_type == PERLSI_SORT &&
2758 PL_sortcop == CvSTART(cv))
1c846c1f 2759 Perl_croak(aTHX_
7bac28a0
PP
2760 "Can't redefine active sort subroutine %s",
2761 GvENAME((GV*)dstr));
beab0874
JT
2762 /* Redefining a sub - warning is mandatory if
2763 it was a const and its value changed. */
2764 if (ckWARN(WARN_REDEFINE)
2765 || (CvCONST(cv)
2766 && (!CvCONST((CV*)sref)
2767 || sv_cmp(cv_const_sv(cv),
2768 cv_const_sv((CV*)sref)))))
2769 {
2770 Perl_warner(aTHX_ WARN_REDEFINE,
2771 CvCONST(cv)
2772 ? "Constant subroutine %s redefined"
47deb5e7 2773 : "Subroutine %s redefined",
beab0874
JT
2774 GvENAME((GV*)dstr));
2775 }
9607fc9c 2776 }
3fe9a6f1
PP
2777 cv_ckproto(cv, (GV*)dstr,
2778 SvPOK(sref) ? SvPVX(sref) : Nullch);
4633a7c4 2779 }
a5f75d66 2780 GvCV(dstr) = (CV*)sref;
7a4c00b4 2781 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
a5f75d66 2782 GvASSUMECV_on(dstr);
3280af22 2783 PL_sub_generation++;
a5f75d66 2784 }
39bac7f7 2785 if (!GvIMPORTED_CV(dstr)
1d7c1841
GS
2786 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
2787 {
a5f75d66 2788 GvIMPORTED_CV_on(dstr);
1d7c1841 2789 }
8990e307 2790 break;
91bba347
LW
2791 case SVt_PVIO:
2792 if (intro)
2793 SAVESPTR(GvIOp(dstr));
2794 else
2795 dref = (SV*)GvIOp(dstr);
2796 GvIOp(dstr) = (IO*)sref;
2797 break;
f4d13ee9
JH
2798 case SVt_PVFM:
2799 if (intro)
2800 SAVESPTR(GvFORM(dstr));
2801 else
2802 dref = (SV*)GvFORM(dstr);
2803 GvFORM(dstr) = (CV*)sref;
2804 break;
8990e307 2805 default:
a0d0e21e
LW
2806 if (intro)
2807 SAVESPTR(GvSV(dstr));
2808 else
2809 dref = (SV*)GvSV(dstr);
8990e307 2810 GvSV(dstr) = sref;
39bac7f7 2811 if (!GvIMPORTED_SV(dstr)
1d7c1841
GS
2812 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
2813 {
a5f75d66 2814 GvIMPORTED_SV_on(dstr);
1d7c1841 2815 }
8990e307
LW
2816 break;
2817 }
2818 if (dref)
2819 SvREFCNT_dec(dref);
a0d0e21e
LW
2820 if (intro)
2821 SAVEFREESV(sref);
27c9684d
AP
2822 if (SvTAINTED(sstr))
2823 SvTAINT(dstr);
8990e307
LW
2824 return;
2825 }
a0d0e21e 2826 if (SvPVX(dstr)) {
760ac839 2827 (void)SvOOK_off(dstr); /* backoff */
50483b2c
JD
2828 if (SvLEN(dstr))
2829 Safefree(SvPVX(dstr));
a0d0e21e
LW
2830 SvLEN(dstr)=SvCUR(dstr)=0;
2831 }
8990e307 2832 }
a0d0e21e 2833 (void)SvOK_off(dstr);
8990e307 2834 SvRV(dstr) = SvREFCNT_inc(SvRV(sstr));
ed6116ce 2835 SvROK_on(dstr);
8990e307 2836 if (sflags & SVp_NOK) {
ed6116ce
LW
2837 SvNOK_on(dstr);
2838 SvNVX(dstr) = SvNVX(sstr);
2839 }
8990e307 2840 if (sflags & SVp_IOK) {
a0d0e21e 2841 (void)SvIOK_on(dstr);
ed6116ce 2842 SvIVX(dstr) = SvIVX(sstr);
2b1c7e3e 2843 if (sflags & SVf_IVisUV)
25da4f38 2844 SvIsUV_on(dstr);
ed6116ce 2845 }
a0d0e21e
LW
2846 if (SvAMAGIC(sstr)) {
2847 SvAMAGIC_on(dstr);
2848 }
ed6116ce 2849 }
8990e307 2850 else if (sflags & SVp_POK) {
79072805
LW
2851
2852 /*
2853 * Check to see if we can just swipe the string. If so, it's a
2854 * possible small lose on short strings, but a big win on long ones.
463ee0b2
LW
2855 * It might even be a win on short strings if SvPVX(dstr)
2856 * has to be allocated and SvPVX(sstr) has to be freed.
79072805
LW
2857 */
2858
ff68c719 2859 if (SvTEMP(sstr) && /* slated for free anyway? */
01b73108 2860 SvREFCNT(sstr) == 1 && /* and no other references to it? */
1c846c1f
NIS
2861 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
2862 SvLEN(sstr)) /* and really is a string */
a5f75d66 2863 {
adbc6bb1 2864 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
a5f75d66
AD
2865 if (SvOOK(dstr)) {
2866 SvFLAGS(dstr) &= ~SVf_OOK;
2867 Safefree(SvPVX(dstr) - SvIVX(dstr));
2868 }
50483b2c 2869 else if (SvLEN(dstr))
a5f75d66 2870 Safefree(SvPVX(dstr));
79072805 2871 }
a5f75d66 2872 (void)SvPOK_only(dstr);
463ee0b2 2873 SvPV_set(dstr, SvPVX(sstr));
79072805
LW
2874 SvLEN_set(dstr, SvLEN(sstr));
2875 SvCUR_set(dstr, SvCUR(sstr));
f4e86e0f 2876
79072805 2877 SvTEMP_off(dstr);
2b1c7e3e 2878 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
79072805
LW
2879 SvPV_set(sstr, Nullch);
2880 SvLEN_set(sstr, 0);
a5f75d66
AD
2881 SvCUR_set(sstr, 0);
2882 SvTEMP_off(sstr);
79072805
LW
2883 }
2884 else { /* have to copy actual string */
8990e307
LW
2885 STRLEN len = SvCUR(sstr);
2886
2887 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
2888 Move(SvPVX(sstr),SvPVX(dstr),len,char);
2889 SvCUR_set(dstr, len);
2890 *SvEND(dstr) = '\0';
a0d0e21e 2891 (void)SvPOK_only(dstr);
79072805 2892 }
3aa33fe5 2893 if ((sflags & SVf_UTF8) && !IN_BYTE)
a7cb1f99 2894 SvUTF8_on(dstr);
79072805 2895 /*SUPPRESS 560*/
8990e307 2896 if (sflags & SVp_NOK) {
79072805 2897 SvNOK_on(dstr);
463ee0b2 2898 SvNVX(dstr) = SvNVX(sstr);
79072805 2899 }
8990e307 2900 if (sflags & SVp_IOK) {
a0d0e21e 2901 (void)SvIOK_on(dstr);
463ee0b2 2902 SvIVX(dstr) = SvIVX(sstr);
2b1c7e3e 2903 if (sflags & SVf_IVisUV)
25da4f38 2904 SvIsUV_on(dstr);
79072805
LW
2905 }
2906 }
8990e307 2907 else if (sflags & SVp_NOK) {
463ee0b2 2908 SvNVX(dstr) = SvNVX(sstr);
a0d0e21e 2909 (void)SvNOK_only(dstr);
2b1c7e3e 2910 if (sflags & SVf_IOK) {
a0d0e21e 2911 (void)SvIOK_on(dstr);
463ee0b2 2912 SvIVX(dstr) = SvIVX(sstr);
25da4f38 2913 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
2b1c7e3e 2914 if (sflags & SVf_IVisUV)
25da4f38 2915 SvIsUV_on(dstr);
79072805
LW
2916 }
2917 }
8990e307 2918 else if (sflags & SVp_IOK) {
a0d0e21e 2919 (void)SvIOK_only(dstr);
463ee0b2 2920 SvIVX(dstr) = SvIVX(sstr);
2b1c7e3e 2921 if (sflags & SVf_IVisUV)
25da4f38 2922 SvIsUV_on(dstr);
79072805
LW
2923 }
2924 else {
20408e3c 2925 if (dtype == SVt_PVGV) {
e476b1b5
GS
2926 if (ckWARN(WARN_MISC))
2927 Perl_warner(aTHX_ WARN_MISC, "Undefined value assigned to typeglob");
20408e3c
GS
2928 }
2929 else
2930 (void)SvOK_off(dstr);
a0d0e21e 2931 }
27c9684d
AP
2932 if (SvTAINTED(sstr))
2933 SvTAINT(dstr);
79072805
LW
2934}
2935
954c1994
GS
2936/*
2937=for apidoc sv_setsv_mg
2938
2939Like C<sv_setsv>, but also handles 'set' magic.
2940
2941=cut
2942*/
2943
79072805 2944void
864dbfa3 2945Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
ef50df4b
GS
2946{
2947 sv_setsv(dstr,sstr);
2948 SvSETMAGIC(dstr);
2949}
2950
954c1994
GS
2951/*
2952=for apidoc sv_setpvn
2953
2954Copies a string into an SV. The C<len> parameter indicates the number of
2955bytes to be copied. Does not handle 'set' magic. See C<sv_setpvn_mg>.
2956
2957=cut
2958*/
2959
ef50df4b 2960void
864dbfa3 2961Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
79072805 2962{
c6f8c383 2963 register char *dptr;
4561caa4
CS
2964 assert(len >= 0); /* STRLEN is probably unsigned, so this may
2965 elicit a warning, but it won't hurt. */
2213622d 2966 SV_CHECK_THINKFIRST(sv);
463ee0b2 2967 if (!ptr) {
a0d0e21e 2968 (void)SvOK_off(sv);
463ee0b2
LW
2969 return;
2970 }
6fc92669 2971 (void)SvUPGRADE(sv, SVt_PV);
c6f8c383 2972
79072805 2973 SvGROW(sv, len + 1);
c6f8c383
GA
2974 dptr = SvPVX(sv);
2975 Move(ptr,dptr,len,char);
2976 dptr[len] = '\0';
79072805 2977 SvCUR_set(sv, len);
a0d0e21e 2978 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2 2979 SvTAINT(sv);
79072805
LW
2980}
2981
954c1994
GS
2982/*
2983=for apidoc sv_setpvn_mg
2984
2985Like C<sv_setpvn>, but also handles 'set' magic.
2986
2987=cut
2988*/
2989
79072805 2990void
864dbfa3 2991Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
ef50df4b
GS
2992{
2993 sv_setpvn(sv,ptr,len);
2994 SvSETMAGIC(sv);
2995}
2996
954c1994
GS
2997/*
2998=for apidoc sv_setpv
2999
3000Copies a string into an SV. The string must be null-terminated. Does not
3001handle 'set' magic. See C<sv_setpv_mg>.
3002
3003=cut
3004*/
3005
ef50df4b 3006void
864dbfa3 3007Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
79072805
LW
3008{
3009 register STRLEN len;
3010
2213622d 3011 SV_CHECK_THINKFIRST(sv);
463ee0b2 3012 if (!ptr) {
a0d0e21e 3013 (void)SvOK_off(sv);
463ee0b2
LW
3014 return;
3015 }
79072805 3016 len = strlen(ptr);
6fc92669 3017 (void)SvUPGRADE(sv, SVt_PV);
c6f8c383 3018
79072805 3019 SvGROW(sv, len + 1);
463ee0b2 3020 Move(ptr,SvPVX(sv),len+1,char);
79072805 3021 SvCUR_set(sv, len);
a0d0e21e 3022 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2
LW
3023 SvTAINT(sv);
3024}
3025
954c1994
GS
3026/*
3027=for apidoc sv_setpv_mg
3028
3029Like C<sv_setpv>, but also handles 'set' magic.
3030
3031=cut
3032*/
3033
463ee0b2 3034void
864dbfa3 3035Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
ef50df4b
GS
3036{
3037 sv_setpv(sv,ptr);
3038 SvSETMAGIC(sv);
3039}
3040
954c1994
GS
3041/*
3042=for apidoc sv_usepvn
3043
3044Tells an SV to use C<ptr> to find its string value. Normally the string is
1c846c1f 3045stored inside the SV but sv_usepvn allows the SV to use an outside string.
954c1994
GS
3046The C<ptr> should point to memory that was allocated by C<malloc>. The
3047string length, C<len>, must be supplied. This function will realloc the
3048memory pointed to by C<ptr>, so that pointer should not be freed or used by
3049the programmer after giving it to sv_usepvn. Does not handle 'set' magic.
3050See C<sv_usepvn_mg>.
3051
3052=cut
3053*/
3054
ef50df4b 3055void
864dbfa3 3056Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
463ee0b2 3057{
2213622d 3058 SV_CHECK_THINKFIRST(sv);
c6f8c383 3059 (void)SvUPGRADE(sv, SVt_PV);
463ee0b2 3060 if (!ptr) {
a0d0e21e 3061 (void)SvOK_off(sv);
463ee0b2
LW
3062 return;
3063 }
a0ed51b3 3064 (void)SvOOK_off(sv);
50483b2c 3065 if (SvPVX(sv) && SvLEN(sv))
463ee0b2
LW
3066 Safefree(SvPVX(sv));
3067 Renew(ptr, len+1, char);
3068 SvPVX(sv) = ptr;
3069 SvCUR_set(sv, len);
3070 SvLEN_set(sv, len+1);
3071 *SvEND(sv) = '\0';
a0d0e21e 3072 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2 3073 SvTAINT(sv);
79072805
LW
3074}
3075
954c1994
GS
3076/*
3077=for apidoc sv_usepvn_mg
3078
3079Like C<sv_usepvn>, but also handles 'set' magic.
3080
3081=cut
3082*/
3083
ef50df4b 3084void
864dbfa3 3085Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
ef50df4b 3086{
51c1089b 3087 sv_usepvn(sv,ptr,len);
ef50df4b
GS
3088 SvSETMAGIC(sv);
3089}
3090
6fc92669 3091void
864dbfa3 3092Perl_sv_force_normal(pTHX_ register SV *sv)
0f15f207 3093{
2213622d
GA
3094 if (SvREADONLY(sv)) {
3095 dTHR;
1c846c1f
NIS
3096 if (SvFAKE(sv)) {
3097 char *pvx = SvPVX(sv);
3098 STRLEN len = SvCUR(sv);
3099 U32 hash = SvUVX(sv);
3100 SvGROW(sv, len + 1);
3101 Move(pvx,SvPVX(sv),len,char);
3102 *SvEND(sv) = '\0';
3103 SvFAKE_off(sv);
3104 SvREADONLY_off(sv);
3105 unsharepvn(pvx,len,hash);
3106 }
3107 else if (PL_curcop != &PL_compiling)
cea2e8a9 3108 Perl_croak(aTHX_ PL_no_modify);
0f15f207 3109 }
2213622d
GA
3110 if (SvROK(sv))
3111 sv_unref(sv);
6fc92669
GS
3112 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
3113 sv_unglob(sv);
0f15f207 3114}
1c846c1f 3115
954c1994
GS
3116/*
3117=for apidoc sv_chop
3118
1c846c1f 3119Efficient removal of characters from the beginning of the string buffer.
954c1994
GS
3120SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside
3121the string buffer. The C<ptr> becomes the first character of the adjusted
3122string.
3123
3124=cut
3125*/
3126
79072805 3127void
864dbfa3 3128Perl_sv_chop(pTHX_ register SV *sv, register char *ptr) /* like set but assuming ptr is in sv */
1c846c1f
NIS
3129
3130
79072805
LW
3131{
3132 register STRLEN delta;
3133
a0d0e21e 3134 if (!ptr || !SvPOKp(sv))
79072805 3135 return;
2213622d 3136 SV_CHECK_THINKFIRST(sv);
79072805
LW
3137 if (SvTYPE(sv) < SVt_PVIV)
3138 sv_upgrade(sv,SVt_PVIV);
3139
3140 if (!SvOOK(sv)) {
50483b2c
JD
3141 if (!SvLEN(sv)) { /* make copy of shared string */
3142 char *pvx = SvPVX(sv);
3143 STRLEN len = SvCUR(sv);
3144 SvGROW(sv, len + 1);
3145 Move(pvx,SvPVX(sv),len,char);
3146 *SvEND(sv) = '\0';
3147 }
463ee0b2 3148 SvIVX(sv) = 0;
79072805
LW
3149 SvFLAGS(sv) |= SVf_OOK;
3150 }
25da4f38 3151 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK|SVf_IVisUV);
463ee0b2 3152 delta = ptr - SvPVX(sv);
79072805
LW
3153 SvLEN(sv) -= delta;
3154 SvCUR(sv) -= delta;
463ee0b2
LW
3155 SvPVX(sv) += delta;
3156 SvIVX(sv) += delta;
79072805
LW
3157}
3158
954c1994
GS
3159/*
3160=for apidoc sv_catpvn
3161
3162Concatenates the string onto the end of the string which is in the SV. The
3163C<len> indicates number of bytes to copy. Handles 'get' magic, but not
3164'set' magic. See C<sv_catpvn_mg>.
3165
3166=cut
3167*/
3168
79072805 3169void
864dbfa3 3170Perl_sv_catpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
79072805 3171{
463ee0b2 3172 STRLEN tlen;
748a9306 3173 char *junk;
a0d0e21e 3174
748a9306 3175 junk = SvPV_force(sv, tlen);
463ee0b2 3176 SvGROW(sv, tlen + len + 1);
4633a7c4
LW
3177 if (ptr == junk)
3178 ptr = SvPVX(sv);
463ee0b2 3179 Move(ptr,SvPVX(sv)+tlen,len,char);
79072805
LW
3180 SvCUR(sv) += len;
3181 *SvEND(sv) = '\0';
d41ff1b8 3182 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 3183 SvTAINT(sv);
79072805
LW
3184}
3185
954c1994
GS
3186/*
3187=for apidoc sv_catpvn_mg
3188
3189Like C<sv_catpvn>, but also handles 'set' magic.
3190
3191=cut
3192*/
3193
79072805 3194void
864dbfa3 3195Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
ef50df4b
GS
3196{
3197 sv_catpvn(sv,ptr,len);
3198 SvSETMAGIC(sv);
3199}
3200
954c1994
GS
3201/*
3202=for apidoc sv_catsv
3203
3204Concatenates the string from SV C<ssv> onto the end of the string in SV
3205C<dsv>. Handles 'get' magic, but not 'set' magic. See C<sv_catsv_mg>.
3206
3207=cut
3208*/
3209
ef50df4b 3210void
864dbfa3 3211Perl_sv_catsv(pTHX_ SV *dstr, register SV *sstr)
79072805
LW
3212{
3213 char *s;
463ee0b2 3214 STRLEN len;
79072805
LW
3215 if (!sstr)
3216 return;
155aba94 3217 if ((s = SvPV(sstr, len))) {
e84ff256 3218 if (DO_UTF8(sstr)) {
560a288e 3219 sv_utf8_upgrade(dstr);
e84ff256 3220 sv_catpvn(dstr,s,len);
560a288e 3221 SvUTF8_on(dstr);
e84ff256
GS
3222 }
3223 else
3224 sv_catpvn(dstr,s,len);
560a288e 3225 }
79072805
LW
3226}
3227
954c1994
GS
3228/*
3229=for apidoc sv_catsv_mg
3230
3231Like C<sv_catsv>, but also handles 'set' magic.
3232
3233=cut
3234*/
3235
79072805 3236void
864dbfa3 3237Perl_sv_catsv_mg(pTHX_ SV *dstr, register SV *sstr)
ef50df4b
GS
3238{
3239 sv_catsv(dstr,sstr);
3240 SvSETMAGIC(dstr);
3241}
3242
954c1994
GS
3243/*
3244=for apidoc sv_catpv
3245
3246Concatenates the string onto the end of the string which is in the SV.
3247Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>.
3248
3249=cut
3250*/
3251
ef50df4b 3252void
864dbfa3 3253Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr)
79072805
LW
3254{
3255 register STRLEN len;
463ee0b2 3256 STRLEN tlen;
748a9306 3257 char *junk;
79072805 3258
79072805
LW
3259 if (!ptr)
3260 return;
748a9306 3261 junk = SvPV_force(sv, tlen);
79072805 3262 len = strlen(ptr);
463ee0b2 3263 SvGROW(sv, tlen + len + 1);
4633a7c4
LW
3264 if (ptr == junk)
3265 ptr = SvPVX(sv);
463ee0b2 3266 Move(ptr,SvPVX(sv)+tlen,len+1,char);
79072805 3267 SvCUR(sv) += len;
d41ff1b8 3268 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 3269 SvTAINT(sv);
79072805
LW
3270}
3271
954c1994
GS
3272/*
3273=for apidoc sv_catpv_mg
3274
3275Like C<sv_catpv>, but also handles 'set' magic.
3276
3277=cut
3278*/
3279
ef50df4b 3280void
864dbfa3 3281Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr)
ef50df4b 3282{
51c1089b 3283 sv_catpv(sv,ptr);
ef50df4b
GS
3284 SvSETMAGIC(sv);
3285}
3286
79072805 3287SV *
864dbfa3 3288Perl_newSV(pTHX_ STRLEN len)
79072805
LW
3289{
3290 register SV *sv;
1c846c1f 3291
4561caa4 3292 new_SV(sv);
79072805
LW
3293 if (len) {
3294 sv_upgrade(sv, SVt_PV);
3295 SvGROW(sv, len + 1);
3296 }
3297 return sv;
3298}
3299
1edc1566
PP
3300/* name is assumed to contain an SV* if (name && namelen == HEf_SVKEY) */
3301
954c1994
GS
3302/*
3303=for apidoc sv_magic
3304
3305Adds magic to an SV.
3306
3307=cut
3308*/
3309
79072805 3310void
864dbfa3 3311Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen)
79072805
LW
3312{
3313 MAGIC* mg;
1c846c1f 3314
0f15f207
MB
3315 if (SvREADONLY(sv)) {
3316 dTHR;
3280af22 3317 if (PL_curcop != &PL_compiling && !strchr("gBf", how))
cea2e8a9 3318 Perl_croak(aTHX_ PL_no_modify);
0f15f207 3319 }
4633a7c4 3320 if (SvMAGICAL(sv) || (how == 't' && SvTYPE(sv) >= SVt_PVMG)) {
748a9306
LW
3321 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
3322 if (how == 't')
565764a8 3323 mg->mg_len |= 1;
463ee0b2 3324 return;
748a9306 3325 }
463ee0b2
LW
3326 }
3327 else {
c6f8c383 3328 (void)SvUPGRADE(sv, SVt_PVMG);
463ee0b2 3329 }
79072805
LW
3330 Newz(702,mg, 1, MAGIC);
3331 mg->mg_moremagic = SvMAGIC(sv);
463ee0b2 3332
79072805 3333 SvMAGIC(sv) = mg;
c277df42 3334 if (!obj || obj == sv || how == '#' || how == 'r')
8990e307 3335 mg->mg_obj = obj;
85e6fe83 3336 else {
11343788 3337 dTHR;
8990e307 3338 mg->mg_obj = SvREFCNT_inc(obj);
85e6fe83
LW
3339 mg->mg_flags |= MGf_REFCOUNTED;
3340 }
79072805 3341 mg->mg_type = how;
565764a8 3342 mg->mg_len = namlen;
1edc1566
PP
3343 if (name)
3344 if (namlen >= 0)
3345 mg->mg_ptr = savepvn(name, namlen);
c6ee37c5 3346 else if (namlen == HEf_SVKEY)
1edc1566 3347 mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
1c846c1f 3348
79072805
LW
3349 switch (how) {
3350 case 0:
22c35a8c 3351 mg->mg_virtual = &PL_vtbl_sv;
79072805 3352 break;
a0d0e21e 3353 case 'A':
22c35a8c 3354 mg->mg_virtual = &PL_vtbl_amagic;
a0d0e21e
LW
3355 break;
3356 case 'a':
22c35a8c 3357 mg->mg_virtual = &PL_vtbl_amagicelem;
a0d0e21e
LW
3358 break;
3359 case 'c':
3360 mg->mg_virtual = 0;
3361 break;
79072805 3362 case 'B':
22c35a8c 3363 mg->mg_virtual = &PL_vtbl_bm;
79072805 3364 break;
6cef1e77 3365 case 'D':
22c35a8c 3366 mg->mg_virtual = &PL_vtbl_regdata;
6cef1e77
IZ
3367 break;
3368 case 'd':
22c35a8c 3369 mg->mg_virtual = &PL_vtbl_regdatum;
6cef1e77 3370 break;
79072805 3371 case 'E':
22c35a8c 3372 mg->mg_virtual = &PL_vtbl_env;
79072805 3373 break;
55497cff 3374 case 'f':
22c35a8c 3375 mg->mg_virtual = &PL_vtbl_fm;
55497cff 3376 break;
79072805 3377 case 'e':
22c35a8c 3378 mg->mg_virtual = &PL_vtbl_envelem;
79072805 3379 break;
93a17b20 3380 case 'g':
22c35a8c 3381 mg->mg_virtual = &PL_vtbl_mglob;
93a17b20 3382 break;
463ee0b2 3383 case 'I':
22c35a8c 3384 mg->mg_virtual = &PL_vtbl_isa;
463ee0b2
LW
3385 break;
3386 case 'i':
22c35a8c 3387 mg->mg_virtual = &PL_vtbl_isaelem;
463ee0b2 3388 break;
16660edb 3389 case 'k':
22c35a8c 3390 mg->mg_virtual = &PL_vtbl_nkeys;
16660edb 3391 break;
79072805 3392 case 'L':
a0d0e21e 3393 SvRMAGICAL_on(sv);
93a17b20
LW
3394 mg->mg_virtual = 0;
3395 break;
3396 case 'l':
22c35a8c 3397 mg->mg_virtual = &PL_vtbl_dbline;
79072805 3398 break;
f93b4edd
MB
3399#ifdef USE_THREADS
3400 case 'm':
22c35a8c 3401 mg->mg_virtual = &PL_vtbl_mutex;
f93b4edd
MB
3402 break;
3403#endif /* USE_THREADS */
36477c24 3404#ifdef USE_LOCALE_COLLATE
bbce6d69 3405 case 'o':
22c35a8c 3406 mg->mg_virtual = &PL_vtbl_collxfrm;
bbce6d69 3407 break;
36477c24 3408#endif /* USE_LOCALE_COLLATE */
463ee0b2 3409 case 'P':
22c35a8c 3410 mg->mg_virtual = &PL_vtbl_pack;
463ee0b2
LW
3411 break;
3412 case 'p':
a0d0e21e 3413 case 'q':
22c35a8c 3414 mg->mg_virtual = &PL_vtbl_packelem;
463ee0b2 3415 break;
c277df42 3416 case 'r':
22c35a8c 3417 mg->mg_virtual = &PL_vtbl_regexp;
c277df42 3418 break;
79072805 3419 case 'S':
22c35a8c 3420 mg->mg_virtual = &PL_vtbl_sig;
79072805
LW
3421 break;
3422 case 's':
22c35a8c 3423 mg->mg_virtual = &PL_vtbl_sigelem;
79072805 3424 break;
463ee0b2 3425 case 't':
22c35a8c 3426 mg->mg_virtual = &PL_vtbl_taint;
565764a8 3427 mg->mg_len = 1;
463ee0b2 3428 break;
79072805 3429 case 'U':
22c35a8c 3430 mg->mg_virtual = &PL_vtbl_uvar;
79072805
LW
3431 break;
3432 case 'v':
22c35a8c 3433 mg->mg_virtual = &PL_vtbl_vec;
79072805
LW
3434 break;
3435 case 'x':
22c35a8c 3436 mg->mg_virtual = &PL_vtbl_substr;
79072805 3437 break;
5f05dabc 3438 case 'y':
22c35a8c 3439 mg->mg_virtual = &PL_vtbl_defelem;
5f05dabc 3440 break;
79072805 3441 case '*':
22c35a8c 3442 mg->mg_virtual = &PL_vtbl_glob;
79072805
LW
3443 break;
3444 case '#':
22c35a8c 3445 mg->mg_virtual = &PL_vtbl_arylen;
79072805 3446 break;
a0d0e21e 3447 case '.':
22c35a8c 3448 mg->mg_virtual = &PL_vtbl_pos;
a0d0e21e 3449 break;
810b8aa5
GS
3450 case '<':
3451 mg->mg_virtual = &PL_vtbl_backref;
3452 break;
4633a7c4
LW
3453 case '~': /* Reserved for use by extensions not perl internals. */
3454 /* Useful for attaching extension internal data to perl vars. */
3455 /* Note that multiple extensions may clash if magical scalars */
3456 /* etc holding private data from one are passed to another. */
3457 SvRMAGICAL_on(sv);
a0d0e21e 3458 break;
79072805 3459 default:
cea2e8a9 3460 Perl_croak(aTHX_ "Don't know how to handle magic of type '%c'", how);
463ee0b2 3461 }
8990e307
LW
3462 mg_magical(sv);
3463 if (SvGMAGICAL(sv))
3464 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
463ee0b2
LW
3465}
3466
c461cf8f
JH
3467/*
3468=for apidoc sv_unmagic
3469
3470Removes magic from an SV.
3471
3472=cut
3473*/
3474
463ee0b2 3475int
864dbfa3 3476Perl_sv_unmagic(pTHX_ SV *sv, int type)
463ee0b2
LW
3477{
3478 MAGIC* mg;
3479 MAGIC** mgp;
91bba347 3480 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
463ee0b2
LW
3481 return 0;
3482 mgp = &SvMAGIC(sv);
3483 for (mg = *mgp; mg; mg = *mgp) {
3484 if (mg->mg_type == type) {
3485 MGVTBL* vtbl = mg->mg_virtual;
3486 *mgp = mg->mg_moremagic;
1d7c1841 3487 if (vtbl && vtbl->svt_free)
fc0dc3b3 3488 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
463ee0b2 3489 if (mg->mg_ptr && mg->mg_type != 'g')
565764a8 3490 if (mg->mg_len >= 0)
1edc1566 3491 Safefree(mg->mg_ptr);
565764a8 3492 else if (mg->mg_len == HEf_SVKEY)
1edc1566 3493 SvREFCNT_dec((SV*)mg->mg_ptr);
a0d0e21e
LW
3494 if (mg->mg_flags & MGf_REFCOUNTED)
3495 SvREFCNT_dec(mg->mg_obj);
463ee0b2
LW
3496 Safefree(mg);
3497 }
3498 else
3499 mgp = &mg->mg_moremagic;
79072805 3500 }
91bba347 3501 if (!SvMAGIC(sv)) {
463ee0b2 3502 SvMAGICAL_off(sv);
8990e307 3503 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
463ee0b2
LW
3504 }
3505
3506 return 0;
79072805
LW
3507}
3508
c461cf8f
JH
3509/*
3510=for apidoc sv_rvweaken
3511
3512Weaken a reference.
3513
3514=cut
3515*/
3516
810b8aa5 3517SV *
864dbfa3 3518Perl_sv_rvweaken(pTHX_ SV *sv)
810b8aa5
GS
3519{
3520 SV *tsv;
3521 if (!SvOK(sv)) /* let undefs pass */
3522 return sv;
3523 if (!SvROK(sv))
cea2e8a9 3524 Perl_croak(aTHX_ "Can't weaken a nonreference");
810b8aa5
GS
3525 else if (SvWEAKREF(sv)) {
3526 dTHR;
3527 if (ckWARN(WARN_MISC))
cea2e8a9 3528 Perl_warner(aTHX_ WARN_MISC, "Reference is already weak");
810b8aa5
GS
3529 return sv;
3530 }
3531 tsv = SvRV(sv);
3532 sv_add_backref(tsv, sv);
3533 SvWEAKREF_on(sv);
1c846c1f 3534 SvREFCNT_dec(tsv);
810b8aa5
GS
3535 return sv;
3536}
3537
3538STATIC void
cea2e8a9 3539S_sv_add_backref(pTHX_ SV *tsv, SV *sv)
810b8aa5
GS
3540{
3541 AV *av;
3542 MAGIC *mg;
3543 if (SvMAGICAL(tsv) && (mg = mg_find(tsv, '<')))
3544 av = (AV*)mg->mg_obj;
3545 else {
3546 av = newAV();
3547 sv_magic(tsv, (SV*)av, '<', NULL, 0);
3548 SvREFCNT_dec(av); /* for sv_magic */
3549 }
3550 av_push(av,sv);
3551}
3552
1c846c1f 3553STATIC void
cea2e8a9 3554S_sv_del_backref(pTHX_ SV *sv)
810b8aa5
GS
3555{
3556 AV *av;
3557 SV **svp;
3558 I32 i;
3559 SV *tsv = SvRV(sv);
3560 MAGIC *mg;
3561 if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, '<')))
cea2e8a9 3562 Perl_croak(aTHX_ "panic: del_backref");
810b8aa5
GS
3563 av = (AV *)mg->mg_obj;
3564 svp = AvARRAY(av);
3565 i = AvFILLp(av);
3566 while (i >= 0) {
3567 if (svp[i] == sv) {
3568 svp[i] = &PL_sv_undef; /* XXX */
3569 }
3570 i--;
3571 }
3572}
3573
954c1994
GS
3574/*
3575=for apidoc sv_insert
3576
3577Inserts a string at the specified offset/length within the SV. Similar to
3578the Perl substr() function.
3579
3580=cut
3581*/
3582
79072805 3583void
864dbfa3 3584Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, char *little, STRLEN littlelen)
79072805
LW
3585{
3586 register char *big;
3587 register char *mid;
3588 register char *midend;
3589 register char *bigend;
3590 register I32 i;
6ff81951 3591 STRLEN curlen;
1c846c1f 3592
79072805 3593
8990e307 3594 if (!bigstr)
cea2e8a9 3595 Perl_croak(aTHX_ "Can't modify non-existent substring");
6ff81951 3596 SvPV_force(bigstr, curlen);
60fa28ff 3597 (void)SvPOK_only_UTF8(bigstr);
6ff81951
GS
3598 if (offset + len > curlen) {
3599 SvGROW(bigstr, offset+len+1);
3600 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
3601 SvCUR_set(bigstr, offset+len);
3602 }
79072805 3603
69b47968 3604 SvTAINT(bigstr);
79072805
LW
3605 i = littlelen - len;
3606 if (i > 0) { /* string might grow */
a0d0e21e 3607 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
79072805
LW
3608 mid = big + offset + len;
3609 midend = bigend = big + SvCUR(bigstr);
3610 bigend += i;
3611 *bigend = '\0';
3612 while (midend > mid) /* shove everything down */
3613 *--bigend = *--midend;
3614 Move(little,big+offset,littlelen,char);
3615 SvCUR(bigstr) += i;
3616 SvSETMAGIC(bigstr);
3617 return;
3618 }
3619 else if (i == 0) {
463ee0b2 3620 Move(little,SvPVX(bigstr)+offset,len,char);
79072805
LW
3621 SvSETMAGIC(bigstr);
3622 return;
3623 }
3624
463ee0b2 3625 big = SvPVX(bigstr);
79072805
LW
3626 mid = big + offset;
3627 midend = mid + len;
3628 bigend = big + SvCUR(bigstr);
3629
3630 if (midend > bigend)
cea2e8a9 3631 Perl_croak(aTHX_ "panic: sv_insert");
79072805
LW
3632
3633 if (mid - big > bigend - midend) { /* faster to shorten from end */
3634 if (littlelen) {
3635 Move(little, mid, littlelen,char);
3636 mid += littlelen;
3637 }
3638 i = bigend - midend;
3639 if (i > 0) {
3640 Move(midend, mid, i,char);
3641 mid += i;
3642 }
3643 *mid = '\0';
3644 SvCUR_set(bigstr, mid - big);
3645 }
3646 /*SUPPRESS 560*/
155aba94 3647 else if ((i = mid - big)) { /* faster from front */
79072805
LW
3648 midend -= littlelen;
3649 mid = midend;
3650 sv_chop(bigstr,midend-i);
3651 big += i;
3652 while (i--)
3653 *--midend = *--big;
3654 if (littlelen)
3655 Move(little, mid, littlelen,char);
3656 }
3657 else if (littlelen) {
3658 midend -= littlelen;
3659 sv_chop(bigstr,midend);
3660 Move(little,midend,littlelen,char);
3661 }
3662 else {
3663 sv_chop(bigstr,midend);
3664 }
3665 SvSETMAGIC(bigstr);
3666}
3667
c461cf8f
JH
3668/*
3669=for apidoc sv_replace
3670
3671Make the first argument a copy of the second, then delete the original.
3672
3673=cut
3674*/
79072805
LW
3675
3676void
864dbfa3 3677Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv)
79072805 3678{
0453d815 3679 dTHR;
79072805 3680 U32 refcnt = SvREFCNT(sv);
2213622d 3681 SV_CHECK_THINKFIRST(sv);
0453d815
PM
3682 if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL))
3683 Perl_warner(aTHX_ WARN_INTERNAL, "Reference miscount in sv_replace()");
93a17b20 3684 if (SvMAGICAL(sv)) {
a0d0e21e
LW
3685 if (SvMAGICAL(nsv))
3686 mg_free(nsv);
3687 else
3688 sv_upgrade(nsv, SVt_PVMG);
93a17b20 3689 SvMAGIC(nsv) = SvMAGIC(sv);
a0d0e21e 3690 SvFLAGS(nsv) |= SvMAGICAL(sv);
93a17b20
LW
3691 SvMAGICAL_off(sv);
3692 SvMAGIC(sv) = 0;
3693 }
79072805
LW
3694 SvREFCNT(sv) = 0;
3695 sv_clear(sv);
477f5d66 3696 assert(!SvREFCNT(sv));
79072805
LW
3697 StructCopy(nsv,sv,SV);
3698 SvREFCNT(sv) = refcnt;
1edc1566 3699 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
463ee0b2 3700 del_SV(nsv);
79072805
LW
3701}
3702
c461cf8f
JH
3703/*
3704=for apidoc sv_clear
3705
3706Clear an SV, making it empty. Does not free the memory used by the SV
3707itself.
3708
3709=cut
3710*/
3711
79072805 3712void
864dbfa3 3713Perl_sv_clear(pTHX_ register SV *sv)
79072805 3714{
ec12f114 3715 HV* stash;
79072805
LW
3716 assert(sv);
3717 assert(SvREFCNT(sv) == 0);
3718
ed6116ce 3719 if (SvOBJECT(sv)) {