This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Lexical use open ... support:
[perl5.git] / op.c
CommitLineData
a0d0e21e 1/* op.c
79072805 2 *
3818b22b 3 * Copyright (c) 1991-2000, 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
18#include "EXTERN.h"
864dbfa3 19#define PERL_IN_OP_C
79072805 20#include "perl.h"
77ca0c92 21#include "keywords.h"
79072805 22
b7dc083c 23/* #define PL_OP_SLAB_ALLOC */
7934575e 24
1c846c1f 25#ifdef PL_OP_SLAB_ALLOC
b7dc083c
NIS
26#define SLAB_SIZE 8192
27static char *PL_OpPtr = NULL;
28static int PL_OpSpace = 0;
29#define NewOp(m,var,c,type) do { if ((PL_OpSpace -= c*sizeof(type)) >= 0) \
30 var = (type *)(PL_OpPtr -= c*sizeof(type)); \
31 else \
32 var = (type *) Slab_Alloc(m,c*sizeof(type)); \
33 } while (0)
34
1c846c1f 35STATIC void *
cea2e8a9 36S_Slab_Alloc(pTHX_ int m, size_t sz)
1c846c1f 37{
b7dc083c
NIS
38 Newz(m,PL_OpPtr,SLAB_SIZE,char);
39 PL_OpSpace = SLAB_SIZE - sz;
40 return PL_OpPtr += PL_OpSpace;
41}
76e3520e 42
1c846c1f 43#else
b7dc083c
NIS
44#define NewOp(m, var, c, type) Newz(m, var, c, type)
45#endif
e50aee73 46/*
5dc0d613 47 * In the following definition, the ", Nullop" is just to make the compiler
a5f75d66 48 * think the expression is of the right type: croak actually does a Siglongjmp.
e50aee73 49 */
11343788 50#define CHECKOP(type,o) \
3280af22 51 ((PL_op_mask && PL_op_mask[type]) \
5dc0d613 52 ? ( op_free((OP*)o), \
cea2e8a9 53 Perl_croak(aTHX_ "%s trapped by operation mask", PL_op_desc[type]), \
28757baa 54 Nullop ) \
fc0dc3b3 55 : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o))
e50aee73 56
c53d7c7d
HS
57#define PAD_MAX 999999999
58
76e3520e 59STATIC char*
cea2e8a9 60S_gv_ename(pTHX_ GV *gv)
4633a7c4 61{
2d8e6c8d 62 STRLEN n_a;
4633a7c4 63 SV* tmpsv = sv_newmortal();
46fc3d4c 64 gv_efullname3(tmpsv, gv, Nullch);
2d8e6c8d 65 return SvPV(tmpsv,n_a);
4633a7c4
LW
66}
67
76e3520e 68STATIC OP *
cea2e8a9 69S_no_fh_allowed(pTHX_ OP *o)
79072805 70{
cea2e8a9 71 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
22c35a8c 72 PL_op_desc[o->op_type]));
11343788 73 return o;
79072805
LW
74}
75
76e3520e 76STATIC OP *
cea2e8a9 77S_too_few_arguments(pTHX_ OP *o, char *name)
79072805 78{
cea2e8a9 79 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
11343788 80 return o;
79072805
LW
81}
82
76e3520e 83STATIC OP *
cea2e8a9 84S_too_many_arguments(pTHX_ OP *o, char *name)
79072805 85{
cea2e8a9 86 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
11343788 87 return o;
79072805
LW
88}
89
76e3520e 90STATIC void
cea2e8a9 91S_bad_type(pTHX_ I32 n, char *t, char *name, OP *kid)
8990e307 92{
cea2e8a9 93 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
22c35a8c 94 (int)n, name, t, PL_op_desc[kid->op_type]));
8990e307
LW
95}
96
7a52d87a 97STATIC void
cea2e8a9 98S_no_bareword_allowed(pTHX_ OP *o)
7a52d87a 99{
5a844595
GS
100 qerror(Perl_mess(aTHX_
101 "Bareword \"%s\" not allowed while \"strict subs\" in use",
7766f137 102 SvPV_nolen(cSVOPo_sv)));
7a52d87a
GS
103}
104
79072805
LW
105/* "register" allocation */
106
107PADOFFSET
864dbfa3 108Perl_pad_allocmy(pTHX_ char *name)
93a17b20 109{
11343788 110 dTHR;
a0d0e21e
LW
111 PADOFFSET off;
112 SV *sv;
113
155aba94
GS
114 if (!(PL_in_my == KEY_our ||
115 isALPHA(name[1]) ||
116 (PL_hints & HINT_UTF8 && (name[1] & 0xc0) == 0xc0) ||
117 (name[1] == '_' && (int)strlen(name) > 2)))
834a4ddd 118 {
c4d0567e 119 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
2b92dfce
GS
120 /* 1999-02-27 mjd@plover.com */
121 char *p;
122 p = strchr(name, '\0');
123 /* The next block assumes the buffer is at least 205 chars
124 long. At present, it's always at least 256 chars. */
125 if (p-name > 200) {
126 strcpy(name+200, "...");
127 p = name+199;
128 }
129 else {
130 p[1] = '\0';
131 }
132 /* Move everything else down one character */
133 for (; p-name > 2; p--)
134 *p = *(p-1);
46fc3d4c
PP
135 name[2] = toCTRL(name[1]);
136 name[1] = '^';
137 }
cea2e8a9 138 yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
a0d0e21e 139 }
e476b1b5 140 if (ckWARN(WARN_MISC) && AvFILLp(PL_comppad_name) >= 0) {
3280af22 141 SV **svp = AvARRAY(PL_comppad_name);
33633739
GS
142 HV *ourstash = (PL_curstash ? PL_curstash : PL_defstash);
143 PADOFFSET top = AvFILLp(PL_comppad_name);
144 for (off = top; off > PL_comppad_name_floor; off--) {
b1cb66bf 145 if ((sv = svp[off])
3280af22 146 && sv != &PL_sv_undef
c53d7c7d 147 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
33633739
GS
148 && (PL_in_my != KEY_our
149 || ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash))
b1cb66bf
PP
150 && strEQ(name, SvPVX(sv)))
151 {
e476b1b5 152 Perl_warner(aTHX_ WARN_MISC,
1c846c1f 153 "\"%s\" variable %s masks earlier declaration in same %s",
33633739
GS
154 (PL_in_my == KEY_our ? "our" : "my"),
155 name,
156 (SvIVX(sv) == PAD_MAX ? "scope" : "statement"));
157 --off;
158 break;
159 }
160 }
161 if (PL_in_my == KEY_our) {
635bab04 162 do {
33633739
GS
163 if ((sv = svp[off])
164 && sv != &PL_sv_undef
5ce0178e 165 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
33633739
GS
166 && ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash)
167 && strEQ(name, SvPVX(sv)))
f472eb5c 168 {
e476b1b5 169 Perl_warner(aTHX_ WARN_MISC,
33633739 170 "\"our\" variable %s redeclared", name);
e476b1b5 171 Perl_warner(aTHX_ WARN_MISC,
cc507455 172 "\t(Did you mean \"local\" instead of \"our\"?)\n");
33633739 173 break;
f472eb5c 174 }
635bab04 175 } while ( off-- > 0 );
b1cb66bf
PP
176 }
177 }
a0d0e21e
LW
178 off = pad_alloc(OP_PADSV, SVs_PADMY);
179 sv = NEWSV(1102,0);
93a17b20
LW
180 sv_upgrade(sv, SVt_PVNV);
181 sv_setpv(sv, name);
3280af22 182 if (PL_in_my_stash) {
c750a3ec 183 if (*name != '$')
eb64745e
GS
184 yyerror(Perl_form(aTHX_ "Can't declare class for non-scalar %s in \"%s\"",
185 name, PL_in_my == KEY_our ? "our" : "my"));
c750a3ec
MB
186 SvOBJECT_on(sv);
187 (void)SvUPGRADE(sv, SVt_PVMG);
3280af22
NIS
188 SvSTASH(sv) = (HV*)SvREFCNT_inc(PL_in_my_stash);
189 PL_sv_objcount++;
c750a3ec 190 }
f472eb5c
GS
191 if (PL_in_my == KEY_our) {
192 (void)SvUPGRADE(sv, SVt_PVGV);
ef75a179 193 GvSTASH(sv) = (HV*)SvREFCNT_inc(PL_curstash ? (SV*)PL_curstash : (SV*)PL_defstash);
77ca0c92 194 SvFLAGS(sv) |= SVpad_OUR;
f472eb5c 195 }
3280af22 196 av_store(PL_comppad_name, off, sv);
65202027 197 SvNVX(sv) = (NV)PAD_MAX;
8990e307 198 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
3280af22
NIS
199 if (!PL_min_intro_pending)
200 PL_min_intro_pending = off;
201 PL_max_intro_pending = off;
93a17b20 202 if (*name == '@')
3280af22 203 av_store(PL_comppad, off, (SV*)newAV());
93a17b20 204 else if (*name == '%')
3280af22
NIS
205 av_store(PL_comppad, off, (SV*)newHV());
206 SvPADMY_on(PL_curpad[off]);
93a17b20
LW
207 return off;
208}
209
94f23f41
GS
210STATIC PADOFFSET
211S_pad_addlex(pTHX_ SV *proto_namesv)
212{
213 SV *namesv = NEWSV(1103,0);
214 PADOFFSET newoff = pad_alloc(OP_PADSV, SVs_PADMY);
215 sv_upgrade(namesv, SVt_PVNV);
216 sv_setpv(namesv, SvPVX(proto_namesv));
217 av_store(PL_comppad_name, newoff, namesv);
218 SvNVX(namesv) = (NV)PL_curcop->cop_seq;
219 SvIVX(namesv) = PAD_MAX; /* A ref, intro immediately */
220 SvFAKE_on(namesv); /* A ref, not a real var */
221 if (SvFLAGS(proto_namesv) & SVpad_OUR) { /* An "our" variable */
222 SvFLAGS(namesv) |= SVpad_OUR;
223 (void)SvUPGRADE(namesv, SVt_PVGV);
224 GvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)GvSTASH(proto_namesv));
225 }
226 if (SvOBJECT(proto_namesv)) { /* A typed var */
227 SvOBJECT_on(namesv);
228 (void)SvUPGRADE(namesv, SVt_PVMG);
229 SvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)SvSTASH(proto_namesv));
230 PL_sv_objcount++;
231 }
232 return newoff;
233}
234
2680586e
GS
235#define FINDLEX_NOSEARCH 1 /* don't search outer contexts */
236
76e3520e 237STATIC PADOFFSET
cea2e8a9 238S_pad_findlex(pTHX_ char *name, PADOFFSET newoff, U32 seq, CV* startcv,
864dbfa3 239 I32 cx_ix, I32 saweval, U32 flags)
93a17b20 240{
11343788 241 dTHR;
748a9306 242 CV *cv;
93a17b20
LW
243 I32 off;
244 SV *sv;
93a17b20 245 register I32 i;
c09156bb 246 register PERL_CONTEXT *cx;
93a17b20 247
748a9306 248 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
4fdae800
PP
249 AV *curlist = CvPADLIST(cv);
250 SV **svp = av_fetch(curlist, 0, FALSE);
748a9306 251 AV *curname;
4fdae800 252
3280af22 253 if (!svp || *svp == &PL_sv_undef)
4633a7c4 254 continue;
748a9306
LW
255 curname = (AV*)*svp;
256 svp = AvARRAY(curname);
93965878 257 for (off = AvFILLp(curname); off > 0; off--) {
748a9306 258 if ((sv = svp[off]) &&
3280af22 259 sv != &PL_sv_undef &&
748a9306 260 seq <= SvIVX(sv) &&
13826f2c 261 seq > I_32(SvNVX(sv)) &&
748a9306
LW
262 strEQ(SvPVX(sv), name))
263 {
5f05dabc
PP
264 I32 depth;
265 AV *oldpad;
266 SV *oldsv;
267
268 depth = CvDEPTH(cv);
269 if (!depth) {
9607fc9c
PP
270 if (newoff) {
271 if (SvFAKE(sv))
272 continue;
4fdae800 273 return 0; /* don't clone from inactive stack frame */
9607fc9c 274 }
5f05dabc
PP
275 depth = 1;
276 }
94f23f41 277 oldpad = (AV*)AvARRAY(curlist)[depth];
5f05dabc 278 oldsv = *av_fetch(oldpad, off, TRUE);
748a9306 279 if (!newoff) { /* Not a mere clone operation. */
94f23f41 280 newoff = pad_addlex(sv);
3280af22 281 if (CvANON(PL_compcv) || SvTYPE(PL_compcv) == SVt_PVFM) {
28757baa 282 /* "It's closures all the way down." */
3280af22 283 CvCLONE_on(PL_compcv);
54310121 284 if (cv == startcv) {
3280af22 285 if (CvANON(PL_compcv))
54310121
PP
286 oldsv = Nullsv; /* no need to keep ref */
287 }
288 else {
28757baa
PP
289 CV *bcv;
290 for (bcv = startcv;
291 bcv && bcv != cv && !CvCLONE(bcv);
6b35e009
GS
292 bcv = CvOUTSIDE(bcv))
293 {
94f23f41
GS
294 if (CvANON(bcv)) {
295 /* install the missing pad entry in intervening
296 * nested subs and mark them cloneable.
297 * XXX fix pad_foo() to not use globals */
298 AV *ocomppad_name = PL_comppad_name;
299 AV *ocomppad = PL_comppad;
300 SV **ocurpad = PL_curpad;
301 AV *padlist = CvPADLIST(bcv);
302 PL_comppad_name = (AV*)AvARRAY(padlist)[0];
303 PL_comppad = (AV*)AvARRAY(padlist)[1];
304 PL_curpad = AvARRAY(PL_comppad);
305 pad_addlex(sv);
306 PL_comppad_name = ocomppad_name;
307 PL_comppad = ocomppad;
308 PL_curpad = ocurpad;
28757baa 309 CvCLONE_on(bcv);
94f23f41 310 }
28757baa 311 else {
6b35e009
GS
312 if (ckWARN(WARN_CLOSURE)
313 && !CvUNIQUE(bcv) && !CvUNIQUE(cv))
314 {
cea2e8a9 315 Perl_warner(aTHX_ WARN_CLOSURE,
44a8e56a 316 "Variable \"%s\" may be unavailable",
28757baa 317 name);
6b35e009 318 }
28757baa
PP
319 break;
320 }
321 }
322 }
323 }
3280af22 324 else if (!CvUNIQUE(PL_compcv)) {
741b6338
GS
325 if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv)
326 && !(SvFLAGS(sv) & SVpad_OUR))
327 {
cea2e8a9 328 Perl_warner(aTHX_ WARN_CLOSURE,
599cee73 329 "Variable \"%s\" will not stay shared", name);
741b6338 330 }
5f05dabc 331 }
748a9306 332 }
3280af22 333 av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv));
748a9306
LW
334 return newoff;
335 }
93a17b20
LW
336 }
337 }
338
2680586e
GS
339 if (flags & FINDLEX_NOSEARCH)
340 return 0;
341
93a17b20
LW
342 /* Nothing in current lexical context--try eval's context, if any.
343 * This is necessary to let the perldb get at lexically scoped variables.
344 * XXX This will also probably interact badly with eval tree caching.
345 */
346
748a9306 347 for (i = cx_ix; i >= 0; i--) {
93a17b20 348 cx = &cxstack[i];
6b35e009 349 switch (CxTYPE(cx)) {
93a17b20 350 default:
748a9306
LW
351 if (i == 0 && saweval) {
352 seq = cxstack[saweval].blk_oldcop->cop_seq;
2680586e 353 return pad_findlex(name, newoff, seq, PL_main_cv, -1, saweval, 0);
748a9306 354 }
93a17b20
LW
355 break;
356 case CXt_EVAL:
44a8e56a
PP
357 switch (cx->blk_eval.old_op_type) {
358 case OP_ENTEREVAL:
6b35e009
GS
359 if (CxREALEVAL(cx))
360 saweval = i;
44a8e56a 361 break;
faa7e5bb 362 case OP_DOFILE:
44a8e56a 363 case OP_REQUIRE:
faa7e5bb 364 /* require/do must have their own scope */
44a8e56a
PP
365 return 0;
366 }
93a17b20 367 break;
7766f137 368 case CXt_FORMAT:
93a17b20
LW
369 case CXt_SUB:
370 if (!saweval)
371 return 0;
372 cv = cx->blk_sub.cv;
3280af22 373 if (PL_debstash && CvSTASH(cv) == PL_debstash) { /* ignore DB'* scope */
748a9306 374 saweval = i; /* so we know where we were called from */
93a17b20 375 continue;
93a17b20 376 }
748a9306 377 seq = cxstack[saweval].blk_oldcop->cop_seq;
2680586e 378 return pad_findlex(name, newoff, seq, cv, i-1, saweval,FINDLEX_NOSEARCH);
93a17b20
LW
379 }
380 }
381
748a9306
LW
382 return 0;
383}
a0d0e21e 384
748a9306 385PADOFFSET
864dbfa3 386Perl_pad_findmy(pTHX_ char *name)
748a9306 387{
11343788 388 dTHR;
748a9306 389 I32 off;
54310121 390 I32 pendoff = 0;
748a9306 391 SV *sv;
3280af22
NIS
392 SV **svp = AvARRAY(PL_comppad_name);
393 U32 seq = PL_cop_seqmax;
6b35e009 394 PERL_CONTEXT *cx;
33b8ce05 395 CV *outside;
748a9306 396
11343788
MB
397#ifdef USE_THREADS
398 /*
399 * Special case to get lexical (and hence per-thread) @_.
400 * XXX I need to find out how to tell at parse-time whether use
401 * of @_ should refer to a lexical (from a sub) or defgv (global
402 * scope and maybe weird sub-ish things like formats). See
403 * startsub in perly.y. It's possible that @_ could be lexical
404 * (at least from subs) even in non-threaded perl.
405 */
406 if (strEQ(name, "@_"))
407 return 0; /* success. (NOT_IN_PAD indicates failure) */
408#endif /* USE_THREADS */
409
748a9306 410 /* The one we're looking for is probably just before comppad_name_fill. */
3280af22 411 for (off = AvFILLp(PL_comppad_name); off > 0; off--) {
a0d0e21e 412 if ((sv = svp[off]) &&
3280af22 413 sv != &PL_sv_undef &&
54310121
PP
414 (!SvIVX(sv) ||
415 (seq <= SvIVX(sv) &&
416 seq > I_32(SvNVX(sv)))) &&
a0d0e21e
LW
417 strEQ(SvPVX(sv), name))
418 {
77ca0c92 419 if (SvIVX(sv) || SvFLAGS(sv) & SVpad_OUR)
54310121
PP
420 return (PADOFFSET)off;
421 pendoff = off; /* this pending def. will override import */
a0d0e21e
LW
422 }
423 }
748a9306 424
33b8ce05
GS
425 outside = CvOUTSIDE(PL_compcv);
426
427 /* Check if if we're compiling an eval'', and adjust seq to be the
428 * eval's seq number. This depends on eval'' having a non-null
429 * CvOUTSIDE() while it is being compiled. The eval'' itself is
1aff0e91
GS
430 * identified by CvEVAL being true and CvGV being null. */
431 if (outside && CvEVAL(PL_compcv) && !CvGV(PL_compcv) && cxstack_ix >= 0) {
6b35e009
GS
432 cx = &cxstack[cxstack_ix];
433 if (CxREALEVAL(cx))
434 seq = cx->blk_oldcop->cop_seq;
435 }
436
748a9306 437 /* See if it's in a nested scope */
2680586e 438 off = pad_findlex(name, 0, seq, outside, cxstack_ix, 0, 0);
54310121
PP
439 if (off) {
440 /* If there is a pending local definition, this new alias must die */
441 if (pendoff)
3280af22 442 SvIVX(AvARRAY(PL_comppad_name)[off]) = seq;
11343788 443 return off; /* pad_findlex returns 0 for failure...*/
54310121 444 }
11343788 445 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
93a17b20
LW
446}
447
448void
864dbfa3 449Perl_pad_leavemy(pTHX_ I32 fill)
93a17b20 450{
0453d815 451 dTHR;
93a17b20 452 I32 off;
3280af22 453 SV **svp = AvARRAY(PL_comppad_name);
93a17b20 454 SV *sv;
3280af22
NIS
455 if (PL_min_intro_pending && fill < PL_min_intro_pending) {
456 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
0453d815
PM
457 if ((sv = svp[off]) && sv != &PL_sv_undef && ckWARN_d(WARN_INTERNAL))
458 Perl_warner(aTHX_ WARN_INTERNAL, "%s never introduced", SvPVX(sv));
8990e307
LW
459 }
460 }
461 /* "Deintroduce" my variables that are leaving with this scope. */
3280af22 462 for (off = AvFILLp(PL_comppad_name); off > fill; off--) {
c53d7c7d 463 if ((sv = svp[off]) && sv != &PL_sv_undef && SvIVX(sv) == PAD_MAX)
3280af22 464 SvIVX(sv) = PL_cop_seqmax;
93a17b20
LW
465 }
466}
467
468PADOFFSET
864dbfa3 469Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype)
79072805 470{
11343788 471 dTHR;
79072805
LW
472 SV *sv;
473 I32 retval;
474
3280af22 475 if (AvARRAY(PL_comppad) != PL_curpad)
cea2e8a9 476 Perl_croak(aTHX_ "panic: pad_alloc");
3280af22 477 if (PL_pad_reset_pending)
a0d0e21e 478 pad_reset();
ed6116ce 479 if (tmptype & SVs_PADMY) {
79072805 480 do {
3280af22 481 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
ed6116ce 482 } while (SvPADBUSY(sv)); /* need a fresh one */
3280af22 483 retval = AvFILLp(PL_comppad);
79072805
LW
484 }
485 else {
3280af22
NIS
486 SV **names = AvARRAY(PL_comppad_name);
487 SSize_t names_fill = AvFILLp(PL_comppad_name);
bbce6d69
PP
488 for (;;) {
489 /*
490 * "foreach" index vars temporarily become aliases to non-"my"
491 * values. Thus we must skip, not just pad values that are
492 * marked as current pad values, but also those with names.
493 */
3280af22
NIS
494 if (++PL_padix <= names_fill &&
495 (sv = names[PL_padix]) && sv != &PL_sv_undef)
bbce6d69 496 continue;
3280af22 497 sv = *av_fetch(PL_comppad, PL_padix, TRUE);
3049cdab
SB
498 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)) &&
499 !IS_PADGV(sv) && !IS_PADCONST(sv))
bbce6d69
PP
500 break;
501 }
3280af22 502 retval = PL_padix;
79072805 503 }
8990e307 504 SvFLAGS(sv) |= tmptype;
3280af22 505 PL_curpad = AvARRAY(PL_comppad);
11343788 506#ifdef USE_THREADS
b900a521
JH
507 DEBUG_X(PerlIO_printf(Perl_debug_log,
508 "0x%"UVxf" Pad 0x%"UVxf" alloc %ld for %s\n",
509 PTR2UV(thr), PTR2UV(PL_curpad),
22c35a8c 510 (long) retval, PL_op_name[optype]));
11343788 511#else
b900a521
JH
512 DEBUG_X(PerlIO_printf(Perl_debug_log,
513 "Pad 0x%"UVxf" alloc %ld for %s\n",
514 PTR2UV(PL_curpad),
22c35a8c 515 (long) retval, PL_op_name[optype]));
11343788 516#endif /* USE_THREADS */
79072805
LW
517 return (PADOFFSET)retval;
518}
519
520SV *
864dbfa3 521Perl_pad_sv(pTHX_ PADOFFSET po)
79072805 522{
11343788
MB
523 dTHR;
524#ifdef USE_THREADS
b900a521 525 DEBUG_X(PerlIO_printf(Perl_debug_log,
f1dbda3d
JH
526 "0x%"UVxf" Pad 0x%"UVxf" sv %"IVdf"\n",
527 PTR2UV(thr), PTR2UV(PL_curpad), (IV)po));
11343788 528#else
79072805 529 if (!po)
cea2e8a9 530 Perl_croak(aTHX_ "panic: pad_sv po");
97835f67
JH
531 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" sv %"IVdf"\n",
532 PTR2UV(PL_curpad), (IV)po));
11343788 533#endif /* USE_THREADS */
3280af22 534 return PL_curpad[po]; /* eventually we'll turn this into a macro */
79072805
LW
535}
536
537void
864dbfa3 538Perl_pad_free(pTHX_ PADOFFSET po)
79072805 539{
11343788 540 dTHR;
3280af22 541 if (!PL_curpad)
a0d0e21e 542 return;
3280af22 543 if (AvARRAY(PL_comppad) != PL_curpad)
cea2e8a9 544 Perl_croak(aTHX_ "panic: pad_free curpad");
79072805 545 if (!po)
cea2e8a9 546 Perl_croak(aTHX_ "panic: pad_free po");
11343788 547#ifdef USE_THREADS
b900a521 548 DEBUG_X(PerlIO_printf(Perl_debug_log,
7766f137 549 "0x%"UVxf" Pad 0x%"UVxf" free %"IVdf"\n",
f1dbda3d 550 PTR2UV(thr), PTR2UV(PL_curpad), (IV)po));
11343788 551#else
97835f67
JH
552 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" free %"IVdf"\n",
553 PTR2UV(PL_curpad), (IV)po));
11343788 554#endif /* USE_THREADS */
2aa1bedc 555 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef) {
3280af22 556 SvPADTMP_off(PL_curpad[po]);
2aa1bedc
GS
557#ifdef USE_ITHREADS
558 SvREADONLY_off(PL_curpad[po]); /* could be a freed constant */
559#endif
560 }
3280af22
NIS
561 if ((I32)po < PL_padix)
562 PL_padix = po - 1;
79072805
LW
563}
564
565void
864dbfa3 566Perl_pad_swipe(pTHX_ PADOFFSET po)
79072805 567{
11343788 568 dTHR;
3280af22 569 if (AvARRAY(PL_comppad) != PL_curpad)
cea2e8a9 570 Perl_croak(aTHX_ "panic: pad_swipe curpad");
79072805 571 if (!po)
cea2e8a9 572 Perl_croak(aTHX_ "panic: pad_swipe po");
11343788 573#ifdef USE_THREADS
b900a521 574 DEBUG_X(PerlIO_printf(Perl_debug_log,
f1dbda3d
JH
575 "0x%"UVxf" Pad 0x%"UVxf" swipe %"IVdf"\n",
576 PTR2UV(thr), PTR2UV(PL_curpad), (IV)po));
11343788 577#else
97835f67
JH
578 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" swipe %"IVdf"\n",
579 PTR2UV(PL_curpad), (IV)po));
11343788 580#endif /* USE_THREADS */
3280af22
NIS
581 SvPADTMP_off(PL_curpad[po]);
582 PL_curpad[po] = NEWSV(1107,0);
583 SvPADTMP_on(PL_curpad[po]);
584 if ((I32)po < PL_padix)
585 PL_padix = po - 1;
79072805
LW
586}
587
d9bb4600
GS
588/* XXX pad_reset() is currently disabled because it results in serious bugs.
589 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
590 * on the stack by OPs that use them, there are several ways to get an alias
591 * to a shared TARG. Such an alias will change randomly and unpredictably.
592 * We avoid doing this until we can think of a Better Way.
593 * GSAR 97-10-29 */
79072805 594void
864dbfa3 595Perl_pad_reset(pTHX)
79072805 596{
d9bb4600 597#ifdef USE_BROKEN_PAD_RESET
11343788 598 dTHR;
79072805
LW
599 register I32 po;
600
6b88bc9c 601 if (AvARRAY(PL_comppad) != PL_curpad)
cea2e8a9 602 Perl_croak(aTHX_ "panic: pad_reset curpad");
11343788 603#ifdef USE_THREADS
b900a521
JH
604 DEBUG_X(PerlIO_printf(Perl_debug_log,
605 "0x%"UVxf" Pad 0x%"UVxf" reset\n",
606 PTR2UV(thr), PTR2UV(PL_curpad)));
11343788 607#else
b900a521
JH
608 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" reset\n",
609 PTR2UV(PL_curpad)));
11343788 610#endif /* USE_THREADS */
6b88bc9c
GS
611 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
612 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
613 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
614 SvPADTMP_off(PL_curpad[po]);
748a9306 615 }
6b88bc9c 616 PL_padix = PL_padix_floor;
79072805 617 }
d9bb4600 618#endif
3280af22 619 PL_pad_reset_pending = FALSE;
79072805
LW
620}
621
a863c7d1 622#ifdef USE_THREADS
54b9620d 623/* find_threadsv is not reentrant */
a863c7d1 624PADOFFSET
864dbfa3 625Perl_find_threadsv(pTHX_ const char *name)
a863c7d1
MB
626{
627 dTHR;
628 char *p;
629 PADOFFSET key;
554b3eca 630 SV **svp;
54b9620d 631 /* We currently only handle names of a single character */
533c011a 632 p = strchr(PL_threadsv_names, *name);
a863c7d1
MB
633 if (!p)
634 return NOT_IN_PAD;
533c011a 635 key = p - PL_threadsv_names;
2d8e6c8d 636 MUTEX_LOCK(&thr->mutex);
54b9620d 637 svp = av_fetch(thr->threadsv, key, FALSE);
2d8e6c8d
GS
638 if (svp)
639 MUTEX_UNLOCK(&thr->mutex);
640 else {
554b3eca 641 SV *sv = NEWSV(0, 0);
54b9620d 642 av_store(thr->threadsv, key, sv);
940cb80d 643 thr->threadsvp = AvARRAY(thr->threadsv);
2d8e6c8d 644 MUTEX_UNLOCK(&thr->mutex);
554b3eca
MB
645 /*
646 * Some magic variables used to be automagically initialised
647 * in gv_fetchpv. Those which are now per-thread magicals get
648 * initialised here instead.
649 */
650 switch (*name) {
54b9620d
MB
651 case '_':
652 break;
554b3eca
MB
653 case ';':
654 sv_setpv(sv, "\034");
1c846c1f 655 sv_magic(sv, 0, 0, name, 1);
554b3eca 656 break;
c277df42
IZ
657 case '&':
658 case '`':
659 case '\'':
533c011a 660 PL_sawampersand = TRUE;
a3f914c5
GS
661 /* FALL THROUGH */
662 case '1':
663 case '2':
664 case '3':
665 case '4':
666 case '5':
667 case '6':
668 case '7':
669 case '8':
670 case '9':
c277df42 671 SvREADONLY_on(sv);
d8b5173a 672 /* FALL THROUGH */
067391ea
GS
673
674 /* XXX %! tied to Errno.pm needs to be added here.
675 * See gv_fetchpv(). */
676 /* case '!': */
677
54b9620d 678 default:
1c846c1f 679 sv_magic(sv, 0, 0, name, 1);
554b3eca 680 }
bf49b057 681 DEBUG_S(PerlIO_printf(Perl_error_log,
54b9620d 682 "find_threadsv: new SV %p for $%s%c\n",
554b3eca
MB
683 sv, (*name < 32) ? "^" : "",
684 (*name < 32) ? toCTRL(*name) : *name));
a863c7d1
MB
685 }
686 return key;
687}
688#endif /* USE_THREADS */
689
79072805
LW
690/* Destructor */
691
692void
864dbfa3 693Perl_op_free(pTHX_ OP *o)
79072805 694{
85e6fe83 695 register OP *kid, *nextkid;
acb36ea4 696 OPCODE type;
79072805 697
5dc0d613 698 if (!o || o->op_seq == (U16)-1)
79072805
LW
699 return;
700
7934575e
GS
701 if (o->op_private & OPpREFCOUNTED) {
702 switch (o->op_type) {
703 case OP_LEAVESUB:
704 case OP_LEAVESUBLV:
705 case OP_LEAVEEVAL:
706 case OP_LEAVE:
707 case OP_SCOPE:
708 case OP_LEAVEWRITE:
709 OP_REFCNT_LOCK;
710 if (OpREFCNT_dec(o)) {
711 OP_REFCNT_UNLOCK;
712 return;
713 }
714 OP_REFCNT_UNLOCK;
715 break;
716 default:
717 break;
718 }
719 }
720
11343788
MB
721 if (o->op_flags & OPf_KIDS) {
722 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
85e6fe83 723 nextkid = kid->op_sibling; /* Get before next freeing kid */
79072805 724 op_free(kid);
85e6fe83 725 }
79072805 726 }
acb36ea4
GS
727 type = o->op_type;
728 if (type == OP_NULL)
729 type = o->op_targ;
730
731 /* COP* is not cleared by op_clear() so that we may track line
732 * numbers etc even after null() */
733 if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
734 cop_free((COP*)o);
735
736 op_clear(o);
737
738#ifdef PL_OP_SLAB_ALLOC
739 if ((char *) o == PL_OpPtr)
740 {
741 }
742#else
743 Safefree(o);
744#endif
745}
79072805 746
acb36ea4
GS
747STATIC void
748S_op_clear(pTHX_ OP *o)
749{
11343788 750 switch (o->op_type) {
acb36ea4
GS
751 case OP_NULL: /* Was holding old type, if any. */
752 case OP_ENTEREVAL: /* Was holding hints. */
753#ifdef USE_THREADS
754 case OP_THREADSV: /* Was holding index into thr->threadsv AV. */
755#endif
756 o->op_targ = 0;
a0d0e21e 757 break;
554b3eca 758#ifdef USE_THREADS
8dd3ba40
SM
759 case OP_ENTERITER:
760 if (!(o->op_flags & OPf_SPECIAL))
761 break;
762 /* FALL THROUGH */
554b3eca 763#endif /* USE_THREADS */
a6006777 764 default:
ac4c12e7 765 if (!(o->op_flags & OPf_REF)
0b94c7bb 766 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
a6006777
PP
767 break;
768 /* FALL THROUGH */
463ee0b2 769 case OP_GVSV:
79072805 770 case OP_GV:
a6006777 771 case OP_AELEMFAST:
350de78d 772#ifdef USE_ITHREADS
971a9dd3
GS
773 if (cPADOPo->op_padix > 0) {
774 if (PL_curpad) {
638eceb6 775 GV *gv = cGVOPo_gv;
971a9dd3
GS
776 pad_swipe(cPADOPo->op_padix);
777 /* No GvIN_PAD_off(gv) here, because other references may still
778 * exist on the pad */
779 SvREFCNT_dec(gv);
780 }
781 cPADOPo->op_padix = 0;
782 }
350de78d 783#else
971a9dd3 784 SvREFCNT_dec(cSVOPo->op_sv);
7934575e 785 cSVOPo->op_sv = Nullsv;
350de78d 786#endif
79072805 787 break;
a1ae71d2 788 case OP_METHOD_NAMED:
79072805 789 case OP_CONST:
11343788 790 SvREFCNT_dec(cSVOPo->op_sv);
acb36ea4 791 cSVOPo->op_sv = Nullsv;
79072805 792 break;
748a9306
LW
793 case OP_GOTO:
794 case OP_NEXT:
795 case OP_LAST:
796 case OP_REDO:
11343788 797 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
748a9306
LW
798 break;
799 /* FALL THROUGH */
a0d0e21e 800 case OP_TRANS:
acb36ea4 801 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
a0ed51b3 802 SvREFCNT_dec(cSVOPo->op_sv);
acb36ea4
GS
803 cSVOPo->op_sv = Nullsv;
804 }
805 else {
a0ed51b3 806 Safefree(cPVOPo->op_pv);
acb36ea4
GS
807 cPVOPo->op_pv = Nullch;
808 }
a0d0e21e
LW
809 break;
810 case OP_SUBST:
11343788 811 op_free(cPMOPo->op_pmreplroot);
971a9dd3 812 goto clear_pmop;
748a9306 813 case OP_PUSHRE:
971a9dd3
GS
814#ifdef USE_ITHREADS
815 if ((PADOFFSET)cPMOPo->op_pmreplroot) {
816 if (PL_curpad) {
817 GV *gv = (GV*)PL_curpad[(PADOFFSET)cPMOPo->op_pmreplroot];
818 pad_swipe((PADOFFSET)cPMOPo->op_pmreplroot);
819 /* No GvIN_PAD_off(gv) here, because other references may still
820 * exist on the pad */
821 SvREFCNT_dec(gv);
822 }
823 }
824#else
825 SvREFCNT_dec((SV*)cPMOPo->op_pmreplroot);
826#endif
827 /* FALL THROUGH */
a0d0e21e 828 case OP_MATCH:
8782bef2 829 case OP_QR:
971a9dd3
GS
830clear_pmop:
831 cPMOPo->op_pmreplroot = Nullop;
c277df42 832 ReREFCNT_dec(cPMOPo->op_pmregexp);
acb36ea4 833 cPMOPo->op_pmregexp = (REGEXP*)NULL;
a0d0e21e 834 break;
79072805
LW
835 }
836
743e66e6 837 if (o->op_targ > 0) {
11343788 838 pad_free(o->op_targ);
743e66e6
GS
839 o->op_targ = 0;
840 }
79072805
LW
841}
842
76e3520e 843STATIC void
3eb57f73
HS
844S_cop_free(pTHX_ COP* cop)
845{
846 Safefree(cop->cop_label);
57843af0 847#ifdef USE_ITHREADS
f4dd75d9
GS
848 Safefree(CopFILE(cop)); /* XXX share in a pvtable? */
849 Safefree(CopSTASHPV(cop)); /* XXX share in a pvtable? */
57843af0 850#else
11faa288 851 /* NOTE: COP.cop_stash is not refcounted */
cc49e20b 852 SvREFCNT_dec(CopFILEGV(cop));
57843af0 853#endif
0453d815 854 if (! specialWARN(cop->cop_warnings))
3eb57f73 855 SvREFCNT_dec(cop->cop_warnings);
ac27b0f5
NIS
856 if (! specialCopIO(cop->cop_io))
857 SvREFCNT_dec(cop->cop_io);
3eb57f73
HS
858}
859
860STATIC void
cea2e8a9 861S_null(pTHX_ OP *o)
8990e307 862{
acb36ea4
GS
863 if (o->op_type == OP_NULL)
864 return;
865 op_clear(o);
11343788
MB
866 o->op_targ = o->op_type;
867 o->op_type = OP_NULL;
22c35a8c 868 o->op_ppaddr = PL_ppaddr[OP_NULL];
8990e307
LW
869}
870
79072805
LW
871/* Contextualizers */
872
463ee0b2 873#define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
79072805
LW
874
875OP *
864dbfa3 876Perl_linklist(pTHX_ OP *o)
79072805
LW
877{
878 register OP *kid;
879
11343788
MB
880 if (o->op_next)
881 return o->op_next;
79072805
LW
882
883 /* establish postfix order */
11343788
MB
884 if (cUNOPo->op_first) {
885 o->op_next = LINKLIST(cUNOPo->op_first);
886 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
79072805
LW
887 if (kid->op_sibling)
888 kid->op_next = LINKLIST(kid->op_sibling);
889 else
11343788 890 kid->op_next = o;
79072805
LW
891 }
892 }
893 else
11343788 894 o->op_next = o;
79072805 895
11343788 896 return o->op_next;
79072805
LW
897}
898
899OP *
864dbfa3 900Perl_scalarkids(pTHX_ OP *o)
79072805
LW
901{
902 OP *kid;
11343788
MB
903 if (o && o->op_flags & OPf_KIDS) {
904 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805
LW
905 scalar(kid);
906 }
11343788 907 return o;
79072805
LW
908}
909
76e3520e 910STATIC OP *
cea2e8a9 911S_scalarboolean(pTHX_ OP *o)
8990e307 912{
d008e5eb 913 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
0f15f207 914 dTHR;
d008e5eb 915 if (ckWARN(WARN_SYNTAX)) {
57843af0 916 line_t oldline = CopLINE(PL_curcop);
a0d0e21e 917
d008e5eb 918 if (PL_copline != NOLINE)
57843af0 919 CopLINE_set(PL_curcop, PL_copline);
cea2e8a9 920 Perl_warner(aTHX_ WARN_SYNTAX, "Found = in conditional, should be ==");
57843af0 921 CopLINE_set(PL_curcop, oldline);
d008e5eb 922 }
a0d0e21e 923 }
11343788 924 return scalar(o);
8990e307
LW
925}
926
927OP *
864dbfa3 928Perl_scalar(pTHX_ OP *o)
79072805
LW
929{
930 OP *kid;
931
a0d0e21e 932 /* assumes no premature commitment */
3280af22 933 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
5dc0d613 934 || o->op_type == OP_RETURN)
7e363e51 935 {
11343788 936 return o;
7e363e51 937 }
79072805 938
5dc0d613 939 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
79072805 940
11343788 941 switch (o->op_type) {
79072805 942 case OP_REPEAT:
11343788
MB
943 if (o->op_private & OPpREPEAT_DOLIST)
944 null(((LISTOP*)cBINOPo->op_first)->op_first);
945 scalar(cBINOPo->op_first);
8990e307 946 break;
79072805
LW
947 case OP_OR:
948 case OP_AND:
949 case OP_COND_EXPR:
11343788 950 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
8990e307 951 scalar(kid);
79072805 952 break;
a0d0e21e 953 case OP_SPLIT:
11343788 954 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
a0d0e21e
LW
955 if (!kPMOP->op_pmreplroot)
956 deprecate("implicit split to @_");
957 }
958 /* FALL THROUGH */
79072805 959 case OP_MATCH:
8782bef2 960 case OP_QR:
79072805
LW
961 case OP_SUBST:
962 case OP_NULL:
8990e307 963 default:
11343788
MB
964 if (o->op_flags & OPf_KIDS) {
965 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
8990e307
LW
966 scalar(kid);
967 }
79072805
LW
968 break;
969 case OP_LEAVE:
970 case OP_LEAVETRY:
5dc0d613 971 kid = cLISTOPo->op_first;
54310121 972 scalar(kid);
155aba94 973 while ((kid = kid->op_sibling)) {
54310121
PP
974 if (kid->op_sibling)
975 scalarvoid(kid);
976 else
977 scalar(kid);
978 }
3280af22 979 WITH_THR(PL_curcop = &PL_compiling);
54310121 980 break;
748a9306 981 case OP_SCOPE:
79072805 982 case OP_LINESEQ:
8990e307 983 case OP_LIST:
11343788 984 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
79072805
LW
985 if (kid->op_sibling)
986 scalarvoid(kid);
987 else
988 scalar(kid);
989 }
3280af22 990 WITH_THR(PL_curcop = &PL_compiling);
79072805
LW
991 break;
992 }
11343788 993 return o;
79072805
LW
994}
995
996OP *
864dbfa3 997Perl_scalarvoid(pTHX_ OP *o)
79072805
LW
998{
999 OP *kid;
8990e307
LW
1000 char* useless = 0;
1001 SV* sv;
2ebea0a1
GS
1002 U8 want;
1003
acb36ea4
GS
1004 if (o->op_type == OP_NEXTSTATE
1005 || o->op_type == OP_SETSTATE
1006 || o->op_type == OP_DBSTATE
1007 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1008 || o->op_targ == OP_SETSTATE
1009 || o->op_targ == OP_DBSTATE)))
2ebea0a1
GS
1010 {
1011 dTHR;
1012 PL_curcop = (COP*)o; /* for warning below */
1013 }
79072805 1014
54310121 1015 /* assumes no premature commitment */
2ebea0a1
GS
1016 want = o->op_flags & OPf_WANT;
1017 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
5dc0d613 1018 || o->op_type == OP_RETURN)
7e363e51 1019 {
11343788 1020 return o;
7e363e51 1021 }
79072805 1022
b162f9ea 1023 if ((o->op_private & OPpTARGET_MY)
7e363e51
GS
1024 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1025 {
b162f9ea 1026 return scalar(o); /* As if inside SASSIGN */
7e363e51 1027 }
1c846c1f 1028
5dc0d613 1029 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
79072805 1030
11343788 1031 switch (o->op_type) {
79072805 1032 default:
22c35a8c 1033 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
8990e307 1034 break;
36477c24
PP
1035 /* FALL THROUGH */
1036 case OP_REPEAT:
11343788 1037 if (o->op_flags & OPf_STACKED)
8990e307 1038 break;
5d82c453
GA
1039 goto func_ops;
1040 case OP_SUBSTR:
1041 if (o->op_private == 4)
1042 break;
8990e307
LW
1043 /* FALL THROUGH */
1044 case OP_GVSV:
1045 case OP_WANTARRAY:
1046 case OP_GV:
1047 case OP_PADSV:
1048 case OP_PADAV:
1049 case OP_PADHV:
1050 case OP_PADANY:
1051 case OP_AV2ARYLEN:
8990e307 1052 case OP_REF:
a0d0e21e
LW
1053 case OP_REFGEN:
1054 case OP_SREFGEN:
8990e307
LW
1055 case OP_DEFINED:
1056 case OP_HEX:
1057 case OP_OCT:
1058 case OP_LENGTH:
8990e307
LW
1059 case OP_VEC:
1060 case OP_INDEX:
1061 case OP_RINDEX:
1062 case OP_SPRINTF:
1063 case OP_AELEM:
1064 case OP_AELEMFAST:
1065 case OP_ASLICE:
8990e307
LW
1066 case OP_HELEM:
1067 case OP_HSLICE:
1068 case OP_UNPACK:
1069 case OP_PACK:
8990e307
LW
1070 case OP_JOIN:
1071 case OP_LSLICE:
1072 case OP_ANONLIST:
1073 case OP_ANONHASH:
1074 case OP_SORT:
1075 case OP_REVERSE:
1076 case OP_RANGE:
1077 case OP_FLIP:
1078 case OP_FLOP:
1079 case OP_CALLER:
1080 case OP_FILENO:
1081 case OP_EOF:
1082 case OP_TELL:
1083 case OP_GETSOCKNAME:
1084 case OP_GETPEERNAME:
1085 case OP_READLINK:
1086 case OP_TELLDIR:
1087 case OP_GETPPID:
1088 case OP_GETPGRP:
1089 case OP_GETPRIORITY:
1090 case OP_TIME:
1091 case OP_TMS:
1092 case OP_LOCALTIME:
1093 case OP_GMTIME:
1094 case OP_GHBYNAME:
1095 case OP_GHBYADDR:
1096 case OP_GHOSTENT:
1097 case OP_GNBYNAME:
1098 case OP_GNBYADDR:
1099 case OP_GNETENT:
1100 case OP_GPBYNAME:
1101 case OP_GPBYNUMBER:
1102 case OP_GPROTOENT:
1103 case OP_GSBYNAME:
1104 case OP_GSBYPORT:
1105 case OP_GSERVENT:
1106 case OP_GPWNAM:
1107 case OP_GPWUID:
1108 case OP_GGRNAM:
1109 case OP_GGRGID:
1110 case OP_GETLOGIN:
5d82c453 1111 func_ops:
64aac5a9 1112 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
22c35a8c 1113 useless = PL_op_desc[o->op_type];
8990e307
LW
1114 break;
1115
1116 case OP_RV2GV:
1117 case OP_RV2SV:
1118 case OP_RV2AV:
1119 case OP_RV2HV:
192587c2 1120 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
11343788 1121 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
8990e307
LW
1122 useless = "a variable";
1123 break;
79072805
LW
1124
1125 case OP_CONST:
7766f137 1126 sv = cSVOPo_sv;
7a52d87a
GS
1127 if (cSVOPo->op_private & OPpCONST_STRICT)
1128 no_bareword_allowed(o);
1129 else {
d008e5eb
GS
1130 dTHR;
1131 if (ckWARN(WARN_VOID)) {
1132 useless = "a constant";
1133 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1134 useless = 0;
1135 else if (SvPOK(sv)) {
1136 if (strnEQ(SvPVX(sv), "di", 2) ||
1137 strnEQ(SvPVX(sv), "ds", 2) ||
1138 strnEQ(SvPVX(sv), "ig", 2))
1139 useless = 0;
1140 }
8990e307
LW
1141 }
1142 }
acb36ea4 1143 null(o); /* don't execute or even remember it */
79072805
LW
1144 break;
1145
1146 case OP_POSTINC:
11343788 1147 o->op_type = OP_PREINC; /* pre-increment is faster */
22c35a8c 1148 o->op_ppaddr = PL_ppaddr[OP_PREINC];
79072805
LW
1149 break;
1150
1151 case OP_POSTDEC:
11343788 1152 o->op_type = OP_PREDEC; /* pre-decrement is faster */
22c35a8c 1153 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
79072805
LW
1154 break;
1155
79072805
LW
1156 case OP_OR:
1157 case OP_AND:
1158 case OP_COND_EXPR:
11343788 1159 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
79072805
LW
1160 scalarvoid(kid);
1161 break;
5aabfad6 1162
a0d0e21e 1163 case OP_NULL:
11343788 1164 if (o->op_flags & OPf_STACKED)
a0d0e21e 1165 break;
5aabfad6 1166 /* FALL THROUGH */
2ebea0a1
GS
1167 case OP_NEXTSTATE:
1168 case OP_DBSTATE:
79072805
LW
1169 case OP_ENTERTRY:
1170 case OP_ENTER:
11343788 1171 if (!(o->op_flags & OPf_KIDS))
79072805 1172 break;
54310121 1173 /* FALL THROUGH */
463ee0b2 1174 case OP_SCOPE:
79072805
LW
1175 case OP_LEAVE:
1176 case OP_LEAVETRY:
a0d0e21e 1177 case OP_LEAVELOOP:
79072805 1178 case OP_LINESEQ:
79072805 1179 case OP_LIST:
11343788 1180 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805
LW
1181 scalarvoid(kid);
1182 break;
c90c0ff4 1183 case OP_ENTEREVAL:
5196be3e 1184 scalarkids(o);
c90c0ff4 1185 break;
5aabfad6 1186 case OP_REQUIRE:
c90c0ff4 1187 /* all requires must return a boolean value */
5196be3e 1188 o->op_flags &= ~OPf_WANT;
d6483035
GS
1189 /* FALL THROUGH */
1190 case OP_SCALAR:
5196be3e 1191 return scalar(o);
a0d0e21e 1192 case OP_SPLIT:
11343788 1193 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
a0d0e21e
LW
1194 if (!kPMOP->op_pmreplroot)
1195 deprecate("implicit split to @_");
1196 }
1197 break;
79072805 1198 }
d008e5eb
GS
1199 if (useless) {
1200 dTHR;
1201 if (ckWARN(WARN_VOID))
cea2e8a9 1202 Perl_warner(aTHX_ WARN_VOID, "Useless use of %s in void context", useless);
d008e5eb 1203 }
11343788 1204 return o;
79072805
LW
1205}
1206
1207OP *
864dbfa3 1208Perl_listkids(pTHX_ OP *o)
79072805
LW
1209{
1210 OP *kid;
11343788
MB
1211 if (o && o->op_flags & OPf_KIDS) {
1212 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805
LW
1213 list(kid);
1214 }
11343788 1215 return o;
79072805
LW
1216}
1217
1218OP *
864dbfa3 1219Perl_list(pTHX_ OP *o)
79072805
LW
1220{
1221 OP *kid;
1222
a0d0e21e 1223 /* assumes no premature commitment */
3280af22 1224 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
5dc0d613 1225 || o->op_type == OP_RETURN)
7e363e51 1226 {
11343788 1227 return o;
7e363e51 1228 }
79072805 1229
b162f9ea 1230 if ((o->op_private & OPpTARGET_MY)
7e363e51
GS
1231 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1232 {
b162f9ea 1233 return o; /* As if inside SASSIGN */
7e363e51 1234 }
1c846c1f 1235
5dc0d613 1236 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
79072805 1237
11343788 1238 switch (o->op_type) {
79072805
LW
1239 case OP_FLOP:
1240 case OP_REPEAT:
11343788 1241 list(cBINOPo->op_first);
79072805
LW
1242 break;
1243 case OP_OR:
1244 case OP_AND:
1245 case OP_COND_EXPR:
11343788 1246 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
79072805
LW
1247 list(kid);
1248 break;
1249 default:
1250 case OP_MATCH:
8782bef2 1251 case OP_QR:
79072805
LW
1252 case OP_SUBST:
1253 case OP_NULL:
11343788 1254 if (!(o->op_flags & OPf_KIDS))
79072805 1255 break;
11343788
MB
1256 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1257 list(cBINOPo->op_first);
1258 return gen_constant_list(o);
79072805
LW
1259 }
1260 case OP_LIST:
11343788 1261 listkids(o);
79072805
LW
1262 break;
1263 case OP_LEAVE:
1264 case OP_LEAVETRY:
5dc0d613 1265 kid = cLISTOPo->op_first;
54310121 1266 list(kid);
155aba94 1267 while ((kid = kid->op_sibling)) {
54310121
PP
1268 if (kid->op_sibling)
1269 scalarvoid(kid);
1270 else
1271 list(kid);
1272 }
3280af22 1273 WITH_THR(PL_curcop = &PL_compiling);
54310121 1274 break;
748a9306 1275 case OP_SCOPE:
79072805 1276 case OP_LINESEQ:
11343788 1277 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
79072805
LW
1278 if (kid->op_sibling)
1279 scalarvoid(kid);
1280 else
1281 list(kid);
1282 }
3280af22 1283 WITH_THR(PL_curcop = &PL_compiling);
79072805 1284 break;
c90c0ff4
PP
1285 case OP_REQUIRE:
1286 /* all requires must return a boolean value */
5196be3e
MB
1287 o->op_flags &= ~OPf_WANT;
1288 return scalar(o);
79072805 1289 }
11343788 1290 return o;
79072805
LW
1291}
1292
1293OP *
864dbfa3 1294Perl_scalarseq(pTHX_ OP *o)
79072805
LW
1295{
1296 OP *kid;
1297
11343788
MB
1298 if (o) {
1299 if (o->op_type == OP_LINESEQ ||
1300 o->op_type == OP_SCOPE ||
1301 o->op_type == OP_LEAVE ||
1302 o->op_type == OP_LEAVETRY)
463ee0b2 1303 {
0f15f207 1304 dTHR;
11343788 1305 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
ed6116ce 1306 if (kid->op_sibling) {
463ee0b2 1307 scalarvoid(kid);
ed6116ce 1308 }
463ee0b2 1309 }
3280af22 1310 PL_curcop = &PL_compiling;
79072805 1311 }
11343788 1312 o->op_flags &= ~OPf_PARENS;
3280af22 1313 if (PL_hints & HINT_BLOCK_SCOPE)
11343788 1314 o->op_flags |= OPf_PARENS;
79072805 1315 }
8990e307 1316 else
11343788
MB
1317 o = newOP(OP_STUB, 0);
1318 return o;
79072805
LW
1319}
1320
76e3520e 1321STATIC OP *
cea2e8a9 1322S_modkids(pTHX_ OP *o, I32 type)
79072805
LW
1323{
1324 OP *kid;
11343788
MB
1325 if (o && o->op_flags & OPf_KIDS) {
1326 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
463ee0b2 1327 mod(kid, type);
79072805 1328 }
11343788 1329 return o;
79072805
LW
1330}
1331
79072805 1332OP *
864dbfa3 1333Perl_mod(pTHX_ OP *o, I32 type)
79072805 1334{
11343788 1335 dTHR;
79072805 1336 OP *kid;
2d8e6c8d 1337 STRLEN n_a;
79072805 1338
3280af22 1339 if (!o || PL_error_count)
11343788 1340 return o;
79072805 1341
b162f9ea 1342 if ((o->op_private & OPpTARGET_MY)
7e363e51
GS
1343 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1344 {
b162f9ea 1345 return o;
7e363e51 1346 }
1c846c1f 1347
11343788 1348 switch (o->op_type) {
68dc0745 1349 case OP_UNDEF:
3280af22 1350 PL_modcount++;
5dc0d613 1351 return o;
a0d0e21e 1352 case OP_CONST:
11343788 1353 if (!(o->op_private & (OPpCONST_ARYBASE)))
a0d0e21e 1354 goto nomod;
3280af22 1355 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
7766f137 1356 PL_compiling.cop_arybase = (I32)SvIV(cSVOPx(PL_eval_start)->op_sv);
3280af22 1357 PL_eval_start = 0;
a0d0e21e
LW
1358 }
1359 else if (!type) {
3280af22
NIS
1360 SAVEI32(PL_compiling.cop_arybase);
1361 PL_compiling.cop_arybase = 0;
a0d0e21e
LW
1362 }
1363 else if (type == OP_REFGEN)
1364 goto nomod;
1365 else
cea2e8a9 1366 Perl_croak(aTHX_ "That use of $[ is unsupported");
a0d0e21e 1367 break;
5f05dabc 1368 case OP_STUB:
5196be3e 1369 if (o->op_flags & OPf_PARENS)
5f05dabc
PP
1370 break;
1371 goto nomod;
a0d0e21e
LW
1372 case OP_ENTERSUB:
1373 if ((type == OP_UNDEF || type == OP_REFGEN) &&
11343788
MB
1374 !(o->op_flags & OPf_STACKED)) {
1375 o->op_type = OP_RV2CV; /* entersub => rv2cv */
22c35a8c 1376 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
11343788
MB
1377 assert(cUNOPo->op_first->op_type == OP_NULL);
1378 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
79072805
LW
1379 break;
1380 }
cd06dffe
GS
1381 else { /* lvalue subroutine call */
1382 o->op_private |= OPpLVAL_INTRO;
4978d6d9 1383 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
cd06dffe
GS
1384 /* Backward compatibility mode: */
1385 o->op_private |= OPpENTERSUB_INARGS;
1386 break;
1387 }
1388 else { /* Compile-time error message: */
1389 OP *kid = cUNOPo->op_first;
1390 CV *cv;
1391 OP *okid;
1392
1393 if (kid->op_type == OP_PUSHMARK)
1394 goto skip_kids;
1395 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1396 Perl_croak(aTHX_
1397 "panic: unexpected lvalue entersub "
1398 "args: type/targ %ld:%ld",
1399 (long)kid->op_type,kid->op_targ);
1400 kid = kLISTOP->op_first;
1401 skip_kids:
1402 while (kid->op_sibling)
1403 kid = kid->op_sibling;
1404 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1405 /* Indirect call */
1406 if (kid->op_type == OP_METHOD_NAMED
1407 || kid->op_type == OP_METHOD)
1408 {
87d7fd28 1409 UNOP *newop;
cd06dffe
GS
1410
1411 if (kid->op_sibling || kid->op_next != kid) {
1412 yyerror("panic: unexpected optree near method call");
1413 break;
1414 }
1415
87d7fd28 1416 NewOp(1101, newop, 1, UNOP);
349fd7b7
GS
1417 newop->op_type = OP_RV2CV;
1418 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
87d7fd28
GS
1419 newop->op_first = Nullop;
1420 newop->op_next = (OP*)newop;
1421 kid->op_sibling = (OP*)newop;
349fd7b7 1422 newop->op_private |= OPpLVAL_INTRO;
cd06dffe
GS
1423 break;
1424 }
1c846c1f 1425
cd06dffe
GS
1426 if (kid->op_type != OP_RV2CV)
1427 Perl_croak(aTHX_
1428 "panic: unexpected lvalue entersub "
1429 "entry via type/targ %ld:%ld",
1430 (long)kid->op_type,kid->op_targ);
1431 kid->op_private |= OPpLVAL_INTRO;
1432 break; /* Postpone until runtime */
1433 }
1434
1435 okid = kid;
1436 kid = kUNOP->op_first;
1437 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1438 kid = kUNOP->op_first;
1439 if (kid->op_type == OP_NULL)
1440 Perl_croak(aTHX_
1441 "Unexpected constant lvalue entersub "
1442 "entry via type/targ %ld:%ld",
1443 (long)kid->op_type,kid->op_targ);
1444 if (kid->op_type != OP_GV) {
1445 /* Restore RV2CV to check lvalueness */
1446 restore_2cv:
1447 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1448 okid->op_next = kid->op_next;
1449 kid->op_next = okid;
1450 }
1451 else
1452 okid->op_next = Nullop;
1453 okid->op_type = OP_RV2CV;
1454 okid->op_targ = 0;
1455 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1456 okid->op_private |= OPpLVAL_INTRO;
1457 break;
1458 }
1459
638eceb6 1460 cv = GvCV(kGVOP_gv);
1c846c1f 1461 if (!cv)
cd06dffe
GS
1462 goto restore_2cv;
1463 if (CvLVALUE(cv))
1464 break;
1465 }
1466 }
79072805
LW
1467 /* FALL THROUGH */
1468 default:
a0d0e21e
LW
1469 nomod:
1470 /* grep, foreach, subcalls, refgen */
1471 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1472 break;
cea2e8a9 1473 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
638bc118 1474 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
cd06dffe
GS
1475 ? "do block"
1476 : (o->op_type == OP_ENTERSUB
1477 ? "non-lvalue subroutine call"
1478 : PL_op_desc[o->op_type])),
22c35a8c 1479 type ? PL_op_desc[type] : "local"));
11343788 1480 return o;
79072805 1481
a0d0e21e
LW
1482 case OP_PREINC:
1483 case OP_PREDEC:
1484 case OP_POW:
1485 case OP_MULTIPLY:
1486 case OP_DIVIDE:
1487 case OP_MODULO:
1488 case OP_REPEAT:
1489 case OP_ADD:
1490 case OP_SUBTRACT:
1491 case OP_CONCAT:
1492 case OP_LEFT_SHIFT:
1493 case OP_RIGHT_SHIFT:
1494 case OP_BIT_AND:
1495 case OP_BIT_XOR:
1496 case OP_BIT_OR:
1497 case OP_I_MULTIPLY:
1498 case OP_I_DIVIDE:
1499 case OP_I_MODULO:
1500 case OP_I_ADD:
1501 case OP_I_SUBTRACT:
11343788 1502 if (!(o->op_flags & OPf_STACKED))
a0d0e21e 1503 goto nomod;
3280af22 1504 PL_modcount++;
a0d0e21e
LW
1505 break;
1506
79072805 1507 case OP_COND_EXPR:
11343788 1508 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
463ee0b2 1509 mod(kid, type);
79072805
LW
1510 break;
1511
1512 case OP_RV2AV:
1513 case OP_RV2HV:
93af7a87 1514 if (!type && cUNOPo->op_first->op_type != OP_GV)
cea2e8a9 1515 Perl_croak(aTHX_ "Can't localize through a reference");
11343788 1516 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
3280af22 1517 PL_modcount = 10000;
11343788 1518 return o; /* Treat \(@foo) like ordinary list. */
748a9306
LW
1519 }
1520 /* FALL THROUGH */
79072805 1521 case OP_RV2GV:
5dc0d613 1522 if (scalar_mod_type(o, type))
3fe9a6f1 1523 goto nomod;
11343788 1524 ref(cUNOPo->op_first, o->op_type);
79072805
LW
1525 /* FALL THROUGH */
1526 case OP_AASSIGN:
1527 case OP_ASLICE:
1528 case OP_HSLICE:
93a17b20
LW
1529 case OP_NEXTSTATE:
1530 case OP_DBSTATE:
a0d0e21e
LW
1531 case OP_REFGEN:
1532 case OP_CHOMP:
3280af22 1533 PL_modcount = 10000;
79072805 1534 break;
463ee0b2 1535 case OP_RV2SV:
11343788 1536 if (!type && cUNOPo->op_first->op_type != OP_GV)
cea2e8a9 1537 Perl_croak(aTHX_ "Can't localize through a reference");
aeea060c 1538 ref(cUNOPo->op_first, o->op_type);
463ee0b2 1539 /* FALL THROUGH */
79072805 1540 case OP_GV:
463ee0b2 1541 case OP_AV2ARYLEN:
3280af22 1542 PL_hints |= HINT_BLOCK_SCOPE;
463ee0b2 1543 case OP_SASSIGN:
bf4b1e52
GS
1544 case OP_ANDASSIGN:
1545 case OP_ORASSIGN:
8990e307 1546 case OP_AELEMFAST:
3280af22 1547 PL_modcount++;
8990e307
LW
1548 break;
1549
748a9306
LW
1550 case OP_PADAV:
1551 case OP_PADHV:
3280af22 1552 PL_modcount = 10000;
5196be3e
MB
1553 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1554 return o; /* Treat \(@foo) like ordinary list. */
1555 if (scalar_mod_type(o, type))
3fe9a6f1 1556 goto nomod;
748a9306
LW
1557 /* FALL THROUGH */
1558 case OP_PADSV:
3280af22 1559 PL_modcount++;
748a9306 1560 if (!type)
cea2e8a9 1561 Perl_croak(aTHX_ "Can't localize lexical variable %s",
2d8e6c8d 1562 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
463ee0b2
LW
1563 break;
1564
554b3eca 1565#ifdef USE_THREADS
2faa37cc 1566 case OP_THREADSV:
533c011a 1567 PL_modcount++; /* XXX ??? */
554b3eca
MB
1568 break;
1569#endif /* USE_THREADS */
1570
748a9306
LW
1571 case OP_PUSHMARK:
1572 break;
a0d0e21e 1573
69969c6f
SB
1574 case OP_KEYS:
1575 if (type != OP_SASSIGN)
1576 goto nomod;
5d82c453
GA
1577 goto lvalue_func;
1578 case OP_SUBSTR:
1579 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1580 goto nomod;
5f05dabc 1581 /* FALL THROUGH */
a0d0e21e 1582 case OP_POS:
463ee0b2 1583 case OP_VEC:
5d82c453 1584 lvalue_func:
11343788
MB
1585 pad_free(o->op_targ);
1586 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
5dc0d613 1587 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
11343788
MB
1588 if (o->op_flags & OPf_KIDS)
1589 mod(cBINOPo->op_first->op_sibling, type);
463ee0b2 1590 break;
a0d0e21e 1591
463ee0b2
LW
1592 case OP_AELEM:
1593 case OP_HELEM:
11343788 1594 ref(cBINOPo->op_first, o->op_type);
68dc0745 1595 if (type == OP_ENTERSUB &&
5dc0d613
MB
1596 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1597 o->op_private |= OPpLVAL_DEFER;
3280af22 1598 PL_modcount++;
463ee0b2
LW
1599 break;
1600
1601 case OP_SCOPE:
1602 case OP_LEAVE:
1603 case OP_ENTER:
11343788
MB
1604 if (o->op_flags & OPf_KIDS)
1605 mod(cLISTOPo->op_last, type);
a0d0e21e
LW
1606 break;
1607
1608 case OP_NULL:
638bc118
GS
1609 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1610 goto nomod;
1611 else if (!(o->op_flags & OPf_KIDS))
463ee0b2 1612 break;
11343788
MB
1613 if (o->op_targ != OP_LIST) {
1614 mod(cBINOPo->op_first, type);
a0d0e21e
LW
1615 break;
1616 }
1617 /* FALL THROUGH */
463ee0b2 1618 case OP_LIST:
11343788 1619 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
463ee0b2
LW
1620 mod(kid, type);
1621 break;
1622 }
11343788 1623 o->op_flags |= OPf_MOD;
a0d0e21e
LW
1624
1625 if (type == OP_AASSIGN || type == OP_SASSIGN)
11343788 1626 o->op_flags |= OPf_SPECIAL|OPf_REF;
a0d0e21e 1627 else if (!type) {
11343788
MB
1628 o->op_private |= OPpLVAL_INTRO;
1629 o->op_flags &= ~OPf_SPECIAL;
3280af22 1630 PL_hints |= HINT_BLOCK_SCOPE;
463ee0b2 1631 }
a0d0e21e 1632 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
11343788
MB
1633 o->op_flags |= OPf_REF;
1634 return o;
463ee0b2
LW
1635}
1636
864dbfa3 1637STATIC bool
cea2e8a9 1638S_scalar_mod_type(pTHX_ OP *o, I32 type)
3fe9a6f1
PP
1639{
1640 switch (type) {
1641 case OP_SASSIGN:
5196be3e 1642 if (o->op_type == OP_RV2GV)
3fe9a6f1
PP
1643 return FALSE;
1644 /* FALL THROUGH */
1645 case OP_PREINC:
1646 case OP_PREDEC:
1647 case OP_POSTINC:
1648 case OP_POSTDEC:
1649 case OP_I_PREINC:
1650 case OP_I_PREDEC:
1651 case OP_I_POSTINC:
1652 case OP_I_POSTDEC:
1653 case OP_POW:
1654 case OP_MULTIPLY:
1655 case OP_DIVIDE:
1656 case OP_MODULO:
1657 case OP_REPEAT:
1658 case OP_ADD:
1659 case OP_SUBTRACT:
1660 case OP_I_MULTIPLY:
1661 case OP_I_DIVIDE:
1662 case OP_I_MODULO:
1663 case OP_I_ADD:
1664 case OP_I_SUBTRACT:
1665 case OP_LEFT_SHIFT:
1666 case OP_RIGHT_SHIFT:
1667 case OP_BIT_AND:
1668 case OP_BIT_XOR:
1669 case OP_BIT_OR:
1670 case OP_CONCAT:
1671 case OP_SUBST:
1672 case OP_TRANS:
49e9fbe6
GS
1673 case OP_READ:
1674 case OP_SYSREAD:
1675 case OP_RECV:
bf4b1e52
GS
1676 case OP_ANDASSIGN:
1677 case OP_ORASSIGN:
3fe9a6f1
PP
1678 return TRUE;
1679 default:
1680 return FALSE;
1681 }
1682}
1683
35cd451c 1684STATIC bool
cea2e8a9 1685S_is_handle_constructor(pTHX_ OP *o, I32 argnum)
35cd451c
GS
1686{
1687 switch (o->op_type) {
1688 case OP_PIPE_OP:
1689 case OP_SOCKPAIR:
1690 if (argnum == 2)
1691 return TRUE;
1692 /* FALL THROUGH */
1693 case OP_SYSOPEN:
1694 case OP_OPEN:
ded8aa31 1695 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
35cd451c
GS
1696 case OP_SOCKET:
1697 case OP_OPEN_DIR:
1698 case OP_ACCEPT:
1699 if (argnum == 1)
1700 return TRUE;
1701 /* FALL THROUGH */
1702 default:
1703 return FALSE;
1704 }
1705}
1706
463ee0b2 1707OP *
864dbfa3 1708Perl_refkids(pTHX_ OP *o, I32 type)
463ee0b2
LW
1709{
1710 OP *kid;
11343788
MB
1711 if (o && o->op_flags & OPf_KIDS) {
1712 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
463ee0b2
LW
1713 ref(kid, type);
1714 }
11343788 1715 return o;
463ee0b2
LW
1716}
1717
1718OP *
864dbfa3 1719Perl_ref(pTHX_ OP *o, I32 type)
463ee0b2
LW
1720{
1721 OP *kid;
463ee0b2 1722
3280af22 1723 if (!o || PL_error_count)
11343788 1724 return o;
463ee0b2 1725
11343788 1726 switch (o->op_type) {
a0d0e21e 1727 case OP_ENTERSUB:
afebc493 1728 if ((type == OP_EXISTS || type == OP_DEFINED || type == OP_LOCK) &&
11343788
MB
1729 !(o->op_flags & OPf_STACKED)) {
1730 o->op_type = OP_RV2CV; /* entersub => rv2cv */
22c35a8c 1731 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
11343788
MB
1732 assert(cUNOPo->op_first->op_type == OP_NULL);
1733 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1734 o->op_flags |= OPf_SPECIAL;
8990e307
LW
1735 }
1736 break;
aeea060c 1737
463ee0b2 1738 case OP_COND_EXPR:
11343788 1739 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
463ee0b2
LW
1740 ref(kid, type);
1741 break;
8990e307 1742 case OP_RV2SV:
35cd451c
GS
1743 if (type == OP_DEFINED)
1744 o->op_flags |= OPf_SPECIAL; /* don't create GV */
11343788 1745 ref(cUNOPo->op_first, o->op_type);
4633a7c4
LW
1746 /* FALL THROUGH */
1747 case OP_PADSV:
5f05dabc 1748 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
5dc0d613
MB
1749 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1750 : type == OP_RV2HV ? OPpDEREF_HV
1751 : OPpDEREF_SV);
11343788 1752 o->op_flags |= OPf_MOD;
a0d0e21e 1753 }
8990e307 1754 break;
1c846c1f 1755
2faa37cc 1756 case OP_THREADSV:
a863c7d1
MB
1757 o->op_flags |= OPf_MOD; /* XXX ??? */
1758 break;
1759
463ee0b2
LW
1760 case OP_RV2AV:
1761 case OP_RV2HV:
aeea060c 1762 o->op_flags |= OPf_REF;
8990e307 1763 /* FALL THROUGH */
463ee0b2 1764 case OP_RV2GV:
35cd451c
GS
1765 if (type == OP_DEFINED)
1766 o->op_flags |= OPf_SPECIAL; /* don't create GV */
11343788 1767 ref(cUNOPo->op_first, o->op_type);
463ee0b2 1768 break;
8990e307 1769
463ee0b2
LW
1770 case OP_PADAV:
1771 case OP_PADHV:
aeea060c 1772 o->op_flags |= OPf_REF;
79072805 1773 break;
aeea060c 1774
8990e307 1775 case OP_SCALAR:
79072805 1776 case OP_NULL:
11343788 1777 if (!(o->op_flags & OPf_KIDS))
463ee0b2 1778 break;
11343788 1779 ref(cBINOPo->op_first, type);
79072805
LW
1780 break;
1781 case OP_AELEM:
1782 case OP_HELEM:
11343788 1783 ref(cBINOPo->op_first, o->op_type);
5f05dabc 1784 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
5dc0d613
MB
1785 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1786 : type == OP_RV2HV ? OPpDEREF_HV
1787 : OPpDEREF_SV);
11343788 1788 o->op_flags |= OPf_MOD;
8990e307 1789 }
79072805
LW
1790 break;
1791
463ee0b2 1792 case OP_SCOPE:
79072805
LW
1793 case OP_LEAVE:
1794 case OP_ENTER:
8990e307 1795 case OP_LIST:
11343788 1796 if (!(o->op_flags & OPf_KIDS))
79072805 1797 break;
11343788 1798 ref(cLISTOPo->op_last, type);
79072805 1799 break;
a0d0e21e
LW
1800 default:
1801 break;
79072805 1802 }
11343788 1803 return scalar(o);
8990e307 1804
79072805
LW
1805}
1806
09bef843
SB
1807STATIC OP *
1808S_dup_attrlist(pTHX_ OP *o)
1809{
1810 OP *rop = Nullop;
1811
1812 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1813 * where the first kid is OP_PUSHMARK and the remaining ones
1814 * are OP_CONST. We need to push the OP_CONST values.
1815 */
1816 if (o->op_type == OP_CONST)
1817 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc(cSVOPo->op_sv));
1818 else {
1819 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1820 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1821 if (o->op_type == OP_CONST)
1822 rop = append_elem(OP_LIST, rop,
1823 newSVOP(OP_CONST, o->op_flags,
1824 SvREFCNT_inc(cSVOPo->op_sv)));
1825 }
1826 }
1827 return rop;
1828}
1829
1830STATIC void
1831S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
1832{
09bef843
SB
1833 SV *stashsv;
1834
1835 /* fake up C<use attributes $pkg,$rv,@attrs> */
1836 ENTER; /* need to protect against side-effects of 'use' */
1837 SAVEINT(PL_expect);
1838 if (stash && HvNAME(stash))
1839 stashsv = newSVpv(HvNAME(stash), 0);
1840 else
1841 stashsv = &PL_sv_no;
e4783991 1842
09bef843 1843#define ATTRSMODULE "attributes"
e4783991
GS
1844
1845 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1846 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1847 Nullsv,
1848 prepend_elem(OP_LIST,
1849 newSVOP(OP_CONST, 0, stashsv),
1850 prepend_elem(OP_LIST,
1851 newSVOP(OP_CONST, 0,
1852 newRV(target)),
1853 dup_attrlist(attrs))));
09bef843
SB
1854 LEAVE;
1855}
1856
be3174d2
GS
1857void
1858Perl_apply_attrs_string(pTHX_ char *stashpv, CV *cv,
1859 char *attrstr, STRLEN len)
1860{
1861 OP *attrs = Nullop;
1862
1863 if (!len) {
1864 len = strlen(attrstr);
1865 }
1866
1867 while (len) {
1868 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
1869 if (len) {
1870 char *sstr = attrstr;
1871 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
1872 attrs = append_elem(OP_LIST, attrs,
1873 newSVOP(OP_CONST, 0,
1874 newSVpvn(sstr, attrstr-sstr)));
1875 }
1876 }
1877
1878 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1879 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1880 Nullsv, prepend_elem(OP_LIST,
1881 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
1882 prepend_elem(OP_LIST,
1883 newSVOP(OP_CONST, 0,
1884 newRV((SV*)cv)),
1885 attrs)));
1886}
1887
09bef843
SB
1888STATIC OP *
1889S_my_kid(pTHX_ OP *o, OP *attrs)
93a17b20
LW
1890{
1891 OP *kid;
93a17b20
LW
1892 I32 type;
1893
3280af22 1894 if (!o || PL_error_count)
11343788 1895 return o;
93a17b20 1896
11343788 1897 type = o->op_type;
93a17b20 1898 if (type == OP_LIST) {
11343788 1899 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
09bef843 1900 my_kid(kid, attrs);
dab48698 1901 } else if (type == OP_UNDEF) {
7766148a 1902 return o;
77ca0c92
LW
1903 } else if (type == OP_RV2SV || /* "our" declaration */
1904 type == OP_RV2AV ||
1905 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
192587c2 1906 o->op_private |= OPpOUR_INTRO;
77ca0c92 1907 return o;
dab48698 1908 } else if (type != OP_PADSV &&
93a17b20
LW
1909 type != OP_PADAV &&
1910 type != OP_PADHV &&
1911 type != OP_PUSHMARK)
1912 {
eb64745e
GS
1913 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
1914 PL_op_desc[o->op_type],
1915 PL_in_my == KEY_our ? "our" : "my"));
11343788 1916 return o;
93a17b20 1917 }
09bef843
SB
1918 else if (attrs && type != OP_PUSHMARK) {
1919 HV *stash;
1920 SV *padsv;
1921 SV **namesvp;
1922
eb64745e
GS
1923 PL_in_my = FALSE;
1924 PL_in_my_stash = Nullhv;
1925
09bef843
SB
1926 /* check for C<my Dog $spot> when deciding package */
1927 namesvp = av_fetch(PL_comppad_name, o->op_targ, FALSE);
1928 if (namesvp && *namesvp && SvOBJECT(*namesvp) && HvNAME(SvSTASH(*namesvp)))
1929 stash = SvSTASH(*namesvp);
1930 else
1931 stash = PL_curstash;
1932 padsv = PAD_SV(o->op_targ);
1933 apply_attrs(stash, padsv, attrs);
1934 }
11343788
MB
1935 o->op_flags |= OPf_MOD;
1936 o->op_private |= OPpLVAL_INTRO;
1937 return o;
93a17b20
LW
1938}
1939
1940OP *
09bef843
SB
1941Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1942{
1943 if (o->op_flags & OPf_PARENS)
1944 list(o);
09bef843
SB
1945 if (attrs)
1946 SAVEFREEOP(attrs);
eb64745e
GS
1947 o = my_kid(o, attrs);
1948 PL_in_my = FALSE;
1949 PL_in_my_stash = Nullhv;
1950 return o;
09bef843
SB
1951}
1952
1953OP *
1954Perl_my(pTHX_ OP *o)
1955{
1956 return my_kid(o, Nullop);
1957}
1958
1959OP *
864dbfa3 1960Perl_sawparens(pTHX_ OP *o)
79072805
LW
1961{
1962 if (o)
1963 o->op_flags |= OPf_PARENS;
1964 return o;
1965}
1966
1967OP *
864dbfa3 1968Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
79072805 1969{
d008e5eb 1970 dTHR;
11343788 1971 OP *o;
79072805 1972
e476b1b5 1973 if (ckWARN(WARN_MISC) &&
599cee73
PM
1974 (left->op_type == OP_RV2AV ||
1975 left->op_type == OP_RV2HV ||
1976 left->op_type == OP_PADAV ||
1977 left->op_type == OP_PADHV)) {
22c35a8c 1978 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
599cee73
PM
1979 right->op_type == OP_TRANS)
1980 ? right->op_type : OP_MATCH];
dff6d3cd
GS
1981 const char *sample = ((left->op_type == OP_RV2AV ||
1982 left->op_type == OP_PADAV)
1983 ? "@array" : "%hash");
e476b1b5 1984 Perl_warner(aTHX_ WARN_MISC,
1c846c1f 1985 "Applying %s to %s will act on scalar(%s)",
599cee73 1986 desc, sample, sample);
2ae324a7
PP
1987 }
1988
de4bf5b3
G
1989 if (!(right->op_flags & OPf_STACKED) &&
1990 (right->op_type == OP_MATCH ||
79072805 1991 right->op_type == OP_SUBST ||
de4bf5b3 1992 right->op_type == OP_TRANS)) {
79072805 1993 right->op_flags |= OPf_STACKED;
d897a58d
G
1994 if (right->op_type != OP_MATCH &&
1995 ! (right->op_type == OP_TRANS &&
1996 right->op_private & OPpTRANS_IDENTICAL))
463ee0b2 1997 left = mod(left, right->op_type);
79072805 1998 if (right->op_type == OP_TRANS)
11343788 1999 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
79072805 2000 else
11343788 2001 o = prepend_elem(right->op_type, scalar(left), right);
79072805 2002 if (type == OP_NOT)
11343788
MB
2003 return newUNOP(OP_NOT, 0, scalar(o));
2004 return o;
79072805
LW
2005 }
2006 else
2007 return bind_match(type, left,
2008 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
2009}
2010
2011OP *
864dbfa3 2012Perl_invert(pTHX_ OP *o)
79072805 2013{
11343788
MB
2014 if (!o)
2015 return o;
79072805 2016 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
11343788 2017 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
79072805
LW
2018}
2019
2020OP *
864dbfa3 2021Perl_scope(pTHX_ OP *o)
79072805
LW
2022{
2023 if (o) {
3280af22 2024 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
463ee0b2
LW
2025 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
2026 o->op_type = OP_LEAVE;
22c35a8c 2027 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
463ee0b2
LW
2028 }
2029 else {
2030 if (o->op_type == OP_LINESEQ) {
2031 OP *kid;
2032 o->op_type = OP_SCOPE;
22c35a8c 2033 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
c3ed7a6a
GS
2034 kid = ((LISTOP*)o)->op_first;
2035 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
2036 null(kid);
463ee0b2
LW
2037 }
2038 else
748a9306 2039 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
463ee0b2 2040 }
79072805
LW
2041 }
2042 return o;
2043}
2044
b3ac6de7 2045void
864dbfa3 2046Perl_save_hints(pTHX)
b3ac6de7 2047{
3280af22
NIS
2048 SAVEI32(PL_hints);
2049 SAVESPTR(GvHV(PL_hintgv));
2050 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
2051 SAVEFREESV(GvHV(PL_hintgv));
b3ac6de7
IZ
2052}
2053
a0d0e21e 2054int
864dbfa3 2055Perl_block_start(pTHX_ int full)
79072805 2056{
11343788 2057 dTHR;
3280af22 2058 int retval = PL_savestack_ix;
b3ac6de7 2059
3280af22 2060 SAVEI32(PL_comppad_name_floor);
43d4d5c6
GS
2061 PL_comppad_name_floor = AvFILLp(PL_comppad_name);
2062 if (full)
2063 PL_comppad_name_fill = PL_comppad_name_floor;
2064 if (PL_comppad_name_floor < 0)
2065 PL_comppad_name_floor = 0;
3280af22
NIS
2066 SAVEI32(PL_min_intro_pending);
2067 SAVEI32(PL_max_intro_pending);
2068 PL_min_intro_pending = 0;
2069 SAVEI32(PL_comppad_name_fill);
2070 SAVEI32(PL_padix_floor);
2071 PL_padix_floor = PL_padix;
2072 PL_pad_reset_pending = FALSE;
b3ac6de7 2073 SAVEHINTS();
3280af22 2074 PL_hints &= ~HINT_BLOCK_SCOPE;
1c846c1f 2075 SAVESPTR(PL_compiling.cop_warnings);
0453d815 2076 if (! specialWARN(PL_compiling.cop_warnings)) {
599cee73
PM
2077 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
2078 SAVEFREESV(PL_compiling.cop_warnings) ;
2079 }
ac27b0f5
NIS
2080 SAVESPTR(PL_compiling.cop_io);
2081 if (! specialCopIO(PL_compiling.cop_io)) {
2082 PL_compiling.cop_io = newSVsv(PL_compiling.cop_io) ;
2083 SAVEFREESV(PL_compiling.cop_io) ;
2084 }
a0d0e21e
LW
2085 return retval;
2086}
2087
2088OP*
864dbfa3 2089Perl_block_end(pTHX_ I32 floor, OP *seq)
a0d0e21e 2090{
11343788 2091 dTHR;
3280af22 2092 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
a0d0e21e 2093 OP* retval = scalarseq(seq);
a0d0e21e 2094 LEAVE_SCOPE(floor);
3280af22 2095 PL_pad_reset_pending = FALSE;
e24b16f9 2096 PL_compiling.op_private = PL_hints;
a0d0e21e 2097 if (needblockscope)
3280af22
NIS
2098 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
2099 pad_leavemy(PL_comppad_name_fill);
2100 PL_cop_seqmax++;
a0d0e21e
LW
2101 return retval;
2102}
2103
76e3520e 2104STATIC OP *
cea2e8a9 2105S_newDEFSVOP(pTHX)
54b9620d
MB
2106{
2107#ifdef USE_THREADS
2108 OP *o = newOP(OP_THREADSV, 0);
2109 o->op_targ = find_threadsv("_");
2110 return o;
2111#else
3280af22 2112 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
54b9620d
MB
2113#endif /* USE_THREADS */
2114}
2115
a0d0e21e 2116void
864dbfa3 2117Perl_newPROG(pTHX_ OP *o)
a0d0e21e 2118{
11343788 2119 dTHR;
3280af22 2120 if (PL_in_eval) {
b295d113
TH
2121 if (PL_eval_root)
2122 return;
faef0170
HS
2123 PL_eval_root = newUNOP(OP_LEAVEEVAL,
2124 ((PL_in_eval & EVAL_KEEPERR)
2125 ? OPf_SPECIAL : 0), o);
3280af22 2126 PL_eval_start = linklist(PL_eval_root);
7934575e
GS
2127 PL_eval_root->op_private |= OPpREFCOUNTED;
2128 OpREFCNT_set(PL_eval_root, 1);
3280af22
NIS
2129 PL_eval_root->op_next = 0;
2130 peep(PL_eval_start);
a0d0e21e
LW
2131 }
2132 else {
5dc0d613 2133 if (!o)
a0d0e21e 2134 return;
3280af22
NIS
2135 PL_main_root = scope(sawparens(scalarvoid(o)));
2136 PL_curcop = &PL_compiling;
2137 PL_main_start = LINKLIST(PL_main_root);
7934575e
GS
2138 PL_main_root->op_private |= OPpREFCOUNTED;
2139 OpREFCNT_set(PL_main_root, 1);
3280af22
NIS
2140 PL_main_root->op_next = 0;
2141 peep(PL_main_start);
2142 PL_compcv = 0;
3841441e 2143
4fdae800 2144 /* Register with debugger */
84902520 2145 if (PERLDB_INTER) {
864dbfa3 2146 CV *cv = get_cv("DB::postponed", FALSE);
3841441e
CS
2147 if (cv) {
2148 dSP;
924508f0 2149 PUSHMARK(SP);
cc49e20b 2150 XPUSHs((SV*)CopFILEGV(&PL_compiling));
3841441e 2151 PUTBACK;
864dbfa3 2152 call_sv((SV*)cv, G_DISCARD);
3841441e
CS
2153 }
2154 }
79072805 2155 }
79072805
LW
2156}
2157
2158OP *
864dbfa3 2159Perl_localize(pTHX_ OP *o, I32 lex)
79072805
LW
2160{
2161 if (o->op_flags & OPf_PARENS)
2162 list(o);
8990e307 2163 else {
d008e5eb 2164 dTHR;
599cee73 2165 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
8990e307 2166 char *s;
834a4ddd 2167 for (s = PL_bufptr; *s && (isALNUM(*s) || (*s & 0x80) || strchr("@$%, ",*s)); s++) ;
a0d0e21e 2168 if (*s == ';' || *s == '=')
eb64745e
GS
2169 Perl_warner(aTHX_ WARN_PARENTHESIS,
2170 "Parentheses missing around \"%s\" list",
2171 lex ? (PL_in_my == KEY_our ? "our" : "my") : "local");
8990e307
LW
2172 }
2173 }
93a17b20 2174 if (lex)
eb64745e 2175 o = my(o);
93a17b20 2176 else
eb64745e
GS
2177 o = mod(o, OP_NULL); /* a bit kludgey */
2178 PL_in_my = FALSE;
2179 PL_in_my_stash = Nullhv;
2180 return o;
79072805
LW
2181}
2182
2183OP *
864dbfa3 2184Perl_jmaybe(pTHX_ OP *o)
79072805
LW
2185{
2186 if (o->op_type == OP_LIST) {
554b3eca
MB
2187 OP *o2;
2188#ifdef USE_THREADS
2faa37cc 2189 o2 = newOP(OP_THREADSV, 0);
54b9620d 2190 o2->op_targ = find_threadsv(";");
554b3eca
MB
2191#else
2192 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
2193#endif /* USE_THREADS */
2194 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
79072805
LW
2195 }
2196 return o;
2197}
2198
2199OP *
864dbfa3 2200Perl_fold_constants(pTHX_ register OP *o)
79072805 2201{
11343788 2202 dTHR;
79072805
LW
2203 register OP *curop;
2204 I32 type = o->op_type;
748a9306 2205 SV *sv;
79072805 2206
22c35a8c 2207 if (PL_opargs[type] & OA_RETSCALAR)
79072805 2208 scalar(o);
b162f9ea 2209 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
ed6116ce 2210 o->op_targ = pad_alloc(type, SVs_PADTMP);
79072805 2211
eac055e9
GS
2212 /* integerize op, unless it happens to be C<-foo>.
2213 * XXX should pp_i_negate() do magic string negation instead? */
2214 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2215 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2216 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2217 {
22c35a8c 2218 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
eac055e9 2219 }
85e6fe83 2220
22c35a8c 2221 if (!(PL_opargs[type] & OA_FOLDCONST))
79072805
LW
2222 goto nope;
2223
de939608 2224 switch (type) {
7a52d87a
GS
2225 case OP_NEGATE:
2226 /* XXX might want a ck_negate() for this */
2227 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2228 break;
de939608
CS
2229 case OP_SPRINTF:
2230 case OP_UCFIRST:
2231 case OP_LCFIRST:
2232 case OP_UC:
2233 case OP_LC:
69dcf70c
MB
2234 case OP_SLT:
2235 case OP_SGT:
2236 case OP_SLE:
2237 case OP_SGE:
2238 case OP_SCMP:
2239
de939608
CS
2240 if (o->op_private & OPpLOCALE)
2241 goto nope;
2242 }
2243
3280af22 2244 if (PL_error_count)
a0d0e21e
LW
2245 goto nope; /* Don't try to run w/ errors */
2246
79072805 2247 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
11fa937b
GS
2248 if ((curop->op_type != OP_CONST ||
2249 (curop->op_private & OPpCONST_BARE)) &&
7a52d87a
GS
2250 curop->op_type != OP_LIST &&
2251 curop->op_type != OP_SCALAR &&
2252 curop->op_type != OP_NULL &&
2253 curop->op_type != OP_PUSHMARK)
2254 {
79072805
LW
2255 goto nope;
2256 }
2257 }
2258
2259 curop = LINKLIST(o);
2260 o->op_next = 0;
533c011a 2261 PL_op = curop;
cea2e8a9 2262 CALLRUNOPS(aTHX);
3280af22 2263 sv = *(PL_stack_sp--);
748a9306 2264 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
79072805 2265 pad_swipe(o->op_targ);
748a9306
LW
2266 else if (SvTEMP(sv)) { /* grab mortal temp? */
2267 (void)SvREFCNT_inc(sv);
2268 SvTEMP_off(sv);
85e6fe83 2269 }
79072805
LW
2270 op_free(o);
2271 if (type == OP_RV2GV)
b1cb66bf 2272 return newGVOP(OP_GV, 0, (GV*)sv);
748a9306 2273 else {
ee580363
GS
2274 /* try to smush double to int, but don't smush -2.0 to -2 */
2275 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
2276 type != OP_NEGATE)
2277 {
748a9306 2278 IV iv = SvIV(sv);
65202027 2279 if ((NV)iv == SvNV(sv)) {
748a9306
LW
2280 SvREFCNT_dec(sv);
2281 sv = newSViv(iv);
2282 }
b1cb66bf
PP
2283 else
2284 SvIOK_off(sv); /* undo SvIV() damage */
748a9306
LW
2285 }
2286 return newSVOP(OP_CONST, 0, sv);
2287 }
aeea060c 2288
79072805 2289 nope:
22c35a8c 2290 if (!(PL_opargs[type] & OA_OTHERINT))
79072805 2291 return o;
79072805 2292
3280af22 2293 if (!(PL_hints & HINT_INTEGER)) {
4bb9f687
GS
2294 if (type == OP_MODULO
2295 || type == OP_DIVIDE
2296 || !(o->op_flags & OPf_KIDS))
2297 {
85e6fe83 2298 return o;
4bb9f687 2299 }
85e6fe83
LW
2300
2301 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
2302 if (curop->op_type == OP_CONST) {
b1cb66bf 2303 if (SvIOK(((SVOP*)curop)->op_sv))
85e6fe83
LW
2304 continue;
2305 return o;
2306 }
22c35a8c 2307 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
79072805
LW
2308 continue;
2309 return o;
2310 }
22c35a8c 2311 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
79072805
LW
2312 }
2313
79072805
LW
2314 return o;
2315}
2316
2317OP *
864dbfa3 2318Perl_gen_constant_list(pTHX_ register OP *o)
79072805 2319{
11343788 2320 dTHR;
79072805 2321 register OP *curop;
3280af22 2322 I32 oldtmps_floor = PL_tmps_floor;
79072805 2323
a0d0e21e 2324 list(o);
3280af22 2325 if (PL_error_count)
a0d0e21e
LW
2326 return o; /* Don't attempt to run with errors */
2327
533c011a 2328 PL_op = curop = LINKLIST(o);
a0d0e21e 2329 o->op_next = 0;
7d4045d4 2330 peep(curop);
cea2e8a9
GS
2331 pp_pushmark();
2332 CALLRUNOPS(aTHX);
533c011a 2333 PL_op = curop;
cea2e8a9 2334 pp_anonlist();
3280af22 2335 PL_tmps_floor = oldtmps_floor;
79072805
LW
2336
2337 o->op_type = OP_RV2AV;
22c35a8c 2338 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
79072805 2339 curop = ((UNOP*)o)->op_first;
3280af22 2340 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
79072805 2341 op_free(curop);
79072805
LW
2342 linklist(o);
2343 return list(o);
2344}
2345
2346OP *
864dbfa3 2347Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
79072805
LW
2348{
2349 OP *kid;
a0d0e21e 2350 OP *last = 0;
79072805 2351
11343788
MB
2352 if (!o || o->op_type != OP_LIST)
2353 o = newLISTOP(OP_LIST, 0, o, Nullop);
748a9306 2354 else
5dc0d613 2355 o->op_flags &= ~OPf_WANT;
79072805 2356
22c35a8c 2357 if (!(PL_opargs[type] & OA_MARK))
11343788 2358 null(cLISTOPo->op_first);
8990e307 2359
11343788 2360 o->op_type = type;
22c35a8c 2361 o->op_ppaddr = PL_ppaddr[type];
11343788 2362 o->op_flags |= flags;
79072805 2363
11343788
MB
2364 o = CHECKOP(type, o);
2365 if (o->op_type != type)
2366 return o;
79072805 2367
11343788 2368 if (cLISTOPo->op_children < 7) {
79072805 2369 /* XXX do we really need to do this if we're done appending?? */
11343788 2370 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805 2371 last = kid;
11343788 2372 cLISTOPo->op_last = last; /* in case check substituted last arg */
79072805
LW
2373 }
2374
11343788 2375 return fold_constants(o);
79072805
LW
2376}
2377
2378/* List constructors */
2379
2380OP *
864dbfa3 2381Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
79072805
LW
2382{
2383 if (!first)
2384 return last;
8990e307
LW
2385
2386 if (!last)
79072805 2387 return first;
8990e307 2388
155aba94
GS
2389 if (first->op_type != type
2390 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
2391 {
2392 return newLISTOP(type, 0, first, last);
2393 }
79072805 2394
a0d0e21e
LW
2395 if (first->op_flags & OPf_KIDS)
2396 ((LISTOP*)first)->op_last->op_sibling = last;
2397 else {
2398 first->op_flags |= OPf_KIDS;
2399 ((LISTOP*)first)->op_first = last;
2400 }
2401 ((LISTOP*)first)->op_last = last;
2402 ((LISTOP*)first)->op_children++;
2403 return first;
79072805
LW
2404}
2405
2406OP *
864dbfa3 2407Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
79072805
LW
2408{
2409 if (!first)
2410 return (OP*)last;
8990e307
LW
2411
2412 if (!last)
79072805 2413 return (OP*)first;
8990e307
LW
2414
2415 if (first->op_type != type)
79072805 2416 return prepend_elem(type, (OP*)first, (OP*)last);
8990e307
LW
2417
2418 if (last->op_type != type)
79072805
LW
2419 return append_elem(type, (OP*)first, (OP*)last);
2420
2421 first->op_last->op_sibling = last->op_first;
2422 first->op_last = last->op_last;
2423 first->op_children += last->op_children;
2424 if (first->op_children)
acb74605 2425 first->op_flags |= OPf_KIDS;
1c846c1f 2426
b7dc083c
NIS
2427#ifdef PL_OP_SLAB_ALLOC
2428#else
1c846c1f 2429 Safefree(last);
b7dc083c 2430#endif
79072805
LW
2431 return (OP*)first;
2432}
2433
2434OP *
864dbfa3 2435Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
79072805
LW
2436{
2437 if (!first)
2438 return last;
8990e307
LW
2439
2440 if (!last)
79072805 2441 return first;
8990e307
LW
2442
2443 if (last->op_type == type) {
2444 if (type == OP_LIST) { /* already a PUSHMARK there */
2445 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2446 ((LISTOP*)last)->op_first->op_sibling = first;
2447 }
2448 else {
2449 if (!(last->op_flags & OPf_KIDS)) {
2450 ((LISTOP*)last)->op_last = first;
2451 last->op_flags |= OPf_KIDS;
2452 }
2453 first->op_sibling = ((LISTOP*)last)->op_first;
2454 ((LISTOP*)last)->op_first = first;
79072805 2455 }
79072805
LW
2456 ((LISTOP*)last)->op_children++;
2457 return last;
2458 }
2459
2460 return newLISTOP(type, 0, first, last);
2461}
2462
2463/* Constructors */
2464
2465OP *
864dbfa3 2466Perl_newNULLLIST(pTHX)
79072805 2467{
8990e307
LW
2468 return newOP(OP_STUB, 0);
2469}
2470
2471OP *
864dbfa3 2472Perl_force_list(pTHX_ OP *o)
8990e307 2473{
11343788
MB
2474 if (!o || o->op_type != OP_LIST)
2475 o = newLISTOP(OP_LIST, 0, o, Nullop);
2476 null(o);
2477 return o;
79072805
LW
2478}
2479
2480OP *
864dbfa3 2481Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
79072805
LW
2482{
2483 LISTOP *listop;
2484
b7dc083c 2485 NewOp(1101, listop, 1, LISTOP);
79072805
LW
2486
2487 listop->op_type = type;
22c35a8c 2488 listop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2489 listop->op_children = (first != 0) + (last != 0);
2490 listop->op_flags = flags;
79072805
LW
2491
2492 if (!last && first)
2493 last = first;
2494 else if (!first && last)
2495 first = last;
8990e307
LW
2496 else if (first)
2497 first->op_sibling = last;
79072805
LW
2498 listop->op_first = first;
2499 listop->op_last = last;
8990e307
LW
2500 if (type == OP_LIST) {
2501 OP* pushop;
2502 pushop = newOP(OP_PUSHMARK, 0);
2503 pushop->op_sibling = first;
2504 listop->op_first = pushop;
2505 listop->op_flags |= OPf_KIDS;
2506 if (!last)
2507 listop->op_last = pushop;
2508 }
2509 else if (listop->op_children)
2510 listop->op_flags |= OPf_KIDS;
79072805
LW
2511
2512 return (OP*)listop;
2513}
2514
2515OP *
864dbfa3 2516Perl_newOP(pTHX_ I32 type, I32 flags)
79072805 2517{
11343788 2518 OP *o;
b7dc083c 2519 NewOp(1101, o, 1, OP);
11343788 2520 o->op_type = type;
22c35a8c 2521 o->op_ppaddr = PL_ppaddr[type];
11343788 2522 o->op_flags = flags;
79072805 2523
11343788
MB
2524 o->op_next = o;
2525 o->op_private = 0 + (flags >> 8);
22c35a8c 2526 if (PL_opargs[type] & OA_RETSCALAR)
11343788 2527 scalar(o);
22c35a8c 2528 if (PL_opargs[type] & OA_TARGET)
11343788
MB
2529 o->op_targ = pad_alloc(type, SVs_PADTMP);
2530 return CHECKOP(type, o);
79072805
LW
2531}
2532
2533OP *
864dbfa3 2534Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
79072805
LW
2535{
2536 UNOP *unop;
2537
93a17b20 2538 if (!first)
aeea060c 2539 first = newOP(OP_STUB, 0);
22c35a8c 2540 if (PL_opargs[type] & OA_MARK)
8990e307 2541 first = force_list(first);
93a17b20 2542
b7dc083c 2543 NewOp(1101, unop, 1, UNOP);
79072805 2544 unop->op_type = type;
22c35a8c 2545 unop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2546 unop->op_first = first;
2547 unop->op_flags = flags | OPf_KIDS;
c07a80fd 2548 unop->op_private = 1 | (flags >> 8);
e50aee73 2549 unop = (UNOP*) CHECKOP(type, unop);
79072805
LW
2550 if (unop->op_next)
2551 return (OP*)unop;
2552
a0d0e21e 2553 return fold_constants((OP *) unop);
79072805
LW
2554}
2555
2556OP *
864dbfa3 2557Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
79072805
LW
2558{
2559 BINOP *binop;
b7dc083c 2560 NewOp(1101, binop, 1, BINOP);
79072805
LW
2561
2562 if (!first)
2563 first = newOP(OP_NULL, 0);
2564
2565 binop->op_type = type;
22c35a8c 2566 binop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2567 binop->op_first = first;
2568 binop->op_flags = flags | OPf_KIDS;
2569 if (!last) {
2570 last = first;
c07a80fd 2571 binop->op_private = 1 | (flags >> 8);
79072805
LW
2572 }
2573 else {
c07a80fd 2574 binop->op_private = 2 | (flags >> 8);
79072805
LW
2575 first->op_sibling = last;
2576 }
2577
e50aee73 2578 binop = (BINOP*)CHECKOP(type, binop);
b162f9ea 2579 if (binop->op_next || binop->op_type != type)
79072805
LW
2580 return (OP*)binop;
2581
7284ab6f 2582 binop->op_last = binop->op_first->op_sibling;
79072805 2583
a0d0e21e 2584 return fold_constants((OP *)binop);
79072805
LW
2585}
2586
a0ed51b3
LW
2587static int
2588utf8compare(const void *a, const void *b)
2589{
2590 int i;
2591 for (i = 0; i < 10; i++) {
2592 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2593 return -1;
2594 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2595 return 1;
2596 }
2597 return 0;
2598}
2599
79072805 2600OP *
864dbfa3 2601Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
79072805 2602{
79072805
LW
2603 SV *tstr = ((SVOP*)expr)->op_sv;
2604 SV *rstr = ((SVOP*)repl)->op_sv;
463ee0b2
LW
2605 STRLEN tlen;
2606 STRLEN rlen;
ec49126f
PP
2607 register U8 *t = (U8*)SvPV(tstr, tlen);
2608 register U8 *r = (U8*)SvPV(rstr, rlen);
79072805
LW
2609 register I32 i;
2610 register I32 j;
a0ed51b3 2611 I32 del;
79072805 2612 I32 complement;
5d06d08e 2613 I32 squash;
79072805
LW
2614 register short *tbl;
2615
11343788 2616 complement = o->op_private & OPpTRANS_COMPLEMENT;
a0ed51b3 2617 del = o->op_private & OPpTRANS_DELETE;
5d06d08e 2618 squash = o->op_private & OPpTRANS_SQUASH;
1c846c1f 2619
036b4402
GS
2620 if (SvUTF8(tstr))
2621 o->op_private |= OPpTRANS_FROM_UTF;
1c846c1f
NIS
2622
2623 if (SvUTF8(rstr))
036b4402 2624 o->op_private |= OPpTRANS_TO_UTF;
79072805 2625
a0ed51b3 2626 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
79cb57f6 2627 SV* listsv = newSVpvn("# comment\n",10);
a0ed51b3
LW
2628 SV* transv = 0;
2629 U8* tend = t + tlen;
2630 U8* rend = r + rlen;
ba210ebe 2631 STRLEN ulen;
a0ed51b3
LW
2632 U32 tfirst = 1;
2633 U32 tlast = 0;
2634 I32 tdiff;
2635 U32 rfirst = 1;
2636 U32 rlast = 0;
2637 I32 rdiff;
2638 I32 diff;
2639 I32 none = 0;
2640 U32 max = 0;
2641 I32 bits;
2642 I32 grows = 0;
2643 I32 havefinal = 0;
2644 U32 final;
a0ed51b3
LW
2645 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2646 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2647
2648 if (complement) {
806e7201 2649 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3 2650 U8** cp;
ba210ebe 2651 I32* cl;
a0ed51b3
LW
2652 UV nextmin = 0;
2653 New(1109, cp, tlen, U8*);
2654 i = 0;
79cb57f6 2655 transv = newSVpvn("",0);
a0ed51b3
LW
2656 while (t < tend) {
2657 cp[i++] = t;
2658 t += UTF8SKIP(t);
2659 if (*t == 0xff) {
2660 t++;
2661 t += UTF8SKIP(t);
2662 }
2663 }
2664 qsort(cp, i, sizeof(U8*), utf8compare);
2665 for (j = 0; j < i; j++) {
2666 U8 *s = cp[j];
ba210ebe 2667 I32 cur = j < i ? cp[j+1] - s : tend - s;
dcad2880 2668 UV val = utf8_to_uv(s, cur, &ulen, 0);
a0ed51b3
LW
2669 s += ulen;
2670 diff = val - nextmin;
2671 if (diff > 0) {
2672 t = uv_to_utf8(tmpbuf,nextmin);
dfe13c55 2673 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2674 if (diff > 1) {
2675 t = uv_to_utf8(tmpbuf, val - 1);
2676 sv_catpvn(transv, "\377", 1);
dfe13c55 2677 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2678 }
2679 }
2680 if (*s == 0xff)
dcad2880 2681 val = utf8_to_uv(s+1, cur - 1, &ulen, 0);
a0ed51b3
LW
2682 if (val >= nextmin)
2683 nextmin = val + 1;
2684 }
2685 t = uv_to_utf8(tmpbuf,nextmin);
dfe13c55 2686 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2687 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2688 sv_catpvn(transv, "\377", 1);
dfe13c55
GS
2689 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2690 t = (U8*)SvPVX(transv);
a0ed51b3
LW
2691 tlen = SvCUR(transv);
2692 tend = t + tlen;
2693 }
2694 else if (!rlen && !del) {
2695 r = t; rlen = tlen; rend = tend;
4757a243
LW
2696 }
2697 if (!squash) {
12ae5dfc
JH
2698 if (t == r ||
2699 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
01ec43d0 2700 {
4757a243 2701 o->op_private |= OPpTRANS_IDENTICAL;
01ec43d0 2702 }
a0ed51b3
LW
2703 }
2704
2705 while (t < tend || tfirst <= tlast) {
2706 /* see if we need more "t" chars */
2707 if (tfirst > tlast) {
dcad2880 2708 tfirst = (I32)utf8_to_uv(t, tend - t, &ulen, 0);
a0ed51b3
LW
2709 t += ulen;
2710 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
ba210ebe 2711 t++;
dcad2880 2712 tlast = (I32)utf8_to_uv(t, tend - t, &ulen, 0);
a0ed51b3
LW
2713 t += ulen;
2714 }
2715 else
2716 tlast = tfirst;
2717 }
2718
2719 /* now see if we need more "r" chars */
2720 if (rfirst > rlast) {
2721 if (r < rend) {
dcad2880 2722 rfirst = (I32)utf8_to_uv(r, rend - r, &ulen, 0);
a0ed51b3
LW
2723 r += ulen;
2724 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
ba210ebe 2725 r++;
dcad2880 2726 rlast = (I32)utf8_to_uv(r, rend - r, &ulen, 0);
a0ed51b3
LW
2727 r += ulen;
2728 }
2729 else
2730 rlast = rfirst;
2731 }
2732 else {
2733 if (!havefinal++)
2734 final = rlast;
2735 rfirst = rlast = 0xffffffff;
2736 }
2737 }
2738
2739 /* now see which range will peter our first, if either. */
2740 tdiff = tlast - tfirst;
2741 rdiff = rlast - rfirst;
2742
2743 if (tdiff <= rdiff)
2744 diff = tdiff;
2745 else
2746 diff = rdiff;
2747
2748 if (rfirst == 0xffffffff) {
2749 diff = tdiff; /* oops, pretend rdiff is infinite */
2750 if (diff > 0)
894356b3
GS
2751 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
2752 (long)tfirst, (long)tlast);
a0ed51b3 2753 else
894356b3 2754 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
a0ed51b3
LW
2755 }
2756 else {
2757 if (diff > 0)
894356b3
GS
2758 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
2759 (long)tfirst, (long)(tfirst + diff),
2760 (long)rfirst);
a0ed51b3 2761 else
894356b3
GS
2762 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
2763 (long)tfirst, (long)rfirst);
a0ed51b3
LW
2764
2765 if (rfirst + diff > max)
2766 max = rfirst + diff;
2767 rfirst += diff + 1;
2768 if (!grows) {
2769 if (rfirst <= 0x80)
2770 ;
2771 else if (rfirst <= 0x800)
2772 grows |= (tfirst < 0x80);
2773 else if (rfirst <= 0x10000)
2774 grows |= (tfirst < 0x800);
2775 else if (rfirst <= 0x200000)
2776 grows |= (tfirst < 0x10000);
2777 else if (rfirst <= 0x4000000)
2778 grows |= (tfirst < 0x200000);
2779 else if (rfirst <= 0x80000000)
2780 grows |= (tfirst < 0x4000000);
2781 }
2782 }
2783 tfirst += diff + 1;
2784 }
2785
2786 none = ++max;
2787 if (del)
2788 del = ++max;
2789
2790 if (max > 0xffff)
2791 bits = 32;
2792 else if (max > 0xff)
2793 bits = 16;
2794 else
2795 bits = 8;
2796
2797 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2798 SvREFCNT_dec(listsv);
2799 if (transv)
2800 SvREFCNT_dec(transv);
2801
2802 if (!del && havefinal)
b448e4fe
JH
2803 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5,
2804 newSVuv((UV)final), 0);
a0ed51b3
LW
2805
2806 if (grows && to_utf)
2807 o->op_private |= OPpTRANS_GROWS;
2808
2809 op_free(expr);
2810 op_free(repl);
2811 return o;
2812 }
2813
2814 tbl = (short*)cPVOPo->op_pv;
79072805
LW
2815 if (complement) {
2816 Zero(tbl, 256, short);
2817 for (i = 0; i < tlen; i++)
ec49126f 2818 tbl[t[i]] = -1;
79072805
LW
2819 for (i = 0, j = 0; i < 256; i++) {
2820 if (!tbl[i]) {
2821 if (j >= rlen) {
a0ed51b3 2822 if (del)
79072805
LW
2823 tbl[i] = -2;
2824 else if (rlen)
ec49126f 2825 tbl[i] = r[j-1];
79072805
LW
2826 else
2827 tbl[i] = i;
2828 }
2829 else
ec49126f 2830 tbl[i] = r[j++];
79072805
LW
2831 }
2832 }
2833 }
2834 else {
a0ed51b3 2835 if (!rlen && !del) {
79072805 2836 r = t; rlen = tlen;
5d06d08e 2837 if (!squash)
4757a243 2838 o->op_private |= OPpTRANS_IDENTICAL;
79072805
LW
2839 }
2840 for (i = 0; i < 256; i++)
2841 tbl[i] = -1;
2842 for (i = 0, j = 0; i < tlen; i++,j++) {
2843 if (j >= rlen) {
a0ed51b3 2844 if (del) {
ec49126f
PP
2845 if (tbl[t[i]] == -1)
2846 tbl[t[i]] = -2;
79072805
LW
2847 continue;
2848 }
2849 --j;
2850 }
ec49126f
PP
2851 if (tbl[t[i]] == -1)
2852 tbl[t[i]] = r[j];
79072805
LW
2853 }
2854 }
2855 op_free(expr);
2856 op_free(repl);
2857
11343788 2858 return o;
79072805
LW
2859}
2860
2861OP *
864dbfa3 2862Perl_newPMOP(pTHX_ I32 type, I32 flags)
79072805 2863{
11343788 2864 dTHR;
79072805
LW
2865 PMOP *pmop;
2866
b7dc083c 2867 NewOp(1101, pmop, 1, PMOP);
79072805 2868 pmop->op_type = type;
22c35a8c 2869 pmop->op_ppaddr = PL_ppaddr[type];
79072805 2870 pmop->op_flags = flags;
c07a80fd 2871 pmop->op_private = 0 | (flags >> 8);
79072805 2872
3280af22 2873 if (PL_hints & HINT_RE_TAINT)
b3eb6a9b 2874 pmop->op_pmpermflags |= PMf_RETAINT;
3280af22 2875 if (PL_hints & HINT_LOCALE)
b3eb6a9b
GS
2876 pmop->op_pmpermflags |= PMf_LOCALE;
2877 pmop->op_pmflags = pmop->op_pmpermflags;
36477c24 2878
79072805 2879 /* link into pm list */
3280af22
NIS
2880 if (type != OP_TRANS && PL_curstash) {
2881 pmop->op_pmnext = HvPMROOT(PL_curstash);
2882 HvPMROOT(PL_curstash) = pmop;
79072805
LW
2883 }
2884
2885 return (OP*)pmop;
2886}
2887
2888OP *
864dbfa3 2889Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
79072805 2890{
5c0ca799 2891 dTHR;
79072805
LW
2892 PMOP *pm;
2893 LOGOP *rcop;
ce862d02 2894 I32 repl_has_vars = 0;
79072805 2895
11343788
MB
2896 if (o->op_type == OP_TRANS)
2897 return pmtrans(o, expr, repl);
79072805 2898
3280af22 2899 PL_hints |= HINT_BLOCK_SCOPE;
11343788 2900 pm = (PMOP*)o;
79072805
LW
2901
2902 if (expr->op_type == OP_CONST) {
463ee0b2 2903 STRLEN plen;
79072805 2904 SV *pat = ((SVOP*)expr)->op_sv;
463ee0b2 2905 char *p = SvPV(pat, plen);
11343788 2906 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
93a17b20 2907 sv_setpvn(pat, "\\s+", 3);
463ee0b2 2908 p = SvPV(pat, plen);
79072805
LW
2909 pm->op_pmflags |= PMf_SKIPWHITE;
2910 }
393fec97 2911 if ((PL_hints & HINT_UTF8) || (SvUTF8(pat) && !(PL_hints & HINT_BYTE)))
7e2040f0 2912 pm->op_pmdynflags |= PMdf_UTF8;
cea2e8a9 2913 pm->op_pmregexp = CALLREGCOMP(aTHX_ p, p + plen, pm);
aeea060c 2914 if (strEQ("\\s+", pm->op_pmregexp->precomp))
85e6fe83 2915 pm->op_pmflags |= PMf_WHITE;
79072805
LW
2916 op_free(expr);
2917 }
2918 else {
393fec97
GS
2919 if (PL_hints & HINT_UTF8)
2920 pm->op_pmdynflags |= PMdf_UTF8;
3280af22 2921 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
1c846c1f 2922 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2cd61cdb
IZ
2923 ? OP_REGCRESET
2924 : OP_REGCMAYBE),0,expr);
463ee0b2 2925
b7dc083c 2926 NewOp(1101, rcop, 1, LOGOP);
79072805 2927 rcop->op_type = OP_REGCOMP;
22c35a8c 2928 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
79072805 2929 rcop->op_first = scalar(expr);
1c846c1f 2930 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2cd61cdb
IZ
2931 ? (OPf_SPECIAL | OPf_KIDS)
2932 : OPf_KIDS);
79072805 2933 rcop->op_private = 1;
11343788 2934 rcop->op_other = o;
79072805
LW
2935
2936 /* establish postfix order */
3280af22 2937 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
463ee0b2
LW
2938 LINKLIST(expr);
2939 rcop->op_next = expr;
2940 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2941 }
2942 else {
2943 rcop->op_next = LINKLIST(expr);
2944 expr->op_next = (OP*)rcop;
2945 }
79072805 2946
11343788 2947 prepend_elem(o->op_type, scalar((OP*)rcop), o);
79072805
LW
2948 }
2949
2950 if (repl) {
748a9306 2951 OP *curop;
0244c3a4 2952 if (pm->op_pmflags & PMf_EVAL) {
748a9306 2953 curop = 0;
57843af0
GS
2954 if (CopLINE(PL_curcop) < PL_multi_end)
2955 CopLINE_set(PL_curcop, PL_multi_end);
0244c3a4 2956 }
554b3eca 2957#ifdef USE_THREADS
2faa37cc 2958 else if (repl->op_type == OP_THREADSV
554b3eca 2959 && strchr("&`'123456789+",
533c011a 2960 PL_threadsv_names[repl->op_targ]))
554b3eca
MB
2961 {
2962 curop = 0;
2963 }
2964#endif /* USE_THREADS */
748a9306
LW
2965 else if (repl->op_type == OP_CONST)
2966 curop = repl;
79072805 2967 else {
79072805
LW
2968 OP *lastop = 0;
2969 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
22c35a8c 2970 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
554b3eca 2971#ifdef USE_THREADS
ce862d02
IZ
2972 if (curop->op_type == OP_THREADSV) {
2973 repl_has_vars = 1;
be949f6f 2974 if (strchr("&`'123456789+", curop->op_private))
ce862d02 2975 break;
554b3eca
MB
2976 }
2977#else
79072805 2978 if (curop->op_type == OP_GV) {
638eceb6 2979 GV *gv = cGVOPx_gv(curop);
ce862d02 2980 repl_has_vars = 1;
93a17b20 2981 if (strchr("&`'123456789+", *GvENAME(gv)))
79072805
LW
2982 break;
2983 }
554b3eca 2984#endif /* USE_THREADS */
79072805
LW
2985 else if (curop->op_type == OP_RV2CV)
2986 break;
2987 else if (curop->op_type == OP_RV2SV ||
2988 curop->op_type == OP_RV2AV ||
2989 curop->op_type == OP_RV2HV ||
2990 curop->op_type == OP_RV2GV) {
2991 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2992 break;
2993 }
748a9306
LW
2994 else if (curop->op_type == OP_PADSV ||
2995 curop->op_type == OP_PADAV ||
2996 curop->op_type == OP_PADHV ||
554b3eca 2997 curop->op_type == OP_PADANY) {
ce862d02 2998 repl_has_vars = 1;
748a9306 2999 }
1167e5da
SM
3000 else if (curop->op_type == OP_PUSHRE)
3001 ; /* Okay here, dangerous in newASSIGNOP */
79072805
LW
3002 else
3003 break;
3004 }
3005 lastop = curop;
3006 }
748a9306 3007 }
ce862d02 3008 if (curop == repl
1c846c1f
NIS
3009 && !(repl_has_vars
3010 && (!pm->op_pmregexp
ce862d02 3011 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
748a9306 3012 pm->op_pmflags |= PMf_CONST; /* const for long enough */
4633a7c4 3013 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
11343788 3014 prepend_elem(o->op_type, scalar(repl), o);
748a9306
LW
3015 }
3016 else {
ce862d02
IZ
3017 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
3018 pm->op_pmflags |= PMf_MAYBE_CONST;
3019 pm->op_pmpermflags |= PMf_MAYBE_CONST;
3020 }
b7dc083c 3021 NewOp(1101, rcop, 1, LOGOP);
748a9306 3022 rcop->op_type = OP_SUBSTCONT;
22c35a8c 3023 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
748a9306
LW
3024 rcop->op_first = scalar(repl);
3025 rcop->op_flags |= OPf_KIDS;
3026 rcop->op_private = 1;
11343788 3027 rcop->op_other = o;
748a9306
LW
3028
3029 /* establish postfix order */
3030 rcop->op_next = LINKLIST(repl);
3031 repl->op_next = (OP*)rcop;
3032
3033 pm->op_pmreplroot = scalar((OP*)rcop);
3034 pm->op_pmreplstart = LINKLIST(rcop);
3035 rcop->op_next = 0;
79072805
LW
3036 }
3037 }
3038
3039 return (OP*)pm;
3040}
3041
3042OP *
864dbfa3 3043Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
79072805
LW
3044{
3045 SVOP *svop;
b7dc083c 3046 NewOp(1101, svop, 1, SVOP);
79072805 3047 svop->op_type = type;
22c35a8c 3048 svop->op_ppaddr = PL_ppaddr[type];
79072805
LW
3049 svop->op_sv = sv;
3050 svop->op_next = (OP*)svop;
3051 svop->op_flags = flags;
22c35a8c 3052 if (PL_opargs[type] & OA_RETSCALAR)
463ee0b2 3053 scalar((OP*)svop);
22c35a8c 3054 if (PL_opargs[type] & OA_TARGET)
ed6116ce 3055 svop->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 3056 return CHECKOP(type, svop);
79072805
LW
3057}
3058
3059OP *
350de78d
GS
3060Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
3061{
3062 PADOP *padop;
3063 NewOp(1101, padop, 1, PADOP);
3064 padop->op_type = type;
3065 padop->op_ppaddr = PL_ppaddr[type];
3066 padop->op_padix = pad_alloc(type, SVs_PADTMP);
7766f137 3067 SvREFCNT_dec(PL_curpad[padop->op_padix]);
350de78d 3068 PL_curpad[padop->op_padix] = sv;
7766f137 3069 SvPADTMP_on(sv);
350de78d
GS
3070 padop->op_next = (OP*)padop;
3071 padop->op_flags = flags;
3072 if (PL_opargs[type] & OA_RETSCALAR)
3073 scalar((OP*)padop);
3074 if (PL_opargs[type] & OA_TARGET)
3075 padop->op_targ = pad_alloc(type, SVs_PADTMP);
3076 return CHECKOP(type, padop);
3077}
3078
3079OP *
864dbfa3 3080Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
79072805 3081{
11343788 3082 dTHR;
350de78d 3083#ifdef USE_ITHREADS
743e66e6 3084 GvIN_PAD_on(gv);
350de78d
GS
3085 return newPADOP(type, flags, SvREFCNT_inc(gv));
3086#else
7934575e 3087 return newSVOP(type, flags, SvREFCNT_inc(gv));
350de78d 3088#endif
79072805
LW
3089}
3090
3091OP *
864dbfa3 3092Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
79072805
LW
3093{
3094 PVOP *pvop;
b7dc083c 3095 NewOp(1101, pvop, 1, PVOP);
79072805 3096 pvop->op_type = type;
22c35a8c 3097 pvop->op_ppaddr = PL_ppaddr[type];
79072805
LW
3098 pvop->op_pv = pv;
3099 pvop->op_next = (OP*)pvop;
3100 pvop->op_flags = flags;
22c35a8c 3101 if (PL_opargs[type] & OA_RETSCALAR)
463ee0b2 3102 scalar((OP*)pvop);
22c35a8c 3103 if (PL_opargs[type] & OA_TARGET)
ed6116ce 3104 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 3105 return CHECKOP(type, pvop);
79072805
LW
3106}
3107
79072805 3108void
864dbfa3 3109Perl_package(pTHX_ OP *o)
79072805 3110{
11343788 3111 dTHR;
93a17b20 3112 SV *sv;
79072805 3113
3280af22
NIS
3114 save_hptr(&PL_curstash);
3115 save_item(PL_curstname);
11343788 3116 if (o) {
463ee0b2
LW
3117 STRLEN len;
3118 char *name;
11343788 3119 sv = cSVOPo->op_sv;
463ee0b2 3120 name = SvPV(sv, len);
3280af22
NIS
3121 PL_curstash = gv_stashpvn(name,len,TRUE);
3122 sv_setpvn(PL_curstname, name, len);
11343788 3123 op_free(o);
93a17b20
LW
3124 }
3125 else {
3280af22
NIS
3126 sv_setpv(PL_curstname,"<none>");
3127 PL_curstash = Nullhv;
93a17b20 3128 }
7ad382f4 3129 PL_hints |= HINT_BLOCK_SCOPE;
3280af22
NIS
3130 PL_copline = NOLINE;
3131 PL_expect = XSTATE;
79072805
LW
3132}
3133
85e6fe83 3134void
864dbfa3 3135Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg)
85e6fe83 3136{
a0d0e21e 3137 OP *pack;
a0d0e21e
LW
3138 OP *rqop;
3139 OP *imop;
b1cb66bf 3140 OP *veop;
78ca652e 3141 GV *gv;
85e6fe83 3142
a0d0e21e 3143 if (id->op_type != OP_CONST)
cea2e8a9 3144 Perl_croak(aTHX_ "Module name must be constant");
85e6fe83 3145
b1cb66bf
PP
3146 veop = Nullop;
3147
0f79a09d 3148 if (version != Nullop) {
b1cb66bf
PP
3149 SV *vesv = ((SVOP*)version)->op_sv;
3150
44dcb63b 3151 if (arg == Nullop && !SvNIOKp(vesv)) {
b1cb66bf
PP
3152 arg = version;
3153 }
3154 else {
3155 OP *pack;
0f79a09d 3156 SV *meth;
b1cb66bf 3157
44dcb63b 3158 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
cea2e8a9 3159 Perl_croak(aTHX_ "Version number must be constant number");
b1cb66bf
PP
3160
3161 /* Make copy of id so we don't free it twice */
3162 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3163
3164 /* Fake up a method call to VERSION */
0f79a09d
GS
3165 meth = newSVpvn("VERSION",7);
3166 sv_upgrade(meth, SVt_PVIV);
155aba94 3167 (void)SvIOK_on(meth);
0f79a09d 3168 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
b1cb66bf
PP
3169 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3170 append_elem(OP_LIST,
0f79a09d
GS
3171 prepend_elem(OP_LIST, pack, list(version)),
3172 newSVOP(OP_METHOD_NAMED, 0, meth)));
b1cb66bf
PP
3173 }
3174 }
aeea060c 3175
a0d0e21e 3176 /* Fake up an import/unimport */
4633a7c4
LW
3177 if (arg && arg->op_type == OP_STUB)
3178 imop = arg; /* no import on explicit () */
44dcb63b 3179 else if (SvNIOKp(((SVOP*)id)->op_sv)) {
b1cb66bf
PP
3180 imop = Nullop; /* use 5.0; */
3181 }
4633a7c4 3182 else {
0f79a09d
GS
3183 SV *meth;
3184
4633a7c4
LW
3185 /* Make copy of id so we don't free it twice */
3186 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
0f79a09d
GS
3187
3188 /* Fake up a method call to import/unimport */
3189 meth = aver ? newSVpvn("import",6) : newSVpvn("unimport", 8);;
3190 sv_upgrade(meth, SVt_PVIV);
155aba94 3191 (void)SvIOK_on(meth);
0f79a09d 3192 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
4633a7c4 3193 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
0f79a09d
GS
3194 append_elem(OP_LIST,
3195 prepend_elem(OP_LIST, pack, list(arg)),
3196 newSVOP(OP_METHOD_NAMED, 0, meth)));
4633a7c4
LW
3197 }
3198
78ca652e
GS
3199 /* Fake up a require, handle override, if any */
3200 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
3201 if (!(gv && GvIMPORTED_CV(gv)))
3202 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
3203
3204 if (gv && GvIMPORTED_CV(gv)) {
3205 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3206 append_elem(OP_LIST, id,
3207 scalar(newUNOP(OP_RV2CV, 0,
3208 newGVOP(OP_GV, 0,
3209 gv))))));
3210 }
3211 else {
3212 rqop = newUNOP(OP_REQUIRE, 0, id);
3213 }
a0d0e21e
LW
3214
3215 /* Fake up the BEGIN {}, which does its thing immediately. */
09bef843 3216 newATTRSUB(floor,
79cb57f6 3217 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
4633a7c4 3218 Nullop,
09bef843 3219 Nullop,
a0d0e21e 3220 append_elem(OP_LINESEQ,
b1cb66bf
PP
3221 append_elem(OP_LINESEQ,
3222 newSTATEOP(0, Nullch, rqop),
3223 newSTATEOP(0, Nullch, veop)),
a0d0e21e 3224 newSTATEOP(0, Nullch, imop) ));
85e6fe83 3225
c305c6a0 3226 PL_hints |= HINT_BLOCK_SCOPE;
3280af22
NIS
3227 PL_copline = NOLINE;
3228 PL_expect = XSTATE;
85e6fe83
LW
3229}
3230
e4783991
GS
3231void
3232Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
3233{
3234 va_list args;
3235 va_start(args, ver);
3236 vload_module(flags, name, ver, &args);
3237 va_end(args);
3238}
3239
3240#ifdef PERL_IMPLICIT_CONTEXT
3241void
3242Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
3243{
3244 dTHX;
3245 va_list args;
3246 va_start(args, ver);
3247 vload_module(flags, name, ver, &args);
3248 va_end(args);
3249}
3250#endif
3251
3252void
3253Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
3254{
3255 OP *modname, *veop, *imop;
3256
3257 modname = newSVOP(OP_CONST, 0, name);
3258 modname->op_private |= OPpCONST_BARE;
3259 if (ver) {
3260 veop = newSVOP(OP_CONST, 0, ver);
3261 }
3262 else
3263 veop = Nullop;
3264 if (flags & PERL_LOADMOD_NOIMPORT) {
3265 imop = sawparens(newNULLLIST());
3266 }
3267 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
3268 imop = va_arg(*args, OP*);
3269 }
3270 else {
3271 SV *sv;
3272 imop = Nullop;
3273 sv = va_arg(*args, SV*);
3274 while (sv) {
3275 imop = append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
3276 sv = va_arg(*args, SV*);
3277 }
3278 }
81885997
GS
3279 {
3280 line_t ocopline = PL_copline;
3281 int oexpect = PL_expect;
3282
3283 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
3284 veop, modname, imop);
3285 PL_expect = oexpect;
3286 PL_copline = ocopline;
3287 }
e4783991
GS
3288}
3289
79072805 3290OP *
864dbfa3 3291Perl_dofile(pTHX_ OP *term)
78ca652e
GS
3292{
3293 OP *doop;
3294 GV *gv;
3295
3296 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
3297 if (!(gv && GvIMPORTED_CV(gv)))
3298 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
3299
3300 if (gv && GvIMPORTED_CV(gv)) {
3301 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3302 append_elem(OP_LIST, term,
3303 scalar(newUNOP(OP_RV2CV, 0,
3304 newGVOP(OP_GV, 0,
3305 gv))))));
3306 }
3307 else {
3308 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3309 }
3310 return doop;
3311}
3312
3313OP *
864dbfa3 3314Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
79072805
LW
3315{
3316 return newBINOP(OP_LSLICE, flags,
8990e307
LW
3317 list(force_list(subscript)),
3318 list(force_list(listval)) );
79072805
LW
3319}
3320
76e3520e 3321STATIC I32
cea2e8a9 3322S_list_assignment(pTHX_ register OP *o)
79072805 3323{
11343788 3324 if (!o)
79072805
LW
3325 return TRUE;
3326
11343788
MB
3327 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3328 o = cUNOPo->op_first;
79072805 3329
11343788 3330 if (o->op_type == OP_COND_EXPR) {
1a67a97c
SM
3331 I32 t = list_assignment(cLOGOPo->op_first->op_sibling);
3332 I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
79072805
LW
3333
3334 if (t && f)
3335 return TRUE;
3336 if (t || f)
3337 yyerror("Assignment to both a list and a scalar");
3338 return FALSE;
3339 }
3340
11343788
MB
3341 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3342 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3343 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
79072805
LW
3344 return TRUE;
3345
11343788 3346 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
93a17b20
LW
3347 return TRUE;
3348
11343788 3349 if (o->op_type == OP_RV2SV)
79072805
LW
3350 return FALSE;
3351
3352 return FALSE;
3353}
3354
3355OP *
864dbfa3 3356Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
79072805 3357{
11343788 3358 OP *o;
79072805 3359
a0d0e21e
LW
3360 if (optype) {
3361 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
3362 return newLOGOP(optype, 0,
3363 mod(scalar(left), optype),
3364 newUNOP(OP_SASSIGN, 0, scalar(right)));
3365 }
3366 else {
3367 return newBINOP(optype, OPf_STACKED,
3368 mod(scalar(left), optype), scalar(right));
3369 }
3370 }
3371
79072805 3372 if (list_assignment(left)) {
6ee623d5 3373 dTHR;
10c8fecd
GS
3374 OP *curop;
3375
3280af22
NIS
3376 PL_modcount = 0;
3377 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
463ee0b2 3378 left = mod(left, OP_AASSIGN);
3280af22
NIS
3379 if (PL_eval_start)
3380 PL_eval_start = 0;
748a9306 3381 else {
a0d0e21e
LW
3382 op_free(left);
3383 op_free(right);
3384 return Nullop;
3385 }
10c8fecd
GS
3386 curop = list(force_list(left));
3387 o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
11343788 3388 o->op_private = 0 | (flags >> 8);
10c8fecd
GS
3389 for (curop = ((LISTOP*)curop)->op_first;
3390 curop; curop = curop->op_sibling)
3391 {
3392 if (curop->op_type == OP_RV2HV &&
3393 ((UNOP*)curop)->op_first->op_type != OP_GV) {
3394 o->op_private |= OPpASSIGN_HASH;
3395 break;
3396 }
3397 }
a0d0e21e 3398 if (!(left->op_private & OPpLVAL_INTRO)) {
11343788 3399 OP *lastop = o;
3280af22 3400 PL_generation++;
11343788 3401 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
22c35a8c 3402 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
79072805 3403 if (curop->op_type == OP_GV) {
638eceb6 3404 GV *gv = cGVOPx_gv(curop);
3280af22 3405 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
79072805 3406 break;
3280af22 3407 SvCUR(gv) = PL_generation;
79072805 3408 }
748a9306
LW
3409 else if (curop->op_type == OP_PADSV ||
3410 curop->op_type == OP_PADAV ||
3411 curop->op_type == OP_PADHV ||
3412 curop->op_type == OP_PADANY) {
3280af22 3413 SV **svp = AvARRAY(PL_comppad_name);
8e07c86e 3414 SV *sv = svp[curop->op_targ];
3280af22 3415 if (SvCUR(sv) == PL_generation)
748a9306 3416 break;
3280af22 3417 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
748a9306 3418 }
79072805
LW
3419 else if (curop->op_type == OP_RV2CV)
3420 break;
3421 else if (curop->op_type == OP_RV2SV ||
3422 curop->op_type == OP_RV2AV ||
3423 curop->op_type == OP_RV2HV ||
3424 curop->op_type == OP_RV2GV) {
3425 if (lastop->op_type != OP_GV) /* funny deref? */
3426 break;
3427 }
1167e5da
SM
3428 else if (curop->op_type == OP_PUSHRE) {
3429 if (((PMOP*)curop)->op_pmreplroot) {
b3f5893f
GS
3430#ifdef USE_ITHREADS
3431 GV *gv = (GV*)PL_curpad[(PADOFFSET)((PMOP*)curop)->op_pmreplroot];
3432#else
1167e5da 3433 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
b3f5893f 3434#endif
3280af22 3435 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
1167e5da 3436 break;
3280af22 3437 SvCUR(gv) = PL_generation;
1167e5da
SM
3438 }
3439 }
79072805
LW
3440 else
3441 break;
3442 }
3443 lastop = curop;
3444 }
11343788 3445 if (curop != o)
10c8fecd 3446 o->op_private |= OPpASSIGN_COMMON;
79072805 3447 }
c07a80fd
PP
3448 if (right && right->op_type == OP_SPLIT) {
3449 OP* tmpop;
3450 if ((tmpop = ((LISTOP*)right)->op_first) &&
3451 tmpop->op_type == OP_PUSHRE)
3452 {
3453 PMOP *pm = (PMOP*)tmpop;
3454 if (left->op_type == OP_RV2AV &&
3455 !(left->op_private & OPpLVAL_INTRO) &&
11343788 3456 !(o->op_private & OPpASSIGN_COMMON) )
c07a80fd
PP
3457 {
3458 tmpop = ((UNOP*)left)->op_first;
3459 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
971a9dd3
GS
3460#ifdef USE_ITHREADS
3461 pm->op_pmreplroot = (OP*)cPADOPx(tmpop)->op_padix;
3462 cPADOPx(tmpop)->op_padix = 0; /* steal it */
3463#else
3464 pm->op_pmreplroot = (OP*)cSVOPx(tmpop)->op_sv;
3465 cSVOPx(tmpop)->op_sv = Nullsv; /* steal it */
3466#endif
c07a80fd 3467 pm->op_pmflags |= PMf_ONCE;
11343788 3468 tmpop = cUNOPo->op_first; /* to list (nulled) */
c07a80fd
PP
3469 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3470 tmpop->op_sibling = Nullop; /* don't free split */
3471 right->op_next = tmpop->op_next; /* fix starting loc */
11343788 3472 op_free(o); /* blow off assign */
54310121 3473 right->op_flags &= ~OPf_WANT;
a5f75d66 3474 /* "I don't know and I don't care." */
c07a80fd
PP
3475 return right;
3476 }
3477 }
3478 else {
3280af22 3479 if (PL_modcount < 10000 &&
c07a80fd
PP
3480 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3481 {
3482 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3483 if (SvIVX(sv) == 0)
3280af22 3484 sv_setiv(sv, PL_modcount+1);
c07a80fd
PP
3485 }
3486 }
3487 }
3488 }
11343788 3489 return o;
79072805
LW
3490 }
3491 if (!right)
3492 right = newOP(OP_UNDEF, 0);
3493 if (right->op_type == OP_READLINE) {
3494 right->op_flags |= OPf_STACKED;
463ee0b2 3495 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
79072805 3496 }
a0d0e21e 3497 else {
3280af22 3498 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
11343788 3499 o = newBINOP(OP_SASSIGN, flags,
463ee0b2 3500 scalar(right), mod(scalar(left), OP_SASSIGN) );
3280af22
NIS
3501 if (PL_eval_start)
3502 PL_eval_start = 0;
748a9306 3503 else {
11343788 3504 op_free(o);
a0d0e21e
LW
3505 return Nullop;
3506 }
3507 }
11343788 3508 return o;
79072805
LW
3509}
3510
3511OP *
864dbfa3 3512Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
79072805 3513{
11343788 3514 dTHR;
bbce6d69 3515 U32 seq = intro_my();
79072805
LW
3516 register COP *cop;
3517
b7dc083c 3518 NewOp(1101, cop, 1, COP);
57843af0 3519 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
8990e307 3520 cop->op_type = OP_DBSTATE;
22c35a8c 3521 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
8990e307
LW
3522 }
3523 else {
3524 cop->op_type = OP_NEXTSTATE;
22c35a8c 3525 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
8990e307 3526 }
79072805 3527 cop->op_flags = flags;
393fec97 3528 cop->op_private = (PL_hints & HINT_BYTE);
ff0cee69
PP
3529#ifdef NATIVE_HINTS
3530 cop->op_private |= NATIVE_HINTS;
3531#endif
e24b16f9 3532 PL_compiling.op_private = cop->op_private;
79072805
LW
3533 cop->op_next = (OP*)cop;
3534
463ee0b2
LW
3535 if (label) {
3536 cop->cop_label = label;
3280af22 3537 PL_hints |= HINT_BLOCK_SCOPE;
463ee0b2 3538 }
bbce6d69 3539 cop->cop_seq = seq;
3280af22 3540 cop->cop_arybase = PL_curcop->cop_arybase;
0453d815 3541 if (specialWARN(PL_curcop->cop_warnings))
599cee73 3542 cop->cop_warnings = PL_curcop->cop_warnings ;
1c846c1f 3543