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