ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/staticperl/perl/op.c
Revision: 1.1
Committed: Thu Jun 30 14:26:42 2005 UTC (21 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: PERL-5-8-7, HEAD
Branch point for: PERL
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /* op.c
2     *
3     * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4     * 2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others
5     *
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.
8     *
9     */
10    
11     /*
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
17     */
18    
19     /* This file contains the functions that create, manipulate and optimize
20     * the OP structures that hold a compiled perl program.
21     *
22     * A Perl program is compiled into a tree of OPs. Each op contains
23     * structural pointers (eg to its siblings and the next op in the
24     * execution sequence), a pointer to the function that would execute the
25     * op, plus any data specific to that op. For example, an OP_CONST op
26     * points to the pp_const() function and to an SV containing the constant
27     * value. When pp_const() is executed, its job is to push that SV onto the
28     * stack.
29     *
30     * OPs are mainly created by the newFOO() functions, which are mainly
31     * called from the parser (in perly.y) as the code is parsed. For example
32     * the Perl code $a + $b * $c would cause the equivalent of the following
33     * to be called (oversimplifying a bit):
34     *
35     * newBINOP(OP_ADD, flags,
36     * newSVREF($a),
37     * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
38     * )
39     *
40     * Note that during the build of miniperl, a temporary copy of this file
41     * is made, called opmini.c.
42     */
43    
44     /*
45     Perl's compiler is essentially a 3-pass compiler with interleaved phases:
46    
47     A bottom-up pass
48     A top-down pass
49     An execution-order pass
50    
51     The bottom-up pass is represented by all the "newOP" routines and
52     the ck_ routines. The bottom-upness is actually driven by yacc.
53     So at the point that a ck_ routine fires, we have no idea what the
54     context is, either upward in the syntax tree, or either forward or
55     backward in the execution order. (The bottom-up parser builds that
56     part of the execution order it knows about, but if you follow the "next"
57     links around, you'll find it's actually a closed loop through the
58     top level node.
59    
60     Whenever the bottom-up parser gets to a node that supplies context to
61     its components, it invokes that portion of the top-down pass that applies
62     to that part of the subtree (and marks the top node as processed, so
63     if a node further up supplies context, it doesn't have to take the
64     plunge again). As a particular subcase of this, as the new node is
65     built, it takes all the closed execution loops of its subcomponents
66     and links them into a new closed loop for the higher level node. But
67     it's still not the real execution order.
68    
69     The actual execution order is not known till we get a grammar reduction
70     to a top-level unit like a subroutine or file that will be called by
71     "name" rather than via a "next" pointer. At that point, we can call
72     into peep() to do that code's portion of the 3rd pass. It has to be
73     recursive, but it's recursive on basic blocks, not on tree nodes.
74     */
75    
76     #include "EXTERN.h"
77     #define PERL_IN_OP_C
78     #include "perl.h"
79     #include "keywords.h"
80    
81     #define CALL_PEEP(o) CALL_FPTR(PL_peepp)(aTHX_ o)
82    
83     #if defined(PL_OP_SLAB_ALLOC)
84    
85     #ifndef PERL_SLAB_SIZE
86     #define PERL_SLAB_SIZE 2048
87     #endif
88    
89     void *
90     Perl_Slab_Alloc(pTHX_ int m, size_t sz)
91     {
92     /*
93     * To make incrementing use count easy PL_OpSlab is an I32 *
94     * To make inserting the link to slab PL_OpPtr is I32 **
95     * So compute size in units of sizeof(I32 *) as that is how Pl_OpPtr increments
96     * Add an overhead for pointer to slab and round up as a number of pointers
97     */
98     sz = (sz + 2*sizeof(I32 *) -1)/sizeof(I32 *);
99     if ((PL_OpSpace -= sz) < 0) {
100     PL_OpPtr = (I32 **) PerlMemShared_malloc(PERL_SLAB_SIZE*sizeof(I32*));
101     if (!PL_OpPtr) {
102     return NULL;
103     }
104     Zero(PL_OpPtr,PERL_SLAB_SIZE,I32 **);
105     /* We reserve the 0'th I32 sized chunk as a use count */
106     PL_OpSlab = (I32 *) PL_OpPtr;
107     /* Reduce size by the use count word, and by the size we need.
108     * Latter is to mimic the '-=' in the if() above
109     */
110     PL_OpSpace = PERL_SLAB_SIZE - (sizeof(I32)+sizeof(I32 **)-1)/sizeof(I32 **) - sz;
111     /* Allocation pointer starts at the top.
112     Theory: because we build leaves before trunk allocating at end
113     means that at run time access is cache friendly upward
114     */
115     PL_OpPtr += PERL_SLAB_SIZE;
116     }
117     assert( PL_OpSpace >= 0 );
118     /* Move the allocation pointer down */
119     PL_OpPtr -= sz;
120     assert( PL_OpPtr > (I32 **) PL_OpSlab );
121     *PL_OpPtr = PL_OpSlab; /* Note which slab it belongs to */
122     (*PL_OpSlab)++; /* Increment use count of slab */
123     assert( PL_OpPtr+sz <= ((I32 **) PL_OpSlab + PERL_SLAB_SIZE) );
124     assert( *PL_OpSlab > 0 );
125     return (void *)(PL_OpPtr + 1);
126     }
127    
128     void
129     Perl_Slab_Free(pTHX_ void *op)
130     {
131     I32 **ptr = (I32 **) op;
132     I32 *slab = ptr[-1];
133     assert( ptr-1 > (I32 **) slab );
134     assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) );
135     assert( *slab > 0 );
136     if (--(*slab) == 0) {
137     # ifdef NETWARE
138     # define PerlMemShared PerlMem
139     # endif
140    
141     PerlMemShared_free(slab);
142     if (slab == PL_OpSlab) {
143     PL_OpSpace = 0;
144     }
145     }
146     }
147     #endif
148     /*
149     * In the following definition, the ", Nullop" is just to make the compiler
150     * think the expression is of the right type: croak actually does a Siglongjmp.
151     */
152     #define CHECKOP(type,o) \
153     ((PL_op_mask && PL_op_mask[type]) \
154     ? ( op_free((OP*)o), \
155     Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
156     Nullop ) \
157     : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o))
158    
159     #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
160    
161     STATIC char*
162     S_gv_ename(pTHX_ GV *gv)
163     {
164     STRLEN n_a;
165     SV* tmpsv = sv_newmortal();
166     gv_efullname3(tmpsv, gv, Nullch);
167     return SvPV(tmpsv,n_a);
168     }
169    
170     STATIC OP *
171     S_no_fh_allowed(pTHX_ OP *o)
172     {
173     yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
174     OP_DESC(o)));
175     return o;
176     }
177    
178     STATIC OP *
179     S_too_few_arguments(pTHX_ OP *o, char *name)
180     {
181     yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
182     return o;
183     }
184    
185     STATIC OP *
186     S_too_many_arguments(pTHX_ OP *o, char *name)
187     {
188     yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
189     return o;
190     }
191    
192     STATIC void
193     S_bad_type(pTHX_ I32 n, char *t, char *name, OP *kid)
194     {
195     yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
196     (int)n, name, t, OP_DESC(kid)));
197     }
198    
199     STATIC void
200     S_no_bareword_allowed(pTHX_ OP *o)
201     {
202     qerror(Perl_mess(aTHX_
203     "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
204     cSVOPo_sv));
205     }
206    
207     /* "register" allocation */
208    
209     PADOFFSET
210     Perl_allocmy(pTHX_ char *name)
211     {
212     PADOFFSET off;
213    
214     /* complain about "my $_" etc etc */
215     if (!(PL_in_my == KEY_our ||
216     isALPHA(name[1]) ||
217     (USE_UTF8_IN_NAMES && UTF8_IS_START(name[1])) ||
218     (name[1] == '_' && (int)strlen(name) > 2)))
219     {
220     if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
221     /* 1999-02-27 mjd@plover.com */
222     char *p;
223     p = strchr(name, '\0');
224     /* The next block assumes the buffer is at least 205 chars
225     long. At present, it's always at least 256 chars. */
226     if (p-name > 200) {
227     strcpy(name+200, "...");
228     p = name+199;
229     }
230     else {
231     p[1] = '\0';
232     }
233     /* Move everything else down one character */
234     for (; p-name > 2; p--)
235     *p = *(p-1);
236     name[2] = toCTRL(name[1]);
237     name[1] = '^';
238     }
239     yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
240     }
241     /* check for duplicate declaration */
242     pad_check_dup(name,
243     (bool)(PL_in_my == KEY_our),
244     (PL_curstash ? PL_curstash : PL_defstash)
245     );
246    
247     if (PL_in_my_stash && *name != '$') {
248     yyerror(Perl_form(aTHX_
249     "Can't declare class for non-scalar %s in \"%s\"",
250     name, PL_in_my == KEY_our ? "our" : "my"));
251     }
252    
253     /* allocate a spare slot and store the name in that slot */
254    
255     off = pad_add_name(name,
256     PL_in_my_stash,
257     (PL_in_my == KEY_our
258     ? (PL_curstash ? PL_curstash : PL_defstash)
259     : Nullhv
260     ),
261     0 /* not fake */
262     );
263     return off;
264     }
265    
266    
267     #ifdef USE_5005THREADS
268     /* find_threadsv is not reentrant */
269     PADOFFSET
270     Perl_find_threadsv(pTHX_ const char *name)
271     {
272     char *p;
273     PADOFFSET key;
274     SV **svp;
275     /* We currently only handle names of a single character */
276     p = strchr(PL_threadsv_names, *name);
277     if (!p)
278     return NOT_IN_PAD;
279     key = p - PL_threadsv_names;
280     MUTEX_LOCK(&thr->mutex);
281     svp = av_fetch(thr->threadsv, key, FALSE);
282     if (svp)
283     MUTEX_UNLOCK(&thr->mutex);
284     else {
285     SV *sv = NEWSV(0, 0);
286     av_store(thr->threadsv, key, sv);
287     thr->threadsvp = AvARRAY(thr->threadsv);
288     MUTEX_UNLOCK(&thr->mutex);
289     /*
290     * Some magic variables used to be automagically initialised
291     * in gv_fetchpv. Those which are now per-thread magicals get
292     * initialised here instead.
293     */
294     switch (*name) {
295     case '_':
296     break;
297     case ';':
298     sv_setpv(sv, "\034");
299     sv_magic(sv, 0, PERL_MAGIC_sv, name, 1);
300     break;
301     case '&':
302     case '`':
303     case '\'':
304     PL_sawampersand = TRUE;
305     /* FALL THROUGH */
306     case '1':
307     case '2':
308     case '3':
309     case '4':
310     case '5':
311     case '6':
312     case '7':
313     case '8':
314     case '9':
315     SvREADONLY_on(sv);
316     /* FALL THROUGH */
317    
318     /* XXX %! tied to Errno.pm needs to be added here.
319     * See gv_fetchpv(). */
320     /* case '!': */
321    
322     default:
323     sv_magic(sv, 0, PERL_MAGIC_sv, name, 1);
324     }
325     DEBUG_S(PerlIO_printf(Perl_error_log,
326     "find_threadsv: new SV %p for $%s%c\n",
327     sv, (*name < 32) ? "^" : "",
328     (*name < 32) ? toCTRL(*name) : *name));
329     }
330     return key;
331     }
332     #endif /* USE_5005THREADS */
333    
334     /* Destructor */
335    
336     void
337     Perl_op_free(pTHX_ OP *o)
338     {
339     register OP *kid, *nextkid;
340     OPCODE type;
341     PADOFFSET refcnt;
342    
343     if (!o || o->op_seq == (U16)-1)
344     return;
345    
346     if (o->op_private & OPpREFCOUNTED) {
347     switch (o->op_type) {
348     case OP_LEAVESUB:
349     case OP_LEAVESUBLV:
350     case OP_LEAVEEVAL:
351     case OP_LEAVE:
352     case OP_SCOPE:
353     case OP_LEAVEWRITE:
354     OP_REFCNT_LOCK;
355     refcnt = OpREFCNT_dec(o);
356     OP_REFCNT_UNLOCK;
357     if (refcnt)
358     return;
359     break;
360     default:
361     break;
362     }
363     }
364    
365     if (o->op_flags & OPf_KIDS) {
366     for (kid = cUNOPo->op_first; kid; kid = nextkid) {
367     nextkid = kid->op_sibling; /* Get before next freeing kid */
368     op_free(kid);
369     }
370     }
371     type = o->op_type;
372     if (type == OP_NULL)
373     type = (OPCODE)o->op_targ;
374    
375     /* COP* is not cleared by op_clear() so that we may track line
376     * numbers etc even after null() */
377     if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
378     cop_free((COP*)o);
379    
380     op_clear(o);
381     FreeOp(o);
382     }
383    
384     void
385     Perl_op_clear(pTHX_ OP *o)
386     {
387    
388     switch (o->op_type) {
389     case OP_NULL: /* Was holding old type, if any. */
390     case OP_ENTEREVAL: /* Was holding hints. */
391     #ifdef USE_5005THREADS
392     case OP_THREADSV: /* Was holding index into thr->threadsv AV. */
393     #endif
394     o->op_targ = 0;
395     break;
396     #ifdef USE_5005THREADS
397     case OP_ENTERITER:
398     if (!(o->op_flags & OPf_SPECIAL))
399     break;
400     /* FALL THROUGH */
401     #endif /* USE_5005THREADS */
402     default:
403     if (!(o->op_flags & OPf_REF)
404     || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
405     break;
406     /* FALL THROUGH */
407     case OP_GVSV:
408     case OP_GV:
409     case OP_AELEMFAST:
410     if (! (o->op_type == OP_AELEMFAST && o->op_flags & OPf_SPECIAL)) {
411     /* not an OP_PADAV replacement */
412     #ifdef USE_ITHREADS
413     if (cPADOPo->op_padix > 0) {
414     /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
415     * may still exist on the pad */
416     pad_swipe(cPADOPo->op_padix, TRUE);
417     cPADOPo->op_padix = 0;
418     }
419     #else
420     SvREFCNT_dec(cSVOPo->op_sv);
421     cSVOPo->op_sv = Nullsv;
422     #endif
423     }
424     break;
425     case OP_METHOD_NAMED:
426     case OP_CONST:
427     SvREFCNT_dec(cSVOPo->op_sv);
428     cSVOPo->op_sv = Nullsv;
429     #ifdef USE_ITHREADS
430     /** Bug #15654
431     Even if op_clear does a pad_free for the target of the op,
432     pad_free doesn't actually remove the sv that exists in the pad;
433     instead it lives on. This results in that it could be reused as
434     a target later on when the pad was reallocated.
435     **/
436     if(o->op_targ) {
437     pad_swipe(o->op_targ,1);
438     o->op_targ = 0;
439     }
440     #endif
441     break;
442     case OP_GOTO:
443     case OP_NEXT:
444     case OP_LAST:
445     case OP_REDO:
446     if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
447     break;
448     /* FALL THROUGH */
449     case OP_TRANS:
450     if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
451     SvREFCNT_dec(cSVOPo->op_sv);
452     cSVOPo->op_sv = Nullsv;
453     }
454     else {
455     Safefree(cPVOPo->op_pv);
456     cPVOPo->op_pv = Nullch;
457     }
458     break;
459     case OP_SUBST:
460     op_free(cPMOPo->op_pmreplroot);
461     goto clear_pmop;
462     case OP_PUSHRE:
463     #ifdef USE_ITHREADS
464     if (INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot)) {
465     /* No GvIN_PAD_off here, because other references may still
466     * exist on the pad */
467     pad_swipe(INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot), TRUE);
468     }
469     #else
470     SvREFCNT_dec((SV*)cPMOPo->op_pmreplroot);
471     #endif
472     /* FALL THROUGH */
473     case OP_MATCH:
474     case OP_QR:
475     clear_pmop:
476     {
477     HV *pmstash = PmopSTASH(cPMOPo);
478     if (pmstash && SvREFCNT(pmstash)) {
479     PMOP *pmop = HvPMROOT(pmstash);
480     PMOP *lastpmop = NULL;
481     while (pmop) {
482     if (cPMOPo == pmop) {
483     if (lastpmop)
484     lastpmop->op_pmnext = pmop->op_pmnext;
485     else
486     HvPMROOT(pmstash) = pmop->op_pmnext;
487     break;
488     }
489     lastpmop = pmop;
490     pmop = pmop->op_pmnext;
491     }
492     }
493     PmopSTASH_free(cPMOPo);
494     }
495     cPMOPo->op_pmreplroot = Nullop;
496     /* we use the "SAFE" version of the PM_ macros here
497     * since sv_clean_all might release some PMOPs
498     * after PL_regex_padav has been cleared
499     * and the clearing of PL_regex_padav needs to
500     * happen before sv_clean_all
501     */
502     ReREFCNT_dec(PM_GETRE_SAFE(cPMOPo));
503     PM_SETRE_SAFE(cPMOPo, (REGEXP*)NULL);
504     #ifdef USE_ITHREADS
505     if(PL_regex_pad) { /* We could be in destruction */
506     av_push((AV*) PL_regex_pad[0],(SV*) PL_regex_pad[(cPMOPo)->op_pmoffset]);
507     SvREPADTMP_on(PL_regex_pad[(cPMOPo)->op_pmoffset]);
508     PM_SETRE(cPMOPo, (cPMOPo)->op_pmoffset);
509     }
510     #endif
511    
512     break;
513     }
514    
515     if (o->op_targ > 0) {
516     pad_free(o->op_targ);
517     o->op_targ = 0;
518     }
519     }
520    
521     STATIC void
522     S_cop_free(pTHX_ COP* cop)
523     {
524     Safefree(cop->cop_label); /* FIXME: treaddead ??? */
525     CopFILE_free(cop);
526     CopSTASH_free(cop);
527     if (! specialWARN(cop->cop_warnings))
528     SvREFCNT_dec(cop->cop_warnings);
529     if (! specialCopIO(cop->cop_io)) {
530     #ifdef USE_ITHREADS
531     #if 0
532     STRLEN len;
533     char *s = SvPV(cop->cop_io,len);
534     Perl_warn(aTHX_ "io='%.*s'",(int) len,s); /* ??? --jhi */
535     #endif
536     #else
537     SvREFCNT_dec(cop->cop_io);
538     #endif
539     }
540     }
541    
542     void
543     Perl_op_null(pTHX_ OP *o)
544     {
545     if (o->op_type == OP_NULL)
546     return;
547     op_clear(o);
548     o->op_targ = o->op_type;
549     o->op_type = OP_NULL;
550     o->op_ppaddr = PL_ppaddr[OP_NULL];
551     }
552    
553     void
554     Perl_op_refcnt_lock(pTHX)
555     {
556     OP_REFCNT_LOCK;
557     }
558    
559     void
560     Perl_op_refcnt_unlock(pTHX)
561     {
562     OP_REFCNT_UNLOCK;
563     }
564    
565     /* Contextualizers */
566    
567     #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
568    
569     OP *
570     Perl_linklist(pTHX_ OP *o)
571     {
572     register OP *kid;
573    
574     if (o->op_next)
575     return o->op_next;
576    
577     /* establish postfix order */
578     if (cUNOPo->op_first) {
579     o->op_next = LINKLIST(cUNOPo->op_first);
580     for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
581     if (kid->op_sibling)
582     kid->op_next = LINKLIST(kid->op_sibling);
583     else
584     kid->op_next = o;
585     }
586     }
587     else
588     o->op_next = o;
589    
590     return o->op_next;
591     }
592    
593     OP *
594     Perl_scalarkids(pTHX_ OP *o)
595     {
596     OP *kid;
597     if (o && o->op_flags & OPf_KIDS) {
598     for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
599     scalar(kid);
600     }
601     return o;
602     }
603    
604     STATIC OP *
605     S_scalarboolean(pTHX_ OP *o)
606     {
607     if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
608     if (ckWARN(WARN_SYNTAX)) {
609     line_t oldline = CopLINE(PL_curcop);
610    
611     if (PL_copline != NOLINE)
612     CopLINE_set(PL_curcop, PL_copline);
613     Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
614     CopLINE_set(PL_curcop, oldline);
615     }
616     }
617     return scalar(o);
618     }
619    
620     OP *
621     Perl_scalar(pTHX_ OP *o)
622     {
623     OP *kid;
624    
625     /* assumes no premature commitment */
626     if (!o || (o->op_flags & OPf_WANT) || PL_error_count
627     || o->op_type == OP_RETURN)
628     {
629     return o;
630     }
631    
632     o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
633    
634     switch (o->op_type) {
635     case OP_REPEAT:
636     scalar(cBINOPo->op_first);
637     break;
638     case OP_OR:
639     case OP_AND:
640     case OP_COND_EXPR:
641     for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
642     scalar(kid);
643     break;
644     case OP_SPLIT:
645     if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
646     if (!kPMOP->op_pmreplroot)
647     deprecate_old("implicit split to @_");
648     }
649     /* FALL THROUGH */
650     case OP_MATCH:
651     case OP_QR:
652     case OP_SUBST:
653     case OP_NULL:
654     default:
655     if (o->op_flags & OPf_KIDS) {
656     for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
657     scalar(kid);
658     }
659     break;
660     case OP_LEAVE:
661     case OP_LEAVETRY:
662     kid = cLISTOPo->op_first;
663     scalar(kid);
664     while ((kid = kid->op_sibling)) {
665     if (kid->op_sibling)
666     scalarvoid(kid);
667     else
668     scalar(kid);
669     }
670     WITH_THR(PL_curcop = &PL_compiling);
671     break;
672     case OP_SCOPE:
673     case OP_LINESEQ:
674     case OP_LIST:
675     for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
676     if (kid->op_sibling)
677     scalarvoid(kid);
678     else
679     scalar(kid);
680     }
681     WITH_THR(PL_curcop = &PL_compiling);
682     break;
683     case OP_SORT:
684     if (ckWARN(WARN_VOID))
685     Perl_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
686     }
687     return o;
688     }
689    
690     OP *
691     Perl_scalarvoid(pTHX_ OP *o)
692     {
693     OP *kid;
694     char* useless = 0;
695     SV* sv;
696     U8 want;
697    
698     if (o->op_type == OP_NEXTSTATE
699     || o->op_type == OP_SETSTATE
700     || o->op_type == OP_DBSTATE
701     || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
702     || o->op_targ == OP_SETSTATE
703     || o->op_targ == OP_DBSTATE)))
704     PL_curcop = (COP*)o; /* for warning below */
705    
706     /* assumes no premature commitment */
707     want = o->op_flags & OPf_WANT;
708     if ((want && want != OPf_WANT_SCALAR) || PL_error_count
709     || o->op_type == OP_RETURN)
710     {
711     return o;
712     }
713    
714     if ((o->op_private & OPpTARGET_MY)
715     && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
716     {
717     return scalar(o); /* As if inside SASSIGN */
718     }
719    
720     o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
721    
722     switch (o->op_type) {
723     default:
724     if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
725     break;
726     /* FALL THROUGH */
727     case OP_REPEAT:
728     if (o->op_flags & OPf_STACKED)
729     break;
730     goto func_ops;
731     case OP_SUBSTR:
732     if (o->op_private == 4)
733     break;
734     /* FALL THROUGH */
735     case OP_GVSV:
736     case OP_WANTARRAY:
737     case OP_GV:
738     case OP_PADSV:
739     case OP_PADAV:
740     case OP_PADHV:
741     case OP_PADANY:
742     case OP_AV2ARYLEN:
743     case OP_REF:
744     case OP_REFGEN:
745     case OP_SREFGEN:
746     case OP_DEFINED:
747     case OP_HEX:
748     case OP_OCT:
749     case OP_LENGTH:
750     case OP_VEC:
751     case OP_INDEX:
752     case OP_RINDEX:
753     case OP_SPRINTF:
754     case OP_AELEM:
755     case OP_AELEMFAST:
756     case OP_ASLICE:
757     case OP_HELEM:
758     case OP_HSLICE:
759     case OP_UNPACK:
760     case OP_PACK:
761     case OP_JOIN:
762     case OP_LSLICE:
763     case OP_ANONLIST:
764     case OP_ANONHASH:
765     case OP_SORT:
766     case OP_REVERSE:
767     case OP_RANGE:
768     case OP_FLIP:
769     case OP_FLOP:
770     case OP_CALLER:
771     case OP_FILENO:
772     case OP_EOF:
773     case OP_TELL:
774     case OP_GETSOCKNAME:
775     case OP_GETPEERNAME:
776     case OP_READLINK:
777     case OP_TELLDIR:
778     case OP_GETPPID:
779     case OP_GETPGRP:
780     case OP_GETPRIORITY:
781     case OP_TIME:
782     case OP_TMS:
783     case OP_LOCALTIME:
784     case OP_GMTIME:
785     case OP_GHBYNAME:
786     case OP_GHBYADDR:
787     case OP_GHOSTENT:
788     case OP_GNBYNAME:
789     case OP_GNBYADDR:
790     case OP_GNETENT:
791     case OP_GPBYNAME:
792     case OP_GPBYNUMBER:
793     case OP_GPROTOENT:
794     case OP_GSBYNAME:
795     case OP_GSBYPORT:
796     case OP_GSERVENT:
797     case OP_GPWNAM:
798     case OP_GPWUID:
799     case OP_GGRNAM:
800     case OP_GGRGID:
801     case OP_GETLOGIN:
802     case OP_PROTOTYPE:
803     func_ops:
804     if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
805     useless = OP_DESC(o);
806     break;
807    
808     case OP_RV2GV:
809     case OP_RV2SV:
810     case OP_RV2AV:
811     case OP_RV2HV:
812     if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
813     (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
814     useless = "a variable";
815     break;
816    
817     case OP_CONST:
818     sv = cSVOPo_sv;
819     if (cSVOPo->op_private & OPpCONST_STRICT)
820     no_bareword_allowed(o);
821     else {
822     if (ckWARN(WARN_VOID)) {
823     useless = "a constant";
824     /* don't warn on optimised away booleans, eg
825     * use constant Foo, 5; Foo || print; */
826     if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
827     useless = 0;
828     /* the constants 0 and 1 are permitted as they are
829     conventionally used as dummies in constructs like
830     1 while some_condition_with_side_effects; */
831     else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
832     useless = 0;
833     else if (SvPOK(sv)) {
834     /* perl4's way of mixing documentation and code
835     (before the invention of POD) was based on a
836     trick to mix nroff and perl code. The trick was
837     built upon these three nroff macros being used in
838     void context. The pink camel has the details in
839     the script wrapman near page 319. */
840     if (strnEQ(SvPVX(sv), "di", 2) ||
841     strnEQ(SvPVX(sv), "ds", 2) ||
842     strnEQ(SvPVX(sv), "ig", 2))
843     useless = 0;
844     }
845     }
846     }
847     op_null(o); /* don't execute or even remember it */
848     break;
849    
850     case OP_POSTINC:
851     o->op_type = OP_PREINC; /* pre-increment is faster */
852     o->op_ppaddr = PL_ppaddr[OP_PREINC];
853     break;
854    
855     case OP_POSTDEC:
856     o->op_type = OP_PREDEC; /* pre-decrement is faster */
857     o->op_ppaddr = PL_ppaddr[OP_PREDEC];
858     break;
859    
860     case OP_OR:
861     case OP_AND:
862     case OP_COND_EXPR:
863     for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
864     scalarvoid(kid);
865     break;
866    
867     case OP_NULL:
868     if (o->op_flags & OPf_STACKED)
869     break;
870     /* FALL THROUGH */
871     case OP_NEXTSTATE:
872     case OP_DBSTATE:
873     case OP_ENTERTRY:
874     case OP_ENTER:
875     if (!(o->op_flags & OPf_KIDS))
876     break;
877     /* FALL THROUGH */
878     case OP_SCOPE:
879     case OP_LEAVE:
880     case OP_LEAVETRY:
881     case OP_LEAVELOOP:
882     case OP_LINESEQ:
883     case OP_LIST:
884     for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
885     scalarvoid(kid);
886     break;
887     case OP_ENTEREVAL:
888     scalarkids(o);
889     break;
890     case OP_REQUIRE:
891     /* all requires must return a boolean value */
892     o->op_flags &= ~OPf_WANT;
893     /* FALL THROUGH */
894     case OP_SCALAR:
895     return scalar(o);
896     case OP_SPLIT:
897     if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
898     if (!kPMOP->op_pmreplroot)
899     deprecate_old("implicit split to @_");
900     }
901     break;
902     }
903     if (useless && ckWARN(WARN_VOID))
904     Perl_warner(aTHX_ packWARN(WARN_VOID), "Useless use of %s in void context", useless);
905     return o;
906     }
907    
908     OP *
909     Perl_listkids(pTHX_ OP *o)
910     {
911     OP *kid;
912     if (o && o->op_flags & OPf_KIDS) {
913     for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
914     list(kid);
915     }
916     return o;
917     }
918    
919     OP *
920     Perl_list(pTHX_ OP *o)
921     {
922     OP *kid;
923    
924     /* assumes no premature commitment */
925     if (!o || (o->op_flags & OPf_WANT) || PL_error_count
926     || o->op_type == OP_RETURN)
927     {
928     return o;
929     }
930    
931     if ((o->op_private & OPpTARGET_MY)
932     && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
933     {
934     return o; /* As if inside SASSIGN */
935     }
936    
937     o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
938    
939     switch (o->op_type) {
940     case OP_FLOP:
941     case OP_REPEAT:
942     list(cBINOPo->op_first);
943     break;
944     case OP_OR:
945     case OP_AND:
946     case OP_COND_EXPR:
947     for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
948     list(kid);
949     break;
950     default:
951     case OP_MATCH:
952     case OP_QR:
953     case OP_SUBST:
954     case OP_NULL:
955     if (!(o->op_flags & OPf_KIDS))
956     break;
957     if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
958     list(cBINOPo->op_first);
959     return gen_constant_list(o);
960     }
961     case OP_LIST:
962     listkids(o);
963     break;
964     case OP_LEAVE:
965     case OP_LEAVETRY:
966     kid = cLISTOPo->op_first;
967     list(kid);
968     while ((kid = kid->op_sibling)) {
969     if (kid->op_sibling)
970     scalarvoid(kid);
971     else
972     list(kid);
973     }
974     WITH_THR(PL_curcop = &PL_compiling);
975     break;
976     case OP_SCOPE:
977     case OP_LINESEQ:
978     for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
979     if (kid->op_sibling)
980     scalarvoid(kid);
981     else
982     list(kid);
983     }
984     WITH_THR(PL_curcop = &PL_compiling);
985     break;
986     case OP_REQUIRE:
987     /* all requires must return a boolean value */
988     o->op_flags &= ~OPf_WANT;
989     return scalar(o);
990     }
991     return o;
992     }
993    
994     OP *
995     Perl_scalarseq(pTHX_ OP *o)
996     {
997     OP *kid;
998    
999     if (o) {
1000     if (o->op_type == OP_LINESEQ ||
1001     o->op_type == OP_SCOPE ||
1002     o->op_type == OP_LEAVE ||
1003     o->op_type == OP_LEAVETRY)
1004     {
1005     for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1006     if (kid->op_sibling) {
1007     scalarvoid(kid);
1008     }
1009     }
1010     PL_curcop = &PL_compiling;
1011     }
1012     o->op_flags &= ~OPf_PARENS;
1013     if (PL_hints & HINT_BLOCK_SCOPE)
1014     o->op_flags |= OPf_PARENS;
1015     }
1016     else
1017     o = newOP(OP_STUB, 0);
1018     return o;
1019     }
1020    
1021     STATIC OP *
1022     S_modkids(pTHX_ OP *o, I32 type)
1023     {
1024     OP *kid;
1025     if (o && o->op_flags & OPf_KIDS) {
1026     for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1027     mod(kid, type);
1028     }
1029     return o;
1030     }
1031    
1032     OP *
1033     Perl_mod(pTHX_ OP *o, I32 type)
1034     {
1035     OP *kid;
1036    
1037     if (!o || PL_error_count)
1038     return o;
1039    
1040     if ((o->op_private & OPpTARGET_MY)
1041     && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1042     {
1043     return o;
1044     }
1045    
1046     switch (o->op_type) {
1047     case OP_UNDEF:
1048     PL_modcount++;
1049     return o;
1050     case OP_CONST:
1051     if (!(o->op_private & (OPpCONST_ARYBASE)))
1052     goto nomod;
1053     if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1054     PL_compiling.cop_arybase = (I32)SvIV(cSVOPx(PL_eval_start)->op_sv);
1055     PL_eval_start = 0;
1056     }
1057     else if (!type) {
1058     SAVEI32(PL_compiling.cop_arybase);
1059     PL_compiling.cop_arybase = 0;
1060     }
1061     else if (type == OP_REFGEN)
1062     goto nomod;
1063     else
1064     Perl_croak(aTHX_ "That use of $[ is unsupported");
1065     break;
1066     case OP_STUB:
1067     if (o->op_flags & OPf_PARENS)
1068     break;
1069     goto nomod;
1070     case OP_ENTERSUB:
1071     if ((type == OP_UNDEF || type == OP_REFGEN) &&
1072     !(o->op_flags & OPf_STACKED)) {
1073     o->op_type = OP_RV2CV; /* entersub => rv2cv */
1074     o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1075     assert(cUNOPo->op_first->op_type == OP_NULL);
1076     op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1077     break;
1078     }
1079     else if (o->op_private & OPpENTERSUB_NOMOD)
1080     return o;
1081     else { /* lvalue subroutine call */
1082     o->op_private |= OPpLVAL_INTRO;
1083     PL_modcount = RETURN_UNLIMITED_NUMBER;
1084     if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
1085     /* Backward compatibility mode: */
1086     o->op_private |= OPpENTERSUB_INARGS;
1087     break;
1088     }
1089     else { /* Compile-time error message: */
1090     OP *kid = cUNOPo->op_first;
1091     CV *cv;
1092     OP *okid;
1093    
1094     if (kid->op_type == OP_PUSHMARK)
1095     goto skip_kids;
1096     if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1097     Perl_croak(aTHX_
1098     "panic: unexpected lvalue entersub "
1099     "args: type/targ %ld:%"UVuf,
1100     (long)kid->op_type, (UV)kid->op_targ);
1101     kid = kLISTOP->op_first;
1102     skip_kids:
1103     while (kid->op_sibling)
1104     kid = kid->op_sibling;
1105     if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1106     /* Indirect call */
1107     if (kid->op_type == OP_METHOD_NAMED
1108     || kid->op_type == OP_METHOD)
1109     {
1110     UNOP *newop;
1111    
1112     NewOp(1101, newop, 1, UNOP);
1113     newop->op_type = OP_RV2CV;
1114     newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1115     newop->op_first = Nullop;
1116     newop->op_next = (OP*)newop;
1117     kid->op_sibling = (OP*)newop;
1118     newop->op_private |= OPpLVAL_INTRO;
1119     break;
1120     }
1121    
1122     if (kid->op_type != OP_RV2CV)
1123     Perl_croak(aTHX_
1124     "panic: unexpected lvalue entersub "
1125     "entry via type/targ %ld:%"UVuf,
1126     (long)kid->op_type, (UV)kid->op_targ);
1127     kid->op_private |= OPpLVAL_INTRO;
1128     break; /* Postpone until runtime */
1129     }
1130    
1131     okid = kid;
1132     kid = kUNOP->op_first;
1133     if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1134     kid = kUNOP->op_first;
1135     if (kid->op_type == OP_NULL)
1136     Perl_croak(aTHX_
1137     "Unexpected constant lvalue entersub "
1138     "entry via type/targ %ld:%"UVuf,
1139     (long)kid->op_type, (UV)kid->op_targ);
1140     if (kid->op_type != OP_GV) {
1141     /* Restore RV2CV to check lvalueness */
1142     restore_2cv:
1143     if (kid->op_next && kid->op_next != kid) { /* Happens? */
1144     okid->op_next = kid->op_next;
1145     kid->op_next = okid;
1146     }
1147     else
1148     okid->op_next = Nullop;
1149     okid->op_type = OP_RV2CV;
1150     okid->op_targ = 0;
1151     okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1152     okid->op_private |= OPpLVAL_INTRO;
1153     break;
1154     }
1155    
1156     cv = GvCV(kGVOP_gv);
1157     if (!cv)
1158     goto restore_2cv;
1159     if (CvLVALUE(cv))
1160     break;
1161     }
1162     }
1163     /* FALL THROUGH */
1164     default:
1165     nomod:
1166     /* grep, foreach, subcalls, refgen */
1167     if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1168     break;
1169     yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1170     (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1171     ? "do block"
1172     : (o->op_type == OP_ENTERSUB
1173     ? "non-lvalue subroutine call"
1174     : OP_DESC(o))),
1175     type ? PL_op_desc[type] : "local"));
1176     return o;
1177    
1178     case OP_PREINC:
1179     case OP_PREDEC:
1180     case OP_POW:
1181     case OP_MULTIPLY:
1182     case OP_DIVIDE:
1183     case OP_MODULO:
1184     case OP_REPEAT:
1185     case OP_ADD:
1186     case OP_SUBTRACT:
1187     case OP_CONCAT:
1188     case OP_LEFT_SHIFT:
1189     case OP_RIGHT_SHIFT:
1190     case OP_BIT_AND:
1191     case OP_BIT_XOR:
1192     case OP_BIT_OR:
1193     case OP_I_MULTIPLY:
1194     case OP_I_DIVIDE:
1195     case OP_I_MODULO:
1196     case OP_I_ADD:
1197     case OP_I_SUBTRACT:
1198     if (!(o->op_flags & OPf_STACKED))
1199     goto nomod;
1200     PL_modcount++;
1201     break;
1202    
1203     case OP_COND_EXPR:
1204     for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1205     mod(kid, type);
1206     break;
1207    
1208     case OP_RV2AV:
1209     case OP_RV2HV:
1210     if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1211     PL_modcount = RETURN_UNLIMITED_NUMBER;
1212     return o; /* Treat \(@foo) like ordinary list. */
1213     }
1214     /* FALL THROUGH */
1215     case OP_RV2GV:
1216     if (scalar_mod_type(o, type))
1217     goto nomod;
1218     ref(cUNOPo->op_first, o->op_type);
1219     /* FALL THROUGH */
1220     case OP_ASLICE:
1221     case OP_HSLICE:
1222     if (type == OP_LEAVESUBLV)
1223     o->op_private |= OPpMAYBE_LVSUB;
1224     /* FALL THROUGH */
1225     case OP_AASSIGN:
1226     case OP_NEXTSTATE:
1227     case OP_DBSTATE:
1228     PL_modcount = RETURN_UNLIMITED_NUMBER;
1229     break;
1230     case OP_RV2SV:
1231     ref(cUNOPo->op_first, o->op_type);
1232     /* FALL THROUGH */
1233     case OP_GV:
1234     case OP_AV2ARYLEN:
1235     PL_hints |= HINT_BLOCK_SCOPE;
1236     case OP_SASSIGN:
1237     case OP_ANDASSIGN:
1238     case OP_ORASSIGN:
1239     case OP_AELEMFAST:
1240     /* Needed if maint gets patch 19588
1241     localize = -1;
1242     */
1243     PL_modcount++;
1244     break;
1245    
1246     case OP_PADAV:
1247     case OP_PADHV:
1248     PL_modcount = RETURN_UNLIMITED_NUMBER;
1249     if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1250     return o; /* Treat \(@foo) like ordinary list. */
1251     if (scalar_mod_type(o, type))
1252     goto nomod;
1253     if (type == OP_LEAVESUBLV)
1254     o->op_private |= OPpMAYBE_LVSUB;
1255     /* FALL THROUGH */
1256     case OP_PADSV:
1257     PL_modcount++;
1258     if (!type)
1259     { /* XXX DAPM 2002.08.25 tmp assert test */
1260     /* XXX */ assert(av_fetch(PL_comppad_name, (o->op_targ), FALSE));
1261     /* XXX */ assert(*av_fetch(PL_comppad_name, (o->op_targ), FALSE));
1262    
1263     Perl_croak(aTHX_ "Can't localize lexical variable %s",
1264     PAD_COMPNAME_PV(o->op_targ));
1265     }
1266     break;
1267    
1268     #ifdef USE_5005THREADS
1269     case OP_THREADSV:
1270     PL_modcount++; /* XXX ??? */
1271     break;
1272     #endif /* USE_5005THREADS */
1273    
1274     case OP_PUSHMARK:
1275     break;
1276    
1277     case OP_KEYS:
1278     if (type != OP_SASSIGN)
1279     goto nomod;
1280     goto lvalue_func;
1281     case OP_SUBSTR:
1282     if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1283     goto nomod;
1284     /* FALL THROUGH */
1285     case OP_POS:
1286     case OP_VEC:
1287     if (type == OP_LEAVESUBLV)
1288     o->op_private |= OPpMAYBE_LVSUB;
1289     lvalue_func:
1290     pad_free(o->op_targ);
1291     o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1292     assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1293     if (o->op_flags & OPf_KIDS)
1294     mod(cBINOPo->op_first->op_sibling, type);
1295     break;
1296    
1297     case OP_AELEM:
1298     case OP_HELEM:
1299     ref(cBINOPo->op_first, o->op_type);
1300     if (type == OP_ENTERSUB &&
1301     !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1302     o->op_private |= OPpLVAL_DEFER;
1303     if (type == OP_LEAVESUBLV)
1304     o->op_private |= OPpMAYBE_LVSUB;
1305     PL_modcount++;
1306     break;
1307    
1308     case OP_SCOPE:
1309     case OP_LEAVE:
1310     case OP_ENTER:
1311     case OP_LINESEQ:
1312     if (o->op_flags & OPf_KIDS)
1313     mod(cLISTOPo->op_last, type);
1314     break;
1315    
1316     case OP_NULL:
1317     if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1318     goto nomod;
1319     else if (!(o->op_flags & OPf_KIDS))
1320     break;
1321     if (o->op_targ != OP_LIST) {
1322     mod(cBINOPo->op_first, type);
1323     break;
1324     }
1325     /* FALL THROUGH */
1326     case OP_LIST:
1327     for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1328     mod(kid, type);
1329     break;
1330    
1331     case OP_RETURN:
1332     if (type != OP_LEAVESUBLV)
1333     goto nomod;
1334     break; /* mod()ing was handled by ck_return() */
1335     }
1336    
1337     /* [20011101.069] File test operators interpret OPf_REF to mean that
1338     their argument is a filehandle; thus \stat(".") should not set
1339     it. AMS 20011102 */
1340     if (type == OP_REFGEN &&
1341     PL_check[o->op_type] == MEMBER_TO_FPTR(Perl_ck_ftst))
1342     return o;
1343    
1344     if (type != OP_LEAVESUBLV)
1345     o->op_flags |= OPf_MOD;
1346    
1347     if (type == OP_AASSIGN || type == OP_SASSIGN)
1348     o->op_flags |= OPf_SPECIAL|OPf_REF;
1349     else if (!type) {
1350     o->op_private |= OPpLVAL_INTRO;
1351     o->op_flags &= ~OPf_SPECIAL;
1352     PL_hints |= HINT_BLOCK_SCOPE;
1353     }
1354     else if (type != OP_GREPSTART && type != OP_ENTERSUB
1355     && type != OP_LEAVESUBLV)
1356     o->op_flags |= OPf_REF;
1357     return o;
1358     }
1359    
1360     STATIC bool
1361     S_scalar_mod_type(pTHX_ OP *o, I32 type)
1362     {
1363     switch (type) {
1364     case OP_SASSIGN:
1365     if (o->op_type == OP_RV2GV)
1366     return FALSE;
1367     /* FALL THROUGH */
1368     case OP_PREINC:
1369     case OP_PREDEC:
1370     case OP_POSTINC:
1371     case OP_POSTDEC:
1372     case OP_I_PREINC:
1373     case OP_I_PREDEC:
1374     case OP_I_POSTINC:
1375     case OP_I_POSTDEC:
1376     case OP_POW:
1377     case OP_MULTIPLY:
1378     case OP_DIVIDE:
1379     case OP_MODULO:
1380     case OP_REPEAT:
1381     case OP_ADD:
1382     case OP_SUBTRACT:
1383     case OP_I_MULTIPLY:
1384     case OP_I_DIVIDE:
1385     case OP_I_MODULO:
1386     case OP_I_ADD:
1387     case OP_I_SUBTRACT:
1388     case OP_LEFT_SHIFT:
1389     case OP_RIGHT_SHIFT:
1390     case OP_BIT_AND:
1391     case OP_BIT_XOR:
1392     case OP_BIT_OR:
1393     case OP_CONCAT:
1394     case OP_SUBST:
1395     case OP_TRANS:
1396     case OP_READ:
1397     case OP_SYSREAD:
1398     case OP_RECV:
1399     case OP_ANDASSIGN:
1400     case OP_ORASSIGN:
1401     return TRUE;
1402     default:
1403     return FALSE;
1404     }
1405     }
1406    
1407     STATIC bool
1408     S_is_handle_constructor(pTHX_ OP *o, I32 argnum)
1409     {
1410     switch (o->op_type) {
1411     case OP_PIPE_OP:
1412     case OP_SOCKPAIR:
1413     if (argnum == 2)
1414     return TRUE;
1415     /* FALL THROUGH */
1416     case OP_SYSOPEN:
1417     case OP_OPEN:
1418     case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1419     case OP_SOCKET:
1420     case OP_OPEN_DIR:
1421     case OP_ACCEPT:
1422     if (argnum == 1)
1423     return TRUE;
1424     /* FALL THROUGH */
1425     default:
1426     return FALSE;
1427     }
1428     }
1429    
1430     OP *
1431     Perl_refkids(pTHX_ OP *o, I32 type)
1432     {
1433     OP *kid;
1434     if (o && o->op_flags & OPf_KIDS) {
1435     for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1436     ref(kid, type);
1437     }
1438     return o;
1439     }
1440    
1441     OP *
1442     Perl_ref(pTHX_ OP *o, I32 type)
1443     {
1444     OP *kid;
1445    
1446     if (!o || PL_error_count)
1447     return o;
1448    
1449     switch (o->op_type) {
1450     case OP_ENTERSUB:
1451     if ((type == OP_EXISTS || type == OP_DEFINED || type == OP_LOCK) &&
1452     !(o->op_flags & OPf_STACKED)) {
1453     o->op_type = OP_RV2CV; /* entersub => rv2cv */
1454     o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1455     assert(cUNOPo->op_first->op_type == OP_NULL);
1456     op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1457     o->op_flags |= OPf_SPECIAL;
1458     }
1459     break;
1460    
1461     case OP_COND_EXPR:
1462     for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1463     ref(kid, type);
1464     break;
1465     case OP_RV2SV:
1466     if (type == OP_DEFINED)
1467     o->op_flags |= OPf_SPECIAL; /* don't create GV */
1468     ref(cUNOPo->op_first, o->op_type);
1469     /* FALL THROUGH */
1470     case OP_PADSV:
1471     if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1472     o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1473     : type == OP_RV2HV ? OPpDEREF_HV
1474     : OPpDEREF_SV);
1475     o->op_flags |= OPf_MOD;
1476     }
1477     break;
1478    
1479     case OP_THREADSV:
1480     o->op_flags |= OPf_MOD; /* XXX ??? */
1481     break;
1482    
1483     case OP_RV2AV:
1484     case OP_RV2HV:
1485     o->op_flags |= OPf_REF;
1486     /* FALL THROUGH */
1487     case OP_RV2GV:
1488     if (type == OP_DEFINED)
1489     o->op_flags |= OPf_SPECIAL; /* don't create GV */
1490     ref(cUNOPo->op_first, o->op_type);
1491     break;
1492    
1493     case OP_PADAV:
1494     case OP_PADHV:
1495     o->op_flags |= OPf_REF;
1496     break;
1497    
1498     case OP_SCALAR:
1499     case OP_NULL:
1500     if (!(o->op_flags & OPf_KIDS))
1501     break;
1502     ref(cBINOPo->op_first, type);
1503     break;
1504     case OP_AELEM:
1505     case OP_HELEM:
1506     ref(cBINOPo->op_first, o->op_type);
1507     if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1508     o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1509     : type == OP_RV2HV ? OPpDEREF_HV
1510     : OPpDEREF_SV);
1511     o->op_flags |= OPf_MOD;
1512     }
1513     break;
1514    
1515     case OP_SCOPE:
1516     case OP_LEAVE:
1517     case OP_ENTER:
1518     case OP_LIST:
1519     if (!(o->op_flags & OPf_KIDS))
1520     break;
1521     ref(cLISTOPo->op_last, type);
1522     break;
1523     default:
1524     break;
1525     }
1526     return scalar(o);
1527    
1528     }
1529    
1530     STATIC OP *
1531     S_dup_attrlist(pTHX_ OP *o)
1532     {
1533     OP *rop = Nullop;
1534    
1535     /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1536     * where the first kid is OP_PUSHMARK and the remaining ones
1537     * are OP_CONST. We need to push the OP_CONST values.
1538     */
1539     if (o->op_type == OP_CONST)
1540     rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc(cSVOPo->op_sv));
1541     else {
1542     assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1543     for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1544     if (o->op_type == OP_CONST)
1545     rop = append_elem(OP_LIST, rop,
1546     newSVOP(OP_CONST, o->op_flags,
1547     SvREFCNT_inc(cSVOPo->op_sv)));
1548     }
1549     }
1550     return rop;
1551     }
1552    
1553     STATIC void
1554     S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs, bool for_my)
1555     {
1556     SV *stashsv;
1557    
1558     /* fake up C<use attributes $pkg,$rv,@attrs> */
1559     ENTER; /* need to protect against side-effects of 'use' */
1560     SAVEINT(PL_expect);
1561     if (stash)
1562     stashsv = newSVpv(HvNAME(stash), 0);
1563     else
1564     stashsv = &PL_sv_no;
1565    
1566     #define ATTRSMODULE "attributes"
1567     #define ATTRSMODULE_PM "attributes.pm"
1568    
1569     if (for_my) {
1570     SV **svp;
1571     /* Don't force the C<use> if we don't need it. */
1572     svp = hv_fetch(GvHVn(PL_incgv), ATTRSMODULE_PM,
1573     sizeof(ATTRSMODULE_PM)-1, 0);
1574     if (svp && *svp != &PL_sv_undef)
1575     ; /* already in %INC */
1576     else
1577     Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
1578     newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1579     Nullsv);
1580     }
1581     else {
1582     Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1583     newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1584     Nullsv,
1585     prepend_elem(OP_LIST,
1586     newSVOP(OP_CONST, 0, stashsv),
1587     prepend_elem(OP_LIST,
1588     newSVOP(OP_CONST, 0,
1589     newRV(target)),
1590     dup_attrlist(attrs))));
1591     }
1592     LEAVE;
1593     }
1594    
1595     STATIC void
1596     S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
1597     {
1598     OP *pack, *imop, *arg;
1599     SV *meth, *stashsv;
1600    
1601     if (!attrs)
1602     return;
1603    
1604     assert(target->op_type == OP_PADSV ||
1605     target->op_type == OP_PADHV ||
1606     target->op_type == OP_PADAV);
1607    
1608     /* Ensure that attributes.pm is loaded. */
1609     apply_attrs(stash, PAD_SV(target->op_targ), attrs, TRUE);
1610    
1611     /* Need package name for method call. */
1612     pack = newSVOP(OP_CONST, 0, newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1));
1613    
1614     /* Build up the real arg-list. */
1615     if (stash)
1616     stashsv = newSVpv(HvNAME(stash), 0);
1617     else
1618     stashsv = &PL_sv_no;
1619     arg = newOP(OP_PADSV, 0);
1620     arg->op_targ = target->op_targ;
1621     arg = prepend_elem(OP_LIST,
1622     newSVOP(OP_CONST, 0, stashsv),
1623     prepend_elem(OP_LIST,
1624     newUNOP(OP_REFGEN, 0,
1625     mod(arg, OP_REFGEN)),
1626     dup_attrlist(attrs)));
1627    
1628     /* Fake up a method call to import */
1629     meth = newSVpvn("import", 6);
1630     (void)SvUPGRADE(meth, SVt_PVIV);
1631     (void)SvIOK_on(meth);
1632     PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
1633     imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
1634     append_elem(OP_LIST,
1635     prepend_elem(OP_LIST, pack, list(arg)),
1636     newSVOP(OP_METHOD_NAMED, 0, meth)));
1637     imop->op_private |= OPpENTERSUB_NOMOD;
1638    
1639     /* Combine the ops. */
1640     *imopsp = append_elem(OP_LIST, *imopsp, imop);
1641     }
1642    
1643     /*
1644     =notfor apidoc apply_attrs_string
1645    
1646     Attempts to apply a list of attributes specified by the C<attrstr> and
1647     C<len> arguments to the subroutine identified by the C<cv> argument which
1648     is expected to be associated with the package identified by the C<stashpv>
1649     argument (see L<attributes>). It gets this wrong, though, in that it
1650     does not correctly identify the boundaries of the individual attribute
1651     specifications within C<attrstr>. This is not really intended for the
1652     public API, but has to be listed here for systems such as AIX which
1653     need an explicit export list for symbols. (It's called from XS code
1654     in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
1655     to respect attribute syntax properly would be welcome.
1656    
1657     =cut
1658     */
1659    
1660     void
1661     Perl_apply_attrs_string(pTHX_ char *stashpv, CV *cv,
1662     char *attrstr, STRLEN len)
1663     {
1664     OP *attrs = Nullop;
1665    
1666     if (!len) {
1667     len = strlen(attrstr);
1668     }
1669    
1670     while (len) {
1671     for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
1672     if (len) {
1673     char *sstr = attrstr;
1674     for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
1675     attrs = append_elem(OP_LIST, attrs,
1676     newSVOP(OP_CONST, 0,
1677     newSVpvn(sstr, attrstr-sstr)));
1678     }
1679     }
1680    
1681     Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1682     newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1683     Nullsv, prepend_elem(OP_LIST,
1684     newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
1685     prepend_elem(OP_LIST,
1686     newSVOP(OP_CONST, 0,
1687     newRV((SV*)cv)),
1688     attrs)));
1689     }
1690    
1691     STATIC OP *
1692     S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
1693     {
1694     OP *kid;
1695     I32 type;
1696    
1697     if (!o || PL_error_count)
1698     return o;
1699    
1700     type = o->op_type;
1701     if (type == OP_LIST) {
1702     for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1703     my_kid(kid, attrs, imopsp);
1704     } else if (type == OP_UNDEF) {
1705     return o;
1706     } else if (type == OP_RV2SV || /* "our" declaration */
1707     type == OP_RV2AV ||
1708     type == OP_RV2HV) { /* XXX does this let anything illegal in? */
1709     if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
1710     yyerror(Perl_form(aTHX_ "Can't declare %s in %s",
1711     OP_DESC(o), PL_in_my == KEY_our ? "our" : "my"));
1712     } else if (attrs) {
1713     GV *gv = cGVOPx_gv(cUNOPo->op_first);
1714     PL_in_my = FALSE;
1715     PL_in_my_stash = Nullhv;
1716     apply_attrs(GvSTASH(gv),
1717     (type == OP_RV2SV ? GvSV(gv) :
1718     type == OP_RV2AV ? (SV*)GvAV(gv) :
1719     type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)gv),
1720     attrs, FALSE);
1721     }
1722     o->op_private |= OPpOUR_INTRO;
1723     return o;
1724     }
1725     else if (type != OP_PADSV &&
1726     type != OP_PADAV &&
1727     type != OP_PADHV &&
1728     type != OP_PUSHMARK)
1729     {
1730     yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
1731     OP_DESC(o),
1732     PL_in_my == KEY_our ? "our" : "my"));
1733     return o;
1734     }
1735     else if (attrs && type != OP_PUSHMARK) {
1736     HV *stash;
1737    
1738     PL_in_my = FALSE;
1739     PL_in_my_stash = Nullhv;
1740    
1741     /* check for C<my Dog $spot> when deciding package */
1742     stash = PAD_COMPNAME_TYPE(o->op_targ);
1743     if (!stash)
1744     stash = PL_curstash;
1745     apply_attrs_my(stash, o, attrs, imopsp);
1746     }
1747     o->op_flags |= OPf_MOD;
1748     o->op_private |= OPpLVAL_INTRO;
1749     return o;
1750     }
1751    
1752     OP *
1753     Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1754     {
1755     OP *rops = Nullop;
1756     int maybe_scalar = 0;
1757    
1758     /* [perl #17376]: this appears to be premature, and results in code such as
1759     C< our(%x); > executing in list mode rather than void mode */
1760     #if 0
1761     if (o->op_flags & OPf_PARENS)
1762     list(o);
1763     else
1764     maybe_scalar = 1;
1765     #else
1766     maybe_scalar = 1;
1767     #endif
1768     if (attrs)
1769     SAVEFREEOP(attrs);
1770     o = my_kid(o, attrs, &rops);
1771     if (rops) {
1772     if (maybe_scalar && o->op_type == OP_PADSV) {
1773     o = scalar(append_list(OP_LIST, (LISTOP*)rops, (LISTOP*)o));
1774     o->op_private |= OPpLVAL_INTRO;
1775     }
1776     else
1777     o = append_list(OP_LIST, (LISTOP*)o, (LISTOP*)rops);
1778     }
1779     PL_in_my = FALSE;
1780     PL_in_my_stash = Nullhv;
1781     return o;
1782     }
1783    
1784     OP *
1785     Perl_my(pTHX_ OP *o)
1786     {
1787     return my_attrs(o, Nullop);
1788     }
1789    
1790     OP *
1791     Perl_sawparens(pTHX_ OP *o)
1792     {
1793     if (o)
1794     o->op_flags |= OPf_PARENS;
1795     return o;
1796     }
1797    
1798     OP *
1799     Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
1800     {
1801     OP *o;
1802    
1803     if (ckWARN(WARN_MISC) &&
1804     (left->op_type == OP_RV2AV ||
1805     left->op_type == OP_RV2HV ||
1806     left->op_type == OP_PADAV ||
1807     left->op_type == OP_PADHV)) {
1808     char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
1809     right->op_type == OP_TRANS)
1810     ? right->op_type : OP_MATCH];
1811     const char *sample = ((left->op_type == OP_RV2AV ||
1812     left->op_type == OP_PADAV)
1813     ? "@array" : "%hash");
1814     Perl_warner(aTHX_ packWARN(WARN_MISC),
1815     "Applying %s to %s will act on scalar(%s)",
1816     desc, sample, sample);
1817     }
1818    
1819     if (right->op_type == OP_CONST &&
1820     cSVOPx(right)->op_private & OPpCONST_BARE &&
1821     cSVOPx(right)->op_private & OPpCONST_STRICT)
1822     {
1823     no_bareword_allowed(right);
1824     }
1825    
1826     if (!(right->op_flags & OPf_STACKED) &&
1827     (right->op_type == OP_MATCH ||
1828     right->op_type == OP_SUBST ||
1829     right->op_type == OP_TRANS)) {
1830     right->op_flags |= OPf_STACKED;
1831     if (right->op_type != OP_MATCH &&
1832     ! (right->op_type == OP_TRANS &&
1833     right->op_private & OPpTRANS_IDENTICAL))
1834     left = mod(left, right->op_type);
1835     if (right->op_type == OP_TRANS)
1836     o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1837     else
1838     o = prepend_elem(right->op_type, scalar(left), right);
1839     if (type == OP_NOT)
1840     return newUNOP(OP_NOT, 0, scalar(o));
1841     return o;
1842     }
1843     else
1844     return bind_match(type, left,
1845     pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1846     }
1847    
1848     OP *
1849     Perl_invert(pTHX_ OP *o)
1850     {
1851     if (!o)
1852     return o;
1853     /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1854     return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1855     }
1856    
1857     OP *
1858     Perl_scope(pTHX_ OP *o)
1859     {
1860     if (o) {
1861     if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
1862     o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1863     o->op_type = OP_LEAVE;
1864     o->op_ppaddr = PL_ppaddr[OP_LEAVE];
1865     }
1866     else if (o->op_type == OP_LINESEQ) {
1867     OP *kid;
1868     o->op_type = OP_SCOPE;
1869     o->op_ppaddr = PL_ppaddr[OP_SCOPE];
1870     kid = ((LISTOP*)o)->op_first;
1871     if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
1872     op_null(kid);
1873     }
1874     else
1875     o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1876     }
1877     return o;
1878     }
1879    
1880     /* XXX kept for BINCOMPAT only */
1881     void
1882     Perl_save_hints(pTHX)
1883     {
1884     Perl_croak(aTHX_ "internal error: obsolete function save_hints() called");
1885     }
1886    
1887     int
1888     Perl_block_start(pTHX_ int full)
1889     {
1890     int retval = PL_savestack_ix;
1891     /* If there were syntax errors, don't try to start a block */
1892     if (PL_yynerrs) return retval;
1893    
1894     pad_block_start(full);
1895     SAVEHINTS();
1896     PL_hints &= ~HINT_BLOCK_SCOPE;
1897     SAVESPTR(PL_compiling.cop_warnings);
1898     if (! specialWARN(PL_compiling.cop_warnings)) {
1899     PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1900     SAVEFREESV(PL_compiling.cop_warnings) ;
1901     }
1902     SAVESPTR(PL_compiling.cop_io);
1903     if (! specialCopIO(PL_compiling.cop_io)) {
1904     PL_compiling.cop_io = newSVsv(PL_compiling.cop_io) ;
1905     SAVEFREESV(PL_compiling.cop_io) ;
1906     }
1907     return retval;
1908     }
1909    
1910     OP*
1911     Perl_block_end(pTHX_ I32 floor, OP *seq)
1912     {
1913     int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
1914     OP* retval = scalarseq(seq);
1915     /* If there were syntax errors, don't try to close a block */
1916     if (PL_yynerrs) return retval;
1917     LEAVE_SCOPE(floor);
1918     PL_compiling.op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
1919     if (needblockscope)
1920     PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1921     pad_leavemy();
1922     return retval;
1923     }
1924    
1925     STATIC OP *
1926     S_newDEFSVOP(pTHX)
1927     {
1928     #ifdef USE_5005THREADS
1929     OP *o = newOP(OP_THREADSV, 0);
1930     o->op_targ = find_threadsv("_");
1931     return o;
1932     #else
1933     return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
1934     #endif /* USE_5005THREADS */
1935     }
1936    
1937     void
1938     Perl_newPROG(pTHX_ OP *o)
1939     {
1940     if (PL_in_eval) {
1941     if (PL_eval_root)
1942     return;
1943     PL_eval_root = newUNOP(OP_LEAVEEVAL,
1944     ((PL_in_eval & EVAL_KEEPERR)
1945     ? OPf_SPECIAL : 0), o);
1946     PL_eval_start = linklist(PL_eval_root);
1947     PL_eval_root->op_private |= OPpREFCOUNTED;
1948     OpREFCNT_set(PL_eval_root, 1);
1949     PL_eval_root->op_next = 0;
1950     CALL_PEEP(PL_eval_start);
1951     }
1952     else {
1953     if (o->op_type == OP_STUB) {
1954     PL_comppad_name = 0;
1955     PL_compcv = 0;
1956     FreeOp(o);
1957     return;
1958     }
1959     PL_main_root = scope(sawparens(scalarvoid(o)));
1960     PL_curcop = &PL_compiling;
1961     PL_main_start = LINKLIST(PL_main_root);
1962     PL_main_root->op_private |= OPpREFCOUNTED;
1963     OpREFCNT_set(PL_main_root, 1);
1964     PL_main_root->op_next = 0;
1965     CALL_PEEP(PL_main_start);
1966     PL_compcv = 0;
1967    
1968     /* Register with debugger */
1969     if (PERLDB_INTER) {
1970     CV *cv = get_cv("DB::postponed", FALSE);
1971     if (cv) {
1972     dSP;
1973     PUSHMARK(SP);
1974     XPUSHs((SV*)CopFILEGV(&PL_compiling));
1975     PUTBACK;
1976     call_sv((SV*)cv, G_DISCARD);
1977     }
1978     }
1979     }
1980     }
1981    
1982     OP *
1983     Perl_localize(pTHX_ OP *o, I32 lex)
1984     {
1985     if (o->op_flags & OPf_PARENS)
1986     /* [perl #17376]: this appears to be premature, and results in code such as
1987     C< our(%x); > executing in list mode rather than void mode */
1988     #if 0
1989     list(o);
1990     #else
1991     ;
1992     #endif
1993     else {
1994     if (ckWARN(WARN_PARENTHESIS)
1995     && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',')
1996     {
1997     char *s = PL_bufptr;
1998     bool sigil = FALSE;
1999    
2000     /* some heuristics to detect a potential error */
2001     while (*s && (strchr(", \t\n", *s)))
2002     s++;
2003    
2004     while (1) {
2005     if (*s && strchr("@$%*", *s) && *++s
2006     && (isALNUM(*s) || UTF8_IS_CONTINUED(*s))) {
2007     s++;
2008     sigil = TRUE;
2009     while (*s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s)))
2010     s++;
2011     while (*s && (strchr(", \t\n", *s)))
2012     s++;
2013     }
2014     else
2015     break;
2016     }
2017     if (sigil && (*s == ';' || *s == '=')) {
2018     Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
2019     "Parentheses missing around \"%s\" list",
2020     lex ? (PL_in_my == KEY_our ? "our" : "my")
2021     : "local");
2022     }
2023     }
2024     }
2025     if (lex)
2026     o = my(o);
2027     else
2028     o = mod(o, OP_NULL); /* a bit kludgey */
2029     PL_in_my = FALSE;
2030     PL_in_my_stash = Nullhv;
2031     return o;
2032     }
2033    
2034     OP *
2035     Perl_jmaybe(pTHX_ OP *o)
2036     {
2037     if (o->op_type == OP_LIST) {
2038     OP *o2;
2039     #ifdef USE_5005THREADS
2040     o2 = newOP(OP_THREADSV, 0);
2041     o2->op_targ = find_threadsv(";");
2042     #else
2043     o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
2044     #endif /* USE_5005THREADS */
2045     o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2046     }
2047     return o;
2048     }
2049    
2050     OP *
2051     Perl_fold_constants(pTHX_ register OP *o)
2052     {
2053     register OP *curop;
2054     I32 type = o->op_type;
2055     SV *sv;
2056    
2057     if (PL_opargs[type] & OA_RETSCALAR)
2058     scalar(o);
2059     if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2060     o->op_targ = pad_alloc(type, SVs_PADTMP);
2061    
2062     /* integerize op, unless it happens to be C<-foo>.
2063     * XXX should pp_i_negate() do magic string negation instead? */
2064     if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2065     && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2066     && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2067     {
2068     o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2069     }
2070    
2071     if (!(PL_opargs[type] & OA_FOLDCONST))
2072     goto nope;
2073    
2074     switch (type) {
2075     case OP_NEGATE:
2076     /* XXX might want a ck_negate() for this */
2077     cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2078     break;
2079     case OP_SPRINTF:
2080     case OP_UCFIRST:
2081     case OP_LCFIRST:
2082     case OP_UC:
2083     case OP_LC:
2084     case OP_SLT:
2085     case OP_SGT:
2086     case OP_SLE:
2087     case OP_SGE:
2088     case OP_SCMP:
2089     /* XXX what about the numeric ops? */
2090     if (PL_hints & HINT_LOCALE)
2091     goto nope;
2092     }
2093    
2094     if (PL_error_count)
2095     goto nope; /* Don't try to run w/ errors */
2096    
2097     for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2098     if ((curop->op_type != OP_CONST ||
2099     (curop->op_private & OPpCONST_BARE)) &&
2100     curop->op_type != OP_LIST &&
2101     curop->op_type != OP_SCALAR &&
2102     curop->op_type != OP_NULL &&
2103     curop->op_type != OP_PUSHMARK)
2104     {
2105     goto nope;
2106     }
2107     }
2108    
2109     curop = LINKLIST(o);
2110     o->op_next = 0;
2111     PL_op = curop;
2112     CALLRUNOPS(aTHX);
2113     sv = *(PL_stack_sp--);
2114     if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
2115     pad_swipe(o->op_targ, FALSE);
2116     else if (SvTEMP(sv)) { /* grab mortal temp? */
2117     (void)SvREFCNT_inc(sv);
2118     SvTEMP_off(sv);
2119     }
2120     op_free(o);
2121     if (type == OP_RV2GV)
2122     return newGVOP(OP_GV, 0, (GV*)sv);
2123     return newSVOP(OP_CONST, 0, sv);
2124    
2125     nope:
2126     return o;
2127     }
2128    
2129     OP *
2130     Perl_gen_constant_list(pTHX_ register OP *o)
2131     {
2132     register OP *curop;
2133     I32 oldtmps_floor = PL_tmps_floor;
2134    
2135     list(o);
2136     if (PL_error_count)
2137     return o; /* Don't attempt to run with errors */
2138    
2139     PL_op = curop = LINKLIST(o);
2140     o->op_next = 0;
2141     CALL_PEEP(curop);
2142     pp_pushmark();
2143     CALLRUNOPS(aTHX);
2144     PL_op = curop;
2145     pp_anonlist();
2146     PL_tmps_floor = oldtmps_floor;
2147    
2148     o->op_type = OP_RV2AV;
2149     o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2150     o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
2151     o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
2152     o->op_seq = 0; /* needs to be revisited in peep() */
2153     curop = ((UNOP*)o)->op_first;
2154     ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
2155     op_free(curop);
2156     linklist(o);
2157     return list(o);
2158     }
2159    
2160     OP *
2161     Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2162     {
2163     if (!o || o->op_type != OP_LIST)
2164     o = newLISTOP(OP_LIST, 0, o, Nullop);
2165     else
2166     o->op_flags &= ~OPf_WANT;
2167    
2168     if (!(PL_opargs[type] & OA_MARK))
2169     op_null(cLISTOPo->op_first);
2170    
2171     o->op_type = (OPCODE)type;
2172     o->op_ppaddr = PL_ppaddr[type];
2173     o->op_flags |= flags;
2174    
2175     o = CHECKOP(type, o);
2176     if (o->op_type != (unsigned)type)
2177     return o;
2178    
2179     return fold_constants(o);
2180     }
2181    
2182     /* List constructors */
2183    
2184     OP *
2185     Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2186     {
2187     if (!first)
2188     return last;
2189    
2190     if (!last)
2191     return first;
2192    
2193     if (first->op_type != (unsigned)type
2194     || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
2195     {
2196     return newLISTOP(type, 0, first, last);
2197     }
2198    
2199     if (first->op_flags & OPf_KIDS)
2200     ((LISTOP*)first)->op_last->op_sibling = last;
2201     else {
2202     first->op_flags |= OPf_KIDS;
2203     ((LISTOP*)first)->op_first = last;
2204     }
2205     ((LISTOP*)first)->op_last = last;
2206     return first;
2207     }
2208    
2209     OP *
2210     Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2211     {
2212     if (!first)
2213     return (OP*)last;
2214    
2215     if (!last)
2216     return (OP*)first;
2217    
2218     if (first->op_type != (unsigned)type)
2219     return prepend_elem(type, (OP*)first, (OP*)last);
2220    
2221     if (last->op_type != (unsigned)type)
2222     return append_elem(type, (OP*)first, (OP*)last);
2223    
2224     first->op_last->op_sibling = last->op_first;
2225     first->op_last = last->op_last;
2226     first->op_flags |= (last->op_flags & OPf_KIDS);
2227    
2228     FreeOp(last);
2229    
2230     return (OP*)first;
2231     }
2232    
2233     OP *
2234     Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2235     {
2236     if (!first)
2237     return last;
2238    
2239     if (!last)
2240     return first;
2241    
2242     if (last->op_type == (unsigned)type) {
2243     if (type == OP_LIST) { /* already a PUSHMARK there */
2244     first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2245     ((LISTOP*)last)->op_first->op_sibling = first;
2246     if (!(first->op_flags & OPf_PARENS))
2247     last->op_flags &= ~OPf_PARENS;
2248     }
2249     else {
2250     if (!(last->op_flags & OPf_KIDS)) {
2251     ((LISTOP*)last)->op_last = first;
2252     last->op_flags |= OPf_KIDS;
2253     }
2254     first->op_sibling = ((LISTOP*)last)->op_first;
2255     ((LISTOP*)last)->op_first = first;
2256     }
2257     last->op_flags |= OPf_KIDS;
2258     return last;
2259     }
2260    
2261     return newLISTOP(type, 0, first, last);
2262     }
2263    
2264     /* Constructors */
2265    
2266     OP *
2267     Perl_newNULLLIST(pTHX)
2268     {
2269     return newOP(OP_STUB, 0);
2270     }
2271    
2272     OP *
2273     Perl_force_list(pTHX_ OP *o)
2274     {
2275     if (!o || o->op_type != OP_LIST)
2276     o = newLISTOP(OP_LIST, 0, o, Nullop);
2277     op_null(o);
2278     return o;
2279     }
2280    
2281     OP *
2282     Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2283     {
2284     LISTOP *listop;
2285    
2286     NewOp(1101, listop, 1, LISTOP);
2287    
2288     listop->op_type = (OPCODE)type;
2289     listop->op_ppaddr = PL_ppaddr[type];
2290     if (first || last)
2291     flags |= OPf_KIDS;
2292     listop->op_flags = (U8)flags;
2293    
2294     if (!last && first)
2295     last = first;
2296     else if (!first && last)
2297     first = last;
2298     else if (first)
2299     first->op_sibling = last;
2300     listop->op_first = first;
2301     listop->op_last = last;
2302     if (type == OP_LIST) {
2303     OP* pushop;
2304     pushop = newOP(OP_PUSHMARK, 0);
2305     pushop->op_sibling = first;
2306     listop->op_first = pushop;
2307     listop->op_flags |= OPf_KIDS;
2308     if (!last)
2309     listop->op_last = pushop;
2310     }
2311    
2312     return CHECKOP(type, listop);
2313     }
2314    
2315     OP *
2316     Perl_newOP(pTHX_ I32 type, I32 flags)
2317     {
2318     OP *o;
2319     NewOp(1101, o, 1, OP);
2320     o->op_type = (OPCODE)type;
2321     o->op_ppaddr = PL_ppaddr[type];
2322     o->op_flags = (U8)flags;
2323    
2324     o->op_next = o;
2325     o->op_private = (U8)(0 | (flags >> 8));
2326     if (PL_opargs[type] & OA_RETSCALAR)
2327     scalar(o);
2328     if (PL_opargs[type] & OA_TARGET)
2329     o->op_targ = pad_alloc(type, SVs_PADTMP);
2330     return CHECKOP(type, o);
2331     }
2332    
2333     OP *
2334     Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
2335     {
2336     UNOP *unop;
2337    
2338     if (!first)
2339     first = newOP(OP_STUB, 0);
2340     if (PL_opargs[type] & OA_MARK)
2341     first = force_list(first);
2342    
2343     NewOp(1101, unop, 1, UNOP);
2344     unop->op_type = (OPCODE)type;
2345     unop->op_ppaddr = PL_ppaddr[type];
2346     unop->op_first = first;
2347     unop->op_flags = flags | OPf_KIDS;
2348     unop->op_private = (U8)(1 | (flags >> 8));
2349     unop = (UNOP*) CHECKOP(type, unop);
2350     if (unop->op_next)
2351     return (OP*)unop;
2352    
2353     return fold_constants((OP *) unop);
2354     }
2355    
2356     OP *
2357     Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2358     {
2359     BINOP *binop;
2360     NewOp(1101, binop, 1, BINOP);
2361    
2362     if (!first)
2363     first = newOP(OP_NULL, 0);
2364    
2365     binop->op_type = (OPCODE)type;
2366     binop->op_ppaddr = PL_ppaddr[type];
2367     binop->op_first = first;
2368     binop->op_flags = flags | OPf_KIDS;
2369     if (!last) {
2370     last = first;
2371     binop->op_private = (U8)(1 | (flags >> 8));
2372     }
2373     else {
2374     binop->op_private = (U8)(2 | (flags >> 8));
2375     first->op_sibling = last;
2376     }
2377    
2378     binop = (BINOP*)CHECKOP(type, binop);
2379     if (binop->op_next || binop->op_type != (OPCODE)type)
2380     return (OP*)binop;
2381    
2382     binop->op_last = binop->op_first->op_sibling;
2383    
2384     return fold_constants((OP *)binop);
2385     }
2386    
2387     static int
2388     uvcompare(const void *a, const void *b)
2389     {
2390     if (*((UV *)a) < (*(UV *)b))
2391     return -1;
2392     if (*((UV *)a) > (*(UV *)b))
2393     return 1;
2394     if (*((UV *)a+1) < (*(UV *)b+1))
2395     return -1;
2396     if (*((UV *)a+1) > (*(UV *)b+1))
2397     return 1;
2398     return 0;
2399     }
2400    
2401     OP *
2402     Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
2403     {
2404     SV *tstr = ((SVOP*)expr)->op_sv;
2405     SV *rstr = ((SVOP*)repl)->op_sv;
2406     STRLEN tlen;
2407     STRLEN rlen;
2408     U8 *t = (U8*)SvPV(tstr, tlen);
2409     U8 *r = (U8*)SvPV(rstr, rlen);
2410     register I32 i;
2411     register I32 j;
2412     I32 del;
2413     I32 complement;
2414     I32 squash;
2415     I32 grows = 0;
2416     register short *tbl;
2417    
2418     PL_hints |= HINT_BLOCK_SCOPE;
2419     complement = o->op_private & OPpTRANS_COMPLEMENT;
2420     del = o->op_private & OPpTRANS_DELETE;
2421     squash = o->op_private & OPpTRANS_SQUASH;
2422    
2423     if (SvUTF8(tstr))
2424     o->op_private |= OPpTRANS_FROM_UTF;
2425    
2426     if (SvUTF8(rstr))
2427     o->op_private |= OPpTRANS_TO_UTF;
2428    
2429     if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2430     SV* listsv = newSVpvn("# comment\n",10);
2431     SV* transv = 0;
2432     U8* tend = t + tlen;
2433     U8* rend = r + rlen;
2434     STRLEN ulen;
2435     UV tfirst = 1;
2436     UV tlast = 0;
2437     IV tdiff;
2438     UV rfirst = 1;
2439     UV rlast = 0;
2440     IV rdiff;
2441     IV diff;
2442     I32 none = 0;
2443     U32 max = 0;
2444     I32 bits;
2445     I32 havefinal = 0;
2446     U32 final = 0;
2447     I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2448     I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2449     U8* tsave = NULL;
2450     U8* rsave = NULL;
2451    
2452     if (!from_utf) {
2453     STRLEN len = tlen;
2454     tsave = t = bytes_to_utf8(t, &len);
2455     tend = t + len;
2456     }
2457     if (!to_utf && rlen) {
2458     STRLEN len = rlen;
2459     rsave = r = bytes_to_utf8(r, &len);
2460     rend = r + len;
2461     }
2462    
2463     /* There are several snags with this code on EBCDIC:
2464     1. 0xFF is a legal UTF-EBCDIC byte (there are no illegal bytes).
2465     2. scan_const() in toke.c has encoded chars in native encoding which makes
2466     ranges at least in EBCDIC 0..255 range the bottom odd.
2467     */
2468    
2469     if (complement) {
2470     U8 tmpbuf[UTF8_MAXBYTES+1];
2471     UV *cp;
2472     UV nextmin = 0;
2473     New(1109, cp, 2*tlen, UV);
2474     i = 0;
2475     transv = newSVpvn("",0);
2476     while (t < tend) {
2477     cp[2*i] = utf8n_to_uvuni(t, tend-t, &ulen, 0);
2478     t += ulen;
2479     if (t < tend && NATIVE_TO_UTF(*t) == 0xff) {
2480     t++;
2481     cp[2*i+1] = utf8n_to_uvuni(t, tend-t, &ulen, 0);
2482     t += ulen;
2483     }
2484     else {
2485     cp[2*i+1] = cp[2*i];
2486     }
2487     i++;
2488     }
2489     qsort(cp, i, 2*sizeof(UV), uvcompare);
2490     for (j = 0; j < i; j++) {
2491     UV val = cp[2*j];
2492     diff = val - nextmin;
2493     if (diff > 0) {
2494     t = uvuni_to_utf8(tmpbuf,nextmin);
2495     sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2496     if (diff > 1) {
2497     U8 range_mark = UTF_TO_NATIVE(0xff);
2498     t = uvuni_to_utf8(tmpbuf, val - 1);
2499     sv_catpvn(transv, (char *)&range_mark, 1);
2500     sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2501     }
2502     }
2503     val = cp[2*j+1];
2504     if (val >= nextmin)
2505     nextmin = val + 1;
2506     }
2507     t = uvuni_to_utf8(tmpbuf,nextmin);
2508     sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2509     {
2510     U8 range_mark = UTF_TO_NATIVE(0xff);
2511     sv_catpvn(transv, (char *)&range_mark, 1);
2512     }
2513     t = uvuni_to_utf8_flags(tmpbuf, 0x7fffffff,
2514     UNICODE_ALLOW_SUPER);
2515     sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2516     t = (U8*)SvPVX(transv);
2517     tlen = SvCUR(transv);
2518     tend = t + tlen;
2519     Safefree(cp);
2520     }
2521     else if (!rlen && !del) {
2522     r = t; rlen = tlen; rend = tend;
2523     }
2524     if (!squash) {
2525     if ((!rlen && !del) || t == r ||
2526     (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
2527     {
2528     o->op_private |= OPpTRANS_IDENTICAL;
2529     }
2530     }
2531    
2532     while (t < tend || tfirst <= tlast) {
2533     /* see if we need more "t" chars */
2534     if (tfirst > tlast) {
2535     tfirst = (I32)utf8n_to_uvuni(t, tend - t, &ulen, 0);
2536     t += ulen;
2537     if (t < tend && NATIVE_TO_UTF(*t) == 0xff) { /* illegal utf8 val indicates range */
2538     t++;
2539     tlast = (I32)utf8n_to_uvuni(t, tend - t, &ulen, 0);
2540     t += ulen;
2541     }
2542     else
2543     tlast = tfirst;
2544     }
2545    
2546     /* now see if we need more "r" chars */
2547     if (rfirst > rlast) {
2548     if (r < rend) {
2549     rfirst = (I32)utf8n_to_uvuni(r, rend - r, &ulen, 0);
2550     r += ulen;
2551     if (r < rend && NATIVE_TO_UTF(*r) == 0xff) { /* illegal utf8 val indicates range */
2552     r++;
2553     rlast = (I32)utf8n_to_uvuni(r, rend - r, &ulen, 0);
2554     r += ulen;
2555     }
2556     else
2557     rlast = rfirst;
2558     }
2559     else {
2560     if (!havefinal++)
2561     final = rlast;
2562     rfirst = rlast = 0xffffffff;
2563     }
2564     }
2565    
2566     /* now see which range will peter our first, if either. */
2567     tdiff = tlast - tfirst;
2568     rdiff = rlast - rfirst;
2569    
2570     if (tdiff <= rdiff)
2571     diff = tdiff;
2572     else
2573     diff = rdiff;
2574    
2575     if (rfirst == 0xffffffff) {
2576     diff = tdiff; /* oops, pretend rdiff is infinite */
2577     if (diff > 0)
2578     Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
2579     (long)tfirst, (long)tlast);
2580     else
2581     Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
2582     }
2583     else {
2584     if (diff > 0)
2585     Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
2586     (long)tfirst, (long)(tfirst + diff),
2587     (long)rfirst);
2588     else
2589     Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
2590     (long)tfirst, (long)rfirst);
2591    
2592     if (rfirst + diff > max)
2593     max = rfirst + diff;
2594     if (!grows)
2595     grows = (tfirst < rfirst &&
2596     UNISKIP(tfirst) < UNISKIP(rfirst + diff));
2597     rfirst += diff + 1;
2598     }
2599     tfirst += diff + 1;
2600     }
2601    
2602     none = ++max;
2603     if (del)
2604     del = ++max;
2605    
2606     if (max > 0xffff)
2607     bits = 32;
2608     else if (max > 0xff)
2609     bits = 16;
2610     else
2611     bits = 8;
2612    
2613     Safefree(cPVOPo->op_pv);
2614     cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2615     SvREFCNT_dec(listsv);
2616     if (transv)
2617     SvREFCNT_dec(transv);
2618    
2619     if (!del && havefinal && rlen)
2620     (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5,
2621     newSVuv((UV)final), 0);
2622    
2623     if (grows)
2624     o->op_private |= OPpTRANS_GROWS;
2625    
2626     if (tsave)
2627     Safefree(tsave);
2628     if (rsave)
2629     Safefree(rsave);
2630    
2631     op_free(expr);
2632     op_free(repl);
2633     return o;
2634     }
2635    
2636     tbl = (short*)cPVOPo->op_pv;
2637     if (complement) {
2638     Zero(tbl, 256, short);
2639     for (i = 0; i < (I32)tlen; i++)
2640     tbl[t[i]] = -1;
2641     for (i = 0, j = 0; i < 256; i++) {
2642     if (!tbl[i]) {
2643     if (j >= (I32)rlen) {
2644     if (del)
2645     tbl[i] = -2;
2646     else if (rlen)
2647     tbl[i] = r[j-1];
2648     else
2649     tbl[i] = (short)i;
2650     }
2651     else {
2652     if (i < 128 && r[j] >= 128)
2653     grows = 1;
2654     tbl[i] = r[j++];
2655     }
2656     }
2657     }
2658     if (!del) {
2659     if (!rlen) {
2660     j = rlen;
2661     if (!squash)
2662     o->op_private |= OPpTRANS_IDENTICAL;
2663     }
2664     else if (j >= (I32)rlen)
2665     j = rlen - 1;
2666     else
2667     cPVOPo->op_pv = (char*)Renew(tbl, 0x101+rlen-j, short);
2668     tbl[0x100] = rlen - j;
2669     for (i=0; i < (I32)rlen - j; i++)
2670     tbl[0x101+i] = r[j+i];
2671     }
2672     }
2673     else {
2674     if (!rlen && !del) {
2675     r = t; rlen = tlen;
2676     if (!squash)
2677     o->op_private |= OPpTRANS_IDENTICAL;
2678     }
2679     else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
2680     o->op_private |= OPpTRANS_IDENTICAL;
2681     }
2682     for (i = 0; i < 256; i++)
2683     tbl[i] = -1;
2684     for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
2685     if (j >= (I32)rlen) {
2686     if (del) {
2687     if (tbl[t[i]] == -1)
2688     tbl[t[i]] = -2;
2689     continue;
2690     }
2691     --j;
2692     }
2693     if (tbl[t[i]] == -1) {
2694     if (t[i] < 128 && r[j] >= 128)
2695     grows = 1;
2696     tbl[t[i]] = r[j];
2697     }
2698     }
2699     }
2700     if (grows)
2701     o->op_private |= OPpTRANS_GROWS;
2702     op_free(expr);
2703     op_free(repl);
2704    
2705     return o;
2706     }
2707    
2708     OP *
2709     Perl_newPMOP(pTHX_ I32 type, I32 flags)
2710     {
2711     PMOP *pmop;
2712    
2713     NewOp(1101, pmop, 1, PMOP);
2714     pmop->op_type = (OPCODE)type;
2715     pmop->op_ppaddr = PL_ppaddr[type];
2716     pmop->op_flags = (U8)flags;
2717     pmop->op_private = (U8)(0 | (flags >> 8));
2718    
2719     if (PL_hints & HINT_RE_TAINT)
2720     pmop->op_pmpermflags |= PMf_RETAINT;
2721     if (PL_hints & HINT_LOCALE)
2722     pmop->op_pmpermflags |= PMf_LOCALE;
2723     pmop->op_pmflags = pmop->op_pmpermflags;
2724    
2725     #ifdef USE_ITHREADS
2726     {
2727     SV* repointer;
2728     if(av_len((AV*) PL_regex_pad[0]) > -1) {
2729     repointer = av_pop((AV*)PL_regex_pad[0]);
2730     pmop->op_pmoffset = SvIV(repointer);
2731     SvREPADTMP_off(repointer);
2732     sv_setiv(repointer,0);
2733     } else {
2734     repointer = newSViv(0);
2735     av_push(PL_regex_padav,SvREFCNT_inc(repointer));
2736     pmop->op_pmoffset = av_len(PL_regex_padav);
2737     PL_regex_pad = AvARRAY(PL_regex_padav);
2738     }
2739     }
2740     #endif
2741    
2742     /* link into pm list */
2743     if (type != OP_TRANS && PL_curstash) {
2744     pmop->op_pmnext = HvPMROOT(PL_curstash);
2745     HvPMROOT(PL_curstash) = pmop;
2746     PmopSTASH_set(pmop,PL_curstash);
2747     }
2748    
2749     return CHECKOP(type, pmop);
2750     }
2751    
2752     OP *
2753     Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
2754     {
2755     PMOP *pm;
2756     LOGOP *rcop;
2757     I32 repl_has_vars = 0;
2758    
2759     if (o->op_type == OP_TRANS)
2760     return pmtrans(o, expr, repl);
2761    
2762     PL_hints |= HINT_BLOCK_SCOPE;
2763     pm = (PMOP*)o;
2764    
2765     if (expr->op_type == OP_CONST) {
2766     STRLEN plen;
2767     SV *pat = ((SVOP*)expr)->op_sv;
2768     char *p = SvPV(pat, plen);
2769     if ((o->op_flags & OPf_SPECIAL) && (*p == ' ' && p[1] == '\0')) {
2770     sv_setpvn(pat, "\\s+", 3);
2771     p = SvPV(pat, plen);
2772     pm->op_pmflags |= PMf_SKIPWHITE;
2773     }
2774     if (DO_UTF8(pat))
2775     pm->op_pmdynflags |= PMdf_UTF8;
2776     PM_SETRE(pm, CALLREGCOMP(aTHX_ p, p + plen, pm));
2777     if (strEQ("\\s+", PM_GETRE(pm)->precomp))
2778     pm->op_pmflags |= PMf_WHITE;
2779     op_free(expr);
2780     }
2781     else {
2782     if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2783     expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2784     ? OP_REGCRESET
2785     : OP_REGCMAYBE),0,expr);
2786    
2787     NewOp(1101, rcop, 1, LOGOP);
2788     rcop->op_type = OP_REGCOMP;
2789     rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2790     rcop->op_first = scalar(expr);
2791     rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2792     ? (OPf_SPECIAL | OPf_KIDS)
2793     : OPf_KIDS);
2794     rcop->op_private = 1;
2795     rcop->op_other = o;
2796    
2797     /* establish postfix order */
2798     if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2799     LINKLIST(expr);
2800     rcop->op_next = expr;
2801     ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2802     }
2803     else {
2804     rcop->op_next = LINKLIST(expr);
2805     expr->op_next = (OP*)rcop;
2806     }
2807    
2808     prepend_elem(o->op_type, scalar((OP*)rcop), o);
2809     }
2810    
2811     if (repl) {
2812     OP *curop;
2813     if (pm->op_pmflags & PMf_EVAL) {
2814     curop = 0;
2815     if (CopLINE(PL_curcop) < (line_t)PL_multi_end)
2816     CopLINE_set(PL_curcop, (line_t)PL_multi_end);
2817     }
2818     #ifdef USE_5005THREADS
2819     else if (repl->op_type == OP_THREADSV
2820     && strchr("&`'123456789+",
2821     PL_threadsv_names[repl->op_targ]))
2822     {
2823     curop = 0;
2824     }
2825     #endif /* USE_5005THREADS */
2826     else if (repl->op_type == OP_CONST)
2827     curop = repl;
2828     else {
2829     OP *lastop = 0;
2830     for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2831     if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2832     #ifdef USE_5005THREADS
2833     if (curop->op_type == OP_THREADSV) {
2834     repl_has_vars = 1;
2835     if (strchr("&`'123456789+", curop->op_private))
2836     break;
2837     }
2838     #else
2839     if (curop->op_type == OP_GV) {
2840     GV *gv = cGVOPx_gv(curop);
2841     repl_has_vars = 1;
2842     if (strchr("&`'123456789+-\016\022", *GvENAME(gv)))
2843     break;
2844     }
2845     #endif /* USE_5005THREADS */
2846     else if (curop->op_type == OP_RV2CV)
2847     break;
2848     else if (curop->op_type == OP_RV2SV ||
2849     curop->op_type == OP_RV2AV ||
2850     curop->op_type == OP_RV2HV ||
2851     curop->op_type == OP_RV2GV) {
2852     if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2853     break;
2854     }
2855     else if (curop->op_type == OP_PADSV ||
2856     curop->op_type == OP_PADAV ||
2857     curop->op_type == OP_PADHV ||
2858     curop->op_type == OP_PADANY) {
2859     repl_has_vars = 1;
2860     }
2861     else if (curop->op_type == OP_PUSHRE)
2862     ; /* Okay here, dangerous in newASSIGNOP */
2863     else
2864     break;
2865     }
2866     lastop = curop;
2867     }
2868     }
2869     if (curop == repl
2870     && !(repl_has_vars
2871     && (!PM_GETRE(pm)
2872     || PM_GETRE(pm)->reganch & ROPT_EVAL_SEEN))) {
2873     pm->op_pmflags |= PMf_CONST; /* const for long enough */
2874     pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2875     prepend_elem(o->op_type, scalar(repl), o);
2876     }
2877     else {
2878     if (curop == repl && !PM_GETRE(pm)) { /* Has variables. */
2879     pm->op_pmflags |= PMf_MAYBE_CONST;
2880     pm->op_pmpermflags |= PMf_MAYBE_CONST;
2881     }
2882     NewOp(1101, rcop, 1, LOGOP);
2883     rcop->op_type = OP_SUBSTCONT;
2884     rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
2885     rcop->op_first = scalar(repl);
2886     rcop->op_flags |= OPf_KIDS;
2887     rcop->op_private = 1;
2888     rcop->op_other = o;
2889    
2890     /* establish postfix order */
2891     rcop->op_next = LINKLIST(repl);
2892     repl->op_next = (OP*)rcop;
2893    
2894     pm->op_pmreplroot = scalar((OP*)rcop);
2895     pm->op_pmreplstart = LINKLIST(rcop);
2896     rcop->op_next = 0;
2897     }
2898     }
2899    
2900     return (OP*)pm;
2901     }
2902    
2903     OP *
2904     Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
2905     {
2906     SVOP *svop;
2907     NewOp(1101, svop, 1, SVOP);
2908     svop->op_type = (OPCODE)type;
2909     svop->op_ppaddr = PL_ppaddr[type];
2910     svop->op_sv = sv;
2911     svop->op_next = (OP*)svop;
2912     svop->op_flags = (U8)flags;
2913     if (PL_opargs[type] & OA_RETSCALAR)
2914     scalar((OP*)svop);
2915     if (PL_opargs[type] & OA_TARGET)
2916     svop->op_targ = pad_alloc(type, SVs_PADTMP);
2917     return CHECKOP(type, svop);
2918     }
2919    
2920     OP *
2921     Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
2922     {
2923     PADOP *padop;
2924     NewOp(1101, padop, 1, PADOP);
2925     padop->op_type = (OPCODE)type;
2926     padop->op_ppaddr = PL_ppaddr[type];
2927     padop->op_padix = pad_alloc(type, SVs_PADTMP);
2928     SvREFCNT_dec(PAD_SVl(padop->op_padix));
2929     PAD_SETSV(padop->op_padix, sv);
2930     if (sv)
2931     SvPADTMP_on(sv);
2932     padop->op_next = (OP*)padop;
2933     padop->op_flags = (U8)flags;
2934     if (PL_opargs[type] & OA_RETSCALAR)
2935     scalar((OP*)padop);
2936     if (PL_opargs[type] & OA_TARGET)
2937     padop->op_targ = pad_alloc(type, SVs_PADTMP);
2938     return CHECKOP(type, padop);
2939     }
2940    
2941     OP *
2942     Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
2943     {
2944     #ifdef USE_ITHREADS
2945     if (gv)
2946     GvIN_PAD_on(gv);
2947     return newPADOP(type, flags, SvREFCNT_inc(gv));
2948     #else
2949     return newSVOP(type, flags, SvREFCNT_inc(gv));
2950     #endif
2951     }
2952    
2953     OP *
2954     Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
2955     {
2956     PVOP *pvop;
2957     NewOp(1101, pvop, 1, PVOP);
2958     pvop->op_type = (OPCODE)type;
2959     pvop->op_ppaddr = PL_ppaddr[type];
2960     pvop->op_pv = pv;
2961     pvop->op_next = (OP*)pvop;
2962     pvop->op_flags = (U8)flags;
2963     if (PL_opargs[type] & OA_RETSCALAR)
2964     scalar((OP*)pvop);
2965     if (PL_opargs[type] & OA_TARGET)
2966     pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2967     return CHECKOP(type, pvop);
2968     }
2969    
2970     void
2971     Perl_package(pTHX_ OP *o)
2972     {
2973     SV *sv;
2974    
2975     save_hptr(&PL_curstash);
2976     save_item(PL_curstname);
2977     if (o) {
2978     STRLEN len;
2979     char *name;
2980     sv = cSVOPo->op_sv;
2981     name = SvPV(sv, len);
2982     PL_curstash = gv_stashpvn(name,len,TRUE);
2983     sv_setpvn(PL_curstname, name, len);
2984     op_free(o);
2985     }
2986     else {
2987     deprecate("\"package\" with no arguments");
2988     sv_setpv(PL_curstname,"<none>");
2989     PL_curstash = Nullhv;
2990     }
2991     PL_hints |= HINT_BLOCK_SCOPE;
2992     PL_copline = NOLINE;
2993     PL_expect = XSTATE;
2994     }
2995    
2996     void
2997     Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
2998     {
2999     OP *pack;
3000     OP *imop;
3001     OP *veop;
3002    
3003     if (idop->op_type != OP_CONST)
3004     Perl_croak(aTHX_ "Module name must be constant");
3005    
3006     veop = Nullop;
3007    
3008     if (version != Nullop) {
3009     SV *vesv = ((SVOP*)version)->op_sv;
3010    
3011     if (arg == Nullop && !SvNIOKp(vesv)) {
3012     arg = version;
3013     }
3014     else {
3015     OP *pack;
3016     SV *meth;
3017    
3018     if (version->op_type != OP_CONST || !SvNIOKp(vesv))
3019     Perl_croak(aTHX_ "Version number must be constant number");
3020    
3021     /* Make copy of idop so we don't free it twice */
3022     pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
3023    
3024     /* Fake up a method call to VERSION */
3025     meth = newSVpvn("VERSION",7);
3026     sv_upgrade(meth, SVt_PVIV);
3027     (void)SvIOK_on(meth);
3028     PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
3029     veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3030     append_elem(OP_LIST,
3031     prepend_elem(OP_LIST, pack, list(version)),
3032     newSVOP(OP_METHOD_NAMED, 0, meth)));
3033     }
3034     }
3035    
3036     /* Fake up an import/unimport */
3037     if (arg && arg->op_type == OP_STUB)
3038     imop = arg; /* no import on explicit () */
3039     else if (SvNIOKp(((SVOP*)idop)->op_sv)) {
3040     imop = Nullop; /* use 5.0; */
3041     }
3042     else {
3043     SV *meth;
3044    
3045     /* Make copy of idop so we don't free it twice */
3046     pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
3047    
3048     /* Fake up a method call to import/unimport */
3049     meth = aver ? newSVpvn("import",6) : newSVpvn("unimport", 8);
3050     (void)SvUPGRADE(meth, SVt_PVIV);
3051     (void)SvIOK_on(meth);
3052     PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
3053     imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3054     append_elem(OP_LIST,
3055     prepend_elem(OP_LIST, pack, list(arg)),
3056     newSVOP(OP_METHOD_NAMED, 0, meth)));
3057     }
3058    
3059     /* Fake up the BEGIN {}, which does its thing immediately. */
3060     newATTRSUB(floor,
3061     newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
3062     Nullop,
3063     Nullop,
3064     append_elem(OP_LINESEQ,
3065     append_elem(OP_LINESEQ,
3066     newSTATEOP(0, Nullch, newUNOP(OP_REQUIRE, 0, idop)),
3067     newSTATEOP(0, Nullch, veop)),
3068     newSTATEOP(0, Nullch, imop) ));
3069    
3070     /* The "did you use incorrect case?" warning used to be here.
3071     * The problem is that on case-insensitive filesystems one
3072     * might get false positives for "use" (and "require"):
3073     * "use Strict" or "require CARP" will work. This causes
3074     * portability problems for the script: in case-strict
3075     * filesystems the script will stop working.
3076     *
3077     * The "incorrect case" warning checked whether "use Foo"
3078     * imported "Foo" to your namespace, but that is wrong, too:
3079     * there is no requirement nor promise in the language that
3080     * a Foo.pm should or would contain anything in package "Foo".
3081     *
3082     * There is very little Configure-wise that can be done, either:
3083     * the case-sensitivity of the build filesystem of Perl does not
3084     * help in guessing the case-sensitivity of the runtime environment.
3085     */
3086    
3087     PL_hints |= HINT_BLOCK_SCOPE;
3088     PL_copline = NOLINE;
3089     PL_expect = XSTATE;
3090     PL_cop_seqmax++; /* Purely for B::*'s benefit */
3091     }
3092    
3093     /*
3094     =head1 Embedding Functions
3095    
3096     =for apidoc load_module
3097    
3098     Loads the module whose name is pointed to by the string part of name.
3099     Note that the actual module name, not its filename, should be given.
3100     Eg, "Foo::Bar" instead of "Foo/Bar.pm". flags can be any of
3101     PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS
3102     (or 0 for no flags). ver, if specified, provides version semantics
3103     similar to C<use Foo::Bar VERSION>. The optional trailing SV*
3104     arguments can be used to specify arguments to the module's import()
3105     method, similar to C<use Foo::Bar VERSION LIST>.
3106    
3107     =cut */
3108    
3109     void
3110     Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
3111     {
3112     va_list args;
3113     va_start(args, ver);
3114     vload_module(flags, name, ver, &args);
3115     va_end(args);
3116     }
3117    
3118     #ifdef PERL_IMPLICIT_CONTEXT
3119     void
3120     Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
3121     {
3122     dTHX;
3123     va_list args;
3124     va_start(args, ver);
3125     vload_module(flags, name, ver, &args);
3126     va_end(args);
3127     }
3128     #endif
3129    
3130     void
3131     Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
3132     {
3133     OP *modname, *veop, *imop;
3134    
3135     modname = newSVOP(OP_CONST, 0, name);
3136     modname->op_private |= OPpCONST_BARE;
3137     if (ver) {
3138     veop = newSVOP(OP_CONST, 0, ver);
3139     }
3140     else
3141     veop = Nullop;
3142     if (flags & PERL_LOADMOD_NOIMPORT) {
3143     imop = sawparens(newNULLLIST());
3144     }
3145     else if (flags & PERL_LOADMOD_IMPORT_OPS) {
3146     imop = va_arg(*args, OP*);
3147     }
3148     else {
3149     SV *sv;
3150     imop = Nullop;
3151     sv = va_arg(*args, SV*);
3152     while (sv) {
3153     imop = append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
3154     sv = va_arg(*args, SV*);
3155     }
3156     }
3157     {
3158     line_t ocopline = PL_copline;
3159     COP *ocurcop = PL_curcop;
3160     int oexpect = PL_expect;
3161    
3162     utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
3163     veop, modname, imop);
3164     PL_expect = oexpect;
3165     PL_copline = ocopline;
3166     PL_curcop = ocurcop;
3167     }
3168     }
3169    
3170     OP *
3171     Perl_dofile(pTHX_ OP *term)
3172     {
3173     OP *doop;
3174     GV *gv;
3175    
3176     gv = gv_fetchpv("do", FALSE, SVt_PVCV);
3177     if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv)))
3178     gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
3179    
3180     if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
3181     doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3182     append_elem(OP_LIST, term,
3183     scalar(newUNOP(OP_RV2CV, 0,
3184     newGVOP(OP_GV, 0,
3185     gv))))));
3186     }
3187     else {
3188     doop = newUNOP(OP_DOFILE, 0, scalar(term));
3189     }
3190     return doop;
3191     }
3192    
3193     OP *
3194     Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
3195     {
3196     return newBINOP(OP_LSLICE, flags,
3197     list(force_list(subscript)),
3198     list(force_list(listval)) );
3199     }
3200    
3201     STATIC I32
3202     S_list_assignment(pTHX_ register OP *o)
3203     {
3204     if (!o)
3205     return TRUE;
3206    
3207     if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3208     o = cUNOPo->op_first;
3209    
3210     if (o->op_type == OP_COND_EXPR) {
3211     I32 t = list_assignment(cLOGOPo->op_first->op_sibling);
3212     I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
3213    
3214     if (t && f)
3215     return TRUE;
3216     if (t || f)
3217     yyerror("Assignment to both a list and a scalar");
3218     return FALSE;
3219     }
3220    
3221     if (o->op_type == OP_LIST &&
3222     (o->op_flags & OPf_WANT) == OPf_WANT_SCALAR &&
3223     o->op_private & OPpLVAL_INTRO)
3224     return FALSE;
3225    
3226     if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3227     o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3228     o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
3229     return TRUE;
3230    
3231     if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
3232     return TRUE;
3233    
3234     if (o->op_type == OP_RV2SV)
3235     return FALSE;
3236    
3237     return FALSE;
3238     }
3239    
3240     OP *
3241     Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
3242     {
3243     OP *o;
3244    
3245     if (optype) {
3246     if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
3247     return newLOGOP(optype, 0,
3248     mod(scalar(left), optype),
3249     newUNOP(OP_SASSIGN, 0, scalar(right)));
3250     }
3251     else {
3252     return newBINOP(optype, OPf_STACKED,
3253     mod(scalar(left), optype), scalar(right));
3254     }
3255     }
3256    
3257     if (list_assignment(left)) {
3258     OP *curop;
3259    
3260     PL_modcount = 0;
3261     PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3262     left = mod(left, OP_AASSIGN);
3263     if (PL_eval_start)
3264     PL_eval_start = 0;
3265     else {
3266     op_free(left);
3267     op_free(right);
3268     return Nullop;
3269     }
3270     /* optimise C<my @x = ()> to C<my @x>, and likewise for hashes */
3271     if ((left->op_type == OP_PADAV || left->op_type == OP_PADHV)
3272     && right->op_type == OP_STUB
3273     && (left->op_private & OPpLVAL_INTRO))
3274     {
3275     op_free(right);
3276     left->op_flags &= ~(OPf_REF|OPf_SPECIAL);
3277     return left;
3278     }
3279     curop = list(force_list(left));
3280     o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
3281     o->op_private = (U8)(0 | (flags >> 8));
3282     for (curop = ((LISTOP*)curop)->op_first;
3283     curop; curop = curop->op_sibling)
3284     {
3285     if (curop->op_type == OP_RV2HV &&
3286     ((UNOP*)curop)->op_first->op_type != OP_GV) {
3287     o->op_private |= OPpASSIGN_HASH;
3288     break;
3289     }
3290     }
3291    
3292     /* PL_generation sorcery:
3293     * an assignment like ($a,$b) = ($c,$d) is easier than
3294     * ($a,$b) = ($c,$a), since there is no need for temporary vars.
3295     * To detect whether there are common vars, the global var
3296     * PL_generation is incremented for each assign op we compile.
3297     * Then, while compiling the assign op, we run through all the
3298     * variables on both sides of the assignment, setting a spare slot
3299     * in each of them to PL_generation. If any of them already have
3300     * that value, we know we've got commonality. We could use a
3301     * single bit marker, but then we'd have to make 2 passes, first
3302     * to clear the flag, then to test and set it. To find somewhere
3303     * to store these values, evil chicanery is done with SvCUR().
3304     */
3305    
3306     if (!(left->op_private & OPpLVAL_INTRO)) {
3307     OP *lastop = o;
3308     PL_generation++;
3309     for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3310     if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3311     if (curop->op_type == OP_GV) {
3312     GV *gv = cGVOPx_gv(curop);
3313     if (gv == PL_defgv || (int)SvCUR(gv) == PL_generation)
3314     break;
3315     SvCUR(gv) = PL_generation;
3316     }
3317     else if (curop->op_type == OP_PADSV ||
3318     curop->op_type == OP_PADAV ||
3319     curop->op_type == OP_PADHV ||
3320     curop->op_type == OP_PADANY)
3321     {
3322     if ((int)PAD_COMPNAME_GEN(curop->op_targ)
3323     == PL_generation)
3324     break;
3325     PAD_COMPNAME_GEN(curop->op_targ)
3326     = PL_generation;
3327    
3328     }
3329     else if (curop->op_type == OP_RV2CV)
3330     break;
3331     else if (curop->op_type == OP_RV2SV ||
3332     curop->op_type == OP_RV2AV ||
3333     curop->op_type == OP_RV2HV ||
3334     curop->op_type == OP_RV2GV) {
3335     if (lastop->op_type != OP_GV) /* funny deref? */
3336     break;
3337     }
3338     else if (curop->op_type == OP_PUSHRE) {
3339     if (((PMOP*)curop)->op_pmreplroot) {
3340     #ifdef USE_ITHREADS
3341     GV *gv = (GV*)PAD_SVl(INT2PTR(PADOFFSET,
3342     ((PMOP*)curop)->op_pmreplroot));
3343     #else
3344     GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3345     #endif
3346     if (gv == PL_defgv || (int)SvCUR(gv) == PL_generation)
3347     break;
3348     SvCUR(gv) = PL_generation;
3349     }
3350     }
3351     else
3352     break;
3353     }
3354     lastop = curop;
3355     }
3356     if (curop != o)
3357     o->op_private |= OPpASSIGN_COMMON;
3358     }
3359     if (right && right->op_type == OP_SPLIT) {
3360     OP* tmpop;
3361     if ((tmpop = ((LISTOP*)right)->op_first) &&
3362     tmpop->op_type == OP_PUSHRE)
3363     {
3364     PMOP *pm = (PMOP*)tmpop;
3365     if (left->op_type == OP_RV2AV &&
3366     !(left->op_private & OPpLVAL_INTRO) &&
3367     !(o->op_private & OPpASSIGN_COMMON) )
3368     {
3369     tmpop = ((UNOP*)left)->op_first;
3370     if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
3371     #ifdef USE_ITHREADS
3372     pm->op_pmreplroot = INT2PTR(OP*, cPADOPx(tmpop)->op_padix);
3373     cPADOPx(tmpop)->op_padix = 0; /* steal it */
3374     #else
3375     pm->op_pmreplroot = (OP*)cSVOPx(tmpop)->op_sv;
3376     cSVOPx(tmpop)->op_sv = Nullsv; /* steal it */
3377     #endif
3378     pm->op_pmflags |= PMf_ONCE;
3379     tmpop = cUNOPo->op_first; /* to list (nulled) */
3380     tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3381     tmpop->op_sibling = Nullop; /* don't free split */
3382     right->op_next = tmpop->op_next; /* fix starting loc */
3383     op_free(o); /* blow off assign */
3384     right->op_flags &= ~OPf_WANT;
3385     /* "I don't know and I don't care." */
3386     return right;
3387     }
3388     }
3389     else {
3390     if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
3391     ((LISTOP*)right)->op_last->op_type == OP_CONST)
3392     {
3393     SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3394     if (SvIVX(sv) == 0)
3395     sv_setiv(sv, PL_modcount+1);
3396     }
3397     }
3398     }
3399     }
3400     return o;
3401     }
3402     if (!right)
3403     right = newOP(OP_UNDEF, 0);
3404     if (right->op_type == OP_READLINE) {
3405     right->op_flags |= OPf_STACKED;
3406     return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3407     }
3408     else {
3409     PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3410     o = newBINOP(OP_SASSIGN, flags,
3411     scalar(right), mod(scalar(left), OP_SASSIGN) );
3412     if (PL_eval_start)
3413     PL_eval_start = 0;
3414     else {
3415     op_free(o);
3416     return Nullop;
3417     }
3418     }
3419     return o;
3420     }
3421    
3422     OP *
3423     Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3424     {
3425     U32 seq = intro_my();
3426     register COP *cop;
3427    
3428     NewOp(1101, cop, 1, COP);
3429     if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
3430     cop->op_type = OP_DBSTATE;
3431     cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3432     }
3433     else {
3434     cop->op_type = OP_NEXTSTATE;
3435     cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3436     }
3437     cop->op_flags = (U8)flags;
3438     cop->op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
3439     #ifdef NATIVE_HINTS
3440     cop->op_private |= NATIVE_HINTS;
3441     #endif
3442     PL_compiling.op_private = cop->op_private;
3443     cop->op_next = (OP*)cop;
3444    
3445     if (label) {
3446     cop->cop_label = label;
3447     PL_hints |= HINT_BLOCK_SCOPE;
3448     }
3449     cop->cop_seq = seq;
3450     cop->cop_arybase = PL_curcop->cop_arybase;
3451     if (specialWARN(PL_curcop->cop_warnings))
3452     cop->cop_warnings = PL_curcop->cop_warnings ;
3453     else
3454     cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3455     if (specialCopIO(PL_curcop->cop_io))
3456     cop->cop_io = PL_curcop->cop_io;
3457     else
3458     cop->cop_io = newSVsv(PL_curcop->cop_io) ;
3459    
3460    
3461     if (PL_copline == NOLINE)
3462     CopLINE_set(cop, CopLINE(PL_curcop));
3463     else {
3464     CopLINE_set(cop, PL_copline);
3465     PL_copline = NOLINE;
3466     }
3467     #ifdef USE_ITHREADS
3468     CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
3469     #else
3470     CopFILEGV_set(cop, CopFILEGV(PL_curcop));
3471     #endif
3472     CopSTASH_set(cop, PL_curstash);
3473    
3474     if (PERLDB_LINE && PL_curstash != PL_debstash) {
3475     SV **svp = av_fetch(CopFILEAV(PL_curcop), (I32)CopLINE(cop), FALSE);
3476     if (svp && *svp != &PL_sv_undef ) {
3477     (void)SvIOK_on(*svp);
3478     SvIVX(*svp) = PTR2IV(cop);
3479     }
3480     }
3481    
3482     return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3483     }
3484    
3485    
3486     OP *
3487     Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
3488     {
3489     return new_logop(type, flags, &first, &other);
3490     }
3491    
3492     STATIC OP *
3493     S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
3494     {
3495     LOGOP *logop;
3496     OP *o;
3497     OP *first = *firstp;
3498     OP *other = *otherp;
3499    
3500     if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3501     return newBINOP(type, flags, scalar(first), scalar(other));
3502    
3503     scalarboolean(first);
3504     /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3505     if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3506     if (type == OP_AND || type == OP_OR) {
3507     if (type == OP_AND)
3508     type = OP_OR;
3509     else
3510     type = OP_AND;
3511     o = first;
3512     first = *firstp = cUNOPo->op_first;
3513     if (o->op_next)
3514     first->op_next = o->op_next;
3515     cUNOPo->op_first = Nullop;
3516     op_free(o);
3517     }
3518     }
3519     if (first->op_type == OP_CONST) {
3520     if (first->op_private & OPpCONST_STRICT)
3521     no_bareword_allowed(first);
3522     else if (ckWARN(WARN_BAREWORD) && (first->op_private & OPpCONST_BARE))
3523     Perl_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional");
3524     if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3525     op_free(first);
3526     *firstp = Nullop;
3527     if (other->op_type == OP_CONST)
3528     other->op_private |= OPpCONST_SHORTCIRCUIT;
3529     return other;
3530     }
3531     else {
3532     op_free(other);
3533     *otherp = Nullop;
3534     if (first->op_type == OP_CONST)
3535     first->op_private |= OPpCONST_SHORTCIRCUIT;
3536     return first;
3537     }
3538     }
3539     else if (ckWARN(WARN_MISC) && (first->op_flags & OPf_KIDS)) {
3540     OP *k1 = ((UNOP*)first)->op_first;
3541     OP *k2 = k1->op_sibling;
3542     OPCODE warnop = 0;
3543     switch (first->op_type)
3544     {
3545     case OP_NULL:
3546     if (k2 && k2->op_type == OP_READLINE
3547     && (k2->op_flags & OPf_STACKED)
3548     && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3549     {
3550     warnop = k2->op_type;
3551     }
3552     break;
3553    
3554     case OP_SASSIGN:
3555     if (k1->op_type == OP_READDIR
3556     || k1->op_type == OP_GLOB
3557     || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3558     || k1->op_type == OP_EACH)
3559     {
3560     warnop = ((k1->op_type == OP_NULL)
3561     ? (OPCODE)k1->op_targ : k1->op_type);
3562     }
3563     break;
3564     }
3565     if (warnop) {
3566     line_t oldline = CopLINE(PL_curcop);
3567     CopLINE_set(PL_curcop, PL_copline);
3568     Perl_warner(aTHX_ packWARN(WARN_MISC),
3569     "Value of %s%s can be \"0\"; test with defined()",
3570     PL_op_desc[warnop],
3571     ((warnop == OP_READLINE || warnop == OP_GLOB)
3572     ? " construct" : "() operator"));
3573     CopLINE_set(PL_curcop, oldline);
3574     }
3575     }
3576    
3577     if (!other)
3578     return first;
3579    
3580     if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3581     other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3582    
3583     NewOp(1101, logop, 1, LOGOP);
3584    
3585     logop->op_type = (OPCODE)type;
3586     logop->op_ppaddr = PL_ppaddr[type];
3587     logop->op_first = first;
3588     logop->op_flags = flags | OPf_KIDS;
3589     logop->op_other = LINKLIST(other);
3590     logop->op_private = (U8)(1 | (flags >> 8));
3591    
3592     /* establish postfix order */
3593     logop->op_next = LINKLIST(first);
3594     first->op_next = (OP*)logop;
3595     first->op_sibling = other;
3596    
3597     CHECKOP(type,logop);
3598    
3599     o = newUNOP(OP_NULL, 0, (OP*)logop);
3600     other->op_next = o;
3601    
3602     return o;
3603     }
3604    
3605     OP *
3606     Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
3607     {
3608     LOGOP *logop;
3609     OP *start;
3610     OP *o;
3611    
3612     if (!falseop)
3613     return newLOGOP(OP_AND, 0, first, trueop);
3614     if (!trueop)
3615     return newLOGOP(OP_OR, 0, first, falseop);
3616    
3617     scalarboolean(first);
3618     if (first->op_type == OP_CONST) {
3619     if (first->op_private & OPpCONST_BARE &&
3620     first->op_private & OPpCONST_STRICT) {
3621     no_bareword_allowed(first);
3622     }
3623     if (SvTRUE(((SVOP*)first)->op_sv)) {
3624     op_free(first);
3625     op_free(falseop);
3626     return trueop;
3627     }
3628     else {
3629     op_free(first);
3630     op_free(trueop);
3631     return falseop;
3632     }
3633     }
3634     NewOp(1101, logop, 1, LOGOP);
3635     logop->op_type = OP_COND_EXPR;
3636     logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3637     logop->op_first = first;
3638     logop->op_flags = flags | OPf_KIDS;
3639     logop->op_private = (U8)(1 | (flags >> 8));
3640     logop->op_other = LINKLIST(trueop);
3641     logop->op_next = LINKLIST(falseop);
3642    
3643     CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
3644     logop);
3645    
3646     /* establish postfix order */
3647     start = LINKLIST(first);
3648     first->op_next = (OP*)logop;
3649    
3650     first->op_sibling = trueop;
3651     trueop->op_sibling = falseop;
3652     o = newUNOP(OP_NULL, 0, (OP*)logop);
3653    
3654     trueop->op_next = falseop->op_next = o;
3655    
3656     o->op_next = start;
3657     return o;
3658     }
3659    
3660     OP *
3661     Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
3662     {
3663     LOGOP *range;
3664     OP *flip;
3665     OP *flop;
3666     OP *leftstart;
3667     OP *o;
3668    
3669     NewOp(1101, range, 1, LOGOP);
3670    
3671     range->op_type = OP_RANGE;
3672     range->op_ppaddr = PL_ppaddr[OP_RANGE];
3673     range->op_first = left;
3674     range->op_flags = OPf_KIDS;
3675     leftstart = LINKLIST(left);
3676     range->op_other = LINKLIST(right);
3677     range->op_private = (U8)(1 | (flags >> 8));
3678    
3679     left->op_sibling = right;
3680    
3681     range->op_next = (OP*)range;
3682     flip = newUNOP(OP_FLIP, flags, (OP*)range);
3683     flop = newUNOP(OP_FLOP, 0, flip);
3684     o = newUNOP(OP_NULL, 0, flop);
3685     linklist(flop);
3686     range->op_next = leftstart;
3687    
3688     left->op_next = flip;
3689     right->op_next = flop;
3690    
3691     range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3692     sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
3693     flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3694     sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3695    
3696     flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3697     flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3698    
3699     flip->op_next = o;
3700     if (!flip->op_private || !flop->op_private)
3701     linklist(o); /* blow off optimizer unless constant */
3702    
3703     return o;
3704     }
3705    
3706     OP *
3707     Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
3708     {
3709     OP* listop;
3710     OP* o;
3711     int once = block && block->op_flags & OPf_SPECIAL &&
3712     (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3713    
3714     if (expr) {
3715     if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3716     return block; /* do {} while 0 does once */
3717     if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3718     || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3719     expr = newUNOP(OP_DEFINED, 0,
3720     newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3721     } else if (expr->op_flags & OPf_KIDS) {
3722     OP *k1 = ((UNOP*)expr)->op_first;
3723     OP *k2 = (k1) ? k1->op_sibling : NULL;
3724     switch (expr->op_type) {
3725     case OP_NULL:
3726     if (k2 && k2->op_type == OP_READLINE
3727     && (k2->op_flags & OPf_STACKED)
3728     && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3729     expr = newUNOP(OP_DEFINED, 0, expr);
3730     break;
3731    
3732     case OP_SASSIGN:
3733     if (k1->op_type == OP_READDIR
3734     || k1->op_type == OP_GLOB
3735     || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3736     || k1->op_type == OP_EACH)
3737     expr = newUNOP(OP_DEFINED, 0, expr);
3738     break;
3739     }
3740     }
3741     }
3742    
3743     /* if block is null, the next append_elem() would put UNSTACK, a scalar
3744     * op, in listop. This is wrong. [perl #27024] */
3745     if (!block)
3746     block = newOP(OP_NULL, 0);
3747     listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3748     o = new_logop(OP_AND, 0, &expr, &listop);
3749    
3750     if (listop)
3751     ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3752    
3753     if (once && o != listop)
3754     o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3755    
3756     if (o == listop)
3757     o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3758    
3759     o->op_flags |= flags;
3760     o = scope(o);
3761     o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3762     return o;
3763     }
3764    
3765     OP *
3766     Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
3767     {
3768     OP *redo;
3769     OP *next = 0;
3770     OP *listop;
3771     OP *o;
3772     U8 loopflags = 0;
3773    
3774     if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3775     || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
3776     expr = newUNOP(OP_DEFINED, 0,
3777     newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3778     } else if (expr && (expr->op_flags & OPf_KIDS)) {
3779     OP *k1 = ((UNOP*)expr)->op_first;
3780     OP *k2 = (k1) ? k1->op_sibling : NULL;
3781     switch (expr->op_type) {
3782     case OP_NULL:
3783     if (k2 && k2->op_type == OP_READLINE
3784     && (k2->op_flags & OPf_STACKED)
3785     && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3786     expr = newUNOP(OP_DEFINED, 0, expr);
3787     break;
3788    
3789     case OP_SASSIGN:
3790     if (k1->op_type == OP_READDIR
3791     || k1->op_type == OP_GLOB
3792     || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3793     || k1->op_type == OP_EACH)
3794     expr = newUNOP(OP_DEFINED, 0, expr);
3795     break;
3796     }
3797     }
3798    
3799     if (!block)
3800     block = newOP(OP_NULL, 0);
3801     else if (cont) {
3802     block = scope(block);
3803     }
3804    
3805     if (cont) {
3806     next = LINKLIST(cont);
3807     }
3808     if (expr) {
3809     OP *unstack = newOP(OP_UNSTACK, 0);
3810     if (!next)
3811     next = unstack;
3812     cont = append_elem(OP_LINESEQ, cont, unstack);
3813     }
3814    
3815     listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3816     redo = LINKLIST(listop);
3817    
3818     if (expr) {
3819     PL_copline = (line_t)whileline;
3820     scalar(listop);
3821     o = new_logop(OP_AND, 0, &expr, &listop);
3822     if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3823     op_free(expr); /* oops, it's a while (0) */
3824     op_free((OP*)loop);
3825     return Nullop; /* listop already freed by new_logop */
3826     }
3827     if (listop)
3828     ((LISTOP*)listop)->op_last->op_next =
3829     (o == listop ? redo : LINKLIST(o));
3830     }
3831     else
3832     o = listop;
3833    
3834     if (!loop) {
3835     NewOp(1101,loop,1,LOOP);
3836     loop->op_type = OP_ENTERLOOP;
3837     loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3838     loop->op_private = 0;
3839     loop->op_next = (OP*)loop;
3840     }
3841    
3842     o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3843    
3844     loop->op_redoop = redo;
3845     loop->op_lastop = o;
3846     o->op_private |= loopflags;
3847    
3848     if (next)
3849     loop->op_nextop = next;
3850     else
3851     loop->op_nextop = o;
3852    
3853     o->op_flags |= flags;
3854     o->op_private |= (flags >> 8);
3855     return o;
3856     }
3857    
3858     OP *
3859     Perl_newFOROP(pTHX_ I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
3860     {
3861     LOOP *loop;
3862     OP *wop;
3863     PADOFFSET padoff = 0;
3864     I32 iterflags = 0;
3865     I32 iterpflags = 0;
3866    
3867     if (sv) {
3868     if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3869     iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */
3870     sv->op_type = OP_RV2GV;
3871     sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
3872     }
3873     else if (sv->op_type == OP_PADSV) { /* private variable */
3874     iterpflags = sv->op_private & OPpLVAL_INTRO; /* for my $x () */
3875     padoff = sv->op_targ;
3876     sv->op_targ = 0;
3877     op_free(sv);
3878     sv = Nullop;
3879     }
3880     else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3881     padoff = sv->op_targ;
3882     sv->op_targ = 0;
3883     iterflags |= OPf_SPECIAL;
3884     op_free(sv);
3885     sv = Nullop;
3886     }
3887     else
3888     Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
3889     }
3890     else {
3891     #ifdef USE_5005THREADS
3892     padoff = find_threadsv("_");
3893     iterflags |= OPf_SPECIAL;
3894     #else
3895     sv = newGVOP(OP_GV, 0, PL_defgv);
3896     #endif
3897     }
3898     if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3899     expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3900     iterflags |= OPf_STACKED;
3901     }
3902     else if (expr->op_type == OP_NULL &&
3903     (expr->op_flags & OPf_KIDS) &&
3904     ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3905     {
3906     /* Basically turn for($x..$y) into the same as for($x,$y), but we
3907     * set the STACKED flag to indicate that these values are to be
3908     * treated as min/max values by 'pp_iterinit'.
3909     */
3910     UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3911     LOGOP* range = (LOGOP*) flip->op_first;
3912     OP* left = range->op_first;
3913     OP* right = left->op_sibling;
3914     LISTOP* listop;
3915    
3916     range->op_flags &= ~OPf_KIDS;
3917     range->op_first = Nullop;
3918    
3919     listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3920     listop->op_first->op_next = range->op_next;
3921     left->op_next = range->op_other;
3922     right->op_next = (OP*)listop;
3923     listop->op_next = listop->op_first;
3924    
3925     op_free(expr);
3926     expr = (OP*)(listop);
3927     op_null(expr);
3928     iterflags |= OPf_STACKED;
3929     }
3930     else {
3931     expr = mod(force_list(expr), OP_GREPSTART);
3932     }
3933    
3934     loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
3935     append_elem(OP_LIST, expr, scalar(sv))));
3936     assert(!loop->op_next);
3937     /* for my $x () sets OPpLVAL_INTRO;
3938     * for our $x () sets OPpOUR_INTRO; both only used by Deparse.pm */
3939     loop->op_private = (U8)iterpflags;
3940     #ifdef PL_OP_SLAB_ALLOC
3941     {
3942     LOOP *tmp;
3943     NewOp(1234,tmp,1,LOOP);
3944     Copy(loop,tmp,1,LISTOP);
3945     FreeOp(loop);
3946     loop = tmp;
3947     }
3948     #else
3949     Renew(loop, 1, LOOP);
3950     #endif
3951     loop->op_targ = padoff;
3952     wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
3953     PL_copline = forline;
3954     return newSTATEOP(0, label, wop);
3955     }
3956    
3957     OP*
3958     Perl_newLOOPEX(pTHX_ I32 type, OP *label)
3959     {
3960     OP *o;
3961     STRLEN n_a;
3962    
3963     if (type != OP_GOTO || label->op_type == OP_CONST) {
3964     /* "last()" means "last" */
3965     if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
3966     o = newOP(type, OPf_SPECIAL);
3967     else {
3968     o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
3969     ? SvPVx(((SVOP*)label)->op_sv, n_a)
3970     : ""));
3971     }
3972     op_free(label);
3973     }
3974     else {
3975     /* Check whether it's going to be a goto &function */
3976     if (label->op_type == OP_ENTERSUB
3977     && !(label->op_flags & OPf_STACKED))
3978     label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
3979     o = newUNOP(type, OPf_STACKED, label);
3980     }
3981     PL_hints |= HINT_BLOCK_SCOPE;
3982     return o;
3983     }
3984    
3985     /*
3986     =for apidoc cv_undef
3987    
3988     Clear out all the active components of a CV. This can happen either
3989     by an explicit C<undef &foo>, or by the reference count going to zero.
3990     In the former case, we keep the CvOUTSIDE pointer, so that any anonymous
3991     children can still follow the full lexical scope chain.
3992    
3993     =cut
3994     */
3995    
3996     void
3997     Perl_cv_undef(pTHX_ CV *cv)
3998     {
3999     #ifdef USE_5005THREADS
4000     if (CvMUTEXP(cv)) {
4001     MUTEX_DESTROY(CvMUTEXP(cv));
4002     Safefree(CvMUTEXP(cv));
4003     CvMUTEXP(cv) = 0;
4004     }
4005     #endif /* USE_5005THREADS */
4006    
4007     #ifdef USE_ITHREADS
4008     if (CvFILE(cv) && !CvXSUB(cv)) {
4009     /* for XSUBs CvFILE point directly to static memory; __FILE__ */
4010     Safefree(CvFILE(cv));
4011     }
4012     CvFILE(cv) = 0;
4013     #endif
4014    
4015     if (!CvXSUB(cv) && CvROOT(cv)) {
4016     #ifdef USE_5005THREADS
4017     if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
4018     Perl_croak(aTHX_ "Can't undef active subroutine");
4019     #else
4020     if (CvDEPTH(cv))
4021     Perl_croak(aTHX_ "Can't undef active subroutine");
4022     #endif /* USE_5005THREADS */
4023     ENTER;
4024    
4025     PAD_SAVE_SETNULLPAD();
4026    
4027     op_free(CvROOT(cv));
4028     CvROOT(cv) = Nullop;
4029     LEAVE;
4030     }
4031     SvPOK_off((SV*)cv); /* forget prototype */
4032     CvGV(cv) = Nullgv;
4033    
4034     pad_undef(cv);
4035    
4036     /* remove CvOUTSIDE unless this is an undef rather than a free */
4037     if (!SvREFCNT(cv) && CvOUTSIDE(cv)) {
4038     if (!CvWEAKOUTSIDE(cv))
4039     SvREFCNT_dec(CvOUTSIDE(cv));
4040     CvOUTSIDE(cv) = Nullcv;
4041     }
4042     if (CvCONST(cv)) {
4043     SvREFCNT_dec((SV*)CvXSUBANY(cv).any_ptr);
4044     CvCONST_off(cv);
4045     }
4046     if (CvXSUB(cv)) {
4047     CvXSUB(cv) = 0;
4048     }
4049     /* delete all flags except WEAKOUTSIDE */
4050     CvFLAGS(cv) &= CVf_WEAKOUTSIDE;
4051     }
4052    
4053     void
4054     Perl_cv_ckproto(pTHX_ CV *cv, GV *gv, char *p)
4055     {
4056     if (((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) && ckWARN_d(WARN_PROTOTYPE)) {
4057     SV* msg = sv_newmortal();
4058     SV* name = Nullsv;
4059    
4060     if (gv)
4061     gv_efullname3(name = sv_newmortal(), gv, Nullch);
4062     sv_setpv(msg, "Prototype mismatch:");
4063     if (name)
4064     Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, name);
4065     if (SvPOK(cv))
4066     Perl_sv_catpvf(aTHX_ msg, " (%"SVf")", (SV *)cv);
4067     else
4068     Perl_sv_catpv(aTHX_ msg, ": none");
4069     sv_catpv(msg, " vs ");
4070     if (p)
4071     Perl_sv_catpvf(aTHX_ msg, "(%s)", p);
4072     else
4073     sv_catpv(msg, "none");
4074     Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%"SVf, msg);
4075     }
4076     }
4077    
4078     static void const_sv_xsub(pTHX_ CV* cv);
4079    
4080     /*
4081    
4082     =head1 Optree Manipulation Functions
4083    
4084     =for apidoc cv_const_sv
4085    
4086     If C<cv> is a constant sub eligible for inlining. returns the constant
4087     value returned by the sub. Otherwise, returns NULL.
4088    
4089     Constant subs can be created with C<newCONSTSUB> or as described in
4090     L<perlsub/"Constant Functions">.
4091    
4092     =cut
4093     */
4094     SV *
4095     Perl_cv_const_sv(pTHX_ CV *cv)
4096     {
4097     if (!cv || !CvCONST(cv))
4098     return Nullsv;
4099     return (SV*)CvXSUBANY(cv).any_ptr;
4100     }
4101    
4102     SV *
4103     Perl_op_const_sv(pTHX_ OP *o, CV *cv)
4104     {
4105     SV *sv = Nullsv;
4106    
4107     if (!o)
4108     return Nullsv;
4109    
4110     if (o->op_type == OP_LINESEQ && cLISTOPo->op_first)
4111     o = cLISTOPo->op_first->op_sibling;
4112    
4113     for (; o; o = o->op_next) {
4114     OPCODE type = o->op_type;
4115    
4116     if (sv && o->op_next == o)
4117     return sv;
4118     if (o->op_next != o) {
4119     if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
4120     continue;
4121     if (type == OP_DBSTATE)
4122     continue;
4123     }
4124     if (type == OP_LEAVESUB || type == OP_RETURN)
4125     break;
4126     if (sv)
4127     return Nullsv;
4128     if (type == OP_CONST && cSVOPo->op_sv)
4129     sv = cSVOPo->op_sv;
4130     else if ((type == OP_PADSV || type == OP_CONST) && cv) {
4131     sv = PAD_BASE_SV(CvPADLIST(cv), o->op_targ);
4132     if (!sv)
4133     return Nullsv;
4134     if (CvCONST(cv)) {
4135     /* We get here only from cv_clone2() while creating a closure.
4136     Copy the const value here instead of in cv_clone2 so that
4137     SvREADONLY_on doesn't lead to problems when leaving
4138     scope.
4139     */
4140     sv = newSVsv(sv);
4141     }
4142     if (!SvREADONLY(sv) && SvREFCNT(sv) > 1)
4143     return Nullsv;
4144     }
4145     else
4146     return Nullsv;
4147     }
4148     if (sv)
4149     SvREADONLY_on(sv);
4150     return sv;
4151     }
4152    
4153     void
4154     Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4155     {
4156     if (o)
4157     SAVEFREEOP(o);
4158     if (proto)
4159     SAVEFREEOP(proto);
4160     if (attrs)
4161     SAVEFREEOP(attrs);
4162     if (block)
4163     SAVEFREEOP(block);
4164     Perl_croak(aTHX_ "\"my sub\" not yet implemented");
4165     }
4166    
4167     CV *
4168     Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
4169     {
4170     return Perl_newATTRSUB(aTHX_ floor, o, proto, Nullop, block);
4171     }
4172    
4173     CV *
4174     Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4175     {
4176     STRLEN n_a;
4177     char *name;
4178     char *aname;
4179     GV *gv;
4180     char *ps;
4181     register CV *cv=0;
4182     SV *const_sv;
4183    
4184     name = o ? SvPVx(cSVOPo->op_sv, n_a) : Nullch;
4185    
4186     if (proto) {
4187     assert(proto->op_type == OP_CONST);
4188     ps = SvPVx(((SVOP*)proto)->op_sv, n_a);
4189     }
4190     else
4191     ps = Nullch;
4192    
4193     if (!name && PERLDB_NAMEANON && CopLINE(PL_curcop)) {
4194     SV *sv = sv_newmortal();
4195     Perl_sv_setpvf(aTHX_ sv, "%s[%s:%"IVdf"]",
4196     PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
4197     CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
4198     aname = SvPVX(sv);
4199     }
4200     else
4201     aname = Nullch;
4202     gv = gv_fetchpv(name ? name : (aname ? aname :
4203     (PL_curstash ? "__ANON__" : "__ANON__::__ANON__")),
4204     GV_ADDMULTI | ((block || attrs) ? 0 : GV_NOINIT),
4205     SVt_PVCV);
4206    
4207     if (o)
4208     SAVEFREEOP(o);
4209     if (proto)
4210     SAVEFREEOP(proto);
4211     if (attrs)
4212     SAVEFREEOP(attrs);
4213    
4214     if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
4215     maximum a prototype before. */
4216     if (SvTYPE(gv) > SVt_NULL) {
4217     if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1)
4218     && ckWARN_d(WARN_PROTOTYPE))
4219     {
4220     Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "Runaway prototype");
4221     }
4222     cv_ckproto((CV*)gv, NULL, ps);
4223     }
4224     if (ps)
4225     sv_setpv((SV*)gv, ps);
4226     else
4227     sv_setiv((SV*)gv, -1);
4228     SvREFCNT_dec(PL_compcv);
4229     cv = PL_compcv = NULL;
4230     PL_sub_generation++;
4231     goto done;
4232     }
4233    
4234     cv = (!name || GvCVGEN(gv)) ? Nullcv : GvCV(gv);
4235    
4236     #ifdef GV_UNIQUE_CHECK
4237     if (cv && GvUNIQUE(gv) && SvREADONLY(cv)) {
4238     Perl_croak(aTHX_ "Can't define subroutine %s (GV is unique)", name);
4239     }
4240     #endif
4241    
4242     if (!block || !ps || *ps || attrs)
4243     const_sv = Nullsv;
4244     else
4245     const_sv = op_const_sv(block, Nullcv);
4246    
4247     if (cv) {
4248     bool exists = CvROOT(cv) || CvXSUB(cv);
4249    
4250     #ifdef GV_UNIQUE_CHECK
4251     if (exists && GvUNIQUE(gv)) {
4252     Perl_croak(aTHX_ "Can't redefine unique subroutine %s", name);
4253     }
4254     #endif
4255    
4256     /* if the subroutine doesn't exist and wasn't pre-declared
4257     * with a prototype, assume it will be AUTOLOADed,
4258     * skipping the prototype check
4259     */
4260     if (exists || SvPOK(cv))
4261     cv_ckproto(cv, gv, ps);
4262     /* already defined (or promised)? */
4263     if (exists || GvASSUMECV(gv)) {
4264     if (!block && !attrs) {
4265     if (CvFLAGS(PL_compcv)) {
4266     /* might have had built-in attrs applied */
4267     CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
4268     }
4269     /* just a "sub foo;" when &foo is already defined */
4270     SAVEFREESV(PL_compcv);
4271     goto done;
4272     }
4273     /* ahem, death to those who redefine active sort subs */
4274     if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv))
4275     Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", name);
4276     if (block) {
4277     if (ckWARN(WARN_REDEFINE)
4278     || (CvCONST(cv)
4279     && (!const_sv || sv_cmp(cv_const_sv(cv), const_sv))))
4280     {
4281     line_t oldline = CopLINE(PL_curcop);
4282     if (PL_copline != NOLINE)
4283     CopLINE_set(PL_curcop, PL_copline);
4284     Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
4285     CvCONST(cv) ? "Constant subroutine %s redefined"
4286     : "Subroutine %s redefined", name);
4287     CopLINE_set(PL_curcop, oldline);
4288     }
4289     SvREFCNT_dec(cv);
4290     cv = Nullcv;
4291     }
4292     }
4293     }
4294     if (const_sv) {
4295     SvREFCNT_inc(const_sv);
4296     if (cv) {
4297     assert(!CvROOT(cv) && !CvCONST(cv));
4298     sv_setpv((SV*)cv, ""); /* prototype is "" */
4299     CvXSUBANY(cv).any_ptr = const_sv;
4300     CvXSUB(cv) = const_sv_xsub;
4301     CvCONST_on(cv);
4302     }
4303     else {
4304     GvCV(gv) = Nullcv;
4305     cv = newCONSTSUB(NULL, name, const_sv);
4306     }
4307     op_free(block);
4308     SvREFCNT_dec(PL_compcv);
4309     PL_compcv = NULL;
4310     PL_sub_generation++;
4311     goto done;
4312     }
4313     if (attrs) {
4314     HV *stash;
4315     SV *rcv;
4316    
4317     /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>
4318     * before we clobber PL_compcv.
4319     */
4320     if (cv && !block) {
4321     rcv = (SV*)cv;
4322     /* Might have had built-in attributes applied -- propagate them. */
4323     CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
4324     if (CvGV(cv) && GvSTASH(CvGV(cv)))
4325     stash = GvSTASH(CvGV(cv));
4326     else if (CvSTASH(cv))
4327     stash = CvSTASH(cv);
4328     else
4329     stash = PL_curstash;
4330     }
4331     else {
4332     /* possibly about to re-define existing subr -- ignore old cv */
4333     rcv = (SV*)PL_compcv;
4334     if (name && GvSTASH(gv))
4335     stash = GvSTASH(gv);
4336     else
4337     stash = PL_curstash;
4338     }
4339     apply_attrs(stash, rcv, attrs, FALSE);
4340     }
4341     if (cv) { /* must reuse cv if autoloaded */
4342     if (!block) {
4343     /* got here with just attrs -- work done, so bug out */
4344     SAVEFREESV(PL_compcv);
4345     goto done;
4346     }
4347     /* transfer PL_compcv to cv */
4348     cv_undef(cv);
4349     CvFLAGS(cv) = CvFLAGS(PL_compcv);
4350     if (!CvWEAKOUTSIDE(cv))
4351     SvREFCNT_dec(CvOUTSIDE(cv));
4352     CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
4353     CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv);
4354     CvOUTSIDE(PL_compcv) = 0;
4355     CvPADLIST(cv) = CvPADLIST(PL_compcv);
4356     CvPADLIST(PL_compcv) = 0;
4357     /* inner references to PL_compcv must be fixed up ... */
4358     pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv);
4359     /* ... before we throw it away */
4360     SvREFCNT_dec(PL_compcv);
4361     if (PERLDB_INTER)/* Advice debugger on the new sub. */
4362     ++PL_sub_generation;
4363     }
4364     else {
4365     cv = PL_compcv;
4366     if (name) {
4367     GvCV(gv) = cv;
4368     GvCVGEN(gv) = 0;
4369     PL_sub_generation++;
4370     }
4371     }
4372     CvGV(cv) = gv;
4373     CvFILE_set_from_cop(cv, PL_curcop);
4374     CvSTASH(cv) = PL_curstash;
4375     #ifdef USE_5005THREADS
4376     CvOWNER(cv) = 0;
4377     if (!CvMUTEXP(cv)) {
4378     New(666, CvMUTEXP(cv), 1, perl_mutex);
4379     MUTEX_INIT(CvMUTEXP(cv));
4380     }
4381     #endif /* USE_5005THREADS */
4382    
4383     if (ps)
4384     sv_setpv((SV*)cv, ps);
4385    
4386     if (PL_error_count) {
4387     op_free(block);
4388     block = Nullop;
4389     if (name) {
4390     char *s = strrchr(name, ':');
4391     s = s ? s+1 : name;
4392     if (strEQ(s, "BEGIN")) {
4393     char *not_safe =
4394     "BEGIN not safe after errors--compilation aborted";
4395     if (PL_in_eval & EVAL_KEEPERR)
4396     Perl_croak(aTHX_ not_safe);
4397     else {
4398     /* force display of errors found but not reported */
4399     sv_catpv(ERRSV, not_safe);
4400     Perl_croak(aTHX_ "%"SVf, ERRSV);
4401     }
4402     }
4403     }
4404     }
4405     if (!block)
4406     goto done;
4407    
4408     if (CvLVALUE(cv)) {
4409     CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0,
4410     mod(scalarseq(block), OP_LEAVESUBLV));
4411     }
4412     else {
4413     /* This makes sub {}; work as expected. */
4414     if (block->op_type == OP_STUB) {
4415     op_free(block);
4416     block = newSTATEOP(0, Nullch, 0);
4417     }
4418     CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4419     }
4420     CvROOT(cv)->op_private |= OPpREFCOUNTED;
4421     OpREFCNT_set(CvROOT(cv), 1);
4422     CvSTART(cv) = LINKLIST(CvROOT(cv));
4423     CvROOT(cv)->op_next = 0;
4424     CALL_PEEP(CvSTART(cv));
4425    
4426     /* now that optimizer has done its work, adjust pad values */
4427    
4428     pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
4429    
4430     if (CvCLONE(cv)) {
4431     assert(!CvCONST(cv));
4432     if (ps && !*ps && op_const_sv(block, cv))
4433     CvCONST_on(cv);
4434     }
4435    
4436     if (name || aname) {
4437     char *s;
4438     char *tname = (name ? name : aname);
4439    
4440     if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4441     SV *sv = NEWSV(0,0);
4442     SV *tmpstr = sv_newmortal();
4443     GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
4444     CV *pcv;
4445     HV *hv;
4446    
4447     Perl_sv_setpvf(aTHX_ sv, "%s:%ld-%ld",
4448     CopFILE(PL_curcop),
4449     (long)PL_subline, (long)CopLINE(PL_curcop));
4450     gv_efullname3(tmpstr, gv, Nullch);
4451     hv_store(GvHV(PL_DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
4452     hv = GvHVn(db_postponed);
4453     if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
4454     && (pcv = GvCV(db_postponed)))
4455     {
4456     dSP;
4457     PUSHMARK(SP);
4458     XPUSHs(tmpstr);
4459     PUTBACK;
4460     call_sv((SV*)pcv, G_DISCARD);
4461     }
4462     }
4463    
4464     if ((s = strrchr(tname,':')))
4465     s++;
4466     else
4467     s = tname;
4468    
4469     if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4470     goto done;
4471    
4472     if (strEQ(s, "BEGIN")) {
4473     I32 oldscope = PL_scopestack_ix;
4474     ENTER;
4475     SAVECOPFILE(&PL_compiling);
4476     SAVECOPLINE(&PL_compiling);
4477    
4478     if (!PL_beginav)
4479     PL_beginav = newAV();
4480     DEBUG_x( dump_sub(gv) );
4481     av_push(PL_beginav, (SV*)cv);
4482     GvCV(gv) = 0; /* cv has been hijacked */
4483     call_list(oldscope, PL_beginav);
4484    
4485     PL_curcop = &PL_compiling;
4486     PL_compiling.op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
4487     LEAVE;
4488     }
4489     else if (strEQ(s, "END") && !PL_error_count) {
4490     if (!PL_endav)
4491     PL_endav = newAV();
4492     DEBUG_x( dump_sub(gv) );
4493     av_unshift(PL_endav, 1);
4494     av_store(PL_endav, 0, (SV*)cv);
4495     GvCV(gv) = 0; /* cv has been hijacked */
4496     }
4497     else if (strEQ(s, "CHECK") && !PL_error_count) {
4498     if (!PL_checkav)
4499     PL_checkav = newAV();
4500     DEBUG_x( dump_sub(gv) );
4501     if (PL_main_start && ckWARN(WARN_VOID))
4502     Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run CHECK block");
4503     av_unshift(PL_checkav, 1);
4504     av_store(PL_checkav, 0, (SV*)cv);
4505     GvCV(gv) = 0; /* cv has been hijacked */
4506     }
4507     else if (strEQ(s, "INIT") && !PL_error_count) {
4508     if (!PL_initav)
4509     PL_initav = newAV();
4510     DEBUG_x( dump_sub(gv) );
4511     if (PL_main_start && ckWARN(WARN_VOID))
4512     Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run INIT block");
4513     av_push(PL_initav, (SV*)cv);
4514     GvCV(gv) = 0; /* cv has been hijacked */
4515     }
4516     }
4517    
4518     done:
4519     PL_copline = NOLINE;
4520     LEAVE_SCOPE(floor);
4521     return cv;
4522     }
4523    
4524     /* XXX unsafe for threads if eval_owner isn't held */
4525     /*
4526     =for apidoc newCONSTSUB
4527    
4528     Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is
4529     eligible for inlining at compile-time.
4530    
4531     =cut
4532     */
4533    
4534     CV *
4535     Perl_newCONSTSUB(pTHX_ HV *stash, char *name, SV *sv)
4536     {
4537     CV* cv;
4538    
4539     ENTER;
4540    
4541     SAVECOPLINE(PL_curcop);
4542     CopLINE_set(PL_curcop, PL_copline);
4543    
4544     SAVEHINTS();
4545     PL_hints &= ~HINT_BLOCK_SCOPE;
4546    
4547     if (stash) {
4548     SAVESPTR(PL_curstash);
4549     SAVECOPSTASH(PL_curcop);
4550     PL_curstash = stash;
4551     CopSTASH_set(PL_curcop,stash);
4552     }
4553    
4554     cv = newXS(name, const_sv_xsub, savepv(CopFILE(PL_curcop)));
4555     CvXSUBANY(cv).any_ptr = sv;
4556     CvCONST_on(cv);
4557     sv_setpv((SV*)cv, ""); /* prototype is "" */
4558    
4559     if (stash)
4560     CopSTASH_free(PL_curcop);
4561    
4562     LEAVE;
4563    
4564     return cv;
4565     }
4566    
4567     /*
4568     =for apidoc U||newXS
4569    
4570     Used by C<xsubpp> to hook up XSUBs as Perl subs.
4571    
4572     =cut
4573     */
4574    
4575     CV *
4576     Perl_newXS(pTHX_ char *name, XSUBADDR_t subaddr, char *filename)
4577     {
4578     GV *gv = gv_fetchpv(name ? name :
4579     (PL_curstash ? "__ANON__" : "__ANON__::__ANON__"),
4580     GV_ADDMULTI, SVt_PVCV);
4581     register CV *cv;
4582    
4583     if ((cv = (name ? GvCV(gv) : Nullcv))) {
4584     if (GvCVGEN(gv)) {
4585     /* just a cached method */
4586     SvREFCNT_dec(cv);
4587     cv = 0;
4588     }
4589     else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4590     /* already defined (or promised) */
4591     if (ckWARN(WARN_REDEFINE) && !(CvGV(cv) && GvSTASH(CvGV(cv))
4592     && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
4593     line_t oldline = CopLINE(PL_curcop);
4594     if (PL_copline != NOLINE)
4595     CopLINE_set(PL_curcop, PL_copline);
4596     Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
4597     CvCONST(cv) ? "Constant subroutine %s redefined"
4598     : "Subroutine %s redefined"
4599     ,name);
4600     CopLINE_set(PL_curcop, oldline);
4601     }
4602     SvREFCNT_dec(cv);
4603     cv = 0;
4604     }
4605     }
4606    
4607     if (cv) /* must reuse cv if autoloaded */
4608     cv_undef(cv);
4609     else {
4610     cv = (CV*)NEWSV(1105,0);
4611     sv_upgrade((SV *)cv, SVt_PVCV);
4612     if (name) {
4613     GvCV(gv) = cv;
4614     GvCVGEN(gv) = 0;
4615     PL_sub_generation++;
4616     }
4617     }
4618     CvGV(cv) = gv;
4619     #ifdef USE_5005THREADS
4620     New(666, CvMUTEXP(cv), 1, perl_mutex);
4621     MUTEX_INIT(CvMUTEXP(cv));
4622     CvOWNER(cv) = 0;
4623     #endif /* USE_5005THREADS */
4624     (void)gv_fetchfile(filename);
4625     CvFILE(cv) = filename; /* NOTE: not copied, as it is expected to be
4626     an external constant string */
4627     CvXSUB(cv) = subaddr;
4628    
4629     if (name) {
4630     char *s = strrchr(name,':');
4631     if (s)
4632     s++;
4633     else
4634     s = name;
4635    
4636     if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4637     goto done;
4638    
4639     if (strEQ(s, "BEGIN")) {
4640     if (!PL_beginav)
4641     PL_beginav = newAV();
4642     av_push(PL_beginav, (SV*)cv);
4643     GvCV(gv) = 0; /* cv has been hijacked */
4644     }
4645     else if (strEQ(s, "END")) {
4646     if (!PL_endav)
4647     PL_endav = newAV();
4648     av_unshift(PL_endav, 1);
4649     av_store(PL_endav, 0, (SV*)cv);
4650     GvCV(gv) = 0; /* cv has been hijacked */
4651     }
4652     else if (strEQ(s, "CHECK")) {
4653     if (!PL_checkav)
4654     PL_checkav = newAV();
4655     if (PL_main_start && ckWARN(WARN_VOID))
4656     Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run CHECK block");
4657     av_unshift(PL_checkav, 1);
4658     av_store(PL_checkav, 0, (SV*)cv);
4659     GvCV(gv) = 0; /* cv has been hijacked */
4660     }
4661     else if (strEQ(s, "INIT")) {
4662     if (!PL_initav)
4663     PL_initav = newAV();
4664     if (PL_main_start && ckWARN(WARN_VOID))
4665     Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run INIT block");
4666     av_push(PL_initav, (SV*)cv);
4667     GvCV(gv) = 0; /* cv has been hijacked */
4668     }
4669     }
4670     else
4671     CvANON_on(cv);
4672    
4673     done:
4674     return cv;
4675     }
4676    
4677     void
4678     Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
4679     {
4680     register CV *cv;
4681     char *name;
4682     GV *gv;
4683     STRLEN n_a;
4684    
4685     if (o)
4686     name = SvPVx(cSVOPo->op_sv, n_a);
4687     else
4688     name = "STDOUT";
4689     gv = gv_fetchpv(name,TRUE, SVt_PVFM);
4690     #ifdef GV_UNIQUE_CHECK
4691     if (GvUNIQUE(gv)) {
4692     Perl_croak(aTHX_ "Bad symbol for form (GV is unique)");
4693     }
4694     #endif
4695     GvMULTI_on(gv);
4696     if ((cv = GvFORM(gv))) {
4697     if (ckWARN(WARN_REDEFINE)) {
4698     line_t oldline = CopLINE(PL_curcop);
4699     if (PL_copline != NOLINE)
4700     CopLINE_set(PL_curcop, PL_copline);
4701     Perl_warner(aTHX_ packWARN(WARN_REDEFINE), "Format %s redefined",name);
4702     CopLINE_set(PL_curcop, oldline);
4703     }
4704     SvREFCNT_dec(cv);
4705     }
4706     cv = PL_compcv;
4707     GvFORM(gv) = cv;
4708     CvGV(cv) = gv;
4709     CvFILE_set_from_cop(cv, PL_curcop);
4710    
4711    
4712     pad_tidy(padtidy_FORMAT);
4713     CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4714     CvROOT(cv)->op_private |= OPpREFCOUNTED;
4715     OpREFCNT_set(CvROOT(cv), 1);
4716     CvSTART(cv) = LINKLIST(CvROOT(cv));
4717     CvROOT(cv)->op_next = 0;
4718     CALL_PEEP(CvSTART(cv));
4719     op_free(o);
4720     PL_copline = NOLINE;
4721     LEAVE_SCOPE(floor);
4722     }
4723    
4724     OP *
4725     Perl_newANONLIST(pTHX_ OP *o)
4726     {
4727     return newUNOP(OP_REFGEN, 0,
4728     mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
4729     }
4730    
4731     OP *
4732     Perl_newANONHASH(pTHX_ OP *o)
4733     {
4734     return newUNOP(OP_REFGEN, 0,
4735     mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
4736     }
4737    
4738     OP *
4739     Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
4740     {
4741     return newANONATTRSUB(floor, proto, Nullop, block);
4742     }
4743    
4744     OP *
4745     Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
4746     {
4747     return newUNOP(OP_REFGEN, 0,
4748     newSVOP(OP_ANONCODE, 0,
4749     (SV*)newATTRSUB(floor, 0, proto, attrs, block)));
4750     }
4751    
4752     OP *
4753     Perl_oopsAV(pTHX_ OP *o)
4754     {
4755     switch (o->op_type) {
4756     case OP_PADSV:
4757     o->op_type = OP_PADAV;
4758     o->op_ppaddr = PL_ppaddr[OP_PADAV];
4759     return ref(o, OP_RV2AV);
4760    
4761     case OP_RV2SV:
4762     o->op_type = OP_RV2AV;
4763     o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4764     ref(o, OP_RV2AV);
4765     break;
4766    
4767     default:
4768     if (ckWARN_d(WARN_INTERNAL))
4769     Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV");
4770     break;
4771     }
4772     return o;
4773     }
4774    
4775     OP *
4776     Perl_oopsHV(pTHX_ OP *o)
4777     {
4778     switch (o->op_type) {
4779     case OP_PADSV:
4780     case OP_PADAV:
4781     o->op_type = OP_PADHV;
4782     o->op_ppaddr = PL_ppaddr[OP_PADHV];
4783     return ref(o, OP_RV2HV);
4784    
4785     case OP_RV2SV:
4786     case OP_RV2AV:
4787     o->op_type = OP_RV2HV;
4788     o->op_ppaddr = PL_ppaddr[OP_RV2HV];
4789     ref(o, OP_RV2HV);
4790     break;
4791    
4792     default:
4793     if (ckWARN_d(WARN_INTERNAL))
4794     Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV");
4795     break;
4796     }
4797     return o;
4798     }
4799    
4800     OP *
4801     Perl_newAVREF(pTHX_ OP *o)
4802     {
4803     if (o->op_type == OP_PADANY) {
4804     o->op_type = OP_PADAV;
4805     o->op_ppaddr = PL_ppaddr[OP_PADAV];
4806     return o;
4807     }
4808     else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)
4809     && ckWARN(WARN_DEPRECATED)) {
4810     Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
4811     "Using an array as a reference is deprecated");
4812     }
4813     return newUNOP(OP_RV2AV, 0, scalar(o));
4814     }
4815    
4816     OP *
4817     Perl_newGVREF(pTHX_ I32 type, OP *o)
4818     {
4819     if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
4820     return newUNOP(OP_NULL, 0, o);
4821     return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
4822     }
4823    
4824     OP *
4825     Perl_newHVREF(pTHX_ OP *o)
4826     {
4827     if (o->op_type == OP_PADANY) {
4828     o->op_type = OP_PADHV;
4829     o->op_ppaddr = PL_ppaddr[OP_PADHV];
4830     return o;
4831     }
4832     else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)
4833     && ckWARN(WARN_DEPRECATED)) {
4834     Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
4835     "Using a hash as a reference is deprecated");
4836     }
4837     return newUNOP(OP_RV2HV, 0, scalar(o));
4838     }
4839    
4840     OP *
4841     Perl_oopsCV(pTHX_ OP *o)
4842     {
4843     Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
4844     /* STUB */
4845     return o;
4846     }
4847    
4848     OP *
4849     Perl_newCVREF(pTHX_ I32 flags, OP *o)
4850     {
4851     return newUNOP(OP_RV2CV, flags, scalar(o));
4852     }
4853    
4854     OP *
4855     Perl_newSVREF(pTHX_ OP *o)
4856     {
4857     if (o->op_type == OP_PADANY) {
4858     o->op_type = OP_PADSV;
4859     o->op_ppaddr = PL_ppaddr[OP_PADSV];
4860     return o;
4861     }
4862     else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
4863     o->op_flags |= OPpDONE_SVREF;
4864     return o;
4865     }
4866     return newUNOP(OP_RV2SV, 0, scalar(o));
4867     }
4868    
4869     /* Check routines. See the comments at the top of this file for details
4870     * on when these are called */
4871    
4872     OP *
4873     Perl_ck_anoncode(pTHX_ OP *o)
4874     {
4875     cSVOPo->op_targ = pad_add_anon(cSVOPo->op_sv, o->op_type);
4876     cSVOPo->op_sv = Nullsv;
4877     return o;
4878     }
4879    
4880     OP *
4881     Perl_ck_bitop(pTHX_ OP *o)
4882     {
4883     #define OP_IS_NUMCOMPARE(op) \
4884     ((op) == OP_LT || (op) == OP_I_LT || \
4885     (op) == OP_GT || (op) == OP_I_GT || \
4886     (op) == OP_LE || (op) == OP_I_LE || \
4887     (op) == OP_GE || (op) == OP_I_GE || \
4888     (op) == OP_EQ || (op) == OP_I_EQ || \
4889     (op) == OP_NE || (op) == OP_I_NE || \
4890     (op) == OP_NCMP || (op) == OP_I_NCMP)
4891     o->op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
4892     if (!(o->op_flags & OPf_STACKED) /* Not an assignment */
4893     && (o->op_type == OP_BIT_OR
4894     || o->op_type == OP_BIT_AND
4895     || o->op_type == OP_BIT_XOR))
4896     {
4897     OP * left = cBINOPo->op_first;
4898     OP * right = left->op_sibling;
4899     if ((OP_IS_NUMCOMPARE(left->op_type) &&
4900     (left->op_flags & OPf_PARENS) == 0) ||
4901     (OP_IS_NUMCOMPARE(right->op_type) &&
4902     (right->op_flags & OPf_PARENS) == 0))
4903     if (ckWARN(WARN_PRECEDENCE))
4904     Perl_warner(aTHX_ packWARN(WARN_PRECEDENCE),
4905     "Possible precedence problem on bitwise %c operator",
4906     o->op_type == OP_BIT_OR ? '|'
4907     : o->op_type == OP_BIT_AND ? '&' : '^'
4908     );
4909     }
4910     return o;
4911     }
4912    
4913     OP *
4914     Perl_ck_concat(pTHX_ OP *o)
4915     {
4916     OP *kid = cUNOPo->op_first;
4917     if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) &&
4918     !(kUNOP->op_first->op_flags & OPf_MOD))
4919     o->op_flags |= OPf_STACKED;
4920     return o;
4921     }
4922    
4923     OP *
4924     Perl_ck_spair(pTHX_ OP *o)
4925     {
4926     if (o->op_flags & OPf_KIDS) {
4927     OP* newop;
4928     OP* kid;
4929     OPCODE type = o->op_type;
4930     o = modkids(ck_fun(o), type);
4931     kid = cUNOPo->op_first;
4932     newop = kUNOP->op_first->op_sibling;
4933     if (newop &&
4934     (newop->op_sibling ||
4935     !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
4936     newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
4937     newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
4938    
4939     return o;
4940     }
4941     op_free(kUNOP->op_first);
4942     kUNOP->op_first = newop;
4943     }
4944     o->op_ppaddr = PL_ppaddr[++o->op_type];
4945     return ck_fun(o);
4946     }
4947    
4948     OP *
4949     Perl_ck_delete(pTHX_ OP *o)
4950     {
4951     o = ck_fun(o);
4952     o->op_private = 0;
4953     if (o->op_flags & OPf_KIDS) {
4954     OP *kid = cUNOPo->op_first;
4955     switch (kid->op_type) {
4956     case OP_ASLICE:
4957     o->op_flags |= OPf_SPECIAL;
4958     /* FALL THROUGH */
4959     case OP_HSLICE:
4960     o->op_private |= OPpSLICE;
4961     break;
4962     case OP_AELEM:
4963     o->op_flags |= OPf_SPECIAL;
4964     /* FALL THROUGH */
4965     case OP_HELEM:
4966     break;
4967     default:
4968     Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element or slice",
4969     OP_DESC(o));
4970     }
4971     op_null(kid);
4972     }
4973     return o;
4974     }
4975    
4976     OP *
4977     Perl_ck_die(pTHX_ OP *o)
4978     {
4979     #ifdef VMS
4980     if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH;
4981     #endif
4982     return ck_fun(o);
4983     }
4984    
4985     OP *
4986     Perl_ck_eof(pTHX_ OP *o)
4987     {
4988     I32 type = o->op_type;
4989    
4990     if (o->op_flags & OPf_KIDS) {
4991     if (cLISTOPo->op_first->op_type == OP_STUB) {
4992     op_free(o);
4993     o = newUNOP(type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv));
4994     }
4995     return ck_fun(o);
4996     }
4997     return o;
4998     }
4999    
5000     OP *
5001     Perl_ck_eval(pTHX_ OP *o)
5002     {
5003     PL_hints |= HINT_BLOCK_SCOPE;
5004     if (o->op_flags & OPf_KIDS) {
5005     SVOP *kid = (SVOP*)cUNOPo->op_first;
5006    
5007     if (!kid) {
5008     o->op_flags &= ~OPf_KIDS;
5009     op_null(o);
5010     }
5011     else if (kid->op_type == OP_LINESEQ || kid->op_type == OP_STUB) {
5012     LOGOP *enter;
5013    
5014     cUNOPo->op_first = 0;
5015     op_free(o);
5016    
5017     NewOp(1101, enter, 1, LOGOP);
5018     enter->op_type = OP_ENTERTRY;
5019     enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
5020     enter->op_private = 0;
5021    
5022     /* establish postfix order */
5023     enter->op_next = (OP*)enter;
5024    
5025     o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
5026     o->op_type = OP_LEAVETRY;
5027     o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
5028     enter->op_other = o;
5029     return o;
5030     }
5031     else
5032     scalar((OP*)kid);
5033     }
5034     else {
5035     op_free(o);
5036     o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
5037     }
5038     o->op_targ = (PADOFFSET)PL_hints;
5039     return o;
5040     }
5041    
5042     OP *
5043     Perl_ck_exit(pTHX_ OP *o)
5044     {
5045     #ifdef VMS
5046     HV *table = GvHV(PL_hintgv);
5047     if (table) {
5048     SV **svp = hv_fetch(table, "vmsish_exit", 11, FALSE);
5049     if (svp && *svp && SvTRUE(*svp))
5050     o->op_private |= OPpEXIT_VMSISH;
5051     }
5052     if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH;
5053     #endif
5054     return ck_fun(o);
5055     }
5056    
5057     OP *
5058     Perl_ck_exec(pTHX_ OP *o)
5059     {
5060     OP *kid;
5061     if (o->op_flags & OPf_STACKED) {
5062     o = ck_fun(o);
5063     kid = cUNOPo->op_first->op_sibling;
5064     if (kid->op_type == OP_RV2GV)
5065     op_null(kid);
5066     }
5067     else
5068     o = listkids(o);
5069     return o;
5070     }
5071    
5072     OP *
5073     Perl_ck_exists(pTHX_ OP *o)
5074     {
5075     o = ck_fun(o);
5076     if (o->op_flags & OPf_KIDS) {
5077     OP *kid = cUNOPo->op_first;
5078     if (kid->op_type == OP_ENTERSUB) {
5079     (void) ref(kid, o->op_type);
5080     if (kid->op_type != OP_RV2CV && !PL_error_count)
5081     Perl_croak(aTHX_ "%s argument is not a subroutine name",
5082     OP_DESC(o));
5083     o->op_private |= OPpEXISTS_SUB;
5084     }
5085     else if (kid->op_type == OP_AELEM)
5086     o->op_flags |= OPf_SPECIAL;
5087     else if (kid->op_type != OP_HELEM)
5088     Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element",
5089     OP_DESC(o));
5090     op_null(kid);
5091     }
5092     return o;
5093     }
5094    
5095     #if 0
5096     OP *
5097     Perl_ck_gvconst(pTHX_ register OP *o)
5098     {
5099     o = fold_constants(o);
5100     if (o->op_type == OP_CONST)
5101     o->op_type = OP_GV;
5102     return o;
5103     }
5104     #endif
5105    
5106     OP *
5107     Perl_ck_rvconst(pTHX_ register OP *o)
5108     {
5109     SVOP *kid = (SVOP*)cUNOPo->op_first;
5110    
5111     o->op_private |= (PL_hints & HINT_STRICT_REFS);
5112     if (kid->op_type == OP_CONST) {
5113     char *name;
5114     int iscv;
5115     GV *gv;
5116     SV *kidsv = kid->op_sv;
5117     STRLEN n_a;
5118    
5119     /* Is it a constant from cv_const_sv()? */
5120     if (SvROK(kidsv) && SvREADONLY(kidsv)) {
5121     SV *rsv = SvRV(kidsv);
5122     int svtype = SvTYPE(rsv);
5123     char *badtype = Nullch;
5124    
5125     switch (o->op_type) {
5126     case OP_RV2SV:
5127     if (svtype > SVt_PVMG)
5128     badtype = "a SCALAR";
5129     break;
5130     case OP_RV2AV:
5131     if (svtype != SVt_PVAV)
5132     badtype = "an ARRAY";
5133     break;
5134     case OP_RV2HV:
5135     if (svtype != SVt_PVHV) {
5136     if (svtype == SVt_PVAV) { /* pseudohash? */
5137     SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
5138     if (ksv && SvROK(*ksv)
5139     && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
5140     {
5141     break;
5142     }
5143     }
5144     badtype = "a HASH";
5145     }
5146     break;
5147     case OP_RV2CV:
5148     if (svtype != SVt_PVCV)
5149     badtype = "a CODE";
5150     break;
5151     }
5152     if (badtype)
5153     Perl_croak(aTHX_ "Constant is not %s reference", badtype);
5154     return o;
5155     }
5156     name = SvPV(kidsv, n_a);
5157     if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
5158     char *badthing = Nullch;
5159     switch (o->op_type) {
5160     case OP_RV2SV:
5161     badthing = "a SCALAR";
5162     break;
5163     case OP_RV2AV:
5164     badthing = "an ARRAY";
5165     break;
5166     case OP_RV2HV:
5167     badthing = "a HASH";
5168     break;
5169     }
5170     if (badthing)
5171     Perl_croak(aTHX_
5172     "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
5173     name, badthing);
5174     }
5175     /*
5176     * This is a little tricky. We only want to add the symbol if we
5177     * didn't add it in the lexer. Otherwise we get duplicate strict
5178     * warnings. But if we didn't add it in the lexer, we must at
5179     * least pretend like we wanted to add it even if it existed before,
5180     * or we get possible typo warnings. OPpCONST_ENTERED says
5181     * whether the lexer already added THIS instance of this symbol.
5182     */
5183     iscv = (o->op_type == OP_RV2CV) * 2;
5184     do {
5185     gv = gv_fetchpv(name,
5186     iscv | !(kid->op_private & OPpCONST_ENTERED),
5187     iscv
5188     ? SVt_PVCV
5189     : o->op_type == OP_RV2SV
5190     ? SVt_PV
5191     : o->op_type == OP_RV2AV
5192     ? SVt_PVAV
5193     : o->op_type == OP_RV2HV
5194     ? SVt_PVHV
5195     : SVt_PVGV);
5196     } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
5197     if (gv) {
5198     kid->op_type = OP_GV;
5199     SvREFCNT_dec(kid->op_sv);
5200     #ifdef USE_ITHREADS
5201     /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
5202     kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
5203     SvREFCNT_dec(PAD_SVl(kPADOP->op_padix));
5204     GvIN_PAD_on(gv);
5205     PAD_SETSV(kPADOP->op_padix, (SV*) SvREFCNT_inc(gv));
5206     #else
5207     kid->op_sv = SvREFCNT_inc(gv);
5208     #endif
5209     kid->op_private = 0;
5210     kid->op_ppaddr = PL_ppaddr[OP_GV];
5211     }
5212     }
5213     return o;
5214     }
5215    
5216     OP *
5217     Perl_ck_ftst(pTHX_ OP *o)
5218     {
5219     I32 type = o->op_type;
5220    
5221     if (o->op_flags & OPf_REF) {
5222     /* nothing */
5223     }
5224     else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
5225     SVOP *kid = (SVOP*)cUNOPo->op_first;
5226    
5227     if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5228     STRLEN n_a;
5229     OP *newop = newGVOP(type, OPf_REF,
5230     gv_fetchpv(SvPVx(kid->op_sv, n_a), TRUE, SVt_PVIO));
5231     op_free(o);
5232     o = newop;
5233     }
5234     else {
5235     if ((PL_hints & HINT_FILETEST_ACCESS) &&
5236     OP_IS_FILETEST_ACCESS(o))
5237     o->op_private |= OPpFT_ACCESS;
5238     }
5239     }
5240     else {
5241     op_free(o);
5242     if (type == OP_FTTTY)
5243     o = newGVOP(type, OPf_REF, PL_stdingv);
5244     else
5245     o = newUNOP(type, 0, newDEFSVOP());
5246     }
5247     return o;
5248     }
5249    
5250     OP *
5251     Perl_ck_fun(pTHX_ OP *o)
5252     {
5253     register OP *kid;
5254     OP **tokid;
5255     OP *sibl;
5256     I32 numargs = 0;
5257     int type = o->op_type;
5258     register I32 oa = PL_opargs[type] >> OASHIFT;
5259    
5260     if (o->op_flags & OPf_STACKED) {
5261     if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
5262     oa &= ~OA_OPTIONAL;
5263     else
5264     return no_fh_allowed(o);
5265     }
5266    
5267     if (o->op_flags & OPf_KIDS) {
5268     STRLEN n_a;
5269     tokid = &cLISTOPo->op_first;
5270     kid = cLISTOPo->op_first;
5271     if (kid->op_type == OP_PUSHMARK ||
5272     (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
5273     {
5274     tokid = &kid->op_sibling;
5275     kid = kid->op_sibling;
5276     }
5277     if (!kid && PL_opargs[type] & OA_DEFGV)
5278     *tokid = kid = newDEFSVOP();
5279    
5280     while (oa && kid) {
5281     numargs++;
5282     sibl = kid->op_sibling;
5283     switch (oa & 7) {
5284     case OA_SCALAR:
5285     /* list seen where single (scalar) arg expected? */
5286     if (numargs == 1 && !(oa >> 4)
5287     && kid->op_type == OP_LIST && type != OP_SCALAR)
5288     {
5289     return too_many_arguments(o,PL_op_desc[type]);
5290     }
5291     scalar(kid);
5292     break;
5293     case OA_LIST:
5294     if (oa < 16) {
5295     kid = 0;
5296     continue;
5297     }
5298     else
5299     list(kid);
5300     break;
5301     case OA_AVREF:
5302     if ((type == OP_PUSH || type == OP_UNSHIFT)
5303     && !kid->op_sibling && ckWARN(WARN_SYNTAX))
5304     Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
5305     "Useless use of %s with no values",
5306     PL_op_desc[type]);
5307    
5308     if (kid->op_type == OP_CONST &&
5309     (kid->op_private & OPpCONST_BARE))
5310     {
5311     char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5312     OP *newop = newAVREF(newGVOP(OP_GV, 0,
5313     gv_fetchpv(name, TRUE, SVt_PVAV) ));
5314     if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX))
5315     Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5316     "Array @%s missing the @ in argument %"IVdf" of %s()",
5317     name, (IV)numargs, PL_op_desc[type]);
5318     op_free(kid);
5319     kid = newop;
5320     kid->op_sibling = sibl;
5321     *tokid = kid;
5322     }
5323     else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
5324     bad_type(numargs, "array", PL_op_desc[type], kid);
5325     mod(kid, type);
5326     break;
5327     case OA_HVREF:
5328     if (kid->op_type == OP_CONST &&
5329     (kid->op_private & OPpCONST_BARE))
5330     {
5331     char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5332     OP *newop = newHVREF(newGVOP(OP_GV, 0,
5333     gv_fetchpv(name, TRUE, SVt_PVHV) ));
5334     if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX))
5335     Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5336     "Hash %%%s missing the %% in argument %"IVdf" of %s()",
5337     name, (IV)numargs, PL_op_desc[type]);
5338     op_free(kid);
5339     kid = newop;
5340     kid->op_sibling = sibl;
5341     *tokid = kid;
5342     }
5343     else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
5344     bad_type(numargs, "hash", PL_op_desc[type], kid);
5345     mod(kid, type);
5346     break;
5347     case OA_CVREF:
5348     {
5349     OP *newop = newUNOP(OP_NULL, 0, kid);
5350     kid->op_sibling = 0;
5351     linklist(kid);
5352     newop->op_next = newop;
5353     kid = newop;
5354     kid->op_sibling = sibl;
5355     *tokid = kid;
5356     }
5357     break;
5358     case OA_FILEREF:
5359     if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
5360     if (kid->op_type == OP_CONST &&
5361     (kid->op_private & OPpCONST_BARE))
5362     {
5363     OP *newop = newGVOP(OP_GV, 0,
5364     gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, n_a), TRUE,
5365     SVt_PVIO) );
5366     if (!(o->op_private & 1) && /* if not unop */
5367     kid == cLISTOPo->op_last)
5368     cLISTOPo->op_last = newop;
5369     op_free(kid);
5370     kid = newop;
5371     }
5372     else if (kid->op_type == OP_READLINE) {
5373     /* neophyte patrol: open(<FH>), close(<FH>) etc. */
5374     bad_type(numargs, "HANDLE", OP_DESC(o), kid);
5375     }
5376     else {
5377     I32 flags = OPf_SPECIAL;
5378     I32 priv = 0;
5379     PADOFFSET targ = 0;
5380    
5381     /* is this op a FH constructor? */
5382     if (is_handle_constructor(o,numargs)) {
5383     char *name = Nullch;
5384     STRLEN len = 0;
5385    
5386     flags = 0;
5387     /* Set a flag to tell rv2gv to vivify
5388     * need to "prove" flag does not mean something
5389     * else already - NI-S 1999/05/07
5390     */
5391     priv = OPpDEREF;
5392     if (kid->op_type == OP_PADSV) {
5393     /*XXX DAPM 2002.08.25 tmp assert test */
5394     /*XXX*/ assert(av_fetch(PL_comppad_name, (kid->op_targ), FALSE));
5395     /*XXX*/ assert(*av_fetch(PL_comppad_name, (kid->op_targ), FALSE));
5396    
5397     name = PAD_COMPNAME_PV(kid->op_targ);
5398     /* SvCUR of a pad namesv can't be trusted
5399     * (see PL_generation), so calc its length
5400     * manually */
5401     if (name)
5402     len = strlen(name);
5403    
5404     }
5405     else if (kid->op_type == OP_RV2SV
5406     && kUNOP->op_first->op_type == OP_GV)
5407     {
5408     GV *gv = cGVOPx_gv(kUNOP->op_first);
5409     name = GvNAME(gv);
5410     len = GvNAMELEN(gv);
5411     }
5412     else if (kid->op_type == OP_AELEM
5413     || kid->op_type == OP_HELEM)
5414     {
5415     OP *op;
5416    
5417     name = 0;
5418     if ((op = ((BINOP*)kid)->op_first)) {
5419     SV *tmpstr = Nullsv;
5420     char *a =
5421     kid->op_type == OP_AELEM ?
5422     "[]" : "{}";
5423     if (((op->op_type == OP_RV2AV) ||
5424     (op->op_type == OP_RV2HV)) &&
5425     (op = ((UNOP*)op)->op_first) &&
5426     (op->op_type == OP_GV)) {
5427     /* packagevar $a[] or $h{} */
5428     GV *gv = cGVOPx_gv(op);
5429     if (gv)
5430     tmpstr =
5431     Perl_newSVpvf(aTHX_
5432     "%s%c...%c",
5433     GvNAME(gv),
5434     a[0], a[1]);
5435     }
5436     else if (op->op_type == OP_PADAV
5437     || op->op_type == OP_PADHV) {
5438     /* lexicalvar $a[] or $h{} */
5439     char *padname =
5440     PAD_COMPNAME_PV(op->op_targ);
5441     if (padname)
5442     tmpstr =
5443     Perl_newSVpvf(aTHX_
5444     "%s%c...%c",
5445     padname + 1,
5446     a[0], a[1]);
5447    
5448     }
5449     if (tmpstr) {
5450     name = SvPV(tmpstr, len);
5451     sv_2mortal(tmpstr);
5452     }
5453     }
5454     if (!name) {
5455     name = "__ANONIO__";
5456     len = 10;
5457     }
5458     mod(kid, type);
5459     }
5460     if (name) {
5461     SV *namesv;
5462     targ = pad_alloc(OP_RV2GV, SVs_PADTMP);
5463     namesv = PAD_SVl(targ);
5464     (void)SvUPGRADE(namesv, SVt_PV);
5465     if (*name != '$')
5466     sv_setpvn(namesv, "$", 1);
5467     sv_catpvn(namesv, name, len);
5468     }
5469     }
5470     kid->op_sibling = 0;
5471     kid = newUNOP(OP_RV2GV, flags, scalar(kid));
5472     kid->op_targ = targ;
5473     kid->op_private |= priv;
5474     }
5475     kid->op_sibling = sibl;
5476     *tokid = kid;
5477     }
5478     scalar(kid);
5479     break;
5480     case OA_SCALARREF:
5481     mod(scalar(kid), type);
5482     break;
5483     }
5484     oa >>= 4;
5485     tokid = &kid->op_sibling;
5486     kid = kid->op_sibling;
5487     }
5488     o->op_private |= numargs;
5489     if (kid)
5490     return too_many_arguments(o,OP_DESC(o));
5491     listkids(o);
5492     }
5493     else if (PL_opargs[type] & OA_DEFGV) {
5494     op_free(o);
5495     return newUNOP(type, 0, newDEFSVOP());
5496     }
5497    
5498     if (oa) {
5499     while (oa & OA_OPTIONAL)
5500     oa >>= 4;
5501     if (oa && oa != OA_LIST)
5502     return too_few_arguments(o,OP_DESC(o));
5503     }
5504     return o;
5505     }
5506    
5507     OP *
5508     Perl_ck_glob(pTHX_ OP *o)
5509     {
5510     GV *gv;
5511    
5512     o = ck_fun(o);
5513     if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
5514     append_elem(OP_GLOB, o, newDEFSVOP());
5515    
5516     if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV))
5517     && GvCVu(gv) && GvIMPORTED_CV(gv)))
5518     {
5519     gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5520     }
5521    
5522     #if !defined(PERL_EXTERNAL_GLOB)
5523     /* XXX this can be tightened up and made more failsafe. */
5524     if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
5525     GV *glob_gv;
5526     ENTER;
5527     Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
5528     newSVpvn("File::Glob", 10), Nullsv, Nullsv, Nullsv);
5529     gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5530     glob_gv = gv_fetchpv("File::Glob::csh_glob", FALSE, SVt_PVCV);
5531     GvCV(gv) = GvCV(glob_gv);
5532     SvREFCNT_inc((SV*)GvCV(gv));
5533     GvIMPORTED_CV_on(gv);
5534     LEAVE;
5535     }
5536     #endif /* PERL_EXTERNAL_GLOB */
5537    
5538     if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
5539     append_elem(OP_GLOB, o,
5540     newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
5541     o->op_type = OP_LIST;
5542     o->op_ppaddr = PL_ppaddr[OP_LIST];
5543     cLISTOPo->op_first->op_type = OP_PUSHMARK;
5544     cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
5545     cLISTOPo->op_first->op_targ = 0;
5546     o = newUNOP(OP_ENTERSUB, OPf_STACKED,
5547     append_elem(OP_LIST, o,
5548     scalar(newUNOP(OP_RV2CV, 0,
5549     newGVOP(OP_GV, 0, gv)))));
5550     o = newUNOP(OP_NULL, 0, ck_subr(o));
5551     o->op_targ = OP_GLOB; /* hint at what it used to be */
5552     return o;
5553     }
5554     gv = newGVgen("main");
5555     gv_IOadd(gv);
5556     append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
5557     scalarkids(o);
5558     return o;
5559     }
5560    
5561     OP *
5562     Perl_ck_grep(pTHX_ OP *o)
5563     {
5564     LOGOP *gwop;
5565     OP *kid;
5566     OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
5567    
5568     o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
5569     NewOp(1101, gwop, 1, LOGOP);
5570    
5571     if (o->op_flags & OPf_STACKED) {
5572     OP* k;
5573     o = ck_sort(o);
5574     kid = cLISTOPo->op_first->op_sibling;
5575     if (!cUNOPx(kid)->op_next)
5576     Perl_croak(aTHX_ "panic: ck_grep");
5577     for (k = cUNOPx(kid)->op_first; k; k = k->op_next) {
5578     kid = k;
5579     }
5580     kid->op_next = (OP*)gwop;
5581     o->op_flags &= ~OPf_STACKED;
5582     }
5583     kid = cLISTOPo->op_first->op_sibling;
5584     if (type == OP_MAPWHILE)
5585     list(kid);
5586     else
5587     scalar(kid);
5588     o = ck_fun(o);
5589     if (PL_error_count)
5590     return o;
5591     kid = cLISTOPo->op_first->op_sibling;
5592     if (kid->op_type != OP_NULL)
5593     Perl_croak(aTHX_ "panic: ck_grep");
5594     kid = kUNOP->op_first;
5595    
5596     gwop->op_type = type;
5597     gwop->op_ppaddr = PL_ppaddr[type];
5598     gwop->op_first = listkids(o);
5599     gwop->op_flags |= OPf_KIDS;
5600     gwop->op_private = 1;
5601     gwop->op_other = LINKLIST(kid);
5602     gwop->op_targ = pad_alloc(type, SVs_PADTMP);
5603     kid->op_next = (OP*)gwop;
5604    
5605     kid = cLISTOPo->op_first->op_sibling;
5606     if (!kid || !kid->op_sibling)
5607     return too_few_arguments(o,OP_DESC(o));
5608     for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
5609     mod(kid, OP_GREPSTART);
5610    
5611     return (OP*)gwop;
5612     }
5613    
5614     OP *
5615     Perl_ck_index(pTHX_ OP *o)
5616     {
5617     if (o->op_flags & OPf_KIDS) {
5618     OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5619     if (kid)
5620     kid = kid->op_sibling; /* get past "big" */
5621     if (kid && kid->op_type == OP_CONST)
5622     fbm_compile(((SVOP*)kid)->op_sv, 0);
5623     }
5624     return ck_fun(o);
5625     }
5626    
5627     OP *
5628     Perl_ck_lengthconst(pTHX_ OP *o)
5629     {
5630     /* XXX length optimization goes here */
5631     return ck_fun(o);
5632     }
5633    
5634     OP *
5635     Perl_ck_lfun(pTHX_ OP *o)
5636     {
5637     OPCODE type = o->op_type;
5638     return modkids(ck_fun(o), type);
5639     }
5640    
5641     OP *
5642     Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
5643     {
5644     if ((o->op_flags & OPf_KIDS) && ckWARN2(WARN_DEPRECATED, WARN_SYNTAX)) {
5645     switch (cUNOPo->op_first->op_type) {
5646     case OP_RV2AV:
5647     /* This is needed for
5648     if (defined %stash::)
5649     to work. Do not break Tk.
5650     */
5651     break; /* Globals via GV can be undef */
5652     case OP_PADAV:
5653     case OP_AASSIGN: /* Is this a good idea? */
5654     Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5655     "defined(@array) is deprecated");
5656     Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5657     "\t(Maybe you should just omit the defined()?)\n");
5658     break;
5659     case OP_RV2HV:
5660     /* This is needed for
5661     if (defined %stash::)
5662     to work. Do not break Tk.
5663     */
5664     break; /* Globals via GV can be undef */
5665     case OP_PADHV:
5666     Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5667     "defined(%%hash) is deprecated");
5668     Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5669     "\t(Maybe you should just omit the defined()?)\n");
5670     break;
5671     default:
5672     /* no warning */
5673     break;
5674     }
5675     }
5676     return ck_rfun(o);
5677     }
5678    
5679     OP *
5680     Perl_ck_rfun(pTHX_ OP *o)
5681     {
5682     OPCODE type = o->op_type;
5683     return refkids(ck_fun(o), type);
5684     }
5685    
5686     OP *
5687     Perl_ck_listiob(pTHX_ OP *o)
5688     {
5689     register OP *kid;
5690    
5691     kid = cLISTOPo->op_first;
5692     if (!kid) {
5693     o = force_list(o);
5694     kid = cLISTOPo->op_first;
5695     }
5696     if (kid->op_type == OP_PUSHMARK)
5697     kid = kid->op_sibling;
5698     if (kid && o->op_flags & OPf_STACKED)
5699     kid = kid->op_sibling;
5700     else if (kid && !kid->op_sibling) { /* print HANDLE; */
5701     if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
5702     o->op_flags |= OPf_STACKED; /* make it a filehandle */
5703     kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
5704     cLISTOPo->op_first->op_sibling = kid;
5705     cLISTOPo->op_last = kid;
5706     kid = kid->op_sibling;
5707     }
5708     }
5709    
5710     if (!kid)
5711     append_elem(o->op_type, o, newDEFSVOP());
5712    
5713     return listkids(o);
5714     }
5715    
5716     OP *
5717     Perl_ck_sassign(pTHX_ OP *o)
5718     {
5719     OP *kid = cLISTOPo->op_first;
5720     /* has a disposable target? */
5721     if ((PL_opargs[kid->op_type] & OA_TARGLEX)
5722     && !(kid->op_flags & OPf_STACKED)
5723     /* Cannot steal the second time! */
5724     && !(kid->op_private & OPpTARGET_MY))
5725     {
5726     OP *kkid = kid->op_sibling;
5727    
5728     /* Can just relocate the target. */
5729     if (kkid && kkid->op_type == OP_PADSV
5730     && !(kkid->op_private & OPpLVAL_INTRO))
5731     {
5732     kid->op_targ = kkid->op_targ;
5733     kkid->op_targ = 0;
5734     /* Now we do not need PADSV and SASSIGN. */
5735     kid->op_sibling = o->op_sibling; /* NULL */
5736     cLISTOPo->op_first = NULL;
5737     op_free(o);
5738     op_free(kkid);
5739     kid->op_private |= OPpTARGET_MY; /* Used for context settings */
5740     return kid;
5741     }
5742     }
5743     /* optimise C<my $x = undef> to C<my $x> */
5744     if (kid->op_type == OP_UNDEF) {
5745     OP *kkid = kid->op_sibling;
5746     if (kkid && kkid->op_type == OP_PADSV
5747     && (kkid->op_private & OPpLVAL_INTRO))
5748     {
5749     cLISTOPo->op_first = NULL;
5750     kid->op_sibling = NULL;
5751     op_free(o);
5752     op_free(kid);
5753     return kkid;
5754     }
5755     }
5756     return o;
5757     }
5758    
5759     OP *
5760     Perl_ck_match(pTHX_ OP *o)
5761     {
5762     o->op_private |= OPpRUNTIME;
5763     return o;
5764     }
5765    
5766     OP *
5767     Perl_ck_method(pTHX_ OP *o)
5768     {
5769     OP *kid = cUNOPo->op_first;
5770     if (kid->op_type == OP_CONST) {
5771     SV* sv = kSVOP->op_sv;
5772     if (!(strchr(SvPVX(sv), ':') || strchr(SvPVX(sv), '\''))) {
5773     OP *cmop;
5774     if (!SvREADONLY(sv) || !SvFAKE(sv)) {
5775     sv = newSVpvn_share(SvPVX(sv), SvCUR(sv), 0);
5776     }
5777     else {
5778     kSVOP->op_sv = Nullsv;
5779     }
5780     cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
5781     op_free(o);
5782     return cmop;
5783     }
5784     }
5785     return o;
5786     }
5787    
5788     OP *
5789     Perl_ck_null(pTHX_ OP *o)
5790     {
5791     return o;
5792     }
5793    
5794     OP *
5795     Perl_ck_open(pTHX_ OP *o)
5796     {
5797     HV *table = GvHV(PL_hintgv);
5798     if (table) {
5799     SV **svp;
5800     I32 mode;
5801     svp = hv_fetch(table, "open_IN", 7, FALSE);
5802     if (svp && *svp) {
5803     mode = mode_from_discipline(*svp);
5804     if (mode & O_BINARY)
5805     o->op_private |= OPpOPEN_IN_RAW;
5806     else if (mode & O_TEXT)
5807     o->op_private |= OPpOPEN_IN_CRLF;
5808     }
5809    
5810     svp = hv_fetch(table, "open_OUT", 8, FALSE);
5811     if (svp && *svp) {
5812     mode = mode_from_discipline(*svp);
5813     if (mode & O_BINARY)
5814     o->op_private |= OPpOPEN_OUT_RAW;
5815     else if (mode & O_TEXT)
5816     o->op_private |= OPpOPEN_OUT_CRLF;
5817     }
5818     }
5819     if (o->op_type == OP_BACKTICK)
5820     return o;
5821     {
5822     /* In case of three-arg dup open remove strictness
5823     * from the last arg if it is a bareword. */
5824     OP *first = cLISTOPx(o)->op_first; /* The pushmark. */
5825     OP *last = cLISTOPx(o)->op_last; /* The bareword. */
5826     OP *oa;
5827     char *mode;
5828    
5829     if ((last->op_type == OP_CONST) && /* The bareword. */
5830     (last->op_private & OPpCONST_BARE) &&
5831     (last->op_private & OPpCONST_STRICT) &&
5832     (oa = first->op_sibling) && /* The fh. */
5833     (oa = oa->op_sibling) && /* The mode. */
5834     SvPOK(((SVOP*)oa)->op_sv) &&
5835     (mode = SvPVX(((SVOP*)oa)->op_sv)) &&
5836     mode[0] == '>' && mode[1] == '&' && /* A dup open. */
5837     (last == oa->op_sibling)) /* The bareword. */
5838     last->op_private &= ~OPpCONST_STRICT;
5839     }
5840     return ck_fun(o);
5841     }
5842    
5843     OP *
5844     Perl_ck_repeat(pTHX_ OP *o)
5845     {
5846     if (cBINOPo->op_first->op_flags & OPf_PARENS) {
5847     o->op_private |= OPpREPEAT_DOLIST;
5848     cBINOPo->op_first = force_list(cBINOPo->op_first);
5849     }
5850     else
5851     scalar(o);
5852     return o;
5853     }
5854    
5855     OP *
5856     Perl_ck_require(pTHX_ OP *o)
5857     {
5858     GV* gv;
5859    
5860     if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
5861     SVOP *kid = (SVOP*)cUNOPo->op_first;
5862    
5863     if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5864     char *s;
5865     for (s = SvPVX(kid->op_sv); *s; s++) {
5866     if (*s == ':' && s[1] == ':') {
5867     *s = '/';
5868     Move(s+2, s+1, strlen(s+2)+1, char);
5869     --SvCUR(kid->op_sv);
5870     }
5871     }
5872     if (SvREADONLY(kid->op_sv)) {
5873     SvREADONLY_off(kid->op_sv);
5874     sv_catpvn(kid->op_sv, ".pm", 3);
5875     SvREADONLY_on(kid->op_sv);
5876     }
5877     else
5878     sv_catpvn(kid->op_sv, ".pm", 3);
5879     }
5880     }
5881    
5882     /* handle override, if any */
5883     gv = gv_fetchpv("require", FALSE, SVt_PVCV);
5884     if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv)))
5885     gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
5886    
5887     if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
5888     OP *kid = cUNOPo->op_first;
5889     cUNOPo->op_first = 0;
5890     op_free(o);
5891     return ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
5892     append_elem(OP_LIST, kid,
5893     scalar(newUNOP(OP_RV2CV, 0,
5894     newGVOP(OP_GV, 0,
5895     gv))))));
5896     }
5897    
5898     return ck_fun(o);
5899     }
5900    
5901     OP *
5902     Perl_ck_return(pTHX_ OP *o)
5903     {
5904     OP *kid;
5905     if (CvLVALUE(PL_compcv)) {
5906     for (kid = cLISTOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
5907     mod(kid, OP_LEAVESUBLV);
5908     }
5909     return o;
5910     }
5911    
5912     #if 0
5913     OP *
5914     Perl_ck_retarget(pTHX_ OP *o)
5915     {
5916     Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
5917     /* STUB */
5918     return o;
5919     }
5920     #endif
5921    
5922     OP *
5923     Perl_ck_select(pTHX_ OP *o)
5924     {
5925     OP* kid;
5926     if (o->op_flags & OPf_KIDS) {
5927     kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5928     if (kid && kid->op_sibling) {
5929     o->op_type = OP_SSELECT;
5930     o->op_ppaddr = PL_ppaddr[OP_SSELECT];
5931     o = ck_fun(o);
5932     return fold_constants(o);
5933     }
5934     }
5935     o = ck_fun(o);
5936     kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5937     if (kid && kid->op_type == OP_RV2GV)
5938     kid->op_private &= ~HINT_STRICT_REFS;
5939     return o;
5940     }
5941    
5942     OP *
5943     Perl_ck_shift(pTHX_ OP *o)
5944     {
5945     I32 type = o->op_type;
5946    
5947     if (!(o->op_flags & OPf_KIDS)) {
5948     OP *argop;
5949    
5950     op_free(o);
5951     #ifdef USE_5005THREADS
5952     if (!CvUNIQUE(PL_compcv)) {
5953     argop = newOP(OP_PADAV, OPf_REF);
5954     argop->op_targ = 0; /* PAD_SV(0) is @_ */
5955     }
5956     else {
5957     argop = newUNOP(OP_RV2AV, 0,
5958     scalar(newGVOP(OP_GV, 0,
5959     gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
5960     }
5961     #else
5962     argop = newUNOP(OP_RV2AV, 0,
5963     scalar(newGVOP(OP_GV, 0, CvUNIQUE(PL_compcv) ? PL_argvgv : PL_defgv)));
5964     #endif /* USE_5005THREADS */
5965     return newUNOP(type, 0, scalar(argop));
5966     }
5967     return scalar(modkids(ck_fun(o), type));
5968     }
5969    
5970     OP *
5971     Perl_ck_sort(pTHX_ OP *o)
5972     {
5973     OP *firstkid;
5974    
5975     if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
5976     simplify_sort(o);
5977     firstkid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5978     if (o->op_flags & OPf_STACKED) { /* may have been cleared */
5979     OP *k = NULL;
5980     OP *kid = cUNOPx(firstkid)->op_first; /* get past null */
5981    
5982     if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
5983     linklist(kid);
5984     if (kid->op_type == OP_SCOPE) {
5985     k = kid->op_next;
5986     kid->op_next = 0;
5987     }
5988     else if (kid->op_type == OP_LEAVE) {
5989     if (o->op_type == OP_SORT) {
5990     op_null(kid); /* wipe out leave */
5991     kid->op_next = kid;
5992    
5993     for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
5994     if (k->op_next == kid)
5995     k->op_next = 0;
5996     /* don't descend into loops */
5997     else if (k->op_type == OP_ENTERLOOP
5998     || k->op_type == OP_ENTERITER)
5999     {
6000     k = cLOOPx(k)->op_lastop;
6001     }
6002     }
6003     }
6004     else
6005     kid->op_next = 0; /* just disconnect the leave */
6006     k = kLISTOP->op_first;
6007     }
6008     CALL_PEEP(k);
6009    
6010     kid = firstkid;
6011     if (o->op_type == OP_SORT) {
6012     /* provide scalar context for comparison function/block */
6013     kid = scalar(kid);
6014     kid->op_next = kid;
6015     }
6016     else
6017     kid->op_next = k;
6018     o->op_flags |= OPf_SPECIAL;
6019     }
6020     else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
6021     op_null(firstkid);
6022    
6023     firstkid = firstkid->op_sibling;
6024     }
6025    
6026     /* provide list context for arguments */
6027     if (o->op_type == OP_SORT)
6028     list(firstkid);
6029    
6030     return o;
6031     }
6032    
6033     STATIC void
6034     S_simplify_sort(pTHX_ OP *o)
6035     {
6036     register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6037     OP *k;
6038     int descending;
6039     GV *gv;
6040     const char *gvname;
6041     if (!(o->op_flags & OPf_STACKED))
6042     return;
6043     GvMULTI_on(gv_fetchpv("a", TRUE, SVt_PV));
6044     GvMULTI_on(gv_fetchpv("b", TRUE, SVt_PV));
6045     kid = kUNOP->op_first; /* get past null */
6046     if (kid->op_type != OP_SCOPE)
6047     return;
6048     kid = kLISTOP->op_last; /* get past scope */
6049     switch(kid->op_type) {
6050     case OP_NCMP:
6051     case OP_I_NCMP:
6052     case OP_SCMP:
6053     break;
6054     default:
6055     return;
6056     }
6057     k = kid; /* remember this node*/
6058     if (kBINOP->op_first->op_type != OP_RV2SV)
6059     return;
6060     kid = kBINOP->op_first; /* get past cmp */
6061     if (kUNOP->op_first->op_type != OP_GV)
6062     return;
6063     kid = kUNOP->op_first; /* get past rv2sv */
6064     gv = kGVOP_gv;
6065     if (GvSTASH(gv) != PL_curstash)
6066     return;
6067     gvname = GvNAME(gv);
6068     if (*gvname == 'a' && gvname[1] == '\0')
6069     descending = 0;
6070     else if (*gvname == 'b' && gvname[1] == '\0')
6071     descending = 1;
6072     else
6073     return;
6074    
6075     kid = k; /* back to cmp */
6076     if (kBINOP->op_last->op_type != OP_RV2SV)
6077     return;
6078     kid = kBINOP->op_last; /* down to 2nd arg */
6079     if (kUNOP->op_first->op_type != OP_GV)
6080     return;
6081     kid = kUNOP->op_first; /* get past rv2sv */
6082     gv = kGVOP_gv;
6083     if (GvSTASH(gv) != PL_curstash)
6084     return;
6085     gvname = GvNAME(gv);
6086     if ( descending
6087     ? !(*gvname == 'a' && gvname[1] == '\0')
6088     : !(*gvname == 'b' && gvname[1] == '\0'))
6089     return;
6090     o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
6091     if (descending)
6092     o->op_private |= OPpSORT_DESCEND;
6093     if (k->op_type == OP_NCMP)
6094     o->op_private |= OPpSORT_NUMERIC;
6095     if (k->op_type == OP_I_NCMP)
6096     o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
6097     kid = cLISTOPo->op_first->op_sibling;
6098     cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
6099     op_free(kid); /* then delete it */
6100     }
6101    
6102     OP *
6103     Perl_ck_split(pTHX_ OP *o)
6104     {
6105     register OP *kid;
6106    
6107     if (o->op_flags & OPf_STACKED)
6108     return no_fh_allowed(o);
6109    
6110     kid = cLISTOPo->op_first;
6111     if (kid->op_type != OP_NULL)
6112     Perl_croak(aTHX_ "panic: ck_split");
6113     kid = kid->op_sibling;
6114     op_free(cLISTOPo->op_first);
6115     cLISTOPo->op_first = kid;
6116     if (!kid) {
6117     cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvn(" ", 1));
6118     cLISTOPo->op_last = kid; /* There was only one element previously */
6119     }
6120    
6121     if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) {
6122     OP *sibl = kid->op_sibling;
6123     kid->op_sibling = 0;
6124     kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
6125     if (cLISTOPo->op_first == cLISTOPo->op_last)
6126     cLISTOPo->op_last = kid;
6127     cLISTOPo->op_first = kid;
6128     kid->op_sibling = sibl;
6129     }
6130    
6131     kid->op_type = OP_PUSHRE;
6132     kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
6133     scalar(kid);
6134     if (ckWARN(WARN_REGEXP) && ((PMOP *)kid)->op_pmflags & PMf_GLOBAL) {
6135     Perl_warner(aTHX_ packWARN(WARN_REGEXP),
6136     "Use of /g modifier is meaningless in split");
6137     }
6138    
6139     if (!kid->op_sibling)
6140     append_elem(OP_SPLIT, o, newDEFSVOP());
6141    
6142     kid = kid->op_sibling;
6143     scalar(kid);
6144    
6145     if (!kid->op_sibling)
6146     append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
6147    
6148     kid = kid->op_sibling;
6149     scalar(kid);
6150    
6151     if (kid->op_sibling)
6152     return too_many_arguments(o,OP_DESC(o));
6153    
6154     return o;
6155     }
6156    
6157     OP *
6158     Perl_ck_join(pTHX_ OP *o)
6159     {
6160     if (ckWARN(WARN_SYNTAX)) {
6161     OP *kid = cLISTOPo->op_first->op_sibling;
6162     if (kid && kid->op_type == OP_MATCH) {
6163     char *pmstr = "STRING";
6164     if (PM_GETRE(kPMOP))
6165     pmstr = PM_GETRE(kPMOP)->precomp;
6166     Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
6167     "/%s/ should probably be written as \"%s\"",
6168     pmstr, pmstr);
6169     }
6170     }
6171     return ck_fun(o);
6172     }
6173    
6174     OP *
6175     Perl_ck_subr(pTHX_ OP *o)
6176     {
6177     OP *prev = ((cUNOPo->op_first->op_sibling)
6178     ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
6179     OP *o2 = prev->op_sibling;
6180     OP *cvop;
6181     char *proto = 0;
6182     CV *cv = 0;
6183     GV *namegv = 0;
6184     int optional = 0;
6185     I32 arg = 0;
6186     I32 contextclass = 0;
6187     char *e = 0;
6188     STRLEN n_a;
6189    
6190     o->op_private |= OPpENTERSUB_HASTARG;
6191     for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
6192     if (cvop->op_type == OP_RV2CV) {
6193     SVOP* tmpop;
6194     o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
6195     op_null(cvop); /* disable rv2cv */
6196     tmpop = (SVOP*)((UNOP*)cvop)->op_first;
6197     if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) {
6198     GV *gv = cGVOPx_gv(tmpop);
6199     cv = GvCVu(gv);
6200     if (!cv)
6201     tmpop->op_private |= OPpEARLY_CV;
6202     else if (SvPOK(cv)) {
6203     namegv = CvANON(cv) ? gv : CvGV(cv);
6204     proto = SvPV((SV*)cv, n_a);
6205     }
6206     }
6207     }
6208     else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
6209     if (o2->op_type == OP_CONST)
6210     o2->op_private &= ~OPpCONST_STRICT;
6211     else if (o2->op_type == OP_LIST) {
6212     OP *o = ((UNOP*)o2)->op_first->op_sibling;
6213     if (o && o->op_type == OP_CONST)
6214     o->op_private &= ~OPpCONST_STRICT;
6215     }
6216     }
6217     o->op_private |= (PL_hints & HINT_STRICT_REFS);
6218     if (PERLDB_SUB && PL_curstash != PL_debstash)
6219     o->op_private |= OPpENTERSUB_DB;
6220     while (o2 != cvop) {
6221     if (proto) {
6222     switch (*proto) {
6223     case '\0':
6224     return too_many_arguments(o, gv_ename(namegv));
6225     case ';':
6226     optional = 1;
6227     proto++;
6228     continue;
6229     case '$':
6230     proto++;
6231     arg++;
6232     scalar(o2);
6233     break;
6234     case '%':
6235     case '@':
6236     list(o2);
6237     arg++;
6238     break;
6239     case '&':
6240     proto++;
6241     arg++;
6242     if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
6243     bad_type(arg,
6244     arg == 1 ? "block or sub {}" : "sub {}",
6245     gv_ename(namegv), o2);
6246     break;
6247     case '*':
6248     /* '*' allows any scalar type, including bareword */
6249     proto++;
6250     arg++;
6251     if (o2->op_type == OP_RV2GV)
6252     goto wrapref; /* autoconvert GLOB -> GLOBref */
6253     else if (o2->op_type == OP_CONST)
6254     o2->op_private &= ~OPpCONST_STRICT;
6255     else if (o2->op_type == OP_ENTERSUB) {
6256     /* accidental subroutine, revert to bareword */
6257     OP *gvop = ((UNOP*)o2)->op_first;
6258     if (gvop && gvop->op_type == OP_NULL) {
6259     gvop = ((UNOP*)gvop)->op_first;
6260     if (gvop) {
6261     for (; gvop->op_sibling; gvop = gvop->op_sibling)
6262     ;
6263     if (gvop &&
6264     (gvop->op_private & OPpENTERSUB_NOPAREN) &&
6265     (gvop = ((UNOP*)gvop)->op_first) &&
6266     gvop->op_type == OP_GV)
6267     {
6268     GV *gv = cGVOPx_gv(gvop);
6269     OP *sibling = o2->op_sibling;
6270     SV *n = newSVpvn("",0);
6271     op_free(o2);
6272     gv_fullname4(n, gv, "", FALSE);
6273     o2 = newSVOP(OP_CONST, 0, n);
6274     prev->op_sibling = o2;
6275     o2->op_sibling = sibling;
6276     }
6277     }
6278     }
6279     }
6280     scalar(o2);
6281     break;
6282     case '[': case ']':
6283     goto oops;
6284     break;
6285     case '\\':
6286     proto++;
6287     arg++;
6288     again:
6289     switch (*proto++) {
6290     case '[':
6291     if (contextclass++ == 0) {
6292     e = strchr(proto, ']');
6293     if (!e || e == proto)
6294     goto oops;
6295     }
6296     else
6297     goto oops;
6298     goto again;
6299     break;
6300     case ']':
6301     if (contextclass) {
6302     char *p = proto;
6303     char s = *p;
6304     contextclass = 0;
6305     *p = '\0';
6306     while (*--p != '[');
6307     bad_type(arg, Perl_form(aTHX_ "one of %s", p),
6308     gv_ename(namegv), o2);
6309     *proto = s;
6310     } else
6311     goto oops;
6312     break;
6313     case '*':
6314     if (o2->op_type == OP_RV2GV)
6315     goto wrapref;
6316     if (!contextclass)
6317     bad_type(arg, "symbol", gv_ename(namegv), o2);
6318     break;
6319     case '&':
6320     if (o2->op_type == OP_ENTERSUB)
6321     goto wrapref;
6322     if (!contextclass)
6323     bad_type(arg, "subroutine entry", gv_ename(namegv), o2);
6324     break;
6325     case '$':
6326     if (o2->op_type == OP_RV2SV ||
6327     o2->op_type == OP_PADSV ||
6328     o2->op_type == OP_HELEM ||
6329     o2->op_type == OP_AELEM ||
6330     o2->op_type == OP_THREADSV)
6331     goto wrapref;
6332     if (!contextclass)
6333     bad_type(arg, "scalar", gv_ename(namegv), o2);
6334     break;
6335     case '@':
6336     if (o2->op_type == OP_RV2AV ||
6337     o2->op_type == OP_PADAV)
6338     goto wrapref;
6339     if (!contextclass)
6340     bad_type(arg, "array", gv_ename(namegv), o2);
6341     break;
6342     case '%':
6343     if (o2->op_type == OP_RV2HV ||
6344     o2->op_type == OP_PADHV)
6345     goto wrapref;
6346     if (!contextclass)
6347     bad_type(arg, "hash", gv_ename(namegv), o2);
6348     break;
6349     wrapref:
6350     {
6351     OP* kid = o2;
6352     OP* sib = kid->op_sibling;
6353     kid->op_sibling = 0;
6354     o2 = newUNOP(OP_REFGEN, 0, kid);
6355     o2->op_sibling = sib;
6356     prev->op_sibling = o2;
6357     }
6358     if (contextclass && e) {
6359     proto = e + 1;
6360     contextclass = 0;
6361     }
6362     break;
6363     default: goto oops;
6364     }
6365     if (contextclass)
6366     goto again;
6367     break;
6368     case ' ':
6369     proto++;
6370     continue;
6371     default:
6372     oops:
6373     Perl_croak(aTHX_ "Malformed prototype for %s: %"SVf,
6374     gv_ename(namegv), cv);
6375     }
6376     }
6377     else
6378     list(o2);
6379     mod(o2, OP_ENTERSUB);
6380     prev = o2;
6381     o2 = o2->op_sibling;
6382     }
6383     if (proto && !optional &&
6384     (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
6385     return too_few_arguments(o, gv_ename(namegv));
6386     return o;
6387     }
6388    
6389     OP *
6390     Perl_ck_svconst(pTHX_ OP *o)
6391     {
6392     SvREADONLY_on(cSVOPo->op_sv);
6393     return o;
6394     }
6395    
6396     OP *
6397     Perl_ck_trunc(pTHX_ OP *o)
6398     {
6399     if (o->op_flags & OPf_KIDS) {
6400     SVOP *kid = (SVOP*)cUNOPo->op_first;
6401    
6402     if (kid->op_type == OP_NULL)
6403     kid = (SVOP*)kid->op_sibling;
6404     if (kid && kid->op_type == OP_CONST &&
6405     (kid->op_private & OPpCONST_BARE))
6406     {
6407     o->op_flags |= OPf_SPECIAL;
6408     kid->op_private &= ~OPpCONST_STRICT;
6409     }
6410     }
6411     return ck_fun(o);
6412     }
6413    
6414     OP *
6415     Perl_ck_substr(pTHX_ OP *o)
6416     {
6417     o = ck_fun(o);
6418     if ((o->op_flags & OPf_KIDS) && o->op_private == 4) {
6419     OP *kid = cLISTOPo->op_first;
6420    
6421     if (kid->op_type == OP_NULL)
6422     kid = kid->op_sibling;
6423     if (kid)
6424     kid->op_flags |= OPf_MOD;
6425    
6426     }
6427     return o;
6428     }
6429    
6430     /* A peephole optimizer. We visit the ops in the order they're to execute.
6431     * See the comments at the top of this file for more details about when
6432     * peep() is called */
6433    
6434     void
6435     Perl_peep(pTHX_ register OP *o)
6436     {
6437     register OP* oldop = 0;
6438     STRLEN n_a;
6439    
6440     if (!o || o->op_seq)
6441     return;
6442     ENTER;
6443     SAVEOP();
6444     SAVEVPTR(PL_curcop);
6445     for (; o; o = o->op_next) {
6446     if (o->op_seq)
6447     break;
6448     /* The special value -1 is used by the B::C compiler backend to indicate
6449     * that an op is statically defined and should not be freed */
6450     if (!PL_op_seqmax || PL_op_seqmax == (U16)-1)
6451     PL_op_seqmax = 1;
6452     PL_op = o;
6453     switch (o->op_type) {
6454     case OP_SETSTATE:
6455     case OP_NEXTSTATE:
6456     case OP_DBSTATE:
6457     PL_curcop = ((COP*)o); /* for warnings */
6458     o->op_seq = PL_op_seqmax++;
6459     break;
6460    
6461     case OP_CONST:
6462     if (cSVOPo->op_private & OPpCONST_STRICT)
6463     no_bareword_allowed(o);
6464     #ifdef USE_ITHREADS
6465     case OP_METHOD_NAMED:
6466     /* Relocate sv to the pad for thread safety.
6467     * Despite being a "constant", the SV is written to,
6468     * for reference counts, sv_upgrade() etc. */
6469     if (cSVOP->op_sv) {
6470     PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP);
6471     if (o->op_type == OP_CONST && SvPADTMP(cSVOPo->op_sv)) {
6472     /* If op_sv is already a PADTMP then it is being used by
6473     * some pad, so make a copy. */
6474     sv_setsv(PAD_SVl(ix),cSVOPo->op_sv);
6475     SvREADONLY_on(PAD_SVl(ix));
6476     SvREFCNT_dec(cSVOPo->op_sv);
6477     }
6478     else {
6479     SvREFCNT_dec(PAD_SVl(ix));
6480     SvPADTMP_on(cSVOPo->op_sv);
6481     PAD_SETSV(ix, cSVOPo->op_sv);
6482     /* XXX I don't know how this isn't readonly already. */
6483     SvREADONLY_on(PAD_SVl(ix));
6484     }
6485     cSVOPo->op_sv = Nullsv;
6486     o->op_targ = ix;
6487     }
6488     #endif
6489     o->op_seq = PL_op_seqmax++;
6490     break;
6491    
6492     case OP_CONCAT:
6493     if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
6494     if (o->op_next->op_private & OPpTARGET_MY) {
6495     if (o->op_flags & OPf_STACKED) /* chained concats */
6496     goto ignore_optimization;
6497     else {
6498     /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
6499     o->op_targ = o->op_next->op_targ;
6500     o->op_next->op_targ = 0;
6501     o->op_private |= OPpTARGET_MY;
6502     }
6503     }
6504     op_null(o->op_next);
6505     }
6506     ignore_optimization:
6507     o->op_seq = PL_op_seqmax++;
6508     break;
6509     case OP_STUB:
6510     if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
6511     o->op_seq = PL_op_seqmax++;
6512     break; /* Scalar stub must produce undef. List stub is noop */
6513     }
6514     goto nothin;
6515     case OP_NULL:
6516     if (o->op_targ == OP_NEXTSTATE
6517     || o->op_targ == OP_DBSTATE
6518     || o->op_targ == OP_SETSTATE)
6519     {
6520     PL_curcop = ((COP*)o);
6521     }
6522     /* XXX: We avoid setting op_seq here to prevent later calls
6523     to peep() from mistakenly concluding that optimisation
6524     has already occurred. This doesn't fix the real problem,
6525     though (See 20010220.007). AMS 20010719 */
6526     if (oldop && o->op_next) {
6527     oldop->op_next = o->op_next;
6528     continue;
6529     }
6530     break;
6531     case OP_SCALAR:
6532     case OP_LINESEQ:
6533     case OP_SCOPE:
6534     nothin:
6535     if (oldop && o->op_next) {
6536     oldop->op_next = o->op_next;
6537     continue;
6538     }
6539     o->op_seq = PL_op_seqmax++;
6540     break;
6541    
6542     case OP_PADAV:
6543     case OP_GV:
6544     if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) {
6545     OP* pop = (o->op_type == OP_PADAV) ?
6546     o->op_next : o->op_next->op_next;
6547     IV i;
6548     if (pop && pop->op_type == OP_CONST &&
6549     ((PL_op = pop->op_next)) &&
6550     pop->op_next->op_type == OP_AELEM &&
6551     !(pop->op_next->op_private &
6552     (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) &&
6553     (i = SvIV(((SVOP*)pop)->op_sv) - PL_curcop->cop_arybase)
6554     <= 255 &&
6555     i >= 0)
6556     {
6557     GV *gv;
6558     if (cSVOPx(pop)->op_private & OPpCONST_STRICT)
6559     no_bareword_allowed(pop);
6560     if (o->op_type == OP_GV)
6561     op_null(o->op_next);
6562     op_null(pop->op_next);
6563     op_null(pop);
6564     o->op_flags |= pop->op_next->op_flags & OPf_MOD;
6565     o->op_next = pop->op_next->op_next;
6566     o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
6567     o->op_private = (U8)i;
6568     if (o->op_type == OP_GV) {
6569     gv = cGVOPo_gv;
6570     GvAVn(gv);
6571     }
6572     else
6573     o->op_flags |= OPf_SPECIAL;
6574     o->op_type = OP_AELEMFAST;
6575     }
6576     o->op_seq = PL_op_seqmax++;
6577     break;
6578     }
6579    
6580     if (o->op_next->op_type == OP_RV2SV) {
6581     if (!(o->op_next->op_private & OPpDEREF)) {
6582     op_null(o->op_next);
6583     o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
6584     | OPpOUR_INTRO);
6585     o->op_next = o->op_next->op_next;
6586     o->op_type = OP_GVSV;
6587     o->op_ppaddr = PL_ppaddr[OP_GVSV];
6588     }
6589     }
6590     else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
6591     GV *gv = cGVOPo_gv;
6592     if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX(GvCV(gv))) {
6593     /* XXX could check prototype here instead of just carping */
6594     SV *sv = sv_newmortal();
6595     gv_efullname3(sv, gv, Nullch);
6596     Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
6597     "%"SVf"() called too early to check prototype",
6598     sv);
6599     }
6600     }
6601     else if (o->op_next->op_type == OP_READLINE
6602     && o->op_next->op_next->op_type == OP_CONCAT
6603     && (o->op_next->op_next->op_flags & OPf_STACKED))
6604     {
6605     /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */
6606     o->op_type = OP_RCATLINE;
6607     o->op_flags |= OPf_STACKED;
6608     o->op_ppaddr = PL_ppaddr[OP_RCATLINE];
6609     op_null(o->op_next->op_next);
6610     op_null(o->op_next);
6611     }
6612    
6613     o->op_seq = PL_op_seqmax++;
6614     break;
6615    
6616     case OP_MAPWHILE:
6617     case OP_GREPWHILE:
6618     case OP_AND:
6619     case OP_OR:
6620     case OP_ANDASSIGN:
6621     case OP_ORASSIGN:
6622     case OP_COND_EXPR:
6623     case OP_RANGE:
6624     o->op_seq = PL_op_seqmax++;
6625     while (cLOGOP->op_other->op_type == OP_NULL)
6626     cLOGOP->op_other = cLOGOP->op_other->op_next;
6627     peep(cLOGOP->op_other); /* Recursive calls are not replaced by fptr calls */
6628     break;
6629    
6630     case OP_ENTERLOOP:
6631     case OP_ENTERITER:
6632     o->op_seq = PL_op_seqmax++;
6633     while (cLOOP->op_redoop->op_type == OP_NULL)
6634     cLOOP->op_redoop = cLOOP->op_redoop->op_next;
6635     peep(cLOOP->op_redoop);
6636     while (cLOOP->op_nextop->op_type == OP_NULL)
6637     cLOOP->op_nextop = cLOOP->op_nextop->op_next;
6638     peep(cLOOP->op_nextop);
6639     while (cLOOP->op_lastop->op_type == OP_NULL)
6640     cLOOP->op_lastop = cLOOP->op_lastop->op_next;
6641     peep(cLOOP->op_lastop);
6642     break;
6643    
6644     case OP_QR:
6645     case OP_MATCH:
6646     case OP_SUBST:
6647     o->op_seq = PL_op_seqmax++;
6648     while (cPMOP->op_pmreplstart &&
6649     cPMOP->op_pmreplstart->op_type == OP_NULL)
6650     cPMOP->op_pmreplstart = cPMOP->op_pmreplstart->op_next;
6651     peep(cPMOP->op_pmreplstart);
6652     break;
6653    
6654     case OP_EXEC:
6655     o->op_seq = PL_op_seqmax++;
6656     if (ckWARN(WARN_SYNTAX) && o->op_next
6657     && o->op_next->op_type == OP_NEXTSTATE) {
6658     if (o->op_next->op_sibling &&
6659     o->op_next->op_sibling->op_type != OP_EXIT &&
6660     o->op_next->op_sibling->op_type != OP_WARN &&
6661     o->op_next->op_sibling->op_type != OP_DIE) {
6662     line_t oldline = CopLINE(PL_curcop);
6663    
6664     CopLINE_set(PL_curcop, CopLINE((COP*)o->op_next));
6665     Perl_warner(aTHX_ packWARN(WARN_EXEC),
6666     "Statement unlikely to be reached");
6667     Perl_warner(aTHX_ packWARN(WARN_EXEC),
6668     "\t(Maybe you meant system() when you said exec()?)\n");
6669     CopLINE_set(PL_curcop, oldline);
6670     }
6671     }
6672     break;
6673    
6674     case OP_HELEM: {
6675     UNOP *rop;
6676     SV *lexname;
6677     GV **fields;
6678     SV **svp, **indsvp, *sv;
6679     I32 ind;
6680     char *key = NULL;
6681     STRLEN keylen;
6682    
6683     o->op_seq = PL_op_seqmax++;
6684    
6685     if (((BINOP*)o)->op_last->op_type != OP_CONST)
6686     break;
6687    
6688     /* Make the CONST have a shared SV */
6689     svp = cSVOPx_svp(((BINOP*)o)->op_last);
6690     if ((!SvFAKE(sv = *svp) || !SvREADONLY(sv)) && !IS_PADCONST(sv)) {
6691     key = SvPV(sv, keylen);
6692     lexname = newSVpvn_share(key,
6693     SvUTF8(sv) ? -(I32)keylen : keylen,
6694     0);
6695     SvREFCNT_dec(sv);
6696     *svp = lexname;
6697     }
6698    
6699     if ((o->op_private & (OPpLVAL_INTRO)))
6700     break;
6701    
6702     rop = (UNOP*)((BINOP*)o)->op_first;
6703     if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6704     break;
6705     lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6706     if (!(SvFLAGS(lexname) & SVpad_TYPED))
6707     break;
6708     fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6709     if (!fields || !GvHV(*fields))
6710     break;
6711     key = SvPV(*svp, keylen);
6712     indsvp = hv_fetch(GvHV(*fields), key,
6713     SvUTF8(*svp) ? -(I32)keylen : keylen, FALSE);
6714     if (!indsvp) {
6715     Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" in variable %s of type %s",
6716     key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
6717     }
6718     ind = SvIV(*indsvp);
6719     if (ind < 1)
6720     Perl_croak(aTHX_ "Bad index while coercing array into hash");
6721     rop->op_type = OP_RV2AV;
6722     rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6723     o->op_type = OP_AELEM;
6724     o->op_ppaddr = PL_ppaddr[OP_AELEM];
6725     sv = newSViv(ind);
6726     if (SvREADONLY(*svp))
6727     SvREADONLY_on(sv);
6728     SvFLAGS(sv) |= (SvFLAGS(*svp)
6729     & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
6730     SvREFCNT_dec(*svp);
6731     *svp = sv;
6732     break;
6733     }
6734    
6735     case OP_HSLICE: {
6736     UNOP *rop;
6737     SV *lexname;
6738     GV **fields;
6739     SV **svp, **indsvp, *sv;
6740     I32 ind;
6741     char *key;
6742     STRLEN keylen;
6743     SVOP *first_key_op, *key_op;
6744    
6745     o->op_seq = PL_op_seqmax++;
6746     if ((o->op_private & (OPpLVAL_INTRO))
6747     /* I bet there's always a pushmark... */
6748     || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST)
6749     /* hmmm, no optimization if list contains only one key. */
6750     break;
6751     rop = (UNOP*)((LISTOP*)o)->op_last;
6752     if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6753     break;
6754     lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6755     if (!(SvFLAGS(lexname) & SVpad_TYPED))
6756     break;
6757     fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6758     if (!fields || !GvHV(*fields))
6759     break;
6760     /* Again guessing that the pushmark can be jumped over.... */
6761     first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling)
6762     ->op_first->op_sibling;
6763     /* Check that the key list contains only constants. */
6764     for (key_op = first_key_op; key_op;
6765     key_op = (SVOP*)key_op->op_sibling)
6766     if (key_op->op_type != OP_CONST)
6767     break;
6768     if (key_op)
6769     break;
6770     rop->op_type = OP_RV2AV;
6771     rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6772     o->op_type = OP_ASLICE;
6773     o->op_ppaddr = PL_ppaddr[OP_ASLICE];
6774     for (key_op = first_key_op; key_op;
6775     key_op = (SVOP*)key_op->op_sibling) {
6776     svp = cSVOPx_svp(key_op);
6777     key = SvPV(*svp, keylen);
6778     indsvp = hv_fetch(GvHV(*fields), key,
6779     SvUTF8(*svp) ? -(I32)keylen : keylen, FALSE);
6780     if (!indsvp) {
6781     Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" "
6782     "in variable %s of type %s",
6783     key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
6784     }
6785     ind = SvIV(*indsvp);
6786     if (ind < 1)
6787     Perl_croak(aTHX_ "Bad index while coercing array into hash");
6788     sv = newSViv(ind);
6789     if (SvREADONLY(*svp))
6790     SvREADONLY_on(sv);
6791     SvFLAGS(sv) |= (SvFLAGS(*svp)
6792     & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
6793     SvREFCNT_dec(*svp);
6794     *svp = sv;
6795     }
6796     break;
6797     }
6798    
6799     case OP_SORT: {
6800     /* will point to RV2AV or PADAV op on LHS/RHS of assign */
6801     OP *oleft, *oright;
6802     OP *o2;
6803    
6804     /* check that RHS of sort is a single plain array */
6805     oright = cUNOPo->op_first;
6806     if (!oright || oright->op_type != OP_PUSHMARK)
6807     break;
6808    
6809     /* reverse sort ... can be optimised. */
6810     if (!cUNOPo->op_sibling) {
6811     /* Nothing follows us on the list. */
6812     OP *reverse = o->op_next;
6813    
6814     if (reverse->op_type == OP_REVERSE &&
6815     (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) {
6816     OP *pushmark = cUNOPx(reverse)->op_first;
6817     if (pushmark && (pushmark->op_type == OP_PUSHMARK)
6818     && (cUNOPx(pushmark)->op_sibling == o)) {
6819     /* reverse -> pushmark -> sort */
6820     o->op_private |= OPpSORT_REVERSE;
6821     op_null(reverse);
6822     pushmark->op_next = oright->op_next;
6823     op_null(oright);
6824     }
6825     }
6826     }
6827    
6828     /* make @a = sort @a act in-place */
6829    
6830     o->op_seq = PL_op_seqmax++;
6831    
6832     oright = cUNOPx(oright)->op_sibling;
6833     if (!oright)
6834     break;
6835     if (oright->op_type == OP_NULL) { /* skip sort block/sub */
6836     oright = cUNOPx(oright)->op_sibling;
6837     }
6838    
6839     if (!oright ||
6840     (oright->op_type != OP_RV2AV && oright->op_type != OP_PADAV)
6841     || oright->op_next != o
6842     || (oright->op_private & OPpLVAL_INTRO)
6843     )
6844     break;
6845    
6846     /* o2 follows the chain of op_nexts through the LHS of the
6847     * assign (if any) to the aassign op itself */
6848     o2 = o->op_next;
6849     if (!o2 || o2->op_type != OP_NULL)
6850     break;
6851     o2 = o2->op_next;
6852     if (!o2 || o2->op_type != OP_PUSHMARK)
6853     break;
6854     o2 = o2->op_next;
6855     if (o2 && o2->op_type == OP_GV)
6856     o2 = o2->op_next;
6857     if (!o2
6858     || (o2->op_type != OP_PADAV && o2->op_type != OP_RV2AV)
6859     || (o2->op_private & OPpLVAL_INTRO)
6860     )
6861     break;
6862     oleft = o2;
6863     o2 = o2->op_next;
6864     if (!o2 || o2->op_type != OP_NULL)
6865     break;
6866     o2 = o2->op_next;
6867     if (!o2 || o2->op_type != OP_AASSIGN
6868     || (o2->op_flags & OPf_WANT) != OPf_WANT_VOID)
6869     break;
6870    
6871     /* check that the sort is the first arg on RHS of assign */
6872    
6873     o2 = cUNOPx(o2)->op_first;
6874     if (!o2 || o2->op_type != OP_NULL)
6875     break;
6876     o2 = cUNOPx(o2)->op_first;
6877     if (!o2 || o2->op_type != OP_PUSHMARK)
6878     break;
6879     if (o2->op_sibling != o)
6880     break;
6881    
6882     /* check the array is the same on both sides */
6883     if (oleft->op_type == OP_RV2AV) {
6884     if (oright->op_type != OP_RV2AV
6885     || !cUNOPx(oright)->op_first
6886     || cUNOPx(oright)->op_first->op_type != OP_GV
6887     || cGVOPx_gv(cUNOPx(oleft)->op_first) !=
6888     cGVOPx_gv(cUNOPx(oright)->op_first)
6889     )
6890     break;
6891     }
6892     else if (oright->op_type != OP_PADAV
6893     || oright->op_targ != oleft->op_targ
6894     )
6895     break;
6896    
6897     /* transfer MODishness etc from LHS arg to RHS arg */
6898     oright->op_flags = oleft->op_flags;
6899     o->op_private |= OPpSORT_INPLACE;
6900    
6901     /* excise push->gv->rv2av->null->aassign */
6902     o2 = o->op_next->op_next;
6903     op_null(o2); /* PUSHMARK */
6904     o2 = o2->op_next;
6905     if (o2->op_type == OP_GV) {
6906     op_null(o2); /* GV */
6907     o2 = o2->op_next;
6908     }
6909     op_null(o2); /* RV2AV or PADAV */
6910     o2 = o2->op_next->op_next;
6911     op_null(o2); /* AASSIGN */
6912    
6913     o->op_next = o2->op_next;
6914    
6915     break;
6916     }
6917    
6918     case OP_REVERSE: {
6919     OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av;
6920     OP *gvop = NULL;
6921     LISTOP *enter, *exlist;
6922     o->op_seq = PL_op_seqmax++;
6923    
6924     enter = (LISTOP *) o->op_next;
6925     if (!enter)
6926     break;
6927     if (enter->op_type == OP_NULL) {
6928     enter = (LISTOP *) enter->op_next;
6929     if (!enter)
6930     break;
6931     }
6932     /* for $a (...) will have OP_GV then OP_RV2GV here.
6933     for (...) just has an OP_GV. */
6934     if (enter->op_type == OP_GV) {
6935     gvop = (OP *) enter;
6936     enter = (LISTOP *) enter->op_next;
6937     if (!enter)
6938     break;
6939     if (enter->op_type == OP_RV2GV) {
6940     enter = (LISTOP *) enter->op_next;
6941     if (!enter)
6942     break;
6943     }
6944     }
6945    
6946     if (enter->op_type != OP_ENTERITER)
6947     break;
6948    
6949     iter = enter->op_next;
6950     if (!iter || iter->op_type != OP_ITER)
6951     break;
6952    
6953     expushmark = enter->op_first;
6954     if (!expushmark || expushmark->op_type != OP_NULL
6955     || expushmark->op_targ != OP_PUSHMARK)
6956     break;
6957    
6958     exlist = (LISTOP *) expushmark->op_sibling;
6959     if (!exlist || exlist->op_type != OP_NULL
6960     || exlist->op_targ != OP_LIST)
6961     break;
6962    
6963     if (exlist->op_last != o) {
6964     /* Mmm. Was expecting to point back to this op. */
6965     break;
6966     }
6967     theirmark = exlist->op_first;
6968     if (!theirmark || theirmark->op_type != OP_PUSHMARK)
6969     break;
6970    
6971     if (theirmark->op_sibling != o) {
6972     /* There's something between the mark and the reverse, eg
6973     for (1, reverse (...))
6974     so no go. */
6975     break;
6976     }
6977    
6978     ourmark = ((LISTOP *)o)->op_first;
6979     if (!ourmark || ourmark->op_type != OP_PUSHMARK)
6980     break;
6981    
6982     ourlast = ((LISTOP *)o)->op_last;
6983     if (!ourlast || ourlast->op_next != o)
6984     break;
6985    
6986     rv2av = ourmark->op_sibling;
6987     if (rv2av && rv2av->op_type == OP_RV2AV && rv2av->op_sibling == 0
6988     && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)
6989     && enter->op_flags == (OPf_WANT_LIST | OPf_KIDS)) {
6990     /* We're just reversing a single array. */
6991     rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF;
6992     enter->op_flags |= OPf_STACKED;
6993     }
6994    
6995     /* We don't have control over who points to theirmark, so sacrifice
6996     ours. */
6997     theirmark->op_next = ourmark->op_next;
6998     theirmark->op_flags = ourmark->op_flags;
6999     ourlast->op_next = gvop ? gvop : (OP *) enter;
7000     op_null(ourmark);
7001     op_null(o);
7002     enter->op_private |= OPpITER_REVERSED;
7003     iter->op_private |= OPpITER_REVERSED;
7004    
7005     break;
7006     }
7007    
7008     default:
7009     o->op_seq = PL_op_seqmax++;
7010     break;
7011     }
7012     oldop = o;
7013     }
7014     LEAVE;
7015     }
7016    
7017    
7018    
7019     char* Perl_custom_op_name(pTHX_ OP* o)
7020     {
7021     IV index = PTR2IV(o->op_ppaddr);
7022     SV* keysv;
7023     HE* he;
7024    
7025     if (!PL_custom_op_names) /* This probably shouldn't happen */
7026     return PL_op_name[OP_CUSTOM];
7027    
7028     keysv = sv_2mortal(newSViv(index));
7029    
7030     he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0);
7031     if (!he)
7032     return PL_op_name[OP_CUSTOM]; /* Don't know who you are */
7033    
7034     return SvPV_nolen(HeVAL(he));
7035     }
7036    
7037     char* Perl_custom_op_desc(pTHX_ OP* o)
7038     {
7039     IV index = PTR2IV(o->op_ppaddr);
7040     SV* keysv;
7041     HE* he;
7042    
7043     if (!PL_custom_op_descs)
7044     return PL_op_desc[OP_CUSTOM];
7045    
7046     keysv = sv_2mortal(newSViv(index));
7047    
7048     he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0);
7049     if (!he)
7050     return PL_op_desc[OP_CUSTOM];
7051    
7052     return SvPV_nolen(HeVAL(he));
7053     }
7054    
7055    
7056     #include "XSUB.h"
7057    
7058     /* Efficient sub that returns a constant scalar value. */
7059     static void
7060     const_sv_xsub(pTHX_ CV* cv)
7061     {
7062     dXSARGS;
7063     if (items != 0) {
7064     #if 0
7065     Perl_croak(aTHX_ "usage: %s::%s()",
7066     HvNAME(GvSTASH(CvGV(cv))), GvNAME(CvGV(cv)));
7067     #endif
7068     }
7069     EXTEND(sp, 1);
7070     ST(0) = (SV*)XSANY.any_ptr;
7071     XSRETURN(1);
7072     }