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, "0x%lx Pad 0x%lx alloc %ld for %s\n",
454 (unsigned long) thr, (unsigned long) PL_curpad,
455 (long) retval, PL_op_name[optype]));
457 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx alloc %ld for %s\n",
458 (unsigned long) PL_curpad,
459 (long) retval, PL_op_name[optype]));
460 #endif /* USE_THREADS */
461 return (PADOFFSET)retval;
465 Perl_pad_sv(pTHX_ PADOFFSET po)
469 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx sv %d\n",
470 (unsigned long) thr, (unsigned long) PL_curpad, po));
473 Perl_croak(aTHX_ "panic: pad_sv po");
474 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx sv %d\n",
475 (unsigned long) PL_curpad, po));
476 #endif /* USE_THREADS */
477 return PL_curpad[po]; /* eventually we'll turn this into a macro */
481 Perl_pad_free(pTHX_ PADOFFSET po)
486 if (AvARRAY(PL_comppad) != PL_curpad)
487 Perl_croak(aTHX_ "panic: pad_free curpad");
489 Perl_croak(aTHX_ "panic: pad_free po");
491 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx free %d\n",
492 (unsigned long) thr, (unsigned long) PL_curpad, po));
494 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx free %d\n",
495 (unsigned long) PL_curpad, po));
496 #endif /* USE_THREADS */
497 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef)
498 SvPADTMP_off(PL_curpad[po]);
499 if ((I32)po < PL_padix)
504 Perl_pad_swipe(pTHX_ PADOFFSET po)
507 if (AvARRAY(PL_comppad) != PL_curpad)
508 Perl_croak(aTHX_ "panic: pad_swipe curpad");
510 Perl_croak(aTHX_ "panic: pad_swipe po");
512 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx swipe %d\n",
513 (unsigned long) thr, (unsigned long) PL_curpad, po));
515 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx swipe %d\n",
516 (unsigned long) PL_curpad, po));
517 #endif /* USE_THREADS */
518 SvPADTMP_off(PL_curpad[po]);
519 PL_curpad[po] = NEWSV(1107,0);
520 SvPADTMP_on(PL_curpad[po]);
521 if ((I32)po < PL_padix)
525 /* XXX pad_reset() is currently disabled because it results in serious bugs.
526 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
527 * on the stack by OPs that use them, there are several ways to get an alias
528 * to a shared TARG. Such an alias will change randomly and unpredictably.
529 * We avoid doing this until we can think of a Better Way.
534 #ifdef USE_BROKEN_PAD_RESET
538 if (AvARRAY(PL_comppad) != PL_curpad)
539 Perl_croak(aTHX_ "panic: pad_reset curpad");
541 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx reset\n",
542 (unsigned long) thr, (unsigned long) PL_curpad));
544 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx reset\n",
545 (unsigned long) PL_curpad));
546 #endif /* USE_THREADS */
547 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
548 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
549 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
550 SvPADTMP_off(PL_curpad[po]);
552 PL_padix = PL_padix_floor;
555 PL_pad_reset_pending = FALSE;
559 /* find_threadsv is not reentrant */
561 Perl_find_threadsv(pTHX_ const char *name)
567 /* We currently only handle names of a single character */
568 p = strchr(PL_threadsv_names, *name);
571 key = p - PL_threadsv_names;
572 MUTEX_LOCK(&thr->mutex);
573 svp = av_fetch(thr->threadsv, key, FALSE);
575 MUTEX_UNLOCK(&thr->mutex);
577 SV *sv = NEWSV(0, 0);
578 av_store(thr->threadsv, key, sv);
579 thr->threadsvp = AvARRAY(thr->threadsv);
580 MUTEX_UNLOCK(&thr->mutex);
582 * Some magic variables used to be automagically initialised
583 * in gv_fetchpv. Those which are now per-thread magicals get
584 * initialised here instead.
590 sv_setpv(sv, "\034");
591 sv_magic(sv, 0, 0, name, 1);
596 PL_sawampersand = TRUE;
610 /* XXX %! tied to Errno.pm needs to be added here.
611 * See gv_fetchpv(). */
615 sv_magic(sv, 0, 0, name, 1);
617 DEBUG_S(PerlIO_printf(Perl_error_log,
618 "find_threadsv: new SV %p for $%s%c\n",
619 sv, (*name < 32) ? "^" : "",
620 (*name < 32) ? toCTRL(*name) : *name));
624 #endif /* USE_THREADS */
629 Perl_op_free(pTHX_ OP *o)
631 register OP *kid, *nextkid;
634 if (!o || o->op_seq == (U16)-1)
637 if (o->op_flags & OPf_KIDS) {
638 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
639 nextkid = kid->op_sibling; /* Get before next freeing kid */
647 /* COP* is not cleared by op_clear() so that we may track line
648 * numbers etc even after null() */
649 if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
654 #ifdef PL_OP_SLAB_ALLOC
655 if ((char *) o == PL_OpPtr)
664 S_op_clear(pTHX_ OP *o)
666 switch (o->op_type) {
667 case OP_NULL: /* Was holding old type, if any. */
668 case OP_ENTEREVAL: /* Was holding hints. */
670 case OP_THREADSV: /* Was holding index into thr->threadsv AV. */
676 if (!(o->op_flags & OPf_SPECIAL))
679 #endif /* USE_THREADS */
681 if (!(o->op_flags & OPf_REF)
682 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
688 SvREFCNT_dec(cGVOPo->op_gv);
689 cGVOPo->op_gv = Nullgv;
692 SvREFCNT_dec(cSVOPo->op_sv);
693 cSVOPo->op_sv = Nullsv;
699 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
703 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
704 SvREFCNT_dec(cSVOPo->op_sv);
705 cSVOPo->op_sv = Nullsv;
708 Safefree(cPVOPo->op_pv);
709 cPVOPo->op_pv = Nullch;
713 op_free(cPMOPo->op_pmreplroot);
714 cPMOPo->op_pmreplroot = Nullop;
719 ReREFCNT_dec(cPMOPo->op_pmregexp);
720 cPMOPo->op_pmregexp = (REGEXP*)NULL;
725 pad_free(o->op_targ);
729 S_cop_free(pTHX_ COP* cop)
731 Safefree(cop->cop_label);
732 SvREFCNT_dec(cop->cop_filegv);
733 if (! specialWARN(cop->cop_warnings))
734 SvREFCNT_dec(cop->cop_warnings);
740 if (o->op_type == OP_NULL)
743 o->op_targ = o->op_type;
744 o->op_type = OP_NULL;
745 o->op_ppaddr = PL_ppaddr[OP_NULL];
748 /* Contextualizers */
750 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
753 Perl_linklist(pTHX_ OP *o)
760 /* establish postfix order */
761 if (cUNOPo->op_first) {
762 o->op_next = LINKLIST(cUNOPo->op_first);
763 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
765 kid->op_next = LINKLIST(kid->op_sibling);
777 Perl_scalarkids(pTHX_ OP *o)
780 if (o && o->op_flags & OPf_KIDS) {
781 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
788 S_scalarboolean(pTHX_ OP *o)
790 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
792 if (ckWARN(WARN_SYNTAX)) {
793 line_t oldline = PL_curcop->cop_line;
795 if (PL_copline != NOLINE)
796 PL_curcop->cop_line = PL_copline;
797 Perl_warner(aTHX_ WARN_SYNTAX, "Found = in conditional, should be ==");
798 PL_curcop->cop_line = oldline;
805 Perl_scalar(pTHX_ OP *o)
809 /* assumes no premature commitment */
810 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
811 || o->op_type == OP_RETURN)
814 if ((o->op_private & OPpTARGET_MY)
815 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
816 return scalar(o); /* As if inside SASSIGN */
818 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
820 switch (o->op_type) {
822 if (o->op_private & OPpREPEAT_DOLIST)
823 null(((LISTOP*)cBINOPo->op_first)->op_first);
824 scalar(cBINOPo->op_first);
829 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
833 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
834 if (!kPMOP->op_pmreplroot)
835 deprecate("implicit split to @_");
843 if (o->op_flags & OPf_KIDS) {
844 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
850 kid = cLISTOPo->op_first;
852 while (kid = kid->op_sibling) {
858 WITH_THR(PL_curcop = &PL_compiling);
863 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
869 WITH_THR(PL_curcop = &PL_compiling);
876 Perl_scalarvoid(pTHX_ OP *o)
883 if (o->op_type == OP_NEXTSTATE
884 || o->op_type == OP_SETSTATE
885 || o->op_type == OP_DBSTATE
886 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
887 || o->op_targ == OP_SETSTATE
888 || o->op_targ == OP_DBSTATE)))
891 PL_curcop = (COP*)o; /* for warning below */
894 /* assumes no premature commitment */
895 want = o->op_flags & OPf_WANT;
896 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
897 || o->op_type == OP_RETURN)
900 if ((o->op_private & OPpTARGET_MY)
901 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
902 return scalar(o); /* As if inside SASSIGN */
904 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
906 switch (o->op_type) {
908 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
912 if (o->op_flags & OPf_STACKED)
916 if (o->op_private == 4)
987 if (!(o->op_private & OPpLVAL_INTRO))
988 useless = PL_op_desc[o->op_type];
995 if (!(o->op_private & OPpLVAL_INTRO) &&
996 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
997 useless = "a variable";
1002 if (cSVOPo->op_private & OPpCONST_STRICT)
1003 no_bareword_allowed(o);
1006 if (ckWARN(WARN_VOID)) {
1007 useless = "a constant";
1008 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1010 else if (SvPOK(sv)) {
1011 if (strnEQ(SvPVX(sv), "di", 2) ||
1012 strnEQ(SvPVX(sv), "ds", 2) ||
1013 strnEQ(SvPVX(sv), "ig", 2))
1018 null(o); /* don't execute or even remember it */
1022 o->op_type = OP_PREINC; /* pre-increment is faster */
1023 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1027 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1028 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1034 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1039 if (o->op_flags & OPf_STACKED)
1047 if (!(o->op_flags & OPf_KIDS))
1056 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1063 /* all requires must return a boolean value */
1064 o->op_flags &= ~OPf_WANT;
1067 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
1068 if (!kPMOP->op_pmreplroot)
1069 deprecate("implicit split to @_");
1075 if (ckWARN(WARN_VOID))
1076 Perl_warner(aTHX_ WARN_VOID, "Useless use of %s in void context", useless);
1082 Perl_listkids(pTHX_ OP *o)
1085 if (o && o->op_flags & OPf_KIDS) {
1086 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1093 Perl_list(pTHX_ OP *o)
1097 /* assumes no premature commitment */
1098 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
1099 || o->op_type == OP_RETURN)
1102 if ((o->op_private & OPpTARGET_MY)
1103 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
1104 return o; /* As if inside SASSIGN */
1106 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1108 switch (o->op_type) {
1111 list(cBINOPo->op_first);
1116 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1124 if (!(o->op_flags & OPf_KIDS))
1126 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1127 list(cBINOPo->op_first);
1128 return gen_constant_list(o);
1135 kid = cLISTOPo->op_first;
1137 while (kid = kid->op_sibling) {
1138 if (kid->op_sibling)
1143 WITH_THR(PL_curcop = &PL_compiling);
1147 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1148 if (kid->op_sibling)
1153 WITH_THR(PL_curcop = &PL_compiling);
1156 /* all requires must return a boolean value */
1157 o->op_flags &= ~OPf_WANT;
1164 Perl_scalarseq(pTHX_ OP *o)
1169 if (o->op_type == OP_LINESEQ ||
1170 o->op_type == OP_SCOPE ||
1171 o->op_type == OP_LEAVE ||
1172 o->op_type == OP_LEAVETRY)
1175 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1176 if (kid->op_sibling) {
1180 PL_curcop = &PL_compiling;
1182 o->op_flags &= ~OPf_PARENS;
1183 if (PL_hints & HINT_BLOCK_SCOPE)
1184 o->op_flags |= OPf_PARENS;
1187 o = newOP(OP_STUB, 0);
1192 S_modkids(pTHX_ OP *o, I32 type)
1195 if (o && o->op_flags & OPf_KIDS) {
1196 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1203 Perl_mod(pTHX_ OP *o, I32 type)
1210 if (!o || PL_error_count)
1213 if ((o->op_private & OPpTARGET_MY)
1214 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
1217 switch (o->op_type) {
1222 if (!(o->op_private & (OPpCONST_ARYBASE)))
1224 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1225 PL_compiling.cop_arybase = (I32)SvIV(((SVOP*)PL_eval_start)->op_sv);
1229 SAVEI32(PL_compiling.cop_arybase);
1230 PL_compiling.cop_arybase = 0;
1232 else if (type == OP_REFGEN)
1235 Perl_croak(aTHX_ "That use of $[ is unsupported");
1238 if (o->op_flags & OPf_PARENS)
1242 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1243 !(o->op_flags & OPf_STACKED)) {
1244 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1245 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1246 assert(cUNOPo->op_first->op_type == OP_NULL);
1247 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1250 else { /* lvalue subroutine call */
1251 o->op_private |= OPpLVAL_INTRO;
1252 if (type == OP_GREPSTART || type == OP_ENTERSUB) {
1253 /* Backward compatibility mode: */
1254 o->op_private |= OPpENTERSUB_INARGS;
1257 else { /* Compile-time error message: */
1258 OP *kid = cUNOPo->op_first;
1262 if (kid->op_type == OP_PUSHMARK)
1264 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1266 "panic: unexpected lvalue entersub "
1267 "args: type/targ %ld:%ld",
1268 (long)kid->op_type,kid->op_targ);
1269 kid = kLISTOP->op_first;
1271 while (kid->op_sibling)
1272 kid = kid->op_sibling;
1273 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1275 if (kid->op_type == OP_METHOD_NAMED
1276 || kid->op_type == OP_METHOD)
1280 if (kid->op_sibling || kid->op_next != kid) {
1281 yyerror("panic: unexpected optree near method call");
1285 NewOp(1101, newop, 1, OP);
1286 newop->op_type = OP_RV2CV;
1287 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1288 newop->op_next = newop;
1289 kid->op_sibling = newop;
1290 newop->op_private |= OPpLVAL_INTRO;
1294 if (kid->op_type != OP_RV2CV)
1296 "panic: unexpected lvalue entersub "
1297 "entry via type/targ %ld:%ld",
1298 (long)kid->op_type,kid->op_targ);
1299 kid->op_private |= OPpLVAL_INTRO;
1300 break; /* Postpone until runtime */
1304 kid = kUNOP->op_first;
1305 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1306 kid = kUNOP->op_first;
1307 if (kid->op_type == OP_NULL)
1309 "Unexpected constant lvalue entersub "
1310 "entry via type/targ %ld:%ld",
1311 (long)kid->op_type,kid->op_targ);
1312 if (kid->op_type != OP_GV) {
1313 /* Restore RV2CV to check lvalueness */
1315 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1316 okid->op_next = kid->op_next;
1317 kid->op_next = okid;
1320 okid->op_next = Nullop;
1321 okid->op_type = OP_RV2CV;
1323 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1324 okid->op_private |= OPpLVAL_INTRO;
1328 cv = GvCV(kGVOP->op_gv);
1338 /* grep, foreach, subcalls, refgen */
1339 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1341 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1342 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1344 : (o->op_type == OP_ENTERSUB
1345 ? "non-lvalue subroutine call"
1346 : PL_op_desc[o->op_type])),
1347 type ? PL_op_desc[type] : "local"));
1361 case OP_RIGHT_SHIFT:
1370 if (!(o->op_flags & OPf_STACKED))
1376 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1382 if (!type && cUNOPo->op_first->op_type != OP_GV)
1383 Perl_croak(aTHX_ "Can't localize through a reference");
1384 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1385 PL_modcount = 10000;
1386 return o; /* Treat \(@foo) like ordinary list. */
1390 if (scalar_mod_type(o, type))
1392 ref(cUNOPo->op_first, o->op_type);
1401 PL_modcount = 10000;
1404 if (!type && cUNOPo->op_first->op_type != OP_GV)
1405 Perl_croak(aTHX_ "Can't localize through a reference");
1406 ref(cUNOPo->op_first, o->op_type);
1410 PL_hints |= HINT_BLOCK_SCOPE;
1420 PL_modcount = 10000;
1421 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1422 return o; /* Treat \(@foo) like ordinary list. */
1423 if (scalar_mod_type(o, type))
1429 Perl_croak(aTHX_ "Can't localize lexical variable %s",
1430 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
1435 PL_modcount++; /* XXX ??? */
1437 #endif /* USE_THREADS */
1443 if (type != OP_SASSIGN)
1447 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1453 pad_free(o->op_targ);
1454 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1455 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1456 if (o->op_flags & OPf_KIDS)
1457 mod(cBINOPo->op_first->op_sibling, type);
1462 ref(cBINOPo->op_first, o->op_type);
1463 if (type == OP_ENTERSUB &&
1464 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1465 o->op_private |= OPpLVAL_DEFER;
1472 if (o->op_flags & OPf_KIDS)
1473 mod(cLISTOPo->op_last, type);
1477 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1479 else if (!(o->op_flags & OPf_KIDS))
1481 if (o->op_targ != OP_LIST) {
1482 mod(cBINOPo->op_first, type);
1487 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1491 o->op_flags |= OPf_MOD;
1493 if (type == OP_AASSIGN || type == OP_SASSIGN)
1494 o->op_flags |= OPf_SPECIAL|OPf_REF;
1496 o->op_private |= OPpLVAL_INTRO;
1497 o->op_flags &= ~OPf_SPECIAL;
1498 PL_hints |= HINT_BLOCK_SCOPE;
1500 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1501 o->op_flags |= OPf_REF;
1506 S_scalar_mod_type(pTHX_ OP *o, I32 type)
1510 if (o->op_type == OP_RV2GV)
1534 case OP_RIGHT_SHIFT:
1553 S_is_handle_constructor(pTHX_ OP *o, I32 argnum)
1555 switch (o->op_type) {
1563 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1576 Perl_refkids(pTHX_ OP *o, I32 type)
1579 if (o && o->op_flags & OPf_KIDS) {
1580 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1587 Perl_ref(pTHX_ OP *o, I32 type)
1591 if (!o || PL_error_count)
1594 switch (o->op_type) {
1596 if ((type == OP_DEFINED || type == OP_LOCK) &&
1597 !(o->op_flags & OPf_STACKED)) {
1598 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1599 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1600 assert(cUNOPo->op_first->op_type == OP_NULL);
1601 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1602 o->op_flags |= OPf_SPECIAL;
1607 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1611 if (type == OP_DEFINED)
1612 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1613 ref(cUNOPo->op_first, o->op_type);
1616 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1617 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1618 : type == OP_RV2HV ? OPpDEREF_HV
1620 o->op_flags |= OPf_MOD;
1625 o->op_flags |= OPf_MOD; /* XXX ??? */
1630 o->op_flags |= OPf_REF;
1633 if (type == OP_DEFINED)
1634 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1635 ref(cUNOPo->op_first, o->op_type);
1640 o->op_flags |= OPf_REF;
1645 if (!(o->op_flags & OPf_KIDS))
1647 ref(cBINOPo->op_first, type);
1651 ref(cBINOPo->op_first, o->op_type);
1652 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1653 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1654 : type == OP_RV2HV ? OPpDEREF_HV
1656 o->op_flags |= OPf_MOD;
1664 if (!(o->op_flags & OPf_KIDS))
1666 ref(cLISTOPo->op_last, type);
1676 S_dup_attrlist(pTHX_ OP *o)
1680 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1681 * where the first kid is OP_PUSHMARK and the remaining ones
1682 * are OP_CONST. We need to push the OP_CONST values.
1684 if (o->op_type == OP_CONST)
1685 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc(cSVOPo->op_sv));
1687 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1688 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1689 if (o->op_type == OP_CONST)
1690 rop = append_elem(OP_LIST, rop,
1691 newSVOP(OP_CONST, o->op_flags,
1692 SvREFCNT_inc(cSVOPo->op_sv)));
1699 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
1701 OP *modname; /* for 'use' */
1704 /* fake up C<use attributes $pkg,$rv,@attrs> */
1705 ENTER; /* need to protect against side-effects of 'use' */
1707 if (stash && HvNAME(stash))
1708 stashsv = newSVpv(HvNAME(stash), 0);
1710 stashsv = &PL_sv_no;
1711 #define ATTRSMODULE "attributes"
1712 modname = newSVOP(OP_CONST, 0,
1713 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1));
1714 modname->op_private |= OPpCONST_BARE;
1715 /* that flag is required to make 'use' work right */
1716 utilize(1, start_subparse(FALSE, 0),
1717 Nullop, /* version */
1719 prepend_elem(OP_LIST,
1720 newSVOP(OP_CONST, 0, stashsv),
1721 prepend_elem(OP_LIST,
1722 newSVOP(OP_CONST, 0, newRV(target)),
1723 dup_attrlist(attrs))));
1728 S_my_kid(pTHX_ OP *o, OP *attrs)
1733 if (!o || PL_error_count)
1737 if (type == OP_LIST) {
1738 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1740 } else if (type == OP_UNDEF) {
1742 } else if (type == OP_RV2SV || /* "our" declaration */
1744 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
1746 } else if (type != OP_PADSV &&
1749 type != OP_PUSHMARK)
1751 yyerror(Perl_form(aTHX_ "Can't declare %s in my", PL_op_desc[o->op_type]));
1754 else if (attrs && type != OP_PUSHMARK) {
1759 /* check for C<my Dog $spot> when deciding package */
1760 namesvp = av_fetch(PL_comppad_name, o->op_targ, FALSE);
1761 if (namesvp && *namesvp && SvOBJECT(*namesvp) && HvNAME(SvSTASH(*namesvp)))
1762 stash = SvSTASH(*namesvp);
1764 stash = PL_curstash;
1765 padsv = PAD_SV(o->op_targ);
1766 apply_attrs(stash, padsv, attrs);
1768 o->op_flags |= OPf_MOD;
1769 o->op_private |= OPpLVAL_INTRO;
1774 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1776 if (o->op_flags & OPf_PARENS)
1779 PL_in_my_stash = Nullhv;
1782 return my_kid(o, attrs);
1786 Perl_my(pTHX_ OP *o)
1788 return my_kid(o, Nullop);
1792 Perl_sawparens(pTHX_ OP *o)
1795 o->op_flags |= OPf_PARENS;
1800 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
1805 if (ckWARN(WARN_UNSAFE) &&
1806 (left->op_type == OP_RV2AV ||
1807 left->op_type == OP_RV2HV ||
1808 left->op_type == OP_PADAV ||
1809 left->op_type == OP_PADHV)) {
1810 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
1811 right->op_type == OP_TRANS)
1812 ? right->op_type : OP_MATCH];
1813 char *sample = ((left->op_type == OP_RV2AV ||
1814 left->op_type == OP_PADAV)
1815 ? "@array" : "%hash");
1816 Perl_warner(aTHX_ WARN_UNSAFE,
1817 "Applying %s to %s will act on scalar(%s)",
1818 desc, sample, sample);
1821 if (right->op_type == OP_MATCH ||
1822 right->op_type == OP_SUBST ||
1823 right->op_type == OP_TRANS) {
1824 right->op_flags |= OPf_STACKED;
1825 if (right->op_type != OP_MATCH)
1826 left = mod(left, right->op_type);
1827 if (right->op_type == OP_TRANS)
1828 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1830 o = prepend_elem(right->op_type, scalar(left), right);
1832 return newUNOP(OP_NOT, 0, scalar(o));
1836 return bind_match(type, left,
1837 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1841 Perl_invert(pTHX_ OP *o)
1845 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1846 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1850 Perl_scope(pTHX_ OP *o)
1853 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
1854 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1855 o->op_type = OP_LEAVE;
1856 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
1859 if (o->op_type == OP_LINESEQ) {
1861 o->op_type = OP_SCOPE;
1862 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
1863 kid = ((LISTOP*)o)->op_first;
1864 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
1868 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1875 Perl_save_hints(pTHX)
1878 SAVESPTR(GvHV(PL_hintgv));
1879 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
1880 SAVEFREESV(GvHV(PL_hintgv));
1884 Perl_block_start(pTHX_ int full)
1887 int retval = PL_savestack_ix;
1889 SAVEI32(PL_comppad_name_floor);
1891 if ((PL_comppad_name_fill = AvFILLp(PL_comppad_name)) > 0)
1892 PL_comppad_name_floor = PL_comppad_name_fill;
1894 PL_comppad_name_floor = 0;
1896 SAVEI32(PL_min_intro_pending);
1897 SAVEI32(PL_max_intro_pending);
1898 PL_min_intro_pending = 0;
1899 SAVEI32(PL_comppad_name_fill);
1900 SAVEI32(PL_padix_floor);
1901 PL_padix_floor = PL_padix;
1902 PL_pad_reset_pending = FALSE;
1904 PL_hints &= ~HINT_BLOCK_SCOPE;
1905 SAVEPPTR(PL_compiling.cop_warnings);
1906 if (! specialWARN(PL_compiling.cop_warnings)) {
1907 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1908 SAVEFREESV(PL_compiling.cop_warnings) ;
1916 Perl_block_end(pTHX_ I32 floor, OP *seq)
1919 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
1920 OP* retval = scalarseq(seq);
1922 PL_pad_reset_pending = FALSE;
1923 PL_compiling.op_private = PL_hints;
1925 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1926 pad_leavemy(PL_comppad_name_fill);
1935 OP *o = newOP(OP_THREADSV, 0);
1936 o->op_targ = find_threadsv("_");
1939 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
1940 #endif /* USE_THREADS */
1944 Perl_newPROG(pTHX_ OP *o)
1950 PL_eval_root = newUNOP(OP_LEAVEEVAL,
1951 ((PL_in_eval & EVAL_KEEPERR)
1952 ? OPf_SPECIAL : 0), o);
1953 PL_eval_start = linklist(PL_eval_root);
1954 PL_eval_root->op_next = 0;
1955 peep(PL_eval_start);
1960 PL_main_root = scope(sawparens(scalarvoid(o)));
1961 PL_curcop = &PL_compiling;
1962 PL_main_start = LINKLIST(PL_main_root);
1963 PL_main_root->op_next = 0;
1964 peep(PL_main_start);
1967 /* Register with debugger */
1969 CV *cv = get_cv("DB::postponed", FALSE);
1973 XPUSHs((SV*)PL_compiling.cop_filegv);
1975 call_sv((SV*)cv, G_DISCARD);
1982 Perl_localize(pTHX_ OP *o, I32 lex)
1984 if (o->op_flags & OPf_PARENS)
1988 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
1990 for (s = PL_bufptr; *s && (isALNUM(*s) || (*s & 0x80) || strchr("@$%, ",*s)); s++) ;
1991 if (*s == ';' || *s == '=')
1992 Perl_warner(aTHX_ WARN_PARENTHESIS, "Parentheses missing around \"%s\" list",
1993 lex ? "my" : "local");
1997 PL_in_my_stash = Nullhv;
2001 return mod(o, OP_NULL); /* a bit kludgey */
2005 Perl_jmaybe(pTHX_ OP *o)
2007 if (o->op_type == OP_LIST) {
2010 o2 = newOP(OP_THREADSV, 0);
2011 o2->op_targ = find_threadsv(";");
2013 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
2014 #endif /* USE_THREADS */
2015 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2021 Perl_fold_constants(pTHX_ register OP *o)
2025 I32 type = o->op_type;
2028 if (PL_opargs[type] & OA_RETSCALAR)
2030 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2031 o->op_targ = pad_alloc(type, SVs_PADTMP);
2033 /* integerize op, unless it happens to be C<-foo>.
2034 * XXX should pp_i_negate() do magic string negation instead? */
2035 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2036 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2037 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2039 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2042 if (!(PL_opargs[type] & OA_FOLDCONST))
2047 /* XXX might want a ck_negate() for this */
2048 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2061 if (o->op_private & OPpLOCALE)
2066 goto nope; /* Don't try to run w/ errors */
2068 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2069 if ((curop->op_type != OP_CONST ||
2070 (curop->op_private & OPpCONST_BARE)) &&
2071 curop->op_type != OP_LIST &&
2072 curop->op_type != OP_SCALAR &&
2073 curop->op_type != OP_NULL &&
2074 curop->op_type != OP_PUSHMARK)
2080 curop = LINKLIST(o);
2084 sv = *(PL_stack_sp--);
2085 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
2086 pad_swipe(o->op_targ);
2087 else if (SvTEMP(sv)) { /* grab mortal temp? */
2088 (void)SvREFCNT_inc(sv);
2092 if (type == OP_RV2GV)
2093 return newGVOP(OP_GV, 0, (GV*)sv);
2095 /* try to smush double to int, but don't smush -2.0 to -2 */
2096 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
2100 if ((NV)iv == SvNV(sv)) {
2105 SvIOK_off(sv); /* undo SvIV() damage */
2107 return newSVOP(OP_CONST, 0, sv);
2111 if (!(PL_opargs[type] & OA_OTHERINT))
2114 if (!(PL_hints & HINT_INTEGER)) {
2115 if (type == OP_MODULO
2116 || type == OP_DIVIDE
2117 || !(o->op_flags & OPf_KIDS))
2122 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
2123 if (curop->op_type == OP_CONST) {
2124 if (SvIOK(((SVOP*)curop)->op_sv))
2128 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
2132 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
2139 Perl_gen_constant_list(pTHX_ register OP *o)
2143 I32 oldtmps_floor = PL_tmps_floor;
2147 return o; /* Don't attempt to run with errors */
2149 PL_op = curop = LINKLIST(o);
2155 PL_tmps_floor = oldtmps_floor;
2157 o->op_type = OP_RV2AV;
2158 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2159 curop = ((UNOP*)o)->op_first;
2160 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
2167 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2172 if (!o || o->op_type != OP_LIST)
2173 o = newLISTOP(OP_LIST, 0, o, Nullop);
2175 o->op_flags &= ~OPf_WANT;
2177 if (!(PL_opargs[type] & OA_MARK))
2178 null(cLISTOPo->op_first);
2181 o->op_ppaddr = PL_ppaddr[type];
2182 o->op_flags |= flags;
2184 o = CHECKOP(type, o);
2185 if (o->op_type != type)
2188 if (cLISTOPo->op_children < 7) {
2189 /* XXX do we really need to do this if we're done appending?? */
2190 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2192 cLISTOPo->op_last = last; /* in case check substituted last arg */
2195 return fold_constants(o);
2198 /* List constructors */
2201 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2209 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
2210 return newLISTOP(type, 0, first, last);
2212 if (first->op_flags & OPf_KIDS)
2213 ((LISTOP*)first)->op_last->op_sibling = last;
2215 first->op_flags |= OPf_KIDS;
2216 ((LISTOP*)first)->op_first = last;
2218 ((LISTOP*)first)->op_last = last;
2219 ((LISTOP*)first)->op_children++;
2224 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2232 if (first->op_type != type)
2233 return prepend_elem(type, (OP*)first, (OP*)last);
2235 if (last->op_type != type)
2236 return append_elem(type, (OP*)first, (OP*)last);
2238 first->op_last->op_sibling = last->op_first;
2239 first->op_last = last->op_last;
2240 first->op_children += last->op_children;
2241 if (first->op_children)
2242 first->op_flags |= OPf_KIDS;
2244 #ifdef PL_OP_SLAB_ALLOC
2252 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2260 if (last->op_type == type) {
2261 if (type == OP_LIST) { /* already a PUSHMARK there */
2262 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2263 ((LISTOP*)last)->op_first->op_sibling = first;
2266 if (!(last->op_flags & OPf_KIDS)) {
2267 ((LISTOP*)last)->op_last = first;
2268 last->op_flags |= OPf_KIDS;
2270 first->op_sibling = ((LISTOP*)last)->op_first;
2271 ((LISTOP*)last)->op_first = first;
2273 ((LISTOP*)last)->op_children++;
2277 return newLISTOP(type, 0, first, last);
2283 Perl_newNULLLIST(pTHX)
2285 return newOP(OP_STUB, 0);
2289 Perl_force_list(pTHX_ OP *o)
2291 if (!o || o->op_type != OP_LIST)
2292 o = newLISTOP(OP_LIST, 0, o, Nullop);
2298 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2302 NewOp(1101, listop, 1, LISTOP);
2304 listop->op_type = type;
2305 listop->op_ppaddr = PL_ppaddr[type];
2306 listop->op_children = (first != 0) + (last != 0);
2307 listop->op_flags = flags;
2311 else if (!first && last)
2314 first->op_sibling = last;
2315 listop->op_first = first;
2316 listop->op_last = last;
2317 if (type == OP_LIST) {
2319 pushop = newOP(OP_PUSHMARK, 0);
2320 pushop->op_sibling = first;
2321 listop->op_first = pushop;
2322 listop->op_flags |= OPf_KIDS;
2324 listop->op_last = pushop;
2326 else if (listop->op_children)
2327 listop->op_flags |= OPf_KIDS;
2333 Perl_newOP(pTHX_ I32 type, I32 flags)
2336 NewOp(1101, o, 1, OP);
2338 o->op_ppaddr = PL_ppaddr[type];
2339 o->op_flags = flags;
2342 o->op_private = 0 + (flags >> 8);
2343 if (PL_opargs[type] & OA_RETSCALAR)
2345 if (PL_opargs[type] & OA_TARGET)
2346 o->op_targ = pad_alloc(type, SVs_PADTMP);
2347 return CHECKOP(type, o);
2351 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
2356 first = newOP(OP_STUB, 0);
2357 if (PL_opargs[type] & OA_MARK)
2358 first = force_list(first);
2360 NewOp(1101, unop, 1, UNOP);
2361 unop->op_type = type;
2362 unop->op_ppaddr = PL_ppaddr[type];
2363 unop->op_first = first;
2364 unop->op_flags = flags | OPf_KIDS;
2365 unop->op_private = 1 | (flags >> 8);
2366 unop = (UNOP*) CHECKOP(type, unop);
2370 return fold_constants((OP *) unop);
2374 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2377 NewOp(1101, binop, 1, BINOP);
2380 first = newOP(OP_NULL, 0);
2382 binop->op_type = type;
2383 binop->op_ppaddr = PL_ppaddr[type];
2384 binop->op_first = first;
2385 binop->op_flags = flags | OPf_KIDS;
2388 binop->op_private = 1 | (flags >> 8);
2391 binop->op_private = 2 | (flags >> 8);
2392 first->op_sibling = last;
2395 binop = (BINOP*)CHECKOP(type, binop);
2396 if (binop->op_next || binop->op_type != type)
2399 binop->op_last = binop->op_first->op_sibling;
2401 return fold_constants((OP *)binop);
2405 utf8compare(const void *a, const void *b)
2408 for (i = 0; i < 10; i++) {
2409 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2411 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2418 Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
2420 SV *tstr = ((SVOP*)expr)->op_sv;
2421 SV *rstr = ((SVOP*)repl)->op_sv;
2424 register U8 *t = (U8*)SvPV(tstr, tlen);
2425 register U8 *r = (U8*)SvPV(rstr, rlen);
2431 register short *tbl;
2433 complement = o->op_private & OPpTRANS_COMPLEMENT;
2434 del = o->op_private & OPpTRANS_DELETE;
2435 squash = o->op_private & OPpTRANS_SQUASH;
2437 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2438 SV* listsv = newSVpvn("# comment\n",10);
2440 U8* tend = t + tlen;
2441 U8* rend = r + rlen;
2457 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2458 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2464 New(1109, cp, tlen, U8*);
2466 transv = newSVpvn("",0);
2475 qsort(cp, i, sizeof(U8*), utf8compare);
2476 for (j = 0; j < i; j++) {
2478 UV val = utf8_to_uv(s, &ulen);
2480 diff = val - nextmin;
2482 t = uv_to_utf8(tmpbuf,nextmin);
2483 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2485 t = uv_to_utf8(tmpbuf, val - 1);
2486 sv_catpvn(transv, "\377", 1);
2487 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2491 val = utf8_to_uv(s+1, &ulen);
2495 t = uv_to_utf8(tmpbuf,nextmin);
2496 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2497 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2498 sv_catpvn(transv, "\377", 1);
2499 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2500 t = (U8*)SvPVX(transv);
2501 tlen = SvCUR(transv);
2504 else if (!rlen && !del) {
2505 r = t; rlen = tlen; rend = tend;
2508 if (to_utf && from_utf) { /* only counting characters */
2509 if (t == r || (tlen == rlen && memEQ(t, r, tlen)))
2510 o->op_private |= OPpTRANS_IDENTICAL;
2512 else { /* straight latin-1 translation */
2513 if (tlen == 4 && memEQ(t, "\0\377\303\277", 4) &&
2514 rlen == 4 && memEQ(r, "\0\377\303\277", 4))
2515 o->op_private |= OPpTRANS_IDENTICAL;
2519 while (t < tend || tfirst <= tlast) {
2520 /* see if we need more "t" chars */
2521 if (tfirst > tlast) {
2522 tfirst = (I32)utf8_to_uv(t, &ulen);
2524 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
2525 tlast = (I32)utf8_to_uv(++t, &ulen);
2532 /* now see if we need more "r" chars */
2533 if (rfirst > rlast) {
2535 rfirst = (I32)utf8_to_uv(r, &ulen);
2537 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
2538 rlast = (I32)utf8_to_uv(++r, &ulen);
2547 rfirst = rlast = 0xffffffff;
2551 /* now see which range will peter our first, if either. */
2552 tdiff = tlast - tfirst;
2553 rdiff = rlast - rfirst;
2560 if (rfirst == 0xffffffff) {
2561 diff = tdiff; /* oops, pretend rdiff is infinite */
2563 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\tXXXX\n", tfirst, tlast);
2565 Perl_sv_catpvf(aTHX_ listsv, "%04x\t\tXXXX\n", tfirst);
2569 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\t%04x\n", tfirst, tfirst + diff, rfirst);
2571 Perl_sv_catpvf(aTHX_ listsv, "%04x\t\t%04x\n", tfirst, rfirst);
2573 if (rfirst + diff > max)
2574 max = rfirst + diff;
2579 else if (rfirst <= 0x800)
2580 grows |= (tfirst < 0x80);
2581 else if (rfirst <= 0x10000)
2582 grows |= (tfirst < 0x800);
2583 else if (rfirst <= 0x200000)
2584 grows |= (tfirst < 0x10000);
2585 else if (rfirst <= 0x4000000)
2586 grows |= (tfirst < 0x200000);
2587 else if (rfirst <= 0x80000000)
2588 grows |= (tfirst < 0x4000000);
2600 else if (max > 0xff)
2605 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2606 SvREFCNT_dec(listsv);
2608 SvREFCNT_dec(transv);
2610 if (!del && havefinal)
2611 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5, newSViv((IV)final), 0);
2613 if (grows && to_utf)
2614 o->op_private |= OPpTRANS_GROWS;
2621 tbl = (short*)cPVOPo->op_pv;
2623 Zero(tbl, 256, short);
2624 for (i = 0; i < tlen; i++)
2626 for (i = 0, j = 0; i < 256; i++) {
2642 if (!rlen && !del) {
2645 o->op_private |= OPpTRANS_IDENTICAL;
2647 for (i = 0; i < 256; i++)
2649 for (i = 0, j = 0; i < tlen; i++,j++) {
2652 if (tbl[t[i]] == -1)
2658 if (tbl[t[i]] == -1)
2669 Perl_newPMOP(pTHX_ I32 type, I32 flags)
2674 NewOp(1101, pmop, 1, PMOP);
2675 pmop->op_type = type;
2676 pmop->op_ppaddr = PL_ppaddr[type];
2677 pmop->op_flags = flags;
2678 pmop->op_private = 0 | (flags >> 8);
2680 if (PL_hints & HINT_RE_TAINT)
2681 pmop->op_pmpermflags |= PMf_RETAINT;
2682 if (PL_hints & HINT_LOCALE)
2683 pmop->op_pmpermflags |= PMf_LOCALE;
2684 pmop->op_pmflags = pmop->op_pmpermflags;
2686 /* link into pm list */
2687 if (type != OP_TRANS && PL_curstash) {
2688 pmop->op_pmnext = HvPMROOT(PL_curstash);
2689 HvPMROOT(PL_curstash) = pmop;
2696 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
2701 I32 repl_has_vars = 0;
2703 if (o->op_type == OP_TRANS)
2704 return pmtrans(o, expr, repl);
2706 PL_hints |= HINT_BLOCK_SCOPE;
2709 if (expr->op_type == OP_CONST) {
2711 SV *pat = ((SVOP*)expr)->op_sv;
2712 char *p = SvPV(pat, plen);
2713 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2714 sv_setpvn(pat, "\\s+", 3);
2715 p = SvPV(pat, plen);
2716 pm->op_pmflags |= PMf_SKIPWHITE;
2718 pm->op_pmregexp = CALLREGCOMP(aTHX_ p, p + plen, pm);
2719 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2720 pm->op_pmflags |= PMf_WHITE;
2724 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2725 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2727 : OP_REGCMAYBE),0,expr);
2729 NewOp(1101, rcop, 1, LOGOP);
2730 rcop->op_type = OP_REGCOMP;
2731 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2732 rcop->op_first = scalar(expr);
2733 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2734 ? (OPf_SPECIAL | OPf_KIDS)
2736 rcop->op_private = 1;
2739 /* establish postfix order */
2740 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2742 rcop->op_next = expr;
2743 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2746 rcop->op_next = LINKLIST(expr);
2747 expr->op_next = (OP*)rcop;
2750 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2755 if (pm->op_pmflags & PMf_EVAL) {
2757 if (PL_curcop->cop_line < PL_multi_end)
2758 PL_curcop->cop_line = PL_multi_end;
2761 else if (repl->op_type == OP_THREADSV
2762 && strchr("&`'123456789+",
2763 PL_threadsv_names[repl->op_targ]))
2767 #endif /* USE_THREADS */
2768 else if (repl->op_type == OP_CONST)
2772 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2773 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2775 if (curop->op_type == OP_THREADSV) {
2777 if (strchr("&`'123456789+", curop->op_private))
2781 if (curop->op_type == OP_GV) {
2782 GV *gv = ((GVOP*)curop)->op_gv;
2784 if (strchr("&`'123456789+", *GvENAME(gv)))
2787 #endif /* USE_THREADS */
2788 else if (curop->op_type == OP_RV2CV)
2790 else if (curop->op_type == OP_RV2SV ||
2791 curop->op_type == OP_RV2AV ||
2792 curop->op_type == OP_RV2HV ||
2793 curop->op_type == OP_RV2GV) {
2794 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2797 else if (curop->op_type == OP_PADSV ||
2798 curop->op_type == OP_PADAV ||
2799 curop->op_type == OP_PADHV ||
2800 curop->op_type == OP_PADANY) {
2803 else if (curop->op_type == OP_PUSHRE)
2804 ; /* Okay here, dangerous in newASSIGNOP */
2813 && (!pm->op_pmregexp
2814 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
2815 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2816 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2817 prepend_elem(o->op_type, scalar(repl), o);
2820 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
2821 pm->op_pmflags |= PMf_MAYBE_CONST;
2822 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2824 NewOp(1101, rcop, 1, LOGOP);
2825 rcop->op_type = OP_SUBSTCONT;
2826 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
2827 rcop->op_first = scalar(repl);
2828 rcop->op_flags |= OPf_KIDS;
2829 rcop->op_private = 1;
2832 /* establish postfix order */
2833 rcop->op_next = LINKLIST(repl);
2834 repl->op_next = (OP*)rcop;
2836 pm->op_pmreplroot = scalar((OP*)rcop);
2837 pm->op_pmreplstart = LINKLIST(rcop);
2846 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
2849 NewOp(1101, svop, 1, SVOP);
2850 svop->op_type = type;
2851 svop->op_ppaddr = PL_ppaddr[type];
2853 svop->op_next = (OP*)svop;
2854 svop->op_flags = flags;
2855 if (PL_opargs[type] & OA_RETSCALAR)
2857 if (PL_opargs[type] & OA_TARGET)
2858 svop->op_targ = pad_alloc(type, SVs_PADTMP);
2859 return CHECKOP(type, svop);
2863 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
2867 NewOp(1101, gvop, 1, GVOP);
2868 gvop->op_type = type;
2869 gvop->op_ppaddr = PL_ppaddr[type];
2870 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
2871 gvop->op_next = (OP*)gvop;
2872 gvop->op_flags = flags;
2873 if (PL_opargs[type] & OA_RETSCALAR)
2875 if (PL_opargs[type] & OA_TARGET)
2876 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
2877 return CHECKOP(type, gvop);
2881 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
2884 NewOp(1101, pvop, 1, PVOP);
2885 pvop->op_type = type;
2886 pvop->op_ppaddr = PL_ppaddr[type];
2888 pvop->op_next = (OP*)pvop;
2889 pvop->op_flags = flags;
2890 if (PL_opargs[type] & OA_RETSCALAR)
2892 if (PL_opargs[type] & OA_TARGET)
2893 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2894 return CHECKOP(type, pvop);
2898 Perl_package(pTHX_ OP *o)
2903 save_hptr(&PL_curstash);
2904 save_item(PL_curstname);
2909 name = SvPV(sv, len);
2910 PL_curstash = gv_stashpvn(name,len,TRUE);
2911 sv_setpvn(PL_curstname, name, len);
2915 sv_setpv(PL_curstname,"<none>");
2916 PL_curstash = Nullhv;
2918 PL_hints |= HINT_BLOCK_SCOPE;
2919 PL_copline = NOLINE;
2924 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg)
2933 if (id->op_type != OP_CONST)
2934 Perl_croak(aTHX_ "Module name must be constant");
2938 if(version != Nullop) {
2939 SV *vesv = ((SVOP*)version)->op_sv;
2941 if (arg == Nullop && !SvNIOK(vesv)) {
2947 if (version->op_type != OP_CONST || !SvNIOK(vesv))
2948 Perl_croak(aTHX_ "Version number must be constant number");
2950 /* Make copy of id so we don't free it twice */
2951 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2953 /* Fake up a method call to VERSION */
2954 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2955 append_elem(OP_LIST,
2956 prepend_elem(OP_LIST, pack, list(version)),
2957 newSVOP(OP_METHOD_NAMED, 0,
2958 newSVpvn("VERSION", 7))));
2962 /* Fake up an import/unimport */
2963 if (arg && arg->op_type == OP_STUB)
2964 imop = arg; /* no import on explicit () */
2965 else if(SvNIOK(((SVOP*)id)->op_sv)) {
2966 imop = Nullop; /* use 5.0; */
2969 /* Make copy of id so we don't free it twice */
2970 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2971 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2972 append_elem(OP_LIST,
2973 prepend_elem(OP_LIST, pack, list(arg)),
2974 newSVOP(OP_METHOD_NAMED, 0,
2975 aver ? newSVpvn("import", 6)
2976 : newSVpvn("unimport", 8))));
2979 /* Fake up a require, handle override, if any */
2980 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
2981 if (!(gv && GvIMPORTED_CV(gv)))
2982 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
2984 if (gv && GvIMPORTED_CV(gv)) {
2985 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
2986 append_elem(OP_LIST, id,
2987 scalar(newUNOP(OP_RV2CV, 0,
2992 rqop = newUNOP(OP_REQUIRE, 0, id);
2995 /* Fake up the BEGIN {}, which does its thing immediately. */
2997 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
3000 append_elem(OP_LINESEQ,
3001 append_elem(OP_LINESEQ,
3002 newSTATEOP(0, Nullch, rqop),
3003 newSTATEOP(0, Nullch, veop)),
3004 newSTATEOP(0, Nullch, imop) ));
3006 PL_hints |= HINT_BLOCK_SCOPE;
3007 PL_copline = NOLINE;
3012 Perl_dofile(pTHX_ OP *term)
3017 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
3018 if (!(gv && GvIMPORTED_CV(gv)))
3019 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
3021 if (gv && GvIMPORTED_CV(gv)) {
3022 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3023 append_elem(OP_LIST, term,
3024 scalar(newUNOP(OP_RV2CV, 0,
3029 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3035 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
3037 return newBINOP(OP_LSLICE, flags,
3038 list(force_list(subscript)),
3039 list(force_list(listval)) );
3043 S_list_assignment(pTHX_ register OP *o)
3048 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3049 o = cUNOPo->op_first;
3051 if (o->op_type == OP_COND_EXPR) {
3052 I32 t = list_assignment(cLOGOPo->op_first->op_sibling);
3053 I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
3058 yyerror("Assignment to both a list and a scalar");
3062 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3063 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3064 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
3067 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
3070 if (o->op_type == OP_RV2SV)
3077 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
3082 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
3083 return newLOGOP(optype, 0,
3084 mod(scalar(left), optype),
3085 newUNOP(OP_SASSIGN, 0, scalar(right)));
3088 return newBINOP(optype, OPf_STACKED,
3089 mod(scalar(left), optype), scalar(right));
3093 if (list_assignment(left)) {
3096 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3097 left = mod(left, OP_AASSIGN);
3105 o = newBINOP(OP_AASSIGN, flags,
3106 list(force_list(right)),
3107 list(force_list(left)) );
3108 o->op_private = 0 | (flags >> 8);
3109 if (!(left->op_private & OPpLVAL_INTRO)) {
3113 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3114 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3115 if (curop->op_type == OP_GV) {
3116 GV *gv = ((GVOP*)curop)->op_gv;
3117 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3119 SvCUR(gv) = PL_generation;
3121 else if (curop->op_type == OP_PADSV ||
3122 curop->op_type == OP_PADAV ||
3123 curop->op_type == OP_PADHV ||
3124 curop->op_type == OP_PADANY) {
3125 SV **svp = AvARRAY(PL_comppad_name);
3126 SV *sv = svp[curop->op_targ];
3127 if (SvCUR(sv) == PL_generation)
3129 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
3131 else if (curop->op_type == OP_RV2CV)
3133 else if (curop->op_type == OP_RV2SV ||
3134 curop->op_type == OP_RV2AV ||
3135 curop->op_type == OP_RV2HV ||
3136 curop->op_type == OP_RV2GV) {
3137 if (lastop->op_type != OP_GV) /* funny deref? */
3140 else if (curop->op_type == OP_PUSHRE) {
3141 if (((PMOP*)curop)->op_pmreplroot) {
3142 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3143 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3145 SvCUR(gv) = PL_generation;
3154 o->op_private = OPpASSIGN_COMMON;
3156 if (right && right->op_type == OP_SPLIT) {
3158 if ((tmpop = ((LISTOP*)right)->op_first) &&
3159 tmpop->op_type == OP_PUSHRE)
3161 PMOP *pm = (PMOP*)tmpop;
3162 if (left->op_type == OP_RV2AV &&
3163 !(left->op_private & OPpLVAL_INTRO) &&
3164 !(o->op_private & OPpASSIGN_COMMON) )
3166 tmpop = ((UNOP*)left)->op_first;
3167 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
3168 pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
3169 pm->op_pmflags |= PMf_ONCE;
3170 tmpop = cUNOPo->op_first; /* to list (nulled) */
3171 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3172 tmpop->op_sibling = Nullop; /* don't free split */
3173 right->op_next = tmpop->op_next; /* fix starting loc */
3174 op_free(o); /* blow off assign */
3175 right->op_flags &= ~OPf_WANT;
3176 /* "I don't know and I don't care." */
3181 if (PL_modcount < 10000 &&
3182 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3184 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3186 sv_setiv(sv, PL_modcount+1);
3194 right = newOP(OP_UNDEF, 0);
3195 if (right->op_type == OP_READLINE) {
3196 right->op_flags |= OPf_STACKED;
3197 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3200 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3201 o = newBINOP(OP_SASSIGN, flags,
3202 scalar(right), mod(scalar(left), OP_SASSIGN) );
3214 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3217 U32 seq = intro_my();
3220 NewOp(1101, cop, 1, COP);
3221 if (PERLDB_LINE && PL_curcop->cop_line && PL_curstash != PL_debstash) {
3222 cop->op_type = OP_DBSTATE;
3223 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3226 cop->op_type = OP_NEXTSTATE;
3227 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3229 cop->op_flags = flags;
3230 cop->op_private = (PL_hints & HINT_UTF8);
3232 cop->op_private |= NATIVE_HINTS;
3234 PL_compiling.op_private = cop->op_private;
3235 cop->op_next = (OP*)cop;
3238 cop->cop_label = label;
3239 PL_hints |= HINT_BLOCK_SCOPE;
3242 cop->cop_arybase = PL_curcop->cop_arybase;
3243 if (specialWARN(PL_curcop->cop_warnings))
3244 cop->cop_warnings = PL_curcop->cop_warnings ;
3246 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3249 if (PL_copline == NOLINE)
3250 cop->cop_line = PL_curcop->cop_line;
3252 cop->cop_line = PL_copline;
3253 PL_copline = NOLINE;
3255 cop->cop_filegv = (GV*)SvREFCNT_inc(PL_curcop->cop_filegv);
3256 cop->cop_stash = PL_curstash;
3258 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3259 SV **svp = av_fetch(GvAV(PL_curcop->cop_filegv),(I32)cop->cop_line, FALSE);
3260 if (svp && *svp != &PL_sv_undef && !SvIOK(*svp)) {
3261 (void)SvIOK_on(*svp);
3263 SvSTASH(*svp) = (HV*)cop;
3267 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3270 /* "Introduce" my variables to visible status. */
3278 if (! PL_min_intro_pending)
3279 return PL_cop_seqmax;
3281 svp = AvARRAY(PL_comppad_name);
3282 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
3283 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
3284 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
3285 SvNVX(sv) = (NV)PL_cop_seqmax;
3288 PL_min_intro_pending = 0;
3289 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
3290 return PL_cop_seqmax++;
3294 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
3296 return new_logop(type, flags, &first, &other);
3300 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
3305 OP *first = *firstp;
3306 OP *other = *otherp;
3308 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3309 return newBINOP(type, flags, scalar(first), scalar(other));
3311 scalarboolean(first);
3312 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3313 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3314 if (type == OP_AND || type == OP_OR) {
3320 first = *firstp = cUNOPo->op_first;
3322 first->op_next = o->op_next;
3323 cUNOPo->op_first = Nullop;
3327 if (first->op_type == OP_CONST) {
3328 if (ckWARN(WARN_PRECEDENCE) && (first->op_private & OPpCONST_BARE))
3329 Perl_warner(aTHX_ WARN_PRECEDENCE, "Probable precedence problem on %s",
3331 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3342 else if (first->op_type == OP_WANTARRAY) {
3348 else if (ckWARN(WARN_UNSAFE) && (first->op_flags & OPf_KIDS)) {
3349 OP *k1 = ((UNOP*)first)->op_first;
3350 OP *k2 = k1->op_sibling;
3352 switch (first->op_type)
3355 if (k2 && k2->op_type == OP_READLINE
3356 && (k2->op_flags & OPf_STACKED)
3357 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3359 warnop = k2->op_type;
3364 if (k1->op_type == OP_READDIR
3365 || k1->op_type == OP_GLOB
3366 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3367 || k1->op_type == OP_EACH)
3369 warnop = ((k1->op_type == OP_NULL)
3370 ? k1->op_targ : k1->op_type);
3375 line_t oldline = PL_curcop->cop_line;
3376 PL_curcop->cop_line = PL_copline;
3377 Perl_warner(aTHX_ WARN_UNSAFE,
3378 "Value of %s%s can be \"0\"; test with defined()",
3380 ((warnop == OP_READLINE || warnop == OP_GLOB)
3381 ? " construct" : "() operator"));
3382 PL_curcop->cop_line = oldline;
3389 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3390 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3392 NewOp(1101, logop, 1, LOGOP);
3394 logop->op_type = type;
3395 logop->op_ppaddr = PL_ppaddr[type];
3396 logop->op_first = first;
3397 logop->op_flags = flags | OPf_KIDS;
3398 logop->op_other = LINKLIST(other);
3399 logop->op_private = 1 | (flags >> 8);
3401 /* establish postfix order */
3402 logop->op_next = LINKLIST(first);
3403 first->op_next = (OP*)logop;
3404 first->op_sibling = other;
3406 o = newUNOP(OP_NULL, 0, (OP*)logop);
3413 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
3421 return newLOGOP(OP_AND, 0, first, trueop);
3423 return newLOGOP(OP_OR, 0, first, falseop);
3425 scalarboolean(first);
3426 if (first->op_type == OP_CONST) {
3427 if (SvTRUE(((SVOP*)first)->op_sv)) {
3438 else if (first->op_type == OP_WANTARRAY) {
3442 NewOp(1101, logop, 1, LOGOP);
3443 logop->op_type = OP_COND_EXPR;
3444 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3445 logop->op_first = first;
3446 logop->op_flags = flags | OPf_KIDS;
3447 logop->op_private = 1 | (flags >> 8);
3448 logop->op_other = LINKLIST(trueop);
3449 logop->op_next = LINKLIST(falseop);
3452 /* establish postfix order */
3453 start = LINKLIST(first);
3454 first->op_next = (OP*)logop;
3456 first->op_sibling = trueop;
3457 trueop->op_sibling = falseop;
3458 o = newUNOP(OP_NULL, 0, (OP*)logop);
3460 trueop->op_next = falseop->op_next = o;
3467 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
3476 NewOp(1101, range, 1, LOGOP);
3478 range->op_type = OP_RANGE;
3479 range->op_ppaddr = PL_ppaddr[OP_RANGE];
3480 range->op_first = left;
3481 range->op_flags = OPf_KIDS;
3482 leftstart = LINKLIST(left);
3483 range->op_other = LINKLIST(right);
3484 range->op_private = 1 | (flags >> 8);
3486 left->op_sibling = right;
3488 range->op_next = (OP*)range;
3489 flip = newUNOP(OP_FLIP, flags, (OP*)range);
3490 flop = newUNOP(OP_FLOP, 0, flip);
3491 o = newUNOP(OP_NULL, 0, flop);
3493 range->op_next = leftstart;
3495 left->op_next = flip;
3496 right->op_next = flop;
3498 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3499 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
3500 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3501 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3503 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3504 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3507 if (!flip->op_private || !flop->op_private)
3508 linklist(o); /* blow off optimizer unless constant */
3514 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
3519 int once = block && block->op_flags & OPf_SPECIAL &&
3520 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3523 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3524 return block; /* do {} while 0 does once */
3525 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3526 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3527 expr = newUNOP(OP_DEFINED, 0,
3528 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3529 } else if (expr->op_flags & OPf_KIDS) {
3530 OP *k1 = ((UNOP*)expr)->op_first;
3531 OP *k2 = (k1) ? k1->op_sibling : NULL;
3532 switch (expr->op_type) {
3534 if (k2 && k2->op_type == OP_READLINE
3535 && (k2->op_flags & OPf_STACKED)
3536 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3537 expr = newUNOP(OP_DEFINED, 0, expr);
3541 if (k1->op_type == OP_READDIR
3542 || k1->op_type == OP_GLOB
3543 || (k1->op_type == OP_NULL && k1->op_targ == OP_NULL)
3544 || k1->op_type == OP_EACH)
3545 expr = newUNOP(OP_DEFINED, 0, expr);
3551 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3552 o = new_logop(OP_AND, 0, &expr, &listop);
3555 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3557 if (once && o != listop)
3558 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3561 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3563 o->op_flags |= flags;
3565 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3570 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
3579 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3580 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
3581 expr = newUNOP(OP_DEFINED, 0,
3582 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3583 } else if (expr && (expr->op_flags & OPf_KIDS)) {
3584 OP *k1 = ((UNOP*)expr)->op_first;
3585 OP *k2 = (k1) ? k1->op_sibling : NULL;
3586 switch (expr->op_type) {
3588 if (k2 && k2->op_type == OP_READLINE
3589 && (k2->op_flags & OPf_STACKED)
3590 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3591 expr = newUNOP(OP_DEFINED, 0, expr);
3595 if (k1->op_type == OP_READDIR
3596 || k1->op_type == OP_GLOB
3597 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3598 || k1->op_type == OP_EACH)
3599 expr = newUNOP(OP_DEFINED, 0, expr);
3605 block = newOP(OP_NULL, 0);
3608 next = LINKLIST(cont);
3610 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
3611 if ((line_t)whileline != NOLINE) {
3612 PL_copline = whileline;
3613 cont = append_elem(OP_LINESEQ, cont,
3614 newSTATEOP(0, Nullch, Nullop));
3618 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3619 redo = LINKLIST(listop);
3622 PL_copline = whileline;
3624 o = new_logop(OP_AND, 0, &expr, &listop);
3625 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3626 op_free(expr); /* oops, it's a while (0) */
3628 return Nullop; /* listop already freed by new_logop */
3631 ((LISTOP*)listop)->op_last->op_next = condop =
3632 (o == listop ? redo : LINKLIST(o));
3640 NewOp(1101,loop,1,LOOP);
3641 loop->op_type = OP_ENTERLOOP;
3642 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3643 loop->op_private = 0;
3644 loop->op_next = (OP*)loop;
3647 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3649 loop->op_redoop = redo;
3650 loop->op_lastop = o;
3653 loop->op_nextop = next;
3655 loop->op_nextop = o;
3657 o->op_flags |= flags;
3658 o->op_private |= (flags >> 8);
3663 Perl_newFOROP(pTHX_ I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
3672 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3673 sv->op_type = OP_RV2GV;
3674 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
3676 else if (sv->op_type == OP_PADSV) { /* private variable */
3677 padoff = sv->op_targ;
3681 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3682 padoff = sv->op_targ;
3683 iterflags |= OPf_SPECIAL;
3688 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
3692 padoff = find_threadsv("_");
3693 iterflags |= OPf_SPECIAL;
3695 sv = newGVOP(OP_GV, 0, PL_defgv);
3698 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3699 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3700 iterflags |= OPf_STACKED;
3702 else if (expr->op_type == OP_NULL &&
3703 (expr->op_flags & OPf_KIDS) &&
3704 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3706 /* Basically turn for($x..$y) into the same as for($x,$y), but we
3707 * set the STACKED flag to indicate that these values are to be
3708 * treated as min/max values by 'pp_iterinit'.
3710 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3711 LOGOP* range = (LOGOP*) flip->op_first;
3712 OP* left = range->op_first;
3713 OP* right = left->op_sibling;
3716 range->op_flags &= ~OPf_KIDS;
3717 range->op_first = Nullop;
3719 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3720 listop->op_first->op_next = range->op_next;
3721 left->op_next = range->op_other;
3722 right->op_next = (OP*)listop;
3723 listop->op_next = listop->op_first;
3726 expr = (OP*)(listop);
3728 iterflags |= OPf_STACKED;
3731 expr = mod(force_list(expr), OP_GREPSTART);
3735 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
3736 append_elem(OP_LIST, expr, scalar(sv))));
3737 assert(!loop->op_next);
3738 #ifdef PL_OP_SLAB_ALLOC
3739 NewOp(1234,tmp,1,LOOP);
3740 Copy(loop,tmp,1,LOOP);
3743 Renew(loop, 1, LOOP);
3745 loop->op_targ = padoff;
3746 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
3747 PL_copline = forline;
3748 return newSTATEOP(0, label, wop);
3752 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
3758 if (type != OP_GOTO || label->op_type == OP_CONST) {
3759 /* "last()" means "last" */
3760 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
3761 o = newOP(type, OPf_SPECIAL);
3763 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
3764 ? SvPVx(((SVOP*)label)->op_sv, n_a)
3770 if (label->op_type == OP_ENTERSUB)
3771 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
3772 o = newUNOP(type, OPf_STACKED, label);
3774 PL_hints |= HINT_BLOCK_SCOPE;
3779 Perl_cv_undef(pTHX_ CV *cv)
3784 MUTEX_DESTROY(CvMUTEXP(cv));
3785 Safefree(CvMUTEXP(cv));
3788 #endif /* USE_THREADS */
3790 if (!CvXSUB(cv) && CvROOT(cv)) {
3792 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
3793 Perl_croak(aTHX_ "Can't undef active subroutine");
3796 Perl_croak(aTHX_ "Can't undef active subroutine");
3797 #endif /* USE_THREADS */
3800 SAVESPTR(PL_curpad);
3804 op_free(CvROOT(cv));
3805 CvROOT(cv) = Nullop;
3808 SvPOK_off((SV*)cv); /* forget prototype */
3810 SvREFCNT_dec(CvGV(cv));
3812 SvREFCNT_dec(CvOUTSIDE(cv));
3813 CvOUTSIDE(cv) = Nullcv;
3814 if (CvPADLIST(cv)) {
3815 /* may be during global destruction */
3816 if (SvREFCNT(CvPADLIST(cv))) {
3817 I32 i = AvFILLp(CvPADLIST(cv));
3819 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
3820 SV* sv = svp ? *svp : Nullsv;
3823 if (sv == (SV*)PL_comppad_name)
3824 PL_comppad_name = Nullav;
3825 else if (sv == (SV*)PL_comppad) {
3826 PL_comppad = Nullav;
3827 PL_curpad = Null(SV**);
3831 SvREFCNT_dec((SV*)CvPADLIST(cv));
3833 CvPADLIST(cv) = Nullav;
3837 #ifdef DEBUG_CLOSURES
3841 CV *outside = CvOUTSIDE(cv);
3842 AV* padlist = CvPADLIST(cv);
3849 PerlIO_printf(Perl_debug_log, "\tCV=0x%lx (%s), OUTSIDE=0x%lx (%s)\n",
3851 (CvANON(cv) ? "ANON"
3852 : (cv == PL_main_cv) ? "MAIN"
3853 : CvUNIQUE(cv) ? "UNIQUE"
3854 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
3857 : CvANON(outside) ? "ANON"
3858 : (outside == PL_main_cv) ? "MAIN"
3859 : CvUNIQUE(outside) ? "UNIQUE"
3860 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
3865 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
3866 pad = (AV*)*av_fetch(padlist, 1, FALSE);
3867 pname = AvARRAY(pad_name);
3868 ppad = AvARRAY(pad);
3870 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
3871 if (SvPOK(pname[ix]))
3872 PerlIO_printf(Perl_debug_log, "\t%4d. 0x%lx (%s\"%s\" %ld-%ld)\n",
3874 SvFAKE(pname[ix]) ? "FAKE " : "",
3876 (long)I_32(SvNVX(pname[ix])),
3877 (long)SvIVX(pname[ix]));
3880 #endif /* DEBUG_CLOSURES */
3883 S_cv_clone2(pTHX_ CV *proto, CV *outside)
3888 AV* protopadlist = CvPADLIST(proto);
3889 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
3890 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
3891 SV** pname = AvARRAY(protopad_name);
3892 SV** ppad = AvARRAY(protopad);
3893 I32 fname = AvFILLp(protopad_name);
3894 I32 fpad = AvFILLp(protopad);
3898 assert(!CvUNIQUE(proto));
3901 SAVESPTR(PL_curpad);
3902 SAVESPTR(PL_comppad);
3903 SAVESPTR(PL_comppad_name);
3904 SAVESPTR(PL_compcv);
3906 cv = PL_compcv = (CV*)NEWSV(1104,0);
3907 sv_upgrade((SV *)cv, SvTYPE(proto));
3913 New(666, CvMUTEXP(cv), 1, perl_mutex);
3914 MUTEX_INIT(CvMUTEXP(cv));
3916 #endif /* USE_THREADS */
3917 CvFILEGV(cv) = CvFILEGV(proto);
3918 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
3919 CvSTASH(cv) = CvSTASH(proto);
3920 CvROOT(cv) = CvROOT(proto);
3921 CvSTART(cv) = CvSTART(proto);
3923 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
3926 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
3928 PL_comppad_name = newAV();
3929 for (ix = fname; ix >= 0; ix--)
3930 av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix]));
3932 PL_comppad = newAV();
3934 comppadlist = newAV();
3935 AvREAL_off(comppadlist);
3936 av_store(comppadlist, 0, (SV*)PL_comppad_name);
3937 av_store(comppadlist, 1, (SV*)PL_comppad);
3938 CvPADLIST(cv) = comppadlist;
3939 av_fill(PL_comppad, AvFILLp(protopad));
3940 PL_curpad = AvARRAY(PL_comppad);
3942 av = newAV(); /* will be @_ */
3944 av_store(PL_comppad, 0, (SV*)av);
3945 AvFLAGS(av) = AVf_REIFY;
3947 for (ix = fpad; ix > 0; ix--) {
3948 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3949 if (namesv && namesv != &PL_sv_undef) {
3950 char *name = SvPVX(namesv); /* XXX */
3951 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
3952 I32 off = pad_findlex(name, ix, SvIVX(namesv),
3953 CvOUTSIDE(cv), cxstack_ix, 0, 0);
3955 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
3957 Perl_croak(aTHX_ "panic: cv_clone: %s", name);
3959 else { /* our own lexical */
3962 /* anon code -- we'll come back for it */
3963 sv = SvREFCNT_inc(ppad[ix]);
3965 else if (*name == '@')
3967 else if (*name == '%')
3977 SV* sv = NEWSV(0,0);
3983 /* Now that vars are all in place, clone nested closures. */
3985 for (ix = fpad; ix > 0; ix--) {
3986 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3988 && namesv != &PL_sv_undef
3989 && !(SvFLAGS(namesv) & SVf_FAKE)
3990 && *SvPVX(namesv) == '&'
3991 && CvCLONE(ppad[ix]))
3993 CV *kid = cv_clone2((CV*)ppad[ix], cv);
3994 SvREFCNT_dec(ppad[ix]);
3997 PL_curpad[ix] = (SV*)kid;
4001 #ifdef DEBUG_CLOSURES
4002 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
4004 PerlIO_printf(Perl_debug_log, " from:\n");
4006 PerlIO_printf(Perl_debug_log, " to:\n");
4015 Perl_cv_clone(pTHX_ CV *proto)
4018 MUTEX_LOCK(&PL_cred_mutex); /* XXX create separate mutex */
4019 cv = cv_clone2(proto, CvOUTSIDE(proto));
4020 MUTEX_UNLOCK(&PL_cred_mutex); /* XXX create separate mutex */
4025 Perl_cv_ckproto(pTHX_ CV *cv, GV *gv, char *p)
4029 if (((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) && ckWARN_d(WARN_UNSAFE)) {
4030 SV* msg = sv_newmortal();
4034 gv_efullname3(name = sv_newmortal(), gv, Nullch);
4035 sv_setpv(msg, "Prototype mismatch:");
4037 Perl_sv_catpvf(aTHX_ msg, " sub %_", name);
4039 Perl_sv_catpvf(aTHX_ msg, " (%s)", SvPVX(cv));
4040 sv_catpv(msg, " vs ");
4042 Perl_sv_catpvf(aTHX_ msg, "(%s)", p);
4044 sv_catpv(msg, "none");
4045 Perl_warner(aTHX_ WARN_UNSAFE, "%_", msg);
4050 Perl_cv_const_sv(pTHX_ CV *cv)
4052 if (!cv || !SvPOK(cv) || SvCUR(cv))
4054 return op_const_sv(CvSTART(cv), cv);
4058 Perl_op_const_sv(pTHX_ OP *o, CV *cv)
4065 if(o->op_type == OP_LINESEQ && cLISTOPo->op_first)
4066 o = cLISTOPo->op_first->op_sibling;
4068 for (; o; o = o->op_next) {
4069 OPCODE type = o->op_type;
4071 if (sv && o->op_next == o)
4073 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
4075 if (type == OP_LEAVESUB || type == OP_RETURN)
4079 if (type == OP_CONST)
4081 else if (type == OP_PADSV && cv) {
4082 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
4083 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
4084 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
4096 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4106 Perl_croak(aTHX_ "\"my sub\" not yet implemented");
4110 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
4112 return Perl_newATTRSUB(aTHX_ floor, o, proto, Nullop, block);
4116 Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4120 char *name = o ? SvPVx(cSVOPo->op_sv, n_a) : Nullch;
4121 GV *gv = gv_fetchpv(name ? name : "__ANON__",
4122 GV_ADDMULTI | ((block || attrs) ? 0 : GV_NOINIT),
4124 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, n_a) : Nullch;
4135 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
4136 maximum a prototype before. */
4137 if (SvTYPE(gv) > SVt_NULL) {
4138 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1)
4139 && ckWARN_d(WARN_UNSAFE))
4141 Perl_warner(aTHX_ WARN_UNSAFE, "Runaway prototype");
4143 cv_ckproto((CV*)gv, NULL, ps);
4146 sv_setpv((SV*)gv, ps);
4148 sv_setiv((SV*)gv, -1);
4149 SvREFCNT_dec(PL_compcv);
4150 cv = PL_compcv = NULL;
4151 PL_sub_generation++;
4155 if (!name || GvCVGEN(gv))
4157 else if (cv = GvCV(gv)) {
4158 cv_ckproto(cv, gv, ps);
4159 /* already defined (or promised)? */
4160 if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4162 bool const_changed = TRUE;
4163 if (!block && !attrs) {
4164 /* just a "sub foo;" when &foo is already defined */
4165 SAVEFREESV(PL_compcv);
4168 /* ahem, death to those who redefine active sort subs */
4169 if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv))
4170 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", name);
4173 if(const_sv = cv_const_sv(cv))
4174 const_changed = sv_cmp(const_sv, op_const_sv(block, Nullcv));
4175 if ((const_sv && const_changed) || ckWARN(WARN_REDEFINE)
4176 && !(CvGV(cv) && GvSTASH(CvGV(cv))
4177 && HvNAME(GvSTASH(CvGV(cv)))
4178 && strEQ(HvNAME(GvSTASH(CvGV(cv))),
4180 line_t oldline = PL_curcop->cop_line;
4181 PL_curcop->cop_line = PL_copline;
4182 Perl_warner(aTHX_ WARN_REDEFINE,
4183 const_sv ? "Constant subroutine %s redefined"
4184 : "Subroutine %s redefined", name);
4185 PL_curcop->cop_line = oldline;
4196 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>
4197 * before we clobber PL_compcv.
4201 if (CvGV(cv) && GvSTASH(CvGV(cv)) && HvNAME(GvSTASH(CvGV(cv))))
4202 stash = GvSTASH(CvGV(cv));
4203 else if (CvSTASH(cv) && HvNAME(CvSTASH(cv)))
4204 stash = CvSTASH(cv);
4206 stash = PL_curstash;
4209 /* possibly about to re-define existing subr -- ignore old cv */
4210 rcv = (SV*)PL_compcv;
4211 if (name && GvSTASH(gv) && HvNAME(GvSTASH(gv)))
4212 stash = GvSTASH(gv);
4214 stash = PL_curstash;
4216 apply_attrs(stash, rcv, attrs);
4218 if (cv) { /* must reuse cv if autoloaded */
4220 /* got here with just attrs -- work done, so bug out */
4221 SAVEFREESV(PL_compcv);
4225 CvFLAGS(cv) = CvFLAGS(PL_compcv);
4226 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
4227 CvOUTSIDE(PL_compcv) = 0;
4228 CvPADLIST(cv) = CvPADLIST(PL_compcv);
4229 CvPADLIST(PL_compcv) = 0;
4230 if (SvREFCNT(PL_compcv) > 1) /* XXX Make closures transit through stub. */
4231 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc((SV*)cv);
4232 SvREFCNT_dec(PL_compcv);
4239 PL_sub_generation++;
4242 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4243 CvFILEGV(cv) = PL_curcop->cop_filegv;
4244 CvSTASH(cv) = PL_curstash;
4247 if (!CvMUTEXP(cv)) {
4248 New(666, CvMUTEXP(cv), 1, perl_mutex);
4249 MUTEX_INIT(CvMUTEXP(cv));
4251 #endif /* USE_THREADS */
4254 sv_setpv((SV*)cv, ps);
4256 if (PL_error_count) {
4260 char *s = strrchr(name, ':');
4262 if (strEQ(s, "BEGIN")) {
4264 "BEGIN not safe after errors--compilation aborted";
4265 if (PL_in_eval & EVAL_KEEPERR)
4266 Perl_croak(aTHX_ not_safe);
4268 /* force display of errors found but not reported */
4269 sv_catpv(ERRSV, not_safe);
4270 Perl_croak(aTHX_ "%s", SvPVx(ERRSV, n_a));
4277 PL_copline = NOLINE;
4282 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad))
4283 av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv);
4286 SV **namep = AvARRAY(PL_comppad_name);
4287 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4290 if (SvIMMORTAL(PL_curpad[ix]))
4293 * The only things that a clonable function needs in its
4294 * pad are references to outer lexicals and anonymous subs.
4295 * The rest are created anew during cloning.
4297 if (!((namesv = namep[ix]) != Nullsv &&
4298 namesv != &PL_sv_undef &&
4300 *SvPVX(namesv) == '&')))
4302 SvREFCNT_dec(PL_curpad[ix]);
4303 PL_curpad[ix] = Nullsv;
4308 AV *av = newAV(); /* Will be @_ */
4310 av_store(PL_comppad, 0, (SV*)av);
4311 AvFLAGS(av) = AVf_REIFY;
4313 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4314 if (SvIMMORTAL(PL_curpad[ix]))
4316 if (!SvPADMY(PL_curpad[ix]))
4317 SvPADTMP_on(PL_curpad[ix]);
4322 CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0, scalarseq(block));
4325 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4327 CvSTART(cv) = LINKLIST(CvROOT(cv));
4328 CvROOT(cv)->op_next = 0;
4334 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4335 SV *sv = NEWSV(0,0);
4336 SV *tmpstr = sv_newmortal();
4337 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
4341 Perl_sv_setpvf(aTHX_ sv, "%_:%ld-%ld",
4342 GvSV(PL_curcop->cop_filegv),
4343 (long)PL_subline, (long)PL_curcop->cop_line);
4344 gv_efullname3(tmpstr, gv, Nullch);
4345 hv_store(GvHV(PL_DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
4346 hv = GvHVn(db_postponed);
4347 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
4348 && (cv = GvCV(db_postponed))) {
4353 call_sv((SV*)cv, G_DISCARD);
4357 if ((s = strrchr(name,':')))
4361 if (strEQ(s, "BEGIN")) {
4362 I32 oldscope = PL_scopestack_ix;
4364 SAVESPTR(PL_compiling.cop_filegv);
4365 SAVEI16(PL_compiling.cop_line);
4367 sv_setsv(PL_rs, PL_nrs);
4370 PL_beginav = newAV();
4371 DEBUG_x( dump_sub(gv) );
4372 av_push(PL_beginav, (SV *)cv);
4374 call_list(oldscope, PL_beginav);
4376 PL_curcop = &PL_compiling;
4377 PL_compiling.op_private = PL_hints;
4380 else if (strEQ(s, "END") && !PL_error_count) {
4383 av_unshift(PL_endav, 1);
4384 av_store(PL_endav, 0, (SV *)cv);
4387 else if (strEQ(s, "INIT") && !PL_error_count) {
4389 PL_initav = newAV();
4390 av_push(PL_initav, SvREFCNT_inc(cv));
4396 PL_copline = NOLINE;
4401 /* XXX unsafe for threads if eval_owner isn't held */
4403 Perl_newCONSTSUB(pTHX_ HV *stash, char *name, SV *sv)
4406 U32 oldhints = PL_hints;
4407 HV *old_cop_stash = PL_curcop->cop_stash;
4408 HV *old_curstash = PL_curstash;
4409 line_t oldline = PL_curcop->cop_line;
4410 PL_curcop->cop_line = PL_copline;
4412 PL_hints &= ~HINT_BLOCK_SCOPE;
4414 PL_curstash = PL_curcop->cop_stash = stash;
4417 start_subparse(FALSE, 0),
4418 newSVOP(OP_CONST, 0, newSVpv(name,0)),
4419 newSVOP(OP_CONST, 0, &PL_sv_no), /* SvPV(&PL_sv_no) == "" -- GMB */
4421 newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv))
4424 PL_hints = oldhints;
4425 PL_curcop->cop_stash = old_cop_stash;
4426 PL_curstash = old_curstash;
4427 PL_curcop->cop_line = oldline;
4431 Perl_newXS(pTHX_ char *name, XSUBADDR_t subaddr, char *filename)
4434 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
4437 if (cv = (name ? GvCV(gv) : Nullcv)) {
4439 /* just a cached method */
4443 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4444 /* already defined (or promised) */
4445 if (ckWARN(WARN_REDEFINE) && !(CvGV(cv) && GvSTASH(CvGV(cv))
4446 && HvNAME(GvSTASH(CvGV(cv)))
4447 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
4448 line_t oldline = PL_curcop->cop_line;
4449 if (PL_copline != NOLINE)
4450 PL_curcop->cop_line = PL_copline;
4451 Perl_warner(aTHX_ WARN_REDEFINE, "Subroutine %s redefined",name);
4452 PL_curcop->cop_line = oldline;
4459 if (cv) /* must reuse cv if autoloaded */
4462 cv = (CV*)NEWSV(1105,0);
4463 sv_upgrade((SV *)cv, SVt_PVCV);
4467 PL_sub_generation++;
4470 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4472 New(666, CvMUTEXP(cv), 1, perl_mutex);
4473 MUTEX_INIT(CvMUTEXP(cv));
4475 #endif /* USE_THREADS */
4476 CvFILEGV(cv) = gv_fetchfile(filename);
4477 CvXSUB(cv) = subaddr;
4480 char *s = strrchr(name,':');
4485 if (strEQ(s, "BEGIN")) {
4487 PL_beginav = newAV();
4488 av_push(PL_beginav, (SV *)cv);
4491 else if (strEQ(s, "END")) {
4494 av_unshift(PL_endav, 1);
4495 av_store(PL_endav, 0, (SV *)cv);
4498 else if (strEQ(s, "INIT")) {
4500 PL_initav = newAV();
4501 av_push(PL_initav, (SV *)cv);
4512 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
4522 name = SvPVx(cSVOPo->op_sv, n_a);
4525 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
4527 if (cv = GvFORM(gv)) {
4528 if (ckWARN(WARN_REDEFINE)) {
4529 line_t oldline = PL_curcop->cop_line;
4531 PL_curcop->cop_line = PL_copline;
4532 Perl_warner(aTHX_ WARN_REDEFINE, "Format %s redefined",name);
4533 PL_curcop->cop_line = oldline;
4539 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4540 CvFILEGV(cv) = PL_curcop->cop_filegv;
4542 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4543 if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix]))
4544 SvPADTMP_on(PL_curpad[ix]);
4547 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4548 CvSTART(cv) = LINKLIST(CvROOT(cv));
4549 CvROOT(cv)->op_next = 0;
4552 PL_copline = NOLINE;
4557 Perl_newANONLIST(pTHX_ OP *o)
4559 return newUNOP(OP_REFGEN, 0,
4560 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
4564 Perl_newANONHASH(pTHX_ OP *o)
4566 return newUNOP(OP_REFGEN, 0,
4567 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
4571 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
4573 return newANONATTRSUB(floor, proto, Nullop, block);
4577 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
4579 return newUNOP(OP_REFGEN, 0,
4580 newSVOP(OP_ANONCODE, 0,
4581 (SV*)newATTRSUB(floor, 0, proto, attrs, block)));
4585 Perl_oopsAV(pTHX_ OP *o)
4587 switch (o->op_type) {
4589 o->op_type = OP_PADAV;
4590 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4591 return ref(o, OP_RV2AV);
4594 o->op_type = OP_RV2AV;
4595 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4600 if (ckWARN_d(WARN_INTERNAL))
4601 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsAV");
4608 Perl_oopsHV(pTHX_ OP *o)
4612 switch (o->op_type) {
4615 o->op_type = OP_PADHV;
4616 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4617 return ref(o, OP_RV2HV);
4621 o->op_type = OP_RV2HV;
4622 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
4627 if (ckWARN_d(WARN_INTERNAL))
4628 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsHV");
4635 Perl_newAVREF(pTHX_ OP *o)
4637 if (o->op_type == OP_PADANY) {
4638 o->op_type = OP_PADAV;
4639 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4642 return newUNOP(OP_RV2AV, 0, scalar(o));
4646 Perl_newGVREF(pTHX_ I32 type, OP *o)
4648 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
4649 return newUNOP(OP_NULL, 0, o);
4650 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
4654 Perl_newHVREF(pTHX_ OP *o)
4656 if (o->op_type == OP_PADANY) {
4657 o->op_type = OP_PADHV;
4658 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4661 return newUNOP(OP_RV2HV, 0, scalar(o));
4665 Perl_oopsCV(pTHX_ OP *o)
4667 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
4673 Perl_newCVREF(pTHX_ I32 flags, OP *o)
4675 return newUNOP(OP_RV2CV, flags, scalar(o));
4679 Perl_newSVREF(pTHX_ OP *o)
4681 if (o->op_type == OP_PADANY) {
4682 o->op_type = OP_PADSV;
4683 o->op_ppaddr = PL_ppaddr[OP_PADSV];
4686 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
4687 o->op_flags |= OPpDONE_SVREF;
4690 return newUNOP(OP_RV2SV, 0, scalar(o));
4693 /* Check routines. */
4696 Perl_ck_anoncode(pTHX_ OP *o)
4701 name = NEWSV(1106,0);
4702 sv_upgrade(name, SVt_PVNV);
4703 sv_setpvn(name, "&", 1);
4706 ix = pad_alloc(o->op_type, SVs_PADMY);
4707 av_store(PL_comppad_name, ix, name);
4708 av_store(PL_comppad, ix, cSVOPo->op_sv);
4709 SvPADMY_on(cSVOPo->op_sv);
4710 cSVOPo->op_sv = Nullsv;
4711 cSVOPo->op_targ = ix;
4716 Perl_ck_bitop(pTHX_ OP *o)
4718 o->op_private = PL_hints;
4723 Perl_ck_concat(pTHX_ OP *o)
4725 if (cUNOPo->op_first->op_type == OP_CONCAT)
4726 o->op_flags |= OPf_STACKED;
4731 Perl_ck_spair(pTHX_ OP *o)
4733 if (o->op_flags & OPf_KIDS) {
4736 OPCODE type = o->op_type;
4737 o = modkids(ck_fun(o), type);
4738 kid = cUNOPo->op_first;
4739 newop = kUNOP->op_first->op_sibling;
4741 (newop->op_sibling ||
4742 !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
4743 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
4744 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
4748 op_free(kUNOP->op_first);
4749 kUNOP->op_first = newop;
4751 o->op_ppaddr = PL_ppaddr[++o->op_type];
4756 Perl_ck_delete(pTHX_ OP *o)
4760 if (o->op_flags & OPf_KIDS) {
4761 OP *kid = cUNOPo->op_first;
4762 if (kid->op_type == OP_HSLICE)
4763 o->op_private |= OPpSLICE;
4764 else if (kid->op_type != OP_HELEM)
4765 Perl_croak(aTHX_ "%s argument is not a HASH element or slice",
4766 PL_op_desc[o->op_type]);
4773 Perl_ck_eof(pTHX_ OP *o)
4775 I32 type = o->op_type;
4777 if (o->op_flags & OPf_KIDS) {
4778 if (cLISTOPo->op_first->op_type == OP_STUB) {
4780 o = newUNOP(type, OPf_SPECIAL,
4781 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
4789 Perl_ck_eval(pTHX_ OP *o)
4791 PL_hints |= HINT_BLOCK_SCOPE;
4792 if (o->op_flags & OPf_KIDS) {
4793 SVOP *kid = (SVOP*)cUNOPo->op_first;
4796 o->op_flags &= ~OPf_KIDS;
4799 else if (kid->op_type == OP_LINESEQ) {
4802 kid->op_next = o->op_next;
4803 cUNOPo->op_first = 0;
4806 NewOp(1101, enter, 1, LOGOP);
4807 enter->op_type = OP_ENTERTRY;
4808 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
4809 enter->op_private = 0;
4811 /* establish postfix order */
4812 enter->op_next = (OP*)enter;
4814 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
4815 o->op_type = OP_LEAVETRY;
4816 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
4817 enter->op_other = o;
4825 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
4827 o->op_targ = (PADOFFSET)PL_hints;
4832 Perl_ck_exec(pTHX_ OP *o)
4835 if (o->op_flags & OPf_STACKED) {
4837 kid = cUNOPo->op_first->op_sibling;
4838 if (kid->op_type == OP_RV2GV)
4847 Perl_ck_exists(pTHX_ OP *o)
4850 if (o->op_flags & OPf_KIDS) {
4851 OP *kid = cUNOPo->op_first;
4852 if (kid->op_type != OP_HELEM)
4853 Perl_croak(aTHX_ "%s argument is not a HASH element", PL_op_desc[o->op_type]);
4861 Perl_ck_gvconst(pTHX_ register OP *o)
4863 o = fold_constants(o);
4864 if (o->op_type == OP_CONST)
4871 Perl_ck_rvconst(pTHX_ register OP *o)
4874 SVOP *kid = (SVOP*)cUNOPo->op_first;
4876 o->op_private |= (PL_hints & HINT_STRICT_REFS);
4877 if (kid->op_type == OP_CONST) {
4881 SV *kidsv = kid->op_sv;
4884 /* Is it a constant from cv_const_sv()? */
4885 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
4886 SV *rsv = SvRV(kidsv);
4887 int svtype = SvTYPE(rsv);
4888 char *badtype = Nullch;
4890 switch (o->op_type) {
4892 if (svtype > SVt_PVMG)
4893 badtype = "a SCALAR";
4896 if (svtype != SVt_PVAV)
4897 badtype = "an ARRAY";
4900 if (svtype != SVt_PVHV) {
4901 if (svtype == SVt_PVAV) { /* pseudohash? */
4902 SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
4903 if (ksv && SvROK(*ksv)
4904 && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
4913 if (svtype != SVt_PVCV)
4918 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
4921 name = SvPV(kidsv, n_a);
4922 if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
4923 char *badthing = Nullch;
4924 switch (o->op_type) {
4926 badthing = "a SCALAR";
4929 badthing = "an ARRAY";
4932 badthing = "a HASH";
4937 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
4941 * This is a little tricky. We only want to add the symbol if we
4942 * didn't add it in the lexer. Otherwise we get duplicate strict
4943 * warnings. But if we didn't add it in the lexer, we must at
4944 * least pretend like we wanted to add it even if it existed before,
4945 * or we get possible typo warnings. OPpCONST_ENTERED says
4946 * whether the lexer already added THIS instance of this symbol.
4948 iscv = (o->op_type == OP_RV2CV) * 2;
4950 gv = gv_fetchpv(name,
4951 iscv | !(kid->op_private & OPpCONST_ENTERED),
4954 : o->op_type == OP_RV2SV
4956 : o->op_type == OP_RV2AV
4958 : o->op_type == OP_RV2HV
4961 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
4963 kid->op_type = OP_GV;
4964 SvREFCNT_dec(kid->op_sv);
4965 kid->op_sv = SvREFCNT_inc(gv);
4966 kid->op_ppaddr = PL_ppaddr[OP_GV];
4973 Perl_ck_ftst(pTHX_ OP *o)
4976 I32 type = o->op_type;
4978 if (o->op_flags & OPf_REF)
4981 if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
4982 SVOP *kid = (SVOP*)cUNOPo->op_first;
4984 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4986 OP *newop = newGVOP(type, OPf_REF,
4987 gv_fetchpv(SvPVx(kid->op_sv, n_a), TRUE, SVt_PVIO));
4994 if (type == OP_FTTTY)
4995 return newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
4998 return newUNOP(type, 0, newDEFSVOP());
5004 Perl_ck_fun(pTHX_ OP *o)
5011 int type = o->op_type;
5012 register I32 oa = PL_opargs[type] >> OASHIFT;
5014 if (o->op_flags & OPf_STACKED) {
5015 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
5018 return no_fh_allowed(o);
5021 if (o->op_flags & OPf_KIDS) {
5023 tokid = &cLISTOPo->op_first;
5024 kid = cLISTOPo->op_first;
5025 if (kid->op_type == OP_PUSHMARK ||
5026 kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
5028 tokid = &kid->op_sibling;
5029 kid = kid->op_sibling;
5031 if (!kid && PL_opargs[type] & OA_DEFGV)
5032 *tokid = kid = newDEFSVOP();
5036 sibl = kid->op_sibling;
5039 /* list seen where single (scalar) arg expected? */
5040 if (numargs == 1 && !(oa >> 4)
5041 && kid->op_type == OP_LIST && type != OP_SCALAR)
5043 return too_many_arguments(o,PL_op_desc[type]);
5056 if (kid->op_type == OP_CONST &&
5057 (kid->op_private & OPpCONST_BARE))
5059 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5060 OP *newop = newAVREF(newGVOP(OP_GV, 0,
5061 gv_fetchpv(name, TRUE, SVt_PVAV) ));
5062 if (ckWARN(WARN_SYNTAX))
5063 Perl_warner(aTHX_ WARN_SYNTAX,
5064 "Array @%s missing the @ in argument %"IVdf" of %s()",
5065 name, (IV)numargs, PL_op_desc[type]);
5068 kid->op_sibling = sibl;
5071 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
5072 bad_type(numargs, "array", PL_op_desc[type], kid);
5076 if (kid->op_type == OP_CONST &&
5077 (kid->op_private & OPpCONST_BARE))
5079 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5080 OP *newop = newHVREF(newGVOP(OP_GV, 0,
5081 gv_fetchpv(name, TRUE, SVt_PVHV) ));
5082 if (ckWARN(WARN_SYNTAX))
5083 Perl_warner(aTHX_ WARN_SYNTAX,
5084 "Hash %%%s missing the %% in argument %"IVdf" of %s()",
5085 name, (IV)numargs, PL_op_desc[type]);
5088 kid->op_sibling = sibl;
5091 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
5092 bad_type(numargs, "hash", PL_op_desc[type], kid);
5097 OP *newop = newUNOP(OP_NULL, 0, kid);
5098 kid->op_sibling = 0;
5100 newop->op_next = newop;
5102 kid->op_sibling = sibl;
5107 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
5108 if (kid->op_type == OP_CONST &&
5109 (kid->op_private & OPpCONST_BARE))
5111 OP *newop = newGVOP(OP_GV, 0,
5112 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, n_a), TRUE,
5117 else if (kid->op_type == OP_READLINE) {
5118 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
5119 bad_type(numargs, "HANDLE", PL_op_desc[o->op_type], kid);
5122 I32 flags = OPf_SPECIAL;
5124 /* is this op a FH constructor? */
5125 if (is_handle_constructor(o,numargs)) {
5127 /* Set a flag to tell rv2gv to vivify
5128 * need to "prove" flag does not mean something
5129 * else already - NI-S 1999/05/07
5133 /* Helps with open($array[$n],...)
5134 but is too simplistic - need to do selectively
5139 kid->op_sibling = 0;
5140 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
5142 kid->op_private |= priv;
5145 kid->op_sibling = sibl;
5151 mod(scalar(kid), type);
5155 tokid = &kid->op_sibling;
5156 kid = kid->op_sibling;
5158 o->op_private |= numargs;
5160 return too_many_arguments(o,PL_op_desc[o->op_type]);
5163 else if (PL_opargs[type] & OA_DEFGV) {
5165 return newUNOP(type, 0, newDEFSVOP());
5169 while (oa & OA_OPTIONAL)
5171 if (oa && oa != OA_LIST)
5172 return too_few_arguments(o,PL_op_desc[o->op_type]);
5178 Perl_ck_glob(pTHX_ OP *o)
5182 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
5183 append_elem(OP_GLOB, o, newDEFSVOP());
5185 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
5186 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5188 #if defined(PERL_INTERNAL_GLOB) && !defined(MINIPERL_BUILD)
5189 /* XXX this can be tightened up and made more failsafe. */
5191 OP *modname = newSVOP(OP_CONST, 0, newSVpvn("File::Glob", 10));
5192 modname->op_private |= OPpCONST_BARE;
5194 utilize(1, start_subparse(FALSE, 0), Nullop, modname,
5195 newSVOP(OP_CONST, 0, newSVpvn("globally", 8)));
5196 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5199 #endif /* PERL_INTERNAL_GLOB && !MINIPERL_BUILD */
5201 if (gv && GvIMPORTED_CV(gv)) {
5202 append_elem(OP_GLOB, o,
5203 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
5204 o->op_type = OP_LIST;
5205 o->op_ppaddr = PL_ppaddr[OP_LIST];
5206 cLISTOPo->op_first->op_type = OP_PUSHMARK;
5207 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
5208 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
5209 append_elem(OP_LIST, o,
5210 scalar(newUNOP(OP_RV2CV, 0,
5211 newGVOP(OP_GV, 0, gv)))));
5212 o = newUNOP(OP_NULL, 0, ck_subr(o));
5213 o->op_targ = OP_GLOB; /* hint at what it used to be */
5216 gv = newGVgen("main");
5218 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
5224 Perl_ck_grep(pTHX_ OP *o)
5228 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
5230 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
5231 NewOp(1101, gwop, 1, LOGOP);
5233 if (o->op_flags & OPf_STACKED) {
5236 kid = cLISTOPo->op_first->op_sibling;
5237 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
5240 kid->op_next = (OP*)gwop;
5241 o->op_flags &= ~OPf_STACKED;
5243 kid = cLISTOPo->op_first->op_sibling;
5244 if (type == OP_MAPWHILE)
5251 kid = cLISTOPo->op_first->op_sibling;
5252 if (kid->op_type != OP_NULL)
5253 Perl_croak(aTHX_ "panic: ck_grep");
5254 kid = kUNOP->op_first;
5256 gwop->op_type = type;
5257 gwop->op_ppaddr = PL_ppaddr[type];
5258 gwop->op_first = listkids(o);
5259 gwop->op_flags |= OPf_KIDS;
5260 gwop->op_private = 1;
5261 gwop->op_other = LINKLIST(kid);
5262 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
5263 kid->op_next = (OP*)gwop;
5265 kid = cLISTOPo->op_first->op_sibling;
5266 if (!kid || !kid->op_sibling)
5267 return too_few_arguments(o,PL_op_desc[o->op_type]);
5268 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
5269 mod(kid, OP_GREPSTART);
5275 Perl_ck_index(pTHX_ OP *o)
5277 if (o->op_flags & OPf_KIDS) {
5278 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5280 kid = kid->op_sibling; /* get past "big" */
5281 if (kid && kid->op_type == OP_CONST)
5282 fbm_compile(((SVOP*)kid)->op_sv, 0);
5288 Perl_ck_lengthconst(pTHX_ OP *o)
5290 /* XXX length optimization goes here */
5295 Perl_ck_lfun(pTHX_ OP *o)
5297 OPCODE type = o->op_type;
5298 return modkids(ck_fun(o), type);
5302 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
5305 if ((o->op_flags & OPf_KIDS) && ckWARN(WARN_DEPRECATED)) {
5306 switch (cUNOPo->op_first->op_type) {
5308 break; /* Globals via GV can be undef */
5310 case OP_AASSIGN: /* Is this a good idea? */
5311 Perl_warner(aTHX_ WARN_DEPRECATED,
5312 "defined(@array) is deprecated");
5313 Perl_warner(aTHX_ WARN_DEPRECATED,
5314 "(Maybe you should just omit the defined()?)\n");
5317 break; /* Globals via GV can be undef */
5319 Perl_warner(aTHX_ WARN_DEPRECATED,
5320 "defined(%hash) is deprecated");
5321 Perl_warner(aTHX_ WARN_DEPRECATED,
5322 "(Maybe you should just omit the defined()?)\n");
5333 Perl_ck_rfun(pTHX_ OP *o)
5335 OPCODE type = o->op_type;
5336 return refkids(ck_fun(o), type);
5340 Perl_ck_listiob(pTHX_ OP *o)
5344 kid = cLISTOPo->op_first;
5347 kid = cLISTOPo->op_first;
5349 if (kid->op_type == OP_PUSHMARK)
5350 kid = kid->op_sibling;
5351 if (kid && o->op_flags & OPf_STACKED)
5352 kid = kid->op_sibling;
5353 else if (kid && !kid->op_sibling) { /* print HANDLE; */
5354 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
5355 o->op_flags |= OPf_STACKED; /* make it a filehandle */
5356 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
5357 cLISTOPo->op_first->op_sibling = kid;
5358 cLISTOPo->op_last = kid;
5359 kid = kid->op_sibling;
5364 append_elem(o->op_type, o, newDEFSVOP());
5370 if (PL_hints & HINT_LOCALE)
5371 o->op_private |= OPpLOCALE;
5378 Perl_ck_fun_locale(pTHX_ OP *o)
5384 if (PL_hints & HINT_LOCALE)
5385 o->op_private |= OPpLOCALE;
5392 Perl_ck_sassign(pTHX_ OP *o)
5394 OP *kid = cLISTOPo->op_first;
5395 /* has a disposable target? */
5396 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
5397 && !(kid->op_flags & OPf_STACKED))
5399 OP *kkid = kid->op_sibling;
5401 /* Can just relocate the target. */
5402 if (kkid && kkid->op_type == OP_PADSV
5403 && !(kkid->op_private & OPpLVAL_INTRO))
5405 /* Concat has problems if target is equal to right arg. */
5406 if (kid->op_type == OP_CONCAT) {
5407 if (kLISTOP->op_first->op_sibling->op_type == OP_PADSV
5408 && kLISTOP->op_first->op_sibling->op_targ == kkid->op_targ)
5411 else if (kid->op_type == OP_JOIN) {
5412 /* do_join has problems if the arguments coincide with target.
5413 In fact the second argument *can* safely coincide,
5414 but ignore=pessimize this rare occasion. */
5415 OP *arg = kLISTOP->op_first->op_sibling; /* Skip PUSHMARK */
5418 if (arg->op_type == OP_PADSV
5419 && arg->op_targ == kkid->op_targ)
5421 arg = arg->op_sibling;
5424 else if (kid->op_type == OP_QUOTEMETA) {
5425 /* quotemeta has problems if the argument coincides with target. */
5426 if (kLISTOP->op_first->op_type == OP_PADSV
5427 && kLISTOP->op_first->op_targ == kkid->op_targ)
5430 kid->op_targ = kkid->op_targ;
5431 /* Now we do not need PADSV and SASSIGN. */
5432 kid->op_sibling = o->op_sibling; /* NULL */
5433 cLISTOPo->op_first = NULL;
5436 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
5444 Perl_ck_scmp(pTHX_ OP *o)
5448 if (PL_hints & HINT_LOCALE)
5449 o->op_private |= OPpLOCALE;
5456 Perl_ck_match(pTHX_ OP *o)
5458 o->op_private |= OPpRUNTIME;
5463 Perl_ck_method(pTHX_ OP *o)
5465 OP *kid = cUNOPo->op_first;
5466 if (kid->op_type == OP_CONST) {
5467 SV* sv = kSVOP->op_sv;
5468 if (!(strchr(SvPVX(sv), ':') || strchr(SvPVX(sv), '\''))) {
5470 sv_upgrade(sv, SVt_PVIV);
5472 PERL_HASH(SvUVX(sv), SvPVX(sv), SvCUR(sv));
5473 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
5474 kSVOP->op_sv = Nullsv;
5483 Perl_ck_null(pTHX_ OP *o)
5489 Perl_ck_repeat(pTHX_ OP *o)
5491 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
5492 o->op_private |= OPpREPEAT_DOLIST;
5493 cBINOPo->op_first = force_list(cBINOPo->op_first);
5501 Perl_ck_require(pTHX_ OP *o)
5503 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
5504 SVOP *kid = (SVOP*)cUNOPo->op_first;
5506 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5508 for (s = SvPVX(kid->op_sv); *s; s++) {
5509 if (*s == ':' && s[1] == ':') {
5511 Move(s+2, s+1, strlen(s+2)+1, char);
5512 --SvCUR(kid->op_sv);
5515 sv_catpvn(kid->op_sv, ".pm", 3);
5523 Perl_ck_retarget(pTHX_ OP *o)
5525 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
5532 Perl_ck_select(pTHX_ OP *o)
5535 if (o->op_flags & OPf_KIDS) {
5536 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5537 if (kid && kid->op_sibling) {
5538 o->op_type = OP_SSELECT;
5539 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
5541 return fold_constants(o);
5545 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5546 if (kid && kid->op_type == OP_RV2GV)
5547 kid->op_private &= ~HINT_STRICT_REFS;
5552 Perl_ck_shift(pTHX_ OP *o)
5554 I32 type = o->op_type;
5556 if (!(o->op_flags & OPf_KIDS)) {
5561 if (!CvUNIQUE(PL_compcv)) {
5562 argop = newOP(OP_PADAV, OPf_REF);
5563 argop->op_targ = 0; /* PL_curpad[0] is @_ */
5566 argop = newUNOP(OP_RV2AV, 0,
5567 scalar(newGVOP(OP_GV, 0,
5568 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5571 argop = newUNOP(OP_RV2AV, 0,
5572 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(PL_compcv) ?
5573 PL_defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5574 #endif /* USE_THREADS */
5575 return newUNOP(type, 0, scalar(argop));
5577 return scalar(modkids(ck_fun(o), type));
5581 Perl_ck_sort(pTHX_ OP *o)
5585 if (PL_hints & HINT_LOCALE)
5586 o->op_private |= OPpLOCALE;
5589 if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
5591 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
5592 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5594 kid = kUNOP->op_first; /* get past null */
5596 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
5598 if (kid->op_type == OP_SCOPE) {
5602 else if (kid->op_type == OP_LEAVE) {
5603 if (o->op_type == OP_SORT) {
5604 null(kid); /* wipe out leave */
5607 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
5608 if (k->op_next == kid)
5613 kid->op_next = 0; /* just disconnect the leave */
5614 k = kLISTOP->op_first;
5618 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5619 if (o->op_type == OP_SORT)
5623 o->op_flags |= OPf_SPECIAL;
5625 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
5626 null(cLISTOPo->op_first->op_sibling);
5633 S_simplify_sort(pTHX_ OP *o)
5636 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5639 if (!(o->op_flags & OPf_STACKED))
5641 GvMULTI_on(gv_fetchpv("a", TRUE, SVt_PV));
5642 GvMULTI_on(gv_fetchpv("b", TRUE, SVt_PV));
5643 kid = kUNOP->op_first; /* get past null */
5644 if (kid->op_type != OP_SCOPE)
5646 kid = kLISTOP->op_last; /* get past scope */
5647 switch(kid->op_type) {
5655 k = kid; /* remember this node*/
5656 if (kBINOP->op_first->op_type != OP_RV2SV)
5658 kid = kBINOP->op_first; /* get past cmp */
5659 if (kUNOP->op_first->op_type != OP_GV)
5661 kid = kUNOP->op_first; /* get past rv2sv */
5662 if (GvSTASH(kGVOP->op_gv) != PL_curstash)
5664 if (strEQ(GvNAME(kGVOP->op_gv), "a"))
5666 else if(strEQ(GvNAME(kGVOP->op_gv), "b"))
5670 kid = k; /* back to cmp */
5671 if (kBINOP->op_last->op_type != OP_RV2SV)
5673 kid = kBINOP->op_last; /* down to 2nd arg */
5674 if (kUNOP->op_first->op_type != OP_GV)
5676 kid = kUNOP->op_first; /* get past rv2sv */
5677 if (GvSTASH(kGVOP->op_gv) != PL_curstash
5679 ? strNE(GvNAME(kGVOP->op_gv), "a")
5680 : strNE(GvNAME(kGVOP->op_gv), "b")))
5682 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
5684 o->op_private |= OPpSORT_REVERSE;
5685 if (k->op_type == OP_NCMP)
5686 o->op_private |= OPpSORT_NUMERIC;
5687 if (k->op_type == OP_I_NCMP)
5688 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
5689 kid = cLISTOPo->op_first->op_sibling;
5690 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
5691 op_free(kid); /* then delete it */
5692 cLISTOPo->op_children--;
5696 Perl_ck_split(pTHX_ OP *o)
5700 if (o->op_flags & OPf_STACKED)
5701 return no_fh_allowed(o);
5703 kid = cLISTOPo->op_first;
5704 if (kid->op_type != OP_NULL)
5705 Perl_croak(aTHX_ "panic: ck_split");
5706 kid = kid->op_sibling;
5707 op_free(cLISTOPo->op_first);
5708 cLISTOPo->op_first = kid;
5710 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvn(" ", 1));
5711 cLISTOPo->op_last = kid; /* There was only one element previously */
5714 if (kid->op_type != OP_MATCH) {
5715 OP *sibl = kid->op_sibling;
5716 kid->op_sibling = 0;
5717 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
5718 if (cLISTOPo->op_first == cLISTOPo->op_last)
5719 cLISTOPo->op_last = kid;
5720 cLISTOPo->op_first = kid;
5721 kid->op_sibling = sibl;
5724 kid->op_type = OP_PUSHRE;
5725 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
5728 if (!kid->op_sibling)
5729 append_elem(OP_SPLIT, o, newDEFSVOP());
5731 kid = kid->op_sibling;
5734 if (!kid->op_sibling)
5735 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
5737 kid = kid->op_sibling;
5740 if (kid->op_sibling)
5741 return too_many_arguments(o,PL_op_desc[o->op_type]);
5747 Perl_ck_join(pTHX_ OP *o)
5749 if (ckWARN(WARN_SYNTAX)) {
5750 OP *kid = cLISTOPo->op_first->op_sibling;
5751 if (kid && kid->op_type == OP_MATCH) {
5752 char *pmstr = "STRING";
5753 if (kPMOP->op_pmregexp)
5754 pmstr = kPMOP->op_pmregexp->precomp;
5755 Perl_warner(aTHX_ WARN_SYNTAX,
5756 "/%s/ should probably be written as \"%s\"",
5764 Perl_ck_subr(pTHX_ OP *o)
5767 OP *prev = ((cUNOPo->op_first->op_sibling)
5768 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
5769 OP *o2 = prev->op_sibling;
5778 o->op_private |= OPpENTERSUB_HASTARG;
5779 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
5780 if (cvop->op_type == OP_RV2CV) {
5782 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
5783 null(cvop); /* disable rv2cv */
5784 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
5785 if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) {
5786 cv = GvCVu(tmpop->op_sv);
5788 tmpop->op_private |= OPpEARLY_CV;
5789 else if (SvPOK(cv)) {
5790 namegv = CvANON(cv) ? (GV*)tmpop->op_sv : CvGV(cv);
5791 proto = SvPV((SV*)cv, n_a);
5795 else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
5796 if (o2->op_type == OP_CONST)
5797 o2->op_private &= ~OPpCONST_STRICT;
5798 else if (o2->op_type == OP_LIST) {
5799 OP *o = ((UNOP*)o2)->op_first->op_sibling;
5800 if (o && o->op_type == OP_CONST)
5801 o->op_private &= ~OPpCONST_STRICT;
5804 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5805 if (PERLDB_SUB && PL_curstash != PL_debstash)
5806 o->op_private |= OPpENTERSUB_DB;
5807 while (o2 != cvop) {
5811 return too_many_arguments(o, gv_ename(namegv));
5829 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
5830 bad_type(arg, "block", gv_ename(namegv), o2);
5833 /* '*' allows any scalar type, including bareword */
5836 if (o2->op_type == OP_RV2GV)
5837 goto wrapref; /* autoconvert GLOB -> GLOBref */
5838 else if (o2->op_type == OP_CONST)
5839 o2->op_private &= ~OPpCONST_STRICT;
5840 else if (o2->op_type == OP_ENTERSUB) {
5841 /* accidental subroutine, revert to bareword */
5842 OP *gvop = ((UNOP*)o2)->op_first;
5843 if (gvop && gvop->op_type == OP_NULL) {
5844 gvop = ((UNOP*)gvop)->op_first;
5846 for (; gvop->op_sibling; gvop = gvop->op_sibling)
5849 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
5850 (gvop = ((UNOP*)gvop)->op_first) &&
5851 gvop->op_type == OP_GV)
5853 GV *gv = (GV*)((SVOP*)gvop)->op_sv;
5854 OP *sibling = o2->op_sibling;
5855 SV *n = newSVpvn("",0);
5857 gv_fullname3(n, gv, "");
5858 if (SvCUR(n)>6 && strnEQ(SvPVX(n),"main::",6))
5859 sv_chop(n, SvPVX(n)+6);
5860 o2 = newSVOP(OP_CONST, 0, n);
5861 prev->op_sibling = o2;
5862 o2->op_sibling = sibling;
5874 if (o2->op_type != OP_RV2GV)
5875 bad_type(arg, "symbol", gv_ename(namegv), o2);
5878 if (o2->op_type != OP_RV2CV)
5879 bad_type(arg, "sub", gv_ename(namegv), o2);
5882 if (o2->op_type != OP_RV2SV
5883 && o2->op_type != OP_PADSV
5884 && o2->op_type != OP_HELEM
5885 && o2->op_type != OP_AELEM
5886 && o2->op_type != OP_THREADSV)
5888 bad_type(arg, "scalar", gv_ename(namegv), o2);
5892 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
5893 bad_type(arg, "array", gv_ename(namegv), o2);
5896 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
5897 bad_type(arg, "hash", gv_ename(namegv), o2);
5901 OP* sib = kid->op_sibling;
5902 kid->op_sibling = 0;
5903 o2 = newUNOP(OP_REFGEN, 0, kid);
5904 o2->op_sibling = sib;
5905 prev->op_sibling = o2;
5916 Perl_croak(aTHX_ "Malformed prototype for %s: %s",
5917 gv_ename(namegv), SvPV((SV*)cv, n_a));
5922 mod(o2, OP_ENTERSUB);
5924 o2 = o2->op_sibling;
5926 if (proto && !optional &&
5927 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
5928 return too_few_arguments(o, gv_ename(namegv));
5933 Perl_ck_svconst(pTHX_ OP *o)
5935 SvREADONLY_on(cSVOPo->op_sv);
5940 Perl_ck_trunc(pTHX_ OP *o)
5942 if (o->op_flags & OPf_KIDS) {
5943 SVOP *kid = (SVOP*)cUNOPo->op_first;
5945 if (kid->op_type == OP_NULL)
5946 kid = (SVOP*)kid->op_sibling;
5947 if (kid && kid->op_type == OP_CONST &&
5948 (kid->op_private & OPpCONST_BARE))
5950 o->op_flags |= OPf_SPECIAL;
5951 kid->op_private &= ~OPpCONST_STRICT;
5957 /* A peephole optimizer. We visit the ops in the order they're to execute. */
5960 Perl_peep(pTHX_ register OP *o)
5963 register OP* oldop = 0;
5965 OP *last_composite = Nullop;
5967 if (!o || o->op_seq)
5971 SAVESPTR(PL_curcop);
5972 for (; o; o = o->op_next) {
5978 switch (o->op_type) {
5982 PL_curcop = ((COP*)o); /* for warnings */
5983 o->op_seq = PL_op_seqmax++;
5984 last_composite = Nullop;
5988 if (cSVOPo->op_private & OPpCONST_STRICT)
5989 no_bareword_allowed(o);
5995 if ( o->op_next && o->op_next->op_type == OP_STRINGIFY
5996 && !(o->op_next->op_private & OPpTARGET_MY) )
5998 o->op_seq = PL_op_seqmax++;
6003 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
6004 if (o->op_next->op_private & OPpTARGET_MY) {
6005 if ((o->op_flags & OPf_STACKED) /* chained concats */
6006 || (o->op_type == OP_CONCAT
6007 /* Concat has problems if target is equal to right arg. */
6008 && (((LISTOP*)o)->op_first->op_sibling->op_type
6010 && (((LISTOP*)o)->op_first->op_sibling->op_targ
6011 == o->op_next->op_targ))) {
6012 goto ignore_optimization;
6015 o->op_targ = o->op_next->op_targ;
6016 o->op_private |= OPpTARGET_MY;
6021 ignore_optimization:
6022 o->op_seq = PL_op_seqmax++;
6025 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
6026 o->op_seq = PL_op_seqmax++;
6027 break; /* Scalar stub must produce undef. List stub is noop */
6031 if (o->op_targ == OP_NEXTSTATE
6032 || o->op_targ == OP_DBSTATE
6033 || o->op_targ == OP_SETSTATE)
6035 PL_curcop = ((COP*)o);
6042 if (oldop && o->op_next) {
6043 oldop->op_next = o->op_next;
6046 o->op_seq = PL_op_seqmax++;
6050 if (o->op_next->op_type == OP_RV2SV) {
6051 if (!(o->op_next->op_private & OPpDEREF)) {
6053 o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
6054 o->op_next = o->op_next->op_next;
6055 o->op_type = OP_GVSV;
6056 o->op_ppaddr = PL_ppaddr[OP_GVSV];
6059 else if (o->op_next->op_type == OP_RV2AV) {
6060 OP* pop = o->op_next->op_next;
6062 if (pop->op_type == OP_CONST &&
6063 (PL_op = pop->op_next) &&
6064 pop->op_next->op_type == OP_AELEM &&
6065 !(pop->op_next->op_private &
6066 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
6067 (i = SvIV(((SVOP*)pop)->op_sv) - PL_compiling.cop_arybase)
6074 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
6075 o->op_next = pop->op_next->op_next;
6076 o->op_type = OP_AELEMFAST;
6077 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
6078 o->op_private = (U8)i;
6079 GvAVn(((GVOP*)o)->op_gv);
6082 else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_UNSAFE)) {
6083 GV *gv = cGVOPo->op_gv;
6084 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX(GvCV(gv))) {
6085 /* XXX could check prototype here instead of just carping */
6086 SV *sv = sv_newmortal();
6087 gv_efullname3(sv, gv, Nullch);
6088 Perl_warner(aTHX_ WARN_UNSAFE,
6089 "%s() called too early to check prototype",
6094 o->op_seq = PL_op_seqmax++;
6105 o->op_seq = PL_op_seqmax++;
6106 while (cLOGOP->op_other->op_type == OP_NULL)
6107 cLOGOP->op_other = cLOGOP->op_other->op_next;
6108 peep(cLOGOP->op_other);
6112 o->op_seq = PL_op_seqmax++;
6113 peep(cLOOP->op_redoop);
6114 peep(cLOOP->op_nextop);
6115 peep(cLOOP->op_lastop);
6121 o->op_seq = PL_op_seqmax++;
6122 peep(cPMOP->op_pmreplstart);
6126 o->op_seq = PL_op_seqmax++;
6127 if (ckWARN(WARN_SYNTAX) && o->op_next
6128 && o->op_next->op_type == OP_NEXTSTATE) {
6129 if (o->op_next->op_sibling &&
6130 o->op_next->op_sibling->op_type != OP_EXIT &&
6131 o->op_next->op_sibling->op_type != OP_WARN &&
6132 o->op_next->op_sibling->op_type != OP_DIE) {
6133 line_t oldline = PL_curcop->cop_line;
6135 PL_curcop->cop_line = ((COP*)o->op_next)->cop_line;
6136 Perl_warner(aTHX_ WARN_SYNTAX, "Statement unlikely to be reached");
6137 Perl_warner(aTHX_ WARN_SYNTAX, "(Maybe you meant system() when you said exec()?)\n");
6138 PL_curcop->cop_line = oldline;
6152 if ((o->op_private & (OPpLVAL_INTRO))
6153 || ((BINOP*)o)->op_last->op_type != OP_CONST)
6155 rop = (UNOP*)((BINOP*)o)->op_first;
6156 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6158 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6159 if (!SvOBJECT(lexname))
6161 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6162 if (!fields || !GvHV(*fields))
6164 svp = &((SVOP*)((BINOP*)o)->op_last)->op_sv;
6165 key = SvPV(*svp, keylen);
6166 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
6168 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" in variable %s of type %s",
6169 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
6171 ind = SvIV(*indsvp);
6173 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6174 rop->op_type = OP_RV2AV;
6175 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6176 o->op_type = OP_AELEM;
6177 o->op_ppaddr = PL_ppaddr[OP_AELEM];
6179 *svp = newSViv(ind);
6185 if (!(o->op_flags & OPf_WANT)
6186 || (o->op_flags & OPf_WANT) == OPf_WANT_LIST)
6190 o->op_seq = PL_op_seqmax++;
6194 if (o->op_next && o->op_next->op_type != OP_LEAVESUBLV) {
6195 o->op_seq = PL_op_seqmax++;
6201 if (last_composite) {
6202 OP *r = last_composite;
6204 while (r->op_sibling)
6207 || (r->op_next->op_type == OP_LIST
6208 && r->op_next->op_next == o))
6210 if (last_composite->op_type == OP_RV2AV)
6211 yyerror("Lvalue subs returning arrays not implemented yet");
6213 yyerror("Lvalue subs returning hashes not implemented yet");
6220 o->op_seq = PL_op_seqmax++;