3 * Copyright (c) 1991-2002, 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
24 #define CALL_PEEP(o) CALL_FPTR(PL_peepp)(aTHX_ o)
26 #if defined(PL_OP_SLAB_ALLOC)
28 #ifndef PERL_SLAB_SIZE
29 #define PERL_SLAB_SIZE 2048
32 #define NewOp(m,var,c,type) \
33 STMT_START { var = (type *) Slab_Alloc(m,c*sizeof(type)); } STMT_END
35 #define FreeOp(p) Slab_Free(p)
38 S_Slab_Alloc(pTHX_ int m, size_t sz)
41 * To make incrementing use count easy PL_OpSlab is an I32 *
42 * To make inserting the link to slab PL_OpPtr is I32 **
43 * So compute size in units of sizeof(I32 *) as that is how Pl_OpPtr increments
44 * Add an overhead for pointer to slab and round up as a number of pointers
46 sz = (sz + 2*sizeof(I32 *) -1)/sizeof(I32 *);
47 if ((PL_OpSpace -= sz) < 0) {
48 PL_OpPtr = (I32 **) PerlMemShared_malloc(PERL_SLAB_SIZE*sizeof(I32*));
52 Zero(PL_OpPtr,PERL_SLAB_SIZE,I32 **);
53 /* We reserve the 0'th I32 sized chunk as a use count */
54 PL_OpSlab = (I32 *) PL_OpPtr;
55 /* Reduce size by the use count word, and by the size we need.
56 * Latter is to mimic the '-=' in the if() above
58 PL_OpSpace = PERL_SLAB_SIZE - (sizeof(I32)+sizeof(I32 **)-1)/sizeof(I32 **) - sz;
59 /* Allocation pointer starts at the top.
60 Theory: because we build leaves before trunk allocating at end
61 means that at run time access is cache friendly upward
63 PL_OpPtr += PERL_SLAB_SIZE;
65 assert( PL_OpSpace >= 0 );
66 /* Move the allocation pointer down */
68 assert( PL_OpPtr > (I32 **) PL_OpSlab );
69 *PL_OpPtr = PL_OpSlab; /* Note which slab it belongs to */
70 (*PL_OpSlab)++; /* Increment use count of slab */
71 assert( PL_OpPtr+sz <= ((I32 **) PL_OpSlab + PERL_SLAB_SIZE) );
72 assert( *PL_OpSlab > 0 );
73 return (void *)(PL_OpPtr + 1);
77 S_Slab_Free(pTHX_ void *op)
79 I32 **ptr = (I32 **) op;
81 assert( ptr-1 > (I32 **) slab );
82 assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) );
86 #define PerlMemShared PerlMem
89 PerlMemShared_free(slab);
90 if (slab == PL_OpSlab) {
97 #define NewOp(m, var, c, type) Newz(m, var, c, type)
98 #define FreeOp(p) Safefree(p)
101 * In the following definition, the ", Nullop" is just to make the compiler
102 * think the expression is of the right type: croak actually does a Siglongjmp.
104 #define CHECKOP(type,o) \
105 ((PL_op_mask && PL_op_mask[type]) \
106 ? ( op_free((OP*)o), \
107 Perl_croak(aTHX_ "%s trapped by operation mask", PL_op_desc[type]), \
109 : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o))
111 #define PAD_MAX 999999999
112 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
115 S_gv_ename(pTHX_ GV *gv)
118 SV* tmpsv = sv_newmortal();
119 gv_efullname3(tmpsv, gv, Nullch);
120 return SvPV(tmpsv,n_a);
124 S_no_fh_allowed(pTHX_ OP *o)
126 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
132 S_too_few_arguments(pTHX_ OP *o, char *name)
134 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
139 S_too_many_arguments(pTHX_ OP *o, char *name)
141 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
146 S_bad_type(pTHX_ I32 n, char *t, char *name, OP *kid)
148 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
149 (int)n, name, t, OP_DESC(kid)));
153 S_no_bareword_allowed(pTHX_ OP *o)
155 qerror(Perl_mess(aTHX_
156 "Bareword \"%s\" not allowed while \"strict subs\" in use",
157 SvPV_nolen(cSVOPo_sv)));
160 /* "register" allocation */
163 Perl_pad_allocmy(pTHX_ char *name)
168 if (!(PL_in_my == KEY_our ||
170 (USE_UTF8_IN_NAMES && UTF8_IS_START(name[1])) ||
171 (name[1] == '_' && (int)strlen(name) > 2)))
173 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
174 /* 1999-02-27 mjd@plover.com */
176 p = strchr(name, '\0');
177 /* The next block assumes the buffer is at least 205 chars
178 long. At present, it's always at least 256 chars. */
180 strcpy(name+200, "...");
186 /* Move everything else down one character */
187 for (; p-name > 2; p--)
189 name[2] = toCTRL(name[1]);
192 yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
194 if (ckWARN(WARN_MISC) && AvFILLp(PL_comppad_name) >= 0) {
195 SV **svp = AvARRAY(PL_comppad_name);
196 HV *ourstash = (PL_curstash ? PL_curstash : PL_defstash);
197 PADOFFSET top = AvFILLp(PL_comppad_name);
198 for (off = top; (I32)off > PL_comppad_name_floor; off--) {
200 && sv != &PL_sv_undef
201 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
202 && (PL_in_my != KEY_our
203 || ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash))
204 && strEQ(name, SvPVX(sv)))
206 Perl_warner(aTHX_ packWARN(WARN_MISC),
207 "\"%s\" variable %s masks earlier declaration in same %s",
208 (PL_in_my == KEY_our ? "our" : "my"),
210 (SvIVX(sv) == PAD_MAX ? "scope" : "statement"));
215 if (PL_in_my == KEY_our) {
218 && sv != &PL_sv_undef
219 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
220 && ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash)
221 && strEQ(name, SvPVX(sv)))
223 Perl_warner(aTHX_ packWARN(WARN_MISC),
224 "\"our\" variable %s redeclared", name);
225 Perl_warner(aTHX_ packWARN(WARN_MISC),
226 "\t(Did you mean \"local\" instead of \"our\"?)\n");
229 } while ( off-- > 0 );
232 off = pad_alloc(OP_PADSV, SVs_PADMY);
234 sv_upgrade(sv, SVt_PVNV);
236 if (PL_in_my_stash) {
238 yyerror(Perl_form(aTHX_ "Can't declare class for non-scalar %s in \"%s\"",
239 name, PL_in_my == KEY_our ? "our" : "my"));
240 SvFLAGS(sv) |= SVpad_TYPED;
241 (void)SvUPGRADE(sv, SVt_PVMG);
242 SvSTASH(sv) = (HV*)SvREFCNT_inc(PL_in_my_stash);
244 if (PL_in_my == KEY_our) {
245 (void)SvUPGRADE(sv, SVt_PVGV);
246 GvSTASH(sv) = (HV*)SvREFCNT_inc(PL_curstash ? (SV*)PL_curstash : (SV*)PL_defstash);
247 SvFLAGS(sv) |= SVpad_OUR;
249 av_store(PL_comppad_name, off, sv);
250 SvNVX(sv) = (NV)PAD_MAX;
251 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
252 if (!PL_min_intro_pending)
253 PL_min_intro_pending = off;
254 PL_max_intro_pending = off;
256 av_store(PL_comppad, off, (SV*)newAV());
257 else if (*name == '%')
258 av_store(PL_comppad, off, (SV*)newHV());
259 SvPADMY_on(PL_curpad[off]);
264 S_pad_addlex(pTHX_ SV *proto_namesv)
266 SV *namesv = NEWSV(1103,0);
267 PADOFFSET newoff = pad_alloc(OP_PADSV, SVs_PADMY);
268 sv_upgrade(namesv, SVt_PVNV);
269 sv_setpv(namesv, SvPVX(proto_namesv));
270 av_store(PL_comppad_name, newoff, namesv);
271 SvNVX(namesv) = (NV)PL_curcop->cop_seq;
272 SvIVX(namesv) = PAD_MAX; /* A ref, intro immediately */
273 SvFAKE_on(namesv); /* A ref, not a real var */
274 if (SvFLAGS(proto_namesv) & SVpad_OUR) { /* An "our" variable */
275 SvFLAGS(namesv) |= SVpad_OUR;
276 (void)SvUPGRADE(namesv, SVt_PVGV);
277 GvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)GvSTASH(proto_namesv));
279 if (SvFLAGS(proto_namesv) & SVpad_TYPED) { /* A typed lexical */
280 SvFLAGS(namesv) |= SVpad_TYPED;
281 (void)SvUPGRADE(namesv, SVt_PVMG);
282 SvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)SvSTASH(proto_namesv));
287 #define FINDLEX_NOSEARCH 1 /* don't search outer contexts */
290 S_pad_findlex(pTHX_ char *name, PADOFFSET newoff, U32 seq, CV* startcv,
291 I32 cx_ix, I32 saweval, U32 flags)
297 register PERL_CONTEXT *cx;
299 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
300 AV *curlist = CvPADLIST(cv);
301 SV **svp = av_fetch(curlist, 0, FALSE);
304 if (!svp || *svp == &PL_sv_undef)
307 svp = AvARRAY(curname);
308 for (off = AvFILLp(curname); off > 0; off--) {
309 if ((sv = svp[off]) &&
310 sv != &PL_sv_undef &&
311 seq <= (U32)SvIVX(sv) &&
312 seq > (U32)I_32(SvNVX(sv)) &&
313 strEQ(SvPVX(sv), name))
324 return 0; /* don't clone from inactive stack frame */
328 oldpad = (AV*)AvARRAY(curlist)[depth];
329 oldsv = *av_fetch(oldpad, off, TRUE);
330 if (!newoff) { /* Not a mere clone operation. */
331 newoff = pad_addlex(sv);
332 if (CvANON(PL_compcv) || SvTYPE(PL_compcv) == SVt_PVFM) {
333 /* "It's closures all the way down." */
334 CvCLONE_on(PL_compcv);
336 if (CvANON(PL_compcv))
337 oldsv = Nullsv; /* no need to keep ref */
342 bcv && bcv != cv && !CvCLONE(bcv);
343 bcv = CvOUTSIDE(bcv))
346 /* install the missing pad entry in intervening
347 * nested subs and mark them cloneable.
348 * XXX fix pad_foo() to not use globals */
349 AV *ocomppad_name = PL_comppad_name;
350 AV *ocomppad = PL_comppad;
351 SV **ocurpad = PL_curpad;
352 AV *padlist = CvPADLIST(bcv);
353 PL_comppad_name = (AV*)AvARRAY(padlist)[0];
354 PL_comppad = (AV*)AvARRAY(padlist)[1];
355 PL_curpad = AvARRAY(PL_comppad);
357 PL_comppad_name = ocomppad_name;
358 PL_comppad = ocomppad;
363 if (ckWARN(WARN_CLOSURE)
364 && !CvUNIQUE(bcv) && !CvUNIQUE(cv))
366 Perl_warner(aTHX_ packWARN(WARN_CLOSURE),
367 "Variable \"%s\" may be unavailable",
375 else if (!CvUNIQUE(PL_compcv)) {
376 if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv)
377 && !(SvFLAGS(sv) & SVpad_OUR))
379 Perl_warner(aTHX_ packWARN(WARN_CLOSURE),
380 "Variable \"%s\" will not stay shared", name);
384 av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv));
390 if (flags & FINDLEX_NOSEARCH)
393 /* Nothing in current lexical context--try eval's context, if any.
394 * This is necessary to let the perldb get at lexically scoped variables.
395 * XXX This will also probably interact badly with eval tree caching.
398 for (i = cx_ix; i >= 0; i--) {
400 switch (CxTYPE(cx)) {
402 if (i == 0 && saweval) {
403 return pad_findlex(name, newoff, seq, PL_main_cv, -1, saweval, 0);
407 switch (cx->blk_eval.old_op_type) {
409 if (CxREALEVAL(cx)) {
412 seq = cxstack[i].blk_oldcop->cop_seq;
413 startcv = cxstack[i].blk_eval.cv;
414 if (startcv && CvOUTSIDE(startcv)) {
415 off = pad_findlex(name, newoff, seq, CvOUTSIDE(startcv),
417 if (off) /* continue looking if not found here */
424 /* require/do must have their own scope */
433 if (PL_debstash && CvSTASH(cv) == PL_debstash) { /* ignore DB'* scope */
434 saweval = i; /* so we know where we were called from */
435 seq = cxstack[i].blk_oldcop->cop_seq;
438 return pad_findlex(name, newoff, seq, cv, i-1, saweval,FINDLEX_NOSEARCH);
446 Perl_pad_findmy(pTHX_ char *name)
451 SV **svp = AvARRAY(PL_comppad_name);
452 U32 seq = PL_cop_seqmax;
456 #ifdef USE_5005THREADS
458 * Special case to get lexical (and hence per-thread) @_.
459 * XXX I need to find out how to tell at parse-time whether use
460 * of @_ should refer to a lexical (from a sub) or defgv (global
461 * scope and maybe weird sub-ish things like formats). See
462 * startsub in perly.y. It's possible that @_ could be lexical
463 * (at least from subs) even in non-threaded perl.
465 if (strEQ(name, "@_"))
466 return 0; /* success. (NOT_IN_PAD indicates failure) */
467 #endif /* USE_5005THREADS */
469 /* The one we're looking for is probably just before comppad_name_fill. */
470 for (off = AvFILLp(PL_comppad_name); off > 0; off--) {
471 if ((sv = svp[off]) &&
472 sv != &PL_sv_undef &&
474 (seq <= (U32)SvIVX(sv) &&
475 seq > (U32)I_32(SvNVX(sv)))) &&
476 strEQ(SvPVX(sv), name))
478 if (SvIVX(sv) || SvFLAGS(sv) & SVpad_OUR)
479 return (PADOFFSET)off;
480 pendoff = off; /* this pending def. will override import */
484 outside = CvOUTSIDE(PL_compcv);
486 /* Check if if we're compiling an eval'', and adjust seq to be the
487 * eval's seq number. This depends on eval'' having a non-null
488 * CvOUTSIDE() while it is being compiled. The eval'' itself is
489 * identified by CvEVAL being true and CvGV being null. */
490 if (outside && CvEVAL(PL_compcv) && !CvGV(PL_compcv) && cxstack_ix >= 0) {
491 cx = &cxstack[cxstack_ix];
493 seq = cx->blk_oldcop->cop_seq;
496 /* See if it's in a nested scope */
497 off = pad_findlex(name, 0, seq, outside, cxstack_ix, 0, 0);
499 /* If there is a pending local definition, this new alias must die */
501 SvIVX(AvARRAY(PL_comppad_name)[off]) = seq;
502 return off; /* pad_findlex returns 0 for failure...*/
504 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
508 Perl_pad_leavemy(pTHX_ I32 fill)
511 SV **svp = AvARRAY(PL_comppad_name);
513 if (PL_min_intro_pending && fill < PL_min_intro_pending) {
514 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
515 if ((sv = svp[off]) && sv != &PL_sv_undef && ckWARN_d(WARN_INTERNAL))
516 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "%s never introduced", SvPVX(sv));
519 /* "Deintroduce" my variables that are leaving with this scope. */
520 for (off = AvFILLp(PL_comppad_name); off > fill; off--) {
521 if ((sv = svp[off]) && sv != &PL_sv_undef && SvIVX(sv) == PAD_MAX)
522 SvIVX(sv) = PL_cop_seqmax;
527 Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype)
532 if (AvARRAY(PL_comppad) != PL_curpad)
533 Perl_croak(aTHX_ "panic: pad_alloc");
534 if (PL_pad_reset_pending)
536 if (tmptype & SVs_PADMY) {
538 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
539 } while (SvPADBUSY(sv)); /* need a fresh one */
540 retval = AvFILLp(PL_comppad);
543 SV **names = AvARRAY(PL_comppad_name);
544 SSize_t names_fill = AvFILLp(PL_comppad_name);
547 * "foreach" index vars temporarily become aliases to non-"my"
548 * values. Thus we must skip, not just pad values that are
549 * marked as current pad values, but also those with names.
551 if (++PL_padix <= names_fill &&
552 (sv = names[PL_padix]) && sv != &PL_sv_undef)
554 sv = *av_fetch(PL_comppad, PL_padix, TRUE);
555 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)) &&
556 !IS_PADGV(sv) && !IS_PADCONST(sv))
561 SvFLAGS(sv) |= tmptype;
562 PL_curpad = AvARRAY(PL_comppad);
563 #ifdef USE_5005THREADS
564 DEBUG_X(PerlIO_printf(Perl_debug_log,
565 "0x%"UVxf" Pad 0x%"UVxf" alloc %ld for %s\n",
566 PTR2UV(thr), PTR2UV(PL_curpad),
567 (long) retval, PL_op_name[optype]));
569 DEBUG_X(PerlIO_printf(Perl_debug_log,
570 "Pad 0x%"UVxf" alloc %ld for %s\n",
572 (long) retval, PL_op_name[optype]));
573 #endif /* USE_5005THREADS */
574 return (PADOFFSET)retval;
578 Perl_pad_sv(pTHX_ PADOFFSET po)
580 #ifdef USE_5005THREADS
581 DEBUG_X(PerlIO_printf(Perl_debug_log,
582 "0x%"UVxf" Pad 0x%"UVxf" sv %"IVdf"\n",
583 PTR2UV(thr), PTR2UV(PL_curpad), (IV)po));
586 Perl_croak(aTHX_ "panic: pad_sv po");
587 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" sv %"IVdf"\n",
588 PTR2UV(PL_curpad), (IV)po));
589 #endif /* USE_5005THREADS */
590 return PL_curpad[po]; /* eventually we'll turn this into a macro */
594 Perl_pad_free(pTHX_ PADOFFSET po)
598 if (AvARRAY(PL_comppad) != PL_curpad)
599 Perl_croak(aTHX_ "panic: pad_free curpad");
601 Perl_croak(aTHX_ "panic: pad_free po");
602 #ifdef USE_5005THREADS
603 DEBUG_X(PerlIO_printf(Perl_debug_log,
604 "0x%"UVxf" Pad 0x%"UVxf" free %"IVdf"\n",
605 PTR2UV(thr), PTR2UV(PL_curpad), (IV)po));
607 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" free %"IVdf"\n",
608 PTR2UV(PL_curpad), (IV)po));
609 #endif /* USE_5005THREADS */
610 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef) {
611 SvPADTMP_off(PL_curpad[po]);
613 #ifdef PERL_COPY_ON_WRITE
614 if (SvIsCOW(PL_curpad[po])) {
615 sv_force_normal_flags(PL_curpad[po], SV_COW_DROP_PV);
618 SvREADONLY_off(PL_curpad[po]); /* could be a freed constant */
621 if ((I32)po < PL_padix)
626 Perl_pad_swipe(pTHX_ PADOFFSET po)
628 if (AvARRAY(PL_comppad) != PL_curpad)
629 Perl_croak(aTHX_ "panic: pad_swipe curpad");
631 Perl_croak(aTHX_ "panic: pad_swipe po");
632 #ifdef USE_5005THREADS
633 DEBUG_X(PerlIO_printf(Perl_debug_log,
634 "0x%"UVxf" Pad 0x%"UVxf" swipe %"IVdf"\n",
635 PTR2UV(thr), PTR2UV(PL_curpad), (IV)po));
637 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" swipe %"IVdf"\n",
638 PTR2UV(PL_curpad), (IV)po));
639 #endif /* USE_5005THREADS */
640 SvPADTMP_off(PL_curpad[po]);
641 PL_curpad[po] = NEWSV(1107,0);
642 SvPADTMP_on(PL_curpad[po]);
643 if ((I32)po < PL_padix)
647 /* XXX pad_reset() is currently disabled because it results in serious bugs.
648 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
649 * on the stack by OPs that use them, there are several ways to get an alias
650 * to a shared TARG. Such an alias will change randomly and unpredictably.
651 * We avoid doing this until we can think of a Better Way.
656 #ifdef USE_BROKEN_PAD_RESET
659 if (AvARRAY(PL_comppad) != PL_curpad)
660 Perl_croak(aTHX_ "panic: pad_reset curpad");
661 #ifdef USE_5005THREADS
662 DEBUG_X(PerlIO_printf(Perl_debug_log,
663 "0x%"UVxf" Pad 0x%"UVxf" reset\n",
664 PTR2UV(thr), PTR2UV(PL_curpad)));
666 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" reset\n",
668 #endif /* USE_5005THREADS */
669 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
670 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
671 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
672 SvPADTMP_off(PL_curpad[po]);
674 PL_padix = PL_padix_floor;
677 PL_pad_reset_pending = FALSE;
680 #ifdef USE_5005THREADS
681 /* find_threadsv is not reentrant */
683 Perl_find_threadsv(pTHX_ const char *name)
688 /* We currently only handle names of a single character */
689 p = strchr(PL_threadsv_names, *name);
692 key = p - PL_threadsv_names;
693 MUTEX_LOCK(&thr->mutex);
694 svp = av_fetch(thr->threadsv, key, FALSE);
696 MUTEX_UNLOCK(&thr->mutex);
698 SV *sv = NEWSV(0, 0);
699 av_store(thr->threadsv, key, sv);
700 thr->threadsvp = AvARRAY(thr->threadsv);
701 MUTEX_UNLOCK(&thr->mutex);
703 * Some magic variables used to be automagically initialised
704 * in gv_fetchpv. Those which are now per-thread magicals get
705 * initialised here instead.
711 sv_setpv(sv, "\034");
712 sv_magic(sv, 0, PERL_MAGIC_sv, name, 1);
717 PL_sawampersand = TRUE;
731 /* XXX %! tied to Errno.pm needs to be added here.
732 * See gv_fetchpv(). */
736 sv_magic(sv, 0, PERL_MAGIC_sv, name, 1);
738 DEBUG_S(PerlIO_printf(Perl_error_log,
739 "find_threadsv: new SV %p for $%s%c\n",
740 sv, (*name < 32) ? "^" : "",
741 (*name < 32) ? toCTRL(*name) : *name));
745 #endif /* USE_5005THREADS */
750 Perl_op_free(pTHX_ OP *o)
752 register OP *kid, *nextkid;
755 if (!o || o->op_seq == (U16)-1)
758 if (o->op_private & OPpREFCOUNTED) {
759 switch (o->op_type) {
767 if (OpREFCNT_dec(o)) {
778 if (o->op_flags & OPf_KIDS) {
779 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
780 nextkid = kid->op_sibling; /* Get before next freeing kid */
786 type = (OPCODE)o->op_targ;
788 /* COP* is not cleared by op_clear() so that we may track line
789 * numbers etc even after null() */
790 if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
798 Perl_op_clear(pTHX_ OP *o)
801 switch (o->op_type) {
802 case OP_NULL: /* Was holding old type, if any. */
803 case OP_ENTEREVAL: /* Was holding hints. */
804 #ifdef USE_5005THREADS
805 case OP_THREADSV: /* Was holding index into thr->threadsv AV. */
809 #ifdef USE_5005THREADS
811 if (!(o->op_flags & OPf_SPECIAL))
814 #endif /* USE_5005THREADS */
816 if (!(o->op_flags & OPf_REF)
817 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
824 if (cPADOPo->op_padix > 0) {
827 pad_swipe(cPADOPo->op_padix);
828 /* No GvIN_PAD_off(gv) here, because other references may still
829 * exist on the pad */
832 cPADOPo->op_padix = 0;
835 SvREFCNT_dec(cSVOPo->op_sv);
836 cSVOPo->op_sv = Nullsv;
839 case OP_METHOD_NAMED:
841 SvREFCNT_dec(cSVOPo->op_sv);
842 cSVOPo->op_sv = Nullsv;
848 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
852 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
853 SvREFCNT_dec(cSVOPo->op_sv);
854 cSVOPo->op_sv = Nullsv;
857 Safefree(cPVOPo->op_pv);
858 cPVOPo->op_pv = Nullch;
862 op_free(cPMOPo->op_pmreplroot);
866 if (INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot)) {
868 GV *gv = (GV*)PL_curpad[INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot)];
869 pad_swipe(INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot));
870 /* No GvIN_PAD_off(gv) here, because other references may still
871 * exist on the pad */
876 SvREFCNT_dec((SV*)cPMOPo->op_pmreplroot);
883 HV *pmstash = PmopSTASH(cPMOPo);
884 if (pmstash && SvREFCNT(pmstash)) {
885 PMOP *pmop = HvPMROOT(pmstash);
886 PMOP *lastpmop = NULL;
888 if (cPMOPo == pmop) {
890 lastpmop->op_pmnext = pmop->op_pmnext;
892 HvPMROOT(pmstash) = pmop->op_pmnext;
896 pmop = pmop->op_pmnext;
899 PmopSTASH_free(cPMOPo);
901 cPMOPo->op_pmreplroot = Nullop;
902 /* we use the "SAFE" version of the PM_ macros here
903 * since sv_clean_all might release some PMOPs
904 * after PL_regex_padav has been cleared
905 * and the clearing of PL_regex_padav needs to
906 * happen before sv_clean_all
908 ReREFCNT_dec(PM_GETRE_SAFE(cPMOPo));
909 PM_SETRE_SAFE(cPMOPo, (REGEXP*)NULL);
911 if(PL_regex_pad) { /* We could be in destruction */
912 av_push((AV*) PL_regex_pad[0],(SV*) PL_regex_pad[(cPMOPo)->op_pmoffset]);
913 SvREPADTMP_on(PL_regex_pad[(cPMOPo)->op_pmoffset]);
914 PM_SETRE(cPMOPo, (cPMOPo)->op_pmoffset);
921 if (o->op_targ > 0) {
922 pad_free(o->op_targ);
928 S_cop_free(pTHX_ COP* cop)
930 Safefree(cop->cop_label); /* FIXME: treaddead ??? */
933 if (! specialWARN(cop->cop_warnings))
934 SvREFCNT_dec(cop->cop_warnings);
935 if (! specialCopIO(cop->cop_io)) {
939 char *s = SvPV(cop->cop_io,len);
940 Perl_warn(aTHX_ "io='%.*s'",(int) len,s); /* ??? --jhi */
943 SvREFCNT_dec(cop->cop_io);
949 Perl_op_null(pTHX_ OP *o)
951 if (o->op_type == OP_NULL)
954 o->op_targ = o->op_type;
955 o->op_type = OP_NULL;
956 o->op_ppaddr = PL_ppaddr[OP_NULL];
959 /* Contextualizers */
961 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
964 Perl_linklist(pTHX_ OP *o)
971 /* establish postfix order */
972 if (cUNOPo->op_first) {
973 o->op_next = LINKLIST(cUNOPo->op_first);
974 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
976 kid->op_next = LINKLIST(kid->op_sibling);
988 Perl_scalarkids(pTHX_ OP *o)
991 if (o && o->op_flags & OPf_KIDS) {
992 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
999 S_scalarboolean(pTHX_ OP *o)
1001 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
1002 if (ckWARN(WARN_SYNTAX)) {
1003 line_t oldline = CopLINE(PL_curcop);
1005 if (PL_copline != NOLINE)
1006 CopLINE_set(PL_curcop, PL_copline);
1007 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1008 CopLINE_set(PL_curcop, oldline);
1015 Perl_scalar(pTHX_ OP *o)
1019 /* assumes no premature commitment */
1020 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
1021 || o->op_type == OP_RETURN)
1026 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1028 switch (o->op_type) {
1030 scalar(cBINOPo->op_first);
1035 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1039 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
1040 if (!kPMOP->op_pmreplroot)
1041 deprecate_old("implicit split to @_");
1049 if (o->op_flags & OPf_KIDS) {
1050 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
1056 kid = cLISTOPo->op_first;
1058 while ((kid = kid->op_sibling)) {
1059 if (kid->op_sibling)
1064 WITH_THR(PL_curcop = &PL_compiling);
1069 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1070 if (kid->op_sibling)
1075 WITH_THR(PL_curcop = &PL_compiling);
1078 if (ckWARN(WARN_VOID))
1079 Perl_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
1085 Perl_scalarvoid(pTHX_ OP *o)
1092 if (o->op_type == OP_NEXTSTATE
1093 || o->op_type == OP_SETSTATE
1094 || o->op_type == OP_DBSTATE
1095 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1096 || o->op_targ == OP_SETSTATE
1097 || o->op_targ == OP_DBSTATE)))
1098 PL_curcop = (COP*)o; /* for warning below */
1100 /* assumes no premature commitment */
1101 want = o->op_flags & OPf_WANT;
1102 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
1103 || o->op_type == OP_RETURN)
1108 if ((o->op_private & OPpTARGET_MY)
1109 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1111 return scalar(o); /* As if inside SASSIGN */
1114 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1116 switch (o->op_type) {
1118 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1122 if (o->op_flags & OPf_STACKED)
1126 if (o->op_private == 4)
1168 case OP_GETSOCKNAME:
1169 case OP_GETPEERNAME:
1174 case OP_GETPRIORITY:
1197 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1198 useless = OP_DESC(o);
1205 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1206 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1207 useless = "a variable";
1212 if (cSVOPo->op_private & OPpCONST_STRICT)
1213 no_bareword_allowed(o);
1215 if (ckWARN(WARN_VOID)) {
1216 useless = "a constant";
1217 /* the constants 0 and 1 are permitted as they are
1218 conventionally used as dummies in constructs like
1219 1 while some_condition_with_side_effects; */
1220 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1222 else if (SvPOK(sv)) {
1223 /* perl4's way of mixing documentation and code
1224 (before the invention of POD) was based on a
1225 trick to mix nroff and perl code. The trick was
1226 built upon these three nroff macros being used in
1227 void context. The pink camel has the details in
1228 the script wrapman near page 319. */
1229 if (strnEQ(SvPVX(sv), "di", 2) ||
1230 strnEQ(SvPVX(sv), "ds", 2) ||
1231 strnEQ(SvPVX(sv), "ig", 2))
1236 op_null(o); /* don't execute or even remember it */
1240 o->op_type = OP_PREINC; /* pre-increment is faster */
1241 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1245 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1246 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1253 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1258 if (o->op_flags & OPf_STACKED)
1265 if (!(o->op_flags & OPf_KIDS))
1274 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1281 /* all requires must return a boolean value */
1282 o->op_flags &= ~OPf_WANT;
1287 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
1288 if (!kPMOP->op_pmreplroot)
1289 deprecate_old("implicit split to @_");
1293 if (useless && ckWARN(WARN_VOID))
1294 Perl_warner(aTHX_ packWARN(WARN_VOID), "Useless use of %s in void context", useless);
1299 Perl_listkids(pTHX_ OP *o)
1302 if (o && o->op_flags & OPf_KIDS) {
1303 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1310 Perl_list(pTHX_ OP *o)
1314 /* assumes no premature commitment */
1315 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
1316 || o->op_type == OP_RETURN)
1321 if ((o->op_private & OPpTARGET_MY)
1322 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1324 return o; /* As if inside SASSIGN */
1327 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1329 switch (o->op_type) {
1332 list(cBINOPo->op_first);
1337 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1345 if (!(o->op_flags & OPf_KIDS))
1347 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1348 list(cBINOPo->op_first);
1349 return gen_constant_list(o);
1356 kid = cLISTOPo->op_first;
1358 while ((kid = kid->op_sibling)) {
1359 if (kid->op_sibling)
1364 WITH_THR(PL_curcop = &PL_compiling);
1368 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1369 if (kid->op_sibling)
1374 WITH_THR(PL_curcop = &PL_compiling);
1377 /* all requires must return a boolean value */
1378 o->op_flags &= ~OPf_WANT;
1385 Perl_scalarseq(pTHX_ OP *o)
1390 if (o->op_type == OP_LINESEQ ||
1391 o->op_type == OP_SCOPE ||
1392 o->op_type == OP_LEAVE ||
1393 o->op_type == OP_LEAVETRY)
1395 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1396 if (kid->op_sibling) {
1400 PL_curcop = &PL_compiling;
1402 o->op_flags &= ~OPf_PARENS;
1403 if (PL_hints & HINT_BLOCK_SCOPE)
1404 o->op_flags |= OPf_PARENS;
1407 o = newOP(OP_STUB, 0);
1412 S_modkids(pTHX_ OP *o, I32 type)
1415 if (o && o->op_flags & OPf_KIDS) {
1416 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1423 Perl_mod(pTHX_ OP *o, I32 type)
1428 if (!o || PL_error_count)
1431 if ((o->op_private & OPpTARGET_MY)
1432 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1437 switch (o->op_type) {
1442 if (!(o->op_private & (OPpCONST_ARYBASE)))
1444 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1445 PL_compiling.cop_arybase = (I32)SvIV(cSVOPx(PL_eval_start)->op_sv);
1449 SAVEI32(PL_compiling.cop_arybase);
1450 PL_compiling.cop_arybase = 0;
1452 else if (type == OP_REFGEN)
1455 Perl_croak(aTHX_ "That use of $[ is unsupported");
1458 if (o->op_flags & OPf_PARENS)
1462 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1463 !(o->op_flags & OPf_STACKED)) {
1464 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1465 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1466 assert(cUNOPo->op_first->op_type == OP_NULL);
1467 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1470 else if (o->op_private & OPpENTERSUB_NOMOD)
1472 else { /* lvalue subroutine call */
1473 o->op_private |= OPpLVAL_INTRO;
1474 PL_modcount = RETURN_UNLIMITED_NUMBER;
1475 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
1476 /* Backward compatibility mode: */
1477 o->op_private |= OPpENTERSUB_INARGS;
1480 else { /* Compile-time error message: */
1481 OP *kid = cUNOPo->op_first;
1485 if (kid->op_type == OP_PUSHMARK)
1487 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1489 "panic: unexpected lvalue entersub "
1490 "args: type/targ %ld:%"UVuf,
1491 (long)kid->op_type, (UV)kid->op_targ);
1492 kid = kLISTOP->op_first;
1494 while (kid->op_sibling)
1495 kid = kid->op_sibling;
1496 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1498 if (kid->op_type == OP_METHOD_NAMED
1499 || kid->op_type == OP_METHOD)
1503 NewOp(1101, newop, 1, UNOP);
1504 newop->op_type = OP_RV2CV;
1505 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1506 newop->op_first = Nullop;
1507 newop->op_next = (OP*)newop;
1508 kid->op_sibling = (OP*)newop;
1509 newop->op_private |= OPpLVAL_INTRO;
1513 if (kid->op_type != OP_RV2CV)
1515 "panic: unexpected lvalue entersub "
1516 "entry via type/targ %ld:%"UVuf,
1517 (long)kid->op_type, (UV)kid->op_targ);
1518 kid->op_private |= OPpLVAL_INTRO;
1519 break; /* Postpone until runtime */
1523 kid = kUNOP->op_first;
1524 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1525 kid = kUNOP->op_first;
1526 if (kid->op_type == OP_NULL)
1528 "Unexpected constant lvalue entersub "
1529 "entry via type/targ %ld:%"UVuf,
1530 (long)kid->op_type, (UV)kid->op_targ);
1531 if (kid->op_type != OP_GV) {
1532 /* Restore RV2CV to check lvalueness */
1534 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1535 okid->op_next = kid->op_next;
1536 kid->op_next = okid;
1539 okid->op_next = Nullop;
1540 okid->op_type = OP_RV2CV;
1542 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1543 okid->op_private |= OPpLVAL_INTRO;
1547 cv = GvCV(kGVOP_gv);
1557 /* grep, foreach, subcalls, refgen */
1558 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1560 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1561 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1563 : (o->op_type == OP_ENTERSUB
1564 ? "non-lvalue subroutine call"
1566 type ? PL_op_desc[type] : "local"));
1580 case OP_RIGHT_SHIFT:
1589 if (!(o->op_flags & OPf_STACKED))
1595 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1601 if (!type && cUNOPo->op_first->op_type != OP_GV)
1602 Perl_croak(aTHX_ "Can't localize through a reference");
1603 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1604 PL_modcount = RETURN_UNLIMITED_NUMBER;
1605 return o; /* Treat \(@foo) like ordinary list. */
1609 if (scalar_mod_type(o, type))
1611 ref(cUNOPo->op_first, o->op_type);
1615 if (type == OP_LEAVESUBLV)
1616 o->op_private |= OPpMAYBE_LVSUB;
1621 PL_modcount = RETURN_UNLIMITED_NUMBER;
1624 if (!type && cUNOPo->op_first->op_type != OP_GV)
1625 Perl_croak(aTHX_ "Can't localize through a reference");
1626 ref(cUNOPo->op_first, o->op_type);
1630 PL_hints |= HINT_BLOCK_SCOPE;
1641 PL_modcount = RETURN_UNLIMITED_NUMBER;
1642 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1643 return o; /* Treat \(@foo) like ordinary list. */
1644 if (scalar_mod_type(o, type))
1646 if (type == OP_LEAVESUBLV)
1647 o->op_private |= OPpMAYBE_LVSUB;
1652 Perl_croak(aTHX_ "Can't localize lexical variable %s",
1653 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
1656 #ifdef USE_5005THREADS
1658 PL_modcount++; /* XXX ??? */
1660 #endif /* USE_5005THREADS */
1666 if (type != OP_SASSIGN)
1670 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1675 if (type == OP_LEAVESUBLV)
1676 o->op_private |= OPpMAYBE_LVSUB;
1678 pad_free(o->op_targ);
1679 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1680 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1681 if (o->op_flags & OPf_KIDS)
1682 mod(cBINOPo->op_first->op_sibling, type);
1687 ref(cBINOPo->op_first, o->op_type);
1688 if (type == OP_ENTERSUB &&
1689 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1690 o->op_private |= OPpLVAL_DEFER;
1691 if (type == OP_LEAVESUBLV)
1692 o->op_private |= OPpMAYBE_LVSUB;
1700 if (o->op_flags & OPf_KIDS)
1701 mod(cLISTOPo->op_last, type);
1705 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1707 else if (!(o->op_flags & OPf_KIDS))
1709 if (o->op_targ != OP_LIST) {
1710 mod(cBINOPo->op_first, type);
1715 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1720 if (type != OP_LEAVESUBLV)
1722 break; /* mod()ing was handled by ck_return() */
1725 /* [20011101.069] File test operators interpret OPf_REF to mean that
1726 their argument is a filehandle; thus \stat(".") should not set
1728 if (type == OP_REFGEN &&
1729 PL_check[o->op_type] == MEMBER_TO_FPTR(Perl_ck_ftst))
1732 if (type != OP_LEAVESUBLV)
1733 o->op_flags |= OPf_MOD;
1735 if (type == OP_AASSIGN || type == OP_SASSIGN)
1736 o->op_flags |= OPf_SPECIAL|OPf_REF;
1738 o->op_private |= OPpLVAL_INTRO;
1739 o->op_flags &= ~OPf_SPECIAL;
1740 PL_hints |= HINT_BLOCK_SCOPE;
1742 else if (type != OP_GREPSTART && type != OP_ENTERSUB
1743 && type != OP_LEAVESUBLV)
1744 o->op_flags |= OPf_REF;
1749 S_scalar_mod_type(pTHX_ OP *o, I32 type)
1753 if (o->op_type == OP_RV2GV)
1777 case OP_RIGHT_SHIFT:
1796 S_is_handle_constructor(pTHX_ OP *o, I32 argnum)
1798 switch (o->op_type) {
1806 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1819 Perl_refkids(pTHX_ OP *o, I32 type)
1822 if (o && o->op_flags & OPf_KIDS) {
1823 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1830 Perl_ref(pTHX_ OP *o, I32 type)
1834 if (!o || PL_error_count)
1837 switch (o->op_type) {
1839 if ((type == OP_EXISTS || type == OP_DEFINED || type == OP_LOCK) &&
1840 !(o->op_flags & OPf_STACKED)) {
1841 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1842 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1843 assert(cUNOPo->op_first->op_type == OP_NULL);
1844 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1845 o->op_flags |= OPf_SPECIAL;
1850 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1854 if (type == OP_DEFINED)
1855 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1856 ref(cUNOPo->op_first, o->op_type);
1859 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1860 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1861 : type == OP_RV2HV ? OPpDEREF_HV
1863 o->op_flags |= OPf_MOD;
1868 o->op_flags |= OPf_MOD; /* XXX ??? */
1873 o->op_flags |= OPf_REF;
1876 if (type == OP_DEFINED)
1877 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1878 ref(cUNOPo->op_first, o->op_type);
1883 o->op_flags |= OPf_REF;
1888 if (!(o->op_flags & OPf_KIDS))
1890 ref(cBINOPo->op_first, type);
1894 ref(cBINOPo->op_first, o->op_type);
1895 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1896 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1897 : type == OP_RV2HV ? OPpDEREF_HV
1899 o->op_flags |= OPf_MOD;
1907 if (!(o->op_flags & OPf_KIDS))
1909 ref(cLISTOPo->op_last, type);
1919 S_dup_attrlist(pTHX_ OP *o)
1923 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1924 * where the first kid is OP_PUSHMARK and the remaining ones
1925 * are OP_CONST. We need to push the OP_CONST values.
1927 if (o->op_type == OP_CONST)
1928 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc(cSVOPo->op_sv));
1930 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1931 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1932 if (o->op_type == OP_CONST)
1933 rop = append_elem(OP_LIST, rop,
1934 newSVOP(OP_CONST, o->op_flags,
1935 SvREFCNT_inc(cSVOPo->op_sv)));
1942 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs, bool for_my)
1946 /* fake up C<use attributes $pkg,$rv,@attrs> */
1947 ENTER; /* need to protect against side-effects of 'use' */
1950 stashsv = newSVpv(HvNAME(stash), 0);
1952 stashsv = &PL_sv_no;
1954 #define ATTRSMODULE "attributes"
1955 #define ATTRSMODULE_PM "attributes.pm"
1959 /* Don't force the C<use> if we don't need it. */
1960 svp = hv_fetch(GvHVn(PL_incgv), ATTRSMODULE_PM,
1961 sizeof(ATTRSMODULE_PM)-1, 0);
1962 if (svp && *svp != &PL_sv_undef)
1963 ; /* already in %INC */
1965 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
1966 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1970 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1971 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1973 prepend_elem(OP_LIST,
1974 newSVOP(OP_CONST, 0, stashsv),
1975 prepend_elem(OP_LIST,
1976 newSVOP(OP_CONST, 0,
1978 dup_attrlist(attrs))));
1984 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
1986 OP *pack, *imop, *arg;
1992 assert(target->op_type == OP_PADSV ||
1993 target->op_type == OP_PADHV ||
1994 target->op_type == OP_PADAV);
1996 /* Ensure that attributes.pm is loaded. */
1997 apply_attrs(stash, pad_sv(target->op_targ), attrs, TRUE);
1999 /* Need package name for method call. */
2000 pack = newSVOP(OP_CONST, 0, newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1));
2002 /* Build up the real arg-list. */
2004 stashsv = newSVpv(HvNAME(stash), 0);
2006 stashsv = &PL_sv_no;
2007 arg = newOP(OP_PADSV, 0);
2008 arg->op_targ = target->op_targ;
2009 arg = prepend_elem(OP_LIST,
2010 newSVOP(OP_CONST, 0, stashsv),
2011 prepend_elem(OP_LIST,
2012 newUNOP(OP_REFGEN, 0,
2013 mod(arg, OP_REFGEN)),
2014 dup_attrlist(attrs)));
2016 /* Fake up a method call to import */
2017 meth = newSVpvn("import", 6);
2018 (void)SvUPGRADE(meth, SVt_PVIV);
2019 (void)SvIOK_on(meth);
2020 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
2021 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
2022 append_elem(OP_LIST,
2023 prepend_elem(OP_LIST, pack, list(arg)),
2024 newSVOP(OP_METHOD_NAMED, 0, meth)));
2025 imop->op_private |= OPpENTERSUB_NOMOD;
2027 /* Combine the ops. */
2028 *imopsp = append_elem(OP_LIST, *imopsp, imop);
2032 =notfor apidoc apply_attrs_string
2034 Attempts to apply a list of attributes specified by the C<attrstr> and
2035 C<len> arguments to the subroutine identified by the C<cv> argument which
2036 is expected to be associated with the package identified by the C<stashpv>
2037 argument (see L<attributes>). It gets this wrong, though, in that it
2038 does not correctly identify the boundaries of the individual attribute
2039 specifications within C<attrstr>. This is not really intended for the
2040 public API, but has to be listed here for systems such as AIX which
2041 need an explicit export list for symbols. (It's called from XS code
2042 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
2043 to respect attribute syntax properly would be welcome.
2049 Perl_apply_attrs_string(pTHX_ char *stashpv, CV *cv,
2050 char *attrstr, STRLEN len)
2055 len = strlen(attrstr);
2059 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
2061 char *sstr = attrstr;
2062 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
2063 attrs = append_elem(OP_LIST, attrs,
2064 newSVOP(OP_CONST, 0,
2065 newSVpvn(sstr, attrstr-sstr)));
2069 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2070 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
2071 Nullsv, prepend_elem(OP_LIST,
2072 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
2073 prepend_elem(OP_LIST,
2074 newSVOP(OP_CONST, 0,
2080 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
2085 if (!o || PL_error_count)
2089 if (type == OP_LIST) {
2090 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2091 my_kid(kid, attrs, imopsp);
2092 } else if (type == OP_UNDEF) {
2094 } else if (type == OP_RV2SV || /* "our" declaration */
2096 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
2097 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
2098 yyerror(Perl_form(aTHX_ "Can't declare %s in %s",
2099 OP_DESC(o), PL_in_my == KEY_our ? "our" : "my"));
2101 GV *gv = cGVOPx_gv(cUNOPo->op_first);
2103 PL_in_my_stash = Nullhv;
2104 apply_attrs(GvSTASH(gv),
2105 (type == OP_RV2SV ? GvSV(gv) :
2106 type == OP_RV2AV ? (SV*)GvAV(gv) :
2107 type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)gv),
2110 o->op_private |= OPpOUR_INTRO;
2113 else if (type != OP_PADSV &&
2116 type != OP_PUSHMARK)
2118 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2120 PL_in_my == KEY_our ? "our" : "my"));
2123 else if (attrs && type != OP_PUSHMARK) {
2128 PL_in_my_stash = Nullhv;
2130 /* check for C<my Dog $spot> when deciding package */
2131 namesvp = av_fetch(PL_comppad_name, o->op_targ, FALSE);
2132 if (namesvp && *namesvp && (SvFLAGS(*namesvp) & SVpad_TYPED))
2133 stash = SvSTASH(*namesvp);
2135 stash = PL_curstash;
2136 apply_attrs_my(stash, o, attrs, imopsp);
2138 o->op_flags |= OPf_MOD;
2139 o->op_private |= OPpLVAL_INTRO;
2144 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
2147 int maybe_scalar = 0;
2149 if (o->op_flags & OPf_PARENS)
2155 o = my_kid(o, attrs, &rops);
2157 if (maybe_scalar && o->op_type == OP_PADSV) {
2158 o = scalar(append_list(OP_LIST, (LISTOP*)rops, (LISTOP*)o));
2159 o->op_private |= OPpLVAL_INTRO;
2162 o = append_list(OP_LIST, (LISTOP*)o, (LISTOP*)rops);
2165 PL_in_my_stash = Nullhv;
2170 Perl_my(pTHX_ OP *o)
2172 return my_attrs(o, Nullop);
2176 Perl_sawparens(pTHX_ OP *o)
2179 o->op_flags |= OPf_PARENS;
2184 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
2188 if (ckWARN(WARN_MISC) &&
2189 (left->op_type == OP_RV2AV ||
2190 left->op_type == OP_RV2HV ||
2191 left->op_type == OP_PADAV ||
2192 left->op_type == OP_PADHV)) {
2193 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
2194 right->op_type == OP_TRANS)
2195 ? right->op_type : OP_MATCH];
2196 const char *sample = ((left->op_type == OP_RV2AV ||
2197 left->op_type == OP_PADAV)
2198 ? "@array" : "%hash");
2199 Perl_warner(aTHX_ packWARN(WARN_MISC),
2200 "Applying %s to %s will act on scalar(%s)",
2201 desc, sample, sample);
2204 if (right->op_type == OP_CONST &&
2205 cSVOPx(right)->op_private & OPpCONST_BARE &&
2206 cSVOPx(right)->op_private & OPpCONST_STRICT)
2208 no_bareword_allowed(right);
2211 if (!(right->op_flags & OPf_STACKED) &&
2212 (right->op_type == OP_MATCH ||
2213 right->op_type == OP_SUBST ||
2214 right->op_type == OP_TRANS)) {
2215 right->op_flags |= OPf_STACKED;
2216 if (right->op_type != OP_MATCH &&
2217 ! (right->op_type == OP_TRANS &&
2218 right->op_private & OPpTRANS_IDENTICAL))
2219 left = mod(left, right->op_type);
2220 if (right->op_type == OP_TRANS)
2221 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
2223 o = prepend_elem(right->op_type, scalar(left), right);
2225 return newUNOP(OP_NOT, 0, scalar(o));
2229 return bind_match(type, left,
2230 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
2234 Perl_invert(pTHX_ OP *o)
2238 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
2239 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
2243 Perl_scope(pTHX_ OP *o)
2246 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
2247 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
2248 o->op_type = OP_LEAVE;
2249 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
2252 if (o->op_type == OP_LINESEQ) {
2254 o->op_type = OP_SCOPE;
2255 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
2256 kid = ((LISTOP*)o)->op_first;
2257 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
2261 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
2268 Perl_save_hints(pTHX)
2271 SAVESPTR(GvHV(PL_hintgv));
2272 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
2273 SAVEFREESV(GvHV(PL_hintgv));
2277 Perl_block_start(pTHX_ int full)
2279 int retval = PL_savestack_ix;
2281 SAVEI32(PL_comppad_name_floor);
2282 PL_comppad_name_floor = AvFILLp(PL_comppad_name);
2284 PL_comppad_name_fill = PL_comppad_name_floor;
2285 if (PL_comppad_name_floor < 0)
2286 PL_comppad_name_floor = 0;
2287 SAVEI32(PL_min_intro_pending);
2288 SAVEI32(PL_max_intro_pending);
2289 PL_min_intro_pending = 0;
2290 SAVEI32(PL_comppad_name_fill);
2291 SAVEI32(PL_padix_floor);
2292 PL_padix_floor = PL_padix;
2293 PL_pad_reset_pending = FALSE;
2295 PL_hints &= ~HINT_BLOCK_SCOPE;
2296 SAVESPTR(PL_compiling.cop_warnings);
2297 if (! specialWARN(PL_compiling.cop_warnings)) {
2298 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
2299 SAVEFREESV(PL_compiling.cop_warnings) ;
2301 SAVESPTR(PL_compiling.cop_io);
2302 if (! specialCopIO(PL_compiling.cop_io)) {
2303 PL_compiling.cop_io = newSVsv(PL_compiling.cop_io) ;
2304 SAVEFREESV(PL_compiling.cop_io) ;
2310 Perl_block_end(pTHX_ I32 floor, OP *seq)
2312 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
2313 line_t copline = PL_copline;
2314 /* there should be a nextstate in every block */
2315 OP* retval = seq ? scalarseq(seq) : newSTATEOP(0, Nullch, seq);
2316 PL_copline = copline; /* XXX newSTATEOP may reset PL_copline */
2318 PL_pad_reset_pending = FALSE;
2319 PL_compiling.op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
2321 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
2322 pad_leavemy(PL_comppad_name_fill);
2330 #ifdef USE_5005THREADS
2331 OP *o = newOP(OP_THREADSV, 0);
2332 o->op_targ = find_threadsv("_");
2335 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
2336 #endif /* USE_5005THREADS */
2340 Perl_newPROG(pTHX_ OP *o)
2345 PL_eval_root = newUNOP(OP_LEAVEEVAL,
2346 ((PL_in_eval & EVAL_KEEPERR)
2347 ? OPf_SPECIAL : 0), o);
2348 PL_eval_start = linklist(PL_eval_root);
2349 PL_eval_root->op_private |= OPpREFCOUNTED;
2350 OpREFCNT_set(PL_eval_root, 1);
2351 PL_eval_root->op_next = 0;
2352 CALL_PEEP(PL_eval_start);
2357 PL_main_root = scope(sawparens(scalarvoid(o)));
2358 PL_curcop = &PL_compiling;
2359 PL_main_start = LINKLIST(PL_main_root);
2360 PL_main_root->op_private |= OPpREFCOUNTED;
2361 OpREFCNT_set(PL_main_root, 1);
2362 PL_main_root->op_next = 0;
2363 CALL_PEEP(PL_main_start);
2366 /* Register with debugger */
2368 CV *cv = get_cv("DB::postponed", FALSE);
2372 XPUSHs((SV*)CopFILEGV(&PL_compiling));
2374 call_sv((SV*)cv, G_DISCARD);
2381 Perl_localize(pTHX_ OP *o, I32 lex)
2383 if (o->op_flags & OPf_PARENS)
2386 if (ckWARN(WARN_PARENTHESIS)
2387 && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',')
2389 char *s = PL_bufptr;
2391 while (*s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s) || strchr("@$%, ", *s)))
2394 if (*s == ';' || *s == '=')
2395 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
2396 "Parentheses missing around \"%s\" list",
2397 lex ? (PL_in_my == KEY_our ? "our" : "my") : "local");
2403 o = mod(o, OP_NULL); /* a bit kludgey */
2405 PL_in_my_stash = Nullhv;
2410 Perl_jmaybe(pTHX_ OP *o)
2412 if (o->op_type == OP_LIST) {
2414 #ifdef USE_5005THREADS
2415 o2 = newOP(OP_THREADSV, 0);
2416 o2->op_targ = find_threadsv(";");
2418 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
2419 #endif /* USE_5005THREADS */
2420 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2426 Perl_fold_constants(pTHX_ register OP *o)
2429 I32 type = o->op_type;
2432 if (PL_opargs[type] & OA_RETSCALAR)
2434 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2435 o->op_targ = pad_alloc(type, SVs_PADTMP);
2437 /* integerize op, unless it happens to be C<-foo>.
2438 * XXX should pp_i_negate() do magic string negation instead? */
2439 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2440 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2441 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2443 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2446 if (!(PL_opargs[type] & OA_FOLDCONST))
2451 /* XXX might want a ck_negate() for this */
2452 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2464 /* XXX what about the numeric ops? */
2465 if (PL_hints & HINT_LOCALE)
2470 goto nope; /* Don't try to run w/ errors */
2472 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2473 if ((curop->op_type != OP_CONST ||
2474 (curop->op_private & OPpCONST_BARE)) &&
2475 curop->op_type != OP_LIST &&
2476 curop->op_type != OP_SCALAR &&
2477 curop->op_type != OP_NULL &&
2478 curop->op_type != OP_PUSHMARK)
2484 curop = LINKLIST(o);
2488 sv = *(PL_stack_sp--);
2489 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
2490 pad_swipe(o->op_targ);
2491 else if (SvTEMP(sv)) { /* grab mortal temp? */
2492 (void)SvREFCNT_inc(sv);
2496 if (type == OP_RV2GV)
2497 return newGVOP(OP_GV, 0, (GV*)sv);
2499 /* try to smush double to int, but don't smush -2.0 to -2 */
2500 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
2503 #ifdef PERL_PRESERVE_IVUV
2504 /* Only bother to attempt to fold to IV if
2505 most operators will benefit */
2509 return newSVOP(OP_CONST, 0, sv);
2517 Perl_gen_constant_list(pTHX_ register OP *o)
2520 I32 oldtmps_floor = PL_tmps_floor;
2524 return o; /* Don't attempt to run with errors */
2526 PL_op = curop = LINKLIST(o);
2533 PL_tmps_floor = oldtmps_floor;
2535 o->op_type = OP_RV2AV;
2536 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2537 o->op_seq = 0; /* needs to be revisited in peep() */
2538 curop = ((UNOP*)o)->op_first;
2539 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
2546 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2548 if (!o || o->op_type != OP_LIST)
2549 o = newLISTOP(OP_LIST, 0, o, Nullop);
2551 o->op_flags &= ~OPf_WANT;
2553 if (!(PL_opargs[type] & OA_MARK))
2554 op_null(cLISTOPo->op_first);
2556 o->op_type = (OPCODE)type;
2557 o->op_ppaddr = PL_ppaddr[type];
2558 o->op_flags |= flags;
2560 o = CHECKOP(type, o);
2561 if (o->op_type != type)
2564 return fold_constants(o);
2567 /* List constructors */
2570 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2578 if (first->op_type != type
2579 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
2581 return newLISTOP(type, 0, first, last);
2584 if (first->op_flags & OPf_KIDS)
2585 ((LISTOP*)first)->op_last->op_sibling = last;
2587 first->op_flags |= OPf_KIDS;
2588 ((LISTOP*)first)->op_first = last;
2590 ((LISTOP*)first)->op_last = last;
2595 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2603 if (first->op_type != type)
2604 return prepend_elem(type, (OP*)first, (OP*)last);
2606 if (last->op_type != type)
2607 return append_elem(type, (OP*)first, (OP*)last);
2609 first->op_last->op_sibling = last->op_first;
2610 first->op_last = last->op_last;
2611 first->op_flags |= (last->op_flags & OPf_KIDS);
2619 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2627 if (last->op_type == type) {
2628 if (type == OP_LIST) { /* already a PUSHMARK there */
2629 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2630 ((LISTOP*)last)->op_first->op_sibling = first;
2631 if (!(first->op_flags & OPf_PARENS))
2632 last->op_flags &= ~OPf_PARENS;
2635 if (!(last->op_flags & OPf_KIDS)) {
2636 ((LISTOP*)last)->op_last = first;
2637 last->op_flags |= OPf_KIDS;
2639 first->op_sibling = ((LISTOP*)last)->op_first;
2640 ((LISTOP*)last)->op_first = first;
2642 last->op_flags |= OPf_KIDS;
2646 return newLISTOP(type, 0, first, last);
2652 Perl_newNULLLIST(pTHX)
2654 return newOP(OP_STUB, 0);
2658 Perl_force_list(pTHX_ OP *o)
2660 if (!o || o->op_type != OP_LIST)
2661 o = newLISTOP(OP_LIST, 0, o, Nullop);
2667 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2671 NewOp(1101, listop, 1, LISTOP);
2673 listop->op_type = (OPCODE)type;
2674 listop->op_ppaddr = PL_ppaddr[type];
2677 listop->op_flags = (U8)flags;
2681 else if (!first && last)
2684 first->op_sibling = last;
2685 listop->op_first = first;
2686 listop->op_last = last;
2687 if (type == OP_LIST) {
2689 pushop = newOP(OP_PUSHMARK, 0);
2690 pushop->op_sibling = first;
2691 listop->op_first = pushop;
2692 listop->op_flags |= OPf_KIDS;
2694 listop->op_last = pushop;
2701 Perl_newOP(pTHX_ I32 type, I32 flags)
2704 NewOp(1101, o, 1, OP);
2705 o->op_type = (OPCODE)type;
2706 o->op_ppaddr = PL_ppaddr[type];
2707 o->op_flags = (U8)flags;
2710 o->op_private = (U8)(0 | (flags >> 8));
2711 if (PL_opargs[type] & OA_RETSCALAR)
2713 if (PL_opargs[type] & OA_TARGET)
2714 o->op_targ = pad_alloc(type, SVs_PADTMP);
2715 return CHECKOP(type, o);
2719 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
2724 first = newOP(OP_STUB, 0);
2725 if (PL_opargs[type] & OA_MARK)
2726 first = force_list(first);
2728 NewOp(1101, unop, 1, UNOP);
2729 unop->op_type = (OPCODE)type;
2730 unop->op_ppaddr = PL_ppaddr[type];
2731 unop->op_first = first;
2732 unop->op_flags = flags | OPf_KIDS;
2733 unop->op_private = (U8)(1 | (flags >> 8));
2734 unop = (UNOP*) CHECKOP(type, unop);
2738 return fold_constants((OP *) unop);
2742 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2745 NewOp(1101, binop, 1, BINOP);
2748 first = newOP(OP_NULL, 0);
2750 binop->op_type = (OPCODE)type;
2751 binop->op_ppaddr = PL_ppaddr[type];
2752 binop->op_first = first;
2753 binop->op_flags = flags | OPf_KIDS;
2756 binop->op_private = (U8)(1 | (flags >> 8));
2759 binop->op_private = (U8)(2 | (flags >> 8));
2760 first->op_sibling = last;
2763 binop = (BINOP*)CHECKOP(type, binop);
2764 if (binop->op_next || binop->op_type != (OPCODE)type)
2767 binop->op_last = binop->op_first->op_sibling;
2769 return fold_constants((OP *)binop);
2773 uvcompare(const void *a, const void *b)
2775 if (*((UV *)a) < (*(UV *)b))
2777 if (*((UV *)a) > (*(UV *)b))
2779 if (*((UV *)a+1) < (*(UV *)b+1))
2781 if (*((UV *)a+1) > (*(UV *)b+1))
2787 Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
2789 SV *tstr = ((SVOP*)expr)->op_sv;
2790 SV *rstr = ((SVOP*)repl)->op_sv;
2793 U8 *t = (U8*)SvPV(tstr, tlen);
2794 U8 *r = (U8*)SvPV(rstr, rlen);
2801 register short *tbl;
2803 PL_hints |= HINT_BLOCK_SCOPE;
2804 complement = o->op_private & OPpTRANS_COMPLEMENT;
2805 del = o->op_private & OPpTRANS_DELETE;
2806 squash = o->op_private & OPpTRANS_SQUASH;
2809 o->op_private |= OPpTRANS_FROM_UTF;
2812 o->op_private |= OPpTRANS_TO_UTF;
2814 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2815 SV* listsv = newSVpvn("# comment\n",10);
2817 U8* tend = t + tlen;
2818 U8* rend = r + rlen;
2832 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2833 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2839 tsave = t = bytes_to_utf8(t, &len);
2842 if (!to_utf && rlen) {
2844 rsave = r = bytes_to_utf8(r, &len);
2848 /* There are several snags with this code on EBCDIC:
2849 1. 0xFF is a legal UTF-EBCDIC byte (there are no illegal bytes).
2850 2. scan_const() in toke.c has encoded chars in native encoding which makes
2851 ranges at least in EBCDIC 0..255 range the bottom odd.
2855 U8 tmpbuf[UTF8_MAXLEN+1];
2858 New(1109, cp, 2*tlen, UV);
2860 transv = newSVpvn("",0);
2862 cp[2*i] = utf8n_to_uvuni(t, tend-t, &ulen, 0);
2864 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) {
2866 cp[2*i+1] = utf8n_to_uvuni(t, tend-t, &ulen, 0);
2870 cp[2*i+1] = cp[2*i];
2874 qsort(cp, i, 2*sizeof(UV), uvcompare);
2875 for (j = 0; j < i; j++) {
2877 diff = val - nextmin;
2879 t = uvuni_to_utf8(tmpbuf,nextmin);
2880 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2882 U8 range_mark = UTF_TO_NATIVE(0xff);
2883 t = uvuni_to_utf8(tmpbuf, val - 1);
2884 sv_catpvn(transv, (char *)&range_mark, 1);
2885 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2892 t = uvuni_to_utf8(tmpbuf,nextmin);
2893 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2895 U8 range_mark = UTF_TO_NATIVE(0xff);
2896 sv_catpvn(transv, (char *)&range_mark, 1);
2898 t = uvuni_to_utf8_flags(tmpbuf, 0x7fffffff,
2899 UNICODE_ALLOW_SUPER);
2900 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2901 t = (U8*)SvPVX(transv);
2902 tlen = SvCUR(transv);
2906 else if (!rlen && !del) {
2907 r = t; rlen = tlen; rend = tend;
2910 if ((!rlen && !del) || t == r ||
2911 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
2913 o->op_private |= OPpTRANS_IDENTICAL;
2917 while (t < tend || tfirst <= tlast) {
2918 /* see if we need more "t" chars */
2919 if (tfirst > tlast) {
2920 tfirst = (I32)utf8n_to_uvuni(t, tend - t, &ulen, 0);
2922 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) { /* illegal utf8 val indicates range */
2924 tlast = (I32)utf8n_to_uvuni(t, tend - t, &ulen, 0);
2931 /* now see if we need more "r" chars */
2932 if (rfirst > rlast) {
2934 rfirst = (I32)utf8n_to_uvuni(r, rend - r, &ulen, 0);
2936 if (r < rend && NATIVE_TO_UTF(*r) == 0xff) { /* illegal utf8 val indicates range */
2938 rlast = (I32)utf8n_to_uvuni(r, rend - r, &ulen, 0);
2947 rfirst = rlast = 0xffffffff;
2951 /* now see which range will peter our first, if either. */
2952 tdiff = tlast - tfirst;
2953 rdiff = rlast - rfirst;
2960 if (rfirst == 0xffffffff) {
2961 diff = tdiff; /* oops, pretend rdiff is infinite */
2963 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
2964 (long)tfirst, (long)tlast);
2966 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
2970 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
2971 (long)tfirst, (long)(tfirst + diff),
2974 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
2975 (long)tfirst, (long)rfirst);
2977 if (rfirst + diff > max)
2978 max = rfirst + diff;
2980 grows = (tfirst < rfirst &&
2981 UNISKIP(tfirst) < UNISKIP(rfirst + diff));
2993 else if (max > 0xff)
2998 Safefree(cPVOPo->op_pv);
2999 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
3000 SvREFCNT_dec(listsv);
3002 SvREFCNT_dec(transv);
3004 if (!del && havefinal && rlen)
3005 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5,
3006 newSVuv((UV)final), 0);
3009 o->op_private |= OPpTRANS_GROWS;
3021 tbl = (short*)cPVOPo->op_pv;
3023 Zero(tbl, 256, short);
3024 for (i = 0; i < (I32)tlen; i++)
3026 for (i = 0, j = 0; i < 256; i++) {
3028 if (j >= (I32)rlen) {
3037 if (i < 128 && r[j] >= 128)
3047 o->op_private |= OPpTRANS_IDENTICAL;
3049 else if (j >= (I32)rlen)
3052 cPVOPo->op_pv = (char*)Renew(tbl, 0x101+rlen-j, short);
3053 tbl[0x100] = rlen - j;
3054 for (i=0; i < (I32)rlen - j; i++)
3055 tbl[0x101+i] = r[j+i];
3059 if (!rlen && !del) {
3062 o->op_private |= OPpTRANS_IDENTICAL;
3064 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
3065 o->op_private |= OPpTRANS_IDENTICAL;
3067 for (i = 0; i < 256; i++)
3069 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
3070 if (j >= (I32)rlen) {
3072 if (tbl[t[i]] == -1)
3078 if (tbl[t[i]] == -1) {
3079 if (t[i] < 128 && r[j] >= 128)
3086 o->op_private |= OPpTRANS_GROWS;
3094 Perl_newPMOP(pTHX_ I32 type, I32 flags)
3098 NewOp(1101, pmop, 1, PMOP);
3099 pmop->op_type = (OPCODE)type;
3100 pmop->op_ppaddr = PL_ppaddr[type];
3101 pmop->op_flags = (U8)flags;
3102 pmop->op_private = (U8)(0 | (flags >> 8));
3104 if (PL_hints & HINT_RE_TAINT)
3105 pmop->op_pmpermflags |= PMf_RETAINT;
3106 if (PL_hints & HINT_LOCALE)
3107 pmop->op_pmpermflags |= PMf_LOCALE;
3108 pmop->op_pmflags = pmop->op_pmpermflags;
3113 if(av_len((AV*) PL_regex_pad[0]) > -1) {
3114 repointer = av_pop((AV*)PL_regex_pad[0]);
3115 pmop->op_pmoffset = SvIV(repointer);
3116 SvREPADTMP_off(repointer);
3117 sv_setiv(repointer,0);
3119 repointer = newSViv(0);
3120 av_push(PL_regex_padav,SvREFCNT_inc(repointer));
3121 pmop->op_pmoffset = av_len(PL_regex_padav);
3122 PL_regex_pad = AvARRAY(PL_regex_padav);
3127 /* link into pm list */
3128 if (type != OP_TRANS && PL_curstash) {
3129 pmop->op_pmnext = HvPMROOT(PL_curstash);
3130 HvPMROOT(PL_curstash) = pmop;
3131 PmopSTASH_set(pmop,PL_curstash);
3138 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
3142 I32 repl_has_vars = 0;
3144 if (o->op_type == OP_TRANS)
3145 return pmtrans(o, expr, repl);
3147 PL_hints |= HINT_BLOCK_SCOPE;
3150 if (expr->op_type == OP_CONST) {
3152 SV *pat = ((SVOP*)expr)->op_sv;
3153 char *p = SvPV(pat, plen);
3154 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
3155 sv_setpvn(pat, "\\s+", 3);
3156 p = SvPV(pat, plen);
3157 pm->op_pmflags |= PMf_SKIPWHITE;
3160 pm->op_pmdynflags |= PMdf_UTF8;
3161 PM_SETRE(pm, CALLREGCOMP(aTHX_ p, p + plen, pm));
3162 if (strEQ("\\s+", PM_GETRE(pm)->precomp))
3163 pm->op_pmflags |= PMf_WHITE;
3167 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
3168 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
3170 : OP_REGCMAYBE),0,expr);
3172 NewOp(1101, rcop, 1, LOGOP);
3173 rcop->op_type = OP_REGCOMP;
3174 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
3175 rcop->op_first = scalar(expr);
3176 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
3177 ? (OPf_SPECIAL | OPf_KIDS)
3179 rcop->op_private = 1;
3182 /* establish postfix order */
3183 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
3185 rcop->op_next = expr;
3186 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
3189 rcop->op_next = LINKLIST(expr);
3190 expr->op_next = (OP*)rcop;
3193 prepend_elem(o->op_type, scalar((OP*)rcop), o);
3198 if (pm->op_pmflags & PMf_EVAL) {
3200 if (CopLINE(PL_curcop) < PL_multi_end)
3201 CopLINE_set(PL_curcop, (line_t)PL_multi_end);
3203 #ifdef USE_5005THREADS
3204 else if (repl->op_type == OP_THREADSV
3205 && strchr("&`'123456789+",
3206 PL_threadsv_names[repl->op_targ]))
3210 #endif /* USE_5005THREADS */
3211 else if (repl->op_type == OP_CONST)
3215 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
3216 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3217 #ifdef USE_5005THREADS
3218 if (curop->op_type == OP_THREADSV) {
3220 if (strchr("&`'123456789+", curop->op_private))
3224 if (curop->op_type == OP_GV) {
3225 GV *gv = cGVOPx_gv(curop);
3227 if (strchr("&`'123456789+", *GvENAME(gv)))
3230 #endif /* USE_5005THREADS */
3231 else if (curop->op_type == OP_RV2CV)
3233 else if (curop->op_type == OP_RV2SV ||
3234 curop->op_type == OP_RV2AV ||
3235 curop->op_type == OP_RV2HV ||
3236 curop->op_type == OP_RV2GV) {
3237 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
3240 else if (curop->op_type == OP_PADSV ||
3241 curop->op_type == OP_PADAV ||
3242 curop->op_type == OP_PADHV ||
3243 curop->op_type == OP_PADANY) {
3246 else if (curop->op_type == OP_PUSHRE)
3247 ; /* Okay here, dangerous in newASSIGNOP */
3257 || PM_GETRE(pm)->reganch & ROPT_EVAL_SEEN))) {
3258 pm->op_pmflags |= PMf_CONST; /* const for long enough */
3259 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
3260 prepend_elem(o->op_type, scalar(repl), o);
3263 if (curop == repl && !PM_GETRE(pm)) { /* Has variables. */
3264 pm->op_pmflags |= PMf_MAYBE_CONST;
3265 pm->op_pmpermflags |= PMf_MAYBE_CONST;
3267 NewOp(1101, rcop, 1, LOGOP);
3268 rcop->op_type = OP_SUBSTCONT;
3269 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
3270 rcop->op_first = scalar(repl);
3271 rcop->op_flags |= OPf_KIDS;
3272 rcop->op_private = 1;
3275 /* establish postfix order */
3276 rcop->op_next = LINKLIST(repl);
3277 repl->op_next = (OP*)rcop;
3279 pm->op_pmreplroot = scalar((OP*)rcop);
3280 pm->op_pmreplstart = LINKLIST(rcop);
3289 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
3292 NewOp(1101, svop, 1, SVOP);
3293 svop->op_type = (OPCODE)type;
3294 svop->op_ppaddr = PL_ppaddr[type];
3296 svop->op_next = (OP*)svop;
3297 svop->op_flags = (U8)flags;
3298 if (PL_opargs[type] & OA_RETSCALAR)
3300 if (PL_opargs[type] & OA_TARGET)
3301 svop->op_targ = pad_alloc(type, SVs_PADTMP);
3302 return CHECKOP(type, svop);
3306 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
3309 NewOp(1101, padop, 1, PADOP);
3310 padop->op_type = (OPCODE)type;
3311 padop->op_ppaddr = PL_ppaddr[type];
3312 padop->op_padix = pad_alloc(type, SVs_PADTMP);
3313 SvREFCNT_dec(PL_curpad[padop->op_padix]);
3314 PL_curpad[padop->op_padix] = sv;
3316 padop->op_next = (OP*)padop;
3317 padop->op_flags = (U8)flags;
3318 if (PL_opargs[type] & OA_RETSCALAR)
3320 if (PL_opargs[type] & OA_TARGET)
3321 padop->op_targ = pad_alloc(type, SVs_PADTMP);
3322 return CHECKOP(type, padop);
3326 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
3330 return newPADOP(type, flags, SvREFCNT_inc(gv));
3332 return newSVOP(type, flags, SvREFCNT_inc(gv));
3337 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
3340 NewOp(1101, pvop, 1, PVOP);
3341 pvop->op_type = (OPCODE)type;
3342 pvop->op_ppaddr = PL_ppaddr[type];
3344 pvop->op_next = (OP*)pvop;
3345 pvop->op_flags = (U8)flags;
3346 if (PL_opargs[type] & OA_RETSCALAR)
3348 if (PL_opargs[type] & OA_TARGET)
3349 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
3350 return CHECKOP(type, pvop);
3354 Perl_package(pTHX_ OP *o)
3359 save_hptr(&PL_curstash);
3360 save_item(PL_curstname);
3362 name = SvPV(cSVOPo->op_sv, len);
3363 PL_curstash = gv_stashpvn(name, len, TRUE);
3364 sv_setpvn(PL_curstname, name, len);
3367 PL_hints |= HINT_BLOCK_SCOPE;
3368 PL_copline = NOLINE;
3373 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg)
3379 if (id->op_type != OP_CONST)
3380 Perl_croak(aTHX_ "Module name must be constant");
3384 if (version != Nullop) {
3385 SV *vesv = ((SVOP*)version)->op_sv;
3387 if (arg == Nullop && !SvNIOKp(vesv)) {
3394 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
3395 Perl_croak(aTHX_ "Version number must be constant number");
3397 /* Make copy of id so we don't free it twice */
3398 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3400 /* Fake up a method call to VERSION */
3401 meth = newSVpvn("VERSION",7);
3402 sv_upgrade(meth, SVt_PVIV);
3403 (void)SvIOK_on(meth);
3404 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
3405 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3406 append_elem(OP_LIST,
3407 prepend_elem(OP_LIST, pack, list(version)),
3408 newSVOP(OP_METHOD_NAMED, 0, meth)));
3412 /* Fake up an import/unimport */
3413 if (arg && arg->op_type == OP_STUB)
3414 imop = arg; /* no import on explicit () */
3415 else if (SvNIOKp(((SVOP*)id)->op_sv)) {
3416 imop = Nullop; /* use 5.0; */
3421 /* Make copy of id so we don't free it twice */
3422 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3424 /* Fake up a method call to import/unimport */
3425 meth = aver ? newSVpvn("import",6) : newSVpvn("unimport", 8);
3426 (void)SvUPGRADE(meth, SVt_PVIV);
3427 (void)SvIOK_on(meth);
3428 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
3429 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3430 append_elem(OP_LIST,
3431 prepend_elem(OP_LIST, pack, list(arg)),
3432 newSVOP(OP_METHOD_NAMED, 0, meth)));
3435 /* Fake up the BEGIN {}, which does its thing immediately. */
3437 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
3440 append_elem(OP_LINESEQ,
3441 append_elem(OP_LINESEQ,
3442 newSTATEOP(0, Nullch, newUNOP(OP_REQUIRE, 0, id)),
3443 newSTATEOP(0, Nullch, veop)),
3444 newSTATEOP(0, Nullch, imop) ));
3446 /* The "did you use incorrect case?" warning used to be here.
3447 * The problem is that on case-insensitive filesystems one
3448 * might get false positives for "use" (and "require"):
3449 * "use Strict" or "require CARP" will work. This causes
3450 * portability problems for the script: in case-strict
3451 * filesystems the script will stop working.
3453 * The "incorrect case" warning checked whether "use Foo"
3454 * imported "Foo" to your namespace, but that is wrong, too:
3455 * there is no requirement nor promise in the language that
3456 * a Foo.pm should or would contain anything in package "Foo".
3458 * There is very little Configure-wise that can be done, either:
3459 * the case-sensitivity of the build filesystem of Perl does not
3460 * help in guessing the case-sensitivity of the runtime environment.
3463 PL_hints |= HINT_BLOCK_SCOPE;
3464 PL_copline = NOLINE;
3469 =head1 Embedding Functions
3471 =for apidoc load_module
3473 Loads the module whose name is pointed to by the string part of name.
3474 Note that the actual module name, not its filename, should be given.
3475 Eg, "Foo::Bar" instead of "Foo/Bar.pm". flags can be any of
3476 PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS
3477 (or 0 for no flags). ver, if specified, provides version semantics
3478 similar to C<use Foo::Bar VERSION>. The optional trailing SV*
3479 arguments can be used to specify arguments to the module's import()
3480 method, similar to C<use Foo::Bar VERSION LIST>.
3485 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
3488 va_start(args, ver);
3489 vload_module(flags, name, ver, &args);
3493 #ifdef PERL_IMPLICIT_CONTEXT
3495 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
3499 va_start(args, ver);
3500 vload_module(flags, name, ver, &args);
3506 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
3508 OP *modname, *veop, *imop;
3510 modname = newSVOP(OP_CONST, 0, name);
3511 modname->op_private |= OPpCONST_BARE;
3513 veop = newSVOP(OP_CONST, 0, ver);
3517 if (flags & PERL_LOADMOD_NOIMPORT) {
3518 imop = sawparens(newNULLLIST());
3520 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
3521 imop = va_arg(*args, OP*);
3526 sv = va_arg(*args, SV*);
3528 imop = append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
3529 sv = va_arg(*args, SV*);
3533 line_t ocopline = PL_copline;
3534 int oexpect = PL_expect;
3536 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
3537 veop, modname, imop);
3538 PL_expect = oexpect;
3539 PL_copline = ocopline;
3544 Perl_dofile(pTHX_ OP *term)
3549 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
3550 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv)))
3551 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
3553 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
3554 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3555 append_elem(OP_LIST, term,
3556 scalar(newUNOP(OP_RV2CV, 0,
3561 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3567 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
3569 return newBINOP(OP_LSLICE, flags,
3570 list(force_list(subscript)),
3571 list(force_list(listval)) );
3575 S_list_assignment(pTHX_ register OP *o)
3580 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3581 o = cUNOPo->op_first;
3583 if (o->op_type == OP_COND_EXPR) {
3584 I32 t = list_assignment(cLOGOPo->op_first->op_sibling);
3585 I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
3590 yyerror("Assignment to both a list and a scalar");
3594 if (o->op_type == OP_LIST &&
3595 (o->op_flags & OPf_WANT) == OPf_WANT_SCALAR &&
3596 o->op_private & OPpLVAL_INTRO)
3599 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3600 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3601 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
3604 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
3607 if (o->op_type == OP_RV2SV)
3614 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
3619 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN || optype == OP_DORASSIGN) {
3620 return newLOGOP(optype, 0,
3621 mod(scalar(left), optype),
3622 newUNOP(OP_SASSIGN, 0, scalar(right)));
3625 return newBINOP(optype, OPf_STACKED,
3626 mod(scalar(left), optype), scalar(right));
3630 if (list_assignment(left)) {
3634 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3635 left = mod(left, OP_AASSIGN);
3643 curop = list(force_list(left));
3644 o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
3645 o->op_private = (U8)(0 | (flags >> 8));
3646 if (!(left->op_private & OPpLVAL_INTRO)) {
3649 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3650 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3651 if (curop->op_type == OP_GV) {
3652 GV *gv = cGVOPx_gv(curop);
3653 if (gv == PL_defgv || (int)SvCUR(gv) == PL_generation)
3655 SvCUR(gv) = PL_generation;
3657 else if (curop->op_type == OP_PADSV ||
3658 curop->op_type == OP_PADAV ||
3659 curop->op_type == OP_PADHV ||
3660 curop->op_type == OP_PADANY) {
3661 SV **svp = AvARRAY(PL_comppad_name);
3662 SV *sv = svp[curop->op_targ];
3663 if ((int)SvCUR(sv) == PL_generation)
3665 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
3667 else if (curop->op_type == OP_RV2CV)
3669 else if (curop->op_type == OP_RV2SV ||
3670 curop->op_type == OP_RV2AV ||
3671 curop->op_type == OP_RV2HV ||
3672 curop->op_type == OP_RV2GV) {
3673 if (lastop->op_type != OP_GV) /* funny deref? */
3676 else if (curop->op_type == OP_PUSHRE) {
3677 if (((PMOP*)curop)->op_pmreplroot) {
3679 GV *gv = (GV*)PL_curpad[INT2PTR(PADOFFSET,((PMOP*)curop)->op_pmreplroot)];
3681 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3683 if (gv == PL_defgv || (int)SvCUR(gv) == PL_generation)
3685 SvCUR(gv) = PL_generation;
3694 o->op_private |= OPpASSIGN_COMMON;
3696 if (right && right->op_type == OP_SPLIT) {
3698 if ((tmpop = ((LISTOP*)right)->op_first) &&
3699 tmpop->op_type == OP_PUSHRE)
3701 PMOP *pm = (PMOP*)tmpop;
3702 if (left->op_type == OP_RV2AV &&
3703 !(left->op_private & OPpLVAL_INTRO) &&
3704 !(o->op_private & OPpASSIGN_COMMON) )
3706 tmpop = ((UNOP*)left)->op_first;
3707 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
3709 pm->op_pmreplroot = INT2PTR(OP*, cPADOPx(tmpop)->op_padix);
3710 cPADOPx(tmpop)->op_padix = 0; /* steal it */
3712 pm->op_pmreplroot = (OP*)cSVOPx(tmpop)->op_sv;
3713 cSVOPx(tmpop)->op_sv = Nullsv; /* steal it */
3715 pm->op_pmflags |= PMf_ONCE;
3716 tmpop = cUNOPo->op_first; /* to list (nulled) */
3717 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3718 tmpop->op_sibling = Nullop; /* don't free split */
3719 right->op_next = tmpop->op_next; /* fix starting loc */
3720 op_free(o); /* blow off assign */
3721 right->op_flags &= ~OPf_WANT;
3722 /* "I don't know and I don't care." */
3727 if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
3728 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3730 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3732 sv_setiv(sv, PL_modcount+1);
3740 right = newOP(OP_UNDEF, 0);
3741 if (right->op_type == OP_READLINE) {
3742 right->op_flags |= OPf_STACKED;
3743 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3746 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3747 o = newBINOP(OP_SASSIGN, flags,
3748 scalar(right), mod(scalar(left), OP_SASSIGN) );
3760 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3762 U32 seq = intro_my();
3765 NewOp(1101, cop, 1, COP);
3766 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
3767 cop->op_type = OP_DBSTATE;
3768 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3771 cop->op_type = OP_NEXTSTATE;
3772 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3774 cop->op_flags = (U8)flags;
3775 cop->op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
3777 cop->op_private |= NATIVE_HINTS;
3779 PL_compiling.op_private = cop->op_private;
3780 cop->op_next = (OP*)cop;
3783 cop->cop_label = label;
3784 PL_hints |= HINT_BLOCK_SCOPE;
3787 cop->cop_arybase = PL_curcop->cop_arybase;
3788 if (specialWARN(PL_curcop->cop_warnings))
3789 cop->cop_warnings = PL_curcop->cop_warnings ;
3791 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3792 if (specialCopIO(PL_curcop->cop_io))
3793 cop->cop_io = PL_curcop->cop_io;
3795 cop->cop_io = newSVsv(PL_curcop->cop_io) ;
3798 if (PL_copline == NOLINE)
3799 CopLINE_set(cop, CopLINE(PL_curcop));
3801 CopLINE_set(cop, PL_copline);
3802 PL_copline = NOLINE;
3805 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
3807 CopFILEGV_set(cop, CopFILEGV(PL_curcop));
3809 CopSTASH_set(cop, PL_curstash);
3811 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3812 SV **svp = av_fetch(CopFILEAV(PL_curcop), (I32)CopLINE(cop), FALSE);
3813 if (svp && *svp != &PL_sv_undef ) {
3814 (void)SvIOK_on(*svp);
3815 SvIVX(*svp) = PTR2IV(cop);
3819 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3822 /* "Introduce" my variables to visible status. */
3830 if (! PL_min_intro_pending)
3831 return PL_cop_seqmax;
3833 svp = AvARRAY(PL_comppad_name);
3834 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
3835 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
3836 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
3837 SvNVX(sv) = (NV)PL_cop_seqmax;
3840 PL_min_intro_pending = 0;
3841 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
3842 return PL_cop_seqmax++;
3846 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
3848 return new_logop(type, flags, &first, &other);
3852 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
3856 OP *first = *firstp;
3857 OP *other = *otherp;
3859 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3860 return newBINOP(type, flags, scalar(first), scalar(other));
3862 scalarboolean(first);
3863 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3864 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3865 if (type == OP_AND || type == OP_OR) {
3871 first = *firstp = cUNOPo->op_first;
3873 first->op_next = o->op_next;
3874 cUNOPo->op_first = Nullop;
3878 if (first->op_type == OP_CONST) {
3879 if (ckWARN(WARN_BAREWORD) && (first->op_private & OPpCONST_BARE)) {
3880 if (first->op_private & OPpCONST_STRICT)
3881 no_bareword_allowed(first);
3883 Perl_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional");
3885 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3896 else if (ckWARN(WARN_MISC) && (first->op_flags & OPf_KIDS)) {
3897 OP *k1 = ((UNOP*)first)->op_first;
3898 OP *k2 = k1->op_sibling;
3900 switch (first->op_type)
3903 if (k2 && k2->op_type == OP_READLINE
3904 && (k2->op_flags & OPf_STACKED)
3905 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3907 warnop = k2->op_type;
3912 if (k1->op_type == OP_READDIR
3913 || k1->op_type == OP_GLOB
3914 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3915 || k1->op_type == OP_EACH)
3917 warnop = ((k1->op_type == OP_NULL)
3918 ? (OPCODE)k1->op_targ : k1->op_type);
3923 line_t oldline = CopLINE(PL_curcop);
3924 CopLINE_set(PL_curcop, PL_copline);
3925 Perl_warner(aTHX_ packWARN(WARN_MISC),
3926 "Value of %s%s can be \"0\"; test with defined()",
3928 ((warnop == OP_READLINE || warnop == OP_GLOB)
3929 ? " construct" : "() operator"));
3930 CopLINE_set(PL_curcop, oldline);
3937 if (type == OP_ANDASSIGN || type == OP_ORASSIGN || type == OP_DORASSIGN)
3938 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3940 NewOp(1101, logop, 1, LOGOP);
3942 logop->op_type = (OPCODE)type;
3943 logop->op_ppaddr = PL_ppaddr[type];
3944 logop->op_first = first;
3945 logop->op_flags = flags | OPf_KIDS;
3946 logop->op_other = LINKLIST(other);
3947 logop->op_private = (U8)(1 | (flags >> 8));
3949 /* establish postfix order */
3950 logop->op_next = LINKLIST(first);
3951 first->op_next = (OP*)logop;
3952 first->op_sibling = other;
3954 o = newUNOP(OP_NULL, 0, (OP*)logop);
3961 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
3968 return newLOGOP(OP_AND, 0, first, trueop);
3970 return newLOGOP(OP_OR, 0, first, falseop);
3972 scalarboolean(first);
3973 if (first->op_type == OP_CONST) {
3974 if (first->op_private & OPpCONST_BARE &&
3975 first->op_private & OPpCONST_STRICT) {
3976 no_bareword_allowed(first);
3978 if (SvTRUE(((SVOP*)first)->op_sv)) {
3989 NewOp(1101, logop, 1, LOGOP);
3990 logop->op_type = OP_COND_EXPR;
3991 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3992 logop->op_first = first;
3993 logop->op_flags = flags | OPf_KIDS;
3994 logop->op_private = (U8)(1 | (flags >> 8));
3995 logop->op_other = LINKLIST(trueop);
3996 logop->op_next = LINKLIST(falseop);
3999 /* establish postfix order */
4000 start = LINKLIST(first);
4001 first->op_next = (OP*)logop;
4003 first->op_sibling = trueop;
4004 trueop->op_sibling = falseop;
4005 o = newUNOP(OP_NULL, 0, (OP*)logop);
4007 trueop->op_next = falseop->op_next = o;
4014 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
4022 NewOp(1101, range, 1, LOGOP);
4024 range->op_type = OP_RANGE;
4025 range->op_ppaddr = PL_ppaddr[OP_RANGE];
4026 range->op_first = left;
4027 range->op_flags = OPf_KIDS;
4028 leftstart = LINKLIST(left);
4029 range->op_other = LINKLIST(right);
4030 range->op_private = (U8)(1 | (flags >> 8));
4032 left->op_sibling = right;
4034 range->op_next = (OP*)range;
4035 flip = newUNOP(OP_FLIP, flags, (OP*)range);
4036 flop = newUNOP(OP_FLOP, 0, flip);
4037 o = newUNOP(OP_NULL, 0, flop);
4039 range->op_next = leftstart;
4041 left->op_next = flip;
4042 right->op_next = flop;
4044 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
4045 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
4046 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
4047 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
4049 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
4050 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
4053 if (!flip->op_private || !flop->op_private)
4054 linklist(o); /* blow off optimizer unless constant */
4060 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
4064 int once = block && block->op_flags & OPf_SPECIAL &&
4065 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
4068 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
4069 return block; /* do {} while 0 does once */
4070 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
4071 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
4072 expr = newUNOP(OP_DEFINED, 0,
4073 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
4074 } else if (expr->op_flags & OPf_KIDS) {
4075 OP *k1 = ((UNOP*)expr)->op_first;
4076 OP *k2 = (k1) ? k1->op_sibling : NULL;
4077 switch (expr->op_type) {
4079 if (k2 && k2->op_type == OP_READLINE
4080 && (k2->op_flags & OPf_STACKED)
4081 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
4082 expr = newUNOP(OP_DEFINED, 0, expr);
4086 if (k1->op_type == OP_READDIR
4087 || k1->op_type == OP_GLOB
4088 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
4089 || k1->op_type == OP_EACH)
4090 expr = newUNOP(OP_DEFINED, 0, expr);
4096 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
4097 o = new_logop(OP_AND, 0, &expr, &listop);
4100 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
4102 if (once && o != listop)
4103 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
4106 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
4108 o->op_flags |= flags;
4110 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
4115 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
4123 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
4124 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
4125 expr = newUNOP(OP_DEFINED, 0,
4126 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
4127 } else if (expr && (expr->op_flags & OPf_KIDS)) {
4128 OP *k1 = ((UNOP*)expr)->op_first;
4129 OP *k2 = (k1) ? k1->op_sibling : NULL;
4130 switch (expr->op_type) {
4132 if (k2 && k2->op_type == OP_READLINE
4133 && (k2->op_flags & OPf_STACKED)
4134 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
4135 expr = newUNOP(OP_DEFINED, 0, expr);
4139 if (k1->op_type == OP_READDIR
4140 || k1->op_type == OP_GLOB
4141 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
4142 || k1->op_type == OP_EACH)
4143 expr = newUNOP(OP_DEFINED, 0, expr);
4149 block = newOP(OP_NULL, 0);
4151 block = scope(block);
4155 next = LINKLIST(cont);
4158 OP *unstack = newOP(OP_UNSTACK, 0);
4161 cont = append_elem(OP_LINESEQ, cont, unstack);
4162 if ((line_t)whileline != NOLINE) {
4163 PL_copline = (line_t)whileline;
4164 cont = append_elem(OP_LINESEQ, cont,
4165 newSTATEOP(0, Nullch, Nullop));
4169 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
4170 redo = LINKLIST(listop);
4173 PL_copline = (line_t)whileline;
4175 o = new_logop(OP_AND, 0, &expr, &listop);
4176 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
4177 op_free(expr); /* oops, it's a while (0) */
4179 return Nullop; /* listop already freed by new_logop */
4182 ((LISTOP*)listop)->op_last->op_next =
4183 (o == listop ? redo : LINKLIST(o));
4189 NewOp(1101,loop,1,LOOP);
4190 loop->op_type = OP_ENTERLOOP;
4191 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
4192 loop->op_private = 0;
4193 loop->op_next = (OP*)loop;
4196 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
4198 loop->op_redoop = redo;
4199 loop->op_lastop = o;
4200 o->op_private |= loopflags;
4203 loop->op_nextop = next;
4205 loop->op_nextop = o;
4207 o->op_flags |= flags;
4208 o->op_private |= (flags >> 8);
4213 Perl_newFOROP(pTHX_ I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
4217 PADOFFSET padoff = 0;
4221 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
4222 sv->op_type = OP_RV2GV;
4223 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
4225 else if (sv->op_type == OP_PADSV) { /* private variable */
4226 padoff = sv->op_targ;
4231 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
4232 padoff = sv->op_targ;
4234 iterflags |= OPf_SPECIAL;
4239 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
4242 #ifdef USE_5005THREADS
4243 padoff = find_threadsv("_");
4244 iterflags |= OPf_SPECIAL;
4246 sv = newGVOP(OP_GV, 0, PL_defgv);
4249 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
4250 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
4251 iterflags |= OPf_STACKED;
4253 else if (expr->op_type == OP_NULL &&
4254 (expr->op_flags & OPf_KIDS) &&
4255 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
4257 /* Basically turn for($x..$y) into the same as for($x,$y), but we
4258 * set the STACKED flag to indicate that these values are to be
4259 * treated as min/max values by 'pp_iterinit'.
4261 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
4262 LOGOP* range = (LOGOP*) flip->op_first;
4263 OP* left = range->op_first;
4264 OP* right = left->op_sibling;
4267 range->op_flags &= ~OPf_KIDS;
4268 range->op_first = Nullop;
4270 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
4271 listop->op_first->op_next = range->op_next;
4272 left->op_next = range->op_other;
4273 right->op_next = (OP*)listop;
4274 listop->op_next = listop->op_first;
4277 expr = (OP*)(listop);
4279 iterflags |= OPf_STACKED;
4282 expr = mod(force_list(expr), OP_GREPSTART);
4286 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
4287 append_elem(OP_LIST, expr, scalar(sv))));
4288 assert(!loop->op_next);
4289 #ifdef PL_OP_SLAB_ALLOC
4292 NewOp(1234,tmp,1,LOOP);
4293 Copy(loop,tmp,1,LOOP);
4298 Renew(loop, 1, LOOP);
4300 loop->op_targ = padoff;
4301 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
4302 PL_copline = forline;
4303 return newSTATEOP(0, label, wop);
4307 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
4312 if (type != OP_GOTO || label->op_type == OP_CONST) {
4313 /* "last()" means "last" */
4314 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
4315 o = newOP(type, OPf_SPECIAL);
4317 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
4318 ? SvPVx(((SVOP*)label)->op_sv, n_a)
4324 if (label->op_type == OP_ENTERSUB)
4325 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
4326 o = newUNOP(type, OPf_STACKED, label);
4328 PL_hints |= HINT_BLOCK_SCOPE;
4333 Perl_cv_undef(pTHX_ CV *cv)
4336 CV *freecv = Nullcv;
4337 bool is_eval = CvEVAL(cv) && !CvGV(cv); /* is this eval"" ? */
4339 #ifdef USE_5005THREADS
4341 MUTEX_DESTROY(CvMUTEXP(cv));
4342 Safefree(CvMUTEXP(cv));
4345 #endif /* USE_5005THREADS */
4348 if (CvFILE(cv) && !CvXSUB(cv)) {
4349 /* for XSUBs CvFILE point directly to static memory; __FILE__ */
4350 Safefree(CvFILE(cv));
4355 if (!CvXSUB(cv) && CvROOT(cv)) {
4356 #ifdef USE_5005THREADS
4357 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
4358 Perl_croak(aTHX_ "Can't undef active subroutine");
4361 Perl_croak(aTHX_ "Can't undef active subroutine");
4362 #endif /* USE_5005THREADS */
4365 SAVEVPTR(PL_curpad);
4368 op_free(CvROOT(cv));
4369 CvROOT(cv) = Nullop;
4372 SvPOK_off((SV*)cv); /* forget prototype */
4374 outsidecv = CvOUTSIDE(cv);
4375 /* Since closure prototypes have the same lifetime as the containing
4376 * CV, they don't hold a refcount on the outside CV. This avoids
4377 * the refcount loop between the outer CV (which keeps a refcount to
4378 * the closure prototype in the pad entry for pp_anoncode()) and the
4379 * closure prototype, and the ensuing memory leak. --GSAR */
4380 if (!CvANON(cv) || CvCLONED(cv))
4382 CvOUTSIDE(cv) = Nullcv;
4384 SvREFCNT_dec((SV*)CvXSUBANY(cv).any_ptr);
4387 if (CvPADLIST(cv)) {
4388 /* may be during global destruction */
4389 if (SvREFCNT(CvPADLIST(cv))) {
4390 AV *padlist = CvPADLIST(cv);
4392 /* pads may be cleared out already during global destruction */
4393 if ((is_eval && !PL_dirty) || CvSPECIAL(cv)) {
4394 /* inner references to eval's cv must be fixed up */
4395 AV *comppad_name = (AV*)AvARRAY(padlist)[0];
4396 AV *comppad = (AV*)AvARRAY(padlist)[1];
4397 SV **namepad = AvARRAY(comppad_name);
4398 SV **curpad = AvARRAY(comppad);
4399 for (ix = AvFILLp(comppad_name); ix > 0; ix--) {
4400 SV *namesv = namepad[ix];
4401 if (namesv && namesv != &PL_sv_undef
4402 && *SvPVX(namesv) == '&'
4403 && ix <= AvFILLp(comppad))
4405 CV *innercv = (CV*)curpad[ix];
4406 if (innercv && SvTYPE(innercv) == SVt_PVCV
4407 && CvOUTSIDE(innercv) == cv)
4409 CvOUTSIDE(innercv) = outsidecv;
4410 if (!CvANON(innercv) || CvCLONED(innercv)) {
4411 (void)SvREFCNT_inc(outsidecv);
4420 SvREFCNT_dec(freecv);
4421 ix = AvFILLp(padlist);
4423 SV* sv = AvARRAY(padlist)[ix--];
4426 if (sv == (SV*)PL_comppad_name)
4427 PL_comppad_name = Nullav;
4428 else if (sv == (SV*)PL_comppad) {
4429 PL_comppad = Nullav;
4430 PL_curpad = Null(SV**);
4434 SvREFCNT_dec((SV*)CvPADLIST(cv));
4436 CvPADLIST(cv) = Nullav;
4439 SvREFCNT_dec(freecv);
4446 #ifdef DEBUG_CLOSURES
4448 S_cv_dump(pTHX_ CV *cv)
4451 CV *outside = CvOUTSIDE(cv);
4452 AV* padlist = CvPADLIST(cv);
4459 PerlIO_printf(Perl_debug_log,
4460 "\tCV=0x%"UVxf" (%s), OUTSIDE=0x%"UVxf" (%s)\n",
4462 (CvANON(cv) ? "ANON"
4463 : (cv == PL_main_cv) ? "MAIN"
4464 : CvUNIQUE(cv) ? "UNIQUE"
4465 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
4468 : CvANON(outside) ? "ANON"
4469 : (outside == PL_main_cv) ? "MAIN"
4470 : CvUNIQUE(outside) ? "UNIQUE"
4471 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
4476 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
4477 pad = (AV*)*av_fetch(padlist, 1, FALSE);
4478 pname = AvARRAY(pad_name);
4479 ppad = AvARRAY(pad);
4481 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
4482 if (SvPOK(pname[ix]))
4483 PerlIO_printf(Perl_debug_log,
4484 "\t%4d. 0x%"UVxf" (%s\"%s\" %"IVdf"-%"IVdf")\n",
4485 (int)ix, PTR2UV(ppad[ix]),
4486 SvFAKE(pname[ix]) ? "FAKE " : "",
4488 (IV)I_32(SvNVX(pname[ix])),
4491 #endif /* DEBUGGING */
4493 #endif /* DEBUG_CLOSURES */
4496 S_cv_clone2(pTHX_ CV *proto, CV *outside)
4500 AV* protopadlist = CvPADLIST(proto);
4501 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
4502 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
4503 SV** pname = AvARRAY(protopad_name);
4504 SV** ppad = AvARRAY(protopad);
4505 I32 fname = AvFILLp(protopad_name);
4506 I32 fpad = AvFILLp(protopad);
4510 assert(!CvUNIQUE(proto));
4514 SAVESPTR(PL_comppad_name);
4515 SAVESPTR(PL_compcv);
4517 cv = PL_compcv = (CV*)NEWSV(1104,0);
4518 sv_upgrade((SV *)cv, SvTYPE(proto));
4519 CvFLAGS(cv) = CvFLAGS(proto) & ~CVf_CLONE;
4522 #ifdef USE_5005THREADS
4523 New(666, CvMUTEXP(cv), 1, perl_mutex);
4524 MUTEX_INIT(CvMUTEXP(cv));
4526 #endif /* USE_5005THREADS */
4528 CvFILE(cv) = CvXSUB(proto) ? CvFILE(proto)
4529 : savepv(CvFILE(proto));
4531 CvFILE(cv) = CvFILE(proto);
4533 CvGV(cv) = CvGV(proto);
4534 CvSTASH(cv) = CvSTASH(proto);
4535 CvROOT(cv) = OpREFCNT_inc(CvROOT(proto));
4536 CvSTART(cv) = CvSTART(proto);
4538 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
4541 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
4543 PL_comppad_name = newAV();
4544 for (ix = fname; ix >= 0; ix--)
4545 av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix]));
4547 PL_comppad = newAV();
4549 comppadlist = newAV();
4550 AvREAL_off(comppadlist);
4551 av_store(comppadlist, 0, (SV*)PL_comppad_name);
4552 av_store(comppadlist, 1, (SV*)PL_comppad);
4553 CvPADLIST(cv) = comppadlist;
4554 av_fill(PL_comppad, AvFILLp(protopad));
4555 PL_curpad = AvARRAY(PL_comppad);
4557 av = newAV(); /* will be @_ */
4559 av_store(PL_comppad, 0, (SV*)av);
4560 AvFLAGS(av) = AVf_REIFY;
4562 for (ix = fpad; ix > 0; ix--) {
4563 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
4564 if (namesv && namesv != &PL_sv_undef) {
4565 char *name = SvPVX(namesv); /* XXX */
4566 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
4567 I32 off = pad_findlex(name, ix, SvIVX(namesv),
4568 CvOUTSIDE(cv), cxstack_ix, 0, 0);
4570 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
4572 Perl_croak(aTHX_ "panic: cv_clone: %s", name);
4574 else { /* our own lexical */
4577 /* anon code -- we'll come back for it */
4578 sv = SvREFCNT_inc(ppad[ix]);
4580 else if (*name == '@')
4582 else if (*name == '%')
4591 else if (IS_PADGV(ppad[ix]) || IS_PADCONST(ppad[ix])) {
4592 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
4595 SV* sv = NEWSV(0,0);
4601 /* Now that vars are all in place, clone nested closures. */
4603 for (ix = fpad; ix > 0; ix--) {
4604 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
4606 && namesv != &PL_sv_undef
4607 && !(SvFLAGS(namesv) & SVf_FAKE)
4608 && *SvPVX(namesv) == '&'
4609 && CvCLONE(ppad[ix]))
4611 CV *kid = cv_clone2((CV*)ppad[ix], cv);
4612 SvREFCNT_dec(ppad[ix]);
4615 PL_curpad[ix] = (SV*)kid;
4619 #ifdef DEBUG_CLOSURES
4620 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
4622 PerlIO_printf(Perl_debug_log, " from:\n");
4624 PerlIO_printf(Perl_debug_log, " to:\n");
4631 SV* const_sv = op_const_sv(CvSTART(cv), cv);
4633 /* constant sub () { $x } closing over $x - see lib/constant.pm */
4635 cv = newCONSTSUB(CvSTASH(proto), 0, const_sv);
4642 Perl_cv_clone(pTHX_ CV *proto)
4645 LOCK_CRED_MUTEX; /* XXX create separate mutex */
4646 cv = cv_clone2(proto, CvOUTSIDE(proto));
4647 UNLOCK_CRED_MUTEX; /* XXX create separate mutex */
4652 Perl_cv_ckproto(pTHX_ CV *cv, GV *gv, char *p)
4654 if (((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) && ckWARN_d(WARN_PROTOTYPE)) {
4655 SV* msg = sv_newmortal();
4659 gv_efullname3(name = sv_newmortal(), gv, Nullch);
4660 sv_setpv(msg, "Prototype mismatch:");
4662 Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, name);
4664 Perl_sv_catpvf(aTHX_ msg, " (%s)", SvPVX(cv));
4665 sv_catpv(msg, " vs ");
4667 Perl_sv_catpvf(aTHX_ msg, "(%s)", p);
4669 sv_catpv(msg, "none");
4670 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%"SVf, msg);
4674 static void const_sv_xsub(pTHX_ CV* cv);
4678 =head1 Optree Manipulation Functions
4680 =for apidoc cv_const_sv
4682 If C<cv> is a constant sub eligible for inlining. returns the constant
4683 value returned by the sub. Otherwise, returns NULL.
4685 Constant subs can be created with C<newCONSTSUB> or as described in
4686 L<perlsub/"Constant Functions">.
4691 Perl_cv_const_sv(pTHX_ CV *cv)
4693 if (!cv || !CvCONST(cv))
4695 return (SV*)CvXSUBANY(cv).any_ptr;
4699 Perl_op_const_sv(pTHX_ OP *o, CV *cv)
4706 if (o->op_type == OP_LINESEQ && cLISTOPo->op_first)
4707 o = cLISTOPo->op_first->op_sibling;
4709 for (; o; o = o->op_next) {
4710 OPCODE type = o->op_type;
4712 if (sv && o->op_next == o)
4714 if (o->op_next != o) {
4715 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
4717 if (type == OP_DBSTATE)
4720 if (type == OP_LEAVESUB || type == OP_RETURN)
4724 if (type == OP_CONST && cSVOPo->op_sv)
4726 else if ((type == OP_PADSV || type == OP_CONST) && cv) {
4727 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
4728 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
4732 /* We get here only from cv_clone2() while creating a closure.
4733 Copy the const value here instead of in cv_clone2 so that
4734 SvREADONLY_on doesn't lead to problems when leaving
4739 if (!SvREADONLY(sv) && SvREFCNT(sv) > 1)
4751 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4761 Perl_croak(aTHX_ "\"my sub\" not yet implemented");
4765 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
4767 return Perl_newATTRSUB(aTHX_ floor, o, proto, Nullop, block);