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