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