3 * Copyright (c) 1991-2000, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
12 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
13 * youngest of the Old Took's daughters); and Mr. Drogo was his second
14 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
15 * either way, as the saying is, if you follow me." --the Gaffer
23 /* #define PL_OP_SLAB_ALLOC */
25 #ifdef PL_OP_SLAB_ALLOC
26 #define SLAB_SIZE 8192
27 static char *PL_OpPtr = NULL;
28 static int PL_OpSpace = 0;
29 #define NewOp(m,var,c,type) do { if ((PL_OpSpace -= c*sizeof(type)) >= 0) \
30 var = (type *)(PL_OpPtr -= c*sizeof(type)); \
32 var = (type *) Slab_Alloc(m,c*sizeof(type)); \
36 S_Slab_Alloc(pTHX_ int m, size_t sz)
38 Newz(m,PL_OpPtr,SLAB_SIZE,char);
39 PL_OpSpace = SLAB_SIZE - sz;
40 return PL_OpPtr += PL_OpSpace;
44 #define NewOp(m, var, c, type) Newz(m, var, c, type)
47 * In the following definition, the ", Nullop" is just to make the compiler
48 * think the expression is of the right type: croak actually does a Siglongjmp.
50 #define CHECKOP(type,o) \
51 ((PL_op_mask && PL_op_mask[type]) \
52 ? ( op_free((OP*)o), \
53 Perl_croak(aTHX_ "%s trapped by operation mask", PL_op_desc[type]), \
55 : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o))
57 #define PAD_MAX 999999999
60 S_gv_ename(pTHX_ GV *gv)
63 SV* tmpsv = sv_newmortal();
64 gv_efullname3(tmpsv, gv, Nullch);
65 return SvPV(tmpsv,n_a);
69 S_no_fh_allowed(pTHX_ OP *o)
71 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
72 PL_op_desc[o->op_type]));
77 S_too_few_arguments(pTHX_ OP *o, char *name)
79 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
84 S_too_many_arguments(pTHX_ OP *o, char *name)
86 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
91 S_bad_type(pTHX_ I32 n, char *t, char *name, OP *kid)
93 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
94 (int)n, name, t, PL_op_desc[kid->op_type]));
98 S_no_bareword_allowed(pTHX_ OP *o)
100 qerror(Perl_mess(aTHX_
101 "Bareword \"%s\" not allowed while \"strict subs\" in use",
102 SvPV_nolen(cSVOPo_sv)));
105 /* "register" allocation */
108 Perl_pad_allocmy(pTHX_ char *name)
114 if (!(PL_in_my == KEY_our ||
116 (PL_hints & HINT_UTF8 && (name[1] & 0xc0) == 0xc0) ||
117 (name[1] == '_' && (int)strlen(name) > 2)))
119 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
120 /* 1999-02-27 mjd@plover.com */
122 p = strchr(name, '\0');
123 /* The next block assumes the buffer is at least 205 chars
124 long. At present, it's always at least 256 chars. */
126 strcpy(name+200, "...");
132 /* Move everything else down one character */
133 for (; p-name > 2; p--)
135 name[2] = toCTRL(name[1]);
138 yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
140 if (ckWARN(WARN_MISC) && AvFILLp(PL_comppad_name) >= 0) {
141 SV **svp = AvARRAY(PL_comppad_name);
142 HV *ourstash = (PL_curstash ? PL_curstash : PL_defstash);
143 PADOFFSET top = AvFILLp(PL_comppad_name);
144 for (off = top; off > PL_comppad_name_floor; off--) {
146 && sv != &PL_sv_undef
147 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
148 && (PL_in_my != KEY_our
149 || ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash))
150 && strEQ(name, SvPVX(sv)))
152 Perl_warner(aTHX_ WARN_MISC,
153 "\"%s\" variable %s masks earlier declaration in same %s",
154 (PL_in_my == KEY_our ? "our" : "my"),
156 (SvIVX(sv) == PAD_MAX ? "scope" : "statement"));
161 if (PL_in_my == KEY_our) {
164 && sv != &PL_sv_undef
165 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
166 && ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash)
167 && strEQ(name, SvPVX(sv)))
169 Perl_warner(aTHX_ WARN_MISC,
170 "\"our\" variable %s redeclared", name);
171 Perl_warner(aTHX_ WARN_MISC,
172 "\t(Did you mean \"local\" instead of \"our\"?)\n");
175 } while ( off-- > 0 );
178 off = pad_alloc(OP_PADSV, SVs_PADMY);
180 sv_upgrade(sv, SVt_PVNV);
182 if (PL_in_my_stash) {
184 yyerror(Perl_form(aTHX_ "Can't declare class for non-scalar %s in \"%s\"",
185 name, PL_in_my == KEY_our ? "our" : "my"));
187 (void)SvUPGRADE(sv, SVt_PVMG);
188 SvSTASH(sv) = (HV*)SvREFCNT_inc(PL_in_my_stash);
191 if (PL_in_my == KEY_our) {
192 (void)SvUPGRADE(sv, SVt_PVGV);
193 GvSTASH(sv) = (HV*)SvREFCNT_inc(PL_curstash ? (SV*)PL_curstash : (SV*)PL_defstash);
194 SvFLAGS(sv) |= SVpad_OUR;
196 av_store(PL_comppad_name, off, sv);
197 SvNVX(sv) = (NV)PAD_MAX;
198 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
199 if (!PL_min_intro_pending)
200 PL_min_intro_pending = off;
201 PL_max_intro_pending = off;
203 av_store(PL_comppad, off, (SV*)newAV());
204 else if (*name == '%')
205 av_store(PL_comppad, off, (SV*)newHV());
206 SvPADMY_on(PL_curpad[off]);
211 S_pad_addlex(pTHX_ SV *proto_namesv)
213 SV *namesv = NEWSV(1103,0);
214 PADOFFSET newoff = pad_alloc(OP_PADSV, SVs_PADMY);
215 sv_upgrade(namesv, SVt_PVNV);
216 sv_setpv(namesv, SvPVX(proto_namesv));
217 av_store(PL_comppad_name, newoff, namesv);
218 SvNVX(namesv) = (NV)PL_curcop->cop_seq;
219 SvIVX(namesv) = PAD_MAX; /* A ref, intro immediately */
220 SvFAKE_on(namesv); /* A ref, not a real var */
221 if (SvFLAGS(proto_namesv) & SVpad_OUR) { /* An "our" variable */
222 SvFLAGS(namesv) |= SVpad_OUR;
223 (void)SvUPGRADE(namesv, SVt_PVGV);
224 GvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)GvSTASH(proto_namesv));
226 if (SvOBJECT(proto_namesv)) { /* A typed var */
228 (void)SvUPGRADE(namesv, SVt_PVMG);
229 SvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)SvSTASH(proto_namesv));
235 #define FINDLEX_NOSEARCH 1 /* don't search outer contexts */
238 S_pad_findlex(pTHX_ char *name, PADOFFSET newoff, U32 seq, CV* startcv,
239 I32 cx_ix, I32 saweval, U32 flags)
246 register PERL_CONTEXT *cx;
248 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
249 AV *curlist = CvPADLIST(cv);
250 SV **svp = av_fetch(curlist, 0, FALSE);
253 if (!svp || *svp == &PL_sv_undef)
256 svp = AvARRAY(curname);
257 for (off = AvFILLp(curname); off > 0; off--) {
258 if ((sv = svp[off]) &&
259 sv != &PL_sv_undef &&
261 seq > I_32(SvNVX(sv)) &&
262 strEQ(SvPVX(sv), name))
273 return 0; /* don't clone from inactive stack frame */
277 oldpad = (AV*)AvARRAY(curlist)[depth];
278 oldsv = *av_fetch(oldpad, off, TRUE);
279 if (!newoff) { /* Not a mere clone operation. */
280 newoff = pad_addlex(sv);
281 if (CvANON(PL_compcv) || SvTYPE(PL_compcv) == SVt_PVFM) {
282 /* "It's closures all the way down." */
283 CvCLONE_on(PL_compcv);
285 if (CvANON(PL_compcv))
286 oldsv = Nullsv; /* no need to keep ref */
291 bcv && bcv != cv && !CvCLONE(bcv);
292 bcv = CvOUTSIDE(bcv))
295 /* install the missing pad entry in intervening
296 * nested subs and mark them cloneable.
297 * XXX fix pad_foo() to not use globals */
298 AV *ocomppad_name = PL_comppad_name;
299 AV *ocomppad = PL_comppad;
300 SV **ocurpad = PL_curpad;
301 AV *padlist = CvPADLIST(bcv);
302 PL_comppad_name = (AV*)AvARRAY(padlist)[0];
303 PL_comppad = (AV*)AvARRAY(padlist)[1];
304 PL_curpad = AvARRAY(PL_comppad);
306 PL_comppad_name = ocomppad_name;
307 PL_comppad = ocomppad;
312 if (ckWARN(WARN_CLOSURE)
313 && !CvUNIQUE(bcv) && !CvUNIQUE(cv))
315 Perl_warner(aTHX_ WARN_CLOSURE,
316 "Variable \"%s\" may be unavailable",
324 else if (!CvUNIQUE(PL_compcv)) {
325 if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv)
326 && !(SvFLAGS(sv) & SVpad_OUR))
328 Perl_warner(aTHX_ WARN_CLOSURE,
329 "Variable \"%s\" will not stay shared", name);
333 av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv));
339 if (flags & FINDLEX_NOSEARCH)
342 /* Nothing in current lexical context--try eval's context, if any.
343 * This is necessary to let the perldb get at lexically scoped variables.
344 * XXX This will also probably interact badly with eval tree caching.
347 for (i = cx_ix; i >= 0; i--) {
349 switch (CxTYPE(cx)) {
351 if (i == 0 && saweval) {
352 seq = cxstack[saweval].blk_oldcop->cop_seq;
353 return pad_findlex(name, newoff, seq, PL_main_cv, -1, saweval, 0);
357 switch (cx->blk_eval.old_op_type) {
364 /* require/do must have their own scope */
373 if (PL_debstash && CvSTASH(cv) == PL_debstash) { /* ignore DB'* scope */
374 saweval = i; /* so we know where we were called from */
377 seq = cxstack[saweval].blk_oldcop->cop_seq;
378 return pad_findlex(name, newoff, seq, cv, i-1, saweval,FINDLEX_NOSEARCH);
386 Perl_pad_findmy(pTHX_ char *name)
392 SV **svp = AvARRAY(PL_comppad_name);
393 U32 seq = PL_cop_seqmax;
399 * Special case to get lexical (and hence per-thread) @_.
400 * XXX I need to find out how to tell at parse-time whether use
401 * of @_ should refer to a lexical (from a sub) or defgv (global
402 * scope and maybe weird sub-ish things like formats). See
403 * startsub in perly.y. It's possible that @_ could be lexical
404 * (at least from subs) even in non-threaded perl.
406 if (strEQ(name, "@_"))
407 return 0; /* success. (NOT_IN_PAD indicates failure) */
408 #endif /* USE_THREADS */
410 /* The one we're looking for is probably just before comppad_name_fill. */
411 for (off = AvFILLp(PL_comppad_name); off > 0; off--) {
412 if ((sv = svp[off]) &&
413 sv != &PL_sv_undef &&
416 seq > I_32(SvNVX(sv)))) &&
417 strEQ(SvPVX(sv), name))
419 if (SvIVX(sv) || SvFLAGS(sv) & SVpad_OUR)
420 return (PADOFFSET)off;
421 pendoff = off; /* this pending def. will override import */
425 outside = CvOUTSIDE(PL_compcv);
427 /* Check if if we're compiling an eval'', and adjust seq to be the
428 * eval's seq number. This depends on eval'' having a non-null
429 * CvOUTSIDE() while it is being compiled. The eval'' itself is
430 * identified by CvEVAL being true and CvGV being null. */
431 if (outside && CvEVAL(PL_compcv) && !CvGV(PL_compcv) && cxstack_ix >= 0) {
432 cx = &cxstack[cxstack_ix];
434 seq = cx->blk_oldcop->cop_seq;
437 /* See if it's in a nested scope */
438 off = pad_findlex(name, 0, seq, outside, cxstack_ix, 0, 0);
440 /* If there is a pending local definition, this new alias must die */
442 SvIVX(AvARRAY(PL_comppad_name)[off]) = seq;
443 return off; /* pad_findlex returns 0 for failure...*/
445 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
449 Perl_pad_leavemy(pTHX_ I32 fill)
453 SV **svp = AvARRAY(PL_comppad_name);
455 if (PL_min_intro_pending && fill < PL_min_intro_pending) {
456 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
457 if ((sv = svp[off]) && sv != &PL_sv_undef && ckWARN_d(WARN_INTERNAL))
458 Perl_warner(aTHX_ WARN_INTERNAL, "%s never introduced", SvPVX(sv));
461 /* "Deintroduce" my variables that are leaving with this scope. */
462 for (off = AvFILLp(PL_comppad_name); off > fill; off--) {
463 if ((sv = svp[off]) && sv != &PL_sv_undef && SvIVX(sv) == PAD_MAX)
464 SvIVX(sv) = PL_cop_seqmax;
469 Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype)
475 if (AvARRAY(PL_comppad) != PL_curpad)
476 Perl_croak(aTHX_ "panic: pad_alloc");
477 if (PL_pad_reset_pending)
479 if (tmptype & SVs_PADMY) {
481 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
482 } while (SvPADBUSY(sv)); /* need a fresh one */
483 retval = AvFILLp(PL_comppad);
486 SV **names = AvARRAY(PL_comppad_name);
487 SSize_t names_fill = AvFILLp(PL_comppad_name);
490 * "foreach" index vars temporarily become aliases to non-"my"
491 * values. Thus we must skip, not just pad values that are
492 * marked as current pad values, but also those with names.
494 if (++PL_padix <= names_fill &&
495 (sv = names[PL_padix]) && sv != &PL_sv_undef)
497 sv = *av_fetch(PL_comppad, PL_padix, TRUE);
498 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)) && !IS_PADGV(sv))
503 SvFLAGS(sv) |= tmptype;
504 PL_curpad = AvARRAY(PL_comppad);
506 DEBUG_X(PerlIO_printf(Perl_debug_log,
507 "0x%"UVxf" Pad 0x%"UVxf" alloc %ld for %s\n",
508 PTR2UV(thr), PTR2UV(PL_curpad),
509 (long) retval, PL_op_name[optype]));
511 DEBUG_X(PerlIO_printf(Perl_debug_log,
512 "Pad 0x%"UVxf" alloc %ld for %s\n",
514 (long) retval, PL_op_name[optype]));
515 #endif /* USE_THREADS */
516 return (PADOFFSET)retval;
520 Perl_pad_sv(pTHX_ PADOFFSET po)
524 DEBUG_X(PerlIO_printf(Perl_debug_log,
525 "0x%"UVxf" Pad 0x%"UVxf" sv %"IVdf"\n",
526 PTR2UV(thr), PTR2UV(PL_curpad), (IV)po));
529 Perl_croak(aTHX_ "panic: pad_sv po");
530 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" sv %"IVdf"\n",
531 PTR2UV(PL_curpad), (IV)po));
532 #endif /* USE_THREADS */
533 return PL_curpad[po]; /* eventually we'll turn this into a macro */
537 Perl_pad_free(pTHX_ PADOFFSET po)
542 if (AvARRAY(PL_comppad) != PL_curpad)
543 Perl_croak(aTHX_ "panic: pad_free curpad");
545 Perl_croak(aTHX_ "panic: pad_free po");
547 DEBUG_X(PerlIO_printf(Perl_debug_log,
548 "0x%"UVxf" Pad 0x%"UVxf" free %"IVdf"\n",
549 PTR2UV(thr), PTR2UV(PL_curpad), (IV)po));
551 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" free %"IVdf"\n",
552 PTR2UV(PL_curpad), (IV)po));
553 #endif /* USE_THREADS */
554 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef) {
555 SvPADTMP_off(PL_curpad[po]);
557 SvREADONLY_off(PL_curpad[po]); /* could be a freed constant */
560 if ((I32)po < PL_padix)
565 Perl_pad_swipe(pTHX_ PADOFFSET po)
568 if (AvARRAY(PL_comppad) != PL_curpad)
569 Perl_croak(aTHX_ "panic: pad_swipe curpad");
571 Perl_croak(aTHX_ "panic: pad_swipe po");
573 DEBUG_X(PerlIO_printf(Perl_debug_log,
574 "0x%"UVxf" Pad 0x%"UVxf" swipe %"IVdf"\n",
575 PTR2UV(thr), PTR2UV(PL_curpad), (IV)po));
577 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" swipe %"IVdf"\n",
578 PTR2UV(PL_curpad), (IV)po));
579 #endif /* USE_THREADS */
580 SvPADTMP_off(PL_curpad[po]);
581 PL_curpad[po] = NEWSV(1107,0);
582 SvPADTMP_on(PL_curpad[po]);
583 if ((I32)po < PL_padix)
587 /* XXX pad_reset() is currently disabled because it results in serious bugs.
588 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
589 * on the stack by OPs that use them, there are several ways to get an alias
590 * to a shared TARG. Such an alias will change randomly and unpredictably.
591 * We avoid doing this until we can think of a Better Way.
596 #ifdef USE_BROKEN_PAD_RESET
600 if (AvARRAY(PL_comppad) != PL_curpad)
601 Perl_croak(aTHX_ "panic: pad_reset curpad");
603 DEBUG_X(PerlIO_printf(Perl_debug_log,
604 "0x%"UVxf" Pad 0x%"UVxf" reset\n",
605 PTR2UV(thr), PTR2UV(PL_curpad)));
607 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" reset\n",
609 #endif /* USE_THREADS */
610 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
611 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
612 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
613 SvPADTMP_off(PL_curpad[po]);
615 PL_padix = PL_padix_floor;
618 PL_pad_reset_pending = FALSE;
622 /* find_threadsv is not reentrant */
624 Perl_find_threadsv(pTHX_ const char *name)
630 /* We currently only handle names of a single character */
631 p = strchr(PL_threadsv_names, *name);
634 key = p - PL_threadsv_names;
635 MUTEX_LOCK(&thr->mutex);
636 svp = av_fetch(thr->threadsv, key, FALSE);
638 MUTEX_UNLOCK(&thr->mutex);
640 SV *sv = NEWSV(0, 0);
641 av_store(thr->threadsv, key, sv);
642 thr->threadsvp = AvARRAY(thr->threadsv);
643 MUTEX_UNLOCK(&thr->mutex);
645 * Some magic variables used to be automagically initialised
646 * in gv_fetchpv. Those which are now per-thread magicals get
647 * initialised here instead.
653 sv_setpv(sv, "\034");
654 sv_magic(sv, 0, 0, name, 1);
659 PL_sawampersand = TRUE;
673 /* XXX %! tied to Errno.pm needs to be added here.
674 * See gv_fetchpv(). */
678 sv_magic(sv, 0, 0, name, 1);
680 DEBUG_S(PerlIO_printf(Perl_error_log,
681 "find_threadsv: new SV %p for $%s%c\n",
682 sv, (*name < 32) ? "^" : "",
683 (*name < 32) ? toCTRL(*name) : *name));
687 #endif /* USE_THREADS */
692 Perl_op_free(pTHX_ OP *o)
694 register OP *kid, *nextkid;
697 if (!o || o->op_seq == (U16)-1)
700 if (o->op_private & OPpREFCOUNTED) {
701 switch (o->op_type) {
709 if (OpREFCNT_dec(o)) {
720 if (o->op_flags & OPf_KIDS) {
721 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
722 nextkid = kid->op_sibling; /* Get before next freeing kid */
730 /* COP* is not cleared by op_clear() so that we may track line
731 * numbers etc even after null() */
732 if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
737 #ifdef PL_OP_SLAB_ALLOC
738 if ((char *) o == PL_OpPtr)
747 S_op_clear(pTHX_ OP *o)
749 switch (o->op_type) {
750 case OP_NULL: /* Was holding old type, if any. */
751 case OP_ENTEREVAL: /* Was holding hints. */
753 case OP_THREADSV: /* Was holding index into thr->threadsv AV. */
759 if (!(o->op_flags & OPf_SPECIAL))
762 #endif /* USE_THREADS */
764 if (!(o->op_flags & OPf_REF)
765 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
772 if (cPADOPo->op_padix > 0) {
775 pad_swipe(cPADOPo->op_padix);
776 /* No GvIN_PAD_off(gv) here, because other references may still
777 * exist on the pad */
780 cPADOPo->op_padix = 0;
783 SvREFCNT_dec(cSVOPo->op_sv);
784 cSVOPo->op_sv = Nullsv;
787 case OP_METHOD_NAMED:
789 SvREFCNT_dec(cSVOPo->op_sv);
790 cSVOPo->op_sv = Nullsv;
796 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
800 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
801 SvREFCNT_dec(cSVOPo->op_sv);
802 cSVOPo->op_sv = Nullsv;
805 Safefree(cPVOPo->op_pv);
806 cPVOPo->op_pv = Nullch;
810 op_free(cPMOPo->op_pmreplroot);
814 if ((PADOFFSET)cPMOPo->op_pmreplroot) {
816 GV *gv = (GV*)PL_curpad[(PADOFFSET)cPMOPo->op_pmreplroot];
817 pad_swipe((PADOFFSET)cPMOPo->op_pmreplroot);
818 /* No GvIN_PAD_off(gv) here, because other references may still
819 * exist on the pad */
824 SvREFCNT_dec((SV*)cPMOPo->op_pmreplroot);
830 cPMOPo->op_pmreplroot = Nullop;
831 ReREFCNT_dec(cPMOPo->op_pmregexp);
832 cPMOPo->op_pmregexp = (REGEXP*)NULL;
836 if (o->op_targ > 0) {
837 pad_free(o->op_targ);
843 S_cop_free(pTHX_ COP* cop)
845 Safefree(cop->cop_label);
847 Safefree(CopFILE(cop)); /* XXX share in a pvtable? */
848 Safefree(CopSTASHPV(cop)); /* XXX share in a pvtable? */
850 /* NOTE: COP.cop_stash is not refcounted */
851 SvREFCNT_dec(CopFILEGV(cop));
853 if (! specialWARN(cop->cop_warnings))
854 SvREFCNT_dec(cop->cop_warnings);
860 if (o->op_type == OP_NULL)
863 o->op_targ = o->op_type;
864 o->op_type = OP_NULL;
865 o->op_ppaddr = PL_ppaddr[OP_NULL];
868 /* Contextualizers */
870 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
873 Perl_linklist(pTHX_ OP *o)
880 /* establish postfix order */
881 if (cUNOPo->op_first) {
882 o->op_next = LINKLIST(cUNOPo->op_first);
883 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
885 kid->op_next = LINKLIST(kid->op_sibling);
897 Perl_scalarkids(pTHX_ OP *o)
900 if (o && o->op_flags & OPf_KIDS) {
901 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
908 S_scalarboolean(pTHX_ OP *o)
910 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
912 if (ckWARN(WARN_SYNTAX)) {
913 line_t oldline = CopLINE(PL_curcop);
915 if (PL_copline != NOLINE)
916 CopLINE_set(PL_curcop, PL_copline);
917 Perl_warner(aTHX_ WARN_SYNTAX, "Found = in conditional, should be ==");
918 CopLINE_set(PL_curcop, oldline);
925 Perl_scalar(pTHX_ OP *o)
929 /* assumes no premature commitment */
930 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
931 || o->op_type == OP_RETURN)
936 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
938 switch (o->op_type) {
940 if (o->op_private & OPpREPEAT_DOLIST)
941 null(((LISTOP*)cBINOPo->op_first)->op_first);
942 scalar(cBINOPo->op_first);
947 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
951 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
952 if (!kPMOP->op_pmreplroot)
953 deprecate("implicit split to @_");
961 if (o->op_flags & OPf_KIDS) {
962 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
968 kid = cLISTOPo->op_first;
970 while ((kid = kid->op_sibling)) {
976 WITH_THR(PL_curcop = &PL_compiling);
981 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
987 WITH_THR(PL_curcop = &PL_compiling);
994 Perl_scalarvoid(pTHX_ OP *o)
1001 if (o->op_type == OP_NEXTSTATE
1002 || o->op_type == OP_SETSTATE
1003 || o->op_type == OP_DBSTATE
1004 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1005 || o->op_targ == OP_SETSTATE
1006 || o->op_targ == OP_DBSTATE)))
1009 PL_curcop = (COP*)o; /* for warning below */
1012 /* assumes no premature commitment */
1013 want = o->op_flags & OPf_WANT;
1014 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
1015 || o->op_type == OP_RETURN)
1020 if ((o->op_private & OPpTARGET_MY)
1021 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1023 return scalar(o); /* As if inside SASSIGN */
1026 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1028 switch (o->op_type) {
1030 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1034 if (o->op_flags & OPf_STACKED)
1038 if (o->op_private == 4)
1080 case OP_GETSOCKNAME:
1081 case OP_GETPEERNAME:
1086 case OP_GETPRIORITY:
1109 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1110 useless = PL_op_desc[o->op_type];
1117 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1118 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1119 useless = "a variable";
1124 if (cSVOPo->op_private & OPpCONST_STRICT)
1125 no_bareword_allowed(o);
1128 if (ckWARN(WARN_VOID)) {
1129 useless = "a constant";
1130 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1132 else if (SvPOK(sv)) {
1133 if (strnEQ(SvPVX(sv), "di", 2) ||
1134 strnEQ(SvPVX(sv), "ds", 2) ||
1135 strnEQ(SvPVX(sv), "ig", 2))
1140 null(o); /* don't execute or even remember it */
1144 o->op_type = OP_PREINC; /* pre-increment is faster */
1145 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1149 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1150 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1156 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1161 if (o->op_flags & OPf_STACKED)
1168 if (!(o->op_flags & OPf_KIDS))
1177 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1184 /* all requires must return a boolean value */
1185 o->op_flags &= ~OPf_WANT;
1190 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
1191 if (!kPMOP->op_pmreplroot)
1192 deprecate("implicit split to @_");
1198 if (ckWARN(WARN_VOID))
1199 Perl_warner(aTHX_ WARN_VOID, "Useless use of %s in void context", useless);
1205 Perl_listkids(pTHX_ OP *o)
1208 if (o && o->op_flags & OPf_KIDS) {
1209 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1216 Perl_list(pTHX_ OP *o)
1220 /* assumes no premature commitment */
1221 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
1222 || o->op_type == OP_RETURN)
1227 if ((o->op_private & OPpTARGET_MY)
1228 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1230 return o; /* As if inside SASSIGN */
1233 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1235 switch (o->op_type) {
1238 list(cBINOPo->op_first);
1243 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1251 if (!(o->op_flags & OPf_KIDS))
1253 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1254 list(cBINOPo->op_first);
1255 return gen_constant_list(o);
1262 kid = cLISTOPo->op_first;
1264 while ((kid = kid->op_sibling)) {
1265 if (kid->op_sibling)
1270 WITH_THR(PL_curcop = &PL_compiling);
1274 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1275 if (kid->op_sibling)
1280 WITH_THR(PL_curcop = &PL_compiling);
1283 /* all requires must return a boolean value */
1284 o->op_flags &= ~OPf_WANT;
1291 Perl_scalarseq(pTHX_ OP *o)
1296 if (o->op_type == OP_LINESEQ ||
1297 o->op_type == OP_SCOPE ||
1298 o->op_type == OP_LEAVE ||
1299 o->op_type == OP_LEAVETRY)
1302 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1303 if (kid->op_sibling) {
1307 PL_curcop = &PL_compiling;
1309 o->op_flags &= ~OPf_PARENS;
1310 if (PL_hints & HINT_BLOCK_SCOPE)
1311 o->op_flags |= OPf_PARENS;
1314 o = newOP(OP_STUB, 0);
1319 S_modkids(pTHX_ OP *o, I32 type)
1322 if (o && o->op_flags & OPf_KIDS) {
1323 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1330 Perl_mod(pTHX_ OP *o, I32 type)
1336 if (!o || PL_error_count)
1339 if ((o->op_private & OPpTARGET_MY)
1340 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1345 switch (o->op_type) {
1350 if (!(o->op_private & (OPpCONST_ARYBASE)))
1352 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1353 PL_compiling.cop_arybase = (I32)SvIV(cSVOPx(PL_eval_start)->op_sv);
1357 SAVEI32(PL_compiling.cop_arybase);
1358 PL_compiling.cop_arybase = 0;
1360 else if (type == OP_REFGEN)
1363 Perl_croak(aTHX_ "That use of $[ is unsupported");
1366 if (o->op_flags & OPf_PARENS)
1370 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1371 !(o->op_flags & OPf_STACKED)) {
1372 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1373 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1374 assert(cUNOPo->op_first->op_type == OP_NULL);
1375 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1378 else { /* lvalue subroutine call */
1379 o->op_private |= OPpLVAL_INTRO;
1380 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
1381 /* Backward compatibility mode: */
1382 o->op_private |= OPpENTERSUB_INARGS;
1385 else { /* Compile-time error message: */
1386 OP *kid = cUNOPo->op_first;
1390 if (kid->op_type == OP_PUSHMARK)
1392 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1394 "panic: unexpected lvalue entersub "
1395 "args: type/targ %ld:%ld",
1396 (long)kid->op_type,kid->op_targ);
1397 kid = kLISTOP->op_first;
1399 while (kid->op_sibling)
1400 kid = kid->op_sibling;
1401 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1403 if (kid->op_type == OP_METHOD_NAMED
1404 || kid->op_type == OP_METHOD)
1408 if (kid->op_sibling || kid->op_next != kid) {
1409 yyerror("panic: unexpected optree near method call");
1413 NewOp(1101, newop, 1, UNOP);
1414 newop->op_type = OP_RV2CV;
1415 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1416 newop->op_first = Nullop;
1417 newop->op_next = (OP*)newop;
1418 kid->op_sibling = (OP*)newop;
1419 newop->op_private |= OPpLVAL_INTRO;
1423 if (kid->op_type != OP_RV2CV)
1425 "panic: unexpected lvalue entersub "
1426 "entry via type/targ %ld:%ld",
1427 (long)kid->op_type,kid->op_targ);
1428 kid->op_private |= OPpLVAL_INTRO;
1429 break; /* Postpone until runtime */
1433 kid = kUNOP->op_first;
1434 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1435 kid = kUNOP->op_first;
1436 if (kid->op_type == OP_NULL)
1438 "Unexpected constant lvalue entersub "
1439 "entry via type/targ %ld:%ld",
1440 (long)kid->op_type,kid->op_targ);
1441 if (kid->op_type != OP_GV) {
1442 /* Restore RV2CV to check lvalueness */
1444 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1445 okid->op_next = kid->op_next;
1446 kid->op_next = okid;
1449 okid->op_next = Nullop;
1450 okid->op_type = OP_RV2CV;
1452 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1453 okid->op_private |= OPpLVAL_INTRO;
1457 cv = GvCV(kGVOP_gv);
1467 /* grep, foreach, subcalls, refgen */
1468 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1470 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1471 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1473 : (o->op_type == OP_ENTERSUB
1474 ? "non-lvalue subroutine call"
1475 : PL_op_desc[o->op_type])),
1476 type ? PL_op_desc[type] : "local"));
1490 case OP_RIGHT_SHIFT:
1499 if (!(o->op_flags & OPf_STACKED))
1505 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1511 if (!type && cUNOPo->op_first->op_type != OP_GV)
1512 Perl_croak(aTHX_ "Can't localize through a reference");
1513 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1514 PL_modcount = 10000;
1515 return o; /* Treat \(@foo) like ordinary list. */
1519 if (scalar_mod_type(o, type))
1521 ref(cUNOPo->op_first, o->op_type);
1530 PL_modcount = 10000;
1533 if (!type && cUNOPo->op_first->op_type != OP_GV)
1534 Perl_croak(aTHX_ "Can't localize through a reference");
1535 ref(cUNOPo->op_first, o->op_type);
1539 PL_hints |= HINT_BLOCK_SCOPE;
1549 PL_modcount = 10000;
1550 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1551 return o; /* Treat \(@foo) like ordinary list. */
1552 if (scalar_mod_type(o, type))
1558 Perl_croak(aTHX_ "Can't localize lexical variable %s",
1559 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
1564 PL_modcount++; /* XXX ??? */
1566 #endif /* USE_THREADS */
1572 if (type != OP_SASSIGN)
1576 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1582 pad_free(o->op_targ);
1583 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1584 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1585 if (o->op_flags & OPf_KIDS)
1586 mod(cBINOPo->op_first->op_sibling, type);
1591 ref(cBINOPo->op_first, o->op_type);
1592 if (type == OP_ENTERSUB &&
1593 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1594 o->op_private |= OPpLVAL_DEFER;
1601 if (o->op_flags & OPf_KIDS)
1602 mod(cLISTOPo->op_last, type);
1606 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1608 else if (!(o->op_flags & OPf_KIDS))
1610 if (o->op_targ != OP_LIST) {
1611 mod(cBINOPo->op_first, type);
1616 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1620 o->op_flags |= OPf_MOD;
1622 if (type == OP_AASSIGN || type == OP_SASSIGN)
1623 o->op_flags |= OPf_SPECIAL|OPf_REF;
1625 o->op_private |= OPpLVAL_INTRO;
1626 o->op_flags &= ~OPf_SPECIAL;
1627 PL_hints |= HINT_BLOCK_SCOPE;
1629 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1630 o->op_flags |= OPf_REF;
1635 S_scalar_mod_type(pTHX_ OP *o, I32 type)
1639 if (o->op_type == OP_RV2GV)
1663 case OP_RIGHT_SHIFT:
1682 S_is_handle_constructor(pTHX_ OP *o, I32 argnum)
1684 switch (o->op_type) {
1692 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1705 Perl_refkids(pTHX_ OP *o, I32 type)
1708 if (o && o->op_flags & OPf_KIDS) {
1709 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1716 Perl_ref(pTHX_ OP *o, I32 type)
1720 if (!o || PL_error_count)
1723 switch (o->op_type) {
1725 if ((type == OP_EXISTS || type == OP_DEFINED || type == OP_LOCK) &&
1726 !(o->op_flags & OPf_STACKED)) {
1727 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1728 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1729 assert(cUNOPo->op_first->op_type == OP_NULL);
1730 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1731 o->op_flags |= OPf_SPECIAL;
1736 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1740 if (type == OP_DEFINED)
1741 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1742 ref(cUNOPo->op_first, o->op_type);
1745 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1746 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1747 : type == OP_RV2HV ? OPpDEREF_HV
1749 o->op_flags |= OPf_MOD;
1754 o->op_flags |= OPf_MOD; /* XXX ??? */
1759 o->op_flags |= OPf_REF;
1762 if (type == OP_DEFINED)
1763 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1764 ref(cUNOPo->op_first, o->op_type);
1769 o->op_flags |= OPf_REF;
1774 if (!(o->op_flags & OPf_KIDS))
1776 ref(cBINOPo->op_first, type);
1780 ref(cBINOPo->op_first, o->op_type);
1781 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1782 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1783 : type == OP_RV2HV ? OPpDEREF_HV
1785 o->op_flags |= OPf_MOD;
1793 if (!(o->op_flags & OPf_KIDS))
1795 ref(cLISTOPo->op_last, type);
1805 S_dup_attrlist(pTHX_ OP *o)
1809 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1810 * where the first kid is OP_PUSHMARK and the remaining ones
1811 * are OP_CONST. We need to push the OP_CONST values.
1813 if (o->op_type == OP_CONST)
1814 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc(cSVOPo->op_sv));
1816 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1817 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1818 if (o->op_type == OP_CONST)
1819 rop = append_elem(OP_LIST, rop,
1820 newSVOP(OP_CONST, o->op_flags,
1821 SvREFCNT_inc(cSVOPo->op_sv)));
1828 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
1832 /* fake up C<use attributes $pkg,$rv,@attrs> */
1833 ENTER; /* need to protect against side-effects of 'use' */
1835 if (stash && HvNAME(stash))
1836 stashsv = newSVpv(HvNAME(stash), 0);
1838 stashsv = &PL_sv_no;
1840 #define ATTRSMODULE "attributes"
1842 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1843 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1845 prepend_elem(OP_LIST,
1846 newSVOP(OP_CONST, 0, stashsv),
1847 prepend_elem(OP_LIST,
1848 newSVOP(OP_CONST, 0,
1850 dup_attrlist(attrs))));
1855 Perl_apply_attrs_string(pTHX_ char *stashpv, CV *cv,
1856 char *attrstr, STRLEN len)
1861 len = strlen(attrstr);
1865 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
1867 char *sstr = attrstr;
1868 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
1869 attrs = append_elem(OP_LIST, attrs,
1870 newSVOP(OP_CONST, 0,
1871 newSVpvn(sstr, attrstr-sstr)));
1875 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1876 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1877 Nullsv, prepend_elem(OP_LIST,
1878 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
1879 prepend_elem(OP_LIST,
1880 newSVOP(OP_CONST, 0,
1886 S_my_kid(pTHX_ OP *o, OP *attrs)
1891 if (!o || PL_error_count)
1895 if (type == OP_LIST) {
1896 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1898 } else if (type == OP_UNDEF) {
1900 } else if (type == OP_RV2SV || /* "our" declaration */
1902 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
1903 o->op_private |= OPpOUR_INTRO;
1905 } else if (type != OP_PADSV &&
1908 type != OP_PUSHMARK)
1910 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
1911 PL_op_desc[o->op_type],
1912 PL_in_my == KEY_our ? "our" : "my"));
1915 else if (attrs && type != OP_PUSHMARK) {
1921 PL_in_my_stash = Nullhv;
1923 /* check for C<my Dog $spot> when deciding package */
1924 namesvp = av_fetch(PL_comppad_name, o->op_targ, FALSE);
1925 if (namesvp && *namesvp && SvOBJECT(*namesvp) && HvNAME(SvSTASH(*namesvp)))
1926 stash = SvSTASH(*namesvp);
1928 stash = PL_curstash;
1929 padsv = PAD_SV(o->op_targ);
1930 apply_attrs(stash, padsv, attrs);
1932 o->op_flags |= OPf_MOD;
1933 o->op_private |= OPpLVAL_INTRO;
1938 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1940 if (o->op_flags & OPf_PARENS)
1944 o = my_kid(o, attrs);
1946 PL_in_my_stash = Nullhv;
1951 Perl_my(pTHX_ OP *o)
1953 return my_kid(o, Nullop);
1957 Perl_sawparens(pTHX_ OP *o)
1960 o->op_flags |= OPf_PARENS;
1965 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
1970 if (ckWARN(WARN_MISC) &&
1971 (left->op_type == OP_RV2AV ||
1972 left->op_type == OP_RV2HV ||
1973 left->op_type == OP_PADAV ||
1974 left->op_type == OP_PADHV)) {
1975 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
1976 right->op_type == OP_TRANS)
1977 ? right->op_type : OP_MATCH];
1978 const char *sample = ((left->op_type == OP_RV2AV ||
1979 left->op_type == OP_PADAV)
1980 ? "@array" : "%hash");
1981 Perl_warner(aTHX_ WARN_MISC,
1982 "Applying %s to %s will act on scalar(%s)",
1983 desc, sample, sample);
1986 if (!(right->op_flags & OPf_STACKED) &&
1987 (right->op_type == OP_MATCH ||
1988 right->op_type == OP_SUBST ||
1989 right->op_type == OP_TRANS)) {
1990 right->op_flags |= OPf_STACKED;
1991 if (right->op_type != OP_MATCH &&
1992 ! (right->op_type == OP_TRANS &&
1993 right->op_private & OPpTRANS_IDENTICAL))
1994 left = mod(left, right->op_type);
1995 if (right->op_type == OP_TRANS)
1996 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1998 o = prepend_elem(right->op_type, scalar(left), right);
2000 return newUNOP(OP_NOT, 0, scalar(o));
2004 return bind_match(type, left,
2005 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
2009 Perl_invert(pTHX_ OP *o)
2013 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
2014 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
2018 Perl_scope(pTHX_ OP *o)
2021 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
2022 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
2023 o->op_type = OP_LEAVE;
2024 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
2027 if (o->op_type == OP_LINESEQ) {
2029 o->op_type = OP_SCOPE;
2030 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
2031 kid = ((LISTOP*)o)->op_first;
2032 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
2036 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
2043 Perl_save_hints(pTHX)
2046 SAVESPTR(GvHV(PL_hintgv));
2047 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
2048 SAVEFREESV(GvHV(PL_hintgv));
2052 Perl_block_start(pTHX_ int full)
2055 int retval = PL_savestack_ix;
2057 SAVEI32(PL_comppad_name_floor);
2058 PL_comppad_name_floor = AvFILLp(PL_comppad_name);
2060 PL_comppad_name_fill = PL_comppad_name_floor;
2061 if (PL_comppad_name_floor < 0)
2062 PL_comppad_name_floor = 0;
2063 SAVEI32(PL_min_intro_pending);
2064 SAVEI32(PL_max_intro_pending);
2065 PL_min_intro_pending = 0;
2066 SAVEI32(PL_comppad_name_fill);
2067 SAVEI32(PL_padix_floor);
2068 PL_padix_floor = PL_padix;
2069 PL_pad_reset_pending = FALSE;
2071 PL_hints &= ~HINT_BLOCK_SCOPE;
2072 SAVESPTR(PL_compiling.cop_warnings);
2073 if (! specialWARN(PL_compiling.cop_warnings)) {
2074 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
2075 SAVEFREESV(PL_compiling.cop_warnings) ;
2081 Perl_block_end(pTHX_ I32 floor, OP *seq)
2084 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
2085 OP* retval = scalarseq(seq);
2087 PL_pad_reset_pending = FALSE;
2088 PL_compiling.op_private = PL_hints;
2090 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
2091 pad_leavemy(PL_comppad_name_fill);
2100 OP *o = newOP(OP_THREADSV, 0);
2101 o->op_targ = find_threadsv("_");
2104 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
2105 #endif /* USE_THREADS */
2109 Perl_newPROG(pTHX_ OP *o)
2115 PL_eval_root = newUNOP(OP_LEAVEEVAL,
2116 ((PL_in_eval & EVAL_KEEPERR)
2117 ? OPf_SPECIAL : 0), o);
2118 PL_eval_start = linklist(PL_eval_root);
2119 PL_eval_root->op_private |= OPpREFCOUNTED;
2120 OpREFCNT_set(PL_eval_root, 1);
2121 PL_eval_root->op_next = 0;
2122 peep(PL_eval_start);
2127 PL_main_root = scope(sawparens(scalarvoid(o)));
2128 PL_curcop = &PL_compiling;
2129 PL_main_start = LINKLIST(PL_main_root);
2130 PL_main_root->op_private |= OPpREFCOUNTED;
2131 OpREFCNT_set(PL_main_root, 1);
2132 PL_main_root->op_next = 0;
2133 peep(PL_main_start);
2136 /* Register with debugger */
2138 CV *cv = get_cv("DB::postponed", FALSE);
2142 XPUSHs((SV*)CopFILEGV(&PL_compiling));
2144 call_sv((SV*)cv, G_DISCARD);
2151 Perl_localize(pTHX_ OP *o, I32 lex)
2153 if (o->op_flags & OPf_PARENS)
2157 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
2159 for (s = PL_bufptr; *s && (isALNUM(*s) || (*s & 0x80) || strchr("@$%, ",*s)); s++) ;
2160 if (*s == ';' || *s == '=')
2161 Perl_warner(aTHX_ WARN_PARENTHESIS,
2162 "Parentheses missing around \"%s\" list",
2163 lex ? (PL_in_my == KEY_our ? "our" : "my") : "local");
2169 o = mod(o, OP_NULL); /* a bit kludgey */
2171 PL_in_my_stash = Nullhv;
2176 Perl_jmaybe(pTHX_ OP *o)
2178 if (o->op_type == OP_LIST) {
2181 o2 = newOP(OP_THREADSV, 0);
2182 o2->op_targ = find_threadsv(";");
2184 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
2185 #endif /* USE_THREADS */
2186 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2192 Perl_fold_constants(pTHX_ register OP *o)
2196 I32 type = o->op_type;
2199 if (PL_opargs[type] & OA_RETSCALAR)
2201 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2202 o->op_targ = pad_alloc(type, SVs_PADTMP);
2204 /* integerize op, unless it happens to be C<-foo>.
2205 * XXX should pp_i_negate() do magic string negation instead? */
2206 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2207 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2208 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2210 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2213 if (!(PL_opargs[type] & OA_FOLDCONST))
2218 /* XXX might want a ck_negate() for this */
2219 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2232 if (o->op_private & OPpLOCALE)
2237 goto nope; /* Don't try to run w/ errors */
2239 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2240 if ((curop->op_type != OP_CONST ||
2241 (curop->op_private & OPpCONST_BARE)) &&
2242 curop->op_type != OP_LIST &&
2243 curop->op_type != OP_SCALAR &&
2244 curop->op_type != OP_NULL &&
2245 curop->op_type != OP_PUSHMARK)
2251 curop = LINKLIST(o);
2255 sv = *(PL_stack_sp--);
2256 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
2257 pad_swipe(o->op_targ);
2258 else if (SvTEMP(sv)) { /* grab mortal temp? */
2259 (void)SvREFCNT_inc(sv);
2263 if (type == OP_RV2GV)
2264 return newGVOP(OP_GV, 0, (GV*)sv);
2266 /* try to smush double to int, but don't smush -2.0 to -2 */
2267 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
2271 if ((NV)iv == SvNV(sv)) {
2276 SvIOK_off(sv); /* undo SvIV() damage */
2278 return newSVOP(OP_CONST, 0, sv);
2282 if (!(PL_opargs[type] & OA_OTHERINT))
2285 if (!(PL_hints & HINT_INTEGER)) {
2286 if (type == OP_MODULO
2287 || type == OP_DIVIDE
2288 || !(o->op_flags & OPf_KIDS))
2293 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
2294 if (curop->op_type == OP_CONST) {
2295 if (SvIOK(((SVOP*)curop)->op_sv))
2299 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
2303 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
2310 Perl_gen_constant_list(pTHX_ register OP *o)
2314 I32 oldtmps_floor = PL_tmps_floor;
2318 return o; /* Don't attempt to run with errors */
2320 PL_op = curop = LINKLIST(o);
2327 PL_tmps_floor = oldtmps_floor;
2329 o->op_type = OP_RV2AV;
2330 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2331 curop = ((UNOP*)o)->op_first;
2332 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
2339 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2344 if (!o || o->op_type != OP_LIST)
2345 o = newLISTOP(OP_LIST, 0, o, Nullop);
2347 o->op_flags &= ~OPf_WANT;
2349 if (!(PL_opargs[type] & OA_MARK))
2350 null(cLISTOPo->op_first);
2353 o->op_ppaddr = PL_ppaddr[type];
2354 o->op_flags |= flags;
2356 o = CHECKOP(type, o);
2357 if (o->op_type != type)
2360 if (cLISTOPo->op_children < 7) {
2361 /* XXX do we really need to do this if we're done appending?? */
2362 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2364 cLISTOPo->op_last = last; /* in case check substituted last arg */
2367 return fold_constants(o);
2370 /* List constructors */
2373 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2381 if (first->op_type != type
2382 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
2384 return newLISTOP(type, 0, first, last);
2387 if (first->op_flags & OPf_KIDS)
2388 ((LISTOP*)first)->op_last->op_sibling = last;
2390 first->op_flags |= OPf_KIDS;
2391 ((LISTOP*)first)->op_first = last;
2393 ((LISTOP*)first)->op_last = last;
2394 ((LISTOP*)first)->op_children++;
2399 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2407 if (first->op_type != type)
2408 return prepend_elem(type, (OP*)first, (OP*)last);
2410 if (last->op_type != type)
2411 return append_elem(type, (OP*)first, (OP*)last);
2413 first->op_last->op_sibling = last->op_first;
2414 first->op_last = last->op_last;
2415 first->op_children += last->op_children;
2416 if (first->op_children)
2417 first->op_flags |= OPf_KIDS;
2419 #ifdef PL_OP_SLAB_ALLOC
2427 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2435 if (last->op_type == type) {
2436 if (type == OP_LIST) { /* already a PUSHMARK there */
2437 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2438 ((LISTOP*)last)->op_first->op_sibling = first;
2441 if (!(last->op_flags & OPf_KIDS)) {
2442 ((LISTOP*)last)->op_last = first;
2443 last->op_flags |= OPf_KIDS;
2445 first->op_sibling = ((LISTOP*)last)->op_first;
2446 ((LISTOP*)last)->op_first = first;
2448 ((LISTOP*)last)->op_children++;
2452 return newLISTOP(type, 0, first, last);
2458 Perl_newNULLLIST(pTHX)
2460 return newOP(OP_STUB, 0);
2464 Perl_force_list(pTHX_ OP *o)
2466 if (!o || o->op_type != OP_LIST)
2467 o = newLISTOP(OP_LIST, 0, o, Nullop);
2473 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2477 NewOp(1101, listop, 1, LISTOP);
2479 listop->op_type = type;
2480 listop->op_ppaddr = PL_ppaddr[type];
2481 listop->op_children = (first != 0) + (last != 0);
2482 listop->op_flags = flags;
2486 else if (!first && last)
2489 first->op_sibling = last;
2490 listop->op_first = first;
2491 listop->op_last = last;
2492 if (type == OP_LIST) {
2494 pushop = newOP(OP_PUSHMARK, 0);
2495 pushop->op_sibling = first;
2496 listop->op_first = pushop;
2497 listop->op_flags |= OPf_KIDS;
2499 listop->op_last = pushop;
2501 else if (listop->op_children)
2502 listop->op_flags |= OPf_KIDS;
2508 Perl_newOP(pTHX_ I32 type, I32 flags)
2511 NewOp(1101, o, 1, OP);
2513 o->op_ppaddr = PL_ppaddr[type];
2514 o->op_flags = flags;
2517 o->op_private = 0 + (flags >> 8);
2518 if (PL_opargs[type] & OA_RETSCALAR)
2520 if (PL_opargs[type] & OA_TARGET)
2521 o->op_targ = pad_alloc(type, SVs_PADTMP);
2522 return CHECKOP(type, o);
2526 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
2531 first = newOP(OP_STUB, 0);
2532 if (PL_opargs[type] & OA_MARK)
2533 first = force_list(first);
2535 NewOp(1101, unop, 1, UNOP);
2536 unop->op_type = type;
2537 unop->op_ppaddr = PL_ppaddr[type];
2538 unop->op_first = first;
2539 unop->op_flags = flags | OPf_KIDS;
2540 unop->op_private = 1 | (flags >> 8);
2541 unop = (UNOP*) CHECKOP(type, unop);
2545 return fold_constants((OP *) unop);
2549 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2552 NewOp(1101, binop, 1, BINOP);
2555 first = newOP(OP_NULL, 0);
2557 binop->op_type = type;
2558 binop->op_ppaddr = PL_ppaddr[type];
2559 binop->op_first = first;
2560 binop->op_flags = flags | OPf_KIDS;
2563 binop->op_private = 1 | (flags >> 8);
2566 binop->op_private = 2 | (flags >> 8);
2567 first->op_sibling = last;
2570 binop = (BINOP*)CHECKOP(type, binop);
2571 if (binop->op_next || binop->op_type != type)
2574 binop->op_last = binop->op_first->op_sibling;
2576 return fold_constants((OP *)binop);
2580 utf8compare(const void *a, const void *b)
2583 for (i = 0; i < 10; i++) {
2584 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2586 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2593 Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
2595 SV *tstr = ((SVOP*)expr)->op_sv;
2596 SV *rstr = ((SVOP*)repl)->op_sv;
2599 register U8 *t = (U8*)SvPV(tstr, tlen);
2600 register U8 *r = (U8*)SvPV(rstr, rlen);
2606 register short *tbl;
2608 complement = o->op_private & OPpTRANS_COMPLEMENT;
2609 del = o->op_private & OPpTRANS_DELETE;
2610 squash = o->op_private & OPpTRANS_SQUASH;
2613 o->op_private |= OPpTRANS_FROM_UTF;
2616 o->op_private |= OPpTRANS_TO_UTF;
2618 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2619 SV* listsv = newSVpvn("# comment\n",10);
2621 U8* tend = t + tlen;
2622 U8* rend = r + rlen;
2637 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2638 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2641 U8 tmpbuf[UTF8_MAXLEN];
2644 New(1109, cp, tlen, U8*);
2646 transv = newSVpvn("",0);
2655 qsort(cp, i, sizeof(U8*), utf8compare);
2656 for (j = 0; j < i; j++) {
2658 UV val = utf8_to_uv(s, &ulen);
2660 diff = val - nextmin;
2662 t = uv_to_utf8(tmpbuf,nextmin);
2663 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2665 t = uv_to_utf8(tmpbuf, val - 1);
2666 sv_catpvn(transv, "\377", 1);
2667 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2671 val = utf8_to_uv(s+1, &ulen);
2675 t = uv_to_utf8(tmpbuf,nextmin);
2676 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2677 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2678 sv_catpvn(transv, "\377", 1);
2679 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2680 t = (U8*)SvPVX(transv);
2681 tlen = SvCUR(transv);
2684 else if (!rlen && !del) {
2685 r = t; rlen = tlen; rend = tend;
2689 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
2691 o->op_private |= OPpTRANS_IDENTICAL;
2695 while (t < tend || tfirst <= tlast) {
2696 /* see if we need more "t" chars */
2697 if (tfirst > tlast) {
2698 tfirst = (I32)utf8_to_uv(t, &ulen);
2700 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
2701 tlast = (I32)utf8_to_uv(++t, &ulen);
2708 /* now see if we need more "r" chars */
2709 if (rfirst > rlast) {
2711 rfirst = (I32)utf8_to_uv(r, &ulen);
2713 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
2714 rlast = (I32)utf8_to_uv(++r, &ulen);
2723 rfirst = rlast = 0xffffffff;
2727 /* now see which range will peter our first, if either. */
2728 tdiff = tlast - tfirst;
2729 rdiff = rlast - rfirst;
2736 if (rfirst == 0xffffffff) {
2737 diff = tdiff; /* oops, pretend rdiff is infinite */
2739 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
2740 (long)tfirst, (long)tlast);
2742 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
2746 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
2747 (long)tfirst, (long)(tfirst + diff),
2750 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
2751 (long)tfirst, (long)rfirst);
2753 if (rfirst + diff > max)
2754 max = rfirst + diff;
2759 else if (rfirst <= 0x800)
2760 grows |= (tfirst < 0x80);
2761 else if (rfirst <= 0x10000)
2762 grows |= (tfirst < 0x800);
2763 else if (rfirst <= 0x200000)
2764 grows |= (tfirst < 0x10000);
2765 else if (rfirst <= 0x4000000)
2766 grows |= (tfirst < 0x200000);
2767 else if (rfirst <= 0x80000000)
2768 grows |= (tfirst < 0x4000000);
2780 else if (max > 0xff)
2785 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2786 SvREFCNT_dec(listsv);
2788 SvREFCNT_dec(transv);
2790 if (!del && havefinal)
2791 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5,
2792 newSVuv((UV)final), 0);
2794 if (grows && to_utf)
2795 o->op_private |= OPpTRANS_GROWS;
2802 tbl = (short*)cPVOPo->op_pv;
2804 Zero(tbl, 256, short);
2805 for (i = 0; i < tlen; i++)
2807 for (i = 0, j = 0; i < 256; i++) {
2823 if (!rlen && !del) {
2826 o->op_private |= OPpTRANS_IDENTICAL;
2828 for (i = 0; i < 256; i++)
2830 for (i = 0, j = 0; i < tlen; i++,j++) {
2833 if (tbl[t[i]] == -1)
2839 if (tbl[t[i]] == -1)
2850 Perl_newPMOP(pTHX_ I32 type, I32 flags)
2855 NewOp(1101, pmop, 1, PMOP);
2856 pmop->op_type = type;
2857 pmop->op_ppaddr = PL_ppaddr[type];
2858 pmop->op_flags = flags;
2859 pmop->op_private = 0 | (flags >> 8);
2861 if (PL_hints & HINT_RE_TAINT)
2862 pmop->op_pmpermflags |= PMf_RETAINT;
2863 if (PL_hints & HINT_LOCALE)
2864 pmop->op_pmpermflags |= PMf_LOCALE;
2865 pmop->op_pmflags = pmop->op_pmpermflags;
2867 /* link into pm list */
2868 if (type != OP_TRANS && PL_curstash) {
2869 pmop->op_pmnext = HvPMROOT(PL_curstash);
2870 HvPMROOT(PL_curstash) = pmop;
2877 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
2882 I32 repl_has_vars = 0;
2884 if (o->op_type == OP_TRANS)
2885 return pmtrans(o, expr, repl);
2887 PL_hints |= HINT_BLOCK_SCOPE;
2890 if (expr->op_type == OP_CONST) {
2892 SV *pat = ((SVOP*)expr)->op_sv;
2893 char *p = SvPV(pat, plen);
2894 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2895 sv_setpvn(pat, "\\s+", 3);
2896 p = SvPV(pat, plen);
2897 pm->op_pmflags |= PMf_SKIPWHITE;
2899 if ((PL_hints & HINT_UTF8) || (SvUTF8(pat) && !(PL_hints & HINT_BYTE)))
2900 pm->op_pmdynflags |= PMdf_UTF8;
2901 pm->op_pmregexp = CALLREGCOMP(aTHX_ p, p + plen, pm);
2902 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2903 pm->op_pmflags |= PMf_WHITE;
2907 if (PL_hints & HINT_UTF8)
2908 pm->op_pmdynflags |= PMdf_UTF8;
2909 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2910 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2912 : OP_REGCMAYBE),0,expr);
2914 NewOp(1101, rcop, 1, LOGOP);
2915 rcop->op_type = OP_REGCOMP;
2916 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2917 rcop->op_first = scalar(expr);
2918 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2919 ? (OPf_SPECIAL | OPf_KIDS)
2921 rcop->op_private = 1;
2924 /* establish postfix order */
2925 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2927 rcop->op_next = expr;
2928 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2931 rcop->op_next = LINKLIST(expr);
2932 expr->op_next = (OP*)rcop;
2935 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2940 if (pm->op_pmflags & PMf_EVAL) {
2942 if (CopLINE(PL_curcop) < PL_multi_end)
2943 CopLINE_set(PL_curcop, PL_multi_end);
2946 else if (repl->op_type == OP_THREADSV
2947 && strchr("&`'123456789+",
2948 PL_threadsv_names[repl->op_targ]))
2952 #endif /* USE_THREADS */
2953 else if (repl->op_type == OP_CONST)
2957 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2958 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2960 if (curop->op_type == OP_THREADSV) {
2962 if (strchr("&`'123456789+", curop->op_private))
2966 if (curop->op_type == OP_GV) {
2967 GV *gv = cGVOPx_gv(curop);
2969 if (strchr("&`'123456789+", *GvENAME(gv)))
2972 #endif /* USE_THREADS */
2973 else if (curop->op_type == OP_RV2CV)
2975 else if (curop->op_type == OP_RV2SV ||
2976 curop->op_type == OP_RV2AV ||
2977 curop->op_type == OP_RV2HV ||
2978 curop->op_type == OP_RV2GV) {
2979 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2982 else if (curop->op_type == OP_PADSV ||
2983 curop->op_type == OP_PADAV ||
2984 curop->op_type == OP_PADHV ||
2985 curop->op_type == OP_PADANY) {
2988 else if (curop->op_type == OP_PUSHRE)
2989 ; /* Okay here, dangerous in newASSIGNOP */
2998 && (!pm->op_pmregexp
2999 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
3000 pm->op_pmflags |= PMf_CONST; /* const for long enough */
3001 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
3002 prepend_elem(o->op_type, scalar(repl), o);
3005 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
3006 pm->op_pmflags |= PMf_MAYBE_CONST;
3007 pm->op_pmpermflags |= PMf_MAYBE_CONST;
3009 NewOp(1101, rcop, 1, LOGOP);
3010 rcop->op_type = OP_SUBSTCONT;
3011 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
3012 rcop->op_first = scalar(repl);
3013 rcop->op_flags |= OPf_KIDS;
3014 rcop->op_private = 1;
3017 /* establish postfix order */
3018 rcop->op_next = LINKLIST(repl);
3019 repl->op_next = (OP*)rcop;
3021 pm->op_pmreplroot = scalar((OP*)rcop);
3022 pm->op_pmreplstart = LINKLIST(rcop);
3031 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
3034 NewOp(1101, svop, 1, SVOP);
3035 svop->op_type = type;
3036 svop->op_ppaddr = PL_ppaddr[type];
3038 svop->op_next = (OP*)svop;
3039 svop->op_flags = flags;
3040 if (PL_opargs[type] & OA_RETSCALAR)
3042 if (PL_opargs[type] & OA_TARGET)
3043 svop->op_targ = pad_alloc(type, SVs_PADTMP);
3044 return CHECKOP(type, svop);
3048 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
3051 NewOp(1101, padop, 1, PADOP);
3052 padop->op_type = type;
3053 padop->op_ppaddr = PL_ppaddr[type];
3054 padop->op_padix = pad_alloc(type, SVs_PADTMP);
3055 SvREFCNT_dec(PL_curpad[padop->op_padix]);
3056 PL_curpad[padop->op_padix] = sv;
3058 padop->op_next = (OP*)padop;
3059 padop->op_flags = flags;
3060 if (PL_opargs[type] & OA_RETSCALAR)
3062 if (PL_opargs[type] & OA_TARGET)
3063 padop->op_targ = pad_alloc(type, SVs_PADTMP);
3064 return CHECKOP(type, padop);
3068 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
3073 return newPADOP(type, flags, SvREFCNT_inc(gv));
3075 return newSVOP(type, flags, SvREFCNT_inc(gv));
3080 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
3083 NewOp(1101, pvop, 1, PVOP);
3084 pvop->op_type = type;
3085 pvop->op_ppaddr = PL_ppaddr[type];
3087 pvop->op_next = (OP*)pvop;
3088 pvop->op_flags = flags;
3089 if (PL_opargs[type] & OA_RETSCALAR)
3091 if (PL_opargs[type] & OA_TARGET)
3092 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
3093 return CHECKOP(type, pvop);
3097 Perl_package(pTHX_ OP *o)
3102 save_hptr(&PL_curstash);
3103 save_item(PL_curstname);
3108 name = SvPV(sv, len);
3109 PL_curstash = gv_stashpvn(name,len,TRUE);
3110 sv_setpvn(PL_curstname, name, len);
3114 sv_setpv(PL_curstname,"<none>");
3115 PL_curstash = Nullhv;
3117 PL_hints |= HINT_BLOCK_SCOPE;
3118 PL_copline = NOLINE;
3123 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg)
3131 if (id->op_type != OP_CONST)
3132 Perl_croak(aTHX_ "Module name must be constant");
3136 if (version != Nullop) {
3137 SV *vesv = ((SVOP*)version)->op_sv;
3139 if (arg == Nullop && !SvNIOKp(vesv)) {
3146 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
3147 Perl_croak(aTHX_ "Version number must be constant number");
3149 /* Make copy of id so we don't free it twice */
3150 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3152 /* Fake up a method call to VERSION */
3153 meth = newSVpvn("VERSION",7);
3154 sv_upgrade(meth, SVt_PVIV);
3155 (void)SvIOK_on(meth);
3156 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
3157 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3158 append_elem(OP_LIST,
3159 prepend_elem(OP_LIST, pack, list(version)),
3160 newSVOP(OP_METHOD_NAMED, 0, meth)));
3164 /* Fake up an import/unimport */
3165 if (arg && arg->op_type == OP_STUB)
3166 imop = arg; /* no import on explicit () */
3167 else if (SvNIOKp(((SVOP*)id)->op_sv)) {
3168 imop = Nullop; /* use 5.0; */
3173 /* Make copy of id so we don't free it twice */
3174 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3176 /* Fake up a method call to import/unimport */
3177 meth = aver ? newSVpvn("import",6) : newSVpvn("unimport", 8);;
3178 sv_upgrade(meth, SVt_PVIV);
3179 (void)SvIOK_on(meth);
3180 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
3181 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3182 append_elem(OP_LIST,
3183 prepend_elem(OP_LIST, pack, list(arg)),
3184 newSVOP(OP_METHOD_NAMED, 0, meth)));
3187 /* Fake up a require, handle override, if any */
3188 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
3189 if (!(gv && GvIMPORTED_CV(gv)))
3190 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
3192 if (gv && GvIMPORTED_CV(gv)) {
3193 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3194 append_elem(OP_LIST, id,
3195 scalar(newUNOP(OP_RV2CV, 0,
3200 rqop = newUNOP(OP_REQUIRE, 0, id);
3203 /* Fake up the BEGIN {}, which does its thing immediately. */
3205 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
3208 append_elem(OP_LINESEQ,
3209 append_elem(OP_LINESEQ,
3210 newSTATEOP(0, Nullch, rqop),
3211 newSTATEOP(0, Nullch, veop)),
3212 newSTATEOP(0, Nullch, imop) ));
3214 PL_hints |= HINT_BLOCK_SCOPE;
3215 PL_copline = NOLINE;
3220 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
3223 va_start(args, ver);
3224 vload_module(flags, name, ver, &args);
3228 #ifdef PERL_IMPLICIT_CONTEXT
3230 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
3234 va_start(args, ver);
3235 vload_module(flags, name, ver, &args);
3241 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
3243 OP *modname, *veop, *imop;
3245 modname = newSVOP(OP_CONST, 0, name);
3246 modname->op_private |= OPpCONST_BARE;
3248 veop = newSVOP(OP_CONST, 0, ver);
3252 if (flags & PERL_LOADMOD_NOIMPORT) {
3253 imop = sawparens(newNULLLIST());
3255 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
3256 imop = va_arg(*args, OP*);
3261 sv = va_arg(*args, SV*);
3263 imop = append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
3264 sv = va_arg(*args, SV*);
3268 line_t ocopline = PL_copline;
3269 int oexpect = PL_expect;
3271 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
3272 veop, modname, imop);
3273 PL_expect = oexpect;
3274 PL_copline = ocopline;
3279 Perl_dofile(pTHX_ OP *term)
3284 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
3285 if (!(gv && GvIMPORTED_CV(gv)))
3286 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
3288 if (gv && GvIMPORTED_CV(gv)) {
3289 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3290 append_elem(OP_LIST, term,
3291 scalar(newUNOP(OP_RV2CV, 0,
3296 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3302 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
3304 return newBINOP(OP_LSLICE, flags,
3305 list(force_list(subscript)),
3306 list(force_list(listval)) );
3310 S_list_assignment(pTHX_ register OP *o)
3315 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3316 o = cUNOPo->op_first;
3318 if (o->op_type == OP_COND_EXPR) {
3319 I32 t = list_assignment(cLOGOPo->op_first->op_sibling);
3320 I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
3325 yyerror("Assignment to both a list and a scalar");
3329 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3330 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3331 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
3334 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
3337 if (o->op_type == OP_RV2SV)
3344 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
3349 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
3350 return newLOGOP(optype, 0,
3351 mod(scalar(left), optype),
3352 newUNOP(OP_SASSIGN, 0, scalar(right)));
3355 return newBINOP(optype, OPf_STACKED,
3356 mod(scalar(left), optype), scalar(right));
3360 if (list_assignment(left)) {
3365 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3366 left = mod(left, OP_AASSIGN);
3374 curop = list(force_list(left));
3375 o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
3376 o->op_private = 0 | (flags >> 8);
3377 for (curop = ((LISTOP*)curop)->op_first;
3378 curop; curop = curop->op_sibling)
3380 if (curop->op_type == OP_RV2HV &&
3381 ((UNOP*)curop)->op_first->op_type != OP_GV) {
3382 o->op_private |= OPpASSIGN_HASH;
3386 if (!(left->op_private & OPpLVAL_INTRO)) {
3389 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3390 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3391 if (curop->op_type == OP_GV) {
3392 GV *gv = cGVOPx_gv(curop);
3393 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3395 SvCUR(gv) = PL_generation;
3397 else if (curop->op_type == OP_PADSV ||
3398 curop->op_type == OP_PADAV ||
3399 curop->op_type == OP_PADHV ||
3400 curop->op_type == OP_PADANY) {
3401 SV **svp = AvARRAY(PL_comppad_name);
3402 SV *sv = svp[curop->op_targ];
3403 if (SvCUR(sv) == PL_generation)
3405 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
3407 else if (curop->op_type == OP_RV2CV)
3409 else if (curop->op_type == OP_RV2SV ||
3410 curop->op_type == OP_RV2AV ||
3411 curop->op_type == OP_RV2HV ||
3412 curop->op_type == OP_RV2GV) {
3413 if (lastop->op_type != OP_GV) /* funny deref? */
3416 else if (curop->op_type == OP_PUSHRE) {
3417 if (((PMOP*)curop)->op_pmreplroot) {
3418 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3419 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3421 SvCUR(gv) = PL_generation;
3430 o->op_private |= OPpASSIGN_COMMON;
3432 if (right && right->op_type == OP_SPLIT) {
3434 if ((tmpop = ((LISTOP*)right)->op_first) &&
3435 tmpop->op_type == OP_PUSHRE)
3437 PMOP *pm = (PMOP*)tmpop;
3438 if (left->op_type == OP_RV2AV &&
3439 !(left->op_private & OPpLVAL_INTRO) &&
3440 !(o->op_private & OPpASSIGN_COMMON) )
3442 tmpop = ((UNOP*)left)->op_first;
3443 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
3445 pm->op_pmreplroot = (OP*)cPADOPx(tmpop)->op_padix;
3446 cPADOPx(tmpop)->op_padix = 0; /* steal it */
3448 pm->op_pmreplroot = (OP*)cSVOPx(tmpop)->op_sv;
3449 cSVOPx(tmpop)->op_sv = Nullsv; /* steal it */
3451 pm->op_pmflags |= PMf_ONCE;
3452 tmpop = cUNOPo->op_first; /* to list (nulled) */
3453 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3454 tmpop->op_sibling = Nullop; /* don't free split */
3455 right->op_next = tmpop->op_next; /* fix starting loc */
3456 op_free(o); /* blow off assign */
3457 right->op_flags &= ~OPf_WANT;
3458 /* "I don't know and I don't care." */
3463 if (PL_modcount < 10000 &&
3464 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3466 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3468 sv_setiv(sv, PL_modcount+1);
3476 right = newOP(OP_UNDEF, 0);
3477 if (right->op_type == OP_READLINE) {
3478 right->op_flags |= OPf_STACKED;
3479 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3482 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3483 o = newBINOP(OP_SASSIGN, flags,
3484 scalar(right), mod(scalar(left), OP_SASSIGN) );
3496 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3499 U32 seq = intro_my();
3502 NewOp(1101, cop, 1, COP);
3503 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
3504 cop->op_type = OP_DBSTATE;
3505 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3508 cop->op_type = OP_NEXTSTATE;
3509 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3511 cop->op_flags = flags;
3512 cop->op_private = (PL_hints & HINT_BYTE);
3514 cop->op_private |= NATIVE_HINTS;
3516 PL_compiling.op_private = cop->op_private;
3517 cop->op_next = (OP*)cop;
3520 cop->cop_label = label;
3521 PL_hints |= HINT_BLOCK_SCOPE;
3524 cop->cop_arybase = PL_curcop->cop_arybase;
3525 if (specialWARN(PL_curcop->cop_warnings))
3526 cop->cop_warnings = PL_curcop->cop_warnings ;
3528 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3531 if (PL_copline == NOLINE)
3532 CopLINE_set(cop, CopLINE(PL_curcop));
3534 CopLINE_set(cop, PL_copline);
3535 PL_copline = NOLINE;
3538 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
3540 CopFILEGV_set(cop, CopFILEGV(PL_curcop));
3542 CopSTASH_set(cop, PL_curstash);
3544 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3545 SV **svp = av_fetch(CopFILEAV(PL_curcop), (I32)CopLINE(cop), FALSE);
3546 if (svp && *svp != &PL_sv_undef && !SvIOK(*svp)) {
3547 (void)SvIOK_on(*svp);
3548 SvIVX(*svp) = PTR2IV(cop);
3552 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3555 /* "Introduce" my variables to visible status. */
3563 if (! PL_min_intro_pending)
3564 return PL_cop_seqmax;
3566 svp = AvARRAY(PL_comppad_name);
3567 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
3568 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
3569 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
3570 SvNVX(sv) = (NV)PL_cop_seqmax;
3573 PL_min_intro_pending = 0;
3574 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
3575 return PL_cop_seqmax++;
3579 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
3581 return new_logop(type, flags, &first, &other);
3585 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
3590 OP *first = *firstp;
3591 OP *other = *otherp;
3593 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3594 return newBINOP(type, flags, scalar(first), scalar(other));
3596 scalarboolean(first);
3597 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3598 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3599 if (type == OP_AND || type == OP_OR) {
3605 first = *firstp = cUNOPo->op_first;
3607 first->op_next = o->op_next;
3608 cUNOPo->op_first = Nullop;
3612 if (first->op_type == OP_CONST) {
3613 if (ckWARN(WARN_BAREWORD) && (first->op_private & OPpCONST_BARE))
3614 Perl_warner(aTHX_ WARN_BAREWORD, "Bareword found in conditional");
3615 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3626 else if (first->op_type == OP_WANTARRAY) {
3632 else if (ckWARN(WARN_MISC) && (first->op_flags & OPf_KIDS)) {
3633 OP *k1 = ((UNOP*)first)->op_first;
3634 OP *k2 = k1->op_sibling;
3636 switch (first->op_type)
3639 if (k2 && k2->op_type == OP_READLINE
3640 && (k2->op_flags & OPf_STACKED)
3641 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3643 warnop = k2->op_type;
3648 if (k1->op_type == OP_READDIR
3649 || k1->op_type == OP_GLOB
3650 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3651 || k1->op_type == OP_EACH)
3653 warnop = ((k1->op_type == OP_NULL)
3654 ? k1->op_targ : k1->op_type);
3659 line_t oldline = CopLINE(PL_curcop);
3660 CopLINE_set(PL_curcop, PL_copline);
3661 Perl_warner(aTHX_ WARN_MISC,
3662 "Value of %s%s can be \"0\"; test with defined()",
3664 ((warnop == OP_READLINE || warnop == OP_GLOB)
3665 ? " construct" : "() operator"));
3666 CopLINE_set(PL_curcop, oldline);
3673 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3674 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3676 NewOp(1101, logop, 1, LOGOP);
3678 logop->op_type = type;
3679 logop->op_ppaddr = PL_ppaddr[type];
3680 logop->op_first = first;
3681 logop->op_flags = flags | OPf_KIDS;
3682 logop->op_other = LINKLIST(other);
3683 logop->op_private = 1 | (flags >> 8);
3685 /* establish postfix order */
3686 logop->op_next = LINKLIST(first);
3687 first->op_next = (OP*)logop;
3688 first->op_sibling = other;
3690 o = newUNOP(OP_NULL, 0, (OP*)logop);
3697 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
3705 return newLOGOP(OP_AND, 0, first, trueop);
3707 return newLOGOP(OP_OR, 0, first, falseop);
3709 scalarboolean(first);
3710 if (first->op_type == OP_CONST) {
3711 if (SvTRUE(((SVOP*)first)->op_sv)) {
3722 else if (first->op_type == OP_WANTARRAY) {
3726 NewOp(1101, logop, 1, LOGOP);
3727 logop->op_type = OP_COND_EXPR;
3728 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3729 logop->op_first = first;
3730 logop->op_flags = flags | OPf_KIDS;
3731 logop->op_private = 1 | (flags >> 8);
3732 logop->op_other = LINKLIST(trueop);
3733 logop->op_next = LINKLIST(falseop);
3736 /* establish postfix order */
3737 start = LINKLIST(first);
3738 first->op_next = (OP*)logop;
3740 first->op_sibling = trueop;
3741 trueop->op_sibling = falseop;
3742 o = newUNOP(OP_NULL, 0, (OP*)logop);
3744 trueop->op_next = falseop->op_next = o;
3751 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
3760 NewOp(1101, range, 1, LOGOP);
3762 range->op_type = OP_RANGE;
3763 range->op_ppaddr = PL_ppaddr[OP_RANGE];
3764 range->op_first = left;
3765 range->op_flags = OPf_KIDS;
3766 leftstart = LINKLIST(left);
3767 range->op_other = LINKLIST(right);
3768 range->op_private = 1 | (flags >> 8);
3770 left->op_sibling = right;
3772 range->op_next = (OP*)range;
3773 flip = newUNOP(OP_FLIP, flags, (OP*)range);
3774 flop = newUNOP(OP_FLOP, 0, flip);
3775 o = newUNOP(OP_NULL, 0, flop);
3777 range->op_next = leftstart;
3779 left->op_next = flip;
3780 right->op_next = flop;
3782 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3783 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
3784 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3785 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3787 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3788 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3791 if (!flip->op_private || !flop->op_private)
3792 linklist(o); /* blow off optimizer unless constant */
3798 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
3803 int once = block && block->op_flags & OPf_SPECIAL &&
3804 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3807 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3808 return block; /* do {} while 0 does once */
3809 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3810 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3811 expr = newUNOP(OP_DEFINED, 0,
3812 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3813 } else if (expr->op_flags & OPf_KIDS) {
3814 OP *k1 = ((UNOP*)expr)->op_first;
3815 OP *k2 = (k1) ? k1->op_sibling : NULL;
3816 switch (expr->op_type) {
3818 if (k2 && k2->op_type == OP_READLINE
3819 && (k2->op_flags & OPf_STACKED)
3820 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3821 expr = newUNOP(OP_DEFINED, 0, expr);
3825 if (k1->op_type == OP_READDIR
3826 || k1->op_type == OP_GLOB
3827 || (k1->op_type == OP_NULL && k1->op_targ == OP_NULL)
3828 || k1->op_type == OP_EACH)
3829 expr = newUNOP(OP_DEFINED, 0, expr);
3835 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3836 o = new_logop(OP_AND, 0, &expr, &listop);
3839 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3841 if (once && o != listop)
3842 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3845 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3847 o->op_flags |= flags;
3849 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3854 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
3864 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3865 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
3866 expr = newUNOP(OP_DEFINED, 0,
3867 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3868 } else if (expr && (expr->op_flags & OPf_KIDS)) {
3869 OP *k1 = ((UNOP*)expr)->op_first;
3870 OP *k2 = (k1) ? k1->op_sibling : NULL;
3871 switch (expr->op_type) {
3873 if (k2 && k2->op_type == OP_READLINE
3874 && (k2->op_flags & OPf_STACKED)
3875 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3876 expr = newUNOP(OP_DEFINED, 0, expr);
3880 if (k1->op_type == OP_READDIR
3881 || k1->op_type == OP_GLOB
3882 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3883 || k1->op_type == OP_EACH)
3884 expr = newUNOP(OP_DEFINED, 0, expr);
3890 block = newOP(OP_NULL, 0);
3892 block = scope(block);
3896 next = LINKLIST(cont);
3897 loopflags |= OPpLOOP_CONTINUE;
3900 OP *unstack = newOP(OP_UNSTACK, 0);
3903 cont = append_elem(OP_LINESEQ, cont, unstack);
3904 if ((line_t)whileline != NOLINE) {
3905 PL_copline = whileline;
3906 cont = append_elem(OP_LINESEQ, cont,
3907 newSTATEOP(0, Nullch, Nullop));
3911 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3912 redo = LINKLIST(listop);
3915 PL_copline = whileline;
3917 o = new_logop(OP_AND, 0, &expr, &listop);
3918 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3919 op_free(expr); /* oops, it's a while (0) */
3921 return Nullop; /* listop already freed by new_logop */
3924 ((LISTOP*)listop)->op_last->op_next = condop =
3925 (o == listop ? redo : LINKLIST(o));
3931 NewOp(1101,loop,1,LOOP);
3932 loop->op_type = OP_ENTERLOOP;
3933 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3934 loop->op_private = 0;
3935 loop->op_next = (OP*)loop;
3938 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3940 loop->op_redoop = redo;
3941 loop->op_lastop = o;
3942 o->op_private |= loopflags;
3945 loop->op_nextop = next;
3947 loop->op_nextop = o;
3949 o->op_flags |= flags;
3950 o->op_private |= (flags >> 8);
3955 Perl_newFOROP(pTHX_ I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
3963 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3964 sv->op_type = OP_RV2GV;
3965 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
3967 else if (sv->op_type == OP_PADSV) { /* private variable */
3968 padoff = sv->op_targ;
3973 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3974 padoff = sv->op_targ;
3976 iterflags |= OPf_SPECIAL;
3981 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
3985 padoff = find_threadsv("_");
3986 iterflags |= OPf_SPECIAL;
3988 sv = newGVOP(OP_GV, 0, PL_defgv);
3991 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3992 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3993 iterflags |= OPf_STACKED;
3995 else if (expr->op_type == OP_NULL &&
3996 (expr->op_flags & OPf_KIDS) &&
3997 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3999 /* Basically turn for($x..$y) into the same as for($x,$y), but we
4000 * set the STACKED flag to indicate that these values are to be
4001 * treated as min/max values by 'pp_iterinit'.
4003 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
4004 LOGOP* range = (LOGOP*) flip->op_first;
4005 OP* left = range->op_first;
4006 OP* right = left->op_sibling;
4009 range->op_flags &= ~OPf_KIDS;
4010 range->op_first = Nullop;
4012 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
4013 listop->op_first->op_next = range->op_next;
4014 left->op_next = range->op_other;
4015 right->op_next = (OP*)listop;
4016 listop->op_next = listop->op_first;
4019 expr = (OP*)(listop);
4021 iterflags |= OPf_STACKED;
4024 expr = mod(force_list(expr), OP_GREPSTART);
4028 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
4029 append_elem(OP_LIST, expr, scalar(sv))));
4030 assert(!loop->op_next);
4031 #ifdef PL_OP_SLAB_ALLOC
4034 NewOp(1234,tmp,1,LOOP);
4035 Copy(loop,tmp,1,LOOP);
4039 Renew(loop, 1, LOOP);
4041 loop->op_targ = padoff;
4042 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
4043 PL_copline = forline;
4044 return newSTATEOP(0, label, wop);
4048 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
4054 if (type != OP_GOTO || label->op_type == OP_CONST) {
4055 /* "last()" means "last" */
4056 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
4057 o = newOP(type, OPf_SPECIAL);
4059 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
4060 ? SvPVx(((SVOP*)label)->op_sv, n_a)
4066 if (label->op_type == OP_ENTERSUB)
4067 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
4068 o = newUNOP(type, OPf_STACKED, label);
4070 PL_hints |= HINT_BLOCK_SCOPE;
4075 Perl_cv_undef(pTHX_ CV *cv)
4080 MUTEX_DESTROY(CvMUTEXP(cv));
4081 Safefree(CvMUTEXP(cv));
4084 #endif /* USE_THREADS */
4086 if (!CvXSUB(cv) && CvROOT(cv)) {
4088 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
4089 Perl_croak(aTHX_ "Can't undef active subroutine");
4092 Perl_croak(aTHX_ "Can't undef active subroutine");
4093 #endif /* USE_THREADS */
4096 SAVEVPTR(PL_curpad);
4100 op_free(CvROOT(cv));
4101 CvROOT(cv) = Nullop;
4104 SvPOK_off((SV*)cv); /* forget prototype */
4106 SvREFCNT_dec(CvGV(cv));
4108 SvREFCNT_dec(CvOUTSIDE(cv));
4109 CvOUTSIDE(cv) = Nullcv;
4110 if (CvPADLIST(cv)) {
4111 /* may be during global destruction */
4112 if (SvREFCNT(CvPADLIST(cv))) {
4113 I32 i = AvFILLp(CvPADLIST(cv));
4115 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
4116 SV* sv = svp ? *svp : Nullsv;
4119 if (sv == (SV*)PL_comppad_name)
4120 PL_comppad_name = Nullav;
4121 else if (sv == (SV*)PL_comppad) {
4122 PL_comppad = Nullav;
4123 PL_curpad = Null(SV**);
4127 SvREFCNT_dec((SV*)CvPADLIST(cv));
4129 CvPADLIST(cv) = Nullav;
4134 S_cv_dump(pTHX_ CV *cv)
4137 CV *outside = CvOUTSIDE(cv);
4138 AV* padlist = CvPADLIST(cv);
4145 PerlIO_printf(Perl_debug_log,
4146 "\tCV=0x%"UVxf" (%s), OUTSIDE=0x%"UVxf" (%s)\n",
4148 (CvANON(cv) ? "ANON"
4149 : (cv == PL_main_cv) ? "MAIN"
4150 : CvUNIQUE(cv) ? "UNIQUE"
4151 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
4154 : CvANON(outside) ? "ANON"
4155 : (outside == PL_main_cv) ? "MAIN"
4156 : CvUNIQUE(outside) ? "UNIQUE"
4157 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
4162 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
4163 pad = (AV*)*av_fetch(padlist, 1, FALSE);
4164 pname = AvARRAY(pad_name);
4165 ppad = AvARRAY(pad);
4167 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
4168 if (SvPOK(pname[ix]))
4169 PerlIO_printf(Perl_debug_log,
4170 "\t%4d. 0x%"UVxf" (%s\"%s\" %"IVdf"-%"IVdf")\n",
4171 (int)ix, PTR2UV(ppad[ix]),
4172 SvFAKE(pname[ix]) ? "FAKE " : "",
4174 (IV)I_32(SvNVX(pname[ix])),
4177 #endif /* DEBUGGING */
4181 S_cv_clone2(pTHX_ CV *proto, CV *outside)
4186 AV* protopadlist = CvPADLIST(proto);
4187 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
4188 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
4189 SV** pname = AvARRAY(protopad_name);
4190 SV** ppad = AvARRAY(protopad);
4191 I32 fname = AvFILLp(protopad_name);
4192 I32 fpad = AvFILLp(protopad);
4196 assert(!CvUNIQUE(proto));
4200 SAVESPTR(PL_comppad_name);
4201 SAVESPTR(PL_compcv);
4203 cv = PL_compcv = (CV*)NEWSV(1104,0);
4204 sv_upgrade((SV *)cv, SvTYPE(proto));
4205 CvFLAGS(cv) = CvFLAGS(proto) & ~CVf_CLONE;
4209 New(666, CvMUTEXP(cv), 1, perl_mutex);
4210 MUTEX_INIT(CvMUTEXP(cv));
4212 #endif /* USE_THREADS */
4213 CvFILE(cv) = CvFILE(proto);
4214 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
4215 CvSTASH(cv) = CvSTASH(proto);
4216 CvROOT(cv) = CvROOT(proto);
4217 CvSTART(cv) = CvSTART(proto);
4219 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
4222 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
4224 PL_comppad_name = newAV();
4225 for (ix = fname; ix >= 0; ix--)
4226 av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix]));
4228 PL_comppad = newAV();
4230 comppadlist = newAV();
4231 AvREAL_off(comppadlist);
4232 av_store(comppadlist, 0, (SV*)PL_comppad_name);
4233 av_store(comppadlist, 1, (SV*)PL_comppad);
4234 CvPADLIST(cv) = comppadlist;
4235 av_fill(PL_comppad, AvFILLp(protopad));
4236 PL_curpad = AvARRAY(PL_comppad);
4238 av = newAV(); /* will be @_ */
4240 av_store(PL_comppad, 0, (SV*)av);
4241 AvFLAGS(av) = AVf_REIFY;
4243 for (ix = fpad; ix > 0; ix--) {
4244 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
4245 if (namesv && namesv != &PL_sv_undef) {
4246 char *name = SvPVX(namesv); /* XXX */
4247 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
4248 I32 off = pad_findlex(name, ix, SvIVX(namesv),
4249 CvOUTSIDE(cv), cxstack_ix, 0, 0);
4251 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
4253 Perl_croak(aTHX_ "panic: cv_clone: %s", name);
4255 else { /* our own lexical */
4258 /* anon code -- we'll come back for it */
4259 sv = SvREFCNT_inc(ppad[ix]);
4261 else if (*name == '@')
4263 else if (*name == '%')
4272 else if (IS_PADGV(ppad[ix]) || IS_PADCONST(ppad[ix])) {
4273 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
4276 SV* sv = NEWSV(0,0);
4282 /* Now that vars are all in place, clone nested closures. */
4284 for (ix = fpad; ix > 0; ix--) {
4285 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
4287 && namesv != &PL_sv_undef
4288 && !(SvFLAGS(namesv) & SVf_FAKE)
4289 && *SvPVX(namesv) == '&'
4290 && CvCLONE(ppad[ix]))
4292 CV *kid = cv_clone2((CV*)ppad[ix], cv);
4293 SvREFCNT_dec(ppad[ix]);
4296 PL_curpad[ix] = (SV*)kid;
4300 #ifdef DEBUG_CLOSURES
4301 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
4303 PerlIO_printf(Perl_debug_log, " from:\n");
4305 PerlIO_printf(Perl_debug_log, " to:\n");
4314 Perl_cv_clone(pTHX_ CV *proto)
4317 LOCK_CRED_MUTEX; /* XXX create separate mutex */
4318 cv = cv_clone2(proto, CvOUTSIDE(proto));
4319 UNLOCK_CRED_MUTEX; /* XXX create separate mutex */
4324 Perl_cv_ckproto(pTHX_ CV *cv, GV *gv, char *p)
4328 if (((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) && ckWARN_d(WARN_PROTOTYPE)) {
4329 SV* msg = sv_newmortal();
4333 gv_efullname3(name = sv_newmortal(), gv, Nullch);
4334 sv_setpv(msg, "Prototype mismatch:");
4336 Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, name);
4338 Perl_sv_catpvf(aTHX_ msg, " (%s)", SvPVX(cv));
4339 sv_catpv(msg, " vs ");
4341 Perl_sv_catpvf(aTHX_ msg, "(%s)", p);
4343 sv_catpv(msg, "none");
4344 Perl_warner(aTHX_ WARN_PROTOTYPE, "%"SVf, msg);
4349 Perl_cv_const_sv(pTHX_ CV *cv)
4351 if (!cv || !SvPOK(cv) || SvCUR(cv))
4353 return op_const_sv(CvSTART(cv), cv);
4357 Perl_op_const_sv(pTHX_ OP *o, CV *cv)
4364 if (o->op_type == OP_LINESEQ && cLISTOPo->op_first)
4365 o = cLISTOPo->op_first->op_sibling;
4367 for (; o; o = o->op_next) {
4368 OPCODE type = o->op_type;
4370 if (sv && o->op_next == o)
4372 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
4374 if (type == OP_LEAVESUB || type == OP_RETURN)
4378 if (type == OP_CONST && cSVOPo->op_sv)
4380 else if ((type == OP_PADSV || type == OP_CONST) && cv) {
4381 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
4382 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
4383 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
4395 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4405 Perl_croak(aTHX_ "\"my sub\" not yet implemented");
4409 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
4411 return Perl_newATTRSUB(aTHX_ floor, o, proto, Nullop, block);
4415 Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4422 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, n_a) : Nullch;
4426 name = o ? SvPVx(cSVOPo->op_sv, n_a) : Nullch;
4427 if (!name && PERLDB_NAMEANON && CopLINE(PL_curcop)) {
4428 SV *sv = sv_newmortal();
4429 Perl_sv_setpvf(aTHX_ sv, "__ANON__[%s:%"IVdf"]",
4430 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
4435 gv = gv_fetchpv(name ? name : (aname ? aname : "__ANON__"),
4436 GV_ADDMULTI | ((block || attrs) ? 0 : GV_NOINIT),
4446 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
4447 maximum a prototype before. */
4448 if (SvTYPE(gv) > SVt_NULL) {
4449 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1)
4450 && ckWARN_d(WARN_PROTOTYPE))
4452 Perl_warner(aTHX_ WARN_PROTOTYPE, "Runaway prototype");
4454 cv_ckproto((CV*)gv, NULL, ps);
4457 sv_setpv((SV*)gv, ps);
4459 sv_setiv((SV*)gv, -1);
4460 SvREFCNT_dec(PL_compcv);
4461 cv = PL_compcv = NULL;
4462 PL_sub_generation++;
4466 if (!name || GvCVGEN(gv))
4468 else if ((cv = GvCV(gv))) {
4469 bool exists = CvROOT(cv) || CvXSUB(cv);
4470 /* if the subroutine doesn't exist and wasn't pre-declared
4471 * with a prototype, assume it will be AUTOLOADed,
4472 * skipping the prototype check
4474 if (exists || SvPOK(cv))
4475 cv_ckproto(cv, gv, ps);
4476 /* already defined (or promised)? */
4477 if (exists || GvASSUMECV(gv)) {
4479 bool const_changed = TRUE;
4480 if (!block && !attrs) {
4481 /* just a "sub foo;" when &foo is already defined */
4482 SAVEFREESV(PL_compcv);
4485 /* ahem, death to those who redefine active sort subs */
4486 if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv))
4487 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", name);
4490 if ((const_sv = cv_const_sv(cv)))
4491 const_changed = sv_cmp(const_sv, op_const_sv(block, Nullcv));
4492 if ((const_sv && const_changed) || ckWARN(WARN_REDEFINE))
4494 line_t oldline = CopLINE(PL_curcop);
4495 CopLINE_set(PL_curcop, PL_copline);
4496 Perl_warner(aTHX_ WARN_REDEFINE,
4497 const_sv ? "Constant subroutine %s redefined"
4498 : "Subroutine %s redefined", name);
4499 CopLINE_set(PL_curcop, oldline);
4510 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>
4511 * before we clobber PL_compcv.
4515 if (CvGV(cv) && GvSTASH(CvGV(cv)) && HvNAME(GvSTASH(CvGV(cv))))
4516 stash = GvSTASH(CvGV(cv));
4517 else if (CvSTASH(cv) && HvNAME(CvSTASH(cv)))
4518 stash = CvSTASH(cv);
4520 stash = PL_curstash;
4523 /* possibly about to re-define existing subr -- ignore old cv */
4524 rcv = (SV*)PL_compcv;
4525 if (name && GvSTASH(gv) && HvNAME(GvSTASH(gv)))
4526 stash = GvSTASH(gv);
4528 stash = PL_curstash;
4530 apply_attrs(stash, rcv, attrs);
4532 if (cv) { /* must reuse cv if autoloaded */
4534 /* got here with just attrs -- work done, so bug out */
4535 SAVEFREESV(PL_compcv);
4539 CvFLAGS(cv) = CvFLAGS(PL_compcv);
4540 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
4541 CvOUTSIDE(PL_compcv) = 0;
4542 CvPADLIST(cv) = CvPADLIST(PL_compcv);
4543 CvPADLIST(PL_compcv) = 0;
4544 if (SvREFCNT(PL_compcv) > 1) /* XXX Make closures transit through stub. */
4545 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc((SV*)cv);
4546 SvREFCNT_dec(PL_compcv);
4553 PL_sub_generation++;
4556 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4557 CvFILE(cv) = CopFILE(PL_curcop);
4558 CvSTASH(cv) = PL_curstash;
4561 if (!CvMUTEXP(cv)) {
4562 New(666, CvMUTEXP(cv), 1, perl_mutex);
4563 MUTEX_INIT(CvMUTEXP(cv));
4565 #endif /* USE_THREADS */
4568 sv_setpv((SV*)cv, ps);
4570 if (PL_error_count) {
4574 char *s = strrchr(name, ':');
4576 if (strEQ(s, "BEGIN")) {
4578 "BEGIN not safe after errors--compilation aborted";
4579 if (PL_in_eval & EVAL_KEEPERR)
4580 Perl_croak(aTHX_ not_safe);
4582 /* force display of errors found but not reported */
4583 sv_catpv(ERRSV, not_safe);
4584 Perl_croak(aTHX_ "%s", SvPVx(ERRSV, n_a));
4591 PL_copline = NOLINE;
4596 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad))
4597 av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv);
4600 CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0, scalarseq(block));
4603 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4605 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4606 OpREFCNT_set(CvROOT(cv), 1);
4607 CvSTART(cv) = LINKLIST(CvROOT(cv));
4608 CvROOT(cv)->op_next = 0;
4611 /* now that optimizer has done its work, adjust pad values */
4613 SV **namep = AvARRAY(PL_comppad_name);
4614 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4617 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix]))
4620 * The only things that a clonable function needs in its
4621 * pad are references to outer lexicals and anonymous subs.
4622 * The rest are created anew during cloning.
4624 if (!((namesv = namep[ix]) != Nullsv &&
4625 namesv != &PL_sv_undef &&
4627 *SvPVX(namesv) == '&')))
4629 SvREFCNT_dec(PL_curpad[ix]);
4630 PL_curpad[ix] = Nullsv;
4635 AV *av = newAV(); /* Will be @_ */
4637 av_store(PL_comppad, 0, (SV*)av);
4638 AvFLAGS(av) = AVf_REIFY;
4640 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4641 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix]))
4643 if (!SvPADMY(PL_curpad[ix]))
4644 SvPADTMP_on(PL_curpad[ix]);
4648 if (name || aname) {
4650 char *tname = (name ? name : aname);
4652 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4653 SV *sv = NEWSV(0,0);
4654 SV *tmpstr = sv_newmortal();
4655 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
4659 Perl_sv_setpvf(aTHX_ sv, "%s:%ld-%ld",
4661 (long)PL_subline, (long)CopLINE(PL_curcop));
4662 gv_efullname3(tmpstr, gv, Nullch);
4663 hv_store(GvHV(PL_DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
4664 hv = GvHVn(db_postponed);
4665 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
4666 && (pcv = GvCV(db_postponed)))
4672 call_sv((SV*)pcv, G_DISCARD);
4676 if ((s = strrchr(tname,':')))
4681 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4684 if (strEQ(s, "BEGIN")) {
4685 I32 oldscope = PL_scopestack_ix;
4687 SAVECOPFILE(&PL_compiling);
4688 SAVECOPLINE(&PL_compiling);
4690 sv_setsv(PL_rs, PL_nrs);
4693 PL_beginav = newAV();
4694 DEBUG_x( dump_sub(gv) );
4695 av_push(PL_beginav, (SV*)cv);
4696 GvCV(gv) = 0; /* cv has been hijacked */
4697 call_list(oldscope, PL_beginav);
4699 PL_curcop = &PL_compiling;
4700 PL_compiling.op_private = PL_hints;
4703 else if (strEQ(s, "END") && !PL_error_count) {
4706 DEBUG_x( dump_sub(gv) );
4707 av_unshift(PL_endav, 1);
4708 av_store(PL_endav, 0, (SV*)cv);
4709 GvCV(gv) = 0; /* cv has been hijacked */
4711 else if (strEQ(s, "CHECK") && !PL_error_count) {
4713 PL_checkav = newAV();
4714 DEBUG_x( dump_sub(gv) );
4715 if (PL_main_start && ckWARN(WARN_VOID))
4716 Perl_warner(aTHX_ WARN_VOID, "Too late to run CHECK block");
4717 av_unshift(PL_checkav, 1);
4718 av_store(PL_checkav, 0, (SV*)cv);
4719 GvCV(gv) = 0; /* cv has been hijacked */
4721 else if (strEQ(s, "INIT") && !PL_error_count) {
4723 PL_initav = newAV();
4724 DEBUG_x( dump_sub(gv) );
4725 if (PL_main_start && ckWARN(WARN_VOID))
4726 Perl_warner(aTHX_ WARN_VOID, "Too late to run INIT block");
4727 av_push(PL_initav, (SV*)cv);
4728 GvCV(gv) = 0; /* cv has been hijacked */
4733 PL_copline = NOLINE;
4738 /* XXX unsafe for threads if eval_owner isn't held */
4740 =for apidoc newCONSTSUB
4742 Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is
4743 eligible for inlining at compile-time.
4749 Perl_newCONSTSUB(pTHX_ HV *stash, char *name, SV *sv)
4755 SAVECOPLINE(PL_curcop);
4756 CopLINE_set(PL_curcop, PL_copline);
4759 PL_hints &= ~HINT_BLOCK_SCOPE;
4762 SAVESPTR(PL_curstash);
4763 SAVECOPSTASH(PL_curcop);
4764 PL_curstash = stash;
4766 CopSTASHPV(PL_curcop) = stash ? HvNAME(stash) : Nullch;
4768 CopSTASH(PL_curcop) = stash;
4773 start_subparse(FALSE, 0),
4774 newSVOP(OP_CONST, 0, newSVpv(name,0)),
4775 newSVOP(OP_CONST, 0, &PL_sv_no), /* SvPV(&PL_sv_no) == "" -- GMB */
4777 newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv))
4784 =for apidoc U||newXS
4786 Used by C<xsubpp> to hook up XSUBs as Perl subs.
4792 Perl_newXS(pTHX_ char *name, XSUBADDR_t subaddr, char *filename)
4795 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
4798 if ((cv = (name ? GvCV(gv) : Nullcv))) {
4800 /* just a cached method */
4804 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4805 /* already defined (or promised) */
4806 if (ckWARN(WARN_REDEFINE) && !(CvGV(cv) && GvSTASH(CvGV(cv))
4807 && HvNAME(GvSTASH(CvGV(cv)))
4808 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
4809 line_t oldline = CopLINE(PL_curcop);
4810 if (PL_copline != NOLINE)
4811 CopLINE_set(PL_curcop, PL_copline);
4812 Perl_warner(aTHX_ WARN_REDEFINE, "Subroutine %s redefined",name);
4813 CopLINE_set(PL_curcop, oldline);
4820 if (cv) /* must reuse cv if autoloaded */
4823 cv = (CV*)NEWSV(1105,0);
4824 sv_upgrade((SV *)cv, SVt_PVCV);
4828 PL_sub_generation++;
4831 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4833 New(666, CvMUTEXP(cv), 1, perl_mutex);
4834 MUTEX_INIT(CvMUTEXP(cv));
4836 #endif /* USE_THREADS */
4837 (void)gv_fetchfile(filename);
4838 CvFILE(cv) = filename; /* NOTE: not copied, as it is expected to be
4839 an external constant string */
4840 CvXSUB(cv) = subaddr;
4843 char *s = strrchr(name,':');
4849 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4852 if (strEQ(s, "BEGIN")) {
4854 PL_beginav = newAV();
4855 av_push(PL_beginav, (SV*)cv);
4856 GvCV(gv) = 0; /* cv has been hijacked */
4858 else if (strEQ(s, "END")) {
4861 av_unshift(PL_endav, 1);
4862 av_store(PL_endav, 0, (SV*)cv);
4863 GvCV(gv) = 0; /* cv has been hijacked */
4865 else if (strEQ(s, "CHECK")) {
4867 PL_checkav = newAV();
4868 if (PL_main_start && ckWARN(WARN_VOID))
4869 Perl_warner(aTHX_ WARN_VOID, "Too late to run CHECK block");
4870 av_unshift(PL_checkav, 1);
4871 av_store(PL_checkav, 0, (SV*)cv);
4872 GvCV(gv) = 0; /* cv has been hijacked */
4874 else if (strEQ(s, "INIT")) {
4876 PL_initav = newAV();
4877 if (PL_main_start && ckWARN(WARN_VOID))
4878 Perl_warner(aTHX_ WARN_VOID, "Too late to run INIT block");
4879 av_push(PL_initav, (SV*)cv);
4880 GvCV(gv) = 0; /* cv has been hijacked */
4891 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
4901 name = SvPVx(cSVOPo->op_sv, n_a);
4904 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
4906 if ((cv = GvFORM(gv))) {
4907 if (ckWARN(WARN_REDEFINE)) {
4908 line_t oldline = CopLINE(PL_curcop);
4910 CopLINE_set(PL_curcop, PL_copline);
4911 Perl_warner(aTHX_ WARN_REDEFINE, "Format %s redefined",name);
4912 CopLINE_set(PL_curcop, oldline);
4918 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4919 CvFILE(cv) = CopFILE(PL_curcop);
4921 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4922 if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix]))
4923 SvPADTMP_on(PL_curpad[ix]);
4926 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4927 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4928 OpREFCNT_set(CvROOT(cv), 1);
4929 CvSTART(cv) = LINKLIST(CvROOT(cv));
4930 CvROOT(cv)->op_next = 0;
4933 PL_copline = NOLINE;
4938 Perl_newANONLIST(pTHX_ OP *o)
4940 return newUNOP(OP_REFGEN, 0,
4941 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
4945 Perl_newANONHASH(pTHX_ OP *o)
4947 return newUNOP(OP_REFGEN, 0,
4948 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
4952 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
4954 return newANONATTRSUB(floor, proto, Nullop, block);
4958 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
4960 return newUNOP(OP_REFGEN, 0,
4961 newSVOP(OP_ANONCODE, 0,
4962 (SV*)newATTRSUB(floor, 0, proto, attrs, block)));
4966 Perl_oopsAV(pTHX_ OP *o)
4968 switch (o->op_type) {
4970 o->op_type = OP_PADAV;
4971 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4972 return ref(o, OP_RV2AV);
4975 o->op_type = OP_RV2AV;
4976 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4981 if (ckWARN_d(WARN_INTERNAL))
4982 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsAV");
4989 Perl_oopsHV(pTHX_ OP *o)
4993 switch (o->op_type) {
4996 o->op_type = OP_PADHV;
4997 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4998 return ref(o, OP_RV2HV);
5002 o->op_type = OP_RV2HV;
5003 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
5008 if (ckWARN_d(WARN_INTERNAL))
5009 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsHV");
5016 Perl_newAVREF(pTHX_ OP *o)
5018 if (o->op_type == OP_PADANY) {
5019 o->op_type = OP_PADAV;
5020 o->op_ppaddr = PL_ppaddr[OP_PADAV];
5023 return newUNOP(OP_RV2AV, 0, scalar(o));
5027 Perl_newGVREF(pTHX_ I32 type, OP *o)
5029 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
5030 return newUNOP(OP_NULL, 0, o);
5031 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
5035 Perl_newHVREF(pTHX_ OP *o)
5037 if (o->op_type == OP_PADANY) {
5038 o->op_type = OP_PADHV;
5039 o->op_ppaddr = PL_ppaddr[OP_PADHV];
5042 return newUNOP(OP_RV2HV, 0, scalar(o));
5046 Perl_oopsCV(pTHX_ OP *o)
5048 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
5054 Perl_newCVREF(pTHX_ I32 flags, OP *o)
5056 return newUNOP(OP_RV2CV, flags, scalar(o));
5060 Perl_newSVREF(pTHX_ OP *o)
5062 if (o->op_type == OP_PADANY) {
5063 o->op_type = OP_PADSV;
5064 o->op_ppaddr = PL_ppaddr[OP_PADSV];
5067 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
5068 o->op_flags |= OPpDONE_SVREF;
5071 return newUNOP(OP_RV2SV, 0, scalar(o));
5074 /* Check routines. */
5077 Perl_ck_anoncode(pTHX_ OP *o)
5082 name = NEWSV(1106,0);
5083 sv_upgrade(name, SVt_PVNV);
5084 sv_setpvn(name, "&", 1);
5087 ix = pad_alloc(o->op_type, SVs_PADMY);
5088 av_store(PL_comppad_name, ix, name);
5089 av_store(PL_comppad, ix, cSVOPo->op_sv);
5090 SvPADMY_on(cSVOPo->op_sv);
5091 cSVOPo->op_sv = Nullsv;
5092 cSVOPo->op_targ = ix;
5097 Perl_ck_bitop(pTHX_ OP *o)
5099 o->op_private = PL_hints;
5104 Perl_ck_concat(pTHX_ OP *o)
5106 if (cUNOPo->op_first->op_type == OP_CONCAT)
5107 o->op_flags |= OPf_STACKED;
5112 Perl_ck_spair(pTHX_ OP *o)
5114 if (o->op_flags & OPf_KIDS) {
5117 OPCODE type = o->op_type;
5118 o = modkids(ck_fun(o), type);
5119 kid = cUNOPo->op_first;
5120 newop = kUNOP->op_first->op_sibling;
5122 (newop->op_sibling ||
5123 !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
5124 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
5125 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
5129 op_free(kUNOP->op_first);
5130 kUNOP->op_first = newop;
5132 o->op_ppaddr = PL_ppaddr[++o->op_type];
5137 Perl_ck_delete(pTHX_ OP *o)
5141 if (o->op_flags & OPf_KIDS) {
5142 OP *kid = cUNOPo->op_first;
5143 switch (kid->op_type) {
5145 o->op_flags |= OPf_SPECIAL;
5148 o->op_private |= OPpSLICE;
5151 o->op_flags |= OPf_SPECIAL;
5156 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element or slice",
5157 PL_op_desc[o->op_type]);
5165 Perl_ck_eof(pTHX_ OP *o)
5167 I32 type = o->op_type;
5169 if (o->op_flags & OPf_KIDS) {
5170 if (cLISTOPo->op_first->op_type == OP_STUB) {
5172 o = newUNOP(type, OPf_SPECIAL,
5173 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
5181 Perl_ck_eval(pTHX_ OP *o)
5183 PL_hints |= HINT_BLOCK_SCOPE;
5184 if (o->op_flags & OPf_KIDS) {
5185 SVOP *kid = (SVOP*)cUNOPo->op_first;
5188 o->op_flags &= ~OPf_KIDS;
5191 else if (kid->op_type == OP_LINESEQ) {
5194 kid->op_next = o->op_next;
5195 cUNOPo->op_first = 0;
5198 NewOp(1101, enter, 1, LOGOP);
5199 enter->op_type = OP_ENTERTRY;
5200 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
5201 enter->op_private = 0;
5203 /* establish postfix order */
5204 enter->op_next = (OP*)enter;
5206 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
5207 o->op_type = OP_LEAVETRY;
5208 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
5209 enter->op_other = o;
5217 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
5219 o->op_targ = (PADOFFSET)PL_hints;
5224 Perl_ck_exit(pTHX_ OP *o)
5227 HV *table = GvHV(PL_hintgv);
5229 SV **svp = hv_fetch(table, "vmsish_exit", 11, FALSE);
5230 if (svp && *svp && SvTRUE(*svp))
5231 o->op_private |= OPpEXIT_VMSISH;
5238 Perl_ck_exec(pTHX_ OP *o)
5241 if (o->op_flags & OPf_STACKED) {
5243 kid = cUNOPo->op_first->op_sibling;
5244 if (kid->op_type == OP_RV2GV)
5253 Perl_ck_exists(pTHX_ OP *o)
5256 if (o->op_flags & OPf_KIDS) {
5257 OP *kid = cUNOPo->op_first;
5258 if (kid->op_type == OP_ENTERSUB) {
5259 (void) ref(kid, o->op_type);
5260 if (kid->op_type != OP_RV2CV && !PL_error_count)
5261 Perl_croak(aTHX_ "%s argument is not a subroutine name",
5262 PL_op_desc[o->op_type]);
5263 o->op_private |= OPpEXISTS_SUB;
5265 else if (kid->op_type == OP_AELEM)
5266 o->op_flags |= OPf_SPECIAL;
5267 else if (kid->op_type != OP_HELEM)
5268 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element",
5269 PL_op_desc[o->op_type]);
5277 Perl_ck_gvconst(pTHX_ register OP *o)
5279 o = fold_constants(o);
5280 if (o->op_type == OP_CONST)
5287 Perl_ck_rvconst(pTHX_ register OP *o)
5290 SVOP *kid = (SVOP*)cUNOPo->op_first;
5292 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5293 if (kid->op_type == OP_CONST) {
5297 SV *kidsv = kid->op_sv;
5300 /* Is it a constant from cv_const_sv()? */
5301 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
5302 SV *rsv = SvRV(kidsv);
5303 int svtype = SvTYPE(rsv);
5304 char *badtype = Nullch;
5306 switch (o->op_type) {
5308 if (svtype > SVt_PVMG)
5309 badtype = "a SCALAR";
5312 if (svtype != SVt_PVAV)
5313 badtype = "an ARRAY";
5316 if (svtype != SVt_PVHV) {
5317 if (svtype == SVt_PVAV) { /* pseudohash? */
5318 SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
5319 if (ksv && SvROK(*ksv)
5320 && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
5329 if (svtype != SVt_PVCV)
5334 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
5337 name = SvPV(kidsv, n_a);
5338 if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
5339 char *badthing = Nullch;
5340 switch (o->op_type) {
5342 badthing = "a SCALAR";
5345 badthing = "an ARRAY";
5348 badthing = "a HASH";
5353 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
5357 * This is a little tricky. We only want to add the symbol if we
5358 * didn't add it in the lexer. Otherwise we get duplicate strict
5359 * warnings. But if we didn't add it in the lexer, we must at
5360 * least pretend like we wanted to add it even if it existed before,
5361 * or we get possible typo warnings. OPpCONST_ENTERED says
5362 * whether the lexer already added THIS instance of this symbol.
5364 iscv = (o->op_type == OP_RV2CV) * 2;
5366 gv = gv_fetchpv(name,
5367 iscv | !(kid->op_private & OPpCONST_ENTERED),
5370 : o->op_type == OP_RV2SV
5372 : o->op_type == OP_RV2AV
5374 : o->op_type == OP_RV2HV
5377 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
5379 kid->op_type = OP_GV;
5380 SvREFCNT_dec(kid->op_sv);
5382 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
5383 kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
5384 SvREFCNT_dec(PL_curpad[kPADOP->op_padix]);
5386 PL_curpad[kPADOP->op_padix] = SvREFCNT_inc(gv);
5388 kid->op_sv = SvREFCNT_inc(gv);
5390 kid->op_ppaddr = PL_ppaddr[OP_GV];
5397 Perl_ck_ftst(pTHX_ OP *o)
5400 I32 type = o->op_type;
5402 if (o->op_flags & OPf_REF) {
5405 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
5406 SVOP *kid = (SVOP*)cUNOPo->op_first;
5408 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5410 OP *newop = newGVOP(type, OPf_REF,
5411 gv_fetchpv(SvPVx(kid->op_sv, n_a), TRUE, SVt_PVIO));
5418 if (type == OP_FTTTY)
5419 o = newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
5422 o = newUNOP(type, 0, newDEFSVOP());
5425 if (type == OP_FTTEXT || type == OP_FTBINARY) {
5427 if (PL_hints & HINT_LOCALE)
5428 o->op_private |= OPpLOCALE;
5435 Perl_ck_fun(pTHX_ OP *o)
5442 int type = o->op_type;
5443 register I32 oa = PL_opargs[type] >> OASHIFT;
5445 if (o->op_flags & OPf_STACKED) {
5446 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
5449 return no_fh_allowed(o);
5452 if (o->op_flags & OPf_KIDS) {
5454 tokid = &cLISTOPo->op_first;
5455 kid = cLISTOPo->op_first;
5456 if (kid->op_type == OP_PUSHMARK ||
5457 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
5459 tokid = &kid->op_sibling;
5460 kid = kid->op_sibling;
5462 if (!kid && PL_opargs[type] & OA_DEFGV)
5463 *tokid = kid = newDEFSVOP();
5467 sibl = kid->op_sibling;
5470 /* list seen where single (scalar) arg expected? */
5471 if (numargs == 1 && !(oa >> 4)
5472 && kid->op_type == OP_LIST && type != OP_SCALAR)
5474 return too_many_arguments(o,PL_op_desc[type]);
5487 if (kid->op_type == OP_CONST &&
5488 (kid->op_private & OPpCONST_BARE))
5490 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5491 OP *newop = newAVREF(newGVOP(OP_GV, 0,
5492 gv_fetchpv(name, TRUE, SVt_PVAV) ));
5493 if (ckWARN(WARN_DEPRECATED))
5494 Perl_warner(aTHX_ WARN_DEPRECATED,
5495 "Array @%s missing the @ in argument %"IVdf" of %s()",
5496 name, (IV)numargs, PL_op_desc[type]);
5499 kid->op_sibling = sibl;
5502 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
5503 bad_type(numargs, "array", PL_op_desc[type], kid);
5507 if (kid->op_type == OP_CONST &&
5508 (kid->op_private & OPpCONST_BARE))
5510 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5511 OP *newop = newHVREF(newGVOP(OP_GV, 0,
5512 gv_fetchpv(name, TRUE, SVt_PVHV) ));
5513 if (ckWARN(WARN_DEPRECATED))
5514 Perl_warner(aTHX_ WARN_DEPRECATED,
5515 "Hash %%%s missing the %% in argument %"IVdf" of %s()",
5516 name, (IV)numargs, PL_op_desc[type]);
5519 kid->op_sibling = sibl;
5522 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
5523 bad_type(numargs, "hash", PL_op_desc[type], kid);
5528 OP *newop = newUNOP(OP_NULL, 0, kid);
5529 kid->op_sibling = 0;
5531 newop->op_next = newop;
5533 kid->op_sibling = sibl;
5538 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
5539 if (kid->op_type == OP_CONST &&
5540 (kid->op_private & OPpCONST_BARE))
5542 OP *newop = newGVOP(OP_GV, 0,
5543 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, n_a), TRUE,
5548 else if (kid->op_type == OP_READLINE) {
5549 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
5550 bad_type(numargs, "HANDLE", PL_op_desc[o->op_type], kid);
5553 I32 flags = OPf_SPECIAL;
5557 /* is this op a FH constructor? */
5558 if (is_handle_constructor(o,numargs)) {
5559 char *name = Nullch;
5563 /* Set a flag to tell rv2gv to vivify
5564 * need to "prove" flag does not mean something
5565 * else already - NI-S 1999/05/07
5568 if (kid->op_type == OP_PADSV) {
5569 SV **namep = av_fetch(PL_comppad_name,
5571 if (namep && *namep)
5572 name = SvPV(*namep, len);
5574 else if (kid->op_type == OP_RV2SV
5575 && kUNOP->op_first->op_type == OP_GV)
5577 GV *gv = cGVOPx_gv(kUNOP->op_first);
5579 len = GvNAMELEN(gv);
5581 else if (kid->op_type == OP_AELEM
5582 || kid->op_type == OP_HELEM)
5584 name = "__ANONIO__";
5590 targ = pad_alloc(OP_RV2GV, SVs_PADTMP);
5591 namesv = PL_curpad[targ];
5592 (void)SvUPGRADE(namesv, SVt_PV);
5594 sv_setpvn(namesv, "$", 1);
5595 sv_catpvn(namesv, name, len);
5598 kid->op_sibling = 0;
5599 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
5600 kid->op_targ = targ;
5601 kid->op_private |= priv;
5603 kid->op_sibling = sibl;
5609 mod(scalar(kid), type);
5613 tokid = &kid->op_sibling;
5614 kid = kid->op_sibling;
5616 o->op_private |= numargs;
5618 return too_many_arguments(o,PL_op_desc[o->op_type]);
5621 else if (PL_opargs[type] & OA_DEFGV) {
5623 return newUNOP(type, 0, newDEFSVOP());
5627 while (oa & OA_OPTIONAL)
5629 if (oa && oa != OA_LIST)
5630 return too_few_arguments(o,PL_op_desc[o->op_type]);
5636 Perl_ck_glob(pTHX_ OP *o)
5641 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
5642 append_elem(OP_GLOB, o, newDEFSVOP());
5644 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
5645 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5647 #if !defined(PERL_EXTERNAL_GLOB)
5648 /* XXX this can be tightened up and made more failsafe. */
5651 Perl_load_module(aTHX_ 0, newSVpvn("File::Glob", 10), Nullsv,
5652 /* null-terminated import list */
5653 newSVpvn(":globally", 9), Nullsv);
5654 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5657 #endif /* PERL_EXTERNAL_GLOB */
5659 if (gv && GvIMPORTED_CV(gv)) {
5660 append_elem(OP_GLOB, o,
5661 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
5662 o->op_type = OP_LIST;
5663 o->op_ppaddr = PL_ppaddr[OP_LIST];
5664 cLISTOPo->op_first->op_type = OP_PUSHMARK;
5665 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
5666 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
5667 append_elem(OP_LIST, o,
5668 scalar(newUNOP(OP_RV2CV, 0,
5669 newGVOP(OP_GV, 0, gv)))));
5670 o = newUNOP(OP_NULL, 0, ck_subr(o));
5671 o->op_targ = OP_GLOB; /* hint at what it used to be */
5674 gv = newGVgen("main");
5676 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
5682 Perl_ck_grep(pTHX_ OP *o)
5686 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
5688 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
5689 NewOp(1101, gwop, 1, LOGOP);
5691 if (o->op_flags & OPf_STACKED) {
5694 kid = cLISTOPo->op_first->op_sibling;
5695 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
5698 kid->op_next = (OP*)gwop;
5699 o->op_flags &= ~OPf_STACKED;
5701 kid = cLISTOPo->op_first->op_sibling;
5702 if (type == OP_MAPWHILE)
5709 kid = cLISTOPo->op_first->op_sibling;
5710 if (kid->op_type != OP_NULL)
5711 Perl_croak(aTHX_ "panic: ck_grep");
5712 kid = kUNOP->op_first;
5714 gwop->op_type = type;
5715 gwop->op_ppaddr = PL_ppaddr[type];
5716 gwop->op_first = listkids(o);
5717 gwop->op_flags |= OPf_KIDS;
5718 gwop->op_private = 1;
5719 gwop->op_other = LINKLIST(kid);
5720 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
5721 kid->op_next = (OP*)gwop;
5723 kid = cLISTOPo->op_first->op_sibling;
5724 if (!kid || !kid->op_sibling)
5725 return too_few_arguments(o,PL_op_desc[o->op_type]);
5726 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
5727 mod(kid, OP_GREPSTART);
5733 Perl_ck_index(pTHX_ OP *o)
5735 if (o->op_flags & OPf_KIDS) {
5736 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5738 kid = kid->op_sibling; /* get past "big" */
5739 if (kid && kid->op_type == OP_CONST)
5740 fbm_compile(((SVOP*)kid)->op_sv, 0);
5746 Perl_ck_lengthconst(pTHX_ OP *o)
5748 /* XXX length optimization goes here */
5753 Perl_ck_lfun(pTHX_ OP *o)
5755 OPCODE type = o->op_type;
5756 return modkids(ck_fun(o), type);
5760 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
5763 if ((o->op_flags & OPf_KIDS) && ckWARN(WARN_DEPRECATED)) {
5764 switch (cUNOPo->op_first->op_type) {
5766 break; /* Globals via GV can be undef */
5768 case OP_AASSIGN: /* Is this a good idea? */
5769 Perl_warner(aTHX_ WARN_DEPRECATED,
5770 "defined(@array) is deprecated");
5771 Perl_warner(aTHX_ WARN_DEPRECATED,
5772 "\t(Maybe you should just omit the defined()?)\n");
5775 break; /* Globals via GV can be undef */
5777 Perl_warner(aTHX_ WARN_DEPRECATED,
5778 "defined(%%hash) is deprecated");
5779 Perl_warner(aTHX_ WARN_DEPRECATED,
5780 "\t(Maybe you should just omit the defined()?)\n");
5791 Perl_ck_rfun(pTHX_ OP *o)
5793 OPCODE type = o->op_type;
5794 return refkids(ck_fun(o), type);
5798 Perl_ck_listiob(pTHX_ OP *o)
5802 kid = cLISTOPo->op_first;
5805 kid = cLISTOPo->op_first;
5807 if (kid->op_type == OP_PUSHMARK)
5808 kid = kid->op_sibling;
5809 if (kid && o->op_flags & OPf_STACKED)
5810 kid = kid->op_sibling;
5811 else if (kid && !kid->op_sibling) { /* print HANDLE; */
5812 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
5813 o->op_flags |= OPf_STACKED; /* make it a filehandle */
5814 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
5815 cLISTOPo->op_first->op_sibling = kid;
5816 cLISTOPo->op_last = kid;
5817 kid = kid->op_sibling;
5822 append_elem(o->op_type, o, newDEFSVOP());
5828 if (PL_hints & HINT_LOCALE)
5829 o->op_private |= OPpLOCALE;
5836 Perl_ck_fun_locale(pTHX_ OP *o)
5842 if (PL_hints & HINT_LOCALE)
5843 o->op_private |= OPpLOCALE;
5850 Perl_ck_sassign(pTHX_ OP *o)
5852 OP *kid = cLISTOPo->op_first;
5853 /* has a disposable target? */
5854 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
5855 && !(kid->op_flags & OPf_STACKED)
5856 /* Cannot steal the second time! */
5857 && !(kid->op_private & OPpTARGET_MY))
5859 OP *kkid = kid->op_sibling;
5861 /* Can just relocate the target. */
5862 if (kkid && kkid->op_type == OP_PADSV
5863 && !(kkid->op_private & OPpLVAL_INTRO))
5865 kid->op_targ = kkid->op_targ;
5867 /* Now we do not need PADSV and SASSIGN. */
5868 kid->op_sibling = o->op_sibling; /* NULL */
5869 cLISTOPo->op_first = NULL;
5872 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
5880 Perl_ck_scmp(pTHX_ OP *o)
5884 if (PL_hints & HINT_LOCALE)
5885 o->op_private |= OPpLOCALE;
5892 Perl_ck_match(pTHX_ OP *o)
5894 o->op_private |= OPpRUNTIME;
5899 Perl_ck_method(pTHX_ OP *o)
5901 OP *kid = cUNOPo->op_first;
5902 if (kid->op_type == OP_CONST) {
5903 SV* sv = kSVOP->op_sv;
5904 if (!(strchr(SvPVX(sv), ':') || strchr(SvPVX(sv), '\''))) {
5906 (void)SvUPGRADE(sv, SVt_PVIV);
5908 PERL_HASH(SvUVX(sv), SvPVX(sv), SvCUR(sv));
5909 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
5910 kSVOP->op_sv = Nullsv;
5919 Perl_ck_null(pTHX_ OP *o)
5925 Perl_ck_open(pTHX_ OP *o)
5927 HV *table = GvHV(PL_hintgv);
5931 svp = hv_fetch(table, "open_IN", 7, FALSE);
5933 mode = mode_from_discipline(*svp);
5934 if (mode & O_BINARY)
5935 o->op_private |= OPpOPEN_IN_RAW;
5936 else if (mode & O_TEXT)
5937 o->op_private |= OPpOPEN_IN_CRLF;
5940 svp = hv_fetch(table, "open_OUT", 8, FALSE);
5942 mode = mode_from_discipline(*svp);
5943 if (mode & O_BINARY)
5944 o->op_private |= OPpOPEN_OUT_RAW;
5945 else if (mode & O_TEXT)
5946 o->op_private |= OPpOPEN_OUT_CRLF;
5949 if (o->op_type == OP_BACKTICK)
5955 Perl_ck_repeat(pTHX_ OP *o)
5957 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
5958 o->op_private |= OPpREPEAT_DOLIST;
5959 cBINOPo->op_first = force_list(cBINOPo->op_first);
5967 Perl_ck_require(pTHX_ OP *o)
5969 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
5970 SVOP *kid = (SVOP*)cUNOPo->op_first;
5972 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5974 for (s = SvPVX(kid->op_sv); *s; s++) {
5975 if (*s == ':' && s[1] == ':') {
5977 Move(s+2, s+1, strlen(s+2)+1, char);
5978 --SvCUR(kid->op_sv);
5981 if (SvREADONLY(kid->op_sv)) {
5982 SvREADONLY_off(kid->op_sv);
5983 sv_catpvn(kid->op_sv, ".pm", 3);
5984 SvREADONLY_on(kid->op_sv);
5987 sv_catpvn(kid->op_sv, ".pm", 3);
5995 Perl_ck_retarget(pTHX_ OP *o)
5997 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
6004 Perl_ck_select(pTHX_ OP *o)
6007 if (o->op_flags & OPf_KIDS) {
6008 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6009 if (kid && kid->op_sibling) {
6010 o->op_type = OP_SSELECT;
6011 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
6013 return fold_constants(o);
6017 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6018 if (kid && kid->op_type == OP_RV2GV)
6019 kid->op_private &= ~HINT_STRICT_REFS;
6024 Perl_ck_shift(pTHX_ OP *o)
6026 I32 type = o->op_type;
6028 if (!(o->op_flags & OPf_KIDS)) {
6033 if (!CvUNIQUE(PL_compcv)) {
6034 argop = newOP(OP_PADAV, OPf_REF);
6035 argop->op_targ = 0; /* PL_curpad[0] is @_ */
6038 argop = newUNOP(OP_RV2AV, 0,
6039 scalar(newGVOP(OP_GV, 0,
6040 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
6043 argop = newUNOP(OP_RV2AV, 0,
6044 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(PL_compcv) ?
6045 PL_defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
6046 #endif /* USE_THREADS */
6047 return newUNOP(type, 0, scalar(argop));
6049 return scalar(modkids(ck_fun(o), type));
6053 Perl_ck_sort(pTHX_ OP *o)
6058 if (PL_hints & HINT_LOCALE)
6059 o->op_private |= OPpLOCALE;
6062 if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
6064 firstkid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6065 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
6067 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */
6069 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
6071 if (kid->op_type == OP_SCOPE) {
6075 else if (kid->op_type == OP_LEAVE) {
6076 if (o->op_type == OP_SORT) {
6077 null(kid); /* wipe out leave */
6080 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
6081 if (k->op_next == kid)
6083 /* don't descend into loops */
6084 else if (k->op_type == OP_ENTERLOOP
6085 || k->op_type == OP_ENTERITER)
6087 k = cLOOPx(k)->op_lastop;
6092 kid->op_next = 0; /* just disconnect the leave */
6093 k = kLISTOP->op_first;
6098 if (o->op_type == OP_SORT) {
6099 /* provide scalar context for comparison function/block */
6105 o->op_flags |= OPf_SPECIAL;
6107 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
6110 firstkid = firstkid->op_sibling;
6113 /* provide list context for arguments */
6114 if (o->op_type == OP_SORT)
6121 S_simplify_sort(pTHX_ OP *o)
6124 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6128 if (!(o->op_flags & OPf_STACKED))
6130 GvMULTI_on(gv_fetchpv("a", TRUE, SVt_PV));
6131 GvMULTI_on(gv_fetchpv("b", TRUE, SVt_PV));
6132 kid = kUNOP->op_first; /* get past null */
6133 if (kid->op_type != OP_SCOPE)
6135 kid = kLISTOP->op_last; /* get past scope */
6136 switch(kid->op_type) {
6144 k = kid; /* remember this node*/
6145 if (kBINOP->op_first->op_type != OP_RV2SV)
6147 kid = kBINOP->op_first; /* get past cmp */
6148 if (kUNOP->op_first->op_type != OP_GV)
6150 kid = kUNOP->op_first; /* get past rv2sv */
6152 if (GvSTASH(gv) != PL_curstash)
6154 if (strEQ(GvNAME(gv), "a"))
6156 else if (strEQ(GvNAME(gv), "b"))
6160 kid = k; /* back to cmp */
6161 if (kBINOP->op_last->op_type != OP_RV2SV)
6163 kid = kBINOP->op_last; /* down to 2nd arg */
6164 if (kUNOP->op_first->op_type != OP_GV)
6166 kid = kUNOP->op_first; /* get past rv2sv */
6168 if (GvSTASH(gv) != PL_curstash
6170 ? strNE(GvNAME(gv), "a")
6171 : strNE(GvNAME(gv), "b")))
6173 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
6175 o->op_private |= OPpSORT_REVERSE;
6176 if (k->op_type == OP_NCMP)
6177 o->op_private |= OPpSORT_NUMERIC;
6178 if (k->op_type == OP_I_NCMP)
6179 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
6180 kid = cLISTOPo->op_first->op_sibling;
6181 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
6182 op_free(kid); /* then delete it */
6183 cLISTOPo->op_children--;
6187 Perl_ck_split(pTHX_ OP *o)
6191 if (o->op_flags & OPf_STACKED)
6192 return no_fh_allowed(o);
6194 kid = cLISTOPo->op_first;
6195 if (kid->op_type != OP_NULL)
6196 Perl_croak(aTHX_ "panic: ck_split");
6197 kid = kid->op_sibling;
6198 op_free(cLISTOPo->op_first);
6199 cLISTOPo->op_first = kid;
6201 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvn(" ", 1));
6202 cLISTOPo->op_last = kid; /* There was only one element previously */
6205 if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) {
6206 OP *sibl = kid->op_sibling;
6207 kid->op_sibling = 0;
6208 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
6209 if (cLISTOPo->op_first == cLISTOPo->op_last)
6210 cLISTOPo->op_last = kid;
6211 cLISTOPo->op_first = kid;
6212 kid->op_sibling = sibl;
6215 kid->op_type = OP_PUSHRE;
6216 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
6219 if (!kid->op_sibling)
6220 append_elem(OP_SPLIT, o, newDEFSVOP());
6222 kid = kid->op_sibling;
6225 if (!kid->op_sibling)
6226 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
6228 kid = kid->op_sibling;
6231 if (kid->op_sibling)
6232 return too_many_arguments(o,PL_op_desc[o->op_type]);
6238 Perl_ck_join(pTHX_ OP *o)
6240 if (ckWARN(WARN_SYNTAX)) {
6241 OP *kid = cLISTOPo->op_first->op_sibling;
6242 if (kid && kid->op_type == OP_MATCH) {
6243 char *pmstr = "STRING";
6244 if (kPMOP->op_pmregexp)
6245 pmstr = kPMOP->op_pmregexp->precomp;
6246 Perl_warner(aTHX_ WARN_SYNTAX,
6247 "/%s/ should probably be written as \"%s\"",
6255 Perl_ck_subr(pTHX_ OP *o)
6258 OP *prev = ((cUNOPo->op_first->op_sibling)
6259 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
6260 OP *o2 = prev->op_sibling;
6269 o->op_private |= OPpENTERSUB_HASTARG;
6270 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
6271 if (cvop->op_type == OP_RV2CV) {
6273 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
6274 null(cvop); /* disable rv2cv */
6275 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
6276 if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) {
6277 GV *gv = cGVOPx_gv(tmpop);
6280 tmpop->op_private |= OPpEARLY_CV;
6281 else if (SvPOK(cv)) {
6282 namegv = CvANON(cv) ? gv : CvGV(cv);
6283 proto = SvPV((SV*)cv, n_a);
6287 else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
6288 if (o2->op_type == OP_CONST)
6289 o2->op_private &= ~OPpCONST_STRICT;
6290 else if (o2->op_type == OP_LIST) {
6291 OP *o = ((UNOP*)o2)->op_first->op_sibling;
6292 if (o && o->op_type == OP_CONST)
6293 o->op_private &= ~OPpCONST_STRICT;
6296 o->op_private |= (PL_hints & HINT_STRICT_REFS);
6297 if (PERLDB_SUB && PL_curstash != PL_debstash)
6298 o->op_private |= OPpENTERSUB_DB;
6299 while (o2 != cvop) {
6303 return too_many_arguments(o, gv_ename(namegv));
6321 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
6323 arg == 1 ? "block or sub {}" : "sub {}",
6324 gv_ename(namegv), o2);
6327 /* '*' allows any scalar type, including bareword */
6330 if (o2->op_type == OP_RV2GV)
6331 goto wrapref; /* autoconvert GLOB -> GLOBref */
6332 else if (o2->op_type == OP_CONST)
6333 o2->op_private &= ~OPpCONST_STRICT;
6334 else if (o2->op_type == OP_ENTERSUB) {
6335 /* accidental subroutine, revert to bareword */
6336 OP *gvop = ((UNOP*)o2)->op_first;
6337 if (gvop && gvop->op_type == OP_NULL) {
6338 gvop = ((UNOP*)gvop)->op_first;
6340 for (; gvop->op_sibling; gvop = gvop->op_sibling)
6343 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
6344 (gvop = ((UNOP*)gvop)->op_first) &&
6345 gvop->op_type == OP_GV)
6347 GV *gv = cGVOPx_gv(gvop);
6348 OP *sibling = o2->op_sibling;
6349 SV *n = newSVpvn("",0);
6351 gv_fullname3(n, gv, "");
6352 if (SvCUR(n)>6 && strnEQ(SvPVX(n),"main::",6))
6353 sv_chop(n, SvPVX(n)+6);
6354 o2 = newSVOP(OP_CONST, 0, n);
6355 prev->op_sibling = o2;
6356 o2->op_sibling = sibling;
6368 if (o2->op_type != OP_RV2GV)
6369 bad_type(arg, "symbol", gv_ename(namegv), o2);
6372 if (o2->op_type != OP_ENTERSUB)
6373 bad_type(arg, "subroutine entry", gv_ename(namegv), o2);
6376 if (o2->op_type != OP_RV2SV
6377 && o2->op_type != OP_PADSV
6378 && o2->op_type != OP_HELEM
6379 && o2->op_type != OP_AELEM
6380 && o2->op_type != OP_THREADSV)
6382 bad_type(arg, "scalar", gv_ename(namegv), o2);
6386 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
6387 bad_type(arg, "array", gv_ename(namegv), o2);
6390 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
6391 bad_type(arg, "hash", gv_ename(namegv), o2);
6395 OP* sib = kid->op_sibling;
6396 kid->op_sibling = 0;
6397 o2 = newUNOP(OP_REFGEN, 0, kid);
6398 o2->op_sibling = sib;
6399 prev->op_sibling = o2;
6410 Perl_croak(aTHX_ "Malformed prototype for %s: %s",
6411 gv_ename(namegv), SvPV((SV*)cv, n_a));
6416 mod(o2, OP_ENTERSUB);
6418 o2 = o2->op_sibling;
6420 if (proto && !optional &&
6421 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
6422 return too_few_arguments(o, gv_ename(namegv));
6427 Perl_ck_svconst(pTHX_ OP *o)
6429 SvREADONLY_on(cSVOPo->op_sv);
6434 Perl_ck_trunc(pTHX_ OP *o)
6436 if (o->op_flags & OPf_KIDS) {
6437 SVOP *kid = (SVOP*)cUNOPo->op_first;
6439 if (kid->op_type == OP_NULL)
6440 kid = (SVOP*)kid->op_sibling;
6441 if (kid && kid->op_type == OP_CONST &&
6442 (kid->op_private & OPpCONST_BARE))
6444 o->op_flags |= OPf_SPECIAL;
6445 kid->op_private &= ~OPpCONST_STRICT;
6451 /* A peephole optimizer. We visit the ops in the order they're to execute. */
6454 Perl_peep(pTHX_ register OP *o)
6457 register OP* oldop = 0;
6459 OP *last_composite = Nullop;
6461 if (!o || o->op_seq)
6465 SAVEVPTR(PL_curcop);
6466 for (; o; o = o->op_next) {
6472 switch (o->op_type) {
6476 PL_curcop = ((COP*)o); /* for warnings */
6477 o->op_seq = PL_op_seqmax++;
6478 last_composite = Nullop;
6482 if (cSVOPo->op_private & OPpCONST_STRICT)
6483 no_bareword_allowed(o);
6485 /* Relocate sv to the pad for thread safety.
6486 * Despite being a "constant", the SV is written to,
6487 * for reference counts, sv_upgrade() etc. */
6489 PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP);
6490 if (SvPADTMP(cSVOPo->op_sv)) {
6491 /* If op_sv is already a PADTMP then it is being used by
6492 * another pad, so make a copy. */
6493 sv_setsv(PL_curpad[ix],cSVOPo->op_sv);
6494 SvREADONLY_on(PL_curpad[ix]);
6495 SvREFCNT_dec(cSVOPo->op_sv);
6498 SvREFCNT_dec(PL_curpad[ix]);
6499 SvPADTMP_on(cSVOPo->op_sv);
6500 PL_curpad[ix] = cSVOPo->op_sv;
6502 cSVOPo->op_sv = Nullsv;
6506 o->op_seq = PL_op_seqmax++;
6510 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
6511 if (o->op_next->op_private & OPpTARGET_MY) {
6512 if (o->op_flags & OPf_STACKED) /* chained concats */
6513 goto ignore_optimization;
6515 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
6516 o->op_targ = o->op_next->op_targ;
6517 o->op_next->op_targ = 0;
6518 o->op_private |= OPpTARGET_MY;
6523 ignore_optimization:
6524 o->op_seq = PL_op_seqmax++;
6527 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
6528 o->op_seq = PL_op_seqmax++;
6529 break; /* Scalar stub must produce undef. List stub is noop */
6533 if (o->op_targ == OP_NEXTSTATE
6534 || o->op_targ == OP_DBSTATE
6535 || o->op_targ == OP_SETSTATE)
6537 PL_curcop = ((COP*)o);
6544 if (oldop && o->op_next) {
6545 oldop->op_next = o->op_next;
6548 o->op_seq = PL_op_seqmax++;
6552 if (o->op_next->op_type == OP_RV2SV) {
6553 if (!(o->op_next->op_private & OPpDEREF)) {
6555 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
6557 o->op_next = o->op_next->op_next;
6558 o->op_type = OP_GVSV;
6559 o->op_ppaddr = PL_ppaddr[OP_GVSV];
6562 else if (o->op_next->op_type == OP_RV2AV) {
6563 OP* pop = o->op_next->op_next;
6565 if (pop->op_type == OP_CONST &&
6566 (PL_op = pop->op_next) &&
6567 pop->op_next->op_type == OP_AELEM &&
6568 !(pop->op_next->op_private &
6569 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
6570 (i = SvIV(((SVOP*)pop)->op_sv) - PL_compiling.cop_arybase)
6578 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
6579 o->op_next = pop->op_next->op_next;
6580 o->op_type = OP_AELEMFAST;
6581 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
6582 o->op_private = (U8)i;
6587 else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
6589 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX(GvCV(gv))) {
6590 /* XXX could check prototype here instead of just carping */
6591 SV *sv = sv_newmortal();
6592 gv_efullname3(sv, gv, Nullch);
6593 Perl_warner(aTHX_ WARN_PROTOTYPE,
6594 "%s() called too early to check prototype",
6599 o->op_seq = PL_op_seqmax++;
6610 o->op_seq = PL_op_seqmax++;
6611 while (cLOGOP->op_other->op_type == OP_NULL)
6612 cLOGOP->op_other = cLOGOP->op_other->op_next;
6613 peep(cLOGOP->op_other);
6617 o->op_seq = PL_op_seqmax++;
6618 peep(cLOOP->op_redoop);
6619 peep(cLOOP->op_nextop);
6620 peep(cLOOP->op_lastop);
6626 o->op_seq = PL_op_seqmax++;
6627 peep(cPMOP->op_pmreplstart);
6631 o->op_seq = PL_op_seqmax++;
6632 if (ckWARN(WARN_SYNTAX) && o->op_next
6633 && o->op_next->op_type == OP_NEXTSTATE) {
6634 if (o->op_next->op_sibling &&
6635 o->op_next->op_sibling->op_type != OP_EXIT &&
6636 o->op_next->op_sibling->op_type != OP_WARN &&
6637 o->op_next->op_sibling->op_type != OP_DIE) {
6638 line_t oldline = CopLINE(PL_curcop);
6640 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_next));
6641 Perl_warner(aTHX_ WARN_EXEC,
6642 "Statement unlikely to be reached");
6643 Perl_warner(aTHX_ WARN_EXEC,
6644 "\t(Maybe you meant system() when you said exec()?)\n");
6645 CopLINE_set(PL_curcop, oldline);
6654 SV **svp, **indsvp, *sv;
6659 o->op_seq = PL_op_seqmax++;
6660 if ((o->op_private & (OPpLVAL_INTRO))
6661 || ((BINOP*)o)->op_last->op_type != OP_CONST)
6663 rop = (UNOP*)((BINOP*)o)->op_first;
6664 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6666 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6667 if (!SvOBJECT(lexname))
6669 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6670 if (!fields || !GvHV(*fields))
6672 svp = cSVOPx_svp(((BINOP*)o)->op_last);
6673 key = SvPV(*svp, keylen);
6674 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
6676 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" in variable %s of type %s",
6677 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
6679 ind = SvIV(*indsvp);
6681 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6682 rop->op_type = OP_RV2AV;
6683 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6684 o->op_type = OP_AELEM;
6685 o->op_ppaddr = PL_ppaddr[OP_AELEM];
6687 if (SvREADONLY(*svp))
6689 SvFLAGS(sv) |= (SvFLAGS(*svp)
6690 & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
6700 SV **svp, **indsvp, *sv;
6704 SVOP *first_key_op, *key_op;
6706 o->op_seq = PL_op_seqmax++;
6707 if ((o->op_private & (OPpLVAL_INTRO))
6708 /* I bet there's always a pushmark... */
6709 || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST)
6710 /* hmmm, no optimization if list contains only one key. */
6712 rop = (UNOP*)((LISTOP*)o)->op_last;
6713 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6715 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6716 if (!SvOBJECT(lexname))
6718 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6719 if (!fields || !GvHV(*fields))
6721 /* Again guessing that the pushmark can be jumped over.... */
6722 first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling)
6723 ->op_first->op_sibling;
6724 /* Check that the key list contains only constants. */
6725 for (key_op = first_key_op; key_op;
6726 key_op = (SVOP*)key_op->op_sibling)
6727 if (key_op->op_type != OP_CONST)
6731 rop->op_type = OP_RV2AV;
6732 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6733 o->op_type = OP_ASLICE;
6734 o->op_ppaddr = PL_ppaddr[OP_ASLICE];
6735 for (key_op = first_key_op; key_op;
6736 key_op = (SVOP*)key_op->op_sibling) {
6737 svp = cSVOPx_svp(key_op);
6738 key = SvPV(*svp, keylen);
6739 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
6741 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" "
6742 "in variable %s of type %s",
6743 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
6745 ind = SvIV(*indsvp);
6747 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6749 if (SvREADONLY(*svp))
6751 SvFLAGS(sv) |= (SvFLAGS(*svp)
6752 & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
6761 if (!(o->op_flags & OPf_WANT)
6762 || (o->op_flags & OPf_WANT) == OPf_WANT_LIST)
6766 o->op_seq = PL_op_seqmax++;
6770 if (o->op_next && o->op_next->op_type != OP_LEAVESUBLV) {
6771 o->op_seq = PL_op_seqmax++;
6777 if (last_composite) {
6778 OP *r = last_composite;
6780 while (r->op_sibling)
6783 || (r->op_next->op_type == OP_LIST
6784 && r->op_next->op_next == o))
6786 if (last_composite->op_type == OP_RV2AV)
6787 yyerror("Lvalue subs returning arrays not implemented yet");
6789 yyerror("Lvalue subs returning hashes not implemented yet");
6796 o->op_seq = PL_op_seqmax++;