3 * Copyright (c) 1991-1999, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
12 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
13 * youngest of the Old Took's daughters); and Mr. Drogo was his second
14 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
15 * either way, as the saying is, if you follow me." --the Gaffer
23 /* #define PL_OP_SLAB_ALLOC */
26 #define OP_REFCNT_LOCK NOOP
27 #define OP_REFCNT_UNLOCK NOOP
28 #define OpREFCNT_set(o,n) NOOP
29 #define OpREFCNT_dec(o) 0
31 #ifdef PL_OP_SLAB_ALLOC
32 #define SLAB_SIZE 8192
33 static char *PL_OpPtr = NULL;
34 static int PL_OpSpace = 0;
35 #define NewOp(m,var,c,type) do { if ((PL_OpSpace -= c*sizeof(type)) >= 0) \
36 var = (type *)(PL_OpPtr -= c*sizeof(type)); \
38 var = (type *) Slab_Alloc(m,c*sizeof(type)); \
42 S_Slab_Alloc(pTHX_ int m, size_t sz)
44 Newz(m,PL_OpPtr,SLAB_SIZE,char);
45 PL_OpSpace = SLAB_SIZE - sz;
46 return PL_OpPtr += PL_OpSpace;
50 #define NewOp(m, var, c, type) Newz(m, var, c, type)
53 * In the following definition, the ", Nullop" is just to make the compiler
54 * think the expression is of the right type: croak actually does a Siglongjmp.
56 #define CHECKOP(type,o) \
57 ((PL_op_mask && PL_op_mask[type]) \
58 ? ( op_free((OP*)o), \
59 Perl_croak(aTHX_ "%s trapped by operation mask", PL_op_desc[type]), \
61 : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o))
63 #define PAD_MAX 999999999
66 S_gv_ename(pTHX_ GV *gv)
69 SV* tmpsv = sv_newmortal();
70 gv_efullname3(tmpsv, gv, Nullch);
71 return SvPV(tmpsv,n_a);
75 S_no_fh_allowed(pTHX_ OP *o)
77 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
78 PL_op_desc[o->op_type]));
83 S_too_few_arguments(pTHX_ OP *o, char *name)
85 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
90 S_too_many_arguments(pTHX_ OP *o, char *name)
92 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
97 S_bad_type(pTHX_ I32 n, char *t, char *name, OP *kid)
99 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
100 (int)n, name, t, PL_op_desc[kid->op_type]));
104 S_no_bareword_allowed(pTHX_ OP *o)
106 qerror(Perl_mess(aTHX_
107 "Bareword \"%s\" not allowed while \"strict subs\" in use",
108 SvPV_nolen(cSVOPo->op_sv)));
111 /* "register" allocation */
114 Perl_pad_allocmy(pTHX_ char *name)
121 PL_in_my == KEY_our ||
123 (PL_hints & HINT_UTF8 && (name[1] & 0xc0) == 0xc0) ||
124 name[1] == '_' && (int)strlen(name) > 2 ))
126 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
127 /* 1999-02-27 mjd@plover.com */
129 p = strchr(name, '\0');
130 /* The next block assumes the buffer is at least 205 chars
131 long. At present, it's always at least 256 chars. */
133 strcpy(name+200, "...");
139 /* Move everything else down one character */
140 for (; p-name > 2; p--)
142 name[2] = toCTRL(name[1]);
145 yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
147 if (ckWARN(WARN_UNSAFE) && AvFILLp(PL_comppad_name) >= 0) {
148 SV **svp = AvARRAY(PL_comppad_name);
149 for (off = AvFILLp(PL_comppad_name); off > PL_comppad_name_floor; off--) {
151 && sv != &PL_sv_undef
152 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
153 && strEQ(name, SvPVX(sv)))
155 Perl_warner(aTHX_ WARN_UNSAFE,
156 "\"%s\" variable %s masks earlier declaration in same %s",
157 (PL_in_my == KEY_our ? "our" : "my"),
159 (SvIVX(sv) == PAD_MAX ? "scope" : "statement"));
164 off = pad_alloc(OP_PADSV, SVs_PADMY);
166 sv_upgrade(sv, SVt_PVNV);
168 if (PL_in_my_stash) {
170 yyerror(Perl_form(aTHX_ "Can't declare class for non-scalar %s in \"my\"",
173 (void)SvUPGRADE(sv, SVt_PVMG);
174 SvSTASH(sv) = (HV*)SvREFCNT_inc(PL_in_my_stash);
177 if (PL_in_my == KEY_our)
178 SvFLAGS(sv) |= SVpad_OUR;
179 av_store(PL_comppad_name, off, sv);
180 SvNVX(sv) = (NV)PAD_MAX;
181 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
182 if (!PL_min_intro_pending)
183 PL_min_intro_pending = off;
184 PL_max_intro_pending = off;
186 av_store(PL_comppad, off, (SV*)newAV());
187 else if (*name == '%')
188 av_store(PL_comppad, off, (SV*)newHV());
189 SvPADMY_on(PL_curpad[off]);
193 #define FINDLEX_NOSEARCH 1 /* don't search outer contexts */
196 S_pad_findlex(pTHX_ char *name, PADOFFSET newoff, U32 seq, CV* startcv,
197 I32 cx_ix, I32 saweval, U32 flags)
204 register PERL_CONTEXT *cx;
206 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
207 AV *curlist = CvPADLIST(cv);
208 SV **svp = av_fetch(curlist, 0, FALSE);
211 if (!svp || *svp == &PL_sv_undef)
214 svp = AvARRAY(curname);
215 for (off = AvFILLp(curname); off > 0; off--) {
216 if ((sv = svp[off]) &&
217 sv != &PL_sv_undef &&
219 seq > I_32(SvNVX(sv)) &&
220 strEQ(SvPVX(sv), name))
231 return 0; /* don't clone from inactive stack frame */
235 oldpad = (AV*)*av_fetch(curlist, depth, FALSE);
236 oldsv = *av_fetch(oldpad, off, TRUE);
237 if (!newoff) { /* Not a mere clone operation. */
238 SV *namesv = NEWSV(1103,0);
239 newoff = pad_alloc(OP_PADSV, SVs_PADMY);
240 sv_upgrade(namesv, SVt_PVNV);
241 sv_setpv(namesv, name);
242 av_store(PL_comppad_name, newoff, namesv);
243 SvNVX(namesv) = (NV)PL_curcop->cop_seq;
244 SvIVX(namesv) = PAD_MAX; /* A ref, intro immediately */
245 SvFAKE_on(namesv); /* A ref, not a real var */
246 if (SvFLAGS(sv) & SVpad_OUR)/* An "our" variable */
247 SvFLAGS(namesv) |= SVpad_OUR;
248 if (SvOBJECT(sv)) { /* A typed var */
250 (void)SvUPGRADE(namesv, SVt_PVMG);
251 SvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)SvSTASH(sv));
254 if (CvANON(PL_compcv) || SvTYPE(PL_compcv) == SVt_PVFM) {
255 /* "It's closures all the way down." */
256 CvCLONE_on(PL_compcv);
258 if (CvANON(PL_compcv))
259 oldsv = Nullsv; /* no need to keep ref */
264 bcv && bcv != cv && !CvCLONE(bcv);
265 bcv = CvOUTSIDE(bcv))
270 if (ckWARN(WARN_CLOSURE)
271 && !CvUNIQUE(bcv) && !CvUNIQUE(cv))
273 Perl_warner(aTHX_ WARN_CLOSURE,
274 "Variable \"%s\" may be unavailable",
282 else if (!CvUNIQUE(PL_compcv)) {
283 if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv))
284 Perl_warner(aTHX_ WARN_CLOSURE,
285 "Variable \"%s\" will not stay shared", name);
288 av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv));
294 if (flags & FINDLEX_NOSEARCH)
297 /* Nothing in current lexical context--try eval's context, if any.
298 * This is necessary to let the perldb get at lexically scoped variables.
299 * XXX This will also probably interact badly with eval tree caching.
302 for (i = cx_ix; i >= 0; i--) {
304 switch (CxTYPE(cx)) {
306 if (i == 0 && saweval) {
307 seq = cxstack[saweval].blk_oldcop->cop_seq;
308 return pad_findlex(name, newoff, seq, PL_main_cv, -1, saweval, 0);
312 switch (cx->blk_eval.old_op_type) {
318 /* require must have its own scope */
326 if (PL_debstash && CvSTASH(cv) == PL_debstash) { /* ignore DB'* scope */
327 saweval = i; /* so we know where we were called from */
330 seq = cxstack[saweval].blk_oldcop->cop_seq;
331 return pad_findlex(name, newoff, seq, cv, i-1, saweval,FINDLEX_NOSEARCH);
339 Perl_pad_findmy(pTHX_ char *name)
345 SV **svp = AvARRAY(PL_comppad_name);
346 U32 seq = PL_cop_seqmax;
352 * Special case to get lexical (and hence per-thread) @_.
353 * XXX I need to find out how to tell at parse-time whether use
354 * of @_ should refer to a lexical (from a sub) or defgv (global
355 * scope and maybe weird sub-ish things like formats). See
356 * startsub in perly.y. It's possible that @_ could be lexical
357 * (at least from subs) even in non-threaded perl.
359 if (strEQ(name, "@_"))
360 return 0; /* success. (NOT_IN_PAD indicates failure) */
361 #endif /* USE_THREADS */
363 /* The one we're looking for is probably just before comppad_name_fill. */
364 for (off = AvFILLp(PL_comppad_name); off > 0; off--) {
365 if ((sv = svp[off]) &&
366 sv != &PL_sv_undef &&
369 seq > I_32(SvNVX(sv)))) &&
370 strEQ(SvPVX(sv), name))
372 if (SvIVX(sv) || SvFLAGS(sv) & SVpad_OUR)
373 return (PADOFFSET)off;
374 pendoff = off; /* this pending def. will override import */
378 outside = CvOUTSIDE(PL_compcv);
380 /* Check if if we're compiling an eval'', and adjust seq to be the
381 * eval's seq number. This depends on eval'' having a non-null
382 * CvOUTSIDE() while it is being compiled. The eval'' itself is
383 * identified by CvEVAL being true and CvGV being null. */
384 if (outside && CvEVAL(PL_compcv) && !CvGV(PL_compcv) && cxstack_ix >= 0) {
385 cx = &cxstack[cxstack_ix];
387 seq = cx->blk_oldcop->cop_seq;
390 /* See if it's in a nested scope */
391 off = pad_findlex(name, 0, seq, outside, cxstack_ix, 0, 0);
393 /* If there is a pending local definition, this new alias must die */
395 SvIVX(AvARRAY(PL_comppad_name)[off]) = seq;
396 return off; /* pad_findlex returns 0 for failure...*/
398 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
402 Perl_pad_leavemy(pTHX_ I32 fill)
406 SV **svp = AvARRAY(PL_comppad_name);
408 if (PL_min_intro_pending && fill < PL_min_intro_pending) {
409 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
410 if ((sv = svp[off]) && sv != &PL_sv_undef && ckWARN_d(WARN_INTERNAL))
411 Perl_warner(aTHX_ WARN_INTERNAL, "%s never introduced", SvPVX(sv));
414 /* "Deintroduce" my variables that are leaving with this scope. */
415 for (off = AvFILLp(PL_comppad_name); off > fill; off--) {
416 if ((sv = svp[off]) && sv != &PL_sv_undef && SvIVX(sv) == PAD_MAX)
417 SvIVX(sv) = PL_cop_seqmax;
422 Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype)
428 if (AvARRAY(PL_comppad) != PL_curpad)
429 Perl_croak(aTHX_ "panic: pad_alloc");
430 if (PL_pad_reset_pending)
432 if (tmptype & SVs_PADMY) {
434 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
435 } while (SvPADBUSY(sv)); /* need a fresh one */
436 retval = AvFILLp(PL_comppad);
439 SV **names = AvARRAY(PL_comppad_name);
440 SSize_t names_fill = AvFILLp(PL_comppad_name);
443 * "foreach" index vars temporarily become aliases to non-"my"
444 * values. Thus we must skip, not just pad values that are
445 * marked as current pad values, but also those with names.
447 if (++PL_padix <= names_fill &&
448 (sv = names[PL_padix]) && sv != &PL_sv_undef)
450 sv = *av_fetch(PL_comppad, PL_padix, TRUE);
451 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)))
456 SvFLAGS(sv) |= tmptype;
457 PL_curpad = AvARRAY(PL_comppad);
459 DEBUG_X(PerlIO_printf(Perl_debug_log,
460 "0x%"UVxf" Pad 0x%"UVxf" alloc %ld for %s\n",
461 PTR2UV(thr), PTR2UV(PL_curpad),
462 (long) retval, PL_op_name[optype]));
464 DEBUG_X(PerlIO_printf(Perl_debug_log,
465 "Pad 0x%"UVxf" alloc %ld for %s\n",
467 (long) retval, PL_op_name[optype]));
468 #endif /* USE_THREADS */
469 return (PADOFFSET)retval;
473 Perl_pad_sv(pTHX_ PADOFFSET po)
477 DEBUG_X(PerlIO_printf(Perl_debug_log,
478 "0x%"UVxf" Pad 0x%"UVxf" sv %d\n",
479 PTR2UV(thr), PTR2UV(PL_curpad), po));
482 Perl_croak(aTHX_ "panic: pad_sv po");
483 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" sv %d\n",
484 PTR2UV(PL_curpad), po));
485 #endif /* USE_THREADS */
486 return PL_curpad[po]; /* eventually we'll turn this into a macro */
490 Perl_pad_free(pTHX_ PADOFFSET po)
495 if (AvARRAY(PL_comppad) != PL_curpad)
496 Perl_croak(aTHX_ "panic: pad_free curpad");
498 Perl_croak(aTHX_ "panic: pad_free po");
500 DEBUG_X(PerlIO_printf(Perl_debug_log,
501 "0x%"UVxf" Pad 0x%"UVxf" free %d\n",
502 PTR2UV(thr), PTR2UV(PL_curpad), po));
504 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" free %d\n",
505 PTR2UV(PL_curpad), po));
506 #endif /* USE_THREADS */
507 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef)
508 SvPADTMP_off(PL_curpad[po]);
509 if ((I32)po < PL_padix)
514 Perl_pad_swipe(pTHX_ PADOFFSET po)
517 if (AvARRAY(PL_comppad) != PL_curpad)
518 Perl_croak(aTHX_ "panic: pad_swipe curpad");
520 Perl_croak(aTHX_ "panic: pad_swipe po");
522 DEBUG_X(PerlIO_printf(Perl_debug_log,
523 "0x%"UVxf" Pad 0x%"UVxf" swipe %d\n",
524 PTR2UV(thr), PTR2UV(PL_curpad), po));
526 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" swipe %d\n",
527 PTR2UV(PL_curpad), po));
528 #endif /* USE_THREADS */
529 SvPADTMP_off(PL_curpad[po]);
530 PL_curpad[po] = NEWSV(1107,0);
531 SvPADTMP_on(PL_curpad[po]);
532 if ((I32)po < PL_padix)
536 /* XXX pad_reset() is currently disabled because it results in serious bugs.
537 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
538 * on the stack by OPs that use them, there are several ways to get an alias
539 * to a shared TARG. Such an alias will change randomly and unpredictably.
540 * We avoid doing this until we can think of a Better Way.
545 #ifdef USE_BROKEN_PAD_RESET
549 if (AvARRAY(PL_comppad) != PL_curpad)
550 Perl_croak(aTHX_ "panic: pad_reset curpad");
552 DEBUG_X(PerlIO_printf(Perl_debug_log,
553 "0x%"UVxf" Pad 0x%"UVxf" reset\n",
554 PTR2UV(thr), PTR2UV(PL_curpad)));
556 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" reset\n",
558 #endif /* USE_THREADS */
559 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
560 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
561 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
562 SvPADTMP_off(PL_curpad[po]);
564 PL_padix = PL_padix_floor;
567 PL_pad_reset_pending = FALSE;
571 /* find_threadsv is not reentrant */
573 Perl_find_threadsv(pTHX_ const char *name)
579 /* We currently only handle names of a single character */
580 p = strchr(PL_threadsv_names, *name);
583 key = p - PL_threadsv_names;
584 MUTEX_LOCK(&thr->mutex);
585 svp = av_fetch(thr->threadsv, key, FALSE);
587 MUTEX_UNLOCK(&thr->mutex);
589 SV *sv = NEWSV(0, 0);
590 av_store(thr->threadsv, key, sv);
591 thr->threadsvp = AvARRAY(thr->threadsv);
592 MUTEX_UNLOCK(&thr->mutex);
594 * Some magic variables used to be automagically initialised
595 * in gv_fetchpv. Those which are now per-thread magicals get
596 * initialised here instead.
602 sv_setpv(sv, "\034");
603 sv_magic(sv, 0, 0, name, 1);
608 PL_sawampersand = TRUE;
622 /* XXX %! tied to Errno.pm needs to be added here.
623 * See gv_fetchpv(). */
627 sv_magic(sv, 0, 0, name, 1);
629 DEBUG_S(PerlIO_printf(Perl_error_log,
630 "find_threadsv: new SV %p for $%s%c\n",
631 sv, (*name < 32) ? "^" : "",
632 (*name < 32) ? toCTRL(*name) : *name));
636 #endif /* USE_THREADS */
641 Perl_op_free(pTHX_ OP *o)
643 register OP *kid, *nextkid;
646 if (!o || o->op_seq == (U16)-1)
649 if (o->op_private & OPpREFCOUNTED) {
650 switch (o->op_type) {
658 if (OpREFCNT_dec(o)) {
669 if (o->op_flags & OPf_KIDS) {
670 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
671 nextkid = kid->op_sibling; /* Get before next freeing kid */
679 /* COP* is not cleared by op_clear() so that we may track line
680 * numbers etc even after null() */
681 if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
686 #ifdef PL_OP_SLAB_ALLOC
687 if ((char *) o == PL_OpPtr)
696 S_op_clear(pTHX_ OP *o)
698 switch (o->op_type) {
699 case OP_NULL: /* Was holding old type, if any. */
700 case OP_ENTEREVAL: /* Was holding hints. */
702 case OP_THREADSV: /* Was holding index into thr->threadsv AV. */
708 if (!(o->op_flags & OPf_SPECIAL))
711 #endif /* USE_THREADS */
713 if (!(o->op_flags & OPf_REF)
714 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
722 SvREFCNT_dec(cGVOPo);
723 PL_curpad[cPADOPo->op_padix] = Nullsv;
725 pad_free(cPADOPo->op_padix);
726 cPADOPo->op_padix = 0;
728 SvREFCNT_dec(cGVOPo);
729 cSVOPo->op_sv = Nullsv;
733 SvREFCNT_dec(cSVOPo->op_sv);
734 cSVOPo->op_sv = Nullsv;
740 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
744 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
745 SvREFCNT_dec(cSVOPo->op_sv);
746 cSVOPo->op_sv = Nullsv;
749 Safefree(cPVOPo->op_pv);
750 cPVOPo->op_pv = Nullch;
754 op_free(cPMOPo->op_pmreplroot);
755 cPMOPo->op_pmreplroot = Nullop;
760 ReREFCNT_dec(cPMOPo->op_pmregexp);
761 cPMOPo->op_pmregexp = (REGEXP*)NULL;
766 pad_free(o->op_targ);
770 S_cop_free(pTHX_ COP* cop)
772 Safefree(cop->cop_label);
773 SvREFCNT_dec(CopFILEGV(cop));
774 if (! specialWARN(cop->cop_warnings))
775 SvREFCNT_dec(cop->cop_warnings);
781 if (o->op_type == OP_NULL)
784 o->op_targ = o->op_type;
785 o->op_type = OP_NULL;
786 o->op_ppaddr = PL_ppaddr[OP_NULL];
789 /* Contextualizers */
791 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
794 Perl_linklist(pTHX_ OP *o)
801 /* establish postfix order */
802 if (cUNOPo->op_first) {
803 o->op_next = LINKLIST(cUNOPo->op_first);
804 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
806 kid->op_next = LINKLIST(kid->op_sibling);
818 Perl_scalarkids(pTHX_ OP *o)
821 if (o && o->op_flags & OPf_KIDS) {
822 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
829 S_scalarboolean(pTHX_ OP *o)
831 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
833 if (ckWARN(WARN_SYNTAX)) {
834 line_t oldline = PL_curcop->cop_line;
836 if (PL_copline != NOLINE)
837 PL_curcop->cop_line = PL_copline;
838 Perl_warner(aTHX_ WARN_SYNTAX, "Found = in conditional, should be ==");
839 PL_curcop->cop_line = oldline;
846 Perl_scalar(pTHX_ OP *o)
850 /* assumes no premature commitment */
851 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
852 || o->op_type == OP_RETURN)
857 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
859 switch (o->op_type) {
861 if (o->op_private & OPpREPEAT_DOLIST)
862 null(((LISTOP*)cBINOPo->op_first)->op_first);
863 scalar(cBINOPo->op_first);
868 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
872 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
873 if (!kPMOP->op_pmreplroot)
874 deprecate("implicit split to @_");
882 if (o->op_flags & OPf_KIDS) {
883 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
889 kid = cLISTOPo->op_first;
891 while (kid = kid->op_sibling) {
897 WITH_THR(PL_curcop = &PL_compiling);
902 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
908 WITH_THR(PL_curcop = &PL_compiling);
915 Perl_scalarvoid(pTHX_ OP *o)
922 if (o->op_type == OP_NEXTSTATE
923 || o->op_type == OP_SETSTATE
924 || o->op_type == OP_DBSTATE
925 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
926 || o->op_targ == OP_SETSTATE
927 || o->op_targ == OP_DBSTATE)))
930 PL_curcop = (COP*)o; /* for warning below */
933 /* assumes no premature commitment */
934 want = o->op_flags & OPf_WANT;
935 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
936 || o->op_type == OP_RETURN)
941 if ((o->op_private & OPpTARGET_MY)
942 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
944 return scalar(o); /* As if inside SASSIGN */
947 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
949 switch (o->op_type) {
951 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
955 if (o->op_flags & OPf_STACKED)
959 if (o->op_private == 4)
1001 case OP_GETSOCKNAME:
1002 case OP_GETPEERNAME:
1007 case OP_GETPRIORITY:
1030 if (!(o->op_private & OPpLVAL_INTRO))
1031 useless = PL_op_desc[o->op_type];
1038 if (!(o->op_private & OPpLVAL_INTRO) &&
1039 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1040 useless = "a variable";
1045 if (cSVOPo->op_private & OPpCONST_STRICT)
1046 no_bareword_allowed(o);
1049 if (ckWARN(WARN_VOID)) {
1050 useless = "a constant";
1051 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1053 else if (SvPOK(sv)) {
1054 if (strnEQ(SvPVX(sv), "di", 2) ||
1055 strnEQ(SvPVX(sv), "ds", 2) ||
1056 strnEQ(SvPVX(sv), "ig", 2))
1061 null(o); /* don't execute or even remember it */
1065 o->op_type = OP_PREINC; /* pre-increment is faster */
1066 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1070 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1071 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1077 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1082 if (o->op_flags & OPf_STACKED)
1090 if (!(o->op_flags & OPf_KIDS))
1099 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1106 /* all requires must return a boolean value */
1107 o->op_flags &= ~OPf_WANT;
1110 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
1111 if (!kPMOP->op_pmreplroot)
1112 deprecate("implicit split to @_");
1118 if (ckWARN(WARN_VOID))
1119 Perl_warner(aTHX_ WARN_VOID, "Useless use of %s in void context", useless);
1125 Perl_listkids(pTHX_ OP *o)
1128 if (o && o->op_flags & OPf_KIDS) {
1129 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1136 Perl_list(pTHX_ OP *o)
1140 /* assumes no premature commitment */
1141 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
1142 || o->op_type == OP_RETURN)
1147 if ((o->op_private & OPpTARGET_MY)
1148 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1150 return o; /* As if inside SASSIGN */
1153 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1155 switch (o->op_type) {
1158 list(cBINOPo->op_first);
1163 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1171 if (!(o->op_flags & OPf_KIDS))
1173 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1174 list(cBINOPo->op_first);
1175 return gen_constant_list(o);
1182 kid = cLISTOPo->op_first;
1184 while (kid = kid->op_sibling) {
1185 if (kid->op_sibling)
1190 WITH_THR(PL_curcop = &PL_compiling);
1194 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1195 if (kid->op_sibling)
1200 WITH_THR(PL_curcop = &PL_compiling);
1203 /* all requires must return a boolean value */
1204 o->op_flags &= ~OPf_WANT;
1211 Perl_scalarseq(pTHX_ OP *o)
1216 if (o->op_type == OP_LINESEQ ||
1217 o->op_type == OP_SCOPE ||
1218 o->op_type == OP_LEAVE ||
1219 o->op_type == OP_LEAVETRY)
1222 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1223 if (kid->op_sibling) {
1227 PL_curcop = &PL_compiling;
1229 o->op_flags &= ~OPf_PARENS;
1230 if (PL_hints & HINT_BLOCK_SCOPE)
1231 o->op_flags |= OPf_PARENS;
1234 o = newOP(OP_STUB, 0);
1239 S_modkids(pTHX_ OP *o, I32 type)
1242 if (o && o->op_flags & OPf_KIDS) {
1243 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1250 Perl_mod(pTHX_ OP *o, I32 type)
1257 if (!o || PL_error_count)
1260 if ((o->op_private & OPpTARGET_MY)
1261 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1266 switch (o->op_type) {
1271 if (!(o->op_private & (OPpCONST_ARYBASE)))
1273 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1274 PL_compiling.cop_arybase = (I32)SvIV(((SVOP*)PL_eval_start)->op_sv);
1278 SAVEI32(PL_compiling.cop_arybase);
1279 PL_compiling.cop_arybase = 0;
1281 else if (type == OP_REFGEN)
1284 Perl_croak(aTHX_ "That use of $[ is unsupported");
1287 if (o->op_flags & OPf_PARENS)
1291 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1292 !(o->op_flags & OPf_STACKED)) {
1293 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1294 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1295 assert(cUNOPo->op_first->op_type == OP_NULL);
1296 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1299 else { /* lvalue subroutine call */
1300 o->op_private |= OPpLVAL_INTRO;
1301 if (type == OP_GREPSTART || type == OP_ENTERSUB) {
1302 /* Backward compatibility mode: */
1303 o->op_private |= OPpENTERSUB_INARGS;
1306 else { /* Compile-time error message: */
1307 OP *kid = cUNOPo->op_first;
1311 if (kid->op_type == OP_PUSHMARK)
1313 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1315 "panic: unexpected lvalue entersub "
1316 "args: type/targ %ld:%ld",
1317 (long)kid->op_type,kid->op_targ);
1318 kid = kLISTOP->op_first;
1320 while (kid->op_sibling)
1321 kid = kid->op_sibling;
1322 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1324 if (kid->op_type == OP_METHOD_NAMED
1325 || kid->op_type == OP_METHOD)
1329 if (kid->op_sibling || kid->op_next != kid) {
1330 yyerror("panic: unexpected optree near method call");
1334 NewOp(1101, newop, 1, OP);
1335 newop->op_type = OP_RV2CV;
1336 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1337 newop->op_next = newop;
1338 kid->op_sibling = newop;
1339 newop->op_private |= OPpLVAL_INTRO;
1343 if (kid->op_type != OP_RV2CV)
1345 "panic: unexpected lvalue entersub "
1346 "entry via type/targ %ld:%ld",
1347 (long)kid->op_type,kid->op_targ);
1348 kid->op_private |= OPpLVAL_INTRO;
1349 break; /* Postpone until runtime */
1353 kid = kUNOP->op_first;
1354 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1355 kid = kUNOP->op_first;
1356 if (kid->op_type == OP_NULL)
1358 "Unexpected constant lvalue entersub "
1359 "entry via type/targ %ld:%ld",
1360 (long)kid->op_type,kid->op_targ);
1361 if (kid->op_type != OP_GV) {
1362 /* Restore RV2CV to check lvalueness */
1364 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1365 okid->op_next = kid->op_next;
1366 kid->op_next = okid;
1369 okid->op_next = Nullop;
1370 okid->op_type = OP_RV2CV;
1372 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1373 okid->op_private |= OPpLVAL_INTRO;
1387 /* grep, foreach, subcalls, refgen */
1388 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1390 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1391 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1393 : (o->op_type == OP_ENTERSUB
1394 ? "non-lvalue subroutine call"
1395 : PL_op_desc[o->op_type])),
1396 type ? PL_op_desc[type] : "local"));
1410 case OP_RIGHT_SHIFT:
1419 if (!(o->op_flags & OPf_STACKED))
1425 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1431 if (!type && cUNOPo->op_first->op_type != OP_GV)
1432 Perl_croak(aTHX_ "Can't localize through a reference");
1433 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1434 PL_modcount = 10000;
1435 return o; /* Treat \(@foo) like ordinary list. */
1439 if (scalar_mod_type(o, type))
1441 ref(cUNOPo->op_first, o->op_type);
1450 PL_modcount = 10000;
1453 if (!type && cUNOPo->op_first->op_type != OP_GV)
1454 Perl_croak(aTHX_ "Can't localize through a reference");
1455 ref(cUNOPo->op_first, o->op_type);
1459 PL_hints |= HINT_BLOCK_SCOPE;
1469 PL_modcount = 10000;
1470 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1471 return o; /* Treat \(@foo) like ordinary list. */
1472 if (scalar_mod_type(o, type))
1478 Perl_croak(aTHX_ "Can't localize lexical variable %s",
1479 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
1484 PL_modcount++; /* XXX ??? */
1486 #endif /* USE_THREADS */
1492 if (type != OP_SASSIGN)
1496 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1502 pad_free(o->op_targ);
1503 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1504 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1505 if (o->op_flags & OPf_KIDS)
1506 mod(cBINOPo->op_first->op_sibling, type);
1511 ref(cBINOPo->op_first, o->op_type);
1512 if (type == OP_ENTERSUB &&
1513 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1514 o->op_private |= OPpLVAL_DEFER;
1521 if (o->op_flags & OPf_KIDS)
1522 mod(cLISTOPo->op_last, type);
1526 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1528 else if (!(o->op_flags & OPf_KIDS))
1530 if (o->op_targ != OP_LIST) {
1531 mod(cBINOPo->op_first, type);
1536 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1540 o->op_flags |= OPf_MOD;
1542 if (type == OP_AASSIGN || type == OP_SASSIGN)
1543 o->op_flags |= OPf_SPECIAL|OPf_REF;
1545 o->op_private |= OPpLVAL_INTRO;
1546 o->op_flags &= ~OPf_SPECIAL;
1547 PL_hints |= HINT_BLOCK_SCOPE;
1549 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1550 o->op_flags |= OPf_REF;
1555 S_scalar_mod_type(pTHX_ OP *o, I32 type)
1559 if (o->op_type == OP_RV2GV)
1583 case OP_RIGHT_SHIFT:
1602 S_is_handle_constructor(pTHX_ OP *o, I32 argnum)
1604 switch (o->op_type) {
1612 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1625 Perl_refkids(pTHX_ OP *o, I32 type)
1628 if (o && o->op_flags & OPf_KIDS) {
1629 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1636 Perl_ref(pTHX_ OP *o, I32 type)
1640 if (!o || PL_error_count)
1643 switch (o->op_type) {
1645 if ((type == OP_DEFINED || type == OP_LOCK) &&
1646 !(o->op_flags & OPf_STACKED)) {
1647 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1648 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1649 assert(cUNOPo->op_first->op_type == OP_NULL);
1650 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1651 o->op_flags |= OPf_SPECIAL;
1656 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1660 if (type == OP_DEFINED)
1661 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1662 ref(cUNOPo->op_first, o->op_type);
1665 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1666 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1667 : type == OP_RV2HV ? OPpDEREF_HV
1669 o->op_flags |= OPf_MOD;
1674 o->op_flags |= OPf_MOD; /* XXX ??? */
1679 o->op_flags |= OPf_REF;
1682 if (type == OP_DEFINED)
1683 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1684 ref(cUNOPo->op_first, o->op_type);
1689 o->op_flags |= OPf_REF;
1694 if (!(o->op_flags & OPf_KIDS))
1696 ref(cBINOPo->op_first, type);
1700 ref(cBINOPo->op_first, o->op_type);
1701 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1702 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1703 : type == OP_RV2HV ? OPpDEREF_HV
1705 o->op_flags |= OPf_MOD;
1713 if (!(o->op_flags & OPf_KIDS))
1715 ref(cLISTOPo->op_last, type);
1725 S_dup_attrlist(pTHX_ OP *o)
1729 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1730 * where the first kid is OP_PUSHMARK and the remaining ones
1731 * are OP_CONST. We need to push the OP_CONST values.
1733 if (o->op_type == OP_CONST)
1734 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc(cSVOPo->op_sv));
1736 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1737 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1738 if (o->op_type == OP_CONST)
1739 rop = append_elem(OP_LIST, rop,
1740 newSVOP(OP_CONST, o->op_flags,
1741 SvREFCNT_inc(cSVOPo->op_sv)));
1748 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
1750 OP *modname; /* for 'use' */
1753 /* fake up C<use attributes $pkg,$rv,@attrs> */
1754 ENTER; /* need to protect against side-effects of 'use' */
1756 if (stash && HvNAME(stash))
1757 stashsv = newSVpv(HvNAME(stash), 0);
1759 stashsv = &PL_sv_no;
1760 #define ATTRSMODULE "attributes"
1761 modname = newSVOP(OP_CONST, 0,
1762 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1));
1763 modname->op_private |= OPpCONST_BARE;
1764 /* that flag is required to make 'use' work right */
1765 utilize(1, start_subparse(FALSE, 0),
1766 Nullop, /* version */
1768 prepend_elem(OP_LIST,
1769 newSVOP(OP_CONST, 0, stashsv),
1770 prepend_elem(OP_LIST,
1771 newSVOP(OP_CONST, 0, newRV(target)),
1772 dup_attrlist(attrs))));
1777 S_my_kid(pTHX_ OP *o, OP *attrs)
1782 if (!o || PL_error_count)
1786 if (type == OP_LIST) {
1787 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1789 } else if (type == OP_UNDEF) {
1791 } else if (type == OP_RV2SV || /* "our" declaration */
1793 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
1795 } else if (type != OP_PADSV &&
1798 type != OP_PUSHMARK)
1800 yyerror(Perl_form(aTHX_ "Can't declare %s in my", PL_op_desc[o->op_type]));
1803 else if (attrs && type != OP_PUSHMARK) {
1808 /* check for C<my Dog $spot> when deciding package */
1809 namesvp = av_fetch(PL_comppad_name, o->op_targ, FALSE);
1810 if (namesvp && *namesvp && SvOBJECT(*namesvp) && HvNAME(SvSTASH(*namesvp)))
1811 stash = SvSTASH(*namesvp);
1813 stash = PL_curstash;
1814 padsv = PAD_SV(o->op_targ);
1815 apply_attrs(stash, padsv, attrs);
1817 o->op_flags |= OPf_MOD;
1818 o->op_private |= OPpLVAL_INTRO;
1823 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1825 if (o->op_flags & OPf_PARENS)
1828 PL_in_my_stash = Nullhv;
1831 return my_kid(o, attrs);
1835 Perl_my(pTHX_ OP *o)
1837 return my_kid(o, Nullop);
1841 Perl_sawparens(pTHX_ OP *o)
1844 o->op_flags |= OPf_PARENS;
1849 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
1854 if (ckWARN(WARN_UNSAFE) &&
1855 (left->op_type == OP_RV2AV ||
1856 left->op_type == OP_RV2HV ||
1857 left->op_type == OP_PADAV ||
1858 left->op_type == OP_PADHV)) {
1859 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
1860 right->op_type == OP_TRANS)
1861 ? right->op_type : OP_MATCH];
1862 const char *sample = ((left->op_type == OP_RV2AV ||
1863 left->op_type == OP_PADAV)
1864 ? "@array" : "%hash");
1865 Perl_warner(aTHX_ WARN_UNSAFE,
1866 "Applying %s to %s will act on scalar(%s)",
1867 desc, sample, sample);
1870 if (right->op_type == OP_MATCH ||
1871 right->op_type == OP_SUBST ||
1872 right->op_type == OP_TRANS) {
1873 right->op_flags |= OPf_STACKED;
1874 if (right->op_type != OP_MATCH)
1875 left = mod(left, right->op_type);
1876 if (right->op_type == OP_TRANS)
1877 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1879 o = prepend_elem(right->op_type, scalar(left), right);
1881 return newUNOP(OP_NOT, 0, scalar(o));
1885 return bind_match(type, left,
1886 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1890 Perl_invert(pTHX_ OP *o)
1894 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1895 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1899 Perl_scope(pTHX_ OP *o)
1902 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
1903 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1904 o->op_type = OP_LEAVE;
1905 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
1908 if (o->op_type == OP_LINESEQ) {
1910 o->op_type = OP_SCOPE;
1911 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
1912 kid = ((LISTOP*)o)->op_first;
1913 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
1917 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1924 Perl_save_hints(pTHX)
1927 SAVESPTR(GvHV(PL_hintgv));
1928 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
1929 SAVEFREESV(GvHV(PL_hintgv));
1933 Perl_block_start(pTHX_ int full)
1936 int retval = PL_savestack_ix;
1938 SAVEI32(PL_comppad_name_floor);
1940 if ((PL_comppad_name_fill = AvFILLp(PL_comppad_name)) > 0)
1941 PL_comppad_name_floor = PL_comppad_name_fill;
1943 PL_comppad_name_floor = 0;
1945 SAVEI32(PL_min_intro_pending);
1946 SAVEI32(PL_max_intro_pending);
1947 PL_min_intro_pending = 0;
1948 SAVEI32(PL_comppad_name_fill);
1949 SAVEI32(PL_padix_floor);
1950 PL_padix_floor = PL_padix;
1951 PL_pad_reset_pending = FALSE;
1953 PL_hints &= ~HINT_BLOCK_SCOPE;
1954 SAVEPPTR(PL_compiling.cop_warnings);
1955 if (! specialWARN(PL_compiling.cop_warnings)) {
1956 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1957 SAVEFREESV(PL_compiling.cop_warnings) ;
1965 Perl_block_end(pTHX_ I32 floor, OP *seq)
1968 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
1969 OP* retval = scalarseq(seq);
1971 PL_pad_reset_pending = FALSE;
1972 PL_compiling.op_private = PL_hints;
1974 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1975 pad_leavemy(PL_comppad_name_fill);
1984 OP *o = newOP(OP_THREADSV, 0);
1985 o->op_targ = find_threadsv("_");
1988 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
1989 #endif /* USE_THREADS */
1993 Perl_newPROG(pTHX_ OP *o)
1999 PL_eval_root = newUNOP(OP_LEAVEEVAL,
2000 ((PL_in_eval & EVAL_KEEPERR)
2001 ? OPf_SPECIAL : 0), o);
2002 PL_eval_start = linklist(PL_eval_root);
2003 PL_eval_root->op_private |= OPpREFCOUNTED;
2004 OpREFCNT_set(PL_eval_root, 1);
2005 PL_eval_root->op_next = 0;
2006 peep(PL_eval_start);
2011 PL_main_root = scope(sawparens(scalarvoid(o)));
2012 PL_curcop = &PL_compiling;
2013 PL_main_start = LINKLIST(PL_main_root);
2014 PL_main_root->op_private |= OPpREFCOUNTED;
2015 OpREFCNT_set(PL_main_root, 1);
2016 PL_main_root->op_next = 0;
2017 peep(PL_main_start);
2020 /* Register with debugger */
2022 CV *cv = get_cv("DB::postponed", FALSE);
2026 XPUSHs((SV*)CopFILEGV(&PL_compiling));
2028 call_sv((SV*)cv, G_DISCARD);
2035 Perl_localize(pTHX_ OP *o, I32 lex)
2037 if (o->op_flags & OPf_PARENS)
2041 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
2043 for (s = PL_bufptr; *s && (isALNUM(*s) || (*s & 0x80) || strchr("@$%, ",*s)); s++) ;
2044 if (*s == ';' || *s == '=')
2045 Perl_warner(aTHX_ WARN_PARENTHESIS, "Parentheses missing around \"%s\" list",
2046 lex ? "my" : "local");
2050 PL_in_my_stash = Nullhv;
2054 return mod(o, OP_NULL); /* a bit kludgey */
2058 Perl_jmaybe(pTHX_ OP *o)
2060 if (o->op_type == OP_LIST) {
2063 o2 = newOP(OP_THREADSV, 0);
2064 o2->op_targ = find_threadsv(";");
2066 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
2067 #endif /* USE_THREADS */
2068 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2074 Perl_fold_constants(pTHX_ register OP *o)
2078 I32 type = o->op_type;
2081 if (PL_opargs[type] & OA_RETSCALAR)
2083 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2084 o->op_targ = pad_alloc(type, SVs_PADTMP);
2086 /* integerize op, unless it happens to be C<-foo>.
2087 * XXX should pp_i_negate() do magic string negation instead? */
2088 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2089 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2090 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2092 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2095 if (!(PL_opargs[type] & OA_FOLDCONST))
2100 /* XXX might want a ck_negate() for this */
2101 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2114 if (o->op_private & OPpLOCALE)
2119 goto nope; /* Don't try to run w/ errors */
2121 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2122 if ((curop->op_type != OP_CONST ||
2123 (curop->op_private & OPpCONST_BARE)) &&
2124 curop->op_type != OP_LIST &&
2125 curop->op_type != OP_SCALAR &&
2126 curop->op_type != OP_NULL &&
2127 curop->op_type != OP_PUSHMARK)
2133 curop = LINKLIST(o);
2137 sv = *(PL_stack_sp--);
2138 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
2139 pad_swipe(o->op_targ);
2140 else if (SvTEMP(sv)) { /* grab mortal temp? */
2141 (void)SvREFCNT_inc(sv);
2145 if (type == OP_RV2GV)
2146 return newGVOP(OP_GV, 0, (GV*)sv);
2148 /* try to smush double to int, but don't smush -2.0 to -2 */
2149 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
2153 if ((NV)iv == SvNV(sv)) {
2158 SvIOK_off(sv); /* undo SvIV() damage */
2160 return newSVOP(OP_CONST, 0, sv);
2164 if (!(PL_opargs[type] & OA_OTHERINT))
2167 if (!(PL_hints & HINT_INTEGER)) {
2168 if (type == OP_MODULO
2169 || type == OP_DIVIDE
2170 || !(o->op_flags & OPf_KIDS))
2175 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
2176 if (curop->op_type == OP_CONST) {
2177 if (SvIOK(((SVOP*)curop)->op_sv))
2181 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
2185 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
2192 Perl_gen_constant_list(pTHX_ register OP *o)
2196 I32 oldtmps_floor = PL_tmps_floor;
2200 return o; /* Don't attempt to run with errors */
2202 PL_op = curop = LINKLIST(o);
2208 PL_tmps_floor = oldtmps_floor;
2210 o->op_type = OP_RV2AV;
2211 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2212 curop = ((UNOP*)o)->op_first;
2213 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
2220 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2225 if (!o || o->op_type != OP_LIST)
2226 o = newLISTOP(OP_LIST, 0, o, Nullop);
2228 o->op_flags &= ~OPf_WANT;
2230 if (!(PL_opargs[type] & OA_MARK))
2231 null(cLISTOPo->op_first);
2234 o->op_ppaddr = PL_ppaddr[type];
2235 o->op_flags |= flags;
2237 o = CHECKOP(type, o);
2238 if (o->op_type != type)
2241 if (cLISTOPo->op_children < 7) {
2242 /* XXX do we really need to do this if we're done appending?? */
2243 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2245 cLISTOPo->op_last = last; /* in case check substituted last arg */
2248 return fold_constants(o);
2251 /* List constructors */
2254 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2262 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
2263 return newLISTOP(type, 0, first, last);
2265 if (first->op_flags & OPf_KIDS)
2266 ((LISTOP*)first)->op_last->op_sibling = last;
2268 first->op_flags |= OPf_KIDS;
2269 ((LISTOP*)first)->op_first = last;
2271 ((LISTOP*)first)->op_last = last;
2272 ((LISTOP*)first)->op_children++;
2277 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2285 if (first->op_type != type)
2286 return prepend_elem(type, (OP*)first, (OP*)last);
2288 if (last->op_type != type)
2289 return append_elem(type, (OP*)first, (OP*)last);
2291 first->op_last->op_sibling = last->op_first;
2292 first->op_last = last->op_last;
2293 first->op_children += last->op_children;
2294 if (first->op_children)
2295 first->op_flags |= OPf_KIDS;
2297 #ifdef PL_OP_SLAB_ALLOC
2305 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2313 if (last->op_type == type) {
2314 if (type == OP_LIST) { /* already a PUSHMARK there */
2315 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2316 ((LISTOP*)last)->op_first->op_sibling = first;
2319 if (!(last->op_flags & OPf_KIDS)) {
2320 ((LISTOP*)last)->op_last = first;
2321 last->op_flags |= OPf_KIDS;
2323 first->op_sibling = ((LISTOP*)last)->op_first;
2324 ((LISTOP*)last)->op_first = first;
2326 ((LISTOP*)last)->op_children++;
2330 return newLISTOP(type, 0, first, last);
2336 Perl_newNULLLIST(pTHX)
2338 return newOP(OP_STUB, 0);
2342 Perl_force_list(pTHX_ OP *o)
2344 if (!o || o->op_type != OP_LIST)
2345 o = newLISTOP(OP_LIST, 0, o, Nullop);
2351 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2355 NewOp(1101, listop, 1, LISTOP);
2357 listop->op_type = type;
2358 listop->op_ppaddr = PL_ppaddr[type];
2359 listop->op_children = (first != 0) + (last != 0);
2360 listop->op_flags = flags;
2364 else if (!first && last)
2367 first->op_sibling = last;
2368 listop->op_first = first;
2369 listop->op_last = last;
2370 if (type == OP_LIST) {
2372 pushop = newOP(OP_PUSHMARK, 0);
2373 pushop->op_sibling = first;
2374 listop->op_first = pushop;
2375 listop->op_flags |= OPf_KIDS;
2377 listop->op_last = pushop;
2379 else if (listop->op_children)
2380 listop->op_flags |= OPf_KIDS;
2386 Perl_newOP(pTHX_ I32 type, I32 flags)
2389 NewOp(1101, o, 1, OP);
2391 o->op_ppaddr = PL_ppaddr[type];
2392 o->op_flags = flags;
2395 o->op_private = 0 + (flags >> 8);
2396 if (PL_opargs[type] & OA_RETSCALAR)
2398 if (PL_opargs[type] & OA_TARGET)
2399 o->op_targ = pad_alloc(type, SVs_PADTMP);
2400 return CHECKOP(type, o);
2404 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
2409 first = newOP(OP_STUB, 0);
2410 if (PL_opargs[type] & OA_MARK)
2411 first = force_list(first);
2413 NewOp(1101, unop, 1, UNOP);
2414 unop->op_type = type;
2415 unop->op_ppaddr = PL_ppaddr[type];
2416 unop->op_first = first;
2417 unop->op_flags = flags | OPf_KIDS;
2418 unop->op_private = 1 | (flags >> 8);
2419 unop = (UNOP*) CHECKOP(type, unop);
2423 return fold_constants((OP *) unop);
2427 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2430 NewOp(1101, binop, 1, BINOP);
2433 first = newOP(OP_NULL, 0);
2435 binop->op_type = type;
2436 binop->op_ppaddr = PL_ppaddr[type];
2437 binop->op_first = first;
2438 binop->op_flags = flags | OPf_KIDS;
2441 binop->op_private = 1 | (flags >> 8);
2444 binop->op_private = 2 | (flags >> 8);
2445 first->op_sibling = last;
2448 binop = (BINOP*)CHECKOP(type, binop);
2449 if (binop->op_next || binop->op_type != type)
2452 binop->op_last = binop->op_first->op_sibling;
2454 return fold_constants((OP *)binop);
2458 utf8compare(const void *a, const void *b)
2461 for (i = 0; i < 10; i++) {
2462 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2464 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2471 Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
2473 SV *tstr = ((SVOP*)expr)->op_sv;
2474 SV *rstr = ((SVOP*)repl)->op_sv;
2477 register U8 *t = (U8*)SvPV(tstr, tlen);
2478 register U8 *r = (U8*)SvPV(rstr, rlen);
2484 register short *tbl;
2486 complement = o->op_private & OPpTRANS_COMPLEMENT;
2487 del = o->op_private & OPpTRANS_DELETE;
2488 squash = o->op_private & OPpTRANS_SQUASH;
2490 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2491 SV* listsv = newSVpvn("# comment\n",10);
2493 U8* tend = t + tlen;
2494 U8* rend = r + rlen;
2510 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2511 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2517 New(1109, cp, tlen, U8*);
2519 transv = newSVpvn("",0);
2528 qsort(cp, i, sizeof(U8*), utf8compare);
2529 for (j = 0; j < i; j++) {
2531 UV val = utf8_to_uv(s, &ulen);
2533 diff = val - nextmin;
2535 t = uv_to_utf8(tmpbuf,nextmin);
2536 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2538 t = uv_to_utf8(tmpbuf, val - 1);
2539 sv_catpvn(transv, "\377", 1);
2540 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2544 val = utf8_to_uv(s+1, &ulen);
2548 t = uv_to_utf8(tmpbuf,nextmin);
2549 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2550 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2551 sv_catpvn(transv, "\377", 1);
2552 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2553 t = (U8*)SvPVX(transv);
2554 tlen = SvCUR(transv);
2557 else if (!rlen && !del) {
2558 r = t; rlen = tlen; rend = tend;
2561 if (to_utf && from_utf) { /* only counting characters */
2562 if (t == r || (tlen == rlen && memEQ(t, r, tlen)))
2563 o->op_private |= OPpTRANS_IDENTICAL;
2565 else { /* straight latin-1 translation */
2566 if (tlen == 4 && memEQ(t, "\0\377\303\277", 4) &&
2567 rlen == 4 && memEQ(r, "\0\377\303\277", 4))
2568 o->op_private |= OPpTRANS_IDENTICAL;
2572 while (t < tend || tfirst <= tlast) {
2573 /* see if we need more "t" chars */
2574 if (tfirst > tlast) {
2575 tfirst = (I32)utf8_to_uv(t, &ulen);
2577 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
2578 tlast = (I32)utf8_to_uv(++t, &ulen);
2585 /* now see if we need more "r" chars */
2586 if (rfirst > rlast) {
2588 rfirst = (I32)utf8_to_uv(r, &ulen);
2590 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
2591 rlast = (I32)utf8_to_uv(++r, &ulen);
2600 rfirst = rlast = 0xffffffff;
2604 /* now see which range will peter our first, if either. */
2605 tdiff = tlast - tfirst;
2606 rdiff = rlast - rfirst;
2613 if (rfirst == 0xffffffff) {
2614 diff = tdiff; /* oops, pretend rdiff is infinite */
2616 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\tXXXX\n", tfirst, tlast);
2618 Perl_sv_catpvf(aTHX_ listsv, "%04x\t\tXXXX\n", tfirst);
2622 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\t%04x\n", tfirst, tfirst + diff, rfirst);
2624 Perl_sv_catpvf(aTHX_ listsv, "%04x\t\t%04x\n", tfirst, rfirst);
2626 if (rfirst + diff > max)
2627 max = rfirst + diff;
2632 else if (rfirst <= 0x800)
2633 grows |= (tfirst < 0x80);
2634 else if (rfirst <= 0x10000)
2635 grows |= (tfirst < 0x800);
2636 else if (rfirst <= 0x200000)
2637 grows |= (tfirst < 0x10000);
2638 else if (rfirst <= 0x4000000)
2639 grows |= (tfirst < 0x200000);
2640 else if (rfirst <= 0x80000000)
2641 grows |= (tfirst < 0x4000000);
2653 else if (max > 0xff)
2658 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2659 SvREFCNT_dec(listsv);
2661 SvREFCNT_dec(transv);
2663 if (!del && havefinal)
2664 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5, newSViv((IV)final), 0);
2666 if (grows && to_utf)
2667 o->op_private |= OPpTRANS_GROWS;
2674 tbl = (short*)cPVOPo->op_pv;
2676 Zero(tbl, 256, short);
2677 for (i = 0; i < tlen; i++)
2679 for (i = 0, j = 0; i < 256; i++) {
2695 if (!rlen && !del) {
2698 o->op_private |= OPpTRANS_IDENTICAL;
2700 for (i = 0; i < 256; i++)
2702 for (i = 0, j = 0; i < tlen; i++,j++) {
2705 if (tbl[t[i]] == -1)
2711 if (tbl[t[i]] == -1)
2722 Perl_newPMOP(pTHX_ I32 type, I32 flags)
2727 NewOp(1101, pmop, 1, PMOP);
2728 pmop->op_type = type;
2729 pmop->op_ppaddr = PL_ppaddr[type];
2730 pmop->op_flags = flags;
2731 pmop->op_private = 0 | (flags >> 8);
2733 if (PL_hints & HINT_RE_TAINT)
2734 pmop->op_pmpermflags |= PMf_RETAINT;
2735 if (PL_hints & HINT_LOCALE)
2736 pmop->op_pmpermflags |= PMf_LOCALE;
2737 pmop->op_pmflags = pmop->op_pmpermflags;
2739 /* link into pm list */
2740 if (type != OP_TRANS && PL_curstash) {
2741 pmop->op_pmnext = HvPMROOT(PL_curstash);
2742 HvPMROOT(PL_curstash) = pmop;
2749 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
2754 I32 repl_has_vars = 0;
2756 if (o->op_type == OP_TRANS)
2757 return pmtrans(o, expr, repl);
2759 PL_hints |= HINT_BLOCK_SCOPE;
2762 if (expr->op_type == OP_CONST) {
2764 SV *pat = ((SVOP*)expr)->op_sv;
2765 char *p = SvPV(pat, plen);
2766 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2767 sv_setpvn(pat, "\\s+", 3);
2768 p = SvPV(pat, plen);
2769 pm->op_pmflags |= PMf_SKIPWHITE;
2771 pm->op_pmregexp = CALLREGCOMP(aTHX_ p, p + plen, pm);
2772 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2773 pm->op_pmflags |= PMf_WHITE;
2777 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2778 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2780 : OP_REGCMAYBE),0,expr);
2782 NewOp(1101, rcop, 1, LOGOP);
2783 rcop->op_type = OP_REGCOMP;
2784 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2785 rcop->op_first = scalar(expr);
2786 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2787 ? (OPf_SPECIAL | OPf_KIDS)
2789 rcop->op_private = 1;
2792 /* establish postfix order */
2793 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2795 rcop->op_next = expr;
2796 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2799 rcop->op_next = LINKLIST(expr);
2800 expr->op_next = (OP*)rcop;
2803 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2808 if (pm->op_pmflags & PMf_EVAL) {
2810 if (PL_curcop->cop_line < PL_multi_end)
2811 PL_curcop->cop_line = PL_multi_end;
2814 else if (repl->op_type == OP_THREADSV
2815 && strchr("&`'123456789+",
2816 PL_threadsv_names[repl->op_targ]))
2820 #endif /* USE_THREADS */
2821 else if (repl->op_type == OP_CONST)
2825 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2826 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2828 if (curop->op_type == OP_THREADSV) {
2830 if (strchr("&`'123456789+", curop->op_private))
2834 if (curop->op_type == OP_GV) {
2835 GV *gv = cGVOPx(curop);
2837 if (strchr("&`'123456789+", *GvENAME(gv)))
2840 #endif /* USE_THREADS */
2841 else if (curop->op_type == OP_RV2CV)
2843 else if (curop->op_type == OP_RV2SV ||
2844 curop->op_type == OP_RV2AV ||
2845 curop->op_type == OP_RV2HV ||
2846 curop->op_type == OP_RV2GV) {
2847 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2850 else if (curop->op_type == OP_PADSV ||
2851 curop->op_type == OP_PADAV ||
2852 curop->op_type == OP_PADHV ||
2853 curop->op_type == OP_PADANY) {
2856 else if (curop->op_type == OP_PUSHRE)
2857 ; /* Okay here, dangerous in newASSIGNOP */
2866 && (!pm->op_pmregexp
2867 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
2868 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2869 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2870 prepend_elem(o->op_type, scalar(repl), o);
2873 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
2874 pm->op_pmflags |= PMf_MAYBE_CONST;
2875 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2877 NewOp(1101, rcop, 1, LOGOP);
2878 rcop->op_type = OP_SUBSTCONT;
2879 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
2880 rcop->op_first = scalar(repl);
2881 rcop->op_flags |= OPf_KIDS;
2882 rcop->op_private = 1;
2885 /* establish postfix order */
2886 rcop->op_next = LINKLIST(repl);
2887 repl->op_next = (OP*)rcop;
2889 pm->op_pmreplroot = scalar((OP*)rcop);
2890 pm->op_pmreplstart = LINKLIST(rcop);
2899 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
2902 NewOp(1101, svop, 1, SVOP);
2903 svop->op_type = type;
2904 svop->op_ppaddr = PL_ppaddr[type];
2906 svop->op_next = (OP*)svop;
2907 svop->op_flags = flags;
2908 if (PL_opargs[type] & OA_RETSCALAR)
2910 if (PL_opargs[type] & OA_TARGET)
2911 svop->op_targ = pad_alloc(type, SVs_PADTMP);
2912 return CHECKOP(type, svop);
2916 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
2919 NewOp(1101, padop, 1, PADOP);
2920 padop->op_type = type;
2921 padop->op_ppaddr = PL_ppaddr[type];
2922 padop->op_padix = pad_alloc(type, SVs_PADTMP);
2923 PL_curpad[padop->op_padix] = sv;
2924 padop->op_next = (OP*)padop;
2925 padop->op_flags = flags;
2926 if (PL_opargs[type] & OA_RETSCALAR)
2928 if (PL_opargs[type] & OA_TARGET)
2929 padop->op_targ = pad_alloc(type, SVs_PADTMP);
2930 return CHECKOP(type, padop);
2934 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
2938 return newPADOP(type, flags, SvREFCNT_inc(gv));
2940 return newSVOP(type, flags, SvREFCNT_inc(gv));
2945 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
2948 NewOp(1101, pvop, 1, PVOP);
2949 pvop->op_type = type;
2950 pvop->op_ppaddr = PL_ppaddr[type];
2952 pvop->op_next = (OP*)pvop;
2953 pvop->op_flags = flags;
2954 if (PL_opargs[type] & OA_RETSCALAR)
2956 if (PL_opargs[type] & OA_TARGET)
2957 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2958 return CHECKOP(type, pvop);
2962 Perl_package(pTHX_ OP *o)
2967 save_hptr(&PL_curstash);
2968 save_item(PL_curstname);
2973 name = SvPV(sv, len);
2974 PL_curstash = gv_stashpvn(name,len,TRUE);
2975 sv_setpvn(PL_curstname, name, len);
2979 sv_setpv(PL_curstname,"<none>");
2980 PL_curstash = Nullhv;
2982 PL_hints |= HINT_BLOCK_SCOPE;
2983 PL_copline = NOLINE;
2988 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg)
2997 if (id->op_type != OP_CONST)
2998 Perl_croak(aTHX_ "Module name must be constant");
3002 if(version != Nullop) {
3003 SV *vesv = ((SVOP*)version)->op_sv;
3005 if (arg == Nullop && !SvNIOK(vesv)) {
3011 if (version->op_type != OP_CONST || !SvNIOK(vesv))
3012 Perl_croak(aTHX_ "Version number must be constant number");
3014 /* Make copy of id so we don't free it twice */
3015 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3017 /* Fake up a method call to VERSION */
3018 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3019 append_elem(OP_LIST,
3020 prepend_elem(OP_LIST, pack, list(version)),
3021 newSVOP(OP_METHOD_NAMED, 0,
3022 newSVpvn("VERSION", 7))));
3026 /* Fake up an import/unimport */
3027 if (arg && arg->op_type == OP_STUB)
3028 imop = arg; /* no import on explicit () */
3029 else if(SvNIOK(((SVOP*)id)->op_sv)) {
3030 imop = Nullop; /* use 5.0; */
3033 /* Make copy of id so we don't free it twice */
3034 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3035 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3036 append_elem(OP_LIST,
3037 prepend_elem(OP_LIST, pack, list(arg)),
3038 newSVOP(OP_METHOD_NAMED, 0,
3039 aver ? newSVpvn("import", 6)
3040 : newSVpvn("unimport", 8))));
3043 /* Fake up a require, handle override, if any */
3044 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
3045 if (!(gv && GvIMPORTED_CV(gv)))
3046 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
3048 if (gv && GvIMPORTED_CV(gv)) {
3049 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3050 append_elem(OP_LIST, id,
3051 scalar(newUNOP(OP_RV2CV, 0,
3056 rqop = newUNOP(OP_REQUIRE, 0, id);
3059 /* Fake up the BEGIN {}, which does its thing immediately. */
3061 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
3064 append_elem(OP_LINESEQ,
3065 append_elem(OP_LINESEQ,
3066 newSTATEOP(0, Nullch, rqop),
3067 newSTATEOP(0, Nullch, veop)),
3068 newSTATEOP(0, Nullch, imop) ));
3070 PL_hints |= HINT_BLOCK_SCOPE;
3071 PL_copline = NOLINE;
3076 Perl_dofile(pTHX_ OP *term)
3081 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
3082 if (!(gv && GvIMPORTED_CV(gv)))
3083 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
3085 if (gv && GvIMPORTED_CV(gv)) {
3086 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3087 append_elem(OP_LIST, term,
3088 scalar(newUNOP(OP_RV2CV, 0,
3093 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3099 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
3101 return newBINOP(OP_LSLICE, flags,
3102 list(force_list(subscript)),
3103 list(force_list(listval)) );
3107 S_list_assignment(pTHX_ register OP *o)
3112 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3113 o = cUNOPo->op_first;
3115 if (o->op_type == OP_COND_EXPR) {
3116 I32 t = list_assignment(cLOGOPo->op_first->op_sibling);
3117 I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
3122 yyerror("Assignment to both a list and a scalar");
3126 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3127 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3128 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
3131 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
3134 if (o->op_type == OP_RV2SV)
3141 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
3146 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
3147 return newLOGOP(optype, 0,
3148 mod(scalar(left), optype),
3149 newUNOP(OP_SASSIGN, 0, scalar(right)));
3152 return newBINOP(optype, OPf_STACKED,
3153 mod(scalar(left), optype), scalar(right));
3157 if (list_assignment(left)) {
3160 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3161 left = mod(left, OP_AASSIGN);
3169 o = newBINOP(OP_AASSIGN, flags,
3170 list(force_list(right)),
3171 list(force_list(left)) );
3172 o->op_private = 0 | (flags >> 8);
3173 if (!(left->op_private & OPpLVAL_INTRO)) {
3177 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3178 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3179 if (curop->op_type == OP_GV) {
3180 GV *gv = cGVOPx(curop);
3181 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3183 SvCUR(gv) = PL_generation;
3185 else if (curop->op_type == OP_PADSV ||
3186 curop->op_type == OP_PADAV ||
3187 curop->op_type == OP_PADHV ||
3188 curop->op_type == OP_PADANY) {
3189 SV **svp = AvARRAY(PL_comppad_name);
3190 SV *sv = svp[curop->op_targ];
3191 if (SvCUR(sv) == PL_generation)
3193 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
3195 else if (curop->op_type == OP_RV2CV)
3197 else if (curop->op_type == OP_RV2SV ||
3198 curop->op_type == OP_RV2AV ||
3199 curop->op_type == OP_RV2HV ||
3200 curop->op_type == OP_RV2GV) {
3201 if (lastop->op_type != OP_GV) /* funny deref? */
3204 else if (curop->op_type == OP_PUSHRE) {
3205 if (((PMOP*)curop)->op_pmreplroot) {
3206 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3207 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3209 SvCUR(gv) = PL_generation;
3218 o->op_private = OPpASSIGN_COMMON;
3220 if (right && right->op_type == OP_SPLIT) {
3222 if ((tmpop = ((LISTOP*)right)->op_first) &&
3223 tmpop->op_type == OP_PUSHRE)
3225 PMOP *pm = (PMOP*)tmpop;
3226 if (left->op_type == OP_RV2AV &&
3227 !(left->op_private & OPpLVAL_INTRO) &&
3228 !(o->op_private & OPpASSIGN_COMMON) )
3230 tmpop = ((UNOP*)left)->op_first;
3231 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
3232 pm->op_pmreplroot = (OP*)((SVOP*)tmpop)->op_sv; /* XXXXXX */
3233 pm->op_pmflags |= PMf_ONCE;
3234 tmpop = cUNOPo->op_first; /* to list (nulled) */
3235 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3236 tmpop->op_sibling = Nullop; /* don't free split */
3237 right->op_next = tmpop->op_next; /* fix starting loc */
3238 op_free(o); /* blow off assign */
3239 right->op_flags &= ~OPf_WANT;
3240 /* "I don't know and I don't care." */
3245 if (PL_modcount < 10000 &&
3246 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3248 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3250 sv_setiv(sv, PL_modcount+1);
3258 right = newOP(OP_UNDEF, 0);
3259 if (right->op_type == OP_READLINE) {
3260 right->op_flags |= OPf_STACKED;
3261 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3264 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3265 o = newBINOP(OP_SASSIGN, flags,
3266 scalar(right), mod(scalar(left), OP_SASSIGN) );
3278 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3281 U32 seq = intro_my();
3284 NewOp(1101, cop, 1, COP);
3285 if (PERLDB_LINE && PL_curcop->cop_line && PL_curstash != PL_debstash) {
3286 cop->op_type = OP_DBSTATE;
3287 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3290 cop->op_type = OP_NEXTSTATE;
3291 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3293 cop->op_flags = flags;
3294 cop->op_private = (PL_hints & HINT_UTF8);
3296 cop->op_private |= NATIVE_HINTS;
3298 PL_compiling.op_private = cop->op_private;
3299 cop->op_next = (OP*)cop;
3302 cop->cop_label = label;
3303 PL_hints |= HINT_BLOCK_SCOPE;
3306 cop->cop_arybase = PL_curcop->cop_arybase;
3307 if (specialWARN(PL_curcop->cop_warnings))
3308 cop->cop_warnings = PL_curcop->cop_warnings ;
3310 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3313 if (PL_copline == NOLINE)
3314 cop->cop_line = PL_curcop->cop_line;
3316 cop->cop_line = PL_copline;
3317 PL_copline = NOLINE;
3319 CopFILEGV_set(cop, (GV*)SvREFCNT_inc(CopFILEGV(PL_curcop)));
3320 cop->cop_stash = PL_curstash;
3322 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3323 SV **svp = av_fetch(CopFILEAV(PL_curcop), (I32)CopLINE(cop), FALSE);
3324 if (svp && *svp != &PL_sv_undef && !SvIOK(*svp)) {
3325 (void)SvIOK_on(*svp);
3327 SvSTASH(*svp) = (HV*)cop;
3331 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3334 /* "Introduce" my variables to visible status. */
3342 if (! PL_min_intro_pending)
3343 return PL_cop_seqmax;
3345 svp = AvARRAY(PL_comppad_name);
3346 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
3347 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
3348 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
3349 SvNVX(sv) = (NV)PL_cop_seqmax;
3352 PL_min_intro_pending = 0;
3353 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
3354 return PL_cop_seqmax++;
3358 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
3360 return new_logop(type, flags, &first, &other);
3364 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
3369 OP *first = *firstp;
3370 OP *other = *otherp;
3372 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3373 return newBINOP(type, flags, scalar(first), scalar(other));
3375 scalarboolean(first);
3376 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3377 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3378 if (type == OP_AND || type == OP_OR) {
3384 first = *firstp = cUNOPo->op_first;
3386 first->op_next = o->op_next;
3387 cUNOPo->op_first = Nullop;
3391 if (first->op_type == OP_CONST) {
3392 if (ckWARN(WARN_PRECEDENCE) && (first->op_private & OPpCONST_BARE))
3393 Perl_warner(aTHX_ WARN_PRECEDENCE, "Probable precedence problem on %s",
3395 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3406 else if (first->op_type == OP_WANTARRAY) {
3412 else if (ckWARN(WARN_UNSAFE) && (first->op_flags & OPf_KIDS)) {
3413 OP *k1 = ((UNOP*)first)->op_first;
3414 OP *k2 = k1->op_sibling;
3416 switch (first->op_type)
3419 if (k2 && k2->op_type == OP_READLINE
3420 && (k2->op_flags & OPf_STACKED)
3421 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3423 warnop = k2->op_type;
3428 if (k1->op_type == OP_READDIR
3429 || k1->op_type == OP_GLOB
3430 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3431 || k1->op_type == OP_EACH)
3433 warnop = ((k1->op_type == OP_NULL)
3434 ? k1->op_targ : k1->op_type);
3439 line_t oldline = PL_curcop->cop_line;
3440 PL_curcop->cop_line = PL_copline;
3441 Perl_warner(aTHX_ WARN_UNSAFE,
3442 "Value of %s%s can be \"0\"; test with defined()",
3444 ((warnop == OP_READLINE || warnop == OP_GLOB)
3445 ? " construct" : "() operator"));
3446 PL_curcop->cop_line = oldline;
3453 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3454 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3456 NewOp(1101, logop, 1, LOGOP);
3458 logop->op_type = type;
3459 logop->op_ppaddr = PL_ppaddr[type];
3460 logop->op_first = first;
3461 logop->op_flags = flags | OPf_KIDS;
3462 logop->op_other = LINKLIST(other);
3463 logop->op_private = 1 | (flags >> 8);
3465 /* establish postfix order */
3466 logop->op_next = LINKLIST(first);
3467 first->op_next = (OP*)logop;
3468 first->op_sibling = other;
3470 o = newUNOP(OP_NULL, 0, (OP*)logop);
3477 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
3485 return newLOGOP(OP_AND, 0, first, trueop);
3487 return newLOGOP(OP_OR, 0, first, falseop);
3489 scalarboolean(first);
3490 if (first->op_type == OP_CONST) {
3491 if (SvTRUE(((SVOP*)first)->op_sv)) {
3502 else if (first->op_type == OP_WANTARRAY) {
3506 NewOp(1101, logop, 1, LOGOP);
3507 logop->op_type = OP_COND_EXPR;
3508 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3509 logop->op_first = first;
3510 logop->op_flags = flags | OPf_KIDS;
3511 logop->op_private = 1 | (flags >> 8);
3512 logop->op_other = LINKLIST(trueop);
3513 logop->op_next = LINKLIST(falseop);
3516 /* establish postfix order */
3517 start = LINKLIST(first);
3518 first->op_next = (OP*)logop;
3520 first->op_sibling = trueop;
3521 trueop->op_sibling = falseop;
3522 o = newUNOP(OP_NULL, 0, (OP*)logop);
3524 trueop->op_next = falseop->op_next = o;
3531 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
3540 NewOp(1101, range, 1, LOGOP);
3542 range->op_type = OP_RANGE;
3543 range->op_ppaddr = PL_ppaddr[OP_RANGE];
3544 range->op_first = left;
3545 range->op_flags = OPf_KIDS;
3546 leftstart = LINKLIST(left);
3547 range->op_other = LINKLIST(right);
3548 range->op_private = 1 | (flags >> 8);
3550 left->op_sibling = right;
3552 range->op_next = (OP*)range;
3553 flip = newUNOP(OP_FLIP, flags, (OP*)range);
3554 flop = newUNOP(OP_FLOP, 0, flip);
3555 o = newUNOP(OP_NULL, 0, flop);
3557 range->op_next = leftstart;
3559 left->op_next = flip;
3560 right->op_next = flop;
3562 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3563 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
3564 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3565 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3567 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3568 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3571 if (!flip->op_private || !flop->op_private)
3572 linklist(o); /* blow off optimizer unless constant */
3578 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
3583 int once = block && block->op_flags & OPf_SPECIAL &&
3584 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3587 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3588 return block; /* do {} while 0 does once */
3589 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3590 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3591 expr = newUNOP(OP_DEFINED, 0,
3592 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3593 } else if (expr->op_flags & OPf_KIDS) {
3594 OP *k1 = ((UNOP*)expr)->op_first;
3595 OP *k2 = (k1) ? k1->op_sibling : NULL;
3596 switch (expr->op_type) {
3598 if (k2 && k2->op_type == OP_READLINE
3599 && (k2->op_flags & OPf_STACKED)
3600 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3601 expr = newUNOP(OP_DEFINED, 0, expr);
3605 if (k1->op_type == OP_READDIR
3606 || k1->op_type == OP_GLOB
3607 || (k1->op_type == OP_NULL && k1->op_targ == OP_NULL)
3608 || k1->op_type == OP_EACH)
3609 expr = newUNOP(OP_DEFINED, 0, expr);
3615 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3616 o = new_logop(OP_AND, 0, &expr, &listop);
3619 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3621 if (once && o != listop)
3622 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3625 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3627 o->op_flags |= flags;
3629 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3634 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
3643 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3644 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
3645 expr = newUNOP(OP_DEFINED, 0,
3646 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3647 } else if (expr && (expr->op_flags & OPf_KIDS)) {
3648 OP *k1 = ((UNOP*)expr)->op_first;
3649 OP *k2 = (k1) ? k1->op_sibling : NULL;
3650 switch (expr->op_type) {
3652 if (k2 && k2->op_type == OP_READLINE
3653 && (k2->op_flags & OPf_STACKED)
3654 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3655 expr = newUNOP(OP_DEFINED, 0, expr);
3659 if (k1->op_type == OP_READDIR
3660 || k1->op_type == OP_GLOB
3661 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3662 || k1->op_type == OP_EACH)
3663 expr = newUNOP(OP_DEFINED, 0, expr);
3669 block = newOP(OP_NULL, 0);
3672 next = LINKLIST(cont);
3674 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
3675 if ((line_t)whileline != NOLINE) {
3676 PL_copline = whileline;
3677 cont = append_elem(OP_LINESEQ, cont,
3678 newSTATEOP(0, Nullch, Nullop));
3682 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3683 redo = LINKLIST(listop);
3686 PL_copline = whileline;
3688 o = new_logop(OP_AND, 0, &expr, &listop);
3689 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3690 op_free(expr); /* oops, it's a while (0) */
3692 return Nullop; /* listop already freed by new_logop */
3695 ((LISTOP*)listop)->op_last->op_next = condop =
3696 (o == listop ? redo : LINKLIST(o));
3704 NewOp(1101,loop,1,LOOP);
3705 loop->op_type = OP_ENTERLOOP;
3706 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3707 loop->op_private = 0;
3708 loop->op_next = (OP*)loop;
3711 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3713 loop->op_redoop = redo;
3714 loop->op_lastop = o;
3717 loop->op_nextop = next;
3719 loop->op_nextop = o;
3721 o->op_flags |= flags;
3722 o->op_private |= (flags >> 8);
3727 Perl_newFOROP(pTHX_ I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
3736 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3737 sv->op_type = OP_RV2GV;
3738 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
3740 else if (sv->op_type == OP_PADSV) { /* private variable */
3741 padoff = sv->op_targ;
3745 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3746 padoff = sv->op_targ;
3747 iterflags |= OPf_SPECIAL;
3752 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
3756 padoff = find_threadsv("_");
3757 iterflags |= OPf_SPECIAL;
3759 sv = newGVOP(OP_GV, 0, PL_defgv);
3762 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3763 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3764 iterflags |= OPf_STACKED;
3766 else if (expr->op_type == OP_NULL &&
3767 (expr->op_flags & OPf_KIDS) &&
3768 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3770 /* Basically turn for($x..$y) into the same as for($x,$y), but we
3771 * set the STACKED flag to indicate that these values are to be
3772 * treated as min/max values by 'pp_iterinit'.
3774 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3775 LOGOP* range = (LOGOP*) flip->op_first;
3776 OP* left = range->op_first;
3777 OP* right = left->op_sibling;
3780 range->op_flags &= ~OPf_KIDS;
3781 range->op_first = Nullop;
3783 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3784 listop->op_first->op_next = range->op_next;
3785 left->op_next = range->op_other;
3786 right->op_next = (OP*)listop;
3787 listop->op_next = listop->op_first;
3790 expr = (OP*)(listop);
3792 iterflags |= OPf_STACKED;
3795 expr = mod(force_list(expr), OP_GREPSTART);
3799 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
3800 append_elem(OP_LIST, expr, scalar(sv))));
3801 assert(!loop->op_next);
3802 #ifdef PL_OP_SLAB_ALLOC
3803 NewOp(1234,tmp,1,LOOP);
3804 Copy(loop,tmp,1,LOOP);
3807 Renew(loop, 1, LOOP);
3809 loop->op_targ = padoff;
3810 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
3811 PL_copline = forline;
3812 return newSTATEOP(0, label, wop);
3816 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
3822 if (type != OP_GOTO || label->op_type == OP_CONST) {
3823 /* "last()" means "last" */
3824 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
3825 o = newOP(type, OPf_SPECIAL);
3827 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
3828 ? SvPVx(((SVOP*)label)->op_sv, n_a)
3834 if (label->op_type == OP_ENTERSUB)
3835 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
3836 o = newUNOP(type, OPf_STACKED, label);
3838 PL_hints |= HINT_BLOCK_SCOPE;
3843 Perl_cv_undef(pTHX_ CV *cv)
3848 MUTEX_DESTROY(CvMUTEXP(cv));
3849 Safefree(CvMUTEXP(cv));
3852 #endif /* USE_THREADS */
3854 if (!CvXSUB(cv) && CvROOT(cv)) {
3856 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
3857 Perl_croak(aTHX_ "Can't undef active subroutine");
3860 Perl_croak(aTHX_ "Can't undef active subroutine");
3861 #endif /* USE_THREADS */
3864 SAVESPTR(PL_curpad);
3868 op_free(CvROOT(cv));
3869 CvROOT(cv) = Nullop;
3872 SvPOK_off((SV*)cv); /* forget prototype */
3874 SvREFCNT_dec(CvGV(cv));
3876 SvREFCNT_dec(CvOUTSIDE(cv));
3877 CvOUTSIDE(cv) = Nullcv;
3878 if (CvPADLIST(cv)) {
3879 /* may be during global destruction */
3880 if (SvREFCNT(CvPADLIST(cv))) {
3881 I32 i = AvFILLp(CvPADLIST(cv));
3883 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
3884 SV* sv = svp ? *svp : Nullsv;
3887 if (sv == (SV*)PL_comppad_name)
3888 PL_comppad_name = Nullav;
3889 else if (sv == (SV*)PL_comppad) {
3890 PL_comppad = Nullav;
3891 PL_curpad = Null(SV**);
3895 SvREFCNT_dec((SV*)CvPADLIST(cv));
3897 CvPADLIST(cv) = Nullav;
3901 #ifdef DEBUG_CLOSURES
3905 CV *outside = CvOUTSIDE(cv);
3906 AV* padlist = CvPADLIST(cv);
3913 PerlIO_printf(Perl_debug_log,
3914 "\tCV=0x%"UVxf" (%s), OUTSIDE=0x%"UVxf" (%s)\n",
3916 (CvANON(cv) ? "ANON"
3917 : (cv == PL_main_cv) ? "MAIN"
3918 : CvUNIQUE(cv) ? "UNIQUE"
3919 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
3922 : CvANON(outside) ? "ANON"
3923 : (outside == PL_main_cv) ? "MAIN"
3924 : CvUNIQUE(outside) ? "UNIQUE"
3925 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
3930 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
3931 pad = (AV*)*av_fetch(padlist, 1, FALSE);
3932 pname = AvARRAY(pad_name);
3933 ppad = AvARRAY(pad);
3935 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
3936 if (SvPOK(pname[ix]))
3937 PerlIO_printf(Perl_debug_log,
3938 "\t%4d. 0x%"UVxf" (%s\"%s\" %"IVdf"-%"IVdf")\n",
3939 ix, PTR2UV(ppad[ix]),
3940 SvFAKE(pname[ix]) ? "FAKE " : "",
3942 (IV)I_32(SvNVX(pname[ix])),
3946 #endif /* DEBUG_CLOSURES */
3949 S_cv_clone2(pTHX_ CV *proto, CV *outside)
3954 AV* protopadlist = CvPADLIST(proto);
3955 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
3956 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
3957 SV** pname = AvARRAY(protopad_name);
3958 SV** ppad = AvARRAY(protopad);
3959 I32 fname = AvFILLp(protopad_name);
3960 I32 fpad = AvFILLp(protopad);
3964 assert(!CvUNIQUE(proto));
3967 SAVESPTR(PL_curpad);
3968 SAVESPTR(PL_comppad);
3969 SAVESPTR(PL_comppad_name);
3970 SAVESPTR(PL_compcv);
3972 cv = PL_compcv = (CV*)NEWSV(1104,0);
3973 sv_upgrade((SV *)cv, SvTYPE(proto));
3979 New(666, CvMUTEXP(cv), 1, perl_mutex);
3980 MUTEX_INIT(CvMUTEXP(cv));
3982 #endif /* USE_THREADS */
3983 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
3984 CvSTASH(cv) = CvSTASH(proto);
3985 CvROOT(cv) = CvROOT(proto);
3986 CvSTART(cv) = CvSTART(proto);
3988 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
3991 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
3993 PL_comppad_name = newAV();
3994 for (ix = fname; ix >= 0; ix--)
3995 av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix]));
3997 PL_comppad = newAV();
3999 comppadlist = newAV();
4000 AvREAL_off(comppadlist);
4001 av_store(comppadlist, 0, (SV*)PL_comppad_name);
4002 av_store(comppadlist, 1, (SV*)PL_comppad);
4003 CvPADLIST(cv) = comppadlist;
4004 av_fill(PL_comppad, AvFILLp(protopad));
4005 PL_curpad = AvARRAY(PL_comppad);
4007 av = newAV(); /* will be @_ */
4009 av_store(PL_comppad, 0, (SV*)av);
4010 AvFLAGS(av) = AVf_REIFY;
4012 for (ix = fpad; ix > 0; ix--) {
4013 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
4014 if (namesv && namesv != &PL_sv_undef) {
4015 char *name = SvPVX(namesv); /* XXX */
4016 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
4017 I32 off = pad_findlex(name, ix, SvIVX(namesv),
4018 CvOUTSIDE(cv), cxstack_ix, 0, 0);
4020 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
4022 Perl_croak(aTHX_ "panic: cv_clone: %s", name);
4024 else { /* our own lexical */
4027 /* anon code -- we'll come back for it */
4028 sv = SvREFCNT_inc(ppad[ix]);
4030 else if (*name == '@')
4032 else if (*name == '%')
4042 SV* sv = NEWSV(0,0);
4048 /* Now that vars are all in place, clone nested closures. */
4050 for (ix = fpad; ix > 0; ix--) {
4051 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
4053 && namesv != &PL_sv_undef
4054 && !(SvFLAGS(namesv) & SVf_FAKE)
4055 && *SvPVX(namesv) == '&'
4056 && CvCLONE(ppad[ix]))
4058 CV *kid = cv_clone2((CV*)ppad[ix], cv);
4059 SvREFCNT_dec(ppad[ix]);
4062 PL_curpad[ix] = (SV*)kid;
4066 #ifdef DEBUG_CLOSURES
4067 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
4069 PerlIO_printf(Perl_debug_log, " from:\n");
4071 PerlIO_printf(Perl_debug_log, " to:\n");
4080 Perl_cv_clone(pTHX_ CV *proto)
4083 MUTEX_LOCK(&PL_cred_mutex); /* XXX create separate mutex */
4084 cv = cv_clone2(proto, CvOUTSIDE(proto));
4085 MUTEX_UNLOCK(&PL_cred_mutex); /* XXX create separate mutex */
4090 Perl_cv_ckproto(pTHX_ CV *cv, GV *gv, char *p)
4094 if (((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) && ckWARN_d(WARN_UNSAFE)) {
4095 SV* msg = sv_newmortal();
4099 gv_efullname3(name = sv_newmortal(), gv, Nullch);
4100 sv_setpv(msg, "Prototype mismatch:");
4102 Perl_sv_catpvf(aTHX_ msg, " sub %_", name);
4104 Perl_sv_catpvf(aTHX_ msg, " (%s)", SvPVX(cv));
4105 sv_catpv(msg, " vs ");
4107 Perl_sv_catpvf(aTHX_ msg, "(%s)", p);
4109 sv_catpv(msg, "none");
4110 Perl_warner(aTHX_ WARN_UNSAFE, "%_", msg);
4115 Perl_cv_const_sv(pTHX_ CV *cv)
4117 if (!cv || !SvPOK(cv) || SvCUR(cv))
4119 return op_const_sv(CvSTART(cv), cv);
4123 Perl_op_const_sv(pTHX_ OP *o, CV *cv)
4130 if(o->op_type == OP_LINESEQ && cLISTOPo->op_first)
4131 o = cLISTOPo->op_first->op_sibling;
4133 for (; o; o = o->op_next) {
4134 OPCODE type = o->op_type;
4136 if (sv && o->op_next == o)
4138 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
4140 if (type == OP_LEAVESUB || type == OP_RETURN)
4144 if (type == OP_CONST)
4146 else if (type == OP_PADSV && cv) {
4147 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
4148 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
4149 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
4161 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4171 Perl_croak(aTHX_ "\"my sub\" not yet implemented");
4175 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
4177 return Perl_newATTRSUB(aTHX_ floor, o, proto, Nullop, block);
4181 Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4185 char *name = o ? SvPVx(cSVOPo->op_sv, n_a) : Nullch;
4186 GV *gv = gv_fetchpv(name ? name : "__ANON__",
4187 GV_ADDMULTI | ((block || attrs) ? 0 : GV_NOINIT),
4189 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, n_a) : Nullch;
4200 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
4201 maximum a prototype before. */
4202 if (SvTYPE(gv) > SVt_NULL) {
4203 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1)
4204 && ckWARN_d(WARN_UNSAFE))
4206 Perl_warner(aTHX_ WARN_UNSAFE, "Runaway prototype");
4208 cv_ckproto((CV*)gv, NULL, ps);
4211 sv_setpv((SV*)gv, ps);
4213 sv_setiv((SV*)gv, -1);
4214 SvREFCNT_dec(PL_compcv);
4215 cv = PL_compcv = NULL;
4216 PL_sub_generation++;
4220 if (!name || GvCVGEN(gv))
4222 else if (cv = GvCV(gv)) {
4223 cv_ckproto(cv, gv, ps);
4224 /* already defined (or promised)? */
4225 if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4227 bool const_changed = TRUE;
4228 if (!block && !attrs) {
4229 /* just a "sub foo;" when &foo is already defined */
4230 SAVEFREESV(PL_compcv);
4233 /* ahem, death to those who redefine active sort subs */
4234 if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv))
4235 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", name);
4238 if(const_sv = cv_const_sv(cv))
4239 const_changed = sv_cmp(const_sv, op_const_sv(block, Nullcv));
4240 if ((const_sv && const_changed) || ckWARN(WARN_REDEFINE)
4241 && !(CvGV(cv) && GvSTASH(CvGV(cv))
4242 && HvNAME(GvSTASH(CvGV(cv)))
4243 && strEQ(HvNAME(GvSTASH(CvGV(cv))),
4245 line_t oldline = PL_curcop->cop_line;
4246 PL_curcop->cop_line = PL_copline;
4247 Perl_warner(aTHX_ WARN_REDEFINE,
4248 const_sv ? "Constant subroutine %s redefined"
4249 : "Subroutine %s redefined", name);
4250 PL_curcop->cop_line = oldline;
4261 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>
4262 * before we clobber PL_compcv.
4266 if (CvGV(cv) && GvSTASH(CvGV(cv)) && HvNAME(GvSTASH(CvGV(cv))))
4267 stash = GvSTASH(CvGV(cv));
4268 else if (CvSTASH(cv) && HvNAME(CvSTASH(cv)))
4269 stash = CvSTASH(cv);
4271 stash = PL_curstash;
4274 /* possibly about to re-define existing subr -- ignore old cv */
4275 rcv = (SV*)PL_compcv;
4276 if (name && GvSTASH(gv) && HvNAME(GvSTASH(gv)))
4277 stash = GvSTASH(gv);
4279 stash = PL_curstash;
4281 apply_attrs(stash, rcv, attrs);
4283 if (cv) { /* must reuse cv if autoloaded */
4285 /* got here with just attrs -- work done, so bug out */
4286 SAVEFREESV(PL_compcv);
4290 CvFLAGS(cv) = CvFLAGS(PL_compcv);
4291 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
4292 CvOUTSIDE(PL_compcv) = 0;
4293 CvPADLIST(cv) = CvPADLIST(PL_compcv);
4294 CvPADLIST(PL_compcv) = 0;
4295 if (SvREFCNT(PL_compcv) > 1) /* XXX Make closures transit through stub. */
4296 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc((SV*)cv);
4297 SvREFCNT_dec(PL_compcv);
4304 PL_sub_generation++;
4307 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4308 CvSTASH(cv) = PL_curstash;
4311 if (!CvMUTEXP(cv)) {
4312 New(666, CvMUTEXP(cv), 1, perl_mutex);
4313 MUTEX_INIT(CvMUTEXP(cv));
4315 #endif /* USE_THREADS */
4318 sv_setpv((SV*)cv, ps);
4320 if (PL_error_count) {
4324 char *s = strrchr(name, ':');
4326 if (strEQ(s, "BEGIN")) {
4328 "BEGIN not safe after errors--compilation aborted";
4329 if (PL_in_eval & EVAL_KEEPERR)
4330 Perl_croak(aTHX_ not_safe);
4332 /* force display of errors found but not reported */
4333 sv_catpv(ERRSV, not_safe);
4334 Perl_croak(aTHX_ "%s", SvPVx(ERRSV, n_a));
4341 PL_copline = NOLINE;
4346 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad))
4347 av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv);
4350 SV **namep = AvARRAY(PL_comppad_name);
4351 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4354 if (SvIMMORTAL(PL_curpad[ix]))
4357 * The only things that a clonable function needs in its
4358 * pad are references to outer lexicals and anonymous subs.
4359 * The rest are created anew during cloning.
4361 if (!((namesv = namep[ix]) != Nullsv &&
4362 namesv != &PL_sv_undef &&
4364 *SvPVX(namesv) == '&')))
4366 SvREFCNT_dec(PL_curpad[ix]);
4367 PL_curpad[ix] = Nullsv;
4372 AV *av = newAV(); /* Will be @_ */
4374 av_store(PL_comppad, 0, (SV*)av);
4375 AvFLAGS(av) = AVf_REIFY;
4377 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4378 if (SvIMMORTAL(PL_curpad[ix]))
4380 if (!SvPADMY(PL_curpad[ix]))
4381 SvPADTMP_on(PL_curpad[ix]);
4386 CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0, scalarseq(block));
4389 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4391 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4392 OpREFCNT_set(CvROOT(cv), 1);
4393 CvSTART(cv) = LINKLIST(CvROOT(cv));
4394 CvROOT(cv)->op_next = 0;
4400 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4401 SV *sv = NEWSV(0,0);
4402 SV *tmpstr = sv_newmortal();
4403 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
4407 Perl_sv_setpvf(aTHX_ sv, "%_:%ld-%ld",
4408 CopFILESV(PL_curcop),
4409 (long)PL_subline, (long)CopLINE(PL_curcop));
4410 gv_efullname3(tmpstr, gv, Nullch);
4411 hv_store(GvHV(PL_DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
4412 hv = GvHVn(db_postponed);
4413 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
4414 && (cv = GvCV(db_postponed))) {
4419 call_sv((SV*)cv, G_DISCARD);
4423 if ((s = strrchr(name,':')))
4427 if (strEQ(s, "BEGIN")) {
4428 I32 oldscope = PL_scopestack_ix;
4430 SAVESPTR(CopFILEGV(&PL_compiling));
4431 SAVEI16(PL_compiling.cop_line);
4433 sv_setsv(PL_rs, PL_nrs);
4436 PL_beginav = newAV();
4437 DEBUG_x( dump_sub(gv) );
4438 av_push(PL_beginav, (SV *)cv);
4440 call_list(oldscope, PL_beginav);
4442 PL_curcop = &PL_compiling;
4443 PL_compiling.op_private = PL_hints;
4446 else if (strEQ(s, "END") && !PL_error_count) {
4449 av_unshift(PL_endav, 1);
4450 av_store(PL_endav, 0, (SV *)cv);
4453 else if (strEQ(s, "STOP") && !PL_error_count) {
4455 PL_stopav = newAV();
4456 av_unshift(PL_stopav, 1);
4457 av_store(PL_stopav, 0, (SV *)cv);
4460 else if (strEQ(s, "INIT") && !PL_error_count) {
4462 PL_initav = newAV();
4463 av_push(PL_initav, SvREFCNT_inc(cv));
4469 PL_copline = NOLINE;
4474 /* XXX unsafe for threads if eval_owner isn't held */
4476 Perl_newCONSTSUB(pTHX_ HV *stash, char *name, SV *sv)
4479 U32 oldhints = PL_hints;
4480 HV *old_cop_stash = PL_curcop->cop_stash;
4481 HV *old_curstash = PL_curstash;
4482 line_t oldline = PL_curcop->cop_line;
4483 PL_curcop->cop_line = PL_copline;
4485 PL_hints &= ~HINT_BLOCK_SCOPE;
4487 PL_curstash = PL_curcop->cop_stash = stash;
4490 start_subparse(FALSE, 0),
4491 newSVOP(OP_CONST, 0, newSVpv(name,0)),
4492 newSVOP(OP_CONST, 0, &PL_sv_no), /* SvPV(&PL_sv_no) == "" -- GMB */
4494 newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv))
4497 PL_hints = oldhints;
4498 PL_curcop->cop_stash = old_cop_stash;
4499 PL_curstash = old_curstash;
4500 PL_curcop->cop_line = oldline;
4504 Perl_newXS(pTHX_ char *name, XSUBADDR_t subaddr, char *filename)
4507 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
4510 if (cv = (name ? GvCV(gv) : Nullcv)) {
4512 /* just a cached method */
4516 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4517 /* already defined (or promised) */
4518 if (ckWARN(WARN_REDEFINE) && !(CvGV(cv) && GvSTASH(CvGV(cv))
4519 && HvNAME(GvSTASH(CvGV(cv)))
4520 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
4521 line_t oldline = PL_curcop->cop_line;
4522 if (PL_copline != NOLINE)
4523 PL_curcop->cop_line = PL_copline;
4524 Perl_warner(aTHX_ WARN_REDEFINE, "Subroutine %s redefined",name);
4525 PL_curcop->cop_line = oldline;
4532 if (cv) /* must reuse cv if autoloaded */
4535 cv = (CV*)NEWSV(1105,0);
4536 sv_upgrade((SV *)cv, SVt_PVCV);
4540 PL_sub_generation++;
4543 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4545 New(666, CvMUTEXP(cv), 1, perl_mutex);
4546 MUTEX_INIT(CvMUTEXP(cv));
4548 #endif /* USE_THREADS */
4549 (void)gv_fetchfile(filename);
4550 CvXSUB(cv) = subaddr;
4553 char *s = strrchr(name,':');
4558 if (strEQ(s, "BEGIN")) {
4560 PL_beginav = newAV();
4561 av_push(PL_beginav, (SV *)cv);
4564 else if (strEQ(s, "END")) {
4567 av_unshift(PL_endav, 1);
4568 av_store(PL_endav, 0, (SV *)cv);
4571 else if (strEQ(s, "STOP")) {
4573 PL_stopav = newAV();
4574 av_unshift(PL_stopav, 1);
4575 av_store(PL_stopav, 0, (SV *)cv);
4578 else if (strEQ(s, "INIT")) {
4580 PL_initav = newAV();
4581 av_push(PL_initav, (SV *)cv);
4592 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
4602 name = SvPVx(cSVOPo->op_sv, n_a);
4605 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
4607 if (cv = GvFORM(gv)) {
4608 if (ckWARN(WARN_REDEFINE)) {
4609 line_t oldline = PL_curcop->cop_line;
4611 PL_curcop->cop_line = PL_copline;
4612 Perl_warner(aTHX_ WARN_REDEFINE, "Format %s redefined",name);
4613 PL_curcop->cop_line = oldline;
4619 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4621 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4622 if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix]))
4623 SvPADTMP_on(PL_curpad[ix]);
4626 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4627 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4628 OpREFCNT_set(CvROOT(cv), 1);
4629 CvSTART(cv) = LINKLIST(CvROOT(cv));
4630 CvROOT(cv)->op_next = 0;
4633 PL_copline = NOLINE;
4638 Perl_newANONLIST(pTHX_ OP *o)
4640 return newUNOP(OP_REFGEN, 0,
4641 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
4645 Perl_newANONHASH(pTHX_ OP *o)
4647 return newUNOP(OP_REFGEN, 0,
4648 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
4652 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
4654 return newANONATTRSUB(floor, proto, Nullop, block);
4658 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
4660 return newUNOP(OP_REFGEN, 0,
4661 newSVOP(OP_ANONCODE, 0,
4662 (SV*)newATTRSUB(floor, 0, proto, attrs, block)));
4666 Perl_oopsAV(pTHX_ OP *o)
4668 switch (o->op_type) {
4670 o->op_type = OP_PADAV;
4671 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4672 return ref(o, OP_RV2AV);
4675 o->op_type = OP_RV2AV;
4676 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4681 if (ckWARN_d(WARN_INTERNAL))
4682 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsAV");
4689 Perl_oopsHV(pTHX_ OP *o)
4693 switch (o->op_type) {
4696 o->op_type = OP_PADHV;
4697 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4698 return ref(o, OP_RV2HV);
4702 o->op_type = OP_RV2HV;
4703 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
4708 if (ckWARN_d(WARN_INTERNAL))
4709 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsHV");
4716 Perl_newAVREF(pTHX_ OP *o)
4718 if (o->op_type == OP_PADANY) {
4719 o->op_type = OP_PADAV;
4720 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4723 return newUNOP(OP_RV2AV, 0, scalar(o));
4727 Perl_newGVREF(pTHX_ I32 type, OP *o)
4729 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
4730 return newUNOP(OP_NULL, 0, o);
4731 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
4735 Perl_newHVREF(pTHX_ OP *o)
4737 if (o->op_type == OP_PADANY) {
4738 o->op_type = OP_PADHV;
4739 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4742 return newUNOP(OP_RV2HV, 0, scalar(o));
4746 Perl_oopsCV(pTHX_ OP *o)
4748 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
4754 Perl_newCVREF(pTHX_ I32 flags, OP *o)
4756 return newUNOP(OP_RV2CV, flags, scalar(o));
4760 Perl_newSVREF(pTHX_ OP *o)
4762 if (o->op_type == OP_PADANY) {
4763 o->op_type = OP_PADSV;
4764 o->op_ppaddr = PL_ppaddr[OP_PADSV];
4767 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
4768 o->op_flags |= OPpDONE_SVREF;
4771 return newUNOP(OP_RV2SV, 0, scalar(o));
4774 /* Check routines. */
4777 Perl_ck_anoncode(pTHX_ OP *o)
4782 name = NEWSV(1106,0);
4783 sv_upgrade(name, SVt_PVNV);
4784 sv_setpvn(name, "&", 1);
4787 ix = pad_alloc(o->op_type, SVs_PADMY);
4788 av_store(PL_comppad_name, ix, name);
4789 av_store(PL_comppad, ix, cSVOPo->op_sv);
4790 SvPADMY_on(cSVOPo->op_sv);
4791 cSVOPo->op_sv = Nullsv;
4792 cSVOPo->op_targ = ix;
4797 Perl_ck_bitop(pTHX_ OP *o)
4799 o->op_private = PL_hints;
4804 Perl_ck_concat(pTHX_ OP *o)
4806 if (cUNOPo->op_first->op_type == OP_CONCAT)
4807 o->op_flags |= OPf_STACKED;
4812 Perl_ck_spair(pTHX_ OP *o)
4814 if (o->op_flags & OPf_KIDS) {
4817 OPCODE type = o->op_type;
4818 o = modkids(ck_fun(o), type);
4819 kid = cUNOPo->op_first;
4820 newop = kUNOP->op_first->op_sibling;
4822 (newop->op_sibling ||
4823 !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
4824 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
4825 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
4829 op_free(kUNOP->op_first);
4830 kUNOP->op_first = newop;
4832 o->op_ppaddr = PL_ppaddr[++o->op_type];
4837 Perl_ck_delete(pTHX_ OP *o)
4841 if (o->op_flags & OPf_KIDS) {
4842 OP *kid = cUNOPo->op_first;
4843 if (kid->op_type == OP_HSLICE)
4844 o->op_private |= OPpSLICE;
4845 else if (kid->op_type != OP_HELEM)
4846 Perl_croak(aTHX_ "%s argument is not a HASH element or slice",
4847 PL_op_desc[o->op_type]);
4854 Perl_ck_eof(pTHX_ OP *o)
4856 I32 type = o->op_type;
4858 if (o->op_flags & OPf_KIDS) {
4859 if (cLISTOPo->op_first->op_type == OP_STUB) {
4861 o = newUNOP(type, OPf_SPECIAL,
4862 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
4870 Perl_ck_eval(pTHX_ OP *o)
4872 PL_hints |= HINT_BLOCK_SCOPE;
4873 if (o->op_flags & OPf_KIDS) {
4874 SVOP *kid = (SVOP*)cUNOPo->op_first;
4877 o->op_flags &= ~OPf_KIDS;
4880 else if (kid->op_type == OP_LINESEQ) {
4883 kid->op_next = o->op_next;
4884 cUNOPo->op_first = 0;
4887 NewOp(1101, enter, 1, LOGOP);
4888 enter->op_type = OP_ENTERTRY;
4889 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
4890 enter->op_private = 0;
4892 /* establish postfix order */
4893 enter->op_next = (OP*)enter;
4895 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
4896 o->op_type = OP_LEAVETRY;
4897 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
4898 enter->op_other = o;
4906 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
4908 o->op_targ = (PADOFFSET)PL_hints;
4913 Perl_ck_exec(pTHX_ OP *o)
4916 if (o->op_flags & OPf_STACKED) {
4918 kid = cUNOPo->op_first->op_sibling;
4919 if (kid->op_type == OP_RV2GV)
4928 Perl_ck_exists(pTHX_ OP *o)
4931 if (o->op_flags & OPf_KIDS) {
4932 OP *kid = cUNOPo->op_first;
4933 if (kid->op_type != OP_HELEM)
4934 Perl_croak(aTHX_ "%s argument is not a HASH element", PL_op_desc[o->op_type]);
4942 Perl_ck_gvconst(pTHX_ register OP *o)
4944 o = fold_constants(o);
4945 if (o->op_type == OP_CONST)
4952 Perl_ck_rvconst(pTHX_ register OP *o)
4955 SVOP *kid = (SVOP*)cUNOPo->op_first;
4957 o->op_private |= (PL_hints & HINT_STRICT_REFS);
4958 if (kid->op_type == OP_CONST) {
4962 SV *kidsv = kid->op_sv;
4965 /* Is it a constant from cv_const_sv()? */
4966 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
4967 SV *rsv = SvRV(kidsv);
4968 int svtype = SvTYPE(rsv);
4969 char *badtype = Nullch;
4971 switch (o->op_type) {
4973 if (svtype > SVt_PVMG)
4974 badtype = "a SCALAR";
4977 if (svtype != SVt_PVAV)
4978 badtype = "an ARRAY";
4981 if (svtype != SVt_PVHV) {
4982 if (svtype == SVt_PVAV) { /* pseudohash? */
4983 SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
4984 if (ksv && SvROK(*ksv)
4985 && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
4994 if (svtype != SVt_PVCV)
4999 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
5002 name = SvPV(kidsv, n_a);
5003 if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
5004 char *badthing = Nullch;
5005 switch (o->op_type) {
5007 badthing = "a SCALAR";
5010 badthing = "an ARRAY";
5013 badthing = "a HASH";
5018 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
5022 * This is a little tricky. We only want to add the symbol if we
5023 * didn't add it in the lexer. Otherwise we get duplicate strict
5024 * warnings. But if we didn't add it in the lexer, we must at
5025 * least pretend like we wanted to add it even if it existed before,
5026 * or we get possible typo warnings. OPpCONST_ENTERED says
5027 * whether the lexer already added THIS instance of this symbol.
5029 iscv = (o->op_type == OP_RV2CV) * 2;
5031 gv = gv_fetchpv(name,
5032 iscv | !(kid->op_private & OPpCONST_ENTERED),
5035 : o->op_type == OP_RV2SV
5037 : o->op_type == OP_RV2AV
5039 : o->op_type == OP_RV2HV
5042 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
5044 kid->op_type = OP_GV;
5045 SvREFCNT_dec(kid->op_sv);
5047 /* XXXXXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
5048 kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
5049 PL_curpad[kPADOP->op_padix] = SvREFCNT_inc(gv);
5051 kid->op_sv = SvREFCNT_inc(gv);
5053 kid->op_ppaddr = PL_ppaddr[OP_GV];
5060 Perl_ck_ftst(pTHX_ OP *o)
5063 I32 type = o->op_type;
5065 if (o->op_flags & OPf_REF)
5068 if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
5069 SVOP *kid = (SVOP*)cUNOPo->op_first;
5071 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5073 OP *newop = newGVOP(type, OPf_REF,
5074 gv_fetchpv(SvPVx(kid->op_sv, n_a), TRUE, SVt_PVIO));
5081 if (type == OP_FTTTY)
5082 return newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
5085 return newUNOP(type, 0, newDEFSVOP());
5091 Perl_ck_fun(pTHX_ OP *o)
5098 int type = o->op_type;
5099 register I32 oa = PL_opargs[type] >> OASHIFT;
5101 if (o->op_flags & OPf_STACKED) {
5102 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
5105 return no_fh_allowed(o);
5108 if (o->op_flags & OPf_KIDS) {
5110 tokid = &cLISTOPo->op_first;
5111 kid = cLISTOPo->op_first;
5112 if (kid->op_type == OP_PUSHMARK ||
5113 kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
5115 tokid = &kid->op_sibling;
5116 kid = kid->op_sibling;
5118 if (!kid && PL_opargs[type] & OA_DEFGV)
5119 *tokid = kid = newDEFSVOP();
5123 sibl = kid->op_sibling;
5126 /* list seen where single (scalar) arg expected? */
5127 if (numargs == 1 && !(oa >> 4)
5128 && kid->op_type == OP_LIST && type != OP_SCALAR)
5130 return too_many_arguments(o,PL_op_desc[type]);
5143 if (kid->op_type == OP_CONST &&
5144 (kid->op_private & OPpCONST_BARE))
5146 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5147 OP *newop = newAVREF(newGVOP(OP_GV, 0,
5148 gv_fetchpv(name, TRUE, SVt_PVAV) ));
5149 if (ckWARN(WARN_SYNTAX))
5150 Perl_warner(aTHX_ WARN_SYNTAX,
5151 "Array @%s missing the @ in argument %"IVdf" of %s()",
5152 name, (IV)numargs, PL_op_desc[type]);
5155 kid->op_sibling = sibl;
5158 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
5159 bad_type(numargs, "array", PL_op_desc[type], kid);
5163 if (kid->op_type == OP_CONST &&
5164 (kid->op_private & OPpCONST_BARE))
5166 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5167 OP *newop = newHVREF(newGVOP(OP_GV, 0,
5168 gv_fetchpv(name, TRUE, SVt_PVHV) ));
5169 if (ckWARN(WARN_SYNTAX))
5170 Perl_warner(aTHX_ WARN_SYNTAX,
5171 "Hash %%%s missing the %% in argument %"IVdf" of %s()",
5172 name, (IV)numargs, PL_op_desc[type]);
5175 kid->op_sibling = sibl;
5178 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
5179 bad_type(numargs, "hash", PL_op_desc[type], kid);
5184 OP *newop = newUNOP(OP_NULL, 0, kid);
5185 kid->op_sibling = 0;
5187 newop->op_next = newop;
5189 kid->op_sibling = sibl;
5194 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
5195 if (kid->op_type == OP_CONST &&
5196 (kid->op_private & OPpCONST_BARE))
5198 OP *newop = newGVOP(OP_GV, 0,
5199 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, n_a), TRUE,
5204 else if (kid->op_type == OP_READLINE) {
5205 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
5206 bad_type(numargs, "HANDLE", PL_op_desc[o->op_type], kid);
5209 I32 flags = OPf_SPECIAL;
5211 /* is this op a FH constructor? */
5212 if (is_handle_constructor(o,numargs)) {
5214 /* Set a flag to tell rv2gv to vivify
5215 * need to "prove" flag does not mean something
5216 * else already - NI-S 1999/05/07
5220 /* Helps with open($array[$n],...)
5221 but is too simplistic - need to do selectively
5226 kid->op_sibling = 0;
5227 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
5229 kid->op_private |= priv;
5232 kid->op_sibling = sibl;
5238 mod(scalar(kid), type);
5242 tokid = &kid->op_sibling;
5243 kid = kid->op_sibling;
5245 o->op_private |= numargs;
5247 return too_many_arguments(o,PL_op_desc[o->op_type]);
5250 else if (PL_opargs[type] & OA_DEFGV) {
5252 return newUNOP(type, 0, newDEFSVOP());
5256 while (oa & OA_OPTIONAL)
5258 if (oa && oa != OA_LIST)
5259 return too_few_arguments(o,PL_op_desc[o->op_type]);
5265 Perl_ck_glob(pTHX_ OP *o)
5269 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
5270 append_elem(OP_GLOB, o, newDEFSVOP());
5272 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
5273 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5275 #if defined(PERL_INTERNAL_GLOB) && !defined(MINIPERL_BUILD)
5276 /* XXX this can be tightened up and made more failsafe. */
5278 OP *modname = newSVOP(OP_CONST, 0, newSVpvn("File::Glob", 10));
5279 modname->op_private |= OPpCONST_BARE;
5281 utilize(1, start_subparse(FALSE, 0), Nullop, modname,
5282 newSVOP(OP_CONST, 0, newSVpvn("globally", 8)));
5283 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5286 #endif /* PERL_INTERNAL_GLOB && !MINIPERL_BUILD */
5288 if (gv && GvIMPORTED_CV(gv)) {
5289 append_elem(OP_GLOB, o,
5290 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
5291 o->op_type = OP_LIST;
5292 o->op_ppaddr = PL_ppaddr[OP_LIST];
5293 cLISTOPo->op_first->op_type = OP_PUSHMARK;
5294 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
5295 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
5296 append_elem(OP_LIST, o,
5297 scalar(newUNOP(OP_RV2CV, 0,
5298 newGVOP(OP_GV, 0, gv)))));
5299 o = newUNOP(OP_NULL, 0, ck_subr(o));
5300 o->op_targ = OP_GLOB; /* hint at what it used to be */
5303 gv = newGVgen("main");
5305 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
5311 Perl_ck_grep(pTHX_ OP *o)
5315 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
5317 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
5318 NewOp(1101, gwop, 1, LOGOP);
5320 if (o->op_flags & OPf_STACKED) {
5323 kid = cLISTOPo->op_first->op_sibling;
5324 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
5327 kid->op_next = (OP*)gwop;
5328 o->op_flags &= ~OPf_STACKED;
5330 kid = cLISTOPo->op_first->op_sibling;
5331 if (type == OP_MAPWHILE)
5338 kid = cLISTOPo->op_first->op_sibling;
5339 if (kid->op_type != OP_NULL)
5340 Perl_croak(aTHX_ "panic: ck_grep");
5341 kid = kUNOP->op_first;
5343 gwop->op_type = type;
5344 gwop->op_ppaddr = PL_ppaddr[type];
5345 gwop->op_first = listkids(o);
5346 gwop->op_flags |= OPf_KIDS;
5347 gwop->op_private = 1;
5348 gwop->op_other = LINKLIST(kid);
5349 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
5350 kid->op_next = (OP*)gwop;
5352 kid = cLISTOPo->op_first->op_sibling;
5353 if (!kid || !kid->op_sibling)
5354 return too_few_arguments(o,PL_op_desc[o->op_type]);
5355 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
5356 mod(kid, OP_GREPSTART);
5362 Perl_ck_index(pTHX_ OP *o)
5364 if (o->op_flags & OPf_KIDS) {
5365 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5367 kid = kid->op_sibling; /* get past "big" */
5368 if (kid && kid->op_type == OP_CONST)
5369 fbm_compile(((SVOP*)kid)->op_sv, 0);
5375 Perl_ck_lengthconst(pTHX_ OP *o)
5377 /* XXX length optimization goes here */
5382 Perl_ck_lfun(pTHX_ OP *o)
5384 OPCODE type = o->op_type;
5385 return modkids(ck_fun(o), type);
5389 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
5392 if ((o->op_flags & OPf_KIDS) && ckWARN(WARN_DEPRECATED)) {
5393 switch (cUNOPo->op_first->op_type) {
5395 break; /* Globals via GV can be undef */
5397 case OP_AASSIGN: /* Is this a good idea? */
5398 Perl_warner(aTHX_ WARN_DEPRECATED,
5399 "defined(@array) is deprecated");
5400 Perl_warner(aTHX_ WARN_DEPRECATED,
5401 "(Maybe you should just omit the defined()?)\n");
5404 break; /* Globals via GV can be undef */
5406 Perl_warner(aTHX_ WARN_DEPRECATED,
5407 "defined(%hash) is deprecated");
5408 Perl_warner(aTHX_ WARN_DEPRECATED,
5409 "(Maybe you should just omit the defined()?)\n");
5420 Perl_ck_rfun(pTHX_ OP *o)
5422 OPCODE type = o->op_type;
5423 return refkids(ck_fun(o), type);
5427 Perl_ck_listiob(pTHX_ OP *o)
5431 kid = cLISTOPo->op_first;
5434 kid = cLISTOPo->op_first;
5436 if (kid->op_type == OP_PUSHMARK)
5437 kid = kid->op_sibling;
5438 if (kid && o->op_flags & OPf_STACKED)
5439 kid = kid->op_sibling;
5440 else if (kid && !kid->op_sibling) { /* print HANDLE; */
5441 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
5442 o->op_flags |= OPf_STACKED; /* make it a filehandle */
5443 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
5444 cLISTOPo->op_first->op_sibling = kid;
5445 cLISTOPo->op_last = kid;
5446 kid = kid->op_sibling;
5451 append_elem(o->op_type, o, newDEFSVOP());
5457 if (PL_hints & HINT_LOCALE)
5458 o->op_private |= OPpLOCALE;
5465 Perl_ck_fun_locale(pTHX_ OP *o)
5471 if (PL_hints & HINT_LOCALE)
5472 o->op_private |= OPpLOCALE;
5479 Perl_ck_sassign(pTHX_ OP *o)
5481 OP *kid = cLISTOPo->op_first;
5482 /* has a disposable target? */
5483 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
5484 && !(kid->op_flags & OPf_STACKED))
5486 OP *kkid = kid->op_sibling;
5488 /* Can just relocate the target. */
5489 if (kkid && kkid->op_type == OP_PADSV
5490 && !(kkid->op_private & OPpLVAL_INTRO))
5492 /* Concat has problems if target is equal to right arg. */
5493 if (kid->op_type == OP_CONCAT) {
5494 if (kLISTOP->op_first->op_sibling->op_type == OP_PADSV
5495 && kLISTOP->op_first->op_sibling->op_targ == kkid->op_targ)
5498 else if (kid->op_type == OP_JOIN) {
5499 /* do_join has problems if the arguments coincide with target.
5500 In fact the second argument *can* safely coincide,
5501 but ignore=pessimize this rare occasion. */
5502 OP *arg = kLISTOP->op_first->op_sibling; /* Skip PUSHMARK */
5505 if (arg->op_type == OP_PADSV
5506 && arg->op_targ == kkid->op_targ)
5508 arg = arg->op_sibling;
5511 else if (kid->op_type == OP_QUOTEMETA) {
5512 /* quotemeta has problems if the argument coincides with target. */
5513 if (kLISTOP->op_first->op_type == OP_PADSV
5514 && kLISTOP->op_first->op_targ == kkid->op_targ)
5517 kid->op_targ = kkid->op_targ;
5518 /* Now we do not need PADSV and SASSIGN. */
5519 kid->op_sibling = o->op_sibling; /* NULL */
5520 cLISTOPo->op_first = NULL;
5523 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
5531 Perl_ck_scmp(pTHX_ OP *o)
5535 if (PL_hints & HINT_LOCALE)
5536 o->op_private |= OPpLOCALE;
5543 Perl_ck_match(pTHX_ OP *o)
5545 o->op_private |= OPpRUNTIME;
5550 Perl_ck_method(pTHX_ OP *o)
5552 OP *kid = cUNOPo->op_first;
5553 if (kid->op_type == OP_CONST) {
5554 SV* sv = kSVOP->op_sv;
5555 if (!(strchr(SvPVX(sv), ':') || strchr(SvPVX(sv), '\''))) {
5557 sv_upgrade(sv, SVt_PVIV);
5559 PERL_HASH(SvUVX(sv), SvPVX(sv), SvCUR(sv));
5560 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
5561 kSVOP->op_sv = Nullsv;
5570 Perl_ck_null(pTHX_ OP *o)
5576 Perl_ck_repeat(pTHX_ OP *o)
5578 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
5579 o->op_private |= OPpREPEAT_DOLIST;
5580 cBINOPo->op_first = force_list(cBINOPo->op_first);
5588 Perl_ck_require(pTHX_ OP *o)
5590 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
5591 SVOP *kid = (SVOP*)cUNOPo->op_first;
5593 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5595 for (s = SvPVX(kid->op_sv); *s; s++) {
5596 if (*s == ':' && s[1] == ':') {
5598 Move(s+2, s+1, strlen(s+2)+1, char);
5599 --SvCUR(kid->op_sv);
5602 sv_catpvn(kid->op_sv, ".pm", 3);
5610 Perl_ck_retarget(pTHX_ OP *o)
5612 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
5619 Perl_ck_select(pTHX_ OP *o)
5622 if (o->op_flags & OPf_KIDS) {
5623 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5624 if (kid && kid->op_sibling) {
5625 o->op_type = OP_SSELECT;
5626 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
5628 return fold_constants(o);
5632 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5633 if (kid && kid->op_type == OP_RV2GV)
5634 kid->op_private &= ~HINT_STRICT_REFS;
5639 Perl_ck_shift(pTHX_ OP *o)
5641 I32 type = o->op_type;
5643 if (!(o->op_flags & OPf_KIDS)) {
5648 if (!CvUNIQUE(PL_compcv)) {
5649 argop = newOP(OP_PADAV, OPf_REF);
5650 argop->op_targ = 0; /* PL_curpad[0] is @_ */
5653 argop = newUNOP(OP_RV2AV, 0,
5654 scalar(newGVOP(OP_GV, 0,
5655 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5658 argop = newUNOP(OP_RV2AV, 0,
5659 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(PL_compcv) ?
5660 PL_defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5661 #endif /* USE_THREADS */
5662 return newUNOP(type, 0, scalar(argop));
5664 return scalar(modkids(ck_fun(o), type));
5668 Perl_ck_sort(pTHX_ OP *o)
5672 if (PL_hints & HINT_LOCALE)
5673 o->op_private |= OPpLOCALE;
5676 if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
5678 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
5679 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5681 kid = kUNOP->op_first; /* get past null */
5683 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
5685 if (kid->op_type == OP_SCOPE) {
5689 else if (kid->op_type == OP_LEAVE) {
5690 if (o->op_type == OP_SORT) {
5691 null(kid); /* wipe out leave */
5694 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
5695 if (k->op_next == kid)
5700 kid->op_next = 0; /* just disconnect the leave */
5701 k = kLISTOP->op_first;
5705 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5706 if (o->op_type == OP_SORT)
5710 o->op_flags |= OPf_SPECIAL;
5712 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
5713 null(cLISTOPo->op_first->op_sibling);
5720 S_simplify_sort(pTHX_ OP *o)
5723 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5727 if (!(o->op_flags & OPf_STACKED))
5729 GvMULTI_on(gv_fetchpv("a", TRUE, SVt_PV));
5730 GvMULTI_on(gv_fetchpv("b", TRUE, SVt_PV));
5731 kid = kUNOP->op_first; /* get past null */
5732 if (kid->op_type != OP_SCOPE)
5734 kid = kLISTOP->op_last; /* get past scope */
5735 switch(kid->op_type) {
5743 k = kid; /* remember this node*/
5744 if (kBINOP->op_first->op_type != OP_RV2SV)
5746 kid = kBINOP->op_first; /* get past cmp */
5747 if (kUNOP->op_first->op_type != OP_GV)
5749 kid = kUNOP->op_first; /* get past rv2sv */
5751 if (GvSTASH(gv) != PL_curstash)
5753 if (strEQ(GvNAME(gv), "a"))
5755 else if(strEQ(GvNAME(gv), "b"))
5759 kid = k; /* back to cmp */
5760 if (kBINOP->op_last->op_type != OP_RV2SV)
5762 kid = kBINOP->op_last; /* down to 2nd arg */
5763 if (kUNOP->op_first->op_type != OP_GV)
5765 kid = kUNOP->op_first; /* get past rv2sv */
5767 if (GvSTASH(gv) != PL_curstash
5769 ? strNE(GvNAME(gv), "a")
5770 : strNE(GvNAME(gv), "b")))
5772 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
5774 o->op_private |= OPpSORT_REVERSE;
5775 if (k->op_type == OP_NCMP)
5776 o->op_private |= OPpSORT_NUMERIC;
5777 if (k->op_type == OP_I_NCMP)
5778 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
5779 kid = cLISTOPo->op_first->op_sibling;
5780 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
5781 op_free(kid); /* then delete it */
5782 cLISTOPo->op_children--;
5786 Perl_ck_split(pTHX_ OP *o)
5790 if (o->op_flags & OPf_STACKED)
5791 return no_fh_allowed(o);
5793 kid = cLISTOPo->op_first;
5794 if (kid->op_type != OP_NULL)
5795 Perl_croak(aTHX_ "panic: ck_split");
5796 kid = kid->op_sibling;
5797 op_free(cLISTOPo->op_first);
5798 cLISTOPo->op_first = kid;
5800 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvn(" ", 1));
5801 cLISTOPo->op_last = kid; /* There was only one element previously */
5804 if (kid->op_type != OP_MATCH) {
5805 OP *sibl = kid->op_sibling;
5806 kid->op_sibling = 0;
5807 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
5808 if (cLISTOPo->op_first == cLISTOPo->op_last)
5809 cLISTOPo->op_last = kid;
5810 cLISTOPo->op_first = kid;
5811 kid->op_sibling = sibl;
5814 kid->op_type = OP_PUSHRE;
5815 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
5818 if (!kid->op_sibling)
5819 append_elem(OP_SPLIT, o, newDEFSVOP());
5821 kid = kid->op_sibling;
5824 if (!kid->op_sibling)
5825 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
5827 kid = kid->op_sibling;
5830 if (kid->op_sibling)
5831 return too_many_arguments(o,PL_op_desc[o->op_type]);
5837 Perl_ck_join(pTHX_ OP *o)
5839 if (ckWARN(WARN_SYNTAX)) {
5840 OP *kid = cLISTOPo->op_first->op_sibling;
5841 if (kid && kid->op_type == OP_MATCH) {
5842 char *pmstr = "STRING";
5843 if (kPMOP->op_pmregexp)
5844 pmstr = kPMOP->op_pmregexp->precomp;
5845 Perl_warner(aTHX_ WARN_SYNTAX,
5846 "/%s/ should probably be written as \"%s\"",
5854 Perl_ck_subr(pTHX_ OP *o)
5857 OP *prev = ((cUNOPo->op_first->op_sibling)
5858 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
5859 OP *o2 = prev->op_sibling;
5868 o->op_private |= OPpENTERSUB_HASTARG;
5869 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
5870 if (cvop->op_type == OP_RV2CV) {
5872 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
5873 null(cvop); /* disable rv2cv */
5874 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
5875 if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) {
5876 GV *gv = cGVOPx(tmpop);
5879 tmpop->op_private |= OPpEARLY_CV;
5880 else if (SvPOK(cv)) {
5881 namegv = CvANON(cv) ? gv : CvGV(cv);
5882 proto = SvPV((SV*)cv, n_a);
5886 else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
5887 if (o2->op_type == OP_CONST)
5888 o2->op_private &= ~OPpCONST_STRICT;
5889 else if (o2->op_type == OP_LIST) {
5890 OP *o = ((UNOP*)o2)->op_first->op_sibling;
5891 if (o && o->op_type == OP_CONST)
5892 o->op_private &= ~OPpCONST_STRICT;
5895 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5896 if (PERLDB_SUB && PL_curstash != PL_debstash)
5897 o->op_private |= OPpENTERSUB_DB;
5898 while (o2 != cvop) {
5902 return too_many_arguments(o, gv_ename(namegv));
5920 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
5921 bad_type(arg, "block", gv_ename(namegv), o2);
5924 /* '*' allows any scalar type, including bareword */
5927 if (o2->op_type == OP_RV2GV)
5928 goto wrapref; /* autoconvert GLOB -> GLOBref */
5929 else if (o2->op_type == OP_CONST)
5930 o2->op_private &= ~OPpCONST_STRICT;
5931 else if (o2->op_type == OP_ENTERSUB) {
5932 /* accidental subroutine, revert to bareword */
5933 OP *gvop = ((UNOP*)o2)->op_first;
5934 if (gvop && gvop->op_type == OP_NULL) {
5935 gvop = ((UNOP*)gvop)->op_first;
5937 for (; gvop->op_sibling; gvop = gvop->op_sibling)
5940 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
5941 (gvop = ((UNOP*)gvop)->op_first) &&
5942 gvop->op_type == OP_GV)
5944 GV *gv = cGVOPx(gvop);
5945 OP *sibling = o2->op_sibling;
5946 SV *n = newSVpvn("",0);
5948 gv_fullname3(n, gv, "");
5949 if (SvCUR(n)>6 && strnEQ(SvPVX(n),"main::",6))
5950 sv_chop(n, SvPVX(n)+6);
5951 o2 = newSVOP(OP_CONST, 0, n);
5952 prev->op_sibling = o2;
5953 o2->op_sibling = sibling;
5965 if (o2->op_type != OP_RV2GV)
5966 bad_type(arg, "symbol", gv_ename(namegv), o2);
5969 if (o2->op_type != OP_RV2CV)
5970 bad_type(arg, "sub", gv_ename(namegv), o2);
5973 if (o2->op_type != OP_RV2SV
5974 && o2->op_type != OP_PADSV
5975 && o2->op_type != OP_HELEM
5976 && o2->op_type != OP_AELEM
5977 && o2->op_type != OP_THREADSV)
5979 bad_type(arg, "scalar", gv_ename(namegv), o2);
5983 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
5984 bad_type(arg, "array", gv_ename(namegv), o2);
5987 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
5988 bad_type(arg, "hash", gv_ename(namegv), o2);
5992 OP* sib = kid->op_sibling;
5993 kid->op_sibling = 0;
5994 o2 = newUNOP(OP_REFGEN, 0, kid);
5995 o2->op_sibling = sib;
5996 prev->op_sibling = o2;
6007 Perl_croak(aTHX_ "Malformed prototype for %s: %s",
6008 gv_ename(namegv), SvPV((SV*)cv, n_a));
6013 mod(o2, OP_ENTERSUB);
6015 o2 = o2->op_sibling;
6017 if (proto && !optional &&
6018 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
6019 return too_few_arguments(o, gv_ename(namegv));
6024 Perl_ck_svconst(pTHX_ OP *o)
6026 SvREADONLY_on(cSVOPo->op_sv);
6031 Perl_ck_trunc(pTHX_ OP *o)
6033 if (o->op_flags & OPf_KIDS) {
6034 SVOP *kid = (SVOP*)cUNOPo->op_first;
6036 if (kid->op_type == OP_NULL)
6037 kid = (SVOP*)kid->op_sibling;
6038 if (kid && kid->op_type == OP_CONST &&
6039 (kid->op_private & OPpCONST_BARE))
6041 o->op_flags |= OPf_SPECIAL;
6042 kid->op_private &= ~OPpCONST_STRICT;
6048 /* A peephole optimizer. We visit the ops in the order they're to execute. */
6051 Perl_peep(pTHX_ register OP *o)
6054 register OP* oldop = 0;
6056 OP *last_composite = Nullop;
6058 if (!o || o->op_seq)
6062 SAVESPTR(PL_curcop);
6063 for (; o; o = o->op_next) {
6069 switch (o->op_type) {
6073 PL_curcop = ((COP*)o); /* for warnings */
6074 o->op_seq = PL_op_seqmax++;
6075 last_composite = Nullop;
6079 if (cSVOPo->op_private & OPpCONST_STRICT)
6080 no_bareword_allowed(o);
6086 if ( o->op_next && o->op_next->op_type == OP_STRINGIFY
6087 && !(o->op_next->op_private & OPpTARGET_MY) )
6089 o->op_seq = PL_op_seqmax++;
6094 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
6095 if (o->op_next->op_private & OPpTARGET_MY) {
6096 if ((o->op_flags & OPf_STACKED) /* chained concats */
6097 || (o->op_type == OP_CONCAT
6098 /* Concat has problems if target is equal to right arg. */
6099 && (((LISTOP*)o)->op_first->op_sibling->op_type
6101 && (((LISTOP*)o)->op_first->op_sibling->op_targ
6102 == o->op_next->op_targ))) {
6103 goto ignore_optimization;
6106 o->op_targ = o->op_next->op_targ;
6107 o->op_private |= OPpTARGET_MY;
6112 ignore_optimization:
6113 o->op_seq = PL_op_seqmax++;
6116 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
6117 o->op_seq = PL_op_seqmax++;
6118 break; /* Scalar stub must produce undef. List stub is noop */
6122 if (o->op_targ == OP_NEXTSTATE
6123 || o->op_targ == OP_DBSTATE
6124 || o->op_targ == OP_SETSTATE)
6126 PL_curcop = ((COP*)o);
6133 if (oldop && o->op_next) {
6134 oldop->op_next = o->op_next;
6137 o->op_seq = PL_op_seqmax++;
6141 if (o->op_next->op_type == OP_RV2SV) {
6142 if (!(o->op_next->op_private & OPpDEREF)) {
6144 o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
6145 o->op_next = o->op_next->op_next;
6146 o->op_type = OP_GVSV;
6147 o->op_ppaddr = PL_ppaddr[OP_GVSV];
6150 else if (o->op_next->op_type == OP_RV2AV) {
6151 OP* pop = o->op_next->op_next;
6153 if (pop->op_type == OP_CONST &&
6154 (PL_op = pop->op_next) &&
6155 pop->op_next->op_type == OP_AELEM &&
6156 !(pop->op_next->op_private &
6157 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
6158 (i = SvIV(((SVOP*)pop)->op_sv) - PL_compiling.cop_arybase)
6166 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
6167 o->op_next = pop->op_next->op_next;
6168 o->op_type = OP_AELEMFAST;
6169 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
6170 o->op_private = (U8)i;
6175 else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_UNSAFE)) {
6177 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX(GvCV(gv))) {
6178 /* XXX could check prototype here instead of just carping */
6179 SV *sv = sv_newmortal();
6180 gv_efullname3(sv, gv, Nullch);
6181 Perl_warner(aTHX_ WARN_UNSAFE,
6182 "%s() called too early to check prototype",
6187 o->op_seq = PL_op_seqmax++;
6198 o->op_seq = PL_op_seqmax++;
6199 while (cLOGOP->op_other->op_type == OP_NULL)
6200 cLOGOP->op_other = cLOGOP->op_other->op_next;
6201 peep(cLOGOP->op_other);
6205 o->op_seq = PL_op_seqmax++;
6206 peep(cLOOP->op_redoop);
6207 peep(cLOOP->op_nextop);
6208 peep(cLOOP->op_lastop);
6214 o->op_seq = PL_op_seqmax++;
6215 peep(cPMOP->op_pmreplstart);
6219 o->op_seq = PL_op_seqmax++;
6220 if (ckWARN(WARN_SYNTAX) && o->op_next
6221 && o->op_next->op_type == OP_NEXTSTATE) {
6222 if (o->op_next->op_sibling &&
6223 o->op_next->op_sibling->op_type != OP_EXIT &&
6224 o->op_next->op_sibling->op_type != OP_WARN &&
6225 o->op_next->op_sibling->op_type != OP_DIE) {
6226 line_t oldline = PL_curcop->cop_line;
6228 PL_curcop->cop_line = ((COP*)o->op_next)->cop_line;
6229 Perl_warner(aTHX_ WARN_SYNTAX, "Statement unlikely to be reached");
6230 Perl_warner(aTHX_ WARN_SYNTAX, "(Maybe you meant system() when you said exec()?)\n");
6231 PL_curcop->cop_line = oldline;
6245 if ((o->op_private & (OPpLVAL_INTRO))
6246 || ((BINOP*)o)->op_last->op_type != OP_CONST)
6248 rop = (UNOP*)((BINOP*)o)->op_first;
6249 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6251 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6252 if (!SvOBJECT(lexname))
6254 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6255 if (!fields || !GvHV(*fields))
6257 svp = &((SVOP*)((BINOP*)o)->op_last)->op_sv;
6258 key = SvPV(*svp, keylen);
6259 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
6261 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" in variable %s of type %s",
6262 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
6264 ind = SvIV(*indsvp);
6266 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6267 rop->op_type = OP_RV2AV;
6268 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6269 o->op_type = OP_AELEM;
6270 o->op_ppaddr = PL_ppaddr[OP_AELEM];
6272 *svp = newSViv(ind);
6278 if (!(o->op_flags & OPf_WANT)
6279 || (o->op_flags & OPf_WANT) == OPf_WANT_LIST)
6283 o->op_seq = PL_op_seqmax++;
6287 if (o->op_next && o->op_next->op_type != OP_LEAVESUBLV) {
6288 o->op_seq = PL_op_seqmax++;
6294 if (last_composite) {
6295 OP *r = last_composite;
6297 while (r->op_sibling)
6300 || (r->op_next->op_type == OP_LIST
6301 && r->op_next->op_next == o))
6303 if (last_composite->op_type == OP_RV2AV)
6304 yyerror("Lvalue subs returning arrays not implemented yet");
6306 yyerror("Lvalue subs returning hashes not implemented yet");
6313 o->op_seq = PL_op_seqmax++;