3 * Copyright (c) 1991-2000, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
12 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
13 * youngest of the Old Took's daughters); and Mr. Drogo was his second
14 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
15 * either way, as the saying is, if you follow me." --the Gaffer
23 /* #define PL_OP_SLAB_ALLOC */
25 #ifdef PL_OP_SLAB_ALLOC
26 #define SLAB_SIZE 8192
27 static char *PL_OpPtr = NULL;
28 static int PL_OpSpace = 0;
29 #define NewOp(m,var,c,type) do { if ((PL_OpSpace -= c*sizeof(type)) >= 0) \
30 var = (type *)(PL_OpPtr -= c*sizeof(type)); \
32 var = (type *) Slab_Alloc(m,c*sizeof(type)); \
36 S_Slab_Alloc(pTHX_ int m, size_t sz)
38 Newz(m,PL_OpPtr,SLAB_SIZE,char);
39 PL_OpSpace = SLAB_SIZE - sz;
40 return PL_OpPtr += PL_OpSpace;
44 #define NewOp(m, var, c, type) Newz(m, var, c, type)
47 * In the following definition, the ", Nullop" is just to make the compiler
48 * think the expression is of the right type: croak actually does a Siglongjmp.
50 #define CHECKOP(type,o) \
51 ((PL_op_mask && PL_op_mask[type]) \
52 ? ( op_free((OP*)o), \
53 Perl_croak(aTHX_ "%s trapped by operation mask", PL_op_desc[type]), \
55 : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o))
57 #define PAD_MAX 999999999
60 S_gv_ename(pTHX_ GV *gv)
63 SV* tmpsv = sv_newmortal();
64 gv_efullname3(tmpsv, gv, Nullch);
65 return SvPV(tmpsv,n_a);
69 S_no_fh_allowed(pTHX_ OP *o)
71 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
72 PL_op_desc[o->op_type]));
77 S_too_few_arguments(pTHX_ OP *o, char *name)
79 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
84 S_too_many_arguments(pTHX_ OP *o, char *name)
86 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
91 S_bad_type(pTHX_ I32 n, char *t, char *name, OP *kid)
93 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
94 (int)n, name, t, PL_op_desc[kid->op_type]));
98 S_no_bareword_allowed(pTHX_ OP *o)
100 qerror(Perl_mess(aTHX_
101 "Bareword \"%s\" not allowed while \"strict subs\" in use",
102 SvPV_nolen(cSVOPo_sv)));
105 /* "register" allocation */
108 Perl_pad_allocmy(pTHX_ char *name)
114 if (!(PL_in_my == KEY_our ||
116 (PL_hints & HINT_UTF8 && (name[1] & 0xc0) == 0xc0) ||
117 (name[1] == '_' && (int)strlen(name) > 2)))
119 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
120 /* 1999-02-27 mjd@plover.com */
122 p = strchr(name, '\0');
123 /* The next block assumes the buffer is at least 205 chars
124 long. At present, it's always at least 256 chars. */
126 strcpy(name+200, "...");
132 /* Move everything else down one character */
133 for (; p-name > 2; p--)
135 name[2] = toCTRL(name[1]);
138 yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
140 if (ckWARN(WARN_MISC) && AvFILLp(PL_comppad_name) >= 0) {
141 SV **svp = AvARRAY(PL_comppad_name);
142 HV *ourstash = (PL_curstash ? PL_curstash : PL_defstash);
143 PADOFFSET top = AvFILLp(PL_comppad_name);
144 for (off = top; off > PL_comppad_name_floor; off--) {
146 && sv != &PL_sv_undef
147 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
148 && (PL_in_my != KEY_our
149 || ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash))
150 && strEQ(name, SvPVX(sv)))
152 Perl_warner(aTHX_ WARN_MISC,
153 "\"%s\" variable %s masks earlier declaration in same %s",
154 (PL_in_my == KEY_our ? "our" : "my"),
156 (SvIVX(sv) == PAD_MAX ? "scope" : "statement"));
161 if (PL_in_my == KEY_our) {
164 && sv != &PL_sv_undef
165 && ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash)
166 && strEQ(name, SvPVX(sv)))
168 Perl_warner(aTHX_ WARN_MISC,
169 "\"our\" variable %s redeclared", name);
170 Perl_warner(aTHX_ WARN_MISC,
171 "\t(Did you mean \"local\" instead of \"our\"?)\n");
174 } while ( off-- > 0 );
177 off = pad_alloc(OP_PADSV, SVs_PADMY);
179 sv_upgrade(sv, SVt_PVNV);
181 if (PL_in_my_stash) {
183 yyerror(Perl_form(aTHX_ "Can't declare class for non-scalar %s in \"%s\"",
184 name, PL_in_my == KEY_our ? "our" : "my"));
186 (void)SvUPGRADE(sv, SVt_PVMG);
187 SvSTASH(sv) = (HV*)SvREFCNT_inc(PL_in_my_stash);
190 if (PL_in_my == KEY_our) {
191 (void)SvUPGRADE(sv, SVt_PVGV);
192 GvSTASH(sv) = (HV*)SvREFCNT_inc(PL_curstash ? (SV*)PL_curstash : (SV*)PL_defstash);
193 SvFLAGS(sv) |= SVpad_OUR;
195 av_store(PL_comppad_name, off, sv);
196 SvNVX(sv) = (NV)PAD_MAX;
197 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
198 if (!PL_min_intro_pending)
199 PL_min_intro_pending = off;
200 PL_max_intro_pending = off;
202 av_store(PL_comppad, off, (SV*)newAV());
203 else if (*name == '%')
204 av_store(PL_comppad, off, (SV*)newHV());
205 SvPADMY_on(PL_curpad[off]);
210 S_pad_addlex(pTHX_ SV *proto_namesv)
212 SV *namesv = NEWSV(1103,0);
213 PADOFFSET newoff = pad_alloc(OP_PADSV, SVs_PADMY);
214 sv_upgrade(namesv, SVt_PVNV);
215 sv_setpv(namesv, SvPVX(proto_namesv));
216 av_store(PL_comppad_name, newoff, namesv);
217 SvNVX(namesv) = (NV)PL_curcop->cop_seq;
218 SvIVX(namesv) = PAD_MAX; /* A ref, intro immediately */
219 SvFAKE_on(namesv); /* A ref, not a real var */
220 if (SvFLAGS(proto_namesv) & SVpad_OUR) { /* An "our" variable */
221 SvFLAGS(namesv) |= SVpad_OUR;
222 (void)SvUPGRADE(namesv, SVt_PVGV);
223 GvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)GvSTASH(proto_namesv));
225 if (SvOBJECT(proto_namesv)) { /* A typed var */
227 (void)SvUPGRADE(namesv, SVt_PVMG);
228 SvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)SvSTASH(proto_namesv));
234 #define FINDLEX_NOSEARCH 1 /* don't search outer contexts */
237 S_pad_findlex(pTHX_ char *name, PADOFFSET newoff, U32 seq, CV* startcv,
238 I32 cx_ix, I32 saweval, U32 flags)
245 register PERL_CONTEXT *cx;
247 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
248 AV *curlist = CvPADLIST(cv);
249 SV **svp = av_fetch(curlist, 0, FALSE);
252 if (!svp || *svp == &PL_sv_undef)
255 svp = AvARRAY(curname);
256 for (off = AvFILLp(curname); off > 0; off--) {
257 if ((sv = svp[off]) &&
258 sv != &PL_sv_undef &&
260 seq > I_32(SvNVX(sv)) &&
261 strEQ(SvPVX(sv), name))
272 return 0; /* don't clone from inactive stack frame */
276 oldpad = (AV*)AvARRAY(curlist)[depth];
277 oldsv = *av_fetch(oldpad, off, TRUE);
278 if (!newoff) { /* Not a mere clone operation. */
279 newoff = pad_addlex(sv);
280 if (CvANON(PL_compcv) || SvTYPE(PL_compcv) == SVt_PVFM) {
281 /* "It's closures all the way down." */
282 CvCLONE_on(PL_compcv);
284 if (CvANON(PL_compcv))
285 oldsv = Nullsv; /* no need to keep ref */
290 bcv && bcv != cv && !CvCLONE(bcv);
291 bcv = CvOUTSIDE(bcv))
294 /* install the missing pad entry in intervening
295 * nested subs and mark them cloneable.
296 * XXX fix pad_foo() to not use globals */
297 AV *ocomppad_name = PL_comppad_name;
298 AV *ocomppad = PL_comppad;
299 SV **ocurpad = PL_curpad;
300 AV *padlist = CvPADLIST(bcv);
301 PL_comppad_name = (AV*)AvARRAY(padlist)[0];
302 PL_comppad = (AV*)AvARRAY(padlist)[1];
303 PL_curpad = AvARRAY(PL_comppad);
305 PL_comppad_name = ocomppad_name;
306 PL_comppad = ocomppad;
311 if (ckWARN(WARN_CLOSURE)
312 && !CvUNIQUE(bcv) && !CvUNIQUE(cv))
314 Perl_warner(aTHX_ WARN_CLOSURE,
315 "Variable \"%s\" may be unavailable",
323 else if (!CvUNIQUE(PL_compcv)) {
324 if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv))
325 Perl_warner(aTHX_ WARN_CLOSURE,
326 "Variable \"%s\" will not stay shared", name);
329 av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv));
335 if (flags & FINDLEX_NOSEARCH)
338 /* Nothing in current lexical context--try eval's context, if any.
339 * This is necessary to let the perldb get at lexically scoped variables.
340 * XXX This will also probably interact badly with eval tree caching.
343 for (i = cx_ix; i >= 0; i--) {
345 switch (CxTYPE(cx)) {
347 if (i == 0 && saweval) {
348 seq = cxstack[saweval].blk_oldcop->cop_seq;
349 return pad_findlex(name, newoff, seq, PL_main_cv, -1, saweval, 0);
353 switch (cx->blk_eval.old_op_type) {
360 /* require/do must have their own scope */
369 if (PL_debstash && CvSTASH(cv) == PL_debstash) { /* ignore DB'* scope */
370 saweval = i; /* so we know where we were called from */
373 seq = cxstack[saweval].blk_oldcop->cop_seq;
374 return pad_findlex(name, newoff, seq, cv, i-1, saweval,FINDLEX_NOSEARCH);
382 Perl_pad_findmy(pTHX_ char *name)
388 SV **svp = AvARRAY(PL_comppad_name);
389 U32 seq = PL_cop_seqmax;
395 * Special case to get lexical (and hence per-thread) @_.
396 * XXX I need to find out how to tell at parse-time whether use
397 * of @_ should refer to a lexical (from a sub) or defgv (global
398 * scope and maybe weird sub-ish things like formats). See
399 * startsub in perly.y. It's possible that @_ could be lexical
400 * (at least from subs) even in non-threaded perl.
402 if (strEQ(name, "@_"))
403 return 0; /* success. (NOT_IN_PAD indicates failure) */
404 #endif /* USE_THREADS */
406 /* The one we're looking for is probably just before comppad_name_fill. */
407 for (off = AvFILLp(PL_comppad_name); off > 0; off--) {
408 if ((sv = svp[off]) &&
409 sv != &PL_sv_undef &&
412 seq > I_32(SvNVX(sv)))) &&
413 strEQ(SvPVX(sv), name))
415 if (SvIVX(sv) || SvFLAGS(sv) & SVpad_OUR)
416 return (PADOFFSET)off;
417 pendoff = off; /* this pending def. will override import */
421 outside = CvOUTSIDE(PL_compcv);
423 /* Check if if we're compiling an eval'', and adjust seq to be the
424 * eval's seq number. This depends on eval'' having a non-null
425 * CvOUTSIDE() while it is being compiled. The eval'' itself is
426 * identified by CvEVAL being true and CvGV being null. */
427 if (outside && CvEVAL(PL_compcv) && !CvGV(PL_compcv) && cxstack_ix >= 0) {
428 cx = &cxstack[cxstack_ix];
430 seq = cx->blk_oldcop->cop_seq;
433 /* See if it's in a nested scope */
434 off = pad_findlex(name, 0, seq, outside, cxstack_ix, 0, 0);
436 /* If there is a pending local definition, this new alias must die */
438 SvIVX(AvARRAY(PL_comppad_name)[off]) = seq;
439 return off; /* pad_findlex returns 0 for failure...*/
441 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
445 Perl_pad_leavemy(pTHX_ I32 fill)
449 SV **svp = AvARRAY(PL_comppad_name);
451 if (PL_min_intro_pending && fill < PL_min_intro_pending) {
452 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
453 if ((sv = svp[off]) && sv != &PL_sv_undef && ckWARN_d(WARN_INTERNAL))
454 Perl_warner(aTHX_ WARN_INTERNAL, "%s never introduced", SvPVX(sv));
457 /* "Deintroduce" my variables that are leaving with this scope. */
458 for (off = AvFILLp(PL_comppad_name); off > fill; off--) {
459 if ((sv = svp[off]) && sv != &PL_sv_undef && SvIVX(sv) == PAD_MAX)
460 SvIVX(sv) = PL_cop_seqmax;
465 Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype)
471 if (AvARRAY(PL_comppad) != PL_curpad)
472 Perl_croak(aTHX_ "panic: pad_alloc");
473 if (PL_pad_reset_pending)
475 if (tmptype & SVs_PADMY) {
477 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
478 } while (SvPADBUSY(sv)); /* need a fresh one */
479 retval = AvFILLp(PL_comppad);
482 SV **names = AvARRAY(PL_comppad_name);
483 SSize_t names_fill = AvFILLp(PL_comppad_name);
486 * "foreach" index vars temporarily become aliases to non-"my"
487 * values. Thus we must skip, not just pad values that are
488 * marked as current pad values, but also those with names.
490 if (++PL_padix <= names_fill &&
491 (sv = names[PL_padix]) && sv != &PL_sv_undef)
493 sv = *av_fetch(PL_comppad, PL_padix, TRUE);
494 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)) && !IS_PADGV(sv))
499 SvFLAGS(sv) |= tmptype;
500 PL_curpad = AvARRAY(PL_comppad);
502 DEBUG_X(PerlIO_printf(Perl_debug_log,
503 "0x%"UVxf" Pad 0x%"UVxf" alloc %ld for %s\n",
504 PTR2UV(thr), PTR2UV(PL_curpad),
505 (long) retval, PL_op_name[optype]));
507 DEBUG_X(PerlIO_printf(Perl_debug_log,
508 "Pad 0x%"UVxf" alloc %ld for %s\n",
510 (long) retval, PL_op_name[optype]));
511 #endif /* USE_THREADS */
512 return (PADOFFSET)retval;
516 Perl_pad_sv(pTHX_ PADOFFSET po)
520 DEBUG_X(PerlIO_printf(Perl_debug_log,
521 "0x%"UVxf" Pad 0x%"UVxf" sv %"IVdf"\n",
522 PTR2UV(thr), PTR2UV(PL_curpad), (IV)po));
525 Perl_croak(aTHX_ "panic: pad_sv po");
526 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" sv %"IVdf"\n",
527 PTR2UV(PL_curpad), (IV)po));
528 #endif /* USE_THREADS */
529 return PL_curpad[po]; /* eventually we'll turn this into a macro */
533 Perl_pad_free(pTHX_ PADOFFSET po)
538 if (AvARRAY(PL_comppad) != PL_curpad)
539 Perl_croak(aTHX_ "panic: pad_free curpad");
541 Perl_croak(aTHX_ "panic: pad_free po");
543 DEBUG_X(PerlIO_printf(Perl_debug_log,
544 "0x%"UVxf" Pad 0x%"UVxf" free %"IVdf"\n",
545 PTR2UV(thr), PTR2UV(PL_curpad), (IV)po));
547 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" free %"IVdf"\n",
548 PTR2UV(PL_curpad), (IV)po));
549 #endif /* USE_THREADS */
550 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef) {
551 SvPADTMP_off(PL_curpad[po]);
553 SvREADONLY_off(PL_curpad[po]); /* could be a freed constant */
556 if ((I32)po < PL_padix)
561 Perl_pad_swipe(pTHX_ PADOFFSET po)
564 if (AvARRAY(PL_comppad) != PL_curpad)
565 Perl_croak(aTHX_ "panic: pad_swipe curpad");
567 Perl_croak(aTHX_ "panic: pad_swipe po");
569 DEBUG_X(PerlIO_printf(Perl_debug_log,
570 "0x%"UVxf" Pad 0x%"UVxf" swipe %"IVdf"\n",
571 PTR2UV(thr), PTR2UV(PL_curpad), (IV)po));
573 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" swipe %"IVdf"\n",
574 PTR2UV(PL_curpad), (IV)po));
575 #endif /* USE_THREADS */
576 SvPADTMP_off(PL_curpad[po]);
577 PL_curpad[po] = NEWSV(1107,0);
578 SvPADTMP_on(PL_curpad[po]);
579 if ((I32)po < PL_padix)
583 /* XXX pad_reset() is currently disabled because it results in serious bugs.
584 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
585 * on the stack by OPs that use them, there are several ways to get an alias
586 * to a shared TARG. Such an alias will change randomly and unpredictably.
587 * We avoid doing this until we can think of a Better Way.
592 #ifdef USE_BROKEN_PAD_RESET
596 if (AvARRAY(PL_comppad) != PL_curpad)
597 Perl_croak(aTHX_ "panic: pad_reset curpad");
599 DEBUG_X(PerlIO_printf(Perl_debug_log,
600 "0x%"UVxf" Pad 0x%"UVxf" reset\n",
601 PTR2UV(thr), PTR2UV(PL_curpad)));
603 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" reset\n",
605 #endif /* USE_THREADS */
606 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
607 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
608 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
609 SvPADTMP_off(PL_curpad[po]);
611 PL_padix = PL_padix_floor;
614 PL_pad_reset_pending = FALSE;
618 /* find_threadsv is not reentrant */
620 Perl_find_threadsv(pTHX_ const char *name)
626 /* We currently only handle names of a single character */
627 p = strchr(PL_threadsv_names, *name);
630 key = p - PL_threadsv_names;
631 MUTEX_LOCK(&thr->mutex);
632 svp = av_fetch(thr->threadsv, key, FALSE);
634 MUTEX_UNLOCK(&thr->mutex);
636 SV *sv = NEWSV(0, 0);
637 av_store(thr->threadsv, key, sv);
638 thr->threadsvp = AvARRAY(thr->threadsv);
639 MUTEX_UNLOCK(&thr->mutex);
641 * Some magic variables used to be automagically initialised
642 * in gv_fetchpv. Those which are now per-thread magicals get
643 * initialised here instead.
649 sv_setpv(sv, "\034");
650 sv_magic(sv, 0, 0, name, 1);
655 PL_sawampersand = TRUE;
669 /* XXX %! tied to Errno.pm needs to be added here.
670 * See gv_fetchpv(). */
674 sv_magic(sv, 0, 0, name, 1);
676 DEBUG_S(PerlIO_printf(Perl_error_log,
677 "find_threadsv: new SV %p for $%s%c\n",
678 sv, (*name < 32) ? "^" : "",
679 (*name < 32) ? toCTRL(*name) : *name));
683 #endif /* USE_THREADS */
688 Perl_op_free(pTHX_ OP *o)
690 register OP *kid, *nextkid;
693 if (!o || o->op_seq == (U16)-1)
696 if (o->op_private & OPpREFCOUNTED) {
697 switch (o->op_type) {
705 if (OpREFCNT_dec(o)) {
716 if (o->op_flags & OPf_KIDS) {
717 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
718 nextkid = kid->op_sibling; /* Get before next freeing kid */
726 /* COP* is not cleared by op_clear() so that we may track line
727 * numbers etc even after null() */
728 if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
733 #ifdef PL_OP_SLAB_ALLOC
734 if ((char *) o == PL_OpPtr)
743 S_op_clear(pTHX_ OP *o)
745 switch (o->op_type) {
746 case OP_NULL: /* Was holding old type, if any. */
747 case OP_ENTEREVAL: /* Was holding hints. */
749 case OP_THREADSV: /* Was holding index into thr->threadsv AV. */
755 if (!(o->op_flags & OPf_SPECIAL))
758 #endif /* USE_THREADS */
760 if (!(o->op_flags & OPf_REF)
761 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
768 if (cPADOPo->op_padix > 0) {
771 pad_swipe(cPADOPo->op_padix);
772 /* No GvIN_PAD_off(gv) here, because other references may still
773 * exist on the pad */
776 cPADOPo->op_padix = 0;
779 SvREFCNT_dec(cSVOPo->op_sv);
780 cSVOPo->op_sv = Nullsv;
784 SvREFCNT_dec(cSVOPo->op_sv);
785 cSVOPo->op_sv = Nullsv;
791 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
795 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
796 SvREFCNT_dec(cSVOPo->op_sv);
797 cSVOPo->op_sv = Nullsv;
800 Safefree(cPVOPo->op_pv);
801 cPVOPo->op_pv = Nullch;
805 op_free(cPMOPo->op_pmreplroot);
809 if ((PADOFFSET)cPMOPo->op_pmreplroot) {
811 GV *gv = (GV*)PL_curpad[(PADOFFSET)cPMOPo->op_pmreplroot];
812 pad_swipe((PADOFFSET)cPMOPo->op_pmreplroot);
813 /* No GvIN_PAD_off(gv) here, because other references may still
814 * exist on the pad */
819 SvREFCNT_dec((SV*)cPMOPo->op_pmreplroot);
825 cPMOPo->op_pmreplroot = Nullop;
826 ReREFCNT_dec(cPMOPo->op_pmregexp);
827 cPMOPo->op_pmregexp = (REGEXP*)NULL;
831 if (o->op_targ > 0) {
832 pad_free(o->op_targ);
838 S_cop_free(pTHX_ COP* cop)
840 Safefree(cop->cop_label);
842 Safefree(CopFILE(cop)); /* XXXXX share in a pvtable? */
843 Safefree(CopSTASHPV(cop)); /* XXXXX share in a pvtable? */
845 /* NOTE: COP.cop_stash is not refcounted */
846 SvREFCNT_dec(CopFILEGV(cop));
848 if (! specialWARN(cop->cop_warnings))
849 SvREFCNT_dec(cop->cop_warnings);
855 if (o->op_type == OP_NULL)
858 o->op_targ = o->op_type;
859 o->op_type = OP_NULL;
860 o->op_ppaddr = PL_ppaddr[OP_NULL];
863 /* Contextualizers */
865 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
868 Perl_linklist(pTHX_ OP *o)
875 /* establish postfix order */
876 if (cUNOPo->op_first) {
877 o->op_next = LINKLIST(cUNOPo->op_first);
878 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
880 kid->op_next = LINKLIST(kid->op_sibling);
892 Perl_scalarkids(pTHX_ OP *o)
895 if (o && o->op_flags & OPf_KIDS) {
896 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
903 S_scalarboolean(pTHX_ OP *o)
905 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
907 if (ckWARN(WARN_SYNTAX)) {
908 line_t oldline = CopLINE(PL_curcop);
910 if (PL_copline != NOLINE)
911 CopLINE_set(PL_curcop, PL_copline);
912 Perl_warner(aTHX_ WARN_SYNTAX, "Found = in conditional, should be ==");
913 CopLINE_set(PL_curcop, oldline);
920 Perl_scalar(pTHX_ OP *o)
924 /* assumes no premature commitment */
925 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
926 || o->op_type == OP_RETURN)
931 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
933 switch (o->op_type) {
935 if (o->op_private & OPpREPEAT_DOLIST)
936 null(((LISTOP*)cBINOPo->op_first)->op_first);
937 scalar(cBINOPo->op_first);
942 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
946 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
947 if (!kPMOP->op_pmreplroot)
948 deprecate("implicit split to @_");
956 if (o->op_flags & OPf_KIDS) {
957 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
963 kid = cLISTOPo->op_first;
965 while ((kid = kid->op_sibling)) {
971 WITH_THR(PL_curcop = &PL_compiling);
976 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
982 WITH_THR(PL_curcop = &PL_compiling);
989 Perl_scalarvoid(pTHX_ OP *o)
996 if (o->op_type == OP_NEXTSTATE
997 || o->op_type == OP_SETSTATE
998 || o->op_type == OP_DBSTATE
999 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1000 || o->op_targ == OP_SETSTATE
1001 || o->op_targ == OP_DBSTATE)))
1004 PL_curcop = (COP*)o; /* for warning below */
1007 /* assumes no premature commitment */
1008 want = o->op_flags & OPf_WANT;
1009 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
1010 || o->op_type == OP_RETURN)
1015 if ((o->op_private & OPpTARGET_MY)
1016 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1018 return scalar(o); /* As if inside SASSIGN */
1021 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1023 switch (o->op_type) {
1025 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1029 if (o->op_flags & OPf_STACKED)
1033 if (o->op_private == 4)
1075 case OP_GETSOCKNAME:
1076 case OP_GETPEERNAME:
1081 case OP_GETPRIORITY:
1104 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1105 useless = PL_op_desc[o->op_type];
1112 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1113 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1114 useless = "a variable";
1119 if (cSVOPo->op_private & OPpCONST_STRICT)
1120 no_bareword_allowed(o);
1123 if (ckWARN(WARN_VOID)) {
1124 useless = "a constant";
1125 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1127 else if (SvPOK(sv)) {
1128 if (strnEQ(SvPVX(sv), "di", 2) ||
1129 strnEQ(SvPVX(sv), "ds", 2) ||
1130 strnEQ(SvPVX(sv), "ig", 2))
1135 null(o); /* don't execute or even remember it */
1139 o->op_type = OP_PREINC; /* pre-increment is faster */
1140 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1144 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1145 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1151 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1156 if (o->op_flags & OPf_STACKED)
1164 if (!(o->op_flags & OPf_KIDS))
1173 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1180 /* all requires must return a boolean value */
1181 o->op_flags &= ~OPf_WANT;
1184 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
1185 if (!kPMOP->op_pmreplroot)
1186 deprecate("implicit split to @_");
1192 if (ckWARN(WARN_VOID))
1193 Perl_warner(aTHX_ WARN_VOID, "Useless use of %s in void context", useless);
1199 Perl_listkids(pTHX_ OP *o)
1202 if (o && o->op_flags & OPf_KIDS) {
1203 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1210 Perl_list(pTHX_ OP *o)
1214 /* assumes no premature commitment */
1215 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
1216 || o->op_type == OP_RETURN)
1221 if ((o->op_private & OPpTARGET_MY)
1222 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1224 return o; /* As if inside SASSIGN */
1227 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1229 switch (o->op_type) {
1232 list(cBINOPo->op_first);
1237 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1245 if (!(o->op_flags & OPf_KIDS))
1247 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1248 list(cBINOPo->op_first);
1249 return gen_constant_list(o);
1256 kid = cLISTOPo->op_first;
1258 while ((kid = kid->op_sibling)) {
1259 if (kid->op_sibling)
1264 WITH_THR(PL_curcop = &PL_compiling);
1268 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1269 if (kid->op_sibling)
1274 WITH_THR(PL_curcop = &PL_compiling);
1277 /* all requires must return a boolean value */
1278 o->op_flags &= ~OPf_WANT;
1285 Perl_scalarseq(pTHX_ OP *o)
1290 if (o->op_type == OP_LINESEQ ||
1291 o->op_type == OP_SCOPE ||
1292 o->op_type == OP_LEAVE ||
1293 o->op_type == OP_LEAVETRY)
1296 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1297 if (kid->op_sibling) {
1301 PL_curcop = &PL_compiling;
1303 o->op_flags &= ~OPf_PARENS;
1304 if (PL_hints & HINT_BLOCK_SCOPE)
1305 o->op_flags |= OPf_PARENS;
1308 o = newOP(OP_STUB, 0);
1313 S_modkids(pTHX_ OP *o, I32 type)
1316 if (o && o->op_flags & OPf_KIDS) {
1317 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1324 Perl_mod(pTHX_ OP *o, I32 type)
1330 if (!o || PL_error_count)
1333 if ((o->op_private & OPpTARGET_MY)
1334 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1339 switch (o->op_type) {
1344 if (!(o->op_private & (OPpCONST_ARYBASE)))
1346 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1347 PL_compiling.cop_arybase = (I32)SvIV(cSVOPx(PL_eval_start)->op_sv);
1351 SAVEI32(PL_compiling.cop_arybase);
1352 PL_compiling.cop_arybase = 0;
1354 else if (type == OP_REFGEN)
1357 Perl_croak(aTHX_ "That use of $[ is unsupported");
1360 if (o->op_flags & OPf_PARENS)
1364 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1365 !(o->op_flags & OPf_STACKED)) {
1366 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1367 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1368 assert(cUNOPo->op_first->op_type == OP_NULL);
1369 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1372 else { /* lvalue subroutine call */
1373 o->op_private |= OPpLVAL_INTRO;
1374 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
1375 /* Backward compatibility mode: */
1376 o->op_private |= OPpENTERSUB_INARGS;
1379 else { /* Compile-time error message: */
1380 OP *kid = cUNOPo->op_first;
1384 if (kid->op_type == OP_PUSHMARK)
1386 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1388 "panic: unexpected lvalue entersub "
1389 "args: type/targ %ld:%ld",
1390 (long)kid->op_type,kid->op_targ);
1391 kid = kLISTOP->op_first;
1393 while (kid->op_sibling)
1394 kid = kid->op_sibling;
1395 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1397 if (kid->op_type == OP_METHOD_NAMED
1398 || kid->op_type == OP_METHOD)
1402 if (kid->op_sibling || kid->op_next != kid) {
1403 yyerror("panic: unexpected optree near method call");
1407 NewOp(1101, newop, 1, UNOP);
1408 newop->op_type = OP_RV2CV;
1409 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1410 newop->op_first = Nullop;
1411 newop->op_next = (OP*)newop;
1412 kid->op_sibling = (OP*)newop;
1413 newop->op_private |= OPpLVAL_INTRO;
1417 if (kid->op_type != OP_RV2CV)
1419 "panic: unexpected lvalue entersub "
1420 "entry via type/targ %ld:%ld",
1421 (long)kid->op_type,kid->op_targ);
1422 kid->op_private |= OPpLVAL_INTRO;
1423 break; /* Postpone until runtime */
1427 kid = kUNOP->op_first;
1428 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1429 kid = kUNOP->op_first;
1430 if (kid->op_type == OP_NULL)
1432 "Unexpected constant lvalue entersub "
1433 "entry via type/targ %ld:%ld",
1434 (long)kid->op_type,kid->op_targ);
1435 if (kid->op_type != OP_GV) {
1436 /* Restore RV2CV to check lvalueness */
1438 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1439 okid->op_next = kid->op_next;
1440 kid->op_next = okid;
1443 okid->op_next = Nullop;
1444 okid->op_type = OP_RV2CV;
1446 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1447 okid->op_private |= OPpLVAL_INTRO;
1451 cv = GvCV(kGVOP_gv);
1461 /* grep, foreach, subcalls, refgen */
1462 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1464 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1465 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1467 : (o->op_type == OP_ENTERSUB
1468 ? "non-lvalue subroutine call"
1469 : PL_op_desc[o->op_type])),
1470 type ? PL_op_desc[type] : "local"));
1484 case OP_RIGHT_SHIFT:
1493 if (!(o->op_flags & OPf_STACKED))
1499 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1505 if (!type && cUNOPo->op_first->op_type != OP_GV)
1506 Perl_croak(aTHX_ "Can't localize through a reference");
1507 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1508 PL_modcount = 10000;
1509 return o; /* Treat \(@foo) like ordinary list. */
1513 if (scalar_mod_type(o, type))
1515 ref(cUNOPo->op_first, o->op_type);
1524 PL_modcount = 10000;
1527 if (!type && cUNOPo->op_first->op_type != OP_GV)
1528 Perl_croak(aTHX_ "Can't localize through a reference");
1529 ref(cUNOPo->op_first, o->op_type);
1533 PL_hints |= HINT_BLOCK_SCOPE;
1543 PL_modcount = 10000;
1544 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1545 return o; /* Treat \(@foo) like ordinary list. */
1546 if (scalar_mod_type(o, type))
1552 Perl_croak(aTHX_ "Can't localize lexical variable %s",
1553 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
1558 PL_modcount++; /* XXX ??? */
1560 #endif /* USE_THREADS */
1566 if (type != OP_SASSIGN)
1570 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1576 pad_free(o->op_targ);
1577 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1578 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1579 if (o->op_flags & OPf_KIDS)
1580 mod(cBINOPo->op_first->op_sibling, type);
1585 ref(cBINOPo->op_first, o->op_type);
1586 if (type == OP_ENTERSUB &&
1587 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1588 o->op_private |= OPpLVAL_DEFER;
1595 if (o->op_flags & OPf_KIDS)
1596 mod(cLISTOPo->op_last, type);
1600 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1602 else if (!(o->op_flags & OPf_KIDS))
1604 if (o->op_targ != OP_LIST) {
1605 mod(cBINOPo->op_first, type);
1610 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1614 o->op_flags |= OPf_MOD;
1616 if (type == OP_AASSIGN || type == OP_SASSIGN)
1617 o->op_flags |= OPf_SPECIAL|OPf_REF;
1619 o->op_private |= OPpLVAL_INTRO;
1620 o->op_flags &= ~OPf_SPECIAL;
1621 PL_hints |= HINT_BLOCK_SCOPE;
1623 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1624 o->op_flags |= OPf_REF;
1629 S_scalar_mod_type(pTHX_ OP *o, I32 type)
1633 if (o->op_type == OP_RV2GV)
1657 case OP_RIGHT_SHIFT:
1676 S_is_handle_constructor(pTHX_ OP *o, I32 argnum)
1678 switch (o->op_type) {
1686 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1699 Perl_refkids(pTHX_ OP *o, I32 type)
1702 if (o && o->op_flags & OPf_KIDS) {
1703 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1710 Perl_ref(pTHX_ OP *o, I32 type)
1714 if (!o || PL_error_count)
1717 switch (o->op_type) {
1719 if ((type == OP_EXISTS || type == OP_DEFINED || type == OP_LOCK) &&
1720 !(o->op_flags & OPf_STACKED)) {
1721 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1722 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1723 assert(cUNOPo->op_first->op_type == OP_NULL);
1724 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1725 o->op_flags |= OPf_SPECIAL;
1730 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1734 if (type == OP_DEFINED)
1735 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1736 ref(cUNOPo->op_first, o->op_type);
1739 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1740 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1741 : type == OP_RV2HV ? OPpDEREF_HV
1743 o->op_flags |= OPf_MOD;
1748 o->op_flags |= OPf_MOD; /* XXX ??? */
1753 o->op_flags |= OPf_REF;
1756 if (type == OP_DEFINED)
1757 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1758 ref(cUNOPo->op_first, o->op_type);
1763 o->op_flags |= OPf_REF;
1768 if (!(o->op_flags & OPf_KIDS))
1770 ref(cBINOPo->op_first, type);
1774 ref(cBINOPo->op_first, o->op_type);
1775 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1776 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1777 : type == OP_RV2HV ? OPpDEREF_HV
1779 o->op_flags |= OPf_MOD;
1787 if (!(o->op_flags & OPf_KIDS))
1789 ref(cLISTOPo->op_last, type);
1799 S_dup_attrlist(pTHX_ OP *o)
1803 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1804 * where the first kid is OP_PUSHMARK and the remaining ones
1805 * are OP_CONST. We need to push the OP_CONST values.
1807 if (o->op_type == OP_CONST)
1808 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc(cSVOPo->op_sv));
1810 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1811 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1812 if (o->op_type == OP_CONST)
1813 rop = append_elem(OP_LIST, rop,
1814 newSVOP(OP_CONST, o->op_flags,
1815 SvREFCNT_inc(cSVOPo->op_sv)));
1822 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
1826 /* fake up C<use attributes $pkg,$rv,@attrs> */
1827 ENTER; /* need to protect against side-effects of 'use' */
1829 if (stash && HvNAME(stash))
1830 stashsv = newSVpv(HvNAME(stash), 0);
1832 stashsv = &PL_sv_no;
1834 #define ATTRSMODULE "attributes"
1836 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1837 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1839 prepend_elem(OP_LIST,
1840 newSVOP(OP_CONST, 0, stashsv),
1841 prepend_elem(OP_LIST,
1842 newSVOP(OP_CONST, 0,
1844 dup_attrlist(attrs))));
1849 S_my_kid(pTHX_ OP *o, OP *attrs)
1854 if (!o || PL_error_count)
1858 if (type == OP_LIST) {
1859 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1861 } else if (type == OP_UNDEF) {
1863 } else if (type == OP_RV2SV || /* "our" declaration */
1865 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
1866 o->op_private |= OPpOUR_INTRO;
1868 } else if (type != OP_PADSV &&
1871 type != OP_PUSHMARK)
1873 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
1874 PL_op_desc[o->op_type],
1875 PL_in_my == KEY_our ? "our" : "my"));
1878 else if (attrs && type != OP_PUSHMARK) {
1884 PL_in_my_stash = Nullhv;
1886 /* check for C<my Dog $spot> when deciding package */
1887 namesvp = av_fetch(PL_comppad_name, o->op_targ, FALSE);
1888 if (namesvp && *namesvp && SvOBJECT(*namesvp) && HvNAME(SvSTASH(*namesvp)))
1889 stash = SvSTASH(*namesvp);
1891 stash = PL_curstash;
1892 padsv = PAD_SV(o->op_targ);
1893 apply_attrs(stash, padsv, attrs);
1895 o->op_flags |= OPf_MOD;
1896 o->op_private |= OPpLVAL_INTRO;
1901 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1903 if (o->op_flags & OPf_PARENS)
1907 o = my_kid(o, attrs);
1909 PL_in_my_stash = Nullhv;
1914 Perl_my(pTHX_ OP *o)
1916 return my_kid(o, Nullop);
1920 Perl_sawparens(pTHX_ OP *o)
1923 o->op_flags |= OPf_PARENS;
1928 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
1933 if (ckWARN(WARN_MISC) &&
1934 (left->op_type == OP_RV2AV ||
1935 left->op_type == OP_RV2HV ||
1936 left->op_type == OP_PADAV ||
1937 left->op_type == OP_PADHV)) {
1938 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
1939 right->op_type == OP_TRANS)
1940 ? right->op_type : OP_MATCH];
1941 const char *sample = ((left->op_type == OP_RV2AV ||
1942 left->op_type == OP_PADAV)
1943 ? "@array" : "%hash");
1944 Perl_warner(aTHX_ WARN_MISC,
1945 "Applying %s to %s will act on scalar(%s)",
1946 desc, sample, sample);
1949 if (right->op_type == OP_MATCH ||
1950 right->op_type == OP_SUBST ||
1951 right->op_type == OP_TRANS) {
1952 right->op_flags |= OPf_STACKED;
1953 if (right->op_type != OP_MATCH)
1954 left = mod(left, right->op_type);
1955 if (right->op_type == OP_TRANS)
1956 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1958 o = prepend_elem(right->op_type, scalar(left), right);
1960 return newUNOP(OP_NOT, 0, scalar(o));
1964 return bind_match(type, left,
1965 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1969 Perl_invert(pTHX_ OP *o)
1973 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1974 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1978 Perl_scope(pTHX_ OP *o)
1981 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
1982 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1983 o->op_type = OP_LEAVE;
1984 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
1987 if (o->op_type == OP_LINESEQ) {
1989 o->op_type = OP_SCOPE;
1990 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
1991 kid = ((LISTOP*)o)->op_first;
1992 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
1996 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
2003 Perl_save_hints(pTHX)
2006 SAVESPTR(GvHV(PL_hintgv));
2007 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
2008 SAVEFREESV(GvHV(PL_hintgv));
2012 Perl_block_start(pTHX_ int full)
2015 int retval = PL_savestack_ix;
2017 SAVEI32(PL_comppad_name_floor);
2018 PL_comppad_name_floor = AvFILLp(PL_comppad_name);
2020 PL_comppad_name_fill = PL_comppad_name_floor;
2021 if (PL_comppad_name_floor < 0)
2022 PL_comppad_name_floor = 0;
2023 SAVEI32(PL_min_intro_pending);
2024 SAVEI32(PL_max_intro_pending);
2025 PL_min_intro_pending = 0;
2026 SAVEI32(PL_comppad_name_fill);
2027 SAVEI32(PL_padix_floor);
2028 PL_padix_floor = PL_padix;
2029 PL_pad_reset_pending = FALSE;
2031 PL_hints &= ~HINT_BLOCK_SCOPE;
2032 SAVESPTR(PL_compiling.cop_warnings);
2033 if (! specialWARN(PL_compiling.cop_warnings)) {
2034 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
2035 SAVEFREESV(PL_compiling.cop_warnings) ;
2041 Perl_block_end(pTHX_ I32 floor, OP *seq)
2044 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
2045 OP* retval = scalarseq(seq);
2047 PL_pad_reset_pending = FALSE;
2048 PL_compiling.op_private = PL_hints;
2050 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
2051 pad_leavemy(PL_comppad_name_fill);
2060 OP *o = newOP(OP_THREADSV, 0);
2061 o->op_targ = find_threadsv("_");
2064 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
2065 #endif /* USE_THREADS */
2069 Perl_newPROG(pTHX_ OP *o)
2075 PL_eval_root = newUNOP(OP_LEAVEEVAL,
2076 ((PL_in_eval & EVAL_KEEPERR)
2077 ? OPf_SPECIAL : 0), o);
2078 PL_eval_start = linklist(PL_eval_root);
2079 PL_eval_root->op_private |= OPpREFCOUNTED;
2080 OpREFCNT_set(PL_eval_root, 1);
2081 PL_eval_root->op_next = 0;
2082 peep(PL_eval_start);
2087 PL_main_root = scope(sawparens(scalarvoid(o)));
2088 PL_curcop = &PL_compiling;
2089 PL_main_start = LINKLIST(PL_main_root);
2090 PL_main_root->op_private |= OPpREFCOUNTED;
2091 OpREFCNT_set(PL_main_root, 1);
2092 PL_main_root->op_next = 0;
2093 peep(PL_main_start);
2096 /* Register with debugger */
2098 CV *cv = get_cv("DB::postponed", FALSE);
2102 XPUSHs((SV*)CopFILEGV(&PL_compiling));
2104 call_sv((SV*)cv, G_DISCARD);
2111 Perl_localize(pTHX_ OP *o, I32 lex)
2113 if (o->op_flags & OPf_PARENS)
2117 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
2119 for (s = PL_bufptr; *s && (isALNUM(*s) || (*s & 0x80) || strchr("@$%, ",*s)); s++) ;
2120 if (*s == ';' || *s == '=')
2121 Perl_warner(aTHX_ WARN_PARENTHESIS,
2122 "Parentheses missing around \"%s\" list",
2123 lex ? (PL_in_my == KEY_our ? "our" : "my") : "local");
2129 o = mod(o, OP_NULL); /* a bit kludgey */
2131 PL_in_my_stash = Nullhv;
2136 Perl_jmaybe(pTHX_ OP *o)
2138 if (o->op_type == OP_LIST) {
2141 o2 = newOP(OP_THREADSV, 0);
2142 o2->op_targ = find_threadsv(";");
2144 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
2145 #endif /* USE_THREADS */
2146 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2152 Perl_fold_constants(pTHX_ register OP *o)
2156 I32 type = o->op_type;
2159 if (PL_opargs[type] & OA_RETSCALAR)
2161 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2162 o->op_targ = pad_alloc(type, SVs_PADTMP);
2164 /* integerize op, unless it happens to be C<-foo>.
2165 * XXX should pp_i_negate() do magic string negation instead? */
2166 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2167 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2168 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2170 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2173 if (!(PL_opargs[type] & OA_FOLDCONST))
2178 /* XXX might want a ck_negate() for this */
2179 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2192 if (o->op_private & OPpLOCALE)
2197 goto nope; /* Don't try to run w/ errors */
2199 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2200 if ((curop->op_type != OP_CONST ||
2201 (curop->op_private & OPpCONST_BARE)) &&
2202 curop->op_type != OP_LIST &&
2203 curop->op_type != OP_SCALAR &&
2204 curop->op_type != OP_NULL &&
2205 curop->op_type != OP_PUSHMARK)
2211 curop = LINKLIST(o);
2215 sv = *(PL_stack_sp--);
2216 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
2217 pad_swipe(o->op_targ);
2218 else if (SvTEMP(sv)) { /* grab mortal temp? */
2219 (void)SvREFCNT_inc(sv);
2223 if (type == OP_RV2GV)
2224 return newGVOP(OP_GV, 0, (GV*)sv);
2226 /* try to smush double to int, but don't smush -2.0 to -2 */
2227 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
2231 if ((NV)iv == SvNV(sv)) {
2236 SvIOK_off(sv); /* undo SvIV() damage */
2238 return newSVOP(OP_CONST, 0, sv);
2242 if (!(PL_opargs[type] & OA_OTHERINT))
2245 if (!(PL_hints & HINT_INTEGER)) {
2246 if (type == OP_MODULO
2247 || type == OP_DIVIDE
2248 || !(o->op_flags & OPf_KIDS))
2253 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
2254 if (curop->op_type == OP_CONST) {
2255 if (SvIOK(((SVOP*)curop)->op_sv))
2259 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
2263 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
2270 Perl_gen_constant_list(pTHX_ register OP *o)
2274 I32 oldtmps_floor = PL_tmps_floor;
2278 return o; /* Don't attempt to run with errors */
2280 PL_op = curop = LINKLIST(o);
2287 PL_tmps_floor = oldtmps_floor;
2289 o->op_type = OP_RV2AV;
2290 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2291 curop = ((UNOP*)o)->op_first;
2292 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
2299 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2304 if (!o || o->op_type != OP_LIST)
2305 o = newLISTOP(OP_LIST, 0, o, Nullop);
2307 o->op_flags &= ~OPf_WANT;
2309 if (!(PL_opargs[type] & OA_MARK))
2310 null(cLISTOPo->op_first);
2313 o->op_ppaddr = PL_ppaddr[type];
2314 o->op_flags |= flags;
2316 o = CHECKOP(type, o);
2317 if (o->op_type != type)
2320 if (cLISTOPo->op_children < 7) {
2321 /* XXX do we really need to do this if we're done appending?? */
2322 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2324 cLISTOPo->op_last = last; /* in case check substituted last arg */
2327 return fold_constants(o);
2330 /* List constructors */
2333 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2341 if (first->op_type != type
2342 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
2344 return newLISTOP(type, 0, first, last);
2347 if (first->op_flags & OPf_KIDS)
2348 ((LISTOP*)first)->op_last->op_sibling = last;
2350 first->op_flags |= OPf_KIDS;
2351 ((LISTOP*)first)->op_first = last;
2353 ((LISTOP*)first)->op_last = last;
2354 ((LISTOP*)first)->op_children++;
2359 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2367 if (first->op_type != type)
2368 return prepend_elem(type, (OP*)first, (OP*)last);
2370 if (last->op_type != type)
2371 return append_elem(type, (OP*)first, (OP*)last);
2373 first->op_last->op_sibling = last->op_first;
2374 first->op_last = last->op_last;
2375 first->op_children += last->op_children;
2376 if (first->op_children)
2377 first->op_flags |= OPf_KIDS;
2379 #ifdef PL_OP_SLAB_ALLOC
2387 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2395 if (last->op_type == type) {
2396 if (type == OP_LIST) { /* already a PUSHMARK there */
2397 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2398 ((LISTOP*)last)->op_first->op_sibling = first;
2401 if (!(last->op_flags & OPf_KIDS)) {
2402 ((LISTOP*)last)->op_last = first;
2403 last->op_flags |= OPf_KIDS;
2405 first->op_sibling = ((LISTOP*)last)->op_first;
2406 ((LISTOP*)last)->op_first = first;
2408 ((LISTOP*)last)->op_children++;
2412 return newLISTOP(type, 0, first, last);
2418 Perl_newNULLLIST(pTHX)
2420 return newOP(OP_STUB, 0);
2424 Perl_force_list(pTHX_ OP *o)
2426 if (!o || o->op_type != OP_LIST)
2427 o = newLISTOP(OP_LIST, 0, o, Nullop);
2433 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2437 NewOp(1101, listop, 1, LISTOP);
2439 listop->op_type = type;
2440 listop->op_ppaddr = PL_ppaddr[type];
2441 listop->op_children = (first != 0) + (last != 0);
2442 listop->op_flags = flags;
2446 else if (!first && last)
2449 first->op_sibling = last;
2450 listop->op_first = first;
2451 listop->op_last = last;
2452 if (type == OP_LIST) {
2454 pushop = newOP(OP_PUSHMARK, 0);
2455 pushop->op_sibling = first;
2456 listop->op_first = pushop;
2457 listop->op_flags |= OPf_KIDS;
2459 listop->op_last = pushop;
2461 else if (listop->op_children)
2462 listop->op_flags |= OPf_KIDS;
2468 Perl_newOP(pTHX_ I32 type, I32 flags)
2471 NewOp(1101, o, 1, OP);
2473 o->op_ppaddr = PL_ppaddr[type];
2474 o->op_flags = flags;
2477 o->op_private = 0 + (flags >> 8);
2478 if (PL_opargs[type] & OA_RETSCALAR)
2480 if (PL_opargs[type] & OA_TARGET)
2481 o->op_targ = pad_alloc(type, SVs_PADTMP);
2482 return CHECKOP(type, o);
2486 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
2491 first = newOP(OP_STUB, 0);
2492 if (PL_opargs[type] & OA_MARK)
2493 first = force_list(first);
2495 NewOp(1101, unop, 1, UNOP);
2496 unop->op_type = type;
2497 unop->op_ppaddr = PL_ppaddr[type];
2498 unop->op_first = first;
2499 unop->op_flags = flags | OPf_KIDS;
2500 unop->op_private = 1 | (flags >> 8);
2501 unop = (UNOP*) CHECKOP(type, unop);
2505 return fold_constants((OP *) unop);
2509 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2512 NewOp(1101, binop, 1, BINOP);
2515 first = newOP(OP_NULL, 0);
2517 binop->op_type = type;
2518 binop->op_ppaddr = PL_ppaddr[type];
2519 binop->op_first = first;
2520 binop->op_flags = flags | OPf_KIDS;
2523 binop->op_private = 1 | (flags >> 8);
2526 binop->op_private = 2 | (flags >> 8);
2527 first->op_sibling = last;
2530 binop = (BINOP*)CHECKOP(type, binop);
2531 if (binop->op_next || binop->op_type != type)
2534 binop->op_last = binop->op_first->op_sibling;
2536 return fold_constants((OP *)binop);
2540 utf8compare(const void *a, const void *b)
2543 for (i = 0; i < 10; i++) {
2544 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2546 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2553 Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
2555 SV *tstr = ((SVOP*)expr)->op_sv;
2556 SV *rstr = ((SVOP*)repl)->op_sv;
2559 register U8 *t = (U8*)SvPV(tstr, tlen);
2560 register U8 *r = (U8*)SvPV(rstr, rlen);
2566 register short *tbl;
2568 complement = o->op_private & OPpTRANS_COMPLEMENT;
2569 del = o->op_private & OPpTRANS_DELETE;
2570 squash = o->op_private & OPpTRANS_SQUASH;
2572 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2573 SV* listsv = newSVpvn("# comment\n",10);
2575 U8* tend = t + tlen;
2576 U8* rend = r + rlen;
2591 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2592 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2595 U8 tmpbuf[UTF8_MAXLEN];
2598 New(1109, cp, tlen, U8*);
2600 transv = newSVpvn("",0);
2609 qsort(cp, i, sizeof(U8*), utf8compare);
2610 for (j = 0; j < i; j++) {
2612 UV val = utf8_to_uv(s, &ulen);
2614 diff = val - nextmin;
2616 t = uv_to_utf8(tmpbuf,nextmin);
2617 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2619 t = uv_to_utf8(tmpbuf, val - 1);
2620 sv_catpvn(transv, "\377", 1);
2621 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2625 val = utf8_to_uv(s+1, &ulen);
2629 t = uv_to_utf8(tmpbuf,nextmin);
2630 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2631 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2632 sv_catpvn(transv, "\377", 1);
2633 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2634 t = (U8*)SvPVX(transv);
2635 tlen = SvCUR(transv);
2638 else if (!rlen && !del) {
2639 r = t; rlen = tlen; rend = tend;
2642 if (to_utf && from_utf) { /* only counting characters */
2643 if (t == r || (tlen == rlen && memEQ(t, r, tlen)))
2644 o->op_private |= OPpTRANS_IDENTICAL;
2646 else { /* straight latin-1 translation */
2647 if (tlen == 4 && memEQ(t, "\0\377\303\277", 4) &&
2648 rlen == 4 && memEQ(r, "\0\377\303\277", 4))
2649 o->op_private |= OPpTRANS_IDENTICAL;
2653 while (t < tend || tfirst <= tlast) {
2654 /* see if we need more "t" chars */
2655 if (tfirst > tlast) {
2656 tfirst = (I32)utf8_to_uv(t, &ulen);
2658 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
2659 tlast = (I32)utf8_to_uv(++t, &ulen);
2666 /* now see if we need more "r" chars */
2667 if (rfirst > rlast) {
2669 rfirst = (I32)utf8_to_uv(r, &ulen);
2671 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
2672 rlast = (I32)utf8_to_uv(++r, &ulen);
2681 rfirst = rlast = 0xffffffff;
2685 /* now see which range will peter our first, if either. */
2686 tdiff = tlast - tfirst;
2687 rdiff = rlast - rfirst;
2694 if (rfirst == 0xffffffff) {
2695 diff = tdiff; /* oops, pretend rdiff is infinite */
2697 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
2698 (long)tfirst, (long)tlast);
2700 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
2704 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
2705 (long)tfirst, (long)(tfirst + diff),
2708 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
2709 (long)tfirst, (long)rfirst);
2711 if (rfirst + diff > max)
2712 max = rfirst + diff;
2717 else if (rfirst <= 0x800)
2718 grows |= (tfirst < 0x80);
2719 else if (rfirst <= 0x10000)
2720 grows |= (tfirst < 0x800);
2721 else if (rfirst <= 0x200000)
2722 grows |= (tfirst < 0x10000);
2723 else if (rfirst <= 0x4000000)
2724 grows |= (tfirst < 0x200000);
2725 else if (rfirst <= 0x80000000)
2726 grows |= (tfirst < 0x4000000);
2738 else if (max > 0xff)
2743 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2744 SvREFCNT_dec(listsv);
2746 SvREFCNT_dec(transv);
2748 if (!del && havefinal)
2749 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5,
2750 newSVuv((UV)final), 0);
2752 if (grows && to_utf)
2753 o->op_private |= OPpTRANS_GROWS;
2760 tbl = (short*)cPVOPo->op_pv;
2762 Zero(tbl, 256, short);
2763 for (i = 0; i < tlen; i++)
2765 for (i = 0, j = 0; i < 256; i++) {
2781 if (!rlen && !del) {
2784 o->op_private |= OPpTRANS_IDENTICAL;
2786 for (i = 0; i < 256; i++)
2788 for (i = 0, j = 0; i < tlen; i++,j++) {
2791 if (tbl[t[i]] == -1)
2797 if (tbl[t[i]] == -1)
2808 Perl_newPMOP(pTHX_ I32 type, I32 flags)
2813 NewOp(1101, pmop, 1, PMOP);
2814 pmop->op_type = type;
2815 pmop->op_ppaddr = PL_ppaddr[type];
2816 pmop->op_flags = flags;
2817 pmop->op_private = 0 | (flags >> 8);
2819 if (PL_hints & HINT_RE_TAINT)
2820 pmop->op_pmpermflags |= PMf_RETAINT;
2821 if (PL_hints & HINT_LOCALE)
2822 pmop->op_pmpermflags |= PMf_LOCALE;
2823 pmop->op_pmflags = pmop->op_pmpermflags;
2825 /* link into pm list */
2826 if (type != OP_TRANS && PL_curstash) {
2827 pmop->op_pmnext = HvPMROOT(PL_curstash);
2828 HvPMROOT(PL_curstash) = pmop;
2835 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
2840 I32 repl_has_vars = 0;
2842 if (o->op_type == OP_TRANS)
2843 return pmtrans(o, expr, repl);
2845 PL_hints |= HINT_BLOCK_SCOPE;
2848 if (expr->op_type == OP_CONST) {
2850 SV *pat = ((SVOP*)expr)->op_sv;
2851 char *p = SvPV(pat, plen);
2852 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2853 sv_setpvn(pat, "\\s+", 3);
2854 p = SvPV(pat, plen);
2855 pm->op_pmflags |= PMf_SKIPWHITE;
2857 if ((PL_hints & HINT_UTF8) || (SvUTF8(pat) && !(PL_hints & HINT_BYTE)))
2858 pm->op_pmdynflags |= PMdf_UTF8;
2859 pm->op_pmregexp = CALLREGCOMP(aTHX_ p, p + plen, pm);
2860 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2861 pm->op_pmflags |= PMf_WHITE;
2865 if (PL_hints & HINT_UTF8)
2866 pm->op_pmdynflags |= PMdf_UTF8;
2867 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2868 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2870 : OP_REGCMAYBE),0,expr);
2872 NewOp(1101, rcop, 1, LOGOP);
2873 rcop->op_type = OP_REGCOMP;
2874 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2875 rcop->op_first = scalar(expr);
2876 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2877 ? (OPf_SPECIAL | OPf_KIDS)
2879 rcop->op_private = 1;
2882 /* establish postfix order */
2883 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2885 rcop->op_next = expr;
2886 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2889 rcop->op_next = LINKLIST(expr);
2890 expr->op_next = (OP*)rcop;
2893 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2898 if (pm->op_pmflags & PMf_EVAL) {
2900 if (CopLINE(PL_curcop) < PL_multi_end)
2901 CopLINE_set(PL_curcop, PL_multi_end);
2904 else if (repl->op_type == OP_THREADSV
2905 && strchr("&`'123456789+",
2906 PL_threadsv_names[repl->op_targ]))
2910 #endif /* USE_THREADS */
2911 else if (repl->op_type == OP_CONST)
2915 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2916 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2918 if (curop->op_type == OP_THREADSV) {
2920 if (strchr("&`'123456789+", curop->op_private))
2924 if (curop->op_type == OP_GV) {
2925 GV *gv = cGVOPx_gv(curop);
2927 if (strchr("&`'123456789+", *GvENAME(gv)))
2930 #endif /* USE_THREADS */
2931 else if (curop->op_type == OP_RV2CV)
2933 else if (curop->op_type == OP_RV2SV ||
2934 curop->op_type == OP_RV2AV ||
2935 curop->op_type == OP_RV2HV ||
2936 curop->op_type == OP_RV2GV) {
2937 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2940 else if (curop->op_type == OP_PADSV ||
2941 curop->op_type == OP_PADAV ||
2942 curop->op_type == OP_PADHV ||
2943 curop->op_type == OP_PADANY) {
2946 else if (curop->op_type == OP_PUSHRE)
2947 ; /* Okay here, dangerous in newASSIGNOP */
2956 && (!pm->op_pmregexp
2957 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
2958 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2959 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2960 prepend_elem(o->op_type, scalar(repl), o);
2963 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
2964 pm->op_pmflags |= PMf_MAYBE_CONST;
2965 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2967 NewOp(1101, rcop, 1, LOGOP);
2968 rcop->op_type = OP_SUBSTCONT;
2969 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
2970 rcop->op_first = scalar(repl);
2971 rcop->op_flags |= OPf_KIDS;
2972 rcop->op_private = 1;
2975 /* establish postfix order */
2976 rcop->op_next = LINKLIST(repl);
2977 repl->op_next = (OP*)rcop;
2979 pm->op_pmreplroot = scalar((OP*)rcop);
2980 pm->op_pmreplstart = LINKLIST(rcop);
2989 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
2992 NewOp(1101, svop, 1, SVOP);
2993 svop->op_type = type;
2994 svop->op_ppaddr = PL_ppaddr[type];
2996 svop->op_next = (OP*)svop;
2997 svop->op_flags = flags;
2998 if (PL_opargs[type] & OA_RETSCALAR)
3000 if (PL_opargs[type] & OA_TARGET)
3001 svop->op_targ = pad_alloc(type, SVs_PADTMP);
3002 return CHECKOP(type, svop);
3006 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
3009 NewOp(1101, padop, 1, PADOP);
3010 padop->op_type = type;
3011 padop->op_ppaddr = PL_ppaddr[type];
3012 padop->op_padix = pad_alloc(type, SVs_PADTMP);
3013 SvREFCNT_dec(PL_curpad[padop->op_padix]);
3014 PL_curpad[padop->op_padix] = sv;
3016 padop->op_next = (OP*)padop;
3017 padop->op_flags = flags;
3018 if (PL_opargs[type] & OA_RETSCALAR)
3020 if (PL_opargs[type] & OA_TARGET)
3021 padop->op_targ = pad_alloc(type, SVs_PADTMP);
3022 return CHECKOP(type, padop);
3026 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
3031 return newPADOP(type, flags, SvREFCNT_inc(gv));
3033 return newSVOP(type, flags, SvREFCNT_inc(gv));
3038 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
3041 NewOp(1101, pvop, 1, PVOP);
3042 pvop->op_type = type;
3043 pvop->op_ppaddr = PL_ppaddr[type];
3045 pvop->op_next = (OP*)pvop;
3046 pvop->op_flags = flags;
3047 if (PL_opargs[type] & OA_RETSCALAR)
3049 if (PL_opargs[type] & OA_TARGET)
3050 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
3051 return CHECKOP(type, pvop);
3055 Perl_package(pTHX_ OP *o)
3060 save_hptr(&PL_curstash);
3061 save_item(PL_curstname);
3066 name = SvPV(sv, len);
3067 PL_curstash = gv_stashpvn(name,len,TRUE);
3068 sv_setpvn(PL_curstname, name, len);
3072 sv_setpv(PL_curstname,"<none>");
3073 PL_curstash = Nullhv;
3075 PL_hints |= HINT_BLOCK_SCOPE;
3076 PL_copline = NOLINE;
3081 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg)
3089 if (id->op_type != OP_CONST)
3090 Perl_croak(aTHX_ "Module name must be constant");
3094 if (version != Nullop) {
3095 SV *vesv = ((SVOP*)version)->op_sv;
3097 if (arg == Nullop && !SvNIOKp(vesv)) {
3104 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
3105 Perl_croak(aTHX_ "Version number must be constant number");
3107 /* Make copy of id so we don't free it twice */
3108 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3110 /* Fake up a method call to VERSION */
3111 meth = newSVpvn("VERSION",7);
3112 sv_upgrade(meth, SVt_PVIV);
3113 (void)SvIOK_on(meth);
3114 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
3115 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3116 append_elem(OP_LIST,
3117 prepend_elem(OP_LIST, pack, list(version)),
3118 newSVOP(OP_METHOD_NAMED, 0, meth)));
3122 /* Fake up an import/unimport */
3123 if (arg && arg->op_type == OP_STUB)
3124 imop = arg; /* no import on explicit () */
3125 else if (SvNIOKp(((SVOP*)id)->op_sv)) {
3126 imop = Nullop; /* use 5.0; */
3131 /* Make copy of id so we don't free it twice */
3132 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3134 /* Fake up a method call to import/unimport */
3135 meth = aver ? newSVpvn("import",6) : newSVpvn("unimport", 8);;
3136 sv_upgrade(meth, SVt_PVIV);
3137 (void)SvIOK_on(meth);
3138 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
3139 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3140 append_elem(OP_LIST,
3141 prepend_elem(OP_LIST, pack, list(arg)),
3142 newSVOP(OP_METHOD_NAMED, 0, meth)));
3145 /* Fake up a require, handle override, if any */
3146 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
3147 if (!(gv && GvIMPORTED_CV(gv)))
3148 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
3150 if (gv && GvIMPORTED_CV(gv)) {
3151 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3152 append_elem(OP_LIST, id,
3153 scalar(newUNOP(OP_RV2CV, 0,
3158 rqop = newUNOP(OP_REQUIRE, 0, id);
3161 /* Fake up the BEGIN {}, which does its thing immediately. */
3163 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
3166 append_elem(OP_LINESEQ,
3167 append_elem(OP_LINESEQ,
3168 newSTATEOP(0, Nullch, rqop),
3169 newSTATEOP(0, Nullch, veop)),
3170 newSTATEOP(0, Nullch, imop) ));
3172 PL_hints |= HINT_BLOCK_SCOPE;
3173 PL_copline = NOLINE;
3178 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
3181 va_start(args, ver);
3182 vload_module(flags, name, ver, &args);
3186 #ifdef PERL_IMPLICIT_CONTEXT
3188 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
3192 va_start(args, ver);
3193 vload_module(flags, name, ver, &args);
3199 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
3201 OP *modname, *veop, *imop;
3203 modname = newSVOP(OP_CONST, 0, name);
3204 modname->op_private |= OPpCONST_BARE;
3206 veop = newSVOP(OP_CONST, 0, ver);
3210 if (flags & PERL_LOADMOD_NOIMPORT) {
3211 imop = sawparens(newNULLLIST());
3213 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
3214 imop = va_arg(*args, OP*);
3219 sv = va_arg(*args, SV*);
3221 imop = append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
3222 sv = va_arg(*args, SV*);
3225 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
3226 veop, modname, imop);
3230 Perl_dofile(pTHX_ OP *term)
3235 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
3236 if (!(gv && GvIMPORTED_CV(gv)))
3237 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
3239 if (gv && GvIMPORTED_CV(gv)) {
3240 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3241 append_elem(OP_LIST, term,
3242 scalar(newUNOP(OP_RV2CV, 0,
3247 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3253 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
3255 return newBINOP(OP_LSLICE, flags,
3256 list(force_list(subscript)),
3257 list(force_list(listval)) );
3261 S_list_assignment(pTHX_ register OP *o)
3266 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3267 o = cUNOPo->op_first;
3269 if (o->op_type == OP_COND_EXPR) {
3270 I32 t = list_assignment(cLOGOPo->op_first->op_sibling);
3271 I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
3276 yyerror("Assignment to both a list and a scalar");
3280 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3281 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3282 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
3285 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
3288 if (o->op_type == OP_RV2SV)
3295 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
3300 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
3301 return newLOGOP(optype, 0,
3302 mod(scalar(left), optype),
3303 newUNOP(OP_SASSIGN, 0, scalar(right)));
3306 return newBINOP(optype, OPf_STACKED,
3307 mod(scalar(left), optype), scalar(right));
3311 if (list_assignment(left)) {
3316 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3317 left = mod(left, OP_AASSIGN);
3325 curop = list(force_list(left));
3326 o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
3327 o->op_private = 0 | (flags >> 8);
3328 for (curop = ((LISTOP*)curop)->op_first;
3329 curop; curop = curop->op_sibling)
3331 if (curop->op_type == OP_RV2HV &&
3332 ((UNOP*)curop)->op_first->op_type != OP_GV) {
3333 o->op_private |= OPpASSIGN_HASH;
3337 if (!(left->op_private & OPpLVAL_INTRO)) {
3340 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3341 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3342 if (curop->op_type == OP_GV) {
3343 GV *gv = cGVOPx_gv(curop);
3344 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3346 SvCUR(gv) = PL_generation;
3348 else if (curop->op_type == OP_PADSV ||
3349 curop->op_type == OP_PADAV ||
3350 curop->op_type == OP_PADHV ||
3351 curop->op_type == OP_PADANY) {
3352 SV **svp = AvARRAY(PL_comppad_name);
3353 SV *sv = svp[curop->op_targ];
3354 if (SvCUR(sv) == PL_generation)
3356 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
3358 else if (curop->op_type == OP_RV2CV)
3360 else if (curop->op_type == OP_RV2SV ||
3361 curop->op_type == OP_RV2AV ||
3362 curop->op_type == OP_RV2HV ||
3363 curop->op_type == OP_RV2GV) {
3364 if (lastop->op_type != OP_GV) /* funny deref? */
3367 else if (curop->op_type == OP_PUSHRE) {
3368 if (((PMOP*)curop)->op_pmreplroot) {
3369 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3370 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3372 SvCUR(gv) = PL_generation;
3381 o->op_private |= OPpASSIGN_COMMON;
3383 if (right && right->op_type == OP_SPLIT) {
3385 if ((tmpop = ((LISTOP*)right)->op_first) &&
3386 tmpop->op_type == OP_PUSHRE)
3388 PMOP *pm = (PMOP*)tmpop;
3389 if (left->op_type == OP_RV2AV &&
3390 !(left->op_private & OPpLVAL_INTRO) &&
3391 !(o->op_private & OPpASSIGN_COMMON) )
3393 tmpop = ((UNOP*)left)->op_first;
3394 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
3396 pm->op_pmreplroot = (OP*)cPADOPx(tmpop)->op_padix;
3397 cPADOPx(tmpop)->op_padix = 0; /* steal it */
3399 pm->op_pmreplroot = (OP*)cSVOPx(tmpop)->op_sv;
3400 cSVOPx(tmpop)->op_sv = Nullsv; /* steal it */
3402 pm->op_pmflags |= PMf_ONCE;
3403 tmpop = cUNOPo->op_first; /* to list (nulled) */
3404 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3405 tmpop->op_sibling = Nullop; /* don't free split */
3406 right->op_next = tmpop->op_next; /* fix starting loc */
3407 op_free(o); /* blow off assign */
3408 right->op_flags &= ~OPf_WANT;
3409 /* "I don't know and I don't care." */
3414 if (PL_modcount < 10000 &&
3415 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3417 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3419 sv_setiv(sv, PL_modcount+1);
3427 right = newOP(OP_UNDEF, 0);
3428 if (right->op_type == OP_READLINE) {
3429 right->op_flags |= OPf_STACKED;
3430 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3433 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3434 o = newBINOP(OP_SASSIGN, flags,
3435 scalar(right), mod(scalar(left), OP_SASSIGN) );
3447 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3450 U32 seq = intro_my();
3453 NewOp(1101, cop, 1, COP);
3454 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
3455 cop->op_type = OP_DBSTATE;
3456 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3459 cop->op_type = OP_NEXTSTATE;
3460 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3462 cop->op_flags = flags;
3463 cop->op_private = (PL_hints & HINT_BYTE);
3465 cop->op_private |= NATIVE_HINTS;
3467 PL_compiling.op_private = cop->op_private;
3468 cop->op_next = (OP*)cop;
3471 cop->cop_label = label;
3472 PL_hints |= HINT_BLOCK_SCOPE;
3475 cop->cop_arybase = PL_curcop->cop_arybase;
3476 if (specialWARN(PL_curcop->cop_warnings))
3477 cop->cop_warnings = PL_curcop->cop_warnings ;
3479 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3482 if (PL_copline == NOLINE)
3483 CopLINE_set(cop, CopLINE(PL_curcop));
3485 CopLINE_set(cop, PL_copline);
3486 PL_copline = NOLINE;
3489 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXXXX share in a pvtable? */
3491 CopFILEGV_set(cop, (GV*)SvREFCNT_inc(CopFILEGV(PL_curcop)));
3493 CopSTASH_set(cop, PL_curstash);
3495 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3496 SV **svp = av_fetch(CopFILEAV(PL_curcop), (I32)CopLINE(cop), FALSE);
3497 if (svp && *svp != &PL_sv_undef && !SvIOK(*svp)) {
3498 (void)SvIOK_on(*svp);
3499 SvIVX(*svp) = PTR2IV(cop);
3503 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3506 /* "Introduce" my variables to visible status. */
3514 if (! PL_min_intro_pending)
3515 return PL_cop_seqmax;
3517 svp = AvARRAY(PL_comppad_name);
3518 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
3519 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
3520 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
3521 SvNVX(sv) = (NV)PL_cop_seqmax;
3524 PL_min_intro_pending = 0;
3525 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
3526 return PL_cop_seqmax++;
3530 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
3532 return new_logop(type, flags, &first, &other);
3536 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
3541 OP *first = *firstp;
3542 OP *other = *otherp;
3544 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3545 return newBINOP(type, flags, scalar(first), scalar(other));
3547 scalarboolean(first);
3548 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3549 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3550 if (type == OP_AND || type == OP_OR) {
3556 first = *firstp = cUNOPo->op_first;
3558 first->op_next = o->op_next;
3559 cUNOPo->op_first = Nullop;
3563 if (first->op_type == OP_CONST) {
3564 if (ckWARN(WARN_BAREWORD) && (first->op_private & OPpCONST_BARE))
3565 Perl_warner(aTHX_ WARN_BAREWORD, "Bareword found in conditional");
3566 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3577 else if (first->op_type == OP_WANTARRAY) {
3583 else if (ckWARN(WARN_MISC) && (first->op_flags & OPf_KIDS)) {
3584 OP *k1 = ((UNOP*)first)->op_first;
3585 OP *k2 = k1->op_sibling;
3587 switch (first->op_type)
3590 if (k2 && k2->op_type == OP_READLINE
3591 && (k2->op_flags & OPf_STACKED)
3592 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3594 warnop = k2->op_type;
3599 if (k1->op_type == OP_READDIR
3600 || k1->op_type == OP_GLOB
3601 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3602 || k1->op_type == OP_EACH)
3604 warnop = ((k1->op_type == OP_NULL)
3605 ? k1->op_targ : k1->op_type);
3610 line_t oldline = CopLINE(PL_curcop);
3611 CopLINE_set(PL_curcop, PL_copline);
3612 Perl_warner(aTHX_ WARN_MISC,
3613 "Value of %s%s can be \"0\"; test with defined()",
3615 ((warnop == OP_READLINE || warnop == OP_GLOB)
3616 ? " construct" : "() operator"));
3617 CopLINE_set(PL_curcop, oldline);
3624 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3625 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3627 NewOp(1101, logop, 1, LOGOP);
3629 logop->op_type = type;
3630 logop->op_ppaddr = PL_ppaddr[type];
3631 logop->op_first = first;
3632 logop->op_flags = flags | OPf_KIDS;
3633 logop->op_other = LINKLIST(other);
3634 logop->op_private = 1 | (flags >> 8);
3636 /* establish postfix order */
3637 logop->op_next = LINKLIST(first);
3638 first->op_next = (OP*)logop;
3639 first->op_sibling = other;
3641 o = newUNOP(OP_NULL, 0, (OP*)logop);
3648 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
3656 return newLOGOP(OP_AND, 0, first, trueop);
3658 return newLOGOP(OP_OR, 0, first, falseop);
3660 scalarboolean(first);
3661 if (first->op_type == OP_CONST) {
3662 if (SvTRUE(((SVOP*)first)->op_sv)) {
3673 else if (first->op_type == OP_WANTARRAY) {
3677 NewOp(1101, logop, 1, LOGOP);
3678 logop->op_type = OP_COND_EXPR;
3679 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3680 logop->op_first = first;
3681 logop->op_flags = flags | OPf_KIDS;
3682 logop->op_private = 1 | (flags >> 8);
3683 logop->op_other = LINKLIST(trueop);
3684 logop->op_next = LINKLIST(falseop);
3687 /* establish postfix order */
3688 start = LINKLIST(first);
3689 first->op_next = (OP*)logop;
3691 first->op_sibling = trueop;
3692 trueop->op_sibling = falseop;
3693 o = newUNOP(OP_NULL, 0, (OP*)logop);
3695 trueop->op_next = falseop->op_next = o;
3702 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
3711 NewOp(1101, range, 1, LOGOP);
3713 range->op_type = OP_RANGE;
3714 range->op_ppaddr = PL_ppaddr[OP_RANGE];
3715 range->op_first = left;
3716 range->op_flags = OPf_KIDS;
3717 leftstart = LINKLIST(left);
3718 range->op_other = LINKLIST(right);
3719 range->op_private = 1 | (flags >> 8);
3721 left->op_sibling = right;
3723 range->op_next = (OP*)range;
3724 flip = newUNOP(OP_FLIP, flags, (OP*)range);
3725 flop = newUNOP(OP_FLOP, 0, flip);
3726 o = newUNOP(OP_NULL, 0, flop);
3728 range->op_next = leftstart;
3730 left->op_next = flip;
3731 right->op_next = flop;
3733 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3734 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
3735 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3736 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3738 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3739 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3742 if (!flip->op_private || !flop->op_private)
3743 linklist(o); /* blow off optimizer unless constant */
3749 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
3754 int once = block && block->op_flags & OPf_SPECIAL &&
3755 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3758 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3759 return block; /* do {} while 0 does once */
3760 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3761 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3762 expr = newUNOP(OP_DEFINED, 0,
3763 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3764 } else if (expr->op_flags & OPf_KIDS) {
3765 OP *k1 = ((UNOP*)expr)->op_first;
3766 OP *k2 = (k1) ? k1->op_sibling : NULL;
3767 switch (expr->op_type) {
3769 if (k2 && k2->op_type == OP_READLINE
3770 && (k2->op_flags & OPf_STACKED)
3771 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3772 expr = newUNOP(OP_DEFINED, 0, expr);
3776 if (k1->op_type == OP_READDIR
3777 || k1->op_type == OP_GLOB
3778 || (k1->op_type == OP_NULL && k1->op_targ == OP_NULL)
3779 || k1->op_type == OP_EACH)
3780 expr = newUNOP(OP_DEFINED, 0, expr);
3786 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3787 o = new_logop(OP_AND, 0, &expr, &listop);
3790 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3792 if (once && o != listop)
3793 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3796 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3798 o->op_flags |= flags;
3800 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3805 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
3815 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3816 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
3817 expr = newUNOP(OP_DEFINED, 0,
3818 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3819 } else if (expr && (expr->op_flags & OPf_KIDS)) {
3820 OP *k1 = ((UNOP*)expr)->op_first;
3821 OP *k2 = (k1) ? k1->op_sibling : NULL;
3822 switch (expr->op_type) {
3824 if (k2 && k2->op_type == OP_READLINE
3825 && (k2->op_flags & OPf_STACKED)
3826 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3827 expr = newUNOP(OP_DEFINED, 0, expr);
3831 if (k1->op_type == OP_READDIR
3832 || k1->op_type == OP_GLOB
3833 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3834 || k1->op_type == OP_EACH)
3835 expr = newUNOP(OP_DEFINED, 0, expr);
3841 block = newOP(OP_NULL, 0);
3843 block = scope(block);
3847 next = LINKLIST(cont);
3848 loopflags |= OPpLOOP_CONTINUE;
3851 OP *unstack = newOP(OP_UNSTACK, 0);
3854 cont = append_elem(OP_LINESEQ, cont, unstack);
3855 if ((line_t)whileline != NOLINE) {
3856 PL_copline = whileline;
3857 cont = append_elem(OP_LINESEQ, cont,
3858 newSTATEOP(0, Nullch, Nullop));
3862 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3863 redo = LINKLIST(listop);
3866 PL_copline = whileline;
3868 o = new_logop(OP_AND, 0, &expr, &listop);
3869 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3870 op_free(expr); /* oops, it's a while (0) */
3872 return Nullop; /* listop already freed by new_logop */
3875 ((LISTOP*)listop)->op_last->op_next = condop =
3876 (o == listop ? redo : LINKLIST(o));
3882 NewOp(1101,loop,1,LOOP);
3883 loop->op_type = OP_ENTERLOOP;
3884 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3885 loop->op_private = 0;
3886 loop->op_next = (OP*)loop;
3889 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3891 loop->op_redoop = redo;
3892 loop->op_lastop = o;
3893 o->op_private |= loopflags;
3896 loop->op_nextop = next;
3898 loop->op_nextop = o;
3900 o->op_flags |= flags;
3901 o->op_private |= (flags >> 8);
3906 Perl_newFOROP(pTHX_ I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
3914 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3915 sv->op_type = OP_RV2GV;
3916 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
3918 else if (sv->op_type == OP_PADSV) { /* private variable */
3919 padoff = sv->op_targ;
3924 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3925 padoff = sv->op_targ;
3927 iterflags |= OPf_SPECIAL;
3932 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
3936 padoff = find_threadsv("_");
3937 iterflags |= OPf_SPECIAL;
3939 sv = newGVOP(OP_GV, 0, PL_defgv);
3942 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3943 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3944 iterflags |= OPf_STACKED;
3946 else if (expr->op_type == OP_NULL &&
3947 (expr->op_flags & OPf_KIDS) &&
3948 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3950 /* Basically turn for($x..$y) into the same as for($x,$y), but we
3951 * set the STACKED flag to indicate that these values are to be
3952 * treated as min/max values by 'pp_iterinit'.
3954 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3955 LOGOP* range = (LOGOP*) flip->op_first;
3956 OP* left = range->op_first;
3957 OP* right = left->op_sibling;
3960 range->op_flags &= ~OPf_KIDS;
3961 range->op_first = Nullop;
3963 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3964 listop->op_first->op_next = range->op_next;
3965 left->op_next = range->op_other;
3966 right->op_next = (OP*)listop;
3967 listop->op_next = listop->op_first;
3970 expr = (OP*)(listop);
3972 iterflags |= OPf_STACKED;
3975 expr = mod(force_list(expr), OP_GREPSTART);
3979 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
3980 append_elem(OP_LIST, expr, scalar(sv))));
3981 assert(!loop->op_next);
3982 #ifdef PL_OP_SLAB_ALLOC
3985 NewOp(1234,tmp,1,LOOP);
3986 Copy(loop,tmp,1,LOOP);
3990 Renew(loop, 1, LOOP);
3992 loop->op_targ = padoff;
3993 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
3994 PL_copline = forline;
3995 return newSTATEOP(0, label, wop);
3999 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
4005 if (type != OP_GOTO || label->op_type == OP_CONST) {
4006 /* "last()" means "last" */
4007 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
4008 o = newOP(type, OPf_SPECIAL);
4010 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
4011 ? SvPVx(((SVOP*)label)->op_sv, n_a)
4017 if (label->op_type == OP_ENTERSUB)
4018 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
4019 o = newUNOP(type, OPf_STACKED, label);
4021 PL_hints |= HINT_BLOCK_SCOPE;
4026 Perl_cv_undef(pTHX_ CV *cv)
4031 MUTEX_DESTROY(CvMUTEXP(cv));
4032 Safefree(CvMUTEXP(cv));
4035 #endif /* USE_THREADS */
4037 if (!CvXSUB(cv) && CvROOT(cv)) {
4039 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
4040 Perl_croak(aTHX_ "Can't undef active subroutine");
4043 Perl_croak(aTHX_ "Can't undef active subroutine");
4044 #endif /* USE_THREADS */
4047 SAVEVPTR(PL_curpad);
4051 op_free(CvROOT(cv));
4052 CvROOT(cv) = Nullop;
4055 SvPOK_off((SV*)cv); /* forget prototype */
4057 SvREFCNT_dec(CvGV(cv));
4059 SvREFCNT_dec(CvOUTSIDE(cv));
4060 CvOUTSIDE(cv) = Nullcv;
4061 if (CvPADLIST(cv)) {
4062 /* may be during global destruction */
4063 if (SvREFCNT(CvPADLIST(cv))) {
4064 I32 i = AvFILLp(CvPADLIST(cv));
4066 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
4067 SV* sv = svp ? *svp : Nullsv;
4070 if (sv == (SV*)PL_comppad_name)
4071 PL_comppad_name = Nullav;
4072 else if (sv == (SV*)PL_comppad) {
4073 PL_comppad = Nullav;
4074 PL_curpad = Null(SV**);
4078 SvREFCNT_dec((SV*)CvPADLIST(cv));
4080 CvPADLIST(cv) = Nullav;
4085 S_cv_dump(pTHX_ CV *cv)
4088 CV *outside = CvOUTSIDE(cv);
4089 AV* padlist = CvPADLIST(cv);
4096 PerlIO_printf(Perl_debug_log,
4097 "\tCV=0x%"UVxf" (%s), OUTSIDE=0x%"UVxf" (%s)\n",
4099 (CvANON(cv) ? "ANON"
4100 : (cv == PL_main_cv) ? "MAIN"
4101 : CvUNIQUE(cv) ? "UNIQUE"
4102 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
4105 : CvANON(outside) ? "ANON"
4106 : (outside == PL_main_cv) ? "MAIN"
4107 : CvUNIQUE(outside) ? "UNIQUE"
4108 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
4113 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
4114 pad = (AV*)*av_fetch(padlist, 1, FALSE);
4115 pname = AvARRAY(pad_name);
4116 ppad = AvARRAY(pad);
4118 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
4119 if (SvPOK(pname[ix]))
4120 PerlIO_printf(Perl_debug_log,
4121 "\t%4d. 0x%"UVxf" (%s\"%s\" %"IVdf"-%"IVdf")\n",
4122 (int)ix, PTR2UV(ppad[ix]),
4123 SvFAKE(pname[ix]) ? "FAKE " : "",
4125 (IV)I_32(SvNVX(pname[ix])),
4128 #endif /* DEBUGGING */
4132 S_cv_clone2(pTHX_ CV *proto, CV *outside)
4137 AV* protopadlist = CvPADLIST(proto);
4138 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
4139 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
4140 SV** pname = AvARRAY(protopad_name);
4141 SV** ppad = AvARRAY(protopad);
4142 I32 fname = AvFILLp(protopad_name);
4143 I32 fpad = AvFILLp(protopad);
4147 assert(!CvUNIQUE(proto));
4151 SAVESPTR(PL_comppad_name);
4152 SAVESPTR(PL_compcv);
4154 cv = PL_compcv = (CV*)NEWSV(1104,0);
4155 sv_upgrade((SV *)cv, SvTYPE(proto));
4156 CvFLAGS(cv) = CvFLAGS(proto) & ~CVf_CLONE;
4160 New(666, CvMUTEXP(cv), 1, perl_mutex);
4161 MUTEX_INIT(CvMUTEXP(cv));
4163 #endif /* USE_THREADS */
4164 CvFILE(cv) = CvFILE(proto);
4165 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
4166 CvSTASH(cv) = CvSTASH(proto);
4167 CvROOT(cv) = CvROOT(proto);
4168 CvSTART(cv) = CvSTART(proto);
4170 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
4173 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
4175 PL_comppad_name = newAV();
4176 for (ix = fname; ix >= 0; ix--)
4177 av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix]));
4179 PL_comppad = newAV();
4181 comppadlist = newAV();
4182 AvREAL_off(comppadlist);
4183 av_store(comppadlist, 0, (SV*)PL_comppad_name);
4184 av_store(comppadlist, 1, (SV*)PL_comppad);
4185 CvPADLIST(cv) = comppadlist;
4186 av_fill(PL_comppad, AvFILLp(protopad));
4187 PL_curpad = AvARRAY(PL_comppad);
4189 av = newAV(); /* will be @_ */
4191 av_store(PL_comppad, 0, (SV*)av);
4192 AvFLAGS(av) = AVf_REIFY;
4194 for (ix = fpad; ix > 0; ix--) {
4195 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
4196 if (namesv && namesv != &PL_sv_undef) {
4197 char *name = SvPVX(namesv); /* XXX */
4198 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
4199 I32 off = pad_findlex(name, ix, SvIVX(namesv),
4200 CvOUTSIDE(cv), cxstack_ix, 0, 0);
4202 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
4204 Perl_croak(aTHX_ "panic: cv_clone: %s", name);
4206 else { /* our own lexical */
4209 /* anon code -- we'll come back for it */
4210 sv = SvREFCNT_inc(ppad[ix]);
4212 else if (*name == '@')
4214 else if (*name == '%')
4223 else if (IS_PADGV(ppad[ix]) || IS_PADCONST(ppad[ix])) {
4224 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
4227 SV* sv = NEWSV(0,0);
4233 /* Now that vars are all in place, clone nested closures. */
4235 for (ix = fpad; ix > 0; ix--) {
4236 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
4238 && namesv != &PL_sv_undef
4239 && !(SvFLAGS(namesv) & SVf_FAKE)
4240 && *SvPVX(namesv) == '&'
4241 && CvCLONE(ppad[ix]))
4243 CV *kid = cv_clone2((CV*)ppad[ix], cv);
4244 SvREFCNT_dec(ppad[ix]);
4247 PL_curpad[ix] = (SV*)kid;
4251 #ifdef DEBUG_CLOSURES
4252 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
4254 PerlIO_printf(Perl_debug_log, " from:\n");
4256 PerlIO_printf(Perl_debug_log, " to:\n");
4265 Perl_cv_clone(pTHX_ CV *proto)
4268 LOCK_CRED_MUTEX; /* XXX create separate mutex */
4269 cv = cv_clone2(proto, CvOUTSIDE(proto));
4270 UNLOCK_CRED_MUTEX; /* XXX create separate mutex */
4275 Perl_cv_ckproto(pTHX_ CV *cv, GV *gv, char *p)
4279 if (((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) && ckWARN_d(WARN_PROTOTYPE)) {
4280 SV* msg = sv_newmortal();
4284 gv_efullname3(name = sv_newmortal(), gv, Nullch);
4285 sv_setpv(msg, "Prototype mismatch:");
4287 Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, name);
4289 Perl_sv_catpvf(aTHX_ msg, " (%s)", SvPVX(cv));
4290 sv_catpv(msg, " vs ");
4292 Perl_sv_catpvf(aTHX_ msg, "(%s)", p);
4294 sv_catpv(msg, "none");
4295 Perl_warner(aTHX_ WARN_PROTOTYPE, "%"SVf, msg);
4300 Perl_cv_const_sv(pTHX_ CV *cv)
4302 if (!cv || !SvPOK(cv) || SvCUR(cv))
4304 return op_const_sv(CvSTART(cv), cv);
4308 Perl_op_const_sv(pTHX_ OP *o, CV *cv)
4315 if (o->op_type == OP_LINESEQ && cLISTOPo->op_first)
4316 o = cLISTOPo->op_first->op_sibling;
4318 for (; o; o = o->op_next) {
4319 OPCODE type = o->op_type;
4321 if (sv && o->op_next == o)
4323 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
4325 if (type == OP_LEAVESUB || type == OP_RETURN)
4329 if (type == OP_CONST && cSVOPo->op_sv)
4331 else if ((type == OP_PADSV || type == OP_CONST) && cv) {
4332 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
4333 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
4334 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
4346 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4356 Perl_croak(aTHX_ "\"my sub\" not yet implemented");
4360 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
4362 return Perl_newATTRSUB(aTHX_ floor, o, proto, Nullop, block);
4366 Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4373 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, n_a) : Nullch;
4377 name = o ? SvPVx(cSVOPo->op_sv, n_a) : Nullch;
4378 if (!name && PERLDB_NAMEANON && CopLINE(PL_curcop)) {
4379 SV *sv = sv_newmortal();
4380 Perl_sv_setpvf(aTHX_ sv, "__ANON__[%s:%"IVdf"]",
4381 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
4386 gv = gv_fetchpv(name ? name : (aname ? aname : "__ANON__"),
4387 GV_ADDMULTI | ((block || attrs) ? 0 : GV_NOINIT),
4397 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
4398 maximum a prototype before. */
4399 if (SvTYPE(gv) > SVt_NULL) {
4400 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1)
4401 && ckWARN_d(WARN_PROTOTYPE))
4403 Perl_warner(aTHX_ WARN_PROTOTYPE, "Runaway prototype");
4405 cv_ckproto((CV*)gv, NULL, ps);
4408 sv_setpv((SV*)gv, ps);
4410 sv_setiv((SV*)gv, -1);
4411 SvREFCNT_dec(PL_compcv);
4412 cv = PL_compcv = NULL;
4413 PL_sub_generation++;
4417 if (!name || GvCVGEN(gv))
4419 else if ((cv = GvCV(gv))) {
4420 cv_ckproto(cv, gv, ps);
4421 /* already defined (or promised)? */
4422 if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4424 bool const_changed = TRUE;
4425 if (!block && !attrs) {
4426 /* just a "sub foo;" when &foo is already defined */
4427 SAVEFREESV(PL_compcv);
4430 /* ahem, death to those who redefine active sort subs */
4431 if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv))
4432 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", name);
4435 if ((const_sv = cv_const_sv(cv)))
4436 const_changed = sv_cmp(const_sv, op_const_sv(block, Nullcv));
4437 if ((const_sv || const_changed) && ckWARN(WARN_REDEFINE))
4439 line_t oldline = CopLINE(PL_curcop);
4440 CopLINE_set(PL_curcop, PL_copline);
4441 Perl_warner(aTHX_ WARN_REDEFINE,
4442 const_sv ? "Constant subroutine %s redefined"
4443 : "Subroutine %s redefined", name);
4444 CopLINE_set(PL_curcop, oldline);
4455 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>
4456 * before we clobber PL_compcv.
4460 if (CvGV(cv) && GvSTASH(CvGV(cv)) && HvNAME(GvSTASH(CvGV(cv))))
4461 stash = GvSTASH(CvGV(cv));
4462 else if (CvSTASH(cv) && HvNAME(CvSTASH(cv)))
4463 stash = CvSTASH(cv);
4465 stash = PL_curstash;
4468 /* possibly about to re-define existing subr -- ignore old cv */
4469 rcv = (SV*)PL_compcv;
4470 if (name && GvSTASH(gv) && HvNAME(GvSTASH(gv)))
4471 stash = GvSTASH(gv);
4473 stash = PL_curstash;
4475 apply_attrs(stash, rcv, attrs);
4477 if (cv) { /* must reuse cv if autoloaded */
4479 /* got here with just attrs -- work done, so bug out */
4480 SAVEFREESV(PL_compcv);
4484 CvFLAGS(cv) = CvFLAGS(PL_compcv);
4485 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
4486 CvOUTSIDE(PL_compcv) = 0;
4487 CvPADLIST(cv) = CvPADLIST(PL_compcv);
4488 CvPADLIST(PL_compcv) = 0;
4489 if (SvREFCNT(PL_compcv) > 1) /* XXX Make closures transit through stub. */
4490 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc((SV*)cv);
4491 SvREFCNT_dec(PL_compcv);
4498 PL_sub_generation++;
4501 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4502 CvFILE(cv) = CopFILE(PL_curcop);
4503 CvSTASH(cv) = PL_curstash;
4506 if (!CvMUTEXP(cv)) {
4507 New(666, CvMUTEXP(cv), 1, perl_mutex);
4508 MUTEX_INIT(CvMUTEXP(cv));
4510 #endif /* USE_THREADS */
4513 sv_setpv((SV*)cv, ps);
4515 if (PL_error_count) {
4519 char *s = strrchr(name, ':');
4521 if (strEQ(s, "BEGIN")) {
4523 "BEGIN not safe after errors--compilation aborted";
4524 if (PL_in_eval & EVAL_KEEPERR)
4525 Perl_croak(aTHX_ not_safe);
4527 /* force display of errors found but not reported */
4528 sv_catpv(ERRSV, not_safe);
4529 Perl_croak(aTHX_ "%s", SvPVx(ERRSV, n_a));
4536 PL_copline = NOLINE;
4541 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad))
4542 av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv);
4545 CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0, scalarseq(block));
4548 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4550 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4551 OpREFCNT_set(CvROOT(cv), 1);
4552 CvSTART(cv) = LINKLIST(CvROOT(cv));
4553 CvROOT(cv)->op_next = 0;
4556 /* now that optimizer has done its work, adjust pad values */
4558 SV **namep = AvARRAY(PL_comppad_name);
4559 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4562 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix]))
4565 * The only things that a clonable function needs in its
4566 * pad are references to outer lexicals and anonymous subs.
4567 * The rest are created anew during cloning.
4569 if (!((namesv = namep[ix]) != Nullsv &&
4570 namesv != &PL_sv_undef &&
4572 *SvPVX(namesv) == '&')))
4574 SvREFCNT_dec(PL_curpad[ix]);
4575 PL_curpad[ix] = Nullsv;
4580 AV *av = newAV(); /* Will be @_ */
4582 av_store(PL_comppad, 0, (SV*)av);
4583 AvFLAGS(av) = AVf_REIFY;
4585 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4586 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix]))
4588 if (!SvPADMY(PL_curpad[ix]))
4589 SvPADTMP_on(PL_curpad[ix]);
4593 if (name || aname) {
4595 char *tname = (name ? name : aname);
4597 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4598 SV *sv = NEWSV(0,0);
4599 SV *tmpstr = sv_newmortal();
4600 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
4604 Perl_sv_setpvf(aTHX_ sv, "%s:%ld-%ld",
4606 (long)PL_subline, (long)CopLINE(PL_curcop));
4607 gv_efullname3(tmpstr, gv, Nullch);
4608 hv_store(GvHV(PL_DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
4609 hv = GvHVn(db_postponed);
4610 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
4611 && (pcv = GvCV(db_postponed)))
4617 call_sv((SV*)pcv, G_DISCARD);
4621 if ((s = strrchr(tname,':')))
4626 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4629 if (strEQ(s, "BEGIN")) {
4630 I32 oldscope = PL_scopestack_ix;
4632 SAVECOPFILE(&PL_compiling);
4633 SAVECOPLINE(&PL_compiling);
4635 sv_setsv(PL_rs, PL_nrs);
4638 PL_beginav = newAV();
4639 DEBUG_x( dump_sub(gv) );
4640 av_push(PL_beginav, SvREFCNT_inc(cv));
4642 call_list(oldscope, PL_beginav);
4644 PL_curcop = &PL_compiling;
4645 PL_compiling.op_private = PL_hints;
4648 else if (strEQ(s, "END") && !PL_error_count) {
4651 DEBUG_x( dump_sub(gv) );
4652 av_unshift(PL_endav, 1);
4653 av_store(PL_endav, 0, SvREFCNT_inc(cv));
4656 else if (strEQ(s, "CHECK") && !PL_error_count) {
4658 PL_checkav = newAV();
4659 DEBUG_x( dump_sub(gv) );
4660 if (PL_main_start && ckWARN(WARN_VOID))
4661 Perl_warner(aTHX_ WARN_VOID, "Too late to run CHECK block");
4662 av_unshift(PL_checkav, 1);
4663 av_store(PL_checkav, 0, SvREFCNT_inc(cv));
4666 else if (strEQ(s, "INIT") && !PL_error_count) {
4668 PL_initav = newAV();
4669 DEBUG_x( dump_sub(gv) );
4670 if (PL_main_start && ckWARN(WARN_VOID))
4671 Perl_warner(aTHX_ WARN_VOID, "Too late to run INIT block");
4672 av_push(PL_initav, SvREFCNT_inc(cv));
4678 PL_copline = NOLINE;
4683 /* XXX unsafe for threads if eval_owner isn't held */
4685 =for apidoc newCONSTSUB
4687 Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is
4688 eligible for inlining at compile-time.
4694 Perl_newCONSTSUB(pTHX_ HV *stash, char *name, SV *sv)
4699 SAVECOPLINE(PL_curcop);
4702 CopLINE_set(PL_curcop, PL_copline);
4703 PL_hints &= ~HINT_BLOCK_SCOPE;
4706 SAVESPTR(PL_curstash);
4707 SAVECOPSTASH(PL_curcop);
4708 PL_curstash = stash;
4710 CopSTASHPV(PL_curcop) = stash ? HvNAME(stash) : Nullch;
4712 CopSTASH(PL_curcop) = stash;
4717 start_subparse(FALSE, 0),
4718 newSVOP(OP_CONST, 0, newSVpv(name,0)),
4719 newSVOP(OP_CONST, 0, &PL_sv_no), /* SvPV(&PL_sv_no) == "" -- GMB */
4721 newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv))
4728 =for apidoc U||newXS
4730 Used by C<xsubpp> to hook up XSUBs as Perl subs.
4736 Perl_newXS(pTHX_ char *name, XSUBADDR_t subaddr, char *filename)
4739 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
4742 if ((cv = (name ? GvCV(gv) : Nullcv))) {
4744 /* just a cached method */
4748 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4749 /* already defined (or promised) */
4750 if (ckWARN(WARN_REDEFINE) && !(CvGV(cv) && GvSTASH(CvGV(cv))
4751 && HvNAME(GvSTASH(CvGV(cv)))
4752 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
4753 line_t oldline = CopLINE(PL_curcop);
4754 if (PL_copline != NOLINE)
4755 CopLINE_set(PL_curcop, PL_copline);
4756 Perl_warner(aTHX_ WARN_REDEFINE, "Subroutine %s redefined",name);
4757 CopLINE_set(PL_curcop, oldline);
4764 if (cv) /* must reuse cv if autoloaded */
4767 cv = (CV*)NEWSV(1105,0);
4768 sv_upgrade((SV *)cv, SVt_PVCV);
4772 PL_sub_generation++;
4775 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4777 New(666, CvMUTEXP(cv), 1, perl_mutex);
4778 MUTEX_INIT(CvMUTEXP(cv));
4780 #endif /* USE_THREADS */
4781 (void)gv_fetchfile(filename);
4782 CvFILE(cv) = filename; /* NOTE: not copied, as it is expected to be
4783 an external constant string */
4784 CvXSUB(cv) = subaddr;
4787 char *s = strrchr(name,':');
4793 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4796 if (strEQ(s, "BEGIN")) {
4798 PL_beginav = newAV();
4799 av_push(PL_beginav, SvREFCNT_inc(cv));
4802 else if (strEQ(s, "END")) {
4805 av_unshift(PL_endav, 1);
4806 av_store(PL_endav, 0, SvREFCNT_inc(cv));
4809 else if (strEQ(s, "CHECK")) {
4811 PL_checkav = newAV();
4812 if (PL_main_start && ckWARN(WARN_VOID))
4813 Perl_warner(aTHX_ WARN_VOID, "Too late to run CHECK block");
4814 av_unshift(PL_checkav, 1);
4815 av_store(PL_checkav, 0, SvREFCNT_inc(cv));
4818 else if (strEQ(s, "INIT")) {
4820 PL_initav = newAV();
4821 if (PL_main_start && ckWARN(WARN_VOID))
4822 Perl_warner(aTHX_ WARN_VOID, "Too late to run INIT block");
4823 av_push(PL_initav, SvREFCNT_inc(cv));
4835 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
4845 name = SvPVx(cSVOPo->op_sv, n_a);
4848 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
4850 if ((cv = GvFORM(gv))) {
4851 if (ckWARN(WARN_REDEFINE)) {
4852 line_t oldline = CopLINE(PL_curcop);
4854 CopLINE_set(PL_curcop, PL_copline);
4855 Perl_warner(aTHX_ WARN_REDEFINE, "Format %s redefined",name);
4856 CopLINE_set(PL_curcop, oldline);
4862 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4863 CvFILE(cv) = CopFILE(PL_curcop);
4865 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4866 if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix]))
4867 SvPADTMP_on(PL_curpad[ix]);
4870 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4871 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4872 OpREFCNT_set(CvROOT(cv), 1);
4873 CvSTART(cv) = LINKLIST(CvROOT(cv));
4874 CvROOT(cv)->op_next = 0;
4877 PL_copline = NOLINE;
4882 Perl_newANONLIST(pTHX_ OP *o)
4884 return newUNOP(OP_REFGEN, 0,
4885 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
4889 Perl_newANONHASH(pTHX_ OP *o)
4891 return newUNOP(OP_REFGEN, 0,
4892 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
4896 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
4898 return newANONATTRSUB(floor, proto, Nullop, block);
4902 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
4904 return newUNOP(OP_REFGEN, 0,
4905 newSVOP(OP_ANONCODE, 0,
4906 (SV*)newATTRSUB(floor, 0, proto, attrs, block)));
4910 Perl_oopsAV(pTHX_ OP *o)
4912 switch (o->op_type) {
4914 o->op_type = OP_PADAV;
4915 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4916 return ref(o, OP_RV2AV);
4919 o->op_type = OP_RV2AV;
4920 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4925 if (ckWARN_d(WARN_INTERNAL))
4926 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsAV");
4933 Perl_oopsHV(pTHX_ OP *o)
4937 switch (o->op_type) {
4940 o->op_type = OP_PADHV;
4941 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4942 return ref(o, OP_RV2HV);
4946 o->op_type = OP_RV2HV;
4947 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
4952 if (ckWARN_d(WARN_INTERNAL))
4953 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsHV");
4960 Perl_newAVREF(pTHX_ OP *o)
4962 if (o->op_type == OP_PADANY) {
4963 o->op_type = OP_PADAV;
4964 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4967 return newUNOP(OP_RV2AV, 0, scalar(o));
4971 Perl_newGVREF(pTHX_ I32 type, OP *o)
4973 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
4974 return newUNOP(OP_NULL, 0, o);
4975 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
4979 Perl_newHVREF(pTHX_ OP *o)
4981 if (o->op_type == OP_PADANY) {
4982 o->op_type = OP_PADHV;
4983 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4986 return newUNOP(OP_RV2HV, 0, scalar(o));
4990 Perl_oopsCV(pTHX_ OP *o)
4992 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
4998 Perl_newCVREF(pTHX_ I32 flags, OP *o)
5000 return newUNOP(OP_RV2CV, flags, scalar(o));
5004 Perl_newSVREF(pTHX_ OP *o)
5006 if (o->op_type == OP_PADANY) {
5007 o->op_type = OP_PADSV;
5008 o->op_ppaddr = PL_ppaddr[OP_PADSV];
5011 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
5012 o->op_flags |= OPpDONE_SVREF;
5015 return newUNOP(OP_RV2SV, 0, scalar(o));
5018 /* Check routines. */
5021 Perl_ck_anoncode(pTHX_ OP *o)
5026 name = NEWSV(1106,0);
5027 sv_upgrade(name, SVt_PVNV);
5028 sv_setpvn(name, "&", 1);
5031 ix = pad_alloc(o->op_type, SVs_PADMY);
5032 av_store(PL_comppad_name, ix, name);
5033 av_store(PL_comppad, ix, cSVOPo->op_sv);
5034 SvPADMY_on(cSVOPo->op_sv);
5035 cSVOPo->op_sv = Nullsv;
5036 cSVOPo->op_targ = ix;
5041 Perl_ck_bitop(pTHX_ OP *o)
5043 o->op_private = PL_hints;
5048 Perl_ck_concat(pTHX_ OP *o)
5050 if (cUNOPo->op_first->op_type == OP_CONCAT)
5051 o->op_flags |= OPf_STACKED;
5056 Perl_ck_spair(pTHX_ OP *o)
5058 if (o->op_flags & OPf_KIDS) {
5061 OPCODE type = o->op_type;
5062 o = modkids(ck_fun(o), type);
5063 kid = cUNOPo->op_first;
5064 newop = kUNOP->op_first->op_sibling;
5066 (newop->op_sibling ||
5067 !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
5068 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
5069 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
5073 op_free(kUNOP->op_first);
5074 kUNOP->op_first = newop;
5076 o->op_ppaddr = PL_ppaddr[++o->op_type];
5081 Perl_ck_delete(pTHX_ OP *o)
5085 if (o->op_flags & OPf_KIDS) {
5086 OP *kid = cUNOPo->op_first;
5087 switch (kid->op_type) {
5089 o->op_flags |= OPf_SPECIAL;
5092 o->op_private |= OPpSLICE;
5095 o->op_flags |= OPf_SPECIAL;
5100 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element or slice",
5101 PL_op_desc[o->op_type]);
5109 Perl_ck_eof(pTHX_ OP *o)
5111 I32 type = o->op_type;
5113 if (o->op_flags & OPf_KIDS) {
5114 if (cLISTOPo->op_first->op_type == OP_STUB) {
5116 o = newUNOP(type, OPf_SPECIAL,
5117 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
5125 Perl_ck_eval(pTHX_ OP *o)
5127 PL_hints |= HINT_BLOCK_SCOPE;
5128 if (o->op_flags & OPf_KIDS) {
5129 SVOP *kid = (SVOP*)cUNOPo->op_first;
5132 o->op_flags &= ~OPf_KIDS;
5135 else if (kid->op_type == OP_LINESEQ) {
5138 kid->op_next = o->op_next;
5139 cUNOPo->op_first = 0;
5142 NewOp(1101, enter, 1, LOGOP);
5143 enter->op_type = OP_ENTERTRY;
5144 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
5145 enter->op_private = 0;
5147 /* establish postfix order */
5148 enter->op_next = (OP*)enter;
5150 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
5151 o->op_type = OP_LEAVETRY;
5152 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
5153 enter->op_other = o;
5161 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
5163 o->op_targ = (PADOFFSET)PL_hints;
5168 Perl_ck_exit(pTHX_ OP *o)
5171 HV *table = GvHV(PL_hintgv);
5173 SV **svp = hv_fetch(table, "vmsish_exit", 11, FALSE);
5174 if (svp && *svp && SvTRUE(*svp))
5175 o->op_private |= OPpEXIT_VMSISH;
5182 Perl_ck_exec(pTHX_ OP *o)
5185 if (o->op_flags & OPf_STACKED) {
5187 kid = cUNOPo->op_first->op_sibling;
5188 if (kid->op_type == OP_RV2GV)
5197 Perl_ck_exists(pTHX_ OP *o)
5200 if (o->op_flags & OPf_KIDS) {
5201 OP *kid = cUNOPo->op_first;
5202 if (kid->op_type == OP_ENTERSUB) {
5203 (void) ref(kid, o->op_type);
5204 if (kid->op_type != OP_RV2CV && !PL_error_count)
5205 Perl_croak(aTHX_ "%s argument is not a subroutine name",
5206 PL_op_desc[o->op_type]);
5207 o->op_private |= OPpEXISTS_SUB;
5209 else if (kid->op_type == OP_AELEM)
5210 o->op_flags |= OPf_SPECIAL;
5211 else if (kid->op_type != OP_HELEM)
5212 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element",
5213 PL_op_desc[o->op_type]);
5221 Perl_ck_gvconst(pTHX_ register OP *o)
5223 o = fold_constants(o);
5224 if (o->op_type == OP_CONST)
5231 Perl_ck_rvconst(pTHX_ register OP *o)
5234 SVOP *kid = (SVOP*)cUNOPo->op_first;
5236 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5237 if (kid->op_type == OP_CONST) {
5241 SV *kidsv = kid->op_sv;
5244 /* Is it a constant from cv_const_sv()? */
5245 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
5246 SV *rsv = SvRV(kidsv);
5247 int svtype = SvTYPE(rsv);
5248 char *badtype = Nullch;
5250 switch (o->op_type) {
5252 if (svtype > SVt_PVMG)
5253 badtype = "a SCALAR";
5256 if (svtype != SVt_PVAV)
5257 badtype = "an ARRAY";
5260 if (svtype != SVt_PVHV) {
5261 if (svtype == SVt_PVAV) { /* pseudohash? */
5262 SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
5263 if (ksv && SvROK(*ksv)
5264 && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
5273 if (svtype != SVt_PVCV)
5278 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
5281 name = SvPV(kidsv, n_a);
5282 if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
5283 char *badthing = Nullch;
5284 switch (o->op_type) {
5286 badthing = "a SCALAR";
5289 badthing = "an ARRAY";
5292 badthing = "a HASH";
5297 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
5301 * This is a little tricky. We only want to add the symbol if we
5302 * didn't add it in the lexer. Otherwise we get duplicate strict
5303 * warnings. But if we didn't add it in the lexer, we must at
5304 * least pretend like we wanted to add it even if it existed before,
5305 * or we get possible typo warnings. OPpCONST_ENTERED says
5306 * whether the lexer already added THIS instance of this symbol.
5308 iscv = (o->op_type == OP_RV2CV) * 2;
5310 gv = gv_fetchpv(name,
5311 iscv | !(kid->op_private & OPpCONST_ENTERED),
5314 : o->op_type == OP_RV2SV
5316 : o->op_type == OP_RV2AV
5318 : o->op_type == OP_RV2HV
5321 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
5323 kid->op_type = OP_GV;
5324 SvREFCNT_dec(kid->op_sv);
5326 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
5327 kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
5329 PL_curpad[kPADOP->op_padix] = SvREFCNT_inc(gv);
5331 kid->op_sv = SvREFCNT_inc(gv);
5333 kid->op_ppaddr = PL_ppaddr[OP_GV];
5340 Perl_ck_ftst(pTHX_ OP *o)
5343 I32 type = o->op_type;
5345 if (o->op_flags & OPf_REF) {
5348 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
5349 SVOP *kid = (SVOP*)cUNOPo->op_first;
5351 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5353 OP *newop = newGVOP(type, OPf_REF,
5354 gv_fetchpv(SvPVx(kid->op_sv, n_a), TRUE, SVt_PVIO));
5361 if (type == OP_FTTTY)
5362 o = newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
5365 o = newUNOP(type, 0, newDEFSVOP());
5368 if (type == OP_FTTEXT || type == OP_FTBINARY) {
5370 if (PL_hints & HINT_LOCALE)
5371 o->op_private |= OPpLOCALE;
5378 Perl_ck_fun(pTHX_ OP *o)
5385 int type = o->op_type;
5386 register I32 oa = PL_opargs[type] >> OASHIFT;
5388 if (o->op_flags & OPf_STACKED) {
5389 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
5392 return no_fh_allowed(o);
5395 if (o->op_flags & OPf_KIDS) {
5397 tokid = &cLISTOPo->op_first;
5398 kid = cLISTOPo->op_first;
5399 if (kid->op_type == OP_PUSHMARK ||
5400 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
5402 tokid = &kid->op_sibling;
5403 kid = kid->op_sibling;
5405 if (!kid && PL_opargs[type] & OA_DEFGV)
5406 *tokid = kid = newDEFSVOP();
5410 sibl = kid->op_sibling;
5413 /* list seen where single (scalar) arg expected? */
5414 if (numargs == 1 && !(oa >> 4)
5415 && kid->op_type == OP_LIST && type != OP_SCALAR)
5417 return too_many_arguments(o,PL_op_desc[type]);
5430 if (kid->op_type == OP_CONST &&
5431 (kid->op_private & OPpCONST_BARE))
5433 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5434 OP *newop = newAVREF(newGVOP(OP_GV, 0,
5435 gv_fetchpv(name, TRUE, SVt_PVAV) ));
5436 if (ckWARN(WARN_DEPRECATED))
5437 Perl_warner(aTHX_ WARN_DEPRECATED,
5438 "Array @%s missing the @ in argument %"IVdf" of %s()",
5439 name, (IV)numargs, PL_op_desc[type]);
5442 kid->op_sibling = sibl;
5445 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
5446 bad_type(numargs, "array", PL_op_desc[type], kid);
5450 if (kid->op_type == OP_CONST &&
5451 (kid->op_private & OPpCONST_BARE))
5453 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5454 OP *newop = newHVREF(newGVOP(OP_GV, 0,
5455 gv_fetchpv(name, TRUE, SVt_PVHV) ));
5456 if (ckWARN(WARN_DEPRECATED))
5457 Perl_warner(aTHX_ WARN_DEPRECATED,
5458 "Hash %%%s missing the %% in argument %"IVdf" of %s()",
5459 name, (IV)numargs, PL_op_desc[type]);
5462 kid->op_sibling = sibl;
5465 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
5466 bad_type(numargs, "hash", PL_op_desc[type], kid);
5471 OP *newop = newUNOP(OP_NULL, 0, kid);
5472 kid->op_sibling = 0;
5474 newop->op_next = newop;
5476 kid->op_sibling = sibl;
5481 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
5482 if (kid->op_type == OP_CONST &&
5483 (kid->op_private & OPpCONST_BARE))
5485 OP *newop = newGVOP(OP_GV, 0,
5486 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, n_a), TRUE,
5491 else if (kid->op_type == OP_READLINE) {
5492 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
5493 bad_type(numargs, "HANDLE", PL_op_desc[o->op_type], kid);
5496 I32 flags = OPf_SPECIAL;
5500 /* is this op a FH constructor? */
5501 if (is_handle_constructor(o,numargs)) {
5502 char *name = Nullch;
5506 /* Set a flag to tell rv2gv to vivify
5507 * need to "prove" flag does not mean something
5508 * else already - NI-S 1999/05/07
5511 if (kid->op_type == OP_PADSV) {
5512 SV **namep = av_fetch(PL_comppad_name,
5514 if (namep && *namep)
5515 name = SvPV(*namep, len);
5517 else if (kid->op_type == OP_RV2SV
5518 && kUNOP->op_first->op_type == OP_GV)
5520 GV *gv = cGVOPx_gv(kUNOP->op_first);
5522 len = GvNAMELEN(gv);
5524 else if (kid->op_type == OP_AELEM
5525 || kid->op_type == OP_HELEM)
5527 name = "__ANONIO__";
5533 targ = pad_alloc(OP_RV2GV, SVs_PADTMP);
5534 namesv = PL_curpad[targ];
5535 (void)SvUPGRADE(namesv, SVt_PV);
5537 sv_setpvn(namesv, "$", 1);
5538 sv_catpvn(namesv, name, len);
5541 kid->op_sibling = 0;
5542 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
5543 kid->op_targ = targ;
5544 kid->op_private |= priv;
5546 kid->op_sibling = sibl;
5552 mod(scalar(kid), type);
5556 tokid = &kid->op_sibling;
5557 kid = kid->op_sibling;
5559 o->op_private |= numargs;
5561 return too_many_arguments(o,PL_op_desc[o->op_type]);
5564 else if (PL_opargs[type] & OA_DEFGV) {
5566 return newUNOP(type, 0, newDEFSVOP());
5570 while (oa & OA_OPTIONAL)
5572 if (oa && oa != OA_LIST)
5573 return too_few_arguments(o,PL_op_desc[o->op_type]);
5579 Perl_ck_glob(pTHX_ OP *o)
5584 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
5585 append_elem(OP_GLOB, o, newDEFSVOP());
5587 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
5588 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5590 #if !defined(PERL_EXTERNAL_GLOB)
5591 /* XXX this can be tightened up and made more failsafe. */
5594 Perl_load_module(aTHX_ 0, newSVpvn("File::Glob", 10), Nullsv,
5595 /* null-terminated import list */
5596 newSVpvn(":globally", 9), Nullsv);
5597 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5600 #endif /* PERL_EXTERNAL_GLOB */
5602 if (gv && GvIMPORTED_CV(gv)) {
5603 append_elem(OP_GLOB, o,
5604 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
5605 o->op_type = OP_LIST;
5606 o->op_ppaddr = PL_ppaddr[OP_LIST];
5607 cLISTOPo->op_first->op_type = OP_PUSHMARK;
5608 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
5609 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
5610 append_elem(OP_LIST, o,
5611 scalar(newUNOP(OP_RV2CV, 0,
5612 newGVOP(OP_GV, 0, gv)))));
5613 o = newUNOP(OP_NULL, 0, ck_subr(o));
5614 o->op_targ = OP_GLOB; /* hint at what it used to be */
5617 gv = newGVgen("main");
5619 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
5625 Perl_ck_grep(pTHX_ OP *o)
5629 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
5631 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
5632 NewOp(1101, gwop, 1, LOGOP);
5634 if (o->op_flags & OPf_STACKED) {
5637 kid = cLISTOPo->op_first->op_sibling;
5638 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
5641 kid->op_next = (OP*)gwop;
5642 o->op_flags &= ~OPf_STACKED;
5644 kid = cLISTOPo->op_first->op_sibling;
5645 if (type == OP_MAPWHILE)
5652 kid = cLISTOPo->op_first->op_sibling;
5653 if (kid->op_type != OP_NULL)
5654 Perl_croak(aTHX_ "panic: ck_grep");
5655 kid = kUNOP->op_first;
5657 gwop->op_type = type;
5658 gwop->op_ppaddr = PL_ppaddr[type];
5659 gwop->op_first = listkids(o);
5660 gwop->op_flags |= OPf_KIDS;
5661 gwop->op_private = 1;
5662 gwop->op_other = LINKLIST(kid);
5663 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
5664 kid->op_next = (OP*)gwop;
5666 kid = cLISTOPo->op_first->op_sibling;
5667 if (!kid || !kid->op_sibling)
5668 return too_few_arguments(o,PL_op_desc[o->op_type]);
5669 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
5670 mod(kid, OP_GREPSTART);
5676 Perl_ck_index(pTHX_ OP *o)
5678 if (o->op_flags & OPf_KIDS) {
5679 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5681 kid = kid->op_sibling; /* get past "big" */
5682 if (kid && kid->op_type == OP_CONST)
5683 fbm_compile(((SVOP*)kid)->op_sv, 0);
5689 Perl_ck_lengthconst(pTHX_ OP *o)
5691 /* XXX length optimization goes here */
5696 Perl_ck_lfun(pTHX_ OP *o)
5698 OPCODE type = o->op_type;
5699 return modkids(ck_fun(o), type);
5703 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
5706 if ((o->op_flags & OPf_KIDS) && ckWARN(WARN_DEPRECATED)) {
5707 switch (cUNOPo->op_first->op_type) {
5709 break; /* Globals via GV can be undef */
5711 case OP_AASSIGN: /* Is this a good idea? */
5712 Perl_warner(aTHX_ WARN_DEPRECATED,
5713 "defined(@array) is deprecated");
5714 Perl_warner(aTHX_ WARN_DEPRECATED,
5715 "\t(Maybe you should just omit the defined()?)\n");
5718 break; /* Globals via GV can be undef */
5720 Perl_warner(aTHX_ WARN_DEPRECATED,
5721 "defined(%%hash) is deprecated");
5722 Perl_warner(aTHX_ WARN_DEPRECATED,
5723 "\t(Maybe you should just omit the defined()?)\n");
5734 Perl_ck_rfun(pTHX_ OP *o)
5736 OPCODE type = o->op_type;
5737 return refkids(ck_fun(o), type);
5741 Perl_ck_listiob(pTHX_ OP *o)
5745 kid = cLISTOPo->op_first;
5748 kid = cLISTOPo->op_first;
5750 if (kid->op_type == OP_PUSHMARK)
5751 kid = kid->op_sibling;
5752 if (kid && o->op_flags & OPf_STACKED)
5753 kid = kid->op_sibling;
5754 else if (kid && !kid->op_sibling) { /* print HANDLE; */
5755 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
5756 o->op_flags |= OPf_STACKED; /* make it a filehandle */
5757 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
5758 cLISTOPo->op_first->op_sibling = kid;
5759 cLISTOPo->op_last = kid;
5760 kid = kid->op_sibling;
5765 append_elem(o->op_type, o, newDEFSVOP());
5771 if (PL_hints & HINT_LOCALE)
5772 o->op_private |= OPpLOCALE;
5779 Perl_ck_fun_locale(pTHX_ OP *o)
5785 if (PL_hints & HINT_LOCALE)
5786 o->op_private |= OPpLOCALE;
5793 Perl_ck_sassign(pTHX_ OP *o)
5795 OP *kid = cLISTOPo->op_first;
5796 /* has a disposable target? */
5797 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
5798 && !(kid->op_flags & OPf_STACKED)
5799 /* Cannot steal the second time! */
5800 && !(kid->op_private & OPpTARGET_MY))
5802 OP *kkid = kid->op_sibling;
5804 /* Can just relocate the target. */
5805 if (kkid && kkid->op_type == OP_PADSV
5806 && !(kkid->op_private & OPpLVAL_INTRO))
5808 kid->op_targ = kkid->op_targ;
5810 /* Now we do not need PADSV and SASSIGN. */
5811 kid->op_sibling = o->op_sibling; /* NULL */
5812 cLISTOPo->op_first = NULL;
5815 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
5823 Perl_ck_scmp(pTHX_ OP *o)
5827 if (PL_hints & HINT_LOCALE)
5828 o->op_private |= OPpLOCALE;
5835 Perl_ck_match(pTHX_ OP *o)
5837 o->op_private |= OPpRUNTIME;
5842 Perl_ck_method(pTHX_ OP *o)
5844 OP *kid = cUNOPo->op_first;
5845 if (kid->op_type == OP_CONST) {
5846 SV* sv = kSVOP->op_sv;
5847 if (!(strchr(SvPVX(sv), ':') || strchr(SvPVX(sv), '\''))) {
5849 (void)SvUPGRADE(sv, SVt_PVIV);
5851 PERL_HASH(SvUVX(sv), SvPVX(sv), SvCUR(sv));
5852 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
5853 kSVOP->op_sv = Nullsv;
5862 Perl_ck_null(pTHX_ OP *o)
5868 Perl_ck_open(pTHX_ OP *o)
5870 HV *table = GvHV(PL_hintgv);
5874 svp = hv_fetch(table, "open_IN", 7, FALSE);
5876 mode = mode_from_discipline(*svp);
5877 if (mode & O_BINARY)
5878 o->op_private |= OPpOPEN_IN_RAW;
5879 else if (mode & O_TEXT)
5880 o->op_private |= OPpOPEN_IN_CRLF;
5883 svp = hv_fetch(table, "open_OUT", 8, FALSE);
5885 mode = mode_from_discipline(*svp);
5886 if (mode & O_BINARY)
5887 o->op_private |= OPpOPEN_OUT_RAW;
5888 else if (mode & O_TEXT)
5889 o->op_private |= OPpOPEN_OUT_CRLF;
5892 if (o->op_type == OP_BACKTICK)
5898 Perl_ck_repeat(pTHX_ OP *o)
5900 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
5901 o->op_private |= OPpREPEAT_DOLIST;
5902 cBINOPo->op_first = force_list(cBINOPo->op_first);
5910 Perl_ck_require(pTHX_ OP *o)
5912 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
5913 SVOP *kid = (SVOP*)cUNOPo->op_first;
5915 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5917 for (s = SvPVX(kid->op_sv); *s; s++) {
5918 if (*s == ':' && s[1] == ':') {
5920 Move(s+2, s+1, strlen(s+2)+1, char);
5921 --SvCUR(kid->op_sv);
5924 if (SvREADONLY(kid->op_sv)) {
5925 SvREADONLY_off(kid->op_sv);
5926 sv_catpvn(kid->op_sv, ".pm", 3);
5927 SvREADONLY_on(kid->op_sv);
5930 sv_catpvn(kid->op_sv, ".pm", 3);
5938 Perl_ck_retarget(pTHX_ OP *o)
5940 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
5947 Perl_ck_select(pTHX_ OP *o)
5950 if (o->op_flags & OPf_KIDS) {
5951 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5952 if (kid && kid->op_sibling) {
5953 o->op_type = OP_SSELECT;
5954 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
5956 return fold_constants(o);
5960 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5961 if (kid && kid->op_type == OP_RV2GV)
5962 kid->op_private &= ~HINT_STRICT_REFS;
5967 Perl_ck_shift(pTHX_ OP *o)
5969 I32 type = o->op_type;
5971 if (!(o->op_flags & OPf_KIDS)) {
5976 if (!CvUNIQUE(PL_compcv)) {
5977 argop = newOP(OP_PADAV, OPf_REF);
5978 argop->op_targ = 0; /* PL_curpad[0] is @_ */
5981 argop = newUNOP(OP_RV2AV, 0,
5982 scalar(newGVOP(OP_GV, 0,
5983 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5986 argop = newUNOP(OP_RV2AV, 0,
5987 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(PL_compcv) ?
5988 PL_defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5989 #endif /* USE_THREADS */
5990 return newUNOP(type, 0, scalar(argop));
5992 return scalar(modkids(ck_fun(o), type));
5996 Perl_ck_sort(pTHX_ OP *o)
6000 if (PL_hints & HINT_LOCALE)
6001 o->op_private |= OPpLOCALE;
6004 if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
6006 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
6007 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6009 kid = kUNOP->op_first; /* get past null */
6011 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
6013 if (kid->op_type == OP_SCOPE) {
6017 else if (kid->op_type == OP_LEAVE) {
6018 if (o->op_type == OP_SORT) {
6019 null(kid); /* wipe out leave */
6022 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
6023 if (k->op_next == kid)
6025 /* don't descend into loops */
6026 else if (k->op_type == OP_ENTERLOOP
6027 || k->op_type == OP_ENTERITER)
6029 k = cLOOPx(k)->op_lastop;
6034 kid->op_next = 0; /* just disconnect the leave */
6035 k = kLISTOP->op_first;
6039 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6040 if (o->op_type == OP_SORT)
6044 o->op_flags |= OPf_SPECIAL;
6046 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
6047 null(cLISTOPo->op_first->op_sibling);
6054 S_simplify_sort(pTHX_ OP *o)
6057 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6061 if (!(o->op_flags & OPf_STACKED))
6063 GvMULTI_on(gv_fetchpv("a", TRUE, SVt_PV));
6064 GvMULTI_on(gv_fetchpv("b", TRUE, SVt_PV));
6065 kid = kUNOP->op_first; /* get past null */
6066 if (kid->op_type != OP_SCOPE)
6068 kid = kLISTOP->op_last; /* get past scope */
6069 switch(kid->op_type) {
6077 k = kid; /* remember this node*/
6078 if (kBINOP->op_first->op_type != OP_RV2SV)
6080 kid = kBINOP->op_first; /* get past cmp */
6081 if (kUNOP->op_first->op_type != OP_GV)
6083 kid = kUNOP->op_first; /* get past rv2sv */
6085 if (GvSTASH(gv) != PL_curstash)
6087 if (strEQ(GvNAME(gv), "a"))
6089 else if (strEQ(GvNAME(gv), "b"))
6093 kid = k; /* back to cmp */
6094 if (kBINOP->op_last->op_type != OP_RV2SV)
6096 kid = kBINOP->op_last; /* down to 2nd arg */
6097 if (kUNOP->op_first->op_type != OP_GV)
6099 kid = kUNOP->op_first; /* get past rv2sv */
6101 if (GvSTASH(gv) != PL_curstash
6103 ? strNE(GvNAME(gv), "a")
6104 : strNE(GvNAME(gv), "b")))
6106 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
6108 o->op_private |= OPpSORT_REVERSE;
6109 if (k->op_type == OP_NCMP)
6110 o->op_private |= OPpSORT_NUMERIC;
6111 if (k->op_type == OP_I_NCMP)
6112 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
6113 kid = cLISTOPo->op_first->op_sibling;
6114 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
6115 op_free(kid); /* then delete it */
6116 cLISTOPo->op_children--;
6120 Perl_ck_split(pTHX_ OP *o)
6124 if (o->op_flags & OPf_STACKED)
6125 return no_fh_allowed(o);
6127 kid = cLISTOPo->op_first;
6128 if (kid->op_type != OP_NULL)
6129 Perl_croak(aTHX_ "panic: ck_split");
6130 kid = kid->op_sibling;
6131 op_free(cLISTOPo->op_first);
6132 cLISTOPo->op_first = kid;
6134 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvn(" ", 1));
6135 cLISTOPo->op_last = kid; /* There was only one element previously */
6138 if (kid->op_type != OP_MATCH) {
6139 OP *sibl = kid->op_sibling;
6140 kid->op_sibling = 0;
6141 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
6142 if (cLISTOPo->op_first == cLISTOPo->op_last)
6143 cLISTOPo->op_last = kid;
6144 cLISTOPo->op_first = kid;
6145 kid->op_sibling = sibl;
6148 kid->op_type = OP_PUSHRE;
6149 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
6152 if (!kid->op_sibling)
6153 append_elem(OP_SPLIT, o, newDEFSVOP());
6155 kid = kid->op_sibling;
6158 if (!kid->op_sibling)
6159 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
6161 kid = kid->op_sibling;
6164 if (kid->op_sibling)
6165 return too_many_arguments(o,PL_op_desc[o->op_type]);
6171 Perl_ck_join(pTHX_ OP *o)
6173 if (ckWARN(WARN_SYNTAX)) {
6174 OP *kid = cLISTOPo->op_first->op_sibling;
6175 if (kid && kid->op_type == OP_MATCH) {
6176 char *pmstr = "STRING";
6177 if (kPMOP->op_pmregexp)
6178 pmstr = kPMOP->op_pmregexp->precomp;
6179 Perl_warner(aTHX_ WARN_SYNTAX,
6180 "/%s/ should probably be written as \"%s\"",
6188 Perl_ck_subr(pTHX_ OP *o)
6191 OP *prev = ((cUNOPo->op_first->op_sibling)
6192 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
6193 OP *o2 = prev->op_sibling;
6202 o->op_private |= OPpENTERSUB_HASTARG;
6203 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
6204 if (cvop->op_type == OP_RV2CV) {
6206 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
6207 null(cvop); /* disable rv2cv */
6208 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
6209 if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) {
6210 GV *gv = cGVOPx_gv(tmpop);
6213 tmpop->op_private |= OPpEARLY_CV;
6214 else if (SvPOK(cv)) {
6215 namegv = CvANON(cv) ? gv : CvGV(cv);
6216 proto = SvPV((SV*)cv, n_a);
6220 else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
6221 if (o2->op_type == OP_CONST)
6222 o2->op_private &= ~OPpCONST_STRICT;
6223 else if (o2->op_type == OP_LIST) {
6224 OP *o = ((UNOP*)o2)->op_first->op_sibling;
6225 if (o && o->op_type == OP_CONST)
6226 o->op_private &= ~OPpCONST_STRICT;
6229 o->op_private |= (PL_hints & HINT_STRICT_REFS);
6230 if (PERLDB_SUB && PL_curstash != PL_debstash)
6231 o->op_private |= OPpENTERSUB_DB;
6232 while (o2 != cvop) {
6236 return too_many_arguments(o, gv_ename(namegv));
6254 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
6255 bad_type(arg, "block", gv_ename(namegv), o2);
6258 /* '*' allows any scalar type, including bareword */
6261 if (o2->op_type == OP_RV2GV)
6262 goto wrapref; /* autoconvert GLOB -> GLOBref */
6263 else if (o2->op_type == OP_CONST)
6264 o2->op_private &= ~OPpCONST_STRICT;
6265 else if (o2->op_type == OP_ENTERSUB) {
6266 /* accidental subroutine, revert to bareword */
6267 OP *gvop = ((UNOP*)o2)->op_first;
6268 if (gvop && gvop->op_type == OP_NULL) {
6269 gvop = ((UNOP*)gvop)->op_first;
6271 for (; gvop->op_sibling; gvop = gvop->op_sibling)
6274 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
6275 (gvop = ((UNOP*)gvop)->op_first) &&
6276 gvop->op_type == OP_GV)
6278 GV *gv = cGVOPx_gv(gvop);
6279 OP *sibling = o2->op_sibling;
6280 SV *n = newSVpvn("",0);
6282 gv_fullname3(n, gv, "");
6283 if (SvCUR(n)>6 && strnEQ(SvPVX(n),"main::",6))
6284 sv_chop(n, SvPVX(n)+6);
6285 o2 = newSVOP(OP_CONST, 0, n);
6286 prev->op_sibling = o2;
6287 o2->op_sibling = sibling;
6299 if (o2->op_type != OP_RV2GV)
6300 bad_type(arg, "symbol", gv_ename(namegv), o2);
6303 if (o2->op_type != OP_RV2CV)
6304 bad_type(arg, "sub", gv_ename(namegv), o2);
6307 if (o2->op_type != OP_RV2SV
6308 && o2->op_type != OP_PADSV
6309 && o2->op_type != OP_HELEM
6310 && o2->op_type != OP_AELEM
6311 && o2->op_type != OP_THREADSV)
6313 bad_type(arg, "scalar", gv_ename(namegv), o2);
6317 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
6318 bad_type(arg, "array", gv_ename(namegv), o2);
6321 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
6322 bad_type(arg, "hash", gv_ename(namegv), o2);
6326 OP* sib = kid->op_sibling;
6327 kid->op_sibling = 0;
6328 o2 = newUNOP(OP_REFGEN, 0, kid);
6329 o2->op_sibling = sib;
6330 prev->op_sibling = o2;
6341 Perl_croak(aTHX_ "Malformed prototype for %s: %s",
6342 gv_ename(namegv), SvPV((SV*)cv, n_a));
6347 mod(o2, OP_ENTERSUB);
6349 o2 = o2->op_sibling;
6351 if (proto && !optional &&
6352 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
6353 return too_few_arguments(o, gv_ename(namegv));
6358 Perl_ck_svconst(pTHX_ OP *o)
6360 SvREADONLY_on(cSVOPo->op_sv);
6365 Perl_ck_trunc(pTHX_ OP *o)
6367 if (o->op_flags & OPf_KIDS) {
6368 SVOP *kid = (SVOP*)cUNOPo->op_first;
6370 if (kid->op_type == OP_NULL)
6371 kid = (SVOP*)kid->op_sibling;
6372 if (kid && kid->op_type == OP_CONST &&
6373 (kid->op_private & OPpCONST_BARE))
6375 o->op_flags |= OPf_SPECIAL;
6376 kid->op_private &= ~OPpCONST_STRICT;
6382 /* A peephole optimizer. We visit the ops in the order they're to execute. */
6385 Perl_peep(pTHX_ register OP *o)
6388 register OP* oldop = 0;
6390 OP *last_composite = Nullop;
6392 if (!o || o->op_seq)
6396 SAVEVPTR(PL_curcop);
6397 for (; o; o = o->op_next) {
6403 switch (o->op_type) {
6407 PL_curcop = ((COP*)o); /* for warnings */
6408 o->op_seq = PL_op_seqmax++;
6409 last_composite = Nullop;
6413 if (cSVOPo->op_private & OPpCONST_STRICT)
6414 no_bareword_allowed(o);
6416 /* Relocate sv to the pad for thread safety.
6417 * Despite being a "constant", the SV is written to,
6418 * for reference counts, sv_upgrade() etc. */
6420 PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP);
6421 SvREFCNT_dec(PL_curpad[ix]);
6422 SvPADTMP_on(cSVOPo->op_sv);
6423 PL_curpad[ix] = cSVOPo->op_sv;
6424 cSVOPo->op_sv = Nullsv;
6428 o->op_seq = PL_op_seqmax++;
6432 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
6433 if (o->op_next->op_private & OPpTARGET_MY) {
6434 if (o->op_flags & OPf_STACKED) /* chained concats */
6435 goto ignore_optimization;
6437 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
6438 o->op_targ = o->op_next->op_targ;
6439 o->op_next->op_targ = 0;
6440 o->op_private |= OPpTARGET_MY;
6445 ignore_optimization:
6446 o->op_seq = PL_op_seqmax++;
6449 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
6450 o->op_seq = PL_op_seqmax++;
6451 break; /* Scalar stub must produce undef. List stub is noop */
6455 if (o->op_targ == OP_NEXTSTATE
6456 || o->op_targ == OP_DBSTATE
6457 || o->op_targ == OP_SETSTATE)
6459 PL_curcop = ((COP*)o);
6466 if (oldop && o->op_next) {
6467 oldop->op_next = o->op_next;
6470 o->op_seq = PL_op_seqmax++;
6474 if (o->op_next->op_type == OP_RV2SV) {
6475 if (!(o->op_next->op_private & OPpDEREF)) {
6477 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
6479 o->op_next = o->op_next->op_next;
6480 o->op_type = OP_GVSV;
6481 o->op_ppaddr = PL_ppaddr[OP_GVSV];
6484 else if (o->op_next->op_type == OP_RV2AV) {
6485 OP* pop = o->op_next->op_next;
6487 if (pop->op_type == OP_CONST &&
6488 (PL_op = pop->op_next) &&
6489 pop->op_next->op_type == OP_AELEM &&
6490 !(pop->op_next->op_private &
6491 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
6492 (i = SvIV(((SVOP*)pop)->op_sv) - PL_compiling.cop_arybase)
6500 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
6501 o->op_next = pop->op_next->op_next;
6502 o->op_type = OP_AELEMFAST;
6503 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
6504 o->op_private = (U8)i;
6509 else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
6511 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX(GvCV(gv))) {
6512 /* XXX could check prototype here instead of just carping */
6513 SV *sv = sv_newmortal();
6514 gv_efullname3(sv, gv, Nullch);
6515 Perl_warner(aTHX_ WARN_PROTOTYPE,
6516 "%s() called too early to check prototype",
6521 o->op_seq = PL_op_seqmax++;
6532 o->op_seq = PL_op_seqmax++;
6533 while (cLOGOP->op_other->op_type == OP_NULL)
6534 cLOGOP->op_other = cLOGOP->op_other->op_next;
6535 peep(cLOGOP->op_other);
6539 o->op_seq = PL_op_seqmax++;
6540 peep(cLOOP->op_redoop);
6541 peep(cLOOP->op_nextop);
6542 peep(cLOOP->op_lastop);
6548 o->op_seq = PL_op_seqmax++;
6549 peep(cPMOP->op_pmreplstart);
6553 o->op_seq = PL_op_seqmax++;
6554 if (ckWARN(WARN_SYNTAX) && o->op_next
6555 && o->op_next->op_type == OP_NEXTSTATE) {
6556 if (o->op_next->op_sibling &&
6557 o->op_next->op_sibling->op_type != OP_EXIT &&
6558 o->op_next->op_sibling->op_type != OP_WARN &&
6559 o->op_next->op_sibling->op_type != OP_DIE) {
6560 line_t oldline = CopLINE(PL_curcop);
6562 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_next));
6563 Perl_warner(aTHX_ WARN_EXEC,
6564 "Statement unlikely to be reached");
6565 Perl_warner(aTHX_ WARN_EXEC,
6566 "\t(Maybe you meant system() when you said exec()?)\n");
6567 CopLINE_set(PL_curcop, oldline);
6576 SV **svp, **indsvp, *sv;
6581 o->op_seq = PL_op_seqmax++;
6582 if ((o->op_private & (OPpLVAL_INTRO))
6583 || ((BINOP*)o)->op_last->op_type != OP_CONST)
6585 rop = (UNOP*)((BINOP*)o)->op_first;
6586 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6588 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6589 if (!SvOBJECT(lexname))
6591 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6592 if (!fields || !GvHV(*fields))
6594 svp = cSVOPx_svp(((BINOP*)o)->op_last);
6595 key = SvPV(*svp, keylen);
6596 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
6598 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" in variable %s of type %s",
6599 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
6601 ind = SvIV(*indsvp);
6603 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6604 rop->op_type = OP_RV2AV;
6605 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6606 o->op_type = OP_AELEM;
6607 o->op_ppaddr = PL_ppaddr[OP_AELEM];
6609 if (SvREADONLY(*svp))
6611 SvFLAGS(sv) |= (SvFLAGS(*svp)
6612 & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
6622 SV **svp, **indsvp, *sv;
6626 SVOP *first_key_op, *key_op;
6628 o->op_seq = PL_op_seqmax++;
6629 if ((o->op_private & (OPpLVAL_INTRO))
6630 /* I bet there's always a pushmark... */
6631 || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST)
6632 /* hmmm, no optimization if list contains only one key. */
6634 rop = (UNOP*)((LISTOP*)o)->op_last;
6635 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6637 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6638 if (!SvOBJECT(lexname))
6640 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6641 if (!fields || !GvHV(*fields))
6643 /* Again guessing that the pushmark can be jumped over.... */
6644 first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling)
6645 ->op_first->op_sibling;
6646 /* Check that the key list contains only constants. */
6647 for (key_op = first_key_op; key_op;
6648 key_op = (SVOP*)key_op->op_sibling)
6649 if (key_op->op_type != OP_CONST)
6653 rop->op_type = OP_RV2AV;
6654 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6655 o->op_type = OP_ASLICE;
6656 o->op_ppaddr = PL_ppaddr[OP_ASLICE];
6657 for (key_op = first_key_op; key_op;
6658 key_op = (SVOP*)key_op->op_sibling) {
6659 svp = cSVOPx_svp(key_op);
6660 key = SvPV(*svp, keylen);
6661 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
6663 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" "
6664 "in variable %s of type %s",
6665 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
6667 ind = SvIV(*indsvp);
6669 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6671 if (SvREADONLY(*svp))
6673 SvFLAGS(sv) |= (SvFLAGS(*svp)
6674 & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
6683 if (!(o->op_flags & OPf_WANT)
6684 || (o->op_flags & OPf_WANT) == OPf_WANT_LIST)
6688 o->op_seq = PL_op_seqmax++;
6692 if (o->op_next && o->op_next->op_type != OP_LEAVESUBLV) {
6693 o->op_seq = PL_op_seqmax++;
6699 if (last_composite) {
6700 OP *r = last_composite;
6702 while (r->op_sibling)
6705 || (r->op_next->op_type == OP_LIST
6706 && r->op_next->op_next == o))
6708 if (last_composite->op_type == OP_RV2AV)
6709 yyerror("Lvalue subs returning arrays not implemented yet");
6711 yyerror("Lvalue subs returning hashes not implemented yet");
6718 o->op_seq = PL_op_seqmax++;