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