This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[8095] HP-UX 11.00 / cc / 64bitint & 64bitall / perlio
[perl5.git] / pp_hot.c
CommitLineData
a0d0e21e
LW
1/* pp_hot.c
2 *
3818b22b 3 * Copyright (c) 1991-2000, Larry Wall
a0d0e21e
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 *
8 */
9
10/*
11 * Then he heard Merry change the note, and up went the Horn-cry of Buckland,
12 * shaking the air.
13 *
14 * Awake! Awake! Fear, Fire, Foes! Awake!
15 * Fire, Foes! Awake!
16 */
17
18#include "EXTERN.h"
864dbfa3 19#define PERL_IN_PP_HOT_C
a0d0e21e
LW
20#include "perl.h"
21
22/* Hot code. */
23
11343788 24#ifdef USE_THREADS
51371543 25static void unset_cvowner(pTHXo_ void *cvarg);
11343788
MB
26#endif /* USE_THREADS */
27
a0d0e21e
LW
28PP(pp_const)
29{
4e35701f 30 djSP;
1d7c1841 31 XPUSHs(cSVOP_sv);
a0d0e21e
LW
32 RETURN;
33}
34
35PP(pp_nextstate)
36{
533c011a 37 PL_curcop = (COP*)PL_op;
a0d0e21e 38 TAINT_NOT; /* Each statement is presumed innocent */
3280af22 39 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e
LW
40 FREETMPS;
41 return NORMAL;
42}
43
44PP(pp_gvsv)
45{
4e35701f 46 djSP;
924508f0 47 EXTEND(SP,1);
533c011a 48 if (PL_op->op_private & OPpLVAL_INTRO)
1d7c1841 49 PUSHs(save_scalar(cGVOP_gv));
a0d0e21e 50 else
1d7c1841 51 PUSHs(GvSV(cGVOP_gv));
a0d0e21e
LW
52 RETURN;
53}
54
55PP(pp_null)
56{
57 return NORMAL;
58}
59
7399586d
HS
60PP(pp_setstate)
61{
62 PL_curcop = (COP*)PL_op;
63 return NORMAL;
64}
65
a0d0e21e
LW
66PP(pp_pushmark)
67{
3280af22 68 PUSHMARK(PL_stack_sp);
a0d0e21e
LW
69 return NORMAL;
70}
71
72PP(pp_stringify)
73{
4e35701f 74 djSP; dTARGET;
a0d0e21e
LW
75 STRLEN len;
76 char *s;
77 s = SvPV(TOPs,len);
78 sv_setpvn(TARG,s,len);
234a4bc6
GS
79 if (SvUTF8(TOPs) && !IN_BYTE)
80 SvUTF8_on(TARG);
a0d0e21e
LW
81 SETTARG;
82 RETURN;
83}
84
85PP(pp_gv)
86{
4e35701f 87 djSP;
1d7c1841 88 XPUSHs((SV*)cGVOP_gv);
a0d0e21e
LW
89 RETURN;
90}
91
92PP(pp_and)
93{
4e35701f 94 djSP;
a0d0e21e
LW
95 if (!SvTRUE(TOPs))
96 RETURN;
97 else {
98 --SP;
99 RETURNOP(cLOGOP->op_other);
100 }
101}
102
103PP(pp_sassign)
104{
4e35701f 105 djSP; dPOPTOPssrl;
748a9306 106
533c011a 107 if (PL_op->op_private & OPpASSIGN_BACKWARDS) {
a0d0e21e
LW
108 SV *temp;
109 temp = left; left = right; right = temp;
110 }
3280af22 111 if (PL_tainting && PL_tainted && !SvTAINTED(left))
a0d0e21e 112 TAINT_NOT;
54310121 113 SvSetMagicSV(right, left);
a0d0e21e
LW
114 SETs(right);
115 RETURN;
116}
117
118PP(pp_cond_expr)
119{
4e35701f 120 djSP;
a0d0e21e 121 if (SvTRUEx(POPs))
1a67a97c 122 RETURNOP(cLOGOP->op_other);
a0d0e21e 123 else
1a67a97c 124 RETURNOP(cLOGOP->op_next);
a0d0e21e
LW
125}
126
127PP(pp_unstack)
128{
129 I32 oldsave;
130 TAINT_NOT; /* Each statement is presumed innocent */
3280af22 131 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e 132 FREETMPS;
3280af22 133 oldsave = PL_scopestack[PL_scopestack_ix - 1];
a0d0e21e
LW
134 LEAVE_SCOPE(oldsave);
135 return NORMAL;
136}
137
a0d0e21e
LW
138PP(pp_concat)
139{
4e35701f 140 djSP; dATARGET; tryAMAGICbin(concat,opASSIGN);
748a9306
LW
141 {
142 dPOPTOPssrl;
a0d0e21e 143 STRLEN len;
37931a30 144 U8 *s;
ed646e6e
SC
145 bool left_utf8;
146 bool right_utf8;
69b47968 147
82f2f503
SC
148 if (TARG == right && SvGMAGICAL(right))
149 mg_get(right);
150 if (SvGMAGICAL(left))
151 mg_get(left);
152
ed646e6e
SC
153 left_utf8 = DO_UTF8(left);
154 right_utf8 = DO_UTF8(right);
7889fe52 155
ed646e6e
SC
156 if (left_utf8 != right_utf8) {
157 if (TARG == right && !right_utf8) {
37931a30
JH
158 sv_utf8_upgrade(TARG); /* Now straight binary copy */
159 SvUTF8_on(TARG);
160 }
161 else {
162 /* Set TARG to PV(left), then add right */
163 U8 *l, *c, *olds = NULL;
164 STRLEN targlen;
15bb2692 165 s = (U8*)SvPV(right,len);
ed646e6e 166 right_utf8 |= DO_UTF8(right);
37931a30 167 if (TARG == right) {
15bb2692 168 /* Take a copy since we're about to overwrite TARG */
b7018214 169 olds = s = (U8*)savepvn((char*)s, len);
37931a30 170 }
689440ec 171 if (!SvOK(left) && SvTYPE(left) <= SVt_PVMG) {
905d5022
JH
172 if (SvREADONLY(left))
173 left = sv_2mortal(newSVsv(left));
689440ec
JH
174 else
175 sv_setpv(left, ""); /* Suppress warning. */
176 }
37931a30 177 l = (U8*)SvPV(left, targlen);
ed646e6e 178 left_utf8 |= DO_UTF8(left);
37931a30
JH
179 if (TARG != left)
180 sv_setpvn(TARG, (char*)l, targlen);
ed646e6e 181 if (!left_utf8)
37931a30
JH
182 sv_utf8_upgrade(TARG);
183 /* Extend TARG to length of right (s) */
184 targlen = SvCUR(TARG) + len;
ed646e6e 185 if (!right_utf8) {
37931a30 186 /* plus one for each hi-byte char if we have to upgrade */
15bb2692 187 for (c = s; c < s + len; c++) {
ed646e6e 188 if (UTF8_IS_CONTINUED(*c))
37931a30
JH
189 targlen++;
190 }
191 }
192 SvGROW(TARG, targlen+1);
193 /* And now copy, maybe upgrading right to UTF8 on the fly */
ed646e6e
SC
194 if (right_utf8)
195 Copy(s, SvEND(TARG), len, U8);
196 else {
197 for (c = (U8*)SvEND(TARG); len--; s++)
198 c = uv_to_utf8(c, *s);
199 }
37931a30
JH
200 SvCUR_set(TARG, targlen);
201 *SvEND(TARG) = '\0';
202 SvUTF8_on(TARG);
203 SETs(TARG);
204 Safefree(olds);
205 RETURN;
206 }
207 }
208
a0d0e21e 209 if (TARG != left) {
37931a30 210 s = (U8*)SvPV(left,len);
69b47968 211 if (TARG == right) {
37931a30 212 sv_insert(TARG, 0, 0, (char*)s, len);
69b47968
GS
213 SETs(TARG);
214 RETURN;
215 }
37931a30 216 sv_setpvn(TARG, (char *)s, len);
a0d0e21e 217 }
37931a30 218 else if (!SvOK(TARG) && SvTYPE(TARG) <= SVt_PVMG)
748a9306 219 sv_setpv(TARG, ""); /* Suppress warning. */
37931a30 220 s = (U8*)SvPV(right,len);
5bc28da9
NIS
221 if (SvOK(TARG)) {
222#if defined(PERL_Y2KWARN)
e476b1b5 223 if ((SvIOK(right) || SvNOK(right)) && ckWARN(WARN_Y2K)) {
5bc28da9
NIS
224 STRLEN n;
225 char *s = SvPV(TARG,n);
226 if (n >= 2 && s[n-2] == '1' && s[n-1] == '9'
227 && (n == 2 || !isDIGIT(s[n-3])))
228 {
e476b1b5 229 Perl_warner(aTHX_ WARN_Y2K, "Possible Y2K bug: %s",
5bc28da9
NIS
230 "about to append an integer to '19'");
231 }
232 }
233#endif
37931a30 234 sv_catpvn(TARG, (char *)s, len);
5bc28da9 235 }
68dc0745 236 else
37931a30 237 sv_setpvn(TARG, (char *)s, len); /* suppress warning */
ed646e6e 238 if (left_utf8)
e84ff256 239 SvUTF8_on(TARG);
a0d0e21e
LW
240 SETTARG;
241 RETURN;
748a9306 242 }
a0d0e21e
LW
243}
244
245PP(pp_padsv)
246{
4e35701f 247 djSP; dTARGET;
a0d0e21e 248 XPUSHs(TARG);
533c011a
NIS
249 if (PL_op->op_flags & OPf_MOD) {
250 if (PL_op->op_private & OPpLVAL_INTRO)
251 SAVECLEARSV(PL_curpad[PL_op->op_targ]);
252 else if (PL_op->op_private & OPpDEREF) {
8ec5e241 253 PUTBACK;
533c011a 254 vivify_ref(PL_curpad[PL_op->op_targ], PL_op->op_private & OPpDEREF);
8ec5e241
NIS
255 SPAGAIN;
256 }
4633a7c4 257 }
a0d0e21e
LW
258 RETURN;
259}
260
261PP(pp_readline)
262{
f5284f61 263 tryAMAGICunTARGET(iter, 0);
3280af22 264 PL_last_in_gv = (GV*)(*PL_stack_sp--);
8efb3254 265 if (SvTYPE(PL_last_in_gv) != SVt_PVGV) {
1c846c1f 266 if (SvROK(PL_last_in_gv) && SvTYPE(SvRV(PL_last_in_gv)) == SVt_PVGV)
f5284f61 267 PL_last_in_gv = (GV*)SvRV(PL_last_in_gv);
8efb3254 268 else {
f5284f61
IZ
269 dSP;
270 XPUSHs((SV*)PL_last_in_gv);
271 PUTBACK;
cea2e8a9 272 pp_rv2gv();
f5284f61 273 PL_last_in_gv = (GV*)(*PL_stack_sp--);
f5284f61
IZ
274 }
275 }
a0d0e21e
LW
276 return do_readline();
277}
278
279PP(pp_eq)
280{
1c846c1f 281 djSP; tryAMAGICbinSET(eq,0);
a0d0e21e
LW
282 {
283 dPOPnv;
54310121 284 SETs(boolSV(TOPn == value));
a0d0e21e
LW
285 RETURN;
286 }
287}
288
289PP(pp_preinc)
290{
4e35701f 291 djSP;
68dc0745 292 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
d470f89e 293 DIE(aTHX_ PL_no_modify);
25da4f38 294 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
55497cff
PP
295 SvIVX(TOPs) != IV_MAX)
296 {
748a9306 297 ++SvIVX(TOPs);
55497cff 298 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
299 }
300 else
301 sv_inc(TOPs);
a0d0e21e
LW
302 SvSETMAGIC(TOPs);
303 return NORMAL;
304}
305
306PP(pp_or)
307{
4e35701f 308 djSP;
a0d0e21e
LW
309 if (SvTRUE(TOPs))
310 RETURN;
311 else {
312 --SP;
313 RETURNOP(cLOGOP->op_other);
314 }
315}
316
317PP(pp_add)
318{
1c846c1f 319 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
a0d0e21e 320 {
7a4c00b4 321 dPOPTOPnnrl_ul;
a0d0e21e
LW
322 SETn( left + right );
323 RETURN;
324 }
325}
326
327PP(pp_aelemfast)
328{
4e35701f 329 djSP;
1d7c1841 330 AV *av = GvAV(cGVOP_gv);
533c011a
NIS
331 U32 lval = PL_op->op_flags & OPf_MOD;
332 SV** svp = av_fetch(av, PL_op->op_private, lval);
3280af22 333 SV *sv = (svp ? *svp : &PL_sv_undef);
6ff81951 334 EXTEND(SP, 1);
be6c24e0
GS
335 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
336 sv = sv_mortalcopy(sv);
337 PUSHs(sv);
a0d0e21e
LW
338 RETURN;
339}
340
341PP(pp_join)
342{
4e35701f 343 djSP; dMARK; dTARGET;
a0d0e21e
LW
344 MARK++;
345 do_join(TARG, *MARK, MARK, SP);
346 SP = MARK;
347 SETs(TARG);
348 RETURN;
349}
350
351PP(pp_pushre)
352{
4e35701f 353 djSP;
44a8e56a
PP
354#ifdef DEBUGGING
355 /*
356 * We ass_u_me that LvTARGOFF() comes first, and that two STRLENs
357 * will be enough to hold an OP*.
358 */
359 SV* sv = sv_newmortal();
360 sv_upgrade(sv, SVt_PVLV);
361 LvTYPE(sv) = '/';
533c011a 362 Copy(&PL_op, &LvTARGOFF(sv), 1, OP*);
44a8e56a
PP
363 XPUSHs(sv);
364#else
6b88bc9c 365 XPUSHs((SV*)PL_op);
44a8e56a 366#endif
a0d0e21e
LW
367 RETURN;
368}
369
370/* Oversized hot code. */
371
372PP(pp_print)
373{
4e35701f 374 djSP; dMARK; dORIGMARK;
a0d0e21e
LW
375 GV *gv;
376 IO *io;
760ac839 377 register PerlIO *fp;
236988e4 378 MAGIC *mg;
2d8e6c8d 379 STRLEN n_a;
a0d0e21e 380
533c011a 381 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
382 gv = (GV*)*++MARK;
383 else
3280af22 384 gv = PL_defoutgv;
155aba94 385 if ((mg = SvTIED_mg((SV*)gv, 'q'))) {
01bb7c6d 386 had_magic:
68dc0745 387 if (MARK == ORIGMARK) {
1c846c1f 388 /* If using default handle then we need to make space to
a60c0954
NIS
389 * pass object as 1st arg, so move other args up ...
390 */
4352c267 391 MEXTEND(SP, 1);
68dc0745
PP
392 ++MARK;
393 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
394 ++SP;
395 }
396 PUSHMARK(MARK - 1);
33c27489 397 *MARK = SvTIED_obj((SV*)gv, mg);
68dc0745 398 PUTBACK;
236988e4 399 ENTER;
864dbfa3 400 call_method("PRINT", G_SCALAR);
236988e4
PP
401 LEAVE;
402 SPAGAIN;
68dc0745
PP
403 MARK = ORIGMARK + 1;
404 *MARK = *SP;
405 SP = MARK;
236988e4
PP
406 RETURN;
407 }
a0d0e21e 408 if (!(io = GvIO(gv))) {
01bb7c6d
DC
409 if ((GvEGV(gv)) && (mg = SvTIED_mg((SV*)GvEGV(gv),'q')))
410 goto had_magic;
2dd78f96
JH
411 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
412 report_evil_fh(gv, io, PL_op->op_type);
748a9306 413 SETERRNO(EBADF,RMS$_IFI);
a0d0e21e
LW
414 goto just_say_no;
415 }
416 else if (!(fp = IoOFP(io))) {
599cee73 417 if (ckWARN2(WARN_CLOSED, WARN_IO)) {
4c80c0b2
NC
418 if (IoIFP(io))
419 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
2dd78f96 420 else if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
bc37a18f 421 report_evil_fh(gv, io, PL_op->op_type);
a0d0e21e 422 }
748a9306 423 SETERRNO(EBADF,IoIFP(io)?RMS$_FAC:RMS$_IFI);
a0d0e21e
LW
424 goto just_say_no;
425 }
426 else {
427 MARK++;
7889fe52 428 if (PL_ofs_sv && SvOK(PL_ofs_sv)) {
a0d0e21e
LW
429 while (MARK <= SP) {
430 if (!do_print(*MARK, fp))
431 break;
432 MARK++;
433 if (MARK <= SP) {
7889fe52 434 if (!do_print(PL_ofs_sv, fp)) { /* $, */
a0d0e21e
LW
435 MARK--;
436 break;
437 }
438 }
439 }
440 }
441 else {
442 while (MARK <= SP) {
443 if (!do_print(*MARK, fp))
444 break;
445 MARK++;
446 }
447 }
448 if (MARK <= SP)
449 goto just_say_no;
450 else {
7889fe52
NIS
451 if (PL_ors_sv && SvOK(PL_ors_sv))
452 if (!do_print(PL_ors_sv, fp)) /* $\ */
a0d0e21e
LW
453 goto just_say_no;
454
455 if (IoFLAGS(io) & IOf_FLUSH)
760ac839 456 if (PerlIO_flush(fp) == EOF)
a0d0e21e
LW
457 goto just_say_no;
458 }
459 }
460 SP = ORIGMARK;
3280af22 461 PUSHs(&PL_sv_yes);
a0d0e21e
LW
462 RETURN;
463
464 just_say_no:
465 SP = ORIGMARK;
3280af22 466 PUSHs(&PL_sv_undef);
a0d0e21e
LW
467 RETURN;
468}
469
470PP(pp_rv2av)
471{
f5284f61 472 djSP; dTOPss;
a0d0e21e
LW
473 AV *av;
474
475 if (SvROK(sv)) {
476 wasref:
f5284f61
IZ
477 tryAMAGICunDEREF(to_av);
478
a0d0e21e
LW
479 av = (AV*)SvRV(sv);
480 if (SvTYPE(av) != SVt_PVAV)
cea2e8a9 481 DIE(aTHX_ "Not an ARRAY reference");
533c011a 482 if (PL_op->op_flags & OPf_REF) {
f5284f61 483 SETs((SV*)av);
a0d0e21e
LW
484 RETURN;
485 }
486 }
487 else {
488 if (SvTYPE(sv) == SVt_PVAV) {
489 av = (AV*)sv;
533c011a 490 if (PL_op->op_flags & OPf_REF) {
f5284f61 491 SETs((SV*)av);
a0d0e21e
LW
492 RETURN;
493 }
494 }
495 else {
67955e0c 496 GV *gv;
1c846c1f 497
a0d0e21e 498 if (SvTYPE(sv) != SVt_PVGV) {
748a9306 499 char *sym;
c9d5ac95 500 STRLEN len;
748a9306 501
a0d0e21e
LW
502 if (SvGMAGICAL(sv)) {
503 mg_get(sv);
504 if (SvROK(sv))
505 goto wasref;
506 }
507 if (!SvOK(sv)) {
533c011a
NIS
508 if (PL_op->op_flags & OPf_REF ||
509 PL_op->op_private & HINT_STRICT_REFS)
cea2e8a9 510 DIE(aTHX_ PL_no_usym, "an ARRAY");
599cee73 511 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 512 report_uninit();
f5284f61 513 if (GIMME == G_ARRAY) {
c2444246 514 (void)POPs;
4633a7c4 515 RETURN;
f5284f61
IZ
516 }
517 RETSETUNDEF;
a0d0e21e 518 }
c9d5ac95 519 sym = SvPV(sv,len);
35cd451c
GS
520 if ((PL_op->op_flags & OPf_SPECIAL) &&
521 !(PL_op->op_flags & OPf_MOD))
522 {
523 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PVAV);
c9d5ac95
GS
524 if (!gv
525 && (!is_gv_magical(sym,len,0)
526 || !(gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVAV))))
527 {
35cd451c 528 RETSETUNDEF;
c9d5ac95 529 }
35cd451c
GS
530 }
531 else {
532 if (PL_op->op_private & HINT_STRICT_REFS)
cea2e8a9 533 DIE(aTHX_ PL_no_symref, sym, "an ARRAY");
35cd451c
GS
534 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVAV);
535 }
536 }
537 else {
67955e0c 538 gv = (GV*)sv;
a0d0e21e 539 }
67955e0c 540 av = GvAVn(gv);
533c011a 541 if (PL_op->op_private & OPpLVAL_INTRO)
67955e0c 542 av = save_ary(gv);
533c011a 543 if (PL_op->op_flags & OPf_REF) {
f5284f61 544 SETs((SV*)av);
a0d0e21e
LW
545 RETURN;
546 }
547 }
548 }
549
550 if (GIMME == G_ARRAY) {
551 I32 maxarg = AvFILL(av) + 1;
c2444246 552 (void)POPs; /* XXXX May be optimized away? */
1c846c1f 553 EXTEND(SP, maxarg);
93965878 554 if (SvRMAGICAL(av)) {
1c846c1f 555 U32 i;
93965878
NIS
556 for (i=0; i < maxarg; i++) {
557 SV **svp = av_fetch(av, i, FALSE);
3280af22 558 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
93965878 559 }
1c846c1f 560 }
93965878
NIS
561 else {
562 Copy(AvARRAY(av), SP+1, maxarg, SV*);
563 }
a0d0e21e
LW
564 SP += maxarg;
565 }
566 else {
567 dTARGET;
568 I32 maxarg = AvFILL(av) + 1;
f5284f61 569 SETi(maxarg);
a0d0e21e
LW
570 }
571 RETURN;
572}
573
574PP(pp_rv2hv)
575{
4e35701f 576 djSP; dTOPss;
a0d0e21e
LW
577 HV *hv;
578
579 if (SvROK(sv)) {
580 wasref:
f5284f61
IZ
581 tryAMAGICunDEREF(to_hv);
582
a0d0e21e 583 hv = (HV*)SvRV(sv);
c750a3ec 584 if (SvTYPE(hv) != SVt_PVHV && SvTYPE(hv) != SVt_PVAV)
cea2e8a9 585 DIE(aTHX_ "Not a HASH reference");
533c011a 586 if (PL_op->op_flags & OPf_REF) {
a0d0e21e
LW
587 SETs((SV*)hv);
588 RETURN;
589 }
590 }
591 else {
c750a3ec 592 if (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV) {
a0d0e21e 593 hv = (HV*)sv;
533c011a 594 if (PL_op->op_flags & OPf_REF) {
a0d0e21e
LW
595 SETs((SV*)hv);
596 RETURN;
597 }
598 }
599 else {
67955e0c 600 GV *gv;
1c846c1f 601
a0d0e21e 602 if (SvTYPE(sv) != SVt_PVGV) {
748a9306 603 char *sym;
c9d5ac95 604 STRLEN len;
748a9306 605
a0d0e21e
LW
606 if (SvGMAGICAL(sv)) {
607 mg_get(sv);
608 if (SvROK(sv))
609 goto wasref;
610 }
611 if (!SvOK(sv)) {
533c011a
NIS
612 if (PL_op->op_flags & OPf_REF ||
613 PL_op->op_private & HINT_STRICT_REFS)
cea2e8a9 614 DIE(aTHX_ PL_no_usym, "a HASH");
599cee73 615 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 616 report_uninit();
4633a7c4
LW
617 if (GIMME == G_ARRAY) {
618 SP--;
619 RETURN;
620 }
a0d0e21e
LW
621 RETSETUNDEF;
622 }
c9d5ac95 623 sym = SvPV(sv,len);
35cd451c
GS
624 if ((PL_op->op_flags & OPf_SPECIAL) &&
625 !(PL_op->op_flags & OPf_MOD))
626 {
627 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PVHV);
c9d5ac95
GS
628 if (!gv
629 && (!is_gv_magical(sym,len,0)
630 || !(gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVHV))))
631 {
35cd451c 632 RETSETUNDEF;
c9d5ac95 633 }
35cd451c
GS
634 }
635 else {
636 if (PL_op->op_private & HINT_STRICT_REFS)
cea2e8a9 637 DIE(aTHX_ PL_no_symref, sym, "a HASH");
35cd451c
GS
638 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVHV);
639 }
640 }
641 else {
67955e0c 642 gv = (GV*)sv;
a0d0e21e 643 }
67955e0c 644 hv = GvHVn(gv);
533c011a 645 if (PL_op->op_private & OPpLVAL_INTRO)
67955e0c 646 hv = save_hash(gv);
533c011a 647 if (PL_op->op_flags & OPf_REF) {
a0d0e21e
LW
648 SETs((SV*)hv);
649 RETURN;
650 }
651 }
652 }
653
654 if (GIMME == G_ARRAY) { /* array wanted */
3280af22 655 *PL_stack_sp = (SV*)hv;
cea2e8a9 656 return do_kv();
a0d0e21e
LW
657 }
658 else {
659 dTARGET;
4b154ab5
GA
660 if (SvTYPE(hv) == SVt_PVAV)
661 hv = avhv_keys((AV*)hv);
b9c39e73 662 if (HvFILL(hv))
57def98f
JH
663 Perl_sv_setpvf(aTHX_ TARG, "%"IVdf"/%"IVdf,
664 (IV)HvFILL(hv), (IV)HvMAX(hv) + 1);
a0d0e21e
LW
665 else
666 sv_setiv(TARG, 0);
c750a3ec 667
a0d0e21e
LW
668 SETTARG;
669 RETURN;
670 }
671}
672
10c8fecd
GS
673STATIC int
674S_do_maybe_phash(pTHX_ AV *ary, SV **lelem, SV **firstlelem, SV **relem,
675 SV **lastrelem)
676{
677 OP *leftop;
10c8fecd
GS
678 I32 i;
679
680 leftop = ((BINOP*)PL_op)->op_last;
681 assert(leftop);
682 assert(leftop->op_type == OP_NULL && leftop->op_targ == OP_LIST);
683 leftop = ((LISTOP*)leftop)->op_first;
684 assert(leftop);
685 /* Skip PUSHMARK and each element already assigned to. */
686 for (i = lelem - firstlelem; i > 0; i--) {
687 leftop = leftop->op_sibling;
688 assert(leftop);
689 }
690 if (leftop->op_type != OP_RV2HV)
691 return 0;
692
693 /* pseudohash */
694 if (av_len(ary) > 0)
695 av_fill(ary, 0); /* clear all but the fields hash */
696 if (lastrelem >= relem) {
697 while (relem < lastrelem) { /* gobble up all the rest */
698 SV *tmpstr;
699 assert(relem[0]);
700 assert(relem[1]);
701 /* Avoid a memory leak when avhv_store_ent dies. */
702 tmpstr = sv_newmortal();
703 sv_setsv(tmpstr,relem[1]); /* value */
704 relem[1] = tmpstr;
705 if (avhv_store_ent(ary,relem[0],tmpstr,0))
d16e9ed9 706 (void)SvREFCNT_inc(tmpstr);
10c8fecd
GS
707 if (SvMAGICAL(ary) != 0 && SvSMAGICAL(tmpstr))
708 mg_set(tmpstr);
709 relem += 2;
710 TAINT_NOT;
711 }
712 }
713 if (relem == lastrelem)
714 return 1;
715 return 2;
716}
717
718STATIC void
719S_do_oddball(pTHX_ HV *hash, SV **relem, SV **firstrelem)
720{
721 if (*relem) {
722 SV *tmpstr;
723 if (ckWARN(WARN_MISC)) {
724 if (relem == firstrelem &&
725 SvROK(*relem) &&
726 (SvTYPE(SvRV(*relem)) == SVt_PVAV ||
727 SvTYPE(SvRV(*relem)) == SVt_PVHV))
728 {
729 Perl_warner(aTHX_ WARN_MISC,
730 "Reference found where even-sized list expected");
731 }
732 else
733 Perl_warner(aTHX_ WARN_MISC,
734 "Odd number of elements in hash assignment");
735 }
736 if (SvTYPE(hash) == SVt_PVAV) {
737 /* pseudohash */
738 tmpstr = sv_newmortal();
739 if (avhv_store_ent((AV*)hash,*relem,tmpstr,0))
d16e9ed9 740 (void)SvREFCNT_inc(tmpstr);
10c8fecd
GS
741 if (SvMAGICAL(hash) && SvSMAGICAL(tmpstr))
742 mg_set(tmpstr);
743 }
744 else {
745 HE *didstore;
746 tmpstr = NEWSV(29,0);
747 didstore = hv_store_ent(hash,*relem,tmpstr,0);
748 if (SvMAGICAL(hash)) {
749 if (SvSMAGICAL(tmpstr))
750 mg_set(tmpstr);
751 if (!didstore)
752 sv_2mortal(tmpstr);
753 }
754 }
755 TAINT_NOT;
756 }
757}
758
a0d0e21e
LW
759PP(pp_aassign)
760{
4e35701f 761 djSP;
3280af22
NIS
762 SV **lastlelem = PL_stack_sp;
763 SV **lastrelem = PL_stack_base + POPMARK;
764 SV **firstrelem = PL_stack_base + POPMARK + 1;
a0d0e21e
LW
765 SV **firstlelem = lastrelem + 1;
766
767 register SV **relem;
768 register SV **lelem;
769
770 register SV *sv;
771 register AV *ary;
772
54310121 773 I32 gimme;
a0d0e21e
LW
774 HV *hash;
775 I32 i;
776 int magic;
777
3280af22 778 PL_delaymagic = DM_DELAY; /* catch simultaneous items */
a0d0e21e
LW
779
780 /* If there's a common identifier on both sides we have to take
781 * special care that assigning the identifier on the left doesn't
782 * clobber a value on the right that's used later in the list.
783 */
10c8fecd 784 if (PL_op->op_private & (OPpASSIGN_COMMON)) {
cc5e57d2 785 EXTEND_MORTAL(lastrelem - firstrelem + 1);
10c8fecd
GS
786 for (relem = firstrelem; relem <= lastrelem; relem++) {
787 /*SUPPRESS 560*/
155aba94 788 if ((sv = *relem)) {
a1f49e72 789 TAINT_NOT; /* Each item is independent */
10c8fecd 790 *relem = sv_mortalcopy(sv);
a1f49e72 791 }
10c8fecd 792 }
a0d0e21e
LW
793 }
794
795 relem = firstrelem;
796 lelem = firstlelem;
797 ary = Null(AV*);
798 hash = Null(HV*);
10c8fecd 799
a0d0e21e 800 while (lelem <= lastlelem) {
bbce6d69 801 TAINT_NOT; /* Each item stands on its own, taintwise. */
a0d0e21e
LW
802 sv = *lelem++;
803 switch (SvTYPE(sv)) {
804 case SVt_PVAV:
805 ary = (AV*)sv;
748a9306 806 magic = SvMAGICAL(ary) != 0;
10c8fecd
GS
807 if (PL_op->op_private & OPpASSIGN_HASH) {
808 switch (do_maybe_phash(ary, lelem, firstlelem, relem,
809 lastrelem))
810 {
811 case 0:
812 goto normal_array;
813 case 1:
814 do_oddball((HV*)ary, relem, firstrelem);
815 }
816 relem = lastrelem + 1;
817 break;
818 }
819 normal_array:
a0d0e21e 820 av_clear(ary);
7e42bd57 821 av_extend(ary, lastrelem - relem);
a0d0e21e
LW
822 i = 0;
823 while (relem <= lastrelem) { /* gobble up all the rest */
5117ca91 824 SV **didstore;
a0d0e21e
LW
825 sv = NEWSV(28,0);
826 assert(*relem);
827 sv_setsv(sv,*relem);
828 *(relem++) = sv;
5117ca91
GS
829 didstore = av_store(ary,i++,sv);
830 if (magic) {
fb73857a
PP
831 if (SvSMAGICAL(sv))
832 mg_set(sv);
5117ca91 833 if (!didstore)
8127e0e3 834 sv_2mortal(sv);
5117ca91 835 }
bbce6d69 836 TAINT_NOT;
a0d0e21e
LW
837 }
838 break;
10c8fecd 839 case SVt_PVHV: { /* normal hash */
a0d0e21e
LW
840 SV *tmpstr;
841
842 hash = (HV*)sv;
748a9306 843 magic = SvMAGICAL(hash) != 0;
a0d0e21e
LW
844 hv_clear(hash);
845
846 while (relem < lastrelem) { /* gobble up all the rest */
5117ca91 847 HE *didstore;
4633a7c4 848 if (*relem)
a0d0e21e 849 sv = *(relem++);
4633a7c4 850 else
3280af22 851 sv = &PL_sv_no, relem++;
a0d0e21e
LW
852 tmpstr = NEWSV(29,0);
853 if (*relem)
854 sv_setsv(tmpstr,*relem); /* value */
855 *(relem++) = tmpstr;
5117ca91
GS
856 didstore = hv_store_ent(hash,sv,tmpstr,0);
857 if (magic) {
fb73857a
PP
858 if (SvSMAGICAL(tmpstr))
859 mg_set(tmpstr);
5117ca91 860 if (!didstore)
8127e0e3 861 sv_2mortal(tmpstr);
5117ca91 862 }
bbce6d69 863 TAINT_NOT;
8e07c86e 864 }
6a0deba8 865 if (relem == lastrelem) {
10c8fecd 866 do_oddball(hash, relem, firstrelem);
6a0deba8 867 relem++;
1930e939 868 }
a0d0e21e
LW
869 }
870 break;
871 default:
6fc92669
GS
872 if (SvIMMORTAL(sv)) {
873 if (relem <= lastrelem)
874 relem++;
875 break;
a0d0e21e
LW
876 }
877 if (relem <= lastrelem) {
878 sv_setsv(sv, *relem);
879 *(relem++) = sv;
880 }
881 else
3280af22 882 sv_setsv(sv, &PL_sv_undef);
a0d0e21e
LW
883 SvSETMAGIC(sv);
884 break;
885 }
886 }
3280af22
NIS
887 if (PL_delaymagic & ~DM_DELAY) {
888 if (PL_delaymagic & DM_UID) {
a0d0e21e 889#ifdef HAS_SETRESUID
b28d0864 890 (void)setresuid(PL_uid,PL_euid,(Uid_t)-1);
56febc5e
AD
891#else
892# ifdef HAS_SETREUID
3280af22 893 (void)setreuid(PL_uid,PL_euid);
56febc5e
AD
894# else
895# ifdef HAS_SETRUID
b28d0864
NIS
896 if ((PL_delaymagic & DM_UID) == DM_RUID) {
897 (void)setruid(PL_uid);
898 PL_delaymagic &= ~DM_RUID;
a0d0e21e 899 }
56febc5e
AD
900# endif /* HAS_SETRUID */
901# ifdef HAS_SETEUID
b28d0864
NIS
902 if ((PL_delaymagic & DM_UID) == DM_EUID) {
903 (void)seteuid(PL_uid);
904 PL_delaymagic &= ~DM_EUID;
a0d0e21e 905 }
56febc5e 906# endif /* HAS_SETEUID */
b28d0864
NIS
907 if (PL_delaymagic & DM_UID) {
908 if (PL_uid != PL_euid)
cea2e8a9 909 DIE(aTHX_ "No setreuid available");
b28d0864 910 (void)PerlProc_setuid(PL_uid);
a0d0e21e 911 }
56febc5e
AD
912# endif /* HAS_SETREUID */
913#endif /* HAS_SETRESUID */
d8eceb89
JH
914 PL_uid = PerlProc_getuid();
915 PL_euid = PerlProc_geteuid();
a0d0e21e 916 }
3280af22 917 if (PL_delaymagic & DM_GID) {
a0d0e21e 918#ifdef HAS_SETRESGID
b28d0864 919 (void)setresgid(PL_gid,PL_egid,(Gid_t)-1);
56febc5e
AD
920#else
921# ifdef HAS_SETREGID
3280af22 922 (void)setregid(PL_gid,PL_egid);
56febc5e
AD
923# else
924# ifdef HAS_SETRGID
b28d0864
NIS
925 if ((PL_delaymagic & DM_GID) == DM_RGID) {
926 (void)setrgid(PL_gid);
927 PL_delaymagic &= ~DM_RGID;
a0d0e21e 928 }
56febc5e
AD
929# endif /* HAS_SETRGID */
930# ifdef HAS_SETEGID
b28d0864
NIS
931 if ((PL_delaymagic & DM_GID) == DM_EGID) {
932 (void)setegid(PL_gid);
933 PL_delaymagic &= ~DM_EGID;
a0d0e21e 934 }
56febc5e 935# endif /* HAS_SETEGID */
b28d0864
NIS
936 if (PL_delaymagic & DM_GID) {
937 if (PL_gid != PL_egid)
cea2e8a9 938 DIE(aTHX_ "No setregid available");
b28d0864 939 (void)PerlProc_setgid(PL_gid);
a0d0e21e 940 }
56febc5e
AD
941# endif /* HAS_SETREGID */
942#endif /* HAS_SETRESGID */
d8eceb89
JH
943 PL_gid = PerlProc_getgid();
944 PL_egid = PerlProc_getegid();
a0d0e21e 945 }
3280af22 946 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
a0d0e21e 947 }
3280af22 948 PL_delaymagic = 0;
54310121
PP
949
950 gimme = GIMME_V;
951 if (gimme == G_VOID)
952 SP = firstrelem - 1;
953 else if (gimme == G_SCALAR) {
954 dTARGET;
955 SP = firstrelem;
956 SETi(lastrelem - firstrelem + 1);
957 }
958 else {
a0d0e21e
LW
959 if (ary || hash)
960 SP = lastrelem;
961 else
962 SP = firstrelem + (lastlelem - firstlelem);
0c8c7a05 963 lelem = firstlelem + (relem - firstrelem);
5f05dabc 964 while (relem <= SP)
3280af22 965 *relem++ = (lelem <= lastlelem) ? *lelem++ : &PL_sv_undef;
a0d0e21e 966 }
54310121 967 RETURN;
a0d0e21e
LW
968}
969
8782bef2
GB
970PP(pp_qr)
971{
972 djSP;
973 register PMOP *pm = cPMOP;
974 SV *rv = sv_newmortal();
57668c4d 975 SV *sv = newSVrv(rv, "Regexp");
8782bef2
GB
976 sv_magic(sv,(SV*)ReREFCNT_inc(pm->op_pmregexp),'r',0,0);
977 RETURNX(PUSHs(rv));
978}
979
a0d0e21e
LW
980PP(pp_match)
981{
4e35701f 982 djSP; dTARG;
a0d0e21e
LW
983 register PMOP *pm = cPMOP;
984 register char *t;
985 register char *s;
986 char *strend;
987 I32 global;
f722798b
IZ
988 I32 r_flags = REXEC_CHECKED;
989 char *truebase; /* Start of string */
d9f97599 990 register REGEXP *rx = pm->op_pmregexp;
b3eb6a9b 991 bool rxtainted;
a0d0e21e
LW
992 I32 gimme = GIMME;
993 STRLEN len;
748a9306 994 I32 minmatch = 0;
3280af22 995 I32 oldsave = PL_savestack_ix;
f86702cc 996 I32 update_minmatch = 1;
e60df1fa 997 I32 had_zerolen = 0;
a0d0e21e 998
533c011a 999 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
1000 TARG = POPs;
1001 else {
54b9620d 1002 TARG = DEFSV;
a0d0e21e
LW
1003 EXTEND(SP,1);
1004 }
c277df42 1005 PUTBACK; /* EVAL blocks need stack_sp. */
a0d0e21e
LW
1006 s = SvPV(TARG, len);
1007 strend = s + len;
1008 if (!s)
cea2e8a9 1009 DIE(aTHX_ "panic: do_match");
b3eb6a9b 1010 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
3280af22 1011 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
9212bbba 1012 TAINT_NOT;
a0d0e21e 1013
48c036b1 1014 if (pm->op_pmdynflags & PMdf_USED) {
c277df42 1015 failure:
a0d0e21e
LW
1016 if (gimme == G_ARRAY)
1017 RETURN;
1018 RETPUSHNO;
1019 }
1020
3280af22
NIS
1021 if (!rx->prelen && PL_curpm) {
1022 pm = PL_curpm;
d9f97599 1023 rx = pm->op_pmregexp;
a0d0e21e 1024 }
d9f97599 1025 if (rx->minlen > len) goto failure;
c277df42 1026
a0d0e21e 1027 truebase = t = s;
ad94a511
IZ
1028
1029 /* XXXX What part of this is needed with true \G-support? */
155aba94 1030 if ((global = pm->op_pmflags & PMf_GLOBAL)) {
cf93c79d 1031 rx->startp[0] = -1;
a0d0e21e
LW
1032 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
1033 MAGIC* mg = mg_find(TARG, 'g');
565764a8 1034 if (mg && mg->mg_len >= 0) {
b7a35066 1035 if (!(rx->reganch & ROPT_GPOS_SEEN))
1c846c1f 1036 rx->endp[0] = rx->startp[0] = mg->mg_len;
0ef3e39e
HS
1037 else if (rx->reganch & ROPT_ANCH_GPOS) {
1038 r_flags |= REXEC_IGNOREPOS;
1c846c1f 1039 rx->endp[0] = rx->startp[0] = mg->mg_len;
0ef3e39e 1040 }
748a9306 1041 minmatch = (mg->mg_flags & MGf_MINMATCH);
f86702cc 1042 update_minmatch = 0;
748a9306 1043 }
a0d0e21e
LW
1044 }
1045 }
0ef3e39e
HS
1046 if ((gimme != G_ARRAY && !global && rx->nparens)
1047 || SvTEMP(TARG) || PL_sawampersand)
1048 r_flags |= REXEC_COPY_STR;
1c846c1f 1049 if (SvSCREAM(TARG))
22e551b9
IZ
1050 r_flags |= REXEC_SCREAM;
1051
a0d0e21e 1052 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
3280af22
NIS
1053 SAVEINT(PL_multiline);
1054 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
a0d0e21e
LW
1055 }
1056
1057play_it_again:
cf93c79d
IZ
1058 if (global && rx->startp[0] != -1) {
1059 t = s = rx->endp[0] + truebase;
d9f97599 1060 if ((s + rx->minlen) > strend)
a0d0e21e 1061 goto nope;
f86702cc 1062 if (update_minmatch++)
e60df1fa 1063 minmatch = had_zerolen;
a0d0e21e 1064 }
f722798b
IZ
1065 if (rx->reganch & RE_USE_INTUIT) {
1066 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, s, strend, r_flags, NULL);
1067
1068 if (!s)
1069 goto nope;
1070 if ( (rx->reganch & ROPT_CHECK_ALL)
1c846c1f 1071 && !PL_sawampersand
f722798b
IZ
1072 && ((rx->reganch & ROPT_NOSCAN)
1073 || !((rx->reganch & RE_INTUIT_TAIL)
05b4157f
GS
1074 && (r_flags & REXEC_SCREAM)))
1075 && !SvROK(TARG)) /* Cannot trust since INTUIT cannot guess ^ */
f722798b 1076 goto yup;
a0d0e21e 1077 }
cea2e8a9 1078 if (CALLREGEXEC(aTHX_ rx, s, strend, truebase, minmatch, TARG, NULL, r_flags))
bbce6d69 1079 {
3280af22 1080 PL_curpm = pm;
a0d0e21e 1081 if (pm->op_pmflags & PMf_ONCE)
48c036b1 1082 pm->op_pmdynflags |= PMdf_USED;
a0d0e21e
LW
1083 goto gotcha;
1084 }
1085 else
1086 goto ret_no;
1087 /*NOTREACHED*/
1088
1089 gotcha:
72311751
GS
1090 if (rxtainted)
1091 RX_MATCH_TAINTED_on(rx);
1092 TAINT_IF(RX_MATCH_TAINTED(rx));
a0d0e21e
LW
1093 if (gimme == G_ARRAY) {
1094 I32 iters, i, len;
1095
d9f97599 1096 iters = rx->nparens;
a0d0e21e
LW
1097 if (global && !iters)
1098 i = 1;
1099 else
1100 i = 0;
c277df42 1101 SPAGAIN; /* EVAL blocks could move the stack. */
a0d0e21e 1102 EXTEND(SP, iters + i);
bbce6d69 1103 EXTEND_MORTAL(iters + i);
a0d0e21e
LW
1104 for (i = !i; i <= iters; i++) {
1105 PUSHs(sv_newmortal());
1106 /*SUPPRESS 560*/
cf93c79d
IZ
1107 if ((rx->startp[i] != -1) && rx->endp[i] != -1 ) {
1108 len = rx->endp[i] - rx->startp[i];
1109 s = rx->startp[i] + truebase;
a0d0e21e 1110 sv_setpvn(*SP, s, len);
a197cbdd
GS
1111 if ((pm->op_pmdynflags & PMdf_UTF8) && !IN_BYTE) {
1112 SvUTF8_on(*SP);
1113 sv_utf8_downgrade(*SP, TRUE);
1114 }
a0d0e21e
LW
1115 }
1116 }
1117 if (global) {
cf93c79d
IZ
1118 had_zerolen = (rx->startp[0] != -1
1119 && rx->startp[0] == rx->endp[0]);
c277df42 1120 PUTBACK; /* EVAL blocks may use stack */
cf93c79d 1121 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
a0d0e21e
LW
1122 goto play_it_again;
1123 }
f7e33566 1124 else if (!iters)
bde848c5 1125 XPUSHs(&PL_sv_yes);
4633a7c4 1126 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1127 RETURN;
1128 }
1129 else {
1130 if (global) {
1131 MAGIC* mg = 0;
1132 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
1133 mg = mg_find(TARG, 'g');
1134 if (!mg) {
1135 sv_magic(TARG, (SV*)0, 'g', Nullch, 0);
1136 mg = mg_find(TARG, 'g');
1137 }
cf93c79d
IZ
1138 if (rx->startp[0] != -1) {
1139 mg->mg_len = rx->endp[0];
d9f97599 1140 if (rx->startp[0] == rx->endp[0])
748a9306
LW
1141 mg->mg_flags |= MGf_MINMATCH;
1142 else
1143 mg->mg_flags &= ~MGf_MINMATCH;
1144 }
a0d0e21e 1145 }
4633a7c4 1146 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1147 RETPUSHYES;
1148 }
1149
f722798b 1150yup: /* Confirmed by INTUIT */
72311751
GS
1151 if (rxtainted)
1152 RX_MATCH_TAINTED_on(rx);
1153 TAINT_IF(RX_MATCH_TAINTED(rx));
3280af22 1154 PL_curpm = pm;
a0d0e21e 1155 if (pm->op_pmflags & PMf_ONCE)
48c036b1 1156 pm->op_pmdynflags |= PMdf_USED;
cf93c79d
IZ
1157 if (RX_MATCH_COPIED(rx))
1158 Safefree(rx->subbeg);
1159 RX_MATCH_COPIED_off(rx);
1160 rx->subbeg = Nullch;
a0d0e21e 1161 if (global) {
d9f97599 1162 rx->subbeg = truebase;
cf93c79d 1163 rx->startp[0] = s - truebase;
f722798b 1164 rx->endp[0] = s - truebase + rx->minlen;
cf93c79d 1165 rx->sublen = strend - truebase;
a0d0e21e 1166 goto gotcha;
1c846c1f 1167 }
3280af22 1168 if (PL_sawampersand) {
cf93c79d 1169 I32 off;
a0d0e21e 1170
cf93c79d
IZ
1171 rx->subbeg = savepvn(t, strend - t);
1172 rx->sublen = strend - t;
1173 RX_MATCH_COPIED_on(rx);
1174 off = rx->startp[0] = s - t;
f722798b 1175 rx->endp[0] = off + rx->minlen;
cf93c79d
IZ
1176 }
1177 else { /* startp/endp are used by @- @+. */
1178 rx->startp[0] = s - truebase;
f722798b 1179 rx->endp[0] = s - truebase + rx->minlen;
a0d0e21e 1180 }
fc19f8d0 1181 rx->nparens = rx->lastparen = 0; /* used by @- and @+ */
4633a7c4 1182 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1183 RETPUSHYES;
1184
1185nope:
a0d0e21e 1186ret_no:
c90c0ff4 1187 if (global && !(pm->op_pmflags & PMf_CONTINUE)) {
a0d0e21e
LW
1188 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
1189 MAGIC* mg = mg_find(TARG, 'g');
1190 if (mg)
565764a8 1191 mg->mg_len = -1;
a0d0e21e
LW
1192 }
1193 }
4633a7c4 1194 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1195 if (gimme == G_ARRAY)
1196 RETURN;
1197 RETPUSHNO;
1198}
1199
1200OP *
864dbfa3 1201Perl_do_readline(pTHX)
a0d0e21e
LW
1202{
1203 dSP; dTARGETSTACKED;
1204 register SV *sv;
1205 STRLEN tmplen = 0;
1206 STRLEN offset;
760ac839 1207 PerlIO *fp;
3280af22 1208 register IO *io = GvIO(PL_last_in_gv);
533c011a 1209 register I32 type = PL_op->op_type;
54310121 1210 I32 gimme = GIMME_V;
e79b0511 1211 MAGIC *mg;
a0d0e21e 1212
155aba94 1213 if ((mg = SvTIED_mg((SV*)PL_last_in_gv, 'q'))) {
e79b0511 1214 PUSHMARK(SP);
33c27489 1215 XPUSHs(SvTIED_obj((SV*)PL_last_in_gv, mg));
e79b0511
PP
1216 PUTBACK;
1217 ENTER;
864dbfa3 1218 call_method("READLINE", gimme);
e79b0511
PP
1219 LEAVE;
1220 SPAGAIN;
54310121
PP
1221 if (gimme == G_SCALAR)
1222 SvSetMagicSV_nosteal(TARG, TOPs);
e79b0511
PP
1223 RETURN;
1224 }
a0d0e21e
LW
1225 fp = Nullfp;
1226 if (io) {
1227 fp = IoIFP(io);
1228 if (!fp) {
1229 if (IoFLAGS(io) & IOf_ARGV) {
1230 if (IoFLAGS(io) & IOf_START) {
a0d0e21e 1231 IoLINES(io) = 0;
3280af22 1232 if (av_len(GvAVn(PL_last_in_gv)) < 0) {
1d7c1841 1233 IoFLAGS(io) &= ~IOf_START;
9d116dd7 1234 do_open(PL_last_in_gv,"-",1,FALSE,O_RDONLY,0,Nullfp);
3280af22
NIS
1235 sv_setpvn(GvSV(PL_last_in_gv), "-", 1);
1236 SvSETMAGIC(GvSV(PL_last_in_gv));
a2008d6d
GS
1237 fp = IoIFP(io);
1238 goto have_fp;
a0d0e21e
LW
1239 }
1240 }
3280af22 1241 fp = nextargv(PL_last_in_gv);
a0d0e21e 1242 if (!fp) { /* Note: fp != IoIFP(io) */
3280af22 1243 (void)do_close(PL_last_in_gv, FALSE); /* now it does*/
a0d0e21e
LW
1244 }
1245 }
1246 else if (type == OP_GLOB) {
1247 SV *tmpcmd = NEWSV(55, 0);
1248 SV *tmpglob = POPs;
1249 ENTER;
1250 SAVEFREESV(tmpcmd);
1251#ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
1252 /* since spawning off a process is a real performance hit */
1253 {
1254#include <descrip.h>
1255#include <lib$routines.h>
1256#include <nam.h>
1257#include <rmsdef.h>
1258 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
1259 char vmsspec[NAM$C_MAXRSS+1];
1260 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
1261 char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
1262 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
760ac839 1263 PerlIO *tmpfp;
a0d0e21e
LW
1264 STRLEN i;
1265 struct dsc$descriptor_s wilddsc
1266 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
1267 struct dsc$descriptor_vs rsdsc
1268 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
1269 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
1270
1271 /* We could find out if there's an explicit dev/dir or version
1272 by peeking into lib$find_file's internal context at
1273 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
1274 but that's unsupported, so I don't want to do it now and
1275 have it bite someone in the future. */
6ad3d225 1276 strcat(tmpfnam,PerlLIO_tmpnam(NULL));
a0d0e21e
LW
1277 cp = SvPV(tmpglob,i);
1278 for (; i; i--) {
1279 if (cp[i] == ';') hasver = 1;
1280 if (cp[i] == '.') {
1281 if (sts) hasver = 1;
1282 else sts = 1;
1283 }
1284 if (cp[i] == '/') {
1285 hasdir = isunix = 1;
1286 break;
748a9306 1287 }
a0d0e21e
LW
1288 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
1289 hasdir = 1;
1290 break;
1291 }
1292 }
760ac839 1293 if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
09b7f37c 1294 Stat_t st;
c6ed36e1 1295 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
09b7f37c
CB
1296 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
1297 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
a0d0e21e
LW
1298 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
1299 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
1300 &dfltdsc,NULL,NULL,NULL))&1)) {
1301 end = rstr + (unsigned long int) *rslt;
1302 if (!hasver) while (*end != ';') end--;
1303 *(end++) = '\n'; *end = '\0';
1304 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
1305 if (hasdir) {
f86702cc 1306 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
a0d0e21e
LW
1307 begin = rstr;
1308 }
1309 else {
1310 begin = end;
1311 while (*(--begin) != ']' && *begin != '>') ;
1312 ++begin;
1313 }
760ac839 1314 ok = (PerlIO_puts(tmpfp,begin) != EOF);
a0d0e21e
LW
1315 }
1316 if (cxt) (void)lib$find_file_end(&cxt);
748a9306
LW
1317 if (ok && sts != RMS$_NMF &&
1318 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
a0d0e21e 1319 if (!ok) {
c07a80fd
PP
1320 if (!(sts & 1)) {
1321 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
1322 }
760ac839 1323 PerlIO_close(tmpfp);
a0d0e21e
LW
1324 fp = NULL;
1325 }
1326 else {
760ac839 1327 PerlIO_rewind(tmpfp);
9f37169a 1328 IoTYPE(io) = IoTYPE_RDONLY;
a0d0e21e 1329 IoIFP(io) = fp = tmpfp;
1e422769 1330 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
a0d0e21e
LW
1331 }
1332 }
1333 }
1334#else /* !VMS */
1d7c1841
GS
1335#ifdef MACOS_TRADITIONAL
1336 sv_setpv(tmpcmd, "glob ");
1337 sv_catsv(tmpcmd, tmpglob);
1338 sv_catpv(tmpcmd, " |");
1339#else
a0d0e21e 1340#ifdef DOSISH
67955e0c
PP
1341#ifdef OS2
1342 sv_setpv(tmpcmd, "for a in ");
1343 sv_catsv(tmpcmd, tmpglob);
1344 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
1345#else
39e571d4
ML
1346#ifdef DJGPP
1347 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
1348 sv_catsv(tmpcmd, tmpglob);
1349#else
a0d0e21e
LW
1350 sv_setpv(tmpcmd, "perlglob ");
1351 sv_catsv(tmpcmd, tmpglob);
1352 sv_catpv(tmpcmd, " |");
39e571d4 1353#endif /* !DJGPP */
67955e0c
PP
1354#endif /* !OS2 */
1355#else /* !DOSISH */
1356#if defined(CSH)
3280af22 1357 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
a0d0e21e
LW
1358 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
1359 sv_catsv(tmpcmd, tmpglob);
16d20bd9 1360 sv_catpv(tmpcmd, "' 2>/dev/null |");
a0d0e21e
LW
1361#else
1362 sv_setpv(tmpcmd, "echo ");
1363 sv_catsv(tmpcmd, tmpglob);
1364#if 'z' - 'a' == 25
1365 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
1366#else
1367 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
1368#endif
1369#endif /* !CSH */
67955e0c 1370#endif /* !DOSISH */
1d7c1841 1371#endif /* MACOS_TRADITIONAL */
3280af22 1372 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
9d116dd7 1373 FALSE, O_RDONLY, 0, Nullfp);
a0d0e21e
LW
1374 fp = IoIFP(io);
1375#endif /* !VMS */
1376 LEAVE;
1377 }
1378 }
1379 else if (type == OP_GLOB)
1380 SP--;
af8c498a 1381 else if (ckWARN(WARN_IO) /* stdout/stderr or other write fh */
9f37169a 1382 && (IoTYPE(io) == IoTYPE_WRONLY || fp == PerlIO_stdout()
af8c498a 1383 || fp == PerlIO_stderr()))
4c80c0b2 1384 report_evil_fh(PL_last_in_gv, io, OP_phoney_OUTPUT_ONLY);
a0d0e21e
LW
1385 }
1386 if (!fp) {
790090df
HS
1387 if (ckWARN2(WARN_GLOB, WARN_CLOSED)
1388 && (!io || !(IoFLAGS(io) & IOf_START))) {
3f4520fe 1389 if (type == OP_GLOB)
e476b1b5 1390 Perl_warner(aTHX_ WARN_GLOB,
af8c498a
GS
1391 "glob failed (can't start child: %s)",
1392 Strerror(errno));
69282e91 1393 else
bc37a18f 1394 report_evil_fh(PL_last_in_gv, io, PL_op->op_type);
3f4520fe 1395 }
54310121 1396 if (gimme == G_SCALAR) {
a0d0e21e
LW
1397 (void)SvOK_off(TARG);
1398 PUSHTARG;
1399 }
1400 RETURN;
1401 }
a2008d6d 1402 have_fp:
54310121 1403 if (gimme == G_SCALAR) {
a0d0e21e 1404 sv = TARG;
9607fc9c
PP
1405 if (SvROK(sv))
1406 sv_unref(sv);
a0d0e21e
LW
1407 (void)SvUPGRADE(sv, SVt_PV);
1408 tmplen = SvLEN(sv); /* remember if already alloced */
1409 if (!tmplen)
1410 Sv_Grow(sv, 80); /* try short-buffering it */
1411 if (type == OP_RCATLINE)
1412 offset = SvCUR(sv);
1413 else
1414 offset = 0;
1415 }
54310121
PP
1416 else {
1417 sv = sv_2mortal(NEWSV(57, 80));
1418 offset = 0;
1419 }
fbad3eb5 1420
3887d568
AP
1421 /* This should not be marked tainted if the fp is marked clean */
1422#define MAYBE_TAINT_LINE(io, sv) \
1423 if (!(IoFLAGS(io) & IOf_UNTAINT)) { \
1424 TAINT; \
1425 SvTAINTED_on(sv); \
1426 }
1427
684bef36 1428/* delay EOF state for a snarfed empty file */
fbad3eb5 1429#define SNARF_EOF(gimme,rs,io,sv) \
684bef36 1430 (gimme != G_SCALAR || SvCUR(sv) \
b9fee9ba 1431 || (IoFLAGS(io) & IOf_NOLINE) || !RsSNARF(rs))
fbad3eb5 1432
a0d0e21e 1433 for (;;) {
fbad3eb5
GS
1434 if (!sv_gets(sv, fp, offset)
1435 && (type == OP_GLOB || SNARF_EOF(gimme, PL_rs, io, sv)))
1436 {
760ac839 1437 PerlIO_clearerr(fp);
a0d0e21e 1438 if (IoFLAGS(io) & IOf_ARGV) {
3280af22 1439 fp = nextargv(PL_last_in_gv);
a0d0e21e
LW
1440 if (fp)
1441 continue;
3280af22 1442 (void)do_close(PL_last_in_gv, FALSE);
a0d0e21e
LW
1443 }
1444 else if (type == OP_GLOB) {
e476b1b5
GS
1445 if (!do_close(PL_last_in_gv, FALSE) && ckWARN(WARN_GLOB)) {
1446 Perl_warner(aTHX_ WARN_GLOB,
4eb79ab5 1447 "glob failed (child exited with status %d%s)",
894356b3 1448 (int)(STATUS_CURRENT >> 8),
cf494569 1449 (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
4eb79ab5 1450 }
a0d0e21e 1451 }
54310121 1452 if (gimme == G_SCALAR) {
a0d0e21e
LW
1453 (void)SvOK_off(TARG);
1454 PUSHTARG;
1455 }
3887d568 1456 MAYBE_TAINT_LINE(io, sv);
a0d0e21e
LW
1457 RETURN;
1458 }
3887d568 1459 MAYBE_TAINT_LINE(io, sv);
a0d0e21e 1460 IoLINES(io)++;
b9fee9ba 1461 IoFLAGS(io) |= IOf_NOLINE;
71be2cbc 1462 SvSETMAGIC(sv);
a0d0e21e 1463 XPUSHs(sv);
a0d0e21e
LW
1464 if (type == OP_GLOB) {
1465 char *tmps;
1466
3280af22 1467 if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) {
c07a80fd 1468 tmps = SvEND(sv) - 1;
3280af22 1469 if (*tmps == *SvPVX(PL_rs)) {
c07a80fd
PP
1470 *tmps = '\0';
1471 SvCUR(sv)--;
1472 }
1473 }
a0d0e21e
LW
1474 for (tmps = SvPVX(sv); *tmps; tmps++)
1475 if (!isALPHA(*tmps) && !isDIGIT(*tmps) &&
1476 strchr("$&*(){}[]'\";\\|?<>~`", *tmps))
1477 break;
43384a1a 1478 if (*tmps && PerlLIO_lstat(SvPVX(sv), &PL_statbuf) < 0) {
a0d0e21e
LW
1479 (void)POPs; /* Unmatched wildcard? Chuck it... */
1480 continue;
1481 }
1482 }
54310121 1483 if (gimme == G_ARRAY) {
a0d0e21e
LW
1484 if (SvLEN(sv) - SvCUR(sv) > 20) {
1485 SvLEN_set(sv, SvCUR(sv)+1);
1486 Renew(SvPVX(sv), SvLEN(sv), char);
1487 }
1488 sv = sv_2mortal(NEWSV(58, 80));
1489 continue;
1490 }
54310121 1491 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
a0d0e21e
LW
1492 /* try to reclaim a bit of scalar space (only on 1st alloc) */
1493 if (SvCUR(sv) < 60)
1494 SvLEN_set(sv, 80);
1495 else
1496 SvLEN_set(sv, SvCUR(sv)+40); /* allow some slop */
1497 Renew(SvPVX(sv), SvLEN(sv), char);
1498 }
1499 RETURN;
1500 }
1501}
1502
1503PP(pp_enter)
1504{
4e35701f 1505 djSP;
c09156bb 1506 register PERL_CONTEXT *cx;
533c011a 1507 I32 gimme = OP_GIMME(PL_op, -1);
a0d0e21e 1508
54310121
PP
1509 if (gimme == -1) {
1510 if (cxstack_ix >= 0)
1511 gimme = cxstack[cxstack_ix].blk_gimme;
1512 else
1513 gimme = G_SCALAR;
1514 }
a0d0e21e
LW
1515
1516 ENTER;
1517
1518 SAVETMPS;
924508f0 1519 PUSHBLOCK(cx, CXt_BLOCK, SP);
a0d0e21e
LW
1520
1521 RETURN;
1522}
1523
1524PP(pp_helem)
1525{
4e35701f 1526 djSP;
760ac839 1527 HE* he;
ae77835f 1528 SV **svp;
a0d0e21e 1529 SV *keysv = POPs;
a0d0e21e 1530 HV *hv = (HV*)POPs;
533c011a
NIS
1531 U32 lval = PL_op->op_flags & OPf_MOD;
1532 U32 defer = PL_op->op_private & OPpLVAL_DEFER;
be6c24e0 1533 SV *sv;
1c846c1f 1534 U32 hash = (SvFAKE(keysv) && SvREADONLY(keysv)) ? SvUVX(keysv) : 0;
1f5346dc 1535 I32 preeminent;
a0d0e21e 1536
ae77835f 1537 if (SvTYPE(hv) == SVt_PVHV) {
1f5346dc
SC
1538 if (PL_op->op_private & OPpLVAL_INTRO)
1539 preeminent = SvRMAGICAL(hv) ? 1 : hv_exists_ent(hv, keysv, 0);
1c846c1f 1540 he = hv_fetch_ent(hv, keysv, lval && !defer, hash);
97fcbf96 1541 svp = he ? &HeVAL(he) : 0;
ae77835f
MB
1542 }
1543 else if (SvTYPE(hv) == SVt_PVAV) {
0ebe0038 1544 if (PL_op->op_private & OPpLVAL_INTRO)
cea2e8a9 1545 DIE(aTHX_ "Can't localize pseudo-hash element");
1c846c1f 1546 svp = avhv_fetch_ent((AV*)hv, keysv, lval && !defer, hash);
ae77835f 1547 }
c750a3ec 1548 else {
a0d0e21e 1549 RETPUSHUNDEF;
c750a3ec 1550 }
a0d0e21e 1551 if (lval) {
3280af22 1552 if (!svp || *svp == &PL_sv_undef) {
68dc0745
PP
1553 SV* lv;
1554 SV* key2;
2d8e6c8d
GS
1555 if (!defer) {
1556 STRLEN n_a;
cea2e8a9 1557 DIE(aTHX_ PL_no_helem, SvPV(keysv, n_a));
2d8e6c8d 1558 }
68dc0745
PP
1559 lv = sv_newmortal();
1560 sv_upgrade(lv, SVt_PVLV);
1561 LvTYPE(lv) = 'y';
1562 sv_magic(lv, key2 = newSVsv(keysv), 'y', Nullch, 0);
1563 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1564 LvTARG(lv) = SvREFCNT_inc(hv);
1565 LvTARGLEN(lv) = 1;
1566 PUSHs(lv);
1567 RETURN;
1568 }
533c011a 1569 if (PL_op->op_private & OPpLVAL_INTRO) {
ae77835f 1570 if (HvNAME(hv) && isGV(*svp))
533c011a 1571 save_gp((GV*)*svp, !(PL_op->op_flags & OPf_SPECIAL));
1f5346dc
SC
1572 else {
1573 if (!preeminent) {
1574 STRLEN keylen;
1575 char *key = SvPV(keysv, keylen);
1576 save_delete(hv, key, keylen);
1577 } else
1578 save_helem(hv, keysv, svp);
1579 }
5f05dabc 1580 }
533c011a
NIS
1581 else if (PL_op->op_private & OPpDEREF)
1582 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
a0d0e21e 1583 }
3280af22 1584 sv = (svp ? *svp : &PL_sv_undef);
be6c24e0
GS
1585 /* This makes C<local $tied{foo} = $tied{foo}> possible.
1586 * Pushing the magical RHS on to the stack is useless, since
1587 * that magic is soon destined to be misled by the local(),
1588 * and thus the later pp_sassign() will fail to mg_get() the
1589 * old value. This should also cure problems with delayed
1590 * mg_get()s. GSAR 98-07-03 */
1591 if (!lval && SvGMAGICAL(sv))
1592 sv = sv_mortalcopy(sv);
1593 PUSHs(sv);
a0d0e21e
LW
1594 RETURN;
1595}
1596
1597PP(pp_leave)
1598{
4e35701f 1599 djSP;
c09156bb 1600 register PERL_CONTEXT *cx;
a0d0e21e
LW
1601 register SV **mark;
1602 SV **newsp;
1603 PMOP *newpm;
1604 I32 gimme;
1605
533c011a 1606 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e 1607 cx = &cxstack[cxstack_ix];
3280af22 1608 cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */
a0d0e21e
LW
1609 }
1610
1611 POPBLOCK(cx,newpm);
1612
533c011a 1613 gimme = OP_GIMME(PL_op, -1);
54310121
PP
1614 if (gimme == -1) {
1615 if (cxstack_ix >= 0)
1616 gimme = cxstack[cxstack_ix].blk_gimme;
1617 else
1618 gimme = G_SCALAR;
1619 }
a0d0e21e 1620
a1f49e72 1621 TAINT_NOT;
54310121
PP
1622 if (gimme == G_VOID)
1623 SP = newsp;
1624 else if (gimme == G_SCALAR) {
1625 MARK = newsp + 1;
1626 if (MARK <= SP)
1627 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1628 *MARK = TOPs;
1629 else
1630 *MARK = sv_mortalcopy(TOPs);
a0d0e21e 1631 else {
54310121 1632 MEXTEND(mark,0);
3280af22 1633 *MARK = &PL_sv_undef;
a0d0e21e 1634 }
54310121 1635 SP = MARK;
a0d0e21e 1636 }
54310121 1637 else if (gimme == G_ARRAY) {
a1f49e72
CS
1638 /* in case LEAVE wipes old return values */
1639 for (mark = newsp + 1; mark <= SP; mark++) {
1640 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
a0d0e21e 1641 *mark = sv_mortalcopy(*mark);
a1f49e72
CS
1642 TAINT_NOT; /* Each item is independent */
1643 }
1644 }
a0d0e21e 1645 }
3280af22 1646 PL_curpm = newpm; /* Don't pop $1 et al till now */
a0d0e21e
LW
1647
1648 LEAVE;
1649
1650 RETURN;
1651}
1652
1653PP(pp_iter)
1654{
4e35701f 1655 djSP;
c09156bb 1656 register PERL_CONTEXT *cx;
5f05dabc 1657 SV* sv;
4633a7c4 1658 AV* av;
1d7c1841 1659 SV **itersvp;
a0d0e21e 1660
924508f0 1661 EXTEND(SP, 1);
a0d0e21e 1662 cx = &cxstack[cxstack_ix];
6b35e009 1663 if (CxTYPE(cx) != CXt_LOOP)
cea2e8a9 1664 DIE(aTHX_ "panic: pp_iter");
a0d0e21e 1665
1d7c1841 1666 itersvp = CxITERVAR(cx);
4633a7c4 1667 av = cx->blk_loop.iterary;
89ea2908
GA
1668 if (SvTYPE(av) != SVt_PVAV) {
1669 /* iterate ($min .. $max) */
1670 if (cx->blk_loop.iterlval) {
1671 /* string increment */
1672 register SV* cur = cx->blk_loop.iterlval;
1673 STRLEN maxlen;
1674 char *max = SvPV((SV*)av, maxlen);
1675 if (!SvNIOK(cur) && SvCUR(cur) <= maxlen) {
eaa5c2d6 1676#ifndef USE_THREADS /* don't risk potential race */
1d7c1841 1677 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
eaa5c2d6 1678 /* safe to reuse old SV */
1d7c1841 1679 sv_setsv(*itersvp, cur);
eaa5c2d6 1680 }
1c846c1f 1681 else
eaa5c2d6
GA
1682#endif
1683 {
1684 /* we need a fresh SV every time so that loop body sees a
1685 * completely new SV for closures/references to work as
1686 * they used to */
1d7c1841
GS
1687 SvREFCNT_dec(*itersvp);
1688 *itersvp = newSVsv(cur);
eaa5c2d6 1689 }
89ea2908
GA
1690 if (strEQ(SvPVX(cur), max))
1691 sv_setiv(cur, 0); /* terminate next time */
1692 else
1693 sv_inc(cur);
1694 RETPUSHYES;
1695 }
1696 RETPUSHNO;
1697 }
1698 /* integer increment */
1699 if (cx->blk_loop.iterix > cx->blk_loop.itermax)
1700 RETPUSHNO;
7f61b687 1701
eaa5c2d6 1702#ifndef USE_THREADS /* don't risk potential race */
1d7c1841 1703 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
eaa5c2d6 1704 /* safe to reuse old SV */
1d7c1841 1705 sv_setiv(*itersvp, cx->blk_loop.iterix++);
eaa5c2d6 1706 }
1c846c1f 1707 else
eaa5c2d6
GA
1708#endif
1709 {
1710 /* we need a fresh SV every time so that loop body sees a
1711 * completely new SV for closures/references to work as they
1712 * used to */
1d7c1841
GS
1713 SvREFCNT_dec(*itersvp);
1714 *itersvp = newSViv(cx->blk_loop.iterix++);
eaa5c2d6 1715 }
89ea2908
GA
1716 RETPUSHYES;
1717 }
1718
1719 /* iterate array */
3280af22 1720 if (cx->blk_loop.iterix >= (av == PL_curstack ? cx->blk_oldsp : AvFILL(av)))
4633a7c4 1721 RETPUSHNO;
a0d0e21e 1722
1d7c1841 1723 SvREFCNT_dec(*itersvp);
a0d0e21e 1724
155aba94 1725 if ((sv = SvMAGICAL(av)
1c846c1f 1726 ? *av_fetch(av, ++cx->blk_loop.iterix, FALSE)
155aba94 1727 : AvARRAY(av)[++cx->blk_loop.iterix]))
a0d0e21e 1728 SvTEMP_off(sv);
a0d0e21e 1729 else
3280af22
NIS
1730 sv = &PL_sv_undef;
1731 if (av != PL_curstack && SvIMMORTAL(sv)) {
5f05dabc 1732 SV *lv = cx->blk_loop.iterlval;
71be2cbc
PP
1733 if (lv && SvREFCNT(lv) > 1) {
1734 SvREFCNT_dec(lv);
1735 lv = Nullsv;
1736 }
5f05dabc
PP
1737 if (lv)
1738 SvREFCNT_dec(LvTARG(lv));
1739 else {
68dc0745 1740 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
5f05dabc 1741 sv_upgrade(lv, SVt_PVLV);
5f05dabc 1742 LvTYPE(lv) = 'y';
68dc0745 1743 sv_magic(lv, Nullsv, 'y', Nullch, 0);
5f05dabc
PP
1744 }
1745 LvTARG(lv) = SvREFCNT_inc(av);
1746 LvTARGOFF(lv) = cx->blk_loop.iterix;
42718184 1747 LvTARGLEN(lv) = (STRLEN)UV_MAX;
5f05dabc
PP
1748 sv = (SV*)lv;
1749 }
a0d0e21e 1750
1d7c1841 1751 *itersvp = SvREFCNT_inc(sv);
a0d0e21e
LW
1752 RETPUSHYES;
1753}
1754
1755PP(pp_subst)
1756{
4e35701f 1757 djSP; dTARG;
a0d0e21e
LW
1758 register PMOP *pm = cPMOP;
1759 PMOP *rpm = pm;
1760 register SV *dstr;
1761 register char *s;
1762 char *strend;
1763 register char *m;
1764 char *c;
1765 register char *d;
1766 STRLEN clen;
1767 I32 iters = 0;
1768 I32 maxiters;
1769 register I32 i;
1770 bool once;
71be2cbc 1771 bool rxtainted;
a0d0e21e 1772 char *orig;
22e551b9 1773 I32 r_flags;
d9f97599 1774 register REGEXP *rx = pm->op_pmregexp;
a0d0e21e
LW
1775 STRLEN len;
1776 int force_on_match = 0;
3280af22 1777 I32 oldsave = PL_savestack_ix;
a0d0e21e 1778
5cd24f17
PP
1779 /* known replacement string? */
1780 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
533c011a 1781 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
1782 TARG = POPs;
1783 else {
54b9620d 1784 TARG = DEFSV;
a0d0e21e 1785 EXTEND(SP,1);
1c846c1f 1786 }
eca06228
NIS
1787 if (SvFAKE(TARG) && SvREADONLY(TARG))
1788 sv_force_normal(TARG);
68dc0745
PP
1789 if (SvREADONLY(TARG)
1790 || (SvTYPE(TARG) > SVt_PVLV
1791 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG))))
d470f89e 1792 DIE(aTHX_ PL_no_modify);
8ec5e241
NIS
1793 PUTBACK;
1794
a0d0e21e 1795 s = SvPV(TARG, len);
68dc0745 1796 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
a0d0e21e 1797 force_on_match = 1;
b3eb6a9b 1798 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
3280af22
NIS
1799 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
1800 if (PL_tainted)
b3eb6a9b 1801 rxtainted |= 2;
9212bbba 1802 TAINT_NOT;
a0d0e21e
LW
1803
1804 force_it:
1805 if (!pm || !s)
cea2e8a9 1806 DIE(aTHX_ "panic: do_subst");
a0d0e21e
LW
1807
1808 strend = s + len;
1c846c1f 1809 maxiters = 2*(strend - s) + 10; /* We can match twice at each
2beec16e
IZ
1810 position, once with zero-length,
1811 second time with non-zero. */
a0d0e21e 1812
3280af22
NIS
1813 if (!rx->prelen && PL_curpm) {
1814 pm = PL_curpm;
d9f97599 1815 rx = pm->op_pmregexp;
a0d0e21e 1816 }
22e551b9 1817 r_flags = (rx->nparens || SvTEMP(TARG) || PL_sawampersand)
9d080a66 1818 ? REXEC_COPY_STR : 0;
f722798b 1819 if (SvSCREAM(TARG))
22e551b9 1820 r_flags |= REXEC_SCREAM;
a0d0e21e 1821 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
3280af22
NIS
1822 SAVEINT(PL_multiline);
1823 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
a0d0e21e
LW
1824 }
1825 orig = m = s;
f722798b
IZ
1826 if (rx->reganch & RE_USE_INTUIT) {
1827 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, s, strend, r_flags, NULL);
1828
1829 if (!s)
1830 goto nope;
1831 /* How to do it in subst? */
1832/* if ( (rx->reganch & ROPT_CHECK_ALL)
1c846c1f 1833 && !PL_sawampersand
f722798b
IZ
1834 && ((rx->reganch & ROPT_NOSCAN)
1835 || !((rx->reganch & RE_INTUIT_TAIL)
1836 && (r_flags & REXEC_SCREAM))))
1837 goto yup;
1838*/
a0d0e21e 1839 }
71be2cbc
PP
1840
1841 /* only replace once? */
a0d0e21e 1842 once = !(rpm->op_pmflags & PMf_GLOBAL);
71be2cbc
PP
1843
1844 /* known replacement string? */
5cd24f17 1845 c = dstr ? SvPV(dstr, clen) : Nullch;
71be2cbc
PP
1846
1847 /* can do inplace substitution? */
22e551b9 1848 if (c && clen <= rx->minlen && (once || !(r_flags & REXEC_COPY_STR))
d9f97599 1849 && !(rx->reganch & ROPT_LOOKBEHIND_SEEN)) {
f722798b
IZ
1850 if (!CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
1851 r_flags | REXEC_CHECKED))
1852 {
8ec5e241 1853 SPAGAIN;
3280af22 1854 PUSHs(&PL_sv_no);
71be2cbc
PP
1855 LEAVE_SCOPE(oldsave);
1856 RETURN;
1857 }
1858 if (force_on_match) {
1859 force_on_match = 0;
1860 s = SvPV_force(TARG, len);
1861 goto force_it;
1862 }
71be2cbc 1863 d = s;
3280af22 1864 PL_curpm = pm;
71be2cbc
PP
1865 SvSCREAM_off(TARG); /* disable possible screamer */
1866 if (once) {
48c036b1 1867 rxtainted |= RX_MATCH_TAINTED(rx);
cf93c79d
IZ
1868 m = orig + rx->startp[0];
1869 d = orig + rx->endp[0];
71be2cbc
PP
1870 s = orig;
1871 if (m - s > strend - d) { /* faster to shorten from end */
1872 if (clen) {
1873 Copy(c, m, clen, char);
1874 m += clen;
a0d0e21e 1875 }
71be2cbc
PP
1876 i = strend - d;
1877 if (i > 0) {
1878 Move(d, m, i, char);
1879 m += i;
a0d0e21e 1880 }
71be2cbc
PP
1881 *m = '\0';
1882 SvCUR_set(TARG, m - s);
1883 }
1884 /*SUPPRESS 560*/
155aba94 1885 else if ((i = m - s)) { /* faster from front */
71be2cbc
PP
1886 d -= clen;
1887 m = d;
1888 sv_chop(TARG, d-i);
1889 s += i;
1890 while (i--)
1891 *--d = *--s;
1892 if (clen)
1893 Copy(c, m, clen, char);
1894 }
1895 else if (clen) {
1896 d -= clen;
1897 sv_chop(TARG, d);
1898 Copy(c, d, clen, char);
1899 }
1900 else {
1901 sv_chop(TARG, d);
1902 }
48c036b1 1903 TAINT_IF(rxtainted & 1);
8ec5e241 1904 SPAGAIN;
3280af22 1905 PUSHs(&PL_sv_yes);
71be2cbc
PP
1906 }
1907 else {
71be2cbc
PP
1908 do {
1909 if (iters++ > maxiters)
cea2e8a9 1910 DIE(aTHX_ "Substitution loop");
d9f97599 1911 rxtainted |= RX_MATCH_TAINTED(rx);
cf93c79d 1912 m = rx->startp[0] + orig;
71be2cbc 1913 /*SUPPRESS 560*/
155aba94 1914 if ((i = m - s)) {
71be2cbc
PP
1915 if (s != d)
1916 Move(s, d, i, char);
1917 d += i;
a0d0e21e 1918 }
71be2cbc
PP
1919 if (clen) {
1920 Copy(c, d, clen, char);
1921 d += clen;
1922 }
cf93c79d 1923 s = rx->endp[0] + orig;
cea2e8a9 1924 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
f722798b
IZ
1925 TARG, NULL,
1926 /* don't match same null twice */
1927 REXEC_NOT_FIRST|REXEC_IGNOREPOS));
71be2cbc
PP
1928 if (s != d) {
1929 i = strend - s;
1930 SvCUR_set(TARG, d - SvPVX(TARG) + i);
1931 Move(s, d, i+1, char); /* include the NUL */
a0d0e21e 1932 }
48c036b1 1933 TAINT_IF(rxtainted & 1);
8ec5e241 1934 SPAGAIN;
71be2cbc 1935 PUSHs(sv_2mortal(newSViv((I32)iters)));
a0d0e21e 1936 }
80b498e0 1937 (void)SvPOK_only_UTF8(TARG);
48c036b1 1938 TAINT_IF(rxtainted);
8ec5e241
NIS
1939 if (SvSMAGICAL(TARG)) {
1940 PUTBACK;
1941 mg_set(TARG);
1942 SPAGAIN;
1943 }
9212bbba 1944 SvTAINT(TARG);
71be2cbc
PP
1945 LEAVE_SCOPE(oldsave);
1946 RETURN;
a0d0e21e 1947 }
71be2cbc 1948
f722798b
IZ
1949 if (CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
1950 r_flags | REXEC_CHECKED))
1951 {
a0d0e21e
LW
1952 if (force_on_match) {
1953 force_on_match = 0;
1954 s = SvPV_force(TARG, len);
1955 goto force_it;
1956 }
48c036b1 1957 rxtainted |= RX_MATCH_TAINTED(rx);
8ec5e241 1958 dstr = NEWSV(25, len);
a0d0e21e 1959 sv_setpvn(dstr, m, s-m);
3280af22 1960 PL_curpm = pm;
a0d0e21e 1961 if (!c) {
c09156bb 1962 register PERL_CONTEXT *cx;
8ec5e241 1963 SPAGAIN;
a0d0e21e
LW
1964 PUSHSUBST(cx);
1965 RETURNOP(cPMOP->op_pmreplroot);
1966 }
cf93c79d 1967 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
a0d0e21e
LW
1968 do {
1969 if (iters++ > maxiters)
cea2e8a9 1970 DIE(aTHX_ "Substitution loop");
d9f97599 1971 rxtainted |= RX_MATCH_TAINTED(rx);
cf93c79d 1972 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
a0d0e21e
LW
1973 m = s;
1974 s = orig;
cf93c79d 1975 orig = rx->subbeg;
a0d0e21e
LW
1976 s = orig + (m - s);
1977 strend = s + (strend - m);
1978 }
cf93c79d 1979 m = rx->startp[0] + orig;
a0d0e21e 1980 sv_catpvn(dstr, s, m-s);
cf93c79d 1981 s = rx->endp[0] + orig;
a0d0e21e
LW
1982 if (clen)
1983 sv_catpvn(dstr, c, clen);
1984 if (once)
1985 break;
cea2e8a9 1986 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m, TARG, NULL, r_flags));
a0d0e21e 1987 sv_catpvn(dstr, s, strend - s);
748a9306 1988
4633a7c4 1989 (void)SvOOK_off(TARG);
cb0b1708 1990 Safefree(SvPVX(TARG));
748a9306
LW
1991 SvPVX(TARG) = SvPVX(dstr);
1992 SvCUR_set(TARG, SvCUR(dstr));
1993 SvLEN_set(TARG, SvLEN(dstr));
1994 SvPVX(dstr) = 0;
1995 sv_free(dstr);
1996
48c036b1 1997 TAINT_IF(rxtainted & 1);
f878fbec 1998 SPAGAIN;
48c036b1
GS
1999 PUSHs(sv_2mortal(newSViv((I32)iters)));
2000
a0d0e21e 2001 (void)SvPOK_only(TARG);
48c036b1 2002 TAINT_IF(rxtainted);
a0d0e21e 2003 SvSETMAGIC(TARG);
9212bbba 2004 SvTAINT(TARG);
4633a7c4 2005 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
2006 RETURN;
2007 }
5cd24f17 2008 goto ret_no;
a0d0e21e
LW
2009
2010nope:
1c846c1f 2011ret_no:
8ec5e241 2012 SPAGAIN;
3280af22 2013 PUSHs(&PL_sv_no);
4633a7c4 2014 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
2015 RETURN;
2016}
2017
2018PP(pp_grepwhile)
2019{
4e35701f 2020 djSP;
a0d0e21e
LW
2021
2022 if (SvTRUEx(POPs))
3280af22
NIS
2023 PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr];
2024 ++*PL_markstack_ptr;
a0d0e21e
LW
2025 LEAVE; /* exit inner scope */
2026
2027 /* All done yet? */
3280af22 2028 if (PL_stack_base + *PL_markstack_ptr > SP) {
a0d0e21e 2029 I32 items;
54310121 2030 I32 gimme = GIMME_V;
a0d0e21e
LW
2031
2032 LEAVE; /* exit outer scope */
2033 (void)POPMARK; /* pop src */
3280af22 2034 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
a0d0e21e 2035 (void)POPMARK; /* pop dst */
3280af22 2036 SP = PL_stack_base + POPMARK; /* pop original mark */
54310121 2037 if (gimme == G_SCALAR) {
a0d0e21e
LW
2038 dTARGET;
2039 XPUSHi(items);
a0d0e21e 2040 }
54310121
PP
2041 else if (gimme == G_ARRAY)
2042 SP += items;
a0d0e21e
LW
2043 RETURN;
2044 }
2045 else {
2046 SV *src;
2047
2048 ENTER; /* enter inner scope */
1d7c1841 2049 SAVEVPTR(PL_curpm);
a0d0e21e 2050
3280af22 2051 src = PL_stack_base[*PL_markstack_ptr];
a0d0e21e 2052 SvTEMP_off(src);
54b9620d 2053 DEFSV = src;
a0d0e21e
LW
2054
2055 RETURNOP(cLOGOP->op_other);
2056 }
2057}
2058
2059PP(pp_leavesub)
2060{
4e35701f 2061 djSP;
a0d0e21e
LW
2062 SV **mark;
2063 SV **newsp;
2064 PMOP *newpm;
2065 I32 gimme;
c09156bb 2066 register PERL_CONTEXT *cx;
b0d9ce38 2067 SV *sv;
a0d0e21e
LW
2068
2069 POPBLOCK(cx,newpm);
1c846c1f 2070
a1f49e72 2071 TAINT_NOT;
a0d0e21e
LW
2072 if (gimme == G_SCALAR) {
2073 MARK = newsp + 1;
a29cdaf0 2074 if (MARK <= SP) {
a8bba7fa 2075 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
a29cdaf0
IZ
2076 if (SvTEMP(TOPs)) {
2077 *MARK = SvREFCNT_inc(TOPs);
2078 FREETMPS;
2079 sv_2mortal(*MARK);
cd06dffe
GS
2080 }
2081 else {
959e3673 2082 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
a29cdaf0 2083 FREETMPS;
959e3673
GS
2084 *MARK = sv_mortalcopy(sv);
2085 SvREFCNT_dec(sv);
a29cdaf0 2086 }
cd06dffe
GS
2087 }
2088 else
a29cdaf0 2089 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
cd06dffe
GS
2090 }
2091 else {
f86702cc 2092 MEXTEND(MARK, 0);
3280af22 2093 *MARK = &PL_sv_undef;
a0d0e21e
LW
2094 }
2095 SP = MARK;
2096 }
54310121 2097 else if (gimme == G_ARRAY) {
f86702cc 2098 for (MARK = newsp + 1; MARK <= SP; MARK++) {
a1f49e72 2099 if (!SvTEMP(*MARK)) {
f86702cc 2100 *MARK = sv_mortalcopy(*MARK);
a1f49e72
CS
2101 TAINT_NOT; /* Each item is independent */
2102 }
f86702cc 2103 }
a0d0e21e 2104 }
f86702cc 2105 PUTBACK;
1c846c1f 2106
b0d9ce38 2107 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
3280af22 2108 PL_curpm = newpm; /* ... and pop $1 et al */
a0d0e21e
LW
2109
2110 LEAVE;
b0d9ce38 2111 LEAVESUB(sv);
a0d0e21e
LW
2112 return pop_return();
2113}
2114
cd06dffe
GS
2115/* This duplicates the above code because the above code must not
2116 * get any slower by more conditions */
2117PP(pp_leavesublv)
2118{
2119 djSP;
2120 SV **mark;
2121 SV **newsp;
2122 PMOP *newpm;
2123 I32 gimme;
2124 register PERL_CONTEXT *cx;
b0d9ce38 2125 SV *sv;
cd06dffe
GS
2126
2127 POPBLOCK(cx,newpm);
1c846c1f 2128
cd06dffe
GS
2129 TAINT_NOT;
2130
2131 if (cx->blk_sub.lval & OPpENTERSUB_INARGS) {
2132 /* We are an argument to a function or grep().
2133 * This kind of lvalueness was legal before lvalue
2134 * subroutines too, so be backward compatible:
2135 * cannot report errors. */
2136
2137 /* Scalar context *is* possible, on the LHS of -> only,
2138 * as in f()->meth(). But this is not an lvalue. */
2139 if (gimme == G_SCALAR)
2140 goto temporise;
2141 if (gimme == G_ARRAY) {
a8bba7fa 2142 if (!CvLVALUE(cx->blk_sub.cv))
cd06dffe
GS
2143 goto temporise_array;
2144 EXTEND_MORTAL(SP - newsp);
2145 for (mark = newsp + 1; mark <= SP; mark++) {
2146 if (SvTEMP(*mark))
2147 /* empty */ ;
2148 else if (SvFLAGS(*mark) & (SVs_PADTMP | SVf_READONLY))
2149 *mark = sv_mortalcopy(*mark);
2150 else {
2151 /* Can be a localized value subject to deletion. */
2152 PL_tmps_stack[++PL_tmps_ix] = *mark;
e1f15930 2153 (void)SvREFCNT_inc(*mark);
cd06dffe
GS
2154 }
2155 }
2156 }
2157 }
2158 else if (cx->blk_sub.lval) { /* Leave it as it is if we can. */
2159 /* Here we go for robustness, not for speed, so we change all
2160 * the refcounts so the caller gets a live guy. Cannot set
2161 * TEMP, so sv_2mortal is out of question. */
a8bba7fa 2162 if (!CvLVALUE(cx->blk_sub.cv)) {
b0d9ce38 2163 POPSUB(cx,sv);
d470f89e 2164 PL_curpm = newpm;
b0d9ce38
GS
2165 LEAVE;
2166 LEAVESUB(sv);
d470f89e
GS
2167 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
2168 }
cd06dffe
GS
2169 if (gimme == G_SCALAR) {
2170 MARK = newsp + 1;
2171 EXTEND_MORTAL(1);
2172 if (MARK == SP) {
d470f89e 2173 if (SvFLAGS(TOPs) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) {
b0d9ce38 2174 POPSUB(cx,sv);
d470f89e 2175 PL_curpm = newpm;
b0d9ce38
GS
2176 LEAVE;
2177 LEAVESUB(sv);
d470f89e 2178 DIE(aTHX_ "Can't return a %s from lvalue subroutine",
cd06dffe 2179 SvREADONLY(TOPs) ? "readonly value" : "temporary");
d470f89e 2180 }
cd06dffe
GS
2181 else { /* Can be a localized value
2182 * subject to deletion. */
2183 PL_tmps_stack[++PL_tmps_ix] = *mark;
e1f15930 2184 (void)SvREFCNT_inc(*mark);
cd06dffe
GS
2185 }
2186 }
d470f89e 2187 else { /* Should not happen? */
b0d9ce38 2188 POPSUB(cx,sv);
d470f89e 2189 PL_curpm = newpm;
b0d9ce38
GS
2190 LEAVE;
2191 LEAVESUB(sv);
d470f89e 2192 DIE(aTHX_ "%s returned from lvalue subroutine in scalar context",
cd06dffe 2193 (MARK > SP ? "Empty array" : "Array"));
d470f89e 2194 }
cd06dffe
GS
2195 SP = MARK;
2196 }
2197 else if (gimme == G_ARRAY) {
2198 EXTEND_MORTAL(SP - newsp);
2199 for (mark = newsp + 1; mark <= SP; mark++) {
d470f89e
GS
2200 if (SvFLAGS(*mark) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) {
2201 /* Might be flattened array after $#array = */
2202 PUTBACK;
b0d9ce38 2203 POPSUB(cx,sv);
d470f89e 2204 PL_curpm = newpm;
b0d9ce38
GS
2205 LEAVE;
2206 LEAVESUB(sv);
d470f89e 2207 DIE(aTHX_ "Can't return %s from lvalue subroutine",
cd06dffe
GS
2208 (*mark != &PL_sv_undef)
2209 ? (SvREADONLY(TOPs)
2210 ? "a readonly value" : "a temporary")
2211 : "an uninitialized value");
d470f89e 2212 }
cd06dffe 2213 else {
cd06dffe
GS
2214 /* Can be a localized value subject to deletion. */
2215 PL_tmps_stack[++PL_tmps_ix] = *mark;
e1f15930 2216 (void)SvREFCNT_inc(*mark);
cd06dffe
GS
2217 }
2218 }
2219 }
2220 }
2221 else {
2222 if (gimme == G_SCALAR) {
2223 temporise:
2224 MARK = newsp + 1;
2225 if (MARK <= SP) {
a8bba7fa 2226 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
cd06dffe
GS
2227 if (SvTEMP(TOPs)) {
2228 *MARK = SvREFCNT_inc(TOPs);
2229 FREETMPS;
2230 sv_2mortal(*MARK);
2231 }
2232 else {
959e3673 2233 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
cd06dffe 2234 FREETMPS;
959e3673
GS
2235 *MARK = sv_mortalcopy(sv);
2236 SvREFCNT_dec(sv);
cd06dffe
GS
2237 }
2238 }
2239 else
2240 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
2241 }
2242 else {
2243 MEXTEND(MARK, 0);
2244 *MARK = &PL_sv_undef;
2245 }
2246 SP = MARK;
2247 }
2248 else if (gimme == G_ARRAY) {
2249 temporise_array:
2250 for (MARK = newsp + 1; MARK <= SP; MARK++) {
2251 if (!SvTEMP(*MARK)) {
2252 *MARK = sv_mortalcopy(*MARK);
2253 TAINT_NOT; /* Each item is independent */
2254 }
2255 }
2256 }
2257 }
2258 PUTBACK;
1c846c1f 2259
b0d9ce38 2260 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
cd06dffe
GS
2261 PL_curpm = newpm; /* ... and pop $1 et al */
2262
2263 LEAVE;
b0d9ce38 2264 LEAVESUB(sv);
cd06dffe
GS
2265 return pop_return();
2266}
2267
2268
76e3520e 2269STATIC CV *
cea2e8a9 2270S_get_db_sub(pTHX_ SV **svp, CV *cv)
3de9ffa1 2271{
3280af22 2272 SV *dbsv = GvSV(PL_DBsub);
491527d0
GS
2273
2274 if (!PERLDB_SUB_NN) {
2275 GV *gv = CvGV(cv);
2276
2277 save_item(dbsv);
2278 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
1c846c1f 2279 || strEQ(GvNAME(gv), "END")
491527d0
GS
2280 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
2281 !( (SvTYPE(*svp) == SVt_PVGV) && (GvCV((GV*)*svp) == cv)
2282 && (gv = (GV*)*svp) ))) {
2283 /* Use GV from the stack as a fallback. */
2284 /* GV is potentially non-unique, or contain different CV. */
c2e66d9e
GS
2285 SV *tmp = newRV((SV*)cv);
2286 sv_setsv(dbsv, tmp);
2287 SvREFCNT_dec(tmp);
491527d0
GS
2288 }
2289 else {
2290 gv_efullname3(dbsv, gv, Nullch);
2291 }
3de9ffa1
MB
2292 }
2293 else {
155aba94
GS
2294 (void)SvUPGRADE(dbsv, SVt_PVIV);
2295 (void)SvIOK_on(dbsv);
491527d0 2296 SAVEIV(SvIVX(dbsv));
5bc28da9 2297 SvIVX(dbsv) = PTR2IV(cv); /* Do it the quickest way */
3de9ffa1 2298 }
491527d0 2299
3de9ffa1 2300 if (CvXSUB(cv))
3280af22
NIS
2301 PL_curcopdb = PL_curcop;
2302 cv = GvCV(PL_DBsub);
3de9ffa1
MB
2303 return cv;
2304}
2305
a0d0e21e
LW
2306PP(pp_entersub)
2307{
4e35701f 2308 djSP; dPOPss;
a0d0e21e
LW
2309 GV *gv;
2310 HV *stash;
2311 register CV *cv;
c09156bb 2312 register PERL_CONTEXT *cx;
5d94fbed 2313 I32 gimme;
533c011a 2314 bool hasargs = (PL_op->op_flags & OPf_STACKED) != 0;
a0d0e21e
LW
2315
2316 if (!sv)
cea2e8a9 2317 DIE(aTHX_ "Not a CODE reference");
a0d0e21e
LW
2318 switch (SvTYPE(sv)) {
2319 default:
2320 if (!SvROK(sv)) {
748a9306 2321 char *sym;
2d8e6c8d 2322 STRLEN n_a;
748a9306 2323
3280af22 2324 if (sv == &PL_sv_yes) { /* unfound import, ignore */
fb73857a 2325 if (hasargs)
3280af22 2326 SP = PL_stack_base + POPMARK;
a0d0e21e 2327 RETURN;
fb73857a 2328 }
15ff848f
CS
2329 if (SvGMAGICAL(sv)) {
2330 mg_get(sv);
2331 sym = SvPOKp(sv) ? SvPVX(sv) : Nullch;
2332 }
2333 else
2d8e6c8d 2334 sym = SvPV(sv, n_a);
15ff848f 2335 if (!sym)
cea2e8a9 2336 DIE(aTHX_ PL_no_usym, "a subroutine");
533c011a 2337 if (PL_op->op_private & HINT_STRICT_REFS)
cea2e8a9 2338 DIE(aTHX_ PL_no_symref, sym, "a subroutine");
864dbfa3 2339 cv = get_cv(sym, TRUE);
a0d0e21e
LW
2340 break;
2341 }
f5284f61
IZ
2342 {
2343 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
2344 tryAMAGICunDEREF(to_cv);
2345 }
a0d0e21e
LW
2346 cv = (CV*)SvRV(sv);
2347 if (SvTYPE(cv) == SVt_PVCV)
2348 break;
2349 /* FALL THROUGH */
2350 case SVt_PVHV:
2351 case SVt_PVAV:
cea2e8a9 2352 DIE(aTHX_ "Not a CODE reference");
a0d0e21e
LW
2353 case SVt_PVCV:
2354 cv = (CV*)sv;
2355 break;
2356 case SVt_PVGV:
8ebc5c01 2357 if (!(cv = GvCVu((GV*)sv)))
f6ec51f7
GS
2358 cv = sv_2cv(sv, &stash, &gv, FALSE);
2359 if (!cv) {
2360 ENTER;
2361 SAVETMPS;
2362 goto try_autoload;
2363 }
2364 break;
a0d0e21e
LW
2365 }
2366
2367 ENTER;
2368 SAVETMPS;
2369
2370 retry:
a0d0e21e 2371 if (!CvROOT(cv) && !CvXSUB(cv)) {
44a8e56a 2372 GV* autogv;
22239a37 2373 SV* sub_name;
44a8e56a
PP
2374
2375 /* anonymous or undef'd function leaves us no recourse */
2376 if (CvANON(cv) || !(gv = CvGV(cv)))
cea2e8a9 2377 DIE(aTHX_ "Undefined subroutine called");
67caa1fe 2378
44a8e56a
PP
2379 /* autoloaded stub? */
2380 if (cv != GvCV(gv)) {
2381 cv = GvCV(gv);
a0d0e21e 2382 }
44a8e56a 2383 /* should call AUTOLOAD now? */
67caa1fe 2384 else {
f6ec51f7
GS
2385try_autoload:
2386 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
2387 FALSE)))
2388 {
2389 cv = GvCV(autogv);
2390 }
2391 /* sorry */
2392 else {
2393 sub_name = sv_newmortal();
2394 gv_efullname3(sub_name, gv, Nullch);
cea2e8a9 2395 DIE(aTHX_ "Undefined subroutine &%s called", SvPVX(sub_name));
f6ec51f7 2396 }
67caa1fe
GS
2397 }
2398 if (!cv)
cea2e8a9 2399 DIE(aTHX_ "Not a CODE reference");
67caa1fe 2400 goto retry;
a0d0e21e
LW
2401 }
2402
54310121 2403 gimme = GIMME_V;
67caa1fe 2404 if ((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub) && !CvNODEBUG(cv)) {
4f01c5a5 2405 cv = get_db_sub(&sv, cv);
67caa1fe 2406 if (!cv)
cea2e8a9 2407 DIE(aTHX_ "No DBsub routine");
67caa1fe 2408 }
a0d0e21e 2409
11343788 2410#ifdef USE_THREADS
3de9ffa1
MB
2411 /*
2412 * First we need to check if the sub or method requires locking.
458fb581
MB
2413 * If so, we gain a lock on the CV, the first argument or the
2414 * stash (for static methods), as appropriate. This has to be
2415 * inline because for FAKE_THREADS, COND_WAIT inlines code to
2416 * reschedule by returning a new op.
3de9ffa1 2417 */
11343788 2418 MUTEX_LOCK(CvMUTEXP(cv));
77a005ab
MB
2419 if (CvFLAGS(cv) & CVf_LOCKED) {
2420 MAGIC *mg;
2421 if (CvFLAGS(cv) & CVf_METHOD) {
533c011a
NIS
2422 if (SP > PL_stack_base + TOPMARK)
2423 sv = *(PL_stack_base + TOPMARK + 1);
77a005ab 2424 else {
13e08037
GS
2425 AV *av = (AV*)PL_curpad[0];
2426 if (hasargs || !av || AvFILLp(av) < 0
2427 || !(sv = AvARRAY(av)[0]))
2428 {
2429 MUTEX_UNLOCK(CvMUTEXP(cv));
d470f89e 2430 DIE(aTHX_ "no argument for locked method call");
13e08037 2431 }
77a005ab
MB
2432 }
2433 if (SvROK(sv))
2434 sv = SvRV(sv);
458fb581
MB
2435 else {
2436 STRLEN len;
2437 char *stashname = SvPV(sv, len);
2438 sv = (SV*)gv_stashpvn(stashname, len, TRUE);
2439 }
77a005ab
MB
2440 }
2441 else {
2442 sv = (SV*)cv;
2443 }
2444 MUTEX_UNLOCK(CvMUTEXP(cv));
2445 mg = condpair_magic(sv);
2446 MUTEX_LOCK(MgMUTEXP(mg));
2447 if (MgOWNER(mg) == thr)
2448 MUTEX_UNLOCK(MgMUTEXP(mg));
2449 else {
2450 while (MgOWNER(mg))
2451 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
2452 MgOWNER(mg) = thr;
bf49b057 2453 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: pp_entersub lock %p\n",
1fd28e87 2454 thr, sv);)
77a005ab 2455 MUTEX_UNLOCK(MgMUTEXP(mg));
c76ac1ee 2456 SAVEDESTRUCTOR_X(Perl_unlock_condpair, sv);
11343788 2457 }
77a005ab 2458 MUTEX_LOCK(CvMUTEXP(cv));
11343788 2459 }
3de9ffa1
MB
2460 /*
2461 * Now we have permission to enter the sub, we must distinguish
2462 * four cases. (0) It's an XSUB (in which case we don't care
2463 * about ownership); (1) it's ours already (and we're recursing);
2464 * (2) it's free (but we may already be using a cached clone);
2465 * (3) another thread owns it. Case (1) is easy: we just use it.
2466 * Case (2) means we look for a clone--if we have one, use it
2467 * otherwise grab ownership of cv. Case (3) means we look for a
2468 * clone (for non-XSUBs) and have to create one if we don't
2469 * already have one.
2470 * Why look for a clone in case (2) when we could just grab
2471 * ownership of cv straight away? Well, we could be recursing,
2472 * i.e. we originally tried to enter cv while another thread
2473 * owned it (hence we used a clone) but it has been freed up
2474 * and we're now recursing into it. It may or may not be "better"
2475 * to use the clone but at least CvDEPTH can be trusted.
2476 */
2477 if (CvOWNER(cv) == thr || CvXSUB(cv))
2478 MUTEX_UNLOCK(CvMUTEXP(cv));
11343788 2479 else {
3de9ffa1
MB
2480 /* Case (2) or (3) */
2481 SV **svp;
2482
11343788 2483 /*
3de9ffa1
MB
2484 * XXX Might it be better to release CvMUTEXP(cv) while we
2485 * do the hv_fetch? We might find someone has pinched it
2486 * when we look again, in which case we would be in case
2487 * (3) instead of (2) so we'd have to clone. Would the fact
2488 * that we released the mutex more quickly make up for this?
2489 */
b099ddc0 2490 if ((svp = hv_fetch(thr->cvcache, (char *)cv, sizeof(cv), FALSE)))
6ee623d5 2491 {
3de9ffa1 2492 /* We already have a clone to use */
11343788 2493 MUTEX_UNLOCK(CvMUTEXP(cv));
3de9ffa1 2494 cv = *(CV**)svp;
bf49b057 2495 DEBUG_S(PerlIO_printf(Perl_debug_log,
1fd28e87
MB
2496 "entersub: %p already has clone %p:%s\n",
2497 thr, cv, SvPEEK((SV*)cv)));
3de9ffa1
MB
2498 CvOWNER(cv) = thr;
2499 SvREFCNT_inc(cv);
2500 if (CvDEPTH(cv) == 0)
c76ac1ee 2501 SAVEDESTRUCTOR_X(unset_cvowner, (void*) cv);
3de9ffa1 2502 }
11343788 2503 else {
3de9ffa1
MB
2504 /* (2) => grab ownership of cv. (3) => make clone */
2505 if (!CvOWNER(cv)) {
2506 CvOWNER(cv) = thr;
2507 SvREFCNT_inc(cv);
11343788 2508 MUTEX_UNLOCK(CvMUTEXP(cv));
bf49b057 2509 DEBUG_S(PerlIO_printf(Perl_debug_log,
1fd28e87
MB
2510 "entersub: %p grabbing %p:%s in stash %s\n",
2511 thr, cv, SvPEEK((SV*)cv), CvSTASH(cv) ?
3de9ffa1 2512 HvNAME(CvSTASH(cv)) : "(none)"));
cd06dffe
GS
2513 }
2514 else {
3de9ffa1
MB
2515 /* Make a new clone. */
2516 CV *clonecv;
2517 SvREFCNT_inc(cv); /* don't let it vanish from under us */
2518 MUTEX_UNLOCK(CvMUTEXP(cv));
bf49b057 2519 DEBUG_S((PerlIO_printf(Perl_debug_log,
1fd28e87
MB
2520 "entersub: %p cloning %p:%s\n",
2521 thr, cv, SvPEEK((SV*)cv))));
3de9ffa1
MB
2522 /*
2523 * We're creating a new clone so there's no race
2524 * between the original MUTEX_UNLOCK and the
2525 * SvREFCNT_inc since no one will be trying to undef
2526 * it out from underneath us. At least, I don't think
2527 * there's a race...
2528 */
2529 clonecv = cv_clone(cv);
2530 SvREFCNT_dec(cv); /* finished with this */
199100c8 2531 hv_store(thr->cvcache, (char*)cv, sizeof(cv), (SV*)clonecv,0);
3de9ffa1
MB
2532 CvOWNER(clonecv) = thr;
2533 cv = clonecv;
11343788 2534 SvREFCNT_inc(cv);
11343788 2535 }
8b73bbec 2536 DEBUG_S(if (CvDEPTH(cv) != 0)
bf49b057 2537 PerlIO_printf(Perl_debug_log, "depth %ld != 0\n",
3de9ffa1 2538 CvDEPTH(cv)););
c76ac1ee 2539 SAVEDESTRUCTOR_X(unset_cvowner, (void*) cv);
11343788 2540 }
3de9ffa1 2541 }
11343788
MB
2542#endif /* USE_THREADS */
2543
a0d0e21e 2544 if (CvXSUB(cv)) {
67caa1fe 2545#ifdef PERL_XSUB_OLDSTYLE
a0d0e21e 2546 if (CvOLDSTYLE(cv)) {
20ce7b12 2547 I32 (*fp3)(int,int,int);
a0d0e21e
LW
2548 dMARK;
2549 register I32 items = SP - MARK;
67955e0c 2550 /* We dont worry to copy from @_. */
924508f0
GS
2551 while (SP > mark) {
2552 SP[1] = SP[0];
2553 SP--;
a0d0e21e 2554 }
3280af22 2555 PL_stack_sp = mark + 1;
1d7c1841 2556 fp3 = (I32(*)(int,int,int))CvXSUB(cv);
1c846c1f 2557 items = (*fp3)(CvXSUBANY(cv).any_i32,
3280af22 2558 MARK - PL_stack_base + 1,
ecfc5424 2559 items);
3280af22 2560 PL_stack_sp = PL_stack_base + items;
a0d0e21e 2561 }
67caa1fe
GS
2562 else
2563#endif /* PERL_XSUB_OLDSTYLE */
2564 {
748a9306
LW
2565 I32 markix = TOPMARK;
2566
a0d0e21e 2567 PUTBACK;
67955e0c
PP
2568
2569 if (!hasargs) {
2570 /* Need to copy @_ to stack. Alternative may be to
2571 * switch stack to @_, and copy return values
2572 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
6d4ff0d2
MB
2573 AV* av;
2574 I32 items;
2575#ifdef USE_THREADS
533c011a 2576 av = (AV*)PL_curpad[0];
6d4ff0d2 2577#else
3280af22 2578 av = GvAV(PL_defgv);
6d4ff0d2 2579#endif /* USE_THREADS */
93965878 2580 items = AvFILLp(av) + 1; /* @_ is not tieable */
67955e0c
PP
2581
2582 if (items) {
2583 /* Mark is at the end of the stack. */
924508f0
GS
2584 EXTEND(SP, items);
2585 Copy(AvARRAY(av), SP + 1, items, SV*);
2586 SP += items;
1c846c1f 2587 PUTBACK ;
67955e0c
PP
2588 }
2589 }
67caa1fe
GS
2590 /* We assume first XSUB in &DB::sub is the called one. */
2591 if (PL_curcopdb) {
1d7c1841 2592 SAVEVPTR(PL_curcop);
3280af22
NIS
2593 PL_curcop = PL_curcopdb;
2594 PL_curcopdb = NULL;
67955e0c
PP
2595 }
2596 /* Do we need to open block here? XXXX */
0cb96387 2597 (void)(*CvXSUB(cv))(aTHXo_ cv);
748a9306
LW
2598
2599 /* Enforce some sanity in scalar context. */
3280af22
NIS
2600 if (gimme == G_SCALAR && ++markix != PL_stack_sp - PL_stack_base ) {
2601 if (markix > PL_stack_sp - PL_stack_base)
2602 *(PL_stack_base + markix) = &PL_sv_undef;
748a9306 2603 else
3280af22
NIS
2604 *(PL_stack_base + markix) = *PL_stack_sp;
2605 PL_stack_sp = PL_stack_base + markix;
748a9306 2606 }
a0d0e21e
LW
2607 }
2608 LEAVE;
2609 return NORMAL;
2610 }
2611 else {
2612 dMARK;
2613 register I32 items = SP - MARK;
a0d0e21e
LW
2614 AV* padlist = CvPADLIST(cv);
2615 SV** svp = AvARRAY(padlist);
533c011a 2616 push_return(PL_op->op_next);
a0d0e21e
LW
2617 PUSHBLOCK(cx, CXt_SUB, MARK);
2618 PUSHSUB(cx);
2619 CvDEPTH(cv)++;
6b35e009
GS
2620 /* XXX This would be a natural place to set C<PL_compcv = cv> so
2621 * that eval'' ops within this sub know the correct lexical space.
2622 * Owing the speed considerations, we choose to search for the cv
2623 * in doeval() instead.
2624 */
a0d0e21e
LW
2625 if (CvDEPTH(cv) < 2)
2626 (void)SvREFCNT_inc(cv);
2627 else { /* save temporaries on recursion? */
1d7c1841 2628 PERL_STACK_OVERFLOW_CHECK();
93965878 2629 if (CvDEPTH(cv) > AvFILLp(padlist)) {
a0d0e21e
LW
2630 AV *av;
2631 AV *newpad = newAV();
4aa0a1f7 2632 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
93965878 2633 I32 ix = AvFILLp((AV*)svp[1]);
1d7c1841 2634 I32 names_fill = AvFILLp((AV*)svp[0]);
a0d0e21e 2635 svp = AvARRAY(svp[0]);
748a9306 2636 for ( ;ix > 0; ix--) {
1d7c1841 2637 if (names_fill >= ix && svp[ix] != &PL_sv_undef) {
748a9306 2638 char *name = SvPVX(svp[ix]);
5f05dabc
PP
2639 if ((SvFLAGS(svp[ix]) & SVf_FAKE) /* outer lexical? */
2640 || *name == '&') /* anonymous code? */
2641 {
2642 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix]));
748a9306
LW
2643 }
2644 else { /* our own lexical */
2645 if (*name == '@')
2646 av_store(newpad, ix, sv = (SV*)newAV());
2647 else if (*name == '%')
2648 av_store(newpad, ix, sv = (SV*)newHV());
2649 else
2650 av_store(newpad, ix, sv = NEWSV(0,0));
2651 SvPADMY_on(sv);
2652 }
a0d0e21e 2653 }
1d7c1841
GS
2654 else if (IS_PADGV(oldpad[ix]) || IS_PADCONST(oldpad[ix])) {
2655 av_store(newpad, ix, sv = SvREFCNT_inc(oldpad[ix]));
2656 }
a0d0e21e 2657 else {
748a9306 2658 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
2659 SvPADTMP_on(sv);
2660 }
2661 }
2662 av = newAV(); /* will be @_ */
2663 av_extend(av, 0);
2664 av_store(newpad, 0, (SV*)av);
2665 AvFLAGS(av) = AVf_REIFY;
2666 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
93965878 2667 AvFILLp(padlist) = CvDEPTH(cv);
a0d0e21e
LW
2668 svp = AvARRAY(padlist);
2669 }
2670 }
6d4ff0d2
MB
2671#ifdef USE_THREADS
2672 if (!hasargs) {
533c011a 2673 AV* av = (AV*)PL_curpad[0];
6d4ff0d2 2674
93965878 2675 items = AvFILLp(av) + 1;
6d4ff0d2
MB
2676 if (items) {
2677 /* Mark is at the end of the stack. */
924508f0
GS
2678 EXTEND(SP, items);
2679 Copy(AvARRAY(av), SP + 1, items, SV*);
2680 SP += items;
1c846c1f 2681 PUTBACK ;
6d4ff0d2
MB
2682 }
2683 }
2684#endif /* USE_THREADS */
1d7c1841 2685 SAVEVPTR(PL_curpad);
3280af22 2686 PL_curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
6d4ff0d2
MB
2687#ifndef USE_THREADS
2688 if (hasargs)
2689#endif /* USE_THREADS */
2690 {
2691 AV* av;
a0d0e21e
LW
2692 SV** ary;
2693
77a005ab 2694#if 0
bf49b057 2695 DEBUG_S(PerlIO_printf(Perl_debug_log,
0f15f207 2696 "%p entersub preparing @_\n", thr));
77a005ab 2697#endif
3280af22 2698 av = (AV*)PL_curpad[0];
221373f0
GS
2699 if (AvREAL(av)) {
2700 /* @_ is normally not REAL--this should only ever
2701 * happen when DB::sub() calls things that modify @_ */
2702 av_clear(av);
2703 AvREAL_off(av);
2704 AvREIFY_on(av);
2705 }
6d4ff0d2 2706#ifndef USE_THREADS
3280af22
NIS
2707 cx->blk_sub.savearray = GvAV(PL_defgv);
2708 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
6d4ff0d2 2709#endif /* USE_THREADS */
7032098e 2710 cx->blk_sub.oldcurpad = PL_curpad;
6d4ff0d2 2711 cx->blk_sub.argarray = av;
a0d0e21e
LW
2712 ++MARK;
2713
2714 if (items > AvMAX(av) + 1) {
2715 ary = AvALLOC(av);
2716 if (AvARRAY(av) != ary) {
2717 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2718 SvPVX(av) = (char*)ary;
2719 }
2720 if (items > AvMAX(av) + 1) {
2721 AvMAX(av) = items - 1;
2722 Renew(ary,items,SV*);
2723 AvALLOC(av) = ary;
2724 SvPVX(av) = (char*)ary;
2725 }
2726 }
2727 Copy(MARK,AvARRAY(av),items,SV*);
93965878 2728 AvFILLp(av) = items - 1;
1c846c1f 2729
a0d0e21e
LW
2730 while (items--) {
2731 if (*MARK)
2732 SvTEMP_off(*MARK);
2733 MARK++;
2734 }
2735 }
4a925ff6
GS
2736 /* warning must come *after* we fully set up the context
2737 * stuff so that __WARN__ handlers can safely dounwind()
2738 * if they want to
2739 */
2740 if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION)
2741 && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))
2742 sub_crush_depth(cv);
77a005ab 2743#if 0
bf49b057 2744 DEBUG_S(PerlIO_printf(Perl_debug_log,
0f15f207 2745 "%p entersub returning %p\n", thr, CvSTART(cv)));
77a005ab 2746#endif
a0d0e21e
LW
2747 RETURNOP(CvSTART(cv));
2748 }
2749}
2750
44a8e56a 2751void
864dbfa3 2752Perl_sub_crush_depth(pTHX_ CV *cv)
44a8e56a
PP
2753{
2754 if (CvANON(cv))
cea2e8a9 2755 Perl_warner(aTHX_ WARN_RECURSION, "Deep recursion on anonymous subroutine");
44a8e56a
PP
2756 else {
2757 SV* tmpstr = sv_newmortal();
2758 gv_efullname3(tmpstr, CvGV(cv), Nullch);
1c846c1f 2759 Perl_warner(aTHX_ WARN_RECURSION, "Deep recursion on subroutine \"%s\"",
599cee73 2760 SvPVX(tmpstr));
44a8e56a
PP
2761 }
2762}
2763
a0d0e21e
LW
2764PP(pp_aelem)
2765{
4e35701f 2766 djSP;
a0d0e21e 2767 SV** svp;
467f0320 2768 IV elem = POPi;
68dc0745 2769 AV* av = (AV*)POPs;
533c011a
NIS
2770 U32 lval = PL_op->op_flags & OPf_MOD;
2771 U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > AvFILL(av));
be6c24e0 2772 SV *sv;
a0d0e21e 2773
748a9306 2774 if (elem > 0)
3280af22 2775 elem -= PL_curcop->cop_arybase;
a0d0e21e
LW
2776 if (SvTYPE(av) != SVt_PVAV)
2777 RETPUSHUNDEF;
68dc0745 2778 svp = av_fetch(av, elem, lval && !defer);
a0d0e21e 2779 if (lval) {
3280af22 2780 if (!svp || *svp == &PL_sv_undef) {
68dc0745
PP
2781 SV* lv;
2782 if (!defer)
cea2e8a9 2783 DIE(aTHX_ PL_no_aelem, elem);
68dc0745
PP
2784 lv = sv_newmortal();
2785 sv_upgrade(lv, SVt_PVLV);
2786 LvTYPE(lv) = 'y';
2787 sv_magic(lv, Nullsv, 'y', Nullch, 0);
2788 LvTARG(lv) = SvREFCNT_inc(av);
2789 LvTARGOFF(lv) = elem;
2790 LvTARGLEN(lv) = 1;
2791 PUSHs(lv);
2792 RETURN;
2793 }
533c011a 2794 if (PL_op->op_private & OPpLVAL_INTRO)
161b7d16 2795 save_aelem(av, elem, svp);
533c011a
NIS
2796 else if (PL_op->op_private & OPpDEREF)
2797 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
a0d0e21e 2798 }
3280af22 2799 sv = (svp ? *svp : &PL_sv_undef);
be6c24e0
GS
2800 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
2801 sv = sv_mortalcopy(sv);
2802 PUSHs(sv);
a0d0e21e
LW
2803 RETURN;
2804}
2805
02a9e968 2806void
864dbfa3 2807Perl_vivify_ref(pTHX_ SV *sv, U32 to_what)
02a9e968
CS
2808{
2809 if (SvGMAGICAL(sv))
2810 mg_get(sv);
2811 if (!SvOK(sv)) {
2812 if (SvREADONLY(sv))
cea2e8a9 2813 Perl_croak(aTHX_ PL_no_modify);
5f05dabc
PP
2814 if (SvTYPE(sv) < SVt_RV)
2815 sv_upgrade(sv, SVt_RV);
2816 else if (SvTYPE(sv) >= SVt_PV) {
2817 (void)SvOOK_off(sv);
2818 Safefree(SvPVX(sv));
2819 SvLEN(sv) = SvCUR(sv) = 0;
2820 }
68dc0745 2821 switch (to_what) {
5f05dabc 2822 case OPpDEREF_SV:
8c52afec 2823 SvRV(sv) = NEWSV(355,0);
5f05dabc
PP
2824 break;
2825 case OPpDEREF_AV:
2826 SvRV(sv) = (SV*)newAV();
2827 break;
2828 case OPpDEREF_HV:
2829 SvRV(sv) = (SV*)newHV();
2830 break;
2831 }
02a9e968
CS
2832 SvROK_on(sv);
2833 SvSETMAGIC(sv);
2834 }
2835}
2836
a0d0e21e
LW
2837PP(pp_method)
2838{
4e35701f 2839 djSP;
f5d5a27c
CS
2840 SV* sv = TOPs;
2841
2842 if (SvROK(sv)) {
eda383f2 2843 SV* rsv = SvRV(sv);
f5d5a27c
CS
2844 if (SvTYPE(rsv) == SVt_PVCV) {
2845 SETs(rsv);
2846 RETURN;
2847 }
2848 }
2849
2850 SETs(method_common(sv, Null(U32*)));
2851 RETURN;
2852}
2853
2854PP(pp_method_named)
2855{
2856 djSP;
2857 SV* sv = cSVOP->op_sv;
2858 U32 hash = SvUVX(sv);
2859
2860 XPUSHs(method_common(sv, &hash));
2861 RETURN;
2862}
2863
2864STATIC SV *
2865S_method_common(pTHX_ SV* meth, U32* hashp)
2866{
a0d0e21e
LW
2867 SV* sv;
2868 SV* ob;
2869 GV* gv;
56304f61
CS
2870 HV* stash;
2871 char* name;
f5d5a27c 2872 STRLEN namelen;
ac91690f
CS
2873 char* packname;
2874 STRLEN packlen;
a0d0e21e 2875
f5d5a27c 2876 name = SvPV(meth, namelen);
3280af22 2877 sv = *(PL_stack_base + TOPMARK + 1);
f5d5a27c 2878
4f1b7578
SC
2879 if (!sv)
2880 Perl_croak(aTHX_ "Can't call method \"%s\" on an undefined value", name);
2881
16d20bd9
AD
2882 if (SvGMAGICAL(sv))
2883 mg_get(sv);
a0d0e21e 2884 if (SvROK(sv))
16d20bd9 2885 ob = (SV*)SvRV(sv);
a0d0e21e
LW
2886 else {
2887 GV* iogv;
a0d0e21e 2888
56304f61 2889 packname = Nullch;
a0d0e21e 2890 if (!SvOK(sv) ||
56304f61 2891 !(packname = SvPV(sv, packlen)) ||
a0d0e21e
LW
2892 !(iogv = gv_fetchpv(packname, FALSE, SVt_PVIO)) ||
2893 !(ob=(SV*)GvIO(iogv)))
2894 {
1c846c1f 2895 if (!packname ||
7e2040f0 2896 ((*(U8*)packname >= 0xc0 && DO_UTF8(sv))
b86a2fa7 2897 ? !isIDFIRST_utf8((U8*)packname)
834a4ddd
LW
2898 : !isIDFIRST(*packname)
2899 ))
2900 {
f5d5a27c
CS
2901 Perl_croak(aTHX_ "Can't call method \"%s\" %s", name,
2902 SvOK(sv) ? "without a package or object reference"
2903 : "on an undefined value");
834a4ddd 2904 }
56304f61 2905 stash = gv_stashpvn(packname, packlen, TRUE);
ac91690f 2906 goto fetch;
a0d0e21e 2907 }
3280af22 2908 *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
a0d0e21e
LW
2909 }
2910
f0d43078
GS
2911 if (!ob || !(SvOBJECT(ob)
2912 || (SvTYPE(ob) == SVt_PVGV && (ob = (SV*)GvIO((GV*)ob))
2913 && SvOBJECT(ob))))
2914 {
f5d5a27c
CS
2915 Perl_croak(aTHX_ "Can't call method \"%s\" on unblessed reference",
2916 name);
f0d43078 2917 }
a0d0e21e 2918
56304f61 2919 stash = SvSTASH(ob);
a0d0e21e 2920
ac91690f 2921 fetch:
f5d5a27c
CS
2922 /* shortcut for simple names */
2923 if (hashp) {
2924 HE* he = hv_fetch_ent(stash, meth, 0, *hashp);
2925 if (he) {
2926 gv = (GV*)HeVAL(he);
2927 if (isGV(gv) && GvCV(gv) &&
2928 (!GvCVGEN(gv) || GvCVGEN(gv) == PL_sub_generation))
2929 return (SV*)GvCV(gv);
2930 }
2931 }
2932
ac91690f 2933 gv = gv_fetchmethod(stash, name);
56304f61
CS
2934 if (!gv) {
2935 char* leaf = name;
2936 char* sep = Nullch;
2937 char* p;
c1899e02 2938 GV* gv;
56304f61
CS
2939
2940 for (p = name; *p; p++) {
2941 if (*p == '\'')
2942 sep = p, leaf = p + 1;
2943 else if (*p == ':' && *(p + 1) == ':')
2944 sep = p, leaf = p + 2;
2945 }
2946 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
1d7c1841 2947 packname = sep ? CopSTASHPV(PL_curcop) : HvNAME(stash);
56304f61
CS
2948 packlen = strlen(packname);
2949 }
2950 else {
2951 packname = name;
2952 packlen = sep - name;
2953 }
c1899e02
GS
2954 gv = gv_fetchpv(packname, 0, SVt_PVHV);
2955 if (gv && isGV(gv)) {
2956 Perl_croak(aTHX_
2957 "Can't locate object method \"%s\" via package \"%s\"",
2958 leaf, packname);
2959 }
2960 else {
2961 Perl_croak(aTHX_
f6e565ef 2962 "Can't locate object method \"%s\" via package \"%s\""
c1899e02
GS
2963 " (perhaps you forgot to load \"%s\"?)",
2964 leaf, packname, packname);
2965 }
56304f61 2966 }
f5d5a27c 2967 return isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv;
a0d0e21e 2968}
22239a37 2969
51371543
GS
2970#ifdef USE_THREADS
2971static void
2972unset_cvowner(pTHXo_ void *cvarg)
2973{
2974 register CV* cv = (CV *) cvarg;
51371543 2975
bf49b057 2976 DEBUG_S((PerlIO_printf(Perl_debug_log, "%p unsetting CvOWNER of %p:%s\n",
51371543
GS
2977 thr, cv, SvPEEK((SV*)cv))));
2978 MUTEX_LOCK(CvMUTEXP(cv));
2979 DEBUG_S(if (CvDEPTH(cv) != 0)
bf49b057 2980 PerlIO_printf(Perl_debug_log, "depth %ld != 0\n",
51371543
GS
2981 CvDEPTH(cv)););
2982 assert(thr == CvOWNER(cv));
2983 CvOWNER(cv) = 0;
2984 MUTEX_UNLOCK(CvMUTEXP(cv));
2985 SvREFCNT_dec(cv);
2986}
2987#endif /* USE_THREADS */