3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 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
25 #define CALL_PEEP(o) CALL_FPTR(PL_peepp)(aTHX_ o)
27 #if defined(PL_OP_SLAB_ALLOC)
29 #ifndef PERL_SLAB_SIZE
30 #define PERL_SLAB_SIZE 2048
34 Perl_Slab_Alloc(pTHX_ int m, size_t sz)
37 * To make incrementing use count easy PL_OpSlab is an I32 *
38 * To make inserting the link to slab PL_OpPtr is I32 **
39 * So compute size in units of sizeof(I32 *) as that is how Pl_OpPtr increments
40 * Add an overhead for pointer to slab and round up as a number of pointers
42 sz = (sz + 2*sizeof(I32 *) -1)/sizeof(I32 *);
43 if ((PL_OpSpace -= sz) < 0) {
44 PL_OpPtr = (I32 **) PerlMemShared_malloc(PERL_SLAB_SIZE*sizeof(I32*));
48 Zero(PL_OpPtr,PERL_SLAB_SIZE,I32 **);
49 /* We reserve the 0'th I32 sized chunk as a use count */
50 PL_OpSlab = (I32 *) PL_OpPtr;
51 /* Reduce size by the use count word, and by the size we need.
52 * Latter is to mimic the '-=' in the if() above
54 PL_OpSpace = PERL_SLAB_SIZE - (sizeof(I32)+sizeof(I32 **)-1)/sizeof(I32 **) - sz;
55 /* Allocation pointer starts at the top.
56 Theory: because we build leaves before trunk allocating at end
57 means that at run time access is cache friendly upward
59 PL_OpPtr += PERL_SLAB_SIZE;
61 assert( PL_OpSpace >= 0 );
62 /* Move the allocation pointer down */
64 assert( PL_OpPtr > (I32 **) PL_OpSlab );
65 *PL_OpPtr = PL_OpSlab; /* Note which slab it belongs to */
66 (*PL_OpSlab)++; /* Increment use count of slab */
67 assert( PL_OpPtr+sz <= ((I32 **) PL_OpSlab + PERL_SLAB_SIZE) );
68 assert( *PL_OpSlab > 0 );
69 return (void *)(PL_OpPtr + 1);
73 Perl_Slab_Free(pTHX_ void *op)
75 I32 **ptr = (I32 **) op;
77 assert( ptr-1 > (I32 **) slab );
78 assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) );
82 # define PerlMemShared PerlMem
85 PerlMemShared_free(slab);
86 if (slab == PL_OpSlab) {
93 * In the following definition, the ", Nullop" is just to make the compiler
94 * think the expression is of the right type: croak actually does a Siglongjmp.
96 #define CHECKOP(type,o) \
97 ((PL_op_mask && PL_op_mask[type]) \
98 ? ( op_free((OP*)o), \
99 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
101 : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o))
103 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
106 S_gv_ename(pTHX_ GV *gv)
109 SV* tmpsv = sv_newmortal();
110 gv_efullname3(tmpsv, gv, Nullch);
111 return SvPV(tmpsv,n_a);
115 S_no_fh_allowed(pTHX_ OP *o)
117 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
123 S_too_few_arguments(pTHX_ OP *o, char *name)
125 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
130 S_too_many_arguments(pTHX_ OP *o, char *name)
132 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
137 S_bad_type(pTHX_ I32 n, char *t, char *name, OP *kid)
139 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
140 (int)n, name, t, OP_DESC(kid)));
144 S_no_bareword_allowed(pTHX_ OP *o)
146 qerror(Perl_mess(aTHX_
147 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
151 /* "register" allocation */
154 Perl_allocmy(pTHX_ char *name)
158 /* complain about "my $_" etc etc */
159 if (!(PL_in_my == KEY_our ||
161 (USE_UTF8_IN_NAMES && UTF8_IS_START(name[1])) ||
162 (name[1] == '_' && (int)strlen(name) > 2)))
164 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
165 /* 1999-02-27 mjd@plover.com */
167 p = strchr(name, '\0');
168 /* The next block assumes the buffer is at least 205 chars
169 long. At present, it's always at least 256 chars. */
171 strcpy(name+200, "...");
177 /* Move everything else down one character */
178 for (; p-name > 2; p--)
180 name[2] = toCTRL(name[1]);
183 yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
186 /* check for duplicate declaration */
188 (bool)(PL_in_my == KEY_our),
189 (PL_curstash ? PL_curstash : PL_defstash)
192 if (PL_in_my_stash && *name != '$') {
193 yyerror(Perl_form(aTHX_
194 "Can't declare class for non-scalar %s in \"%s\"",
195 name, PL_in_my == KEY_our ? "our" : "my"));
198 /* allocate a spare slot and store the name in that slot */
200 off = pad_add_name(name,
203 ? (PL_curstash ? PL_curstash : PL_defstash)
214 Perl_op_free(pTHX_ OP *o)
216 register OP *kid, *nextkid;
219 if (!o || o->op_seq == (U16)-1)
222 if (o->op_private & OPpREFCOUNTED) {
223 switch (o->op_type) {
231 if (OpREFCNT_dec(o)) {
242 if (o->op_flags & OPf_KIDS) {
243 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
244 nextkid = kid->op_sibling; /* Get before next freeing kid */
250 type = (OPCODE)o->op_targ;
252 /* COP* is not cleared by op_clear() so that we may track line
253 * numbers etc even after null() */
254 if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
262 Perl_op_clear(pTHX_ OP *o)
265 switch (o->op_type) {
266 case OP_NULL: /* Was holding old type, if any. */
267 case OP_ENTEREVAL: /* Was holding hints. */
271 if (!(o->op_flags & OPf_REF)
272 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
279 if (cPADOPo->op_padix > 0) {
280 /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
281 * may still exist on the pad */
282 pad_swipe(cPADOPo->op_padix, TRUE);
283 cPADOPo->op_padix = 0;
286 SvREFCNT_dec(cSVOPo->op_sv);
287 cSVOPo->op_sv = Nullsv;
290 case OP_METHOD_NAMED:
292 SvREFCNT_dec(cSVOPo->op_sv);
293 cSVOPo->op_sv = Nullsv;
296 Even if op_clear does a pad_free for the target of the op,
297 pad_free doesn't actually remove the sv that exists in the bad
298 instead it lives on. This results in that it could be reused as
299 a target later on when the pad was reallocated.
302 pad_swipe(o->op_targ,1);
311 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
315 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
316 SvREFCNT_dec(cSVOPo->op_sv);
317 cSVOPo->op_sv = Nullsv;
320 Safefree(cPVOPo->op_pv);
321 cPVOPo->op_pv = Nullch;
325 op_free(cPMOPo->op_pmreplroot);
329 if (INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot)) {
330 /* No GvIN_PAD_off here, because other references may still
331 * exist on the pad */
332 pad_swipe(INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot), TRUE);
335 SvREFCNT_dec((SV*)cPMOPo->op_pmreplroot);
342 HV *pmstash = PmopSTASH(cPMOPo);
343 if (pmstash && SvREFCNT(pmstash)) {
344 PMOP *pmop = HvPMROOT(pmstash);
345 PMOP *lastpmop = NULL;
347 if (cPMOPo == pmop) {
349 lastpmop->op_pmnext = pmop->op_pmnext;
351 HvPMROOT(pmstash) = pmop->op_pmnext;
355 pmop = pmop->op_pmnext;
358 PmopSTASH_free(cPMOPo);
360 cPMOPo->op_pmreplroot = Nullop;
361 /* we use the "SAFE" version of the PM_ macros here
362 * since sv_clean_all might release some PMOPs
363 * after PL_regex_padav has been cleared
364 * and the clearing of PL_regex_padav needs to
365 * happen before sv_clean_all
367 ReREFCNT_dec(PM_GETRE_SAFE(cPMOPo));
368 PM_SETRE_SAFE(cPMOPo, (REGEXP*)NULL);
370 if(PL_regex_pad) { /* We could be in destruction */
371 av_push((AV*) PL_regex_pad[0],(SV*) PL_regex_pad[(cPMOPo)->op_pmoffset]);
372 SvREPADTMP_on(PL_regex_pad[(cPMOPo)->op_pmoffset]);
373 PM_SETRE(cPMOPo, (cPMOPo)->op_pmoffset);
380 if (o->op_targ > 0) {
381 pad_free(o->op_targ);
387 S_cop_free(pTHX_ COP* cop)
389 Safefree(cop->cop_label); /* FIXME: treaddead ??? */
392 if (! specialWARN(cop->cop_warnings))
393 SvREFCNT_dec(cop->cop_warnings);
394 if (! specialCopIO(cop->cop_io)) {
398 char *s = SvPV(cop->cop_io,len);
399 Perl_warn(aTHX_ "io='%.*s'",(int) len,s); /* ??? --jhi */
402 SvREFCNT_dec(cop->cop_io);
408 Perl_op_null(pTHX_ OP *o)
410 if (o->op_type == OP_NULL)
413 o->op_targ = o->op_type;
414 o->op_type = OP_NULL;
415 o->op_ppaddr = PL_ppaddr[OP_NULL];
418 /* Contextualizers */
420 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
423 Perl_linklist(pTHX_ OP *o)
430 /* establish postfix order */
431 if (cUNOPo->op_first) {
432 o->op_next = LINKLIST(cUNOPo->op_first);
433 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
435 kid->op_next = LINKLIST(kid->op_sibling);
447 Perl_scalarkids(pTHX_ OP *o)
450 if (o && o->op_flags & OPf_KIDS) {
451 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
458 S_scalarboolean(pTHX_ OP *o)
460 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
461 if (ckWARN(WARN_SYNTAX)) {
462 line_t oldline = CopLINE(PL_curcop);
464 if (PL_copline != NOLINE)
465 CopLINE_set(PL_curcop, PL_copline);
466 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
467 CopLINE_set(PL_curcop, oldline);
474 Perl_scalar(pTHX_ OP *o)
478 /* assumes no premature commitment */
479 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
480 || o->op_type == OP_RETURN)
485 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
487 switch (o->op_type) {
489 scalar(cBINOPo->op_first);
494 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
498 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
499 if (!kPMOP->op_pmreplroot)
500 deprecate_old("implicit split to @_");
508 if (o->op_flags & OPf_KIDS) {
509 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
515 kid = cLISTOPo->op_first;
517 while ((kid = kid->op_sibling)) {
523 WITH_THR(PL_curcop = &PL_compiling);
528 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
534 WITH_THR(PL_curcop = &PL_compiling);
537 if (ckWARN(WARN_VOID))
538 Perl_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
544 Perl_scalarvoid(pTHX_ OP *o)
551 if (o->op_type == OP_NEXTSTATE
552 || o->op_type == OP_SETSTATE
553 || o->op_type == OP_DBSTATE
554 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
555 || o->op_targ == OP_SETSTATE
556 || o->op_targ == OP_DBSTATE)))
557 PL_curcop = (COP*)o; /* for warning below */
559 /* assumes no premature commitment */
560 want = o->op_flags & OPf_WANT;
561 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
562 || o->op_type == OP_RETURN)
567 if ((o->op_private & OPpTARGET_MY)
568 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
570 return scalar(o); /* As if inside SASSIGN */
573 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
575 switch (o->op_type) {
577 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
581 if (o->op_flags & OPf_STACKED)
585 if (o->op_private == 4)
657 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
658 useless = OP_DESC(o);
665 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
666 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
667 useless = "a variable";
672 if (cSVOPo->op_private & OPpCONST_STRICT)
673 no_bareword_allowed(o);
675 if (ckWARN(WARN_VOID)) {
676 useless = "a constant";
677 /* the constants 0 and 1 are permitted as they are
678 conventionally used as dummies in constructs like
679 1 while some_condition_with_side_effects; */
680 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
682 else if (SvPOK(sv)) {
683 /* perl4's way of mixing documentation and code
684 (before the invention of POD) was based on a
685 trick to mix nroff and perl code. The trick was
686 built upon these three nroff macros being used in
687 void context. The pink camel has the details in
688 the script wrapman near page 319. */
689 if (strnEQ(SvPVX(sv), "di", 2) ||
690 strnEQ(SvPVX(sv), "ds", 2) ||
691 strnEQ(SvPVX(sv), "ig", 2))
696 op_null(o); /* don't execute or even remember it */
700 o->op_type = OP_PREINC; /* pre-increment is faster */
701 o->op_ppaddr = PL_ppaddr[OP_PREINC];
705 o->op_type = OP_PREDEC; /* pre-decrement is faster */
706 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
713 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
718 if (o->op_flags & OPf_STACKED)
725 if (!(o->op_flags & OPf_KIDS))
734 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
741 /* all requires must return a boolean value */
742 o->op_flags &= ~OPf_WANT;
747 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
748 if (!kPMOP->op_pmreplroot)
749 deprecate_old("implicit split to @_");
753 if (useless && ckWARN(WARN_VOID))
754 Perl_warner(aTHX_ packWARN(WARN_VOID), "Useless use of %s in void context", useless);
759 Perl_listkids(pTHX_ OP *o)
762 if (o && o->op_flags & OPf_KIDS) {
763 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
770 Perl_list(pTHX_ OP *o)
774 /* assumes no premature commitment */
775 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
776 || o->op_type == OP_RETURN)
781 if ((o->op_private & OPpTARGET_MY)
782 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
784 return o; /* As if inside SASSIGN */
787 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
789 switch (o->op_type) {
792 list(cBINOPo->op_first);
797 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
805 if (!(o->op_flags & OPf_KIDS))
807 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
808 list(cBINOPo->op_first);
809 return gen_constant_list(o);
816 kid = cLISTOPo->op_first;
818 while ((kid = kid->op_sibling)) {
824 WITH_THR(PL_curcop = &PL_compiling);
828 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
834 WITH_THR(PL_curcop = &PL_compiling);
837 /* all requires must return a boolean value */
838 o->op_flags &= ~OPf_WANT;
845 Perl_scalarseq(pTHX_ OP *o)
850 if (o->op_type == OP_LINESEQ ||
851 o->op_type == OP_SCOPE ||
852 o->op_type == OP_LEAVE ||
853 o->op_type == OP_LEAVETRY)
855 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
856 if (kid->op_sibling) {
860 PL_curcop = &PL_compiling;
862 o->op_flags &= ~OPf_PARENS;
863 if (PL_hints & HINT_BLOCK_SCOPE)
864 o->op_flags |= OPf_PARENS;
867 o = newOP(OP_STUB, 0);
872 S_modkids(pTHX_ OP *o, I32 type)
875 if (o && o->op_flags & OPf_KIDS) {
876 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
882 /* Propagate lvalue ("modifiable") context to an op and it's children.
883 * 'type' represents the context type, roughly based on the type of op that
884 * would do the modifying, although local() is represented by OP_NULL.
885 * It's responsible for detecting things that can't be modified, flag
886 * things that need to behave specially in an lvalue context (e.g., "$$x = 5"
887 * might have to vivify a reference in $x), and so on.
889 * For example, "$a+1 = 2" would cause mod() to be called with o being
890 * OP_ADD and type being OP_SASSIGN, and would output an error.
894 Perl_mod(pTHX_ OP *o, I32 type)
897 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
900 if (!o || PL_error_count)
903 if ((o->op_private & OPpTARGET_MY)
904 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
909 switch (o->op_type) {
915 if (!(o->op_private & (OPpCONST_ARYBASE)))
917 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
918 PL_compiling.cop_arybase = (I32)SvIV(cSVOPx(PL_eval_start)->op_sv);
922 SAVEI32(PL_compiling.cop_arybase);
923 PL_compiling.cop_arybase = 0;
925 else if (type == OP_REFGEN)
928 Perl_croak(aTHX_ "That use of $[ is unsupported");
931 if (o->op_flags & OPf_PARENS)
935 if ((type == OP_UNDEF || type == OP_REFGEN) &&
936 !(o->op_flags & OPf_STACKED)) {
937 o->op_type = OP_RV2CV; /* entersub => rv2cv */
938 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
939 assert(cUNOPo->op_first->op_type == OP_NULL);
940 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
943 else if (o->op_private & OPpENTERSUB_NOMOD)
945 else { /* lvalue subroutine call */
946 o->op_private |= OPpLVAL_INTRO;
947 PL_modcount = RETURN_UNLIMITED_NUMBER;
948 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
949 /* Backward compatibility mode: */
950 o->op_private |= OPpENTERSUB_INARGS;
953 else { /* Compile-time error message: */
954 OP *kid = cUNOPo->op_first;
958 if (kid->op_type == OP_PUSHMARK)
960 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
962 "panic: unexpected lvalue entersub "
963 "args: type/targ %ld:%"UVuf,
964 (long)kid->op_type, (UV)kid->op_targ);
965 kid = kLISTOP->op_first;
967 while (kid->op_sibling)
968 kid = kid->op_sibling;
969 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
971 if (kid->op_type == OP_METHOD_NAMED
972 || kid->op_type == OP_METHOD)
976 NewOp(1101, newop, 1, UNOP);
977 newop->op_type = OP_RV2CV;
978 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
979 newop->op_first = Nullop;
980 newop->op_next = (OP*)newop;
981 kid->op_sibling = (OP*)newop;
982 newop->op_private |= OPpLVAL_INTRO;
986 if (kid->op_type != OP_RV2CV)
988 "panic: unexpected lvalue entersub "
989 "entry via type/targ %ld:%"UVuf,
990 (long)kid->op_type, (UV)kid->op_targ);
991 kid->op_private |= OPpLVAL_INTRO;
992 break; /* Postpone until runtime */
996 kid = kUNOP->op_first;
997 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
998 kid = kUNOP->op_first;
999 if (kid->op_type == OP_NULL)
1001 "Unexpected constant lvalue entersub "
1002 "entry via type/targ %ld:%"UVuf,
1003 (long)kid->op_type, (UV)kid->op_targ);
1004 if (kid->op_type != OP_GV) {
1005 /* Restore RV2CV to check lvalueness */
1007 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1008 okid->op_next = kid->op_next;
1009 kid->op_next = okid;
1012 okid->op_next = Nullop;
1013 okid->op_type = OP_RV2CV;
1015 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1016 okid->op_private |= OPpLVAL_INTRO;
1020 cv = GvCV(kGVOP_gv);
1030 /* grep, foreach, subcalls, refgen */
1031 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1033 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1034 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1036 : (o->op_type == OP_ENTERSUB
1037 ? "non-lvalue subroutine call"
1039 type ? PL_op_desc[type] : "local"));
1053 case OP_RIGHT_SHIFT:
1062 if (!(o->op_flags & OPf_STACKED))
1069 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1075 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1076 PL_modcount = RETURN_UNLIMITED_NUMBER;
1077 return o; /* Treat \(@foo) like ordinary list. */
1081 if (scalar_mod_type(o, type))
1083 ref(cUNOPo->op_first, o->op_type);
1087 if (type == OP_LEAVESUBLV)
1088 o->op_private |= OPpMAYBE_LVSUB;
1094 PL_modcount = RETURN_UNLIMITED_NUMBER;
1097 ref(cUNOPo->op_first, o->op_type);
1102 PL_hints |= HINT_BLOCK_SCOPE;
1117 PL_modcount = RETURN_UNLIMITED_NUMBER;
1118 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1119 return o; /* Treat \(@foo) like ordinary list. */
1120 if (scalar_mod_type(o, type))
1122 if (type == OP_LEAVESUBLV)
1123 o->op_private |= OPpMAYBE_LVSUB;
1127 if (!type) /* local() */
1128 Perl_croak(aTHX_ "Can't localize lexical variable %s",
1129 PAD_COMPNAME_PV(o->op_targ));
1137 if (type != OP_SASSIGN)
1141 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1146 if (type == OP_LEAVESUBLV)
1147 o->op_private |= OPpMAYBE_LVSUB;
1149 pad_free(o->op_targ);
1150 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1151 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1152 if (o->op_flags & OPf_KIDS)
1153 mod(cBINOPo->op_first->op_sibling, type);
1158 ref(cBINOPo->op_first, o->op_type);
1159 if (type == OP_ENTERSUB &&
1160 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1161 o->op_private |= OPpLVAL_DEFER;
1162 if (type == OP_LEAVESUBLV)
1163 o->op_private |= OPpMAYBE_LVSUB;
1173 if (o->op_flags & OPf_KIDS)
1174 mod(cLISTOPo->op_last, type);
1179 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1181 else if (!(o->op_flags & OPf_KIDS))
1183 if (o->op_targ != OP_LIST) {
1184 mod(cBINOPo->op_first, type);
1190 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1195 if (type != OP_LEAVESUBLV)
1197 break; /* mod()ing was handled by ck_return() */
1200 /* [20011101.069] File test operators interpret OPf_REF to mean that
1201 their argument is a filehandle; thus \stat(".") should not set
1203 if (type == OP_REFGEN &&
1204 PL_check[o->op_type] == MEMBER_TO_FPTR(Perl_ck_ftst))
1207 if (type != OP_LEAVESUBLV)
1208 o->op_flags |= OPf_MOD;
1210 if (type == OP_AASSIGN || type == OP_SASSIGN)
1211 o->op_flags |= OPf_SPECIAL|OPf_REF;
1212 else if (!type) { /* local() */
1215 o->op_private |= OPpLVAL_INTRO;
1216 o->op_flags &= ~OPf_SPECIAL;
1217 PL_hints |= HINT_BLOCK_SCOPE;
1222 if (ckWARN(WARN_SYNTAX)) {
1223 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1224 "Useless localization of %s", OP_DESC(o));
1228 else if (type != OP_GREPSTART && type != OP_ENTERSUB
1229 && type != OP_LEAVESUBLV)
1230 o->op_flags |= OPf_REF;
1235 S_scalar_mod_type(pTHX_ OP *o, I32 type)
1239 if (o->op_type == OP_RV2GV)
1263 case OP_RIGHT_SHIFT:
1282 S_is_handle_constructor(pTHX_ OP *o, I32 argnum)
1284 switch (o->op_type) {
1292 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1305 Perl_refkids(pTHX_ OP *o, I32 type)
1308 if (o && o->op_flags & OPf_KIDS) {
1309 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1316 Perl_ref(pTHX_ OP *o, I32 type)
1320 if (!o || PL_error_count)
1323 switch (o->op_type) {
1325 if ((type == OP_EXISTS || type == OP_DEFINED || type == OP_LOCK) &&
1326 !(o->op_flags & OPf_STACKED)) {
1327 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1328 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1329 assert(cUNOPo->op_first->op_type == OP_NULL);
1330 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1331 o->op_flags |= OPf_SPECIAL;
1336 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1340 if (type == OP_DEFINED)
1341 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1342 ref(cUNOPo->op_first, o->op_type);
1345 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1346 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1347 : type == OP_RV2HV ? OPpDEREF_HV
1349 o->op_flags |= OPf_MOD;
1354 o->op_flags |= OPf_MOD; /* XXX ??? */
1359 o->op_flags |= OPf_REF;
1362 if (type == OP_DEFINED)
1363 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1364 ref(cUNOPo->op_first, o->op_type);
1369 o->op_flags |= OPf_REF;
1374 if (!(o->op_flags & OPf_KIDS))
1376 ref(cBINOPo->op_first, type);
1380 ref(cBINOPo->op_first, o->op_type);
1381 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1382 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1383 : type == OP_RV2HV ? OPpDEREF_HV
1385 o->op_flags |= OPf_MOD;
1393 if (!(o->op_flags & OPf_KIDS))
1395 ref(cLISTOPo->op_last, type);
1405 S_dup_attrlist(pTHX_ OP *o)
1409 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1410 * where the first kid is OP_PUSHMARK and the remaining ones
1411 * are OP_CONST. We need to push the OP_CONST values.
1413 if (o->op_type == OP_CONST)
1414 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc(cSVOPo->op_sv));
1416 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1417 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1418 if (o->op_type == OP_CONST)
1419 rop = append_elem(OP_LIST, rop,
1420 newSVOP(OP_CONST, o->op_flags,
1421 SvREFCNT_inc(cSVOPo->op_sv)));
1428 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs, bool for_my)
1432 /* fake up C<use attributes $pkg,$rv,@attrs> */
1433 ENTER; /* need to protect against side-effects of 'use' */
1436 stashsv = newSVpv(HvNAME(stash), 0);
1438 stashsv = &PL_sv_no;
1440 #define ATTRSMODULE "attributes"
1441 #define ATTRSMODULE_PM "attributes.pm"
1445 /* Don't force the C<use> if we don't need it. */
1446 svp = hv_fetch(GvHVn(PL_incgv), ATTRSMODULE_PM,
1447 sizeof(ATTRSMODULE_PM)-1, 0);
1448 if (svp && *svp != &PL_sv_undef)
1449 ; /* already in %INC */
1451 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
1452 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1456 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1457 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1459 prepend_elem(OP_LIST,
1460 newSVOP(OP_CONST, 0, stashsv),
1461 prepend_elem(OP_LIST,
1462 newSVOP(OP_CONST, 0,
1464 dup_attrlist(attrs))));
1470 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
1472 OP *pack, *imop, *arg;
1478 assert(target->op_type == OP_PADSV ||
1479 target->op_type == OP_PADHV ||
1480 target->op_type == OP_PADAV);
1482 /* Ensure that attributes.pm is loaded. */
1483 apply_attrs(stash, PAD_SV(target->op_targ), attrs, TRUE);
1485 /* Need package name for method call. */
1486 pack = newSVOP(OP_CONST, 0, newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1));
1488 /* Build up the real arg-list. */
1490 stashsv = newSVpv(HvNAME(stash), 0);
1492 stashsv = &PL_sv_no;
1493 arg = newOP(OP_PADSV, 0);
1494 arg->op_targ = target->op_targ;
1495 arg = prepend_elem(OP_LIST,
1496 newSVOP(OP_CONST, 0, stashsv),
1497 prepend_elem(OP_LIST,
1498 newUNOP(OP_REFGEN, 0,
1499 mod(arg, OP_REFGEN)),
1500 dup_attrlist(attrs)));
1502 /* Fake up a method call to import */
1503 meth = newSVpvn("import", 6);
1504 (void)SvUPGRADE(meth, SVt_PVIV);
1505 (void)SvIOK_on(meth);
1506 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
1507 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
1508 append_elem(OP_LIST,
1509 prepend_elem(OP_LIST, pack, list(arg)),
1510 newSVOP(OP_METHOD_NAMED, 0, meth)));
1511 imop->op_private |= OPpENTERSUB_NOMOD;
1513 /* Combine the ops. */
1514 *imopsp = append_elem(OP_LIST, *imopsp, imop);
1518 =notfor apidoc apply_attrs_string
1520 Attempts to apply a list of attributes specified by the C<attrstr> and
1521 C<len> arguments to the subroutine identified by the C<cv> argument which
1522 is expected to be associated with the package identified by the C<stashpv>
1523 argument (see L<attributes>). It gets this wrong, though, in that it
1524 does not correctly identify the boundaries of the individual attribute
1525 specifications within C<attrstr>. This is not really intended for the
1526 public API, but has to be listed here for systems such as AIX which
1527 need an explicit export list for symbols. (It's called from XS code
1528 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
1529 to respect attribute syntax properly would be welcome.
1535 Perl_apply_attrs_string(pTHX_ char *stashpv, CV *cv,
1536 char *attrstr, STRLEN len)
1541 len = strlen(attrstr);
1545 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
1547 char *sstr = attrstr;
1548 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
1549 attrs = append_elem(OP_LIST, attrs,
1550 newSVOP(OP_CONST, 0,
1551 newSVpvn(sstr, attrstr-sstr)));
1555 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1556 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1557 Nullsv, prepend_elem(OP_LIST,
1558 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
1559 prepend_elem(OP_LIST,
1560 newSVOP(OP_CONST, 0,
1566 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
1571 if (!o || PL_error_count)
1575 if (type == OP_LIST) {
1576 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1577 my_kid(kid, attrs, imopsp);
1578 } else if (type == OP_UNDEF) {
1580 } else if (type == OP_RV2SV || /* "our" declaration */
1582 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
1583 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
1584 yyerror(Perl_form(aTHX_ "Can't declare %s in %s",
1585 OP_DESC(o), PL_in_my == KEY_our ? "our" : "my"));
1587 GV *gv = cGVOPx_gv(cUNOPo->op_first);
1589 PL_in_my_stash = Nullhv;
1590 apply_attrs(GvSTASH(gv),
1591 (type == OP_RV2SV ? GvSV(gv) :
1592 type == OP_RV2AV ? (SV*)GvAV(gv) :
1593 type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)gv),
1596 o->op_private |= OPpOUR_INTRO;
1599 else if (type != OP_PADSV &&
1602 type != OP_PUSHMARK)
1604 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
1606 PL_in_my == KEY_our ? "our" : "my"));
1609 else if (attrs && type != OP_PUSHMARK) {
1613 PL_in_my_stash = Nullhv;
1615 /* check for C<my Dog $spot> when deciding package */
1616 stash = PAD_COMPNAME_TYPE(o->op_targ);
1618 stash = PL_curstash;
1619 apply_attrs_my(stash, o, attrs, imopsp);
1621 o->op_flags |= OPf_MOD;
1622 o->op_private |= OPpLVAL_INTRO;
1627 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1630 int maybe_scalar = 0;
1632 /* [perl #17376]: this appears to be premature, and results in code such as
1633 C< our(%x); > executing in list mode rather than void mode */
1635 if (o->op_flags & OPf_PARENS)
1644 o = my_kid(o, attrs, &rops);
1646 if (maybe_scalar && o->op_type == OP_PADSV) {
1647 o = scalar(append_list(OP_LIST, (LISTOP*)rops, (LISTOP*)o));
1648 o->op_private |= OPpLVAL_INTRO;
1651 o = append_list(OP_LIST, (LISTOP*)o, (LISTOP*)rops);
1654 PL_in_my_stash = Nullhv;
1659 Perl_my(pTHX_ OP *o)
1661 return my_attrs(o, Nullop);
1665 Perl_sawparens(pTHX_ OP *o)
1668 o->op_flags |= OPf_PARENS;
1673 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
1677 if (ckWARN(WARN_MISC) &&
1678 (left->op_type == OP_RV2AV ||
1679 left->op_type == OP_RV2HV ||
1680 left->op_type == OP_PADAV ||
1681 left->op_type == OP_PADHV)) {
1682 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
1683 right->op_type == OP_TRANS)
1684 ? right->op_type : OP_MATCH];
1685 const char *sample = ((left->op_type == OP_RV2AV ||
1686 left->op_type == OP_PADAV)
1687 ? "@array" : "%hash");
1688 Perl_warner(aTHX_ packWARN(WARN_MISC),
1689 "Applying %s to %s will act on scalar(%s)",
1690 desc, sample, sample);
1693 if (right->op_type == OP_CONST &&
1694 cSVOPx(right)->op_private & OPpCONST_BARE &&
1695 cSVOPx(right)->op_private & OPpCONST_STRICT)
1697 no_bareword_allowed(right);
1700 if (!(right->op_flags & OPf_STACKED) &&
1701 (right->op_type == OP_MATCH ||
1702 right->op_type == OP_SUBST ||
1703 right->op_type == OP_TRANS)) {
1704 right->op_flags |= OPf_STACKED;
1705 if (right->op_type != OP_MATCH &&
1706 ! (right->op_type == OP_TRANS &&
1707 right->op_private & OPpTRANS_IDENTICAL))
1708 left = mod(left, right->op_type);
1709 if (right->op_type == OP_TRANS)
1710 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1712 o = prepend_elem(right->op_type, scalar(left), right);
1714 return newUNOP(OP_NOT, 0, scalar(o));
1718 return bind_match(type, left,
1719 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1723 Perl_invert(pTHX_ OP *o)
1727 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1728 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1732 Perl_scope(pTHX_ OP *o)
1735 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
1736 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1737 o->op_type = OP_LEAVE;
1738 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
1740 else if (o->op_type == OP_LINESEQ) {
1742 o->op_type = OP_SCOPE;
1743 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
1744 kid = ((LISTOP*)o)->op_first;
1745 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
1749 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1755 Perl_save_hints(pTHX)
1758 SAVESPTR(GvHV(PL_hintgv));
1759 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
1760 SAVEFREESV(GvHV(PL_hintgv));
1764 Perl_block_start(pTHX_ int full)
1766 int retval = PL_savestack_ix;
1767 /* If there were syntax errors, don't try to start a block */
1768 if (PL_yynerrs) return retval;
1770 pad_block_start(full);
1772 PL_hints &= ~HINT_BLOCK_SCOPE;
1773 SAVESPTR(PL_compiling.cop_warnings);
1774 if (! specialWARN(PL_compiling.cop_warnings)) {
1775 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1776 SAVEFREESV(PL_compiling.cop_warnings) ;
1778 SAVESPTR(PL_compiling.cop_io);
1779 if (! specialCopIO(PL_compiling.cop_io)) {
1780 PL_compiling.cop_io = newSVsv(PL_compiling.cop_io) ;
1781 SAVEFREESV(PL_compiling.cop_io) ;
1787 Perl_block_end(pTHX_ I32 floor, OP *seq)
1789 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
1790 OP* retval = scalarseq(seq);
1791 /* If there were syntax errors, don't try to close a block */
1792 if (PL_yynerrs) return retval;
1794 PL_compiling.op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
1796 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1804 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
1808 Perl_newPROG(pTHX_ OP *o)
1813 PL_eval_root = newUNOP(OP_LEAVEEVAL,
1814 ((PL_in_eval & EVAL_KEEPERR)
1815 ? OPf_SPECIAL : 0), o);
1816 PL_eval_start = linklist(PL_eval_root);
1817 PL_eval_root->op_private |= OPpREFCOUNTED;
1818 OpREFCNT_set(PL_eval_root, 1);
1819 PL_eval_root->op_next = 0;
1820 CALL_PEEP(PL_eval_start);
1823 if (o->op_type == OP_STUB) {
1824 PL_comppad_name = 0;
1829 PL_main_root = scope(sawparens(scalarvoid(o)));
1830 PL_curcop = &PL_compiling;
1831 PL_main_start = LINKLIST(PL_main_root);
1832 PL_main_root->op_private |= OPpREFCOUNTED;
1833 OpREFCNT_set(PL_main_root, 1);
1834 PL_main_root->op_next = 0;
1835 CALL_PEEP(PL_main_start);
1838 /* Register with debugger */
1840 CV *cv = get_cv("DB::postponed", FALSE);
1844 XPUSHs((SV*)CopFILEGV(&PL_compiling));
1846 call_sv((SV*)cv, G_DISCARD);
1853 Perl_localize(pTHX_ OP *o, I32 lex)
1855 if (o->op_flags & OPf_PARENS)
1856 /* [perl #17376]: this appears to be premature, and results in code such as
1857 C< our(%x); > executing in list mode rather than void mode */
1864 if (ckWARN(WARN_PARENTHESIS)
1865 && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',')
1867 char *s = PL_bufptr;
1870 /* some heuristics to detect a potential error */
1871 while (*s && (strchr(", \t\n", *s)
1872 || (strchr("@$%*", *s) && ++sigil) ))
1875 while (*s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s)
1876 || strchr("@$%*, \t\n", *s)))
1879 if (*s == ';' || *s == '=')
1880 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
1881 "Parentheses missing around \"%s\" list",
1882 lex ? (PL_in_my == KEY_our ? "our" : "my")
1890 o = mod(o, OP_NULL); /* a bit kludgey */
1892 PL_in_my_stash = Nullhv;
1897 Perl_jmaybe(pTHX_ OP *o)
1899 if (o->op_type == OP_LIST) {
1901 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
1902 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
1908 Perl_fold_constants(pTHX_ register OP *o)
1911 I32 type = o->op_type;
1914 if (PL_opargs[type] & OA_RETSCALAR)
1916 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
1917 o->op_targ = pad_alloc(type, SVs_PADTMP);
1919 /* integerize op, unless it happens to be C<-foo>.
1920 * XXX should pp_i_negate() do magic string negation instead? */
1921 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
1922 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
1923 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
1925 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
1928 if (!(PL_opargs[type] & OA_FOLDCONST))
1933 /* XXX might want a ck_negate() for this */
1934 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
1946 /* XXX what about the numeric ops? */
1947 if (PL_hints & HINT_LOCALE)
1952 goto nope; /* Don't try to run w/ errors */
1954 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
1955 if ((curop->op_type != OP_CONST ||
1956 (curop->op_private & OPpCONST_BARE)) &&
1957 curop->op_type != OP_LIST &&
1958 curop->op_type != OP_SCALAR &&
1959 curop->op_type != OP_NULL &&
1960 curop->op_type != OP_PUSHMARK)
1966 curop = LINKLIST(o);
1970 sv = *(PL_stack_sp--);
1971 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
1972 pad_swipe(o->op_targ, FALSE);
1973 else if (SvTEMP(sv)) { /* grab mortal temp? */
1974 (void)SvREFCNT_inc(sv);
1978 if (type == OP_RV2GV)
1979 return newGVOP(OP_GV, 0, (GV*)sv);
1980 return newSVOP(OP_CONST, 0, sv);
1987 Perl_gen_constant_list(pTHX_ register OP *o)
1990 I32 oldtmps_floor = PL_tmps_floor;
1994 return o; /* Don't attempt to run with errors */
1996 PL_op = curop = LINKLIST(o);
2003 PL_tmps_floor = oldtmps_floor;
2005 o->op_type = OP_RV2AV;
2006 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2007 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
2008 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
2009 o->op_seq = 0; /* needs to be revisited in peep() */
2010 curop = ((UNOP*)o)->op_first;
2011 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
2018 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2020 if (!o || o->op_type != OP_LIST)
2021 o = newLISTOP(OP_LIST, 0, o, Nullop);
2023 o->op_flags &= ~OPf_WANT;
2025 if (!(PL_opargs[type] & OA_MARK))
2026 op_null(cLISTOPo->op_first);
2028 o->op_type = (OPCODE)type;
2029 o->op_ppaddr = PL_ppaddr[type];
2030 o->op_flags |= flags;
2032 o = CHECKOP(type, o);
2033 if (o->op_type != type)
2036 return fold_constants(o);
2039 /* List constructors */
2042 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2050 if (first->op_type != type
2051 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
2053 return newLISTOP(type, 0, first, last);
2056 if (first->op_flags & OPf_KIDS)
2057 ((LISTOP*)first)->op_last->op_sibling = last;
2059 first->op_flags |= OPf_KIDS;
2060 ((LISTOP*)first)->op_first = last;
2062 ((LISTOP*)first)->op_last = last;
2067 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2075 if (first->op_type != type)
2076 return prepend_elem(type, (OP*)first, (OP*)last);
2078 if (last->op_type != type)
2079 return append_elem(type, (OP*)first, (OP*)last);
2081 first->op_last->op_sibling = last->op_first;
2082 first->op_last = last->op_last;
2083 first->op_flags |= (last->op_flags & OPf_KIDS);
2091 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2099 if (last->op_type == type) {
2100 if (type == OP_LIST) { /* already a PUSHMARK there */
2101 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2102 ((LISTOP*)last)->op_first->op_sibling = first;
2103 if (!(first->op_flags & OPf_PARENS))
2104 last->op_flags &= ~OPf_PARENS;
2107 if (!(last->op_flags & OPf_KIDS)) {
2108 ((LISTOP*)last)->op_last = first;
2109 last->op_flags |= OPf_KIDS;
2111 first->op_sibling = ((LISTOP*)last)->op_first;
2112 ((LISTOP*)last)->op_first = first;
2114 last->op_flags |= OPf_KIDS;
2118 return newLISTOP(type, 0, first, last);
2124 Perl_newNULLLIST(pTHX)
2126 return newOP(OP_STUB, 0);
2130 Perl_force_list(pTHX_ OP *o)
2132 if (!o || o->op_type != OP_LIST)
2133 o = newLISTOP(OP_LIST, 0, o, Nullop);
2139 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2143 NewOp(1101, listop, 1, LISTOP);
2145 listop->op_type = (OPCODE)type;
2146 listop->op_ppaddr = PL_ppaddr[type];
2149 listop->op_flags = (U8)flags;
2153 else if (!first && last)
2156 first->op_sibling = last;
2157 listop->op_first = first;
2158 listop->op_last = last;
2159 if (type == OP_LIST) {
2161 pushop = newOP(OP_PUSHMARK, 0);
2162 pushop->op_sibling = first;
2163 listop->op_first = pushop;
2164 listop->op_flags |= OPf_KIDS;
2166 listop->op_last = pushop;
2169 return CHECKOP(type, listop);
2173 Perl_newOP(pTHX_ I32 type, I32 flags)
2176 NewOp(1101, o, 1, OP);
2177 o->op_type = (OPCODE)type;
2178 o->op_ppaddr = PL_ppaddr[type];
2179 o->op_flags = (U8)flags;
2182 o->op_private = (U8)(0 | (flags >> 8));
2183 if (PL_opargs[type] & OA_RETSCALAR)
2185 if (PL_opargs[type] & OA_TARGET)
2186 o->op_targ = pad_alloc(type, SVs_PADTMP);
2187 return CHECKOP(type, o);
2191 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
2196 first = newOP(OP_STUB, 0);
2197 if (PL_opargs[type] & OA_MARK)
2198 first = force_list(first);
2200 NewOp(1101, unop, 1, UNOP);
2201 unop->op_type = (OPCODE)type;
2202 unop->op_ppaddr = PL_ppaddr[type];
2203 unop->op_first = first;
2204 unop->op_flags = flags | OPf_KIDS;
2205 unop->op_private = (U8)(1 | (flags >> 8));
2206 unop = (UNOP*) CHECKOP(type, unop);
2210 return fold_constants((OP *) unop);
2214 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2217 NewOp(1101, binop, 1, BINOP);
2220 first = newOP(OP_NULL, 0);
2222 binop->op_type = (OPCODE)type;
2223 binop->op_ppaddr = PL_ppaddr[type];
2224 binop->op_first = first;
2225 binop->op_flags = flags | OPf_KIDS;
2228 binop->op_private = (U8)(1 | (flags >> 8));
2231 binop->op_private = (U8)(2 | (flags >> 8));
2232 first->op_sibling = last;
2235 binop = (BINOP*)CHECKOP(type, binop);
2236 if (binop->op_next || binop->op_type != (OPCODE)type)
2239 binop->op_last = binop->op_first->op_sibling;
2241 return fold_constants((OP *)binop);
2245 uvcompare(const void *a, const void *b)
2247 if (*((UV *)a) < (*(UV *)b))
2249 if (*((UV *)a) > (*(UV *)b))
2251 if (*((UV *)a+1) < (*(UV *)b+1))
2253 if (*((UV *)a+1) > (*(UV *)b+1))
2259 Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
2261 SV *tstr = ((SVOP*)expr)->op_sv;
2262 SV *rstr = ((SVOP*)repl)->op_sv;
2265 U8 *t = (U8*)SvPV(tstr, tlen);
2266 U8 *r = (U8*)SvPV(rstr, rlen);
2273 register short *tbl;
2275 PL_hints |= HINT_BLOCK_SCOPE;
2276 complement = o->op_private & OPpTRANS_COMPLEMENT;
2277 del = o->op_private & OPpTRANS_DELETE;
2278 squash = o->op_private & OPpTRANS_SQUASH;
2281 o->op_private |= OPpTRANS_FROM_UTF;
2284 o->op_private |= OPpTRANS_TO_UTF;
2286 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2287 SV* listsv = newSVpvn("# comment\n",10);
2289 U8* tend = t + tlen;
2290 U8* rend = r + rlen;
2304 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2305 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2311 tsave = t = bytes_to_utf8(t, &len);
2314 if (!to_utf && rlen) {
2316 rsave = r = bytes_to_utf8(r, &len);
2320 /* There are several snags with this code on EBCDIC:
2321 1. 0xFF is a legal UTF-EBCDIC byte (there are no illegal bytes).
2322 2. scan_const() in toke.c has encoded chars in native encoding which makes
2323 ranges at least in EBCDIC 0..255 range the bottom odd.
2327 U8 tmpbuf[UTF8_MAXLEN+1];
2330 New(1109, cp, 2*tlen, UV);
2332 transv = newSVpvn("",0);
2334 cp[2*i] = utf8n_to_uvuni(t, tend-t, &ulen, 0);
2336 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) {
2338 cp[2*i+1] = utf8n_to_uvuni(t, tend-t, &ulen, 0);
2342 cp[2*i+1] = cp[2*i];
2346 qsort(cp, i, 2*sizeof(UV), uvcompare);
2347 for (j = 0; j < i; j++) {
2349 diff = val - nextmin;
2351 t = uvuni_to_utf8(tmpbuf,nextmin);
2352 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2354 U8 range_mark = UTF_TO_NATIVE(0xff);
2355 t = uvuni_to_utf8(tmpbuf, val - 1);
2356 sv_catpvn(transv, (char *)&range_mark, 1);
2357 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2364 t = uvuni_to_utf8(tmpbuf,nextmin);
2365 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2367 U8 range_mark = UTF_TO_NATIVE(0xff);
2368 sv_catpvn(transv, (char *)&range_mark, 1);
2370 t = uvuni_to_utf8_flags(tmpbuf, 0x7fffffff,
2371 UNICODE_ALLOW_SUPER);
2372 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2373 t = (U8*)SvPVX(transv);
2374 tlen = SvCUR(transv);
2378 else if (!rlen && !del) {
2379 r = t; rlen = tlen; rend = tend;
2382 if ((!rlen && !del) || t == r ||
2383 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
2385 o->op_private |= OPpTRANS_IDENTICAL;
2389 while (t < tend || tfirst <= tlast) {
2390 /* see if we need more "t" chars */
2391 if (tfirst > tlast) {
2392 tfirst = (I32)utf8n_to_uvuni(t, tend - t, &ulen, 0);
2394 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) { /* illegal utf8 val indicates range */
2396 tlast = (I32)utf8n_to_uvuni(t, tend - t, &ulen, 0);
2403 /* now see if we need more "r" chars */
2404 if (rfirst > rlast) {
2406 rfirst = (I32)utf8n_to_uvuni(r, rend - r, &ulen, 0);
2408 if (r < rend && NATIVE_TO_UTF(*r) == 0xff) { /* illegal utf8 val indicates range */
2410 rlast = (I32)utf8n_to_uvuni(r, rend - r, &ulen, 0);
2419 rfirst = rlast = 0xffffffff;
2423 /* now see which range will peter our first, if either. */
2424 tdiff = tlast - tfirst;
2425 rdiff = rlast - rfirst;
2432 if (rfirst == 0xffffffff) {
2433 diff = tdiff; /* oops, pretend rdiff is infinite */
2435 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
2436 (long)tfirst, (long)tlast);
2438 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
2442 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
2443 (long)tfirst, (long)(tfirst + diff),
2446 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
2447 (long)tfirst, (long)rfirst);
2449 if (rfirst + diff > max)
2450 max = rfirst + diff;
2452 grows = (tfirst < rfirst &&
2453 UNISKIP(tfirst) < UNISKIP(rfirst + diff));
2465 else if (max > 0xff)
2470 Safefree(cPVOPo->op_pv);
2471 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2472 SvREFCNT_dec(listsv);
2474 SvREFCNT_dec(transv);
2476 if (!del && havefinal && rlen)
2477 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5,
2478 newSVuv((UV)final), 0);
2481 o->op_private |= OPpTRANS_GROWS;
2493 tbl = (short*)cPVOPo->op_pv;
2495 Zero(tbl, 256, short);
2496 for (i = 0; i < (I32)tlen; i++)
2498 for (i = 0, j = 0; i < 256; i++) {
2500 if (j >= (I32)rlen) {
2509 if (i < 128 && r[j] >= 128)
2519 o->op_private |= OPpTRANS_IDENTICAL;
2521 else if (j >= (I32)rlen)
2524 cPVOPo->op_pv = (char*)Renew(tbl, 0x101+rlen-j, short);
2525 tbl[0x100] = rlen - j;
2526 for (i=0; i < (I32)rlen - j; i++)
2527 tbl[0x101+i] = r[j+i];
2531 if (!rlen && !del) {
2534 o->op_private |= OPpTRANS_IDENTICAL;
2536 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
2537 o->op_private |= OPpTRANS_IDENTICAL;
2539 for (i = 0; i < 256; i++)
2541 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
2542 if (j >= (I32)rlen) {
2544 if (tbl[t[i]] == -1)
2550 if (tbl[t[i]] == -1) {
2551 if (t[i] < 128 && r[j] >= 128)
2558 o->op_private |= OPpTRANS_GROWS;
2566 Perl_newPMOP(pTHX_ I32 type, I32 flags)
2570 NewOp(1101, pmop, 1, PMOP);
2571 pmop->op_type = (OPCODE)type;
2572 pmop->op_ppaddr = PL_ppaddr[type];
2573 pmop->op_flags = (U8)flags;
2574 pmop->op_private = (U8)(0 | (flags >> 8));
2576 if (PL_hints & HINT_RE_TAINT)
2577 pmop->op_pmpermflags |= PMf_RETAINT;
2578 if (PL_hints & HINT_LOCALE)
2579 pmop->op_pmpermflags |= PMf_LOCALE;
2580 pmop->op_pmflags = pmop->op_pmpermflags;
2585 if(av_len((AV*) PL_regex_pad[0]) > -1) {
2586 repointer = av_pop((AV*)PL_regex_pad[0]);
2587 pmop->op_pmoffset = SvIV(repointer);
2588 SvREPADTMP_off(repointer);
2589 sv_setiv(repointer,0);
2591 repointer = newSViv(0);
2592 av_push(PL_regex_padav,SvREFCNT_inc(repointer));
2593 pmop->op_pmoffset = av_len(PL_regex_padav);
2594 PL_regex_pad = AvARRAY(PL_regex_padav);
2599 /* link into pm list */
2600 if (type != OP_TRANS && PL_curstash) {
2601 pmop->op_pmnext = HvPMROOT(PL_curstash);
2602 HvPMROOT(PL_curstash) = pmop;
2603 PmopSTASH_set(pmop,PL_curstash);
2606 return CHECKOP(type, pmop);
2610 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
2614 I32 repl_has_vars = 0;
2616 if (o->op_type == OP_TRANS)
2617 return pmtrans(o, expr, repl);
2619 PL_hints |= HINT_BLOCK_SCOPE;
2622 if (expr->op_type == OP_CONST) {
2624 SV *pat = ((SVOP*)expr)->op_sv;
2625 char *p = SvPV(pat, plen);
2626 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2627 sv_setpvn(pat, "\\s+", 3);
2628 p = SvPV(pat, plen);
2629 pm->op_pmflags |= PMf_SKIPWHITE;
2632 pm->op_pmdynflags |= PMdf_UTF8;
2633 PM_SETRE(pm, CALLREGCOMP(aTHX_ p, p + plen, pm));
2634 if (strEQ("\\s+", PM_GETRE(pm)->precomp))
2635 pm->op_pmflags |= PMf_WHITE;
2639 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2640 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2642 : OP_REGCMAYBE),0,expr);
2644 NewOp(1101, rcop, 1, LOGOP);
2645 rcop->op_type = OP_REGCOMP;
2646 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2647 rcop->op_first = scalar(expr);
2648 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2649 ? (OPf_SPECIAL | OPf_KIDS)
2651 rcop->op_private = 1;
2653 /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */
2656 /* establish postfix order */
2657 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2659 rcop->op_next = expr;
2660 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2663 rcop->op_next = LINKLIST(expr);
2664 expr->op_next = (OP*)rcop;
2667 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2672 if (pm->op_pmflags & PMf_EVAL) {
2674 if (CopLINE(PL_curcop) < (line_t)PL_multi_end)
2675 CopLINE_set(PL_curcop, (line_t)PL_multi_end);
2677 else if (repl->op_type == OP_CONST)
2681 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2682 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2683 if (curop->op_type == OP_GV) {
2684 GV *gv = cGVOPx_gv(curop);
2686 if (strchr("&`'123456789+-\016\022", *GvENAME(gv)))
2689 else if (curop->op_type == OP_RV2CV)
2691 else if (curop->op_type == OP_RV2SV ||
2692 curop->op_type == OP_RV2AV ||
2693 curop->op_type == OP_RV2HV ||
2694 curop->op_type == OP_RV2GV) {
2695 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2698 else if (curop->op_type == OP_PADSV ||
2699 curop->op_type == OP_PADAV ||
2700 curop->op_type == OP_PADHV ||
2701 curop->op_type == OP_PADANY) {
2704 else if (curop->op_type == OP_PUSHRE)
2705 ; /* Okay here, dangerous in newASSIGNOP */
2715 || PM_GETRE(pm)->reganch & ROPT_EVAL_SEEN))) {
2716 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2717 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2718 prepend_elem(o->op_type, scalar(repl), o);
2721 if (curop == repl && !PM_GETRE(pm)) { /* Has variables. */
2722 pm->op_pmflags |= PMf_MAYBE_CONST;
2723 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2725 NewOp(1101, rcop, 1, LOGOP);
2726 rcop->op_type = OP_SUBSTCONT;
2727 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
2728 rcop->op_first = scalar(repl);
2729 rcop->op_flags |= OPf_KIDS;
2730 rcop->op_private = 1;
2733 /* establish postfix order */
2734 rcop->op_next = LINKLIST(repl);
2735 repl->op_next = (OP*)rcop;
2737 pm->op_pmreplroot = scalar((OP*)rcop);
2738 pm->op_pmreplstart = LINKLIST(rcop);
2747 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
2750 NewOp(1101, svop, 1, SVOP);
2751 svop->op_type = (OPCODE)type;
2752 svop->op_ppaddr = PL_ppaddr[type];
2754 svop->op_next = (OP*)svop;
2755 svop->op_flags = (U8)flags;
2756 if (PL_opargs[type] & OA_RETSCALAR)
2758 if (PL_opargs[type] & OA_TARGET)
2759 svop->op_targ = pad_alloc(type, SVs_PADTMP);
2760 return CHECKOP(type, svop);
2764 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
2767 NewOp(1101, padop, 1, PADOP);
2768 padop->op_type = (OPCODE)type;
2769 padop->op_ppaddr = PL_ppaddr[type];
2770 padop->op_padix = pad_alloc(type, SVs_PADTMP);
2771 SvREFCNT_dec(PAD_SVl(padop->op_padix));
2772 PAD_SETSV(padop->op_padix, sv);
2775 padop->op_next = (OP*)padop;
2776 padop->op_flags = (U8)flags;
2777 if (PL_opargs[type] & OA_RETSCALAR)
2779 if (PL_opargs[type] & OA_TARGET)
2780 padop->op_targ = pad_alloc(type, SVs_PADTMP);
2781 return CHECKOP(type, padop);
2785 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
2790 return newPADOP(type, flags, SvREFCNT_inc(gv));
2792 return newSVOP(type, flags, SvREFCNT_inc(gv));
2797 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
2800 NewOp(1101, pvop, 1, PVOP);
2801 pvop->op_type = (OPCODE)type;
2802 pvop->op_ppaddr = PL_ppaddr[type];
2804 pvop->op_next = (OP*)pvop;
2805 pvop->op_flags = (U8)flags;
2806 if (PL_opargs[type] & OA_RETSCALAR)
2808 if (PL_opargs[type] & OA_TARGET)
2809 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2810 return CHECKOP(type, pvop);
2814 Perl_package(pTHX_ OP *o)
2819 save_hptr(&PL_curstash);
2820 save_item(PL_curstname);
2822 name = SvPV(cSVOPo->op_sv, len);
2823 PL_curstash = gv_stashpvn(name, len, TRUE);
2824 sv_setpvn(PL_curstname, name, len);
2827 PL_hints |= HINT_BLOCK_SCOPE;
2828 PL_copline = NOLINE;
2833 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
2839 if (idop->op_type != OP_CONST)
2840 Perl_croak(aTHX_ "Module name must be constant");
2844 if (version != Nullop) {
2845 SV *vesv = ((SVOP*)version)->op_sv;
2847 if (arg == Nullop && !SvNIOKp(vesv)) {
2854 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
2855 Perl_croak(aTHX_ "Version number must be constant number");
2857 /* Make copy of idop so we don't free it twice */
2858 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
2860 /* Fake up a method call to VERSION */
2861 meth = newSVpvn("VERSION",7);
2862 sv_upgrade(meth, SVt_PVIV);
2863 (void)SvIOK_on(meth);
2864 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
2865 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2866 append_elem(OP_LIST,
2867 prepend_elem(OP_LIST, pack, list(version)),
2868 newSVOP(OP_METHOD_NAMED, 0, meth)));
2872 /* Fake up an import/unimport */
2873 if (arg && arg->op_type == OP_STUB)
2874 imop = arg; /* no import on explicit () */
2875 else if (SvNIOKp(((SVOP*)idop)->op_sv)) {
2876 imop = Nullop; /* use 5.0; */
2881 /* Make copy of idop so we don't free it twice */
2882 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
2884 /* Fake up a method call to import/unimport */
2885 meth = aver ? newSVpvn("import",6) : newSVpvn("unimport", 8);
2886 (void)SvUPGRADE(meth, SVt_PVIV);
2887 (void)SvIOK_on(meth);
2888 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
2889 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2890 append_elem(OP_LIST,
2891 prepend_elem(OP_LIST, pack, list(arg)),
2892 newSVOP(OP_METHOD_NAMED, 0, meth)));
2895 /* Fake up the BEGIN {}, which does its thing immediately. */
2897 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
2900 append_elem(OP_LINESEQ,
2901 append_elem(OP_LINESEQ,
2902 newSTATEOP(0, Nullch, newUNOP(OP_REQUIRE, 0, idop)),
2903 newSTATEOP(0, Nullch, veop)),
2904 newSTATEOP(0, Nullch, imop) ));
2906 /* The "did you use incorrect case?" warning used to be here.
2907 * The problem is that on case-insensitive filesystems one
2908 * might get false positives for "use" (and "require"):
2909 * "use Strict" or "require CARP" will work. This causes
2910 * portability problems for the script: in case-strict
2911 * filesystems the script will stop working.
2913 * The "incorrect case" warning checked whether "use Foo"
2914 * imported "Foo" to your namespace, but that is wrong, too:
2915 * there is no requirement nor promise in the language that
2916 * a Foo.pm should or would contain anything in package "Foo".
2918 * There is very little Configure-wise that can be done, either:
2919 * the case-sensitivity of the build filesystem of Perl does not
2920 * help in guessing the case-sensitivity of the runtime environment.
2923 PL_hints |= HINT_BLOCK_SCOPE;
2924 PL_copline = NOLINE;
2926 PL_cop_seqmax++; /* Purely for B::*'s benefit */
2930 =head1 Embedding Functions
2932 =for apidoc load_module
2934 Loads the module whose name is pointed to by the string part of name.
2935 Note that the actual module name, not its filename, should be given.
2936 Eg, "Foo::Bar" instead of "Foo/Bar.pm". flags can be any of
2937 PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS
2938 (or 0 for no flags). ver, if specified, provides version semantics
2939 similar to C<use Foo::Bar VERSION>. The optional trailing SV*
2940 arguments can be used to specify arguments to the module's import()
2941 method, similar to C<use Foo::Bar VERSION LIST>.
2946 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
2949 va_start(args, ver);
2950 vload_module(flags, name, ver, &args);
2954 #ifdef PERL_IMPLICIT_CONTEXT
2956 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
2960 va_start(args, ver);
2961 vload_module(flags, name, ver, &args);
2967 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
2969 OP *modname, *veop, *imop;
2971 modname = newSVOP(OP_CONST, 0, name);
2972 modname->op_private |= OPpCONST_BARE;
2974 veop = newSVOP(OP_CONST, 0, ver);
2978 if (flags & PERL_LOADMOD_NOIMPORT) {
2979 imop = sawparens(newNULLLIST());
2981 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
2982 imop = va_arg(*args, OP*);
2987 sv = va_arg(*args, SV*);
2989 imop = append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
2990 sv = va_arg(*args, SV*);
2994 line_t ocopline = PL_copline;
2995 COP *ocurcop = PL_curcop;
2996 int oexpect = PL_expect;
2998 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
2999 veop, modname, imop);
3000 PL_expect = oexpect;
3001 PL_copline = ocopline;
3002 PL_curcop = ocurcop;
3007 Perl_dofile(pTHX_ OP *term)
3012 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
3013 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv)))
3014 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
3016 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
3017 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3018 append_elem(OP_LIST, term,
3019 scalar(newUNOP(OP_RV2CV, 0,
3024 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3030 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
3032 return newBINOP(OP_LSLICE, flags,
3033 list(force_list(subscript)),
3034 list(force_list(listval)) );
3038 S_list_assignment(pTHX_ register OP *o)
3043 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3044 o = cUNOPo->op_first;
3046 if (o->op_type == OP_COND_EXPR) {
3047 I32 t = list_assignment(cLOGOPo->op_first->op_sibling);
3048 I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
3053 yyerror("Assignment to both a list and a scalar");
3057 if (o->op_type == OP_LIST &&
3058 (o->op_flags & OPf_WANT) == OPf_WANT_SCALAR &&
3059 o->op_private & OPpLVAL_INTRO)
3062 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3063 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3064 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
3067 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
3070 if (o->op_type == OP_RV2SV)
3077 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
3082 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN || optype == OP_DORASSIGN) {
3083 return newLOGOP(optype, 0,
3084 mod(scalar(left), optype),
3085 newUNOP(OP_SASSIGN, 0, scalar(right)));
3088 return newBINOP(optype, OPf_STACKED,
3089 mod(scalar(left), optype), scalar(right));
3093 if (list_assignment(left)) {
3097 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3098 left = mod(left, OP_AASSIGN);
3106 curop = list(force_list(left));
3107 o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
3108 o->op_private = (U8)(0 | (flags >> 8));
3110 /* PL_generation sorcery:
3111 * an assignment like ($a,$b) = ($c,$d) is easier than
3112 * ($a,$b) = ($c,$a), since there is no need for temporary vars.
3113 * To detect whether there are common vars, the global var
3114 * PL_generation is incremented for each assign op we compile.
3115 * Then, while compiling the assign op, we run through all the
3116 * variables on both sides of the assignment, setting a spare slot
3117 * in each of them to PL_generation. If any of them already have
3118 * that value, we know we've got commonality. We could use a
3119 * single bit marker, but then we'd have to make 2 passes, first
3120 * to clear the flag, then to test and set it. To find somewhere
3121 * to store these values, evil chicanery is done with SvCUR().
3124 if (!(left->op_private & OPpLVAL_INTRO)) {
3127 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3128 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3129 if (curop->op_type == OP_GV) {
3130 GV *gv = cGVOPx_gv(curop);
3131 if (gv == PL_defgv || (int)SvCUR(gv) == PL_generation)
3133 SvCUR(gv) = PL_generation;
3135 else if (curop->op_type == OP_PADSV ||
3136 curop->op_type == OP_PADAV ||
3137 curop->op_type == OP_PADHV ||
3138 curop->op_type == OP_PADANY)
3140 if (PAD_COMPNAME_GEN(curop->op_targ)
3141 == (STRLEN)PL_generation)
3143 PAD_COMPNAME_GEN(curop->op_targ)
3147 else if (curop->op_type == OP_RV2CV)
3149 else if (curop->op_type == OP_RV2SV ||
3150 curop->op_type == OP_RV2AV ||
3151 curop->op_type == OP_RV2HV ||
3152 curop->op_type == OP_RV2GV) {
3153 if (lastop->op_type != OP_GV) /* funny deref? */
3156 else if (curop->op_type == OP_PUSHRE) {
3157 if (((PMOP*)curop)->op_pmreplroot) {
3159 GV *gv = (GV*)PAD_SVl(INT2PTR(PADOFFSET,
3160 ((PMOP*)curop)->op_pmreplroot));
3162 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3164 if (gv == PL_defgv || (int)SvCUR(gv) == PL_generation)
3166 SvCUR(gv) = PL_generation;
3175 o->op_private |= OPpASSIGN_COMMON;
3177 if (right && right->op_type == OP_SPLIT) {
3179 if ((tmpop = ((LISTOP*)right)->op_first) &&
3180 tmpop->op_type == OP_PUSHRE)
3182 PMOP *pm = (PMOP*)tmpop;
3183 if (left->op_type == OP_RV2AV &&
3184 !(left->op_private & OPpLVAL_INTRO) &&
3185 !(o->op_private & OPpASSIGN_COMMON) )
3187 tmpop = ((UNOP*)left)->op_first;
3188 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
3190 pm->op_pmreplroot = INT2PTR(OP*, cPADOPx(tmpop)->op_padix);
3191 cPADOPx(tmpop)->op_padix = 0; /* steal it */
3193 pm->op_pmreplroot = (OP*)cSVOPx(tmpop)->op_sv;
3194 cSVOPx(tmpop)->op_sv = Nullsv; /* steal it */
3196 pm->op_pmflags |= PMf_ONCE;
3197 tmpop = cUNOPo->op_first; /* to list (nulled) */
3198 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3199 tmpop->op_sibling = Nullop; /* don't free split */
3200 right->op_next = tmpop->op_next; /* fix starting loc */
3201 op_free(o); /* blow off assign */
3202 right->op_flags &= ~OPf_WANT;
3203 /* "I don't know and I don't care." */
3208 if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
3209 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3211 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3213 sv_setiv(sv, PL_modcount+1);
3221 right = newOP(OP_UNDEF, 0);
3222 if (right->op_type == OP_READLINE) {
3223 right->op_flags |= OPf_STACKED;
3224 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3227 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3228 o = newBINOP(OP_SASSIGN, flags,
3229 scalar(right), mod(scalar(left), OP_SASSIGN) );
3241 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3243 U32 seq = intro_my();
3246 NewOp(1101, cop, 1, COP);
3247 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
3248 cop->op_type = OP_DBSTATE;
3249 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3252 cop->op_type = OP_NEXTSTATE;
3253 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3255 cop->op_flags = (U8)flags;
3256 cop->op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
3258 cop->op_private |= NATIVE_HINTS;
3260 PL_compiling.op_private = cop->op_private;
3261 cop->op_next = (OP*)cop;
3264 cop->cop_label = label;
3265 PL_hints |= HINT_BLOCK_SCOPE;
3268 cop->cop_arybase = PL_curcop->cop_arybase;
3269 if (specialWARN(PL_curcop->cop_warnings))
3270 cop->cop_warnings = PL_curcop->cop_warnings ;
3272 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3273 if (specialCopIO(PL_curcop->cop_io))
3274 cop->cop_io = PL_curcop->cop_io;
3276 cop->cop_io = newSVsv(PL_curcop->cop_io) ;
3279 if (PL_copline == NOLINE)
3280 CopLINE_set(cop, CopLINE(PL_curcop));
3282 CopLINE_set(cop, PL_copline);
3283 PL_copline = NOLINE;
3286 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
3288 CopFILEGV_set(cop, CopFILEGV(PL_curcop));
3290 CopSTASH_set(cop, PL_curstash);
3292 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3293 SV **svp = av_fetch(CopFILEAV(PL_curcop), (I32)CopLINE(cop), FALSE);
3294 if (svp && *svp != &PL_sv_undef ) {
3295 (void)SvIOK_on(*svp);
3296 SvIVX(*svp) = PTR2IV(cop);
3300 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3305 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
3307 return new_logop(type, flags, &first, &other);
3311 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
3315 OP *first = *firstp;
3316 OP *other = *otherp;
3318 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3319 return newBINOP(type, flags, scalar(first), scalar(other));
3321 scalarboolean(first);
3322 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3323 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3324 if (type == OP_AND || type == OP_OR) {
3330 first = *firstp = cUNOPo->op_first;
3332 first->op_next = o->op_next;
3333 cUNOPo->op_first = Nullop;
3337 if (first->op_type == OP_CONST) {
3338 if (ckWARN(WARN_BAREWORD) && (first->op_private & OPpCONST_BARE)) {
3339 if (first->op_private & OPpCONST_STRICT)
3340 no_bareword_allowed(first);
3342 Perl_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional");
3344 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3355 else if (ckWARN(WARN_MISC) && (first->op_flags & OPf_KIDS) &&
3356 type != OP_DOR) /* [#24076] Don't warn for <FH> err FOO. */
3358 OP *k1 = ((UNOP*)first)->op_first;
3359 OP *k2 = k1->op_sibling;
3361 switch (first->op_type)
3364 if (k2 && k2->op_type == OP_READLINE
3365 && (k2->op_flags & OPf_STACKED)
3366 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3368 warnop = k2->op_type;
3373 if (k1->op_type == OP_READDIR
3374 || k1->op_type == OP_GLOB
3375 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3376 || k1->op_type == OP_EACH)
3378 warnop = ((k1->op_type == OP_NULL)
3379 ? (OPCODE)k1->op_targ : k1->op_type);
3384 line_t oldline = CopLINE(PL_curcop);
3385 CopLINE_set(PL_curcop, PL_copline);
3386 Perl_warner(aTHX_ packWARN(WARN_MISC),
3387 "Value of %s%s can be \"0\"; test with defined()",
3389 ((warnop == OP_READLINE || warnop == OP_GLOB)
3390 ? " construct" : "() operator"));
3391 CopLINE_set(PL_curcop, oldline);
3398 if (type == OP_ANDASSIGN || type == OP_ORASSIGN || type == OP_DORASSIGN)
3399 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3401 NewOp(1101, logop, 1, LOGOP);
3403 logop->op_type = (OPCODE)type;
3404 logop->op_ppaddr = PL_ppaddr[type];
3405 logop->op_first = first;
3406 logop->op_flags = flags | OPf_KIDS;
3407 logop->op_other = LINKLIST(other);
3408 logop->op_private = (U8)(1 | (flags >> 8));
3410 /* establish postfix order */
3411 logop->op_next = LINKLIST(first);
3412 first->op_next = (OP*)logop;
3413 first->op_sibling = other;
3415 CHECKOP(type,logop);
3417 o = newUNOP(OP_NULL, 0, (OP*)logop);
3424 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
3431 return newLOGOP(OP_AND, 0, first, trueop);
3433 return newLOGOP(OP_OR, 0, first, falseop);
3435 scalarboolean(first);
3436 if (first->op_type == OP_CONST) {
3437 if (first->op_private & OPpCONST_BARE &&
3438 first->op_private & OPpCONST_STRICT) {
3439 no_bareword_allowed(first);
3441 if (SvTRUE(((SVOP*)first)->op_sv)) {
3452 NewOp(1101, logop, 1, LOGOP);
3453 logop->op_type = OP_COND_EXPR;
3454 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3455 logop->op_first = first;
3456 logop->op_flags = flags | OPf_KIDS;
3457 logop->op_private = (U8)(1 | (flags >> 8));
3458 logop->op_other = LINKLIST(trueop);
3459 logop->op_next = LINKLIST(falseop);
3461 CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
3464 /* establish postfix order */
3465 start = LINKLIST(first);
3466 first->op_next = (OP*)logop;
3468 first->op_sibling = trueop;
3469 trueop->op_sibling = falseop;
3470 o = newUNOP(OP_NULL, 0, (OP*)logop);
3472 trueop->op_next = falseop->op_next = o;
3479 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
3487 NewOp(1101, range, 1, LOGOP);
3489 range->op_type = OP_RANGE;
3490 range->op_ppaddr = PL_ppaddr[OP_RANGE];
3491 range->op_first = left;
3492 range->op_flags = OPf_KIDS;
3493 leftstart = LINKLIST(left);
3494 range->op_other = LINKLIST(right);
3495 range->op_private = (U8)(1 | (flags >> 8));
3497 left->op_sibling = right;
3499 range->op_next = (OP*)range;
3500 flip = newUNOP(OP_FLIP, flags, (OP*)range);
3501 flop = newUNOP(OP_FLOP, 0, flip);
3502 o = newUNOP(OP_NULL, 0, flop);
3504 range->op_next = leftstart;
3506 left->op_next = flip;
3507 right->op_next = flop;
3509 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3510 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
3511 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3512 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3514 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3515 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3518 if (!flip->op_private || !flop->op_private)
3519 linklist(o); /* blow off optimizer unless constant */
3525 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
3529 int once = block && block->op_flags & OPf_SPECIAL &&
3530 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3533 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3534 return block; /* do {} while 0 does once */
3535 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3536 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3537 expr = newUNOP(OP_DEFINED, 0,
3538 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3539 } else if (expr->op_flags & OPf_KIDS) {
3540 OP *k1 = ((UNOP*)expr)->op_first;
3541 OP *k2 = (k1) ? k1->op_sibling : NULL;
3542 switch (expr->op_type) {
3544 if (k2 && k2->op_type == OP_READLINE
3545 && (k2->op_flags & OPf_STACKED)
3546 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3547 expr = newUNOP(OP_DEFINED, 0, expr);
3551 if (k1->op_type == OP_READDIR
3552 || k1->op_type == OP_GLOB
3553 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3554 || k1->op_type == OP_EACH)
3555 expr = newUNOP(OP_DEFINED, 0, expr);
3561 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3562 o = new_logop(OP_AND, 0, &expr, &listop);
3565 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3567 if (once && o != listop)
3568 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3571 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3573 o->op_flags |= flags;
3575 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3580 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
3588 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3589 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
3590 expr = newUNOP(OP_DEFINED, 0,
3591 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3592 } else if (expr && (expr->op_flags & OPf_KIDS)) {
3593 OP *k1 = ((UNOP*)expr)->op_first;
3594 OP *k2 = (k1) ? k1->op_sibling : NULL;
3595 switch (expr->op_type) {
3597 if (k2 && k2->op_type == OP_READLINE
3598 && (k2->op_flags & OPf_STACKED)
3599 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3600 expr = newUNOP(OP_DEFINED, 0, expr);
3604 if (k1->op_type == OP_READDIR
3605 || k1->op_type == OP_GLOB
3606 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3607 || k1->op_type == OP_EACH)
3608 expr = newUNOP(OP_DEFINED, 0, expr);
3614 block = newOP(OP_NULL, 0);
3616 block = scope(block);
3620 next = LINKLIST(cont);
3623 OP *unstack = newOP(OP_UNSTACK, 0);
3626 cont = append_elem(OP_LINESEQ, cont, unstack);
3629 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3630 redo = LINKLIST(listop);
3633 PL_copline = (line_t)whileline;
3635 o = new_logop(OP_AND, 0, &expr, &listop);
3636 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3637 op_free(expr); /* oops, it's a while (0) */
3639 return Nullop; /* listop already freed by new_logop */
3642 ((LISTOP*)listop)->op_last->op_next =
3643 (o == listop ? redo : LINKLIST(o));
3649 NewOp(1101,loop,1,LOOP);
3650 loop->op_type = OP_ENTERLOOP;
3651 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3652 loop->op_private = 0;
3653 loop->op_next = (OP*)loop;
3656 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3658 loop->op_redoop = redo;
3659 loop->op_lastop = o;
3660 o->op_private |= loopflags;
3663 loop->op_nextop = next;
3665 loop->op_nextop = o;
3667 o->op_flags |= flags;
3668 o->op_private |= (flags >> 8);
3673 Perl_newFOROP(pTHX_ I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
3677 PADOFFSET padoff = 0;
3682 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3683 iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */
3684 sv->op_type = OP_RV2GV;
3685 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
3687 else if (sv->op_type == OP_PADSV) { /* private variable */
3688 iterpflags = sv->op_private & OPpLVAL_INTRO; /* for my $x () */
3689 padoff = sv->op_targ;
3694 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3695 padoff = sv->op_targ;
3697 iterflags |= OPf_SPECIAL;
3702 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
3705 sv = newGVOP(OP_GV, 0, PL_defgv);
3707 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3708 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3709 iterflags |= OPf_STACKED;
3711 else if (expr->op_type == OP_NULL &&
3712 (expr->op_flags & OPf_KIDS) &&
3713 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3715 /* Basically turn for($x..$y) into the same as for($x,$y), but we
3716 * set the STACKED flag to indicate that these values are to be
3717 * treated as min/max values by 'pp_iterinit'.
3719 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3720 LOGOP* range = (LOGOP*) flip->op_first;
3721 OP* left = range->op_first;
3722 OP* right = left->op_sibling;
3725 range->op_flags &= ~OPf_KIDS;
3726 range->op_first = Nullop;
3728 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3729 listop->op_first->op_next = range->op_next;
3730 left->op_next = range->op_other;
3731 right->op_next = (OP*)listop;
3732 listop->op_next = listop->op_first;
3735 expr = (OP*)(listop);
3737 iterflags |= OPf_STACKED;
3740 expr = mod(force_list(expr), OP_GREPSTART);
3744 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
3745 append_elem(OP_LIST, expr, scalar(sv))));
3746 assert(!loop->op_next);
3747 /* for my $x () sets OPpLVAL_INTRO;
3748 * for our $x () sets OPpOUR_INTRO */
3749 loop->op_private = (U8)iterpflags;
3750 #ifdef PL_OP_SLAB_ALLOC
3753 NewOp(1234,tmp,1,LOOP);
3754 Copy(loop,tmp,1,LOOP);
3759 Renew(loop, 1, LOOP);
3761 loop->op_targ = padoff;
3762 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
3763 PL_copline = forline;
3764 return newSTATEOP(0, label, wop);
3768 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
3773 if (type != OP_GOTO || label->op_type == OP_CONST) {
3774 /* "last()" means "last" */
3775 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
3776 o = newOP(type, OPf_SPECIAL);
3778 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
3779 ? SvPVx(((SVOP*)label)->op_sv, n_a)
3785 /* Check whether it's going to be a goto &function */
3786 if (label->op_type == OP_ENTERSUB
3787 && !(label->op_flags & OPf_STACKED))
3788 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
3789 o = newUNOP(type, OPf_STACKED, label);
3791 PL_hints |= HINT_BLOCK_SCOPE;
3796 =for apidoc cv_undef
3798 Clear out all the active components of a CV. This can happen either
3799 by an explicit C<undef &foo>, or by the reference count going to zero.
3800 In the former case, we keep the CvOUTSIDE pointer, so that any anonymous
3801 children can still follow the full lexical scope chain.
3807 Perl_cv_undef(pTHX_ CV *cv)
3810 if (CvFILE(cv) && !CvXSUB(cv)) {
3811 /* for XSUBs CvFILE point directly to static memory; __FILE__ */
3812 Safefree(CvFILE(cv));
3817 if (!CvXSUB(cv) && CvROOT(cv)) {
3819 Perl_croak(aTHX_ "Can't undef active subroutine");
3822 PAD_SAVE_SETNULLPAD();
3824 op_free(CvROOT(cv));
3825 CvROOT(cv) = Nullop;
3828 SvPOK_off((SV*)cv); /* forget prototype */
3833 /* remove CvOUTSIDE unless this is an undef rather than a free */
3834 if (!SvREFCNT(cv) && CvOUTSIDE(cv)) {
3835 if (!CvWEAKOUTSIDE(cv))
3836 SvREFCNT_dec(CvOUTSIDE(cv));
3837 CvOUTSIDE(cv) = Nullcv;
3840 SvREFCNT_dec((SV*)CvXSUBANY(cv).any_ptr);
3846 /* delete all flags except WEAKOUTSIDE */
3847 CvFLAGS(cv) &= CVf_WEAKOUTSIDE;
3851 Perl_cv_ckproto(pTHX_ CV *cv, GV *gv, char *p)
3853 if (((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) && ckWARN_d(WARN_PROTOTYPE)) {
3854 SV* msg = sv_newmortal();
3858 gv_efullname3(name = sv_newmortal(), gv, Nullch);
3859 sv_setpv(msg, "Prototype mismatch:");
3861 Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, name);
3863 Perl_sv_catpvf(aTHX_ msg, " (%"SVf")", (SV *)cv);
3864 sv_catpv(msg, " vs ");
3866 Perl_sv_catpvf(aTHX_ msg, "(%s)", p);
3868 sv_catpv(msg, "none");
3869 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%"SVf, msg);
3873 static void const_sv_xsub(pTHX_ CV* cv);
3877 =head1 Optree Manipulation Functions
3879 =for apidoc cv_const_sv
3881 If C<cv> is a constant sub eligible for inlining. returns the constant
3882 value returned by the sub. Otherwise, returns NULL.
3884 Constant subs can be created with C<newCONSTSUB> or as described in
3885 L<perlsub/"Constant Functions">.
3890 Perl_cv_const_sv(pTHX_ CV *cv)
3892 if (!cv || !CvCONST(cv))
3894 return (SV*)CvXSUBANY(cv).any_ptr;
3897 /* op_const_sv: examine an optree to determine whether it's in-lineable.
3898 * Can be called in 3 ways:
3901 * look for a single OP_CONST with attached value: return the value
3903 * cv && CvCLONE(cv) && !CvCONST(cv)
3905 * examine the clone prototype, and if contains only a single
3906 * OP_CONST referencing a pad const, or a single PADSV referencing
3907 * an outer lexical, return a non-zero value to indicate the CV is
3908 * a candidate for "constizing" at clone time
3912 * We have just cloned an anon prototype that was marked as a const
3913 * candidiate. Try to grab the current value, and in the case of
3914 * PADSV, ignore it if it has multiple references. Return the value.
3918 Perl_op_const_sv(pTHX_ OP *o, CV *cv)
3925 if (o->op_type == OP_LINESEQ && cLISTOPo->op_first)
3926 o = cLISTOPo->op_first->op_sibling;
3928 for (; o; o = o->op_next) {
3929 OPCODE type = o->op_type;
3931 if (sv && o->op_next == o)
3933 if (o->op_next != o) {
3934 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
3936 if (type == OP_DBSTATE)
3939 if (type == OP_LEAVESUB || type == OP_RETURN)
3943 if (type == OP_CONST && cSVOPo->op_sv)
3945 else if (cv && type == OP_CONST) {
3946 sv = PAD_BASE_SV(CvPADLIST(cv), o->op_targ);
3950 else if (cv && type == OP_PADSV) {
3951 if (CvCONST(cv)) { /* newly cloned anon */
3952 sv = PAD_BASE_SV(CvPADLIST(cv), o->op_targ);
3953 /* the candidate should have 1 ref from this pad and 1 ref
3954 * from the parent */
3955 if (!sv || SvREFCNT(sv) != 2)
3962 if (PAD_COMPNAME_FLAGS(o->op_targ) & SVf_FAKE)
3963 sv = &PL_sv_undef; /* an arbitrary non-null value */
3974 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
3984 Perl_croak(aTHX_ "\"my sub\" not yet implemented");
3988 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
3990 return Perl_newATTRSUB(aTHX_ floor, o, proto, Nullop, block);
3994 Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4000 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, n_a) : Nullch;
4004 name = o ? SvPVx(cSVOPo->op_sv, n_a) : Nullch;
4005 if (!name && PERLDB_NAMEANON && CopLINE(PL_curcop)) {
4006 SV *sv = sv_newmortal();
4007 Perl_sv_setpvf(aTHX_ sv, "%s[%s:%"IVdf"]",
4008 PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
4009 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
4014 gv = gv_fetchpv(name ? name : (aname ? aname :
4015 (PL_curstash ? "__ANON__" : "__ANON__::__ANON__")),
4016 GV_ADDMULTI | ((block || attrs) ? 0 : GV_NOINIT),
4026 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
4027 maximum a prototype before. */
4028 if (SvTYPE(gv) > SVt_NULL) {
4029 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1)
4030 && ckWARN_d(WARN_PROTOTYPE))
4032 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "Runaway prototype");
4034 cv_ckproto((CV*)gv, NULL, ps);
4037 sv_setpv((SV*)gv, ps);
4039 sv_setiv((SV*)gv, -1);
4040 SvREFCNT_dec(PL_compcv);
4041 cv = PL_compcv = NULL;
4042 PL_sub_generation++;
4046 cv = (!name || GvCVGEN(gv)) ? Nullcv : GvCV(gv);
4048 #ifdef GV_UNIQUE_CHECK
4049 if (cv && GvUNIQUE(gv) && SvREADONLY(cv)) {
4050 Perl_croak(aTHX_ "Can't define subroutine %s (GV is unique)", name);
4054 if (!block || !ps || *ps || attrs)
4057 const_sv = op_const_sv(block, Nullcv);
4060 bool exists = CvROOT(cv) || CvXSUB(cv);
4062 #ifdef GV_UNIQUE_CHECK
4063 if (exists && GvUNIQUE(gv)) {
4064 Perl_croak(aTHX_ "Can't redefine unique subroutine %s", name);
4068 /* if the subroutine doesn't exist and wasn't pre-declared
4069 * with a prototype, assume it will be AUTOLOADed,
4070 * skipping the prototype check
4072 if (exists || SvPOK(cv))
4073 cv_ckproto(cv, gv, ps);
4074 /* already defined (or promised)? */
4075 if (exists || GvASSUMECV(gv)) {
4076 if (!block && !attrs) {
4077 if (CvFLAGS(PL_compcv)) {
4078 /* might have had built-in attrs applied */
4079 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
4081 /* just a "sub foo;" when &foo is already defined */
4082 SAVEFREESV(PL_compcv);
4085 /* ahem, death to those who redefine active sort subs */
4086 if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv))
4087 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", name);
4089 if (ckWARN(WARN_REDEFINE)
4091 && (!const_sv || sv_cmp(cv_const_sv(cv), const_sv))))
4093 line_t oldline = CopLINE(PL_curcop);
4094 if (PL_copline != NOLINE)
4095 CopLINE_set(PL_curcop, PL_copline);
4096 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
4097 CvCONST(cv) ? "Constant subroutine %s redefined"
4098 : "Subroutine %s redefined", name);
4099 CopLINE_set(PL_curcop, oldline);
4107 SvREFCNT_inc(const_sv);
4109 assert(!CvROOT(cv) && !CvCONST(cv));
4110 sv_setpv((SV*)cv, ""); /* prototype is "" */
4111 CvXSUBANY(cv).any_ptr = const_sv;
4112 CvXSUB(cv) = const_sv_xsub;
4117 cv = newCONSTSUB(NULL, name, const_sv);
4120 SvREFCNT_dec(PL_compcv);
4122 PL_sub_generation++;
4129 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>
4130 * before we clobber PL_compcv.
4134 /* Might have had built-in attributes applied -- propagate them. */
4135 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
4136 if (CvGV(cv) && GvSTASH(CvGV(cv)))
4137 stash = GvSTASH(CvGV(cv));
4138 else if (CvSTASH(cv))
4139 stash = CvSTASH(cv);
4141 stash = PL_curstash;
4144 /* possibly about to re-define existing subr -- ignore old cv */
4145 rcv = (SV*)PL_compcv;
4146 if (name && GvSTASH(gv))
4147 stash = GvSTASH(gv);
4149 stash = PL_curstash;
4151 apply_attrs(stash, rcv, attrs, FALSE);
4153 if (cv) { /* must reuse cv if autoloaded */
4155 /* got here with just attrs -- work done, so bug out */
4156 SAVEFREESV(PL_compcv);
4159 /* transfer PL_compcv to cv */
4161 CvFLAGS(cv) = CvFLAGS(PL_compcv);
4162 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
4163 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv);
4164 CvOUTSIDE(PL_compcv) = 0;
4165 CvPADLIST(cv) = CvPADLIST(PL_compcv);
4166 CvPADLIST(PL_compcv) = 0;
4167 /* inner references to PL_compcv must be fixed up ... */
4168 pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv);
4169 /* ... before we throw it away */
4170 SvREFCNT_dec(PL_compcv);
4172 if (PERLDB_INTER)/* Advice debugger on the new sub. */
4173 ++PL_sub_generation;
4180 PL_sub_generation++;
4184 CvFILE_set_from_cop(cv, PL_curcop);
4185 CvSTASH(cv) = PL_curstash;
4188 sv_setpv((SV*)cv, ps);
4190 if (PL_error_count) {
4194 char *s = strrchr(name, ':');
4196 if (strEQ(s, "BEGIN")) {
4198 "BEGIN not safe after errors--compilation aborted";
4199 if (PL_in_eval & EVAL_KEEPERR)
4200 Perl_croak(aTHX_ not_safe);
4202 /* force display of errors found but not reported */
4203 sv_catpv(ERRSV, not_safe);
4204 Perl_croak(aTHX_ "%"SVf, ERRSV);
4213 CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0,
4214 mod(scalarseq(block), OP_LEAVESUBLV));
4217 /* This makes sub {}; work as expected. */
4218 if (block->op_type == OP_STUB) {
4220 block = newSTATEOP(0, Nullch, 0);
4222 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4224 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4225 OpREFCNT_set(CvROOT(cv), 1);
4226 CvSTART(cv) = LINKLIST(CvROOT(cv));
4227 CvROOT(cv)->op_next = 0;
4228 CALL_PEEP(CvSTART(cv));
4230 /* now that optimizer has done its work, adjust pad values */
4232 pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
4235 assert(!CvCONST(cv));
4236 if (ps && !*ps && op_const_sv(block, cv))
4240 if (name || aname) {
4242 char *tname = (name ? name : aname);
4244 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4245 SV *sv = NEWSV(0,0);
4246 SV *tmpstr = sv_newmortal();
4247 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
4251 Perl_sv_setpvf(aTHX_ sv, "%s:%ld-%ld",
4253 (long)PL_subline, (long)CopLINE(PL_curcop));
4254 gv_efullname3(tmpstr, gv, Nullch);
4255 hv_store(GvHV(PL_DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
4256 hv = GvHVn(db_postponed);
4257 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
4258 && (pcv = GvCV(db_postponed)))
4264 call_sv((SV*)pcv, G_DISCARD);
4268 if ((s = strrchr(tname,':')))
4273 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4276 if (strEQ(s, "BEGIN") && !PL_error_count) {
4277 I32 oldscope = PL_scopestack_ix;
4279 SAVECOPFILE(&PL_compiling);
4280 SAVECOPLINE(&PL_compiling);
4283 PL_beginav = newAV();
4284 DEBUG_x( dump_sub(gv) );
4285 av_push(PL_beginav, (SV*)cv);
4286 GvCV(gv) = 0; /* cv has been hijacked */
4287 call_list(oldscope, PL_beginav);
4289 PL_curcop = &PL_compiling;
4290 PL_compiling.op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
4293 else if (strEQ(s, "END") && !PL_error_count) {
4296 DEBUG_x( dump_sub(gv) );
4297 av_unshift(PL_endav, 1);
4298 av_store(PL_endav, 0, (SV*)cv);
4299 GvCV(gv) = 0; /* cv has been hijacked */
4301 else if (strEQ(s, "CHECK") && !PL_error_count) {
4303 PL_checkav = newAV();
4304 DEBUG_x( dump_sub(gv) );
4305 if (PL_main_start && ckWARN(WARN_VOID))
4306 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run CHECK block");
4307 av_unshift(PL_checkav, 1);
4308 av_store(PL_checkav, 0, (SV*)cv);
4309 GvCV(gv) = 0; /* cv has been hijacked */
4311 else if (strEQ(s, "INIT") && !PL_error_count) {
4313 PL_initav = newAV();
4314 DEBUG_x( dump_sub(gv) );
4315 if (PL_main_start && ckWARN(WARN_VOID))
4316 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run INIT block");
4317 av_push(PL_initav, (SV*)cv);
4318 GvCV(gv) = 0; /* cv has been hijacked */
4323 PL_copline = NOLINE;
4328 /* XXX unsafe for threads if eval_owner isn't held */
4330 =for apidoc newCONSTSUB
4332 Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is
4333 eligible for inlining at compile-time.
4339 Perl_newCONSTSUB(pTHX_ HV *stash, char *name, SV *sv)
4345 SAVECOPLINE(PL_curcop);
4346 CopLINE_set(PL_curcop, PL_copline);
4349 PL_hints &= ~HINT_BLOCK_SCOPE;
4352 SAVESPTR(PL_curstash);
4353 SAVECOPSTASH(PL_curcop);
4354 PL_curstash = stash;
4355 CopSTASH_set(PL_curcop,stash);
4358 cv = newXS(name, const_sv_xsub, savepv(CopFILE(PL_curcop)));
4359 CvXSUBANY(cv).any_ptr = sv;
4361 sv_setpv((SV*)cv, ""); /* prototype is "" */
4364 CopSTASH_free(PL_curcop);
4372 =for apidoc U||newXS
4374 Used by C<xsubpp> to hook up XSUBs as Perl subs.
4380 Perl_newXS(pTHX_ char *name, XSUBADDR_t subaddr, char *filename)
4382 GV *gv = gv_fetchpv(name ? name :
4383 (PL_curstash ? "__ANON__" : "__ANON__::__ANON__"),
4384 GV_ADDMULTI, SVt_PVCV);
4388 Perl_croak(aTHX_ "panic: no address for '%s' in '%s'", name, filename);
4390 if ((cv = (name ? GvCV(gv) : Nullcv))) {
4392 /* just a cached method */
4396 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4397 /* already defined (or promised) */
4398 if (ckWARN(WARN_REDEFINE) && !(CvGV(cv) && GvSTASH(CvGV(cv))
4399 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
4400 line_t oldline = CopLINE(PL_curcop);
4401 if (PL_copline != NOLINE)
4402 CopLINE_set(PL_curcop, PL_copline);
4403 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
4404 CvCONST(cv) ? "Constant subroutine %s redefined"
4405 : "Subroutine %s redefined"
4407 CopLINE_set(PL_curcop, oldline);
4414 if (cv) /* must reuse cv if autoloaded */
4417 cv = (CV*)NEWSV(1105,0);
4418 sv_upgrade((SV *)cv, SVt_PVCV);
4422 PL_sub_generation++;
4426 (void)gv_fetchfile(filename);
4427 CvFILE(cv) = filename; /* NOTE: not copied, as it is expected to be
4428 an external constant string */
4429 CvXSUB(cv) = subaddr;
4432 char *s = strrchr(name,':');
4438 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4441 if (strEQ(s, "BEGIN")) {
4443 PL_beginav = newAV();
4444 av_push(PL_beginav, (SV*)cv);
4445 GvCV(gv) = 0; /* cv has been hijacked */
4447 else if (strEQ(s, "END")) {
4450 av_unshift(PL_endav, 1);
4451 av_store(PL_endav, 0, (SV*)cv);
4452 GvCV(gv) = 0; /* cv has been hijacked */
4454 else if (strEQ(s, "CHECK")) {
4456 PL_checkav = newAV();
4457 if (PL_main_start && ckWARN(WARN_VOID))
4458 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run CHECK block");
4459 av_unshift(PL_checkav, 1);
4460 av_store(PL_checkav, 0, (SV*)cv);
4461 GvCV(gv) = 0; /* cv has been hijacked */
4463 else if (strEQ(s, "INIT")) {
4465 PL_initav = newAV();
4466 if (PL_main_start && ckWARN(WARN_VOID))
4467 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run INIT block");
4468 av_push(PL_initav, (SV*)cv);
4469 GvCV(gv) = 0; /* cv has been hijacked */
4480 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
4488 name = SvPVx(cSVOPo->op_sv, n_a);
4491 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
4492 #ifdef GV_UNIQUE_CHECK
4494 Perl_croak(aTHX_ "Bad symbol for form (GV is unique)");
4498 if ((cv = GvFORM(gv))) {
4499 if (ckWARN(WARN_REDEFINE)) {
4500 line_t oldline = CopLINE(PL_curcop);
4501 if (PL_copline != NOLINE)
4502 CopLINE_set(PL_curcop, PL_copline);
4503 Perl_warner(aTHX_ packWARN(WARN_REDEFINE), "Format %s redefined",name);
4504 CopLINE_set(PL_curcop, oldline);
4511 CvFILE_set_from_cop(cv, PL_curcop);
4514 pad_tidy(padtidy_FORMAT);
4515 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4516 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4517 OpREFCNT_set(CvROOT(cv), 1);
4518 CvSTART(cv) = LINKLIST(CvROOT(cv));
4519 CvROOT(cv)->op_next = 0;
4520 CALL_PEEP(CvSTART(cv));
4522 PL_copline = NOLINE;
4527 Perl_newANONLIST(pTHX_ OP *o)
4529 return newUNOP(OP_REFGEN, 0,
4530 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
4534 Perl_newANONHASH(pTHX_ OP *o)
4536 return newUNOP(OP_REFGEN, 0,
4537 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
4541 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
4543 return newANONATTRSUB(floor, proto, Nullop, block);
4547 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
4549 return newUNOP(OP_REFGEN, 0,
4550 newSVOP(OP_ANONCODE, 0,
4551 (SV*)newATTRSUB(floor, 0, proto, attrs, block)));
4555 Perl_oopsAV(pTHX_ OP *o)
4557 switch (o->op_type) {
4559 o->op_type = OP_PADAV;
4560 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4561 return ref(o, OP_RV2AV);
4564 o->op_type = OP_RV2AV;
4565 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4570 if (ckWARN_d(WARN_INTERNAL))
4571 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV");
4578 Perl_oopsHV(pTHX_ OP *o)
4580 switch (o->op_type) {
4583 o->op_type = OP_PADHV;
4584 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4585 return ref(o, OP_RV2HV);
4589 o->op_type = OP_RV2HV;
4590 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
4595 if (ckWARN_d(WARN_INTERNAL))
4596 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV");
4603 Perl_newAVREF(pTHX_ OP *o)
4605 if (o->op_type == OP_PADANY) {
4606 o->op_type = OP_PADAV;
4607 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4610 else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)
4611 && ckWARN(WARN_DEPRECATED)) {
4612 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
4613 "Using an array as a reference is deprecated");
4615 return newUNOP(OP_RV2AV, 0, scalar(o));
4619 Perl_newGVREF(pTHX_ I32 type, OP *o)
4621 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
4622 return newUNOP(OP_NULL, 0, o);
4623 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
4627 Perl_newHVREF(pTHX_ OP *o)
4629 if (o->op_type == OP_PADANY) {
4630 o->op_type = OP_PADHV;
4631 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4634 else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)
4635 && ckWARN(WARN_DEPRECATED)) {
4636 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
4637 "Using a hash as a reference is deprecated");
4639 return newUNOP(OP_RV2HV, 0, scalar(o));
4643 Perl_oopsCV(pTHX_ OP *o)
4645 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
4651 Perl_newCVREF(pTHX_ I32 flags, OP *o)
4653 return newUNOP(OP_RV2CV, flags, scalar(o));
4657 Perl_newSVREF(pTHX_ OP *o)
4659 if (o->op_type == OP_PADANY) {
4660 o->op_type = OP_PADSV;
4661 o->op_ppaddr = PL_ppaddr[OP_PADSV];
4664 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
4665 o->op_flags |= OPpDONE_SVREF;
4668 return newUNOP(OP_RV2SV, 0, scalar(o));
4671 /* Check routines. */
4674 Perl_ck_anoncode(pTHX_ OP *o)
4676 cSVOPo->op_targ = pad_add_anon(cSVOPo->op_sv, o->op_type);
4677 cSVOPo->op_sv = Nullsv;
4682 Perl_ck_bitop(pTHX_ OP *o)
4684 #define OP_IS_NUMCOMPARE(op) \
4685 ((op) == OP_LT || (op) == OP_I_LT || \
4686 (op) == OP_GT || (op) == OP_I_GT || \
4687 (op) == OP_LE || (op) == OP_I_LE || \
4688 (op) == OP_GE || (op) == OP_I_GE || \
4689 (op) == OP_EQ || (op) == OP_I_EQ || \
4690 (op) == OP_NE || (op) == OP_I_NE || \
4691 (op) == OP_NCMP || (op) == OP_I_NCMP)
4692 o->op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
4693 if (!(o->op_flags & OPf_STACKED) /* Not an assignment */
4694 && (o->op_type == OP_BIT_OR
4695 || o->op_type == OP_BIT_AND
4696 || o->op_type == OP_BIT_XOR))
4698 OP * left = cBINOPo->op_first;
4699 OP * right = left->op_sibling;
4700 if ((OP_IS_NUMCOMPARE(left->op_type) &&
4701 (left->op_flags & OPf_PARENS) == 0) ||
4702 (OP_IS_NUMCOMPARE(right->op_type) &&
4703 (right->op_flags & OPf_PARENS) == 0))
4704 if (ckWARN(WARN_PRECEDENCE))
4705 Perl_warner(aTHX_ packWARN(WARN_PRECEDENCE),
4706 "Possible precedence problem on bitwise %c operator",
4707 o->op_type == OP_BIT_OR ? '|'
4708 : o->op_type == OP_BIT_AND ? '&' : '^'
4715 Perl_ck_concat(pTHX_ OP *o)
4717 OP *kid = cUNOPo->op_first;
4718 if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) &&
4719 !(kUNOP->op_first->op_flags & OPf_MOD))
4720 o->op_flags |= OPf_STACKED;
4725 Perl_ck_spair(pTHX_ OP *o)
4727 if (o->op_flags & OPf_KIDS) {
4730 OPCODE type = o->op_type;
4731 o = modkids(ck_fun(o), type);
4732 kid = cUNOPo->op_first;
4733 newop = kUNOP->op_first->op_sibling;
4735 (newop->op_sibling ||
4736 !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
4737 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
4738 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
4742 op_free(kUNOP->op_first);
4743 kUNOP->op_first = newop;
4745 o->op_ppaddr = PL_ppaddr[++o->op_type];
4750 Perl_ck_delete(pTHX_ OP *o)
4754 if (o->op_flags & OPf_KIDS) {
4755 OP *kid = cUNOPo->op_first;
4756 switch (kid->op_type) {
4758 o->op_flags |= OPf_SPECIAL;
4761 o->op_private |= OPpSLICE;
4764 o->op_flags |= OPf_SPECIAL;
4769 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element or slice",
4778 Perl_ck_die(pTHX_ OP *o)
4781 if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH;
4787 Perl_ck_eof(pTHX_ OP *o)
4789 I32 type = o->op_type;
4791 if (o->op_flags & OPf_KIDS) {
4792 if (cLISTOPo->op_first->op_type == OP_STUB) {
4794 o = newUNOP(type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv));
4802 Perl_ck_eval(pTHX_ OP *o)
4804 PL_hints |= HINT_BLOCK_SCOPE;
4805 if (o->op_flags & OPf_KIDS) {
4806 SVOP *kid = (SVOP*)cUNOPo->op_first;
4809 o->op_flags &= ~OPf_KIDS;
4812 else if (kid->op_type == OP_LINESEQ || kid->op_type == OP_STUB) {
4815 cUNOPo->op_first = 0;
4818 NewOp(1101, enter, 1, LOGOP);
4819 enter->op_type = OP_ENTERTRY;
4820 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
4821 enter->op_private = 0;
4823 /* establish postfix order */
4824 enter->op_next = (OP*)enter;
4826 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
4827 o->op_type = OP_LEAVETRY;
4828 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
4829 enter->op_other = o;
4839 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
4841 o->op_targ = (PADOFFSET)PL_hints;
4846 Perl_ck_exit(pTHX_ OP *o)