This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate post-beta tweaks to ansiperl
[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) {
eaa5c2d6
GA
1460#ifndef USE_THREADS /* don't risk potential race */
1461 if (SvREFCNT(*cx->blk_loop.itervar) == 1) {
1462 /* safe to reuse old SV */
1463 sv_setsv(*cx->blk_loop.itervar, cur);
1464 }
1465 else
1466#endif
1467 {
1468 /* we need a fresh SV every time so that loop body sees a
1469 * completely new SV for closures/references to work as
1470 * they used to */
1471 SvREFCNT_dec(*cx->blk_loop.itervar);
1472 *cx->blk_loop.itervar = newSVsv(cur);
1473 }
89ea2908
GA
1474 if (strEQ(SvPVX(cur), max))
1475 sv_setiv(cur, 0); /* terminate next time */
1476 else
1477 sv_inc(cur);
1478 RETPUSHYES;
1479 }
1480 RETPUSHNO;
1481 }
1482 /* integer increment */
1483 if (cx->blk_loop.iterix > cx->blk_loop.itermax)
1484 RETPUSHNO;
7f61b687 1485
eaa5c2d6
GA
1486#ifndef USE_THREADS /* don't risk potential race */
1487 if (SvREFCNT(*cx->blk_loop.itervar) == 1) {
1488 /* safe to reuse old SV */
1489 sv_setiv(*cx->blk_loop.itervar, cx->blk_loop.iterix++);
1490 }
1491 else
1492#endif
1493 {
1494 /* we need a fresh SV every time so that loop body sees a
1495 * completely new SV for closures/references to work as they
1496 * used to */
1497 SvREFCNT_dec(*cx->blk_loop.itervar);
1498 *cx->blk_loop.itervar = newSViv(cx->blk_loop.iterix++);
1499 }
89ea2908
GA
1500 RETPUSHYES;
1501 }
1502
1503 /* iterate array */
71be2cbc 1504 if (cx->blk_loop.iterix >= (av == curstack ? cx->blk_oldsp : AvFILL(av)))
4633a7c4 1505 RETPUSHNO;
a0d0e21e 1506
7a4c00b4 1507 SvREFCNT_dec(*cx->blk_loop.itervar);
a0d0e21e 1508
93965878
NIS
1509 if (sv = (SvMAGICAL(av))
1510 ? *av_fetch(av, ++cx->blk_loop.iterix, FALSE)
1511 : AvARRAY(av)[++cx->blk_loop.iterix])
a0d0e21e 1512 SvTEMP_off(sv);
a0d0e21e 1513 else
5f05dabc
PP
1514 sv = &sv_undef;
1515 if (av != curstack && SvIMMORTAL(sv)) {
1516 SV *lv = cx->blk_loop.iterlval;
71be2cbc
PP
1517 if (lv && SvREFCNT(lv) > 1) {
1518 SvREFCNT_dec(lv);
1519 lv = Nullsv;
1520 }
5f05dabc
PP
1521 if (lv)
1522 SvREFCNT_dec(LvTARG(lv));
1523 else {
68dc0745 1524 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
5f05dabc 1525 sv_upgrade(lv, SVt_PVLV);
5f05dabc 1526 LvTYPE(lv) = 'y';
68dc0745 1527 sv_magic(lv, Nullsv, 'y', Nullch, 0);
5f05dabc
PP
1528 }
1529 LvTARG(lv) = SvREFCNT_inc(av);
1530 LvTARGOFF(lv) = cx->blk_loop.iterix;
8ac85365 1531 LvTARGLEN(lv) = (UV) -1;
5f05dabc
PP
1532 sv = (SV*)lv;
1533 }
a0d0e21e 1534
7a4c00b4 1535 *cx->blk_loop.itervar = SvREFCNT_inc(sv);
a0d0e21e
LW
1536 RETPUSHYES;
1537}
1538
1539PP(pp_subst)
1540{
4e35701f 1541 djSP; dTARG;
a0d0e21e
LW
1542 register PMOP *pm = cPMOP;
1543 PMOP *rpm = pm;
1544 register SV *dstr;
1545 register char *s;
1546 char *strend;
1547 register char *m;
1548 char *c;
1549 register char *d;
1550 STRLEN clen;
1551 I32 iters = 0;
1552 I32 maxiters;
1553 register I32 i;
1554 bool once;
71be2cbc 1555 bool rxtainted;
a0d0e21e
LW
1556 char *orig;
1557 I32 safebase;
d9f97599 1558 register REGEXP *rx = pm->op_pmregexp;
a0d0e21e
LW
1559 STRLEN len;
1560 int force_on_match = 0;
4633a7c4 1561 I32 oldsave = savestack_ix;
c277df42
IZ
1562 I32 update_minmatch = 1;
1563 SV *screamer;
a0d0e21e 1564
5cd24f17
PP
1565 /* known replacement string? */
1566 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
a0d0e21e
LW
1567 if (op->op_flags & OPf_STACKED)
1568 TARG = POPs;
1569 else {
54b9620d 1570 TARG = DEFSV;
a0d0e21e 1571 EXTEND(SP,1);
8ec5e241 1572 }
68dc0745
PP
1573 if (SvREADONLY(TARG)
1574 || (SvTYPE(TARG) > SVt_PVLV
1575 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG))))
1576 croak(no_modify);
8ec5e241
NIS
1577 PUTBACK;
1578
a0d0e21e 1579 s = SvPV(TARG, len);
68dc0745 1580 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
a0d0e21e 1581 force_on_match = 1;
b3eb6a9b
GS
1582 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
1583 (tainted && (pm->op_pmflags & PMf_RETAINT)));
1584 if (tainted)
1585 rxtainted |= 2;
9212bbba 1586 TAINT_NOT;
a0d0e21e
LW
1587
1588 force_it:
1589 if (!pm || !s)
1590 DIE("panic: do_subst");
1591
1592 strend = s + len;
1593 maxiters = (strend - s) + 10;
1594
d9f97599 1595 if (!rx->prelen && curpm) {
a0d0e21e 1596 pm = curpm;
d9f97599 1597 rx = pm->op_pmregexp;
a0d0e21e 1598 }
d9f97599
GS
1599 screamer = ( (SvSCREAM(TARG) && rx->check_substr
1600 && SvTYPE(rx->check_substr) == SVt_PVBM
1601 && SvVALID(rx->check_substr))
c277df42 1602 ? TARG : Nullsv);
d9f97599 1603 safebase = (!rx->nparens && !sawampersand) ? 0 : REXEC_COPY_STR;
a0d0e21e
LW
1604 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
1605 SAVEINT(multiline);
1606 multiline = pm->op_pmflags & PMf_MULTILINE;
1607 }
1608 orig = m = s;
d9f97599
GS
1609 if (rx->check_substr) {
1610 if (!(rx->reganch & ROPT_NOSCAN)) { /* It floats. */
c277df42
IZ
1611 if (screamer) {
1612 I32 p = -1;
1613
d9f97599 1614 if (screamfirst[BmRARE(rx->check_substr)] < 0)
a0d0e21e 1615 goto nope;
d9f97599 1616 else if (!(s = screaminstr(TARG, rx->check_substr, rx->check_offset_min, 0, &p, 0)))
a0d0e21e
LW
1617 goto nope;
1618 }
d9f97599 1619 else if (!(s = fbm_instr((unsigned char*)s + rx->check_offset_min,
c277df42 1620 (unsigned char*)strend,
411d5715 1621 rx->check_substr, 0)))
a0d0e21e 1622 goto nope;
d9f97599
GS
1623 if (s && rx->check_offset_max < s - m) {
1624 ++BmUSEFUL(rx->check_substr);
1625 s -= rx->check_offset_max;
a0d0e21e
LW
1626 }
1627 else
1628 s = m;
1629 }
c277df42
IZ
1630 /* Now checkstring is fixed, i.e. at fixed offset from the
1631 beginning of match, and the match is anchored at s. */
1632 else if (!multiline) { /* Anchored at beginning of string. */
1633 I32 slen;
d9f97599
GS
1634 if (*SvPVX(rx->check_substr) != s[rx->check_offset_min]
1635 || ((slen = SvCUR(rx->check_substr)) > 1
1636 && memNE(SvPVX(rx->check_substr),
1637 s + rx->check_offset_min, slen)))
bbce6d69 1638 goto nope;
a0d0e21e 1639 }
d9f97599
GS
1640 if (!rx->naughty && --BmUSEFUL(rx->check_substr) < 0
1641 && rx->check_substr == rx->float_substr) {
1642 SvREFCNT_dec(rx->check_substr);
1643 rx->check_substr = Nullsv; /* opt is being useless */
1644 rx->float_substr = Nullsv;
a0d0e21e
LW
1645 }
1646 }
71be2cbc
PP
1647
1648 /* only replace once? */
a0d0e21e 1649 once = !(rpm->op_pmflags & PMf_GLOBAL);
71be2cbc
PP
1650
1651 /* known replacement string? */
5cd24f17 1652 c = dstr ? SvPV(dstr, clen) : Nullch;
71be2cbc
PP
1653
1654 /* can do inplace substitution? */
d9f97599
GS
1655 if (c && clen <= rx->minlen && (once || !(safebase & REXEC_COPY_STR))
1656 && !(rx->reganch & ROPT_LOOKBEHIND_SEEN)) {
15e52e56 1657 if (!CALLREGEXEC(rx, s, strend, orig, 0, screamer, NULL, safebase)) {
8ec5e241 1658 SPAGAIN;
71be2cbc
PP
1659 PUSHs(&sv_no);
1660 LEAVE_SCOPE(oldsave);
1661 RETURN;
1662 }
1663 if (force_on_match) {
1664 force_on_match = 0;
1665 s = SvPV_force(TARG, len);
1666 goto force_it;
1667 }
71be2cbc
PP
1668 d = s;
1669 curpm = pm;
1670 SvSCREAM_off(TARG); /* disable possible screamer */
1671 if (once) {
48c036b1 1672 rxtainted |= RX_MATCH_TAINTED(rx);
d9f97599
GS
1673 if (rx->subbase) {
1674 m = orig + (rx->startp[0] - rx->subbase);
1675 d = orig + (rx->endp[0] - rx->subbase);
c277df42 1676 } else {
d9f97599
GS
1677 m = rx->startp[0];
1678 d = rx->endp[0];
c277df42 1679 }
71be2cbc
PP
1680 s = orig;
1681 if (m - s > strend - d) { /* faster to shorten from end */
1682 if (clen) {
1683 Copy(c, m, clen, char);
1684 m += clen;
a0d0e21e 1685 }
71be2cbc
PP
1686 i = strend - d;
1687 if (i > 0) {
1688 Move(d, m, i, char);
1689 m += i;
a0d0e21e 1690 }
71be2cbc
PP
1691 *m = '\0';
1692 SvCUR_set(TARG, m - s);
1693 }
1694 /*SUPPRESS 560*/
1695 else if (i = m - s) { /* faster from front */
1696 d -= clen;
1697 m = d;
1698 sv_chop(TARG, d-i);
1699 s += i;
1700 while (i--)
1701 *--d = *--s;
1702 if (clen)
1703 Copy(c, m, clen, char);
1704 }
1705 else if (clen) {
1706 d -= clen;
1707 sv_chop(TARG, d);
1708 Copy(c, d, clen, char);
1709 }
1710 else {
1711 sv_chop(TARG, d);
1712 }
48c036b1 1713 TAINT_IF(rxtainted & 1);
8ec5e241 1714 SPAGAIN;
71be2cbc
PP
1715 PUSHs(&sv_yes);
1716 }
1717 else {
71be2cbc
PP
1718 do {
1719 if (iters++ > maxiters)
1720 DIE("Substitution loop");
d9f97599
GS
1721 rxtainted |= RX_MATCH_TAINTED(rx);
1722 m = rx->startp[0];
71be2cbc
PP
1723 /*SUPPRESS 560*/
1724 if (i = m - s) {
1725 if (s != d)
1726 Move(s, d, i, char);
1727 d += i;
a0d0e21e 1728 }
71be2cbc
PP
1729 if (clen) {
1730 Copy(c, d, clen, char);
1731 d += clen;
1732 }
d9f97599 1733 s = rx->endp[0];
15e52e56 1734 } while (CALLREGEXEC(rx, s, strend, orig, s == m,
c277df42 1735 Nullsv, NULL, 0)); /* don't match same null twice */
71be2cbc
PP
1736 if (s != d) {
1737 i = strend - s;
1738 SvCUR_set(TARG, d - SvPVX(TARG) + i);
1739 Move(s, d, i+1, char); /* include the NUL */
a0d0e21e 1740 }
48c036b1 1741 TAINT_IF(rxtainted & 1);
8ec5e241 1742 SPAGAIN;
71be2cbc 1743 PUSHs(sv_2mortal(newSViv((I32)iters)));
a0d0e21e 1744 }
71be2cbc 1745 (void)SvPOK_only(TARG);
48c036b1 1746 TAINT_IF(rxtainted);
8ec5e241
NIS
1747 if (SvSMAGICAL(TARG)) {
1748 PUTBACK;
1749 mg_set(TARG);
1750 SPAGAIN;
1751 }
9212bbba 1752 SvTAINT(TARG);
71be2cbc
PP
1753 LEAVE_SCOPE(oldsave);
1754 RETURN;
a0d0e21e 1755 }
71be2cbc 1756
15e52e56 1757 if (CALLREGEXEC(rx, s, strend, orig, 0, screamer, NULL, safebase)) {
a0d0e21e
LW
1758 if (force_on_match) {
1759 force_on_match = 0;
1760 s = SvPV_force(TARG, len);
1761 goto force_it;
1762 }
48c036b1 1763 rxtainted |= RX_MATCH_TAINTED(rx);
8ec5e241 1764 dstr = NEWSV(25, len);
a0d0e21e
LW
1765 sv_setpvn(dstr, m, s-m);
1766 curpm = pm;
1767 if (!c) {
c09156bb 1768 register PERL_CONTEXT *cx;
8ec5e241 1769 SPAGAIN;
a0d0e21e
LW
1770 PUSHSUBST(cx);
1771 RETURNOP(cPMOP->op_pmreplroot);
1772 }
1773 do {
1774 if (iters++ > maxiters)
1775 DIE("Substitution loop");
d9f97599
GS
1776 rxtainted |= RX_MATCH_TAINTED(rx);
1777 if (rx->subbase && rx->subbase != orig) {
a0d0e21e
LW
1778 m = s;
1779 s = orig;
d9f97599 1780 orig = rx->subbase;
a0d0e21e
LW
1781 s = orig + (m - s);
1782 strend = s + (strend - m);
1783 }
d9f97599 1784 m = rx->startp[0];
a0d0e21e 1785 sv_catpvn(dstr, s, m-s);
d9f97599 1786 s = rx->endp[0];
a0d0e21e
LW
1787 if (clen)
1788 sv_catpvn(dstr, c, clen);
1789 if (once)
1790 break;
15e52e56 1791 } while (CALLREGEXEC(rx, s, strend, orig, s == m, Nullsv, NULL, safebase));
a0d0e21e 1792 sv_catpvn(dstr, s, strend - s);
748a9306 1793
4633a7c4 1794 (void)SvOOK_off(TARG);
cb0b1708 1795 Safefree(SvPVX(TARG));
748a9306
LW
1796 SvPVX(TARG) = SvPVX(dstr);
1797 SvCUR_set(TARG, SvCUR(dstr));
1798 SvLEN_set(TARG, SvLEN(dstr));
1799 SvPVX(dstr) = 0;
1800 sv_free(dstr);
1801
48c036b1 1802 TAINT_IF(rxtainted & 1);
f878fbec 1803 SPAGAIN;
48c036b1
GS
1804 PUSHs(sv_2mortal(newSViv((I32)iters)));
1805
a0d0e21e 1806 (void)SvPOK_only(TARG);
48c036b1 1807 TAINT_IF(rxtainted);
a0d0e21e 1808 SvSETMAGIC(TARG);
9212bbba 1809 SvTAINT(TARG);
4633a7c4 1810 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1811 RETURN;
1812 }
5cd24f17 1813 goto ret_no;
a0d0e21e
LW
1814
1815nope:
d9f97599 1816 ++BmUSEFUL(rx->check_substr);
5cd24f17 1817
8ec5e241
NIS
1818ret_no:
1819 SPAGAIN;
a0d0e21e 1820 PUSHs(&sv_no);
4633a7c4 1821 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1822 RETURN;
1823}
1824
1825PP(pp_grepwhile)
1826{
4e35701f 1827 djSP;
a0d0e21e
LW
1828
1829 if (SvTRUEx(POPs))
1830 stack_base[markstack_ptr[-1]++] = stack_base[*markstack_ptr];
1831 ++*markstack_ptr;
1832 LEAVE; /* exit inner scope */
1833
1834 /* All done yet? */
924508f0 1835 if (stack_base + *markstack_ptr > SP) {
a0d0e21e 1836 I32 items;
54310121 1837 I32 gimme = GIMME_V;
a0d0e21e
LW
1838
1839 LEAVE; /* exit outer scope */
1840 (void)POPMARK; /* pop src */
1841 items = --*markstack_ptr - markstack_ptr[-1];
1842 (void)POPMARK; /* pop dst */
1843 SP = stack_base + POPMARK; /* pop original mark */
54310121 1844 if (gimme == G_SCALAR) {
a0d0e21e
LW
1845 dTARGET;
1846 XPUSHi(items);
a0d0e21e 1847 }
54310121
PP
1848 else if (gimme == G_ARRAY)
1849 SP += items;
a0d0e21e
LW
1850 RETURN;
1851 }
1852 else {
1853 SV *src;
1854
1855 ENTER; /* enter inner scope */
1856 SAVESPTR(curpm);
1857
1858 src = stack_base[*markstack_ptr];
1859 SvTEMP_off(src);
54b9620d 1860 DEFSV = src;
a0d0e21e
LW
1861
1862 RETURNOP(cLOGOP->op_other);
1863 }
1864}
1865
1866PP(pp_leavesub)
1867{
4e35701f 1868 djSP;
a0d0e21e
LW
1869 SV **mark;
1870 SV **newsp;
1871 PMOP *newpm;
1872 I32 gimme;
c09156bb 1873 register PERL_CONTEXT *cx;
f86702cc 1874 struct block_sub cxsub;
a0d0e21e
LW
1875
1876 POPBLOCK(cx,newpm);
f86702cc
PP
1877 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1878
a1f49e72 1879 TAINT_NOT;
a0d0e21e
LW
1880 if (gimme == G_SCALAR) {
1881 MARK = newsp + 1;
a29cdaf0
IZ
1882 if (MARK <= SP) {
1883 if (cxsub.cv && CvDEPTH(cxsub.cv) > 1) {
1884 if (SvTEMP(TOPs)) {
1885 *MARK = SvREFCNT_inc(TOPs);
1886 FREETMPS;
1887 sv_2mortal(*MARK);
1888 } else {
1889 FREETMPS;
1890 *MARK = sv_mortalcopy(TOPs);
1891 }
1892 } else
1893 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
1894 } else {
f86702cc 1895 MEXTEND(MARK, 0);
a0d0e21e
LW
1896 *MARK = &sv_undef;
1897 }
1898 SP = MARK;
1899 }
54310121 1900 else if (gimme == G_ARRAY) {
f86702cc 1901 for (MARK = newsp + 1; MARK <= SP; MARK++) {
a1f49e72 1902 if (!SvTEMP(*MARK)) {
f86702cc 1903 *MARK = sv_mortalcopy(*MARK);
a1f49e72
CS
1904 TAINT_NOT; /* Each item is independent */
1905 }
f86702cc 1906 }
a0d0e21e 1907 }
f86702cc
PP
1908 PUTBACK;
1909
1910 POPSUB2(); /* Stack values are safe: release CV and @_ ... */
1911 curpm = newpm; /* ... and pop $1 et al */
a0d0e21e
LW
1912
1913 LEAVE;
a0d0e21e
LW
1914 return pop_return();
1915}
1916
76e3520e 1917STATIC CV *
8ac85365 1918get_db_sub(SV **svp, CV *cv)
3de9ffa1
MB
1919{
1920 dTHR;
491527d0
GS
1921 SV *dbsv = GvSV(DBsub);
1922
1923 if (!PERLDB_SUB_NN) {
1924 GV *gv = CvGV(cv);
1925
1926 save_item(dbsv);
1927 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
1928 || strEQ(GvNAME(gv), "END")
1929 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
1930 !( (SvTYPE(*svp) == SVt_PVGV) && (GvCV((GV*)*svp) == cv)
1931 && (gv = (GV*)*svp) ))) {
1932 /* Use GV from the stack as a fallback. */
1933 /* GV is potentially non-unique, or contain different CV. */
1934 sv_setsv(dbsv, newRV((SV*)cv));
1935 }
1936 else {
1937 gv_efullname3(dbsv, gv, Nullch);
1938 }
3de9ffa1
MB
1939 }
1940 else {
491527d0
GS
1941 SvUPGRADE(dbsv, SVt_PVIV);
1942 SvIOK_on(dbsv);
1943 SAVEIV(SvIVX(dbsv));
1944 SvIVX(dbsv) = (IV)cv; /* Do it the quickest way */
3de9ffa1 1945 }
491527d0 1946
3de9ffa1
MB
1947 if (CvXSUB(cv))
1948 curcopdb = curcop;
491527d0 1949 cv = GvCV(DBsub);
3de9ffa1
MB
1950 return cv;
1951}
1952
a0d0e21e
LW
1953PP(pp_entersub)
1954{
4e35701f 1955 djSP; dPOPss;
a0d0e21e
LW
1956 GV *gv;
1957 HV *stash;
1958 register CV *cv;
c09156bb 1959 register PERL_CONTEXT *cx;
5d94fbed 1960 I32 gimme;
07055b4c 1961 bool hasargs = (op->op_flags & OPf_STACKED) != 0;
a0d0e21e
LW
1962
1963 if (!sv)
1964 DIE("Not a CODE reference");
1965 switch (SvTYPE(sv)) {
1966 default:
1967 if (!SvROK(sv)) {
748a9306
LW
1968 char *sym;
1969
fb73857a
PP
1970 if (sv == &sv_yes) { /* unfound import, ignore */
1971 if (hasargs)
1972 SP = stack_base + POPMARK;
a0d0e21e 1973 RETURN;
fb73857a 1974 }
15ff848f
CS
1975 if (SvGMAGICAL(sv)) {
1976 mg_get(sv);
1977 sym = SvPOKp(sv) ? SvPVX(sv) : Nullch;
1978 }
1979 else
1980 sym = SvPV(sv, na);
1981 if (!sym)
a0d0e21e
LW
1982 DIE(no_usym, "a subroutine");
1983 if (op->op_private & HINT_STRICT_REFS)
748a9306
LW
1984 DIE(no_symref, sym, "a subroutine");
1985 cv = perl_get_cv(sym, TRUE);
a0d0e21e
LW
1986 break;
1987 }
1988 cv = (CV*)SvRV(sv);
1989 if (SvTYPE(cv) == SVt_PVCV)
1990 break;
1991 /* FALL THROUGH */
1992 case SVt_PVHV:
1993 case SVt_PVAV:
1994 DIE("Not a CODE reference");
1995 case SVt_PVCV:
1996 cv = (CV*)sv;
1997 break;
1998 case SVt_PVGV:
8ebc5c01 1999 if (!(cv = GvCVu((GV*)sv)))
a0d0e21e
LW
2000 cv = sv_2cv(sv, &stash, &gv, TRUE);
2001 break;
2002 }
2003
2004 ENTER;
2005 SAVETMPS;
2006
2007 retry:
2008 if (!cv)
2009 DIE("Not a CODE reference");
2010
2011 if (!CvROOT(cv) && !CvXSUB(cv)) {
44a8e56a 2012 GV* autogv;
22239a37 2013 SV* sub_name;
44a8e56a
PP
2014
2015 /* anonymous or undef'd function leaves us no recourse */
2016 if (CvANON(cv) || !(gv = CvGV(cv)))
2017 DIE("Undefined subroutine called");
2018 /* autoloaded stub? */
2019 if (cv != GvCV(gv)) {
2020 cv = GvCV(gv);
2021 goto retry;
a0d0e21e 2022 }
44a8e56a 2023 /* should call AUTOLOAD now? */
54310121
PP
2024 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
2025 FALSE)))
2026 {
44a8e56a
PP
2027 cv = GvCV(autogv);
2028 goto retry;
a0d0e21e 2029 }
44a8e56a 2030 /* sorry */
22239a37
NIS
2031 sub_name = sv_newmortal();
2032 gv_efullname3(sub_name, gv, Nullch);
2033 DIE("Undefined subroutine &%s called", SvPVX(sub_name));
a0d0e21e
LW
2034 }
2035
54310121 2036 gimme = GIMME_V;
3de9ffa1 2037 if ((op->op_private & OPpENTERSUB_DB) && GvCV(DBsub) && !CvNODEBUG(cv))
4f01c5a5 2038 cv = get_db_sub(&sv, cv);
3de9ffa1
MB
2039 if (!cv)
2040 DIE("No DBsub routine");
a0d0e21e 2041
11343788 2042#ifdef USE_THREADS
3de9ffa1
MB
2043 /*
2044 * First we need to check if the sub or method requires locking.
458fb581
MB
2045 * If so, we gain a lock on the CV, the first argument or the
2046 * stash (for static methods), as appropriate. This has to be
2047 * inline because for FAKE_THREADS, COND_WAIT inlines code to
2048 * reschedule by returning a new op.
3de9ffa1 2049 */
11343788 2050 MUTEX_LOCK(CvMUTEXP(cv));
77a005ab
MB
2051 if (CvFLAGS(cv) & CVf_LOCKED) {
2052 MAGIC *mg;
2053 if (CvFLAGS(cv) & CVf_METHOD) {
2054 if (SP > stack_base + TOPMARK)
2055 sv = *(stack_base + TOPMARK + 1);
2056 else {
2057 MUTEX_UNLOCK(CvMUTEXP(cv));
2058 croak("no argument for locked method call");
2059 }
2060 if (SvROK(sv))
2061 sv = SvRV(sv);
458fb581
MB
2062 else {
2063 STRLEN len;
2064 char *stashname = SvPV(sv, len);
2065 sv = (SV*)gv_stashpvn(stashname, len, TRUE);
2066 }
77a005ab
MB
2067 }
2068 else {
2069 sv = (SV*)cv;
2070 }
2071 MUTEX_UNLOCK(CvMUTEXP(cv));
2072 mg = condpair_magic(sv);
2073 MUTEX_LOCK(MgMUTEXP(mg));
2074 if (MgOWNER(mg) == thr)
2075 MUTEX_UNLOCK(MgMUTEXP(mg));
2076 else {
2077 while (MgOWNER(mg))
2078 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
2079 MgOWNER(mg) = thr;
1fd28e87
MB
2080 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: pp_entersub lock %p\n",
2081 thr, sv);)
77a005ab 2082 MUTEX_UNLOCK(MgMUTEXP(mg));
e55aaa0e 2083 SvREFCNT_inc(sv); /* Keep alive until magic_mutexfree */
77a005ab 2084 save_destructor(unlock_condpair, sv);
11343788 2085 }
77a005ab 2086 MUTEX_LOCK(CvMUTEXP(cv));
11343788 2087 }
3de9ffa1
MB
2088 /*
2089 * Now we have permission to enter the sub, we must distinguish
2090 * four cases. (0) It's an XSUB (in which case we don't care
2091 * about ownership); (1) it's ours already (and we're recursing);
2092 * (2) it's free (but we may already be using a cached clone);
2093 * (3) another thread owns it. Case (1) is easy: we just use it.
2094 * Case (2) means we look for a clone--if we have one, use it
2095 * otherwise grab ownership of cv. Case (3) means we look for a
2096 * clone (for non-XSUBs) and have to create one if we don't
2097 * already have one.
2098 * Why look for a clone in case (2) when we could just grab
2099 * ownership of cv straight away? Well, we could be recursing,
2100 * i.e. we originally tried to enter cv while another thread
2101 * owned it (hence we used a clone) but it has been freed up
2102 * and we're now recursing into it. It may or may not be "better"
2103 * to use the clone but at least CvDEPTH can be trusted.
2104 */
2105 if (CvOWNER(cv) == thr || CvXSUB(cv))
2106 MUTEX_UNLOCK(CvMUTEXP(cv));
11343788 2107 else {
3de9ffa1
MB
2108 /* Case (2) or (3) */
2109 SV **svp;
2110
11343788 2111 /*
3de9ffa1
MB
2112 * XXX Might it be better to release CvMUTEXP(cv) while we
2113 * do the hv_fetch? We might find someone has pinched it
2114 * when we look again, in which case we would be in case
2115 * (3) instead of (2) so we'd have to clone. Would the fact
2116 * that we released the mutex more quickly make up for this?
2117 */
6ee623d5
GS
2118 if (threadnum &&
2119 (svp = hv_fetch(thr->cvcache, (char *)cv, sizeof(cv), FALSE)))
2120 {
3de9ffa1 2121 /* We already have a clone to use */
11343788 2122 MUTEX_UNLOCK(CvMUTEXP(cv));
3de9ffa1
MB
2123 cv = *(CV**)svp;
2124 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
2125 "entersub: %p already has clone %p:%s\n",
2126 thr, cv, SvPEEK((SV*)cv)));
3de9ffa1
MB
2127 CvOWNER(cv) = thr;
2128 SvREFCNT_inc(cv);
2129 if (CvDEPTH(cv) == 0)
2130 SAVEDESTRUCTOR(unset_cvowner, (void*) cv);
2131 }
11343788 2132 else {
3de9ffa1
MB
2133 /* (2) => grab ownership of cv. (3) => make clone */
2134 if (!CvOWNER(cv)) {
2135 CvOWNER(cv) = thr;
2136 SvREFCNT_inc(cv);
11343788 2137 MUTEX_UNLOCK(CvMUTEXP(cv));
d9f997d7 2138 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
2139 "entersub: %p grabbing %p:%s in stash %s\n",
2140 thr, cv, SvPEEK((SV*)cv), CvSTASH(cv) ?
3de9ffa1
MB
2141 HvNAME(CvSTASH(cv)) : "(none)"));
2142 } else {
2143 /* Make a new clone. */
2144 CV *clonecv;
2145 SvREFCNT_inc(cv); /* don't let it vanish from under us */
2146 MUTEX_UNLOCK(CvMUTEXP(cv));
2147 DEBUG_L((PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
2148 "entersub: %p cloning %p:%s\n",
2149 thr, cv, SvPEEK((SV*)cv))));
3de9ffa1
MB
2150 /*
2151 * We're creating a new clone so there's no race
2152 * between the original MUTEX_UNLOCK and the
2153 * SvREFCNT_inc since no one will be trying to undef
2154 * it out from underneath us. At least, I don't think
2155 * there's a race...
2156 */
2157 clonecv = cv_clone(cv);
2158 SvREFCNT_dec(cv); /* finished with this */
199100c8 2159 hv_store(thr->cvcache, (char*)cv, sizeof(cv), (SV*)clonecv,0);
3de9ffa1
MB
2160 CvOWNER(clonecv) = thr;
2161 cv = clonecv;
11343788 2162 SvREFCNT_inc(cv);
11343788 2163 }
3de9ffa1
MB
2164 DEBUG_L(if (CvDEPTH(cv) != 0)
2165 PerlIO_printf(PerlIO_stderr(), "depth %ld != 0\n",
2166 CvDEPTH(cv)););
2167 SAVEDESTRUCTOR(unset_cvowner, (void*) cv);
11343788 2168 }
3de9ffa1 2169 }
11343788
MB
2170#endif /* USE_THREADS */
2171
a0d0e21e
LW
2172 if (CvXSUB(cv)) {
2173 if (CvOLDSTYLE(cv)) {
ecfc5424 2174 I32 (*fp3)_((int,int,int));
a0d0e21e
LW
2175 dMARK;
2176 register I32 items = SP - MARK;
67955e0c 2177 /* We dont worry to copy from @_. */
924508f0
GS
2178 while (SP > mark) {
2179 SP[1] = SP[0];
2180 SP--;
a0d0e21e
LW
2181 }
2182 stack_sp = mark + 1;
ecfc5424
AD
2183 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
2184 items = (*fp3)(CvXSUBANY(cv).any_i32,
2185 MARK - stack_base + 1,
2186 items);
a0d0e21e
LW
2187 stack_sp = stack_base + items;
2188 }
2189 else {
748a9306
LW
2190 I32 markix = TOPMARK;
2191
a0d0e21e 2192 PUTBACK;
67955e0c
PP
2193
2194 if (!hasargs) {
2195 /* Need to copy @_ to stack. Alternative may be to
2196 * switch stack to @_, and copy return values
2197 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
6d4ff0d2
MB
2198 AV* av;
2199 I32 items;
2200#ifdef USE_THREADS
2201 av = (AV*)curpad[0];
2202#else
2203 av = GvAV(defgv);
2204#endif /* USE_THREADS */
93965878 2205 items = AvFILLp(av) + 1; /* @_ is not tieable */
67955e0c
PP
2206
2207 if (items) {
2208 /* Mark is at the end of the stack. */
924508f0
GS
2209 EXTEND(SP, items);
2210 Copy(AvARRAY(av), SP + 1, items, SV*);
2211 SP += items;
67955e0c
PP
2212 PUTBACK ;
2213 }
2214 }
2215 if (curcopdb) { /* We assume that the first
2216 XSUB in &DB::sub is the
2217 called one. */
2218 SAVESPTR(curcop);
2219 curcop = curcopdb;
2220 curcopdb = NULL;
2221 }
2222 /* Do we need to open block here? XXXX */
1d583055 2223 (void)(*CvXSUB(cv))(cv _PERL_OBJECT_THIS);
748a9306
LW
2224
2225 /* Enforce some sanity in scalar context. */
5d94fbed 2226 if (gimme == G_SCALAR && ++markix != stack_sp - stack_base ) {
748a9306
LW
2227 if (markix > stack_sp - stack_base)
2228 *(stack_base + markix) = &sv_undef;
2229 else
2230 *(stack_base + markix) = *stack_sp;
2231 stack_sp = stack_base + markix;
2232 }
a0d0e21e
LW
2233 }
2234 LEAVE;
2235 return NORMAL;
2236 }
2237 else {
2238 dMARK;
2239 register I32 items = SP - MARK;
a0d0e21e
LW
2240 AV* padlist = CvPADLIST(cv);
2241 SV** svp = AvARRAY(padlist);
2242 push_return(op->op_next);
2243 PUSHBLOCK(cx, CXt_SUB, MARK);
2244 PUSHSUB(cx);
2245 CvDEPTH(cv)++;
2246 if (CvDEPTH(cv) < 2)
2247 (void)SvREFCNT_inc(cv);
2248 else { /* save temporaries on recursion? */
5f05dabc 2249 if (CvDEPTH(cv) == 100 && dowarn
84902520 2250 && !(PERLDB_SUB && cv == GvCV(DBsub)))
44a8e56a 2251 sub_crush_depth(cv);
93965878 2252 if (CvDEPTH(cv) > AvFILLp(padlist)) {
a0d0e21e
LW
2253 AV *av;
2254 AV *newpad = newAV();
4aa0a1f7 2255 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
93965878 2256 I32 ix = AvFILLp((AV*)svp[1]);
a0d0e21e 2257 svp = AvARRAY(svp[0]);
748a9306 2258 for ( ;ix > 0; ix--) {
a0d0e21e 2259 if (svp[ix] != &sv_undef) {
748a9306 2260 char *name = SvPVX(svp[ix]);
5f05dabc
PP
2261 if ((SvFLAGS(svp[ix]) & SVf_FAKE) /* outer lexical? */
2262 || *name == '&') /* anonymous code? */
2263 {
2264 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix]));
748a9306
LW
2265 }
2266 else { /* our own lexical */
2267 if (*name == '@')
2268 av_store(newpad, ix, sv = (SV*)newAV());
2269 else if (*name == '%')
2270 av_store(newpad, ix, sv = (SV*)newHV());
2271 else
2272 av_store(newpad, ix, sv = NEWSV(0,0));
2273 SvPADMY_on(sv);
2274 }
a0d0e21e
LW
2275 }
2276 else {
748a9306 2277 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
2278 SvPADTMP_on(sv);
2279 }
2280 }
2281 av = newAV(); /* will be @_ */
2282 av_extend(av, 0);
2283 av_store(newpad, 0, (SV*)av);
2284 AvFLAGS(av) = AVf_REIFY;
2285 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
93965878 2286 AvFILLp(padlist) = CvDEPTH(cv);
a0d0e21e
LW
2287 svp = AvARRAY(padlist);
2288 }
2289 }
6d4ff0d2
MB
2290#ifdef USE_THREADS
2291 if (!hasargs) {
a0d0e21e 2292 AV* av = (AV*)curpad[0];
6d4ff0d2 2293
93965878 2294 items = AvFILLp(av) + 1;
6d4ff0d2
MB
2295 if (items) {
2296 /* Mark is at the end of the stack. */
924508f0
GS
2297 EXTEND(SP, items);
2298 Copy(AvARRAY(av), SP + 1, items, SV*);
2299 SP += items;
6d4ff0d2
MB
2300 PUTBACK ;
2301 }
2302 }
2303#endif /* USE_THREADS */
2304 SAVESPTR(curpad);
2305 curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
2306#ifndef USE_THREADS
2307 if (hasargs)
2308#endif /* USE_THREADS */
2309 {
2310 AV* av;
a0d0e21e
LW
2311 SV** ary;
2312
77a005ab 2313#if 0
0f15f207
MB
2314 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
2315 "%p entersub preparing @_\n", thr));
77a005ab 2316#endif
6d4ff0d2 2317 av = (AV*)curpad[0];
a0d0e21e
LW
2318 if (AvREAL(av)) {
2319 av_clear(av);
2320 AvREAL_off(av);
2321 }
6d4ff0d2 2322#ifndef USE_THREADS
a0d0e21e 2323 cx->blk_sub.savearray = GvAV(defgv);
0c8c7a05 2324 GvAV(defgv) = (AV*)SvREFCNT_inc(av);
6d4ff0d2
MB
2325#endif /* USE_THREADS */
2326 cx->blk_sub.argarray = av;
a0d0e21e
LW
2327 ++MARK;
2328
2329 if (items > AvMAX(av) + 1) {
2330 ary = AvALLOC(av);
2331 if (AvARRAY(av) != ary) {
2332 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2333 SvPVX(av) = (char*)ary;
2334 }
2335 if (items > AvMAX(av) + 1) {
2336 AvMAX(av) = items - 1;
2337 Renew(ary,items,SV*);
2338 AvALLOC(av) = ary;
2339 SvPVX(av) = (char*)ary;
2340 }
2341 }
2342 Copy(MARK,AvARRAY(av),items,SV*);
93965878 2343 AvFILLp(av) = items - 1;
a0d0e21e
LW
2344
2345 while (items--) {
2346 if (*MARK)
2347 SvTEMP_off(*MARK);
2348 MARK++;
2349 }
2350 }
77a005ab 2351#if 0
0f15f207
MB
2352 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
2353 "%p entersub returning %p\n", thr, CvSTART(cv)));
77a005ab 2354#endif
a0d0e21e
LW
2355 RETURNOP(CvSTART(cv));
2356 }
2357}
2358
44a8e56a 2359void
8ac85365 2360sub_crush_depth(CV *cv)
44a8e56a
PP
2361{
2362 if (CvANON(cv))
2363 warn("Deep recursion on anonymous subroutine");
2364 else {
2365 SV* tmpstr = sv_newmortal();
2366 gv_efullname3(tmpstr, CvGV(cv), Nullch);
2367 warn("Deep recursion on subroutine \"%s\"", SvPVX(tmpstr));
2368 }
2369}
2370
a0d0e21e
LW
2371PP(pp_aelem)
2372{
4e35701f 2373 djSP;
a0d0e21e 2374 SV** svp;
748a9306 2375 I32 elem = POPi;
68dc0745
PP
2376 AV* av = (AV*)POPs;
2377 U32 lval = op->op_flags & OPf_MOD;
2378 U32 defer = (op->op_private & OPpLVAL_DEFER) && (elem > AvFILL(av));
be6c24e0 2379 SV *sv;
a0d0e21e 2380
748a9306
LW
2381 if (elem > 0)
2382 elem -= curcop->cop_arybase;
a0d0e21e
LW
2383 if (SvTYPE(av) != SVt_PVAV)
2384 RETPUSHUNDEF;
68dc0745 2385 svp = av_fetch(av, elem, lval && !defer);
a0d0e21e 2386 if (lval) {
68dc0745
PP
2387 if (!svp || *svp == &sv_undef) {
2388 SV* lv;
2389 if (!defer)
2390 DIE(no_aelem, elem);
2391 lv = sv_newmortal();
2392 sv_upgrade(lv, SVt_PVLV);
2393 LvTYPE(lv) = 'y';
2394 sv_magic(lv, Nullsv, 'y', Nullch, 0);
2395 LvTARG(lv) = SvREFCNT_inc(av);
2396 LvTARGOFF(lv) = elem;
2397 LvTARGLEN(lv) = 1;
2398 PUSHs(lv);
2399 RETURN;
2400 }
a0d0e21e 2401 if (op->op_private & OPpLVAL_INTRO)
161b7d16 2402 save_aelem(av, elem, svp);
5f05dabc 2403 else if (op->op_private & OPpDEREF)
68dc0745 2404 vivify_ref(*svp, op->op_private & OPpDEREF);
a0d0e21e 2405 }
be6c24e0
GS
2406 sv = (svp ? *svp : &sv_undef);
2407 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
2408 sv = sv_mortalcopy(sv);
2409 PUSHs(sv);
a0d0e21e
LW
2410 RETURN;
2411}
2412
02a9e968 2413void
8ac85365 2414vivify_ref(SV *sv, U32 to_what)
02a9e968
CS
2415{
2416 if (SvGMAGICAL(sv))
2417 mg_get(sv);
2418 if (!SvOK(sv)) {
2419 if (SvREADONLY(sv))
2420 croak(no_modify);
5f05dabc
PP
2421 if (SvTYPE(sv) < SVt_RV)
2422 sv_upgrade(sv, SVt_RV);
2423 else if (SvTYPE(sv) >= SVt_PV) {
2424 (void)SvOOK_off(sv);
2425 Safefree(SvPVX(sv));
2426 SvLEN(sv) = SvCUR(sv) = 0;
2427 }
68dc0745 2428 switch (to_what) {
5f05dabc 2429 case OPpDEREF_SV:
8c52afec 2430 SvRV(sv) = NEWSV(355,0);
5f05dabc
PP
2431 break;
2432 case OPpDEREF_AV:
2433 SvRV(sv) = (SV*)newAV();
2434 break;
2435 case OPpDEREF_HV:
2436 SvRV(sv) = (SV*)newHV();
2437 break;
2438 }
02a9e968
CS
2439 SvROK_on(sv);
2440 SvSETMAGIC(sv);
2441 }
2442}
2443
a0d0e21e
LW
2444PP(pp_method)
2445{
4e35701f 2446 djSP;
a0d0e21e
LW
2447 SV* sv;
2448 SV* ob;
2449 GV* gv;
56304f61
CS
2450 HV* stash;
2451 char* name;
ac91690f
CS
2452 char* packname;
2453 STRLEN packlen;
a0d0e21e 2454
fb73857a
PP
2455 if (SvROK(TOPs)) {
2456 sv = SvRV(TOPs);
2457 if (SvTYPE(sv) == SVt_PVCV) {
2458 SETs(sv);
2459 RETURN;
2460 }
2461 }
2462
56304f61 2463 name = SvPV(TOPs, na);
a0d0e21e
LW
2464 sv = *(stack_base + TOPMARK + 1);
2465
16d20bd9
AD
2466 if (SvGMAGICAL(sv))
2467 mg_get(sv);
a0d0e21e 2468 if (SvROK(sv))
16d20bd9 2469 ob = (SV*)SvRV(sv);
a0d0e21e
LW
2470 else {
2471 GV* iogv;
a0d0e21e 2472
56304f61 2473 packname = Nullch;
a0d0e21e 2474 if (!SvOK(sv) ||
56304f61 2475 !(packname = SvPV(sv, packlen)) ||
a0d0e21e
LW
2476 !(iogv = gv_fetchpv(packname, FALSE, SVt_PVIO)) ||
2477 !(ob=(SV*)GvIO(iogv)))
2478 {
56304f61 2479 if (!packname || !isIDFIRST(*packname))
72b5445b
GS
2480 DIE("Can't call method \"%s\" %s", name,
2481 SvOK(sv)? "without a package or object reference"
2482 : "on an undefined value");
56304f61 2483 stash = gv_stashpvn(packname, packlen, TRUE);
ac91690f 2484 goto fetch;
a0d0e21e 2485 }
ac91690f 2486 *(stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
a0d0e21e
LW
2487 }
2488
ac91690f 2489 if (!ob || !SvOBJECT(ob))
a0d0e21e 2490 DIE("Can't call method \"%s\" on unblessed reference", name);
a0d0e21e 2491
56304f61 2492 stash = SvSTASH(ob);
a0d0e21e 2493
ac91690f 2494 fetch:
ac91690f 2495 gv = gv_fetchmethod(stash, name);
56304f61
CS
2496 if (!gv) {
2497 char* leaf = name;
2498 char* sep = Nullch;
2499 char* p;
2500
2501 for (p = name; *p; p++) {
2502 if (*p == '\'')
2503 sep = p, leaf = p + 1;
2504 else if (*p == ':' && *(p + 1) == ':')
2505 sep = p, leaf = p + 2;
2506 }
2507 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
2508 packname = HvNAME(sep ? curcop->cop_stash : stash);
2509 packlen = strlen(packname);
2510 }
2511 else {
2512 packname = name;
2513 packlen = sep - name;
2514 }
2515 DIE("Can't locate object method \"%s\" via package \"%.*s\"",
2516 leaf, (int)packlen, packname);
2517 }
8ebc5c01 2518 SETs(isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv);
a0d0e21e
LW
2519 RETURN;
2520}
22239a37 2521