3 * Copyright (c) 1991-2001, 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
58 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
61 S_gv_ename(pTHX_ GV *gv)
64 SV* tmpsv = sv_newmortal();
65 gv_efullname3(tmpsv, gv, Nullch);
66 return SvPV(tmpsv,n_a);
70 S_no_fh_allowed(pTHX_ OP *o)
72 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
73 PL_op_desc[o->op_type]));
78 S_too_few_arguments(pTHX_ OP *o, char *name)
80 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
85 S_too_many_arguments(pTHX_ OP *o, char *name)
87 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
92 S_bad_type(pTHX_ I32 n, char *t, char *name, OP *kid)
94 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
95 (int)n, name, t, PL_op_desc[kid->op_type]));
99 S_no_bareword_allowed(pTHX_ OP *o)
101 qerror(Perl_mess(aTHX_
102 "Bareword \"%s\" not allowed while \"strict subs\" in use",
103 SvPV_nolen(cSVOPo_sv)));
107 S_trlist_upgrade(pTHX_ U8** sp, U8** ep)
113 Newz(801, d, (e - s) * 2, U8);
117 if (*s < 0x80 || *s == 0xff)
121 *d++ = ((c >> 6) | 0xc0);
122 *d++ = ((c & 0x3f) | 0x80);
130 /* "register" allocation */
133 Perl_pad_allocmy(pTHX_ char *name)
138 if (!(PL_in_my == KEY_our ||
140 (PL_hints & HINT_UTF8 && UTF8_IS_START(name[1])) ||
141 (name[1] == '_' && (int)strlen(name) > 2)))
143 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
144 /* 1999-02-27 mjd@plover.com */
146 p = strchr(name, '\0');
147 /* The next block assumes the buffer is at least 205 chars
148 long. At present, it's always at least 256 chars. */
150 strcpy(name+200, "...");
156 /* Move everything else down one character */
157 for (; p-name > 2; p--)
159 name[2] = toCTRL(name[1]);
162 yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
164 if (ckWARN(WARN_MISC) && AvFILLp(PL_comppad_name) >= 0) {
165 SV **svp = AvARRAY(PL_comppad_name);
166 HV *ourstash = (PL_curstash ? PL_curstash : PL_defstash);
167 PADOFFSET top = AvFILLp(PL_comppad_name);
168 for (off = top; off > PL_comppad_name_floor; off--) {
170 && sv != &PL_sv_undef
171 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
172 && (PL_in_my != KEY_our
173 || ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash))
174 && strEQ(name, SvPVX(sv)))
176 Perl_warner(aTHX_ WARN_MISC,
177 "\"%s\" variable %s masks earlier declaration in same %s",
178 (PL_in_my == KEY_our ? "our" : "my"),
180 (SvIVX(sv) == PAD_MAX ? "scope" : "statement"));
185 if (PL_in_my == KEY_our) {
188 && sv != &PL_sv_undef
189 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
190 && ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash)
191 && strEQ(name, SvPVX(sv)))
193 Perl_warner(aTHX_ WARN_MISC,
194 "\"our\" variable %s redeclared", name);
195 Perl_warner(aTHX_ WARN_MISC,
196 "\t(Did you mean \"local\" instead of \"our\"?)\n");
199 } while ( off-- > 0 );
202 off = pad_alloc(OP_PADSV, SVs_PADMY);
204 sv_upgrade(sv, SVt_PVNV);
206 if (PL_in_my_stash) {
208 yyerror(Perl_form(aTHX_ "Can't declare class for non-scalar %s in \"%s\"",
209 name, PL_in_my == KEY_our ? "our" : "my"));
211 (void)SvUPGRADE(sv, SVt_PVMG);
212 SvSTASH(sv) = (HV*)SvREFCNT_inc(PL_in_my_stash);
215 if (PL_in_my == KEY_our) {
216 (void)SvUPGRADE(sv, SVt_PVGV);
217 GvSTASH(sv) = (HV*)SvREFCNT_inc(PL_curstash ? (SV*)PL_curstash : (SV*)PL_defstash);
218 SvFLAGS(sv) |= SVpad_OUR;
220 av_store(PL_comppad_name, off, sv);
221 SvNVX(sv) = (NV)PAD_MAX;
222 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
223 if (!PL_min_intro_pending)
224 PL_min_intro_pending = off;
225 PL_max_intro_pending = off;
227 av_store(PL_comppad, off, (SV*)newAV());
228 else if (*name == '%')
229 av_store(PL_comppad, off, (SV*)newHV());
230 SvPADMY_on(PL_curpad[off]);
235 S_pad_addlex(pTHX_ SV *proto_namesv)
237 SV *namesv = NEWSV(1103,0);
238 PADOFFSET newoff = pad_alloc(OP_PADSV, SVs_PADMY);
239 sv_upgrade(namesv, SVt_PVNV);
240 sv_setpv(namesv, SvPVX(proto_namesv));
241 av_store(PL_comppad_name, newoff, namesv);
242 SvNVX(namesv) = (NV)PL_curcop->cop_seq;
243 SvIVX(namesv) = PAD_MAX; /* A ref, intro immediately */
244 SvFAKE_on(namesv); /* A ref, not a real var */
245 if (SvFLAGS(proto_namesv) & SVpad_OUR) { /* An "our" variable */
246 SvFLAGS(namesv) |= SVpad_OUR;
247 (void)SvUPGRADE(namesv, SVt_PVGV);
248 GvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)GvSTASH(proto_namesv));
250 if (SvOBJECT(proto_namesv)) { /* A typed var */
252 (void)SvUPGRADE(namesv, SVt_PVMG);
253 SvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)SvSTASH(proto_namesv));
259 #define FINDLEX_NOSEARCH 1 /* don't search outer contexts */
262 S_pad_findlex(pTHX_ char *name, PADOFFSET newoff, U32 seq, CV* startcv,
263 I32 cx_ix, I32 saweval, U32 flags)
269 register PERL_CONTEXT *cx;
271 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
272 AV *curlist = CvPADLIST(cv);
273 SV **svp = av_fetch(curlist, 0, FALSE);
276 if (!svp || *svp == &PL_sv_undef)
279 svp = AvARRAY(curname);
280 for (off = AvFILLp(curname); off > 0; off--) {
281 if ((sv = svp[off]) &&
282 sv != &PL_sv_undef &&
284 seq > I_32(SvNVX(sv)) &&
285 strEQ(SvPVX(sv), name))
296 return 0; /* don't clone from inactive stack frame */
300 oldpad = (AV*)AvARRAY(curlist)[depth];
301 oldsv = *av_fetch(oldpad, off, TRUE);
302 if (!newoff) { /* Not a mere clone operation. */
303 newoff = pad_addlex(sv);
304 if (CvANON(PL_compcv) || SvTYPE(PL_compcv) == SVt_PVFM) {
305 /* "It's closures all the way down." */
306 CvCLONE_on(PL_compcv);
308 if (CvANON(PL_compcv))
309 oldsv = Nullsv; /* no need to keep ref */
314 bcv && bcv != cv && !CvCLONE(bcv);
315 bcv = CvOUTSIDE(bcv))
318 /* install the missing pad entry in intervening
319 * nested subs and mark them cloneable.
320 * XXX fix pad_foo() to not use globals */
321 AV *ocomppad_name = PL_comppad_name;
322 AV *ocomppad = PL_comppad;
323 SV **ocurpad = PL_curpad;
324 AV *padlist = CvPADLIST(bcv);
325 PL_comppad_name = (AV*)AvARRAY(padlist)[0];
326 PL_comppad = (AV*)AvARRAY(padlist)[1];
327 PL_curpad = AvARRAY(PL_comppad);
329 PL_comppad_name = ocomppad_name;
330 PL_comppad = ocomppad;
335 if (ckWARN(WARN_CLOSURE)
336 && !CvUNIQUE(bcv) && !CvUNIQUE(cv))
338 Perl_warner(aTHX_ WARN_CLOSURE,
339 "Variable \"%s\" may be unavailable",
347 else if (!CvUNIQUE(PL_compcv)) {
348 if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv)
349 && !(SvFLAGS(sv) & SVpad_OUR))
351 Perl_warner(aTHX_ WARN_CLOSURE,
352 "Variable \"%s\" will not stay shared", name);
356 av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv));
362 if (flags & FINDLEX_NOSEARCH)
365 /* Nothing in current lexical context--try eval's context, if any.
366 * This is necessary to let the perldb get at lexically scoped variables.
367 * XXX This will also probably interact badly with eval tree caching.
370 for (i = cx_ix; i >= 0; i--) {
372 switch (CxTYPE(cx)) {
374 if (i == 0 && saweval) {
375 seq = cxstack[saweval].blk_oldcop->cop_seq;
376 return pad_findlex(name, newoff, seq, PL_main_cv, -1, saweval, 0);
380 switch (cx->blk_eval.old_op_type) {
387 /* require/do must have their own scope */
396 if (PL_debstash && CvSTASH(cv) == PL_debstash) { /* ignore DB'* scope */
397 saweval = i; /* so we know where we were called from */
400 seq = cxstack[saweval].blk_oldcop->cop_seq;
401 return pad_findlex(name, newoff, seq, cv, i-1, saweval,FINDLEX_NOSEARCH);
409 Perl_pad_findmy(pTHX_ char *name)
414 SV **svp = AvARRAY(PL_comppad_name);
415 U32 seq = PL_cop_seqmax;
421 * Special case to get lexical (and hence per-thread) @_.
422 * XXX I need to find out how to tell at parse-time whether use
423 * of @_ should refer to a lexical (from a sub) or defgv (global
424 * scope and maybe weird sub-ish things like formats). See
425 * startsub in perly.y. It's possible that @_ could be lexical
426 * (at least from subs) even in non-threaded perl.
428 if (strEQ(name, "@_"))
429 return 0; /* success. (NOT_IN_PAD indicates failure) */
430 #endif /* USE_THREADS */
432 /* The one we're looking for is probably just before comppad_name_fill. */
433 for (off = AvFILLp(PL_comppad_name); off > 0; off--) {
434 if ((sv = svp[off]) &&
435 sv != &PL_sv_undef &&
438 seq > I_32(SvNVX(sv)))) &&
439 strEQ(SvPVX(sv), name))
441 if (SvIVX(sv) || SvFLAGS(sv) & SVpad_OUR)
442 return (PADOFFSET)off;
443 pendoff = off; /* this pending def. will override import */
447 outside = CvOUTSIDE(PL_compcv);
449 /* Check if if we're compiling an eval'', and adjust seq to be the
450 * eval's seq number. This depends on eval'' having a non-null
451 * CvOUTSIDE() while it is being compiled. The eval'' itself is
452 * identified by CvEVAL being true and CvGV being null. */
453 if (outside && CvEVAL(PL_compcv) && !CvGV(PL_compcv) && cxstack_ix >= 0) {
454 cx = &cxstack[cxstack_ix];
456 seq = cx->blk_oldcop->cop_seq;
459 /* See if it's in a nested scope */
460 off = pad_findlex(name, 0, seq, outside, cxstack_ix, 0, 0);
462 /* If there is a pending local definition, this new alias must die */
464 SvIVX(AvARRAY(PL_comppad_name)[off]) = seq;
465 return off; /* pad_findlex returns 0 for failure...*/
467 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
471 Perl_pad_leavemy(pTHX_ I32 fill)
474 SV **svp = AvARRAY(PL_comppad_name);
476 if (PL_min_intro_pending && fill < PL_min_intro_pending) {
477 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
478 if ((sv = svp[off]) && sv != &PL_sv_undef && ckWARN_d(WARN_INTERNAL))
479 Perl_warner(aTHX_ WARN_INTERNAL, "%s never introduced", SvPVX(sv));
482 /* "Deintroduce" my variables that are leaving with this scope. */
483 for (off = AvFILLp(PL_comppad_name); off > fill; off--) {
484 if ((sv = svp[off]) && sv != &PL_sv_undef && SvIVX(sv) == PAD_MAX)
485 SvIVX(sv) = PL_cop_seqmax;
490 Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype)
495 if (AvARRAY(PL_comppad) != PL_curpad)
496 Perl_croak(aTHX_ "panic: pad_alloc");
497 if (PL_pad_reset_pending)
499 if (tmptype & SVs_PADMY) {
501 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
502 } while (SvPADBUSY(sv)); /* need a fresh one */
503 retval = AvFILLp(PL_comppad);
506 SV **names = AvARRAY(PL_comppad_name);
507 SSize_t names_fill = AvFILLp(PL_comppad_name);
510 * "foreach" index vars temporarily become aliases to non-"my"
511 * values. Thus we must skip, not just pad values that are
512 * marked as current pad values, but also those with names.
514 if (++PL_padix <= names_fill &&
515 (sv = names[PL_padix]) && sv != &PL_sv_undef)
517 sv = *av_fetch(PL_comppad, PL_padix, TRUE);
518 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)) &&
519 !IS_PADGV(sv) && !IS_PADCONST(sv))
524 SvFLAGS(sv) |= tmptype;
525 PL_curpad = AvARRAY(PL_comppad);
527 DEBUG_X(PerlIO_printf(Perl_debug_log,
528 "0x%"UVxf" Pad 0x%"UVxf" alloc %ld for %s\n",
529 PTR2UV(thr), PTR2UV(PL_curpad),
530 (long) retval, PL_op_name[optype]));
532 DEBUG_X(PerlIO_printf(Perl_debug_log,
533 "Pad 0x%"UVxf" alloc %ld for %s\n",
535 (long) retval, PL_op_name[optype]));
536 #endif /* USE_THREADS */
537 return (PADOFFSET)retval;
541 Perl_pad_sv(pTHX_ PADOFFSET po)
544 DEBUG_X(PerlIO_printf(Perl_debug_log,
545 "0x%"UVxf" Pad 0x%"UVxf" sv %"IVdf"\n",
546 PTR2UV(thr), PTR2UV(PL_curpad), (IV)po));
549 Perl_croak(aTHX_ "panic: pad_sv po");
550 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" sv %"IVdf"\n",
551 PTR2UV(PL_curpad), (IV)po));
552 #endif /* USE_THREADS */
553 return PL_curpad[po]; /* eventually we'll turn this into a macro */
557 Perl_pad_free(pTHX_ PADOFFSET po)
561 if (AvARRAY(PL_comppad) != PL_curpad)
562 Perl_croak(aTHX_ "panic: pad_free curpad");
564 Perl_croak(aTHX_ "panic: pad_free po");
566 DEBUG_X(PerlIO_printf(Perl_debug_log,
567 "0x%"UVxf" Pad 0x%"UVxf" free %"IVdf"\n",
568 PTR2UV(thr), PTR2UV(PL_curpad), (IV)po));
570 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" free %"IVdf"\n",
571 PTR2UV(PL_curpad), (IV)po));
572 #endif /* USE_THREADS */
573 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef) {
574 SvPADTMP_off(PL_curpad[po]);
576 SvREADONLY_off(PL_curpad[po]); /* could be a freed constant */
579 if ((I32)po < PL_padix)
584 Perl_pad_swipe(pTHX_ PADOFFSET po)
586 if (AvARRAY(PL_comppad) != PL_curpad)
587 Perl_croak(aTHX_ "panic: pad_swipe curpad");
589 Perl_croak(aTHX_ "panic: pad_swipe po");
591 DEBUG_X(PerlIO_printf(Perl_debug_log,
592 "0x%"UVxf" Pad 0x%"UVxf" swipe %"IVdf"\n",
593 PTR2UV(thr), PTR2UV(PL_curpad), (IV)po));
595 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" swipe %"IVdf"\n",
596 PTR2UV(PL_curpad), (IV)po));
597 #endif /* USE_THREADS */
598 SvPADTMP_off(PL_curpad[po]);
599 PL_curpad[po] = NEWSV(1107,0);
600 SvPADTMP_on(PL_curpad[po]);
601 if ((I32)po < PL_padix)
605 /* XXX pad_reset() is currently disabled because it results in serious bugs.
606 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
607 * on the stack by OPs that use them, there are several ways to get an alias
608 * to a shared TARG. Such an alias will change randomly and unpredictably.
609 * We avoid doing this until we can think of a Better Way.
614 #ifdef USE_BROKEN_PAD_RESET
617 if (AvARRAY(PL_comppad) != PL_curpad)
618 Perl_croak(aTHX_ "panic: pad_reset curpad");
620 DEBUG_X(PerlIO_printf(Perl_debug_log,
621 "0x%"UVxf" Pad 0x%"UVxf" reset\n",
622 PTR2UV(thr), PTR2UV(PL_curpad)));
624 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" reset\n",
626 #endif /* USE_THREADS */
627 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
628 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
629 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
630 SvPADTMP_off(PL_curpad[po]);
632 PL_padix = PL_padix_floor;
635 PL_pad_reset_pending = FALSE;
639 /* find_threadsv is not reentrant */
641 Perl_find_threadsv(pTHX_ const char *name)
646 /* We currently only handle names of a single character */
647 p = strchr(PL_threadsv_names, *name);
650 key = p - PL_threadsv_names;
651 MUTEX_LOCK(&thr->mutex);
652 svp = av_fetch(thr->threadsv, key, FALSE);
654 MUTEX_UNLOCK(&thr->mutex);
656 SV *sv = NEWSV(0, 0);
657 av_store(thr->threadsv, key, sv);
658 thr->threadsvp = AvARRAY(thr->threadsv);
659 MUTEX_UNLOCK(&thr->mutex);
661 * Some magic variables used to be automagically initialised
662 * in gv_fetchpv. Those which are now per-thread magicals get
663 * initialised here instead.
669 sv_setpv(sv, "\034");
670 sv_magic(sv, 0, 0, name, 1);
675 PL_sawampersand = TRUE;
689 /* XXX %! tied to Errno.pm needs to be added here.
690 * See gv_fetchpv(). */
694 sv_magic(sv, 0, 0, name, 1);
696 DEBUG_S(PerlIO_printf(Perl_error_log,
697 "find_threadsv: new SV %p for $%s%c\n",
698 sv, (*name < 32) ? "^" : "",
699 (*name < 32) ? toCTRL(*name) : *name));
703 #endif /* USE_THREADS */
708 Perl_op_free(pTHX_ OP *o)
710 register OP *kid, *nextkid;
713 if (!o || o->op_seq == (U16)-1)
716 if (o->op_private & OPpREFCOUNTED) {
717 switch (o->op_type) {
725 if (OpREFCNT_dec(o)) {
736 if (o->op_flags & OPf_KIDS) {
737 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
738 nextkid = kid->op_sibling; /* Get before next freeing kid */
746 /* COP* is not cleared by op_clear() so that we may track line
747 * numbers etc even after null() */
748 if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
753 #ifdef PL_OP_SLAB_ALLOC
754 if ((char *) o == PL_OpPtr)
763 S_op_clear(pTHX_ OP *o)
765 switch (o->op_type) {
766 case OP_NULL: /* Was holding old type, if any. */
767 case OP_ENTEREVAL: /* Was holding hints. */
769 case OP_THREADSV: /* Was holding index into thr->threadsv AV. */
775 if (!(o->op_flags & OPf_SPECIAL))
778 #endif /* USE_THREADS */
780 if (!(o->op_flags & OPf_REF)
781 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
788 if (cPADOPo->op_padix > 0) {
791 pad_swipe(cPADOPo->op_padix);
792 /* No GvIN_PAD_off(gv) here, because other references may still
793 * exist on the pad */
796 cPADOPo->op_padix = 0;
799 SvREFCNT_dec(cSVOPo->op_sv);
800 cSVOPo->op_sv = Nullsv;
803 case OP_METHOD_NAMED:
805 SvREFCNT_dec(cSVOPo->op_sv);
806 cSVOPo->op_sv = Nullsv;
812 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
816 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
817 SvREFCNT_dec(cSVOPo->op_sv);
818 cSVOPo->op_sv = Nullsv;
821 Safefree(cPVOPo->op_pv);
822 cPVOPo->op_pv = Nullch;
826 op_free(cPMOPo->op_pmreplroot);
830 if ((PADOFFSET)cPMOPo->op_pmreplroot) {
832 GV *gv = (GV*)PL_curpad[(PADOFFSET)cPMOPo->op_pmreplroot];
833 pad_swipe((PADOFFSET)cPMOPo->op_pmreplroot);
834 /* No GvIN_PAD_off(gv) here, because other references may still
835 * exist on the pad */
840 SvREFCNT_dec((SV*)cPMOPo->op_pmreplroot);
846 cPMOPo->op_pmreplroot = Nullop;
847 ReREFCNT_dec(cPMOPo->op_pmregexp);
848 cPMOPo->op_pmregexp = (REGEXP*)NULL;
852 if (o->op_targ > 0) {
853 pad_free(o->op_targ);
859 S_cop_free(pTHX_ COP* cop)
861 Safefree(cop->cop_label);
863 Safefree(CopFILE(cop)); /* XXX share in a pvtable? */
864 Safefree(CopSTASHPV(cop)); /* XXX share in a pvtable? */
866 /* NOTE: COP.cop_stash is not refcounted */
867 SvREFCNT_dec(CopFILEGV(cop));
869 if (! specialWARN(cop->cop_warnings))
870 SvREFCNT_dec(cop->cop_warnings);
871 if (! specialCopIO(cop->cop_io))
872 SvREFCNT_dec(cop->cop_io);
878 if (o->op_type == OP_NULL)
881 o->op_targ = o->op_type;
882 o->op_type = OP_NULL;
883 o->op_ppaddr = PL_ppaddr[OP_NULL];
886 /* Contextualizers */
888 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
891 Perl_linklist(pTHX_ OP *o)
898 /* establish postfix order */
899 if (cUNOPo->op_first) {
900 o->op_next = LINKLIST(cUNOPo->op_first);
901 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
903 kid->op_next = LINKLIST(kid->op_sibling);
915 Perl_scalarkids(pTHX_ OP *o)
918 if (o && o->op_flags & OPf_KIDS) {
919 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
926 S_scalarboolean(pTHX_ OP *o)
928 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
929 if (ckWARN(WARN_SYNTAX)) {
930 line_t oldline = CopLINE(PL_curcop);
932 if (PL_copline != NOLINE)
933 CopLINE_set(PL_curcop, PL_copline);
934 Perl_warner(aTHX_ WARN_SYNTAX, "Found = in conditional, should be ==");
935 CopLINE_set(PL_curcop, oldline);
942 Perl_scalar(pTHX_ OP *o)
946 /* assumes no premature commitment */
947 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
948 || o->op_type == OP_RETURN)
953 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
955 switch (o->op_type) {
957 if (o->op_private & OPpREPEAT_DOLIST)
958 null(((LISTOP*)cBINOPo->op_first)->op_first);
959 scalar(cBINOPo->op_first);
964 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
968 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
969 if (!kPMOP->op_pmreplroot)
970 deprecate("implicit split to @_");
978 if (o->op_flags & OPf_KIDS) {
979 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
985 kid = cLISTOPo->op_first;
987 while ((kid = kid->op_sibling)) {
993 WITH_THR(PL_curcop = &PL_compiling);
998 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1004 WITH_THR(PL_curcop = &PL_compiling);
1011 Perl_scalarvoid(pTHX_ OP *o)
1018 if (o->op_type == OP_NEXTSTATE
1019 || o->op_type == OP_SETSTATE
1020 || o->op_type == OP_DBSTATE
1021 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1022 || o->op_targ == OP_SETSTATE
1023 || o->op_targ == OP_DBSTATE)))
1024 PL_curcop = (COP*)o; /* for warning below */
1026 /* assumes no premature commitment */
1027 want = o->op_flags & OPf_WANT;
1028 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
1029 || o->op_type == OP_RETURN)
1034 if ((o->op_private & OPpTARGET_MY)
1035 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1037 return scalar(o); /* As if inside SASSIGN */
1040 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1042 switch (o->op_type) {
1044 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1048 if (o->op_flags & OPf_STACKED)
1052 if (o->op_private == 4)
1094 case OP_GETSOCKNAME:
1095 case OP_GETPEERNAME:
1100 case OP_GETPRIORITY:
1123 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1124 useless = PL_op_desc[o->op_type];
1131 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1132 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1133 useless = "a variable";
1138 if (cSVOPo->op_private & OPpCONST_STRICT)
1139 no_bareword_allowed(o);
1141 if (ckWARN(WARN_VOID)) {
1142 useless = "a constant";
1143 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1145 else if (SvPOK(sv)) {
1146 /* perl4's way of mixing documentation and code
1147 (before the invention of POD) was based on a
1148 trick to mix nroff and perl code. The trick was
1149 built upon these three nroff macros being used in
1150 void context. The pink camel has the details in
1151 the script wrapman near page 319. */
1152 if (strnEQ(SvPVX(sv), "di", 2) ||
1153 strnEQ(SvPVX(sv), "ds", 2) ||
1154 strnEQ(SvPVX(sv), "ig", 2))
1159 null(o); /* don't execute or even remember it */
1163 o->op_type = OP_PREINC; /* pre-increment is faster */
1164 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1168 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1169 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1175 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1180 if (o->op_flags & OPf_STACKED)
1187 if (!(o->op_flags & OPf_KIDS))
1196 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1203 /* all requires must return a boolean value */
1204 o->op_flags &= ~OPf_WANT;
1209 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
1210 if (!kPMOP->op_pmreplroot)
1211 deprecate("implicit split to @_");
1215 if (useless && ckWARN(WARN_VOID))
1216 Perl_warner(aTHX_ WARN_VOID, "Useless use of %s in void context", useless);
1221 Perl_listkids(pTHX_ OP *o)
1224 if (o && o->op_flags & OPf_KIDS) {
1225 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1232 Perl_list(pTHX_ OP *o)
1236 /* assumes no premature commitment */
1237 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
1238 || o->op_type == OP_RETURN)
1243 if ((o->op_private & OPpTARGET_MY)
1244 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1246 return o; /* As if inside SASSIGN */
1249 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1251 switch (o->op_type) {
1254 list(cBINOPo->op_first);
1259 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1267 if (!(o->op_flags & OPf_KIDS))
1269 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1270 list(cBINOPo->op_first);
1271 return gen_constant_list(o);
1278 kid = cLISTOPo->op_first;
1280 while ((kid = kid->op_sibling)) {
1281 if (kid->op_sibling)
1286 WITH_THR(PL_curcop = &PL_compiling);
1290 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1291 if (kid->op_sibling)
1296 WITH_THR(PL_curcop = &PL_compiling);
1299 /* all requires must return a boolean value */
1300 o->op_flags &= ~OPf_WANT;
1307 Perl_scalarseq(pTHX_ OP *o)
1312 if (o->op_type == OP_LINESEQ ||
1313 o->op_type == OP_SCOPE ||
1314 o->op_type == OP_LEAVE ||
1315 o->op_type == OP_LEAVETRY)
1317 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1318 if (kid->op_sibling) {
1322 PL_curcop = &PL_compiling;
1324 o->op_flags &= ~OPf_PARENS;
1325 if (PL_hints & HINT_BLOCK_SCOPE)
1326 o->op_flags |= OPf_PARENS;
1329 o = newOP(OP_STUB, 0);
1334 S_modkids(pTHX_ OP *o, I32 type)
1337 if (o && o->op_flags & OPf_KIDS) {
1338 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1345 Perl_mod(pTHX_ OP *o, I32 type)
1350 if (!o || PL_error_count)
1353 if ((o->op_private & OPpTARGET_MY)
1354 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1359 switch (o->op_type) {
1364 if (o->op_private & (OPpCONST_BARE) &&
1365 !(type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)) {
1366 SV *sv = ((SVOP*)o)->op_sv;
1369 /* Could be a filehandle */
1370 if (gv = gv_fetchpv(SvPV_nolen(sv), FALSE, SVt_PVIO)) {
1371 OP* gvio = newUNOP(OP_RV2GV, 0, newGVOP(OP_GV, 0, gv));
1375 /* OK, it's a sub */
1377 gv = gv_fetchpv(SvPV_nolen(sv), TRUE, SVt_PVCV);
1379 enter = newUNOP(OP_ENTERSUB,0,
1380 newUNOP(OP_RV2CV, 0,
1381 newGVOP(OP_GV, 0, gv)
1383 enter->op_private |= OPpLVAL_INTRO;
1389 if (!(o->op_private & (OPpCONST_ARYBASE)))
1391 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1392 PL_compiling.cop_arybase = (I32)SvIV(cSVOPx(PL_eval_start)->op_sv);
1396 SAVEI32(PL_compiling.cop_arybase);
1397 PL_compiling.cop_arybase = 0;
1399 else if (type == OP_REFGEN)
1402 Perl_croak(aTHX_ "That use of $[ is unsupported");
1405 if (o->op_flags & OPf_PARENS)
1409 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1410 !(o->op_flags & OPf_STACKED)) {
1411 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1412 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1413 assert(cUNOPo->op_first->op_type == OP_NULL);
1414 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1417 else { /* lvalue subroutine call */
1418 o->op_private |= OPpLVAL_INTRO;
1419 PL_modcount = RETURN_UNLIMITED_NUMBER;
1420 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
1421 /* Backward compatibility mode: */
1422 o->op_private |= OPpENTERSUB_INARGS;
1425 else { /* Compile-time error message: */
1426 OP *kid = cUNOPo->op_first;
1430 if (kid->op_type == OP_PUSHMARK)
1432 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1434 "panic: unexpected lvalue entersub "
1435 "args: type/targ %ld:%ld",
1436 (long)kid->op_type,kid->op_targ);
1437 kid = kLISTOP->op_first;
1439 while (kid->op_sibling)
1440 kid = kid->op_sibling;
1441 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1443 if (kid->op_type == OP_METHOD_NAMED
1444 || kid->op_type == OP_METHOD)
1448 if (kid->op_sibling || kid->op_next != kid) {
1449 yyerror("panic: unexpected optree near method call");
1453 NewOp(1101, newop, 1, UNOP);
1454 newop->op_type = OP_RV2CV;
1455 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1456 newop->op_first = Nullop;
1457 newop->op_next = (OP*)newop;
1458 kid->op_sibling = (OP*)newop;
1459 newop->op_private |= OPpLVAL_INTRO;
1463 if (kid->op_type != OP_RV2CV)
1465 "panic: unexpected lvalue entersub "
1466 "entry via type/targ %ld:%ld",
1467 (long)kid->op_type,kid->op_targ);
1468 kid->op_private |= OPpLVAL_INTRO;
1469 break; /* Postpone until runtime */
1473 kid = kUNOP->op_first;
1474 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1475 kid = kUNOP->op_first;
1476 if (kid->op_type == OP_NULL)
1478 "Unexpected constant lvalue entersub "
1479 "entry via type/targ %ld:%ld",
1480 (long)kid->op_type,kid->op_targ);
1481 if (kid->op_type != OP_GV) {
1482 /* Restore RV2CV to check lvalueness */
1484 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1485 okid->op_next = kid->op_next;
1486 kid->op_next = okid;
1489 okid->op_next = Nullop;
1490 okid->op_type = OP_RV2CV;
1492 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1493 okid->op_private |= OPpLVAL_INTRO;
1497 cv = GvCV(kGVOP_gv);
1507 /* grep, foreach, subcalls, refgen */
1508 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1510 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1511 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1513 : (o->op_type == OP_ENTERSUB
1514 ? "non-lvalue subroutine call"
1515 : PL_op_desc[o->op_type])),
1516 type ? PL_op_desc[type] : "local"));
1530 case OP_RIGHT_SHIFT:
1539 if (!(o->op_flags & OPf_STACKED))
1545 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1551 if (!type && cUNOPo->op_first->op_type != OP_GV)
1552 Perl_croak(aTHX_ "Can't localize through a reference");
1553 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1554 PL_modcount = RETURN_UNLIMITED_NUMBER;
1555 return o; /* Treat \(@foo) like ordinary list. */
1559 if (scalar_mod_type(o, type))
1561 ref(cUNOPo->op_first, o->op_type);
1565 if (type == OP_LEAVESUBLV)
1566 o->op_private |= OPpMAYBE_LVSUB;
1572 PL_modcount = RETURN_UNLIMITED_NUMBER;
1575 if (!type && cUNOPo->op_first->op_type != OP_GV)
1576 Perl_croak(aTHX_ "Can't localize through a reference");
1577 ref(cUNOPo->op_first, o->op_type);
1581 PL_hints |= HINT_BLOCK_SCOPE;
1591 PL_modcount = RETURN_UNLIMITED_NUMBER;
1592 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1593 return o; /* Treat \(@foo) like ordinary list. */
1594 if (scalar_mod_type(o, type))
1596 if (type == OP_LEAVESUBLV)
1597 o->op_private |= OPpMAYBE_LVSUB;
1602 Perl_croak(aTHX_ "Can't localize lexical variable %s",
1603 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
1608 PL_modcount++; /* XXX ??? */
1610 #endif /* USE_THREADS */
1616 if (type != OP_SASSIGN)
1620 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1625 if (type == OP_LEAVESUBLV)
1626 o->op_private |= OPpMAYBE_LVSUB;
1628 pad_free(o->op_targ);
1629 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1630 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1631 if (o->op_flags & OPf_KIDS)
1632 mod(cBINOPo->op_first->op_sibling, type);
1637 ref(cBINOPo->op_first, o->op_type);
1638 if (type == OP_ENTERSUB &&
1639 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1640 o->op_private |= OPpLVAL_DEFER;
1641 if (type == OP_LEAVESUBLV)
1642 o->op_private |= OPpMAYBE_LVSUB;
1650 if (o->op_flags & OPf_KIDS)
1651 mod(cLISTOPo->op_last, type);
1655 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1657 else if (!(o->op_flags & OPf_KIDS))
1659 if (o->op_targ != OP_LIST) {
1660 mod(cBINOPo->op_first, type);
1665 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1670 if (type != OP_LEAVESUBLV)
1672 break; /* mod()ing was handled by ck_return() */
1674 if (type != OP_LEAVESUBLV)
1675 o->op_flags |= OPf_MOD;
1677 if (type == OP_AASSIGN || type == OP_SASSIGN)
1678 o->op_flags |= OPf_SPECIAL|OPf_REF;
1680 o->op_private |= OPpLVAL_INTRO;
1681 o->op_flags &= ~OPf_SPECIAL;
1682 PL_hints |= HINT_BLOCK_SCOPE;
1684 else if (type != OP_GREPSTART && type != OP_ENTERSUB
1685 && type != OP_LEAVESUBLV)
1686 o->op_flags |= OPf_REF;
1691 S_scalar_mod_type(pTHX_ OP *o, I32 type)
1695 if (o->op_type == OP_RV2GV)
1719 case OP_RIGHT_SHIFT:
1738 S_is_handle_constructor(pTHX_ OP *o, I32 argnum)
1740 switch (o->op_type) {
1748 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1761 Perl_refkids(pTHX_ OP *o, I32 type)
1764 if (o && o->op_flags & OPf_KIDS) {
1765 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1772 Perl_ref(pTHX_ OP *o, I32 type)
1776 if (!o || PL_error_count)
1779 switch (o->op_type) {
1781 if ((type == OP_EXISTS || type == OP_DEFINED || type == OP_LOCK) &&
1782 !(o->op_flags & OPf_STACKED)) {
1783 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1784 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1785 assert(cUNOPo->op_first->op_type == OP_NULL);
1786 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1787 o->op_flags |= OPf_SPECIAL;
1792 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1796 if (type == OP_DEFINED)
1797 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1798 ref(cUNOPo->op_first, o->op_type);
1801 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1802 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1803 : type == OP_RV2HV ? OPpDEREF_HV
1805 o->op_flags |= OPf_MOD;
1810 o->op_flags |= OPf_MOD; /* XXX ??? */
1815 o->op_flags |= OPf_REF;
1818 if (type == OP_DEFINED)
1819 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1820 ref(cUNOPo->op_first, o->op_type);
1825 o->op_flags |= OPf_REF;
1830 if (!(o->op_flags & OPf_KIDS))
1832 ref(cBINOPo->op_first, type);
1836 ref(cBINOPo->op_first, o->op_type);
1837 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1838 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1839 : type == OP_RV2HV ? OPpDEREF_HV
1841 o->op_flags |= OPf_MOD;
1849 if (!(o->op_flags & OPf_KIDS))
1851 ref(cLISTOPo->op_last, type);
1861 S_dup_attrlist(pTHX_ OP *o)
1865 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1866 * where the first kid is OP_PUSHMARK and the remaining ones
1867 * are OP_CONST. We need to push the OP_CONST values.
1869 if (o->op_type == OP_CONST)
1870 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc(cSVOPo->op_sv));
1872 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1873 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1874 if (o->op_type == OP_CONST)
1875 rop = append_elem(OP_LIST, rop,
1876 newSVOP(OP_CONST, o->op_flags,
1877 SvREFCNT_inc(cSVOPo->op_sv)));
1884 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
1888 /* fake up C<use attributes $pkg,$rv,@attrs> */
1889 ENTER; /* need to protect against side-effects of 'use' */
1891 if (stash && HvNAME(stash))
1892 stashsv = newSVpv(HvNAME(stash), 0);
1894 stashsv = &PL_sv_no;
1896 #define ATTRSMODULE "attributes"
1898 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1899 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1901 prepend_elem(OP_LIST,
1902 newSVOP(OP_CONST, 0, stashsv),
1903 prepend_elem(OP_LIST,
1904 newSVOP(OP_CONST, 0,
1906 dup_attrlist(attrs))));
1911 Perl_apply_attrs_string(pTHX_ char *stashpv, CV *cv,
1912 char *attrstr, STRLEN len)
1917 len = strlen(attrstr);
1921 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
1923 char *sstr = attrstr;
1924 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
1925 attrs = append_elem(OP_LIST, attrs,
1926 newSVOP(OP_CONST, 0,
1927 newSVpvn(sstr, attrstr-sstr)));
1931 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1932 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1933 Nullsv, prepend_elem(OP_LIST,
1934 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
1935 prepend_elem(OP_LIST,
1936 newSVOP(OP_CONST, 0,
1942 S_my_kid(pTHX_ OP *o, OP *attrs)
1947 if (!o || PL_error_count)
1951 if (type == OP_LIST) {
1952 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1954 } else if (type == OP_UNDEF) {
1956 } else if (type == OP_RV2SV || /* "our" declaration */
1958 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
1959 o->op_private |= OPpOUR_INTRO;
1961 } else if (type != OP_PADSV &&
1964 type != OP_PUSHMARK)
1966 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
1967 PL_op_desc[o->op_type],
1968 PL_in_my == KEY_our ? "our" : "my"));
1971 else if (attrs && type != OP_PUSHMARK) {
1977 PL_in_my_stash = Nullhv;
1979 /* check for C<my Dog $spot> when deciding package */
1980 namesvp = av_fetch(PL_comppad_name, o->op_targ, FALSE);
1981 if (namesvp && *namesvp && SvOBJECT(*namesvp) && HvNAME(SvSTASH(*namesvp)))
1982 stash = SvSTASH(*namesvp);
1984 stash = PL_curstash;
1985 padsv = PAD_SV(o->op_targ);
1986 apply_attrs(stash, padsv, attrs);
1988 o->op_flags |= OPf_MOD;
1989 o->op_private |= OPpLVAL_INTRO;
1994 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1996 if (o->op_flags & OPf_PARENS)
2000 o = my_kid(o, attrs);
2002 PL_in_my_stash = Nullhv;
2007 Perl_my(pTHX_ OP *o)
2009 return my_kid(o, Nullop);
2013 Perl_sawparens(pTHX_ OP *o)
2016 o->op_flags |= OPf_PARENS;
2021 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
2025 if (ckWARN(WARN_MISC) &&
2026 (left->op_type == OP_RV2AV ||
2027 left->op_type == OP_RV2HV ||
2028 left->op_type == OP_PADAV ||
2029 left->op_type == OP_PADHV)) {
2030 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
2031 right->op_type == OP_TRANS)
2032 ? right->op_type : OP_MATCH];
2033 const char *sample = ((left->op_type == OP_RV2AV ||
2034 left->op_type == OP_PADAV)
2035 ? "@array" : "%hash");
2036 Perl_warner(aTHX_ WARN_MISC,
2037 "Applying %s to %s will act on scalar(%s)",
2038 desc, sample, sample);
2041 if (!(right->op_flags & OPf_STACKED) &&
2042 (right->op_type == OP_MATCH ||
2043 right->op_type == OP_SUBST ||
2044 right->op_type == OP_TRANS)) {
2045 right->op_flags |= OPf_STACKED;
2046 if (right->op_type != OP_MATCH &&
2047 ! (right->op_type == OP_TRANS &&
2048 right->op_private & OPpTRANS_IDENTICAL))
2049 left = mod(left, right->op_type);
2050 if (right->op_type == OP_TRANS)
2051 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
2053 o = prepend_elem(right->op_type, scalar(left), right);
2055 return newUNOP(OP_NOT, 0, scalar(o));
2059 return bind_match(type, left,
2060 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
2064 Perl_invert(pTHX_ OP *o)
2068 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
2069 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
2073 Perl_scope(pTHX_ OP *o)
2076 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
2077 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
2078 o->op_type = OP_LEAVE;
2079 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
2082 if (o->op_type == OP_LINESEQ) {
2084 o->op_type = OP_SCOPE;
2085 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
2086 kid = ((LISTOP*)o)->op_first;
2087 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
2091 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
2098 Perl_save_hints(pTHX)
2101 SAVESPTR(GvHV(PL_hintgv));
2102 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
2103 SAVEFREESV(GvHV(PL_hintgv));
2107 Perl_block_start(pTHX_ int full)
2109 int retval = PL_savestack_ix;
2111 SAVEI32(PL_comppad_name_floor);
2112 PL_comppad_name_floor = AvFILLp(PL_comppad_name);
2114 PL_comppad_name_fill = PL_comppad_name_floor;
2115 if (PL_comppad_name_floor < 0)
2116 PL_comppad_name_floor = 0;
2117 SAVEI32(PL_min_intro_pending);
2118 SAVEI32(PL_max_intro_pending);
2119 PL_min_intro_pending = 0;
2120 SAVEI32(PL_comppad_name_fill);
2121 SAVEI32(PL_padix_floor);
2122 PL_padix_floor = PL_padix;
2123 PL_pad_reset_pending = FALSE;
2125 PL_hints &= ~HINT_BLOCK_SCOPE;
2126 SAVESPTR(PL_compiling.cop_warnings);
2127 if (! specialWARN(PL_compiling.cop_warnings)) {
2128 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
2129 SAVEFREESV(PL_compiling.cop_warnings) ;
2131 SAVESPTR(PL_compiling.cop_io);
2132 if (! specialCopIO(PL_compiling.cop_io)) {
2133 PL_compiling.cop_io = newSVsv(PL_compiling.cop_io) ;
2134 SAVEFREESV(PL_compiling.cop_io) ;
2140 Perl_block_end(pTHX_ I32 floor, OP *seq)
2142 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
2143 OP* retval = scalarseq(seq);
2145 PL_pad_reset_pending = FALSE;
2146 PL_compiling.op_private = PL_hints;
2148 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
2149 pad_leavemy(PL_comppad_name_fill);
2158 OP *o = newOP(OP_THREADSV, 0);
2159 o->op_targ = find_threadsv("_");
2162 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
2163 #endif /* USE_THREADS */
2167 Perl_newPROG(pTHX_ OP *o)
2172 PL_eval_root = newUNOP(OP_LEAVEEVAL,
2173 ((PL_in_eval & EVAL_KEEPERR)
2174 ? OPf_SPECIAL : 0), o);
2175 PL_eval_start = linklist(PL_eval_root);
2176 PL_eval_root->op_private |= OPpREFCOUNTED;
2177 OpREFCNT_set(PL_eval_root, 1);
2178 PL_eval_root->op_next = 0;
2179 peep(PL_eval_start);
2184 PL_main_root = scope(sawparens(scalarvoid(o)));
2185 PL_curcop = &PL_compiling;
2186 PL_main_start = LINKLIST(PL_main_root);
2187 PL_main_root->op_private |= OPpREFCOUNTED;
2188 OpREFCNT_set(PL_main_root, 1);
2189 PL_main_root->op_next = 0;
2190 peep(PL_main_start);
2193 /* Register with debugger */
2195 CV *cv = get_cv("DB::postponed", FALSE);
2199 XPUSHs((SV*)CopFILEGV(&PL_compiling));
2201 call_sv((SV*)cv, G_DISCARD);
2208 Perl_localize(pTHX_ OP *o, I32 lex)
2210 if (o->op_flags & OPf_PARENS)
2213 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
2215 for (s = PL_bufptr; *s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s) || strchr("@$%, ",*s)); s++) ;
2216 if (*s == ';' || *s == '=')
2217 Perl_warner(aTHX_ WARN_PARENTHESIS,
2218 "Parentheses missing around \"%s\" list",
2219 lex ? (PL_in_my == KEY_our ? "our" : "my") : "local");
2225 o = mod(o, OP_NULL); /* a bit kludgey */
2227 PL_in_my_stash = Nullhv;
2232 Perl_jmaybe(pTHX_ OP *o)
2234 if (o->op_type == OP_LIST) {
2237 o2 = newOP(OP_THREADSV, 0);
2238 o2->op_targ = find_threadsv(";");
2240 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
2241 #endif /* USE_THREADS */
2242 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2248 Perl_fold_constants(pTHX_ register OP *o)
2251 I32 type = o->op_type;
2254 if (PL_opargs[type] & OA_RETSCALAR)
2256 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2257 o->op_targ = pad_alloc(type, SVs_PADTMP);
2259 /* integerize op, unless it happens to be C<-foo>.
2260 * XXX should pp_i_negate() do magic string negation instead? */
2261 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2262 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2263 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2265 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2268 if (!(PL_opargs[type] & OA_FOLDCONST))
2273 /* XXX might want a ck_negate() for this */
2274 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2287 if (o->op_private & OPpLOCALE)
2292 goto nope; /* Don't try to run w/ errors */
2294 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2295 if ((curop->op_type != OP_CONST ||
2296 (curop->op_private & OPpCONST_BARE)) &&
2297 curop->op_type != OP_LIST &&
2298 curop->op_type != OP_SCALAR &&
2299 curop->op_type != OP_NULL &&
2300 curop->op_type != OP_PUSHMARK)
2306 curop = LINKLIST(o);
2310 sv = *(PL_stack_sp--);
2311 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
2312 pad_swipe(o->op_targ);
2313 else if (SvTEMP(sv)) { /* grab mortal temp? */
2314 (void)SvREFCNT_inc(sv);
2318 if (type == OP_RV2GV)
2319 return newGVOP(OP_GV, 0, (GV*)sv);
2321 /* try to smush double to int, but don't smush -2.0 to -2 */
2322 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
2325 #ifdef PERL_PRESERVE_IVUV
2326 /* Only bother to attempt to fold to IV if
2327 most operators will benefit */
2331 return newSVOP(OP_CONST, 0, sv);
2335 if (!(PL_opargs[type] & OA_OTHERINT))
2338 if (!(PL_hints & HINT_INTEGER)) {
2339 if (type == OP_MODULO
2340 || type == OP_DIVIDE
2341 || !(o->op_flags & OPf_KIDS))
2346 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
2347 if (curop->op_type == OP_CONST) {
2348 if (SvIOK(((SVOP*)curop)->op_sv))
2352 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
2356 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
2363 Perl_gen_constant_list(pTHX_ register OP *o)
2366 I32 oldtmps_floor = PL_tmps_floor;
2370 return o; /* Don't attempt to run with errors */
2372 PL_op = curop = LINKLIST(o);
2379 PL_tmps_floor = oldtmps_floor;
2381 o->op_type = OP_RV2AV;
2382 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2383 curop = ((UNOP*)o)->op_first;
2384 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
2391 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2396 if (!o || o->op_type != OP_LIST)
2397 o = newLISTOP(OP_LIST, 0, o, Nullop);
2399 o->op_flags &= ~OPf_WANT;
2401 if (!(PL_opargs[type] & OA_MARK))
2402 null(cLISTOPo->op_first);
2405 o->op_ppaddr = PL_ppaddr[type];
2406 o->op_flags |= flags;
2408 o = CHECKOP(type, o);
2409 if (o->op_type != type)
2412 return fold_constants(o);
2415 /* List constructors */
2418 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2426 if (first->op_type != type
2427 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
2429 return newLISTOP(type, 0, first, last);
2432 if (first->op_flags & OPf_KIDS)
2433 ((LISTOP*)first)->op_last->op_sibling = last;
2435 first->op_flags |= OPf_KIDS;
2436 ((LISTOP*)first)->op_first = last;
2438 ((LISTOP*)first)->op_last = last;
2443 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2451 if (first->op_type != type)
2452 return prepend_elem(type, (OP*)first, (OP*)last);
2454 if (last->op_type != type)
2455 return append_elem(type, (OP*)first, (OP*)last);
2457 first->op_last->op_sibling = last->op_first;
2458 first->op_last = last->op_last;
2459 first->op_flags |= (last->op_flags & OPf_KIDS);
2461 #ifdef PL_OP_SLAB_ALLOC
2469 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2477 if (last->op_type == type) {
2478 if (type == OP_LIST) { /* already a PUSHMARK there */
2479 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2480 ((LISTOP*)last)->op_first->op_sibling = first;
2481 if (!(first->op_flags & OPf_PARENS))
2482 last->op_flags &= ~OPf_PARENS;
2485 if (!(last->op_flags & OPf_KIDS)) {
2486 ((LISTOP*)last)->op_last = first;
2487 last->op_flags |= OPf_KIDS;
2489 first->op_sibling = ((LISTOP*)last)->op_first;
2490 ((LISTOP*)last)->op_first = first;
2492 last->op_flags |= OPf_KIDS;
2496 return newLISTOP(type, 0, first, last);
2502 Perl_newNULLLIST(pTHX)
2504 return newOP(OP_STUB, 0);
2508 Perl_force_list(pTHX_ OP *o)
2510 if (!o || o->op_type != OP_LIST)
2511 o = newLISTOP(OP_LIST, 0, o, Nullop);
2517 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2521 NewOp(1101, listop, 1, LISTOP);
2523 listop->op_type = type;
2524 listop->op_ppaddr = PL_ppaddr[type];
2527 listop->op_flags = flags;
2531 else if (!first && last)
2534 first->op_sibling = last;
2535 listop->op_first = first;
2536 listop->op_last = last;
2537 if (type == OP_LIST) {
2539 pushop = newOP(OP_PUSHMARK, 0);
2540 pushop->op_sibling = first;
2541 listop->op_first = pushop;
2542 listop->op_flags |= OPf_KIDS;
2544 listop->op_last = pushop;
2551 Perl_newOP(pTHX_ I32 type, I32 flags)
2554 NewOp(1101, o, 1, OP);
2556 o->op_ppaddr = PL_ppaddr[type];
2557 o->op_flags = flags;
2560 o->op_private = 0 + (flags >> 8);
2561 if (PL_opargs[type] & OA_RETSCALAR)
2563 if (PL_opargs[type] & OA_TARGET)
2564 o->op_targ = pad_alloc(type, SVs_PADTMP);
2565 return CHECKOP(type, o);
2569 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
2574 first = newOP(OP_STUB, 0);
2575 if (PL_opargs[type] & OA_MARK)
2576 first = force_list(first);
2578 NewOp(1101, unop, 1, UNOP);
2579 unop->op_type = type;
2580 unop->op_ppaddr = PL_ppaddr[type];
2581 unop->op_first = first;
2582 unop->op_flags = flags | OPf_KIDS;
2583 unop->op_private = 1 | (flags >> 8);
2584 unop = (UNOP*) CHECKOP(type, unop);
2588 return fold_constants((OP *) unop);
2592 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2595 NewOp(1101, binop, 1, BINOP);
2598 first = newOP(OP_NULL, 0);
2600 binop->op_type = type;
2601 binop->op_ppaddr = PL_ppaddr[type];
2602 binop->op_first = first;
2603 binop->op_flags = flags | OPf_KIDS;
2606 binop->op_private = 1 | (flags >> 8);
2609 binop->op_private = 2 | (flags >> 8);
2610 first->op_sibling = last;
2613 binop = (BINOP*)CHECKOP(type, binop);
2614 if (binop->op_next || binop->op_type != type)
2617 binop->op_last = binop->op_first->op_sibling;
2619 return fold_constants((OP *)binop);
2623 utf8compare(const void *a, const void *b)
2626 for (i = 0; i < 10; i++) {
2627 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2629 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2636 Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
2638 SV *tstr = ((SVOP*)expr)->op_sv;
2639 SV *rstr = ((SVOP*)repl)->op_sv;
2642 U8 *t = (U8*)SvPV(tstr, tlen);
2643 U8 *r = (U8*)SvPV(rstr, rlen);
2650 register short *tbl;
2652 complement = o->op_private & OPpTRANS_COMPLEMENT;
2653 del = o->op_private & OPpTRANS_DELETE;
2654 squash = o->op_private & OPpTRANS_SQUASH;
2657 o->op_private |= OPpTRANS_FROM_UTF;
2660 o->op_private |= OPpTRANS_TO_UTF;
2662 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2663 SV* listsv = newSVpvn("# comment\n",10);
2665 U8* tend = t + tlen;
2666 U8* rend = r + rlen;
2680 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2681 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2682 U8* tsave = from_utf ? NULL : trlist_upgrade(&t, &tend);
2683 U8* rsave = to_utf ? NULL : trlist_upgrade(&r, &rend);
2686 U8 tmpbuf[UTF8_MAXLEN+1];
2690 New(1109, cp, tlen, U8*);
2692 transv = newSVpvn("",0);
2701 qsort(cp, i, sizeof(U8*), utf8compare);
2702 for (j = 0; j < i; j++) {
2704 I32 cur = j < i ? cp[j+1] - s : tend - s;
2705 UV val = utf8_to_uv(s, cur, &ulen, 0);
2707 diff = val - nextmin;
2709 t = uv_to_utf8(tmpbuf,nextmin);
2710 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2712 t = uv_to_utf8(tmpbuf, val - 1);
2713 sv_catpvn(transv, "\377", 1);
2714 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2718 val = utf8_to_uv(s+1, cur - 1, &ulen, 0);
2722 t = uv_to_utf8(tmpbuf,nextmin);
2723 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2724 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2725 sv_catpvn(transv, "\377", 1);
2726 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2727 t = (U8*)SvPVX(transv);
2728 tlen = SvCUR(transv);
2731 else if (!rlen && !del) {
2732 r = t; rlen = tlen; rend = tend;
2736 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
2738 o->op_private |= OPpTRANS_IDENTICAL;
2742 while (t < tend || tfirst <= tlast) {
2743 /* see if we need more "t" chars */
2744 if (tfirst > tlast) {
2745 tfirst = (I32)utf8_to_uv(t, tend - t, &ulen, 0);
2747 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
2749 tlast = (I32)utf8_to_uv(t, tend - t, &ulen, 0);
2756 /* now see if we need more "r" chars */
2757 if (rfirst > rlast) {
2759 rfirst = (I32)utf8_to_uv(r, rend - r, &ulen, 0);
2761 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
2763 rlast = (I32)utf8_to_uv(r, rend - r, &ulen, 0);
2772 rfirst = rlast = 0xffffffff;
2776 /* now see which range will peter our first, if either. */
2777 tdiff = tlast - tfirst;
2778 rdiff = rlast - rfirst;
2785 if (rfirst == 0xffffffff) {
2786 diff = tdiff; /* oops, pretend rdiff is infinite */
2788 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
2789 (long)tfirst, (long)tlast);
2791 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
2795 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
2796 (long)tfirst, (long)(tfirst + diff),
2799 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
2800 (long)tfirst, (long)rfirst);
2802 if (rfirst + diff > max)
2803 max = rfirst + diff;
2806 grows = (UNISKIP(tfirst) < UNISKIP(rfirst));
2817 else if (max > 0xff)
2822 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2823 SvREFCNT_dec(listsv);
2825 SvREFCNT_dec(transv);
2827 if (!del && havefinal)
2828 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5,
2829 newSVuv((UV)final), 0);
2832 o->op_private |= OPpTRANS_GROWS;
2844 tbl = (short*)cPVOPo->op_pv;
2846 Zero(tbl, 256, short);
2847 for (i = 0; i < tlen; i++)
2849 for (i = 0, j = 0; i < 256; i++) {
2860 if (i < 128 && r[j] >= 128)
2868 if (!rlen && !del) {
2871 o->op_private |= OPpTRANS_IDENTICAL;
2873 for (i = 0; i < 256; i++)
2875 for (i = 0, j = 0; i < tlen; i++,j++) {
2878 if (tbl[t[i]] == -1)
2884 if (tbl[t[i]] == -1) {
2885 if (t[i] < 128 && r[j] >= 128)
2892 o->op_private |= OPpTRANS_GROWS;
2900 Perl_newPMOP(pTHX_ I32 type, I32 flags)
2904 NewOp(1101, pmop, 1, PMOP);
2905 pmop->op_type = type;
2906 pmop->op_ppaddr = PL_ppaddr[type];
2907 pmop->op_flags = flags;
2908 pmop->op_private = 0 | (flags >> 8);
2910 if (PL_hints & HINT_RE_TAINT)
2911 pmop->op_pmpermflags |= PMf_RETAINT;
2912 if (PL_hints & HINT_LOCALE)
2913 pmop->op_pmpermflags |= PMf_LOCALE;
2914 pmop->op_pmflags = pmop->op_pmpermflags;
2916 /* link into pm list */
2917 if (type != OP_TRANS && PL_curstash) {
2918 pmop->op_pmnext = HvPMROOT(PL_curstash);
2919 HvPMROOT(PL_curstash) = pmop;
2926 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
2930 I32 repl_has_vars = 0;
2932 if (o->op_type == OP_TRANS)
2933 return pmtrans(o, expr, repl);
2935 PL_hints |= HINT_BLOCK_SCOPE;
2938 if (expr->op_type == OP_CONST) {
2940 SV *pat = ((SVOP*)expr)->op_sv;
2941 char *p = SvPV(pat, plen);
2942 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2943 sv_setpvn(pat, "\\s+", 3);
2944 p = SvPV(pat, plen);
2945 pm->op_pmflags |= PMf_SKIPWHITE;
2947 if ((PL_hints & HINT_UTF8) || DO_UTF8(pat))
2948 pm->op_pmdynflags |= PMdf_UTF8;
2949 pm->op_pmregexp = CALLREGCOMP(aTHX_ p, p + plen, pm);
2950 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2951 pm->op_pmflags |= PMf_WHITE;
2955 if (PL_hints & HINT_UTF8)
2956 pm->op_pmdynflags |= PMdf_UTF8;
2957 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2958 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2960 : OP_REGCMAYBE),0,expr);
2962 NewOp(1101, rcop, 1, LOGOP);
2963 rcop->op_type = OP_REGCOMP;
2964 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2965 rcop->op_first = scalar(expr);
2966 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2967 ? (OPf_SPECIAL | OPf_KIDS)
2969 rcop->op_private = 1;
2972 /* establish postfix order */
2973 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2975 rcop->op_next = expr;
2976 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2979 rcop->op_next = LINKLIST(expr);
2980 expr->op_next = (OP*)rcop;
2983 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2988 if (pm->op_pmflags & PMf_EVAL) {
2990 if (CopLINE(PL_curcop) < PL_multi_end)
2991 CopLINE_set(PL_curcop, PL_multi_end);
2994 else if (repl->op_type == OP_THREADSV
2995 && strchr("&`'123456789+",
2996 PL_threadsv_names[repl->op_targ]))
3000 #endif /* USE_THREADS */
3001 else if (repl->op_type == OP_CONST)
3005 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
3006 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3008 if (curop->op_type == OP_THREADSV) {
3010 if (strchr("&`'123456789+", curop->op_private))
3014 if (curop->op_type == OP_GV) {
3015 GV *gv = cGVOPx_gv(curop);
3017 if (strchr("&`'123456789+", *GvENAME(gv)))
3020 #endif /* USE_THREADS */
3021 else if (curop->op_type == OP_RV2CV)
3023 else if (curop->op_type == OP_RV2SV ||
3024 curop->op_type == OP_RV2AV ||
3025 curop->op_type == OP_RV2HV ||
3026 curop->op_type == OP_RV2GV) {
3027 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
3030 else if (curop->op_type == OP_PADSV ||
3031 curop->op_type == OP_PADAV ||
3032 curop->op_type == OP_PADHV ||
3033 curop->op_type == OP_PADANY) {
3036 else if (curop->op_type == OP_PUSHRE)
3037 ; /* Okay here, dangerous in newASSIGNOP */
3046 && (!pm->op_pmregexp
3047 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
3048 pm->op_pmflags |= PMf_CONST; /* const for long enough */
3049 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
3050 prepend_elem(o->op_type, scalar(repl), o);
3053 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
3054 pm->op_pmflags |= PMf_MAYBE_CONST;
3055 pm->op_pmpermflags |= PMf_MAYBE_CONST;
3057 NewOp(1101, rcop, 1, LOGOP);
3058 rcop->op_type = OP_SUBSTCONT;
3059 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
3060 rcop->op_first = scalar(repl);
3061 rcop->op_flags |= OPf_KIDS;
3062 rcop->op_private = 1;
3065 /* establish postfix order */
3066 rcop->op_next = LINKLIST(repl);
3067 repl->op_next = (OP*)rcop;
3069 pm->op_pmreplroot = scalar((OP*)rcop);
3070 pm->op_pmreplstart = LINKLIST(rcop);
3079 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
3082 NewOp(1101, svop, 1, SVOP);
3083 svop->op_type = type;
3084 svop->op_ppaddr = PL_ppaddr[type];
3086 svop->op_next = (OP*)svop;
3087 svop->op_flags = flags;
3088 if (PL_opargs[type] & OA_RETSCALAR)
3090 if (PL_opargs[type] & OA_TARGET)
3091 svop->op_targ = pad_alloc(type, SVs_PADTMP);
3092 return CHECKOP(type, svop);
3096 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
3099 NewOp(1101, padop, 1, PADOP);
3100 padop->op_type = type;
3101 padop->op_ppaddr = PL_ppaddr[type];
3102 padop->op_padix = pad_alloc(type, SVs_PADTMP);
3103 SvREFCNT_dec(PL_curpad[padop->op_padix]);
3104 PL_curpad[padop->op_padix] = sv;
3106 padop->op_next = (OP*)padop;
3107 padop->op_flags = flags;
3108 if (PL_opargs[type] & OA_RETSCALAR)
3110 if (PL_opargs[type] & OA_TARGET)
3111 padop->op_targ = pad_alloc(type, SVs_PADTMP);
3112 return CHECKOP(type, padop);
3116 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
3120 return newPADOP(type, flags, SvREFCNT_inc(gv));
3122 return newSVOP(type, flags, SvREFCNT_inc(gv));
3127 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
3130 NewOp(1101, pvop, 1, PVOP);
3131 pvop->op_type = type;
3132 pvop->op_ppaddr = PL_ppaddr[type];
3134 pvop->op_next = (OP*)pvop;
3135 pvop->op_flags = flags;
3136 if (PL_opargs[type] & OA_RETSCALAR)
3138 if (PL_opargs[type] & OA_TARGET)
3139 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
3140 return CHECKOP(type, pvop);
3144 Perl_package(pTHX_ OP *o)
3148 save_hptr(&PL_curstash);
3149 save_item(PL_curstname);
3154 name = SvPV(sv, len);
3155 PL_curstash = gv_stashpvn(name,len,TRUE);
3156 sv_setpvn(PL_curstname, name, len);
3160 sv_setpv(PL_curstname,"<none>");
3161 PL_curstash = Nullhv;
3163 PL_hints |= HINT_BLOCK_SCOPE;
3164 PL_copline = NOLINE;
3169 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg)
3177 if (id->op_type != OP_CONST)
3178 Perl_croak(aTHX_ "Module name must be constant");
3182 if (version != Nullop) {
3183 SV *vesv = ((SVOP*)version)->op_sv;
3185 if (arg == Nullop && !SvNIOKp(vesv)) {
3192 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
3193 Perl_croak(aTHX_ "Version number must be constant number");
3195 /* Make copy of id so we don't free it twice */
3196 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3198 /* Fake up a method call to VERSION */
3199 meth = newSVpvn("VERSION",7);
3200 sv_upgrade(meth, SVt_PVIV);
3201 (void)SvIOK_on(meth);
3202 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
3203 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3204 append_elem(OP_LIST,
3205 prepend_elem(OP_LIST, pack, list(version)),
3206 newSVOP(OP_METHOD_NAMED, 0, meth)));
3210 /* Fake up an import/unimport */
3211 if (arg && arg->op_type == OP_STUB)
3212 imop = arg; /* no import on explicit () */
3213 else if (SvNIOKp(((SVOP*)id)->op_sv)) {
3214 imop = Nullop; /* use 5.0; */
3219 /* Make copy of id so we don't free it twice */
3220 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3222 /* Fake up a method call to import/unimport */
3223 meth = aver ? newSVpvn("import",6) : newSVpvn("unimport", 8);;
3224 sv_upgrade(meth, SVt_PVIV);
3225 (void)SvIOK_on(meth);
3226 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
3227 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3228 append_elem(OP_LIST,
3229 prepend_elem(OP_LIST, pack, list(arg)),
3230 newSVOP(OP_METHOD_NAMED, 0, meth)));
3233 /* Fake up a require, handle override, if any */
3234 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
3235 if (!(gv && GvIMPORTED_CV(gv)))
3236 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
3238 if (gv && GvIMPORTED_CV(gv)) {
3239 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3240 append_elem(OP_LIST, id,
3241 scalar(newUNOP(OP_RV2CV, 0,
3246 rqop = newUNOP(OP_REQUIRE, 0, id);
3249 /* Fake up the BEGIN {}, which does its thing immediately. */
3251 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
3254 append_elem(OP_LINESEQ,
3255 append_elem(OP_LINESEQ,
3256 newSTATEOP(0, Nullch, rqop),
3257 newSTATEOP(0, Nullch, veop)),
3258 newSTATEOP(0, Nullch, imop) ));
3260 PL_hints |= HINT_BLOCK_SCOPE;
3261 PL_copline = NOLINE;
3266 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
3269 va_start(args, ver);
3270 vload_module(flags, name, ver, &args);
3274 #ifdef PERL_IMPLICIT_CONTEXT
3276 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
3280 va_start(args, ver);
3281 vload_module(flags, name, ver, &args);
3287 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
3289 OP *modname, *veop, *imop;
3291 modname = newSVOP(OP_CONST, 0, name);
3292 modname->op_private |= OPpCONST_BARE;
3294 veop = newSVOP(OP_CONST, 0, ver);
3298 if (flags & PERL_LOADMOD_NOIMPORT) {
3299 imop = sawparens(newNULLLIST());
3301 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
3302 imop = va_arg(*args, OP*);
3307 sv = va_arg(*args, SV*);
3309 imop = append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
3310 sv = va_arg(*args, SV*);
3314 line_t ocopline = PL_copline;
3315 int oexpect = PL_expect;
3317 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
3318 veop, modname, imop);
3319 PL_expect = oexpect;
3320 PL_copline = ocopline;
3325 Perl_dofile(pTHX_ OP *term)
3330 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
3331 if (!(gv && GvIMPORTED_CV(gv)))
3332 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
3334 if (gv && GvIMPORTED_CV(gv)) {
3335 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3336 append_elem(OP_LIST, term,
3337 scalar(newUNOP(OP_RV2CV, 0,
3342 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3348 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
3350 return newBINOP(OP_LSLICE, flags,
3351 list(force_list(subscript)),
3352 list(force_list(listval)) );
3356 S_list_assignment(pTHX_ register OP *o)
3361 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3362 o = cUNOPo->op_first;
3364 if (o->op_type == OP_COND_EXPR) {
3365 I32 t = list_assignment(cLOGOPo->op_first->op_sibling);
3366 I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
3371 yyerror("Assignment to both a list and a scalar");
3375 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3376 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3377 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
3380 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
3383 if (o->op_type == OP_RV2SV)
3390 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
3395 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
3396 return newLOGOP(optype, 0,
3397 mod(scalar(left), optype),
3398 newUNOP(OP_SASSIGN, 0, scalar(right)));
3401 return newBINOP(optype, OPf_STACKED,
3402 mod(scalar(left), optype), scalar(right));
3406 if (list_assignment(left)) {
3410 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3411 left = mod(left, OP_AASSIGN);
3419 curop = list(force_list(left));
3420 o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
3421 o->op_private = 0 | (flags >> 8);
3422 for (curop = ((LISTOP*)curop)->op_first;
3423 curop; curop = curop->op_sibling)
3425 if (curop->op_type == OP_RV2HV &&
3426 ((UNOP*)curop)->op_first->op_type != OP_GV) {
3427 o->op_private |= OPpASSIGN_HASH;
3431 if (!(left->op_private & OPpLVAL_INTRO)) {
3434 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3435 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3436 if (curop->op_type == OP_GV) {
3437 GV *gv = cGVOPx_gv(curop);
3438 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3440 SvCUR(gv) = PL_generation;
3442 else if (curop->op_type == OP_PADSV ||
3443 curop->op_type == OP_PADAV ||
3444 curop->op_type == OP_PADHV ||
3445 curop->op_type == OP_PADANY) {
3446 SV **svp = AvARRAY(PL_comppad_name);
3447 SV *sv = svp[curop->op_targ];
3448 if (SvCUR(sv) == PL_generation)
3450 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
3452 else if (curop->op_type == OP_RV2CV)
3454 else if (curop->op_type == OP_RV2SV ||
3455 curop->op_type == OP_RV2AV ||
3456 curop->op_type == OP_RV2HV ||
3457 curop->op_type == OP_RV2GV) {
3458 if (lastop->op_type != OP_GV) /* funny deref? */
3461 else if (curop->op_type == OP_PUSHRE) {
3462 if (((PMOP*)curop)->op_pmreplroot) {
3464 GV *gv = (GV*)PL_curpad[(PADOFFSET)((PMOP*)curop)->op_pmreplroot];
3466 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3468 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3470 SvCUR(gv) = PL_generation;
3479 o->op_private |= OPpASSIGN_COMMON;
3481 if (right && right->op_type == OP_SPLIT) {
3483 if ((tmpop = ((LISTOP*)right)->op_first) &&
3484 tmpop->op_type == OP_PUSHRE)
3486 PMOP *pm = (PMOP*)tmpop;
3487 if (left->op_type == OP_RV2AV &&
3488 !(left->op_private & OPpLVAL_INTRO) &&
3489 !(o->op_private & OPpASSIGN_COMMON) )
3491 tmpop = ((UNOP*)left)->op_first;
3492 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
3494 pm->op_pmreplroot = (OP*)cPADOPx(tmpop)->op_padix;
3495 cPADOPx(tmpop)->op_padix = 0; /* steal it */
3497 pm->op_pmreplroot = (OP*)cSVOPx(tmpop)->op_sv;
3498 cSVOPx(tmpop)->op_sv = Nullsv; /* steal it */
3500 pm->op_pmflags |= PMf_ONCE;
3501 tmpop = cUNOPo->op_first; /* to list (nulled) */
3502 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3503 tmpop->op_sibling = Nullop; /* don't free split */
3504 right->op_next = tmpop->op_next; /* fix starting loc */
3505 op_free(o); /* blow off assign */
3506 right->op_flags &= ~OPf_WANT;
3507 /* "I don't know and I don't care." */
3512 if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
3513 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3515 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3517 sv_setiv(sv, PL_modcount+1);
3525 right = newOP(OP_UNDEF, 0);
3526 if (right->op_type == OP_READLINE) {
3527 right->op_flags |= OPf_STACKED;
3528 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3531 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3532 o = newBINOP(OP_SASSIGN, flags,
3533 scalar(right), mod(scalar(left), OP_SASSIGN) );
3545 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3547 U32 seq = intro_my();
3550 NewOp(1101, cop, 1, COP);
3551 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
3552 cop->op_type = OP_DBSTATE;
3553 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3556 cop->op_type = OP_NEXTSTATE;
3557 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3559 cop->op_flags = flags;
3560 cop->op_private = (PL_hints & HINT_BYTE);
3562 cop->op_private |= NATIVE_HINTS;
3564 PL_compiling.op_private = cop->op_private;
3565 cop->op_next = (OP*)cop;
3568 cop->cop_label = label;
3569 PL_hints |= HINT_BLOCK_SCOPE;
3572 cop->cop_arybase = PL_curcop->cop_arybase;
3573 if (specialWARN(PL_curcop->cop_warnings))
3574 cop->cop_warnings = PL_curcop->cop_warnings ;
3576 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3577 if (specialCopIO(PL_curcop->cop_io))
3578 cop->cop_io = PL_curcop->cop_io;
3580 cop->cop_io = newSVsv(PL_curcop->cop_io) ;
3583 if (PL_copline == NOLINE)
3584 CopLINE_set(cop, CopLINE(PL_curcop));
3586 CopLINE_set(cop, PL_copline);
3587 PL_copline = NOLINE;
3590 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
3592 CopFILEGV_set(cop, CopFILEGV(PL_curcop));
3594 CopSTASH_set(cop, PL_curstash);
3596 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3597 SV **svp = av_fetch(CopFILEAV(PL_curcop), (I32)CopLINE(cop), FALSE);
3598 if (svp && *svp != &PL_sv_undef && !SvIOK(*svp)) {
3599 (void)SvIOK_on(*svp);
3600 SvIVX(*svp) = PTR2IV(cop);
3604 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3607 /* "Introduce" my variables to visible status. */
3615 if (! PL_min_intro_pending)
3616 return PL_cop_seqmax;
3618 svp = AvARRAY(PL_comppad_name);
3619 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
3620 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
3621 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
3622 SvNVX(sv) = (NV)PL_cop_seqmax;
3625 PL_min_intro_pending = 0;
3626 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
3627 return PL_cop_seqmax++;
3631 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
3633 return new_logop(type, flags, &first, &other);
3637 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
3641 OP *first = *firstp;
3642 OP *other = *otherp;
3644 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3645 return newBINOP(type, flags, scalar(first), scalar(other));
3647 scalarboolean(first);
3648 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3649 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3650 if (type == OP_AND || type == OP_OR) {
3656 first = *firstp = cUNOPo->op_first;
3658 first->op_next = o->op_next;
3659 cUNOPo->op_first = Nullop;
3663 if (first->op_type == OP_CONST) {
3664 if (ckWARN(WARN_BAREWORD) && (first->op_private & OPpCONST_BARE))
3665 Perl_warner(aTHX_ WARN_BAREWORD, "Bareword found in conditional");
3666 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3677 else if (first->op_type == OP_WANTARRAY) {
3683 else if (ckWARN(WARN_MISC) && (first->op_flags & OPf_KIDS)) {
3684 OP *k1 = ((UNOP*)first)->op_first;
3685 OP *k2 = k1->op_sibling;
3687 switch (first->op_type)
3690 if (k2 && k2->op_type == OP_READLINE
3691 && (k2->op_flags & OPf_STACKED)
3692 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3694 warnop = k2->op_type;
3699 if (k1->op_type == OP_READDIR
3700 || k1->op_type == OP_GLOB
3701 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3702 || k1->op_type == OP_EACH)
3704 warnop = ((k1->op_type == OP_NULL)
3705 ? k1->op_targ : k1->op_type);
3710 line_t oldline = CopLINE(PL_curcop);
3711 CopLINE_set(PL_curcop, PL_copline);
3712 Perl_warner(aTHX_ WARN_MISC,
3713 "Value of %s%s can be \"0\"; test with defined()",
3715 ((warnop == OP_READLINE || warnop == OP_GLOB)
3716 ? " construct" : "() operator"));
3717 CopLINE_set(PL_curcop, oldline);
3724 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3725 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3727 NewOp(1101, logop, 1, LOGOP);
3729 logop->op_type = type;
3730 logop->op_ppaddr = PL_ppaddr[type];
3731 logop->op_first = first;
3732 logop->op_flags = flags | OPf_KIDS;
3733 logop->op_other = LINKLIST(other);
3734 logop->op_private = 1 | (flags >> 8);
3736 /* establish postfix order */
3737 logop->op_next = LINKLIST(first);
3738 first->op_next = (OP*)logop;
3739 first->op_sibling = other;
3741 o = newUNOP(OP_NULL, 0, (OP*)logop);
3748 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
3755 return newLOGOP(OP_AND, 0, first, trueop);
3757 return newLOGOP(OP_OR, 0, first, falseop);
3759 scalarboolean(first);
3760 if (first->op_type == OP_CONST) {
3761 if (SvTRUE(((SVOP*)first)->op_sv)) {
3772 else if (first->op_type == OP_WANTARRAY) {
3776 NewOp(1101, logop, 1, LOGOP);
3777 logop->op_type = OP_COND_EXPR;
3778 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3779 logop->op_first = first;
3780 logop->op_flags = flags | OPf_KIDS;
3781 logop->op_private = 1 | (flags >> 8);
3782 logop->op_other = LINKLIST(trueop);
3783 logop->op_next = LINKLIST(falseop);
3786 /* establish postfix order */
3787 start = LINKLIST(first);
3788 first->op_next = (OP*)logop;
3790 first->op_sibling = trueop;
3791 trueop->op_sibling = falseop;
3792 o = newUNOP(OP_NULL, 0, (OP*)logop);
3794 trueop->op_next = falseop->op_next = o;
3801 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
3809 NewOp(1101, range, 1, LOGOP);
3811 range->op_type = OP_RANGE;
3812 range->op_ppaddr = PL_ppaddr[OP_RANGE];
3813 range->op_first = left;
3814 range->op_flags = OPf_KIDS;
3815 leftstart = LINKLIST(left);
3816 range->op_other = LINKLIST(right);
3817 range->op_private = 1 | (flags >> 8);
3819 left->op_sibling = right;
3821 range->op_next = (OP*)range;
3822 flip = newUNOP(OP_FLIP, flags, (OP*)range);
3823 flop = newUNOP(OP_FLOP, 0, flip);
3824 o = newUNOP(OP_NULL, 0, flop);
3826 range->op_next = leftstart;
3828 left->op_next = flip;
3829 right->op_next = flop;
3831 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3832 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
3833 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3834 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3836 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3837 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3840 if (!flip->op_private || !flop->op_private)
3841 linklist(o); /* blow off optimizer unless constant */
3847 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
3851 int once = block && block->op_flags & OPf_SPECIAL &&
3852 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3855 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3856 return block; /* do {} while 0 does once */
3857 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3858 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3859 expr = newUNOP(OP_DEFINED, 0,
3860 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3861 } else if (expr->op_flags & OPf_KIDS) {
3862 OP *k1 = ((UNOP*)expr)->op_first;
3863 OP *k2 = (k1) ? k1->op_sibling : NULL;
3864 switch (expr->op_type) {
3866 if (k2 && k2->op_type == OP_READLINE
3867 && (k2->op_flags & OPf_STACKED)
3868 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3869 expr = newUNOP(OP_DEFINED, 0, expr);
3873 if (k1->op_type == OP_READDIR
3874 || k1->op_type == OP_GLOB
3875 || (k1->op_type == OP_NULL && k1->op_targ == OP_NULL)
3876 || k1->op_type == OP_EACH)
3877 expr = newUNOP(OP_DEFINED, 0, expr);
3883 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3884 o = new_logop(OP_AND, 0, &expr, &listop);
3887 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3889 if (once && o != listop)
3890 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3893 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3895 o->op_flags |= flags;
3897 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3902 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
3911 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3912 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
3913 expr = newUNOP(OP_DEFINED, 0,
3914 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3915 } else if (expr && (expr->op_flags & OPf_KIDS)) {
3916 OP *k1 = ((UNOP*)expr)->op_first;
3917 OP *k2 = (k1) ? k1->op_sibling : NULL;
3918 switch (expr->op_type) {
3920 if (k2 && k2->op_type == OP_READLINE
3921 && (k2->op_flags & OPf_STACKED)
3922 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3923 expr = newUNOP(OP_DEFINED, 0, expr);
3927 if (k1->op_type == OP_READDIR
3928 || k1->op_type == OP_GLOB
3929 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3930 || k1->op_type == OP_EACH)
3931 expr = newUNOP(OP_DEFINED, 0, expr);
3937 block = newOP(OP_NULL, 0);
3939 block = scope(block);
3943 next = LINKLIST(cont);
3946 OP *unstack = newOP(OP_UNSTACK, 0);
3949 cont = append_elem(OP_LINESEQ, cont, unstack);
3950 if ((line_t)whileline != NOLINE) {
3951 PL_copline = whileline;
3952 cont = append_elem(OP_LINESEQ, cont,
3953 newSTATEOP(0, Nullch, Nullop));
3957 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3958 redo = LINKLIST(listop);
3961 PL_copline = whileline;
3963 o = new_logop(OP_AND, 0, &expr, &listop);
3964 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3965 op_free(expr); /* oops, it's a while (0) */
3967 return Nullop; /* listop already freed by new_logop */
3970 ((LISTOP*)listop)->op_last->op_next = condop =
3971 (o == listop ? redo : LINKLIST(o));
3977 NewOp(1101,loop,1,LOOP);
3978 loop->op_type = OP_ENTERLOOP;
3979 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3980 loop->op_private = 0;
3981 loop->op_next = (OP*)loop;
3984 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3986 loop->op_redoop = redo;
3987 loop->op_lastop = o;
3988 o->op_private |= loopflags;
3991 loop->op_nextop = next;
3993 loop->op_nextop = o;
3995 o->op_flags |= flags;
3996 o->op_private |= (flags >> 8);
4001 Perl_newFOROP(pTHX_ I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
4009 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
4010 sv->op_type = OP_RV2GV;
4011 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
4013 else if (sv->op_type == OP_PADSV) { /* private variable */
4014 padoff = sv->op_targ;
4019 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
4020 padoff = sv->op_targ;
4022 iterflags |= OPf_SPECIAL;
4027 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
4031 padoff = find_threadsv("_");
4032 iterflags |= OPf_SPECIAL;
4034 sv = newGVOP(OP_GV, 0, PL_defgv);
4037 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
4038 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
4039 iterflags |= OPf_STACKED;
4041 else if (expr->op_type == OP_NULL &&
4042 (expr->op_flags & OPf_KIDS) &&
4043 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
4045 /* Basically turn for($x..$y) into the same as for($x,$y), but we
4046 * set the STACKED flag to indicate that these values are to be
4047 * treated as min/max values by 'pp_iterinit'.
4049 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
4050 LOGOP* range = (LOGOP*) flip->op_first;
4051 OP* left = range->op_first;
4052 OP* right = left->op_sibling;
4055 range->op_flags &= ~OPf_KIDS;
4056 range->op_first = Nullop;
4058 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
4059 listop->op_first->op_next = range->op_next;
4060 left->op_next = range->op_other;
4061 right->op_next = (OP*)listop;
4062 listop->op_next = listop->op_first;
4065 expr = (OP*)(listop);
4067 iterflags |= OPf_STACKED;
4070 expr = mod(force_list(expr), OP_GREPSTART);
4074 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
4075 append_elem(OP_LIST, expr, scalar(sv))));
4076 assert(!loop->op_next);
4077 #ifdef PL_OP_SLAB_ALLOC
4080 NewOp(1234,tmp,1,LOOP);
4081 Copy(loop,tmp,1,LOOP);
4085 Renew(loop, 1, LOOP);
4087 loop->op_targ = padoff;
4088 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
4089 PL_copline = forline;
4090 return newSTATEOP(0, label, wop);
4094 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
4099 if (type != OP_GOTO || label->op_type == OP_CONST) {
4100 /* "last()" means "last" */
4101 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
4102 o = newOP(type, OPf_SPECIAL);
4104 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
4105 ? SvPVx(((SVOP*)label)->op_sv, n_a)
4111 if (label->op_type == OP_ENTERSUB)
4112 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
4113 o = newUNOP(type, OPf_STACKED, label);
4115 PL_hints |= HINT_BLOCK_SCOPE;
4120 Perl_cv_undef(pTHX_ CV *cv)
4124 MUTEX_DESTROY(CvMUTEXP(cv));
4125 Safefree(CvMUTEXP(cv));
4128 #endif /* USE_THREADS */
4130 if (!CvXSUB(cv) && CvROOT(cv)) {
4132 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
4133 Perl_croak(aTHX_ "Can't undef active subroutine");
4136 Perl_croak(aTHX_ "Can't undef active subroutine");
4137 #endif /* USE_THREADS */
4140 SAVEVPTR(PL_curpad);
4144 op_free(CvROOT(cv));
4145 CvROOT(cv) = Nullop;
4148 SvPOK_off((SV*)cv); /* forget prototype */
4150 SvREFCNT_dec(CvGV(cv));
4152 SvREFCNT_dec(CvOUTSIDE(cv));
4153 CvOUTSIDE(cv) = Nullcv;
4155 SvREFCNT_dec((SV*)CvXSUBANY(cv).any_ptr);
4158 if (CvPADLIST(cv)) {
4159 /* may be during global destruction */
4160 if (SvREFCNT(CvPADLIST(cv))) {
4161 I32 i = AvFILLp(CvPADLIST(cv));
4163 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
4164 SV* sv = svp ? *svp : Nullsv;
4167 if (sv == (SV*)PL_comppad_name)
4168 PL_comppad_name = Nullav;
4169 else if (sv == (SV*)PL_comppad) {
4170 PL_comppad = Nullav;
4171 PL_curpad = Null(SV**);
4175 SvREFCNT_dec((SV*)CvPADLIST(cv));
4177 CvPADLIST(cv) = Nullav;
4182 S_cv_dump(pTHX_ CV *cv)
4185 CV *outside = CvOUTSIDE(cv);
4186 AV* padlist = CvPADLIST(cv);
4193 PerlIO_printf(Perl_debug_log,
4194 "\tCV=0x%"UVxf" (%s), OUTSIDE=0x%"UVxf" (%s)\n",
4196 (CvANON(cv) ? "ANON"
4197 : (cv == PL_main_cv) ? "MAIN"
4198 : CvUNIQUE(cv) ? "UNIQUE"
4199 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
4202 : CvANON(outside) ? "ANON"
4203 : (outside == PL_main_cv) ? "MAIN"
4204 : CvUNIQUE(outside) ? "UNIQUE"
4205 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
4210 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
4211 pad = (AV*)*av_fetch(padlist, 1, FALSE);
4212 pname = AvARRAY(pad_name);
4213 ppad = AvARRAY(pad);
4215 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
4216 if (SvPOK(pname[ix]))
4217 PerlIO_printf(Perl_debug_log,
4218 "\t%4d. 0x%"UVxf" (%s\"%s\" %"IVdf"-%"IVdf")\n",
4219 (int)ix, PTR2UV(ppad[ix]),
4220 SvFAKE(pname[ix]) ? "FAKE " : "",
4222 (IV)I_32(SvNVX(pname[ix])),
4225 #endif /* DEBUGGING */
4229 S_cv_clone2(pTHX_ CV *proto, CV *outside)
4233 AV* protopadlist = CvPADLIST(proto);
4234 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
4235 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
4236 SV** pname = AvARRAY(protopad_name);
4237 SV** ppad = AvARRAY(protopad);
4238 I32 fname = AvFILLp(protopad_name);
4239 I32 fpad = AvFILLp(protopad);
4243 assert(!CvUNIQUE(proto));
4247 SAVESPTR(PL_comppad_name);
4248 SAVESPTR(PL_compcv);
4250 cv = PL_compcv = (CV*)NEWSV(1104,0);
4251 sv_upgrade((SV *)cv, SvTYPE(proto));
4252 CvFLAGS(cv) = CvFLAGS(proto) & ~CVf_CLONE;
4256 New(666, CvMUTEXP(cv), 1, perl_mutex);
4257 MUTEX_INIT(CvMUTEXP(cv));
4259 #endif /* USE_THREADS */
4260 CvFILE(cv) = CvFILE(proto);
4261 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
4262 CvSTASH(cv) = CvSTASH(proto);
4263 CvROOT(cv) = CvROOT(proto);
4264 CvSTART(cv) = CvSTART(proto);
4266 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
4269 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
4271 PL_comppad_name = newAV();
4272 for (ix = fname; ix >= 0; ix--)
4273 av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix]));
4275 PL_comppad = newAV();
4277 comppadlist = newAV();
4278 AvREAL_off(comppadlist);
4279 av_store(comppadlist, 0, (SV*)PL_comppad_name);
4280 av_store(comppadlist, 1, (SV*)PL_comppad);
4281 CvPADLIST(cv) = comppadlist;
4282 av_fill(PL_comppad, AvFILLp(protopad));
4283 PL_curpad = AvARRAY(PL_comppad);
4285 av = newAV(); /* will be @_ */
4287 av_store(PL_comppad, 0, (SV*)av);
4288 AvFLAGS(av) = AVf_REIFY;
4290 for (ix = fpad; ix > 0; ix--) {
4291 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
4292 if (namesv && namesv != &PL_sv_undef) {
4293 char *name = SvPVX(namesv); /* XXX */
4294 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
4295 I32 off = pad_findlex(name, ix, SvIVX(namesv),
4296 CvOUTSIDE(cv), cxstack_ix, 0, 0);
4298 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
4300 Perl_croak(aTHX_ "panic: cv_clone: %s", name);
4302 else { /* our own lexical */
4305 /* anon code -- we'll come back for it */
4306 sv = SvREFCNT_inc(ppad[ix]);
4308 else if (*name == '@')
4310 else if (*name == '%')
4319 else if (IS_PADGV(ppad[ix]) || IS_PADCONST(ppad[ix])) {
4320 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
4323 SV* sv = NEWSV(0,0);
4329 /* Now that vars are all in place, clone nested closures. */
4331 for (ix = fpad; ix > 0; ix--) {
4332 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
4334 && namesv != &PL_sv_undef
4335 && !(SvFLAGS(namesv) & SVf_FAKE)
4336 && *SvPVX(namesv) == '&'
4337 && CvCLONE(ppad[ix]))
4339 CV *kid = cv_clone2((CV*)ppad[ix], cv);
4340 SvREFCNT_dec(ppad[ix]);
4343 PL_curpad[ix] = (SV*)kid;
4347 #ifdef DEBUG_CLOSURES
4348 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
4350 PerlIO_printf(Perl_debug_log, " from:\n");
4352 PerlIO_printf(Perl_debug_log, " to:\n");
4359 SV* const_sv = op_const_sv(CvSTART(cv), cv);
4361 /* constant sub () { $x } closing over $x - see lib/constant.pm */
4363 cv = newCONSTSUB(CvSTASH(proto), 0, const_sv);
4370 Perl_cv_clone(pTHX_ CV *proto)
4373 LOCK_CRED_MUTEX; /* XXX create separate mutex */
4374 cv = cv_clone2(proto, CvOUTSIDE(proto));
4375 UNLOCK_CRED_MUTEX; /* XXX create separate mutex */
4380 Perl_cv_ckproto(pTHX_ CV *cv, GV *gv, char *p)
4382 if (((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) && ckWARN_d(WARN_PROTOTYPE)) {
4383 SV* msg = sv_newmortal();
4387 gv_efullname3(name = sv_newmortal(), gv, Nullch);
4388 sv_setpv(msg, "Prototype mismatch:");
4390 Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, name);
4392 Perl_sv_catpvf(aTHX_ msg, " (%s)", SvPVX(cv));
4393 sv_catpv(msg, " vs ");
4395 Perl_sv_catpvf(aTHX_ msg, "(%s)", p);
4397 sv_catpv(msg, "none");
4398 Perl_warner(aTHX_ WARN_PROTOTYPE, "%"SVf, msg);
4402 static void const_sv_xsub(pTHXo_ CV* cv);
4405 =for apidoc cv_const_sv
4407 If C<cv> is a constant sub eligible for inlining. returns the constant
4408 value returned by the sub. Otherwise, returns NULL.
4410 Constant subs can be created with C<newCONSTSUB> or as described in
4411 L<perlsub/"Constant Functions">.
4416 Perl_cv_const_sv(pTHX_ CV *cv)
4418 if (!cv || !CvCONST(cv))
4420 return (SV*)CvXSUBANY(cv).any_ptr;
4424 Perl_op_const_sv(pTHX_ OP *o, CV *cv)
4431 if (o->op_type == OP_LINESEQ && cLISTOPo->op_first)
4432 o = cLISTOPo->op_first->op_sibling;
4434 for (; o; o = o->op_next) {
4435 OPCODE type = o->op_type;
4437 if (sv && o->op_next == o)
4439 if (o->op_next != o) {
4440 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
4442 if (type == OP_DBSTATE)
4445 if (type == OP_LEAVESUB || type == OP_RETURN)
4449 if (type == OP_CONST && cSVOPo->op_sv)
4451 else if ((type == OP_PADSV || type == OP_CONST) && cv) {
4452 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
4453 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
4457 /* We get here only from cv_clone2() while creating a closure.
4458 Copy the const value here instead of in cv_clone2 so that
4459 SvREADONLY_on doesn't lead to problems when leaving
4464 if (!SvREADONLY(sv) && SvREFCNT(sv) > 1)
4476 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4486 Perl_croak(aTHX_ "\"my sub\" not yet implemented");
4490 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
4492 return Perl_newATTRSUB(aTHX_ floor, o, proto, Nullop, block);
4496 Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4502 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, n_a) : Nullch;
4507 name = o ? SvPVx(cSVOPo->op_sv, n_a) : Nullch;
4508 if (!name && PERLDB_NAMEANON && CopLINE(PL_curcop)) {
4509 SV *sv = sv_newmortal();
4510 Perl_sv_setpvf(aTHX_ sv, "__ANON__[%s:%"IVdf"]",
4511 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
4516 gv = gv_fetchpv(name ? name : (aname ? aname : "__ANON__"),
4517 GV_ADDMULTI | ((block || attrs) ? 0 : GV_NOINIT),
4527 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
4528 maximum a prototype before. */
4529 if (SvTYPE(gv) > SVt_NULL) {
4530 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1)
4531 && ckWARN_d(WARN_PROTOTYPE))
4533 Perl_warner(aTHX_ WARN_PROTOTYPE, "Runaway prototype");
4535 cv_ckproto((CV*)gv, NULL, ps);
4538 sv_setpv((SV*)gv, ps);
4540 sv_setiv((SV*)gv, -1);
4541 SvREFCNT_dec(PL_compcv);
4542 cv = PL_compcv = NULL;
4543 PL_sub_generation++;
4547 cv = (!name || GvCVGEN(gv)) ? Nullcv : GvCV(gv);
4549 if (!block || !ps || *ps || attrs)
4552 const_sv = op_const_sv(block, Nullcv);
4555 bool exists = CvROOT(cv) || CvXSUB(cv);
4556 /* if the subroutine doesn't exist and wasn't pre-declared
4557 * with a prototype, assume it will be AUTOLOADed,
4558 * skipping the prototype check
4560 if (exists || SvPOK(cv))
4561 cv_ckproto(cv, gv, ps);
4562 /* already defined (or promised)? */
4563 if (exists || GvASSUMECV(gv)) {
4564 if (!block && !attrs) {
4565 /* just a "sub foo;" when &foo is already defined */
4566 SAVEFREESV(PL_compcv);
4569 /* ahem, death to those who redefine active sort subs */
4570 if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv))
4571 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", name);
4573 if (ckWARN(WARN_REDEFINE)
4575 && (!const_sv || sv_cmp(cv_const_sv(cv), const_sv))))
4577 line_t oldline = CopLINE(PL_curcop);
4578 CopLINE_set(PL_curcop, PL_copline);
4579 Perl_warner(aTHX_ WARN_REDEFINE,
4580 CvCONST(cv) ? "Constant subroutine %s redefined"
4581 : "Subroutine %s redefined", name);
4582 CopLINE_set(PL_curcop, oldline);
4590 SvREFCNT_inc(const_sv);
4592 assert(!CvROOT(cv) && !CvCONST(cv));
4593 sv_setpv((SV*)cv, ""); /* prototype is "" */
4594 CvXSUBANY(cv).any_ptr = const_sv;
4595 CvXSUB(cv) = const_sv_xsub;
4600 cv = newCONSTSUB(NULL, name, const_sv);
4603 SvREFCNT_dec(PL_compcv);
4605 PL_sub_generation++;
4612 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>
4613 * before we clobber PL_compcv.
4617 if (CvGV(cv) && GvSTASH(CvGV(cv)) && HvNAME(GvSTASH(CvGV(cv))))
4618 stash = GvSTASH(CvGV(cv));
4619 else if (CvSTASH(cv) && HvNAME(CvSTASH(cv)))
4620 stash = CvSTASH(cv);
4622 stash = PL_curstash;
4625 /* possibly about to re-define existing subr -- ignore old cv */
4626 rcv = (SV*)PL_compcv;
4627 if (name && GvSTASH(gv) && HvNAME(GvSTASH(gv)))
4628 stash = GvSTASH(gv);
4630 stash = PL_curstash;
4632 apply_attrs(stash, rcv, attrs);
4634 if (cv) { /* must reuse cv if autoloaded */
4636 /* got here with just attrs -- work done, so bug out */
4637 SAVEFREESV(PL_compcv);
4641 CvFLAGS(cv) = CvFLAGS(PL_compcv);
4642 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
4643 CvOUTSIDE(PL_compcv) = 0;
4644 CvPADLIST(cv) = CvPADLIST(PL_compcv);
4645 CvPADLIST(PL_compcv) = 0;
4646 if (SvREFCNT(PL_compcv) > 1) /* XXX Make closures transit through stub. */
4647 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc((SV*)cv);
4648 SvREFCNT_dec(PL_compcv);
4655 PL_sub_generation++;
4658 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4659 CvFILE(cv) = CopFILE(PL_curcop);
4660 CvSTASH(cv) = PL_curstash;
4663 if (!CvMUTEXP(cv)) {
4664 New(666, CvMUTEXP(cv), 1, perl_mutex);
4665 MUTEX_INIT(CvMUTEXP(cv));
4667 #endif /* USE_THREADS */
4670 sv_setpv((SV*)cv, ps);
4672 if (PL_error_count) {
4676 char *s = strrchr(name, ':');
4678 if (strEQ(s, "BEGIN")) {
4680 "BEGIN not safe after errors--compilation aborted";
4681 if (PL_in_eval & EVAL_KEEPERR)
4682 Perl_croak(aTHX_ not_safe);
4684 /* force display of errors found but not reported */
4685 sv_catpv(ERRSV, not_safe);
4686 Perl_croak(aTHX_ "%s", SvPVx(ERRSV, n_a));
4694 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad))
4695 av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv);
4698 CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0,
4699 mod(scalarseq(block), OP_LEAVESUBLV));
4702 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4704 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4705 OpREFCNT_set(CvROOT(cv), 1);
4706 CvSTART(cv) = LINKLIST(CvROOT(cv));
4707 CvROOT(cv)->op_next = 0;
4710 /* now that optimizer has done its work, adjust pad values */
4712 SV **namep = AvARRAY(PL_comppad_name);
4713 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4716 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix]))
4719 * The only things that a clonable function needs in its
4720 * pad are references to outer lexicals and anonymous subs.
4721 * The rest are created anew during cloning.
4723 if (!((namesv = namep[ix]) != Nullsv &&
4724 namesv != &PL_sv_undef &&
4726 *SvPVX(namesv) == '&')))
4728 SvREFCNT_dec(PL_curpad[ix]);
4729 PL_curpad[ix] = Nullsv;
4732 assert(!CvCONST(cv));
4733 if (ps && !*ps && op_const_sv(block, cv))
4737 AV *av = newAV(); /* Will be @_ */
4739 av_store(PL_comppad, 0, (SV*)av);
4740 AvFLAGS(av) = AVf_REIFY;
4742 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4743 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix]))
4745 if (!SvPADMY(PL_curpad[ix]))
4746 SvPADTMP_on(PL_curpad[ix]);
4750 if (name || aname) {
4752 char *tname = (name ? name : aname);
4754 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4755 SV *sv = NEWSV(0,0);
4756 SV *tmpstr = sv_newmortal();
4757 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
4761 Perl_sv_setpvf(aTHX_ sv, "%s:%ld-%ld",
4763 (long)PL_subline, (long)CopLINE(PL_curcop));
4764 gv_efullname3(tmpstr, gv, Nullch);
4765 hv_store(GvHV(PL_DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
4766 hv = GvHVn(db_postponed);
4767 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
4768 && (pcv = GvCV(db_postponed)))
4774 call_sv((SV*)pcv, G_DISCARD);
4778 if ((s = strrchr(tname,':')))
4783 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4786 if (strEQ(s, "BEGIN")) {
4787 I32 oldscope = PL_scopestack_ix;
4789 SAVECOPFILE(&PL_compiling);
4790 SAVECOPLINE(&PL_compiling);
4792 sv_setsv(PL_rs, PL_nrs);
4795 PL_beginav = newAV();
4796 DEBUG_x( dump_sub(gv) );
4797 av_push(PL_beginav, (SV*)cv);
4798 GvCV(gv) = 0; /* cv has been hijacked */
4799 call_list(oldscope, PL_beginav);
4801 PL_curcop = &PL_compiling;
4802 PL_compiling.op_private = PL_hints;
4805 else if (strEQ(s, "END") && !PL_error_count) {
4808 DEBUG_x( dump_sub(gv) );
4809 av_unshift(PL_endav, 1);
4810 av_store(PL_endav, 0, (SV*)cv);
4811 GvCV(gv) = 0; /* cv has been hijacked */
4813 else if (strEQ(s, "CHECK") && !PL_error_count) {
4815 PL_checkav = newAV();
4816 DEBUG_x( dump_sub(gv) );
4817 if (PL_main_start && ckWARN(WARN_VOID))
4818 Perl_warner(aTHX_ WARN_VOID, "Too late to run CHECK block");
4819 av_unshift(PL_checkav, 1);
4820 av_store(PL_checkav, 0, (SV*)cv);
4821 GvCV(gv) = 0; /* cv has been hijacked */
4823 else if (strEQ(s, "INIT") && !PL_error_count) {
4825 PL_initav = newAV();
4826 DEBUG_x( dump_sub(gv) );
4827 if (PL_main_start && ckWARN(WARN_VOID))
4828 Perl_warner(aTHX_ WARN_VOID, "Too late to run INIT block");
4829 av_push(PL_initav, (SV*)cv);
4830 GvCV(gv) = 0; /* cv has been hijacked */
4835 PL_copline = NOLINE;
4840 /* XXX unsafe for threads if eval_owner isn't held */
4842 =for apidoc newCONSTSUB
4844 Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is
4845 eligible for inlining at compile-time.
4851 Perl_newCONSTSUB(pTHX_ HV *stash, char *name, SV *sv)
4857 SAVECOPLINE(PL_curcop);
4858 CopLINE_set(PL_curcop, PL_copline);
4861 PL_hints &= ~HINT_BLOCK_SCOPE;
4864 SAVESPTR(PL_curstash);
4865 SAVECOPSTASH(PL_curcop);
4866 PL_curstash = stash;
4868 CopSTASHPV(PL_curcop) = stash ? HvNAME(stash) : Nullch;
4870 CopSTASH(PL_curcop) = stash;
4874 cv = newXS(name, const_sv_xsub, __FILE__);
4875 CvXSUBANY(cv).any_ptr = sv;
4877 sv_setpv((SV*)cv, ""); /* prototype is "" */
4885 =for apidoc U||newXS
4887 Used by C<xsubpp> to hook up XSUBs as Perl subs.
4893 Perl_newXS(pTHX_ char *name, XSUBADDR_t subaddr, char *filename)
4895 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
4898 if ((cv = (name ? GvCV(gv) : Nullcv))) {
4900 /* just a cached method */
4904 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4905 /* already defined (or promised) */
4906 if (ckWARN(WARN_REDEFINE) && !(CvGV(cv) && GvSTASH(CvGV(cv))
4907 && HvNAME(GvSTASH(CvGV(cv)))
4908 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
4909 line_t oldline = CopLINE(PL_curcop);
4910 if (PL_copline != NOLINE)
4911 CopLINE_set(PL_curcop, PL_copline);
4912 Perl_warner(aTHX_ WARN_REDEFINE,
4913 CvCONST(cv) ? "Constant subroutine %s redefined"
4914 : "Subroutine %s redefined"
4916 CopLINE_set(PL_curcop, oldline);
4923 if (cv) /* must reuse cv if autoloaded */
4926 cv = (CV*)NEWSV(1105,0);
4927 sv_upgrade((SV *)cv, SVt_PVCV);
4931 PL_sub_generation++;
4934 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4936 New(666, CvMUTEXP(cv), 1, perl_mutex);
4937 MUTEX_INIT(CvMUTEXP(cv));
4939 #endif /* USE_THREADS */
4940 (void)gv_fetchfile(filename);
4941 CvFILE(cv) = filename; /* NOTE: not copied, as it is expected to be
4942 an external constant string */
4943 CvXSUB(cv) = subaddr;
4946 char *s = strrchr(name,':');
4952 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4955 if (strEQ(s, "BEGIN")) {
4957 PL_beginav = newAV();
4958 av_push(PL_beginav, (SV*)cv);
4959 GvCV(gv) = 0; /* cv has been hijacked */
4961 else if (strEQ(s, "END")) {
4964 av_unshift(PL_endav, 1);
4965 av_store(PL_endav, 0, (SV*)cv);
4966 GvCV(gv) = 0; /* cv has been hijacked */
4968 else if (strEQ(s, "CHECK")) {
4970 PL_checkav = newAV();
4971 if (PL_main_start && ckWARN(WARN_VOID))
4972 Perl_warner(aTHX_ WARN_VOID, "Too late to run CHECK block");
4973 av_unshift(PL_checkav, 1);
4974 av_store(PL_checkav, 0, (SV*)cv);
4975 GvCV(gv) = 0; /* cv has been hijacked */
4977 else if (strEQ(s, "INIT")) {
4979 PL_initav = newAV();
4980 if (PL_main_start && ckWARN(WARN_VOID))
4981 Perl_warner(aTHX_ WARN_VOID, "Too late to run INIT block");
4982 av_push(PL_initav, (SV*)cv);
4983 GvCV(gv) = 0; /* cv has been hijacked */
4994 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
5003 name = SvPVx(cSVOPo->op_sv, n_a);
5006 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
5008 if ((cv = GvFORM(gv))) {
5009 if (ckWARN(WARN_REDEFINE)) {
5010 line_t oldline = CopLINE(PL_curcop);
5012 CopLINE_set(PL_curcop, PL_copline);
5013 Perl_warner(aTHX_ WARN_REDEFINE, "Format %s redefined",name);
5014 CopLINE_set(PL_curcop, oldline);
5020 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
5021 CvFILE(cv) = CopFILE(PL_curcop);
5023 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
5024 if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix]))
5025 SvPADTMP_on(PL_curpad[ix]);
5028 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
5029 CvROOT(cv)->op_private |= OPpREFCOUNTED;
5030 OpREFCNT_set(CvROOT(cv), 1);
5031 CvSTART(cv) = LINKLIST(CvROOT(cv));
5032 CvROOT(cv)->op_next = 0;
5035 PL_copline = NOLINE;
5040 Perl_newANONLIST(pTHX_ OP *o)
5042 return newUNOP(OP_REFGEN, 0,
5043 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
5047 Perl_newANONHASH(pTHX_ OP *o)
5049 return newUNOP(OP_REFGEN, 0,
5050 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
5054 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
5056 return newANONATTRSUB(floor, proto, Nullop, block);
5060 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
5062 return newUNOP(OP_REFGEN, 0,
5063 newSVOP(OP_ANONCODE, 0,
5064 (SV*)newATTRSUB(floor, 0, proto, attrs, block)));
5068 Perl_oopsAV(pTHX_ OP *o)
5070 switch (o->op_type) {
5072 o->op_type = OP_PADAV;
5073 o->op_ppaddr = PL_ppaddr[OP_PADAV];
5074 return ref(o, OP_RV2AV);
5077 o->op_type = OP_RV2AV;
5078 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
5083 if (ckWARN_d(WARN_INTERNAL))
5084 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsAV");
5091 Perl_oopsHV(pTHX_ OP *o)
5093 switch (o->op_type) {
5096 o->op_type = OP_PADHV;
5097 o->op_ppaddr = PL_ppaddr[OP_PADHV];
5098 return ref(o, OP_RV2HV);
5102 o->op_type = OP_RV2HV;
5103 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
5108 if (ckWARN_d(WARN_INTERNAL))
5109 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsHV");
5116 Perl_newAVREF(pTHX_ OP *o)
5118 if (o->op_type == OP_PADANY) {
5119 o->op_type = OP_PADAV;
5120 o->op_ppaddr = PL_ppaddr[OP_PADAV];
5123 return newUNOP(OP_RV2AV, 0, scalar(o));
5127 Perl_newGVREF(pTHX_ I32 type, OP *o)
5129 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
5130 return newUNOP(OP_NULL, 0, o);
5131 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
5135 Perl_newHVREF(pTHX_ OP *o)
5137 if (o->op_type == OP_PADANY) {
5138 o->op_type = OP_PADHV;
5139 o->op_ppaddr = PL_ppaddr[OP_PADHV];
5142 return newUNOP(OP_RV2HV, 0, scalar(o));
5146 Perl_oopsCV(pTHX_ OP *o)
5148 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
5154 Perl_newCVREF(pTHX_ I32 flags, OP *o)
5156 return newUNOP(OP_RV2CV, flags, scalar(o));
5160 Perl_newSVREF(pTHX_ OP *o)
5162 if (o->op_type == OP_PADANY) {
5163 o->op_type = OP_PADSV;
5164 o->op_ppaddr = PL_ppaddr[OP_PADSV];
5167 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
5168 o->op_flags |= OPpDONE_SVREF;
5171 return newUNOP(OP_RV2SV, 0, scalar(o));
5174 /* Check routines. */
5177 Perl_ck_anoncode(pTHX_ OP *o)
5182 name = NEWSV(1106,0);
5183 sv_upgrade(name, SVt_PVNV);
5184 sv_setpvn(name, "&", 1);
5187 ix = pad_alloc(o->op_type, SVs_PADMY);
5188 av_store(PL_comppad_name, ix, name);
5189 av_store(PL_comppad, ix, cSVOPo->op_sv);
5190 SvPADMY_on(cSVOPo->op_sv);
5191 cSVOPo->op_sv = Nullsv;
5192 cSVOPo->op_targ = ix;
5197 Perl_ck_bitop(pTHX_ OP *o)
5199 o->op_private = PL_hints;
5204 Perl_ck_concat(pTHX_ OP *o)
5206 if (cUNOPo->op_first->op_type == OP_CONCAT)
5207 o->op_flags |= OPf_STACKED;
5212 Perl_ck_spair(pTHX_ OP *o)
5214 if (o->op_flags & OPf_KIDS) {
5217 OPCODE type = o->op_type;
5218 o = modkids(ck_fun(o), type);
5219 kid = cUNOPo->op_first;
5220 newop = kUNOP->op_first->op_sibling;
5222 (newop->op_sibling ||
5223 !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
5224 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
5225 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
5229 op_free(kUNOP->op_first);
5230 kUNOP->op_first = newop;
5232 o->op_ppaddr = PL_ppaddr[++o->op_type];
5237 Perl_ck_delete(pTHX_ OP *o)
5241 if (o->op_flags & OPf_KIDS) {
5242 OP *kid = cUNOPo->op_first;
5243 switch (kid->op_type) {
5245 o->op_flags |= OPf_SPECIAL;
5248 o->op_private |= OPpSLICE;
5251 o->op_flags |= OPf_SPECIAL;
5256 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element or slice",
5257 PL_op_desc[o->op_type]);
5265 Perl_ck_eof(pTHX_ OP *o)
5267 I32 type = o->op_type;
5269 if (o->op_flags & OPf_KIDS) {
5270 if (cLISTOPo->op_first->op_type == OP_STUB) {
5272 o = newUNOP(type, OPf_SPECIAL,
5273 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
5281 Perl_ck_eval(pTHX_ OP *o)
5283 PL_hints |= HINT_BLOCK_SCOPE;
5284 if (o->op_flags & OPf_KIDS) {
5285 SVOP *kid = (SVOP*)cUNOPo->op_first;
5288 o->op_flags &= ~OPf_KIDS;
5291 else if (kid->op_type == OP_LINESEQ) {
5294 kid->op_next = o->op_next;
5295 cUNOPo->op_first = 0;
5298 NewOp(1101, enter, 1, LOGOP);
5299 enter->op_type = OP_ENTERTRY;
5300 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
5301 enter->op_private = 0;
5303 /* establish postfix order */
5304 enter->op_next = (OP*)enter;
5306 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
5307 o->op_type = OP_LEAVETRY;
5308 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
5309 enter->op_other = o;
5317 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
5319 o->op_targ = (PADOFFSET)PL_hints;
5324 Perl_ck_exit(pTHX_ OP *o)
5327 HV *table = GvHV(PL_hintgv);
5329 SV **svp = hv_fetch(table, "vmsish_exit", 11, FALSE);
5330 if (svp && *svp && SvTRUE(*svp))
5331 o->op_private |= OPpEXIT_VMSISH;
5338 Perl_ck_exec(pTHX_ OP *o)
5341 if (o->op_flags & OPf_STACKED) {
5343 kid = cUNOPo->op_first->op_sibling;
5344 if (kid->op_type == OP_RV2GV)
5353 Perl_ck_exists(pTHX_ OP *o)
5356 if (o->op_flags & OPf_KIDS) {
5357 OP *kid = cUNOPo->op_first;
5358 if (kid->op_type == OP_ENTERSUB) {
5359 (void) ref(kid, o->op_type);
5360 if (kid->op_type != OP_RV2CV && !PL_error_count)
5361 Perl_croak(aTHX_ "%s argument is not a subroutine name",
5362 PL_op_desc[o->op_type]);
5363 o->op_private |= OPpEXISTS_SUB;
5365 else if (kid->op_type == OP_AELEM)
5366 o->op_flags |= OPf_SPECIAL;
5367 else if (kid->op_type != OP_HELEM)
5368 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element",
5369 PL_op_desc[o->op_type]);
5377 Perl_ck_gvconst(pTHX_ register OP *o)
5379 o = fold_constants(o);
5380 if (o->op_type == OP_CONST)
5387 Perl_ck_rvconst(pTHX_ register OP *o)
5389 SVOP *kid = (SVOP*)cUNOPo->op_first;
5391 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5392 if (kid->op_type == OP_CONST) {
5396 SV *kidsv = kid->op_sv;
5399 /* Is it a constant from cv_const_sv()? */
5400 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
5401 SV *rsv = SvRV(kidsv);
5402 int svtype = SvTYPE(rsv);
5403 char *badtype = Nullch;
5405 switch (o->op_type) {
5407 if (svtype > SVt_PVMG)
5408 badtype = "a SCALAR";
5411 if (svtype != SVt_PVAV)
5412 badtype = "an ARRAY";
5415 if (svtype != SVt_PVHV) {
5416 if (svtype == SVt_PVAV) { /* pseudohash? */
5417 SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
5418 if (ksv && SvROK(*ksv)
5419 && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
5428 if (svtype != SVt_PVCV)
5433 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
5436 name = SvPV(kidsv, n_a);
5437 if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
5438 char *badthing = Nullch;
5439 switch (o->op_type) {
5441 badthing = "a SCALAR";
5444 badthing = "an ARRAY";
5447 badthing = "a HASH";
5452 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
5456 * This is a little tricky. We only want to add the symbol if we
5457 * didn't add it in the lexer. Otherwise we get duplicate strict
5458 * warnings. But if we didn't add it in the lexer, we must at
5459 * least pretend like we wanted to add it even if it existed before,
5460 * or we get possible typo warnings. OPpCONST_ENTERED says
5461 * whether the lexer already added THIS instance of this symbol.
5463 iscv = (o->op_type == OP_RV2CV) * 2;
5465 gv = gv_fetchpv(name,
5466 iscv | !(kid->op_private & OPpCONST_ENTERED),
5469 : o->op_type == OP_RV2SV
5471 : o->op_type == OP_RV2AV
5473 : o->op_type == OP_RV2HV
5476 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
5478 kid->op_type = OP_GV;
5479 SvREFCNT_dec(kid->op_sv);
5481 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
5482 kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
5483 SvREFCNT_dec(PL_curpad[kPADOP->op_padix]);
5485 PL_curpad[kPADOP->op_padix] = SvREFCNT_inc(gv);
5487 kid->op_sv = SvREFCNT_inc(gv);
5489 kid->op_private = 0;
5490 kid->op_ppaddr = PL_ppaddr[OP_GV];
5497 Perl_ck_ftst(pTHX_ OP *o)
5499 I32 type = o->op_type;
5501 if (o->op_flags & OPf_REF) {
5504 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
5505 SVOP *kid = (SVOP*)cUNOPo->op_first;
5507 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5509 OP *newop = newGVOP(type, OPf_REF,
5510 gv_fetchpv(SvPVx(kid->op_sv, n_a), TRUE, SVt_PVIO));
5517 if (type == OP_FTTTY)
5518 o = newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
5521 o = newUNOP(type, 0, newDEFSVOP());
5524 if (type == OP_FTTEXT || type == OP_FTBINARY) {
5526 if (PL_hints & HINT_LOCALE)
5527 o->op_private |= OPpLOCALE;
5534 Perl_ck_fun(pTHX_ OP *o)
5540 int type = o->op_type;
5541 register I32 oa = PL_opargs[type] >> OASHIFT;
5543 if (o->op_flags & OPf_STACKED) {
5544 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
5547 return no_fh_allowed(o);
5550 if (o->op_flags & OPf_KIDS) {
5552 tokid = &cLISTOPo->op_first;
5553 kid = cLISTOPo->op_first;
5554 if (kid->op_type == OP_PUSHMARK ||
5555 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
5557 tokid = &kid->op_sibling;
5558 kid = kid->op_sibling;
5560 if (!kid && PL_opargs[type] & OA_DEFGV)
5561 *tokid = kid = newDEFSVOP();
5565 sibl = kid->op_sibling;
5568 /* list seen where single (scalar) arg expected? */
5569 if (numargs == 1 && !(oa >> 4)
5570 && kid->op_type == OP_LIST && type != OP_SCALAR)
5572 return too_many_arguments(o,PL_op_desc[type]);
5585 if (kid->op_type == OP_CONST &&
5586 (kid->op_private & OPpCONST_BARE))
5588 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5589 OP *newop = newAVREF(newGVOP(OP_GV, 0,
5590 gv_fetchpv(name, TRUE, SVt_PVAV) ));
5591 if (ckWARN(WARN_DEPRECATED))
5592 Perl_warner(aTHX_ WARN_DEPRECATED,
5593 "Array @%s missing the @ in argument %"IVdf" of %s()",
5594 name, (IV)numargs, PL_op_desc[type]);
5597 kid->op_sibling = sibl;
5600 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
5601 bad_type(numargs, "array", PL_op_desc[type], kid);
5605 if (kid->op_type == OP_CONST &&
5606 (kid->op_private & OPpCONST_BARE))
5608 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5609 OP *newop = newHVREF(newGVOP(OP_GV, 0,
5610 gv_fetchpv(name, TRUE, SVt_PVHV) ));
5611 if (ckWARN(WARN_DEPRECATED))
5612 Perl_warner(aTHX_ WARN_DEPRECATED,
5613 "Hash %%%s missing the %% in argument %"IVdf" of %s()",
5614 name, (IV)numargs, PL_op_desc[type]);
5617 kid->op_sibling = sibl;
5620 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
5621 bad_type(numargs, "hash", PL_op_desc[type], kid);
5626 OP *newop = newUNOP(OP_NULL, 0, kid);
5627 kid->op_sibling = 0;
5629 newop->op_next = newop;
5631 kid->op_sibling = sibl;
5636 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
5637 if (kid->op_type == OP_CONST &&
5638 (kid->op_private & OPpCONST_BARE))
5640 OP *newop = newGVOP(OP_GV, 0,
5641 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, n_a), TRUE,
5646 else if (kid->op_type == OP_READLINE) {
5647 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
5648 bad_type(numargs, "HANDLE", PL_op_desc[o->op_type], kid);
5651 I32 flags = OPf_SPECIAL;
5655 /* is this op a FH constructor? */
5656 if (is_handle_constructor(o,numargs)) {
5657 char *name = Nullch;
5661 /* Set a flag to tell rv2gv to vivify
5662 * need to "prove" flag does not mean something
5663 * else already - NI-S 1999/05/07
5666 if (kid->op_type == OP_PADSV) {
5667 SV **namep = av_fetch(PL_comppad_name,
5669 if (namep && *namep)
5670 name = SvPV(*namep, len);
5672 else if (kid->op_type == OP_RV2SV
5673 && kUNOP->op_first->op_type == OP_GV)
5675 GV *gv = cGVOPx_gv(kUNOP->op_first);
5677 len = GvNAMELEN(gv);
5679 else if (kid->op_type == OP_AELEM
5680 || kid->op_type == OP_HELEM)
5682 name = "__ANONIO__";
5688 targ = pad_alloc(OP_RV2GV, SVs_PADTMP);
5689 namesv = PL_curpad[targ];
5690 (void)SvUPGRADE(namesv, SVt_PV);
5692 sv_setpvn(namesv, "$", 1);
5693 sv_catpvn(namesv, name, len);
5696 kid->op_sibling = 0;
5697 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
5698 kid->op_targ = targ;
5699 kid->op_private |= priv;
5701 kid->op_sibling = sibl;
5707 mod(scalar(kid), type);
5711 tokid = &kid->op_sibling;
5712 kid = kid->op_sibling;
5714 o->op_private |= numargs;
5716 return too_many_arguments(o,PL_op_desc[o->op_type]);
5719 else if (PL_opargs[type] & OA_DEFGV) {
5721 return newUNOP(type, 0, newDEFSVOP());
5725 while (oa & OA_OPTIONAL)
5727 if (oa && oa != OA_LIST)
5728 return too_few_arguments(o,PL_op_desc[o->op_type]);
5734 Perl_ck_glob(pTHX_ OP *o)
5739 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
5740 append_elem(OP_GLOB, o, newDEFSVOP());
5742 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
5743 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5745 #if !defined(PERL_EXTERNAL_GLOB)
5746 /* XXX this can be tightened up and made more failsafe. */
5749 Perl_load_module(aTHX_ 0, newSVpvn("File::Glob", 10), Nullsv,
5750 /* null-terminated import list */
5751 newSVpvn(":globally", 9), Nullsv);
5752 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5755 #endif /* PERL_EXTERNAL_GLOB */
5757 if (gv && GvIMPORTED_CV(gv)) {
5758 append_elem(OP_GLOB, o,
5759 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
5760 o->op_type = OP_LIST;
5761 o->op_ppaddr = PL_ppaddr[OP_LIST];
5762 cLISTOPo->op_first->op_type = OP_PUSHMARK;
5763 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
5764 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
5765 append_elem(OP_LIST, o,
5766 scalar(newUNOP(OP_RV2CV, 0,
5767 newGVOP(OP_GV, 0, gv)))));
5768 o = newUNOP(OP_NULL, 0, ck_subr(o));
5769 o->op_targ = OP_GLOB; /* hint at what it used to be */
5772 gv = newGVgen("main");
5774 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
5780 Perl_ck_grep(pTHX_ OP *o)
5784 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
5786 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
5787 NewOp(1101, gwop, 1, LOGOP);
5789 if (o->op_flags & OPf_STACKED) {
5792 kid = cLISTOPo->op_first->op_sibling;
5793 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
5796 kid->op_next = (OP*)gwop;
5797 o->op_flags &= ~OPf_STACKED;
5799 kid = cLISTOPo->op_first->op_sibling;
5800 if (type == OP_MAPWHILE)
5807 kid = cLISTOPo->op_first->op_sibling;
5808 if (kid->op_type != OP_NULL)
5809 Perl_croak(aTHX_ "panic: ck_grep");
5810 kid = kUNOP->op_first;
5812 gwop->op_type = type;
5813 gwop->op_ppaddr = PL_ppaddr[type];
5814 gwop->op_first = listkids(o);
5815 gwop->op_flags |= OPf_KIDS;
5816 gwop->op_private = 1;
5817 gwop->op_other = LINKLIST(kid);
5818 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
5819 kid->op_next = (OP*)gwop;
5821 kid = cLISTOPo->op_first->op_sibling;
5822 if (!kid || !kid->op_sibling)
5823 return too_few_arguments(o,PL_op_desc[o->op_type]);
5824 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
5825 mod(kid, OP_GREPSTART);
5831 Perl_ck_index(pTHX_ OP *o)
5833 if (o->op_flags & OPf_KIDS) {
5834 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5836 kid = kid->op_sibling; /* get past "big" */
5837 if (kid && kid->op_type == OP_CONST)
5838 fbm_compile(((SVOP*)kid)->op_sv, 0);
5844 Perl_ck_lengthconst(pTHX_ OP *o)
5846 /* XXX length optimization goes here */
5851 Perl_ck_lfun(pTHX_ OP *o)
5853 OPCODE type = o->op_type;
5854 return modkids(ck_fun(o), type);
5858 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
5860 if ((o->op_flags & OPf_KIDS) && ckWARN(WARN_DEPRECATED)) {
5861 switch (cUNOPo->op_first->op_type) {
5863 /* This is needed for
5864 if (defined %stash::)
5865 to work. Do not break Tk.
5867 break; /* Globals via GV can be undef */
5869 case OP_AASSIGN: /* Is this a good idea? */
5870 Perl_warner(aTHX_ WARN_DEPRECATED,
5871 "defined(@array) is deprecated");
5872 Perl_warner(aTHX_ WARN_DEPRECATED,
5873 "\t(Maybe you should just omit the defined()?)\n");
5876 /* This is needed for
5877 if (defined %stash::)
5878 to work. Do not break Tk.
5880 break; /* Globals via GV can be undef */
5882 Perl_warner(aTHX_ WARN_DEPRECATED,
5883 "defined(%%hash) is deprecated");
5884 Perl_warner(aTHX_ WARN_DEPRECATED,
5885 "\t(Maybe you should just omit the defined()?)\n");
5896 Perl_ck_rfun(pTHX_ OP *o)
5898 OPCODE type = o->op_type;
5899 return refkids(ck_fun(o), type);
5903 Perl_ck_listiob(pTHX_ OP *o)
5907 kid = cLISTOPo->op_first;
5910 kid = cLISTOPo->op_first;
5912 if (kid->op_type == OP_PUSHMARK)
5913 kid = kid->op_sibling;
5914 if (kid && o->op_flags & OPf_STACKED)
5915 kid = kid->op_sibling;
5916 else if (kid && !kid->op_sibling) { /* print HANDLE; */
5917 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
5918 o->op_flags |= OPf_STACKED; /* make it a filehandle */
5919 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
5920 cLISTOPo->op_first->op_sibling = kid;
5921 cLISTOPo->op_last = kid;
5922 kid = kid->op_sibling;
5927 append_elem(o->op_type, o, newDEFSVOP());
5933 if (PL_hints & HINT_LOCALE)
5934 o->op_private |= OPpLOCALE;
5941 Perl_ck_fun_locale(pTHX_ OP *o)
5947 if (PL_hints & HINT_LOCALE)
5948 o->op_private |= OPpLOCALE;
5955 Perl_ck_sassign(pTHX_ OP *o)
5957 OP *kid = cLISTOPo->op_first;
5958 /* has a disposable target? */
5959 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
5960 && !(kid->op_flags & OPf_STACKED)
5961 /* Cannot steal the second time! */
5962 && !(kid->op_private & OPpTARGET_MY))
5964 OP *kkid = kid->op_sibling;
5966 /* Can just relocate the target. */
5967 if (kkid && kkid->op_type == OP_PADSV
5968 && !(kkid->op_private & OPpLVAL_INTRO))
5970 kid->op_targ = kkid->op_targ;
5972 /* Now we do not need PADSV and SASSIGN. */
5973 kid->op_sibling = o->op_sibling; /* NULL */
5974 cLISTOPo->op_first = NULL;
5977 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
5985 Perl_ck_scmp(pTHX_ OP *o)
5989 if (PL_hints & HINT_LOCALE)
5990 o->op_private |= OPpLOCALE;
5997 Perl_ck_match(pTHX_ OP *o)
5999 o->op_private |= OPpRUNTIME;
6004 Perl_ck_method(pTHX_ OP *o)
6006 OP *kid = cUNOPo->op_first;
6007 if (kid->op_type == OP_CONST) {
6008 SV* sv = kSVOP->op_sv;
6009 if (!(strchr(SvPVX(sv), ':') || strchr(SvPVX(sv), '\''))) {
6011 if (!SvREADONLY(sv) || !SvFAKE(sv)) {
6012 sv = newSVpvn_share(SvPVX(sv), SvCUR(sv), 0);
6015 kSVOP->op_sv = Nullsv;
6017 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
6026 Perl_ck_null(pTHX_ OP *o)
6032 Perl_ck_open(pTHX_ OP *o)
6034 HV *table = GvHV(PL_hintgv);
6038 svp = hv_fetch(table, "open_IN", 7, FALSE);
6040 mode = mode_from_discipline(*svp);
6041 if (mode & O_BINARY)
6042 o->op_private |= OPpOPEN_IN_RAW;
6043 else if (mode & O_TEXT)
6044 o->op_private |= OPpOPEN_IN_CRLF;
6047 svp = hv_fetch(table, "open_OUT", 8, FALSE);
6049 mode = mode_from_discipline(*svp);
6050 if (mode & O_BINARY)
6051 o->op_private |= OPpOPEN_OUT_RAW;
6052 else if (mode & O_TEXT)
6053 o->op_private |= OPpOPEN_OUT_CRLF;
6056 if (o->op_type == OP_BACKTICK)
6062 Perl_ck_repeat(pTHX_ OP *o)
6064 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
6065 o->op_private |= OPpREPEAT_DOLIST;
6066 cBINOPo->op_first = force_list(cBINOPo->op_first);
6074 Perl_ck_require(pTHX_ OP *o)
6076 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
6077 SVOP *kid = (SVOP*)cUNOPo->op_first;
6079 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
6081 for (s = SvPVX(kid->op_sv); *s; s++) {
6082 if (*s == ':' && s[1] == ':') {
6084 Move(s+2, s+1, strlen(s+2)+1, char);
6085 --SvCUR(kid->op_sv);
6088 if (SvREADONLY(kid->op_sv)) {
6089 SvREADONLY_off(kid->op_sv);
6090 sv_catpvn(kid->op_sv, ".pm", 3);
6091 SvREADONLY_on(kid->op_sv);
6094 sv_catpvn(kid->op_sv, ".pm", 3);
6101 Perl_ck_return(pTHX_ OP *o)
6104 if (CvLVALUE(PL_compcv)) {
6105 for (kid = cLISTOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
6106 mod(kid, OP_LEAVESUBLV);
6113 Perl_ck_retarget(pTHX_ OP *o)
6115 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
6122 Perl_ck_select(pTHX_ OP *o)
6125 if (o->op_flags & OPf_KIDS) {
6126 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6127 if (kid && kid->op_sibling) {
6128 o->op_type = OP_SSELECT;
6129 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
6131 return fold_constants(o);
6135 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6136 if (kid && kid->op_type == OP_RV2GV)
6137 kid->op_private &= ~HINT_STRICT_REFS;
6142 Perl_ck_shift(pTHX_ OP *o)
6144 I32 type = o->op_type;
6146 if (!(o->op_flags & OPf_KIDS)) {
6151 if (!CvUNIQUE(PL_compcv)) {
6152 argop = newOP(OP_PADAV, OPf_REF);
6153 argop->op_targ = 0; /* PL_curpad[0] is @_ */
6156 argop = newUNOP(OP_RV2AV, 0,
6157 scalar(newGVOP(OP_GV, 0,
6158 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
6161 argop = newUNOP(OP_RV2AV, 0,
6162 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(PL_compcv) ?
6163 PL_defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
6164 #endif /* USE_THREADS */
6165 return newUNOP(type, 0, scalar(argop));
6167 return scalar(modkids(ck_fun(o), type));
6171 Perl_ck_sort(pTHX_ OP *o)
6176 if (PL_hints & HINT_LOCALE)
6177 o->op_private |= OPpLOCALE;
6180 if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
6182 firstkid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6183 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
6185 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */
6187 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
6189 if (kid->op_type == OP_SCOPE) {
6193 else if (kid->op_type == OP_LEAVE) {
6194 if (o->op_type == OP_SORT) {
6195 null(kid); /* wipe out leave */
6198 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
6199 if (k->op_next == kid)
6201 /* don't descend into loops */
6202 else if (k->op_type == OP_ENTERLOOP
6203 || k->op_type == OP_ENTERITER)
6205 k = cLOOPx(k)->op_lastop;
6210 kid->op_next = 0; /* just disconnect the leave */
6211 k = kLISTOP->op_first;
6216 if (o->op_type == OP_SORT) {
6217 /* provide scalar context for comparison function/block */
6223 o->op_flags |= OPf_SPECIAL;
6225 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
6228 firstkid = firstkid->op_sibling;
6231 /* provide list context for arguments */
6232 if (o->op_type == OP_SORT)
6239 S_simplify_sort(pTHX_ OP *o)
6241 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6245 if (!(o->op_flags & OPf_STACKED))
6247 GvMULTI_on(gv_fetchpv("a", TRUE, SVt_PV));
6248 GvMULTI_on(gv_fetchpv("b", TRUE, SVt_PV));
6249 kid = kUNOP->op_first; /* get past null */
6250 if (kid->op_type != OP_SCOPE)
6252 kid = kLISTOP->op_last; /* get past scope */
6253 switch(kid->op_type) {
6261 k = kid; /* remember this node*/
6262 if (kBINOP->op_first->op_type != OP_RV2SV)
6264 kid = kBINOP->op_first; /* get past cmp */
6265 if (kUNOP->op_first->op_type != OP_GV)
6267 kid = kUNOP->op_first; /* get past rv2sv */
6269 if (GvSTASH(gv) != PL_curstash)
6271 if (strEQ(GvNAME(gv), "a"))
6273 else if (strEQ(GvNAME(gv), "b"))
6277 kid = k; /* back to cmp */
6278 if (kBINOP->op_last->op_type != OP_RV2SV)
6280 kid = kBINOP->op_last; /* down to 2nd arg */
6281 if (kUNOP->op_first->op_type != OP_GV)
6283 kid = kUNOP->op_first; /* get past rv2sv */
6285 if (GvSTASH(gv) != PL_curstash
6287 ? strNE(GvNAME(gv), "a")
6288 : strNE(GvNAME(gv), "b")))
6290 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
6292 o->op_private |= OPpSORT_REVERSE;
6293 if (k->op_type == OP_NCMP)
6294 o->op_private |= OPpSORT_NUMERIC;
6295 if (k->op_type == OP_I_NCMP)
6296 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
6297 kid = cLISTOPo->op_first->op_sibling;
6298 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
6299 op_free(kid); /* then delete it */
6303 Perl_ck_split(pTHX_ OP *o)
6307 if (o->op_flags & OPf_STACKED)
6308 return no_fh_allowed(o);
6310 kid = cLISTOPo->op_first;
6311 if (kid->op_type != OP_NULL)
6312 Perl_croak(aTHX_ "panic: ck_split");
6313 kid = kid->op_sibling;
6314 op_free(cLISTOPo->op_first);
6315 cLISTOPo->op_first = kid;
6317 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvn(" ", 1));
6318 cLISTOPo->op_last = kid; /* There was only one element previously */
6321 if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) {
6322 OP *sibl = kid->op_sibling;
6323 kid->op_sibling = 0;
6324 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
6325 if (cLISTOPo->op_first == cLISTOPo->op_last)
6326 cLISTOPo->op_last = kid;
6327 cLISTOPo->op_first = kid;
6328 kid->op_sibling = sibl;
6331 kid->op_type = OP_PUSHRE;
6332 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
6335 if (!kid->op_sibling)
6336 append_elem(OP_SPLIT, o, newDEFSVOP());
6338 kid = kid->op_sibling;
6341 if (!kid->op_sibling)
6342 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
6344 kid = kid->op_sibling;
6347 if (kid->op_sibling)
6348 return too_many_arguments(o,PL_op_desc[o->op_type]);
6354 Perl_ck_join(pTHX_ OP *o)
6356 if (ckWARN(WARN_SYNTAX)) {
6357 OP *kid = cLISTOPo->op_first->op_sibling;
6358 if (kid && kid->op_type == OP_MATCH) {
6359 char *pmstr = "STRING";
6360 if (kPMOP->op_pmregexp)
6361 pmstr = kPMOP->op_pmregexp->precomp;
6362 Perl_warner(aTHX_ WARN_SYNTAX,
6363 "/%s/ should probably be written as \"%s\"",
6371 Perl_ck_subr(pTHX_ OP *o)
6373 OP *prev = ((cUNOPo->op_first->op_sibling)
6374 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
6375 OP *o2 = prev->op_sibling;
6384 o->op_private |= OPpENTERSUB_HASTARG;
6385 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
6386 if (cvop->op_type == OP_RV2CV) {
6388 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
6389 null(cvop); /* disable rv2cv */
6390 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
6391 if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) {
6392 GV *gv = cGVOPx_gv(tmpop);
6395 tmpop->op_private |= OPpEARLY_CV;
6396 else if (SvPOK(cv)) {
6397 namegv = CvANON(cv) ? gv : CvGV(cv);
6398 proto = SvPV((SV*)cv, n_a);
6402 else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
6403 if (o2->op_type == OP_CONST)
6404 o2->op_private &= ~OPpCONST_STRICT;
6405 else if (o2->op_type == OP_LIST) {
6406 OP *o = ((UNOP*)o2)->op_first->op_sibling;
6407 if (o && o->op_type == OP_CONST)
6408 o->op_private &= ~OPpCONST_STRICT;
6411 o->op_private |= (PL_hints & HINT_STRICT_REFS);
6412 if (PERLDB_SUB && PL_curstash != PL_debstash)
6413 o->op_private |= OPpENTERSUB_DB;
6414 while (o2 != cvop) {
6418 return too_many_arguments(o, gv_ename(namegv));
6436 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
6438 arg == 1 ? "block or sub {}" : "sub {}",
6439 gv_ename(namegv), o2);
6442 /* '*' allows any scalar type, including bareword */
6445 if (o2->op_type == OP_RV2GV)
6446 goto wrapref; /* autoconvert GLOB -> GLOBref */
6447 else if (o2->op_type == OP_CONST)
6448 o2->op_private &= ~OPpCONST_STRICT;
6449 else if (o2->op_type == OP_ENTERSUB) {
6450 /* accidental subroutine, revert to bareword */
6451 OP *gvop = ((UNOP*)o2)->op_first;
6452 if (gvop && gvop->op_type == OP_NULL) {
6453 gvop = ((UNOP*)gvop)->op_first;
6455 for (; gvop->op_sibling; gvop = gvop->op_sibling)
6458 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
6459 (gvop = ((UNOP*)gvop)->op_first) &&
6460 gvop->op_type == OP_GV)
6462 GV *gv = cGVOPx_gv(gvop);
6463 OP *sibling = o2->op_sibling;
6464 SV *n = newSVpvn("",0);
6466 gv_fullname3(n, gv, "");
6467 if (SvCUR(n)>6 && strnEQ(SvPVX(n),"main::",6))
6468 sv_chop(n, SvPVX(n)+6);
6469 o2 = newSVOP(OP_CONST, 0, n);
6470 prev->op_sibling = o2;
6471 o2->op_sibling = sibling;
6483 if (o2->op_type != OP_RV2GV)
6484 bad_type(arg, "symbol", gv_ename(namegv), o2);
6487 if (o2->op_type != OP_ENTERSUB)
6488 bad_type(arg, "subroutine entry", gv_ename(namegv), o2);
6491 if (o2->op_type != OP_RV2SV
6492 && o2->op_type != OP_PADSV
6493 && o2->op_type != OP_HELEM
6494 && o2->op_type != OP_AELEM
6495 && o2->op_type != OP_THREADSV)
6497 bad_type(arg, "scalar", gv_ename(namegv), o2);
6501 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
6502 bad_type(arg, "array", gv_ename(namegv), o2);
6505 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
6506 bad_type(arg, "hash", gv_ename(namegv), o2);
6510 OP* sib = kid->op_sibling;
6511 kid->op_sibling = 0;
6512 o2 = newUNOP(OP_REFGEN, 0, kid);
6513 o2->op_sibling = sib;
6514 prev->op_sibling = o2;
6525 Perl_croak(aTHX_ "Malformed prototype for %s: %s",
6526 gv_ename(namegv), SvPV((SV*)cv, n_a));
6531 mod(o2, OP_ENTERSUB);
6533 o2 = o2->op_sibling;
6535 if (proto && !optional &&
6536 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
6537 return too_few_arguments(o, gv_ename(namegv));
6542 Perl_ck_svconst(pTHX_ OP *o)
6544 SvREADONLY_on(cSVOPo->op_sv);
6549 Perl_ck_trunc(pTHX_ OP *o)
6551 if (o->op_flags & OPf_KIDS) {
6552 SVOP *kid = (SVOP*)cUNOPo->op_first;
6554 if (kid->op_type == OP_NULL)
6555 kid = (SVOP*)kid->op_sibling;
6556 if (kid && kid->op_type == OP_CONST &&
6557 (kid->op_private & OPpCONST_BARE))
6559 o->op_flags |= OPf_SPECIAL;
6560 kid->op_private &= ~OPpCONST_STRICT;
6567 Perl_ck_substr(pTHX_ OP *o)
6570 if ((o->op_flags & OPf_KIDS) && o->op_private == 4) {
6571 OP *kid = cLISTOPo->op_first;
6573 if (kid->op_type == OP_NULL)
6574 kid = kid->op_sibling;
6576 kid->op_flags |= OPf_MOD;
6582 /* A peephole optimizer. We visit the ops in the order they're to execute. */
6585 Perl_peep(pTHX_ register OP *o)
6587 register OP* oldop = 0;
6590 if (!o || o->op_seq)
6594 SAVEVPTR(PL_curcop);
6595 for (; o; o = o->op_next) {
6601 switch (o->op_type) {
6605 PL_curcop = ((COP*)o); /* for warnings */
6606 o->op_seq = PL_op_seqmax++;
6610 if (cSVOPo->op_private & OPpCONST_STRICT)
6611 no_bareword_allowed(o);
6613 /* Relocate sv to the pad for thread safety.
6614 * Despite being a "constant", the SV is written to,
6615 * for reference counts, sv_upgrade() etc. */
6617 PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP);
6618 if (SvPADTMP(cSVOPo->op_sv)) {
6619 /* If op_sv is already a PADTMP then it is being used by
6620 * some pad, so make a copy. */
6621 sv_setsv(PL_curpad[ix],cSVOPo->op_sv);
6622 SvREADONLY_on(PL_curpad[ix]);
6623 SvREFCNT_dec(cSVOPo->op_sv);
6626 SvREFCNT_dec(PL_curpad[ix]);
6627 SvPADTMP_on(cSVOPo->op_sv);
6628 PL_curpad[ix] = cSVOPo->op_sv;
6629 /* XXX I don't know how this isn't readonly already. */
6630 SvREADONLY_on(PL_curpad[ix]);
6632 cSVOPo->op_sv = Nullsv;
6636 o->op_seq = PL_op_seqmax++;
6640 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
6641 if (o->op_next->op_private & OPpTARGET_MY) {
6642 if (o->op_flags & OPf_STACKED) /* chained concats */
6643 goto ignore_optimization;
6645 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
6646 o->op_targ = o->op_next->op_targ;
6647 o->op_next->op_targ = 0;
6648 o->op_private |= OPpTARGET_MY;
6653 ignore_optimization:
6654 o->op_seq = PL_op_seqmax++;
6657 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
6658 o->op_seq = PL_op_seqmax++;
6659 break; /* Scalar stub must produce undef. List stub is noop */
6663 if (o->op_targ == OP_NEXTSTATE
6664 || o->op_targ == OP_DBSTATE
6665 || o->op_targ == OP_SETSTATE)
6667 PL_curcop = ((COP*)o);
6674 if (oldop && o->op_next) {
6675 oldop->op_next = o->op_next;
6678 o->op_seq = PL_op_seqmax++;
6682 if (o->op_next->op_type == OP_RV2SV) {
6683 if (!(o->op_next->op_private & OPpDEREF)) {
6685 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
6687 o->op_next = o->op_next->op_next;
6688 o->op_type = OP_GVSV;
6689 o->op_ppaddr = PL_ppaddr[OP_GVSV];
6692 else if (o->op_next->op_type == OP_RV2AV) {
6693 OP* pop = o->op_next->op_next;
6695 if (pop->op_type == OP_CONST &&
6696 (PL_op = pop->op_next) &&
6697 pop->op_next->op_type == OP_AELEM &&
6698 !(pop->op_next->op_private &
6699 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) &&
6700 (i = SvIV(((SVOP*)pop)->op_sv) - PL_compiling.cop_arybase)
6708 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
6709 o->op_next = pop->op_next->op_next;
6710 o->op_type = OP_AELEMFAST;
6711 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
6712 o->op_private = (U8)i;
6717 else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
6719 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX(GvCV(gv))) {
6720 /* XXX could check prototype here instead of just carping */
6721 SV *sv = sv_newmortal();
6722 gv_efullname3(sv, gv, Nullch);
6723 Perl_warner(aTHX_ WARN_PROTOTYPE,
6724 "%s() called too early to check prototype",
6729 o->op_seq = PL_op_seqmax++;
6740 o->op_seq = PL_op_seqmax++;
6741 while (cLOGOP->op_other->op_type == OP_NULL)
6742 cLOGOP->op_other = cLOGOP->op_other->op_next;
6743 peep(cLOGOP->op_other);
6747 o->op_seq = PL_op_seqmax++;
6748 while (cLOOP->op_redoop->op_type == OP_NULL)
6749 cLOOP->op_redoop = cLOOP->op_redoop->op_next;
6750 peep(cLOOP->op_redoop);
6751 while (cLOOP->op_nextop->op_type == OP_NULL)
6752 cLOOP->op_nextop = cLOOP->op_nextop->op_next;
6753 peep(cLOOP->op_nextop);
6754 while (cLOOP->op_lastop->op_type == OP_NULL)
6755 cLOOP->op_lastop = cLOOP->op_lastop->op_next;
6756 peep(cLOOP->op_lastop);
6762 o->op_seq = PL_op_seqmax++;
6763 while (cPMOP->op_pmreplstart &&
6764 cPMOP->op_pmreplstart->op_type == OP_NULL)
6765 cPMOP->op_pmreplstart = cPMOP->op_pmreplstart->op_next;
6766 peep(cPMOP->op_pmreplstart);
6770 o->op_seq = PL_op_seqmax++;
6771 if (ckWARN(WARN_SYNTAX) && o->op_next
6772 && o->op_next->op_type == OP_NEXTSTATE) {
6773 if (o->op_next->op_sibling &&
6774 o->op_next->op_sibling->op_type != OP_EXIT &&
6775 o->op_next->op_sibling->op_type != OP_WARN &&
6776 o->op_next->op_sibling->op_type != OP_DIE) {
6777 line_t oldline = CopLINE(PL_curcop);
6779 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_next));
6780 Perl_warner(aTHX_ WARN_EXEC,
6781 "Statement unlikely to be reached");
6782 Perl_warner(aTHX_ WARN_EXEC,
6783 "\t(Maybe you meant system() when you said exec()?)\n");
6784 CopLINE_set(PL_curcop, oldline);
6793 SV **svp, **indsvp, *sv;
6798 o->op_seq = PL_op_seqmax++;
6800 if (((BINOP*)o)->op_last->op_type != OP_CONST)
6803 /* Make the CONST have a shared SV */
6804 svp = cSVOPx_svp(((BINOP*)o)->op_last);
6805 if ((!SvFAKE(sv = *svp) || !SvREADONLY(sv)) && !IS_PADCONST(sv)) {
6806 key = SvPV(sv, keylen);
6809 lexname = newSVpvn_share(key, keylen, 0);
6814 if ((o->op_private & (OPpLVAL_INTRO)))
6817 rop = (UNOP*)((BINOP*)o)->op_first;
6818 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6820 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6821 if (!SvOBJECT(lexname))
6823 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6824 if (!fields || !GvHV(*fields))
6826 key = SvPV(*svp, keylen);
6829 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
6831 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" in variable %s of type %s",
6832 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
6834 ind = SvIV(*indsvp);
6836 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6837 rop->op_type = OP_RV2AV;
6838 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6839 o->op_type = OP_AELEM;
6840 o->op_ppaddr = PL_ppaddr[OP_AELEM];
6842 if (SvREADONLY(*svp))
6844 SvFLAGS(sv) |= (SvFLAGS(*svp)
6845 & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
6855 SV **svp, **indsvp, *sv;
6859 SVOP *first_key_op, *key_op;
6861 o->op_seq = PL_op_seqmax++;
6862 if ((o->op_private & (OPpLVAL_INTRO))
6863 /* I bet there's always a pushmark... */
6864 || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST)
6865 /* hmmm, no optimization if list contains only one key. */
6867 rop = (UNOP*)((LISTOP*)o)->op_last;
6868 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6870 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6871 if (!SvOBJECT(lexname))
6873 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6874 if (!fields || !GvHV(*fields))
6876 /* Again guessing that the pushmark can be jumped over.... */
6877 first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling)
6878 ->op_first->op_sibling;
6879 /* Check that the key list contains only constants. */
6880 for (key_op = first_key_op; key_op;
6881 key_op = (SVOP*)key_op->op_sibling)
6882 if (key_op->op_type != OP_CONST)
6886 rop->op_type = OP_RV2AV;
6887 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6888 o->op_type = OP_ASLICE;
6889 o->op_ppaddr = PL_ppaddr[OP_ASLICE];
6890 for (key_op = first_key_op; key_op;
6891 key_op = (SVOP*)key_op->op_sibling) {
6892 svp = cSVOPx_svp(key_op);
6893 key = SvPV(*svp, keylen);
6896 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
6898 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" "
6899 "in variable %s of type %s",
6900 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
6902 ind = SvIV(*indsvp);
6904 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6906 if (SvREADONLY(*svp))
6908 SvFLAGS(sv) |= (SvFLAGS(*svp)
6909 & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
6917 o->op_seq = PL_op_seqmax++;
6927 /* Efficient sub that returns a constant scalar value. */
6929 const_sv_xsub(pTHXo_ CV* cv)
6933 ST(0) = (SV*)XSANY.any_ptr;