3 * Copyright (c) 1991-1999, 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 * "You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
12 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
13 * youngest of the Old Took's daughters); and Mr. Drogo was his second
14 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
15 * either way, as the saying is, if you follow me." --the Gaffer
23 /* #define PL_OP_SLAB_ALLOC */
26 #define OP_REFCNT_LOCK NOOP
27 #define OP_REFCNT_UNLOCK NOOP
28 #define OpREFCNT_set(o,n) NOOP
29 #define OpREFCNT_dec(o) 0
31 #ifdef PL_OP_SLAB_ALLOC
32 #define SLAB_SIZE 8192
33 static char *PL_OpPtr = NULL;
34 static int PL_OpSpace = 0;
35 #define NewOp(m,var,c,type) do { if ((PL_OpSpace -= c*sizeof(type)) >= 0) \
36 var = (type *)(PL_OpPtr -= c*sizeof(type)); \
38 var = (type *) Slab_Alloc(m,c*sizeof(type)); \
42 S_Slab_Alloc(pTHX_ int m, size_t sz)
44 Newz(m,PL_OpPtr,SLAB_SIZE,char);
45 PL_OpSpace = SLAB_SIZE - sz;
46 return PL_OpPtr += PL_OpSpace;
50 #define NewOp(m, var, c, type) Newz(m, var, c, type)
53 * In the following definition, the ", Nullop" is just to make the compiler
54 * think the expression is of the right type: croak actually does a Siglongjmp.
56 #define CHECKOP(type,o) \
57 ((PL_op_mask && PL_op_mask[type]) \
58 ? ( op_free((OP*)o), \
59 Perl_croak(aTHX_ "%s trapped by operation mask", PL_op_desc[type]), \
61 : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o))
63 #define PAD_MAX 999999999
66 S_gv_ename(pTHX_ GV *gv)
69 SV* tmpsv = sv_newmortal();
70 gv_efullname3(tmpsv, gv, Nullch);
71 return SvPV(tmpsv,n_a);
75 S_no_fh_allowed(pTHX_ OP *o)
77 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
78 PL_op_desc[o->op_type]));
83 S_too_few_arguments(pTHX_ OP *o, char *name)
85 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
90 S_too_many_arguments(pTHX_ OP *o, char *name)
92 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
97 S_bad_type(pTHX_ I32 n, char *t, char *name, OP *kid)
99 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
100 (int)n, name, t, PL_op_desc[kid->op_type]));
104 S_no_bareword_allowed(pTHX_ OP *o)
106 qerror(Perl_mess(aTHX_
107 "Bareword \"%s\" not allowed while \"strict subs\" in use",
108 SvPV_nolen(cSVOPo->op_sv)));
111 /* "register" allocation */
114 Perl_pad_allocmy(pTHX_ char *name)
121 PL_in_my == KEY_our ||
123 (PL_hints & HINT_UTF8 && (name[1] & 0xc0) == 0xc0) ||
124 name[1] == '_' && (int)strlen(name) > 2 ))
126 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
127 /* 1999-02-27 mjd@plover.com */
129 p = strchr(name, '\0');
130 /* The next block assumes the buffer is at least 205 chars
131 long. At present, it's always at least 256 chars. */
133 strcpy(name+200, "...");
139 /* Move everything else down one character */
140 for (; p-name > 2; p--)
142 name[2] = toCTRL(name[1]);
145 yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
147 if (ckWARN(WARN_UNSAFE) && AvFILLp(PL_comppad_name) >= 0) {
148 SV **svp = AvARRAY(PL_comppad_name);
149 for (off = AvFILLp(PL_comppad_name); off > PL_comppad_name_floor; off--) {
151 && sv != &PL_sv_undef
152 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
153 && strEQ(name, SvPVX(sv)))
155 Perl_warner(aTHX_ WARN_UNSAFE,
156 "\"%s\" variable %s masks earlier declaration in same %s",
157 (PL_in_my == KEY_our ? "our" : "my"),
159 (SvIVX(sv) == PAD_MAX ? "scope" : "statement"));
164 off = pad_alloc(OP_PADSV, SVs_PADMY);
166 sv_upgrade(sv, SVt_PVNV);
168 if (PL_in_my_stash) {
170 yyerror(Perl_form(aTHX_ "Can't declare class for non-scalar %s in \"my\"",
173 (void)SvUPGRADE(sv, SVt_PVMG);
174 SvSTASH(sv) = (HV*)SvREFCNT_inc(PL_in_my_stash);
177 if (PL_in_my == KEY_our)
178 SvFLAGS(sv) |= SVpad_OUR;
179 av_store(PL_comppad_name, off, sv);
180 SvNVX(sv) = (NV)PAD_MAX;
181 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
182 if (!PL_min_intro_pending)
183 PL_min_intro_pending = off;
184 PL_max_intro_pending = off;
186 av_store(PL_comppad, off, (SV*)newAV());
187 else if (*name == '%')
188 av_store(PL_comppad, off, (SV*)newHV());
189 SvPADMY_on(PL_curpad[off]);
193 #define FINDLEX_NOSEARCH 1 /* don't search outer contexts */
196 S_pad_findlex(pTHX_ char *name, PADOFFSET newoff, U32 seq, CV* startcv,
197 I32 cx_ix, I32 saweval, U32 flags)
204 register PERL_CONTEXT *cx;
206 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
207 AV *curlist = CvPADLIST(cv);
208 SV **svp = av_fetch(curlist, 0, FALSE);
211 if (!svp || *svp == &PL_sv_undef)
214 svp = AvARRAY(curname);
215 for (off = AvFILLp(curname); off > 0; off--) {
216 if ((sv = svp[off]) &&
217 sv != &PL_sv_undef &&
219 seq > I_32(SvNVX(sv)) &&
220 strEQ(SvPVX(sv), name))
231 return 0; /* don't clone from inactive stack frame */
235 oldpad = (AV*)*av_fetch(curlist, depth, FALSE);
236 oldsv = *av_fetch(oldpad, off, TRUE);
237 if (!newoff) { /* Not a mere clone operation. */
238 SV *namesv = NEWSV(1103,0);
239 newoff = pad_alloc(OP_PADSV, SVs_PADMY);
240 sv_upgrade(namesv, SVt_PVNV);
241 sv_setpv(namesv, name);
242 av_store(PL_comppad_name, newoff, namesv);
243 SvNVX(namesv) = (NV)PL_curcop->cop_seq;
244 SvIVX(namesv) = PAD_MAX; /* A ref, intro immediately */
245 SvFAKE_on(namesv); /* A ref, not a real var */
246 if (SvFLAGS(sv) & SVpad_OUR)/* An "our" variable */
247 SvFLAGS(namesv) |= SVpad_OUR;
248 if (SvOBJECT(sv)) { /* A typed var */
250 (void)SvUPGRADE(namesv, SVt_PVMG);
251 SvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)SvSTASH(sv));
254 if (CvANON(PL_compcv) || SvTYPE(PL_compcv) == SVt_PVFM) {
255 /* "It's closures all the way down." */
256 CvCLONE_on(PL_compcv);
258 if (CvANON(PL_compcv))
259 oldsv = Nullsv; /* no need to keep ref */
264 bcv && bcv != cv && !CvCLONE(bcv);
265 bcv = CvOUTSIDE(bcv))
270 if (ckWARN(WARN_CLOSURE)
271 && !CvUNIQUE(bcv) && !CvUNIQUE(cv))
273 Perl_warner(aTHX_ WARN_CLOSURE,
274 "Variable \"%s\" may be unavailable",
282 else if (!CvUNIQUE(PL_compcv)) {
283 if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv))
284 Perl_warner(aTHX_ WARN_CLOSURE,
285 "Variable \"%s\" will not stay shared", name);
288 av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv));
294 if (flags & FINDLEX_NOSEARCH)
297 /* Nothing in current lexical context--try eval's context, if any.
298 * This is necessary to let the perldb get at lexically scoped variables.
299 * XXX This will also probably interact badly with eval tree caching.
302 for (i = cx_ix; i >= 0; i--) {
304 switch (CxTYPE(cx)) {
306 if (i == 0 && saweval) {
307 seq = cxstack[saweval].blk_oldcop->cop_seq;
308 return pad_findlex(name, newoff, seq, PL_main_cv, -1, saweval, 0);
312 switch (cx->blk_eval.old_op_type) {
318 /* require must have its own scope */
326 if (PL_debstash && CvSTASH(cv) == PL_debstash) { /* ignore DB'* scope */
327 saweval = i; /* so we know where we were called from */
330 seq = cxstack[saweval].blk_oldcop->cop_seq;
331 return pad_findlex(name, newoff, seq, cv, i-1, saweval,FINDLEX_NOSEARCH);
339 Perl_pad_findmy(pTHX_ char *name)
345 SV **svp = AvARRAY(PL_comppad_name);
346 U32 seq = PL_cop_seqmax;
352 * Special case to get lexical (and hence per-thread) @_.
353 * XXX I need to find out how to tell at parse-time whether use
354 * of @_ should refer to a lexical (from a sub) or defgv (global
355 * scope and maybe weird sub-ish things like formats). See
356 * startsub in perly.y. It's possible that @_ could be lexical
357 * (at least from subs) even in non-threaded perl.
359 if (strEQ(name, "@_"))
360 return 0; /* success. (NOT_IN_PAD indicates failure) */
361 #endif /* USE_THREADS */
363 /* The one we're looking for is probably just before comppad_name_fill. */
364 for (off = AvFILLp(PL_comppad_name); off > 0; off--) {
365 if ((sv = svp[off]) &&
366 sv != &PL_sv_undef &&
369 seq > I_32(SvNVX(sv)))) &&
370 strEQ(SvPVX(sv), name))
372 if (SvIVX(sv) || SvFLAGS(sv) & SVpad_OUR)
373 return (PADOFFSET)off;
374 pendoff = off; /* this pending def. will override import */
378 outside = CvOUTSIDE(PL_compcv);
380 /* Check if if we're compiling an eval'', and adjust seq to be the
381 * eval's seq number. This depends on eval'' having a non-null
382 * CvOUTSIDE() while it is being compiled. The eval'' itself is
383 * identified by CvEVAL being true and CvGV being null. */
384 if (outside && CvEVAL(PL_compcv) && !CvGV(PL_compcv) && cxstack_ix >= 0) {
385 cx = &cxstack[cxstack_ix];
387 seq = cx->blk_oldcop->cop_seq;
390 /* See if it's in a nested scope */
391 off = pad_findlex(name, 0, seq, outside, cxstack_ix, 0, 0);
393 /* If there is a pending local definition, this new alias must die */
395 SvIVX(AvARRAY(PL_comppad_name)[off]) = seq;
396 return off; /* pad_findlex returns 0 for failure...*/
398 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
402 Perl_pad_leavemy(pTHX_ I32 fill)
406 SV **svp = AvARRAY(PL_comppad_name);
408 if (PL_min_intro_pending && fill < PL_min_intro_pending) {
409 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
410 if ((sv = svp[off]) && sv != &PL_sv_undef && ckWARN_d(WARN_INTERNAL))
411 Perl_warner(aTHX_ WARN_INTERNAL, "%s never introduced", SvPVX(sv));
414 /* "Deintroduce" my variables that are leaving with this scope. */
415 for (off = AvFILLp(PL_comppad_name); off > fill; off--) {
416 if ((sv = svp[off]) && sv != &PL_sv_undef && SvIVX(sv) == PAD_MAX)
417 SvIVX(sv) = PL_cop_seqmax;
422 Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype)
428 if (AvARRAY(PL_comppad) != PL_curpad)
429 Perl_croak(aTHX_ "panic: pad_alloc");
430 if (PL_pad_reset_pending)
432 if (tmptype & SVs_PADMY) {
434 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
435 } while (SvPADBUSY(sv)); /* need a fresh one */
436 retval = AvFILLp(PL_comppad);
439 SV **names = AvARRAY(PL_comppad_name);
440 SSize_t names_fill = AvFILLp(PL_comppad_name);
443 * "foreach" index vars temporarily become aliases to non-"my"
444 * values. Thus we must skip, not just pad values that are
445 * marked as current pad values, but also those with names.
447 if (++PL_padix <= names_fill &&
448 (sv = names[PL_padix]) && sv != &PL_sv_undef)
450 sv = *av_fetch(PL_comppad, PL_padix, TRUE);
451 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)) && !IS_PADGV(sv))
456 SvFLAGS(sv) |= tmptype;
457 PL_curpad = AvARRAY(PL_comppad);
459 DEBUG_X(PerlIO_printf(Perl_debug_log,
460 "0x%"UVxf" Pad 0x%"UVxf" alloc %ld for %s\n",
461 PTR2UV(thr), PTR2UV(PL_curpad),
462 (long) retval, PL_op_name[optype]));
464 DEBUG_X(PerlIO_printf(Perl_debug_log,
465 "Pad 0x%"UVxf" alloc %ld for %s\n",
467 (long) retval, PL_op_name[optype]));
468 #endif /* USE_THREADS */
469 return (PADOFFSET)retval;
473 Perl_pad_sv(pTHX_ PADOFFSET po)
477 DEBUG_X(PerlIO_printf(Perl_debug_log,
478 "0x%"UVxf" Pad 0x%"UVxf" sv %d\n",
479 PTR2UV(thr), PTR2UV(PL_curpad), po));
482 Perl_croak(aTHX_ "panic: pad_sv po");
483 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" sv %d\n",
484 PTR2UV(PL_curpad), po));
485 #endif /* USE_THREADS */
486 return PL_curpad[po]; /* eventually we'll turn this into a macro */
490 Perl_pad_free(pTHX_ PADOFFSET po)
495 if (AvARRAY(PL_comppad) != PL_curpad)
496 Perl_croak(aTHX_ "panic: pad_free curpad");
498 Perl_croak(aTHX_ "panic: pad_free po");
500 DEBUG_X(PerlIO_printf(Perl_debug_log,
501 "0x%"UVxf" Pad 0x%"UVxf" free %d\n",
502 PTR2UV(thr), PTR2UV(PL_curpad), po));
504 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" free %d\n",
505 PTR2UV(PL_curpad), po));
506 #endif /* USE_THREADS */
507 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef)
508 SvPADTMP_off(PL_curpad[po]);
509 if ((I32)po < PL_padix)
514 Perl_pad_swipe(pTHX_ PADOFFSET po)
517 if (AvARRAY(PL_comppad) != PL_curpad)
518 Perl_croak(aTHX_ "panic: pad_swipe curpad");
520 Perl_croak(aTHX_ "panic: pad_swipe po");
522 DEBUG_X(PerlIO_printf(Perl_debug_log,
523 "0x%"UVxf" Pad 0x%"UVxf" swipe %d\n",
524 PTR2UV(thr), PTR2UV(PL_curpad), po));
526 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" swipe %d\n",
527 PTR2UV(PL_curpad), po));
528 #endif /* USE_THREADS */
529 SvPADTMP_off(PL_curpad[po]);
530 PL_curpad[po] = NEWSV(1107,0);
531 SvPADTMP_on(PL_curpad[po]);
532 if ((I32)po < PL_padix)
536 /* XXX pad_reset() is currently disabled because it results in serious bugs.
537 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
538 * on the stack by OPs that use them, there are several ways to get an alias
539 * to a shared TARG. Such an alias will change randomly and unpredictably.
540 * We avoid doing this until we can think of a Better Way.
545 #ifdef USE_BROKEN_PAD_RESET
549 if (AvARRAY(PL_comppad) != PL_curpad)
550 Perl_croak(aTHX_ "panic: pad_reset curpad");
552 DEBUG_X(PerlIO_printf(Perl_debug_log,
553 "0x%"UVxf" Pad 0x%"UVxf" reset\n",
554 PTR2UV(thr), PTR2UV(PL_curpad)));
556 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" reset\n",
558 #endif /* USE_THREADS */
559 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
560 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
561 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
562 SvPADTMP_off(PL_curpad[po]);
564 PL_padix = PL_padix_floor;
567 PL_pad_reset_pending = FALSE;
571 /* find_threadsv is not reentrant */
573 Perl_find_threadsv(pTHX_ const char *name)
579 /* We currently only handle names of a single character */
580 p = strchr(PL_threadsv_names, *name);
583 key = p - PL_threadsv_names;
584 MUTEX_LOCK(&thr->mutex);
585 svp = av_fetch(thr->threadsv, key, FALSE);
587 MUTEX_UNLOCK(&thr->mutex);
589 SV *sv = NEWSV(0, 0);
590 av_store(thr->threadsv, key, sv);
591 thr->threadsvp = AvARRAY(thr->threadsv);
592 MUTEX_UNLOCK(&thr->mutex);
594 * Some magic variables used to be automagically initialised
595 * in gv_fetchpv. Those which are now per-thread magicals get
596 * initialised here instead.
602 sv_setpv(sv, "\034");
603 sv_magic(sv, 0, 0, name, 1);
608 PL_sawampersand = TRUE;
622 /* XXX %! tied to Errno.pm needs to be added here.
623 * See gv_fetchpv(). */
627 sv_magic(sv, 0, 0, name, 1);
629 DEBUG_S(PerlIO_printf(Perl_error_log,
630 "find_threadsv: new SV %p for $%s%c\n",
631 sv, (*name < 32) ? "^" : "",
632 (*name < 32) ? toCTRL(*name) : *name));
636 #endif /* USE_THREADS */
641 Perl_op_free(pTHX_ OP *o)
643 register OP *kid, *nextkid;
646 if (!o || o->op_seq == (U16)-1)
649 if (o->op_private & OPpREFCOUNTED) {
650 switch (o->op_type) {
658 if (OpREFCNT_dec(o)) {
669 if (o->op_flags & OPf_KIDS) {
670 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
671 nextkid = kid->op_sibling; /* Get before next freeing kid */
679 /* COP* is not cleared by op_clear() so that we may track line
680 * numbers etc even after null() */
681 if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
686 #ifdef PL_OP_SLAB_ALLOC
687 if ((char *) o == PL_OpPtr)
696 S_op_clear(pTHX_ OP *o)
698 switch (o->op_type) {
699 case OP_NULL: /* Was holding old type, if any. */
700 case OP_ENTEREVAL: /* Was holding hints. */
702 case OP_THREADSV: /* Was holding index into thr->threadsv AV. */
708 if (!(o->op_flags & OPf_SPECIAL))
711 #endif /* USE_THREADS */
713 if (!(o->op_flags & OPf_REF)
714 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
723 pad_swipe(cPADOPo->op_padix);
724 /* No GvIN_PAD_off(gv) here, because other references may still
725 * exist on the pad */
728 cPADOPo->op_padix = 0;
730 SvREFCNT_dec(cGVOPo);
731 cSVOPo->op_sv = Nullsv;
735 SvREFCNT_dec(cSVOPo->op_sv);
736 cSVOPo->op_sv = Nullsv;
742 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
746 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
747 SvREFCNT_dec(cSVOPo->op_sv);
748 cSVOPo->op_sv = Nullsv;
751 Safefree(cPVOPo->op_pv);
752 cPVOPo->op_pv = Nullch;
756 op_free(cPMOPo->op_pmreplroot);
757 cPMOPo->op_pmreplroot = Nullop;
762 ReREFCNT_dec(cPMOPo->op_pmregexp);
763 cPMOPo->op_pmregexp = (REGEXP*)NULL;
767 if (o->op_targ > 0) {
768 pad_free(o->op_targ);
774 S_cop_free(pTHX_ COP* cop)
776 Safefree(cop->cop_label);
778 Safefree(CopFILE(cop)); /* XXXXX share in a pvtable? */
780 SvREFCNT_dec(CopFILEGV(cop));
782 if (! specialWARN(cop->cop_warnings))
783 SvREFCNT_dec(cop->cop_warnings);
789 if (o->op_type == OP_NULL)
792 o->op_targ = o->op_type;
793 o->op_type = OP_NULL;
794 o->op_ppaddr = PL_ppaddr[OP_NULL];
797 /* Contextualizers */
799 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
802 Perl_linklist(pTHX_ OP *o)
809 /* establish postfix order */
810 if (cUNOPo->op_first) {
811 o->op_next = LINKLIST(cUNOPo->op_first);
812 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
814 kid->op_next = LINKLIST(kid->op_sibling);
826 Perl_scalarkids(pTHX_ OP *o)
829 if (o && o->op_flags & OPf_KIDS) {
830 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
837 S_scalarboolean(pTHX_ OP *o)
839 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
841 if (ckWARN(WARN_SYNTAX)) {
842 line_t oldline = CopLINE(PL_curcop);
844 if (PL_copline != NOLINE)
845 CopLINE_set(PL_curcop, PL_copline);
846 Perl_warner(aTHX_ WARN_SYNTAX, "Found = in conditional, should be ==");
847 CopLINE_set(PL_curcop, oldline);
854 Perl_scalar(pTHX_ OP *o)
858 /* assumes no premature commitment */
859 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
860 || o->op_type == OP_RETURN)
865 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
867 switch (o->op_type) {
869 if (o->op_private & OPpREPEAT_DOLIST)
870 null(((LISTOP*)cBINOPo->op_first)->op_first);
871 scalar(cBINOPo->op_first);
876 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
880 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
881 if (!kPMOP->op_pmreplroot)
882 deprecate("implicit split to @_");
890 if (o->op_flags & OPf_KIDS) {
891 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
897 kid = cLISTOPo->op_first;
899 while (kid = kid->op_sibling) {
905 WITH_THR(PL_curcop = &PL_compiling);
910 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
916 WITH_THR(PL_curcop = &PL_compiling);
923 Perl_scalarvoid(pTHX_ OP *o)
930 if (o->op_type == OP_NEXTSTATE
931 || o->op_type == OP_SETSTATE
932 || o->op_type == OP_DBSTATE
933 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
934 || o->op_targ == OP_SETSTATE
935 || o->op_targ == OP_DBSTATE)))
938 PL_curcop = (COP*)o; /* for warning below */
941 /* assumes no premature commitment */
942 want = o->op_flags & OPf_WANT;
943 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
944 || o->op_type == OP_RETURN)
949 if ((o->op_private & OPpTARGET_MY)
950 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
952 return scalar(o); /* As if inside SASSIGN */
955 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
957 switch (o->op_type) {
959 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
963 if (o->op_flags & OPf_STACKED)
967 if (o->op_private == 4)
1009 case OP_GETSOCKNAME:
1010 case OP_GETPEERNAME:
1015 case OP_GETPRIORITY:
1038 if (!(o->op_private & OPpLVAL_INTRO))
1039 useless = PL_op_desc[o->op_type];
1046 if (!(o->op_private & OPpLVAL_INTRO) &&
1047 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1048 useless = "a variable";
1053 if (cSVOPo->op_private & OPpCONST_STRICT)
1054 no_bareword_allowed(o);
1057 if (ckWARN(WARN_VOID)) {
1058 useless = "a constant";
1059 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1061 else if (SvPOK(sv)) {
1062 if (strnEQ(SvPVX(sv), "di", 2) ||
1063 strnEQ(SvPVX(sv), "ds", 2) ||
1064 strnEQ(SvPVX(sv), "ig", 2))
1069 null(o); /* don't execute or even remember it */
1073 o->op_type = OP_PREINC; /* pre-increment is faster */
1074 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1078 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1079 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1085 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1090 if (o->op_flags & OPf_STACKED)
1098 if (!(o->op_flags & OPf_KIDS))
1107 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1114 /* all requires must return a boolean value */
1115 o->op_flags &= ~OPf_WANT;
1118 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
1119 if (!kPMOP->op_pmreplroot)
1120 deprecate("implicit split to @_");
1126 if (ckWARN(WARN_VOID))
1127 Perl_warner(aTHX_ WARN_VOID, "Useless use of %s in void context", useless);
1133 Perl_listkids(pTHX_ OP *o)
1136 if (o && o->op_flags & OPf_KIDS) {
1137 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1144 Perl_list(pTHX_ OP *o)
1148 /* assumes no premature commitment */
1149 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
1150 || o->op_type == OP_RETURN)
1155 if ((o->op_private & OPpTARGET_MY)
1156 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1158 return o; /* As if inside SASSIGN */
1161 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1163 switch (o->op_type) {
1166 list(cBINOPo->op_first);
1171 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1179 if (!(o->op_flags & OPf_KIDS))
1181 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1182 list(cBINOPo->op_first);
1183 return gen_constant_list(o);
1190 kid = cLISTOPo->op_first;
1192 while (kid = kid->op_sibling) {
1193 if (kid->op_sibling)
1198 WITH_THR(PL_curcop = &PL_compiling);
1202 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1203 if (kid->op_sibling)
1208 WITH_THR(PL_curcop = &PL_compiling);
1211 /* all requires must return a boolean value */
1212 o->op_flags &= ~OPf_WANT;
1219 Perl_scalarseq(pTHX_ OP *o)
1224 if (o->op_type == OP_LINESEQ ||
1225 o->op_type == OP_SCOPE ||
1226 o->op_type == OP_LEAVE ||
1227 o->op_type == OP_LEAVETRY)
1230 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1231 if (kid->op_sibling) {
1235 PL_curcop = &PL_compiling;
1237 o->op_flags &= ~OPf_PARENS;
1238 if (PL_hints & HINT_BLOCK_SCOPE)
1239 o->op_flags |= OPf_PARENS;
1242 o = newOP(OP_STUB, 0);
1247 S_modkids(pTHX_ OP *o, I32 type)
1250 if (o && o->op_flags & OPf_KIDS) {
1251 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1258 Perl_mod(pTHX_ OP *o, I32 type)
1265 if (!o || PL_error_count)
1268 if ((o->op_private & OPpTARGET_MY)
1269 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1274 switch (o->op_type) {
1279 if (!(o->op_private & (OPpCONST_ARYBASE)))
1281 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1282 PL_compiling.cop_arybase = (I32)SvIV(((SVOP*)PL_eval_start)->op_sv);
1286 SAVEI32(PL_compiling.cop_arybase);
1287 PL_compiling.cop_arybase = 0;
1289 else if (type == OP_REFGEN)
1292 Perl_croak(aTHX_ "That use of $[ is unsupported");
1295 if (o->op_flags & OPf_PARENS)
1299 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1300 !(o->op_flags & OPf_STACKED)) {
1301 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1302 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1303 assert(cUNOPo->op_first->op_type == OP_NULL);
1304 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1307 else { /* lvalue subroutine call */
1308 o->op_private |= OPpLVAL_INTRO;
1309 if (type == OP_GREPSTART || type == OP_ENTERSUB) {
1310 /* Backward compatibility mode: */
1311 o->op_private |= OPpENTERSUB_INARGS;
1314 else { /* Compile-time error message: */
1315 OP *kid = cUNOPo->op_first;
1319 if (kid->op_type == OP_PUSHMARK)
1321 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1323 "panic: unexpected lvalue entersub "
1324 "args: type/targ %ld:%ld",
1325 (long)kid->op_type,kid->op_targ);
1326 kid = kLISTOP->op_first;
1328 while (kid->op_sibling)
1329 kid = kid->op_sibling;
1330 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1332 if (kid->op_type == OP_METHOD_NAMED
1333 || kid->op_type == OP_METHOD)
1337 if (kid->op_sibling || kid->op_next != kid) {
1338 yyerror("panic: unexpected optree near method call");
1342 NewOp(1101, newop, 1, OP);
1343 newop->op_type = OP_RV2CV;
1344 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1345 newop->op_next = newop;
1346 kid->op_sibling = newop;
1347 newop->op_private |= OPpLVAL_INTRO;
1351 if (kid->op_type != OP_RV2CV)
1353 "panic: unexpected lvalue entersub "
1354 "entry via type/targ %ld:%ld",
1355 (long)kid->op_type,kid->op_targ);
1356 kid->op_private |= OPpLVAL_INTRO;
1357 break; /* Postpone until runtime */
1361 kid = kUNOP->op_first;
1362 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1363 kid = kUNOP->op_first;
1364 if (kid->op_type == OP_NULL)
1366 "Unexpected constant lvalue entersub "
1367 "entry via type/targ %ld:%ld",
1368 (long)kid->op_type,kid->op_targ);
1369 if (kid->op_type != OP_GV) {
1370 /* Restore RV2CV to check lvalueness */
1372 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1373 okid->op_next = kid->op_next;
1374 kid->op_next = okid;
1377 okid->op_next = Nullop;
1378 okid->op_type = OP_RV2CV;
1380 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1381 okid->op_private |= OPpLVAL_INTRO;
1395 /* grep, foreach, subcalls, refgen */
1396 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1398 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1399 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1401 : (o->op_type == OP_ENTERSUB
1402 ? "non-lvalue subroutine call"
1403 : PL_op_desc[o->op_type])),
1404 type ? PL_op_desc[type] : "local"));
1418 case OP_RIGHT_SHIFT:
1427 if (!(o->op_flags & OPf_STACKED))
1433 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1439 if (!type && cUNOPo->op_first->op_type != OP_GV)
1440 Perl_croak(aTHX_ "Can't localize through a reference");
1441 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1442 PL_modcount = 10000;
1443 return o; /* Treat \(@foo) like ordinary list. */
1447 if (scalar_mod_type(o, type))
1449 ref(cUNOPo->op_first, o->op_type);
1458 PL_modcount = 10000;
1461 if (!type && cUNOPo->op_first->op_type != OP_GV)
1462 Perl_croak(aTHX_ "Can't localize through a reference");
1463 ref(cUNOPo->op_first, o->op_type);
1467 PL_hints |= HINT_BLOCK_SCOPE;
1477 PL_modcount = 10000;
1478 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1479 return o; /* Treat \(@foo) like ordinary list. */
1480 if (scalar_mod_type(o, type))
1486 Perl_croak(aTHX_ "Can't localize lexical variable %s",
1487 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
1492 PL_modcount++; /* XXX ??? */
1494 #endif /* USE_THREADS */
1500 if (type != OP_SASSIGN)
1504 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1510 pad_free(o->op_targ);
1511 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1512 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1513 if (o->op_flags & OPf_KIDS)
1514 mod(cBINOPo->op_first->op_sibling, type);
1519 ref(cBINOPo->op_first, o->op_type);
1520 if (type == OP_ENTERSUB &&
1521 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1522 o->op_private |= OPpLVAL_DEFER;
1529 if (o->op_flags & OPf_KIDS)
1530 mod(cLISTOPo->op_last, type);
1534 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1536 else if (!(o->op_flags & OPf_KIDS))
1538 if (o->op_targ != OP_LIST) {
1539 mod(cBINOPo->op_first, type);
1544 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1548 o->op_flags |= OPf_MOD;
1550 if (type == OP_AASSIGN || type == OP_SASSIGN)
1551 o->op_flags |= OPf_SPECIAL|OPf_REF;
1553 o->op_private |= OPpLVAL_INTRO;
1554 o->op_flags &= ~OPf_SPECIAL;
1555 PL_hints |= HINT_BLOCK_SCOPE;
1557 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1558 o->op_flags |= OPf_REF;
1563 S_scalar_mod_type(pTHX_ OP *o, I32 type)
1567 if (o->op_type == OP_RV2GV)
1591 case OP_RIGHT_SHIFT:
1610 S_is_handle_constructor(pTHX_ OP *o, I32 argnum)
1612 switch (o->op_type) {
1620 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1633 Perl_refkids(pTHX_ OP *o, I32 type)
1636 if (o && o->op_flags & OPf_KIDS) {
1637 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1644 Perl_ref(pTHX_ OP *o, I32 type)
1648 if (!o || PL_error_count)
1651 switch (o->op_type) {
1653 if ((type == OP_DEFINED || type == OP_LOCK) &&
1654 !(o->op_flags & OPf_STACKED)) {
1655 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1656 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1657 assert(cUNOPo->op_first->op_type == OP_NULL);
1658 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1659 o->op_flags |= OPf_SPECIAL;
1664 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1668 if (type == OP_DEFINED)
1669 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1670 ref(cUNOPo->op_first, o->op_type);
1673 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1674 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1675 : type == OP_RV2HV ? OPpDEREF_HV
1677 o->op_flags |= OPf_MOD;
1682 o->op_flags |= OPf_MOD; /* XXX ??? */
1687 o->op_flags |= OPf_REF;
1690 if (type == OP_DEFINED)
1691 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1692 ref(cUNOPo->op_first, o->op_type);
1697 o->op_flags |= OPf_REF;
1702 if (!(o->op_flags & OPf_KIDS))
1704 ref(cBINOPo->op_first, type);
1708 ref(cBINOPo->op_first, o->op_type);
1709 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1710 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1711 : type == OP_RV2HV ? OPpDEREF_HV
1713 o->op_flags |= OPf_MOD;
1721 if (!(o->op_flags & OPf_KIDS))
1723 ref(cLISTOPo->op_last, type);
1733 S_dup_attrlist(pTHX_ OP *o)
1737 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1738 * where the first kid is OP_PUSHMARK and the remaining ones
1739 * are OP_CONST. We need to push the OP_CONST values.
1741 if (o->op_type == OP_CONST)
1742 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc(cSVOPo->op_sv));
1744 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1745 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1746 if (o->op_type == OP_CONST)
1747 rop = append_elem(OP_LIST, rop,
1748 newSVOP(OP_CONST, o->op_flags,
1749 SvREFCNT_inc(cSVOPo->op_sv)));
1756 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
1758 OP *modname; /* for 'use' */
1761 /* fake up C<use attributes $pkg,$rv,@attrs> */
1762 ENTER; /* need to protect against side-effects of 'use' */
1764 if (stash && HvNAME(stash))
1765 stashsv = newSVpv(HvNAME(stash), 0);
1767 stashsv = &PL_sv_no;
1768 #define ATTRSMODULE "attributes"
1769 modname = newSVOP(OP_CONST, 0,
1770 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1));
1771 modname->op_private |= OPpCONST_BARE;
1772 /* that flag is required to make 'use' work right */
1773 utilize(1, start_subparse(FALSE, 0),
1774 Nullop, /* version */
1776 prepend_elem(OP_LIST,
1777 newSVOP(OP_CONST, 0, stashsv),
1778 prepend_elem(OP_LIST,
1779 newSVOP(OP_CONST, 0, newRV(target)),
1780 dup_attrlist(attrs))));
1785 S_my_kid(pTHX_ OP *o, OP *attrs)
1790 if (!o || PL_error_count)
1794 if (type == OP_LIST) {
1795 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1797 } else if (type == OP_UNDEF) {
1799 } else if (type == OP_RV2SV || /* "our" declaration */
1801 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
1803 } else if (type != OP_PADSV &&
1806 type != OP_PUSHMARK)
1808 yyerror(Perl_form(aTHX_ "Can't declare %s in my", PL_op_desc[o->op_type]));
1811 else if (attrs && type != OP_PUSHMARK) {
1816 /* check for C<my Dog $spot> when deciding package */
1817 namesvp = av_fetch(PL_comppad_name, o->op_targ, FALSE);
1818 if (namesvp && *namesvp && SvOBJECT(*namesvp) && HvNAME(SvSTASH(*namesvp)))
1819 stash = SvSTASH(*namesvp);
1821 stash = PL_curstash;
1822 padsv = PAD_SV(o->op_targ);
1823 apply_attrs(stash, padsv, attrs);
1825 o->op_flags |= OPf_MOD;
1826 o->op_private |= OPpLVAL_INTRO;
1831 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1833 if (o->op_flags & OPf_PARENS)
1836 PL_in_my_stash = Nullhv;
1839 return my_kid(o, attrs);
1843 Perl_my(pTHX_ OP *o)
1845 return my_kid(o, Nullop);
1849 Perl_sawparens(pTHX_ OP *o)
1852 o->op_flags |= OPf_PARENS;
1857 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
1862 if (ckWARN(WARN_UNSAFE) &&
1863 (left->op_type == OP_RV2AV ||
1864 left->op_type == OP_RV2HV ||
1865 left->op_type == OP_PADAV ||
1866 left->op_type == OP_PADHV)) {
1867 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
1868 right->op_type == OP_TRANS)
1869 ? right->op_type : OP_MATCH];
1870 const char *sample = ((left->op_type == OP_RV2AV ||
1871 left->op_type == OP_PADAV)
1872 ? "@array" : "%hash");
1873 Perl_warner(aTHX_ WARN_UNSAFE,
1874 "Applying %s to %s will act on scalar(%s)",
1875 desc, sample, sample);
1878 if (right->op_type == OP_MATCH ||
1879 right->op_type == OP_SUBST ||
1880 right->op_type == OP_TRANS) {
1881 right->op_flags |= OPf_STACKED;
1882 if (right->op_type != OP_MATCH)
1883 left = mod(left, right->op_type);
1884 if (right->op_type == OP_TRANS)
1885 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1887 o = prepend_elem(right->op_type, scalar(left), right);
1889 return newUNOP(OP_NOT, 0, scalar(o));
1893 return bind_match(type, left,
1894 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1898 Perl_invert(pTHX_ OP *o)
1902 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1903 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1907 Perl_scope(pTHX_ OP *o)
1910 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
1911 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1912 o->op_type = OP_LEAVE;
1913 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
1916 if (o->op_type == OP_LINESEQ) {
1918 o->op_type = OP_SCOPE;
1919 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
1920 kid = ((LISTOP*)o)->op_first;
1921 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
1925 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1932 Perl_save_hints(pTHX)
1935 SAVESPTR(GvHV(PL_hintgv));
1936 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
1937 SAVEFREESV(GvHV(PL_hintgv));
1941 Perl_block_start(pTHX_ int full)
1944 int retval = PL_savestack_ix;
1946 SAVEI32(PL_comppad_name_floor);
1948 if ((PL_comppad_name_fill = AvFILLp(PL_comppad_name)) > 0)
1949 PL_comppad_name_floor = PL_comppad_name_fill;
1951 PL_comppad_name_floor = 0;
1953 SAVEI32(PL_min_intro_pending);
1954 SAVEI32(PL_max_intro_pending);
1955 PL_min_intro_pending = 0;
1956 SAVEI32(PL_comppad_name_fill);
1957 SAVEI32(PL_padix_floor);
1958 PL_padix_floor = PL_padix;
1959 PL_pad_reset_pending = FALSE;
1961 PL_hints &= ~HINT_BLOCK_SCOPE;
1962 SAVEPPTR(PL_compiling.cop_warnings);
1963 if (! specialWARN(PL_compiling.cop_warnings)) {
1964 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1965 SAVEFREESV(PL_compiling.cop_warnings) ;
1973 Perl_block_end(pTHX_ I32 floor, OP *seq)
1976 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
1977 OP* retval = scalarseq(seq);
1979 PL_pad_reset_pending = FALSE;
1980 PL_compiling.op_private = PL_hints;
1982 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1983 pad_leavemy(PL_comppad_name_fill);
1992 OP *o = newOP(OP_THREADSV, 0);
1993 o->op_targ = find_threadsv("_");
1996 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
1997 #endif /* USE_THREADS */
2001 Perl_newPROG(pTHX_ OP *o)
2007 PL_eval_root = newUNOP(OP_LEAVEEVAL,
2008 ((PL_in_eval & EVAL_KEEPERR)
2009 ? OPf_SPECIAL : 0), o);
2010 PL_eval_start = linklist(PL_eval_root);
2011 PL_eval_root->op_private |= OPpREFCOUNTED;
2012 OpREFCNT_set(PL_eval_root, 1);
2013 PL_eval_root->op_next = 0;
2014 peep(PL_eval_start);
2019 PL_main_root = scope(sawparens(scalarvoid(o)));
2020 PL_curcop = &PL_compiling;
2021 PL_main_start = LINKLIST(PL_main_root);
2022 PL_main_root->op_private |= OPpREFCOUNTED;
2023 OpREFCNT_set(PL_main_root, 1);
2024 PL_main_root->op_next = 0;
2025 peep(PL_main_start);
2028 /* Register with debugger */
2030 CV *cv = get_cv("DB::postponed", FALSE);
2034 XPUSHs((SV*)CopFILEGV(&PL_compiling));
2036 call_sv((SV*)cv, G_DISCARD);
2043 Perl_localize(pTHX_ OP *o, I32 lex)
2045 if (o->op_flags & OPf_PARENS)
2049 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
2051 for (s = PL_bufptr; *s && (isALNUM(*s) || (*s & 0x80) || strchr("@$%, ",*s)); s++) ;
2052 if (*s == ';' || *s == '=')
2053 Perl_warner(aTHX_ WARN_PARENTHESIS, "Parentheses missing around \"%s\" list",
2054 lex ? "my" : "local");
2058 PL_in_my_stash = Nullhv;
2062 return mod(o, OP_NULL); /* a bit kludgey */
2066 Perl_jmaybe(pTHX_ OP *o)
2068 if (o->op_type == OP_LIST) {
2071 o2 = newOP(OP_THREADSV, 0);
2072 o2->op_targ = find_threadsv(";");
2074 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
2075 #endif /* USE_THREADS */
2076 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2082 Perl_fold_constants(pTHX_ register OP *o)
2086 I32 type = o->op_type;
2089 if (PL_opargs[type] & OA_RETSCALAR)
2091 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2092 o->op_targ = pad_alloc(type, SVs_PADTMP);
2094 /* integerize op, unless it happens to be C<-foo>.
2095 * XXX should pp_i_negate() do magic string negation instead? */
2096 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2097 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2098 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2100 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2103 if (!(PL_opargs[type] & OA_FOLDCONST))
2108 /* XXX might want a ck_negate() for this */
2109 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2122 if (o->op_private & OPpLOCALE)
2127 goto nope; /* Don't try to run w/ errors */
2129 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2130 if ((curop->op_type != OP_CONST ||
2131 (curop->op_private & OPpCONST_BARE)) &&
2132 curop->op_type != OP_LIST &&
2133 curop->op_type != OP_SCALAR &&
2134 curop->op_type != OP_NULL &&
2135 curop->op_type != OP_PUSHMARK)
2141 curop = LINKLIST(o);
2145 sv = *(PL_stack_sp--);
2146 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
2147 pad_swipe(o->op_targ);
2148 else if (SvTEMP(sv)) { /* grab mortal temp? */
2149 (void)SvREFCNT_inc(sv);
2153 if (type == OP_RV2GV)
2154 return newGVOP(OP_GV, 0, (GV*)sv);
2156 /* try to smush double to int, but don't smush -2.0 to -2 */
2157 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
2161 if ((NV)iv == SvNV(sv)) {
2166 SvIOK_off(sv); /* undo SvIV() damage */
2168 return newSVOP(OP_CONST, 0, sv);
2172 if (!(PL_opargs[type] & OA_OTHERINT))
2175 if (!(PL_hints & HINT_INTEGER)) {
2176 if (type == OP_MODULO
2177 || type == OP_DIVIDE
2178 || !(o->op_flags & OPf_KIDS))
2183 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
2184 if (curop->op_type == OP_CONST) {
2185 if (SvIOK(((SVOP*)curop)->op_sv))
2189 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
2193 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
2200 Perl_gen_constant_list(pTHX_ register OP *o)
2204 I32 oldtmps_floor = PL_tmps_floor;
2208 return o; /* Don't attempt to run with errors */
2210 PL_op = curop = LINKLIST(o);
2216 PL_tmps_floor = oldtmps_floor;
2218 o->op_type = OP_RV2AV;
2219 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2220 curop = ((UNOP*)o)->op_first;
2221 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
2228 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2233 if (!o || o->op_type != OP_LIST)
2234 o = newLISTOP(OP_LIST, 0, o, Nullop);
2236 o->op_flags &= ~OPf_WANT;
2238 if (!(PL_opargs[type] & OA_MARK))
2239 null(cLISTOPo->op_first);
2242 o->op_ppaddr = PL_ppaddr[type];
2243 o->op_flags |= flags;
2245 o = CHECKOP(type, o);
2246 if (o->op_type != type)
2249 if (cLISTOPo->op_children < 7) {
2250 /* XXX do we really need to do this if we're done appending?? */
2251 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2253 cLISTOPo->op_last = last; /* in case check substituted last arg */
2256 return fold_constants(o);
2259 /* List constructors */
2262 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2270 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
2271 return newLISTOP(type, 0, first, last);
2273 if (first->op_flags & OPf_KIDS)
2274 ((LISTOP*)first)->op_last->op_sibling = last;
2276 first->op_flags |= OPf_KIDS;
2277 ((LISTOP*)first)->op_first = last;
2279 ((LISTOP*)first)->op_last = last;
2280 ((LISTOP*)first)->op_children++;
2285 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2293 if (first->op_type != type)
2294 return prepend_elem(type, (OP*)first, (OP*)last);
2296 if (last->op_type != type)
2297 return append_elem(type, (OP*)first, (OP*)last);
2299 first->op_last->op_sibling = last->op_first;
2300 first->op_last = last->op_last;
2301 first->op_children += last->op_children;
2302 if (first->op_children)
2303 first->op_flags |= OPf_KIDS;
2305 #ifdef PL_OP_SLAB_ALLOC
2313 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2321 if (last->op_type == type) {
2322 if (type == OP_LIST) { /* already a PUSHMARK there */
2323 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2324 ((LISTOP*)last)->op_first->op_sibling = first;
2327 if (!(last->op_flags & OPf_KIDS)) {
2328 ((LISTOP*)last)->op_last = first;
2329 last->op_flags |= OPf_KIDS;
2331 first->op_sibling = ((LISTOP*)last)->op_first;
2332 ((LISTOP*)last)->op_first = first;
2334 ((LISTOP*)last)->op_children++;
2338 return newLISTOP(type, 0, first, last);
2344 Perl_newNULLLIST(pTHX)
2346 return newOP(OP_STUB, 0);
2350 Perl_force_list(pTHX_ OP *o)
2352 if (!o || o->op_type != OP_LIST)
2353 o = newLISTOP(OP_LIST, 0, o, Nullop);
2359 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2363 NewOp(1101, listop, 1, LISTOP);
2365 listop->op_type = type;
2366 listop->op_ppaddr = PL_ppaddr[type];
2367 listop->op_children = (first != 0) + (last != 0);
2368 listop->op_flags = flags;
2372 else if (!first && last)
2375 first->op_sibling = last;
2376 listop->op_first = first;
2377 listop->op_last = last;
2378 if (type == OP_LIST) {
2380 pushop = newOP(OP_PUSHMARK, 0);
2381 pushop->op_sibling = first;
2382 listop->op_first = pushop;
2383 listop->op_flags |= OPf_KIDS;
2385 listop->op_last = pushop;
2387 else if (listop->op_children)
2388 listop->op_flags |= OPf_KIDS;
2394 Perl_newOP(pTHX_ I32 type, I32 flags)
2397 NewOp(1101, o, 1, OP);
2399 o->op_ppaddr = PL_ppaddr[type];
2400 o->op_flags = flags;
2403 o->op_private = 0 + (flags >> 8);
2404 if (PL_opargs[type] & OA_RETSCALAR)
2406 if (PL_opargs[type] & OA_TARGET)
2407 o->op_targ = pad_alloc(type, SVs_PADTMP);
2408 return CHECKOP(type, o);
2412 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
2417 first = newOP(OP_STUB, 0);
2418 if (PL_opargs[type] & OA_MARK)
2419 first = force_list(first);
2421 NewOp(1101, unop, 1, UNOP);
2422 unop->op_type = type;
2423 unop->op_ppaddr = PL_ppaddr[type];
2424 unop->op_first = first;
2425 unop->op_flags = flags | OPf_KIDS;
2426 unop->op_private = 1 | (flags >> 8);
2427 unop = (UNOP*) CHECKOP(type, unop);
2431 return fold_constants((OP *) unop);
2435 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2438 NewOp(1101, binop, 1, BINOP);
2441 first = newOP(OP_NULL, 0);
2443 binop->op_type = type;
2444 binop->op_ppaddr = PL_ppaddr[type];
2445 binop->op_first = first;
2446 binop->op_flags = flags | OPf_KIDS;
2449 binop->op_private = 1 | (flags >> 8);
2452 binop->op_private = 2 | (flags >> 8);
2453 first->op_sibling = last;
2456 binop = (BINOP*)CHECKOP(type, binop);
2457 if (binop->op_next || binop->op_type != type)
2460 binop->op_last = binop->op_first->op_sibling;
2462 return fold_constants((OP *)binop);
2466 utf8compare(const void *a, const void *b)
2469 for (i = 0; i < 10; i++) {
2470 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2472 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2479 Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
2481 SV *tstr = ((SVOP*)expr)->op_sv;
2482 SV *rstr = ((SVOP*)repl)->op_sv;
2485 register U8 *t = (U8*)SvPV(tstr, tlen);
2486 register U8 *r = (U8*)SvPV(rstr, rlen);
2492 register short *tbl;
2494 complement = o->op_private & OPpTRANS_COMPLEMENT;
2495 del = o->op_private & OPpTRANS_DELETE;
2496 squash = o->op_private & OPpTRANS_SQUASH;
2498 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2499 SV* listsv = newSVpvn("# comment\n",10);
2501 U8* tend = t + tlen;
2502 U8* rend = r + rlen;
2518 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2519 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2525 New(1109, cp, tlen, U8*);
2527 transv = newSVpvn("",0);
2536 qsort(cp, i, sizeof(U8*), utf8compare);
2537 for (j = 0; j < i; j++) {
2539 UV val = utf8_to_uv(s, &ulen);
2541 diff = val - nextmin;
2543 t = uv_to_utf8(tmpbuf,nextmin);
2544 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2546 t = uv_to_utf8(tmpbuf, val - 1);
2547 sv_catpvn(transv, "\377", 1);
2548 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2552 val = utf8_to_uv(s+1, &ulen);
2556 t = uv_to_utf8(tmpbuf,nextmin);
2557 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2558 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2559 sv_catpvn(transv, "\377", 1);
2560 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2561 t = (U8*)SvPVX(transv);
2562 tlen = SvCUR(transv);
2565 else if (!rlen && !del) {
2566 r = t; rlen = tlen; rend = tend;
2569 if (to_utf && from_utf) { /* only counting characters */
2570 if (t == r || (tlen == rlen && memEQ(t, r, tlen)))
2571 o->op_private |= OPpTRANS_IDENTICAL;
2573 else { /* straight latin-1 translation */
2574 if (tlen == 4 && memEQ(t, "\0\377\303\277", 4) &&
2575 rlen == 4 && memEQ(r, "\0\377\303\277", 4))
2576 o->op_private |= OPpTRANS_IDENTICAL;
2580 while (t < tend || tfirst <= tlast) {
2581 /* see if we need more "t" chars */
2582 if (tfirst > tlast) {
2583 tfirst = (I32)utf8_to_uv(t, &ulen);
2585 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
2586 tlast = (I32)utf8_to_uv(++t, &ulen);
2593 /* now see if we need more "r" chars */
2594 if (rfirst > rlast) {
2596 rfirst = (I32)utf8_to_uv(r, &ulen);
2598 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
2599 rlast = (I32)utf8_to_uv(++r, &ulen);
2608 rfirst = rlast = 0xffffffff;
2612 /* now see which range will peter our first, if either. */
2613 tdiff = tlast - tfirst;
2614 rdiff = rlast - rfirst;
2621 if (rfirst == 0xffffffff) {
2622 diff = tdiff; /* oops, pretend rdiff is infinite */
2624 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\tXXXX\n", tfirst, tlast);
2626 Perl_sv_catpvf(aTHX_ listsv, "%04x\t\tXXXX\n", tfirst);
2630 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\t%04x\n", tfirst, tfirst + diff, rfirst);
2632 Perl_sv_catpvf(aTHX_ listsv, "%04x\t\t%04x\n", tfirst, rfirst);
2634 if (rfirst + diff > max)
2635 max = rfirst + diff;
2640 else if (rfirst <= 0x800)
2641 grows |= (tfirst < 0x80);
2642 else if (rfirst <= 0x10000)
2643 grows |= (tfirst < 0x800);
2644 else if (rfirst <= 0x200000)
2645 grows |= (tfirst < 0x10000);
2646 else if (rfirst <= 0x4000000)
2647 grows |= (tfirst < 0x200000);
2648 else if (rfirst <= 0x80000000)
2649 grows |= (tfirst < 0x4000000);
2661 else if (max > 0xff)
2666 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2667 SvREFCNT_dec(listsv);
2669 SvREFCNT_dec(transv);
2671 if (!del && havefinal)
2672 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5, newSViv((IV)final), 0);
2674 if (grows && to_utf)
2675 o->op_private |= OPpTRANS_GROWS;
2682 tbl = (short*)cPVOPo->op_pv;
2684 Zero(tbl, 256, short);
2685 for (i = 0; i < tlen; i++)
2687 for (i = 0, j = 0; i < 256; i++) {
2703 if (!rlen && !del) {
2706 o->op_private |= OPpTRANS_IDENTICAL;
2708 for (i = 0; i < 256; i++)
2710 for (i = 0, j = 0; i < tlen; i++,j++) {
2713 if (tbl[t[i]] == -1)
2719 if (tbl[t[i]] == -1)
2730 Perl_newPMOP(pTHX_ I32 type, I32 flags)
2735 NewOp(1101, pmop, 1, PMOP);
2736 pmop->op_type = type;
2737 pmop->op_ppaddr = PL_ppaddr[type];
2738 pmop->op_flags = flags;
2739 pmop->op_private = 0 | (flags >> 8);
2741 if (PL_hints & HINT_RE_TAINT)
2742 pmop->op_pmpermflags |= PMf_RETAINT;
2743 if (PL_hints & HINT_LOCALE)
2744 pmop->op_pmpermflags |= PMf_LOCALE;
2745 pmop->op_pmflags = pmop->op_pmpermflags;
2747 /* link into pm list */
2748 if (type != OP_TRANS && PL_curstash) {
2749 pmop->op_pmnext = HvPMROOT(PL_curstash);
2750 HvPMROOT(PL_curstash) = pmop;
2757 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
2762 I32 repl_has_vars = 0;
2764 if (o->op_type == OP_TRANS)
2765 return pmtrans(o, expr, repl);
2767 PL_hints |= HINT_BLOCK_SCOPE;
2770 if (expr->op_type == OP_CONST) {
2772 SV *pat = ((SVOP*)expr)->op_sv;
2773 char *p = SvPV(pat, plen);
2774 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2775 sv_setpvn(pat, "\\s+", 3);
2776 p = SvPV(pat, plen);
2777 pm->op_pmflags |= PMf_SKIPWHITE;
2779 pm->op_pmregexp = CALLREGCOMP(aTHX_ p, p + plen, pm);
2780 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2781 pm->op_pmflags |= PMf_WHITE;
2785 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2786 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2788 : OP_REGCMAYBE),0,expr);
2790 NewOp(1101, rcop, 1, LOGOP);
2791 rcop->op_type = OP_REGCOMP;
2792 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2793 rcop->op_first = scalar(expr);
2794 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2795 ? (OPf_SPECIAL | OPf_KIDS)
2797 rcop->op_private = 1;
2800 /* establish postfix order */
2801 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2803 rcop->op_next = expr;
2804 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2807 rcop->op_next = LINKLIST(expr);
2808 expr->op_next = (OP*)rcop;
2811 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2816 if (pm->op_pmflags & PMf_EVAL) {
2818 if (CopLINE(PL_curcop) < PL_multi_end)
2819 CopLINE_set(PL_curcop, PL_multi_end);
2822 else if (repl->op_type == OP_THREADSV
2823 && strchr("&`'123456789+",
2824 PL_threadsv_names[repl->op_targ]))
2828 #endif /* USE_THREADS */
2829 else if (repl->op_type == OP_CONST)
2833 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2834 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2836 if (curop->op_type == OP_THREADSV) {
2838 if (strchr("&`'123456789+", curop->op_private))
2842 if (curop->op_type == OP_GV) {
2843 GV *gv = cGVOPx(curop);
2845 if (strchr("&`'123456789+", *GvENAME(gv)))
2848 #endif /* USE_THREADS */
2849 else if (curop->op_type == OP_RV2CV)
2851 else if (curop->op_type == OP_RV2SV ||
2852 curop->op_type == OP_RV2AV ||
2853 curop->op_type == OP_RV2HV ||
2854 curop->op_type == OP_RV2GV) {
2855 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2858 else if (curop->op_type == OP_PADSV ||
2859 curop->op_type == OP_PADAV ||
2860 curop->op_type == OP_PADHV ||
2861 curop->op_type == OP_PADANY) {
2864 else if (curop->op_type == OP_PUSHRE)
2865 ; /* Okay here, dangerous in newASSIGNOP */
2874 && (!pm->op_pmregexp
2875 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
2876 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2877 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2878 prepend_elem(o->op_type, scalar(repl), o);
2881 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
2882 pm->op_pmflags |= PMf_MAYBE_CONST;
2883 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2885 NewOp(1101, rcop, 1, LOGOP);
2886 rcop->op_type = OP_SUBSTCONT;
2887 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
2888 rcop->op_first = scalar(repl);
2889 rcop->op_flags |= OPf_KIDS;
2890 rcop->op_private = 1;
2893 /* establish postfix order */
2894 rcop->op_next = LINKLIST(repl);
2895 repl->op_next = (OP*)rcop;
2897 pm->op_pmreplroot = scalar((OP*)rcop);
2898 pm->op_pmreplstart = LINKLIST(rcop);
2907 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
2910 NewOp(1101, svop, 1, SVOP);
2911 svop->op_type = type;
2912 svop->op_ppaddr = PL_ppaddr[type];
2914 svop->op_next = (OP*)svop;
2915 svop->op_flags = flags;
2916 if (PL_opargs[type] & OA_RETSCALAR)
2918 if (PL_opargs[type] & OA_TARGET)
2919 svop->op_targ = pad_alloc(type, SVs_PADTMP);
2920 return CHECKOP(type, svop);
2924 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
2927 NewOp(1101, padop, 1, PADOP);
2928 padop->op_type = type;
2929 padop->op_ppaddr = PL_ppaddr[type];
2930 padop->op_padix = pad_alloc(type, SVs_PADTMP);
2931 PL_curpad[padop->op_padix] = sv;
2932 padop->op_next = (OP*)padop;
2933 padop->op_flags = flags;
2934 if (PL_opargs[type] & OA_RETSCALAR)
2936 if (PL_opargs[type] & OA_TARGET)
2937 padop->op_targ = pad_alloc(type, SVs_PADTMP);
2938 return CHECKOP(type, padop);
2942 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
2947 return newPADOP(type, flags, SvREFCNT_inc(gv));
2949 return newSVOP(type, flags, SvREFCNT_inc(gv));
2954 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
2957 NewOp(1101, pvop, 1, PVOP);
2958 pvop->op_type = type;
2959 pvop->op_ppaddr = PL_ppaddr[type];
2961 pvop->op_next = (OP*)pvop;
2962 pvop->op_flags = flags;
2963 if (PL_opargs[type] & OA_RETSCALAR)
2965 if (PL_opargs[type] & OA_TARGET)
2966 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2967 return CHECKOP(type, pvop);
2971 Perl_package(pTHX_ OP *o)
2976 save_hptr(&PL_curstash);
2977 save_item(PL_curstname);
2982 name = SvPV(sv, len);
2983 PL_curstash = gv_stashpvn(name,len,TRUE);
2984 sv_setpvn(PL_curstname, name, len);
2988 sv_setpv(PL_curstname,"<none>");
2989 PL_curstash = Nullhv;
2991 PL_hints |= HINT_BLOCK_SCOPE;
2992 PL_copline = NOLINE;
2997 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg)
3006 if (id->op_type != OP_CONST)
3007 Perl_croak(aTHX_ "Module name must be constant");
3011 if(version != Nullop) {
3012 SV *vesv = ((SVOP*)version)->op_sv;
3014 if (arg == Nullop && !SvNIOK(vesv)) {
3020 if (version->op_type != OP_CONST || !SvNIOK(vesv))
3021 Perl_croak(aTHX_ "Version number must be constant number");
3023 /* Make copy of id so we don't free it twice */
3024 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3026 /* Fake up a method call to VERSION */
3027 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3028 append_elem(OP_LIST,
3029 prepend_elem(OP_LIST, pack, list(version)),
3030 newSVOP(OP_METHOD_NAMED, 0,
3031 newSVpvn("VERSION", 7))));
3035 /* Fake up an import/unimport */
3036 if (arg && arg->op_type == OP_STUB)
3037 imop = arg; /* no import on explicit () */
3038 else if(SvNIOK(((SVOP*)id)->op_sv)) {
3039 imop = Nullop; /* use 5.0; */
3042 /* Make copy of id so we don't free it twice */
3043 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3044 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3045 append_elem(OP_LIST,
3046 prepend_elem(OP_LIST, pack, list(arg)),
3047 newSVOP(OP_METHOD_NAMED, 0,
3048 aver ? newSVpvn("import", 6)
3049 : newSVpvn("unimport", 8))));
3052 /* Fake up a require, handle override, if any */
3053 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
3054 if (!(gv && GvIMPORTED_CV(gv)))
3055 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
3057 if (gv && GvIMPORTED_CV(gv)) {
3058 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3059 append_elem(OP_LIST, id,
3060 scalar(newUNOP(OP_RV2CV, 0,
3065 rqop = newUNOP(OP_REQUIRE, 0, id);
3068 /* Fake up the BEGIN {}, which does its thing immediately. */
3070 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
3073 append_elem(OP_LINESEQ,
3074 append_elem(OP_LINESEQ,
3075 newSTATEOP(0, Nullch, rqop),
3076 newSTATEOP(0, Nullch, veop)),
3077 newSTATEOP(0, Nullch, imop) ));
3079 PL_hints |= HINT_BLOCK_SCOPE;
3080 PL_copline = NOLINE;
3085 Perl_dofile(pTHX_ OP *term)
3090 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
3091 if (!(gv && GvIMPORTED_CV(gv)))
3092 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
3094 if (gv && GvIMPORTED_CV(gv)) {
3095 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3096 append_elem(OP_LIST, term,
3097 scalar(newUNOP(OP_RV2CV, 0,
3102 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3108 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
3110 return newBINOP(OP_LSLICE, flags,
3111 list(force_list(subscript)),
3112 list(force_list(listval)) );
3116 S_list_assignment(pTHX_ register OP *o)
3121 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3122 o = cUNOPo->op_first;
3124 if (o->op_type == OP_COND_EXPR) {
3125 I32 t = list_assignment(cLOGOPo->op_first->op_sibling);
3126 I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
3131 yyerror("Assignment to both a list and a scalar");
3135 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3136 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3137 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
3140 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
3143 if (o->op_type == OP_RV2SV)
3150 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
3155 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
3156 return newLOGOP(optype, 0,
3157 mod(scalar(left), optype),
3158 newUNOP(OP_SASSIGN, 0, scalar(right)));
3161 return newBINOP(optype, OPf_STACKED,
3162 mod(scalar(left), optype), scalar(right));
3166 if (list_assignment(left)) {
3169 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3170 left = mod(left, OP_AASSIGN);
3178 o = newBINOP(OP_AASSIGN, flags,
3179 list(force_list(right)),
3180 list(force_list(left)) );
3181 o->op_private = 0 | (flags >> 8);
3182 if (!(left->op_private & OPpLVAL_INTRO)) {
3186 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3187 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3188 if (curop->op_type == OP_GV) {
3189 GV *gv = cGVOPx(curop);
3190 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3192 SvCUR(gv) = PL_generation;
3194 else if (curop->op_type == OP_PADSV ||
3195 curop->op_type == OP_PADAV ||
3196 curop->op_type == OP_PADHV ||
3197 curop->op_type == OP_PADANY) {
3198 SV **svp = AvARRAY(PL_comppad_name);
3199 SV *sv = svp[curop->op_targ];
3200 if (SvCUR(sv) == PL_generation)
3202 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
3204 else if (curop->op_type == OP_RV2CV)
3206 else if (curop->op_type == OP_RV2SV ||
3207 curop->op_type == OP_RV2AV ||
3208 curop->op_type == OP_RV2HV ||
3209 curop->op_type == OP_RV2GV) {
3210 if (lastop->op_type != OP_GV) /* funny deref? */
3213 else if (curop->op_type == OP_PUSHRE) {
3214 if (((PMOP*)curop)->op_pmreplroot) {
3215 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3216 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3218 SvCUR(gv) = PL_generation;
3227 o->op_private = OPpASSIGN_COMMON;
3229 if (right && right->op_type == OP_SPLIT) {
3231 if ((tmpop = ((LISTOP*)right)->op_first) &&
3232 tmpop->op_type == OP_PUSHRE)
3234 PMOP *pm = (PMOP*)tmpop;
3235 if (left->op_type == OP_RV2AV &&
3236 !(left->op_private & OPpLVAL_INTRO) &&
3237 !(o->op_private & OPpASSIGN_COMMON) )
3239 tmpop = ((UNOP*)left)->op_first;
3240 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
3241 pm->op_pmreplroot = (OP*)cGVOPx(tmpop);
3242 pm->op_pmflags |= PMf_ONCE;
3243 tmpop = cUNOPo->op_first; /* to list (nulled) */
3244 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3245 tmpop->op_sibling = Nullop; /* don't free split */
3246 right->op_next = tmpop->op_next; /* fix starting loc */
3247 op_free(o); /* blow off assign */
3248 right->op_flags &= ~OPf_WANT;
3249 /* "I don't know and I don't care." */
3254 if (PL_modcount < 10000 &&
3255 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3257 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3259 sv_setiv(sv, PL_modcount+1);
3267 right = newOP(OP_UNDEF, 0);
3268 if (right->op_type == OP_READLINE) {
3269 right->op_flags |= OPf_STACKED;
3270 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3273 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3274 o = newBINOP(OP_SASSIGN, flags,
3275 scalar(right), mod(scalar(left), OP_SASSIGN) );
3287 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3290 U32 seq = intro_my();
3293 NewOp(1101, cop, 1, COP);
3294 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
3295 cop->op_type = OP_DBSTATE;
3296 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3299 cop->op_type = OP_NEXTSTATE;
3300 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3302 cop->op_flags = flags;
3303 cop->op_private = (PL_hints & HINT_UTF8);
3305 cop->op_private |= NATIVE_HINTS;
3307 PL_compiling.op_private = cop->op_private;
3308 cop->op_next = (OP*)cop;
3311 cop->cop_label = label;
3312 PL_hints |= HINT_BLOCK_SCOPE;
3315 cop->cop_arybase = PL_curcop->cop_arybase;
3316 if (specialWARN(PL_curcop->cop_warnings))
3317 cop->cop_warnings = PL_curcop->cop_warnings ;
3319 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3322 if (PL_copline == NOLINE)
3323 CopLINE_set(cop, CopLINE(PL_curcop));
3325 CopLINE_set(cop, PL_copline);
3326 PL_copline = NOLINE;
3329 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXXXX share in a pvtable? */
3331 CopFILEGV_set(cop, (GV*)SvREFCNT_inc(CopFILEGV(PL_curcop)));
3333 cop->cop_stash = PL_curstash;
3335 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3336 SV **svp = av_fetch(CopFILEAV(PL_curcop), (I32)CopLINE(cop), FALSE);
3337 if (svp && *svp != &PL_sv_undef && !SvIOK(*svp)) {
3338 (void)SvIOK_on(*svp);
3340 SvSTASH(*svp) = (HV*)cop;
3344 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3347 /* "Introduce" my variables to visible status. */
3355 if (! PL_min_intro_pending)
3356 return PL_cop_seqmax;
3358 svp = AvARRAY(PL_comppad_name);
3359 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
3360 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
3361 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
3362 SvNVX(sv) = (NV)PL_cop_seqmax;
3365 PL_min_intro_pending = 0;
3366 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
3367 return PL_cop_seqmax++;
3371 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
3373 return new_logop(type, flags, &first, &other);
3377 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
3382 OP *first = *firstp;
3383 OP *other = *otherp;
3385 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3386 return newBINOP(type, flags, scalar(first), scalar(other));
3388 scalarboolean(first);
3389 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3390 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3391 if (type == OP_AND || type == OP_OR) {
3397 first = *firstp = cUNOPo->op_first;
3399 first->op_next = o->op_next;
3400 cUNOPo->op_first = Nullop;
3404 if (first->op_type == OP_CONST) {
3405 if (ckWARN(WARN_PRECEDENCE) && (first->op_private & OPpCONST_BARE))
3406 Perl_warner(aTHX_ WARN_PRECEDENCE, "Probable precedence problem on %s",
3408 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3419 else if (first->op_type == OP_WANTARRAY) {
3425 else if (ckWARN(WARN_UNSAFE) && (first->op_flags & OPf_KIDS)) {
3426 OP *k1 = ((UNOP*)first)->op_first;
3427 OP *k2 = k1->op_sibling;
3429 switch (first->op_type)
3432 if (k2 && k2->op_type == OP_READLINE
3433 && (k2->op_flags & OPf_STACKED)
3434 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3436 warnop = k2->op_type;
3441 if (k1->op_type == OP_READDIR
3442 || k1->op_type == OP_GLOB
3443 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3444 || k1->op_type == OP_EACH)
3446 warnop = ((k1->op_type == OP_NULL)
3447 ? k1->op_targ : k1->op_type);
3452 line_t oldline = CopLINE(PL_curcop);
3453 CopLINE_set(PL_curcop, PL_copline);
3454 Perl_warner(aTHX_ WARN_UNSAFE,
3455 "Value of %s%s can be \"0\"; test with defined()",
3457 ((warnop == OP_READLINE || warnop == OP_GLOB)
3458 ? " construct" : "() operator"));
3459 CopLINE_set(PL_curcop, oldline);
3466 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3467 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3469 NewOp(1101, logop, 1, LOGOP);
3471 logop->op_type = type;
3472 logop->op_ppaddr = PL_ppaddr[type];
3473 logop->op_first = first;
3474 logop->op_flags = flags | OPf_KIDS;
3475 logop->op_other = LINKLIST(other);
3476 logop->op_private = 1 | (flags >> 8);
3478 /* establish postfix order */
3479 logop->op_next = LINKLIST(first);
3480 first->op_next = (OP*)logop;
3481 first->op_sibling = other;
3483 o = newUNOP(OP_NULL, 0, (OP*)logop);
3490 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
3498 return newLOGOP(OP_AND, 0, first, trueop);
3500 return newLOGOP(OP_OR, 0, first, falseop);
3502 scalarboolean(first);
3503 if (first->op_type == OP_CONST) {
3504 if (SvTRUE(((SVOP*)first)->op_sv)) {
3515 else if (first->op_type == OP_WANTARRAY) {
3519 NewOp(1101, logop, 1, LOGOP);
3520 logop->op_type = OP_COND_EXPR;
3521 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3522 logop->op_first = first;
3523 logop->op_flags = flags | OPf_KIDS;
3524 logop->op_private = 1 | (flags >> 8);
3525 logop->op_other = LINKLIST(trueop);
3526 logop->op_next = LINKLIST(falseop);
3529 /* establish postfix order */
3530 start = LINKLIST(first);
3531 first->op_next = (OP*)logop;
3533 first->op_sibling = trueop;
3534 trueop->op_sibling = falseop;
3535 o = newUNOP(OP_NULL, 0, (OP*)logop);
3537 trueop->op_next = falseop->op_next = o;
3544 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
3553 NewOp(1101, range, 1, LOGOP);
3555 range->op_type = OP_RANGE;
3556 range->op_ppaddr = PL_ppaddr[OP_RANGE];
3557 range->op_first = left;
3558 range->op_flags = OPf_KIDS;
3559 leftstart = LINKLIST(left);
3560 range->op_other = LINKLIST(right);
3561 range->op_private = 1 | (flags >> 8);
3563 left->op_sibling = right;
3565 range->op_next = (OP*)range;
3566 flip = newUNOP(OP_FLIP, flags, (OP*)range);
3567 flop = newUNOP(OP_FLOP, 0, flip);
3568 o = newUNOP(OP_NULL, 0, flop);
3570 range->op_next = leftstart;
3572 left->op_next = flip;
3573 right->op_next = flop;
3575 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3576 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
3577 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3578 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3580 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3581 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3584 if (!flip->op_private || !flop->op_private)
3585 linklist(o); /* blow off optimizer unless constant */
3591 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
3596 int once = block && block->op_flags & OPf_SPECIAL &&
3597 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3600 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3601 return block; /* do {} while 0 does once */
3602 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3603 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3604 expr = newUNOP(OP_DEFINED, 0,
3605 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3606 } else if (expr->op_flags & OPf_KIDS) {
3607 OP *k1 = ((UNOP*)expr)->op_first;
3608 OP *k2 = (k1) ? k1->op_sibling : NULL;
3609 switch (expr->op_type) {
3611 if (k2 && k2->op_type == OP_READLINE
3612 && (k2->op_flags & OPf_STACKED)
3613 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3614 expr = newUNOP(OP_DEFINED, 0, expr);
3618 if (k1->op_type == OP_READDIR
3619 || k1->op_type == OP_GLOB
3620 || (k1->op_type == OP_NULL && k1->op_targ == OP_NULL)
3621 || k1->op_type == OP_EACH)
3622 expr = newUNOP(OP_DEFINED, 0, expr);
3628 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3629 o = new_logop(OP_AND, 0, &expr, &listop);
3632 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3634 if (once && o != listop)
3635 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3638 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3640 o->op_flags |= flags;
3642 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3647 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
3656 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3657 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
3658 expr = newUNOP(OP_DEFINED, 0,
3659 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3660 } else if (expr && (expr->op_flags & OPf_KIDS)) {
3661 OP *k1 = ((UNOP*)expr)->op_first;
3662 OP *k2 = (k1) ? k1->op_sibling : NULL;
3663 switch (expr->op_type) {
3665 if (k2 && k2->op_type == OP_READLINE
3666 && (k2->op_flags & OPf_STACKED)
3667 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3668 expr = newUNOP(OP_DEFINED, 0, expr);
3672 if (k1->op_type == OP_READDIR
3673 || k1->op_type == OP_GLOB
3674 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3675 || k1->op_type == OP_EACH)
3676 expr = newUNOP(OP_DEFINED, 0, expr);
3682 block = newOP(OP_NULL, 0);
3685 next = LINKLIST(cont);
3687 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
3688 if ((line_t)whileline != NOLINE) {
3689 PL_copline = whileline;
3690 cont = append_elem(OP_LINESEQ, cont,
3691 newSTATEOP(0, Nullch, Nullop));
3695 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3696 redo = LINKLIST(listop);
3699 PL_copline = whileline;
3701 o = new_logop(OP_AND, 0, &expr, &listop);
3702 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3703 op_free(expr); /* oops, it's a while (0) */
3705 return Nullop; /* listop already freed by new_logop */
3708 ((LISTOP*)listop)->op_last->op_next = condop =
3709 (o == listop ? redo : LINKLIST(o));
3717 NewOp(1101,loop,1,LOOP);
3718 loop->op_type = OP_ENTERLOOP;
3719 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3720 loop->op_private = 0;
3721 loop->op_next = (OP*)loop;
3724 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3726 loop->op_redoop = redo;
3727 loop->op_lastop = o;
3730 loop->op_nextop = next;
3732 loop->op_nextop = o;
3734 o->op_flags |= flags;
3735 o->op_private |= (flags >> 8);
3740 Perl_newFOROP(pTHX_ I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
3749 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3750 sv->op_type = OP_RV2GV;
3751 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
3753 else if (sv->op_type == OP_PADSV) { /* private variable */
3754 padoff = sv->op_targ;
3759 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3760 padoff = sv->op_targ;
3762 iterflags |= OPf_SPECIAL;
3767 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
3771 padoff = find_threadsv("_");
3772 iterflags |= OPf_SPECIAL;
3774 sv = newGVOP(OP_GV, 0, PL_defgv);
3777 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3778 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3779 iterflags |= OPf_STACKED;
3781 else if (expr->op_type == OP_NULL &&
3782 (expr->op_flags & OPf_KIDS) &&
3783 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3785 /* Basically turn for($x..$y) into the same as for($x,$y), but we
3786 * set the STACKED flag to indicate that these values are to be
3787 * treated as min/max values by 'pp_iterinit'.
3789 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3790 LOGOP* range = (LOGOP*) flip->op_first;
3791 OP* left = range->op_first;
3792 OP* right = left->op_sibling;
3795 range->op_flags &= ~OPf_KIDS;
3796 range->op_first = Nullop;
3798 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3799 listop->op_first->op_next = range->op_next;
3800 left->op_next = range->op_other;
3801 right->op_next = (OP*)listop;
3802 listop->op_next = listop->op_first;
3805 expr = (OP*)(listop);
3807 iterflags |= OPf_STACKED;
3810 expr = mod(force_list(expr), OP_GREPSTART);
3814 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
3815 append_elem(OP_LIST, expr, scalar(sv))));
3816 assert(!loop->op_next);
3817 #ifdef PL_OP_SLAB_ALLOC
3818 NewOp(1234,tmp,1,LOOP);
3819 Copy(loop,tmp,1,LOOP);
3822 Renew(loop, 1, LOOP);
3824 loop->op_targ = padoff;
3825 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
3826 PL_copline = forline;
3827 return newSTATEOP(0, label, wop);
3831 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
3837 if (type != OP_GOTO || label->op_type == OP_CONST) {
3838 /* "last()" means "last" */
3839 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
3840 o = newOP(type, OPf_SPECIAL);
3842 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
3843 ? SvPVx(((SVOP*)label)->op_sv, n_a)
3849 if (label->op_type == OP_ENTERSUB)
3850 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
3851 o = newUNOP(type, OPf_STACKED, label);
3853 PL_hints |= HINT_BLOCK_SCOPE;
3858 Perl_cv_undef(pTHX_ CV *cv)
3863 MUTEX_DESTROY(CvMUTEXP(cv));
3864 Safefree(CvMUTEXP(cv));
3867 #endif /* USE_THREADS */
3869 if (!CvXSUB(cv) && CvROOT(cv)) {
3871 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
3872 Perl_croak(aTHX_ "Can't undef active subroutine");
3875 Perl_croak(aTHX_ "Can't undef active subroutine");
3876 #endif /* USE_THREADS */
3879 SAVESPTR(PL_curpad);
3883 op_free(CvROOT(cv));
3884 CvROOT(cv) = Nullop;
3887 SvPOK_off((SV*)cv); /* forget prototype */
3889 SvREFCNT_dec(CvGV(cv));
3891 SvREFCNT_dec(CvOUTSIDE(cv));
3892 CvOUTSIDE(cv) = Nullcv;
3893 if (CvPADLIST(cv)) {
3894 /* may be during global destruction */
3895 if (SvREFCNT(CvPADLIST(cv))) {
3896 I32 i = AvFILLp(CvPADLIST(cv));
3898 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
3899 SV* sv = svp ? *svp : Nullsv;
3902 if (sv == (SV*)PL_comppad_name)
3903 PL_comppad_name = Nullav;
3904 else if (sv == (SV*)PL_comppad) {
3905 PL_comppad = Nullav;
3906 PL_curpad = Null(SV**);
3910 SvREFCNT_dec((SV*)CvPADLIST(cv));
3912 CvPADLIST(cv) = Nullav;
3918 S_cv_dump(pTHX_ CV *cv)
3920 CV *outside = CvOUTSIDE(cv);
3921 AV* padlist = CvPADLIST(cv);
3928 PerlIO_printf(Perl_debug_log,
3929 "\tCV=0x%"UVxf" (%s), OUTSIDE=0x%"UVxf" (%s)\n",
3931 (CvANON(cv) ? "ANON"
3932 : (cv == PL_main_cv) ? "MAIN"
3933 : CvUNIQUE(cv) ? "UNIQUE"
3934 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
3937 : CvANON(outside) ? "ANON"
3938 : (outside == PL_main_cv) ? "MAIN"
3939 : CvUNIQUE(outside) ? "UNIQUE"
3940 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
3945 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
3946 pad = (AV*)*av_fetch(padlist, 1, FALSE);
3947 pname = AvARRAY(pad_name);
3948 ppad = AvARRAY(pad);
3950 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
3951 if (SvPOK(pname[ix]))
3952 PerlIO_printf(Perl_debug_log,
3953 "\t%4d. 0x%"UVxf" (%s\"%s\" %"IVdf"-%"IVdf")\n",
3954 ix, PTR2UV(ppad[ix]),
3955 SvFAKE(pname[ix]) ? "FAKE " : "",
3957 (IV)I_32(SvNVX(pname[ix])),
3961 #endif /* DEBUGGING */
3964 S_cv_clone2(pTHX_ CV *proto, CV *outside)
3969 AV* protopadlist = CvPADLIST(proto);
3970 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
3971 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
3972 SV** pname = AvARRAY(protopad_name);
3973 SV** ppad = AvARRAY(protopad);
3974 I32 fname = AvFILLp(protopad_name);
3975 I32 fpad = AvFILLp(protopad);
3979 assert(!CvUNIQUE(proto));
3982 SAVESPTR(PL_curpad);
3983 SAVESPTR(PL_comppad);
3984 SAVESPTR(PL_comppad_name);
3985 SAVESPTR(PL_compcv);
3987 cv = PL_compcv = (CV*)NEWSV(1104,0);
3988 sv_upgrade((SV *)cv, SvTYPE(proto));
3994 New(666, CvMUTEXP(cv), 1, perl_mutex);
3995 MUTEX_INIT(CvMUTEXP(cv));
3997 #endif /* USE_THREADS */
3998 CvFILE(cv) = CvFILE(proto);
3999 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
4000 CvSTASH(cv) = CvSTASH(proto);
4001 CvROOT(cv) = CvROOT(proto);
4002 CvSTART(cv) = CvSTART(proto);
4004 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
4007 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
4009 PL_comppad_name = newAV();
4010 for (ix = fname; ix >= 0; ix--)
4011 av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix]));
4013 PL_comppad = newAV();
4015 comppadlist = newAV();
4016 AvREAL_off(comppadlist);
4017 av_store(comppadlist, 0, (SV*)PL_comppad_name);
4018 av_store(comppadlist, 1, (SV*)PL_comppad);
4019 CvPADLIST(cv) = comppadlist;
4020 av_fill(PL_comppad, AvFILLp(protopad));
4021 PL_curpad = AvARRAY(PL_comppad);
4023 av = newAV(); /* will be @_ */
4025 av_store(PL_comppad, 0, (SV*)av);
4026 AvFLAGS(av) = AVf_REIFY;
4028 for (ix = fpad; ix > 0; ix--) {
4029 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
4030 if (namesv && namesv != &PL_sv_undef) {
4031 char *name = SvPVX(namesv); /* XXX */
4032 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
4033 I32 off = pad_findlex(name, ix, SvIVX(namesv),
4034 CvOUTSIDE(cv), cxstack_ix, 0, 0);
4036 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
4038 Perl_croak(aTHX_ "panic: cv_clone: %s", name);
4040 else { /* our own lexical */
4043 /* anon code -- we'll come back for it */
4044 sv = SvREFCNT_inc(ppad[ix]);
4046 else if (*name == '@')
4048 else if (*name == '%')
4057 else if (IS_PADGV(ppad[ix])) {
4058 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
4061 SV* sv = NEWSV(0,0);
4067 /* Now that vars are all in place, clone nested closures. */
4069 for (ix = fpad; ix > 0; ix--) {
4070 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
4072 && namesv != &PL_sv_undef
4073 && !(SvFLAGS(namesv) & SVf_FAKE)
4074 && *SvPVX(namesv) == '&'
4075 && CvCLONE(ppad[ix]))
4077 CV *kid = cv_clone2((CV*)ppad[ix], cv);
4078 SvREFCNT_dec(ppad[ix]);
4081 PL_curpad[ix] = (SV*)kid;
4085 #ifdef DEBUG_CLOSURES
4086 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
4088 PerlIO_printf(Perl_debug_log, " from:\n");
4090 PerlIO_printf(Perl_debug_log, " to:\n");
4099 Perl_cv_clone(pTHX_ CV *proto)
4102 MUTEX_LOCK(&PL_cred_mutex); /* XXX create separate mutex */
4103 cv = cv_clone2(proto, CvOUTSIDE(proto));
4104 MUTEX_UNLOCK(&PL_cred_mutex); /* XXX create separate mutex */
4109 Perl_cv_ckproto(pTHX_ CV *cv, GV *gv, char *p)
4113 if (((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) && ckWARN_d(WARN_UNSAFE)) {
4114 SV* msg = sv_newmortal();
4118 gv_efullname3(name = sv_newmortal(), gv, Nullch);
4119 sv_setpv(msg, "Prototype mismatch:");
4121 Perl_sv_catpvf(aTHX_ msg, " sub %_", name);
4123 Perl_sv_catpvf(aTHX_ msg, " (%s)", SvPVX(cv));
4124 sv_catpv(msg, " vs ");
4126 Perl_sv_catpvf(aTHX_ msg, "(%s)", p);
4128 sv_catpv(msg, "none");
4129 Perl_warner(aTHX_ WARN_UNSAFE, "%_", msg);
4134 Perl_cv_const_sv(pTHX_ CV *cv)
4136 if (!cv || !SvPOK(cv) || SvCUR(cv))
4138 return op_const_sv(CvSTART(cv), cv);
4142 Perl_op_const_sv(pTHX_ OP *o, CV *cv)
4149 if(o->op_type == OP_LINESEQ && cLISTOPo->op_first)
4150 o = cLISTOPo->op_first->op_sibling;
4152 for (; o; o = o->op_next) {
4153 OPCODE type = o->op_type;
4155 if (sv && o->op_next == o)
4157 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
4159 if (type == OP_LEAVESUB || type == OP_RETURN)
4163 if (type == OP_CONST)
4165 else if (type == OP_PADSV && cv) {
4166 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
4167 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
4168 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
4180 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4190 Perl_croak(aTHX_ "\"my sub\" not yet implemented");
4194 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
4196 return Perl_newATTRSUB(aTHX_ floor, o, proto, Nullop, block);
4200 Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4204 char *name = o ? SvPVx(cSVOPo->op_sv, n_a) : Nullch;
4205 GV *gv = gv_fetchpv(name ? name : "__ANON__",
4206 GV_ADDMULTI | ((block || attrs) ? 0 : GV_NOINIT),
4208 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, n_a) : Nullch;
4219 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
4220 maximum a prototype before. */
4221 if (SvTYPE(gv) > SVt_NULL) {
4222 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1)
4223 && ckWARN_d(WARN_UNSAFE))
4225 Perl_warner(aTHX_ WARN_UNSAFE, "Runaway prototype");
4227 cv_ckproto((CV*)gv, NULL, ps);
4230 sv_setpv((SV*)gv, ps);
4232 sv_setiv((SV*)gv, -1);
4233 SvREFCNT_dec(PL_compcv);
4234 cv = PL_compcv = NULL;
4235 PL_sub_generation++;
4239 if (!name || GvCVGEN(gv))
4241 else if (cv = GvCV(gv)) {
4242 cv_ckproto(cv, gv, ps);
4243 /* already defined (or promised)? */
4244 if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4246 bool const_changed = TRUE;
4247 if (!block && !attrs) {
4248 /* just a "sub foo;" when &foo is already defined */
4249 SAVEFREESV(PL_compcv);
4252 /* ahem, death to those who redefine active sort subs */
4253 if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv))
4254 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", name);
4257 if(const_sv = cv_const_sv(cv))
4258 const_changed = sv_cmp(const_sv, op_const_sv(block, Nullcv));
4259 if ((const_sv && const_changed) || ckWARN(WARN_REDEFINE)
4260 && !(CvGV(cv) && GvSTASH(CvGV(cv))
4261 && HvNAME(GvSTASH(CvGV(cv)))
4262 && strEQ(HvNAME(GvSTASH(CvGV(cv))),
4264 line_t oldline = CopLINE(PL_curcop);
4265 CopLINE_set(PL_curcop, PL_copline);
4266 Perl_warner(aTHX_ WARN_REDEFINE,
4267 const_sv ? "Constant subroutine %s redefined"
4268 : "Subroutine %s redefined", name);
4269 CopLINE_set(PL_curcop, oldline);
4280 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>
4281 * before we clobber PL_compcv.
4285 if (CvGV(cv) && GvSTASH(CvGV(cv)) && HvNAME(GvSTASH(CvGV(cv))))
4286 stash = GvSTASH(CvGV(cv));
4287 else if (CvSTASH(cv) && HvNAME(CvSTASH(cv)))
4288 stash = CvSTASH(cv);
4290 stash = PL_curstash;
4293 /* possibly about to re-define existing subr -- ignore old cv */
4294 rcv = (SV*)PL_compcv;
4295 if (name && GvSTASH(gv) && HvNAME(GvSTASH(gv)))
4296 stash = GvSTASH(gv);
4298 stash = PL_curstash;
4300 apply_attrs(stash, rcv, attrs);
4302 if (cv) { /* must reuse cv if autoloaded */
4304 /* got here with just attrs -- work done, so bug out */
4305 SAVEFREESV(PL_compcv);
4309 CvFLAGS(cv) = CvFLAGS(PL_compcv);
4310 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
4311 CvOUTSIDE(PL_compcv) = 0;
4312 CvPADLIST(cv) = CvPADLIST(PL_compcv);
4313 CvPADLIST(PL_compcv) = 0;
4314 if (SvREFCNT(PL_compcv) > 1) /* XXX Make closures transit through stub. */
4315 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc((SV*)cv);
4316 SvREFCNT_dec(PL_compcv);
4323 PL_sub_generation++;
4326 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4327 CvFILE(cv) = CopFILE(PL_curcop);
4328 CvSTASH(cv) = PL_curstash;
4331 if (!CvMUTEXP(cv)) {
4332 New(666, CvMUTEXP(cv), 1, perl_mutex);
4333 MUTEX_INIT(CvMUTEXP(cv));
4335 #endif /* USE_THREADS */
4338 sv_setpv((SV*)cv, ps);
4340 if (PL_error_count) {
4344 char *s = strrchr(name, ':');
4346 if (strEQ(s, "BEGIN")) {
4348 "BEGIN not safe after errors--compilation aborted";
4349 if (PL_in_eval & EVAL_KEEPERR)
4350 Perl_croak(aTHX_ not_safe);
4352 /* force display of errors found but not reported */
4353 sv_catpv(ERRSV, not_safe);
4354 Perl_croak(aTHX_ "%s", SvPVx(ERRSV, n_a));
4361 PL_copline = NOLINE;
4366 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad))
4367 av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv);
4370 SV **namep = AvARRAY(PL_comppad_name);
4371 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4374 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]))
4377 * The only things that a clonable function needs in its
4378 * pad are references to outer lexicals and anonymous subs.
4379 * The rest are created anew during cloning.
4381 if (!((namesv = namep[ix]) != Nullsv &&
4382 namesv != &PL_sv_undef &&
4384 *SvPVX(namesv) == '&')))
4386 SvREFCNT_dec(PL_curpad[ix]);
4387 PL_curpad[ix] = Nullsv;
4392 AV *av = newAV(); /* Will be @_ */
4394 av_store(PL_comppad, 0, (SV*)av);
4395 AvFLAGS(av) = AVf_REIFY;
4397 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4398 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]))
4400 if (!SvPADMY(PL_curpad[ix]))
4401 SvPADTMP_on(PL_curpad[ix]);
4406 CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0, scalarseq(block));
4409 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4411 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4412 OpREFCNT_set(CvROOT(cv), 1);
4413 CvSTART(cv) = LINKLIST(CvROOT(cv));
4414 CvROOT(cv)->op_next = 0;
4420 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4421 SV *sv = NEWSV(0,0);
4422 SV *tmpstr = sv_newmortal();
4423 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
4427 Perl_sv_setpvf(aTHX_ sv, "%_:%ld-%ld",
4428 CopFILESV(PL_curcop),
4429 (long)PL_subline, (long)CopLINE(PL_curcop));
4430 gv_efullname3(tmpstr, gv, Nullch);
4431 hv_store(GvHV(PL_DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
4432 hv = GvHVn(db_postponed);
4433 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
4434 && (cv = GvCV(db_postponed))) {
4439 call_sv((SV*)cv, G_DISCARD);
4443 if ((s = strrchr(name,':')))
4447 if (strEQ(s, "BEGIN")) {
4448 I32 oldscope = PL_scopestack_ix;
4450 SAVECOPFILE(&PL_compiling);
4451 SAVECOPLINE(&PL_compiling);
4453 sv_setsv(PL_rs, PL_nrs);
4456 PL_beginav = newAV();
4457 DEBUG_x( dump_sub(gv) );
4458 av_push(PL_beginav, (SV *)cv);
4460 call_list(oldscope, PL_beginav);
4462 PL_curcop = &PL_compiling;
4463 PL_compiling.op_private = PL_hints;
4466 else if (strEQ(s, "END") && !PL_error_count) {
4469 av_unshift(PL_endav, 1);
4470 av_store(PL_endav, 0, (SV *)cv);
4473 else if (strEQ(s, "STOP") && !PL_error_count) {
4475 PL_stopav = newAV();
4476 av_unshift(PL_stopav, 1);
4477 av_store(PL_stopav, 0, (SV *)cv);
4480 else if (strEQ(s, "INIT") && !PL_error_count) {
4482 PL_initav = newAV();
4483 av_push(PL_initav, SvREFCNT_inc(cv));
4489 PL_copline = NOLINE;
4494 /* XXX unsafe for threads if eval_owner isn't held */
4496 Perl_newCONSTSUB(pTHX_ HV *stash, char *name, SV *sv)
4499 U32 oldhints = PL_hints;
4500 HV *old_cop_stash = PL_curcop->cop_stash;
4501 HV *old_curstash = PL_curstash;
4502 line_t oldline = CopLINE(PL_curcop);
4503 CopLINE_set(PL_curcop, PL_copline);
4505 PL_hints &= ~HINT_BLOCK_SCOPE;
4507 PL_curstash = PL_curcop->cop_stash = stash;
4510 start_subparse(FALSE, 0),
4511 newSVOP(OP_CONST, 0, newSVpv(name,0)),
4512 newSVOP(OP_CONST, 0, &PL_sv_no), /* SvPV(&PL_sv_no) == "" -- GMB */
4514 newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv))
4517 PL_hints = oldhints;
4518 PL_curcop->cop_stash = old_cop_stash;
4519 PL_curstash = old_curstash;
4520 CopLINE_set(PL_curcop, oldline);
4524 Perl_newXS(pTHX_ char *name, XSUBADDR_t subaddr, char *filename)
4527 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
4530 if (cv = (name ? GvCV(gv) : Nullcv)) {
4532 /* just a cached method */
4536 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4537 /* already defined (or promised) */
4538 if (ckWARN(WARN_REDEFINE) && !(CvGV(cv) && GvSTASH(CvGV(cv))
4539 && HvNAME(GvSTASH(CvGV(cv)))
4540 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
4541 line_t oldline = CopLINE(PL_curcop);
4542 if (PL_copline != NOLINE)
4543 CopLINE_set(PL_curcop, PL_copline);
4544 Perl_warner(aTHX_ WARN_REDEFINE, "Subroutine %s redefined",name);
4545 CopLINE_set(PL_curcop, oldline);
4552 if (cv) /* must reuse cv if autoloaded */
4555 cv = (CV*)NEWSV(1105,0);
4556 sv_upgrade((SV *)cv, SVt_PVCV);
4560 PL_sub_generation++;
4563 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4565 New(666, CvMUTEXP(cv), 1, perl_mutex);
4566 MUTEX_INIT(CvMUTEXP(cv));
4568 #endif /* USE_THREADS */
4569 (void)gv_fetchfile(filename);
4570 CvFILE(cv) = filename; /* NOTE: not copied, as it is expected to be
4571 an external constant string */
4572 CvXSUB(cv) = subaddr;
4575 char *s = strrchr(name,':');
4580 if (strEQ(s, "BEGIN")) {
4582 PL_beginav = newAV();
4583 av_push(PL_beginav, (SV *)cv);
4586 else if (strEQ(s, "END")) {
4589 av_unshift(PL_endav, 1);
4590 av_store(PL_endav, 0, (SV *)cv);
4593 else if (strEQ(s, "STOP")) {
4595 PL_stopav = newAV();
4596 av_unshift(PL_stopav, 1);
4597 av_store(PL_stopav, 0, (SV *)cv);
4600 else if (strEQ(s, "INIT")) {
4602 PL_initav = newAV();
4603 av_push(PL_initav, (SV *)cv);
4614 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
4624 name = SvPVx(cSVOPo->op_sv, n_a);
4627 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
4629 if (cv = GvFORM(gv)) {
4630 if (ckWARN(WARN_REDEFINE)) {
4631 line_t oldline = CopLINE(PL_curcop);
4633 CopLINE_set(PL_curcop, PL_copline);
4634 Perl_warner(aTHX_ WARN_REDEFINE, "Format %s redefined",name);
4635 CopLINE_set(PL_curcop, oldline);
4641 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4642 CvFILE(cv) = CopFILE(PL_curcop);
4644 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4645 if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix]))
4646 SvPADTMP_on(PL_curpad[ix]);
4649 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4650 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4651 OpREFCNT_set(CvROOT(cv), 1);
4652 CvSTART(cv) = LINKLIST(CvROOT(cv));
4653 CvROOT(cv)->op_next = 0;
4656 PL_copline = NOLINE;
4661 Perl_newANONLIST(pTHX_ OP *o)
4663 return newUNOP(OP_REFGEN, 0,
4664 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
4668 Perl_newANONHASH(pTHX_ OP *o)
4670 return newUNOP(OP_REFGEN, 0,
4671 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
4675 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
4677 return newANONATTRSUB(floor, proto, Nullop, block);
4681 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
4683 return newUNOP(OP_REFGEN, 0,
4684 newSVOP(OP_ANONCODE, 0,
4685 (SV*)newATTRSUB(floor, 0, proto, attrs, block)));
4689 Perl_oopsAV(pTHX_ OP *o)
4691 switch (o->op_type) {
4693 o->op_type = OP_PADAV;
4694 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4695 return ref(o, OP_RV2AV);
4698 o->op_type = OP_RV2AV;
4699 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4704 if (ckWARN_d(WARN_INTERNAL))
4705 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsAV");
4712 Perl_oopsHV(pTHX_ OP *o)
4716 switch (o->op_type) {
4719 o->op_type = OP_PADHV;
4720 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4721 return ref(o, OP_RV2HV);
4725 o->op_type = OP_RV2HV;
4726 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
4731 if (ckWARN_d(WARN_INTERNAL))
4732 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsHV");
4739 Perl_newAVREF(pTHX_ OP *o)
4741 if (o->op_type == OP_PADANY) {
4742 o->op_type = OP_PADAV;
4743 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4746 return newUNOP(OP_RV2AV, 0, scalar(o));
4750 Perl_newGVREF(pTHX_ I32 type, OP *o)
4752 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
4753 return newUNOP(OP_NULL, 0, o);
4754 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
4758 Perl_newHVREF(pTHX_ OP *o)
4760 if (o->op_type == OP_PADANY) {
4761 o->op_type = OP_PADHV;
4762 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4765 return newUNOP(OP_RV2HV, 0, scalar(o));
4769 Perl_oopsCV(pTHX_ OP *o)
4771 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
4777 Perl_newCVREF(pTHX_ I32 flags, OP *o)
4779 return newUNOP(OP_RV2CV, flags, scalar(o));
4783 Perl_newSVREF(pTHX_ OP *o)
4785 if (o->op_type == OP_PADANY) {
4786 o->op_type = OP_PADSV;
4787 o->op_ppaddr = PL_ppaddr[OP_PADSV];
4790 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
4791 o->op_flags |= OPpDONE_SVREF;
4794 return newUNOP(OP_RV2SV, 0, scalar(o));
4797 /* Check routines. */
4800 Perl_ck_anoncode(pTHX_ OP *o)
4805 name = NEWSV(1106,0);
4806 sv_upgrade(name, SVt_PVNV);
4807 sv_setpvn(name, "&", 1);
4810 ix = pad_alloc(o->op_type, SVs_PADMY);
4811 av_store(PL_comppad_name, ix, name);
4812 av_store(PL_comppad, ix, cSVOPo->op_sv);
4813 SvPADMY_on(cSVOPo->op_sv);
4814 cSVOPo->op_sv = Nullsv;
4815 cSVOPo->op_targ = ix;
4820 Perl_ck_bitop(pTHX_ OP *o)
4822 o->op_private = PL_hints;
4827 Perl_ck_concat(pTHX_ OP *o)
4829 if (cUNOPo->op_first->op_type == OP_CONCAT)
4830 o->op_flags |= OPf_STACKED;
4835 Perl_ck_spair(pTHX_ OP *o)
4837 if (o->op_flags & OPf_KIDS) {
4840 OPCODE type = o->op_type;
4841 o = modkids(ck_fun(o), type);
4842 kid = cUNOPo->op_first;
4843 newop = kUNOP->op_first->op_sibling;
4845 (newop->op_sibling ||
4846 !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
4847 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
4848 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
4852 op_free(kUNOP->op_first);
4853 kUNOP->op_first = newop;
4855 o->op_ppaddr = PL_ppaddr[++o->op_type];
4860 Perl_ck_delete(pTHX_ OP *o)
4864 if (o->op_flags & OPf_KIDS) {
4865 OP *kid = cUNOPo->op_first;
4866 if (kid->op_type == OP_HSLICE)
4867 o->op_private |= OPpSLICE;
4868 else if (kid->op_type != OP_HELEM)
4869 Perl_croak(aTHX_ "%s argument is not a HASH element or slice",
4870 PL_op_desc[o->op_type]);
4877 Perl_ck_eof(pTHX_ OP *o)
4879 I32 type = o->op_type;
4881 if (o->op_flags & OPf_KIDS) {
4882 if (cLISTOPo->op_first->op_type == OP_STUB) {
4884 o = newUNOP(type, OPf_SPECIAL,
4885 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
4893 Perl_ck_eval(pTHX_ OP *o)
4895 PL_hints |= HINT_BLOCK_SCOPE;
4896 if (o->op_flags & OPf_KIDS) {
4897 SVOP *kid = (SVOP*)cUNOPo->op_first;
4900 o->op_flags &= ~OPf_KIDS;
4903 else if (kid->op_type == OP_LINESEQ) {
4906 kid->op_next = o->op_next;
4907 cUNOPo->op_first = 0;
4910 NewOp(1101, enter, 1, LOGOP);
4911 enter->op_type = OP_ENTERTRY;
4912 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
4913 enter->op_private = 0;
4915 /* establish postfix order */
4916 enter->op_next = (OP*)enter;
4918 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
4919 o->op_type = OP_LEAVETRY;
4920 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
4921 enter->op_other = o;
4929 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
4931 o->op_targ = (PADOFFSET)PL_hints;
4936 Perl_ck_exec(pTHX_ OP *o)
4939 if (o->op_flags & OPf_STACKED) {
4941 kid = cUNOPo->op_first->op_sibling;
4942 if (kid->op_type == OP_RV2GV)
4951 Perl_ck_exists(pTHX_ OP *o)
4954 if (o->op_flags & OPf_KIDS) {
4955 OP *kid = cUNOPo->op_first;
4956 if (kid->op_type != OP_HELEM)
4957 Perl_croak(aTHX_ "%s argument is not a HASH element", PL_op_desc[o->op_type]);
4965 Perl_ck_gvconst(pTHX_ register OP *o)
4967 o = fold_constants(o);
4968 if (o->op_type == OP_CONST)
4975 Perl_ck_rvconst(pTHX_ register OP *o)
4978 SVOP *kid = (SVOP*)cUNOPo->op_first;
4980 o->op_private |= (PL_hints & HINT_STRICT_REFS);
4981 if (kid->op_type == OP_CONST) {
4985 SV *kidsv = kid->op_sv;
4988 /* Is it a constant from cv_const_sv()? */
4989 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
4990 SV *rsv = SvRV(kidsv);
4991 int svtype = SvTYPE(rsv);
4992 char *badtype = Nullch;
4994 switch (o->op_type) {
4996 if (svtype > SVt_PVMG)
4997 badtype = "a SCALAR";
5000 if (svtype != SVt_PVAV)
5001 badtype = "an ARRAY";
5004 if (svtype != SVt_PVHV) {
5005 if (svtype == SVt_PVAV) { /* pseudohash? */
5006 SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
5007 if (ksv && SvROK(*ksv)
5008 && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
5017 if (svtype != SVt_PVCV)
5022 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
5025 name = SvPV(kidsv, n_a);
5026 if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
5027 char *badthing = Nullch;
5028 switch (o->op_type) {
5030 badthing = "a SCALAR";
5033 badthing = "an ARRAY";
5036 badthing = "a HASH";
5041 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
5045 * This is a little tricky. We only want to add the symbol if we
5046 * didn't add it in the lexer. Otherwise we get duplicate strict
5047 * warnings. But if we didn't add it in the lexer, we must at
5048 * least pretend like we wanted to add it even if it existed before,
5049 * or we get possible typo warnings. OPpCONST_ENTERED says
5050 * whether the lexer already added THIS instance of this symbol.
5052 iscv = (o->op_type == OP_RV2CV) * 2;
5054 gv = gv_fetchpv(name,
5055 iscv | !(kid->op_private & OPpCONST_ENTERED),
5058 : o->op_type == OP_RV2SV
5060 : o->op_type == OP_RV2AV
5062 : o->op_type == OP_RV2HV
5065 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
5067 kid->op_type = OP_GV;
5068 SvREFCNT_dec(kid->op_sv);
5070 /* XXXXXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
5071 kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
5073 PL_curpad[kPADOP->op_padix] = SvREFCNT_inc(gv);
5075 kid->op_sv = SvREFCNT_inc(gv);
5077 kid->op_ppaddr = PL_ppaddr[OP_GV];
5084 Perl_ck_ftst(pTHX_ OP *o)
5087 I32 type = o->op_type;
5089 if (o->op_flags & OPf_REF)
5092 if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
5093 SVOP *kid = (SVOP*)cUNOPo->op_first;
5095 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5097 OP *newop = newGVOP(type, OPf_REF,
5098 gv_fetchpv(SvPVx(kid->op_sv, n_a), TRUE, SVt_PVIO));
5105 if (type == OP_FTTTY)
5106 return newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
5109 return newUNOP(type, 0, newDEFSVOP());
5115 Perl_ck_fun(pTHX_ OP *o)
5122 int type = o->op_type;
5123 register I32 oa = PL_opargs[type] >> OASHIFT;
5125 if (o->op_flags & OPf_STACKED) {
5126 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
5129 return no_fh_allowed(o);
5132 if (o->op_flags & OPf_KIDS) {
5134 tokid = &cLISTOPo->op_first;
5135 kid = cLISTOPo->op_first;
5136 if (kid->op_type == OP_PUSHMARK ||
5137 kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
5139 tokid = &kid->op_sibling;
5140 kid = kid->op_sibling;
5142 if (!kid && PL_opargs[type] & OA_DEFGV)
5143 *tokid = kid = newDEFSVOP();
5147 sibl = kid->op_sibling;
5150 /* list seen where single (scalar) arg expected? */
5151 if (numargs == 1 && !(oa >> 4)
5152 && kid->op_type == OP_LIST && type != OP_SCALAR)
5154 return too_many_arguments(o,PL_op_desc[type]);
5167 if (kid->op_type == OP_CONST &&
5168 (kid->op_private & OPpCONST_BARE))
5170 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5171 OP *newop = newAVREF(newGVOP(OP_GV, 0,
5172 gv_fetchpv(name, TRUE, SVt_PVAV) ));
5173 if (ckWARN(WARN_SYNTAX))
5174 Perl_warner(aTHX_ WARN_SYNTAX,
5175 "Array @%s missing the @ in argument %"IVdf" of %s()",
5176 name, (IV)numargs, PL_op_desc[type]);
5179 kid->op_sibling = sibl;
5182 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
5183 bad_type(numargs, "array", PL_op_desc[type], kid);
5187 if (kid->op_type == OP_CONST &&
5188 (kid->op_private & OPpCONST_BARE))
5190 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5191 OP *newop = newHVREF(newGVOP(OP_GV, 0,
5192 gv_fetchpv(name, TRUE, SVt_PVHV) ));
5193 if (ckWARN(WARN_SYNTAX))
5194 Perl_warner(aTHX_ WARN_SYNTAX,
5195 "Hash %%%s missing the %% in argument %"IVdf" of %s()",
5196 name, (IV)numargs, PL_op_desc[type]);
5199 kid->op_sibling = sibl;
5202 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
5203 bad_type(numargs, "hash", PL_op_desc[type], kid);
5208 OP *newop = newUNOP(OP_NULL, 0, kid);
5209 kid->op_sibling = 0;
5211 newop->op_next = newop;
5213 kid->op_sibling = sibl;
5218 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
5219 if (kid->op_type == OP_CONST &&
5220 (kid->op_private & OPpCONST_BARE))
5222 OP *newop = newGVOP(OP_GV, 0,
5223 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, n_a), TRUE,
5228 else if (kid->op_type == OP_READLINE) {
5229 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
5230 bad_type(numargs, "HANDLE", PL_op_desc[o->op_type], kid);
5233 I32 flags = OPf_SPECIAL;
5235 /* is this op a FH constructor? */
5236 if (is_handle_constructor(o,numargs)) {
5238 /* Set a flag to tell rv2gv to vivify
5239 * need to "prove" flag does not mean something
5240 * else already - NI-S 1999/05/07
5244 /* Helps with open($array[$n],...)
5245 but is too simplistic - need to do selectively
5250 kid->op_sibling = 0;
5251 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
5253 kid->op_private |= priv;
5256 kid->op_sibling = sibl;
5262 mod(scalar(kid), type);
5266 tokid = &kid->op_sibling;
5267 kid = kid->op_sibling;
5269 o->op_private |= numargs;
5271 return too_many_arguments(o,PL_op_desc[o->op_type]);
5274 else if (PL_opargs[type] & OA_DEFGV) {
5276 return newUNOP(type, 0, newDEFSVOP());
5280 while (oa & OA_OPTIONAL)
5282 if (oa && oa != OA_LIST)
5283 return too_few_arguments(o,PL_op_desc[o->op_type]);
5289 Perl_ck_glob(pTHX_ OP *o)
5293 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
5294 append_elem(OP_GLOB, o, newDEFSVOP());
5296 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
5297 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5299 #if defined(PERL_INTERNAL_GLOB) && !defined(MINIPERL_BUILD)
5300 /* XXX this can be tightened up and made more failsafe. */
5302 OP *modname = newSVOP(OP_CONST, 0, newSVpvn("File::Glob", 10));
5303 modname->op_private |= OPpCONST_BARE;
5305 utilize(1, start_subparse(FALSE, 0), Nullop, modname,
5306 newSVOP(OP_CONST, 0, newSVpvn("globally", 8)));
5307 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5310 #endif /* PERL_INTERNAL_GLOB && !MINIPERL_BUILD */
5312 if (gv && GvIMPORTED_CV(gv)) {
5313 append_elem(OP_GLOB, o,
5314 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
5315 o->op_type = OP_LIST;
5316 o->op_ppaddr = PL_ppaddr[OP_LIST];
5317 cLISTOPo->op_first->op_type = OP_PUSHMARK;
5318 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
5319 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
5320 append_elem(OP_LIST, o,
5321 scalar(newUNOP(OP_RV2CV, 0,
5322 newGVOP(OP_GV, 0, gv)))));
5323 o = newUNOP(OP_NULL, 0, ck_subr(o));
5324 o->op_targ = OP_GLOB; /* hint at what it used to be */
5327 gv = newGVgen("main");
5329 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
5335 Perl_ck_grep(pTHX_ OP *o)
5339 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
5341 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
5342 NewOp(1101, gwop, 1, LOGOP);
5344 if (o->op_flags & OPf_STACKED) {
5347 kid = cLISTOPo->op_first->op_sibling;
5348 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
5351 kid->op_next = (OP*)gwop;
5352 o->op_flags &= ~OPf_STACKED;
5354 kid = cLISTOPo->op_first->op_sibling;
5355 if (type == OP_MAPWHILE)
5362 kid = cLISTOPo->op_first->op_sibling;
5363 if (kid->op_type != OP_NULL)
5364 Perl_croak(aTHX_ "panic: ck_grep");
5365 kid = kUNOP->op_first;
5367 gwop->op_type = type;
5368 gwop->op_ppaddr = PL_ppaddr[type];
5369 gwop->op_first = listkids(o);
5370 gwop->op_flags |= OPf_KIDS;
5371 gwop->op_private = 1;
5372 gwop->op_other = LINKLIST(kid);
5373 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
5374 kid->op_next = (OP*)gwop;
5376 kid = cLISTOPo->op_first->op_sibling;
5377 if (!kid || !kid->op_sibling)
5378 return too_few_arguments(o,PL_op_desc[o->op_type]);
5379 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
5380 mod(kid, OP_GREPSTART);
5386 Perl_ck_index(pTHX_ OP *o)
5388 if (o->op_flags & OPf_KIDS) {
5389 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5391 kid = kid->op_sibling; /* get past "big" */
5392 if (kid && kid->op_type == OP_CONST)
5393 fbm_compile(((SVOP*)kid)->op_sv, 0);
5399 Perl_ck_lengthconst(pTHX_ OP *o)
5401 /* XXX length optimization goes here */
5406 Perl_ck_lfun(pTHX_ OP *o)
5408 OPCODE type = o->op_type;
5409 return modkids(ck_fun(o), type);
5413 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
5416 if ((o->op_flags & OPf_KIDS) && ckWARN(WARN_DEPRECATED)) {
5417 switch (cUNOPo->op_first->op_type) {
5419 break; /* Globals via GV can be undef */
5421 case OP_AASSIGN: /* Is this a good idea? */
5422 Perl_warner(aTHX_ WARN_DEPRECATED,
5423 "defined(@array) is deprecated");
5424 Perl_warner(aTHX_ WARN_DEPRECATED,
5425 "(Maybe you should just omit the defined()?)\n");
5428 break; /* Globals via GV can be undef */
5430 Perl_warner(aTHX_ WARN_DEPRECATED,
5431 "defined(%hash) is deprecated");
5432 Perl_warner(aTHX_ WARN_DEPRECATED,
5433 "(Maybe you should just omit the defined()?)\n");
5444 Perl_ck_rfun(pTHX_ OP *o)
5446 OPCODE type = o->op_type;
5447 return refkids(ck_fun(o), type);
5451 Perl_ck_listiob(pTHX_ OP *o)
5455 kid = cLISTOPo->op_first;
5458 kid = cLISTOPo->op_first;
5460 if (kid->op_type == OP_PUSHMARK)
5461 kid = kid->op_sibling;
5462 if (kid && o->op_flags & OPf_STACKED)
5463 kid = kid->op_sibling;
5464 else if (kid && !kid->op_sibling) { /* print HANDLE; */
5465 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
5466 o->op_flags |= OPf_STACKED; /* make it a filehandle */
5467 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
5468 cLISTOPo->op_first->op_sibling = kid;
5469 cLISTOPo->op_last = kid;
5470 kid = kid->op_sibling;
5475 append_elem(o->op_type, o, newDEFSVOP());
5481 if (PL_hints & HINT_LOCALE)
5482 o->op_private |= OPpLOCALE;
5489 Perl_ck_fun_locale(pTHX_ OP *o)
5495 if (PL_hints & HINT_LOCALE)
5496 o->op_private |= OPpLOCALE;
5503 Perl_ck_sassign(pTHX_ OP *o)
5505 OP *kid = cLISTOPo->op_first;
5506 /* has a disposable target? */
5507 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
5508 && !(kid->op_flags & OPf_STACKED))
5510 OP *kkid = kid->op_sibling;
5512 /* Can just relocate the target. */
5513 if (kkid && kkid->op_type == OP_PADSV
5514 && !(kkid->op_private & OPpLVAL_INTRO))
5516 /* Concat has problems if target is equal to right arg. */
5517 if (kid->op_type == OP_CONCAT) {
5518 if (kLISTOP->op_first->op_sibling->op_type == OP_PADSV
5519 && kLISTOP->op_first->op_sibling->op_targ == kkid->op_targ)
5522 else if (kid->op_type == OP_JOIN) {
5523 /* do_join has problems if the arguments coincide with target.
5524 In fact the second argument *can* safely coincide,
5525 but ignore=pessimize this rare occasion. */
5526 OP *arg = kLISTOP->op_first->op_sibling; /* Skip PUSHMARK */
5529 if (arg->op_type == OP_PADSV
5530 && arg->op_targ == kkid->op_targ)
5532 arg = arg->op_sibling;
5535 else if (kid->op_type == OP_QUOTEMETA) {
5536 /* quotemeta has problems if the argument coincides with target. */
5537 if (kLISTOP->op_first->op_type == OP_PADSV
5538 && kLISTOP->op_first->op_targ == kkid->op_targ)
5541 kid->op_targ = kkid->op_targ;
5543 /* Now we do not need PADSV and SASSIGN. */
5544 kid->op_sibling = o->op_sibling; /* NULL */
5545 cLISTOPo->op_first = NULL;
5548 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
5556 Perl_ck_scmp(pTHX_ OP *o)
5560 if (PL_hints & HINT_LOCALE)
5561 o->op_private |= OPpLOCALE;
5568 Perl_ck_match(pTHX_ OP *o)
5570 o->op_private |= OPpRUNTIME;
5575 Perl_ck_method(pTHX_ OP *o)
5577 OP *kid = cUNOPo->op_first;
5578 if (kid->op_type == OP_CONST) {
5579 SV* sv = kSVOP->op_sv;
5580 if (!(strchr(SvPVX(sv), ':') || strchr(SvPVX(sv), '\''))) {
5582 sv_upgrade(sv, SVt_PVIV);
5584 PERL_HASH(SvUVX(sv), SvPVX(sv), SvCUR(sv));
5585 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
5586 kSVOP->op_sv = Nullsv;
5595 Perl_ck_null(pTHX_ OP *o)
5601 Perl_ck_repeat(pTHX_ OP *o)
5603 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
5604 o->op_private |= OPpREPEAT_DOLIST;
5605 cBINOPo->op_first = force_list(cBINOPo->op_first);
5613 Perl_ck_require(pTHX_ OP *o)
5615 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
5616 SVOP *kid = (SVOP*)cUNOPo->op_first;
5618 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5620 for (s = SvPVX(kid->op_sv); *s; s++) {
5621 if (*s == ':' && s[1] == ':') {
5623 Move(s+2, s+1, strlen(s+2)+1, char);
5624 --SvCUR(kid->op_sv);
5627 sv_catpvn(kid->op_sv, ".pm", 3);
5635 Perl_ck_retarget(pTHX_ OP *o)
5637 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
5644 Perl_ck_select(pTHX_ OP *o)
5647 if (o->op_flags & OPf_KIDS) {
5648 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5649 if (kid && kid->op_sibling) {
5650 o->op_type = OP_SSELECT;
5651 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
5653 return fold_constants(o);
5657 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5658 if (kid && kid->op_type == OP_RV2GV)
5659 kid->op_private &= ~HINT_STRICT_REFS;
5664 Perl_ck_shift(pTHX_ OP *o)
5666 I32 type = o->op_type;
5668 if (!(o->op_flags & OPf_KIDS)) {
5673 if (!CvUNIQUE(PL_compcv)) {
5674 argop = newOP(OP_PADAV, OPf_REF);
5675 argop->op_targ = 0; /* PL_curpad[0] is @_ */
5678 argop = newUNOP(OP_RV2AV, 0,
5679 scalar(newGVOP(OP_GV, 0,
5680 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5683 argop = newUNOP(OP_RV2AV, 0,
5684 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(PL_compcv) ?
5685 PL_defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5686 #endif /* USE_THREADS */
5687 return newUNOP(type, 0, scalar(argop));
5689 return scalar(modkids(ck_fun(o), type));
5693 Perl_ck_sort(pTHX_ OP *o)
5697 if (PL_hints & HINT_LOCALE)
5698 o->op_private |= OPpLOCALE;
5701 if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
5703 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
5704 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5706 kid = kUNOP->op_first; /* get past null */
5708 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
5710 if (kid->op_type == OP_SCOPE) {
5714 else if (kid->op_type == OP_LEAVE) {
5715 if (o->op_type == OP_SORT) {
5716 null(kid); /* wipe out leave */
5719 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
5720 if (k->op_next == kid)
5725 kid->op_next = 0; /* just disconnect the leave */
5726 k = kLISTOP->op_first;
5730 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5731 if (o->op_type == OP_SORT)
5735 o->op_flags |= OPf_SPECIAL;
5737 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
5738 null(cLISTOPo->op_first->op_sibling);
5745 S_simplify_sort(pTHX_ OP *o)
5748 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5752 if (!(o->op_flags & OPf_STACKED))
5754 GvMULTI_on(gv_fetchpv("a", TRUE, SVt_PV));
5755 GvMULTI_on(gv_fetchpv("b", TRUE, SVt_PV));
5756 kid = kUNOP->op_first; /* get past null */
5757 if (kid->op_type != OP_SCOPE)
5759 kid = kLISTOP->op_last; /* get past scope */
5760 switch(kid->op_type) {
5768 k = kid; /* remember this node*/
5769 if (kBINOP->op_first->op_type != OP_RV2SV)
5771 kid = kBINOP->op_first; /* get past cmp */
5772 if (kUNOP->op_first->op_type != OP_GV)
5774 kid = kUNOP->op_first; /* get past rv2sv */
5776 if (GvSTASH(gv) != PL_curstash)
5778 if (strEQ(GvNAME(gv), "a"))
5780 else if(strEQ(GvNAME(gv), "b"))
5784 kid = k; /* back to cmp */
5785 if (kBINOP->op_last->op_type != OP_RV2SV)
5787 kid = kBINOP->op_last; /* down to 2nd arg */
5788 if (kUNOP->op_first->op_type != OP_GV)
5790 kid = kUNOP->op_first; /* get past rv2sv */
5792 if (GvSTASH(gv) != PL_curstash
5794 ? strNE(GvNAME(gv), "a")
5795 : strNE(GvNAME(gv), "b")))
5797 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
5799 o->op_private |= OPpSORT_REVERSE;
5800 if (k->op_type == OP_NCMP)
5801 o->op_private |= OPpSORT_NUMERIC;
5802 if (k->op_type == OP_I_NCMP)
5803 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
5804 kid = cLISTOPo->op_first->op_sibling;
5805 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
5806 op_free(kid); /* then delete it */
5807 cLISTOPo->op_children--;
5811 Perl_ck_split(pTHX_ OP *o)
5815 if (o->op_flags & OPf_STACKED)
5816 return no_fh_allowed(o);
5818 kid = cLISTOPo->op_first;
5819 if (kid->op_type != OP_NULL)
5820 Perl_croak(aTHX_ "panic: ck_split");
5821 kid = kid->op_sibling;
5822 op_free(cLISTOPo->op_first);
5823 cLISTOPo->op_first = kid;
5825 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvn(" ", 1));
5826 cLISTOPo->op_last = kid; /* There was only one element previously */
5829 if (kid->op_type != OP_MATCH) {
5830 OP *sibl = kid->op_sibling;
5831 kid->op_sibling = 0;
5832 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
5833 if (cLISTOPo->op_first == cLISTOPo->op_last)
5834 cLISTOPo->op_last = kid;
5835 cLISTOPo->op_first = kid;
5836 kid->op_sibling = sibl;
5839 kid->op_type = OP_PUSHRE;
5840 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
5843 if (!kid->op_sibling)
5844 append_elem(OP_SPLIT, o, newDEFSVOP());
5846 kid = kid->op_sibling;
5849 if (!kid->op_sibling)
5850 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
5852 kid = kid->op_sibling;
5855 if (kid->op_sibling)
5856 return too_many_arguments(o,PL_op_desc[o->op_type]);
5862 Perl_ck_join(pTHX_ OP *o)
5864 if (ckWARN(WARN_SYNTAX)) {
5865 OP *kid = cLISTOPo->op_first->op_sibling;
5866 if (kid && kid->op_type == OP_MATCH) {
5867 char *pmstr = "STRING";
5868 if (kPMOP->op_pmregexp)
5869 pmstr = kPMOP->op_pmregexp->precomp;
5870 Perl_warner(aTHX_ WARN_SYNTAX,
5871 "/%s/ should probably be written as \"%s\"",
5879 Perl_ck_subr(pTHX_ OP *o)
5882 OP *prev = ((cUNOPo->op_first->op_sibling)
5883 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
5884 OP *o2 = prev->op_sibling;
5893 o->op_private |= OPpENTERSUB_HASTARG;
5894 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
5895 if (cvop->op_type == OP_RV2CV) {
5897 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
5898 null(cvop); /* disable rv2cv */
5899 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
5900 if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) {
5901 GV *gv = cGVOPx(tmpop);
5904 tmpop->op_private |= OPpEARLY_CV;
5905 else if (SvPOK(cv)) {
5906 namegv = CvANON(cv) ? gv : CvGV(cv);
5907 proto = SvPV((SV*)cv, n_a);
5911 else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
5912 if (o2->op_type == OP_CONST)
5913 o2->op_private &= ~OPpCONST_STRICT;
5914 else if (o2->op_type == OP_LIST) {
5915 OP *o = ((UNOP*)o2)->op_first->op_sibling;
5916 if (o && o->op_type == OP_CONST)
5917 o->op_private &= ~OPpCONST_STRICT;
5920 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5921 if (PERLDB_SUB && PL_curstash != PL_debstash)
5922 o->op_private |= OPpENTERSUB_DB;
5923 while (o2 != cvop) {
5927 return too_many_arguments(o, gv_ename(namegv));
5945 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
5946 bad_type(arg, "block", gv_ename(namegv), o2);
5949 /* '*' allows any scalar type, including bareword */
5952 if (o2->op_type == OP_RV2GV)
5953 goto wrapref; /* autoconvert GLOB -> GLOBref */
5954 else if (o2->op_type == OP_CONST)
5955 o2->op_private &= ~OPpCONST_STRICT;
5956 else if (o2->op_type == OP_ENTERSUB) {
5957 /* accidental subroutine, revert to bareword */
5958 OP *gvop = ((UNOP*)o2)->op_first;
5959 if (gvop && gvop->op_type == OP_NULL) {
5960 gvop = ((UNOP*)gvop)->op_first;
5962 for (; gvop->op_sibling; gvop = gvop->op_sibling)
5965 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
5966 (gvop = ((UNOP*)gvop)->op_first) &&
5967 gvop->op_type == OP_GV)
5969 GV *gv = cGVOPx(gvop);
5970 OP *sibling = o2->op_sibling;
5971 SV *n = newSVpvn("",0);
5973 gv_fullname3(n, gv, "");
5974 if (SvCUR(n)>6 && strnEQ(SvPVX(n),"main::",6))
5975 sv_chop(n, SvPVX(n)+6);
5976 o2 = newSVOP(OP_CONST, 0, n);
5977 prev->op_sibling = o2;
5978 o2->op_sibling = sibling;
5990 if (o2->op_type != OP_RV2GV)
5991 bad_type(arg, "symbol", gv_ename(namegv), o2);
5994 if (o2->op_type != OP_RV2CV)
5995 bad_type(arg, "sub", gv_ename(namegv), o2);
5998 if (o2->op_type != OP_RV2SV
5999 && o2->op_type != OP_PADSV
6000 && o2->op_type != OP_HELEM
6001 && o2->op_type != OP_AELEM
6002 && o2->op_type != OP_THREADSV)
6004 bad_type(arg, "scalar", gv_ename(namegv), o2);
6008 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
6009 bad_type(arg, "array", gv_ename(namegv), o2);
6012 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
6013 bad_type(arg, "hash", gv_ename(namegv), o2);
6017 OP* sib = kid->op_sibling;
6018 kid->op_sibling = 0;
6019 o2 = newUNOP(OP_REFGEN, 0, kid);
6020 o2->op_sibling = sib;
6021 prev->op_sibling = o2;
6032 Perl_croak(aTHX_ "Malformed prototype for %s: %s",
6033 gv_ename(namegv), SvPV((SV*)cv, n_a));
6038 mod(o2, OP_ENTERSUB);
6040 o2 = o2->op_sibling;
6042 if (proto && !optional &&
6043 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
6044 return too_few_arguments(o, gv_ename(namegv));
6049 Perl_ck_svconst(pTHX_ OP *o)
6051 SvREADONLY_on(cSVOPo->op_sv);
6056 Perl_ck_trunc(pTHX_ OP *o)
6058 if (o->op_flags & OPf_KIDS) {
6059 SVOP *kid = (SVOP*)cUNOPo->op_first;
6061 if (kid->op_type == OP_NULL)
6062 kid = (SVOP*)kid->op_sibling;
6063 if (kid && kid->op_type == OP_CONST &&
6064 (kid->op_private & OPpCONST_BARE))
6066 o->op_flags |= OPf_SPECIAL;
6067 kid->op_private &= ~OPpCONST_STRICT;
6073 /* A peephole optimizer. We visit the ops in the order they're to execute. */
6076 Perl_peep(pTHX_ register OP *o)
6079 register OP* oldop = 0;
6081 OP *last_composite = Nullop;
6083 if (!o || o->op_seq)
6087 SAVESPTR(PL_curcop);
6088 for (; o; o = o->op_next) {
6094 switch (o->op_type) {
6098 PL_curcop = ((COP*)o); /* for warnings */
6099 o->op_seq = PL_op_seqmax++;
6100 last_composite = Nullop;
6104 if (cSVOPo->op_private & OPpCONST_STRICT)
6105 no_bareword_allowed(o);
6111 if ( o->op_next && o->op_next->op_type == OP_STRINGIFY
6112 && !(o->op_next->op_private & OPpTARGET_MY) )
6114 o->op_seq = PL_op_seqmax++;
6119 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
6120 if (o->op_next->op_private & OPpTARGET_MY) {
6121 if ((o->op_flags & OPf_STACKED) /* chained concats */
6122 || (o->op_type == OP_CONCAT
6123 /* Concat has problems if target is equal to right arg. */
6124 && (((LISTOP*)o)->op_first->op_sibling->op_type
6126 && (((LISTOP*)o)->op_first->op_sibling->op_targ
6127 == o->op_next->op_targ)))
6129 goto ignore_optimization;
6132 o->op_targ = o->op_next->op_targ;
6133 o->op_next->op_targ = 0;
6134 o->op_private |= OPpTARGET_MY;
6139 ignore_optimization:
6140 o->op_seq = PL_op_seqmax++;
6143 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
6144 o->op_seq = PL_op_seqmax++;
6145 break; /* Scalar stub must produce undef. List stub is noop */
6149 if (o->op_targ == OP_NEXTSTATE
6150 || o->op_targ == OP_DBSTATE
6151 || o->op_targ == OP_SETSTATE)
6153 PL_curcop = ((COP*)o);
6160 if (oldop && o->op_next) {
6161 oldop->op_next = o->op_next;
6164 o->op_seq = PL_op_seqmax++;
6168 if (o->op_next->op_type == OP_RV2SV) {
6169 if (!(o->op_next->op_private & OPpDEREF)) {
6171 o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
6172 o->op_next = o->op_next->op_next;
6173 o->op_type = OP_GVSV;
6174 o->op_ppaddr = PL_ppaddr[OP_GVSV];
6177 else if (o->op_next->op_type == OP_RV2AV) {
6178 OP* pop = o->op_next->op_next;
6180 if (pop->op_type == OP_CONST &&
6181 (PL_op = pop->op_next) &&
6182 pop->op_next->op_type == OP_AELEM &&
6183 !(pop->op_next->op_private &
6184 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
6185 (i = SvIV(((SVOP*)pop)->op_sv) - PL_compiling.cop_arybase)
6193 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
6194 o->op_next = pop->op_next->op_next;
6195 o->op_type = OP_AELEMFAST;
6196 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
6197 o->op_private = (U8)i;
6202 else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_UNSAFE)) {
6204 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX(GvCV(gv))) {
6205 /* XXX could check prototype here instead of just carping */
6206 SV *sv = sv_newmortal();
6207 gv_efullname3(sv, gv, Nullch);
6208 Perl_warner(aTHX_ WARN_UNSAFE,
6209 "%s() called too early to check prototype",
6214 o->op_seq = PL_op_seqmax++;
6225 o->op_seq = PL_op_seqmax++;
6226 while (cLOGOP->op_other->op_type == OP_NULL)
6227 cLOGOP->op_other = cLOGOP->op_other->op_next;
6228 peep(cLOGOP->op_other);
6232 o->op_seq = PL_op_seqmax++;
6233 peep(cLOOP->op_redoop);
6234 peep(cLOOP->op_nextop);
6235 peep(cLOOP->op_lastop);
6241 o->op_seq = PL_op_seqmax++;
6242 peep(cPMOP->op_pmreplstart);
6246 o->op_seq = PL_op_seqmax++;
6247 if (ckWARN(WARN_SYNTAX) && o->op_next
6248 && o->op_next->op_type == OP_NEXTSTATE) {
6249 if (o->op_next->op_sibling &&
6250 o->op_next->op_sibling->op_type != OP_EXIT &&
6251 o->op_next->op_sibling->op_type != OP_WARN &&
6252 o->op_next->op_sibling->op_type != OP_DIE) {
6253 line_t oldline = CopLINE(PL_curcop);
6255 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_next));
6256 Perl_warner(aTHX_ WARN_SYNTAX, "Statement unlikely to be reached");
6257 Perl_warner(aTHX_ WARN_SYNTAX, "(Maybe you meant system() when you said exec()?)\n");
6258 CopLINE_set(PL_curcop, oldline);
6272 if ((o->op_private & (OPpLVAL_INTRO))
6273 || ((BINOP*)o)->op_last->op_type != OP_CONST)
6275 rop = (UNOP*)((BINOP*)o)->op_first;
6276 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6278 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6279 if (!SvOBJECT(lexname))
6281 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6282 if (!fields || !GvHV(*fields))
6284 svp = &((SVOP*)((BINOP*)o)->op_last)->op_sv;
6285 key = SvPV(*svp, keylen);
6286 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
6288 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" in variable %s of type %s",
6289 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
6291 ind = SvIV(*indsvp);
6293 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6294 rop->op_type = OP_RV2AV;
6295 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6296 o->op_type = OP_AELEM;
6297 o->op_ppaddr = PL_ppaddr[OP_AELEM];
6299 *svp = newSViv(ind);
6305 if (!(o->op_flags & OPf_WANT)
6306 || (o->op_flags & OPf_WANT) == OPf_WANT_LIST)
6310 o->op_seq = PL_op_seqmax++;
6314 if (o->op_next && o->op_next->op_type != OP_LEAVESUBLV) {
6315 o->op_seq = PL_op_seqmax++;
6321 if (last_composite) {
6322 OP *r = last_composite;
6324 while (r->op_sibling)
6327 || (r->op_next->op_type == OP_LIST
6328 && r->op_next->op_next == o))
6330 if (last_composite->op_type == OP_RV2AV)
6331 yyerror("Lvalue subs returning arrays not implemented yet");
6333 yyerror("Lvalue subs returning hashes not implemented yet");
6340 o->op_seq = PL_op_seqmax++;