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 **ptr = (I32 **) op;
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)
165 SV* tmpsv = sv_newmortal();
166 gv_efullname3(tmpsv, gv, Nullch);
167 return SvPV(tmpsv,n_a);
171 S_no_fh_allowed(pTHX_ OP *o)
173 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
179 S_too_few_arguments(pTHX_ OP *o, const char *name)
181 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
186 S_too_many_arguments(pTHX_ OP *o, const char *name)
188 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
193 S_bad_type(pTHX_ I32 n, const char *t, const char *name, const OP *kid)
195 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
196 (int)n, name, t, OP_DESC(kid)));
200 S_no_bareword_allowed(pTHX_ const OP *o)
202 qerror(Perl_mess(aTHX_
203 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
207 /* "register" allocation */
210 Perl_allocmy(pTHX_ char *name)
214 /* complain about "my $_" etc etc */
215 if (!(PL_in_my == KEY_our ||
217 (USE_UTF8_IN_NAMES && UTF8_IS_START(name[1])) ||
218 (name[1] == '_' && (int)strlen(name) > 2)))
220 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
221 /* 1999-02-27 mjd@plover.com */
223 p = strchr(name, '\0');
224 /* The next block assumes the buffer is at least 205 chars
225 long. At present, it's always at least 256 chars. */
227 strcpy(name+200, "...");
233 /* Move everything else down one character */
234 for (; p-name > 2; p--)
236 name[2] = toCTRL(name[1]);
239 yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
241 /* check for duplicate declaration */
243 (bool)(PL_in_my == KEY_our),
244 (PL_curstash ? PL_curstash : PL_defstash)
247 if (PL_in_my_stash && *name != '$') {
248 yyerror(Perl_form(aTHX_
249 "Can't declare class for non-scalar %s in \"%s\"",
250 name, PL_in_my == KEY_our ? "our" : "my"));
253 /* allocate a spare slot and store the name in that slot */
255 off = pad_add_name(name,
258 ? (PL_curstash ? PL_curstash : PL_defstash)
267 #ifdef USE_5005THREADS
268 /* find_threadsv is not reentrant */
270 Perl_find_threadsv(pTHX_ const char *name)
275 /* We currently only handle names of a single character */
276 p = strchr(PL_threadsv_names, *name);
279 key = p - PL_threadsv_names;
280 MUTEX_LOCK(&thr->mutex);
281 svp = av_fetch(thr->threadsv, key, FALSE);
283 MUTEX_UNLOCK(&thr->mutex);
285 SV *sv = NEWSV(0, 0);
286 av_store(thr->threadsv, key, sv);
287 thr->threadsvp = AvARRAY(thr->threadsv);
288 MUTEX_UNLOCK(&thr->mutex);
290 * Some magic variables used to be automagically initialised
291 * in gv_fetchpv. Those which are now per-thread magicals get
292 * initialised here instead.
298 sv_setpv(sv, "\034");
299 sv_magic(sv, 0, PERL_MAGIC_sv, name, 1);
304 PL_sawampersand = TRUE;
318 /* XXX %! tied to Errno.pm needs to be added here.
319 * See gv_fetchpv(). */
323 sv_magic(sv, 0, PERL_MAGIC_sv, name, 1);
325 DEBUG_S(PerlIO_printf(Perl_error_log,
326 "find_threadsv: new SV %p for $%s%c\n",
327 sv, (*name < 32) ? "^" : "",
328 (*name < 32) ? toCTRL(*name) : *name));
332 #endif /* USE_5005THREADS */
337 Perl_op_free(pTHX_ OP *o)
342 if (!o || o->op_seq == (U16)-1)
345 if (o->op_private & OPpREFCOUNTED) {
346 switch (o->op_type) {
354 refcnt = OpREFCNT_dec(o);
364 if (o->op_flags & OPf_KIDS) {
365 register OP *kid, *nextkid;
366 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
367 nextkid = kid->op_sibling; /* Get before next freeing kid */
373 type = (OPCODE)o->op_targ;
375 /* COP* is not cleared by op_clear() so that we may track line
376 * numbers etc even after null() */
377 if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
385 Perl_op_clear(pTHX_ OP *o)
388 switch (o->op_type) {
389 case OP_NULL: /* Was holding old type, if any. */
390 case OP_ENTEREVAL: /* Was holding hints. */
391 #ifdef USE_5005THREADS
392 case OP_THREADSV: /* Was holding index into thr->threadsv AV. */
396 #ifdef USE_5005THREADS
398 if (!(o->op_flags & OPf_SPECIAL))
401 #endif /* USE_5005THREADS */
403 if (!(o->op_flags & OPf_REF)
404 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
410 if (! (o->op_type == OP_AELEMFAST && o->op_flags & OPf_SPECIAL)) {
411 /* not an OP_PADAV replacement */
413 if (cPADOPo->op_padix > 0) {
414 /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
415 * may still exist on the pad */
416 pad_swipe(cPADOPo->op_padix, TRUE);
417 cPADOPo->op_padix = 0;
420 SvREFCNT_dec(cSVOPo->op_sv);
421 cSVOPo->op_sv = Nullsv;
425 case OP_METHOD_NAMED:
427 SvREFCNT_dec(cSVOPo->op_sv);
428 cSVOPo->op_sv = Nullsv;
431 Even if op_clear does a pad_free for the target of the op,
432 pad_free doesn't actually remove the sv that exists in the pad;
433 instead it lives on. This results in that it could be reused as
434 a target later on when the pad was reallocated.
437 pad_swipe(o->op_targ,1);
446 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
450 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
451 SvREFCNT_dec(cSVOPo->op_sv);
452 cSVOPo->op_sv = Nullsv;
455 Safefree(cPVOPo->op_pv);
456 cPVOPo->op_pv = Nullch;
460 op_free(cPMOPo->op_pmreplroot);
464 if (INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot)) {
465 /* No GvIN_PAD_off here, because other references may still
466 * exist on the pad */
467 pad_swipe(INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot), TRUE);
470 SvREFCNT_dec((SV*)cPMOPo->op_pmreplroot);
477 HV *pmstash = PmopSTASH(cPMOPo);
478 if (pmstash && SvREFCNT(pmstash)) {
479 PMOP *pmop = HvPMROOT(pmstash);
480 PMOP *lastpmop = NULL;
482 if (cPMOPo == pmop) {
484 lastpmop->op_pmnext = pmop->op_pmnext;
486 HvPMROOT(pmstash) = pmop->op_pmnext;
490 pmop = pmop->op_pmnext;
493 PmopSTASH_free(cPMOPo);
495 cPMOPo->op_pmreplroot = Nullop;
496 /* we use the "SAFE" version of the PM_ macros here
497 * since sv_clean_all might release some PMOPs
498 * after PL_regex_padav has been cleared
499 * and the clearing of PL_regex_padav needs to
500 * happen before sv_clean_all
502 ReREFCNT_dec(PM_GETRE_SAFE(cPMOPo));
503 PM_SETRE_SAFE(cPMOPo, (REGEXP*)NULL);
505 if(PL_regex_pad) { /* We could be in destruction */
506 av_push((AV*) PL_regex_pad[0],(SV*) PL_regex_pad[(cPMOPo)->op_pmoffset]);
507 SvREPADTMP_on(PL_regex_pad[(cPMOPo)->op_pmoffset]);
508 PM_SETRE(cPMOPo, (cPMOPo)->op_pmoffset);
515 if (o->op_targ > 0) {
516 pad_free(o->op_targ);
522 S_cop_free(pTHX_ COP* cop)
524 Safefree(cop->cop_label); /* FIXME: treaddead ??? */
527 if (! specialWARN(cop->cop_warnings))
528 SvREFCNT_dec(cop->cop_warnings);
529 if (! specialCopIO(cop->cop_io)) {
533 char *s = SvPV(cop->cop_io,len);
534 Perl_warn(aTHX_ "io='%.*s'",(int) len,s); /* ??? --jhi */
537 SvREFCNT_dec(cop->cop_io);
543 Perl_op_null(pTHX_ OP *o)
545 if (o->op_type == OP_NULL)
548 o->op_targ = o->op_type;
549 o->op_type = OP_NULL;
550 o->op_ppaddr = PL_ppaddr[OP_NULL];
554 Perl_op_refcnt_lock(pTHX)
560 Perl_op_refcnt_unlock(pTHX)
565 /* Contextualizers */
567 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
570 Perl_linklist(pTHX_ OP *o)
576 /* establish postfix order */
577 if (cUNOPo->op_first) {
579 o->op_next = LINKLIST(cUNOPo->op_first);
580 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
582 kid->op_next = LINKLIST(kid->op_sibling);
594 Perl_scalarkids(pTHX_ OP *o)
596 if (o && o->op_flags & OPf_KIDS) {
598 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
605 S_scalarboolean(pTHX_ OP *o)
607 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
608 if (ckWARN(WARN_SYNTAX)) {
609 const line_t oldline = CopLINE(PL_curcop);
611 if (PL_copline != NOLINE)
612 CopLINE_set(PL_curcop, PL_copline);
613 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
614 CopLINE_set(PL_curcop, oldline);
621 Perl_scalar(pTHX_ OP *o)
625 /* assumes no premature commitment */
626 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
627 || o->op_type == OP_RETURN)
632 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
634 switch (o->op_type) {
636 scalar(cBINOPo->op_first);
641 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
645 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
646 if (!kPMOP->op_pmreplroot)
647 deprecate_old("implicit split to @_");
655 if (o->op_flags & OPf_KIDS) {
656 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
662 kid = cLISTOPo->op_first;
664 while ((kid = kid->op_sibling)) {
670 WITH_THR(PL_curcop = &PL_compiling);
675 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
681 WITH_THR(PL_curcop = &PL_compiling);
684 if (ckWARN(WARN_VOID))
685 Perl_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
691 Perl_scalarvoid(pTHX_ OP *o)
694 const char* useless = 0;
698 if (o->op_type == OP_NEXTSTATE
699 || o->op_type == OP_SETSTATE
700 || o->op_type == OP_DBSTATE
701 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
702 || o->op_targ == OP_SETSTATE
703 || o->op_targ == OP_DBSTATE)))
704 PL_curcop = (COP*)o; /* for warning below */
706 /* assumes no premature commitment */
707 want = o->op_flags & OPf_WANT;
708 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
709 || o->op_type == OP_RETURN)
714 if ((o->op_private & OPpTARGET_MY)
715 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
717 return scalar(o); /* As if inside SASSIGN */
720 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
722 switch (o->op_type) {
724 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
728 if (o->op_flags & OPf_STACKED)
732 if (o->op_private == 4)
804 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
805 useless = OP_DESC(o);
812 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
813 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
814 useless = "a variable";
819 if (cSVOPo->op_private & OPpCONST_STRICT)
820 no_bareword_allowed(o);
822 if (ckWARN(WARN_VOID)) {
823 useless = "a constant";
824 /* don't warn on optimised away booleans, eg
825 * use constant Foo, 5; Foo || print; */
826 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
828 /* the constants 0 and 1 are permitted as they are
829 conventionally used as dummies in constructs like
830 1 while some_condition_with_side_effects; */
831 else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
833 else if (SvPOK(sv)) {
834 /* perl4's way of mixing documentation and code
835 (before the invention of POD) was based on a
836 trick to mix nroff and perl code. The trick was
837 built upon these three nroff macros being used in
838 void context. The pink camel has the details in
839 the script wrapman near page 319. */
840 if (strnEQ(SvPVX_const(sv), "di", 2) ||
841 strnEQ(SvPVX_const(sv), "ds", 2) ||
842 strnEQ(SvPVX_const(sv), "ig", 2))
847 op_null(o); /* don't execute or even remember it */
851 o->op_type = OP_PREINC; /* pre-increment is faster */
852 o->op_ppaddr = PL_ppaddr[OP_PREINC];
856 o->op_type = OP_PREDEC; /* pre-decrement is faster */
857 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
863 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
868 if (o->op_flags & OPf_STACKED)
875 if (!(o->op_flags & OPf_KIDS))
884 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
891 /* all requires must return a boolean value */
892 o->op_flags &= ~OPf_WANT;
897 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
898 if (!kPMOP->op_pmreplroot)
899 deprecate_old("implicit split to @_");
903 if (useless && ckWARN(WARN_VOID))
904 Perl_warner(aTHX_ packWARN(WARN_VOID), "Useless use of %s in void context", useless);
909 Perl_listkids(pTHX_ OP *o)
911 if (o && o->op_flags & OPf_KIDS) {
913 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
920 Perl_list(pTHX_ OP *o)
924 /* assumes no premature commitment */
925 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
926 || o->op_type == OP_RETURN)
931 if ((o->op_private & OPpTARGET_MY)
932 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
934 return o; /* As if inside SASSIGN */
937 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
939 switch (o->op_type) {
942 list(cBINOPo->op_first);
947 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
955 if (!(o->op_flags & OPf_KIDS))
957 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
958 list(cBINOPo->op_first);
959 return gen_constant_list(o);
966 kid = cLISTOPo->op_first;
968 while ((kid = kid->op_sibling)) {
974 WITH_THR(PL_curcop = &PL_compiling);
978 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
984 WITH_THR(PL_curcop = &PL_compiling);
987 /* all requires must return a boolean value */
988 o->op_flags &= ~OPf_WANT;
995 Perl_scalarseq(pTHX_ OP *o)
998 if (o->op_type == OP_LINESEQ ||
999 o->op_type == OP_SCOPE ||
1000 o->op_type == OP_LEAVE ||
1001 o->op_type == OP_LEAVETRY)
1004 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1005 if (kid->op_sibling) {
1009 PL_curcop = &PL_compiling;
1011 o->op_flags &= ~OPf_PARENS;
1012 if (PL_hints & HINT_BLOCK_SCOPE)
1013 o->op_flags |= OPf_PARENS;
1016 o = newOP(OP_STUB, 0);
1021 S_modkids(pTHX_ OP *o, I32 type)
1023 if (o && o->op_flags & OPf_KIDS) {
1025 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1032 Perl_mod(pTHX_ OP *o, I32 type)
1036 if (!o || PL_error_count)
1039 if ((o->op_private & OPpTARGET_MY)
1040 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1045 switch (o->op_type) {
1050 if (!(o->op_private & (OPpCONST_ARYBASE)))
1052 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1053 PL_compiling.cop_arybase = (I32)SvIV(cSVOPx(PL_eval_start)->op_sv);
1057 SAVEI32(PL_compiling.cop_arybase);
1058 PL_compiling.cop_arybase = 0;
1060 else if (type == OP_REFGEN)
1063 Perl_croak(aTHX_ "That use of $[ is unsupported");
1066 if (o->op_flags & OPf_PARENS)
1070 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1071 !(o->op_flags & OPf_STACKED)) {
1072 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1073 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1074 assert(cUNOPo->op_first->op_type == OP_NULL);
1075 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1078 else if (o->op_private & OPpENTERSUB_NOMOD)
1080 else { /* lvalue subroutine call */
1081 o->op_private |= OPpLVAL_INTRO;
1082 PL_modcount = RETURN_UNLIMITED_NUMBER;
1083 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
1084 /* Backward compatibility mode: */
1085 o->op_private |= OPpENTERSUB_INARGS;
1088 else { /* Compile-time error message: */
1089 OP *kid = cUNOPo->op_first;
1093 if (kid->op_type == OP_PUSHMARK)
1095 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1097 "panic: unexpected lvalue entersub "
1098 "args: type/targ %ld:%"UVuf,
1099 (long)kid->op_type, (UV)kid->op_targ);
1100 kid = kLISTOP->op_first;
1102 while (kid->op_sibling)
1103 kid = kid->op_sibling;
1104 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1106 if (kid->op_type == OP_METHOD_NAMED
1107 || kid->op_type == OP_METHOD)
1111 NewOp(1101, newop, 1, UNOP);
1112 newop->op_type = OP_RV2CV;
1113 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1114 newop->op_first = Nullop;
1115 newop->op_next = (OP*)newop;
1116 kid->op_sibling = (OP*)newop;
1117 newop->op_private |= OPpLVAL_INTRO;
1121 if (kid->op_type != OP_RV2CV)
1123 "panic: unexpected lvalue entersub "
1124 "entry via type/targ %ld:%"UVuf,
1125 (long)kid->op_type, (UV)kid->op_targ);
1126 kid->op_private |= OPpLVAL_INTRO;
1127 break; /* Postpone until runtime */
1131 kid = kUNOP->op_first;
1132 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1133 kid = kUNOP->op_first;
1134 if (kid->op_type == OP_NULL)
1136 "Unexpected constant lvalue entersub "
1137 "entry via type/targ %ld:%"UVuf,
1138 (long)kid->op_type, (UV)kid->op_targ);
1139 if (kid->op_type != OP_GV) {
1140 /* Restore RV2CV to check lvalueness */
1142 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1143 okid->op_next = kid->op_next;
1144 kid->op_next = okid;
1147 okid->op_next = Nullop;
1148 okid->op_type = OP_RV2CV;
1150 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1151 okid->op_private |= OPpLVAL_INTRO;
1155 cv = GvCV(kGVOP_gv);
1165 /* grep, foreach, subcalls, refgen */
1166 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1168 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1169 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1171 : (o->op_type == OP_ENTERSUB
1172 ? "non-lvalue subroutine call"
1174 type ? PL_op_desc[type] : "local"));
1188 case OP_RIGHT_SHIFT:
1197 if (!(o->op_flags & OPf_STACKED))
1203 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1209 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1210 PL_modcount = RETURN_UNLIMITED_NUMBER;
1211 return o; /* Treat \(@foo) like ordinary list. */
1215 if (scalar_mod_type(o, type))
1217 ref(cUNOPo->op_first, o->op_type);
1221 if (type == OP_LEAVESUBLV)
1222 o->op_private |= OPpMAYBE_LVSUB;
1227 PL_modcount = RETURN_UNLIMITED_NUMBER;
1230 ref(cUNOPo->op_first, o->op_type);
1234 PL_hints |= HINT_BLOCK_SCOPE;
1239 /* Needed if maint gets patch 19588
1247 PL_modcount = RETURN_UNLIMITED_NUMBER;
1248 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1249 return o; /* Treat \(@foo) like ordinary list. */
1250 if (scalar_mod_type(o, type))
1252 if (type == OP_LEAVESUBLV)
1253 o->op_private |= OPpMAYBE_LVSUB;
1258 { /* XXX DAPM 2002.08.25 tmp assert test */
1259 /* XXX */ assert(av_fetch(PL_comppad_name, (o->op_targ), FALSE));
1260 /* XXX */ assert(*av_fetch(PL_comppad_name, (o->op_targ), FALSE));
1262 Perl_croak(aTHX_ "Can't localize lexical variable %s",
1263 PAD_COMPNAME_PV(o->op_targ));
1267 #ifdef USE_5005THREADS
1269 PL_modcount++; /* XXX ??? */
1271 #endif /* USE_5005THREADS */
1277 if (type != OP_SASSIGN)
1281 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1286 if (type == OP_LEAVESUBLV)
1287 o->op_private |= OPpMAYBE_LVSUB;
1289 pad_free(o->op_targ);
1290 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1291 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1292 if (o->op_flags & OPf_KIDS)
1293 mod(cBINOPo->op_first->op_sibling, type);
1298 ref(cBINOPo->op_first, o->op_type);
1299 if (type == OP_ENTERSUB &&
1300 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1301 o->op_private |= OPpLVAL_DEFER;
1302 if (type == OP_LEAVESUBLV)
1303 o->op_private |= OPpMAYBE_LVSUB;
1311 if (o->op_flags & OPf_KIDS)
1312 mod(cLISTOPo->op_last, type);
1316 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1318 else if (!(o->op_flags & OPf_KIDS))
1320 if (o->op_targ != OP_LIST) {
1321 mod(cBINOPo->op_first, type);
1326 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1331 if (type != OP_LEAVESUBLV)
1333 break; /* mod()ing was handled by ck_return() */
1336 /* [20011101.069] File test operators interpret OPf_REF to mean that
1337 their argument is a filehandle; thus \stat(".") should not set
1339 if (type == OP_REFGEN &&
1340 PL_check[o->op_type] == MEMBER_TO_FPTR(Perl_ck_ftst))
1343 if (type != OP_LEAVESUBLV)
1344 o->op_flags |= OPf_MOD;
1346 if (type == OP_AASSIGN || type == OP_SASSIGN)
1347 o->op_flags |= OPf_SPECIAL|OPf_REF;
1349 o->op_private |= OPpLVAL_INTRO;
1350 o->op_flags &= ~OPf_SPECIAL;
1351 PL_hints |= HINT_BLOCK_SCOPE;
1353 else if (type != OP_GREPSTART && type != OP_ENTERSUB
1354 && type != OP_LEAVESUBLV)
1355 o->op_flags |= OPf_REF;
1360 S_scalar_mod_type(pTHX_ const OP *o, I32 type)
1364 if (o->op_type == OP_RV2GV)
1388 case OP_RIGHT_SHIFT:
1407 S_is_handle_constructor(pTHX_ const OP *o, I32 numargs)
1409 switch (o->op_type) {
1417 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1430 Perl_refkids(pTHX_ OP *o, I32 type)
1432 if (o && o->op_flags & OPf_KIDS) {
1434 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1441 Perl_ref(pTHX_ OP *o, I32 type)
1445 if (!o || PL_error_count)
1448 switch (o->op_type) {
1450 if ((type == OP_EXISTS || type == OP_DEFINED || type == OP_LOCK) &&
1451 !(o->op_flags & OPf_STACKED)) {
1452 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1453 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1454 assert(cUNOPo->op_first->op_type == OP_NULL);
1455 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1456 o->op_flags |= OPf_SPECIAL;
1461 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1465 if (type == OP_DEFINED)
1466 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1467 ref(cUNOPo->op_first, o->op_type);
1470 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1471 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1472 : type == OP_RV2HV ? OPpDEREF_HV
1474 o->op_flags |= OPf_MOD;
1479 o->op_flags |= OPf_MOD; /* XXX ??? */
1484 o->op_flags |= OPf_REF;
1487 if (type == OP_DEFINED)
1488 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1489 ref(cUNOPo->op_first, o->op_type);
1494 o->op_flags |= OPf_REF;
1499 if (!(o->op_flags & OPf_KIDS))
1501 ref(cBINOPo->op_first, type);
1505 ref(cBINOPo->op_first, o->op_type);
1506 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1507 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1508 : type == OP_RV2HV ? OPpDEREF_HV
1510 o->op_flags |= OPf_MOD;
1518 if (!(o->op_flags & OPf_KIDS))
1520 ref(cLISTOPo->op_last, type);
1530 S_dup_attrlist(pTHX_ OP *o)
1534 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1535 * where the first kid is OP_PUSHMARK and the remaining ones
1536 * are OP_CONST. We need to push the OP_CONST values.
1538 if (o->op_type == OP_CONST)
1539 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc(cSVOPo->op_sv));
1541 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1542 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1543 if (o->op_type == OP_CONST)
1544 rop = append_elem(OP_LIST, rop,
1545 newSVOP(OP_CONST, o->op_flags,
1546 SvREFCNT_inc(cSVOPo->op_sv)));
1553 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs, bool for_my)
1557 /* fake up C<use attributes $pkg,$rv,@attrs> */
1558 ENTER; /* need to protect against side-effects of 'use' */
1561 stashsv = newSVpv(HvNAME_get(stash), 0);
1563 stashsv = &PL_sv_no;
1565 #define ATTRSMODULE "attributes"
1566 #define ATTRSMODULE_PM "attributes.pm"
1569 /* Don't force the C<use> if we don't need it. */
1570 SV **svp = hv_fetch(GvHVn(PL_incgv), ATTRSMODULE_PM,
1571 sizeof(ATTRSMODULE_PM)-1, 0);
1572 if (svp && *svp != &PL_sv_undef)
1573 ; /* already in %INC */
1575 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
1576 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1580 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1581 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1583 prepend_elem(OP_LIST,
1584 newSVOP(OP_CONST, 0, stashsv),
1585 prepend_elem(OP_LIST,
1586 newSVOP(OP_CONST, 0,
1588 dup_attrlist(attrs))));
1594 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
1596 OP *pack, *imop, *arg;
1602 assert(target->op_type == OP_PADSV ||
1603 target->op_type == OP_PADHV ||
1604 target->op_type == OP_PADAV);
1606 /* Ensure that attributes.pm is loaded. */
1607 apply_attrs(stash, PAD_SV(target->op_targ), attrs, TRUE);
1609 /* Need package name for method call. */
1610 pack = newSVOP(OP_CONST, 0, newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1));
1612 /* Build up the real arg-list. */
1614 stashsv = newSVpv(HvNAME_get(stash), 0);
1616 stashsv = &PL_sv_no;
1617 arg = newOP(OP_PADSV, 0);
1618 arg->op_targ = target->op_targ;
1619 arg = prepend_elem(OP_LIST,
1620 newSVOP(OP_CONST, 0, stashsv),
1621 prepend_elem(OP_LIST,
1622 newUNOP(OP_REFGEN, 0,
1623 mod(arg, OP_REFGEN)),
1624 dup_attrlist(attrs)));
1626 /* Fake up a method call to import */
1627 meth = newSVpvn("import", 6);
1628 (void)SvUPGRADE(meth, SVt_PVIV);
1629 (void)SvIOK_on(meth);
1632 PERL_HASH(hash, SvPVX_const(meth), SvCUR(meth));
1633 SvUV_set(meth, hash);
1635 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
1636 append_elem(OP_LIST,
1637 prepend_elem(OP_LIST, pack, list(arg)),
1638 newSVOP(OP_METHOD_NAMED, 0, meth)));
1639 imop->op_private |= OPpENTERSUB_NOMOD;
1641 /* Combine the ops. */
1642 *imopsp = append_elem(OP_LIST, *imopsp, imop);
1646 =notfor apidoc apply_attrs_string
1648 Attempts to apply a list of attributes specified by the C<attrstr> and
1649 C<len> arguments to the subroutine identified by the C<cv> argument which
1650 is expected to be associated with the package identified by the C<stashpv>
1651 argument (see L<attributes>). It gets this wrong, though, in that it
1652 does not correctly identify the boundaries of the individual attribute
1653 specifications within C<attrstr>. This is not really intended for the
1654 public API, but has to be listed here for systems such as AIX which
1655 need an explicit export list for symbols. (It's called from XS code
1656 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
1657 to respect attribute syntax properly would be welcome.
1663 Perl_apply_attrs_string(pTHX_ char *stashpv, CV *cv,
1664 char *attrstr, STRLEN len)
1669 len = strlen(attrstr);
1673 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
1675 const char *sstr = attrstr;
1676 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
1677 attrs = append_elem(OP_LIST, attrs,
1678 newSVOP(OP_CONST, 0,
1679 newSVpvn(sstr, attrstr-sstr)));
1683 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1684 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1685 Nullsv, prepend_elem(OP_LIST,
1686 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
1687 prepend_elem(OP_LIST,
1688 newSVOP(OP_CONST, 0,
1694 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
1698 if (!o || PL_error_count)
1702 if (type == OP_LIST) {
1704 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1705 my_kid(kid, attrs, imopsp);
1706 } else if (type == OP_UNDEF) {
1708 } else if (type == OP_RV2SV || /* "our" declaration */
1710 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
1711 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
1712 yyerror(Perl_form(aTHX_ "Can't declare %s in %s",
1713 OP_DESC(o), PL_in_my == KEY_our ? "our" : "my"));
1715 GV *gv = cGVOPx_gv(cUNOPo->op_first);
1717 PL_in_my_stash = Nullhv;
1718 apply_attrs(GvSTASH(gv),
1719 (type == OP_RV2SV ? GvSV(gv) :
1720 type == OP_RV2AV ? (SV*)GvAV(gv) :
1721 type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)gv),
1724 o->op_private |= OPpOUR_INTRO;
1727 else if (type != OP_PADSV &&
1730 type != OP_PUSHMARK)
1732 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
1734 PL_in_my == KEY_our ? "our" : "my"));
1737 else if (attrs && type != OP_PUSHMARK) {
1741 PL_in_my_stash = Nullhv;
1743 /* check for C<my Dog $spot> when deciding package */
1744 stash = PAD_COMPNAME_TYPE(o->op_targ);
1746 stash = PL_curstash;
1747 apply_attrs_my(stash, o, attrs, imopsp);
1749 o->op_flags |= OPf_MOD;
1750 o->op_private |= OPpLVAL_INTRO;
1755 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1758 int maybe_scalar = 0;
1760 /* [perl #17376]: this appears to be premature, and results in code such as
1761 C< our(%x); > executing in list mode rather than void mode */
1763 if (o->op_flags & OPf_PARENS)
1772 o = my_kid(o, attrs, &rops);
1774 if (maybe_scalar && o->op_type == OP_PADSV) {
1775 o = scalar(append_list(OP_LIST, (LISTOP*)rops, (LISTOP*)o));
1776 o->op_private |= OPpLVAL_INTRO;
1779 o = append_list(OP_LIST, (LISTOP*)o, (LISTOP*)rops);
1782 PL_in_my_stash = Nullhv;
1787 Perl_my(pTHX_ OP *o)
1789 return my_attrs(o, Nullop);
1793 Perl_sawparens(pTHX_ OP *o)
1796 o->op_flags |= OPf_PARENS;
1801 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
1805 if (ckWARN(WARN_MISC) &&
1806 (left->op_type == OP_RV2AV ||
1807 left->op_type == OP_RV2HV ||
1808 left->op_type == OP_PADAV ||
1809 left->op_type == OP_PADHV)) {
1810 const char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
1811 right->op_type == OP_TRANS)
1812 ? right->op_type : OP_MATCH];
1813 const char *sample = ((left->op_type == OP_RV2AV ||
1814 left->op_type == OP_PADAV)
1815 ? "@array" : "%hash");
1816 Perl_warner(aTHX_ packWARN(WARN_MISC),
1817 "Applying %s to %s will act on scalar(%s)",
1818 desc, sample, sample);
1821 if (right->op_type == OP_CONST &&
1822 cSVOPx(right)->op_private & OPpCONST_BARE &&
1823 cSVOPx(right)->op_private & OPpCONST_STRICT)
1825 no_bareword_allowed(right);
1828 if (!(right->op_flags & OPf_STACKED) &&
1829 (right->op_type == OP_MATCH ||
1830 right->op_type == OP_SUBST ||
1831 right->op_type == OP_TRANS)) {
1832 right->op_flags |= OPf_STACKED;
1833 if (right->op_type != OP_MATCH &&
1834 ! (right->op_type == OP_TRANS &&
1835 right->op_private & OPpTRANS_IDENTICAL))
1836 left = mod(left, right->op_type);
1837 if (right->op_type == OP_TRANS)
1838 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1840 o = prepend_elem(right->op_type, scalar(left), right);
1842 return newUNOP(OP_NOT, 0, scalar(o));
1846 return bind_match(type, left,
1847 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1851 Perl_invert(pTHX_ OP *o)
1855 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1856 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1860 Perl_scope(pTHX_ OP *o)
1863 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
1864 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1865 o->op_type = OP_LEAVE;
1866 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
1868 else if (o->op_type == OP_LINESEQ) {
1870 o->op_type = OP_SCOPE;
1871 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
1872 kid = ((LISTOP*)o)->op_first;
1873 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
1877 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1882 /* XXX kept for BINCOMPAT only */
1884 Perl_save_hints(pTHX)
1886 Perl_croak(aTHX_ "internal error: obsolete function save_hints() called");
1890 Perl_block_start(pTHX_ int full)
1892 const int retval = PL_savestack_ix;
1893 /* If there were syntax errors, don't try to start a block */
1894 if (PL_yynerrs) return retval;
1896 pad_block_start(full);
1898 PL_hints &= ~HINT_BLOCK_SCOPE;
1899 SAVESPTR(PL_compiling.cop_warnings);
1900 if (! specialWARN(PL_compiling.cop_warnings)) {
1901 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1902 SAVEFREESV(PL_compiling.cop_warnings) ;
1904 SAVESPTR(PL_compiling.cop_io);
1905 if (! specialCopIO(PL_compiling.cop_io)) {
1906 PL_compiling.cop_io = newSVsv(PL_compiling.cop_io) ;
1907 SAVEFREESV(PL_compiling.cop_io) ;
1913 Perl_block_end(pTHX_ I32 floor, OP *seq)
1915 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
1916 OP* retval = scalarseq(seq);
1917 /* If there were syntax errors, don't try to close a block */
1918 if (PL_yynerrs) return retval;
1920 PL_compiling.op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
1922 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1930 #ifdef USE_5005THREADS
1931 OP *o = newOP(OP_THREADSV, 0);
1932 o->op_targ = find_threadsv("_");
1935 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
1936 #endif /* USE_5005THREADS */
1940 Perl_newPROG(pTHX_ OP *o)
1945 PL_eval_root = newUNOP(OP_LEAVEEVAL,
1946 ((PL_in_eval & EVAL_KEEPERR)
1947 ? OPf_SPECIAL : 0), o);
1948 PL_eval_start = linklist(PL_eval_root);
1949 PL_eval_root->op_private |= OPpREFCOUNTED;
1950 OpREFCNT_set(PL_eval_root, 1);
1951 PL_eval_root->op_next = 0;
1952 CALL_PEEP(PL_eval_start);
1955 if (o->op_type == OP_STUB) {
1956 PL_comppad_name = 0;
1961 PL_main_root = scope(sawparens(scalarvoid(o)));
1962 PL_curcop = &PL_compiling;
1963 PL_main_start = LINKLIST(PL_main_root);
1964 PL_main_root->op_private |= OPpREFCOUNTED;
1965 OpREFCNT_set(PL_main_root, 1);
1966 PL_main_root->op_next = 0;
1967 CALL_PEEP(PL_main_start);
1970 /* Register with debugger */
1972 CV *cv = get_cv("DB::postponed", FALSE);
1976 XPUSHs((SV*)CopFILEGV(&PL_compiling));
1978 call_sv((SV*)cv, G_DISCARD);
1985 Perl_localize(pTHX_ OP *o, I32 lex)
1987 if (o->op_flags & OPf_PARENS)
1988 /* [perl #17376]: this appears to be premature, and results in code such as
1989 C< our(%x); > executing in list mode rather than void mode */
1996 if (ckWARN(WARN_PARENTHESIS)
1997 && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',')
1999 char *s = PL_bufptr;
2002 /* some heuristics to detect a potential error */
2003 while (*s && (strchr(", \t\n", *s)))
2007 if (*s && strchr("@$%*", *s) && *++s
2008 && (isALNUM(*s) || UTF8_IS_CONTINUED(*s))) {
2011 while (*s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s)))
2013 while (*s && (strchr(", \t\n", *s)))
2019 if (sigil && (*s == ';' || *s == '=')) {
2020 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
2021 "Parentheses missing around \"%s\" list",
2022 lex ? (PL_in_my == KEY_our ? "our" : "my")
2030 o = mod(o, OP_NULL); /* a bit kludgey */
2032 PL_in_my_stash = Nullhv;
2037 Perl_jmaybe(pTHX_ OP *o)
2039 if (o->op_type == OP_LIST) {
2041 #ifdef USE_5005THREADS
2042 o2 = newOP(OP_THREADSV, 0);
2043 o2->op_targ = find_threadsv(";");
2045 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
2046 #endif /* USE_5005THREADS */
2047 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2053 Perl_fold_constants(pTHX_ register OP *o)
2056 I32 type = o->op_type;
2059 if (PL_opargs[type] & OA_RETSCALAR)
2061 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2062 o->op_targ = pad_alloc(type, SVs_PADTMP);
2064 /* integerize op, unless it happens to be C<-foo>.
2065 * XXX should pp_i_negate() do magic string negation instead? */
2066 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2067 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2068 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2070 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2073 if (!(PL_opargs[type] & OA_FOLDCONST))
2078 /* XXX might want a ck_negate() for this */
2079 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2091 /* XXX what about the numeric ops? */
2092 if (PL_hints & HINT_LOCALE)
2097 goto nope; /* Don't try to run w/ errors */
2099 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2100 if ((curop->op_type != OP_CONST ||
2101 (curop->op_private & OPpCONST_BARE)) &&
2102 curop->op_type != OP_LIST &&
2103 curop->op_type != OP_SCALAR &&
2104 curop->op_type != OP_NULL &&
2105 curop->op_type != OP_PUSHMARK)
2111 curop = LINKLIST(o);
2115 sv = *(PL_stack_sp--);
2116 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
2117 pad_swipe(o->op_targ, FALSE);
2118 else if (SvTEMP(sv)) { /* grab mortal temp? */
2119 (void)SvREFCNT_inc(sv);
2123 if (type == OP_RV2GV)
2124 return newGVOP(OP_GV, 0, (GV*)sv);
2125 return newSVOP(OP_CONST, 0, sv);
2132 Perl_gen_constant_list(pTHX_ register OP *o)
2135 const I32 oldtmps_floor = PL_tmps_floor;
2139 return o; /* Don't attempt to run with errors */
2141 PL_op = curop = LINKLIST(o);
2148 PL_tmps_floor = oldtmps_floor;
2150 o->op_type = OP_RV2AV;
2151 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2152 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
2153 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
2154 o->op_seq = 0; /* needs to be revisited in peep() */
2155 curop = ((UNOP*)o)->op_first;
2156 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
2163 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2165 if (!o || o->op_type != OP_LIST)
2166 o = newLISTOP(OP_LIST, 0, o, Nullop);
2168 o->op_flags &= ~OPf_WANT;
2170 if (!(PL_opargs[type] & OA_MARK))
2171 op_null(cLISTOPo->op_first);
2173 o->op_type = (OPCODE)type;
2174 o->op_ppaddr = PL_ppaddr[type];
2175 o->op_flags |= flags;
2177 o = CHECKOP(type, o);
2178 if (o->op_type != (unsigned)type)
2181 return fold_constants(o);
2184 /* List constructors */
2187 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2195 if (first->op_type != (unsigned)type
2196 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
2198 return newLISTOP(type, 0, first, last);
2201 if (first->op_flags & OPf_KIDS)
2202 ((LISTOP*)first)->op_last->op_sibling = last;
2204 first->op_flags |= OPf_KIDS;
2205 ((LISTOP*)first)->op_first = last;
2207 ((LISTOP*)first)->op_last = last;
2212 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2220 if (first->op_type != (unsigned)type)
2221 return prepend_elem(type, (OP*)first, (OP*)last);
2223 if (last->op_type != (unsigned)type)
2224 return append_elem(type, (OP*)first, (OP*)last);
2226 first->op_last->op_sibling = last->op_first;
2227 first->op_last = last->op_last;
2228 first->op_flags |= (last->op_flags & OPf_KIDS);
2236 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2244 if (last->op_type == (unsigned)type) {
2245 if (type == OP_LIST) { /* already a PUSHMARK there */
2246 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2247 ((LISTOP*)last)->op_first->op_sibling = first;
2248 if (!(first->op_flags & OPf_PARENS))
2249 last->op_flags &= ~OPf_PARENS;
2252 if (!(last->op_flags & OPf_KIDS)) {
2253 ((LISTOP*)last)->op_last = first;
2254 last->op_flags |= OPf_KIDS;
2256 first->op_sibling = ((LISTOP*)last)->op_first;
2257 ((LISTOP*)last)->op_first = first;
2259 last->op_flags |= OPf_KIDS;
2263 return newLISTOP(type, 0, first, last);
2269 Perl_newNULLLIST(pTHX)
2271 return newOP(OP_STUB, 0);
2275 Perl_force_list(pTHX_ OP *o)
2277 if (!o || o->op_type != OP_LIST)
2278 o = newLISTOP(OP_LIST, 0, o, Nullop);
2284 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2288 NewOp(1101, listop, 1, LISTOP);
2290 listop->op_type = (OPCODE)type;
2291 listop->op_ppaddr = PL_ppaddr[type];
2294 listop->op_flags = (U8)flags;
2298 else if (!first && last)
2301 first->op_sibling = last;
2302 listop->op_first = first;
2303 listop->op_last = last;
2304 if (type == OP_LIST) {
2306 pushop = newOP(OP_PUSHMARK, 0);
2307 pushop->op_sibling = first;
2308 listop->op_first = pushop;
2309 listop->op_flags |= OPf_KIDS;
2311 listop->op_last = pushop;
2314 return CHECKOP(type, listop);
2318 Perl_newOP(pTHX_ I32 type, I32 flags)
2321 NewOp(1101, o, 1, OP);
2322 o->op_type = (OPCODE)type;
2323 o->op_ppaddr = PL_ppaddr[type];
2324 o->op_flags = (U8)flags;
2327 o->op_private = (U8)(0 | (flags >> 8));
2328 if (PL_opargs[type] & OA_RETSCALAR)
2330 if (PL_opargs[type] & OA_TARGET)
2331 o->op_targ = pad_alloc(type, SVs_PADTMP);
2332 return CHECKOP(type, o);
2336 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
2341 first = newOP(OP_STUB, 0);
2342 if (PL_opargs[type] & OA_MARK)
2343 first = force_list(first);
2345 NewOp(1101, unop, 1, UNOP);
2346 unop->op_type = (OPCODE)type;
2347 unop->op_ppaddr = PL_ppaddr[type];
2348 unop->op_first = first;
2349 unop->op_flags = flags | OPf_KIDS;
2350 unop->op_private = (U8)(1 | (flags >> 8));
2351 unop = (UNOP*) CHECKOP(type, unop);
2355 return fold_constants((OP *) unop);
2359 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2362 NewOp(1101, binop, 1, BINOP);
2365 first = newOP(OP_NULL, 0);
2367 binop->op_type = (OPCODE)type;
2368 binop->op_ppaddr = PL_ppaddr[type];
2369 binop->op_first = first;
2370 binop->op_flags = flags | OPf_KIDS;
2373 binop->op_private = (U8)(1 | (flags >> 8));
2376 binop->op_private = (U8)(2 | (flags >> 8));
2377 first->op_sibling = last;
2380 binop = (BINOP*)CHECKOP(type, binop);
2381 if (binop->op_next || binop->op_type != (OPCODE)type)
2384 binop->op_last = binop->op_first->op_sibling;
2386 return fold_constants((OP *)binop);
2389 static int uvcompare(const void *a, const void *b) __attribute__nonnull__(1) __attribute__nonnull__(2) __attribute__pure__;
2390 static int uvcompare(const void *a, const void *b)
2392 if (*((const UV *)a) < (*(const UV *)b))
2394 if (*((const UV *)a) > (*(const UV *)b))
2396 if (*((const UV *)a+1) < (*(const UV *)b+1))
2398 if (*((const UV *)a+1) > (*(const UV *)b+1))
2404 Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
2406 SV *tstr = ((SVOP*)expr)->op_sv;
2407 SV *rstr = ((SVOP*)repl)->op_sv;
2410 const U8 *t = (U8*)SvPV_const(tstr, tlen);
2411 const U8 *r = (U8*)SvPV_const(rstr, rlen);
2418 register short *tbl;
2420 PL_hints |= HINT_BLOCK_SCOPE;
2421 complement = o->op_private & OPpTRANS_COMPLEMENT;
2422 del = o->op_private & OPpTRANS_DELETE;
2423 squash = o->op_private & OPpTRANS_SQUASH;
2426 o->op_private |= OPpTRANS_FROM_UTF;
2429 o->op_private |= OPpTRANS_TO_UTF;
2431 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2432 SV* listsv = newSVpvn("# comment\n",10);
2434 const U8* tend = t + tlen;
2435 const U8* rend = r + rlen;
2449 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2450 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2456 t = tsave = bytes_to_utf8(t, &len);
2459 if (!to_utf && rlen) {
2461 r = rsave = bytes_to_utf8(r, &len);
2465 /* There are several snags with this code on EBCDIC:
2466 1. 0xFF is a legal UTF-EBCDIC byte (there are no illegal bytes).
2467 2. scan_const() in toke.c has encoded chars in native encoding which makes
2468 ranges at least in EBCDIC 0..255 range the bottom odd.
2472 U8 tmpbuf[UTF8_MAXBYTES+1];
2475 New(1109, cp, 2*tlen, UV);
2477 transv = newSVpvn("",0);
2479 cp[2*i] = utf8n_to_uvuni(t, tend-t, &ulen, 0);
2481 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) {
2483 cp[2*i+1] = utf8n_to_uvuni(t, tend-t, &ulen, 0);
2487 cp[2*i+1] = cp[2*i];
2491 qsort(cp, i, 2*sizeof(UV), uvcompare);
2492 for (j = 0; j < i; j++) {
2494 diff = val - nextmin;
2496 t = uvuni_to_utf8(tmpbuf,nextmin);
2497 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2499 U8 range_mark = UTF_TO_NATIVE(0xff);
2500 t = uvuni_to_utf8(tmpbuf, val - 1);
2501 sv_catpvn(transv, (char *)&range_mark, 1);
2502 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2509 t = uvuni_to_utf8(tmpbuf,nextmin);
2510 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2512 U8 range_mark = UTF_TO_NATIVE(0xff);
2513 sv_catpvn(transv, (char *)&range_mark, 1);
2515 t = uvuni_to_utf8_flags(tmpbuf, 0x7fffffff,
2516 UNICODE_ALLOW_SUPER);
2517 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2518 t = (U8*)SvPVX(transv);
2519 tlen = SvCUR(transv);
2523 else if (!rlen && !del) {
2524 r = t; rlen = tlen; rend = tend;
2527 if ((!rlen && !del) || t == r ||
2528 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
2530 o->op_private |= OPpTRANS_IDENTICAL;
2534 while (t < tend || tfirst <= tlast) {
2535 /* see if we need more "t" chars */
2536 if (tfirst > tlast) {
2537 tfirst = (I32)utf8n_to_uvuni(t, tend - t, &ulen, 0);
2539 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) { /* illegal utf8 val indicates range */
2541 tlast = (I32)utf8n_to_uvuni(t, tend - t, &ulen, 0);
2548 /* now see if we need more "r" chars */
2549 if (rfirst > rlast) {
2551 rfirst = (I32)utf8n_to_uvuni(r, rend - r, &ulen, 0);
2553 if (r < rend && NATIVE_TO_UTF(*r) == 0xff) { /* illegal utf8 val indicates range */
2555 rlast = (I32)utf8n_to_uvuni(r, rend - r, &ulen, 0);
2564 rfirst = rlast = 0xffffffff;
2568 /* now see which range will peter our first, if either. */
2569 tdiff = tlast - tfirst;
2570 rdiff = rlast - rfirst;
2577 if (rfirst == 0xffffffff) {
2578 diff = tdiff; /* oops, pretend rdiff is infinite */
2580 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
2581 (long)tfirst, (long)tlast);
2583 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
2587 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
2588 (long)tfirst, (long)(tfirst + diff),
2591 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
2592 (long)tfirst, (long)rfirst);
2594 if (rfirst + diff > max)
2595 max = rfirst + diff;
2597 grows = (tfirst < rfirst &&
2598 UNISKIP(tfirst) < UNISKIP(rfirst + diff));
2610 else if (max > 0xff)
2615 Safefree(cPVOPo->op_pv);
2616 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2617 SvREFCNT_dec(listsv);
2619 SvREFCNT_dec(transv);
2621 if (!del && havefinal && rlen)
2622 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5,
2623 newSVuv((UV)final), 0);
2626 o->op_private |= OPpTRANS_GROWS;
2638 tbl = (short*)cPVOPo->op_pv;
2640 Zero(tbl, 256, short);
2641 for (i = 0; i < (I32)tlen; i++)
2643 for (i = 0, j = 0; i < 256; i++) {
2645 if (j >= (I32)rlen) {
2654 if (i < 128 && r[j] >= 128)
2664 o->op_private |= OPpTRANS_IDENTICAL;
2666 else if (j >= (I32)rlen)
2669 cPVOPo->op_pv = (char*)Renew(tbl, 0x101+rlen-j, short);
2670 tbl[0x100] = rlen - j;
2671 for (i=0; i < (I32)rlen - j; i++)
2672 tbl[0x101+i] = r[j+i];
2676 if (!rlen && !del) {
2679 o->op_private |= OPpTRANS_IDENTICAL;
2681 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
2682 o->op_private |= OPpTRANS_IDENTICAL;
2684 for (i = 0; i < 256; i++)
2686 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
2687 if (j >= (I32)rlen) {
2689 if (tbl[t[i]] == -1)
2695 if (tbl[t[i]] == -1) {
2696 if (t[i] < 128 && r[j] >= 128)
2703 o->op_private |= OPpTRANS_GROWS;
2711 Perl_newPMOP(pTHX_ I32 type, I32 flags)
2715 NewOp(1101, pmop, 1, PMOP);
2716 pmop->op_type = (OPCODE)type;
2717 pmop->op_ppaddr = PL_ppaddr[type];
2718 pmop->op_flags = (U8)flags;
2719 pmop->op_private = (U8)(0 | (flags >> 8));
2721 if (PL_hints & HINT_RE_TAINT)
2722 pmop->op_pmpermflags |= PMf_RETAINT;
2723 if (PL_hints & HINT_LOCALE)
2724 pmop->op_pmpermflags |= PMf_LOCALE;
2725 pmop->op_pmflags = pmop->op_pmpermflags;
2730 if(av_len((AV*) PL_regex_pad[0]) > -1) {
2731 repointer = av_pop((AV*)PL_regex_pad[0]);
2732 pmop->op_pmoffset = SvIV(repointer);
2733 SvREPADTMP_off(repointer);
2734 sv_setiv(repointer,0);
2736 repointer = newSViv(0);
2737 av_push(PL_regex_padav,SvREFCNT_inc(repointer));
2738 pmop->op_pmoffset = av_len(PL_regex_padav);
2739 PL_regex_pad = AvARRAY(PL_regex_padav);
2744 /* link into pm list */
2745 if (type != OP_TRANS && PL_curstash) {
2746 pmop->op_pmnext = HvPMROOT(PL_curstash);
2747 HvPMROOT(PL_curstash) = pmop;
2748 PmopSTASH_set(pmop,PL_curstash);
2751 return CHECKOP(type, pmop);
2755 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
2759 I32 repl_has_vars = 0;
2761 if (o->op_type == OP_TRANS)
2762 return pmtrans(o, expr, repl);
2764 PL_hints |= HINT_BLOCK_SCOPE;
2767 if (expr->op_type == OP_CONST) {
2769 SV *pat = ((SVOP*)expr)->op_sv;
2770 const char *p = SvPV_const(pat, plen);
2771 if ((o->op_flags & OPf_SPECIAL) && (*p == ' ' && p[1] == '\0')) {
2772 U32 was_readonly = SvREADONLY(pat);
2776 sv_force_normal_flags(pat, 0);
2777 assert(!SvREADONLY(pat));
2780 SvREADONLY_off(pat);
2784 sv_setpvn(pat, "\\s+", 3);
2786 SvFLAGS(pat) |= was_readonly;
2788 p = SvPV_const(pat, plen);
2789 pm->op_pmflags |= PMf_SKIPWHITE;
2792 pm->op_pmdynflags |= PMdf_UTF8;
2793 /* FIXME - can we make this function take const char * args? */
2794 PM_SETRE(pm, CALLREGCOMP(aTHX_ (char*)p, (char*)p + plen, pm));
2795 if (strEQ("\\s+", PM_GETRE(pm)->precomp))
2796 pm->op_pmflags |= PMf_WHITE;
2800 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2801 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2803 : OP_REGCMAYBE),0,expr);
2805 NewOp(1101, rcop, 1, LOGOP);
2806 rcop->op_type = OP_REGCOMP;
2807 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2808 rcop->op_first = scalar(expr);
2809 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2810 ? (OPf_SPECIAL | OPf_KIDS)
2812 rcop->op_private = 1;
2815 /* establish postfix order */
2816 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2818 rcop->op_next = expr;
2819 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2822 rcop->op_next = LINKLIST(expr);
2823 expr->op_next = (OP*)rcop;
2826 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2831 if (pm->op_pmflags & PMf_EVAL) {
2833 if (CopLINE(PL_curcop) < (line_t)PL_multi_end)
2834 CopLINE_set(PL_curcop, (line_t)PL_multi_end);
2836 #ifdef USE_5005THREADS
2837 else if (repl->op_type == OP_THREADSV
2838 && strchr("&`'123456789+",
2839 PL_threadsv_names[repl->op_targ]))
2843 #endif /* USE_5005THREADS */
2844 else if (repl->op_type == OP_CONST)
2848 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2849 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2850 #ifdef USE_5005THREADS
2851 if (curop->op_type == OP_THREADSV) {
2853 if (strchr("&`'123456789+", curop->op_private))
2857 if (curop->op_type == OP_GV) {
2858 GV *gv = cGVOPx_gv(curop);
2860 if (strchr("&`'123456789+-\016\022", *GvENAME(gv)))
2863 #endif /* USE_5005THREADS */
2864 else if (curop->op_type == OP_RV2CV)
2866 else if (curop->op_type == OP_RV2SV ||
2867 curop->op_type == OP_RV2AV ||
2868 curop->op_type == OP_RV2HV ||
2869 curop->op_type == OP_RV2GV) {
2870 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2873 else if (curop->op_type == OP_PADSV ||
2874 curop->op_type == OP_PADAV ||
2875 curop->op_type == OP_PADHV ||
2876 curop->op_type == OP_PADANY) {
2879 else if (curop->op_type == OP_PUSHRE)
2880 ; /* Okay here, dangerous in newASSIGNOP */
2890 || PM_GETRE(pm)->reganch & ROPT_EVAL_SEEN))) {
2891 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2892 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2893 prepend_elem(o->op_type, scalar(repl), o);
2896 if (curop == repl && !PM_GETRE(pm)) { /* Has variables. */
2897 pm->op_pmflags |= PMf_MAYBE_CONST;
2898 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2900 NewOp(1101, rcop, 1, LOGOP);
2901 rcop->op_type = OP_SUBSTCONT;
2902 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
2903 rcop->op_first = scalar(repl);
2904 rcop->op_flags |= OPf_KIDS;
2905 rcop->op_private = 1;
2908 /* establish postfix order */
2909 rcop->op_next = LINKLIST(repl);
2910 repl->op_next = (OP*)rcop;
2912 pm->op_pmreplroot = scalar((OP*)rcop);
2913 pm->op_pmreplstart = LINKLIST(rcop);
2922 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
2925 NewOp(1101, svop, 1, SVOP);
2926 svop->op_type = (OPCODE)type;
2927 svop->op_ppaddr = PL_ppaddr[type];
2929 svop->op_next = (OP*)svop;
2930 svop->op_flags = (U8)flags;
2931 if (PL_opargs[type] & OA_RETSCALAR)
2933 if (PL_opargs[type] & OA_TARGET)
2934 svop->op_targ = pad_alloc(type, SVs_PADTMP);
2935 return CHECKOP(type, svop);
2939 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
2942 NewOp(1101, padop, 1, PADOP);
2943 padop->op_type = (OPCODE)type;
2944 padop->op_ppaddr = PL_ppaddr[type];
2945 padop->op_padix = pad_alloc(type, SVs_PADTMP);
2946 SvREFCNT_dec(PAD_SVl(padop->op_padix));
2947 PAD_SETSV(padop->op_padix, sv);
2950 padop->op_next = (OP*)padop;
2951 padop->op_flags = (U8)flags;
2952 if (PL_opargs[type] & OA_RETSCALAR)
2954 if (PL_opargs[type] & OA_TARGET)
2955 padop->op_targ = pad_alloc(type, SVs_PADTMP);
2956 return CHECKOP(type, padop);
2960 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
2965 return newPADOP(type, flags, SvREFCNT_inc(gv));
2967 return newSVOP(type, flags, SvREFCNT_inc(gv));
2972 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
2975 NewOp(1101, pvop, 1, PVOP);
2976 pvop->op_type = (OPCODE)type;
2977 pvop->op_ppaddr = PL_ppaddr[type];
2979 pvop->op_next = (OP*)pvop;
2980 pvop->op_flags = (U8)flags;
2981 if (PL_opargs[type] & OA_RETSCALAR)
2983 if (PL_opargs[type] & OA_TARGET)
2984 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2985 return CHECKOP(type, pvop);
2989 Perl_package(pTHX_ OP *o)
2993 save_hptr(&PL_curstash);
2994 save_item(PL_curstname);
2999 name = SvPV_const(sv, len);
3000 PL_curstash = gv_stashpvn(name,len,TRUE);
3001 sv_setpvn(PL_curstname, name, len);
3005 deprecate("\"package\" with no arguments");
3006 sv_setpv(PL_curstname,"<none>");
3007 PL_curstash = Nullhv;
3009 PL_hints |= HINT_BLOCK_SCOPE;
3010 PL_copline = NOLINE;
3015 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
3021 if (idop->op_type != OP_CONST)
3022 Perl_croak(aTHX_ "Module name must be constant");
3026 if (version != Nullop) {
3027 SV *vesv = ((SVOP*)version)->op_sv;
3029 if (arg == Nullop && !SvNIOKp(vesv)) {
3036 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
3037 Perl_croak(aTHX_ "Version number must be constant number");
3039 /* Make copy of idop so we don't free it twice */
3040 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
3042 /* Fake up a method call to VERSION */
3043 meth = newSVpvn("VERSION",7);
3044 sv_upgrade(meth, SVt_PVIV);
3045 (void)SvIOK_on(meth);
3048 PERL_HASH(hash, SvPVX_const(meth), SvCUR(meth));
3049 SvUV_set(meth, hash);
3051 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3052 append_elem(OP_LIST,
3053 prepend_elem(OP_LIST, pack, list(version)),
3054 newSVOP(OP_METHOD_NAMED, 0, meth)));
3058 /* Fake up an import/unimport */
3059 if (arg && arg->op_type == OP_STUB)
3060 imop = arg; /* no import on explicit () */
3061 else if (SvNIOKp(((SVOP*)idop)->op_sv)) {
3062 imop = Nullop; /* use 5.0; */
3067 /* Make copy of idop so we don't free it twice */
3068 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
3070 /* Fake up a method call to import/unimport */
3071 meth = aver ? newSVpvn("import",6) : newSVpvn("unimport", 8);
3072 (void)SvUPGRADE(meth, SVt_PVIV);
3073 (void)SvIOK_on(meth);
3076 PERL_HASH(hash, SvPVX_const(meth), SvCUR(meth));
3077 SvUV_set(meth, hash);
3079 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3080 append_elem(OP_LIST,
3081 prepend_elem(OP_LIST, pack, list(arg)),
3082 newSVOP(OP_METHOD_NAMED, 0, meth)));
3085 /* Fake up the BEGIN {}, which does its thing immediately. */
3087 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
3090 append_elem(OP_LINESEQ,
3091 append_elem(OP_LINESEQ,
3092 newSTATEOP(0, Nullch, newUNOP(OP_REQUIRE, 0, idop)),
3093 newSTATEOP(0, Nullch, veop)),
3094 newSTATEOP(0, Nullch, imop) ));
3096 /* The "did you use incorrect case?" warning used to be here.
3097 * The problem is that on case-insensitive filesystems one
3098 * might get false positives for "use" (and "require"):
3099 * "use Strict" or "require CARP" will work. This causes
3100 * portability problems for the script: in case-strict
3101 * filesystems the script will stop working.
3103 * The "incorrect case" warning checked whether "use Foo"
3104 * imported "Foo" to your namespace, but that is wrong, too:
3105 * there is no requirement nor promise in the language that
3106 * a Foo.pm should or would contain anything in package "Foo".
3108 * There is very little Configure-wise that can be done, either:
3109 * the case-sensitivity of the build filesystem of Perl does not
3110 * help in guessing the case-sensitivity of the runtime environment.
3113 PL_hints |= HINT_BLOCK_SCOPE;
3114 PL_copline = NOLINE;
3116 PL_cop_seqmax++; /* Purely for B::*'s benefit */
3120 =head1 Embedding Functions
3122 =for apidoc load_module
3124 Loads the module whose name is pointed to by the string part of name.
3125 Note that the actual module name, not its filename, should be given.
3126 Eg, "Foo::Bar" instead of "Foo/Bar.pm". flags can be any of
3127 PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS
3128 (or 0 for no flags). ver, if specified, provides version semantics
3129 similar to C<use Foo::Bar VERSION>. The optional trailing SV*
3130 arguments can be used to specify arguments to the module's import()
3131 method, similar to C<use Foo::Bar VERSION LIST>.
3136 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
3139 va_start(args, ver);
3140 vload_module(flags, name, ver, &args);
3144 #ifdef PERL_IMPLICIT_CONTEXT
3146 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
3150 va_start(args, ver);
3151 vload_module(flags, name, ver, &args);
3157 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
3159 OP *modname, *veop, *imop;
3161 modname = newSVOP(OP_CONST, 0, name);
3162 modname->op_private |= OPpCONST_BARE;
3164 veop = newSVOP(OP_CONST, 0, ver);
3168 if (flags & PERL_LOADMOD_NOIMPORT) {
3169 imop = sawparens(newNULLLIST());
3171 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
3172 imop = va_arg(*args, OP*);
3177 sv = va_arg(*args, SV*);
3179 imop = append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
3180 sv = va_arg(*args, SV*);
3184 const line_t ocopline = PL_copline;
3185 COP * const ocurcop = PL_curcop;
3186 const int oexpect = PL_expect;
3188 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
3189 veop, modname, imop);
3190 PL_expect = oexpect;
3191 PL_copline = ocopline;
3192 PL_curcop = ocurcop;
3197 Perl_dofile(pTHX_ OP *term)
3202 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
3203 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv)))
3204 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
3206 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
3207 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3208 append_elem(OP_LIST, term,
3209 scalar(newUNOP(OP_RV2CV, 0,
3214 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3220 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
3222 return newBINOP(OP_LSLICE, flags,
3223 list(force_list(subscript)),
3224 list(force_list(listval)) );
3228 S_is_list_assignment(pTHX_ register const OP *o)
3233 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3234 o = cUNOPo->op_first;
3236 if (o->op_type == OP_COND_EXPR) {
3237 const I32 t = is_list_assignment(cLOGOPo->op_first->op_sibling);
3238 const I32 f = is_list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
3243 yyerror("Assignment to both a list and a scalar");
3247 if (o->op_type == OP_LIST &&
3248 (o->op_flags & OPf_WANT) == OPf_WANT_SCALAR &&
3249 o->op_private & OPpLVAL_INTRO)
3252 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3253 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3254 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
3257 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
3260 if (o->op_type == OP_RV2SV)
3267 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
3272 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
3273 return newLOGOP(optype, 0,
3274 mod(scalar(left), optype),
3275 newUNOP(OP_SASSIGN, 0, scalar(right)));
3278 return newBINOP(optype, OPf_STACKED,
3279 mod(scalar(left), optype), scalar(right));
3283 if (is_list_assignment(left)) {
3287 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3288 left = mod(left, OP_AASSIGN);
3296 /* optimise C<my @x = ()> to C<my @x>, and likewise for hashes */
3297 if ((left->op_type == OP_PADAV || left->op_type == OP_PADHV)
3298 && right->op_type == OP_STUB
3299 && (left->op_private & OPpLVAL_INTRO))
3302 left->op_flags &= ~(OPf_REF|OPf_SPECIAL);
3305 curop = list(force_list(left));
3306 o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
3307 o->op_private = (U8)(0 | (flags >> 8));
3308 for (curop = ((LISTOP*)curop)->op_first;
3309 curop; curop = curop->op_sibling)
3311 if (curop->op_type == OP_RV2HV &&
3312 ((UNOP*)curop)->op_first->op_type != OP_GV) {
3313 o->op_private |= OPpASSIGN_HASH;
3318 /* PL_generation sorcery:
3319 * an assignment like ($a,$b) = ($c,$d) is easier than
3320 * ($a,$b) = ($c,$a), since there is no need for temporary vars.
3321 * To detect whether there are common vars, the global var
3322 * PL_generation is incremented for each assign op we compile.
3323 * Then, while compiling the assign op, we run through all the
3324 * variables on both sides of the assignment, setting a spare slot
3325 * in each of them to PL_generation. If any of them already have
3326 * that value, we know we've got commonality. We could use a
3327 * single bit marker, but then we'd have to make 2 passes, first
3328 * to clear the flag, then to test and set it. To find somewhere
3329 * to store these values, evil chicanery is done with SvCUR().
3332 if (!(left->op_private & OPpLVAL_INTRO)) {
3335 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3336 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3337 if (curop->op_type == OP_GV) {
3338 GV *gv = cGVOPx_gv(curop);
3339 if (gv == PL_defgv || (int)SvCUR(gv) == PL_generation)
3341 SvCUR_set(gv, PL_generation);
3343 else if (curop->op_type == OP_PADSV ||
3344 curop->op_type == OP_PADAV ||
3345 curop->op_type == OP_PADHV ||
3346 curop->op_type == OP_PADANY)
3348 if ((int)PAD_COMPNAME_GEN(curop->op_targ)
3351 PAD_COMPNAME_GEN_set(curop->op_targ, PL_generation);
3354 else if (curop->op_type == OP_RV2CV)
3356 else if (curop->op_type == OP_RV2SV ||
3357 curop->op_type == OP_RV2AV ||
3358 curop->op_type == OP_RV2HV ||
3359 curop->op_type == OP_RV2GV) {
3360 if (lastop->op_type != OP_GV) /* funny deref? */
3363 else if (curop->op_type == OP_PUSHRE) {
3364 if (((PMOP*)curop)->op_pmreplroot) {
3366 GV *gv = (GV*)PAD_SVl(INT2PTR(PADOFFSET,
3367 ((PMOP*)curop)->op_pmreplroot));
3369 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3371 if (gv == PL_defgv || (int)SvCUR(gv) == PL_generation)
3373 SvCUR_set(gv, PL_generation);
3382 o->op_private |= OPpASSIGN_COMMON;
3384 if (right && right->op_type == OP_SPLIT) {
3386 if ((tmpop = ((LISTOP*)right)->op_first) &&
3387 tmpop->op_type == OP_PUSHRE)
3389 PMOP *pm = (PMOP*)tmpop;
3390 if (left->op_type == OP_RV2AV &&
3391 !(left->op_private & OPpLVAL_INTRO) &&
3392 !(o->op_private & OPpASSIGN_COMMON) )
3394 tmpop = ((UNOP*)left)->op_first;
3395 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
3397 pm->op_pmreplroot = INT2PTR(OP*, cPADOPx(tmpop)->op_padix);
3398 cPADOPx(tmpop)->op_padix = 0; /* steal it */
3400 pm->op_pmreplroot = (OP*)cSVOPx(tmpop)->op_sv;
3401 cSVOPx(tmpop)->op_sv = Nullsv; /* steal it */
3403 pm->op_pmflags |= PMf_ONCE;
3404 tmpop = cUNOPo->op_first; /* to list (nulled) */
3405 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3406 tmpop->op_sibling = Nullop; /* don't free split */
3407 right->op_next = tmpop->op_next; /* fix starting loc */
3408 op_free(o); /* blow off assign */
3409 right->op_flags &= ~OPf_WANT;
3410 /* "I don't know and I don't care." */
3415 if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
3416 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3418 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3420 sv_setiv(sv, PL_modcount+1);
3428 right = newOP(OP_UNDEF, 0);
3429 if (right->op_type == OP_READLINE) {
3430 right->op_flags |= OPf_STACKED;
3431 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3434 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3435 o = newBINOP(OP_SASSIGN, flags,
3436 scalar(right), mod(scalar(left), OP_SASSIGN) );
3448 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3450 const U32 seq = intro_my();
3453 NewOp(1101, cop, 1, COP);
3454 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
3455 cop->op_type = OP_DBSTATE;
3456 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3459 cop->op_type = OP_NEXTSTATE;
3460 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3462 cop->op_flags = (U8)flags;
3463 cop->op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
3465 cop->op_private |= NATIVE_HINTS;
3467 PL_compiling.op_private = cop->op_private;
3468 cop->op_next = (OP*)cop;
3471 cop->cop_label = label;
3472 PL_hints |= HINT_BLOCK_SCOPE;
3475 cop->cop_arybase = PL_curcop->cop_arybase;
3476 if (specialWARN(PL_curcop->cop_warnings))
3477 cop->cop_warnings = PL_curcop->cop_warnings ;
3479 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3480 if (specialCopIO(PL_curcop->cop_io))
3481 cop->cop_io = PL_curcop->cop_io;
3483 cop->cop_io = newSVsv(PL_curcop->cop_io) ;
3486 if (PL_copline == NOLINE)
3487 CopLINE_set(cop, CopLINE(PL_curcop));
3489 CopLINE_set(cop, PL_copline);
3490 PL_copline = NOLINE;
3493 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
3495 CopFILEGV_set(cop, CopFILEGV(PL_curcop));
3497 CopSTASH_set(cop, PL_curstash);
3499 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3500 SV **svp = av_fetch(CopFILEAV(PL_curcop), (I32)CopLINE(cop), FALSE);
3501 if (svp && *svp != &PL_sv_undef ) {
3502 (void)SvIOK_on(*svp);
3503 SvIV_set(*svp, PTR2IV(cop));
3507 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3512 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
3514 return new_logop(type, flags, &first, &other);
3518 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
3522 OP *first = *firstp;
3523 OP *other = *otherp;
3525 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3526 return newBINOP(type, flags, scalar(first), scalar(other));
3528 scalarboolean(first);
3529 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3530 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3531 if (type == OP_AND || type == OP_OR) {
3537 first = *firstp = cUNOPo->op_first;
3539 first->op_next = o->op_next;
3540 cUNOPo->op_first = Nullop;
3544 if (first->op_type == OP_CONST) {
3545 if (first->op_private & OPpCONST_STRICT)
3546 no_bareword_allowed(first);
3547 else if (ckWARN(WARN_BAREWORD) && (first->op_private & OPpCONST_BARE))
3548 Perl_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional");
3549 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3552 if (other->op_type == OP_CONST)
3553 other->op_private |= OPpCONST_SHORTCIRCUIT;
3559 if (first->op_type == OP_CONST)
3560 first->op_private |= OPpCONST_SHORTCIRCUIT;
3564 else if (ckWARN(WARN_MISC) && (first->op_flags & OPf_KIDS)) {
3565 const OP *k1 = ((UNOP*)first)->op_first;
3566 const OP *k2 = k1->op_sibling;
3568 switch (first->op_type)
3571 if (k2 && k2->op_type == OP_READLINE
3572 && (k2->op_flags & OPf_STACKED)
3573 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3575 warnop = k2->op_type;
3580 if (k1->op_type == OP_READDIR
3581 || k1->op_type == OP_GLOB
3582 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3583 || k1->op_type == OP_EACH)
3585 warnop = ((k1->op_type == OP_NULL)
3586 ? (OPCODE)k1->op_targ : k1->op_type);
3591 const line_t oldline = CopLINE(PL_curcop);
3592 CopLINE_set(PL_curcop, PL_copline);
3593 Perl_warner(aTHX_ packWARN(WARN_MISC),
3594 "Value of %s%s can be \"0\"; test with defined()",
3596 ((warnop == OP_READLINE || warnop == OP_GLOB)
3597 ? " construct" : "() operator"));
3598 CopLINE_set(PL_curcop, oldline);
3605 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3606 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3608 NewOp(1101, logop, 1, LOGOP);
3610 logop->op_type = (OPCODE)type;
3611 logop->op_ppaddr = PL_ppaddr[type];
3612 logop->op_first = first;
3613 logop->op_flags = flags | OPf_KIDS;
3614 logop->op_other = LINKLIST(other);
3615 logop->op_private = (U8)(1 | (flags >> 8));
3617 /* establish postfix order */
3618 logop->op_next = LINKLIST(first);
3619 first->op_next = (OP*)logop;
3620 first->op_sibling = other;
3622 CHECKOP(type,logop);
3624 o = newUNOP(OP_NULL, 0, (OP*)logop);
3631 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
3638 return newLOGOP(OP_AND, 0, first, trueop);
3640 return newLOGOP(OP_OR, 0, first, falseop);
3642 scalarboolean(first);
3643 if (first->op_type == OP_CONST) {
3644 if (first->op_private & OPpCONST_BARE &&
3645 first->op_private & OPpCONST_STRICT) {
3646 no_bareword_allowed(first);
3648 if (SvTRUE(((SVOP*)first)->op_sv)) {
3659 NewOp(1101, logop, 1, LOGOP);
3660 logop->op_type = OP_COND_EXPR;
3661 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3662 logop->op_first = first;
3663 logop->op_flags = flags | OPf_KIDS;
3664 logop->op_private = (U8)(1 | (flags >> 8));
3665 logop->op_other = LINKLIST(trueop);
3666 logop->op_next = LINKLIST(falseop);
3668 CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
3671 /* establish postfix order */
3672 start = LINKLIST(first);
3673 first->op_next = (OP*)logop;
3675 first->op_sibling = trueop;
3676 trueop->op_sibling = falseop;
3677 o = newUNOP(OP_NULL, 0, (OP*)logop);
3679 trueop->op_next = falseop->op_next = o;
3686 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
3694 NewOp(1101, range, 1, LOGOP);
3696 range->op_type = OP_RANGE;
3697 range->op_ppaddr = PL_ppaddr[OP_RANGE];
3698 range->op_first = left;
3699 range->op_flags = OPf_KIDS;
3700 leftstart = LINKLIST(left);
3701 range->op_other = LINKLIST(right);
3702 range->op_private = (U8)(1 | (flags >> 8));
3704 left->op_sibling = right;
3706 range->op_next = (OP*)range;
3707 flip = newUNOP(OP_FLIP, flags, (OP*)range);
3708 flop = newUNOP(OP_FLOP, 0, flip);
3709 o = newUNOP(OP_NULL, 0, flop);
3711 range->op_next = leftstart;
3713 left->op_next = flip;
3714 right->op_next = flop;
3716 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3717 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
3718 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3719 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3721 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3722 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3725 if (!flip->op_private || !flop->op_private)
3726 linklist(o); /* blow off optimizer unless constant */
3732 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
3736 const bool once = block && block->op_flags & OPf_SPECIAL &&
3737 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3741 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3742 return block; /* do {} while 0 does once */
3743 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3744 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3745 expr = newUNOP(OP_DEFINED, 0,
3746 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3747 } else if (expr->op_flags & OPf_KIDS) {
3748 const OP *k1 = ((UNOP*)expr)->op_first;
3749 const OP *k2 = (k1) ? k1->op_sibling : NULL;
3750 switch (expr->op_type) {
3752 if (k2 && k2->op_type == OP_READLINE
3753 && (k2->op_flags & OPf_STACKED)
3754 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3755 expr = newUNOP(OP_DEFINED, 0, expr);
3759 if (k1->op_type == OP_READDIR
3760 || k1->op_type == OP_GLOB
3761 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3762 || k1->op_type == OP_EACH)
3763 expr = newUNOP(OP_DEFINED, 0, expr);
3769 /* if block is null, the next append_elem() would put UNSTACK, a scalar
3770 * op, in listop. This is wrong. [perl #27024] */
3772 block = newOP(OP_NULL, 0);
3773 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3774 o = new_logop(OP_AND, 0, &expr, &listop);
3777 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3779 if (once && o != listop)
3780 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3783 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3785 o->op_flags |= flags;
3787 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3792 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
3801 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3802 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
3803 expr = newUNOP(OP_DEFINED, 0,
3804 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3805 } else if (expr && (expr->op_flags & OPf_KIDS)) {
3806 const OP *k1 = ((UNOP*)expr)->op_first;
3807 const OP *k2 = (k1) ? k1->op_sibling : NULL;
3808 switch (expr->op_type) {
3810 if (k2 && k2->op_type == OP_READLINE
3811 && (k2->op_flags & OPf_STACKED)
3812 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3813 expr = newUNOP(OP_DEFINED, 0, expr);
3817 if (k1->op_type == OP_READDIR
3818 || k1->op_type == OP_GLOB
3819 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3820 || k1->op_type == OP_EACH)
3821 expr = newUNOP(OP_DEFINED, 0, expr);
3827 block = newOP(OP_NULL, 0);
3829 block = scope(block);
3833 next = LINKLIST(cont);
3836 OP *unstack = newOP(OP_UNSTACK, 0);
3839 cont = append_elem(OP_LINESEQ, cont, unstack);
3842 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3843 redo = LINKLIST(listop);
3846 PL_copline = (line_t)whileline;
3848 o = new_logop(OP_AND, 0, &expr, &listop);
3849 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3850 op_free(expr); /* oops, it's a while (0) */
3852 return Nullop; /* listop already freed by new_logop */
3855 ((LISTOP*)listop)->op_last->op_next =
3856 (o == listop ? redo : LINKLIST(o));
3862 NewOp(1101,loop,1,LOOP);
3863 loop->op_type = OP_ENTERLOOP;
3864 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3865 loop->op_private = 0;
3866 loop->op_next = (OP*)loop;
3869 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3871 loop->op_redoop = redo;
3872 loop->op_lastop = o;
3873 o->op_private |= loopflags;
3876 loop->op_nextop = next;
3878 loop->op_nextop = o;
3880 o->op_flags |= flags;
3881 o->op_private |= (flags >> 8);
3886 Perl_newFOROP(pTHX_ I32 flags, char *label, line_t forline, OP *sv, OP *expr, OP *block, OP *cont)
3890 PADOFFSET padoff = 0;
3895 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3896 iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */
3897 sv->op_type = OP_RV2GV;
3898 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
3900 else if (sv->op_type == OP_PADSV) { /* private variable */
3901 iterpflags = sv->op_private & OPpLVAL_INTRO; /* for my $x () */
3902 padoff = sv->op_targ;
3907 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3908 padoff = sv->op_targ;
3910 iterflags |= OPf_SPECIAL;
3915 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
3918 #ifdef USE_5005THREADS
3919 padoff = find_threadsv("_");
3920 iterflags |= OPf_SPECIAL;
3922 sv = newGVOP(OP_GV, 0, PL_defgv);
3925 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3926 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3927 iterflags |= OPf_STACKED;
3929 else if (expr->op_type == OP_NULL &&
3930 (expr->op_flags & OPf_KIDS) &&
3931 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3933 /* Basically turn for($x..$y) into the same as for($x,$y), but we
3934 * set the STACKED flag to indicate that these values are to be
3935 * treated as min/max values by 'pp_iterinit'.
3937 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3938 LOGOP* range = (LOGOP*) flip->op_first;
3939 OP* const left = range->op_first;
3940 OP* const right = left->op_sibling;
3943 range->op_flags &= ~OPf_KIDS;
3944 range->op_first = Nullop;
3946 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3947 listop->op_first->op_next = range->op_next;
3948 left->op_next = range->op_other;
3949 right->op_next = (OP*)listop;
3950 listop->op_next = listop->op_first;
3953 expr = (OP*)(listop);
3955 iterflags |= OPf_STACKED;
3958 expr = mod(force_list(expr), OP_GREPSTART);
3961 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
3962 append_elem(OP_LIST, expr, scalar(sv))));
3963 assert(!loop->op_next);
3964 /* for my $x () sets OPpLVAL_INTRO;
3965 * for our $x () sets OPpOUR_INTRO; both only used by Deparse.pm */
3966 loop->op_private = (U8)iterpflags;
3967 #ifdef PL_OP_SLAB_ALLOC
3970 NewOp(1234,tmp,1,LOOP);
3971 Copy(loop,tmp,1,LISTOP);
3976 Renew(loop, 1, LOOP);
3978 loop->op_targ = padoff;
3979 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
3980 PL_copline = forline;
3981 return newSTATEOP(0, label, wop);
3985 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
3990 if (type != OP_GOTO || label->op_type == OP_CONST) {
3991 /* "last()" means "last" */
3992 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
3993 o = newOP(type, OPf_SPECIAL);
3995 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
3996 ? SvPVx_const(((SVOP*)label)->op_sv, n_a)
4002 /* Check whether it's going to be a goto &function */
4003 if (label->op_type == OP_ENTERSUB
4004 && !(label->op_flags & OPf_STACKED))
4005 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
4006 o = newUNOP(type, OPf_STACKED, label);
4008 PL_hints |= HINT_BLOCK_SCOPE;
4013 =for apidoc cv_undef
4015 Clear out all the active components of a CV. This can happen either
4016 by an explicit C<undef &foo>, or by the reference count going to zero.
4017 In the former case, we keep the CvOUTSIDE pointer, so that any anonymous
4018 children can still follow the full lexical scope chain.
4024 Perl_cv_undef(pTHX_ CV *cv)
4026 #ifdef USE_5005THREADS
4028 MUTEX_DESTROY(CvMUTEXP(cv));
4029 Safefree(CvMUTEXP(cv));
4032 #endif /* USE_5005THREADS */
4035 if (CvFILE(cv) && !CvXSUB(cv)) {
4036 /* for XSUBs CvFILE point directly to static memory; __FILE__ */
4037 Safefree(CvFILE(cv));
4042 if (!CvXSUB(cv) && CvROOT(cv)) {
4043 #ifdef USE_5005THREADS
4044 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
4045 Perl_croak(aTHX_ "Can't undef active subroutine");
4048 Perl_croak(aTHX_ "Can't undef active subroutine");
4049 #endif /* USE_5005THREADS */
4052 PAD_SAVE_SETNULLPAD();
4054 op_free(CvROOT(cv));
4055 CvROOT(cv) = Nullop;
4056 CvSTART(cv) = Nullop;
4059 SvPOK_off((SV*)cv); /* forget prototype */
4064 /* remove CvOUTSIDE unless this is an undef rather than a free */
4065 if (!SvREFCNT(cv) && CvOUTSIDE(cv)) {
4066 if (!CvWEAKOUTSIDE(cv))
4067 SvREFCNT_dec(CvOUTSIDE(cv));
4068 CvOUTSIDE(cv) = Nullcv;
4071 SvREFCNT_dec((SV*)CvXSUBANY(cv).any_ptr);
4077 /* delete all flags except WEAKOUTSIDE */
4078 CvFLAGS(cv) &= CVf_WEAKOUTSIDE;
4082 Perl_cv_ckproto(pTHX_ CV *cv, GV *gv, char *p)
4084 if (((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX_const(cv)))) && ckWARN_d(WARN_PROTOTYPE)) {
4085 SV* msg = sv_newmortal();
4089 gv_efullname3(name = sv_newmortal(), gv, Nullch);
4090 sv_setpv(msg, "Prototype mismatch:");
4092 Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, name);
4094 Perl_sv_catpvf(aTHX_ msg, " (%"SVf")", (const SV *)cv);
4096 Perl_sv_catpv(aTHX_ msg, ": none");
4097 sv_catpv(msg, " vs ");
4099 Perl_sv_catpvf(aTHX_ msg, "(%s)", p);
4101 sv_catpv(msg, "none");
4102 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%"SVf, msg);
4106 static void const_sv_xsub(pTHX_ CV* cv);
4110 =head1 Optree Manipulation Functions
4112 =for apidoc cv_const_sv
4114 If C<cv> is a constant sub eligible for inlining. returns the constant
4115 value returned by the sub. Otherwise, returns NULL.
4117 Constant subs can be created with C<newCONSTSUB> or as described in
4118 L<perlsub/"Constant Functions">.
4123 Perl_cv_const_sv(pTHX_ CV *cv)
4125 if (!cv || !CvCONST(cv))
4127 return (SV*)CvXSUBANY(cv).any_ptr;
4131 Perl_op_const_sv(pTHX_ OP *o, CV *cv)
4138 if (o->op_type == OP_LINESEQ && cLISTOPo->op_first)
4139 o = cLISTOPo->op_first->op_sibling;
4141 for (; o; o = o->op_next) {
4142 OPCODE type = o->op_type;
4144 if (sv && o->op_next == o)
4146 if (o->op_next != o) {
4147 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
4149 if (type == OP_DBSTATE)
4152 if (type == OP_LEAVESUB || type == OP_RETURN)
4156 if (type == OP_CONST && cSVOPo->op_sv)
4158 else if ((type == OP_PADSV || type == OP_CONST) && cv) {
4159 sv = PAD_BASE_SV(CvPADLIST(cv), o->op_targ);
4163 /* We get here only from cv_clone2() while creating a closure.
4164 Copy the const value here instead of in cv_clone2 so that
4165 SvREADONLY_on doesn't lead to problems when leaving
4170 if (!SvREADONLY(sv) && SvREFCNT(sv) > 1)
4182 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4193 Perl_croak(aTHX_ "\"my sub\" not yet implemented");
4197 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
4199 return Perl_newATTRSUB(aTHX_ floor, o, proto, Nullop, block);
4203 Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4213 const char * const name = o ? SvPVx_const(cSVOPo->op_sv, n_a) : Nullch;
4216 assert(proto->op_type == OP_CONST);
4217 ps = SvPVx_const(((SVOP*)proto)->op_sv, ps_len);
4222 if (!name && PERLDB_NAMEANON && CopLINE(PL_curcop)) {
4223 SV *sv = sv_newmortal();
4224 Perl_sv_setpvf(aTHX_ sv, "%s[%s:%"IVdf"]",
4225 PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
4226 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
4227 aname = SvPVX_const(sv);
4231 gv = gv_fetchpv(name ? name : (aname ? aname :
4232 (PL_curstash ? "__ANON__" : "__ANON__::__ANON__")),
4233 GV_ADDMULTI | ((block || attrs) ? 0 : GV_NOINIT),
4243 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
4244 maximum a prototype before. */
4245 if (SvTYPE(gv) > SVt_NULL) {
4246 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1)
4247 && ckWARN_d(WARN_PROTOTYPE))
4249 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "Runaway prototype");
4251 cv_ckproto((CV*)gv, NULL, ps);
4254 sv_setpvn((SV*)gv, ps, ps_len);
4256 sv_setiv((SV*)gv, -1);
4257 SvREFCNT_dec(PL_compcv);
4258 cv = PL_compcv = NULL;
4259 PL_sub_generation++;
4263 cv = (!name || GvCVGEN(gv)) ? Nullcv : GvCV(gv);
4265 #ifdef GV_UNIQUE_CHECK
4266 if (cv && GvUNIQUE(gv) && SvREADONLY(cv)) {
4267 Perl_croak(aTHX_ "Can't define subroutine %s (GV is unique)", name);
4271 if (!block || !ps || *ps || attrs)
4274 const_sv = op_const_sv(block, Nullcv);
4277 const bool exists = CvROOT(cv) || CvXSUB(cv);
4279 #ifdef GV_UNIQUE_CHECK
4280 if (exists && GvUNIQUE(gv)) {
4281 Perl_croak(aTHX_ "Can't redefine unique subroutine %s", name);
4285 /* if the subroutine doesn't exist and wasn't pre-declared
4286 * with a prototype, assume it will be AUTOLOADed,
4287 * skipping the prototype check
4289 if (exists || SvPOK(cv))
4290 cv_ckproto(cv, gv, ps);
4291 /* already defined (or promised)? */
4292 if (exists || GvASSUMECV(gv)) {
4293 if (!block && !attrs) {
4294 if (CvFLAGS(PL_compcv)) {
4295 /* might have had built-in attrs applied */
4296 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
4298 /* just a "sub foo;" when &foo is already defined */
4299 SAVEFREESV(PL_compcv);
4302 /* ahem, death to those who redefine active sort subs */
4303 if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv))
4304 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", name);
4306 if (ckWARN(WARN_REDEFINE)
4308 && (!const_sv || sv_cmp(cv_const_sv(cv), const_sv))))
4310 const line_t oldline = CopLINE(PL_curcop);
4311 if (PL_copline != NOLINE)
4312 CopLINE_set(PL_curcop, PL_copline);
4313 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
4314 CvCONST(cv) ? "Constant subroutine %s redefined"
4315 : "Subroutine %s redefined", name);
4316 CopLINE_set(PL_curcop, oldline);
4324 (void)SvREFCNT_inc(const_sv);
4326 assert(!CvROOT(cv) && !CvCONST(cv));
4327 sv_setpvn((SV*)cv, "", 0); /* prototype is "" */
4328 CvXSUBANY(cv).any_ptr = const_sv;
4329 CvXSUB(cv) = const_sv_xsub;
4334 cv = newCONSTSUB(NULL, name, const_sv);
4337 SvREFCNT_dec(PL_compcv);
4339 PL_sub_generation++;
4346 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>
4347 * before we clobber PL_compcv.
4351 /* Might have had built-in attributes applied -- propagate them. */
4352 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
4353 if (CvGV(cv) && GvSTASH(CvGV(cv)))
4354 stash = GvSTASH(CvGV(cv));
4355 else if (CvSTASH(cv))
4356 stash = CvSTASH(cv);
4358 stash = PL_curstash;
4361 /* possibly about to re-define existing subr -- ignore old cv */
4362 rcv = (SV*)PL_compcv;
4363 if (name && GvSTASH(gv))
4364 stash = GvSTASH(gv);
4366 stash = PL_curstash;
4368 apply_attrs(stash, rcv, attrs, FALSE);
4370 if (cv) { /* must reuse cv if autoloaded */
4372 /* got here with just attrs -- work done, so bug out */
4373 SAVEFREESV(PL_compcv);
4376 /* transfer PL_compcv to cv */
4378 CvFLAGS(cv) = CvFLAGS(PL_compcv);
4379 if (!CvWEAKOUTSIDE(cv))
4380 SvREFCNT_dec(CvOUTSIDE(cv));
4381 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
4382 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv);
4383 CvOUTSIDE(PL_compcv) = 0;
4384 CvPADLIST(cv) = CvPADLIST(PL_compcv);
4385 CvPADLIST(PL_compcv) = 0;
4386 /* inner references to PL_compcv must be fixed up ... */
4387 pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv);
4388 /* ... before we throw it away */
4389 SvREFCNT_dec(PL_compcv);
4390 if (PERLDB_INTER)/* Advice debugger on the new sub. */
4391 ++PL_sub_generation;
4398 PL_sub_generation++;
4402 CvFILE_set_from_cop(cv, PL_curcop);
4403 CvSTASH(cv) = PL_curstash;
4404 #ifdef USE_5005THREADS
4406 if (!CvMUTEXP(cv)) {
4407 New(666, CvMUTEXP(cv), 1, perl_mutex);
4408 MUTEX_INIT(CvMUTEXP(cv));
4410 #endif /* USE_5005THREADS */
4413 sv_setpvn((SV*)cv, ps, ps_len);
4415 if (PL_error_count) {
4419 const char *s = strrchr(name, ':');
4421 if (strEQ(s, "BEGIN")) {
4422 const char not_safe[] =
4423 "BEGIN not safe after errors--compilation aborted";
4424 if (PL_in_eval & EVAL_KEEPERR)
4425 Perl_croak(aTHX_ not_safe);
4427 /* force display of errors found but not reported */
4428 sv_catpv(ERRSV, not_safe);
4429 Perl_croak(aTHX_ "%"SVf, ERRSV);
4438 CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0,
4439 mod(scalarseq(block), OP_LEAVESUBLV));
4442 /* This makes sub {}; work as expected. */
4443 if (block->op_type == OP_STUB) {
4445 block = newSTATEOP(0, Nullch, 0);
4447 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4449 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4450 OpREFCNT_set(CvROOT(cv), 1);
4451 CvSTART(cv) = LINKLIST(CvROOT(cv));
4452 CvROOT(cv)->op_next = 0;
4453 CALL_PEEP(CvSTART(cv));
4455 /* now that optimizer has done its work, adjust pad values */
4457 pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
4460 assert(!CvCONST(cv));
4461 if (ps && !*ps && op_const_sv(block, cv))
4465 if (name || aname) {
4467 const char *tname = (name ? name : aname);
4469 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4470 SV *sv = NEWSV(0,0);
4471 SV *tmpstr = sv_newmortal();
4472 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
4476 Perl_sv_setpvf(aTHX_ sv, "%s:%ld-%ld",
4478 (long)PL_subline, (long)CopLINE(PL_curcop));
4479 gv_efullname3(tmpstr, gv, Nullch);
4480 hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr), SvCUR(tmpstr), sv, 0);
4481 hv = GvHVn(db_postponed);
4482 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvCUR(tmpstr))
4483 && (pcv = GvCV(db_postponed)))
4489 call_sv((SV*)pcv, G_DISCARD);
4493 if ((s = strrchr(tname,':')))
4498 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4501 if (strEQ(s, "BEGIN")) {
4502 const I32 oldscope = PL_scopestack_ix;
4504 SAVECOPFILE(&PL_compiling);
4505 SAVECOPLINE(&PL_compiling);
4508 PL_beginav = newAV();
4509 DEBUG_x( dump_sub(gv) );
4510 av_push(PL_beginav, (SV*)cv);
4511 GvCV(gv) = 0; /* cv has been hijacked */
4512 call_list(oldscope, PL_beginav);
4514 PL_curcop = &PL_compiling;
4515 PL_compiling.op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
4518 else if (strEQ(s, "END") && !PL_error_count) {
4521 DEBUG_x( dump_sub(gv) );
4522 av_unshift(PL_endav, 1);
4523 av_store(PL_endav, 0, (SV*)cv);
4524 GvCV(gv) = 0; /* cv has been hijacked */
4526 else if (strEQ(s, "CHECK") && !PL_error_count) {
4528 PL_checkav = newAV();
4529 DEBUG_x( dump_sub(gv) );
4530 if (PL_main_start && ckWARN(WARN_VOID))
4531 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run CHECK block");
4532 av_unshift(PL_checkav, 1);
4533 av_store(PL_checkav, 0, (SV*)cv);
4534 GvCV(gv) = 0; /* cv has been hijacked */
4536 else if (strEQ(s, "INIT") && !PL_error_count) {
4538 PL_initav = newAV();
4539 DEBUG_x( dump_sub(gv) );
4540 if (PL_main_start && ckWARN(WARN_VOID))
4541 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run INIT block");
4542 av_push(PL_initav, (SV*)cv);
4543 GvCV(gv) = 0; /* cv has been hijacked */
4548 PL_copline = NOLINE;
4553 /* XXX unsafe for threads if eval_owner isn't held */
4555 =for apidoc newCONSTSUB
4557 Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is
4558 eligible for inlining at compile-time.
4564 Perl_newCONSTSUB(pTHX_ HV *stash, char *name, SV *sv)
4570 SAVECOPLINE(PL_curcop);
4571 CopLINE_set(PL_curcop, PL_copline);
4574 PL_hints &= ~HINT_BLOCK_SCOPE;
4577 SAVESPTR(PL_curstash);
4578 SAVECOPSTASH(PL_curcop);
4579 PL_curstash = stash;
4580 CopSTASH_set(PL_curcop,stash);
4583 cv = newXS(name, const_sv_xsub, savepv(CopFILE(PL_curcop)));
4584 CvXSUBANY(cv).any_ptr = sv;
4586 sv_setpvn((SV*)cv, "", 0); /* prototype is "" */
4589 CopSTASH_free(PL_curcop);
4597 =for apidoc U||newXS
4599 Used by C<xsubpp> to hook up XSUBs as Perl subs.
4605 Perl_newXS(pTHX_ char *name, XSUBADDR_t subaddr, char *filename)
4607 GV *gv = gv_fetchpv(name ? name :
4608 (PL_curstash ? "__ANON__" : "__ANON__::__ANON__"),
4609 GV_ADDMULTI, SVt_PVCV);
4612 if ((cv = (name ? GvCV(gv) : Nullcv))) {
4614 /* just a cached method */
4618 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4619 /* already defined (or promised) */
4620 /* XXX It's possible for this HvNAME_get to return null, and get passed into strEQ */
4621 if (ckWARN(WARN_REDEFINE)) {
4622 GV * const gvcv = CvGV(cv);
4624 HV * const stash = GvSTASH(gvcv);
4626 const char *name = HvNAME_get(stash);
4627 if ( strEQ(name,"autouse") ) {
4628 const line_t oldline = CopLINE(PL_curcop);
4629 if (PL_copline != NOLINE)
4630 CopLINE_set(PL_curcop, PL_copline);
4631 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
4632 CvCONST(cv) ? "Constant subroutine %s redefined"
4633 : "Subroutine %s redefined"
4635 CopLINE_set(PL_curcop, oldline);
4645 if (cv) /* must reuse cv if autoloaded */
4648 cv = (CV*)NEWSV(1105,0);
4649 sv_upgrade((SV *)cv, SVt_PVCV);
4653 PL_sub_generation++;
4657 #ifdef USE_5005THREADS
4658 New(666, CvMUTEXP(cv), 1, perl_mutex);
4659 MUTEX_INIT(CvMUTEXP(cv));
4661 #endif /* USE_5005THREADS */
4662 (void)gv_fetchfile(filename);
4663 CvFILE(cv) = (char *)filename; /* NOTE: not copied, as it is expected to be
4664 an external constant string */
4665 CvXSUB(cv) = subaddr;
4668 const char *s = strrchr(name,':');
4674 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4677 if (strEQ(s, "BEGIN")) {
4679 PL_beginav = newAV();
4680 av_push(PL_beginav, (SV*)cv);
4681 GvCV(gv) = 0; /* cv has been hijacked */
4683 else if (strEQ(s, "END")) {
4686 av_unshift(PL_endav, 1);
4687 av_store(PL_endav, 0, (SV*)cv);
4688 GvCV(gv) = 0; /* cv has been hijacked */
4690 else if (strEQ(s, "CHECK")) {
4692 PL_checkav = newAV();
4693 if (PL_main_start && ckWARN(WARN_VOID))
4694 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run CHECK block");
4695 av_unshift(PL_checkav, 1);
4696 av_store(PL_checkav, 0, (SV*)cv);
4697 GvCV(gv) = 0; /* cv has been hijacked */
4699 else if (strEQ(s, "INIT")) {
4701 PL_initav = newAV();
4702 if (PL_main_start && ckWARN(WARN_VOID))
4703 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run INIT block");
4704 av_push(PL_initav, (SV*)cv);
4705 GvCV(gv) = 0; /* cv has been hijacked */
4716 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
4724 name = SvPVx(cSVOPo->op_sv, n_a);
4727 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
4728 #ifdef GV_UNIQUE_CHECK
4730 Perl_croak(aTHX_ "Bad symbol for form (GV is unique)");
4734 if ((cv = GvFORM(gv))) {
4735 if (ckWARN(WARN_REDEFINE)) {
4736 const line_t oldline = CopLINE(PL_curcop);
4737 if (PL_copline != NOLINE)
4738 CopLINE_set(PL_curcop, PL_copline);
4739 Perl_warner(aTHX_ packWARN(WARN_REDEFINE), "Format %s redefined",name);
4740 CopLINE_set(PL_curcop, oldline);
4747 CvFILE_set_from_cop(cv, PL_curcop);
4750 pad_tidy(padtidy_FORMAT);
4751 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4752 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4753 OpREFCNT_set(CvROOT(cv), 1);
4754 CvSTART(cv) = LINKLIST(CvROOT(cv));
4755 CvROOT(cv)->op_next = 0;
4756 CALL_PEEP(CvSTART(cv));
4758 PL_copline = NOLINE;
4763 Perl_newANONLIST(pTHX_ OP *o)
4765 return newUNOP(OP_REFGEN, 0,
4766 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
4770 Perl_newANONHASH(pTHX_ OP *o)
4772 return newUNOP(OP_REFGEN, 0,
4773 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
4777 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
4779 return newANONATTRSUB(floor, proto, Nullop, block);
4783 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
4785 return newUNOP(OP_REFGEN, 0,
4786 newSVOP(OP_ANONCODE, 0,
4787 (SV*)newATTRSUB(floor, 0, proto, attrs, block)));
4791 Perl_oopsAV(pTHX_ OP *o)
4793 switch (o->op_type) {
4795 o->op_type = OP_PADAV;
4796 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4797 return ref(o, OP_RV2AV);
4800 o->op_type = OP_RV2AV;
4801 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4806 if (ckWARN_d(WARN_INTERNAL))
4807 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV");
4814 Perl_oopsHV(pTHX_ OP *o)
4816 switch (o->op_type) {
4819 o->op_type = OP_PADHV;
4820 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4821 return ref(o, OP_RV2HV);
4825 o->op_type = OP_RV2HV;
4826 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
4831 if (ckWARN_d(WARN_INTERNAL))
4832 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV");
4839 Perl_newAVREF(pTHX_ OP *o)
4841 if (o->op_type == OP_PADANY) {
4842 o->op_type = OP_PADAV;
4843 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4846 else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)
4847 && ckWARN(WARN_DEPRECATED)) {
4848 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
4849 "Using an array as a reference is deprecated");
4851 return newUNOP(OP_RV2AV, 0, scalar(o));
4855 Perl_newGVREF(pTHX_ I32 type, OP *o)
4857 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
4858 return newUNOP(OP_NULL, 0, o);
4859 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
4863 Perl_newHVREF(pTHX_ OP *o)
4865 if (o->op_type == OP_PADANY) {
4866 o->op_type = OP_PADHV;
4867 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4870 else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)
4871 && ckWARN(WARN_DEPRECATED)) {
4872 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
4873 "Using a hash as a reference is deprecated");
4875 return newUNOP(OP_RV2HV, 0, scalar(o));
4879 Perl_oopsCV(pTHX_ OP *o)
4881 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
4884 NORETURN_FUNCTION_END;
4888 Perl_newCVREF(pTHX_ I32 flags, OP *o)
4890 return newUNOP(OP_RV2CV, flags, scalar(o));
4894 Perl_newSVREF(pTHX_ OP *o)
4896 if (o->op_type == OP_PADANY) {
4897 o->op_type = OP_PADSV;
4898 o->op_ppaddr = PL_ppaddr[OP_PADSV];
4901 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
4902 o->op_flags |= OPpDONE_SVREF;
4905 return newUNOP(OP_RV2SV, 0, scalar(o));
4908 /* Check routines. See the comments at the top of this file for details
4909 * on when these are called */
4912 Perl_ck_anoncode(pTHX_ OP *o)
4914 cSVOPo->op_targ = pad_add_anon(cSVOPo->op_sv, o->op_type);
4915 cSVOPo->op_sv = Nullsv;
4920 Perl_ck_bitop(pTHX_ OP *o)
4922 #define OP_IS_NUMCOMPARE(op) \
4923 ((op) == OP_LT || (op) == OP_I_LT || \
4924 (op) == OP_GT || (op) == OP_I_GT || \
4925 (op) == OP_LE || (op) == OP_I_LE || \
4926 (op) == OP_GE || (op) == OP_I_GE || \
4927 (op) == OP_EQ || (op) == OP_I_EQ || \
4928 (op) == OP_NE || (op) == OP_I_NE || \
4929 (op) == OP_NCMP || (op) == OP_I_NCMP)
4930 o->op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
4931 if (!(o->op_flags & OPf_STACKED) /* Not an assignment */
4932 && (o->op_type == OP_BIT_OR
4933 || o->op_type == OP_BIT_AND
4934 || o->op_type == OP_BIT_XOR))
4936 const OP * const left = cBINOPo->op_first;
4937 const OP * const right = left->op_sibling;
4938 if ((OP_IS_NUMCOMPARE(left->op_type) &&
4939 (left->op_flags & OPf_PARENS) == 0) ||
4940 (OP_IS_NUMCOMPARE(right->op_type) &&
4941 (right->op_flags & OPf_PARENS) == 0))
4942 if (ckWARN(WARN_PRECEDENCE))
4943 Perl_warner(aTHX_ packWARN(WARN_PRECEDENCE),
4944 "Possible precedence problem on bitwise %c operator",
4945 o->op_type == OP_BIT_OR ? '|'
4946 : o->op_type == OP_BIT_AND ? '&' : '^'
4953 Perl_ck_concat(pTHX_ OP *o)
4955 const OP *kid = cUNOPo->op_first;
4956 if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) &&
4957 !(kUNOP->op_first->op_flags & OPf_MOD))
4958 o->op_flags |= OPf_STACKED;
4963 Perl_ck_spair(pTHX_ OP *o)
4965 if (o->op_flags & OPf_KIDS) {
4968 const OPCODE type = o->op_type;
4969 o = modkids(ck_fun(o), type);
4970 kid = cUNOPo->op_first;
4971 newop = kUNOP->op_first->op_sibling;
4973 (newop->op_sibling ||
4974 !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
4975 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
4976 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
4980 op_free(kUNOP->op_first);
4981 kUNOP->op_first = newop;
4983 o->op_ppaddr = PL_ppaddr[++o->op_type];
4988 Perl_ck_delete(pTHX_ OP *o)
4992 if (o->op_flags & OPf_KIDS) {
4993 OP *kid = cUNOPo->op_first;
4994 switch (kid->op_type) {
4996 o->op_flags |= OPf_SPECIAL;
4999 o->op_private |= OPpSLICE;
5002 o->op_flags |= OPf_SPECIAL;
5007 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element or slice",
5016 Perl_ck_die(pTHX_ OP *o)
5019 if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH;
5025 Perl_ck_eof(pTHX_ OP *o)
5027 const I32 type = o->op_type;
5029 if (o->op_flags & OPf_KIDS) {
5030 if (cLISTOPo->op_first->op_type == OP_STUB) {
5032 o = newUNOP(type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv));
5040 Perl_ck_eval(pTHX_ OP *o)
5042 PL_hints |= HINT_BLOCK_SCOPE;
5043 if (o->op_flags & OPf_KIDS) {
5044 SVOP *kid = (SVOP*)cUNOPo->op_first;
5047 o->op_flags &= ~OPf_KIDS;
5050 else if (kid->op_type == OP_LINESEQ || kid->op_type == OP_STUB) {
5053 cUNOPo->op_first = 0;
5056 NewOp(1101, enter, 1, LOGOP);
5057 enter->op_type = OP_ENTERTRY;
5058 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
5059 enter->op_private = 0;
5061 /* establish postfix order */
5062 enter->op_next = (OP*)enter;
5064 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
5065 o->op_type = OP_LEAVETRY;
5066 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
5067 enter->op_other = o;
5075 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
5077 o->op_targ = (PADOFFSET)PL_hints;
5082 Perl_ck_exit(pTHX_ OP *o)
5085 HV *table = GvHV(PL_hintgv);
5087 SV **svp = hv_fetch(table, "vmsish_exit", 11, FALSE);
5088 if (svp && *svp && SvTRUE(*svp))
5089 o->op_private |= OPpEXIT_VMSISH;
5091 if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH;
5097 Perl_ck_exec(pTHX_ OP *o)
5099 if (o->op_flags & OPf_STACKED) {
5102 kid = cUNOPo->op_first->op_sibling;
5103 if (kid->op_type == OP_RV2GV)
5112 Perl_ck_exists(pTHX_ OP *o)
5115 if (o->op_flags & OPf_KIDS) {
5116 OP *kid = cUNOPo->op_first;
5117 if (kid->op_type == OP_ENTERSUB) {
5118 (void) ref(kid, o->op_type);
5119 if (kid->op_type != OP_RV2CV && !PL_error_count)
5120 Perl_croak(aTHX_ "%s argument is not a subroutine name",
5122 o->op_private |= OPpEXISTS_SUB;
5124 else if (kid->op_type == OP_AELEM)
5125 o->op_flags |= OPf_SPECIAL;
5126 else if (kid->op_type != OP_HELEM)
5127 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element",
5135 Perl_ck_rvconst(pTHX_ register OP *o)
5137 SVOP *kid = (SVOP*)cUNOPo->op_first;
5139 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5140 if (kid->op_type == OP_CONST) {
5144 SV * const kidsv = kid->op_sv;
5147 /* Is it a constant from cv_const_sv()? */
5148 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
5149 SV *rsv = SvRV(kidsv);
5150 const int svtype = SvTYPE(rsv);
5151 const char *badtype = Nullch;
5153 switch (o->op_type) {
5155 if (svtype > SVt_PVMG)
5156 badtype = "a SCALAR";
5159 if (svtype != SVt_PVAV)
5160 badtype = "an ARRAY";
5163 if (svtype != SVt_PVHV) {
5164 if (svtype == SVt_PVAV) { /* pseudohash? */
5165 SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
5166 if (ksv && SvROK(*ksv)
5167 && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
5176 if (svtype != SVt_PVCV)
5181 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
5184 name = SvPV(kidsv, n_a);
5185 if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
5186 const char *badthing = Nullch;
5187 switch (o->op_type) {
5189 badthing = "a SCALAR";
5192 badthing = "an ARRAY";
5195 badthing = "a HASH";
5200 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
5204 * This is a little tricky. We only want to add the symbol if we
5205 * didn't add it in the lexer. Otherwise we get duplicate strict
5206 * warnings. But if we didn't add it in the lexer, we must at
5207 * least pretend like we wanted to add it even if it existed before,
5208 * or we get possible typo warnings. OPpCONST_ENTERED says
5209 * whether the lexer already added THIS instance of this symbol.
5211 iscv = (o->op_type == OP_RV2CV) * 2;
5213 gv = gv_fetchpv(name,
5214 iscv | !(kid->op_private & OPpCONST_ENTERED),
5217 : o->op_type == OP_RV2SV
5219 : o->op_type == OP_RV2AV
5221 : o->op_type == OP_RV2HV
5224 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
5226 kid->op_type = OP_GV;
5227 SvREFCNT_dec(kid->op_sv);
5229 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
5230 kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
5231 SvREFCNT_dec(PAD_SVl(kPADOP->op_padix));
5233 PAD_SETSV(kPADOP->op_padix, (SV*) SvREFCNT_inc(gv));
5235 kid->op_sv = SvREFCNT_inc(gv);
5237 kid->op_private = 0;
5238 kid->op_ppaddr = PL_ppaddr[OP_GV];
5245 Perl_ck_ftst(pTHX_ OP *o)
5247 const I32 type = o->op_type;
5249 if (o->op_flags & OPf_REF) {
5252 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
5253 SVOP *kid = (SVOP*)cUNOPo->op_first;
5255 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5257 OP *newop = newGVOP(type, OPf_REF,
5258 gv_fetchpv(SvPVx(kid->op_sv, n_a), TRUE, SVt_PVIO));
5263 if ((PL_hints & HINT_FILETEST_ACCESS) &&
5264 OP_IS_FILETEST_ACCESS(o))
5265 o->op_private |= OPpFT_ACCESS;
5270 if (type == OP_FTTTY)
5271 o = newGVOP(type, OPf_REF, PL_stdingv);
5273 o = newUNOP(type, 0, newDEFSVOP());
5279 Perl_ck_fun(pTHX_ OP *o)
5281 const int type = o->op_type;
5282 register I32 oa = PL_opargs[type] >> OASHIFT;
5284 if (o->op_flags & OPf_STACKED) {
5285 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
5288 return no_fh_allowed(o);
5291 if (o->op_flags & OPf_KIDS) {
5293 OP **tokid = &cLISTOPo->op_first;
5294 register OP *kid = cLISTOPo->op_first;
5298 if (kid->op_type == OP_PUSHMARK ||
5299 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
5301 tokid = &kid->op_sibling;
5302 kid = kid->op_sibling;
5304 if (!kid && PL_opargs[type] & OA_DEFGV)
5305 *tokid = kid = newDEFSVOP();
5309 sibl = kid->op_sibling;
5312 /* list seen where single (scalar) arg expected? */
5313 if (numargs == 1 && !(oa >> 4)
5314 && kid->op_type == OP_LIST && type != OP_SCALAR)
5316 return too_many_arguments(o,PL_op_desc[type]);
5329 if ((type == OP_PUSH || type == OP_UNSHIFT)
5330 && !kid->op_sibling && ckWARN(WARN_SYNTAX))
5331 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
5332 "Useless use of %s with no values",
5335 if (kid->op_type == OP_CONST &&
5336 (kid->op_private & OPpCONST_BARE))
5338 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5339 OP *newop = newAVREF(newGVOP(OP_GV, 0,
5340 gv_fetchpv(name, TRUE, SVt_PVAV) ));
5341 if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX))
5342 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5343 "Array @%s missing the @ in argument %"IVdf" of %s()",
5344 name, (IV)numargs, PL_op_desc[type]);
5347 kid->op_sibling = sibl;
5350 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
5351 bad_type(numargs, "array", PL_op_desc[type], kid);
5355 if (kid->op_type == OP_CONST &&
5356 (kid->op_private & OPpCONST_BARE))
5358 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5359 OP *newop = newHVREF(newGVOP(OP_GV, 0,
5360 gv_fetchpv(name, TRUE, SVt_PVHV) ));
5361 if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX))
5362 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5363 "Hash %%%s missing the %% in argument %"IVdf" of %s()",
5364 name, (IV)numargs, PL_op_desc[type]);
5367 kid->op_sibling = sibl;
5370 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
5371 bad_type(numargs, "hash", PL_op_desc[type], kid);
5376 OP *newop = newUNOP(OP_NULL, 0, kid);
5377 kid->op_sibling = 0;
5379 newop->op_next = newop;
5381 kid->op_sibling = sibl;
5386 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
5387 if (kid->op_type == OP_CONST &&
5388 (kid->op_private & OPpCONST_BARE))
5390 OP *newop = newGVOP(OP_GV, 0,
5391 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, n_a), TRUE,
5393 if (!(o->op_private & 1) && /* if not unop */
5394 kid == cLISTOPo->op_last)
5395 cLISTOPo->op_last = newop;
5399 else if (kid->op_type == OP_READLINE) {
5400 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
5401 bad_type(numargs, "HANDLE", OP_DESC(o), kid);
5404 I32 flags = OPf_SPECIAL;
5408 /* is this op a FH constructor? */
5409 if (is_handle_constructor(o,numargs)) {
5410 const char *name = Nullch;
5414 /* Set a flag to tell rv2gv to vivify
5415 * need to "prove" flag does not mean something
5416 * else already - NI-S 1999/05/07
5419 if (kid->op_type == OP_PADSV) {
5420 /*XXX DAPM 2002.08.25 tmp assert test */
5421 /*XXX*/ assert(av_fetch(PL_comppad_name, (kid->op_targ), FALSE));
5422 /*XXX*/ assert(*av_fetch(PL_comppad_name, (kid->op_targ), FALSE));
5424 name = PAD_COMPNAME_PV(kid->op_targ);
5425 /* SvCUR of a pad namesv can't be trusted
5426 * (see PL_generation), so calc its length
5432 else if (kid->op_type == OP_RV2SV
5433 && kUNOP->op_first->op_type == OP_GV)
5435 GV *gv = cGVOPx_gv(kUNOP->op_first);
5437 len = GvNAMELEN(gv);
5439 else if (kid->op_type == OP_AELEM
5440 || kid->op_type == OP_HELEM)
5445 if ((op = ((BINOP*)kid)->op_first)) {
5446 SV *tmpstr = Nullsv;
5448 kid->op_type == OP_AELEM ?
5450 if (((op->op_type == OP_RV2AV) ||
5451 (op->op_type == OP_RV2HV)) &&
5452 (op = ((UNOP*)op)->op_first) &&
5453 (op->op_type == OP_GV)) {
5454 /* packagevar $a[] or $h{} */
5455 GV *gv = cGVOPx_gv(op);
5463 else if (op->op_type == OP_PADAV
5464 || op->op_type == OP_PADHV) {
5465 /* lexicalvar $a[] or $h{} */
5466 const char *padname =
5467 PAD_COMPNAME_PV(op->op_targ);
5477 name = SvPV(tmpstr, len);
5482 name = "__ANONIO__";
5489 targ = pad_alloc(OP_RV2GV, SVs_PADTMP);
5490 namesv = PAD_SVl(targ);
5491 (void)SvUPGRADE(namesv, SVt_PV);
5493 sv_setpvn(namesv, "$", 1);
5494 sv_catpvn(namesv, name, len);
5497 kid->op_sibling = 0;
5498 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
5499 kid->op_targ = targ;
5500 kid->op_private |= priv;
5502 kid->op_sibling = sibl;
5508 mod(scalar(kid), type);
5512 tokid = &kid->op_sibling;
5513 kid = kid->op_sibling;
5515 o->op_private |= numargs;
5517 return too_many_arguments(o,OP_DESC(o));
5520 else if (PL_opargs[type] & OA_DEFGV) {
5522 return newUNOP(type, 0, newDEFSVOP());
5526 while (oa & OA_OPTIONAL)
5528 if (oa && oa != OA_LIST)
5529 return too_few_arguments(o,OP_DESC(o));
5535 Perl_ck_glob(pTHX_ OP *o)
5540 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
5541 append_elem(OP_GLOB, o, newDEFSVOP());
5543 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV))
5544 && GvCVu(gv) && GvIMPORTED_CV(gv)))
5546 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5549 #if !defined(PERL_EXTERNAL_GLOB)
5550 /* XXX this can be tightened up and made more failsafe. */
5551 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
5554 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
5555 newSVpvn("File::Glob", 10), Nullsv, Nullsv, Nullsv);
5556 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5557 glob_gv = gv_fetchpv("File::Glob::csh_glob", FALSE, SVt_PVCV);
5558 GvCV(gv) = GvCV(glob_gv);
5559 (void)SvREFCNT_inc((SV*)GvCV(gv));
5560 GvIMPORTED_CV_on(gv);
5563 #endif /* PERL_EXTERNAL_GLOB */
5565 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
5566 append_elem(OP_GLOB, o,
5567 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
5568 o->op_type = OP_LIST;
5569 o->op_ppaddr = PL_ppaddr[OP_LIST];
5570 cLISTOPo->op_first->op_type = OP_PUSHMARK;
5571 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
5572 cLISTOPo->op_first->op_targ = 0;
5573 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
5574 append_elem(OP_LIST, o,
5575 scalar(newUNOP(OP_RV2CV, 0,
5576 newGVOP(OP_GV, 0, gv)))));
5577 o = newUNOP(OP_NULL, 0, ck_subr(o));
5578 o->op_targ = OP_GLOB; /* hint at what it used to be */
5581 gv = newGVgen("main");
5583 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
5589 Perl_ck_grep(pTHX_ OP *o)
5593 const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
5595 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
5596 NewOp(1101, gwop, 1, LOGOP);
5598 if (o->op_flags & OPf_STACKED) {
5601 kid = cLISTOPo->op_first->op_sibling;
5602 if (!cUNOPx(kid)->op_next)
5603 Perl_croak(aTHX_ "panic: ck_grep");
5604 for (k = cUNOPx(kid)->op_first; k; k = k->op_next) {
5607 kid->op_next = (OP*)gwop;
5608 o->op_flags &= ~OPf_STACKED;
5610 kid = cLISTOPo->op_first->op_sibling;
5611 if (type == OP_MAPWHILE)
5618 kid = cLISTOPo->op_first->op_sibling;
5619 if (kid->op_type != OP_NULL)
5620 Perl_croak(aTHX_ "panic: ck_grep");
5621 kid = kUNOP->op_first;
5623 gwop->op_type = type;
5624 gwop->op_ppaddr = PL_ppaddr[type];
5625 gwop->op_first = listkids(o);
5626 gwop->op_flags |= OPf_KIDS;
5627 gwop->op_private = 1;
5628 gwop->op_other = LINKLIST(kid);
5629 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
5630 kid->op_next = (OP*)gwop;
5632 kid = cLISTOPo->op_first->op_sibling;
5633 if (!kid || !kid->op_sibling)
5634 return too_few_arguments(o,OP_DESC(o));
5635 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
5636 mod(kid, OP_GREPSTART);
5642 Perl_ck_index(pTHX_ OP *o)
5644 if (o->op_flags & OPf_KIDS) {
5645 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5647 kid = kid->op_sibling; /* get past "big" */
5648 if (kid && kid->op_type == OP_CONST)
5649 fbm_compile(((SVOP*)kid)->op_sv, 0);
5655 Perl_ck_lengthconst(pTHX_ OP *o)
5657 /* XXX length optimization goes here */
5662 Perl_ck_lfun(pTHX_ OP *o)
5664 const OPCODE type = o->op_type;
5665 return modkids(ck_fun(o), type);
5669 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
5671 if ((o->op_flags & OPf_KIDS) && ckWARN2(WARN_DEPRECATED, WARN_SYNTAX)) {
5672 switch (cUNOPo->op_first->op_type) {
5674 /* This is needed for
5675 if (defined %stash::)
5676 to work. Do not break Tk.
5678 break; /* Globals via GV can be undef */
5680 case OP_AASSIGN: /* Is this a good idea? */
5681 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5682 "defined(@array) is deprecated");
5683 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5684 "\t(Maybe you should just omit the defined()?)\n");
5687 /* This is needed for
5688 if (defined %stash::)
5689 to work. Do not break Tk.
5691 break; /* Globals via GV can be undef */
5693 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5694 "defined(%%hash) is deprecated");
5695 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5696 "\t(Maybe you should just omit the defined()?)\n");
5707 Perl_ck_rfun(pTHX_ OP *o)
5709 const OPCODE type = o->op_type;
5710 return refkids(ck_fun(o), type);
5714 Perl_ck_listiob(pTHX_ OP *o)
5718 kid = cLISTOPo->op_first;
5721 kid = cLISTOPo->op_first;
5723 if (kid->op_type == OP_PUSHMARK)
5724 kid = kid->op_sibling;
5725 if (kid && o->op_flags & OPf_STACKED)
5726 kid = kid->op_sibling;
5727 else if (kid && !kid->op_sibling) { /* print HANDLE; */
5728 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
5729 o->op_flags |= OPf_STACKED; /* make it a filehandle */
5730 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
5731 cLISTOPo->op_first->op_sibling = kid;
5732 cLISTOPo->op_last = kid;
5733 kid = kid->op_sibling;
5738 append_elem(o->op_type, o, newDEFSVOP());
5744 Perl_ck_sassign(pTHX_ OP *o)
5746 OP *kid = cLISTOPo->op_first;
5747 /* has a disposable target? */
5748 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
5749 && !(kid->op_flags & OPf_STACKED)
5750 /* Cannot steal the second time! */
5751 && !(kid->op_private & OPpTARGET_MY))
5753 OP *kkid = kid->op_sibling;
5755 /* Can just relocate the target. */
5756 if (kkid && kkid->op_type == OP_PADSV
5757 && !(kkid->op_private & OPpLVAL_INTRO))
5759 kid->op_targ = kkid->op_targ;
5761 /* Now we do not need PADSV and SASSIGN. */
5762 kid->op_sibling = o->op_sibling; /* NULL */
5763 cLISTOPo->op_first = NULL;
5766 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
5770 /* optimise C<my $x = undef> to C<my $x> */
5771 if (kid->op_type == OP_UNDEF) {
5772 OP *kkid = kid->op_sibling;
5773 if (kkid && kkid->op_type == OP_PADSV
5774 && (kkid->op_private & OPpLVAL_INTRO))
5776 cLISTOPo->op_first = NULL;
5777 kid->op_sibling = NULL;
5787 Perl_ck_match(pTHX_ OP *o)
5789 o->op_private |= OPpRUNTIME;
5794 Perl_ck_method(pTHX_ OP *o)
5796 OP *kid = cUNOPo->op_first;
5797 if (kid->op_type == OP_CONST) {
5798 SV* sv = kSVOP->op_sv;
5799 if (!(strchr(SvPVX_const(sv), ':') || strchr(SvPVX_const(sv), '\''))) {
5801 if (!SvREADONLY(sv) || !SvFAKE(sv)) {
5802 sv = newSVpvn_share(SvPVX_const(sv), SvCUR(sv), 0);
5805 kSVOP->op_sv = Nullsv;
5807 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
5816 Perl_ck_null(pTHX_ OP *o)
5822 Perl_ck_open(pTHX_ OP *o)
5824 HV *table = GvHV(PL_hintgv);
5828 svp = hv_fetch(table, "open_IN", 7, FALSE);
5830 mode = mode_from_discipline(*svp);
5831 if (mode & O_BINARY)
5832 o->op_private |= OPpOPEN_IN_RAW;
5833 else if (mode & O_TEXT)
5834 o->op_private |= OPpOPEN_IN_CRLF;
5837 svp = hv_fetch(table, "open_OUT", 8, FALSE);
5839 mode = mode_from_discipline(*svp);
5840 if (mode & O_BINARY)
5841 o->op_private |= OPpOPEN_OUT_RAW;
5842 else if (mode & O_TEXT)
5843 o->op_private |= OPpOPEN_OUT_CRLF;
5846 if (o->op_type == OP_BACKTICK)
5849 /* In case of three-arg dup open remove strictness
5850 * from the last arg if it is a bareword. */
5851 OP *first = cLISTOPx(o)->op_first; /* The pushmark. */
5852 OP *last = cLISTOPx(o)->op_last; /* The bareword. */
5856 if ((last->op_type == OP_CONST) && /* The bareword. */
5857 (last->op_private & OPpCONST_BARE) &&
5858 (last->op_private & OPpCONST_STRICT) &&
5859 (oa = first->op_sibling) && /* The fh. */
5860 (oa = oa->op_sibling) && /* The mode. */
5861 SvPOK(((SVOP*)oa)->op_sv) &&
5862 (mode = SvPVX_const(((SVOP*)oa)->op_sv)) &&
5863 mode[0] == '>' && mode[1] == '&' && /* A dup open. */
5864 (last == oa->op_sibling)) /* The bareword. */
5865 last->op_private &= ~OPpCONST_STRICT;
5871 Perl_ck_repeat(pTHX_ OP *o)
5873 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
5874 o->op_private |= OPpREPEAT_DOLIST;
5875 cBINOPo->op_first = force_list(cBINOPo->op_first);
5883 Perl_ck_require(pTHX_ OP *o)
5887 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
5888 SVOP *kid = (SVOP*)cUNOPo->op_first;
5890 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5891 SV *sv = kid->op_sv;
5892 U32 was_readonly = SvREADONLY(sv);
5897 sv_force_normal_flags(sv, 0);
5898 assert(!SvREADONLY(sv));
5905 for (s = SvPVX(sv); *s; s++) {
5906 if (*s == ':' && s[1] == ':') {
5908 Move(s+2, s+1, strlen(s+2)+1, char);
5909 SvCUR_set(sv, SvCUR(sv) - 1);
5912 sv_catpvn(sv, ".pm", 3);
5913 SvFLAGS(sv) |= was_readonly;
5917 /* handle override, if any */
5918 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
5919 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv)))
5920 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
5922 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
5923 OP *kid = cUNOPo->op_first;
5924 cUNOPo->op_first = 0;
5926 return ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
5927 append_elem(OP_LIST, kid,
5928 scalar(newUNOP(OP_RV2CV, 0,
5937 Perl_ck_return(pTHX_ OP *o)
5939 if (CvLVALUE(PL_compcv)) {
5941 for (kid = cLISTOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
5942 mod(kid, OP_LEAVESUBLV);
5949 Perl_ck_retarget(pTHX_ OP *o)
5951 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
5958 Perl_ck_select(pTHX_ OP *o)
5961 if (o->op_flags & OPf_KIDS) {
5962 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5963 if (kid && kid->op_sibling) {
5964 o->op_type = OP_SSELECT;
5965 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
5967 return fold_constants(o);
5971 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5972 if (kid && kid->op_type == OP_RV2GV)
5973 kid->op_private &= ~HINT_STRICT_REFS;
5978 Perl_ck_shift(pTHX_ OP *o)
5980 const I32 type = o->op_type;
5982 if (!(o->op_flags & OPf_KIDS)) {
5986 #ifdef USE_5005THREADS
5987 if (!CvUNIQUE(PL_compcv)) {
5988 argop = newOP(OP_PADAV, OPf_REF);
5989 argop->op_targ = 0; /* PAD_SV(0) is @_ */
5992 argop = newUNOP(OP_RV2AV, 0,
5993 scalar(newGVOP(OP_GV, 0,
5994 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5997 argop = newUNOP(OP_RV2AV, 0,
5998 scalar(newGVOP(OP_GV, 0, CvUNIQUE(PL_compcv) ? PL_argvgv : PL_defgv)));
5999 #endif /* USE_5005THREADS */
6000 return newUNOP(type, 0, scalar(argop));
6002 return scalar(modkids(ck_fun(o), type));
6006 Perl_ck_sort(pTHX_ OP *o)
6010 if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
6012 firstkid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6013 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
6015 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */
6017 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
6019 if (kid->op_type == OP_SCOPE) {
6023 else if (kid->op_type == OP_LEAVE) {
6024 if (o->op_type == OP_SORT) {
6025 op_null(kid); /* wipe out leave */
6028 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
6029 if (k->op_next == kid)
6031 /* don't descend into loops */
6032 else if (k->op_type == OP_ENTERLOOP
6033 || k->op_type == OP_ENTERITER)
6035 k = cLOOPx(k)->op_lastop;
6040 kid->op_next = 0; /* just disconnect the leave */
6041 k = kLISTOP->op_first;
6046 if (o->op_type == OP_SORT) {
6047 /* provide scalar context for comparison function/block */
6053 o->op_flags |= OPf_SPECIAL;
6055 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
6058 firstkid = firstkid->op_sibling;
6061 /* provide list context for arguments */
6062 if (o->op_type == OP_SORT)
6069 S_simplify_sort(pTHX_ OP *o)
6071 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6076 if (!(o->op_flags & OPf_STACKED))
6078 GvMULTI_on(gv_fetchpv("a", TRUE, SVt_PV));
6079 GvMULTI_on(gv_fetchpv("b", TRUE, SVt_PV));
6080 kid = kUNOP->op_first; /* get past null */
6081 if (kid->op_type != OP_SCOPE)
6083 kid = kLISTOP->op_last; /* get past scope */
6084 switch(kid->op_type) {
6092 k = kid; /* remember this node*/
6093 if (kBINOP->op_first->op_type != OP_RV2SV)
6095 kid = kBINOP->op_first; /* get past cmp */
6096 if (kUNOP->op_first->op_type != OP_GV)
6098 kid = kUNOP->op_first; /* get past rv2sv */
6100 if (GvSTASH(gv) != PL_curstash)
6102 gvname = GvNAME(gv);
6103 if (*gvname == 'a' && gvname[1] == '\0')
6105 else if (*gvname == 'b' && gvname[1] == '\0')
6110 kid = k; /* back to cmp */
6111 if (kBINOP->op_last->op_type != OP_RV2SV)
6113 kid = kBINOP->op_last; /* down to 2nd arg */
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);
6122 ? !(*gvname == 'a' && gvname[1] == '\0')
6123 : !(*gvname == 'b' && gvname[1] == '\0'))
6125 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
6127 o->op_private |= OPpSORT_DESCEND;
6128 if (k->op_type == OP_NCMP)
6129 o->op_private |= OPpSORT_NUMERIC;
6130 if (k->op_type == OP_I_NCMP)
6131 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
6132 kid = cLISTOPo->op_first->op_sibling;
6133 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
6134 op_free(kid); /* then delete it */
6138 Perl_ck_split(pTHX_ OP *o)
6142 if (o->op_flags & OPf_STACKED)
6143 return no_fh_allowed(o);
6145 kid = cLISTOPo->op_first;
6146 if (kid->op_type != OP_NULL)
6147 Perl_croak(aTHX_ "panic: ck_split");
6148 kid = kid->op_sibling;
6149 op_free(cLISTOPo->op_first);
6150 cLISTOPo->op_first = kid;
6152 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvn(" ", 1));
6153 cLISTOPo->op_last = kid; /* There was only one element previously */
6156 if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) {
6157 OP *sibl = kid->op_sibling;
6158 kid->op_sibling = 0;
6159 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
6160 if (cLISTOPo->op_first == cLISTOPo->op_last)
6161 cLISTOPo->op_last = kid;
6162 cLISTOPo->op_first = kid;
6163 kid->op_sibling = sibl;
6166 kid->op_type = OP_PUSHRE;
6167 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
6169 if (ckWARN(WARN_REGEXP) && ((PMOP *)kid)->op_pmflags & PMf_GLOBAL) {
6170 Perl_warner(aTHX_ packWARN(WARN_REGEXP),
6171 "Use of /g modifier is meaningless in split");
6174 if (!kid->op_sibling)
6175 append_elem(OP_SPLIT, o, newDEFSVOP());
6177 kid = kid->op_sibling;
6180 if (!kid->op_sibling)
6181 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
6183 kid = kid->op_sibling;
6186 if (kid->op_sibling)
6187 return too_many_arguments(o,OP_DESC(o));
6193 Perl_ck_join(pTHX_ OP *o)
6195 if (ckWARN(WARN_SYNTAX)) {
6196 const OP *kid = cLISTOPo->op_first->op_sibling;
6197 if (kid && kid->op_type == OP_MATCH) {
6198 const REGEXP *re = PM_GETRE(kPMOP);
6199 const char *pmstr = re ? re->precomp : "STRING";
6200 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
6201 "/%s/ should probably be written as \"%s\"",
6209 Perl_ck_subr(pTHX_ OP *o)
6211 OP *prev = ((cUNOPo->op_first->op_sibling)
6212 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
6213 OP *o2 = prev->op_sibling;
6220 I32 contextclass = 0;
6224 o->op_private |= OPpENTERSUB_HASTARG;
6225 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
6226 if (cvop->op_type == OP_RV2CV) {
6228 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
6229 op_null(cvop); /* disable rv2cv */
6230 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
6231 if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) {
6232 GV *gv = cGVOPx_gv(tmpop);
6235 tmpop->op_private |= OPpEARLY_CV;
6236 else if (SvPOK(cv)) {
6237 namegv = CvANON(cv) ? gv : CvGV(cv);
6238 proto = SvPV((SV*)cv, n_a);
6242 else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
6243 if (o2->op_type == OP_CONST)
6244 o2->op_private &= ~OPpCONST_STRICT;
6245 else if (o2->op_type == OP_LIST) {
6246 OP *o = ((UNOP*)o2)->op_first->op_sibling;
6247 if (o && o->op_type == OP_CONST)
6248 o->op_private &= ~OPpCONST_STRICT;
6251 o->op_private |= (PL_hints & HINT_STRICT_REFS);
6252 if (PERLDB_SUB && PL_curstash != PL_debstash)
6253 o->op_private |= OPpENTERSUB_DB;
6254 while (o2 != cvop) {
6258 return too_many_arguments(o, gv_ename(namegv));
6276 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
6278 arg == 1 ? "block or sub {}" : "sub {}",
6279 gv_ename(namegv), o2);
6282 /* '*' allows any scalar type, including bareword */
6285 if (o2->op_type == OP_RV2GV)
6286 goto wrapref; /* autoconvert GLOB -> GLOBref */
6287 else if (o2->op_type == OP_CONST)
6288 o2->op_private &= ~OPpCONST_STRICT;
6289 else if (o2->op_type == OP_ENTERSUB) {
6290 /* accidental subroutine, revert to bareword */
6291 OP *gvop = ((UNOP*)o2)->op_first;
6292 if (gvop && gvop->op_type == OP_NULL) {
6293 gvop = ((UNOP*)gvop)->op_first;
6295 for (; gvop->op_sibling; gvop = gvop->op_sibling)
6298 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
6299 (gvop = ((UNOP*)gvop)->op_first) &&
6300 gvop->op_type == OP_GV)
6302 GV *gv = cGVOPx_gv(gvop);
6303 OP *sibling = o2->op_sibling;
6304 SV *n = newSVpvn("",0);
6306 gv_fullname4(n, gv, "", FALSE);
6307 o2 = newSVOP(OP_CONST, 0, n);
6308 prev->op_sibling = o2;
6309 o2->op_sibling = sibling;
6325 if (contextclass++ == 0) {
6326 e = strchr(proto, ']');
6327 if (!e || e == proto)
6340 while (*--p != '[');
6341 bad_type(arg, Perl_form(aTHX_ "one of %s", p),
6342 gv_ename(namegv), o2);
6348 if (o2->op_type == OP_RV2GV)
6351 bad_type(arg, "symbol", gv_ename(namegv), o2);
6354 if (o2->op_type == OP_ENTERSUB)
6357 bad_type(arg, "subroutine entry", gv_ename(namegv), o2);
6360 if (o2->op_type == OP_RV2SV ||
6361 o2->op_type == OP_PADSV ||
6362 o2->op_type == OP_HELEM ||
6363 o2->op_type == OP_AELEM ||
6364 o2->op_type == OP_THREADSV)
6367 bad_type(arg, "scalar", gv_ename(namegv), o2);
6370 if (o2->op_type == OP_RV2AV ||
6371 o2->op_type == OP_PADAV)
6374 bad_type(arg, "array", gv_ename(namegv), o2);
6377 if (o2->op_type == OP_RV2HV ||
6378 o2->op_type == OP_PADHV)
6381 bad_type(arg, "hash", gv_ename(namegv), o2);
6386 OP* sib = kid->op_sibling;
6387 kid->op_sibling = 0;
6388 o2 = newUNOP(OP_REFGEN, 0, kid);
6389 o2->op_sibling = sib;
6390 prev->op_sibling = o2;
6392 if (contextclass && e) {
6407 Perl_croak(aTHX_ "Malformed prototype for %s: %"SVf,
6408 gv_ename(namegv), cv);
6413 mod(o2, OP_ENTERSUB);
6415 o2 = o2->op_sibling;
6417 if (proto && !optional &&
6418 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
6419 return too_few_arguments(o, gv_ename(namegv));
6424 Perl_ck_svconst(pTHX_ OP *o)
6426 SvREADONLY_on(cSVOPo->op_sv);
6431 Perl_ck_trunc(pTHX_ OP *o)
6433 if (o->op_flags & OPf_KIDS) {
6434 SVOP *kid = (SVOP*)cUNOPo->op_first;
6436 if (kid->op_type == OP_NULL)
6437 kid = (SVOP*)kid->op_sibling;
6438 if (kid && kid->op_type == OP_CONST &&
6439 (kid->op_private & OPpCONST_BARE))
6441 o->op_flags |= OPf_SPECIAL;
6442 kid->op_private &= ~OPpCONST_STRICT;
6449 Perl_ck_substr(pTHX_ OP *o)
6452 if ((o->op_flags & OPf_KIDS) && o->op_private == 4) {
6453 OP *kid = cLISTOPo->op_first;
6455 if (kid->op_type == OP_NULL)
6456 kid = kid->op_sibling;
6458 kid->op_flags |= OPf_MOD;
6464 /* A peephole optimizer. We visit the ops in the order they're to execute.
6465 * See the comments at the top of this file for more details about when
6466 * peep() is called */
6469 Perl_peep(pTHX_ register OP *o)
6471 register OP* oldop = 0;
6474 if (!o || o->op_seq)
6478 SAVEVPTR(PL_curcop);
6479 for (; o; o = o->op_next) {
6482 /* The special value -1 is used by the B::C compiler backend to indicate
6483 * that an op is statically defined and should not be freed */
6484 if (!PL_op_seqmax || PL_op_seqmax == (U16)-1)
6487 switch (o->op_type) {
6491 PL_curcop = ((COP*)o); /* for warnings */
6492 o->op_seq = PL_op_seqmax++;
6496 if (cSVOPo->op_private & OPpCONST_STRICT)
6497 no_bareword_allowed(o);
6499 case OP_METHOD_NAMED:
6500 /* Relocate sv to the pad for thread safety.
6501 * Despite being a "constant", the SV is written to,
6502 * for reference counts, sv_upgrade() etc. */
6504 const PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP);
6505 if (o->op_type == OP_CONST && SvPADTMP(cSVOPo->op_sv)) {
6506 /* If op_sv is already a PADTMP then it is being used by
6507 * some pad, so make a copy. */
6508 sv_setsv(PAD_SVl(ix),cSVOPo->op_sv);
6509 SvREADONLY_on(PAD_SVl(ix));
6510 SvREFCNT_dec(cSVOPo->op_sv);
6513 SvREFCNT_dec(PAD_SVl(ix));
6514 SvPADTMP_on(cSVOPo->op_sv);
6515 PAD_SETSV(ix, cSVOPo->op_sv);
6516 /* XXX I don't know how this isn't readonly already. */
6517 SvREADONLY_on(PAD_SVl(ix));
6519 cSVOPo->op_sv = Nullsv;
6523 o->op_seq = PL_op_seqmax++;
6527 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
6528 if (o->op_next->op_private & OPpTARGET_MY) {
6529 if (o->op_flags & OPf_STACKED) /* chained concats */
6530 goto ignore_optimization;
6532 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
6533 o->op_targ = o->op_next->op_targ;
6534 o->op_next->op_targ = 0;
6535 o->op_private |= OPpTARGET_MY;
6538 op_null(o->op_next);
6540 ignore_optimization:
6541 o->op_seq = PL_op_seqmax++;
6544 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
6545 o->op_seq = PL_op_seqmax++;
6546 break; /* Scalar stub must produce undef. List stub is noop */
6550 if (o->op_targ == OP_NEXTSTATE
6551 || o->op_targ == OP_DBSTATE
6552 || o->op_targ == OP_SETSTATE)
6554 PL_curcop = ((COP*)o);
6556 /* XXX: We avoid setting op_seq here to prevent later calls
6557 to peep() from mistakenly concluding that optimisation
6558 has already occurred. This doesn't fix the real problem,
6559 though (See 20010220.007). AMS 20010719 */
6560 if (oldop && o->op_next) {
6561 oldop->op_next = o->op_next;
6569 if (oldop && o->op_next) {
6570 oldop->op_next = o->op_next;
6573 o->op_seq = PL_op_seqmax++;
6578 if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) {
6579 OP* pop = (o->op_type == OP_PADAV) ?
6580 o->op_next : o->op_next->op_next;
6582 if (pop && pop->op_type == OP_CONST &&
6583 ((PL_op = pop->op_next)) &&
6584 pop->op_next->op_type == OP_AELEM &&
6585 !(pop->op_next->op_private &
6586 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) &&
6587 (i = SvIV(((SVOP*)pop)->op_sv) - PL_curcop->cop_arybase)
6592 if (cSVOPx(pop)->op_private & OPpCONST_STRICT)
6593 no_bareword_allowed(pop);
6594 if (o->op_type == OP_GV)
6595 op_null(o->op_next);
6596 op_null(pop->op_next);
6598 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
6599 o->op_next = pop->op_next->op_next;
6600 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
6601 o->op_private = (U8)i;
6602 if (o->op_type == OP_GV) {
6607 o->op_flags |= OPf_SPECIAL;
6608 o->op_type = OP_AELEMFAST;
6610 o->op_seq = PL_op_seqmax++;
6614 if (o->op_next->op_type == OP_RV2SV) {
6615 if (!(o->op_next->op_private & OPpDEREF)) {
6616 op_null(o->op_next);
6617 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
6619 o->op_next = o->op_next->op_next;
6620 o->op_type = OP_GVSV;
6621 o->op_ppaddr = PL_ppaddr[OP_GVSV];
6624 else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
6626 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
6627 /* XXX could check prototype here instead of just carping */
6628 SV *sv = sv_newmortal();
6629 gv_efullname3(sv, gv, Nullch);
6630 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
6631 "%"SVf"() called too early to check prototype",
6635 else if (o->op_next->op_type == OP_READLINE
6636 && o->op_next->op_next->op_type == OP_CONCAT
6637 && (o->op_next->op_next->op_flags & OPf_STACKED))
6639 /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */
6640 o->op_type = OP_RCATLINE;
6641 o->op_flags |= OPf_STACKED;
6642 o->op_ppaddr = PL_ppaddr[OP_RCATLINE];
6643 op_null(o->op_next->op_next);
6644 op_null(o->op_next);
6647 o->op_seq = PL_op_seqmax++;
6658 o->op_seq = PL_op_seqmax++;
6659 while (cLOGOP->op_other->op_type == OP_NULL)
6660 cLOGOP->op_other = cLOGOP->op_other->op_next;
6661 peep(cLOGOP->op_other); /* Recursive calls are not replaced by fptr calls */
6666 o->op_seq = PL_op_seqmax++;
6667 while (cLOOP->op_redoop->op_type == OP_NULL)
6668 cLOOP->op_redoop = cLOOP->op_redoop->op_next;
6669 peep(cLOOP->op_redoop);
6670 while (cLOOP->op_nextop->op_type == OP_NULL)
6671 cLOOP->op_nextop = cLOOP->op_nextop->op_next;
6672 peep(cLOOP->op_nextop);
6673 while (cLOOP->op_lastop->op_type == OP_NULL)
6674 cLOOP->op_lastop = cLOOP->op_lastop->op_next;
6675 peep(cLOOP->op_lastop);
6681 o->op_seq = PL_op_seqmax++;
6682 while (cPMOP->op_pmreplstart &&
6683 cPMOP->op_pmreplstart->op_type == OP_NULL)
6684 cPMOP->op_pmreplstart = cPMOP->op_pmreplstart->op_next;
6685 peep(cPMOP->op_pmreplstart);
6689 o->op_seq = PL_op_seqmax++;
6690 if (ckWARN(WARN_SYNTAX) && o->op_next
6691 && o->op_next->op_type == OP_NEXTSTATE) {
6692 if (o->op_next->op_sibling &&
6693 o->op_next->op_sibling->op_type != OP_EXIT &&
6694 o->op_next->op_sibling->op_type != OP_WARN &&
6695 o->op_next->op_sibling->op_type != OP_DIE) {
6696 const line_t oldline = CopLINE(PL_curcop);
6698 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_next));
6699 Perl_warner(aTHX_ packWARN(WARN_EXEC),
6700 "Statement unlikely to be reached");
6701 Perl_warner(aTHX_ packWARN(WARN_EXEC),
6702 "\t(Maybe you meant system() when you said exec()?)\n");
6703 CopLINE_set(PL_curcop, oldline);
6712 SV **svp, **indsvp, *sv;
6717 o->op_seq = PL_op_seqmax++;
6719 if (((BINOP*)o)->op_last->op_type != OP_CONST)
6722 /* Make the CONST have a shared SV */
6723 svp = cSVOPx_svp(((BINOP*)o)->op_last);
6724 if ((!SvFAKE(sv = *svp) || !SvREADONLY(sv)) && !IS_PADCONST(sv)) {
6725 key = SvPV(sv, keylen);
6726 lexname = newSVpvn_share(key,
6727 SvUTF8(sv) ? -(I32)keylen : keylen,
6733 if ((o->op_private & (OPpLVAL_INTRO)))
6736 rop = (UNOP*)((BINOP*)o)->op_first;
6737 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6739 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6740 if (!(SvFLAGS(lexname) & SVpad_TYPED))
6742 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6743 if (!fields || !GvHV(*fields))
6745 key = SvPV(*svp, keylen);
6746 indsvp = hv_fetch(GvHV(*fields), key,
6747 SvUTF8(*svp) ? -(I32)keylen : keylen, FALSE);
6749 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" in variable %s of type %s",
6750 key, SvPV(lexname, n_a), HvNAME_get(SvSTASH(lexname)));
6752 ind = SvIV(*indsvp);
6754 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6755 rop->op_type = OP_RV2AV;
6756 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6757 o->op_type = OP_AELEM;
6758 o->op_ppaddr = PL_ppaddr[OP_AELEM];
6760 if (SvREADONLY(*svp))
6762 SvFLAGS(sv) |= (SvFLAGS(*svp)
6763 & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
6773 SV **svp, **indsvp, *sv;
6777 SVOP *first_key_op, *key_op;
6779 o->op_seq = PL_op_seqmax++;
6780 if ((o->op_private & (OPpLVAL_INTRO))
6781 /* I bet there's always a pushmark... */
6782 || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST)
6783 /* hmmm, no optimization if list contains only one key. */
6785 rop = (UNOP*)((LISTOP*)o)->op_last;
6786 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6788 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6789 if (!(SvFLAGS(lexname) & SVpad_TYPED))
6791 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6792 if (!fields || !GvHV(*fields))
6794 /* Again guessing that the pushmark can be jumped over.... */
6795 first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling)
6796 ->op_first->op_sibling;
6797 /* Check that the key list contains only constants. */
6798 for (key_op = first_key_op; key_op;
6799 key_op = (SVOP*)key_op->op_sibling)
6800 if (key_op->op_type != OP_CONST)
6804 rop->op_type = OP_RV2AV;
6805 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6806 o->op_type = OP_ASLICE;
6807 o->op_ppaddr = PL_ppaddr[OP_ASLICE];
6808 for (key_op = first_key_op; key_op;
6809 key_op = (SVOP*)key_op->op_sibling) {
6810 svp = cSVOPx_svp(key_op);
6811 key = SvPV(*svp, keylen);
6812 indsvp = hv_fetch(GvHV(*fields), key,
6813 SvUTF8(*svp) ? -(I32)keylen : keylen, FALSE);
6815 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" "
6816 "in variable %s of type %s",
6817 key, SvPV(lexname, n_a), HvNAME_get(SvSTASH(lexname)));
6819 ind = SvIV(*indsvp);
6821 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6823 if (SvREADONLY(*svp))
6825 SvFLAGS(sv) |= (SvFLAGS(*svp)
6826 & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
6834 /* will point to RV2AV or PADAV op on LHS/RHS of assign */
6838 /* check that RHS of sort is a single plain array */
6839 oright = cUNOPo->op_first;
6840 if (!oright || oright->op_type != OP_PUSHMARK)
6843 /* reverse sort ... can be optimised. */
6844 if (!cUNOPo->op_sibling) {
6845 /* Nothing follows us on the list. */
6846 OP *reverse = o->op_next;
6848 if (reverse->op_type == OP_REVERSE &&
6849 (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) {
6850 OP *pushmark = cUNOPx(reverse)->op_first;
6851 if (pushmark && (pushmark->op_type == OP_PUSHMARK)
6852 && (cUNOPx(pushmark)->op_sibling == o)) {
6853 /* reverse -> pushmark -> sort */
6854 o->op_private |= OPpSORT_REVERSE;
6856 pushmark->op_next = oright->op_next;
6862 /* make @a = sort @a act in-place */
6864 o->op_seq = PL_op_seqmax++;
6866 oright = cUNOPx(oright)->op_sibling;
6869 if (oright->op_type == OP_NULL) { /* skip sort block/sub */
6870 oright = cUNOPx(oright)->op_sibling;
6874 (oright->op_type != OP_RV2AV && oright->op_type != OP_PADAV)
6875 || oright->op_next != o
6876 || (oright->op_private & OPpLVAL_INTRO)
6880 /* o2 follows the chain of op_nexts through the LHS of the
6881 * assign (if any) to the aassign op itself */
6883 if (!o2 || o2->op_type != OP_NULL)
6886 if (!o2 || o2->op_type != OP_PUSHMARK)
6889 if (o2 && o2->op_type == OP_GV)
6892 || (o2->op_type != OP_PADAV && o2->op_type != OP_RV2AV)
6893 || (o2->op_private & OPpLVAL_INTRO)
6898 if (!o2 || o2->op_type != OP_NULL)
6901 if (!o2 || o2->op_type != OP_AASSIGN
6902 || (o2->op_flags & OPf_WANT) != OPf_WANT_VOID)
6905 /* check that the sort is the first arg on RHS of assign */
6907 o2 = cUNOPx(o2)->op_first;
6908 if (!o2 || o2->op_type != OP_NULL)
6910 o2 = cUNOPx(o2)->op_first;
6911 if (!o2 || o2->op_type != OP_PUSHMARK)
6913 if (o2->op_sibling != o)
6916 /* check the array is the same on both sides */
6917 if (oleft->op_type == OP_RV2AV) {
6918 if (oright->op_type != OP_RV2AV
6919 || !cUNOPx(oright)->op_first
6920 || cUNOPx(oright)->op_first->op_type != OP_GV
6921 || cGVOPx_gv(cUNOPx(oleft)->op_first) !=
6922 cGVOPx_gv(cUNOPx(oright)->op_first)
6926 else if (oright->op_type != OP_PADAV
6927 || oright->op_targ != oleft->op_targ
6931 /* transfer MODishness etc from LHS arg to RHS arg */
6932 oright->op_flags = oleft->op_flags;
6933 o->op_private |= OPpSORT_INPLACE;
6935 /* excise push->gv->rv2av->null->aassign */
6936 o2 = o->op_next->op_next;
6937 op_null(o2); /* PUSHMARK */
6939 if (o2->op_type == OP_GV) {
6940 op_null(o2); /* GV */
6943 op_null(o2); /* RV2AV or PADAV */
6944 o2 = o2->op_next->op_next;
6945 op_null(o2); /* AASSIGN */
6947 o->op_next = o2->op_next;
6953 OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av;
6955 LISTOP *enter, *exlist;
6956 o->op_seq = PL_op_seqmax++;
6958 enter = (LISTOP *) o->op_next;
6961 if (enter->op_type == OP_NULL) {
6962 enter = (LISTOP *) enter->op_next;
6966 /* for $a (...) will have OP_GV then OP_RV2GV here.
6967 for (...) just has an OP_GV. */
6968 if (enter->op_type == OP_GV) {
6969 gvop = (OP *) enter;
6970 enter = (LISTOP *) enter->op_next;
6973 if (enter->op_type == OP_RV2GV) {
6974 enter = (LISTOP *) enter->op_next;
6980 if (enter->op_type != OP_ENTERITER)
6983 iter = enter->op_next;
6984 if (!iter || iter->op_type != OP_ITER)
6987 expushmark = enter->op_first;
6988 if (!expushmark || expushmark->op_type != OP_NULL
6989 || expushmark->op_targ != OP_PUSHMARK)
6992 exlist = (LISTOP *) expushmark->op_sibling;
6993 if (!exlist || exlist->op_type != OP_NULL
6994 || exlist->op_targ != OP_LIST)
6997 if (exlist->op_last != o) {
6998 /* Mmm. Was expecting to point back to this op. */
7001 theirmark = exlist->op_first;
7002 if (!theirmark || theirmark->op_type != OP_PUSHMARK)
7005 if (theirmark->op_sibling != o) {
7006 /* There's something between the mark and the reverse, eg
7007 for (1, reverse (...))
7012 ourmark = ((LISTOP *)o)->op_first;
7013 if (!ourmark || ourmark->op_type != OP_PUSHMARK)
7016 ourlast = ((LISTOP *)o)->op_last;
7017 if (!ourlast || ourlast->op_next != o)
7020 rv2av = ourmark->op_sibling;
7021 if (rv2av && rv2av->op_type == OP_RV2AV && rv2av->op_sibling == 0
7022 && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)
7023 && enter->op_flags == (OPf_WANT_LIST | OPf_KIDS)) {
7024 /* We're just reversing a single array. */
7025 rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF;
7026 enter->op_flags |= OPf_STACKED;
7029 /* We don't have control over who points to theirmark, so sacrifice
7031 theirmark->op_next = ourmark->op_next;
7032 theirmark->op_flags = ourmark->op_flags;
7033 ourlast->op_next = gvop ? gvop : (OP *) enter;
7036 enter->op_private |= OPpITER_REVERSED;
7037 iter->op_private |= OPpITER_REVERSED;
7043 o->op_seq = PL_op_seqmax++;
7052 Perl_custom_op_name(pTHX_ OP* o)
7054 const IV index = PTR2IV(o->op_ppaddr);
7058 if (!PL_custom_op_names) /* This probably shouldn't happen */
7059 return PL_op_name[OP_CUSTOM];
7061 keysv = sv_2mortal(newSViv(index));
7063 he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0);
7065 return PL_op_name[OP_CUSTOM]; /* Don't know who you are */
7067 return SvPV_nolen(HeVAL(he));
7071 Perl_custom_op_desc(pTHX_ OP* o)
7073 const IV index = PTR2IV(o->op_ppaddr);
7077 if (!PL_custom_op_descs)
7078 return PL_op_desc[OP_CUSTOM];
7080 keysv = sv_2mortal(newSViv(index));
7082 he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0);
7084 return PL_op_desc[OP_CUSTOM];
7086 return SvPV_nolen(HeVAL(he));
7091 /* Efficient sub that returns a constant scalar value. */
7093 const_sv_xsub(pTHX_ CV* cv)
7098 Perl_croak(aTHX_ "usage: %s::%s()",
7099 HvNAME_get(GvSTASH(CvGV(cv))), GvNAME(CvGV(cv)));
7103 ST(0) = (SV*)XSANY.any_ptr;
7109 * c-indentation-style: bsd
7111 * indent-tabs-mode: t
7114 * ex: set ts=8 sts=4 sw=4 noet: