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