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.
76 /* To implement user lexical pragmas, there needs to be a way at run time to
77 get the compile time state of %^H for that block. Storing %^H in every
78 block (or even COP) would be very expensive, so a different approach is
79 taken. The (running) state of %^H is serialised into a tree of HE-like
80 structs. Stores into %^H are chained onto the current leaf as a struct
81 refcounted_he * with the key and the value. Deletes from %^H are saved
82 with a value of PL_sv_placeholder. The state of %^H at any point can be
83 turned back into a regular HV by walking back up the tree from that point's
84 leaf, ignoring any key you've already seen (placeholder or not), storing
85 the rest into the HV structure, then removing the placeholders. Hence
86 memory is only used to store the %^H deltas from the enclosing COP, rather
87 than the entire %^H on each COP.
89 To cause actions on %^H to write out the serialisation records, it has
90 magic type 'H'. This magic (itself) does nothing, but its presence causes
91 the values to gain magic type 'h', which has entries for set and clear.
92 C<Perl_magic_sethint> updates C<PL_compiling.cop_hints> with a store
93 record, with deletes written by C<Perl_magic_clearhint>. C<SAVE_HINTS>
94 saves the current C<PL_compiling.cop_hints> on the save stack, so that it
95 will be correctly restored when any inner compiling scope is exited.
101 #include "keywords.h"
103 #define CALL_PEEP(o) CALL_FPTR(PL_peepp)(aTHX_ o)
105 #if defined(PL_OP_SLAB_ALLOC)
107 #ifndef PERL_SLAB_SIZE
108 #define PERL_SLAB_SIZE 2048
112 Perl_Slab_Alloc(pTHX_ int m, size_t sz)
115 * To make incrementing use count easy PL_OpSlab is an I32 *
116 * To make inserting the link to slab PL_OpPtr is I32 **
117 * So compute size in units of sizeof(I32 *) as that is how Pl_OpPtr increments
118 * Add an overhead for pointer to slab and round up as a number of pointers
120 sz = (sz + 2*sizeof(I32 *) -1)/sizeof(I32 *);
121 if ((PL_OpSpace -= sz) < 0) {
122 PL_OpPtr = (I32 **) PerlMemShared_malloc(PERL_SLAB_SIZE*sizeof(I32*));
126 Zero(PL_OpPtr,PERL_SLAB_SIZE,I32 **);
127 /* We reserve the 0'th I32 sized chunk as a use count */
128 PL_OpSlab = (I32 *) PL_OpPtr;
129 /* Reduce size by the use count word, and by the size we need.
130 * Latter is to mimic the '-=' in the if() above
132 PL_OpSpace = PERL_SLAB_SIZE - (sizeof(I32)+sizeof(I32 **)-1)/sizeof(I32 **) - sz;
133 /* Allocation pointer starts at the top.
134 Theory: because we build leaves before trunk allocating at end
135 means that at run time access is cache friendly upward
137 PL_OpPtr += PERL_SLAB_SIZE;
139 assert( PL_OpSpace >= 0 );
140 /* Move the allocation pointer down */
142 assert( PL_OpPtr > (I32 **) PL_OpSlab );
143 *PL_OpPtr = PL_OpSlab; /* Note which slab it belongs to */
144 (*PL_OpSlab)++; /* Increment use count of slab */
145 assert( PL_OpPtr+sz <= ((I32 **) PL_OpSlab + PERL_SLAB_SIZE) );
146 assert( *PL_OpSlab > 0 );
147 return (void *)(PL_OpPtr + 1);
151 Perl_Slab_Free(pTHX_ void *op)
153 I32 * const * const ptr = (I32 **) op;
154 I32 * const slab = ptr[-1];
155 assert( ptr-1 > (I32 **) slab );
156 assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) );
158 if (--(*slab) == 0) {
160 # define PerlMemShared PerlMem
163 PerlMemShared_free(slab);
164 if (slab == PL_OpSlab) {
171 * In the following definition, the ", (OP*)0" is just to make the compiler
172 * think the expression is of the right type: croak actually does a Siglongjmp.
174 #define CHECKOP(type,o) \
175 ((PL_op_mask && PL_op_mask[type]) \
176 ? ( op_free((OP*)o), \
177 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
179 : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o))
181 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
184 S_gv_ename(pTHX_ GV *gv)
186 SV* const tmpsv = sv_newmortal();
187 gv_efullname3(tmpsv, gv, NULL);
188 return SvPV_nolen_const(tmpsv);
192 S_no_fh_allowed(pTHX_ OP *o)
194 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
200 S_too_few_arguments(pTHX_ OP *o, const char *name)
202 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
207 S_too_many_arguments(pTHX_ OP *o, const char *name)
209 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
214 S_bad_type(pTHX_ I32 n, const char *t, const char *name, const OP *kid)
216 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
217 (int)n, name, t, OP_DESC(kid)));
221 S_no_bareword_allowed(pTHX_ const OP *o)
224 return; /* various ok barewords are hidden in extra OP_NULL */
225 qerror(Perl_mess(aTHX_
226 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
230 /* "register" allocation */
233 Perl_allocmy(pTHX_ char *name)
237 const bool is_our = (PL_in_my == KEY_our);
239 /* complain about "my $<special_var>" etc etc */
243 (USE_UTF8_IN_NAMES && UTF8_IS_START(name[1])) ||
244 (name[1] == '_' && (*name == '$' || name[2]))))
246 /* name[2] is true if strlen(name) > 2 */
247 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
248 /* 1999-02-27 mjd@plover.com */
250 p = strchr(name, '\0');
252 /* The next block assumes the buffer is at least 205 chars
253 long. At present, it's always at least 256 chars. */
254 if (p - name > 200) {
256 strlcpy(name + 200, "...", 4);
258 strcpy(name + 200, "...");
265 /* Move everything else down one character */
266 for (; p-name > 2; p--)
268 name[2] = toCTRL(name[1]);
271 yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
274 /* check for duplicate declaration */
275 pad_check_dup(name, is_our, (PL_curstash ? PL_curstash : PL_defstash));
277 if (PL_in_my_stash && *name != '$') {
278 yyerror(Perl_form(aTHX_
279 "Can't declare class for non-scalar %s in \"%s\"",
281 is_our ? "our" : PL_in_my == KEY_state ? "state" : "my"));
284 /* allocate a spare slot and store the name in that slot */
286 off = pad_add_name(name,
289 /* $_ is always in main::, even with our */
290 ? (PL_curstash && !strEQ(name,"$_") ? PL_curstash : PL_defstash)
294 PL_in_my == KEY_state
302 Perl_op_free(pTHX_ OP *o)
307 if (!o || o->op_static)
311 if (o->op_private & OPpREFCOUNTED) {
322 refcnt = OpREFCNT_dec(o);
333 if (o->op_flags & OPf_KIDS) {
334 register OP *kid, *nextkid;
335 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
336 nextkid = kid->op_sibling; /* Get before next freeing kid */
341 type = (OPCODE)o->op_targ;
343 /* COP* is not cleared by op_clear() so that we may track line
344 * numbers etc even after null() */
345 if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
350 #ifdef DEBUG_LEAKING_SCALARS
357 Perl_op_clear(pTHX_ OP *o)
362 /* if (o->op_madprop && o->op_madprop->mad_next)
364 /* FIXME for MAD - if I uncomment these two lines t/op/pack.t fails with
365 "modification of a read only value" for a reason I can't fathom why.
366 It's the "" stringification of $_, where $_ was set to '' in a foreach
367 loop, but it defies simplification into a small test case.
368 However, commenting them out has caused ext/List/Util/t/weak.t to fail
371 mad_free(o->op_madprop);
377 switch (o->op_type) {
378 case OP_NULL: /* Was holding old type, if any. */
379 if (PL_madskills && o->op_targ != OP_NULL) {
380 o->op_type = o->op_targ;
384 case OP_ENTEREVAL: /* Was holding hints. */
388 if (!(o->op_flags & OPf_REF)
389 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
395 if (! (o->op_type == OP_AELEMFAST && o->op_flags & OPf_SPECIAL)) {
396 /* not an OP_PADAV replacement */
398 if (cPADOPo->op_padix > 0) {
399 /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
400 * may still exist on the pad */
401 pad_swipe(cPADOPo->op_padix, TRUE);
402 cPADOPo->op_padix = 0;
405 SvREFCNT_dec(cSVOPo->op_sv);
406 cSVOPo->op_sv = NULL;
410 case OP_METHOD_NAMED:
412 SvREFCNT_dec(cSVOPo->op_sv);
413 cSVOPo->op_sv = NULL;
416 Even if op_clear does a pad_free for the target of the op,
417 pad_free doesn't actually remove the sv that exists in the pad;
418 instead it lives on. This results in that it could be reused as
419 a target later on when the pad was reallocated.
422 pad_swipe(o->op_targ,1);
431 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
435 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
436 SvREFCNT_dec(cSVOPo->op_sv);
437 cSVOPo->op_sv = NULL;
440 Safefree(cPVOPo->op_pv);
441 cPVOPo->op_pv = NULL;
445 op_free(cPMOPo->op_pmreplroot);
449 if (INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot)) {
450 /* No GvIN_PAD_off here, because other references may still
451 * exist on the pad */
452 pad_swipe(INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot), TRUE);
455 SvREFCNT_dec((SV*)cPMOPo->op_pmreplroot);
462 HV * const pmstash = PmopSTASH(cPMOPo);
463 if (pmstash && !SvIS_FREED(pmstash)) {
464 MAGIC * const mg = mg_find((SV*)pmstash, PERL_MAGIC_symtab);
466 PMOP *pmop = (PMOP*) mg->mg_obj;
467 PMOP *lastpmop = NULL;
469 if (cPMOPo == pmop) {
471 lastpmop->op_pmnext = pmop->op_pmnext;
473 mg->mg_obj = (SV*) pmop->op_pmnext;
477 pmop = pmop->op_pmnext;
481 PmopSTASH_free(cPMOPo);
483 cPMOPo->op_pmreplroot = NULL;
484 /* we use the "SAFE" version of the PM_ macros here
485 * since sv_clean_all might release some PMOPs
486 * after PL_regex_padav has been cleared
487 * and the clearing of PL_regex_padav needs to
488 * happen before sv_clean_all
490 ReREFCNT_dec(PM_GETRE_SAFE(cPMOPo));
491 PM_SETRE_SAFE(cPMOPo, NULL);
493 if(PL_regex_pad) { /* We could be in destruction */
494 av_push((AV*) PL_regex_pad[0],(SV*) PL_regex_pad[(cPMOPo)->op_pmoffset]);
495 SvREPADTMP_on(PL_regex_pad[(cPMOPo)->op_pmoffset]);
496 PM_SETRE(cPMOPo, (cPMOPo)->op_pmoffset);
503 if (o->op_targ > 0) {
504 pad_free(o->op_targ);
510 S_cop_free(pTHX_ COP* cop)
512 Safefree(cop->cop_label); /* FIXME: treaddead ??? */
515 if (! specialWARN(cop->cop_warnings))
516 PerlMemShared_free(cop->cop_warnings);
517 if (! specialCopIO(cop->cop_io)) {
521 SvREFCNT_dec(cop->cop_io);
524 Perl_refcounted_he_free(aTHX_ cop->cop_hints);
528 Perl_op_null(pTHX_ OP *o)
531 if (o->op_type == OP_NULL)
535 o->op_targ = o->op_type;
536 o->op_type = OP_NULL;
537 o->op_ppaddr = PL_ppaddr[OP_NULL];
541 Perl_op_refcnt_lock(pTHX)
549 Perl_op_refcnt_unlock(pTHX)
556 /* Contextualizers */
558 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
561 Perl_linklist(pTHX_ OP *o)
568 /* establish postfix order */
569 first = cUNOPo->op_first;
572 o->op_next = LINKLIST(first);
575 if (kid->op_sibling) {
576 kid->op_next = LINKLIST(kid->op_sibling);
577 kid = kid->op_sibling;
591 Perl_scalarkids(pTHX_ OP *o)
593 if (o && o->op_flags & OPf_KIDS) {
595 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
602 S_scalarboolean(pTHX_ OP *o)
605 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
606 if (ckWARN(WARN_SYNTAX)) {
607 const line_t oldline = CopLINE(PL_curcop);
609 if (PL_copline != NOLINE)
610 CopLINE_set(PL_curcop, PL_copline);
611 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
612 CopLINE_set(PL_curcop, oldline);
619 Perl_scalar(pTHX_ OP *o)
624 /* assumes no premature commitment */
625 if (!o || PL_error_count || (o->op_flags & OPf_WANT)
626 || o->op_type == OP_RETURN)
631 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
633 switch (o->op_type) {
635 scalar(cBINOPo->op_first);
640 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
644 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
645 if (!kPMOP->op_pmreplroot)
646 deprecate_old("implicit split to @_");
654 if (o->op_flags & OPf_KIDS) {
655 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
661 kid = cLISTOPo->op_first;
663 while ((kid = kid->op_sibling)) {
669 WITH_THR(PL_curcop = &PL_compiling);
674 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
680 WITH_THR(PL_curcop = &PL_compiling);
683 if (ckWARN(WARN_VOID))
684 Perl_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
690 Perl_scalarvoid(pTHX_ OP *o)
694 const char* useless = NULL;
698 /* trailing mad null ops don't count as "there" for void processing */
700 o->op_type != OP_NULL &&
702 o->op_sibling->op_type == OP_NULL)
705 for (sib = o->op_sibling;
706 sib && sib->op_type == OP_NULL;
707 sib = sib->op_sibling) ;
713 if (o->op_type == OP_NEXTSTATE
714 || o->op_type == OP_SETSTATE
715 || o->op_type == OP_DBSTATE
716 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
717 || o->op_targ == OP_SETSTATE
718 || o->op_targ == OP_DBSTATE)))
719 PL_curcop = (COP*)o; /* for warning below */
721 /* assumes no premature commitment */
722 want = o->op_flags & OPf_WANT;
723 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
724 || o->op_type == OP_RETURN)
729 if ((o->op_private & OPpTARGET_MY)
730 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
732 return scalar(o); /* As if inside SASSIGN */
735 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
737 switch (o->op_type) {
739 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
743 if (o->op_flags & OPf_STACKED)
747 if (o->op_private == 4)
819 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
820 useless = OP_DESC(o);
824 kid = cUNOPo->op_first;
825 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
826 kid->op_type != OP_TRANS) {
829 useless = "negative pattern binding (!~)";
836 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
837 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
838 useless = "a variable";
843 if (cSVOPo->op_private & OPpCONST_STRICT)
844 no_bareword_allowed(o);
846 if (ckWARN(WARN_VOID)) {
847 useless = "a constant";
848 if (o->op_private & OPpCONST_ARYBASE)
850 /* don't warn on optimised away booleans, eg
851 * use constant Foo, 5; Foo || print; */
852 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
854 /* the constants 0 and 1 are permitted as they are
855 conventionally used as dummies in constructs like
856 1 while some_condition_with_side_effects; */
857 else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
859 else if (SvPOK(sv)) {
860 /* perl4's way of mixing documentation and code
861 (before the invention of POD) was based on a
862 trick to mix nroff and perl code. The trick was
863 built upon these three nroff macros being used in
864 void context. The pink camel has the details in
865 the script wrapman near page 319. */
866 const char * const maybe_macro = SvPVX_const(sv);
867 if (strnEQ(maybe_macro, "di", 2) ||
868 strnEQ(maybe_macro, "ds", 2) ||
869 strnEQ(maybe_macro, "ig", 2))
874 op_null(o); /* don't execute or even remember it */
878 o->op_type = OP_PREINC; /* pre-increment is faster */
879 o->op_ppaddr = PL_ppaddr[OP_PREINC];
883 o->op_type = OP_PREDEC; /* pre-decrement is faster */
884 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
888 o->op_type = OP_I_PREINC; /* pre-increment is faster */
889 o->op_ppaddr = PL_ppaddr[OP_I_PREINC];
893 o->op_type = OP_I_PREDEC; /* pre-decrement is faster */
894 o->op_ppaddr = PL_ppaddr[OP_I_PREDEC];
903 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
908 if (o->op_flags & OPf_STACKED)
915 if (!(o->op_flags & OPf_KIDS))
926 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
933 /* all requires must return a boolean value */
934 o->op_flags &= ~OPf_WANT;
939 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
940 if (!kPMOP->op_pmreplroot)
941 deprecate_old("implicit split to @_");
945 if (useless && ckWARN(WARN_VOID))
946 Perl_warner(aTHX_ packWARN(WARN_VOID), "Useless use of %s in void context", useless);
951 Perl_listkids(pTHX_ OP *o)
953 if (o && o->op_flags & OPf_KIDS) {
955 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
962 Perl_list(pTHX_ OP *o)
967 /* assumes no premature commitment */
968 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
969 || o->op_type == OP_RETURN)
974 if ((o->op_private & OPpTARGET_MY)
975 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
977 return o; /* As if inside SASSIGN */
980 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
982 switch (o->op_type) {
985 list(cBINOPo->op_first);
990 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
998 if (!(o->op_flags & OPf_KIDS))
1000 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1001 list(cBINOPo->op_first);
1002 return gen_constant_list(o);
1009 kid = cLISTOPo->op_first;
1011 while ((kid = kid->op_sibling)) {
1012 if (kid->op_sibling)
1017 WITH_THR(PL_curcop = &PL_compiling);
1021 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1022 if (kid->op_sibling)
1027 WITH_THR(PL_curcop = &PL_compiling);
1030 /* all requires must return a boolean value */
1031 o->op_flags &= ~OPf_WANT;
1038 Perl_scalarseq(pTHX_ OP *o)
1042 const OPCODE type = o->op_type;
1044 if (type == OP_LINESEQ || type == OP_SCOPE ||
1045 type == OP_LEAVE || type == OP_LEAVETRY)
1048 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1049 if (kid->op_sibling) {
1053 PL_curcop = &PL_compiling;
1055 o->op_flags &= ~OPf_PARENS;
1056 if (PL_hints & HINT_BLOCK_SCOPE)
1057 o->op_flags |= OPf_PARENS;
1060 o = newOP(OP_STUB, 0);
1065 S_modkids(pTHX_ OP *o, I32 type)
1067 if (o && o->op_flags & OPf_KIDS) {
1069 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1075 /* Propagate lvalue ("modifiable") context to an op and its children.
1076 * 'type' represents the context type, roughly based on the type of op that
1077 * would do the modifying, although local() is represented by OP_NULL.
1078 * It's responsible for detecting things that can't be modified, flag
1079 * things that need to behave specially in an lvalue context (e.g., "$$x = 5"
1080 * might have to vivify a reference in $x), and so on.
1082 * For example, "$a+1 = 2" would cause mod() to be called with o being
1083 * OP_ADD and type being OP_SASSIGN, and would output an error.
1087 Perl_mod(pTHX_ OP *o, I32 type)
1091 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
1094 if (!o || PL_error_count)
1097 if ((o->op_private & OPpTARGET_MY)
1098 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1103 switch (o->op_type) {
1109 if (!(o->op_private & OPpCONST_ARYBASE))
1112 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1113 CopARYBASE_set(&PL_compiling,
1114 (I32)SvIV(cSVOPx(PL_eval_start)->op_sv));
1118 SAVECOPARYBASE(&PL_compiling);
1119 CopARYBASE_set(&PL_compiling, 0);
1121 else if (type == OP_REFGEN)
1124 Perl_croak(aTHX_ "That use of $[ is unsupported");
1127 if (o->op_flags & OPf_PARENS || PL_madskills)
1131 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1132 !(o->op_flags & OPf_STACKED)) {
1133 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1134 /* The default is to set op_private to the number of children,
1135 which for a UNOP such as RV2CV is always 1. And w're using
1136 the bit for a flag in RV2CV, so we need it clear. */
1137 o->op_private &= ~1;
1138 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1139 assert(cUNOPo->op_first->op_type == OP_NULL);
1140 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1143 else if (o->op_private & OPpENTERSUB_NOMOD)
1145 else { /* lvalue subroutine call */
1146 o->op_private |= OPpLVAL_INTRO;
1147 PL_modcount = RETURN_UNLIMITED_NUMBER;
1148 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
1149 /* Backward compatibility mode: */
1150 o->op_private |= OPpENTERSUB_INARGS;
1153 else { /* Compile-time error message: */
1154 OP *kid = cUNOPo->op_first;
1158 if (kid->op_type != OP_PUSHMARK) {
1159 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1161 "panic: unexpected lvalue entersub "
1162 "args: type/targ %ld:%"UVuf,
1163 (long)kid->op_type, (UV)kid->op_targ);
1164 kid = kLISTOP->op_first;
1166 while (kid->op_sibling)
1167 kid = kid->op_sibling;
1168 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1170 if (kid->op_type == OP_METHOD_NAMED
1171 || kid->op_type == OP_METHOD)
1175 NewOp(1101, newop, 1, UNOP);
1176 newop->op_type = OP_RV2CV;
1177 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1178 newop->op_first = NULL;
1179 newop->op_next = (OP*)newop;
1180 kid->op_sibling = (OP*)newop;
1181 newop->op_private |= OPpLVAL_INTRO;
1182 newop->op_private &= ~1;
1186 if (kid->op_type != OP_RV2CV)
1188 "panic: unexpected lvalue entersub "
1189 "entry via type/targ %ld:%"UVuf,
1190 (long)kid->op_type, (UV)kid->op_targ);
1191 kid->op_private |= OPpLVAL_INTRO;
1192 break; /* Postpone until runtime */
1196 kid = kUNOP->op_first;
1197 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1198 kid = kUNOP->op_first;
1199 if (kid->op_type == OP_NULL)
1201 "Unexpected constant lvalue entersub "
1202 "entry via type/targ %ld:%"UVuf,
1203 (long)kid->op_type, (UV)kid->op_targ);
1204 if (kid->op_type != OP_GV) {
1205 /* Restore RV2CV to check lvalueness */
1207 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1208 okid->op_next = kid->op_next;
1209 kid->op_next = okid;
1212 okid->op_next = NULL;
1213 okid->op_type = OP_RV2CV;
1215 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1216 okid->op_private |= OPpLVAL_INTRO;
1217 okid->op_private &= ~1;
1221 cv = GvCV(kGVOP_gv);
1231 /* grep, foreach, subcalls, refgen */
1232 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1234 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1235 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1237 : (o->op_type == OP_ENTERSUB
1238 ? "non-lvalue subroutine call"
1240 type ? PL_op_desc[type] : "local"));
1254 case OP_RIGHT_SHIFT:
1263 if (!(o->op_flags & OPf_STACKED))
1270 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1276 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1277 PL_modcount = RETURN_UNLIMITED_NUMBER;
1278 return o; /* Treat \(@foo) like ordinary list. */
1282 if (scalar_mod_type(o, type))
1284 ref(cUNOPo->op_first, o->op_type);
1288 if (type == OP_LEAVESUBLV)
1289 o->op_private |= OPpMAYBE_LVSUB;
1295 PL_modcount = RETURN_UNLIMITED_NUMBER;
1298 ref(cUNOPo->op_first, o->op_type);
1303 PL_hints |= HINT_BLOCK_SCOPE;
1318 PL_modcount = RETURN_UNLIMITED_NUMBER;
1319 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1320 return o; /* Treat \(@foo) like ordinary list. */
1321 if (scalar_mod_type(o, type))
1323 if (type == OP_LEAVESUBLV)
1324 o->op_private |= OPpMAYBE_LVSUB;
1328 if (!type) /* local() */
1329 Perl_croak(aTHX_ "Can't localize lexical variable %s",
1330 PAD_COMPNAME_PV(o->op_targ));
1338 if (type != OP_SASSIGN)
1342 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1347 if (type == OP_LEAVESUBLV)
1348 o->op_private |= OPpMAYBE_LVSUB;
1350 pad_free(o->op_targ);
1351 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1352 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1353 if (o->op_flags & OPf_KIDS)
1354 mod(cBINOPo->op_first->op_sibling, type);
1359 ref(cBINOPo->op_first, o->op_type);
1360 if (type == OP_ENTERSUB &&
1361 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1362 o->op_private |= OPpLVAL_DEFER;
1363 if (type == OP_LEAVESUBLV)
1364 o->op_private |= OPpMAYBE_LVSUB;
1374 if (o->op_flags & OPf_KIDS)
1375 mod(cLISTOPo->op_last, type);
1380 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1382 else if (!(o->op_flags & OPf_KIDS))
1384 if (o->op_targ != OP_LIST) {
1385 mod(cBINOPo->op_first, type);
1391 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1396 if (type != OP_LEAVESUBLV)
1398 break; /* mod()ing was handled by ck_return() */
1401 /* [20011101.069] File test operators interpret OPf_REF to mean that
1402 their argument is a filehandle; thus \stat(".") should not set
1404 if (type == OP_REFGEN &&
1405 PL_check[o->op_type] == MEMBER_TO_FPTR(Perl_ck_ftst))
1408 if (type != OP_LEAVESUBLV)
1409 o->op_flags |= OPf_MOD;
1411 if (type == OP_AASSIGN || type == OP_SASSIGN)
1412 o->op_flags |= OPf_SPECIAL|OPf_REF;
1413 else if (!type) { /* local() */
1416 o->op_private |= OPpLVAL_INTRO;
1417 o->op_flags &= ~OPf_SPECIAL;
1418 PL_hints |= HINT_BLOCK_SCOPE;
1423 if (ckWARN(WARN_SYNTAX)) {
1424 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1425 "Useless localization of %s", OP_DESC(o));
1429 else if (type != OP_GREPSTART && type != OP_ENTERSUB
1430 && type != OP_LEAVESUBLV)
1431 o->op_flags |= OPf_REF;
1436 S_scalar_mod_type(const OP *o, I32 type)
1440 if (o->op_type == OP_RV2GV)
1464 case OP_RIGHT_SHIFT:
1483 S_is_handle_constructor(const OP *o, I32 numargs)
1485 switch (o->op_type) {
1493 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1506 Perl_refkids(pTHX_ OP *o, I32 type)
1508 if (o && o->op_flags & OPf_KIDS) {
1510 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1517 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
1522 if (!o || PL_error_count)
1525 switch (o->op_type) {
1527 if ((type == OP_EXISTS || type == OP_DEFINED || type == OP_LOCK) &&
1528 !(o->op_flags & OPf_STACKED)) {
1529 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1530 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1531 assert(cUNOPo->op_first->op_type == OP_NULL);
1532 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1533 o->op_flags |= OPf_SPECIAL;
1534 o->op_private &= ~1;
1539 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1540 doref(kid, type, set_op_ref);
1543 if (type == OP_DEFINED)
1544 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1545 doref(cUNOPo->op_first, o->op_type, set_op_ref);
1548 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1549 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1550 : type == OP_RV2HV ? OPpDEREF_HV
1552 o->op_flags |= OPf_MOD;
1557 o->op_flags |= OPf_MOD; /* XXX ??? */
1563 o->op_flags |= OPf_REF;
1566 if (type == OP_DEFINED)
1567 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1568 doref(cUNOPo->op_first, o->op_type, set_op_ref);
1574 o->op_flags |= OPf_REF;
1579 if (!(o->op_flags & OPf_KIDS))
1581 doref(cBINOPo->op_first, type, set_op_ref);
1585 doref(cBINOPo->op_first, o->op_type, set_op_ref);
1586 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1587 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1588 : type == OP_RV2HV ? OPpDEREF_HV
1590 o->op_flags |= OPf_MOD;
1600 if (!(o->op_flags & OPf_KIDS))
1602 doref(cLISTOPo->op_last, type, set_op_ref);
1612 S_dup_attrlist(pTHX_ OP *o)
1617 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1618 * where the first kid is OP_PUSHMARK and the remaining ones
1619 * are OP_CONST. We need to push the OP_CONST values.
1621 if (o->op_type == OP_CONST)
1622 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
1624 else if (o->op_type == OP_NULL)
1628 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1630 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1631 if (o->op_type == OP_CONST)
1632 rop = append_elem(OP_LIST, rop,
1633 newSVOP(OP_CONST, o->op_flags,
1634 SvREFCNT_inc_NN(cSVOPo->op_sv)));
1641 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs, bool for_my)
1646 /* fake up C<use attributes $pkg,$rv,@attrs> */
1647 ENTER; /* need to protect against side-effects of 'use' */
1649 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
1651 #define ATTRSMODULE "attributes"
1652 #define ATTRSMODULE_PM "attributes.pm"
1655 /* Don't force the C<use> if we don't need it. */
1656 SV * const * const svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
1657 if (svp && *svp != &PL_sv_undef)
1658 NOOP; /* already in %INC */
1660 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
1661 newSVpvs(ATTRSMODULE), NULL);
1664 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1665 newSVpvs(ATTRSMODULE),
1667 prepend_elem(OP_LIST,
1668 newSVOP(OP_CONST, 0, stashsv),
1669 prepend_elem(OP_LIST,
1670 newSVOP(OP_CONST, 0,
1672 dup_attrlist(attrs))));
1678 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
1681 OP *pack, *imop, *arg;
1687 assert(target->op_type == OP_PADSV ||
1688 target->op_type == OP_PADHV ||
1689 target->op_type == OP_PADAV);
1691 /* Ensure that attributes.pm is loaded. */
1692 apply_attrs(stash, PAD_SV(target->op_targ), attrs, TRUE);
1694 /* Need package name for method call. */
1695 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
1697 /* Build up the real arg-list. */
1698 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
1700 arg = newOP(OP_PADSV, 0);
1701 arg->op_targ = target->op_targ;
1702 arg = prepend_elem(OP_LIST,
1703 newSVOP(OP_CONST, 0, stashsv),
1704 prepend_elem(OP_LIST,
1705 newUNOP(OP_REFGEN, 0,
1706 mod(arg, OP_REFGEN)),
1707 dup_attrlist(attrs)));
1709 /* Fake up a method call to import */
1710 meth = newSVpvs_share("import");
1711 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
1712 append_elem(OP_LIST,
1713 prepend_elem(OP_LIST, pack, list(arg)),
1714 newSVOP(OP_METHOD_NAMED, 0, meth)));
1715 imop->op_private |= OPpENTERSUB_NOMOD;
1717 /* Combine the ops. */
1718 *imopsp = append_elem(OP_LIST, *imopsp, imop);
1722 =notfor apidoc apply_attrs_string
1724 Attempts to apply a list of attributes specified by the C<attrstr> and
1725 C<len> arguments to the subroutine identified by the C<cv> argument which
1726 is expected to be associated with the package identified by the C<stashpv>
1727 argument (see L<attributes>). It gets this wrong, though, in that it
1728 does not correctly identify the boundaries of the individual attribute
1729 specifications within C<attrstr>. This is not really intended for the
1730 public API, but has to be listed here for systems such as AIX which
1731 need an explicit export list for symbols. (It's called from XS code
1732 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
1733 to respect attribute syntax properly would be welcome.
1739 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
1740 const char *attrstr, STRLEN len)
1745 len = strlen(attrstr);
1749 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
1751 const char * const sstr = attrstr;
1752 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
1753 attrs = append_elem(OP_LIST, attrs,
1754 newSVOP(OP_CONST, 0,
1755 newSVpvn(sstr, attrstr-sstr)));
1759 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1760 newSVpvs(ATTRSMODULE),
1761 NULL, prepend_elem(OP_LIST,
1762 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
1763 prepend_elem(OP_LIST,
1764 newSVOP(OP_CONST, 0,
1770 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
1775 if (!o || PL_error_count)
1779 if (PL_madskills && type == OP_NULL && o->op_flags & OPf_KIDS) {
1780 (void)my_kid(cUNOPo->op_first, attrs, imopsp);
1784 if (type == OP_LIST) {
1786 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1787 my_kid(kid, attrs, imopsp);
1788 } else if (type == OP_UNDEF
1794 } else if (type == OP_RV2SV || /* "our" declaration */
1796 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
1797 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
1798 yyerror(Perl_form(aTHX_ "Can't declare %s in %s",
1800 PL_in_my == KEY_our ? "our" : PL_in_my == KEY_state ? "state" : "my"));
1802 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
1804 PL_in_my_stash = NULL;
1805 apply_attrs(GvSTASH(gv),
1806 (type == OP_RV2SV ? GvSV(gv) :
1807 type == OP_RV2AV ? (SV*)GvAV(gv) :
1808 type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)gv),
1811 o->op_private |= OPpOUR_INTRO;
1814 else if (type != OP_PADSV &&
1817 type != OP_PUSHMARK)
1819 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
1821 PL_in_my == KEY_our ? "our" : PL_in_my == KEY_state ? "state" : "my"));
1824 else if (attrs && type != OP_PUSHMARK) {
1828 PL_in_my_stash = NULL;
1830 /* check for C<my Dog $spot> when deciding package */
1831 stash = PAD_COMPNAME_TYPE(o->op_targ);
1833 stash = PL_curstash;
1834 apply_attrs_my(stash, o, attrs, imopsp);
1836 o->op_flags |= OPf_MOD;
1837 o->op_private |= OPpLVAL_INTRO;
1838 if (PL_in_my == KEY_state)
1839 o->op_private |= OPpPAD_STATE;
1844 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1848 int maybe_scalar = 0;
1850 /* [perl #17376]: this appears to be premature, and results in code such as
1851 C< our(%x); > executing in list mode rather than void mode */
1853 if (o->op_flags & OPf_PARENS)
1863 o = my_kid(o, attrs, &rops);
1865 if (maybe_scalar && o->op_type == OP_PADSV) {
1866 o = scalar(append_list(OP_LIST, (LISTOP*)rops, (LISTOP*)o));
1867 o->op_private |= OPpLVAL_INTRO;
1870 o = append_list(OP_LIST, (LISTOP*)o, (LISTOP*)rops);
1873 PL_in_my_stash = NULL;
1878 Perl_my(pTHX_ OP *o)
1880 return my_attrs(o, NULL);
1884 Perl_sawparens(pTHX_ OP *o)
1886 PERL_UNUSED_CONTEXT;
1888 o->op_flags |= OPf_PARENS;
1893 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
1897 const OPCODE ltype = left->op_type;
1898 const OPCODE rtype = right->op_type;
1900 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
1901 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
1903 const char * const desc
1904 = PL_op_desc[(rtype == OP_SUBST || rtype == OP_TRANS)
1905 ? rtype : OP_MATCH];
1906 const char * const sample = ((ltype == OP_RV2AV || ltype == OP_PADAV)
1907 ? "@array" : "%hash");
1908 Perl_warner(aTHX_ packWARN(WARN_MISC),
1909 "Applying %s to %s will act on scalar(%s)",
1910 desc, sample, sample);
1913 if (rtype == OP_CONST &&
1914 cSVOPx(right)->op_private & OPpCONST_BARE &&
1915 cSVOPx(right)->op_private & OPpCONST_STRICT)
1917 no_bareword_allowed(right);
1920 ismatchop = rtype == OP_MATCH ||
1921 rtype == OP_SUBST ||
1923 if (ismatchop && right->op_private & OPpTARGET_MY) {
1925 right->op_private &= ~OPpTARGET_MY;
1927 if (!(right->op_flags & OPf_STACKED) && ismatchop) {
1930 right->op_flags |= OPf_STACKED;
1931 if (rtype != OP_MATCH &&
1932 ! (rtype == OP_TRANS &&
1933 right->op_private & OPpTRANS_IDENTICAL))
1934 newleft = mod(left, rtype);
1937 if (right->op_type == OP_TRANS)
1938 o = newBINOP(OP_NULL, OPf_STACKED, scalar(newleft), right);
1940 o = prepend_elem(rtype, scalar(newleft), right);
1942 return newUNOP(OP_NOT, 0, scalar(o));
1946 return bind_match(type, left,
1947 pmruntime(newPMOP(OP_MATCH, 0), right, 0));
1951 Perl_invert(pTHX_ OP *o)
1955 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1959 Perl_scope(pTHX_ OP *o)
1963 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
1964 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1965 o->op_type = OP_LEAVE;
1966 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
1968 else if (o->op_type == OP_LINESEQ) {
1970 o->op_type = OP_SCOPE;
1971 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
1972 kid = ((LISTOP*)o)->op_first;
1973 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
1976 /* The following deals with things like 'do {1 for 1}' */
1977 kid = kid->op_sibling;
1979 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
1984 o = newLISTOP(OP_SCOPE, 0, o, NULL);
1990 Perl_block_start(pTHX_ int full)
1993 const int retval = PL_savestack_ix;
1994 pad_block_start(full);
1996 PL_hints &= ~HINT_BLOCK_SCOPE;
1997 SAVECOMPILEWARNINGS();
1998 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
1999 SAVESPTR(PL_compiling.cop_io);
2000 if (! specialCopIO(PL_compiling.cop_io)) {
2001 PL_compiling.cop_io = newSVsv(PL_compiling.cop_io) ;
2002 SAVEFREESV(PL_compiling.cop_io) ;
2008 Perl_block_end(pTHX_ I32 floor, OP *seq)
2011 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
2012 OP* const retval = scalarseq(seq);
2014 CopHINTS_set(&PL_compiling, PL_hints);
2016 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
2025 const PADOFFSET offset = pad_findmy("$_");
2026 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
2027 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
2030 OP * const o = newOP(OP_PADSV, 0);
2031 o->op_targ = offset;
2037 Perl_newPROG(pTHX_ OP *o)
2043 PL_eval_root = newUNOP(OP_LEAVEEVAL,
2044 ((PL_in_eval & EVAL_KEEPERR)
2045 ? OPf_SPECIAL : 0), o);
2046 PL_eval_start = linklist(PL_eval_root);
2047 PL_eval_root->op_private |= OPpREFCOUNTED;
2048 OpREFCNT_set(PL_eval_root, 1);
2049 PL_eval_root->op_next = 0;
2050 CALL_PEEP(PL_eval_start);
2053 if (o->op_type == OP_STUB) {
2054 PL_comppad_name = 0;
2059 PL_main_root = scope(sawparens(scalarvoid(o)));
2060 PL_curcop = &PL_compiling;
2061 PL_main_start = LINKLIST(PL_main_root);
2062 PL_main_root->op_private |= OPpREFCOUNTED;
2063 OpREFCNT_set(PL_main_root, 1);
2064 PL_main_root->op_next = 0;
2065 CALL_PEEP(PL_main_start);
2068 /* Register with debugger */
2070 CV * const cv = get_cv("DB::postponed", FALSE);
2074 XPUSHs((SV*)CopFILEGV(&PL_compiling));
2076 call_sv((SV*)cv, G_DISCARD);
2083 Perl_localize(pTHX_ OP *o, I32 lex)
2086 if (o->op_flags & OPf_PARENS)
2087 /* [perl #17376]: this appears to be premature, and results in code such as
2088 C< our(%x); > executing in list mode rather than void mode */
2095 if ( PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ','
2096 && ckWARN(WARN_PARENTHESIS))
2098 char *s = PL_bufptr;
2101 /* some heuristics to detect a potential error */
2102 while (*s && (strchr(", \t\n", *s)))
2106 if (*s && strchr("@$%*", *s) && *++s
2107 && (isALNUM(*s) || UTF8_IS_CONTINUED(*s))) {
2110 while (*s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s)))
2112 while (*s && (strchr(", \t\n", *s)))
2118 if (sigil && (*s == ';' || *s == '=')) {
2119 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
2120 "Parentheses missing around \"%s\" list",
2121 lex ? (PL_in_my == KEY_our ? "our" : PL_in_my == KEY_state ? "state" : "my")
2129 o = mod(o, OP_NULL); /* a bit kludgey */
2131 PL_in_my_stash = NULL;
2136 Perl_jmaybe(pTHX_ OP *o)
2138 if (o->op_type == OP_LIST) {
2140 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
2141 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2147 Perl_fold_constants(pTHX_ register OP *o)
2152 I32 type = o->op_type;
2159 if (PL_opargs[type] & OA_RETSCALAR)
2161 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2162 o->op_targ = pad_alloc(type, SVs_PADTMP);
2164 /* integerize op, unless it happens to be C<-foo>.
2165 * XXX should pp_i_negate() do magic string negation instead? */
2166 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2167 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2168 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2170 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2173 if (!(PL_opargs[type] & OA_FOLDCONST))
2178 /* XXX might want a ck_negate() for this */
2179 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2190 /* XXX what about the numeric ops? */
2191 if (PL_hints & HINT_LOCALE)
2196 goto nope; /* Don't try to run w/ errors */
2198 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2199 const OPCODE type = curop->op_type;
2200 if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
2202 type != OP_SCALAR &&
2204 type != OP_PUSHMARK)
2210 curop = LINKLIST(o);
2211 old_next = o->op_next;
2215 oldscope = PL_scopestack_ix;
2216 create_eval_scope(G_FAKINGEVAL);
2223 sv = *(PL_stack_sp--);
2224 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
2225 pad_swipe(o->op_targ, FALSE);
2226 else if (SvTEMP(sv)) { /* grab mortal temp? */
2227 SvREFCNT_inc_simple_void(sv);
2232 /* my_exit() was called; propagate it */
2237 /* Something tried to die. Abandon constant folding. */
2238 /* Pretend the error never happened. */
2239 sv_setpvn(ERRSV,"",0);
2240 o->op_next = old_next;
2244 /* Don't expect 1 (setjmp failed) */
2245 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
2250 if (PL_scopestack_ix > oldscope)
2251 delete_eval_scope();
2260 if (type == OP_RV2GV)
2261 newop = newGVOP(OP_GV, 0, (GV*)sv);
2263 newop = newSVOP(OP_CONST, 0, sv);
2264 op_getmad(o,newop,'f');
2272 Perl_gen_constant_list(pTHX_ register OP *o)
2276 const I32 oldtmps_floor = PL_tmps_floor;
2280 return o; /* Don't attempt to run with errors */
2282 PL_op = curop = LINKLIST(o);
2289 PL_tmps_floor = oldtmps_floor;
2291 o->op_type = OP_RV2AV;
2292 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2293 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
2294 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
2295 o->op_opt = 0; /* needs to be revisited in peep() */
2296 curop = ((UNOP*)o)->op_first;
2297 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc_NN(*PL_stack_sp--));
2299 op_getmad(curop,o,'O');
2308 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2311 if (!o || o->op_type != OP_LIST)
2312 o = newLISTOP(OP_LIST, 0, o, NULL);
2314 o->op_flags &= ~OPf_WANT;
2316 if (!(PL_opargs[type] & OA_MARK))
2317 op_null(cLISTOPo->op_first);
2319 o->op_type = (OPCODE)type;
2320 o->op_ppaddr = PL_ppaddr[type];
2321 o->op_flags |= flags;
2323 o = CHECKOP(type, o);
2324 if (o->op_type != (unsigned)type)
2327 return fold_constants(o);
2330 /* List constructors */
2333 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2341 if (first->op_type != (unsigned)type
2342 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
2344 return newLISTOP(type, 0, first, last);
2347 if (first->op_flags & OPf_KIDS)
2348 ((LISTOP*)first)->op_last->op_sibling = last;
2350 first->op_flags |= OPf_KIDS;
2351 ((LISTOP*)first)->op_first = last;
2353 ((LISTOP*)first)->op_last = last;
2358 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2366 if (first->op_type != (unsigned)type)
2367 return prepend_elem(type, (OP*)first, (OP*)last);
2369 if (last->op_type != (unsigned)type)
2370 return append_elem(type, (OP*)first, (OP*)last);
2372 first->op_last->op_sibling = last->op_first;
2373 first->op_last = last->op_last;
2374 first->op_flags |= (last->op_flags & OPf_KIDS);
2377 if (last->op_first && first->op_madprop) {
2378 MADPROP *mp = last->op_first->op_madprop;
2380 while (mp->mad_next)
2382 mp->mad_next = first->op_madprop;
2385 last->op_first->op_madprop = first->op_madprop;
2388 first->op_madprop = last->op_madprop;
2389 last->op_madprop = 0;
2398 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2406 if (last->op_type == (unsigned)type) {
2407 if (type == OP_LIST) { /* already a PUSHMARK there */
2408 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2409 ((LISTOP*)last)->op_first->op_sibling = first;
2410 if (!(first->op_flags & OPf_PARENS))
2411 last->op_flags &= ~OPf_PARENS;
2414 if (!(last->op_flags & OPf_KIDS)) {
2415 ((LISTOP*)last)->op_last = first;
2416 last->op_flags |= OPf_KIDS;
2418 first->op_sibling = ((LISTOP*)last)->op_first;
2419 ((LISTOP*)last)->op_first = first;
2421 last->op_flags |= OPf_KIDS;
2425 return newLISTOP(type, 0, first, last);
2433 Perl_newTOKEN(pTHX_ I32 optype, YYSTYPE lval, MADPROP* madprop)
2436 Newxz(tk, 1, TOKEN);
2437 tk->tk_type = (OPCODE)optype;
2438 tk->tk_type = 12345;
2440 tk->tk_mad = madprop;
2445 Perl_token_free(pTHX_ TOKEN* tk)
2447 if (tk->tk_type != 12345)
2449 mad_free(tk->tk_mad);
2454 Perl_token_getmad(pTHX_ TOKEN* tk, OP* o, char slot)
2458 if (tk->tk_type != 12345) {
2459 Perl_warner(aTHX_ packWARN(WARN_MISC),
2460 "Invalid TOKEN object ignored");
2467 /* faked up qw list? */
2469 tm->mad_type == MAD_SV &&
2470 SvPVX((SV*)tm->mad_val)[0] == 'q')
2477 /* pretend constant fold didn't happen? */
2478 if (mp->mad_key == 'f' &&
2479 (o->op_type == OP_CONST ||
2480 o->op_type == OP_GV) )
2482 token_getmad(tk,(OP*)mp->mad_val,slot);
2496 if (mp->mad_key == 'X')
2497 mp->mad_key = slot; /* just change the first one */
2507 Perl_op_getmad_weak(pTHX_ OP* from, OP* o, char slot)
2516 /* pretend constant fold didn't happen? */
2517 if (mp->mad_key == 'f' &&
2518 (o->op_type == OP_CONST ||
2519 o->op_type == OP_GV) )
2521 op_getmad(from,(OP*)mp->mad_val,slot);
2528 mp->mad_next = newMADPROP(slot,MAD_OP,from,0);
2531 o->op_madprop = newMADPROP(slot,MAD_OP,from,0);
2537 Perl_op_getmad(pTHX_ OP* from, OP* o, char slot)
2546 /* pretend constant fold didn't happen? */
2547 if (mp->mad_key == 'f' &&
2548 (o->op_type == OP_CONST ||
2549 o->op_type == OP_GV) )
2551 op_getmad(from,(OP*)mp->mad_val,slot);
2558 mp->mad_next = newMADPROP(slot,MAD_OP,from,1);
2561 o->op_madprop = newMADPROP(slot,MAD_OP,from,1);
2565 PerlIO_printf(PerlIO_stderr(),
2566 "DESTROYING op = %0"UVxf"\n", PTR2UV(from));
2572 Perl_prepend_madprops(pTHX_ MADPROP* mp, OP* o, char slot)
2590 Perl_append_madprops(pTHX_ MADPROP* tm, OP* o, char slot)
2594 addmad(tm, &(o->op_madprop), slot);
2598 Perl_addmad(pTHX_ MADPROP* tm, MADPROP** root, char slot)
2619 Perl_newMADsv(pTHX_ char key, SV* sv)
2621 return newMADPROP(key, MAD_SV, sv, 0);
2625 Perl_newMADPROP(pTHX_ char key, char type, void* val, I32 vlen)
2628 Newxz(mp, 1, MADPROP);
2631 mp->mad_vlen = vlen;
2632 mp->mad_type = type;
2634 /* PerlIO_printf(PerlIO_stderr(), "NEW mp = %0x\n", mp); */
2639 Perl_mad_free(pTHX_ MADPROP* mp)
2641 /* PerlIO_printf(PerlIO_stderr(), "FREE mp = %0x\n", mp); */
2645 mad_free(mp->mad_next);
2646 /* if (PL_lex_state != LEX_NOTPARSING && mp->mad_vlen)
2647 PerlIO_printf(PerlIO_stderr(), "DESTROYING '%c'=<%s>\n", mp->mad_key & 255, mp->mad_val); */
2648 switch (mp->mad_type) {
2652 Safefree((char*)mp->mad_val);
2655 if (mp->mad_vlen) /* vlen holds "strong/weak" boolean */
2656 op_free((OP*)mp->mad_val);
2659 sv_free((SV*)mp->mad_val);
2662 PerlIO_printf(PerlIO_stderr(), "Unrecognized mad\n");
2671 Perl_newNULLLIST(pTHX)
2673 return newOP(OP_STUB, 0);
2677 Perl_force_list(pTHX_ OP *o)
2679 if (!o || o->op_type != OP_LIST)
2680 o = newLISTOP(OP_LIST, 0, o, NULL);
2686 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2691 NewOp(1101, listop, 1, LISTOP);
2693 listop->op_type = (OPCODE)type;
2694 listop->op_ppaddr = PL_ppaddr[type];
2697 listop->op_flags = (U8)flags;
2701 else if (!first && last)
2704 first->op_sibling = last;
2705 listop->op_first = first;
2706 listop->op_last = last;
2707 if (type == OP_LIST) {
2708 OP* const pushop = newOP(OP_PUSHMARK, 0);
2709 pushop->op_sibling = first;
2710 listop->op_first = pushop;
2711 listop->op_flags |= OPf_KIDS;
2713 listop->op_last = pushop;
2716 return CHECKOP(type, listop);
2720 Perl_newOP(pTHX_ I32 type, I32 flags)
2724 NewOp(1101, o, 1, OP);
2725 o->op_type = (OPCODE)type;
2726 o->op_ppaddr = PL_ppaddr[type];
2727 o->op_flags = (U8)flags;
2730 o->op_private = (U8)(0 | (flags >> 8));
2731 if (PL_opargs[type] & OA_RETSCALAR)
2733 if (PL_opargs[type] & OA_TARGET)
2734 o->op_targ = pad_alloc(type, SVs_PADTMP);
2735 return CHECKOP(type, o);
2739 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
2745 first = newOP(OP_STUB, 0);
2746 if (PL_opargs[type] & OA_MARK)
2747 first = force_list(first);
2749 NewOp(1101, unop, 1, UNOP);
2750 unop->op_type = (OPCODE)type;
2751 unop->op_ppaddr = PL_ppaddr[type];
2752 unop->op_first = first;
2753 unop->op_flags = (U8)(flags | OPf_KIDS);
2754 unop->op_private = (U8)(1 | (flags >> 8));
2755 unop = (UNOP*) CHECKOP(type, unop);
2759 return fold_constants((OP *) unop);
2763 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2767 NewOp(1101, binop, 1, BINOP);
2770 first = newOP(OP_NULL, 0);
2772 binop->op_type = (OPCODE)type;
2773 binop->op_ppaddr = PL_ppaddr[type];
2774 binop->op_first = first;
2775 binop->op_flags = (U8)(flags | OPf_KIDS);
2778 binop->op_private = (U8)(1 | (flags >> 8));
2781 binop->op_private = (U8)(2 | (flags >> 8));
2782 first->op_sibling = last;
2785 binop = (BINOP*)CHECKOP(type, binop);
2786 if (binop->op_next || binop->op_type != (OPCODE)type)
2789 binop->op_last = binop->op_first->op_sibling;
2791 return fold_constants((OP *)binop);
2794 static int uvcompare(const void *a, const void *b)
2795 __attribute__nonnull__(1)
2796 __attribute__nonnull__(2)
2797 __attribute__pure__;
2798 static int uvcompare(const void *a, const void *b)
2800 if (*((const UV *)a) < (*(const UV *)b))
2802 if (*((const UV *)a) > (*(const UV *)b))
2804 if (*((const UV *)a+1) < (*(const UV *)b+1))
2806 if (*((const UV *)a+1) > (*(const UV *)b+1))
2812 Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
2815 SV * const tstr = ((SVOP*)expr)->op_sv;
2816 SV * const rstr = ((SVOP*)repl)->op_sv;
2819 const U8 *t = (U8*)SvPV_const(tstr, tlen);
2820 const U8 *r = (U8*)SvPV_const(rstr, rlen);
2824 register short *tbl;
2826 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
2827 const I32 squash = o->op_private & OPpTRANS_SQUASH;
2828 I32 del = o->op_private & OPpTRANS_DELETE;
2829 PL_hints |= HINT_BLOCK_SCOPE;
2832 o->op_private |= OPpTRANS_FROM_UTF;
2835 o->op_private |= OPpTRANS_TO_UTF;
2837 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2838 SV* const listsv = newSVpvs("# comment\n");
2840 const U8* tend = t + tlen;
2841 const U8* rend = r + rlen;
2855 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2856 const I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2859 const U32 flags = UTF8_ALLOW_DEFAULT;
2863 t = tsave = bytes_to_utf8(t, &len);
2866 if (!to_utf && rlen) {
2868 r = rsave = bytes_to_utf8(r, &len);
2872 /* There are several snags with this code on EBCDIC:
2873 1. 0xFF is a legal UTF-EBCDIC byte (there are no illegal bytes).
2874 2. scan_const() in toke.c has encoded chars in native encoding which makes
2875 ranges at least in EBCDIC 0..255 range the bottom odd.
2879 U8 tmpbuf[UTF8_MAXBYTES+1];
2882 Newx(cp, 2*tlen, UV);
2884 transv = newSVpvs("");
2886 cp[2*i] = utf8n_to_uvuni(t, tend-t, &ulen, flags);
2888 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) {
2890 cp[2*i+1] = utf8n_to_uvuni(t, tend-t, &ulen, flags);
2894 cp[2*i+1] = cp[2*i];
2898 qsort(cp, i, 2*sizeof(UV), uvcompare);
2899 for (j = 0; j < i; j++) {
2901 diff = val - nextmin;
2903 t = uvuni_to_utf8(tmpbuf,nextmin);
2904 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2906 U8 range_mark = UTF_TO_NATIVE(0xff);
2907 t = uvuni_to_utf8(tmpbuf, val - 1);
2908 sv_catpvn(transv, (char *)&range_mark, 1);
2909 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2916 t = uvuni_to_utf8(tmpbuf,nextmin);
2917 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2919 U8 range_mark = UTF_TO_NATIVE(0xff);
2920 sv_catpvn(transv, (char *)&range_mark, 1);
2922 t = uvuni_to_utf8_flags(tmpbuf, 0x7fffffff,
2923 UNICODE_ALLOW_SUPER);
2924 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2925 t = (const U8*)SvPVX_const(transv);
2926 tlen = SvCUR(transv);
2930 else if (!rlen && !del) {
2931 r = t; rlen = tlen; rend = tend;
2934 if ((!rlen && !del) || t == r ||
2935 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
2937 o->op_private |= OPpTRANS_IDENTICAL;
2941 while (t < tend || tfirst <= tlast) {
2942 /* see if we need more "t" chars */
2943 if (tfirst > tlast) {
2944 tfirst = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags);
2946 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) { /* illegal utf8 val indicates range */
2948 tlast = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags);
2955 /* now see if we need more "r" chars */
2956 if (rfirst > rlast) {
2958 rfirst = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags);
2960 if (r < rend && NATIVE_TO_UTF(*r) == 0xff) { /* illegal utf8 val indicates range */
2962 rlast = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags);
2971 rfirst = rlast = 0xffffffff;
2975 /* now see which range will peter our first, if either. */
2976 tdiff = tlast - tfirst;
2977 rdiff = rlast - rfirst;
2984 if (rfirst == 0xffffffff) {
2985 diff = tdiff; /* oops, pretend rdiff is infinite */
2987 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
2988 (long)tfirst, (long)tlast);
2990 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
2994 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
2995 (long)tfirst, (long)(tfirst + diff),
2998 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
2999 (long)tfirst, (long)rfirst);
3001 if (rfirst + diff > max)
3002 max = rfirst + diff;
3004 grows = (tfirst < rfirst &&
3005 UNISKIP(tfirst) < UNISKIP(rfirst + diff));
3017 else if (max > 0xff)
3022 Safefree(cPVOPo->op_pv);
3023 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
3024 SvREFCNT_dec(listsv);
3025 SvREFCNT_dec(transv);
3027 if (!del && havefinal && rlen)
3028 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5,
3029 newSVuv((UV)final), 0);
3032 o->op_private |= OPpTRANS_GROWS;
3038 op_getmad(expr,o,'e');
3039 op_getmad(repl,o,'r');
3047 tbl = (short*)cPVOPo->op_pv;
3049 Zero(tbl, 256, short);
3050 for (i = 0; i < (I32)tlen; i++)
3052 for (i = 0, j = 0; i < 256; i++) {
3054 if (j >= (I32)rlen) {
3063 if (i < 128 && r[j] >= 128)
3073 o->op_private |= OPpTRANS_IDENTICAL;
3075 else if (j >= (I32)rlen)
3078 cPVOPo->op_pv = (char*)Renew(tbl, 0x101+rlen-j, short);
3079 tbl[0x100] = (short)(rlen - j);
3080 for (i=0; i < (I32)rlen - j; i++)
3081 tbl[0x101+i] = r[j+i];
3085 if (!rlen && !del) {
3088 o->op_private |= OPpTRANS_IDENTICAL;
3090 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
3091 o->op_private |= OPpTRANS_IDENTICAL;
3093 for (i = 0; i < 256; i++)
3095 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
3096 if (j >= (I32)rlen) {
3098 if (tbl[t[i]] == -1)
3104 if (tbl[t[i]] == -1) {
3105 if (t[i] < 128 && r[j] >= 128)
3112 o->op_private |= OPpTRANS_GROWS;
3114 op_getmad(expr,o,'e');
3115 op_getmad(repl,o,'r');
3125 Perl_newPMOP(pTHX_ I32 type, I32 flags)
3130 NewOp(1101, pmop, 1, PMOP);
3131 pmop->op_type = (OPCODE)type;
3132 pmop->op_ppaddr = PL_ppaddr[type];
3133 pmop->op_flags = (U8)flags;
3134 pmop->op_private = (U8)(0 | (flags >> 8));
3136 if (PL_hints & HINT_RE_TAINT)
3137 pmop->op_pmpermflags |= PMf_RETAINT;
3138 if (PL_hints & HINT_LOCALE)
3139 pmop->op_pmpermflags |= PMf_LOCALE;
3140 pmop->op_pmflags = pmop->op_pmpermflags;
3143 if (av_len((AV*) PL_regex_pad[0]) > -1) {
3144 SV * const repointer = av_pop((AV*)PL_regex_pad[0]);
3145 pmop->op_pmoffset = SvIV(repointer);
3146 SvREPADTMP_off(repointer);
3147 sv_setiv(repointer,0);
3149 SV * const repointer = newSViv(0);
3150 av_push(PL_regex_padav, SvREFCNT_inc_simple_NN(repointer));
3151 pmop->op_pmoffset = av_len(PL_regex_padav);
3152 PL_regex_pad = AvARRAY(PL_regex_padav);
3156 /* link into pm list */
3157 if (type != OP_TRANS && PL_curstash) {
3158 MAGIC *mg = mg_find((SV*)PL_curstash, PERL_MAGIC_symtab);
3161 mg = sv_magicext((SV*)PL_curstash, 0, PERL_MAGIC_symtab, 0, 0, 0);
3163 pmop->op_pmnext = (PMOP*)mg->mg_obj;
3164 mg->mg_obj = (SV*)pmop;
3165 PmopSTASH_set(pmop,PL_curstash);
3168 return CHECKOP(type, pmop);
3171 /* Given some sort of match op o, and an expression expr containing a
3172 * pattern, either compile expr into a regex and attach it to o (if it's
3173 * constant), or convert expr into a runtime regcomp op sequence (if it's
3176 * isreg indicates that the pattern is part of a regex construct, eg
3177 * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or
3178 * split "pattern", which aren't. In the former case, expr will be a list
3179 * if the pattern contains more than one term (eg /a$b/) or if it contains
3180 * a replacement, ie s/// or tr///.
3184 Perl_pmruntime(pTHX_ OP *o, OP *expr, bool isreg)
3189 I32 repl_has_vars = 0;
3193 if (o->op_type == OP_SUBST || o->op_type == OP_TRANS) {
3194 /* last element in list is the replacement; pop it */
3196 repl = cLISTOPx(expr)->op_last;
3197 kid = cLISTOPx(expr)->op_first;
3198 while (kid->op_sibling != repl)
3199 kid = kid->op_sibling;
3200 kid->op_sibling = NULL;
3201 cLISTOPx(expr)->op_last = kid;
3204 if (isreg && expr->op_type == OP_LIST &&
3205 cLISTOPx(expr)->op_first->op_sibling == cLISTOPx(expr)->op_last)
3207 /* convert single element list to element */
3208 OP* const oe = expr;
3209 expr = cLISTOPx(oe)->op_first->op_sibling;
3210 cLISTOPx(oe)->op_first->op_sibling = NULL;
3211 cLISTOPx(oe)->op_last = NULL;
3215 if (o->op_type == OP_TRANS) {
3216 return pmtrans(o, expr, repl);
3219 reglist = isreg && expr->op_type == OP_LIST;
3223 PL_hints |= HINT_BLOCK_SCOPE;
3226 if (expr->op_type == OP_CONST) {
3228 SV * const pat = ((SVOP*)expr)->op_sv;
3229 const char *p = SvPV_const(pat, plen);
3230 if ((o->op_flags & OPf_SPECIAL) && (*p == ' ' && p[1] == '\0')) {
3231 U32 was_readonly = SvREADONLY(pat);
3235 sv_force_normal_flags(pat, 0);
3236 assert(!SvREADONLY(pat));
3239 SvREADONLY_off(pat);
3243 sv_setpvn(pat, "\\s+", 3);
3245 SvFLAGS(pat) |= was_readonly;
3247 p = SvPV_const(pat, plen);
3248 pm->op_pmflags |= PMf_SKIPWHITE;
3251 pm->op_pmdynflags |= PMdf_UTF8;
3252 /* FIXME - can we make this function take const char * args? */
3253 PM_SETRE(pm, CALLREGCOMP(aTHX_ (char*)p, (char*)p + plen, pm));
3254 if (strEQ("\\s+", PM_GETRE(pm)->precomp))
3255 pm->op_pmflags |= PMf_WHITE;
3257 op_getmad(expr,(OP*)pm,'e');
3263 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
3264 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
3266 : OP_REGCMAYBE),0,expr);
3268 NewOp(1101, rcop, 1, LOGOP);
3269 rcop->op_type = OP_REGCOMP;
3270 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
3271 rcop->op_first = scalar(expr);
3272 rcop->op_flags |= OPf_KIDS
3273 | ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0)
3274 | (reglist ? OPf_STACKED : 0);
3275 rcop->op_private = 1;
3278 rcop->op_targ = pad_alloc(rcop->op_type, SVs_PADTMP);
3280 /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */
3283 /* establish postfix order */
3284 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
3286 rcop->op_next = expr;
3287 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
3290 rcop->op_next = LINKLIST(expr);
3291 expr->op_next = (OP*)rcop;
3294 prepend_elem(o->op_type, scalar((OP*)rcop), o);
3299 if (pm->op_pmflags & PMf_EVAL) {
3301 if (CopLINE(PL_curcop) < (line_t)PL_multi_end)
3302 CopLINE_set(PL_curcop, (line_t)PL_multi_end);
3304 else if (repl->op_type == OP_CONST)
3308 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
3309 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3310 if (curop->op_type == OP_GV) {
3311 GV * const gv = cGVOPx_gv(curop);
3313 if (strchr("&`'123456789+-\016\022", *GvENAME(gv)))
3316 else if (curop->op_type == OP_RV2CV)
3318 else if (curop->op_type == OP_RV2SV ||
3319 curop->op_type == OP_RV2AV ||
3320 curop->op_type == OP_RV2HV ||
3321 curop->op_type == OP_RV2GV) {
3322 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
3325 else if (curop->op_type == OP_PADSV ||
3326 curop->op_type == OP_PADAV ||
3327 curop->op_type == OP_PADHV ||
3328 curop->op_type == OP_PADANY) {
3331 else if (curop->op_type == OP_PUSHRE)
3332 NOOP; /* Okay here, dangerous in newASSIGNOP */
3342 || PM_GETRE(pm)->reganch & ROPT_EVAL_SEEN))) {
3343 pm->op_pmflags |= PMf_CONST; /* const for long enough */
3344 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
3345 prepend_elem(o->op_type, scalar(repl), o);
3348 if (curop == repl && !PM_GETRE(pm)) { /* Has variables. */
3349 pm->op_pmflags |= PMf_MAYBE_CONST;
3350 pm->op_pmpermflags |= PMf_MAYBE_CONST;
3352 NewOp(1101, rcop, 1, LOGOP);
3353 rcop->op_type = OP_SUBSTCONT;
3354 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
3355 rcop->op_first = scalar(repl);
3356 rcop->op_flags |= OPf_KIDS;
3357 rcop->op_private = 1;
3360 /* establish postfix order */
3361 rcop->op_next = LINKLIST(repl);
3362 repl->op_next = (OP*)rcop;
3364 pm->op_pmreplroot = scalar((OP*)rcop);
3365 pm->op_pmreplstart = LINKLIST(rcop);
3374 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
3378 NewOp(1101, svop, 1, SVOP);
3379 svop->op_type = (OPCODE)type;
3380 svop->op_ppaddr = PL_ppaddr[type];
3382 svop->op_next = (OP*)svop;
3383 svop->op_flags = (U8)flags;
3384 if (PL_opargs[type] & OA_RETSCALAR)
3386 if (PL_opargs[type] & OA_TARGET)
3387 svop->op_targ = pad_alloc(type, SVs_PADTMP);
3388 return CHECKOP(type, svop);
3392 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
3396 NewOp(1101, padop, 1, PADOP);
3397 padop->op_type = (OPCODE)type;
3398 padop->op_ppaddr = PL_ppaddr[type];
3399 padop->op_padix = pad_alloc(type, SVs_PADTMP);
3400 SvREFCNT_dec(PAD_SVl(padop->op_padix));
3401 PAD_SETSV(padop->op_padix, sv);
3404 padop->op_next = (OP*)padop;
3405 padop->op_flags = (U8)flags;
3406 if (PL_opargs[type] & OA_RETSCALAR)
3408 if (PL_opargs[type] & OA_TARGET)
3409 padop->op_targ = pad_alloc(type, SVs_PADTMP);
3410 return CHECKOP(type, padop);
3414 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
3420 return newPADOP(type, flags, SvREFCNT_inc_simple(gv));
3422 return newSVOP(type, flags, SvREFCNT_inc_simple(gv));
3427 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
3431 NewOp(1101, pvop, 1, PVOP);
3432 pvop->op_type = (OPCODE)type;
3433 pvop->op_ppaddr = PL_ppaddr[type];
3435 pvop->op_next = (OP*)pvop;
3436 pvop->op_flags = (U8)flags;
3437 if (PL_opargs[type] & OA_RETSCALAR)
3439 if (PL_opargs[type] & OA_TARGET)
3440 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
3441 return CHECKOP(type, pvop);
3449 Perl_package(pTHX_ OP *o)
3458 save_hptr(&PL_curstash);
3459 save_item(PL_curstname);
3461 name = SvPV_const(cSVOPo->op_sv, len);
3462 PL_curstash = gv_stashpvn(name, len, TRUE);
3463 sv_setpvn(PL_curstname, name, len);
3465 PL_hints |= HINT_BLOCK_SCOPE;
3466 PL_copline = NOLINE;
3472 if (!PL_madskills) {
3477 pegop = newOP(OP_NULL,0);
3478 op_getmad(o,pegop,'P');
3488 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
3495 OP *pegop = newOP(OP_NULL,0);
3498 if (idop->op_type != OP_CONST)
3499 Perl_croak(aTHX_ "Module name must be constant");
3502 op_getmad(idop,pegop,'U');
3507 SV * const vesv = ((SVOP*)version)->op_sv;
3510 op_getmad(version,pegop,'V');
3511 if (!arg && !SvNIOKp(vesv)) {
3518 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
3519 Perl_croak(aTHX_ "Version number must be constant number");
3521 /* Make copy of idop so we don't free it twice */
3522 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
3524 /* Fake up a method call to VERSION */
3525 meth = newSVpvs_share("VERSION");
3526 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3527 append_elem(OP_LIST,
3528 prepend_elem(OP_LIST, pack, list(version)),
3529 newSVOP(OP_METHOD_NAMED, 0, meth)));
3533 /* Fake up an import/unimport */
3534 if (arg && arg->op_type == OP_STUB) {
3536 op_getmad(arg,pegop,'S');
3537 imop = arg; /* no import on explicit () */
3539 else if (SvNIOKp(((SVOP*)idop)->op_sv)) {
3540 imop = NULL; /* use 5.0; */
3542 idop->op_private |= OPpCONST_NOVER;
3548 op_getmad(arg,pegop,'A');
3550 /* Make copy of idop so we don't free it twice */
3551 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
3553 /* Fake up a method call to import/unimport */
3555 ? newSVpvs_share("import") : newSVpvs_share("unimport");
3556 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3557 append_elem(OP_LIST,
3558 prepend_elem(OP_LIST, pack, list(arg)),
3559 newSVOP(OP_METHOD_NAMED, 0, meth)));
3562 /* Fake up the BEGIN {}, which does its thing immediately. */
3564 newSVOP(OP_CONST, 0, newSVpvs_share("BEGIN")),
3567 append_elem(OP_LINESEQ,
3568 append_elem(OP_LINESEQ,
3569 newSTATEOP(0, NULL, newUNOP(OP_REQUIRE, 0, idop)),
3570 newSTATEOP(0, NULL, veop)),
3571 newSTATEOP(0, NULL, imop) ));
3573 /* The "did you use incorrect case?" warning used to be here.
3574 * The problem is that on case-insensitive filesystems one
3575 * might get false positives for "use" (and "require"):
3576 * "use Strict" or "require CARP" will work. This causes
3577 * portability problems for the script: in case-strict
3578 * filesystems the script will stop working.
3580 * The "incorrect case" warning checked whether "use Foo"
3581 * imported "Foo" to your namespace, but that is wrong, too:
3582 * there is no requirement nor promise in the language that
3583 * a Foo.pm should or would contain anything in package "Foo".
3585 * There is very little Configure-wise that can be done, either:
3586 * the case-sensitivity of the build filesystem of Perl does not
3587 * help in guessing the case-sensitivity of the runtime environment.
3590 PL_hints |= HINT_BLOCK_SCOPE;
3591 PL_copline = NOLINE;
3593 PL_cop_seqmax++; /* Purely for B::*'s benefit */
3596 if (!PL_madskills) {
3597 /* FIXME - don't allocate pegop if !PL_madskills */
3606 =head1 Embedding Functions
3608 =for apidoc load_module
3610 Loads the module whose name is pointed to by the string part of name.
3611 Note that the actual module name, not its filename, should be given.
3612 Eg, "Foo::Bar" instead of "Foo/Bar.pm". flags can be any of
3613 PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS
3614 (or 0 for no flags). ver, if specified, provides version semantics
3615 similar to C<use Foo::Bar VERSION>. The optional trailing SV*
3616 arguments can be used to specify arguments to the module's import()
3617 method, similar to C<use Foo::Bar VERSION LIST>.
3622 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
3625 va_start(args, ver);
3626 vload_module(flags, name, ver, &args);
3630 #ifdef PERL_IMPLICIT_CONTEXT
3632 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
3636 va_start(args, ver);
3637 vload_module(flags, name, ver, &args);
3643 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
3648 OP * const modname = newSVOP(OP_CONST, 0, name);
3649 modname->op_private |= OPpCONST_BARE;
3651 veop = newSVOP(OP_CONST, 0, ver);
3655 if (flags & PERL_LOADMOD_NOIMPORT) {
3656 imop = sawparens(newNULLLIST());
3658 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
3659 imop = va_arg(*args, OP*);
3664 sv = va_arg(*args, SV*);
3666 imop = append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
3667 sv = va_arg(*args, SV*);
3671 const line_t ocopline = PL_copline;
3672 COP * const ocurcop = PL_curcop;
3673 const int oexpect = PL_expect;
3675 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
3676 veop, modname, imop);
3677 PL_expect = oexpect;
3678 PL_copline = ocopline;
3679 PL_curcop = ocurcop;
3684 Perl_dofile(pTHX_ OP *term, I32 force_builtin)
3690 if (!force_builtin) {
3691 gv = gv_fetchpvs("do", GV_NOTQUAL, SVt_PVCV);
3692 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
3693 GV * const * const gvp = (GV**)hv_fetchs(PL_globalstash, "do", FALSE);
3694 gv = gvp ? *gvp : NULL;
3698 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
3699 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3700 append_elem(OP_LIST, term,
3701 scalar(newUNOP(OP_RV2CV, 0,
3702 newGVOP(OP_GV, 0, gv))))));
3705 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3711 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
3713 return newBINOP(OP_LSLICE, flags,
3714 list(force_list(subscript)),
3715 list(force_list(listval)) );
3719 S_is_list_assignment(pTHX_ register const OP *o)
3727 if ((o->op_type == OP_NULL) && (o->op_flags & OPf_KIDS))
3728 o = cUNOPo->op_first;
3730 flags = o->op_flags;
3732 if (type == OP_COND_EXPR) {
3733 const I32 t = is_list_assignment(cLOGOPo->op_first->op_sibling);
3734 const I32 f = is_list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
3739 yyerror("Assignment to both a list and a scalar");
3743 if (type == OP_LIST &&
3744 (flags & OPf_WANT) == OPf_WANT_SCALAR &&
3745 o->op_private & OPpLVAL_INTRO)
3748 if (type == OP_LIST || flags & OPf_PARENS ||
3749 type == OP_RV2AV || type == OP_RV2HV ||
3750 type == OP_ASLICE || type == OP_HSLICE)
3753 if (type == OP_PADAV || type == OP_PADHV)
3756 if (type == OP_RV2SV)
3763 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
3769 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN || optype == OP_DORASSIGN) {
3770 return newLOGOP(optype, 0,
3771 mod(scalar(left), optype),
3772 newUNOP(OP_SASSIGN, 0, scalar(right)));
3775 return newBINOP(optype, OPf_STACKED,
3776 mod(scalar(left), optype), scalar(right));
3780 if (is_list_assignment(left)) {
3784 /* Grandfathering $[ assignment here. Bletch.*/
3785 /* Only simple assignments like C<< ($[) = 1 >> are allowed */
3786 PL_eval_start = (left->op_type == OP_CONST) ? right : 0;
3787 left = mod(left, OP_AASSIGN);
3790 else if (left->op_type == OP_CONST) {
3792 /* Result of assignment is always 1 (or we'd be dead already) */
3793 return newSVOP(OP_CONST, 0, newSViv(1));
3795 curop = list(force_list(left));
3796 o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
3797 o->op_private = (U8)(0 | (flags >> 8));
3799 /* PL_generation sorcery:
3800 * an assignment like ($a,$b) = ($c,$d) is easier than
3801 * ($a,$b) = ($c,$a), since there is no need for temporary vars.
3802 * To detect whether there are common vars, the global var
3803 * PL_generation is incremented for each assign op we compile.
3804 * Then, while compiling the assign op, we run through all the
3805 * variables on both sides of the assignment, setting a spare slot
3806 * in each of them to PL_generation. If any of them already have
3807 * that value, we know we've got commonality. We could use a
3808 * single bit marker, but then we'd have to make 2 passes, first
3809 * to clear the flag, then to test and set it. To find somewhere
3810 * to store these values, evil chicanery is done with SvCUR().
3813 if (!(left->op_private & OPpLVAL_INTRO)) {
3816 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3817 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3818 if (curop->op_type == OP_GV) {
3819 GV *gv = cGVOPx_gv(curop);
3821 || (int)GvASSIGN_GENERATION(gv) == PL_generation)
3823 GvASSIGN_GENERATION_set(gv, PL_generation);
3825 else if (curop->op_type == OP_PADSV ||
3826 curop->op_type == OP_PADAV ||
3827 curop->op_type == OP_PADHV ||
3828 curop->op_type == OP_PADANY)
3830 if (PAD_COMPNAME_GEN(curop->op_targ)
3831 == (STRLEN)PL_generation)
3833 PAD_COMPNAME_GEN_set(curop->op_targ, PL_generation);
3836 else if (curop->op_type == OP_RV2CV)
3838 else if (curop->op_type == OP_RV2SV ||
3839 curop->op_type == OP_RV2AV ||
3840 curop->op_type == OP_RV2HV ||
3841 curop->op_type == OP_RV2GV) {
3842 if (lastop->op_type != OP_GV) /* funny deref? */
3845 else if (curop->op_type == OP_PUSHRE) {
3846 if (((PMOP*)curop)->op_pmreplroot) {
3848 GV *gv = (GV*)PAD_SVl(INT2PTR(PADOFFSET,
3849 ((PMOP*)curop)->op_pmreplroot));
3851 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3854 || (int)GvASSIGN_GENERATION(gv) == PL_generation)
3856 GvASSIGN_GENERATION_set(gv, PL_generation);
3857 GvASSIGN_GENERATION_set(gv, PL_generation);
3866 o->op_private |= OPpASSIGN_COMMON;
3868 if (right && right->op_type == OP_SPLIT) {
3869 OP* tmpop = ((LISTOP*)right)->op_first;
3870 if (tmpop && (tmpop->op_type == OP_PUSHRE)) {
3871 PMOP * const pm = (PMOP*)tmpop;
3872 if (left->op_type == OP_RV2AV &&
3873 !(left->op_private & OPpLVAL_INTRO) &&
3874 !(o->op_private & OPpASSIGN_COMMON) )
3876 tmpop = ((UNOP*)left)->op_first;
3877 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
3879 pm->op_pmreplroot = INT2PTR(OP*, cPADOPx(tmpop)->op_padix);
3880 cPADOPx(tmpop)->op_padix = 0; /* steal it */
3882 pm->op_pmreplroot = (OP*)cSVOPx(tmpop)->op_sv;
3883 cSVOPx(tmpop)->op_sv = NULL; /* steal it */
3885 pm->op_pmflags |= PMf_ONCE;
3886 tmpop = cUNOPo->op_first; /* to list (nulled) */
3887 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3888 tmpop->op_sibling = NULL; /* don't free split */
3889 right->op_next = tmpop->op_next; /* fix starting loc */
3891 op_getmad(o,right,'R'); /* blow off assign */
3893 op_free(o); /* blow off assign */
3895 right->op_flags &= ~OPf_WANT;
3896 /* "I don't know and I don't care." */
3901 if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
3902 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3904 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3906 sv_setiv(sv, PL_modcount+1);
3914 right = newOP(OP_UNDEF, 0);
3915 if (right->op_type == OP_READLINE) {
3916 right->op_flags |= OPf_STACKED;
3917 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3920 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3921 o = newBINOP(OP_SASSIGN, flags,
3922 scalar(right), mod(scalar(left), OP_SASSIGN) );
3928 o = newSVOP(OP_CONST, 0, newSViv(CopARYBASE_get(&PL_compiling)));
3929 o->op_private |= OPpCONST_ARYBASE;
3936 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3939 const U32 seq = intro_my();
3942 NewOp(1101, cop, 1, COP);
3943 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
3944 cop->op_type = OP_DBSTATE;
3945 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3948 cop->op_type = OP_NEXTSTATE;
3949 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3951 cop->op_flags = (U8)flags;
3952 CopHINTS_set(cop, PL_hints);
3954 cop->op_private |= NATIVE_HINTS;
3956 CopHINTS_set(&PL_compiling, CopHINTS_get(cop));
3957 cop->op_next = (OP*)cop;
3960 cop->cop_label = label;
3961 PL_hints |= HINT_BLOCK_SCOPE;
3964 CopARYBASE_set(cop, CopARYBASE_get(PL_curcop));
3965 cop->cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
3966 if (specialCopIO(PL_curcop->cop_io))
3967 cop->cop_io = PL_curcop->cop_io;
3969 cop->cop_io = newSVsv(PL_curcop->cop_io) ;
3970 cop->cop_hints = PL_curcop->cop_hints;
3971 if (cop->cop_hints) {
3973 cop->cop_hints->refcounted_he_refcnt++;
3974 HINTS_REFCNT_UNLOCK;
3977 if (PL_copline == NOLINE)
3978 CopLINE_set(cop, CopLINE(PL_curcop));
3980 CopLINE_set(cop, PL_copline);
3981 PL_copline = NOLINE;
3984 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
3986 CopFILEGV_set(cop, CopFILEGV(PL_curcop));
3988 CopSTASH_set(cop, PL_curstash);
3990 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3991 SV * const * const svp = av_fetch(CopFILEAVx(PL_curcop), (I32)CopLINE(cop), FALSE);
3992 if (svp && *svp != &PL_sv_undef ) {
3993 (void)SvIOK_on(*svp);
3994 SvIV_set(*svp, PTR2IV(cop));
3998 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
4003 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
4006 return new_logop(type, flags, &first, &other);
4010 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
4015 OP *first = *firstp;
4016 OP * const other = *otherp;
4018 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
4019 return newBINOP(type, flags, scalar(first), scalar(other));
4021 scalarboolean(first);
4022 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
4023 if (first->op_type == OP_NOT
4024 && (first->op_flags & OPf_SPECIAL)
4025 && (first->op_flags & OPf_KIDS)) {
4026 if (type == OP_AND || type == OP_OR) {
4032 first = *firstp = cUNOPo->op_first;
4034 first->op_next = o->op_next;
4035 cUNOPo->op_first = NULL;
4037 op_getmad(o,first,'O');
4043 if (first->op_type == OP_CONST) {
4044 if (first->op_private & OPpCONST_STRICT)
4045 no_bareword_allowed(first);
4046 else if ((first->op_private & OPpCONST_BARE) && ckWARN(WARN_BAREWORD))
4047 Perl_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional");
4048 if ((type == OP_AND && SvTRUE(((SVOP*)first)->op_sv)) ||
4049 (type == OP_OR && !SvTRUE(((SVOP*)first)->op_sv)) ||
4050 (type == OP_DOR && !SvOK(((SVOP*)first)->op_sv))) {
4052 if (other->op_type == OP_CONST)
4053 other->op_private |= OPpCONST_SHORTCIRCUIT;
4055 OP *newop = newUNOP(OP_NULL, 0, other);
4056 op_getmad(first, newop, '1');
4057 newop->op_targ = type; /* set "was" field */
4064 /* check for C<my $x if 0>, or C<my($x,$y) if 0> */
4065 const OP *o2 = other;
4066 if ( ! (o2->op_type == OP_LIST
4067 && (( o2 = cUNOPx(o2)->op_first))
4068 && o2->op_type == OP_PUSHMARK
4069 && (( o2 = o2->op_sibling)) )
4072 if ((o2->op_type == OP_PADSV || o2->op_type == OP_PADAV
4073 || o2->op_type == OP_PADHV)
4074 && o2->op_private & OPpLVAL_INTRO
4075 && ckWARN(WARN_DEPRECATED))
4077 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
4078 "Deprecated use of my() in false conditional");
4082 if (first->op_type == OP_CONST)
4083 first->op_private |= OPpCONST_SHORTCIRCUIT;
4085 first = newUNOP(OP_NULL, 0, first);
4086 op_getmad(other, first, '2');
4087 first->op_targ = type; /* set "was" field */
4094 else if ((first->op_flags & OPf_KIDS) && type != OP_DOR
4095 && ckWARN(WARN_MISC)) /* [#24076] Don't warn for <FH> err FOO. */
4097 const OP * const k1 = ((UNOP*)first)->op_first;
4098 const OP * const k2 = k1->op_sibling;
4100 switch (first->op_type)
4103 if (k2 && k2->op_type == OP_READLINE
4104 && (k2->op_flags & OPf_STACKED)
4105 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
4107 warnop = k2->op_type;
4112 if (k1->op_type == OP_READDIR
4113 || k1->op_type == OP_GLOB
4114 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
4115 || k1->op_type == OP_EACH)
4117 warnop = ((k1->op_type == OP_NULL)
4118 ? (OPCODE)k1->op_targ : k1->op_type);
4123 const line_t oldline = CopLINE(PL_curcop);
4124 CopLINE_set(PL_curcop, PL_copline);
4125 Perl_warner(aTHX_ packWARN(WARN_MISC),
4126 "Value of %s%s can be \"0\"; test with defined()",
4128 ((warnop == OP_READLINE || warnop == OP_GLOB)
4129 ? " construct" : "() operator"));
4130 CopLINE_set(PL_curcop, oldline);
4137 if (type == OP_ANDASSIGN || type == OP_ORASSIGN || type == OP_DORASSIGN)
4138 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
4140 NewOp(1101, logop, 1, LOGOP);
4142 logop->op_type = (OPCODE)type;
4143 logop->op_ppaddr = PL_ppaddr[type];
4144 logop->op_first = first;
4145 logop->op_flags = (U8)(flags | OPf_KIDS);
4146 logop->op_other = LINKLIST(other);
4147 logop->op_private = (U8)(1 | (flags >> 8));
4149 /* establish postfix order */
4150 logop->op_next = LINKLIST(first);
4151 first->op_next = (OP*)logop;
4152 first->op_sibling = other;
4154 CHECKOP(type,logop);
4156 o = newUNOP(OP_NULL, 0, (OP*)logop);
4163 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
4171 return newLOGOP(OP_AND, 0, first, trueop);
4173 return newLOGOP(OP_OR, 0, first, falseop);
4175 scalarboolean(first);
4176 if (first->op_type == OP_CONST) {
4177 if (first->op_private & OPpCONST_BARE &&
4178 first->op_private & OPpCONST_STRICT) {
4179 no_bareword_allowed(first);
4181 if (SvTRUE(((SVOP*)first)->op_sv)) {
4184 trueop = newUNOP(OP_NULL, 0, trueop);
4185 op_getmad(first,trueop,'C');
4186 op_getmad(falseop,trueop,'e');
4188 /* FIXME for MAD - should there be an ELSE here? */
4198 falseop = newUNOP(OP_NULL, 0, falseop);
4199 op_getmad(first,falseop,'C');
4200 op_getmad(trueop,falseop,'t');
4202 /* FIXME for MAD - should there be an ELSE here? */
4210 NewOp(1101, logop, 1, LOGOP);
4211 logop->op_type = OP_COND_EXPR;
4212 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
4213 logop->op_first = first;
4214 logop->op_flags = (U8)(flags | OPf_KIDS);
4215 logop->op_private = (U8)(1 | (flags >> 8));
4216 logop->op_other = LINKLIST(trueop);
4217 logop->op_next = LINKLIST(falseop);
4219 CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
4222 /* establish postfix order */
4223 start = LINKLIST(first);
4224 first->op_next = (OP*)logop;
4226 first->op_sibling = trueop;
4227 trueop->op_sibling = falseop;
4228 o = newUNOP(OP_NULL, 0, (OP*)logop);
4230 trueop->op_next = falseop->op_next = o;
4237 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
4246 NewOp(1101, range, 1, LOGOP);
4248 range->op_type = OP_RANGE;
4249 range->op_ppaddr = PL_ppaddr[OP_RANGE];
4250 range->op_first = left;
4251 range->op_flags = OPf_KIDS;
4252 leftstart = LINKLIST(left);
4253 range->op_other = LINKLIST(right);
4254 range->op_private = (U8)(1 | (flags >> 8));
4256 left->op_sibling = right;
4258 range->op_next = (OP*)range;
4259 flip = newUNOP(OP_FLIP, flags, (OP*)range);
4260 flop = newUNOP(OP_FLOP, 0, flip);
4261 o = newUNOP(OP_NULL, 0, flop);
4263 range->op_next = leftstart;
4265 left->op_next = flip;
4266 right->op_next = flop;
4268 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
4269 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
4270 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
4271 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
4273 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
4274 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
4277 if (!flip->op_private || !flop->op_private)
4278 linklist(o); /* blow off optimizer unless constant */
4284 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
4289 const bool once = block && block->op_flags & OPf_SPECIAL &&
4290 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
4292 PERL_UNUSED_ARG(debuggable);
4295 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
4296 return block; /* do {} while 0 does once */
4297 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
4298 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
4299 expr = newUNOP(OP_DEFINED, 0,
4300 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
4301 } else if (expr->op_flags & OPf_KIDS) {
4302 const OP * const k1 = ((UNOP*)expr)->op_first;
4303 const OP * const k2 = k1 ? k1->op_sibling : NULL;
4304 switch (expr->op_type) {
4306 if (k2 && k2->op_type == OP_READLINE
4307 && (k2->op_flags & OPf_STACKED)
4308 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
4309 expr = newUNOP(OP_DEFINED, 0, expr);
4313 if (k1 && (k1->op_type == OP_READDIR
4314 || k1->op_type == OP_GLOB
4315 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
4316 || k1->op_type == OP_EACH))
4317 expr = newUNOP(OP_DEFINED, 0, expr);
4323 /* if block is null, the next append_elem() would put UNSTACK, a scalar
4324 * op, in listop. This is wrong. [perl #27024] */
4326 block = newOP(OP_NULL, 0);
4327 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
4328 o = new_logop(OP_AND, 0, &expr, &listop);
4331 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
4333 if (once && o != listop)
4334 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
4337 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
4339 o->op_flags |= flags;
4341 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
4346 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32
4347 whileline, OP *expr, OP *block, OP *cont, I32 has_my)
4356 PERL_UNUSED_ARG(debuggable);
4359 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
4360 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
4361 expr = newUNOP(OP_DEFINED, 0,
4362 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
4363 } else if (expr->op_flags & OPf_KIDS) {
4364 const OP * const k1 = ((UNOP*)expr)->op_first;
4365 const OP * const k2 = (k1) ? k1->op_sibling : NULL;
4366 switch (expr->op_type) {
4368 if (k2 && k2->op_type == OP_READLINE
4369 && (k2->op_flags & OPf_STACKED)
4370 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
4371 expr = newUNOP(OP_DEFINED, 0, expr);
4375 if (k1 && (k1->op_type == OP_READDIR
4376 || k1->op_type == OP_GLOB
4377 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
4378 || k1->op_type == OP_EACH))
4379 expr = newUNOP(OP_DEFINED, 0, expr);
4386 block = newOP(OP_NULL, 0);
4387 else if (cont || has_my) {
4388 block = scope(block);
4392 next = LINKLIST(cont);
4395 OP * const unstack = newOP(OP_UNSTACK, 0);
4398 cont = append_elem(OP_LINESEQ, cont, unstack);
4402 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
4404 redo = LINKLIST(listop);
4407 PL_copline = (line_t)whileline;
4409 o = new_logop(OP_AND, 0, &expr, &listop);
4410 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
4411 op_free(expr); /* oops, it's a while (0) */
4413 return NULL; /* listop already freed by new_logop */
4416 ((LISTOP*)listop)->op_last->op_next =
4417 (o == listop ? redo : LINKLIST(o));
4423 NewOp(1101,loop,1,LOOP);
4424 loop->op_type = OP_ENTERLOOP;
4425 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
4426 loop->op_private = 0;
4427 loop->op_next = (OP*)loop;
4430 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
4432 loop->op_redoop = redo;
4433 loop->op_lastop = o;
4434 o->op_private |= loopflags;
4437 loop->op_nextop = next;
4439 loop->op_nextop = o;
4441 o->op_flags |= flags;
4442 o->op_private |= (flags >> 8);
4447 Perl_newFOROP(pTHX_ I32 flags, char *label, line_t forline, OP *sv, OP *expr, OP *block, OP *cont)
4452 PADOFFSET padoff = 0;
4458 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
4459 iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */
4460 sv->op_type = OP_RV2GV;
4461 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
4462 if (cGVOPx_gv(cUNOPx(sv)->op_first) == PL_defgv)
4463 iterpflags |= OPpITER_DEF;
4465 else if (sv->op_type == OP_PADSV) { /* private variable */
4466 iterpflags = sv->op_private & OPpLVAL_INTRO; /* for my $x () */
4467 padoff = sv->op_targ;
4476 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
4477 padoff = sv->op_targ;
4482 iterflags |= OPf_SPECIAL;
4488 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
4489 if (padoff && strEQ(PAD_COMPNAME_PV(padoff), "$_"))
4490 iterpflags |= OPpITER_DEF;
4493 const PADOFFSET offset = pad_findmy("$_");
4494 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
4495 sv = newGVOP(OP_GV, 0, PL_defgv);
4500 iterpflags |= OPpITER_DEF;
4502 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
4503 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
4504 iterflags |= OPf_STACKED;
4506 else if (expr->op_type == OP_NULL &&
4507 (expr->op_flags & OPf_KIDS) &&
4508 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
4510 /* Basically turn for($x..$y) into the same as for($x,$y), but we
4511 * set the STACKED flag to indicate that these values are to be
4512 * treated as min/max values by 'pp_iterinit'.
4514 const UNOP* const flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
4515 LOGOP* const range = (LOGOP*) flip->op_first;
4516 OP* const left = range->op_first;
4517 OP* const right = left->op_sibling;
4520 range->op_flags &= ~OPf_KIDS;
4521 range->op_first = NULL;
4523 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
4524 listop->op_first->op_next = range->op_next;
4525 left->op_next = range->op_other;
4526 right->op_next = (OP*)listop;
4527 listop->op_next = listop->op_first;
4530 op_getmad(expr,(OP*)listop,'O');
4534 expr = (OP*)(listop);
4536 iterflags |= OPf_STACKED;
4539 expr = mod(force_list(expr), OP_GREPSTART);
4542 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
4543 append_elem(OP_LIST, expr, scalar(sv))));
4544 assert(!loop->op_next);
4545 /* for my $x () sets OPpLVAL_INTRO;
4546 * for our $x () sets OPpOUR_INTRO */
4547 loop->op_private = (U8)iterpflags;
4548 #ifdef PL_OP_SLAB_ALLOC
4551 NewOp(1234,tmp,1,LOOP);
4552 Copy(loop,tmp,1,LISTOP);
4557 loop = PerlMemShared_realloc(loop, sizeof(LOOP));
4559 loop->op_targ = padoff;
4560 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont, 0);
4562 op_getmad(madsv, (OP*)loop, 'v');
4563 PL_copline = forline;
4564 return newSTATEOP(0, label, wop);
4568 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
4573 if (type != OP_GOTO || label->op_type == OP_CONST) {
4574 /* "last()" means "last" */
4575 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
4576 o = newOP(type, OPf_SPECIAL);
4578 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
4579 ? SvPVx_nolen_const(((SVOP*)label)->op_sv)
4583 op_getmad(label,o,'L');
4589 /* Check whether it's going to be a goto &function */
4590 if (label->op_type == OP_ENTERSUB
4591 && !(label->op_flags & OPf_STACKED))
4592 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
4593 o = newUNOP(type, OPf_STACKED, label);
4595 PL_hints |= HINT_BLOCK_SCOPE;
4599 /* if the condition is a literal array or hash
4600 (or @{ ... } etc), make a reference to it.
4603 S_ref_array_or_hash(pTHX_ OP *cond)
4606 && (cond->op_type == OP_RV2AV
4607 || cond->op_type == OP_PADAV
4608 || cond->op_type == OP_RV2HV
4609 || cond->op_type == OP_PADHV))
4611 return newUNOP(OP_REFGEN,
4612 0, mod(cond, OP_REFGEN));
4618 /* These construct the optree fragments representing given()
4621 entergiven and enterwhen are LOGOPs; the op_other pointer
4622 points up to the associated leave op. We need this so we
4623 can put it in the context and make break/continue work.
4624 (Also, of course, pp_enterwhen will jump straight to
4625 op_other if the match fails.)
4630 S_newGIVWHENOP(pTHX_ OP *cond, OP *block,
4631 I32 enter_opcode, I32 leave_opcode,
4632 PADOFFSET entertarg)
4638 NewOp(1101, enterop, 1, LOGOP);
4639 enterop->op_type = enter_opcode;
4640 enterop->op_ppaddr = PL_ppaddr[enter_opcode];
4641 enterop->op_flags = (U8) OPf_KIDS;
4642 enterop->op_targ = ((entertarg == NOT_IN_PAD) ? 0 : entertarg);
4643 enterop->op_private = 0;
4645 o = newUNOP(leave_opcode, 0, (OP *) enterop);
4648 enterop->op_first = scalar(cond);
4649 cond->op_sibling = block;
4651 o->op_next = LINKLIST(cond);
4652 cond->op_next = (OP *) enterop;
4655 /* This is a default {} block */
4656 enterop->op_first = block;
4657 enterop->op_flags |= OPf_SPECIAL;
4659 o->op_next = (OP *) enterop;
4662 CHECKOP(enter_opcode, enterop); /* Currently does nothing, since
4663 entergiven and enterwhen both
4666 enterop->op_next = LINKLIST(block);
4667 block->op_next = enterop->op_other = o;
4672 /* Does this look like a boolean operation? For these purposes
4673 a boolean operation is:
4674 - a subroutine call [*]
4675 - a logical connective
4676 - a comparison operator
4677 - a filetest operator, with the exception of -s -M -A -C
4678 - defined(), exists() or eof()
4679 - /$re/ or $foo =~ /$re/
4681 [*] possibly surprising
4685 S_looks_like_bool(pTHX_ const OP *o)
4688 switch(o->op_type) {
4690 return looks_like_bool(cLOGOPo->op_first);
4694 looks_like_bool(cLOGOPo->op_first)
4695 && looks_like_bool(cLOGOPo->op_first->op_sibling));
4699 case OP_NOT: case OP_XOR:
4700 /* Note that OP_DOR is not here */
4702 case OP_EQ: case OP_NE: case OP_LT:
4703 case OP_GT: case OP_LE: case OP_GE:
4705 case OP_I_EQ: case OP_I_NE: case OP_I_LT:
4706 case OP_I_GT: case OP_I_LE: case OP_I_GE:
4708 case OP_SEQ: case OP_SNE: case OP_SLT:
4709 case OP_SGT: case OP_SLE: case OP_SGE:
4713 case OP_FTRREAD: case OP_FTRWRITE: case OP_FTREXEC:
4714 case OP_FTEREAD: case OP_FTEWRITE: case OP_FTEEXEC:
4715 case OP_FTIS: case OP_FTEOWNED: case OP_FTROWNED:
4716 case OP_FTZERO: case OP_FTSOCK: case OP_FTCHR:
4717 case OP_FTBLK: case OP_FTFILE: case OP_FTDIR:
4718 case OP_FTPIPE: case OP_FTLINK: case OP_FTSUID:
4719 case OP_FTSGID: case OP_FTSVTX: case OP_FTTTY:
4720 case OP_FTTEXT: case OP_FTBINARY:
4722 case OP_DEFINED: case OP_EXISTS:
4723 case OP_MATCH: case OP_EOF:
4728 /* Detect comparisons that have been optimized away */
4729 if (cSVOPo->op_sv == &PL_sv_yes
4730 || cSVOPo->op_sv == &PL_sv_no)
4741 Perl_newGIVENOP(pTHX_ OP *cond, OP *block, PADOFFSET defsv_off)
4745 return newGIVWHENOP(
4746 ref_array_or_hash(cond),
4748 OP_ENTERGIVEN, OP_LEAVEGIVEN,
4752 /* If cond is null, this is a default {} block */
4754 Perl_newWHENOP(pTHX_ OP *cond, OP *block)
4756 const bool cond_llb = (!cond || looks_like_bool(cond));
4762 cond_op = newBINOP(OP_SMARTMATCH, OPf_SPECIAL,
4764 scalar(ref_array_or_hash(cond)));
4767 return newGIVWHENOP(
4769 append_elem(block->op_type, block, newOP(OP_BREAK, OPf_SPECIAL)),
4770 OP_ENTERWHEN, OP_LEAVEWHEN, 0);
4774 =for apidoc cv_undef
4776 Clear out all the active components of a CV. This can happen either
4777 by an explicit C<undef &foo>, or by the reference count going to zero.
4778 In the former case, we keep the CvOUTSIDE pointer, so that any anonymous
4779 children can still follow the full lexical scope chain.
4785 Perl_cv_undef(pTHX_ CV *cv)
4789 if (CvFILE(cv) && !CvISXSUB(cv)) {
4790 /* for XSUBs CvFILE point directly to static memory; __FILE__ */
4791 Safefree(CvFILE(cv));
4796 if (!CvISXSUB(cv) && CvROOT(cv)) {
4797 if (SvTYPE(cv) == SVt_PVCV && CvDEPTH(cv))
4798 Perl_croak(aTHX_ "Can't undef active subroutine");
4801 PAD_SAVE_SETNULLPAD();
4803 op_free(CvROOT(cv));
4808 SvPOK_off((SV*)cv); /* forget prototype */
4813 /* remove CvOUTSIDE unless this is an undef rather than a free */
4814 if (!SvREFCNT(cv) && CvOUTSIDE(cv)) {
4815 if (!CvWEAKOUTSIDE(cv))
4816 SvREFCNT_dec(CvOUTSIDE(cv));
4817 CvOUTSIDE(cv) = NULL;
4820 SvREFCNT_dec((SV*)CvXSUBANY(cv).any_ptr);
4823 if (CvISXSUB(cv) && CvXSUB(cv)) {
4826 /* delete all flags except WEAKOUTSIDE */
4827 CvFLAGS(cv) &= CVf_WEAKOUTSIDE;
4831 Perl_cv_ckproto_len(pTHX_ const CV *cv, const GV *gv, const char *p,
4834 /* Can't just use a strcmp on the prototype, as CONSTSUBs "cheat" by
4835 relying on SvCUR, and doubling up the buffer to hold CvFILE(). */
4836 if (((!p != !SvPOK(cv)) /* One has prototype, one has not. */
4837 || (p && (len != SvCUR(cv) /* Not the same length. */
4838 || memNE(p, SvPVX_const(cv), len))))
4839 && ckWARN_d(WARN_PROTOTYPE)) {
4840 SV* const msg = sv_newmortal();
4844 gv_efullname3(name = sv_newmortal(), gv, NULL);
4845 sv_setpv(msg, "Prototype mismatch:");
4847 Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, (void*)name);
4849 Perl_sv_catpvf(aTHX_ msg, " (%"SVf")", (void*)cv);
4851 sv_catpvs(msg, ": none");
4852 sv_catpvs(msg, " vs ");
4854 Perl_sv_catpvf(aTHX_ msg, "(%.*s)", (int) len, p);
4856 sv_catpvs(msg, "none");
4857 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%"SVf, (void*)msg);
4861 static void const_sv_xsub(pTHX_ CV* cv);
4865 =head1 Optree Manipulation Functions
4867 =for apidoc cv_const_sv
4869 If C<cv> is a constant sub eligible for inlining. returns the constant
4870 value returned by the sub. Otherwise, returns NULL.
4872 Constant subs can be created with C<newCONSTSUB> or as described in
4873 L<perlsub/"Constant Functions">.
4878 Perl_cv_const_sv(pTHX_ CV *cv)
4880 PERL_UNUSED_CONTEXT;
4883 if (!(SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM))
4885 return CvCONST(cv) ? (SV*)CvXSUBANY(cv).any_ptr : NULL;
4888 /* op_const_sv: examine an optree to determine whether it's in-lineable.
4889 * Can be called in 3 ways:
4892 * look for a single OP_CONST with attached value: return the value
4894 * cv && CvCLONE(cv) && !CvCONST(cv)
4896 * examine the clone prototype, and if contains only a single
4897 * OP_CONST referencing a pad const, or a single PADSV referencing
4898 * an outer lexical, return a non-zero value to indicate the CV is
4899 * a candidate for "constizing" at clone time
4903 * We have just cloned an anon prototype that was marked as a const
4904 * candidiate. Try to grab the current value, and in the case of
4905 * PADSV, ignore it if it has multiple references. Return the value.
4909 Perl_op_const_sv(pTHX_ const OP *o, CV *cv)
4917 if (o->op_type == OP_LINESEQ && cLISTOPo->op_first)
4918 o = cLISTOPo->op_first->op_sibling;
4920 for (; o; o = o->op_next) {
4921 const OPCODE type = o->op_type;
4923 if (sv && o->op_next == o)
4925 if (o->op_next != o) {
4926 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
4928 if (type == OP_DBSTATE)
4931 if (type == OP_LEAVESUB || type == OP_RETURN)
4935 if (type == OP_CONST && cSVOPo->op_sv)
4937 else if (cv && type == OP_CONST) {
4938 sv = PAD_BASE_SV(CvPADLIST(cv), o->op_targ);
4942 else if (cv && type == OP_PADSV) {
4943 if (CvCONST(cv)) { /* newly cloned anon */
4944 sv = PAD_BASE_SV(CvPADLIST(cv), o->op_targ);
4945 /* the candidate should have 1 ref from this pad and 1 ref
4946 * from the parent */
4947 if (!sv || SvREFCNT(sv) != 2)
4954 if (PAD_COMPNAME_FLAGS(o->op_targ) & SVf_FAKE)
4955 sv = &PL_sv_undef; /* an arbitrary non-null value */
4970 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4973 /* This would be the return value, but the return cannot be reached. */
4974 OP* pegop = newOP(OP_NULL, 0);
4977 PERL_UNUSED_ARG(floor);
4987 Perl_croak(aTHX_ "\"my sub\" not yet implemented");
4989 NORETURN_FUNCTION_END;
4994 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
4996 return Perl_newATTRSUB(aTHX_ floor, o, proto, NULL, block);
5000 Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
5007 register CV *cv = NULL;
5009 /* If the subroutine has no body, no attributes, and no builtin attributes
5010 then it's just a sub declaration, and we may be able to get away with
5011 storing with a placeholder scalar in the symbol table, rather than a
5012 full GV and CV. If anything is present then it will take a full CV to
5014 const I32 gv_fetch_flags
5015 = (block || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)
5017 ? GV_ADDMULTI : GV_ADDMULTI | GV_NOINIT;
5018 const char * const name = o ? SvPVx_nolen_const(cSVOPo->op_sv) : NULL;
5021 assert(proto->op_type == OP_CONST);
5022 ps = SvPVx_const(((SVOP*)proto)->op_sv, ps_len);
5027 if (!name && PERLDB_NAMEANON && CopLINE(PL_curcop)) {
5028 SV * const sv = sv_newmortal();
5029 Perl_sv_setpvf(aTHX_ sv, "%s[%s:%"IVdf"]",
5030 PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
5031 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
5032 aname = SvPVX_const(sv);
5037 gv = name ? gv_fetchsv(cSVOPo->op_sv, gv_fetch_flags, SVt_PVCV)
5038 : gv_fetchpv(aname ? aname
5039 : (PL_curstash ? "__ANON__" : "__ANON__::__ANON__"),
5040 gv_fetch_flags, SVt_PVCV);
5042 if (!PL_madskills) {
5051 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
5052 maximum a prototype before. */
5053 if (SvTYPE(gv) > SVt_NULL) {
5054 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1)
5055 && ckWARN_d(WARN_PROTOTYPE))
5057 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "Runaway prototype");
5059 cv_ckproto_len((CV*)gv, NULL, ps, ps_len);
5062 sv_setpvn((SV*)gv, ps, ps_len);
5064 sv_setiv((SV*)gv, -1);
5065 SvREFCNT_dec(PL_compcv);
5066 cv = PL_compcv = NULL;
5067 PL_sub_generation++;
5071 cv = (!name || GvCVGEN(gv)) ? NULL : GvCV(gv);
5073 #ifdef GV_UNIQUE_CHECK
5074 if (cv && GvUNIQUE(gv) && SvREADONLY(cv)) {
5075 Perl_croak(aTHX_ "Can't define subroutine %s (GV is unique)", name);
5079 if (!block || !ps || *ps || attrs
5080 || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)
5082 || block->op_type == OP_NULL
5087 const_sv = op_const_sv(block, NULL);
5090 const bool exists = CvROOT(cv) || CvXSUB(cv);
5092 #ifdef GV_UNIQUE_CHECK
5093 if (exists && GvUNIQUE(gv)) {
5094 Perl_croak(aTHX_ "Can't redefine unique subroutine %s", name);
5098 /* if the subroutine doesn't exist and wasn't pre-declared
5099 * with a prototype, assume it will be AUTOLOADed,
5100 * skipping the prototype check
5102 if (exists || SvPOK(cv))
5103 cv_ckproto_len(cv, gv, ps, ps_len);
5104 /* already defined (or promised)? */
5105 if (exists || GvASSUMECV(gv)) {
5108 || block->op_type == OP_NULL
5111 if (CvFLAGS(PL_compcv)) {
5112 /* might have had built-in attrs applied */
5113 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
5115 /* just a "sub foo;" when &foo is already defined */
5116 SAVEFREESV(PL_compcv);
5121 && block->op_type != OP_NULL
5124 if (ckWARN(WARN_REDEFINE)
5126 && (!const_sv || sv_cmp(cv_const_sv(cv), const_sv))))
5128 const line_t oldline = CopLINE(PL_curcop);
5129 if (PL_copline != NOLINE)
5130 CopLINE_set(PL_curcop, PL_copline);
5131 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
5132 CvCONST(cv) ? "Constant subroutine %s redefined"
5133 : "Subroutine %s redefined", name);
5134 CopLINE_set(PL_curcop, oldline);
5137 if (!PL_minus_c) /* keep old one around for madskills */
5140 /* (PL_madskills unset in used file.) */
5148 SvREFCNT_inc_simple_void_NN(const_sv);
5150 assert(!CvROOT(cv) && !CvCONST(cv));
5151 sv_setpvn((SV*)cv, "", 0); /* prototype is "" */
5152 CvXSUBANY(cv).any_ptr = const_sv;
5153 CvXSUB(cv) = const_sv_xsub;
5159 cv = newCONSTSUB(NULL, name, const_sv);
5161 PL_sub_generation++;
5165 SvREFCNT_dec(PL_compcv);
5173 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>
5174 * before we clobber PL_compcv.
5178 || block->op_type == OP_NULL
5182 /* Might have had built-in attributes applied -- propagate them. */
5183 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
5184 if (CvGV(cv) && GvSTASH(CvGV(cv)))
5185 stash = GvSTASH(CvGV(cv));
5186 else if (CvSTASH(cv))
5187 stash = CvSTASH(cv);
5189 stash = PL_curstash;
5192 /* possibly about to re-define existing subr -- ignore old cv */
5193 rcv = (SV*)PL_compcv;
5194 if (name && GvSTASH(gv))
5195 stash = GvSTASH(gv);
5197 stash = PL_curstash;
5199 apply_attrs(stash, rcv, attrs, FALSE);
5201 if (cv) { /* must reuse cv if autoloaded */
5208 || block->op_type == OP_NULL) && !PL_madskills
5211 /* got here with just attrs -- work done, so bug out */
5212 SAVEFREESV(PL_compcv);
5215 /* transfer PL_compcv to cv */
5217 CvFLAGS(cv) = CvFLAGS(PL_compcv);
5218 if (!CvWEAKOUTSIDE(cv))
5219 SvREFCNT_dec(CvOUTSIDE(cv));
5220 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
5221 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv);
5222 CvOUTSIDE(PL_compcv) = 0;
5223 CvPADLIST(cv) = CvPADLIST(PL_compcv);
5224 CvPADLIST(PL_compcv) = 0;
5225 /* inner references to PL_compcv must be fixed up ... */
5226 pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv);
5227 /* ... before we throw it away */
5228 SvREFCNT_dec(PL_compcv);
5230 if (PERLDB_INTER)/* Advice debugger on the new sub. */
5231 ++PL_sub_generation;
5238 if (strEQ(name, "import")) {
5239 PL_formfeed = (SV*)cv;
5240 Perl_warner(aTHX_ packWARN(WARN_VOID), "%lx\n", (long)cv);
5244 PL_sub_generation++;
5248 CvFILE_set_from_cop(cv, PL_curcop);
5249 CvSTASH(cv) = PL_curstash;
5252 sv_setpvn((SV*)cv, ps, ps_len);
5254 if (PL_error_count) {
5258 const char *s = strrchr(name, ':');
5260 if (strEQ(s, "BEGIN")) {
5261 const char not_safe[] =
5262 "BEGIN not safe after errors--compilation aborted";
5263 if (PL_in_eval & EVAL_KEEPERR)
5264 Perl_croak(aTHX_ not_safe);
5266 /* force display of errors found but not reported */
5267 sv_catpv(ERRSV, not_safe);
5268 Perl_croak(aTHX_ "%"SVf, (void*)ERRSV);
5278 CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0,
5279 mod(scalarseq(block), OP_LEAVESUBLV));
5282 /* This makes sub {}; work as expected. */
5283 if (block->op_type == OP_STUB) {
5284 OP* const newblock = newSTATEOP(0, NULL, 0);
5286 op_getmad(block,newblock,'B');
5292 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
5294 CvROOT(cv)->op_private |= OPpREFCOUNTED;
5295 OpREFCNT_set(CvROOT(cv), 1);
5296 CvSTART(cv) = LINKLIST(CvROOT(cv));
5297 CvROOT(cv)->op_next = 0;
5298 CALL_PEEP(CvSTART(cv));
5300 /* now that optimizer has done its work, adjust pad values */
5302 pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
5305 assert(!CvCONST(cv));
5306 if (ps && !*ps && op_const_sv(block, cv))
5310 if (name || aname) {
5312 const char * const tname = (name ? name : aname);
5314 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
5315 SV * const sv = newSV(0);
5316 SV * const tmpstr = sv_newmortal();
5317 GV * const db_postponed = gv_fetchpvs("DB::postponed",
5318 GV_ADDMULTI, SVt_PVHV);
5321 Perl_sv_setpvf(aTHX_ sv, "%s:%ld-%ld",
5323 (long)PL_subline, (long)CopLINE(PL_curcop));
5324 gv_efullname3(tmpstr, gv, NULL);
5325 hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr), SvCUR(tmpstr), sv, 0);
5326 hv = GvHVn(db_postponed);
5327 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvCUR(tmpstr))) {
5328 CV * const pcv = GvCV(db_postponed);
5334 call_sv((SV*)pcv, G_DISCARD);
5339 if ((s = strrchr(tname,':')))
5344 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
5347 if (strEQ(s, "BEGIN") && !PL_error_count) {
5348 const I32 oldscope = PL_scopestack_ix;
5350 SAVECOPFILE(&PL_compiling);
5351 SAVECOPLINE(&PL_compiling);
5354 PL_beginav = newAV();
5355 DEBUG_x( dump_sub(gv) );
5356 av_push(PL_beginav, (SV*)cv);
5357 GvCV(gv) = 0; /* cv has been hijacked */
5358 call_list(oldscope, PL_beginav);
5360 PL_curcop = &PL_compiling;
5361 CopHINTS_set(&PL_compiling, PL_hints);
5364 else if (strEQ(s, "END") && !PL_error_count) {
5367 DEBUG_x( dump_sub(gv) );
5368 av_unshift(PL_endav, 1);
5369 av_store(PL_endav, 0, (SV*)cv);
5370 GvCV(gv) = 0; /* cv has been hijacked */
5372 else if (strEQ(s, "CHECK") && !PL_error_count) {
5374 PL_checkav = newAV();
5375 DEBUG_x( dump_sub(gv) );
5376 if (PL_main_start && ckWARN(WARN_VOID))
5377 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run CHECK block");
5378 av_unshift(PL_checkav, 1);
5379 av_store(PL_checkav, 0, (SV*)cv);
5380 GvCV(gv) = 0; /* cv has been hijacked */
5382 else if (strEQ(s, "INIT") && !PL_error_count) {
5384 PL_initav = newAV();
5385 DEBUG_x( dump_sub(gv) );
5386 if (PL_main_start && ckWARN(WARN_VOID))
5387 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run INIT block");
5388 av_push(PL_initav, (SV*)cv);
5389 GvCV(gv) = 0; /* cv has been hijacked */
5394 PL_copline = NOLINE;
5399 /* XXX unsafe for threads if eval_owner isn't held */
5401 =for apidoc newCONSTSUB
5403 Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is
5404 eligible for inlining at compile-time.
5410 Perl_newCONSTSUB(pTHX_ HV *stash, const char *name, SV *sv)
5415 const char *const temp_p = CopFILE(PL_curcop);
5416 const STRLEN len = temp_p ? strlen(temp_p) : 0;
5418 SV *const temp_sv = CopFILESV(PL_curcop);
5420 const char *const temp_p = temp_sv ? SvPV_const(temp_sv, len) : NULL;
5422 char *const file = savepvn(temp_p, temp_p ? len : 0);
5426 SAVECOPLINE(PL_curcop);
5427 CopLINE_set(PL_curcop, PL_copline);
5430 PL_hints &= ~HINT_BLOCK_SCOPE;
5433 SAVESPTR(PL_curstash);
5434 SAVECOPSTASH(PL_curcop);
5435 PL_curstash = stash;
5436 CopSTASH_set(PL_curcop,stash);
5439 /* file becomes the CvFILE. For an XS, it's supposed to be static storage,
5440 and so doesn't get free()d. (It's expected to be from the C pre-
5441 processor __FILE__ directive). But we need a dynamically allocated one,
5442 and we need it to get freed. */
5443 cv = newXS_flags(name, const_sv_xsub, file, "", XS_DYNAMIC_FILENAME);
5444 CvXSUBANY(cv).any_ptr = sv;
5449 CopSTASH_free(PL_curcop);
5457 Perl_newXS_flags(pTHX_ const char *name, XSUBADDR_t subaddr,
5458 const char *const filename, const char *const proto,
5461 CV *cv = newXS(name, subaddr, filename);
5463 if (flags & XS_DYNAMIC_FILENAME) {
5464 /* We need to "make arrangements" (ie cheat) to ensure that the
5465 filename lasts as long as the PVCV we just created, but also doesn't
5467 STRLEN filename_len = strlen(filename);
5468 STRLEN proto_and_file_len = filename_len;
5469 char *proto_and_file;
5473 proto_len = strlen(proto);
5474 proto_and_file_len += proto_len;
5476 Newx(proto_and_file, proto_and_file_len + 1, char);
5477 Copy(proto, proto_and_file, proto_len, char);
5478 Copy(filename, proto_and_file + proto_len, filename_len + 1, char);
5481 proto_and_file = savepvn(filename, filename_len);
5484 /* This gets free()d. :-) */
5485 sv_usepvn_flags((SV*)cv, proto_and_file, proto_and_file_len,
5486 SV_HAS_TRAILING_NUL);
5488 /* This gives us the correct prototype, rather than one with the
5489 file name appended. */
5490 SvCUR_set(cv, proto_len);
5494 CvFILE(cv) = proto_and_file + proto_len;
5496 sv_setpv((SV *)cv, proto);
5502 =for apidoc U||newXS
5504 Used by C<xsubpp> to hook up XSUBs as Perl subs. I<filename> needs to be
5505 static storage, as it is used directly as CvFILE(), without a copy being made.
5511 Perl_newXS(pTHX_ const char *name, XSUBADDR_t subaddr, const char *filename)
5514 GV * const gv = gv_fetchpv(name ? name :
5515 (PL_curstash ? "__ANON__" : "__ANON__::__ANON__"),
5516 GV_ADDMULTI, SVt_PVCV);
5520 Perl_croak(aTHX_ "panic: no address for '%s' in '%s'", name, filename);
5522 if ((cv = (name ? GvCV(gv) : NULL))) {
5524 /* just a cached method */
5528 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
5529 /* already defined (or promised) */
5530 /* XXX It's possible for this HvNAME_get to return null, and get passed into strEQ */
5531 if (ckWARN(WARN_REDEFINE)) {
5532 GV * const gvcv = CvGV(cv);
5534 HV * const stash = GvSTASH(gvcv);
5536 const char *redefined_name = HvNAME_get(stash);
5537 if ( strEQ(redefined_name,"autouse") ) {
5538 const line_t oldline = CopLINE(PL_curcop);
5539 if (PL_copline != NOLINE)
5540 CopLINE_set(PL_curcop, PL_copline);
5541 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
5542 CvCONST(cv) ? "Constant subroutine %s redefined"
5543 : "Subroutine %s redefined"
5545 CopLINE_set(PL_curcop, oldline);
5555 if (cv) /* must reuse cv if autoloaded */
5559 sv_upgrade((SV *)cv, SVt_PVCV);
5563 PL_sub_generation++;
5567 (void)gv_fetchfile(filename);
5568 CvFILE(cv) = (char *)filename; /* NOTE: not copied, as it is expected to be
5569 an external constant string */
5571 CvXSUB(cv) = subaddr;
5574 const char *s = strrchr(name,':');
5580 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
5583 if (strEQ(s, "BEGIN")) {
5585 PL_beginav = newAV();
5586 av_push(PL_beginav, (SV*)cv);
5587 GvCV(gv) = 0; /* cv has been hijacked */
5589 else if (strEQ(s, "END")) {
5592 av_unshift(PL_endav, 1);
5593 av_store(PL_endav, 0, (SV*)cv);
5594 GvCV(gv) = 0; /* cv has been hijacked */
5596 else if (strEQ(s, "CHECK")) {
5598 PL_checkav = newAV();
5599 if (PL_main_start && ckWARN(WARN_VOID))
5600 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run CHECK block");
5601 av_unshift(PL_checkav, 1);
5602 av_store(PL_checkav, 0, (SV*)cv);
5603 GvCV(gv) = 0; /* cv has been hijacked */
5605 else if (strEQ(s, "INIT")) {
5607 PL_initav = newAV();
5608 if (PL_main_start && ckWARN(WARN_VOID))
5609 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run INIT block");
5610 av_push(PL_initav, (SV*)cv);
5611 GvCV(gv) = 0; /* cv has been hijacked */
5626 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
5631 OP* pegop = newOP(OP_NULL, 0);
5635 ? gv_fetchsv(cSVOPo->op_sv, GV_ADD, SVt_PVFM)
5636 : gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVFM);
5638 #ifdef GV_UNIQUE_CHECK
5640 Perl_croak(aTHX_ "Bad symbol for form (GV is unique)");
5644 if ((cv = GvFORM(gv))) {
5645 if (ckWARN(WARN_REDEFINE)) {
5646 const line_t oldline = CopLINE(PL_curcop);
5647 if (PL_copline != NOLINE)
5648 CopLINE_set(PL_curcop, PL_copline);
5649 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
5650 o ? "Format %"SVf" redefined"
5651 : "Format STDOUT redefined", (void*)cSVOPo->op_sv);
5652 CopLINE_set(PL_curcop, oldline);
5659 CvFILE_set_from_cop(cv, PL_curcop);
5662 pad_tidy(padtidy_FORMAT);
5663 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
5664 CvROOT(cv)->op_private |= OPpREFCOUNTED;
5665 OpREFCNT_set(CvROOT(cv), 1);
5666 CvSTART(cv) = LINKLIST(CvROOT(cv));
5667 CvROOT(cv)->op_next = 0;
5668 CALL_PEEP(CvSTART(cv));
5670 op_getmad(o,pegop,'n');
5671 op_getmad_weak(block, pegop, 'b');
5675 PL_copline = NOLINE;
5683 Perl_newANONLIST(pTHX_ OP *o)
5685 return newUNOP(OP_REFGEN, 0,
5686 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
5690 Perl_newANONHASH(pTHX_ OP *o)
5692 return newUNOP(OP_REFGEN, 0,
5693 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
5697 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
5699 return newANONATTRSUB(floor, proto, NULL, block);
5703 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
5705 return newUNOP(OP_REFGEN, 0,
5706 newSVOP(OP_ANONCODE, 0,
5707 (SV*)newATTRSUB(floor, 0, proto, attrs, block)));
5711 Perl_oopsAV(pTHX_ OP *o)
5714 switch (o->op_type) {
5716 o->op_type = OP_PADAV;
5717 o->op_ppaddr = PL_ppaddr[OP_PADAV];
5718 return ref(o, OP_RV2AV);
5721 o->op_type = OP_RV2AV;
5722 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
5727 if (ckWARN_d(WARN_INTERNAL))
5728 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV");
5735 Perl_oopsHV(pTHX_ OP *o)
5738 switch (o->op_type) {
5741 o->op_type = OP_PADHV;
5742 o->op_ppaddr = PL_ppaddr[OP_PADHV];
5743 return ref(o, OP_RV2HV);
5747 o->op_type = OP_RV2HV;
5748 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
5753 if (ckWARN_d(WARN_INTERNAL))
5754 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV");
5761 Perl_newAVREF(pTHX_ OP *o)
5764 if (o->op_type == OP_PADANY) {
5765 o->op_type = OP_PADAV;
5766 o->op_ppaddr = PL_ppaddr[OP_PADAV];
5769 else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)
5770 && ckWARN(WARN_DEPRECATED)) {
5771 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
5772 "Using an array as a reference is deprecated");
5774 return newUNOP(OP_RV2AV, 0, scalar(o));
5778 Perl_newGVREF(pTHX_ I32 type, OP *o)
5780 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
5781 return newUNOP(OP_NULL, 0, o);
5782 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
5786 Perl_newHVREF(pTHX_ OP *o)
5789 if (o->op_type == OP_PADANY) {
5790 o->op_type = OP_PADHV;
5791 o->op_ppaddr = PL_ppaddr[OP_PADHV];
5794 else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)
5795 && ckWARN(WARN_DEPRECATED)) {
5796 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
5797 "Using a hash as a reference is deprecated");
5799 return newUNOP(OP_RV2HV, 0, scalar(o));
5803 Perl_newCVREF(pTHX_ I32 flags, OP *o)
5805 return newUNOP(OP_RV2CV, flags, scalar(o));
5809 Perl_newSVREF(pTHX_ OP *o)
5812 if (o->op_type == OP_PADANY) {
5813 o->op_type = OP_PADSV;
5814 o->op_ppaddr = PL_ppaddr[OP_PADSV];
5817 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
5818 o->op_flags |= OPpDONE_SVREF;
5821 return newUNOP(OP_RV2SV, 0, scalar(o));
5824 /* Check routines. See the comments at the top of this file for details
5825 * on when these are called */
5828 Perl_ck_anoncode(pTHX_ OP *o)
5830 cSVOPo->op_targ = pad_add_anon(cSVOPo->op_sv, o->op_type);
5832 cSVOPo->op_sv = NULL;
5837 Perl_ck_bitop(pTHX_ OP *o)
5840 #define OP_IS_NUMCOMPARE(op) \
5841 ((op) == OP_LT || (op) == OP_I_LT || \
5842 (op) == OP_GT || (op) == OP_I_GT || \
5843 (op) == OP_LE || (op) == OP_I_LE || \
5844 (op) == OP_GE || (op) == OP_I_GE || \
5845 (op) == OP_EQ || (op) == OP_I_EQ || \
5846 (op) == OP_NE || (op) == OP_I_NE || \
5847 (op) == OP_NCMP || (op) == OP_I_NCMP)
5848 o->op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
5849 if (!(o->op_flags & OPf_STACKED) /* Not an assignment */
5850 && (o->op_type == OP_BIT_OR
5851 || o->op_type == OP_BIT_AND
5852 || o->op_type == OP_BIT_XOR))
5854 const OP * const left = cBINOPo->op_first;
5855 const OP * const right = left->op_sibling;
5856 if ((OP_IS_NUMCOMPARE(left->op_type) &&
5857 (left->op_flags & OPf_PARENS) == 0) ||
5858 (OP_IS_NUMCOMPARE(right->op_type) &&
5859 (right->op_flags & OPf_PARENS) == 0))
5860 if (ckWARN(WARN_PRECEDENCE))
5861 Perl_warner(aTHX_ packWARN(WARN_PRECEDENCE),
5862 "Possible precedence problem on bitwise %c operator",
5863 o->op_type == OP_BIT_OR ? '|'
5864 : o->op_type == OP_BIT_AND ? '&' : '^'
5871 Perl_ck_concat(pTHX_ OP *o)
5873 const OP * const kid = cUNOPo->op_first;
5874 PERL_UNUSED_CONTEXT;
5875 if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) &&
5876 !(kUNOP->op_first->op_flags & OPf_MOD))
5877 o->op_flags |= OPf_STACKED;
5882 Perl_ck_spair(pTHX_ OP *o)
5885 if (o->op_flags & OPf_KIDS) {
5888 const OPCODE type = o->op_type;
5889 o = modkids(ck_fun(o), type);
5890 kid = cUNOPo->op_first;
5891 newop = kUNOP->op_first->op_sibling;
5893 const OPCODE type = newop->op_type;
5894 if (newop->op_sibling || !(PL_opargs[type] & OA_RETSCALAR) ||
5895 type == OP_PADAV || type == OP_PADHV ||
5896 type == OP_RV2AV || type == OP_RV2HV)
5900 op_getmad(kUNOP->op_first,newop,'K');
5902 op_free(kUNOP->op_first);
5904 kUNOP->op_first = newop;
5906 o->op_ppaddr = PL_ppaddr[++o->op_type];
5911 Perl_ck_delete(pTHX_ OP *o)
5915 if (o->op_flags & OPf_KIDS) {
5916 OP * const kid = cUNOPo->op_first;
5917 switch (kid->op_type) {
5919 o->op_flags |= OPf_SPECIAL;
5922 o->op_private |= OPpSLICE;
5925 o->op_flags |= OPf_SPECIAL;
5930 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element or slice",
5939 Perl_ck_die(pTHX_ OP *o)
5942 if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH;
5948 Perl_ck_eof(pTHX_ OP *o)
5952 if (o->op_flags & OPf_KIDS) {
5953 if (cLISTOPo->op_first->op_type == OP_STUB) {
5955 = newUNOP(o->op_type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv));
5957 op_getmad(o,newop,'O');
5969 Perl_ck_eval(pTHX_ OP *o)
5972 PL_hints |= HINT_BLOCK_SCOPE;
5973 if (o->op_flags & OPf_KIDS) {
5974 SVOP * const kid = (SVOP*)cUNOPo->op_first;
5977 o->op_flags &= ~OPf_KIDS;
5980 else if (kid->op_type == OP_LINESEQ || kid->op_type == OP_STUB) {
5986 cUNOPo->op_first = 0;
5991 NewOp(1101, enter, 1, LOGOP);
5992 enter->op_type = OP_ENTERTRY;
5993 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
5994 enter->op_private = 0;
5996 /* establish postfix order */
5997 enter->op_next = (OP*)enter;
5999 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
6000 o->op_type = OP_LEAVETRY;
6001 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
6002 enter->op_other = o;
6003 op_getmad(oldo,o,'O');
6017 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
6018 op_getmad(oldo,o,'O');
6020 o->op_targ = (PADOFFSET)PL_hints;
6021 if ((PL_hints & HINT_LOCALIZE_HH) != 0 && GvHV(PL_hintgv)) {
6022 /* Store a copy of %^H that pp_entereval can pick up */
6023 OP *hhop = newSVOP(OP_CONST, 0,
6024 (SV*)Perl_hv_copy_hints_hv(aTHX_ GvHV(PL_hintgv)));
6025 cUNOPo->op_first->op_sibling = hhop;
6026 o->op_private |= OPpEVAL_HAS_HH;
6032 Perl_ck_exit(pTHX_ OP *o)
6035 HV * const table = GvHV(PL_hintgv);
6037 SV * const * const svp = hv_fetchs(table, "vmsish_exit", FALSE);
6038 if (svp && *svp && SvTRUE(*svp))
6039 o->op_private |= OPpEXIT_VMSISH;
6041 if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH;
6047 Perl_ck_exec(pTHX_ OP *o)
6049 if (o->op_flags & OPf_STACKED) {
6052 kid = cUNOPo->op_first->op_sibling;
6053 if (kid->op_type == OP_RV2GV)
6062 Perl_ck_exists(pTHX_ OP *o)
6066 if (o->op_flags & OPf_KIDS) {
6067 OP * const kid = cUNOPo->op_first;
6068 if (kid->op_type == OP_ENTERSUB) {
6069 (void) ref(kid, o->op_type);
6070 if (kid->op_type != OP_RV2CV && !PL_error_count)
6071 Perl_croak(aTHX_ "%s argument is not a subroutine name",
6073 o->op_private |= OPpEXISTS_SUB;
6075 else if (kid->op_type == OP_AELEM)
6076 o->op_flags |= OPf_SPECIAL;
6077 else if (kid->op_type != OP_HELEM)
6078 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element",
6086 Perl_ck_rvconst(pTHX_ register OP *o)
6089 SVOP * const kid = (SVOP*)cUNOPo->op_first;
6091 o->op_private |= (PL_hints & HINT_STRICT_REFS);
6092 if (o->op_type == OP_RV2CV)
6093 o->op_private &= ~1;
6095 if (kid->op_type == OP_CONST) {
6098 SV * const kidsv = kid->op_sv;
6100 /* Is it a constant from cv_const_sv()? */
6101 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
6102 SV * const rsv = SvRV(kidsv);
6103 const int svtype = SvTYPE(rsv);
6104 const char *badtype = NULL;
6106 switch (o->op_type) {
6108 if (svtype > SVt_PVMG)
6109 badtype = "a SCALAR";
6112 if (svtype != SVt_PVAV)
6113 badtype = "an ARRAY";
6116 if (svtype != SVt_PVHV)
6120 if (svtype != SVt_PVCV)
6125 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
6128 else if ((o->op_type == OP_RV2HV || o->op_type == OP_RV2SV) &&
6129 (PL_hints & HINT_STRICT_REFS) && SvPOK(kidsv)) {
6130 /* If this is an access to a stash, disable "strict refs", because
6131 * stashes aren't auto-vivified at compile-time (unless we store
6132 * symbols in them), and we don't want to produce a run-time
6133 * stricture error when auto-vivifying the stash. */
6134 const char *s = SvPV_nolen(kidsv);
6135 const STRLEN l = SvCUR(kidsv);
6136 if (l > 1 && s[l-1] == ':' && s[l-2] == ':')
6137 o->op_private &= ~HINT_STRICT_REFS;
6139 if ((o->op_private & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
6140 const char *badthing;
6141 switch (o->op_type) {
6143 badthing = "a SCALAR";
6146 badthing = "an ARRAY";
6149 badthing = "a HASH";
6157 "Can't use bareword (\"%"SVf"\") as %s ref while \"strict refs\" in use",
6158 (void*)kidsv, badthing);
6161 * This is a little tricky. We only want to add the symbol if we
6162 * didn't add it in the lexer. Otherwise we get duplicate strict
6163 * warnings. But if we didn't add it in the lexer, we must at
6164 * least pretend like we wanted to add it even if it existed before,
6165 * or we get possible typo warnings. OPpCONST_ENTERED says
6166 * whether the lexer already added THIS instance of this symbol.
6168 iscv = (o->op_type == OP_RV2CV) * 2;
6170 gv = gv_fetchsv(kidsv,
6171 iscv | !(kid->op_private & OPpCONST_ENTERED),
6174 : o->op_type == OP_RV2SV
6176 : o->op_type == OP_RV2AV
6178 : o->op_type == OP_RV2HV
6181 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
6183 kid->op_type = OP_GV;
6184 SvREFCNT_dec(kid->op_sv);
6186 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
6187 kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
6188 SvREFCNT_dec(PAD_SVl(kPADOP->op_padix));
6190 PAD_SETSV(kPADOP->op_padix, (SV*) SvREFCNT_inc_simple_NN(gv));
6192 kid->op_sv = SvREFCNT_inc_simple_NN(gv);
6194 kid->op_private = 0;
6195 kid->op_ppaddr = PL_ppaddr[OP_GV];
6202 Perl_ck_ftst(pTHX_ OP *o)
6205 const I32 type = o->op_type;
6207 if (o->op_flags & OPf_REF) {
6210 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
6211 SVOP * const kid = (SVOP*)cUNOPo->op_first;
6212 const OPCODE kidtype = kid->op_type;
6214 if (kidtype == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
6215 OP * const newop = newGVOP(type, OPf_REF,
6216 gv_fetchsv(kid->op_sv, GV_ADD, SVt_PVIO));
6218 op_getmad(o,newop,'O');
6224 if ((PL_hints & HINT_FILETEST_ACCESS) && OP_IS_FILETEST_ACCESS(o))
6225 o->op_private |= OPpFT_ACCESS;
6226 if (PL_check[kidtype] == MEMBER_TO_FPTR(Perl_ck_ftst)
6227 && kidtype != OP_STAT && kidtype != OP_LSTAT)
6228 o->op_private |= OPpFT_STACKED;
6236 if (type == OP_FTTTY)
6237 o = newGVOP(type, OPf_REF, PL_stdingv);
6239 o = newUNOP(type, 0, newDEFSVOP());
6240 op_getmad(oldo,o,'O');
6246 Perl_ck_fun(pTHX_ OP *o)
6249 const int type = o->op_type;
6250 register I32 oa = PL_opargs[type] >> OASHIFT;
6252 if (o->op_flags & OPf_STACKED) {
6253 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
6256 return no_fh_allowed(o);
6259 if (o->op_flags & OPf_KIDS) {
6260 OP **tokid = &cLISTOPo->op_first;
6261 register OP *kid = cLISTOPo->op_first;
6265 if (kid->op_type == OP_PUSHMARK ||
6266 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
6268 tokid = &kid->op_sibling;
6269 kid = kid->op_sibling;
6271 if (!kid && PL_opargs[type] & OA_DEFGV)
6272 *tokid = kid = newDEFSVOP();
6276 sibl = kid->op_sibling;
6278 if (!sibl && kid->op_type == OP_STUB) {
6285 /* list seen where single (scalar) arg expected? */
6286 if (numargs == 1 && !(oa >> 4)
6287 && kid->op_type == OP_LIST && type != OP_SCALAR)
6289 return too_many_arguments(o,PL_op_desc[type]);
6302 if ((type == OP_PUSH || type == OP_UNSHIFT)
6303 && !kid->op_sibling && ckWARN(WARN_SYNTAX))
6304 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
6305 "Useless use of %s with no values",
6308 if (kid->op_type == OP_CONST &&
6309 (kid->op_private & OPpCONST_BARE))
6311 OP * const newop = newAVREF(newGVOP(OP_GV, 0,
6312 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVAV) ));
6313 if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX))
6314 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
6315 "Array @%"SVf" missing the @ in argument %"IVdf" of %s()",
6316 (void*)((SVOP*)kid)->op_sv, (IV)numargs, PL_op_desc[type]);
6318 op_getmad(kid,newop,'K');
6323 kid->op_sibling = sibl;
6326 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
6327 bad_type(numargs, "array", PL_op_desc[type], kid);
6331 if (kid->op_type == OP_CONST &&
6332 (kid->op_private & OPpCONST_BARE))
6334 OP * const newop = newHVREF(newGVOP(OP_GV, 0,
6335 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVHV) ));
6336 if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX))
6337 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
6338 "Hash %%%"SVf" missing the %% in argument %"IVdf" of %s()",
6339 (void*)((SVOP*)kid)->op_sv, (IV)numargs, PL_op_desc[type]);
6341 op_getmad(kid,newop,'K');
6346 kid->op_sibling = sibl;
6349 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
6350 bad_type(numargs, "hash", PL_op_desc[type], kid);
6355 OP * const newop = newUNOP(OP_NULL, 0, kid);
6356 kid->op_sibling = 0;
6358 newop->op_next = newop;
6360 kid->op_sibling = sibl;
6365 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
6366 if (kid->op_type == OP_CONST &&
6367 (kid->op_private & OPpCONST_BARE))
6369 OP * const newop = newGVOP(OP_GV, 0,
6370 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVIO));
6371 if (!(o->op_private & 1) && /* if not unop */
6372 kid == cLISTOPo->op_last)
6373 cLISTOPo->op_last = newop;
6375 op_getmad(kid,newop,'K');
6381 else if (kid->op_type == OP_READLINE) {
6382 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
6383 bad_type(numargs, "HANDLE", OP_DESC(o), kid);
6386 I32 flags = OPf_SPECIAL;
6390 /* is this op a FH constructor? */
6391 if (is_handle_constructor(o,numargs)) {
6392 const char *name = NULL;
6396 /* Set a flag to tell rv2gv to vivify
6397 * need to "prove" flag does not mean something
6398 * else already - NI-S 1999/05/07
6401 if (kid->op_type == OP_PADSV) {
6402 name = PAD_COMPNAME_PV(kid->op_targ);
6403 /* SvCUR of a pad namesv can't be trusted
6404 * (see PL_generation), so calc its length
6410 else if (kid->op_type == OP_RV2SV
6411 && kUNOP->op_first->op_type == OP_GV)
6413 GV * const gv = cGVOPx_gv(kUNOP->op_first);
6415 len = GvNAMELEN(gv);
6417 else if (kid->op_type == OP_AELEM
6418 || kid->op_type == OP_HELEM)
6421 OP *op = ((BINOP*)kid)->op_first;
6425 const char * const a =
6426 kid->op_type == OP_AELEM ?
6428 if (((op->op_type == OP_RV2AV) ||
6429 (op->op_type == OP_RV2HV)) &&
6430 (firstop = ((UNOP*)op)->op_first) &&
6431 (firstop->op_type == OP_GV)) {
6432 /* packagevar $a[] or $h{} */
6433 GV * const gv = cGVOPx_gv(firstop);
6441 else if (op->op_type == OP_PADAV
6442 || op->op_type == OP_PADHV) {
6443 /* lexicalvar $a[] or $h{} */
6444 const char * const padname =
6445 PAD_COMPNAME_PV(op->op_targ);
6454 name = SvPV_const(tmpstr, len);
6459 name = "__ANONIO__";
6466 targ = pad_alloc(OP_RV2GV, SVs_PADTMP);
6467 namesv = PAD_SVl(targ);
6468 SvUPGRADE(namesv, SVt_PV);
6470 sv_setpvn(namesv, "$", 1);
6471 sv_catpvn(namesv, name, len);
6474 kid->op_sibling = 0;
6475 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
6476 kid->op_targ = targ;
6477 kid->op_private |= priv;
6479 kid->op_sibling = sibl;
6485 mod(scalar(kid), type);
6489 tokid = &kid->op_sibling;
6490 kid = kid->op_sibling;
6493 if (kid && kid->op_type != OP_STUB)
6494 return too_many_arguments(o,OP_DESC(o));
6495 o->op_private |= numargs;
6497 /* FIXME - should the numargs move as for the PERL_MAD case? */
6498 o->op_private |= numargs;
6500 return too_many_arguments(o,OP_DESC(o));
6504 else if (PL_opargs[type] & OA_DEFGV) {
6506 OP *newop = newUNOP(type, 0, newDEFSVOP());
6507 op_getmad(o,newop,'O');
6510 /* Ordering of these two is important to keep f_map.t passing. */
6512 return newUNOP(type, 0, newDEFSVOP());
6517 while (oa & OA_OPTIONAL)
6519 if (oa && oa != OA_LIST)
6520 return too_few_arguments(o,OP_DESC(o));
6526 Perl_ck_glob(pTHX_ OP *o)
6532 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
6533 append_elem(OP_GLOB, o, newDEFSVOP());
6535 if (!((gv = gv_fetchpvs("glob", GV_NOTQUAL, SVt_PVCV))
6536 && GvCVu(gv) && GvIMPORTED_CV(gv)))
6538 gv = gv_fetchpvs("CORE::GLOBAL::glob", 0, SVt_PVCV);
6541 #if !defined(PERL_EXTERNAL_GLOB)
6542 /* XXX this can be tightened up and made more failsafe. */
6543 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
6546 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
6547 newSVpvs("File::Glob"), NULL, NULL, NULL);
6548 gv = gv_fetchpvs("CORE::GLOBAL::glob", 0, SVt_PVCV);
6549 glob_gv = gv_fetchpvs("File::Glob::csh_glob", 0, SVt_PVCV);
6550 GvCV(gv) = GvCV(glob_gv);
6551 SvREFCNT_inc_void((SV*)GvCV(gv));
6552 GvIMPORTED_CV_on(gv);
6555 #endif /* PERL_EXTERNAL_GLOB */
6557 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
6558 append_elem(OP_GLOB, o,
6559 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
6560 o->op_type = OP_LIST;
6561 o->op_ppaddr = PL_ppaddr[OP_LIST];
6562 cLISTOPo->op_first->op_type = OP_PUSHMARK;
6563 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
6564 cLISTOPo->op_first->op_targ = 0;
6565 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
6566 append_elem(OP_LIST, o,
6567 scalar(newUNOP(OP_RV2CV, 0,
6568 newGVOP(OP_GV, 0, gv)))));
6569 o = newUNOP(OP_NULL, 0, ck_subr(o));
6570 o->op_targ = OP_GLOB; /* hint at what it used to be */
6573 gv = newGVgen("main");
6575 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
6581 Perl_ck_grep(pTHX_ OP *o)
6586 const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
6589 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
6590 /* don't allocate gwop here, as we may leak it if PL_error_count > 0 */
6592 if (o->op_flags & OPf_STACKED) {
6595 kid = cLISTOPo->op_first->op_sibling;
6596 if (!cUNOPx(kid)->op_next)
6597 Perl_croak(aTHX_ "panic: ck_grep");
6598 for (k = cUNOPx(kid)->op_first; k; k = k->op_next) {
6601 NewOp(1101, gwop, 1, LOGOP);
6602 kid->op_next = (OP*)gwop;
6603 o->op_flags &= ~OPf_STACKED;
6605 kid = cLISTOPo->op_first->op_sibling;
6606 if (type == OP_MAPWHILE)
6613 kid = cLISTOPo->op_first->op_sibling;
6614 if (kid->op_type != OP_NULL)
6615 Perl_croak(aTHX_ "panic: ck_grep");
6616 kid = kUNOP->op_first;
6619 NewOp(1101, gwop, 1, LOGOP);
6620 gwop->op_type = type;
6621 gwop->op_ppaddr = PL_ppaddr[type];
6622 gwop->op_first = listkids(o);
6623 gwop->op_flags |= OPf_KIDS;
6624 gwop->op_other = LINKLIST(kid);
6625 kid->op_next = (OP*)gwop;
6626 offset = pad_findmy("$_");
6627 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
6628 o->op_private = gwop->op_private = 0;
6629 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
6632 o->op_private = gwop->op_private = OPpGREP_LEX;
6633 gwop->op_targ = o->op_targ = offset;
6636 kid = cLISTOPo->op_first->op_sibling;
6637 if (!kid || !kid->op_sibling)
6638 return too_few_arguments(o,OP_DESC(o));
6639 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
6640 mod(kid, OP_GREPSTART);
6646 Perl_ck_index(pTHX_ OP *o)
6648 if (o->op_flags & OPf_KIDS) {
6649 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6651 kid = kid->op_sibling; /* get past "big" */
6652 if (kid && kid->op_type == OP_CONST)
6653 fbm_compile(((SVOP*)kid)->op_sv, 0);
6659 Perl_ck_lengthconst(pTHX_ OP *o)
6661 /* XXX length optimization goes here */
6666 Perl_ck_lfun(pTHX_ OP *o)
6668 const OPCODE type = o->op_type;
6669 return modkids(ck_fun(o), type);
6673 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
6675 if ((o->op_flags & OPf_KIDS) && ckWARN2(WARN_DEPRECATED, WARN_SYNTAX)) {
6676 switch (cUNOPo->op_first->op_type) {
6678 /* This is needed for
6679 if (defined %stash::)
6680 to work. Do not break Tk.
6682 break; /* Globals via GV can be undef */
6684 case OP_AASSIGN: /* Is this a good idea? */
6685 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
6686 "defined(@array) is deprecated");
6687 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
6688 "\t(Maybe you should just omit the defined()?)\n");
6691 /* This is needed for
6692 if (defined %stash::)
6693 to work. Do not break Tk.
6695 break; /* Globals via GV can be undef */
6697 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
6698 "defined(%%hash) is deprecated");
6699 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
6700 "\t(Maybe you should just omit the defined()?)\n");
6711 Perl_ck_rfun(pTHX_ OP *o)
6713 const OPCODE type = o->op_type;
6714 return refkids(ck_fun(o), type);
6718 Perl_ck_listiob(pTHX_ OP *o)
6722 kid = cLISTOPo->op_first;
6725 kid = cLISTOPo->op_first;
6727 if (kid->op_type == OP_PUSHMARK)
6728 kid = kid->op_sibling;
6729 if (kid && o->op_flags & OPf_STACKED)
6730 kid = kid->op_sibling;
6731 else if (kid && !kid->op_sibling) { /* print HANDLE; */
6732 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
6733 o->op_flags |= OPf_STACKED; /* make it a filehandle */
6734 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
6735 cLISTOPo->op_first->op_sibling = kid;
6736 cLISTOPo->op_last = kid;
6737 kid = kid->op_sibling;
6742 append_elem(o->op_type, o, newDEFSVOP());
6748 Perl_ck_say(pTHX_ OP *o)
6751 o->op_type = OP_PRINT;
6752 cLISTOPo->op_last = cLISTOPo->op_last->op_sibling
6753 = newSVOP(OP_CONST, 0, newSVpvs("\n"));
6758 Perl_ck_smartmatch(pTHX_ OP *o)
6761 if (0 == (o->op_flags & OPf_SPECIAL)) {
6762 OP *first = cBINOPo->op_first;
6763 OP *second = first->op_sibling;
6765 /* Implicitly take a reference to an array or hash */
6766 first->op_sibling = NULL;
6767 first = cBINOPo->op_first = ref_array_or_hash(first);
6768 second = first->op_sibling = ref_array_or_hash(second);
6770 /* Implicitly take a reference to a regular expression */
6771 if (first->op_type == OP_MATCH) {
6772 first->op_type = OP_QR;
6773 first->op_ppaddr = PL_ppaddr[OP_QR];
6775 if (second->op_type == OP_MATCH) {
6776 second->op_type = OP_QR;
6777 second->op_ppaddr = PL_ppaddr[OP_QR];
6786 Perl_ck_sassign(pTHX_ OP *o)
6788 OP * const kid = cLISTOPo->op_first;
6789 /* has a disposable target? */
6790 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
6791 && !(kid->op_flags & OPf_STACKED)
6792 /* Cannot steal the second time! */
6793 && !(kid->op_private & OPpTARGET_MY))
6795 OP * const kkid = kid->op_sibling;
6797 /* Can just relocate the target. */
6798 if (kkid && kkid->op_type == OP_PADSV
6799 && !(kkid->op_private & OPpLVAL_INTRO))
6801 kid->op_targ = kkid->op_targ;
6803 /* Now we do not need PADSV and SASSIGN. */
6804 kid->op_sibling = o->op_sibling; /* NULL */
6805 cLISTOPo->op_first = NULL;
6807 op_getmad(o,kid,'O');
6808 op_getmad(kkid,kid,'M');
6813 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
6817 if (kid->op_sibling) {
6818 OP *kkid = kid->op_sibling;
6819 if (kkid->op_type == OP_PADSV
6820 && (kkid->op_private & OPpLVAL_INTRO)
6821 && SvPAD_STATE(*av_fetch(PL_comppad_name, kkid->op_targ, FALSE))) {
6822 o->op_private |= OPpASSIGN_STATE;
6823 /* hijacking PADSTALE for uninitialized state variables */
6824 SvPADSTALE_on(PAD_SVl(kkid->op_targ));
6831 Perl_ck_match(pTHX_ OP *o)
6834 if (o->op_type != OP_QR && PL_compcv) {
6835 const PADOFFSET offset = pad_findmy("$_");
6836 if (offset != NOT_IN_PAD && !(PAD_COMPNAME_FLAGS_isOUR(offset))) {
6837 o->op_targ = offset;
6838 o->op_private |= OPpTARGET_MY;
6841 if (o->op_type == OP_MATCH || o->op_type == OP_QR)
6842 o->op_private |= OPpRUNTIME;
6847 Perl_ck_method(pTHX_ OP *o)
6849 OP * const kid = cUNOPo->op_first;
6850 if (kid->op_type == OP_CONST) {
6851 SV* sv = kSVOP->op_sv;
6852 const char * const method = SvPVX_const(sv);
6853 if (!(strchr(method, ':') || strchr(method, '\''))) {
6855 if (!SvREADONLY(sv) || !SvFAKE(sv)) {
6856 sv = newSVpvn_share(method, SvCUR(sv), 0);
6859 kSVOP->op_sv = NULL;
6861 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
6863 op_getmad(o,cmop,'O');
6874 Perl_ck_null(pTHX_ OP *o)
6876 PERL_UNUSED_CONTEXT;
6881 Perl_ck_open(pTHX_ OP *o)
6884 HV * const table = GvHV(PL_hintgv);
6886 SV **svp = hv_fetchs(table, "open_IN", FALSE);
6888 const I32 mode = mode_from_discipline(*svp);
6889 if (mode & O_BINARY)
6890 o->op_private |= OPpOPEN_IN_RAW;
6891 else if (mode & O_TEXT)
6892 o->op_private |= OPpOPEN_IN_CRLF;
6895 svp = hv_fetchs(table, "open_OUT", FALSE);
6897 const I32 mode = mode_from_discipline(*svp);
6898 if (mode & O_BINARY)
6899 o->op_private |= OPpOPEN_OUT_RAW;
6900 else if (mode & O_TEXT)
6901 o->op_private |= OPpOPEN_OUT_CRLF;
6904 if (o->op_type == OP_BACKTICK)
6907 /* In case of three-arg dup open remove strictness
6908 * from the last arg if it is a bareword. */
6909 OP * const first = cLISTOPx(o)->op_first; /* The pushmark. */
6910 OP * const last = cLISTOPx(o)->op_last; /* The bareword. */
6914 if ((last->op_type == OP_CONST) && /* The bareword. */
6915 (last->op_private & OPpCONST_BARE) &&
6916 (last->op_private & OPpCONST_STRICT) &&
6917 (oa = first->op_sibling) && /* The fh. */
6918 (oa = oa->op_sibling) && /* The mode. */
6919 (oa->op_type == OP_CONST) &&
6920 SvPOK(((SVOP*)oa)->op_sv) &&
6921 (mode = SvPVX_const(((SVOP*)oa)->op_sv)) &&
6922 mode[0] == '>' && mode[1] == '&' && /* A dup open. */
6923 (last == oa->op_sibling)) /* The bareword. */
6924 last->op_private &= ~OPpCONST_STRICT;
6930 Perl_ck_repeat(pTHX_ OP *o)
6932 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
6933 o->op_private |= OPpREPEAT_DOLIST;
6934 cBINOPo->op_first = force_list(cBINOPo->op_first);
6942 Perl_ck_require(pTHX_ OP *o)
6947 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
6948 SVOP * const kid = (SVOP*)cUNOPo->op_first;
6950 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
6951 SV * const sv = kid->op_sv;
6952 U32 was_readonly = SvREADONLY(sv);
6957 sv_force_normal_flags(sv, 0);
6958 assert(!SvREADONLY(sv));
6965 for (s = SvPVX(sv); *s; s++) {
6966 if (*s == ':' && s[1] == ':') {
6967 const STRLEN len = strlen(s+2)+1;
6969 Move(s+2, s+1, len, char);
6970 SvCUR_set(sv, SvCUR(sv) - 1);
6973 sv_catpvs(sv, ".pm");
6974 SvFLAGS(sv) |= was_readonly;
6978 if (!(o->op_flags & OPf_SPECIAL)) { /* Wasn't written as CORE::require */
6979 /* handle override, if any */
6980 gv = gv_fetchpvs("require", GV_NOTQUAL, SVt_PVCV);
6981 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
6982 GV * const * const gvp = (GV**)hv_fetchs(PL_globalstash, "require", FALSE);
6983 gv = gvp ? *gvp : NULL;
6987 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
6988 OP * const kid = cUNOPo->op_first;
6991 cUNOPo->op_first = 0;
6995 newop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
6996 append_elem(OP_LIST, kid,
6997 scalar(newUNOP(OP_RV2CV, 0,
7000 op_getmad(o,newop,'O');
7008 Perl_ck_return(pTHX_ OP *o)
7011 if (CvLVALUE(PL_compcv)) {
7013 for (kid = cLISTOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
7014 mod(kid, OP_LEAVESUBLV);
7020 Perl_ck_select(pTHX_ OP *o)
7024 if (o->op_flags & OPf_KIDS) {
7025 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
7026 if (kid && kid->op_sibling) {
7027 o->op_type = OP_SSELECT;
7028 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
7030 return fold_constants(o);
7034 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
7035 if (kid && kid->op_type == OP_RV2GV)
7036 kid->op_private &= ~HINT_STRICT_REFS;
7041 Perl_ck_shift(pTHX_ OP *o)
7044 const I32 type = o->op_type;
7046 if (!(o->op_flags & OPf_KIDS)) {
7048 /* FIXME - this can be refactored to reduce code in #ifdefs */
7050 OP * const oldo = o;
7054 argop = newUNOP(OP_RV2AV, 0,
7055 scalar(newGVOP(OP_GV, 0, CvUNIQUE(PL_compcv) ? PL_argvgv : PL_defgv)));
7057 o = newUNOP(type, 0, scalar(argop));
7058 op_getmad(oldo,o,'O');
7061 return newUNOP(type, 0, scalar(argop));
7064 return scalar(modkids(ck_fun(o), type));
7068 Perl_ck_sort(pTHX_ OP *o)
7073 if (o->op_type == OP_SORT && (PL_hints & HINT_LOCALIZE_HH) != 0) {
7074 HV * const hinthv = GvHV(PL_hintgv);
7076 SV ** const svp = hv_fetchs(hinthv, "sort", FALSE);
7078 const I32 sorthints = (I32)SvIV(*svp);
7079 if ((sorthints & HINT_SORT_QUICKSORT) != 0)
7080 o->op_private |= OPpSORT_QSORT;
7081 if ((sorthints & HINT_SORT_STABLE) != 0)
7082 o->op_private |= OPpSORT_STABLE;
7087 if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
7089 firstkid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
7090 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
7092 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */
7094 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
7096 if (kid->op_type == OP_SCOPE) {
7100 else if (kid->op_type == OP_LEAVE) {
7101 if (o->op_type == OP_SORT) {
7102 op_null(kid); /* wipe out leave */
7105 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
7106 if (k->op_next == kid)
7108 /* don't descend into loops */
7109 else if (k->op_type == OP_ENTERLOOP
7110 || k->op_type == OP_ENTERITER)
7112 k = cLOOPx(k)->op_lastop;
7117 kid->op_next = 0; /* just disconnect the leave */
7118 k = kLISTOP->op_first;
7123 if (o->op_type == OP_SORT) {
7124 /* provide scalar context for comparison function/block */
7130 o->op_flags |= OPf_SPECIAL;
7132 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
7135 firstkid = firstkid->op_sibling;
7138 /* provide list context for arguments */
7139 if (o->op_type == OP_SORT)
7146 S_simplify_sort(pTHX_ OP *o)
7149 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
7154 if (!(o->op_flags & OPf_STACKED))
7156 GvMULTI_on(gv_fetchpvs("a", GV_ADD|GV_NOTQUAL, SVt_PV));
7157 GvMULTI_on(gv_fetchpvs("b", GV_ADD|GV_NOTQUAL, SVt_PV));
7158 kid = kUNOP->op_first; /* get past null */
7159 if (kid->op_type != OP_SCOPE)
7161 kid = kLISTOP->op_last; /* get past scope */
7162 switch(kid->op_type) {
7170 k = kid; /* remember this node*/
7171 if (kBINOP->op_first->op_type != OP_RV2SV)
7173 kid = kBINOP->op_first; /* get past cmp */
7174 if (kUNOP->op_first->op_type != OP_GV)
7176 kid = kUNOP->op_first; /* get past rv2sv */
7178 if (GvSTASH(gv) != PL_curstash)
7180 gvname = GvNAME(gv);
7181 if (*gvname == 'a' && gvname[1] == '\0')
7183 else if (*gvname == 'b' && gvname[1] == '\0')
7188 kid = k; /* back to cmp */
7189 if (kBINOP->op_last->op_type != OP_RV2SV)
7191 kid = kBINOP->op_last; /* down to 2nd arg */
7192 if (kUNOP->op_first->op_type != OP_GV)
7194 kid = kUNOP->op_first; /* get past rv2sv */
7196 if (GvSTASH(gv) != PL_curstash)
7198 gvname = GvNAME(gv);
7200 ? !(*gvname == 'a' && gvname[1] == '\0')
7201 : !(*gvname == 'b' && gvname[1] == '\0'))
7203 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
7205 o->op_private |= OPpSORT_DESCEND;
7206 if (k->op_type == OP_NCMP)
7207 o->op_private |= OPpSORT_NUMERIC;
7208 if (k->op_type == OP_I_NCMP)
7209 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
7210 kid = cLISTOPo->op_first->op_sibling;
7211 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
7213 op_getmad(kid,o,'S'); /* then delete it */
7215 op_free(kid); /* then delete it */
7220 Perl_ck_split(pTHX_ OP *o)
7225 if (o->op_flags & OPf_STACKED)
7226 return no_fh_allowed(o);
7228 kid = cLISTOPo->op_first;
7229 if (kid->op_type != OP_NULL)
7230 Perl_croak(aTHX_ "panic: ck_split");
7231 kid = kid->op_sibling;
7232 op_free(cLISTOPo->op_first);
7233 cLISTOPo->op_first = kid;
7235 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvs(" "));
7236 cLISTOPo->op_last = kid; /* There was only one element previously */
7239 if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) {
7240 OP * const sibl = kid->op_sibling;
7241 kid->op_sibling = 0;
7242 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, 0);
7243 if (cLISTOPo->op_first == cLISTOPo->op_last)
7244 cLISTOPo->op_last = kid;
7245 cLISTOPo->op_first = kid;
7246 kid->op_sibling = sibl;
7249 kid->op_type = OP_PUSHRE;
7250 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
7252 if (((PMOP *)kid)->op_pmflags & PMf_GLOBAL && ckWARN(WARN_REGEXP)) {
7253 Perl_warner(aTHX_ packWARN(WARN_REGEXP),
7254 "Use of /g modifier is meaningless in split");
7257 if (!kid->op_sibling)
7258 append_elem(OP_SPLIT, o, newDEFSVOP());
7260 kid = kid->op_sibling;
7263 if (!kid->op_sibling)
7264 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
7265 assert(kid->op_sibling);
7267 kid = kid->op_sibling;
7270 if (kid->op_sibling)
7271 return too_many_arguments(o,OP_DESC(o));
7277 Perl_ck_join(pTHX_ OP *o)
7279 const OP * const kid = cLISTOPo->op_first->op_sibling;
7280 if (kid && kid->op_type == OP_MATCH) {
7281 if (ckWARN(WARN_SYNTAX)) {
7282 const REGEXP *re = PM_GETRE(kPMOP);
7283 const char *pmstr = re ? re->precomp : "STRING";
7284 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
7285 "/%s/ should probably be written as \"%s\"",
7293 Perl_ck_subr(pTHX_ OP *o)
7296 OP *prev = ((cUNOPo->op_first->op_sibling)
7297 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
7298 OP *o2 = prev->op_sibling;
7300 const char *proto = NULL;
7301 const char *proto_end = NULL;
7306 I32 contextclass = 0;
7310 o->op_private |= OPpENTERSUB_HASTARG;
7311 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
7312 if (cvop->op_type == OP_RV2CV) {
7314 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
7315 op_null(cvop); /* disable rv2cv */
7316 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
7317 if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) {
7318 GV *gv = cGVOPx_gv(tmpop);
7321 tmpop->op_private |= OPpEARLY_CV;
7325 namegv = CvANON(cv) ? gv : CvGV(cv);
7326 proto = SvPV((SV*)cv, len);
7327 proto_end = proto + len;
7329 if (CvASSERTION(cv)) {
7330 if (PL_hints & HINT_ASSERTING) {
7331 if (PERLDB_ASSERTION && PL_curstash != PL_debstash)
7332 o->op_private |= OPpENTERSUB_DB;
7336 if (!(PL_hints & HINT_ASSERTIONSSEEN) && ckWARN(WARN_ASSERTIONS)) {
7337 Perl_warner(aTHX_ packWARN(WARN_ASSERTIONS),
7338 "Impossible to activate assertion call");
7345 else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
7346 if (o2->op_type == OP_CONST)
7347 o2->op_private &= ~OPpCONST_STRICT;
7348 else if (o2->op_type == OP_LIST) {
7349 OP * const sib = ((UNOP*)o2)->op_first->op_sibling;
7350 if (sib && sib->op_type == OP_CONST)
7351 sib->op_private &= ~OPpCONST_STRICT;
7354 o->op_private |= (PL_hints & HINT_STRICT_REFS);
7355 if (PERLDB_SUB && PL_curstash != PL_debstash)
7356 o->op_private |= OPpENTERSUB_DB;
7357 while (o2 != cvop) {
7359 if (PL_madskills && o2->op_type == OP_NULL)
7360 o3 = ((UNOP*)o2)->op_first;
7364 if (proto >= proto_end)
7365 return too_many_arguments(o, gv_ename(namegv));
7385 if (o3->op_type != OP_REFGEN && o3->op_type != OP_UNDEF)
7387 arg == 1 ? "block or sub {}" : "sub {}",
7388 gv_ename(namegv), o3);
7391 /* '*' allows any scalar type, including bareword */
7394 if (o3->op_type == OP_RV2GV)
7395 goto wrapref; /* autoconvert GLOB -> GLOBref */
7396 else if (o3->op_type == OP_CONST)
7397 o3->op_private &= ~OPpCONST_STRICT;
7398 else if (o3->op_type == OP_ENTERSUB) {
7399 /* accidental subroutine, revert to bareword */
7400 OP *gvop = ((UNOP*)o3)->op_first;
7401 if (gvop && gvop->op_type == OP_NULL) {
7402 gvop = ((UNOP*)gvop)->op_first;
7404 for (; gvop->op_sibling; gvop = gvop->op_sibling)
7407 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
7408 (gvop = ((UNOP*)gvop)->op_first) &&
7409 gvop->op_type == OP_GV)
7411 GV * const gv = cGVOPx_gv(gvop);
7412 OP * const sibling = o2->op_sibling;
7413 SV * const n = newSVpvs("");
7415 OP * const oldo2 = o2;
7419 gv_fullname4(n, gv, "", FALSE);
7420 o2 = newSVOP(OP_CONST, 0, n);
7421 op_getmad(oldo2,o2,'O');
7422 prev->op_sibling = o2;
7423 o2->op_sibling = sibling;
7439 if (contextclass++ == 0) {
7440 e = strchr(proto, ']');
7441 if (!e || e == proto)
7450 const char *p = proto;
7451 const char *const end = proto;
7453 while (*--p != '[');
7454 bad_type(arg, Perl_form(aTHX_ "one of %.*s",
7456 gv_ename(namegv), o3);
7461 if (o3->op_type == OP_RV2GV)
7464 bad_type(arg, "symbol", gv_ename(namegv), o3);
7467 if (o3->op_type == OP_ENTERSUB)
7470 bad_type(arg, "subroutine entry", gv_ename(namegv),
7474 if (o3->op_type == OP_RV2SV ||
7475 o3->op_type == OP_PADSV ||
7476 o3->op_type == OP_HELEM ||
7477 o3->op_type == OP_AELEM ||
7478 o3->op_type == OP_THREADSV)
7481 bad_type(arg, "scalar", gv_ename(namegv), o3);
7484 if (o3->op_type == OP_RV2AV ||
7485 o3->op_type == OP_PADAV)
7488 bad_type(arg, "array", gv_ename(namegv), o3);
7491 if (o3->op_type == OP_RV2HV ||
7492 o3->op_type == OP_PADHV)
7495 bad_type(arg, "hash", gv_ename(namegv), o3);
7500 OP* const sib = kid->op_sibling;
7501 kid->op_sibling = 0;
7502 o2 = newUNOP(OP_REFGEN, 0, kid);
7503 o2->op_sibling = sib;
7504 prev->op_sibling = o2;
7506 if (contextclass && e) {
7521 Perl_croak(aTHX_ "Malformed prototype for %s: %"SVf,
7522 gv_ename(namegv), (void*)cv);
7527 mod(o2, OP_ENTERSUB);
7529 o2 = o2->op_sibling;
7531 if (proto && !optional && proto_end > proto &&
7532 (*proto != '@' && *proto != '%' && *proto != ';'))
7533 return too_few_arguments(o, gv_ename(namegv));
7536 OP * const oldo = o;
7540 o=newSVOP(OP_CONST, 0, newSViv(0));
7541 op_getmad(oldo,o,'O');
7547 Perl_ck_svconst(pTHX_ OP *o)
7549 PERL_UNUSED_CONTEXT;
7550 SvREADONLY_on(cSVOPo->op_sv);
7555 Perl_ck_chdir(pTHX_ OP *o)
7557 if (o->op_flags & OPf_KIDS) {
7558 SVOP * const kid = (SVOP*)cUNOPo->op_first;
7560 if (kid && kid->op_type == OP_CONST &&
7561 (kid->op_private & OPpCONST_BARE))
7563 o->op_flags |= OPf_SPECIAL;
7564 kid->op_private &= ~OPpCONST_STRICT;
7571 Perl_ck_trunc(pTHX_ OP *o)
7573 if (o->op_flags & OPf_KIDS) {
7574 SVOP *kid = (SVOP*)cUNOPo->op_first;
7576 if (kid->op_type == OP_NULL)
7577 kid = (SVOP*)kid->op_sibling;
7578 if (kid && kid->op_type == OP_CONST &&
7579 (kid->op_private & OPpCONST_BARE))
7581 o->op_flags |= OPf_SPECIAL;
7582 kid->op_private &= ~OPpCONST_STRICT;
7589 Perl_ck_unpack(pTHX_ OP *o)
7591 OP *kid = cLISTOPo->op_first;
7592 if (kid->op_sibling) {
7593 kid = kid->op_sibling;
7594 if (!kid->op_sibling)
7595 kid->op_sibling = newDEFSVOP();
7601 Perl_ck_substr(pTHX_ OP *o)
7604 if ((o->op_flags & OPf_KIDS) && (o->op_private == 4)) {
7605 OP *kid = cLISTOPo->op_first;
7607 if (kid->op_type == OP_NULL)
7608 kid = kid->op_sibling;
7610 kid->op_flags |= OPf_MOD;
7616 /* A peephole optimizer. We visit the ops in the order they're to execute.
7617 * See the comments at the top of this file for more details about when
7618 * peep() is called */
7621 Perl_peep(pTHX_ register OP *o)
7624 register OP* oldop = NULL;
7626 if (!o || o->op_opt)
7630 SAVEVPTR(PL_curcop);
7631 for (; o; o = o->op_next) {
7635 switch (o->op_type) {
7639 PL_curcop = ((COP*)o); /* for warnings */
7644 if (cSVOPo->op_private & OPpCONST_STRICT)
7645 no_bareword_allowed(o);
7647 case OP_METHOD_NAMED:
7648 /* Relocate sv to the pad for thread safety.
7649 * Despite being a "constant", the SV is written to,
7650 * for reference counts, sv_upgrade() etc. */
7652 const PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP);
7653 if (o->op_type == OP_CONST && SvPADTMP(cSVOPo->op_sv)) {
7654 /* If op_sv is already a PADTMP then it is being used by
7655 * some pad, so make a copy. */
7656 sv_setsv(PAD_SVl(ix),cSVOPo->op_sv);
7657 SvREADONLY_on(PAD_SVl(ix));
7658 SvREFCNT_dec(cSVOPo->op_sv);
7660 else if (o->op_type == OP_CONST
7661 && cSVOPo->op_sv == &PL_sv_undef) {
7662 /* PL_sv_undef is hack - it's unsafe to store it in the
7663 AV that is the pad, because av_fetch treats values of
7664 PL_sv_undef as a "free" AV entry and will merrily
7665 replace them with a new SV, causing pad_alloc to think
7666 that this pad slot is free. (When, clearly, it is not)
7668 SvOK_off(PAD_SVl(ix));
7669 SvPADTMP_on(PAD_SVl(ix));
7670 SvREADONLY_on(PAD_SVl(ix));
7673 SvREFCNT_dec(PAD_SVl(ix));
7674 SvPADTMP_on(cSVOPo->op_sv);
7675 PAD_SETSV(ix, cSVOPo->op_sv);
7676 /* XXX I don't know how this isn't readonly already. */
7677 SvREADONLY_on(PAD_SVl(ix));
7679 cSVOPo->op_sv = NULL;
7687 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
7688 if (o->op_next->op_private & OPpTARGET_MY) {
7689 if (o->op_flags & OPf_STACKED) /* chained concats */
7690 goto ignore_optimization;
7692 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
7693 o->op_targ = o->op_next->op_targ;
7694 o->op_next->op_targ = 0;
7695 o->op_private |= OPpTARGET_MY;
7698 op_null(o->op_next);
7700 ignore_optimization:
7704 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
7706 break; /* Scalar stub must produce undef. List stub is noop */
7710 if (o->op_targ == OP_NEXTSTATE
7711 || o->op_targ == OP_DBSTATE
7712 || o->op_targ == OP_SETSTATE)
7714 PL_curcop = ((COP*)o);
7716 /* XXX: We avoid setting op_seq here to prevent later calls
7717 to peep() from mistakenly concluding that optimisation
7718 has already occurred. This doesn't fix the real problem,
7719 though (See 20010220.007). AMS 20010719 */
7720 /* op_seq functionality is now replaced by op_opt */
7721 if (oldop && o->op_next) {
7722 oldop->op_next = o->op_next;
7730 if (oldop && o->op_next) {
7731 oldop->op_next = o->op_next;
7739 if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) {
7740 OP* const pop = (o->op_type == OP_PADAV) ?
7741 o->op_next : o->op_next->op_next;
7743 if (pop && pop->op_type == OP_CONST &&
7744 ((PL_op = pop->op_next)) &&
7745 pop->op_next->op_type == OP_AELEM &&
7746 !(pop->op_next->op_private &
7747 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) &&
7748 (i = SvIV(((SVOP*)pop)->op_sv) - CopARYBASE_get(PL_curcop))
7753 if (cSVOPx(pop)->op_private & OPpCONST_STRICT)
7754 no_bareword_allowed(pop);
7755 if (o->op_type == OP_GV)
7756 op_null(o->op_next);
7757 op_null(pop->op_next);
7759 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
7760 o->op_next = pop->op_next->op_next;
7761 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
7762 o->op_private = (U8)i;
7763 if (o->op_type == OP_GV) {
7768 o->op_flags |= OPf_SPECIAL;
7769 o->op_type = OP_AELEMFAST;
7775 if (o->op_next->op_type == OP_RV2SV) {
7776 if (!(o->op_next->op_private & OPpDEREF)) {
7777 op_null(o->op_next);
7778 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
7780 o->op_next = o->op_next->op_next;
7781 o->op_type = OP_GVSV;
7782 o->op_ppaddr = PL_ppaddr[OP_GVSV];
7785 else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
7786 GV * const gv = cGVOPo_gv;
7787 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
7788 /* XXX could check prototype here instead of just carping */
7789 SV * const sv = sv_newmortal();
7790 gv_efullname3(sv, gv, NULL);
7791 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
7792 "%"SVf"() called too early to check prototype",
7796 else if (o->op_next->op_type == OP_READLINE
7797 && o->op_next->op_next->op_type == OP_CONCAT
7798 && (o->op_next->op_next->op_flags & OPf_STACKED))
7800 /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */
7801 o->op_type = OP_RCATLINE;
7802 o->op_flags |= OPf_STACKED;
7803 o->op_ppaddr = PL_ppaddr[OP_RCATLINE];
7804 op_null(o->op_next->op_next);
7805 op_null(o->op_next);
7822 while (cLOGOP->op_other->op_type == OP_NULL)
7823 cLOGOP->op_other = cLOGOP->op_other->op_next;
7824 peep(cLOGOP->op_other); /* Recursive calls are not replaced by fptr calls */
7830 while (cLOOP->op_redoop->op_type == OP_NULL)
7831 cLOOP->op_redoop = cLOOP->op_redoop->op_next;
7832 peep(cLOOP->op_redoop);
7833 while (cLOOP->op_nextop->op_type == OP_NULL)
7834 cLOOP->op_nextop = cLOOP->op_nextop->op_next;
7835 peep(cLOOP->op_nextop);
7836 while (cLOOP->op_lastop->op_type == OP_NULL)
7837 cLOOP->op_lastop = cLOOP->op_lastop->op_next;
7838 peep(cLOOP->op_lastop);
7845 while (cPMOP->op_pmreplstart &&
7846 cPMOP->op_pmreplstart->op_type == OP_NULL)
7847 cPMOP->op_pmreplstart = cPMOP->op_pmreplstart->op_next;
7848 peep(cPMOP->op_pmreplstart);
7853 if (o->op_next && o->op_next->op_type == OP_NEXTSTATE
7854 && ckWARN(WARN_SYNTAX))
7856 if (o->op_next->op_sibling) {
7857 const OPCODE type = o->op_next->op_sibling->op_type;
7858 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
7859 const line_t oldline = CopLINE(PL_curcop);
7860 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_next));
7861 Perl_warner(aTHX_ packWARN(WARN_EXEC),
7862 "Statement unlikely to be reached");
7863 Perl_warner(aTHX_ packWARN(WARN_EXEC),
7864 "\t(Maybe you meant system() when you said exec()?)\n");
7865 CopLINE_set(PL_curcop, oldline);
7876 const char *key = NULL;
7881 if (((BINOP*)o)->op_last->op_type != OP_CONST)
7884 /* Make the CONST have a shared SV */
7885 svp = cSVOPx_svp(((BINOP*)o)->op_last);
7886 if ((!SvFAKE(sv = *svp) || !SvREADONLY(sv)) && !IS_PADCONST(sv)) {
7887 key = SvPV_const(sv, keylen);
7888 lexname = newSVpvn_share(key,
7889 SvUTF8(sv) ? -(I32)keylen : (I32)keylen,
7895 if ((o->op_private & (OPpLVAL_INTRO)))
7898 rop = (UNOP*)((BINOP*)o)->op_first;
7899 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
7901 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
7902 if (!SvPAD_TYPED(lexname))
7904 fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE);
7905 if (!fields || !GvHV(*fields))
7907 key = SvPV_const(*svp, keylen);
7908 if (!hv_fetch(GvHV(*fields), key,
7909 SvUTF8(*svp) ? -(I32)keylen : (I32)keylen, FALSE))
7911 Perl_croak(aTHX_ "No such class field \"%s\" "
7912 "in variable %s of type %s",
7913 key, SvPV_nolen_const(lexname), HvNAME_get(SvSTASH(lexname)));
7926 SVOP *first_key_op, *key_op;
7928 if ((o->op_private & (OPpLVAL_INTRO))
7929 /* I bet there's always a pushmark... */
7930 || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST)
7931 /* hmmm, no optimization if list contains only one key. */
7933 rop = (UNOP*)((LISTOP*)o)->op_last;
7934 if (rop->op_type != OP_RV2HV)
7936 if (rop->op_first->op_type == OP_PADSV)
7937 /* @$hash{qw(keys here)} */
7938 rop = (UNOP*)rop->op_first;
7940 /* @{$hash}{qw(keys here)} */
7941 if (rop->op_first->op_type == OP_SCOPE
7942 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
7944 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
7950 lexname = *av_fetch(PL_comppad_name, rop->op_targ, TRUE);
7951 if (!SvPAD_TYPED(lexname))
7953 fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE);
7954 if (!fields || !GvHV(*fields))
7956 /* Again guessing that the pushmark can be jumped over.... */
7957 first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling)
7958 ->op_first->op_sibling;
7959 for (key_op = first_key_op; key_op;
7960 key_op = (SVOP*)key_op->op_sibling) {
7961 if (key_op->op_type != OP_CONST)
7963 svp = cSVOPx_svp(key_op);
7964 key = SvPV_const(*svp, keylen);
7965 if (!hv_fetch(GvHV(*fields), key,
7966 SvUTF8(*svp) ? -(I32)keylen : (I32)keylen, FALSE))
7968 Perl_croak(aTHX_ "No such class field \"%s\" "
7969 "in variable %s of type %s",
7970 key, SvPV_nolen(lexname), HvNAME_get(SvSTASH(lexname)));
7977 /* will point to RV2AV or PADAV op on LHS/RHS of assign */
7981 /* check that RHS of sort is a single plain array */
7982 OP *oright = cUNOPo->op_first;
7983 if (!oright || oright->op_type != OP_PUSHMARK)
7986 /* reverse sort ... can be optimised. */
7987 if (!cUNOPo->op_sibling) {
7988 /* Nothing follows us on the list. */
7989 OP * const reverse = o->op_next;
7991 if (reverse->op_type == OP_REVERSE &&
7992 (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) {
7993 OP * const pushmark = cUNOPx(reverse)->op_first;
7994 if (pushmark && (pushmark->op_type == OP_PUSHMARK)
7995 && (cUNOPx(pushmark)->op_sibling == o)) {
7996 /* reverse -> pushmark -> sort */
7997 o->op_private |= OPpSORT_REVERSE;
7999 pushmark->op_next = oright->op_next;
8005 /* make @a = sort @a act in-place */
8009 oright = cUNOPx(oright)->op_sibling;
8012 if (oright->op_type == OP_NULL) { /* skip sort block/sub */
8013 oright = cUNOPx(oright)->op_sibling;
8017 (oright->op_type != OP_RV2AV && oright->op_type != OP_PADAV)
8018 || oright->op_next != o
8019 || (oright->op_private & OPpLVAL_INTRO)
8023 /* o2 follows the chain of op_nexts through the LHS of the
8024 * assign (if any) to the aassign op itself */
8026 if (!o2 || o2->op_type != OP_NULL)
8029 if (!o2 || o2->op_type != OP_PUSHMARK)
8032 if (o2 && o2->op_type == OP_GV)
8035 || (o2->op_type != OP_PADAV && o2->op_type != OP_RV2AV)
8036 || (o2->op_private & OPpLVAL_INTRO)
8041 if (!o2 || o2->op_type != OP_NULL)
8044 if (!o2 || o2->op_type != OP_AASSIGN
8045 || (o2->op_flags & OPf_WANT) != OPf_WANT_VOID)
8048 /* check that the sort is the first arg on RHS of assign */
8050 o2 = cUNOPx(o2)->op_first;
8051 if (!o2 || o2->op_type != OP_NULL)
8053 o2 = cUNOPx(o2)->op_first;
8054 if (!o2 || o2->op_type != OP_PUSHMARK)
8056 if (o2->op_sibling != o)
8059 /* check the array is the same on both sides */
8060 if (oleft->op_type == OP_RV2AV) {
8061 if (oright->op_type != OP_RV2AV
8062 || !cUNOPx(oright)->op_first
8063 || cUNOPx(oright)->op_first->op_type != OP_GV
8064 || cGVOPx_gv(cUNOPx(oleft)->op_first) !=
8065 cGVOPx_gv(cUNOPx(oright)->op_first)
8069 else if (oright->op_type != OP_PADAV
8070 || oright->op_targ != oleft->op_targ
8074 /* transfer MODishness etc from LHS arg to RHS arg */
8075 oright->op_flags = oleft->op_flags;
8076 o->op_private |= OPpSORT_INPLACE;
8078 /* excise push->gv->rv2av->null->aassign */
8079 o2 = o->op_next->op_next;
8080 op_null(o2); /* PUSHMARK */
8082 if (o2->op_type == OP_GV) {
8083 op_null(o2); /* GV */
8086 op_null(o2); /* RV2AV or PADAV */
8087 o2 = o2->op_next->op_next;
8088 op_null(o2); /* AASSIGN */
8090 o->op_next = o2->op_next;
8096 OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av;
8098 LISTOP *enter, *exlist;
8101 enter = (LISTOP *) o->op_next;
8104 if (enter->op_type == OP_NULL) {
8105 enter = (LISTOP *) enter->op_next;
8109 /* for $a (...) will have OP_GV then OP_RV2GV here.
8110 for (...) just has an OP_GV. */
8111 if (enter->op_type == OP_GV) {
8112 gvop = (OP *) enter;
8113 enter = (LISTOP *) enter->op_next;
8116 if (enter->op_type == OP_RV2GV) {
8117 enter = (LISTOP *) enter->op_next;
8123 if (enter->op_type != OP_ENTERITER)
8126 iter = enter->op_next;
8127 if (!iter || iter->op_type != OP_ITER)
8130 expushmark = enter->op_first;
8131 if (!expushmark || expushmark->op_type != OP_NULL
8132 || expushmark->op_targ != OP_PUSHMARK)
8135 exlist = (LISTOP *) expushmark->op_sibling;
8136 if (!exlist || exlist->op_type != OP_NULL
8137 || exlist->op_targ != OP_LIST)
8140 if (exlist->op_last != o) {
8141 /* Mmm. Was expecting to point back to this op. */
8144 theirmark = exlist->op_first;
8145 if (!theirmark || theirmark->op_type != OP_PUSHMARK)
8148 if (theirmark->op_sibling != o) {
8149 /* There's something between the mark and the reverse, eg
8150 for (1, reverse (...))
8155 ourmark = ((LISTOP *)o)->op_first;
8156 if (!ourmark || ourmark->op_type != OP_PUSHMARK)
8159 ourlast = ((LISTOP *)o)->op_last;
8160 if (!ourlast || ourlast->op_next != o)
8163 rv2av = ourmark->op_sibling;
8164 if (rv2av && rv2av->op_type == OP_RV2AV && rv2av->op_sibling == 0
8165 && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)
8166 && enter->op_flags == (OPf_WANT_LIST | OPf_KIDS)) {
8167 /* We're just reversing a single array. */
8168 rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF;
8169 enter->op_flags |= OPf_STACKED;
8172 /* We don't have control over who points to theirmark, so sacrifice
8174 theirmark->op_next = ourmark->op_next;
8175 theirmark->op_flags = ourmark->op_flags;
8176 ourlast->op_next = gvop ? gvop : (OP *) enter;
8179 enter->op_private |= OPpITER_REVERSED;
8180 iter->op_private |= OPpITER_REVERSED;
8187 UNOP *refgen, *rv2cv;
8190 /* I do not understand this, but if o->op_opt isn't set to 1,
8191 various tests in ext/B/t/bytecode.t fail with no readily
8197 if ((o->op_flags && OPf_WANT) != OPf_WANT_VOID)
8200 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
8203 rv2gv = ((BINOP *)o)->op_last;
8204 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
8207 refgen = (UNOP *)((BINOP *)o)->op_first;
8209 if (!refgen || refgen->op_type != OP_REFGEN)
8212 exlist = (LISTOP *)refgen->op_first;
8213 if (!exlist || exlist->op_type != OP_NULL
8214 || exlist->op_targ != OP_LIST)
8217 if (exlist->op_first->op_type != OP_PUSHMARK)
8220 rv2cv = (UNOP*)exlist->op_last;
8222 if (rv2cv->op_type != OP_RV2CV)
8225 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
8226 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
8227 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
8229 o->op_private |= OPpASSIGN_CV_TO_GV;
8230 rv2gv->op_private |= OPpDONT_INIT_GV;
8231 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
8247 Perl_custom_op_name(pTHX_ const OP* o)
8250 const IV index = PTR2IV(o->op_ppaddr);
8254 if (!PL_custom_op_names) /* This probably shouldn't happen */
8255 return (char *)PL_op_name[OP_CUSTOM];
8257 keysv = sv_2mortal(newSViv(index));
8259 he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0);
8261 return (char *)PL_op_name[OP_CUSTOM]; /* Don't know who you are */
8263 return SvPV_nolen(HeVAL(he));
8267 Perl_custom_op_desc(pTHX_ const OP* o)
8270 const IV index = PTR2IV(o->op_ppaddr);
8274 if (!PL_custom_op_descs)
8275 return (char *)PL_op_desc[OP_CUSTOM];
8277 keysv = sv_2mortal(newSViv(index));
8279 he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0);
8281 return (char *)PL_op_desc[OP_CUSTOM];
8283 return SvPV_nolen(HeVAL(he));
8288 /* Efficient sub that returns a constant scalar value. */
8290 const_sv_xsub(pTHX_ CV* cv)
8297 Perl_croak(aTHX_ "usage: %s::%s()",
8298 HvNAME_get(GvSTASH(CvGV(cv))), GvNAME(CvGV(cv)));
8302 ST(0) = (SV*)XSANY.any_ptr;
8308 * c-indentation-style: bsd
8310 * indent-tabs-mode: t
8313 * ex: set ts=8 sts=4 sw=4 noet: