3 * Copyright (c) 1991-2001, 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 /* prevent recompiling under /o and ithreads. */
93 #if defined(USE_ITHREADS) || defined(USE_THREADS)
94 if (pm->op_pmflags & PMf_KEEP && PM_GETRE(pm))
100 SV *sv = SvRV(tmpstr);
102 mg = mg_find(sv, PERL_MAGIC_qr);
105 regexp *re = (regexp *)mg->mg_obj;
106 ReREFCNT_dec(PM_GETRE(pm));
107 PM_SETRE(pm, ReREFCNT_inc(re));
110 t = SvPV(tmpstr, len);
112 /* Check against the last compiled regexp. */
113 if (!PM_GETRE(pm) || !PM_GETRE(pm)->precomp ||
114 PM_GETRE(pm)->prelen != len ||
115 memNE(PM_GETRE(pm)->precomp, t, len))
118 ReREFCNT_dec(PM_GETRE(pm));
119 PM_SETRE(pm, Null(REGEXP*)); /* crucial if regcomp aborts */
121 if (PL_op->op_flags & OPf_SPECIAL)
122 PL_reginterp_cnt = I32_MAX; /* Mark as safe. */
124 pm->op_pmflags = pm->op_pmpermflags; /* reset case sensitivity */
126 pm->op_pmdynflags |= PMdf_DYN_UTF8;
128 pm->op_pmdynflags &= ~PMdf_DYN_UTF8;
129 if (pm->op_pmdynflags & PMdf_UTF8)
130 t = (char*)bytes_to_utf8((U8*)t, &len);
132 PM_SETRE(pm, CALLREGCOMP(aTHX_ t, t + len, pm));
133 if (!DO_UTF8(tmpstr) && (pm->op_pmdynflags & PMdf_UTF8))
135 PL_reginterp_cnt = 0; /* XXXX Be extra paranoid - needed
136 inside tie/overload accessors. */
140 #ifndef INCOMPLETE_TAINTS
143 pm->op_pmdynflags |= PMdf_TAINTED;
145 pm->op_pmdynflags &= ~PMdf_TAINTED;
149 if (!PM_GETRE(pm)->prelen && PL_curpm)
151 else if (strEQ("\\s+", PM_GETRE(pm)->precomp))
152 pm->op_pmflags |= PMf_WHITE;
154 /* XXX runtime compiled output needs to move to the pad */
155 if (pm->op_pmflags & PMf_KEEP) {
156 pm->op_private &= ~OPpRUNTIME; /* no point compiling again */
157 #if !defined(USE_ITHREADS) && !defined(USE_THREADS)
158 /* XXX can't change the optree at runtime either */
159 cLOGOP->op_first->op_next = PL_op->op_next;
168 register PMOP *pm = (PMOP*) cLOGOP->op_other;
169 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
170 register SV *dstr = cx->sb_dstr;
171 register char *s = cx->sb_s;
172 register char *m = cx->sb_m;
173 char *orig = cx->sb_orig;
174 register REGEXP *rx = cx->sb_rx;
176 rxres_restore(&cx->sb_rxres, rx);
178 if (cx->sb_iters++) {
179 if (cx->sb_iters > cx->sb_maxiters)
180 DIE(aTHX_ "Substitution loop");
182 if (!(cx->sb_rxtainted & 2) && SvTAINTED(TOPs))
183 cx->sb_rxtainted |= 2;
184 sv_catsv(dstr, POPs);
187 if (cx->sb_once || !CALLREGEXEC(aTHX_ rx, s, cx->sb_strend, orig,
188 s == m, cx->sb_targ, NULL,
189 ((cx->sb_rflags & REXEC_COPY_STR)
190 ? (REXEC_IGNOREPOS|REXEC_NOT_FIRST)
191 : (REXEC_COPY_STR|REXEC_IGNOREPOS|REXEC_NOT_FIRST))))
193 SV *targ = cx->sb_targ;
195 sv_catpvn(dstr, s, cx->sb_strend - s);
196 cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
198 (void)SvOOK_off(targ);
199 Safefree(SvPVX(targ));
200 SvPVX(targ) = SvPVX(dstr);
201 SvCUR_set(targ, SvCUR(dstr));
202 SvLEN_set(targ, SvLEN(dstr));
208 TAINT_IF(cx->sb_rxtainted & 1);
209 PUSHs(sv_2mortal(newSViv((I32)cx->sb_iters - 1)));
211 (void)SvPOK_only_UTF8(targ);
212 TAINT_IF(cx->sb_rxtainted);
216 LEAVE_SCOPE(cx->sb_oldsave);
218 RETURNOP(pm->op_next);
221 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
224 cx->sb_orig = orig = rx->subbeg;
226 cx->sb_strend = s + (cx->sb_strend - m);
228 cx->sb_m = m = rx->startp[0] + orig;
230 sv_catpvn(dstr, s, m-s);
231 cx->sb_s = rx->endp[0] + orig;
232 { /* Update the pos() information. */
233 SV *sv = cx->sb_targ;
236 if (SvTYPE(sv) < SVt_PVMG)
237 (void)SvUPGRADE(sv, SVt_PVMG);
238 if (!(mg = mg_find(sv, PERL_MAGIC_regex_global))) {
239 sv_magic(sv, Nullsv, PERL_MAGIC_regex_global, Nullch, 0);
240 mg = mg_find(sv, PERL_MAGIC_regex_global);
247 cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
248 rxres_save(&cx->sb_rxres, rx);
249 RETURNOP(pm->op_pmreplstart);
253 Perl_rxres_save(pTHX_ void **rsp, REGEXP *rx)
258 if (!p || p[1] < rx->nparens) {
259 i = 6 + rx->nparens * 2;
267 *p++ = PTR2UV(RX_MATCH_COPIED(rx) ? rx->subbeg : Nullch);
268 RX_MATCH_COPIED_off(rx);
272 *p++ = PTR2UV(rx->subbeg);
273 *p++ = (UV)rx->sublen;
274 for (i = 0; i <= rx->nparens; ++i) {
275 *p++ = (UV)rx->startp[i];
276 *p++ = (UV)rx->endp[i];
281 Perl_rxres_restore(pTHX_ void **rsp, REGEXP *rx)
286 if (RX_MATCH_COPIED(rx))
287 Safefree(rx->subbeg);
288 RX_MATCH_COPIED_set(rx, *p);
293 rx->subbeg = INT2PTR(char*,*p++);
294 rx->sublen = (I32)(*p++);
295 for (i = 0; i <= rx->nparens; ++i) {
296 rx->startp[i] = (I32)(*p++);
297 rx->endp[i] = (I32)(*p++);
302 Perl_rxres_free(pTHX_ void **rsp)
307 Safefree(INT2PTR(char*,*p));
315 dSP; dMARK; dORIGMARK;
316 register SV *tmpForm = *++MARK;
323 register SV *sv = Nullsv;
328 bool chopspace = (strchr(PL_chopset, ' ') != Nullch);
329 char *chophere = Nullch;
330 char *linemark = Nullch;
332 bool gotsome = FALSE;
334 STRLEN fudge = SvCUR(tmpForm) * (IN_BYTES ? 1 : 3) + 1;
335 bool item_is_utf = FALSE;
337 if (!SvMAGICAL(tmpForm) || !SvCOMPILED(tmpForm)) {
338 if (SvREADONLY(tmpForm)) {
339 SvREADONLY_off(tmpForm);
340 doparseform(tmpForm);
341 SvREADONLY_on(tmpForm);
344 doparseform(tmpForm);
347 SvPV_force(PL_formtarget, len);
348 t = SvGROW(PL_formtarget, len + fudge + 1); /* XXX SvCUR bad */
350 f = SvPV(tmpForm, len);
351 /* need to jump to the next word */
352 s = f + len + WORD_ALIGN - SvCUR(tmpForm) % WORD_ALIGN;
361 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
362 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
363 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
364 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
365 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
367 case FF_CHECKNL: name = "CHECKNL"; break;
368 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
369 case FF_SPACE: name = "SPACE"; break;
370 case FF_HALFSPACE: name = "HALFSPACE"; break;
371 case FF_ITEM: name = "ITEM"; break;
372 case FF_CHOP: name = "CHOP"; break;
373 case FF_LINEGLOB: name = "LINEGLOB"; break;
374 case FF_NEWLINE: name = "NEWLINE"; break;
375 case FF_MORE: name = "MORE"; break;
376 case FF_LINEMARK: name = "LINEMARK"; break;
377 case FF_END: name = "END"; break;
378 case FF_0DECIMAL: name = "0DECIMAL"; break;
381 PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg);
383 PerlIO_printf(Perl_debug_log, "%-16s\n", name);
411 if (ckWARN(WARN_SYNTAX))
412 Perl_warner(aTHX_ WARN_SYNTAX, "Not enough format arguments");
417 item = s = SvPV(sv, len);
420 itemsize = sv_len_utf8(sv);
421 if (itemsize != len) {
423 if (itemsize > fieldsize) {
424 itemsize = fieldsize;
425 itembytes = itemsize;
426 sv_pos_u2b(sv, &itembytes, 0);
430 send = chophere = s + itembytes;
440 sv_pos_b2u(sv, &itemsize);
445 if (itemsize > fieldsize)
446 itemsize = fieldsize;
447 send = chophere = s + itemsize;
459 item = s = SvPV(sv, len);
462 itemsize = sv_len_utf8(sv);
463 if (itemsize != len) {
465 if (itemsize <= fieldsize) {
466 send = chophere = s + itemsize;
477 itemsize = fieldsize;
478 itembytes = itemsize;
479 sv_pos_u2b(sv, &itembytes, 0);
480 send = chophere = s + itembytes;
481 while (s < send || (s == send && isSPACE(*s))) {
491 if (strchr(PL_chopset, *s))
496 itemsize = chophere - item;
497 sv_pos_b2u(sv, &itemsize);
504 if (itemsize <= fieldsize) {
505 send = chophere = s + itemsize;
516 itemsize = fieldsize;
517 send = chophere = s + itemsize;
518 while (s < send || (s == send && isSPACE(*s))) {
528 if (strchr(PL_chopset, *s))
533 itemsize = chophere - item;
538 arg = fieldsize - itemsize;
547 arg = fieldsize - itemsize;
561 if (UTF8_IS_CONTINUED(*s)) {
562 STRLEN skip = UTF8SKIP(s);
579 if ( !((*t++ = *s++) & ~31) )
587 int ch = *t++ = *s++;
590 if ( !((*t++ = *s++) & ~31) )
599 while (*s && isSPACE(*s))
606 item = s = SvPV(sv, len);
608 item_is_utf = FALSE; /* XXX is this correct? */
620 SvCUR_set(PL_formtarget, t - SvPVX(PL_formtarget));
621 sv_catpvn(PL_formtarget, item, itemsize);
622 SvGROW(PL_formtarget, SvCUR(PL_formtarget) + fudge + 1);
623 t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
628 /* If the field is marked with ^ and the value is undefined,
631 if ((arg & 512) && !SvOK(sv)) {
639 /* Formats aren't yet marked for locales, so assume "yes". */
641 STORE_NUMERIC_STANDARD_SET_LOCAL();
642 #if defined(USE_LONG_DOUBLE)
644 sprintf(t, "%#*.*" PERL_PRIfldbl,
645 (int) fieldsize, (int) arg & 255, value);
647 sprintf(t, "%*.0" PERL_PRIfldbl, (int) fieldsize, value);
652 (int) fieldsize, (int) arg & 255, value);
655 (int) fieldsize, value);
658 RESTORE_NUMERIC_STANDARD();
664 /* If the field is marked with ^ and the value is undefined,
667 if ((arg & 512) && !SvOK(sv)) {
675 /* Formats aren't yet marked for locales, so assume "yes". */
677 STORE_NUMERIC_STANDARD_SET_LOCAL();
678 #if defined(USE_LONG_DOUBLE)
680 sprintf(t, "%#0*.*" PERL_PRIfldbl,
681 (int) fieldsize, (int) arg & 255, value);
682 /* is this legal? I don't have long doubles */
684 sprintf(t, "%0*.0" PERL_PRIfldbl, (int) fieldsize, value);
688 sprintf(t, "%#0*.*f",
689 (int) fieldsize, (int) arg & 255, value);
692 (int) fieldsize, value);
695 RESTORE_NUMERIC_STANDARD();
702 while (t-- > linemark && *t == ' ') ;
710 if (arg) { /* repeat until fields exhausted? */
712 SvCUR_set(PL_formtarget, t - SvPVX(PL_formtarget));
713 lines += FmLINES(PL_formtarget);
716 if (strnEQ(linemark, linemark - arg, arg))
717 DIE(aTHX_ "Runaway format");
719 FmLINES(PL_formtarget) = lines;
721 RETURNOP(cLISTOP->op_first);
734 while (*s && isSPACE(*s) && s < send)
738 arg = fieldsize - itemsize;
745 if (strnEQ(s," ",3)) {
746 while (s > SvPVX(PL_formtarget) && isSPACE(s[-1]))
757 SvCUR_set(PL_formtarget, t - SvPVX(PL_formtarget));
758 FmLINES(PL_formtarget) += lines;
770 if (PL_stack_base + *PL_markstack_ptr == SP) {
772 if (GIMME_V == G_SCALAR)
773 XPUSHs(sv_2mortal(newSViv(0)));
774 RETURNOP(PL_op->op_next->op_next);
776 PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1;
777 pp_pushmark(); /* push dst */
778 pp_pushmark(); /* push src */
779 ENTER; /* enter outer scope */
782 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
784 ENTER; /* enter inner scope */
787 src = PL_stack_base[*PL_markstack_ptr];
792 if (PL_op->op_type == OP_MAPSTART)
793 pp_pushmark(); /* push top */
794 return ((LOGOP*)PL_op->op_next)->op_other;
799 DIE(aTHX_ "panic: mapstart"); /* uses grepstart */
805 I32 items = (SP - PL_stack_base) - *PL_markstack_ptr; /* how many new items */
811 /* first, move source pointer to the next item in the source list */
812 ++PL_markstack_ptr[-1];
814 /* if there are new items, push them into the destination list */
816 /* might need to make room back there first */
817 if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
818 /* XXX this implementation is very pessimal because the stack
819 * is repeatedly extended for every set of items. Is possible
820 * to do this without any stack extension or copying at all
821 * by maintaining a separate list over which the map iterates
822 * (like foreach does). --gsar */
824 /* everything in the stack after the destination list moves
825 * towards the end the stack by the amount of room needed */
826 shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
828 /* items to shift up (accounting for the moved source pointer) */
829 count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1);
831 /* This optimization is by Ben Tilly and it does
832 * things differently from what Sarathy (gsar)
833 * is describing. The downside of this optimization is
834 * that leaves "holes" (uninitialized and hopefully unused areas)
835 * to the Perl stack, but on the other hand this
836 * shouldn't be a problem. If Sarathy's idea gets
837 * implemented, this optimization should become
838 * irrelevant. --jhi */
840 shift = count; /* Avoid shifting too often --Ben Tilly */
845 PL_markstack_ptr[-1] += shift;
846 *PL_markstack_ptr += shift;
850 /* copy the new items down to the destination list */
851 dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1;
853 *dst-- = SvTEMP(TOPs) ? POPs : sv_mortalcopy(POPs);
855 LEAVE; /* exit inner scope */
858 if (PL_markstack_ptr[-1] > *PL_markstack_ptr) {
861 (void)POPMARK; /* pop top */
862 LEAVE; /* exit outer scope */
863 (void)POPMARK; /* pop src */
864 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
865 (void)POPMARK; /* pop dst */
866 SP = PL_stack_base + POPMARK; /* pop original mark */
867 if (gimme == G_SCALAR) {
871 else if (gimme == G_ARRAY)
878 ENTER; /* enter inner scope */
881 /* set $_ to the new source item */
882 src = PL_stack_base[PL_markstack_ptr[-1]];
886 RETURNOP(cLOGOP->op_other);
892 dSP; dMARK; dORIGMARK;
894 SV **myorigmark = ORIGMARK;
900 OP* nextop = PL_op->op_next;
902 bool hasargs = FALSE;
905 if (gimme != G_ARRAY) {
911 SAVEVPTR(PL_sortcop);
912 if (PL_op->op_flags & OPf_STACKED) {
913 if (PL_op->op_flags & OPf_SPECIAL) {
914 OP *kid = cLISTOP->op_first->op_sibling; /* pass pushmark */
915 kid = kUNOP->op_first; /* pass rv2gv */
916 kid = kUNOP->op_first; /* pass leave */
917 PL_sortcop = kid->op_next;
918 stash = CopSTASH(PL_curcop);
921 cv = sv_2cv(*++MARK, &stash, &gv, 0);
922 if (cv && SvPOK(cv)) {
924 char *proto = SvPV((SV*)cv, n_a);
925 if (proto && strEQ(proto, "$$")) {
929 if (!(cv && CvROOT(cv))) {
930 if (cv && CvXSUB(cv)) {
934 SV *tmpstr = sv_newmortal();
935 gv_efullname3(tmpstr, gv, Nullch);
936 DIE(aTHX_ "Undefined sort subroutine \"%s\" called",
940 DIE(aTHX_ "Undefined subroutine in sort");
945 PL_sortcop = (OP*)cv;
947 PL_sortcop = CvSTART(cv);
948 SAVEVPTR(CvROOT(cv)->op_ppaddr);
949 CvROOT(cv)->op_ppaddr = PL_ppaddr[OP_NULL];
952 PL_curpad = AvARRAY((AV*)AvARRAY(CvPADLIST(cv))[1]);
958 stash = CopSTASH(PL_curcop);
962 while (MARK < SP) { /* This may or may not shift down one here. */
964 if ((*up = *++MARK)) { /* Weed out nulls. */
966 if (!PL_sortcop && !SvPOK(*up)) {
971 (void)sv_2pv(*up, &n_a);
976 max = --up - myorigmark;
981 bool oldcatch = CATCH_GET;
987 PUSHSTACKi(PERLSI_SORT);
988 if (!hasargs && !is_xsub) {
989 if (PL_sortstash != stash || !PL_firstgv || !PL_secondgv) {
990 SAVESPTR(PL_firstgv);
991 SAVESPTR(PL_secondgv);
992 PL_firstgv = gv_fetchpv("a", TRUE, SVt_PV);
993 PL_secondgv = gv_fetchpv("b", TRUE, SVt_PV);
994 PL_sortstash = stash;
997 sv_lock((SV *)PL_firstgv);
998 sv_lock((SV *)PL_secondgv);
1000 SAVESPTR(GvSV(PL_firstgv));
1001 SAVESPTR(GvSV(PL_secondgv));
1004 PUSHBLOCK(cx, CXt_NULL, PL_stack_base);
1005 if (!(PL_op->op_flags & OPf_SPECIAL)) {
1006 cx->cx_type = CXt_SUB;
1007 cx->blk_gimme = G_SCALAR;
1010 (void)SvREFCNT_inc(cv); /* in preparation for POPSUB */
1012 PL_sortcxix = cxstack_ix;
1014 if (hasargs && !is_xsub) {
1015 /* This is mostly copied from pp_entersub */
1016 AV *av = (AV*)PL_curpad[0];
1019 cx->blk_sub.savearray = GvAV(PL_defgv);
1020 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
1021 #endif /* USE_THREADS */
1022 cx->blk_sub.oldcurpad = PL_curpad;
1023 cx->blk_sub.argarray = av;
1025 qsortsv((myorigmark+1), max,
1026 is_xsub ? sortcv_xsub : hasargs ? sortcv_stacked : sortcv);
1028 POPBLOCK(cx,PL_curpm);
1029 PL_stack_sp = newsp;
1031 CATCH_SET(oldcatch);
1036 MEXTEND(SP, 20); /* Can't afford stack realloc on signal. */
1037 qsortsv(ORIGMARK+1, max,
1038 (PL_op->op_private & OPpSORT_NUMERIC)
1039 ? ( (PL_op->op_private & OPpSORT_INTEGER)
1040 ? ( overloading ? amagic_i_ncmp : sv_i_ncmp)
1041 : ( overloading ? amagic_ncmp : sv_ncmp))
1042 : ( IN_LOCALE_RUNTIME
1045 : sv_cmp_locale_static)
1046 : ( overloading ? amagic_cmp : sv_cmp_static)));
1047 if (PL_op->op_private & OPpSORT_REVERSE) {
1048 SV **p = ORIGMARK+1;
1049 SV **q = ORIGMARK+max;
1059 PL_stack_sp = ORIGMARK + max;
1067 if (GIMME == G_ARRAY)
1069 if (SvTRUEx(PAD_SV(PL_op->op_targ)))
1070 return cLOGOP->op_other;
1079 if (GIMME == G_ARRAY) {
1080 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1084 SV *targ = PAD_SV(PL_op->op_targ);
1087 if (PL_op->op_private & OPpFLIP_LINENUM) {
1089 flip = PL_last_in_gv
1090 && (gp_io = GvIO(PL_last_in_gv))
1091 && SvIV(sv) == (IV)IoLINES(gp_io);
1096 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
1097 if (PL_op->op_flags & OPf_SPECIAL) {
1105 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1118 if (GIMME == G_ARRAY) {
1124 if (SvGMAGICAL(left))
1126 if (SvGMAGICAL(right))
1129 if (SvNIOKp(left) || !SvPOKp(left) ||
1130 SvNIOKp(right) || !SvPOKp(right) ||
1131 (looks_like_number(left) && *SvPVX(left) != '0' &&
1132 looks_like_number(right) && *SvPVX(right) != '0'))
1134 if (SvNV(left) < IV_MIN || SvNV(right) > IV_MAX)
1135 DIE(aTHX_ "Range iterator outside integer range");
1146 sv = sv_2mortal(newSViv(i++));
1151 SV *final = sv_mortalcopy(right);
1153 char *tmps = SvPV(final, len);
1155 sv = sv_mortalcopy(left);
1157 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
1159 if (strEQ(SvPVX(sv),tmps))
1161 sv = sv_2mortal(newSVsv(sv));
1168 SV *targ = PAD_SV(cUNOP->op_first->op_targ);
1170 if ((PL_op->op_private & OPpFLIP_LINENUM)
1171 ? (GvIO(PL_last_in_gv)
1172 && SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv)))
1174 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
1175 sv_catpv(targ, "E0");
1186 S_dopoptolabel(pTHX_ char *label)
1189 register PERL_CONTEXT *cx;
1191 for (i = cxstack_ix; i >= 0; i--) {
1193 switch (CxTYPE(cx)) {
1195 if (ckWARN(WARN_EXITING))
1196 Perl_warner(aTHX_ WARN_EXITING, "Exiting substitution via %s",
1197 PL_op_name[PL_op->op_type]);
1200 if (ckWARN(WARN_EXITING))
1201 Perl_warner(aTHX_ WARN_EXITING, "Exiting subroutine via %s",
1202 PL_op_name[PL_op->op_type]);
1205 if (ckWARN(WARN_EXITING))
1206 Perl_warner(aTHX_ WARN_EXITING, "Exiting format via %s",
1207 PL_op_name[PL_op->op_type]);
1210 if (ckWARN(WARN_EXITING))
1211 Perl_warner(aTHX_ WARN_EXITING, "Exiting eval via %s",
1212 PL_op_name[PL_op->op_type]);
1215 if (ckWARN(WARN_EXITING))
1216 Perl_warner(aTHX_ WARN_EXITING, "Exiting pseudo-block via %s",
1217 PL_op_name[PL_op->op_type]);
1220 if (!cx->blk_loop.label ||
1221 strNE(label, cx->blk_loop.label) ) {
1222 DEBUG_l(Perl_deb(aTHX_ "(Skipping label #%ld %s)\n",
1223 (long)i, cx->blk_loop.label));
1226 DEBUG_l( Perl_deb(aTHX_ "(Found label #%ld %s)\n", (long)i, label));
1234 Perl_dowantarray(pTHX)
1236 I32 gimme = block_gimme();
1237 return (gimme == G_VOID) ? G_SCALAR : gimme;
1241 Perl_block_gimme(pTHX)
1245 cxix = dopoptosub(cxstack_ix);
1249 switch (cxstack[cxix].blk_gimme) {
1257 Perl_croak(aTHX_ "panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
1264 Perl_is_lvalue_sub(pTHX)
1268 cxix = dopoptosub(cxstack_ix);
1269 assert(cxix >= 0); /* We should only be called from inside subs */
1271 if (cxstack[cxix].blk_sub.lval && CvLVALUE(cxstack[cxix].blk_sub.cv))
1272 return cxstack[cxix].blk_sub.lval;
1278 S_dopoptosub(pTHX_ I32 startingblock)
1280 return dopoptosub_at(cxstack, startingblock);
1284 S_dopoptosub_at(pTHX_ PERL_CONTEXT *cxstk, I32 startingblock)
1287 register PERL_CONTEXT *cx;
1288 for (i = startingblock; i >= 0; i--) {
1290 switch (CxTYPE(cx)) {
1296 DEBUG_l( Perl_deb(aTHX_ "(Found sub #%ld)\n", (long)i));
1304 S_dopoptoeval(pTHX_ I32 startingblock)
1307 register PERL_CONTEXT *cx;
1308 for (i = startingblock; i >= 0; i--) {
1310 switch (CxTYPE(cx)) {
1314 DEBUG_l( Perl_deb(aTHX_ "(Found eval #%ld)\n", (long)i));
1322 S_dopoptoloop(pTHX_ I32 startingblock)
1325 register PERL_CONTEXT *cx;
1326 for (i = startingblock; i >= 0; i--) {
1328 switch (CxTYPE(cx)) {
1330 if (ckWARN(WARN_EXITING))
1331 Perl_warner(aTHX_ WARN_EXITING, "Exiting substitution via %s",
1332 PL_op_name[PL_op->op_type]);
1335 if (ckWARN(WARN_EXITING))
1336 Perl_warner(aTHX_ WARN_EXITING, "Exiting subroutine via %s",
1337 PL_op_name[PL_op->op_type]);
1340 if (ckWARN(WARN_EXITING))
1341 Perl_warner(aTHX_ WARN_EXITING, "Exiting format via %s",
1342 PL_op_name[PL_op->op_type]);
1345 if (ckWARN(WARN_EXITING))
1346 Perl_warner(aTHX_ WARN_EXITING, "Exiting eval via %s",
1347 PL_op_name[PL_op->op_type]);
1350 if (ckWARN(WARN_EXITING))
1351 Perl_warner(aTHX_ WARN_EXITING, "Exiting pseudo-block via %s",
1352 PL_op_name[PL_op->op_type]);
1355 DEBUG_l( Perl_deb(aTHX_ "(Found loop #%ld)\n", (long)i));
1363 Perl_dounwind(pTHX_ I32 cxix)
1365 register PERL_CONTEXT *cx;
1368 while (cxstack_ix > cxix) {
1370 cx = &cxstack[cxstack_ix];
1371 DEBUG_l(PerlIO_printf(Perl_debug_log, "Unwinding block %ld, type %s\n",
1372 (long) cxstack_ix, PL_block_type[CxTYPE(cx)]));
1373 /* Note: we don't need to restore the base context info till the end. */
1374 switch (CxTYPE(cx)) {
1377 continue; /* not break */
1399 Perl_qerror(pTHX_ SV *err)
1402 sv_catsv(ERRSV, err);
1404 sv_catsv(PL_errors, err);
1406 Perl_warn(aTHX_ "%"SVf, err);
1411 Perl_die_where(pTHX_ char *message, STRLEN msglen)
1416 register PERL_CONTEXT *cx;
1421 if (PL_in_eval & EVAL_KEEPERR) {
1422 static char prefix[] = "\t(in cleanup) ";
1427 else if (SvCUR(err) >= sizeof(prefix)+msglen-1) {
1430 if (*e != *message || strNE(e,message))
1434 SvGROW(err, SvCUR(err)+sizeof(prefix)+msglen);
1435 sv_catpvn(err, prefix, sizeof(prefix)-1);
1436 sv_catpvn(err, message, msglen);
1437 if (ckWARN(WARN_MISC)) {
1438 STRLEN start = SvCUR(err)-msglen-sizeof(prefix)+1;
1439 Perl_warner(aTHX_ WARN_MISC, SvPVX(err)+start);
1444 sv_setpvn(ERRSV, message, msglen);
1448 message = SvPVx(ERRSV, msglen);
1450 while ((cxix = dopoptoeval(cxstack_ix)) < 0
1451 && PL_curstackinfo->si_prev)
1460 if (cxix < cxstack_ix)
1463 POPBLOCK(cx,PL_curpm);
1464 if (CxTYPE(cx) != CXt_EVAL) {
1465 PerlIO_write(Perl_error_log, "panic: die ", 11);
1466 PerlIO_write(Perl_error_log, message, msglen);
1471 if (gimme == G_SCALAR)
1472 *++newsp = &PL_sv_undef;
1473 PL_stack_sp = newsp;
1477 /* LEAVE could clobber PL_curcop (see save_re_context())
1478 * XXX it might be better to find a way to avoid messing with
1479 * PL_curcop in save_re_context() instead, but this is a more
1480 * minimal fix --GSAR */
1481 PL_curcop = cx->blk_oldcop;
1483 if (optype == OP_REQUIRE) {
1484 char* msg = SvPVx(ERRSV, n_a);
1485 DIE(aTHX_ "%sCompilation failed in require",
1486 *msg ? msg : "Unknown error\n");
1488 return pop_return();
1492 message = SvPVx(ERRSV, msglen);
1495 /* SFIO can really mess with your errno */
1498 PerlIO *serr = Perl_error_log;
1500 PerlIO_write(serr, message, msglen);
1501 (void)PerlIO_flush(serr);
1514 if (SvTRUE(left) != SvTRUE(right))
1526 RETURNOP(cLOGOP->op_other);
1535 RETURNOP(cLOGOP->op_other);
1541 register I32 cxix = dopoptosub(cxstack_ix);
1542 register PERL_CONTEXT *cx;
1543 register PERL_CONTEXT *ccstack = cxstack;
1544 PERL_SI *top_si = PL_curstackinfo;
1555 /* we may be in a higher stacklevel, so dig down deeper */
1556 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1557 top_si = top_si->si_prev;
1558 ccstack = top_si->si_cxstack;
1559 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1562 if (GIMME != G_ARRAY)
1566 if (PL_DBsub && cxix >= 0 &&
1567 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
1571 cxix = dopoptosub_at(ccstack, cxix - 1);
1574 cx = &ccstack[cxix];
1575 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1576 dbcxix = dopoptosub_at(ccstack, cxix - 1);
1577 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
1578 field below is defined for any cx. */
1579 if (PL_DBsub && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
1580 cx = &ccstack[dbcxix];
1583 stashname = CopSTASHPV(cx->blk_oldcop);
1584 if (GIMME != G_ARRAY) {
1586 PUSHs(&PL_sv_undef);
1589 sv_setpv(TARG, stashname);
1596 PUSHs(&PL_sv_undef);
1598 PUSHs(sv_2mortal(newSVpv(stashname, 0)));
1599 PUSHs(sv_2mortal(newSVpv(CopFILE(cx->blk_oldcop), 0)));
1600 PUSHs(sv_2mortal(newSViv((I32)CopLINE(cx->blk_oldcop))));
1603 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1604 /* So is ccstack[dbcxix]. */
1606 gv_efullname3(sv, CvGV(ccstack[cxix].blk_sub.cv), Nullch);
1607 PUSHs(sv_2mortal(sv));
1608 PUSHs(sv_2mortal(newSViv((I32)cx->blk_sub.hasargs)));
1611 PUSHs(sv_2mortal(newSVpvn("(eval)",6)));
1612 PUSHs(sv_2mortal(newSViv(0)));
1614 gimme = (I32)cx->blk_gimme;
1615 if (gimme == G_VOID)
1616 PUSHs(&PL_sv_undef);
1618 PUSHs(sv_2mortal(newSViv(gimme & G_ARRAY)));
1619 if (CxTYPE(cx) == CXt_EVAL) {
1621 if (cx->blk_eval.old_op_type == OP_ENTEREVAL) {
1622 PUSHs(cx->blk_eval.cur_text);
1626 else if (cx->blk_eval.old_namesv) {
1627 PUSHs(sv_2mortal(newSVsv(cx->blk_eval.old_namesv)));
1630 /* eval BLOCK (try blocks have old_namesv == 0) */
1632 PUSHs(&PL_sv_undef);
1633 PUSHs(&PL_sv_undef);
1637 PUSHs(&PL_sv_undef);
1638 PUSHs(&PL_sv_undef);
1640 if (CxTYPE(cx) == CXt_SUB && cx->blk_sub.hasargs
1641 && CopSTASH_eq(PL_curcop, PL_debstash))
1643 AV *ary = cx->blk_sub.argarray;
1644 int off = AvARRAY(ary) - AvALLOC(ary);
1648 PL_dbargs = GvAV(gv_AVadd(tmpgv = gv_fetchpv("DB::args", TRUE,
1651 AvREAL_off(PL_dbargs); /* XXX should be REIFY (see av.h) */
1654 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1655 av_extend(PL_dbargs, AvFILLp(ary) + off);
1656 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1657 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
1659 /* XXX only hints propagated via op_private are currently
1660 * visible (others are not easily accessible, since they
1661 * use the global PL_hints) */
1662 PUSHs(sv_2mortal(newSViv((I32)cx->blk_oldcop->op_private &
1663 HINT_PRIVATE_MASK)));
1666 SV * old_warnings = cx->blk_oldcop->cop_warnings ;
1668 if (old_warnings == pWARN_NONE ||
1669 (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0))
1670 mask = newSVpvn(WARN_NONEstring, WARNsize) ;
1671 else if (old_warnings == pWARN_ALL ||
1672 (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON))
1673 mask = newSVpvn(WARN_ALLstring, WARNsize) ;
1675 mask = newSVsv(old_warnings);
1676 PUSHs(sv_2mortal(mask));
1691 sv_reset(tmps, CopSTASH(PL_curcop));
1703 PL_curcop = (COP*)PL_op;
1704 TAINT_NOT; /* Each statement is presumed innocent */
1705 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
1708 if (PL_op->op_private || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace))
1712 register PERL_CONTEXT *cx;
1713 I32 gimme = G_ARRAY;
1720 DIE(aTHX_ "No DB::DB routine defined");
1722 if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG))
1723 /* don't do recursive DB::DB call */
1735 push_return(PL_op->op_next);
1736 PUSHBLOCK(cx, CXt_SUB, SP);
1739 (void)SvREFCNT_inc(cv);
1740 SAVEVPTR(PL_curpad);
1741 PL_curpad = AvARRAY((AV*)*av_fetch(CvPADLIST(cv),1,FALSE));
1742 RETURNOP(CvSTART(cv));
1756 register PERL_CONTEXT *cx;
1757 I32 gimme = GIMME_V;
1759 U32 cxtype = CXt_LOOP;
1768 if (PL_op->op_flags & OPf_SPECIAL) {
1769 svp = &THREADSV(PL_op->op_targ); /* per-thread variable */
1770 SAVEGENERICSV(*svp);
1774 #endif /* USE_THREADS */
1775 if (PL_op->op_targ) {
1776 #ifndef USE_ITHREADS
1777 svp = &PL_curpad[PL_op->op_targ]; /* "my" variable */
1780 SAVEPADSV(PL_op->op_targ);
1781 iterdata = (void*)PL_op->op_targ;
1782 cxtype |= CXp_PADVAR;
1787 svp = &GvSV(gv); /* symbol table variable */
1788 SAVEGENERICSV(*svp);
1791 iterdata = (void*)gv;
1797 PUSHBLOCK(cx, cxtype, SP);
1799 PUSHLOOP(cx, iterdata, MARK);
1801 PUSHLOOP(cx, svp, MARK);
1803 if (PL_op->op_flags & OPf_STACKED) {
1804 cx->blk_loop.iterary = (AV*)SvREFCNT_inc(POPs);
1805 if (SvTYPE(cx->blk_loop.iterary) != SVt_PVAV) {
1807 if (SvNIOKp(sv) || !SvPOKp(sv) ||
1808 SvNIOKp(cx->blk_loop.iterary) || !SvPOKp(cx->blk_loop.iterary) ||
1809 (looks_like_number(sv) && *SvPVX(sv) != '0' &&
1810 looks_like_number((SV*)cx->blk_loop.iterary) &&
1811 *SvPVX(cx->blk_loop.iterary) != '0'))
1813 if (SvNV(sv) < IV_MIN ||
1814 SvNV((SV*)cx->blk_loop.iterary) >= IV_MAX)
1815 DIE(aTHX_ "Range iterator outside integer range");
1816 cx->blk_loop.iterix = SvIV(sv);
1817 cx->blk_loop.itermax = SvIV((SV*)cx->blk_loop.iterary);
1820 cx->blk_loop.iterlval = newSVsv(sv);
1824 cx->blk_loop.iterary = PL_curstack;
1825 AvFILLp(PL_curstack) = SP - PL_stack_base;
1826 cx->blk_loop.iterix = MARK - PL_stack_base;
1835 register PERL_CONTEXT *cx;
1836 I32 gimme = GIMME_V;
1842 PUSHBLOCK(cx, CXt_LOOP, SP);
1843 PUSHLOOP(cx, 0, SP);
1851 register PERL_CONTEXT *cx;
1859 newsp = PL_stack_base + cx->blk_loop.resetsp;
1862 if (gimme == G_VOID)
1864 else if (gimme == G_SCALAR) {
1866 *++newsp = sv_mortalcopy(*SP);
1868 *++newsp = &PL_sv_undef;
1872 *++newsp = sv_mortalcopy(*++mark);
1873 TAINT_NOT; /* Each item is independent */
1879 POPLOOP(cx); /* Stack values are safe: release loop vars ... */
1880 PL_curpm = newpm; /* ... and pop $1 et al */
1892 register PERL_CONTEXT *cx;
1893 bool popsub2 = FALSE;
1894 bool clear_errsv = FALSE;
1901 if (PL_curstackinfo->si_type == PERLSI_SORT) {
1902 if (cxstack_ix == PL_sortcxix
1903 || dopoptosub(cxstack_ix) <= PL_sortcxix)
1905 if (cxstack_ix > PL_sortcxix)
1906 dounwind(PL_sortcxix);
1907 AvARRAY(PL_curstack)[1] = *SP;
1908 PL_stack_sp = PL_stack_base + 1;
1913 cxix = dopoptosub(cxstack_ix);
1915 DIE(aTHX_ "Can't return outside a subroutine");
1916 if (cxix < cxstack_ix)
1920 switch (CxTYPE(cx)) {
1925 if (!(PL_in_eval & EVAL_KEEPERR))
1931 if (optype == OP_REQUIRE &&
1932 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
1934 /* Unassume the success we assumed earlier. */
1935 SV *nsv = cx->blk_eval.old_namesv;
1936 (void)hv_delete(GvHVn(PL_incgv), SvPVX(nsv), SvCUR(nsv), G_DISCARD);
1937 DIE(aTHX_ "%s did not return a true value", SvPVX(nsv));
1944 DIE(aTHX_ "panic: return");
1948 if (gimme == G_SCALAR) {
1951 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
1953 *++newsp = SvREFCNT_inc(*SP);
1958 sv = SvREFCNT_inc(*SP); /* FREETMPS could clobber it */
1960 *++newsp = sv_mortalcopy(sv);
1965 *++newsp = (SvTEMP(*SP)) ? *SP : sv_mortalcopy(*SP);
1968 *++newsp = sv_mortalcopy(*SP);
1971 *++newsp = &PL_sv_undef;
1973 else if (gimme == G_ARRAY) {
1974 while (++MARK <= SP) {
1975 *++newsp = (popsub2 && SvTEMP(*MARK))
1976 ? *MARK : sv_mortalcopy(*MARK);
1977 TAINT_NOT; /* Each item is independent */
1980 PL_stack_sp = newsp;
1982 /* Stack values are safe: */
1984 POPSUB(cx,sv); /* release CV and @_ ... */
1988 PL_curpm = newpm; /* ... and pop $1 et al */
1994 return pop_return();
2001 register PERL_CONTEXT *cx;
2011 if (PL_op->op_flags & OPf_SPECIAL) {
2012 cxix = dopoptoloop(cxstack_ix);
2014 DIE(aTHX_ "Can't \"last\" outside a loop block");
2017 cxix = dopoptolabel(cPVOP->op_pv);
2019 DIE(aTHX_ "Label not found for \"last %s\"", cPVOP->op_pv);
2021 if (cxix < cxstack_ix)
2026 switch (CxTYPE(cx)) {
2029 newsp = PL_stack_base + cx->blk_loop.resetsp;
2030 nextop = cx->blk_loop.last_op->op_next;
2034 nextop = pop_return();
2038 nextop = pop_return();
2042 nextop = pop_return();
2045 DIE(aTHX_ "panic: last");
2049 if (gimme == G_SCALAR) {
2051 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
2052 ? *SP : sv_mortalcopy(*SP);
2054 *++newsp = &PL_sv_undef;
2056 else if (gimme == G_ARRAY) {
2057 while (++MARK <= SP) {
2058 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
2059 ? *MARK : sv_mortalcopy(*MARK);
2060 TAINT_NOT; /* Each item is independent */
2066 /* Stack values are safe: */
2069 POPLOOP(cx); /* release loop vars ... */
2073 POPSUB(cx,sv); /* release CV and @_ ... */
2076 PL_curpm = newpm; /* ... and pop $1 et al */
2086 register PERL_CONTEXT *cx;
2089 if (PL_op->op_flags & OPf_SPECIAL) {
2090 cxix = dopoptoloop(cxstack_ix);
2092 DIE(aTHX_ "Can't \"next\" outside a loop block");
2095 cxix = dopoptolabel(cPVOP->op_pv);
2097 DIE(aTHX_ "Label not found for \"next %s\"", cPVOP->op_pv);
2099 if (cxix < cxstack_ix)
2102 /* clear off anything above the scope we're re-entering, but
2103 * save the rest until after a possible continue block */
2104 inner = PL_scopestack_ix;
2106 if (PL_scopestack_ix < inner)
2107 leave_scope(PL_scopestack[PL_scopestack_ix]);
2108 return cx->blk_loop.next_op;
2114 register PERL_CONTEXT *cx;
2117 if (PL_op->op_flags & OPf_SPECIAL) {
2118 cxix = dopoptoloop(cxstack_ix);
2120 DIE(aTHX_ "Can't \"redo\" outside a loop block");
2123 cxix = dopoptolabel(cPVOP->op_pv);
2125 DIE(aTHX_ "Label not found for \"redo %s\"", cPVOP->op_pv);
2127 if (cxix < cxstack_ix)
2131 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2132 LEAVE_SCOPE(oldsave);
2133 return cx->blk_loop.redo_op;
2137 S_dofindlabel(pTHX_ OP *o, char *label, OP **opstack, OP **oplimit)
2141 static char too_deep[] = "Target of goto is too deeply nested";
2144 Perl_croak(aTHX_ too_deep);
2145 if (o->op_type == OP_LEAVE ||
2146 o->op_type == OP_SCOPE ||
2147 o->op_type == OP_LEAVELOOP ||
2148 o->op_type == OP_LEAVETRY)
2150 *ops++ = cUNOPo->op_first;
2152 Perl_croak(aTHX_ too_deep);
2155 if (o->op_flags & OPf_KIDS) {
2156 /* First try all the kids at this level, since that's likeliest. */
2157 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2158 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
2159 kCOP->cop_label && strEQ(kCOP->cop_label, label))
2162 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2163 if (kid == PL_lastgotoprobe)
2165 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
2167 (ops[-1]->op_type != OP_NEXTSTATE &&
2168 ops[-1]->op_type != OP_DBSTATE)))
2170 if ((o = dofindlabel(kid, label, ops, oplimit)))
2189 register PERL_CONTEXT *cx;
2190 #define GOTO_DEPTH 64
2191 OP *enterops[GOTO_DEPTH];
2193 int do_dump = (PL_op->op_type == OP_DUMP);
2194 static char must_have_label[] = "goto must have label";
2197 if (PL_op->op_flags & OPf_STACKED) {
2201 /* This egregious kludge implements goto &subroutine */
2202 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
2204 register PERL_CONTEXT *cx;
2205 CV* cv = (CV*)SvRV(sv);
2211 if (!CvROOT(cv) && !CvXSUB(cv)) {
2216 /* autoloaded stub? */
2217 if (cv != GvCV(gv) && (cv = GvCV(gv)))
2219 autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv),
2220 GvNAMELEN(gv), FALSE);
2221 if (autogv && (cv = GvCV(autogv)))
2223 tmpstr = sv_newmortal();
2224 gv_efullname3(tmpstr, gv, Nullch);
2225 DIE(aTHX_ "Goto undefined subroutine &%s",SvPVX(tmpstr));
2227 DIE(aTHX_ "Goto undefined subroutine");
2230 /* First do some returnish stuff. */
2231 cxix = dopoptosub(cxstack_ix);
2233 DIE(aTHX_ "Can't goto subroutine outside a subroutine");
2234 if (cxix < cxstack_ix)
2238 DIE(aTHX_ "Can't goto subroutine from an eval-string");
2240 if (CxTYPE(cx) == CXt_SUB && cx->blk_sub.hasargs) {
2241 /* put @_ back onto stack */
2242 AV* av = cx->blk_sub.argarray;
2244 items = AvFILLp(av) + 1;
2246 EXTEND(PL_stack_sp, items); /* @_ could have been extended. */
2247 Copy(AvARRAY(av), PL_stack_sp, items, SV*);
2248 PL_stack_sp += items;
2250 SvREFCNT_dec(GvAV(PL_defgv));
2251 GvAV(PL_defgv) = cx->blk_sub.savearray;
2252 #endif /* USE_THREADS */
2253 /* abandon @_ if it got reified */
2255 (void)sv_2mortal((SV*)av); /* delay until return */
2257 av_extend(av, items-1);
2258 AvFLAGS(av) = AVf_REIFY;
2259 PL_curpad[0] = (SV*)(cx->blk_sub.argarray = av);
2262 else if (CvXSUB(cv)) { /* put GvAV(defgv) back onto stack */
2265 av = (AV*)PL_curpad[0];
2267 av = GvAV(PL_defgv);
2269 items = AvFILLp(av) + 1;
2271 EXTEND(PL_stack_sp, items); /* @_ could have been extended. */
2272 Copy(AvARRAY(av), PL_stack_sp, items, SV*);
2273 PL_stack_sp += items;
2275 if (CxTYPE(cx) == CXt_SUB &&
2276 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
2277 SvREFCNT_dec(cx->blk_sub.cv);
2278 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2279 LEAVE_SCOPE(oldsave);
2281 /* Now do some callish stuff. */
2284 #ifdef PERL_XSUB_OLDSTYLE
2285 if (CvOLDSTYLE(cv)) {
2286 I32 (*fp3)(int,int,int);
2291 fp3 = (I32(*)(int,int,int))CvXSUB(cv);
2292 items = (*fp3)(CvXSUBANY(cv).any_i32,
2293 mark - PL_stack_base + 1,
2295 SP = PL_stack_base + items;
2298 #endif /* PERL_XSUB_OLDSTYLE */
2303 PL_stack_sp--; /* There is no cv arg. */
2304 /* Push a mark for the start of arglist */
2306 (void)(*CvXSUB(cv))(aTHXo_ cv);
2307 /* Pop the current context like a decent sub should */
2308 POPBLOCK(cx, PL_curpm);
2309 /* Do _not_ use PUTBACK, keep the XSUB's return stack! */
2312 return pop_return();
2315 AV* padlist = CvPADLIST(cv);
2316 SV** svp = AvARRAY(padlist);
2317 if (CxTYPE(cx) == CXt_EVAL) {
2318 PL_in_eval = cx->blk_eval.old_in_eval;
2319 PL_eval_root = cx->blk_eval.old_eval_root;
2320 cx->cx_type = CXt_SUB;
2321 cx->blk_sub.hasargs = 0;
2323 cx->blk_sub.cv = cv;
2324 cx->blk_sub.olddepth = CvDEPTH(cv);
2326 if (CvDEPTH(cv) < 2)
2327 (void)SvREFCNT_inc(cv);
2328 else { /* save temporaries on recursion? */
2329 if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION))
2330 sub_crush_depth(cv);
2331 if (CvDEPTH(cv) > AvFILLp(padlist)) {
2332 AV *newpad = newAV();
2333 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
2334 I32 ix = AvFILLp((AV*)svp[1]);
2335 I32 names_fill = AvFILLp((AV*)svp[0]);
2336 svp = AvARRAY(svp[0]);
2337 for ( ;ix > 0; ix--) {
2338 if (names_fill >= ix && svp[ix] != &PL_sv_undef) {
2339 char *name = SvPVX(svp[ix]);
2340 if ((SvFLAGS(svp[ix]) & SVf_FAKE)
2343 /* outer lexical or anon code */
2344 av_store(newpad, ix,
2345 SvREFCNT_inc(oldpad[ix]) );
2347 else { /* our own lexical */
2349 av_store(newpad, ix, sv = (SV*)newAV());
2350 else if (*name == '%')
2351 av_store(newpad, ix, sv = (SV*)newHV());
2353 av_store(newpad, ix, sv = NEWSV(0,0));
2357 else if (IS_PADGV(oldpad[ix]) || IS_PADCONST(oldpad[ix])) {
2358 av_store(newpad, ix, sv = SvREFCNT_inc(oldpad[ix]));
2361 av_store(newpad, ix, sv = NEWSV(0,0));
2365 if (cx->blk_sub.hasargs) {
2368 av_store(newpad, 0, (SV*)av);
2369 AvFLAGS(av) = AVf_REIFY;
2371 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
2372 AvFILLp(padlist) = CvDEPTH(cv);
2373 svp = AvARRAY(padlist);
2377 if (!cx->blk_sub.hasargs) {
2378 AV* av = (AV*)PL_curpad[0];
2380 items = AvFILLp(av) + 1;
2382 /* Mark is at the end of the stack. */
2384 Copy(AvARRAY(av), SP + 1, items, SV*);
2389 #endif /* USE_THREADS */
2390 SAVEVPTR(PL_curpad);
2391 PL_curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
2393 if (cx->blk_sub.hasargs)
2394 #endif /* USE_THREADS */
2396 AV* av = (AV*)PL_curpad[0];
2400 cx->blk_sub.savearray = GvAV(PL_defgv);
2401 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
2402 #endif /* USE_THREADS */
2403 cx->blk_sub.oldcurpad = PL_curpad;
2404 cx->blk_sub.argarray = av;
2407 if (items >= AvMAX(av) + 1) {
2409 if (AvARRAY(av) != ary) {
2410 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2411 SvPVX(av) = (char*)ary;
2413 if (items >= AvMAX(av) + 1) {
2414 AvMAX(av) = items - 1;
2415 Renew(ary,items+1,SV*);
2417 SvPVX(av) = (char*)ary;
2420 Copy(mark,AvARRAY(av),items,SV*);
2421 AvFILLp(av) = items - 1;
2422 assert(!AvREAL(av));
2429 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
2431 * We do not care about using sv to call CV;
2432 * it's for informational purposes only.
2434 SV *sv = GvSV(PL_DBsub);
2437 if (PERLDB_SUB_NN) {
2438 SvIVX(sv) = PTR2IV(cv); /* Already upgraded, saved */
2441 gv_efullname3(sv, CvGV(cv), Nullch);
2444 && (gotocv = get_cv("DB::goto", FALSE)) ) {
2445 PUSHMARK( PL_stack_sp );
2446 call_sv((SV*)gotocv, G_SCALAR | G_NODEBUG);
2450 RETURNOP(CvSTART(cv));
2454 label = SvPV(sv,n_a);
2455 if (!(do_dump || *label))
2456 DIE(aTHX_ must_have_label);
2459 else if (PL_op->op_flags & OPf_SPECIAL) {
2461 DIE(aTHX_ must_have_label);
2464 label = cPVOP->op_pv;
2466 if (label && *label) {
2468 bool leaving_eval = FALSE;
2469 PERL_CONTEXT *last_eval_cx = 0;
2473 PL_lastgotoprobe = 0;
2475 for (ix = cxstack_ix; ix >= 0; ix--) {
2477 switch (CxTYPE(cx)) {
2479 leaving_eval = TRUE;
2480 if (CxREALEVAL(cx)) {
2481 gotoprobe = (last_eval_cx ?
2482 last_eval_cx->blk_eval.old_eval_root :
2487 /* else fall through */
2489 gotoprobe = cx->blk_oldcop->op_sibling;
2495 gotoprobe = cx->blk_oldcop->op_sibling;
2497 gotoprobe = PL_main_root;
2500 if (CvDEPTH(cx->blk_sub.cv)) {
2501 gotoprobe = CvROOT(cx->blk_sub.cv);
2507 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
2510 DIE(aTHX_ "panic: goto");
2511 gotoprobe = PL_main_root;
2515 retop = dofindlabel(gotoprobe, label,
2516 enterops, enterops + GOTO_DEPTH);
2520 PL_lastgotoprobe = gotoprobe;
2523 DIE(aTHX_ "Can't find label %s", label);
2525 /* if we're leaving an eval, check before we pop any frames
2526 that we're not going to punt, otherwise the error
2529 if (leaving_eval && *enterops && enterops[1]) {
2531 for (i = 1; enterops[i]; i++)
2532 if (enterops[i]->op_type == OP_ENTERITER)
2533 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2536 /* pop unwanted frames */
2538 if (ix < cxstack_ix) {
2545 oldsave = PL_scopestack[PL_scopestack_ix];
2546 LEAVE_SCOPE(oldsave);
2549 /* push wanted frames */
2551 if (*enterops && enterops[1]) {
2553 for (ix = 1; enterops[ix]; ix++) {
2554 PL_op = enterops[ix];
2555 /* Eventually we may want to stack the needed arguments
2556 * for each op. For now, we punt on the hard ones. */
2557 if (PL_op->op_type == OP_ENTERITER)
2558 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2559 CALL_FPTR(PL_op->op_ppaddr)(aTHX);
2567 if (!retop) retop = PL_main_start;
2569 PL_restartop = retop;
2570 PL_do_undump = TRUE;
2574 PL_restartop = 0; /* hmm, must be GNU unexec().. */
2575 PL_do_undump = FALSE;
2591 if (anum == 1 && (PL_op->op_private & OPpEXIT_VMSISH))
2595 PL_exit_flags |= PERL_EXIT_EXPECTED;
2597 PUSHs(&PL_sv_undef);
2605 NV value = SvNVx(GvSV(cCOP->cop_gv));
2606 register I32 match = I_32(value);
2609 if (((NV)match) > value)
2610 --match; /* was fractional--truncate other way */
2612 match -= cCOP->uop.scop.scop_offset;
2615 else if (match > cCOP->uop.scop.scop_max)
2616 match = cCOP->uop.scop.scop_max;
2617 PL_op = cCOP->uop.scop.scop_next[match];
2627 PL_op = PL_op->op_next; /* can't assume anything */
2630 match = *(SvPVx(GvSV(cCOP->cop_gv), n_a)) & 255;
2631 match -= cCOP->uop.scop.scop_offset;
2634 else if (match > cCOP->uop.scop.scop_max)
2635 match = cCOP->uop.scop.scop_max;
2636 PL_op = cCOP->uop.scop.scop_next[match];
2645 S_save_lines(pTHX_ AV *array, SV *sv)
2647 register char *s = SvPVX(sv);
2648 register char *send = SvPVX(sv) + SvCUR(sv);
2650 register I32 line = 1;
2652 while (s && s < send) {
2653 SV *tmpstr = NEWSV(85,0);
2655 sv_upgrade(tmpstr, SVt_PVMG);
2656 t = strchr(s, '\n');
2662 sv_setpvn(tmpstr, s, t - s);
2663 av_store(array, line++, tmpstr);
2668 #ifdef PERL_FLEXIBLE_EXCEPTIONS
2670 S_docatch_body(pTHX_ va_list args)
2672 return docatch_body();
2677 S_docatch_body(pTHX)
2684 S_docatch(pTHX_ OP *o)
2688 volatile PERL_SI *cursi = PL_curstackinfo;
2692 assert(CATCH_GET == TRUE);
2695 #ifdef PERL_FLEXIBLE_EXCEPTIONS
2697 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_docatch_body));
2703 #ifndef PERL_FLEXIBLE_EXCEPTIONS
2709 if (PL_restartop && cursi == PL_curstackinfo) {
2710 PL_op = PL_restartop;
2727 Perl_sv_compile_2op(pTHX_ SV *sv, OP** startop, char *code, AV** avp)
2728 /* sv Text to convert to OP tree. */
2729 /* startop op_free() this to undo. */
2730 /* code Short string id of the caller. */
2732 dSP; /* Make POPBLOCK work. */
2735 I32 gimme = 0; /* SUSPECT - INITIALZE TO WHAT? NI-S */
2739 char tbuf[TYPE_DIGITS(long) + 12 + 10];
2740 char *tmpbuf = tbuf;
2746 /* switch to eval mode */
2748 if (PL_curcop == &PL_compiling) {
2749 SAVECOPSTASH_FREE(&PL_compiling);
2750 CopSTASH_set(&PL_compiling, PL_curstash);
2752 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
2753 SV *sv = sv_newmortal();
2754 Perl_sv_setpvf(aTHX_ sv, "_<(%.10seval %lu)[%s:%"IVdf"]",
2755 code, (unsigned long)++PL_evalseq,
2756 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
2760 sprintf(tmpbuf, "_<(%.10s_eval %lu)", code, (unsigned long)++PL_evalseq);
2761 SAVECOPFILE_FREE(&PL_compiling);
2762 CopFILE_set(&PL_compiling, tmpbuf+2);
2763 SAVECOPLINE(&PL_compiling);
2764 CopLINE_set(&PL_compiling, 1);
2765 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2766 deleting the eval's FILEGV from the stash before gv_check() runs
2767 (i.e. before run-time proper). To work around the coredump that
2768 ensues, we always turn GvMULTI_on for any globals that were
2769 introduced within evals. See force_ident(). GSAR 96-10-12 */
2770 safestr = savepv(tmpbuf);
2771 SAVEDELETE(PL_defstash, safestr, strlen(safestr));
2773 #ifdef OP_IN_REGISTER
2778 PL_hints &= HINT_UTF8;
2781 PL_op->op_type = OP_ENTEREVAL;
2782 PL_op->op_flags = 0; /* Avoid uninit warning. */
2783 PUSHBLOCK(cx, CXt_EVAL|(PL_curcop == &PL_compiling ? 0 : CXp_REAL), SP);
2784 PUSHEVAL(cx, 0, Nullgv);
2785 rop = doeval(G_SCALAR, startop);
2786 POPBLOCK(cx,PL_curpm);
2789 (*startop)->op_type = OP_NULL;
2790 (*startop)->op_ppaddr = PL_ppaddr[OP_NULL];
2792 *avp = (AV*)SvREFCNT_inc(PL_comppad);
2794 if (PL_curcop == &PL_compiling)
2795 PL_compiling.op_private = PL_hints;
2796 #ifdef OP_IN_REGISTER
2802 /* With USE_THREADS, eval_owner must be held on entry to doeval */
2804 S_doeval(pTHX_ int gimme, OP** startop)
2812 PL_in_eval = ((saveop && saveop->op_type == OP_REQUIRE)
2813 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
2818 /* set up a scratch pad */
2821 SAVEVPTR(PL_curpad);
2822 SAVESPTR(PL_comppad);
2823 SAVESPTR(PL_comppad_name);
2824 SAVEI32(PL_comppad_name_fill);
2825 SAVEI32(PL_min_intro_pending);
2826 SAVEI32(PL_max_intro_pending);
2829 for (i = cxstack_ix - 1; i >= 0; i--) {
2830 PERL_CONTEXT *cx = &cxstack[i];
2831 if (CxTYPE(cx) == CXt_EVAL)
2833 else if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
2834 caller = cx->blk_sub.cv;
2839 SAVESPTR(PL_compcv);
2840 PL_compcv = (CV*)NEWSV(1104,0);
2841 sv_upgrade((SV *)PL_compcv, SVt_PVCV);
2842 CvEVAL_on(PL_compcv);
2843 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
2844 cxstack[cxstack_ix].blk_eval.cv = PL_compcv;
2847 CvOWNER(PL_compcv) = 0;
2848 New(666, CvMUTEXP(PL_compcv), 1, perl_mutex);
2849 MUTEX_INIT(CvMUTEXP(PL_compcv));
2850 #endif /* USE_THREADS */
2852 PL_comppad = newAV();
2853 av_push(PL_comppad, Nullsv);
2854 PL_curpad = AvARRAY(PL_comppad);
2855 PL_comppad_name = newAV();
2856 PL_comppad_name_fill = 0;
2857 PL_min_intro_pending = 0;
2860 av_store(PL_comppad_name, 0, newSVpvn("@_", 2));
2861 PL_curpad[0] = (SV*)newAV();
2862 SvPADMY_on(PL_curpad[0]); /* XXX Needed? */
2863 #endif /* USE_THREADS */
2865 comppadlist = newAV();
2866 AvREAL_off(comppadlist);
2867 av_store(comppadlist, 0, (SV*)PL_comppad_name);
2868 av_store(comppadlist, 1, (SV*)PL_comppad);
2869 CvPADLIST(PL_compcv) = comppadlist;
2872 (saveop->op_type != OP_REQUIRE && saveop->op_type != OP_DOFILE))
2874 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc(caller);
2877 SAVEMORTALIZESV(PL_compcv); /* must remain until end of current statement */
2879 /* make sure we compile in the right package */
2881 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
2882 SAVESPTR(PL_curstash);
2883 PL_curstash = CopSTASH(PL_curcop);
2885 SAVESPTR(PL_beginav);
2886 PL_beginav = newAV();
2887 SAVEFREESV(PL_beginav);
2888 SAVEI32(PL_error_count);
2890 /* try to compile it */
2892 PL_eval_root = Nullop;
2894 PL_curcop = &PL_compiling;
2895 PL_curcop->cop_arybase = 0;
2896 SvREFCNT_dec(PL_rs);
2897 PL_rs = newSVpvn("\n", 1);
2898 if (saveop && saveop->op_flags & OPf_SPECIAL)
2899 PL_in_eval |= EVAL_KEEPERR;
2902 if (yyparse() || PL_error_count || !PL_eval_root) {
2906 I32 optype = 0; /* Might be reset by POPEVAL. */
2911 op_free(PL_eval_root);
2912 PL_eval_root = Nullop;
2914 SP = PL_stack_base + POPMARK; /* pop original mark */
2916 POPBLOCK(cx,PL_curpm);
2922 if (optype == OP_REQUIRE) {
2923 char* msg = SvPVx(ERRSV, n_a);
2924 DIE(aTHX_ "%sCompilation failed in require",
2925 *msg ? msg : "Unknown error\n");
2928 char* msg = SvPVx(ERRSV, n_a);
2930 POPBLOCK(cx,PL_curpm);
2932 Perl_croak(aTHX_ "%sCompilation failed in regexp",
2933 (*msg ? msg : "Unknown error\n"));
2935 SvREFCNT_dec(PL_rs);
2936 PL_rs = SvREFCNT_inc(PL_nrs);
2938 MUTEX_LOCK(&PL_eval_mutex);
2940 COND_SIGNAL(&PL_eval_cond);
2941 MUTEX_UNLOCK(&PL_eval_mutex);
2942 #endif /* USE_THREADS */
2945 SvREFCNT_dec(PL_rs);
2946 PL_rs = SvREFCNT_inc(PL_nrs);
2947 CopLINE_set(&PL_compiling, 0);
2949 *startop = PL_eval_root;
2950 SvREFCNT_dec(CvOUTSIDE(PL_compcv));
2951 CvOUTSIDE(PL_compcv) = Nullcv;
2953 SAVEFREEOP(PL_eval_root);
2955 scalarvoid(PL_eval_root);
2956 else if (gimme & G_ARRAY)
2959 scalar(PL_eval_root);
2961 DEBUG_x(dump_eval());
2963 /* Register with debugger: */
2964 if (PERLDB_INTER && saveop->op_type == OP_REQUIRE) {
2965 CV *cv = get_cv("DB::postponed", FALSE);
2969 XPUSHs((SV*)CopFILEGV(&PL_compiling));
2971 call_sv((SV*)cv, G_DISCARD);
2975 /* compiled okay, so do it */
2977 CvDEPTH(PL_compcv) = 1;
2978 SP = PL_stack_base + POPMARK; /* pop original mark */
2979 PL_op = saveop; /* The caller may need it. */
2980 PL_lex_state = LEX_NOTPARSING; /* $^S needs this. */
2982 MUTEX_LOCK(&PL_eval_mutex);
2984 COND_SIGNAL(&PL_eval_cond);
2985 MUTEX_UNLOCK(&PL_eval_mutex);
2986 #endif /* USE_THREADS */
2988 RETURNOP(PL_eval_start);
2992 S_doopen_pmc(pTHX_ const char *name, const char *mode)
2994 STRLEN namelen = strlen(name);
2997 if (namelen > 3 && strEQ(name + namelen - 3, ".pm")) {
2998 SV *pmcsv = Perl_newSVpvf(aTHX_ "%s%c", name, 'c');
2999 char *pmc = SvPV_nolen(pmcsv);
3002 if (PerlLIO_stat(pmc, &pmcstat) < 0) {
3003 fp = PerlIO_open(name, mode);
3006 if (PerlLIO_stat(name, &pmstat) < 0 ||
3007 pmstat.st_mtime < pmcstat.st_mtime)
3009 fp = PerlIO_open(pmc, mode);
3012 fp = PerlIO_open(name, mode);
3015 SvREFCNT_dec(pmcsv);
3018 fp = PerlIO_open(name, mode);
3026 register PERL_CONTEXT *cx;
3030 char *tryname = Nullch;
3031 SV *namesv = Nullsv;
3033 I32 gimme = GIMME_V;
3034 PerlIO *tryrsfp = 0;
3036 int filter_has_file = 0;
3037 GV *filter_child_proc = 0;
3038 SV *filter_state = 0;
3043 if (SvPOK(sv) && SvNOK(sv) && SvNV(sv)) { /* require v5.6.1 */
3044 UV rev = 0, ver = 0, sver = 0;
3046 U8 *s = (U8*)SvPVX(sv);
3047 U8 *end = (U8*)SvPVX(sv) + SvCUR(sv);
3049 rev = utf8n_to_uvchr(s, end - s, &len, 0);
3052 ver = utf8n_to_uvchr(s, end - s, &len, 0);
3055 sver = utf8n_to_uvchr(s, end - s, &len, 0);
3058 if (PERL_REVISION < rev
3059 || (PERL_REVISION == rev
3060 && (PERL_VERSION < ver
3061 || (PERL_VERSION == ver
3062 && PERL_SUBVERSION < sver))))
3064 DIE(aTHX_ "Perl v%"UVuf".%"UVuf".%"UVuf" required--this is only "
3065 "v%d.%d.%d, stopped", rev, ver, sver, PERL_REVISION,
3066 PERL_VERSION, PERL_SUBVERSION);
3070 else if (!SvPOKp(sv)) { /* require 5.005_03 */
3071 if ((NV)PERL_REVISION + ((NV)PERL_VERSION/(NV)1000)
3072 + ((NV)PERL_SUBVERSION/(NV)1000000)
3073 + 0.00000099 < SvNV(sv))
3077 NV nver = (nrev - rev) * 1000;
3078 UV ver = (UV)(nver + 0.0009);
3079 NV nsver = (nver - ver) * 1000;
3080 UV sver = (UV)(nsver + 0.0009);
3082 /* help out with the "use 5.6" confusion */
3083 if (sver == 0 && (rev > 5 || (rev == 5 && ver >= 100))) {
3084 DIE(aTHX_ "Perl v%"UVuf".%"UVuf".%"UVuf" required--"
3085 "this is only v%d.%d.%d, stopped"
3086 " (did you mean v%"UVuf".%"UVuf".0?)",
3087 rev, ver, sver, PERL_REVISION, PERL_VERSION,
3088 PERL_SUBVERSION, rev, ver/100);
3091 DIE(aTHX_ "Perl v%"UVuf".%"UVuf".%"UVuf" required--"
3092 "this is only v%d.%d.%d, stopped",
3093 rev, ver, sver, PERL_REVISION, PERL_VERSION,
3100 name = SvPV(sv, len);
3101 if (!(name && len > 0 && *name))
3102 DIE(aTHX_ "Null filename used");
3103 TAINT_PROPER("require");
3104 if (PL_op->op_type == OP_REQUIRE &&
3105 (svp = hv_fetch(GvHVn(PL_incgv), name, len, 0)) &&
3106 *svp != &PL_sv_undef)
3109 /* prepare to compile file */
3111 #ifdef MACOS_TRADITIONAL
3112 if (PERL_FILE_IS_ABSOLUTE(name)
3113 || (*name == ':' && name[1] != ':' && strchr(name+2, ':')))
3116 tryrsfp = doopen_pmc(name,PERL_SCRIPT_MODE);
3117 /* We consider paths of the form :a:b ambiguous and interpret them first
3118 as global then as local
3120 if (!tryrsfp && *name == ':' && name[1] != ':' && strchr(name+2, ':'))
3126 if (PERL_FILE_IS_ABSOLUTE(name)
3127 || (*name == '.' && (name[1] == '/' ||
3128 (name[1] == '.' && name[2] == '/'))))
3131 tryrsfp = doopen_pmc(name,PERL_SCRIPT_MODE);
3135 AV *ar = GvAVn(PL_incgv);
3139 if ((unixname = tounixspec(name, Nullch)) != Nullch)
3142 namesv = NEWSV(806, 0);
3143 for (i = 0; i <= AvFILL(ar); i++) {
3144 SV *dirsv = *av_fetch(ar, i, TRUE);
3150 if (SvTYPE(SvRV(loader)) == SVt_PVAV) {
3151 loader = *av_fetch((AV *)SvRV(loader), 0, TRUE);
3154 Perl_sv_setpvf(aTHX_ namesv, "/loader/0x%"UVxf"/%s",
3155 PTR2UV(SvANY(loader)), name);
3156 tryname = SvPVX(namesv);
3167 if (sv_isobject(loader))
3168 count = call_method("INC", G_ARRAY);
3170 count = call_sv(loader, G_ARRAY);
3180 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVGV) {
3184 if (SvTYPE(arg) == SVt_PVGV) {
3185 IO *io = GvIO((GV *)arg);
3190 tryrsfp = IoIFP(io);
3191 if (IoTYPE(io) == IoTYPE_PIPE) {
3192 /* reading from a child process doesn't
3193 nest -- when returning from reading
3194 the inner module, the outer one is
3195 unreadable (closed?) I've tried to
3196 save the gv to manage the lifespan of
3197 the pipe, but this didn't help. XXX */
3198 filter_child_proc = (GV *)arg;
3199 (void)SvREFCNT_inc(filter_child_proc);
3202 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
3203 PerlIO_close(IoOFP(io));
3215 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVCV) {
3217 (void)SvREFCNT_inc(filter_sub);
3220 filter_state = SP[i];
3221 (void)SvREFCNT_inc(filter_state);
3225 tryrsfp = PerlIO_open("/dev/null",
3239 filter_has_file = 0;
3240 if (filter_child_proc) {
3241 SvREFCNT_dec(filter_child_proc);
3242 filter_child_proc = 0;
3245 SvREFCNT_dec(filter_state);
3249 SvREFCNT_dec(filter_sub);
3254 char *dir = SvPVx(dirsv, n_a);
3255 #ifdef MACOS_TRADITIONAL
3257 Perl_sv_setpvf(aTHX_ namesv, "%s%s", MacPerl_CanonDir(dir, buf), name+(name[0] == ':'));
3261 if ((unixdir = tounixpath(dir, Nullch)) == Nullch)
3263 sv_setpv(namesv, unixdir);
3264 sv_catpv(namesv, unixname);
3266 Perl_sv_setpvf(aTHX_ namesv, "%s/%s", dir, name);
3269 TAINT_PROPER("require");
3270 tryname = SvPVX(namesv);
3271 #ifdef MACOS_TRADITIONAL
3273 /* Convert slashes in the name part, but not the directory part, to colons */
3275 for (colon = tryname+strlen(dir); colon = strchr(colon, '/'); )
3279 tryrsfp = doopen_pmc(tryname, PERL_SCRIPT_MODE);
3281 if (tryname[0] == '.' && tryname[1] == '/')
3289 SAVECOPFILE_FREE(&PL_compiling);
3290 CopFILE_set(&PL_compiling, tryrsfp ? tryname : name);
3291 SvREFCNT_dec(namesv);
3293 if (PL_op->op_type == OP_REQUIRE) {
3294 char *msgstr = name;
3295 if (namesv) { /* did we lookup @INC? */
3296 SV *msg = sv_2mortal(newSVpv(msgstr,0));
3297 SV *dirmsgsv = NEWSV(0, 0);
3298 AV *ar = GvAVn(PL_incgv);
3300 sv_catpvn(msg, " in @INC", 8);
3301 if (instr(SvPVX(msg), ".h "))
3302 sv_catpv(msg, " (change .h to .ph maybe?)");
3303 if (instr(SvPVX(msg), ".ph "))
3304 sv_catpv(msg, " (did you run h2ph?)");
3305 sv_catpv(msg, " (@INC contains:");
3306 for (i = 0; i <= AvFILL(ar); i++) {
3307 char *dir = SvPVx(*av_fetch(ar, i, TRUE), n_a);
3308 Perl_sv_setpvf(aTHX_ dirmsgsv, " %s", dir);
3309 sv_catsv(msg, dirmsgsv);
3311 sv_catpvn(msg, ")", 1);
3312 SvREFCNT_dec(dirmsgsv);
3313 msgstr = SvPV_nolen(msg);
3315 DIE(aTHX_ "Can't locate %s", msgstr);
3321 SETERRNO(0, SS$_NORMAL);
3323 /* Assume success here to prevent recursive requirement. */
3324 (void)hv_store(GvHVn(PL_incgv), name, strlen(name),
3325 newSVpv(CopFILE(&PL_compiling), 0), 0 );
3329 lex_start(sv_2mortal(newSVpvn("",0)));
3330 SAVEGENERICSV(PL_rsfp_filters);
3331 PL_rsfp_filters = Nullav;
3336 SAVESPTR(PL_compiling.cop_warnings);
3337 if (PL_dowarn & G_WARN_ALL_ON)
3338 PL_compiling.cop_warnings = pWARN_ALL ;
3339 else if (PL_dowarn & G_WARN_ALL_OFF)
3340 PL_compiling.cop_warnings = pWARN_NONE ;
3342 PL_compiling.cop_warnings = pWARN_STD ;
3343 SAVESPTR(PL_compiling.cop_io);
3344 PL_compiling.cop_io = Nullsv;
3346 if (filter_sub || filter_child_proc) {
3347 SV *datasv = filter_add(run_user_filter, Nullsv);
3348 IoLINES(datasv) = filter_has_file;
3349 IoFMT_GV(datasv) = (GV *)filter_child_proc;
3350 IoTOP_GV(datasv) = (GV *)filter_state;
3351 IoBOTTOM_GV(datasv) = (GV *)filter_sub;
3354 /* switch to eval mode */
3355 push_return(PL_op->op_next);
3356 PUSHBLOCK(cx, CXt_EVAL, SP);
3357 PUSHEVAL(cx, name, Nullgv);
3359 SAVECOPLINE(&PL_compiling);
3360 CopLINE_set(&PL_compiling, 0);
3364 MUTEX_LOCK(&PL_eval_mutex);
3365 if (PL_eval_owner && PL_eval_owner != thr)
3366 while (PL_eval_owner)
3367 COND_WAIT(&PL_eval_cond, &PL_eval_mutex);
3368 PL_eval_owner = thr;
3369 MUTEX_UNLOCK(&PL_eval_mutex);
3370 #endif /* USE_THREADS */
3371 return DOCATCH(doeval(gimme, NULL));
3376 return pp_require();
3382 register PERL_CONTEXT *cx;
3384 I32 gimme = GIMME_V, was = PL_sub_generation;
3385 char tbuf[TYPE_DIGITS(long) + 12];
3386 char *tmpbuf = tbuf;
3391 if (!SvPV(sv,len) || !len)
3393 TAINT_PROPER("eval");
3399 /* switch to eval mode */
3401 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
3402 SV *sv = sv_newmortal();
3403 Perl_sv_setpvf(aTHX_ sv, "_<(eval %lu)[%s:%"IVdf"]",
3404 (unsigned long)++PL_evalseq,
3405 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3409 sprintf(tmpbuf, "_<(eval %lu)", (unsigned long)++PL_evalseq);
3410 SAVECOPFILE_FREE(&PL_compiling);
3411 CopFILE_set(&PL_compiling, tmpbuf+2);
3412 SAVECOPLINE(&PL_compiling);
3413 CopLINE_set(&PL_compiling, 1);
3414 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3415 deleting the eval's FILEGV from the stash before gv_check() runs
3416 (i.e. before run-time proper). To work around the coredump that
3417 ensues, we always turn GvMULTI_on for any globals that were
3418 introduced within evals. See force_ident(). GSAR 96-10-12 */
3419 safestr = savepv(tmpbuf);
3420 SAVEDELETE(PL_defstash, safestr, strlen(safestr));
3422 PL_hints = PL_op->op_targ;
3423 SAVESPTR(PL_compiling.cop_warnings);
3424 if (specialWARN(PL_curcop->cop_warnings))
3425 PL_compiling.cop_warnings = PL_curcop->cop_warnings;
3427 PL_compiling.cop_warnings = newSVsv(PL_curcop->cop_warnings);
3428 SAVEFREESV(PL_compiling.cop_warnings);
3430 SAVESPTR(PL_compiling.cop_io);
3431 if (specialCopIO(PL_curcop->cop_io))
3432 PL_compiling.cop_io = PL_curcop->cop_io;
3434 PL_compiling.cop_io = newSVsv(PL_curcop->cop_io);
3435 SAVEFREESV(PL_compiling.cop_io);
3438 push_return(PL_op->op_next);
3439 PUSHBLOCK(cx, (CXt_EVAL|CXp_REAL), SP);
3440 PUSHEVAL(cx, 0, Nullgv);
3442 /* prepare to compile string */
3444 if (PERLDB_LINE && PL_curstash != PL_debstash)
3445 save_lines(CopFILEAV(&PL_compiling), PL_linestr);
3448 MUTEX_LOCK(&PL_eval_mutex);
3449 if (PL_eval_owner && PL_eval_owner != thr)
3450 while (PL_eval_owner)
3451 COND_WAIT(&PL_eval_cond, &PL_eval_mutex);
3452 PL_eval_owner = thr;
3453 MUTEX_UNLOCK(&PL_eval_mutex);
3454 #endif /* USE_THREADS */
3455 ret = doeval(gimme, NULL);
3456 if (PERLDB_INTER && was != PL_sub_generation /* Some subs defined here. */
3457 && ret != PL_op->op_next) { /* Successive compilation. */
3458 strcpy(safestr, "_<(eval )"); /* Anything fake and short. */
3460 return DOCATCH(ret);
3470 register PERL_CONTEXT *cx;
3472 U8 save_flags = PL_op -> op_flags;
3477 retop = pop_return();
3480 if (gimme == G_VOID)
3482 else if (gimme == G_SCALAR) {
3485 if (SvFLAGS(TOPs) & SVs_TEMP)
3488 *MARK = sv_mortalcopy(TOPs);
3492 *MARK = &PL_sv_undef;
3497 /* in case LEAVE wipes old return values */
3498 for (mark = newsp + 1; mark <= SP; mark++) {
3499 if (!(SvFLAGS(*mark) & SVs_TEMP)) {
3500 *mark = sv_mortalcopy(*mark);
3501 TAINT_NOT; /* Each item is independent */
3505 PL_curpm = newpm; /* Don't pop $1 et al till now */
3508 assert(CvDEPTH(PL_compcv) == 1);
3510 CvDEPTH(PL_compcv) = 0;
3513 if (optype == OP_REQUIRE &&
3514 !(gimme == G_SCALAR ? SvTRUE(*SP) : SP > newsp))
3516 /* Unassume the success we assumed earlier. */
3517 SV *nsv = cx->blk_eval.old_namesv;
3518 (void)hv_delete(GvHVn(PL_incgv), SvPVX(nsv), SvCUR(nsv), G_DISCARD);
3519 retop = Perl_die(aTHX_ "%s did not return a true value", SvPVX(nsv));
3520 /* die_where() did LEAVE, or we won't be here */
3524 if (!(save_flags & OPf_SPECIAL))
3534 register PERL_CONTEXT *cx;
3535 I32 gimme = GIMME_V;
3540 push_return(cLOGOP->op_other->op_next);
3541 PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), SP);
3544 PL_in_eval = EVAL_INEVAL;
3547 return DOCATCH(PL_op->op_next);
3557 register PERL_CONTEXT *cx;
3565 if (gimme == G_VOID)
3567 else if (gimme == G_SCALAR) {
3570 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
3573 *MARK = sv_mortalcopy(TOPs);
3577 *MARK = &PL_sv_undef;
3582 /* in case LEAVE wipes old return values */
3583 for (mark = newsp + 1; mark <= SP; mark++) {
3584 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
3585 *mark = sv_mortalcopy(*mark);
3586 TAINT_NOT; /* Each item is independent */
3590 PL_curpm = newpm; /* Don't pop $1 et al till now */
3598 S_doparseform(pTHX_ SV *sv)
3601 register char *s = SvPV_force(sv, len);
3602 register char *send = s + len;
3603 register char *base = Nullch;
3604 register I32 skipspaces = 0;
3605 bool noblank = FALSE;
3606 bool repeat = FALSE;
3607 bool postspace = FALSE;
3615 Perl_croak(aTHX_ "Null picture in formline");
3617 New(804, fops, (send - s)*3+10, U16); /* Almost certainly too long... */
3622 *fpc++ = FF_LINEMARK;
3623 noblank = repeat = FALSE;
3641 case ' ': case '\t':
3652 *fpc++ = FF_LITERAL;
3660 *fpc++ = skipspaces;
3664 *fpc++ = FF_NEWLINE;
3668 arg = fpc - linepc + 1;
3675 *fpc++ = FF_LINEMARK;
3676 noblank = repeat = FALSE;
3685 ischop = s[-1] == '^';
3691 arg = (s - base) - 1;
3693 *fpc++ = FF_LITERAL;
3702 *fpc++ = FF_LINEGLOB;
3704 else if (*s == '#' || (*s == '.' && s[1] == '#')) {
3705 arg = ischop ? 512 : 0;
3715 arg |= 256 + (s - f);
3717 *fpc++ = s - base; /* fieldsize for FETCH */
3718 *fpc++ = FF_DECIMAL;
3721 else if (*s == '0' && s[1] == '#') { /* Zero padded decimals */
3722 arg = ischop ? 512 : 0;
3724 s++; /* skip the '0' first */
3733 arg |= 256 + (s - f);
3735 *fpc++ = s - base; /* fieldsize for FETCH */
3736 *fpc++ = FF_0DECIMAL;
3741 bool ismore = FALSE;
3744 while (*++s == '>') ;
3745 prespace = FF_SPACE;
3747 else if (*s == '|') {
3748 while (*++s == '|') ;
3749 prespace = FF_HALFSPACE;
3754 while (*++s == '<') ;
3757 if (*s == '.' && s[1] == '.' && s[2] == '.') {
3761 *fpc++ = s - base; /* fieldsize for FETCH */
3763 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
3781 { /* need to jump to the next word */
3783 z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
3784 SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U16) + 4);
3785 s = SvPVX(sv) + SvCUR(sv) + z;
3787 Copy(fops, s, arg, U16);
3789 sv_magic(sv, Nullsv, PERL_MAGIC_fm, Nullch, 0);
3794 * The mergesort implementation is by Peter M. Mcilroy <pmcilroy@lucent.com>.
3796 * The original code was written in conjunction with BSD Computer Software
3797 * Research Group at University of California, Berkeley.
3799 * See also: "Optimistic Merge Sort" (SODA '92)
3801 * The integration to Perl is by John P. Linderman <jpl@research.att.com>.
3803 * The code can be distributed under the same terms as Perl itself.
3808 #include <sys/types.h>
3813 #define New(ID,VAR,N,TYPE) VAR=(TYPE *)malloc((N)*sizeof(TYPE))
3814 #define Safefree(VAR) free(VAR)
3815 typedef int (*SVCOMPARE_t) (pTHXo_ SV*, SV*);
3816 #endif /* TESTHARNESS */
3818 typedef char * aptr; /* pointer for arithmetic on sizes */
3819 typedef SV * gptr; /* pointers in our lists */
3821 /* Binary merge internal sort, with a few special mods
3822 ** for the special perl environment it now finds itself in.
3824 ** Things that were once options have been hotwired
3825 ** to values suitable for this use. In particular, we'll always
3826 ** initialize looking for natural runs, we'll always produce stable
3827 ** output, and we'll always do Peter McIlroy's binary merge.
3830 /* Pointer types for arithmetic and storage and convenience casts */
3832 #define APTR(P) ((aptr)(P))
3833 #define GPTP(P) ((gptr *)(P))
3834 #define GPPP(P) ((gptr **)(P))
3837 /* byte offset from pointer P to (larger) pointer Q */
3838 #define BYTEOFF(P, Q) (APTR(Q) - APTR(P))
3840 #define PSIZE sizeof(gptr)
3842 /* If PSIZE is power of 2, make PSHIFT that power, if that helps */
3845 #define PNELEM(P, Q) (BYTEOFF(P,Q) >> (PSHIFT))
3846 #define PNBYTE(N) ((N) << (PSHIFT))
3847 #define PINDEX(P, N) (GPTP(APTR(P) + PNBYTE(N)))
3849 /* Leave optimization to compiler */
3850 #define PNELEM(P, Q) (GPTP(Q) - GPTP(P))
3851 #define PNBYTE(N) ((N) * (PSIZE))
3852 #define PINDEX(P, N) (GPTP(P) + (N))
3855 /* Pointer into other corresponding to pointer into this */
3856 #define POTHER(P, THIS, OTHER) GPTP(APTR(OTHER) + BYTEOFF(THIS,P))
3858 #define FROMTOUPTO(src, dst, lim) do *dst++ = *src++; while(src<lim)
3861 /* Runs are identified by a pointer in the auxilliary list.
3862 ** The pointer is at the start of the list,
3863 ** and it points to the start of the next list.
3864 ** NEXT is used as an lvalue, too.
3867 #define NEXT(P) (*GPPP(P))
3870 /* PTHRESH is the minimum number of pairs with the same sense to justify
3871 ** checking for a run and extending it. Note that PTHRESH counts PAIRS,
3872 ** not just elements, so PTHRESH == 8 means a run of 16.
3877 /* RTHRESH is the number of elements in a run that must compare low
3878 ** to the low element from the opposing run before we justify
3879 ** doing a binary rampup instead of single stepping.
3880 ** In random input, N in a row low should only happen with
3881 ** probability 2^(1-N), so we can risk that we are dealing
3882 ** with orderly input without paying much when we aren't.
3889 ** Overview of algorithm and variables.
3890 ** The array of elements at list1 will be organized into runs of length 2,
3891 ** or runs of length >= 2 * PTHRESH. We only try to form long runs when
3892 ** PTHRESH adjacent pairs compare in the same way, suggesting overall order.
3894 ** Unless otherwise specified, pair pointers address the first of two elements.
3896 ** b and b+1 are a pair that compare with sense ``sense''.
3897 ** b is the ``bottom'' of adjacent pairs that might form a longer run.
3899 ** p2 parallels b in the list2 array, where runs are defined by
3902 ** t represents the ``top'' of the adjacent pairs that might extend
3903 ** the run beginning at b. Usually, t addresses a pair
3904 ** that compares with opposite sense from (b,b+1).
3905 ** However, it may also address a singleton element at the end of list1,
3906 ** or it may be equal to ``last'', the first element beyond list1.
3908 ** r addresses the Nth pair following b. If this would be beyond t,
3909 ** we back it off to t. Only when r is less than t do we consider the
3910 ** run long enough to consider checking.
3912 ** q addresses a pair such that the pairs at b through q already form a run.
3913 ** Often, q will equal b, indicating we only are sure of the pair itself.
3914 ** However, a search on the previous cycle may have revealed a longer run,
3915 ** so q may be greater than b.
3917 ** p is used to work back from a candidate r, trying to reach q,
3918 ** which would mean b through r would be a run. If we discover such a run,
3919 ** we start q at r and try to push it further towards t.
3920 ** If b through r is NOT a run, we detect the wrong order at (p-1,p).
3921 ** In any event, after the check (if any), we have two main cases.
3923 ** 1) Short run. b <= q < p <= r <= t.
3924 ** b through q is a run (perhaps trivial)
3925 ** q through p are uninteresting pairs
3926 ** p through r is a run
3928 ** 2) Long run. b < r <= q < t.
3929 ** b through q is a run (of length >= 2 * PTHRESH)
3931 ** Note that degenerate cases are not only possible, but likely.
3932 ** For example, if the pair following b compares with opposite sense,
3933 ** then b == q < p == r == t.
3938 dynprep(pTHX_ gptr *list1, gptr *list2, size_t nmemb, SVCOMPARE_t cmp)
3941 register gptr *b, *p, *q, *t, *p2;
3942 register gptr c, *last, *r;
3946 last = PINDEX(b, nmemb);
3947 sense = (cmp(aTHX_ *b, *(b+1)) > 0);
3948 for (p2 = list2; b < last; ) {
3949 /* We just started, or just reversed sense.
3950 ** Set t at end of pairs with the prevailing sense.
3952 for (p = b+2, t = p; ++p < last; t = ++p) {
3953 if ((cmp(aTHX_ *t, *p) > 0) != sense) break;
3956 /* Having laid out the playing field, look for long runs */
3958 p = r = b + (2 * PTHRESH);
3959 if (r >= t) p = r = t; /* too short to care about */
3961 while (((cmp(aTHX_ *(p-1), *p) > 0) == sense) &&
3964 /* b through r is a (long) run.
3965 ** Extend it as far as possible.
3968 while (((p += 2) < t) &&
3969 ((cmp(aTHX_ *(p-1), *p) > 0) == sense)) q = p;
3970 r = p = q + 2; /* no simple pairs, no after-run */
3973 if (q > b) { /* run of greater than 2 at b */
3976 /* pick up singleton, if possible */
3978 ((t + 1) == last) &&
3979 ((cmp(aTHX_ *(p-1), *p) > 0) == sense))
3980 savep = r = p = q = last;
3981 p2 = NEXT(p2) = p2 + (p - b);
3982 if (sense) while (b < --p) {
3989 while (q < p) { /* simple pairs */
3990 p2 = NEXT(p2) = p2 + 2;
3997 if (((b = p) == t) && ((t+1) == last)) {
4009 /* Overview of bmerge variables:
4011 ** list1 and list2 address the main and auxiliary arrays.
4012 ** They swap identities after each merge pass.
4013 ** Base points to the original list1, so we can tell if
4014 ** the pointers ended up where they belonged (or must be copied).
4016 ** When we are merging two lists, f1 and f2 are the next elements
4017 ** on the respective lists. l1 and l2 mark the end of the lists.
4018 ** tp2 is the current location in the merged list.
4020 ** p1 records where f1 started.
4021 ** After the merge, a new descriptor is built there.
4023 ** p2 is a ``parallel'' pointer in (what starts as) descriptor space.
4024 ** It is used to identify and delimit the runs.
4026 ** In the heat of determining where q, the greater of the f1/f2 elements,
4027 ** belongs in the other list, b, t and p, represent bottom, top and probe
4028 ** locations, respectively, in the other list.
4029 ** They make convenient temporary pointers in other places.
4033 S_qsortsv(pTHX_ gptr *list1, size_t nmemb, SVCOMPARE_t cmp)
4037 register gptr *f1, *f2, *t, *b, *p, *tp2, *l1, *l2, *q;
4038 gptr *aux, *list2, *p2, *last;
4042 if (nmemb <= 1) return; /* sorted trivially */
4043 New(799,list2,nmemb,gptr); /* allocate auxilliary array */
4045 dynprep(aTHX_ list1, list2, nmemb, cmp);
4046 last = PINDEX(list2, nmemb);
4047 while (NEXT(list2) != last) {
4048 /* More than one run remains. Do some merging to reduce runs. */
4050 for (tp2 = p2 = list2; p2 != last;) {
4051 /* The new first run begins where the old second list ended.
4052 ** Use the p2 ``parallel'' pointer to identify the end of the run.
4056 f2 = l1 = POTHER(t, list2, list1);
4057 if (t != last) t = NEXT(t);
4058 l2 = POTHER(t, list2, list1);
4060 while (f1 < l1 && f2 < l2) {
4061 /* If head 1 is larger than head 2, find ALL the elements
4062 ** in list 2 strictly less than head1, write them all,
4063 ** then head 1. Then compare the new heads, and repeat,
4064 ** until one or both lists are exhausted.
4066 ** In all comparisons (after establishing
4067 ** which head to merge) the item to merge
4068 ** (at pointer q) is the first operand of
4069 ** the comparison. When we want to know
4070 ** if ``q is strictly less than the other'',
4072 ** cmp(q, other) < 0
4073 ** because stability demands that we treat equality
4074 ** as high when q comes from l2, and as low when
4075 ** q was from l1. So we ask the question by doing
4076 ** cmp(q, other) <= sense
4077 ** and make sense == 0 when equality should look low,
4078 ** and -1 when equality should look high.
4082 if (cmp(aTHX_ *f1, *f2) <= 0) {
4083 q = f2; b = f1; t = l1;
4086 q = f1; b = f2; t = l2;
4093 ** Leave t at something strictly
4094 ** greater than q (or at the end of the list),
4095 ** and b at something strictly less than q.
4097 for (i = 1, run = 0 ;;) {
4098 if ((p = PINDEX(b, i)) >= t) {
4100 if (((p = PINDEX(t, -1)) > b) &&
4101 (cmp(aTHX_ *q, *p) <= sense))
4105 } else if (cmp(aTHX_ *q, *p) <= sense) {
4109 if (++run >= RTHRESH) i += i;
4113 /* q is known to follow b and must be inserted before t.
4114 ** Increment b, so the range of possibilities is [b,t).
4115 ** Round binary split down, to favor early appearance.
4116 ** Adjust b and t until q belongs just before t.
4121 p = PINDEX(b, (PNELEM(b, t) - 1) / 2);
4122 if (cmp(aTHX_ *q, *p) <= sense) {
4128 /* Copy all the strictly low elements */
4131 FROMTOUPTO(f2, tp2, t);
4134 FROMTOUPTO(f1, tp2, t);
4140 /* Run out remaining list */
4142 if (f2 < l2) FROMTOUPTO(f2, tp2, l2);
4143 } else FROMTOUPTO(f1, tp2, l1);
4144 p1 = NEXT(p1) = POTHER(tp2, list2, list1);
4149 last = PINDEX(list2, nmemb);
4151 if (base == list2) {
4152 last = PINDEX(list1, nmemb);
4153 FROMTOUPTO(list1, list2, last);
4168 sortcv(pTHXo_ SV *a, SV *b)
4170 I32 oldsaveix = PL_savestack_ix;
4171 I32 oldscopeix = PL_scopestack_ix;
4173 GvSV(PL_firstgv) = a;
4174 GvSV(PL_secondgv) = b;
4175 PL_stack_sp = PL_stack_base;
4178 if (PL_stack_sp != PL_stack_base + 1)
4179 Perl_croak(aTHX_ "Sort subroutine didn't return single value");
4180 if (!SvNIOKp(*PL_stack_sp))
4181 Perl_croak(aTHX_ "Sort subroutine didn't return a numeric value");
4182 result = SvIV(*PL_stack_sp);
4183 while (PL_scopestack_ix > oldscopeix) {
4186 leave_scope(oldsaveix);
4191 sortcv_stacked(pTHXo_ SV *a, SV *b)
4193 I32 oldsaveix = PL_savestack_ix;
4194 I32 oldscopeix = PL_scopestack_ix;
4199 av = (AV*)PL_curpad[0];
4201 av = GvAV(PL_defgv);
4204 if (AvMAX(av) < 1) {
4205 SV** ary = AvALLOC(av);
4206 if (AvARRAY(av) != ary) {
4207 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
4208 SvPVX(av) = (char*)ary;
4210 if (AvMAX(av) < 1) {
4213 SvPVX(av) = (char*)ary;
4220 PL_stack_sp = PL_stack_base;
4223 if (PL_stack_sp != PL_stack_base + 1)
4224 Perl_croak(aTHX_ "Sort subroutine didn't return single value");
4225 if (!SvNIOKp(*PL_stack_sp))
4226 Perl_croak(aTHX_ "Sort subroutine didn't return a numeric value");
4227 result = SvIV(*PL_stack_sp);
4228 while (PL_scopestack_ix > oldscopeix) {
4231 leave_scope(oldsaveix);
4236 sortcv_xsub(pTHXo_ SV *a, SV *b)
4239 I32 oldsaveix = PL_savestack_ix;
4240 I32 oldscopeix = PL_scopestack_ix;
4242 CV *cv=(CV*)PL_sortcop;
4250 (void)(*CvXSUB(cv))(aTHXo_ cv);
4251 if (PL_stack_sp != PL_stack_base + 1)
4252 Perl_croak(aTHX_ "Sort subroutine didn't return single value");
4253 if (!SvNIOKp(*PL_stack_sp))
4254 Perl_croak(aTHX_ "Sort subroutine didn't return a numeric value");
4255 result = SvIV(*PL_stack_sp);
4256 while (PL_scopestack_ix > oldscopeix) {
4259 leave_scope(oldsaveix);
4265 sv_ncmp(pTHXo_ SV *a, SV *b)
4269 return nv1 < nv2 ? -1 : nv1 > nv2 ? 1 : 0;
4273 sv_i_ncmp(pTHXo_ SV *a, SV *b)
4277 return iv1 < iv2 ? -1 : iv1 > iv2 ? 1 : 0;
4279 #define tryCALL_AMAGICbin(left,right,meth,svp) STMT_START { \
4281 if (PL_amagic_generation) { \
4282 if (SvAMAGIC(left)||SvAMAGIC(right))\
4283 *svp = amagic_call(left, \
4291 amagic_ncmp(pTHXo_ register SV *a, register SV *b)
4294 tryCALL_AMAGICbin(a,b,ncmp,&tmpsv);
4299 I32 i = SvIVX(tmpsv);
4309 return sv_ncmp(aTHXo_ a, b);
4313 amagic_i_ncmp(pTHXo_ register SV *a, register SV *b)
4316 tryCALL_AMAGICbin(a,b,ncmp,&tmpsv);
4321 I32 i = SvIVX(tmpsv);
4331 return sv_i_ncmp(aTHXo_ a, b);
4335 amagic_cmp(pTHXo_ register SV *str1, register SV *str2)
4338 tryCALL_AMAGICbin(str1,str2,scmp,&tmpsv);
4343 I32 i = SvIVX(tmpsv);
4353 return sv_cmp(str1, str2);
4357 amagic_cmp_locale(pTHXo_ register SV *str1, register SV *str2)
4360 tryCALL_AMAGICbin(str1,str2,scmp,&tmpsv);
4365 I32 i = SvIVX(tmpsv);
4375 return sv_cmp_locale(str1, str2);
4379 run_user_filter(pTHXo_ int idx, SV *buf_sv, int maxlen)
4381 SV *datasv = FILTER_DATA(idx);
4382 int filter_has_file = IoLINES(datasv);
4383 GV *filter_child_proc = (GV *)IoFMT_GV(datasv);
4384 SV *filter_state = (SV *)IoTOP_GV(datasv);
4385 SV *filter_sub = (SV *)IoBOTTOM_GV(datasv);
4388 /* I was having segfault trouble under Linux 2.2.5 after a
4389 parse error occured. (Had to hack around it with a test
4390 for PL_error_count == 0.) Solaris doesn't segfault --
4391 not sure where the trouble is yet. XXX */
4393 if (filter_has_file) {
4394 len = FILTER_READ(idx+1, buf_sv, maxlen);
4397 if (filter_sub && len >= 0) {
4408 PUSHs(sv_2mortal(newSViv(maxlen)));
4410 PUSHs(filter_state);
4413 count = call_sv(filter_sub, G_SCALAR);
4429 IoLINES(datasv) = 0;
4430 if (filter_child_proc) {
4431 SvREFCNT_dec(filter_child_proc);
4432 IoFMT_GV(datasv) = Nullgv;
4435 SvREFCNT_dec(filter_state);
4436 IoTOP_GV(datasv) = Nullgv;
4439 SvREFCNT_dec(filter_sub);
4440 IoBOTTOM_GV(datasv) = Nullgv;
4442 filter_del(run_user_filter);
4451 sv_cmp_locale_static(pTHXo_ register SV *str1, register SV *str2)
4453 return sv_cmp_locale(str1, str2);
4457 sv_cmp_static(pTHXo_ register SV *str1, register SV *str2)
4459 return sv_cmp(str1, str2);
4462 #endif /* PERL_OBJECT */