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