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
22 /* #define PL_OP_SLAB_ALLOC */
24 #ifdef PL_OP_SLAB_ALLOC
25 #define SLAB_SIZE 8192
26 static char *PL_OpPtr = NULL;
27 static int PL_OpSpace = 0;
28 #define NewOp(m,var,c,type) do { if ((PL_OpSpace -= c*sizeof(type)) >= 0) \
29 var = (type *)(PL_OpPtr -= c*sizeof(type)); \
31 var = (type *) Slab_Alloc(m,c*sizeof(type)); \
35 S_Slab_Alloc(pTHX_ int m, size_t sz)
37 Newz(m,PL_OpPtr,SLAB_SIZE,char);
38 PL_OpSpace = SLAB_SIZE - sz;
39 return PL_OpPtr += PL_OpSpace;
43 #define NewOp(m, var, c, type) Newz(m, var, c, type)
46 * In the following definition, the ", Nullop" is just to make the compiler
47 * think the expression is of the right type: croak actually does a Siglongjmp.
49 #define CHECKOP(type,o) \
50 ((PL_op_mask && PL_op_mask[type]) \
51 ? ( op_free((OP*)o), \
52 Perl_croak(aTHX_ "%s trapped by operation mask", PL_op_desc[type]), \
54 : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o))
56 #define PAD_MAX 999999999
59 S_gv_ename(pTHX_ GV *gv)
62 SV* tmpsv = sv_newmortal();
63 gv_efullname3(tmpsv, gv, Nullch);
64 return SvPV(tmpsv,n_a);
68 S_no_fh_allowed(pTHX_ OP *o)
70 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
71 PL_op_desc[o->op_type]));
76 S_too_few_arguments(pTHX_ OP *o, char *name)
78 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
83 S_too_many_arguments(pTHX_ OP *o, char *name)
85 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
90 S_bad_type(pTHX_ I32 n, char *t, char *name, OP *kid)
92 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
93 (int)n, name, t, PL_op_desc[kid->op_type]));
97 S_no_bareword_allowed(pTHX_ OP *o)
99 Perl_warn(aTHX_ "Bareword \"%s\" not allowed while \"strict subs\" in use",
100 SvPV_nolen(cSVOPo->op_sv));
104 /* "register" allocation */
107 Perl_pad_allocmy(pTHX_ char *name)
115 (PL_hints & HINT_UTF8 && (name[1] & 0xc0) == 0xc0) ||
116 name[1] == '_' && (int)strlen(name) > 2))
118 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
119 /* 1999-02-27 mjd@plover.com */
121 p = strchr(name, '\0');
122 /* The next block assumes the buffer is at least 205 chars
123 long. At present, it's always at least 256 chars. */
125 strcpy(name+200, "...");
131 /* Move everything else down one character */
132 for (; p-name > 2; p--)
134 name[2] = toCTRL(name[1]);
137 yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
139 if (ckWARN(WARN_UNSAFE) && AvFILLp(PL_comppad_name) >= 0) {
140 SV **svp = AvARRAY(PL_comppad_name);
141 for (off = AvFILLp(PL_comppad_name); off > PL_comppad_name_floor; off--) {
143 && sv != &PL_sv_undef
144 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
145 && strEQ(name, SvPVX(sv)))
147 Perl_warner(aTHX_ WARN_UNSAFE,
148 "\"my\" variable %s masks earlier declaration in same %s",
149 name, (SvIVX(sv) == PAD_MAX ? "scope" : "statement"));
154 off = pad_alloc(OP_PADSV, SVs_PADMY);
156 sv_upgrade(sv, SVt_PVNV);
158 if (PL_in_my_stash) {
160 yyerror(Perl_form(aTHX_ "Can't declare class for non-scalar %s in \"my\"",
163 (void)SvUPGRADE(sv, SVt_PVMG);
164 SvSTASH(sv) = (HV*)SvREFCNT_inc(PL_in_my_stash);
167 av_store(PL_comppad_name, off, sv);
168 SvNVX(sv) = (NV)PAD_MAX;
169 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
170 if (!PL_min_intro_pending)
171 PL_min_intro_pending = off;
172 PL_max_intro_pending = off;
174 av_store(PL_comppad, off, (SV*)newAV());
175 else if (*name == '%')
176 av_store(PL_comppad, off, (SV*)newHV());
177 SvPADMY_on(PL_curpad[off]);
181 #define FINDLEX_NOSEARCH 1 /* don't search outer contexts */
184 S_pad_findlex(pTHX_ char *name, PADOFFSET newoff, U32 seq, CV* startcv,
185 I32 cx_ix, I32 saweval, U32 flags)
192 register PERL_CONTEXT *cx;
194 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
195 AV *curlist = CvPADLIST(cv);
196 SV **svp = av_fetch(curlist, 0, FALSE);
199 if (!svp || *svp == &PL_sv_undef)
202 svp = AvARRAY(curname);
203 for (off = AvFILLp(curname); off > 0; off--) {
204 if ((sv = svp[off]) &&
205 sv != &PL_sv_undef &&
207 seq > I_32(SvNVX(sv)) &&
208 strEQ(SvPVX(sv), name))
219 return 0; /* don't clone from inactive stack frame */
223 oldpad = (AV*)*av_fetch(curlist, depth, FALSE);
224 oldsv = *av_fetch(oldpad, off, TRUE);
225 if (!newoff) { /* Not a mere clone operation. */
226 SV *namesv = NEWSV(1103,0);
227 newoff = pad_alloc(OP_PADSV, SVs_PADMY);
228 sv_upgrade(namesv, SVt_PVNV);
229 sv_setpv(namesv, name);
230 av_store(PL_comppad_name, newoff, namesv);
231 SvNVX(namesv) = (NV)PL_curcop->cop_seq;
232 SvIVX(namesv) = PAD_MAX; /* A ref, intro immediately */
233 SvFAKE_on(namesv); /* A ref, not a real var */
234 if (SvOBJECT(sv)) { /* A typed var */
236 (void)SvUPGRADE(namesv, SVt_PVMG);
237 SvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)SvSTASH(sv));
240 if (CvANON(PL_compcv) || SvTYPE(PL_compcv) == SVt_PVFM) {
241 /* "It's closures all the way down." */
242 CvCLONE_on(PL_compcv);
244 if (CvANON(PL_compcv))
245 oldsv = Nullsv; /* no need to keep ref */
250 bcv && bcv != cv && !CvCLONE(bcv);
251 bcv = CvOUTSIDE(bcv))
256 if (ckWARN(WARN_CLOSURE)
257 && !CvUNIQUE(bcv) && !CvUNIQUE(cv))
259 Perl_warner(aTHX_ WARN_CLOSURE,
260 "Variable \"%s\" may be unavailable",
268 else if (!CvUNIQUE(PL_compcv)) {
269 if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv))
270 Perl_warner(aTHX_ WARN_CLOSURE,
271 "Variable \"%s\" will not stay shared", name);
274 av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv));
280 if (flags & FINDLEX_NOSEARCH)
283 /* Nothing in current lexical context--try eval's context, if any.
284 * This is necessary to let the perldb get at lexically scoped variables.
285 * XXX This will also probably interact badly with eval tree caching.
288 for (i = cx_ix; i >= 0; i--) {
290 switch (CxTYPE(cx)) {
292 if (i == 0 && saweval) {
293 seq = cxstack[saweval].blk_oldcop->cop_seq;
294 return pad_findlex(name, newoff, seq, PL_main_cv, -1, saweval, 0);
298 switch (cx->blk_eval.old_op_type) {
304 /* require must have its own scope */
312 if (PL_debstash && CvSTASH(cv) == PL_debstash) { /* ignore DB'* scope */
313 saweval = i; /* so we know where we were called from */
316 seq = cxstack[saweval].blk_oldcop->cop_seq;
317 return pad_findlex(name, newoff, seq, cv, i-1, saweval,FINDLEX_NOSEARCH);
325 Perl_pad_findmy(pTHX_ char *name)
331 SV **svp = AvARRAY(PL_comppad_name);
332 U32 seq = PL_cop_seqmax;
338 * Special case to get lexical (and hence per-thread) @_.
339 * XXX I need to find out how to tell at parse-time whether use
340 * of @_ should refer to a lexical (from a sub) or defgv (global
341 * scope and maybe weird sub-ish things like formats). See
342 * startsub in perly.y. It's possible that @_ could be lexical
343 * (at least from subs) even in non-threaded perl.
345 if (strEQ(name, "@_"))
346 return 0; /* success. (NOT_IN_PAD indicates failure) */
347 #endif /* USE_THREADS */
349 /* The one we're looking for is probably just before comppad_name_fill. */
350 for (off = AvFILLp(PL_comppad_name); off > 0; off--) {
351 if ((sv = svp[off]) &&
352 sv != &PL_sv_undef &&
355 seq > I_32(SvNVX(sv)))) &&
356 strEQ(SvPVX(sv), name))
359 return (PADOFFSET)off;
360 pendoff = off; /* this pending def. will override import */
364 outside = CvOUTSIDE(PL_compcv);
366 /* Check if if we're compiling an eval'', and adjust seq to be the
367 * eval's seq number. This depends on eval'' having a non-null
368 * CvOUTSIDE() while it is being compiled. The eval'' itself is
369 * identified by CvEVAL being true and CvGV being null. */
370 if (outside && CvEVAL(PL_compcv) && !CvGV(PL_compcv) && cxstack_ix >= 0) {
371 cx = &cxstack[cxstack_ix];
373 seq = cx->blk_oldcop->cop_seq;
376 /* See if it's in a nested scope */
377 off = pad_findlex(name, 0, seq, outside, cxstack_ix, 0, 0);
379 /* If there is a pending local definition, this new alias must die */
381 SvIVX(AvARRAY(PL_comppad_name)[off]) = seq;
382 return off; /* pad_findlex returns 0 for failure...*/
384 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
388 Perl_pad_leavemy(pTHX_ I32 fill)
392 SV **svp = AvARRAY(PL_comppad_name);
394 if (PL_min_intro_pending && fill < PL_min_intro_pending) {
395 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
396 if ((sv = svp[off]) && sv != &PL_sv_undef && ckWARN_d(WARN_INTERNAL))
397 Perl_warner(aTHX_ WARN_INTERNAL, "%s never introduced", SvPVX(sv));
400 /* "Deintroduce" my variables that are leaving with this scope. */
401 for (off = AvFILLp(PL_comppad_name); off > fill; off--) {
402 if ((sv = svp[off]) && sv != &PL_sv_undef && SvIVX(sv) == PAD_MAX)
403 SvIVX(sv) = PL_cop_seqmax;
408 Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype)
414 if (AvARRAY(PL_comppad) != PL_curpad)
415 Perl_croak(aTHX_ "panic: pad_alloc");
416 if (PL_pad_reset_pending)
418 if (tmptype & SVs_PADMY) {
420 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
421 } while (SvPADBUSY(sv)); /* need a fresh one */
422 retval = AvFILLp(PL_comppad);
425 SV **names = AvARRAY(PL_comppad_name);
426 SSize_t names_fill = AvFILLp(PL_comppad_name);
429 * "foreach" index vars temporarily become aliases to non-"my"
430 * values. Thus we must skip, not just pad values that are
431 * marked as current pad values, but also those with names.
433 if (++PL_padix <= names_fill &&
434 (sv = names[PL_padix]) && sv != &PL_sv_undef)
436 sv = *av_fetch(PL_comppad, PL_padix, TRUE);
437 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)))
442 SvFLAGS(sv) |= tmptype;
443 PL_curpad = AvARRAY(PL_comppad);
445 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx alloc %ld for %s\n",
446 (unsigned long) thr, (unsigned long) PL_curpad,
447 (long) retval, PL_op_name[optype]));
449 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx alloc %ld for %s\n",
450 (unsigned long) PL_curpad,
451 (long) retval, PL_op_name[optype]));
452 #endif /* USE_THREADS */
453 return (PADOFFSET)retval;
457 Perl_pad_sv(pTHX_ PADOFFSET po)
461 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx sv %d\n",
462 (unsigned long) thr, (unsigned long) PL_curpad, po));
465 Perl_croak(aTHX_ "panic: pad_sv po");
466 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx sv %d\n",
467 (unsigned long) PL_curpad, po));
468 #endif /* USE_THREADS */
469 return PL_curpad[po]; /* eventually we'll turn this into a macro */
473 Perl_pad_free(pTHX_ PADOFFSET po)
478 if (AvARRAY(PL_comppad) != PL_curpad)
479 Perl_croak(aTHX_ "panic: pad_free curpad");
481 Perl_croak(aTHX_ "panic: pad_free po");
483 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx free %d\n",
484 (unsigned long) thr, (unsigned long) PL_curpad, po));
486 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx free %d\n",
487 (unsigned long) PL_curpad, po));
488 #endif /* USE_THREADS */
489 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef)
490 SvPADTMP_off(PL_curpad[po]);
491 if ((I32)po < PL_padix)
496 Perl_pad_swipe(pTHX_ PADOFFSET po)
499 if (AvARRAY(PL_comppad) != PL_curpad)
500 Perl_croak(aTHX_ "panic: pad_swipe curpad");
502 Perl_croak(aTHX_ "panic: pad_swipe po");
504 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx swipe %d\n",
505 (unsigned long) thr, (unsigned long) PL_curpad, po));
507 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx swipe %d\n",
508 (unsigned long) PL_curpad, po));
509 #endif /* USE_THREADS */
510 SvPADTMP_off(PL_curpad[po]);
511 PL_curpad[po] = NEWSV(1107,0);
512 SvPADTMP_on(PL_curpad[po]);
513 if ((I32)po < PL_padix)
517 /* XXX pad_reset() is currently disabled because it results in serious bugs.
518 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
519 * on the stack by OPs that use them, there are several ways to get an alias
520 * to a shared TARG. Such an alias will change randomly and unpredictably.
521 * We avoid doing this until we can think of a Better Way.
526 #ifdef USE_BROKEN_PAD_RESET
530 if (AvARRAY(PL_comppad) != PL_curpad)
531 Perl_croak(aTHX_ "panic: pad_reset curpad");
533 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx reset\n",
534 (unsigned long) thr, (unsigned long) PL_curpad));
536 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx reset\n",
537 (unsigned long) PL_curpad));
538 #endif /* USE_THREADS */
539 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
540 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
541 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
542 SvPADTMP_off(PL_curpad[po]);
544 PL_padix = PL_padix_floor;
547 PL_pad_reset_pending = FALSE;
551 /* find_threadsv is not reentrant */
553 Perl_find_threadsv(pTHX_ const char *name)
559 /* We currently only handle names of a single character */
560 p = strchr(PL_threadsv_names, *name);
563 key = p - PL_threadsv_names;
564 MUTEX_LOCK(&thr->mutex);
565 svp = av_fetch(thr->threadsv, key, FALSE);
567 MUTEX_UNLOCK(&thr->mutex);
569 SV *sv = NEWSV(0, 0);
570 av_store(thr->threadsv, key, sv);
571 thr->threadsvp = AvARRAY(thr->threadsv);
572 MUTEX_UNLOCK(&thr->mutex);
574 * Some magic variables used to be automagically initialised
575 * in gv_fetchpv. Those which are now per-thread magicals get
576 * initialised here instead.
582 sv_setpv(sv, "\034");
583 sv_magic(sv, 0, 0, name, 1);
588 PL_sawampersand = TRUE;
602 /* XXX %! tied to Errno.pm needs to be added here.
603 * See gv_fetchpv(). */
607 sv_magic(sv, 0, 0, name, 1);
609 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
610 "find_threadsv: new SV %p for $%s%c\n",
611 sv, (*name < 32) ? "^" : "",
612 (*name < 32) ? toCTRL(*name) : *name));
616 #endif /* USE_THREADS */
621 Perl_op_free(pTHX_ OP *o)
623 register OP *kid, *nextkid;
626 if (!o || o->op_seq == (U16)-1)
629 if (o->op_flags & OPf_KIDS) {
630 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
631 nextkid = kid->op_sibling; /* Get before next freeing kid */
639 /* COP* is not cleared by op_clear() so that we may track line
640 * numbers etc even after null() */
641 if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
646 #ifdef PL_OP_SLAB_ALLOC
647 if ((char *) o == PL_OpPtr)
656 S_op_clear(pTHX_ OP *o)
658 switch (o->op_type) {
659 case OP_NULL: /* Was holding old type, if any. */
660 case OP_ENTEREVAL: /* Was holding hints. */
662 case OP_THREADSV: /* Was holding index into thr->threadsv AV. */
668 if (!(o->op_flags & OPf_SPECIAL))
671 #endif /* USE_THREADS */
673 if (!(o->op_flags & OPf_REF)
674 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
680 SvREFCNT_dec(cGVOPo->op_gv);
681 cGVOPo->op_gv = Nullgv;
684 SvREFCNT_dec(cSVOPo->op_sv);
685 cSVOPo->op_sv = Nullsv;
691 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
695 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
696 SvREFCNT_dec(cSVOPo->op_sv);
697 cSVOPo->op_sv = Nullsv;
700 Safefree(cPVOPo->op_pv);
701 cPVOPo->op_pv = Nullch;
705 op_free(cPMOPo->op_pmreplroot);
706 cPMOPo->op_pmreplroot = Nullop;
711 ReREFCNT_dec(cPMOPo->op_pmregexp);
712 cPMOPo->op_pmregexp = (REGEXP*)NULL;
717 pad_free(o->op_targ);
721 S_cop_free(pTHX_ COP* cop)
723 Safefree(cop->cop_label);
724 SvREFCNT_dec(cop->cop_filegv);
725 if (! specialWARN(cop->cop_warnings))
726 SvREFCNT_dec(cop->cop_warnings);
732 if (o->op_type == OP_NULL)
735 o->op_targ = o->op_type;
736 o->op_type = OP_NULL;
737 o->op_ppaddr = PL_ppaddr[OP_NULL];
740 /* Contextualizers */
742 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
745 Perl_linklist(pTHX_ OP *o)
752 /* establish postfix order */
753 if (cUNOPo->op_first) {
754 o->op_next = LINKLIST(cUNOPo->op_first);
755 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
757 kid->op_next = LINKLIST(kid->op_sibling);
769 Perl_scalarkids(pTHX_ OP *o)
772 if (o && o->op_flags & OPf_KIDS) {
773 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
780 S_scalarboolean(pTHX_ OP *o)
782 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
784 if (ckWARN(WARN_SYNTAX)) {
785 line_t oldline = PL_curcop->cop_line;
787 if (PL_copline != NOLINE)
788 PL_curcop->cop_line = PL_copline;
789 Perl_warner(aTHX_ WARN_SYNTAX, "Found = in conditional, should be ==");
790 PL_curcop->cop_line = oldline;
797 Perl_scalar(pTHX_ OP *o)
801 /* assumes no premature commitment */
802 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
803 || o->op_type == OP_RETURN)
806 if ((o->op_private & OPpTARGET_MY)
807 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
808 return scalar(o); /* As if inside SASSIGN */
810 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
812 switch (o->op_type) {
814 if (o->op_private & OPpREPEAT_DOLIST)
815 null(((LISTOP*)cBINOPo->op_first)->op_first);
816 scalar(cBINOPo->op_first);
821 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
825 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
826 if (!kPMOP->op_pmreplroot)
827 deprecate("implicit split to @_");
835 if (o->op_flags & OPf_KIDS) {
836 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
842 kid = cLISTOPo->op_first;
844 while (kid = kid->op_sibling) {
850 WITH_THR(PL_curcop = &PL_compiling);
855 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
861 WITH_THR(PL_curcop = &PL_compiling);
868 Perl_scalarvoid(pTHX_ OP *o)
875 if (o->op_type == OP_NEXTSTATE
876 || o->op_type == OP_SETSTATE
877 || o->op_type == OP_DBSTATE
878 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
879 || o->op_targ == OP_SETSTATE
880 || o->op_targ == OP_DBSTATE)))
883 PL_curcop = (COP*)o; /* for warning below */
886 /* assumes no premature commitment */
887 want = o->op_flags & OPf_WANT;
888 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
889 || o->op_type == OP_RETURN)
892 if ((o->op_private & OPpTARGET_MY)
893 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
894 return scalar(o); /* As if inside SASSIGN */
896 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
898 switch (o->op_type) {
900 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
904 if (o->op_flags & OPf_STACKED)
908 if (o->op_private == 4)
979 if (!(o->op_private & OPpLVAL_INTRO))
980 useless = PL_op_desc[o->op_type];
987 if (!(o->op_private & OPpLVAL_INTRO) &&
988 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
989 useless = "a variable";
994 if (cSVOPo->op_private & OPpCONST_STRICT)
995 no_bareword_allowed(o);
998 if (ckWARN(WARN_VOID)) {
999 useless = "a constant";
1000 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1002 else if (SvPOK(sv)) {
1003 if (strnEQ(SvPVX(sv), "di", 2) ||
1004 strnEQ(SvPVX(sv), "ds", 2) ||
1005 strnEQ(SvPVX(sv), "ig", 2))
1010 null(o); /* don't execute or even remember it */
1014 o->op_type = OP_PREINC; /* pre-increment is faster */
1015 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1019 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1020 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1026 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1031 if (o->op_flags & OPf_STACKED)
1039 if (!(o->op_flags & OPf_KIDS))
1048 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1055 /* all requires must return a boolean value */
1056 o->op_flags &= ~OPf_WANT;
1059 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
1060 if (!kPMOP->op_pmreplroot)
1061 deprecate("implicit split to @_");
1067 if (ckWARN(WARN_VOID))
1068 Perl_warner(aTHX_ WARN_VOID, "Useless use of %s in void context", useless);
1074 Perl_listkids(pTHX_ OP *o)
1077 if (o && o->op_flags & OPf_KIDS) {
1078 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1085 Perl_list(pTHX_ OP *o)
1089 /* assumes no premature commitment */
1090 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
1091 || o->op_type == OP_RETURN)
1094 if ((o->op_private & OPpTARGET_MY)
1095 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
1096 return o; /* As if inside SASSIGN */
1098 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1100 switch (o->op_type) {
1103 list(cBINOPo->op_first);
1108 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1116 if (!(o->op_flags & OPf_KIDS))
1118 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1119 list(cBINOPo->op_first);
1120 return gen_constant_list(o);
1127 kid = cLISTOPo->op_first;
1129 while (kid = kid->op_sibling) {
1130 if (kid->op_sibling)
1135 WITH_THR(PL_curcop = &PL_compiling);
1139 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1140 if (kid->op_sibling)
1145 WITH_THR(PL_curcop = &PL_compiling);
1148 /* all requires must return a boolean value */
1149 o->op_flags &= ~OPf_WANT;
1156 Perl_scalarseq(pTHX_ OP *o)
1161 if (o->op_type == OP_LINESEQ ||
1162 o->op_type == OP_SCOPE ||
1163 o->op_type == OP_LEAVE ||
1164 o->op_type == OP_LEAVETRY)
1167 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1168 if (kid->op_sibling) {
1172 PL_curcop = &PL_compiling;
1174 o->op_flags &= ~OPf_PARENS;
1175 if (PL_hints & HINT_BLOCK_SCOPE)
1176 o->op_flags |= OPf_PARENS;
1179 o = newOP(OP_STUB, 0);
1184 S_modkids(pTHX_ OP *o, I32 type)
1187 if (o && o->op_flags & OPf_KIDS) {
1188 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1195 Perl_mod(pTHX_ OP *o, I32 type)
1202 if (!o || PL_error_count)
1205 if ((o->op_private & OPpTARGET_MY)
1206 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
1209 switch (o->op_type) {
1214 if (!(o->op_private & (OPpCONST_ARYBASE)))
1216 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1217 PL_compiling.cop_arybase = (I32)SvIV(((SVOP*)PL_eval_start)->op_sv);
1221 SAVEI32(PL_compiling.cop_arybase);
1222 PL_compiling.cop_arybase = 0;
1224 else if (type == OP_REFGEN)
1227 Perl_croak(aTHX_ "That use of $[ is unsupported");
1230 if (o->op_flags & OPf_PARENS)
1234 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1235 !(o->op_flags & OPf_STACKED)) {
1236 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1237 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1238 assert(cUNOPo->op_first->op_type == OP_NULL);
1239 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1242 else { /* lvalue subroutine call */
1243 o->op_private |= OPpLVAL_INTRO;
1244 if (type == OP_GREPSTART || type == OP_ENTERSUB) {
1245 /* Backward compatibility mode: */
1246 o->op_private |= OPpENTERSUB_INARGS;
1249 else { /* Compile-time error message: */
1250 OP *kid = cUNOPo->op_first;
1254 if (kid->op_type == OP_PUSHMARK)
1256 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1258 "panic: unexpected lvalue entersub "
1259 "args: type/targ %ld:%ld",
1260 (long)kid->op_type,kid->op_targ);
1261 kid = kLISTOP->op_first;
1263 while (kid->op_sibling)
1264 kid = kid->op_sibling;
1265 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1267 if (kid->op_type == OP_METHOD_NAMED
1268 || kid->op_type == OP_METHOD)
1272 if (kid->op_sibling || kid->op_next != kid) {
1273 yyerror("panic: unexpected optree near method call");
1277 NewOp(1101, newop, 1, OP);
1278 newop->op_type = OP_RV2CV;
1279 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1280 newop->op_next = newop;
1281 kid->op_sibling = newop;
1282 newop->op_private |= OPpLVAL_INTRO;
1286 if (kid->op_type != OP_RV2CV)
1288 "panic: unexpected lvalue entersub "
1289 "entry via type/targ %ld:%ld",
1290 (long)kid->op_type,kid->op_targ);
1291 kid->op_private |= OPpLVAL_INTRO;
1292 break; /* Postpone until runtime */
1296 kid = kUNOP->op_first;
1297 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1298 kid = kUNOP->op_first;
1299 if (kid->op_type == OP_NULL)
1301 "Unexpected constant lvalue entersub "
1302 "entry via type/targ %ld:%ld",
1303 (long)kid->op_type,kid->op_targ);
1304 if (kid->op_type != OP_GV) {
1305 /* Restore RV2CV to check lvalueness */
1307 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1308 okid->op_next = kid->op_next;
1309 kid->op_next = okid;
1312 okid->op_next = Nullop;
1313 okid->op_type = OP_RV2CV;
1315 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1316 okid->op_private |= OPpLVAL_INTRO;
1320 cv = GvCV(kGVOP->op_gv);
1330 /* grep, foreach, subcalls, refgen */
1331 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1333 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1334 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1336 : (o->op_type == OP_ENTERSUB
1337 ? "non-lvalue subroutine call"
1338 : PL_op_desc[o->op_type])),
1339 type ? PL_op_desc[type] : "local"));
1353 case OP_RIGHT_SHIFT:
1362 if (!(o->op_flags & OPf_STACKED))
1368 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1374 if (!type && cUNOPo->op_first->op_type != OP_GV)
1375 Perl_croak(aTHX_ "Can't localize through a reference");
1376 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1377 PL_modcount = 10000;
1378 return o; /* Treat \(@foo) like ordinary list. */
1382 if (scalar_mod_type(o, type))
1384 ref(cUNOPo->op_first, o->op_type);
1393 PL_modcount = 10000;
1396 if (!type && cUNOPo->op_first->op_type != OP_GV)
1397 Perl_croak(aTHX_ "Can't localize through a reference");
1398 ref(cUNOPo->op_first, o->op_type);
1402 PL_hints |= HINT_BLOCK_SCOPE;
1412 PL_modcount = 10000;
1413 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1414 return o; /* Treat \(@foo) like ordinary list. */
1415 if (scalar_mod_type(o, type))
1421 Perl_croak(aTHX_ "Can't localize lexical variable %s",
1422 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
1427 PL_modcount++; /* XXX ??? */
1429 #endif /* USE_THREADS */
1435 if (type != OP_SASSIGN)
1439 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1445 pad_free(o->op_targ);
1446 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1447 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1448 if (o->op_flags & OPf_KIDS)
1449 mod(cBINOPo->op_first->op_sibling, type);
1454 ref(cBINOPo->op_first, o->op_type);
1455 if (type == OP_ENTERSUB &&
1456 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1457 o->op_private |= OPpLVAL_DEFER;
1464 if (o->op_flags & OPf_KIDS)
1465 mod(cLISTOPo->op_last, type);
1469 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1471 else if (!(o->op_flags & OPf_KIDS))
1473 if (o->op_targ != OP_LIST) {
1474 mod(cBINOPo->op_first, type);
1479 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1483 o->op_flags |= OPf_MOD;
1485 if (type == OP_AASSIGN || type == OP_SASSIGN)
1486 o->op_flags |= OPf_SPECIAL|OPf_REF;
1488 o->op_private |= OPpLVAL_INTRO;
1489 o->op_flags &= ~OPf_SPECIAL;
1490 PL_hints |= HINT_BLOCK_SCOPE;
1492 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1493 o->op_flags |= OPf_REF;
1498 S_scalar_mod_type(pTHX_ OP *o, I32 type)
1502 if (o->op_type == OP_RV2GV)
1526 case OP_RIGHT_SHIFT:
1545 S_is_handle_constructor(pTHX_ OP *o, I32 argnum)
1547 switch (o->op_type) {
1555 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1568 Perl_refkids(pTHX_ OP *o, I32 type)
1571 if (o && o->op_flags & OPf_KIDS) {
1572 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1579 Perl_ref(pTHX_ OP *o, I32 type)
1583 if (!o || PL_error_count)
1586 switch (o->op_type) {
1588 if ((type == OP_DEFINED || type == OP_LOCK) &&
1589 !(o->op_flags & OPf_STACKED)) {
1590 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1591 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1592 assert(cUNOPo->op_first->op_type == OP_NULL);
1593 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1594 o->op_flags |= OPf_SPECIAL;
1599 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1603 if (type == OP_DEFINED)
1604 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1605 ref(cUNOPo->op_first, o->op_type);
1608 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1609 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1610 : type == OP_RV2HV ? OPpDEREF_HV
1612 o->op_flags |= OPf_MOD;
1617 o->op_flags |= OPf_MOD; /* XXX ??? */
1622 o->op_flags |= OPf_REF;
1625 if (type == OP_DEFINED)
1626 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1627 ref(cUNOPo->op_first, o->op_type);
1632 o->op_flags |= OPf_REF;
1637 if (!(o->op_flags & OPf_KIDS))
1639 ref(cBINOPo->op_first, type);
1643 ref(cBINOPo->op_first, o->op_type);
1644 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1645 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1646 : type == OP_RV2HV ? OPpDEREF_HV
1648 o->op_flags |= OPf_MOD;
1656 if (!(o->op_flags & OPf_KIDS))
1658 ref(cLISTOPo->op_last, type);
1668 S_dup_attrlist(pTHX_ OP *o)
1672 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1673 * where the first kid is OP_PUSHMARK and the remaining ones
1674 * are OP_CONST. We need to push the OP_CONST values.
1676 if (o->op_type == OP_CONST)
1677 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc(cSVOPo->op_sv));
1679 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1680 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1681 if (o->op_type == OP_CONST)
1682 rop = append_elem(OP_LIST, rop,
1683 newSVOP(OP_CONST, o->op_flags,
1684 SvREFCNT_inc(cSVOPo->op_sv)));
1691 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
1693 OP *modname; /* for 'use' */
1696 /* fake up C<use attributes $pkg,$rv,@attrs> */
1697 ENTER; /* need to protect against side-effects of 'use' */
1699 if (stash && HvNAME(stash))
1700 stashsv = newSVpv(HvNAME(stash), 0);
1702 stashsv = &PL_sv_no;
1703 #define ATTRSMODULE "attributes"
1704 modname = newSVOP(OP_CONST, 0,
1705 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1));
1706 modname->op_private |= OPpCONST_BARE;
1707 /* that flag is required to make 'use' work right */
1708 utilize(1, start_subparse(FALSE, 0),
1709 Nullop, /* version */
1711 prepend_elem(OP_LIST,
1712 newSVOP(OP_CONST, 0, stashsv),
1713 prepend_elem(OP_LIST,
1714 newSVOP(OP_CONST, 0, newRV(target)),
1715 dup_attrlist(attrs))));
1720 S_my_kid(pTHX_ OP *o, OP *attrs)
1725 if (!o || PL_error_count)
1729 if (type == OP_LIST) {
1730 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1732 } else if (type == OP_UNDEF) {
1734 } else if (type != OP_PADSV &&
1737 type != OP_PUSHMARK)
1739 yyerror(Perl_form(aTHX_ "Can't declare %s in my", PL_op_desc[o->op_type]));
1742 else if (attrs && type != OP_PUSHMARK) {
1747 /* check for C<my Dog $spot> when deciding package */
1748 namesvp = av_fetch(PL_comppad_name, o->op_targ, FALSE);
1749 if (namesvp && *namesvp && SvOBJECT(*namesvp) && HvNAME(SvSTASH(*namesvp)))
1750 stash = SvSTASH(*namesvp);
1752 stash = PL_curstash;
1753 padsv = PAD_SV(o->op_targ);
1754 apply_attrs(stash, padsv, attrs);
1756 o->op_flags |= OPf_MOD;
1757 o->op_private |= OPpLVAL_INTRO;
1762 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1764 if (o->op_flags & OPf_PARENS)
1767 PL_in_my_stash = Nullhv;
1770 return my_kid(o, attrs);
1774 Perl_my(pTHX_ OP *o)
1776 return my_kid(o, Nullop);
1780 Perl_sawparens(pTHX_ OP *o)
1783 o->op_flags |= OPf_PARENS;
1788 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
1793 if (ckWARN(WARN_UNSAFE) &&
1794 (left->op_type == OP_RV2AV ||
1795 left->op_type == OP_RV2HV ||
1796 left->op_type == OP_PADAV ||
1797 left->op_type == OP_PADHV)) {
1798 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
1799 right->op_type == OP_TRANS)
1800 ? right->op_type : OP_MATCH];
1801 char *sample = ((left->op_type == OP_RV2AV ||
1802 left->op_type == OP_PADAV)
1803 ? "@array" : "%hash");
1804 Perl_warner(aTHX_ WARN_UNSAFE,
1805 "Applying %s to %s will act on scalar(%s)",
1806 desc, sample, sample);
1809 if (right->op_type == OP_MATCH ||
1810 right->op_type == OP_SUBST ||
1811 right->op_type == OP_TRANS) {
1812 right->op_flags |= OPf_STACKED;
1813 if (right->op_type != OP_MATCH)
1814 left = mod(left, right->op_type);
1815 if (right->op_type == OP_TRANS)
1816 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1818 o = prepend_elem(right->op_type, scalar(left), right);
1820 return newUNOP(OP_NOT, 0, scalar(o));
1824 return bind_match(type, left,
1825 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1829 Perl_invert(pTHX_ OP *o)
1833 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1834 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1838 Perl_scope(pTHX_ OP *o)
1841 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
1842 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1843 o->op_type = OP_LEAVE;
1844 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
1847 if (o->op_type == OP_LINESEQ) {
1849 o->op_type = OP_SCOPE;
1850 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
1851 kid = ((LISTOP*)o)->op_first;
1852 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE){
1853 kid->op_type = OP_SETSTATE;
1854 kid->op_ppaddr = PL_ppaddr[OP_SETSTATE];
1858 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1865 Perl_save_hints(pTHX)
1868 SAVESPTR(GvHV(PL_hintgv));
1869 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
1870 SAVEFREESV(GvHV(PL_hintgv));
1874 Perl_block_start(pTHX_ int full)
1877 int retval = PL_savestack_ix;
1879 SAVEI32(PL_comppad_name_floor);
1881 if ((PL_comppad_name_fill = AvFILLp(PL_comppad_name)) > 0)
1882 PL_comppad_name_floor = PL_comppad_name_fill;
1884 PL_comppad_name_floor = 0;
1886 SAVEI32(PL_min_intro_pending);
1887 SAVEI32(PL_max_intro_pending);
1888 PL_min_intro_pending = 0;
1889 SAVEI32(PL_comppad_name_fill);
1890 SAVEI32(PL_padix_floor);
1891 PL_padix_floor = PL_padix;
1892 PL_pad_reset_pending = FALSE;
1894 PL_hints &= ~HINT_BLOCK_SCOPE;
1895 SAVEPPTR(PL_compiling.cop_warnings);
1896 if (! specialWARN(PL_compiling.cop_warnings)) {
1897 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1898 SAVEFREESV(PL_compiling.cop_warnings) ;
1906 Perl_block_end(pTHX_ I32 floor, OP *seq)
1909 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
1910 OP* retval = scalarseq(seq);
1912 PL_pad_reset_pending = FALSE;
1913 PL_compiling.op_private = PL_hints;
1915 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1916 pad_leavemy(PL_comppad_name_fill);
1925 OP *o = newOP(OP_THREADSV, 0);
1926 o->op_targ = find_threadsv("_");
1929 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
1930 #endif /* USE_THREADS */
1934 Perl_newPROG(pTHX_ OP *o)
1940 PL_eval_root = newUNOP(OP_LEAVEEVAL,
1941 ((PL_in_eval & EVAL_KEEPERR)
1942 ? OPf_SPECIAL : 0), o);
1943 PL_eval_start = linklist(PL_eval_root);
1944 PL_eval_root->op_next = 0;
1945 peep(PL_eval_start);
1950 PL_main_root = scope(sawparens(scalarvoid(o)));
1951 PL_curcop = &PL_compiling;
1952 PL_main_start = LINKLIST(PL_main_root);
1953 PL_main_root->op_next = 0;
1954 peep(PL_main_start);
1957 /* Register with debugger */
1959 CV *cv = get_cv("DB::postponed", FALSE);
1963 XPUSHs((SV*)PL_compiling.cop_filegv);
1965 call_sv((SV*)cv, G_DISCARD);
1972 Perl_localize(pTHX_ OP *o, I32 lex)
1974 if (o->op_flags & OPf_PARENS)
1978 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
1980 for (s = PL_bufptr; *s && (isALNUM(*s) || (*s & 0x80) || strchr("@$%, ",*s)); s++) ;
1981 if (*s == ';' || *s == '=')
1982 Perl_warner(aTHX_ WARN_PARENTHESIS, "Parentheses missing around \"%s\" list",
1983 lex ? "my" : "local");
1987 PL_in_my_stash = Nullhv;
1991 return mod(o, OP_NULL); /* a bit kludgey */
1995 Perl_jmaybe(pTHX_ OP *o)
1997 if (o->op_type == OP_LIST) {
2000 o2 = newOP(OP_THREADSV, 0);
2001 o2->op_targ = find_threadsv(";");
2003 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
2004 #endif /* USE_THREADS */
2005 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2011 Perl_fold_constants(pTHX_ register OP *o)
2015 I32 type = o->op_type;
2018 if (PL_opargs[type] & OA_RETSCALAR)
2020 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2021 o->op_targ = pad_alloc(type, SVs_PADTMP);
2023 /* integerize op, unless it happens to be C<-foo>.
2024 * XXX should pp_i_negate() do magic string negation instead? */
2025 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2026 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2027 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2029 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2032 if (!(PL_opargs[type] & OA_FOLDCONST))
2037 /* XXX might want a ck_negate() for this */
2038 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2051 if (o->op_private & OPpLOCALE)
2056 goto nope; /* Don't try to run w/ errors */
2058 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2059 if ((curop->op_type != OP_CONST ||
2060 (curop->op_private & OPpCONST_BARE)) &&
2061 curop->op_type != OP_LIST &&
2062 curop->op_type != OP_SCALAR &&
2063 curop->op_type != OP_NULL &&
2064 curop->op_type != OP_PUSHMARK)
2070 curop = LINKLIST(o);
2074 sv = *(PL_stack_sp--);
2075 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
2076 pad_swipe(o->op_targ);
2077 else if (SvTEMP(sv)) { /* grab mortal temp? */
2078 (void)SvREFCNT_inc(sv);
2082 if (type == OP_RV2GV)
2083 return newGVOP(OP_GV, 0, (GV*)sv);
2085 /* try to smush double to int, but don't smush -2.0 to -2 */
2086 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
2090 if ((NV)iv == SvNV(sv)) {
2095 SvIOK_off(sv); /* undo SvIV() damage */
2097 return newSVOP(OP_CONST, 0, sv);
2101 if (!(PL_opargs[type] & OA_OTHERINT))
2104 if (!(PL_hints & HINT_INTEGER)) {
2105 if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS))
2108 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
2109 if (curop->op_type == OP_CONST) {
2110 if (SvIOK(((SVOP*)curop)->op_sv))
2114 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
2118 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
2125 Perl_gen_constant_list(pTHX_ register OP *o)
2129 I32 oldtmps_floor = PL_tmps_floor;
2133 return o; /* Don't attempt to run with errors */
2135 PL_op = curop = LINKLIST(o);
2141 PL_tmps_floor = oldtmps_floor;
2143 o->op_type = OP_RV2AV;
2144 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2145 curop = ((UNOP*)o)->op_first;
2146 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
2153 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2158 if (!o || o->op_type != OP_LIST)
2159 o = newLISTOP(OP_LIST, 0, o, Nullop);
2161 o->op_flags &= ~OPf_WANT;
2163 if (!(PL_opargs[type] & OA_MARK))
2164 null(cLISTOPo->op_first);
2167 o->op_ppaddr = PL_ppaddr[type];
2168 o->op_flags |= flags;
2170 o = CHECKOP(type, o);
2171 if (o->op_type != type)
2174 if (cLISTOPo->op_children < 7) {
2175 /* XXX do we really need to do this if we're done appending?? */
2176 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2178 cLISTOPo->op_last = last; /* in case check substituted last arg */
2181 return fold_constants(o);
2184 /* List constructors */
2187 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2195 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
2196 return newLISTOP(type, 0, first, last);
2198 if (first->op_flags & OPf_KIDS)
2199 ((LISTOP*)first)->op_last->op_sibling = last;
2201 first->op_flags |= OPf_KIDS;
2202 ((LISTOP*)first)->op_first = last;
2204 ((LISTOP*)first)->op_last = last;
2205 ((LISTOP*)first)->op_children++;
2210 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2218 if (first->op_type != type)
2219 return prepend_elem(type, (OP*)first, (OP*)last);
2221 if (last->op_type != type)
2222 return append_elem(type, (OP*)first, (OP*)last);
2224 first->op_last->op_sibling = last->op_first;
2225 first->op_last = last->op_last;
2226 first->op_children += last->op_children;
2227 if (first->op_children)
2228 first->op_flags |= OPf_KIDS;
2230 #ifdef PL_OP_SLAB_ALLOC
2238 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2246 if (last->op_type == type) {
2247 if (type == OP_LIST) { /* already a PUSHMARK there */
2248 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2249 ((LISTOP*)last)->op_first->op_sibling = first;
2252 if (!(last->op_flags & OPf_KIDS)) {
2253 ((LISTOP*)last)->op_last = first;
2254 last->op_flags |= OPf_KIDS;
2256 first->op_sibling = ((LISTOP*)last)->op_first;
2257 ((LISTOP*)last)->op_first = first;
2259 ((LISTOP*)last)->op_children++;
2263 return newLISTOP(type, 0, first, last);
2269 Perl_newNULLLIST(pTHX)
2271 return newOP(OP_STUB, 0);
2275 Perl_force_list(pTHX_ OP *o)
2277 if (!o || o->op_type != OP_LIST)
2278 o = newLISTOP(OP_LIST, 0, o, Nullop);
2284 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2288 NewOp(1101, listop, 1, LISTOP);
2290 listop->op_type = type;
2291 listop->op_ppaddr = PL_ppaddr[type];
2292 listop->op_children = (first != 0) + (last != 0);
2293 listop->op_flags = flags;
2297 else if (!first && last)
2300 first->op_sibling = last;
2301 listop->op_first = first;
2302 listop->op_last = last;
2303 if (type == OP_LIST) {
2305 pushop = newOP(OP_PUSHMARK, 0);
2306 pushop->op_sibling = first;
2307 listop->op_first = pushop;
2308 listop->op_flags |= OPf_KIDS;
2310 listop->op_last = pushop;
2312 else if (listop->op_children)
2313 listop->op_flags |= OPf_KIDS;
2319 Perl_newOP(pTHX_ I32 type, I32 flags)
2322 NewOp(1101, o, 1, OP);
2324 o->op_ppaddr = PL_ppaddr[type];
2325 o->op_flags = flags;
2328 o->op_private = 0 + (flags >> 8);
2329 if (PL_opargs[type] & OA_RETSCALAR)
2331 if (PL_opargs[type] & OA_TARGET)
2332 o->op_targ = pad_alloc(type, SVs_PADTMP);
2333 return CHECKOP(type, o);
2337 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
2342 first = newOP(OP_STUB, 0);
2343 if (PL_opargs[type] & OA_MARK)
2344 first = force_list(first);
2346 NewOp(1101, unop, 1, UNOP);
2347 unop->op_type = type;
2348 unop->op_ppaddr = PL_ppaddr[type];
2349 unop->op_first = first;
2350 unop->op_flags = flags | OPf_KIDS;
2351 unop->op_private = 1 | (flags >> 8);
2352 unop = (UNOP*) CHECKOP(type, unop);
2356 return fold_constants((OP *) unop);
2360 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2363 NewOp(1101, binop, 1, BINOP);
2366 first = newOP(OP_NULL, 0);
2368 binop->op_type = type;
2369 binop->op_ppaddr = PL_ppaddr[type];
2370 binop->op_first = first;
2371 binop->op_flags = flags | OPf_KIDS;
2374 binop->op_private = 1 | (flags >> 8);
2377 binop->op_private = 2 | (flags >> 8);
2378 first->op_sibling = last;
2381 binop = (BINOP*)CHECKOP(type, binop);
2382 if (binop->op_next || binop->op_type != type)
2385 binop->op_last = binop->op_first->op_sibling;
2387 return fold_constants((OP *)binop);
2391 utf8compare(const void *a, const void *b)
2394 for (i = 0; i < 10; i++) {
2395 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2397 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2404 Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
2406 SV *tstr = ((SVOP*)expr)->op_sv;
2407 SV *rstr = ((SVOP*)repl)->op_sv;
2410 register U8 *t = (U8*)SvPV(tstr, tlen);
2411 register U8 *r = (U8*)SvPV(rstr, rlen);
2417 register short *tbl;
2419 complement = o->op_private & OPpTRANS_COMPLEMENT;
2420 del = o->op_private & OPpTRANS_DELETE;
2421 squash = o->op_private & OPpTRANS_SQUASH;
2423 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2424 SV* listsv = newSVpvn("# comment\n",10);
2426 U8* tend = t + tlen;
2427 U8* rend = r + rlen;
2443 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2444 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2450 New(1109, cp, tlen, U8*);
2452 transv = newSVpvn("",0);
2461 qsort(cp, i, sizeof(U8*), utf8compare);
2462 for (j = 0; j < i; j++) {
2464 UV val = utf8_to_uv(s, &ulen);
2466 diff = val - nextmin;
2468 t = uv_to_utf8(tmpbuf,nextmin);
2469 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2471 t = uv_to_utf8(tmpbuf, val - 1);
2472 sv_catpvn(transv, "\377", 1);
2473 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2477 val = utf8_to_uv(s+1, &ulen);
2481 t = uv_to_utf8(tmpbuf,nextmin);
2482 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2483 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2484 sv_catpvn(transv, "\377", 1);
2485 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2486 t = (U8*)SvPVX(transv);
2487 tlen = SvCUR(transv);
2490 else if (!rlen && !del) {
2491 r = t; rlen = tlen; rend = tend;
2494 if (to_utf && from_utf) { /* only counting characters */
2495 if (t == r || (tlen == rlen && memEQ(t, r, tlen)))
2496 o->op_private |= OPpTRANS_IDENTICAL;
2498 else { /* straight latin-1 translation */
2499 if (tlen == 4 && memEQ(t, "\0\377\303\277", 4) &&
2500 rlen == 4 && memEQ(r, "\0\377\303\277", 4))
2501 o->op_private |= OPpTRANS_IDENTICAL;
2505 while (t < tend || tfirst <= tlast) {
2506 /* see if we need more "t" chars */
2507 if (tfirst > tlast) {
2508 tfirst = (I32)utf8_to_uv(t, &ulen);
2510 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
2511 tlast = (I32)utf8_to_uv(++t, &ulen);
2518 /* now see if we need more "r" chars */
2519 if (rfirst > rlast) {
2521 rfirst = (I32)utf8_to_uv(r, &ulen);
2523 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
2524 rlast = (I32)utf8_to_uv(++r, &ulen);
2533 rfirst = rlast = 0xffffffff;
2537 /* now see which range will peter our first, if either. */
2538 tdiff = tlast - tfirst;
2539 rdiff = rlast - rfirst;
2546 if (rfirst == 0xffffffff) {
2547 diff = tdiff; /* oops, pretend rdiff is infinite */
2549 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\tXXXX\n", tfirst, tlast);
2551 Perl_sv_catpvf(aTHX_ listsv, "%04x\t\tXXXX\n", tfirst);
2555 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\t%04x\n", tfirst, tfirst + diff, rfirst);
2557 Perl_sv_catpvf(aTHX_ listsv, "%04x\t\t%04x\n", tfirst, rfirst);
2559 if (rfirst + diff > max)
2560 max = rfirst + diff;
2565 else if (rfirst <= 0x800)
2566 grows |= (tfirst < 0x80);
2567 else if (rfirst <= 0x10000)
2568 grows |= (tfirst < 0x800);
2569 else if (rfirst <= 0x200000)
2570 grows |= (tfirst < 0x10000);
2571 else if (rfirst <= 0x4000000)
2572 grows |= (tfirst < 0x200000);
2573 else if (rfirst <= 0x80000000)
2574 grows |= (tfirst < 0x4000000);
2586 else if (max > 0xff)
2591 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2592 SvREFCNT_dec(listsv);
2594 SvREFCNT_dec(transv);
2596 if (!del && havefinal)
2597 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5, newSViv((IV)final), 0);
2599 if (grows && to_utf)
2600 o->op_private |= OPpTRANS_GROWS;
2607 tbl = (short*)cPVOPo->op_pv;
2609 Zero(tbl, 256, short);
2610 for (i = 0; i < tlen; i++)
2612 for (i = 0, j = 0; i < 256; i++) {
2628 if (!rlen && !del) {
2631 o->op_private |= OPpTRANS_IDENTICAL;
2633 for (i = 0; i < 256; i++)
2635 for (i = 0, j = 0; i < tlen; i++,j++) {
2638 if (tbl[t[i]] == -1)
2644 if (tbl[t[i]] == -1)
2655 Perl_newPMOP(pTHX_ I32 type, I32 flags)
2660 NewOp(1101, pmop, 1, PMOP);
2661 pmop->op_type = type;
2662 pmop->op_ppaddr = PL_ppaddr[type];
2663 pmop->op_flags = flags;
2664 pmop->op_private = 0 | (flags >> 8);
2666 if (PL_hints & HINT_RE_TAINT)
2667 pmop->op_pmpermflags |= PMf_RETAINT;
2668 if (PL_hints & HINT_LOCALE)
2669 pmop->op_pmpermflags |= PMf_LOCALE;
2670 pmop->op_pmflags = pmop->op_pmpermflags;
2672 /* link into pm list */
2673 if (type != OP_TRANS && PL_curstash) {
2674 pmop->op_pmnext = HvPMROOT(PL_curstash);
2675 HvPMROOT(PL_curstash) = pmop;
2682 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
2687 I32 repl_has_vars = 0;
2689 if (o->op_type == OP_TRANS)
2690 return pmtrans(o, expr, repl);
2692 PL_hints |= HINT_BLOCK_SCOPE;
2695 if (expr->op_type == OP_CONST) {
2697 SV *pat = ((SVOP*)expr)->op_sv;
2698 char *p = SvPV(pat, plen);
2699 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2700 sv_setpvn(pat, "\\s+", 3);
2701 p = SvPV(pat, plen);
2702 pm->op_pmflags |= PMf_SKIPWHITE;
2704 pm->op_pmregexp = CALLREGCOMP(aTHX_ p, p + plen, pm);
2705 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2706 pm->op_pmflags |= PMf_WHITE;
2710 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2711 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2713 : OP_REGCMAYBE),0,expr);
2715 NewOp(1101, rcop, 1, LOGOP);
2716 rcop->op_type = OP_REGCOMP;
2717 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2718 rcop->op_first = scalar(expr);
2719 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2720 ? (OPf_SPECIAL | OPf_KIDS)
2722 rcop->op_private = 1;
2725 /* establish postfix order */
2726 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2728 rcop->op_next = expr;
2729 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2732 rcop->op_next = LINKLIST(expr);
2733 expr->op_next = (OP*)rcop;
2736 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2741 if (pm->op_pmflags & PMf_EVAL) {
2743 if (PL_curcop->cop_line < PL_multi_end)
2744 PL_curcop->cop_line = PL_multi_end;
2747 else if (repl->op_type == OP_THREADSV
2748 && strchr("&`'123456789+",
2749 PL_threadsv_names[repl->op_targ]))
2753 #endif /* USE_THREADS */
2754 else if (repl->op_type == OP_CONST)
2758 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2759 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2761 if (curop->op_type == OP_THREADSV) {
2763 if (strchr("&`'123456789+", curop->op_private))
2767 if (curop->op_type == OP_GV) {
2768 GV *gv = ((GVOP*)curop)->op_gv;
2770 if (strchr("&`'123456789+", *GvENAME(gv)))
2773 #endif /* USE_THREADS */
2774 else if (curop->op_type == OP_RV2CV)
2776 else if (curop->op_type == OP_RV2SV ||
2777 curop->op_type == OP_RV2AV ||
2778 curop->op_type == OP_RV2HV ||
2779 curop->op_type == OP_RV2GV) {
2780 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2783 else if (curop->op_type == OP_PADSV ||
2784 curop->op_type == OP_PADAV ||
2785 curop->op_type == OP_PADHV ||
2786 curop->op_type == OP_PADANY) {
2789 else if (curop->op_type == OP_PUSHRE)
2790 ; /* Okay here, dangerous in newASSIGNOP */
2799 && (!pm->op_pmregexp
2800 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
2801 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2802 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2803 prepend_elem(o->op_type, scalar(repl), o);
2806 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
2807 pm->op_pmflags |= PMf_MAYBE_CONST;
2808 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2810 NewOp(1101, rcop, 1, LOGOP);
2811 rcop->op_type = OP_SUBSTCONT;
2812 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
2813 rcop->op_first = scalar(repl);
2814 rcop->op_flags |= OPf_KIDS;
2815 rcop->op_private = 1;
2818 /* establish postfix order */
2819 rcop->op_next = LINKLIST(repl);
2820 repl->op_next = (OP*)rcop;
2822 pm->op_pmreplroot = scalar((OP*)rcop);
2823 pm->op_pmreplstart = LINKLIST(rcop);
2832 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
2835 NewOp(1101, svop, 1, SVOP);
2836 svop->op_type = type;
2837 svop->op_ppaddr = PL_ppaddr[type];
2839 svop->op_next = (OP*)svop;
2840 svop->op_flags = flags;
2841 if (PL_opargs[type] & OA_RETSCALAR)
2843 if (PL_opargs[type] & OA_TARGET)
2844 svop->op_targ = pad_alloc(type, SVs_PADTMP);
2845 return CHECKOP(type, svop);
2849 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
2853 NewOp(1101, gvop, 1, GVOP);
2854 gvop->op_type = type;
2855 gvop->op_ppaddr = PL_ppaddr[type];
2856 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
2857 gvop->op_next = (OP*)gvop;
2858 gvop->op_flags = flags;
2859 if (PL_opargs[type] & OA_RETSCALAR)
2861 if (PL_opargs[type] & OA_TARGET)
2862 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
2863 return CHECKOP(type, gvop);
2867 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
2870 NewOp(1101, pvop, 1, PVOP);
2871 pvop->op_type = type;
2872 pvop->op_ppaddr = PL_ppaddr[type];
2874 pvop->op_next = (OP*)pvop;
2875 pvop->op_flags = flags;
2876 if (PL_opargs[type] & OA_RETSCALAR)
2878 if (PL_opargs[type] & OA_TARGET)
2879 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2880 return CHECKOP(type, pvop);
2884 Perl_package(pTHX_ OP *o)
2889 save_hptr(&PL_curstash);
2890 save_item(PL_curstname);
2895 name = SvPV(sv, len);
2896 PL_curstash = gv_stashpvn(name,len,TRUE);
2897 sv_setpvn(PL_curstname, name, len);
2901 sv_setpv(PL_curstname,"<none>");
2902 PL_curstash = Nullhv;
2904 PL_hints |= HINT_BLOCK_SCOPE;
2905 PL_copline = NOLINE;
2910 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg)
2919 if (id->op_type != OP_CONST)
2920 Perl_croak(aTHX_ "Module name must be constant");
2924 if(version != Nullop) {
2925 SV *vesv = ((SVOP*)version)->op_sv;
2927 if (arg == Nullop && !SvNIOK(vesv)) {
2933 if (version->op_type != OP_CONST || !SvNIOK(vesv))
2934 Perl_croak(aTHX_ "Version number must be constant number");
2936 /* Make copy of id so we don't free it twice */
2937 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2939 /* Fake up a method call to VERSION */
2940 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2941 append_elem(OP_LIST,
2942 prepend_elem(OP_LIST, pack, list(version)),
2943 newSVOP(OP_METHOD_NAMED, 0,
2944 newSVpvn("VERSION", 7))));
2948 /* Fake up an import/unimport */
2949 if (arg && arg->op_type == OP_STUB)
2950 imop = arg; /* no import on explicit () */
2951 else if(SvNIOK(((SVOP*)id)->op_sv)) {
2952 imop = Nullop; /* use 5.0; */
2955 /* Make copy of id so we don't free it twice */
2956 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2957 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2958 append_elem(OP_LIST,
2959 prepend_elem(OP_LIST, pack, list(arg)),
2960 newSVOP(OP_METHOD_NAMED, 0,
2961 aver ? newSVpvn("import", 6)
2962 : newSVpvn("unimport", 8))));
2965 /* Fake up a require, handle override, if any */
2966 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
2967 if (!(gv && GvIMPORTED_CV(gv)))
2968 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
2970 if (gv && GvIMPORTED_CV(gv)) {
2971 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
2972 append_elem(OP_LIST, id,
2973 scalar(newUNOP(OP_RV2CV, 0,
2978 rqop = newUNOP(OP_REQUIRE, 0, id);
2981 /* Fake up the BEGIN {}, which does its thing immediately. */
2983 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
2986 append_elem(OP_LINESEQ,
2987 append_elem(OP_LINESEQ,
2988 newSTATEOP(0, Nullch, rqop),
2989 newSTATEOP(0, Nullch, veop)),
2990 newSTATEOP(0, Nullch, imop) ));
2992 PL_copline = NOLINE;
2997 Perl_dofile(pTHX_ OP *term)
3002 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
3003 if (!(gv && GvIMPORTED_CV(gv)))
3004 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
3006 if (gv && GvIMPORTED_CV(gv)) {
3007 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3008 append_elem(OP_LIST, term,
3009 scalar(newUNOP(OP_RV2CV, 0,
3014 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3020 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
3022 return newBINOP(OP_LSLICE, flags,
3023 list(force_list(subscript)),
3024 list(force_list(listval)) );
3028 S_list_assignment(pTHX_ register OP *o)
3033 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3034 o = cUNOPo->op_first;
3036 if (o->op_type == OP_COND_EXPR) {
3037 I32 t = list_assignment(cLOGOPo->op_first->op_sibling);
3038 I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
3043 yyerror("Assignment to both a list and a scalar");
3047 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3048 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3049 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
3052 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
3055 if (o->op_type == OP_RV2SV)
3062 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
3067 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
3068 return newLOGOP(optype, 0,
3069 mod(scalar(left), optype),
3070 newUNOP(OP_SASSIGN, 0, scalar(right)));
3073 return newBINOP(optype, OPf_STACKED,
3074 mod(scalar(left), optype), scalar(right));
3078 if (list_assignment(left)) {
3081 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3082 left = mod(left, OP_AASSIGN);
3090 o = newBINOP(OP_AASSIGN, flags,
3091 list(force_list(right)),
3092 list(force_list(left)) );
3093 o->op_private = 0 | (flags >> 8);
3094 if (!(left->op_private & OPpLVAL_INTRO)) {
3098 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3099 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3100 if (curop->op_type == OP_GV) {
3101 GV *gv = ((GVOP*)curop)->op_gv;
3102 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3104 SvCUR(gv) = PL_generation;
3106 else if (curop->op_type == OP_PADSV ||
3107 curop->op_type == OP_PADAV ||
3108 curop->op_type == OP_PADHV ||
3109 curop->op_type == OP_PADANY) {
3110 SV **svp = AvARRAY(PL_comppad_name);
3111 SV *sv = svp[curop->op_targ];
3112 if (SvCUR(sv) == PL_generation)
3114 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
3116 else if (curop->op_type == OP_RV2CV)
3118 else if (curop->op_type == OP_RV2SV ||
3119 curop->op_type == OP_RV2AV ||
3120 curop->op_type == OP_RV2HV ||
3121 curop->op_type == OP_RV2GV) {
3122 if (lastop->op_type != OP_GV) /* funny deref? */
3125 else if (curop->op_type == OP_PUSHRE) {
3126 if (((PMOP*)curop)->op_pmreplroot) {
3127 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3128 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3130 SvCUR(gv) = PL_generation;
3139 o->op_private = OPpASSIGN_COMMON;
3141 if (right && right->op_type == OP_SPLIT) {
3143 if ((tmpop = ((LISTOP*)right)->op_first) &&
3144 tmpop->op_type == OP_PUSHRE)
3146 PMOP *pm = (PMOP*)tmpop;
3147 if (left->op_type == OP_RV2AV &&
3148 !(left->op_private & OPpLVAL_INTRO) &&
3149 !(o->op_private & OPpASSIGN_COMMON) )
3151 tmpop = ((UNOP*)left)->op_first;
3152 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
3153 pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
3154 pm->op_pmflags |= PMf_ONCE;
3155 tmpop = cUNOPo->op_first; /* to list (nulled) */
3156 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3157 tmpop->op_sibling = Nullop; /* don't free split */
3158 right->op_next = tmpop->op_next; /* fix starting loc */
3159 op_free(o); /* blow off assign */
3160 right->op_flags &= ~OPf_WANT;
3161 /* "I don't know and I don't care." */
3166 if (PL_modcount < 10000 &&
3167 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3169 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3171 sv_setiv(sv, PL_modcount+1);
3179 right = newOP(OP_UNDEF, 0);
3180 if (right->op_type == OP_READLINE) {
3181 right->op_flags |= OPf_STACKED;
3182 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3185 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3186 o = newBINOP(OP_SASSIGN, flags,
3187 scalar(right), mod(scalar(left), OP_SASSIGN) );
3199 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3202 U32 seq = intro_my();
3205 NewOp(1101, cop, 1, COP);
3206 if (PERLDB_LINE && PL_curcop->cop_line && PL_curstash != PL_debstash) {
3207 cop->op_type = OP_DBSTATE;
3208 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3211 cop->op_type = OP_NEXTSTATE;
3212 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3214 cop->op_flags = flags;
3215 cop->op_private = (PL_hints & HINT_UTF8);
3217 cop->op_private |= NATIVE_HINTS;
3219 PL_compiling.op_private = cop->op_private;
3220 cop->op_next = (OP*)cop;
3223 cop->cop_label = label;
3224 PL_hints |= HINT_BLOCK_SCOPE;
3227 cop->cop_arybase = PL_curcop->cop_arybase;
3228 if (specialWARN(PL_curcop->cop_warnings))
3229 cop->cop_warnings = PL_curcop->cop_warnings ;
3231 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3234 if (PL_copline == NOLINE)
3235 cop->cop_line = PL_curcop->cop_line;
3237 cop->cop_line = PL_copline;
3238 PL_copline = NOLINE;
3240 cop->cop_filegv = (GV*)SvREFCNT_inc(PL_curcop->cop_filegv);
3241 cop->cop_stash = PL_curstash;
3243 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3244 SV **svp = av_fetch(GvAV(PL_curcop->cop_filegv),(I32)cop->cop_line, FALSE);
3245 if (svp && *svp != &PL_sv_undef && !SvIOK(*svp)) {
3246 (void)SvIOK_on(*svp);
3248 SvSTASH(*svp) = (HV*)cop;
3252 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3255 /* "Introduce" my variables to visible status. */
3263 if (! PL_min_intro_pending)
3264 return PL_cop_seqmax;
3266 svp = AvARRAY(PL_comppad_name);
3267 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
3268 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
3269 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
3270 SvNVX(sv) = (NV)PL_cop_seqmax;
3273 PL_min_intro_pending = 0;
3274 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
3275 return PL_cop_seqmax++;
3279 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
3281 return new_logop(type, flags, &first, &other);
3285 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
3290 OP *first = *firstp;
3291 OP *other = *otherp;
3293 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3294 return newBINOP(type, flags, scalar(first), scalar(other));
3296 scalarboolean(first);
3297 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3298 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3299 if (type == OP_AND || type == OP_OR) {
3305 first = *firstp = cUNOPo->op_first;
3307 first->op_next = o->op_next;
3308 cUNOPo->op_first = Nullop;
3312 if (first->op_type == OP_CONST) {
3313 if (ckWARN(WARN_PRECEDENCE) && (first->op_private & OPpCONST_BARE))
3314 Perl_warner(aTHX_ WARN_PRECEDENCE, "Probable precedence problem on %s",
3316 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3327 else if (first->op_type == OP_WANTARRAY) {
3333 else if (ckWARN(WARN_UNSAFE) && (first->op_flags & OPf_KIDS)) {
3334 OP *k1 = ((UNOP*)first)->op_first;
3335 OP *k2 = k1->op_sibling;
3337 switch (first->op_type)
3340 if (k2 && k2->op_type == OP_READLINE
3341 && (k2->op_flags & OPf_STACKED)
3342 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3343 warnop = k2->op_type;
3347 if (k1->op_type == OP_READDIR
3348 || k1->op_type == OP_GLOB
3349 || k1->op_type == OP_EACH)
3350 warnop = k1->op_type;
3354 line_t oldline = PL_curcop->cop_line;
3355 PL_curcop->cop_line = PL_copline;
3356 Perl_warner(aTHX_ WARN_UNSAFE,
3357 "Value of %s%s can be \"0\"; test with defined()",
3359 ((warnop == OP_READLINE || warnop == OP_GLOB)
3360 ? " construct" : "() operator"));
3361 PL_curcop->cop_line = oldline;
3368 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3369 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3371 NewOp(1101, logop, 1, LOGOP);
3373 logop->op_type = type;
3374 logop->op_ppaddr = PL_ppaddr[type];
3375 logop->op_first = first;
3376 logop->op_flags = flags | OPf_KIDS;
3377 logop->op_other = LINKLIST(other);
3378 logop->op_private = 1 | (flags >> 8);
3380 /* establish postfix order */
3381 logop->op_next = LINKLIST(first);
3382 first->op_next = (OP*)logop;
3383 first->op_sibling = other;
3385 o = newUNOP(OP_NULL, 0, (OP*)logop);
3392 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
3400 return newLOGOP(OP_AND, 0, first, trueop);
3402 return newLOGOP(OP_OR, 0, first, falseop);
3404 scalarboolean(first);
3405 if (first->op_type == OP_CONST) {
3406 if (SvTRUE(((SVOP*)first)->op_sv)) {
3417 else if (first->op_type == OP_WANTARRAY) {
3421 NewOp(1101, logop, 1, LOGOP);
3422 logop->op_type = OP_COND_EXPR;
3423 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3424 logop->op_first = first;
3425 logop->op_flags = flags | OPf_KIDS;
3426 logop->op_private = 1 | (flags >> 8);
3427 logop->op_other = LINKLIST(trueop);
3428 logop->op_next = LINKLIST(falseop);
3431 /* establish postfix order */
3432 start = LINKLIST(first);
3433 first->op_next = (OP*)logop;
3435 first->op_sibling = trueop;
3436 trueop->op_sibling = falseop;
3437 o = newUNOP(OP_NULL, 0, (OP*)logop);
3439 trueop->op_next = falseop->op_next = o;
3446 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
3455 NewOp(1101, range, 1, LOGOP);
3457 range->op_type = OP_RANGE;
3458 range->op_ppaddr = PL_ppaddr[OP_RANGE];
3459 range->op_first = left;
3460 range->op_flags = OPf_KIDS;
3461 leftstart = LINKLIST(left);
3462 range->op_other = LINKLIST(right);
3463 range->op_private = 1 | (flags >> 8);
3465 left->op_sibling = right;
3467 range->op_next = (OP*)range;
3468 flip = newUNOP(OP_FLIP, flags, (OP*)range);
3469 flop = newUNOP(OP_FLOP, 0, flip);
3470 o = newUNOP(OP_NULL, 0, flop);
3472 range->op_next = leftstart;
3474 left->op_next = flip;
3475 right->op_next = flop;
3477 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3478 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
3479 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3480 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3482 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3483 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3486 if (!flip->op_private || !flop->op_private)
3487 linklist(o); /* blow off optimizer unless constant */
3493 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
3498 int once = block && block->op_flags & OPf_SPECIAL &&
3499 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3502 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3503 return block; /* do {} while 0 does once */
3504 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3505 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3506 expr = newUNOP(OP_DEFINED, 0,
3507 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3508 } else if (expr->op_flags & OPf_KIDS) {
3509 OP *k1 = ((UNOP*)expr)->op_first;
3510 OP *k2 = (k1) ? k1->op_sibling : NULL;
3511 switch (expr->op_type) {
3513 if (k2 && k2->op_type == OP_READLINE
3514 && (k2->op_flags & OPf_STACKED)
3515 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3516 expr = newUNOP(OP_DEFINED, 0, expr);
3520 if (k1->op_type == OP_READDIR
3521 || k1->op_type == OP_GLOB
3522 || k1->op_type == OP_EACH)
3523 expr = newUNOP(OP_DEFINED, 0, expr);
3529 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3530 o = new_logop(OP_AND, 0, &expr, &listop);
3533 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3535 if (once && o != listop)
3536 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3539 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3541 o->op_flags |= flags;
3543 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3548 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
3557 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3558 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
3559 expr = newUNOP(OP_DEFINED, 0,
3560 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3561 } else if (expr && (expr->op_flags & OPf_KIDS)) {
3562 OP *k1 = ((UNOP*)expr)->op_first;
3563 OP *k2 = (k1) ? k1->op_sibling : NULL;
3564 switch (expr->op_type) {
3566 if (k2 && k2->op_type == OP_READLINE
3567 && (k2->op_flags & OPf_STACKED)
3568 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3569 expr = newUNOP(OP_DEFINED, 0, expr);
3573 if (k1->op_type == OP_READDIR
3574 || k1->op_type == OP_GLOB
3575 || k1->op_type == OP_EACH)
3576 expr = newUNOP(OP_DEFINED, 0, expr);
3582 block = newOP(OP_NULL, 0);
3585 next = LINKLIST(cont);
3587 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
3588 if ((line_t)whileline != NOLINE) {
3589 PL_copline = whileline;
3590 cont = append_elem(OP_LINESEQ, cont,
3591 newSTATEOP(0, Nullch, Nullop));
3595 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3596 redo = LINKLIST(listop);
3599 PL_copline = whileline;
3601 o = new_logop(OP_AND, 0, &expr, &listop);
3602 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3603 op_free(expr); /* oops, it's a while (0) */
3605 return Nullop; /* listop already freed by new_logop */
3608 ((LISTOP*)listop)->op_last->op_next = condop =
3609 (o == listop ? redo : LINKLIST(o));
3617 NewOp(1101,loop,1,LOOP);
3618 loop->op_type = OP_ENTERLOOP;
3619 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3620 loop->op_private = 0;
3621 loop->op_next = (OP*)loop;
3624 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3626 loop->op_redoop = redo;
3627 loop->op_lastop = o;
3630 loop->op_nextop = next;
3632 loop->op_nextop = o;
3634 o->op_flags |= flags;
3635 o->op_private |= (flags >> 8);
3640 Perl_newFOROP(pTHX_ I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
3649 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3650 sv->op_type = OP_RV2GV;
3651 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
3653 else if (sv->op_type == OP_PADSV) { /* private variable */
3654 padoff = sv->op_targ;
3658 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3659 padoff = sv->op_targ;
3660 iterflags |= OPf_SPECIAL;
3665 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
3669 padoff = find_threadsv("_");
3670 iterflags |= OPf_SPECIAL;
3672 sv = newGVOP(OP_GV, 0, PL_defgv);
3675 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3676 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3677 iterflags |= OPf_STACKED;
3679 else if (expr->op_type == OP_NULL &&
3680 (expr->op_flags & OPf_KIDS) &&
3681 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3683 /* Basically turn for($x..$y) into the same as for($x,$y), but we
3684 * set the STACKED flag to indicate that these values are to be
3685 * treated as min/max values by 'pp_iterinit'.
3687 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3688 LOGOP* range = (LOGOP*) flip->op_first;
3689 OP* left = range->op_first;
3690 OP* right = left->op_sibling;
3693 range->op_flags &= ~OPf_KIDS;
3694 range->op_first = Nullop;
3696 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3697 listop->op_first->op_next = range->op_next;
3698 left->op_next = range->op_other;
3699 right->op_next = (OP*)listop;
3700 listop->op_next = listop->op_first;
3703 expr = (OP*)(listop);
3705 iterflags |= OPf_STACKED;
3708 expr = mod(force_list(expr), OP_GREPSTART);
3712 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
3713 append_elem(OP_LIST, expr, scalar(sv))));
3714 assert(!loop->op_next);
3715 #ifdef PL_OP_SLAB_ALLOC
3716 NewOp(1234,tmp,1,LOOP);
3717 Copy(loop,tmp,1,LOOP);
3720 Renew(loop, 1, LOOP);
3722 loop->op_targ = padoff;
3723 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
3724 PL_copline = forline;
3725 return newSTATEOP(0, label, wop);
3729 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
3735 if (type != OP_GOTO || label->op_type == OP_CONST) {
3736 /* "last()" means "last" */
3737 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
3738 o = newOP(type, OPf_SPECIAL);
3740 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
3741 ? SvPVx(((SVOP*)label)->op_sv, n_a)
3747 if (label->op_type == OP_ENTERSUB)
3748 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
3749 o = newUNOP(type, OPf_STACKED, label);
3751 PL_hints |= HINT_BLOCK_SCOPE;
3756 Perl_cv_undef(pTHX_ CV *cv)
3761 MUTEX_DESTROY(CvMUTEXP(cv));
3762 Safefree(CvMUTEXP(cv));
3765 #endif /* USE_THREADS */
3767 if (!CvXSUB(cv) && CvROOT(cv)) {
3769 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
3770 Perl_croak(aTHX_ "Can't undef active subroutine");
3773 Perl_croak(aTHX_ "Can't undef active subroutine");
3774 #endif /* USE_THREADS */
3777 SAVESPTR(PL_curpad);
3781 op_free(CvROOT(cv));
3782 CvROOT(cv) = Nullop;
3785 SvPOK_off((SV*)cv); /* forget prototype */
3787 SvREFCNT_dec(CvGV(cv));
3789 SvREFCNT_dec(CvOUTSIDE(cv));
3790 CvOUTSIDE(cv) = Nullcv;
3791 if (CvPADLIST(cv)) {
3792 /* may be during global destruction */
3793 if (SvREFCNT(CvPADLIST(cv))) {
3794 I32 i = AvFILLp(CvPADLIST(cv));
3796 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
3797 SV* sv = svp ? *svp : Nullsv;
3800 if (sv == (SV*)PL_comppad_name)
3801 PL_comppad_name = Nullav;
3802 else if (sv == (SV*)PL_comppad) {
3803 PL_comppad = Nullav;
3804 PL_curpad = Null(SV**);
3808 SvREFCNT_dec((SV*)CvPADLIST(cv));
3810 CvPADLIST(cv) = Nullav;
3814 #ifdef DEBUG_CLOSURES
3818 CV *outside = CvOUTSIDE(cv);
3819 AV* padlist = CvPADLIST(cv);
3826 PerlIO_printf(Perl_debug_log, "\tCV=0x%lx (%s), OUTSIDE=0x%lx (%s)\n",
3828 (CvANON(cv) ? "ANON"
3829 : (cv == PL_main_cv) ? "MAIN"
3830 : CvUNIQUE(cv) ? "UNIQUE"
3831 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
3834 : CvANON(outside) ? "ANON"
3835 : (outside == PL_main_cv) ? "MAIN"
3836 : CvUNIQUE(outside) ? "UNIQUE"
3837 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
3842 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
3843 pad = (AV*)*av_fetch(padlist, 1, FALSE);
3844 pname = AvARRAY(pad_name);
3845 ppad = AvARRAY(pad);
3847 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
3848 if (SvPOK(pname[ix]))
3849 PerlIO_printf(Perl_debug_log, "\t%4d. 0x%lx (%s\"%s\" %ld-%ld)\n",
3851 SvFAKE(pname[ix]) ? "FAKE " : "",
3853 (long)I_32(SvNVX(pname[ix])),
3854 (long)SvIVX(pname[ix]));
3857 #endif /* DEBUG_CLOSURES */
3860 S_cv_clone2(pTHX_ CV *proto, CV *outside)
3865 AV* protopadlist = CvPADLIST(proto);
3866 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
3867 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
3868 SV** pname = AvARRAY(protopad_name);
3869 SV** ppad = AvARRAY(protopad);
3870 I32 fname = AvFILLp(protopad_name);
3871 I32 fpad = AvFILLp(protopad);
3875 assert(!CvUNIQUE(proto));
3878 SAVESPTR(PL_curpad);
3879 SAVESPTR(PL_comppad);
3880 SAVESPTR(PL_comppad_name);
3881 SAVESPTR(PL_compcv);
3883 cv = PL_compcv = (CV*)NEWSV(1104,0);
3884 sv_upgrade((SV *)cv, SvTYPE(proto));
3890 New(666, CvMUTEXP(cv), 1, perl_mutex);
3891 MUTEX_INIT(CvMUTEXP(cv));
3893 #endif /* USE_THREADS */
3894 CvFILEGV(cv) = CvFILEGV(proto);
3895 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
3896 CvSTASH(cv) = CvSTASH(proto);
3897 CvROOT(cv) = CvROOT(proto);
3898 CvSTART(cv) = CvSTART(proto);
3900 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
3903 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
3905 PL_comppad_name = newAV();
3906 for (ix = fname; ix >= 0; ix--)
3907 av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix]));
3909 PL_comppad = newAV();
3911 comppadlist = newAV();
3912 AvREAL_off(comppadlist);
3913 av_store(comppadlist, 0, (SV*)PL_comppad_name);
3914 av_store(comppadlist, 1, (SV*)PL_comppad);
3915 CvPADLIST(cv) = comppadlist;
3916 av_fill(PL_comppad, AvFILLp(protopad));
3917 PL_curpad = AvARRAY(PL_comppad);
3919 av = newAV(); /* will be @_ */
3921 av_store(PL_comppad, 0, (SV*)av);
3922 AvFLAGS(av) = AVf_REIFY;
3924 for (ix = fpad; ix > 0; ix--) {
3925 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3926 if (namesv && namesv != &PL_sv_undef) {
3927 char *name = SvPVX(namesv); /* XXX */
3928 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
3929 I32 off = pad_findlex(name, ix, SvIVX(namesv),
3930 CvOUTSIDE(cv), cxstack_ix, 0, 0);
3932 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
3934 Perl_croak(aTHX_ "panic: cv_clone: %s", name);
3936 else { /* our own lexical */
3939 /* anon code -- we'll come back for it */
3940 sv = SvREFCNT_inc(ppad[ix]);
3942 else if (*name == '@')
3944 else if (*name == '%')
3954 SV* sv = NEWSV(0,0);
3960 /* Now that vars are all in place, clone nested closures. */
3962 for (ix = fpad; ix > 0; ix--) {
3963 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3965 && namesv != &PL_sv_undef
3966 && !(SvFLAGS(namesv) & SVf_FAKE)
3967 && *SvPVX(namesv) == '&'
3968 && CvCLONE(ppad[ix]))
3970 CV *kid = cv_clone2((CV*)ppad[ix], cv);
3971 SvREFCNT_dec(ppad[ix]);
3974 PL_curpad[ix] = (SV*)kid;
3978 #ifdef DEBUG_CLOSURES
3979 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
3981 PerlIO_printf(Perl_debug_log, " from:\n");
3983 PerlIO_printf(Perl_debug_log, " to:\n");
3992 Perl_cv_clone(pTHX_ CV *proto)
3995 MUTEX_LOCK(&PL_cred_mutex); /* XXX create separate mutex */
3996 cv = cv_clone2(proto, CvOUTSIDE(proto));
3997 MUTEX_UNLOCK(&PL_cred_mutex); /* XXX create separate mutex */
4002 Perl_cv_ckproto(pTHX_ CV *cv, GV *gv, char *p)
4006 if (((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) && ckWARN_d(WARN_UNSAFE)) {
4007 SV* msg = sv_newmortal();
4011 gv_efullname3(name = sv_newmortal(), gv, Nullch);
4012 sv_setpv(msg, "Prototype mismatch:");
4014 Perl_sv_catpvf(aTHX_ msg, " sub %_", name);
4016 Perl_sv_catpvf(aTHX_ msg, " (%s)", SvPVX(cv));
4017 sv_catpv(msg, " vs ");
4019 Perl_sv_catpvf(aTHX_ msg, "(%s)", p);
4021 sv_catpv(msg, "none");
4022 Perl_warner(aTHX_ WARN_UNSAFE, "%_", msg);
4027 Perl_cv_const_sv(pTHX_ CV *cv)
4029 if (!cv || !SvPOK(cv) || SvCUR(cv))
4031 return op_const_sv(CvSTART(cv), cv);
4035 Perl_op_const_sv(pTHX_ OP *o, CV *cv)
4042 if(o->op_type == OP_LINESEQ && cLISTOPo->op_first)
4043 o = cLISTOPo->op_first->op_sibling;
4045 for (; o; o = o->op_next) {
4046 OPCODE type = o->op_type;
4048 if (sv && o->op_next == o)
4050 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
4052 if (type == OP_LEAVESUB || type == OP_RETURN)
4056 if (type == OP_CONST)
4058 else if (type == OP_PADSV && cv) {
4059 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
4060 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
4061 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
4073 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4083 Perl_croak(aTHX_ "\"my sub\" not yet implemented");
4087 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
4089 return Perl_newATTRSUB(aTHX_ floor, o, proto, Nullop, block);
4093 Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4097 char *name = o ? SvPVx(cSVOPo->op_sv, n_a) : Nullch;
4098 GV *gv = gv_fetchpv(name ? name : "__ANON__",
4099 GV_ADDMULTI | ((block || attrs) ? 0 : GV_NOINIT),
4101 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, n_a) : Nullch;
4112 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
4113 maximum a prototype before. */
4114 if (SvTYPE(gv) > SVt_NULL) {
4115 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1)
4116 && ckWARN_d(WARN_UNSAFE))
4118 Perl_warner(aTHX_ WARN_UNSAFE, "Runaway prototype");
4120 cv_ckproto((CV*)gv, NULL, ps);
4123 sv_setpv((SV*)gv, ps);
4125 sv_setiv((SV*)gv, -1);
4126 SvREFCNT_dec(PL_compcv);
4127 cv = PL_compcv = NULL;
4128 PL_sub_generation++;
4132 if (!name || GvCVGEN(gv))
4134 else if (cv = GvCV(gv)) {
4135 cv_ckproto(cv, gv, ps);
4136 /* already defined (or promised)? */
4137 if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4139 bool const_changed = TRUE;
4140 if (!block && !attrs) {
4141 /* just a "sub foo;" when &foo is already defined */
4142 SAVEFREESV(PL_compcv);
4145 /* ahem, death to those who redefine active sort subs */
4146 if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv))
4147 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", name);
4150 if(const_sv = cv_const_sv(cv))
4151 const_changed = sv_cmp(const_sv, op_const_sv(block, Nullcv));
4152 if ((const_sv && const_changed) || ckWARN(WARN_REDEFINE)
4153 && !(CvGV(cv) && GvSTASH(CvGV(cv))
4154 && HvNAME(GvSTASH(CvGV(cv)))
4155 && strEQ(HvNAME(GvSTASH(CvGV(cv))),
4157 line_t oldline = PL_curcop->cop_line;
4158 PL_curcop->cop_line = PL_copline;
4159 Perl_warner(aTHX_ WARN_REDEFINE,
4160 const_sv ? "Constant subroutine %s redefined"
4161 : "Subroutine %s redefined", name);
4162 PL_curcop->cop_line = oldline;
4173 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>
4174 * before we clobber PL_compcv.
4178 if (CvGV(cv) && GvSTASH(CvGV(cv)) && HvNAME(GvSTASH(CvGV(cv))))
4179 stash = GvSTASH(CvGV(cv));
4180 else if (CvSTASH(cv) && HvNAME(CvSTASH(cv)))
4181 stash = CvSTASH(cv);
4183 stash = PL_curstash;
4186 /* possibly about to re-define existing subr -- ignore old cv */
4187 rcv = (SV*)PL_compcv;
4188 if (name && GvSTASH(gv) && HvNAME(GvSTASH(gv)))
4189 stash = GvSTASH(gv);
4191 stash = PL_curstash;
4193 apply_attrs(stash, rcv, attrs);
4195 if (cv) { /* must reuse cv if autoloaded */
4197 /* got here with just attrs -- work done, so bug out */
4198 SAVEFREESV(PL_compcv);
4202 CvFLAGS(cv) = CvFLAGS(PL_compcv);
4203 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
4204 CvOUTSIDE(PL_compcv) = 0;
4205 CvPADLIST(cv) = CvPADLIST(PL_compcv);
4206 CvPADLIST(PL_compcv) = 0;
4207 if (SvREFCNT(PL_compcv) > 1) /* XXX Make closures transit through stub. */
4208 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc((SV*)cv);
4209 SvREFCNT_dec(PL_compcv);
4216 PL_sub_generation++;
4219 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4220 CvFILEGV(cv) = PL_curcop->cop_filegv;
4221 CvSTASH(cv) = PL_curstash;
4224 if (!CvMUTEXP(cv)) {
4225 New(666, CvMUTEXP(cv), 1, perl_mutex);
4226 MUTEX_INIT(CvMUTEXP(cv));
4228 #endif /* USE_THREADS */
4231 sv_setpv((SV*)cv, ps);
4233 if (PL_error_count) {
4237 char *s = strrchr(name, ':');
4239 if (strEQ(s, "BEGIN")) {
4241 "BEGIN not safe after errors--compilation aborted";
4242 if (PL_in_eval & EVAL_KEEPERR)
4243 Perl_croak(aTHX_ not_safe);
4245 /* force display of errors found but not reported */
4246 sv_catpv(ERRSV, not_safe);
4247 Perl_croak(aTHX_ "%s", SvPVx(ERRSV, n_a));
4254 PL_copline = NOLINE;
4259 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad))
4260 av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv);
4263 SV **namep = AvARRAY(PL_comppad_name);
4264 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4267 if (SvIMMORTAL(PL_curpad[ix]))
4270 * The only things that a clonable function needs in its
4271 * pad are references to outer lexicals and anonymous subs.
4272 * The rest are created anew during cloning.
4274 if (!((namesv = namep[ix]) != Nullsv &&
4275 namesv != &PL_sv_undef &&
4277 *SvPVX(namesv) == '&')))
4279 SvREFCNT_dec(PL_curpad[ix]);
4280 PL_curpad[ix] = Nullsv;
4285 AV *av = newAV(); /* Will be @_ */
4287 av_store(PL_comppad, 0, (SV*)av);
4288 AvFLAGS(av) = AVf_REIFY;
4290 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4291 if (SvIMMORTAL(PL_curpad[ix]))
4293 if (!SvPADMY(PL_curpad[ix]))
4294 SvPADTMP_on(PL_curpad[ix]);
4299 CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0, scalarseq(block));
4302 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4304 CvSTART(cv) = LINKLIST(CvROOT(cv));
4305 CvROOT(cv)->op_next = 0;
4311 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4312 SV *sv = NEWSV(0,0);
4313 SV *tmpstr = sv_newmortal();
4314 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
4318 Perl_sv_setpvf(aTHX_ sv, "%_:%ld-%ld",
4319 GvSV(PL_curcop->cop_filegv),
4320 (long)PL_subline, (long)PL_curcop->cop_line);
4321 gv_efullname3(tmpstr, gv, Nullch);
4322 hv_store(GvHV(PL_DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
4323 hv = GvHVn(db_postponed);
4324 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
4325 && (cv = GvCV(db_postponed))) {
4330 call_sv((SV*)cv, G_DISCARD);
4334 if ((s = strrchr(name,':')))
4338 if (strEQ(s, "BEGIN")) {
4339 I32 oldscope = PL_scopestack_ix;
4341 SAVESPTR(PL_compiling.cop_filegv);
4342 SAVEI16(PL_compiling.cop_line);
4344 sv_setsv(PL_rs, PL_nrs);
4347 PL_beginav = newAV();
4348 DEBUG_x( dump_sub(gv) );
4349 av_push(PL_beginav, (SV *)cv);
4351 call_list(oldscope, PL_beginav);
4353 PL_curcop = &PL_compiling;
4354 PL_compiling.op_private = PL_hints;
4357 else if (strEQ(s, "END") && !PL_error_count) {
4360 av_unshift(PL_endav, 1);
4361 av_store(PL_endav, 0, (SV *)cv);
4364 else if (strEQ(s, "INIT") && !PL_error_count) {
4366 PL_initav = newAV();
4367 av_push(PL_initav, SvREFCNT_inc(cv));
4373 PL_copline = NOLINE;
4378 /* XXX unsafe for threads if eval_owner isn't held */
4380 Perl_newCONSTSUB(pTHX_ HV *stash, char *name, SV *sv)
4383 U32 oldhints = PL_hints;
4384 HV *old_cop_stash = PL_curcop->cop_stash;
4385 HV *old_curstash = PL_curstash;
4386 line_t oldline = PL_curcop->cop_line;
4387 PL_curcop->cop_line = PL_copline;
4389 PL_hints &= ~HINT_BLOCK_SCOPE;
4391 PL_curstash = PL_curcop->cop_stash = stash;
4394 start_subparse(FALSE, 0),
4395 newSVOP(OP_CONST, 0, newSVpv(name,0)),
4396 newSVOP(OP_CONST, 0, &PL_sv_no), /* SvPV(&PL_sv_no) == "" -- GMB */
4398 newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv))
4401 PL_hints = oldhints;
4402 PL_curcop->cop_stash = old_cop_stash;
4403 PL_curstash = old_curstash;
4404 PL_curcop->cop_line = oldline;
4408 Perl_newXS(pTHX_ char *name, XSUBADDR_t subaddr, char *filename)
4411 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
4414 if (cv = (name ? GvCV(gv) : Nullcv)) {
4416 /* just a cached method */
4420 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4421 /* already defined (or promised) */
4422 if (ckWARN(WARN_REDEFINE) && !(CvGV(cv) && GvSTASH(CvGV(cv))
4423 && HvNAME(GvSTASH(CvGV(cv)))
4424 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
4425 line_t oldline = PL_curcop->cop_line;
4426 if (PL_copline != NOLINE)
4427 PL_curcop->cop_line = PL_copline;
4428 Perl_warner(aTHX_ WARN_REDEFINE, "Subroutine %s redefined",name);
4429 PL_curcop->cop_line = oldline;
4436 if (cv) /* must reuse cv if autoloaded */
4439 cv = (CV*)NEWSV(1105,0);
4440 sv_upgrade((SV *)cv, SVt_PVCV);
4444 PL_sub_generation++;
4447 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4449 New(666, CvMUTEXP(cv), 1, perl_mutex);
4450 MUTEX_INIT(CvMUTEXP(cv));
4452 #endif /* USE_THREADS */
4453 CvFILEGV(cv) = gv_fetchfile(filename);
4454 CvXSUB(cv) = subaddr;
4457 char *s = strrchr(name,':');
4462 if (strEQ(s, "BEGIN")) {
4464 PL_beginav = newAV();
4465 av_push(PL_beginav, (SV *)cv);
4468 else if (strEQ(s, "END")) {
4471 av_unshift(PL_endav, 1);
4472 av_store(PL_endav, 0, (SV *)cv);
4475 else if (strEQ(s, "INIT")) {
4477 PL_initav = newAV();
4478 av_push(PL_initav, (SV *)cv);
4489 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
4499 name = SvPVx(cSVOPo->op_sv, n_a);
4502 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
4504 if (cv = GvFORM(gv)) {
4505 if (ckWARN(WARN_REDEFINE)) {
4506 line_t oldline = PL_curcop->cop_line;
4508 PL_curcop->cop_line = PL_copline;
4509 Perl_warner(aTHX_ WARN_REDEFINE, "Format %s redefined",name);
4510 PL_curcop->cop_line = oldline;
4516 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4517 CvFILEGV(cv) = PL_curcop->cop_filegv;
4519 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4520 if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix]))
4521 SvPADTMP_on(PL_curpad[ix]);
4524 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4525 CvSTART(cv) = LINKLIST(CvROOT(cv));
4526 CvROOT(cv)->op_next = 0;
4529 PL_copline = NOLINE;
4534 Perl_newANONLIST(pTHX_ OP *o)
4536 return newUNOP(OP_REFGEN, 0,
4537 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
4541 Perl_newANONHASH(pTHX_ OP *o)
4543 return newUNOP(OP_REFGEN, 0,
4544 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
4548 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
4550 return newANONATTRSUB(floor, proto, Nullop, block);
4554 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
4556 return newUNOP(OP_REFGEN, 0,
4557 newSVOP(OP_ANONCODE, 0,
4558 (SV*)newATTRSUB(floor, 0, proto, attrs, block)));
4562 Perl_oopsAV(pTHX_ OP *o)
4564 switch (o->op_type) {
4566 o->op_type = OP_PADAV;
4567 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4568 return ref(o, OP_RV2AV);
4571 o->op_type = OP_RV2AV;
4572 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4577 if (ckWARN_d(WARN_INTERNAL))
4578 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsAV");
4585 Perl_oopsHV(pTHX_ OP *o)
4589 switch (o->op_type) {
4592 o->op_type = OP_PADHV;
4593 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4594 return ref(o, OP_RV2HV);
4598 o->op_type = OP_RV2HV;
4599 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
4604 if (ckWARN_d(WARN_INTERNAL))
4605 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsHV");
4612 Perl_newAVREF(pTHX_ OP *o)
4614 if (o->op_type == OP_PADANY) {
4615 o->op_type = OP_PADAV;
4616 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4619 return newUNOP(OP_RV2AV, 0, scalar(o));
4623 Perl_newGVREF(pTHX_ I32 type, OP *o)
4625 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
4626 return newUNOP(OP_NULL, 0, o);
4627 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
4631 Perl_newHVREF(pTHX_ OP *o)
4633 if (o->op_type == OP_PADANY) {
4634 o->op_type = OP_PADHV;
4635 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4638 return newUNOP(OP_RV2HV, 0, scalar(o));
4642 Perl_oopsCV(pTHX_ OP *o)
4644 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
4650 Perl_newCVREF(pTHX_ I32 flags, OP *o)
4652 return newUNOP(OP_RV2CV, flags, scalar(o));
4656 Perl_newSVREF(pTHX_ OP *o)
4658 if (o->op_type == OP_PADANY) {
4659 o->op_type = OP_PADSV;
4660 o->op_ppaddr = PL_ppaddr[OP_PADSV];
4663 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
4664 o->op_flags |= OPpDONE_SVREF;
4667 return newUNOP(OP_RV2SV, 0, scalar(o));
4670 /* Check routines. */
4673 Perl_ck_anoncode(pTHX_ OP *o)
4678 name = NEWSV(1106,0);
4679 sv_upgrade(name, SVt_PVNV);
4680 sv_setpvn(name, "&", 1);
4683 ix = pad_alloc(o->op_type, SVs_PADMY);
4684 av_store(PL_comppad_name, ix, name);
4685 av_store(PL_comppad, ix, cSVOPo->op_sv);
4686 SvPADMY_on(cSVOPo->op_sv);
4687 cSVOPo->op_sv = Nullsv;
4688 cSVOPo->op_targ = ix;
4693 Perl_ck_bitop(pTHX_ OP *o)
4695 o->op_private = PL_hints;
4700 Perl_ck_concat(pTHX_ OP *o)
4702 if (cUNOPo->op_first->op_type == OP_CONCAT)
4703 o->op_flags |= OPf_STACKED;
4708 Perl_ck_spair(pTHX_ OP *o)
4710 if (o->op_flags & OPf_KIDS) {
4713 OPCODE type = o->op_type;
4714 o = modkids(ck_fun(o), type);
4715 kid = cUNOPo->op_first;
4716 newop = kUNOP->op_first->op_sibling;
4718 (newop->op_sibling ||
4719 !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
4720 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
4721 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
4725 op_free(kUNOP->op_first);
4726 kUNOP->op_first = newop;
4728 o->op_ppaddr = PL_ppaddr[++o->op_type];
4733 Perl_ck_delete(pTHX_ OP *o)
4737 if (o->op_flags & OPf_KIDS) {
4738 OP *kid = cUNOPo->op_first;
4739 if (kid->op_type == OP_HSLICE)
4740 o->op_private |= OPpSLICE;
4741 else if (kid->op_type != OP_HELEM)
4742 Perl_croak(aTHX_ "%s argument is not a HASH element or slice",
4743 PL_op_desc[o->op_type]);
4750 Perl_ck_eof(pTHX_ OP *o)
4752 I32 type = o->op_type;
4754 if (o->op_flags & OPf_KIDS) {
4755 if (cLISTOPo->op_first->op_type == OP_STUB) {
4757 o = newUNOP(type, OPf_SPECIAL,
4758 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
4766 Perl_ck_eval(pTHX_ OP *o)
4768 PL_hints |= HINT_BLOCK_SCOPE;
4769 if (o->op_flags & OPf_KIDS) {
4770 SVOP *kid = (SVOP*)cUNOPo->op_first;
4773 o->op_flags &= ~OPf_KIDS;
4776 else if (kid->op_type == OP_LINESEQ) {
4779 kid->op_next = o->op_next;
4780 cUNOPo->op_first = 0;
4783 NewOp(1101, enter, 1, LOGOP);
4784 enter->op_type = OP_ENTERTRY;
4785 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
4786 enter->op_private = 0;
4788 /* establish postfix order */
4789 enter->op_next = (OP*)enter;
4791 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
4792 o->op_type = OP_LEAVETRY;
4793 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
4794 enter->op_other = o;
4802 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
4804 o->op_targ = (PADOFFSET)PL_hints;
4809 Perl_ck_exec(pTHX_ OP *o)
4812 if (o->op_flags & OPf_STACKED) {
4814 kid = cUNOPo->op_first->op_sibling;
4815 if (kid->op_type == OP_RV2GV)
4824 Perl_ck_exists(pTHX_ OP *o)
4827 if (o->op_flags & OPf_KIDS) {
4828 OP *kid = cUNOPo->op_first;
4829 if (kid->op_type != OP_HELEM)
4830 Perl_croak(aTHX_ "%s argument is not a HASH element", PL_op_desc[o->op_type]);
4838 Perl_ck_gvconst(pTHX_ register OP *o)
4840 o = fold_constants(o);
4841 if (o->op_type == OP_CONST)
4848 Perl_ck_rvconst(pTHX_ register OP *o)
4851 SVOP *kid = (SVOP*)cUNOPo->op_first;
4853 o->op_private |= (PL_hints & HINT_STRICT_REFS);
4854 if (kid->op_type == OP_CONST) {
4858 SV *kidsv = kid->op_sv;
4861 /* Is it a constant from cv_const_sv()? */
4862 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
4863 SV *rsv = SvRV(kidsv);
4864 int svtype = SvTYPE(rsv);
4865 char *badtype = Nullch;
4867 switch (o->op_type) {
4869 if (svtype > SVt_PVMG)
4870 badtype = "a SCALAR";
4873 if (svtype != SVt_PVAV)
4874 badtype = "an ARRAY";
4877 if (svtype != SVt_PVHV) {
4878 if (svtype == SVt_PVAV) { /* pseudohash? */
4879 SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
4880 if (ksv && SvROK(*ksv)
4881 && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
4890 if (svtype != SVt_PVCV)
4895 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
4898 name = SvPV(kidsv, n_a);
4899 if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
4900 char *badthing = Nullch;
4901 switch (o->op_type) {
4903 badthing = "a SCALAR";
4906 badthing = "an ARRAY";
4909 badthing = "a HASH";
4914 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
4918 * This is a little tricky. We only want to add the symbol if we
4919 * didn't add it in the lexer. Otherwise we get duplicate strict
4920 * warnings. But if we didn't add it in the lexer, we must at
4921 * least pretend like we wanted to add it even if it existed before,
4922 * or we get possible typo warnings. OPpCONST_ENTERED says
4923 * whether the lexer already added THIS instance of this symbol.
4925 iscv = (o->op_type == OP_RV2CV) * 2;
4927 gv = gv_fetchpv(name,
4928 iscv | !(kid->op_private & OPpCONST_ENTERED),
4931 : o->op_type == OP_RV2SV
4933 : o->op_type == OP_RV2AV
4935 : o->op_type == OP_RV2HV
4938 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
4940 kid->op_type = OP_GV;
4941 SvREFCNT_dec(kid->op_sv);
4942 kid->op_sv = SvREFCNT_inc(gv);
4943 kid->op_ppaddr = PL_ppaddr[OP_GV];
4950 Perl_ck_ftst(pTHX_ OP *o)
4953 I32 type = o->op_type;
4955 if (o->op_flags & OPf_REF)
4958 if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
4959 SVOP *kid = (SVOP*)cUNOPo->op_first;
4961 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4963 OP *newop = newGVOP(type, OPf_REF,
4964 gv_fetchpv(SvPVx(kid->op_sv, n_a), TRUE, SVt_PVIO));
4971 if (type == OP_FTTTY)
4972 return newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
4975 return newUNOP(type, 0, newDEFSVOP());
4981 Perl_ck_fun(pTHX_ OP *o)
4988 int type = o->op_type;
4989 register I32 oa = PL_opargs[type] >> OASHIFT;
4991 if (o->op_flags & OPf_STACKED) {
4992 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
4995 return no_fh_allowed(o);
4998 if (o->op_flags & OPf_KIDS) {
5000 tokid = &cLISTOPo->op_first;
5001 kid = cLISTOPo->op_first;
5002 if (kid->op_type == OP_PUSHMARK ||
5003 kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
5005 tokid = &kid->op_sibling;
5006 kid = kid->op_sibling;
5008 if (!kid && PL_opargs[type] & OA_DEFGV)
5009 *tokid = kid = newDEFSVOP();
5013 sibl = kid->op_sibling;
5016 /* list seen where single (scalar) arg expected? */
5017 if (numargs == 1 && !(oa >> 4)
5018 && kid->op_type == OP_LIST && type != OP_SCALAR)
5020 return too_many_arguments(o,PL_op_desc[type]);
5033 if (kid->op_type == OP_CONST &&
5034 (kid->op_private & OPpCONST_BARE))
5036 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5037 OP *newop = newAVREF(newGVOP(OP_GV, 0,
5038 gv_fetchpv(name, TRUE, SVt_PVAV) ));
5040 if (ckWARN(WARN_SYNTAX))
5041 Perl_warner(aTHX_ WARN_SYNTAX,
5042 "Array @%s missing the @ in argument %" PERL_PRId64 " of %s()",
5043 name, (IV)numargs, PL_op_desc[type]);
5045 if (ckWARN(WARN_SYNTAX))
5046 Perl_warner(aTHX_ WARN_SYNTAX,
5047 "Array @%s missing the @ in argument %ld of %s()",
5048 name, (long)numargs, PL_op_desc[type]);
5052 kid->op_sibling = sibl;
5055 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
5056 bad_type(numargs, "array", PL_op_desc[type], kid);
5060 if (kid->op_type == OP_CONST &&
5061 (kid->op_private & OPpCONST_BARE))
5063 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5064 OP *newop = newHVREF(newGVOP(OP_GV, 0,
5065 gv_fetchpv(name, TRUE, SVt_PVHV) ));
5067 if (ckWARN(WARN_SYNTAX))
5068 Perl_warner(aTHX_ WARN_SYNTAX,
5069 "Hash %%%s missing the %% in argument %" PERL_PRId64 " of %s()",
5070 name, (IV)numargs, PL_op_desc[type]);
5072 if (ckWARN(WARN_SYNTAX))
5073 Perl_warner(aTHX_ WARN_SYNTAX,
5074 "Hash %%%s missing the %% in argument %ld of %s()",
5075 name, (long)numargs, PL_op_desc[type]);
5079 kid->op_sibling = sibl;
5082 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
5083 bad_type(numargs, "hash", PL_op_desc[type], kid);
5088 OP *newop = newUNOP(OP_NULL, 0, kid);
5089 kid->op_sibling = 0;
5091 newop->op_next = newop;
5093 kid->op_sibling = sibl;
5098 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
5099 if (kid->op_type == OP_CONST &&
5100 (kid->op_private & OPpCONST_BARE))
5102 OP *newop = newGVOP(OP_GV, 0,
5103 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, n_a), TRUE,
5108 else if (kid->op_type == OP_READLINE) {
5109 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
5110 bad_type(numargs, "HANDLE", PL_op_desc[o->op_type], kid);
5113 I32 flags = OPf_SPECIAL;
5115 /* is this op a FH constructor? */
5116 if (is_handle_constructor(o,numargs)) {
5118 /* Set a flag to tell rv2gv to vivify
5119 * need to "prove" flag does not mean something
5120 * else already - NI-S 1999/05/07
5124 /* Helps with open($array[$n],...)
5125 but is too simplistic - need to do selectively
5130 kid->op_sibling = 0;
5131 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
5133 kid->op_private |= priv;
5136 kid->op_sibling = sibl;
5142 mod(scalar(kid), type);
5146 tokid = &kid->op_sibling;
5147 kid = kid->op_sibling;
5149 o->op_private |= numargs;
5151 return too_many_arguments(o,PL_op_desc[o->op_type]);
5154 else if (PL_opargs[type] & OA_DEFGV) {
5156 return newUNOP(type, 0, newDEFSVOP());
5160 while (oa & OA_OPTIONAL)
5162 if (oa && oa != OA_LIST)
5163 return too_few_arguments(o,PL_op_desc[o->op_type]);
5169 Perl_ck_glob(pTHX_ OP *o)
5173 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
5174 append_elem(OP_GLOB, o, newDEFSVOP());
5176 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
5177 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5179 if (gv && GvIMPORTED_CV(gv)) {
5180 append_elem(OP_GLOB, o,
5181 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
5182 o->op_type = OP_LIST;
5183 o->op_ppaddr = PL_ppaddr[OP_LIST];
5184 cLISTOPo->op_first->op_type = OP_PUSHMARK;
5185 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
5186 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
5187 append_elem(OP_LIST, o,
5188 scalar(newUNOP(OP_RV2CV, 0,
5189 newGVOP(OP_GV, 0, gv)))));
5190 o = newUNOP(OP_NULL, 0, ck_subr(o));
5191 o->op_targ = OP_GLOB; /* hint at what it used to be */
5194 gv = newGVgen("main");
5196 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
5202 Perl_ck_grep(pTHX_ OP *o)
5206 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
5208 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
5209 NewOp(1101, gwop, 1, LOGOP);
5211 if (o->op_flags & OPf_STACKED) {
5214 kid = cLISTOPo->op_first->op_sibling;
5215 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
5218 kid->op_next = (OP*)gwop;
5219 o->op_flags &= ~OPf_STACKED;
5221 kid = cLISTOPo->op_first->op_sibling;
5222 if (type == OP_MAPWHILE)
5229 kid = cLISTOPo->op_first->op_sibling;
5230 if (kid->op_type != OP_NULL)
5231 Perl_croak(aTHX_ "panic: ck_grep");
5232 kid = kUNOP->op_first;
5234 gwop->op_type = type;
5235 gwop->op_ppaddr = PL_ppaddr[type];
5236 gwop->op_first = listkids(o);
5237 gwop->op_flags |= OPf_KIDS;
5238 gwop->op_private = 1;
5239 gwop->op_other = LINKLIST(kid);
5240 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
5241 kid->op_next = (OP*)gwop;
5243 kid = cLISTOPo->op_first->op_sibling;
5244 if (!kid || !kid->op_sibling)
5245 return too_few_arguments(o,PL_op_desc[o->op_type]);
5246 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
5247 mod(kid, OP_GREPSTART);
5253 Perl_ck_index(pTHX_ OP *o)
5255 if (o->op_flags & OPf_KIDS) {
5256 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5258 kid = kid->op_sibling; /* get past "big" */
5259 if (kid && kid->op_type == OP_CONST)
5260 fbm_compile(((SVOP*)kid)->op_sv, 0);
5266 Perl_ck_lengthconst(pTHX_ OP *o)
5268 /* XXX length optimization goes here */
5273 Perl_ck_lfun(pTHX_ OP *o)
5275 OPCODE type = o->op_type;
5276 return modkids(ck_fun(o), type);
5280 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
5283 if ((o->op_flags & OPf_KIDS) && ckWARN(WARN_DEPRECATED)) {
5284 switch (cUNOPo->op_first->op_type) {
5286 break; /* Globals via GV can be undef */
5288 case OP_AASSIGN: /* Is this a good idea? */
5289 Perl_warner(aTHX_ WARN_DEPRECATED,
5290 "defined(@array) is deprecated");
5291 Perl_warner(aTHX_ WARN_DEPRECATED,
5292 "(Maybe you should just omit the defined()?)\n");
5295 break; /* Globals via GV can be undef */
5297 Perl_warner(aTHX_ WARN_DEPRECATED,
5298 "defined(%hash) is deprecated");
5299 Perl_warner(aTHX_ WARN_DEPRECATED,
5300 "(Maybe you should just omit the defined()?)\n");
5311 Perl_ck_rfun(pTHX_ OP *o)
5313 OPCODE type = o->op_type;
5314 return refkids(ck_fun(o), type);
5318 Perl_ck_listiob(pTHX_ OP *o)
5322 kid = cLISTOPo->op_first;
5325 kid = cLISTOPo->op_first;
5327 if (kid->op_type == OP_PUSHMARK)
5328 kid = kid->op_sibling;
5329 if (kid && o->op_flags & OPf_STACKED)
5330 kid = kid->op_sibling;
5331 else if (kid && !kid->op_sibling) { /* print HANDLE; */
5332 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
5333 o->op_flags |= OPf_STACKED; /* make it a filehandle */
5334 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
5335 cLISTOPo->op_first->op_sibling = kid;
5336 cLISTOPo->op_last = kid;
5337 kid = kid->op_sibling;
5342 append_elem(o->op_type, o, newDEFSVOP());
5348 if (PL_hints & HINT_LOCALE)
5349 o->op_private |= OPpLOCALE;
5356 Perl_ck_fun_locale(pTHX_ OP *o)
5362 if (PL_hints & HINT_LOCALE)
5363 o->op_private |= OPpLOCALE;
5370 Perl_ck_sassign(pTHX_ OP *o)
5372 OP *kid = cLISTOPo->op_first;
5373 /* has a disposable target? */
5374 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
5375 && !(kid->op_flags & OPf_STACKED))
5377 OP *kkid = kid->op_sibling;
5379 /* Can just relocate the target. */
5380 if (kkid && kkid->op_type == OP_PADSV
5381 && !(kkid->op_private & OPpLVAL_INTRO))
5383 /* Concat has problems if target is equal to right arg. */
5384 if (kid->op_type == OP_CONCAT) {
5385 if (kLISTOP->op_first->op_sibling->op_type == OP_PADSV
5386 && kLISTOP->op_first->op_sibling->op_targ == kkid->op_targ)
5389 else if (kid->op_type == OP_JOIN) {
5390 /* do_join has problems if the arguments coincide with target.
5391 In fact the second argument *can* safely coincide,
5392 but ignore=pessimize this rare occasion. */
5393 OP *arg = kLISTOP->op_first->op_sibling; /* Skip PUSHMARK */
5396 if (arg->op_type == OP_PADSV
5397 && arg->op_targ == kkid->op_targ)
5399 arg = arg->op_sibling;
5402 else if (kid->op_type == OP_QUOTEMETA) {
5403 /* quotemeta has problems if the argument coincides with target. */
5404 if (kLISTOP->op_first->op_type == OP_PADSV
5405 && kLISTOP->op_first->op_targ == kkid->op_targ)
5408 kid->op_targ = kkid->op_targ;
5409 /* Now we do not need PADSV and SASSIGN. */
5410 kid->op_sibling = o->op_sibling; /* NULL */
5411 cLISTOPo->op_first = NULL;
5414 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
5422 Perl_ck_scmp(pTHX_ OP *o)
5426 if (PL_hints & HINT_LOCALE)
5427 o->op_private |= OPpLOCALE;
5434 Perl_ck_match(pTHX_ OP *o)
5436 o->op_private |= OPpRUNTIME;
5441 Perl_ck_method(pTHX_ OP *o)
5443 OP *kid = cUNOPo->op_first;
5444 if (kid->op_type == OP_CONST) {
5445 SV* sv = kSVOP->op_sv;
5446 if (!(strchr(SvPVX(sv), ':') || strchr(SvPVX(sv), '\''))) {
5448 sv_upgrade(sv, SVt_PVIV);
5450 PERL_HASH(SvUVX(sv), SvPVX(sv), SvCUR(sv));
5451 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
5452 kSVOP->op_sv = Nullsv;
5461 Perl_ck_null(pTHX_ OP *o)
5467 Perl_ck_repeat(pTHX_ OP *o)
5469 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
5470 o->op_private |= OPpREPEAT_DOLIST;
5471 cBINOPo->op_first = force_list(cBINOPo->op_first);
5479 Perl_ck_require(pTHX_ OP *o)
5481 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
5482 SVOP *kid = (SVOP*)cUNOPo->op_first;
5484 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5486 for (s = SvPVX(kid->op_sv); *s; s++) {
5487 if (*s == ':' && s[1] == ':') {
5489 Move(s+2, s+1, strlen(s+2)+1, char);
5490 --SvCUR(kid->op_sv);
5493 sv_catpvn(kid->op_sv, ".pm", 3);
5501 Perl_ck_retarget(pTHX_ OP *o)
5503 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
5510 Perl_ck_select(pTHX_ OP *o)
5513 if (o->op_flags & OPf_KIDS) {
5514 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5515 if (kid && kid->op_sibling) {
5516 o->op_type = OP_SSELECT;
5517 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
5519 return fold_constants(o);
5523 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5524 if (kid && kid->op_type == OP_RV2GV)
5525 kid->op_private &= ~HINT_STRICT_REFS;
5530 Perl_ck_shift(pTHX_ OP *o)
5532 I32 type = o->op_type;
5534 if (!(o->op_flags & OPf_KIDS)) {
5539 if (!CvUNIQUE(PL_compcv)) {
5540 argop = newOP(OP_PADAV, OPf_REF);
5541 argop->op_targ = 0; /* PL_curpad[0] is @_ */
5544 argop = newUNOP(OP_RV2AV, 0,
5545 scalar(newGVOP(OP_GV, 0,
5546 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5549 argop = newUNOP(OP_RV2AV, 0,
5550 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(PL_compcv) ?
5551 PL_defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5552 #endif /* USE_THREADS */
5553 return newUNOP(type, 0, scalar(argop));
5555 return scalar(modkids(ck_fun(o), type));
5559 Perl_ck_sort(pTHX_ OP *o)
5563 if (PL_hints & HINT_LOCALE)
5564 o->op_private |= OPpLOCALE;
5567 if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
5569 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
5570 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5572 kid = kUNOP->op_first; /* get past null */
5574 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
5576 if (kid->op_type == OP_SCOPE) {
5580 else if (kid->op_type == OP_LEAVE) {
5581 if (o->op_type == OP_SORT) {
5582 null(kid); /* wipe out leave */
5585 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
5586 if (k->op_next == kid)
5591 kid->op_next = 0; /* just disconnect the leave */
5592 k = kLISTOP->op_first;
5596 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5597 if (o->op_type == OP_SORT)
5601 o->op_flags |= OPf_SPECIAL;
5603 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
5604 null(cLISTOPo->op_first->op_sibling);
5611 S_simplify_sort(pTHX_ OP *o)
5614 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5617 if (!(o->op_flags & OPf_STACKED))
5619 GvMULTI_on(gv_fetchpv("a", TRUE, SVt_PV));
5620 GvMULTI_on(gv_fetchpv("b", TRUE, SVt_PV));
5621 kid = kUNOP->op_first; /* get past null */
5622 if (kid->op_type != OP_SCOPE)
5624 kid = kLISTOP->op_last; /* get past scope */
5625 switch(kid->op_type) {
5633 k = kid; /* remember this node*/
5634 if (kBINOP->op_first->op_type != OP_RV2SV)
5636 kid = kBINOP->op_first; /* get past cmp */
5637 if (kUNOP->op_first->op_type != OP_GV)
5639 kid = kUNOP->op_first; /* get past rv2sv */
5640 if (GvSTASH(kGVOP->op_gv) != PL_curstash)
5642 if (strEQ(GvNAME(kGVOP->op_gv), "a"))
5644 else if(strEQ(GvNAME(kGVOP->op_gv), "b"))
5648 kid = k; /* back to cmp */
5649 if (kBINOP->op_last->op_type != OP_RV2SV)
5651 kid = kBINOP->op_last; /* down to 2nd arg */
5652 if (kUNOP->op_first->op_type != OP_GV)
5654 kid = kUNOP->op_first; /* get past rv2sv */
5655 if (GvSTASH(kGVOP->op_gv) != PL_curstash
5657 ? strNE(GvNAME(kGVOP->op_gv), "a")
5658 : strNE(GvNAME(kGVOP->op_gv), "b")))
5660 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
5662 o->op_private |= OPpSORT_REVERSE;
5663 if (k->op_type == OP_NCMP)
5664 o->op_private |= OPpSORT_NUMERIC;
5665 if (k->op_type == OP_I_NCMP)
5666 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
5667 kid = cLISTOPo->op_first->op_sibling;
5668 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
5669 op_free(kid); /* then delete it */
5670 cLISTOPo->op_children--;
5674 Perl_ck_split(pTHX_ OP *o)
5678 if (o->op_flags & OPf_STACKED)
5679 return no_fh_allowed(o);
5681 kid = cLISTOPo->op_first;
5682 if (kid->op_type != OP_NULL)
5683 Perl_croak(aTHX_ "panic: ck_split");
5684 kid = kid->op_sibling;
5685 op_free(cLISTOPo->op_first);
5686 cLISTOPo->op_first = kid;
5688 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvn(" ", 1));
5689 cLISTOPo->op_last = kid; /* There was only one element previously */
5692 if (kid->op_type != OP_MATCH) {
5693 OP *sibl = kid->op_sibling;
5694 kid->op_sibling = 0;
5695 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
5696 if (cLISTOPo->op_first == cLISTOPo->op_last)
5697 cLISTOPo->op_last = kid;
5698 cLISTOPo->op_first = kid;
5699 kid->op_sibling = sibl;
5702 kid->op_type = OP_PUSHRE;
5703 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
5706 if (!kid->op_sibling)
5707 append_elem(OP_SPLIT, o, newDEFSVOP());
5709 kid = kid->op_sibling;
5712 if (!kid->op_sibling)
5713 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
5715 kid = kid->op_sibling;
5718 if (kid->op_sibling)
5719 return too_many_arguments(o,PL_op_desc[o->op_type]);
5725 Perl_ck_join(pTHX_ OP *o)
5727 if (ckWARN(WARN_SYNTAX)) {
5728 OP *kid = cLISTOPo->op_first->op_sibling;
5729 if (kid && kid->op_type == OP_MATCH) {
5730 char *pmstr = "STRING";
5731 if (kPMOP->op_pmregexp)
5732 pmstr = kPMOP->op_pmregexp->precomp;
5733 Perl_warner(aTHX_ WARN_SYNTAX,
5734 "/%s/ should probably be written as \"%s\"",
5742 Perl_ck_subr(pTHX_ OP *o)
5745 OP *prev = ((cUNOPo->op_first->op_sibling)
5746 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
5747 OP *o2 = prev->op_sibling;
5756 o->op_private |= OPpENTERSUB_HASTARG;
5757 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
5758 if (cvop->op_type == OP_RV2CV) {
5760 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
5761 null(cvop); /* disable rv2cv */
5762 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
5763 if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) {
5764 cv = GvCVu(tmpop->op_sv);
5766 tmpop->op_private |= OPpEARLY_CV;
5767 else if (SvPOK(cv)) {
5768 namegv = CvANON(cv) ? (GV*)tmpop->op_sv : CvGV(cv);
5769 proto = SvPV((SV*)cv, n_a);
5773 else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
5774 if (o2->op_type == OP_CONST)
5775 o2->op_private &= ~OPpCONST_STRICT;
5776 else if (o2->op_type == OP_LIST) {
5777 OP *o = ((UNOP*)o2)->op_first->op_sibling;
5778 if (o && o->op_type == OP_CONST)
5779 o->op_private &= ~OPpCONST_STRICT;
5782 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5783 if (PERLDB_SUB && PL_curstash != PL_debstash)
5784 o->op_private |= OPpENTERSUB_DB;
5785 while (o2 != cvop) {
5789 return too_many_arguments(o, gv_ename(namegv));
5807 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
5808 bad_type(arg, "block", gv_ename(namegv), o2);
5811 /* '*' allows any scalar type, including bareword */
5814 if (o2->op_type == OP_RV2GV)
5815 goto wrapref; /* autoconvert GLOB -> GLOBref */
5816 else if (o2->op_type == OP_CONST)
5817 o2->op_private &= ~OPpCONST_STRICT;
5818 else if (o2->op_type == OP_ENTERSUB) {
5819 /* accidental subroutine, revert to bareword */
5820 OP *gvop = ((UNOP*)o2)->op_first;
5821 if (gvop && gvop->op_type == OP_NULL) {
5822 gvop = ((UNOP*)gvop)->op_first;
5824 for (; gvop->op_sibling; gvop = gvop->op_sibling)
5827 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
5828 (gvop = ((UNOP*)gvop)->op_first) &&
5829 gvop->op_type == OP_GV)
5831 GV *gv = (GV*)((SVOP*)gvop)->op_sv;
5832 OP *sibling = o2->op_sibling;
5833 SV *n = newSVpvn("",0);
5835 gv_fullname3(n, gv, "");
5836 if (SvCUR(n)>6 && strnEQ(SvPVX(n),"main::",6))
5837 sv_chop(n, SvPVX(n)+6);
5838 o2 = newSVOP(OP_CONST, 0, n);
5839 prev->op_sibling = o2;
5840 o2->op_sibling = sibling;
5852 if (o2->op_type != OP_RV2GV)
5853 bad_type(arg, "symbol", gv_ename(namegv), o2);
5856 if (o2->op_type != OP_RV2CV)
5857 bad_type(arg, "sub", gv_ename(namegv), o2);
5860 if (o2->op_type != OP_RV2SV
5861 && o2->op_type != OP_PADSV
5862 && o2->op_type != OP_HELEM
5863 && o2->op_type != OP_AELEM
5864 && o2->op_type != OP_THREADSV)
5866 bad_type(arg, "scalar", gv_ename(namegv), o2);
5870 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
5871 bad_type(arg, "array", gv_ename(namegv), o2);
5874 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
5875 bad_type(arg, "hash", gv_ename(namegv), o2);
5879 OP* sib = kid->op_sibling;
5880 kid->op_sibling = 0;
5881 o2 = newUNOP(OP_REFGEN, 0, kid);
5882 o2->op_sibling = sib;
5883 prev->op_sibling = o2;
5894 Perl_croak(aTHX_ "Malformed prototype for %s: %s",
5895 gv_ename(namegv), SvPV((SV*)cv, n_a));
5900 mod(o2, OP_ENTERSUB);
5902 o2 = o2->op_sibling;
5904 if (proto && !optional &&
5905 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
5906 return too_few_arguments(o, gv_ename(namegv));
5911 Perl_ck_svconst(pTHX_ OP *o)
5913 SvREADONLY_on(cSVOPo->op_sv);
5918 Perl_ck_trunc(pTHX_ OP *o)
5920 if (o->op_flags & OPf_KIDS) {
5921 SVOP *kid = (SVOP*)cUNOPo->op_first;
5923 if (kid->op_type == OP_NULL)
5924 kid = (SVOP*)kid->op_sibling;
5925 if (kid && kid->op_type == OP_CONST &&
5926 (kid->op_private & OPpCONST_BARE))
5928 o->op_flags |= OPf_SPECIAL;
5929 kid->op_private &= ~OPpCONST_STRICT;
5935 /* A peephole optimizer. We visit the ops in the order they're to execute. */
5938 Perl_peep(pTHX_ register OP *o)
5941 register OP* oldop = 0;
5943 OP *last_composite = Nullop;
5945 if (!o || o->op_seq)
5949 SAVESPTR(PL_curcop);
5950 for (; o; o = o->op_next) {
5956 switch (o->op_type) {
5960 PL_curcop = ((COP*)o); /* for warnings */
5961 o->op_seq = PL_op_seqmax++;
5962 last_composite = Nullop;
5966 if (cSVOPo->op_private & OPpCONST_STRICT)
5967 no_bareword_allowed(o);
5973 if ( o->op_next && o->op_next->op_type == OP_STRINGIFY
5974 && !(o->op_next->op_private & OPpTARGET_MY) )
5976 o->op_seq = PL_op_seqmax++;
5981 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
5982 if (o->op_next->op_private & OPpTARGET_MY) {
5983 if ((o->op_flags & OPf_STACKED) /* chained concats */
5984 || (o->op_type == OP_CONCAT
5985 /* Concat has problems if target is equal to right arg. */
5986 && (((LISTOP*)o)->op_first->op_sibling->op_type
5988 && (((LISTOP*)o)->op_first->op_sibling->op_targ
5989 == o->op_next->op_targ))) {
5990 goto ignore_optimization;
5993 o->op_targ = o->op_next->op_targ;
5994 o->op_private |= OPpTARGET_MY;
5999 ignore_optimization:
6000 o->op_seq = PL_op_seqmax++;
6003 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
6004 o->op_seq = PL_op_seqmax++;
6005 break; /* Scalar stub must produce undef. List stub is noop */
6009 if (o->op_targ == OP_NEXTSTATE
6010 || o->op_targ == OP_DBSTATE
6011 || o->op_targ == OP_SETSTATE)
6013 PL_curcop = ((COP*)o);
6020 if (oldop && o->op_next) {
6021 oldop->op_next = o->op_next;
6024 o->op_seq = PL_op_seqmax++;
6028 if (o->op_next->op_type == OP_RV2SV) {
6029 if (!(o->op_next->op_private & OPpDEREF)) {
6031 o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
6032 o->op_next = o->op_next->op_next;
6033 o->op_type = OP_GVSV;
6034 o->op_ppaddr = PL_ppaddr[OP_GVSV];
6037 else if (o->op_next->op_type == OP_RV2AV) {
6038 OP* pop = o->op_next->op_next;
6040 if (pop->op_type == OP_CONST &&
6041 (PL_op = pop->op_next) &&
6042 pop->op_next->op_type == OP_AELEM &&
6043 !(pop->op_next->op_private &
6044 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
6045 (i = SvIV(((SVOP*)pop)->op_sv) - PL_compiling.cop_arybase)
6052 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
6053 o->op_next = pop->op_next->op_next;
6054 o->op_type = OP_AELEMFAST;
6055 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
6056 o->op_private = (U8)i;
6057 GvAVn(((GVOP*)o)->op_gv);
6060 else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_UNSAFE)) {
6061 GV *gv = cGVOPo->op_gv;
6062 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX(GvCV(gv))) {
6063 /* XXX could check prototype here instead of just carping */
6064 SV *sv = sv_newmortal();
6065 gv_efullname3(sv, gv, Nullch);
6066 Perl_warner(aTHX_ WARN_UNSAFE,
6067 "%s() called too early to check prototype",
6072 o->op_seq = PL_op_seqmax++;
6083 o->op_seq = PL_op_seqmax++;
6084 while (cLOGOP->op_other->op_type == OP_NULL)
6085 cLOGOP->op_other = cLOGOP->op_other->op_next;
6086 peep(cLOGOP->op_other);
6090 o->op_seq = PL_op_seqmax++;
6091 peep(cLOOP->op_redoop);
6092 peep(cLOOP->op_nextop);
6093 peep(cLOOP->op_lastop);
6099 o->op_seq = PL_op_seqmax++;
6100 peep(cPMOP->op_pmreplstart);
6104 o->op_seq = PL_op_seqmax++;
6105 if (ckWARN(WARN_SYNTAX) && o->op_next
6106 && o->op_next->op_type == OP_NEXTSTATE) {
6107 if (o->op_next->op_sibling &&
6108 o->op_next->op_sibling->op_type != OP_EXIT &&
6109 o->op_next->op_sibling->op_type != OP_WARN &&
6110 o->op_next->op_sibling->op_type != OP_DIE) {
6111 line_t oldline = PL_curcop->cop_line;
6113 PL_curcop->cop_line = ((COP*)o->op_next)->cop_line;
6114 Perl_warner(aTHX_ WARN_SYNTAX, "Statement unlikely to be reached");
6115 Perl_warner(aTHX_ WARN_SYNTAX, "(Maybe you meant system() when you said exec()?)\n");
6116 PL_curcop->cop_line = oldline;
6130 if ((o->op_private & (OPpLVAL_INTRO))
6131 || ((BINOP*)o)->op_last->op_type != OP_CONST)
6133 rop = (UNOP*)((BINOP*)o)->op_first;
6134 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6136 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6137 if (!SvOBJECT(lexname))
6139 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6140 if (!fields || !GvHV(*fields))
6142 svp = &((SVOP*)((BINOP*)o)->op_last)->op_sv;
6143 key = SvPV(*svp, keylen);
6144 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
6146 Perl_croak(aTHX_ "No such field \"%s\" in variable %s of type %s",
6147 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
6149 ind = SvIV(*indsvp);
6151 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6152 rop->op_type = OP_RV2AV;
6153 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6154 o->op_type = OP_AELEM;
6155 o->op_ppaddr = PL_ppaddr[OP_AELEM];
6157 *svp = newSViv(ind);
6163 if (!(o->op_flags & OPf_WANT)
6164 || o->op_flags & OPf_WANT == OPf_WANT_LIST)
6166 o->op_seq = PL_op_seqmax++;
6170 if (o->op_next->op_type != OP_LEAVESUBLV) {
6171 o->op_seq = PL_op_seqmax++;
6177 if (last_composite) {
6178 OP *r = last_composite;
6180 while (r->op_sibling)
6183 || (r->op_next->op_type == OP_LIST
6184 && r->op_next->op_next == o))
6186 if (last_composite->op_type == OP_RV2AV)
6187 yyerror("Lvalue subs returning arrays not implemented yet");
6189 yyerror("Lvalue subs returning hashes not implemented yet");
6196 o->op_seq = PL_op_seqmax++;