3 * Copyright (c) 1991-1999, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
12 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
13 * youngest of the Old Took's daughters); and Mr. Drogo was his second
14 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
15 * either way, as the saying is, if you follow me." --the Gaffer
23 #define CHECKCALL this->*PL_check
25 #define CHECKCALL *PL_check
28 /* #define PL_OP_SLAB_ALLOC */
30 #ifdef PL_OP_SLAB_ALLOC
31 #define SLAB_SIZE 8192
32 static char *PL_OpPtr = NULL;
33 static int PL_OpSpace = 0;
34 #define NewOp(m,var,c,type) do { if ((PL_OpSpace -= c*sizeof(type)) >= 0) \
35 var = (type *)(PL_OpPtr -= c*sizeof(type)); \
37 var = (type *) Slab_Alloc(m,c*sizeof(type)); \
41 S_Slab_Alloc(pTHX_ int m, size_t sz)
43 Newz(m,PL_OpPtr,SLAB_SIZE,char);
44 PL_OpSpace = SLAB_SIZE - sz;
45 return PL_OpPtr += PL_OpSpace;
49 #define NewOp(m, var, c, type) Newz(m, var, c, type)
52 * In the following definition, the ", Nullop" is just to make the compiler
53 * think the expression is of the right type: croak actually does a Siglongjmp.
55 #define CHECKOP(type,o) \
56 ((PL_op_mask && PL_op_mask[type]) \
57 ? ( op_free((OP*)o), \
58 Perl_croak(aTHX_ "%s trapped by operation mask", PL_op_desc[type]), \
60 : (CHECKCALL[type])(aTHX_ (OP*)o))
62 #define PAD_MAX 999999999
65 S_gv_ename(pTHX_ GV *gv)
68 SV* tmpsv = sv_newmortal();
69 gv_efullname3(tmpsv, gv, Nullch);
70 return SvPV(tmpsv,n_a);
74 S_no_fh_allowed(pTHX_ OP *o)
76 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
77 PL_op_desc[o->op_type]));
82 S_too_few_arguments(pTHX_ OP *o, char *name)
84 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
89 S_too_many_arguments(pTHX_ OP *o, char *name)
91 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
96 S_bad_type(pTHX_ I32 n, char *t, char *name, OP *kid)
98 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
99 (int)n, name, t, PL_op_desc[kid->op_type]));
103 S_no_bareword_allowed(pTHX_ OP *o)
105 Perl_warn(aTHX_ "Bareword \"%s\" not allowed while \"strict subs\" in use",
106 SvPV_nolen(cSVOPo->op_sv));
111 Perl_assertref(pTHX_ OP *o)
113 int type = o->op_type;
114 if (type != OP_AELEM && type != OP_HELEM && type != OP_GELEM) {
115 yyerror(Perl_form(aTHX_ "Can't use subscript on %s", PL_op_desc[type]));
116 if (type == OP_ENTERSUB || type == OP_RV2HV || type == OP_PADHV) {
118 SV *msg = sv_2mortal(
119 Perl_newSVpvf(aTHX_ "(Did you mean $ or @ instead of %c?)\n",
120 type == OP_ENTERSUB ? '&' : '%'));
121 if (PL_in_eval & EVAL_WARNONLY)
122 Perl_warn(aTHX_ "%_", msg);
124 sv_catsv(GvSV(PL_errgv), msg);
126 PerlIO_write(PerlIO_stderr(), SvPVX(msg), SvCUR(msg));
131 /* "register" allocation */
134 Perl_pad_allocmy(pTHX_ char *name)
142 (PL_hints & HINT_UTF8 && (name[1] & 0xc0) == 0xc0) ||
143 name[1] == '_' && (int)strlen(name) > 2))
145 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
146 /* 1999-02-27 mjd@plover.com */
148 p = strchr(name, '\0');
149 /* The next block assumes the buffer is at least 205 chars
150 long. At present, it's always at least 256 chars. */
152 strcpy(name+200, "...");
158 /* Move everything else down one character */
159 for (; p-name > 2; p--)
161 name[2] = toCTRL(name[1]);
164 yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
166 if (ckWARN(WARN_UNSAFE) && AvFILLp(PL_comppad_name) >= 0) {
167 SV **svp = AvARRAY(PL_comppad_name);
168 for (off = AvFILLp(PL_comppad_name); off > PL_comppad_name_floor; off--) {
170 && sv != &PL_sv_undef
171 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
172 && strEQ(name, SvPVX(sv)))
174 Perl_warner(aTHX_ WARN_UNSAFE,
175 "\"my\" variable %s masks earlier declaration in same %s",
176 name, (SvIVX(sv) == PAD_MAX ? "scope" : "statement"));
181 off = pad_alloc(OP_PADSV, SVs_PADMY);
183 sv_upgrade(sv, SVt_PVNV);
185 if (PL_in_my_stash) {
187 yyerror(Perl_form(aTHX_ "Can't declare class for non-scalar %s in \"my\"",
190 (void)SvUPGRADE(sv, SVt_PVMG);
191 SvSTASH(sv) = (HV*)SvREFCNT_inc(PL_in_my_stash);
194 av_store(PL_comppad_name, off, sv);
195 SvNVX(sv) = (NV)PAD_MAX;
196 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
197 if (!PL_min_intro_pending)
198 PL_min_intro_pending = off;
199 PL_max_intro_pending = off;
201 av_store(PL_comppad, off, (SV*)newAV());
202 else if (*name == '%')
203 av_store(PL_comppad, off, (SV*)newHV());
204 SvPADMY_on(PL_curpad[off]);
208 #define FINDLEX_NOSEARCH 1 /* don't search outer contexts */
211 S_pad_findlex(pTHX_ char *name, PADOFFSET newoff, U32 seq, CV* startcv,
212 I32 cx_ix, I32 saweval, U32 flags)
219 register PERL_CONTEXT *cx;
221 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
222 AV *curlist = CvPADLIST(cv);
223 SV **svp = av_fetch(curlist, 0, FALSE);
226 if (!svp || *svp == &PL_sv_undef)
229 svp = AvARRAY(curname);
230 for (off = AvFILLp(curname); off > 0; off--) {
231 if ((sv = svp[off]) &&
232 sv != &PL_sv_undef &&
234 seq > I_32(SvNVX(sv)) &&
235 strEQ(SvPVX(sv), name))
246 return 0; /* don't clone from inactive stack frame */
250 oldpad = (AV*)*av_fetch(curlist, depth, FALSE);
251 oldsv = *av_fetch(oldpad, off, TRUE);
252 if (!newoff) { /* Not a mere clone operation. */
253 SV *namesv = NEWSV(1103,0);
254 newoff = pad_alloc(OP_PADSV, SVs_PADMY);
255 sv_upgrade(namesv, SVt_PVNV);
256 sv_setpv(namesv, name);
257 av_store(PL_comppad_name, newoff, namesv);
258 SvNVX(namesv) = (NV)PL_curcop->cop_seq;
259 SvIVX(namesv) = PAD_MAX; /* A ref, intro immediately */
260 SvFAKE_on(namesv); /* A ref, not a real var */
261 if (SvOBJECT(sv)) { /* A typed var */
263 (void)SvUPGRADE(namesv, SVt_PVMG);
264 SvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)SvSTASH(sv));
267 if (CvANON(PL_compcv) || SvTYPE(PL_compcv) == SVt_PVFM) {
268 /* "It's closures all the way down." */
269 CvCLONE_on(PL_compcv);
271 if (CvANON(PL_compcv))
272 oldsv = Nullsv; /* no need to keep ref */
277 bcv && bcv != cv && !CvCLONE(bcv);
278 bcv = CvOUTSIDE(bcv))
283 if (ckWARN(WARN_CLOSURE)
284 && !CvUNIQUE(bcv) && !CvUNIQUE(cv))
286 Perl_warner(aTHX_ WARN_CLOSURE,
287 "Variable \"%s\" may be unavailable",
295 else if (!CvUNIQUE(PL_compcv)) {
296 if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv))
297 Perl_warner(aTHX_ WARN_CLOSURE,
298 "Variable \"%s\" will not stay shared", name);
301 av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv));
307 if (flags & FINDLEX_NOSEARCH)
310 /* Nothing in current lexical context--try eval's context, if any.
311 * This is necessary to let the perldb get at lexically scoped variables.
312 * XXX This will also probably interact badly with eval tree caching.
315 for (i = cx_ix; i >= 0; i--) {
317 switch (CxTYPE(cx)) {
319 if (i == 0 && saweval) {
320 seq = cxstack[saweval].blk_oldcop->cop_seq;
321 return pad_findlex(name, newoff, seq, PL_main_cv, -1, saweval, 0);
325 switch (cx->blk_eval.old_op_type) {
331 /* require must have its own scope */
339 if (PL_debstash && CvSTASH(cv) == PL_debstash) { /* ignore DB'* scope */
340 saweval = i; /* so we know where we were called from */
343 seq = cxstack[saweval].blk_oldcop->cop_seq;
344 return pad_findlex(name, newoff, seq, cv, i-1, saweval,FINDLEX_NOSEARCH);
352 Perl_pad_findmy(pTHX_ char *name)
358 SV **svp = AvARRAY(PL_comppad_name);
359 U32 seq = PL_cop_seqmax;
365 * Special case to get lexical (and hence per-thread) @_.
366 * XXX I need to find out how to tell at parse-time whether use
367 * of @_ should refer to a lexical (from a sub) or defgv (global
368 * scope and maybe weird sub-ish things like formats). See
369 * startsub in perly.y. It's possible that @_ could be lexical
370 * (at least from subs) even in non-threaded perl.
372 if (strEQ(name, "@_"))
373 return 0; /* success. (NOT_IN_PAD indicates failure) */
374 #endif /* USE_THREADS */
376 /* The one we're looking for is probably just before comppad_name_fill. */
377 for (off = AvFILLp(PL_comppad_name); off > 0; off--) {
378 if ((sv = svp[off]) &&
379 sv != &PL_sv_undef &&
382 seq > I_32(SvNVX(sv)))) &&
383 strEQ(SvPVX(sv), name))
386 return (PADOFFSET)off;
387 pendoff = off; /* this pending def. will override import */
391 outside = CvOUTSIDE(PL_compcv);
393 /* Check if if we're compiling an eval'', and adjust seq to be the
394 * eval's seq number. This depends on eval'' having a non-null
395 * CvOUTSIDE() while it is being compiled. The eval'' itself is
396 * identified by CvEVAL being true and CvGV being null. */
397 if (outside && CvEVAL(PL_compcv) && !CvGV(PL_compcv) && cxstack_ix >= 0) {
398 cx = &cxstack[cxstack_ix];
400 seq = cx->blk_oldcop->cop_seq;
403 /* See if it's in a nested scope */
404 off = pad_findlex(name, 0, seq, outside, cxstack_ix, 0, 0);
406 /* If there is a pending local definition, this new alias must die */
408 SvIVX(AvARRAY(PL_comppad_name)[off]) = seq;
409 return off; /* pad_findlex returns 0 for failure...*/
411 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
415 Perl_pad_leavemy(pTHX_ I32 fill)
419 SV **svp = AvARRAY(PL_comppad_name);
421 if (PL_min_intro_pending && fill < PL_min_intro_pending) {
422 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
423 if ((sv = svp[off]) && sv != &PL_sv_undef && ckWARN_d(WARN_INTERNAL))
424 Perl_warner(aTHX_ WARN_INTERNAL, "%s never introduced", SvPVX(sv));
427 /* "Deintroduce" my variables that are leaving with this scope. */
428 for (off = AvFILLp(PL_comppad_name); off > fill; off--) {
429 if ((sv = svp[off]) && sv != &PL_sv_undef && SvIVX(sv) == PAD_MAX)
430 SvIVX(sv) = PL_cop_seqmax;
435 Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype)
441 if (AvARRAY(PL_comppad) != PL_curpad)
442 Perl_croak(aTHX_ "panic: pad_alloc");
443 if (PL_pad_reset_pending)
445 if (tmptype & SVs_PADMY) {
447 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
448 } while (SvPADBUSY(sv)); /* need a fresh one */
449 retval = AvFILLp(PL_comppad);
452 SV **names = AvARRAY(PL_comppad_name);
453 SSize_t names_fill = AvFILLp(PL_comppad_name);
456 * "foreach" index vars temporarily become aliases to non-"my"
457 * values. Thus we must skip, not just pad values that are
458 * marked as current pad values, but also those with names.
460 if (++PL_padix <= names_fill &&
461 (sv = names[PL_padix]) && sv != &PL_sv_undef)
463 sv = *av_fetch(PL_comppad, PL_padix, TRUE);
464 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)))
469 SvFLAGS(sv) |= tmptype;
470 PL_curpad = AvARRAY(PL_comppad);
472 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx alloc %ld for %s\n",
473 (unsigned long) thr, (unsigned long) PL_curpad,
474 (long) retval, PL_op_name[optype]));
476 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx alloc %ld for %s\n",
477 (unsigned long) PL_curpad,
478 (long) retval, PL_op_name[optype]));
479 #endif /* USE_THREADS */
480 return (PADOFFSET)retval;
484 Perl_pad_sv(pTHX_ PADOFFSET po)
488 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx sv %d\n",
489 (unsigned long) thr, (unsigned long) PL_curpad, po));
492 Perl_croak(aTHX_ "panic: pad_sv po");
493 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx sv %d\n",
494 (unsigned long) PL_curpad, po));
495 #endif /* USE_THREADS */
496 return PL_curpad[po]; /* eventually we'll turn this into a macro */
500 Perl_pad_free(pTHX_ PADOFFSET po)
505 if (AvARRAY(PL_comppad) != PL_curpad)
506 Perl_croak(aTHX_ "panic: pad_free curpad");
508 Perl_croak(aTHX_ "panic: pad_free po");
510 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx free %d\n",
511 (unsigned long) thr, (unsigned long) PL_curpad, po));
513 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx free %d\n",
514 (unsigned long) PL_curpad, po));
515 #endif /* USE_THREADS */
516 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef)
517 SvPADTMP_off(PL_curpad[po]);
518 if ((I32)po < PL_padix)
523 Perl_pad_swipe(pTHX_ PADOFFSET po)
526 if (AvARRAY(PL_comppad) != PL_curpad)
527 Perl_croak(aTHX_ "panic: pad_swipe curpad");
529 Perl_croak(aTHX_ "panic: pad_swipe po");
531 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx swipe %d\n",
532 (unsigned long) thr, (unsigned long) PL_curpad, po));
534 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx swipe %d\n",
535 (unsigned long) PL_curpad, po));
536 #endif /* USE_THREADS */
537 SvPADTMP_off(PL_curpad[po]);
538 PL_curpad[po] = NEWSV(1107,0);
539 SvPADTMP_on(PL_curpad[po]);
540 if ((I32)po < PL_padix)
544 /* XXX pad_reset() is currently disabled because it results in serious bugs.
545 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
546 * on the stack by OPs that use them, there are several ways to get an alias
547 * to a shared TARG. Such an alias will change randomly and unpredictably.
548 * We avoid doing this until we can think of a Better Way.
553 #ifdef USE_BROKEN_PAD_RESET
557 if (AvARRAY(PL_comppad) != PL_curpad)
558 Perl_croak(aTHX_ "panic: pad_reset curpad");
560 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx reset\n",
561 (unsigned long) thr, (unsigned long) PL_curpad));
563 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx reset\n",
564 (unsigned long) PL_curpad));
565 #endif /* USE_THREADS */
566 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
567 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
568 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
569 SvPADTMP_off(PL_curpad[po]);
571 PL_padix = PL_padix_floor;
574 PL_pad_reset_pending = FALSE;
578 /* find_threadsv is not reentrant */
580 Perl_find_threadsv(pTHX_ const char *name)
586 /* We currently only handle names of a single character */
587 p = strchr(PL_threadsv_names, *name);
590 key = p - PL_threadsv_names;
591 MUTEX_LOCK(&thr->mutex);
592 svp = av_fetch(thr->threadsv, key, FALSE);
594 MUTEX_UNLOCK(&thr->mutex);
596 SV *sv = NEWSV(0, 0);
597 av_store(thr->threadsv, key, sv);
598 thr->threadsvp = AvARRAY(thr->threadsv);
599 MUTEX_UNLOCK(&thr->mutex);
601 * Some magic variables used to be automagically initialised
602 * in gv_fetchpv. Those which are now per-thread magicals get
603 * initialised here instead.
609 sv_setpv(sv, "\034");
610 sv_magic(sv, 0, 0, name, 1);
615 PL_sawampersand = TRUE;
629 /* XXX %! tied to Errno.pm needs to be added here.
630 * See gv_fetchpv(). */
634 sv_magic(sv, 0, 0, name, 1);
636 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
637 "find_threadsv: new SV %p for $%s%c\n",
638 sv, (*name < 32) ? "^" : "",
639 (*name < 32) ? toCTRL(*name) : *name));
643 #endif /* USE_THREADS */
648 Perl_op_free(pTHX_ OP *o)
650 register OP *kid, *nextkid;
652 if (!o || o->op_seq == (U16)-1)
655 if (o->op_flags & OPf_KIDS) {
656 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
657 nextkid = kid->op_sibling; /* Get before next freeing kid */
662 switch (o->op_type) {
664 o->op_targ = 0; /* Was holding old type, if any. */
667 o->op_targ = 0; /* Was holding hints. */
671 if (!(o->op_flags & OPf_SPECIAL))
675 o->op_targ = 0; /* Was holding index into thr->threadsv AV. */
677 #endif /* USE_THREADS */
679 if (!(o->op_flags & OPf_REF)
680 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
686 SvREFCNT_dec(cGVOPo->op_gv);
693 SvREFCNT_dec(cSVOPo->op_sv);
699 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
703 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF))
704 SvREFCNT_dec(cSVOPo->op_sv);
706 Safefree(cPVOPo->op_pv);
709 op_free(cPMOPo->op_pmreplroot);
714 ReREFCNT_dec(cPMOPo->op_pmregexp);
719 pad_free(o->op_targ);
721 #ifdef PL_OP_SLAB_ALLOC
722 if ((char *) o == PL_OpPtr)
731 S_cop_free(pTHX_ COP* cop)
733 Safefree(cop->cop_label);
734 SvREFCNT_dec(cop->cop_filegv);
735 if (! specialWARN(cop->cop_warnings))
736 SvREFCNT_dec(cop->cop_warnings);
742 if (o->op_type != OP_NULL && o->op_type != OP_THREADSV && o->op_targ > 0)
743 pad_free(o->op_targ);
744 o->op_targ = o->op_type;
745 o->op_type = OP_NULL;
746 o->op_ppaddr = PL_ppaddr[OP_NULL];
749 /* Contextualizers */
751 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
754 Perl_linklist(pTHX_ OP *o)
761 /* establish postfix order */
762 if (cUNOPo->op_first) {
763 o->op_next = LINKLIST(cUNOPo->op_first);
764 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
766 kid->op_next = LINKLIST(kid->op_sibling);
778 Perl_scalarkids(pTHX_ OP *o)
781 if (o && o->op_flags & OPf_KIDS) {
782 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
789 S_scalarboolean(pTHX_ OP *o)
791 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
793 if (ckWARN(WARN_SYNTAX)) {
794 line_t oldline = PL_curcop->cop_line;
796 if (PL_copline != NOLINE)
797 PL_curcop->cop_line = PL_copline;
798 Perl_warner(aTHX_ WARN_SYNTAX, "Found = in conditional, should be ==");
799 PL_curcop->cop_line = oldline;
806 Perl_scalar(pTHX_ OP *o)
810 /* assumes no premature commitment */
811 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
812 || o->op_type == OP_RETURN)
815 if ((o->op_private & OPpTARGET_MY)
816 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
817 return scalar(o); /* As if inside SASSIGN */
819 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
821 switch (o->op_type) {
823 if (o->op_private & OPpREPEAT_DOLIST)
824 null(((LISTOP*)cBINOPo->op_first)->op_first);
825 scalar(cBINOPo->op_first);
830 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
834 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
835 if (!kPMOP->op_pmreplroot)
836 deprecate("implicit split to @_");
844 if (o->op_flags & OPf_KIDS) {
845 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
851 kid = cLISTOPo->op_first;
853 while (kid = kid->op_sibling) {
859 WITH_THR(PL_curcop = &PL_compiling);
864 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
870 WITH_THR(PL_curcop = &PL_compiling);
877 Perl_scalarvoid(pTHX_ OP *o)
884 if (o->op_type == OP_NEXTSTATE || o->op_type == OP_DBSTATE ||
885 (o->op_type == OP_NULL &&
886 (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)))
889 PL_curcop = (COP*)o; /* for warning below */
892 /* assumes no premature commitment */
893 want = o->op_flags & OPf_WANT;
894 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
895 || o->op_type == OP_RETURN)
898 if ((o->op_private & OPpTARGET_MY)
899 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
900 return scalar(o); /* As if inside SASSIGN */
902 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
904 switch (o->op_type) {
906 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
910 if (o->op_flags & OPf_STACKED)
914 if (o->op_private == 4)
985 if (!(o->op_private & OPpLVAL_INTRO))
986 useless = PL_op_desc[o->op_type];
993 if (!(o->op_private & OPpLVAL_INTRO) &&
994 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
995 useless = "a variable";
1000 if (cSVOPo->op_private & OPpCONST_STRICT)
1001 no_bareword_allowed(o);
1004 if (ckWARN(WARN_VOID)) {
1005 useless = "a constant";
1006 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1008 else if (SvPOK(sv)) {
1009 if (strnEQ(SvPVX(sv), "di", 2) ||
1010 strnEQ(SvPVX(sv), "ds", 2) ||
1011 strnEQ(SvPVX(sv), "ig", 2))
1016 null(o); /* don't execute a constant */
1017 SvREFCNT_dec(sv); /* don't even remember it */
1021 o->op_type = OP_PREINC; /* pre-increment is faster */
1022 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1026 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1027 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1033 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1038 if (o->op_flags & OPf_STACKED)
1046 if (!(o->op_flags & OPf_KIDS))
1055 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1062 /* all requires must return a boolean value */
1063 o->op_flags &= ~OPf_WANT;
1066 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
1067 if (!kPMOP->op_pmreplroot)
1068 deprecate("implicit split to @_");
1074 if (ckWARN(WARN_VOID))
1075 Perl_warner(aTHX_ WARN_VOID, "Useless use of %s in void context", useless);
1081 Perl_listkids(pTHX_ OP *o)
1084 if (o && o->op_flags & OPf_KIDS) {
1085 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1092 Perl_list(pTHX_ OP *o)
1096 /* assumes no premature commitment */
1097 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
1098 || o->op_type == OP_RETURN)
1101 if ((o->op_private & OPpTARGET_MY)
1102 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
1103 return o; /* As if inside SASSIGN */
1105 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1107 switch (o->op_type) {
1110 list(cBINOPo->op_first);
1115 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1123 if (!(o->op_flags & OPf_KIDS))
1125 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1126 list(cBINOPo->op_first);
1127 return gen_constant_list(o);
1134 kid = cLISTOPo->op_first;
1136 while (kid = kid->op_sibling) {
1137 if (kid->op_sibling)
1142 WITH_THR(PL_curcop = &PL_compiling);
1146 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1147 if (kid->op_sibling)
1152 WITH_THR(PL_curcop = &PL_compiling);
1155 /* all requires must return a boolean value */
1156 o->op_flags &= ~OPf_WANT;
1163 Perl_scalarseq(pTHX_ OP *o)
1168 if (o->op_type == OP_LINESEQ ||
1169 o->op_type == OP_SCOPE ||
1170 o->op_type == OP_LEAVE ||
1171 o->op_type == OP_LEAVETRY)
1174 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1175 if (kid->op_sibling) {
1179 PL_curcop = &PL_compiling;
1181 o->op_flags &= ~OPf_PARENS;
1182 if (PL_hints & HINT_BLOCK_SCOPE)
1183 o->op_flags |= OPf_PARENS;
1186 o = newOP(OP_STUB, 0);
1191 S_modkids(pTHX_ OP *o, I32 type)
1194 if (o && o->op_flags & OPf_KIDS) {
1195 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1202 Perl_mod(pTHX_ OP *o, I32 type)
1209 if (!o || PL_error_count)
1212 if ((o->op_private & OPpTARGET_MY)
1213 && (PL_opargs[o->op_type] & OA_TARGLEX)) /* OPp share the meaning */
1216 switch (o->op_type) {
1221 if (!(o->op_private & (OPpCONST_ARYBASE)))
1223 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1224 PL_compiling.cop_arybase = (I32)SvIV(((SVOP*)PL_eval_start)->op_sv);
1228 SAVEI32(PL_compiling.cop_arybase);
1229 PL_compiling.cop_arybase = 0;
1231 else if (type == OP_REFGEN)
1234 Perl_croak(aTHX_ "That use of $[ is unsupported");
1237 if (o->op_flags & OPf_PARENS)
1241 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1242 !(o->op_flags & OPf_STACKED)) {
1243 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1244 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1245 assert(cUNOPo->op_first->op_type == OP_NULL);
1246 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1252 /* grep, foreach, subcalls, refgen */
1253 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1255 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1256 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1257 ? "do block" : PL_op_desc[o->op_type]),
1258 type ? PL_op_desc[type] : "local"));
1272 case OP_RIGHT_SHIFT:
1281 if (!(o->op_flags & OPf_STACKED))
1287 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1293 if (!type && cUNOPo->op_first->op_type != OP_GV)
1294 Perl_croak(aTHX_ "Can't localize through a reference");
1295 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1296 PL_modcount = 10000;
1297 return o; /* Treat \(@foo) like ordinary list. */
1301 if (scalar_mod_type(o, type))
1303 ref(cUNOPo->op_first, o->op_type);
1312 PL_modcount = 10000;
1315 if (!type && cUNOPo->op_first->op_type != OP_GV)
1316 Perl_croak(aTHX_ "Can't localize through a reference");
1317 ref(cUNOPo->op_first, o->op_type);
1321 PL_hints |= HINT_BLOCK_SCOPE;
1331 PL_modcount = 10000;
1332 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1333 return o; /* Treat \(@foo) like ordinary list. */
1334 if (scalar_mod_type(o, type))
1340 Perl_croak(aTHX_ "Can't localize lexical variable %s",
1341 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
1346 PL_modcount++; /* XXX ??? */
1348 #endif /* USE_THREADS */
1354 if (type != OP_SASSIGN)
1358 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1364 pad_free(o->op_targ);
1365 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1366 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1367 if (o->op_flags & OPf_KIDS)
1368 mod(cBINOPo->op_first->op_sibling, type);
1373 ref(cBINOPo->op_first, o->op_type);
1374 if (type == OP_ENTERSUB &&
1375 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1376 o->op_private |= OPpLVAL_DEFER;
1383 if (o->op_flags & OPf_KIDS)
1384 mod(cLISTOPo->op_last, type);
1388 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1390 else if (!(o->op_flags & OPf_KIDS))
1392 if (o->op_targ != OP_LIST) {
1393 mod(cBINOPo->op_first, type);
1398 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1402 o->op_flags |= OPf_MOD;
1404 if (type == OP_AASSIGN || type == OP_SASSIGN)
1405 o->op_flags |= OPf_SPECIAL|OPf_REF;
1407 o->op_private |= OPpLVAL_INTRO;
1408 o->op_flags &= ~OPf_SPECIAL;
1409 PL_hints |= HINT_BLOCK_SCOPE;
1411 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1412 o->op_flags |= OPf_REF;
1417 S_scalar_mod_type(pTHX_ OP *o, I32 type)
1421 if (o->op_type == OP_RV2GV)
1445 case OP_RIGHT_SHIFT:
1464 S_is_handle_constructor(pTHX_ OP *o, I32 argnum)
1466 switch (o->op_type) {
1474 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1487 Perl_refkids(pTHX_ OP *o, I32 type)
1490 if (o && o->op_flags & OPf_KIDS) {
1491 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1498 Perl_ref(pTHX_ OP *o, I32 type)
1502 if (!o || PL_error_count)
1505 switch (o->op_type) {
1507 if ((type == OP_DEFINED || type == OP_LOCK) &&
1508 !(o->op_flags & OPf_STACKED)) {
1509 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1510 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1511 assert(cUNOPo->op_first->op_type == OP_NULL);
1512 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1513 o->op_flags |= OPf_SPECIAL;
1518 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1522 if (type == OP_DEFINED)
1523 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1524 ref(cUNOPo->op_first, o->op_type);
1527 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1528 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1529 : type == OP_RV2HV ? OPpDEREF_HV
1531 o->op_flags |= OPf_MOD;
1536 o->op_flags |= OPf_MOD; /* XXX ??? */
1541 o->op_flags |= OPf_REF;
1544 if (type == OP_DEFINED)
1545 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1546 ref(cUNOPo->op_first, o->op_type);
1551 o->op_flags |= OPf_REF;
1556 if (!(o->op_flags & OPf_KIDS))
1558 ref(cBINOPo->op_first, type);
1562 ref(cBINOPo->op_first, o->op_type);
1563 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1564 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1565 : type == OP_RV2HV ? OPpDEREF_HV
1567 o->op_flags |= OPf_MOD;
1575 if (!(o->op_flags & OPf_KIDS))
1577 ref(cLISTOPo->op_last, type);
1587 Perl_my(pTHX_ OP *o)
1592 if (!o || PL_error_count)
1596 if (type == OP_LIST) {
1597 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1599 } else if (type == OP_UNDEF) {
1601 } else if (type != OP_PADSV &&
1604 type != OP_PUSHMARK)
1606 yyerror(Perl_form(aTHX_ "Can't declare %s in my", PL_op_desc[o->op_type]));
1609 o->op_flags |= OPf_MOD;
1610 o->op_private |= OPpLVAL_INTRO;
1615 Perl_sawparens(pTHX_ OP *o)
1618 o->op_flags |= OPf_PARENS;
1623 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
1628 if (ckWARN(WARN_UNSAFE) &&
1629 (left->op_type == OP_RV2AV ||
1630 left->op_type == OP_RV2HV ||
1631 left->op_type == OP_PADAV ||
1632 left->op_type == OP_PADHV)) {
1633 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
1634 right->op_type == OP_TRANS)
1635 ? right->op_type : OP_MATCH];
1636 char *sample = ((left->op_type == OP_RV2AV ||
1637 left->op_type == OP_PADAV)
1638 ? "@array" : "%hash");
1639 Perl_warner(aTHX_ WARN_UNSAFE,
1640 "Applying %s to %s will act on scalar(%s)",
1641 desc, sample, sample);
1644 if (right->op_type == OP_MATCH ||
1645 right->op_type == OP_SUBST ||
1646 right->op_type == OP_TRANS) {
1647 right->op_flags |= OPf_STACKED;
1648 if (right->op_type != OP_MATCH)
1649 left = mod(left, right->op_type);
1650 if (right->op_type == OP_TRANS)
1651 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1653 o = prepend_elem(right->op_type, scalar(left), right);
1655 return newUNOP(OP_NOT, 0, scalar(o));
1659 return bind_match(type, left,
1660 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1664 Perl_invert(pTHX_ OP *o)
1668 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1669 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1673 Perl_scope(pTHX_ OP *o)
1676 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
1677 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1678 o->op_type = OP_LEAVE;
1679 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
1682 if (o->op_type == OP_LINESEQ) {
1684 o->op_type = OP_SCOPE;
1685 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
1686 kid = ((LISTOP*)o)->op_first;
1687 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE){
1688 cop_free((COP*)kid);
1693 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1700 Perl_save_hints(pTHX)
1703 SAVESPTR(GvHV(PL_hintgv));
1704 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
1705 SAVEFREESV(GvHV(PL_hintgv));
1709 Perl_block_start(pTHX_ int full)
1712 int retval = PL_savestack_ix;
1714 SAVEI32(PL_comppad_name_floor);
1716 if ((PL_comppad_name_fill = AvFILLp(PL_comppad_name)) > 0)
1717 PL_comppad_name_floor = PL_comppad_name_fill;
1719 PL_comppad_name_floor = 0;
1721 SAVEI32(PL_min_intro_pending);
1722 SAVEI32(PL_max_intro_pending);
1723 PL_min_intro_pending = 0;
1724 SAVEI32(PL_comppad_name_fill);
1725 SAVEI32(PL_padix_floor);
1726 PL_padix_floor = PL_padix;
1727 PL_pad_reset_pending = FALSE;
1729 PL_hints &= ~HINT_BLOCK_SCOPE;
1730 SAVEPPTR(PL_compiling.cop_warnings);
1731 if (! specialWARN(PL_compiling.cop_warnings)) {
1732 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1733 SAVEFREESV(PL_compiling.cop_warnings) ;
1741 Perl_block_end(pTHX_ I32 floor, OP *seq)
1744 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
1745 OP* retval = scalarseq(seq);
1747 PL_pad_reset_pending = FALSE;
1748 PL_compiling.op_private = PL_hints;
1750 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1751 pad_leavemy(PL_comppad_name_fill);
1760 OP *o = newOP(OP_THREADSV, 0);
1761 o->op_targ = find_threadsv("_");
1764 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
1765 #endif /* USE_THREADS */
1769 Perl_newPROG(pTHX_ OP *o)
1775 PL_eval_root = newUNOP(OP_LEAVEEVAL,
1776 ((PL_in_eval & EVAL_KEEPERR)
1777 ? OPf_SPECIAL : 0), o);
1778 PL_eval_start = linklist(PL_eval_root);
1779 PL_eval_root->op_next = 0;
1780 peep(PL_eval_start);
1785 PL_main_root = scope(sawparens(scalarvoid(o)));
1786 PL_curcop = &PL_compiling;
1787 PL_main_start = LINKLIST(PL_main_root);
1788 PL_main_root->op_next = 0;
1789 peep(PL_main_start);
1792 /* Register with debugger */
1794 CV *cv = get_cv("DB::postponed", FALSE);
1798 XPUSHs((SV*)PL_compiling.cop_filegv);
1800 call_sv((SV*)cv, G_DISCARD);
1807 Perl_localize(pTHX_ OP *o, I32 lex)
1809 if (o->op_flags & OPf_PARENS)
1813 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
1815 for (s = PL_bufptr; *s && (isALNUM(*s) || (*s & 0x80) || strchr("@$%, ",*s)); s++) ;
1816 if (*s == ';' || *s == '=')
1817 Perl_warner(aTHX_ WARN_PARENTHESIS, "Parentheses missing around \"%s\" list",
1818 lex ? "my" : "local");
1822 PL_in_my_stash = Nullhv;
1826 return mod(o, OP_NULL); /* a bit kludgey */
1830 Perl_jmaybe(pTHX_ OP *o)
1832 if (o->op_type == OP_LIST) {
1835 o2 = newOP(OP_THREADSV, 0);
1836 o2->op_targ = find_threadsv(";");
1838 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
1839 #endif /* USE_THREADS */
1840 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
1846 Perl_fold_constants(pTHX_ register OP *o)
1850 I32 type = o->op_type;
1853 if (PL_opargs[type] & OA_RETSCALAR)
1855 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
1856 o->op_targ = pad_alloc(type, SVs_PADTMP);
1858 /* integerize op, unless it happens to be C<-foo>.
1859 * XXX should pp_i_negate() do magic string negation instead? */
1860 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
1861 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
1862 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
1864 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
1867 if (!(PL_opargs[type] & OA_FOLDCONST))
1872 /* XXX might want a ck_negate() for this */
1873 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
1886 if (o->op_private & OPpLOCALE)
1891 goto nope; /* Don't try to run w/ errors */
1893 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
1894 if (curop->op_type != OP_CONST &&
1895 curop->op_type != OP_LIST &&
1896 curop->op_type != OP_SCALAR &&
1897 curop->op_type != OP_NULL &&
1898 curop->op_type != OP_PUSHMARK)
1904 curop = LINKLIST(o);
1908 sv = *(PL_stack_sp--);
1909 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
1910 pad_swipe(o->op_targ);
1911 else if (SvTEMP(sv)) { /* grab mortal temp? */
1912 (void)SvREFCNT_inc(sv);
1916 if (type == OP_RV2GV)
1917 return newGVOP(OP_GV, 0, (GV*)sv);
1919 /* try to smush double to int, but don't smush -2.0 to -2 */
1920 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
1924 if ((NV)iv == SvNV(sv)) {
1929 SvIOK_off(sv); /* undo SvIV() damage */
1931 return newSVOP(OP_CONST, 0, sv);
1935 if (!(PL_opargs[type] & OA_OTHERINT))
1938 if (!(PL_hints & HINT_INTEGER)) {
1939 if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS))
1942 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
1943 if (curop->op_type == OP_CONST) {
1944 if (SvIOK(((SVOP*)curop)->op_sv))
1948 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
1952 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
1959 Perl_gen_constant_list(pTHX_ register OP *o)
1963 I32 oldtmps_floor = PL_tmps_floor;
1967 return o; /* Don't attempt to run with errors */
1969 PL_op = curop = LINKLIST(o);
1975 PL_tmps_floor = oldtmps_floor;
1977 o->op_type = OP_RV2AV;
1978 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
1979 curop = ((UNOP*)o)->op_first;
1980 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
1987 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
1992 if (!o || o->op_type != OP_LIST)
1993 o = newLISTOP(OP_LIST, 0, o, Nullop);
1995 o->op_flags &= ~OPf_WANT;
1997 if (!(PL_opargs[type] & OA_MARK))
1998 null(cLISTOPo->op_first);
2001 o->op_ppaddr = PL_ppaddr[type];
2002 o->op_flags |= flags;
2004 o = CHECKOP(type, o);
2005 if (o->op_type != type)
2008 if (cLISTOPo->op_children < 7) {
2009 /* XXX do we really need to do this if we're done appending?? */
2010 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2012 cLISTOPo->op_last = last; /* in case check substituted last arg */
2015 return fold_constants(o);
2018 /* List constructors */
2021 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2029 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
2030 return newLISTOP(type, 0, first, last);
2032 if (first->op_flags & OPf_KIDS)
2033 ((LISTOP*)first)->op_last->op_sibling = last;
2035 first->op_flags |= OPf_KIDS;
2036 ((LISTOP*)first)->op_first = last;
2038 ((LISTOP*)first)->op_last = last;
2039 ((LISTOP*)first)->op_children++;
2044 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2052 if (first->op_type != type)
2053 return prepend_elem(type, (OP*)first, (OP*)last);
2055 if (last->op_type != type)
2056 return append_elem(type, (OP*)first, (OP*)last);
2058 first->op_last->op_sibling = last->op_first;
2059 first->op_last = last->op_last;
2060 first->op_children += last->op_children;
2061 if (first->op_children)
2062 first->op_flags |= OPf_KIDS;
2064 #ifdef PL_OP_SLAB_ALLOC
2072 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2080 if (last->op_type == type) {
2081 if (type == OP_LIST) { /* already a PUSHMARK there */
2082 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2083 ((LISTOP*)last)->op_first->op_sibling = first;
2086 if (!(last->op_flags & OPf_KIDS)) {
2087 ((LISTOP*)last)->op_last = first;
2088 last->op_flags |= OPf_KIDS;
2090 first->op_sibling = ((LISTOP*)last)->op_first;
2091 ((LISTOP*)last)->op_first = first;
2093 ((LISTOP*)last)->op_children++;
2097 return newLISTOP(type, 0, first, last);
2103 Perl_newNULLLIST(pTHX)
2105 return newOP(OP_STUB, 0);
2109 Perl_force_list(pTHX_ OP *o)
2111 if (!o || o->op_type != OP_LIST)
2112 o = newLISTOP(OP_LIST, 0, o, Nullop);
2118 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2122 NewOp(1101, listop, 1, LISTOP);
2124 listop->op_type = type;
2125 listop->op_ppaddr = PL_ppaddr[type];
2126 listop->op_children = (first != 0) + (last != 0);
2127 listop->op_flags = flags;
2131 else if (!first && last)
2134 first->op_sibling = last;
2135 listop->op_first = first;
2136 listop->op_last = last;
2137 if (type == OP_LIST) {
2139 pushop = newOP(OP_PUSHMARK, 0);
2140 pushop->op_sibling = first;
2141 listop->op_first = pushop;
2142 listop->op_flags |= OPf_KIDS;
2144 listop->op_last = pushop;
2146 else if (listop->op_children)
2147 listop->op_flags |= OPf_KIDS;
2153 Perl_newOP(pTHX_ I32 type, I32 flags)
2156 NewOp(1101, o, 1, OP);
2158 o->op_ppaddr = PL_ppaddr[type];
2159 o->op_flags = flags;
2162 o->op_private = 0 + (flags >> 8);
2163 if (PL_opargs[type] & OA_RETSCALAR)
2165 if (PL_opargs[type] & OA_TARGET)
2166 o->op_targ = pad_alloc(type, SVs_PADTMP);
2167 return CHECKOP(type, o);
2171 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
2176 first = newOP(OP_STUB, 0);
2177 if (PL_opargs[type] & OA_MARK)
2178 first = force_list(first);
2180 NewOp(1101, unop, 1, UNOP);
2181 unop->op_type = type;
2182 unop->op_ppaddr = PL_ppaddr[type];
2183 unop->op_first = first;
2184 unop->op_flags = flags | OPf_KIDS;
2185 unop->op_private = 1 | (flags >> 8);
2186 unop = (UNOP*) CHECKOP(type, unop);
2190 return fold_constants((OP *) unop);
2194 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2197 NewOp(1101, binop, 1, BINOP);
2200 first = newOP(OP_NULL, 0);
2202 binop->op_type = type;
2203 binop->op_ppaddr = PL_ppaddr[type];
2204 binop->op_first = first;
2205 binop->op_flags = flags | OPf_KIDS;
2208 binop->op_private = 1 | (flags >> 8);
2211 binop->op_private = 2 | (flags >> 8);
2212 first->op_sibling = last;
2215 binop = (BINOP*)CHECKOP(type, binop);
2216 if (binop->op_next || binop->op_type != type)
2219 binop->op_last = binop->op_first->op_sibling;
2221 return fold_constants((OP *)binop);
2225 utf8compare(const void *a, const void *b)
2228 for (i = 0; i < 10; i++) {
2229 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2231 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2238 Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
2240 SV *tstr = ((SVOP*)expr)->op_sv;
2241 SV *rstr = ((SVOP*)repl)->op_sv;
2244 register U8 *t = (U8*)SvPV(tstr, tlen);
2245 register U8 *r = (U8*)SvPV(rstr, rlen);
2251 register short *tbl;
2253 complement = o->op_private & OPpTRANS_COMPLEMENT;
2254 del = o->op_private & OPpTRANS_DELETE;
2255 squash = o->op_private & OPpTRANS_SQUASH;
2257 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2258 SV* listsv = newSVpvn("# comment\n",10);
2260 U8* tend = t + tlen;
2261 U8* rend = r + rlen;
2277 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2278 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2284 New(1109, cp, tlen, U8*);
2286 transv = newSVpvn("",0);
2295 qsort(cp, i, sizeof(U8*), utf8compare);
2296 for (j = 0; j < i; j++) {
2298 UV val = utf8_to_uv(s, &ulen);
2300 diff = val - nextmin;
2302 t = uv_to_utf8(tmpbuf,nextmin);
2303 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2305 t = uv_to_utf8(tmpbuf, val - 1);
2306 sv_catpvn(transv, "\377", 1);
2307 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2311 val = utf8_to_uv(s+1, &ulen);
2315 t = uv_to_utf8(tmpbuf,nextmin);
2316 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2317 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2318 sv_catpvn(transv, "\377", 1);
2319 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2320 t = (U8*)SvPVX(transv);
2321 tlen = SvCUR(transv);
2324 else if (!rlen && !del) {
2325 r = t; rlen = tlen; rend = tend;
2328 if (to_utf && from_utf) { /* only counting characters */
2329 if (t == r || (tlen == rlen && memEQ(t, r, tlen)))
2330 o->op_private |= OPpTRANS_IDENTICAL;
2332 else { /* straight latin-1 translation */
2333 if (tlen == 4 && memEQ(t, "\0\377\303\277", 4) &&
2334 rlen == 4 && memEQ(r, "\0\377\303\277", 4))
2335 o->op_private |= OPpTRANS_IDENTICAL;
2339 while (t < tend || tfirst <= tlast) {
2340 /* see if we need more "t" chars */
2341 if (tfirst > tlast) {
2342 tfirst = (I32)utf8_to_uv(t, &ulen);
2344 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
2345 tlast = (I32)utf8_to_uv(++t, &ulen);
2352 /* now see if we need more "r" chars */
2353 if (rfirst > rlast) {
2355 rfirst = (I32)utf8_to_uv(r, &ulen);
2357 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
2358 rlast = (I32)utf8_to_uv(++r, &ulen);
2367 rfirst = rlast = 0xffffffff;
2371 /* now see which range will peter our first, if either. */
2372 tdiff = tlast - tfirst;
2373 rdiff = rlast - rfirst;
2380 if (rfirst == 0xffffffff) {
2381 diff = tdiff; /* oops, pretend rdiff is infinite */
2383 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\tXXXX\n", tfirst, tlast);
2385 Perl_sv_catpvf(aTHX_ listsv, "%04x\t\tXXXX\n", tfirst);
2389 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\t%04x\n", tfirst, tfirst + diff, rfirst);
2391 Perl_sv_catpvf(aTHX_ listsv, "%04x\t\t%04x\n", tfirst, rfirst);
2393 if (rfirst + diff > max)
2394 max = rfirst + diff;
2399 else if (rfirst <= 0x800)
2400 grows |= (tfirst < 0x80);
2401 else if (rfirst <= 0x10000)
2402 grows |= (tfirst < 0x800);
2403 else if (rfirst <= 0x200000)
2404 grows |= (tfirst < 0x10000);
2405 else if (rfirst <= 0x4000000)
2406 grows |= (tfirst < 0x200000);
2407 else if (rfirst <= 0x80000000)
2408 grows |= (tfirst < 0x4000000);
2420 else if (max > 0xff)
2425 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2426 SvREFCNT_dec(listsv);
2428 SvREFCNT_dec(transv);
2430 if (!del && havefinal)
2431 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5, newSViv((IV)final), 0);
2433 if (grows && to_utf)
2434 o->op_private |= OPpTRANS_GROWS;
2441 tbl = (short*)cPVOPo->op_pv;
2443 Zero(tbl, 256, short);
2444 for (i = 0; i < tlen; i++)
2446 for (i = 0, j = 0; i < 256; i++) {
2462 if (!rlen && !del) {
2465 o->op_private |= OPpTRANS_IDENTICAL;
2467 for (i = 0; i < 256; i++)
2469 for (i = 0, j = 0; i < tlen; i++,j++) {
2472 if (tbl[t[i]] == -1)
2478 if (tbl[t[i]] == -1)
2489 Perl_newPMOP(pTHX_ I32 type, I32 flags)
2494 NewOp(1101, pmop, 1, PMOP);
2495 pmop->op_type = type;
2496 pmop->op_ppaddr = PL_ppaddr[type];
2497 pmop->op_flags = flags;
2498 pmop->op_private = 0 | (flags >> 8);
2500 if (PL_hints & HINT_RE_TAINT)
2501 pmop->op_pmpermflags |= PMf_RETAINT;
2502 if (PL_hints & HINT_LOCALE)
2503 pmop->op_pmpermflags |= PMf_LOCALE;
2504 pmop->op_pmflags = pmop->op_pmpermflags;
2506 /* link into pm list */
2507 if (type != OP_TRANS && PL_curstash) {
2508 pmop->op_pmnext = HvPMROOT(PL_curstash);
2509 HvPMROOT(PL_curstash) = pmop;
2516 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
2521 I32 repl_has_vars = 0;
2523 if (o->op_type == OP_TRANS)
2524 return pmtrans(o, expr, repl);
2526 PL_hints |= HINT_BLOCK_SCOPE;
2529 if (expr->op_type == OP_CONST) {
2531 SV *pat = ((SVOP*)expr)->op_sv;
2532 char *p = SvPV(pat, plen);
2533 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2534 sv_setpvn(pat, "\\s+", 3);
2535 p = SvPV(pat, plen);
2536 pm->op_pmflags |= PMf_SKIPWHITE;
2538 pm->op_pmregexp = CALLREGCOMP(aTHX_ p, p + plen, pm);
2539 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2540 pm->op_pmflags |= PMf_WHITE;
2544 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2545 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2547 : OP_REGCMAYBE),0,expr);
2549 NewOp(1101, rcop, 1, LOGOP);
2550 rcop->op_type = OP_REGCOMP;
2551 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2552 rcop->op_first = scalar(expr);
2553 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2554 ? (OPf_SPECIAL | OPf_KIDS)
2556 rcop->op_private = 1;
2559 /* establish postfix order */
2560 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2562 rcop->op_next = expr;
2563 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2566 rcop->op_next = LINKLIST(expr);
2567 expr->op_next = (OP*)rcop;
2570 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2575 if (pm->op_pmflags & PMf_EVAL) {
2577 if (PL_curcop->cop_line < PL_multi_end)
2578 PL_curcop->cop_line = PL_multi_end;
2581 else if (repl->op_type == OP_THREADSV
2582 && strchr("&`'123456789+",
2583 PL_threadsv_names[repl->op_targ]))
2587 #endif /* USE_THREADS */
2588 else if (repl->op_type == OP_CONST)
2592 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2593 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2595 if (curop->op_type == OP_THREADSV) {
2597 if (strchr("&`'123456789+", curop->op_private))
2601 if (curop->op_type == OP_GV) {
2602 GV *gv = ((GVOP*)curop)->op_gv;
2604 if (strchr("&`'123456789+", *GvENAME(gv)))
2607 #endif /* USE_THREADS */
2608 else if (curop->op_type == OP_RV2CV)
2610 else if (curop->op_type == OP_RV2SV ||
2611 curop->op_type == OP_RV2AV ||
2612 curop->op_type == OP_RV2HV ||
2613 curop->op_type == OP_RV2GV) {
2614 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2617 else if (curop->op_type == OP_PADSV ||
2618 curop->op_type == OP_PADAV ||
2619 curop->op_type == OP_PADHV ||
2620 curop->op_type == OP_PADANY) {
2623 else if (curop->op_type == OP_PUSHRE)
2624 ; /* Okay here, dangerous in newASSIGNOP */
2633 && (!pm->op_pmregexp
2634 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
2635 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2636 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2637 prepend_elem(o->op_type, scalar(repl), o);
2640 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
2641 pm->op_pmflags |= PMf_MAYBE_CONST;
2642 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2644 NewOp(1101, rcop, 1, LOGOP);
2645 rcop->op_type = OP_SUBSTCONT;
2646 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
2647 rcop->op_first = scalar(repl);
2648 rcop->op_flags |= OPf_KIDS;
2649 rcop->op_private = 1;
2652 /* establish postfix order */
2653 rcop->op_next = LINKLIST(repl);
2654 repl->op_next = (OP*)rcop;
2656 pm->op_pmreplroot = scalar((OP*)rcop);
2657 pm->op_pmreplstart = LINKLIST(rcop);
2666 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
2669 NewOp(1101, svop, 1, SVOP);
2670 svop->op_type = type;
2671 svop->op_ppaddr = PL_ppaddr[type];
2673 svop->op_next = (OP*)svop;
2674 svop->op_flags = flags;
2675 if (PL_opargs[type] & OA_RETSCALAR)
2677 if (PL_opargs[type] & OA_TARGET)
2678 svop->op_targ = pad_alloc(type, SVs_PADTMP);
2679 return CHECKOP(type, svop);
2683 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
2687 NewOp(1101, gvop, 1, GVOP);
2688 gvop->op_type = type;
2689 gvop->op_ppaddr = PL_ppaddr[type];
2690 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
2691 gvop->op_next = (OP*)gvop;
2692 gvop->op_flags = flags;
2693 if (PL_opargs[type] & OA_RETSCALAR)
2695 if (PL_opargs[type] & OA_TARGET)
2696 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
2697 return CHECKOP(type, gvop);
2701 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
2704 NewOp(1101, pvop, 1, PVOP);
2705 pvop->op_type = type;
2706 pvop->op_ppaddr = PL_ppaddr[type];
2708 pvop->op_next = (OP*)pvop;
2709 pvop->op_flags = flags;
2710 if (PL_opargs[type] & OA_RETSCALAR)
2712 if (PL_opargs[type] & OA_TARGET)
2713 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2714 return CHECKOP(type, pvop);
2718 Perl_package(pTHX_ OP *o)
2723 save_hptr(&PL_curstash);
2724 save_item(PL_curstname);
2729 name = SvPV(sv, len);
2730 PL_curstash = gv_stashpvn(name,len,TRUE);
2731 sv_setpvn(PL_curstname, name, len);
2735 sv_setpv(PL_curstname,"<none>");
2736 PL_curstash = Nullhv;
2738 PL_hints |= HINT_BLOCK_SCOPE;
2739 PL_copline = NOLINE;
2744 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg)
2753 if (id->op_type != OP_CONST)
2754 Perl_croak(aTHX_ "Module name must be constant");
2758 if(version != Nullop) {
2759 SV *vesv = ((SVOP*)version)->op_sv;
2761 if (arg == Nullop && !SvNIOK(vesv)) {
2768 if (version->op_type != OP_CONST || !SvNIOK(vesv))
2769 Perl_croak(aTHX_ "Version number must be constant number");
2771 /* Make copy of id so we don't free it twice */
2772 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2774 /* Fake up a method call to VERSION */
2775 meth = newSVOP(OP_CONST, 0, newSVpvn("VERSION", 7));
2776 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2777 append_elem(OP_LIST,
2778 prepend_elem(OP_LIST, pack, list(version)),
2779 newUNOP(OP_METHOD, 0, meth)));
2783 /* Fake up an import/unimport */
2784 if (arg && arg->op_type == OP_STUB)
2785 imop = arg; /* no import on explicit () */
2786 else if(SvNIOK(((SVOP*)id)->op_sv)) {
2787 imop = Nullop; /* use 5.0; */
2790 /* Make copy of id so we don't free it twice */
2791 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2792 meth = newSVOP(OP_CONST, 0,
2794 ? newSVpvn("import", 6)
2795 : newSVpvn("unimport", 8)
2797 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2798 append_elem(OP_LIST,
2799 prepend_elem(OP_LIST, pack, list(arg)),
2800 newUNOP(OP_METHOD, 0, meth)));
2803 /* Fake up a require, handle override, if any */
2804 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
2805 if (!(gv && GvIMPORTED_CV(gv)))
2806 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
2808 if (gv && GvIMPORTED_CV(gv)) {
2809 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
2810 append_elem(OP_LIST, id,
2811 scalar(newUNOP(OP_RV2CV, 0,
2816 rqop = newUNOP(OP_REQUIRE, 0, id);
2819 /* Fake up the BEGIN {}, which does its thing immediately. */
2821 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
2823 append_elem(OP_LINESEQ,
2824 append_elem(OP_LINESEQ,
2825 newSTATEOP(0, Nullch, rqop),
2826 newSTATEOP(0, Nullch, veop)),
2827 newSTATEOP(0, Nullch, imop) ));
2829 PL_copline = NOLINE;
2834 Perl_dofile(pTHX_ OP *term)
2839 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
2840 if (!(gv && GvIMPORTED_CV(gv)))
2841 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
2843 if (gv && GvIMPORTED_CV(gv)) {
2844 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
2845 append_elem(OP_LIST, term,
2846 scalar(newUNOP(OP_RV2CV, 0,
2851 doop = newUNOP(OP_DOFILE, 0, scalar(term));
2857 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
2859 return newBINOP(OP_LSLICE, flags,
2860 list(force_list(subscript)),
2861 list(force_list(listval)) );
2865 S_list_assignment(pTHX_ register OP *o)
2870 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
2871 o = cUNOPo->op_first;
2873 if (o->op_type == OP_COND_EXPR) {
2874 I32 t = list_assignment(cLOGOPo->op_first->op_sibling);
2875 I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
2880 yyerror("Assignment to both a list and a scalar");
2884 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
2885 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
2886 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
2889 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
2892 if (o->op_type == OP_RV2SV)
2899 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
2904 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
2905 return newLOGOP(optype, 0,
2906 mod(scalar(left), optype),
2907 newUNOP(OP_SASSIGN, 0, scalar(right)));
2910 return newBINOP(optype, OPf_STACKED,
2911 mod(scalar(left), optype), scalar(right));
2915 if (list_assignment(left)) {
2918 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2919 left = mod(left, OP_AASSIGN);
2927 o = newBINOP(OP_AASSIGN, flags,
2928 list(force_list(right)),
2929 list(force_list(left)) );
2930 o->op_private = 0 | (flags >> 8);
2931 if (!(left->op_private & OPpLVAL_INTRO)) {
2935 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2936 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2937 if (curop->op_type == OP_GV) {
2938 GV *gv = ((GVOP*)curop)->op_gv;
2939 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
2941 SvCUR(gv) = PL_generation;
2943 else if (curop->op_type == OP_PADSV ||
2944 curop->op_type == OP_PADAV ||
2945 curop->op_type == OP_PADHV ||
2946 curop->op_type == OP_PADANY) {
2947 SV **svp = AvARRAY(PL_comppad_name);
2948 SV *sv = svp[curop->op_targ];
2949 if (SvCUR(sv) == PL_generation)
2951 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
2953 else if (curop->op_type == OP_RV2CV)
2955 else if (curop->op_type == OP_RV2SV ||
2956 curop->op_type == OP_RV2AV ||
2957 curop->op_type == OP_RV2HV ||
2958 curop->op_type == OP_RV2GV) {
2959 if (lastop->op_type != OP_GV) /* funny deref? */
2962 else if (curop->op_type == OP_PUSHRE) {
2963 if (((PMOP*)curop)->op_pmreplroot) {
2964 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
2965 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
2967 SvCUR(gv) = PL_generation;
2976 o->op_private = OPpASSIGN_COMMON;
2978 if (right && right->op_type == OP_SPLIT) {
2980 if ((tmpop = ((LISTOP*)right)->op_first) &&
2981 tmpop->op_type == OP_PUSHRE)
2983 PMOP *pm = (PMOP*)tmpop;
2984 if (left->op_type == OP_RV2AV &&
2985 !(left->op_private & OPpLVAL_INTRO) &&
2986 !(o->op_private & OPpASSIGN_COMMON) )
2988 tmpop = ((UNOP*)left)->op_first;
2989 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
2990 pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
2991 pm->op_pmflags |= PMf_ONCE;
2992 tmpop = cUNOPo->op_first; /* to list (nulled) */
2993 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
2994 tmpop->op_sibling = Nullop; /* don't free split */
2995 right->op_next = tmpop->op_next; /* fix starting loc */
2996 op_free(o); /* blow off assign */
2997 right->op_flags &= ~OPf_WANT;
2998 /* "I don't know and I don't care." */
3003 if (PL_modcount < 10000 &&
3004 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3006 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3008 sv_setiv(sv, PL_modcount+1);
3016 right = newOP(OP_UNDEF, 0);
3017 if (right->op_type == OP_READLINE) {
3018 right->op_flags |= OPf_STACKED;
3019 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3022 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3023 o = newBINOP(OP_SASSIGN, flags,
3024 scalar(right), mod(scalar(left), OP_SASSIGN) );
3036 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3039 U32 seq = intro_my();
3042 NewOp(1101, cop, 1, COP);
3043 if (PERLDB_LINE && PL_curcop->cop_line && PL_curstash != PL_debstash) {
3044 cop->op_type = OP_DBSTATE;
3045 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3048 cop->op_type = OP_NEXTSTATE;
3049 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3051 cop->op_flags = flags;
3052 cop->op_private = (PL_hints & HINT_UTF8);
3054 cop->op_private |= NATIVE_HINTS;
3056 PL_compiling.op_private = cop->op_private;
3057 cop->op_next = (OP*)cop;
3060 cop->cop_label = label;
3061 PL_hints |= HINT_BLOCK_SCOPE;
3064 cop->cop_arybase = PL_curcop->cop_arybase;
3065 if (specialWARN(PL_curcop->cop_warnings))
3066 cop->cop_warnings = PL_curcop->cop_warnings ;
3068 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3071 if (PL_copline == NOLINE)
3072 cop->cop_line = PL_curcop->cop_line;
3074 cop->cop_line = PL_copline;
3075 PL_copline = NOLINE;
3077 cop->cop_filegv = (GV*)SvREFCNT_inc(PL_curcop->cop_filegv);
3078 cop->cop_stash = PL_curstash;
3080 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3081 SV **svp = av_fetch(GvAV(PL_curcop->cop_filegv),(I32)cop->cop_line, FALSE);
3082 if (svp && *svp != &PL_sv_undef && !SvIOK(*svp)) {
3083 (void)SvIOK_on(*svp);
3085 SvSTASH(*svp) = (HV*)cop;
3089 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3092 /* "Introduce" my variables to visible status. */
3100 if (! PL_min_intro_pending)
3101 return PL_cop_seqmax;
3103 svp = AvARRAY(PL_comppad_name);
3104 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
3105 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
3106 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
3107 SvNVX(sv) = (NV)PL_cop_seqmax;
3110 PL_min_intro_pending = 0;
3111 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
3112 return PL_cop_seqmax++;
3116 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
3118 return new_logop(type, flags, &first, &other);
3122 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
3127 OP *first = *firstp;
3128 OP *other = *otherp;
3130 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3131 return newBINOP(type, flags, scalar(first), scalar(other));
3133 scalarboolean(first);
3134 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3135 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3136 if (type == OP_AND || type == OP_OR) {
3142 first = *firstp = cUNOPo->op_first;
3144 first->op_next = o->op_next;
3145 cUNOPo->op_first = Nullop;
3149 if (first->op_type == OP_CONST) {
3150 if (ckWARN(WARN_PRECEDENCE) && (first->op_private & OPpCONST_BARE))
3151 Perl_warner(aTHX_ WARN_PRECEDENCE, "Probable precedence problem on %s",
3153 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3164 else if (first->op_type == OP_WANTARRAY) {
3170 else if (ckWARN(WARN_UNSAFE) && (first->op_flags & OPf_KIDS)) {
3171 OP *k1 = ((UNOP*)first)->op_first;
3172 OP *k2 = k1->op_sibling;
3174 switch (first->op_type)
3177 if (k2 && k2->op_type == OP_READLINE
3178 && (k2->op_flags & OPf_STACKED)
3179 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3180 warnop = k2->op_type;
3184 if (k1->op_type == OP_READDIR
3185 || k1->op_type == OP_GLOB
3186 || k1->op_type == OP_EACH)
3187 warnop = k1->op_type;
3191 line_t oldline = PL_curcop->cop_line;
3192 PL_curcop->cop_line = PL_copline;
3193 Perl_warner(aTHX_ WARN_UNSAFE,
3194 "Value of %s%s can be \"0\"; test with defined()",
3196 ((warnop == OP_READLINE || warnop == OP_GLOB)
3197 ? " construct" : "() operator"));
3198 PL_curcop->cop_line = oldline;
3205 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3206 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3208 NewOp(1101, logop, 1, LOGOP);
3210 logop->op_type = type;
3211 logop->op_ppaddr = PL_ppaddr[type];
3212 logop->op_first = first;
3213 logop->op_flags = flags | OPf_KIDS;
3214 logop->op_other = LINKLIST(other);
3215 logop->op_private = 1 | (flags >> 8);
3217 /* establish postfix order */
3218 logop->op_next = LINKLIST(first);
3219 first->op_next = (OP*)logop;
3220 first->op_sibling = other;
3222 o = newUNOP(OP_NULL, 0, (OP*)logop);
3229 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
3237 return newLOGOP(OP_AND, 0, first, trueop);
3239 return newLOGOP(OP_OR, 0, first, falseop);
3241 scalarboolean(first);
3242 if (first->op_type == OP_CONST) {
3243 if (SvTRUE(((SVOP*)first)->op_sv)) {
3254 else if (first->op_type == OP_WANTARRAY) {
3258 NewOp(1101, logop, 1, LOGOP);
3259 logop->op_type = OP_COND_EXPR;
3260 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3261 logop->op_first = first;
3262 logop->op_flags = flags | OPf_KIDS;
3263 logop->op_private = 1 | (flags >> 8);
3264 logop->op_other = LINKLIST(trueop);
3265 logop->op_next = LINKLIST(falseop);
3268 /* establish postfix order */
3269 start = LINKLIST(first);
3270 first->op_next = (OP*)logop;
3272 first->op_sibling = trueop;
3273 trueop->op_sibling = falseop;
3274 o = newUNOP(OP_NULL, 0, (OP*)logop);
3276 trueop->op_next = falseop->op_next = o;
3283 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
3292 NewOp(1101, range, 1, LOGOP);
3294 range->op_type = OP_RANGE;
3295 range->op_ppaddr = PL_ppaddr[OP_RANGE];
3296 range->op_first = left;
3297 range->op_flags = OPf_KIDS;
3298 leftstart = LINKLIST(left);
3299 range->op_other = LINKLIST(right);
3300 range->op_private = 1 | (flags >> 8);
3302 left->op_sibling = right;
3304 range->op_next = (OP*)range;
3305 flip = newUNOP(OP_FLIP, flags, (OP*)range);
3306 flop = newUNOP(OP_FLOP, 0, flip);
3307 o = newUNOP(OP_NULL, 0, flop);
3309 range->op_next = leftstart;
3311 left->op_next = flip;
3312 right->op_next = flop;
3314 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3315 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
3316 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3317 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3319 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3320 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3323 if (!flip->op_private || !flop->op_private)
3324 linklist(o); /* blow off optimizer unless constant */
3330 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
3335 int once = block && block->op_flags & OPf_SPECIAL &&
3336 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3339 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3340 return block; /* do {} while 0 does once */
3341 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3342 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3343 expr = newUNOP(OP_DEFINED, 0,
3344 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3345 } else if (expr->op_flags & OPf_KIDS) {
3346 OP *k1 = ((UNOP*)expr)->op_first;
3347 OP *k2 = (k1) ? k1->op_sibling : NULL;
3348 switch (expr->op_type) {
3350 if (k2 && k2->op_type == OP_READLINE
3351 && (k2->op_flags & OPf_STACKED)
3352 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3353 expr = newUNOP(OP_DEFINED, 0, expr);
3357 if (k1->op_type == OP_READDIR
3358 || k1->op_type == OP_GLOB
3359 || k1->op_type == OP_EACH)
3360 expr = newUNOP(OP_DEFINED, 0, expr);
3366 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3367 o = new_logop(OP_AND, 0, &expr, &listop);
3370 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3372 if (once && o != listop)
3373 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3376 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3378 o->op_flags |= flags;
3380 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3385 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
3394 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3395 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
3396 expr = newUNOP(OP_DEFINED, 0,
3397 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3398 } else if (expr && (expr->op_flags & OPf_KIDS)) {
3399 OP *k1 = ((UNOP*)expr)->op_first;
3400 OP *k2 = (k1) ? k1->op_sibling : NULL;
3401 switch (expr->op_type) {
3403 if (k2 && k2->op_type == OP_READLINE
3404 && (k2->op_flags & OPf_STACKED)
3405 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3406 expr = newUNOP(OP_DEFINED, 0, expr);
3410 if (k1->op_type == OP_READDIR
3411 || k1->op_type == OP_GLOB
3412 || k1->op_type == OP_EACH)
3413 expr = newUNOP(OP_DEFINED, 0, expr);
3419 block = newOP(OP_NULL, 0);
3422 next = LINKLIST(cont);
3424 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
3425 if ((line_t)whileline != NOLINE) {
3426 PL_copline = whileline;
3427 cont = append_elem(OP_LINESEQ, cont,
3428 newSTATEOP(0, Nullch, Nullop));
3432 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3433 redo = LINKLIST(listop);
3436 PL_copline = whileline;
3438 o = new_logop(OP_AND, 0, &expr, &listop);
3439 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3440 op_free(expr); /* oops, it's a while (0) */
3442 return Nullop; /* listop already freed by new_logop */
3445 ((LISTOP*)listop)->op_last->op_next = condop =
3446 (o == listop ? redo : LINKLIST(o));
3454 NewOp(1101,loop,1,LOOP);
3455 loop->op_type = OP_ENTERLOOP;
3456 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3457 loop->op_private = 0;
3458 loop->op_next = (OP*)loop;
3461 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3463 loop->op_redoop = redo;
3464 loop->op_lastop = o;
3467 loop->op_nextop = next;
3469 loop->op_nextop = o;
3471 o->op_flags |= flags;
3472 o->op_private |= (flags >> 8);
3477 Perl_newFOROP(pTHX_ I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
3486 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3487 sv->op_type = OP_RV2GV;
3488 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
3490 else if (sv->op_type == OP_PADSV) { /* private variable */
3491 padoff = sv->op_targ;
3495 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3496 padoff = sv->op_targ;
3497 iterflags |= OPf_SPECIAL;
3502 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
3506 padoff = find_threadsv("_");
3507 iterflags |= OPf_SPECIAL;
3509 sv = newGVOP(OP_GV, 0, PL_defgv);
3512 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3513 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3514 iterflags |= OPf_STACKED;
3516 else if (expr->op_type == OP_NULL &&
3517 (expr->op_flags & OPf_KIDS) &&
3518 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3520 /* Basically turn for($x..$y) into the same as for($x,$y), but we
3521 * set the STACKED flag to indicate that these values are to be
3522 * treated as min/max values by 'pp_iterinit'.
3524 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3525 LOGOP* range = (LOGOP*) flip->op_first;
3526 OP* left = range->op_first;
3527 OP* right = left->op_sibling;
3530 range->op_flags &= ~OPf_KIDS;
3531 range->op_first = Nullop;
3533 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3534 listop->op_first->op_next = range->op_next;
3535 left->op_next = range->op_other;
3536 right->op_next = (OP*)listop;
3537 listop->op_next = listop->op_first;
3540 expr = (OP*)(listop);
3542 iterflags |= OPf_STACKED;
3545 expr = mod(force_list(expr), OP_GREPSTART);
3549 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
3550 append_elem(OP_LIST, expr, scalar(sv))));
3551 assert(!loop->op_next);
3552 #ifdef PL_OP_SLAB_ALLOC
3553 NewOp(1234,tmp,1,LOOP);
3554 Copy(loop,tmp,1,LOOP);
3557 Renew(loop, 1, LOOP);
3559 loop->op_targ = padoff;
3560 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
3561 PL_copline = forline;
3562 return newSTATEOP(0, label, wop);
3566 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
3572 if (type != OP_GOTO || label->op_type == OP_CONST) {
3573 /* "last()" means "last" */
3574 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
3575 o = newOP(type, OPf_SPECIAL);
3577 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
3578 ? SvPVx(((SVOP*)label)->op_sv, n_a)
3584 if (label->op_type == OP_ENTERSUB)
3585 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
3586 o = newUNOP(type, OPf_STACKED, label);
3588 PL_hints |= HINT_BLOCK_SCOPE;
3593 Perl_cv_undef(pTHX_ CV *cv)
3598 MUTEX_DESTROY(CvMUTEXP(cv));
3599 Safefree(CvMUTEXP(cv));
3602 #endif /* USE_THREADS */
3604 if (!CvXSUB(cv) && CvROOT(cv)) {
3606 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
3607 Perl_croak(aTHX_ "Can't undef active subroutine");
3610 Perl_croak(aTHX_ "Can't undef active subroutine");
3611 #endif /* USE_THREADS */
3614 SAVESPTR(PL_curpad);
3618 op_free(CvROOT(cv));
3619 CvROOT(cv) = Nullop;
3622 SvPOK_off((SV*)cv); /* forget prototype */
3624 SvREFCNT_dec(CvGV(cv));
3626 SvREFCNT_dec(CvOUTSIDE(cv));
3627 CvOUTSIDE(cv) = Nullcv;
3628 if (CvPADLIST(cv)) {
3629 /* may be during global destruction */
3630 if (SvREFCNT(CvPADLIST(cv))) {
3631 I32 i = AvFILLp(CvPADLIST(cv));
3633 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
3634 SV* sv = svp ? *svp : Nullsv;
3637 if (sv == (SV*)PL_comppad_name)
3638 PL_comppad_name = Nullav;
3639 else if (sv == (SV*)PL_comppad) {
3640 PL_comppad = Nullav;
3641 PL_curpad = Null(SV**);
3645 SvREFCNT_dec((SV*)CvPADLIST(cv));
3647 CvPADLIST(cv) = Nullav;
3651 #ifdef DEBUG_CLOSURES
3655 CV *outside = CvOUTSIDE(cv);
3656 AV* padlist = CvPADLIST(cv);
3663 PerlIO_printf(Perl_debug_log, "\tCV=0x%lx (%s), OUTSIDE=0x%lx (%s)\n",
3665 (CvANON(cv) ? "ANON"
3666 : (cv == PL_main_cv) ? "MAIN"
3667 : CvUNIQUE(cv) ? "UNIQUE"
3668 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
3671 : CvANON(outside) ? "ANON"
3672 : (outside == PL_main_cv) ? "MAIN"
3673 : CvUNIQUE(outside) ? "UNIQUE"
3674 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
3679 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
3680 pad = (AV*)*av_fetch(padlist, 1, FALSE);
3681 pname = AvARRAY(pad_name);
3682 ppad = AvARRAY(pad);
3684 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
3685 if (SvPOK(pname[ix]))
3686 PerlIO_printf(Perl_debug_log, "\t%4d. 0x%lx (%s\"%s\" %ld-%ld)\n",
3688 SvFAKE(pname[ix]) ? "FAKE " : "",
3690 (long)I_32(SvNVX(pname[ix])),
3691 (long)SvIVX(pname[ix]));
3694 #endif /* DEBUG_CLOSURES */
3697 S_cv_clone2(pTHX_ CV *proto, CV *outside)
3702 AV* protopadlist = CvPADLIST(proto);
3703 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
3704 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
3705 SV** pname = AvARRAY(protopad_name);
3706 SV** ppad = AvARRAY(protopad);
3707 I32 fname = AvFILLp(protopad_name);
3708 I32 fpad = AvFILLp(protopad);
3712 assert(!CvUNIQUE(proto));
3715 SAVESPTR(PL_curpad);
3716 SAVESPTR(PL_comppad);
3717 SAVESPTR(PL_comppad_name);
3718 SAVESPTR(PL_compcv);
3720 cv = PL_compcv = (CV*)NEWSV(1104,0);
3721 sv_upgrade((SV *)cv, SvTYPE(proto));
3727 New(666, CvMUTEXP(cv), 1, perl_mutex);
3728 MUTEX_INIT(CvMUTEXP(cv));
3730 #endif /* USE_THREADS */
3731 CvFILEGV(cv) = CvFILEGV(proto);
3732 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
3733 CvSTASH(cv) = CvSTASH(proto);
3734 CvROOT(cv) = CvROOT(proto);
3735 CvSTART(cv) = CvSTART(proto);
3737 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
3740 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
3742 PL_comppad_name = newAV();
3743 for (ix = fname; ix >= 0; ix--)
3744 av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix]));
3746 PL_comppad = newAV();
3748 comppadlist = newAV();
3749 AvREAL_off(comppadlist);
3750 av_store(comppadlist, 0, (SV*)PL_comppad_name);
3751 av_store(comppadlist, 1, (SV*)PL_comppad);
3752 CvPADLIST(cv) = comppadlist;
3753 av_fill(PL_comppad, AvFILLp(protopad));
3754 PL_curpad = AvARRAY(PL_comppad);
3756 av = newAV(); /* will be @_ */
3758 av_store(PL_comppad, 0, (SV*)av);
3759 AvFLAGS(av) = AVf_REIFY;
3761 for (ix = fpad; ix > 0; ix--) {
3762 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3763 if (namesv && namesv != &PL_sv_undef) {
3764 char *name = SvPVX(namesv); /* XXX */
3765 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
3766 I32 off = pad_findlex(name, ix, SvIVX(namesv),
3767 CvOUTSIDE(cv), cxstack_ix, 0, 0);
3769 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
3771 Perl_croak(aTHX_ "panic: cv_clone: %s", name);
3773 else { /* our own lexical */
3776 /* anon code -- we'll come back for it */
3777 sv = SvREFCNT_inc(ppad[ix]);
3779 else if (*name == '@')
3781 else if (*name == '%')
3791 SV* sv = NEWSV(0,0);
3797 /* Now that vars are all in place, clone nested closures. */
3799 for (ix = fpad; ix > 0; ix--) {
3800 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3802 && namesv != &PL_sv_undef
3803 && !(SvFLAGS(namesv) & SVf_FAKE)
3804 && *SvPVX(namesv) == '&'
3805 && CvCLONE(ppad[ix]))
3807 CV *kid = cv_clone2((CV*)ppad[ix], cv);
3808 SvREFCNT_dec(ppad[ix]);
3811 PL_curpad[ix] = (SV*)kid;
3815 #ifdef DEBUG_CLOSURES
3816 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
3818 PerlIO_printf(Perl_debug_log, " from:\n");
3820 PerlIO_printf(Perl_debug_log, " to:\n");
3829 Perl_cv_clone(pTHX_ CV *proto)
3832 MUTEX_LOCK(&PL_cred_mutex); /* XXX create separate mutex */
3833 cv = cv_clone2(proto, CvOUTSIDE(proto));
3834 MUTEX_UNLOCK(&PL_cred_mutex); /* XXX create separate mutex */
3839 Perl_cv_ckproto(pTHX_ CV *cv, GV *gv, char *p)
3843 if (((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) && ckWARN_d(WARN_UNSAFE)) {
3844 SV* msg = sv_newmortal();
3848 gv_efullname3(name = sv_newmortal(), gv, Nullch);
3849 sv_setpv(msg, "Prototype mismatch:");
3851 Perl_sv_catpvf(aTHX_ msg, " sub %_", name);
3853 Perl_sv_catpvf(aTHX_ msg, " (%s)", SvPVX(cv));
3854 sv_catpv(msg, " vs ");
3856 Perl_sv_catpvf(aTHX_ msg, "(%s)", p);
3858 sv_catpv(msg, "none");
3859 Perl_warner(aTHX_ WARN_UNSAFE, "%_", msg);
3864 Perl_cv_const_sv(pTHX_ CV *cv)
3866 if (!cv || !SvPOK(cv) || SvCUR(cv))
3868 return op_const_sv(CvSTART(cv), cv);
3872 Perl_op_const_sv(pTHX_ OP *o, CV *cv)
3879 if(o->op_type == OP_LINESEQ && cLISTOPo->op_first)
3880 o = cLISTOPo->op_first->op_sibling;
3882 for (; o; o = o->op_next) {
3883 OPCODE type = o->op_type;
3885 if(sv && o->op_next == o)
3887 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
3889 if (type == OP_LEAVESUB || type == OP_RETURN)
3893 if (type == OP_CONST)
3895 else if (type == OP_PADSV && cv) {
3896 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
3897 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
3898 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
3910 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
3914 char *name = o ? SvPVx(cSVOPo->op_sv, n_a) : Nullch;
3915 GV *gv = gv_fetchpv(name ? name : "__ANON__",
3916 GV_ADDMULTI | (block ? 0 : GV_NOINIT), SVt_PVCV);
3917 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, n_a) : Nullch;
3926 if (SvTYPE(gv) != SVt_PVGV) { /* Prototype now, and had
3927 maximum a prototype before. */
3928 if (SvTYPE(gv) > SVt_NULL) {
3929 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1)
3930 && ckWARN_d(WARN_UNSAFE))
3932 Perl_warner(aTHX_ WARN_UNSAFE, "Runaway prototype");
3934 cv_ckproto((CV*)gv, NULL, ps);
3937 sv_setpv((SV*)gv, ps);
3939 sv_setiv((SV*)gv, -1);
3940 SvREFCNT_dec(PL_compcv);
3941 cv = PL_compcv = NULL;
3942 PL_sub_generation++;
3946 if (!name || GvCVGEN(gv))
3948 else if (cv = GvCV(gv)) {
3949 cv_ckproto(cv, gv, ps);
3950 /* already defined (or promised)? */
3951 if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3953 bool const_changed = TRUE;
3955 /* just a "sub foo;" when &foo is already defined */
3956 SAVEFREESV(PL_compcv);
3959 /* ahem, death to those who redefine active sort subs */
3960 if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv))
3961 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", name);
3962 if(const_sv = cv_const_sv(cv))
3963 const_changed = sv_cmp(const_sv, op_const_sv(block, Nullcv));
3964 if ((const_sv && const_changed) || ckWARN(WARN_REDEFINE)
3965 && !(CvGV(cv) && GvSTASH(CvGV(cv))
3966 && HvNAME(GvSTASH(CvGV(cv)))
3967 && strEQ(HvNAME(GvSTASH(CvGV(cv))),
3969 line_t oldline = PL_curcop->cop_line;
3970 PL_curcop->cop_line = PL_copline;
3971 Perl_warner(aTHX_ WARN_REDEFINE,
3972 const_sv ? "Constant subroutine %s redefined"
3973 : "Subroutine %s redefined", name);
3974 PL_curcop->cop_line = oldline;
3980 if (cv) { /* must reuse cv if autoloaded */
3982 CvFLAGS(cv) = CvFLAGS(PL_compcv);
3983 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
3984 CvOUTSIDE(PL_compcv) = 0;
3985 CvPADLIST(cv) = CvPADLIST(PL_compcv);
3986 CvPADLIST(PL_compcv) = 0;
3987 if (SvREFCNT(PL_compcv) > 1) /* XXX Make closures transit through stub. */
3988 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc((SV*)cv);
3989 SvREFCNT_dec(PL_compcv);
3996 PL_sub_generation++;
3999 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4000 CvFILEGV(cv) = PL_curcop->cop_filegv;
4001 CvSTASH(cv) = PL_curstash;
4004 if (!CvMUTEXP(cv)) {
4005 New(666, CvMUTEXP(cv), 1, perl_mutex);
4006 MUTEX_INIT(CvMUTEXP(cv));
4008 #endif /* USE_THREADS */
4011 sv_setpv((SV*)cv, ps);
4013 if (PL_error_count) {
4017 char *s = strrchr(name, ':');
4019 if (strEQ(s, "BEGIN")) {
4021 "BEGIN not safe after errors--compilation aborted";
4022 if (PL_in_eval & EVAL_KEEPERR)
4023 Perl_croak(aTHX_ not_safe);
4025 /* force display of errors found but not reported */
4026 sv_catpv(ERRSV, not_safe);
4027 Perl_croak(aTHX_ "%s", SvPVx(ERRSV, n_a));
4034 PL_copline = NOLINE;
4039 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad))
4040 av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv);
4043 SV **namep = AvARRAY(PL_comppad_name);
4044 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4047 if (SvIMMORTAL(PL_curpad[ix]))
4050 * The only things that a clonable function needs in its
4051 * pad are references to outer lexicals and anonymous subs.
4052 * The rest are created anew during cloning.
4054 if (!((namesv = namep[ix]) != Nullsv &&
4055 namesv != &PL_sv_undef &&
4057 *SvPVX(namesv) == '&')))
4059 SvREFCNT_dec(PL_curpad[ix]);
4060 PL_curpad[ix] = Nullsv;
4065 AV *av = newAV(); /* Will be @_ */
4067 av_store(PL_comppad, 0, (SV*)av);
4068 AvFLAGS(av) = AVf_REIFY;
4070 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4071 if (SvIMMORTAL(PL_curpad[ix]))
4073 if (!SvPADMY(PL_curpad[ix]))
4074 SvPADTMP_on(PL_curpad[ix]);
4078 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4079 CvSTART(cv) = LINKLIST(CvROOT(cv));
4080 CvROOT(cv)->op_next = 0;
4086 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4087 SV *sv = NEWSV(0,0);
4088 SV *tmpstr = sv_newmortal();
4089 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
4093 Perl_sv_setpvf(aTHX_ sv, "%_:%ld-%ld",
4094 GvSV(PL_curcop->cop_filegv),
4095 (long)PL_subline, (long)PL_curcop->cop_line);
4096 gv_efullname3(tmpstr, gv, Nullch);
4097 hv_store(GvHV(PL_DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
4098 hv = GvHVn(db_postponed);
4099 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
4100 && (cv = GvCV(db_postponed))) {
4105 call_sv((SV*)cv, G_DISCARD);
4109 if ((s = strrchr(name,':')))
4113 if (strEQ(s, "BEGIN")) {
4114 I32 oldscope = PL_scopestack_ix;
4116 SAVESPTR(PL_compiling.cop_filegv);
4117 SAVEI16(PL_compiling.cop_line);
4119 sv_setsv(PL_rs, PL_nrs);
4122 PL_beginav = newAV();
4123 DEBUG_x( dump_sub(gv) );
4124 av_push(PL_beginav, (SV *)cv);
4126 call_list(oldscope, PL_beginav);
4128 PL_curcop = &PL_compiling;
4129 PL_compiling.op_private = PL_hints;
4132 else if (strEQ(s, "END") && !PL_error_count) {
4135 av_unshift(PL_endav, 1);
4136 av_store(PL_endav, 0, (SV *)cv);
4139 else if (strEQ(s, "INIT") && !PL_error_count) {
4141 PL_initav = newAV();
4142 av_push(PL_initav, SvREFCNT_inc(cv));
4148 PL_copline = NOLINE;
4153 /* XXX unsafe for threads if eval_owner isn't held */
4155 Perl_newCONSTSUB(pTHX_ HV *stash, char *name, SV *sv)
4158 U32 oldhints = PL_hints;
4159 HV *old_cop_stash = PL_curcop->cop_stash;
4160 HV *old_curstash = PL_curstash;
4161 line_t oldline = PL_curcop->cop_line;
4162 PL_curcop->cop_line = PL_copline;
4164 PL_hints &= ~HINT_BLOCK_SCOPE;
4166 PL_curstash = PL_curcop->cop_stash = stash;
4169 start_subparse(FALSE, 0),
4170 newSVOP(OP_CONST, 0, newSVpv(name,0)),
4171 newSVOP(OP_CONST, 0, &PL_sv_no), /* SvPV(&PL_sv_no) == "" -- GMB */
4172 newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv))
4175 PL_hints = oldhints;
4176 PL_curcop->cop_stash = old_cop_stash;
4177 PL_curstash = old_curstash;
4178 PL_curcop->cop_line = oldline;
4182 Perl_newXS(pTHX_ char *name, XSUBADDR_t subaddr, char *filename)
4185 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
4188 if (cv = (name ? GvCV(gv) : Nullcv)) {
4190 /* just a cached method */
4194 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4195 /* already defined (or promised) */
4196 if (ckWARN(WARN_REDEFINE) && !(CvGV(cv) && GvSTASH(CvGV(cv))
4197 && HvNAME(GvSTASH(CvGV(cv)))
4198 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
4199 line_t oldline = PL_curcop->cop_line;
4200 if (PL_copline != NOLINE)
4201 PL_curcop->cop_line = PL_copline;
4202 Perl_warner(aTHX_ WARN_REDEFINE, "Subroutine %s redefined",name);
4203 PL_curcop->cop_line = oldline;
4210 if (cv) /* must reuse cv if autoloaded */
4213 cv = (CV*)NEWSV(1105,0);
4214 sv_upgrade((SV *)cv, SVt_PVCV);
4218 PL_sub_generation++;
4221 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4223 New(666, CvMUTEXP(cv), 1, perl_mutex);
4224 MUTEX_INIT(CvMUTEXP(cv));
4226 #endif /* USE_THREADS */
4227 CvFILEGV(cv) = gv_fetchfile(filename);
4228 CvXSUB(cv) = subaddr;
4231 char *s = strrchr(name,':');
4236 if (strEQ(s, "BEGIN")) {
4238 PL_beginav = newAV();
4239 av_push(PL_beginav, (SV *)cv);
4242 else if (strEQ(s, "END")) {
4245 av_unshift(PL_endav, 1);
4246 av_store(PL_endav, 0, (SV *)cv);
4249 else if (strEQ(s, "INIT")) {
4251 PL_initav = newAV();
4252 av_push(PL_initav, (SV *)cv);
4263 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
4273 name = SvPVx(cSVOPo->op_sv, n_a);
4276 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
4278 if (cv = GvFORM(gv)) {
4279 if (ckWARN(WARN_REDEFINE)) {
4280 line_t oldline = PL_curcop->cop_line;
4282 PL_curcop->cop_line = PL_copline;
4283 Perl_warner(aTHX_ WARN_REDEFINE, "Format %s redefined",name);
4284 PL_curcop->cop_line = oldline;
4290 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4291 CvFILEGV(cv) = PL_curcop->cop_filegv;
4293 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4294 if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix]))
4295 SvPADTMP_on(PL_curpad[ix]);
4298 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4299 CvSTART(cv) = LINKLIST(CvROOT(cv));
4300 CvROOT(cv)->op_next = 0;
4303 PL_copline = NOLINE;
4308 Perl_newANONLIST(pTHX_ OP *o)
4310 return newUNOP(OP_REFGEN, 0,
4311 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
4315 Perl_newANONHASH(pTHX_ OP *o)
4317 return newUNOP(OP_REFGEN, 0,
4318 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
4322 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
4324 return newUNOP(OP_REFGEN, 0,
4325 newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block)));
4329 Perl_oopsAV(pTHX_ OP *o)
4331 switch (o->op_type) {
4333 o->op_type = OP_PADAV;
4334 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4335 return ref(o, OP_RV2AV);
4338 o->op_type = OP_RV2AV;
4339 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4344 if (ckWARN_d(WARN_INTERNAL))
4345 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsAV");
4352 Perl_oopsHV(pTHX_ OP *o)
4356 switch (o->op_type) {
4359 o->op_type = OP_PADHV;
4360 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4361 return ref(o, OP_RV2HV);
4365 o->op_type = OP_RV2HV;
4366 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
4371 if (ckWARN_d(WARN_INTERNAL))
4372 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsHV");
4379 Perl_newAVREF(pTHX_ OP *o)
4381 if (o->op_type == OP_PADANY) {
4382 o->op_type = OP_PADAV;
4383 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4386 return newUNOP(OP_RV2AV, 0, scalar(o));
4390 Perl_newGVREF(pTHX_ I32 type, OP *o)
4392 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
4393 return newUNOP(OP_NULL, 0, o);
4394 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
4398 Perl_newHVREF(pTHX_ OP *o)
4400 if (o->op_type == OP_PADANY) {
4401 o->op_type = OP_PADHV;
4402 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4405 return newUNOP(OP_RV2HV, 0, scalar(o));
4409 Perl_oopsCV(pTHX_ OP *o)
4411 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
4417 Perl_newCVREF(pTHX_ I32 flags, OP *o)
4419 return newUNOP(OP_RV2CV, flags, scalar(o));
4423 Perl_newSVREF(pTHX_ OP *o)
4425 if (o->op_type == OP_PADANY) {
4426 o->op_type = OP_PADSV;
4427 o->op_ppaddr = PL_ppaddr[OP_PADSV];
4430 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
4431 o->op_flags |= OPpDONE_SVREF;
4434 return newUNOP(OP_RV2SV, 0, scalar(o));
4437 /* Check routines. */
4440 Perl_ck_anoncode(pTHX_ OP *o)
4445 name = NEWSV(1106,0);
4446 sv_upgrade(name, SVt_PVNV);
4447 sv_setpvn(name, "&", 1);
4450 ix = pad_alloc(o->op_type, SVs_PADMY);
4451 av_store(PL_comppad_name, ix, name);
4452 av_store(PL_comppad, ix, cSVOPo->op_sv);
4453 SvPADMY_on(cSVOPo->op_sv);
4454 cSVOPo->op_sv = Nullsv;
4455 cSVOPo->op_targ = ix;
4460 Perl_ck_bitop(pTHX_ OP *o)
4462 o->op_private = PL_hints;
4467 Perl_ck_concat(pTHX_ OP *o)
4469 if (cUNOPo->op_first->op_type == OP_CONCAT)
4470 o->op_flags |= OPf_STACKED;
4475 Perl_ck_spair(pTHX_ OP *o)
4477 if (o->op_flags & OPf_KIDS) {
4480 OPCODE type = o->op_type;
4481 o = modkids(ck_fun(o), type);
4482 kid = cUNOPo->op_first;
4483 newop = kUNOP->op_first->op_sibling;
4485 (newop->op_sibling ||
4486 !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
4487 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
4488 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
4492 op_free(kUNOP->op_first);
4493 kUNOP->op_first = newop;
4495 o->op_ppaddr = PL_ppaddr[++o->op_type];
4500 Perl_ck_delete(pTHX_ OP *o)
4504 if (o->op_flags & OPf_KIDS) {
4505 OP *kid = cUNOPo->op_first;
4506 if (kid->op_type == OP_HSLICE)
4507 o->op_private |= OPpSLICE;
4508 else if (kid->op_type != OP_HELEM)
4509 Perl_croak(aTHX_ "%s argument is not a HASH element or slice",
4510 PL_op_desc[o->op_type]);
4517 Perl_ck_eof(pTHX_ OP *o)
4519 I32 type = o->op_type;
4521 if (o->op_flags & OPf_KIDS) {
4522 if (cLISTOPo->op_first->op_type == OP_STUB) {
4524 o = newUNOP(type, OPf_SPECIAL,
4525 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
4533 Perl_ck_eval(pTHX_ OP *o)
4535 PL_hints |= HINT_BLOCK_SCOPE;
4536 if (o->op_flags & OPf_KIDS) {
4537 SVOP *kid = (SVOP*)cUNOPo->op_first;
4540 o->op_flags &= ~OPf_KIDS;
4543 else if (kid->op_type == OP_LINESEQ) {
4546 kid->op_next = o->op_next;
4547 cUNOPo->op_first = 0;
4550 NewOp(1101, enter, 1, LOGOP);
4551 enter->op_type = OP_ENTERTRY;
4552 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
4553 enter->op_private = 0;
4555 /* establish postfix order */
4556 enter->op_next = (OP*)enter;
4558 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
4559 o->op_type = OP_LEAVETRY;
4560 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
4561 enter->op_other = o;
4569 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
4571 o->op_targ = (PADOFFSET)PL_hints;
4576 Perl_ck_exec(pTHX_ OP *o)
4579 if (o->op_flags & OPf_STACKED) {
4581 kid = cUNOPo->op_first->op_sibling;
4582 if (kid->op_type == OP_RV2GV)
4591 Perl_ck_exists(pTHX_ OP *o)
4594 if (o->op_flags & OPf_KIDS) {
4595 OP *kid = cUNOPo->op_first;
4596 if (kid->op_type != OP_HELEM)
4597 Perl_croak(aTHX_ "%s argument is not a HASH element", PL_op_desc[o->op_type]);
4605 Perl_ck_gvconst(pTHX_ register OP *o)
4607 o = fold_constants(o);
4608 if (o->op_type == OP_CONST)
4615 Perl_ck_rvconst(pTHX_ register OP *o)
4618 SVOP *kid = (SVOP*)cUNOPo->op_first;
4620 o->op_private |= (PL_hints & HINT_STRICT_REFS);
4621 if (kid->op_type == OP_CONST) {
4625 SV *kidsv = kid->op_sv;
4628 /* Is it a constant from cv_const_sv()? */
4629 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
4630 SV *rsv = SvRV(kidsv);
4631 int svtype = SvTYPE(rsv);
4632 char *badtype = Nullch;
4634 switch (o->op_type) {
4636 if (svtype > SVt_PVMG)
4637 badtype = "a SCALAR";
4640 if (svtype != SVt_PVAV)
4641 badtype = "an ARRAY";
4644 if (svtype != SVt_PVHV) {
4645 if (svtype == SVt_PVAV) { /* pseudohash? */
4646 SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
4647 if (ksv && SvROK(*ksv)
4648 && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
4657 if (svtype != SVt_PVCV)
4662 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
4665 name = SvPV(kidsv, n_a);
4666 if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
4667 char *badthing = Nullch;
4668 switch (o->op_type) {
4670 badthing = "a SCALAR";
4673 badthing = "an ARRAY";
4676 badthing = "a HASH";
4681 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
4685 * This is a little tricky. We only want to add the symbol if we
4686 * didn't add it in the lexer. Otherwise we get duplicate strict
4687 * warnings. But if we didn't add it in the lexer, we must at
4688 * least pretend like we wanted to add it even if it existed before,
4689 * or we get possible typo warnings. OPpCONST_ENTERED says
4690 * whether the lexer already added THIS instance of this symbol.
4692 iscv = (o->op_type == OP_RV2CV) * 2;
4694 gv = gv_fetchpv(name,
4695 iscv | !(kid->op_private & OPpCONST_ENTERED),
4698 : o->op_type == OP_RV2SV
4700 : o->op_type == OP_RV2AV
4702 : o->op_type == OP_RV2HV
4705 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
4707 kid->op_type = OP_GV;
4708 SvREFCNT_dec(kid->op_sv);
4709 kid->op_sv = SvREFCNT_inc(gv);
4710 kid->op_ppaddr = PL_ppaddr[OP_GV];
4717 Perl_ck_ftst(pTHX_ OP *o)
4720 I32 type = o->op_type;
4722 if (o->op_flags & OPf_REF)
4725 if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
4726 SVOP *kid = (SVOP*)cUNOPo->op_first;
4728 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4730 OP *newop = newGVOP(type, OPf_REF,
4731 gv_fetchpv(SvPVx(kid->op_sv, n_a), TRUE, SVt_PVIO));
4738 if (type == OP_FTTTY)
4739 return newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
4742 return newUNOP(type, 0, newDEFSVOP());
4748 Perl_ck_fun(pTHX_ OP *o)
4755 int type = o->op_type;
4756 register I32 oa = PL_opargs[type] >> OASHIFT;
4758 if (o->op_flags & OPf_STACKED) {
4759 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
4762 return no_fh_allowed(o);
4765 if (o->op_flags & OPf_KIDS) {
4767 tokid = &cLISTOPo->op_first;
4768 kid = cLISTOPo->op_first;
4769 if (kid->op_type == OP_PUSHMARK ||
4770 kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
4772 tokid = &kid->op_sibling;
4773 kid = kid->op_sibling;
4775 if (!kid && PL_opargs[type] & OA_DEFGV)
4776 *tokid = kid = newDEFSVOP();
4780 sibl = kid->op_sibling;
4783 /* list seen where single (scalar) arg expected? */
4784 if (numargs == 1 && !(oa >> 4)
4785 && kid->op_type == OP_LIST && type != OP_SCALAR)
4787 return too_many_arguments(o,PL_op_desc[type]);
4800 if (kid->op_type == OP_CONST &&
4801 (kid->op_private & OPpCONST_BARE))
4803 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
4804 OP *newop = newAVREF(newGVOP(OP_GV, 0,
4805 gv_fetchpv(name, TRUE, SVt_PVAV) ));
4806 if (ckWARN(WARN_SYNTAX))
4807 Perl_warner(aTHX_ WARN_SYNTAX,
4808 "Array @%s missing the @ in argument %ld of %s()",
4809 name, (long)numargs, PL_op_desc[type]);
4812 kid->op_sibling = sibl;
4815 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
4816 bad_type(numargs, "array", PL_op_desc[type], kid);
4820 if (kid->op_type == OP_CONST &&
4821 (kid->op_private & OPpCONST_BARE))
4823 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
4824 OP *newop = newHVREF(newGVOP(OP_GV, 0,
4825 gv_fetchpv(name, TRUE, SVt_PVHV) ));
4826 if (ckWARN(WARN_SYNTAX))
4827 Perl_warner(aTHX_ WARN_SYNTAX,
4828 "Hash %%%s missing the %% in argument %ld of %s()",
4829 name, (long)numargs, PL_op_desc[type]);
4832 kid->op_sibling = sibl;
4835 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
4836 bad_type(numargs, "hash", PL_op_desc[type], kid);
4841 OP *newop = newUNOP(OP_NULL, 0, kid);
4842 kid->op_sibling = 0;
4844 newop->op_next = newop;
4846 kid->op_sibling = sibl;
4851 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
4852 if (kid->op_type == OP_CONST &&
4853 (kid->op_private & OPpCONST_BARE))
4855 OP *newop = newGVOP(OP_GV, 0,
4856 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, n_a), TRUE,
4861 else if (kid->op_type == OP_READLINE) {
4862 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
4863 bad_type(numargs, "HANDLE", PL_op_desc[o->op_type], kid);
4866 I32 flags = OPf_SPECIAL;
4868 /* is this op a FH constructor? */
4869 if (is_handle_constructor(o,numargs)) {
4871 /* Set a flag to tell rv2gv to vivify
4872 * need to "prove" flag does not mean something
4873 * else already - NI-S 1999/05/07
4877 /* Helps with open($array[$n],...)
4878 but is too simplistic - need to do selectively
4883 kid->op_sibling = 0;
4884 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
4886 kid->op_private |= priv;
4889 kid->op_sibling = sibl;
4895 mod(scalar(kid), type);
4899 tokid = &kid->op_sibling;
4900 kid = kid->op_sibling;
4902 o->op_private |= numargs;
4904 return too_many_arguments(o,PL_op_desc[o->op_type]);
4907 else if (PL_opargs[type] & OA_DEFGV) {
4909 return newUNOP(type, 0, newDEFSVOP());
4913 while (oa & OA_OPTIONAL)
4915 if (oa && oa != OA_LIST)
4916 return too_few_arguments(o,PL_op_desc[o->op_type]);
4922 Perl_ck_glob(pTHX_ OP *o)
4926 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
4927 append_elem(OP_GLOB, o, newDEFSVOP());
4929 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
4930 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
4932 if (gv && GvIMPORTED_CV(gv)) {
4933 append_elem(OP_GLOB, o,
4934 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
4935 o->op_type = OP_LIST;
4936 o->op_ppaddr = PL_ppaddr[OP_LIST];
4937 cLISTOPo->op_first->op_type = OP_PUSHMARK;
4938 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
4939 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
4940 append_elem(OP_LIST, o,
4941 scalar(newUNOP(OP_RV2CV, 0,
4942 newGVOP(OP_GV, 0, gv)))));
4943 o = newUNOP(OP_NULL, 0, ck_subr(o));
4944 o->op_targ = OP_GLOB; /* hint at what it used to be */
4947 gv = newGVgen("main");
4949 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
4955 Perl_ck_grep(pTHX_ OP *o)
4959 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
4961 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
4962 NewOp(1101, gwop, 1, LOGOP);
4964 if (o->op_flags & OPf_STACKED) {
4967 kid = cLISTOPo->op_first->op_sibling;
4968 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
4971 kid->op_next = (OP*)gwop;
4972 o->op_flags &= ~OPf_STACKED;
4974 kid = cLISTOPo->op_first->op_sibling;
4975 if (type == OP_MAPWHILE)
4982 kid = cLISTOPo->op_first->op_sibling;
4983 if (kid->op_type != OP_NULL)
4984 Perl_croak(aTHX_ "panic: ck_grep");
4985 kid = kUNOP->op_first;
4987 gwop->op_type = type;
4988 gwop->op_ppaddr = PL_ppaddr[type];
4989 gwop->op_first = listkids(o);
4990 gwop->op_flags |= OPf_KIDS;
4991 gwop->op_private = 1;
4992 gwop->op_other = LINKLIST(kid);
4993 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
4994 kid->op_next = (OP*)gwop;
4996 kid = cLISTOPo->op_first->op_sibling;
4997 if (!kid || !kid->op_sibling)
4998 return too_few_arguments(o,PL_op_desc[o->op_type]);
4999 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
5000 mod(kid, OP_GREPSTART);
5006 Perl_ck_index(pTHX_ OP *o)
5008 if (o->op_flags & OPf_KIDS) {
5009 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5011 kid = kid->op_sibling; /* get past "big" */
5012 if (kid && kid->op_type == OP_CONST)
5013 fbm_compile(((SVOP*)kid)->op_sv, 0);
5019 Perl_ck_lengthconst(pTHX_ OP *o)
5021 /* XXX length optimization goes here */
5026 Perl_ck_lfun(pTHX_ OP *o)
5028 OPCODE type = o->op_type;
5029 return modkids(ck_fun(o), type);
5033 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
5036 if ((o->op_flags & OPf_KIDS) && ckWARN(WARN_DEPRECATED)) {
5037 switch (cUNOPo->op_first->op_type) {
5040 case OP_AASSIGN: /* Is this a good idea? */
5041 Perl_warner(aTHX_ WARN_DEPRECATED,
5042 "defined(@array) is deprecated");
5043 Perl_warner(aTHX_ WARN_DEPRECATED,
5044 "(Maybe you should just omit the defined()?)\n");
5048 Perl_warner(aTHX_ WARN_DEPRECATED,
5049 "defined(%hash) is deprecated");
5050 Perl_warner(aTHX_ WARN_DEPRECATED,
5051 "(Maybe you should just omit the defined()?)\n");
5062 Perl_ck_rfun(pTHX_ OP *o)
5064 OPCODE type = o->op_type;
5065 return refkids(ck_fun(o), type);
5069 Perl_ck_listiob(pTHX_ OP *o)
5073 kid = cLISTOPo->op_first;
5076 kid = cLISTOPo->op_first;
5078 if (kid->op_type == OP_PUSHMARK)
5079 kid = kid->op_sibling;
5080 if (kid && o->op_flags & OPf_STACKED)
5081 kid = kid->op_sibling;
5082 else if (kid && !kid->op_sibling) { /* print HANDLE; */
5083 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
5084 o->op_flags |= OPf_STACKED; /* make it a filehandle */
5085 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
5086 cLISTOPo->op_first->op_sibling = kid;
5087 cLISTOPo->op_last = kid;
5088 kid = kid->op_sibling;
5093 append_elem(o->op_type, o, newDEFSVOP());
5099 if (PL_hints & HINT_LOCALE)
5100 o->op_private |= OPpLOCALE;
5107 Perl_ck_fun_locale(pTHX_ OP *o)
5113 if (PL_hints & HINT_LOCALE)
5114 o->op_private |= OPpLOCALE;
5121 Perl_ck_sassign(pTHX_ OP *o)
5123 OP *kid = cLISTOPo->op_first;
5124 /* has a disposable target? */
5125 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
5126 && !(kid->op_flags & OPf_STACKED))
5128 OP *kkid = kid->op_sibling;
5130 /* Can just relocate the target. */
5131 if (kkid && kkid->op_type == OP_PADSV) {
5132 /* Concat has problems if target is equal to right arg. */
5133 if (kid->op_type == OP_CONCAT
5134 && kLISTOP->op_first->op_sibling->op_type == OP_PADSV
5135 && kLISTOP->op_first->op_sibling->op_targ == kkid->op_targ)
5139 kid->op_targ = kkid->op_targ;
5140 /* Now we do not need PADSV and SASSIGN. */
5141 kid->op_sibling = o->op_sibling; /* NULL */
5142 cLISTOPo->op_first = NULL;
5145 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
5153 Perl_ck_scmp(pTHX_ OP *o)
5157 if (PL_hints & HINT_LOCALE)
5158 o->op_private |= OPpLOCALE;
5165 Perl_ck_match(pTHX_ OP *o)
5167 o->op_private |= OPpRUNTIME;
5172 Perl_ck_null(pTHX_ OP *o)
5178 Perl_ck_repeat(pTHX_ OP *o)
5180 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
5181 o->op_private |= OPpREPEAT_DOLIST;
5182 cBINOPo->op_first = force_list(cBINOPo->op_first);
5190 Perl_ck_require(pTHX_ OP *o)
5192 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
5193 SVOP *kid = (SVOP*)cUNOPo->op_first;
5195 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5197 for (s = SvPVX(kid->op_sv); *s; s++) {
5198 if (*s == ':' && s[1] == ':') {
5200 Move(s+2, s+1, strlen(s+2)+1, char);
5201 --SvCUR(kid->op_sv);
5204 sv_catpvn(kid->op_sv, ".pm", 3);
5212 Perl_ck_retarget(pTHX_ OP *o)
5214 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
5221 Perl_ck_select(pTHX_ OP *o)
5224 if (o->op_flags & OPf_KIDS) {
5225 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5226 if (kid && kid->op_sibling) {
5227 o->op_type = OP_SSELECT;
5228 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
5230 return fold_constants(o);
5234 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5235 if (kid && kid->op_type == OP_RV2GV)
5236 kid->op_private &= ~HINT_STRICT_REFS;
5241 Perl_ck_shift(pTHX_ OP *o)
5243 I32 type = o->op_type;
5245 if (!(o->op_flags & OPf_KIDS)) {
5250 if (!CvUNIQUE(PL_compcv)) {
5251 argop = newOP(OP_PADAV, OPf_REF);
5252 argop->op_targ = 0; /* PL_curpad[0] is @_ */
5255 argop = newUNOP(OP_RV2AV, 0,
5256 scalar(newGVOP(OP_GV, 0,
5257 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5260 argop = newUNOP(OP_RV2AV, 0,
5261 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(PL_compcv) ?
5262 PL_defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5263 #endif /* USE_THREADS */
5264 return newUNOP(type, 0, scalar(argop));
5266 return scalar(modkids(ck_fun(o), type));
5270 Perl_ck_sort(pTHX_ OP *o)
5274 if (PL_hints & HINT_LOCALE)
5275 o->op_private |= OPpLOCALE;
5278 if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
5280 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
5281 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5283 kid = kUNOP->op_first; /* get past null */
5285 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
5287 if (kid->op_type == OP_SCOPE) {
5291 else if (kid->op_type == OP_LEAVE) {
5292 if (o->op_type == OP_SORT) {
5293 null(kid); /* wipe out leave */
5296 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
5297 if (k->op_next == kid)
5302 kid->op_next = 0; /* just disconnect the leave */
5303 k = kLISTOP->op_first;
5307 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5308 if (o->op_type == OP_SORT)
5312 o->op_flags |= OPf_SPECIAL;
5314 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
5315 null(cLISTOPo->op_first->op_sibling);
5322 S_simplify_sort(pTHX_ OP *o)
5325 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5328 if (!(o->op_flags & OPf_STACKED))
5330 GvMULTI_on(gv_fetchpv("a", TRUE, SVt_PV));
5331 GvMULTI_on(gv_fetchpv("b", TRUE, SVt_PV));
5332 kid = kUNOP->op_first; /* get past null */
5333 if (kid->op_type != OP_SCOPE)
5335 kid = kLISTOP->op_last; /* get past scope */
5336 switch(kid->op_type) {
5344 k = kid; /* remember this node*/
5345 if (kBINOP->op_first->op_type != OP_RV2SV)
5347 kid = kBINOP->op_first; /* get past cmp */
5348 if (kUNOP->op_first->op_type != OP_GV)
5350 kid = kUNOP->op_first; /* get past rv2sv */
5351 if (GvSTASH(kGVOP->op_gv) != PL_curstash)
5353 if (strEQ(GvNAME(kGVOP->op_gv), "a"))
5355 else if(strEQ(GvNAME(kGVOP->op_gv), "b"))
5359 kid = k; /* back to cmp */
5360 if (kBINOP->op_last->op_type != OP_RV2SV)
5362 kid = kBINOP->op_last; /* down to 2nd arg */
5363 if (kUNOP->op_first->op_type != OP_GV)
5365 kid = kUNOP->op_first; /* get past rv2sv */
5366 if (GvSTASH(kGVOP->op_gv) != PL_curstash
5368 ? strNE(GvNAME(kGVOP->op_gv), "a")
5369 : strNE(GvNAME(kGVOP->op_gv), "b")))
5371 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
5373 o->op_private |= OPpSORT_REVERSE;
5374 if (k->op_type == OP_NCMP)
5375 o->op_private |= OPpSORT_NUMERIC;
5376 if (k->op_type == OP_I_NCMP)
5377 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
5378 op_free(cLISTOPo->op_first->op_sibling); /* delete comparison block */
5379 cLISTOPo->op_first->op_sibling = cLISTOPo->op_last;
5380 cLISTOPo->op_children = 1;
5384 Perl_ck_split(pTHX_ OP *o)
5388 if (o->op_flags & OPf_STACKED)
5389 return no_fh_allowed(o);
5391 kid = cLISTOPo->op_first;
5392 if (kid->op_type != OP_NULL)
5393 Perl_croak(aTHX_ "panic: ck_split");
5394 kid = kid->op_sibling;
5395 op_free(cLISTOPo->op_first);
5396 cLISTOPo->op_first = kid;
5398 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvn(" ", 1));
5399 cLISTOPo->op_last = kid; /* There was only one element previously */
5402 if (kid->op_type != OP_MATCH) {
5403 OP *sibl = kid->op_sibling;
5404 kid->op_sibling = 0;
5405 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
5406 if (cLISTOPo->op_first == cLISTOPo->op_last)
5407 cLISTOPo->op_last = kid;
5408 cLISTOPo->op_first = kid;
5409 kid->op_sibling = sibl;
5412 kid->op_type = OP_PUSHRE;
5413 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
5416 if (!kid->op_sibling)
5417 append_elem(OP_SPLIT, o, newDEFSVOP());
5419 kid = kid->op_sibling;
5422 if (!kid->op_sibling)
5423 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
5425 kid = kid->op_sibling;
5428 if (kid->op_sibling)
5429 return too_many_arguments(o,PL_op_desc[o->op_type]);
5435 Perl_ck_subr(pTHX_ OP *o)
5438 OP *prev = ((cUNOPo->op_first->op_sibling)
5439 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
5440 OP *o2 = prev->op_sibling;
5449 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
5450 if (cvop->op_type == OP_RV2CV) {
5452 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
5453 null(cvop); /* disable rv2cv */
5454 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
5455 if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) {
5456 cv = GvCVu(tmpop->op_sv);
5458 tmpop->op_private |= OPpEARLY_CV;
5459 else if (SvPOK(cv)) {
5460 namegv = CvANON(cv) ? (GV*)tmpop->op_sv : CvGV(cv);
5461 proto = SvPV((SV*)cv, n_a);
5465 else if (cvop->op_type == OP_METHOD) {
5466 if (o2->op_type == OP_CONST)
5467 o2->op_private &= ~OPpCONST_STRICT;
5468 else if (o2->op_type == OP_LIST) {
5469 OP *o = ((UNOP*)o2)->op_first->op_sibling;
5470 if (o && o->op_type == OP_CONST)
5471 o->op_private &= ~OPpCONST_STRICT;
5474 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5475 if (PERLDB_SUB && PL_curstash != PL_debstash)
5476 o->op_private |= OPpENTERSUB_DB;
5477 while (o2 != cvop) {
5481 return too_many_arguments(o, gv_ename(namegv));
5499 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
5500 bad_type(arg, "block", gv_ename(namegv), o2);
5503 /* '*' allows any scalar type, including bareword */
5506 if (o2->op_type == OP_RV2GV)
5507 goto wrapref; /* autoconvert GLOB -> GLOBref */
5508 else if (o2->op_type == OP_CONST)
5509 o2->op_private &= ~OPpCONST_STRICT;
5510 else if (o2->op_type == OP_ENTERSUB) {
5511 /* accidental subroutine, revert to bareword */
5512 OP *gvop = ((UNOP*)o2)->op_first;
5513 if (gvop && gvop->op_type == OP_NULL) {
5514 gvop = ((UNOP*)gvop)->op_first;
5516 for (; gvop->op_sibling; gvop = gvop->op_sibling)
5519 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
5520 (gvop = ((UNOP*)gvop)->op_first) &&
5521 gvop->op_type == OP_GV)
5523 GV *gv = (GV*)((SVOP*)gvop)->op_sv;
5524 OP *sibling = o2->op_sibling;
5525 SV *n = newSVpvn("",0);
5527 gv_fullname3(n, gv, "");
5528 if (SvCUR(n)>6 && strnEQ(SvPVX(n),"main::",6))
5529 sv_chop(n, SvPVX(n)+6);
5530 o2 = newSVOP(OP_CONST, 0, n);
5531 prev->op_sibling = o2;
5532 o2->op_sibling = sibling;
5544 if (o2->op_type != OP_RV2GV)
5545 bad_type(arg, "symbol", gv_ename(namegv), o2);
5548 if (o2->op_type != OP_RV2CV)
5549 bad_type(arg, "sub", gv_ename(namegv), o2);
5552 if (o2->op_type != OP_RV2SV
5553 && o2->op_type != OP_PADSV
5554 && o2->op_type != OP_HELEM
5555 && o2->op_type != OP_AELEM
5556 && o2->op_type != OP_THREADSV)
5558 bad_type(arg, "scalar", gv_ename(namegv), o2);
5562 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
5563 bad_type(arg, "array", gv_ename(namegv), o2);
5566 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
5567 bad_type(arg, "hash", gv_ename(namegv), o2);
5571 OP* sib = kid->op_sibling;
5572 kid->op_sibling = 0;
5573 o2 = newUNOP(OP_REFGEN, 0, kid);
5574 o2->op_sibling = sib;
5575 prev->op_sibling = o2;
5586 Perl_croak(aTHX_ "Malformed prototype for %s: %s",
5587 gv_ename(namegv), SvPV((SV*)cv, n_a));
5592 mod(o2, OP_ENTERSUB);
5594 o2 = o2->op_sibling;
5596 if (proto && !optional &&
5597 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
5598 return too_few_arguments(o, gv_ename(namegv));
5603 Perl_ck_svconst(pTHX_ OP *o)
5605 SvREADONLY_on(cSVOPo->op_sv);
5610 Perl_ck_trunc(pTHX_ OP *o)
5612 if (o->op_flags & OPf_KIDS) {
5613 SVOP *kid = (SVOP*)cUNOPo->op_first;
5615 if (kid->op_type == OP_NULL)
5616 kid = (SVOP*)kid->op_sibling;
5617 if (kid && kid->op_type == OP_CONST &&
5618 (kid->op_private & OPpCONST_BARE))
5620 o->op_flags |= OPf_SPECIAL;
5621 kid->op_private &= ~OPpCONST_STRICT;
5627 /* A peephole optimizer. We visit the ops in the order they're to execute. */
5630 Perl_peep(pTHX_ register OP *o)
5633 register OP* oldop = 0;
5636 if (!o || o->op_seq)
5640 SAVESPTR(PL_curcop);
5641 for (; o; o = o->op_next) {
5647 switch (o->op_type) {
5650 PL_curcop = ((COP*)o); /* for warnings */
5651 o->op_seq = PL_op_seqmax++;
5655 if (cSVOPo->op_private & OPpCONST_STRICT)
5656 no_bareword_allowed(o);
5662 if ( o->op_next && o->op_next->op_type == OP_STRINGIFY
5663 && !(o->op_next->op_private & OPpTARGET_MY) )
5665 o->op_seq = PL_op_seqmax++;
5670 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
5671 if (o->op_next->op_private & OPpTARGET_MY) {
5672 if ((o->op_flags & OPf_STACKED) /* chained concats */
5673 || (o->op_type == OP_CONCAT
5674 /* Concat has problems if target is equal to right arg. */
5675 && (((LISTOP*)o)->op_first->op_sibling->op_type
5677 && (((LISTOP*)o)->op_first->op_sibling->op_targ
5678 == o->op_next->op_targ))) {
5679 goto ignore_optimization;
5681 o->op_targ = o->op_next->op_targ;
5686 ignore_optimization:
5687 o->op_seq = PL_op_seqmax++;
5690 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
5691 o->op_seq = PL_op_seqmax++;
5692 break; /* Scalar stub must produce undef. List stub is noop */
5696 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
5697 PL_curcop = ((COP*)o);
5703 if (oldop && o->op_next) {
5704 oldop->op_next = o->op_next;
5707 o->op_seq = PL_op_seqmax++;
5711 if (o->op_next->op_type == OP_RV2SV) {
5712 if (!(o->op_next->op_private & OPpDEREF)) {
5714 o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
5715 o->op_next = o->op_next->op_next;
5716 o->op_type = OP_GVSV;
5717 o->op_ppaddr = PL_ppaddr[OP_GVSV];
5720 else if (o->op_next->op_type == OP_RV2AV) {
5721 OP* pop = o->op_next->op_next;
5723 if (pop->op_type == OP_CONST &&
5724 (PL_op = pop->op_next) &&
5725 pop->op_next->op_type == OP_AELEM &&
5726 !(pop->op_next->op_private &
5727 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
5728 (i = SvIV(((SVOP*)pop)->op_sv) - PL_compiling.cop_arybase)
5732 SvREFCNT_dec(((SVOP*)pop)->op_sv);
5736 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
5737 o->op_next = pop->op_next->op_next;
5738 o->op_type = OP_AELEMFAST;
5739 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
5740 o->op_private = (U8)i;
5741 GvAVn(((GVOP*)o)->op_gv);
5744 else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_UNSAFE)) {
5745 GV *gv = cGVOPo->op_gv;
5746 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX(GvCV(gv))) {
5747 /* XXX could check prototype here instead of just carping */
5748 SV *sv = sv_newmortal();
5749 gv_efullname3(sv, gv, Nullch);
5750 Perl_warner(aTHX_ WARN_UNSAFE,
5751 "%s() called too early to check prototype",
5756 o->op_seq = PL_op_seqmax++;
5765 o->op_seq = PL_op_seqmax++;
5766 while (cLOGOP->op_other->op_type == OP_NULL)
5767 cLOGOP->op_other = cLOGOP->op_other->op_next;
5768 peep(cLOGOP->op_other);
5772 o->op_seq = PL_op_seqmax++;
5773 peep(cLOOP->op_redoop);
5774 peep(cLOOP->op_nextop);
5775 peep(cLOOP->op_lastop);
5781 o->op_seq = PL_op_seqmax++;
5782 peep(cPMOP->op_pmreplstart);
5786 o->op_seq = PL_op_seqmax++;
5787 if (ckWARN(WARN_SYNTAX) && o->op_next
5788 && o->op_next->op_type == OP_NEXTSTATE) {
5789 if (o->op_next->op_sibling &&
5790 o->op_next->op_sibling->op_type != OP_EXIT &&
5791 o->op_next->op_sibling->op_type != OP_WARN &&
5792 o->op_next->op_sibling->op_type != OP_DIE) {
5793 line_t oldline = PL_curcop->cop_line;
5795 PL_curcop->cop_line = ((COP*)o->op_next)->cop_line;
5796 Perl_warner(aTHX_ WARN_SYNTAX, "Statement unlikely to be reached");
5797 Perl_warner(aTHX_ WARN_SYNTAX, "(Maybe you meant system() when you said exec()?)\n");
5798 PL_curcop->cop_line = oldline;
5812 if ((o->op_private & (OPpLVAL_INTRO))
5813 || ((BINOP*)o)->op_last->op_type != OP_CONST)
5815 rop = (UNOP*)((BINOP*)o)->op_first;
5816 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
5818 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
5819 if (!SvOBJECT(lexname))
5821 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
5822 if (!fields || !GvHV(*fields))
5824 svp = &((SVOP*)((BINOP*)o)->op_last)->op_sv;
5825 key = SvPV(*svp, keylen);
5826 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
5828 Perl_croak(aTHX_ "No such field \"%s\" in variable %s of type %s",
5829 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
5831 ind = SvIV(*indsvp);
5833 Perl_croak(aTHX_ "Bad index while coercing array into hash");
5834 rop->op_type = OP_RV2AV;
5835 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
5836 o->op_type = OP_AELEM;
5837 o->op_ppaddr = PL_ppaddr[OP_AELEM];
5839 *svp = newSViv(ind);
5844 o->op_seq = PL_op_seqmax++;