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