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