3 * Copyright (c) 1991-1997, Larry Wall
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.
11 * Then he heard Merry change the note, and up went the Horn-cry of Buckland,
14 * Awake! Awake! Fear, Fire, Foes! Awake!
31 #define HOP(pos,off) (IN_UTF8 ? utf8_hop(pos, off) : (pos + off))
37 unset_cvowner(void *cvarg)
39 register CV* cv = (CV *) cvarg;
42 #endif /* DEBUGGING */
44 DEBUG_S((PerlIO_printf(PerlIO_stderr(), "%p unsetting CvOWNER of %p:%s\n",
45 thr, cv, SvPEEK((SV*)cv))));
46 MUTEX_LOCK(CvMUTEXP(cv));
47 DEBUG_S(if (CvDEPTH(cv) != 0)
48 PerlIO_printf(PerlIO_stderr(), "depth %ld != 0\n",
50 assert(thr == CvOWNER(cv));
52 MUTEX_UNLOCK(CvMUTEXP(cv));
55 #endif /* USE_THREADS */
66 PL_curcop = (COP*)PL_op;
67 TAINT_NOT; /* Each statement is presumed innocent */
68 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
77 if (PL_op->op_private & OPpLVAL_INTRO)
78 PUSHs(save_scalar(cGVOP->op_gv));
80 PUSHs(GvSV(cGVOP->op_gv));
91 PUSHMARK(PL_stack_sp);
101 sv_setpvn(TARG,s,len);
109 XPUSHs((SV*)cGVOP->op_gv);
120 RETURNOP(cLOGOP->op_other);
129 if (PL_op->op_private & OPpASSIGN_BACKWARDS) {
131 temp = left; left = right; right = temp;
133 if (PL_tainting && PL_tainted && !SvTAINTED(left))
135 SvSetMagicSV(right, left);
144 RETURNOP(cCONDOP->op_true);
146 RETURNOP(cCONDOP->op_false);
152 TAINT_NOT; /* Each statement is presumed innocent */
153 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
155 oldsave = PL_scopestack[PL_scopestack_ix - 1];
156 LEAVE_SCOPE(oldsave);
162 djSP; dATARGET; tryAMAGICbin(concat,opASSIGN);
169 sv_setpvn(TARG,s,len);
171 else if (SvGMAGICAL(TARG))
173 else if (!SvOK(TARG) && SvTYPE(TARG) <= SVt_PVMG) {
174 sv_setpv(TARG, ""); /* Suppress warning. */
175 s = SvPV_force(TARG, len);
179 sv_catpvn(TARG,s,len);
181 sv_setpvn(TARG,s,len); /* suppress warning */
191 if (PL_op->op_flags & OPf_MOD) {
192 if (PL_op->op_private & OPpLVAL_INTRO)
193 SAVECLEARSV(PL_curpad[PL_op->op_targ]);
194 else if (PL_op->op_private & OPpDEREF) {
196 vivify_ref(PL_curpad[PL_op->op_targ], PL_op->op_private & OPpDEREF);
205 tryAMAGICunTARGET(iter, 0);
206 PL_last_in_gv = (GV*)(*PL_stack_sp--);
207 if (PL_op->op_flags & OPf_SPECIAL) { /* Are called as <$var> */
208 if (SvROK(PL_last_in_gv)) {
209 if (SvTYPE(SvRV(PL_last_in_gv)) != SVt_PVGV)
211 PL_last_in_gv = (GV*)SvRV(PL_last_in_gv);
212 } else if (SvTYPE(PL_last_in_gv) != SVt_PVGV) {
215 XPUSHs((SV*)PL_last_in_gv);
218 PL_last_in_gv = (GV*)(*PL_stack_sp--);
222 return do_readline();
227 djSP; tryAMAGICbinSET(eq,0);
230 SETs(boolSV(TOPn == value));
238 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
240 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
241 SvIVX(TOPs) != IV_MAX)
244 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
259 RETURNOP(cLOGOP->op_other);
265 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
268 SETn( left + right );
276 AV *av = GvAV((GV*)cSVOP->op_sv);
277 U32 lval = PL_op->op_flags & OPf_MOD;
278 SV** svp = av_fetch(av, PL_op->op_private, lval);
279 SV *sv = (svp ? *svp : &PL_sv_undef);
281 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
282 sv = sv_mortalcopy(sv);
289 djSP; dMARK; dTARGET;
291 do_join(TARG, *MARK, MARK, SP);
302 * We ass_u_me that LvTARGOFF() comes first, and that two STRLENs
303 * will be enough to hold an OP*.
305 SV* sv = sv_newmortal();
306 sv_upgrade(sv, SVt_PVLV);
308 Copy(&PL_op, &LvTARGOFF(sv), 1, OP*);
316 /* Oversized hot code. */
320 djSP; dMARK; dORIGMARK;
326 if (PL_op->op_flags & OPf_STACKED)
330 if (mg = SvTIED_mg((SV*)gv, 'q')) {
331 if (MARK == ORIGMARK) {
332 /* If using default handle then we need to make space to
333 * pass object as 1st arg, so move other args up ...
337 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
341 *MARK = SvTIED_obj((SV*)gv, mg);
344 perl_call_method("PRINT", G_SCALAR);
352 if (!(io = GvIO(gv))) {
353 if (ckWARN(WARN_UNOPENED)) {
354 SV* sv = sv_newmortal();
355 gv_fullname3(sv, gv, Nullch);
356 warner(WARN_UNOPENED, "Filehandle %s never opened", SvPV(sv,PL_na));
359 SETERRNO(EBADF,RMS$_IFI);
362 else if (!(fp = IoOFP(io))) {
363 if (ckWARN2(WARN_CLOSED, WARN_IO)) {
364 SV* sv = sv_newmortal();
365 gv_fullname3(sv, gv, Nullch);
367 warner(WARN_IO, "Filehandle %s opened only for input",
369 else if (ckWARN(WARN_CLOSED))
370 warner(WARN_CLOSED, "print on closed filehandle %s",
373 SETERRNO(EBADF,IoIFP(io)?RMS$_FAC:RMS$_IFI);
380 if (!do_print(*MARK, fp))
384 if (PerlIO_write(fp, PL_ofs, PL_ofslen) == 0 || PerlIO_error(fp)) {
393 if (!do_print(*MARK, fp))
402 if (PerlIO_write(fp, PL_ors, PL_orslen) == 0 || PerlIO_error(fp))
405 if (IoFLAGS(io) & IOf_FLUSH)
406 if (PerlIO_flush(fp) == EOF)
427 tryAMAGICunDEREF(to_av);
430 if (SvTYPE(av) != SVt_PVAV)
431 DIE("Not an ARRAY reference");
432 if (PL_op->op_flags & OPf_REF) {
438 if (SvTYPE(sv) == SVt_PVAV) {
440 if (PL_op->op_flags & OPf_REF) {
448 if (SvTYPE(sv) != SVt_PVGV) {
451 if (SvGMAGICAL(sv)) {
457 if (PL_op->op_flags & OPf_REF ||
458 PL_op->op_private & HINT_STRICT_REFS)
459 DIE(PL_no_usym, "an ARRAY");
460 if (ckWARN(WARN_UNINITIALIZED))
461 warner(WARN_UNINITIALIZED, PL_warn_uninit);
462 if (GIMME == G_ARRAY) {
468 sym = SvPV(sv,PL_na);
469 if (PL_op->op_private & HINT_STRICT_REFS)
470 DIE(PL_no_symref, sym, "an ARRAY");
471 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVAV);
476 if (PL_op->op_private & OPpLVAL_INTRO)
478 if (PL_op->op_flags & OPf_REF) {
485 if (GIMME == G_ARRAY) {
486 I32 maxarg = AvFILL(av) + 1;
487 POPs; /* XXXX May be optimized away? */
489 if (SvRMAGICAL(av)) {
491 for (i=0; i < maxarg; i++) {
492 SV **svp = av_fetch(av, i, FALSE);
493 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
497 Copy(AvARRAY(av), SP+1, maxarg, SV*);
503 I32 maxarg = AvFILL(av) + 1;
516 tryAMAGICunDEREF(to_hv);
519 if (SvTYPE(hv) != SVt_PVHV && SvTYPE(hv) != SVt_PVAV)
520 DIE("Not a HASH reference");
521 if (PL_op->op_flags & OPf_REF) {
527 if (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV) {
529 if (PL_op->op_flags & OPf_REF) {
537 if (SvTYPE(sv) != SVt_PVGV) {
540 if (SvGMAGICAL(sv)) {
546 if (PL_op->op_flags & OPf_REF ||
547 PL_op->op_private & HINT_STRICT_REFS)
548 DIE(PL_no_usym, "a HASH");
549 if (ckWARN(WARN_UNINITIALIZED))
550 warner(WARN_UNINITIALIZED, PL_warn_uninit);
551 if (GIMME == G_ARRAY) {
557 sym = SvPV(sv,PL_na);
558 if (PL_op->op_private & HINT_STRICT_REFS)
559 DIE(PL_no_symref, sym, "a HASH");
560 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVHV);
565 if (PL_op->op_private & OPpLVAL_INTRO)
567 if (PL_op->op_flags & OPf_REF) {
574 if (GIMME == G_ARRAY) { /* array wanted */
575 *PL_stack_sp = (SV*)hv;
580 if (SvTYPE(hv) == SVt_PVAV)
581 hv = avhv_keys((AV*)hv);
583 sv_setpvf(TARG, "%ld/%ld",
584 (long)HvFILL(hv), (long)HvMAX(hv) + 1);
596 SV **lastlelem = PL_stack_sp;
597 SV **lastrelem = PL_stack_base + POPMARK;
598 SV **firstrelem = PL_stack_base + POPMARK + 1;
599 SV **firstlelem = lastrelem + 1;
612 PL_delaymagic = DM_DELAY; /* catch simultaneous items */
614 /* If there's a common identifier on both sides we have to take
615 * special care that assigning the identifier on the left doesn't
616 * clobber a value on the right that's used later in the list.
618 if (PL_op->op_private & OPpASSIGN_COMMON) {
619 EXTEND_MORTAL(lastrelem - firstrelem + 1);
620 for (relem = firstrelem; relem <= lastrelem; relem++) {
623 TAINT_NOT; /* Each item is independent */
624 *relem = sv_mortalcopy(sv);
633 while (lelem <= lastlelem) {
634 TAINT_NOT; /* Each item stands on its own, taintwise. */
636 switch (SvTYPE(sv)) {
639 magic = SvMAGICAL(ary) != 0;
642 av_extend(ary, lastrelem - relem);
644 while (relem <= lastrelem) { /* gobble up all the rest */
650 didstore = av_store(ary,i++,sv);
664 magic = SvMAGICAL(hash) != 0;
667 while (relem < lastrelem) { /* gobble up all the rest */
672 sv = &PL_sv_no, relem++;
673 tmpstr = NEWSV(29,0);
675 sv_setsv(tmpstr,*relem); /* value */
677 didstore = hv_store_ent(hash,sv,tmpstr,0);
679 if (SvSMAGICAL(tmpstr))
682 SvREFCNT_dec(tmpstr);
686 if (relem == lastrelem) {
689 if (ckWARN(WARN_UNSAFE)) {
690 if (relem == firstrelem &&
692 ( SvTYPE(SvRV(*relem)) == SVt_PVAV ||
693 SvTYPE(SvRV(*relem)) == SVt_PVHV ) )
694 warner(WARN_UNSAFE, "Reference found where even-sized list expected");
696 warner(WARN_UNSAFE, "Odd number of elements in hash assignment");
698 tmpstr = NEWSV(29,0);
699 didstore = hv_store_ent(hash,*relem,tmpstr,0);
701 if (SvSMAGICAL(tmpstr))
704 SvREFCNT_dec(tmpstr);
713 if (SvTHINKFIRST(sv)) {
714 if (SvREADONLY(sv) && PL_curcop != &PL_compiling) {
717 if (relem <= lastrelem)
724 if (relem <= lastrelem) {
725 sv_setsv(sv, *relem);
729 sv_setsv(sv, &PL_sv_undef);
734 if (PL_delaymagic & ~DM_DELAY) {
735 if (PL_delaymagic & DM_UID) {
737 (void)setresuid(PL_uid,PL_euid,(Uid_t)-1);
740 (void)setreuid(PL_uid,PL_euid);
743 if ((PL_delaymagic & DM_UID) == DM_RUID) {
744 (void)setruid(PL_uid);
745 PL_delaymagic &= ~DM_RUID;
747 # endif /* HAS_SETRUID */
749 if ((PL_delaymagic & DM_UID) == DM_EUID) {
750 (void)seteuid(PL_uid);
751 PL_delaymagic &= ~DM_EUID;
753 # endif /* HAS_SETEUID */
754 if (PL_delaymagic & DM_UID) {
755 if (PL_uid != PL_euid)
756 DIE("No setreuid available");
757 (void)PerlProc_setuid(PL_uid);
759 # endif /* HAS_SETREUID */
760 #endif /* HAS_SETRESUID */
761 PL_uid = (int)PerlProc_getuid();
762 PL_euid = (int)PerlProc_geteuid();
764 if (PL_delaymagic & DM_GID) {
766 (void)setresgid(PL_gid,PL_egid,(Gid_t)-1);
769 (void)setregid(PL_gid,PL_egid);
772 if ((PL_delaymagic & DM_GID) == DM_RGID) {
773 (void)setrgid(PL_gid);
774 PL_delaymagic &= ~DM_RGID;
776 # endif /* HAS_SETRGID */
778 if ((PL_delaymagic & DM_GID) == DM_EGID) {
779 (void)setegid(PL_gid);
780 PL_delaymagic &= ~DM_EGID;
782 # endif /* HAS_SETEGID */
783 if (PL_delaymagic & DM_GID) {
784 if (PL_gid != PL_egid)
785 DIE("No setregid available");
786 (void)PerlProc_setgid(PL_gid);
788 # endif /* HAS_SETREGID */
789 #endif /* HAS_SETRESGID */
790 PL_gid = (int)PerlProc_getgid();
791 PL_egid = (int)PerlProc_getegid();
793 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
800 else if (gimme == G_SCALAR) {
803 SETi(lastrelem - firstrelem + 1);
809 SP = firstrelem + (lastlelem - firstlelem);
810 lelem = firstlelem + (relem - firstrelem);
812 *relem++ = (lelem <= lastlelem) ? *lelem++ : &PL_sv_undef;
820 register PMOP *pm = cPMOP;
821 SV *rv = sv_newmortal();
822 SV *sv = newSVrv(rv, "re");
823 sv_magic(sv,(SV*)ReREFCNT_inc(pm->op_pmregexp),'r',0,0);
830 register PMOP *pm = cPMOP;
837 register REGEXP *rx = pm->op_pmregexp;
842 I32 oldsave = PL_savestack_ix;
843 I32 update_minmatch = 1;
846 if (PL_op->op_flags & OPf_STACKED)
852 PUTBACK; /* EVAL blocks need stack_sp. */
856 DIE("panic: do_match");
857 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
858 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
861 if (pm->op_pmdynflags & PMdf_USED) {
863 if (gimme == G_ARRAY)
868 if (!rx->prelen && PL_curpm) {
870 rx = pm->op_pmregexp;
872 if (rx->minlen > len) goto failure;
874 screamer = ( (SvSCREAM(TARG) && rx->check_substr
875 && SvTYPE(rx->check_substr) == SVt_PVBM
876 && SvVALID(rx->check_substr))
879 if (global = pm->op_pmflags & PMf_GLOBAL) {
881 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
882 MAGIC* mg = mg_find(TARG, 'g');
883 if (mg && mg->mg_len >= 0) {
884 rx->endp[0] = rx->startp[0] = s + mg->mg_len;
885 minmatch = (mg->mg_flags & MGf_MINMATCH);
890 safebase = ((gimme != G_ARRAY && !global && rx->nparens)
891 || SvTEMP(TARG) || PL_sawampersand)
892 ? REXEC_COPY_STR : 0;
893 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
894 SAVEINT(PL_multiline);
895 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
899 if (global && rx->startp[0]) {
901 if ((s + rx->minlen) > strend)
903 if (update_minmatch++)
904 minmatch = (s == rx->startp[0]);
906 if (rx->check_substr) {
907 if (!(rx->reganch & ROPT_NOSCAN)) { /* Floating checkstring. */
912 if (PL_screamfirst[BmRARE(rx->check_substr)] < 0)
915 b = (char*)HOP((U8*)s, rx->check_offset_min);
916 if (!(s = screaminstr(TARG, rx->check_substr, b - s, 0, &p, 0)))
919 if ((rx->reganch & ROPT_CHECK_ALL)
920 && !PL_sawampersand && !SvTAIL(rx->check_substr))
923 else if (!(s = fbm_instr((unsigned char*)HOP((U8*)s, rx->check_offset_min),
924 (unsigned char*)strend,
925 rx->check_substr, 0)))
927 else if ((rx->reganch & ROPT_CHECK_ALL) && !PL_sawampersand)
929 if (s && rx->check_offset_max < s - t) {
930 ++BmUSEFUL(rx->check_substr);
931 s = (char*)HOP((U8*)s, -rx->check_offset_max);
936 /* Now checkstring is fixed, i.e. at fixed offset from the
937 beginning of match, and the match is anchored at s. */
938 else if (!PL_multiline) { /* Anchored near beginning of string. */
940 char *b = (char*)HOP((U8*)s, rx->check_offset_min);
941 if (*SvPVX(rx->check_substr) != *b
942 || ((slen = SvCUR(rx->check_substr)) > 1
943 && memNE(SvPVX(rx->check_substr), b, slen)))
946 if (!(rx->reganch & ROPT_NAUGHTY) && --BmUSEFUL(rx->check_substr) < 0
947 && rx->check_substr == rx->float_substr) {
948 SvREFCNT_dec(rx->check_substr);
949 rx->check_substr = Nullsv; /* opt is being useless */
950 rx->float_substr = Nullsv;
953 if (CALLREGEXEC(rx, s, strend, truebase, minmatch,
954 screamer, NULL, safebase))
957 if (pm->op_pmflags & PMf_ONCE)
958 pm->op_pmdynflags |= PMdf_USED;
967 RX_MATCH_TAINTED_on(rx);
968 TAINT_IF(RX_MATCH_TAINTED(rx));
969 if (gimme == G_ARRAY) {
973 if (global && !iters)
977 SPAGAIN; /* EVAL blocks could move the stack. */
978 EXTEND(SP, iters + i);
979 EXTEND_MORTAL(iters + i);
980 for (i = !i; i <= iters; i++) {
981 PUSHs(sv_newmortal());
983 if ((s = rx->startp[i]) && rx->endp[i] ) {
984 len = rx->endp[i] - s;
985 sv_setpvn(*SP, s, len);
989 truebase = rx->subbeg;
991 if (rx->startp[0] && rx->startp[0] == rx->endp[0])
993 PUTBACK; /* EVAL blocks may use stack */
998 LEAVE_SCOPE(oldsave);
1004 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
1005 mg = mg_find(TARG, 'g');
1007 sv_magic(TARG, (SV*)0, 'g', Nullch, 0);
1008 mg = mg_find(TARG, 'g');
1010 if (rx->startp[0]) {
1011 mg->mg_len = rx->endp[0] - rx->subbeg;
1012 if (rx->startp[0] == rx->endp[0])
1013 mg->mg_flags |= MGf_MINMATCH;
1015 mg->mg_flags &= ~MGf_MINMATCH;
1018 LEAVE_SCOPE(oldsave);
1022 yup: /* Confirmed by check_substr */
1024 RX_MATCH_TAINTED_on(rx);
1025 TAINT_IF(RX_MATCH_TAINTED(rx));
1026 ++BmUSEFUL(rx->check_substr);
1028 if (pm->op_pmflags & PMf_ONCE)
1029 pm->op_pmdynflags |= PMdf_USED;
1030 Safefree(rx->subbase);
1031 rx->subbase = Nullch;
1033 rx->subbeg = truebase;
1034 rx->subend = strend;
1036 rx->endp[0] = s + SvCUR(rx->check_substr);
1039 if (PL_sawampersand) {
1042 tmps = rx->subbase = savepvn(t, strend-t);
1044 rx->subend = tmps + (strend-t);
1045 tmps = rx->startp[0] = tmps + (s - t);
1046 rx->endp[0] = tmps + SvCUR(rx->check_substr);
1048 LEAVE_SCOPE(oldsave);
1052 if (rx->check_substr)
1053 ++BmUSEFUL(rx->check_substr);
1056 if (global && !(pm->op_pmflags & PMf_CONTINUE)) {
1057 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
1058 MAGIC* mg = mg_find(TARG, 'g');
1063 LEAVE_SCOPE(oldsave);
1064 if (gimme == G_ARRAY)
1072 dSP; dTARGETSTACKED;
1077 register IO *io = GvIO(PL_last_in_gv);
1078 register I32 type = PL_op->op_type;
1079 I32 gimme = GIMME_V;
1082 if (mg = SvTIED_mg((SV*)PL_last_in_gv, 'q')) {
1084 XPUSHs(SvTIED_obj((SV*)PL_last_in_gv, mg));
1087 perl_call_method("READLINE", gimme);
1090 if (gimme == G_SCALAR)
1091 SvSetMagicSV_nosteal(TARG, TOPs);
1098 if (IoFLAGS(io) & IOf_ARGV) {
1099 if (IoFLAGS(io) & IOf_START) {
1100 IoFLAGS(io) &= ~IOf_START;
1102 if (av_len(GvAVn(PL_last_in_gv)) < 0) {
1103 do_open(PL_last_in_gv,"-",1,FALSE,O_RDONLY,0,Nullfp);
1104 sv_setpvn(GvSV(PL_last_in_gv), "-", 1);
1105 SvSETMAGIC(GvSV(PL_last_in_gv));
1110 fp = nextargv(PL_last_in_gv);
1111 if (!fp) { /* Note: fp != IoIFP(io) */
1112 (void)do_close(PL_last_in_gv, FALSE); /* now it does*/
1113 IoFLAGS(io) |= IOf_START;
1116 else if (type == OP_GLOB) {
1117 SV *tmpcmd = NEWSV(55, 0);
1121 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
1122 /* since spawning off a process is a real performance hit */
1124 #include <descrip.h>
1125 #include <lib$routines.h>
1128 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
1129 char vmsspec[NAM$C_MAXRSS+1];
1130 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
1131 char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
1132 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
1135 struct dsc$descriptor_s wilddsc
1136 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
1137 struct dsc$descriptor_vs rsdsc
1138 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
1139 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
1141 /* We could find out if there's an explicit dev/dir or version
1142 by peeking into lib$find_file's internal context at
1143 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
1144 but that's unsupported, so I don't want to do it now and
1145 have it bite someone in the future. */
1146 strcat(tmpfnam,PerlLIO_tmpnam(NULL));
1147 cp = SvPV(tmpglob,i);
1149 if (cp[i] == ';') hasver = 1;
1151 if (sts) hasver = 1;
1155 hasdir = isunix = 1;
1158 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
1163 if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
1165 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
1166 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
1167 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
1168 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
1169 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
1170 &dfltdsc,NULL,NULL,NULL))&1)) {
1171 end = rstr + (unsigned long int) *rslt;
1172 if (!hasver) while (*end != ';') end--;
1173 *(end++) = '\n'; *end = '\0';
1174 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
1176 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
1181 while (*(--begin) != ']' && *begin != '>') ;
1184 ok = (PerlIO_puts(tmpfp,begin) != EOF);
1186 if (cxt) (void)lib$find_file_end(&cxt);
1187 if (ok && sts != RMS$_NMF &&
1188 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
1191 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
1193 PerlIO_close(tmpfp);
1197 PerlIO_rewind(tmpfp);
1199 IoIFP(io) = fp = tmpfp;
1200 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
1207 sv_setpv(tmpcmd, "for a in ");
1208 sv_catsv(tmpcmd, tmpglob);
1209 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
1212 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
1213 sv_catsv(tmpcmd, tmpglob);
1215 sv_setpv(tmpcmd, "perlglob ");
1216 sv_catsv(tmpcmd, tmpglob);
1217 sv_catpv(tmpcmd, " |");
1222 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
1223 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
1224 sv_catsv(tmpcmd, tmpglob);
1225 sv_catpv(tmpcmd, "' 2>/dev/null |");
1227 sv_setpv(tmpcmd, "echo ");
1228 sv_catsv(tmpcmd, tmpglob);
1230 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
1232 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
1235 #endif /* !DOSISH */
1236 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
1237 FALSE, O_RDONLY, 0, Nullfp);
1243 else if (type == OP_GLOB)
1247 if (ckWARN(WARN_CLOSED) && io && !(IoFLAGS(io) & IOf_START))
1249 "Read on closed filehandle <%s>", GvENAME(PL_last_in_gv));
1250 if (gimme == G_SCALAR) {
1251 (void)SvOK_off(TARG);
1257 if (gimme == G_SCALAR) {
1261 (void)SvUPGRADE(sv, SVt_PV);
1262 tmplen = SvLEN(sv); /* remember if already alloced */
1264 Sv_Grow(sv, 80); /* try short-buffering it */
1265 if (type == OP_RCATLINE)
1271 sv = sv_2mortal(NEWSV(57, 80));
1275 if (!sv_gets(sv, fp, offset)) {
1276 PerlIO_clearerr(fp);
1277 if (IoFLAGS(io) & IOf_ARGV) {
1278 fp = nextargv(PL_last_in_gv);
1281 (void)do_close(PL_last_in_gv, FALSE);
1282 IoFLAGS(io) |= IOf_START;
1284 else if (type == OP_GLOB) {
1285 if (!do_close(PL_last_in_gv, FALSE) && ckWARN(WARN_CLOSED)) {
1287 "glob failed (child exited with status %d%s)",
1288 STATUS_CURRENT >> 8,
1289 (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
1292 if (gimme == G_SCALAR) {
1293 (void)SvOK_off(TARG);
1298 /* This should not be marked tainted if the fp is marked clean */
1299 if (!(IoFLAGS(io) & IOf_UNTAINT)) {
1306 if (type == OP_GLOB) {
1309 if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) {
1310 tmps = SvEND(sv) - 1;
1311 if (*tmps == *SvPVX(PL_rs)) {
1316 for (tmps = SvPVX(sv); *tmps; tmps++)
1317 if (!isALPHA(*tmps) && !isDIGIT(*tmps) &&
1318 strchr("$&*(){}[]'\";\\|?<>~`", *tmps))
1320 if (*tmps && PerlLIO_stat(SvPVX(sv), &PL_statbuf) < 0) {
1321 (void)POPs; /* Unmatched wildcard? Chuck it... */
1325 if (gimme == G_ARRAY) {
1326 if (SvLEN(sv) - SvCUR(sv) > 20) {
1327 SvLEN_set(sv, SvCUR(sv)+1);
1328 Renew(SvPVX(sv), SvLEN(sv), char);
1330 sv = sv_2mortal(NEWSV(58, 80));
1333 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
1334 /* try to reclaim a bit of scalar space (only on 1st alloc) */
1338 SvLEN_set(sv, SvCUR(sv)+40); /* allow some slop */
1339 Renew(SvPVX(sv), SvLEN(sv), char);
1348 register PERL_CONTEXT *cx;
1349 I32 gimme = OP_GIMME(PL_op, -1);
1352 if (cxstack_ix >= 0)
1353 gimme = cxstack[cxstack_ix].blk_gimme;
1361 PUSHBLOCK(cx, CXt_BLOCK, SP);
1373 U32 lval = PL_op->op_flags & OPf_MOD;
1374 U32 defer = PL_op->op_private & OPpLVAL_DEFER;
1377 if (SvTYPE(hv) == SVt_PVHV) {
1378 he = hv_fetch_ent(hv, keysv, lval && !defer, 0);
1379 svp = he ? &HeVAL(he) : 0;
1381 else if (SvTYPE(hv) == SVt_PVAV) {
1382 if (PL_op->op_private & OPpLVAL_INTRO)
1383 DIE("Can't localize pseudo-hash element");
1384 svp = avhv_fetch_ent((AV*)hv, keysv, lval && !defer, 0);
1390 if (!svp || *svp == &PL_sv_undef) {
1394 DIE(PL_no_helem, SvPV(keysv, PL_na));
1395 lv = sv_newmortal();
1396 sv_upgrade(lv, SVt_PVLV);
1398 sv_magic(lv, key2 = newSVsv(keysv), 'y', Nullch, 0);
1399 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1400 LvTARG(lv) = SvREFCNT_inc(hv);
1405 if (PL_op->op_private & OPpLVAL_INTRO) {
1406 if (HvNAME(hv) && isGV(*svp))
1407 save_gp((GV*)*svp, !(PL_op->op_flags & OPf_SPECIAL));
1409 save_helem(hv, keysv, svp);
1411 else if (PL_op->op_private & OPpDEREF)
1412 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
1414 sv = (svp ? *svp : &PL_sv_undef);
1415 /* This makes C<local $tied{foo} = $tied{foo}> possible.
1416 * Pushing the magical RHS on to the stack is useless, since
1417 * that magic is soon destined to be misled by the local(),
1418 * and thus the later pp_sassign() will fail to mg_get() the
1419 * old value. This should also cure problems with delayed
1420 * mg_get()s. GSAR 98-07-03 */
1421 if (!lval && SvGMAGICAL(sv))
1422 sv = sv_mortalcopy(sv);
1430 register PERL_CONTEXT *cx;
1436 if (PL_op->op_flags & OPf_SPECIAL) {
1437 cx = &cxstack[cxstack_ix];
1438 cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */
1443 gimme = OP_GIMME(PL_op, -1);
1445 if (cxstack_ix >= 0)
1446 gimme = cxstack[cxstack_ix].blk_gimme;
1452 if (gimme == G_VOID)
1454 else if (gimme == G_SCALAR) {
1457 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1460 *MARK = sv_mortalcopy(TOPs);
1463 *MARK = &PL_sv_undef;
1467 else if (gimme == G_ARRAY) {
1468 /* in case LEAVE wipes old return values */
1469 for (mark = newsp + 1; mark <= SP; mark++) {
1470 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
1471 *mark = sv_mortalcopy(*mark);
1472 TAINT_NOT; /* Each item is independent */
1476 PL_curpm = newpm; /* Don't pop $1 et al till now */
1486 register PERL_CONTEXT *cx;
1491 cx = &cxstack[cxstack_ix];
1492 if (CxTYPE(cx) != CXt_LOOP)
1493 DIE("panic: pp_iter");
1495 av = cx->blk_loop.iterary;
1496 if (SvTYPE(av) != SVt_PVAV) {
1497 /* iterate ($min .. $max) */
1498 if (cx->blk_loop.iterlval) {
1499 /* string increment */
1500 register SV* cur = cx->blk_loop.iterlval;
1502 char *max = SvPV((SV*)av, maxlen);
1503 if (!SvNIOK(cur) && SvCUR(cur) <= maxlen) {
1504 #ifndef USE_THREADS /* don't risk potential race */
1505 if (SvREFCNT(*cx->blk_loop.itervar) == 1
1506 && !SvMAGICAL(*cx->blk_loop.itervar))
1508 /* safe to reuse old SV */
1509 sv_setsv(*cx->blk_loop.itervar, cur);
1514 /* we need a fresh SV every time so that loop body sees a
1515 * completely new SV for closures/references to work as
1517 SvREFCNT_dec(*cx->blk_loop.itervar);
1518 *cx->blk_loop.itervar = newSVsv(cur);
1520 if (strEQ(SvPVX(cur), max))
1521 sv_setiv(cur, 0); /* terminate next time */
1528 /* integer increment */
1529 if (cx->blk_loop.iterix > cx->blk_loop.itermax)
1532 #ifndef USE_THREADS /* don't risk potential race */
1533 if (SvREFCNT(*cx->blk_loop.itervar) == 1
1534 && !SvMAGICAL(*cx->blk_loop.itervar))
1536 /* safe to reuse old SV */
1537 sv_setiv(*cx->blk_loop.itervar, cx->blk_loop.iterix++);
1542 /* we need a fresh SV every time so that loop body sees a
1543 * completely new SV for closures/references to work as they
1545 SvREFCNT_dec(*cx->blk_loop.itervar);
1546 *cx->blk_loop.itervar = newSViv(cx->blk_loop.iterix++);
1552 if (cx->blk_loop.iterix >= (av == PL_curstack ? cx->blk_oldsp : AvFILL(av)))
1555 SvREFCNT_dec(*cx->blk_loop.itervar);
1557 if (sv = (SvMAGICAL(av))
1558 ? *av_fetch(av, ++cx->blk_loop.iterix, FALSE)
1559 : AvARRAY(av)[++cx->blk_loop.iterix])
1563 if (av != PL_curstack && SvIMMORTAL(sv)) {
1564 SV *lv = cx->blk_loop.iterlval;
1565 if (lv && SvREFCNT(lv) > 1) {
1570 SvREFCNT_dec(LvTARG(lv));
1572 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
1573 sv_upgrade(lv, SVt_PVLV);
1575 sv_magic(lv, Nullsv, 'y', Nullch, 0);
1577 LvTARG(lv) = SvREFCNT_inc(av);
1578 LvTARGOFF(lv) = cx->blk_loop.iterix;
1579 LvTARGLEN(lv) = (UV) -1;
1583 *cx->blk_loop.itervar = SvREFCNT_inc(sv);
1590 register PMOP *pm = cPMOP;
1606 register REGEXP *rx = pm->op_pmregexp;
1608 int force_on_match = 0;
1609 I32 oldsave = PL_savestack_ix;
1610 I32 update_minmatch = 1;
1613 /* known replacement string? */
1614 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
1615 if (PL_op->op_flags & OPf_STACKED)
1621 if (SvREADONLY(TARG)
1622 || (SvTYPE(TARG) > SVt_PVLV
1623 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG))))
1624 croak(PL_no_modify);
1627 s = SvPV(TARG, len);
1628 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
1630 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
1631 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
1638 DIE("panic: do_subst");
1641 maxiters = 2*(strend - s) + 10; /* We can match twice at each
1642 position, once with zero-length,
1643 second time with non-zero. */
1645 if (!rx->prelen && PL_curpm) {
1647 rx = pm->op_pmregexp;
1649 screamer = ( (SvSCREAM(TARG) && rx->check_substr
1650 && SvTYPE(rx->check_substr) == SVt_PVBM
1651 && SvVALID(rx->check_substr))
1653 safebase = (rx->nparens || SvTEMP(TARG) || PL_sawampersand)
1654 ? REXEC_COPY_STR : 0;
1655 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
1656 SAVEINT(PL_multiline);
1657 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
1660 if (rx->check_substr) {
1661 if (!(rx->reganch & ROPT_NOSCAN)) { /* It floats. */
1666 if (PL_screamfirst[BmRARE(rx->check_substr)] < 0)
1669 b = (char*)HOP((U8*)s, rx->check_offset_min);
1670 if (!(s = screaminstr(TARG, rx->check_substr, b - s, 0, &p, 0)))
1673 else if (!(s = fbm_instr((unsigned char*)HOP((U8*)s, rx->check_offset_min),
1674 (unsigned char*)strend,
1675 rx->check_substr, 0)))
1677 if (s && rx->check_offset_max < s - m) {
1678 ++BmUSEFUL(rx->check_substr);
1679 s = (char*)HOP((U8*)s, -rx->check_offset_max);
1684 /* Now checkstring is fixed, i.e. at fixed offset from the
1685 beginning of match, and the match is anchored at s. */
1686 else if (!PL_multiline) { /* Anchored at beginning of string. */
1688 char *b = (char*)HOP((U8*)s, rx->check_offset_min);
1689 if (*SvPVX(rx->check_substr) != *b
1690 || ((slen = SvCUR(rx->check_substr)) > 1
1691 && memNE(SvPVX(rx->check_substr), b, slen)))
1694 if (!(rx->reganch & ROPT_NAUGHTY) && --BmUSEFUL(rx->check_substr) < 0
1695 && rx->check_substr == rx->float_substr) {
1696 SvREFCNT_dec(rx->check_substr);
1697 rx->check_substr = Nullsv; /* opt is being useless */
1698 rx->float_substr = Nullsv;
1702 /* only replace once? */
1703 once = !(rpm->op_pmflags & PMf_GLOBAL);
1705 /* known replacement string? */
1706 c = dstr ? SvPV(dstr, clen) : Nullch;
1708 /* can do inplace substitution? */
1709 if (c && clen <= rx->minlen && (once || !(safebase & REXEC_COPY_STR))
1710 && !(rx->reganch & ROPT_LOOKBEHIND_SEEN)) {
1711 if (!CALLREGEXEC(rx, s, strend, orig, 0, screamer, NULL, safebase)) {
1714 LEAVE_SCOPE(oldsave);
1717 if (force_on_match) {
1719 s = SvPV_force(TARG, len);
1724 SvSCREAM_off(TARG); /* disable possible screamer */
1726 rxtainted |= RX_MATCH_TAINTED(rx);
1728 m = orig + (rx->startp[0] - rx->subbase);
1729 d = orig + (rx->endp[0] - rx->subbase);
1735 if (m - s > strend - d) { /* faster to shorten from end */
1737 Copy(c, m, clen, char);
1742 Move(d, m, i, char);
1746 SvCUR_set(TARG, m - s);
1749 else if (i = m - s) { /* faster from front */
1757 Copy(c, m, clen, char);
1762 Copy(c, d, clen, char);
1767 TAINT_IF(rxtainted & 1);
1773 if (iters++ > maxiters)
1774 DIE("Substitution loop");
1775 rxtainted |= RX_MATCH_TAINTED(rx);
1780 Move(s, d, i, char);
1784 Copy(c, d, clen, char);
1788 } while (CALLREGEXEC(rx, s, strend, orig, s == m,
1789 Nullsv, NULL, 0)); /* don't match same null twice */
1792 SvCUR_set(TARG, d - SvPVX(TARG) + i);
1793 Move(s, d, i+1, char); /* include the NUL */
1795 TAINT_IF(rxtainted & 1);
1797 PUSHs(sv_2mortal(newSViv((I32)iters)));
1799 (void)SvPOK_only(TARG);
1800 TAINT_IF(rxtainted);
1801 if (SvSMAGICAL(TARG)) {
1807 LEAVE_SCOPE(oldsave);
1811 if (CALLREGEXEC(rx, s, strend, orig, 0, screamer, NULL, safebase)) {
1812 if (force_on_match) {
1814 s = SvPV_force(TARG, len);
1817 rxtainted |= RX_MATCH_TAINTED(rx);
1818 dstr = NEWSV(25, len);
1819 sv_setpvn(dstr, m, s-m);
1822 register PERL_CONTEXT *cx;
1825 RETURNOP(cPMOP->op_pmreplroot);
1828 if (iters++ > maxiters)
1829 DIE("Substitution loop");
1830 rxtainted |= RX_MATCH_TAINTED(rx);
1831 if (rx->subbase && rx->subbase != orig) {
1836 strend = s + (strend - m);
1839 sv_catpvn(dstr, s, m-s);
1842 sv_catpvn(dstr, c, clen);
1845 } while (CALLREGEXEC(rx, s, strend, orig, s == m, Nullsv, NULL, safebase));
1846 sv_catpvn(dstr, s, strend - s);
1848 (void)SvOOK_off(TARG);
1849 Safefree(SvPVX(TARG));
1850 SvPVX(TARG) = SvPVX(dstr);
1851 SvCUR_set(TARG, SvCUR(dstr));
1852 SvLEN_set(TARG, SvLEN(dstr));
1856 TAINT_IF(rxtainted & 1);
1858 PUSHs(sv_2mortal(newSViv((I32)iters)));
1860 (void)SvPOK_only(TARG);
1861 TAINT_IF(rxtainted);
1864 LEAVE_SCOPE(oldsave);
1870 ++BmUSEFUL(rx->check_substr);
1875 LEAVE_SCOPE(oldsave);
1884 PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr];
1885 ++*PL_markstack_ptr;
1886 LEAVE; /* exit inner scope */
1889 if (PL_stack_base + *PL_markstack_ptr > SP) {
1891 I32 gimme = GIMME_V;
1893 LEAVE; /* exit outer scope */
1894 (void)POPMARK; /* pop src */
1895 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
1896 (void)POPMARK; /* pop dst */
1897 SP = PL_stack_base + POPMARK; /* pop original mark */
1898 if (gimme == G_SCALAR) {
1902 else if (gimme == G_ARRAY)
1909 ENTER; /* enter inner scope */
1912 src = PL_stack_base[*PL_markstack_ptr];
1916 RETURNOP(cLOGOP->op_other);
1927 register PERL_CONTEXT *cx;
1928 struct block_sub cxsub;
1931 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1934 if (gimme == G_SCALAR) {
1937 if (cxsub.cv && CvDEPTH(cxsub.cv) > 1) {
1939 *MARK = SvREFCNT_inc(TOPs);
1944 *MARK = sv_mortalcopy(TOPs);
1947 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
1950 *MARK = &PL_sv_undef;
1954 else if (gimme == G_ARRAY) {
1955 for (MARK = newsp + 1; MARK <= SP; MARK++) {
1956 if (!SvTEMP(*MARK)) {
1957 *MARK = sv_mortalcopy(*MARK);
1958 TAINT_NOT; /* Each item is independent */
1964 POPSUB2(); /* Stack values are safe: release CV and @_ ... */
1965 PL_curpm = newpm; /* ... and pop $1 et al */
1968 return pop_return();
1972 get_db_sub(SV **svp, CV *cv)
1975 SV *dbsv = GvSV(PL_DBsub);
1977 if (!PERLDB_SUB_NN) {
1981 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
1982 || strEQ(GvNAME(gv), "END")
1983 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
1984 !( (SvTYPE(*svp) == SVt_PVGV) && (GvCV((GV*)*svp) == cv)
1985 && (gv = (GV*)*svp) ))) {
1986 /* Use GV from the stack as a fallback. */
1987 /* GV is potentially non-unique, or contain different CV. */
1988 sv_setsv(dbsv, newRV((SV*)cv));
1991 gv_efullname3(dbsv, gv, Nullch);
1995 SvUPGRADE(dbsv, SVt_PVIV);
1997 SAVEIV(SvIVX(dbsv));
1998 SvIVX(dbsv) = (IV)cv; /* Do it the quickest way */
2002 PL_curcopdb = PL_curcop;
2003 cv = GvCV(PL_DBsub);
2013 register PERL_CONTEXT *cx;
2015 bool hasargs = (PL_op->op_flags & OPf_STACKED) != 0;
2018 DIE("Not a CODE reference");
2019 switch (SvTYPE(sv)) {
2024 if (sv == &PL_sv_yes) { /* unfound import, ignore */
2026 SP = PL_stack_base + POPMARK;
2029 if (SvGMAGICAL(sv)) {
2031 sym = SvPOKp(sv) ? SvPVX(sv) : Nullch;
2034 sym = SvPV(sv, PL_na);
2036 DIE(PL_no_usym, "a subroutine");
2037 if (PL_op->op_private & HINT_STRICT_REFS)
2038 DIE(PL_no_symref, sym, "a subroutine");
2039 cv = perl_get_cv(sym, TRUE);
2043 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
2044 tryAMAGICunDEREF(to_cv);
2047 if (SvTYPE(cv) == SVt_PVCV)
2052 DIE("Not a CODE reference");
2057 if (!(cv = GvCVu((GV*)sv)))
2058 cv = sv_2cv(sv, &stash, &gv, TRUE);
2067 DIE("Not a CODE reference");
2069 if (!CvROOT(cv) && !CvXSUB(cv)) {
2073 /* anonymous or undef'd function leaves us no recourse */
2074 if (CvANON(cv) || !(gv = CvGV(cv)))
2075 DIE("Undefined subroutine called");
2076 /* autoloaded stub? */
2077 if (cv != GvCV(gv)) {
2081 /* should call AUTOLOAD now? */
2082 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
2089 sub_name = sv_newmortal();
2090 gv_efullname3(sub_name, gv, Nullch);
2091 DIE("Undefined subroutine &%s called", SvPVX(sub_name));
2095 if ((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub) && !CvNODEBUG(cv))
2096 cv = get_db_sub(&sv, cv);
2098 DIE("No DBsub routine");
2102 * First we need to check if the sub or method requires locking.
2103 * If so, we gain a lock on the CV, the first argument or the
2104 * stash (for static methods), as appropriate. This has to be
2105 * inline because for FAKE_THREADS, COND_WAIT inlines code to
2106 * reschedule by returning a new op.
2108 MUTEX_LOCK(CvMUTEXP(cv));
2109 if (CvFLAGS(cv) & CVf_LOCKED) {
2111 if (CvFLAGS(cv) & CVf_METHOD) {
2112 if (SP > PL_stack_base + TOPMARK)
2113 sv = *(PL_stack_base + TOPMARK + 1);
2115 MUTEX_UNLOCK(CvMUTEXP(cv));
2116 croak("no argument for locked method call");
2122 char *stashname = SvPV(sv, len);
2123 sv = (SV*)gv_stashpvn(stashname, len, TRUE);
2129 MUTEX_UNLOCK(CvMUTEXP(cv));
2130 mg = condpair_magic(sv);
2131 MUTEX_LOCK(MgMUTEXP(mg));
2132 if (MgOWNER(mg) == thr)
2133 MUTEX_UNLOCK(MgMUTEXP(mg));
2136 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
2138 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "%p: pp_entersub lock %p\n",
2140 MUTEX_UNLOCK(MgMUTEXP(mg));
2141 save_destructor(unlock_condpair, sv);
2143 MUTEX_LOCK(CvMUTEXP(cv));
2146 * Now we have permission to enter the sub, we must distinguish
2147 * four cases. (0) It's an XSUB (in which case we don't care
2148 * about ownership); (1) it's ours already (and we're recursing);
2149 * (2) it's free (but we may already be using a cached clone);
2150 * (3) another thread owns it. Case (1) is easy: we just use it.
2151 * Case (2) means we look for a clone--if we have one, use it
2152 * otherwise grab ownership of cv. Case (3) means we look for a
2153 * clone (for non-XSUBs) and have to create one if we don't
2155 * Why look for a clone in case (2) when we could just grab
2156 * ownership of cv straight away? Well, we could be recursing,
2157 * i.e. we originally tried to enter cv while another thread
2158 * owned it (hence we used a clone) but it has been freed up
2159 * and we're now recursing into it. It may or may not be "better"
2160 * to use the clone but at least CvDEPTH can be trusted.
2162 if (CvOWNER(cv) == thr || CvXSUB(cv))
2163 MUTEX_UNLOCK(CvMUTEXP(cv));
2165 /* Case (2) or (3) */
2169 * XXX Might it be better to release CvMUTEXP(cv) while we
2170 * do the hv_fetch? We might find someone has pinched it
2171 * when we look again, in which case we would be in case
2172 * (3) instead of (2) so we'd have to clone. Would the fact
2173 * that we released the mutex more quickly make up for this?
2176 (svp = hv_fetch(thr->cvcache, (char *)cv, sizeof(cv), FALSE)))
2178 /* We already have a clone to use */
2179 MUTEX_UNLOCK(CvMUTEXP(cv));
2181 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
2182 "entersub: %p already has clone %p:%s\n",
2183 thr, cv, SvPEEK((SV*)cv)));
2186 if (CvDEPTH(cv) == 0)
2187 SAVEDESTRUCTOR(unset_cvowner, (void*) cv);
2190 /* (2) => grab ownership of cv. (3) => make clone */
2194 MUTEX_UNLOCK(CvMUTEXP(cv));
2195 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
2196 "entersub: %p grabbing %p:%s in stash %s\n",
2197 thr, cv, SvPEEK((SV*)cv), CvSTASH(cv) ?
2198 HvNAME(CvSTASH(cv)) : "(none)"));
2200 /* Make a new clone. */
2202 SvREFCNT_inc(cv); /* don't let it vanish from under us */
2203 MUTEX_UNLOCK(CvMUTEXP(cv));
2204 DEBUG_S((PerlIO_printf(PerlIO_stderr(),
2205 "entersub: %p cloning %p:%s\n",
2206 thr, cv, SvPEEK((SV*)cv))));
2208 * We're creating a new clone so there's no race
2209 * between the original MUTEX_UNLOCK and the
2210 * SvREFCNT_inc since no one will be trying to undef
2211 * it out from underneath us. At least, I don't think
2214 clonecv = cv_clone(cv);
2215 SvREFCNT_dec(cv); /* finished with this */
2216 hv_store(thr->cvcache, (char*)cv, sizeof(cv), (SV*)clonecv,0);
2217 CvOWNER(clonecv) = thr;
2221 DEBUG_S(if (CvDEPTH(cv) != 0)
2222 PerlIO_printf(PerlIO_stderr(), "depth %ld != 0\n",
2224 SAVEDESTRUCTOR(unset_cvowner, (void*) cv);
2227 #endif /* USE_THREADS */
2230 if (CvOLDSTYLE(cv)) {
2231 I32 (*fp3)_((int,int,int));
2233 register I32 items = SP - MARK;
2234 /* We dont worry to copy from @_. */
2239 PL_stack_sp = mark + 1;
2240 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
2241 items = (*fp3)(CvXSUBANY(cv).any_i32,
2242 MARK - PL_stack_base + 1,
2244 PL_stack_sp = PL_stack_base + items;
2247 I32 markix = TOPMARK;
2252 /* Need to copy @_ to stack. Alternative may be to
2253 * switch stack to @_, and copy return values
2254 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
2258 av = (AV*)PL_curpad[0];
2260 av = GvAV(PL_defgv);
2261 #endif /* USE_THREADS */
2262 items = AvFILLp(av) + 1; /* @_ is not tieable */
2265 /* Mark is at the end of the stack. */
2267 Copy(AvARRAY(av), SP + 1, items, SV*);
2272 if (PL_curcopdb) { /* We assume that the first
2273 XSUB in &DB::sub is the
2275 SAVESPTR(PL_curcop);
2276 PL_curcop = PL_curcopdb;
2279 /* Do we need to open block here? XXXX */
2280 (void)(*CvXSUB(cv))(cv _PERL_OBJECT_THIS);
2282 /* Enforce some sanity in scalar context. */
2283 if (gimme == G_SCALAR && ++markix != PL_stack_sp - PL_stack_base ) {
2284 if (markix > PL_stack_sp - PL_stack_base)
2285 *(PL_stack_base + markix) = &PL_sv_undef;
2287 *(PL_stack_base + markix) = *PL_stack_sp;
2288 PL_stack_sp = PL_stack_base + markix;
2296 register I32 items = SP - MARK;
2297 AV* padlist = CvPADLIST(cv);
2298 SV** svp = AvARRAY(padlist);
2299 push_return(PL_op->op_next);
2300 PUSHBLOCK(cx, CXt_SUB, MARK);
2303 /* XXX This would be a natural place to set C<PL_compcv = cv> so
2304 * that eval'' ops within this sub know the correct lexical space.
2305 * Owing the speed considerations, we choose to search for the cv
2306 * in doeval() instead.
2308 if (CvDEPTH(cv) < 2)
2309 (void)SvREFCNT_inc(cv);
2310 else { /* save temporaries on recursion? */
2311 if (CvDEPTH(cv) > AvFILLp(padlist)) {
2313 AV *newpad = newAV();
2314 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
2315 I32 ix = AvFILLp((AV*)svp[1]);
2316 svp = AvARRAY(svp[0]);
2317 for ( ;ix > 0; ix--) {
2318 if (svp[ix] != &PL_sv_undef) {
2319 char *name = SvPVX(svp[ix]);
2320 if ((SvFLAGS(svp[ix]) & SVf_FAKE) /* outer lexical? */
2321 || *name == '&') /* anonymous code? */
2323 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix]));
2325 else { /* our own lexical */
2327 av_store(newpad, ix, sv = (SV*)newAV());
2328 else if (*name == '%')
2329 av_store(newpad, ix, sv = (SV*)newHV());
2331 av_store(newpad, ix, sv = NEWSV(0,0));
2336 av_store(newpad, ix, sv = NEWSV(0,0));
2340 av = newAV(); /* will be @_ */
2342 av_store(newpad, 0, (SV*)av);
2343 AvFLAGS(av) = AVf_REIFY;
2344 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
2345 AvFILLp(padlist) = CvDEPTH(cv);
2346 svp = AvARRAY(padlist);
2351 AV* av = (AV*)PL_curpad[0];
2353 items = AvFILLp(av) + 1;
2355 /* Mark is at the end of the stack. */
2357 Copy(AvARRAY(av), SP + 1, items, SV*);
2362 #endif /* USE_THREADS */
2363 SAVESPTR(PL_curpad);
2364 PL_curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
2367 #endif /* USE_THREADS */
2373 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
2374 "%p entersub preparing @_\n", thr));
2376 av = (AV*)PL_curpad[0];
2382 cx->blk_sub.savearray = GvAV(PL_defgv);
2383 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
2384 #endif /* USE_THREADS */
2385 cx->blk_sub.argarray = av;
2388 if (items > AvMAX(av) + 1) {
2390 if (AvARRAY(av) != ary) {
2391 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2392 SvPVX(av) = (char*)ary;
2394 if (items > AvMAX(av) + 1) {
2395 AvMAX(av) = items - 1;
2396 Renew(ary,items,SV*);
2398 SvPVX(av) = (char*)ary;
2401 Copy(MARK,AvARRAY(av),items,SV*);
2402 AvFILLp(av) = items - 1;
2410 /* warning must come *after* we fully set up the context
2411 * stuff so that __WARN__ handlers can safely dounwind()
2414 if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION)
2415 && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))
2416 sub_crush_depth(cv);
2418 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
2419 "%p entersub returning %p\n", thr, CvSTART(cv)));
2421 RETURNOP(CvSTART(cv));
2426 sub_crush_depth(CV *cv)
2429 warner(WARN_RECURSION, "Deep recursion on anonymous subroutine");
2431 SV* tmpstr = sv_newmortal();
2432 gv_efullname3(tmpstr, CvGV(cv), Nullch);
2433 warner(WARN_RECURSION, "Deep recursion on subroutine \"%s\"",
2444 U32 lval = PL_op->op_flags & OPf_MOD;
2445 U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > AvFILL(av));
2449 elem -= PL_curcop->cop_arybase;
2450 if (SvTYPE(av) != SVt_PVAV)
2452 svp = av_fetch(av, elem, lval && !defer);
2454 if (!svp || *svp == &PL_sv_undef) {
2457 DIE(PL_no_aelem, elem);
2458 lv = sv_newmortal();
2459 sv_upgrade(lv, SVt_PVLV);
2461 sv_magic(lv, Nullsv, 'y', Nullch, 0);
2462 LvTARG(lv) = SvREFCNT_inc(av);
2463 LvTARGOFF(lv) = elem;
2468 if (PL_op->op_private & OPpLVAL_INTRO)
2469 save_aelem(av, elem, svp);
2470 else if (PL_op->op_private & OPpDEREF)
2471 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
2473 sv = (svp ? *svp : &PL_sv_undef);
2474 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
2475 sv = sv_mortalcopy(sv);
2481 vivify_ref(SV *sv, U32 to_what)
2487 croak(PL_no_modify);
2488 if (SvTYPE(sv) < SVt_RV)
2489 sv_upgrade(sv, SVt_RV);
2490 else if (SvTYPE(sv) >= SVt_PV) {
2491 (void)SvOOK_off(sv);
2492 Safefree(SvPVX(sv));
2493 SvLEN(sv) = SvCUR(sv) = 0;
2497 SvRV(sv) = NEWSV(355,0);
2500 SvRV(sv) = (SV*)newAV();
2503 SvRV(sv) = (SV*)newHV();
2524 if (SvTYPE(sv) == SVt_PVCV) {
2530 name = SvPV(TOPs, PL_na);
2531 sv = *(PL_stack_base + TOPMARK + 1);
2542 !(packname = SvPV(sv, packlen)) ||
2543 !(iogv = gv_fetchpv(packname, FALSE, SVt_PVIO)) ||
2544 !(ob=(SV*)GvIO(iogv)))
2547 ((*(U8*)packname >= 0xc0 && IN_UTF8)
2548 ? !isIDFIRST_utf8((U8*)packname)
2549 : !isIDFIRST(*packname)
2552 DIE("Can't call method \"%s\" %s", name,
2553 SvOK(sv)? "without a package or object reference"
2554 : "on an undefined value");
2556 stash = gv_stashpvn(packname, packlen, TRUE);
2559 *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
2562 if (!ob || !SvOBJECT(ob))
2563 DIE("Can't call method \"%s\" on unblessed reference", name);
2565 stash = SvSTASH(ob);
2568 gv = gv_fetchmethod(stash, name);
2574 for (p = name; *p; p++) {
2576 sep = p, leaf = p + 1;
2577 else if (*p == ':' && *(p + 1) == ':')
2578 sep = p, leaf = p + 2;
2580 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
2581 packname = HvNAME(sep ? PL_curcop->cop_stash : stash);
2582 packlen = strlen(packname);
2586 packlen = sep - name;
2588 DIE("Can't locate object method \"%s\" via package \"%.*s\"",
2589 leaf, (int)packlen, packname);
2591 SETs(isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv);