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