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