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