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