3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * "You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
13 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
14 * youngest of the Old Took's daughters); and Mr. Drogo was his second
15 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
16 * either way, as the saying is, if you follow me." --the Gaffer
19 /* This file contains the functions that create, manipulate and optimize
20 * the OP structures that hold a compiled perl program.
22 * A Perl program is compiled into a tree of OPs. Each op contains
23 * structural pointers (eg to its siblings and the next op in the
24 * execution sequence), a pointer to the function that would execute the
25 * op, plus any data specific to that op. For example, an OP_CONST op
26 * points to the pp_const() function and to an SV containing the constant
27 * value. When pp_const() is executed, its job is to push that SV onto the
30 * OPs are mainly created by the newFOO() functions, which are mainly
31 * called from the parser (in perly.y) as the code is parsed. For example
32 * the Perl code $a + $b * $c would cause the equivalent of the following
33 * to be called (oversimplifying a bit):
35 * newBINOP(OP_ADD, flags,
37 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
40 * Note that during the build of miniperl, a temporary copy of this file
41 * is made, called opmini.c.
45 Perl's compiler is essentially a 3-pass compiler with interleaved phases:
49 An execution-order pass
51 The bottom-up pass is represented by all the "newOP" routines and
52 the ck_ routines. The bottom-upness is actually driven by yacc.
53 So at the point that a ck_ routine fires, we have no idea what the
54 context is, either upward in the syntax tree, or either forward or
55 backward in the execution order. (The bottom-up parser builds that
56 part of the execution order it knows about, but if you follow the "next"
57 links around, you'll find it's actually a closed loop through the
60 Whenever the bottom-up parser gets to a node that supplies context to
61 its components, it invokes that portion of the top-down pass that applies
62 to that part of the subtree (and marks the top node as processed, so
63 if a node further up supplies context, it doesn't have to take the
64 plunge again). As a particular subcase of this, as the new node is
65 built, it takes all the closed execution loops of its subcomponents
66 and links them into a new closed loop for the higher level node. But
67 it's still not the real execution order.
69 The actual execution order is not known till we get a grammar reduction
70 to a top-level unit like a subroutine or file that will be called by
71 "name" rather than via a "next" pointer. At that point, we can call
72 into peep() to do that code's portion of the 3rd pass. It has to be
73 recursive, but it's recursive on basic blocks, not on tree nodes.
81 #define CALL_PEEP(o) CALL_FPTR(PL_peepp)(aTHX_ o)
83 #if defined(PL_OP_SLAB_ALLOC)
85 #ifndef PERL_SLAB_SIZE
86 #define PERL_SLAB_SIZE 2048
90 Perl_Slab_Alloc(pTHX_ int m, size_t sz)
93 * To make incrementing use count easy PL_OpSlab is an I32 *
94 * To make inserting the link to slab PL_OpPtr is I32 **
95 * So compute size in units of sizeof(I32 *) as that is how Pl_OpPtr increments
96 * Add an overhead for pointer to slab and round up as a number of pointers
98 sz = (sz + 2*sizeof(I32 *) -1)/sizeof(I32 *);
99 if ((PL_OpSpace -= sz) < 0) {
100 PL_OpPtr = (I32 **) PerlMemShared_malloc(PERL_SLAB_SIZE*sizeof(I32*));
104 Zero(PL_OpPtr,PERL_SLAB_SIZE,I32 **);
105 /* We reserve the 0'th I32 sized chunk as a use count */
106 PL_OpSlab = (I32 *) PL_OpPtr;
107 /* Reduce size by the use count word, and by the size we need.
108 * Latter is to mimic the '-=' in the if() above
110 PL_OpSpace = PERL_SLAB_SIZE - (sizeof(I32)+sizeof(I32 **)-1)/sizeof(I32 **) - sz;
111 /* Allocation pointer starts at the top.
112 Theory: because we build leaves before trunk allocating at end
113 means that at run time access is cache friendly upward
115 PL_OpPtr += PERL_SLAB_SIZE;
117 assert( PL_OpSpace >= 0 );
118 /* Move the allocation pointer down */
120 assert( PL_OpPtr > (I32 **) PL_OpSlab );
121 *PL_OpPtr = PL_OpSlab; /* Note which slab it belongs to */
122 (*PL_OpSlab)++; /* Increment use count of slab */
123 assert( PL_OpPtr+sz <= ((I32 **) PL_OpSlab + PERL_SLAB_SIZE) );
124 assert( *PL_OpSlab > 0 );
125 return (void *)(PL_OpPtr + 1);
129 Perl_Slab_Free(pTHX_ void *op)
131 I32 ** const ptr = (I32 **) op;
132 I32 * const slab = ptr[-1];
133 assert( ptr-1 > (I32 **) slab );
134 assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) );
136 if (--(*slab) == 0) {
138 # define PerlMemShared PerlMem
141 PerlMemShared_free(slab);
142 if (slab == PL_OpSlab) {
149 * In the following definition, the ", Nullop" is just to make the compiler
150 * think the expression is of the right type: croak actually does a Siglongjmp.
152 #define CHECKOP(type,o) \
153 ((PL_op_mask && PL_op_mask[type]) \
154 ? ( op_free((OP*)o), \
155 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
157 : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o))
159 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
162 S_gv_ename(pTHX_ GV *gv)
164 SV* const tmpsv = sv_newmortal();
165 gv_efullname3(tmpsv, gv, Nullch);
166 return SvPV_nolen_const(tmpsv);
170 S_no_fh_allowed(pTHX_ OP *o)
172 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
178 S_too_few_arguments(pTHX_ OP *o, const char *name)
180 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
185 S_too_many_arguments(pTHX_ OP *o, const char *name)
187 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
192 S_bad_type(pTHX_ I32 n, const char *t, const char *name, const OP *kid)
194 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
195 (int)n, name, t, OP_DESC((OP *)kid)));
199 S_no_bareword_allowed(pTHX_ const OP *o)
201 qerror(Perl_mess(aTHX_
202 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
206 /* "register" allocation */
209 Perl_allocmy(pTHX_ char *name)
213 /* complain about "my $_" etc etc */
214 if (!(PL_in_my == KEY_our ||
216 (USE_UTF8_IN_NAMES && UTF8_IS_START(name[1])) ||
217 (name[1] == '_' && (int)strlen(name) > 2)))
219 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
220 /* 1999-02-27 mjd@plover.com */
222 p = strchr(name, '\0');
223 /* The next block assumes the buffer is at least 205 chars
224 long. At present, it's always at least 256 chars. */
226 strcpy(name+200, "...");
232 /* Move everything else down one character */
233 for (; p-name > 2; p--)
235 name[2] = toCTRL(name[1]);
238 yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
240 /* check for duplicate declaration */
242 (bool)(PL_in_my == KEY_our),
243 (PL_curstash ? PL_curstash : PL_defstash)
246 if (PL_in_my_stash && *name != '$') {
247 yyerror(Perl_form(aTHX_
248 "Can't declare class for non-scalar %s in \"%s\"",
249 name, PL_in_my == KEY_our ? "our" : "my"));
252 /* allocate a spare slot and store the name in that slot */
254 off = pad_add_name(name,
257 ? (PL_curstash ? PL_curstash : PL_defstash)
266 #ifdef USE_5005THREADS
267 /* find_threadsv is not reentrant */
269 Perl_find_threadsv(pTHX_ const char *name)
274 /* We currently only handle names of a single character */
275 p = strchr(PL_threadsv_names, *name);
278 key = p - PL_threadsv_names;
279 MUTEX_LOCK(&thr->mutex);
280 svp = av_fetch(thr->threadsv, key, FALSE);
282 MUTEX_UNLOCK(&thr->mutex);
284 SV *sv = NEWSV(0, 0);
285 av_store(thr->threadsv, key, sv);
286 thr->threadsvp = AvARRAY(thr->threadsv);
287 MUTEX_UNLOCK(&thr->mutex);
289 * Some magic variables used to be automagically initialised
290 * in gv_fetchpv. Those which are now per-thread magicals get
291 * initialised here instead.
297 sv_setpv(sv, "\034");
298 sv_magic(sv, 0, PERL_MAGIC_sv, name, 1);
303 PL_sawampersand = TRUE;
317 /* XXX %! tied to Errno.pm needs to be added here.
318 * See gv_fetchpv(). */
322 sv_magic(sv, 0, PERL_MAGIC_sv, name, 1);
324 DEBUG_S(PerlIO_printf(Perl_error_log,
325 "find_threadsv: new SV %p for $%s%c\n",
326 sv, (*name < 32) ? "^" : "",
327 (*name < 32) ? toCTRL(*name) : *name));
331 #endif /* USE_5005THREADS */
336 Perl_op_free(pTHX_ OP *o)
341 if (!o || o->op_seq == (U16)-1)
344 if (o->op_private & OPpREFCOUNTED) {
345 switch (o->op_type) {
353 refcnt = OpREFCNT_dec(o);
363 if (o->op_flags & OPf_KIDS) {
364 register OP *kid, *nextkid;
365 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
366 nextkid = kid->op_sibling; /* Get before next freeing kid */
372 type = (OPCODE)o->op_targ;
374 /* COP* is not cleared by op_clear() so that we may track line
375 * numbers etc even after null() */
376 if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
384 Perl_op_clear(pTHX_ OP *o)
387 switch (o->op_type) {
388 case OP_NULL: /* Was holding old type, if any. */
389 case OP_ENTEREVAL: /* Was holding hints. */
390 #ifdef USE_5005THREADS
391 case OP_THREADSV: /* Was holding index into thr->threadsv AV. */
395 #ifdef USE_5005THREADS
397 if (!(o->op_flags & OPf_SPECIAL))
400 #endif /* USE_5005THREADS */
402 if (!(o->op_flags & OPf_REF)
403 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
409 if (! (o->op_type == OP_AELEMFAST && o->op_flags & OPf_SPECIAL)) {
410 /* not an OP_PADAV replacement */
412 if (cPADOPo->op_padix > 0) {
413 /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
414 * may still exist on the pad */
415 pad_swipe(cPADOPo->op_padix, TRUE);
416 cPADOPo->op_padix = 0;
419 SvREFCNT_dec(cSVOPo->op_sv);
420 cSVOPo->op_sv = Nullsv;
424 case OP_METHOD_NAMED:
426 SvREFCNT_dec(cSVOPo->op_sv);
427 cSVOPo->op_sv = Nullsv;
430 Even if op_clear does a pad_free for the target of the op,
431 pad_free doesn't actually remove the sv that exists in the pad;
432 instead it lives on. This results in that it could be reused as
433 a target later on when the pad was reallocated.
436 pad_swipe(o->op_targ,1);
445 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
449 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
450 SvREFCNT_dec(cSVOPo->op_sv);
451 cSVOPo->op_sv = Nullsv;
454 Safefree(cPVOPo->op_pv);
455 cPVOPo->op_pv = Nullch;
459 op_free(cPMOPo->op_pmreplroot);
463 if (INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot)) {
464 /* No GvIN_PAD_off here, because other references may still
465 * exist on the pad */
466 pad_swipe(INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot), TRUE);
469 SvREFCNT_dec((SV*)cPMOPo->op_pmreplroot);
476 HV *pmstash = PmopSTASH(cPMOPo);
477 if (pmstash && SvREFCNT(pmstash)) {
478 PMOP *pmop = HvPMROOT(pmstash);
479 PMOP *lastpmop = NULL;
481 if (cPMOPo == pmop) {
483 lastpmop->op_pmnext = pmop->op_pmnext;
485 HvPMROOT(pmstash) = pmop->op_pmnext;
489 pmop = pmop->op_pmnext;
492 PmopSTASH_free(cPMOPo);
494 cPMOPo->op_pmreplroot = Nullop;
495 /* we use the "SAFE" version of the PM_ macros here
496 * since sv_clean_all might release some PMOPs
497 * after PL_regex_padav has been cleared
498 * and the clearing of PL_regex_padav needs to
499 * happen before sv_clean_all
501 ReREFCNT_dec(PM_GETRE_SAFE(cPMOPo));
502 PM_SETRE_SAFE(cPMOPo, (REGEXP*)NULL);
504 if(PL_regex_pad) { /* We could be in destruction */
505 av_push((AV*) PL_regex_pad[0],(SV*) PL_regex_pad[(cPMOPo)->op_pmoffset]);
506 SvREPADTMP_on(PL_regex_pad[(cPMOPo)->op_pmoffset]);
507 PM_SETRE(cPMOPo, (cPMOPo)->op_pmoffset);
514 if (o->op_targ > 0) {
515 pad_free(o->op_targ);
521 S_cop_free(pTHX_ COP* cop)
523 Safefree(cop->cop_label); /* FIXME: treaddead ??? */
526 if (! specialWARN(cop->cop_warnings))
527 SvREFCNT_dec(cop->cop_warnings);
528 if (! specialCopIO(cop->cop_io)) {
532 char *s = SvPV(cop->cop_io,len);
533 Perl_warn(aTHX_ "io='%.*s'",(int) len,s); /* ??? --jhi */
536 SvREFCNT_dec(cop->cop_io);
542 Perl_op_null(pTHX_ OP *o)
544 if (o->op_type == OP_NULL)
547 o->op_targ = o->op_type;
548 o->op_type = OP_NULL;
549 o->op_ppaddr = PL_ppaddr[OP_NULL];
553 Perl_op_refcnt_lock(pTHX)
559 Perl_op_refcnt_unlock(pTHX)
564 /* Contextualizers */
566 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
569 Perl_linklist(pTHX_ OP *o)
575 /* establish postfix order */
576 if (cUNOPo->op_first) {
578 o->op_next = LINKLIST(cUNOPo->op_first);
579 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
581 kid->op_next = LINKLIST(kid->op_sibling);
593 Perl_scalarkids(pTHX_ OP *o)
595 if (o && o->op_flags & OPf_KIDS) {
597 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
604 S_scalarboolean(pTHX_ OP *o)
606 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
607 if (ckWARN(WARN_SYNTAX)) {
608 const line_t oldline = CopLINE(PL_curcop);
610 if (PL_copline != NOLINE)
611 CopLINE_set(PL_curcop, PL_copline);
612 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
613 CopLINE_set(PL_curcop, oldline);
620 Perl_scalar(pTHX_ OP *o)
624 /* assumes no premature commitment */
625 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
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)
693 const char* useless = 0;
697 if (o->op_type == OP_NEXTSTATE
698 || o->op_type == OP_SETSTATE
699 || o->op_type == OP_DBSTATE
700 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
701 || o->op_targ == OP_SETSTATE
702 || o->op_targ == OP_DBSTATE)))
703 PL_curcop = (COP*)o; /* for warning below */
705 /* assumes no premature commitment */
706 want = o->op_flags & OPf_WANT;
707 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
708 || o->op_type == OP_RETURN)
713 if ((o->op_private & OPpTARGET_MY)
714 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
716 return scalar(o); /* As if inside SASSIGN */
719 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
721 switch (o->op_type) {
723 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
727 if (o->op_flags & OPf_STACKED)
731 if (o->op_private == 4)
803 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
804 useless = OP_DESC(o);
811 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
812 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
813 useless = "a variable";
818 if (cSVOPo->op_private & OPpCONST_STRICT)
819 no_bareword_allowed(o);
821 if (ckWARN(WARN_VOID)) {
822 useless = "a constant";
823 /* don't warn on optimised away booleans, eg
824 * use constant Foo, 5; Foo || print; */
825 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
827 /* the constants 0 and 1 are permitted as they are
828 conventionally used as dummies in constructs like
829 1 while some_condition_with_side_effects; */
830 else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
832 else if (SvPOK(sv)) {
833 /* perl4's way of mixing documentation and code
834 (before the invention of POD) was based on a
835 trick to mix nroff and perl code. The trick was
836 built upon these three nroff macros being used in
837 void context. The pink camel has the details in
838 the script wrapman near page 319. */
839 if (strnEQ(SvPVX_const(sv), "di", 2) ||
840 strnEQ(SvPVX_const(sv), "ds", 2) ||
841 strnEQ(SvPVX_const(sv), "ig", 2))
846 op_null(o); /* don't execute or even remember it */
850 o->op_type = OP_PREINC; /* pre-increment is faster */
851 o->op_ppaddr = PL_ppaddr[OP_PREINC];
855 o->op_type = OP_PREDEC; /* pre-decrement is faster */
856 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
860 o->op_type = OP_I_PREINC; /* pre-increment is faster */
861 o->op_ppaddr = PL_ppaddr[OP_I_PREINC];
865 o->op_type = OP_I_PREDEC; /* pre-decrement is faster */
866 o->op_ppaddr = PL_ppaddr[OP_I_PREDEC];
872 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
877 if (o->op_flags & OPf_STACKED)
884 if (!(o->op_flags & OPf_KIDS))
893 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
900 /* all requires must return a boolean value */
901 o->op_flags &= ~OPf_WANT;
906 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
907 if (!kPMOP->op_pmreplroot)
908 deprecate_old("implicit split to @_");
912 if (useless && ckWARN(WARN_VOID))
913 Perl_warner(aTHX_ packWARN(WARN_VOID), "Useless use of %s in void context", useless);
918 Perl_listkids(pTHX_ OP *o)
920 if (o && o->op_flags & OPf_KIDS) {
922 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
929 Perl_list(pTHX_ OP *o)
933 /* assumes no premature commitment */
934 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
935 || o->op_type == OP_RETURN)
940 if ((o->op_private & OPpTARGET_MY)
941 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
943 return o; /* As if inside SASSIGN */
946 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
948 switch (o->op_type) {
951 list(cBINOPo->op_first);
956 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
964 if (!(o->op_flags & OPf_KIDS))
966 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
967 list(cBINOPo->op_first);
968 return gen_constant_list(o);
975 kid = cLISTOPo->op_first;
977 while ((kid = kid->op_sibling)) {
983 WITH_THR(PL_curcop = &PL_compiling);
987 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
993 WITH_THR(PL_curcop = &PL_compiling);
996 /* all requires must return a boolean value */
997 o->op_flags &= ~OPf_WANT;
1004 Perl_scalarseq(pTHX_ OP *o)
1007 if (o->op_type == OP_LINESEQ ||
1008 o->op_type == OP_SCOPE ||
1009 o->op_type == OP_LEAVE ||
1010 o->op_type == OP_LEAVETRY)
1013 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1014 if (kid->op_sibling) {
1018 PL_curcop = &PL_compiling;
1020 o->op_flags &= ~OPf_PARENS;
1021 if (PL_hints & HINT_BLOCK_SCOPE)
1022 o->op_flags |= OPf_PARENS;
1025 o = newOP(OP_STUB, 0);
1030 S_modkids(pTHX_ OP *o, I32 type)
1032 if (o && o->op_flags & OPf_KIDS) {
1034 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1041 Perl_mod(pTHX_ OP *o, I32 type)
1045 if (!o || PL_error_count)
1048 if ((o->op_private & OPpTARGET_MY)
1049 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1054 switch (o->op_type) {
1059 if (!(o->op_private & (OPpCONST_ARYBASE)))
1061 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1062 PL_compiling.cop_arybase = (I32)SvIV(cSVOPx(PL_eval_start)->op_sv);
1066 SAVEI32(PL_compiling.cop_arybase);
1067 PL_compiling.cop_arybase = 0;
1069 else if (type == OP_REFGEN)
1072 Perl_croak(aTHX_ "That use of $[ is unsupported");
1075 if (o->op_flags & OPf_PARENS)
1079 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1080 !(o->op_flags & OPf_STACKED)) {
1081 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1082 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1083 assert(cUNOPo->op_first->op_type == OP_NULL);
1084 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1087 else if (o->op_private & OPpENTERSUB_NOMOD)
1089 else { /* lvalue subroutine call */
1090 o->op_private |= OPpLVAL_INTRO;
1091 PL_modcount = RETURN_UNLIMITED_NUMBER;
1092 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
1093 /* Backward compatibility mode: */
1094 o->op_private |= OPpENTERSUB_INARGS;
1097 else { /* Compile-time error message: */
1098 OP *kid = cUNOPo->op_first;
1102 if (kid->op_type == OP_PUSHMARK)
1104 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1106 "panic: unexpected lvalue entersub "
1107 "args: type/targ %ld:%"UVuf,
1108 (long)kid->op_type, (UV)kid->op_targ);
1109 kid = kLISTOP->op_first;
1111 while (kid->op_sibling)
1112 kid = kid->op_sibling;
1113 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1115 if (kid->op_type == OP_METHOD_NAMED
1116 || kid->op_type == OP_METHOD)
1120 NewOp(1101, newop, 1, UNOP);
1121 newop->op_type = OP_RV2CV;
1122 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1123 newop->op_first = Nullop;
1124 newop->op_next = (OP*)newop;
1125 kid->op_sibling = (OP*)newop;
1126 newop->op_private |= OPpLVAL_INTRO;
1130 if (kid->op_type != OP_RV2CV)
1132 "panic: unexpected lvalue entersub "
1133 "entry via type/targ %ld:%"UVuf,
1134 (long)kid->op_type, (UV)kid->op_targ);
1135 kid->op_private |= OPpLVAL_INTRO;
1136 break; /* Postpone until runtime */
1140 kid = kUNOP->op_first;
1141 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1142 kid = kUNOP->op_first;
1143 if (kid->op_type == OP_NULL)
1145 "Unexpected constant lvalue entersub "
1146 "entry via type/targ %ld:%"UVuf,
1147 (long)kid->op_type, (UV)kid->op_targ);
1148 if (kid->op_type != OP_GV) {
1149 /* Restore RV2CV to check lvalueness */
1151 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1152 okid->op_next = kid->op_next;
1153 kid->op_next = okid;
1156 okid->op_next = Nullop;
1157 okid->op_type = OP_RV2CV;
1159 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1160 okid->op_private |= OPpLVAL_INTRO;
1164 cv = GvCV(kGVOP_gv);
1174 /* grep, foreach, subcalls, refgen */
1175 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1177 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1178 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1180 : (o->op_type == OP_ENTERSUB
1181 ? "non-lvalue subroutine call"
1183 type ? PL_op_desc[type] : "local"));
1197 case OP_RIGHT_SHIFT:
1206 if (!(o->op_flags & OPf_STACKED))
1212 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1218 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1219 PL_modcount = RETURN_UNLIMITED_NUMBER;
1220 return o; /* Treat \(@foo) like ordinary list. */
1224 if (scalar_mod_type(o, type))
1226 ref(cUNOPo->op_first, o->op_type);
1230 if (type == OP_LEAVESUBLV)
1231 o->op_private |= OPpMAYBE_LVSUB;
1236 PL_modcount = RETURN_UNLIMITED_NUMBER;
1239 ref(cUNOPo->op_first, o->op_type);
1243 PL_hints |= HINT_BLOCK_SCOPE;
1248 /* Needed if maint gets patch 19588
1256 PL_modcount = RETURN_UNLIMITED_NUMBER;
1257 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1258 return o; /* Treat \(@foo) like ordinary list. */
1259 if (scalar_mod_type(o, type))
1261 if (type == OP_LEAVESUBLV)
1262 o->op_private |= OPpMAYBE_LVSUB;
1267 { /* XXX DAPM 2002.08.25 tmp assert test */
1268 /* XXX */ assert(av_fetch(PL_comppad_name, (o->op_targ), FALSE));
1269 /* XXX */ assert(*av_fetch(PL_comppad_name, (o->op_targ), FALSE));
1271 Perl_croak(aTHX_ "Can't localize lexical variable %s",
1272 PAD_COMPNAME_PV(o->op_targ));
1276 #ifdef USE_5005THREADS
1278 PL_modcount++; /* XXX ??? */
1280 #endif /* USE_5005THREADS */
1286 if (type != OP_SASSIGN)
1290 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1295 if (type == OP_LEAVESUBLV)
1296 o->op_private |= OPpMAYBE_LVSUB;
1298 pad_free(o->op_targ);
1299 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1300 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1301 if (o->op_flags & OPf_KIDS)
1302 mod(cBINOPo->op_first->op_sibling, type);
1307 ref(cBINOPo->op_first, o->op_type);
1308 if (type == OP_ENTERSUB &&
1309 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1310 o->op_private |= OPpLVAL_DEFER;
1311 if (type == OP_LEAVESUBLV)
1312 o->op_private |= OPpMAYBE_LVSUB;
1320 if (o->op_flags & OPf_KIDS)
1321 mod(cLISTOPo->op_last, type);
1325 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1327 else if (!(o->op_flags & OPf_KIDS))
1329 if (o->op_targ != OP_LIST) {
1330 mod(cBINOPo->op_first, type);
1335 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1340 if (type != OP_LEAVESUBLV)
1342 break; /* mod()ing was handled by ck_return() */
1345 /* [20011101.069] File test operators interpret OPf_REF to mean that
1346 their argument is a filehandle; thus \stat(".") should not set
1348 if (type == OP_REFGEN &&
1349 PL_check[o->op_type] == MEMBER_TO_FPTR(Perl_ck_ftst))
1352 if (type != OP_LEAVESUBLV)
1353 o->op_flags |= OPf_MOD;
1355 if (type == OP_AASSIGN || type == OP_SASSIGN)
1356 o->op_flags |= OPf_SPECIAL|OPf_REF;
1358 o->op_private |= OPpLVAL_INTRO;
1359 o->op_flags &= ~OPf_SPECIAL;
1360 PL_hints |= HINT_BLOCK_SCOPE;
1362 else if (type != OP_GREPSTART && type != OP_ENTERSUB
1363 && type != OP_LEAVESUBLV)
1364 o->op_flags |= OPf_REF;
1369 S_scalar_mod_type(pTHX_ const OP *o, I32 type)
1373 if (o->op_type == OP_RV2GV)
1397 case OP_RIGHT_SHIFT:
1416 S_is_handle_constructor(pTHX_ const OP *o, I32 numargs)
1418 switch (o->op_type) {
1426 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1439 Perl_refkids(pTHX_ OP *o, I32 type)
1441 if (o && o->op_flags & OPf_KIDS) {
1443 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1450 Perl_ref(pTHX_ OP *o, I32 type)
1454 if (!o || PL_error_count)
1457 switch (o->op_type) {
1459 if ((type == OP_EXISTS || type == OP_DEFINED || type == OP_LOCK) &&
1460 !(o->op_flags & OPf_STACKED)) {
1461 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1462 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1463 assert(cUNOPo->op_first->op_type == OP_NULL);
1464 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1465 o->op_flags |= OPf_SPECIAL;
1470 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1474 if (type == OP_DEFINED)
1475 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1476 ref(cUNOPo->op_first, o->op_type);
1479 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1480 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1481 : type == OP_RV2HV ? OPpDEREF_HV
1483 o->op_flags |= OPf_MOD;
1488 o->op_flags |= OPf_MOD; /* XXX ??? */
1493 o->op_flags |= OPf_REF;
1496 if (type == OP_DEFINED)
1497 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1498 ref(cUNOPo->op_first, o->op_type);
1503 o->op_flags |= OPf_REF;
1508 if (!(o->op_flags & OPf_KIDS))
1510 ref(cBINOPo->op_first, type);
1514 ref(cBINOPo->op_first, o->op_type);
1515 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1516 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1517 : type == OP_RV2HV ? OPpDEREF_HV
1519 o->op_flags |= OPf_MOD;
1527 if (!(o->op_flags & OPf_KIDS))
1529 ref(cLISTOPo->op_last, type);
1539 S_dup_attrlist(pTHX_ OP *o)
1543 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1544 * where the first kid is OP_PUSHMARK and the remaining ones
1545 * are OP_CONST. We need to push the OP_CONST values.
1547 if (o->op_type == OP_CONST)
1548 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc(cSVOPo->op_sv));
1550 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1551 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1552 if (o->op_type == OP_CONST)
1553 rop = append_elem(OP_LIST, rop,
1554 newSVOP(OP_CONST, o->op_flags,
1555 SvREFCNT_inc(cSVOPo->op_sv)));
1562 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs, bool for_my)
1566 /* fake up C<use attributes $pkg,$rv,@attrs> */
1567 ENTER; /* need to protect against side-effects of 'use' */
1570 stashsv = newSVpv(HvNAME_get(stash), 0);
1572 stashsv = &PL_sv_no;
1574 #define ATTRSMODULE "attributes"
1575 #define ATTRSMODULE_PM "attributes.pm"
1578 /* Don't force the C<use> if we don't need it. */
1579 SV **svp = hv_fetch(GvHVn(PL_incgv), ATTRSMODULE_PM,
1580 sizeof(ATTRSMODULE_PM)-1, 0);
1581 if (svp && *svp != &PL_sv_undef)
1582 ; /* already in %INC */
1584 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
1585 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1589 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1590 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1592 prepend_elem(OP_LIST,
1593 newSVOP(OP_CONST, 0, stashsv),
1594 prepend_elem(OP_LIST,
1595 newSVOP(OP_CONST, 0,
1597 dup_attrlist(attrs))));
1603 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
1605 OP *pack, *imop, *arg;
1611 assert(target->op_type == OP_PADSV ||
1612 target->op_type == OP_PADHV ||
1613 target->op_type == OP_PADAV);
1615 /* Ensure that attributes.pm is loaded. */
1616 apply_attrs(stash, PAD_SV(target->op_targ), attrs, TRUE);
1618 /* Need package name for method call. */
1619 pack = newSVOP(OP_CONST, 0, newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1));
1621 /* Build up the real arg-list. */
1623 stashsv = newSVpv(HvNAME_get(stash), 0);
1625 stashsv = &PL_sv_no;
1626 arg = newOP(OP_PADSV, 0);
1627 arg->op_targ = target->op_targ;
1628 arg = prepend_elem(OP_LIST,
1629 newSVOP(OP_CONST, 0, stashsv),
1630 prepend_elem(OP_LIST,
1631 newUNOP(OP_REFGEN, 0,
1632 mod(arg, OP_REFGEN)),
1633 dup_attrlist(attrs)));
1635 /* Fake up a method call to import */
1636 meth = newSVpvn("import", 6);
1637 (void)SvUPGRADE(meth, SVt_PVIV);
1638 (void)SvIOK_on(meth);
1641 PERL_HASH(hash, SvPVX_const(meth), SvCUR(meth));
1642 SvUV_set(meth, hash);
1644 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
1645 append_elem(OP_LIST,
1646 prepend_elem(OP_LIST, pack, list(arg)),
1647 newSVOP(OP_METHOD_NAMED, 0, meth)));
1648 imop->op_private |= OPpENTERSUB_NOMOD;
1650 /* Combine the ops. */
1651 *imopsp = append_elem(OP_LIST, *imopsp, imop);
1655 =notfor apidoc apply_attrs_string
1657 Attempts to apply a list of attributes specified by the C<attrstr> and
1658 C<len> arguments to the subroutine identified by the C<cv> argument which
1659 is expected to be associated with the package identified by the C<stashpv>
1660 argument (see L<attributes>). It gets this wrong, though, in that it
1661 does not correctly identify the boundaries of the individual attribute
1662 specifications within C<attrstr>. This is not really intended for the
1663 public API, but has to be listed here for systems such as AIX which
1664 need an explicit export list for symbols. (It's called from XS code
1665 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
1666 to respect attribute syntax properly would be welcome.
1672 Perl_apply_attrs_string(pTHX_ char *stashpv, CV *cv,
1673 char *attrstr, STRLEN len)
1678 len = strlen(attrstr);
1682 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
1684 const char * const sstr = attrstr;
1685 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
1686 attrs = append_elem(OP_LIST, attrs,
1687 newSVOP(OP_CONST, 0,
1688 newSVpvn(sstr, attrstr-sstr)));
1692 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1693 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1694 Nullsv, prepend_elem(OP_LIST,
1695 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
1696 prepend_elem(OP_LIST,
1697 newSVOP(OP_CONST, 0,
1703 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
1707 if (!o || PL_error_count)
1711 if (type == OP_LIST) {
1713 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1714 my_kid(kid, attrs, imopsp);
1715 } else if (type == OP_UNDEF) {
1717 } else if (type == OP_RV2SV || /* "our" declaration */
1719 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
1720 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
1721 yyerror(Perl_form(aTHX_ "Can't declare %s in %s",
1722 OP_DESC(o), PL_in_my == KEY_our ? "our" : "my"));
1724 GV *gv = cGVOPx_gv(cUNOPo->op_first);
1726 PL_in_my_stash = Nullhv;
1727 apply_attrs(GvSTASH(gv),
1728 (type == OP_RV2SV ? GvSV(gv) :
1729 type == OP_RV2AV ? (SV*)GvAV(gv) :
1730 type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)gv),
1733 o->op_private |= OPpOUR_INTRO;
1736 else if (type != OP_PADSV &&
1739 type != OP_PUSHMARK)
1741 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
1743 PL_in_my == KEY_our ? "our" : "my"));
1746 else if (attrs && type != OP_PUSHMARK) {
1750 PL_in_my_stash = Nullhv;
1752 /* check for C<my Dog $spot> when deciding package */
1753 stash = PAD_COMPNAME_TYPE(o->op_targ);
1755 stash = PL_curstash;
1756 apply_attrs_my(stash, o, attrs, imopsp);
1758 o->op_flags |= OPf_MOD;
1759 o->op_private |= OPpLVAL_INTRO;
1764 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1767 int maybe_scalar = 0;
1769 /* [perl #17376]: this appears to be premature, and results in code such as
1770 C< our(%x); > executing in list mode rather than void mode */
1772 if (o->op_flags & OPf_PARENS)
1781 o = my_kid(o, attrs, &rops);
1783 if (maybe_scalar && o->op_type == OP_PADSV) {
1784 o = scalar(append_list(OP_LIST, (LISTOP*)rops, (LISTOP*)o));
1785 o->op_private |= OPpLVAL_INTRO;
1788 o = append_list(OP_LIST, (LISTOP*)o, (LISTOP*)rops);
1791 PL_in_my_stash = Nullhv;
1796 Perl_my(pTHX_ OP *o)
1798 return my_attrs(o, Nullop);
1802 Perl_sawparens(pTHX_ OP *o)
1805 o->op_flags |= OPf_PARENS;
1810 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
1814 if ( (left->op_type == OP_RV2AV ||
1815 left->op_type == OP_RV2HV ||
1816 left->op_type == OP_PADAV ||
1817 left->op_type == OP_PADHV)
1818 && ckWARN(WARN_MISC))
1820 const char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
1821 right->op_type == OP_TRANS)
1822 ? right->op_type : OP_MATCH];
1823 const char *sample = ((left->op_type == OP_RV2AV ||
1824 left->op_type == OP_PADAV)
1825 ? "@array" : "%hash");
1826 Perl_warner(aTHX_ packWARN(WARN_MISC),
1827 "Applying %s to %s will act on scalar(%s)",
1828 desc, sample, sample);
1831 if (right->op_type == OP_CONST &&
1832 cSVOPx(right)->op_private & OPpCONST_BARE &&
1833 cSVOPx(right)->op_private & OPpCONST_STRICT)
1835 no_bareword_allowed(right);
1838 if (!(right->op_flags & OPf_STACKED) &&
1839 (right->op_type == OP_MATCH ||
1840 right->op_type == OP_SUBST ||
1841 right->op_type == OP_TRANS)) {
1842 right->op_flags |= OPf_STACKED;
1843 if (right->op_type != OP_MATCH &&
1844 ! (right->op_type == OP_TRANS &&
1845 right->op_private & OPpTRANS_IDENTICAL))
1846 left = mod(left, right->op_type);
1847 if (right->op_type == OP_TRANS)
1848 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1850 o = prepend_elem(right->op_type, scalar(left), right);
1852 return newUNOP(OP_NOT, 0, scalar(o));
1856 return bind_match(type, left,
1857 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1861 Perl_invert(pTHX_ OP *o)
1865 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1866 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1870 Perl_scope(pTHX_ OP *o)
1873 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
1874 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1875 o->op_type = OP_LEAVE;
1876 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
1878 else if (o->op_type == OP_LINESEQ) {
1880 o->op_type = OP_SCOPE;
1881 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
1882 kid = ((LISTOP*)o)->op_first;
1883 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
1887 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1892 /* XXX kept for BINCOMPAT only */
1894 Perl_save_hints(pTHX)
1896 Perl_croak(aTHX_ "internal error: obsolete function save_hints() called");
1900 Perl_block_start(pTHX_ int full)
1902 const int retval = PL_savestack_ix;
1903 /* If there were syntax errors, don't try to start a block */
1904 if (PL_yynerrs) return retval;
1906 pad_block_start(full);
1908 PL_hints &= ~HINT_BLOCK_SCOPE;
1909 SAVESPTR(PL_compiling.cop_warnings);
1910 if (! specialWARN(PL_compiling.cop_warnings)) {
1911 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1912 SAVEFREESV(PL_compiling.cop_warnings) ;
1914 SAVESPTR(PL_compiling.cop_io);
1915 if (! specialCopIO(PL_compiling.cop_io)) {
1916 PL_compiling.cop_io = newSVsv(PL_compiling.cop_io) ;
1917 SAVEFREESV(PL_compiling.cop_io) ;
1923 Perl_block_end(pTHX_ I32 floor, OP *seq)
1925 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
1926 OP* retval = scalarseq(seq);
1927 /* If there were syntax errors, don't try to close a block */
1928 if (PL_yynerrs) return retval;
1930 PL_compiling.op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
1932 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1940 #ifdef USE_5005THREADS
1941 OP *o = newOP(OP_THREADSV, 0);
1942 o->op_targ = find_threadsv("_");
1945 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
1946 #endif /* USE_5005THREADS */
1950 Perl_newPROG(pTHX_ OP *o)
1955 PL_eval_root = newUNOP(OP_LEAVEEVAL,
1956 ((PL_in_eval & EVAL_KEEPERR)
1957 ? OPf_SPECIAL : 0), o);
1958 PL_eval_start = linklist(PL_eval_root);
1959 PL_eval_root->op_private |= OPpREFCOUNTED;
1960 OpREFCNT_set(PL_eval_root, 1);
1961 PL_eval_root->op_next = 0;
1962 CALL_PEEP(PL_eval_start);
1965 if (o->op_type == OP_STUB) {
1966 PL_comppad_name = 0;
1971 PL_main_root = scope(sawparens(scalarvoid(o)));
1972 PL_curcop = &PL_compiling;
1973 PL_main_start = LINKLIST(PL_main_root);
1974 PL_main_root->op_private |= OPpREFCOUNTED;
1975 OpREFCNT_set(PL_main_root, 1);
1976 PL_main_root->op_next = 0;
1977 CALL_PEEP(PL_main_start);
1980 /* Register with debugger */
1982 CV *cv = get_cv("DB::postponed", FALSE);
1986 XPUSHs((SV*)CopFILEGV(&PL_compiling));
1988 call_sv((SV*)cv, G_DISCARD);
1995 Perl_localize(pTHX_ OP *o, I32 lex)
1997 if (o->op_flags & OPf_PARENS)
1998 /* [perl #17376]: this appears to be premature, and results in code such as
1999 C< our(%x); > executing in list mode rather than void mode */
2006 if ( PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ','
2007 && ckWARN(WARN_PARENTHESIS))
2009 char *s = PL_bufptr;
2012 /* some heuristics to detect a potential error */
2013 while (*s && (strchr(", \t\n", *s)))
2017 if (*s && strchr("@$%*", *s) && *++s
2018 && (isALNUM(*s) || UTF8_IS_CONTINUED(*s))) {
2021 while (*s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s)))
2023 while (*s && (strchr(", \t\n", *s)))
2029 if (sigil && (*s == ';' || *s == '=')) {
2030 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
2031 "Parentheses missing around \"%s\" list",
2032 lex ? (PL_in_my == KEY_our ? "our" : "my")
2040 o = mod(o, OP_NULL); /* a bit kludgey */
2042 PL_in_my_stash = Nullhv;
2047 Perl_jmaybe(pTHX_ OP *o)
2049 if (o->op_type == OP_LIST) {
2051 #ifdef USE_5005THREADS
2052 o2 = newOP(OP_THREADSV, 0);
2053 o2->op_targ = find_threadsv(";");
2055 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
2056 #endif /* USE_5005THREADS */
2057 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2063 Perl_fold_constants(pTHX_ register OP *o)
2066 I32 type = o->op_type;
2069 if (PL_opargs[type] & OA_RETSCALAR)
2071 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2072 o->op_targ = pad_alloc(type, SVs_PADTMP);
2074 /* integerize op, unless it happens to be C<-foo>.
2075 * XXX should pp_i_negate() do magic string negation instead? */
2076 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2077 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2078 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2080 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2083 if (!(PL_opargs[type] & OA_FOLDCONST))
2088 /* XXX might want a ck_negate() for this */
2089 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2100 /* XXX what about the numeric ops? */
2101 if (PL_hints & HINT_LOCALE)
2106 goto nope; /* Don't try to run w/ errors */
2108 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2109 if ((curop->op_type != OP_CONST ||
2110 (curop->op_private & OPpCONST_BARE)) &&
2111 curop->op_type != OP_LIST &&
2112 curop->op_type != OP_SCALAR &&
2113 curop->op_type != OP_NULL &&
2114 curop->op_type != OP_PUSHMARK)
2120 curop = LINKLIST(o);
2124 sv = *(PL_stack_sp--);
2125 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
2126 pad_swipe(o->op_targ, FALSE);
2127 else if (SvTEMP(sv)) { /* grab mortal temp? */
2128 (void)SvREFCNT_inc(sv);
2132 if (type == OP_RV2GV)
2133 return newGVOP(OP_GV, 0, (GV*)sv);
2134 return newSVOP(OP_CONST, 0, sv);
2141 Perl_gen_constant_list(pTHX_ register OP *o)
2144 const I32 oldtmps_floor = PL_tmps_floor;
2148 return o; /* Don't attempt to run with errors */
2150 PL_op = curop = LINKLIST(o);
2157 PL_tmps_floor = oldtmps_floor;
2159 o->op_type = OP_RV2AV;
2160 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2161 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
2162 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
2163 o->op_seq = 0; /* needs to be revisited in peep() */
2164 curop = ((UNOP*)o)->op_first;
2165 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
2172 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2174 if (!o || o->op_type != OP_LIST)
2175 o = newLISTOP(OP_LIST, 0, o, Nullop);
2177 o->op_flags &= ~OPf_WANT;
2179 if (!(PL_opargs[type] & OA_MARK))
2180 op_null(cLISTOPo->op_first);
2182 o->op_type = (OPCODE)type;
2183 o->op_ppaddr = PL_ppaddr[type];
2184 o->op_flags |= flags;
2186 o = CHECKOP(type, o);
2187 if (o->op_type != (unsigned)type)
2190 return fold_constants(o);
2193 /* List constructors */
2196 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2204 if (first->op_type != (unsigned)type
2205 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
2207 return newLISTOP(type, 0, first, last);
2210 if (first->op_flags & OPf_KIDS)
2211 ((LISTOP*)first)->op_last->op_sibling = last;
2213 first->op_flags |= OPf_KIDS;
2214 ((LISTOP*)first)->op_first = last;
2216 ((LISTOP*)first)->op_last = last;
2221 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2229 if (first->op_type != (unsigned)type)
2230 return prepend_elem(type, (OP*)first, (OP*)last);
2232 if (last->op_type != (unsigned)type)
2233 return append_elem(type, (OP*)first, (OP*)last);
2235 first->op_last->op_sibling = last->op_first;
2236 first->op_last = last->op_last;
2237 first->op_flags |= (last->op_flags & OPf_KIDS);
2245 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2253 if (last->op_type == (unsigned)type) {
2254 if (type == OP_LIST) { /* already a PUSHMARK there */
2255 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2256 ((LISTOP*)last)->op_first->op_sibling = first;
2257 if (!(first->op_flags & OPf_PARENS))
2258 last->op_flags &= ~OPf_PARENS;
2261 if (!(last->op_flags & OPf_KIDS)) {
2262 ((LISTOP*)last)->op_last = first;
2263 last->op_flags |= OPf_KIDS;
2265 first->op_sibling = ((LISTOP*)last)->op_first;
2266 ((LISTOP*)last)->op_first = first;
2268 last->op_flags |= OPf_KIDS;
2272 return newLISTOP(type, 0, first, last);
2278 Perl_newNULLLIST(pTHX)
2280 return newOP(OP_STUB, 0);
2284 Perl_force_list(pTHX_ OP *o)
2286 if (!o || o->op_type != OP_LIST)
2287 o = newLISTOP(OP_LIST, 0, o, Nullop);
2293 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2297 NewOp(1101, listop, 1, LISTOP);
2299 listop->op_type = (OPCODE)type;
2300 listop->op_ppaddr = PL_ppaddr[type];
2303 listop->op_flags = (U8)flags;
2307 else if (!first && last)
2310 first->op_sibling = last;
2311 listop->op_first = first;
2312 listop->op_last = last;
2313 if (type == OP_LIST) {
2315 pushop = newOP(OP_PUSHMARK, 0);
2316 pushop->op_sibling = first;
2317 listop->op_first = pushop;
2318 listop->op_flags |= OPf_KIDS;
2320 listop->op_last = pushop;
2323 return CHECKOP(type, listop);
2327 Perl_newOP(pTHX_ I32 type, I32 flags)
2330 NewOp(1101, o, 1, OP);
2331 o->op_type = (OPCODE)type;
2332 o->op_ppaddr = PL_ppaddr[type];
2333 o->op_flags = (U8)flags;
2336 o->op_private = (U8)(0 | (flags >> 8));
2337 if (PL_opargs[type] & OA_RETSCALAR)
2339 if (PL_opargs[type] & OA_TARGET)
2340 o->op_targ = pad_alloc(type, SVs_PADTMP);
2341 return CHECKOP(type, o);
2345 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
2350 first = newOP(OP_STUB, 0);
2351 if (PL_opargs[type] & OA_MARK)
2352 first = force_list(first);
2354 NewOp(1101, unop, 1, UNOP);
2355 unop->op_type = (OPCODE)type;
2356 unop->op_ppaddr = PL_ppaddr[type];
2357 unop->op_first = first;
2358 unop->op_flags = flags | OPf_KIDS;
2359 unop->op_private = (U8)(1 | (flags >> 8));
2360 unop = (UNOP*) CHECKOP(type, unop);
2364 return fold_constants((OP *) unop);
2368 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2371 NewOp(1101, binop, 1, BINOP);
2374 first = newOP(OP_NULL, 0);
2376 binop->op_type = (OPCODE)type;
2377 binop->op_ppaddr = PL_ppaddr[type];
2378 binop->op_first = first;
2379 binop->op_flags = flags | OPf_KIDS;
2382 binop->op_private = (U8)(1 | (flags >> 8));
2385 binop->op_private = (U8)(2 | (flags >> 8));
2386 first->op_sibling = last;
2389 binop = (BINOP*)CHECKOP(type, binop);
2390 if (binop->op_next || binop->op_type != (OPCODE)type)
2393 binop->op_last = binop->op_first->op_sibling;
2395 return fold_constants((OP *)binop);
2398 static int uvcompare(const void *a, const void *b) __attribute__nonnull__(1) __attribute__nonnull__(2) __attribute__pure__;
2399 static int uvcompare(const void *a, const void *b)
2401 if (*((const UV *)a) < (*(const UV *)b))
2403 if (*((const UV *)a) > (*(const UV *)b))
2405 if (*((const UV *)a+1) < (*(const UV *)b+1))
2407 if (*((const UV *)a+1) > (*(const UV *)b+1))
2413 Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
2415 SV * const tstr = ((SVOP*)expr)->op_sv;
2416 SV * const rstr = ((SVOP*)repl)->op_sv;
2419 const U8 *t = (U8*)SvPV_const(tstr, tlen);
2420 const U8 *r = (U8*)SvPV_const(rstr, rlen);
2427 register short *tbl;
2429 PL_hints |= HINT_BLOCK_SCOPE;
2430 complement = o->op_private & OPpTRANS_COMPLEMENT;
2431 del = o->op_private & OPpTRANS_DELETE;
2432 squash = o->op_private & OPpTRANS_SQUASH;
2435 o->op_private |= OPpTRANS_FROM_UTF;
2438 o->op_private |= OPpTRANS_TO_UTF;
2440 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2441 SV* listsv = newSVpvn("# comment\n",10);
2443 const U8* tend = t + tlen;
2444 const U8* rend = r + rlen;
2458 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2459 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2465 t = tsave = bytes_to_utf8((U8 *)t, &len);
2468 if (!to_utf && rlen) {
2470 r = rsave = bytes_to_utf8((U8 *)r, &len);
2474 /* There are several snags with this code on EBCDIC:
2475 1. 0xFF is a legal UTF-EBCDIC byte (there are no illegal bytes).
2476 2. scan_const() in toke.c has encoded chars in native encoding which makes
2477 ranges at least in EBCDIC 0..255 range the bottom odd.
2481 U8 tmpbuf[UTF8_MAXBYTES+1];
2484 Newx(cp, 2*tlen, UV);
2486 transv = newSVpvn("",0);
2488 cp[2*i] = utf8n_to_uvuni((U8 *)t, tend-t, &ulen, 0);
2490 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) {
2492 cp[2*i+1] = utf8n_to_uvuni((U8 *)t, tend-t, &ulen, 0);
2496 cp[2*i+1] = cp[2*i];
2500 qsort(cp, i, 2*sizeof(UV), uvcompare);
2501 for (j = 0; j < i; j++) {
2503 diff = val - nextmin;
2505 t = uvuni_to_utf8(tmpbuf,nextmin);
2506 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2508 U8 range_mark = UTF_TO_NATIVE(0xff);
2509 t = uvuni_to_utf8(tmpbuf, val - 1);
2510 sv_catpvn(transv, (char *)&range_mark, 1);
2511 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2518 t = uvuni_to_utf8(tmpbuf,nextmin);
2519 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2521 U8 range_mark = UTF_TO_NATIVE(0xff);
2522 sv_catpvn(transv, (char *)&range_mark, 1);
2524 t = uvuni_to_utf8_flags(tmpbuf, 0x7fffffff,
2525 UNICODE_ALLOW_SUPER);
2526 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2527 t = (const U8*)SvPVX_const(transv);
2528 tlen = SvCUR(transv);
2532 else if (!rlen && !del) {
2533 r = t; rlen = tlen; rend = tend;
2536 if ((!rlen && !del) || t == r ||
2537 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
2539 o->op_private |= OPpTRANS_IDENTICAL;
2543 while (t < tend || tfirst <= tlast) {
2544 /* see if we need more "t" chars */
2545 if (tfirst > tlast) {
2546 tfirst = (I32)utf8n_to_uvuni((U8 *)t, tend - t, &ulen, 0);
2548 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) { /* illegal utf8 val indicates range */
2550 tlast = (I32)utf8n_to_uvuni((U8 *)t, tend - t, &ulen, 0);
2557 /* now see if we need more "r" chars */
2558 if (rfirst > rlast) {
2560 rfirst = (I32)utf8n_to_uvuni((U8 *)r, rend - r, &ulen, 0);
2562 if (r < rend && NATIVE_TO_UTF(*r) == 0xff) { /* illegal utf8 val indicates range */
2564 rlast = (I32)utf8n_to_uvuni((U8 *)r, rend - r, &ulen,
2574 rfirst = rlast = 0xffffffff;
2578 /* now see which range will peter our first, if either. */
2579 tdiff = tlast - tfirst;
2580 rdiff = rlast - rfirst;
2587 if (rfirst == 0xffffffff) {
2588 diff = tdiff; /* oops, pretend rdiff is infinite */
2590 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
2591 (long)tfirst, (long)tlast);
2593 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
2597 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
2598 (long)tfirst, (long)(tfirst + diff),
2601 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
2602 (long)tfirst, (long)rfirst);
2604 if (rfirst + diff > max)
2605 max = rfirst + diff;
2607 grows = (tfirst < rfirst &&
2608 UNISKIP(tfirst) < UNISKIP(rfirst + diff));
2620 else if (max > 0xff)
2625 Safefree(cPVOPo->op_pv);
2626 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2627 SvREFCNT_dec(listsv);
2629 SvREFCNT_dec(transv);
2631 if (!del && havefinal && rlen)
2632 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5,
2633 newSVuv((UV)final), 0);
2636 o->op_private |= OPpTRANS_GROWS;
2648 tbl = (short*)cPVOPo->op_pv;
2650 Zero(tbl, 256, short);
2651 for (i = 0; i < (I32)tlen; i++)
2653 for (i = 0, j = 0; i < 256; i++) {
2655 if (j >= (I32)rlen) {
2664 if (i < 128 && r[j] >= 128)
2674 o->op_private |= OPpTRANS_IDENTICAL;
2676 else if (j >= (I32)rlen)
2679 cPVOPo->op_pv = (char*)Renew(tbl, 0x101+rlen-j, short);
2680 tbl[0x100] = rlen - j;
2681 for (i=0; i < (I32)rlen - j; i++)
2682 tbl[0x101+i] = r[j+i];
2686 if (!rlen && !del) {
2689 o->op_private |= OPpTRANS_IDENTICAL;
2691 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
2692 o->op_private |= OPpTRANS_IDENTICAL;
2694 for (i = 0; i < 256; i++)
2696 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
2697 if (j >= (I32)rlen) {
2699 if (tbl[t[i]] == -1)
2705 if (tbl[t[i]] == -1) {
2706 if (t[i] < 128 && r[j] >= 128)
2713 o->op_private |= OPpTRANS_GROWS;
2721 Perl_newPMOP(pTHX_ I32 type, I32 flags)
2725 NewOp(1101, pmop, 1, PMOP);
2726 pmop->op_type = (OPCODE)type;
2727 pmop->op_ppaddr = PL_ppaddr[type];
2728 pmop->op_flags = (U8)flags;
2729 pmop->op_private = (U8)(0 | (flags >> 8));
2731 if (PL_hints & HINT_RE_TAINT)
2732 pmop->op_pmpermflags |= PMf_RETAINT;
2733 if (PL_hints & HINT_LOCALE)
2734 pmop->op_pmpermflags |= PMf_LOCALE;
2735 pmop->op_pmflags = pmop->op_pmpermflags;
2740 if(av_len((AV*) PL_regex_pad[0]) > -1) {
2741 repointer = av_pop((AV*)PL_regex_pad[0]);
2742 pmop->op_pmoffset = SvIV(repointer);
2743 SvREPADTMP_off(repointer);
2744 sv_setiv(repointer,0);
2746 repointer = newSViv(0);
2747 av_push(PL_regex_padav,SvREFCNT_inc(repointer));
2748 pmop->op_pmoffset = av_len(PL_regex_padav);
2749 PL_regex_pad = AvARRAY(PL_regex_padav);
2754 /* link into pm list */
2755 if (type != OP_TRANS && PL_curstash) {
2756 pmop->op_pmnext = HvPMROOT(PL_curstash);
2757 HvPMROOT(PL_curstash) = pmop;
2758 PmopSTASH_set(pmop,PL_curstash);
2761 return CHECKOP(type, pmop);
2765 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
2769 I32 repl_has_vars = 0;
2771 if (o->op_type == OP_TRANS)
2772 return pmtrans(o, expr, repl);
2774 PL_hints |= HINT_BLOCK_SCOPE;
2777 if (expr->op_type == OP_CONST) {
2779 SV *pat = ((SVOP*)expr)->op_sv;
2780 const char *p = SvPV_const(pat, plen);
2781 if ((o->op_flags & OPf_SPECIAL) && (*p == ' ' && p[1] == '\0')) {
2782 U32 was_readonly = SvREADONLY(pat);
2786 sv_force_normal_flags(pat, 0);
2787 assert(!SvREADONLY(pat));
2790 SvREADONLY_off(pat);
2794 sv_setpvn(pat, "\\s+", 3);
2796 SvFLAGS(pat) |= was_readonly;
2798 p = SvPV_const(pat, plen);
2799 pm->op_pmflags |= PMf_SKIPWHITE;
2802 pm->op_pmdynflags |= PMdf_UTF8;
2803 /* FIXME - can we make this function take const char * args? */
2804 PM_SETRE(pm, CALLREGCOMP(aTHX_ (char*)p, (char*)p + plen, pm));
2805 if (strEQ("\\s+", PM_GETRE(pm)->precomp))
2806 pm->op_pmflags |= PMf_WHITE;
2810 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2811 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2813 : OP_REGCMAYBE),0,expr);
2815 NewOp(1101, rcop, 1, LOGOP);
2816 rcop->op_type = OP_REGCOMP;
2817 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2818 rcop->op_first = scalar(expr);
2819 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2820 ? (OPf_SPECIAL | OPf_KIDS)
2822 rcop->op_private = 1;
2825 /* establish postfix order */
2826 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2828 rcop->op_next = expr;
2829 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2832 rcop->op_next = LINKLIST(expr);
2833 expr->op_next = (OP*)rcop;
2836 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2841 if (pm->op_pmflags & PMf_EVAL) {
2843 if (CopLINE(PL_curcop) < (line_t)PL_multi_end)
2844 CopLINE_set(PL_curcop, (line_t)PL_multi_end);
2846 #ifdef USE_5005THREADS
2847 else if (repl->op_type == OP_THREADSV
2848 && strchr("&`'123456789+",
2849 PL_threadsv_names[repl->op_targ]))
2853 #endif /* USE_5005THREADS */
2854 else if (repl->op_type == OP_CONST)
2858 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2859 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2860 #ifdef USE_5005THREADS
2861 if (curop->op_type == OP_THREADSV) {
2863 if (strchr("&`'123456789+", curop->op_private))
2867 if (curop->op_type == OP_GV) {
2868 GV *gv = cGVOPx_gv(curop);
2870 if (strchr("&`'123456789+-\016\022", *GvENAME(gv)))
2873 #endif /* USE_5005THREADS */
2874 else if (curop->op_type == OP_RV2CV)
2876 else if (curop->op_type == OP_RV2SV ||
2877 curop->op_type == OP_RV2AV ||
2878 curop->op_type == OP_RV2HV ||
2879 curop->op_type == OP_RV2GV) {
2880 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2883 else if (curop->op_type == OP_PADSV ||
2884 curop->op_type == OP_PADAV ||
2885 curop->op_type == OP_PADHV ||
2886 curop->op_type == OP_PADANY) {
2889 else if (curop->op_type == OP_PUSHRE)
2890 ; /* Okay here, dangerous in newASSIGNOP */
2900 || PM_GETRE(pm)->reganch & ROPT_EVAL_SEEN))) {
2901 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2902 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2903 prepend_elem(o->op_type, scalar(repl), o);
2906 if (curop == repl && !PM_GETRE(pm)) { /* Has variables. */
2907 pm->op_pmflags |= PMf_MAYBE_CONST;
2908 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2910 NewOp(1101, rcop, 1, LOGOP);
2911 rcop->op_type = OP_SUBSTCONT;
2912 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
2913 rcop->op_first = scalar(repl);
2914 rcop->op_flags |= OPf_KIDS;
2915 rcop->op_private = 1;
2918 /* establish postfix order */
2919 rcop->op_next = LINKLIST(repl);
2920 repl->op_next = (OP*)rcop;
2922 pm->op_pmreplroot = scalar((OP*)rcop);
2923 pm->op_pmreplstart = LINKLIST(rcop);
2932 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
2935 NewOp(1101, svop, 1, SVOP);
2936 svop->op_type = (OPCODE)type;
2937 svop->op_ppaddr = PL_ppaddr[type];
2939 svop->op_next = (OP*)svop;
2940 svop->op_flags = (U8)flags;
2941 if (PL_opargs[type] & OA_RETSCALAR)
2943 if (PL_opargs[type] & OA_TARGET)
2944 svop->op_targ = pad_alloc(type, SVs_PADTMP);
2945 return CHECKOP(type, svop);
2949 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
2952 NewOp(1101, padop, 1, PADOP);
2953 padop->op_type = (OPCODE)type;
2954 padop->op_ppaddr = PL_ppaddr[type];
2955 padop->op_padix = pad_alloc(type, SVs_PADTMP);
2956 SvREFCNT_dec(PAD_SVl(padop->op_padix));
2957 PAD_SETSV(padop->op_padix, sv);
2960 padop->op_next = (OP*)padop;
2961 padop->op_flags = (U8)flags;
2962 if (PL_opargs[type] & OA_RETSCALAR)
2964 if (PL_opargs[type] & OA_TARGET)
2965 padop->op_targ = pad_alloc(type, SVs_PADTMP);
2966 return CHECKOP(type, padop);
2970 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
2975 return newPADOP(type, flags, SvREFCNT_inc(gv));
2977 return newSVOP(type, flags, SvREFCNT_inc(gv));
2982 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
2985 NewOp(1101, pvop, 1, PVOP);
2986 pvop->op_type = (OPCODE)type;
2987 pvop->op_ppaddr = PL_ppaddr[type];
2989 pvop->op_next = (OP*)pvop;
2990 pvop->op_flags = (U8)flags;
2991 if (PL_opargs[type] & OA_RETSCALAR)
2993 if (PL_opargs[type] & OA_TARGET)
2994 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2995 return CHECKOP(type, pvop);
2999 Perl_package(pTHX_ OP *o)
3003 save_hptr(&PL_curstash);
3004 save_item(PL_curstname);
3009 name = SvPV_const(sv, len);
3010 PL_curstash = gv_stashpvn(name,len,TRUE);
3011 sv_setpvn(PL_curstname, name, len);
3015 deprecate("\"package\" with no arguments");
3016 sv_setpv(PL_curstname,"<none>");
3017 PL_curstash = Nullhv;
3019 PL_hints |= HINT_BLOCK_SCOPE;
3020 PL_copline = NOLINE;
3025 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
3031 if (idop->op_type != OP_CONST)
3032 Perl_croak(aTHX_ "Module name must be constant");
3037 SV *vesv = ((SVOP*)version)->op_sv;
3039 if (!arg && !SvNIOKp(vesv)) {
3046 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
3047 Perl_croak(aTHX_ "Version number must be constant number");
3049 /* Make copy of idop so we don't free it twice */
3050 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
3052 /* Fake up a method call to VERSION */
3053 meth = newSVpvn("VERSION",7);
3054 sv_upgrade(meth, SVt_PVIV);
3055 (void)SvIOK_on(meth);
3058 PERL_HASH(hash, SvPVX_const(meth), SvCUR(meth));
3059 SvUV_set(meth, hash);
3061 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3062 append_elem(OP_LIST,
3063 prepend_elem(OP_LIST, pack, list(version)),
3064 newSVOP(OP_METHOD_NAMED, 0, meth)));
3068 /* Fake up an import/unimport */
3069 if (arg && arg->op_type == OP_STUB)
3070 imop = arg; /* no import on explicit () */
3071 else if (SvNIOKp(((SVOP*)idop)->op_sv)) {
3072 imop = Nullop; /* use 5.0; */
3077 /* Make copy of idop so we don't free it twice */
3078 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
3080 /* Fake up a method call to import/unimport */
3081 meth = aver ? newSVpvn("import",6) : newSVpvn("unimport", 8);
3082 (void)SvUPGRADE(meth, SVt_PVIV);
3083 (void)SvIOK_on(meth);
3086 PERL_HASH(hash, SvPVX_const(meth), SvCUR(meth));
3087 SvUV_set(meth, hash);
3089 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3090 append_elem(OP_LIST,
3091 prepend_elem(OP_LIST, pack, list(arg)),
3092 newSVOP(OP_METHOD_NAMED, 0, meth)));
3095 /* Fake up the BEGIN {}, which does its thing immediately. */
3097 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
3100 append_elem(OP_LINESEQ,
3101 append_elem(OP_LINESEQ,
3102 newSTATEOP(0, Nullch, newUNOP(OP_REQUIRE, 0, idop)),
3103 newSTATEOP(0, Nullch, veop)),
3104 newSTATEOP(0, Nullch, imop) ));
3106 /* The "did you use incorrect case?" warning used to be here.
3107 * The problem is that on case-insensitive filesystems one
3108 * might get false positives for "use" (and "require"):
3109 * "use Strict" or "require CARP" will work. This causes
3110 * portability problems for the script: in case-strict
3111 * filesystems the script will stop working.
3113 * The "incorrect case" warning checked whether "use Foo"
3114 * imported "Foo" to your namespace, but that is wrong, too:
3115 * there is no requirement nor promise in the language that
3116 * a Foo.pm should or would contain anything in package "Foo".
3118 * There is very little Configure-wise that can be done, either:
3119 * the case-sensitivity of the build filesystem of Perl does not
3120 * help in guessing the case-sensitivity of the runtime environment.
3123 PL_hints |= HINT_BLOCK_SCOPE;
3124 PL_copline = NOLINE;
3126 PL_cop_seqmax++; /* Purely for B::*'s benefit */
3130 =head1 Embedding Functions
3132 =for apidoc load_module
3134 Loads the module whose name is pointed to by the string part of name.
3135 Note that the actual module name, not its filename, should be given.
3136 Eg, "Foo::Bar" instead of "Foo/Bar.pm". flags can be any of
3137 PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS
3138 (or 0 for no flags). ver, if specified, provides version semantics
3139 similar to C<use Foo::Bar VERSION>. The optional trailing SV*
3140 arguments can be used to specify arguments to the module's import()
3141 method, similar to C<use Foo::Bar VERSION LIST>.
3146 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
3149 va_start(args, ver);
3150 vload_module(flags, name, ver, &args);
3154 #ifdef PERL_IMPLICIT_CONTEXT
3156 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
3160 va_start(args, ver);
3161 vload_module(flags, name, ver, &args);
3167 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
3169 OP *modname, *veop, *imop;
3171 modname = newSVOP(OP_CONST, 0, name);
3172 modname->op_private |= OPpCONST_BARE;
3174 veop = newSVOP(OP_CONST, 0, ver);
3178 if (flags & PERL_LOADMOD_NOIMPORT) {
3179 imop = sawparens(newNULLLIST());
3181 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
3182 imop = va_arg(*args, OP*);
3187 sv = va_arg(*args, SV*);
3189 imop = append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
3190 sv = va_arg(*args, SV*);
3194 const line_t ocopline = PL_copline;
3195 COP * const ocurcop = PL_curcop;
3196 const int oexpect = PL_expect;
3198 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
3199 veop, modname, imop);
3200 PL_expect = oexpect;
3201 PL_copline = ocopline;
3202 PL_curcop = ocurcop;
3207 Perl_dofile(pTHX_ OP *term)
3212 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
3213 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv)))
3214 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
3216 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
3217 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3218 append_elem(OP_LIST, term,
3219 scalar(newUNOP(OP_RV2CV, 0,
3224 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3230 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
3232 return newBINOP(OP_LSLICE, flags,
3233 list(force_list(subscript)),
3234 list(force_list(listval)) );
3238 S_is_list_assignment(pTHX_ register const OP *o)
3243 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3244 o = cUNOPo->op_first;
3246 if (o->op_type == OP_COND_EXPR) {
3247 const I32 t = is_list_assignment(cLOGOPo->op_first->op_sibling);
3248 const I32 f = is_list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
3253 yyerror("Assignment to both a list and a scalar");
3257 if (o->op_type == OP_LIST &&
3258 (o->op_flags & OPf_WANT) == OPf_WANT_SCALAR &&
3259 o->op_private & OPpLVAL_INTRO)
3262 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3263 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3264 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
3267 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
3270 if (o->op_type == OP_RV2SV)
3277 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
3282 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
3283 return newLOGOP(optype, 0,
3284 mod(scalar(left), optype),
3285 newUNOP(OP_SASSIGN, 0, scalar(right)));
3288 return newBINOP(optype, OPf_STACKED,
3289 mod(scalar(left), optype), scalar(right));
3293 if (is_list_assignment(left)) {
3297 /* Grandfathering $[ assignment here. Bletch.*/
3298 /* Only simple assignments like C<< ($[) = 1 >> are allowed */
3299 PL_eval_start = (left->op_type == OP_CONST) ? right : 0;
3300 left = mod(left, OP_AASSIGN);
3303 else if (left->op_type == OP_CONST) {
3304 /* Result of assignment is always 1 (or we'd be dead already) */
3305 return newSVOP(OP_CONST, 0, newSViv(1));
3307 /* optimise C<my @x = ()> to C<my @x>, and likewise for hashes */
3308 if ((left->op_type == OP_PADAV || left->op_type == OP_PADHV)
3309 && right->op_type == OP_STUB
3310 && (left->op_private & OPpLVAL_INTRO))
3313 left->op_flags &= ~(OPf_REF|OPf_SPECIAL);
3316 curop = list(force_list(left));
3317 o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
3318 o->op_private = (U8)(0 | (flags >> 8));
3319 for (curop = ((LISTOP*)curop)->op_first;
3320 curop; curop = curop->op_sibling)
3322 if (curop->op_type == OP_RV2HV &&
3323 ((UNOP*)curop)->op_first->op_type != OP_GV) {
3324 o->op_private |= OPpASSIGN_HASH;
3329 /* PL_generation sorcery:
3330 * an assignment like ($a,$b) = ($c,$d) is easier than
3331 * ($a,$b) = ($c,$a), since there is no need for temporary vars.
3332 * To detect whether there are common vars, the global var
3333 * PL_generation is incremented for each assign op we compile.
3334 * Then, while compiling the assign op, we run through all the
3335 * variables on both sides of the assignment, setting a spare slot
3336 * in each of them to PL_generation. If any of them already have
3337 * that value, we know we've got commonality. We could use a
3338 * single bit marker, but then we'd have to make 2 passes, first
3339 * to clear the flag, then to test and set it. To find somewhere
3340 * to store these values, evil chicanery is done with SvCUR().
3343 if (!(left->op_private & OPpLVAL_INTRO)) {
3346 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3347 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3348 if (curop->op_type == OP_GV) {
3349 GV *gv = cGVOPx_gv(curop);
3350 if (gv == PL_defgv || (int)SvCUR(gv) == PL_generation)
3352 SvCUR_set(gv, PL_generation);
3354 else if (curop->op_type == OP_PADSV ||
3355 curop->op_type == OP_PADAV ||
3356 curop->op_type == OP_PADHV ||
3357 curop->op_type == OP_PADANY)
3359 if ((int)PAD_COMPNAME_GEN(curop->op_targ)
3362 PAD_COMPNAME_GEN_set(curop->op_targ, PL_generation);
3365 else if (curop->op_type == OP_RV2CV)
3367 else if (curop->op_type == OP_RV2SV ||
3368 curop->op_type == OP_RV2AV ||
3369 curop->op_type == OP_RV2HV ||
3370 curop->op_type == OP_RV2GV) {
3371 if (lastop->op_type != OP_GV) /* funny deref? */
3374 else if (curop->op_type == OP_PUSHRE) {
3375 if (((PMOP*)curop)->op_pmreplroot) {
3377 GV *gv = (GV*)PAD_SVl(INT2PTR(PADOFFSET,
3378 ((PMOP*)curop)->op_pmreplroot));
3380 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3382 if (gv == PL_defgv || (int)SvCUR(gv) == PL_generation)
3384 SvCUR_set(gv, PL_generation);
3393 o->op_private |= OPpASSIGN_COMMON;
3395 if (right && right->op_type == OP_SPLIT) {
3397 if ((tmpop = ((LISTOP*)right)->op_first) &&
3398 tmpop->op_type == OP_PUSHRE)
3400 PMOP *pm = (PMOP*)tmpop;
3401 if (left->op_type == OP_RV2AV &&
3402 !(left->op_private & OPpLVAL_INTRO) &&
3403 !(o->op_private & OPpASSIGN_COMMON) )
3405 tmpop = ((UNOP*)left)->op_first;
3406 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
3408 pm->op_pmreplroot = INT2PTR(OP*, cPADOPx(tmpop)->op_padix);
3409 cPADOPx(tmpop)->op_padix = 0; /* steal it */
3411 pm->op_pmreplroot = (OP*)cSVOPx(tmpop)->op_sv;
3412 cSVOPx(tmpop)->op_sv = Nullsv; /* steal it */
3414 pm->op_pmflags |= PMf_ONCE;
3415 tmpop = cUNOPo->op_first; /* to list (nulled) */
3416 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3417 tmpop->op_sibling = Nullop; /* don't free split */
3418 right->op_next = tmpop->op_next; /* fix starting loc */
3419 op_free(o); /* blow off assign */
3420 right->op_flags &= ~OPf_WANT;
3421 /* "I don't know and I don't care." */
3426 if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
3427 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3429 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3431 sv_setiv(sv, PL_modcount+1);
3439 right = newOP(OP_UNDEF, 0);
3440 if (right->op_type == OP_READLINE) {
3441 right->op_flags |= OPf_STACKED;
3442 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3445 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3446 o = newBINOP(OP_SASSIGN, flags,
3447 scalar(right), mod(scalar(left), OP_SASSIGN) );
3451 o = newSVOP(OP_CONST, 0, newSViv(PL_compiling.cop_arybase));
3458 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3460 const U32 seq = intro_my();
3463 NewOp(1101, cop, 1, COP);
3464 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
3465 cop->op_type = OP_DBSTATE;
3466 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3469 cop->op_type = OP_NEXTSTATE;
3470 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3472 cop->op_flags = (U8)flags;
3473 cop->op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
3475 cop->op_private |= NATIVE_HINTS;
3477 PL_compiling.op_private = cop->op_private;
3478 cop->op_next = (OP*)cop;
3481 cop->cop_label = label;
3482 PL_hints |= HINT_BLOCK_SCOPE;
3485 cop->cop_arybase = PL_curcop->cop_arybase;
3486 if (specialWARN(PL_curcop->cop_warnings))
3487 cop->cop_warnings = PL_curcop->cop_warnings ;
3489 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3490 if (specialCopIO(PL_curcop->cop_io))
3491 cop->cop_io = PL_curcop->cop_io;
3493 cop->cop_io = newSVsv(PL_curcop->cop_io) ;
3496 if (PL_copline == NOLINE)
3497 CopLINE_set(cop, CopLINE(PL_curcop));
3499 CopLINE_set(cop, PL_copline);
3500 PL_copline = NOLINE;
3503 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
3505 CopFILEGV_set(cop, CopFILEGV(PL_curcop));
3507 CopSTASH_set(cop, PL_curstash);
3509 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3510 SV ** const svp = av_fetch(CopFILEAV(PL_curcop), (I32)CopLINE(cop), FALSE);
3511 if (svp && *svp != &PL_sv_undef ) {
3512 (void)SvIOK_on(*svp);
3513 SvIV_set(*svp, PTR2IV(cop));
3517 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3522 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
3524 return new_logop(type, flags, &first, &other);
3528 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
3532 OP *first = *firstp;
3533 OP * const other = *otherp;
3535 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3536 return newBINOP(type, flags, scalar(first), scalar(other));
3538 scalarboolean(first);
3539 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3540 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3541 if (type == OP_AND || type == OP_OR) {
3547 first = *firstp = cUNOPo->op_first;
3549 first->op_next = o->op_next;
3550 cUNOPo->op_first = Nullop;
3554 if (first->op_type == OP_CONST) {
3555 if (first->op_private & OPpCONST_STRICT)
3556 no_bareword_allowed(first);
3557 else if ((first->op_private & OPpCONST_BARE) && ckWARN(WARN_BAREWORD))
3558 Perl_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional");
3559 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3562 if (other->op_type == OP_CONST)
3563 other->op_private |= OPpCONST_SHORTCIRCUIT;
3569 if (first->op_type == OP_CONST)
3570 first->op_private |= OPpCONST_SHORTCIRCUIT;
3574 else if ((first->op_flags & OPf_KIDS) && ckWARN(WARN_MISC)) {
3575 const OP * const k1 = ((UNOP*)first)->op_first;
3576 const OP * const k2 = k1->op_sibling;
3578 switch (first->op_type)
3581 if (k2 && k2->op_type == OP_READLINE
3582 && (k2->op_flags & OPf_STACKED)
3583 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3585 warnop = k2->op_type;
3590 if (k1->op_type == OP_READDIR
3591 || k1->op_type == OP_GLOB
3592 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3593 || k1->op_type == OP_EACH)
3595 warnop = ((k1->op_type == OP_NULL)
3596 ? (OPCODE)k1->op_targ : k1->op_type);
3601 const line_t oldline = CopLINE(PL_curcop);
3602 CopLINE_set(PL_curcop, PL_copline);
3603 Perl_warner(aTHX_ packWARN(WARN_MISC),
3604 "Value of %s%s can be \"0\"; test with defined()",
3606 ((warnop == OP_READLINE || warnop == OP_GLOB)
3607 ? " construct" : "() operator"));
3608 CopLINE_set(PL_curcop, oldline);
3615 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3616 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3618 NewOp(1101, logop, 1, LOGOP);
3620 logop->op_type = (OPCODE)type;
3621 logop->op_ppaddr = PL_ppaddr[type];
3622 logop->op_first = first;
3623 logop->op_flags = flags | OPf_KIDS;
3624 logop->op_other = LINKLIST(other);
3625 logop->op_private = (U8)(1 | (flags >> 8));
3627 /* establish postfix order */
3628 logop->op_next = LINKLIST(first);
3629 first->op_next = (OP*)logop;
3630 first->op_sibling = other;
3632 CHECKOP(type,logop);
3634 o = newUNOP(OP_NULL, 0, (OP*)logop);
3641 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
3648 return newLOGOP(OP_AND, 0, first, trueop);
3650 return newLOGOP(OP_OR, 0, first, falseop);
3652 scalarboolean(first);
3653 if (first->op_type == OP_CONST) {
3654 if (first->op_private & OPpCONST_BARE &&
3655 first->op_private & OPpCONST_STRICT) {
3656 no_bareword_allowed(first);
3658 if (SvTRUE(((SVOP*)first)->op_sv)) {
3669 NewOp(1101, logop, 1, LOGOP);
3670 logop->op_type = OP_COND_EXPR;
3671 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3672 logop->op_first = first;
3673 logop->op_flags = flags | OPf_KIDS;
3674 logop->op_private = (U8)(1 | (flags >> 8));
3675 logop->op_other = LINKLIST(trueop);
3676 logop->op_next = LINKLIST(falseop);
3678 CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
3681 /* establish postfix order */
3682 start = LINKLIST(first);
3683 first->op_next = (OP*)logop;
3685 first->op_sibling = trueop;
3686 trueop->op_sibling = falseop;
3687 o = newUNOP(OP_NULL, 0, (OP*)logop);
3689 trueop->op_next = falseop->op_next = o;
3696 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
3704 NewOp(1101, range, 1, LOGOP);
3706 range->op_type = OP_RANGE;
3707 range->op_ppaddr = PL_ppaddr[OP_RANGE];
3708 range->op_first = left;
3709 range->op_flags = OPf_KIDS;
3710 leftstart = LINKLIST(left);
3711 range->op_other = LINKLIST(right);
3712 range->op_private = (U8)(1 | (flags >> 8));
3714 left->op_sibling = right;
3716 range->op_next = (OP*)range;
3717 flip = newUNOP(OP_FLIP, flags, (OP*)range);
3718 flop = newUNOP(OP_FLOP, 0, flip);
3719 o = newUNOP(OP_NULL, 0, flop);
3721 range->op_next = leftstart;
3723 left->op_next = flip;
3724 right->op_next = flop;
3726 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3727 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
3728 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3729 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3731 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3732 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3735 if (!flip->op_private || !flop->op_private)
3736 linklist(o); /* blow off optimizer unless constant */
3742 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
3746 const bool once = block && block->op_flags & OPf_SPECIAL &&
3747 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3749 PERL_UNUSED_ARG(debuggable);
3752 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3753 return block; /* do {} while 0 does once */
3754 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3755 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3756 expr = newUNOP(OP_DEFINED, 0,
3757 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3758 } else if (expr->op_flags & OPf_KIDS) {
3759 const OP * const k1 = ((UNOP*)expr)->op_first;
3760 const OP * const k2 = k1 ? k1->op_sibling : NULL;
3761 switch (expr->op_type) {
3763 if (k2 && k2->op_type == OP_READLINE
3764 && (k2->op_flags & OPf_STACKED)
3765 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3766 expr = newUNOP(OP_DEFINED, 0, expr);
3770 if (k1->op_type == OP_READDIR
3771 || k1->op_type == OP_GLOB
3772 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3773 || k1->op_type == OP_EACH)
3774 expr = newUNOP(OP_DEFINED, 0, expr);
3780 /* if block is null, the next append_elem() would put UNSTACK, a scalar
3781 * op, in listop. This is wrong. [perl #27024] */
3783 block = newOP(OP_NULL, 0);
3784 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3785 o = new_logop(OP_AND, 0, &expr, &listop);
3788 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3790 if (once && o != listop)
3791 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3794 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3796 o->op_flags |= flags;
3798 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3803 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
3811 PERL_UNUSED_ARG(debuggable);
3814 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3815 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3816 expr = newUNOP(OP_DEFINED, 0,
3817 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3818 } else if (expr->op_flags & OPf_KIDS) {
3819 const OP * const k1 = ((UNOP*)expr)->op_first;
3820 const OP * const k2 = (k1) ? k1->op_sibling : NULL;
3821 switch (expr->op_type) {
3823 if (k2 && k2->op_type == OP_READLINE
3824 && (k2->op_flags & OPf_STACKED)
3825 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3826 expr = newUNOP(OP_DEFINED, 0, expr);
3830 if (k1->op_type == OP_READDIR
3831 || k1->op_type == OP_GLOB
3832 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3833 || k1->op_type == OP_EACH)
3834 expr = newUNOP(OP_DEFINED, 0, expr);
3841 block = newOP(OP_NULL, 0);
3843 block = scope(block);
3847 next = LINKLIST(cont);
3850 OP *unstack = newOP(OP_UNSTACK, 0);
3853 cont = append_elem(OP_LINESEQ, cont, unstack);
3856 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3857 redo = LINKLIST(listop);
3860 PL_copline = (line_t)whileline;
3862 o = new_logop(OP_AND, 0, &expr, &listop);
3863 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3864 op_free(expr); /* oops, it's a while (0) */
3866 return Nullop; /* listop already freed by new_logop */
3869 ((LISTOP*)listop)->op_last->op_next =
3870 (o == listop ? redo : LINKLIST(o));
3876 NewOp(1101,loop,1,LOOP);
3877 loop->op_type = OP_ENTERLOOP;
3878 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3879 loop->op_private = 0;
3880 loop->op_next = (OP*)loop;
3883 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3885 loop->op_redoop = redo;
3886 loop->op_lastop = o;
3887 o->op_private |= loopflags;
3890 loop->op_nextop = next;
3892 loop->op_nextop = o;
3894 o->op_flags |= flags;
3895 o->op_private |= (flags >> 8);
3900 Perl_newFOROP(pTHX_ I32 flags, char *label, line_t forline, OP *sv, OP *expr, OP *block, OP *cont)
3904 PADOFFSET padoff = 0;
3909 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3910 iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */
3911 sv->op_type = OP_RV2GV;
3912 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
3914 else if (sv->op_type == OP_PADSV) { /* private variable */
3915 iterpflags = sv->op_private & OPpLVAL_INTRO; /* for my $x () */
3916 padoff = sv->op_targ;
3921 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3922 padoff = sv->op_targ;
3924 iterflags |= OPf_SPECIAL;
3929 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
3932 #ifdef USE_5005THREADS
3933 padoff = find_threadsv("_");
3934 iterflags |= OPf_SPECIAL;
3936 sv = newGVOP(OP_GV, 0, PL_defgv);
3939 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3940 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3941 iterflags |= OPf_STACKED;
3943 else if (expr->op_type == OP_NULL &&
3944 (expr->op_flags & OPf_KIDS) &&
3945 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3947 /* Basically turn for($x..$y) into the same as for($x,$y), but we
3948 * set the STACKED flag to indicate that these values are to be
3949 * treated as min/max values by 'pp_iterinit'.
3951 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3952 LOGOP* range = (LOGOP*) flip->op_first;
3953 OP* const left = range->op_first;
3954 OP* const right = left->op_sibling;
3957 range->op_flags &= ~OPf_KIDS;
3958 range->op_first = Nullop;
3960 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3961 listop->op_first->op_next = range->op_next;
3962 left->op_next = range->op_other;
3963 right->op_next = (OP*)listop;
3964 listop->op_next = listop->op_first;
3967 expr = (OP*)(listop);
3969 iterflags |= OPf_STACKED;
3972 expr = mod(force_list(expr), OP_GREPSTART);
3975 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
3976 append_elem(OP_LIST, expr, scalar(sv))));
3977 assert(!loop->op_next);
3978 /* for my $x () sets OPpLVAL_INTRO;
3979 * for our $x () sets OPpOUR_INTRO; both only used by Deparse.pm */
3980 loop->op_private = (U8)iterpflags;
3981 #ifdef PL_OP_SLAB_ALLOC
3984 NewOp(1234,tmp,1,LOOP);
3985 Copy(loop,tmp,1,LISTOP);
3990 Renew(loop, 1, LOOP);
3992 loop->op_targ = padoff;
3993 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
3994 PL_copline = forline;
3995 return newSTATEOP(0, label, wop);
3999 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
4003 if (type != OP_GOTO || label->op_type == OP_CONST) {
4004 /* "last()" means "last" */
4005 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
4006 o = newOP(type, OPf_SPECIAL);
4008 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
4009 ? SvPVx_nolen_const(((SVOP*)label)->op_sv)
4015 /* Check whether it's going to be a goto &function */
4016 if (label->op_type == OP_ENTERSUB
4017 && !(label->op_flags & OPf_STACKED))
4018 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
4019 o = newUNOP(type, OPf_STACKED, label);
4021 PL_hints |= HINT_BLOCK_SCOPE;
4026 =for apidoc cv_undef
4028 Clear out all the active components of a CV. This can happen either
4029 by an explicit C<undef &foo>, or by the reference count going to zero.
4030 In the former case, we keep the CvOUTSIDE pointer, so that any anonymous
4031 children can still follow the full lexical scope chain.
4037 Perl_cv_undef(pTHX_ CV *cv)
4039 #ifdef USE_5005THREADS
4041 MUTEX_DESTROY(CvMUTEXP(cv));
4042 Safefree(CvMUTEXP(cv));
4045 #endif /* USE_5005THREADS */
4048 if (CvFILE(cv) && !CvXSUB(cv)) {
4049 /* for XSUBs CvFILE point directly to static memory; __FILE__ */
4050 Safefree(CvFILE(cv));
4055 if (!CvXSUB(cv) && CvROOT(cv)) {
4056 #ifdef USE_5005THREADS
4057 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
4058 Perl_croak(aTHX_ "Can't undef active subroutine");
4061 Perl_croak(aTHX_ "Can't undef active subroutine");
4062 #endif /* USE_5005THREADS */
4065 PAD_SAVE_SETNULLPAD();
4067 op_free(CvROOT(cv));
4068 CvROOT(cv) = Nullop;
4069 CvSTART(cv) = Nullop;
4072 SvPOK_off((SV*)cv); /* forget prototype */
4077 /* remove CvOUTSIDE unless this is an undef rather than a free */
4078 if (!SvREFCNT(cv) && CvOUTSIDE(cv)) {
4079 if (!CvWEAKOUTSIDE(cv))
4080 SvREFCNT_dec(CvOUTSIDE(cv));
4081 CvOUTSIDE(cv) = Nullcv;
4084 SvREFCNT_dec((SV*)CvXSUBANY(cv).any_ptr);
4090 /* delete all flags except WEAKOUTSIDE */
4091 CvFLAGS(cv) &= CVf_WEAKOUTSIDE;
4095 Perl_cv_ckproto(pTHX_ CV *cv, GV *gv, char *p)
4097 if (((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX_const(cv)))) && ckWARN_d(WARN_PROTOTYPE)) {
4098 SV* const msg = sv_newmortal();
4102 gv_efullname3(name = sv_newmortal(), gv, Nullch);
4103 sv_setpv(msg, "Prototype mismatch:");
4105 Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, name);
4107 Perl_sv_catpvf(aTHX_ msg, " (%"SVf")", (const SV *)cv);
4109 Perl_sv_catpv(aTHX_ msg, ": none");
4110 sv_catpv(msg, " vs ");
4112 Perl_sv_catpvf(aTHX_ msg, "(%s)", p);
4114 sv_catpv(msg, "none");
4115 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%"SVf, msg);
4119 static void const_sv_xsub(pTHX_ CV* cv);
4123 =head1 Optree Manipulation Functions
4125 =for apidoc cv_const_sv
4127 If C<cv> is a constant sub eligible for inlining. returns the constant
4128 value returned by the sub. Otherwise, returns NULL.
4130 Constant subs can be created with C<newCONSTSUB> or as described in
4131 L<perlsub/"Constant Functions">.
4136 Perl_cv_const_sv(pTHX_ CV *cv)
4138 if (!cv || !CvCONST(cv))
4140 return (SV*)CvXSUBANY(cv).any_ptr;
4144 Perl_op_const_sv(pTHX_ OP *o, CV *cv)
4151 if (o->op_type == OP_LINESEQ && cLISTOPo->op_first)
4152 o = cLISTOPo->op_first->op_sibling;
4154 for (; o; o = o->op_next) {
4155 const OPCODE type = o->op_type;
4157 if (sv && o->op_next == o)
4159 if (o->op_next != o) {
4160 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
4162 if (type == OP_DBSTATE)
4165 if (type == OP_LEAVESUB || type == OP_RETURN)
4169 if (type == OP_CONST && cSVOPo->op_sv)
4171 else if ((type == OP_PADSV || type == OP_CONST) && cv) {
4172 sv = PAD_BASE_SV(CvPADLIST(cv), o->op_targ);
4176 /* We get here only from cv_clone2() while creating a closure.
4177 Copy the const value here instead of in cv_clone2 so that
4178 SvREADONLY_on doesn't lead to problems when leaving
4183 if (!SvREADONLY(sv) && SvREFCNT(sv) > 1)
4195 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4197 PERL_UNUSED_ARG(floor);
4207 Perl_croak(aTHX_ "\"my sub\" not yet implemented");
4211 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
4213 return Perl_newATTRSUB(aTHX_ floor, o, proto, Nullop, block);
4217 Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4227 const char * const name = o ? SvPVx_nolen_const(cSVOPo->op_sv) : Nullch;
4230 assert(proto->op_type == OP_CONST);
4231 ps = SvPVx_const(((SVOP*)proto)->op_sv, ps_len);
4236 if (!name && PERLDB_NAMEANON && CopLINE(PL_curcop)) {
4237 SV * const sv = sv_newmortal();
4238 Perl_sv_setpvf(aTHX_ sv, "%s[%s:%"IVdf"]",
4239 PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
4240 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
4241 aname = SvPVX_const(sv);
4246 /* There may be future conflict here as change 23766 is not yet merged. */
4247 gv_fetch_flags = (block || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS))
4248 ? GV_ADDMULTI : GV_ADDMULTI | GV_NOINIT;
4249 gv = gv_fetchpv(name ? name : (aname ? aname :
4250 (PL_curstash ? "__ANON__" : "__ANON__::__ANON__")),
4251 gv_fetch_flags, SVt_PVCV);
4260 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
4261 maximum a prototype before. */
4262 if (SvTYPE(gv) > SVt_NULL) {
4263 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1)
4264 && ckWARN_d(WARN_PROTOTYPE))
4266 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "Runaway prototype");
4268 cv_ckproto((CV*)gv, NULL, (char *)ps);
4271 sv_setpvn((SV*)gv, ps, ps_len);
4273 sv_setiv((SV*)gv, -1);
4274 SvREFCNT_dec(PL_compcv);
4275 cv = PL_compcv = NULL;
4276 PL_sub_generation++;
4280 cv = (!name || GvCVGEN(gv)) ? Nullcv : GvCV(gv);
4282 #ifdef GV_UNIQUE_CHECK
4283 if (cv && GvUNIQUE(gv) && SvREADONLY(cv)) {
4284 Perl_croak(aTHX_ "Can't define subroutine %s (GV is unique)", name);
4288 if (!block || !ps || *ps || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS))
4291 const_sv = op_const_sv(block, Nullcv);
4294 const bool exists = CvROOT(cv) || CvXSUB(cv);
4296 #ifdef GV_UNIQUE_CHECK
4297 if (exists && GvUNIQUE(gv)) {
4298 Perl_croak(aTHX_ "Can't redefine unique subroutine %s", name);
4302 /* if the subroutine doesn't exist and wasn't pre-declared
4303 * with a prototype, assume it will be AUTOLOADed,
4304 * skipping the prototype check
4306 if (exists || SvPOK(cv))
4307 cv_ckproto(cv, gv, (char *)ps);
4308 /* already defined (or promised)? */
4309 if (exists || GvASSUMECV(gv)) {
4310 if (!block && !attrs) {
4311 if (CvFLAGS(PL_compcv)) {
4312 /* might have had built-in attrs applied */
4313 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
4315 /* just a "sub foo;" when &foo is already defined */
4316 SAVEFREESV(PL_compcv);
4319 /* ahem, death to those who redefine active sort subs */
4320 if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv))
4321 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", name);
4323 if (ckWARN(WARN_REDEFINE)
4325 && (!const_sv || sv_cmp(cv_const_sv(cv), const_sv))))
4327 const line_t oldline = CopLINE(PL_curcop);
4328 if (PL_copline != NOLINE)
4329 CopLINE_set(PL_curcop, PL_copline);
4330 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
4331 CvCONST(cv) ? "Constant subroutine %s redefined"
4332 : "Subroutine %s redefined", name);
4333 CopLINE_set(PL_curcop, oldline);
4341 (void)SvREFCNT_inc(const_sv);
4343 assert(!CvROOT(cv) && !CvCONST(cv));
4344 sv_setpvn((SV*)cv, "", 0); /* prototype is "" */
4345 CvXSUBANY(cv).any_ptr = const_sv;
4346 CvXSUB(cv) = const_sv_xsub;
4351 cv = newCONSTSUB(NULL, (char *)name, const_sv);
4354 SvREFCNT_dec(PL_compcv);
4356 PL_sub_generation++;
4363 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>
4364 * before we clobber PL_compcv.
4368 /* Might have had built-in attributes applied -- propagate them. */
4369 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
4370 if (CvGV(cv) && GvSTASH(CvGV(cv)))
4371 stash = GvSTASH(CvGV(cv));
4372 else if (CvSTASH(cv))
4373 stash = CvSTASH(cv);
4375 stash = PL_curstash;
4378 /* possibly about to re-define existing subr -- ignore old cv */
4379 rcv = (SV*)PL_compcv;
4380 if (name && GvSTASH(gv))
4381 stash = GvSTASH(gv);
4383 stash = PL_curstash;
4385 apply_attrs(stash, rcv, attrs, FALSE);
4387 if (cv) { /* must reuse cv if autoloaded */
4389 /* got here with just attrs -- work done, so bug out */
4390 SAVEFREESV(PL_compcv);
4393 /* transfer PL_compcv to cv */
4395 CvFLAGS(cv) = CvFLAGS(PL_compcv);
4396 if (!CvWEAKOUTSIDE(cv))
4397 SvREFCNT_dec(CvOUTSIDE(cv));
4398 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
4399 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv);
4400 CvOUTSIDE(PL_compcv) = 0;
4401 CvPADLIST(cv) = CvPADLIST(PL_compcv);
4402 CvPADLIST(PL_compcv) = 0;
4403 /* inner references to PL_compcv must be fixed up ... */
4404 pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv);
4405 /* ... before we throw it away */
4406 SvREFCNT_dec(PL_compcv);
4407 if (PERLDB_INTER)/* Advice debugger on the new sub. */
4408 ++PL_sub_generation;
4415 PL_sub_generation++;
4419 CvFILE_set_from_cop(cv, PL_curcop);
4420 CvSTASH(cv) = PL_curstash;
4421 #ifdef USE_5005THREADS
4423 if (!CvMUTEXP(cv)) {
4424 New(666, CvMUTEXP(cv), 1, perl_mutex);
4425 MUTEX_INIT(CvMUTEXP(cv));
4427 #endif /* USE_5005THREADS */
4430 sv_setpvn((SV*)cv, ps, ps_len);
4432 if (PL_error_count) {
4436 const char *s = strrchr(name, ':');
4438 if (strEQ(s, "BEGIN")) {
4439 const char not_safe[] =
4440 "BEGIN not safe after errors--compilation aborted";
4441 if (PL_in_eval & EVAL_KEEPERR)
4442 Perl_croak(aTHX_ not_safe);
4444 /* force display of errors found but not reported */
4445 sv_catpv(ERRSV, not_safe);
4446 Perl_croak(aTHX_ "%"SVf, ERRSV);
4455 CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0,
4456 mod(scalarseq(block), OP_LEAVESUBLV));
4459 /* This makes sub {}; work as expected. */
4460 if (block->op_type == OP_STUB) {
4462 block = newSTATEOP(0, Nullch, 0);
4464 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4466 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4467 OpREFCNT_set(CvROOT(cv), 1);
4468 CvSTART(cv) = LINKLIST(CvROOT(cv));
4469 CvROOT(cv)->op_next = 0;
4470 CALL_PEEP(CvSTART(cv));
4472 /* now that optimizer has done its work, adjust pad values */
4474 pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
4477 assert(!CvCONST(cv));
4478 if (ps && !*ps && op_const_sv(block, cv))
4482 if (name || aname) {
4484 const char *tname = (name ? name : aname);
4486 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4487 SV *sv = NEWSV(0,0);
4488 SV *tmpstr = sv_newmortal();
4489 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
4493 Perl_sv_setpvf(aTHX_ sv, "%s:%ld-%ld",
4495 (long)PL_subline, (long)CopLINE(PL_curcop));
4496 gv_efullname3(tmpstr, gv, Nullch);
4497 hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr), SvCUR(tmpstr), sv, 0);
4498 hv = GvHVn(db_postponed);
4499 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvCUR(tmpstr))
4500 && (pcv = GvCV(db_postponed)))
4506 call_sv((SV*)pcv, G_DISCARD);
4510 if ((s = strrchr(tname,':')))
4515 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4518 if (strEQ(s, "BEGIN")) {
4519 const I32 oldscope = PL_scopestack_ix;
4521 SAVECOPFILE(&PL_compiling);
4522 SAVECOPLINE(&PL_compiling);
4525 PL_beginav = newAV();
4526 DEBUG_x( dump_sub(gv) );
4527 av_push(PL_beginav, (SV*)cv);
4528 GvCV(gv) = 0; /* cv has been hijacked */
4529 call_list(oldscope, PL_beginav);
4531 PL_curcop = &PL_compiling;
4532 PL_compiling.op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
4535 else if (strEQ(s, "END") && !PL_error_count) {
4538 DEBUG_x( dump_sub(gv) );
4539 av_unshift(PL_endav, 1);
4540 av_store(PL_endav, 0, (SV*)cv);
4541 GvCV(gv) = 0; /* cv has been hijacked */
4543 else if (strEQ(s, "CHECK") && !PL_error_count) {
4545 PL_checkav = newAV();
4546 DEBUG_x( dump_sub(gv) );
4547 if (PL_main_start && ckWARN(WARN_VOID))
4548 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run CHECK block");
4549 av_unshift(PL_checkav, 1);
4550 av_store(PL_checkav, 0, (SV*)cv);
4551 GvCV(gv) = 0; /* cv has been hijacked */
4553 else if (strEQ(s, "INIT") && !PL_error_count) {
4555 PL_initav = newAV();
4556 DEBUG_x( dump_sub(gv) );
4557 if (PL_main_start && ckWARN(WARN_VOID))
4558 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run INIT block");
4559 av_push(PL_initav, (SV*)cv);
4560 GvCV(gv) = 0; /* cv has been hijacked */
4565 PL_copline = NOLINE;
4570 /* XXX unsafe for threads if eval_owner isn't held */
4572 =for apidoc newCONSTSUB
4574 Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is
4575 eligible for inlining at compile-time.
4581 Perl_newCONSTSUB(pTHX_ HV *stash, char *name, SV *sv)
4587 SAVECOPLINE(PL_curcop);
4588 CopLINE_set(PL_curcop, PL_copline);
4591 PL_hints &= ~HINT_BLOCK_SCOPE;
4594 SAVESPTR(PL_curstash);
4595 SAVECOPSTASH(PL_curcop);
4596 PL_curstash = stash;
4597 CopSTASH_set(PL_curcop,stash);
4600 cv = newXS(name, const_sv_xsub, savepv(CopFILE(PL_curcop)));
4601 CvXSUBANY(cv).any_ptr = sv;
4603 sv_setpvn((SV*)cv, "", 0); /* prototype is "" */
4606 CopSTASH_free(PL_curcop);
4614 =for apidoc U||newXS
4616 Used by C<xsubpp> to hook up XSUBs as Perl subs.
4622 Perl_newXS(pTHX_ char *name, XSUBADDR_t subaddr, char *filename)
4624 GV * const gv = gv_fetchpv(name ? name :
4625 (PL_curstash ? "__ANON__" : "__ANON__::__ANON__"),
4626 GV_ADDMULTI, SVt_PVCV);
4629 if ((cv = (name ? GvCV(gv) : Nullcv))) {
4631 /* just a cached method */
4635 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4636 /* already defined (or promised) */
4637 /* XXX It's possible for this HvNAME_get to return null, and get passed into strEQ */
4638 if (ckWARN(WARN_REDEFINE)) {
4639 GV * const gvcv = CvGV(cv);
4641 HV * const stash = GvSTASH(gvcv);
4643 const char *name = HvNAME_get(stash);
4644 if ( strEQ(name,"autouse") ) {
4645 const line_t oldline = CopLINE(PL_curcop);
4646 if (PL_copline != NOLINE)
4647 CopLINE_set(PL_curcop, PL_copline);
4648 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
4649 CvCONST(cv) ? "Constant subroutine %s redefined"
4650 : "Subroutine %s redefined"
4652 CopLINE_set(PL_curcop, oldline);
4662 if (cv) /* must reuse cv if autoloaded */
4665 cv = (CV*)NEWSV(1105,0);
4666 sv_upgrade((SV *)cv, SVt_PVCV);
4670 PL_sub_generation++;
4674 #ifdef USE_5005THREADS
4675 New(666, CvMUTEXP(cv), 1, perl_mutex);
4676 MUTEX_INIT(CvMUTEXP(cv));
4678 #endif /* USE_5005THREADS */
4679 (void)gv_fetchfile(filename);
4680 CvFILE(cv) = (char *)filename; /* NOTE: not copied, as it is expected to be
4681 an external constant string */
4682 CvXSUB(cv) = subaddr;
4685 const char *s = strrchr(name,':');
4691 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4694 if (strEQ(s, "BEGIN")) {
4696 PL_beginav = newAV();
4697 av_push(PL_beginav, (SV*)cv);
4698 GvCV(gv) = 0; /* cv has been hijacked */
4700 else if (strEQ(s, "END")) {
4703 av_unshift(PL_endav, 1);
4704 av_store(PL_endav, 0, (SV*)cv);
4705 GvCV(gv) = 0; /* cv has been hijacked */
4707 else if (strEQ(s, "CHECK")) {
4709 PL_checkav = newAV();
4710 if (PL_main_start && ckWARN(WARN_VOID))
4711 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run CHECK block");
4712 av_unshift(PL_checkav, 1);
4713 av_store(PL_checkav, 0, (SV*)cv);
4714 GvCV(gv) = 0; /* cv has been hijacked */
4716 else if (strEQ(s, "INIT")) {
4718 PL_initav = newAV();
4719 if (PL_main_start && ckWARN(WARN_VOID))
4720 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run INIT block");
4721 av_push(PL_initav, (SV*)cv);
4722 GvCV(gv) = 0; /* cv has been hijacked */
4733 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
4741 name = SvPVx(cSVOPo->op_sv, n_a);
4744 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
4745 #ifdef GV_UNIQUE_CHECK
4747 Perl_croak(aTHX_ "Bad symbol for form (GV is unique)");
4751 if ((cv = GvFORM(gv))) {
4752 if (ckWARN(WARN_REDEFINE)) {
4753 const line_t oldline = CopLINE(PL_curcop);
4754 if (PL_copline != NOLINE)
4755 CopLINE_set(PL_curcop, PL_copline);
4756 Perl_warner(aTHX_ packWARN(WARN_REDEFINE), "Format %s redefined",name);
4757 CopLINE_set(PL_curcop, oldline);
4764 CvFILE_set_from_cop(cv, PL_curcop);
4767 pad_tidy(padtidy_FORMAT);
4768 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4769 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4770 OpREFCNT_set(CvROOT(cv), 1);
4771 CvSTART(cv) = LINKLIST(CvROOT(cv));
4772 CvROOT(cv)->op_next = 0;
4773 CALL_PEEP(CvSTART(cv));
4775 PL_copline = NOLINE;
4780 Perl_newANONLIST(pTHX_ OP *o)
4782 return newUNOP(OP_REFGEN, 0,
4783 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
4787 Perl_newANONHASH(pTHX_ OP *o)
4789 return newUNOP(OP_REFGEN, 0,
4790 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
4794 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
4796 return newANONATTRSUB(floor, proto, Nullop, block);
4800 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
4802 return newUNOP(OP_REFGEN, 0,
4803 newSVOP(OP_ANONCODE, 0,
4804 (SV*)newATTRSUB(floor, 0, proto, attrs, block)));
4808 Perl_oopsAV(pTHX_ OP *o)
4810 switch (o->op_type) {
4812 o->op_type = OP_PADAV;
4813 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4814 return ref(o, OP_RV2AV);
4817 o->op_type = OP_RV2AV;
4818 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4823 if (ckWARN_d(WARN_INTERNAL))
4824 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV");
4831 Perl_oopsHV(pTHX_ OP *o)
4833 switch (o->op_type) {
4836 o->op_type = OP_PADHV;
4837 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4838 return ref(o, OP_RV2HV);
4842 o->op_type = OP_RV2HV;
4843 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
4848 if (ckWARN_d(WARN_INTERNAL))
4849 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV");
4856 Perl_newAVREF(pTHX_ OP *o)
4858 if (o->op_type == OP_PADANY) {
4859 o->op_type = OP_PADAV;
4860 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4863 else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)
4864 && ckWARN(WARN_DEPRECATED)) {
4865 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
4866 "Using an array as a reference is deprecated");
4868 return newUNOP(OP_RV2AV, 0, scalar(o));
4872 Perl_newGVREF(pTHX_ I32 type, OP *o)
4874 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
4875 return newUNOP(OP_NULL, 0, o);
4876 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
4880 Perl_newHVREF(pTHX_ OP *o)
4882 if (o->op_type == OP_PADANY) {
4883 o->op_type = OP_PADHV;
4884 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4887 else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)
4888 && ckWARN(WARN_DEPRECATED)) {
4889 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
4890 "Using a hash as a reference is deprecated");
4892 return newUNOP(OP_RV2HV, 0, scalar(o));
4896 Perl_oopsCV(pTHX_ OP *o)
4898 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
4901 NORETURN_FUNCTION_END;
4905 Perl_newCVREF(pTHX_ I32 flags, OP *o)
4907 return newUNOP(OP_RV2CV, flags, scalar(o));
4911 Perl_newSVREF(pTHX_ OP *o)
4913 if (o->op_type == OP_PADANY) {
4914 o->op_type = OP_PADSV;
4915 o->op_ppaddr = PL_ppaddr[OP_PADSV];
4918 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
4919 o->op_flags |= OPpDONE_SVREF;
4922 return newUNOP(OP_RV2SV, 0, scalar(o));
4925 /* Check routines. See the comments at the top of this file for details
4926 * on when these are called */
4929 Perl_ck_anoncode(pTHX_ OP *o)
4931 cSVOPo->op_targ = pad_add_anon(cSVOPo->op_sv, o->op_type);
4932 cSVOPo->op_sv = Nullsv;
4937 Perl_ck_bitop(pTHX_ OP *o)
4939 #define OP_IS_NUMCOMPARE(op) \
4940 ((op) == OP_LT || (op) == OP_I_LT || \
4941 (op) == OP_GT || (op) == OP_I_GT || \
4942 (op) == OP_LE || (op) == OP_I_LE || \
4943 (op) == OP_GE || (op) == OP_I_GE || \
4944 (op) == OP_EQ || (op) == OP_I_EQ || \
4945 (op) == OP_NE || (op) == OP_I_NE || \
4946 (op) == OP_NCMP || (op) == OP_I_NCMP)
4947 o->op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
4948 if (!(o->op_flags & OPf_STACKED) /* Not an assignment */
4949 && (o->op_type == OP_BIT_OR
4950 || o->op_type == OP_BIT_AND
4951 || o->op_type == OP_BIT_XOR))
4953 const OP * const left = cBINOPo->op_first;
4954 const OP * const right = left->op_sibling;
4955 if ((OP_IS_NUMCOMPARE(left->op_type) &&
4956 (left->op_flags & OPf_PARENS) == 0) ||
4957 (OP_IS_NUMCOMPARE(right->op_type) &&
4958 (right->op_flags & OPf_PARENS) == 0))
4959 if (ckWARN(WARN_PRECEDENCE))
4960 Perl_warner(aTHX_ packWARN(WARN_PRECEDENCE),
4961 "Possible precedence problem on bitwise %c operator",
4962 o->op_type == OP_BIT_OR ? '|'
4963 : o->op_type == OP_BIT_AND ? '&' : '^'
4970 Perl_ck_concat(pTHX_ OP *o)
4972 const OP *kid = cUNOPo->op_first;
4973 if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) &&
4974 !(kUNOP->op_first->op_flags & OPf_MOD))
4975 o->op_flags |= OPf_STACKED;
4980 Perl_ck_spair(pTHX_ OP *o)
4982 if (o->op_flags & OPf_KIDS) {
4985 const OPCODE type = o->op_type;
4986 o = modkids(ck_fun(o), type);
4987 kid = cUNOPo->op_first;
4988 newop = kUNOP->op_first->op_sibling;
4990 (newop->op_sibling ||
4991 !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
4992 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
4993 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
4997 op_free(kUNOP->op_first);
4998 kUNOP->op_first = newop;
5000 o->op_ppaddr = PL_ppaddr[++o->op_type];
5005 Perl_ck_delete(pTHX_ OP *o)
5009 if (o->op_flags & OPf_KIDS) {
5010 OP *kid = cUNOPo->op_first;
5011 switch (kid->op_type) {
5013 o->op_flags |= OPf_SPECIAL;
5016 o->op_private |= OPpSLICE;
5019 o->op_flags |= OPf_SPECIAL;
5024 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element or slice",
5033 Perl_ck_die(pTHX_ OP *o)
5036 if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH;
5042 Perl_ck_eof(pTHX_ OP *o)
5044 const I32 type = o->op_type;
5046 if (o->op_flags & OPf_KIDS) {
5047 if (cLISTOPo->op_first->op_type == OP_STUB) {
5049 o = newUNOP(type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv));
5057 Perl_ck_eval(pTHX_ OP *o)
5059 PL_hints |= HINT_BLOCK_SCOPE;
5060 if (o->op_flags & OPf_KIDS) {
5061 SVOP * const kid = (SVOP*)cUNOPo->op_first;
5064 o->op_flags &= ~OPf_KIDS;
5067 else if (kid->op_type == OP_LINESEQ || kid->op_type == OP_STUB) {
5070 cUNOPo->op_first = 0;
5073 NewOp(1101, enter, 1, LOGOP);
5074 enter->op_type = OP_ENTERTRY;
5075 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
5076 enter->op_private = 0;
5078 /* establish postfix order */
5079 enter->op_next = (OP*)enter;
5081 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
5082 o->op_type = OP_LEAVETRY;
5083 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
5084 enter->op_other = o;
5092 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
5094 o->op_targ = (PADOFFSET)PL_hints;
5099 Perl_ck_exit(pTHX_ OP *o)
5102 HV *table = GvHV(PL_hintgv);
5104 SV **svp = hv_fetch(table, "vmsish_exit", 11, FALSE);
5105 if (svp && *svp && SvTRUE(*svp))
5106 o->op_private |= OPpEXIT_VMSISH;
5108 if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH;
5114 Perl_ck_exec(pTHX_ OP *o)
5116 if (o->op_flags & OPf_STACKED) {
5119 kid = cUNOPo->op_first->op_sibling;
5120 if (kid->op_type == OP_RV2GV)
5129 Perl_ck_exists(pTHX_ OP *o)
5132 if (o->op_flags & OPf_KIDS) {
5133 OP * const kid = cUNOPo->op_first;
5134 if (kid->op_type == OP_ENTERSUB) {
5135 (void) ref(kid, o->op_type);
5136 if (kid->op_type != OP_RV2CV && !PL_error_count)
5137 Perl_croak(aTHX_ "%s argument is not a subroutine name",
5139 o->op_private |= OPpEXISTS_SUB;
5141 else if (kid->op_type == OP_AELEM)
5142 o->op_flags |= OPf_SPECIAL;
5143 else if (kid->op_type != OP_HELEM)
5144 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element",
5152 Perl_ck_rvconst(pTHX_ register OP *o)
5154 SVOP *kid = (SVOP*)cUNOPo->op_first;
5156 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5157 if (kid->op_type == OP_CONST) {
5161 SV * const kidsv = kid->op_sv;
5164 /* Is it a constant from cv_const_sv()? */
5165 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
5166 SV *rsv = SvRV(kidsv);
5167 const int svtype = SvTYPE(rsv);
5168 const char *badtype = Nullch;
5170 switch (o->op_type) {
5172 if (svtype > SVt_PVMG)
5173 badtype = "a SCALAR";
5176 if (svtype != SVt_PVAV)
5177 badtype = "an ARRAY";
5180 if (svtype != SVt_PVHV) {
5181 if (svtype == SVt_PVAV) { /* pseudohash? */
5182 SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
5183 if (ksv && SvROK(*ksv)
5184 && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
5193 if (svtype != SVt_PVCV)
5198 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
5201 name = SvPV(kidsv, n_a);
5202 if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
5203 const char *badthing = Nullch;
5204 switch (o->op_type) {
5206 badthing = "a SCALAR";
5209 badthing = "an ARRAY";
5212 badthing = "a HASH";
5217 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
5221 * This is a little tricky. We only want to add the symbol if we
5222 * didn't add it in the lexer. Otherwise we get duplicate strict
5223 * warnings. But if we didn't add it in the lexer, we must at
5224 * least pretend like we wanted to add it even if it existed before,
5225 * or we get possible typo warnings. OPpCONST_ENTERED says
5226 * whether the lexer already added THIS instance of this symbol.
5228 iscv = (o->op_type == OP_RV2CV) * 2;
5230 gv = gv_fetchpv(name,
5231 iscv | !(kid->op_private & OPpCONST_ENTERED),
5234 : o->op_type == OP_RV2SV
5236 : o->op_type == OP_RV2AV
5238 : o->op_type == OP_RV2HV
5241 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
5243 kid->op_type = OP_GV;
5244 SvREFCNT_dec(kid->op_sv);
5246 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
5247 kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
5248 SvREFCNT_dec(PAD_SVl(kPADOP->op_padix));
5250 PAD_SETSV(kPADOP->op_padix, (SV*) SvREFCNT_inc(gv));
5252 kid->op_sv = SvREFCNT_inc(gv);
5254 kid->op_private = 0;
5255 kid->op_ppaddr = PL_ppaddr[OP_GV];
5262 Perl_ck_ftst(pTHX_ OP *o)
5264 const I32 type = o->op_type;
5266 if (o->op_flags & OPf_REF) {
5269 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
5270 SVOP *kid = (SVOP*)cUNOPo->op_first;
5272 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5274 OP *newop = newGVOP(type, OPf_REF,
5275 gv_fetchpv(SvPVx(kid->op_sv, n_a), TRUE, SVt_PVIO));
5280 if ((PL_hints & HINT_FILETEST_ACCESS) &&
5281 OP_IS_FILETEST_ACCESS(o))
5282 o->op_private |= OPpFT_ACCESS;
5287 if (type == OP_FTTTY)
5288 o = newGVOP(type, OPf_REF, PL_stdingv);
5290 o = newUNOP(type, 0, newDEFSVOP());
5296 Perl_ck_fun(pTHX_ OP *o)
5298 const int type = o->op_type;
5299 register I32 oa = PL_opargs[type] >> OASHIFT;
5301 if (o->op_flags & OPf_STACKED) {
5302 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
5305 return no_fh_allowed(o);
5308 if (o->op_flags & OPf_KIDS) {
5310 OP **tokid = &cLISTOPo->op_first;
5311 register OP *kid = cLISTOPo->op_first;
5315 if (kid->op_type == OP_PUSHMARK ||
5316 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
5318 tokid = &kid->op_sibling;
5319 kid = kid->op_sibling;
5321 if (!kid && PL_opargs[type] & OA_DEFGV)
5322 *tokid = kid = newDEFSVOP();
5326 sibl = kid->op_sibling;
5329 /* list seen where single (scalar) arg expected? */
5330 if (numargs == 1 && !(oa >> 4)
5331 && kid->op_type == OP_LIST && type != OP_SCALAR)
5333 return too_many_arguments(o,PL_op_desc[type]);
5346 if ((type == OP_PUSH || type == OP_UNSHIFT)
5347 && !kid->op_sibling && ckWARN(WARN_SYNTAX))
5348 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
5349 "Useless use of %s with no values",
5352 if (kid->op_type == OP_CONST &&
5353 (kid->op_private & OPpCONST_BARE))
5355 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5356 OP *newop = newAVREF(newGVOP(OP_GV, 0,
5357 gv_fetchpv(name, TRUE, SVt_PVAV) ));
5358 if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX))
5359 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5360 "Array @%s missing the @ in argument %"IVdf" of %s()",
5361 name, (IV)numargs, PL_op_desc[type]);
5364 kid->op_sibling = sibl;
5367 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
5368 bad_type(numargs, "array", PL_op_desc[type], kid);
5372 if (kid->op_type == OP_CONST &&
5373 (kid->op_private & OPpCONST_BARE))
5375 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5376 OP *newop = newHVREF(newGVOP(OP_GV, 0,
5377 gv_fetchpv(name, TRUE, SVt_PVHV) ));
5378 if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX))
5379 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5380 "Hash %%%s missing the %% in argument %"IVdf" of %s()",
5381 name, (IV)numargs, PL_op_desc[type]);
5384 kid->op_sibling = sibl;
5387 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
5388 bad_type(numargs, "hash", PL_op_desc[type], kid);
5393 OP *newop = newUNOP(OP_NULL, 0, kid);
5394 kid->op_sibling = 0;
5396 newop->op_next = newop;
5398 kid->op_sibling = sibl;
5403 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
5404 if (kid->op_type == OP_CONST &&
5405 (kid->op_private & OPpCONST_BARE))
5407 OP *newop = newGVOP(OP_GV, 0,
5408 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, n_a), TRUE,
5410 if (!(o->op_private & 1) && /* if not unop */
5411 kid == cLISTOPo->op_last)
5412 cLISTOPo->op_last = newop;
5416 else if (kid->op_type == OP_READLINE) {
5417 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
5418 bad_type(numargs, "HANDLE", OP_DESC(o), kid);
5421 I32 flags = OPf_SPECIAL;
5425 /* is this op a FH constructor? */
5426 if (is_handle_constructor(o,numargs)) {
5427 const char *name = Nullch;
5431 /* Set a flag to tell rv2gv to vivify
5432 * need to "prove" flag does not mean something
5433 * else already - NI-S 1999/05/07
5436 if (kid->op_type == OP_PADSV) {
5437 /*XXX DAPM 2002.08.25 tmp assert test */
5438 /*XXX*/ assert(av_fetch(PL_comppad_name, (kid->op_targ), FALSE));
5439 /*XXX*/ assert(*av_fetch(PL_comppad_name, (kid->op_targ), FALSE));
5441 name = PAD_COMPNAME_PV(kid->op_targ);
5442 /* SvCUR of a pad namesv can't be trusted
5443 * (see PL_generation), so calc its length
5449 else if (kid->op_type == OP_RV2SV
5450 && kUNOP->op_first->op_type == OP_GV)
5452 GV *gv = cGVOPx_gv(kUNOP->op_first);
5454 len = GvNAMELEN(gv);
5456 else if (kid->op_type == OP_AELEM
5457 || kid->op_type == OP_HELEM)
5462 if ((op = ((BINOP*)kid)->op_first)) {
5463 SV *tmpstr = Nullsv;
5465 kid->op_type == OP_AELEM ?
5467 if (((op->op_type == OP_RV2AV) ||
5468 (op->op_type == OP_RV2HV)) &&
5469 (op = ((UNOP*)op)->op_first) &&
5470 (op->op_type == OP_GV)) {
5471 /* packagevar $a[] or $h{} */
5472 GV *gv = cGVOPx_gv(op);
5480 else if (op->op_type == OP_PADAV
5481 || op->op_type == OP_PADHV) {
5482 /* lexicalvar $a[] or $h{} */
5483 const char *padname =
5484 PAD_COMPNAME_PV(op->op_targ);
5494 name = SvPV_const(tmpstr, len);
5499 name = "__ANONIO__";
5506 targ = pad_alloc(OP_RV2GV, SVs_PADTMP);
5507 namesv = PAD_SVl(targ);
5508 (void)SvUPGRADE(namesv, SVt_PV);
5510 sv_setpvn(namesv, "$", 1);
5511 sv_catpvn(namesv, name, len);
5514 kid->op_sibling = 0;
5515 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
5516 kid->op_targ = targ;
5517 kid->op_private |= priv;
5519 kid->op_sibling = sibl;
5525 mod(scalar(kid), type);
5529 tokid = &kid->op_sibling;
5530 kid = kid->op_sibling;
5532 o->op_private |= numargs;
5534 return too_many_arguments(o,OP_DESC(o));
5537 else if (PL_opargs[type] & OA_DEFGV) {
5539 return newUNOP(type, 0, newDEFSVOP());
5543 while (oa & OA_OPTIONAL)
5545 if (oa && oa != OA_LIST)
5546 return too_few_arguments(o,OP_DESC(o));
5552 Perl_ck_glob(pTHX_ OP *o)
5557 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
5558 append_elem(OP_GLOB, o, newDEFSVOP());
5560 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV))
5561 && GvCVu(gv) && GvIMPORTED_CV(gv)))
5563 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5566 #if !defined(PERL_EXTERNAL_GLOB)
5567 /* XXX this can be tightened up and made more failsafe. */
5568 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
5571 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
5572 newSVpvn("File::Glob", 10), Nullsv, Nullsv, Nullsv);
5573 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5574 glob_gv = gv_fetchpv("File::Glob::csh_glob", FALSE, SVt_PVCV);
5575 GvCV(gv) = GvCV(glob_gv);
5576 (void)SvREFCNT_inc((SV*)GvCV(gv));
5577 GvIMPORTED_CV_on(gv);
5580 #endif /* PERL_EXTERNAL_GLOB */
5582 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
5583 append_elem(OP_GLOB, o,
5584 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
5585 o->op_type = OP_LIST;
5586 o->op_ppaddr = PL_ppaddr[OP_LIST];
5587 cLISTOPo->op_first->op_type = OP_PUSHMARK;
5588 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
5589 cLISTOPo->op_first->op_targ = 0;
5590 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
5591 append_elem(OP_LIST, o,
5592 scalar(newUNOP(OP_RV2CV, 0,
5593 newGVOP(OP_GV, 0, gv)))));
5594 o = newUNOP(OP_NULL, 0, ck_subr(o));
5595 o->op_targ = OP_GLOB; /* hint at what it used to be */
5598 gv = newGVgen("main");
5600 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
5606 Perl_ck_grep(pTHX_ OP *o)
5610 const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
5612 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
5613 NewOp(1101, gwop, 1, LOGOP);
5615 if (o->op_flags & OPf_STACKED) {
5618 kid = cLISTOPo->op_first->op_sibling;
5619 if (!cUNOPx(kid)->op_next)
5620 Perl_croak(aTHX_ "panic: ck_grep");
5621 for (k = cUNOPx(kid)->op_first; k; k = k->op_next) {
5624 kid->op_next = (OP*)gwop;
5625 o->op_flags &= ~OPf_STACKED;
5627 kid = cLISTOPo->op_first->op_sibling;
5628 if (type == OP_MAPWHILE)
5635 kid = cLISTOPo->op_first->op_sibling;
5636 if (kid->op_type != OP_NULL)
5637 Perl_croak(aTHX_ "panic: ck_grep");
5638 kid = kUNOP->op_first;
5640 gwop->op_type = type;
5641 gwop->op_ppaddr = PL_ppaddr[type];
5642 gwop->op_first = listkids(o);
5643 gwop->op_flags |= OPf_KIDS;
5644 gwop->op_private = 1;
5645 gwop->op_other = LINKLIST(kid);
5646 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
5647 kid->op_next = (OP*)gwop;
5649 kid = cLISTOPo->op_first->op_sibling;
5650 if (!kid || !kid->op_sibling)
5651 return too_few_arguments(o,OP_DESC(o));
5652 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
5653 mod(kid, OP_GREPSTART);
5659 Perl_ck_index(pTHX_ OP *o)
5661 if (o->op_flags & OPf_KIDS) {
5662 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5664 kid = kid->op_sibling; /* get past "big" */
5665 if (kid && kid->op_type == OP_CONST)
5666 fbm_compile(((SVOP*)kid)->op_sv, 0);
5672 Perl_ck_lengthconst(pTHX_ OP *o)
5674 /* XXX length optimization goes here */
5679 Perl_ck_lfun(pTHX_ OP *o)
5681 const OPCODE type = o->op_type;
5682 return modkids(ck_fun(o), type);
5686 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
5688 if ((o->op_flags & OPf_KIDS) && ckWARN2(WARN_DEPRECATED, WARN_SYNTAX)) {
5689 switch (cUNOPo->op_first->op_type) {
5691 /* This is needed for
5692 if (defined %stash::)
5693 to work. Do not break Tk.
5695 break; /* Globals via GV can be undef */
5697 case OP_AASSIGN: /* Is this a good idea? */
5698 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5699 "defined(@array) is deprecated");
5700 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5701 "\t(Maybe you should just omit the defined()?)\n");
5704 /* This is needed for
5705 if (defined %stash::)
5706 to work. Do not break Tk.
5708 break; /* Globals via GV can be undef */
5710 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5711 "defined(%%hash) is deprecated");
5712 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5713 "\t(Maybe you should just omit the defined()?)\n");
5724 Perl_ck_rfun(pTHX_ OP *o)
5726 const OPCODE type = o->op_type;
5727 return refkids(ck_fun(o), type);
5731 Perl_ck_listiob(pTHX_ OP *o)
5735 kid = cLISTOPo->op_first;
5738 kid = cLISTOPo->op_first;
5740 if (kid->op_type == OP_PUSHMARK)
5741 kid = kid->op_sibling;
5742 if (kid && o->op_flags & OPf_STACKED)
5743 kid = kid->op_sibling;
5744 else if (kid && !kid->op_sibling) { /* print HANDLE; */
5745 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
5746 o->op_flags |= OPf_STACKED; /* make it a filehandle */
5747 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
5748 cLISTOPo->op_first->op_sibling = kid;
5749 cLISTOPo->op_last = kid;
5750 kid = kid->op_sibling;
5755 append_elem(o->op_type, o, newDEFSVOP());
5761 Perl_ck_sassign(pTHX_ OP *o)
5763 OP *kid = cLISTOPo->op_first;
5764 /* has a disposable target? */
5765 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
5766 && !(kid->op_flags & OPf_STACKED)
5767 /* Cannot steal the second time! */
5768 && !(kid->op_private & OPpTARGET_MY))
5770 OP *kkid = kid->op_sibling;
5772 /* Can just relocate the target. */
5773 if (kkid && kkid->op_type == OP_PADSV
5774 && !(kkid->op_private & OPpLVAL_INTRO))
5776 kid->op_targ = kkid->op_targ;
5778 /* Now we do not need PADSV and SASSIGN. */
5779 kid->op_sibling = o->op_sibling; /* NULL */
5780 cLISTOPo->op_first = NULL;
5783 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
5787 /* optimise C<my $x = undef> to C<my $x> */
5788 if (kid->op_type == OP_UNDEF) {
5789 OP *kkid = kid->op_sibling;
5790 if (kkid && kkid->op_type == OP_PADSV
5791 && (kkid->op_private & OPpLVAL_INTRO))
5793 cLISTOPo->op_first = NULL;
5794 kid->op_sibling = NULL;
5804 Perl_ck_match(pTHX_ OP *o)
5806 o->op_private |= OPpRUNTIME;
5811 Perl_ck_method(pTHX_ OP *o)
5813 OP *kid = cUNOPo->op_first;
5814 if (kid->op_type == OP_CONST) {
5815 SV* sv = kSVOP->op_sv;
5816 if (!(strchr(SvPVX_const(sv), ':') || strchr(SvPVX_const(sv), '\''))) {
5818 if (!SvREADONLY(sv) || !SvFAKE(sv)) {
5819 sv = newSVpvn_share(SvPVX_const(sv), SvCUR(sv), 0);
5822 kSVOP->op_sv = Nullsv;
5824 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
5833 Perl_ck_null(pTHX_ OP *o)
5839 Perl_ck_open(pTHX_ OP *o)
5841 HV *table = GvHV(PL_hintgv);
5845 svp = hv_fetch(table, "open_IN", 7, FALSE);
5847 mode = mode_from_discipline(*svp);
5848 if (mode & O_BINARY)
5849 o->op_private |= OPpOPEN_IN_RAW;
5850 else if (mode & O_TEXT)
5851 o->op_private |= OPpOPEN_IN_CRLF;
5854 svp = hv_fetch(table, "open_OUT", 8, FALSE);
5856 mode = mode_from_discipline(*svp);
5857 if (mode & O_BINARY)
5858 o->op_private |= OPpOPEN_OUT_RAW;
5859 else if (mode & O_TEXT)
5860 o->op_private |= OPpOPEN_OUT_CRLF;
5863 if (o->op_type == OP_BACKTICK)
5866 /* In case of three-arg dup open remove strictness
5867 * from the last arg if it is a bareword. */
5868 OP *first = cLISTOPx(o)->op_first; /* The pushmark. */
5869 OP *last = cLISTOPx(o)->op_last; /* The bareword. */
5873 if ((last->op_type == OP_CONST) && /* The bareword. */
5874 (last->op_private & OPpCONST_BARE) &&
5875 (last->op_private & OPpCONST_STRICT) &&
5876 (oa = first->op_sibling) && /* The fh. */
5877 (oa = oa->op_sibling) && /* The mode. */
5878 (oa->op_type == OP_CONST) &&
5879 SvPOK(((SVOP*)oa)->op_sv) &&
5880 (mode = SvPVX_const(((SVOP*)oa)->op_sv)) &&
5881 mode[0] == '>' && mode[1] == '&' && /* A dup open. */
5882 (last == oa->op_sibling)) /* The bareword. */
5883 last->op_private &= ~OPpCONST_STRICT;
5889 Perl_ck_repeat(pTHX_ OP *o)
5891 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
5892 o->op_private |= OPpREPEAT_DOLIST;
5893 cBINOPo->op_first = force_list(cBINOPo->op_first);
5901 Perl_ck_require(pTHX_ OP *o)
5905 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
5906 SVOP *kid = (SVOP*)cUNOPo->op_first;
5908 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5909 SV *sv = kid->op_sv;
5910 U32 was_readonly = SvREADONLY(sv);
5915 sv_force_normal_flags(sv, 0);
5916 assert(!SvREADONLY(sv));
5923 for (s = SvPVX(sv); *s; s++) {
5924 if (*s == ':' && s[1] == ':') {
5926 Move(s+2, s+1, strlen(s+2)+1, char);
5927 SvCUR_set(sv, SvCUR(sv) - 1);
5930 sv_catpvn(sv, ".pm", 3);
5931 SvFLAGS(sv) |= was_readonly;
5935 /* handle override, if any */
5936 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
5937 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv)))
5938 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
5940 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
5941 OP *kid = cUNOPo->op_first;
5942 cUNOPo->op_first = 0;
5944 return ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
5945 append_elem(OP_LIST, kid,
5946 scalar(newUNOP(OP_RV2CV, 0,
5955 Perl_ck_return(pTHX_ OP *o)
5957 if (CvLVALUE(PL_compcv)) {
5959 for (kid = cLISTOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
5960 mod(kid, OP_LEAVESUBLV);
5967 Perl_ck_retarget(pTHX_ OP *o)
5969 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
5976 Perl_ck_select(pTHX_ OP *o)
5979 if (o->op_flags & OPf_KIDS) {
5980 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5981 if (kid && kid->op_sibling) {
5982 o->op_type = OP_SSELECT;
5983 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
5985 return fold_constants(o);
5989 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5990 if (kid && kid->op_type == OP_RV2GV)
5991 kid->op_private &= ~HINT_STRICT_REFS;
5996 Perl_ck_shift(pTHX_ OP *o)
5998 const I32 type = o->op_type;
6000 if (!(o->op_flags & OPf_KIDS)) {
6004 #ifdef USE_5005THREADS
6005 if (!CvUNIQUE(PL_compcv)) {
6006 argop = newOP(OP_PADAV, OPf_REF);
6007 argop->op_targ = 0; /* PAD_SV(0) is @_ */
6010 argop = newUNOP(OP_RV2AV, 0,
6011 scalar(newGVOP(OP_GV, 0,
6012 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
6015 argop = newUNOP(OP_RV2AV, 0,
6016 scalar(newGVOP(OP_GV, 0, CvUNIQUE(PL_compcv) ? PL_argvgv : PL_defgv)));
6017 #endif /* USE_5005THREADS */
6018 return newUNOP(type, 0, scalar(argop));
6020 return scalar(modkids(ck_fun(o), type));
6024 Perl_ck_sort(pTHX_ OP *o)
6028 if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
6030 firstkid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6031 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
6033 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */
6035 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
6037 if (kid->op_type == OP_SCOPE) {
6041 else if (kid->op_type == OP_LEAVE) {
6042 if (o->op_type == OP_SORT) {
6043 op_null(kid); /* wipe out leave */
6046 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
6047 if (k->op_next == kid)
6049 /* don't descend into loops */
6050 else if (k->op_type == OP_ENTERLOOP
6051 || k->op_type == OP_ENTERITER)
6053 k = cLOOPx(k)->op_lastop;
6058 kid->op_next = 0; /* just disconnect the leave */
6059 k = kLISTOP->op_first;
6064 if (o->op_type == OP_SORT) {
6065 /* provide scalar context for comparison function/block */
6071 o->op_flags |= OPf_SPECIAL;
6073 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
6076 firstkid = firstkid->op_sibling;
6079 /* provide list context for arguments */
6080 if (o->op_type == OP_SORT)
6087 S_simplify_sort(pTHX_ OP *o)
6089 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6094 if (!(o->op_flags & OPf_STACKED))
6096 GvMULTI_on(gv_fetchpv("a", TRUE, SVt_PV));
6097 GvMULTI_on(gv_fetchpv("b", TRUE, SVt_PV));
6098 kid = kUNOP->op_first; /* get past null */
6099 if (kid->op_type != OP_SCOPE)
6101 kid = kLISTOP->op_last; /* get past scope */
6102 switch(kid->op_type) {
6110 k = kid; /* remember this node*/
6111 if (kBINOP->op_first->op_type != OP_RV2SV)
6113 kid = kBINOP->op_first; /* get past cmp */
6114 if (kUNOP->op_first->op_type != OP_GV)
6116 kid = kUNOP->op_first; /* get past rv2sv */
6118 if (GvSTASH(gv) != PL_curstash)
6120 gvname = GvNAME(gv);
6121 if (*gvname == 'a' && gvname[1] == '\0')
6123 else if (*gvname == 'b' && gvname[1] == '\0')
6128 kid = k; /* back to cmp */
6129 if (kBINOP->op_last->op_type != OP_RV2SV)
6131 kid = kBINOP->op_last; /* down to 2nd arg */
6132 if (kUNOP->op_first->op_type != OP_GV)
6134 kid = kUNOP->op_first; /* get past rv2sv */
6136 if (GvSTASH(gv) != PL_curstash)
6138 gvname = GvNAME(gv);
6140 ? !(*gvname == 'a' && gvname[1] == '\0')
6141 : !(*gvname == 'b' && gvname[1] == '\0'))
6143 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
6145 o->op_private |= OPpSORT_DESCEND;
6146 if (k->op_type == OP_NCMP)
6147 o->op_private |= OPpSORT_NUMERIC;
6148 if (k->op_type == OP_I_NCMP)
6149 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
6150 kid = cLISTOPo->op_first->op_sibling;
6151 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
6152 op_free(kid); /* then delete it */
6156 Perl_ck_split(pTHX_ OP *o)
6160 if (o->op_flags & OPf_STACKED)
6161 return no_fh_allowed(o);
6163 kid = cLISTOPo->op_first;
6164 if (kid->op_type != OP_NULL)
6165 Perl_croak(aTHX_ "panic: ck_split");
6166 kid = kid->op_sibling;
6167 op_free(cLISTOPo->op_first);
6168 cLISTOPo->op_first = kid;
6170 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvn(" ", 1));
6171 cLISTOPo->op_last = kid; /* There was only one element previously */
6174 if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) {
6175 OP *sibl = kid->op_sibling;
6176 kid->op_sibling = 0;
6177 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
6178 if (cLISTOPo->op_first == cLISTOPo->op_last)
6179 cLISTOPo->op_last = kid;
6180 cLISTOPo->op_first = kid;
6181 kid->op_sibling = sibl;
6184 kid->op_type = OP_PUSHRE;
6185 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
6187 if (((PMOP *)kid)->op_pmflags & PMf_GLOBAL && ckWARN(WARN_REGEXP)) {
6188 Perl_warner(aTHX_ packWARN(WARN_REGEXP),
6189 "Use of /g modifier is meaningless in split");
6192 if (!kid->op_sibling)
6193 append_elem(OP_SPLIT, o, newDEFSVOP());
6195 kid = kid->op_sibling;
6198 if (!kid->op_sibling)
6199 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
6201 kid = kid->op_sibling;
6204 if (kid->op_sibling)
6205 return too_many_arguments(o,OP_DESC(o));
6211 Perl_ck_join(pTHX_ OP *o)
6213 const OP *kid = cLISTOPo->op_first->op_sibling;
6214 if (kid && kid->op_type == OP_MATCH) {
6215 if (ckWARN(WARN_SYNTAX)) {
6216 const REGEXP *re = PM_GETRE(kPMOP);
6217 const char *pmstr = re ? re->precomp : "STRING";
6218 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
6219 "/%s/ should probably be written as \"%s\"",
6227 Perl_ck_subr(pTHX_ OP *o)
6229 OP *prev = ((cUNOPo->op_first->op_sibling)
6230 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
6231 OP *o2 = prev->op_sibling;
6238 I32 contextclass = 0;
6241 o->op_private |= OPpENTERSUB_HASTARG;
6242 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
6243 if (cvop->op_type == OP_RV2CV) {
6245 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
6246 op_null(cvop); /* disable rv2cv */
6247 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
6248 if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) {
6249 GV *gv = cGVOPx_gv(tmpop);
6252 tmpop->op_private |= OPpEARLY_CV;
6253 else if (SvPOK(cv)) {
6254 namegv = CvANON(cv) ? gv : CvGV(cv);
6255 proto = SvPV_nolen((SV*)cv);
6259 else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
6260 if (o2->op_type == OP_CONST)
6261 o2->op_private &= ~OPpCONST_STRICT;
6262 else if (o2->op_type == OP_LIST) {
6263 OP *o = ((UNOP*)o2)->op_first->op_sibling;
6264 if (o && o->op_type == OP_CONST)
6265 o->op_private &= ~OPpCONST_STRICT;
6268 o->op_private |= (PL_hints & HINT_STRICT_REFS);
6269 if (PERLDB_SUB && PL_curstash != PL_debstash)
6270 o->op_private |= OPpENTERSUB_DB;
6271 while (o2 != cvop) {
6275 return too_many_arguments(o, gv_ename(namegv));
6293 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
6295 arg == 1 ? "block or sub {}" : "sub {}",
6296 gv_ename(namegv), o2);
6299 /* '*' allows any scalar type, including bareword */
6302 if (o2->op_type == OP_RV2GV)
6303 goto wrapref; /* autoconvert GLOB -> GLOBref */
6304 else if (o2->op_type == OP_CONST)
6305 o2->op_private &= ~OPpCONST_STRICT;
6306 else if (o2->op_type == OP_ENTERSUB) {
6307 /* accidental subroutine, revert to bareword */
6308 OP *gvop = ((UNOP*)o2)->op_first;
6309 if (gvop && gvop->op_type == OP_NULL) {
6310 gvop = ((UNOP*)gvop)->op_first;
6312 for (; gvop->op_sibling; gvop = gvop->op_sibling)
6315 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
6316 (gvop = ((UNOP*)gvop)->op_first) &&
6317 gvop->op_type == OP_GV)
6319 GV *gv = cGVOPx_gv(gvop);
6320 OP *sibling = o2->op_sibling;
6321 SV *n = newSVpvn("",0);
6323 gv_fullname4(n, gv, "", FALSE);
6324 o2 = newSVOP(OP_CONST, 0, n);
6325 prev->op_sibling = o2;
6326 o2->op_sibling = sibling;
6342 if (contextclass++ == 0) {
6343 e = strchr(proto, ']');
6344 if (!e || e == proto)
6357 while (*--p != '[');
6358 bad_type(arg, Perl_form(aTHX_ "one of %s", p),
6359 gv_ename(namegv), o2);
6365 if (o2->op_type == OP_RV2GV)
6368 bad_type(arg, "symbol", gv_ename(namegv), o2);
6371 if (o2->op_type == OP_ENTERSUB)
6374 bad_type(arg, "subroutine entry", gv_ename(namegv), o2);
6377 if (o2->op_type == OP_RV2SV ||
6378 o2->op_type == OP_PADSV ||
6379 o2->op_type == OP_HELEM ||
6380 o2->op_type == OP_AELEM ||
6381 o2->op_type == OP_THREADSV)
6384 bad_type(arg, "scalar", gv_ename(namegv), o2);
6387 if (o2->op_type == OP_RV2AV ||
6388 o2->op_type == OP_PADAV)
6391 bad_type(arg, "array", gv_ename(namegv), o2);
6394 if (o2->op_type == OP_RV2HV ||
6395 o2->op_type == OP_PADHV)
6398 bad_type(arg, "hash", gv_ename(namegv), o2);
6403 OP* sib = kid->op_sibling;
6404 kid->op_sibling = 0;
6405 o2 = newUNOP(OP_REFGEN, 0, kid);
6406 o2->op_sibling = sib;
6407 prev->op_sibling = o2;
6409 if (contextclass && e) {
6424 Perl_croak(aTHX_ "Malformed prototype for %s: %"SVf,
6425 gv_ename(namegv), cv);
6430 mod(o2, OP_ENTERSUB);
6432 o2 = o2->op_sibling;
6434 if (proto && !optional &&
6435 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
6436 return too_few_arguments(o, gv_ename(namegv));
6441 Perl_ck_svconst(pTHX_ OP *o)
6443 SvREADONLY_on(cSVOPo->op_sv);
6448 Perl_ck_trunc(pTHX_ OP *o)
6450 if (o->op_flags & OPf_KIDS) {
6451 SVOP *kid = (SVOP*)cUNOPo->op_first;
6453 if (kid->op_type == OP_NULL)
6454 kid = (SVOP*)kid->op_sibling;
6455 if (kid && kid->op_type == OP_CONST &&
6456 (kid->op_private & OPpCONST_BARE))
6458 o->op_flags |= OPf_SPECIAL;
6459 kid->op_private &= ~OPpCONST_STRICT;
6466 Perl_ck_substr(pTHX_ OP *o)
6469 if ((o->op_flags & OPf_KIDS) && o->op_private == 4) {
6470 OP *kid = cLISTOPo->op_first;
6472 if (kid->op_type == OP_NULL)
6473 kid = kid->op_sibling;
6475 kid->op_flags |= OPf_MOD;
6481 /* A peephole optimizer. We visit the ops in the order they're to execute.
6482 * See the comments at the top of this file for more details about when
6483 * peep() is called */
6486 Perl_peep(pTHX_ register OP *o)
6488 register OP* oldop = 0;
6491 if (!o || o->op_seq)
6495 SAVEVPTR(PL_curcop);
6496 for (; o; o = o->op_next) {
6499 /* The special value -1 is used by the B::C compiler backend to indicate
6500 * that an op is statically defined and should not be freed */
6501 if (!PL_op_seqmax || PL_op_seqmax == (U16)-1)
6504 switch (o->op_type) {
6508 PL_curcop = ((COP*)o); /* for warnings */
6509 o->op_seq = PL_op_seqmax++;
6513 if (cSVOPo->op_private & OPpCONST_STRICT)
6514 no_bareword_allowed(o);
6516 case OP_METHOD_NAMED:
6517 /* Relocate sv to the pad for thread safety.
6518 * Despite being a "constant", the SV is written to,
6519 * for reference counts, sv_upgrade() etc. */
6521 const PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP);
6522 if (o->op_type == OP_CONST && SvPADTMP(cSVOPo->op_sv)) {
6523 /* If op_sv is already a PADTMP then it is being used by
6524 * some pad, so make a copy. */
6525 sv_setsv(PAD_SVl(ix),cSVOPo->op_sv);
6526 SvREADONLY_on(PAD_SVl(ix));
6527 SvREFCNT_dec(cSVOPo->op_sv);
6530 SvREFCNT_dec(PAD_SVl(ix));
6531 SvPADTMP_on(cSVOPo->op_sv);
6532 PAD_SETSV(ix, cSVOPo->op_sv);
6533 /* XXX I don't know how this isn't readonly already. */
6534 SvREADONLY_on(PAD_SVl(ix));
6536 cSVOPo->op_sv = Nullsv;
6540 o->op_seq = PL_op_seqmax++;
6544 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
6545 if (o->op_next->op_private & OPpTARGET_MY) {
6546 if (o->op_flags & OPf_STACKED) /* chained concats */
6547 goto ignore_optimization;
6549 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
6550 o->op_targ = o->op_next->op_targ;
6551 o->op_next->op_targ = 0;
6552 o->op_private |= OPpTARGET_MY;
6555 op_null(o->op_next);
6557 ignore_optimization:
6558 o->op_seq = PL_op_seqmax++;
6561 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
6562 o->op_seq = PL_op_seqmax++;
6563 break; /* Scalar stub must produce undef. List stub is noop */
6567 if (o->op_targ == OP_NEXTSTATE
6568 || o->op_targ == OP_DBSTATE
6569 || o->op_targ == OP_SETSTATE)
6571 PL_curcop = ((COP*)o);
6573 /* XXX: We avoid setting op_seq here to prevent later calls
6574 to peep() from mistakenly concluding that optimisation
6575 has already occurred. This doesn't fix the real problem,
6576 though (See 20010220.007). AMS 20010719 */
6577 if (oldop && o->op_next) {
6578 oldop->op_next = o->op_next;
6586 if (oldop && o->op_next) {
6587 oldop->op_next = o->op_next;
6590 o->op_seq = PL_op_seqmax++;
6595 if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) {
6596 OP* pop = (o->op_type == OP_PADAV) ?
6597 o->op_next : o->op_next->op_next;
6599 if (pop && pop->op_type == OP_CONST &&
6600 ((PL_op = pop->op_next)) &&
6601 pop->op_next->op_type == OP_AELEM &&
6602 !(pop->op_next->op_private &
6603 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) &&
6604 (i = SvIV(((SVOP*)pop)->op_sv) - PL_curcop->cop_arybase)
6609 if (cSVOPx(pop)->op_private & OPpCONST_STRICT)
6610 no_bareword_allowed(pop);
6611 if (o->op_type == OP_GV)
6612 op_null(o->op_next);
6613 op_null(pop->op_next);
6615 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
6616 o->op_next = pop->op_next->op_next;
6617 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
6618 o->op_private = (U8)i;
6619 if (o->op_type == OP_GV) {
6624 o->op_flags |= OPf_SPECIAL;
6625 o->op_type = OP_AELEMFAST;
6627 o->op_seq = PL_op_seqmax++;
6631 if (o->op_next->op_type == OP_RV2SV) {
6632 if (!(o->op_next->op_private & OPpDEREF)) {
6633 op_null(o->op_next);
6634 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
6636 o->op_next = o->op_next->op_next;
6637 o->op_type = OP_GVSV;
6638 o->op_ppaddr = PL_ppaddr[OP_GVSV];
6641 else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
6643 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
6644 /* XXX could check prototype here instead of just carping */
6645 SV *sv = sv_newmortal();
6646 gv_efullname3(sv, gv, Nullch);
6647 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
6648 "%"SVf"() called too early to check prototype",
6652 else if (o->op_next->op_type == OP_READLINE
6653 && o->op_next->op_next->op_type == OP_CONCAT
6654 && (o->op_next->op_next->op_flags & OPf_STACKED))
6656 /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */
6657 o->op_type = OP_RCATLINE;
6658 o->op_flags |= OPf_STACKED;
6659 o->op_ppaddr = PL_ppaddr[OP_RCATLINE];
6660 op_null(o->op_next->op_next);
6661 op_null(o->op_next);
6664 o->op_seq = PL_op_seqmax++;
6675 o->op_seq = PL_op_seqmax++;
6676 while (cLOGOP->op_other->op_type == OP_NULL)
6677 cLOGOP->op_other = cLOGOP->op_other->op_next;
6678 peep(cLOGOP->op_other); /* Recursive calls are not replaced by fptr calls */
6683 o->op_seq = PL_op_seqmax++;
6684 while (cLOOP->op_redoop->op_type == OP_NULL)
6685 cLOOP->op_redoop = cLOOP->op_redoop->op_next;
6686 peep(cLOOP->op_redoop);
6687 while (cLOOP->op_nextop->op_type == OP_NULL)
6688 cLOOP->op_nextop = cLOOP->op_nextop->op_next;
6689 peep(cLOOP->op_nextop);
6690 while (cLOOP->op_lastop->op_type == OP_NULL)
6691 cLOOP->op_lastop = cLOOP->op_lastop->op_next;
6692 peep(cLOOP->op_lastop);
6698 o->op_seq = PL_op_seqmax++;
6699 while (cPMOP->op_pmreplstart &&
6700 cPMOP->op_pmreplstart->op_type == OP_NULL)
6701 cPMOP->op_pmreplstart = cPMOP->op_pmreplstart->op_next;
6702 peep(cPMOP->op_pmreplstart);
6706 o->op_seq = PL_op_seqmax++;
6707 if (o->op_next && o->op_next->op_type == OP_NEXTSTATE
6708 && ckWARN(WARN_SYNTAX))
6710 if (o->op_next->op_sibling &&
6711 o->op_next->op_sibling->op_type != OP_EXIT &&
6712 o->op_next->op_sibling->op_type != OP_WARN &&
6713 o->op_next->op_sibling->op_type != OP_DIE) {
6714 const line_t oldline = CopLINE(PL_curcop);
6716 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_next));
6717 Perl_warner(aTHX_ packWARN(WARN_EXEC),
6718 "Statement unlikely to be reached");
6719 Perl_warner(aTHX_ packWARN(WARN_EXEC),
6720 "\t(Maybe you meant system() when you said exec()?)\n");
6721 CopLINE_set(PL_curcop, oldline);
6730 SV **svp, **indsvp, *sv;
6732 const char *key = NULL;
6735 o->op_seq = PL_op_seqmax++;
6737 if (((BINOP*)o)->op_last->op_type != OP_CONST)
6740 /* Make the CONST have a shared SV */
6741 svp = cSVOPx_svp(((BINOP*)o)->op_last);
6742 if ((!SvFAKE(sv = *svp) || !SvREADONLY(sv)) && !IS_PADCONST(sv)) {
6743 key = SvPV_const(sv, keylen);
6744 lexname = newSVpvn_share(key,
6745 SvUTF8(sv) ? -(I32)keylen : keylen,
6751 if ((o->op_private & (OPpLVAL_INTRO)))
6754 rop = (UNOP*)((BINOP*)o)->op_first;
6755 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6757 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6758 if (!(SvFLAGS(lexname) & SVpad_TYPED))
6760 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6761 if (!fields || !GvHV(*fields))
6763 key = SvPV_const(*svp, keylen);
6764 indsvp = hv_fetch(GvHV(*fields), key,
6765 SvUTF8(*svp) ? -(I32)keylen : keylen, FALSE);
6767 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" "
6768 "in variable %s of type %s",
6769 key, SvPV_nolen_const(lexname),
6770 HvNAME_get(SvSTASH(lexname)));
6772 ind = SvIV(*indsvp);
6774 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6775 rop->op_type = OP_RV2AV;
6776 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6777 o->op_type = OP_AELEM;
6778 o->op_ppaddr = PL_ppaddr[OP_AELEM];
6780 if (SvREADONLY(*svp))
6782 SvFLAGS(sv) |= (SvFLAGS(*svp)
6783 & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
6793 SV **svp, **indsvp, *sv;
6797 SVOP *first_key_op, *key_op;
6799 o->op_seq = PL_op_seqmax++;
6800 if ((o->op_private & (OPpLVAL_INTRO))
6801 /* I bet there's always a pushmark... */
6802 || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST)
6803 /* hmmm, no optimization if list contains only one key. */
6805 rop = (UNOP*)((LISTOP*)o)->op_last;
6806 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6808 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6809 if (!(SvFLAGS(lexname) & SVpad_TYPED))
6811 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6812 if (!fields || !GvHV(*fields))
6814 /* Again guessing that the pushmark can be jumped over.... */
6815 first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling)
6816 ->op_first->op_sibling;
6817 /* Check that the key list contains only constants. */
6818 for (key_op = first_key_op; key_op;
6819 key_op = (SVOP*)key_op->op_sibling)
6820 if (key_op->op_type != OP_CONST)
6824 rop->op_type = OP_RV2AV;
6825 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6826 o->op_type = OP_ASLICE;
6827 o->op_ppaddr = PL_ppaddr[OP_ASLICE];
6828 for (key_op = first_key_op; key_op;
6829 key_op = (SVOP*)key_op->op_sibling) {
6830 svp = cSVOPx_svp(key_op);
6831 key = SvPV_const(*svp, keylen);
6832 indsvp = hv_fetch(GvHV(*fields), key,
6833 SvUTF8(*svp) ? -(I32)keylen : keylen, FALSE);
6835 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" "
6836 "in variable %s of type %s",
6837 key, SvPV(lexname, n_a), HvNAME_get(SvSTASH(lexname)));
6839 ind = SvIV(*indsvp);
6841 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6843 if (SvREADONLY(*svp))
6845 SvFLAGS(sv) |= (SvFLAGS(*svp)
6846 & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
6854 /* will point to RV2AV or PADAV op on LHS/RHS of assign */
6858 /* check that RHS of sort is a single plain array */
6859 oright = cUNOPo->op_first;
6860 if (!oright || oright->op_type != OP_PUSHMARK)
6863 /* reverse sort ... can be optimised. */
6864 if (!cUNOPo->op_sibling) {
6865 /* Nothing follows us on the list. */
6866 OP *reverse = o->op_next;
6868 if (reverse->op_type == OP_REVERSE &&
6869 (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) {
6870 OP *pushmark = cUNOPx(reverse)->op_first;
6871 if (pushmark && (pushmark->op_type == OP_PUSHMARK)
6872 && (cUNOPx(pushmark)->op_sibling == o)) {
6873 /* reverse -> pushmark -> sort */
6874 o->op_private |= OPpSORT_REVERSE;
6876 pushmark->op_next = oright->op_next;
6882 /* make @a = sort @a act in-place */
6884 o->op_seq = PL_op_seqmax++;
6886 oright = cUNOPx(oright)->op_sibling;
6889 if (oright->op_type == OP_NULL) { /* skip sort block/sub */
6890 oright = cUNOPx(oright)->op_sibling;
6894 (oright->op_type != OP_RV2AV && oright->op_type != OP_PADAV)
6895 || oright->op_next != o
6896 || (oright->op_private & OPpLVAL_INTRO)
6900 /* o2 follows the chain of op_nexts through the LHS of the
6901 * assign (if any) to the aassign op itself */
6903 if (!o2 || o2->op_type != OP_NULL)
6906 if (!o2 || o2->op_type != OP_PUSHMARK)
6909 if (o2 && o2->op_type == OP_GV)
6912 || (o2->op_type != OP_PADAV && o2->op_type != OP_RV2AV)
6913 || (o2->op_private & OPpLVAL_INTRO)
6918 if (!o2 || o2->op_type != OP_NULL)
6921 if (!o2 || o2->op_type != OP_AASSIGN
6922 || (o2->op_flags & OPf_WANT) != OPf_WANT_VOID)
6925 /* check that the sort is the first arg on RHS of assign */
6927 o2 = cUNOPx(o2)->op_first;
6928 if (!o2 || o2->op_type != OP_NULL)
6930 o2 = cUNOPx(o2)->op_first;
6931 if (!o2 || o2->op_type != OP_PUSHMARK)
6933 if (o2->op_sibling != o)
6936 /* check the array is the same on both sides */
6937 if (oleft->op_type == OP_RV2AV) {
6938 if (oright->op_type != OP_RV2AV
6939 || !cUNOPx(oright)->op_first
6940 || cUNOPx(oright)->op_first->op_type != OP_GV
6941 || cGVOPx_gv(cUNOPx(oleft)->op_first) !=
6942 cGVOPx_gv(cUNOPx(oright)->op_first)
6946 else if (oright->op_type != OP_PADAV
6947 || oright->op_targ != oleft->op_targ
6951 /* transfer MODishness etc from LHS arg to RHS arg */
6952 oright->op_flags = oleft->op_flags;
6953 o->op_private |= OPpSORT_INPLACE;
6955 /* excise push->gv->rv2av->null->aassign */
6956 o2 = o->op_next->op_next;
6957 op_null(o2); /* PUSHMARK */
6959 if (o2->op_type == OP_GV) {
6960 op_null(o2); /* GV */
6963 op_null(o2); /* RV2AV or PADAV */
6964 o2 = o2->op_next->op_next;
6965 op_null(o2); /* AASSIGN */
6967 o->op_next = o2->op_next;
6973 OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av;
6975 LISTOP *enter, *exlist;
6976 o->op_seq = PL_op_seqmax++;
6978 enter = (LISTOP *) o->op_next;
6981 if (enter->op_type == OP_NULL) {
6982 enter = (LISTOP *) enter->op_next;
6986 /* for $a (...) will have OP_GV then OP_RV2GV here.
6987 for (...) just has an OP_GV. */
6988 if (enter->op_type == OP_GV) {
6989 gvop = (OP *) enter;
6990 enter = (LISTOP *) enter->op_next;
6993 if (enter->op_type == OP_RV2GV) {
6994 enter = (LISTOP *) enter->op_next;
7000 if (enter->op_type != OP_ENTERITER)
7003 iter = enter->op_next;
7004 if (!iter || iter->op_type != OP_ITER)
7007 expushmark = enter->op_first;
7008 if (!expushmark || expushmark->op_type != OP_NULL
7009 || expushmark->op_targ != OP_PUSHMARK)
7012 exlist = (LISTOP *) expushmark->op_sibling;
7013 if (!exlist || exlist->op_type != OP_NULL
7014 || exlist->op_targ != OP_LIST)
7017 if (exlist->op_last != o) {
7018 /* Mmm. Was expecting to point back to this op. */
7021 theirmark = exlist->op_first;
7022 if (!theirmark || theirmark->op_type != OP_PUSHMARK)
7025 if (theirmark->op_sibling != o) {
7026 /* There's something between the mark and the reverse, eg
7027 for (1, reverse (...))
7032 ourmark = ((LISTOP *)o)->op_first;
7033 if (!ourmark || ourmark->op_type != OP_PUSHMARK)
7036 ourlast = ((LISTOP *)o)->op_last;
7037 if (!ourlast || ourlast->op_next != o)
7040 rv2av = ourmark->op_sibling;
7041 if (rv2av && rv2av->op_type == OP_RV2AV && rv2av->op_sibling == 0
7042 && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)
7043 && enter->op_flags == (OPf_WANT_LIST | OPf_KIDS)) {
7044 /* We're just reversing a single array. */
7045 rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF;
7046 enter->op_flags |= OPf_STACKED;
7049 /* We don't have control over who points to theirmark, so sacrifice
7051 theirmark->op_next = ourmark->op_next;
7052 theirmark->op_flags = ourmark->op_flags;
7053 ourlast->op_next = gvop ? gvop : (OP *) enter;
7056 enter->op_private |= OPpITER_REVERSED;
7057 iter->op_private |= OPpITER_REVERSED;
7063 o->op_seq = PL_op_seqmax++;
7072 Perl_custom_op_name(pTHX_ OP* o)
7074 const IV index = PTR2IV(o->op_ppaddr);
7078 if (!PL_custom_op_names) /* This probably shouldn't happen */
7079 return (char *)PL_op_name[OP_CUSTOM];
7081 keysv = sv_2mortal(newSViv(index));
7083 he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0);
7085 return (char *)PL_op_name[OP_CUSTOM]; /* Don't know who you are */
7087 return SvPV_nolen(HeVAL(he));
7091 Perl_custom_op_desc(pTHX_ OP* o)
7093 const IV index = PTR2IV(o->op_ppaddr);
7097 if (!PL_custom_op_descs)
7098 return (char *)PL_op_desc[OP_CUSTOM];
7100 keysv = sv_2mortal(newSViv(index));
7102 he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0);
7104 return (char *)PL_op_desc[OP_CUSTOM];
7106 return SvPV_nolen(HeVAL(he));
7111 /* Efficient sub that returns a constant scalar value. */
7113 const_sv_xsub(pTHX_ CV* cv)
7118 Perl_croak(aTHX_ "usage: %s::%s()",
7119 HvNAME_get(GvSTASH(CvGV(cv))), GvNAME(CvGV(cv)));
7123 ST(0) = (SV*)XSANY.any_ptr;
7129 * c-indentation-style: bsd
7131 * indent-tabs-mode: t
7134 * ex: set ts=8 sts=4 sw=4 noet: