3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * "You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
13 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
14 * youngest of the Old Took's daughters); and Mr. Drogo was his second
15 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
16 * either way, as the saying is, if you follow me." --the Gaffer
19 /* This file contains the functions that create, manipulate and optimize
20 * the OP structures that hold a compiled perl program.
22 * A Perl program is compiled into a tree of OPs. Each op contains
23 * structural pointers (eg to its siblings and the next op in the
24 * execution sequence), a pointer to the function that would execute the
25 * op, plus any data specific to that op. For example, an OP_CONST op
26 * points to the pp_const() function and to an SV containing the constant
27 * value. When pp_const() is executed, its job is to push that SV onto the
30 * OPs are mainly created by the newFOO() functions, which are mainly
31 * called from the parser (in perly.y) as the code is parsed. For example
32 * the Perl code $a + $b * $c would cause the equivalent of the following
33 * to be called (oversimplifying a bit):
35 * newBINOP(OP_ADD, flags,
37 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
40 * Note that during the build of miniperl, a temporary copy of this file
41 * is made, called opmini.c.
45 Perl's compiler is essentially a 3-pass compiler with interleaved phases:
49 An execution-order pass
51 The bottom-up pass is represented by all the "newOP" routines and
52 the ck_ routines. The bottom-upness is actually driven by yacc.
53 So at the point that a ck_ routine fires, we have no idea what the
54 context is, either upward in the syntax tree, or either forward or
55 backward in the execution order. (The bottom-up parser builds that
56 part of the execution order it knows about, but if you follow the "next"
57 links around, you'll find it's actually a closed loop through the
60 Whenever the bottom-up parser gets to a node that supplies context to
61 its components, it invokes that portion of the top-down pass that applies
62 to that part of the subtree (and marks the top node as processed, so
63 if a node further up supplies context, it doesn't have to take the
64 plunge again). As a particular subcase of this, as the new node is
65 built, it takes all the closed execution loops of its subcomponents
66 and links them into a new closed loop for the higher level node. But
67 it's still not the real execution order.
69 The actual execution order is not known till we get a grammar reduction
70 to a top-level unit like a subroutine or file that will be called by
71 "name" rather than via a "next" pointer. At that point, we can call
72 into peep() to do that code's portion of the 3rd pass. It has to be
73 recursive, but it's recursive on basic blocks, not on tree nodes.
81 #define CALL_PEEP(o) CALL_FPTR(PL_peepp)(aTHX_ o)
83 #if defined(PL_OP_SLAB_ALLOC)
85 #ifndef PERL_SLAB_SIZE
86 #define PERL_SLAB_SIZE 2048
90 Perl_Slab_Alloc(pTHX_ int m, size_t sz)
93 * To make incrementing use count easy PL_OpSlab is an I32 *
94 * To make inserting the link to slab PL_OpPtr is I32 **
95 * So compute size in units of sizeof(I32 *) as that is how Pl_OpPtr increments
96 * Add an overhead for pointer to slab and round up as a number of pointers
98 sz = (sz + 2*sizeof(I32 *) -1)/sizeof(I32 *);
99 if ((PL_OpSpace -= sz) < 0) {
100 PL_OpPtr = (I32 **) PerlMemShared_malloc(PERL_SLAB_SIZE*sizeof(I32*));
104 Zero(PL_OpPtr,PERL_SLAB_SIZE,I32 **);
105 /* We reserve the 0'th I32 sized chunk as a use count */
106 PL_OpSlab = (I32 *) PL_OpPtr;
107 /* Reduce size by the use count word, and by the size we need.
108 * Latter is to mimic the '-=' in the if() above
110 PL_OpSpace = PERL_SLAB_SIZE - (sizeof(I32)+sizeof(I32 **)-1)/sizeof(I32 **) - sz;
111 /* Allocation pointer starts at the top.
112 Theory: because we build leaves before trunk allocating at end
113 means that at run time access is cache friendly upward
115 PL_OpPtr += PERL_SLAB_SIZE;
117 assert( PL_OpSpace >= 0 );
118 /* Move the allocation pointer down */
120 assert( PL_OpPtr > (I32 **) PL_OpSlab );
121 *PL_OpPtr = PL_OpSlab; /* Note which slab it belongs to */
122 (*PL_OpSlab)++; /* Increment use count of slab */
123 assert( PL_OpPtr+sz <= ((I32 **) PL_OpSlab + PERL_SLAB_SIZE) );
124 assert( *PL_OpSlab > 0 );
125 return (void *)(PL_OpPtr + 1);
129 Perl_Slab_Free(pTHX_ void *op)
131 I32 * const * const ptr = (I32 **) op;
132 I32 * const slab = ptr[-1];
133 assert( ptr-1 > (I32 **) slab );
134 assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) );
136 if (--(*slab) == 0) {
138 # define PerlMemShared PerlMem
141 PerlMemShared_free(slab);
142 if (slab == PL_OpSlab) {
149 * In the following definition, the ", (OP*)0" is just to make the compiler
150 * think the expression is of the right type: croak actually does a Siglongjmp.
152 #define CHECKOP(type,o) \
153 ((PL_op_mask && PL_op_mask[type]) \
154 ? ( op_free((OP*)o), \
155 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
157 : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o))
159 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
162 S_gv_ename(pTHX_ GV *gv)
164 SV* const tmpsv = sv_newmortal();
165 gv_efullname3(tmpsv, gv, NULL);
166 return SvPV_nolen_const(tmpsv);
170 S_no_fh_allowed(pTHX_ OP *o)
172 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
178 S_too_few_arguments(pTHX_ OP *o, const char *name)
180 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
185 S_too_many_arguments(pTHX_ OP *o, const char *name)
187 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
192 S_bad_type(pTHX_ I32 n, const char *t, const char *name, const OP *kid)
194 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
195 (int)n, name, t, OP_DESC((OP *)kid)));
199 S_no_bareword_allowed(pTHX_ const OP *o)
201 qerror(Perl_mess(aTHX_
202 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
206 /* "register" allocation */
209 Perl_allocmy(pTHX_ char *name)
212 const bool is_our = (PL_in_my == KEY_our);
214 /* complain about "my $_" etc etc */
218 (USE_UTF8_IN_NAMES && UTF8_IS_START(name[1])) ||
219 (name[1] == '_' && name[2])))
221 /* name[2] is true if strlen(name) > 2 */
222 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
223 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%s in \"my\"",
224 name[0], toCTRL(name[1]), name + 2));
226 yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
229 /* check for duplicate declaration */
230 pad_check_dup(name, is_our, (PL_curstash ? PL_curstash : PL_defstash));
232 if (PL_in_my_stash && *name != '$') {
233 yyerror(Perl_form(aTHX_
234 "Can't declare class for non-scalar %s in \"%s\"",
235 name, is_our ? "our" : "my"));
238 /* allocate a spare slot and store the name in that slot */
240 off = pad_add_name(name,
243 ? (PL_curstash ? PL_curstash : PL_defstash)
252 #ifdef USE_5005THREADS
253 /* find_threadsv is not reentrant */
255 Perl_find_threadsv(pTHX_ const char *name)
260 /* We currently only handle names of a single character */
261 p = strchr(PL_threadsv_names, *name);
264 key = p - PL_threadsv_names;
265 MUTEX_LOCK(&thr->mutex);
266 svp = av_fetch(thr->threadsv, key, FALSE);
268 MUTEX_UNLOCK(&thr->mutex);
271 av_store(thr->threadsv, key, sv);
272 thr->threadsvp = AvARRAY(thr->threadsv);
273 MUTEX_UNLOCK(&thr->mutex);
275 * Some magic variables used to be automagically initialised
276 * in gv_fetchpv. Those which are now per-thread magicals get
277 * initialised here instead.
283 sv_setpv(sv, "\034");
284 sv_magic(sv, 0, PERL_MAGIC_sv, name, 1);
289 PL_sawampersand = TRUE;
303 /* XXX %! tied to Errno.pm needs to be added here.
304 * See gv_fetchpv(). */
308 sv_magic(sv, 0, PERL_MAGIC_sv, name, 1);
310 DEBUG_S(PerlIO_printf(Perl_error_log,
311 "find_threadsv: new SV %p for $%s%c\n",
312 sv, (*name < 32) ? "^" : "",
313 (*name < 32) ? toCTRL(*name) : *name));
317 #endif /* USE_5005THREADS */
322 Perl_op_free(pTHX_ OP *o)
326 if (!o || o->op_seq == (U16)-1)
330 if (o->op_private & OPpREFCOUNTED) {
341 refcnt = OpREFCNT_dec(o);
352 if (o->op_flags & OPf_KIDS) {
353 register OP *kid, *nextkid;
354 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
355 nextkid = kid->op_sibling; /* Get before next freeing kid */
360 type = (OPCODE)o->op_targ;
362 /* COP* is not cleared by op_clear() so that we may track line
363 * numbers etc even after null() */
364 if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
372 Perl_op_clear(pTHX_ OP *o)
375 switch (o->op_type) {
376 case OP_NULL: /* Was holding old type, if any. */
377 case OP_ENTEREVAL: /* Was holding hints. */
378 #ifdef USE_5005THREADS
379 case OP_THREADSV: /* Was holding index into thr->threadsv AV. */
383 #ifdef USE_5005THREADS
385 if (!(o->op_flags & OPf_SPECIAL))
388 #endif /* USE_5005THREADS */
390 if (!(o->op_flags & OPf_REF)
391 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
397 if (! (o->op_type == OP_AELEMFAST && o->op_flags & OPf_SPECIAL)) {
398 /* not an OP_PADAV replacement */
400 if (cPADOPo->op_padix > 0) {
401 /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
402 * may still exist on the pad */
403 pad_swipe(cPADOPo->op_padix, TRUE);
404 cPADOPo->op_padix = 0;
407 SvREFCNT_dec(cSVOPo->op_sv);
408 cSVOPo->op_sv = NULL;
412 case OP_METHOD_NAMED:
414 SvREFCNT_dec(cSVOPo->op_sv);
415 cSVOPo->op_sv = NULL;
418 Even if op_clear does a pad_free for the target of the op,
419 pad_free doesn't actually remove the sv that exists in the pad;
420 instead it lives on. This results in that it could be reused as
421 a target later on when the pad was reallocated.
424 pad_swipe(o->op_targ,1);
433 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
437 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
438 SvREFCNT_dec(cSVOPo->op_sv);
439 cSVOPo->op_sv = NULL;
442 Safefree(cPVOPo->op_pv);
443 cPVOPo->op_pv = NULL;
447 op_free(cPMOPo->op_pmreplroot);
451 if (INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot)) {
452 /* No GvIN_PAD_off here, because other references may still
453 * exist on the pad */
454 pad_swipe(INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot), TRUE);
457 SvREFCNT_dec((SV*)cPMOPo->op_pmreplroot);
464 HV * const pmstash = PmopSTASH(cPMOPo);
465 if (pmstash && !SvIS_FREED(pmstash)) {
466 PMOP *pmop = HvPMROOT(pmstash);
467 PMOP *lastpmop = NULL;
469 if (cPMOPo == pmop) {
471 lastpmop->op_pmnext = pmop->op_pmnext;
473 HvPMROOT(pmstash) = pmop->op_pmnext;
477 pmop = pmop->op_pmnext;
480 PmopSTASH_free(cPMOPo);
482 cPMOPo->op_pmreplroot = NULL;
483 /* we use the "SAFE" version of the PM_ macros here
484 * since sv_clean_all might release some PMOPs
485 * after PL_regex_padav has been cleared
486 * and the clearing of PL_regex_padav needs to
487 * happen before sv_clean_all
489 ReREFCNT_dec(PM_GETRE_SAFE(cPMOPo));
490 PM_SETRE_SAFE(cPMOPo, NULL);
492 if(PL_regex_pad) { /* We could be in destruction */
493 av_push((AV*) PL_regex_pad[0],(SV*) PL_regex_pad[(cPMOPo)->op_pmoffset]);
494 SvREPADTMP_on(PL_regex_pad[(cPMOPo)->op_pmoffset]);
495 PM_SETRE(cPMOPo, (cPMOPo)->op_pmoffset);
502 if (o->op_targ > 0) {
503 pad_free(o->op_targ);
509 S_cop_free(pTHX_ COP* cop)
511 Safefree(cop->cop_label); /* FIXME: treaddead ??? */
514 if (! specialWARN(cop->cop_warnings))
515 SvREFCNT_dec(cop->cop_warnings);
516 if (! specialCopIO(cop->cop_io)) {
520 SvREFCNT_dec(cop->cop_io);
523 if (PL_curcop == cop)
528 Perl_op_null(pTHX_ OP *o)
530 if (o->op_type == OP_NULL)
533 o->op_targ = o->op_type;
534 o->op_type = OP_NULL;
535 o->op_ppaddr = PL_ppaddr[OP_NULL];
539 Perl_op_refcnt_lock(pTHX)
546 Perl_op_refcnt_unlock(pTHX)
552 /* Contextualizers */
554 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
557 Perl_linklist(pTHX_ OP *o)
564 /* establish postfix order */
565 first = cUNOPo->op_first;
568 o->op_next = LINKLIST(first);
571 if (kid->op_sibling) {
572 kid->op_next = LINKLIST(kid->op_sibling);
573 kid = kid->op_sibling;
587 Perl_scalarkids(pTHX_ OP *o)
589 if (o && o->op_flags & OPf_KIDS) {
591 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
598 S_scalarboolean(pTHX_ OP *o)
600 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
601 if (ckWARN(WARN_SYNTAX)) {
602 const line_t oldline = CopLINE(PL_curcop);
604 if (PL_copline != NOLINE)
605 CopLINE_set(PL_curcop, PL_copline);
606 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
607 CopLINE_set(PL_curcop, oldline);
614 Perl_scalar(pTHX_ OP *o)
618 /* assumes no premature commitment */
619 if (!o || PL_error_count || (o->op_flags & OPf_WANT)
620 || o->op_type == OP_RETURN)
625 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
627 switch (o->op_type) {
629 scalar(cBINOPo->op_first);
634 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
638 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
639 if (!kPMOP->op_pmreplroot)
640 deprecate_old("implicit split to @_");
648 if (o->op_flags & OPf_KIDS) {
649 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
655 kid = cLISTOPo->op_first;
657 while ((kid = kid->op_sibling)) {
663 PL_curcop = &PL_compiling;
668 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
674 PL_curcop = &PL_compiling;
677 if (ckWARN(WARN_VOID))
678 Perl_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
684 Perl_scalarvoid(pTHX_ OP *o)
687 const char* useless = NULL;
691 if (o->op_type == OP_NEXTSTATE
692 || o->op_type == OP_SETSTATE
693 || o->op_type == OP_DBSTATE
694 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
695 || o->op_targ == OP_SETSTATE
696 || o->op_targ == OP_DBSTATE)))
697 PL_curcop = (COP*)o; /* for warning below */
699 /* assumes no premature commitment */
700 want = o->op_flags & OPf_WANT;
701 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
702 || o->op_type == OP_RETURN)
707 if ((o->op_private & OPpTARGET_MY)
708 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
710 return scalar(o); /* As if inside SASSIGN */
713 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
715 switch (o->op_type) {
717 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
721 if (o->op_flags & OPf_STACKED)
725 if (o->op_private == 4)
797 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
798 useless = OP_DESC(o);
805 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
806 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
807 useless = "a variable";
812 if (cSVOPo->op_private & OPpCONST_STRICT)
813 no_bareword_allowed(o);
815 if (ckWARN(WARN_VOID)) {
816 useless = "a constant";
817 if (o->op_private & OPpCONST_ARYBASE)
819 /* don't warn on optimised away booleans, eg
820 * use constant Foo, 5; Foo || print; */
821 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
823 /* the constants 0 and 1 are permitted as they are
824 conventionally used as dummies in constructs like
825 1 while some_condition_with_side_effects; */
826 else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
828 else if (SvPOK(sv)) {
829 /* perl4's way of mixing documentation and code
830 (before the invention of POD) was based on a
831 trick to mix nroff and perl code. The trick was
832 built upon these three nroff macros being used in
833 void context. The pink camel has the details in
834 the script wrapman near page 319. */
835 const char * const maybe_macro = SvPVX_const(sv);
836 if (strnEQ(maybe_macro, "di", 2) ||
837 strnEQ(maybe_macro, "ds", 2) ||
838 strnEQ(maybe_macro, "ig", 2))
843 op_null(o); /* don't execute or even remember it */
847 o->op_type = OP_PREINC; /* pre-increment is faster */
848 o->op_ppaddr = PL_ppaddr[OP_PREINC];
852 o->op_type = OP_PREDEC; /* pre-decrement is faster */
853 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
857 o->op_type = OP_I_PREINC; /* pre-increment is faster */
858 o->op_ppaddr = PL_ppaddr[OP_I_PREINC];
862 o->op_type = OP_I_PREDEC; /* pre-decrement is faster */
863 o->op_ppaddr = PL_ppaddr[OP_I_PREDEC];
869 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
874 if (o->op_flags & OPf_STACKED)
881 if (!(o->op_flags & OPf_KIDS))
890 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
897 /* all requires must return a boolean value */
898 o->op_flags &= ~OPf_WANT;
903 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
904 if (!kPMOP->op_pmreplroot)
905 deprecate_old("implicit split to @_");
909 if (useless && ckWARN(WARN_VOID))
910 Perl_warner(aTHX_ packWARN(WARN_VOID), "Useless use of %s in void context", useless);
915 Perl_listkids(pTHX_ OP *o)
917 if (o && o->op_flags & OPf_KIDS) {
919 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
926 Perl_list(pTHX_ OP *o)
930 /* assumes no premature commitment */
931 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
932 || o->op_type == OP_RETURN)
937 if ((o->op_private & OPpTARGET_MY)
938 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
940 return o; /* As if inside SASSIGN */
943 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
945 switch (o->op_type) {
948 list(cBINOPo->op_first);
953 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
961 if (!(o->op_flags & OPf_KIDS))
963 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
964 list(cBINOPo->op_first);
965 return gen_constant_list(o);
972 kid = cLISTOPo->op_first;
974 while ((kid = kid->op_sibling)) {
980 PL_curcop = &PL_compiling;
984 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
990 PL_curcop = &PL_compiling;
993 /* all requires must return a boolean value */
994 o->op_flags &= ~OPf_WANT;
1001 Perl_scalarseq(pTHX_ OP *o)
1004 const OPCODE type = o->op_type;
1006 if (type == OP_LINESEQ || type == OP_SCOPE ||
1007 type == OP_LEAVE || type == OP_LEAVETRY)
1010 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1011 if (kid->op_sibling) {
1015 PL_curcop = &PL_compiling;
1017 o->op_flags &= ~OPf_PARENS;
1018 if (PL_hints & HINT_BLOCK_SCOPE)
1019 o->op_flags |= OPf_PARENS;
1022 o = newOP(OP_STUB, 0);
1027 S_modkids(pTHX_ OP *o, I32 type)
1029 if (o && o->op_flags & OPf_KIDS) {
1031 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1038 Perl_mod(pTHX_ OP *o, I32 type)
1042 if (!o || PL_error_count)
1045 if ((o->op_private & OPpTARGET_MY)
1046 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1051 switch (o->op_type) {
1056 if (!(o->op_private & OPpCONST_ARYBASE))
1058 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1059 CopARYBASE_set(&PL_compiling,
1060 (I32)SvIV(cSVOPx(PL_eval_start)->op_sv));
1064 SAVECOPARYBASE(&PL_compiling);
1065 CopARYBASE_set(&PL_compiling, 0);
1067 else if (type == OP_REFGEN)
1070 Perl_croak(aTHX_ "That use of $[ is unsupported");
1073 if (o->op_flags & OPf_PARENS)
1077 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1078 !(o->op_flags & OPf_STACKED)) {
1079 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1080 /* The default is to set op_private to the number of children,
1081 which for a UNOP such as RV2CV is always 1. And w're using
1082 the bit for a flag in RV2CV, so we need it clear. */
1083 o->op_private &= ~1;
1084 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1085 assert(cUNOPo->op_first->op_type == OP_NULL);
1086 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1089 else if (o->op_private & OPpENTERSUB_NOMOD)
1091 else { /* lvalue subroutine call */
1092 o->op_private |= OPpLVAL_INTRO;
1093 PL_modcount = RETURN_UNLIMITED_NUMBER;
1094 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
1095 /* Backward compatibility mode: */
1096 o->op_private |= OPpENTERSUB_INARGS;
1099 else { /* Compile-time error message: */
1100 OP *kid = cUNOPo->op_first;
1104 if (kid->op_type != OP_PUSHMARK) {
1105 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1107 "panic: unexpected lvalue entersub "
1108 "args: type/targ %ld:%"UVuf,
1109 (long)kid->op_type, (UV)kid->op_targ);
1110 kid = kLISTOP->op_first;
1112 while (kid->op_sibling)
1113 kid = kid->op_sibling;
1114 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1116 if (kid->op_type == OP_METHOD_NAMED
1117 || kid->op_type == OP_METHOD)
1121 NewOp(1101, newop, 1, UNOP);
1122 newop->op_type = OP_RV2CV;
1123 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1124 newop->op_first = NULL;
1125 newop->op_next = (OP*)newop;
1126 kid->op_sibling = (OP*)newop;
1127 newop->op_private |= OPpLVAL_INTRO;
1128 newop->op_private &= ~1;
1132 if (kid->op_type != OP_RV2CV)
1134 "panic: unexpected lvalue entersub "
1135 "entry via type/targ %ld:%"UVuf,
1136 (long)kid->op_type, (UV)kid->op_targ);
1137 kid->op_private |= OPpLVAL_INTRO;
1138 break; /* Postpone until runtime */
1142 kid = kUNOP->op_first;
1143 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1144 kid = kUNOP->op_first;
1145 if (kid->op_type == OP_NULL)
1147 "Unexpected constant lvalue entersub "
1148 "entry via type/targ %ld:%"UVuf,
1149 (long)kid->op_type, (UV)kid->op_targ);
1150 if (kid->op_type != OP_GV) {
1151 /* Restore RV2CV to check lvalueness */
1153 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1154 okid->op_next = kid->op_next;
1155 kid->op_next = okid;
1158 okid->op_next = NULL;
1159 okid->op_type = OP_RV2CV;
1161 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1162 okid->op_private |= OPpLVAL_INTRO;
1163 okid->op_private &= ~1;
1167 cv = GvCV(kGVOP_gv);
1177 /* grep, foreach, subcalls, refgen */
1178 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1180 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1181 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1183 : (o->op_type == OP_ENTERSUB
1184 ? "non-lvalue subroutine call"
1186 type ? PL_op_desc[type] : "local"));
1200 case OP_RIGHT_SHIFT:
1209 if (!(o->op_flags & OPf_STACKED))
1215 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1221 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1222 PL_modcount = RETURN_UNLIMITED_NUMBER;
1223 return o; /* Treat \(@foo) like ordinary list. */
1227 if (scalar_mod_type(o, type))
1229 ref(cUNOPo->op_first, o->op_type);
1233 if (type == OP_LEAVESUBLV)
1234 o->op_private |= OPpMAYBE_LVSUB;
1239 PL_modcount = RETURN_UNLIMITED_NUMBER;
1242 ref(cUNOPo->op_first, o->op_type);
1246 PL_hints |= HINT_BLOCK_SCOPE;
1251 /* Needed if maint gets patch 19588
1259 PL_modcount = RETURN_UNLIMITED_NUMBER;
1260 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1261 return o; /* Treat \(@foo) like ordinary list. */
1262 if (scalar_mod_type(o, type))
1264 if (type == OP_LEAVESUBLV)
1265 o->op_private |= OPpMAYBE_LVSUB;
1270 { /* XXX DAPM 2002.08.25 tmp assert test */
1271 /* XXX */ assert(av_fetch(PL_comppad_name, (o->op_targ), FALSE));
1272 /* XXX */ assert(*av_fetch(PL_comppad_name, (o->op_targ), FALSE));
1274 Perl_croak(aTHX_ "Can't localize lexical variable %s",
1275 PAD_COMPNAME_PV(o->op_targ));
1279 #ifdef USE_5005THREADS
1281 PL_modcount++; /* XXX ??? */
1283 #endif /* USE_5005THREADS */
1289 if (type != OP_SASSIGN)
1293 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1298 if (type == OP_LEAVESUBLV)
1299 o->op_private |= OPpMAYBE_LVSUB;
1301 pad_free(o->op_targ);
1302 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1303 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1304 if (o->op_flags & OPf_KIDS)
1305 mod(cBINOPo->op_first->op_sibling, type);
1310 ref(cBINOPo->op_first, o->op_type);
1311 if (type == OP_ENTERSUB &&
1312 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1313 o->op_private |= OPpLVAL_DEFER;
1314 if (type == OP_LEAVESUBLV)
1315 o->op_private |= OPpMAYBE_LVSUB;
1323 if (o->op_flags & OPf_KIDS)
1324 mod(cLISTOPo->op_last, type);
1328 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1330 else if (!(o->op_flags & OPf_KIDS))
1332 if (o->op_targ != OP_LIST) {
1333 mod(cBINOPo->op_first, type);
1338 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1343 if (type != OP_LEAVESUBLV)
1345 break; /* mod()ing was handled by ck_return() */
1348 /* [20011101.069] File test operators interpret OPf_REF to mean that
1349 their argument is a filehandle; thus \stat(".") should not set
1351 if (type == OP_REFGEN &&
1352 PL_check[o->op_type] == MEMBER_TO_FPTR(Perl_ck_ftst))
1355 if (type != OP_LEAVESUBLV)
1356 o->op_flags |= OPf_MOD;
1358 if (type == OP_AASSIGN || type == OP_SASSIGN)
1359 o->op_flags |= OPf_SPECIAL|OPf_REF;
1361 o->op_private |= OPpLVAL_INTRO;
1362 o->op_flags &= ~OPf_SPECIAL;
1363 PL_hints |= HINT_BLOCK_SCOPE;
1365 else if (type != OP_GREPSTART && type != OP_ENTERSUB
1366 && type != OP_LEAVESUBLV)
1367 o->op_flags |= OPf_REF;
1372 S_scalar_mod_type(const OP *o, I32 type)
1376 if (o->op_type == OP_RV2GV)
1400 case OP_RIGHT_SHIFT:
1419 S_is_handle_constructor(const OP *o, I32 numargs)
1421 switch (o->op_type) {
1429 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1442 Perl_refkids(pTHX_ OP *o, I32 type)
1444 if (o && o->op_flags & OPf_KIDS) {
1446 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1453 Perl_ref(pTHX_ OP *o, I32 type)
1457 if (!o || PL_error_count)
1460 switch (o->op_type) {
1462 if ((type == OP_EXISTS || type == OP_DEFINED || type == OP_LOCK) &&
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 */
1468 o->op_flags |= OPf_SPECIAL;
1469 o->op_private &= ~1;
1474 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1478 if (type == OP_DEFINED)
1479 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1480 ref(cUNOPo->op_first, o->op_type);
1483 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1484 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1485 : type == OP_RV2HV ? OPpDEREF_HV
1487 o->op_flags |= OPf_MOD;
1491 #ifdef USE_5005THREADS
1493 o->op_flags |= OPf_MOD; /* XXX ??? */
1499 o->op_flags |= OPf_REF;
1502 if (type == OP_DEFINED)
1503 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1504 ref(cUNOPo->op_first, o->op_type);
1509 o->op_flags |= OPf_REF;
1514 if (!(o->op_flags & OPf_KIDS))
1516 ref(cBINOPo->op_first, type);
1520 ref(cBINOPo->op_first, o->op_type);
1521 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1522 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1523 : type == OP_RV2HV ? OPpDEREF_HV
1525 o->op_flags |= OPf_MOD;
1533 if (!(o->op_flags & OPf_KIDS))
1535 ref(cLISTOPo->op_last, type);
1545 S_dup_attrlist(pTHX_ OP *o)
1549 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1550 * where the first kid is OP_PUSHMARK and the remaining ones
1551 * are OP_CONST. We need to push the OP_CONST values.
1553 if (o->op_type == OP_CONST)
1554 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
1556 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1558 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1559 if (o->op_type == OP_CONST)
1560 rop = append_elem(OP_LIST, rop,
1561 newSVOP(OP_CONST, o->op_flags,
1562 SvREFCNT_inc_NN(cSVOPo->op_sv)));
1569 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs, bool for_my)
1573 /* fake up C<use attributes $pkg,$rv,@attrs> */
1574 ENTER; /* need to protect against side-effects of 'use' */
1577 stashsv = newSVpv(HvNAME_get(stash), 0);
1579 stashsv = &PL_sv_no;
1581 #define ATTRSMODULE "attributes"
1582 #define ATTRSMODULE_PM "attributes.pm"
1585 /* Don't force the C<use> if we don't need it. */
1586 SV * const * const svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
1587 if (svp && *svp != &PL_sv_undef)
1588 NOOP; /* already in %INC */
1590 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
1591 newSVpvs(ATTRSMODULE), NULL);
1594 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1595 newSVpvs(ATTRSMODULE),
1597 prepend_elem(OP_LIST,
1598 newSVOP(OP_CONST, 0, stashsv),
1599 prepend_elem(OP_LIST,
1600 newSVOP(OP_CONST, 0,
1602 dup_attrlist(attrs))));
1608 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
1610 OP *pack, *imop, *arg;
1616 assert(target->op_type == OP_PADSV ||
1617 target->op_type == OP_PADHV ||
1618 target->op_type == OP_PADAV);
1620 /* Ensure that attributes.pm is loaded. */
1621 apply_attrs(stash, PAD_SV(target->op_targ), attrs, TRUE);
1623 /* Need package name for method call. */
1624 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
1626 /* Build up the real arg-list. */
1628 stashsv = newSVpv(HvNAME_get(stash), 0);
1630 stashsv = &PL_sv_no;
1631 arg = newOP(OP_PADSV, 0);
1632 arg->op_targ = target->op_targ;
1633 arg = prepend_elem(OP_LIST,
1634 newSVOP(OP_CONST, 0, stashsv),
1635 prepend_elem(OP_LIST,
1636 newUNOP(OP_REFGEN, 0,
1637 mod(arg, OP_REFGEN)),
1638 dup_attrlist(attrs)));
1640 /* Fake up a method call to import */
1641 meth = newSVpvs("import");
1642 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
1643 append_elem(OP_LIST,
1644 prepend_elem(OP_LIST, pack, list(arg)),
1645 newSVOP(OP_METHOD_NAMED, 0, meth)));
1646 imop->op_private |= OPpENTERSUB_NOMOD;
1648 /* Combine the ops. */
1649 *imopsp = append_elem(OP_LIST, *imopsp, imop);
1653 =notfor apidoc apply_attrs_string
1655 Attempts to apply a list of attributes specified by the C<attrstr> and
1656 C<len> arguments to the subroutine identified by the C<cv> argument which
1657 is expected to be associated with the package identified by the C<stashpv>
1658 argument (see L<attributes>). It gets this wrong, though, in that it
1659 does not correctly identify the boundaries of the individual attribute
1660 specifications within C<attrstr>. This is not really intended for the
1661 public API, but has to be listed here for systems such as AIX which
1662 need an explicit export list for symbols. (It's called from XS code
1663 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
1664 to respect attribute syntax properly would be welcome.
1670 Perl_apply_attrs_string(pTHX_ char *stashpv, CV *cv,
1671 char *attrstr, STRLEN len)
1676 len = strlen(attrstr);
1680 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
1682 const char * const sstr = attrstr;
1683 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
1684 attrs = append_elem(OP_LIST, attrs,
1685 newSVOP(OP_CONST, 0,
1686 newSVpvn(sstr, attrstr-sstr)));
1690 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1691 newSVpvs(ATTRSMODULE),
1692 NULL, prepend_elem(OP_LIST,
1693 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
1694 prepend_elem(OP_LIST,
1695 newSVOP(OP_CONST, 0,
1701 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
1705 if (!o || PL_error_count)
1709 if (type == OP_LIST) {
1711 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1712 my_kid(kid, attrs, imopsp);
1713 } else if (type == OP_UNDEF) {
1715 } else if (type == OP_RV2SV || /* "our" declaration */
1717 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
1718 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
1719 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
1720 OP_DESC(o), PL_in_my == KEY_our ? "our" : "my"));
1722 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
1724 PL_in_my_stash = NULL;
1725 apply_attrs(GvSTASH(gv),
1726 (type == OP_RV2SV ? GvSV(gv) :
1727 type == OP_RV2AV ? (SV*)GvAV(gv) :
1728 type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)gv),
1731 o->op_private |= OPpOUR_INTRO;
1734 else if (type != OP_PADSV &&
1737 type != OP_PUSHMARK)
1739 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
1741 PL_in_my == KEY_our ? "our" : "my"));
1744 else if (attrs && type != OP_PUSHMARK) {
1748 PL_in_my_stash = NULL;
1750 /* check for C<my Dog $spot> when deciding package */
1751 stash = PAD_COMPNAME_TYPE(o->op_targ);
1753 stash = PL_curstash;
1754 apply_attrs_my(stash, o, attrs, imopsp);
1756 o->op_flags |= OPf_MOD;
1757 o->op_private |= OPpLVAL_INTRO;
1762 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1765 int maybe_scalar = 0;
1767 /* [perl #17376]: this appears to be premature, and results in code such as
1768 C< our(%x); > executing in list mode rather than void mode */
1770 if (o->op_flags & OPf_PARENS)
1780 o = my_kid(o, attrs, &rops);
1782 if (maybe_scalar && o->op_type == OP_PADSV) {
1783 o = scalar(append_list(OP_LIST, (LISTOP*)rops, (LISTOP*)o));
1784 o->op_private |= OPpLVAL_INTRO;
1787 o = append_list(OP_LIST, (LISTOP*)o, (LISTOP*)rops);
1790 PL_in_my_stash = NULL;
1795 Perl_my(pTHX_ OP *o)
1797 return my_attrs(o, NULL);
1801 Perl_sawparens(pTHX_ OP *o)
1803 PERL_UNUSED_CONTEXT;
1805 o->op_flags |= OPf_PARENS;
1810 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
1814 const OPCODE ltype = left->op_type;
1815 const OPCODE rtype = right->op_type;
1817 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
1818 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
1820 const char * const desc
1821 = PL_op_desc[(rtype == OP_SUBST || rtype == OP_TRANS)
1822 ? (int)rtype : OP_MATCH];
1823 const char * const sample = ((ltype == OP_RV2AV || ltype == OP_PADAV)
1824 ? "@array" : "%hash");
1825 Perl_warner(aTHX_ packWARN(WARN_MISC),
1826 "Applying %s to %s will act on scalar(%s)",
1827 desc, sample, sample);
1830 if (rtype == OP_CONST &&
1831 cSVOPx(right)->op_private & OPpCONST_BARE &&
1832 cSVOPx(right)->op_private & OPpCONST_STRICT)
1834 no_bareword_allowed(right);
1837 ismatchop = rtype == OP_MATCH ||
1838 rtype == OP_SUBST ||
1840 if (!(right->op_flags & OPf_STACKED) && ismatchop) {
1843 right->op_flags |= OPf_STACKED;
1844 if (rtype != OP_MATCH &&
1845 ! (rtype == OP_TRANS &&
1846 right->op_private & OPpTRANS_IDENTICAL))
1847 newleft = mod(left, rtype);
1850 if (right->op_type == OP_TRANS)
1851 o = newBINOP(OP_NULL, OPf_STACKED, scalar(newleft), right);
1853 o = prepend_elem(rtype, scalar(newleft), right);
1855 return newUNOP(OP_NOT, 0, scalar(o));
1859 return bind_match(type, left,
1860 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1864 Perl_invert(pTHX_ OP *o)
1868 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1869 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1873 Perl_scope(pTHX_ OP *o)
1876 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
1877 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1878 o->op_type = OP_LEAVE;
1879 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
1881 else if (o->op_type == OP_LINESEQ) {
1883 o->op_type = OP_SCOPE;
1884 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
1885 kid = ((LISTOP*)o)->op_first;
1886 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
1889 /* The following deals with things like 'do {1 for 1}' */
1890 kid = kid->op_sibling;
1892 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
1897 o = newLISTOP(OP_SCOPE, 0, o, NULL);
1903 Perl_block_start(pTHX_ int full)
1905 const int retval = PL_savestack_ix;
1906 /* If there were syntax errors, don't try to start a block */
1907 if (PL_yynerrs) return retval;
1909 pad_block_start(full);
1911 PL_hints &= ~HINT_BLOCK_SCOPE;
1912 SAVESPTR(PL_compiling.cop_warnings);
1913 if (! specialWARN(PL_compiling.cop_warnings)) {
1914 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1915 SAVEFREESV(PL_compiling.cop_warnings) ;
1917 SAVESPTR(PL_compiling.cop_io);
1918 if (! specialCopIO(PL_compiling.cop_io)) {
1919 PL_compiling.cop_io = newSVsv(PL_compiling.cop_io) ;
1920 SAVEFREESV(PL_compiling.cop_io) ;
1926 Perl_block_end(pTHX_ I32 floor, OP *seq)
1928 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
1929 OP* const retval = scalarseq(seq);
1930 /* If there were syntax errors, don't try to close a block */
1931 if (PL_yynerrs) return retval;
1933 CopHINTS_set(&PL_compiling, PL_hints);
1935 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1943 #ifdef USE_5005THREADS
1944 OP *const o = newOP(OP_THREADSV, 0);
1945 o->op_targ = find_threadsv("_");
1948 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
1949 #endif /* USE_5005THREADS */
1953 Perl_newPROG(pTHX_ OP *o)
1958 PL_eval_root = newUNOP(OP_LEAVEEVAL,
1959 ((PL_in_eval & EVAL_KEEPERR)
1960 ? OPf_SPECIAL : 0), o);
1961 PL_eval_start = linklist(PL_eval_root);
1962 PL_eval_root->op_private |= OPpREFCOUNTED;
1963 OpREFCNT_set(PL_eval_root, 1);
1964 PL_eval_root->op_next = 0;
1965 CALL_PEEP(PL_eval_start);
1968 if (o->op_type == OP_STUB) {
1969 PL_comppad_name = 0;
1974 PL_main_root = scope(sawparens(scalarvoid(o)));
1975 PL_curcop = &PL_compiling;
1976 PL_main_start = LINKLIST(PL_main_root);
1977 PL_main_root->op_private |= OPpREFCOUNTED;
1978 OpREFCNT_set(PL_main_root, 1);
1979 PL_main_root->op_next = 0;
1980 CALL_PEEP(PL_main_start);
1983 /* Register with debugger */
1985 CV * const cv = get_cv("DB::postponed", FALSE);
1989 XPUSHs((SV*)CopFILEGV(&PL_compiling));
1991 call_sv((SV*)cv, G_DISCARD);
1998 Perl_localize(pTHX_ OP *o, I32 lex)
2000 if (o->op_flags & OPf_PARENS)
2001 /* [perl #17376]: this appears to be premature, and results in code such as
2002 C< our(%x); > executing in list mode rather than void mode */
2009 if ( PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ','
2010 && ckWARN(WARN_PARENTHESIS))
2012 char *s = PL_bufptr;
2015 /* some heuristics to detect a potential error */
2016 while (*s && (strchr(", \t\n", *s)))
2020 if (*s && strchr("@$%*", *s) && *++s
2021 && (isALNUM(*s) || UTF8_IS_CONTINUED(*s))) {
2024 while (*s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s)))
2026 while (*s && (strchr(", \t\n", *s)))
2032 if (sigil && (*s == ';' || *s == '=')) {
2033 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
2034 "Parentheses missing around \"%s\" list",
2035 lex ? (PL_in_my == KEY_our ? "our" : "my")
2043 o = mod(o, OP_NULL); /* a bit kludgey */
2045 PL_in_my_stash = NULL;
2050 Perl_jmaybe(pTHX_ OP *o)
2052 if (o->op_type == OP_LIST) {
2053 #ifdef USE_5005THREADS
2054 OP * const o2 = newOP(OP_THREADSV, 0);
2055 o2->op_targ = find_threadsv(";");
2058 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
2059 #endif /* USE_5005THREADS */
2060 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2065 #ifdef PERL_FLEXIBLE_EXCEPTIONS
2067 S_vcall_runops(pTHX_ va_list args)
2075 Perl_fold_constants(pTHX_ register OP *o)
2078 VOL I32 type = o->op_type;
2083 SV * const oldwarnhook = PL_warnhook;
2084 SV * const olddiehook = PL_diehook;
2087 if (PL_opargs[type] & OA_RETSCALAR)
2089 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2090 o->op_targ = pad_alloc(type, SVs_PADTMP);
2092 /* integerize op, unless it happens to be C<-foo>.
2093 * XXX should pp_i_negate() do magic string negation instead? */
2094 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2095 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2096 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2098 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2101 if (!(PL_opargs[type] & OA_FOLDCONST))
2106 /* XXX might want a ck_negate() for this */
2107 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2118 /* XXX what about the numeric ops? */
2119 if (PL_hints & HINT_LOCALE)
2124 goto nope; /* Don't try to run w/ errors */
2126 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2127 const OPCODE type = curop->op_type;
2128 if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
2130 type != OP_SCALAR &&
2132 type != OP_PUSHMARK)
2138 curop = LINKLIST(o);
2139 old_next = o->op_next;
2143 oldscope = PL_scopestack_ix;
2145 create_eval_scope(G_FAKINGEVAL);
2147 PL_warnhook = PERL_WARNHOOK_FATAL;
2150 #ifdef PERL_FLEXIBLE_EXCEPTIONS
2151 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vcall_runops));
2157 #ifndef PERL_FLEXIBLE_EXCEPTIONS
2160 sv = *(PL_stack_sp--);
2161 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
2162 pad_swipe(o->op_targ, FALSE);
2163 else if (SvTEMP(sv)) { /* grab mortal temp? */
2164 SvREFCNT_inc_simple_void(sv);
2169 /* Something tried to die. Abandon constant folding. */
2170 /* Pretend the error never happened. */
2171 sv_setpvn(ERRSV,"",0);
2172 o->op_next = old_next;
2176 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
2177 PL_warnhook = oldwarnhook;
2178 PL_diehook = olddiehook;
2179 /* XXX note that this croak may fail as we've already blown away
2180 * the stack - eg any nested evals */
2181 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
2184 PL_warnhook = oldwarnhook;
2185 PL_diehook = olddiehook;
2187 if (PL_scopestack_ix > oldscope) {
2188 delete_eval_scope();
2197 if (type == OP_RV2GV)
2198 return newGVOP(OP_GV, 0, (GV*)sv);
2199 return newSVOP(OP_CONST, 0, (SV*)sv);
2206 Perl_gen_constant_list(pTHX_ register OP *o)
2209 const I32 oldtmps_floor = PL_tmps_floor;
2213 return o; /* Don't attempt to run with errors */
2215 PL_op = curop = LINKLIST(o);
2221 assert (!(curop->op_flags & OPf_SPECIAL));
2222 assert(curop->op_type == OP_RANGE);
2224 PL_tmps_floor = oldtmps_floor;
2226 o->op_type = OP_RV2AV;
2227 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2228 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
2229 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
2230 o->op_seq = 0; /* needs to be revisited in peep() */
2231 curop = ((UNOP*)o)->op_first;
2232 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc_NN(*PL_stack_sp--));
2239 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2241 if (!o || o->op_type != OP_LIST)
2242 o = newLISTOP(OP_LIST, 0, o, NULL);
2244 o->op_flags &= ~OPf_WANT;
2246 if (!(PL_opargs[type] & OA_MARK))
2247 op_null(cLISTOPo->op_first);
2249 o->op_type = (OPCODE)type;
2250 o->op_ppaddr = PL_ppaddr[type];
2251 o->op_flags |= flags;
2253 o = CHECKOP(type, o);
2254 if (o->op_type != (unsigned)type)
2257 return fold_constants(o);
2260 /* List constructors */
2263 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2271 if (first->op_type != (unsigned)type
2272 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
2274 return newLISTOP(type, 0, first, last);
2277 if (first->op_flags & OPf_KIDS)
2278 ((LISTOP*)first)->op_last->op_sibling = last;
2280 first->op_flags |= OPf_KIDS;
2281 ((LISTOP*)first)->op_first = last;
2283 ((LISTOP*)first)->op_last = last;
2288 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2296 if (first->op_type != (unsigned)type)
2297 return prepend_elem(type, (OP*)first, (OP*)last);
2299 if (last->op_type != (unsigned)type)
2300 return append_elem(type, (OP*)first, (OP*)last);
2302 first->op_last->op_sibling = last->op_first;
2303 first->op_last = last->op_last;
2304 first->op_flags |= (last->op_flags & OPf_KIDS);
2312 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2320 if (last->op_type == (unsigned)type) {
2321 if (type == OP_LIST) { /* already a PUSHMARK there */
2322 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2323 ((LISTOP*)last)->op_first->op_sibling = first;
2324 if (!(first->op_flags & OPf_PARENS))
2325 last->op_flags &= ~OPf_PARENS;
2328 if (!(last->op_flags & OPf_KIDS)) {
2329 ((LISTOP*)last)->op_last = first;
2330 last->op_flags |= OPf_KIDS;
2332 first->op_sibling = ((LISTOP*)last)->op_first;
2333 ((LISTOP*)last)->op_first = first;
2335 last->op_flags |= OPf_KIDS;
2339 return newLISTOP(type, 0, first, last);
2345 Perl_newNULLLIST(pTHX)
2347 return newOP(OP_STUB, 0);
2351 Perl_force_list(pTHX_ OP *o)
2353 if (!o || o->op_type != OP_LIST)
2354 o = newLISTOP(OP_LIST, 0, o, NULL);
2360 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2364 NewOp(1101, listop, 1, LISTOP);
2366 listop->op_type = (OPCODE)type;
2367 listop->op_ppaddr = PL_ppaddr[type];
2370 listop->op_flags = (U8)flags;
2374 else if (!first && last)
2377 first->op_sibling = last;
2378 listop->op_first = first;
2379 listop->op_last = last;
2380 if (type == OP_LIST) {
2381 OP* const pushop = newOP(OP_PUSHMARK, 0);
2382 pushop->op_sibling = first;
2383 listop->op_first = pushop;
2384 listop->op_flags |= OPf_KIDS;
2386 listop->op_last = pushop;
2389 return CHECKOP(type, listop);
2393 Perl_newOP(pTHX_ I32 type, I32 flags)
2396 NewOp(1101, o, 1, OP);
2397 o->op_type = (OPCODE)type;
2398 o->op_ppaddr = PL_ppaddr[type];
2399 o->op_flags = (U8)flags;
2402 o->op_private = (U8)(0 | (flags >> 8));
2403 if (PL_opargs[type] & OA_RETSCALAR)
2405 if (PL_opargs[type] & OA_TARGET)
2406 o->op_targ = pad_alloc(type, SVs_PADTMP);
2407 return CHECKOP(type, o);
2411 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
2416 first = newOP(OP_STUB, 0);
2417 if (PL_opargs[type] & OA_MARK)
2418 first = force_list(first);
2420 NewOp(1101, unop, 1, UNOP);
2421 unop->op_type = (OPCODE)type;
2422 unop->op_ppaddr = PL_ppaddr[type];
2423 unop->op_first = first;
2424 unop->op_flags = (U8)(flags | OPf_KIDS);
2425 unop->op_private = (U8)(1 | (flags >> 8));
2426 unop = (UNOP*) CHECKOP(type, unop);
2430 return fold_constants((OP *) unop);
2434 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2437 NewOp(1101, binop, 1, BINOP);
2440 first = newOP(OP_NULL, 0);
2442 binop->op_type = (OPCODE)type;
2443 binop->op_ppaddr = PL_ppaddr[type];
2444 binop->op_first = first;
2445 binop->op_flags = (U8)(flags | OPf_KIDS);
2448 binop->op_private = (U8)(1 | (flags >> 8));
2451 binop->op_private = (U8)(2 | (flags >> 8));
2452 first->op_sibling = last;
2455 binop = (BINOP*)CHECKOP(type, binop);
2456 if (binop->op_next || binop->op_type != (OPCODE)type)
2459 binop->op_last = binop->op_first->op_sibling;
2461 return fold_constants((OP *)binop);
2464 static int uvcompare(const void *a, const void *b)
2465 __attribute__nonnull__(1)
2466 __attribute__nonnull__(2)
2467 __attribute__pure__;
2468 static int uvcompare(const void *a, const void *b)
2470 if (*((const UV *)a) < (*(const UV *)b))
2472 if (*((const UV *)a) > (*(const UV *)b))
2474 if (*((const UV *)a+1) < (*(const UV *)b+1))
2476 if (*((const UV *)a+1) > (*(const UV *)b+1))
2482 Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
2484 SV * const tstr = ((SVOP*)expr)->op_sv;
2485 SV * const rstr = ((SVOP*)repl)->op_sv;
2488 const U8 *t = (U8*)SvPV_const(tstr, tlen);
2489 const U8 *r = (U8*)SvPV_const(rstr, rlen);
2493 register short *tbl;
2495 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
2496 const I32 squash = o->op_private & OPpTRANS_SQUASH;
2497 I32 del = o->op_private & OPpTRANS_DELETE;
2498 PL_hints |= HINT_BLOCK_SCOPE;
2501 o->op_private |= OPpTRANS_FROM_UTF;
2504 o->op_private |= OPpTRANS_TO_UTF;
2506 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2507 SV* const listsv = newSVpvs("# comment\n");
2509 const U8* tend = t + tlen;
2510 const U8* rend = r + rlen;
2524 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2525 const I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2531 t = tsave = bytes_to_utf8((U8 *)t, &len);
2534 if (!to_utf && rlen) {
2536 r = rsave = bytes_to_utf8((U8 *)r, &len);
2540 /* There are several snags with this code on EBCDIC:
2541 1. 0xFF is a legal UTF-EBCDIC byte (there are no illegal bytes).
2542 2. scan_const() in toke.c has encoded chars in native encoding which makes
2543 ranges at least in EBCDIC 0..255 range the bottom odd.
2547 U8 tmpbuf[UTF8_MAXBYTES+1];
2550 Newx(cp, 2*tlen, UV);
2552 transv = newSVpvs("");
2554 cp[2*i] = utf8n_to_uvuni((U8 *)t, tend-t, &ulen, 0);
2556 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) {
2558 cp[2*i+1] = utf8n_to_uvuni((U8 *)t, tend-t, &ulen, 0);
2562 cp[2*i+1] = cp[2*i];
2566 qsort(cp, i, 2*sizeof(UV), uvcompare);
2567 for (j = 0; j < i; j++) {
2569 diff = val - nextmin;
2571 t = uvuni_to_utf8(tmpbuf,nextmin);
2572 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2574 U8 range_mark = UTF_TO_NATIVE(0xff);
2575 t = uvuni_to_utf8(tmpbuf, val - 1);
2576 sv_catpvn(transv, (char *)&range_mark, 1);
2577 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2584 t = uvuni_to_utf8(tmpbuf,nextmin);
2585 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2587 U8 range_mark = UTF_TO_NATIVE(0xff);
2588 sv_catpvn(transv, (char *)&range_mark, 1);
2590 t = uvuni_to_utf8_flags(tmpbuf, 0x7fffffff,
2591 UNICODE_ALLOW_SUPER);
2592 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2593 t = (const U8*)SvPVX_const(transv);
2594 tlen = SvCUR(transv);
2598 else if (!rlen && !del) {
2599 r = t; rlen = tlen; rend = tend;
2602 if ((!rlen && !del) || t == r ||
2603 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
2605 o->op_private |= OPpTRANS_IDENTICAL;
2609 while (t < tend || tfirst <= tlast) {
2610 /* see if we need more "t" chars */
2611 if (tfirst > tlast) {
2612 tfirst = (I32)utf8n_to_uvuni((U8 *)t, tend - t, &ulen, 0);
2614 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) { /* illegal utf8 val indicates range */
2616 tlast = (I32)utf8n_to_uvuni((U8 *)t, tend - t, &ulen, 0);
2623 /* now see if we need more "r" chars */
2624 if (rfirst > rlast) {
2626 rfirst = (I32)utf8n_to_uvuni((U8 *)r, rend - r, &ulen, 0);
2628 if (r < rend && NATIVE_TO_UTF(*r) == 0xff) { /* illegal utf8 val indicates range */
2630 rlast = (I32)utf8n_to_uvuni((U8 *)r, rend - r, &ulen,
2640 rfirst = rlast = 0xffffffff;
2644 /* now see which range will peter our first, if either. */
2645 tdiff = tlast - tfirst;
2646 rdiff = rlast - rfirst;
2653 if (rfirst == 0xffffffff) {
2654 diff = tdiff; /* oops, pretend rdiff is infinite */
2656 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
2657 (long)tfirst, (long)tlast);
2659 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
2663 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
2664 (long)tfirst, (long)(tfirst + diff),
2667 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
2668 (long)tfirst, (long)rfirst);
2670 if (rfirst + diff > max)
2671 max = rfirst + diff;
2673 grows = (tfirst < rfirst &&
2674 UNISKIP(tfirst) < UNISKIP(rfirst + diff));
2686 else if (max > 0xff)
2691 Safefree(cPVOPo->op_pv);
2692 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2693 SvREFCNT_dec(listsv);
2694 SvREFCNT_dec(transv);
2696 if (!del && havefinal && rlen)
2697 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5,
2698 newSVuv((UV)final), 0);
2701 o->op_private |= OPpTRANS_GROWS;
2711 tbl = (short*)cPVOPo->op_pv;
2713 Zero(tbl, 256, short);
2714 for (i = 0; i < (I32)tlen; i++)
2716 for (i = 0, j = 0; i < 256; i++) {
2718 if (j >= (I32)rlen) {
2727 if (i < 128 && r[j] >= 128)
2737 o->op_private |= OPpTRANS_IDENTICAL;
2739 else if (j >= (I32)rlen)
2742 cPVOPo->op_pv = (char*)Renew(tbl, 0x101+rlen-j, short);
2743 tbl[0x100] = (short)(rlen - j);
2744 for (i=0; i < (I32)rlen - j; i++)
2745 tbl[0x101+i] = r[j+i];
2749 if (!rlen && !del) {
2752 o->op_private |= OPpTRANS_IDENTICAL;
2754 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
2755 o->op_private |= OPpTRANS_IDENTICAL;
2757 for (i = 0; i < 256; i++)
2759 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
2760 if (j >= (I32)rlen) {
2762 if (tbl[t[i]] == -1)
2768 if (tbl[t[i]] == -1) {
2769 if (t[i] < 128 && r[j] >= 128)
2776 o->op_private |= OPpTRANS_GROWS;
2784 Perl_newPMOP(pTHX_ I32 type, I32 flags)
2788 NewOp(1101, pmop, 1, PMOP);
2789 pmop->op_type = (OPCODE)type;
2790 pmop->op_ppaddr = PL_ppaddr[type];
2791 pmop->op_flags = (U8)flags;
2792 pmop->op_private = (U8)(0 | (flags >> 8));
2794 if (PL_hints & HINT_RE_TAINT)
2795 pmop->op_pmpermflags |= PMf_RETAINT;
2796 if (PL_hints & HINT_LOCALE)
2797 pmop->op_pmpermflags |= PMf_LOCALE;
2798 pmop->op_pmflags = pmop->op_pmpermflags;
2801 if (av_len((AV*) PL_regex_pad[0]) > -1) {
2802 SV * const repointer = av_pop((AV*)PL_regex_pad[0]);
2803 pmop->op_pmoffset = SvIV(repointer);
2804 SvREPADTMP_off(repointer);
2805 sv_setiv(repointer,0);
2807 SV * const repointer = newSViv(0);
2808 av_push(PL_regex_padav, SvREFCNT_inc_simple_NN(repointer));
2809 pmop->op_pmoffset = av_len(PL_regex_padav);
2810 PL_regex_pad = AvARRAY(PL_regex_padav);
2814 /* link into pm list */
2815 if (type != OP_TRANS && PL_curstash) {
2816 pmop->op_pmnext = HvPMROOT(PL_curstash);
2817 HvPMROOT(PL_curstash) = pmop;
2818 PmopSTASH_set(pmop,PL_curstash);
2821 return CHECKOP(type, pmop);
2825 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
2829 I32 repl_has_vars = 0;
2831 if (o->op_type == OP_TRANS)
2832 return pmtrans(o, expr, repl);
2834 PL_hints |= HINT_BLOCK_SCOPE;
2837 if (expr->op_type == OP_CONST) {
2839 SV * const pat = ((SVOP*)expr)->op_sv;
2840 const char *p = SvPV_const(pat, plen);
2841 if ((o->op_flags & OPf_SPECIAL) && (*p == ' ' && p[1] == '\0')) {
2842 U32 was_readonly = SvREADONLY(pat);
2846 sv_force_normal_flags(pat, 0);
2847 assert(!SvREADONLY(pat));
2850 SvREADONLY_off(pat);
2854 sv_setpvn(pat, "\\s+", 3);
2856 SvFLAGS(pat) |= was_readonly;
2858 p = SvPV_const(pat, plen);
2859 pm->op_pmflags |= PMf_SKIPWHITE;
2862 pm->op_pmdynflags |= PMdf_UTF8;
2863 /* FIXME - can we make this function take const char * args? */
2864 PM_SETRE(pm, CALLREGCOMP(aTHX_ (char*)p, (char*)p + plen, pm));
2865 if (strEQ("\\s+", PM_GETRE(pm)->precomp))
2866 pm->op_pmflags |= PMf_WHITE;
2870 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2871 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2873 : OP_REGCMAYBE),0,expr);
2875 NewOp(1101, rcop, 1, LOGOP);
2876 rcop->op_type = OP_REGCOMP;
2877 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2878 rcop->op_first = scalar(expr);
2879 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2880 ? (OPf_SPECIAL | OPf_KIDS)
2882 rcop->op_private = 1;
2885 /* establish postfix order */
2886 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2888 rcop->op_next = expr;
2889 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2892 rcop->op_next = LINKLIST(expr);
2893 expr->op_next = (OP*)rcop;
2896 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2901 if (pm->op_pmflags & PMf_EVAL) {
2903 if (CopLINE(PL_curcop) < (line_t)PL_multi_end)
2904 CopLINE_set(PL_curcop, (line_t)PL_multi_end);
2906 #ifdef USE_5005THREADS
2907 else if (repl->op_type == OP_THREADSV
2908 && strchr("&`'123456789+",
2909 PL_threadsv_names[repl->op_targ]))
2913 #endif /* USE_5005THREADS */
2914 else if (repl->op_type == OP_CONST)
2918 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2919 if (curop->op_type == OP_SCOPE
2920 || curop->op_type == OP_LEAVE
2921 || (PL_opargs[curop->op_type] & OA_DANGEROUS)) {
2922 #ifdef USE_5005THREADS
2923 if (curop->op_type == OP_THREADSV) {
2925 if (strchr("&`'123456789+", curop->op_private))
2929 if (curop->op_type == OP_GV) {
2930 GV * const gv = cGVOPx_gv(curop);
2932 if (strchr("&`'123456789+-\016\022", *GvENAME(gv)))
2935 #endif /* USE_5005THREADS */
2936 else if (curop->op_type == OP_RV2CV)
2938 else if (curop->op_type == OP_RV2SV ||
2939 curop->op_type == OP_RV2AV ||
2940 curop->op_type == OP_RV2HV ||
2941 curop->op_type == OP_RV2GV) {
2942 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2945 else if (curop->op_type == OP_PADSV ||
2946 curop->op_type == OP_PADAV ||
2947 curop->op_type == OP_PADHV ||
2948 curop->op_type == OP_PADANY)
2952 else if (curop->op_type == OP_PUSHRE)
2953 NOOP; /* Okay here, dangerous in newASSIGNOP */
2963 || PM_GETRE(pm)->reganch & ROPT_EVAL_SEEN))) {
2964 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2965 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2966 prepend_elem(o->op_type, scalar(repl), o);
2969 if (curop == repl && !PM_GETRE(pm)) { /* Has variables. */
2970 pm->op_pmflags |= PMf_MAYBE_CONST;
2971 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2973 NewOp(1101, rcop, 1, LOGOP);
2974 rcop->op_type = OP_SUBSTCONT;
2975 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
2976 rcop->op_first = scalar(repl);
2977 rcop->op_flags |= OPf_KIDS;
2978 rcop->op_private = 1;
2981 /* establish postfix order */
2982 rcop->op_next = LINKLIST(repl);
2983 repl->op_next = (OP*)rcop;
2985 pm->op_pmreplroot = scalar((OP*)rcop);
2986 pm->op_pmreplstart = LINKLIST(rcop);
2995 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
2998 NewOp(1101, svop, 1, SVOP);
2999 svop->op_type = (OPCODE)type;
3000 svop->op_ppaddr = PL_ppaddr[type];
3002 svop->op_next = (OP*)svop;
3003 svop->op_flags = (U8)flags;
3004 if (PL_opargs[type] & OA_RETSCALAR)
3006 if (PL_opargs[type] & OA_TARGET)
3007 svop->op_targ = pad_alloc(type, SVs_PADTMP);
3008 return CHECKOP(type, svop);
3012 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
3015 NewOp(1101, padop, 1, PADOP);
3016 padop->op_type = (OPCODE)type;
3017 padop->op_ppaddr = PL_ppaddr[type];
3018 padop->op_padix = pad_alloc(type, SVs_PADTMP);
3019 SvREFCNT_dec(PAD_SVl(padop->op_padix));
3020 PAD_SETSV(padop->op_padix, sv);
3023 padop->op_next = (OP*)padop;
3024 padop->op_flags = (U8)flags;
3025 if (PL_opargs[type] & OA_RETSCALAR)
3027 if (PL_opargs[type] & OA_TARGET)
3028 padop->op_targ = pad_alloc(type, SVs_PADTMP);
3029 return CHECKOP(type, padop);
3033 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
3038 return newPADOP(type, flags, SvREFCNT_inc_simple(gv));
3040 return newSVOP(type, flags, SvREFCNT_inc_simple(gv));
3045 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
3048 NewOp(1101, pvop, 1, PVOP);
3049 pvop->op_type = (OPCODE)type;
3050 pvop->op_ppaddr = PL_ppaddr[type];
3052 pvop->op_next = (OP*)pvop;
3053 pvop->op_flags = (U8)flags;
3054 if (PL_opargs[type] & OA_RETSCALAR)
3056 if (PL_opargs[type] & OA_TARGET)
3057 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
3058 return CHECKOP(type, pvop);
3062 Perl_package(pTHX_ OP *o)
3066 save_hptr(&PL_curstash);
3067 save_item(PL_curstname);
3072 name = SvPV_const(sv, len);
3073 PL_curstash = gv_stashpvn(name,len,TRUE);
3074 sv_setpvn(PL_curstname, name, len);
3078 deprecate("\"package\" with no arguments");
3079 sv_setpv(PL_curstname,"<none>");
3080 PL_curstash = Nullhv;
3082 PL_hints |= HINT_BLOCK_SCOPE;
3083 PL_copline = NOLINE;
3088 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
3094 if (idop->op_type != OP_CONST)
3095 Perl_croak(aTHX_ "Module name must be constant");
3100 SV * const vesv = ((SVOP*)version)->op_sv;
3102 if (!arg && !SvNIOKp(vesv)) {
3109 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
3110 Perl_croak(aTHX_ "Version number must be constant number");
3112 /* Make copy of idop so we don't free it twice */
3113 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
3115 /* Fake up a method call to VERSION */
3116 meth = newSVpvs("VERSION");
3117 sv_upgrade(meth, SVt_PVIV);
3118 (void)SvIOK_on(meth);
3121 PERL_HASH(hash, SvPVX_const(meth), SvCUR(meth));
3122 SvUV_set(meth, hash);
3124 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3125 append_elem(OP_LIST,
3126 prepend_elem(OP_LIST, pack, list(version)),
3127 newSVOP(OP_METHOD_NAMED, 0, meth)));
3131 /* Fake up an import/unimport */
3132 if (arg && arg->op_type == OP_STUB)
3133 imop = arg; /* no import on explicit () */
3134 else if (SvNIOKp(((SVOP*)idop)->op_sv)) {
3135 imop = NULL; /* use 5.0; */
3137 idop->op_private |= OPpCONST_NOVER;
3142 /* Make copy of idop so we don't free it twice */
3143 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
3145 /* Fake up a method call to import/unimport */
3146 meth = aver ? newSVpvs("import") : newSVpvs("unimport");
3147 (void)SvUPGRADE(meth, SVt_PVIV);
3148 (void)SvIOK_on(meth);
3151 PERL_HASH(hash, SvPVX_const(meth), SvCUR(meth));
3152 SvUV_set(meth, hash);
3154 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3155 append_elem(OP_LIST,
3156 prepend_elem(OP_LIST, pack, list(arg)),
3157 newSVOP(OP_METHOD_NAMED, 0, meth)));
3160 /* Fake up the BEGIN {}, which does its thing immediately. */
3162 newSVOP(OP_CONST, 0, newSVpvs("BEGIN")),
3165 append_elem(OP_LINESEQ,
3166 append_elem(OP_LINESEQ,
3167 newSTATEOP(0, NULL, newUNOP(OP_REQUIRE, 0, idop)),
3168 newSTATEOP(0, NULL, veop)),
3169 newSTATEOP(0, NULL, imop) ));
3171 /* The "did you use incorrect case?" warning used to be here.
3172 * The problem is that on case-insensitive filesystems one
3173 * might get false positives for "use" (and "require"):
3174 * "use Strict" or "require CARP" will work. This causes
3175 * portability problems for the script: in case-strict
3176 * filesystems the script will stop working.
3178 * The "incorrect case" warning checked whether "use Foo"
3179 * imported "Foo" to your namespace, but that is wrong, too:
3180 * there is no requirement nor promise in the language that
3181 * a Foo.pm should or would contain anything in package "Foo".
3183 * There is very little Configure-wise that can be done, either:
3184 * the case-sensitivity of the build filesystem of Perl does not
3185 * help in guessing the case-sensitivity of the runtime environment.
3188 PL_hints |= HINT_BLOCK_SCOPE;
3189 PL_copline = NOLINE;
3191 PL_cop_seqmax++; /* Purely for B::*'s benefit */
3195 =head1 Embedding Functions
3197 =for apidoc load_module
3199 Loads the module whose name is pointed to by the string part of name.
3200 Note that the actual module name, not its filename, should be given.
3201 Eg, "Foo::Bar" instead of "Foo/Bar.pm". flags can be any of
3202 PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS
3203 (or 0 for no flags). ver, if specified, provides version semantics
3204 similar to C<use Foo::Bar VERSION>. The optional trailing SV*
3205 arguments can be used to specify arguments to the module's import()
3206 method, similar to C<use Foo::Bar VERSION LIST>.
3211 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
3214 va_start(args, ver);
3215 vload_module(flags, name, ver, &args);
3219 #ifdef PERL_IMPLICIT_CONTEXT
3221 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
3225 va_start(args, ver);
3226 vload_module(flags, name, ver, &args);
3232 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
3236 OP * const modname = newSVOP(OP_CONST, 0, name);
3237 modname->op_private |= OPpCONST_BARE;
3239 veop = newSVOP(OP_CONST, 0, ver);
3243 if (flags & PERL_LOADMOD_NOIMPORT) {
3244 imop = sawparens(newNULLLIST());
3246 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
3247 imop = va_arg(*args, OP*);
3252 sv = va_arg(*args, SV*);
3254 imop = append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
3255 sv = va_arg(*args, SV*);
3259 const line_t ocopline = PL_copline;
3260 COP * const ocurcop = PL_curcop;
3261 const int oexpect = PL_expect;
3263 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
3264 veop, modname, imop);
3265 PL_expect = oexpect;
3266 PL_copline = ocopline;
3267 PL_curcop = ocurcop;
3272 Perl_dofile(pTHX_ OP *term)
3274 return dofile2(term, 0);
3278 Perl_dofile2(pTHX_ OP *term, I32 force_builtin)
3283 if (!force_builtin) {
3284 gv = gv_fetchpvs("do", GV_NOTQUAL, SVt_PVCV);
3285 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
3286 GV * const * const gvp = (GV**)hv_fetchs(PL_globalstash, "do", FALSE);
3287 gv = gvp ? *gvp : Nullgv;
3291 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
3292 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3293 append_elem(OP_LIST, term,
3294 scalar(newUNOP(OP_RV2CV, 0,
3295 newGVOP(OP_GV, 0, gv))))));
3298 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3304 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
3306 return newBINOP(OP_LSLICE, flags,
3307 list(force_list(subscript)),
3308 list(force_list(listval)) );
3312 S_is_list_assignment(pTHX_ register const OP *o)
3320 if ((o->op_type == OP_NULL) && (o->op_flags & OPf_KIDS))
3321 o = cUNOPo->op_first;
3323 flags = o->op_flags;
3325 if (type == OP_COND_EXPR) {
3326 const I32 t = is_list_assignment(cLOGOPo->op_first->op_sibling);
3327 const I32 f = is_list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
3332 yyerror("Assignment to both a list and a scalar");
3336 if (type == OP_LIST &&
3337 (flags & OPf_WANT) == OPf_WANT_SCALAR &&
3338 o->op_private & OPpLVAL_INTRO)
3341 if (type == OP_LIST || flags & OPf_PARENS ||
3342 type == OP_RV2AV || type == OP_RV2HV ||
3343 type == OP_ASLICE || type == OP_HSLICE)
3346 if (type == OP_PADAV || type == OP_PADHV)
3349 if (type == OP_RV2SV)
3356 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
3361 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
3362 return newLOGOP(optype, 0,
3363 mod(scalar(left), optype),
3364 newUNOP(OP_SASSIGN, 0, scalar(right)));
3367 return newBINOP(optype, OPf_STACKED,
3368 mod(scalar(left), optype), scalar(right));
3372 if (is_list_assignment(left)) {
3376 /* Grandfathering $[ assignment here. Bletch.*/
3377 /* Only simple assignments like C<< ($[) = 1 >> are allowed */
3378 PL_eval_start = (left->op_type == OP_CONST) ? right : 0;
3379 left = mod(left, OP_AASSIGN);
3382 else if (left->op_type == OP_CONST) {
3383 /* Result of assignment is always 1 (or we'd be dead already) */
3384 return newSVOP(OP_CONST, 0, newSViv(1));
3386 curop = list(force_list(left));
3387 o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
3388 o->op_private = (U8)(0 | (flags >> 8));
3389 for (curop = ((LISTOP*)curop)->op_first;
3390 curop; curop = curop->op_sibling)
3392 if (curop->op_type == OP_RV2HV &&
3393 ((UNOP*)curop)->op_first->op_type != OP_GV) {
3394 o->op_private |= OPpASSIGN_HASH;
3399 /* PL_generation sorcery:
3400 * an assignment like ($a,$b) = ($c,$d) is easier than
3401 * ($a,$b) = ($c,$a), since there is no need for temporary vars.
3402 * To detect whether there are common vars, the global var
3403 * PL_generation is incremented for each assign op we compile.
3404 * Then, while compiling the assign op, we run through all the
3405 * variables on both sides of the assignment, setting a spare slot
3406 * in each of them to PL_generation. If any of them already have
3407 * that value, we know we've got commonality. We could use a
3408 * single bit marker, but then we'd have to make 2 passes, first
3409 * to clear the flag, then to test and set it. To find somewhere
3410 * to store these values, evil chicanery is done with SvCUR().
3415 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3416 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3417 if (curop->op_type == OP_GV) {
3418 GV *gv = cGVOPx_gv(curop);
3420 || (int)GvASSIGN_GENERATION(gv) == PL_generation)
3422 GvASSIGN_GENERATION_set(gv, PL_generation);
3424 else if (curop->op_type == OP_PADSV ||
3425 curop->op_type == OP_PADAV ||
3426 curop->op_type == OP_PADHV ||
3427 curop->op_type == OP_PADANY)
3429 if ((int)PAD_COMPNAME_GEN(curop->op_targ)
3432 PAD_COMPNAME_GEN_set(curop->op_targ, PL_generation);
3435 else if (curop->op_type == OP_RV2CV)
3437 else if (curop->op_type == OP_RV2SV ||
3438 curop->op_type == OP_RV2AV ||
3439 curop->op_type == OP_RV2HV ||
3440 curop->op_type == OP_RV2GV) {
3441 if (lastop->op_type != OP_GV) /* funny deref? */
3444 else if (curop->op_type == OP_PUSHRE) {
3445 if (((PMOP*)curop)->op_pmreplroot) {
3447 GV *gv = (GV*)PAD_SVl(INT2PTR(PADOFFSET,
3448 ((PMOP*)curop)->op_pmreplroot));
3450 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3453 || (int)GvASSIGN_GENERATION(gv) == PL_generation)
3455 GvASSIGN_GENERATION_set(gv, PL_generation);
3456 GvASSIGN_GENERATION_set(gv, PL_generation);
3465 o->op_private |= OPpASSIGN_COMMON;
3467 if (right && right->op_type == OP_SPLIT) {
3468 OP* tmpop = ((LISTOP*)right)->op_first;
3469 if (tmpop && (tmpop->op_type == OP_PUSHRE)) {
3470 PMOP * const pm = (PMOP*)tmpop;
3471 if (left->op_type == OP_RV2AV &&
3472 !(left->op_private & OPpLVAL_INTRO) &&
3473 !(o->op_private & OPpASSIGN_COMMON) )
3475 tmpop = ((UNOP*)left)->op_first;
3476 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
3478 pm->op_pmreplroot = INT2PTR(OP*, cPADOPx(tmpop)->op_padix);
3479 cPADOPx(tmpop)->op_padix = 0; /* steal it */
3481 pm->op_pmreplroot = (OP*)cSVOPx(tmpop)->op_sv;
3482 cSVOPx(tmpop)->op_sv = NULL; /* steal it */
3484 pm->op_pmflags |= PMf_ONCE;
3485 tmpop = cUNOPo->op_first; /* to list (nulled) */
3486 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3487 tmpop->op_sibling = NULL; /* don't free split */
3488 right->op_next = tmpop->op_next; /* fix starting loc */
3489 op_free(o); /* blow off assign */
3490 right->op_flags &= ~OPf_WANT;
3491 /* "I don't know and I don't care." */
3496 if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
3497 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3499 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3501 sv_setiv(sv, PL_modcount+1);
3509 right = newOP(OP_UNDEF, 0);
3510 if (right->op_type == OP_READLINE) {
3511 right->op_flags |= OPf_STACKED;
3512 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3515 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3516 o = newBINOP(OP_SASSIGN, flags,
3517 scalar(right), mod(scalar(left), OP_SASSIGN) );
3522 o = newSVOP(OP_CONST, 0, newSViv(CopARYBASE_get(&PL_compiling)));
3523 o->op_private |= OPpCONST_ARYBASE;
3530 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3532 const U32 seq = intro_my();
3535 NewOp(1101, cop, 1, COP);
3536 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
3537 cop->op_type = OP_DBSTATE;
3538 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3541 cop->op_type = OP_NEXTSTATE;
3542 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3544 cop->op_flags = (U8)flags;
3545 CopHINTS_set(cop, PL_hints);
3547 cop->op_private |= NATIVE_HINTS;
3549 CopHINTS_set(&PL_compiling, CopHINTS_get(cop));
3550 cop->op_next = (OP*)cop;
3553 cop->cop_label = label;
3554 PL_hints |= HINT_BLOCK_SCOPE;
3557 CopARYBASE_set(cop, CopARYBASE_get(PL_curcop));
3558 if (specialWARN(PL_curcop->cop_warnings))
3559 cop->cop_warnings = PL_curcop->cop_warnings ;
3561 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3562 if (specialCopIO(PL_curcop->cop_io))
3563 cop->cop_io = PL_curcop->cop_io;
3565 cop->cop_io = newSVsv(PL_curcop->cop_io) ;
3568 if (PL_copline == NOLINE)
3569 CopLINE_set(cop, CopLINE(PL_curcop));
3571 CopLINE_set(cop, PL_copline);
3572 PL_copline = NOLINE;
3575 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
3577 CopFILEGV_set(cop, CopFILEGV(PL_curcop));
3579 CopSTASH_set(cop, PL_curstash);
3581 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3582 AV *av = CopFILEAVx(PL_curcop);
3584 SV * const * const svp = av_fetch(av, (I32)CopLINE(cop), FALSE);
3585 if (svp && *svp != &PL_sv_undef ) {
3586 (void)SvIOK_on(*svp);
3587 SvIV_set(*svp, PTR2IV(cop));
3592 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3597 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
3599 return new_logop(type, flags, &first, &other);
3603 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
3607 OP *first = *firstp;
3608 OP * const other = *otherp;
3610 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3611 return newBINOP(type, flags, scalar(first), scalar(other));
3613 scalarboolean(first);
3614 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3615 if (first->op_type == OP_NOT
3616 && (first->op_flags & OPf_SPECIAL)
3617 && (first->op_flags & OPf_KIDS)) {
3618 if (type == OP_AND || type == OP_OR) {
3624 first = *firstp = cUNOPo->op_first;
3626 first->op_next = o->op_next;
3627 cUNOPo->op_first = NULL;
3631 if (first->op_type == OP_CONST) {
3632 if (first->op_private & OPpCONST_STRICT)
3633 no_bareword_allowed(first);
3634 else if ((first->op_private & OPpCONST_BARE) && ckWARN(WARN_BAREWORD))
3635 Perl_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional");
3636 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3639 if (other->op_type == OP_CONST)
3640 other->op_private |= OPpCONST_SHORTCIRCUIT;
3646 if (first->op_type == OP_CONST)
3647 first->op_private |= OPpCONST_SHORTCIRCUIT;
3651 else if ((first->op_flags & OPf_KIDS) && ckWARN(WARN_MISC)) {
3652 const OP * const k1 = ((UNOP*)first)->op_first;
3653 const OP * const k2 = k1->op_sibling;
3655 switch (first->op_type)
3658 if (k2 && k2->op_type == OP_READLINE
3659 && (k2->op_flags & OPf_STACKED)
3660 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3662 warnop = k2->op_type;
3667 if (k1->op_type == OP_READDIR
3668 || k1->op_type == OP_GLOB
3669 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3670 || k1->op_type == OP_EACH)
3672 warnop = ((k1->op_type == OP_NULL)
3673 ? (OPCODE)k1->op_targ : k1->op_type);
3678 const line_t oldline = CopLINE(PL_curcop);
3679 CopLINE_set(PL_curcop, PL_copline);
3680 Perl_warner(aTHX_ packWARN(WARN_MISC),
3681 "Value of %s%s can be \"0\"; test with defined()",
3683 ((warnop == OP_READLINE || warnop == OP_GLOB)
3684 ? " construct" : "() operator"));
3685 CopLINE_set(PL_curcop, oldline);
3692 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3693 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3695 NewOp(1101, logop, 1, LOGOP);
3697 logop->op_type = (OPCODE)type;
3698 logop->op_ppaddr = PL_ppaddr[type];
3699 logop->op_first = first;
3700 logop->op_flags = (U8)(flags | OPf_KIDS);
3701 logop->op_other = LINKLIST(other);
3702 logop->op_private = (U8)(1 | (flags >> 8));
3704 /* establish postfix order */
3705 logop->op_next = LINKLIST(first);
3706 first->op_next = (OP*)logop;
3707 first->op_sibling = other;
3709 CHECKOP(type,logop);
3711 o = newUNOP(OP_NULL, 0, (OP*)logop);
3718 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
3725 return newLOGOP(OP_AND, 0, first, trueop);
3727 return newLOGOP(OP_OR, 0, first, falseop);
3729 scalarboolean(first);
3730 if (first->op_type == OP_CONST) {
3731 if (first->op_private & OPpCONST_BARE &&
3732 first->op_private & OPpCONST_STRICT) {
3733 no_bareword_allowed(first);
3735 if (SvTRUE(((SVOP*)first)->op_sv)) {
3746 NewOp(1101, logop, 1, LOGOP);
3747 logop->op_type = OP_COND_EXPR;
3748 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3749 logop->op_first = first;
3750 logop->op_flags = (U8)(flags | OPf_KIDS);
3751 logop->op_private = (U8)(1 | (flags >> 8));
3752 logop->op_other = LINKLIST(trueop);
3753 logop->op_next = LINKLIST(falseop);
3755 CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
3758 /* establish postfix order */
3759 start = LINKLIST(first);
3760 first->op_next = (OP*)logop;
3762 first->op_sibling = trueop;
3763 trueop->op_sibling = falseop;
3764 o = newUNOP(OP_NULL, 0, (OP*)logop);
3766 trueop->op_next = falseop->op_next = o;
3773 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
3781 NewOp(1101, range, 1, LOGOP);
3783 range->op_type = OP_RANGE;
3784 range->op_ppaddr = PL_ppaddr[OP_RANGE];
3785 range->op_first = left;
3786 range->op_flags = OPf_KIDS;
3787 leftstart = LINKLIST(left);
3788 range->op_other = LINKLIST(right);
3789 range->op_private = (U8)(1 | (flags >> 8));
3791 left->op_sibling = right;
3793 range->op_next = (OP*)range;
3794 flip = newUNOP(OP_FLIP, flags, (OP*)range);
3795 flop = newUNOP(OP_FLOP, 0, flip);
3796 o = newUNOP(OP_NULL, 0, flop);
3798 range->op_next = leftstart;
3800 left->op_next = flip;
3801 right->op_next = flop;
3803 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3804 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
3805 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3806 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3808 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3809 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3812 if (!flip->op_private || !flop->op_private)
3813 linklist(o); /* blow off optimizer unless constant */
3819 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
3823 const bool once = block && block->op_flags & OPf_SPECIAL &&
3824 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3826 PERL_UNUSED_ARG(debuggable);
3829 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3830 return block; /* do {} while 0 does once */
3831 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3832 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3833 expr = newUNOP(OP_DEFINED, 0,
3834 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3835 } else if (expr->op_flags & OPf_KIDS) {
3836 const OP * const k1 = ((UNOP*)expr)->op_first;
3837 const OP * const k2 = k1 ? k1->op_sibling : NULL;
3838 switch (expr->op_type) {
3840 if (k2 && k2->op_type == OP_READLINE
3841 && (k2->op_flags & OPf_STACKED)
3842 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3843 expr = newUNOP(OP_DEFINED, 0, expr);
3847 if (k1 && (k1->op_type == OP_READDIR
3848 || k1->op_type == OP_GLOB
3849 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3850 || k1->op_type == OP_EACH))
3851 expr = newUNOP(OP_DEFINED, 0, expr);
3857 /* if block is null, the next append_elem() would put UNSTACK, a scalar
3858 * op, in listop. This is wrong. [perl #27024] */
3860 block = newOP(OP_NULL, 0);
3861 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3862 o = new_logop(OP_AND, 0, &expr, &listop);
3865 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3867 if (once && o != listop)
3868 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3871 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3873 o->op_flags |= flags;
3875 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3881 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop,
3882 I32 whileline, OP *expr, OP *block, OP *cont)
3884 return newWHILEOP8(flags, debuggable, loop, whileline, expr, block, cont,
3889 Perl_newWHILEOP8(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32
3890 whileline, OP *expr, OP *block, OP *cont, I32 has_my)
3898 PERL_UNUSED_ARG(debuggable);
3901 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3902 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3903 expr = newUNOP(OP_DEFINED, 0,
3904 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3905 } else if (expr->op_flags & OPf_KIDS) {
3906 const OP * const k1 = ((UNOP*)expr)->op_first;
3907 const OP * const k2 = (k1) ? k1->op_sibling : NULL;
3908 switch (expr->op_type) {
3910 if (k2 && k2->op_type == OP_READLINE
3911 && (k2->op_flags & OPf_STACKED)
3912 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3913 expr = newUNOP(OP_DEFINED, 0, expr);
3917 if (k1 && (k1->op_type == OP_READDIR
3918 || k1->op_type == OP_GLOB
3919 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3920 || k1->op_type == OP_EACH))
3921 expr = newUNOP(OP_DEFINED, 0, expr);
3928 block = newOP(OP_NULL, 0);
3929 else if (cont || has_my) {
3930 block = scope(block);
3934 next = LINKLIST(cont);
3937 OP * const unstack = newOP(OP_UNSTACK, 0);
3940 cont = append_elem(OP_LINESEQ, cont, unstack);
3944 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3946 redo = LINKLIST(listop);
3949 PL_copline = (line_t)whileline;
3951 o = new_logop(OP_AND, 0, &expr, &listop);
3952 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3953 op_free(expr); /* oops, it's a while (0) */
3955 return NULL; /* listop already freed by new_logop */
3958 ((LISTOP*)listop)->op_last->op_next =
3959 (o == listop ? redo : LINKLIST(o));
3965 NewOp(1101,loop,1,LOOP);
3966 loop->op_type = OP_ENTERLOOP;
3967 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3968 loop->op_private = 0;
3969 loop->op_next = (OP*)loop;
3972 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3974 loop->op_redoop = redo;
3975 loop->op_lastop = o;
3976 o->op_private |= loopflags;
3979 loop->op_nextop = next;
3981 loop->op_nextop = o;
3983 o->op_flags |= flags;
3984 o->op_private |= (flags >> 8);
3989 Perl_newFOROP(pTHX_ I32 flags, char *label, line_t forline, OP *sv, OP *expr, OP *block, OP *cont)
3993 PADOFFSET padoff = 0;
3998 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3999 iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */
4000 sv->op_type = OP_RV2GV;
4001 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
4003 else if (sv->op_type == OP_PADSV) { /* private variable */
4004 iterpflags = sv->op_private & OPpLVAL_INTRO; /* for my $x () */
4005 padoff = sv->op_targ;
4010 #ifdef USE_5005THREADS
4011 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
4012 padoff = sv->op_targ;
4014 iterflags |= OPf_SPECIAL;
4020 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
4023 #ifdef USE_5005THREADS
4024 padoff = find_threadsv("_");
4025 iterflags |= OPf_SPECIAL;
4027 sv = newGVOP(OP_GV, 0, PL_defgv);
4030 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
4031 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
4032 iterflags |= OPf_STACKED;
4034 else if (expr->op_type == OP_NULL &&
4035 (expr->op_flags & OPf_KIDS) &&
4036 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
4038 /* Basically turn for($x..$y) into the same as for($x,$y), but we
4039 * set the STACKED flag to indicate that these values are to be
4040 * treated as min/max values by 'pp_iterinit'.
4042 const UNOP* const flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
4043 LOGOP* const range = (LOGOP*) flip->op_first;
4044 OP* const left = range->op_first;
4045 OP* const right = left->op_sibling;
4048 range->op_flags &= ~OPf_KIDS;
4049 range->op_first = NULL;
4051 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
4052 listop->op_first->op_next = range->op_next;
4053 left->op_next = range->op_other;
4054 right->op_next = (OP*)listop;
4055 listop->op_next = listop->op_first;
4058 expr = (OP*)(listop);
4060 iterflags |= OPf_STACKED;
4063 expr = mod(force_list(expr), OP_GREPSTART);
4066 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
4067 append_elem(OP_LIST, expr, scalar(sv))));
4068 assert(!loop->op_next);
4069 /* for my $x () sets OPpLVAL_INTRO;
4070 * for our $x () sets OPpOUR_INTRO; both only used by Deparse.pm */
4071 loop->op_private = (U8)iterpflags;
4072 #ifdef PL_OP_SLAB_ALLOC
4075 NewOp(1234,tmp,1,LOOP);
4076 Copy(loop,tmp,1,LISTOP);
4081 Renew(loop, 1, LOOP);
4083 loop->op_targ = padoff;
4084 wop = newWHILEOP8(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont,
4086 PL_copline = forline;
4087 return newSTATEOP(0, label, wop);
4091 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
4095 if (type != OP_GOTO || label->op_type == OP_CONST) {
4096 /* "last()" means "last" */
4097 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
4098 o = newOP(type, OPf_SPECIAL);
4100 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
4101 ? SvPVx_nolen_const(((SVOP*)label)->op_sv)
4107 /* Check whether it's going to be a goto &function */
4108 if (label->op_type == OP_ENTERSUB
4109 && !(label->op_flags & OPf_STACKED))
4110 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
4111 o = newUNOP(type, OPf_STACKED, label);
4113 PL_hints |= HINT_BLOCK_SCOPE;
4118 =for apidoc cv_undef
4120 Clear out all the active components of a CV. This can happen either
4121 by an explicit C<undef &foo>, or by the reference count going to zero.
4122 In the former case, we keep the CvOUTSIDE pointer, so that any anonymous
4123 children can still follow the full lexical scope chain.
4129 Perl_cv_undef(pTHX_ CV *cv)
4131 #ifdef USE_5005THREADS
4133 MUTEX_DESTROY(CvMUTEXP(cv));
4134 Safefree(CvMUTEXP(cv));
4137 #endif /* USE_5005THREADS */
4140 if (CvFILE(cv) && !CvISXSUB(cv)) {
4141 /* for XSUBs CvFILE point directly to static memory; __FILE__ */
4142 Safefree(CvFILE(cv));
4147 if (!CvISXSUB(cv) && CvROOT(cv)) {
4148 #ifdef USE_5005THREADS
4149 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
4150 Perl_croak(aTHX_ "Can't undef active subroutine");
4153 Perl_croak(aTHX_ "Can't undef active subroutine");
4154 #endif /* USE_5005THREADS */
4157 PAD_SAVE_SETNULLPAD();
4159 op_free(CvROOT(cv));
4164 SvPOK_off((SV*)cv); /* forget prototype */
4169 /* remove CvOUTSIDE unless this is an undef rather than a free */
4170 if (!SvREFCNT(cv) && CvOUTSIDE(cv)) {
4171 if (!CvWEAKOUTSIDE(cv))
4172 SvREFCNT_dec(CvOUTSIDE(cv));
4173 CvOUTSIDE(cv) = Nullcv;
4176 SvREFCNT_dec((SV*)CvXSUBANY(cv).any_ptr);
4182 /* delete all flags except WEAKOUTSIDE */
4183 CvFLAGS(cv) &= CVf_WEAKOUTSIDE;
4187 Perl_cv_ckproto_len(pTHX_ const CV *cv, const GV *gv, const char *p,
4190 /* Can't just use a strcmp on the prototype, as CONSTSUBs "cheat" by
4191 relying on SvCUR, and doubling up the buffer to hold CvFILE(). */
4192 if (((!p != !SvPOK(cv)) /* One has prototype, one has not. */
4193 || (p && (len != SvCUR(cv) /* Not the same length. */
4194 || memNE(p, SvPVX_const(cv), len))))
4195 && ckWARN_d(WARN_PROTOTYPE)) {
4196 SV* const msg = sv_newmortal();
4200 gv_efullname3(name = sv_newmortal(), (GV *)gv, NULL);
4201 sv_setpv(msg, "Prototype mismatch:");
4203 Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, (void*)name);
4205 Perl_sv_catpvf(aTHX_ msg, " (%"SVf")", (void*)cv);
4207 sv_catpvs(msg, ": none");
4208 sv_catpvs(msg, " vs ");
4210 Perl_sv_catpvf(aTHX_ msg, "(%.*s)", (int) len, p);
4212 sv_catpvs(msg, "none");
4213 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%"SVf, (void*)msg);
4217 static void const_sv_xsub(pTHX_ CV* cv);
4221 =head1 Optree Manipulation Functions
4223 =for apidoc cv_const_sv
4225 If C<cv> is a constant sub eligible for inlining. returns the constant
4226 value returned by the sub. Otherwise, returns NULL.
4228 Constant subs can be created with C<newCONSTSUB> or as described in
4229 L<perlsub/"Constant Functions">.
4234 Perl_cv_const_sv(pTHX_ CV *cv)
4236 PERL_UNUSED_CONTEXT;
4239 if (!(SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM))
4241 return CvCONST(cv) ? (SV*)CvXSUBANY(cv).any_ptr : NULL;
4245 Perl_op_const_sv(pTHX_ OP *o, CV *cv)
4252 if (o->op_type == OP_LINESEQ && cLISTOPo->op_first)
4253 o = cLISTOPo->op_first->op_sibling;
4255 for (; o; o = o->op_next) {
4256 const OPCODE type = o->op_type;
4258 if (sv && o->op_next == o)
4260 if (o->op_next != o) {
4261 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
4263 if (type == OP_DBSTATE)
4266 if (type == OP_LEAVESUB || type == OP_RETURN)
4270 if (type == OP_CONST && cSVOPo->op_sv)
4272 else if ((type == OP_PADSV || type == OP_CONST) && cv) {
4273 sv = PAD_BASE_SV(CvPADLIST(cv), o->op_targ);
4277 /* We get here only from cv_clone2() while creating a closure.
4278 Copy the const value here instead of in cv_clone2 so that
4279 SvREADONLY_on doesn't lead to problems when leaving
4284 if (!SvREADONLY(sv) && SvREFCNT(sv) > 1)
4296 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4298 PERL_UNUSED_ARG(floor);
4308 Perl_croak(aTHX_ "\"my sub\" not yet implemented");
4312 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
4314 return Perl_newATTRSUB(aTHX_ floor, o, proto, NULL, block);
4318 Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4324 register CV *cv = NULL;
4326 /* If the subroutine has no body, no attributes, and no builtin attributes
4327 then it's just a sub declaration, and we may be able to get away with
4328 storing with a placeholder scalar in the symbol table, rather than a
4329 full GV and CV. If anything is present then it will take a full CV to
4331 const I32 gv_fetch_flags
4332 = (block || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS))
4333 ? GV_ADDMULTI : GV_ADDMULTI | GV_NOINIT;
4334 const char * const name = o ? SvPVx_nolen_const(cSVOPo->op_sv) : NULL;
4337 assert(proto->op_type == OP_CONST);
4338 ps = SvPVx_const(((SVOP*)proto)->op_sv, ps_len);
4343 if (!name && PERLDB_NAMEANON && CopLINE(PL_curcop)) {
4344 SV * const sv = sv_newmortal();
4345 Perl_sv_setpvf(aTHX_ sv, "%s[%s:%"IVdf"]",
4346 PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
4347 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
4348 aname = SvPVX_const(sv);
4353 /* There may be future conflict here as change 23766 is not yet merged. */
4354 gv = name ? gv_fetchsv(cSVOPo->op_sv, gv_fetch_flags, SVt_PVCV)
4355 : gv_fetchpv(aname ? aname
4356 : (PL_curstash ? "__ANON__" : "__ANON__::__ANON__"),
4357 gv_fetch_flags, SVt_PVCV);
4366 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
4367 maximum a prototype before. */
4368 if (SvTYPE(gv) > SVt_NULL) {
4369 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1)
4370 && ckWARN_d(WARN_PROTOTYPE))
4372 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "Runaway prototype");
4374 cv_ckproto_len((CV*)gv, NULL, ps, ps_len);
4377 sv_setpvn((SV*)gv, ps, ps_len);
4379 sv_setiv((SV*)gv, -1);
4380 SvREFCNT_dec(PL_compcv);
4381 cv = PL_compcv = NULL;
4382 PL_sub_generation++;
4386 cv = (!name || GvCVGEN(gv)) ? Nullcv : GvCV(gv);
4388 #ifdef GV_UNIQUE_CHECK
4389 if (cv && GvUNIQUE(gv) && SvREADONLY(cv)) {
4390 Perl_croak(aTHX_ "Can't define subroutine %s (GV is unique)", name);
4394 if (!block || !ps || *ps || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS))
4397 const_sv = op_const_sv(block, Nullcv);
4400 const bool exists = CvROOT(cv) || CvXSUB(cv);
4402 #ifdef GV_UNIQUE_CHECK
4403 if (exists && GvUNIQUE(gv)) {
4404 Perl_croak(aTHX_ "Can't redefine unique subroutine %s", name);
4408 /* if the subroutine doesn't exist and wasn't pre-declared
4409 * with a prototype, assume it will be AUTOLOADed,
4410 * skipping the prototype check
4412 if (exists || SvPOK(cv))
4413 cv_ckproto_len(cv, gv, ps, ps_len);
4414 /* already defined (or promised)? */
4415 if (exists || GvASSUMECV(gv)) {
4416 if (!block && !attrs) {
4417 if (CvFLAGS(PL_compcv)) {
4418 /* might have had built-in attrs applied */
4419 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
4421 /* just a "sub foo;" when &foo is already defined */
4422 SAVEFREESV(PL_compcv);
4426 if (ckWARN(WARN_REDEFINE)
4428 && (!const_sv || sv_cmp(cv_const_sv(cv), const_sv))))
4430 const line_t oldline = CopLINE(PL_curcop);
4431 if (PL_copline != NOLINE)
4432 CopLINE_set(PL_curcop, PL_copline);
4433 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
4434 CvCONST(cv) ? "Constant subroutine %s redefined"
4435 : "Subroutine %s redefined", name);
4436 CopLINE_set(PL_curcop, oldline);
4444 SvREFCNT_inc_simple_void_NN(const_sv);
4446 assert(!CvROOT(cv) && !CvCONST(cv));
4447 sv_setpvn((SV*)cv, "", 0); /* prototype is "" */
4448 CvXSUBANY(cv).any_ptr = const_sv;
4449 CvXSUB(cv) = const_sv_xsub;
4454 cv = newCONSTSUB(NULL, (char *)name, const_sv);
4457 SvREFCNT_dec(PL_compcv);
4459 PL_sub_generation++;
4466 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>
4467 * before we clobber PL_compcv.
4471 /* Might have had built-in attributes applied -- propagate them. */
4472 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
4473 if (CvGV(cv) && GvSTASH(CvGV(cv)))
4474 stash = GvSTASH(CvGV(cv));
4475 else if (CvSTASH(cv))
4476 stash = CvSTASH(cv);
4478 stash = PL_curstash;
4481 /* possibly about to re-define existing subr -- ignore old cv */
4482 rcv = (SV*)PL_compcv;
4483 if (name && GvSTASH(gv))
4484 stash = GvSTASH(gv);
4486 stash = PL_curstash;
4488 apply_attrs(stash, rcv, attrs, FALSE);
4490 if (cv) { /* must reuse cv if autoloaded */
4492 /* got here with just attrs -- work done, so bug out */
4493 SAVEFREESV(PL_compcv);
4496 /* transfer PL_compcv to cv */
4498 CvFLAGS(cv) = CvFLAGS(PL_compcv);
4499 if (!CvWEAKOUTSIDE(cv))
4500 SvREFCNT_dec(CvOUTSIDE(cv));
4501 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
4502 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv);
4503 CvOUTSIDE(PL_compcv) = 0;
4504 CvPADLIST(cv) = CvPADLIST(PL_compcv);
4505 CvPADLIST(PL_compcv) = 0;
4506 /* inner references to PL_compcv must be fixed up ... */
4507 pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv);
4508 /* ... before we throw it away */
4509 SvREFCNT_dec(PL_compcv);
4510 if (PERLDB_INTER)/* Advice debugger on the new sub. */
4511 ++PL_sub_generation;
4518 PL_sub_generation++;
4522 CvFILE_set_from_cop(cv, PL_curcop);
4523 CvSTASH(cv) = PL_curstash;
4524 #ifdef USE_5005THREADS
4526 if (!CvMUTEXP(cv)) {
4527 New(666, CvMUTEXP(cv), 1, perl_mutex);
4528 MUTEX_INIT(CvMUTEXP(cv));
4530 #endif /* USE_5005THREADS */
4533 sv_setpvn((SV*)cv, ps, ps_len);
4535 if (PL_error_count) {
4539 const char *s = strrchr(name, ':');
4541 if (strEQ(s, "BEGIN")) {
4542 const char not_safe[] =
4543 "BEGIN not safe after errors--compilation aborted";
4544 if (PL_in_eval & EVAL_KEEPERR)
4545 Perl_croak(aTHX_ not_safe);
4547 /* force display of errors found but not reported */
4548 sv_catpv(ERRSV, not_safe);
4549 Perl_croak(aTHX_ "%"SVf, (void*)ERRSV);
4558 CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0,
4559 mod(scalarseq(block), OP_LEAVESUBLV));
4562 /* This makes sub {}; work as expected. */
4563 if (block->op_type == OP_STUB) {
4565 block = newSTATEOP(0, NULL, 0);
4567 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4569 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4570 OpREFCNT_set(CvROOT(cv), 1);
4571 CvSTART(cv) = LINKLIST(CvROOT(cv));
4572 CvROOT(cv)->op_next = 0;
4573 CALL_PEEP(CvSTART(cv));
4575 /* now that optimizer has done its work, adjust pad values */
4577 pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
4580 assert(!CvCONST(cv));
4581 if (ps && !*ps && op_const_sv(block, cv))
4585 if (name || aname) {
4587 const char * const tname = (name ? name : aname);
4589 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4590 SV * const sv = newSV(0);
4591 SV * const tmpstr = sv_newmortal();
4592 GV * const db_postponed = gv_fetchpvs("DB::postponed",
4593 GV_ADDMULTI, SVt_PVHV);
4596 Perl_sv_setpvf(aTHX_ sv, "%s:%ld-%ld",
4598 (long)PL_subline, (long)CopLINE(PL_curcop));
4599 gv_efullname3(tmpstr, gv, NULL);
4600 hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr), SvCUR(tmpstr), sv, 0);
4601 hv = GvHVn(db_postponed);
4602 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvCUR(tmpstr))) {
4603 CV * const pcv = GvCV(db_postponed);
4609 call_sv((SV*)pcv, G_DISCARD);
4614 if ((s = strrchr(tname,':')))
4619 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4622 if (strEQ(s, "BEGIN")) {
4623 const I32 oldscope = PL_scopestack_ix;
4625 SAVECOPFILE(&PL_compiling);
4626 SAVECOPLINE(&PL_compiling);
4629 PL_beginav = newAV();
4630 DEBUG_x( dump_sub(gv) );
4631 av_push(PL_beginav, (SV*)cv);
4632 GvCV(gv) = 0; /* cv has been hijacked */
4633 call_list(oldscope, PL_beginav);
4635 PL_curcop = &PL_compiling;
4636 CopHINTS_set(&PL_compiling, PL_hints);
4639 else if (strEQ(s, "END") && !PL_error_count) {
4642 DEBUG_x( dump_sub(gv) );
4643 av_unshift(PL_endav, 1);
4644 av_store(PL_endav, 0, (SV*)cv);
4645 GvCV(gv) = 0; /* cv has been hijacked */
4647 else if (strEQ(s, "CHECK") && !PL_error_count) {
4649 PL_checkav = newAV();
4650 DEBUG_x( dump_sub(gv) );
4651 if (PL_main_start && ckWARN(WARN_VOID))
4652 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run CHECK block");
4653 av_unshift(PL_checkav, 1);
4654 av_store(PL_checkav, 0, (SV*)cv);
4655 GvCV(gv) = 0; /* cv has been hijacked */
4657 else if (strEQ(s, "INIT") && !PL_error_count) {
4659 PL_initav = newAV();
4660 DEBUG_x( dump_sub(gv) );
4661 if (PL_main_start && ckWARN(WARN_VOID))
4662 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run INIT block");
4663 av_push(PL_initav, (SV*)cv);
4664 GvCV(gv) = 0; /* cv has been hijacked */
4669 PL_copline = NOLINE;
4674 /* XXX unsafe for 5005 threads if eval_owner isn't held */
4676 =for apidoc newCONSTSUB
4678 Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is
4679 eligible for inlining at compile-time.
4685 Perl_newCONSTSUB(pTHX_ HV *stash, char *name, SV *sv)
4689 const char *const temp_p = CopFILE(PL_curcop);
4690 const STRLEN len = temp_p ? strlen(temp_p) : 0;
4692 SV *const temp_sv = CopFILESV(PL_curcop);
4694 const char *const temp_p = temp_sv ? SvPV_const(temp_sv, len) : NULL;
4696 char *const file = savepvn(temp_p, temp_p ? len : 0);
4700 SAVECOPLINE(PL_curcop);
4701 CopLINE_set(PL_curcop, PL_copline);
4704 PL_hints &= ~HINT_BLOCK_SCOPE;
4707 SAVESPTR(PL_curstash);
4708 SAVECOPSTASH(PL_curcop);
4709 PL_curstash = stash;
4710 CopSTASH_set(PL_curcop,stash);
4713 /* file becomes the CvFILE. For an XS, it's supposed to be static storage,
4714 and so doesn't get free()d. (It's expected to be from the C pre-
4715 processor __FILE__ directive). But we need a dynamically allocated one,
4716 and we need it to get freed. */
4717 cv = newXS_flags(name, const_sv_xsub, file, "", XS_DYNAMIC_FILENAME);
4718 CvXSUBANY(cv).any_ptr = sv;
4724 CopSTASH_free(PL_curcop);
4732 Perl_newXS_flags(pTHX_ const char *name, XSUBADDR_t subaddr,
4733 const char *const filename, const char *const proto,
4736 CV *cv = newXS((char*)name, subaddr, (char*)filename);
4738 if (flags & XS_DYNAMIC_FILENAME) {
4739 /* We need to "make arrangements" (ie cheat) to ensure that the
4740 filename lasts as long as the PVCV we just created, but also doesn't
4742 STRLEN filename_len = strlen(filename);
4743 STRLEN proto_and_file_len = filename_len;
4744 char *proto_and_file;
4748 proto_len = strlen(proto);
4749 proto_and_file_len += proto_len;
4751 Newx(proto_and_file, proto_and_file_len + 1, char);
4752 Copy(proto, proto_and_file, proto_len, char);
4753 Copy(filename, proto_and_file + proto_len, filename_len + 1, char);
4756 proto_and_file = savepvn(filename, filename_len);
4759 /* This gets free()d. :-) */
4760 sv_usepvn_flags((SV*)cv, proto_and_file, proto_and_file_len,
4761 SV_HAS_TRAILING_NUL);
4763 /* This gives us the correct prototype, rather than one with the
4764 file name appended. */
4765 SvCUR_set(cv, proto_len);
4769 CvFILE(cv) = proto_and_file + proto_len;
4771 sv_setpv((SV *)cv, proto);
4777 =for apidoc U||newXS
4779 Used by C<xsubpp> to hook up XSUBs as Perl subs. I<filename> needs to be
4780 static storage, as it is used directly as CvFILE(), without a copy being made.
4786 Perl_newXS(pTHX_ char *name, XSUBADDR_t subaddr, char *filename)
4788 GV * const gv = gv_fetchpv(name ? name :
4789 (PL_curstash ? "__ANON__" : "__ANON__::__ANON__"),
4790 GV_ADDMULTI, SVt_PVCV);
4793 if ((cv = (name ? GvCV(gv) : Nullcv))) {
4795 /* just a cached method */
4799 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4800 /* already defined (or promised) */
4801 /* XXX It's possible for this HvNAME_get to return null, and get passed into strEQ */
4802 if (ckWARN(WARN_REDEFINE)) {
4803 GV * const gvcv = CvGV(cv);
4805 HV * const stash = GvSTASH(gvcv);
4807 const char *redefined_name = HvNAME_get(stash);
4808 if ( strEQ(redefined_name,"autouse") ) {
4809 const line_t oldline = CopLINE(PL_curcop);
4810 if (PL_copline != NOLINE)
4811 CopLINE_set(PL_curcop, PL_copline);
4812 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
4813 CvCONST(cv) ? "Constant subroutine %s redefined"
4814 : "Subroutine %s redefined"
4816 CopLINE_set(PL_curcop, oldline);
4826 if (cv) /* must reuse cv if autoloaded */
4830 sv_upgrade((SV *)cv, SVt_PVCV);
4834 PL_sub_generation++;
4838 #ifdef USE_5005THREADS
4839 New(666, CvMUTEXP(cv), 1, perl_mutex);
4840 MUTEX_INIT(CvMUTEXP(cv));
4842 #endif /* USE_5005THREADS */
4843 (void)gv_fetchfile(filename);
4844 CvFILE(cv) = (char *)filename; /* NOTE: not copied, as it is expected to be
4845 an external constant string */
4846 CvXSUB(cv) = subaddr;
4849 const char *s = strrchr(name,':');
4855 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4858 if (strEQ(s, "BEGIN")) {
4860 PL_beginav = newAV();
4861 av_push(PL_beginav, (SV*)cv);
4862 GvCV(gv) = 0; /* cv has been hijacked */
4864 else if (strEQ(s, "END")) {
4867 av_unshift(PL_endav, 1);
4868 av_store(PL_endav, 0, (SV*)cv);
4869 GvCV(gv) = 0; /* cv has been hijacked */
4871 else if (strEQ(s, "CHECK")) {
4873 PL_checkav = newAV();
4874 if (PL_main_start && ckWARN(WARN_VOID))
4875 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run CHECK block");
4876 av_unshift(PL_checkav, 1);
4877 av_store(PL_checkav, 0, (SV*)cv);
4878 GvCV(gv) = 0; /* cv has been hijacked */
4880 else if (strEQ(s, "INIT")) {
4882 PL_initav = newAV();
4883 if (PL_main_start && ckWARN(WARN_VOID))
4884 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run INIT block");
4885 av_push(PL_initav, (SV*)cv);
4886 GvCV(gv) = 0; /* cv has been hijacked */
4897 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
4902 ? gv_fetchsv(cSVOPo->op_sv, TRUE, SVt_PVFM)
4903 : gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVFM);
4905 #ifdef GV_UNIQUE_CHECK
4907 Perl_croak(aTHX_ "Bad symbol for form (GV is unique)");
4911 if ((cv = GvFORM(gv))) {
4912 if (ckWARN(WARN_REDEFINE)) {
4913 const line_t oldline = CopLINE(PL_curcop);
4914 if (PL_copline != NOLINE)
4915 CopLINE_set(PL_curcop, PL_copline);
4916 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
4917 o ? "Format %"SVf" redefined"
4918 : "Format STDOUT redefined", (void*)cSVOPo->op_sv);
4919 CopLINE_set(PL_curcop, oldline);
4926 CvFILE_set_from_cop(cv, PL_curcop);
4929 pad_tidy(padtidy_FORMAT);
4930 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4931 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4932 OpREFCNT_set(CvROOT(cv), 1);
4933 CvSTART(cv) = LINKLIST(CvROOT(cv));
4934 CvROOT(cv)->op_next = 0;
4935 CALL_PEEP(CvSTART(cv));
4937 PL_copline = NOLINE;
4942 Perl_newANONLIST(pTHX_ OP *o)
4944 return convert(OP_ANONLIST, OPf_SPECIAL, o);
4948 Perl_newANONHASH(pTHX_ OP *o)
4950 return convert(OP_ANONHASH, OPf_SPECIAL, o);
4954 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
4956 return newANONATTRSUB(floor, proto, NULL, block);
4960 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
4962 return newUNOP(OP_REFGEN, 0,
4963 newSVOP(OP_ANONCODE, 0,
4964 (SV*)newATTRSUB(floor, 0, proto, attrs, block)));
4968 Perl_oopsAV(pTHX_ OP *o)
4970 switch (o->op_type) {
4972 o->op_type = OP_PADAV;
4973 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4974 return ref(o, OP_RV2AV);
4977 o->op_type = OP_RV2AV;
4978 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4983 if (ckWARN_d(WARN_INTERNAL))
4984 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV");
4991 Perl_oopsHV(pTHX_ OP *o)
4993 switch (o->op_type) {
4996 o->op_type = OP_PADHV;
4997 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4998 return ref(o, OP_RV2HV);
5002 o->op_type = OP_RV2HV;
5003 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
5008 if (ckWARN_d(WARN_INTERNAL))
5009 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV");
5016 Perl_newAVREF(pTHX_ OP *o)
5018 if (o->op_type == OP_PADANY) {
5019 o->op_type = OP_PADAV;
5020 o->op_ppaddr = PL_ppaddr[OP_PADAV];
5023 else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)
5024 && ckWARN(WARN_DEPRECATED)) {
5025 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
5026 "Using an array as a reference is deprecated");
5028 return newUNOP(OP_RV2AV, 0, scalar(o));
5032 Perl_newGVREF(pTHX_ I32 type, OP *o)
5034 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
5035 return newUNOP(OP_NULL, 0, o);
5036 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
5040 Perl_newHVREF(pTHX_ OP *o)
5042 if (o->op_type == OP_PADANY) {
5043 o->op_type = OP_PADHV;
5044 o->op_ppaddr = PL_ppaddr[OP_PADHV];
5047 else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)
5048 && ckWARN(WARN_DEPRECATED)) {
5049 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
5050 "Using a hash as a reference is deprecated");
5052 return newUNOP(OP_RV2HV, 0, scalar(o));
5056 Perl_newCVREF(pTHX_ I32 flags, OP *o)
5058 return newUNOP(OP_RV2CV, flags, scalar(o));
5062 Perl_newSVREF(pTHX_ OP *o)
5064 if (o->op_type == OP_PADANY) {
5065 o->op_type = OP_PADSV;
5066 o->op_ppaddr = PL_ppaddr[OP_PADSV];
5069 #ifdef USE_5005THREADS
5070 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
5071 o->op_flags |= OPpDONE_SVREF;
5075 return newUNOP(OP_RV2SV, 0, scalar(o));
5078 /* Check routines. See the comments at the top of this file for details
5079 * on when these are called */
5082 Perl_ck_anoncode(pTHX_ OP *o)
5084 cSVOPo->op_targ = pad_add_anon(cSVOPo->op_sv, o->op_type);
5085 cSVOPo->op_sv = NULL;
5090 Perl_ck_bitop(pTHX_ OP *o)
5092 #define OP_IS_NUMCOMPARE(op) \
5093 ((op) == OP_LT || (op) == OP_I_LT || \
5094 (op) == OP_GT || (op) == OP_I_GT || \
5095 (op) == OP_LE || (op) == OP_I_LE || \
5096 (op) == OP_GE || (op) == OP_I_GE || \
5097 (op) == OP_EQ || (op) == OP_I_EQ || \
5098 (op) == OP_NE || (op) == OP_I_NE || \
5099 (op) == OP_NCMP || (op) == OP_I_NCMP)
5100 o->op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
5101 if (!(o->op_flags & OPf_STACKED) /* Not an assignment */
5102 && (o->op_type == OP_BIT_OR
5103 || o->op_type == OP_BIT_AND
5104 || o->op_type == OP_BIT_XOR))
5106 const OP * const left = cBINOPo->op_first;
5107 const OP * const right = left->op_sibling;
5108 if ((OP_IS_NUMCOMPARE(left->op_type) &&
5109 (left->op_flags & OPf_PARENS) == 0) ||
5110 (OP_IS_NUMCOMPARE(right->op_type) &&
5111 (right->op_flags & OPf_PARENS) == 0))
5112 if (ckWARN(WARN_PRECEDENCE))
5113 Perl_warner(aTHX_ packWARN(WARN_PRECEDENCE),
5114 "Possible precedence problem on bitwise %c operator",
5115 o->op_type == OP_BIT_OR ? '|'
5116 : o->op_type == OP_BIT_AND ? '&' : '^'
5123 Perl_ck_concat(pTHX_ OP *o)
5125 const OP * const kid = cUNOPo->op_first;
5126 PERL_UNUSED_CONTEXT;
5127 if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) &&
5128 !(kUNOP->op_first->op_flags & OPf_MOD))
5129 o->op_flags |= OPf_STACKED;
5134 Perl_ck_spair(pTHX_ OP *o)
5136 if (o->op_flags & OPf_KIDS) {
5139 const OPCODE type = o->op_type;
5140 o = modkids(ck_fun(o), type);
5141 kid = cUNOPo->op_first;
5142 newop = kUNOP->op_first->op_sibling;
5144 const OPCODE type = newop->op_type;
5145 if (newop->op_sibling || !(PL_opargs[type] & OA_RETSCALAR) ||
5146 type == OP_PADAV || type == OP_PADHV ||
5147 type == OP_RV2AV || type == OP_RV2HV)
5150 op_free(kUNOP->op_first);
5151 kUNOP->op_first = newop;
5153 o->op_ppaddr = PL_ppaddr[++o->op_type];
5158 Perl_ck_delete(pTHX_ OP *o)
5162 if (o->op_flags & OPf_KIDS) {
5163 OP * const kid = cUNOPo->op_first;
5164 switch (kid->op_type) {
5166 o->op_flags |= OPf_SPECIAL;
5169 o->op_private |= OPpSLICE;
5172 o->op_flags |= OPf_SPECIAL;
5177 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element or slice",
5186 Perl_ck_die(pTHX_ OP *o)
5189 if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH;
5195 Perl_ck_eof(pTHX_ OP *o)
5197 const I32 type = o->op_type;
5199 if (o->op_flags & OPf_KIDS) {
5200 if (cLISTOPo->op_first->op_type == OP_STUB) {
5202 o = newUNOP(type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv));
5210 Perl_ck_eval(pTHX_ OP *o)
5212 PL_hints |= HINT_BLOCK_SCOPE;
5213 if (o->op_flags & OPf_KIDS) {
5214 SVOP * const kid = (SVOP*)cUNOPo->op_first;
5217 o->op_flags &= ~OPf_KIDS;
5220 else if (kid->op_type == OP_LINESEQ || kid->op_type == OP_STUB) {
5223 cUNOPo->op_first = 0;
5226 NewOp(1101, enter, 1, LOGOP);
5227 enter->op_type = OP_ENTERTRY;
5228 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
5229 enter->op_private = 0;
5231 /* establish postfix order */
5232 enter->op_next = (OP*)enter;
5234 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
5235 o->op_type = OP_LEAVETRY;
5236 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
5237 enter->op_other = o;
5245 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
5247 o->op_targ = (PADOFFSET)PL_hints;
5252 Perl_ck_exit(pTHX_ OP *o)
5255 HV * const table = GvHV(PL_hintgv);
5257 SV * const * const svp = hv_fetchs(table, "vmsish_exit", FALSE);
5258 if (svp && *svp && SvTRUE(*svp))
5259 o->op_private |= OPpEXIT_VMSISH;
5261 if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH;
5267 Perl_ck_exec(pTHX_ OP *o)
5269 if (o->op_flags & OPf_STACKED) {
5272 kid = cUNOPo->op_first->op_sibling;
5273 if (kid->op_type == OP_RV2GV)
5282 Perl_ck_exists(pTHX_ OP *o)
5285 if (o->op_flags & OPf_KIDS) {
5286 OP * const kid = cUNOPo->op_first;
5287 if (kid->op_type == OP_ENTERSUB) {
5288 (void) ref(kid, o->op_type);
5289 if (kid->op_type != OP_RV2CV && !PL_error_count)
5290 Perl_croak(aTHX_ "%s argument is not a subroutine name",
5292 o->op_private |= OPpEXISTS_SUB;
5294 else if (kid->op_type == OP_AELEM)
5295 o->op_flags |= OPf_SPECIAL;
5296 else if (kid->op_type != OP_HELEM)
5297 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element",
5305 Perl_ck_rvconst(pTHX_ register OP *o)
5307 SVOP * const kid = (SVOP*)cUNOPo->op_first;
5309 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5310 if (o->op_type == OP_RV2CV)
5311 o->op_private &= ~1;
5313 if (kid->op_type == OP_CONST) {
5316 SV * const kidsv = kid->op_sv;
5318 /* Is it a constant from cv_const_sv()? */
5319 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
5320 SV * const rsv = SvRV(kidsv);
5321 const svtype type = SvTYPE(rsv);
5322 const char *badtype = NULL;
5324 switch (o->op_type) {
5326 if (type > SVt_PVMG)
5327 badtype = "a SCALAR";
5330 if (type != SVt_PVAV)
5331 badtype = "an ARRAY";
5334 if (type != SVt_PVHV) {
5335 if (type == SVt_PVAV) { /* pseudohash? */
5336 SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
5337 if (ksv && SvROK(*ksv)
5338 && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
5347 if (type != SVt_PVCV)
5352 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
5355 if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
5356 const char *badthing;
5357 switch (o->op_type) {
5359 badthing = "a SCALAR";
5362 badthing = "an ARRAY";
5365 badthing = "a HASH";
5373 "Can't use bareword (\"%"SVf"\") as %s ref while \"strict refs\" in use",
5374 (void*)kidsv, badthing);
5377 * This is a little tricky. We only want to add the symbol if we
5378 * didn't add it in the lexer. Otherwise we get duplicate strict
5379 * warnings. But if we didn't add it in the lexer, we must at
5380 * least pretend like we wanted to add it even if it existed before,
5381 * or we get possible typo warnings. OPpCONST_ENTERED says
5382 * whether the lexer already added THIS instance of this symbol.
5384 iscv = (o->op_type == OP_RV2CV) * 2;
5386 gv = gv_fetchsv(kidsv,
5387 iscv | !(kid->op_private & OPpCONST_ENTERED),
5390 : o->op_type == OP_RV2SV
5392 : o->op_type == OP_RV2AV
5394 : o->op_type == OP_RV2HV
5397 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
5399 kid->op_type = OP_GV;
5400 SvREFCNT_dec(kid->op_sv);
5402 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
5403 kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
5404 SvREFCNT_dec(PAD_SVl(kPADOP->op_padix));
5406 PAD_SETSV(kPADOP->op_padix, (SV*) SvREFCNT_inc_simple_NN(gv));
5408 kid->op_sv = SvREFCNT_inc_simple_NN(gv);
5410 kid->op_private = 0;
5411 kid->op_ppaddr = PL_ppaddr[OP_GV];
5418 Perl_ck_ftst(pTHX_ OP *o)
5420 const I32 type = o->op_type;
5422 if (o->op_flags & OPf_REF) {
5425 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
5426 SVOP * const kid = (SVOP*)cUNOPo->op_first;
5427 const OPCODE kidtype = kid->op_type;
5429 if (kidtype == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5430 OP * const newop = newGVOP(type, OPf_REF,
5431 gv_fetchsv(kid->op_sv, GV_ADD, SVt_PVIO));
5436 if ((PL_hints & HINT_FILETEST_ACCESS) &&
5437 OP_IS_FILETEST_ACCESS(o))
5438 o->op_private |= OPpFT_ACCESS;
5443 if (type == OP_FTTTY)
5444 o = newGVOP(type, OPf_REF, PL_stdingv);
5446 o = newUNOP(type, 0, newDEFSVOP());
5452 Perl_ck_fun(pTHX_ OP *o)
5454 const int type = o->op_type;
5455 register I32 oa = PL_opargs[type] >> OASHIFT;
5457 if (o->op_flags & OPf_STACKED) {
5458 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
5461 return no_fh_allowed(o);
5464 if (o->op_flags & OPf_KIDS) {
5465 OP **tokid = &cLISTOPo->op_first;
5466 register OP *kid = cLISTOPo->op_first;
5470 if (kid->op_type == OP_PUSHMARK ||
5471 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
5473 tokid = &kid->op_sibling;
5474 kid = kid->op_sibling;
5476 if (!kid && PL_opargs[type] & OA_DEFGV)
5477 *tokid = kid = newDEFSVOP();
5481 sibl = kid->op_sibling;
5484 /* list seen where single (scalar) arg expected? */
5485 if (numargs == 1 && !(oa >> 4)
5486 && kid->op_type == OP_LIST && type != OP_SCALAR)
5488 return too_many_arguments(o,PL_op_desc[type]);
5501 if ((type == OP_PUSH || type == OP_UNSHIFT)
5502 && !kid->op_sibling && ckWARN(WARN_SYNTAX))
5503 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
5504 "Useless use of %s with no values",
5507 if (kid->op_type == OP_CONST &&
5508 (kid->op_private & OPpCONST_BARE))
5510 OP * const newop = newAVREF(newGVOP(OP_GV, 0,
5511 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVAV) ));
5512 if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX))
5513 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5514 "Array @%"SVf" missing the @ in argument %"IVdf" of %s()",
5515 (void*)((SVOP*)kid)->op_sv, (IV)numargs, PL_op_desc[type]);
5518 kid->op_sibling = sibl;
5521 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
5522 bad_type(numargs, "array", PL_op_desc[type], kid);
5526 if (kid->op_type == OP_CONST &&
5527 (kid->op_private & OPpCONST_BARE))
5529 OP * const newop = newHVREF(newGVOP(OP_GV, 0,
5530 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVHV) ));
5531 if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX))
5532 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5533 "Hash %%%"SVf" missing the %% in argument %"IVdf" of %s()",
5534 (void*)((SVOP*)kid)->op_sv, (IV)numargs, PL_op_desc[type]);
5537 kid->op_sibling = sibl;
5540 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
5541 bad_type(numargs, "hash", PL_op_desc[type], kid);
5546 OP * const newop = newUNOP(OP_NULL, 0, kid);
5547 kid->op_sibling = 0;
5549 newop->op_next = newop;
5551 kid->op_sibling = sibl;
5556 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
5557 if (kid->op_type == OP_CONST &&
5558 (kid->op_private & OPpCONST_BARE))
5560 OP * const newop = newGVOP(OP_GV, 0,
5561 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVIO));
5562 if (!(o->op_private & 1) && /* if not unop */
5563 kid == cLISTOPo->op_last)
5564 cLISTOPo->op_last = newop;
5568 else if (kid->op_type == OP_READLINE) {
5569 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
5570 bad_type(numargs, "HANDLE", OP_DESC(o), kid);
5573 I32 flags = OPf_SPECIAL;
5577 /* is this op a FH constructor? */
5578 if (is_handle_constructor(o,numargs)) {
5579 const char *name = NULL;
5583 /* Set a flag to tell rv2gv to vivify
5584 * need to "prove" flag does not mean something
5585 * else already - NI-S 1999/05/07
5588 if (kid->op_type == OP_PADSV) {
5589 /*XXX DAPM 2002.08.25 tmp assert test */
5590 /*XXX*/ assert(av_fetch(PL_comppad_name, (kid->op_targ), FALSE));
5591 /*XXX*/ assert(*av_fetch(PL_comppad_name, (kid->op_targ), FALSE));
5593 name = PAD_COMPNAME_PV(kid->op_targ);
5594 /* SvCUR of a pad namesv can't be trusted
5595 * (see PL_generation), so calc its length
5601 else if (kid->op_type == OP_RV2SV
5602 && kUNOP->op_first->op_type == OP_GV)
5604 GV * const gv = cGVOPx_gv(kUNOP->op_first);
5606 len = GvNAMELEN(gv);
5608 else if (kid->op_type == OP_AELEM
5609 || kid->op_type == OP_HELEM)
5612 OP *op = ((BINOP*)kid)->op_first;
5616 const char * const a =
5617 kid->op_type == OP_AELEM ?
5619 if (((op->op_type == OP_RV2AV) ||
5620 (op->op_type == OP_RV2HV)) &&
5621 (firstop = ((UNOP*)op)->op_first) &&
5622 (firstop->op_type == OP_GV)) {
5623 /* packagevar $a[] or $h{} */
5624 GV * const gv = cGVOPx_gv(firstop);
5632 else if (op->op_type == OP_PADAV
5633 || op->op_type == OP_PADHV) {
5634 /* lexicalvar $a[] or $h{} */
5635 const char * const padname =
5636 PAD_COMPNAME_PV(op->op_targ);
5645 name = SvPV_const(tmpstr, len);
5650 name = "__ANONIO__";
5657 targ = pad_alloc(OP_RV2GV, SVs_PADTMP);
5658 namesv = PAD_SVl(targ);
5659 (void)SvUPGRADE(namesv, SVt_PV);
5661 sv_setpvn(namesv, "$", 1);
5662 sv_catpvn(namesv, name, len);
5665 kid->op_sibling = 0;
5666 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
5667 kid->op_targ = targ;
5668 kid->op_private |= priv;
5670 kid->op_sibling = sibl;
5676 mod(scalar(kid), type);
5680 tokid = &kid->op_sibling;
5681 kid = kid->op_sibling;
5683 o->op_private |= numargs;
5685 return too_many_arguments(o,OP_DESC(o));
5688 else if (PL_opargs[type] & OA_DEFGV) {
5690 return newUNOP(type, 0, newDEFSVOP());
5694 while (oa & OA_OPTIONAL)
5696 if (oa && oa != OA_LIST)
5697 return too_few_arguments(o,OP_DESC(o));
5703 Perl_ck_glob(pTHX_ OP *o)
5708 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
5709 append_elem(OP_GLOB, o, newDEFSVOP());
5711 if (!((gv = gv_fetchpvs("glob", GV_NOTQUAL, SVt_PVCV))
5712 && GvCVu(gv) && GvIMPORTED_CV(gv)))
5714 gv = gv_fetchpvs("CORE::GLOBAL::glob", 0, SVt_PVCV);
5717 #if !defined(PERL_EXTERNAL_GLOB)
5718 /* XXX this can be tightened up and made more failsafe. */
5719 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
5722 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
5723 newSVpvs("File::Glob"), NULL, NULL, NULL);
5724 gv = gv_fetchpvs("CORE::GLOBAL::glob", 0, SVt_PVCV);
5725 glob_gv = gv_fetchpvs("File::Glob::csh_glob", 0, SVt_PVCV);
5726 GvCV(gv) = GvCV(glob_gv);
5727 SvREFCNT_inc_void((SV*)GvCV(gv));
5728 GvIMPORTED_CV_on(gv);
5731 #endif /* PERL_EXTERNAL_GLOB */
5733 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
5734 append_elem(OP_GLOB, o,
5735 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
5736 o->op_type = OP_LIST;
5737 o->op_ppaddr = PL_ppaddr[OP_LIST];
5738 cLISTOPo->op_first->op_type = OP_PUSHMARK;
5739 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
5740 cLISTOPo->op_first->op_targ = 0;
5741 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
5742 append_elem(OP_LIST, o,
5743 scalar(newUNOP(OP_RV2CV, 0,
5744 newGVOP(OP_GV, 0, gv)))));
5745 o = newUNOP(OP_NULL, 0, ck_subr(o));
5746 o->op_targ = OP_GLOB; /* hint at what it used to be */
5749 gv = newGVgen("main");
5751 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
5757 Perl_ck_grep(pTHX_ OP *o)
5761 const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
5763 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
5764 /* don't allocate gwop here, as we may leak it if PL_error_count > 0 */
5766 if (o->op_flags & OPf_STACKED) {
5769 kid = cLISTOPo->op_first->op_sibling;
5770 if (!cUNOPx(kid)->op_next)
5771 Perl_croak(aTHX_ "panic: ck_grep");
5772 for (k = cUNOPx(kid)->op_first; k; k = k->op_next) {
5775 NewOp(1101, gwop, 1, LOGOP);
5776 kid->op_next = (OP*)gwop;
5777 o->op_flags &= ~OPf_STACKED;
5779 kid = cLISTOPo->op_first->op_sibling;
5780 if (type == OP_MAPWHILE)
5787 kid = cLISTOPo->op_first->op_sibling;
5788 if (kid->op_type != OP_NULL)
5789 Perl_croak(aTHX_ "panic: ck_grep");
5790 kid = kUNOP->op_first;
5793 NewOp(1101, gwop, 1, LOGOP);
5794 gwop->op_type = type;
5795 gwop->op_ppaddr = PL_ppaddr[type];
5796 gwop->op_first = listkids(o);
5797 gwop->op_flags |= OPf_KIDS;
5798 gwop->op_private = 1;
5799 gwop->op_other = LINKLIST(kid);
5800 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
5801 kid->op_next = (OP*)gwop;
5803 kid = cLISTOPo->op_first->op_sibling;
5804 if (!kid || !kid->op_sibling)
5805 return too_few_arguments(o,OP_DESC(o));
5806 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
5807 mod(kid, OP_GREPSTART);
5813 Perl_ck_index(pTHX_ OP *o)
5815 if (o->op_flags & OPf_KIDS) {
5816 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5818 kid = kid->op_sibling; /* get past "big" */
5819 if (kid && kid->op_type == OP_CONST)
5820 fbm_compile(((SVOP*)kid)->op_sv, 0);
5826 Perl_ck_lengthconst(pTHX_ OP *o)
5828 /* XXX length optimization goes here */
5833 Perl_ck_lfun(pTHX_ OP *o)
5835 const OPCODE type = o->op_type;
5836 return modkids(ck_fun(o), type);
5840 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
5842 if ((o->op_flags & OPf_KIDS) && ckWARN2(WARN_DEPRECATED, WARN_SYNTAX)) {
5843 switch (cUNOPo->op_first->op_type) {
5845 /* This is needed for
5846 if (defined %stash::)
5847 to work. Do not break Tk.
5849 break; /* Globals via GV can be undef */
5851 case OP_AASSIGN: /* Is this a good idea? */
5852 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5853 "defined(@array) is deprecated");
5854 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5855 "\t(Maybe you should just omit the defined()?)\n");
5858 /* This is needed for
5859 if (defined %stash::)
5860 to work. Do not break Tk.
5862 break; /* Globals via GV can be undef */
5864 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5865 "defined(%%hash) is deprecated");
5866 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5867 "\t(Maybe you should just omit the defined()?)\n");
5878 Perl_ck_rfun(pTHX_ OP *o)
5880 const OPCODE type = o->op_type;
5881 return refkids(ck_fun(o), type);
5885 Perl_ck_listiob(pTHX_ OP *o)
5889 kid = cLISTOPo->op_first;
5892 kid = cLISTOPo->op_first;
5894 if (kid->op_type == OP_PUSHMARK)
5895 kid = kid->op_sibling;
5896 if (kid && o->op_flags & OPf_STACKED)
5897 kid = kid->op_sibling;
5898 else if (kid && !kid->op_sibling) { /* print HANDLE; */
5899 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
5900 o->op_flags |= OPf_STACKED; /* make it a filehandle */
5901 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
5902 cLISTOPo->op_first->op_sibling = kid;
5903 cLISTOPo->op_last = kid;
5904 kid = kid->op_sibling;
5909 append_elem(o->op_type, o, newDEFSVOP());
5915 Perl_ck_sassign(pTHX_ OP *o)
5917 OP * const kid = cLISTOPo->op_first;
5918 /* has a disposable target? */
5919 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
5920 && !(kid->op_flags & OPf_STACKED)
5921 /* Cannot steal the second time! */
5922 && !(kid->op_private & OPpTARGET_MY))
5924 OP * const kkid = kid->op_sibling;
5926 /* Can just relocate the target. */
5927 if (kkid && kkid->op_type == OP_PADSV
5928 && !(kkid->op_private & OPpLVAL_INTRO))
5930 kid->op_targ = kkid->op_targ;
5932 /* Now we do not need PADSV and SASSIGN. */
5933 kid->op_sibling = o->op_sibling; /* NULL */
5934 cLISTOPo->op_first = NULL;
5937 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
5945 Perl_ck_match(pTHX_ OP *o)
5947 o->op_private |= OPpRUNTIME;
5952 Perl_ck_method(pTHX_ OP *o)
5954 OP * const kid = cUNOPo->op_first;
5955 if (kid->op_type == OP_CONST) {
5956 SV* sv = kSVOP->op_sv;
5957 const char * const method = SvPVX_const(sv);
5958 if (!(strchr(method, ':') || strchr(method, '\''))) {
5960 if (!SvREADONLY(sv) || !SvFAKE(sv)) {
5961 sv = newSVpvn_share(method, SvCUR(sv), 0);
5964 kSVOP->op_sv = NULL;
5966 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
5975 Perl_ck_null(pTHX_ OP *o)
5977 PERL_UNUSED_CONTEXT;
5982 Perl_ck_open(pTHX_ OP *o)
5984 HV * const table = GvHV(PL_hintgv);
5986 SV **svp = hv_fetchs(table, "open_IN", FALSE);
5988 const I32 mode = mode_from_discipline(*svp);
5989 if (mode & O_BINARY)
5990 o->op_private |= OPpOPEN_IN_RAW;
5991 else if (mode & O_TEXT)
5992 o->op_private |= OPpOPEN_IN_CRLF;
5995 svp = hv_fetchs(table, "open_OUT", FALSE);
5997 const I32 mode = mode_from_discipline(*svp);
5998 if (mode & O_BINARY)
5999 o->op_private |= OPpOPEN_OUT_RAW;
6000 else if (mode & O_TEXT)
6001 o->op_private |= OPpOPEN_OUT_CRLF;
6004 if (o->op_type == OP_BACKTICK)
6007 /* In case of three-arg dup open remove strictness
6008 * from the last arg if it is a bareword. */
6009 OP * const first = cLISTOPx(o)->op_first; /* The pushmark. */
6010 OP * const last = cLISTOPx(o)->op_last; /* The bareword. */
6014 if ((last->op_type == OP_CONST) && /* The bareword. */
6015 (last->op_private & OPpCONST_BARE) &&
6016 (last->op_private & OPpCONST_STRICT) &&
6017 (oa = first->op_sibling) && /* The fh. */
6018 (oa = oa->op_sibling) && /* The mode. */
6019 (oa->op_type == OP_CONST) &&
6020 SvPOK(((SVOP*)oa)->op_sv) &&
6021 (mode = SvPVX_const(((SVOP*)oa)->op_sv)) &&
6022 mode[0] == '>' && mode[1] == '&' && /* A dup open. */
6023 (last == oa->op_sibling)) /* The bareword. */
6024 last->op_private &= ~OPpCONST_STRICT;
6030 Perl_ck_repeat(pTHX_ OP *o)
6032 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
6033 o->op_private |= OPpREPEAT_DOLIST;
6034 cBINOPo->op_first = force_list(cBINOPo->op_first);
6042 Perl_ck_require(pTHX_ OP *o)
6046 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
6047 SVOP * const kid = (SVOP*)cUNOPo->op_first;
6049 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
6050 SV * const sv = kid->op_sv;
6051 U32 was_readonly = SvREADONLY(sv);
6056 sv_force_normal_flags(sv, 0);
6057 assert(!SvREADONLY(sv));
6064 for (s = SvPVX(sv); *s; s++) {
6065 if (*s == ':' && s[1] == ':') {
6066 const STRLEN len = strlen(s+2)+1;
6068 Move(s+2, s+1, len, char);
6069 SvCUR_set(sv, SvCUR(sv) - 1);
6072 sv_catpvs(sv, ".pm");
6073 SvFLAGS(sv) |= was_readonly;
6077 if (!(o->op_flags & OPf_SPECIAL)) { /* Wasn't written as CORE::require */
6078 /* handle override, if any */
6079 gv = gv_fetchpvs("require", GV_NOTQUAL, SVt_PVCV);
6080 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
6081 GV * const * const gvp = (GV**)hv_fetchs(PL_globalstash, "require", FALSE);
6082 gv = gvp ? *gvp : Nullgv;
6086 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
6087 OP * const kid = cUNOPo->op_first;
6088 cUNOPo->op_first = 0;
6090 return ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
6091 append_elem(OP_LIST, kid,
6092 scalar(newUNOP(OP_RV2CV, 0,
6101 Perl_ck_return(pTHX_ OP *o)
6103 if (CvLVALUE(PL_compcv)) {
6105 for (kid = cLISTOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
6106 mod(kid, OP_LEAVESUBLV);
6112 Perl_ck_select(pTHX_ OP *o)
6115 if (o->op_flags & OPf_KIDS) {
6116 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6117 if (kid && kid->op_sibling) {
6118 o->op_type = OP_SSELECT;
6119 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
6121 return fold_constants(o);
6125 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6126 if (kid && kid->op_type == OP_RV2GV)
6127 kid->op_private &= ~HINT_STRICT_REFS;
6132 Perl_ck_shift(pTHX_ OP *o)
6134 const I32 type = o->op_type;
6136 if (!(o->op_flags & OPf_KIDS)) {
6140 #ifdef USE_5005THREADS
6141 if (!CvUNIQUE(PL_compcv)) {
6142 argop = newOP(OP_PADAV, OPf_REF);
6143 argop->op_targ = 0; /* PAD_SV(0) is @_ */
6146 argop = newUNOP(OP_RV2AV, 0,
6147 scalar(newGVOP(OP_GV, 0,
6148 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
6151 argop = newUNOP(OP_RV2AV, 0,
6152 scalar(newGVOP(OP_GV, 0, CvUNIQUE(PL_compcv) ? PL_argvgv : PL_defgv)));
6153 #endif /* USE_5005THREADS */
6154 return newUNOP(type, 0, scalar(argop));
6156 return scalar(modkids(ck_fun(o), type));
6160 Perl_ck_sort(pTHX_ OP *o)
6164 if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
6166 firstkid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6167 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
6169 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */
6171 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
6173 if (kid->op_type == OP_SCOPE) {
6177 else if (kid->op_type == OP_LEAVE) {
6178 if (o->op_type == OP_SORT) {
6179 op_null(kid); /* wipe out leave */
6182 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
6183 if (k->op_next == kid)
6185 /* don't descend into loops */
6186 else if (k->op_type == OP_ENTERLOOP
6187 || k->op_type == OP_ENTERITER)
6189 k = cLOOPx(k)->op_lastop;
6194 kid->op_next = 0; /* just disconnect the leave */
6195 k = kLISTOP->op_first;
6200 if (o->op_type == OP_SORT) {
6201 /* provide scalar context for comparison function/block */
6207 o->op_flags |= OPf_SPECIAL;
6209 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
6212 firstkid = firstkid->op_sibling;
6215 /* provide list context for arguments */
6216 if (o->op_type == OP_SORT)
6223 S_simplify_sort(pTHX_ OP *o)
6225 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6230 if (!(o->op_flags & OPf_STACKED))
6232 GvMULTI_on(gv_fetchpvs("a", GV_ADD|GV_NOTQUAL, SVt_PV));
6233 GvMULTI_on(gv_fetchpvs("b", GV_ADD|GV_NOTQUAL, SVt_PV));
6234 kid = kUNOP->op_first; /* get past null */
6235 if (kid->op_type != OP_SCOPE)
6237 kid = kLISTOP->op_last; /* get past scope */
6238 switch(kid->op_type) {
6246 k = kid; /* remember this node*/
6247 if (kBINOP->op_first->op_type != OP_RV2SV)
6249 kid = kBINOP->op_first; /* get past cmp */
6250 if (kUNOP->op_first->op_type != OP_GV)
6252 kid = kUNOP->op_first; /* get past rv2sv */
6254 if (GvSTASH(gv) != PL_curstash)
6256 gvname = GvNAME(gv);
6257 if (*gvname == 'a' && gvname[1] == '\0')
6259 else if (*gvname == 'b' && gvname[1] == '\0')
6264 kid = k; /* back to cmp */
6265 if (kBINOP->op_last->op_type != OP_RV2SV)
6267 kid = kBINOP->op_last; /* down to 2nd arg */
6268 if (kUNOP->op_first->op_type != OP_GV)
6270 kid = kUNOP->op_first; /* get past rv2sv */
6272 if (GvSTASH(gv) != PL_curstash)
6274 gvname = GvNAME(gv);
6276 ? !(*gvname == 'a' && gvname[1] == '\0')
6277 : !(*gvname == 'b' && gvname[1] == '\0'))
6279 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
6281 o->op_private |= OPpSORT_DESCEND;
6282 if (k->op_type == OP_NCMP)
6283 o->op_private |= OPpSORT_NUMERIC;
6284 if (k->op_type == OP_I_NCMP)
6285 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
6286 kid = cLISTOPo->op_first->op_sibling;
6287 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
6288 op_free(kid); /* then delete it */
6292 Perl_ck_split(pTHX_ OP *o)
6296 if (o->op_flags & OPf_STACKED)
6297 return no_fh_allowed(o);
6299 kid = cLISTOPo->op_first;
6300 if (kid->op_type != OP_NULL)
6301 Perl_croak(aTHX_ "panic: ck_split");
6302 kid = kid->op_sibling;
6303 op_free(cLISTOPo->op_first);
6304 cLISTOPo->op_first = kid;
6306 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvs(" "));
6307 cLISTOPo->op_last = kid; /* There was only one element previously */
6310 if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) {
6311 OP * const sibl = kid->op_sibling;
6312 kid->op_sibling = 0;
6313 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
6314 if (cLISTOPo->op_first == cLISTOPo->op_last)
6315 cLISTOPo->op_last = kid;
6316 cLISTOPo->op_first = kid;
6317 kid->op_sibling = sibl;
6320 kid->op_type = OP_PUSHRE;
6321 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
6323 if (((PMOP *)kid)->op_pmflags & PMf_GLOBAL && ckWARN(WARN_REGEXP)) {
6324 Perl_warner(aTHX_ packWARN(WARN_REGEXP),
6325 "Use of /g modifier is meaningless in split");
6328 if (!kid->op_sibling)
6329 append_elem(OP_SPLIT, o, newDEFSVOP());
6331 kid = kid->op_sibling;
6334 if (!kid->op_sibling)
6335 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
6336 assert(kid->op_sibling);
6338 kid = kid->op_sibling;
6341 if (kid->op_sibling)
6342 return too_many_arguments(o,OP_DESC(o));
6348 Perl_ck_join(pTHX_ OP *o)
6350 const OP * const kid = cLISTOPo->op_first->op_sibling;
6351 if (kid && kid->op_type == OP_MATCH) {
6352 if (ckWARN(WARN_SYNTAX)) {
6353 const REGEXP *re = PM_GETRE(kPMOP);
6354 const char *pmstr = re ? re->precomp : "STRING";
6355 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
6356 "/%s/ should probably be written as \"%s\"",
6364 Perl_ck_subr(pTHX_ OP *o)
6366 OP *prev = ((cUNOPo->op_first->op_sibling)
6367 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
6368 OP *o2 = prev->op_sibling;
6370 const char *proto = NULL;
6371 const char *proto_end = NULL;
6376 I32 contextclass = 0;
6377 const char *e = NULL;
6379 o->op_private |= OPpENTERSUB_HASTARG;
6380 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
6381 if (cvop->op_type == OP_RV2CV) {
6383 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
6384 op_null(cvop); /* disable rv2cv */
6385 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
6386 if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) {
6387 GV *gv = cGVOPx_gv(tmpop);
6390 tmpop->op_private |= OPpEARLY_CV;
6391 else if (SvPOK(cv)) {
6393 namegv = CvANON(cv) ? gv : CvGV(cv);
6394 proto = SvPV((SV*)cv, len);
6395 proto_end = proto + len;
6399 else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
6400 if (o2->op_type == OP_CONST)
6401 o2->op_private &= ~OPpCONST_STRICT;
6402 else if (o2->op_type == OP_LIST) {
6403 OP * const sib = ((UNOP*)o2)->op_first->op_sibling;
6404 if (sib && sib->op_type == OP_CONST)
6405 sib->op_private &= ~OPpCONST_STRICT;
6408 o->op_private |= (PL_hints & HINT_STRICT_REFS);
6409 if (PERLDB_SUB && PL_curstash != PL_debstash)
6410 o->op_private |= OPpENTERSUB_DB;
6411 while (o2 != cvop) {
6413 if (proto >= proto_end)
6414 return too_many_arguments(o, gv_ename(namegv));
6434 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
6436 arg == 1 ? "block or sub {}" : "sub {}",
6437 gv_ename(namegv), o2);
6440 /* '*' allows any scalar type, including bareword */
6443 if (o2->op_type == OP_RV2GV)
6444 goto wrapref; /* autoconvert GLOB -> GLOBref */
6445 else if (o2->op_type == OP_CONST)
6446 o2->op_private &= ~OPpCONST_STRICT;
6447 else if (o2->op_type == OP_ENTERSUB) {
6448 /* accidental subroutine, revert to bareword */
6449 OP *gvop = ((UNOP*)o2)->op_first;
6450 if (gvop && gvop->op_type == OP_NULL) {
6451 gvop = ((UNOP*)gvop)->op_first;
6453 for (; gvop->op_sibling; gvop = gvop->op_sibling)
6456 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
6457 (gvop = ((UNOP*)gvop)->op_first) &&
6458 gvop->op_type == OP_GV)
6460 GV * const gv = cGVOPx_gv(gvop);
6461 OP * const sibling = o2->op_sibling;
6462 SV * const n = newSVpvs("");
6464 gv_fullname4(n, gv, "", FALSE);
6465 o2 = newSVOP(OP_CONST, 0, n);
6466 prev->op_sibling = o2;
6467 o2->op_sibling = sibling;
6483 if (contextclass++ == 0) {
6484 e = strchr(proto, ']');
6485 if (!e || e == proto)
6494 const char *p = proto;
6495 const char *const end = proto;
6497 while (*--p != '[');
6498 bad_type(arg, Perl_form(aTHX_ "one of %.*s",
6500 gv_ename(namegv), o2);
6505 if (o2->op_type == OP_RV2GV)
6508 bad_type(arg, "symbol", gv_ename(namegv), o2);
6511 if (o2->op_type == OP_ENTERSUB)
6514 bad_type(arg, "subroutine entry", gv_ename(namegv), o2);
6517 if (o2->op_type == OP_RV2SV ||
6518 o2->op_type == OP_PADSV ||
6519 o2->op_type == OP_HELEM ||
6520 o2->op_type == OP_AELEM
6521 #ifdef USE_5005THREADS
6522 || o2->op_type == OP_THREADSV
6527 bad_type(arg, "scalar", gv_ename(namegv), o2);
6530 if (o2->op_type == OP_RV2AV ||
6531 o2->op_type == OP_PADAV)
6534 bad_type(arg, "array", gv_ename(namegv), o2);
6537 if (o2->op_type == OP_RV2HV ||
6538 o2->op_type == OP_PADHV)
6541 bad_type(arg, "hash", gv_ename(namegv), o2);
6546 OP* const sib = kid->op_sibling;
6547 kid->op_sibling = 0;
6548 o2 = newUNOP(OP_REFGEN, 0, kid);
6549 o2->op_sibling = sib;
6550 prev->op_sibling = o2;
6552 if (contextclass && e) {
6567 Perl_croak(aTHX_ "Malformed prototype for %s: %"SVf,
6568 gv_ename(namegv), (void*)cv);
6573 mod(o2, OP_ENTERSUB);
6575 o2 = o2->op_sibling;
6577 if (proto && !optional && proto_end > proto &&
6578 (*proto != '@' && *proto != '%' && *proto != ';'))
6579 return too_few_arguments(o, gv_ename(namegv));
6584 Perl_ck_svconst(pTHX_ OP *o)
6586 PERL_UNUSED_CONTEXT;
6587 SvREADONLY_on(cSVOPo->op_sv);
6592 Perl_ck_trunc(pTHX_ OP *o)
6594 if (o->op_flags & OPf_KIDS) {
6595 SVOP *kid = (SVOP*)cUNOPo->op_first;
6597 if (kid->op_type == OP_NULL)
6598 kid = (SVOP*)kid->op_sibling;
6599 if (kid && kid->op_type == OP_CONST &&
6600 (kid->op_private & OPpCONST_BARE))
6602 o->op_flags |= OPf_SPECIAL;
6603 kid->op_private &= ~OPpCONST_STRICT;
6610 Perl_ck_substr(pTHX_ OP *o)
6613 if ((o->op_flags & OPf_KIDS) && o->op_private == 4) {
6614 OP *kid = cLISTOPo->op_first;
6616 if (kid->op_type == OP_NULL)
6617 kid = kid->op_sibling;
6619 kid->op_flags |= OPf_MOD;
6625 /* A peephole optimizer. We visit the ops in the order they're to execute.
6626 * See the comments at the top of this file for more details about when
6627 * peep() is called */
6630 Perl_peep(pTHX_ register OP *o)
6632 register OP* oldop = NULL;
6635 if (!o || o->op_seq)
6639 SAVEVPTR(PL_curcop);
6640 for (; o; o = o->op_next) {
6643 /* The special value -1 is used by the B::C compiler backend to indicate
6644 * that an op is statically defined and should not be freed */
6645 if (!PL_op_seqmax || PL_op_seqmax == (U16)-1)
6648 switch (o->op_type) {
6652 PL_curcop = ((COP*)o); /* for warnings */
6653 o->op_seq = PL_op_seqmax++;
6657 if (cSVOPo->op_private & OPpCONST_STRICT)
6658 no_bareword_allowed(o);
6660 case OP_METHOD_NAMED:
6661 /* Relocate sv to the pad for thread safety.
6662 * Despite being a "constant", the SV is written to,
6663 * for reference counts, sv_upgrade() etc. */
6665 const PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP);
6666 if (o->op_type == OP_CONST && SvPADTMP(cSVOPo->op_sv)) {
6667 /* If op_sv is already a PADTMP then it is being used by
6668 * some pad, so make a copy. */
6669 sv_setsv(PAD_SVl(ix),cSVOPo->op_sv);
6670 SvREADONLY_on(PAD_SVl(ix));
6671 SvREFCNT_dec(cSVOPo->op_sv);
6673 else if (o->op_type == OP_CONST
6674 && cSVOPo->op_sv == &PL_sv_undef) {
6675 /* PL_sv_undef is hack - it's unsafe to store it in the
6676 AV that is the pad, because av_fetch treats values of
6677 PL_sv_undef as a "free" AV entry and will merrily
6678 replace them with a new SV, causing pad_alloc to think
6679 that this pad slot is free. (When, clearly, it is not)
6681 SvOK_off(PAD_SVl(ix));
6682 SvPADTMP_on(PAD_SVl(ix));
6683 SvREADONLY_on(PAD_SVl(ix));
6686 SvREFCNT_dec(PAD_SVl(ix));
6687 SvPADTMP_on(cSVOPo->op_sv);
6688 PAD_SETSV(ix, cSVOPo->op_sv);
6689 /* XXX I don't know how this isn't readonly already. */
6690 SvREADONLY_on(PAD_SVl(ix));
6692 cSVOPo->op_sv = NULL;
6696 o->op_seq = PL_op_seqmax++;
6700 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
6701 if (o->op_next->op_private & OPpTARGET_MY) {
6702 if (o->op_flags & OPf_STACKED) /* chained concats */
6703 goto ignore_optimization;
6705 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
6706 o->op_targ = o->op_next->op_targ;
6707 o->op_next->op_targ = 0;
6708 o->op_private |= OPpTARGET_MY;
6711 op_null(o->op_next);
6713 ignore_optimization:
6714 o->op_seq = PL_op_seqmax++;
6717 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
6718 o->op_seq = PL_op_seqmax++;
6719 break; /* Scalar stub must produce undef. List stub is noop */
6723 if (o->op_targ == OP_NEXTSTATE
6724 || o->op_targ == OP_DBSTATE
6725 || o->op_targ == OP_SETSTATE)
6727 PL_curcop = ((COP*)o);
6729 /* XXX: We avoid setting op_seq here to prevent later calls
6730 to peep() from mistakenly concluding that optimisation
6731 has already occurred. This doesn't fix the real problem,
6732 though (See 20010220.007). AMS 20010719 */
6733 if (oldop && o->op_next) {
6734 oldop->op_next = o->op_next;
6742 if (oldop && o->op_next) {
6743 oldop->op_next = o->op_next;
6746 o->op_seq = PL_op_seqmax++;
6751 if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) {
6752 OP* const pop = (o->op_type == OP_PADAV) ?
6753 o->op_next : o->op_next->op_next;
6755 if (pop && pop->op_type == OP_CONST &&
6756 ((PL_op = pop->op_next)) &&
6757 pop->op_next->op_type == OP_AELEM &&
6758 !(pop->op_next->op_private &
6759 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) &&
6760 (i = SvIV(((SVOP*)pop)->op_sv) - CopARYBASE_get(PL_curcop))
6765 if (cSVOPx(pop)->op_private & OPpCONST_STRICT)
6766 no_bareword_allowed(pop);
6767 if (o->op_type == OP_GV)
6768 op_null(o->op_next);
6769 op_null(pop->op_next);
6771 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
6772 o->op_next = pop->op_next->op_next;
6773 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
6774 o->op_private = (U8)i;
6775 if (o->op_type == OP_GV) {
6780 o->op_flags |= OPf_SPECIAL;
6781 o->op_type = OP_AELEMFAST;
6783 o->op_seq = PL_op_seqmax++;
6787 if (o->op_next->op_type == OP_RV2SV) {
6788 if (!(o->op_next->op_private & OPpDEREF)) {
6789 op_null(o->op_next);
6790 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
6792 o->op_next = o->op_next->op_next;
6793 o->op_type = OP_GVSV;
6794 o->op_ppaddr = PL_ppaddr[OP_GVSV];
6797 else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
6798 GV * const gv = cGVOPo_gv;
6799 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
6800 /* XXX could check prototype here instead of just carping */
6801 SV * const sv = sv_newmortal();
6802 gv_efullname3(sv, gv, NULL);
6803 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
6804 "%"SVf"() called too early to check prototype",
6808 else if (o->op_next->op_type == OP_READLINE
6809 && o->op_next->op_next->op_type == OP_CONCAT
6810 && (o->op_next->op_next->op_flags & OPf_STACKED))
6812 /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */
6813 o->op_type = OP_RCATLINE;
6814 o->op_flags |= OPf_STACKED;
6815 o->op_ppaddr = PL_ppaddr[OP_RCATLINE];
6816 op_null(o->op_next->op_next);
6817 op_null(o->op_next);
6820 o->op_seq = PL_op_seqmax++;
6831 o->op_seq = PL_op_seqmax++;
6832 while (cLOGOP->op_other->op_type == OP_NULL)
6833 cLOGOP->op_other = cLOGOP->op_other->op_next;
6834 peep(cLOGOP->op_other); /* Recursive calls are not replaced by fptr calls */
6839 o->op_seq = PL_op_seqmax++;
6840 while (cLOOP->op_redoop->op_type == OP_NULL)
6841 cLOOP->op_redoop = cLOOP->op_redoop->op_next;
6842 peep(cLOOP->op_redoop);
6843 while (cLOOP->op_nextop->op_type == OP_NULL)
6844 cLOOP->op_nextop = cLOOP->op_nextop->op_next;
6845 peep(cLOOP->op_nextop);
6846 while (cLOOP->op_lastop->op_type == OP_NULL)
6847 cLOOP->op_lastop = cLOOP->op_lastop->op_next;
6848 peep(cLOOP->op_lastop);
6854 o->op_seq = PL_op_seqmax++;
6855 while (cPMOP->op_pmreplstart &&
6856 cPMOP->op_pmreplstart->op_type == OP_NULL)
6857 cPMOP->op_pmreplstart = cPMOP->op_pmreplstart->op_next;
6858 peep(cPMOP->op_pmreplstart);
6862 o->op_seq = PL_op_seqmax++;
6863 if (o->op_next && o->op_next->op_type == OP_NEXTSTATE
6864 && ckWARN(WARN_SYNTAX))
6866 if (o->op_next->op_sibling) {
6867 const OPCODE type = o->op_next->op_sibling->op_type;
6868 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
6869 const line_t oldline = CopLINE(PL_curcop);
6870 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_next));
6871 Perl_warner(aTHX_ packWARN(WARN_EXEC),
6872 "Statement unlikely to be reached");
6873 Perl_warner(aTHX_ packWARN(WARN_EXEC),
6874 "\t(Maybe you meant system() when you said exec()?)\n");
6875 CopLINE_set(PL_curcop, oldline);
6885 SV **svp, **indsvp, *sv;
6887 const char *key = NULL;
6890 o->op_seq = PL_op_seqmax++;
6892 if (((BINOP*)o)->op_last->op_type != OP_CONST)
6895 /* Make the CONST have a shared SV */
6896 svp = cSVOPx_svp(((BINOP*)o)->op_last);
6897 if ((!SvFAKE(sv = *svp) || !SvREADONLY(sv)) && !IS_PADCONST(sv)) {
6898 key = SvPV_const(sv, keylen);
6899 lexname = newSVpvn_share(key,
6900 SvUTF8(sv) ? -(I32)keylen : (I32)keylen,
6906 if ((o->op_private & (OPpLVAL_INTRO)))
6909 rop = (UNOP*)((BINOP*)o)->op_first;
6910 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6912 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6913 if (!SvPAD_TYPED(lexname))
6915 fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE);
6916 if (!fields || !GvHV(*fields))
6918 key = SvPV_const(*svp, keylen);
6919 indsvp = hv_fetch(GvHV(*fields), key,
6920 SvUTF8(*svp) ? -(I32)keylen : (I32)keylen, FALSE);
6922 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" "
6923 "in variable %s of type %s",
6924 key, SvPV_nolen_const(lexname),
6925 HvNAME_get(SvSTASH(lexname)));
6927 ind = SvIV(*indsvp);
6929 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6930 rop->op_type = OP_RV2AV;
6931 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6932 o->op_type = OP_AELEM;
6933 o->op_ppaddr = PL_ppaddr[OP_AELEM];
6935 if (SvREADONLY(*svp))
6937 SvFLAGS(sv) |= (SvFLAGS(*svp)
6938 & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
6948 SV **svp, **indsvp, *sv;
6952 SVOP *first_key_op, *key_op;
6954 o->op_seq = PL_op_seqmax++;
6955 if ((o->op_private & (OPpLVAL_INTRO))
6956 /* I bet there's always a pushmark... */
6957 || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST)
6958 /* hmmm, no optimization if list contains only one key. */
6960 rop = (UNOP*)((LISTOP*)o)->op_last;
6961 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6963 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6964 if (!SvPAD_TYPED(lexname))
6966 fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE);
6967 if (!fields || !GvHV(*fields))
6969 /* Again guessing that the pushmark can be jumped over.... */
6970 first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling)
6971 ->op_first->op_sibling;
6972 /* Check that the key list contains only constants. */
6973 for (key_op = first_key_op; key_op;
6974 key_op = (SVOP*)key_op->op_sibling)
6975 if (key_op->op_type != OP_CONST)
6979 rop->op_type = OP_RV2AV;
6980 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6981 o->op_type = OP_ASLICE;
6982 o->op_ppaddr = PL_ppaddr[OP_ASLICE];
6983 for (key_op = first_key_op; key_op;
6984 key_op = (SVOP*)key_op->op_sibling) {
6985 svp = cSVOPx_svp(key_op);
6986 key = SvPV_const(*svp, keylen);
6987 indsvp = hv_fetch(GvHV(*fields), key,
6988 SvUTF8(*svp) ? -(I32)keylen : (I32)keylen,
6991 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" "
6992 "in variable %s of type %s",
6993 key, SvPV(lexname, n_a), HvNAME_get(SvSTASH(lexname)));
6995 ind = SvIV(*indsvp);
6997 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6999 if (SvREADONLY(*svp))
7001 SvFLAGS(sv) |= (SvFLAGS(*svp)
7002 & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
7010 /* will point to RV2AV or PADAV op on LHS/RHS of assign */
7014 /* check that RHS of sort is a single plain array */
7015 OP *oright = cUNOPo->op_first;
7016 if (!oright || oright->op_type != OP_PUSHMARK)
7019 /* reverse sort ... can be optimised. */
7020 if (!cUNOPo->op_sibling) {
7021 /* Nothing follows us on the list. */
7022 OP * const reverse = o->op_next;
7024 if (reverse->op_type == OP_REVERSE &&
7025 (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) {
7026 OP * const pushmark = cUNOPx(reverse)->op_first;
7027 if (pushmark && (pushmark->op_type == OP_PUSHMARK)
7028 && (cUNOPx(pushmark)->op_sibling == o)) {
7029 /* reverse -> pushmark -> sort */
7030 o->op_private |= OPpSORT_REVERSE;
7032 pushmark->op_next = oright->op_next;
7038 /* make @a = sort @a act in-place */
7040 o->op_seq = PL_op_seqmax++;
7042 oright = cUNOPx(oright)->op_sibling;
7045 if (oright->op_type == OP_NULL) { /* skip sort block/sub */
7046 oright = cUNOPx(oright)->op_sibling;
7050 (oright->op_type != OP_RV2AV && oright->op_type != OP_PADAV)
7051 || oright->op_next != o
7052 || (oright->op_private & OPpLVAL_INTRO)
7056 /* o2 follows the chain of op_nexts through the LHS of the
7057 * assign (if any) to the aassign op itself */
7059 if (!o2 || o2->op_type != OP_NULL)
7062 if (!o2 || o2->op_type != OP_PUSHMARK)
7065 if (o2 && o2->op_type == OP_GV)
7068 || (o2->op_type != OP_PADAV && o2->op_type != OP_RV2AV)
7069 || (o2->op_private & OPpLVAL_INTRO)
7074 if (!o2 || o2->op_type != OP_NULL)
7077 if (!o2 || o2->op_type != OP_AASSIGN
7078 || (o2->op_flags & OPf_WANT) != OPf_WANT_VOID)
7081 /* check that the sort is the first arg on RHS of assign */
7083 o2 = cUNOPx(o2)->op_first;
7084 if (!o2 || o2->op_type != OP_NULL)
7086 o2 = cUNOPx(o2)->op_first;
7087 if (!o2 || o2->op_type != OP_PUSHMARK)
7089 if (o2->op_sibling != o)
7092 /* check the array is the same on both sides */
7093 if (oleft->op_type == OP_RV2AV) {
7094 if (oright->op_type != OP_RV2AV
7095 || !cUNOPx(oright)->op_first
7096 || cUNOPx(oright)->op_first->op_type != OP_GV
7097 || cGVOPx_gv(cUNOPx(oleft)->op_first) !=
7098 cGVOPx_gv(cUNOPx(oright)->op_first)
7102 else if (oright->op_type != OP_PADAV
7103 || oright->op_targ != oleft->op_targ
7107 /* transfer MODishness etc from LHS arg to RHS arg */
7108 oright->op_flags = oleft->op_flags;
7109 o->op_private |= OPpSORT_INPLACE;
7111 /* excise push->gv->rv2av->null->aassign */
7112 o2 = o->op_next->op_next;
7113 op_null(o2); /* PUSHMARK */
7115 if (o2->op_type == OP_GV) {
7116 op_null(o2); /* GV */
7119 op_null(o2); /* RV2AV or PADAV */
7120 o2 = o2->op_next->op_next;
7121 op_null(o2); /* AASSIGN */
7123 o->op_next = o2->op_next;
7129 OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av;
7131 LISTOP *enter, *exlist;
7132 o->op_seq = PL_op_seqmax++;
7134 enter = (LISTOP *) o->op_next;
7137 if (enter->op_type == OP_NULL) {
7138 enter = (LISTOP *) enter->op_next;
7142 /* for $a (...) will have OP_GV then OP_RV2GV here.
7143 for (...) just has an OP_GV. */
7144 if (enter->op_type == OP_GV) {
7145 gvop = (OP *) enter;
7146 enter = (LISTOP *) enter->op_next;
7149 if (enter->op_type == OP_RV2GV) {
7150 enter = (LISTOP *) enter->op_next;
7156 if (enter->op_type != OP_ENTERITER)
7159 iter = enter->op_next;
7160 if (!iter || iter->op_type != OP_ITER)
7163 expushmark = enter->op_first;
7164 if (!expushmark || expushmark->op_type != OP_NULL
7165 || expushmark->op_targ != OP_PUSHMARK)
7168 exlist = (LISTOP *) expushmark->op_sibling;
7169 if (!exlist || exlist->op_type != OP_NULL
7170 || exlist->op_targ != OP_LIST)
7173 if (exlist->op_last != o) {
7174 /* Mmm. Was expecting to point back to this op. */
7177 theirmark = exlist->op_first;
7178 if (!theirmark || theirmark->op_type != OP_PUSHMARK)
7181 if (theirmark->op_sibling != o) {
7182 /* There's something between the mark and the reverse, eg
7183 for (1, reverse (...))
7188 ourmark = ((LISTOP *)o)->op_first;
7189 if (!ourmark || ourmark->op_type != OP_PUSHMARK)
7192 ourlast = ((LISTOP *)o)->op_last;
7193 if (!ourlast || ourlast->op_next != o)
7196 rv2av = ourmark->op_sibling;
7197 if (rv2av && rv2av->op_type == OP_RV2AV && rv2av->op_sibling == 0
7198 && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)
7199 && enter->op_flags == (OPf_WANT_LIST | OPf_KIDS)) {
7200 /* We're just reversing a single array. */
7201 rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF;
7202 enter->op_flags |= OPf_STACKED;
7205 /* We don't have control over who points to theirmark, so sacrifice
7207 theirmark->op_next = ourmark->op_next;
7208 theirmark->op_flags = ourmark->op_flags;
7209 ourlast->op_next = gvop ? gvop : (OP *) enter;
7212 enter->op_private |= OPpITER_REVERSED;
7213 iter->op_private |= OPpITER_REVERSED;
7220 UNOP *refgen, *rv2cv;
7223 /* I do not understand this, but if o->op_opt isn't set to 1,
7224 various tests in ext/B/t/bytecode.t fail with no readily
7226 /* Converted from op_opt to op_seq for 5.8.x. */
7228 o->op_seq = PL_op_seqmax++;
7231 if ((o->op_flags && OPf_WANT) != OPf_WANT_VOID)
7234 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
7237 rv2gv = ((BINOP *)o)->op_last;
7238 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
7241 refgen = (UNOP *)((BINOP *)o)->op_first;
7243 if (!refgen || refgen->op_type != OP_REFGEN)
7246 exlist = (LISTOP *)refgen->op_first;
7247 if (!exlist || exlist->op_type != OP_NULL
7248 || exlist->op_targ != OP_LIST)
7251 if (exlist->op_first->op_type != OP_PUSHMARK)
7254 rv2cv = (UNOP*)exlist->op_last;
7256 if (rv2cv->op_type != OP_RV2CV)
7259 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
7260 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
7261 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
7263 o->op_private |= OPpASSIGN_CV_TO_GV;
7264 rv2gv->op_private |= OPpDONT_INIT_GV;
7265 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
7272 o->op_seq = PL_op_seqmax++;
7281 Perl_custom_op_name(pTHX_ OP* o)
7283 const IV index = PTR2IV(o->op_ppaddr);
7287 if (!PL_custom_op_names) /* This probably shouldn't happen */
7288 return (char *)PL_op_name[OP_CUSTOM];
7290 keysv = sv_2mortal(newSViv(index));
7292 he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0);
7294 return (char *)PL_op_name[OP_CUSTOM]; /* Don't know who you are */
7296 return SvPV_nolen(HeVAL(he));
7300 Perl_custom_op_desc(pTHX_ OP* o)
7302 const IV index = PTR2IV(o->op_ppaddr);
7306 if (!PL_custom_op_descs)
7307 return (char *)PL_op_desc[OP_CUSTOM];
7309 keysv = sv_2mortal(newSViv(index));
7311 he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0);
7313 return (char *)PL_op_desc[OP_CUSTOM];
7315 return SvPV_nolen(HeVAL(he));
7320 /* Efficient sub that returns a constant scalar value. */
7322 const_sv_xsub(pTHX_ CV* cv)
7328 Perl_croak(aTHX_ "usage: %s::%s()",
7329 HvNAME_get(GvSTASH(CvGV(cv))), GvNAME(CvGV(cv)));
7333 ST(0) = (SV*)XSANY.any_ptr;
7339 * c-indentation-style: bsd
7341 * indent-tabs-mode: t
7344 * ex: set ts=8 sts=4 sw=4 noet: