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