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