3 * Copyright (c) 1991-2000, 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 * Now far ahead the Road has gone,
12 * And I must follow, if I can,
13 * Pursuing it with eager feet,
14 * Until it joins some larger way
15 * Where many paths and errands meet.
16 * And whither then? I cannot say.
20 #define PERL_IN_PP_CTL_C
24 #define WORD_ALIGN sizeof(U16)
27 #define DOCATCH(o) ((CATCH_GET == TRUE) ? docatch(o) : (o))
29 static I32 sortcv(pTHXo_ SV *a, SV *b);
30 static I32 sortcv_stacked(pTHXo_ SV *a, SV *b);
31 static I32 sortcv_xsub(pTHXo_ SV *a, SV *b);
32 static I32 sv_ncmp(pTHXo_ SV *a, SV *b);
33 static I32 sv_i_ncmp(pTHXo_ SV *a, SV *b);
34 static I32 amagic_ncmp(pTHXo_ SV *a, SV *b);
35 static I32 amagic_i_ncmp(pTHXo_ SV *a, SV *b);
36 static I32 amagic_cmp(pTHXo_ SV *a, SV *b);
37 static I32 amagic_cmp_locale(pTHXo_ SV *a, SV *b);
38 static I32 run_user_filter(pTHXo_ int idx, SV *buf_sv, int maxlen);
41 static I32 sv_cmp_static(pTHXo_ SV *a, SV *b);
42 static I32 sv_cmp_locale_static(pTHXo_ SV *a, SV *b);
44 #define sv_cmp_static Perl_sv_cmp
45 #define sv_cmp_locale_static Perl_sv_cmp_locale
54 cxix = dopoptosub(cxstack_ix);
58 switch (cxstack[cxix].blk_gimme) {
75 /* XXXX Should store the old value to allow for tie/overload - and
76 restore in regcomp, where marked with XXXX. */
84 register PMOP *pm = (PMOP*)cLOGOP->op_other;
88 MAGIC *mg = Null(MAGIC*);
92 SV *sv = SvRV(tmpstr);
94 mg = mg_find(sv, 'r');
97 regexp *re = (regexp *)mg->mg_obj;
98 ReREFCNT_dec(pm->op_pmregexp);
99 pm->op_pmregexp = ReREFCNT_inc(re);
102 t = SvPV(tmpstr, len);
104 /* Check against the last compiled regexp. */
105 if (!pm->op_pmregexp || !pm->op_pmregexp->precomp ||
106 pm->op_pmregexp->prelen != len ||
107 memNE(pm->op_pmregexp->precomp, t, len))
109 if (pm->op_pmregexp) {
110 ReREFCNT_dec(pm->op_pmregexp);
111 pm->op_pmregexp = Null(REGEXP*); /* crucial if regcomp aborts */
113 if (PL_op->op_flags & OPf_SPECIAL)
114 PL_reginterp_cnt = I32_MAX; /* Mark as safe. */
116 pm->op_pmflags = pm->op_pmpermflags; /* reset case sensitivity */
118 pm->op_pmdynflags |= PMdf_UTF8;
119 pm->op_pmregexp = CALLREGCOMP(aTHX_ t, t + len, pm);
120 PL_reginterp_cnt = 0; /* XXXX Be extra paranoid - needed
121 inside tie/overload accessors. */
125 #ifndef INCOMPLETE_TAINTS
128 pm->op_pmdynflags |= PMdf_TAINTED;
130 pm->op_pmdynflags &= ~PMdf_TAINTED;
134 if (!pm->op_pmregexp->prelen && PL_curpm)
136 else if (strEQ("\\s+", pm->op_pmregexp->precomp))
137 pm->op_pmflags |= PMf_WHITE;
139 /* XXX runtime compiled output needs to move to the pad */
140 if (pm->op_pmflags & PMf_KEEP) {
141 pm->op_private &= ~OPpRUNTIME; /* no point compiling again */
142 #if !defined(USE_ITHREADS) && !defined(USE_THREADS)
143 /* XXX can't change the optree at runtime either */
144 cLOGOP->op_first->op_next = PL_op->op_next;
153 register PMOP *pm = (PMOP*) cLOGOP->op_other;
154 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
155 register SV *dstr = cx->sb_dstr;
156 register char *s = cx->sb_s;
157 register char *m = cx->sb_m;
158 char *orig = cx->sb_orig;
159 register REGEXP *rx = cx->sb_rx;
161 rxres_restore(&cx->sb_rxres, rx);
163 if (cx->sb_iters++) {
164 if (cx->sb_iters > cx->sb_maxiters)
165 DIE(aTHX_ "Substitution loop");
167 if (!(cx->sb_rxtainted & 2) && SvTAINTED(TOPs))
168 cx->sb_rxtainted |= 2;
169 sv_catsv(dstr, POPs);
172 if (cx->sb_once || !CALLREGEXEC(aTHX_ rx, s, cx->sb_strend, orig,
173 s == m, cx->sb_targ, NULL,
174 ((cx->sb_rflags & REXEC_COPY_STR)
175 ? (REXEC_IGNOREPOS|REXEC_NOT_FIRST)
176 : (REXEC_COPY_STR|REXEC_IGNOREPOS|REXEC_NOT_FIRST))))
178 SV *targ = cx->sb_targ;
181 sv_catpvn(dstr, s, cx->sb_strend - s);
182 cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
184 (void)SvOOK_off(targ);
185 Safefree(SvPVX(targ));
186 SvPVX(targ) = SvPVX(dstr);
187 SvCUR_set(targ, SvCUR(dstr));
188 SvLEN_set(targ, SvLEN(dstr));
189 isutf8 = DO_UTF8(dstr);
193 TAINT_IF(cx->sb_rxtainted & 1);
194 PUSHs(sv_2mortal(newSViv((I32)cx->sb_iters - 1)));
196 (void)SvPOK_only(targ);
199 TAINT_IF(cx->sb_rxtainted);
203 LEAVE_SCOPE(cx->sb_oldsave);
205 RETURNOP(pm->op_next);
208 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
211 cx->sb_orig = orig = rx->subbeg;
213 cx->sb_strend = s + (cx->sb_strend - m);
215 cx->sb_m = m = rx->startp[0] + orig;
216 sv_catpvn(dstr, s, m-s);
217 cx->sb_s = rx->endp[0] + orig;
218 { /* Update the pos() information. */
219 SV *sv = cx->sb_targ;
222 if (SvTYPE(sv) < SVt_PVMG)
223 SvUPGRADE(sv, SVt_PVMG);
224 if (!(mg = mg_find(sv, 'g'))) {
225 sv_magic(sv, Nullsv, 'g', Nullch, 0);
226 mg = mg_find(sv, 'g');
233 cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
234 rxres_save(&cx->sb_rxres, rx);
235 RETURNOP(pm->op_pmreplstart);
239 Perl_rxres_save(pTHX_ void **rsp, REGEXP *rx)
244 if (!p || p[1] < rx->nparens) {
245 i = 6 + rx->nparens * 2;
253 *p++ = PTR2UV(RX_MATCH_COPIED(rx) ? rx->subbeg : Nullch);
254 RX_MATCH_COPIED_off(rx);
258 *p++ = PTR2UV(rx->subbeg);
259 *p++ = (UV)rx->sublen;
260 for (i = 0; i <= rx->nparens; ++i) {
261 *p++ = (UV)rx->startp[i];
262 *p++ = (UV)rx->endp[i];
267 Perl_rxres_restore(pTHX_ void **rsp, REGEXP *rx)
272 if (RX_MATCH_COPIED(rx))
273 Safefree(rx->subbeg);
274 RX_MATCH_COPIED_set(rx, *p);
279 rx->subbeg = INT2PTR(char*,*p++);
280 rx->sublen = (I32)(*p++);
281 for (i = 0; i <= rx->nparens; ++i) {
282 rx->startp[i] = (I32)(*p++);
283 rx->endp[i] = (I32)(*p++);
288 Perl_rxres_free(pTHX_ void **rsp)
293 Safefree(INT2PTR(char*,*p));
301 djSP; dMARK; dORIGMARK;
302 register SV *tmpForm = *++MARK;
314 bool chopspace = (strchr(PL_chopset, ' ') != Nullch);
320 STRLEN fudge = SvCUR(tmpForm) * (IN_BYTE ? 1 : 3) + 1;
321 bool item_is_utf = FALSE;
323 if (!SvMAGICAL(tmpForm) || !SvCOMPILED(tmpForm)) {
324 if (SvREADONLY(tmpForm)) {
325 SvREADONLY_off(tmpForm);
326 doparseform(tmpForm);
327 SvREADONLY_on(tmpForm);
330 doparseform(tmpForm);
333 SvPV_force(PL_formtarget, len);
334 t = SvGROW(PL_formtarget, len + fudge + 1); /* XXX SvCUR bad */
336 f = SvPV(tmpForm, len);
337 /* need to jump to the next word */
338 s = f + len + WORD_ALIGN - SvCUR(tmpForm) % WORD_ALIGN;
347 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
348 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
349 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
350 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
351 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
353 case FF_CHECKNL: name = "CHECKNL"; break;
354 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
355 case FF_SPACE: name = "SPACE"; break;
356 case FF_HALFSPACE: name = "HALFSPACE"; break;
357 case FF_ITEM: name = "ITEM"; break;
358 case FF_CHOP: name = "CHOP"; break;
359 case FF_LINEGLOB: name = "LINEGLOB"; break;
360 case FF_NEWLINE: name = "NEWLINE"; break;
361 case FF_MORE: name = "MORE"; break;
362 case FF_LINEMARK: name = "LINEMARK"; break;
363 case FF_END: name = "END"; break;
366 PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg);
368 PerlIO_printf(Perl_debug_log, "%-16s\n", name);
396 if (ckWARN(WARN_SYNTAX))
397 Perl_warner(aTHX_ WARN_SYNTAX, "Not enough format arguments");
402 item = s = SvPV(sv, len);
405 itemsize = sv_len_utf8(sv);
406 if (itemsize != len) {
408 if (itemsize > fieldsize) {
409 itemsize = fieldsize;
410 itembytes = itemsize;
411 sv_pos_u2b(sv, &itembytes, 0);
415 send = chophere = s + itembytes;
425 sv_pos_b2u(sv, &itemsize);
430 if (itemsize > fieldsize)
431 itemsize = fieldsize;
432 send = chophere = s + itemsize;
444 item = s = SvPV(sv, len);
447 itemsize = sv_len_utf8(sv);
448 if (itemsize != len) {
450 if (itemsize <= fieldsize) {
451 send = chophere = s + itemsize;
462 itemsize = fieldsize;
463 itembytes = itemsize;
464 sv_pos_u2b(sv, &itembytes, 0);
465 send = chophere = s + itembytes;
466 while (s < send || (s == send && isSPACE(*s))) {
476 if (strchr(PL_chopset, *s))
481 itemsize = chophere - item;
482 sv_pos_b2u(sv, &itemsize);
489 if (itemsize <= fieldsize) {
490 send = chophere = s + itemsize;
501 itemsize = fieldsize;
502 send = chophere = s + itemsize;
503 while (s < send || (s == send && isSPACE(*s))) {
513 if (strchr(PL_chopset, *s))
518 itemsize = chophere - item;
523 arg = fieldsize - itemsize;
532 arg = fieldsize - itemsize;
546 if (UTF8_IS_CONTINUED(*s)) {
547 switch (UTF8SKIP(s)) {
558 if ( !((*t++ = *s++) & ~31) )
566 int ch = *t++ = *s++;
569 if ( !((*t++ = *s++) & ~31) )
578 while (*s && isSPACE(*s))
585 item = s = SvPV(sv, len);
587 item_is_utf = FALSE; /* XXX is this correct? */
599 SvCUR_set(PL_formtarget, t - SvPVX(PL_formtarget));
600 sv_catpvn(PL_formtarget, item, itemsize);
601 SvGROW(PL_formtarget, SvCUR(PL_formtarget) + fudge + 1);
602 t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
607 /* If the field is marked with ^ and the value is undefined,
610 if ((arg & 512) && !SvOK(sv)) {
618 /* Formats aren't yet marked for locales, so assume "yes". */
620 STORE_NUMERIC_STANDARD_SET_LOCAL();
621 #if defined(USE_LONG_DOUBLE)
623 sprintf(t, "%#*.*" PERL_PRIfldbl,
624 (int) fieldsize, (int) arg & 255, value);
626 sprintf(t, "%*.0" PERL_PRIfldbl, (int) fieldsize, value);
631 (int) fieldsize, (int) arg & 255, value);
634 (int) fieldsize, value);
637 RESTORE_NUMERIC_STANDARD();
644 while (t-- > linemark && *t == ' ') ;
652 if (arg) { /* repeat until fields exhausted? */
654 SvCUR_set(PL_formtarget, t - SvPVX(PL_formtarget));
655 lines += FmLINES(PL_formtarget);
658 if (strnEQ(linemark, linemark - arg, arg))
659 DIE(aTHX_ "Runaway format");
661 FmLINES(PL_formtarget) = lines;
663 RETURNOP(cLISTOP->op_first);
676 while (*s && isSPACE(*s) && s < send)
680 arg = fieldsize - itemsize;
687 if (strnEQ(s," ",3)) {
688 while (s > SvPVX(PL_formtarget) && isSPACE(s[-1]))
699 SvCUR_set(PL_formtarget, t - SvPVX(PL_formtarget));
700 FmLINES(PL_formtarget) += lines;
712 if (PL_stack_base + *PL_markstack_ptr == SP) {
714 if (GIMME_V == G_SCALAR)
715 XPUSHs(sv_2mortal(newSViv(0)));
716 RETURNOP(PL_op->op_next->op_next);
718 PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1;
719 pp_pushmark(); /* push dst */
720 pp_pushmark(); /* push src */
721 ENTER; /* enter outer scope */
724 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
726 ENTER; /* enter inner scope */
729 src = PL_stack_base[*PL_markstack_ptr];
734 if (PL_op->op_type == OP_MAPSTART)
735 pp_pushmark(); /* push top */
736 return ((LOGOP*)PL_op->op_next)->op_other;
741 DIE(aTHX_ "panic: mapstart"); /* uses grepstart */
747 I32 items = (SP - PL_stack_base) - *PL_markstack_ptr; /* how many new items */
753 /* first, move source pointer to the next item in the source list */
754 ++PL_markstack_ptr[-1];
756 /* if there are new items, push them into the destination list */
758 /* might need to make room back there first */
759 if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
760 /* XXX this implementation is very pessimal because the stack
761 * is repeatedly extended for every set of items. Is possible
762 * to do this without any stack extension or copying at all
763 * by maintaining a separate list over which the map iterates
764 * (like foreach does). --gsar */
766 /* everything in the stack after the destination list moves
767 * towards the end the stack by the amount of room needed */
768 shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
770 /* items to shift up (accounting for the moved source pointer) */
771 count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1);
773 /* This optimization is by Ben Tilly and it does
774 * things differently from what Sarathy (gsar)
775 * is describing. The downside of this optimization is
776 * that leaves "holes" (uninitialized and hopefully unused areas)
777 * to the Perl stack, but on the other hand this
778 * shouldn't be a problem. If Sarathy's idea gets
779 * implemented, this optimization should become
780 * irrelevant. --jhi */
782 shift = count; /* Avoid shifting too often --Ben Tilly */
787 PL_markstack_ptr[-1] += shift;
788 *PL_markstack_ptr += shift;
792 /* copy the new items down to the destination list */
793 dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1;
795 *dst-- = SvTEMP(TOPs) ? POPs : sv_mortalcopy(POPs);
797 LEAVE; /* exit inner scope */
800 if (PL_markstack_ptr[-1] > *PL_markstack_ptr) {
803 (void)POPMARK; /* pop top */
804 LEAVE; /* exit outer scope */
805 (void)POPMARK; /* pop src */
806 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
807 (void)POPMARK; /* pop dst */
808 SP = PL_stack_base + POPMARK; /* pop original mark */
809 if (gimme == G_SCALAR) {
813 else if (gimme == G_ARRAY)
820 ENTER; /* enter inner scope */
823 /* set $_ to the new source item */
824 src = PL_stack_base[PL_markstack_ptr[-1]];
828 RETURNOP(cLOGOP->op_other);
834 djSP; dMARK; dORIGMARK;
836 SV **myorigmark = ORIGMARK;
842 OP* nextop = PL_op->op_next;
844 bool hasargs = FALSE;
847 if (gimme != G_ARRAY) {
853 SAVEVPTR(PL_sortcop);
854 if (PL_op->op_flags & OPf_STACKED) {
855 if (PL_op->op_flags & OPf_SPECIAL) {
856 OP *kid = cLISTOP->op_first->op_sibling; /* pass pushmark */
857 kid = kUNOP->op_first; /* pass rv2gv */
858 kid = kUNOP->op_first; /* pass leave */
859 PL_sortcop = kid->op_next;
860 stash = CopSTASH(PL_curcop);
863 cv = sv_2cv(*++MARK, &stash, &gv, 0);
864 if (cv && SvPOK(cv)) {
866 char *proto = SvPV((SV*)cv, n_a);
867 if (proto && strEQ(proto, "$$")) {
871 if (!(cv && CvROOT(cv))) {
872 if (cv && CvXSUB(cv)) {
876 SV *tmpstr = sv_newmortal();
877 gv_efullname3(tmpstr, gv, Nullch);
878 DIE(aTHX_ "Undefined sort subroutine \"%s\" called",
882 DIE(aTHX_ "Undefined subroutine in sort");
887 PL_sortcop = (OP*)cv;
889 PL_sortcop = CvSTART(cv);
890 SAVEVPTR(CvROOT(cv)->op_ppaddr);
891 CvROOT(cv)->op_ppaddr = PL_ppaddr[OP_NULL];
894 PL_curpad = AvARRAY((AV*)AvARRAY(CvPADLIST(cv))[1]);
900 stash = CopSTASH(PL_curcop);
904 while (MARK < SP) { /* This may or may not shift down one here. */
906 if ((*up = *++MARK)) { /* Weed out nulls. */
908 if (!PL_sortcop && !SvPOK(*up)) {
913 (void)sv_2pv(*up, &n_a);
918 max = --up - myorigmark;
923 bool oldcatch = CATCH_GET;
929 PUSHSTACKi(PERLSI_SORT);
930 if (!hasargs && !is_xsub) {
931 if (PL_sortstash != stash || !PL_firstgv || !PL_secondgv) {
932 SAVESPTR(PL_firstgv);
933 SAVESPTR(PL_secondgv);
934 PL_firstgv = gv_fetchpv("a", TRUE, SVt_PV);
935 PL_secondgv = gv_fetchpv("b", TRUE, SVt_PV);
936 PL_sortstash = stash;
939 sv_lock((SV *)PL_firstgv);
940 sv_lock((SV *)PL_secondgv);
942 SAVESPTR(GvSV(PL_firstgv));
943 SAVESPTR(GvSV(PL_secondgv));
946 PUSHBLOCK(cx, CXt_NULL, PL_stack_base);
947 if (!(PL_op->op_flags & OPf_SPECIAL)) {
948 cx->cx_type = CXt_SUB;
949 cx->blk_gimme = G_SCALAR;
952 (void)SvREFCNT_inc(cv); /* in preparation for POPSUB */
954 PL_sortcxix = cxstack_ix;
956 if (hasargs && !is_xsub) {
957 /* This is mostly copied from pp_entersub */
958 AV *av = (AV*)PL_curpad[0];
961 cx->blk_sub.savearray = GvAV(PL_defgv);
962 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
963 #endif /* USE_THREADS */
964 cx->blk_sub.oldcurpad = PL_curpad;
965 cx->blk_sub.argarray = av;
967 qsortsv((myorigmark+1), max,
968 is_xsub ? sortcv_xsub : hasargs ? sortcv_stacked : sortcv);
970 POPBLOCK(cx,PL_curpm);
978 MEXTEND(SP, 20); /* Can't afford stack realloc on signal. */
979 qsortsv(ORIGMARK+1, max,
980 (PL_op->op_private & OPpSORT_NUMERIC)
981 ? ( (PL_op->op_private & OPpSORT_INTEGER)
982 ? ( overloading ? amagic_i_ncmp : sv_i_ncmp)
983 : ( overloading ? amagic_ncmp : sv_ncmp))
984 : ( (PL_op->op_private & OPpLOCALE)
987 : sv_cmp_locale_static)
988 : ( overloading ? amagic_cmp : sv_cmp_static)));
989 if (PL_op->op_private & OPpSORT_REVERSE) {
991 SV **q = ORIGMARK+max;
1001 PL_stack_sp = ORIGMARK + max;
1009 if (GIMME == G_ARRAY)
1011 if (SvTRUEx(PAD_SV(PL_op->op_targ)))
1012 return cLOGOP->op_other;
1021 if (GIMME == G_ARRAY) {
1022 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1026 SV *targ = PAD_SV(PL_op->op_targ);
1029 if (PL_op->op_private & OPpFLIP_LINENUM) {
1031 flip = PL_last_in_gv
1032 && (gp_io = GvIOp(PL_last_in_gv))
1033 && SvIV(sv) == (IV)IoLINES(gp_io);
1038 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
1039 if (PL_op->op_flags & OPf_SPECIAL) {
1047 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1060 if (GIMME == G_ARRAY) {
1066 if (SvGMAGICAL(left))
1068 if (SvGMAGICAL(right))
1071 if (SvNIOKp(left) || !SvPOKp(left) ||
1072 SvNIOKp(right) || !SvPOKp(right) ||
1073 (looks_like_number(left) && *SvPVX(left) != '0' &&
1074 looks_like_number(right) && *SvPVX(right) != '0'))
1076 if (SvNV(left) < IV_MIN || SvNV(right) > IV_MAX)
1077 DIE(aTHX_ "Range iterator outside integer range");
1088 sv = sv_2mortal(newSViv(i++));
1093 SV *final = sv_mortalcopy(right);
1095 char *tmps = SvPV(final, len);
1097 sv = sv_mortalcopy(left);
1099 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
1101 if (strEQ(SvPVX(sv),tmps))
1103 sv = sv_2mortal(newSVsv(sv));
1110 SV *targ = PAD_SV(cUNOP->op_first->op_targ);
1112 if ((PL_op->op_private & OPpFLIP_LINENUM)
1113 ? (PL_last_in_gv && SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv)))
1115 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
1116 sv_catpv(targ, "E0");
1127 S_dopoptolabel(pTHX_ char *label)
1130 register PERL_CONTEXT *cx;
1132 for (i = cxstack_ix; i >= 0; i--) {
1134 switch (CxTYPE(cx)) {
1136 if (ckWARN(WARN_EXITING))
1137 Perl_warner(aTHX_ WARN_EXITING, "Exiting substitution via %s",
1138 PL_op_name[PL_op->op_type]);
1141 if (ckWARN(WARN_EXITING))
1142 Perl_warner(aTHX_ WARN_EXITING, "Exiting subroutine via %s",
1143 PL_op_name[PL_op->op_type]);
1146 if (ckWARN(WARN_EXITING))
1147 Perl_warner(aTHX_ WARN_EXITING, "Exiting format via %s",
1148 PL_op_name[PL_op->op_type]);
1151 if (ckWARN(WARN_EXITING))
1152 Perl_warner(aTHX_ WARN_EXITING, "Exiting eval via %s",
1153 PL_op_name[PL_op->op_type]);
1156 if (ckWARN(WARN_EXITING))
1157 Perl_warner(aTHX_ WARN_EXITING, "Exiting pseudo-block via %s",
1158 PL_op_name[PL_op->op_type]);
1161 if (!cx->blk_loop.label ||
1162 strNE(label, cx->blk_loop.label) ) {
1163 DEBUG_l(Perl_deb(aTHX_ "(Skipping label #%ld %s)\n",
1164 (long)i, cx->blk_loop.label));
1167 DEBUG_l( Perl_deb(aTHX_ "(Found label #%ld %s)\n", (long)i, label));
1175 Perl_dowantarray(pTHX)
1177 I32 gimme = block_gimme();
1178 return (gimme == G_VOID) ? G_SCALAR : gimme;
1182 Perl_block_gimme(pTHX)
1186 cxix = dopoptosub(cxstack_ix);
1190 switch (cxstack[cxix].blk_gimme) {
1198 Perl_croak(aTHX_ "panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
1205 Perl_is_lvalue_sub(pTHX)
1209 cxix = dopoptosub(cxstack_ix);
1210 assert(cxix >= 0); /* We should only be called from inside subs */
1212 if (cxstack[cxix].blk_sub.lval && CvLVALUE(cxstack[cxix].blk_sub.cv))
1213 return cxstack[cxix].blk_sub.lval;
1219 S_dopoptosub(pTHX_ I32 startingblock)
1221 return dopoptosub_at(cxstack, startingblock);
1225 S_dopoptosub_at(pTHX_ PERL_CONTEXT *cxstk, I32 startingblock)
1228 register PERL_CONTEXT *cx;
1229 for (i = startingblock; i >= 0; i--) {
1231 switch (CxTYPE(cx)) {
1237 DEBUG_l( Perl_deb(aTHX_ "(Found sub #%ld)\n", (long)i));
1245 S_dopoptoeval(pTHX_ I32 startingblock)
1248 register PERL_CONTEXT *cx;
1249 for (i = startingblock; i >= 0; i--) {
1251 switch (CxTYPE(cx)) {
1255 DEBUG_l( Perl_deb(aTHX_ "(Found eval #%ld)\n", (long)i));
1263 S_dopoptoloop(pTHX_ I32 startingblock)
1266 register PERL_CONTEXT *cx;
1267 for (i = startingblock; i >= 0; i--) {
1269 switch (CxTYPE(cx)) {
1271 if (ckWARN(WARN_EXITING))
1272 Perl_warner(aTHX_ WARN_EXITING, "Exiting substitution via %s",
1273 PL_op_name[PL_op->op_type]);
1276 if (ckWARN(WARN_EXITING))
1277 Perl_warner(aTHX_ WARN_EXITING, "Exiting subroutine via %s",
1278 PL_op_name[PL_op->op_type]);
1281 if (ckWARN(WARN_EXITING))
1282 Perl_warner(aTHX_ WARN_EXITING, "Exiting format via %s",
1283 PL_op_name[PL_op->op_type]);
1286 if (ckWARN(WARN_EXITING))
1287 Perl_warner(aTHX_ WARN_EXITING, "Exiting eval via %s",
1288 PL_op_name[PL_op->op_type]);
1291 if (ckWARN(WARN_EXITING))
1292 Perl_warner(aTHX_ WARN_EXITING, "Exiting pseudo-block via %s",
1293 PL_op_name[PL_op->op_type]);
1296 DEBUG_l( Perl_deb(aTHX_ "(Found loop #%ld)\n", (long)i));
1304 Perl_dounwind(pTHX_ I32 cxix)
1306 register PERL_CONTEXT *cx;
1309 while (cxstack_ix > cxix) {
1311 cx = &cxstack[cxstack_ix];
1312 DEBUG_l(PerlIO_printf(Perl_debug_log, "Unwinding block %ld, type %s\n",
1313 (long) cxstack_ix, PL_block_type[CxTYPE(cx)]));
1314 /* Note: we don't need to restore the base context info till the end. */
1315 switch (CxTYPE(cx)) {
1318 continue; /* not break */
1340 Perl_qerror(pTHX_ SV *err)
1343 sv_catsv(ERRSV, err);
1345 sv_catsv(PL_errors, err);
1347 Perl_warn(aTHX_ "%"SVf, err);
1352 Perl_die_where(pTHX_ char *message, STRLEN msglen)
1357 register PERL_CONTEXT *cx;
1362 if (PL_in_eval & EVAL_KEEPERR) {
1363 static char prefix[] = "\t(in cleanup) ";
1368 else if (SvCUR(err) >= sizeof(prefix)+msglen-1) {
1371 if (*e != *message || strNE(e,message))
1375 SvGROW(err, SvCUR(err)+sizeof(prefix)+msglen);
1376 sv_catpvn(err, prefix, sizeof(prefix)-1);
1377 sv_catpvn(err, message, msglen);
1378 if (ckWARN(WARN_MISC)) {
1379 STRLEN start = SvCUR(err)-msglen-sizeof(prefix)+1;
1380 Perl_warner(aTHX_ WARN_MISC, SvPVX(err)+start);
1385 sv_setpvn(ERRSV, message, msglen);
1388 message = SvPVx(ERRSV, msglen);
1390 while ((cxix = dopoptoeval(cxstack_ix)) < 0
1391 && PL_curstackinfo->si_prev)
1400 if (cxix < cxstack_ix)
1403 POPBLOCK(cx,PL_curpm);
1404 if (CxTYPE(cx) != CXt_EVAL) {
1405 PerlIO_write(Perl_error_log, "panic: die ", 11);
1406 PerlIO_write(Perl_error_log, message, msglen);
1411 if (gimme == G_SCALAR)
1412 *++newsp = &PL_sv_undef;
1413 PL_stack_sp = newsp;
1417 /* LEAVE could clobber PL_curcop (see save_re_context())
1418 * XXX it might be better to find a way to avoid messing with
1419 * PL_curcop in save_re_context() instead, but this is a more
1420 * minimal fix --GSAR */
1421 PL_curcop = cx->blk_oldcop;
1423 if (optype == OP_REQUIRE) {
1424 char* msg = SvPVx(ERRSV, n_a);
1425 DIE(aTHX_ "%sCompilation failed in require",
1426 *msg ? msg : "Unknown error\n");
1428 return pop_return();
1432 message = SvPVx(ERRSV, msglen);
1435 /* SFIO can really mess with your errno */
1438 PerlIO *serr = Perl_error_log;
1440 PerlIO_write(serr, message, msglen);
1441 (void)PerlIO_flush(serr);
1454 if (SvTRUE(left) != SvTRUE(right))
1466 RETURNOP(cLOGOP->op_other);
1475 RETURNOP(cLOGOP->op_other);
1481 register I32 cxix = dopoptosub(cxstack_ix);
1482 register PERL_CONTEXT *cx;
1483 register PERL_CONTEXT *ccstack = cxstack;
1484 PERL_SI *top_si = PL_curstackinfo;
1495 /* we may be in a higher stacklevel, so dig down deeper */
1496 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1497 top_si = top_si->si_prev;
1498 ccstack = top_si->si_cxstack;
1499 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1502 if (GIMME != G_ARRAY)
1506 if (PL_DBsub && cxix >= 0 &&
1507 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
1511 cxix = dopoptosub_at(ccstack, cxix - 1);
1514 cx = &ccstack[cxix];
1515 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1516 dbcxix = dopoptosub_at(ccstack, cxix - 1);
1517 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
1518 field below is defined for any cx. */
1519 if (PL_DBsub && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
1520 cx = &ccstack[dbcxix];
1523 stashname = CopSTASHPV(cx->blk_oldcop);
1524 if (GIMME != G_ARRAY) {
1526 PUSHs(&PL_sv_undef);
1529 sv_setpv(TARG, stashname);
1536 PUSHs(&PL_sv_undef);
1538 PUSHs(sv_2mortal(newSVpv(stashname, 0)));
1539 PUSHs(sv_2mortal(newSVpv(CopFILE(cx->blk_oldcop), 0)));
1540 PUSHs(sv_2mortal(newSViv((I32)CopLINE(cx->blk_oldcop))));
1543 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1544 /* So is ccstack[dbcxix]. */
1546 gv_efullname3(sv, CvGV(ccstack[cxix].blk_sub.cv), Nullch);
1547 PUSHs(sv_2mortal(sv));
1548 PUSHs(sv_2mortal(newSViv((I32)cx->blk_sub.hasargs)));
1551 PUSHs(sv_2mortal(newSVpvn("(eval)",6)));
1552 PUSHs(sv_2mortal(newSViv(0)));
1554 gimme = (I32)cx->blk_gimme;
1555 if (gimme == G_VOID)
1556 PUSHs(&PL_sv_undef);
1558 PUSHs(sv_2mortal(newSViv(gimme & G_ARRAY)));
1559 if (CxTYPE(cx) == CXt_EVAL) {
1561 if (cx->blk_eval.old_op_type == OP_ENTEREVAL) {
1562 PUSHs(cx->blk_eval.cur_text);
1566 else if (cx->blk_eval.old_namesv) {
1567 PUSHs(sv_2mortal(newSVsv(cx->blk_eval.old_namesv)));
1570 /* eval BLOCK (try blocks have old_namesv == 0) */
1572 PUSHs(&PL_sv_undef);
1573 PUSHs(&PL_sv_undef);
1577 PUSHs(&PL_sv_undef);
1578 PUSHs(&PL_sv_undef);
1580 if (CxTYPE(cx) == CXt_SUB && cx->blk_sub.hasargs
1581 && CopSTASH_eq(PL_curcop, PL_debstash))
1583 AV *ary = cx->blk_sub.argarray;
1584 int off = AvARRAY(ary) - AvALLOC(ary);
1588 PL_dbargs = GvAV(gv_AVadd(tmpgv = gv_fetchpv("DB::args", TRUE,
1591 AvREAL_off(PL_dbargs); /* XXX should be REIFY (see av.h) */
1594 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1595 av_extend(PL_dbargs, AvFILLp(ary) + off);
1596 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1597 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
1599 /* XXX only hints propagated via op_private are currently
1600 * visible (others are not easily accessible, since they
1601 * use the global PL_hints) */
1602 PUSHs(sv_2mortal(newSViv((I32)cx->blk_oldcop->op_private &
1603 HINT_PRIVATE_MASK)));
1606 SV * old_warnings = cx->blk_oldcop->cop_warnings ;
1608 if (old_warnings == pWARN_NONE ||
1609 (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0))
1610 mask = newSVpvn(WARN_NONEstring, WARNsize) ;
1611 else if (old_warnings == pWARN_ALL ||
1612 (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON))
1613 mask = newSVpvn(WARN_ALLstring, WARNsize) ;
1615 mask = newSVsv(old_warnings);
1616 PUSHs(sv_2mortal(mask));
1631 sv_reset(tmps, CopSTASH(PL_curcop));
1643 PL_curcop = (COP*)PL_op;
1644 TAINT_NOT; /* Each statement is presumed innocent */
1645 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
1648 if (PL_op->op_private || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace))
1652 register PERL_CONTEXT *cx;
1653 I32 gimme = G_ARRAY;
1660 DIE(aTHX_ "No DB::DB routine defined");
1662 if (CvDEPTH(cv) >= 1 && !(PL_debug & (1<<30))) /* don't do recursive DB::DB call */
1674 push_return(PL_op->op_next);
1675 PUSHBLOCK(cx, CXt_SUB, SP);
1678 (void)SvREFCNT_inc(cv);
1679 SAVEVPTR(PL_curpad);
1680 PL_curpad = AvARRAY((AV*)*av_fetch(CvPADLIST(cv),1,FALSE));
1681 RETURNOP(CvSTART(cv));
1695 register PERL_CONTEXT *cx;
1696 I32 gimme = GIMME_V;
1698 U32 cxtype = CXt_LOOP;
1707 if (PL_op->op_flags & OPf_SPECIAL) {
1708 svp = &THREADSV(PL_op->op_targ); /* per-thread variable */
1709 SAVEGENERICSV(*svp);
1713 #endif /* USE_THREADS */
1714 if (PL_op->op_targ) {
1715 #ifndef USE_ITHREADS
1716 svp = &PL_curpad[PL_op->op_targ]; /* "my" variable */
1719 SAVEPADSV(PL_op->op_targ);
1720 iterdata = (void*)PL_op->op_targ;
1721 cxtype |= CXp_PADVAR;
1726 svp = &GvSV(gv); /* symbol table variable */
1727 SAVEGENERICSV(*svp);
1730 iterdata = (void*)gv;
1736 PUSHBLOCK(cx, cxtype, SP);
1738 PUSHLOOP(cx, iterdata, MARK);
1740 PUSHLOOP(cx, svp, MARK);
1742 if (PL_op->op_flags & OPf_STACKED) {
1743 cx->blk_loop.iterary = (AV*)SvREFCNT_inc(POPs);
1744 if (SvTYPE(cx->blk_loop.iterary) != SVt_PVAV) {
1746 if (SvNIOKp(sv) || !SvPOKp(sv) ||
1747 SvNIOKp(cx->blk_loop.iterary) || !SvPOKp(cx->blk_loop.iterary) ||
1748 (looks_like_number(sv) && *SvPVX(sv) != '0' &&
1749 looks_like_number((SV*)cx->blk_loop.iterary) &&
1750 *SvPVX(cx->blk_loop.iterary) != '0'))
1752 if (SvNV(sv) < IV_MIN ||
1753 SvNV((SV*)cx->blk_loop.iterary) >= IV_MAX)
1754 DIE(aTHX_ "Range iterator outside integer range");
1755 cx->blk_loop.iterix = SvIV(sv);
1756 cx->blk_loop.itermax = SvIV((SV*)cx->blk_loop.iterary);
1759 cx->blk_loop.iterlval = newSVsv(sv);
1763 cx->blk_loop.iterary = PL_curstack;
1764 AvFILLp(PL_curstack) = SP - PL_stack_base;
1765 cx->blk_loop.iterix = MARK - PL_stack_base;
1774 register PERL_CONTEXT *cx;
1775 I32 gimme = GIMME_V;
1781 PUSHBLOCK(cx, CXt_LOOP, SP);
1782 PUSHLOOP(cx, 0, SP);
1790 register PERL_CONTEXT *cx;
1798 newsp = PL_stack_base + cx->blk_loop.resetsp;
1801 if (gimme == G_VOID)
1803 else if (gimme == G_SCALAR) {
1805 *++newsp = sv_mortalcopy(*SP);
1807 *++newsp = &PL_sv_undef;
1811 *++newsp = sv_mortalcopy(*++mark);
1812 TAINT_NOT; /* Each item is independent */
1818 POPLOOP(cx); /* Stack values are safe: release loop vars ... */
1819 PL_curpm = newpm; /* ... and pop $1 et al */
1831 register PERL_CONTEXT *cx;
1832 bool popsub2 = FALSE;
1833 bool clear_errsv = FALSE;
1840 if (PL_curstackinfo->si_type == PERLSI_SORT) {
1841 if (cxstack_ix == PL_sortcxix
1842 || dopoptosub(cxstack_ix) <= PL_sortcxix)
1844 if (cxstack_ix > PL_sortcxix)
1845 dounwind(PL_sortcxix);
1846 AvARRAY(PL_curstack)[1] = *SP;
1847 PL_stack_sp = PL_stack_base + 1;
1852 cxix = dopoptosub(cxstack_ix);
1854 DIE(aTHX_ "Can't return outside a subroutine");
1855 if (cxix < cxstack_ix)
1859 switch (CxTYPE(cx)) {
1864 if (!(PL_in_eval & EVAL_KEEPERR))
1870 if (optype == OP_REQUIRE &&
1871 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
1873 /* Unassume the success we assumed earlier. */
1874 SV *nsv = cx->blk_eval.old_namesv;
1875 (void)hv_delete(GvHVn(PL_incgv), SvPVX(nsv), SvCUR(nsv), G_DISCARD);
1876 DIE(aTHX_ "%s did not return a true value", SvPVX(nsv));
1883 DIE(aTHX_ "panic: return");
1887 if (gimme == G_SCALAR) {
1890 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
1892 *++newsp = SvREFCNT_inc(*SP);
1897 sv = SvREFCNT_inc(*SP); /* FREETMPS could clobber it */
1899 *++newsp = sv_mortalcopy(sv);
1904 *++newsp = (SvTEMP(*SP)) ? *SP : sv_mortalcopy(*SP);
1907 *++newsp = sv_mortalcopy(*SP);
1910 *++newsp = &PL_sv_undef;
1912 else if (gimme == G_ARRAY) {
1913 while (++MARK <= SP) {
1914 *++newsp = (popsub2 && SvTEMP(*MARK))
1915 ? *MARK : sv_mortalcopy(*MARK);
1916 TAINT_NOT; /* Each item is independent */
1919 PL_stack_sp = newsp;
1921 /* Stack values are safe: */
1923 POPSUB(cx,sv); /* release CV and @_ ... */
1927 PL_curpm = newpm; /* ... and pop $1 et al */
1933 return pop_return();
1940 register PERL_CONTEXT *cx;
1950 if (PL_op->op_flags & OPf_SPECIAL) {
1951 cxix = dopoptoloop(cxstack_ix);
1953 DIE(aTHX_ "Can't \"last\" outside a loop block");
1956 cxix = dopoptolabel(cPVOP->op_pv);
1958 DIE(aTHX_ "Label not found for \"last %s\"", cPVOP->op_pv);
1960 if (cxix < cxstack_ix)
1965 switch (CxTYPE(cx)) {
1968 newsp = PL_stack_base + cx->blk_loop.resetsp;
1969 nextop = cx->blk_loop.last_op->op_next;
1973 nextop = pop_return();
1977 nextop = pop_return();
1981 nextop = pop_return();
1984 DIE(aTHX_ "panic: last");
1988 if (gimme == G_SCALAR) {
1990 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
1991 ? *SP : sv_mortalcopy(*SP);
1993 *++newsp = &PL_sv_undef;
1995 else if (gimme == G_ARRAY) {
1996 while (++MARK <= SP) {
1997 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
1998 ? *MARK : sv_mortalcopy(*MARK);
1999 TAINT_NOT; /* Each item is independent */
2005 /* Stack values are safe: */
2008 POPLOOP(cx); /* release loop vars ... */
2012 POPSUB(cx,sv); /* release CV and @_ ... */
2015 PL_curpm = newpm; /* ... and pop $1 et al */
2025 register PERL_CONTEXT *cx;
2028 if (PL_op->op_flags & OPf_SPECIAL) {
2029 cxix = dopoptoloop(cxstack_ix);
2031 DIE(aTHX_ "Can't \"next\" outside a loop block");
2034 cxix = dopoptolabel(cPVOP->op_pv);
2036 DIE(aTHX_ "Label not found for \"next %s\"", cPVOP->op_pv);
2038 if (cxix < cxstack_ix)
2041 /* clear off anything above the scope we're re-entering, but
2042 * save the rest until after a possible continue block */
2043 inner = PL_scopestack_ix;
2045 if (PL_scopestack_ix < inner)
2046 leave_scope(PL_scopestack[PL_scopestack_ix]);
2047 return cx->blk_loop.next_op;
2053 register PERL_CONTEXT *cx;
2056 if (PL_op->op_flags & OPf_SPECIAL) {
2057 cxix = dopoptoloop(cxstack_ix);
2059 DIE(aTHX_ "Can't \"redo\" outside a loop block");
2062 cxix = dopoptolabel(cPVOP->op_pv);
2064 DIE(aTHX_ "Label not found for \"redo %s\"", cPVOP->op_pv);
2066 if (cxix < cxstack_ix)
2070 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2071 LEAVE_SCOPE(oldsave);
2072 return cx->blk_loop.redo_op;
2076 S_dofindlabel(pTHX_ OP *o, char *label, OP **opstack, OP **oplimit)
2080 static char too_deep[] = "Target of goto is too deeply nested";
2083 Perl_croak(aTHX_ too_deep);
2084 if (o->op_type == OP_LEAVE ||
2085 o->op_type == OP_SCOPE ||
2086 o->op_type == OP_LEAVELOOP ||
2087 o->op_type == OP_LEAVETRY)
2089 *ops++ = cUNOPo->op_first;
2091 Perl_croak(aTHX_ too_deep);
2094 if (o->op_flags & OPf_KIDS) {
2095 /* First try all the kids at this level, since that's likeliest. */
2096 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2097 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
2098 kCOP->cop_label && strEQ(kCOP->cop_label, label))
2101 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2102 if (kid == PL_lastgotoprobe)
2104 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
2106 (ops[-1]->op_type != OP_NEXTSTATE &&
2107 ops[-1]->op_type != OP_DBSTATE)))
2109 if ((o = dofindlabel(kid, label, ops, oplimit)))
2128 register PERL_CONTEXT *cx;
2129 #define GOTO_DEPTH 64
2130 OP *enterops[GOTO_DEPTH];
2132 int do_dump = (PL_op->op_type == OP_DUMP);
2133 static char must_have_label[] = "goto must have label";
2136 if (PL_op->op_flags & OPf_STACKED) {
2140 /* This egregious kludge implements goto &subroutine */
2141 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
2143 register PERL_CONTEXT *cx;
2144 CV* cv = (CV*)SvRV(sv);
2150 if (!CvROOT(cv) && !CvXSUB(cv)) {
2155 /* autoloaded stub? */
2156 if (cv != GvCV(gv) && (cv = GvCV(gv)))
2158 autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv),
2159 GvNAMELEN(gv), FALSE);
2160 if (autogv && (cv = GvCV(autogv)))
2162 tmpstr = sv_newmortal();
2163 gv_efullname3(tmpstr, gv, Nullch);
2164 DIE(aTHX_ "Goto undefined subroutine &%s",SvPVX(tmpstr));
2166 DIE(aTHX_ "Goto undefined subroutine");
2169 /* First do some returnish stuff. */
2170 cxix = dopoptosub(cxstack_ix);
2172 DIE(aTHX_ "Can't goto subroutine outside a subroutine");
2173 if (cxix < cxstack_ix)
2176 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_ENTEREVAL)
2177 DIE(aTHX_ "Can't goto subroutine from an eval-string");
2179 if (CxTYPE(cx) == CXt_SUB && cx->blk_sub.hasargs) {
2180 /* put @_ back onto stack */
2181 AV* av = cx->blk_sub.argarray;
2183 items = AvFILLp(av) + 1;
2185 EXTEND(PL_stack_sp, items); /* @_ could have been extended. */
2186 Copy(AvARRAY(av), PL_stack_sp, items, SV*);
2187 PL_stack_sp += items;
2189 SvREFCNT_dec(GvAV(PL_defgv));
2190 GvAV(PL_defgv) = cx->blk_sub.savearray;
2191 #endif /* USE_THREADS */
2192 /* abandon @_ if it got reified */
2194 (void)sv_2mortal((SV*)av); /* delay until return */
2196 av_extend(av, items-1);
2197 AvFLAGS(av) = AVf_REIFY;
2198 PL_curpad[0] = (SV*)(cx->blk_sub.argarray = av);
2201 else if (CvXSUB(cv)) { /* put GvAV(defgv) back onto stack */
2204 av = (AV*)PL_curpad[0];
2206 av = GvAV(PL_defgv);
2208 items = AvFILLp(av) + 1;
2210 EXTEND(PL_stack_sp, items); /* @_ could have been extended. */
2211 Copy(AvARRAY(av), PL_stack_sp, items, SV*);
2212 PL_stack_sp += items;
2214 if (CxTYPE(cx) == CXt_SUB &&
2215 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
2216 SvREFCNT_dec(cx->blk_sub.cv);
2217 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2218 LEAVE_SCOPE(oldsave);
2220 /* Now do some callish stuff. */
2223 #ifdef PERL_XSUB_OLDSTYLE
2224 if (CvOLDSTYLE(cv)) {
2225 I32 (*fp3)(int,int,int);
2230 fp3 = (I32(*)(int,int,int))CvXSUB(cv);
2231 items = (*fp3)(CvXSUBANY(cv).any_i32,
2232 mark - PL_stack_base + 1,
2234 SP = PL_stack_base + items;
2237 #endif /* PERL_XSUB_OLDSTYLE */
2242 PL_stack_sp--; /* There is no cv arg. */
2243 /* Push a mark for the start of arglist */
2245 (void)(*CvXSUB(cv))(aTHXo_ cv);
2246 /* Pop the current context like a decent sub should */
2247 POPBLOCK(cx, PL_curpm);
2248 /* Do _not_ use PUTBACK, keep the XSUB's return stack! */
2251 return pop_return();
2254 AV* padlist = CvPADLIST(cv);
2255 SV** svp = AvARRAY(padlist);
2256 if (CxTYPE(cx) == CXt_EVAL) {
2257 PL_in_eval = cx->blk_eval.old_in_eval;
2258 PL_eval_root = cx->blk_eval.old_eval_root;
2259 cx->cx_type = CXt_SUB;
2260 cx->blk_sub.hasargs = 0;
2262 cx->blk_sub.cv = cv;
2263 cx->blk_sub.olddepth = CvDEPTH(cv);
2265 if (CvDEPTH(cv) < 2)
2266 (void)SvREFCNT_inc(cv);
2267 else { /* save temporaries on recursion? */
2268 if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION))
2269 sub_crush_depth(cv);
2270 if (CvDEPTH(cv) > AvFILLp(padlist)) {
2271 AV *newpad = newAV();
2272 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
2273 I32 ix = AvFILLp((AV*)svp[1]);
2274 I32 names_fill = AvFILLp((AV*)svp[0]);
2275 svp = AvARRAY(svp[0]);
2276 for ( ;ix > 0; ix--) {
2277 if (names_fill >= ix && svp[ix] != &PL_sv_undef) {
2278 char *name = SvPVX(svp[ix]);
2279 if ((SvFLAGS(svp[ix]) & SVf_FAKE)
2282 /* outer lexical or anon code */
2283 av_store(newpad, ix,
2284 SvREFCNT_inc(oldpad[ix]) );
2286 else { /* our own lexical */
2288 av_store(newpad, ix, sv = (SV*)newAV());
2289 else if (*name == '%')
2290 av_store(newpad, ix, sv = (SV*)newHV());
2292 av_store(newpad, ix, sv = NEWSV(0,0));
2296 else if (IS_PADGV(oldpad[ix]) || IS_PADCONST(oldpad[ix])) {
2297 av_store(newpad, ix, sv = SvREFCNT_inc(oldpad[ix]));
2300 av_store(newpad, ix, sv = NEWSV(0,0));
2304 if (cx->blk_sub.hasargs) {
2307 av_store(newpad, 0, (SV*)av);
2308 AvFLAGS(av) = AVf_REIFY;
2310 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
2311 AvFILLp(padlist) = CvDEPTH(cv);
2312 svp = AvARRAY(padlist);
2316 if (!cx->blk_sub.hasargs) {
2317 AV* av = (AV*)PL_curpad[0];
2319 items = AvFILLp(av) + 1;
2321 /* Mark is at the end of the stack. */
2323 Copy(AvARRAY(av), SP + 1, items, SV*);
2328 #endif /* USE_THREADS */
2329 SAVEVPTR(PL_curpad);
2330 PL_curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
2332 if (cx->blk_sub.hasargs)
2333 #endif /* USE_THREADS */
2335 AV* av = (AV*)PL_curpad[0];
2339 cx->blk_sub.savearray = GvAV(PL_defgv);
2340 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
2341 #endif /* USE_THREADS */
2342 cx->blk_sub.oldcurpad = PL_curpad;
2343 cx->blk_sub.argarray = av;
2346 if (items >= AvMAX(av) + 1) {
2348 if (AvARRAY(av) != ary) {
2349 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2350 SvPVX(av) = (char*)ary;
2352 if (items >= AvMAX(av) + 1) {
2353 AvMAX(av) = items - 1;
2354 Renew(ary,items+1,SV*);
2356 SvPVX(av) = (char*)ary;
2359 Copy(mark,AvARRAY(av),items,SV*);
2360 AvFILLp(av) = items - 1;
2361 assert(!AvREAL(av));
2368 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
2370 * We do not care about using sv to call CV;
2371 * it's for informational purposes only.
2373 SV *sv = GvSV(PL_DBsub);
2376 if (PERLDB_SUB_NN) {
2377 SvIVX(sv) = PTR2IV(cv); /* Already upgraded, saved */
2380 gv_efullname3(sv, CvGV(cv), Nullch);
2383 && (gotocv = get_cv("DB::goto", FALSE)) ) {
2384 PUSHMARK( PL_stack_sp );
2385 call_sv((SV*)gotocv, G_SCALAR | G_NODEBUG);
2389 RETURNOP(CvSTART(cv));
2393 label = SvPV(sv,n_a);
2394 if (!(do_dump || *label))
2395 DIE(aTHX_ must_have_label);
2398 else if (PL_op->op_flags & OPf_SPECIAL) {
2400 DIE(aTHX_ must_have_label);
2403 label = cPVOP->op_pv;
2405 if (label && *label) {
2410 PL_lastgotoprobe = 0;
2412 for (ix = cxstack_ix; ix >= 0; ix--) {
2414 switch (CxTYPE(cx)) {
2416 gotoprobe = PL_eval_root; /* XXX not good for nested eval */
2419 gotoprobe = cx->blk_oldcop->op_sibling;
2425 gotoprobe = cx->blk_oldcop->op_sibling;
2427 gotoprobe = PL_main_root;
2430 if (CvDEPTH(cx->blk_sub.cv)) {
2431 gotoprobe = CvROOT(cx->blk_sub.cv);
2437 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
2440 DIE(aTHX_ "panic: goto");
2441 gotoprobe = PL_main_root;
2445 retop = dofindlabel(gotoprobe, label,
2446 enterops, enterops + GOTO_DEPTH);
2450 PL_lastgotoprobe = gotoprobe;
2453 DIE(aTHX_ "Can't find label %s", label);
2455 /* pop unwanted frames */
2457 if (ix < cxstack_ix) {
2464 oldsave = PL_scopestack[PL_scopestack_ix];
2465 LEAVE_SCOPE(oldsave);
2468 /* push wanted frames */
2470 if (*enterops && enterops[1]) {
2472 for (ix = 1; enterops[ix]; ix++) {
2473 PL_op = enterops[ix];
2474 /* Eventually we may want to stack the needed arguments
2475 * for each op. For now, we punt on the hard ones. */
2476 if (PL_op->op_type == OP_ENTERITER)
2477 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2478 CALL_FPTR(PL_op->op_ppaddr)(aTHX);
2486 if (!retop) retop = PL_main_start;
2488 PL_restartop = retop;
2489 PL_do_undump = TRUE;
2493 PL_restartop = 0; /* hmm, must be GNU unexec().. */
2494 PL_do_undump = FALSE;
2510 if (anum == 1 && (PL_op->op_private & OPpEXIT_VMSISH))
2514 PL_exit_flags |= PERL_EXIT_EXPECTED;
2516 PUSHs(&PL_sv_undef);
2524 NV value = SvNVx(GvSV(cCOP->cop_gv));
2525 register I32 match = I_32(value);
2528 if (((NV)match) > value)
2529 --match; /* was fractional--truncate other way */
2531 match -= cCOP->uop.scop.scop_offset;
2534 else if (match > cCOP->uop.scop.scop_max)
2535 match = cCOP->uop.scop.scop_max;
2536 PL_op = cCOP->uop.scop.scop_next[match];
2546 PL_op = PL_op->op_next; /* can't assume anything */
2549 match = *(SvPVx(GvSV(cCOP->cop_gv), n_a)) & 255;
2550 match -= cCOP->uop.scop.scop_offset;
2553 else if (match > cCOP->uop.scop.scop_max)
2554 match = cCOP->uop.scop.scop_max;
2555 PL_op = cCOP->uop.scop.scop_next[match];
2564 S_save_lines(pTHX_ AV *array, SV *sv)
2566 register char *s = SvPVX(sv);
2567 register char *send = SvPVX(sv) + SvCUR(sv);
2569 register I32 line = 1;
2571 while (s && s < send) {
2572 SV *tmpstr = NEWSV(85,0);
2574 sv_upgrade(tmpstr, SVt_PVMG);
2575 t = strchr(s, '\n');
2581 sv_setpvn(tmpstr, s, t - s);
2582 av_store(array, line++, tmpstr);
2587 #ifdef PERL_FLEXIBLE_EXCEPTIONS
2589 S_docatch_body(pTHX_ va_list args)
2591 return docatch_body();
2596 S_docatch_body(pTHX)
2603 S_docatch(pTHX_ OP *o)
2607 volatile PERL_SI *cursi = PL_curstackinfo;
2611 assert(CATCH_GET == TRUE);
2614 #ifdef PERL_FLEXIBLE_EXCEPTIONS
2616 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_docatch_body));
2622 #ifndef PERL_FLEXIBLE_EXCEPTIONS
2628 if (PL_restartop && cursi == PL_curstackinfo) {
2629 PL_op = PL_restartop;
2646 Perl_sv_compile_2op(pTHX_ SV *sv, OP** startop, char *code, AV** avp)
2647 /* sv Text to convert to OP tree. */
2648 /* startop op_free() this to undo. */
2649 /* code Short string id of the caller. */
2651 dSP; /* Make POPBLOCK work. */
2654 I32 gimme = 0; /* SUSPECT - INITIALZE TO WHAT? NI-S */
2658 char tbuf[TYPE_DIGITS(long) + 12 + 10];
2659 char *tmpbuf = tbuf;
2665 /* switch to eval mode */
2667 if (PL_curcop == &PL_compiling) {
2668 SAVECOPSTASH_FREE(&PL_compiling);
2669 CopSTASH_set(&PL_compiling, PL_curstash);
2671 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
2672 SV *sv = sv_newmortal();
2673 Perl_sv_setpvf(aTHX_ sv, "_<(%.10seval %lu)[%s:%"IVdf"]",
2674 code, (unsigned long)++PL_evalseq,
2675 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
2679 sprintf(tmpbuf, "_<(%.10s_eval %lu)", code, (unsigned long)++PL_evalseq);
2680 SAVECOPFILE_FREE(&PL_compiling);
2681 CopFILE_set(&PL_compiling, tmpbuf+2);
2682 SAVECOPLINE(&PL_compiling);
2683 CopLINE_set(&PL_compiling, 1);
2684 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2685 deleting the eval's FILEGV from the stash before gv_check() runs
2686 (i.e. before run-time proper). To work around the coredump that
2687 ensues, we always turn GvMULTI_on for any globals that were
2688 introduced within evals. See force_ident(). GSAR 96-10-12 */
2689 safestr = savepv(tmpbuf);
2690 SAVEDELETE(PL_defstash, safestr, strlen(safestr));
2692 #ifdef OP_IN_REGISTER
2700 PL_op->op_type = OP_ENTEREVAL;
2701 PL_op->op_flags = 0; /* Avoid uninit warning. */
2702 PUSHBLOCK(cx, CXt_EVAL|(PL_curcop == &PL_compiling ? 0 : CXp_REAL), SP);
2703 PUSHEVAL(cx, 0, Nullgv);
2704 rop = doeval(G_SCALAR, startop);
2705 POPBLOCK(cx,PL_curpm);
2708 (*startop)->op_type = OP_NULL;
2709 (*startop)->op_ppaddr = PL_ppaddr[OP_NULL];
2711 *avp = (AV*)SvREFCNT_inc(PL_comppad);
2713 if (PL_curcop == &PL_compiling)
2714 PL_compiling.op_private = PL_hints;
2715 #ifdef OP_IN_REGISTER
2721 /* With USE_THREADS, eval_owner must be held on entry to doeval */
2723 S_doeval(pTHX_ int gimme, OP** startop)
2731 PL_in_eval = ((saveop && saveop->op_type == OP_REQUIRE)
2732 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
2737 /* set up a scratch pad */
2740 SAVEVPTR(PL_curpad);
2741 SAVESPTR(PL_comppad);
2742 SAVESPTR(PL_comppad_name);
2743 SAVEI32(PL_comppad_name_fill);
2744 SAVEI32(PL_min_intro_pending);
2745 SAVEI32(PL_max_intro_pending);
2748 for (i = cxstack_ix - 1; i >= 0; i--) {
2749 PERL_CONTEXT *cx = &cxstack[i];
2750 if (CxTYPE(cx) == CXt_EVAL)
2752 else if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
2753 caller = cx->blk_sub.cv;
2758 SAVESPTR(PL_compcv);
2759 PL_compcv = (CV*)NEWSV(1104,0);
2760 sv_upgrade((SV *)PL_compcv, SVt_PVCV);
2761 CvEVAL_on(PL_compcv);
2763 CvOWNER(PL_compcv) = 0;
2764 New(666, CvMUTEXP(PL_compcv), 1, perl_mutex);
2765 MUTEX_INIT(CvMUTEXP(PL_compcv));
2766 #endif /* USE_THREADS */
2768 PL_comppad = newAV();
2769 av_push(PL_comppad, Nullsv);
2770 PL_curpad = AvARRAY(PL_comppad);
2771 PL_comppad_name = newAV();
2772 PL_comppad_name_fill = 0;
2773 PL_min_intro_pending = 0;
2776 av_store(PL_comppad_name, 0, newSVpvn("@_", 2));
2777 PL_curpad[0] = (SV*)newAV();
2778 SvPADMY_on(PL_curpad[0]); /* XXX Needed? */
2779 #endif /* USE_THREADS */
2781 comppadlist = newAV();
2782 AvREAL_off(comppadlist);
2783 av_store(comppadlist, 0, (SV*)PL_comppad_name);
2784 av_store(comppadlist, 1, (SV*)PL_comppad);
2785 CvPADLIST(PL_compcv) = comppadlist;
2788 (saveop->op_type != OP_REQUIRE && saveop->op_type != OP_DOFILE))
2790 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc(caller);
2793 SAVEFREESV(PL_compcv);
2795 /* make sure we compile in the right package */
2797 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
2798 SAVESPTR(PL_curstash);
2799 PL_curstash = CopSTASH(PL_curcop);
2801 SAVESPTR(PL_beginav);
2802 PL_beginav = newAV();
2803 SAVEFREESV(PL_beginav);
2804 SAVEI32(PL_error_count);
2806 /* try to compile it */
2808 PL_eval_root = Nullop;
2810 PL_curcop = &PL_compiling;
2811 PL_curcop->cop_arybase = 0;
2812 SvREFCNT_dec(PL_rs);
2813 PL_rs = newSVpvn("\n", 1);
2814 if (saveop && saveop->op_flags & OPf_SPECIAL)
2815 PL_in_eval |= EVAL_KEEPERR;
2818 if (yyparse() || PL_error_count || !PL_eval_root) {
2822 I32 optype = 0; /* Might be reset by POPEVAL. */
2827 op_free(PL_eval_root);
2828 PL_eval_root = Nullop;
2830 SP = PL_stack_base + POPMARK; /* pop original mark */
2832 POPBLOCK(cx,PL_curpm);
2838 if (optype == OP_REQUIRE) {
2839 char* msg = SvPVx(ERRSV, n_a);
2840 DIE(aTHX_ "%sCompilation failed in require",
2841 *msg ? msg : "Unknown error\n");
2844 char* msg = SvPVx(ERRSV, n_a);
2846 POPBLOCK(cx,PL_curpm);
2848 Perl_croak(aTHX_ "%sCompilation failed in regexp",
2849 (*msg ? msg : "Unknown error\n"));
2851 SvREFCNT_dec(PL_rs);
2852 PL_rs = SvREFCNT_inc(PL_nrs);
2854 MUTEX_LOCK(&PL_eval_mutex);
2856 COND_SIGNAL(&PL_eval_cond);
2857 MUTEX_UNLOCK(&PL_eval_mutex);
2858 #endif /* USE_THREADS */
2861 SvREFCNT_dec(PL_rs);
2862 PL_rs = SvREFCNT_inc(PL_nrs);
2863 CopLINE_set(&PL_compiling, 0);
2865 *startop = PL_eval_root;
2866 SvREFCNT_dec(CvOUTSIDE(PL_compcv));
2867 CvOUTSIDE(PL_compcv) = Nullcv;
2869 SAVEFREEOP(PL_eval_root);
2871 scalarvoid(PL_eval_root);
2872 else if (gimme & G_ARRAY)
2875 scalar(PL_eval_root);
2877 DEBUG_x(dump_eval());
2879 /* Register with debugger: */
2880 if (PERLDB_INTER && saveop->op_type == OP_REQUIRE) {
2881 CV *cv = get_cv("DB::postponed", FALSE);
2885 XPUSHs((SV*)CopFILEGV(&PL_compiling));
2887 call_sv((SV*)cv, G_DISCARD);
2891 /* compiled okay, so do it */
2893 CvDEPTH(PL_compcv) = 1;
2894 SP = PL_stack_base + POPMARK; /* pop original mark */
2895 PL_op = saveop; /* The caller may need it. */
2896 PL_lex_state = LEX_NOTPARSING; /* $^S needs this. */
2898 MUTEX_LOCK(&PL_eval_mutex);
2900 COND_SIGNAL(&PL_eval_cond);
2901 MUTEX_UNLOCK(&PL_eval_mutex);
2902 #endif /* USE_THREADS */
2904 RETURNOP(PL_eval_start);
2908 S_doopen_pmc(pTHX_ const char *name, const char *mode)
2910 STRLEN namelen = strlen(name);
2913 if (namelen > 3 && strEQ(name + namelen - 3, ".pm")) {
2914 SV *pmcsv = Perl_newSVpvf(aTHX_ "%s%c", name, 'c');
2915 char *pmc = SvPV_nolen(pmcsv);
2918 if (PerlLIO_stat(pmc, &pmcstat) < 0) {
2919 fp = PerlIO_open(name, mode);
2922 if (PerlLIO_stat(name, &pmstat) < 0 ||
2923 pmstat.st_mtime < pmcstat.st_mtime)
2925 fp = PerlIO_open(pmc, mode);
2928 fp = PerlIO_open(name, mode);
2931 SvREFCNT_dec(pmcsv);
2934 fp = PerlIO_open(name, mode);
2942 register PERL_CONTEXT *cx;
2947 SV *namesv = Nullsv;
2949 I32 gimme = G_SCALAR;
2950 PerlIO *tryrsfp = 0;
2952 int filter_has_file = 0;
2953 GV *filter_child_proc = 0;
2954 SV *filter_state = 0;
2959 if (SvPOK(sv) && SvNOK(sv)) { /* require v5.6.1 */
2960 UV rev = 0, ver = 0, sver = 0;
2962 U8 *s = (U8*)SvPVX(sv);
2963 U8 *end = (U8*)SvPVX(sv) + SvCUR(sv);
2965 rev = utf8_to_uv(s, end - s, &len, 0);
2968 ver = utf8_to_uv(s, end - s, &len, 0);
2971 sver = utf8_to_uv(s, end - s, &len, 0);
2974 if (PERL_REVISION < rev
2975 || (PERL_REVISION == rev
2976 && (PERL_VERSION < ver
2977 || (PERL_VERSION == ver
2978 && PERL_SUBVERSION < sver))))
2980 DIE(aTHX_ "Perl v%"UVuf".%"UVuf".%"UVuf" required--this is only "
2981 "v%d.%d.%d, stopped", rev, ver, sver, PERL_REVISION,
2982 PERL_VERSION, PERL_SUBVERSION);
2986 else if (!SvPOKp(sv)) { /* require 5.005_03 */
2987 if ((NV)PERL_REVISION + ((NV)PERL_VERSION/(NV)1000)
2988 + ((NV)PERL_SUBVERSION/(NV)1000000)
2989 + 0.00000099 < SvNV(sv))
2993 NV nver = (nrev - rev) * 1000;
2994 UV ver = (UV)(nver + 0.0009);
2995 NV nsver = (nver - ver) * 1000;
2996 UV sver = (UV)(nsver + 0.0009);
2998 /* help out with the "use 5.6" confusion */
2999 if (sver == 0 && (rev > 5 || (rev == 5 && ver >= 100))) {
3000 DIE(aTHX_ "Perl v%"UVuf".%"UVuf".%"UVuf" required--"
3001 "this is only v%d.%d.%d, stopped"
3002 " (did you mean v%"UVuf".%"UVuf".0?)",
3003 rev, ver, sver, PERL_REVISION, PERL_VERSION,
3004 PERL_SUBVERSION, rev, ver/100);
3007 DIE(aTHX_ "Perl v%"UVuf".%"UVuf".%"UVuf" required--"
3008 "this is only v%d.%d.%d, stopped",
3009 rev, ver, sver, PERL_REVISION, PERL_VERSION,
3016 name = SvPV(sv, len);
3017 if (!(name && len > 0 && *name))
3018 DIE(aTHX_ "Null filename used");
3019 TAINT_PROPER("require");
3020 if (PL_op->op_type == OP_REQUIRE &&
3021 (svp = hv_fetch(GvHVn(PL_incgv), name, len, 0)) &&
3022 *svp != &PL_sv_undef)
3025 /* prepare to compile file */
3027 #ifdef MACOS_TRADITIONAL
3028 if (PERL_FILE_IS_ABSOLUTE(name)
3029 || (*name == ':' && name[1] != ':' && strchr(name+2, ':')))
3032 tryrsfp = doopen_pmc(name,PERL_SCRIPT_MODE);
3033 /* We consider paths of the form :a:b ambiguous and interpret them first
3034 as global then as local
3036 if (!tryrsfp && *name == ':' && name[1] != ':' && strchr(name+2, ':'))
3042 if (PERL_FILE_IS_ABSOLUTE(name)
3043 || (*name == '.' && (name[1] == '/' ||
3044 (name[1] == '.' && name[2] == '/'))))
3047 tryrsfp = doopen_pmc(name,PERL_SCRIPT_MODE);
3051 AV *ar = GvAVn(PL_incgv);
3055 if ((unixname = tounixspec(name, Nullch)) != Nullch)
3058 namesv = NEWSV(806, 0);
3059 for (i = 0; i <= AvFILL(ar); i++) {
3060 SV *dirsv = *av_fetch(ar, i, TRUE);
3066 if (SvTYPE(SvRV(loader)) == SVt_PVAV) {
3067 loader = *av_fetch((AV *)SvRV(loader), 0, TRUE);
3070 Perl_sv_setpvf(aTHX_ namesv, "/loader/0x%"UVxf"/%s",
3071 PTR2UV(SvANY(loader)), name);
3072 tryname = SvPVX(namesv);
3083 count = call_sv(loader, G_ARRAY);
3093 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVGV) {
3097 if (SvTYPE(arg) == SVt_PVGV) {
3098 IO *io = GvIO((GV *)arg);
3103 tryrsfp = IoIFP(io);
3104 if (IoTYPE(io) == IoTYPE_PIPE) {
3105 /* reading from a child process doesn't
3106 nest -- when returning from reading
3107 the inner module, the outer one is
3108 unreadable (closed?) I've tried to
3109 save the gv to manage the lifespan of
3110 the pipe, but this didn't help. XXX */
3111 filter_child_proc = (GV *)arg;
3112 (void)SvREFCNT_inc(filter_child_proc);
3115 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
3116 PerlIO_close(IoOFP(io));
3128 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVCV) {
3130 (void)SvREFCNT_inc(filter_sub);
3133 filter_state = SP[i];
3134 (void)SvREFCNT_inc(filter_state);
3138 tryrsfp = PerlIO_open("/dev/null",
3152 filter_has_file = 0;
3153 if (filter_child_proc) {
3154 SvREFCNT_dec(filter_child_proc);
3155 filter_child_proc = 0;
3158 SvREFCNT_dec(filter_state);
3162 SvREFCNT_dec(filter_sub);
3167 char *dir = SvPVx(dirsv, n_a);
3168 #ifdef MACOS_TRADITIONAL
3170 Perl_sv_setpvf(aTHX_ namesv, "%s%s", MacPerl_CanonDir(dir, buf), name+(name[0] == ':'));
3174 if ((unixdir = tounixpath(dir, Nullch)) == Nullch)
3176 sv_setpv(namesv, unixdir);
3177 sv_catpv(namesv, unixname);
3179 Perl_sv_setpvf(aTHX_ namesv, "%s/%s", dir, name);
3182 TAINT_PROPER("require");
3183 tryname = SvPVX(namesv);
3184 #ifdef MACOS_TRADITIONAL
3186 /* Convert slashes in the name part, but not the directory part, to colons */
3188 for (colon = tryname+strlen(dir); colon = strchr(colon, '/'); )
3192 tryrsfp = doopen_pmc(tryname, PERL_SCRIPT_MODE);
3194 if (tryname[0] == '.' && tryname[1] == '/')
3202 SAVECOPFILE_FREE(&PL_compiling);
3203 CopFILE_set(&PL_compiling, tryrsfp ? tryname : name);
3204 SvREFCNT_dec(namesv);
3206 if (PL_op->op_type == OP_REQUIRE) {
3207 char *msgstr = name;
3208 if (namesv) { /* did we lookup @INC? */
3209 SV *msg = sv_2mortal(newSVpv(msgstr,0));
3210 SV *dirmsgsv = NEWSV(0, 0);
3211 AV *ar = GvAVn(PL_incgv);
3213 sv_catpvn(msg, " in @INC", 8);
3214 if (instr(SvPVX(msg), ".h "))
3215 sv_catpv(msg, " (change .h to .ph maybe?)");
3216 if (instr(SvPVX(msg), ".ph "))
3217 sv_catpv(msg, " (did you run h2ph?)");
3218 sv_catpv(msg, " (@INC contains:");
3219 for (i = 0; i <= AvFILL(ar); i++) {
3220 char *dir = SvPVx(*av_fetch(ar, i, TRUE), n_a);
3221 Perl_sv_setpvf(aTHX_ dirmsgsv, " %s", dir);
3222 sv_catsv(msg, dirmsgsv);
3224 sv_catpvn(msg, ")", 1);
3225 SvREFCNT_dec(dirmsgsv);
3226 msgstr = SvPV_nolen(msg);
3228 DIE(aTHX_ "Can't locate %s", msgstr);
3234 SETERRNO(0, SS$_NORMAL);
3236 /* Assume success here to prevent recursive requirement. */
3237 (void)hv_store(GvHVn(PL_incgv), name, strlen(name),
3238 newSVpv(CopFILE(&PL_compiling), 0), 0 );
3242 lex_start(sv_2mortal(newSVpvn("",0)));
3243 SAVEGENERICSV(PL_rsfp_filters);
3244 PL_rsfp_filters = Nullav;
3249 SAVESPTR(PL_compiling.cop_warnings);
3250 if (PL_dowarn & G_WARN_ALL_ON)
3251 PL_compiling.cop_warnings = pWARN_ALL ;
3252 else if (PL_dowarn & G_WARN_ALL_OFF)
3253 PL_compiling.cop_warnings = pWARN_NONE ;
3255 PL_compiling.cop_warnings = pWARN_STD ;
3257 if (filter_sub || filter_child_proc) {
3258 SV *datasv = filter_add(run_user_filter, Nullsv);
3259 IoLINES(datasv) = filter_has_file;
3260 IoFMT_GV(datasv) = (GV *)filter_child_proc;
3261 IoTOP_GV(datasv) = (GV *)filter_state;
3262 IoBOTTOM_GV(datasv) = (GV *)filter_sub;
3265 /* switch to eval mode */
3266 push_return(PL_op->op_next);
3267 PUSHBLOCK(cx, CXt_EVAL, SP);
3268 PUSHEVAL(cx, name, Nullgv);
3270 SAVECOPLINE(&PL_compiling);
3271 CopLINE_set(&PL_compiling, 0);
3275 MUTEX_LOCK(&PL_eval_mutex);
3276 if (PL_eval_owner && PL_eval_owner != thr)
3277 while (PL_eval_owner)
3278 COND_WAIT(&PL_eval_cond, &PL_eval_mutex);
3279 PL_eval_owner = thr;
3280 MUTEX_UNLOCK(&PL_eval_mutex);
3281 #endif /* USE_THREADS */
3282 return DOCATCH(doeval(G_SCALAR, NULL));
3287 return pp_require();
3293 register PERL_CONTEXT *cx;
3295 I32 gimme = GIMME_V, was = PL_sub_generation;
3296 char tbuf[TYPE_DIGITS(long) + 12];
3297 char *tmpbuf = tbuf;
3302 if (!SvPV(sv,len) || !len)
3304 TAINT_PROPER("eval");
3310 /* switch to eval mode */
3312 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
3313 SV *sv = sv_newmortal();
3314 Perl_sv_setpvf(aTHX_ sv, "_<(eval %lu)[%s:%"IVdf"]",
3315 (unsigned long)++PL_evalseq,
3316 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3320 sprintf(tmpbuf, "_<(eval %lu)", (unsigned long)++PL_evalseq);
3321 SAVECOPFILE_FREE(&PL_compiling);
3322 CopFILE_set(&PL_compiling, tmpbuf+2);
3323 SAVECOPLINE(&PL_compiling);
3324 CopLINE_set(&PL_compiling, 1);
3325 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3326 deleting the eval's FILEGV from the stash before gv_check() runs
3327 (i.e. before run-time proper). To work around the coredump that
3328 ensues, we always turn GvMULTI_on for any globals that were
3329 introduced within evals. See force_ident(). GSAR 96-10-12 */
3330 safestr = savepv(tmpbuf);
3331 SAVEDELETE(PL_defstash, safestr, strlen(safestr));
3333 PL_hints = PL_op->op_targ;
3334 SAVESPTR(PL_compiling.cop_warnings);
3335 if (specialWARN(PL_curcop->cop_warnings))
3336 PL_compiling.cop_warnings = PL_curcop->cop_warnings;
3338 PL_compiling.cop_warnings = newSVsv(PL_curcop->cop_warnings);
3339 SAVEFREESV(PL_compiling.cop_warnings);
3342 push_return(PL_op->op_next);
3343 PUSHBLOCK(cx, (CXt_EVAL|CXp_REAL), SP);
3344 PUSHEVAL(cx, 0, Nullgv);
3346 /* prepare to compile string */
3348 if (PERLDB_LINE && PL_curstash != PL_debstash)
3349 save_lines(CopFILEAV(&PL_compiling), PL_linestr);
3352 MUTEX_LOCK(&PL_eval_mutex);
3353 if (PL_eval_owner && PL_eval_owner != thr)
3354 while (PL_eval_owner)
3355 COND_WAIT(&PL_eval_cond, &PL_eval_mutex);
3356 PL_eval_owner = thr;
3357 MUTEX_UNLOCK(&PL_eval_mutex);
3358 #endif /* USE_THREADS */
3359 ret = doeval(gimme, NULL);
3360 if (PERLDB_INTER && was != PL_sub_generation /* Some subs defined here. */
3361 && ret != PL_op->op_next) { /* Successive compilation. */
3362 strcpy(safestr, "_<(eval )"); /* Anything fake and short. */
3364 return DOCATCH(ret);
3374 register PERL_CONTEXT *cx;
3376 U8 save_flags = PL_op -> op_flags;
3381 retop = pop_return();
3384 if (gimme == G_VOID)
3386 else if (gimme == G_SCALAR) {
3389 if (SvFLAGS(TOPs) & SVs_TEMP)
3392 *MARK = sv_mortalcopy(TOPs);
3396 *MARK = &PL_sv_undef;
3401 /* in case LEAVE wipes old return values */
3402 for (mark = newsp + 1; mark <= SP; mark++) {
3403 if (!(SvFLAGS(*mark) & SVs_TEMP)) {
3404 *mark = sv_mortalcopy(*mark);
3405 TAINT_NOT; /* Each item is independent */
3409 PL_curpm = newpm; /* Don't pop $1 et al till now */
3412 assert(CvDEPTH(PL_compcv) == 1);
3414 CvDEPTH(PL_compcv) = 0;
3417 if (optype == OP_REQUIRE &&
3418 !(gimme == G_SCALAR ? SvTRUE(*SP) : SP > newsp))
3420 /* Unassume the success we assumed earlier. */
3421 SV *nsv = cx->blk_eval.old_namesv;
3422 (void)hv_delete(GvHVn(PL_incgv), SvPVX(nsv), SvCUR(nsv), G_DISCARD);
3423 retop = Perl_die(aTHX_ "%s did not return a true value", SvPVX(nsv));
3424 /* die_where() did LEAVE, or we won't be here */
3428 if (!(save_flags & OPf_SPECIAL))
3438 register PERL_CONTEXT *cx;
3439 I32 gimme = GIMME_V;
3444 push_return(cLOGOP->op_other->op_next);
3445 PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), SP);
3447 PL_eval_root = PL_op; /* Only needed so that goto works right. */
3449 PL_in_eval = EVAL_INEVAL;
3452 return DOCATCH(PL_op->op_next);
3462 register PERL_CONTEXT *cx;
3470 if (gimme == G_VOID)
3472 else if (gimme == G_SCALAR) {
3475 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
3478 *MARK = sv_mortalcopy(TOPs);
3482 *MARK = &PL_sv_undef;
3487 /* in case LEAVE wipes old return values */
3488 for (mark = newsp + 1; mark <= SP; mark++) {
3489 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
3490 *mark = sv_mortalcopy(*mark);
3491 TAINT_NOT; /* Each item is independent */
3495 PL_curpm = newpm; /* Don't pop $1 et al till now */
3503 S_doparseform(pTHX_ SV *sv)
3506 register char *s = SvPV_force(sv, len);
3507 register char *send = s + len;
3508 register char *base;
3509 register I32 skipspaces = 0;
3512 bool postspace = FALSE;
3520 Perl_croak(aTHX_ "Null picture in formline");
3522 New(804, fops, (send - s)*3+10, U16); /* Almost certainly too long... */
3527 *fpc++ = FF_LINEMARK;
3528 noblank = repeat = FALSE;
3546 case ' ': case '\t':
3557 *fpc++ = FF_LITERAL;
3565 *fpc++ = skipspaces;
3569 *fpc++ = FF_NEWLINE;
3573 arg = fpc - linepc + 1;
3580 *fpc++ = FF_LINEMARK;
3581 noblank = repeat = FALSE;
3590 ischop = s[-1] == '^';
3596 arg = (s - base) - 1;
3598 *fpc++ = FF_LITERAL;
3607 *fpc++ = FF_LINEGLOB;
3609 else if (*s == '#' || (*s == '.' && s[1] == '#')) {
3610 arg = ischop ? 512 : 0;
3620 arg |= 256 + (s - f);
3622 *fpc++ = s - base; /* fieldsize for FETCH */
3623 *fpc++ = FF_DECIMAL;
3628 bool ismore = FALSE;
3631 while (*++s == '>') ;
3632 prespace = FF_SPACE;
3634 else if (*s == '|') {
3635 while (*++s == '|') ;
3636 prespace = FF_HALFSPACE;
3641 while (*++s == '<') ;
3644 if (*s == '.' && s[1] == '.' && s[2] == '.') {
3648 *fpc++ = s - base; /* fieldsize for FETCH */
3650 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
3668 { /* need to jump to the next word */
3670 z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
3671 SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U16) + 4);
3672 s = SvPVX(sv) + SvCUR(sv) + z;
3674 Copy(fops, s, arg, U16);
3676 sv_magic(sv, Nullsv, 'f', Nullch, 0);
3681 * The rest of this file was derived from source code contributed
3684 * NOTE: this code was derived from Tom Horsley's qsort replacement
3685 * and should not be confused with the original code.
3688 /* Copyright (C) Tom Horsley, 1997. All rights reserved.
3690 Permission granted to distribute under the same terms as perl which are
3693 This program is free software; you can redistribute it and/or modify
3694 it under the terms of either:
3696 a) the GNU General Public License as published by the Free
3697 Software Foundation; either version 1, or (at your option) any
3700 b) the "Artistic License" which comes with this Kit.
3702 Details on the perl license can be found in the perl source code which
3703 may be located via the www.perl.com web page.
3705 This is the most wonderfulest possible qsort I can come up with (and
3706 still be mostly portable) My (limited) tests indicate it consistently
3707 does about 20% fewer calls to compare than does the qsort in the Visual
3708 C++ library, other vendors may vary.
3710 Some of the ideas in here can be found in "Algorithms" by Sedgewick,
3711 others I invented myself (or more likely re-invented since they seemed
3712 pretty obvious once I watched the algorithm operate for a while).
3714 Most of this code was written while watching the Marlins sweep the Giants
3715 in the 1997 National League Playoffs - no Braves fans allowed to use this
3716 code (just kidding :-).
3718 I realize that if I wanted to be true to the perl tradition, the only
3719 comment in this file would be something like:
3721 ...they shuffled back towards the rear of the line. 'No, not at the
3722 rear!' the slave-driver shouted. 'Three files up. And stay there...
3724 However, I really needed to violate that tradition just so I could keep
3725 track of what happens myself, not to mention some poor fool trying to
3726 understand this years from now :-).
3729 /* ********************************************************** Configuration */
3731 #ifndef QSORT_ORDER_GUESS
3732 #define QSORT_ORDER_GUESS 2 /* Select doubling version of the netBSD trick */
3735 /* QSORT_MAX_STACK is the largest number of partitions that can be stacked up for
3736 future processing - a good max upper bound is log base 2 of memory size
3737 (32 on 32 bit machines, 64 on 64 bit machines, etc). In reality can
3738 safely be smaller than that since the program is taking up some space and
3739 most operating systems only let you grab some subset of contiguous
3740 memory (not to mention that you are normally sorting data larger than
3741 1 byte element size :-).
3743 #ifndef QSORT_MAX_STACK
3744 #define QSORT_MAX_STACK 32
3747 /* QSORT_BREAK_EVEN is the size of the largest partition we should insertion sort.
3748 Anything bigger and we use qsort. If you make this too small, the qsort
3749 will probably break (or become less efficient), because it doesn't expect
3750 the middle element of a partition to be the same as the right or left -
3751 you have been warned).
3753 #ifndef QSORT_BREAK_EVEN
3754 #define QSORT_BREAK_EVEN 6
3757 /* ************************************************************* Data Types */
3759 /* hold left and right index values of a partition waiting to be sorted (the
3760 partition includes both left and right - right is NOT one past the end or
3761 anything like that).
3763 struct partition_stack_entry {
3766 #ifdef QSORT_ORDER_GUESS
3767 int qsort_break_even;
3771 /* ******************************************************* Shorthand Macros */
3773 /* Note that these macros will be used from inside the qsort function where
3774 we happen to know that the variable 'elt_size' contains the size of an
3775 array element and the variable 'temp' points to enough space to hold a
3776 temp element and the variable 'array' points to the array being sorted
3777 and 'compare' is the pointer to the compare routine.
3779 Also note that there are very many highly architecture specific ways
3780 these might be sped up, but this is simply the most generally portable
3781 code I could think of.
3784 /* Return < 0 == 0 or > 0 as the value of elt1 is < elt2, == elt2, > elt2
3786 #define qsort_cmp(elt1, elt2) \
3787 ((*compare)(aTHXo_ array[elt1], array[elt2]))
3789 #ifdef QSORT_ORDER_GUESS
3790 #define QSORT_NOTICE_SWAP swapped++;
3792 #define QSORT_NOTICE_SWAP
3795 /* swaps contents of array elements elt1, elt2.
3797 #define qsort_swap(elt1, elt2) \
3800 temp = array[elt1]; \
3801 array[elt1] = array[elt2]; \
3802 array[elt2] = temp; \
3805 /* rotate contents of elt1, elt2, elt3 such that elt1 gets elt2, elt2 gets
3806 elt3 and elt3 gets elt1.
3808 #define qsort_rotate(elt1, elt2, elt3) \
3811 temp = array[elt1]; \
3812 array[elt1] = array[elt2]; \
3813 array[elt2] = array[elt3]; \
3814 array[elt3] = temp; \
3817 /* ************************************************************ Debug stuff */
3824 return; /* good place to set a breakpoint */
3827 #define qsort_assert(t) (void)( (t) || (break_here(), 0) )
3830 doqsort_all_asserts(
3834 int (*compare)(const void * elt1, const void * elt2),
3835 int pc_left, int pc_right, int u_left, int u_right)
3839 qsort_assert(pc_left <= pc_right);
3840 qsort_assert(u_right < pc_left);
3841 qsort_assert(pc_right < u_left);
3842 for (i = u_right + 1; i < pc_left; ++i) {
3843 qsort_assert(qsort_cmp(i, pc_left) < 0);
3845 for (i = pc_left; i < pc_right; ++i) {
3846 qsort_assert(qsort_cmp(i, pc_right) == 0);
3848 for (i = pc_right + 1; i < u_left; ++i) {
3849 qsort_assert(qsort_cmp(pc_right, i) < 0);
3853 #define qsort_all_asserts(PC_LEFT, PC_RIGHT, U_LEFT, U_RIGHT) \
3854 doqsort_all_asserts(array, num_elts, elt_size, compare, \
3855 PC_LEFT, PC_RIGHT, U_LEFT, U_RIGHT)
3859 #define qsort_assert(t) ((void)0)
3861 #define qsort_all_asserts(PC_LEFT, PC_RIGHT, U_LEFT, U_RIGHT) ((void)0)
3865 /* ****************************************************************** qsort */
3868 S_qsortsv(pTHX_ SV ** array, size_t num_elts, SVCOMPARE_t compare)
3872 struct partition_stack_entry partition_stack[QSORT_MAX_STACK];
3873 int next_stack_entry = 0;
3877 #ifdef QSORT_ORDER_GUESS
3878 int qsort_break_even;
3882 /* Make sure we actually have work to do.
3884 if (num_elts <= 1) {
3888 /* Setup the initial partition definition and fall into the sorting loop
3891 part_right = (int)(num_elts - 1);
3892 #ifdef QSORT_ORDER_GUESS
3893 qsort_break_even = QSORT_BREAK_EVEN;
3895 #define qsort_break_even QSORT_BREAK_EVEN
3898 if ((part_right - part_left) >= qsort_break_even) {
3899 /* OK, this is gonna get hairy, so lets try to document all the
3900 concepts and abbreviations and variables and what they keep
3903 pc: pivot chunk - the set of array elements we accumulate in the
3904 middle of the partition, all equal in value to the original
3905 pivot element selected. The pc is defined by:
3907 pc_left - the leftmost array index of the pc
3908 pc_right - the rightmost array index of the pc
3910 we start with pc_left == pc_right and only one element
3911 in the pivot chunk (but it can grow during the scan).
3913 u: uncompared elements - the set of elements in the partition
3914 we have not yet compared to the pivot value. There are two
3915 uncompared sets during the scan - one to the left of the pc
3916 and one to the right.
3918 u_right - the rightmost index of the left side's uncompared set
3919 u_left - the leftmost index of the right side's uncompared set
3921 The leftmost index of the left sides's uncompared set
3922 doesn't need its own variable because it is always defined
3923 by the leftmost edge of the whole partition (part_left). The
3924 same goes for the rightmost edge of the right partition
3927 We know there are no uncompared elements on the left once we
3928 get u_right < part_left and no uncompared elements on the
3929 right once u_left > part_right. When both these conditions
3930 are met, we have completed the scan of the partition.
3932 Any elements which are between the pivot chunk and the
3933 uncompared elements should be less than the pivot value on
3934 the left side and greater than the pivot value on the right
3935 side (in fact, the goal of the whole algorithm is to arrange
3936 for that to be true and make the groups of less-than and
3937 greater-then elements into new partitions to sort again).
3939 As you marvel at the complexity of the code and wonder why it
3940 has to be so confusing. Consider some of the things this level
3941 of confusion brings:
3943 Once I do a compare, I squeeze every ounce of juice out of it. I
3944 never do compare calls I don't have to do, and I certainly never
3947 I also never swap any elements unless I can prove there is a
3948 good reason. Many sort algorithms will swap a known value with
3949 an uncompared value just to get things in the right place (or
3950 avoid complexity :-), but that uncompared value, once it gets
3951 compared, may then have to be swapped again. A lot of the
3952 complexity of this code is due to the fact that it never swaps
3953 anything except compared values, and it only swaps them when the
3954 compare shows they are out of position.
3956 int pc_left, pc_right;
3957 int u_right, u_left;
3961 pc_left = ((part_left + part_right) / 2);
3963 u_right = pc_left - 1;
3964 u_left = pc_right + 1;
3966 /* Qsort works best when the pivot value is also the median value
3967 in the partition (unfortunately you can't find the median value
3968 without first sorting :-), so to give the algorithm a helping
3969 hand, we pick 3 elements and sort them and use the median value
3970 of that tiny set as the pivot value.
3972 Some versions of qsort like to use the left middle and right as
3973 the 3 elements to sort so they can insure the ends of the
3974 partition will contain values which will stop the scan in the
3975 compare loop, but when you have to call an arbitrarily complex
3976 routine to do a compare, its really better to just keep track of
3977 array index values to know when you hit the edge of the
3978 partition and avoid the extra compare. An even better reason to
3979 avoid using a compare call is the fact that you can drop off the
3980 edge of the array if someone foolishly provides you with an
3981 unstable compare function that doesn't always provide consistent
3984 So, since it is simpler for us to compare the three adjacent
3985 elements in the middle of the partition, those are the ones we
3986 pick here (conveniently pointed at by u_right, pc_left, and
3987 u_left). The values of the left, center, and right elements
3988 are refered to as l c and r in the following comments.
3991 #ifdef QSORT_ORDER_GUESS
3994 s = qsort_cmp(u_right, pc_left);
3997 s = qsort_cmp(pc_left, u_left);
3998 /* if l < c, c < r - already in order - nothing to do */
4000 /* l < c, c == r - already in order, pc grows */
4002 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
4004 /* l < c, c > r - need to know more */
4005 s = qsort_cmp(u_right, u_left);
4007 /* l < c, c > r, l < r - swap c & r to get ordered */
4008 qsort_swap(pc_left, u_left);
4009 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
4010 } else if (s == 0) {
4011 /* l < c, c > r, l == r - swap c&r, grow pc */
4012 qsort_swap(pc_left, u_left);
4014 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
4016 /* l < c, c > r, l > r - make lcr into rlc to get ordered */
4017 qsort_rotate(pc_left, u_right, u_left);
4018 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
4021 } else if (s == 0) {
4023 s = qsort_cmp(pc_left, u_left);
4025 /* l == c, c < r - already in order, grow pc */
4027 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
4028 } else if (s == 0) {
4029 /* l == c, c == r - already in order, grow pc both ways */
4032 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
4034 /* l == c, c > r - swap l & r, grow pc */
4035 qsort_swap(u_right, u_left);
4037 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
4041 s = qsort_cmp(pc_left, u_left);
4043 /* l > c, c < r - need to know more */
4044 s = qsort_cmp(u_right, u_left);
4046 /* l > c, c < r, l < r - swap l & c to get ordered */
4047 qsort_swap(u_right, pc_left);
4048 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
4049 } else if (s == 0) {
4050 /* l > c, c < r, l == r - swap l & c, grow pc */
4051 qsort_swap(u_right, pc_left);
4053 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
4055 /* l > c, c < r, l > r - rotate lcr into crl to order */
4056 qsort_rotate(u_right, pc_left, u_left);
4057 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
4059 } else if (s == 0) {
4060 /* l > c, c == r - swap ends, grow pc */
4061 qsort_swap(u_right, u_left);
4063 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
4065 /* l > c, c > r - swap ends to get in order */
4066 qsort_swap(u_right, u_left);
4067 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
4070 /* We now know the 3 middle elements have been compared and
4071 arranged in the desired order, so we can shrink the uncompared
4076 qsort_all_asserts(pc_left, pc_right, u_left, u_right);
4078 /* The above massive nested if was the simple part :-). We now have
4079 the middle 3 elements ordered and we need to scan through the
4080 uncompared sets on either side, swapping elements that are on
4081 the wrong side or simply shuffling equal elements around to get
4082 all equal elements into the pivot chunk.
4086 int still_work_on_left;
4087 int still_work_on_right;
4089 /* Scan the uncompared values on the left. If I find a value
4090 equal to the pivot value, move it over so it is adjacent to
4091 the pivot chunk and expand the pivot chunk. If I find a value
4092 less than the pivot value, then just leave it - its already
4093 on the correct side of the partition. If I find a greater
4094 value, then stop the scan.
4096 while ((still_work_on_left = (u_right >= part_left))) {
4097 s = qsort_cmp(u_right, pc_left);
4100 } else if (s == 0) {
4102 if (pc_left != u_right) {
4103 qsort_swap(u_right, pc_left);
4109 qsort_assert(u_right < pc_left);
4110 qsort_assert(pc_left <= pc_right);
4111 qsort_assert(qsort_cmp(u_right + 1, pc_left) <= 0);
4112 qsort_assert(qsort_cmp(pc_left, pc_right) == 0);
4115 /* Do a mirror image scan of uncompared values on the right
4117 while ((still_work_on_right = (u_left <= part_right))) {
4118 s = qsort_cmp(pc_right, u_left);
4121 } else if (s == 0) {
4123 if (pc_right != u_left) {
4124 qsort_swap(pc_right, u_left);
4130 qsort_assert(u_left > pc_right);
4131 qsort_assert(pc_left <= pc_right);
4132 qsort_assert(qsort_cmp(pc_right, u_left - 1) <= 0);
4133 qsort_assert(qsort_cmp(pc_left, pc_right) == 0);
4136 if (still_work_on_left) {
4137 /* I know I have a value on the left side which needs to be
4138 on the right side, but I need to know more to decide
4139 exactly the best thing to do with it.
4141 if (still_work_on_right) {
4142 /* I know I have values on both side which are out of
4143 position. This is a big win because I kill two birds
4144 with one swap (so to speak). I can advance the
4145 uncompared pointers on both sides after swapping both
4146 of them into the right place.
4148 qsort_swap(u_right, u_left);
4151 qsort_all_asserts(pc_left, pc_right, u_left, u_right);
4153 /* I have an out of position value on the left, but the
4154 right is fully scanned, so I "slide" the pivot chunk
4155 and any less-than values left one to make room for the
4156 greater value over on the right. If the out of position
4157 value is immediately adjacent to the pivot chunk (there
4158 are no less-than values), I can do that with a swap,
4159 otherwise, I have to rotate one of the less than values
4160 into the former position of the out of position value
4161 and the right end of the pivot chunk into the left end
4165 if (pc_left == u_right) {
4166 qsort_swap(u_right, pc_right);
4167 qsort_all_asserts(pc_left, pc_right-1, u_left, u_right-1);
4169 qsort_rotate(u_right, pc_left, pc_right);
4170 qsort_all_asserts(pc_left, pc_right-1, u_left, u_right-1);
4175 } else if (still_work_on_right) {
4176 /* Mirror image of complex case above: I have an out of
4177 position value on the right, but the left is fully
4178 scanned, so I need to shuffle things around to make room
4179 for the right value on the left.
4182 if (pc_right == u_left) {
4183 qsort_swap(u_left, pc_left);
4184 qsort_all_asserts(pc_left+1, pc_right, u_left+1, u_right);
4186 qsort_rotate(pc_right, pc_left, u_left);
4187 qsort_all_asserts(pc_left+1, pc_right, u_left+1, u_right);
4192 /* No more scanning required on either side of partition,
4193 break out of loop and figure out next set of partitions
4199 /* The elements in the pivot chunk are now in the right place. They
4200 will never move or be compared again. All I have to do is decide
4201 what to do with the stuff to the left and right of the pivot
4204 Notes on the QSORT_ORDER_GUESS ifdef code:
4206 1. If I just built these partitions without swapping any (or
4207 very many) elements, there is a chance that the elements are
4208 already ordered properly (being properly ordered will
4209 certainly result in no swapping, but the converse can't be
4212 2. A (properly written) insertion sort will run faster on
4213 already ordered data than qsort will.
4215 3. Perhaps there is some way to make a good guess about
4216 switching to an insertion sort earlier than partition size 6
4217 (for instance - we could save the partition size on the stack
4218 and increase the size each time we find we didn't swap, thus
4219 switching to insertion sort earlier for partitions with a
4220 history of not swapping).
4222 4. Naturally, if I just switch right away, it will make
4223 artificial benchmarks with pure ascending (or descending)
4224 data look really good, but is that a good reason in general?
4228 #ifdef QSORT_ORDER_GUESS
4230 #if QSORT_ORDER_GUESS == 1
4231 qsort_break_even = (part_right - part_left) + 1;
4233 #if QSORT_ORDER_GUESS == 2
4234 qsort_break_even *= 2;
4236 #if QSORT_ORDER_GUESS == 3
4237 int prev_break = qsort_break_even;
4238 qsort_break_even *= qsort_break_even;
4239 if (qsort_break_even < prev_break) {
4240 qsort_break_even = (part_right - part_left) + 1;
4244 qsort_break_even = QSORT_BREAK_EVEN;
4248 if (part_left < pc_left) {
4249 /* There are elements on the left which need more processing.
4250 Check the right as well before deciding what to do.
4252 if (pc_right < part_right) {
4253 /* We have two partitions to be sorted. Stack the biggest one
4254 and process the smallest one on the next iteration. This
4255 minimizes the stack height by insuring that any additional
4256 stack entries must come from the smallest partition which
4257 (because it is smallest) will have the fewest
4258 opportunities to generate additional stack entries.
4260 if ((part_right - pc_right) > (pc_left - part_left)) {
4261 /* stack the right partition, process the left */
4262 partition_stack[next_stack_entry].left = pc_right + 1;
4263 partition_stack[next_stack_entry].right = part_right;
4264 #ifdef QSORT_ORDER_GUESS
4265 partition_stack[next_stack_entry].qsort_break_even = qsort_break_even;
4267 part_right = pc_left - 1;
4269 /* stack the left partition, process the right */
4270 partition_stack[next_stack_entry].left = part_left;
4271 partition_stack[next_stack_entry].right = pc_left - 1;
4272 #ifdef QSORT_ORDER_GUESS
4273 partition_stack[next_stack_entry].qsort_break_even = qsort_break_even;
4275 part_left = pc_right + 1;
4277 qsort_assert(next_stack_entry < QSORT_MAX_STACK);
4280 /* The elements on the left are the only remaining elements
4281 that need sorting, arrange for them to be processed as the
4284 part_right = pc_left - 1;
4286 } else if (pc_right < part_right) {
4287 /* There is only one chunk on the right to be sorted, make it
4288 the new partition and loop back around.
4290 part_left = pc_right + 1;
4292 /* This whole partition wound up in the pivot chunk, so
4293 we need to get a new partition off the stack.
4295 if (next_stack_entry == 0) {
4296 /* the stack is empty - we are done */
4300 part_left = partition_stack[next_stack_entry].left;
4301 part_right = partition_stack[next_stack_entry].right;
4302 #ifdef QSORT_ORDER_GUESS
4303 qsort_break_even = partition_stack[next_stack_entry].qsort_break_even;
4307 /* This partition is too small to fool with qsort complexity, just
4308 do an ordinary insertion sort to minimize overhead.
4311 /* Assume 1st element is in right place already, and start checking
4312 at 2nd element to see where it should be inserted.
4314 for (i = part_left + 1; i <= part_right; ++i) {
4316 /* Scan (backwards - just in case 'i' is already in right place)
4317 through the elements already sorted to see if the ith element
4318 belongs ahead of one of them.
4320 for (j = i - 1; j >= part_left; --j) {
4321 if (qsort_cmp(i, j) >= 0) {
4322 /* i belongs right after j
4329 /* Looks like we really need to move some things
4333 for (k = i - 1; k >= j; --k)
4334 array[k + 1] = array[k];
4339 /* That partition is now sorted, grab the next one, or get out
4340 of the loop if there aren't any more.
4343 if (next_stack_entry == 0) {
4344 /* the stack is empty - we are done */
4348 part_left = partition_stack[next_stack_entry].left;
4349 part_right = partition_stack[next_stack_entry].right;
4350 #ifdef QSORT_ORDER_GUESS
4351 qsort_break_even = partition_stack[next_stack_entry].qsort_break_even;
4356 /* Believe it or not, the array is sorted at this point! */
4368 sortcv(pTHXo_ SV *a, SV *b)
4370 I32 oldsaveix = PL_savestack_ix;
4371 I32 oldscopeix = PL_scopestack_ix;
4373 GvSV(PL_firstgv) = a;
4374 GvSV(PL_secondgv) = b;
4375 PL_stack_sp = PL_stack_base;
4378 if (PL_stack_sp != PL_stack_base + 1)
4379 Perl_croak(aTHX_ "Sort subroutine didn't return single value");
4380 if (!SvNIOKp(*PL_stack_sp))
4381 Perl_croak(aTHX_ "Sort subroutine didn't return a numeric value");
4382 result = SvIV(*PL_stack_sp);
4383 while (PL_scopestack_ix > oldscopeix) {
4386 leave_scope(oldsaveix);
4391 sortcv_stacked(pTHXo_ SV *a, SV *b)
4393 I32 oldsaveix = PL_savestack_ix;
4394 I32 oldscopeix = PL_scopestack_ix;
4399 av = (AV*)PL_curpad[0];
4401 av = GvAV(PL_defgv);
4404 if (AvMAX(av) < 1) {
4405 SV** ary = AvALLOC(av);
4406 if (AvARRAY(av) != ary) {
4407 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
4408 SvPVX(av) = (char*)ary;
4410 if (AvMAX(av) < 1) {
4413 SvPVX(av) = (char*)ary;
4420 PL_stack_sp = PL_stack_base;
4423 if (PL_stack_sp != PL_stack_base + 1)
4424 Perl_croak(aTHX_ "Sort subroutine didn't return single value");
4425 if (!SvNIOKp(*PL_stack_sp))
4426 Perl_croak(aTHX_ "Sort subroutine didn't return a numeric value");
4427 result = SvIV(*PL_stack_sp);
4428 while (PL_scopestack_ix > oldscopeix) {
4431 leave_scope(oldsaveix);
4436 sortcv_xsub(pTHXo_ SV *a, SV *b)
4439 I32 oldsaveix = PL_savestack_ix;
4440 I32 oldscopeix = PL_scopestack_ix;
4442 CV *cv=(CV*)PL_sortcop;
4450 (void)(*CvXSUB(cv))(aTHXo_ cv);
4451 if (PL_stack_sp != PL_stack_base + 1)
4452 Perl_croak(aTHX_ "Sort subroutine didn't return single value");
4453 if (!SvNIOKp(*PL_stack_sp))
4454 Perl_croak(aTHX_ "Sort subroutine didn't return a numeric value");
4455 result = SvIV(*PL_stack_sp);
4456 while (PL_scopestack_ix > oldscopeix) {
4459 leave_scope(oldsaveix);
4465 sv_ncmp(pTHXo_ SV *a, SV *b)
4469 return nv1 < nv2 ? -1 : nv1 > nv2 ? 1 : 0;
4473 sv_i_ncmp(pTHXo_ SV *a, SV *b)
4477 return iv1 < iv2 ? -1 : iv1 > iv2 ? 1 : 0;
4479 #define tryCALL_AMAGICbin(left,right,meth,svp) STMT_START { \
4481 if (PL_amagic_generation) { \
4482 if (SvAMAGIC(left)||SvAMAGIC(right))\
4483 *svp = amagic_call(left, \
4491 amagic_ncmp(pTHXo_ register SV *a, register SV *b)
4494 tryCALL_AMAGICbin(a,b,ncmp,&tmpsv);
4499 I32 i = SvIVX(tmpsv);
4509 return sv_ncmp(aTHXo_ a, b);
4513 amagic_i_ncmp(pTHXo_ register SV *a, register SV *b)
4516 tryCALL_AMAGICbin(a,b,ncmp,&tmpsv);
4521 I32 i = SvIVX(tmpsv);
4531 return sv_i_ncmp(aTHXo_ a, b);
4535 amagic_cmp(pTHXo_ register SV *str1, register SV *str2)
4538 tryCALL_AMAGICbin(str1,str2,scmp,&tmpsv);
4543 I32 i = SvIVX(tmpsv);
4553 return sv_cmp(str1, str2);
4557 amagic_cmp_locale(pTHXo_ register SV *str1, register SV *str2)
4560 tryCALL_AMAGICbin(str1,str2,scmp,&tmpsv);
4565 I32 i = SvIVX(tmpsv);
4575 return sv_cmp_locale(str1, str2);
4579 run_user_filter(pTHXo_ int idx, SV *buf_sv, int maxlen)
4581 SV *datasv = FILTER_DATA(idx);
4582 int filter_has_file = IoLINES(datasv);
4583 GV *filter_child_proc = (GV *)IoFMT_GV(datasv);
4584 SV *filter_state = (SV *)IoTOP_GV(datasv);
4585 SV *filter_sub = (SV *)IoBOTTOM_GV(datasv);
4588 /* I was having segfault trouble under Linux 2.2.5 after a
4589 parse error occured. (Had to hack around it with a test
4590 for PL_error_count == 0.) Solaris doesn't segfault --
4591 not sure where the trouble is yet. XXX */
4593 if (filter_has_file) {
4594 len = FILTER_READ(idx+1, buf_sv, maxlen);
4597 if (filter_sub && len >= 0) {
4608 PUSHs(sv_2mortal(newSViv(maxlen)));
4610 PUSHs(filter_state);
4613 count = call_sv(filter_sub, G_SCALAR);
4629 IoLINES(datasv) = 0;
4630 if (filter_child_proc) {
4631 SvREFCNT_dec(filter_child_proc);
4632 IoFMT_GV(datasv) = Nullgv;
4635 SvREFCNT_dec(filter_state);
4636 IoTOP_GV(datasv) = Nullgv;
4639 SvREFCNT_dec(filter_sub);
4640 IoBOTTOM_GV(datasv) = Nullgv;
4642 filter_del(run_user_filter);
4651 sv_cmp_locale_static(pTHXo_ register SV *str1, register SV *str2)
4653 return sv_cmp_locale(str1, str2);
4657 sv_cmp_static(pTHXo_ register SV *str1, register SV *str2)
4659 return sv_cmp(str1, str2);
4662 #endif /* PERL_OBJECT */