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