This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Forget prototype of subroutine after C<undef &subr>
[perl5.git] / pp_hot.c
CommitLineData
a0d0e21e
LW
1/* pp_hot.c
2 *
9607fc9c 3 * Copyright (c) 1991-1997, 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"
19#include "perl.h"
20
21/* Hot code. */
22
23PP(pp_const)
24{
25 dSP;
26 XPUSHs(cSVOP->op_sv);
27 RETURN;
28}
29
30PP(pp_nextstate)
31{
32 curcop = (COP*)op;
33 TAINT_NOT; /* Each statement is presumed innocent */
34 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
35 FREETMPS;
36 return NORMAL;
37}
38
39PP(pp_gvsv)
40{
41 dSP;
42 EXTEND(sp,1);
43 if (op->op_private & OPpLVAL_INTRO)
44 PUSHs(save_scalar(cGVOP->op_gv));
45 else
46 PUSHs(GvSV(cGVOP->op_gv));
47 RETURN;
48}
49
50PP(pp_null)
51{
52 return NORMAL;
53}
54
55PP(pp_pushmark)
56{
57 PUSHMARK(stack_sp);
58 return NORMAL;
59}
60
61PP(pp_stringify)
62{
63 dSP; dTARGET;
64 STRLEN len;
65 char *s;
66 s = SvPV(TOPs,len);
67 sv_setpvn(TARG,s,len);
68 SETTARG;
69 RETURN;
70}
71
72PP(pp_gv)
73{
74 dSP;
75 XPUSHs((SV*)cGVOP->op_gv);
76 RETURN;
77}
78
c07a80fd
PP
79PP(pp_gelem)
80{
81 GV *gv;
82 SV *sv;
83 SV *ref;
84 char *elem;
85 dSP;
86
87 sv = POPs;
88 elem = SvPV(sv, na);
89 gv = (GV*)POPs;
90 ref = Nullsv;
91 sv = Nullsv;
92 switch (elem ? *elem : '\0')
93 {
94 case 'A':
95 if (strEQ(elem, "ARRAY"))
96 ref = (SV*)GvAV(gv);
97 break;
98 case 'C':
99 if (strEQ(elem, "CODE"))
8ebc5c01 100 ref = (SV*)GvCVu(gv);
c07a80fd
PP
101 break;
102 case 'F':
36477c24 103 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
c07a80fd
PP
104 ref = (SV*)GvIOp(gv);
105 break;
106 case 'G':
107 if (strEQ(elem, "GLOB"))
108 ref = (SV*)gv;
109 break;
110 case 'H':
111 if (strEQ(elem, "HASH"))
112 ref = (SV*)GvHV(gv);
113 break;
36477c24
PP
114 case 'I':
115 if (strEQ(elem, "IO"))
116 ref = (SV*)GvIOp(gv);
117 break;
c07a80fd
PP
118 case 'N':
119 if (strEQ(elem, "NAME"))
120 sv = newSVpv(GvNAME(gv), GvNAMELEN(gv));
121 break;
122 case 'P':
123 if (strEQ(elem, "PACKAGE"))
124 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
125 break;
126 case 'S':
127 if (strEQ(elem, "SCALAR"))
128 ref = GvSV(gv);
129 break;
130 }
131 if (ref)
132 sv = newRV(ref);
133 if (sv)
134 sv_2mortal(sv);
135 else
136 sv = &sv_undef;
137 XPUSHs(sv);
138 RETURN;
139}
140
a0d0e21e
LW
141PP(pp_and)
142{
143 dSP;
144 if (!SvTRUE(TOPs))
145 RETURN;
146 else {
147 --SP;
148 RETURNOP(cLOGOP->op_other);
149 }
150}
151
152PP(pp_sassign)
153{
154 dSP; dPOPTOPssrl;
748a9306
LW
155 MAGIC *mg;
156
a0d0e21e
LW
157 if (op->op_private & OPpASSIGN_BACKWARDS) {
158 SV *temp;
159 temp = left; left = right; right = temp;
160 }
bbce6d69 161 if (tainting && tainted && !SvTAINTED(left))
a0d0e21e 162 TAINT_NOT;
54310121 163 SvSetMagicSV(right, left);
a0d0e21e
LW
164 SETs(right);
165 RETURN;
166}
167
168PP(pp_cond_expr)
169{
170 dSP;
171 if (SvTRUEx(POPs))
172 RETURNOP(cCONDOP->op_true);
173 else
174 RETURNOP(cCONDOP->op_false);
175}
176
177PP(pp_unstack)
178{
179 I32 oldsave;
180 TAINT_NOT; /* Each statement is presumed innocent */
181 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
182 FREETMPS;
183 oldsave = scopestack[scopestack_ix - 1];
184 LEAVE_SCOPE(oldsave);
185 return NORMAL;
186}
187
a0d0e21e
LW
188PP(pp_concat)
189{
748a9306
LW
190 dSP; dATARGET; tryAMAGICbin(concat,opASSIGN);
191 {
192 dPOPTOPssrl;
a0d0e21e
LW
193 STRLEN len;
194 char *s;
195 if (TARG != left) {
196 s = SvPV(left,len);
197 sv_setpvn(TARG,s,len);
198 }
c07a80fd
PP
199 else if (SvGMAGICAL(TARG))
200 mg_get(TARG);
7a4c00b4 201 else if (!SvOK(TARG) && SvTYPE(TARG) <= SVt_PVMG) {
748a9306 202 sv_setpv(TARG, ""); /* Suppress warning. */
7a4c00b4 203 s = SvPV_force(TARG, len);
4633a7c4 204 }
a0d0e21e 205 s = SvPV(right,len);
68dc0745
PP
206 if (SvOK(TARG))
207 sv_catpvn(TARG,s,len);
208 else
209 sv_setpvn(TARG,s,len); /* suppress warning */
a0d0e21e
LW
210 SETTARG;
211 RETURN;
748a9306 212 }
a0d0e21e
LW
213}
214
215PP(pp_padsv)
216{
217 dSP; dTARGET;
218 XPUSHs(TARG);
4633a7c4
LW
219 if (op->op_flags & OPf_MOD) {
220 if (op->op_private & OPpLVAL_INTRO)
221 SAVECLEARSV(curpad[op->op_targ]);
5f05dabc 222 else if (op->op_private & OPpDEREF)
54310121 223 vivify_ref(curpad[op->op_targ], op->op_private & OPpDEREF);
4633a7c4 224 }
a0d0e21e
LW
225 RETURN;
226}
227
228PP(pp_readline)
229{
230 last_in_gv = (GV*)(*stack_sp--);
231 return do_readline();
232}
233
234PP(pp_eq)
235{
236 dSP; tryAMAGICbinSET(eq,0);
237 {
238 dPOPnv;
54310121 239 SETs(boolSV(TOPn == value));
a0d0e21e
LW
240 RETURN;
241 }
242}
243
244PP(pp_preinc)
245{
246 dSP;
68dc0745 247 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
44a8e56a 248 croak(no_modify);
55497cff
PP
249 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
250 SvIVX(TOPs) != IV_MAX)
251 {
252 ++SvIVX(TOPs);
253 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
254 }
255 else
256 sv_inc(TOPs);
a0d0e21e
LW
257 SvSETMAGIC(TOPs);
258 return NORMAL;
259}
260
261PP(pp_or)
262{
263 dSP;
264 if (SvTRUE(TOPs))
265 RETURN;
266 else {
267 --SP;
268 RETURNOP(cLOGOP->op_other);
269 }
270}
271
272PP(pp_add)
273{
274 dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
275 {
7a4c00b4 276 dPOPTOPnnrl_ul;
a0d0e21e
LW
277 SETn( left + right );
278 RETURN;
279 }
280}
281
282PP(pp_aelemfast)
283{
284 dSP;
285 AV *av = GvAV((GV*)cSVOP->op_sv);
286 SV** svp = av_fetch(av, op->op_private, op->op_flags & OPf_MOD);
287 PUSHs(svp ? *svp : &sv_undef);
288 RETURN;
289}
290
291PP(pp_join)
292{
293 dSP; dMARK; dTARGET;
294 MARK++;
295 do_join(TARG, *MARK, MARK, SP);
296 SP = MARK;
297 SETs(TARG);
298 RETURN;
299}
300
301PP(pp_pushre)
302{
303 dSP;
44a8e56a
PP
304#ifdef DEBUGGING
305 /*
306 * We ass_u_me that LvTARGOFF() comes first, and that two STRLENs
307 * will be enough to hold an OP*.
308 */
309 SV* sv = sv_newmortal();
310 sv_upgrade(sv, SVt_PVLV);
311 LvTYPE(sv) = '/';
312 Copy(&op, &LvTARGOFF(sv), 1, OP*);
313 XPUSHs(sv);
314#else
a0d0e21e 315 XPUSHs((SV*)op);
44a8e56a 316#endif
a0d0e21e
LW
317 RETURN;
318}
319
320/* Oversized hot code. */
321
322PP(pp_print)
323{
324 dSP; dMARK; dORIGMARK;
325 GV *gv;
326 IO *io;
760ac839 327 register PerlIO *fp;
236988e4 328 MAGIC *mg;
a0d0e21e
LW
329
330 if (op->op_flags & OPf_STACKED)
331 gv = (GV*)*++MARK;
332 else
333 gv = defoutgv;
236988e4 334 if (SvMAGICAL(gv) && (mg = mg_find((SV*)gv, 'q'))) {
68dc0745
PP
335 if (MARK == ORIGMARK) {
336 EXTEND(SP, 1);
337 ++MARK;
338 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
339 ++SP;
340 }
341 PUSHMARK(MARK - 1);
236988e4 342 *MARK = mg->mg_obj;
68dc0745 343 PUTBACK;
236988e4
PP
344 ENTER;
345 perl_call_method("PRINT", G_SCALAR);
346 LEAVE;
347 SPAGAIN;
68dc0745
PP
348 MARK = ORIGMARK + 1;
349 *MARK = *SP;
350 SP = MARK;
236988e4
PP
351 RETURN;
352 }
a0d0e21e 353 if (!(io = GvIO(gv))) {
748a9306
LW
354 if (dowarn) {
355 SV* sv = sv_newmortal();
4f732624 356 gv_fullname3(sv, gv, Nullch);
748a9306
LW
357 warn("Filehandle %s never opened", SvPV(sv,na));
358 }
359
360 SETERRNO(EBADF,RMS$_IFI);
a0d0e21e
LW
361 goto just_say_no;
362 }
363 else if (!(fp = IoOFP(io))) {
364 if (dowarn) {
748a9306 365 SV* sv = sv_newmortal();
4f732624 366 gv_fullname3(sv, gv, Nullch);
a0d0e21e 367 if (IoIFP(io))
748a9306 368 warn("Filehandle %s opened only for input", SvPV(sv,na));
a0d0e21e 369 else
748a9306 370 warn("print on closed filehandle %s", SvPV(sv,na));
a0d0e21e 371 }
748a9306 372 SETERRNO(EBADF,IoIFP(io)?RMS$_FAC:RMS$_IFI);
a0d0e21e
LW
373 goto just_say_no;
374 }
375 else {
376 MARK++;
377 if (ofslen) {
378 while (MARK <= SP) {
379 if (!do_print(*MARK, fp))
380 break;
381 MARK++;
382 if (MARK <= SP) {
760ac839 383 if (PerlIO_write(fp, ofs, ofslen) == 0 || PerlIO_error(fp)) {
a0d0e21e
LW
384 MARK--;
385 break;
386 }
387 }
388 }
389 }
390 else {
391 while (MARK <= SP) {
392 if (!do_print(*MARK, fp))
393 break;
394 MARK++;
395 }
396 }
397 if (MARK <= SP)
398 goto just_say_no;
399 else {
400 if (orslen)
760ac839 401 if (PerlIO_write(fp, ors, orslen) == 0 || PerlIO_error(fp))
a0d0e21e
LW
402 goto just_say_no;
403
404 if (IoFLAGS(io) & IOf_FLUSH)
760ac839 405 if (PerlIO_flush(fp) == EOF)
a0d0e21e
LW
406 goto just_say_no;
407 }
408 }
409 SP = ORIGMARK;
410 PUSHs(&sv_yes);
411 RETURN;
412
413 just_say_no:
414 SP = ORIGMARK;
415 PUSHs(&sv_undef);
416 RETURN;
417}
418
419PP(pp_rv2av)
420{
421 dSP; dPOPss;
a0d0e21e
LW
422 AV *av;
423
424 if (SvROK(sv)) {
425 wasref:
426 av = (AV*)SvRV(sv);
427 if (SvTYPE(av) != SVt_PVAV)
428 DIE("Not an ARRAY reference");
429 if (op->op_private & OPpLVAL_INTRO)
430 av = (AV*)save_svref((SV**)sv);
431 if (op->op_flags & OPf_REF) {
432 PUSHs((SV*)av);
433 RETURN;
434 }
435 }
436 else {
437 if (SvTYPE(sv) == SVt_PVAV) {
438 av = (AV*)sv;
439 if (op->op_flags & OPf_REF) {
440 PUSHs((SV*)av);
441 RETURN;
442 }
443 }
444 else {
67955e0c
PP
445 GV *gv;
446
a0d0e21e 447 if (SvTYPE(sv) != SVt_PVGV) {
748a9306
LW
448 char *sym;
449
a0d0e21e
LW
450 if (SvGMAGICAL(sv)) {
451 mg_get(sv);
452 if (SvROK(sv))
453 goto wasref;
454 }
455 if (!SvOK(sv)) {
456 if (op->op_flags & OPf_REF ||
457 op->op_private & HINT_STRICT_REFS)
458 DIE(no_usym, "an ARRAY");
d83e6520
CS
459 if (dowarn)
460 warn(warn_uninit);
4633a7c4
LW
461 if (GIMME == G_ARRAY)
462 RETURN;
a0d0e21e
LW
463 RETPUSHUNDEF;
464 }
748a9306 465 sym = SvPV(sv,na);
a0d0e21e 466 if (op->op_private & HINT_STRICT_REFS)
748a9306 467 DIE(no_symref, sym, "an ARRAY");
67955e0c
PP
468 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVAV);
469 } else {
470 gv = (GV*)sv;
a0d0e21e 471 }
67955e0c 472 av = GvAVn(gv);
a0d0e21e 473 if (op->op_private & OPpLVAL_INTRO)
67955e0c 474 av = save_ary(gv);
a0d0e21e
LW
475 if (op->op_flags & OPf_REF) {
476 PUSHs((SV*)av);
477 RETURN;
478 }
479 }
480 }
481
482 if (GIMME == G_ARRAY) {
483 I32 maxarg = AvFILL(av) + 1;
484 EXTEND(SP, maxarg);
485 Copy(AvARRAY(av), SP+1, maxarg, SV*);
486 SP += maxarg;
487 }
488 else {
489 dTARGET;
490 I32 maxarg = AvFILL(av) + 1;
491 PUSHi(maxarg);
492 }
493 RETURN;
494}
495
496PP(pp_rv2hv)
497{
a0d0e21e 498 dSP; dTOPss;
a0d0e21e
LW
499 HV *hv;
500
501 if (SvROK(sv)) {
502 wasref:
503 hv = (HV*)SvRV(sv);
504 if (SvTYPE(hv) != SVt_PVHV)
505 DIE("Not a HASH reference");
506 if (op->op_private & OPpLVAL_INTRO)
507 hv = (HV*)save_svref((SV**)sv);
508 if (op->op_flags & OPf_REF) {
509 SETs((SV*)hv);
510 RETURN;
511 }
512 }
513 else {
514 if (SvTYPE(sv) == SVt_PVHV) {
515 hv = (HV*)sv;
516 if (op->op_flags & OPf_REF) {
517 SETs((SV*)hv);
518 RETURN;
519 }
520 }
521 else {
67955e0c
PP
522 GV *gv;
523
a0d0e21e 524 if (SvTYPE(sv) != SVt_PVGV) {
748a9306
LW
525 char *sym;
526
a0d0e21e
LW
527 if (SvGMAGICAL(sv)) {
528 mg_get(sv);
529 if (SvROK(sv))
530 goto wasref;
531 }
532 if (!SvOK(sv)) {
533 if (op->op_flags & OPf_REF ||
534 op->op_private & HINT_STRICT_REFS)
535 DIE(no_usym, "a HASH");
d83e6520
CS
536 if (dowarn)
537 warn(warn_uninit);
4633a7c4
LW
538 if (GIMME == G_ARRAY) {
539 SP--;
540 RETURN;
541 }
a0d0e21e
LW
542 RETSETUNDEF;
543 }
748a9306 544 sym = SvPV(sv,na);
a0d0e21e 545 if (op->op_private & HINT_STRICT_REFS)
748a9306 546 DIE(no_symref, sym, "a HASH");
67955e0c
PP
547 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVHV);
548 } else {
549 gv = (GV*)sv;
a0d0e21e 550 }
67955e0c 551 hv = GvHVn(gv);
a0d0e21e 552 if (op->op_private & OPpLVAL_INTRO)
67955e0c 553 hv = save_hash(gv);
a0d0e21e
LW
554 if (op->op_flags & OPf_REF) {
555 SETs((SV*)hv);
556 RETURN;
557 }
558 }
559 }
560
561 if (GIMME == G_ARRAY) { /* array wanted */
562 *stack_sp = (SV*)hv;
563 return do_kv(ARGS);
564 }
565 else {
566 dTARGET;
46fc3d4c
PP
567 if (HvFILL(hv))
568 sv_setpvf(TARG, "%ld/%ld",
569 (long)HvFILL(hv), (long)HvMAX(hv) + 1);
a0d0e21e
LW
570 else
571 sv_setiv(TARG, 0);
572 SETTARG;
573 RETURN;
574 }
575}
576
577PP(pp_aassign)
578{
579 dSP;
580 SV **lastlelem = stack_sp;
581 SV **lastrelem = stack_base + POPMARK;
582 SV **firstrelem = stack_base + POPMARK + 1;
583 SV **firstlelem = lastrelem + 1;
584
585 register SV **relem;
586 register SV **lelem;
587
588 register SV *sv;
589 register AV *ary;
590
54310121 591 I32 gimme;
a0d0e21e
LW
592 HV *hash;
593 I32 i;
594 int magic;
595
596 delaymagic = DM_DELAY; /* catch simultaneous items */
597
598 /* If there's a common identifier on both sides we have to take
599 * special care that assigning the identifier on the left doesn't
600 * clobber a value on the right that's used later in the list.
601 */
602 if (op->op_private & OPpASSIGN_COMMON) {
603 for (relem = firstrelem; relem <= lastrelem; relem++) {
604 /*SUPPRESS 560*/
605 if (sv = *relem)
606 *relem = sv_mortalcopy(sv);
607 }
608 }
609
610 relem = firstrelem;
611 lelem = firstlelem;
612 ary = Null(AV*);
613 hash = Null(HV*);
614 while (lelem <= lastlelem) {
bbce6d69 615 TAINT_NOT; /* Each item stands on its own, taintwise. */
a0d0e21e
LW
616 sv = *lelem++;
617 switch (SvTYPE(sv)) {
618 case SVt_PVAV:
619 ary = (AV*)sv;
748a9306 620 magic = SvMAGICAL(ary) != 0;
a0d0e21e
LW
621
622 av_clear(ary);
7e42bd57 623 av_extend(ary, lastrelem - relem);
a0d0e21e
LW
624 i = 0;
625 while (relem <= lastrelem) { /* gobble up all the rest */
626 sv = NEWSV(28,0);
627 assert(*relem);
628 sv_setsv(sv,*relem);
629 *(relem++) = sv;
630 (void)av_store(ary,i++,sv);
631 if (magic)
632 mg_set(sv);
bbce6d69 633 TAINT_NOT;
a0d0e21e
LW
634 }
635 break;
636 case SVt_PVHV: {
a0d0e21e
LW
637 SV *tmpstr;
638
639 hash = (HV*)sv;
748a9306 640 magic = SvMAGICAL(hash) != 0;
a0d0e21e
LW
641 hv_clear(hash);
642
643 while (relem < lastrelem) { /* gobble up all the rest */
644 STRLEN len;
4633a7c4 645 if (*relem)
a0d0e21e 646 sv = *(relem++);
4633a7c4 647 else
a0d0e21e 648 sv = &sv_no, relem++;
a0d0e21e
LW
649 tmpstr = NEWSV(29,0);
650 if (*relem)
651 sv_setsv(tmpstr,*relem); /* value */
652 *(relem++) = tmpstr;
760ac839 653 (void)hv_store_ent(hash,sv,tmpstr,0);
a0d0e21e
LW
654 if (magic)
655 mg_set(tmpstr);
bbce6d69 656 TAINT_NOT;
8e07c86e 657 }
760ac839
LW
658 if (relem == lastrelem)
659 warn("Odd number of elements in hash list");
a0d0e21e
LW
660 }
661 break;
662 default:
663 if (SvTHINKFIRST(sv)) {
664 if (SvREADONLY(sv) && curcop != &compiling) {
665 if (sv != &sv_undef && sv != &sv_yes && sv != &sv_no)
666 DIE(no_modify);
667 if (relem <= lastrelem)
668 relem++;
669 break;
670 }
671 if (SvROK(sv))
672 sv_unref(sv);
673 }
674 if (relem <= lastrelem) {
675 sv_setsv(sv, *relem);
676 *(relem++) = sv;
677 }
678 else
679 sv_setsv(sv, &sv_undef);
680 SvSETMAGIC(sv);
681 break;
682 }
683 }
684 if (delaymagic & ~DM_DELAY) {
685 if (delaymagic & DM_UID) {
686#ifdef HAS_SETRESUID
687 (void)setresuid(uid,euid,(Uid_t)-1);
56febc5e
AD
688#else
689# ifdef HAS_SETREUID
a0d0e21e 690 (void)setreuid(uid,euid);
56febc5e
AD
691# else
692# ifdef HAS_SETRUID
a0d0e21e
LW
693 if ((delaymagic & DM_UID) == DM_RUID) {
694 (void)setruid(uid);
748a9306 695 delaymagic &= ~DM_RUID;
a0d0e21e 696 }
56febc5e
AD
697# endif /* HAS_SETRUID */
698# ifdef HAS_SETEUID
a0d0e21e
LW
699 if ((delaymagic & DM_UID) == DM_EUID) {
700 (void)seteuid(uid);
748a9306 701 delaymagic &= ~DM_EUID;
a0d0e21e 702 }
56febc5e 703# endif /* HAS_SETEUID */
a0d0e21e
LW
704 if (delaymagic & DM_UID) {
705 if (uid != euid)
706 DIE("No setreuid available");
707 (void)setuid(uid);
708 }
56febc5e
AD
709# endif /* HAS_SETREUID */
710#endif /* HAS_SETRESUID */
a0d0e21e
LW
711 uid = (int)getuid();
712 euid = (int)geteuid();
713 }
714 if (delaymagic & DM_GID) {
715#ifdef HAS_SETRESGID
716 (void)setresgid(gid,egid,(Gid_t)-1);
56febc5e
AD
717#else
718# ifdef HAS_SETREGID
a0d0e21e 719 (void)setregid(gid,egid);
56febc5e
AD
720# else
721# ifdef HAS_SETRGID
a0d0e21e
LW
722 if ((delaymagic & DM_GID) == DM_RGID) {
723 (void)setrgid(gid);
748a9306 724 delaymagic &= ~DM_RGID;
a0d0e21e 725 }
56febc5e
AD
726# endif /* HAS_SETRGID */
727# ifdef HAS_SETEGID
a0d0e21e
LW
728 if ((delaymagic & DM_GID) == DM_EGID) {
729 (void)setegid(gid);
748a9306 730 delaymagic &= ~DM_EGID;
a0d0e21e 731 }
56febc5e 732# endif /* HAS_SETEGID */
a0d0e21e
LW
733 if (delaymagic & DM_GID) {
734 if (gid != egid)
735 DIE("No setregid available");
736 (void)setgid(gid);
737 }
56febc5e
AD
738# endif /* HAS_SETREGID */
739#endif /* HAS_SETRESGID */
a0d0e21e
LW
740 gid = (int)getgid();
741 egid = (int)getegid();
742 }
4633a7c4 743 tainting |= (uid && (euid != uid || egid != gid));
a0d0e21e
LW
744 }
745 delaymagic = 0;
54310121
PP
746
747 gimme = GIMME_V;
748 if (gimme == G_VOID)
749 SP = firstrelem - 1;
750 else if (gimme == G_SCALAR) {
751 dTARGET;
752 SP = firstrelem;
753 SETi(lastrelem - firstrelem + 1);
754 }
755 else {
a0d0e21e
LW
756 if (ary || hash)
757 SP = lastrelem;
758 else
759 SP = firstrelem + (lastlelem - firstlelem);
0c8c7a05 760 lelem = firstlelem + (relem - firstrelem);
5f05dabc 761 while (relem <= SP)
0c8c7a05 762 *relem++ = (lelem <= lastlelem) ? *lelem++ : &sv_undef;
a0d0e21e 763 }
54310121 764 RETURN;
a0d0e21e
LW
765}
766
767PP(pp_match)
768{
769 dSP; dTARG;
770 register PMOP *pm = cPMOP;
771 register char *t;
772 register char *s;
773 char *strend;
774 I32 global;
775 I32 safebase;
776 char *truebase;
777 register REGEXP *rx = pm->op_pmregexp;
778 I32 gimme = GIMME;
779 STRLEN len;
748a9306 780 I32 minmatch = 0;
4633a7c4 781 I32 oldsave = savestack_ix;
f86702cc 782 I32 update_minmatch = 1;
a0d0e21e
LW
783
784 if (op->op_flags & OPf_STACKED)
785 TARG = POPs;
786 else {
787 TARG = GvSV(defgv);
788 EXTEND(SP,1);
789 }
790 s = SvPV(TARG, len);
791 strend = s + len;
792 if (!s)
793 DIE("panic: do_match");
9212bbba 794 TAINT_NOT;
a0d0e21e
LW
795
796 if (pm->op_pmflags & PMf_USED) {
797 if (gimme == G_ARRAY)
798 RETURN;
799 RETPUSHNO;
800 }
801
802 if (!rx->prelen && curpm) {
803 pm = curpm;
804 rx = pm->op_pmregexp;
805 }
806 truebase = t = s;
807 if (global = pm->op_pmflags & PMf_GLOBAL) {
808 rx->startp[0] = 0;
809 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
810 MAGIC* mg = mg_find(TARG, 'g');
748a9306 811 if (mg && mg->mg_len >= 0) {
a0d0e21e 812 rx->endp[0] = rx->startp[0] = s + mg->mg_len;
748a9306 813 minmatch = (mg->mg_flags & MGf_MINMATCH);
f86702cc 814 update_minmatch = 0;
748a9306 815 }
a0d0e21e
LW
816 }
817 }
748a9306
LW
818 if (!rx->nparens && !global)
819 gimme = G_SCALAR; /* accidental array context? */
137443ea
PP
820 safebase = (((gimme == G_ARRAY) || global || !rx->nparens)
821 && !sawampersand);
a0d0e21e
LW
822 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
823 SAVEINT(multiline);
824 multiline = pm->op_pmflags & PMf_MULTILINE;
825 }
826
827play_it_again:
828 if (global && rx->startp[0]) {
829 t = s = rx->endp[0];
51aa1f50 830 if ((s + rx->minlen) > strend)
a0d0e21e 831 goto nope;
f86702cc
PP
832 if (update_minmatch++)
833 minmatch = (s == rx->startp[0]);
a0d0e21e
LW
834 }
835 if (pm->op_pmshort) {
836 if (pm->op_pmflags & PMf_SCANFIRST) {
837 if (SvSCREAM(TARG)) {
838 if (screamfirst[BmRARE(pm->op_pmshort)] < 0)
839 goto nope;
840 else if (!(s = screaminstr(TARG, pm->op_pmshort)))
841 goto nope;
842 else if (pm->op_pmflags & PMf_ALL)
843 goto yup;
844 }
845 else if (!(s = fbm_instr((unsigned char*)s,
846 (unsigned char*)strend, pm->op_pmshort)))
847 goto nope;
848 else if (pm->op_pmflags & PMf_ALL)
849 goto yup;
850 if (s && rx->regback >= 0) {
851 ++BmUSEFUL(pm->op_pmshort);
852 s -= rx->regback;
853 if (s < t)
854 s = t;
855 }
856 else
857 s = t;
858 }
859 else if (!multiline) {
bbce6d69
PP
860 if (*SvPVX(pm->op_pmshort) != *s
861 || (pm->op_pmslen > 1
36477c24 862 && memNE(SvPVX(pm->op_pmshort), s, pm->op_pmslen)))
bbce6d69 863 goto nope;
a0d0e21e
LW
864 }
865 if (!rx->naughty && --BmUSEFUL(pm->op_pmshort) < 0) {
866 SvREFCNT_dec(pm->op_pmshort);
867 pm->op_pmshort = Nullsv; /* opt is being useless */
868 }
869 }
e50aee73 870 if (pregexec(rx, s, strend, truebase, minmatch,
bbce6d69
PP
871 SvSCREAM(TARG) ? TARG : Nullsv, safebase))
872 {
a0d0e21e
LW
873 curpm = pm;
874 if (pm->op_pmflags & PMf_ONCE)
875 pm->op_pmflags |= PMf_USED;
876 goto gotcha;
877 }
878 else
879 goto ret_no;
880 /*NOTREACHED*/
881
882 gotcha:
9212bbba 883 TAINT_IF(rx->exec_tainted);
a0d0e21e
LW
884 if (gimme == G_ARRAY) {
885 I32 iters, i, len;
886
887 iters = rx->nparens;
888 if (global && !iters)
889 i = 1;
890 else
891 i = 0;
892 EXTEND(SP, iters + i);
bbce6d69 893 EXTEND_MORTAL(iters + i);
a0d0e21e
LW
894 for (i = !i; i <= iters; i++) {
895 PUSHs(sv_newmortal());
896 /*SUPPRESS 560*/
897 if ((s = rx->startp[i]) && rx->endp[i] ) {
898 len = rx->endp[i] - s;
899 sv_setpvn(*SP, s, len);
900 }
901 }
902 if (global) {
903 truebase = rx->subbeg;
5f05dabc 904 strend = rx->subend;
a0d0e21e
LW
905 if (rx->startp[0] && rx->startp[0] == rx->endp[0])
906 ++rx->endp[0];
907 goto play_it_again;
908 }
4633a7c4 909 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
910 RETURN;
911 }
912 else {
913 if (global) {
914 MAGIC* mg = 0;
915 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
916 mg = mg_find(TARG, 'g');
917 if (!mg) {
918 sv_magic(TARG, (SV*)0, 'g', Nullch, 0);
919 mg = mg_find(TARG, 'g');
920 }
748a9306 921 if (rx->startp[0]) {
5f05dabc 922 mg->mg_len = rx->endp[0] - rx->subbeg;
748a9306
LW
923 if (rx->startp[0] == rx->endp[0])
924 mg->mg_flags |= MGf_MINMATCH;
925 else
926 mg->mg_flags &= ~MGf_MINMATCH;
927 }
a0d0e21e 928 }
4633a7c4 929 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
930 RETPUSHYES;
931 }
932
933yup:
9212bbba 934 TAINT_IF(rx->exec_tainted);
a0d0e21e
LW
935 ++BmUSEFUL(pm->op_pmshort);
936 curpm = pm;
937 if (pm->op_pmflags & PMf_ONCE)
938 pm->op_pmflags |= PMf_USED;
5f05dabc
PP
939 Safefree(rx->subbase);
940 rx->subbase = Nullch;
a0d0e21e
LW
941 if (global) {
942 rx->subbeg = truebase;
943 rx->subend = strend;
944 rx->startp[0] = s;
945 rx->endp[0] = s + SvCUR(pm->op_pmshort);
946 goto gotcha;
947 }
948 if (sawampersand) {
949 char *tmps;
950
a0d0e21e
LW
951 tmps = rx->subbase = savepvn(t, strend-t);
952 rx->subbeg = tmps;
953 rx->subend = tmps + (strend-t);
954 tmps = rx->startp[0] = tmps + (s - t);
955 rx->endp[0] = tmps + SvCUR(pm->op_pmshort);
956 }
4633a7c4 957 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
958 RETPUSHYES;
959
960nope:
961 if (pm->op_pmshort)
962 ++BmUSEFUL(pm->op_pmshort);
963
964ret_no:
4633a7c4 965 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
966 if (gimme == G_ARRAY)
967 RETURN;
968 RETPUSHNO;
969}
970
971OP *
972do_readline()
973{
974 dSP; dTARGETSTACKED;
975 register SV *sv;
976 STRLEN tmplen = 0;
977 STRLEN offset;
760ac839 978 PerlIO *fp;
a0d0e21e
LW
979 register IO *io = GvIO(last_in_gv);
980 register I32 type = op->op_type;
54310121 981 I32 gimme = GIMME_V;
e79b0511 982 MAGIC *mg;
a0d0e21e 983
e79b0511
PP
984 if (SvMAGICAL(last_in_gv) && (mg = mg_find((SV*)last_in_gv, 'q'))) {
985 PUSHMARK(SP);
986 XPUSHs(mg->mg_obj);
987 PUTBACK;
988 ENTER;
54310121 989 perl_call_method("READLINE", gimme);
e79b0511
PP
990 LEAVE;
991 SPAGAIN;
54310121
PP
992 if (gimme == G_SCALAR)
993 SvSetMagicSV_nosteal(TARG, TOPs);
e79b0511
PP
994 RETURN;
995 }
a0d0e21e
LW
996 fp = Nullfp;
997 if (io) {
998 fp = IoIFP(io);
999 if (!fp) {
1000 if (IoFLAGS(io) & IOf_ARGV) {
1001 if (IoFLAGS(io) & IOf_START) {
1002 IoFLAGS(io) &= ~IOf_START;
1003 IoLINES(io) = 0;
1004 if (av_len(GvAVn(last_in_gv)) < 0) {
1005 SV *tmpstr = newSVpv("-", 1); /* assume stdin */
1006 av_push(GvAVn(last_in_gv), tmpstr);
1007 }
1008 }
1009 fp = nextargv(last_in_gv);
1010 if (!fp) { /* Note: fp != IoIFP(io) */
1011 (void)do_close(last_in_gv, FALSE); /* now it does*/
1012 IoFLAGS(io) |= IOf_START;
1013 }
1014 }
1015 else if (type == OP_GLOB) {
1016 SV *tmpcmd = NEWSV(55, 0);
1017 SV *tmpglob = POPs;
1018 ENTER;
1019 SAVEFREESV(tmpcmd);
1020#ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
1021 /* since spawning off a process is a real performance hit */
1022 {
1023#include <descrip.h>
1024#include <lib$routines.h>
1025#include <nam.h>
1026#include <rmsdef.h>
1027 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
1028 char vmsspec[NAM$C_MAXRSS+1];
1029 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
1030 char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
1031 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
760ac839 1032 PerlIO *tmpfp;
a0d0e21e
LW
1033 STRLEN i;
1034 struct dsc$descriptor_s wilddsc
1035 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
1036 struct dsc$descriptor_vs rsdsc
1037 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
1038 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
1039
1040 /* We could find out if there's an explicit dev/dir or version
1041 by peeking into lib$find_file's internal context at
1042 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
1043 but that's unsupported, so I don't want to do it now and
1044 have it bite someone in the future. */
1045 strcat(tmpfnam,tmpnam(NULL));
1046 cp = SvPV(tmpglob,i);
1047 for (; i; i--) {
1048 if (cp[i] == ';') hasver = 1;
1049 if (cp[i] == '.') {
1050 if (sts) hasver = 1;
1051 else sts = 1;
1052 }
1053 if (cp[i] == '/') {
1054 hasdir = isunix = 1;
1055 break;
748a9306 1056 }
a0d0e21e
LW
1057 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
1058 hasdir = 1;
1059 break;
1060 }
1061 }
760ac839 1062 if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
a0d0e21e
LW
1063 ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
1064 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
1065 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
1066 &dfltdsc,NULL,NULL,NULL))&1)) {
1067 end = rstr + (unsigned long int) *rslt;
1068 if (!hasver) while (*end != ';') end--;
1069 *(end++) = '\n'; *end = '\0';
1070 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
1071 if (hasdir) {
f86702cc 1072 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
a0d0e21e
LW
1073 begin = rstr;
1074 }
1075 else {
1076 begin = end;
1077 while (*(--begin) != ']' && *begin != '>') ;
1078 ++begin;
1079 }
760ac839 1080 ok = (PerlIO_puts(tmpfp,begin) != EOF);
a0d0e21e
LW
1081 }
1082 if (cxt) (void)lib$find_file_end(&cxt);
748a9306
LW
1083 if (ok && sts != RMS$_NMF &&
1084 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
a0d0e21e 1085 if (!ok) {
c07a80fd
PP
1086 if (!(sts & 1)) {
1087 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
1088 }
760ac839 1089 PerlIO_close(tmpfp);
a0d0e21e
LW
1090 fp = NULL;
1091 }
1092 else {
760ac839 1093 PerlIO_rewind(tmpfp);
a0d0e21e
LW
1094 IoTYPE(io) = '<';
1095 IoIFP(io) = fp = tmpfp;
1e422769 1096 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
a0d0e21e
LW
1097 }
1098 }
1099 }
1100#else /* !VMS */
1101#ifdef DOSISH
67955e0c
PP
1102#ifdef OS2
1103 sv_setpv(tmpcmd, "for a in ");
1104 sv_catsv(tmpcmd, tmpglob);
1105 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
1106#else
a0d0e21e
LW
1107 sv_setpv(tmpcmd, "perlglob ");
1108 sv_catsv(tmpcmd, tmpglob);
1109 sv_catpv(tmpcmd, " |");
67955e0c
PP
1110#endif /* !OS2 */
1111#else /* !DOSISH */
1112#if defined(CSH)
a0d0e21e
LW
1113 sv_setpvn(tmpcmd, cshname, cshlen);
1114 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
1115 sv_catsv(tmpcmd, tmpglob);
16d20bd9 1116 sv_catpv(tmpcmd, "' 2>/dev/null |");
a0d0e21e
LW
1117#else
1118 sv_setpv(tmpcmd, "echo ");
1119 sv_catsv(tmpcmd, tmpglob);
1120#if 'z' - 'a' == 25
1121 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
1122#else
1123 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
1124#endif
1125#endif /* !CSH */
67955e0c 1126#endif /* !DOSISH */
c07a80fd
PP
1127 (void)do_open(last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
1128 FALSE, 0, 0, Nullfp);
a0d0e21e
LW
1129 fp = IoIFP(io);
1130#endif /* !VMS */
1131 LEAVE;
1132 }
1133 }
1134 else if (type == OP_GLOB)
1135 SP--;
1136 }
1137 if (!fp) {
4633a7c4 1138 if (dowarn && io && !(IoFLAGS(io) & IOf_START))
a0d0e21e 1139 warn("Read on closed filehandle <%s>", GvENAME(last_in_gv));
54310121 1140 if (gimme == G_SCALAR) {
a0d0e21e
LW
1141 (void)SvOK_off(TARG);
1142 PUSHTARG;
1143 }
1144 RETURN;
1145 }
54310121 1146 if (gimme == G_SCALAR) {
a0d0e21e 1147 sv = TARG;
9607fc9c
PP
1148 if (SvROK(sv))
1149 sv_unref(sv);
a0d0e21e
LW
1150 (void)SvUPGRADE(sv, SVt_PV);
1151 tmplen = SvLEN(sv); /* remember if already alloced */
1152 if (!tmplen)
1153 Sv_Grow(sv, 80); /* try short-buffering it */
1154 if (type == OP_RCATLINE)
1155 offset = SvCUR(sv);
1156 else
1157 offset = 0;
1158 }
54310121
PP
1159 else {
1160 sv = sv_2mortal(NEWSV(57, 80));
1161 offset = 0;
1162 }
a0d0e21e
LW
1163 for (;;) {
1164 if (!sv_gets(sv, fp, offset)) {
760ac839 1165 PerlIO_clearerr(fp);
a0d0e21e
LW
1166 if (IoFLAGS(io) & IOf_ARGV) {
1167 fp = nextargv(last_in_gv);
1168 if (fp)
1169 continue;
1170 (void)do_close(last_in_gv, FALSE);
1171 IoFLAGS(io) |= IOf_START;
1172 }
1173 else if (type == OP_GLOB) {
5cd24f17
PP
1174 if (do_close(last_in_gv, FALSE) & ~0xFF)
1175 warn("internal error: glob failed");
a0d0e21e 1176 }
54310121 1177 if (gimme == G_SCALAR) {
a0d0e21e
LW
1178 (void)SvOK_off(TARG);
1179 PUSHTARG;
1180 }
1181 RETURN;
1182 }
bbce6d69
PP
1183 /* This should not be marked tainted if the fp is marked clean */
1184 if (!(IoFLAGS(io) & IOf_UNTAINT)) {
1185 TAINT;
1186 SvTAINTED_on(sv);
1187 }
a0d0e21e 1188 IoLINES(io)++;
71be2cbc 1189 SvSETMAGIC(sv);
a0d0e21e 1190 XPUSHs(sv);
a0d0e21e
LW
1191 if (type == OP_GLOB) {
1192 char *tmps;
1193
c07a80fd
PP
1194 if (SvCUR(sv) > 0 && SvCUR(rs) > 0) {
1195 tmps = SvEND(sv) - 1;
1196 if (*tmps == *SvPVX(rs)) {
1197 *tmps = '\0';
1198 SvCUR(sv)--;
1199 }
1200 }
a0d0e21e
LW
1201 for (tmps = SvPVX(sv); *tmps; tmps++)
1202 if (!isALPHA(*tmps) && !isDIGIT(*tmps) &&
1203 strchr("$&*(){}[]'\";\\|?<>~`", *tmps))
1204 break;
1205 if (*tmps && Stat(SvPVX(sv), &statbuf) < 0) {
1206 (void)POPs; /* Unmatched wildcard? Chuck it... */
1207 continue;
1208 }
1209 }
54310121 1210 if (gimme == G_ARRAY) {
a0d0e21e
LW
1211 if (SvLEN(sv) - SvCUR(sv) > 20) {
1212 SvLEN_set(sv, SvCUR(sv)+1);
1213 Renew(SvPVX(sv), SvLEN(sv), char);
1214 }
1215 sv = sv_2mortal(NEWSV(58, 80));
1216 continue;
1217 }
54310121 1218 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
a0d0e21e
LW
1219 /* try to reclaim a bit of scalar space (only on 1st alloc) */
1220 if (SvCUR(sv) < 60)
1221 SvLEN_set(sv, 80);
1222 else
1223 SvLEN_set(sv, SvCUR(sv)+40); /* allow some slop */
1224 Renew(SvPVX(sv), SvLEN(sv), char);
1225 }
1226 RETURN;
1227 }
1228}
1229
1230PP(pp_enter)
1231{
1232 dSP;
1233 register CONTEXT *cx;
54310121 1234 I32 gimme = OP_GIMME(op, -1);
a0d0e21e 1235
54310121
PP
1236 if (gimme == -1) {
1237 if (cxstack_ix >= 0)
1238 gimme = cxstack[cxstack_ix].blk_gimme;
1239 else
1240 gimme = G_SCALAR;
1241 }
a0d0e21e
LW
1242
1243 ENTER;
1244
1245 SAVETMPS;
1246 PUSHBLOCK(cx, CXt_BLOCK, sp);
1247
1248 RETURN;
1249}
1250
1251PP(pp_helem)
1252{
1253 dSP;
760ac839 1254 HE* he;
a0d0e21e 1255 SV *keysv = POPs;
a0d0e21e 1256 HV *hv = (HV*)POPs;
68dc0745
PP
1257 U32 lval = op->op_flags & OPf_MOD;
1258 U32 defer = op->op_private & OPpLVAL_DEFER;
a0d0e21e
LW
1259
1260 if (SvTYPE(hv) != SVt_PVHV)
1261 RETPUSHUNDEF;
68dc0745 1262 he = hv_fetch_ent(hv, keysv, lval && !defer, 0);
a0d0e21e 1263 if (lval) {
68dc0745
PP
1264 if (!he || HeVAL(he) == &sv_undef) {
1265 SV* lv;
1266 SV* key2;
1267 if (!defer)
1268 DIE(no_helem, SvPV(keysv, na));
1269 lv = sv_newmortal();
1270 sv_upgrade(lv, SVt_PVLV);
1271 LvTYPE(lv) = 'y';
1272 sv_magic(lv, key2 = newSVsv(keysv), 'y', Nullch, 0);
1273 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1274 LvTARG(lv) = SvREFCNT_inc(hv);
1275 LvTARGLEN(lv) = 1;
1276 PUSHs(lv);
1277 RETURN;
1278 }
5f05dabc
PP
1279 if (op->op_private & OPpLVAL_INTRO) {
1280 if (HvNAME(hv) && isGV(HeVAL(he)))
1281 save_gp((GV*)HeVAL(he), !(op->op_flags & OPf_SPECIAL));
1282 else
1283 save_svref(&HeVAL(he));
1284 }
1285 else if (op->op_private & OPpDEREF)
68dc0745 1286 vivify_ref(HeVAL(he), op->op_private & OPpDEREF);
a0d0e21e 1287 }
760ac839 1288 PUSHs(he ? HeVAL(he) : &sv_undef);
a0d0e21e
LW
1289 RETURN;
1290}
1291
1292PP(pp_leave)
1293{
1294 dSP;
1295 register CONTEXT *cx;
1296 register SV **mark;
1297 SV **newsp;
1298 PMOP *newpm;
1299 I32 gimme;
1300
1301 if (op->op_flags & OPf_SPECIAL) {
1302 cx = &cxstack[cxstack_ix];
1303 cx->blk_oldpm = curpm; /* fake block should preserve $1 et al */
1304 }
1305
1306 POPBLOCK(cx,newpm);
1307
54310121
PP
1308 gimme = OP_GIMME(op, -1);
1309 if (gimme == -1) {
1310 if (cxstack_ix >= 0)
1311 gimme = cxstack[cxstack_ix].blk_gimme;
1312 else
1313 gimme = G_SCALAR;
1314 }
a0d0e21e 1315
54310121
PP
1316 if (gimme == G_VOID)
1317 SP = newsp;
1318 else if (gimme == G_SCALAR) {
1319 MARK = newsp + 1;
1320 if (MARK <= SP)
1321 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1322 *MARK = TOPs;
1323 else
1324 *MARK = sv_mortalcopy(TOPs);
a0d0e21e 1325 else {
54310121
PP
1326 MEXTEND(mark,0);
1327 *MARK = &sv_undef;
a0d0e21e 1328 }
54310121 1329 SP = MARK;
a0d0e21e 1330 }
54310121 1331 else if (gimme == G_ARRAY) {
a0d0e21e
LW
1332 for (mark = newsp + 1; mark <= SP; mark++)
1333 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP)))
1334 *mark = sv_mortalcopy(*mark);
1335 /* in case LEAVE wipes old return values */
1336 }
1337 curpm = newpm; /* Don't pop $1 et al till now */
1338
1339 LEAVE;
1340
1341 RETURN;
1342}
1343
1344PP(pp_iter)
1345{
1346 dSP;
1347 register CONTEXT *cx;
5f05dabc 1348 SV* sv;
4633a7c4 1349 AV* av;
a0d0e21e
LW
1350
1351 EXTEND(sp, 1);
1352 cx = &cxstack[cxstack_ix];
1353 if (cx->cx_type != CXt_LOOP)
1354 DIE("panic: pp_iter");
1355
71be2cbc
PP
1356 av = cx->blk_loop.iterary;
1357 if (cx->blk_loop.iterix >= (av == curstack ? cx->blk_oldsp : AvFILL(av)))
a0d0e21e
LW
1358 RETPUSHNO;
1359
7a4c00b4
PP
1360 SvREFCNT_dec(*cx->blk_loop.itervar);
1361
5f05dabc 1362 if (sv = AvARRAY(av)[++cx->blk_loop.iterix])
a0d0e21e 1363 SvTEMP_off(sv);
a0d0e21e 1364 else
5f05dabc
PP
1365 sv = &sv_undef;
1366 if (av != curstack && SvIMMORTAL(sv)) {
1367 SV *lv = cx->blk_loop.iterlval;
71be2cbc
PP
1368 if (lv && SvREFCNT(lv) > 1) {
1369 SvREFCNT_dec(lv);
1370 lv = Nullsv;
1371 }
5f05dabc
PP
1372 if (lv)
1373 SvREFCNT_dec(LvTARG(lv));
1374 else {
68dc0745 1375 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
5f05dabc 1376 sv_upgrade(lv, SVt_PVLV);
5f05dabc 1377 LvTYPE(lv) = 'y';
68dc0745 1378 sv_magic(lv, Nullsv, 'y', Nullch, 0);
5f05dabc
PP
1379 }
1380 LvTARG(lv) = SvREFCNT_inc(av);
1381 LvTARGOFF(lv) = cx->blk_loop.iterix;
68dc0745 1382 LvTARGLEN(lv) = -1;
5f05dabc
PP
1383 sv = (SV*)lv;
1384 }
7a4c00b4
PP
1385
1386 *cx->blk_loop.itervar = SvREFCNT_inc(sv);
a0d0e21e
LW
1387 RETPUSHYES;
1388}
1389
1390PP(pp_subst)
1391{
1392 dSP; dTARG;
1393 register PMOP *pm = cPMOP;
1394 PMOP *rpm = pm;
1395 register SV *dstr;
1396 register char *s;
1397 char *strend;
1398 register char *m;
1399 char *c;
1400 register char *d;
1401 STRLEN clen;
1402 I32 iters = 0;
1403 I32 maxiters;
1404 register I32 i;
1405 bool once;
71be2cbc 1406 bool rxtainted;
a0d0e21e
LW
1407 char *orig;
1408 I32 safebase;
1409 register REGEXP *rx = pm->op_pmregexp;
1410 STRLEN len;
1411 int force_on_match = 0;
4633a7c4 1412 I32 oldsave = savestack_ix;
a0d0e21e 1413
5cd24f17
PP
1414 /* known replacement string? */
1415 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
a0d0e21e
LW
1416 if (op->op_flags & OPf_STACKED)
1417 TARG = POPs;
1418 else {
1419 TARG = GvSV(defgv);
1420 EXTEND(SP,1);
1421 }
68dc0745
PP
1422 if (SvREADONLY(TARG)
1423 || (SvTYPE(TARG) > SVt_PVLV
1424 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG))))
1425 croak(no_modify);
a0d0e21e 1426 s = SvPV(TARG, len);
68dc0745 1427 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
a0d0e21e 1428 force_on_match = 1;
9212bbba 1429 TAINT_NOT;
a0d0e21e
LW
1430
1431 force_it:
1432 if (!pm || !s)
1433 DIE("panic: do_subst");
1434
1435 strend = s + len;
1436 maxiters = (strend - s) + 10;
1437
1438 if (!rx->prelen && curpm) {
1439 pm = curpm;
1440 rx = pm->op_pmregexp;
1441 }
5f05dabc 1442 safebase = (!rx->nparens && !sawampersand);
a0d0e21e
LW
1443 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
1444 SAVEINT(multiline);
1445 multiline = pm->op_pmflags & PMf_MULTILINE;
1446 }
1447 orig = m = s;
1448 if (pm->op_pmshort) {
1449 if (pm->op_pmflags & PMf_SCANFIRST) {
1450 if (SvSCREAM(TARG)) {
1451 if (screamfirst[BmRARE(pm->op_pmshort)] < 0)
1452 goto nope;
1453 else if (!(s = screaminstr(TARG, pm->op_pmshort)))
1454 goto nope;
1455 }
1456 else if (!(s = fbm_instr((unsigned char*)s, (unsigned char*)strend,
1457 pm->op_pmshort)))
1458 goto nope;
1459 if (s && rx->regback >= 0) {
1460 ++BmUSEFUL(pm->op_pmshort);
1461 s -= rx->regback;
1462 if (s < m)
1463 s = m;
1464 }
1465 else
1466 s = m;
1467 }
1468 else if (!multiline) {
bbce6d69
PP
1469 if (*SvPVX(pm->op_pmshort) != *s
1470 || (pm->op_pmslen > 1
36477c24 1471 && memNE(SvPVX(pm->op_pmshort), s, pm->op_pmslen)))
bbce6d69 1472 goto nope;
a0d0e21e
LW
1473 }
1474 if (!rx->naughty && --BmUSEFUL(pm->op_pmshort) < 0) {
1475 SvREFCNT_dec(pm->op_pmshort);
1476 pm->op_pmshort = Nullsv; /* opt is being useless */
1477 }
1478 }
71be2cbc
PP
1479
1480 /* only replace once? */
a0d0e21e 1481 once = !(rpm->op_pmflags & PMf_GLOBAL);
71be2cbc
PP
1482
1483 /* known replacement string? */
5cd24f17 1484 c = dstr ? SvPV(dstr, clen) : Nullch;
71be2cbc
PP
1485
1486 /* can do inplace substitution? */
137443ea 1487 if (c && clen <= rx->minlen && safebase) {
71be2cbc
PP
1488 if (! pregexec(rx, s, strend, orig, 0,
1489 SvSCREAM(TARG) ? TARG : Nullsv, safebase)) {
1490 PUSHs(&sv_no);
1491 LEAVE_SCOPE(oldsave);
1492 RETURN;
1493 }
1494 if (force_on_match) {
1495 force_on_match = 0;
1496 s = SvPV_force(TARG, len);
1497 goto force_it;
1498 }
71be2cbc
PP
1499 d = s;
1500 curpm = pm;
1501 SvSCREAM_off(TARG); /* disable possible screamer */
1502 if (once) {
1503 rxtainted = rx->exec_tainted;
1504 m = rx->startp[0];
1505 d = rx->endp[0];
1506 s = orig;
1507 if (m - s > strend - d) { /* faster to shorten from end */
1508 if (clen) {
1509 Copy(c, m, clen, char);
1510 m += clen;
a0d0e21e 1511 }
71be2cbc
PP
1512 i = strend - d;
1513 if (i > 0) {
1514 Move(d, m, i, char);
1515 m += i;
a0d0e21e 1516 }
71be2cbc
PP
1517 *m = '\0';
1518 SvCUR_set(TARG, m - s);
1519 }
1520 /*SUPPRESS 560*/
1521 else if (i = m - s) { /* faster from front */
1522 d -= clen;
1523 m = d;
1524 sv_chop(TARG, d-i);
1525 s += i;
1526 while (i--)
1527 *--d = *--s;
1528 if (clen)
1529 Copy(c, m, clen, char);
1530 }
1531 else if (clen) {
1532 d -= clen;
1533 sv_chop(TARG, d);
1534 Copy(c, d, clen, char);
1535 }
1536 else {
1537 sv_chop(TARG, d);
1538 }
9212bbba 1539 TAINT_IF(rxtainted);
71be2cbc
PP
1540 PUSHs(&sv_yes);
1541 }
1542 else {
1543 rxtainted = 0;
1544 do {
1545 if (iters++ > maxiters)
1546 DIE("Substitution loop");
1547 rxtainted |= rx->exec_tainted;
1548 m = rx->startp[0];
1549 /*SUPPRESS 560*/
1550 if (i = m - s) {
1551 if (s != d)
1552 Move(s, d, i, char);
1553 d += i;
a0d0e21e 1554 }
71be2cbc
PP
1555 if (clen) {
1556 Copy(c, d, clen, char);
1557 d += clen;
1558 }
1559 s = rx->endp[0];
1560 } while (pregexec(rx, s, strend, orig, s == m,
1561 Nullsv, TRUE)); /* don't match same null twice */
1562 if (s != d) {
1563 i = strend - s;
1564 SvCUR_set(TARG, d - SvPVX(TARG) + i);
1565 Move(s, d, i+1, char); /* include the NUL */
a0d0e21e 1566 }
9212bbba 1567 TAINT_IF(rxtainted);
71be2cbc 1568 PUSHs(sv_2mortal(newSViv((I32)iters)));
a0d0e21e 1569 }
71be2cbc
PP
1570 (void)SvPOK_only(TARG);
1571 SvSETMAGIC(TARG);
9212bbba 1572 SvTAINT(TARG);
71be2cbc
PP
1573 LEAVE_SCOPE(oldsave);
1574 RETURN;
a0d0e21e 1575 }
71be2cbc 1576
e50aee73 1577 if (pregexec(rx, s, strend, orig, 0,
5f05dabc 1578 SvSCREAM(TARG) ? TARG : Nullsv, safebase)) {
a0d0e21e
LW
1579 if (force_on_match) {
1580 force_on_match = 0;
1581 s = SvPV_force(TARG, len);
1582 goto force_it;
1583 }
71be2cbc 1584 rxtainted = rx->exec_tainted;
a0d0e21e
LW
1585 dstr = NEWSV(25, sv_len(TARG));
1586 sv_setpvn(dstr, m, s-m);
1587 curpm = pm;
1588 if (!c) {
1589 register CONTEXT *cx;
1590 PUSHSUBST(cx);
1591 RETURNOP(cPMOP->op_pmreplroot);
1592 }
1593 do {
1594 if (iters++ > maxiters)
1595 DIE("Substitution loop");
71be2cbc 1596 rxtainted |= rx->exec_tainted;
a0d0e21e
LW
1597 if (rx->subbase && rx->subbase != orig) {
1598 m = s;
1599 s = orig;
1600 orig = rx->subbase;
1601 s = orig + (m - s);
1602 strend = s + (strend - m);
1603 }
1604 m = rx->startp[0];
1605 sv_catpvn(dstr, s, m-s);
1606 s = rx->endp[0];
1607 if (clen)
1608 sv_catpvn(dstr, c, clen);
1609 if (once)
1610 break;
5f05dabc 1611 } while (pregexec(rx, s, strend, orig, s == m, Nullsv, safebase));
a0d0e21e 1612 sv_catpvn(dstr, s, strend - s);
748a9306 1613
9212bbba
CS
1614 TAINT_IF(rxtainted);
1615
4633a7c4 1616 (void)SvOOK_off(TARG);
cb0b1708 1617 Safefree(SvPVX(TARG));
748a9306
LW
1618 SvPVX(TARG) = SvPVX(dstr);
1619 SvCUR_set(TARG, SvCUR(dstr));
1620 SvLEN_set(TARG, SvLEN(dstr));
1621 SvPVX(dstr) = 0;
1622 sv_free(dstr);
1623
a0d0e21e
LW
1624 (void)SvPOK_only(TARG);
1625 SvSETMAGIC(TARG);
9212bbba 1626 SvTAINT(TARG);
a0d0e21e 1627 PUSHs(sv_2mortal(newSViv((I32)iters)));
4633a7c4 1628 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1629 RETURN;
1630 }
5cd24f17 1631 goto ret_no;
a0d0e21e
LW
1632
1633nope:
1634 ++BmUSEFUL(pm->op_pmshort);
5cd24f17
PP
1635
1636ret_no:
a0d0e21e 1637 PUSHs(&sv_no);
4633a7c4 1638 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1639 RETURN;
1640}
1641
1642PP(pp_grepwhile)
1643{
1644 dSP;
1645
1646 if (SvTRUEx(POPs))
1647 stack_base[markstack_ptr[-1]++] = stack_base[*markstack_ptr];
1648 ++*markstack_ptr;
1649 LEAVE; /* exit inner scope */
1650
1651 /* All done yet? */
1652 if (stack_base + *markstack_ptr > sp) {
1653 I32 items;
54310121 1654 I32 gimme = GIMME_V;
a0d0e21e
LW
1655
1656 LEAVE; /* exit outer scope */
1657 (void)POPMARK; /* pop src */
1658 items = --*markstack_ptr - markstack_ptr[-1];
1659 (void)POPMARK; /* pop dst */
1660 SP = stack_base + POPMARK; /* pop original mark */
54310121 1661 if (gimme == G_SCALAR) {
a0d0e21e
LW
1662 dTARGET;
1663 XPUSHi(items);
a0d0e21e 1664 }
54310121
PP
1665 else if (gimme == G_ARRAY)
1666 SP += items;
a0d0e21e
LW
1667 RETURN;
1668 }
1669 else {
1670 SV *src;
1671
1672 ENTER; /* enter inner scope */
1673 SAVESPTR(curpm);
1674
1675 src = stack_base[*markstack_ptr];
1676 SvTEMP_off(src);
1677 GvSV(defgv) = src;
1678
1679 RETURNOP(cLOGOP->op_other);
1680 }
1681}
1682
1683PP(pp_leavesub)
1684{
1685 dSP;
1686 SV **mark;
1687 SV **newsp;
1688 PMOP *newpm;
1689 I32 gimme;
1690 register CONTEXT *cx;
f86702cc 1691 struct block_sub cxsub;
a0d0e21e
LW
1692
1693 POPBLOCK(cx,newpm);
f86702cc
PP
1694 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1695
a0d0e21e
LW
1696 if (gimme == G_SCALAR) {
1697 MARK = newsp + 1;
1698 if (MARK <= SP)
f86702cc 1699 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
a0d0e21e 1700 else {
f86702cc 1701 MEXTEND(MARK, 0);
a0d0e21e
LW
1702 *MARK = &sv_undef;
1703 }
1704 SP = MARK;
1705 }
54310121 1706 else if (gimme == G_ARRAY) {
f86702cc
PP
1707 for (MARK = newsp + 1; MARK <= SP; MARK++) {
1708 if (!SvTEMP(*MARK))
1709 *MARK = sv_mortalcopy(*MARK);
1710 }
a0d0e21e 1711 }
f86702cc
PP
1712 PUTBACK;
1713
1714 POPSUB2(); /* Stack values are safe: release CV and @_ ... */
1715 curpm = newpm; /* ... and pop $1 et al */
a0d0e21e
LW
1716
1717 LEAVE;
a0d0e21e
LW
1718 return pop_return();
1719}
1720
1721PP(pp_entersub)
1722{
1723 dSP; dPOPss;
1724 GV *gv;
1725 HV *stash;
1726 register CV *cv;
1727 register CONTEXT *cx;
5d94fbed 1728 I32 gimme;
07055b4c 1729 bool hasargs = (op->op_flags & OPf_STACKED) != 0;
a0d0e21e
LW
1730
1731 if (!sv)
1732 DIE("Not a CODE reference");
1733 switch (SvTYPE(sv)) {
1734 default:
1735 if (!SvROK(sv)) {
748a9306
LW
1736 char *sym;
1737
a0d0e21e
LW
1738 if (sv == &sv_yes) /* unfound import, ignore */
1739 RETURN;
15ff848f
CS
1740 if (SvGMAGICAL(sv)) {
1741 mg_get(sv);
1742 sym = SvPOKp(sv) ? SvPVX(sv) : Nullch;
1743 }
1744 else
1745 sym = SvPV(sv, na);
1746 if (!sym)
a0d0e21e
LW
1747 DIE(no_usym, "a subroutine");
1748 if (op->op_private & HINT_STRICT_REFS)
748a9306
LW
1749 DIE(no_symref, sym, "a subroutine");
1750 cv = perl_get_cv(sym, TRUE);
a0d0e21e
LW
1751 break;
1752 }
1753 cv = (CV*)SvRV(sv);
28757baa 1754 if (SvTYPE(cv) == SVt_PVCV)
a0d0e21e
LW
1755 break;
1756 /* FALL THROUGH */
1757 case SVt_PVHV:
1758 case SVt_PVAV:
1759 DIE("Not a CODE reference");
1760 case SVt_PVCV:
1761 cv = (CV*)sv;
1762 break;
1763 case SVt_PVGV:
8ebc5c01 1764 if (!(cv = GvCVu((GV*)sv)))
a0d0e21e
LW
1765 cv = sv_2cv(sv, &stash, &gv, TRUE);
1766 break;
1767 }
1768
8ebc5c01
PP
1769 ENTER;
1770 SAVETMPS;
1771
a0d0e21e
LW
1772 retry:
1773 if (!cv)
1774 DIE("Not a CODE reference");
1775
1776 if (!CvROOT(cv) && !CvXSUB(cv)) {
44a8e56a
PP
1777 GV* autogv;
1778 SV* subname;
1779
1780 /* anonymous or undef'd function leaves us no recourse */
1781 if (CvANON(cv) || !(gv = CvGV(cv)))
1782 DIE("Undefined subroutine called");
1783 /* autoloaded stub? */
1784 if (cv != GvCV(gv)) {
1785 cv = GvCV(gv);
1786 goto retry;
a0d0e21e 1787 }
44a8e56a 1788 /* should call AUTOLOAD now? */
54310121
PP
1789 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
1790 FALSE)))
1791 {
44a8e56a
PP
1792 cv = GvCV(autogv);
1793 goto retry;
1794 }
1795 /* sorry */
1796 subname = sv_newmortal();
1797 gv_efullname3(subname, gv, Nullch);
1798 DIE("Undefined subroutine &%s called", SvPVX(subname));
a0d0e21e
LW
1799 }
1800
54310121 1801 gimme = GIMME_V;
28757baa 1802 if ((op->op_private & OPpENTERSUB_DB) && GvCV(DBsub) && !CvNODEBUG(cv)) {
71be2cbc 1803 SV *oldsv = sv;
a0d0e21e
LW
1804 sv = GvSV(DBsub);
1805 save_item(sv);
67955e0c 1806 gv = CvGV(cv);
71be2cbc
PP
1807 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
1808 || strEQ(GvNAME(gv), "END")
1809 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
1810 !( (SvTYPE(oldsv) == SVt_PVGV) && (GvCV((GV*)oldsv) == cv)
1811 && (gv = (GV*)oldsv) ))) { /* Use GV from the stack as a fallback. */
1812 /* GV is potentially non-unique, or contain different CV. */
748a9306 1813 sv_setsv(sv, newRV((SV*)cv));
a5f75d66 1814 }
748a9306 1815 else {
4f732624 1816 gv_efullname3(sv, gv, Nullch);
748a9306 1817 }
a0d0e21e 1818 cv = GvCV(DBsub);
67955e0c 1819 if (CvXSUB(cv)) curcopdb = curcop;
a0d0e21e
LW
1820 if (!cv)
1821 DIE("No DBsub routine");
1822 }
1823
1824 if (CvXSUB(cv)) {
1825 if (CvOLDSTYLE(cv)) {
ecfc5424 1826 I32 (*fp3)_((int,int,int));
a0d0e21e
LW
1827 dMARK;
1828 register I32 items = SP - MARK;
67955e0c 1829 /* We dont worry to copy from @_. */
a0d0e21e
LW
1830 while (sp > mark) {
1831 sp[1] = sp[0];
1832 sp--;
1833 }
1834 stack_sp = mark + 1;
ecfc5424
AD
1835 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
1836 items = (*fp3)(CvXSUBANY(cv).any_i32,
1837 MARK - stack_base + 1,
1838 items);
a0d0e21e
LW
1839 stack_sp = stack_base + items;
1840 }
1841 else {
748a9306
LW
1842 I32 markix = TOPMARK;
1843
a0d0e21e 1844 PUTBACK;
67955e0c
PP
1845
1846 if (!hasargs) {
1847 /* Need to copy @_ to stack. Alternative may be to
1848 * switch stack to @_, and copy return values
1849 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
1850 AV* av = GvAV(defgv);
1851 I32 items = AvFILL(av) + 1;
1852
1853 if (items) {
1854 /* Mark is at the end of the stack. */
1855 EXTEND(sp, items);
1856 Copy(AvARRAY(av), sp + 1, items, SV*);
1857 sp += items;
1858 PUTBACK ;
1859 }
1860 }
1861 if (curcopdb) { /* We assume that the first
1862 XSUB in &DB::sub is the
1863 called one. */
1864 SAVESPTR(curcop);
1865 curcop = curcopdb;
1866 curcopdb = NULL;
1867 }
1868 /* Do we need to open block here? XXXX */
a0d0e21e 1869 (void)(*CvXSUB(cv))(cv);
748a9306
LW
1870
1871 /* Enforce some sanity in scalar context. */
5d94fbed 1872 if (gimme == G_SCALAR && ++markix != stack_sp - stack_base ) {
748a9306
LW
1873 if (markix > stack_sp - stack_base)
1874 *(stack_base + markix) = &sv_undef;
1875 else
1876 *(stack_base + markix) = *stack_sp;
1877 stack_sp = stack_base + markix;
1878 }
a0d0e21e
LW
1879 }
1880 LEAVE;
1881 return NORMAL;
1882 }
1883 else {
1884 dMARK;
1885 register I32 items = SP - MARK;
a0d0e21e
LW
1886 AV* padlist = CvPADLIST(cv);
1887 SV** svp = AvARRAY(padlist);
1888 push_return(op->op_next);
1889 PUSHBLOCK(cx, CXt_SUB, MARK);
1890 PUSHSUB(cx);
1891 CvDEPTH(cv)++;
1892 if (CvDEPTH(cv) < 2)
1893 (void)SvREFCNT_inc(cv);
1894 else { /* save temporaries on recursion? */
5f05dabc 1895 if (CvDEPTH(cv) == 100 && dowarn
44a8e56a
PP
1896 && !(perldb && cv == GvCV(DBsub)))
1897 sub_crush_depth(cv);
a0d0e21e
LW
1898 if (CvDEPTH(cv) > AvFILL(padlist)) {
1899 AV *av;
1900 AV *newpad = newAV();
4aa0a1f7 1901 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
a0d0e21e
LW
1902 I32 ix = AvFILL((AV*)svp[1]);
1903 svp = AvARRAY(svp[0]);
748a9306 1904 for ( ;ix > 0; ix--) {
a0d0e21e 1905 if (svp[ix] != &sv_undef) {
748a9306 1906 char *name = SvPVX(svp[ix]);
5f05dabc
PP
1907 if ((SvFLAGS(svp[ix]) & SVf_FAKE) /* outer lexical? */
1908 || *name == '&') /* anonymous code? */
1909 {
1910 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix]));
748a9306
LW
1911 }
1912 else { /* our own lexical */
1913 if (*name == '@')
1914 av_store(newpad, ix, sv = (SV*)newAV());
1915 else if (*name == '%')
1916 av_store(newpad, ix, sv = (SV*)newHV());
1917 else
1918 av_store(newpad, ix, sv = NEWSV(0,0));
1919 SvPADMY_on(sv);
1920 }
a0d0e21e
LW
1921 }
1922 else {
748a9306 1923 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
1924 SvPADTMP_on(sv);
1925 }
1926 }
1927 av = newAV(); /* will be @_ */
1928 av_extend(av, 0);
1929 av_store(newpad, 0, (SV*)av);
1930 AvFLAGS(av) = AVf_REIFY;
1931 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
1932 AvFILL(padlist) = CvDEPTH(cv);
1933 svp = AvARRAY(padlist);
1934 }
1935 }
1936 SAVESPTR(curpad);
1937 curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
1938 if (hasargs) {
1939 AV* av = (AV*)curpad[0];
1940 SV** ary;
1941
1942 if (AvREAL(av)) {
1943 av_clear(av);
1944 AvREAL_off(av);
1945 }
1946 cx->blk_sub.savearray = GvAV(defgv);
1947 cx->blk_sub.argarray = av;
0c8c7a05 1948 GvAV(defgv) = (AV*)SvREFCNT_inc(av);
a0d0e21e
LW
1949 ++MARK;
1950
1951 if (items > AvMAX(av) + 1) {
1952 ary = AvALLOC(av);
1953 if (AvARRAY(av) != ary) {
1954 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
1955 SvPVX(av) = (char*)ary;
1956 }
1957 if (items > AvMAX(av) + 1) {
1958 AvMAX(av) = items - 1;
1959 Renew(ary,items,SV*);
1960 AvALLOC(av) = ary;
1961 SvPVX(av) = (char*)ary;
1962 }
1963 }
1964 Copy(MARK,AvARRAY(av),items,SV*);
1965 AvFILL(av) = items - 1;
1966
1967 while (items--) {
1968 if (*MARK)
1969 SvTEMP_off(*MARK);
1970 MARK++;
1971 }
1972 }
1973 RETURNOP(CvSTART(cv));
1974 }
1975}
1976
44a8e56a
PP
1977void
1978sub_crush_depth(cv)
1979CV* cv;
1980{
1981 if (CvANON(cv))
1982 warn("Deep recursion on anonymous subroutine");
1983 else {
1984 SV* tmpstr = sv_newmortal();
1985 gv_efullname3(tmpstr, CvGV(cv), Nullch);
1986 warn("Deep recursion on subroutine \"%s\"", SvPVX(tmpstr));
1987 }
1988}
1989
a0d0e21e
LW
1990PP(pp_aelem)
1991{
1992 dSP;
1993 SV** svp;
748a9306 1994 I32 elem = POPi;
68dc0745
PP
1995 AV* av = (AV*)POPs;
1996 U32 lval = op->op_flags & OPf_MOD;
1997 U32 defer = (op->op_private & OPpLVAL_DEFER) && (elem > AvFILL(av));
a0d0e21e 1998
748a9306
LW
1999 if (elem > 0)
2000 elem -= curcop->cop_arybase;
a0d0e21e
LW
2001 if (SvTYPE(av) != SVt_PVAV)
2002 RETPUSHUNDEF;
68dc0745 2003 svp = av_fetch(av, elem, lval && !defer);
a0d0e21e 2004 if (lval) {
68dc0745
PP
2005 if (!svp || *svp == &sv_undef) {
2006 SV* lv;
2007 if (!defer)
2008 DIE(no_aelem, elem);
2009 lv = sv_newmortal();
2010 sv_upgrade(lv, SVt_PVLV);
2011 LvTYPE(lv) = 'y';
2012 sv_magic(lv, Nullsv, 'y', Nullch, 0);
2013 LvTARG(lv) = SvREFCNT_inc(av);
2014 LvTARGOFF(lv) = elem;
2015 LvTARGLEN(lv) = 1;
2016 PUSHs(lv);
2017 RETURN;
2018 }
a0d0e21e
LW
2019 if (op->op_private & OPpLVAL_INTRO)
2020 save_svref(svp);
5f05dabc 2021 else if (op->op_private & OPpDEREF)
68dc0745 2022 vivify_ref(*svp, op->op_private & OPpDEREF);
a0d0e21e
LW
2023 }
2024 PUSHs(svp ? *svp : &sv_undef);
2025 RETURN;
2026}
2027
02a9e968 2028void
68dc0745 2029vivify_ref(sv, to_what)
02a9e968 2030SV* sv;
68dc0745 2031U32 to_what;
02a9e968
CS
2032{
2033 if (SvGMAGICAL(sv))
2034 mg_get(sv);
2035 if (!SvOK(sv)) {
2036 if (SvREADONLY(sv))
2037 croak(no_modify);
5f05dabc
PP
2038 if (SvTYPE(sv) < SVt_RV)
2039 sv_upgrade(sv, SVt_RV);
2040 else if (SvTYPE(sv) >= SVt_PV) {
2041 (void)SvOOK_off(sv);
2042 Safefree(SvPVX(sv));
2043 SvLEN(sv) = SvCUR(sv) = 0;
2044 }
68dc0745 2045 switch (to_what) {
5f05dabc
PP
2046 case OPpDEREF_SV:
2047 SvRV(sv) = newSV(0);
2048 break;
2049 case OPpDEREF_AV:
2050 SvRV(sv) = (SV*)newAV();
2051 break;
2052 case OPpDEREF_HV:
2053 SvRV(sv) = (SV*)newHV();
2054 break;
2055 }
02a9e968
CS
2056 SvROK_on(sv);
2057 SvSETMAGIC(sv);
2058 }
2059}
2060
a0d0e21e
LW
2061PP(pp_method)
2062{
2063 dSP;
2064 SV* sv;
2065 SV* ob;
2066 GV* gv;
56304f61
CS
2067 HV* stash;
2068 char* name;
ac91690f
CS
2069 char* packname;
2070 STRLEN packlen;
a0d0e21e 2071
56304f61 2072 name = SvPV(TOPs, na);
a0d0e21e 2073 sv = *(stack_base + TOPMARK + 1);
56304f61 2074
16d20bd9
AD
2075 if (SvGMAGICAL(sv))
2076 mg_get(sv);
a0d0e21e 2077 if (SvROK(sv))
16d20bd9 2078 ob = (SV*)SvRV(sv);
a0d0e21e
LW
2079 else {
2080 GV* iogv;
a0d0e21e 2081
56304f61 2082 packname = Nullch;
a0d0e21e 2083 if (!SvOK(sv) ||
56304f61
CS
2084 !(packname = SvPV(sv, packlen)) ||
2085 !(iogv = gv_fetchpv(packname, FALSE, SVt_PVIO)) ||
2086 !(ob=(SV*)GvIO(iogv)))
a0d0e21e 2087 {
56304f61 2088 if (!packname || !isIDFIRST(*packname))
ac91690f 2089 DIE("Can't call method \"%s\" without a package or object reference", name);
56304f61 2090 stash = gv_stashpvn(packname, packlen, TRUE);
ac91690f 2091 goto fetch;
a0d0e21e 2092 }
ac91690f 2093 *(stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
a0d0e21e
LW
2094 }
2095
ac91690f 2096 if (!ob || !SvOBJECT(ob))
56304f61
CS
2097 DIE("Can't call method \"%s\" on unblessed reference", name);
2098
2099 stash = SvSTASH(ob);
a0d0e21e 2100
ac91690f 2101 fetch:
ac91690f 2102 gv = gv_fetchmethod(stash, name);
56304f61
CS
2103 if (!gv) {
2104 char* leaf = name;
2105 char* sep = Nullch;
2106 char* p;
2107
2108 for (p = name; *p; p++) {
2109 if (*p == '\'')
2110 sep = p, leaf = p + 1;
2111 else if (*p == ':' && *(p + 1) == ':')
2112 sep = p, leaf = p + 2;
2113 }
2114 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
2115 packname = HvNAME(sep ? curcop->cop_stash : stash);
2116 packlen = strlen(packname);
2117 }
2118 else {
2119 packname = name;
2120 packlen = sep - name;
2121 }
2122 DIE("Can't locate object method \"%s\" via package \"%.*s\"",
2123 leaf, (int)packlen, packname);
2124 }
8ebc5c01 2125 SETs(isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv);
a0d0e21e
LW
2126 RETURN;
2127}