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