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)))
720 SvREFCNT_dec(cGVOPo);
722 pad_free(cPADOPo->op_padix);
723 cPADOPo->op_padix = 0;
725 cSVOPo->op_sv = Nullsv;
729 SvREFCNT_dec(cSVOPo->op_sv);
730 cSVOPo->op_sv = Nullsv;
736 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
740 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
741 SvREFCNT_dec(cSVOPo->op_sv);
742 cSVOPo->op_sv = Nullsv;
745 Safefree(cPVOPo->op_pv);
746 cPVOPo->op_pv = Nullch;
750 op_free(cPMOPo->op_pmreplroot);
751 cPMOPo->op_pmreplroot = Nullop;
756 ReREFCNT_dec(cPMOPo->op_pmregexp);
757 cPMOPo->op_pmregexp = (REGEXP*)NULL;
762 pad_free(o->op_targ);
766 S_cop_free(pTHX_ COP* cop)
768 Safefree(cop->cop_label);
769 SvREFCNT_dec(CopFILEGV(cop));
770 if (! specialWARN(cop->cop_warnings))
771 SvREFCNT_dec(cop->cop_warnings);
777 if (o->op_type == OP_NULL)
780 o->op_targ = o->op_type;
781 o->op_type = OP_NULL;
782 o->op_ppaddr = PL_ppaddr[OP_NULL];
785 /* Contextualizers */
787 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
790 Perl_linklist(pTHX_ OP *o)
797 /* establish postfix order */
798 if (cUNOPo->op_first) {
799 o->op_next = LINKLIST(cUNOPo->op_first);
800 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
802 kid->op_next = LINKLIST(kid->op_sibling);
814 Perl_scalarkids(pTHX_ OP *o)
817 if (o && o->op_flags & OPf_KIDS) {
818 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
825 S_scalarboolean(pTHX_ OP *o)
827 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
829 if (ckWARN(WARN_SYNTAX)) {
830 line_t oldline = PL_curcop->cop_line;
832 if (PL_copline != NOLINE)
833 PL_curcop->cop_line = PL_copline;
834 Perl_warner(aTHX_ WARN_SYNTAX, "Found = in conditional, should be ==");
835 PL_curcop->cop_line = oldline;
842 Perl_scalar(pTHX_ OP *o)
846 /* assumes no premature commitment */
847 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
848 || o->op_type == OP_RETURN)
851 if ((o->op_private & OPpTARGET_MY)
852 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
853 return scalar(o); /* As if inside SASSIGN */
855 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
857 switch (o->op_type) {
859 if (o->op_private & OPpREPEAT_DOLIST)
860 null(((LISTOP*)cBINOPo->op_first)->op_first);
861 scalar(cBINOPo->op_first);
866 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
870 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
871 if (!kPMOP->op_pmreplroot)
872 deprecate("implicit split to @_");
880 if (o->op_flags & OPf_KIDS) {
881 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
887 kid = cLISTOPo->op_first;
889 while (kid = kid->op_sibling) {
895 WITH_THR(PL_curcop = &PL_compiling);
900 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
906 WITH_THR(PL_curcop = &PL_compiling);
913 Perl_scalarvoid(pTHX_ OP *o)
920 if (o->op_type == OP_NEXTSTATE
921 || o->op_type == OP_SETSTATE
922 || o->op_type == OP_DBSTATE
923 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
924 || o->op_targ == OP_SETSTATE
925 || o->op_targ == OP_DBSTATE)))
928 PL_curcop = (COP*)o; /* for warning below */
931 /* assumes no premature commitment */
932 want = o->op_flags & OPf_WANT;
933 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
934 || o->op_type == OP_RETURN)
937 if ((o->op_private & OPpTARGET_MY)
938 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
939 return scalar(o); /* As if inside SASSIGN */
941 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
943 switch (o->op_type) {
945 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
949 if (o->op_flags & OPf_STACKED)
953 if (o->op_private == 4)
1001 case OP_GETPRIORITY:
1024 if (!(o->op_private & OPpLVAL_INTRO))
1025 useless = PL_op_desc[o->op_type];
1032 if (!(o->op_private & OPpLVAL_INTRO) &&
1033 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1034 useless = "a variable";
1039 if (cSVOPo->op_private & OPpCONST_STRICT)
1040 no_bareword_allowed(o);
1043 if (ckWARN(WARN_VOID)) {
1044 useless = "a constant";
1045 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1047 else if (SvPOK(sv)) {
1048 if (strnEQ(SvPVX(sv), "di", 2) ||
1049 strnEQ(SvPVX(sv), "ds", 2) ||
1050 strnEQ(SvPVX(sv), "ig", 2))
1055 null(o); /* don't execute or even remember it */
1059 o->op_type = OP_PREINC; /* pre-increment is faster */
1060 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1064 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1065 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1071 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1076 if (o->op_flags & OPf_STACKED)
1084 if (!(o->op_flags & OPf_KIDS))
1093 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1100 /* all requires must return a boolean value */
1101 o->op_flags &= ~OPf_WANT;
1104 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
1105 if (!kPMOP->op_pmreplroot)
1106 deprecate("implicit split to @_");
1112 if (ckWARN(WARN_VOID))
1113 Perl_warner(aTHX_ WARN_VOID, "Useless use of %s in void context", useless);
1119 Perl_listkids(pTHX_ OP *o)
1122 if (o && o->op_flags & OPf_KIDS) {
1123 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1130 Perl_list(pTHX_ OP *o)
1134 /* assumes no premature commitment */
1135 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
1136 || o->op_type == OP_RETURN)
1139 if ((o->op_private & OPpTARGET_MY)
1140 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
1141 return o; /* As if inside SASSIGN */
1143 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1145 switch (o->op_type) {
1148 list(cBINOPo->op_first);
1153 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1161 if (!(o->op_flags & OPf_KIDS))
1163 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1164 list(cBINOPo->op_first);
1165 return gen_constant_list(o);
1172 kid = cLISTOPo->op_first;
1174 while (kid = kid->op_sibling) {
1175 if (kid->op_sibling)
1180 WITH_THR(PL_curcop = &PL_compiling);
1184 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1185 if (kid->op_sibling)
1190 WITH_THR(PL_curcop = &PL_compiling);
1193 /* all requires must return a boolean value */
1194 o->op_flags &= ~OPf_WANT;
1201 Perl_scalarseq(pTHX_ OP *o)
1206 if (o->op_type == OP_LINESEQ ||
1207 o->op_type == OP_SCOPE ||
1208 o->op_type == OP_LEAVE ||
1209 o->op_type == OP_LEAVETRY)
1212 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1213 if (kid->op_sibling) {
1217 PL_curcop = &PL_compiling;
1219 o->op_flags &= ~OPf_PARENS;
1220 if (PL_hints & HINT_BLOCK_SCOPE)
1221 o->op_flags |= OPf_PARENS;
1224 o = newOP(OP_STUB, 0);
1229 S_modkids(pTHX_ OP *o, I32 type)
1232 if (o && o->op_flags & OPf_KIDS) {
1233 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1240 Perl_mod(pTHX_ OP *o, I32 type)
1247 if (!o || PL_error_count)
1250 if ((o->op_private & OPpTARGET_MY)
1251 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
1254 switch (o->op_type) {
1259 if (!(o->op_private & (OPpCONST_ARYBASE)))
1261 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1262 PL_compiling.cop_arybase = (I32)SvIV(((SVOP*)PL_eval_start)->op_sv);
1266 SAVEI32(PL_compiling.cop_arybase);
1267 PL_compiling.cop_arybase = 0;
1269 else if (type == OP_REFGEN)
1272 Perl_croak(aTHX_ "That use of $[ is unsupported");
1275 if (o->op_flags & OPf_PARENS)
1279 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1280 !(o->op_flags & OPf_STACKED)) {
1281 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1282 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1283 assert(cUNOPo->op_first->op_type == OP_NULL);
1284 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1287 else { /* lvalue subroutine call */
1288 o->op_private |= OPpLVAL_INTRO;
1289 if (type == OP_GREPSTART || type == OP_ENTERSUB) {
1290 /* Backward compatibility mode: */
1291 o->op_private |= OPpENTERSUB_INARGS;
1294 else { /* Compile-time error message: */
1295 OP *kid = cUNOPo->op_first;
1299 if (kid->op_type == OP_PUSHMARK)
1301 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1303 "panic: unexpected lvalue entersub "
1304 "args: type/targ %ld:%ld",
1305 (long)kid->op_type,kid->op_targ);
1306 kid = kLISTOP->op_first;
1308 while (kid->op_sibling)
1309 kid = kid->op_sibling;
1310 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1312 if (kid->op_type == OP_METHOD_NAMED
1313 || kid->op_type == OP_METHOD)
1317 if (kid->op_sibling || kid->op_next != kid) {
1318 yyerror("panic: unexpected optree near method call");
1322 NewOp(1101, newop, 1, OP);
1323 newop->op_type = OP_RV2CV;
1324 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1325 newop->op_next = newop;
1326 kid->op_sibling = newop;
1327 newop->op_private |= OPpLVAL_INTRO;
1331 if (kid->op_type != OP_RV2CV)
1333 "panic: unexpected lvalue entersub "
1334 "entry via type/targ %ld:%ld",
1335 (long)kid->op_type,kid->op_targ);
1336 kid->op_private |= OPpLVAL_INTRO;
1337 break; /* Postpone until runtime */
1341 kid = kUNOP->op_first;
1342 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1343 kid = kUNOP->op_first;
1344 if (kid->op_type == OP_NULL)
1346 "Unexpected constant lvalue entersub "
1347 "entry via type/targ %ld:%ld",
1348 (long)kid->op_type,kid->op_targ);
1349 if (kid->op_type != OP_GV) {
1350 /* Restore RV2CV to check lvalueness */
1352 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1353 okid->op_next = kid->op_next;
1354 kid->op_next = okid;
1357 okid->op_next = Nullop;
1358 okid->op_type = OP_RV2CV;
1360 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1361 okid->op_private |= OPpLVAL_INTRO;
1375 /* grep, foreach, subcalls, refgen */
1376 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1378 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1379 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1381 : (o->op_type == OP_ENTERSUB
1382 ? "non-lvalue subroutine call"
1383 : PL_op_desc[o->op_type])),
1384 type ? PL_op_desc[type] : "local"));
1398 case OP_RIGHT_SHIFT:
1407 if (!(o->op_flags & OPf_STACKED))
1413 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1419 if (!type && cUNOPo->op_first->op_type != OP_GV)
1420 Perl_croak(aTHX_ "Can't localize through a reference");
1421 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1422 PL_modcount = 10000;
1423 return o; /* Treat \(@foo) like ordinary list. */
1427 if (scalar_mod_type(o, type))
1429 ref(cUNOPo->op_first, o->op_type);
1438 PL_modcount = 10000;
1441 if (!type && cUNOPo->op_first->op_type != OP_GV)
1442 Perl_croak(aTHX_ "Can't localize through a reference");
1443 ref(cUNOPo->op_first, o->op_type);
1447 PL_hints |= HINT_BLOCK_SCOPE;
1457 PL_modcount = 10000;
1458 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1459 return o; /* Treat \(@foo) like ordinary list. */
1460 if (scalar_mod_type(o, type))
1466 Perl_croak(aTHX_ "Can't localize lexical variable %s",
1467 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
1472 PL_modcount++; /* XXX ??? */
1474 #endif /* USE_THREADS */
1480 if (type != OP_SASSIGN)
1484 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1490 pad_free(o->op_targ);
1491 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1492 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1493 if (o->op_flags & OPf_KIDS)
1494 mod(cBINOPo->op_first->op_sibling, type);
1499 ref(cBINOPo->op_first, o->op_type);
1500 if (type == OP_ENTERSUB &&
1501 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1502 o->op_private |= OPpLVAL_DEFER;
1509 if (o->op_flags & OPf_KIDS)
1510 mod(cLISTOPo->op_last, type);
1514 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1516 else if (!(o->op_flags & OPf_KIDS))
1518 if (o->op_targ != OP_LIST) {
1519 mod(cBINOPo->op_first, type);
1524 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1528 o->op_flags |= OPf_MOD;
1530 if (type == OP_AASSIGN || type == OP_SASSIGN)
1531 o->op_flags |= OPf_SPECIAL|OPf_REF;
1533 o->op_private |= OPpLVAL_INTRO;
1534 o->op_flags &= ~OPf_SPECIAL;
1535 PL_hints |= HINT_BLOCK_SCOPE;
1537 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1538 o->op_flags |= OPf_REF;
1543 S_scalar_mod_type(pTHX_ OP *o, I32 type)
1547 if (o->op_type == OP_RV2GV)
1571 case OP_RIGHT_SHIFT:
1590 S_is_handle_constructor(pTHX_ OP *o, I32 argnum)
1592 switch (o->op_type) {
1600 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1613 Perl_refkids(pTHX_ OP *o, I32 type)
1616 if (o && o->op_flags & OPf_KIDS) {
1617 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1624 Perl_ref(pTHX_ OP *o, I32 type)
1628 if (!o || PL_error_count)
1631 switch (o->op_type) {
1633 if ((type == OP_DEFINED || type == OP_LOCK) &&
1634 !(o->op_flags & OPf_STACKED)) {
1635 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1636 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1637 assert(cUNOPo->op_first->op_type == OP_NULL);
1638 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1639 o->op_flags |= OPf_SPECIAL;
1644 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1648 if (type == OP_DEFINED)
1649 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1650 ref(cUNOPo->op_first, o->op_type);
1653 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1654 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1655 : type == OP_RV2HV ? OPpDEREF_HV
1657 o->op_flags |= OPf_MOD;
1662 o->op_flags |= OPf_MOD; /* XXX ??? */
1667 o->op_flags |= OPf_REF;
1670 if (type == OP_DEFINED)
1671 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1672 ref(cUNOPo->op_first, o->op_type);
1677 o->op_flags |= OPf_REF;
1682 if (!(o->op_flags & OPf_KIDS))
1684 ref(cBINOPo->op_first, type);
1688 ref(cBINOPo->op_first, o->op_type);
1689 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1690 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1691 : type == OP_RV2HV ? OPpDEREF_HV
1693 o->op_flags |= OPf_MOD;
1701 if (!(o->op_flags & OPf_KIDS))
1703 ref(cLISTOPo->op_last, type);
1713 S_dup_attrlist(pTHX_ OP *o)
1717 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1718 * where the first kid is OP_PUSHMARK and the remaining ones
1719 * are OP_CONST. We need to push the OP_CONST values.
1721 if (o->op_type == OP_CONST)
1722 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc(cSVOPo->op_sv));
1724 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1725 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1726 if (o->op_type == OP_CONST)
1727 rop = append_elem(OP_LIST, rop,
1728 newSVOP(OP_CONST, o->op_flags,
1729 SvREFCNT_inc(cSVOPo->op_sv)));
1736 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
1738 OP *modname; /* for 'use' */
1741 /* fake up C<use attributes $pkg,$rv,@attrs> */
1742 ENTER; /* need to protect against side-effects of 'use' */
1744 if (stash && HvNAME(stash))
1745 stashsv = newSVpv(HvNAME(stash), 0);
1747 stashsv = &PL_sv_no;
1748 #define ATTRSMODULE "attributes"
1749 modname = newSVOP(OP_CONST, 0,
1750 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1));
1751 modname->op_private |= OPpCONST_BARE;
1752 /* that flag is required to make 'use' work right */
1753 utilize(1, start_subparse(FALSE, 0),
1754 Nullop, /* version */
1756 prepend_elem(OP_LIST,
1757 newSVOP(OP_CONST, 0, stashsv),
1758 prepend_elem(OP_LIST,
1759 newSVOP(OP_CONST, 0, newRV(target)),
1760 dup_attrlist(attrs))));
1765 S_my_kid(pTHX_ OP *o, OP *attrs)
1770 if (!o || PL_error_count)
1774 if (type == OP_LIST) {
1775 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1777 } else if (type == OP_UNDEF) {
1779 } else if (type == OP_RV2SV || /* "our" declaration */
1781 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
1783 } else if (type != OP_PADSV &&
1786 type != OP_PUSHMARK)
1788 yyerror(Perl_form(aTHX_ "Can't declare %s in my", PL_op_desc[o->op_type]));
1791 else if (attrs && type != OP_PUSHMARK) {
1796 /* check for C<my Dog $spot> when deciding package */
1797 namesvp = av_fetch(PL_comppad_name, o->op_targ, FALSE);
1798 if (namesvp && *namesvp && SvOBJECT(*namesvp) && HvNAME(SvSTASH(*namesvp)))
1799 stash = SvSTASH(*namesvp);
1801 stash = PL_curstash;
1802 padsv = PAD_SV(o->op_targ);
1803 apply_attrs(stash, padsv, attrs);
1805 o->op_flags |= OPf_MOD;
1806 o->op_private |= OPpLVAL_INTRO;
1811 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1813 if (o->op_flags & OPf_PARENS)
1816 PL_in_my_stash = Nullhv;
1819 return my_kid(o, attrs);
1823 Perl_my(pTHX_ OP *o)
1825 return my_kid(o, Nullop);
1829 Perl_sawparens(pTHX_ OP *o)
1832 o->op_flags |= OPf_PARENS;
1837 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
1842 if (ckWARN(WARN_UNSAFE) &&
1843 (left->op_type == OP_RV2AV ||
1844 left->op_type == OP_RV2HV ||
1845 left->op_type == OP_PADAV ||
1846 left->op_type == OP_PADHV)) {
1847 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
1848 right->op_type == OP_TRANS)
1849 ? right->op_type : OP_MATCH];
1850 const char *sample = ((left->op_type == OP_RV2AV ||
1851 left->op_type == OP_PADAV)
1852 ? "@array" : "%hash");
1853 Perl_warner(aTHX_ WARN_UNSAFE,
1854 "Applying %s to %s will act on scalar(%s)",
1855 desc, sample, sample);
1858 if (right->op_type == OP_MATCH ||
1859 right->op_type == OP_SUBST ||
1860 right->op_type == OP_TRANS) {
1861 right->op_flags |= OPf_STACKED;
1862 if (right->op_type != OP_MATCH)
1863 left = mod(left, right->op_type);
1864 if (right->op_type == OP_TRANS)
1865 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1867 o = prepend_elem(right->op_type, scalar(left), right);
1869 return newUNOP(OP_NOT, 0, scalar(o));
1873 return bind_match(type, left,
1874 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1878 Perl_invert(pTHX_ OP *o)
1882 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1883 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1887 Perl_scope(pTHX_ OP *o)
1890 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
1891 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1892 o->op_type = OP_LEAVE;
1893 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
1896 if (o->op_type == OP_LINESEQ) {
1898 o->op_type = OP_SCOPE;
1899 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
1900 kid = ((LISTOP*)o)->op_first;
1901 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
1905 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1912 Perl_save_hints(pTHX)
1915 SAVESPTR(GvHV(PL_hintgv));
1916 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
1917 SAVEFREESV(GvHV(PL_hintgv));
1921 Perl_block_start(pTHX_ int full)
1924 int retval = PL_savestack_ix;
1926 SAVEI32(PL_comppad_name_floor);
1928 if ((PL_comppad_name_fill = AvFILLp(PL_comppad_name)) > 0)
1929 PL_comppad_name_floor = PL_comppad_name_fill;
1931 PL_comppad_name_floor = 0;
1933 SAVEI32(PL_min_intro_pending);
1934 SAVEI32(PL_max_intro_pending);
1935 PL_min_intro_pending = 0;
1936 SAVEI32(PL_comppad_name_fill);
1937 SAVEI32(PL_padix_floor);
1938 PL_padix_floor = PL_padix;
1939 PL_pad_reset_pending = FALSE;
1941 PL_hints &= ~HINT_BLOCK_SCOPE;
1942 SAVEPPTR(PL_compiling.cop_warnings);
1943 if (! specialWARN(PL_compiling.cop_warnings)) {
1944 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1945 SAVEFREESV(PL_compiling.cop_warnings) ;
1953 Perl_block_end(pTHX_ I32 floor, OP *seq)
1956 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
1957 OP* retval = scalarseq(seq);
1959 PL_pad_reset_pending = FALSE;
1960 PL_compiling.op_private = PL_hints;
1962 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1963 pad_leavemy(PL_comppad_name_fill);
1972 OP *o = newOP(OP_THREADSV, 0);
1973 o->op_targ = find_threadsv("_");
1976 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
1977 #endif /* USE_THREADS */
1981 Perl_newPROG(pTHX_ OP *o)
1987 PL_eval_root = newUNOP(OP_LEAVEEVAL,
1988 ((PL_in_eval & EVAL_KEEPERR)
1989 ? OPf_SPECIAL : 0), o);
1990 PL_eval_start = linklist(PL_eval_root);
1991 PL_eval_root->op_private |= OPpREFCOUNTED;
1992 OpREFCNT_set(PL_eval_root, 1);
1993 PL_eval_root->op_next = 0;
1994 peep(PL_eval_start);
1999 PL_main_root = scope(sawparens(scalarvoid(o)));
2000 PL_curcop = &PL_compiling;
2001 PL_main_start = LINKLIST(PL_main_root);
2002 PL_main_root->op_private |= OPpREFCOUNTED;
2003 OpREFCNT_set(PL_main_root, 1);
2004 PL_main_root->op_next = 0;
2005 peep(PL_main_start);
2008 /* Register with debugger */
2010 CV *cv = get_cv("DB::postponed", FALSE);
2014 XPUSHs((SV*)CopFILEGV(&PL_compiling));
2016 call_sv((SV*)cv, G_DISCARD);
2023 Perl_localize(pTHX_ OP *o, I32 lex)
2025 if (o->op_flags & OPf_PARENS)
2029 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
2031 for (s = PL_bufptr; *s && (isALNUM(*s) || (*s & 0x80) || strchr("@$%, ",*s)); s++) ;
2032 if (*s == ';' || *s == '=')
2033 Perl_warner(aTHX_ WARN_PARENTHESIS, "Parentheses missing around \"%s\" list",
2034 lex ? "my" : "local");
2038 PL_in_my_stash = Nullhv;
2042 return mod(o, OP_NULL); /* a bit kludgey */
2046 Perl_jmaybe(pTHX_ OP *o)
2048 if (o->op_type == OP_LIST) {
2051 o2 = newOP(OP_THREADSV, 0);
2052 o2->op_targ = find_threadsv(";");
2054 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
2055 #endif /* USE_THREADS */
2056 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2062 Perl_fold_constants(pTHX_ register OP *o)
2066 I32 type = o->op_type;
2069 if (PL_opargs[type] & OA_RETSCALAR)
2071 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2072 o->op_targ = pad_alloc(type, SVs_PADTMP);
2074 /* integerize op, unless it happens to be C<-foo>.
2075 * XXX should pp_i_negate() do magic string negation instead? */
2076 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2077 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2078 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2080 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2083 if (!(PL_opargs[type] & OA_FOLDCONST))
2088 /* XXX might want a ck_negate() for this */
2089 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2102 if (o->op_private & OPpLOCALE)
2107 goto nope; /* Don't try to run w/ errors */
2109 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2110 if ((curop->op_type != OP_CONST ||
2111 (curop->op_private & OPpCONST_BARE)) &&
2112 curop->op_type != OP_LIST &&
2113 curop->op_type != OP_SCALAR &&
2114 curop->op_type != OP_NULL &&
2115 curop->op_type != OP_PUSHMARK)
2121 curop = LINKLIST(o);
2125 sv = *(PL_stack_sp--);
2126 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
2127 pad_swipe(o->op_targ);
2128 else if (SvTEMP(sv)) { /* grab mortal temp? */
2129 (void)SvREFCNT_inc(sv);
2133 if (type == OP_RV2GV)
2134 return newGVOP(OP_GV, 0, (GV*)sv);
2136 /* try to smush double to int, but don't smush -2.0 to -2 */
2137 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
2141 if ((NV)iv == SvNV(sv)) {
2146 SvIOK_off(sv); /* undo SvIV() damage */
2148 return newSVOP(OP_CONST, 0, sv);
2152 if (!(PL_opargs[type] & OA_OTHERINT))
2155 if (!(PL_hints & HINT_INTEGER)) {
2156 if (type == OP_MODULO
2157 || type == OP_DIVIDE
2158 || !(o->op_flags & OPf_KIDS))
2163 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
2164 if (curop->op_type == OP_CONST) {
2165 if (SvIOK(((SVOP*)curop)->op_sv))
2169 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
2173 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
2180 Perl_gen_constant_list(pTHX_ register OP *o)
2184 I32 oldtmps_floor = PL_tmps_floor;
2188 return o; /* Don't attempt to run with errors */
2190 PL_op = curop = LINKLIST(o);
2196 PL_tmps_floor = oldtmps_floor;
2198 o->op_type = OP_RV2AV;
2199 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2200 curop = ((UNOP*)o)->op_first;
2201 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
2208 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2213 if (!o || o->op_type != OP_LIST)
2214 o = newLISTOP(OP_LIST, 0, o, Nullop);
2216 o->op_flags &= ~OPf_WANT;
2218 if (!(PL_opargs[type] & OA_MARK))
2219 null(cLISTOPo->op_first);
2222 o->op_ppaddr = PL_ppaddr[type];
2223 o->op_flags |= flags;
2225 o = CHECKOP(type, o);
2226 if (o->op_type != type)
2229 if (cLISTOPo->op_children < 7) {
2230 /* XXX do we really need to do this if we're done appending?? */
2231 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2233 cLISTOPo->op_last = last; /* in case check substituted last arg */
2236 return fold_constants(o);
2239 /* List constructors */
2242 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2250 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
2251 return newLISTOP(type, 0, first, last);
2253 if (first->op_flags & OPf_KIDS)
2254 ((LISTOP*)first)->op_last->op_sibling = last;
2256 first->op_flags |= OPf_KIDS;
2257 ((LISTOP*)first)->op_first = last;
2259 ((LISTOP*)first)->op_last = last;
2260 ((LISTOP*)first)->op_children++;
2265 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2273 if (first->op_type != type)
2274 return prepend_elem(type, (OP*)first, (OP*)last);
2276 if (last->op_type != type)
2277 return append_elem(type, (OP*)first, (OP*)last);
2279 first->op_last->op_sibling = last->op_first;
2280 first->op_last = last->op_last;
2281 first->op_children += last->op_children;
2282 if (first->op_children)
2283 first->op_flags |= OPf_KIDS;
2285 #ifdef PL_OP_SLAB_ALLOC
2293 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2301 if (last->op_type == type) {
2302 if (type == OP_LIST) { /* already a PUSHMARK there */
2303 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2304 ((LISTOP*)last)->op_first->op_sibling = first;
2307 if (!(last->op_flags & OPf_KIDS)) {
2308 ((LISTOP*)last)->op_last = first;
2309 last->op_flags |= OPf_KIDS;
2311 first->op_sibling = ((LISTOP*)last)->op_first;
2312 ((LISTOP*)last)->op_first = first;
2314 ((LISTOP*)last)->op_children++;
2318 return newLISTOP(type, 0, first, last);
2324 Perl_newNULLLIST(pTHX)
2326 return newOP(OP_STUB, 0);
2330 Perl_force_list(pTHX_ OP *o)
2332 if (!o || o->op_type != OP_LIST)
2333 o = newLISTOP(OP_LIST, 0, o, Nullop);
2339 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2343 NewOp(1101, listop, 1, LISTOP);
2345 listop->op_type = type;
2346 listop->op_ppaddr = PL_ppaddr[type];
2347 listop->op_children = (first != 0) + (last != 0);
2348 listop->op_flags = flags;
2352 else if (!first && last)
2355 first->op_sibling = last;
2356 listop->op_first = first;
2357 listop->op_last = last;
2358 if (type == OP_LIST) {
2360 pushop = newOP(OP_PUSHMARK, 0);
2361 pushop->op_sibling = first;
2362 listop->op_first = pushop;
2363 listop->op_flags |= OPf_KIDS;
2365 listop->op_last = pushop;
2367 else if (listop->op_children)
2368 listop->op_flags |= OPf_KIDS;
2374 Perl_newOP(pTHX_ I32 type, I32 flags)
2377 NewOp(1101, o, 1, OP);
2379 o->op_ppaddr = PL_ppaddr[type];
2380 o->op_flags = flags;
2383 o->op_private = 0 + (flags >> 8);
2384 if (PL_opargs[type] & OA_RETSCALAR)
2386 if (PL_opargs[type] & OA_TARGET)
2387 o->op_targ = pad_alloc(type, SVs_PADTMP);
2388 return CHECKOP(type, o);
2392 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
2397 first = newOP(OP_STUB, 0);
2398 if (PL_opargs[type] & OA_MARK)
2399 first = force_list(first);
2401 NewOp(1101, unop, 1, UNOP);
2402 unop->op_type = type;
2403 unop->op_ppaddr = PL_ppaddr[type];
2404 unop->op_first = first;
2405 unop->op_flags = flags | OPf_KIDS;
2406 unop->op_private = 1 | (flags >> 8);
2407 unop = (UNOP*) CHECKOP(type, unop);
2411 return fold_constants((OP *) unop);
2415 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2418 NewOp(1101, binop, 1, BINOP);
2421 first = newOP(OP_NULL, 0);
2423 binop->op_type = type;
2424 binop->op_ppaddr = PL_ppaddr[type];
2425 binop->op_first = first;
2426 binop->op_flags = flags | OPf_KIDS;
2429 binop->op_private = 1 | (flags >> 8);
2432 binop->op_private = 2 | (flags >> 8);
2433 first->op_sibling = last;
2436 binop = (BINOP*)CHECKOP(type, binop);
2437 if (binop->op_next || binop->op_type != type)
2440 binop->op_last = binop->op_first->op_sibling;
2442 return fold_constants((OP *)binop);
2446 utf8compare(const void *a, const void *b)
2449 for (i = 0; i < 10; i++) {
2450 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2452 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2459 Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
2461 SV *tstr = ((SVOP*)expr)->op_sv;
2462 SV *rstr = ((SVOP*)repl)->op_sv;
2465 register U8 *t = (U8*)SvPV(tstr, tlen);
2466 register U8 *r = (U8*)SvPV(rstr, rlen);
2472 register short *tbl;
2474 complement = o->op_private & OPpTRANS_COMPLEMENT;
2475 del = o->op_private & OPpTRANS_DELETE;
2476 squash = o->op_private & OPpTRANS_SQUASH;
2478 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2479 SV* listsv = newSVpvn("# comment\n",10);
2481 U8* tend = t + tlen;
2482 U8* rend = r + rlen;
2498 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2499 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2505 New(1109, cp, tlen, U8*);
2507 transv = newSVpvn("",0);
2516 qsort(cp, i, sizeof(U8*), utf8compare);
2517 for (j = 0; j < i; j++) {
2519 UV val = utf8_to_uv(s, &ulen);
2521 diff = val - nextmin;
2523 t = uv_to_utf8(tmpbuf,nextmin);
2524 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2526 t = uv_to_utf8(tmpbuf, val - 1);
2527 sv_catpvn(transv, "\377", 1);
2528 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2532 val = utf8_to_uv(s+1, &ulen);
2536 t = uv_to_utf8(tmpbuf,nextmin);
2537 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2538 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2539 sv_catpvn(transv, "\377", 1);
2540 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2541 t = (U8*)SvPVX(transv);
2542 tlen = SvCUR(transv);
2545 else if (!rlen && !del) {
2546 r = t; rlen = tlen; rend = tend;
2549 if (to_utf && from_utf) { /* only counting characters */
2550 if (t == r || (tlen == rlen && memEQ(t, r, tlen)))
2551 o->op_private |= OPpTRANS_IDENTICAL;
2553 else { /* straight latin-1 translation */
2554 if (tlen == 4 && memEQ(t, "\0\377\303\277", 4) &&
2555 rlen == 4 && memEQ(r, "\0\377\303\277", 4))
2556 o->op_private |= OPpTRANS_IDENTICAL;
2560 while (t < tend || tfirst <= tlast) {
2561 /* see if we need more "t" chars */
2562 if (tfirst > tlast) {
2563 tfirst = (I32)utf8_to_uv(t, &ulen);
2565 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
2566 tlast = (I32)utf8_to_uv(++t, &ulen);
2573 /* now see if we need more "r" chars */
2574 if (rfirst > rlast) {
2576 rfirst = (I32)utf8_to_uv(r, &ulen);
2578 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
2579 rlast = (I32)utf8_to_uv(++r, &ulen);
2588 rfirst = rlast = 0xffffffff;
2592 /* now see which range will peter our first, if either. */
2593 tdiff = tlast - tfirst;
2594 rdiff = rlast - rfirst;
2601 if (rfirst == 0xffffffff) {
2602 diff = tdiff; /* oops, pretend rdiff is infinite */
2604 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\tXXXX\n", tfirst, tlast);
2606 Perl_sv_catpvf(aTHX_ listsv, "%04x\t\tXXXX\n", tfirst);
2610 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\t%04x\n", tfirst, tfirst + diff, rfirst);
2612 Perl_sv_catpvf(aTHX_ listsv, "%04x\t\t%04x\n", tfirst, rfirst);
2614 if (rfirst + diff > max)
2615 max = rfirst + diff;
2620 else if (rfirst <= 0x800)
2621 grows |= (tfirst < 0x80);
2622 else if (rfirst <= 0x10000)
2623 grows |= (tfirst < 0x800);
2624 else if (rfirst <= 0x200000)
2625 grows |= (tfirst < 0x10000);
2626 else if (rfirst <= 0x4000000)
2627 grows |= (tfirst < 0x200000);
2628 else if (rfirst <= 0x80000000)
2629 grows |= (tfirst < 0x4000000);
2641 else if (max > 0xff)
2646 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2647 SvREFCNT_dec(listsv);
2649 SvREFCNT_dec(transv);
2651 if (!del && havefinal)
2652 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5, newSViv((IV)final), 0);
2654 if (grows && to_utf)
2655 o->op_private |= OPpTRANS_GROWS;
2662 tbl = (short*)cPVOPo->op_pv;
2664 Zero(tbl, 256, short);
2665 for (i = 0; i < tlen; i++)
2667 for (i = 0, j = 0; i < 256; i++) {
2683 if (!rlen && !del) {
2686 o->op_private |= OPpTRANS_IDENTICAL;
2688 for (i = 0; i < 256; i++)
2690 for (i = 0, j = 0; i < tlen; i++,j++) {
2693 if (tbl[t[i]] == -1)
2699 if (tbl[t[i]] == -1)
2710 Perl_newPMOP(pTHX_ I32 type, I32 flags)
2715 NewOp(1101, pmop, 1, PMOP);
2716 pmop->op_type = type;
2717 pmop->op_ppaddr = PL_ppaddr[type];
2718 pmop->op_flags = flags;
2719 pmop->op_private = 0 | (flags >> 8);
2721 if (PL_hints & HINT_RE_TAINT)
2722 pmop->op_pmpermflags |= PMf_RETAINT;
2723 if (PL_hints & HINT_LOCALE)
2724 pmop->op_pmpermflags |= PMf_LOCALE;
2725 pmop->op_pmflags = pmop->op_pmpermflags;
2727 /* link into pm list */
2728 if (type != OP_TRANS && PL_curstash) {
2729 pmop->op_pmnext = HvPMROOT(PL_curstash);
2730 HvPMROOT(PL_curstash) = pmop;
2737 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
2742 I32 repl_has_vars = 0;
2744 if (o->op_type == OP_TRANS)
2745 return pmtrans(o, expr, repl);
2747 PL_hints |= HINT_BLOCK_SCOPE;
2750 if (expr->op_type == OP_CONST) {
2752 SV *pat = ((SVOP*)expr)->op_sv;
2753 char *p = SvPV(pat, plen);
2754 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2755 sv_setpvn(pat, "\\s+", 3);
2756 p = SvPV(pat, plen);
2757 pm->op_pmflags |= PMf_SKIPWHITE;
2759 pm->op_pmregexp = CALLREGCOMP(aTHX_ p, p + plen, pm);
2760 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2761 pm->op_pmflags |= PMf_WHITE;
2765 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2766 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2768 : OP_REGCMAYBE),0,expr);
2770 NewOp(1101, rcop, 1, LOGOP);
2771 rcop->op_type = OP_REGCOMP;
2772 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2773 rcop->op_first = scalar(expr);
2774 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2775 ? (OPf_SPECIAL | OPf_KIDS)
2777 rcop->op_private = 1;
2780 /* establish postfix order */
2781 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2783 rcop->op_next = expr;
2784 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2787 rcop->op_next = LINKLIST(expr);
2788 expr->op_next = (OP*)rcop;
2791 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2796 if (pm->op_pmflags & PMf_EVAL) {
2798 if (PL_curcop->cop_line < PL_multi_end)
2799 PL_curcop->cop_line = PL_multi_end;
2802 else if (repl->op_type == OP_THREADSV
2803 && strchr("&`'123456789+",
2804 PL_threadsv_names[repl->op_targ]))
2808 #endif /* USE_THREADS */
2809 else if (repl->op_type == OP_CONST)
2813 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2814 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2816 if (curop->op_type == OP_THREADSV) {
2818 if (strchr("&`'123456789+", curop->op_private))
2822 if (curop->op_type == OP_GV) {
2823 GV *gv = cGVOPx(curop);
2825 if (strchr("&`'123456789+", *GvENAME(gv)))
2828 #endif /* USE_THREADS */
2829 else if (curop->op_type == OP_RV2CV)
2831 else if (curop->op_type == OP_RV2SV ||
2832 curop->op_type == OP_RV2AV ||
2833 curop->op_type == OP_RV2HV ||
2834 curop->op_type == OP_RV2GV) {
2835 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2838 else if (curop->op_type == OP_PADSV ||
2839 curop->op_type == OP_PADAV ||
2840 curop->op_type == OP_PADHV ||
2841 curop->op_type == OP_PADANY) {
2844 else if (curop->op_type == OP_PUSHRE)
2845 ; /* Okay here, dangerous in newASSIGNOP */
2854 && (!pm->op_pmregexp
2855 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
2856 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2857 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2858 prepend_elem(o->op_type, scalar(repl), o);
2861 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
2862 pm->op_pmflags |= PMf_MAYBE_CONST;
2863 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2865 NewOp(1101, rcop, 1, LOGOP);
2866 rcop->op_type = OP_SUBSTCONT;
2867 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
2868 rcop->op_first = scalar(repl);
2869 rcop->op_flags |= OPf_KIDS;
2870 rcop->op_private = 1;
2873 /* establish postfix order */
2874 rcop->op_next = LINKLIST(repl);
2875 repl->op_next = (OP*)rcop;
2877 pm->op_pmreplroot = scalar((OP*)rcop);
2878 pm->op_pmreplstart = LINKLIST(rcop);
2887 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
2890 NewOp(1101, svop, 1, SVOP);
2891 svop->op_type = type;
2892 svop->op_ppaddr = PL_ppaddr[type];
2894 svop->op_next = (OP*)svop;
2895 svop->op_flags = flags;
2896 if (PL_opargs[type] & OA_RETSCALAR)
2898 if (PL_opargs[type] & OA_TARGET)
2899 svop->op_targ = pad_alloc(type, SVs_PADTMP);
2900 return CHECKOP(type, svop);
2904 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
2907 NewOp(1101, padop, 1, PADOP);
2908 padop->op_type = type;
2909 padop->op_ppaddr = PL_ppaddr[type];
2910 padop->op_padix = pad_alloc(type, SVs_PADTMP);
2911 PL_curpad[padop->op_padix] = sv;
2912 padop->op_next = (OP*)padop;
2913 padop->op_flags = flags;
2914 if (PL_opargs[type] & OA_RETSCALAR)
2916 if (PL_opargs[type] & OA_TARGET)
2917 padop->op_targ = pad_alloc(type, SVs_PADTMP);
2918 return CHECKOP(type, padop);
2922 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
2926 return newPADOP(type, flags, SvREFCNT_inc(gv));
2928 return newSVOP(type, flags, SvREFCNT_inc(gv));
2933 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
2936 NewOp(1101, pvop, 1, PVOP);
2937 pvop->op_type = type;
2938 pvop->op_ppaddr = PL_ppaddr[type];
2940 pvop->op_next = (OP*)pvop;
2941 pvop->op_flags = flags;
2942 if (PL_opargs[type] & OA_RETSCALAR)
2944 if (PL_opargs[type] & OA_TARGET)
2945 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2946 return CHECKOP(type, pvop);
2950 Perl_package(pTHX_ OP *o)
2955 save_hptr(&PL_curstash);
2956 save_item(PL_curstname);
2961 name = SvPV(sv, len);
2962 PL_curstash = gv_stashpvn(name,len,TRUE);
2963 sv_setpvn(PL_curstname, name, len);
2967 sv_setpv(PL_curstname,"<none>");
2968 PL_curstash = Nullhv;
2970 PL_hints |= HINT_BLOCK_SCOPE;
2971 PL_copline = NOLINE;
2976 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg)
2985 if (id->op_type != OP_CONST)
2986 Perl_croak(aTHX_ "Module name must be constant");
2990 if(version != Nullop) {
2991 SV *vesv = ((SVOP*)version)->op_sv;
2993 if (arg == Nullop && !SvNIOK(vesv)) {
2999 if (version->op_type != OP_CONST || !SvNIOK(vesv))
3000 Perl_croak(aTHX_ "Version number must be constant number");
3002 /* Make copy of id so we don't free it twice */
3003 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3005 /* Fake up a method call to VERSION */
3006 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3007 append_elem(OP_LIST,
3008 prepend_elem(OP_LIST, pack, list(version)),
3009 newSVOP(OP_METHOD_NAMED, 0,
3010 newSVpvn("VERSION", 7))));
3014 /* Fake up an import/unimport */
3015 if (arg && arg->op_type == OP_STUB)
3016 imop = arg; /* no import on explicit () */
3017 else if(SvNIOK(((SVOP*)id)->op_sv)) {
3018 imop = Nullop; /* use 5.0; */
3021 /* Make copy of id so we don't free it twice */
3022 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3023 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3024 append_elem(OP_LIST,
3025 prepend_elem(OP_LIST, pack, list(arg)),
3026 newSVOP(OP_METHOD_NAMED, 0,
3027 aver ? newSVpvn("import", 6)
3028 : newSVpvn("unimport", 8))));
3031 /* Fake up a require, handle override, if any */
3032 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
3033 if (!(gv && GvIMPORTED_CV(gv)))
3034 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
3036 if (gv && GvIMPORTED_CV(gv)) {
3037 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3038 append_elem(OP_LIST, id,
3039 scalar(newUNOP(OP_RV2CV, 0,
3044 rqop = newUNOP(OP_REQUIRE, 0, id);
3047 /* Fake up the BEGIN {}, which does its thing immediately. */
3049 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
3052 append_elem(OP_LINESEQ,
3053 append_elem(OP_LINESEQ,
3054 newSTATEOP(0, Nullch, rqop),
3055 newSTATEOP(0, Nullch, veop)),
3056 newSTATEOP(0, Nullch, imop) ));
3058 PL_hints |= HINT_BLOCK_SCOPE;
3059 PL_copline = NOLINE;
3064 Perl_dofile(pTHX_ OP *term)
3069 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
3070 if (!(gv && GvIMPORTED_CV(gv)))
3071 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
3073 if (gv && GvIMPORTED_CV(gv)) {
3074 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3075 append_elem(OP_LIST, term,
3076 scalar(newUNOP(OP_RV2CV, 0,
3081 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3087 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
3089 return newBINOP(OP_LSLICE, flags,
3090 list(force_list(subscript)),
3091 list(force_list(listval)) );
3095 S_list_assignment(pTHX_ register OP *o)
3100 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3101 o = cUNOPo->op_first;
3103 if (o->op_type == OP_COND_EXPR) {
3104 I32 t = list_assignment(cLOGOPo->op_first->op_sibling);
3105 I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
3110 yyerror("Assignment to both a list and a scalar");
3114 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3115 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3116 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
3119 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
3122 if (o->op_type == OP_RV2SV)
3129 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
3134 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
3135 return newLOGOP(optype, 0,
3136 mod(scalar(left), optype),
3137 newUNOP(OP_SASSIGN, 0, scalar(right)));
3140 return newBINOP(optype, OPf_STACKED,
3141 mod(scalar(left), optype), scalar(right));
3145 if (list_assignment(left)) {
3148 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3149 left = mod(left, OP_AASSIGN);
3157 o = newBINOP(OP_AASSIGN, flags,
3158 list(force_list(right)),
3159 list(force_list(left)) );
3160 o->op_private = 0 | (flags >> 8);
3161 if (!(left->op_private & OPpLVAL_INTRO)) {
3165 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3166 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3167 if (curop->op_type == OP_GV) {
3168 GV *gv = cGVOPx(curop);
3169 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3171 SvCUR(gv) = PL_generation;
3173 else if (curop->op_type == OP_PADSV ||
3174 curop->op_type == OP_PADAV ||
3175 curop->op_type == OP_PADHV ||
3176 curop->op_type == OP_PADANY) {
3177 SV **svp = AvARRAY(PL_comppad_name);
3178 SV *sv = svp[curop->op_targ];
3179 if (SvCUR(sv) == PL_generation)
3181 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
3183 else if (curop->op_type == OP_RV2CV)
3185 else if (curop->op_type == OP_RV2SV ||
3186 curop->op_type == OP_RV2AV ||
3187 curop->op_type == OP_RV2HV ||
3188 curop->op_type == OP_RV2GV) {
3189 if (lastop->op_type != OP_GV) /* funny deref? */
3192 else if (curop->op_type == OP_PUSHRE) {
3193 if (((PMOP*)curop)->op_pmreplroot) {
3194 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3195 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3197 SvCUR(gv) = PL_generation;
3206 o->op_private = OPpASSIGN_COMMON;
3208 if (right && right->op_type == OP_SPLIT) {
3210 if ((tmpop = ((LISTOP*)right)->op_first) &&
3211 tmpop->op_type == OP_PUSHRE)
3213 PMOP *pm = (PMOP*)tmpop;
3214 if (left->op_type == OP_RV2AV &&
3215 !(left->op_private & OPpLVAL_INTRO) &&
3216 !(o->op_private & OPpASSIGN_COMMON) )
3218 tmpop = ((UNOP*)left)->op_first;
3219 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
3220 pm->op_pmreplroot = (OP*)((SVOP*)tmpop)->op_sv; /* XXXXXX */
3221 pm->op_pmflags |= PMf_ONCE;
3222 tmpop = cUNOPo->op_first; /* to list (nulled) */
3223 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3224 tmpop->op_sibling = Nullop; /* don't free split */
3225 right->op_next = tmpop->op_next; /* fix starting loc */
3226 op_free(o); /* blow off assign */
3227 right->op_flags &= ~OPf_WANT;
3228 /* "I don't know and I don't care." */
3233 if (PL_modcount < 10000 &&
3234 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3236 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3238 sv_setiv(sv, PL_modcount+1);
3246 right = newOP(OP_UNDEF, 0);
3247 if (right->op_type == OP_READLINE) {
3248 right->op_flags |= OPf_STACKED;
3249 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3252 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3253 o = newBINOP(OP_SASSIGN, flags,
3254 scalar(right), mod(scalar(left), OP_SASSIGN) );
3266 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3269 U32 seq = intro_my();
3272 NewOp(1101, cop, 1, COP);
3273 if (PERLDB_LINE && PL_curcop->cop_line && PL_curstash != PL_debstash) {
3274 cop->op_type = OP_DBSTATE;
3275 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3278 cop->op_type = OP_NEXTSTATE;
3279 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3281 cop->op_flags = flags;
3282 cop->op_private = (PL_hints & HINT_UTF8);
3284 cop->op_private |= NATIVE_HINTS;
3286 PL_compiling.op_private = cop->op_private;
3287 cop->op_next = (OP*)cop;
3290 cop->cop_label = label;
3291 PL_hints |= HINT_BLOCK_SCOPE;
3294 cop->cop_arybase = PL_curcop->cop_arybase;
3295 if (specialWARN(PL_curcop->cop_warnings))
3296 cop->cop_warnings = PL_curcop->cop_warnings ;
3298 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3301 if (PL_copline == NOLINE)
3302 cop->cop_line = PL_curcop->cop_line;
3304 cop->cop_line = PL_copline;
3305 PL_copline = NOLINE;
3307 CopFILEGV_set(cop, (GV*)SvREFCNT_inc(CopFILEGV(PL_curcop)));
3308 cop->cop_stash = PL_curstash;
3310 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3311 SV **svp = av_fetch(CopFILEAV(PL_curcop), (I32)CopLINE(cop), FALSE);
3312 if (svp && *svp != &PL_sv_undef && !SvIOK(*svp)) {
3313 (void)SvIOK_on(*svp);
3315 SvSTASH(*svp) = (HV*)cop;
3319 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3322 /* "Introduce" my variables to visible status. */
3330 if (! PL_min_intro_pending)
3331 return PL_cop_seqmax;
3333 svp = AvARRAY(PL_comppad_name);
3334 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
3335 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
3336 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
3337 SvNVX(sv) = (NV)PL_cop_seqmax;
3340 PL_min_intro_pending = 0;
3341 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
3342 return PL_cop_seqmax++;
3346 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
3348 return new_logop(type, flags, &first, &other);
3352 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
3357 OP *first = *firstp;
3358 OP *other = *otherp;
3360 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3361 return newBINOP(type, flags, scalar(first), scalar(other));
3363 scalarboolean(first);
3364 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3365 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3366 if (type == OP_AND || type == OP_OR) {
3372 first = *firstp = cUNOPo->op_first;
3374 first->op_next = o->op_next;
3375 cUNOPo->op_first = Nullop;
3379 if (first->op_type == OP_CONST) {
3380 if (ckWARN(WARN_PRECEDENCE) && (first->op_private & OPpCONST_BARE))
3381 Perl_warner(aTHX_ WARN_PRECEDENCE, "Probable precedence problem on %s",
3383 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3394 else if (first->op_type == OP_WANTARRAY) {
3400 else if (ckWARN(WARN_UNSAFE) && (first->op_flags & OPf_KIDS)) {
3401 OP *k1 = ((UNOP*)first)->op_first;
3402 OP *k2 = k1->op_sibling;
3404 switch (first->op_type)
3407 if (k2 && k2->op_type == OP_READLINE
3408 && (k2->op_flags & OPf_STACKED)
3409 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3411 warnop = k2->op_type;
3416 if (k1->op_type == OP_READDIR
3417 || k1->op_type == OP_GLOB
3418 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3419 || k1->op_type == OP_EACH)
3421 warnop = ((k1->op_type == OP_NULL)
3422 ? k1->op_targ : k1->op_type);
3427 line_t oldline = PL_curcop->cop_line;
3428 PL_curcop->cop_line = PL_copline;
3429 Perl_warner(aTHX_ WARN_UNSAFE,
3430 "Value of %s%s can be \"0\"; test with defined()",
3432 ((warnop == OP_READLINE || warnop == OP_GLOB)
3433 ? " construct" : "() operator"));
3434 PL_curcop->cop_line = oldline;
3441 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3442 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3444 NewOp(1101, logop, 1, LOGOP);
3446 logop->op_type = type;
3447 logop->op_ppaddr = PL_ppaddr[type];
3448 logop->op_first = first;
3449 logop->op_flags = flags | OPf_KIDS;
3450 logop->op_other = LINKLIST(other);
3451 logop->op_private = 1 | (flags >> 8);
3453 /* establish postfix order */
3454 logop->op_next = LINKLIST(first);
3455 first->op_next = (OP*)logop;
3456 first->op_sibling = other;
3458 o = newUNOP(OP_NULL, 0, (OP*)logop);
3465 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
3473 return newLOGOP(OP_AND, 0, first, trueop);
3475 return newLOGOP(OP_OR, 0, first, falseop);
3477 scalarboolean(first);
3478 if (first->op_type == OP_CONST) {
3479 if (SvTRUE(((SVOP*)first)->op_sv)) {
3490 else if (first->op_type == OP_WANTARRAY) {
3494 NewOp(1101, logop, 1, LOGOP);
3495 logop->op_type = OP_COND_EXPR;
3496 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3497 logop->op_first = first;
3498 logop->op_flags = flags | OPf_KIDS;
3499 logop->op_private = 1 | (flags >> 8);
3500 logop->op_other = LINKLIST(trueop);
3501 logop->op_next = LINKLIST(falseop);
3504 /* establish postfix order */
3505 start = LINKLIST(first);
3506 first->op_next = (OP*)logop;
3508 first->op_sibling = trueop;
3509 trueop->op_sibling = falseop;
3510 o = newUNOP(OP_NULL, 0, (OP*)logop);
3512 trueop->op_next = falseop->op_next = o;
3519 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
3528 NewOp(1101, range, 1, LOGOP);
3530 range->op_type = OP_RANGE;
3531 range->op_ppaddr = PL_ppaddr[OP_RANGE];
3532 range->op_first = left;
3533 range->op_flags = OPf_KIDS;
3534 leftstart = LINKLIST(left);
3535 range->op_other = LINKLIST(right);
3536 range->op_private = 1 | (flags >> 8);
3538 left->op_sibling = right;
3540 range->op_next = (OP*)range;
3541 flip = newUNOP(OP_FLIP, flags, (OP*)range);
3542 flop = newUNOP(OP_FLOP, 0, flip);
3543 o = newUNOP(OP_NULL, 0, flop);
3545 range->op_next = leftstart;
3547 left->op_next = flip;
3548 right->op_next = flop;
3550 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3551 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
3552 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3553 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3555 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3556 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3559 if (!flip->op_private || !flop->op_private)
3560 linklist(o); /* blow off optimizer unless constant */
3566 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
3571 int once = block && block->op_flags & OPf_SPECIAL &&
3572 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3575 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3576 return block; /* do {} while 0 does once */
3577 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3578 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3579 expr = newUNOP(OP_DEFINED, 0,
3580 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3581 } else if (expr->op_flags & OPf_KIDS) {
3582 OP *k1 = ((UNOP*)expr)->op_first;
3583 OP *k2 = (k1) ? k1->op_sibling : NULL;
3584 switch (expr->op_type) {
3586 if (k2 && k2->op_type == OP_READLINE
3587 && (k2->op_flags & OPf_STACKED)
3588 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3589 expr = newUNOP(OP_DEFINED, 0, expr);
3593 if (k1->op_type == OP_READDIR
3594 || k1->op_type == OP_GLOB
3595 || (k1->op_type == OP_NULL && k1->op_targ == OP_NULL)
3596 || k1->op_type == OP_EACH)
3597 expr = newUNOP(OP_DEFINED, 0, expr);
3603 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3604 o = new_logop(OP_AND, 0, &expr, &listop);
3607 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3609 if (once && o != listop)
3610 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3613 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3615 o->op_flags |= flags;
3617 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3622 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
3631 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3632 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
3633 expr = newUNOP(OP_DEFINED, 0,
3634 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3635 } else if (expr && (expr->op_flags & OPf_KIDS)) {
3636 OP *k1 = ((UNOP*)expr)->op_first;
3637 OP *k2 = (k1) ? k1->op_sibling : NULL;
3638 switch (expr->op_type) {
3640 if (k2 && k2->op_type == OP_READLINE
3641 && (k2->op_flags & OPf_STACKED)
3642 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3643 expr = newUNOP(OP_DEFINED, 0, expr);
3647 if (k1->op_type == OP_READDIR
3648 || k1->op_type == OP_GLOB
3649 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3650 || k1->op_type == OP_EACH)
3651 expr = newUNOP(OP_DEFINED, 0, expr);
3657 block = newOP(OP_NULL, 0);
3660 next = LINKLIST(cont);
3662 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
3663 if ((line_t)whileline != NOLINE) {
3664 PL_copline = whileline;
3665 cont = append_elem(OP_LINESEQ, cont,
3666 newSTATEOP(0, Nullch, Nullop));
3670 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3671 redo = LINKLIST(listop);
3674 PL_copline = whileline;
3676 o = new_logop(OP_AND, 0, &expr, &listop);
3677 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3678 op_free(expr); /* oops, it's a while (0) */
3680 return Nullop; /* listop already freed by new_logop */
3683 ((LISTOP*)listop)->op_last->op_next = condop =
3684 (o == listop ? redo : LINKLIST(o));
3692 NewOp(1101,loop,1,LOOP);
3693 loop->op_type = OP_ENTERLOOP;
3694 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3695 loop->op_private = 0;
3696 loop->op_next = (OP*)loop;
3699 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3701 loop->op_redoop = redo;
3702 loop->op_lastop = o;
3705 loop->op_nextop = next;
3707 loop->op_nextop = o;
3709 o->op_flags |= flags;
3710 o->op_private |= (flags >> 8);
3715 Perl_newFOROP(pTHX_ I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
3724 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3725 sv->op_type = OP_RV2GV;
3726 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
3728 else if (sv->op_type == OP_PADSV) { /* private variable */
3729 padoff = sv->op_targ;
3733 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3734 padoff = sv->op_targ;
3735 iterflags |= OPf_SPECIAL;
3740 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
3744 padoff = find_threadsv("_");
3745 iterflags |= OPf_SPECIAL;
3747 sv = newGVOP(OP_GV, 0, PL_defgv);
3750 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3751 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3752 iterflags |= OPf_STACKED;
3754 else if (expr->op_type == OP_NULL &&
3755 (expr->op_flags & OPf_KIDS) &&
3756 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3758 /* Basically turn for($x..$y) into the same as for($x,$y), but we
3759 * set the STACKED flag to indicate that these values are to be
3760 * treated as min/max values by 'pp_iterinit'.
3762 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3763 LOGOP* range = (LOGOP*) flip->op_first;
3764 OP* left = range->op_first;
3765 OP* right = left->op_sibling;
3768 range->op_flags &= ~OPf_KIDS;
3769 range->op_first = Nullop;
3771 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3772 listop->op_first->op_next = range->op_next;
3773 left->op_next = range->op_other;
3774 right->op_next = (OP*)listop;
3775 listop->op_next = listop->op_first;
3778 expr = (OP*)(listop);
3780 iterflags |= OPf_STACKED;
3783 expr = mod(force_list(expr), OP_GREPSTART);
3787 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
3788 append_elem(OP_LIST, expr, scalar(sv))));
3789 assert(!loop->op_next);
3790 #ifdef PL_OP_SLAB_ALLOC
3791 NewOp(1234,tmp,1,LOOP);
3792 Copy(loop,tmp,1,LOOP);
3795 Renew(loop, 1, LOOP);
3797 loop->op_targ = padoff;
3798 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
3799 PL_copline = forline;
3800 return newSTATEOP(0, label, wop);
3804 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
3810 if (type != OP_GOTO || label->op_type == OP_CONST) {
3811 /* "last()" means "last" */
3812 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
3813 o = newOP(type, OPf_SPECIAL);
3815 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
3816 ? SvPVx(((SVOP*)label)->op_sv, n_a)
3822 if (label->op_type == OP_ENTERSUB)
3823 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
3824 o = newUNOP(type, OPf_STACKED, label);
3826 PL_hints |= HINT_BLOCK_SCOPE;
3831 Perl_cv_undef(pTHX_ CV *cv)
3836 MUTEX_DESTROY(CvMUTEXP(cv));
3837 Safefree(CvMUTEXP(cv));
3840 #endif /* USE_THREADS */
3842 if (!CvXSUB(cv) && CvROOT(cv)) {
3844 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
3845 Perl_croak(aTHX_ "Can't undef active subroutine");
3848 Perl_croak(aTHX_ "Can't undef active subroutine");
3849 #endif /* USE_THREADS */
3852 SAVESPTR(PL_curpad);
3856 op_free(CvROOT(cv));
3857 CvROOT(cv) = Nullop;
3860 SvPOK_off((SV*)cv); /* forget prototype */
3862 SvREFCNT_dec(CvGV(cv));
3864 SvREFCNT_dec(CvOUTSIDE(cv));
3865 CvOUTSIDE(cv) = Nullcv;
3866 if (CvPADLIST(cv)) {
3867 /* may be during global destruction */
3868 if (SvREFCNT(CvPADLIST(cv))) {
3869 I32 i = AvFILLp(CvPADLIST(cv));
3871 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
3872 SV* sv = svp ? *svp : Nullsv;
3875 if (sv == (SV*)PL_comppad_name)
3876 PL_comppad_name = Nullav;
3877 else if (sv == (SV*)PL_comppad) {
3878 PL_comppad = Nullav;
3879 PL_curpad = Null(SV**);
3883 SvREFCNT_dec((SV*)CvPADLIST(cv));
3885 CvPADLIST(cv) = Nullav;
3889 #ifdef DEBUG_CLOSURES
3893 CV *outside = CvOUTSIDE(cv);
3894 AV* padlist = CvPADLIST(cv);
3901 PerlIO_printf(Perl_debug_log,
3902 "\tCV=0x%"UVxf" (%s), OUTSIDE=0x%"UVxf" (%s)\n",
3904 (CvANON(cv) ? "ANON"
3905 : (cv == PL_main_cv) ? "MAIN"
3906 : CvUNIQUE(cv) ? "UNIQUE"
3907 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
3910 : CvANON(outside) ? "ANON"
3911 : (outside == PL_main_cv) ? "MAIN"
3912 : CvUNIQUE(outside) ? "UNIQUE"
3913 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
3918 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
3919 pad = (AV*)*av_fetch(padlist, 1, FALSE);
3920 pname = AvARRAY(pad_name);
3921 ppad = AvARRAY(pad);
3923 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
3924 if (SvPOK(pname[ix]))
3925 PerlIO_printf(Perl_debug_log,
3926 "\t%4d. 0x%"UVxf" (%s\"%s\" %"IVdf"-%"IVdf")\n",
3927 ix, PTR2UV(ppad[ix]),
3928 SvFAKE(pname[ix]) ? "FAKE " : "",
3930 (IV)I_32(SvNVX(pname[ix])),
3934 #endif /* DEBUG_CLOSURES */
3937 S_cv_clone2(pTHX_ CV *proto, CV *outside)
3942 AV* protopadlist = CvPADLIST(proto);
3943 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
3944 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
3945 SV** pname = AvARRAY(protopad_name);
3946 SV** ppad = AvARRAY(protopad);
3947 I32 fname = AvFILLp(protopad_name);
3948 I32 fpad = AvFILLp(protopad);
3952 assert(!CvUNIQUE(proto));
3955 SAVESPTR(PL_curpad);
3956 SAVESPTR(PL_comppad);
3957 SAVESPTR(PL_comppad_name);
3958 SAVESPTR(PL_compcv);
3960 cv = PL_compcv = (CV*)NEWSV(1104,0);
3961 sv_upgrade((SV *)cv, SvTYPE(proto));
3967 New(666, CvMUTEXP(cv), 1, perl_mutex);
3968 MUTEX_INIT(CvMUTEXP(cv));
3970 #endif /* USE_THREADS */
3971 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
3972 CvSTASH(cv) = CvSTASH(proto);
3973 CvROOT(cv) = CvROOT(proto);
3974 CvSTART(cv) = CvSTART(proto);
3976 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
3979 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
3981 PL_comppad_name = newAV();
3982 for (ix = fname; ix >= 0; ix--)
3983 av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix]));
3985 PL_comppad = newAV();
3987 comppadlist = newAV();
3988 AvREAL_off(comppadlist);
3989 av_store(comppadlist, 0, (SV*)PL_comppad_name);
3990 av_store(comppadlist, 1, (SV*)PL_comppad);
3991 CvPADLIST(cv) = comppadlist;
3992 av_fill(PL_comppad, AvFILLp(protopad));
3993 PL_curpad = AvARRAY(PL_comppad);
3995 av = newAV(); /* will be @_ */
3997 av_store(PL_comppad, 0, (SV*)av);
3998 AvFLAGS(av) = AVf_REIFY;
4000 for (ix = fpad; ix > 0; ix--) {
4001 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
4002 if (namesv && namesv != &PL_sv_undef) {
4003 char *name = SvPVX(namesv); /* XXX */
4004 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
4005 I32 off = pad_findlex(name, ix, SvIVX(namesv),
4006 CvOUTSIDE(cv), cxstack_ix, 0, 0);
4008 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
4010 Perl_croak(aTHX_ "panic: cv_clone: %s", name);
4012 else { /* our own lexical */
4015 /* anon code -- we'll come back for it */
4016 sv = SvREFCNT_inc(ppad[ix]);
4018 else if (*name == '@')
4020 else if (*name == '%')
4030 SV* sv = NEWSV(0,0);
4036 /* Now that vars are all in place, clone nested closures. */
4038 for (ix = fpad; ix > 0; ix--) {
4039 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
4041 && namesv != &PL_sv_undef
4042 && !(SvFLAGS(namesv) & SVf_FAKE)
4043 && *SvPVX(namesv) == '&'
4044 && CvCLONE(ppad[ix]))
4046 CV *kid = cv_clone2((CV*)ppad[ix], cv);
4047 SvREFCNT_dec(ppad[ix]);
4050 PL_curpad[ix] = (SV*)kid;
4054 #ifdef DEBUG_CLOSURES
4055 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
4057 PerlIO_printf(Perl_debug_log, " from:\n");
4059 PerlIO_printf(Perl_debug_log, " to:\n");
4068 Perl_cv_clone(pTHX_ CV *proto)
4071 MUTEX_LOCK(&PL_cred_mutex); /* XXX create separate mutex */
4072 cv = cv_clone2(proto, CvOUTSIDE(proto));
4073 MUTEX_UNLOCK(&PL_cred_mutex); /* XXX create separate mutex */
4078 Perl_cv_ckproto(pTHX_ CV *cv, GV *gv, char *p)
4082 if (((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) && ckWARN_d(WARN_UNSAFE)) {
4083 SV* msg = sv_newmortal();
4087 gv_efullname3(name = sv_newmortal(), gv, Nullch);
4088 sv_setpv(msg, "Prototype mismatch:");
4090 Perl_sv_catpvf(aTHX_ msg, " sub %_", name);
4092 Perl_sv_catpvf(aTHX_ msg, " (%s)", SvPVX(cv));
4093 sv_catpv(msg, " vs ");
4095 Perl_sv_catpvf(aTHX_ msg, "(%s)", p);
4097 sv_catpv(msg, "none");
4098 Perl_warner(aTHX_ WARN_UNSAFE, "%_", msg);
4103 Perl_cv_const_sv(pTHX_ CV *cv)
4105 if (!cv || !SvPOK(cv) || SvCUR(cv))
4107 return op_const_sv(CvSTART(cv), cv);
4111 Perl_op_const_sv(pTHX_ OP *o, CV *cv)
4118 if(o->op_type == OP_LINESEQ && cLISTOPo->op_first)
4119 o = cLISTOPo->op_first->op_sibling;
4121 for (; o; o = o->op_next) {
4122 OPCODE type = o->op_type;
4124 if (sv && o->op_next == o)
4126 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
4128 if (type == OP_LEAVESUB || type == OP_RETURN)
4132 if (type == OP_CONST)
4134 else if (type == OP_PADSV && cv) {
4135 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
4136 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
4137 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
4149 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4159 Perl_croak(aTHX_ "\"my sub\" not yet implemented");
4163 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
4165 return Perl_newATTRSUB(aTHX_ floor, o, proto, Nullop, block);
4169 Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4173 char *name = o ? SvPVx(cSVOPo->op_sv, n_a) : Nullch;
4174 GV *gv = gv_fetchpv(name ? name : "__ANON__",
4175 GV_ADDMULTI | ((block || attrs) ? 0 : GV_NOINIT),
4177 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, n_a) : Nullch;
4188 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
4189 maximum a prototype before. */
4190 if (SvTYPE(gv) > SVt_NULL) {
4191 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1)
4192 && ckWARN_d(WARN_UNSAFE))
4194 Perl_warner(aTHX_ WARN_UNSAFE, "Runaway prototype");
4196 cv_ckproto((CV*)gv, NULL, ps);
4199 sv_setpv((SV*)gv, ps);
4201 sv_setiv((SV*)gv, -1);
4202 SvREFCNT_dec(PL_compcv);
4203 cv = PL_compcv = NULL;
4204 PL_sub_generation++;
4208 if (!name || GvCVGEN(gv))
4210 else if (cv = GvCV(gv)) {
4211 cv_ckproto(cv, gv, ps);
4212 /* already defined (or promised)? */
4213 if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4215 bool const_changed = TRUE;
4216 if (!block && !attrs) {
4217 /* just a "sub foo;" when &foo is already defined */
4218 SAVEFREESV(PL_compcv);
4221 /* ahem, death to those who redefine active sort subs */
4222 if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv))
4223 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", name);
4226 if(const_sv = cv_const_sv(cv))
4227 const_changed = sv_cmp(const_sv, op_const_sv(block, Nullcv));
4228 if ((const_sv && const_changed) || ckWARN(WARN_REDEFINE)
4229 && !(CvGV(cv) && GvSTASH(CvGV(cv))
4230 && HvNAME(GvSTASH(CvGV(cv)))
4231 && strEQ(HvNAME(GvSTASH(CvGV(cv))),
4233 line_t oldline = PL_curcop->cop_line;
4234 PL_curcop->cop_line = PL_copline;
4235 Perl_warner(aTHX_ WARN_REDEFINE,
4236 const_sv ? "Constant subroutine %s redefined"
4237 : "Subroutine %s redefined", name);
4238 PL_curcop->cop_line = oldline;
4249 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>
4250 * before we clobber PL_compcv.
4254 if (CvGV(cv) && GvSTASH(CvGV(cv)) && HvNAME(GvSTASH(CvGV(cv))))
4255 stash = GvSTASH(CvGV(cv));
4256 else if (CvSTASH(cv) && HvNAME(CvSTASH(cv)))
4257 stash = CvSTASH(cv);
4259 stash = PL_curstash;
4262 /* possibly about to re-define existing subr -- ignore old cv */
4263 rcv = (SV*)PL_compcv;
4264 if (name && GvSTASH(gv) && HvNAME(GvSTASH(gv)))
4265 stash = GvSTASH(gv);
4267 stash = PL_curstash;
4269 apply_attrs(stash, rcv, attrs);
4271 if (cv) { /* must reuse cv if autoloaded */
4273 /* got here with just attrs -- work done, so bug out */
4274 SAVEFREESV(PL_compcv);
4278 CvFLAGS(cv) = CvFLAGS(PL_compcv);
4279 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
4280 CvOUTSIDE(PL_compcv) = 0;
4281 CvPADLIST(cv) = CvPADLIST(PL_compcv);
4282 CvPADLIST(PL_compcv) = 0;
4283 if (SvREFCNT(PL_compcv) > 1) /* XXX Make closures transit through stub. */
4284 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc((SV*)cv);
4285 SvREFCNT_dec(PL_compcv);
4292 PL_sub_generation++;
4295 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4296 CvSTASH(cv) = PL_curstash;
4299 if (!CvMUTEXP(cv)) {
4300 New(666, CvMUTEXP(cv), 1, perl_mutex);
4301 MUTEX_INIT(CvMUTEXP(cv));
4303 #endif /* USE_THREADS */
4306 sv_setpv((SV*)cv, ps);
4308 if (PL_error_count) {
4312 char *s = strrchr(name, ':');
4314 if (strEQ(s, "BEGIN")) {
4316 "BEGIN not safe after errors--compilation aborted";
4317 if (PL_in_eval & EVAL_KEEPERR)
4318 Perl_croak(aTHX_ not_safe);
4320 /* force display of errors found but not reported */
4321 sv_catpv(ERRSV, not_safe);
4322 Perl_croak(aTHX_ "%s", SvPVx(ERRSV, n_a));
4329 PL_copline = NOLINE;
4334 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad))
4335 av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv);
4338 SV **namep = AvARRAY(PL_comppad_name);
4339 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4342 if (SvIMMORTAL(PL_curpad[ix]))
4345 * The only things that a clonable function needs in its
4346 * pad are references to outer lexicals and anonymous subs.
4347 * The rest are created anew during cloning.
4349 if (!((namesv = namep[ix]) != Nullsv &&
4350 namesv != &PL_sv_undef &&
4352 *SvPVX(namesv) == '&')))
4354 SvREFCNT_dec(PL_curpad[ix]);
4355 PL_curpad[ix] = Nullsv;
4360 AV *av = newAV(); /* Will be @_ */
4362 av_store(PL_comppad, 0, (SV*)av);
4363 AvFLAGS(av) = AVf_REIFY;
4365 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4366 if (SvIMMORTAL(PL_curpad[ix]))
4368 if (!SvPADMY(PL_curpad[ix]))
4369 SvPADTMP_on(PL_curpad[ix]);
4374 CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0, scalarseq(block));
4377 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4379 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4380 OpREFCNT_set(CvROOT(cv), 1);
4381 CvSTART(cv) = LINKLIST(CvROOT(cv));
4382 CvROOT(cv)->op_next = 0;
4388 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4389 SV *sv = NEWSV(0,0);
4390 SV *tmpstr = sv_newmortal();
4391 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
4395 Perl_sv_setpvf(aTHX_ sv, "%_:%ld-%ld",
4396 CopFILESV(PL_curcop),
4397 (long)PL_subline, (long)CopLINE(PL_curcop));
4398 gv_efullname3(tmpstr, gv, Nullch);
4399 hv_store(GvHV(PL_DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
4400 hv = GvHVn(db_postponed);
4401 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
4402 && (cv = GvCV(db_postponed))) {
4407 call_sv((SV*)cv, G_DISCARD);
4411 if ((s = strrchr(name,':')))
4415 if (strEQ(s, "BEGIN")) {
4416 I32 oldscope = PL_scopestack_ix;
4418 SAVESPTR(CopFILEGV(&PL_compiling));
4419 SAVEI16(PL_compiling.cop_line);
4421 sv_setsv(PL_rs, PL_nrs);
4424 PL_beginav = newAV();
4425 DEBUG_x( dump_sub(gv) );
4426 av_push(PL_beginav, (SV *)cv);
4428 call_list(oldscope, PL_beginav);
4430 PL_curcop = &PL_compiling;
4431 PL_compiling.op_private = PL_hints;
4434 else if (strEQ(s, "END") && !PL_error_count) {
4437 av_unshift(PL_endav, 1);
4438 av_store(PL_endav, 0, (SV *)cv);
4441 else if (strEQ(s, "STOP") && !PL_error_count) {
4443 PL_stopav = newAV();
4444 av_unshift(PL_stopav, 1);
4445 av_store(PL_stopav, 0, (SV *)cv);
4448 else if (strEQ(s, "INIT") && !PL_error_count) {
4450 PL_initav = newAV();
4451 av_push(PL_initav, SvREFCNT_inc(cv));
4457 PL_copline = NOLINE;
4462 /* XXX unsafe for threads if eval_owner isn't held */
4464 Perl_newCONSTSUB(pTHX_ HV *stash, char *name, SV *sv)
4467 U32 oldhints = PL_hints;
4468 HV *old_cop_stash = PL_curcop->cop_stash;
4469 HV *old_curstash = PL_curstash;
4470 line_t oldline = PL_curcop->cop_line;
4471 PL_curcop->cop_line = PL_copline;
4473 PL_hints &= ~HINT_BLOCK_SCOPE;
4475 PL_curstash = PL_curcop->cop_stash = stash;
4478 start_subparse(FALSE, 0),
4479 newSVOP(OP_CONST, 0, newSVpv(name,0)),
4480 newSVOP(OP_CONST, 0, &PL_sv_no), /* SvPV(&PL_sv_no) == "" -- GMB */
4482 newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv))
4485 PL_hints = oldhints;
4486 PL_curcop->cop_stash = old_cop_stash;
4487 PL_curstash = old_curstash;
4488 PL_curcop->cop_line = oldline;
4492 Perl_newXS(pTHX_ char *name, XSUBADDR_t subaddr, char *filename)
4495 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
4498 if (cv = (name ? GvCV(gv) : Nullcv)) {
4500 /* just a cached method */
4504 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4505 /* already defined (or promised) */
4506 if (ckWARN(WARN_REDEFINE) && !(CvGV(cv) && GvSTASH(CvGV(cv))
4507 && HvNAME(GvSTASH(CvGV(cv)))
4508 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
4509 line_t oldline = PL_curcop->cop_line;
4510 if (PL_copline != NOLINE)
4511 PL_curcop->cop_line = PL_copline;
4512 Perl_warner(aTHX_ WARN_REDEFINE, "Subroutine %s redefined",name);
4513 PL_curcop->cop_line = oldline;
4520 if (cv) /* must reuse cv if autoloaded */
4523 cv = (CV*)NEWSV(1105,0);
4524 sv_upgrade((SV *)cv, SVt_PVCV);
4528 PL_sub_generation++;
4531 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4533 New(666, CvMUTEXP(cv), 1, perl_mutex);
4534 MUTEX_INIT(CvMUTEXP(cv));
4536 #endif /* USE_THREADS */
4537 (void)gv_fetchfile(filename);
4538 CvXSUB(cv) = subaddr;
4541 char *s = strrchr(name,':');
4546 if (strEQ(s, "BEGIN")) {
4548 PL_beginav = newAV();
4549 av_push(PL_beginav, (SV *)cv);
4552 else if (strEQ(s, "END")) {
4555 av_unshift(PL_endav, 1);
4556 av_store(PL_endav, 0, (SV *)cv);
4559 else if (strEQ(s, "STOP")) {
4561 PL_stopav = newAV();
4562 av_unshift(PL_stopav, 1);
4563 av_store(PL_stopav, 0, (SV *)cv);
4566 else if (strEQ(s, "INIT")) {
4568 PL_initav = newAV();
4569 av_push(PL_initav, (SV *)cv);
4580 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
4590 name = SvPVx(cSVOPo->op_sv, n_a);
4593 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
4595 if (cv = GvFORM(gv)) {
4596 if (ckWARN(WARN_REDEFINE)) {
4597 line_t oldline = PL_curcop->cop_line;
4599 PL_curcop->cop_line = PL_copline;
4600 Perl_warner(aTHX_ WARN_REDEFINE, "Format %s redefined",name);
4601 PL_curcop->cop_line = oldline;
4607 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4609 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4610 if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix]))
4611 SvPADTMP_on(PL_curpad[ix]);
4614 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4615 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4616 OpREFCNT_set(CvROOT(cv), 1);
4617 CvSTART(cv) = LINKLIST(CvROOT(cv));
4618 CvROOT(cv)->op_next = 0;
4621 PL_copline = NOLINE;
4626 Perl_newANONLIST(pTHX_ OP *o)
4628 return newUNOP(OP_REFGEN, 0,
4629 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
4633 Perl_newANONHASH(pTHX_ OP *o)
4635 return newUNOP(OP_REFGEN, 0,
4636 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
4640 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
4642 return newANONATTRSUB(floor, proto, Nullop, block);
4646 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
4648 return newUNOP(OP_REFGEN, 0,
4649 newSVOP(OP_ANONCODE, 0,
4650 (SV*)newATTRSUB(floor, 0, proto, attrs, block)));
4654 Perl_oopsAV(pTHX_ OP *o)
4656 switch (o->op_type) {
4658 o->op_type = OP_PADAV;
4659 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4660 return ref(o, OP_RV2AV);
4663 o->op_type = OP_RV2AV;
4664 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4669 if (ckWARN_d(WARN_INTERNAL))
4670 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsAV");
4677 Perl_oopsHV(pTHX_ OP *o)
4681 switch (o->op_type) {
4684 o->op_type = OP_PADHV;
4685 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4686 return ref(o, OP_RV2HV);
4690 o->op_type = OP_RV2HV;
4691 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
4696 if (ckWARN_d(WARN_INTERNAL))
4697 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsHV");
4704 Perl_newAVREF(pTHX_ OP *o)
4706 if (o->op_type == OP_PADANY) {
4707 o->op_type = OP_PADAV;
4708 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4711 return newUNOP(OP_RV2AV, 0, scalar(o));
4715 Perl_newGVREF(pTHX_ I32 type, OP *o)
4717 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
4718 return newUNOP(OP_NULL, 0, o);
4719 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
4723 Perl_newHVREF(pTHX_ OP *o)
4725 if (o->op_type == OP_PADANY) {
4726 o->op_type = OP_PADHV;
4727 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4730 return newUNOP(OP_RV2HV, 0, scalar(o));
4734 Perl_oopsCV(pTHX_ OP *o)
4736 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
4742 Perl_newCVREF(pTHX_ I32 flags, OP *o)
4744 return newUNOP(OP_RV2CV, flags, scalar(o));
4748 Perl_newSVREF(pTHX_ OP *o)
4750 if (o->op_type == OP_PADANY) {
4751 o->op_type = OP_PADSV;
4752 o->op_ppaddr = PL_ppaddr[OP_PADSV];
4755 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
4756 o->op_flags |= OPpDONE_SVREF;
4759 return newUNOP(OP_RV2SV, 0, scalar(o));
4762 /* Check routines. */
4765 Perl_ck_anoncode(pTHX_ OP *o)
4770 name = NEWSV(1106,0);
4771 sv_upgrade(name, SVt_PVNV);
4772 sv_setpvn(name, "&", 1);
4775 ix = pad_alloc(o->op_type, SVs_PADMY);
4776 av_store(PL_comppad_name, ix, name);
4777 av_store(PL_comppad, ix, cSVOPo->op_sv);
4778 SvPADMY_on(cSVOPo->op_sv);
4779 cSVOPo->op_sv = Nullsv;
4780 cSVOPo->op_targ = ix;
4785 Perl_ck_bitop(pTHX_ OP *o)
4787 o->op_private = PL_hints;
4792 Perl_ck_concat(pTHX_ OP *o)
4794 if (cUNOPo->op_first->op_type == OP_CONCAT)
4795 o->op_flags |= OPf_STACKED;
4800 Perl_ck_spair(pTHX_ OP *o)
4802 if (o->op_flags & OPf_KIDS) {
4805 OPCODE type = o->op_type;
4806 o = modkids(ck_fun(o), type);
4807 kid = cUNOPo->op_first;
4808 newop = kUNOP->op_first->op_sibling;
4810 (newop->op_sibling ||
4811 !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
4812 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
4813 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
4817 op_free(kUNOP->op_first);
4818 kUNOP->op_first = newop;
4820 o->op_ppaddr = PL_ppaddr[++o->op_type];
4825 Perl_ck_delete(pTHX_ OP *o)
4829 if (o->op_flags & OPf_KIDS) {
4830 OP *kid = cUNOPo->op_first;
4831 if (kid->op_type == OP_HSLICE)
4832 o->op_private |= OPpSLICE;
4833 else if (kid->op_type != OP_HELEM)
4834 Perl_croak(aTHX_ "%s argument is not a HASH element or slice",
4835 PL_op_desc[o->op_type]);
4842 Perl_ck_eof(pTHX_ OP *o)
4844 I32 type = o->op_type;
4846 if (o->op_flags & OPf_KIDS) {
4847 if (cLISTOPo->op_first->op_type == OP_STUB) {
4849 o = newUNOP(type, OPf_SPECIAL,
4850 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
4858 Perl_ck_eval(pTHX_ OP *o)
4860 PL_hints |= HINT_BLOCK_SCOPE;
4861 if (o->op_flags & OPf_KIDS) {
4862 SVOP *kid = (SVOP*)cUNOPo->op_first;
4865 o->op_flags &= ~OPf_KIDS;
4868 else if (kid->op_type == OP_LINESEQ) {
4871 kid->op_next = o->op_next;
4872 cUNOPo->op_first = 0;
4875 NewOp(1101, enter, 1, LOGOP);
4876 enter->op_type = OP_ENTERTRY;
4877 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
4878 enter->op_private = 0;
4880 /* establish postfix order */
4881 enter->op_next = (OP*)enter;
4883 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
4884 o->op_type = OP_LEAVETRY;
4885 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
4886 enter->op_other = o;
4894 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
4896 o->op_targ = (PADOFFSET)PL_hints;
4901 Perl_ck_exec(pTHX_ OP *o)
4904 if (o->op_flags & OPf_STACKED) {
4906 kid = cUNOPo->op_first->op_sibling;
4907 if (kid->op_type == OP_RV2GV)
4916 Perl_ck_exists(pTHX_ OP *o)
4919 if (o->op_flags & OPf_KIDS) {
4920 OP *kid = cUNOPo->op_first;
4921 if (kid->op_type != OP_HELEM)
4922 Perl_croak(aTHX_ "%s argument is not a HASH element", PL_op_desc[o->op_type]);
4930 Perl_ck_gvconst(pTHX_ register OP *o)
4932 o = fold_constants(o);
4933 if (o->op_type == OP_CONST)
4940 Perl_ck_rvconst(pTHX_ register OP *o)
4943 SVOP *kid = (SVOP*)cUNOPo->op_first;
4945 o->op_private |= (PL_hints & HINT_STRICT_REFS);
4946 if (kid->op_type == OP_CONST) {
4950 SV *kidsv = kid->op_sv;
4953 /* Is it a constant from cv_const_sv()? */
4954 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
4955 SV *rsv = SvRV(kidsv);
4956 int svtype = SvTYPE(rsv);
4957 char *badtype = Nullch;
4959 switch (o->op_type) {
4961 if (svtype > SVt_PVMG)
4962 badtype = "a SCALAR";
4965 if (svtype != SVt_PVAV)
4966 badtype = "an ARRAY";
4969 if (svtype != SVt_PVHV) {
4970 if (svtype == SVt_PVAV) { /* pseudohash? */
4971 SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
4972 if (ksv && SvROK(*ksv)
4973 && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
4982 if (svtype != SVt_PVCV)
4987 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
4990 name = SvPV(kidsv, n_a);
4991 if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
4992 char *badthing = Nullch;
4993 switch (o->op_type) {
4995 badthing = "a SCALAR";
4998 badthing = "an ARRAY";
5001 badthing = "a HASH";
5006 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
5010 * This is a little tricky. We only want to add the symbol if we
5011 * didn't add it in the lexer. Otherwise we get duplicate strict
5012 * warnings. But if we didn't add it in the lexer, we must at
5013 * least pretend like we wanted to add it even if it existed before,
5014 * or we get possible typo warnings. OPpCONST_ENTERED says
5015 * whether the lexer already added THIS instance of this symbol.
5017 iscv = (o->op_type == OP_RV2CV) * 2;
5019 gv = gv_fetchpv(name,
5020 iscv | !(kid->op_private & OPpCONST_ENTERED),
5023 : o->op_type == OP_RV2SV
5025 : o->op_type == OP_RV2AV
5027 : o->op_type == OP_RV2HV
5030 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
5032 kid->op_type = OP_GV;
5033 SvREFCNT_dec(kid->op_sv);
5035 /* XXXXXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
5036 kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
5037 PL_curpad[kPADOP->op_padix] = SvREFCNT_inc(gv);
5039 kid->op_sv = SvREFCNT_inc(gv);
5041 kid->op_ppaddr = PL_ppaddr[OP_GV];
5048 Perl_ck_ftst(pTHX_ OP *o)
5051 I32 type = o->op_type;
5053 if (o->op_flags & OPf_REF)
5056 if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
5057 SVOP *kid = (SVOP*)cUNOPo->op_first;
5059 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5061 OP *newop = newGVOP(type, OPf_REF,
5062 gv_fetchpv(SvPVx(kid->op_sv, n_a), TRUE, SVt_PVIO));
5069 if (type == OP_FTTTY)
5070 return newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
5073 return newUNOP(type, 0, newDEFSVOP());
5079 Perl_ck_fun(pTHX_ OP *o)
5086 int type = o->op_type;
5087 register I32 oa = PL_opargs[type] >> OASHIFT;
5089 if (o->op_flags & OPf_STACKED) {
5090 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
5093 return no_fh_allowed(o);
5096 if (o->op_flags & OPf_KIDS) {
5098 tokid = &cLISTOPo->op_first;
5099 kid = cLISTOPo->op_first;
5100 if (kid->op_type == OP_PUSHMARK ||
5101 kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
5103 tokid = &kid->op_sibling;
5104 kid = kid->op_sibling;
5106 if (!kid && PL_opargs[type] & OA_DEFGV)
5107 *tokid = kid = newDEFSVOP();
5111 sibl = kid->op_sibling;
5114 /* list seen where single (scalar) arg expected? */
5115 if (numargs == 1 && !(oa >> 4)
5116 && kid->op_type == OP_LIST && type != OP_SCALAR)
5118 return too_many_arguments(o,PL_op_desc[type]);
5131 if (kid->op_type == OP_CONST &&
5132 (kid->op_private & OPpCONST_BARE))
5134 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5135 OP *newop = newAVREF(newGVOP(OP_GV, 0,
5136 gv_fetchpv(name, TRUE, SVt_PVAV) ));
5137 if (ckWARN(WARN_SYNTAX))
5138 Perl_warner(aTHX_ WARN_SYNTAX,
5139 "Array @%s missing the @ in argument %"IVdf" of %s()",
5140 name, (IV)numargs, PL_op_desc[type]);
5143 kid->op_sibling = sibl;
5146 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
5147 bad_type(numargs, "array", PL_op_desc[type], kid);
5151 if (kid->op_type == OP_CONST &&
5152 (kid->op_private & OPpCONST_BARE))
5154 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5155 OP *newop = newHVREF(newGVOP(OP_GV, 0,
5156 gv_fetchpv(name, TRUE, SVt_PVHV) ));
5157 if (ckWARN(WARN_SYNTAX))
5158 Perl_warner(aTHX_ WARN_SYNTAX,
5159 "Hash %%%s missing the %% in argument %"IVdf" of %s()",
5160 name, (IV)numargs, PL_op_desc[type]);
5163 kid->op_sibling = sibl;
5166 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
5167 bad_type(numargs, "hash", PL_op_desc[type], kid);
5172 OP *newop = newUNOP(OP_NULL, 0, kid);
5173 kid->op_sibling = 0;
5175 newop->op_next = newop;
5177 kid->op_sibling = sibl;
5182 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
5183 if (kid->op_type == OP_CONST &&
5184 (kid->op_private & OPpCONST_BARE))
5186 OP *newop = newGVOP(OP_GV, 0,
5187 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, n_a), TRUE,
5192 else if (kid->op_type == OP_READLINE) {
5193 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
5194 bad_type(numargs, "HANDLE", PL_op_desc[o->op_type], kid);
5197 I32 flags = OPf_SPECIAL;
5199 /* is this op a FH constructor? */
5200 if (is_handle_constructor(o,numargs)) {
5202 /* Set a flag to tell rv2gv to vivify
5203 * need to "prove" flag does not mean something
5204 * else already - NI-S 1999/05/07
5208 /* Helps with open($array[$n],...)
5209 but is too simplistic - need to do selectively
5214 kid->op_sibling = 0;
5215 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
5217 kid->op_private |= priv;
5220 kid->op_sibling = sibl;
5226 mod(scalar(kid), type);
5230 tokid = &kid->op_sibling;
5231 kid = kid->op_sibling;
5233 o->op_private |= numargs;
5235 return too_many_arguments(o,PL_op_desc[o->op_type]);
5238 else if (PL_opargs[type] & OA_DEFGV) {
5240 return newUNOP(type, 0, newDEFSVOP());
5244 while (oa & OA_OPTIONAL)
5246 if (oa && oa != OA_LIST)
5247 return too_few_arguments(o,PL_op_desc[o->op_type]);
5253 Perl_ck_glob(pTHX_ OP *o)
5257 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
5258 append_elem(OP_GLOB, o, newDEFSVOP());
5260 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
5261 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5263 #if defined(PERL_INTERNAL_GLOB) && !defined(MINIPERL_BUILD)
5264 /* XXX this can be tightened up and made more failsafe. */
5266 OP *modname = newSVOP(OP_CONST, 0, newSVpvn("File::Glob", 10));
5267 modname->op_private |= OPpCONST_BARE;
5269 utilize(1, start_subparse(FALSE, 0), Nullop, modname,
5270 newSVOP(OP_CONST, 0, newSVpvn("globally", 8)));
5271 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5274 #endif /* PERL_INTERNAL_GLOB && !MINIPERL_BUILD */
5276 if (gv && GvIMPORTED_CV(gv)) {
5277 append_elem(OP_GLOB, o,
5278 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
5279 o->op_type = OP_LIST;
5280 o->op_ppaddr = PL_ppaddr[OP_LIST];
5281 cLISTOPo->op_first->op_type = OP_PUSHMARK;
5282 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
5283 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
5284 append_elem(OP_LIST, o,
5285 scalar(newUNOP(OP_RV2CV, 0,
5286 newGVOP(OP_GV, 0, gv)))));
5287 o = newUNOP(OP_NULL, 0, ck_subr(o));
5288 o->op_targ = OP_GLOB; /* hint at what it used to be */
5291 gv = newGVgen("main");
5293 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
5299 Perl_ck_grep(pTHX_ OP *o)
5303 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
5305 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
5306 NewOp(1101, gwop, 1, LOGOP);
5308 if (o->op_flags & OPf_STACKED) {
5311 kid = cLISTOPo->op_first->op_sibling;
5312 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
5315 kid->op_next = (OP*)gwop;
5316 o->op_flags &= ~OPf_STACKED;
5318 kid = cLISTOPo->op_first->op_sibling;
5319 if (type == OP_MAPWHILE)
5326 kid = cLISTOPo->op_first->op_sibling;
5327 if (kid->op_type != OP_NULL)
5328 Perl_croak(aTHX_ "panic: ck_grep");
5329 kid = kUNOP->op_first;
5331 gwop->op_type = type;
5332 gwop->op_ppaddr = PL_ppaddr[type];
5333 gwop->op_first = listkids(o);
5334 gwop->op_flags |= OPf_KIDS;
5335 gwop->op_private = 1;
5336 gwop->op_other = LINKLIST(kid);
5337 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
5338 kid->op_next = (OP*)gwop;
5340 kid = cLISTOPo->op_first->op_sibling;
5341 if (!kid || !kid->op_sibling)
5342 return too_few_arguments(o,PL_op_desc[o->op_type]);
5343 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
5344 mod(kid, OP_GREPSTART);
5350 Perl_ck_index(pTHX_ OP *o)
5352 if (o->op_flags & OPf_KIDS) {
5353 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5355 kid = kid->op_sibling; /* get past "big" */
5356 if (kid && kid->op_type == OP_CONST)
5357 fbm_compile(((SVOP*)kid)->op_sv, 0);
5363 Perl_ck_lengthconst(pTHX_ OP *o)
5365 /* XXX length optimization goes here */
5370 Perl_ck_lfun(pTHX_ OP *o)
5372 OPCODE type = o->op_type;
5373 return modkids(ck_fun(o), type);
5377 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
5380 if ((o->op_flags & OPf_KIDS) && ckWARN(WARN_DEPRECATED)) {
5381 switch (cUNOPo->op_first->op_type) {
5383 break; /* Globals via GV can be undef */
5385 case OP_AASSIGN: /* Is this a good idea? */
5386 Perl_warner(aTHX_ WARN_DEPRECATED,
5387 "defined(@array) is deprecated");
5388 Perl_warner(aTHX_ WARN_DEPRECATED,
5389 "(Maybe you should just omit the defined()?)\n");
5392 break; /* Globals via GV can be undef */
5394 Perl_warner(aTHX_ WARN_DEPRECATED,
5395 "defined(%hash) is deprecated");
5396 Perl_warner(aTHX_ WARN_DEPRECATED,
5397 "(Maybe you should just omit the defined()?)\n");
5408 Perl_ck_rfun(pTHX_ OP *o)
5410 OPCODE type = o->op_type;
5411 return refkids(ck_fun(o), type);
5415 Perl_ck_listiob(pTHX_ OP *o)
5419 kid = cLISTOPo->op_first;
5422 kid = cLISTOPo->op_first;
5424 if (kid->op_type == OP_PUSHMARK)
5425 kid = kid->op_sibling;
5426 if (kid && o->op_flags & OPf_STACKED)
5427 kid = kid->op_sibling;
5428 else if (kid && !kid->op_sibling) { /* print HANDLE; */
5429 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
5430 o->op_flags |= OPf_STACKED; /* make it a filehandle */
5431 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
5432 cLISTOPo->op_first->op_sibling = kid;
5433 cLISTOPo->op_last = kid;
5434 kid = kid->op_sibling;
5439 append_elem(o->op_type, o, newDEFSVOP());
5445 if (PL_hints & HINT_LOCALE)
5446 o->op_private |= OPpLOCALE;
5453 Perl_ck_fun_locale(pTHX_ OP *o)
5459 if (PL_hints & HINT_LOCALE)
5460 o->op_private |= OPpLOCALE;
5467 Perl_ck_sassign(pTHX_ OP *o)
5469 OP *kid = cLISTOPo->op_first;
5470 /* has a disposable target? */
5471 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
5472 && !(kid->op_flags & OPf_STACKED))
5474 OP *kkid = kid->op_sibling;
5476 /* Can just relocate the target. */
5477 if (kkid && kkid->op_type == OP_PADSV
5478 && !(kkid->op_private & OPpLVAL_INTRO))
5480 /* Concat has problems if target is equal to right arg. */
5481 if (kid->op_type == OP_CONCAT) {
5482 if (kLISTOP->op_first->op_sibling->op_type == OP_PADSV
5483 && kLISTOP->op_first->op_sibling->op_targ == kkid->op_targ)
5486 else if (kid->op_type == OP_JOIN) {
5487 /* do_join has problems if the arguments coincide with target.
5488 In fact the second argument *can* safely coincide,
5489 but ignore=pessimize this rare occasion. */
5490 OP *arg = kLISTOP->op_first->op_sibling; /* Skip PUSHMARK */
5493 if (arg->op_type == OP_PADSV
5494 && arg->op_targ == kkid->op_targ)
5496 arg = arg->op_sibling;
5499 else if (kid->op_type == OP_QUOTEMETA) {
5500 /* quotemeta has problems if the argument coincides with target. */
5501 if (kLISTOP->op_first->op_type == OP_PADSV
5502 && kLISTOP->op_first->op_targ == kkid->op_targ)
5505 kid->op_targ = kkid->op_targ;
5506 /* Now we do not need PADSV and SASSIGN. */
5507 kid->op_sibling = o->op_sibling; /* NULL */
5508 cLISTOPo->op_first = NULL;
5511 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
5519 Perl_ck_scmp(pTHX_ OP *o)
5523 if (PL_hints & HINT_LOCALE)
5524 o->op_private |= OPpLOCALE;
5531 Perl_ck_match(pTHX_ OP *o)
5533 o->op_private |= OPpRUNTIME;
5538 Perl_ck_method(pTHX_ OP *o)
5540 OP *kid = cUNOPo->op_first;
5541 if (kid->op_type == OP_CONST) {
5542 SV* sv = kSVOP->op_sv;
5543 if (!(strchr(SvPVX(sv), ':') || strchr(SvPVX(sv), '\''))) {
5545 sv_upgrade(sv, SVt_PVIV);
5547 PERL_HASH(SvUVX(sv), SvPVX(sv), SvCUR(sv));
5548 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
5549 kSVOP->op_sv = Nullsv;
5558 Perl_ck_null(pTHX_ OP *o)
5564 Perl_ck_repeat(pTHX_ OP *o)
5566 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
5567 o->op_private |= OPpREPEAT_DOLIST;
5568 cBINOPo->op_first = force_list(cBINOPo->op_first);
5576 Perl_ck_require(pTHX_ OP *o)
5578 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
5579 SVOP *kid = (SVOP*)cUNOPo->op_first;
5581 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5583 for (s = SvPVX(kid->op_sv); *s; s++) {
5584 if (*s == ':' && s[1] == ':') {
5586 Move(s+2, s+1, strlen(s+2)+1, char);
5587 --SvCUR(kid->op_sv);
5590 sv_catpvn(kid->op_sv, ".pm", 3);
5598 Perl_ck_retarget(pTHX_ OP *o)
5600 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
5607 Perl_ck_select(pTHX_ OP *o)
5610 if (o->op_flags & OPf_KIDS) {
5611 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5612 if (kid && kid->op_sibling) {
5613 o->op_type = OP_SSELECT;
5614 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
5616 return fold_constants(o);
5620 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5621 if (kid && kid->op_type == OP_RV2GV)
5622 kid->op_private &= ~HINT_STRICT_REFS;
5627 Perl_ck_shift(pTHX_ OP *o)
5629 I32 type = o->op_type;
5631 if (!(o->op_flags & OPf_KIDS)) {
5636 if (!CvUNIQUE(PL_compcv)) {
5637 argop = newOP(OP_PADAV, OPf_REF);
5638 argop->op_targ = 0; /* PL_curpad[0] is @_ */
5641 argop = newUNOP(OP_RV2AV, 0,
5642 scalar(newGVOP(OP_GV, 0,
5643 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5646 argop = newUNOP(OP_RV2AV, 0,
5647 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(PL_compcv) ?
5648 PL_defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5649 #endif /* USE_THREADS */
5650 return newUNOP(type, 0, scalar(argop));
5652 return scalar(modkids(ck_fun(o), type));
5656 Perl_ck_sort(pTHX_ OP *o)
5660 if (PL_hints & HINT_LOCALE)
5661 o->op_private |= OPpLOCALE;
5664 if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
5666 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
5667 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5669 kid = kUNOP->op_first; /* get past null */
5671 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
5673 if (kid->op_type == OP_SCOPE) {
5677 else if (kid->op_type == OP_LEAVE) {
5678 if (o->op_type == OP_SORT) {
5679 null(kid); /* wipe out leave */
5682 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
5683 if (k->op_next == kid)
5688 kid->op_next = 0; /* just disconnect the leave */
5689 k = kLISTOP->op_first;
5693 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5694 if (o->op_type == OP_SORT)
5698 o->op_flags |= OPf_SPECIAL;
5700 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
5701 null(cLISTOPo->op_first->op_sibling);
5708 S_simplify_sort(pTHX_ OP *o)
5711 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5715 if (!(o->op_flags & OPf_STACKED))
5717 GvMULTI_on(gv_fetchpv("a", TRUE, SVt_PV));
5718 GvMULTI_on(gv_fetchpv("b", TRUE, SVt_PV));
5719 kid = kUNOP->op_first; /* get past null */
5720 if (kid->op_type != OP_SCOPE)
5722 kid = kLISTOP->op_last; /* get past scope */
5723 switch(kid->op_type) {
5731 k = kid; /* remember this node*/
5732 if (kBINOP->op_first->op_type != OP_RV2SV)
5734 kid = kBINOP->op_first; /* get past cmp */
5735 if (kUNOP->op_first->op_type != OP_GV)
5737 kid = kUNOP->op_first; /* get past rv2sv */
5739 if (GvSTASH(gv) != PL_curstash)
5741 if (strEQ(GvNAME(gv), "a"))
5743 else if(strEQ(GvNAME(gv), "b"))
5747 kid = k; /* back to cmp */
5748 if (kBINOP->op_last->op_type != OP_RV2SV)
5750 kid = kBINOP->op_last; /* down to 2nd arg */
5751 if (kUNOP->op_first->op_type != OP_GV)
5753 kid = kUNOP->op_first; /* get past rv2sv */
5755 if (GvSTASH(gv) != PL_curstash
5757 ? strNE(GvNAME(gv), "a")
5758 : strNE(GvNAME(gv), "b")))
5760 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
5762 o->op_private |= OPpSORT_REVERSE;
5763 if (k->op_type == OP_NCMP)
5764 o->op_private |= OPpSORT_NUMERIC;
5765 if (k->op_type == OP_I_NCMP)
5766 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
5767 kid = cLISTOPo->op_first->op_sibling;
5768 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
5769 op_free(kid); /* then delete it */
5770 cLISTOPo->op_children--;
5774 Perl_ck_split(pTHX_ OP *o)
5778 if (o->op_flags & OPf_STACKED)
5779 return no_fh_allowed(o);
5781 kid = cLISTOPo->op_first;
5782 if (kid->op_type != OP_NULL)
5783 Perl_croak(aTHX_ "panic: ck_split");
5784 kid = kid->op_sibling;
5785 op_free(cLISTOPo->op_first);
5786 cLISTOPo->op_first = kid;
5788 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvn(" ", 1));
5789 cLISTOPo->op_last = kid; /* There was only one element previously */
5792 if (kid->op_type != OP_MATCH) {
5793 OP *sibl = kid->op_sibling;
5794 kid->op_sibling = 0;
5795 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
5796 if (cLISTOPo->op_first == cLISTOPo->op_last)
5797 cLISTOPo->op_last = kid;
5798 cLISTOPo->op_first = kid;
5799 kid->op_sibling = sibl;
5802 kid->op_type = OP_PUSHRE;
5803 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
5806 if (!kid->op_sibling)
5807 append_elem(OP_SPLIT, o, newDEFSVOP());
5809 kid = kid->op_sibling;
5812 if (!kid->op_sibling)
5813 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
5815 kid = kid->op_sibling;
5818 if (kid->op_sibling)
5819 return too_many_arguments(o,PL_op_desc[o->op_type]);
5825 Perl_ck_join(pTHX_ OP *o)
5827 if (ckWARN(WARN_SYNTAX)) {
5828 OP *kid = cLISTOPo->op_first->op_sibling;
5829 if (kid && kid->op_type == OP_MATCH) {
5830 char *pmstr = "STRING";
5831 if (kPMOP->op_pmregexp)
5832 pmstr = kPMOP->op_pmregexp->precomp;
5833 Perl_warner(aTHX_ WARN_SYNTAX,
5834 "/%s/ should probably be written as \"%s\"",
5842 Perl_ck_subr(pTHX_ OP *o)
5845 OP *prev = ((cUNOPo->op_first->op_sibling)
5846 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
5847 OP *o2 = prev->op_sibling;
5856 o->op_private |= OPpENTERSUB_HASTARG;
5857 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
5858 if (cvop->op_type == OP_RV2CV) {
5860 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
5861 null(cvop); /* disable rv2cv */
5862 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
5863 if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) {
5864 GV *gv = cGVOPx(tmpop);
5867 tmpop->op_private |= OPpEARLY_CV;
5868 else if (SvPOK(cv)) {
5869 namegv = CvANON(cv) ? gv : CvGV(cv);
5870 proto = SvPV((SV*)cv, n_a);
5874 else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
5875 if (o2->op_type == OP_CONST)
5876 o2->op_private &= ~OPpCONST_STRICT;
5877 else if (o2->op_type == OP_LIST) {
5878 OP *o = ((UNOP*)o2)->op_first->op_sibling;
5879 if (o && o->op_type == OP_CONST)
5880 o->op_private &= ~OPpCONST_STRICT;
5883 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5884 if (PERLDB_SUB && PL_curstash != PL_debstash)
5885 o->op_private |= OPpENTERSUB_DB;
5886 while (o2 != cvop) {
5890 return too_many_arguments(o, gv_ename(namegv));
5908 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
5909 bad_type(arg, "block", gv_ename(namegv), o2);
5912 /* '*' allows any scalar type, including bareword */
5915 if (o2->op_type == OP_RV2GV)
5916 goto wrapref; /* autoconvert GLOB -> GLOBref */
5917 else if (o2->op_type == OP_CONST)
5918 o2->op_private &= ~OPpCONST_STRICT;
5919 else if (o2->op_type == OP_ENTERSUB) {
5920 /* accidental subroutine, revert to bareword */
5921 OP *gvop = ((UNOP*)o2)->op_first;
5922 if (gvop && gvop->op_type == OP_NULL) {
5923 gvop = ((UNOP*)gvop)->op_first;
5925 for (; gvop->op_sibling; gvop = gvop->op_sibling)
5928 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
5929 (gvop = ((UNOP*)gvop)->op_first) &&
5930 gvop->op_type == OP_GV)
5932 GV *gv = cGVOPx(gvop);
5933 OP *sibling = o2->op_sibling;
5934 SV *n = newSVpvn("",0);
5936 gv_fullname3(n, gv, "");
5937 if (SvCUR(n)>6 && strnEQ(SvPVX(n),"main::",6))
5938 sv_chop(n, SvPVX(n)+6);
5939 o2 = newSVOP(OP_CONST, 0, n);
5940 prev->op_sibling = o2;
5941 o2->op_sibling = sibling;
5953 if (o2->op_type != OP_RV2GV)
5954 bad_type(arg, "symbol", gv_ename(namegv), o2);
5957 if (o2->op_type != OP_RV2CV)
5958 bad_type(arg, "sub", gv_ename(namegv), o2);
5961 if (o2->op_type != OP_RV2SV
5962 && o2->op_type != OP_PADSV
5963 && o2->op_type != OP_HELEM
5964 && o2->op_type != OP_AELEM
5965 && o2->op_type != OP_THREADSV)
5967 bad_type(arg, "scalar", gv_ename(namegv), o2);
5971 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
5972 bad_type(arg, "array", gv_ename(namegv), o2);
5975 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
5976 bad_type(arg, "hash", gv_ename(namegv), o2);
5980 OP* sib = kid->op_sibling;
5981 kid->op_sibling = 0;
5982 o2 = newUNOP(OP_REFGEN, 0, kid);
5983 o2->op_sibling = sib;
5984 prev->op_sibling = o2;
5995 Perl_croak(aTHX_ "Malformed prototype for %s: %s",
5996 gv_ename(namegv), SvPV((SV*)cv, n_a));
6001 mod(o2, OP_ENTERSUB);
6003 o2 = o2->op_sibling;
6005 if (proto && !optional &&
6006 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
6007 return too_few_arguments(o, gv_ename(namegv));
6012 Perl_ck_svconst(pTHX_ OP *o)
6014 SvREADONLY_on(cSVOPo->op_sv);
6019 Perl_ck_trunc(pTHX_ OP *o)
6021 if (o->op_flags & OPf_KIDS) {
6022 SVOP *kid = (SVOP*)cUNOPo->op_first;
6024 if (kid->op_type == OP_NULL)
6025 kid = (SVOP*)kid->op_sibling;
6026 if (kid && kid->op_type == OP_CONST &&
6027 (kid->op_private & OPpCONST_BARE))
6029 o->op_flags |= OPf_SPECIAL;
6030 kid->op_private &= ~OPpCONST_STRICT;
6036 /* A peephole optimizer. We visit the ops in the order they're to execute. */
6039 Perl_peep(pTHX_ register OP *o)
6042 register OP* oldop = 0;
6044 OP *last_composite = Nullop;
6046 if (!o || o->op_seq)
6050 SAVESPTR(PL_curcop);
6051 for (; o; o = o->op_next) {
6057 switch (o->op_type) {
6061 PL_curcop = ((COP*)o); /* for warnings */
6062 o->op_seq = PL_op_seqmax++;
6063 last_composite = Nullop;
6067 if (cSVOPo->op_private & OPpCONST_STRICT)
6068 no_bareword_allowed(o);
6074 if ( o->op_next && o->op_next->op_type == OP_STRINGIFY
6075 && !(o->op_next->op_private & OPpTARGET_MY) )
6077 o->op_seq = PL_op_seqmax++;
6082 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
6083 if (o->op_next->op_private & OPpTARGET_MY) {
6084 if ((o->op_flags & OPf_STACKED) /* chained concats */
6085 || (o->op_type == OP_CONCAT
6086 /* Concat has problems if target is equal to right arg. */
6087 && (((LISTOP*)o)->op_first->op_sibling->op_type
6089 && (((LISTOP*)o)->op_first->op_sibling->op_targ
6090 == o->op_next->op_targ))) {
6091 goto ignore_optimization;
6094 o->op_targ = o->op_next->op_targ;
6095 o->op_private |= OPpTARGET_MY;
6100 ignore_optimization:
6101 o->op_seq = PL_op_seqmax++;
6104 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
6105 o->op_seq = PL_op_seqmax++;
6106 break; /* Scalar stub must produce undef. List stub is noop */
6110 if (o->op_targ == OP_NEXTSTATE
6111 || o->op_targ == OP_DBSTATE
6112 || o->op_targ == OP_SETSTATE)
6114 PL_curcop = ((COP*)o);
6121 if (oldop && o->op_next) {
6122 oldop->op_next = o->op_next;
6125 o->op_seq = PL_op_seqmax++;
6129 if (o->op_next->op_type == OP_RV2SV) {
6130 if (!(o->op_next->op_private & OPpDEREF)) {
6132 o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
6133 o->op_next = o->op_next->op_next;
6134 o->op_type = OP_GVSV;
6135 o->op_ppaddr = PL_ppaddr[OP_GVSV];
6138 else if (o->op_next->op_type == OP_RV2AV) {
6139 OP* pop = o->op_next->op_next;
6141 if (pop->op_type == OP_CONST &&
6142 (PL_op = pop->op_next) &&
6143 pop->op_next->op_type == OP_AELEM &&
6144 !(pop->op_next->op_private &
6145 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
6146 (i = SvIV(((SVOP*)pop)->op_sv) - PL_compiling.cop_arybase)
6154 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
6155 o->op_next = pop->op_next->op_next;
6156 o->op_type = OP_AELEMFAST;
6157 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
6158 o->op_private = (U8)i;
6163 else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_UNSAFE)) {
6165 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX(GvCV(gv))) {
6166 /* XXX could check prototype here instead of just carping */
6167 SV *sv = sv_newmortal();
6168 gv_efullname3(sv, gv, Nullch);
6169 Perl_warner(aTHX_ WARN_UNSAFE,
6170 "%s() called too early to check prototype",
6175 o->op_seq = PL_op_seqmax++;
6186 o->op_seq = PL_op_seqmax++;
6187 while (cLOGOP->op_other->op_type == OP_NULL)
6188 cLOGOP->op_other = cLOGOP->op_other->op_next;
6189 peep(cLOGOP->op_other);
6193 o->op_seq = PL_op_seqmax++;
6194 peep(cLOOP->op_redoop);
6195 peep(cLOOP->op_nextop);
6196 peep(cLOOP->op_lastop);
6202 o->op_seq = PL_op_seqmax++;
6203 peep(cPMOP->op_pmreplstart);
6207 o->op_seq = PL_op_seqmax++;
6208 if (ckWARN(WARN_SYNTAX) && o->op_next
6209 && o->op_next->op_type == OP_NEXTSTATE) {
6210 if (o->op_next->op_sibling &&
6211 o->op_next->op_sibling->op_type != OP_EXIT &&
6212 o->op_next->op_sibling->op_type != OP_WARN &&
6213 o->op_next->op_sibling->op_type != OP_DIE) {
6214 line_t oldline = PL_curcop->cop_line;
6216 PL_curcop->cop_line = ((COP*)o->op_next)->cop_line;
6217 Perl_warner(aTHX_ WARN_SYNTAX, "Statement unlikely to be reached");
6218 Perl_warner(aTHX_ WARN_SYNTAX, "(Maybe you meant system() when you said exec()?)\n");
6219 PL_curcop->cop_line = oldline;
6233 if ((o->op_private & (OPpLVAL_INTRO))
6234 || ((BINOP*)o)->op_last->op_type != OP_CONST)
6236 rop = (UNOP*)((BINOP*)o)->op_first;
6237 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6239 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6240 if (!SvOBJECT(lexname))
6242 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6243 if (!fields || !GvHV(*fields))
6245 svp = &((SVOP*)((BINOP*)o)->op_last)->op_sv;
6246 key = SvPV(*svp, keylen);
6247 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
6249 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" in variable %s of type %s",
6250 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
6252 ind = SvIV(*indsvp);
6254 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6255 rop->op_type = OP_RV2AV;
6256 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6257 o->op_type = OP_AELEM;
6258 o->op_ppaddr = PL_ppaddr[OP_AELEM];
6260 *svp = newSViv(ind);
6266 if (!(o->op_flags & OPf_WANT)
6267 || (o->op_flags & OPf_WANT) == OPf_WANT_LIST)
6271 o->op_seq = PL_op_seqmax++;
6275 if (o->op_next && o->op_next->op_type != OP_LEAVESUBLV) {
6276 o->op_seq = PL_op_seqmax++;
6282 if (last_composite) {
6283 OP *r = last_composite;
6285 while (r->op_sibling)
6288 || (r->op_next->op_type == OP_LIST
6289 && r->op_next->op_next == o))
6291 if (last_composite->op_type == OP_RV2AV)
6292 yyerror("Lvalue subs returning arrays not implemented yet");
6294 yyerror("Lvalue subs returning hashes not implemented yet");
6301 o->op_seq = PL_op_seqmax++;