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 */
25 #ifdef PL_OP_SLAB_ALLOC
26 #define SLAB_SIZE 8192
27 static char *PL_OpPtr = NULL;
28 static int PL_OpSpace = 0;
29 #define NewOp(m,var,c,type) do { if ((PL_OpSpace -= c*sizeof(type)) >= 0) \
30 var = (type *)(PL_OpPtr -= c*sizeof(type)); \
32 var = (type *) Slab_Alloc(m,c*sizeof(type)); \
36 S_Slab_Alloc(pTHX_ int m, size_t sz)
38 Newz(m,PL_OpPtr,SLAB_SIZE,char);
39 PL_OpSpace = SLAB_SIZE - sz;
40 return PL_OpPtr += PL_OpSpace;
44 #define NewOp(m, var, c, type) Newz(m, var, c, type)
47 * In the following definition, the ", Nullop" is just to make the compiler
48 * think the expression is of the right type: croak actually does a Siglongjmp.
50 #define CHECKOP(type,o) \
51 ((PL_op_mask && PL_op_mask[type]) \
52 ? ( op_free((OP*)o), \
53 Perl_croak(aTHX_ "%s trapped by operation mask", PL_op_desc[type]), \
55 : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o))
57 #define PAD_MAX 999999999
60 S_gv_ename(pTHX_ GV *gv)
63 SV* tmpsv = sv_newmortal();
64 gv_efullname3(tmpsv, gv, Nullch);
65 return SvPV(tmpsv,n_a);
69 S_no_fh_allowed(pTHX_ OP *o)
71 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
72 PL_op_desc[o->op_type]));
77 S_too_few_arguments(pTHX_ OP *o, char *name)
79 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
84 S_too_many_arguments(pTHX_ OP *o, char *name)
86 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
91 S_bad_type(pTHX_ I32 n, char *t, char *name, OP *kid)
93 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
94 (int)n, name, t, PL_op_desc[kid->op_type]));
98 S_no_bareword_allowed(pTHX_ OP *o)
100 qerror(Perl_mess(aTHX_
101 "Bareword \"%s\" not allowed while \"strict subs\" in use",
102 SvPV_nolen(cSVOPo->op_sv)));
105 /* "register" allocation */
108 Perl_pad_allocmy(pTHX_ char *name)
115 PL_in_my == KEY_our ||
117 (PL_hints & HINT_UTF8 && (name[1] & 0xc0) == 0xc0) ||
118 name[1] == '_' && (int)strlen(name) > 2 ))
120 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
121 /* 1999-02-27 mjd@plover.com */
123 p = strchr(name, '\0');
124 /* The next block assumes the buffer is at least 205 chars
125 long. At present, it's always at least 256 chars. */
127 strcpy(name+200, "...");
133 /* Move everything else down one character */
134 for (; p-name > 2; p--)
136 name[2] = toCTRL(name[1]);
139 yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
141 if (ckWARN(WARN_UNSAFE) && AvFILLp(PL_comppad_name) >= 0) {
142 SV **svp = AvARRAY(PL_comppad_name);
143 for (off = AvFILLp(PL_comppad_name); off > PL_comppad_name_floor; off--) {
145 && sv != &PL_sv_undef
146 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
147 && strEQ(name, SvPVX(sv)))
149 Perl_warner(aTHX_ WARN_UNSAFE,
150 "\"%s\" variable %s masks earlier declaration in same %s",
151 (PL_in_my == KEY_our ? "our" : "my"),
153 (SvIVX(sv) == PAD_MAX ? "scope" : "statement"));
158 off = pad_alloc(OP_PADSV, SVs_PADMY);
160 sv_upgrade(sv, SVt_PVNV);
162 if (PL_in_my_stash) {
164 yyerror(Perl_form(aTHX_ "Can't declare class for non-scalar %s in \"my\"",
167 (void)SvUPGRADE(sv, SVt_PVMG);
168 SvSTASH(sv) = (HV*)SvREFCNT_inc(PL_in_my_stash);
171 if (PL_in_my == KEY_our)
172 SvFLAGS(sv) |= SVpad_OUR;
173 av_store(PL_comppad_name, off, sv);
174 SvNVX(sv) = (NV)PAD_MAX;
175 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
176 if (!PL_min_intro_pending)
177 PL_min_intro_pending = off;
178 PL_max_intro_pending = off;
180 av_store(PL_comppad, off, (SV*)newAV());
181 else if (*name == '%')
182 av_store(PL_comppad, off, (SV*)newHV());
183 SvPADMY_on(PL_curpad[off]);
187 #define FINDLEX_NOSEARCH 1 /* don't search outer contexts */
190 S_pad_findlex(pTHX_ char *name, PADOFFSET newoff, U32 seq, CV* startcv,
191 I32 cx_ix, I32 saweval, U32 flags)
198 register PERL_CONTEXT *cx;
200 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
201 AV *curlist = CvPADLIST(cv);
202 SV **svp = av_fetch(curlist, 0, FALSE);
205 if (!svp || *svp == &PL_sv_undef)
208 svp = AvARRAY(curname);
209 for (off = AvFILLp(curname); off > 0; off--) {
210 if ((sv = svp[off]) &&
211 sv != &PL_sv_undef &&
213 seq > I_32(SvNVX(sv)) &&
214 strEQ(SvPVX(sv), name))
225 return 0; /* don't clone from inactive stack frame */
229 oldpad = (AV*)*av_fetch(curlist, depth, FALSE);
230 oldsv = *av_fetch(oldpad, off, TRUE);
231 if (!newoff) { /* Not a mere clone operation. */
232 SV *namesv = NEWSV(1103,0);
233 newoff = pad_alloc(OP_PADSV, SVs_PADMY);
234 sv_upgrade(namesv, SVt_PVNV);
235 sv_setpv(namesv, name);
236 av_store(PL_comppad_name, newoff, namesv);
237 SvNVX(namesv) = (NV)PL_curcop->cop_seq;
238 SvIVX(namesv) = PAD_MAX; /* A ref, intro immediately */
239 SvFAKE_on(namesv); /* A ref, not a real var */
240 if (SvFLAGS(sv) & SVpad_OUR)/* An "our" variable */
241 SvFLAGS(namesv) |= SVpad_OUR;
242 if (SvOBJECT(sv)) { /* A typed var */
244 (void)SvUPGRADE(namesv, SVt_PVMG);
245 SvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)SvSTASH(sv));
248 if (CvANON(PL_compcv) || SvTYPE(PL_compcv) == SVt_PVFM) {
249 /* "It's closures all the way down." */
250 CvCLONE_on(PL_compcv);
252 if (CvANON(PL_compcv))
253 oldsv = Nullsv; /* no need to keep ref */
258 bcv && bcv != cv && !CvCLONE(bcv);
259 bcv = CvOUTSIDE(bcv))
264 if (ckWARN(WARN_CLOSURE)
265 && !CvUNIQUE(bcv) && !CvUNIQUE(cv))
267 Perl_warner(aTHX_ WARN_CLOSURE,
268 "Variable \"%s\" may be unavailable",
276 else if (!CvUNIQUE(PL_compcv)) {
277 if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv))
278 Perl_warner(aTHX_ WARN_CLOSURE,
279 "Variable \"%s\" will not stay shared", name);
282 av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv));
288 if (flags & FINDLEX_NOSEARCH)
291 /* Nothing in current lexical context--try eval's context, if any.
292 * This is necessary to let the perldb get at lexically scoped variables.
293 * XXX This will also probably interact badly with eval tree caching.
296 for (i = cx_ix; i >= 0; i--) {
298 switch (CxTYPE(cx)) {
300 if (i == 0 && saweval) {
301 seq = cxstack[saweval].blk_oldcop->cop_seq;
302 return pad_findlex(name, newoff, seq, PL_main_cv, -1, saweval, 0);
306 switch (cx->blk_eval.old_op_type) {
312 /* require must have its own scope */
320 if (PL_debstash && CvSTASH(cv) == PL_debstash) { /* ignore DB'* scope */
321 saweval = i; /* so we know where we were called from */
324 seq = cxstack[saweval].blk_oldcop->cop_seq;
325 return pad_findlex(name, newoff, seq, cv, i-1, saweval,FINDLEX_NOSEARCH);
333 Perl_pad_findmy(pTHX_ char *name)
339 SV **svp = AvARRAY(PL_comppad_name);
340 U32 seq = PL_cop_seqmax;
346 * Special case to get lexical (and hence per-thread) @_.
347 * XXX I need to find out how to tell at parse-time whether use
348 * of @_ should refer to a lexical (from a sub) or defgv (global
349 * scope and maybe weird sub-ish things like formats). See
350 * startsub in perly.y. It's possible that @_ could be lexical
351 * (at least from subs) even in non-threaded perl.
353 if (strEQ(name, "@_"))
354 return 0; /* success. (NOT_IN_PAD indicates failure) */
355 #endif /* USE_THREADS */
357 /* The one we're looking for is probably just before comppad_name_fill. */
358 for (off = AvFILLp(PL_comppad_name); off > 0; off--) {
359 if ((sv = svp[off]) &&
360 sv != &PL_sv_undef &&
363 seq > I_32(SvNVX(sv)))) &&
364 strEQ(SvPVX(sv), name))
366 if (SvIVX(sv) || SvFLAGS(sv) & SVpad_OUR)
367 return (PADOFFSET)off;
368 pendoff = off; /* this pending def. will override import */
372 outside = CvOUTSIDE(PL_compcv);
374 /* Check if if we're compiling an eval'', and adjust seq to be the
375 * eval's seq number. This depends on eval'' having a non-null
376 * CvOUTSIDE() while it is being compiled. The eval'' itself is
377 * identified by CvEVAL being true and CvGV being null. */
378 if (outside && CvEVAL(PL_compcv) && !CvGV(PL_compcv) && cxstack_ix >= 0) {
379 cx = &cxstack[cxstack_ix];
381 seq = cx->blk_oldcop->cop_seq;
384 /* See if it's in a nested scope */
385 off = pad_findlex(name, 0, seq, outside, cxstack_ix, 0, 0);
387 /* If there is a pending local definition, this new alias must die */
389 SvIVX(AvARRAY(PL_comppad_name)[off]) = seq;
390 return off; /* pad_findlex returns 0 for failure...*/
392 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
396 Perl_pad_leavemy(pTHX_ I32 fill)
400 SV **svp = AvARRAY(PL_comppad_name);
402 if (PL_min_intro_pending && fill < PL_min_intro_pending) {
403 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
404 if ((sv = svp[off]) && sv != &PL_sv_undef && ckWARN_d(WARN_INTERNAL))
405 Perl_warner(aTHX_ WARN_INTERNAL, "%s never introduced", SvPVX(sv));
408 /* "Deintroduce" my variables that are leaving with this scope. */
409 for (off = AvFILLp(PL_comppad_name); off > fill; off--) {
410 if ((sv = svp[off]) && sv != &PL_sv_undef && SvIVX(sv) == PAD_MAX)
411 SvIVX(sv) = PL_cop_seqmax;
416 Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype)
422 if (AvARRAY(PL_comppad) != PL_curpad)
423 Perl_croak(aTHX_ "panic: pad_alloc");
424 if (PL_pad_reset_pending)
426 if (tmptype & SVs_PADMY) {
428 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
429 } while (SvPADBUSY(sv)); /* need a fresh one */
430 retval = AvFILLp(PL_comppad);
433 SV **names = AvARRAY(PL_comppad_name);
434 SSize_t names_fill = AvFILLp(PL_comppad_name);
437 * "foreach" index vars temporarily become aliases to non-"my"
438 * values. Thus we must skip, not just pad values that are
439 * marked as current pad values, but also those with names.
441 if (++PL_padix <= names_fill &&
442 (sv = names[PL_padix]) && sv != &PL_sv_undef)
444 sv = *av_fetch(PL_comppad, PL_padix, TRUE);
445 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)))
450 SvFLAGS(sv) |= tmptype;
451 PL_curpad = AvARRAY(PL_comppad);
453 DEBUG_X(PerlIO_printf(Perl_debug_log,
454 "0x%"UVxf" Pad 0x%"UVxf" alloc %ld for %s\n",
455 PTR2UV(thr), PTR2UV(PL_curpad),
456 (long) retval, PL_op_name[optype]));
458 DEBUG_X(PerlIO_printf(Perl_debug_log,
459 "Pad 0x%"UVxf" alloc %ld for %s\n",
461 (long) retval, PL_op_name[optype]));
462 #endif /* USE_THREADS */
463 return (PADOFFSET)retval;
467 Perl_pad_sv(pTHX_ PADOFFSET po)
471 DEBUG_X(PerlIO_printf(Perl_debug_log,
472 "0x%"UVxf" Pad 0x%"UVxf" sv %d\n",
473 PTR2UV(thr), PTR2UV(PL_curpad), po));
476 Perl_croak(aTHX_ "panic: pad_sv po");
477 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" sv %d\n",
478 PTR2UV(PL_curpad), po));
479 #endif /* USE_THREADS */
480 return PL_curpad[po]; /* eventually we'll turn this into a macro */
484 Perl_pad_free(pTHX_ PADOFFSET po)
489 if (AvARRAY(PL_comppad) != PL_curpad)
490 Perl_croak(aTHX_ "panic: pad_free curpad");
492 Perl_croak(aTHX_ "panic: pad_free po");
494 DEBUG_X(PerlIO_printf(Perl_debug_log,
495 "0x%"UVxf" Pad 0x%"UVxf" free %d\n",
496 PTR2UV(thr), PTR2UV(PL_curpad), po));
498 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" free %d\n",
499 PTR2UV(PL_curpad), po));
500 #endif /* USE_THREADS */
501 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef)
502 SvPADTMP_off(PL_curpad[po]);
503 if ((I32)po < PL_padix)
508 Perl_pad_swipe(pTHX_ PADOFFSET po)
511 if (AvARRAY(PL_comppad) != PL_curpad)
512 Perl_croak(aTHX_ "panic: pad_swipe curpad");
514 Perl_croak(aTHX_ "panic: pad_swipe po");
516 DEBUG_X(PerlIO_printf(Perl_debug_log,
517 "0x%"UVxf" Pad 0x%"UVxf" swipe %d\n",
518 PTR2UV(thr), PTR2UV(PL_curpad), po));
520 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" swipe %d\n",
521 PTR2UV(PL_curpad), po));
522 #endif /* USE_THREADS */
523 SvPADTMP_off(PL_curpad[po]);
524 PL_curpad[po] = NEWSV(1107,0);
525 SvPADTMP_on(PL_curpad[po]);
526 if ((I32)po < PL_padix)
530 /* XXX pad_reset() is currently disabled because it results in serious bugs.
531 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
532 * on the stack by OPs that use them, there are several ways to get an alias
533 * to a shared TARG. Such an alias will change randomly and unpredictably.
534 * We avoid doing this until we can think of a Better Way.
539 #ifdef USE_BROKEN_PAD_RESET
543 if (AvARRAY(PL_comppad) != PL_curpad)
544 Perl_croak(aTHX_ "panic: pad_reset curpad");
546 DEBUG_X(PerlIO_printf(Perl_debug_log,
547 "0x%"UVxf" Pad 0x%"UVxf" reset\n",
548 PTR2UV(thr), PTR2UV(PL_curpad)));
550 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" reset\n",
552 #endif /* USE_THREADS */
553 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
554 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
555 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
556 SvPADTMP_off(PL_curpad[po]);
558 PL_padix = PL_padix_floor;
561 PL_pad_reset_pending = FALSE;
565 /* find_threadsv is not reentrant */
567 Perl_find_threadsv(pTHX_ const char *name)
573 /* We currently only handle names of a single character */
574 p = strchr(PL_threadsv_names, *name);
577 key = p - PL_threadsv_names;
578 MUTEX_LOCK(&thr->mutex);
579 svp = av_fetch(thr->threadsv, key, FALSE);
581 MUTEX_UNLOCK(&thr->mutex);
583 SV *sv = NEWSV(0, 0);
584 av_store(thr->threadsv, key, sv);
585 thr->threadsvp = AvARRAY(thr->threadsv);
586 MUTEX_UNLOCK(&thr->mutex);
588 * Some magic variables used to be automagically initialised
589 * in gv_fetchpv. Those which are now per-thread magicals get
590 * initialised here instead.
596 sv_setpv(sv, "\034");
597 sv_magic(sv, 0, 0, name, 1);
602 PL_sawampersand = TRUE;
616 /* XXX %! tied to Errno.pm needs to be added here.
617 * See gv_fetchpv(). */
621 sv_magic(sv, 0, 0, name, 1);
623 DEBUG_S(PerlIO_printf(Perl_error_log,
624 "find_threadsv: new SV %p for $%s%c\n",
625 sv, (*name < 32) ? "^" : "",
626 (*name < 32) ? toCTRL(*name) : *name));
630 #endif /* USE_THREADS */
635 Perl_op_free(pTHX_ OP *o)
637 register OP *kid, *nextkid;
640 if (!o || o->op_seq == (U16)-1)
643 if (o->op_flags & OPf_KIDS) {
644 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
645 nextkid = kid->op_sibling; /* Get before next freeing kid */
653 /* COP* is not cleared by op_clear() so that we may track line
654 * numbers etc even after null() */
655 if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
660 #ifdef PL_OP_SLAB_ALLOC
661 if ((char *) o == PL_OpPtr)
670 S_op_clear(pTHX_ OP *o)
672 switch (o->op_type) {
673 case OP_NULL: /* Was holding old type, if any. */
674 case OP_ENTEREVAL: /* Was holding hints. */
676 case OP_THREADSV: /* Was holding index into thr->threadsv AV. */
682 if (!(o->op_flags & OPf_SPECIAL))
685 #endif /* USE_THREADS */
687 if (!(o->op_flags & OPf_REF)
688 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
694 SvREFCNT_dec(cGVOPo->op_gv);
695 cGVOPo->op_gv = Nullgv;
698 SvREFCNT_dec(cSVOPo->op_sv);
699 cSVOPo->op_sv = Nullsv;
705 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
709 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
710 SvREFCNT_dec(cSVOPo->op_sv);
711 cSVOPo->op_sv = Nullsv;
714 Safefree(cPVOPo->op_pv);
715 cPVOPo->op_pv = Nullch;
719 op_free(cPMOPo->op_pmreplroot);
720 cPMOPo->op_pmreplroot = Nullop;
725 ReREFCNT_dec(cPMOPo->op_pmregexp);
726 cPMOPo->op_pmregexp = (REGEXP*)NULL;
731 pad_free(o->op_targ);
735 S_cop_free(pTHX_ COP* cop)
737 Safefree(cop->cop_label);
738 SvREFCNT_dec(cop->cop_filegv);
739 if (! specialWARN(cop->cop_warnings))
740 SvREFCNT_dec(cop->cop_warnings);
746 if (o->op_type == OP_NULL)
749 o->op_targ = o->op_type;
750 o->op_type = OP_NULL;
751 o->op_ppaddr = PL_ppaddr[OP_NULL];
754 /* Contextualizers */
756 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
759 Perl_linklist(pTHX_ OP *o)
766 /* establish postfix order */
767 if (cUNOPo->op_first) {
768 o->op_next = LINKLIST(cUNOPo->op_first);
769 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
771 kid->op_next = LINKLIST(kid->op_sibling);
783 Perl_scalarkids(pTHX_ OP *o)
786 if (o && o->op_flags & OPf_KIDS) {
787 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
794 S_scalarboolean(pTHX_ OP *o)
796 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
798 if (ckWARN(WARN_SYNTAX)) {
799 line_t oldline = PL_curcop->cop_line;
801 if (PL_copline != NOLINE)
802 PL_curcop->cop_line = PL_copline;
803 Perl_warner(aTHX_ WARN_SYNTAX, "Found = in conditional, should be ==");
804 PL_curcop->cop_line = oldline;
811 Perl_scalar(pTHX_ OP *o)
815 /* assumes no premature commitment */
816 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
817 || o->op_type == OP_RETURN)
820 if ((o->op_private & OPpTARGET_MY)
821 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
822 return scalar(o); /* As if inside SASSIGN */
824 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
826 switch (o->op_type) {
828 if (o->op_private & OPpREPEAT_DOLIST)
829 null(((LISTOP*)cBINOPo->op_first)->op_first);
830 scalar(cBINOPo->op_first);
835 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
839 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
840 if (!kPMOP->op_pmreplroot)
841 deprecate("implicit split to @_");
849 if (o->op_flags & OPf_KIDS) {
850 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
856 kid = cLISTOPo->op_first;
858 while (kid = kid->op_sibling) {
864 WITH_THR(PL_curcop = &PL_compiling);
869 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
875 WITH_THR(PL_curcop = &PL_compiling);
882 Perl_scalarvoid(pTHX_ OP *o)
889 if (o->op_type == OP_NEXTSTATE
890 || o->op_type == OP_SETSTATE
891 || o->op_type == OP_DBSTATE
892 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
893 || o->op_targ == OP_SETSTATE
894 || o->op_targ == OP_DBSTATE)))
897 PL_curcop = (COP*)o; /* for warning below */
900 /* assumes no premature commitment */
901 want = o->op_flags & OPf_WANT;
902 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
903 || o->op_type == OP_RETURN)
906 if ((o->op_private & OPpTARGET_MY)
907 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
908 return scalar(o); /* As if inside SASSIGN */
910 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
912 switch (o->op_type) {
914 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
918 if (o->op_flags & OPf_STACKED)
922 if (o->op_private == 4)
993 if (!(o->op_private & OPpLVAL_INTRO))
994 useless = PL_op_desc[o->op_type];
1001 if (!(o->op_private & OPpLVAL_INTRO) &&
1002 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1003 useless = "a variable";
1008 if (cSVOPo->op_private & OPpCONST_STRICT)
1009 no_bareword_allowed(o);
1012 if (ckWARN(WARN_VOID)) {
1013 useless = "a constant";
1014 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1016 else if (SvPOK(sv)) {
1017 if (strnEQ(SvPVX(sv), "di", 2) ||
1018 strnEQ(SvPVX(sv), "ds", 2) ||
1019 strnEQ(SvPVX(sv), "ig", 2))
1024 null(o); /* don't execute or even remember it */
1028 o->op_type = OP_PREINC; /* pre-increment is faster */
1029 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1033 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1034 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1040 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1045 if (o->op_flags & OPf_STACKED)
1053 if (!(o->op_flags & OPf_KIDS))
1062 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1069 /* all requires must return a boolean value */
1070 o->op_flags &= ~OPf_WANT;
1073 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
1074 if (!kPMOP->op_pmreplroot)
1075 deprecate("implicit split to @_");
1081 if (ckWARN(WARN_VOID))
1082 Perl_warner(aTHX_ WARN_VOID, "Useless use of %s in void context", useless);
1088 Perl_listkids(pTHX_ OP *o)
1091 if (o && o->op_flags & OPf_KIDS) {
1092 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1099 Perl_list(pTHX_ OP *o)
1103 /* assumes no premature commitment */
1104 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
1105 || o->op_type == OP_RETURN)
1108 if ((o->op_private & OPpTARGET_MY)
1109 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
1110 return o; /* As if inside SASSIGN */
1112 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1114 switch (o->op_type) {
1117 list(cBINOPo->op_first);
1122 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1130 if (!(o->op_flags & OPf_KIDS))
1132 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1133 list(cBINOPo->op_first);
1134 return gen_constant_list(o);
1141 kid = cLISTOPo->op_first;
1143 while (kid = kid->op_sibling) {
1144 if (kid->op_sibling)
1149 WITH_THR(PL_curcop = &PL_compiling);
1153 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1154 if (kid->op_sibling)
1159 WITH_THR(PL_curcop = &PL_compiling);
1162 /* all requires must return a boolean value */
1163 o->op_flags &= ~OPf_WANT;
1170 Perl_scalarseq(pTHX_ OP *o)
1175 if (o->op_type == OP_LINESEQ ||
1176 o->op_type == OP_SCOPE ||
1177 o->op_type == OP_LEAVE ||
1178 o->op_type == OP_LEAVETRY)
1181 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1182 if (kid->op_sibling) {
1186 PL_curcop = &PL_compiling;
1188 o->op_flags &= ~OPf_PARENS;
1189 if (PL_hints & HINT_BLOCK_SCOPE)
1190 o->op_flags |= OPf_PARENS;
1193 o = newOP(OP_STUB, 0);
1198 S_modkids(pTHX_ OP *o, I32 type)
1201 if (o && o->op_flags & OPf_KIDS) {
1202 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1209 Perl_mod(pTHX_ OP *o, I32 type)
1216 if (!o || PL_error_count)
1219 if ((o->op_private & OPpTARGET_MY)
1220 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
1223 switch (o->op_type) {
1228 if (!(o->op_private & (OPpCONST_ARYBASE)))
1230 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1231 PL_compiling.cop_arybase = (I32)SvIV(((SVOP*)PL_eval_start)->op_sv);
1235 SAVEI32(PL_compiling.cop_arybase);
1236 PL_compiling.cop_arybase = 0;
1238 else if (type == OP_REFGEN)
1241 Perl_croak(aTHX_ "That use of $[ is unsupported");
1244 if (o->op_flags & OPf_PARENS)
1248 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1249 !(o->op_flags & OPf_STACKED)) {
1250 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1251 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1252 assert(cUNOPo->op_first->op_type == OP_NULL);
1253 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1256 else { /* lvalue subroutine call */
1257 o->op_private |= OPpLVAL_INTRO;
1258 if (type == OP_GREPSTART || type == OP_ENTERSUB) {
1259 /* Backward compatibility mode: */
1260 o->op_private |= OPpENTERSUB_INARGS;
1263 else { /* Compile-time error message: */
1264 OP *kid = cUNOPo->op_first;
1268 if (kid->op_type == OP_PUSHMARK)
1270 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1272 "panic: unexpected lvalue entersub "
1273 "args: type/targ %ld:%ld",
1274 (long)kid->op_type,kid->op_targ);
1275 kid = kLISTOP->op_first;
1277 while (kid->op_sibling)
1278 kid = kid->op_sibling;
1279 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1281 if (kid->op_type == OP_METHOD_NAMED
1282 || kid->op_type == OP_METHOD)
1286 if (kid->op_sibling || kid->op_next != kid) {
1287 yyerror("panic: unexpected optree near method call");
1291 NewOp(1101, newop, 1, OP);
1292 newop->op_type = OP_RV2CV;
1293 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1294 newop->op_next = newop;
1295 kid->op_sibling = newop;
1296 newop->op_private |= OPpLVAL_INTRO;
1300 if (kid->op_type != OP_RV2CV)
1302 "panic: unexpected lvalue entersub "
1303 "entry via type/targ %ld:%ld",
1304 (long)kid->op_type,kid->op_targ);
1305 kid->op_private |= OPpLVAL_INTRO;
1306 break; /* Postpone until runtime */
1310 kid = kUNOP->op_first;
1311 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1312 kid = kUNOP->op_first;
1313 if (kid->op_type == OP_NULL)
1315 "Unexpected constant lvalue entersub "
1316 "entry via type/targ %ld:%ld",
1317 (long)kid->op_type,kid->op_targ);
1318 if (kid->op_type != OP_GV) {
1319 /* Restore RV2CV to check lvalueness */
1321 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1322 okid->op_next = kid->op_next;
1323 kid->op_next = okid;
1326 okid->op_next = Nullop;
1327 okid->op_type = OP_RV2CV;
1329 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1330 okid->op_private |= OPpLVAL_INTRO;
1334 cv = GvCV(kGVOP->op_gv);
1344 /* grep, foreach, subcalls, refgen */
1345 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1347 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1348 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1350 : (o->op_type == OP_ENTERSUB
1351 ? "non-lvalue subroutine call"
1352 : PL_op_desc[o->op_type])),
1353 type ? PL_op_desc[type] : "local"));
1367 case OP_RIGHT_SHIFT:
1376 if (!(o->op_flags & OPf_STACKED))
1382 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1388 if (!type && cUNOPo->op_first->op_type != OP_GV)
1389 Perl_croak(aTHX_ "Can't localize through a reference");
1390 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1391 PL_modcount = 10000;
1392 return o; /* Treat \(@foo) like ordinary list. */
1396 if (scalar_mod_type(o, type))
1398 ref(cUNOPo->op_first, o->op_type);
1407 PL_modcount = 10000;
1410 if (!type && cUNOPo->op_first->op_type != OP_GV)
1411 Perl_croak(aTHX_ "Can't localize through a reference");
1412 ref(cUNOPo->op_first, o->op_type);
1416 PL_hints |= HINT_BLOCK_SCOPE;
1426 PL_modcount = 10000;
1427 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1428 return o; /* Treat \(@foo) like ordinary list. */
1429 if (scalar_mod_type(o, type))
1435 Perl_croak(aTHX_ "Can't localize lexical variable %s",
1436 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
1441 PL_modcount++; /* XXX ??? */
1443 #endif /* USE_THREADS */
1449 if (type != OP_SASSIGN)
1453 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1459 pad_free(o->op_targ);
1460 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1461 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1462 if (o->op_flags & OPf_KIDS)
1463 mod(cBINOPo->op_first->op_sibling, type);
1468 ref(cBINOPo->op_first, o->op_type);
1469 if (type == OP_ENTERSUB &&
1470 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1471 o->op_private |= OPpLVAL_DEFER;
1478 if (o->op_flags & OPf_KIDS)
1479 mod(cLISTOPo->op_last, type);
1483 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1485 else if (!(o->op_flags & OPf_KIDS))
1487 if (o->op_targ != OP_LIST) {
1488 mod(cBINOPo->op_first, type);
1493 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1497 o->op_flags |= OPf_MOD;
1499 if (type == OP_AASSIGN || type == OP_SASSIGN)
1500 o->op_flags |= OPf_SPECIAL|OPf_REF;
1502 o->op_private |= OPpLVAL_INTRO;
1503 o->op_flags &= ~OPf_SPECIAL;
1504 PL_hints |= HINT_BLOCK_SCOPE;
1506 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1507 o->op_flags |= OPf_REF;
1512 S_scalar_mod_type(pTHX_ OP *o, I32 type)
1516 if (o->op_type == OP_RV2GV)
1540 case OP_RIGHT_SHIFT:
1559 S_is_handle_constructor(pTHX_ OP *o, I32 argnum)
1561 switch (o->op_type) {
1569 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1582 Perl_refkids(pTHX_ OP *o, I32 type)
1585 if (o && o->op_flags & OPf_KIDS) {
1586 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1593 Perl_ref(pTHX_ OP *o, I32 type)
1597 if (!o || PL_error_count)
1600 switch (o->op_type) {
1602 if ((type == OP_DEFINED || type == OP_LOCK) &&
1603 !(o->op_flags & OPf_STACKED)) {
1604 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1605 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1606 assert(cUNOPo->op_first->op_type == OP_NULL);
1607 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1608 o->op_flags |= OPf_SPECIAL;
1613 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1617 if (type == OP_DEFINED)
1618 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1619 ref(cUNOPo->op_first, o->op_type);
1622 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1623 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1624 : type == OP_RV2HV ? OPpDEREF_HV
1626 o->op_flags |= OPf_MOD;
1631 o->op_flags |= OPf_MOD; /* XXX ??? */
1636 o->op_flags |= OPf_REF;
1639 if (type == OP_DEFINED)
1640 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1641 ref(cUNOPo->op_first, o->op_type);
1646 o->op_flags |= OPf_REF;
1651 if (!(o->op_flags & OPf_KIDS))
1653 ref(cBINOPo->op_first, type);
1657 ref(cBINOPo->op_first, o->op_type);
1658 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1659 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1660 : type == OP_RV2HV ? OPpDEREF_HV
1662 o->op_flags |= OPf_MOD;
1670 if (!(o->op_flags & OPf_KIDS))
1672 ref(cLISTOPo->op_last, type);
1682 S_dup_attrlist(pTHX_ OP *o)
1686 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1687 * where the first kid is OP_PUSHMARK and the remaining ones
1688 * are OP_CONST. We need to push the OP_CONST values.
1690 if (o->op_type == OP_CONST)
1691 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc(cSVOPo->op_sv));
1693 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1694 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1695 if (o->op_type == OP_CONST)
1696 rop = append_elem(OP_LIST, rop,
1697 newSVOP(OP_CONST, o->op_flags,
1698 SvREFCNT_inc(cSVOPo->op_sv)));
1705 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
1707 OP *modname; /* for 'use' */
1710 /* fake up C<use attributes $pkg,$rv,@attrs> */
1711 ENTER; /* need to protect against side-effects of 'use' */
1713 if (stash && HvNAME(stash))
1714 stashsv = newSVpv(HvNAME(stash), 0);
1716 stashsv = &PL_sv_no;
1717 #define ATTRSMODULE "attributes"
1718 modname = newSVOP(OP_CONST, 0,
1719 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1));
1720 modname->op_private |= OPpCONST_BARE;
1721 /* that flag is required to make 'use' work right */
1722 utilize(1, start_subparse(FALSE, 0),
1723 Nullop, /* version */
1725 prepend_elem(OP_LIST,
1726 newSVOP(OP_CONST, 0, stashsv),
1727 prepend_elem(OP_LIST,
1728 newSVOP(OP_CONST, 0, newRV(target)),
1729 dup_attrlist(attrs))));
1734 S_my_kid(pTHX_ OP *o, OP *attrs)
1739 if (!o || PL_error_count)
1743 if (type == OP_LIST) {
1744 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1746 } else if (type == OP_UNDEF) {
1748 } else if (type == OP_RV2SV || /* "our" declaration */
1750 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
1752 } else if (type != OP_PADSV &&
1755 type != OP_PUSHMARK)
1757 yyerror(Perl_form(aTHX_ "Can't declare %s in my", PL_op_desc[o->op_type]));
1760 else if (attrs && type != OP_PUSHMARK) {
1765 /* check for C<my Dog $spot> when deciding package */
1766 namesvp = av_fetch(PL_comppad_name, o->op_targ, FALSE);
1767 if (namesvp && *namesvp && SvOBJECT(*namesvp) && HvNAME(SvSTASH(*namesvp)))
1768 stash = SvSTASH(*namesvp);
1770 stash = PL_curstash;
1771 padsv = PAD_SV(o->op_targ);
1772 apply_attrs(stash, padsv, attrs);
1774 o->op_flags |= OPf_MOD;
1775 o->op_private |= OPpLVAL_INTRO;
1780 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1782 if (o->op_flags & OPf_PARENS)
1785 PL_in_my_stash = Nullhv;
1788 return my_kid(o, attrs);
1792 Perl_my(pTHX_ OP *o)
1794 return my_kid(o, Nullop);
1798 Perl_sawparens(pTHX_ OP *o)
1801 o->op_flags |= OPf_PARENS;
1806 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
1811 if (ckWARN(WARN_UNSAFE) &&
1812 (left->op_type == OP_RV2AV ||
1813 left->op_type == OP_RV2HV ||
1814 left->op_type == OP_PADAV ||
1815 left->op_type == OP_PADHV)) {
1816 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
1817 right->op_type == OP_TRANS)
1818 ? right->op_type : OP_MATCH];
1819 const char *sample = ((left->op_type == OP_RV2AV ||
1820 left->op_type == OP_PADAV)
1821 ? "@array" : "%hash");
1822 Perl_warner(aTHX_ WARN_UNSAFE,
1823 "Applying %s to %s will act on scalar(%s)",
1824 desc, sample, sample);
1827 if (right->op_type == OP_MATCH ||
1828 right->op_type == OP_SUBST ||
1829 right->op_type == OP_TRANS) {
1830 right->op_flags |= OPf_STACKED;
1831 if (right->op_type != OP_MATCH)
1832 left = mod(left, right->op_type);
1833 if (right->op_type == OP_TRANS)
1834 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1836 o = prepend_elem(right->op_type, scalar(left), right);
1838 return newUNOP(OP_NOT, 0, scalar(o));
1842 return bind_match(type, left,
1843 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1847 Perl_invert(pTHX_ OP *o)
1851 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1852 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1856 Perl_scope(pTHX_ OP *o)
1859 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
1860 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1861 o->op_type = OP_LEAVE;
1862 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
1865 if (o->op_type == OP_LINESEQ) {
1867 o->op_type = OP_SCOPE;
1868 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
1869 kid = ((LISTOP*)o)->op_first;
1870 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
1874 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1881 Perl_save_hints(pTHX)
1884 SAVESPTR(GvHV(PL_hintgv));
1885 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
1886 SAVEFREESV(GvHV(PL_hintgv));
1890 Perl_block_start(pTHX_ int full)
1893 int retval = PL_savestack_ix;
1895 SAVEI32(PL_comppad_name_floor);
1897 if ((PL_comppad_name_fill = AvFILLp(PL_comppad_name)) > 0)
1898 PL_comppad_name_floor = PL_comppad_name_fill;
1900 PL_comppad_name_floor = 0;
1902 SAVEI32(PL_min_intro_pending);
1903 SAVEI32(PL_max_intro_pending);
1904 PL_min_intro_pending = 0;
1905 SAVEI32(PL_comppad_name_fill);
1906 SAVEI32(PL_padix_floor);
1907 PL_padix_floor = PL_padix;
1908 PL_pad_reset_pending = FALSE;
1910 PL_hints &= ~HINT_BLOCK_SCOPE;
1911 SAVEPPTR(PL_compiling.cop_warnings);
1912 if (! specialWARN(PL_compiling.cop_warnings)) {
1913 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1914 SAVEFREESV(PL_compiling.cop_warnings) ;
1922 Perl_block_end(pTHX_ I32 floor, OP *seq)
1925 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
1926 OP* retval = scalarseq(seq);
1928 PL_pad_reset_pending = FALSE;
1929 PL_compiling.op_private = PL_hints;
1931 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1932 pad_leavemy(PL_comppad_name_fill);
1941 OP *o = newOP(OP_THREADSV, 0);
1942 o->op_targ = find_threadsv("_");
1945 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
1946 #endif /* USE_THREADS */
1950 Perl_newPROG(pTHX_ OP *o)
1956 PL_eval_root = newUNOP(OP_LEAVEEVAL,
1957 ((PL_in_eval & EVAL_KEEPERR)
1958 ? OPf_SPECIAL : 0), o);
1959 PL_eval_start = linklist(PL_eval_root);
1960 PL_eval_root->op_next = 0;
1961 peep(PL_eval_start);
1966 PL_main_root = scope(sawparens(scalarvoid(o)));
1967 PL_curcop = &PL_compiling;
1968 PL_main_start = LINKLIST(PL_main_root);
1969 PL_main_root->op_next = 0;
1970 peep(PL_main_start);
1973 /* Register with debugger */
1975 CV *cv = get_cv("DB::postponed", FALSE);
1979 XPUSHs((SV*)PL_compiling.cop_filegv);
1981 call_sv((SV*)cv, G_DISCARD);
1988 Perl_localize(pTHX_ OP *o, I32 lex)
1990 if (o->op_flags & OPf_PARENS)
1994 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
1996 for (s = PL_bufptr; *s && (isALNUM(*s) || (*s & 0x80) || strchr("@$%, ",*s)); s++) ;
1997 if (*s == ';' || *s == '=')
1998 Perl_warner(aTHX_ WARN_PARENTHESIS, "Parentheses missing around \"%s\" list",
1999 lex ? "my" : "local");
2003 PL_in_my_stash = Nullhv;
2007 return mod(o, OP_NULL); /* a bit kludgey */
2011 Perl_jmaybe(pTHX_ OP *o)
2013 if (o->op_type == OP_LIST) {
2016 o2 = newOP(OP_THREADSV, 0);
2017 o2->op_targ = find_threadsv(";");
2019 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
2020 #endif /* USE_THREADS */
2021 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2027 Perl_fold_constants(pTHX_ register OP *o)
2031 I32 type = o->op_type;
2034 if (PL_opargs[type] & OA_RETSCALAR)
2036 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2037 o->op_targ = pad_alloc(type, SVs_PADTMP);
2039 /* integerize op, unless it happens to be C<-foo>.
2040 * XXX should pp_i_negate() do magic string negation instead? */
2041 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2042 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2043 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2045 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2048 if (!(PL_opargs[type] & OA_FOLDCONST))
2053 /* XXX might want a ck_negate() for this */
2054 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2067 if (o->op_private & OPpLOCALE)
2072 goto nope; /* Don't try to run w/ errors */
2074 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2075 if ((curop->op_type != OP_CONST ||
2076 (curop->op_private & OPpCONST_BARE)) &&
2077 curop->op_type != OP_LIST &&
2078 curop->op_type != OP_SCALAR &&
2079 curop->op_type != OP_NULL &&
2080 curop->op_type != OP_PUSHMARK)
2086 curop = LINKLIST(o);
2090 sv = *(PL_stack_sp--);
2091 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
2092 pad_swipe(o->op_targ);
2093 else if (SvTEMP(sv)) { /* grab mortal temp? */
2094 (void)SvREFCNT_inc(sv);
2098 if (type == OP_RV2GV)
2099 return newGVOP(OP_GV, 0, (GV*)sv);
2101 /* try to smush double to int, but don't smush -2.0 to -2 */
2102 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
2106 if ((NV)iv == SvNV(sv)) {
2111 SvIOK_off(sv); /* undo SvIV() damage */
2113 return newSVOP(OP_CONST, 0, sv);
2117 if (!(PL_opargs[type] & OA_OTHERINT))
2120 if (!(PL_hints & HINT_INTEGER)) {
2121 if (type == OP_MODULO
2122 || type == OP_DIVIDE
2123 || !(o->op_flags & OPf_KIDS))
2128 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
2129 if (curop->op_type == OP_CONST) {
2130 if (SvIOK(((SVOP*)curop)->op_sv))
2134 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
2138 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
2145 Perl_gen_constant_list(pTHX_ register OP *o)
2149 I32 oldtmps_floor = PL_tmps_floor;
2153 return o; /* Don't attempt to run with errors */
2155 PL_op = curop = LINKLIST(o);
2161 PL_tmps_floor = oldtmps_floor;
2163 o->op_type = OP_RV2AV;
2164 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2165 curop = ((UNOP*)o)->op_first;
2166 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
2173 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2178 if (!o || o->op_type != OP_LIST)
2179 o = newLISTOP(OP_LIST, 0, o, Nullop);
2181 o->op_flags &= ~OPf_WANT;
2183 if (!(PL_opargs[type] & OA_MARK))
2184 null(cLISTOPo->op_first);
2187 o->op_ppaddr = PL_ppaddr[type];
2188 o->op_flags |= flags;
2190 o = CHECKOP(type, o);
2191 if (o->op_type != type)
2194 if (cLISTOPo->op_children < 7) {
2195 /* XXX do we really need to do this if we're done appending?? */
2196 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2198 cLISTOPo->op_last = last; /* in case check substituted last arg */
2201 return fold_constants(o);
2204 /* List constructors */
2207 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2215 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
2216 return newLISTOP(type, 0, first, last);
2218 if (first->op_flags & OPf_KIDS)
2219 ((LISTOP*)first)->op_last->op_sibling = last;
2221 first->op_flags |= OPf_KIDS;
2222 ((LISTOP*)first)->op_first = last;
2224 ((LISTOP*)first)->op_last = last;
2225 ((LISTOP*)first)->op_children++;
2230 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2238 if (first->op_type != type)
2239 return prepend_elem(type, (OP*)first, (OP*)last);
2241 if (last->op_type != type)
2242 return append_elem(type, (OP*)first, (OP*)last);
2244 first->op_last->op_sibling = last->op_first;
2245 first->op_last = last->op_last;
2246 first->op_children += last->op_children;
2247 if (first->op_children)
2248 first->op_flags |= OPf_KIDS;
2250 #ifdef PL_OP_SLAB_ALLOC
2258 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2266 if (last->op_type == type) {
2267 if (type == OP_LIST) { /* already a PUSHMARK there */
2268 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2269 ((LISTOP*)last)->op_first->op_sibling = first;
2272 if (!(last->op_flags & OPf_KIDS)) {
2273 ((LISTOP*)last)->op_last = first;
2274 last->op_flags |= OPf_KIDS;
2276 first->op_sibling = ((LISTOP*)last)->op_first;
2277 ((LISTOP*)last)->op_first = first;
2279 ((LISTOP*)last)->op_children++;
2283 return newLISTOP(type, 0, first, last);
2289 Perl_newNULLLIST(pTHX)
2291 return newOP(OP_STUB, 0);
2295 Perl_force_list(pTHX_ OP *o)
2297 if (!o || o->op_type != OP_LIST)
2298 o = newLISTOP(OP_LIST, 0, o, Nullop);
2304 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2308 NewOp(1101, listop, 1, LISTOP);
2310 listop->op_type = type;
2311 listop->op_ppaddr = PL_ppaddr[type];
2312 listop->op_children = (first != 0) + (last != 0);
2313 listop->op_flags = flags;
2317 else if (!first && last)
2320 first->op_sibling = last;
2321 listop->op_first = first;
2322 listop->op_last = last;
2323 if (type == OP_LIST) {
2325 pushop = newOP(OP_PUSHMARK, 0);
2326 pushop->op_sibling = first;
2327 listop->op_first = pushop;
2328 listop->op_flags |= OPf_KIDS;
2330 listop->op_last = pushop;
2332 else if (listop->op_children)
2333 listop->op_flags |= OPf_KIDS;
2339 Perl_newOP(pTHX_ I32 type, I32 flags)
2342 NewOp(1101, o, 1, OP);
2344 o->op_ppaddr = PL_ppaddr[type];
2345 o->op_flags = flags;
2348 o->op_private = 0 + (flags >> 8);
2349 if (PL_opargs[type] & OA_RETSCALAR)
2351 if (PL_opargs[type] & OA_TARGET)
2352 o->op_targ = pad_alloc(type, SVs_PADTMP);
2353 return CHECKOP(type, o);
2357 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
2362 first = newOP(OP_STUB, 0);
2363 if (PL_opargs[type] & OA_MARK)
2364 first = force_list(first);
2366 NewOp(1101, unop, 1, UNOP);
2367 unop->op_type = type;
2368 unop->op_ppaddr = PL_ppaddr[type];
2369 unop->op_first = first;
2370 unop->op_flags = flags | OPf_KIDS;
2371 unop->op_private = 1 | (flags >> 8);
2372 unop = (UNOP*) CHECKOP(type, unop);
2376 return fold_constants((OP *) unop);
2380 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2383 NewOp(1101, binop, 1, BINOP);
2386 first = newOP(OP_NULL, 0);
2388 binop->op_type = type;
2389 binop->op_ppaddr = PL_ppaddr[type];
2390 binop->op_first = first;
2391 binop->op_flags = flags | OPf_KIDS;
2394 binop->op_private = 1 | (flags >> 8);
2397 binop->op_private = 2 | (flags >> 8);
2398 first->op_sibling = last;
2401 binop = (BINOP*)CHECKOP(type, binop);
2402 if (binop->op_next || binop->op_type != type)
2405 binop->op_last = binop->op_first->op_sibling;
2407 return fold_constants((OP *)binop);
2411 utf8compare(const void *a, const void *b)
2414 for (i = 0; i < 10; i++) {
2415 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2417 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2424 Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
2426 SV *tstr = ((SVOP*)expr)->op_sv;
2427 SV *rstr = ((SVOP*)repl)->op_sv;
2430 register U8 *t = (U8*)SvPV(tstr, tlen);
2431 register U8 *r = (U8*)SvPV(rstr, rlen);
2437 register short *tbl;
2439 complement = o->op_private & OPpTRANS_COMPLEMENT;
2440 del = o->op_private & OPpTRANS_DELETE;
2441 squash = o->op_private & OPpTRANS_SQUASH;
2443 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2444 SV* listsv = newSVpvn("# comment\n",10);
2446 U8* tend = t + tlen;
2447 U8* rend = r + rlen;
2463 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2464 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2470 New(1109, cp, tlen, U8*);
2472 transv = newSVpvn("",0);
2481 qsort(cp, i, sizeof(U8*), utf8compare);
2482 for (j = 0; j < i; j++) {
2484 UV val = utf8_to_uv(s, &ulen);
2486 diff = val - nextmin;
2488 t = uv_to_utf8(tmpbuf,nextmin);
2489 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2491 t = uv_to_utf8(tmpbuf, val - 1);
2492 sv_catpvn(transv, "\377", 1);
2493 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2497 val = utf8_to_uv(s+1, &ulen);
2501 t = uv_to_utf8(tmpbuf,nextmin);
2502 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2503 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2504 sv_catpvn(transv, "\377", 1);
2505 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2506 t = (U8*)SvPVX(transv);
2507 tlen = SvCUR(transv);
2510 else if (!rlen && !del) {
2511 r = t; rlen = tlen; rend = tend;
2514 if (to_utf && from_utf) { /* only counting characters */
2515 if (t == r || (tlen == rlen && memEQ(t, r, tlen)))
2516 o->op_private |= OPpTRANS_IDENTICAL;
2518 else { /* straight latin-1 translation */
2519 if (tlen == 4 && memEQ(t, "\0\377\303\277", 4) &&
2520 rlen == 4 && memEQ(r, "\0\377\303\277", 4))
2521 o->op_private |= OPpTRANS_IDENTICAL;
2525 while (t < tend || tfirst <= tlast) {
2526 /* see if we need more "t" chars */
2527 if (tfirst > tlast) {
2528 tfirst = (I32)utf8_to_uv(t, &ulen);
2530 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
2531 tlast = (I32)utf8_to_uv(++t, &ulen);
2538 /* now see if we need more "r" chars */
2539 if (rfirst > rlast) {
2541 rfirst = (I32)utf8_to_uv(r, &ulen);
2543 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
2544 rlast = (I32)utf8_to_uv(++r, &ulen);
2553 rfirst = rlast = 0xffffffff;
2557 /* now see which range will peter our first, if either. */
2558 tdiff = tlast - tfirst;
2559 rdiff = rlast - rfirst;
2566 if (rfirst == 0xffffffff) {
2567 diff = tdiff; /* oops, pretend rdiff is infinite */
2569 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\tXXXX\n", tfirst, tlast);
2571 Perl_sv_catpvf(aTHX_ listsv, "%04x\t\tXXXX\n", tfirst);
2575 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\t%04x\n", tfirst, tfirst + diff, rfirst);
2577 Perl_sv_catpvf(aTHX_ listsv, "%04x\t\t%04x\n", tfirst, rfirst);
2579 if (rfirst + diff > max)
2580 max = rfirst + diff;
2585 else if (rfirst <= 0x800)
2586 grows |= (tfirst < 0x80);
2587 else if (rfirst <= 0x10000)
2588 grows |= (tfirst < 0x800);
2589 else if (rfirst <= 0x200000)
2590 grows |= (tfirst < 0x10000);
2591 else if (rfirst <= 0x4000000)
2592 grows |= (tfirst < 0x200000);
2593 else if (rfirst <= 0x80000000)
2594 grows |= (tfirst < 0x4000000);
2606 else if (max > 0xff)
2611 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2612 SvREFCNT_dec(listsv);
2614 SvREFCNT_dec(transv);
2616 if (!del && havefinal)
2617 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5, newSViv((IV)final), 0);
2619 if (grows && to_utf)
2620 o->op_private |= OPpTRANS_GROWS;
2627 tbl = (short*)cPVOPo->op_pv;
2629 Zero(tbl, 256, short);
2630 for (i = 0; i < tlen; i++)
2632 for (i = 0, j = 0; i < 256; i++) {
2648 if (!rlen && !del) {
2651 o->op_private |= OPpTRANS_IDENTICAL;
2653 for (i = 0; i < 256; i++)
2655 for (i = 0, j = 0; i < tlen; i++,j++) {
2658 if (tbl[t[i]] == -1)
2664 if (tbl[t[i]] == -1)
2675 Perl_newPMOP(pTHX_ I32 type, I32 flags)
2680 NewOp(1101, pmop, 1, PMOP);
2681 pmop->op_type = type;
2682 pmop->op_ppaddr = PL_ppaddr[type];
2683 pmop->op_flags = flags;
2684 pmop->op_private = 0 | (flags >> 8);
2686 if (PL_hints & HINT_RE_TAINT)
2687 pmop->op_pmpermflags |= PMf_RETAINT;
2688 if (PL_hints & HINT_LOCALE)
2689 pmop->op_pmpermflags |= PMf_LOCALE;
2690 pmop->op_pmflags = pmop->op_pmpermflags;
2692 /* link into pm list */
2693 if (type != OP_TRANS && PL_curstash) {
2694 pmop->op_pmnext = HvPMROOT(PL_curstash);
2695 HvPMROOT(PL_curstash) = pmop;
2702 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
2707 I32 repl_has_vars = 0;
2709 if (o->op_type == OP_TRANS)
2710 return pmtrans(o, expr, repl);
2712 PL_hints |= HINT_BLOCK_SCOPE;
2715 if (expr->op_type == OP_CONST) {
2717 SV *pat = ((SVOP*)expr)->op_sv;
2718 char *p = SvPV(pat, plen);
2719 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2720 sv_setpvn(pat, "\\s+", 3);
2721 p = SvPV(pat, plen);
2722 pm->op_pmflags |= PMf_SKIPWHITE;
2724 pm->op_pmregexp = CALLREGCOMP(aTHX_ p, p + plen, pm);
2725 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2726 pm->op_pmflags |= PMf_WHITE;
2730 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2731 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2733 : OP_REGCMAYBE),0,expr);
2735 NewOp(1101, rcop, 1, LOGOP);
2736 rcop->op_type = OP_REGCOMP;
2737 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2738 rcop->op_first = scalar(expr);
2739 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2740 ? (OPf_SPECIAL | OPf_KIDS)
2742 rcop->op_private = 1;
2745 /* establish postfix order */
2746 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2748 rcop->op_next = expr;
2749 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2752 rcop->op_next = LINKLIST(expr);
2753 expr->op_next = (OP*)rcop;
2756 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2761 if (pm->op_pmflags & PMf_EVAL) {
2763 if (PL_curcop->cop_line < PL_multi_end)
2764 PL_curcop->cop_line = PL_multi_end;
2767 else if (repl->op_type == OP_THREADSV
2768 && strchr("&`'123456789+",
2769 PL_threadsv_names[repl->op_targ]))
2773 #endif /* USE_THREADS */
2774 else if (repl->op_type == OP_CONST)
2778 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2779 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2781 if (curop->op_type == OP_THREADSV) {
2783 if (strchr("&`'123456789+", curop->op_private))
2787 if (curop->op_type == OP_GV) {
2788 GV *gv = ((GVOP*)curop)->op_gv;
2790 if (strchr("&`'123456789+", *GvENAME(gv)))
2793 #endif /* USE_THREADS */
2794 else if (curop->op_type == OP_RV2CV)
2796 else if (curop->op_type == OP_RV2SV ||
2797 curop->op_type == OP_RV2AV ||
2798 curop->op_type == OP_RV2HV ||
2799 curop->op_type == OP_RV2GV) {
2800 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2803 else if (curop->op_type == OP_PADSV ||
2804 curop->op_type == OP_PADAV ||
2805 curop->op_type == OP_PADHV ||
2806 curop->op_type == OP_PADANY) {
2809 else if (curop->op_type == OP_PUSHRE)
2810 ; /* Okay here, dangerous in newASSIGNOP */
2819 && (!pm->op_pmregexp
2820 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
2821 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2822 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2823 prepend_elem(o->op_type, scalar(repl), o);
2826 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
2827 pm->op_pmflags |= PMf_MAYBE_CONST;
2828 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2830 NewOp(1101, rcop, 1, LOGOP);
2831 rcop->op_type = OP_SUBSTCONT;
2832 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
2833 rcop->op_first = scalar(repl);
2834 rcop->op_flags |= OPf_KIDS;
2835 rcop->op_private = 1;
2838 /* establish postfix order */
2839 rcop->op_next = LINKLIST(repl);
2840 repl->op_next = (OP*)rcop;
2842 pm->op_pmreplroot = scalar((OP*)rcop);
2843 pm->op_pmreplstart = LINKLIST(rcop);
2852 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
2855 NewOp(1101, svop, 1, SVOP);
2856 svop->op_type = type;
2857 svop->op_ppaddr = PL_ppaddr[type];
2859 svop->op_next = (OP*)svop;
2860 svop->op_flags = flags;
2861 if (PL_opargs[type] & OA_RETSCALAR)
2863 if (PL_opargs[type] & OA_TARGET)
2864 svop->op_targ = pad_alloc(type, SVs_PADTMP);
2865 return CHECKOP(type, svop);
2869 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
2873 NewOp(1101, gvop, 1, GVOP);
2874 gvop->op_type = type;
2875 gvop->op_ppaddr = PL_ppaddr[type];
2876 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
2877 gvop->op_next = (OP*)gvop;
2878 gvop->op_flags = flags;
2879 if (PL_opargs[type] & OA_RETSCALAR)
2881 if (PL_opargs[type] & OA_TARGET)
2882 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
2883 return CHECKOP(type, gvop);
2887 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
2890 NewOp(1101, pvop, 1, PVOP);
2891 pvop->op_type = type;
2892 pvop->op_ppaddr = PL_ppaddr[type];
2894 pvop->op_next = (OP*)pvop;
2895 pvop->op_flags = flags;
2896 if (PL_opargs[type] & OA_RETSCALAR)
2898 if (PL_opargs[type] & OA_TARGET)
2899 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2900 return CHECKOP(type, pvop);
2904 Perl_package(pTHX_ OP *o)
2909 save_hptr(&PL_curstash);
2910 save_item(PL_curstname);
2915 name = SvPV(sv, len);
2916 PL_curstash = gv_stashpvn(name,len,TRUE);
2917 sv_setpvn(PL_curstname, name, len);
2921 sv_setpv(PL_curstname,"<none>");
2922 PL_curstash = Nullhv;
2924 PL_hints |= HINT_BLOCK_SCOPE;
2925 PL_copline = NOLINE;
2930 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg)
2939 if (id->op_type != OP_CONST)
2940 Perl_croak(aTHX_ "Module name must be constant");
2944 if(version != Nullop) {
2945 SV *vesv = ((SVOP*)version)->op_sv;
2947 if (arg == Nullop && !SvNIOK(vesv)) {
2953 if (version->op_type != OP_CONST || !SvNIOK(vesv))
2954 Perl_croak(aTHX_ "Version number must be constant number");
2956 /* Make copy of id so we don't free it twice */
2957 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2959 /* Fake up a method call to VERSION */
2960 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2961 append_elem(OP_LIST,
2962 prepend_elem(OP_LIST, pack, list(version)),
2963 newSVOP(OP_METHOD_NAMED, 0,
2964 newSVpvn("VERSION", 7))));
2968 /* Fake up an import/unimport */
2969 if (arg && arg->op_type == OP_STUB)
2970 imop = arg; /* no import on explicit () */
2971 else if(SvNIOK(((SVOP*)id)->op_sv)) {
2972 imop = Nullop; /* use 5.0; */
2975 /* Make copy of id so we don't free it twice */
2976 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2977 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2978 append_elem(OP_LIST,
2979 prepend_elem(OP_LIST, pack, list(arg)),
2980 newSVOP(OP_METHOD_NAMED, 0,
2981 aver ? newSVpvn("import", 6)
2982 : newSVpvn("unimport", 8))));
2985 /* Fake up a require, handle override, if any */
2986 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
2987 if (!(gv && GvIMPORTED_CV(gv)))
2988 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
2990 if (gv && GvIMPORTED_CV(gv)) {
2991 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
2992 append_elem(OP_LIST, id,
2993 scalar(newUNOP(OP_RV2CV, 0,
2998 rqop = newUNOP(OP_REQUIRE, 0, id);
3001 /* Fake up the BEGIN {}, which does its thing immediately. */
3003 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
3006 append_elem(OP_LINESEQ,
3007 append_elem(OP_LINESEQ,
3008 newSTATEOP(0, Nullch, rqop),
3009 newSTATEOP(0, Nullch, veop)),
3010 newSTATEOP(0, Nullch, imop) ));
3012 PL_hints |= HINT_BLOCK_SCOPE;
3013 PL_copline = NOLINE;
3018 Perl_dofile(pTHX_ OP *term)
3023 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
3024 if (!(gv && GvIMPORTED_CV(gv)))
3025 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
3027 if (gv && GvIMPORTED_CV(gv)) {
3028 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3029 append_elem(OP_LIST, term,
3030 scalar(newUNOP(OP_RV2CV, 0,
3035 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3041 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
3043 return newBINOP(OP_LSLICE, flags,
3044 list(force_list(subscript)),
3045 list(force_list(listval)) );
3049 S_list_assignment(pTHX_ register OP *o)
3054 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3055 o = cUNOPo->op_first;
3057 if (o->op_type == OP_COND_EXPR) {
3058 I32 t = list_assignment(cLOGOPo->op_first->op_sibling);
3059 I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
3064 yyerror("Assignment to both a list and a scalar");
3068 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3069 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3070 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
3073 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
3076 if (o->op_type == OP_RV2SV)
3083 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
3088 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
3089 return newLOGOP(optype, 0,
3090 mod(scalar(left), optype),
3091 newUNOP(OP_SASSIGN, 0, scalar(right)));
3094 return newBINOP(optype, OPf_STACKED,
3095 mod(scalar(left), optype), scalar(right));
3099 if (list_assignment(left)) {
3102 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3103 left = mod(left, OP_AASSIGN);
3111 o = newBINOP(OP_AASSIGN, flags,
3112 list(force_list(right)),
3113 list(force_list(left)) );
3114 o->op_private = 0 | (flags >> 8);
3115 if (!(left->op_private & OPpLVAL_INTRO)) {
3119 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3120 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3121 if (curop->op_type == OP_GV) {
3122 GV *gv = ((GVOP*)curop)->op_gv;
3123 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3125 SvCUR(gv) = PL_generation;
3127 else if (curop->op_type == OP_PADSV ||
3128 curop->op_type == OP_PADAV ||
3129 curop->op_type == OP_PADHV ||
3130 curop->op_type == OP_PADANY) {
3131 SV **svp = AvARRAY(PL_comppad_name);
3132 SV *sv = svp[curop->op_targ];
3133 if (SvCUR(sv) == PL_generation)
3135 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
3137 else if (curop->op_type == OP_RV2CV)
3139 else if (curop->op_type == OP_RV2SV ||
3140 curop->op_type == OP_RV2AV ||
3141 curop->op_type == OP_RV2HV ||
3142 curop->op_type == OP_RV2GV) {
3143 if (lastop->op_type != OP_GV) /* funny deref? */
3146 else if (curop->op_type == OP_PUSHRE) {
3147 if (((PMOP*)curop)->op_pmreplroot) {
3148 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3149 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3151 SvCUR(gv) = PL_generation;
3160 o->op_private = OPpASSIGN_COMMON;
3162 if (right && right->op_type == OP_SPLIT) {
3164 if ((tmpop = ((LISTOP*)right)->op_first) &&
3165 tmpop->op_type == OP_PUSHRE)
3167 PMOP *pm = (PMOP*)tmpop;
3168 if (left->op_type == OP_RV2AV &&
3169 !(left->op_private & OPpLVAL_INTRO) &&
3170 !(o->op_private & OPpASSIGN_COMMON) )
3172 tmpop = ((UNOP*)left)->op_first;
3173 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
3174 pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
3175 pm->op_pmflags |= PMf_ONCE;
3176 tmpop = cUNOPo->op_first; /* to list (nulled) */
3177 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3178 tmpop->op_sibling = Nullop; /* don't free split */
3179 right->op_next = tmpop->op_next; /* fix starting loc */
3180 op_free(o); /* blow off assign */
3181 right->op_flags &= ~OPf_WANT;
3182 /* "I don't know and I don't care." */
3187 if (PL_modcount < 10000 &&
3188 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3190 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3192 sv_setiv(sv, PL_modcount+1);
3200 right = newOP(OP_UNDEF, 0);
3201 if (right->op_type == OP_READLINE) {
3202 right->op_flags |= OPf_STACKED;
3203 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3206 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3207 o = newBINOP(OP_SASSIGN, flags,
3208 scalar(right), mod(scalar(left), OP_SASSIGN) );
3220 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3223 U32 seq = intro_my();
3226 NewOp(1101, cop, 1, COP);
3227 if (PERLDB_LINE && PL_curcop->cop_line && PL_curstash != PL_debstash) {
3228 cop->op_type = OP_DBSTATE;
3229 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3232 cop->op_type = OP_NEXTSTATE;
3233 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3235 cop->op_flags = flags;
3236 cop->op_private = (PL_hints & HINT_UTF8);
3238 cop->op_private |= NATIVE_HINTS;
3240 PL_compiling.op_private = cop->op_private;
3241 cop->op_next = (OP*)cop;
3244 cop->cop_label = label;
3245 PL_hints |= HINT_BLOCK_SCOPE;
3248 cop->cop_arybase = PL_curcop->cop_arybase;
3249 if (specialWARN(PL_curcop->cop_warnings))
3250 cop->cop_warnings = PL_curcop->cop_warnings ;
3252 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3255 if (PL_copline == NOLINE)
3256 cop->cop_line = PL_curcop->cop_line;
3258 cop->cop_line = PL_copline;
3259 PL_copline = NOLINE;
3261 cop->cop_filegv = (GV*)SvREFCNT_inc(PL_curcop->cop_filegv);
3262 cop->cop_stash = PL_curstash;
3264 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3265 SV **svp = av_fetch(GvAV(PL_curcop->cop_filegv),(I32)cop->cop_line, FALSE);
3266 if (svp && *svp != &PL_sv_undef && !SvIOK(*svp)) {
3267 (void)SvIOK_on(*svp);
3269 SvSTASH(*svp) = (HV*)cop;
3273 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3276 /* "Introduce" my variables to visible status. */
3284 if (! PL_min_intro_pending)
3285 return PL_cop_seqmax;
3287 svp = AvARRAY(PL_comppad_name);
3288 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
3289 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
3290 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
3291 SvNVX(sv) = (NV)PL_cop_seqmax;
3294 PL_min_intro_pending = 0;
3295 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
3296 return PL_cop_seqmax++;
3300 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
3302 return new_logop(type, flags, &first, &other);
3306 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
3311 OP *first = *firstp;
3312 OP *other = *otherp;
3314 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3315 return newBINOP(type, flags, scalar(first), scalar(other));
3317 scalarboolean(first);
3318 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3319 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3320 if (type == OP_AND || type == OP_OR) {
3326 first = *firstp = cUNOPo->op_first;
3328 first->op_next = o->op_next;
3329 cUNOPo->op_first = Nullop;
3333 if (first->op_type == OP_CONST) {
3334 if (ckWARN(WARN_PRECEDENCE) && (first->op_private & OPpCONST_BARE))
3335 Perl_warner(aTHX_ WARN_PRECEDENCE, "Probable precedence problem on %s",
3337 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3348 else if (first->op_type == OP_WANTARRAY) {
3354 else if (ckWARN(WARN_UNSAFE) && (first->op_flags & OPf_KIDS)) {
3355 OP *k1 = ((UNOP*)first)->op_first;
3356 OP *k2 = k1->op_sibling;
3358 switch (first->op_type)
3361 if (k2 && k2->op_type == OP_READLINE
3362 && (k2->op_flags & OPf_STACKED)
3363 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3365 warnop = k2->op_type;
3370 if (k1->op_type == OP_READDIR
3371 || k1->op_type == OP_GLOB
3372 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3373 || k1->op_type == OP_EACH)
3375 warnop = ((k1->op_type == OP_NULL)
3376 ? k1->op_targ : k1->op_type);
3381 line_t oldline = PL_curcop->cop_line;
3382 PL_curcop->cop_line = PL_copline;
3383 Perl_warner(aTHX_ WARN_UNSAFE,
3384 "Value of %s%s can be \"0\"; test with defined()",
3386 ((warnop == OP_READLINE || warnop == OP_GLOB)
3387 ? " construct" : "() operator"));
3388 PL_curcop->cop_line = oldline;
3395 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3396 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3398 NewOp(1101, logop, 1, LOGOP);
3400 logop->op_type = type;
3401 logop->op_ppaddr = PL_ppaddr[type];
3402 logop->op_first = first;
3403 logop->op_flags = flags | OPf_KIDS;
3404 logop->op_other = LINKLIST(other);
3405 logop->op_private = 1 | (flags >> 8);
3407 /* establish postfix order */
3408 logop->op_next = LINKLIST(first);
3409 first->op_next = (OP*)logop;
3410 first->op_sibling = other;
3412 o = newUNOP(OP_NULL, 0, (OP*)logop);
3419 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
3427 return newLOGOP(OP_AND, 0, first, trueop);
3429 return newLOGOP(OP_OR, 0, first, falseop);
3431 scalarboolean(first);
3432 if (first->op_type == OP_CONST) {
3433 if (SvTRUE(((SVOP*)first)->op_sv)) {
3444 else if (first->op_type == OP_WANTARRAY) {
3448 NewOp(1101, logop, 1, LOGOP);
3449 logop->op_type = OP_COND_EXPR;
3450 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3451 logop->op_first = first;
3452 logop->op_flags = flags | OPf_KIDS;
3453 logop->op_private = 1 | (flags >> 8);
3454 logop->op_other = LINKLIST(trueop);
3455 logop->op_next = LINKLIST(falseop);
3458 /* establish postfix order */
3459 start = LINKLIST(first);
3460 first->op_next = (OP*)logop;
3462 first->op_sibling = trueop;
3463 trueop->op_sibling = falseop;
3464 o = newUNOP(OP_NULL, 0, (OP*)logop);
3466 trueop->op_next = falseop->op_next = o;
3473 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
3482 NewOp(1101, range, 1, LOGOP);
3484 range->op_type = OP_RANGE;
3485 range->op_ppaddr = PL_ppaddr[OP_RANGE];
3486 range->op_first = left;
3487 range->op_flags = OPf_KIDS;
3488 leftstart = LINKLIST(left);
3489 range->op_other = LINKLIST(right);
3490 range->op_private = 1 | (flags >> 8);
3492 left->op_sibling = right;
3494 range->op_next = (OP*)range;
3495 flip = newUNOP(OP_FLIP, flags, (OP*)range);
3496 flop = newUNOP(OP_FLOP, 0, flip);
3497 o = newUNOP(OP_NULL, 0, flop);
3499 range->op_next = leftstart;
3501 left->op_next = flip;
3502 right->op_next = flop;
3504 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3505 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
3506 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3507 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3509 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3510 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3513 if (!flip->op_private || !flop->op_private)
3514 linklist(o); /* blow off optimizer unless constant */
3520 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
3525 int once = block && block->op_flags & OPf_SPECIAL &&
3526 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3529 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3530 return block; /* do {} while 0 does once */
3531 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3532 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3533 expr = newUNOP(OP_DEFINED, 0,
3534 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3535 } else if (expr->op_flags & OPf_KIDS) {
3536 OP *k1 = ((UNOP*)expr)->op_first;
3537 OP *k2 = (k1) ? k1->op_sibling : NULL;
3538 switch (expr->op_type) {
3540 if (k2 && k2->op_type == OP_READLINE
3541 && (k2->op_flags & OPf_STACKED)
3542 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3543 expr = newUNOP(OP_DEFINED, 0, expr);
3547 if (k1->op_type == OP_READDIR
3548 || k1->op_type == OP_GLOB
3549 || (k1->op_type == OP_NULL && k1->op_targ == OP_NULL)
3550 || k1->op_type == OP_EACH)
3551 expr = newUNOP(OP_DEFINED, 0, expr);
3557 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3558 o = new_logop(OP_AND, 0, &expr, &listop);
3561 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3563 if (once && o != listop)
3564 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3567 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3569 o->op_flags |= flags;
3571 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3576 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
3585 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3586 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
3587 expr = newUNOP(OP_DEFINED, 0,
3588 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3589 } else if (expr && (expr->op_flags & OPf_KIDS)) {
3590 OP *k1 = ((UNOP*)expr)->op_first;
3591 OP *k2 = (k1) ? k1->op_sibling : NULL;
3592 switch (expr->op_type) {
3594 if (k2 && k2->op_type == OP_READLINE
3595 && (k2->op_flags & OPf_STACKED)
3596 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3597 expr = newUNOP(OP_DEFINED, 0, expr);
3601 if (k1->op_type == OP_READDIR
3602 || k1->op_type == OP_GLOB
3603 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3604 || k1->op_type == OP_EACH)
3605 expr = newUNOP(OP_DEFINED, 0, expr);
3611 block = newOP(OP_NULL, 0);
3614 next = LINKLIST(cont);
3616 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
3617 if ((line_t)whileline != NOLINE) {
3618 PL_copline = whileline;
3619 cont = append_elem(OP_LINESEQ, cont,
3620 newSTATEOP(0, Nullch, Nullop));
3624 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3625 redo = LINKLIST(listop);
3628 PL_copline = whileline;
3630 o = new_logop(OP_AND, 0, &expr, &listop);
3631 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3632 op_free(expr); /* oops, it's a while (0) */
3634 return Nullop; /* listop already freed by new_logop */
3637 ((LISTOP*)listop)->op_last->op_next = condop =
3638 (o == listop ? redo : LINKLIST(o));
3646 NewOp(1101,loop,1,LOOP);
3647 loop->op_type = OP_ENTERLOOP;
3648 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3649 loop->op_private = 0;
3650 loop->op_next = (OP*)loop;
3653 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3655 loop->op_redoop = redo;
3656 loop->op_lastop = o;
3659 loop->op_nextop = next;
3661 loop->op_nextop = o;
3663 o->op_flags |= flags;
3664 o->op_private |= (flags >> 8);
3669 Perl_newFOROP(pTHX_ I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
3678 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3679 sv->op_type = OP_RV2GV;
3680 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
3682 else if (sv->op_type == OP_PADSV) { /* private variable */
3683 padoff = sv->op_targ;
3687 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3688 padoff = sv->op_targ;
3689 iterflags |= OPf_SPECIAL;
3694 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
3698 padoff = find_threadsv("_");
3699 iterflags |= OPf_SPECIAL;
3701 sv = newGVOP(OP_GV, 0, PL_defgv);
3704 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3705 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3706 iterflags |= OPf_STACKED;
3708 else if (expr->op_type == OP_NULL &&
3709 (expr->op_flags & OPf_KIDS) &&
3710 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3712 /* Basically turn for($x..$y) into the same as for($x,$y), but we
3713 * set the STACKED flag to indicate that these values are to be
3714 * treated as min/max values by 'pp_iterinit'.
3716 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3717 LOGOP* range = (LOGOP*) flip->op_first;
3718 OP* left = range->op_first;
3719 OP* right = left->op_sibling;
3722 range->op_flags &= ~OPf_KIDS;
3723 range->op_first = Nullop;
3725 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3726 listop->op_first->op_next = range->op_next;
3727 left->op_next = range->op_other;
3728 right->op_next = (OP*)listop;
3729 listop->op_next = listop->op_first;
3732 expr = (OP*)(listop);
3734 iterflags |= OPf_STACKED;
3737 expr = mod(force_list(expr), OP_GREPSTART);
3741 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
3742 append_elem(OP_LIST, expr, scalar(sv))));
3743 assert(!loop->op_next);
3744 #ifdef PL_OP_SLAB_ALLOC
3745 NewOp(1234,tmp,1,LOOP);
3746 Copy(loop,tmp,1,LOOP);
3749 Renew(loop, 1, LOOP);
3751 loop->op_targ = padoff;
3752 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
3753 PL_copline = forline;
3754 return newSTATEOP(0, label, wop);
3758 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
3764 if (type != OP_GOTO || label->op_type == OP_CONST) {
3765 /* "last()" means "last" */
3766 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
3767 o = newOP(type, OPf_SPECIAL);
3769 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
3770 ? SvPVx(((SVOP*)label)->op_sv, n_a)
3776 if (label->op_type == OP_ENTERSUB)
3777 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
3778 o = newUNOP(type, OPf_STACKED, label);
3780 PL_hints |= HINT_BLOCK_SCOPE;
3785 Perl_cv_undef(pTHX_ CV *cv)
3790 MUTEX_DESTROY(CvMUTEXP(cv));
3791 Safefree(CvMUTEXP(cv));
3794 #endif /* USE_THREADS */
3796 if (!CvXSUB(cv) && CvROOT(cv)) {
3798 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
3799 Perl_croak(aTHX_ "Can't undef active subroutine");
3802 Perl_croak(aTHX_ "Can't undef active subroutine");
3803 #endif /* USE_THREADS */
3806 SAVESPTR(PL_curpad);
3810 op_free(CvROOT(cv));
3811 CvROOT(cv) = Nullop;
3814 SvPOK_off((SV*)cv); /* forget prototype */
3816 SvREFCNT_dec(CvGV(cv));
3818 SvREFCNT_dec(CvOUTSIDE(cv));
3819 CvOUTSIDE(cv) = Nullcv;
3820 if (CvPADLIST(cv)) {
3821 /* may be during global destruction */
3822 if (SvREFCNT(CvPADLIST(cv))) {
3823 I32 i = AvFILLp(CvPADLIST(cv));
3825 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
3826 SV* sv = svp ? *svp : Nullsv;
3829 if (sv == (SV*)PL_comppad_name)
3830 PL_comppad_name = Nullav;
3831 else if (sv == (SV*)PL_comppad) {
3832 PL_comppad = Nullav;
3833 PL_curpad = Null(SV**);
3837 SvREFCNT_dec((SV*)CvPADLIST(cv));
3839 CvPADLIST(cv) = Nullav;
3843 #ifdef DEBUG_CLOSURES
3847 CV *outside = CvOUTSIDE(cv);
3848 AV* padlist = CvPADLIST(cv);
3855 PerlIO_printf(Perl_debug_log,
3856 "\tCV=0x%"UVxf" (%s), OUTSIDE=0x%"UVxf" (%s)\n",
3858 (CvANON(cv) ? "ANON"
3859 : (cv == PL_main_cv) ? "MAIN"
3860 : CvUNIQUE(cv) ? "UNIQUE"
3861 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
3864 : CvANON(outside) ? "ANON"
3865 : (outside == PL_main_cv) ? "MAIN"
3866 : CvUNIQUE(outside) ? "UNIQUE"
3867 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
3872 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
3873 pad = (AV*)*av_fetch(padlist, 1, FALSE);
3874 pname = AvARRAY(pad_name);
3875 ppad = AvARRAY(pad);
3877 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
3878 if (SvPOK(pname[ix]))
3879 PerlIO_printf(Perl_debug_log,
3880 "\t%4d. 0x%"UVxf" (%s\"%s\" %"IVdf"-%"IVdf")\n",
3881 ix, PTR2UV(ppad[ix]),
3882 SvFAKE(pname[ix]) ? "FAKE " : "",
3884 (IV)I_32(SvNVX(pname[ix])),
3888 #endif /* DEBUG_CLOSURES */
3891 S_cv_clone2(pTHX_ CV *proto, CV *outside)
3896 AV* protopadlist = CvPADLIST(proto);
3897 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
3898 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
3899 SV** pname = AvARRAY(protopad_name);
3900 SV** ppad = AvARRAY(protopad);
3901 I32 fname = AvFILLp(protopad_name);
3902 I32 fpad = AvFILLp(protopad);
3906 assert(!CvUNIQUE(proto));
3909 SAVESPTR(PL_curpad);
3910 SAVESPTR(PL_comppad);
3911 SAVESPTR(PL_comppad_name);
3912 SAVESPTR(PL_compcv);
3914 cv = PL_compcv = (CV*)NEWSV(1104,0);
3915 sv_upgrade((SV *)cv, SvTYPE(proto));
3921 New(666, CvMUTEXP(cv), 1, perl_mutex);
3922 MUTEX_INIT(CvMUTEXP(cv));
3924 #endif /* USE_THREADS */
3925 CvFILEGV(cv) = CvFILEGV(proto);
3926 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
3927 CvSTASH(cv) = CvSTASH(proto);
3928 CvROOT(cv) = CvROOT(proto);
3929 CvSTART(cv) = CvSTART(proto);
3931 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
3934 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
3936 PL_comppad_name = newAV();
3937 for (ix = fname; ix >= 0; ix--)
3938 av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix]));
3940 PL_comppad = newAV();
3942 comppadlist = newAV();
3943 AvREAL_off(comppadlist);
3944 av_store(comppadlist, 0, (SV*)PL_comppad_name);
3945 av_store(comppadlist, 1, (SV*)PL_comppad);
3946 CvPADLIST(cv) = comppadlist;
3947 av_fill(PL_comppad, AvFILLp(protopad));
3948 PL_curpad = AvARRAY(PL_comppad);
3950 av = newAV(); /* will be @_ */
3952 av_store(PL_comppad, 0, (SV*)av);
3953 AvFLAGS(av) = AVf_REIFY;
3955 for (ix = fpad; ix > 0; ix--) {
3956 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3957 if (namesv && namesv != &PL_sv_undef) {
3958 char *name = SvPVX(namesv); /* XXX */
3959 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
3960 I32 off = pad_findlex(name, ix, SvIVX(namesv),
3961 CvOUTSIDE(cv), cxstack_ix, 0, 0);
3963 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
3965 Perl_croak(aTHX_ "panic: cv_clone: %s", name);
3967 else { /* our own lexical */
3970 /* anon code -- we'll come back for it */
3971 sv = SvREFCNT_inc(ppad[ix]);
3973 else if (*name == '@')
3975 else if (*name == '%')
3985 SV* sv = NEWSV(0,0);
3991 /* Now that vars are all in place, clone nested closures. */
3993 for (ix = fpad; ix > 0; ix--) {
3994 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3996 && namesv != &PL_sv_undef
3997 && !(SvFLAGS(namesv) & SVf_FAKE)
3998 && *SvPVX(namesv) == '&'
3999 && CvCLONE(ppad[ix]))
4001 CV *kid = cv_clone2((CV*)ppad[ix], cv);
4002 SvREFCNT_dec(ppad[ix]);
4005 PL_curpad[ix] = (SV*)kid;
4009 #ifdef DEBUG_CLOSURES
4010 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
4012 PerlIO_printf(Perl_debug_log, " from:\n");
4014 PerlIO_printf(Perl_debug_log, " to:\n");
4023 Perl_cv_clone(pTHX_ CV *proto)
4026 MUTEX_LOCK(&PL_cred_mutex); /* XXX create separate mutex */
4027 cv = cv_clone2(proto, CvOUTSIDE(proto));
4028 MUTEX_UNLOCK(&PL_cred_mutex); /* XXX create separate mutex */
4033 Perl_cv_ckproto(pTHX_ CV *cv, GV *gv, char *p)
4037 if (((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) && ckWARN_d(WARN_UNSAFE)) {
4038 SV* msg = sv_newmortal();
4042 gv_efullname3(name = sv_newmortal(), gv, Nullch);
4043 sv_setpv(msg, "Prototype mismatch:");
4045 Perl_sv_catpvf(aTHX_ msg, " sub %_", name);
4047 Perl_sv_catpvf(aTHX_ msg, " (%s)", SvPVX(cv));
4048 sv_catpv(msg, " vs ");
4050 Perl_sv_catpvf(aTHX_ msg, "(%s)", p);
4052 sv_catpv(msg, "none");
4053 Perl_warner(aTHX_ WARN_UNSAFE, "%_", msg);
4058 Perl_cv_const_sv(pTHX_ CV *cv)
4060 if (!cv || !SvPOK(cv) || SvCUR(cv))
4062 return op_const_sv(CvSTART(cv), cv);
4066 Perl_op_const_sv(pTHX_ OP *o, CV *cv)
4073 if(o->op_type == OP_LINESEQ && cLISTOPo->op_first)
4074 o = cLISTOPo->op_first->op_sibling;
4076 for (; o; o = o->op_next) {
4077 OPCODE type = o->op_type;
4079 if (sv && o->op_next == o)
4081 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
4083 if (type == OP_LEAVESUB || type == OP_RETURN)
4087 if (type == OP_CONST)
4089 else if (type == OP_PADSV && cv) {
4090 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
4091 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
4092 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
4104 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4114 Perl_croak(aTHX_ "\"my sub\" not yet implemented");
4118 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
4120 return Perl_newATTRSUB(aTHX_ floor, o, proto, Nullop, block);
4124 Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4128 char *name = o ? SvPVx(cSVOPo->op_sv, n_a) : Nullch;
4129 GV *gv = gv_fetchpv(name ? name : "__ANON__",
4130 GV_ADDMULTI | ((block || attrs) ? 0 : GV_NOINIT),
4132 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, n_a) : Nullch;
4143 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
4144 maximum a prototype before. */
4145 if (SvTYPE(gv) > SVt_NULL) {
4146 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1)
4147 && ckWARN_d(WARN_UNSAFE))
4149 Perl_warner(aTHX_ WARN_UNSAFE, "Runaway prototype");
4151 cv_ckproto((CV*)gv, NULL, ps);
4154 sv_setpv((SV*)gv, ps);
4156 sv_setiv((SV*)gv, -1);
4157 SvREFCNT_dec(PL_compcv);
4158 cv = PL_compcv = NULL;
4159 PL_sub_generation++;
4163 if (!name || GvCVGEN(gv))
4165 else if (cv = GvCV(gv)) {
4166 cv_ckproto(cv, gv, ps);
4167 /* already defined (or promised)? */
4168 if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4170 bool const_changed = TRUE;
4171 if (!block && !attrs) {
4172 /* just a "sub foo;" when &foo is already defined */
4173 SAVEFREESV(PL_compcv);
4176 /* ahem, death to those who redefine active sort subs */
4177 if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv))
4178 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", name);
4181 if(const_sv = cv_const_sv(cv))
4182 const_changed = sv_cmp(const_sv, op_const_sv(block, Nullcv));
4183 if ((const_sv && const_changed) || ckWARN(WARN_REDEFINE)
4184 && !(CvGV(cv) && GvSTASH(CvGV(cv))
4185 && HvNAME(GvSTASH(CvGV(cv)))
4186 && strEQ(HvNAME(GvSTASH(CvGV(cv))),
4188 line_t oldline = PL_curcop->cop_line;
4189 PL_curcop->cop_line = PL_copline;
4190 Perl_warner(aTHX_ WARN_REDEFINE,
4191 const_sv ? "Constant subroutine %s redefined"
4192 : "Subroutine %s redefined", name);
4193 PL_curcop->cop_line = oldline;
4204 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>
4205 * before we clobber PL_compcv.
4209 if (CvGV(cv) && GvSTASH(CvGV(cv)) && HvNAME(GvSTASH(CvGV(cv))))
4210 stash = GvSTASH(CvGV(cv));
4211 else if (CvSTASH(cv) && HvNAME(CvSTASH(cv)))
4212 stash = CvSTASH(cv);
4214 stash = PL_curstash;
4217 /* possibly about to re-define existing subr -- ignore old cv */
4218 rcv = (SV*)PL_compcv;
4219 if (name && GvSTASH(gv) && HvNAME(GvSTASH(gv)))
4220 stash = GvSTASH(gv);
4222 stash = PL_curstash;
4224 apply_attrs(stash, rcv, attrs);
4226 if (cv) { /* must reuse cv if autoloaded */
4228 /* got here with just attrs -- work done, so bug out */
4229 SAVEFREESV(PL_compcv);
4233 CvFLAGS(cv) = CvFLAGS(PL_compcv);
4234 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
4235 CvOUTSIDE(PL_compcv) = 0;
4236 CvPADLIST(cv) = CvPADLIST(PL_compcv);
4237 CvPADLIST(PL_compcv) = 0;
4238 if (SvREFCNT(PL_compcv) > 1) /* XXX Make closures transit through stub. */
4239 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc((SV*)cv);
4240 SvREFCNT_dec(PL_compcv);
4247 PL_sub_generation++;
4250 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4251 CvFILEGV(cv) = PL_curcop->cop_filegv;
4252 CvSTASH(cv) = PL_curstash;
4255 if (!CvMUTEXP(cv)) {
4256 New(666, CvMUTEXP(cv), 1, perl_mutex);
4257 MUTEX_INIT(CvMUTEXP(cv));
4259 #endif /* USE_THREADS */
4262 sv_setpv((SV*)cv, ps);
4264 if (PL_error_count) {
4268 char *s = strrchr(name, ':');
4270 if (strEQ(s, "BEGIN")) {
4272 "BEGIN not safe after errors--compilation aborted";
4273 if (PL_in_eval & EVAL_KEEPERR)
4274 Perl_croak(aTHX_ not_safe);
4276 /* force display of errors found but not reported */
4277 sv_catpv(ERRSV, not_safe);
4278 Perl_croak(aTHX_ "%s", SvPVx(ERRSV, n_a));
4285 PL_copline = NOLINE;
4290 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad))
4291 av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv);
4294 SV **namep = AvARRAY(PL_comppad_name);
4295 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4298 if (SvIMMORTAL(PL_curpad[ix]))
4301 * The only things that a clonable function needs in its
4302 * pad are references to outer lexicals and anonymous subs.
4303 * The rest are created anew during cloning.
4305 if (!((namesv = namep[ix]) != Nullsv &&
4306 namesv != &PL_sv_undef &&
4308 *SvPVX(namesv) == '&')))
4310 SvREFCNT_dec(PL_curpad[ix]);
4311 PL_curpad[ix] = Nullsv;
4316 AV *av = newAV(); /* Will be @_ */
4318 av_store(PL_comppad, 0, (SV*)av);
4319 AvFLAGS(av) = AVf_REIFY;
4321 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4322 if (SvIMMORTAL(PL_curpad[ix]))
4324 if (!SvPADMY(PL_curpad[ix]))
4325 SvPADTMP_on(PL_curpad[ix]);
4330 CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0, scalarseq(block));
4333 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4335 CvSTART(cv) = LINKLIST(CvROOT(cv));
4336 CvROOT(cv)->op_next = 0;
4342 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4343 SV *sv = NEWSV(0,0);
4344 SV *tmpstr = sv_newmortal();
4345 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
4349 Perl_sv_setpvf(aTHX_ sv, "%_:%ld-%ld",
4350 GvSV(PL_curcop->cop_filegv),
4351 (long)PL_subline, (long)PL_curcop->cop_line);
4352 gv_efullname3(tmpstr, gv, Nullch);
4353 hv_store(GvHV(PL_DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
4354 hv = GvHVn(db_postponed);
4355 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
4356 && (cv = GvCV(db_postponed))) {
4361 call_sv((SV*)cv, G_DISCARD);
4365 if ((s = strrchr(name,':')))
4369 if (strEQ(s, "BEGIN")) {
4370 I32 oldscope = PL_scopestack_ix;
4372 SAVESPTR(PL_compiling.cop_filegv);
4373 SAVEI16(PL_compiling.cop_line);
4375 sv_setsv(PL_rs, PL_nrs);
4378 PL_beginav = newAV();
4379 DEBUG_x( dump_sub(gv) );
4380 av_push(PL_beginav, (SV *)cv);
4382 call_list(oldscope, PL_beginav);
4384 PL_curcop = &PL_compiling;
4385 PL_compiling.op_private = PL_hints;
4388 else if (strEQ(s, "END") && !PL_error_count) {
4391 av_unshift(PL_endav, 1);
4392 av_store(PL_endav, 0, (SV *)cv);
4395 else if (strEQ(s, "INIT") && !PL_error_count) {
4397 PL_initav = newAV();
4398 av_push(PL_initav, SvREFCNT_inc(cv));
4404 PL_copline = NOLINE;
4409 /* XXX unsafe for threads if eval_owner isn't held */
4411 Perl_newCONSTSUB(pTHX_ HV *stash, char *name, SV *sv)
4414 U32 oldhints = PL_hints;
4415 HV *old_cop_stash = PL_curcop->cop_stash;
4416 HV *old_curstash = PL_curstash;
4417 line_t oldline = PL_curcop->cop_line;
4418 PL_curcop->cop_line = PL_copline;
4420 PL_hints &= ~HINT_BLOCK_SCOPE;
4422 PL_curstash = PL_curcop->cop_stash = stash;
4425 start_subparse(FALSE, 0),
4426 newSVOP(OP_CONST, 0, newSVpv(name,0)),
4427 newSVOP(OP_CONST, 0, &PL_sv_no), /* SvPV(&PL_sv_no) == "" -- GMB */
4429 newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv))
4432 PL_hints = oldhints;
4433 PL_curcop->cop_stash = old_cop_stash;
4434 PL_curstash = old_curstash;
4435 PL_curcop->cop_line = oldline;
4439 Perl_newXS(pTHX_ char *name, XSUBADDR_t subaddr, char *filename)
4442 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
4445 if (cv = (name ? GvCV(gv) : Nullcv)) {
4447 /* just a cached method */
4451 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4452 /* already defined (or promised) */
4453 if (ckWARN(WARN_REDEFINE) && !(CvGV(cv) && GvSTASH(CvGV(cv))
4454 && HvNAME(GvSTASH(CvGV(cv)))
4455 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
4456 line_t oldline = PL_curcop->cop_line;
4457 if (PL_copline != NOLINE)
4458 PL_curcop->cop_line = PL_copline;
4459 Perl_warner(aTHX_ WARN_REDEFINE, "Subroutine %s redefined",name);
4460 PL_curcop->cop_line = oldline;
4467 if (cv) /* must reuse cv if autoloaded */
4470 cv = (CV*)NEWSV(1105,0);
4471 sv_upgrade((SV *)cv, SVt_PVCV);
4475 PL_sub_generation++;
4478 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4480 New(666, CvMUTEXP(cv), 1, perl_mutex);
4481 MUTEX_INIT(CvMUTEXP(cv));
4483 #endif /* USE_THREADS */
4484 CvFILEGV(cv) = gv_fetchfile(filename);
4485 CvXSUB(cv) = subaddr;
4488 char *s = strrchr(name,':');
4493 if (strEQ(s, "BEGIN")) {
4495 PL_beginav = newAV();
4496 av_push(PL_beginav, (SV *)cv);
4499 else if (strEQ(s, "END")) {
4502 av_unshift(PL_endav, 1);
4503 av_store(PL_endav, 0, (SV *)cv);
4506 else if (strEQ(s, "INIT")) {
4508 PL_initav = newAV();
4509 av_push(PL_initav, (SV *)cv);
4520 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
4530 name = SvPVx(cSVOPo->op_sv, n_a);
4533 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
4535 if (cv = GvFORM(gv)) {
4536 if (ckWARN(WARN_REDEFINE)) {
4537 line_t oldline = PL_curcop->cop_line;
4539 PL_curcop->cop_line = PL_copline;
4540 Perl_warner(aTHX_ WARN_REDEFINE, "Format %s redefined",name);
4541 PL_curcop->cop_line = oldline;
4547 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4548 CvFILEGV(cv) = PL_curcop->cop_filegv;
4550 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4551 if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix]))
4552 SvPADTMP_on(PL_curpad[ix]);
4555 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4556 CvSTART(cv) = LINKLIST(CvROOT(cv));
4557 CvROOT(cv)->op_next = 0;
4560 PL_copline = NOLINE;
4565 Perl_newANONLIST(pTHX_ OP *o)
4567 return newUNOP(OP_REFGEN, 0,
4568 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
4572 Perl_newANONHASH(pTHX_ OP *o)
4574 return newUNOP(OP_REFGEN, 0,
4575 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
4579 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
4581 return newANONATTRSUB(floor, proto, Nullop, block);
4585 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
4587 return newUNOP(OP_REFGEN, 0,
4588 newSVOP(OP_ANONCODE, 0,
4589 (SV*)newATTRSUB(floor, 0, proto, attrs, block)));
4593 Perl_oopsAV(pTHX_ OP *o)
4595 switch (o->op_type) {
4597 o->op_type = OP_PADAV;
4598 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4599 return ref(o, OP_RV2AV);
4602 o->op_type = OP_RV2AV;
4603 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4608 if (ckWARN_d(WARN_INTERNAL))
4609 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsAV");
4616 Perl_oopsHV(pTHX_ OP *o)
4620 switch (o->op_type) {
4623 o->op_type = OP_PADHV;
4624 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4625 return ref(o, OP_RV2HV);
4629 o->op_type = OP_RV2HV;
4630 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
4635 if (ckWARN_d(WARN_INTERNAL))
4636 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsHV");
4643 Perl_newAVREF(pTHX_ OP *o)
4645 if (o->op_type == OP_PADANY) {
4646 o->op_type = OP_PADAV;
4647 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4650 return newUNOP(OP_RV2AV, 0, scalar(o));
4654 Perl_newGVREF(pTHX_ I32 type, OP *o)
4656 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
4657 return newUNOP(OP_NULL, 0, o);
4658 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
4662 Perl_newHVREF(pTHX_ OP *o)
4664 if (o->op_type == OP_PADANY) {
4665 o->op_type = OP_PADHV;
4666 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4669 return newUNOP(OP_RV2HV, 0, scalar(o));
4673 Perl_oopsCV(pTHX_ OP *o)
4675 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
4681 Perl_newCVREF(pTHX_ I32 flags, OP *o)
4683 return newUNOP(OP_RV2CV, flags, scalar(o));
4687 Perl_newSVREF(pTHX_ OP *o)
4689 if (o->op_type == OP_PADANY) {
4690 o->op_type = OP_PADSV;
4691 o->op_ppaddr = PL_ppaddr[OP_PADSV];
4694 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
4695 o->op_flags |= OPpDONE_SVREF;
4698 return newUNOP(OP_RV2SV, 0, scalar(o));
4701 /* Check routines. */
4704 Perl_ck_anoncode(pTHX_ OP *o)
4709 name = NEWSV(1106,0);
4710 sv_upgrade(name, SVt_PVNV);
4711 sv_setpvn(name, "&", 1);
4714 ix = pad_alloc(o->op_type, SVs_PADMY);
4715 av_store(PL_comppad_name, ix, name);
4716 av_store(PL_comppad, ix, cSVOPo->op_sv);
4717 SvPADMY_on(cSVOPo->op_sv);
4718 cSVOPo->op_sv = Nullsv;
4719 cSVOPo->op_targ = ix;
4724 Perl_ck_bitop(pTHX_ OP *o)
4726 o->op_private = PL_hints;
4731 Perl_ck_concat(pTHX_ OP *o)
4733 if (cUNOPo->op_first->op_type == OP_CONCAT)
4734 o->op_flags |= OPf_STACKED;
4739 Perl_ck_spair(pTHX_ OP *o)
4741 if (o->op_flags & OPf_KIDS) {
4744 OPCODE type = o->op_type;
4745 o = modkids(ck_fun(o), type);
4746 kid = cUNOPo->op_first;
4747 newop = kUNOP->op_first->op_sibling;
4749 (newop->op_sibling ||
4750 !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
4751 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
4752 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
4756 op_free(kUNOP->op_first);
4757 kUNOP->op_first = newop;
4759 o->op_ppaddr = PL_ppaddr[++o->op_type];
4764 Perl_ck_delete(pTHX_ OP *o)
4768 if (o->op_flags & OPf_KIDS) {
4769 OP *kid = cUNOPo->op_first;
4770 if (kid->op_type == OP_HSLICE)
4771 o->op_private |= OPpSLICE;
4772 else if (kid->op_type != OP_HELEM)
4773 Perl_croak(aTHX_ "%s argument is not a HASH element or slice",
4774 PL_op_desc[o->op_type]);
4781 Perl_ck_eof(pTHX_ OP *o)
4783 I32 type = o->op_type;
4785 if (o->op_flags & OPf_KIDS) {
4786 if (cLISTOPo->op_first->op_type == OP_STUB) {
4788 o = newUNOP(type, OPf_SPECIAL,
4789 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
4797 Perl_ck_eval(pTHX_ OP *o)
4799 PL_hints |= HINT_BLOCK_SCOPE;
4800 if (o->op_flags & OPf_KIDS) {
4801 SVOP *kid = (SVOP*)cUNOPo->op_first;
4804 o->op_flags &= ~OPf_KIDS;
4807 else if (kid->op_type == OP_LINESEQ) {
4810 kid->op_next = o->op_next;
4811 cUNOPo->op_first = 0;
4814 NewOp(1101, enter, 1, LOGOP);
4815 enter->op_type = OP_ENTERTRY;
4816 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
4817 enter->op_private = 0;
4819 /* establish postfix order */
4820 enter->op_next = (OP*)enter;
4822 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
4823 o->op_type = OP_LEAVETRY;
4824 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
4825 enter->op_other = o;
4833 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
4835 o->op_targ = (PADOFFSET)PL_hints;
4840 Perl_ck_exec(pTHX_ OP *o)
4843 if (o->op_flags & OPf_STACKED) {
4845 kid = cUNOPo->op_first->op_sibling;
4846 if (kid->op_type == OP_RV2GV)
4855 Perl_ck_exists(pTHX_ OP *o)
4858 if (o->op_flags & OPf_KIDS) {
4859 OP *kid = cUNOPo->op_first;
4860 if (kid->op_type != OP_HELEM)
4861 Perl_croak(aTHX_ "%s argument is not a HASH element", PL_op_desc[o->op_type]);
4869 Perl_ck_gvconst(pTHX_ register OP *o)
4871 o = fold_constants(o);
4872 if (o->op_type == OP_CONST)
4879 Perl_ck_rvconst(pTHX_ register OP *o)
4882 SVOP *kid = (SVOP*)cUNOPo->op_first;
4884 o->op_private |= (PL_hints & HINT_STRICT_REFS);
4885 if (kid->op_type == OP_CONST) {
4889 SV *kidsv = kid->op_sv;
4892 /* Is it a constant from cv_const_sv()? */
4893 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
4894 SV *rsv = SvRV(kidsv);
4895 int svtype = SvTYPE(rsv);
4896 char *badtype = Nullch;
4898 switch (o->op_type) {
4900 if (svtype > SVt_PVMG)
4901 badtype = "a SCALAR";
4904 if (svtype != SVt_PVAV)
4905 badtype = "an ARRAY";
4908 if (svtype != SVt_PVHV) {
4909 if (svtype == SVt_PVAV) { /* pseudohash? */
4910 SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
4911 if (ksv && SvROK(*ksv)
4912 && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
4921 if (svtype != SVt_PVCV)
4926 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
4929 name = SvPV(kidsv, n_a);
4930 if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
4931 char *badthing = Nullch;
4932 switch (o->op_type) {
4934 badthing = "a SCALAR";
4937 badthing = "an ARRAY";
4940 badthing = "a HASH";
4945 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
4949 * This is a little tricky. We only want to add the symbol if we
4950 * didn't add it in the lexer. Otherwise we get duplicate strict
4951 * warnings. But if we didn't add it in the lexer, we must at
4952 * least pretend like we wanted to add it even if it existed before,
4953 * or we get possible typo warnings. OPpCONST_ENTERED says
4954 * whether the lexer already added THIS instance of this symbol.
4956 iscv = (o->op_type == OP_RV2CV) * 2;
4958 gv = gv_fetchpv(name,
4959 iscv | !(kid->op_private & OPpCONST_ENTERED),
4962 : o->op_type == OP_RV2SV
4964 : o->op_type == OP_RV2AV
4966 : o->op_type == OP_RV2HV
4969 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
4971 kid->op_type = OP_GV;
4972 SvREFCNT_dec(kid->op_sv);
4973 kid->op_sv = SvREFCNT_inc(gv);
4974 kid->op_ppaddr = PL_ppaddr[OP_GV];
4981 Perl_ck_ftst(pTHX_ OP *o)
4984 I32 type = o->op_type;
4986 if (o->op_flags & OPf_REF)
4989 if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
4990 SVOP *kid = (SVOP*)cUNOPo->op_first;
4992 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4994 OP *newop = newGVOP(type, OPf_REF,
4995 gv_fetchpv(SvPVx(kid->op_sv, n_a), TRUE, SVt_PVIO));
5002 if (type == OP_FTTTY)
5003 return newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
5006 return newUNOP(type, 0, newDEFSVOP());
5012 Perl_ck_fun(pTHX_ OP *o)
5019 int type = o->op_type;
5020 register I32 oa = PL_opargs[type] >> OASHIFT;
5022 if (o->op_flags & OPf_STACKED) {
5023 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
5026 return no_fh_allowed(o);
5029 if (o->op_flags & OPf_KIDS) {
5031 tokid = &cLISTOPo->op_first;
5032 kid = cLISTOPo->op_first;
5033 if (kid->op_type == OP_PUSHMARK ||
5034 kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
5036 tokid = &kid->op_sibling;
5037 kid = kid->op_sibling;
5039 if (!kid && PL_opargs[type] & OA_DEFGV)
5040 *tokid = kid = newDEFSVOP();
5044 sibl = kid->op_sibling;
5047 /* list seen where single (scalar) arg expected? */
5048 if (numargs == 1 && !(oa >> 4)
5049 && kid->op_type == OP_LIST && type != OP_SCALAR)
5051 return too_many_arguments(o,PL_op_desc[type]);
5064 if (kid->op_type == OP_CONST &&
5065 (kid->op_private & OPpCONST_BARE))
5067 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5068 OP *newop = newAVREF(newGVOP(OP_GV, 0,
5069 gv_fetchpv(name, TRUE, SVt_PVAV) ));
5070 if (ckWARN(WARN_SYNTAX))
5071 Perl_warner(aTHX_ WARN_SYNTAX,
5072 "Array @%s missing the @ in argument %"IVdf" of %s()",
5073 name, (IV)numargs, PL_op_desc[type]);
5076 kid->op_sibling = sibl;
5079 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
5080 bad_type(numargs, "array", PL_op_desc[type], kid);
5084 if (kid->op_type == OP_CONST &&
5085 (kid->op_private & OPpCONST_BARE))
5087 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5088 OP *newop = newHVREF(newGVOP(OP_GV, 0,
5089 gv_fetchpv(name, TRUE, SVt_PVHV) ));
5090 if (ckWARN(WARN_SYNTAX))
5091 Perl_warner(aTHX_ WARN_SYNTAX,
5092 "Hash %%%s missing the %% in argument %"IVdf" of %s()",
5093 name, (IV)numargs, PL_op_desc[type]);
5096 kid->op_sibling = sibl;
5099 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
5100 bad_type(numargs, "hash", PL_op_desc[type], kid);
5105 OP *newop = newUNOP(OP_NULL, 0, kid);
5106 kid->op_sibling = 0;
5108 newop->op_next = newop;
5110 kid->op_sibling = sibl;
5115 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
5116 if (kid->op_type == OP_CONST &&
5117 (kid->op_private & OPpCONST_BARE))
5119 OP *newop = newGVOP(OP_GV, 0,
5120 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, n_a), TRUE,
5125 else if (kid->op_type == OP_READLINE) {
5126 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
5127 bad_type(numargs, "HANDLE", PL_op_desc[o->op_type], kid);
5130 I32 flags = OPf_SPECIAL;
5132 /* is this op a FH constructor? */
5133 if (is_handle_constructor(o,numargs)) {
5135 /* Set a flag to tell rv2gv to vivify
5136 * need to "prove" flag does not mean something
5137 * else already - NI-S 1999/05/07
5141 /* Helps with open($array[$n],...)
5142 but is too simplistic - need to do selectively
5147 kid->op_sibling = 0;
5148 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
5150 kid->op_private |= priv;
5153 kid->op_sibling = sibl;
5159 mod(scalar(kid), type);
5163 tokid = &kid->op_sibling;
5164 kid = kid->op_sibling;
5166 o->op_private |= numargs;
5168 return too_many_arguments(o,PL_op_desc[o->op_type]);
5171 else if (PL_opargs[type] & OA_DEFGV) {
5173 return newUNOP(type, 0, newDEFSVOP());
5177 while (oa & OA_OPTIONAL)
5179 if (oa && oa != OA_LIST)
5180 return too_few_arguments(o,PL_op_desc[o->op_type]);
5186 Perl_ck_glob(pTHX_ OP *o)
5190 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
5191 append_elem(OP_GLOB, o, newDEFSVOP());
5193 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
5194 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5196 #if defined(PERL_INTERNAL_GLOB) && !defined(MINIPERL_BUILD)
5197 /* XXX this can be tightened up and made more failsafe. */
5199 OP *modname = newSVOP(OP_CONST, 0, newSVpvn("File::Glob", 10));
5200 modname->op_private |= OPpCONST_BARE;
5202 utilize(1, start_subparse(FALSE, 0), Nullop, modname,
5203 newSVOP(OP_CONST, 0, newSVpvn("globally", 8)));
5204 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5207 #endif /* PERL_INTERNAL_GLOB && !MINIPERL_BUILD */
5209 if (gv && GvIMPORTED_CV(gv)) {
5210 append_elem(OP_GLOB, o,
5211 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
5212 o->op_type = OP_LIST;
5213 o->op_ppaddr = PL_ppaddr[OP_LIST];
5214 cLISTOPo->op_first->op_type = OP_PUSHMARK;
5215 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
5216 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
5217 append_elem(OP_LIST, o,
5218 scalar(newUNOP(OP_RV2CV, 0,
5219 newGVOP(OP_GV, 0, gv)))));
5220 o = newUNOP(OP_NULL, 0, ck_subr(o));
5221 o->op_targ = OP_GLOB; /* hint at what it used to be */
5224 gv = newGVgen("main");
5226 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
5232 Perl_ck_grep(pTHX_ OP *o)
5236 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
5238 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
5239 NewOp(1101, gwop, 1, LOGOP);
5241 if (o->op_flags & OPf_STACKED) {
5244 kid = cLISTOPo->op_first->op_sibling;
5245 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
5248 kid->op_next = (OP*)gwop;
5249 o->op_flags &= ~OPf_STACKED;
5251 kid = cLISTOPo->op_first->op_sibling;
5252 if (type == OP_MAPWHILE)
5259 kid = cLISTOPo->op_first->op_sibling;
5260 if (kid->op_type != OP_NULL)
5261 Perl_croak(aTHX_ "panic: ck_grep");
5262 kid = kUNOP->op_first;
5264 gwop->op_type = type;
5265 gwop->op_ppaddr = PL_ppaddr[type];
5266 gwop->op_first = listkids(o);
5267 gwop->op_flags |= OPf_KIDS;
5268 gwop->op_private = 1;
5269 gwop->op_other = LINKLIST(kid);
5270 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
5271 kid->op_next = (OP*)gwop;
5273 kid = cLISTOPo->op_first->op_sibling;
5274 if (!kid || !kid->op_sibling)
5275 return too_few_arguments(o,PL_op_desc[o->op_type]);
5276 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
5277 mod(kid, OP_GREPSTART);
5283 Perl_ck_index(pTHX_ OP *o)
5285 if (o->op_flags & OPf_KIDS) {
5286 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5288 kid = kid->op_sibling; /* get past "big" */
5289 if (kid && kid->op_type == OP_CONST)
5290 fbm_compile(((SVOP*)kid)->op_sv, 0);
5296 Perl_ck_lengthconst(pTHX_ OP *o)
5298 /* XXX length optimization goes here */
5303 Perl_ck_lfun(pTHX_ OP *o)
5305 OPCODE type = o->op_type;
5306 return modkids(ck_fun(o), type);
5310 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
5313 if ((o->op_flags & OPf_KIDS) && ckWARN(WARN_DEPRECATED)) {
5314 switch (cUNOPo->op_first->op_type) {
5316 break; /* Globals via GV can be undef */
5318 case OP_AASSIGN: /* Is this a good idea? */
5319 Perl_warner(aTHX_ WARN_DEPRECATED,
5320 "defined(@array) is deprecated");
5321 Perl_warner(aTHX_ WARN_DEPRECATED,
5322 "(Maybe you should just omit the defined()?)\n");
5325 break; /* Globals via GV can be undef */
5327 Perl_warner(aTHX_ WARN_DEPRECATED,
5328 "defined(%hash) is deprecated");
5329 Perl_warner(aTHX_ WARN_DEPRECATED,
5330 "(Maybe you should just omit the defined()?)\n");
5341 Perl_ck_rfun(pTHX_ OP *o)
5343 OPCODE type = o->op_type;
5344 return refkids(ck_fun(o), type);
5348 Perl_ck_listiob(pTHX_ OP *o)
5352 kid = cLISTOPo->op_first;
5355 kid = cLISTOPo->op_first;
5357 if (kid->op_type == OP_PUSHMARK)
5358 kid = kid->op_sibling;
5359 if (kid && o->op_flags & OPf_STACKED)
5360 kid = kid->op_sibling;
5361 else if (kid && !kid->op_sibling) { /* print HANDLE; */
5362 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
5363 o->op_flags |= OPf_STACKED; /* make it a filehandle */
5364 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
5365 cLISTOPo->op_first->op_sibling = kid;
5366 cLISTOPo->op_last = kid;
5367 kid = kid->op_sibling;
5372 append_elem(o->op_type, o, newDEFSVOP());
5378 if (PL_hints & HINT_LOCALE)
5379 o->op_private |= OPpLOCALE;
5386 Perl_ck_fun_locale(pTHX_ OP *o)
5392 if (PL_hints & HINT_LOCALE)
5393 o->op_private |= OPpLOCALE;
5400 Perl_ck_sassign(pTHX_ OP *o)
5402 OP *kid = cLISTOPo->op_first;
5403 /* has a disposable target? */
5404 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
5405 && !(kid->op_flags & OPf_STACKED))
5407 OP *kkid = kid->op_sibling;
5409 /* Can just relocate the target. */
5410 if (kkid && kkid->op_type == OP_PADSV
5411 && !(kkid->op_private & OPpLVAL_INTRO))
5413 /* Concat has problems if target is equal to right arg. */
5414 if (kid->op_type == OP_CONCAT) {
5415 if (kLISTOP->op_first->op_sibling->op_type == OP_PADSV
5416 && kLISTOP->op_first->op_sibling->op_targ == kkid->op_targ)
5419 else if (kid->op_type == OP_JOIN) {
5420 /* do_join has problems if the arguments coincide with target.
5421 In fact the second argument *can* safely coincide,
5422 but ignore=pessimize this rare occasion. */
5423 OP *arg = kLISTOP->op_first->op_sibling; /* Skip PUSHMARK */
5426 if (arg->op_type == OP_PADSV
5427 && arg->op_targ == kkid->op_targ)
5429 arg = arg->op_sibling;
5432 else if (kid->op_type == OP_QUOTEMETA) {
5433 /* quotemeta has problems if the argument coincides with target. */
5434 if (kLISTOP->op_first->op_type == OP_PADSV
5435 && kLISTOP->op_first->op_targ == kkid->op_targ)
5438 kid->op_targ = kkid->op_targ;
5439 /* Now we do not need PADSV and SASSIGN. */
5440 kid->op_sibling = o->op_sibling; /* NULL */
5441 cLISTOPo->op_first = NULL;
5444 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
5452 Perl_ck_scmp(pTHX_ OP *o)
5456 if (PL_hints & HINT_LOCALE)
5457 o->op_private |= OPpLOCALE;
5464 Perl_ck_match(pTHX_ OP *o)
5466 o->op_private |= OPpRUNTIME;
5471 Perl_ck_method(pTHX_ OP *o)
5473 OP *kid = cUNOPo->op_first;
5474 if (kid->op_type == OP_CONST) {
5475 SV* sv = kSVOP->op_sv;
5476 if (!(strchr(SvPVX(sv), ':') || strchr(SvPVX(sv), '\''))) {
5478 sv_upgrade(sv, SVt_PVIV);
5480 PERL_HASH(SvUVX(sv), SvPVX(sv), SvCUR(sv));
5481 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
5482 kSVOP->op_sv = Nullsv;
5491 Perl_ck_null(pTHX_ OP *o)
5497 Perl_ck_repeat(pTHX_ OP *o)
5499 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
5500 o->op_private |= OPpREPEAT_DOLIST;
5501 cBINOPo->op_first = force_list(cBINOPo->op_first);
5509 Perl_ck_require(pTHX_ OP *o)
5511 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
5512 SVOP *kid = (SVOP*)cUNOPo->op_first;
5514 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5516 for (s = SvPVX(kid->op_sv); *s; s++) {
5517 if (*s == ':' && s[1] == ':') {
5519 Move(s+2, s+1, strlen(s+2)+1, char);
5520 --SvCUR(kid->op_sv);
5523 sv_catpvn(kid->op_sv, ".pm", 3);
5531 Perl_ck_retarget(pTHX_ OP *o)
5533 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
5540 Perl_ck_select(pTHX_ OP *o)
5543 if (o->op_flags & OPf_KIDS) {
5544 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5545 if (kid && kid->op_sibling) {
5546 o->op_type = OP_SSELECT;
5547 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
5549 return fold_constants(o);
5553 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5554 if (kid && kid->op_type == OP_RV2GV)
5555 kid->op_private &= ~HINT_STRICT_REFS;
5560 Perl_ck_shift(pTHX_ OP *o)
5562 I32 type = o->op_type;
5564 if (!(o->op_flags & OPf_KIDS)) {
5569 if (!CvUNIQUE(PL_compcv)) {
5570 argop = newOP(OP_PADAV, OPf_REF);
5571 argop->op_targ = 0; /* PL_curpad[0] is @_ */
5574 argop = newUNOP(OP_RV2AV, 0,
5575 scalar(newGVOP(OP_GV, 0,
5576 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5579 argop = newUNOP(OP_RV2AV, 0,
5580 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(PL_compcv) ?
5581 PL_defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5582 #endif /* USE_THREADS */
5583 return newUNOP(type, 0, scalar(argop));
5585 return scalar(modkids(ck_fun(o), type));
5589 Perl_ck_sort(pTHX_ OP *o)
5593 if (PL_hints & HINT_LOCALE)
5594 o->op_private |= OPpLOCALE;
5597 if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
5599 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
5600 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5602 kid = kUNOP->op_first; /* get past null */
5604 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
5606 if (kid->op_type == OP_SCOPE) {
5610 else if (kid->op_type == OP_LEAVE) {
5611 if (o->op_type == OP_SORT) {
5612 null(kid); /* wipe out leave */
5615 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
5616 if (k->op_next == kid)
5621 kid->op_next = 0; /* just disconnect the leave */
5622 k = kLISTOP->op_first;
5626 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5627 if (o->op_type == OP_SORT)
5631 o->op_flags |= OPf_SPECIAL;
5633 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
5634 null(cLISTOPo->op_first->op_sibling);
5641 S_simplify_sort(pTHX_ OP *o)
5644 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5647 if (!(o->op_flags & OPf_STACKED))
5649 GvMULTI_on(gv_fetchpv("a", TRUE, SVt_PV));
5650 GvMULTI_on(gv_fetchpv("b", TRUE, SVt_PV));
5651 kid = kUNOP->op_first; /* get past null */
5652 if (kid->op_type != OP_SCOPE)
5654 kid = kLISTOP->op_last; /* get past scope */
5655 switch(kid->op_type) {
5663 k = kid; /* remember this node*/
5664 if (kBINOP->op_first->op_type != OP_RV2SV)
5666 kid = kBINOP->op_first; /* get past cmp */
5667 if (kUNOP->op_first->op_type != OP_GV)
5669 kid = kUNOP->op_first; /* get past rv2sv */
5670 if (GvSTASH(kGVOP->op_gv) != PL_curstash)
5672 if (strEQ(GvNAME(kGVOP->op_gv), "a"))
5674 else if(strEQ(GvNAME(kGVOP->op_gv), "b"))
5678 kid = k; /* back to cmp */
5679 if (kBINOP->op_last->op_type != OP_RV2SV)
5681 kid = kBINOP->op_last; /* down to 2nd arg */
5682 if (kUNOP->op_first->op_type != OP_GV)
5684 kid = kUNOP->op_first; /* get past rv2sv */
5685 if (GvSTASH(kGVOP->op_gv) != PL_curstash
5687 ? strNE(GvNAME(kGVOP->op_gv), "a")
5688 : strNE(GvNAME(kGVOP->op_gv), "b")))
5690 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
5692 o->op_private |= OPpSORT_REVERSE;
5693 if (k->op_type == OP_NCMP)
5694 o->op_private |= OPpSORT_NUMERIC;
5695 if (k->op_type == OP_I_NCMP)
5696 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
5697 kid = cLISTOPo->op_first->op_sibling;
5698 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
5699 op_free(kid); /* then delete it */
5700 cLISTOPo->op_children--;
5704 Perl_ck_split(pTHX_ OP *o)
5708 if (o->op_flags & OPf_STACKED)
5709 return no_fh_allowed(o);
5711 kid = cLISTOPo->op_first;
5712 if (kid->op_type != OP_NULL)
5713 Perl_croak(aTHX_ "panic: ck_split");
5714 kid = kid->op_sibling;
5715 op_free(cLISTOPo->op_first);
5716 cLISTOPo->op_first = kid;
5718 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvn(" ", 1));
5719 cLISTOPo->op_last = kid; /* There was only one element previously */
5722 if (kid->op_type != OP_MATCH) {
5723 OP *sibl = kid->op_sibling;
5724 kid->op_sibling = 0;
5725 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
5726 if (cLISTOPo->op_first == cLISTOPo->op_last)
5727 cLISTOPo->op_last = kid;
5728 cLISTOPo->op_first = kid;
5729 kid->op_sibling = sibl;
5732 kid->op_type = OP_PUSHRE;
5733 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
5736 if (!kid->op_sibling)
5737 append_elem(OP_SPLIT, o, newDEFSVOP());
5739 kid = kid->op_sibling;
5742 if (!kid->op_sibling)
5743 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
5745 kid = kid->op_sibling;
5748 if (kid->op_sibling)
5749 return too_many_arguments(o,PL_op_desc[o->op_type]);
5755 Perl_ck_join(pTHX_ OP *o)
5757 if (ckWARN(WARN_SYNTAX)) {
5758 OP *kid = cLISTOPo->op_first->op_sibling;
5759 if (kid && kid->op_type == OP_MATCH) {
5760 char *pmstr = "STRING";
5761 if (kPMOP->op_pmregexp)
5762 pmstr = kPMOP->op_pmregexp->precomp;
5763 Perl_warner(aTHX_ WARN_SYNTAX,
5764 "/%s/ should probably be written as \"%s\"",
5772 Perl_ck_subr(pTHX_ OP *o)
5775 OP *prev = ((cUNOPo->op_first->op_sibling)
5776 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
5777 OP *o2 = prev->op_sibling;
5786 o->op_private |= OPpENTERSUB_HASTARG;
5787 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
5788 if (cvop->op_type == OP_RV2CV) {
5790 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
5791 null(cvop); /* disable rv2cv */
5792 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
5793 if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) {
5794 cv = GvCVu(tmpop->op_sv);
5796 tmpop->op_private |= OPpEARLY_CV;
5797 else if (SvPOK(cv)) {
5798 namegv = CvANON(cv) ? (GV*)tmpop->op_sv : CvGV(cv);
5799 proto = SvPV((SV*)cv, n_a);
5803 else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
5804 if (o2->op_type == OP_CONST)
5805 o2->op_private &= ~OPpCONST_STRICT;
5806 else if (o2->op_type == OP_LIST) {
5807 OP *o = ((UNOP*)o2)->op_first->op_sibling;
5808 if (o && o->op_type == OP_CONST)
5809 o->op_private &= ~OPpCONST_STRICT;
5812 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5813 if (PERLDB_SUB && PL_curstash != PL_debstash)
5814 o->op_private |= OPpENTERSUB_DB;
5815 while (o2 != cvop) {
5819 return too_many_arguments(o, gv_ename(namegv));
5837 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
5838 bad_type(arg, "block", gv_ename(namegv), o2);
5841 /* '*' allows any scalar type, including bareword */
5844 if (o2->op_type == OP_RV2GV)
5845 goto wrapref; /* autoconvert GLOB -> GLOBref */
5846 else if (o2->op_type == OP_CONST)
5847 o2->op_private &= ~OPpCONST_STRICT;
5848 else if (o2->op_type == OP_ENTERSUB) {
5849 /* accidental subroutine, revert to bareword */
5850 OP *gvop = ((UNOP*)o2)->op_first;
5851 if (gvop && gvop->op_type == OP_NULL) {
5852 gvop = ((UNOP*)gvop)->op_first;
5854 for (; gvop->op_sibling; gvop = gvop->op_sibling)
5857 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
5858 (gvop = ((UNOP*)gvop)->op_first) &&
5859 gvop->op_type == OP_GV)
5861 GV *gv = (GV*)((SVOP*)gvop)->op_sv;
5862 OP *sibling = o2->op_sibling;
5863 SV *n = newSVpvn("",0);
5865 gv_fullname3(n, gv, "");
5866 if (SvCUR(n)>6 && strnEQ(SvPVX(n),"main::",6))
5867 sv_chop(n, SvPVX(n)+6);
5868 o2 = newSVOP(OP_CONST, 0, n);
5869 prev->op_sibling = o2;
5870 o2->op_sibling = sibling;
5882 if (o2->op_type != OP_RV2GV)
5883 bad_type(arg, "symbol", gv_ename(namegv), o2);
5886 if (o2->op_type != OP_RV2CV)
5887 bad_type(arg, "sub", gv_ename(namegv), o2);
5890 if (o2->op_type != OP_RV2SV
5891 && o2->op_type != OP_PADSV
5892 && o2->op_type != OP_HELEM
5893 && o2->op_type != OP_AELEM
5894 && o2->op_type != OP_THREADSV)
5896 bad_type(arg, "scalar", gv_ename(namegv), o2);
5900 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
5901 bad_type(arg, "array", gv_ename(namegv), o2);
5904 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
5905 bad_type(arg, "hash", gv_ename(namegv), o2);
5909 OP* sib = kid->op_sibling;
5910 kid->op_sibling = 0;
5911 o2 = newUNOP(OP_REFGEN, 0, kid);
5912 o2->op_sibling = sib;
5913 prev->op_sibling = o2;
5924 Perl_croak(aTHX_ "Malformed prototype for %s: %s",
5925 gv_ename(namegv), SvPV((SV*)cv, n_a));
5930 mod(o2, OP_ENTERSUB);
5932 o2 = o2->op_sibling;
5934 if (proto && !optional &&
5935 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
5936 return too_few_arguments(o, gv_ename(namegv));
5941 Perl_ck_svconst(pTHX_ OP *o)
5943 SvREADONLY_on(cSVOPo->op_sv);
5948 Perl_ck_trunc(pTHX_ OP *o)
5950 if (o->op_flags & OPf_KIDS) {
5951 SVOP *kid = (SVOP*)cUNOPo->op_first;
5953 if (kid->op_type == OP_NULL)
5954 kid = (SVOP*)kid->op_sibling;
5955 if (kid && kid->op_type == OP_CONST &&
5956 (kid->op_private & OPpCONST_BARE))
5958 o->op_flags |= OPf_SPECIAL;
5959 kid->op_private &= ~OPpCONST_STRICT;
5965 /* A peephole optimizer. We visit the ops in the order they're to execute. */
5968 Perl_peep(pTHX_ register OP *o)
5971 register OP* oldop = 0;
5973 OP *last_composite = Nullop;
5975 if (!o || o->op_seq)
5979 SAVESPTR(PL_curcop);
5980 for (; o; o = o->op_next) {
5986 switch (o->op_type) {
5990 PL_curcop = ((COP*)o); /* for warnings */
5991 o->op_seq = PL_op_seqmax++;
5992 last_composite = Nullop;
5996 if (cSVOPo->op_private & OPpCONST_STRICT)
5997 no_bareword_allowed(o);
6003 if ( o->op_next && o->op_next->op_type == OP_STRINGIFY
6004 && !(o->op_next->op_private & OPpTARGET_MY) )
6006 o->op_seq = PL_op_seqmax++;
6011 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
6012 if (o->op_next->op_private & OPpTARGET_MY) {
6013 if ((o->op_flags & OPf_STACKED) /* chained concats */
6014 || (o->op_type == OP_CONCAT
6015 /* Concat has problems if target is equal to right arg. */
6016 && (((LISTOP*)o)->op_first->op_sibling->op_type
6018 && (((LISTOP*)o)->op_first->op_sibling->op_targ
6019 == o->op_next->op_targ))) {
6020 goto ignore_optimization;
6023 o->op_targ = o->op_next->op_targ;
6024 o->op_private |= OPpTARGET_MY;
6029 ignore_optimization:
6030 o->op_seq = PL_op_seqmax++;
6033 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
6034 o->op_seq = PL_op_seqmax++;
6035 break; /* Scalar stub must produce undef. List stub is noop */
6039 if (o->op_targ == OP_NEXTSTATE
6040 || o->op_targ == OP_DBSTATE
6041 || o->op_targ == OP_SETSTATE)
6043 PL_curcop = ((COP*)o);
6050 if (oldop && o->op_next) {
6051 oldop->op_next = o->op_next;
6054 o->op_seq = PL_op_seqmax++;
6058 if (o->op_next->op_type == OP_RV2SV) {
6059 if (!(o->op_next->op_private & OPpDEREF)) {
6061 o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
6062 o->op_next = o->op_next->op_next;
6063 o->op_type = OP_GVSV;
6064 o->op_ppaddr = PL_ppaddr[OP_GVSV];
6067 else if (o->op_next->op_type == OP_RV2AV) {
6068 OP* pop = o->op_next->op_next;
6070 if (pop->op_type == OP_CONST &&
6071 (PL_op = pop->op_next) &&
6072 pop->op_next->op_type == OP_AELEM &&
6073 !(pop->op_next->op_private &
6074 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
6075 (i = SvIV(((SVOP*)pop)->op_sv) - PL_compiling.cop_arybase)
6082 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
6083 o->op_next = pop->op_next->op_next;
6084 o->op_type = OP_AELEMFAST;
6085 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
6086 o->op_private = (U8)i;
6087 GvAVn(((GVOP*)o)->op_gv);
6090 else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_UNSAFE)) {
6091 GV *gv = cGVOPo->op_gv;
6092 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX(GvCV(gv))) {
6093 /* XXX could check prototype here instead of just carping */
6094 SV *sv = sv_newmortal();
6095 gv_efullname3(sv, gv, Nullch);
6096 Perl_warner(aTHX_ WARN_UNSAFE,
6097 "%s() called too early to check prototype",
6102 o->op_seq = PL_op_seqmax++;
6113 o->op_seq = PL_op_seqmax++;
6114 while (cLOGOP->op_other->op_type == OP_NULL)
6115 cLOGOP->op_other = cLOGOP->op_other->op_next;
6116 peep(cLOGOP->op_other);
6120 o->op_seq = PL_op_seqmax++;
6121 peep(cLOOP->op_redoop);
6122 peep(cLOOP->op_nextop);
6123 peep(cLOOP->op_lastop);
6129 o->op_seq = PL_op_seqmax++;
6130 peep(cPMOP->op_pmreplstart);
6134 o->op_seq = PL_op_seqmax++;
6135 if (ckWARN(WARN_SYNTAX) && o->op_next
6136 && o->op_next->op_type == OP_NEXTSTATE) {
6137 if (o->op_next->op_sibling &&
6138 o->op_next->op_sibling->op_type != OP_EXIT &&
6139 o->op_next->op_sibling->op_type != OP_WARN &&
6140 o->op_next->op_sibling->op_type != OP_DIE) {
6141 line_t oldline = PL_curcop->cop_line;
6143 PL_curcop->cop_line = ((COP*)o->op_next)->cop_line;
6144 Perl_warner(aTHX_ WARN_SYNTAX, "Statement unlikely to be reached");
6145 Perl_warner(aTHX_ WARN_SYNTAX, "(Maybe you meant system() when you said exec()?)\n");
6146 PL_curcop->cop_line = oldline;
6160 if ((o->op_private & (OPpLVAL_INTRO))
6161 || ((BINOP*)o)->op_last->op_type != OP_CONST)
6163 rop = (UNOP*)((BINOP*)o)->op_first;
6164 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6166 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6167 if (!SvOBJECT(lexname))
6169 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6170 if (!fields || !GvHV(*fields))
6172 svp = &((SVOP*)((BINOP*)o)->op_last)->op_sv;
6173 key = SvPV(*svp, keylen);
6174 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
6176 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" in variable %s of type %s",
6177 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
6179 ind = SvIV(*indsvp);
6181 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6182 rop->op_type = OP_RV2AV;
6183 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6184 o->op_type = OP_AELEM;
6185 o->op_ppaddr = PL_ppaddr[OP_AELEM];
6187 *svp = newSViv(ind);
6193 if (!(o->op_flags & OPf_WANT)
6194 || (o->op_flags & OPf_WANT) == OPf_WANT_LIST)
6198 o->op_seq = PL_op_seqmax++;
6202 if (o->op_next && o->op_next->op_type != OP_LEAVESUBLV) {
6203 o->op_seq = PL_op_seqmax++;
6209 if (last_composite) {
6210 OP *r = last_composite;
6212 while (r->op_sibling)
6215 || (r->op_next->op_type == OP_LIST
6216 && r->op_next->op_next == o))
6218 if (last_composite->op_type == OP_RV2AV)
6219 yyerror("Lvalue subs returning arrays not implemented yet");
6221 yyerror("Lvalue subs returning hashes not implemented yet");
6228 o->op_seq = PL_op_seqmax++;