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