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