This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[win32] merge change#904 from maintbranch
[perl5.git] / pp_hot.c
CommitLineData
a0d0e21e
LW
1/* pp_hot.c
2 *
9607fc9c 3 * Copyright (c) 1991-1997, Larry Wall
a0d0e21e
LW
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
8 */
9
10/*
11 * Then he heard Merry change the note, and up went the Horn-cry of Buckland,
12 * shaking the air.
13 *
14 * Awake! Awake! Fear, Fire, Foes! Awake!
15 * Fire, Foes! Awake!
16 */
17
18#include "EXTERN.h"
19#include "perl.h"
20
8ac85365
NIS
21#ifdef I_UNISTD
22#include <unistd.h>
23#endif
24
a0d0e21e
LW
25/* Hot code. */
26
11343788
MB
27#ifdef USE_THREADS
28static void
8ac85365 29unset_cvowner(void *cvarg)
11343788
MB
30{
31 register CV* cv = (CV *) cvarg;
32#ifdef DEBUGGING
33 dTHR;
34#endif /* DEBUGGING */
35
1fd28e87
MB
36 DEBUG_L((PerlIO_printf(PerlIO_stderr(), "%p unsetting CvOWNER of %p:%s\n",
37 thr, cv, SvPEEK((SV*)cv))));
11343788 38 MUTEX_LOCK(CvMUTEXP(cv));
77a005ab
MB
39 DEBUG_L(if (CvDEPTH(cv) != 0)
40 PerlIO_printf(PerlIO_stderr(), "depth %ld != 0\n",
41 CvDEPTH(cv)););
11343788
MB
42 assert(thr == CvOWNER(cv));
43 CvOWNER(cv) = 0;
11343788
MB
44 MUTEX_UNLOCK(CvMUTEXP(cv));
45 SvREFCNT_dec(cv);
46}
11343788
MB
47#endif /* USE_THREADS */
48
a0d0e21e
LW
49PP(pp_const)
50{
4e35701f 51 djSP;
a0d0e21e
LW
52 XPUSHs(cSVOP->op_sv);
53 RETURN;
54}
55
56PP(pp_nextstate)
57{
58 curcop = (COP*)op;
59 TAINT_NOT; /* Each statement is presumed innocent */
60 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
61 FREETMPS;
62 return NORMAL;
63}
64
65PP(pp_gvsv)
66{
4e35701f 67 djSP;
924508f0 68 EXTEND(SP,1);
a0d0e21e
LW
69 if (op->op_private & OPpLVAL_INTRO)
70 PUSHs(save_scalar(cGVOP->op_gv));
71 else
72 PUSHs(GvSV(cGVOP->op_gv));
73 RETURN;
74}
75
76PP(pp_null)
77{
78 return NORMAL;
79}
80
81PP(pp_pushmark)
82{
83 PUSHMARK(stack_sp);
84 return NORMAL;
85}
86
87PP(pp_stringify)
88{
4e35701f 89 djSP; dTARGET;
a0d0e21e
LW
90 STRLEN len;
91 char *s;
92 s = SvPV(TOPs,len);
93 sv_setpvn(TARG,s,len);
94 SETTARG;
95 RETURN;
96}
97
98PP(pp_gv)
99{
4e35701f 100 djSP;
a0d0e21e
LW
101 XPUSHs((SV*)cGVOP->op_gv);
102 RETURN;
103}
104
105PP(pp_and)
106{
4e35701f 107 djSP;
a0d0e21e
LW
108 if (!SvTRUE(TOPs))
109 RETURN;
110 else {
111 --SP;
112 RETURNOP(cLOGOP->op_other);
113 }
114}
115
116PP(pp_sassign)
117{
4e35701f 118 djSP; dPOPTOPssrl;
748a9306
LW
119 MAGIC *mg;
120
a0d0e21e
LW
121 if (op->op_private & OPpASSIGN_BACKWARDS) {
122 SV *temp;
123 temp = left; left = right; right = temp;
124 }
bbce6d69 125 if (tainting && tainted && !SvTAINTED(left))
a0d0e21e 126 TAINT_NOT;
54310121 127 SvSetMagicSV(right, left);
a0d0e21e
LW
128 SETs(right);
129 RETURN;
130}
131
132PP(pp_cond_expr)
133{
4e35701f 134 djSP;
a0d0e21e
LW
135 if (SvTRUEx(POPs))
136 RETURNOP(cCONDOP->op_true);
137 else
138 RETURNOP(cCONDOP->op_false);
139}
140
141PP(pp_unstack)
142{
143 I32 oldsave;
144 TAINT_NOT; /* Each statement is presumed innocent */
145 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
146 FREETMPS;
147 oldsave = scopestack[scopestack_ix - 1];
148 LEAVE_SCOPE(oldsave);
149 return NORMAL;
150}
151
a0d0e21e
LW
152PP(pp_concat)
153{
4e35701f 154 djSP; dATARGET; tryAMAGICbin(concat,opASSIGN);
748a9306
LW
155 {
156 dPOPTOPssrl;
a0d0e21e
LW
157 STRLEN len;
158 char *s;
159 if (TARG != left) {
160 s = SvPV(left,len);
161 sv_setpvn(TARG,s,len);
162 }
c07a80fd
PP
163 else if (SvGMAGICAL(TARG))
164 mg_get(TARG);
7a4c00b4 165 else if (!SvOK(TARG) && SvTYPE(TARG) <= SVt_PVMG) {
748a9306 166 sv_setpv(TARG, ""); /* Suppress warning. */
7a4c00b4 167 s = SvPV_force(TARG, len);
4633a7c4 168 }
a0d0e21e 169 s = SvPV(right,len);
68dc0745
PP
170 if (SvOK(TARG))
171 sv_catpvn(TARG,s,len);
172 else
173 sv_setpvn(TARG,s,len); /* suppress warning */
a0d0e21e
LW
174 SETTARG;
175 RETURN;
748a9306 176 }
a0d0e21e
LW
177}
178
179PP(pp_padsv)
180{
4e35701f 181 djSP; dTARGET;
a0d0e21e 182 XPUSHs(TARG);
4633a7c4
LW
183 if (op->op_flags & OPf_MOD) {
184 if (op->op_private & OPpLVAL_INTRO)
185 SAVECLEARSV(curpad[op->op_targ]);
8ec5e241
NIS
186 else if (op->op_private & OPpDEREF) {
187 PUTBACK;
54310121 188 vivify_ref(curpad[op->op_targ], op->op_private & OPpDEREF);
8ec5e241
NIS
189 SPAGAIN;
190 }
4633a7c4 191 }
a0d0e21e
LW
192 RETURN;
193}
194
195PP(pp_readline)
196{
197 last_in_gv = (GV*)(*stack_sp--);
198 return do_readline();
199}
200
201PP(pp_eq)
202{
4e35701f 203 djSP; tryAMAGICbinSET(eq,0);
a0d0e21e
LW
204 {
205 dPOPnv;
54310121 206 SETs(boolSV(TOPn == value));
a0d0e21e
LW
207 RETURN;
208 }
209}
210
211PP(pp_preinc)
212{
4e35701f 213 djSP;
68dc0745 214 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
44a8e56a 215 croak(no_modify);
55497cff
PP
216 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
217 SvIVX(TOPs) != IV_MAX)
218 {
748a9306 219 ++SvIVX(TOPs);
55497cff 220 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
221 }
222 else
223 sv_inc(TOPs);
a0d0e21e
LW
224 SvSETMAGIC(TOPs);
225 return NORMAL;
226}
227
228PP(pp_or)
229{
4e35701f 230 djSP;
a0d0e21e
LW
231 if (SvTRUE(TOPs))
232 RETURN;
233 else {
234 --SP;
235 RETURNOP(cLOGOP->op_other);
236 }
237}
238
239PP(pp_add)
240{
4e35701f 241 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
a0d0e21e 242 {
7a4c00b4 243 dPOPTOPnnrl_ul;
a0d0e21e
LW
244 SETn( left + right );
245 RETURN;
246 }
247}
248
249PP(pp_aelemfast)
250{
4e35701f 251 djSP;
a0d0e21e
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 }
1930e939
TP
647 if (relem == lastrelem && dowarn) {
648 if (relem == firstrelem &&
649 SvROK(*relem) &&
650 ( SvTYPE(SvRV(*relem)) == SVt_PVAV ||
651 SvTYPE(SvRV(*relem)) == SVt_PVHV ) )
652 warn("Reference found where even-sized list expected");
653 else
654 warn("Odd number of elements in hash assignment");
655 }
a0d0e21e
LW
656 }
657 break;
658 default:
659 if (SvTHINKFIRST(sv)) {
660 if (SvREADONLY(sv) && curcop != &compiling) {
661 if (sv != &sv_undef && sv != &sv_yes && sv != &sv_no)
662 DIE(no_modify);
663 if (relem <= lastrelem)
664 relem++;
665 break;
666 }
667 if (SvROK(sv))
668 sv_unref(sv);
669 }
670 if (relem <= lastrelem) {
671 sv_setsv(sv, *relem);
672 *(relem++) = sv;
673 }
674 else
675 sv_setsv(sv, &sv_undef);
676 SvSETMAGIC(sv);
677 break;
678 }
679 }
680 if (delaymagic & ~DM_DELAY) {
681 if (delaymagic & DM_UID) {
682#ifdef HAS_SETRESUID
683 (void)setresuid(uid,euid,(Uid_t)-1);
56febc5e
AD
684#else
685# ifdef HAS_SETREUID
a0d0e21e 686 (void)setreuid(uid,euid);
56febc5e
AD
687# else
688# ifdef HAS_SETRUID
a0d0e21e
LW
689 if ((delaymagic & DM_UID) == DM_RUID) {
690 (void)setruid(uid);
748a9306 691 delaymagic &= ~DM_RUID;
a0d0e21e 692 }
56febc5e
AD
693# endif /* HAS_SETRUID */
694# ifdef HAS_SETEUID
a0d0e21e
LW
695 if ((delaymagic & DM_UID) == DM_EUID) {
696 (void)seteuid(uid);
748a9306 697 delaymagic &= ~DM_EUID;
a0d0e21e 698 }
56febc5e 699# endif /* HAS_SETEUID */
a0d0e21e
LW
700 if (delaymagic & DM_UID) {
701 if (uid != euid)
702 DIE("No setreuid available");
703 (void)setuid(uid);
704 }
56febc5e
AD
705# endif /* HAS_SETREUID */
706#endif /* HAS_SETRESUID */
a0d0e21e
LW
707 uid = (int)getuid();
708 euid = (int)geteuid();
709 }
710 if (delaymagic & DM_GID) {
711#ifdef HAS_SETRESGID
712 (void)setresgid(gid,egid,(Gid_t)-1);
56febc5e
AD
713#else
714# ifdef HAS_SETREGID
a0d0e21e 715 (void)setregid(gid,egid);
56febc5e
AD
716# else
717# ifdef HAS_SETRGID
a0d0e21e
LW
718 if ((delaymagic & DM_GID) == DM_RGID) {
719 (void)setrgid(gid);
748a9306 720 delaymagic &= ~DM_RGID;
a0d0e21e 721 }
56febc5e
AD
722# endif /* HAS_SETRGID */
723# ifdef HAS_SETEGID
a0d0e21e
LW
724 if ((delaymagic & DM_GID) == DM_EGID) {
725 (void)setegid(gid);
748a9306 726 delaymagic &= ~DM_EGID;
a0d0e21e 727 }
56febc5e 728# endif /* HAS_SETEGID */
a0d0e21e
LW
729 if (delaymagic & DM_GID) {
730 if (gid != egid)
731 DIE("No setregid available");
732 (void)setgid(gid);
733 }
56febc5e
AD
734# endif /* HAS_SETREGID */
735#endif /* HAS_SETRESGID */
a0d0e21e
LW
736 gid = (int)getgid();
737 egid = (int)getegid();
738 }
4633a7c4 739 tainting |= (uid && (euid != uid || egid != gid));
a0d0e21e
LW
740 }
741 delaymagic = 0;
54310121
PP
742
743 gimme = GIMME_V;
744 if (gimme == G_VOID)
745 SP = firstrelem - 1;
746 else if (gimme == G_SCALAR) {
747 dTARGET;
748 SP = firstrelem;
749 SETi(lastrelem - firstrelem + 1);
750 }
751 else {
a0d0e21e
LW
752 if (ary || hash)
753 SP = lastrelem;
754 else
755 SP = firstrelem + (lastlelem - firstlelem);
0c8c7a05 756 lelem = firstlelem + (relem - firstrelem);
5f05dabc 757 while (relem <= SP)
0c8c7a05 758 *relem++ = (lelem <= lastlelem) ? *lelem++ : &sv_undef;
a0d0e21e 759 }
54310121 760 RETURN;
a0d0e21e
LW
761}
762
763PP(pp_match)
764{
4e35701f 765 djSP; dTARG;
a0d0e21e
LW
766 register PMOP *pm = cPMOP;
767 register char *t;
768 register char *s;
769 char *strend;
770 I32 global;
771 I32 safebase;
772 char *truebase;
773 register REGEXP *rx = pm->op_pmregexp;
774 I32 gimme = GIMME;
775 STRLEN len;
748a9306 776 I32 minmatch = 0;
4633a7c4 777 I32 oldsave = savestack_ix;
f86702cc 778 I32 update_minmatch = 1;
c277df42 779 SV *screamer;
a0d0e21e
LW
780
781 if (op->op_flags & OPf_STACKED)
782 TARG = POPs;
783 else {
54b9620d 784 TARG = DEFSV;
a0d0e21e
LW
785 EXTEND(SP,1);
786 }
c277df42 787 PUTBACK; /* EVAL blocks need stack_sp. */
a0d0e21e
LW
788 s = SvPV(TARG, len);
789 strend = s + len;
790 if (!s)
791 DIE("panic: do_match");
9212bbba 792 TAINT_NOT;
a0d0e21e
LW
793
794 if (pm->op_pmflags & PMf_USED) {
c277df42 795 failure:
a0d0e21e
LW
796 if (gimme == G_ARRAY)
797 RETURN;
798 RETPUSHNO;
799 }
800
801 if (!rx->prelen && curpm) {
802 pm = curpm;
803 rx = pm->op_pmregexp;
804 }
c277df42
IZ
805 if (rx->minlen > len) goto failure;
806
807 screamer = ( (SvSCREAM(TARG) && rx->check_substr
808 && SvTYPE(rx->check_substr) == SVt_PVBM
809 && SvVALID(rx->check_substr))
810 ? TARG : Nullsv);
a0d0e21e
LW
811 truebase = t = s;
812 if (global = pm->op_pmflags & PMf_GLOBAL) {
813 rx->startp[0] = 0;
814 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
815 MAGIC* mg = mg_find(TARG, 'g');
748a9306 816 if (mg && mg->mg_len >= 0) {
a0d0e21e 817 rx->endp[0] = rx->startp[0] = s + mg->mg_len;
748a9306 818 minmatch = (mg->mg_flags & MGf_MINMATCH);
f86702cc 819 update_minmatch = 0;
748a9306 820 }
a0d0e21e
LW
821 }
822 }
748a9306
LW
823 if (!rx->nparens && !global)
824 gimme = G_SCALAR; /* accidental array context? */
137443ea
PP
825 safebase = (((gimme == G_ARRAY) || global || !rx->nparens)
826 && !sawampersand);
c277df42 827 safebase = safebase ? 0 : REXEC_COPY_STR ;
a0d0e21e
LW
828 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
829 SAVEINT(multiline);
830 multiline = pm->op_pmflags & PMf_MULTILINE;
831 }
832
833play_it_again:
834 if (global && rx->startp[0]) {
835 t = s = rx->endp[0];
51aa1f50 836 if ((s + rx->minlen) > strend)
a0d0e21e 837 goto nope;
f86702cc
PP
838 if (update_minmatch++)
839 minmatch = (s == rx->startp[0]);
a0d0e21e 840 }
c277df42
IZ
841 if (rx->check_substr) {
842 if (!(rx->reganch & ROPT_NOSCAN)) { /* Floating checkstring. */
843 if ( screamer ) {
844 I32 p = -1;
845
846 if (screamfirst[BmRARE(rx->check_substr)] < 0)
a0d0e21e 847 goto nope;
c277df42
IZ
848 else if (!(s = screaminstr(TARG, rx->check_substr,
849 rx->check_offset_min, 0, &p, 0)))
a0d0e21e 850 goto nope;
c277df42
IZ
851 else if ((rx->reganch & ROPT_CHECK_ALL)
852 && !sawampersand && !SvTAIL(rx->check_substr))
a0d0e21e
LW
853 goto yup;
854 }
c277df42
IZ
855 else if (!(s = fbm_instr((unsigned char*)s + rx->check_offset_min,
856 (unsigned char*)strend,
857 rx->check_substr)))
a0d0e21e 858 goto nope;
c277df42 859 else if ((rx->reganch & ROPT_CHECK_ALL) && !sawampersand)
a0d0e21e 860 goto yup;
c277df42
IZ
861 if (s && rx->check_offset_max < t - s) {
862 ++BmUSEFUL(rx->check_substr);
863 s -= rx->check_offset_max;
a0d0e21e
LW
864 }
865 else
866 s = t;
867 }
c277df42
IZ
868 /* Now checkstring is fixed, i.e. at fixed offset from the
869 beginning of match, and the match is anchored at s. */
870 else if (!multiline) { /* Anchored near beginning of string. */
871 I32 slen;
872 if (*SvPVX(rx->check_substr) != s[rx->check_offset_min]
873 || ((slen = SvCUR(rx->check_substr)) > 1
874 && memNE(SvPVX(rx->check_substr),
875 s + rx->check_offset_min, slen)))
bbce6d69 876 goto nope;
a0d0e21e 877 }
c277df42
IZ
878 if (!rx->naughty && --BmUSEFUL(rx->check_substr) < 0
879 && rx->check_substr == rx->float_substr) {
880 SvREFCNT_dec(rx->check_substr);
881 rx->check_substr = Nullsv; /* opt is being useless */
882 rx->float_substr = Nullsv;
a0d0e21e
LW
883 }
884 }
c277df42
IZ
885 if (regexec_flags(rx, s, strend, truebase, minmatch,
886 screamer, NULL, safebase))
bbce6d69 887 {
a0d0e21e
LW
888 curpm = pm;
889 if (pm->op_pmflags & PMf_ONCE)
890 pm->op_pmflags |= PMf_USED;
891 goto gotcha;
892 }
893 else
894 goto ret_no;
895 /*NOTREACHED*/
896
897 gotcha:
c277df42 898 TAINT_IF(RX_MATCH_TAINTED(rx));
a0d0e21e
LW
899 if (gimme == G_ARRAY) {
900 I32 iters, i, len;
901
902 iters = rx->nparens;
903 if (global && !iters)
904 i = 1;
905 else
906 i = 0;
c277df42 907 SPAGAIN; /* EVAL blocks could move the stack. */
a0d0e21e 908 EXTEND(SP, iters + i);
bbce6d69 909 EXTEND_MORTAL(iters + i);
a0d0e21e
LW
910 for (i = !i; i <= iters; i++) {
911 PUSHs(sv_newmortal());
912 /*SUPPRESS 560*/
913 if ((s = rx->startp[i]) && rx->endp[i] ) {
914 len = rx->endp[i] - s;
915 sv_setpvn(*SP, s, len);
916 }
917 }
918 if (global) {
919 truebase = rx->subbeg;
5f05dabc 920 strend = rx->subend;
a0d0e21e
LW
921 if (rx->startp[0] && rx->startp[0] == rx->endp[0])
922 ++rx->endp[0];
c277df42 923 PUTBACK; /* EVAL blocks may use stack */
a0d0e21e
LW
924 goto play_it_again;
925 }
4633a7c4 926 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
927 RETURN;
928 }
929 else {
930 if (global) {
931 MAGIC* mg = 0;
932 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
933 mg = mg_find(TARG, 'g');
934 if (!mg) {
935 sv_magic(TARG, (SV*)0, 'g', Nullch, 0);
936 mg = mg_find(TARG, 'g');
937 }
748a9306 938 if (rx->startp[0]) {
5f05dabc 939 mg->mg_len = rx->endp[0] - rx->subbeg;
748a9306
LW
940 if (rx->startp[0] == rx->endp[0])
941 mg->mg_flags |= MGf_MINMATCH;
942 else
943 mg->mg_flags &= ~MGf_MINMATCH;
944 }
a0d0e21e 945 }
4633a7c4 946 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
947 RETPUSHYES;
948 }
949
c277df42
IZ
950yup: /* Confirmed by check_substr */
951 TAINT_IF(RX_MATCH_TAINTED(rx));
952 ++BmUSEFUL(rx->check_substr);
a0d0e21e
LW
953 curpm = pm;
954 if (pm->op_pmflags & PMf_ONCE)
955 pm->op_pmflags |= PMf_USED;
5f05dabc
PP
956 Safefree(rx->subbase);
957 rx->subbase = Nullch;
a0d0e21e
LW
958 if (global) {
959 rx->subbeg = truebase;
960 rx->subend = strend;
961 rx->startp[0] = s;
c277df42 962 rx->endp[0] = s + SvCUR(rx->check_substr);
a0d0e21e
LW
963 goto gotcha;
964 }
965 if (sawampersand) {
966 char *tmps;
967
a0d0e21e
LW
968 tmps = rx->subbase = savepvn(t, strend-t);
969 rx->subbeg = tmps;
970 rx->subend = tmps + (strend-t);
971 tmps = rx->startp[0] = tmps + (s - t);
c277df42 972 rx->endp[0] = tmps + SvCUR(rx->check_substr);
a0d0e21e 973 }
4633a7c4 974 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
975 RETPUSHYES;
976
977nope:
c277df42
IZ
978 if (rx->check_substr)
979 ++BmUSEFUL(rx->check_substr);
a0d0e21e
LW
980
981ret_no:
c90c0ff4 982 if (global && !(pm->op_pmflags & PMf_CONTINUE)) {
a0d0e21e
LW
983 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
984 MAGIC* mg = mg_find(TARG, 'g');
985 if (mg)
986 mg->mg_len = -1;
987 }
988 }
4633a7c4 989 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
990 if (gimme == G_ARRAY)
991 RETURN;
992 RETPUSHNO;
993}
994
995OP *
8ac85365 996do_readline(void)
a0d0e21e
LW
997{
998 dSP; dTARGETSTACKED;
999 register SV *sv;
1000 STRLEN tmplen = 0;
1001 STRLEN offset;
760ac839 1002 PerlIO *fp;
a0d0e21e
LW
1003 register IO *io = GvIO(last_in_gv);
1004 register I32 type = op->op_type;
54310121 1005 I32 gimme = GIMME_V;
e79b0511 1006 MAGIC *mg;
a0d0e21e 1007
3049007d 1008 if (SvRMAGICAL(last_in_gv) && (mg = mg_find((SV*)last_in_gv, 'q'))) {
e79b0511
PP
1009 PUSHMARK(SP);
1010 XPUSHs(mg->mg_obj);
1011 PUTBACK;
1012 ENTER;
54310121 1013 perl_call_method("READLINE", gimme);
e79b0511
PP
1014 LEAVE;
1015 SPAGAIN;
54310121
PP
1016 if (gimme == G_SCALAR)
1017 SvSetMagicSV_nosteal(TARG, TOPs);
e79b0511
PP
1018 RETURN;
1019 }
a0d0e21e
LW
1020 fp = Nullfp;
1021 if (io) {
1022 fp = IoIFP(io);
1023 if (!fp) {
1024 if (IoFLAGS(io) & IOf_ARGV) {
1025 if (IoFLAGS(io) & IOf_START) {
1026 IoFLAGS(io) &= ~IOf_START;
1027 IoLINES(io) = 0;
1028 if (av_len(GvAVn(last_in_gv)) < 0) {
1029 SV *tmpstr = newSVpv("-", 1); /* assume stdin */
1030 av_push(GvAVn(last_in_gv), tmpstr);
1031 }
1032 }
1033 fp = nextargv(last_in_gv);
1034 if (!fp) { /* Note: fp != IoIFP(io) */
1035 (void)do_close(last_in_gv, FALSE); /* now it does*/
1036 IoFLAGS(io) |= IOf_START;
1037 }
1038 }
1039 else if (type == OP_GLOB) {
1040 SV *tmpcmd = NEWSV(55, 0);
1041 SV *tmpglob = POPs;
1042 ENTER;
1043 SAVEFREESV(tmpcmd);
1044#ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
1045 /* since spawning off a process is a real performance hit */
1046 {
1047#include <descrip.h>
1048#include <lib$routines.h>
1049#include <nam.h>
1050#include <rmsdef.h>
1051 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
1052 char vmsspec[NAM$C_MAXRSS+1];
1053 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
1054 char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
1055 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
760ac839 1056 PerlIO *tmpfp;
a0d0e21e
LW
1057 STRLEN i;
1058 struct dsc$descriptor_s wilddsc
1059 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
1060 struct dsc$descriptor_vs rsdsc
1061 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
1062 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
1063
1064 /* We could find out if there's an explicit dev/dir or version
1065 by peeking into lib$find_file's internal context at
1066 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
1067 but that's unsupported, so I don't want to do it now and
1068 have it bite someone in the future. */
3028581b 1069 strcat(tmpfnam,PerlLIO_tmpnam(NULL));
a0d0e21e
LW
1070 cp = SvPV(tmpglob,i);
1071 for (; i; i--) {
1072 if (cp[i] == ';') hasver = 1;
1073 if (cp[i] == '.') {
1074 if (sts) hasver = 1;
1075 else sts = 1;
1076 }
1077 if (cp[i] == '/') {
1078 hasdir = isunix = 1;
1079 break;
748a9306 1080 }
a0d0e21e
LW
1081 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
1082 hasdir = 1;
1083 break;
1084 }
1085 }
760ac839 1086 if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
09b7f37c 1087 Stat_t st;
c6ed36e1 1088 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
09b7f37c
CB
1089 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
1090 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
a0d0e21e
LW
1091 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
1092 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
1093 &dfltdsc,NULL,NULL,NULL))&1)) {
1094 end = rstr + (unsigned long int) *rslt;
1095 if (!hasver) while (*end != ';') end--;
1096 *(end++) = '\n'; *end = '\0';
1097 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
1098 if (hasdir) {
f86702cc 1099 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
a0d0e21e
LW
1100 begin = rstr;
1101 }
1102 else {
1103 begin = end;
1104 while (*(--begin) != ']' && *begin != '>') ;
1105 ++begin;
1106 }
760ac839 1107 ok = (PerlIO_puts(tmpfp,begin) != EOF);
a0d0e21e
LW
1108 }
1109 if (cxt) (void)lib$find_file_end(&cxt);
748a9306
LW
1110 if (ok && sts != RMS$_NMF &&
1111 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
a0d0e21e 1112 if (!ok) {
c07a80fd
PP
1113 if (!(sts & 1)) {
1114 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
1115 }
760ac839 1116 PerlIO_close(tmpfp);
a0d0e21e
LW
1117 fp = NULL;
1118 }
1119 else {
760ac839 1120 PerlIO_rewind(tmpfp);
a0d0e21e
LW
1121 IoTYPE(io) = '<';
1122 IoIFP(io) = fp = tmpfp;
1e422769 1123 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
a0d0e21e
LW
1124 }
1125 }
1126 }
1127#else /* !VMS */
1128#ifdef DOSISH
67955e0c
PP
1129#ifdef OS2
1130 sv_setpv(tmpcmd, "for a in ");
1131 sv_catsv(tmpcmd, tmpglob);
1132 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
1133#else
39e571d4
ML
1134#ifdef DJGPP
1135 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
1136 sv_catsv(tmpcmd, tmpglob);
1137#else
a0d0e21e
LW
1138 sv_setpv(tmpcmd, "perlglob ");
1139 sv_catsv(tmpcmd, tmpglob);
1140 sv_catpv(tmpcmd, " |");
39e571d4 1141#endif /* !DJGPP */
67955e0c
PP
1142#endif /* !OS2 */
1143#else /* !DOSISH */
1144#if defined(CSH)
a0d0e21e
LW
1145 sv_setpvn(tmpcmd, cshname, cshlen);
1146 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
1147 sv_catsv(tmpcmd, tmpglob);
16d20bd9 1148 sv_catpv(tmpcmd, "' 2>/dev/null |");
a0d0e21e
LW
1149#else
1150 sv_setpv(tmpcmd, "echo ");
1151 sv_catsv(tmpcmd, tmpglob);
1152#if 'z' - 'a' == 25
1153 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
1154#else
1155 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
1156#endif
1157#endif /* !CSH */
67955e0c 1158#endif /* !DOSISH */
c07a80fd
PP
1159 (void)do_open(last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
1160 FALSE, 0, 0, Nullfp);
a0d0e21e
LW
1161 fp = IoIFP(io);
1162#endif /* !VMS */
1163 LEAVE;
1164 }
1165 }
1166 else if (type == OP_GLOB)
1167 SP--;
1168 }
1169 if (!fp) {
4633a7c4 1170 if (dowarn && io && !(IoFLAGS(io) & IOf_START))
a0d0e21e 1171 warn("Read on closed filehandle <%s>", GvENAME(last_in_gv));
54310121 1172 if (gimme == G_SCALAR) {
a0d0e21e
LW
1173 (void)SvOK_off(TARG);
1174 PUSHTARG;
1175 }
1176 RETURN;
1177 }
54310121 1178 if (gimme == G_SCALAR) {
a0d0e21e 1179 sv = TARG;
9607fc9c
PP
1180 if (SvROK(sv))
1181 sv_unref(sv);
a0d0e21e
LW
1182 (void)SvUPGRADE(sv, SVt_PV);
1183 tmplen = SvLEN(sv); /* remember if already alloced */
1184 if (!tmplen)
1185 Sv_Grow(sv, 80); /* try short-buffering it */
1186 if (type == OP_RCATLINE)
1187 offset = SvCUR(sv);
1188 else
1189 offset = 0;
1190 }
54310121
PP
1191 else {
1192 sv = sv_2mortal(NEWSV(57, 80));
1193 offset = 0;
1194 }
a0d0e21e
LW
1195 for (;;) {
1196 if (!sv_gets(sv, fp, offset)) {
760ac839 1197 PerlIO_clearerr(fp);
a0d0e21e
LW
1198 if (IoFLAGS(io) & IOf_ARGV) {
1199 fp = nextargv(last_in_gv);
1200 if (fp)
1201 continue;
1202 (void)do_close(last_in_gv, FALSE);
1203 IoFLAGS(io) |= IOf_START;
1204 }
1205 else if (type == OP_GLOB) {
5cd24f17
PP
1206 if (do_close(last_in_gv, FALSE) & ~0xFF)
1207 warn("internal error: glob failed");
a0d0e21e 1208 }
54310121 1209 if (gimme == G_SCALAR) {
a0d0e21e
LW
1210 (void)SvOK_off(TARG);
1211 PUSHTARG;
1212 }
1213 RETURN;
1214 }
bbce6d69
PP
1215 /* This should not be marked tainted if the fp is marked clean */
1216 if (!(IoFLAGS(io) & IOf_UNTAINT)) {
1217 TAINT;
1218 SvTAINTED_on(sv);
1219 }
a0d0e21e 1220 IoLINES(io)++;
71be2cbc 1221 SvSETMAGIC(sv);
a0d0e21e 1222 XPUSHs(sv);
a0d0e21e
LW
1223 if (type == OP_GLOB) {
1224 char *tmps;
1225
c07a80fd
PP
1226 if (SvCUR(sv) > 0 && SvCUR(rs) > 0) {
1227 tmps = SvEND(sv) - 1;
1228 if (*tmps == *SvPVX(rs)) {
1229 *tmps = '\0';
1230 SvCUR(sv)--;
1231 }
1232 }
a0d0e21e
LW
1233 for (tmps = SvPVX(sv); *tmps; tmps++)
1234 if (!isALPHA(*tmps) && !isDIGIT(*tmps) &&
1235 strchr("$&*(){}[]'\";\\|?<>~`", *tmps))
1236 break;
c6ed36e1 1237 if (*tmps && PerlLIO_stat(SvPVX(sv), &statbuf) < 0) {
a0d0e21e
LW
1238 (void)POPs; /* Unmatched wildcard? Chuck it... */
1239 continue;
1240 }
1241 }
54310121 1242 if (gimme == G_ARRAY) {
a0d0e21e
LW
1243 if (SvLEN(sv) - SvCUR(sv) > 20) {
1244 SvLEN_set(sv, SvCUR(sv)+1);
1245 Renew(SvPVX(sv), SvLEN(sv), char);
1246 }
1247 sv = sv_2mortal(NEWSV(58, 80));
1248 continue;
1249 }
54310121 1250 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
a0d0e21e
LW
1251 /* try to reclaim a bit of scalar space (only on 1st alloc) */
1252 if (SvCUR(sv) < 60)
1253 SvLEN_set(sv, 80);
1254 else
1255 SvLEN_set(sv, SvCUR(sv)+40); /* allow some slop */
1256 Renew(SvPVX(sv), SvLEN(sv), char);
1257 }
1258 RETURN;
1259 }
1260}
1261
1262PP(pp_enter)
1263{
4e35701f 1264 djSP;
c09156bb 1265 register PERL_CONTEXT *cx;
54310121 1266 I32 gimme = OP_GIMME(op, -1);
a0d0e21e 1267
54310121
PP
1268 if (gimme == -1) {
1269 if (cxstack_ix >= 0)
1270 gimme = cxstack[cxstack_ix].blk_gimme;
1271 else
1272 gimme = G_SCALAR;
1273 }
a0d0e21e
LW
1274
1275 ENTER;
1276
1277 SAVETMPS;
924508f0 1278 PUSHBLOCK(cx, CXt_BLOCK, SP);
a0d0e21e
LW
1279
1280 RETURN;
1281}
1282
1283PP(pp_helem)
1284{
4e35701f 1285 djSP;
760ac839 1286 HE* he;
ae77835f 1287 SV **svp;
a0d0e21e 1288 SV *keysv = POPs;
a0d0e21e 1289 HV *hv = (HV*)POPs;
68dc0745
PP
1290 U32 lval = op->op_flags & OPf_MOD;
1291 U32 defer = op->op_private & OPpLVAL_DEFER;
a0d0e21e 1292
ae77835f
MB
1293 if (SvTYPE(hv) == SVt_PVHV) {
1294 he = hv_fetch_ent(hv, keysv, lval && !defer, 0);
97fcbf96 1295 svp = he ? &HeVAL(he) : 0;
ae77835f
MB
1296 }
1297 else if (SvTYPE(hv) == SVt_PVAV) {
97fcbf96 1298 svp = avhv_fetch_ent((AV*)hv, keysv, lval && !defer, 0);
ae77835f 1299 }
c750a3ec 1300 else {
a0d0e21e 1301 RETPUSHUNDEF;
c750a3ec 1302 }
a0d0e21e 1303 if (lval) {
97fcbf96 1304 if (!svp || *svp == &sv_undef) {
68dc0745
PP
1305 SV* lv;
1306 SV* key2;
1307 if (!defer)
1308 DIE(no_helem, SvPV(keysv, na));
1309 lv = sv_newmortal();
1310 sv_upgrade(lv, SVt_PVLV);
1311 LvTYPE(lv) = 'y';
1312 sv_magic(lv, key2 = newSVsv(keysv), 'y', Nullch, 0);
1313 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1314 LvTARG(lv) = SvREFCNT_inc(hv);
1315 LvTARGLEN(lv) = 1;
1316 PUSHs(lv);
1317 RETURN;
1318 }
5f05dabc 1319 if (op->op_private & OPpLVAL_INTRO) {
ae77835f
MB
1320 if (HvNAME(hv) && isGV(*svp))
1321 save_gp((GV*)*svp, !(op->op_flags & OPf_SPECIAL));
5f05dabc 1322 else
161b7d16 1323 save_helem(hv, keysv, svp);
5f05dabc
PP
1324 }
1325 else if (op->op_private & OPpDEREF)
ae77835f 1326 vivify_ref(*svp, op->op_private & OPpDEREF);
a0d0e21e
LW
1327 }
1328 PUSHs(svp ? *svp : &sv_undef);
1329 RETURN;
1330}
1331
1332PP(pp_leave)
1333{
4e35701f 1334 djSP;
c09156bb 1335 register PERL_CONTEXT *cx;
a0d0e21e
LW
1336 register SV **mark;
1337 SV **newsp;
1338 PMOP *newpm;
1339 I32 gimme;
1340
1341 if (op->op_flags & OPf_SPECIAL) {
1342 cx = &cxstack[cxstack_ix];
1343 cx->blk_oldpm = curpm; /* fake block should preserve $1 et al */
1344 }
1345
1346 POPBLOCK(cx,newpm);
1347
54310121
PP
1348 gimme = OP_GIMME(op, -1);
1349 if (gimme == -1) {
1350 if (cxstack_ix >= 0)
1351 gimme = cxstack[cxstack_ix].blk_gimme;
1352 else
1353 gimme = G_SCALAR;
1354 }
a0d0e21e 1355
a1f49e72 1356 TAINT_NOT;
54310121
PP
1357 if (gimme == G_VOID)
1358 SP = newsp;
1359 else if (gimme == G_SCALAR) {
1360 MARK = newsp + 1;
1361 if (MARK <= SP)
1362 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1363 *MARK = TOPs;
1364 else
1365 *MARK = sv_mortalcopy(TOPs);
a0d0e21e 1366 else {
54310121
PP
1367 MEXTEND(mark,0);
1368 *MARK = &sv_undef;
a0d0e21e 1369 }
54310121 1370 SP = MARK;
a0d0e21e 1371 }
54310121 1372 else if (gimme == G_ARRAY) {
a1f49e72
CS
1373 /* in case LEAVE wipes old return values */
1374 for (mark = newsp + 1; mark <= SP; mark++) {
1375 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
a0d0e21e 1376 *mark = sv_mortalcopy(*mark);
a1f49e72
CS
1377 TAINT_NOT; /* Each item is independent */
1378 }
1379 }
a0d0e21e
LW
1380 }
1381 curpm = newpm; /* Don't pop $1 et al till now */
1382
1383 LEAVE;
1384
1385 RETURN;
1386}
1387
1388PP(pp_iter)
1389{
4e35701f 1390 djSP;
c09156bb 1391 register PERL_CONTEXT *cx;
5f05dabc 1392 SV* sv;
4633a7c4 1393 AV* av;
a0d0e21e 1394
924508f0 1395 EXTEND(SP, 1);
a0d0e21e
LW
1396 cx = &cxstack[cxstack_ix];
1397 if (cx->cx_type != CXt_LOOP)
1398 DIE("panic: pp_iter");
1399
4633a7c4 1400 av = cx->blk_loop.iterary;
71be2cbc 1401 if (cx->blk_loop.iterix >= (av == curstack ? cx->blk_oldsp : AvFILL(av)))
4633a7c4 1402 RETPUSHNO;
a0d0e21e 1403
7a4c00b4 1404 SvREFCNT_dec(*cx->blk_loop.itervar);
a0d0e21e 1405
93965878
NIS
1406 if (sv = (SvMAGICAL(av))
1407 ? *av_fetch(av, ++cx->blk_loop.iterix, FALSE)
1408 : AvARRAY(av)[++cx->blk_loop.iterix])
a0d0e21e 1409 SvTEMP_off(sv);
a0d0e21e 1410 else
5f05dabc
PP
1411 sv = &sv_undef;
1412 if (av != curstack && SvIMMORTAL(sv)) {
1413 SV *lv = cx->blk_loop.iterlval;
71be2cbc
PP
1414 if (lv && SvREFCNT(lv) > 1) {
1415 SvREFCNT_dec(lv);
1416 lv = Nullsv;
1417 }
5f05dabc
PP
1418 if (lv)
1419 SvREFCNT_dec(LvTARG(lv));
1420 else {
68dc0745 1421 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
5f05dabc 1422 sv_upgrade(lv, SVt_PVLV);
5f05dabc 1423 LvTYPE(lv) = 'y';
68dc0745 1424 sv_magic(lv, Nullsv, 'y', Nullch, 0);
5f05dabc
PP
1425 }
1426 LvTARG(lv) = SvREFCNT_inc(av);
1427 LvTARGOFF(lv) = cx->blk_loop.iterix;
8ac85365 1428 LvTARGLEN(lv) = (UV) -1;
5f05dabc
PP
1429 sv = (SV*)lv;
1430 }
a0d0e21e 1431
7a4c00b4 1432 *cx->blk_loop.itervar = SvREFCNT_inc(sv);
a0d0e21e
LW
1433 RETPUSHYES;
1434}
1435
1436PP(pp_subst)
1437{
4e35701f 1438 djSP; dTARG;
a0d0e21e
LW
1439 register PMOP *pm = cPMOP;
1440 PMOP *rpm = pm;
1441 register SV *dstr;
1442 register char *s;
1443 char *strend;
1444 register char *m;
1445 char *c;
1446 register char *d;
1447 STRLEN clen;
1448 I32 iters = 0;
1449 I32 maxiters;
1450 register I32 i;
1451 bool once;
71be2cbc 1452 bool rxtainted;
a0d0e21e
LW
1453 char *orig;
1454 I32 safebase;
1455 register REGEXP *rx = pm->op_pmregexp;
1456 STRLEN len;
1457 int force_on_match = 0;
4633a7c4 1458 I32 oldsave = savestack_ix;
c277df42
IZ
1459 I32 update_minmatch = 1;
1460 SV *screamer;
a0d0e21e 1461
5cd24f17
PP
1462 /* known replacement string? */
1463 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
a0d0e21e
LW
1464 if (op->op_flags & OPf_STACKED)
1465 TARG = POPs;
1466 else {
54b9620d 1467 TARG = DEFSV;
a0d0e21e 1468 EXTEND(SP,1);
8ec5e241 1469 }
68dc0745
PP
1470 if (SvREADONLY(TARG)
1471 || (SvTYPE(TARG) > SVt_PVLV
1472 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG))))
1473 croak(no_modify);
8ec5e241
NIS
1474 PUTBACK;
1475
a0d0e21e 1476 s = SvPV(TARG, len);
68dc0745 1477 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
a0d0e21e 1478 force_on_match = 1;
9212bbba 1479 TAINT_NOT;
a0d0e21e
LW
1480
1481 force_it:
1482 if (!pm || !s)
1483 DIE("panic: do_subst");
1484
1485 strend = s + len;
1486 maxiters = (strend - s) + 10;
1487
1488 if (!rx->prelen && curpm) {
1489 pm = curpm;
1490 rx = pm->op_pmregexp;
1491 }
c277df42
IZ
1492 screamer = ( (SvSCREAM(TARG) && rx->check_substr
1493 && SvTYPE(rx->check_substr) == SVt_PVBM
1494 && SvVALID(rx->check_substr))
1495 ? TARG : Nullsv);
1496 safebase = (!rx->nparens && !sawampersand) ? 0 : REXEC_COPY_STR;
a0d0e21e
LW
1497 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
1498 SAVEINT(multiline);
1499 multiline = pm->op_pmflags & PMf_MULTILINE;
1500 }
1501 orig = m = s;
c277df42
IZ
1502 if (rx->check_substr) {
1503 if (!(rx->reganch & ROPT_NOSCAN)) { /* It floats. */
1504 if (screamer) {
1505 I32 p = -1;
1506
1507 if (screamfirst[BmRARE(rx->check_substr)] < 0)
a0d0e21e 1508 goto nope;
c277df42 1509 else if (!(s = screaminstr(TARG, rx->check_substr, rx->check_offset_min, 0, &p, 0)))
a0d0e21e
LW
1510 goto nope;
1511 }
c277df42
IZ
1512 else if (!(s = fbm_instr((unsigned char*)s + rx->check_offset_min,
1513 (unsigned char*)strend,
1514 rx->check_substr)))
a0d0e21e 1515 goto nope;
c277df42
IZ
1516 if (s && rx->check_offset_max < s - m) {
1517 ++BmUSEFUL(rx->check_substr);
1518 s -= rx->check_offset_max;
a0d0e21e
LW
1519 }
1520 else
1521 s = m;
1522 }
c277df42
IZ
1523 /* Now checkstring is fixed, i.e. at fixed offset from the
1524 beginning of match, and the match is anchored at s. */
1525 else if (!multiline) { /* Anchored at beginning of string. */
1526 I32 slen;
1527 if (*SvPVX(rx->check_substr) != s[rx->check_offset_min]
1528 || ((slen = SvCUR(rx->check_substr)) > 1
1529 && memNE(SvPVX(rx->check_substr),
1530 s + rx->check_offset_min, slen)))
bbce6d69 1531 goto nope;
a0d0e21e 1532 }
c277df42
IZ
1533 if (!rx->naughty && --BmUSEFUL(rx->check_substr) < 0
1534 && rx->check_substr == rx->float_substr) {
1535 SvREFCNT_dec(rx->check_substr);
1536 rx->check_substr = Nullsv; /* opt is being useless */
1537 rx->float_substr = Nullsv;
a0d0e21e
LW
1538 }
1539 }
71be2cbc
PP
1540
1541 /* only replace once? */
a0d0e21e 1542 once = !(rpm->op_pmflags & PMf_GLOBAL);
71be2cbc
PP
1543
1544 /* known replacement string? */
5cd24f17 1545 c = dstr ? SvPV(dstr, clen) : Nullch;
71be2cbc
PP
1546
1547 /* can do inplace substitution? */
c277df42
IZ
1548 if (c && clen <= rx->minlen && (once || !(safebase & REXEC_COPY_STR))
1549 && !(rx->reganch & ROPT_LOOKBEHIND_SEEN)) {
1550 if (!regexec_flags(rx, s, strend, orig, 0, screamer, NULL, safebase)) {
8ec5e241 1551 SPAGAIN;
71be2cbc
PP
1552 PUSHs(&sv_no);
1553 LEAVE_SCOPE(oldsave);
1554 RETURN;
1555 }
1556 if (force_on_match) {
1557 force_on_match = 0;
1558 s = SvPV_force(TARG, len);
1559 goto force_it;
1560 }
71be2cbc
PP
1561 d = s;
1562 curpm = pm;
1563 SvSCREAM_off(TARG); /* disable possible screamer */
1564 if (once) {
c277df42
IZ
1565 rxtainted = RX_MATCH_TAINTED(rx);
1566 if (rx->subbase) {
1567 m = orig + (rx->startp[0] - rx->subbase);
1568 d = orig + (rx->endp[0] - rx->subbase);
1569 } else {
1570 m = rx->startp[0];
1571 d = rx->endp[0];
1572 }
71be2cbc
PP
1573 s = orig;
1574 if (m - s > strend - d) { /* faster to shorten from end */
1575 if (clen) {
1576 Copy(c, m, clen, char);
1577 m += clen;
a0d0e21e 1578 }
71be2cbc
PP
1579 i = strend - d;
1580 if (i > 0) {
1581 Move(d, m, i, char);
1582 m += i;
a0d0e21e 1583 }
71be2cbc
PP
1584 *m = '\0';
1585 SvCUR_set(TARG, m - s);
1586 }
1587 /*SUPPRESS 560*/
1588 else if (i = m - s) { /* faster from front */
1589 d -= clen;
1590 m = d;
1591 sv_chop(TARG, d-i);
1592 s += i;
1593 while (i--)
1594 *--d = *--s;
1595 if (clen)
1596 Copy(c, m, clen, char);
1597 }
1598 else if (clen) {
1599 d -= clen;
1600 sv_chop(TARG, d);
1601 Copy(c, d, clen, char);
1602 }
1603 else {
1604 sv_chop(TARG, d);
1605 }
9212bbba 1606 TAINT_IF(rxtainted);
8ec5e241 1607 SPAGAIN;
71be2cbc
PP
1608 PUSHs(&sv_yes);
1609 }
1610 else {
1611 rxtainted = 0;
1612 do {
1613 if (iters++ > maxiters)
1614 DIE("Substitution loop");
c277df42 1615 rxtainted |= RX_MATCH_TAINTED(rx);
71be2cbc
PP
1616 m = rx->startp[0];
1617 /*SUPPRESS 560*/
1618 if (i = m - s) {
1619 if (s != d)
1620 Move(s, d, i, char);
1621 d += i;
a0d0e21e 1622 }
71be2cbc
PP
1623 if (clen) {
1624 Copy(c, d, clen, char);
1625 d += clen;
1626 }
1627 s = rx->endp[0];
c277df42
IZ
1628 } while (regexec_flags(rx, s, strend, orig, s == m,
1629 Nullsv, NULL, 0)); /* don't match same null twice */
71be2cbc
PP
1630 if (s != d) {
1631 i = strend - s;
1632 SvCUR_set(TARG, d - SvPVX(TARG) + i);
1633 Move(s, d, i+1, char); /* include the NUL */
a0d0e21e 1634 }
9212bbba 1635 TAINT_IF(rxtainted);
8ec5e241 1636 SPAGAIN;
71be2cbc 1637 PUSHs(sv_2mortal(newSViv((I32)iters)));
a0d0e21e 1638 }
71be2cbc 1639 (void)SvPOK_only(TARG);
8ec5e241
NIS
1640 if (SvSMAGICAL(TARG)) {
1641 PUTBACK;
1642 mg_set(TARG);
1643 SPAGAIN;
1644 }
9212bbba 1645 SvTAINT(TARG);
71be2cbc
PP
1646 LEAVE_SCOPE(oldsave);
1647 RETURN;
a0d0e21e 1648 }
71be2cbc 1649
c277df42 1650 if (regexec_flags(rx, s, strend, orig, 0, screamer, NULL, safebase)) {
a0d0e21e
LW
1651 if (force_on_match) {
1652 force_on_match = 0;
1653 s = SvPV_force(TARG, len);
1654 goto force_it;
1655 }
c277df42 1656 rxtainted = RX_MATCH_TAINTED(rx);
8ec5e241 1657 dstr = NEWSV(25, len);
a0d0e21e
LW
1658 sv_setpvn(dstr, m, s-m);
1659 curpm = pm;
1660 if (!c) {
c09156bb 1661 register PERL_CONTEXT *cx;
8ec5e241 1662 SPAGAIN;
a0d0e21e
LW
1663 PUSHSUBST(cx);
1664 RETURNOP(cPMOP->op_pmreplroot);
1665 }
1666 do {
1667 if (iters++ > maxiters)
1668 DIE("Substitution loop");
c277df42 1669 rxtainted |= RX_MATCH_TAINTED(rx);
a0d0e21e
LW
1670 if (rx->subbase && rx->subbase != orig) {
1671 m = s;
1672 s = orig;
1673 orig = rx->subbase;
1674 s = orig + (m - s);
1675 strend = s + (strend - m);
1676 }
1677 m = rx->startp[0];
1678 sv_catpvn(dstr, s, m-s);
1679 s = rx->endp[0];
1680 if (clen)
1681 sv_catpvn(dstr, c, clen);
1682 if (once)
1683 break;
c277df42 1684 } while (regexec_flags(rx, s, strend, orig, s == m, Nullsv, NULL, safebase));
a0d0e21e 1685 sv_catpvn(dstr, s, strend - s);
748a9306 1686
9212bbba
CS
1687 TAINT_IF(rxtainted);
1688
4633a7c4 1689 (void)SvOOK_off(TARG);
cb0b1708 1690 Safefree(SvPVX(TARG));
748a9306
LW
1691 SvPVX(TARG) = SvPVX(dstr);
1692 SvCUR_set(TARG, SvCUR(dstr));
1693 SvLEN_set(TARG, SvLEN(dstr));
1694 SvPVX(dstr) = 0;
1695 sv_free(dstr);
1696
a0d0e21e
LW
1697 (void)SvPOK_only(TARG);
1698 SvSETMAGIC(TARG);
9212bbba 1699 SvTAINT(TARG);
8ec5e241 1700 SPAGAIN;
a0d0e21e 1701 PUSHs(sv_2mortal(newSViv((I32)iters)));
4633a7c4 1702 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1703 RETURN;
1704 }
5cd24f17 1705 goto ret_no;
a0d0e21e
LW
1706
1707nope:
c277df42 1708 ++BmUSEFUL(rx->check_substr);
5cd24f17 1709
8ec5e241
NIS
1710ret_no:
1711 SPAGAIN;
a0d0e21e 1712 PUSHs(&sv_no);
4633a7c4 1713 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1714 RETURN;
1715}
1716
1717PP(pp_grepwhile)
1718{
4e35701f 1719 djSP;
a0d0e21e
LW
1720
1721 if (SvTRUEx(POPs))
1722 stack_base[markstack_ptr[-1]++] = stack_base[*markstack_ptr];
1723 ++*markstack_ptr;
1724 LEAVE; /* exit inner scope */
1725
1726 /* All done yet? */
924508f0 1727 if (stack_base + *markstack_ptr > SP) {
a0d0e21e 1728 I32 items;
54310121 1729 I32 gimme = GIMME_V;
a0d0e21e
LW
1730
1731 LEAVE; /* exit outer scope */
1732 (void)POPMARK; /* pop src */
1733 items = --*markstack_ptr - markstack_ptr[-1];
1734 (void)POPMARK; /* pop dst */
1735 SP = stack_base + POPMARK; /* pop original mark */
54310121 1736 if (gimme == G_SCALAR) {
a0d0e21e
LW
1737 dTARGET;
1738 XPUSHi(items);
a0d0e21e 1739 }
54310121
PP
1740 else if (gimme == G_ARRAY)
1741 SP += items;
a0d0e21e
LW
1742 RETURN;
1743 }
1744 else {
1745 SV *src;
1746
1747 ENTER; /* enter inner scope */
1748 SAVESPTR(curpm);
1749
1750 src = stack_base[*markstack_ptr];
1751 SvTEMP_off(src);
54b9620d 1752 DEFSV = src;
a0d0e21e
LW
1753
1754 RETURNOP(cLOGOP->op_other);
1755 }
1756}
1757
1758PP(pp_leavesub)
1759{
4e35701f 1760 djSP;
a0d0e21e
LW
1761 SV **mark;
1762 SV **newsp;
1763 PMOP *newpm;
1764 I32 gimme;
c09156bb 1765 register PERL_CONTEXT *cx;
f86702cc 1766 struct block_sub cxsub;
a0d0e21e
LW
1767
1768 POPBLOCK(cx,newpm);
f86702cc
PP
1769 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1770
a1f49e72 1771 TAINT_NOT;
a0d0e21e
LW
1772 if (gimme == G_SCALAR) {
1773 MARK = newsp + 1;
1774 if (MARK <= SP)
f86702cc 1775 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
a0d0e21e 1776 else {
f86702cc 1777 MEXTEND(MARK, 0);
a0d0e21e
LW
1778 *MARK = &sv_undef;
1779 }
1780 SP = MARK;
1781 }
54310121 1782 else if (gimme == G_ARRAY) {
f86702cc 1783 for (MARK = newsp + 1; MARK <= SP; MARK++) {
a1f49e72 1784 if (!SvTEMP(*MARK)) {
f86702cc 1785 *MARK = sv_mortalcopy(*MARK);
a1f49e72
CS
1786 TAINT_NOT; /* Each item is independent */
1787 }
f86702cc 1788 }
a0d0e21e 1789 }
f86702cc
PP
1790 PUTBACK;
1791
1792 POPSUB2(); /* Stack values are safe: release CV and @_ ... */
1793 curpm = newpm; /* ... and pop $1 et al */
a0d0e21e
LW
1794
1795 LEAVE;
a0d0e21e
LW
1796 return pop_return();
1797}
1798
3de9ffa1 1799static CV *
8ac85365 1800get_db_sub(SV **svp, CV *cv)
3de9ffa1
MB
1801{
1802 dTHR;
491527d0
GS
1803 SV *dbsv = GvSV(DBsub);
1804
1805 if (!PERLDB_SUB_NN) {
1806 GV *gv = CvGV(cv);
1807
1808 save_item(dbsv);
1809 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
1810 || strEQ(GvNAME(gv), "END")
1811 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
1812 !( (SvTYPE(*svp) == SVt_PVGV) && (GvCV((GV*)*svp) == cv)
1813 && (gv = (GV*)*svp) ))) {
1814 /* Use GV from the stack as a fallback. */
1815 /* GV is potentially non-unique, or contain different CV. */
1816 sv_setsv(dbsv, newRV((SV*)cv));
1817 }
1818 else {
1819 gv_efullname3(dbsv, gv, Nullch);
1820 }
3de9ffa1
MB
1821 }
1822 else {
491527d0
GS
1823 SvUPGRADE(dbsv, SVt_PVIV);
1824 SvIOK_on(dbsv);
1825 SAVEIV(SvIVX(dbsv));
1826 SvIVX(dbsv) = (IV)cv; /* Do it the quickest way */
3de9ffa1 1827 }
491527d0 1828
3de9ffa1
MB
1829 if (CvXSUB(cv))
1830 curcopdb = curcop;
491527d0 1831 cv = GvCV(DBsub);
3de9ffa1
MB
1832 return cv;
1833}
1834
a0d0e21e
LW
1835PP(pp_entersub)
1836{
4e35701f 1837 djSP; dPOPss;
a0d0e21e
LW
1838 GV *gv;
1839 HV *stash;
1840 register CV *cv;
c09156bb 1841 register PERL_CONTEXT *cx;
5d94fbed 1842 I32 gimme;
07055b4c 1843 bool hasargs = (op->op_flags & OPf_STACKED) != 0;
a0d0e21e
LW
1844
1845 if (!sv)
1846 DIE("Not a CODE reference");
1847 switch (SvTYPE(sv)) {
1848 default:
1849 if (!SvROK(sv)) {
748a9306
LW
1850 char *sym;
1851
fb73857a
PP
1852 if (sv == &sv_yes) { /* unfound import, ignore */
1853 if (hasargs)
1854 SP = stack_base + POPMARK;
a0d0e21e 1855 RETURN;
fb73857a 1856 }
15ff848f
CS
1857 if (SvGMAGICAL(sv)) {
1858 mg_get(sv);
1859 sym = SvPOKp(sv) ? SvPVX(sv) : Nullch;
1860 }
1861 else
1862 sym = SvPV(sv, na);
1863 if (!sym)
a0d0e21e
LW
1864 DIE(no_usym, "a subroutine");
1865 if (op->op_private & HINT_STRICT_REFS)
748a9306
LW
1866 DIE(no_symref, sym, "a subroutine");
1867 cv = perl_get_cv(sym, TRUE);
a0d0e21e
LW
1868 break;
1869 }
1870 cv = (CV*)SvRV(sv);
1871 if (SvTYPE(cv) == SVt_PVCV)
1872 break;
1873 /* FALL THROUGH */
1874 case SVt_PVHV:
1875 case SVt_PVAV:
1876 DIE("Not a CODE reference");
1877 case SVt_PVCV:
1878 cv = (CV*)sv;
1879 break;
1880 case SVt_PVGV:
8ebc5c01 1881 if (!(cv = GvCVu((GV*)sv)))
a0d0e21e
LW
1882 cv = sv_2cv(sv, &stash, &gv, TRUE);
1883 break;
1884 }
1885
1886 ENTER;
1887 SAVETMPS;
1888
1889 retry:
1890 if (!cv)
1891 DIE("Not a CODE reference");
1892
1893 if (!CvROOT(cv) && !CvXSUB(cv)) {
44a8e56a 1894 GV* autogv;
22239a37 1895 SV* sub_name;
44a8e56a
PP
1896
1897 /* anonymous or undef'd function leaves us no recourse */
1898 if (CvANON(cv) || !(gv = CvGV(cv)))
1899 DIE("Undefined subroutine called");
1900 /* autoloaded stub? */
1901 if (cv != GvCV(gv)) {
1902 cv = GvCV(gv);
1903 goto retry;
a0d0e21e 1904 }
44a8e56a 1905 /* should call AUTOLOAD now? */
54310121
PP
1906 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
1907 FALSE)))
1908 {
44a8e56a
PP
1909 cv = GvCV(autogv);
1910 goto retry;
a0d0e21e 1911 }
44a8e56a 1912 /* sorry */
22239a37
NIS
1913 sub_name = sv_newmortal();
1914 gv_efullname3(sub_name, gv, Nullch);
1915 DIE("Undefined subroutine &%s called", SvPVX(sub_name));
a0d0e21e
LW
1916 }
1917
54310121 1918 gimme = GIMME_V;
3de9ffa1 1919 if ((op->op_private & OPpENTERSUB_DB) && GvCV(DBsub) && !CvNODEBUG(cv))
4f01c5a5 1920 cv = get_db_sub(&sv, cv);
3de9ffa1
MB
1921 if (!cv)
1922 DIE("No DBsub routine");
a0d0e21e 1923
11343788 1924#ifdef USE_THREADS
3de9ffa1
MB
1925 /*
1926 * First we need to check if the sub or method requires locking.
458fb581
MB
1927 * If so, we gain a lock on the CV, the first argument or the
1928 * stash (for static methods), as appropriate. This has to be
1929 * inline because for FAKE_THREADS, COND_WAIT inlines code to
1930 * reschedule by returning a new op.
3de9ffa1 1931 */
11343788 1932 MUTEX_LOCK(CvMUTEXP(cv));
77a005ab
MB
1933 if (CvFLAGS(cv) & CVf_LOCKED) {
1934 MAGIC *mg;
1935 if (CvFLAGS(cv) & CVf_METHOD) {
1936 if (SP > stack_base + TOPMARK)
1937 sv = *(stack_base + TOPMARK + 1);
1938 else {
1939 MUTEX_UNLOCK(CvMUTEXP(cv));
1940 croak("no argument for locked method call");
1941 }
1942 if (SvROK(sv))
1943 sv = SvRV(sv);
458fb581
MB
1944 else {
1945 STRLEN len;
1946 char *stashname = SvPV(sv, len);
1947 sv = (SV*)gv_stashpvn(stashname, len, TRUE);
1948 }
77a005ab
MB
1949 }
1950 else {
1951 sv = (SV*)cv;
1952 }
1953 MUTEX_UNLOCK(CvMUTEXP(cv));
1954 mg = condpair_magic(sv);
1955 MUTEX_LOCK(MgMUTEXP(mg));
1956 if (MgOWNER(mg) == thr)
1957 MUTEX_UNLOCK(MgMUTEXP(mg));
1958 else {
1959 while (MgOWNER(mg))
1960 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
1961 MgOWNER(mg) = thr;
1fd28e87
MB
1962 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "%p: pp_entersub lock %p\n",
1963 thr, sv);)
77a005ab 1964 MUTEX_UNLOCK(MgMUTEXP(mg));
e55aaa0e 1965 SvREFCNT_inc(sv); /* Keep alive until magic_mutexfree */
77a005ab 1966 save_destructor(unlock_condpair, sv);
11343788 1967 }
77a005ab 1968 MUTEX_LOCK(CvMUTEXP(cv));
11343788 1969 }
3de9ffa1
MB
1970 /*
1971 * Now we have permission to enter the sub, we must distinguish
1972 * four cases. (0) It's an XSUB (in which case we don't care
1973 * about ownership); (1) it's ours already (and we're recursing);
1974 * (2) it's free (but we may already be using a cached clone);
1975 * (3) another thread owns it. Case (1) is easy: we just use it.
1976 * Case (2) means we look for a clone--if we have one, use it
1977 * otherwise grab ownership of cv. Case (3) means we look for a
1978 * clone (for non-XSUBs) and have to create one if we don't
1979 * already have one.
1980 * Why look for a clone in case (2) when we could just grab
1981 * ownership of cv straight away? Well, we could be recursing,
1982 * i.e. we originally tried to enter cv while another thread
1983 * owned it (hence we used a clone) but it has been freed up
1984 * and we're now recursing into it. It may or may not be "better"
1985 * to use the clone but at least CvDEPTH can be trusted.
1986 */
1987 if (CvOWNER(cv) == thr || CvXSUB(cv))
1988 MUTEX_UNLOCK(CvMUTEXP(cv));
11343788 1989 else {
3de9ffa1
MB
1990 /* Case (2) or (3) */
1991 SV **svp;
1992
11343788 1993 /*
3de9ffa1
MB
1994 * XXX Might it be better to release CvMUTEXP(cv) while we
1995 * do the hv_fetch? We might find someone has pinched it
1996 * when we look again, in which case we would be in case
1997 * (3) instead of (2) so we'd have to clone. Would the fact
1998 * that we released the mutex more quickly make up for this?
1999 */
6ee623d5
GS
2000 if (threadnum &&
2001 (svp = hv_fetch(thr->cvcache, (char *)cv, sizeof(cv), FALSE)))
2002 {
3de9ffa1 2003 /* We already have a clone to use */
11343788 2004 MUTEX_UNLOCK(CvMUTEXP(cv));
3de9ffa1
MB
2005 cv = *(CV**)svp;
2006 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
2007 "entersub: %p already has clone %p:%s\n",
2008 thr, cv, SvPEEK((SV*)cv)));
3de9ffa1
MB
2009 CvOWNER(cv) = thr;
2010 SvREFCNT_inc(cv);
2011 if (CvDEPTH(cv) == 0)
2012 SAVEDESTRUCTOR(unset_cvowner, (void*) cv);
2013 }
11343788 2014 else {
3de9ffa1
MB
2015 /* (2) => grab ownership of cv. (3) => make clone */
2016 if (!CvOWNER(cv)) {
2017 CvOWNER(cv) = thr;
2018 SvREFCNT_inc(cv);
11343788 2019 MUTEX_UNLOCK(CvMUTEXP(cv));
d9f997d7 2020 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
2021 "entersub: %p grabbing %p:%s in stash %s\n",
2022 thr, cv, SvPEEK((SV*)cv), CvSTASH(cv) ?
3de9ffa1
MB
2023 HvNAME(CvSTASH(cv)) : "(none)"));
2024 } else {
2025 /* Make a new clone. */
2026 CV *clonecv;
2027 SvREFCNT_inc(cv); /* don't let it vanish from under us */
2028 MUTEX_UNLOCK(CvMUTEXP(cv));
2029 DEBUG_L((PerlIO_printf(PerlIO_stderr(),
1fd28e87
MB
2030 "entersub: %p cloning %p:%s\n",
2031 thr, cv, SvPEEK((SV*)cv))));
3de9ffa1
MB
2032 /*
2033 * We're creating a new clone so there's no race
2034 * between the original MUTEX_UNLOCK and the
2035 * SvREFCNT_inc since no one will be trying to undef
2036 * it out from underneath us. At least, I don't think
2037 * there's a race...
2038 */
2039 clonecv = cv_clone(cv);
2040 SvREFCNT_dec(cv); /* finished with this */
199100c8 2041 hv_store(thr->cvcache, (char*)cv, sizeof(cv), (SV*)clonecv,0);
3de9ffa1
MB
2042 CvOWNER(clonecv) = thr;
2043 cv = clonecv;
11343788 2044 SvREFCNT_inc(cv);
11343788 2045 }
3de9ffa1
MB
2046 DEBUG_L(if (CvDEPTH(cv) != 0)
2047 PerlIO_printf(PerlIO_stderr(), "depth %ld != 0\n",
2048 CvDEPTH(cv)););
2049 SAVEDESTRUCTOR(unset_cvowner, (void*) cv);
11343788 2050 }
3de9ffa1 2051 }
11343788
MB
2052#endif /* USE_THREADS */
2053
a0d0e21e
LW
2054 if (CvXSUB(cv)) {
2055 if (CvOLDSTYLE(cv)) {
ecfc5424 2056 I32 (*fp3)_((int,int,int));
a0d0e21e
LW
2057 dMARK;
2058 register I32 items = SP - MARK;
67955e0c 2059 /* We dont worry to copy from @_. */
924508f0
GS
2060 while (SP > mark) {
2061 SP[1] = SP[0];
2062 SP--;
a0d0e21e
LW
2063 }
2064 stack_sp = mark + 1;
ecfc5424
AD
2065 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
2066 items = (*fp3)(CvXSUBANY(cv).any_i32,
2067 MARK - stack_base + 1,
2068 items);
a0d0e21e
LW
2069 stack_sp = stack_base + items;
2070 }
2071 else {
748a9306
LW
2072 I32 markix = TOPMARK;
2073
a0d0e21e 2074 PUTBACK;
67955e0c
PP
2075
2076 if (!hasargs) {
2077 /* Need to copy @_ to stack. Alternative may be to
2078 * switch stack to @_, and copy return values
2079 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
6d4ff0d2
MB
2080 AV* av;
2081 I32 items;
2082#ifdef USE_THREADS
2083 av = (AV*)curpad[0];
2084#else
2085 av = GvAV(defgv);
2086#endif /* USE_THREADS */
93965878 2087 items = AvFILLp(av) + 1; /* @_ is not tieable */
67955e0c
PP
2088
2089 if (items) {
2090 /* Mark is at the end of the stack. */
924508f0
GS
2091 EXTEND(SP, items);
2092 Copy(AvARRAY(av), SP + 1, items, SV*);
2093 SP += items;
67955e0c
PP
2094 PUTBACK ;
2095 }
2096 }
2097 if (curcopdb) { /* We assume that the first
2098 XSUB in &DB::sub is the
2099 called one. */
2100 SAVESPTR(curcop);
2101 curcop = curcopdb;
2102 curcopdb = NULL;
2103 }
2104 /* Do we need to open block here? XXXX */
a0d0e21e 2105 (void)(*CvXSUB(cv))(cv);
748a9306
LW
2106
2107 /* Enforce some sanity in scalar context. */
5d94fbed 2108 if (gimme == G_SCALAR && ++markix != stack_sp - stack_base ) {
748a9306
LW
2109 if (markix > stack_sp - stack_base)
2110 *(stack_base + markix) = &sv_undef;
2111 else
2112 *(stack_base + markix) = *stack_sp;
2113 stack_sp = stack_base + markix;
2114 }
a0d0e21e
LW
2115 }
2116 LEAVE;
2117 return NORMAL;
2118 }
2119 else {
2120 dMARK;
2121 register I32 items = SP - MARK;
a0d0e21e
LW
2122 AV* padlist = CvPADLIST(cv);
2123 SV** svp = AvARRAY(padlist);
2124 push_return(op->op_next);
2125 PUSHBLOCK(cx, CXt_SUB, MARK);
2126 PUSHSUB(cx);
2127 CvDEPTH(cv)++;
2128 if (CvDEPTH(cv) < 2)
2129 (void)SvREFCNT_inc(cv);
2130 else { /* save temporaries on recursion? */
5f05dabc 2131 if (CvDEPTH(cv) == 100 && dowarn
84902520 2132 && !(PERLDB_SUB && cv == GvCV(DBsub)))
44a8e56a 2133 sub_crush_depth(cv);
93965878 2134 if (CvDEPTH(cv) > AvFILLp(padlist)) {
a0d0e21e
LW
2135 AV *av;
2136 AV *newpad = newAV();
4aa0a1f7 2137 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
93965878 2138 I32 ix = AvFILLp((AV*)svp[1]);
a0d0e21e 2139 svp = AvARRAY(svp[0]);
748a9306 2140 for ( ;ix > 0; ix--) {
a0d0e21e 2141 if (svp[ix] != &sv_undef) {
748a9306 2142 char *name = SvPVX(svp[ix]);
5f05dabc
PP
2143 if ((SvFLAGS(svp[ix]) & SVf_FAKE) /* outer lexical? */
2144 || *name == '&') /* anonymous code? */
2145 {
2146 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix]));
748a9306
LW
2147 }
2148 else { /* our own lexical */
2149 if (*name == '@')
2150 av_store(newpad, ix, sv = (SV*)newAV());
2151 else if (*name == '%')
2152 av_store(newpad, ix, sv = (SV*)newHV());
2153 else
2154 av_store(newpad, ix, sv = NEWSV(0,0));
2155 SvPADMY_on(sv);
2156 }
a0d0e21e
LW
2157 }
2158 else {
748a9306 2159 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
2160 SvPADTMP_on(sv);
2161 }
2162 }
2163 av = newAV(); /* will be @_ */
2164 av_extend(av, 0);
2165 av_store(newpad, 0, (SV*)av);
2166 AvFLAGS(av) = AVf_REIFY;
2167 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
93965878 2168 AvFILLp(padlist) = CvDEPTH(cv);
a0d0e21e
LW
2169 svp = AvARRAY(padlist);
2170 }
2171 }
6d4ff0d2
MB
2172#ifdef USE_THREADS
2173 if (!hasargs) {
a0d0e21e 2174 AV* av = (AV*)curpad[0];
6d4ff0d2 2175
93965878 2176 items = AvFILLp(av) + 1;
6d4ff0d2
MB
2177 if (items) {
2178 /* Mark is at the end of the stack. */
924508f0
GS
2179 EXTEND(SP, items);
2180 Copy(AvARRAY(av), SP + 1, items, SV*);
2181 SP += items;
6d4ff0d2
MB
2182 PUTBACK ;
2183 }
2184 }
2185#endif /* USE_THREADS */
2186 SAVESPTR(curpad);
2187 curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
2188#ifndef USE_THREADS
2189 if (hasargs)
2190#endif /* USE_THREADS */
2191 {
2192 AV* av;
a0d0e21e
LW
2193 SV** ary;
2194
77a005ab 2195#if 0
0f15f207
MB
2196 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
2197 "%p entersub preparing @_\n", thr));
77a005ab 2198#endif
6d4ff0d2 2199 av = (AV*)curpad[0];
a0d0e21e
LW
2200 if (AvREAL(av)) {
2201 av_clear(av);
2202 AvREAL_off(av);
2203 }
6d4ff0d2 2204#ifndef USE_THREADS
a0d0e21e 2205 cx->blk_sub.savearray = GvAV(defgv);
0c8c7a05 2206 GvAV(defgv) = (AV*)SvREFCNT_inc(av);
6d4ff0d2
MB
2207#endif /* USE_THREADS */
2208 cx->blk_sub.argarray = av;
a0d0e21e
LW
2209 ++MARK;
2210
2211 if (items > AvMAX(av) + 1) {
2212 ary = AvALLOC(av);
2213 if (AvARRAY(av) != ary) {
2214 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2215 SvPVX(av) = (char*)ary;
2216 }
2217 if (items > AvMAX(av) + 1) {
2218 AvMAX(av) = items - 1;
2219 Renew(ary,items,SV*);
2220 AvALLOC(av) = ary;
2221 SvPVX(av) = (char*)ary;
2222 }
2223 }
2224 Copy(MARK,AvARRAY(av),items,SV*);
93965878 2225 AvFILLp(av) = items - 1;
a0d0e21e
LW
2226
2227 while (items--) {
2228 if (*MARK)
2229 SvTEMP_off(*MARK);
2230 MARK++;
2231 }
2232 }
77a005ab 2233#if 0
0f15f207
MB
2234 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
2235 "%p entersub returning %p\n", thr, CvSTART(cv)));
77a005ab 2236#endif
a0d0e21e
LW
2237 RETURNOP(CvSTART(cv));
2238 }
2239}
2240
44a8e56a 2241void
8ac85365 2242sub_crush_depth(CV *cv)
44a8e56a
PP
2243{
2244 if (CvANON(cv))
2245 warn("Deep recursion on anonymous subroutine");
2246 else {
2247 SV* tmpstr = sv_newmortal();
2248 gv_efullname3(tmpstr, CvGV(cv), Nullch);
2249 warn("Deep recursion on subroutine \"%s\"", SvPVX(tmpstr));
2250 }
2251}
2252
a0d0e21e
LW
2253PP(pp_aelem)
2254{
4e35701f 2255 djSP;
a0d0e21e 2256 SV** svp;
748a9306 2257 I32 elem = POPi;
68dc0745
PP
2258 AV* av = (AV*)POPs;
2259 U32 lval = op->op_flags & OPf_MOD;
2260 U32 defer = (op->op_private & OPpLVAL_DEFER) && (elem > AvFILL(av));
a0d0e21e 2261
748a9306
LW
2262 if (elem > 0)
2263 elem -= curcop->cop_arybase;
a0d0e21e
LW
2264 if (SvTYPE(av) != SVt_PVAV)
2265 RETPUSHUNDEF;
68dc0745 2266 svp = av_fetch(av, elem, lval && !defer);
a0d0e21e 2267 if (lval) {
68dc0745
PP
2268 if (!svp || *svp == &sv_undef) {
2269 SV* lv;
2270 if (!defer)
2271 DIE(no_aelem, elem);
2272 lv = sv_newmortal();
2273 sv_upgrade(lv, SVt_PVLV);
2274 LvTYPE(lv) = 'y';
2275 sv_magic(lv, Nullsv, 'y', Nullch, 0);
2276 LvTARG(lv) = SvREFCNT_inc(av);
2277 LvTARGOFF(lv) = elem;
2278 LvTARGLEN(lv) = 1;
2279 PUSHs(lv);
2280 RETURN;
2281 }
a0d0e21e 2282 if (op->op_private & OPpLVAL_INTRO)
161b7d16 2283 save_aelem(av, elem, svp);
5f05dabc 2284 else if (op->op_private & OPpDEREF)
68dc0745 2285 vivify_ref(*svp, op->op_private & OPpDEREF);
a0d0e21e
LW
2286 }
2287 PUSHs(svp ? *svp : &sv_undef);
2288 RETURN;
2289}
2290
02a9e968 2291void
8ac85365 2292vivify_ref(SV *sv, U32 to_what)
02a9e968
CS
2293{
2294 if (SvGMAGICAL(sv))
2295 mg_get(sv);
2296 if (!SvOK(sv)) {
2297 if (SvREADONLY(sv))
2298 croak(no_modify);
5f05dabc
PP
2299 if (SvTYPE(sv) < SVt_RV)
2300 sv_upgrade(sv, SVt_RV);
2301 else if (SvTYPE(sv) >= SVt_PV) {
2302 (void)SvOOK_off(sv);
2303 Safefree(SvPVX(sv));
2304 SvLEN(sv) = SvCUR(sv) = 0;
2305 }
68dc0745 2306 switch (to_what) {
5f05dabc 2307 case OPpDEREF_SV:
8c52afec 2308 SvRV(sv) = NEWSV(355,0);
5f05dabc
PP
2309 break;
2310 case OPpDEREF_AV:
2311 SvRV(sv) = (SV*)newAV();
2312 break;
2313 case OPpDEREF_HV:
2314 SvRV(sv) = (SV*)newHV();
2315 break;
2316 }
02a9e968
CS
2317 SvROK_on(sv);
2318 SvSETMAGIC(sv);
2319 }
2320}
2321
a0d0e21e
LW
2322PP(pp_method)
2323{
4e35701f 2324 djSP;
a0d0e21e
LW
2325 SV* sv;
2326 SV* ob;
2327 GV* gv;
56304f61
CS
2328 HV* stash;
2329 char* name;
ac91690f
CS
2330 char* packname;
2331 STRLEN packlen;
a0d0e21e 2332
fb73857a
PP
2333 if (SvROK(TOPs)) {
2334 sv = SvRV(TOPs);
2335 if (SvTYPE(sv) == SVt_PVCV) {
2336 SETs(sv);
2337 RETURN;
2338 }
2339 }
2340
56304f61 2341 name = SvPV(TOPs, na);
a0d0e21e
LW
2342 sv = *(stack_base + TOPMARK + 1);
2343
16d20bd9
AD
2344 if (SvGMAGICAL(sv))
2345 mg_get(sv);
a0d0e21e 2346 if (SvROK(sv))
16d20bd9 2347 ob = (SV*)SvRV(sv);
a0d0e21e
LW
2348 else {
2349 GV* iogv;
a0d0e21e 2350
56304f61 2351 packname = Nullch;
a0d0e21e 2352 if (!SvOK(sv) ||
56304f61 2353 !(packname = SvPV(sv, packlen)) ||
a0d0e21e
LW
2354 !(iogv = gv_fetchpv(packname, FALSE, SVt_PVIO)) ||
2355 !(ob=(SV*)GvIO(iogv)))
2356 {
56304f61 2357 if (!packname || !isIDFIRST(*packname))
ac91690f 2358 DIE("Can't call method \"%s\" without a package or object reference", name);
56304f61 2359 stash = gv_stashpvn(packname, packlen, TRUE);
ac91690f 2360 goto fetch;
a0d0e21e 2361 }
ac91690f 2362 *(stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
a0d0e21e
LW
2363 }
2364
ac91690f 2365 if (!ob || !SvOBJECT(ob))
a0d0e21e 2366 DIE("Can't call method \"%s\" on unblessed reference", name);
a0d0e21e 2367
56304f61 2368 stash = SvSTASH(ob);
a0d0e21e 2369
ac91690f 2370 fetch:
ac91690f 2371 gv = gv_fetchmethod(stash, name);
56304f61
CS
2372 if (!gv) {
2373 char* leaf = name;
2374 char* sep = Nullch;
2375 char* p;
2376
2377 for (p = name; *p; p++) {
2378 if (*p == '\'')
2379 sep = p, leaf = p + 1;
2380 else if (*p == ':' && *(p + 1) == ':')
2381 sep = p, leaf = p + 2;
2382 }
2383 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
2384 packname = HvNAME(sep ? curcop->cop_stash : stash);
2385 packlen = strlen(packname);
2386 }
2387 else {
2388 packname = name;
2389 packlen = sep - name;
2390 }
2391 DIE("Can't locate object method \"%s\" via package \"%.*s\"",
2392 leaf, (int)packlen, packname);
2393 }
8ebc5c01 2394 SETs(isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv);
a0d0e21e
LW
2395 RETURN;
2396}
22239a37 2397