45a4432023d409384167bcdcb31ec78cb85215fa
[p5sagit/p5-mst-13.2.git] / pp_hot.c
1 /*    pp_hot.c
2  *
3  *    Copyright (c) 1991-1994, Larry Wall
4  *
5  *    You may distribute under the terms of either the GNU General Public
6  *    License or the Artistic License, as specified in the README file.
7  *
8  */
9
10 /*
11  * Then he heard Merry change the note, and up went the Horn-cry of Buckland,
12  * shaking the air.
13  *
14  *            Awake!  Awake!  Fear, Fire, Foes!  Awake!
15  *                     Fire, Foes!  Awake!
16  */
17
18 #include "EXTERN.h"
19 #include "perl.h"
20
21 /* Hot code. */
22
23 PP(pp_const)
24 {
25     dSP;
26     XPUSHs(cSVOP->op_sv);
27     RETURN;
28 }
29
30 PP(pp_nextstate)
31 {
32     curcop = (COP*)op;
33     TAINT_NOT;          /* Each statement is presumed innocent */
34     stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
35     FREETMPS;
36     return NORMAL;
37 }
38
39 PP(pp_gvsv)
40 {
41     dSP;
42     EXTEND(sp,1);
43     if (op->op_private & OPpLVAL_INTRO)
44         PUSHs(save_scalar(cGVOP->op_gv));
45     else
46         PUSHs(GvSV(cGVOP->op_gv));
47     RETURN;
48 }
49
50 PP(pp_null)
51 {
52     return NORMAL;
53 }
54
55 PP(pp_pushmark)
56 {
57     PUSHMARK(stack_sp);
58     return NORMAL;
59 }
60
61 PP(pp_stringify)
62 {
63     dSP; dTARGET;
64     STRLEN len;
65     char *s;
66     s = SvPV(TOPs,len);
67     sv_setpvn(TARG,s,len);
68     SETTARG;
69     RETURN;
70 }
71
72 PP(pp_gv)
73 {
74     dSP;
75     XPUSHs((SV*)cGVOP->op_gv);
76     RETURN;
77 }
78
79 PP(pp_and)
80 {
81     dSP;
82     if (!SvTRUE(TOPs))
83         RETURN;
84     else {
85         --SP;
86         RETURNOP(cLOGOP->op_other);
87     }
88 }
89
90 PP(pp_sassign)
91 {
92     dSP; dPOPTOPssrl;
93     MAGIC *mg;
94
95     if (op->op_private & OPpASSIGN_BACKWARDS) {
96         SV *temp;
97         temp = left; left = right; right = temp;
98     }
99     if (tainting && tainted && (!SvGMAGICAL(left) || !SvSMAGICAL(left) ||
100                                 !((mg = mg_find(left, 't')) && mg->mg_len & 1)))
101     {
102         TAINT_NOT;
103     }
104     SvSetSV(right, left);
105     SvSETMAGIC(right);
106     SETs(right);
107     RETURN;
108 }
109
110 PP(pp_cond_expr)
111 {
112     dSP;
113     if (SvTRUEx(POPs))
114         RETURNOP(cCONDOP->op_true);
115     else
116         RETURNOP(cCONDOP->op_false);
117 }
118
119 PP(pp_unstack)
120 {
121     I32 oldsave;
122     TAINT_NOT;          /* Each statement is presumed innocent */
123     stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
124     FREETMPS;
125     oldsave = scopestack[scopestack_ix - 1];
126     LEAVE_SCOPE(oldsave);
127     return NORMAL;
128 }
129
130 PP(pp_seq)
131 {
132     dSP; tryAMAGICbinSET(seq,0); 
133     {
134       dPOPTOPssrl;
135       SETs( sv_eq(left, right) ? &sv_yes : &sv_no );
136       RETURN;
137     }
138 }
139
140 PP(pp_concat)
141 {
142   dSP; dATARGET; tryAMAGICbin(concat,opASSIGN);
143   {
144     dPOPTOPssrl;
145     STRLEN len;
146     char *s;
147     if (TARG != left) {
148         s = SvPV(left,len);
149         sv_setpvn(TARG,s,len);
150     }
151     else if (!SvOK(TARG))
152         sv_setpv(TARG, "");     /* Suppress warning. */
153     s = SvPV(right,len);
154     sv_catpvn(TARG,s,len);
155     SETTARG;
156     RETURN;
157   }
158 }
159
160 PP(pp_padsv)
161 {
162     dSP; dTARGET;
163     XPUSHs(TARG);
164     if (op->op_private & OPpLVAL_INTRO)
165         SAVECLEARSV(curpad[op->op_targ]);
166     RETURN;
167 }
168
169 PP(pp_readline)
170 {
171     last_in_gv = (GV*)(*stack_sp--);
172     return do_readline();
173 }
174
175 PP(pp_eq)
176 {
177     dSP; tryAMAGICbinSET(eq,0); 
178     {
179       dPOPnv;
180       SETs((TOPn == value) ? &sv_yes : &sv_no);
181       RETURN;
182     }
183 }
184
185 PP(pp_preinc)
186 {
187     dSP;
188     if (SvIOK(TOPs)) {
189         ++SvIVX(TOPs);
190         SvFLAGS(TOPs) &= ~(SVf_NOK|SVf_POK|SVp_NOK|SVp_POK);
191     }
192     else
193         sv_inc(TOPs);
194     SvSETMAGIC(TOPs);
195     return NORMAL;
196 }
197
198 PP(pp_or)
199 {
200     dSP;
201     if (SvTRUE(TOPs))
202         RETURN;
203     else {
204         --SP;
205         RETURNOP(cLOGOP->op_other);
206     }
207 }
208
209 PP(pp_add)
210 {
211     dSP; dATARGET; tryAMAGICbin(add,opASSIGN); 
212     {
213       dPOPTOPnnrl;
214       SETn( left + right );
215       RETURN;
216     }
217 }
218
219 PP(pp_aelemfast)
220 {
221     dSP;
222     AV *av = GvAV((GV*)cSVOP->op_sv);
223     SV** svp = av_fetch(av, op->op_private, op->op_flags & OPf_MOD);
224     PUSHs(svp ? *svp : &sv_undef);
225     RETURN;
226 }
227
228 PP(pp_join)
229 {
230     dSP; dMARK; dTARGET;
231     MARK++;
232     do_join(TARG, *MARK, MARK, SP);
233     SP = MARK;
234     SETs(TARG);
235     RETURN;
236 }
237
238 PP(pp_pushre)
239 {
240     dSP;
241     XPUSHs((SV*)op);
242     RETURN;
243 }
244
245 /* Oversized hot code. */
246
247 PP(pp_print)
248 {
249     dSP; dMARK; dORIGMARK;
250     GV *gv;
251     IO *io;
252     register FILE *fp;
253
254     if (op->op_flags & OPf_STACKED)
255         gv = (GV*)*++MARK;
256     else
257         gv = defoutgv;
258     if (!(io = GvIO(gv))) {
259         if (dowarn) {
260             SV* sv = sv_newmortal();
261             gv_fullname(sv,gv);
262             warn("Filehandle %s never opened", SvPV(sv,na));
263         }
264
265         SETERRNO(EBADF,RMS$_IFI);
266         goto just_say_no;
267     }
268     else if (!(fp = IoOFP(io))) {
269         if (dowarn)  {
270             SV* sv = sv_newmortal();
271             gv_fullname(sv,gv);
272             if (IoIFP(io))
273                 warn("Filehandle %s opened only for input", SvPV(sv,na));
274             else
275                 warn("print on closed filehandle %s", SvPV(sv,na));
276         }
277         SETERRNO(EBADF,IoIFP(io)?RMS$_FAC:RMS$_IFI);
278         goto just_say_no;
279     }
280     else {
281         MARK++;
282         if (ofslen) {
283             while (MARK <= SP) {
284                 if (!do_print(*MARK, fp))
285                     break;
286                 MARK++;
287                 if (MARK <= SP) {
288                     if (fwrite1(ofs, 1, ofslen, fp) == 0 || ferror(fp)) {
289                         MARK--;
290                         break;
291                     }
292                 }
293             }
294         }
295         else {
296             while (MARK <= SP) {
297                 if (!do_print(*MARK, fp))
298                     break;
299                 MARK++;
300             }
301         }
302         if (MARK <= SP)
303             goto just_say_no;
304         else {
305             if (orslen)
306                 if (fwrite1(ors, 1, orslen, fp) == 0 || ferror(fp))
307                     goto just_say_no;
308
309             if (IoFLAGS(io) & IOf_FLUSH)
310                 if (fflush(fp) == EOF)
311                     goto just_say_no;
312         }
313     }
314     SP = ORIGMARK;
315     PUSHs(&sv_yes);
316     RETURN;
317
318   just_say_no:
319     SP = ORIGMARK;
320     PUSHs(&sv_undef);
321     RETURN;
322 }
323
324 PP(pp_rv2av)
325 {
326     dSP; dPOPss;
327
328     AV *av;
329
330     if (SvROK(sv)) {
331       wasref:
332         av = (AV*)SvRV(sv);
333         if (SvTYPE(av) != SVt_PVAV)
334             DIE("Not an ARRAY reference");
335         if (op->op_private & OPpLVAL_INTRO)
336             av = (AV*)save_svref((SV**)sv);
337         if (op->op_flags & OPf_REF) {
338             PUSHs((SV*)av);
339             RETURN;
340         }
341     }
342     else {
343         if (SvTYPE(sv) == SVt_PVAV) {
344             av = (AV*)sv;
345             if (op->op_flags & OPf_REF) {
346                 PUSHs((SV*)av);
347                 RETURN;
348             }
349         }
350         else {
351             if (SvTYPE(sv) != SVt_PVGV) {
352                 char *sym;
353
354                 if (SvGMAGICAL(sv)) {
355                     mg_get(sv);
356                     if (SvROK(sv))
357                         goto wasref;
358                 }
359                 if (!SvOK(sv)) {
360                     if (op->op_flags & OPf_REF ||
361                       op->op_private & HINT_STRICT_REFS)
362                         DIE(no_usym, "an ARRAY");
363                     RETPUSHUNDEF;
364                 }
365                 sym = SvPV(sv,na);
366                 if (op->op_private & HINT_STRICT_REFS)
367                     DIE(no_symref, sym, "an ARRAY");
368                 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVAV);
369             }
370             av = GvAVn(sv);
371             if (op->op_private & OPpLVAL_INTRO)
372                 av = save_ary(sv);
373             if (op->op_flags & OPf_REF) {
374                 PUSHs((SV*)av);
375                 RETURN;
376             }
377         }
378     }
379
380     if (GIMME == G_ARRAY) {
381         I32 maxarg = AvFILL(av) + 1;
382         EXTEND(SP, maxarg);
383         Copy(AvARRAY(av), SP+1, maxarg, SV*);
384         SP += maxarg;
385     }
386     else {
387         dTARGET;
388         I32 maxarg = AvFILL(av) + 1;
389         PUSHi(maxarg);
390     }
391     RETURN;
392 }
393
394 PP(pp_rv2hv)
395 {
396
397     dSP; dTOPss;
398
399     HV *hv;
400
401     if (SvROK(sv)) {
402       wasref:
403         hv = (HV*)SvRV(sv);
404         if (SvTYPE(hv) != SVt_PVHV)
405             DIE("Not a HASH reference");
406         if (op->op_private & OPpLVAL_INTRO)
407             hv = (HV*)save_svref((SV**)sv);
408         if (op->op_flags & OPf_REF) {
409             SETs((SV*)hv);
410             RETURN;
411         }
412     }
413     else {
414         if (SvTYPE(sv) == SVt_PVHV) {
415             hv = (HV*)sv;
416             if (op->op_flags & OPf_REF) {
417                 SETs((SV*)hv);
418                 RETURN;
419             }
420         }
421         else {
422             if (SvTYPE(sv) != SVt_PVGV) {
423                 char *sym;
424
425                 if (SvGMAGICAL(sv)) {
426                     mg_get(sv);
427                     if (SvROK(sv))
428                         goto wasref;
429                 }
430                 if (!SvOK(sv)) {
431                     if (op->op_flags & OPf_REF ||
432                       op->op_private & HINT_STRICT_REFS)
433                         DIE(no_usym, "a HASH");
434                     RETSETUNDEF;
435                 }
436                 sym = SvPV(sv,na);
437                 if (op->op_private & HINT_STRICT_REFS)
438                     DIE(no_symref, sym, "a HASH");
439                 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVHV);
440             }
441             hv = GvHVn(sv);
442             if (op->op_private & OPpLVAL_INTRO)
443                 hv = save_hash(sv);
444             if (op->op_flags & OPf_REF) {
445                 SETs((SV*)hv);
446                 RETURN;
447             }
448         }
449     }
450
451     if (GIMME == G_ARRAY) { /* array wanted */
452         *stack_sp = (SV*)hv;
453         return do_kv(ARGS);
454     }
455     else {
456         dTARGET;
457         if (HvFILL(hv)) {
458             sprintf(buf, "%d/%d", HvFILL(hv), HvMAX(hv)+1);
459             sv_setpv(TARG, buf);
460         }
461         else
462             sv_setiv(TARG, 0);
463         SETTARG;
464         RETURN;
465     }
466 }
467
468 PP(pp_aassign)
469 {
470     dSP;
471     SV **lastlelem = stack_sp;
472     SV **lastrelem = stack_base + POPMARK;
473     SV **firstrelem = stack_base + POPMARK + 1;
474     SV **firstlelem = lastrelem + 1;
475
476     register SV **relem;
477     register SV **lelem;
478
479     register SV *sv;
480     register AV *ary;
481
482     HV *hash;
483     I32 i;
484     int magic;
485
486     delaymagic = DM_DELAY;              /* catch simultaneous items */
487
488     /* If there's a common identifier on both sides we have to take
489      * special care that assigning the identifier on the left doesn't
490      * clobber a value on the right that's used later in the list.
491      */
492     if (op->op_private & OPpASSIGN_COMMON) {
493         for (relem = firstrelem; relem <= lastrelem; relem++) {
494             /*SUPPRESS 560*/
495             if (sv = *relem)
496                 *relem = sv_mortalcopy(sv);
497         }
498     }
499
500     relem = firstrelem;
501     lelem = firstlelem;
502     ary = Null(AV*);
503     hash = Null(HV*);
504     while (lelem <= lastlelem) {
505         sv = *lelem++;
506         switch (SvTYPE(sv)) {
507         case SVt_PVAV:
508             ary = (AV*)sv;
509             magic = SvMAGICAL(ary) != 0;
510             
511             av_clear(ary);
512             i = 0;
513             while (relem <= lastrelem) {        /* gobble up all the rest */
514                 sv = NEWSV(28,0);
515                 assert(*relem);
516                 sv_setsv(sv,*relem);
517                 *(relem++) = sv;
518                 (void)av_store(ary,i++,sv);
519                 if (magic)
520                     mg_set(sv);
521             }
522             break;
523         case SVt_PVHV: {
524                 char *tmps;
525                 SV *tmpstr;
526
527                 hash = (HV*)sv;
528                 magic = SvMAGICAL(hash) != 0;
529                 hv_clear(hash);
530
531                 while (relem < lastrelem) {     /* gobble up all the rest */
532                     STRLEN len;
533                     if (*relem)
534                         sv = *(relem++);
535                     else
536                         sv = &sv_no, relem++;
537                     tmps = SvPV(sv, len);
538                     tmpstr = NEWSV(29,0);
539                     if (*relem)
540                         sv_setsv(tmpstr,*relem);        /* value */
541                     *(relem++) = tmpstr;
542                     (void)hv_store(hash,tmps,len,tmpstr,0);
543                     if (magic)
544                         mg_set(tmpstr);
545                 }
546             }
547             break;
548         default:
549             if (SvTHINKFIRST(sv)) {
550                 if (SvREADONLY(sv) && curcop != &compiling) {
551                     if (sv != &sv_undef && sv != &sv_yes && sv != &sv_no)
552                         DIE(no_modify);
553                     if (relem <= lastrelem)
554                         relem++;
555                     break;
556                 }
557                 if (SvROK(sv))
558                     sv_unref(sv);
559             }
560             if (relem <= lastrelem) {
561                 sv_setsv(sv, *relem);
562                 *(relem++) = sv;
563             }
564             else
565                 sv_setsv(sv, &sv_undef);
566             SvSETMAGIC(sv);
567             break;
568         }
569     }
570     if (delaymagic & ~DM_DELAY) {
571         if (delaymagic & DM_UID) {
572 #ifdef HAS_SETRESUID
573             (void)setresuid(uid,euid,(Uid_t)-1);
574 #else
575 #  ifdef HAS_SETREUID
576             (void)setreuid(uid,euid);
577 #  else
578 #    ifdef HAS_SETRUID
579             if ((delaymagic & DM_UID) == DM_RUID) {
580                 (void)setruid(uid);
581                 delaymagic &= ~DM_RUID;
582             }
583 #    endif /* HAS_SETRUID */
584 #    ifdef HAS_SETEUID
585             if ((delaymagic & DM_UID) == DM_EUID) {
586                 (void)seteuid(uid);
587                 delaymagic &= ~DM_EUID;
588             }
589 #    endif /* HAS_SETEUID */
590             if (delaymagic & DM_UID) {
591                 if (uid != euid)
592                     DIE("No setreuid available");
593                 (void)setuid(uid);
594             }
595 #  endif /* HAS_SETREUID */
596 #endif /* HAS_SETRESUID */
597             uid = (int)getuid();
598             euid = (int)geteuid();
599         }
600         if (delaymagic & DM_GID) {
601 #ifdef HAS_SETRESGID
602             (void)setresgid(gid,egid,(Gid_t)-1);
603 #else
604 #  ifdef HAS_SETREGID
605             (void)setregid(gid,egid);
606 #  else
607 #    ifdef HAS_SETRGID
608             if ((delaymagic & DM_GID) == DM_RGID) {
609                 (void)setrgid(gid);
610                 delaymagic &= ~DM_RGID;
611             }
612 #    endif /* HAS_SETRGID */
613 #    ifdef HAS_SETEGID
614             if ((delaymagic & DM_GID) == DM_EGID) {
615                 (void)setegid(gid);
616                 delaymagic &= ~DM_EGID;
617             }
618 #    endif /* HAS_SETEGID */
619             if (delaymagic & DM_GID) {
620                 if (gid != egid)
621                     DIE("No setregid available");
622                 (void)setgid(gid);
623             }
624 #  endif /* HAS_SETREGID */
625 #endif /* HAS_SETRESGID */
626             gid = (int)getgid();
627             egid = (int)getegid();
628         }
629         tainting |= (euid != uid || egid != gid);
630     }
631     delaymagic = 0;
632     if (GIMME == G_ARRAY) {
633         if (ary || hash)
634             SP = lastrelem;
635         else
636             SP = firstrelem + (lastlelem - firstlelem);
637         RETURN;
638     }
639     else {
640         SP = firstrelem;
641         for (relem = firstrelem; relem <= lastrelem; ++relem) {
642             if (SvOK(*relem)) {
643                 dTARGET;
644                 
645                 SETi(lastrelem - firstrelem + 1);
646                 RETURN;
647             }
648         }
649         RETSETUNDEF;
650     }
651 }
652
653 PP(pp_match)
654 {
655     dSP; dTARG;
656     register PMOP *pm = cPMOP;
657     register char *t;
658     register char *s;
659     char *strend;
660     I32 global;
661     I32 safebase;
662     char *truebase;
663     register REGEXP *rx = pm->op_pmregexp;
664     I32 gimme = GIMME;
665     STRLEN len;
666     I32 minmatch = 0;
667
668     if (op->op_flags & OPf_STACKED)
669         TARG = POPs;
670     else {
671         TARG = GvSV(defgv);
672         EXTEND(SP,1);
673     }
674     s = SvPV(TARG, len);
675     strend = s + len;
676     if (!s)
677         DIE("panic: do_match");
678
679     if (pm->op_pmflags & PMf_USED) {
680         if (gimme == G_ARRAY)
681             RETURN;
682         RETPUSHNO;
683     }
684
685     if (!rx->prelen && curpm) {
686         pm = curpm;
687         rx = pm->op_pmregexp;
688     }
689     truebase = t = s;
690     if (global = pm->op_pmflags & PMf_GLOBAL) {
691         rx->startp[0] = 0;
692         if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
693             MAGIC* mg = mg_find(TARG, 'g');
694             if (mg && mg->mg_len >= 0) {
695                 rx->endp[0] = rx->startp[0] = s + mg->mg_len; 
696                 minmatch = (mg->mg_flags & MGf_MINMATCH);
697             }
698         }
699     }
700     if (!rx->nparens && !global)
701         gimme = G_SCALAR;                       /* accidental array context? */
702     safebase = (gimme == G_ARRAY) || global;
703     if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
704         SAVEINT(multiline);
705         multiline = pm->op_pmflags & PMf_MULTILINE;
706     }
707
708 play_it_again:
709     if (global && rx->startp[0]) {
710         t = s = rx->endp[0];
711         if (s > strend)
712             goto nope;
713         minmatch = (s == rx->startp[0]);
714     }
715     if (pm->op_pmshort) {
716         if (pm->op_pmflags & PMf_SCANFIRST) {
717             if (SvSCREAM(TARG)) {
718                 if (screamfirst[BmRARE(pm->op_pmshort)] < 0)
719                     goto nope;
720                 else if (!(s = screaminstr(TARG, pm->op_pmshort)))
721                     goto nope;
722                 else if (pm->op_pmflags & PMf_ALL)
723                     goto yup;
724             }
725             else if (!(s = fbm_instr((unsigned char*)s,
726               (unsigned char*)strend, pm->op_pmshort)))
727                 goto nope;
728             else if (pm->op_pmflags & PMf_ALL)
729                 goto yup;
730             if (s && rx->regback >= 0) {
731                 ++BmUSEFUL(pm->op_pmshort);
732                 s -= rx->regback;
733                 if (s < t)
734                     s = t;
735             }
736             else
737                 s = t;
738         }
739         else if (!multiline) {
740             if (*SvPVX(pm->op_pmshort) != *s ||
741               bcmp(SvPVX(pm->op_pmshort), s, pm->op_pmslen) ) {
742                 if (pm->op_pmflags & PMf_FOLD) {
743                     if (ibcmp((U8*)SvPVX(pm->op_pmshort), (U8*)s, pm->op_pmslen) )
744                         goto nope;
745                 }
746                 else
747                     goto nope;
748             }
749         }
750         if (!rx->naughty && --BmUSEFUL(pm->op_pmshort) < 0) {
751             SvREFCNT_dec(pm->op_pmshort);
752             pm->op_pmshort = Nullsv;    /* opt is being useless */
753         }
754     }
755     if (regexec(rx, s, strend, truebase, minmatch,
756       SvSCREAM(TARG) ? TARG : Nullsv,
757       safebase)) {
758         curpm = pm;
759         if (pm->op_pmflags & PMf_ONCE)
760             pm->op_pmflags |= PMf_USED;
761         goto gotcha;
762     }
763     else
764         goto ret_no;
765     /*NOTREACHED*/
766
767   gotcha:
768     if (gimme == G_ARRAY) {
769         I32 iters, i, len;
770
771         iters = rx->nparens;
772         if (global && !iters)
773             i = 1;
774         else
775             i = 0;
776         EXTEND(SP, iters + i);
777         for (i = !i; i <= iters; i++) {
778             PUSHs(sv_newmortal());
779             /*SUPPRESS 560*/
780             if ((s = rx->startp[i]) && rx->endp[i] ) {
781                 len = rx->endp[i] - s;
782                 sv_setpvn(*SP, s, len);
783             }
784         }
785         if (global) {
786             truebase = rx->subbeg;
787             if (rx->startp[0] && rx->startp[0] == rx->endp[0])
788                 ++rx->endp[0];
789             goto play_it_again;
790         }
791         RETURN;
792     }
793     else {
794         if (global) {
795             MAGIC* mg = 0;
796             if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
797                 mg = mg_find(TARG, 'g');
798             if (!mg) {
799                 sv_magic(TARG, (SV*)0, 'g', Nullch, 0);
800                 mg = mg_find(TARG, 'g');
801             }
802             if (rx->startp[0]) {
803                 mg->mg_len = rx->endp[0] - truebase;
804                 if (rx->startp[0] == rx->endp[0])
805                     mg->mg_flags |= MGf_MINMATCH;
806                 else
807                     mg->mg_flags &= ~MGf_MINMATCH;
808             }
809             else
810                 mg->mg_len = -1;
811         }
812         RETPUSHYES;
813     }
814
815 yup:
816     ++BmUSEFUL(pm->op_pmshort);
817     curpm = pm;
818     if (pm->op_pmflags & PMf_ONCE)
819         pm->op_pmflags |= PMf_USED;
820     if (global) {
821         rx->subbeg = truebase;
822         rx->subend = strend;
823         rx->startp[0] = s;
824         rx->endp[0] = s + SvCUR(pm->op_pmshort);
825         goto gotcha;
826     }
827     if (sawampersand) {
828         char *tmps;
829
830         if (rx->subbase)
831             Safefree(rx->subbase);
832         tmps = rx->subbase = savepvn(t, strend-t);
833         rx->subbeg = tmps;
834         rx->subend = tmps + (strend-t);
835         tmps = rx->startp[0] = tmps + (s - t);
836         rx->endp[0] = tmps + SvCUR(pm->op_pmshort);
837     }
838     RETPUSHYES;
839
840 nope:
841     if (pm->op_pmshort)
842         ++BmUSEFUL(pm->op_pmshort);
843
844 ret_no:
845     if (global) {
846         if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
847             MAGIC* mg = mg_find(TARG, 'g');
848             if (mg)
849                 mg->mg_len = -1;
850         }
851     }
852     if (gimme == G_ARRAY)
853         RETURN;
854     RETPUSHNO;
855 }
856
857 OP *
858 do_readline()
859 {
860     dSP; dTARGETSTACKED;
861     register SV *sv;
862     STRLEN tmplen = 0;
863     STRLEN offset;
864     FILE *fp;
865     register IO *io = GvIO(last_in_gv);
866     register I32 type = op->op_type;
867
868     fp = Nullfp;
869     if (io) {
870         fp = IoIFP(io);
871         if (!fp) {
872             if (IoFLAGS(io) & IOf_ARGV) {
873                 if (IoFLAGS(io) & IOf_START) {
874                     IoFLAGS(io) &= ~IOf_START;
875                     IoLINES(io) = 0;
876                     if (av_len(GvAVn(last_in_gv)) < 0) {
877                         SV *tmpstr = newSVpv("-", 1); /* assume stdin */
878                         av_push(GvAVn(last_in_gv), tmpstr);
879                     }
880                 }
881                 fp = nextargv(last_in_gv);
882                 if (!fp) { /* Note: fp != IoIFP(io) */
883                     (void)do_close(last_in_gv, FALSE); /* now it does*/
884                     IoFLAGS(io) |= IOf_START;
885                 }
886             }
887             else if (type == OP_GLOB) {
888                 SV *tmpcmd = NEWSV(55, 0);
889                 SV *tmpglob = POPs;
890                 ENTER;
891                 SAVEFREESV(tmpcmd);
892 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
893            /* since spawning off a process is a real performance hit */
894                 {
895 #include <descrip.h>
896 #include <lib$routines.h>
897 #include <nam.h>
898 #include <rmsdef.h>
899                     char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
900                     char vmsspec[NAM$C_MAXRSS+1];
901                     char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
902                     char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
903                     $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
904                     FILE *tmpfp;
905                     STRLEN i;
906                     struct dsc$descriptor_s wilddsc
907                        = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
908                     struct dsc$descriptor_vs rsdsc
909                        = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
910                     unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
911
912                     /* We could find out if there's an explicit dev/dir or version
913                        by peeking into lib$find_file's internal context at
914                        ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
915                        but that's unsupported, so I don't want to do it now and
916                        have it bite someone in the future. */
917                     strcat(tmpfnam,tmpnam(NULL));
918                     cp = SvPV(tmpglob,i);
919                     for (; i; i--) {
920                        if (cp[i] == ';') hasver = 1;
921                        if (cp[i] == '.') {
922                            if (sts) hasver = 1;
923                            else sts = 1;
924                        }
925                        if (cp[i] == '/') {
926                           hasdir = isunix = 1;
927                           break;
928                        }
929                        if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
930                            hasdir = 1;
931                            break;
932                        }
933                     }
934                     if ((tmpfp = fopen(tmpfnam,"w+","fop=dlt")) != NULL) {
935                         ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
936                         if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
937                         while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
938                                                     &dfltdsc,NULL,NULL,NULL))&1)) {
939                             end = rstr + (unsigned long int) *rslt;
940                             if (!hasver) while (*end != ';') end--;
941                             *(end++) = '\n';  *end = '\0';
942                             for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
943                             if (hasdir) {
944                               if (isunix) trim_unixpath(SvPVX(tmpglob),rstr);
945                               begin = rstr;
946                             }
947                             else {
948                                 begin = end;
949                                 while (*(--begin) != ']' && *begin != '>') ;
950                                 ++begin;
951                             }
952                             ok = (fputs(begin,tmpfp) != EOF);
953                         }
954                         if (cxt) (void)lib$find_file_end(&cxt);
955                         if (ok && sts != RMS$_NMF &&
956                             sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
957                         if (!ok) {
958                             fp = NULL;
959                         }
960                         else {
961                            rewind(tmpfp);
962                            IoTYPE(io) = '<';
963                            IoIFP(io) = fp = tmpfp;
964                         }
965                     }
966                 }
967 #else /* !VMS */
968 #ifdef DOSISH
969                 sv_setpv(tmpcmd, "perlglob ");
970                 sv_catsv(tmpcmd, tmpglob);
971                 sv_catpv(tmpcmd, " |");
972 #else
973 #ifdef CSH
974                 sv_setpvn(tmpcmd, cshname, cshlen);
975                 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
976                 sv_catsv(tmpcmd, tmpglob);
977                 sv_catpv(tmpcmd, "'|");
978 #else
979                 sv_setpv(tmpcmd, "echo ");
980                 sv_catsv(tmpcmd, tmpglob);
981 #if 'z' - 'a' == 25
982                 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
983 #else
984                 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
985 #endif
986 #endif /* !CSH */
987 #endif /* !MSDOS */
988                 (void)do_open(last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),Nullfp);
989                 fp = IoIFP(io);
990 #endif /* !VMS */
991                 LEAVE;
992             }
993         }
994         else if (type == OP_GLOB)
995             SP--;
996     }
997     if (!fp) {
998         if (dowarn && !(IoFLAGS(io) & IOf_START))
999             warn("Read on closed filehandle <%s>", GvENAME(last_in_gv));
1000         if (GIMME == G_SCALAR) {
1001             (void)SvOK_off(TARG);
1002             PUSHTARG;
1003         }
1004         RETURN;
1005     }
1006     if (GIMME == G_ARRAY) {
1007         sv = sv_2mortal(NEWSV(57, 80));
1008         offset = 0;
1009     }
1010     else {
1011         sv = TARG;
1012         (void)SvUPGRADE(sv, SVt_PV);
1013         tmplen = SvLEN(sv);     /* remember if already alloced */
1014         if (!tmplen)
1015             Sv_Grow(sv, 80);    /* try short-buffering it */
1016         if (type == OP_RCATLINE)
1017             offset = SvCUR(sv);
1018         else
1019             offset = 0;
1020     }
1021     for (;;) {
1022         if (!sv_gets(sv, fp, offset)) {
1023             clearerr(fp);
1024             if (IoFLAGS(io) & IOf_ARGV) {
1025                 fp = nextargv(last_in_gv);
1026                 if (fp)
1027                     continue;
1028                 (void)do_close(last_in_gv, FALSE);
1029                 IoFLAGS(io) |= IOf_START;
1030             }
1031             else if (type == OP_GLOB) {
1032                 (void)do_close(last_in_gv, FALSE);
1033             }
1034             if (GIMME == G_SCALAR) {
1035                 (void)SvOK_off(TARG);
1036                 PUSHTARG;
1037             }
1038             RETURN;
1039         }
1040         IoLINES(io)++;
1041         XPUSHs(sv);
1042         if (tainting) {
1043             tainted = TRUE;
1044             SvTAINT(sv); /* Anything from the outside world...*/
1045         }
1046         if (type == OP_GLOB) {
1047             char *tmps;
1048
1049             if (SvCUR(sv) > 0)
1050                 SvCUR(sv)--;
1051             if (*SvEND(sv) == rschar)
1052                 *SvEND(sv) = '\0';
1053             else
1054                 SvCUR(sv)++;
1055             for (tmps = SvPVX(sv); *tmps; tmps++)
1056                 if (!isALPHA(*tmps) && !isDIGIT(*tmps) &&
1057                     strchr("$&*(){}[]'\";\\|?<>~`", *tmps))
1058                         break;
1059             if (*tmps && Stat(SvPVX(sv), &statbuf) < 0) {
1060                 (void)POPs;             /* Unmatched wildcard?  Chuck it... */
1061                 continue;
1062             }
1063         }
1064         if (GIMME == G_ARRAY) {
1065             if (SvLEN(sv) - SvCUR(sv) > 20) {
1066                 SvLEN_set(sv, SvCUR(sv)+1);
1067                 Renew(SvPVX(sv), SvLEN(sv), char);
1068             }
1069             sv = sv_2mortal(NEWSV(58, 80));
1070             continue;
1071         }
1072         else if (!tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
1073             /* try to reclaim a bit of scalar space (only on 1st alloc) */
1074             if (SvCUR(sv) < 60)
1075                 SvLEN_set(sv, 80);
1076             else
1077                 SvLEN_set(sv, SvCUR(sv)+40);    /* allow some slop */
1078             Renew(SvPVX(sv), SvLEN(sv), char);
1079         }
1080         RETURN;
1081     }
1082 }
1083
1084 PP(pp_enter)
1085 {
1086     dSP;
1087     register CONTEXT *cx;
1088     I32 gimme;
1089
1090     /*
1091      * We don't just use the GIMME macro here because it assumes there's
1092      * already a context, which ain't necessarily so at initial startup.
1093      */
1094
1095     if (op->op_flags & OPf_KNOW)
1096         gimme = op->op_flags & OPf_LIST;
1097     else if (cxstack_ix >= 0)
1098         gimme = cxstack[cxstack_ix].blk_gimme;
1099     else
1100         gimme = G_SCALAR;
1101
1102     ENTER;
1103
1104     SAVETMPS;
1105     PUSHBLOCK(cx, CXt_BLOCK, sp);
1106
1107     RETURN;
1108 }
1109
1110 PP(pp_helem)
1111 {
1112     dSP;
1113     SV** svp;
1114     SV *keysv = POPs;
1115     STRLEN keylen;
1116     char *key = SvPV(keysv, keylen);
1117     HV *hv = (HV*)POPs;
1118     I32 lval = op->op_flags & OPf_MOD;
1119
1120     if (SvTYPE(hv) != SVt_PVHV)
1121         RETPUSHUNDEF;
1122     svp = hv_fetch(hv, key, keylen, lval);
1123     if (lval) {
1124         if (!svp || *svp == &sv_undef)
1125             DIE(no_helem, key);
1126         if (op->op_private & OPpLVAL_INTRO)
1127             save_svref(svp);
1128         else if (op->op_private & (OPpDEREF_HV|OPpDEREF_AV)) {
1129             SV* sv = *svp;
1130             if (SvGMAGICAL(sv))
1131                 mg_get(sv);
1132             if (!SvOK(sv)) {
1133                 (void)SvUPGRADE(sv, SVt_RV);
1134                 SvRV(sv) = (op->op_private & OPpDEREF_HV ?
1135                             (SV*)newHV() : (SV*)newAV());
1136                 SvROK_on(sv);
1137                 SvSETMAGIC(sv);
1138             }
1139         }
1140     }
1141     PUSHs(svp ? *svp : &sv_undef);
1142     RETURN;
1143 }
1144
1145 PP(pp_leave)
1146 {
1147     dSP;
1148     register CONTEXT *cx;
1149     register SV **mark;
1150     SV **newsp;
1151     PMOP *newpm;
1152     I32 gimme;
1153
1154     if (op->op_flags & OPf_SPECIAL) {
1155         cx = &cxstack[cxstack_ix];
1156         cx->blk_oldpm = curpm;  /* fake block should preserve $1 et al */
1157     }
1158
1159     POPBLOCK(cx,newpm);
1160
1161     if (op->op_flags & OPf_KNOW)
1162         gimme = op->op_flags & OPf_LIST;
1163     else if (cxstack_ix >= 0)
1164         gimme = cxstack[cxstack_ix].blk_gimme;
1165     else
1166         gimme = G_SCALAR;
1167
1168     if (gimme == G_SCALAR) {
1169         if (op->op_private & OPpLEAVE_VOID)
1170             SP = newsp;
1171         else {
1172             MARK = newsp + 1;
1173             if (MARK <= SP)
1174                 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1175                     *MARK = TOPs;
1176                 else
1177                     *MARK = sv_mortalcopy(TOPs);
1178             else {
1179                 MEXTEND(mark,0);
1180                 *MARK = &sv_undef;
1181             }
1182             SP = MARK;
1183         }
1184     }
1185     else {
1186         for (mark = newsp + 1; mark <= SP; mark++)
1187             if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP)))
1188                 *mark = sv_mortalcopy(*mark);
1189                 /* in case LEAVE wipes old return values */
1190     }
1191     curpm = newpm;      /* Don't pop $1 et al till now */
1192
1193     LEAVE;
1194
1195     RETURN;
1196 }
1197
1198 PP(pp_iter)
1199 {
1200     dSP;
1201     register CONTEXT *cx;
1202     SV *sv;
1203
1204     EXTEND(sp, 1);
1205     cx = &cxstack[cxstack_ix];
1206     if (cx->cx_type != CXt_LOOP)
1207         DIE("panic: pp_iter");
1208
1209     if (cx->blk_loop.iterix >= cx->blk_oldsp)
1210         RETPUSHNO;
1211
1212     if (sv = AvARRAY(cx->blk_loop.iterary)[++cx->blk_loop.iterix]) {
1213         SvTEMP_off(sv);
1214         *cx->blk_loop.itervar = sv;
1215     }
1216     else
1217         *cx->blk_loop.itervar = &sv_undef;
1218
1219     RETPUSHYES;
1220 }
1221
1222 PP(pp_subst)
1223 {
1224     dSP; dTARG;
1225     register PMOP *pm = cPMOP;
1226     PMOP *rpm = pm;
1227     register SV *dstr;
1228     register char *s;
1229     char *strend;
1230     register char *m;
1231     char *c;
1232     register char *d;
1233     STRLEN clen;
1234     I32 iters = 0;
1235     I32 maxiters;
1236     register I32 i;
1237     bool once;
1238     char *orig;
1239     I32 safebase;
1240     register REGEXP *rx = pm->op_pmregexp;
1241     STRLEN len;
1242     int force_on_match = 0;
1243
1244     if (pm->op_pmflags & PMf_CONST)     /* known replacement string? */
1245         dstr = POPs;
1246     if (op->op_flags & OPf_STACKED)
1247         TARG = POPs;
1248     else {
1249         TARG = GvSV(defgv);
1250         EXTEND(SP,1);
1251     }
1252     s = SvPV(TARG, len);
1253     if (!SvPOKp(TARG) || SvREADONLY(TARG))
1254         force_on_match = 1;
1255
1256   force_it:
1257     if (!pm || !s)
1258         DIE("panic: do_subst");
1259
1260     strend = s + len;
1261     maxiters = (strend - s) + 10;
1262
1263     if (!rx->prelen && curpm) {
1264         pm = curpm;
1265         rx = pm->op_pmregexp;
1266     }
1267     safebase = ((!rx || !rx->nparens) && !sawampersand);
1268     if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
1269         SAVEINT(multiline);
1270         multiline = pm->op_pmflags & PMf_MULTILINE;
1271     }
1272     orig = m = s;
1273     if (pm->op_pmshort) {
1274         if (pm->op_pmflags & PMf_SCANFIRST) {
1275             if (SvSCREAM(TARG)) {
1276                 if (screamfirst[BmRARE(pm->op_pmshort)] < 0)
1277                     goto nope;
1278                 else if (!(s = screaminstr(TARG, pm->op_pmshort)))
1279                     goto nope;
1280             }
1281             else if (!(s = fbm_instr((unsigned char*)s, (unsigned char*)strend,
1282               pm->op_pmshort)))
1283                 goto nope;
1284             if (s && rx->regback >= 0) {
1285                 ++BmUSEFUL(pm->op_pmshort);
1286                 s -= rx->regback;
1287                 if (s < m)
1288                     s = m;
1289             }
1290             else
1291                 s = m;
1292         }
1293         else if (!multiline) {
1294             if (*SvPVX(pm->op_pmshort) != *s ||
1295               bcmp(SvPVX(pm->op_pmshort), s, pm->op_pmslen) ) {
1296                 if (pm->op_pmflags & PMf_FOLD) {
1297                     if (ibcmp((U8*)SvPVX(pm->op_pmshort), (U8*)s, pm->op_pmslen) )
1298                         goto nope;
1299                 }
1300                 else
1301                     goto nope;
1302             }
1303         }
1304         if (!rx->naughty && --BmUSEFUL(pm->op_pmshort) < 0) {
1305             SvREFCNT_dec(pm->op_pmshort);
1306             pm->op_pmshort = Nullsv;    /* opt is being useless */
1307         }
1308     }
1309     once = !(rpm->op_pmflags & PMf_GLOBAL);
1310     if (rpm->op_pmflags & PMf_CONST) {  /* known replacement string? */
1311         c = SvPV(dstr, clen);
1312         if (clen <= rx->minlen) {
1313                                         /* can do inplace substitution */
1314             if (regexec(rx, s, strend, orig, 0,
1315               SvSCREAM(TARG) ? TARG : Nullsv, safebase)) {
1316                 if (force_on_match) {
1317                     force_on_match = 0;
1318                     s = SvPV_force(TARG, len);
1319                     goto force_it;
1320                 }
1321                 if (rx->subbase)        /* oops, no we can't */
1322                     goto long_way;
1323                 d = s;
1324                 curpm = pm;
1325                 SvSCREAM_off(TARG);     /* disable possible screamer */
1326                 if (once) {
1327                     m = rx->startp[0];
1328                     d = rx->endp[0];
1329                     s = orig;
1330                     if (m - s > strend - d) {   /* faster to shorten from end */
1331                         if (clen) {
1332                             Copy(c, m, clen, char);
1333                             m += clen;
1334                         }
1335                         i = strend - d;
1336                         if (i > 0) {
1337                             Move(d, m, i, char);
1338                             m += i;
1339                         }
1340                         *m = '\0';
1341                         SvCUR_set(TARG, m - s);
1342                         (void)SvPOK_only(TARG);
1343                         SvSETMAGIC(TARG);
1344                         PUSHs(&sv_yes);
1345                         RETURN;
1346                     }
1347                     /*SUPPRESS 560*/
1348                     else if (i = m - s) {       /* faster from front */
1349                         d -= clen;
1350                         m = d;
1351                         sv_chop(TARG, d-i);
1352                         s += i;
1353                         while (i--)
1354                             *--d = *--s;
1355                         if (clen)
1356                             Copy(c, m, clen, char);
1357                         (void)SvPOK_only(TARG);
1358                         SvSETMAGIC(TARG);
1359                         PUSHs(&sv_yes);
1360                         RETURN;
1361                     }
1362                     else if (clen) {
1363                         d -= clen;
1364                         sv_chop(TARG, d);
1365                         Copy(c, d, clen, char);
1366                         (void)SvPOK_only(TARG);
1367                         SvSETMAGIC(TARG);
1368                         PUSHs(&sv_yes);
1369                         RETURN;
1370                     }
1371                     else {
1372                         sv_chop(TARG, d);
1373                         (void)SvPOK_only(TARG);
1374                         SvSETMAGIC(TARG);
1375                         PUSHs(&sv_yes);
1376                         RETURN;
1377                     }
1378                     /* NOTREACHED */
1379                 }
1380                 do {
1381                     if (iters++ > maxiters)
1382                         DIE("Substitution loop");
1383                     m = rx->startp[0];
1384                     /*SUPPRESS 560*/
1385                     if (i = m - s) {
1386                         if (s != d)
1387                             Move(s, d, i, char);
1388                         d += i;
1389                     }
1390                     if (clen) {
1391                         Copy(c, d, clen, char);
1392                         d += clen;
1393                     }
1394                     s = rx->endp[0];
1395                 } while (regexec(rx, s, strend, orig, s == m,
1396                     Nullsv, TRUE));     /* (don't match same null twice) */
1397                 if (s != d) {
1398                     i = strend - s;
1399                     SvCUR_set(TARG, d - SvPVX(TARG) + i);
1400                     Move(s, d, i+1, char);              /* include the Null */
1401                 }
1402                 (void)SvPOK_only(TARG);
1403                 SvSETMAGIC(TARG);
1404                 PUSHs(sv_2mortal(newSViv((I32)iters)));
1405                 RETURN;
1406             }
1407             PUSHs(&sv_no);
1408             RETURN;
1409         }
1410     }
1411     else
1412         c = Nullch;
1413     if (regexec(rx, s, strend, orig, 0,
1414       SvSCREAM(TARG) ? TARG : Nullsv, safebase)) {
1415     long_way:
1416         if (force_on_match) {
1417             force_on_match = 0;
1418             s = SvPV_force(TARG, len);
1419             goto force_it;
1420         }
1421         dstr = NEWSV(25, sv_len(TARG));
1422         sv_setpvn(dstr, m, s-m);
1423         curpm = pm;
1424         if (!c) {
1425             register CONTEXT *cx;
1426             PUSHSUBST(cx);
1427             RETURNOP(cPMOP->op_pmreplroot);
1428         }
1429         do {
1430             if (iters++ > maxiters)
1431                 DIE("Substitution loop");
1432             if (rx->subbase && rx->subbase != orig) {
1433                 m = s;
1434                 s = orig;
1435                 orig = rx->subbase;
1436                 s = orig + (m - s);
1437                 strend = s + (strend - m);
1438             }
1439             m = rx->startp[0];
1440             sv_catpvn(dstr, s, m-s);
1441             s = rx->endp[0];
1442             if (clen)
1443                 sv_catpvn(dstr, c, clen);
1444             if (once)
1445                 break;
1446         } while (regexec(rx, s, strend, orig, s == m, Nullsv,
1447             safebase));
1448         sv_catpvn(dstr, s, strend - s);
1449
1450         Safefree(SvPVX(TARG));
1451         SvPVX(TARG) = SvPVX(dstr);
1452         SvCUR_set(TARG, SvCUR(dstr));
1453         SvLEN_set(TARG, SvLEN(dstr));
1454         SvPVX(dstr) = 0;
1455         sv_free(dstr);
1456
1457         (void)SvPOK_only(TARG);
1458         SvSETMAGIC(TARG);
1459         PUSHs(sv_2mortal(newSViv((I32)iters)));
1460         RETURN;
1461     }
1462     PUSHs(&sv_no);
1463     RETURN;
1464
1465 nope:
1466     ++BmUSEFUL(pm->op_pmshort);
1467     PUSHs(&sv_no);
1468     RETURN;
1469 }
1470
1471 PP(pp_grepwhile)
1472 {
1473     dSP;
1474
1475     if (SvTRUEx(POPs))
1476         stack_base[markstack_ptr[-1]++] = stack_base[*markstack_ptr];
1477     ++*markstack_ptr;
1478     LEAVE;                                      /* exit inner scope */
1479
1480     /* All done yet? */
1481     if (stack_base + *markstack_ptr > sp) {
1482         I32 items;
1483
1484         LEAVE;                                  /* exit outer scope */
1485         (void)POPMARK;                          /* pop src */
1486         items = --*markstack_ptr - markstack_ptr[-1];
1487         (void)POPMARK;                          /* pop dst */
1488         SP = stack_base + POPMARK;              /* pop original mark */
1489         if (GIMME != G_ARRAY) {
1490             dTARGET;
1491             XPUSHi(items);
1492             RETURN;
1493         }
1494         SP += items;
1495         RETURN;
1496     }
1497     else {
1498         SV *src;
1499
1500         ENTER;                                  /* enter inner scope */
1501         SAVESPTR(curpm);
1502
1503         src = stack_base[*markstack_ptr];
1504         SvTEMP_off(src);
1505         GvSV(defgv) = src;
1506
1507         RETURNOP(cLOGOP->op_other);
1508     }
1509 }
1510
1511 PP(pp_leavesub)
1512 {
1513     dSP;
1514     SV **mark;
1515     SV **newsp;
1516     PMOP *newpm;
1517     I32 gimme;
1518     register CONTEXT *cx;
1519
1520     POPBLOCK(cx,newpm);
1521     POPSUB(cx);
1522
1523     if (gimme == G_SCALAR) {
1524         MARK = newsp + 1;
1525         if (MARK <= SP)
1526             if (SvFLAGS(TOPs) & SVs_TEMP)
1527                 *MARK = TOPs;
1528             else
1529                 *MARK = sv_mortalcopy(TOPs);
1530         else {
1531             MEXTEND(mark,0);
1532             *MARK = &sv_undef;
1533         }
1534         SP = MARK;
1535     }
1536     else {
1537         for (mark = newsp + 1; mark <= SP; mark++)
1538             if (!(SvFLAGS(*mark) & SVs_TEMP))
1539                 *mark = sv_mortalcopy(*mark);
1540                 /* in case LEAVE wipes old return values */
1541     }
1542
1543     if (cx->blk_sub.hasargs) {          /* You don't exist; go away. */
1544         AV* av = cx->blk_sub.argarray;
1545
1546         av_clear(av);
1547         AvREAL_off(av);
1548     }
1549     curpm = newpm;      /* Don't pop $1 et al till now */
1550
1551     LEAVE;
1552     PUTBACK;
1553     return pop_return();
1554 }
1555
1556 PP(pp_entersub)
1557 {
1558     dSP; dPOPss;
1559     GV *gv;
1560     HV *stash;
1561     register CV *cv;
1562     register CONTEXT *cx;
1563
1564     if (!sv)
1565         DIE("Not a CODE reference");
1566     switch (SvTYPE(sv)) {
1567     default:
1568         if (!SvROK(sv)) {
1569             char *sym;
1570
1571             if (sv == &sv_yes)          /* unfound import, ignore */
1572                 RETURN;
1573             if (!SvOK(sv))
1574                 DIE(no_usym, "a subroutine");
1575             sym = SvPV(sv,na);
1576             if (op->op_private & HINT_STRICT_REFS)
1577                 DIE(no_symref, sym, "a subroutine");
1578             cv = perl_get_cv(sym, TRUE);
1579             break;
1580         }
1581         cv = (CV*)SvRV(sv);
1582         if (SvTYPE(cv) == SVt_PVCV)
1583             break;
1584         /* FALL THROUGH */
1585     case SVt_PVHV:
1586     case SVt_PVAV:
1587         DIE("Not a CODE reference");
1588     case SVt_PVCV:
1589         cv = (CV*)sv;
1590         break;
1591     case SVt_PVGV:
1592         if (!(cv = GvCV((GV*)sv)))
1593             cv = sv_2cv(sv, &stash, &gv, TRUE);
1594         break;
1595     }
1596
1597     ENTER;
1598     SAVETMPS;
1599
1600   retry:
1601     if (!cv)
1602         DIE("Not a CODE reference");
1603
1604     if (!CvROOT(cv) && !CvXSUB(cv)) {
1605         if (gv = CvGV(cv)) {
1606             SV *tmpstr = sv_newmortal();
1607             GV *ngv;
1608             gv_efullname(tmpstr, gv);
1609             ngv = gv_fetchmethod(GvESTASH(gv), "AUTOLOAD");
1610             if (ngv && ngv != gv && (cv = GvCV(ngv))) { /* One more chance... */
1611                 gv = ngv;
1612                 sv_setsv(GvSV(CvGV(cv)), tmpstr);       /* Set CV's $AUTOLOAD */
1613                 goto retry;
1614             }
1615             else
1616                 DIE("Undefined subroutine &%s called",SvPVX(tmpstr));
1617         }
1618         DIE("Undefined subroutine called");
1619     }
1620
1621     if ((op->op_private & OPpDEREF_DB) && !CvXSUB(cv)) {
1622         sv = GvSV(DBsub);
1623         save_item(sv);
1624         if (SvFLAGS(cv) & SVpcv_ANON)   /* Is GV potentially non-unique? */
1625             sv_setsv(sv, newRV((SV*)cv));
1626         else {
1627             gv = CvGV(cv);
1628             gv_efullname(sv,gv);
1629         }
1630         cv = GvCV(DBsub);
1631         if (!cv)
1632             DIE("No DBsub routine");
1633     }
1634
1635     if (CvXSUB(cv)) {
1636         if (CvOLDSTYLE(cv)) {
1637             I32 (*fp3)_((int,int,int));
1638             dMARK;
1639             register I32 items = SP - MARK;
1640             while (sp > mark) {
1641                 sp[1] = sp[0];
1642                 sp--;
1643             }
1644             stack_sp = mark + 1;
1645             fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
1646             items = (*fp3)(CvXSUBANY(cv).any_i32, 
1647                            MARK - stack_base + 1,
1648                            items);
1649             stack_sp = stack_base + items;
1650         }
1651         else {
1652             I32 markix = TOPMARK;
1653
1654             PUTBACK;
1655             (void)(*CvXSUB(cv))(cv);
1656
1657             /* Enforce some sanity in scalar context. */
1658             if (GIMME == G_SCALAR && ++markix != stack_sp - stack_base ) {
1659                 if (markix > stack_sp - stack_base)
1660                     *(stack_base + markix) = &sv_undef;
1661                 else
1662                     *(stack_base + markix) = *stack_sp;
1663                 stack_sp = stack_base + markix;
1664             }
1665         }
1666         LEAVE;
1667         return NORMAL;
1668     }
1669     else {
1670         dMARK;
1671         register I32 items = SP - MARK;
1672         I32 hasargs = (op->op_flags & OPf_STACKED) != 0;
1673         I32 gimme = GIMME;
1674         AV* padlist = CvPADLIST(cv);
1675         SV** svp = AvARRAY(padlist);
1676         push_return(op->op_next);
1677         PUSHBLOCK(cx, CXt_SUB, MARK);
1678         PUSHSUB(cx);
1679         CvDEPTH(cv)++;
1680         if (CvDEPTH(cv) < 2)
1681             (void)SvREFCNT_inc(cv);
1682         else {  /* save temporaries on recursion? */
1683             if (CvDEPTH(cv) == 100 && dowarn)
1684                 warn("Deep recursion on subroutine \"%s\"",GvENAME(CvGV(cv)));
1685             if (CvDEPTH(cv) > AvFILL(padlist)) {
1686                 AV *av;
1687                 AV *newpad = newAV();
1688                 AV *oldpad = (AV*)AvARRAY(svp[CvDEPTH(cv)-1]);
1689                 I32 ix = AvFILL((AV*)svp[1]);
1690                 svp = AvARRAY(svp[0]);
1691                 for ( ;ix > 0; ix--) {
1692                     if (svp[ix] != &sv_undef) {
1693                         char *name = SvPVX(svp[ix]);
1694                         if (SvFLAGS(svp[ix]) & SVf_FAKE) { /* outer lexical? */
1695                             av_store(newpad, ix,
1696                                 SvREFCNT_inc(AvARRAY(oldpad)[ix]) );
1697                         }
1698                         else {                          /* our own lexical */
1699                             if (*name == '@')
1700                                 av_store(newpad, ix, sv = (SV*)newAV());
1701                             else if (*name == '%')
1702                                 av_store(newpad, ix, sv = (SV*)newHV());
1703                             else
1704                                 av_store(newpad, ix, sv = NEWSV(0,0));
1705                             SvPADMY_on(sv);
1706                         }
1707                     }
1708                     else {
1709                         av_store(newpad, ix, sv = NEWSV(0,0));
1710                         SvPADTMP_on(sv);
1711                     }
1712                 }
1713                 av = newAV();           /* will be @_ */
1714                 av_extend(av, 0);
1715                 av_store(newpad, 0, (SV*)av);
1716                 AvFLAGS(av) = AVf_REIFY;
1717                 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
1718                 AvFILL(padlist) = CvDEPTH(cv);
1719                 svp = AvARRAY(padlist);
1720             }
1721         }
1722         SAVESPTR(curpad);
1723         curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
1724         if (hasargs) {
1725             AV* av = (AV*)curpad[0];
1726             SV** ary;
1727
1728             if (AvREAL(av)) {
1729                 av_clear(av);
1730                 AvREAL_off(av);
1731             }
1732             cx->blk_sub.savearray = GvAV(defgv);
1733             cx->blk_sub.argarray = av;
1734             GvAV(defgv) = cx->blk_sub.argarray;
1735             ++MARK;
1736
1737             if (items > AvMAX(av) + 1) {
1738                 ary = AvALLOC(av);
1739                 if (AvARRAY(av) != ary) {
1740                     AvMAX(av) += AvARRAY(av) - AvALLOC(av);
1741                     SvPVX(av) = (char*)ary;
1742                 }
1743                 if (items > AvMAX(av) + 1) {
1744                     AvMAX(av) = items - 1;
1745                     Renew(ary,items,SV*);
1746                     AvALLOC(av) = ary;
1747                     SvPVX(av) = (char*)ary;
1748                 }
1749             }
1750             Copy(MARK,AvARRAY(av),items,SV*);
1751             AvFILL(av) = items - 1;
1752             
1753             while (items--) {
1754                 if (*MARK)
1755                     SvTEMP_off(*MARK);
1756                 MARK++;
1757             }
1758         }
1759         RETURNOP(CvSTART(cv));
1760     }
1761 }
1762
1763 PP(pp_aelem)
1764 {
1765     dSP;
1766     SV** svp;
1767     I32 elem = POPi;
1768     AV *av = (AV*)POPs;
1769     I32 lval = op->op_flags & OPf_MOD;
1770
1771     if (elem > 0)
1772         elem -= curcop->cop_arybase;
1773     if (SvTYPE(av) != SVt_PVAV)
1774         RETPUSHUNDEF;
1775     svp = av_fetch(av, elem, lval);
1776     if (lval) {
1777         if (!svp || *svp == &sv_undef)
1778             DIE(no_aelem, elem);
1779         if (op->op_private & OPpLVAL_INTRO)
1780             save_svref(svp);
1781         else if (op->op_private & (OPpDEREF_HV|OPpDEREF_AV)) {
1782             SV* sv = *svp;
1783             if (SvGMAGICAL(sv))
1784                 mg_get(sv);
1785             if (!SvOK(sv)) {
1786                 (void)SvUPGRADE(sv, SVt_RV);
1787                 SvRV(sv) = (op->op_private & OPpDEREF_HV ?
1788                             (SV*)newHV() : (SV*)newAV());
1789                 SvROK_on(sv);
1790                 SvSETMAGIC(sv);
1791             }
1792         }
1793     }
1794     PUSHs(svp ? *svp : &sv_undef);
1795     RETURN;
1796 }
1797
1798 PP(pp_method)
1799 {
1800     dSP;
1801     SV* sv;
1802     SV* ob;
1803     GV* gv;
1804     SV* nm;
1805
1806     nm = TOPs;
1807     sv = *(stack_base + TOPMARK + 1);
1808     
1809     gv = 0;
1810     if (SvROK(sv))
1811         ob = SvRV(sv);
1812     else {
1813         GV* iogv;
1814         char* packname = 0;
1815
1816         if (!SvOK(sv) ||
1817             !(packname = SvPV(sv, na)) ||
1818             !(iogv = gv_fetchpv(packname, FALSE, SVt_PVIO)) ||
1819             !(ob=(SV*)GvIO(iogv)))
1820         {
1821             char *name = SvPV(nm, na);
1822             HV *stash;
1823             if (!packname || !isALPHA(*packname))
1824 DIE("Can't call method \"%s\" without a package or object reference", name);
1825             if (!(stash = gv_stashpv(packname, FALSE))) {
1826                 if (gv_stashpv("UNIVERSAL", FALSE))
1827                     stash = gv_stashpv(packname, TRUE);
1828                 else
1829                     DIE("Can't call method \"%s\" in empty package \"%s\"",
1830                         name, packname);
1831             }
1832             gv = gv_fetchmethod(stash,name);
1833             if (!gv)
1834                 DIE("Can't locate object method \"%s\" via package \"%s\"",
1835                     name, packname);
1836             SETs(gv);
1837             RETURN;
1838         }
1839         *(stack_base + TOPMARK + 1) = iogv;
1840     }
1841
1842     if (!ob || !SvOBJECT(ob)) {
1843         char *name = SvPV(nm, na);
1844         DIE("Can't call method \"%s\" on unblessed reference", name);
1845     }
1846
1847     if (!gv) {          /* nothing cached */
1848         char *name = SvPV(nm, na);
1849         gv = gv_fetchmethod(SvSTASH(ob),name);
1850         if (!gv)
1851             DIE("Can't locate object method \"%s\" via package \"%s\"",
1852                 name, HvNAME(SvSTASH(ob)));
1853     }
1854
1855     SETs(gv);
1856     RETURN;
1857 }
1858