This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Builds an passes all tests after integrate and this tweak.
[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 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 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 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 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 287 I32 depth;
288 AV *oldpad;
289 SV *oldsv;
290
291 depth = CvDEPTH(cv);
292 if (!depth) {
9607fc9c 293 if (newoff) {
294 if (SvFAKE(sv))
295 continue;
4fdae800 296 return 0; /* don't clone from inactive stack frame */
9607fc9c 297 }
5f05dabc 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 309 oldsv = Nullsv; /* no need to keep ref */
310 }
311 else {
28757baa 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 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 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 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 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 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 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 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 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 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 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 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 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 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 */
9cbac4c7 1370 if ((gv = gv_fetchpv(SvPV_nolen(sv), FALSE, SVt_PVIO))) {
d38a0a14
SC
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 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 1692{
1693 switch (type) {
1694 case OP_SASSIGN:
5196be3e 1695 if (o->op_type == OP_RV2GV)
3fe9a6f1 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 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? */
0256094b
DM
1959 if (attrs) {
1960 GV *gv = cGVOPx_gv(cUNOPo->op_first);
1961 PL_in_my = FALSE;
1962 PL_in_my_stash = Nullhv;
1963 apply_attrs(GvSTASH(gv),
1964 (type == OP_RV2SV ? GvSV(gv) :
1965 type == OP_RV2AV ? (SV*)GvAV(gv) :
1966 type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)gv),
1967 attrs);
1968 }
192587c2 1969 o->op_private |= OPpOUR_INTRO;
77ca0c92 1970 return o;
dab48698 1971 } else if (type != OP_PADSV &&
93a17b20
LW
1972 type != OP_PADAV &&
1973 type != OP_PADHV &&
1974 type != OP_PUSHMARK)
1975 {
eb64745e
GS
1976 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
1977 PL_op_desc[o->op_type],
1978 PL_in_my == KEY_our ? "our" : "my"));
11343788 1979 return o;
93a17b20 1980 }
09bef843
SB
1981 else if (attrs && type != OP_PUSHMARK) {
1982 HV *stash;
1983 SV *padsv;
1984 SV **namesvp;
1985
eb64745e
GS
1986 PL_in_my = FALSE;
1987 PL_in_my_stash = Nullhv;
1988
09bef843
SB
1989 /* check for C<my Dog $spot> when deciding package */
1990 namesvp = av_fetch(PL_comppad_name, o->op_targ, FALSE);
1991 if (namesvp && *namesvp && SvOBJECT(*namesvp) && HvNAME(SvSTASH(*namesvp)))
1992 stash = SvSTASH(*namesvp);
1993 else
1994 stash = PL_curstash;
1995 padsv = PAD_SV(o->op_targ);
1996 apply_attrs(stash, padsv, attrs);
1997 }
11343788
MB
1998 o->op_flags |= OPf_MOD;
1999 o->op_private |= OPpLVAL_INTRO;
2000 return o;
93a17b20
LW
2001}
2002
2003OP *
09bef843
SB
2004Perl_my_attrs(pTHX_ OP *o, OP *attrs)
2005{
2006 if (o->op_flags & OPf_PARENS)
2007 list(o);
09bef843
SB
2008 if (attrs)
2009 SAVEFREEOP(attrs);
eb64745e
GS
2010 o = my_kid(o, attrs);
2011 PL_in_my = FALSE;
2012 PL_in_my_stash = Nullhv;
2013 return o;
09bef843
SB
2014}
2015
2016OP *
2017Perl_my(pTHX_ OP *o)
2018{
2019 return my_kid(o, Nullop);
2020}
2021
2022OP *
864dbfa3 2023Perl_sawparens(pTHX_ OP *o)
79072805
LW
2024{
2025 if (o)
2026 o->op_flags |= OPf_PARENS;
2027 return o;
2028}
2029
2030OP *
864dbfa3 2031Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
79072805 2032{
11343788 2033 OP *o;
79072805 2034
e476b1b5 2035 if (ckWARN(WARN_MISC) &&
599cee73
PM
2036 (left->op_type == OP_RV2AV ||
2037 left->op_type == OP_RV2HV ||
2038 left->op_type == OP_PADAV ||
2039 left->op_type == OP_PADHV)) {
22c35a8c 2040 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
599cee73
PM
2041 right->op_type == OP_TRANS)
2042 ? right->op_type : OP_MATCH];
dff6d3cd
GS
2043 const char *sample = ((left->op_type == OP_RV2AV ||
2044 left->op_type == OP_PADAV)
2045 ? "@array" : "%hash");
e476b1b5 2046 Perl_warner(aTHX_ WARN_MISC,
1c846c1f 2047 "Applying %s to %s will act on scalar(%s)",
599cee73 2048 desc, sample, sample);
2ae324a7 2049 }
2050
de4bf5b3
MG
2051 if (!(right->op_flags & OPf_STACKED) &&
2052 (right->op_type == OP_MATCH ||
79072805 2053 right->op_type == OP_SUBST ||
de4bf5b3 2054 right->op_type == OP_TRANS)) {
79072805 2055 right->op_flags |= OPf_STACKED;
d897a58d
MG
2056 if (right->op_type != OP_MATCH &&
2057 ! (right->op_type == OP_TRANS &&
2058 right->op_private & OPpTRANS_IDENTICAL))
463ee0b2 2059 left = mod(left, right->op_type);
79072805 2060 if (right->op_type == OP_TRANS)
11343788 2061 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
79072805 2062 else
11343788 2063 o = prepend_elem(right->op_type, scalar(left), right);
79072805 2064 if (type == OP_NOT)
11343788
MB
2065 return newUNOP(OP_NOT, 0, scalar(o));
2066 return o;
79072805
LW
2067 }
2068 else
2069 return bind_match(type, left,
2070 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
2071}
2072
2073OP *
864dbfa3 2074Perl_invert(pTHX_ OP *o)
79072805 2075{
11343788
MB
2076 if (!o)
2077 return o;
79072805 2078 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
11343788 2079 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
79072805
LW
2080}
2081
2082OP *
864dbfa3 2083Perl_scope(pTHX_ OP *o)
79072805
LW
2084{
2085 if (o) {
3280af22 2086 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
463ee0b2
LW
2087 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
2088 o->op_type = OP_LEAVE;
22c35a8c 2089 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
463ee0b2
LW
2090 }
2091 else {
2092 if (o->op_type == OP_LINESEQ) {
2093 OP *kid;
2094 o->op_type = OP_SCOPE;
22c35a8c 2095 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
c3ed7a6a
GS
2096 kid = ((LISTOP*)o)->op_first;
2097 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
2098 null(kid);
463ee0b2
LW
2099 }
2100 else
748a9306 2101 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
463ee0b2 2102 }
79072805
LW
2103 }
2104 return o;
2105}
2106
b3ac6de7 2107void
864dbfa3 2108Perl_save_hints(pTHX)
b3ac6de7 2109{
3280af22
NIS
2110 SAVEI32(PL_hints);
2111 SAVESPTR(GvHV(PL_hintgv));
2112 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
2113 SAVEFREESV(GvHV(PL_hintgv));
b3ac6de7
IZ
2114}
2115
a0d0e21e 2116int
864dbfa3 2117Perl_block_start(pTHX_ int full)
79072805 2118{
3280af22 2119 int retval = PL_savestack_ix;
b3ac6de7 2120
3280af22 2121 SAVEI32(PL_comppad_name_floor);
43d4d5c6
GS
2122 PL_comppad_name_floor = AvFILLp(PL_comppad_name);
2123 if (full)
2124 PL_comppad_name_fill = PL_comppad_name_floor;
2125 if (PL_comppad_name_floor < 0)
2126 PL_comppad_name_floor = 0;
3280af22
NIS
2127 SAVEI32(PL_min_intro_pending);
2128 SAVEI32(PL_max_intro_pending);
2129 PL_min_intro_pending = 0;
2130 SAVEI32(PL_comppad_name_fill);
2131 SAVEI32(PL_padix_floor);
2132 PL_padix_floor = PL_padix;
2133 PL_pad_reset_pending = FALSE;
b3ac6de7 2134 SAVEHINTS();
3280af22 2135 PL_hints &= ~HINT_BLOCK_SCOPE;
1c846c1f 2136 SAVESPTR(PL_compiling.cop_warnings);
0453d815 2137 if (! specialWARN(PL_compiling.cop_warnings)) {
599cee73
PM
2138 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
2139 SAVEFREESV(PL_compiling.cop_warnings) ;
2140 }
ac27b0f5
NIS
2141 SAVESPTR(PL_compiling.cop_io);
2142 if (! specialCopIO(PL_compiling.cop_io)) {
2143 PL_compiling.cop_io = newSVsv(PL_compiling.cop_io) ;
2144 SAVEFREESV(PL_compiling.cop_io) ;
2145 }
a0d0e21e
LW
2146 return retval;
2147}
2148
2149OP*
864dbfa3 2150Perl_block_end(pTHX_ I32 floor, OP *seq)
a0d0e21e 2151{
3280af22 2152 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
a0d0e21e 2153 OP* retval = scalarseq(seq);
a0d0e21e 2154 LEAVE_SCOPE(floor);
3280af22 2155 PL_pad_reset_pending = FALSE;
e24b16f9 2156 PL_compiling.op_private = PL_hints;
a0d0e21e 2157 if (needblockscope)
3280af22
NIS
2158 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
2159 pad_leavemy(PL_comppad_name_fill);
2160 PL_cop_seqmax++;
a0d0e21e
LW
2161 return retval;
2162}
2163
76e3520e 2164STATIC OP *
cea2e8a9 2165S_newDEFSVOP(pTHX)
54b9620d
MB
2166{
2167#ifdef USE_THREADS
2168 OP *o = newOP(OP_THREADSV, 0);
2169 o->op_targ = find_threadsv("_");
2170 return o;
2171#else
3280af22 2172 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
54b9620d
MB
2173#endif /* USE_THREADS */
2174}
2175
a0d0e21e 2176void
864dbfa3 2177Perl_newPROG(pTHX_ OP *o)
a0d0e21e 2178{
3280af22 2179 if (PL_in_eval) {
b295d113
TH
2180 if (PL_eval_root)
2181 return;
faef0170
HS
2182 PL_eval_root = newUNOP(OP_LEAVEEVAL,
2183 ((PL_in_eval & EVAL_KEEPERR)
2184 ? OPf_SPECIAL : 0), o);
3280af22 2185 PL_eval_start = linklist(PL_eval_root);
7934575e
GS
2186 PL_eval_root->op_private |= OPpREFCOUNTED;
2187 OpREFCNT_set(PL_eval_root, 1);
3280af22
NIS
2188 PL_eval_root->op_next = 0;
2189 peep(PL_eval_start);
a0d0e21e
LW
2190 }
2191 else {
5dc0d613 2192 if (!o)
a0d0e21e 2193 return;
3280af22
NIS
2194 PL_main_root = scope(sawparens(scalarvoid(o)));
2195 PL_curcop = &PL_compiling;
2196 PL_main_start = LINKLIST(PL_main_root);
7934575e
GS
2197 PL_main_root->op_private |= OPpREFCOUNTED;
2198 OpREFCNT_set(PL_main_root, 1);
3280af22
NIS
2199 PL_main_root->op_next = 0;
2200 peep(PL_main_start);
2201 PL_compcv = 0;
3841441e 2202
4fdae800 2203 /* Register with debugger */
84902520 2204 if (PERLDB_INTER) {
864dbfa3 2205 CV *cv = get_cv("DB::postponed", FALSE);
3841441e
CS
2206 if (cv) {
2207 dSP;
924508f0 2208 PUSHMARK(SP);
cc49e20b 2209 XPUSHs((SV*)CopFILEGV(&PL_compiling));
3841441e 2210 PUTBACK;
864dbfa3 2211 call_sv((SV*)cv, G_DISCARD);
3841441e
CS
2212 }
2213 }
79072805 2214 }
79072805
LW
2215}
2216
2217OP *
864dbfa3 2218Perl_localize(pTHX_ OP *o, I32 lex)
79072805
LW
2219{
2220 if (o->op_flags & OPf_PARENS)
2221 list(o);
8990e307 2222 else {
599cee73 2223 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
8990e307 2224 char *s;
fd400ab9 2225 for (s = PL_bufptr; *s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s) || strchr("@$%, ",*s)); s++) ;
a0d0e21e 2226 if (*s == ';' || *s == '=')
eb64745e
GS
2227 Perl_warner(aTHX_ WARN_PARENTHESIS,
2228 "Parentheses missing around \"%s\" list",
2229 lex ? (PL_in_my == KEY_our ? "our" : "my") : "local");
8990e307
LW
2230 }
2231 }
93a17b20 2232 if (lex)
eb64745e 2233 o = my(o);
93a17b20 2234 else
eb64745e
GS
2235 o = mod(o, OP_NULL); /* a bit kludgey */
2236 PL_in_my = FALSE;
2237 PL_in_my_stash = Nullhv;
2238 return o;
79072805
LW
2239}
2240
2241OP *
864dbfa3 2242Perl_jmaybe(pTHX_ OP *o)
79072805
LW
2243{
2244 if (o->op_type == OP_LIST) {
554b3eca
MB
2245 OP *o2;
2246#ifdef USE_THREADS
2faa37cc 2247 o2 = newOP(OP_THREADSV, 0);
54b9620d 2248 o2->op_targ = find_threadsv(";");
554b3eca
MB
2249#else
2250 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
2251#endif /* USE_THREADS */
2252 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
79072805
LW
2253 }
2254 return o;
2255}
2256
2257OP *
864dbfa3 2258Perl_fold_constants(pTHX_ register OP *o)
79072805
LW
2259{
2260 register OP *curop;
2261 I32 type = o->op_type;
748a9306 2262 SV *sv;
79072805 2263
22c35a8c 2264 if (PL_opargs[type] & OA_RETSCALAR)
79072805 2265 scalar(o);
b162f9ea 2266 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
ed6116ce 2267 o->op_targ = pad_alloc(type, SVs_PADTMP);
79072805 2268
eac055e9
GS
2269 /* integerize op, unless it happens to be C<-foo>.
2270 * XXX should pp_i_negate() do magic string negation instead? */
2271 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2272 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2273 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2274 {
22c35a8c 2275 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
eac055e9 2276 }
85e6fe83 2277
22c35a8c 2278 if (!(PL_opargs[type] & OA_FOLDCONST))
79072805
LW
2279 goto nope;
2280
de939608 2281 switch (type) {
7a52d87a
GS
2282 case OP_NEGATE:
2283 /* XXX might want a ck_negate() for this */
2284 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2285 break;
de939608
CS
2286 case OP_SPRINTF:
2287 case OP_UCFIRST:
2288 case OP_LCFIRST:
2289 case OP_UC:
2290 case OP_LC:
69dcf70c
MB
2291 case OP_SLT:
2292 case OP_SGT:
2293 case OP_SLE:
2294 case OP_SGE:
2295 case OP_SCMP:
2296
de939608
CS
2297 if (o->op_private & OPpLOCALE)
2298 goto nope;
2299 }
2300
3280af22 2301 if (PL_error_count)
a0d0e21e
LW
2302 goto nope; /* Don't try to run w/ errors */
2303
79072805 2304 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
11fa937b
GS
2305 if ((curop->op_type != OP_CONST ||
2306 (curop->op_private & OPpCONST_BARE)) &&
7a52d87a
GS
2307 curop->op_type != OP_LIST &&
2308 curop->op_type != OP_SCALAR &&
2309 curop->op_type != OP_NULL &&
2310 curop->op_type != OP_PUSHMARK)
2311 {
79072805
LW
2312 goto nope;
2313 }
2314 }
2315
2316 curop = LINKLIST(o);
2317 o->op_next = 0;
533c011a 2318 PL_op = curop;
cea2e8a9 2319 CALLRUNOPS(aTHX);
3280af22 2320 sv = *(PL_stack_sp--);
748a9306 2321 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
79072805 2322 pad_swipe(o->op_targ);
748a9306
LW
2323 else if (SvTEMP(sv)) { /* grab mortal temp? */
2324 (void)SvREFCNT_inc(sv);
2325 SvTEMP_off(sv);
85e6fe83 2326 }
79072805
LW
2327 op_free(o);
2328 if (type == OP_RV2GV)
b1cb66bf 2329 return newGVOP(OP_GV, 0, (GV*)sv);
748a9306 2330 else {
ee580363
GS
2331 /* try to smush double to int, but don't smush -2.0 to -2 */
2332 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
2333 type != OP_NEGATE)
2334 {
28e5dec8
JH
2335#ifdef PERL_PRESERVE_IVUV
2336 /* Only bother to attempt to fold to IV if
2337 most operators will benefit */
2338 SvIV_please(sv);
2339#endif
748a9306
LW
2340 }
2341 return newSVOP(OP_CONST, 0, sv);
2342 }
aeea060c 2343
79072805 2344 nope:
22c35a8c 2345 if (!(PL_opargs[type] & OA_OTHERINT))
79072805 2346 return o;
79072805 2347
3280af22 2348 if (!(PL_hints & HINT_INTEGER)) {
4bb9f687
GS
2349 if (type == OP_MODULO
2350 || type == OP_DIVIDE
2351 || !(o->op_flags & OPf_KIDS))
2352 {
85e6fe83 2353 return o;
4bb9f687 2354 }
85e6fe83
LW
2355
2356 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
2357 if (curop->op_type == OP_CONST) {
b1cb66bf 2358 if (SvIOK(((SVOP*)curop)->op_sv))
85e6fe83
LW
2359 continue;
2360 return o;
2361 }
22c35a8c 2362 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
79072805
LW
2363 continue;
2364 return o;
2365 }
22c35a8c 2366 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
79072805
LW
2367 }
2368
79072805
LW
2369 return o;
2370}
2371
2372OP *
864dbfa3 2373Perl_gen_constant_list(pTHX_ register OP *o)
79072805
LW
2374{
2375 register OP *curop;
3280af22 2376 I32 oldtmps_floor = PL_tmps_floor;
79072805 2377
a0d0e21e 2378 list(o);
3280af22 2379 if (PL_error_count)
a0d0e21e
LW
2380 return o; /* Don't attempt to run with errors */
2381
533c011a 2382 PL_op = curop = LINKLIST(o);
a0d0e21e 2383 o->op_next = 0;
7d4045d4 2384 peep(curop);
cea2e8a9
GS
2385 pp_pushmark();
2386 CALLRUNOPS(aTHX);
533c011a 2387 PL_op = curop;
cea2e8a9 2388 pp_anonlist();
3280af22 2389 PL_tmps_floor = oldtmps_floor;
79072805
LW
2390
2391 o->op_type = OP_RV2AV;
22c35a8c 2392 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
79072805 2393 curop = ((UNOP*)o)->op_first;
3280af22 2394 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
79072805 2395 op_free(curop);
79072805
LW
2396 linklist(o);
2397 return list(o);
2398}
2399
2400OP *
864dbfa3 2401Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
79072805 2402{
11343788
MB
2403 if (!o || o->op_type != OP_LIST)
2404 o = newLISTOP(OP_LIST, 0, o, Nullop);
748a9306 2405 else
5dc0d613 2406 o->op_flags &= ~OPf_WANT;
79072805 2407
22c35a8c 2408 if (!(PL_opargs[type] & OA_MARK))
11343788 2409 null(cLISTOPo->op_first);
8990e307 2410
11343788 2411 o->op_type = type;
22c35a8c 2412 o->op_ppaddr = PL_ppaddr[type];
11343788 2413 o->op_flags |= flags;
79072805 2414
11343788
MB
2415 o = CHECKOP(type, o);
2416 if (o->op_type != type)
2417 return o;
79072805 2418
11343788 2419 return fold_constants(o);
79072805
LW
2420}
2421
2422/* List constructors */
2423
2424OP *
864dbfa3 2425Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
79072805
LW
2426{
2427 if (!first)
2428 return last;
8990e307
LW
2429
2430 if (!last)
79072805 2431 return first;
8990e307 2432
155aba94
GS
2433 if (first->op_type != type
2434 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
2435 {
2436 return newLISTOP(type, 0, first, last);
2437 }
79072805 2438
a0d0e21e
LW
2439 if (first->op_flags & OPf_KIDS)
2440 ((LISTOP*)first)->op_last->op_sibling = last;
2441 else {
2442 first->op_flags |= OPf_KIDS;
2443 ((LISTOP*)first)->op_first = last;
2444 }
2445 ((LISTOP*)first)->op_last = last;
a0d0e21e 2446 return first;
79072805
LW
2447}
2448
2449OP *
864dbfa3 2450Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
79072805
LW
2451{
2452 if (!first)
2453 return (OP*)last;
8990e307
LW
2454
2455 if (!last)
79072805 2456 return (OP*)first;
8990e307
LW
2457
2458 if (first->op_type != type)
79072805 2459 return prepend_elem(type, (OP*)first, (OP*)last);
8990e307
LW
2460
2461 if (last->op_type != type)
79072805
LW
2462 return append_elem(type, (OP*)first, (OP*)last);
2463
2464 first->op_last->op_sibling = last->op_first;
2465 first->op_last = last->op_last;
117dada2 2466 first->op_flags |= (last->op_flags & OPf_KIDS);
1c846c1f 2467
b7dc083c
NIS
2468#ifdef PL_OP_SLAB_ALLOC
2469#else
1c846c1f 2470 Safefree(last);
b7dc083c 2471#endif
79072805
LW
2472 return (OP*)first;
2473}
2474
2475OP *
864dbfa3 2476Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
79072805
LW
2477{
2478 if (!first)
2479 return last;
8990e307
LW
2480
2481 if (!last)
79072805 2482 return first;
8990e307
LW
2483
2484 if (last->op_type == type) {
2485 if (type == OP_LIST) { /* already a PUSHMARK there */
2486 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2487 ((LISTOP*)last)->op_first->op_sibling = first;
36a5d4ba
DC
2488 if (!(first->op_flags & OPf_PARENS))
2489 last->op_flags &= ~OPf_PARENS;
8990e307
LW
2490 }
2491 else {
2492 if (!(last->op_flags & OPf_KIDS)) {
2493 ((LISTOP*)last)->op_last = first;
2494 last->op_flags |= OPf_KIDS;
2495 }
2496 first->op_sibling = ((LISTOP*)last)->op_first;
2497 ((LISTOP*)last)->op_first = first;
79072805 2498 }
117dada2 2499 last->op_flags |= OPf_KIDS;
79072805
LW
2500 return last;
2501 }
2502
2503 return newLISTOP(type, 0, first, last);
2504}
2505
2506/* Constructors */
2507
2508OP *
864dbfa3 2509Perl_newNULLLIST(pTHX)
79072805 2510{
8990e307
LW
2511 return newOP(OP_STUB, 0);
2512}
2513
2514OP *
864dbfa3 2515Perl_force_list(pTHX_ OP *o)
8990e307 2516{
11343788
MB
2517 if (!o || o->op_type != OP_LIST)
2518 o = newLISTOP(OP_LIST, 0, o, Nullop);
2519 null(o);
2520 return o;
79072805
LW
2521}
2522
2523OP *
864dbfa3 2524Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
79072805
LW
2525{
2526 LISTOP *listop;
2527
b7dc083c 2528 NewOp(1101, listop, 1, LISTOP);
79072805
LW
2529
2530 listop->op_type = type;
22c35a8c 2531 listop->op_ppaddr = PL_ppaddr[type];
117dada2
SM
2532 if (first || last)
2533 flags |= OPf_KIDS;
79072805 2534 listop->op_flags = flags;
79072805
LW
2535
2536 if (!last && first)
2537 last = first;
2538 else if (!first && last)
2539 first = last;
8990e307
LW
2540 else if (first)
2541 first->op_sibling = last;
79072805
LW
2542 listop->op_first = first;
2543 listop->op_last = last;
8990e307
LW
2544 if (type == OP_LIST) {
2545 OP* pushop;
2546 pushop = newOP(OP_PUSHMARK, 0);
2547 pushop->op_sibling = first;
2548 listop->op_first = pushop;
2549 listop->op_flags |= OPf_KIDS;
2550 if (!last)
2551 listop->op_last = pushop;
2552 }
79072805
LW
2553
2554 return (OP*)listop;
2555}
2556
2557OP *
864dbfa3 2558Perl_newOP(pTHX_ I32 type, I32 flags)
79072805 2559{
11343788 2560 OP *o;
b7dc083c 2561 NewOp(1101, o, 1, OP);
11343788 2562 o->op_type = type;
22c35a8c 2563 o->op_ppaddr = PL_ppaddr[type];
11343788 2564 o->op_flags = flags;
79072805 2565
11343788
MB
2566 o->op_next = o;
2567 o->op_private = 0 + (flags >> 8);
22c35a8c 2568 if (PL_opargs[type] & OA_RETSCALAR)
11343788 2569 scalar(o);
22c35a8c 2570 if (PL_opargs[type] & OA_TARGET)
11343788
MB
2571 o->op_targ = pad_alloc(type, SVs_PADTMP);
2572 return CHECKOP(type, o);
79072805
LW
2573}
2574
2575OP *
864dbfa3 2576Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
79072805
LW
2577{
2578 UNOP *unop;
2579
93a17b20 2580 if (!first)
aeea060c 2581 first = newOP(OP_STUB, 0);
22c35a8c 2582 if (PL_opargs[type] & OA_MARK)
8990e307 2583 first = force_list(first);
93a17b20 2584
b7dc083c 2585 NewOp(1101, unop, 1, UNOP);
79072805 2586 unop->op_type = type;
22c35a8c 2587 unop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2588 unop->op_first = first;
2589 unop->op_flags = flags | OPf_KIDS;
c07a80fd 2590 unop->op_private = 1 | (flags >> 8);
e50aee73 2591 unop = (UNOP*) CHECKOP(type, unop);
79072805
LW
2592 if (unop->op_next)
2593 return (OP*)unop;
2594
a0d0e21e 2595 return fold_constants((OP *) unop);
79072805
LW
2596}
2597
2598OP *
864dbfa3 2599Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
79072805
LW
2600{
2601 BINOP *binop;
b7dc083c 2602 NewOp(1101, binop, 1, BINOP);
79072805
LW
2603
2604 if (!first)
2605 first = newOP(OP_NULL, 0);
2606
2607 binop->op_type = type;
22c35a8c 2608 binop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2609 binop->op_first = first;
2610 binop->op_flags = flags | OPf_KIDS;
2611 if (!last) {
2612 last = first;
c07a80fd 2613 binop->op_private = 1 | (flags >> 8);
79072805
LW
2614 }
2615 else {
c07a80fd 2616 binop->op_private = 2 | (flags >> 8);
79072805
LW
2617 first->op_sibling = last;
2618 }
2619
e50aee73 2620 binop = (BINOP*)CHECKOP(type, binop);
b162f9ea 2621 if (binop->op_next || binop->op_type != type)
79072805
LW
2622 return (OP*)binop;
2623
7284ab6f 2624 binop->op_last = binop->op_first->op_sibling;
79072805 2625
a0d0e21e 2626 return fold_constants((OP *)binop);
79072805
LW
2627}
2628
a0ed51b3
LW
2629static int
2630utf8compare(const void *a, const void *b)
2631{
2632 int i;
2633 for (i = 0; i < 10; i++) {
2634 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2635 return -1;
2636 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2637 return 1;
2638 }
2639 return 0;
2640}
2641
79072805 2642OP *
864dbfa3 2643Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
79072805 2644{
79072805
LW
2645 SV *tstr = ((SVOP*)expr)->op_sv;
2646 SV *rstr = ((SVOP*)repl)->op_sv;
463ee0b2
LW
2647 STRLEN tlen;
2648 STRLEN rlen;
9b877dbb
IH
2649 U8 *t = (U8*)SvPV(tstr, tlen);
2650 U8 *r = (U8*)SvPV(rstr, rlen);
79072805
LW
2651 register I32 i;
2652 register I32 j;
a0ed51b3 2653 I32 del;
79072805 2654 I32 complement;
5d06d08e 2655 I32 squash;
9b877dbb 2656 I32 grows = 0;
79072805
LW
2657 register short *tbl;
2658
11343788 2659 complement = o->op_private & OPpTRANS_COMPLEMENT;
a0ed51b3 2660 del = o->op_private & OPpTRANS_DELETE;
5d06d08e 2661 squash = o->op_private & OPpTRANS_SQUASH;
1c846c1f 2662
036b4402
GS
2663 if (SvUTF8(tstr))
2664 o->op_private |= OPpTRANS_FROM_UTF;
1c846c1f
NIS
2665
2666 if (SvUTF8(rstr))
036b4402 2667 o->op_private |= OPpTRANS_TO_UTF;
79072805 2668
a0ed51b3 2669 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
79cb57f6 2670 SV* listsv = newSVpvn("# comment\n",10);
a0ed51b3
LW
2671 SV* transv = 0;
2672 U8* tend = t + tlen;
2673 U8* rend = r + rlen;
ba210ebe 2674 STRLEN ulen;
a0ed51b3
LW
2675 U32 tfirst = 1;
2676 U32 tlast = 0;
2677 I32 tdiff;
2678 U32 rfirst = 1;
2679 U32 rlast = 0;
2680 I32 rdiff;
2681 I32 diff;
2682 I32 none = 0;
2683 U32 max = 0;
2684 I32 bits;
a0ed51b3
LW
2685 I32 havefinal = 0;
2686 U32 final;
a0ed51b3
LW
2687 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2688 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
9b877dbb
IH
2689 U8* tsave = from_utf ? NULL : trlist_upgrade(&t, &tend);
2690 U8* rsave = to_utf ? NULL : trlist_upgrade(&r, &rend);
a0ed51b3
LW
2691
2692 if (complement) {
ad391ad9 2693 U8 tmpbuf[UTF8_MAXLEN+1];
a0ed51b3
LW
2694 U8** cp;
2695 UV nextmin = 0;
2696 New(1109, cp, tlen, U8*);
2697 i = 0;
79cb57f6 2698 transv = newSVpvn("",0);
a0ed51b3
LW
2699 while (t < tend) {
2700 cp[i++] = t;
2701 t += UTF8SKIP(t);
455d824a 2702 if (t < tend && *t == 0xff) {
a0ed51b3
LW
2703 t++;
2704 t += UTF8SKIP(t);
2705 }
2706 }
2707 qsort(cp, i, sizeof(U8*), utf8compare);
2708 for (j = 0; j < i; j++) {
2709 U8 *s = cp[j];
455d824a 2710 I32 cur = j < i - 1 ? cp[j+1] - s : tend - s;
dcad2880 2711 UV val = utf8_to_uv(s, cur, &ulen, 0);
a0ed51b3
LW
2712 s += ulen;
2713 diff = val - nextmin;
2714 if (diff > 0) {
2715 t = uv_to_utf8(tmpbuf,nextmin);
dfe13c55 2716 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2717 if (diff > 1) {
2718 t = uv_to_utf8(tmpbuf, val - 1);
2719 sv_catpvn(transv, "\377", 1);
dfe13c55 2720 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2721 }
2722 }
455d824a 2723 if (s < tend && *s == 0xff)
dcad2880 2724 val = utf8_to_uv(s+1, cur - 1, &ulen, 0);
a0ed51b3
LW
2725 if (val >= nextmin)
2726 nextmin = val + 1;
2727 }
2728 t = uv_to_utf8(tmpbuf,nextmin);
dfe13c55 2729 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2730 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2731 sv_catpvn(transv, "\377", 1);
dfe13c55
GS
2732 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2733 t = (U8*)SvPVX(transv);
a0ed51b3
LW
2734 tlen = SvCUR(transv);
2735 tend = t + tlen;
455d824a 2736 Safefree(cp);
a0ed51b3
LW
2737 }
2738 else if (!rlen && !del) {
2739 r = t; rlen = tlen; rend = tend;
4757a243
LW
2740 }
2741 if (!squash) {
12ae5dfc
JH
2742 if (t == r ||
2743 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
01ec43d0 2744 {
4757a243 2745 o->op_private |= OPpTRANS_IDENTICAL;
01ec43d0 2746 }
a0ed51b3
LW
2747 }
2748
2749 while (t < tend || tfirst <= tlast) {
2750 /* see if we need more "t" chars */
2751 if (tfirst > tlast) {
dcad2880 2752 tfirst = (I32)utf8_to_uv(t, tend - t, &ulen, 0);
a0ed51b3
LW
2753 t += ulen;
2754 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
ba210ebe 2755 t++;
dcad2880 2756 tlast = (I32)utf8_to_uv(t, tend - t, &ulen, 0);
a0ed51b3
LW
2757 t += ulen;
2758 }
2759 else
2760 tlast = tfirst;
2761 }
2762
2763 /* now see if we need more "r" chars */
2764 if (rfirst > rlast) {
2765 if (r < rend) {
dcad2880 2766 rfirst = (I32)utf8_to_uv(r, rend - r, &ulen, 0);
a0ed51b3
LW
2767 r += ulen;
2768 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
ba210ebe 2769 r++;
dcad2880 2770 rlast = (I32)utf8_to_uv(r, rend - r, &ulen, 0);
a0ed51b3
LW
2771 r += ulen;
2772 }
2773 else
2774 rlast = rfirst;
2775 }
2776 else {
2777 if (!havefinal++)
2778 final = rlast;
2779 rfirst = rlast = 0xffffffff;
2780 }
2781 }
2782
2783 /* now see which range will peter our first, if either. */
2784 tdiff = tlast - tfirst;
2785 rdiff = rlast - rfirst;
2786
2787 if (tdiff <= rdiff)
2788 diff = tdiff;
2789 else
2790 diff = rdiff;
2791
2792 if (rfirst == 0xffffffff) {
2793 diff = tdiff; /* oops, pretend rdiff is infinite */
2794 if (diff > 0)
894356b3
GS
2795 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
2796 (long)tfirst, (long)tlast);
a0ed51b3 2797 else
894356b3 2798 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
a0ed51b3
LW
2799 }
2800 else {
2801 if (diff > 0)
894356b3
GS
2802 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
2803 (long)tfirst, (long)(tfirst + diff),
2804 (long)rfirst);
a0ed51b3 2805 else
894356b3
GS
2806 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
2807 (long)tfirst, (long)rfirst);
a0ed51b3
LW
2808
2809 if (rfirst + diff > max)
2810 max = rfirst + diff;
2811 rfirst += diff + 1;
9b877dbb
IH
2812 if (!grows)
2813 grows = (UNISKIP(tfirst) < UNISKIP(rfirst));
a0ed51b3
LW
2814 }
2815 tfirst += diff + 1;
2816 }
2817
2818 none = ++max;
2819 if (del)
2820 del = ++max;
2821
2822 if (max > 0xffff)
2823 bits = 32;
2824 else if (max > 0xff)
2825 bits = 16;
2826 else
2827 bits = 8;
2828
455d824a 2829 Safefree(cPVOPo->op_pv);
a0ed51b3
LW
2830 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2831 SvREFCNT_dec(listsv);
2832 if (transv)
2833 SvREFCNT_dec(transv);
2834
2835 if (!del && havefinal)
b448e4fe
JH
2836 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5,
2837 newSVuv((UV)final), 0);
a0ed51b3 2838
9b877dbb 2839 if (grows)
a0ed51b3
LW
2840 o->op_private |= OPpTRANS_GROWS;
2841
9b877dbb
IH
2842 if (tsave)
2843 Safefree(tsave);
2844 if (rsave)
2845 Safefree(rsave);
2846
a0ed51b3
LW
2847 op_free(expr);
2848 op_free(repl);
2849 return o;
2850 }
2851
2852 tbl = (short*)cPVOPo->op_pv;
79072805
LW
2853 if (complement) {
2854 Zero(tbl, 256, short);
2855 for (i = 0; i < tlen; i++)
ec49126f 2856 tbl[t[i]] = -1;
79072805
LW
2857 for (i = 0, j = 0; i < 256; i++) {
2858 if (!tbl[i]) {
2859 if (j >= rlen) {
a0ed51b3 2860 if (del)
79072805
LW
2861 tbl[i] = -2;
2862 else if (rlen)
ec49126f 2863 tbl[i] = r[j-1];
79072805
LW
2864 else
2865 tbl[i] = i;
2866 }
9b877dbb
IH
2867 else {
2868 if (i < 128 && r[j] >= 128)
2869 grows = 1;
ec49126f 2870 tbl[i] = r[j++];
9b877dbb 2871 }
79072805
LW
2872 }
2873 }
8973db79
JH
2874 if (!del) {
2875 if (j >= rlen)
2876 j = rlen - 1;
2877 else
2878 cPVOPo->op_pv = (char*)Renew(tbl, 0x101+rlen-j, short);
2879 tbl[0x100] = rlen - j;
2880 for (i=0; i < rlen - j; i++)
2881 tbl[0x101+i] = r[j+i];
2882 }
79072805
LW
2883 }
2884 else {
a0ed51b3 2885 if (!rlen && !del) {
79072805 2886 r = t; rlen = tlen;
5d06d08e 2887 if (!squash)
4757a243 2888 o->op_private |= OPpTRANS_IDENTICAL;
79072805
LW
2889 }
2890 for (i = 0; i < 256; i++)
2891 tbl[i] = -1;
2892 for (i = 0, j = 0; i < tlen; i++,j++) {
2893 if (j >= rlen) {
a0ed51b3 2894 if (del) {
ec49126f 2895 if (tbl[t[i]] == -1)
2896 tbl[t[i]] = -2;
79072805
LW
2897 continue;
2898 }
2899 --j;
2900 }
9b877dbb
IH
2901 if (tbl[t[i]] == -1) {
2902 if (t[i] < 128 && r[j] >= 128)
2903 grows = 1;
ec49126f 2904 tbl[t[i]] = r[j];
9b877dbb 2905 }
79072805
LW
2906 }
2907 }
9b877dbb
IH
2908 if (grows)
2909 o->op_private |= OPpTRANS_GROWS;
79072805
LW
2910 op_free(expr);
2911 op_free(repl);
2912
11343788 2913 return o;
79072805
LW
2914}
2915
2916OP *
864dbfa3 2917Perl_newPMOP(pTHX_ I32 type, I32 flags)
79072805
LW
2918{
2919 PMOP *pmop;
2920
b7dc083c 2921 NewOp(1101, pmop, 1, PMOP);
79072805 2922 pmop->op_type = type;
22c35a8c 2923 pmop->op_ppaddr = PL_ppaddr[type];
79072805 2924 pmop->op_flags = flags;
c07a80fd 2925 pmop->op_private = 0 | (flags >> 8);
79072805 2926
3280af22 2927 if (PL_hints & HINT_RE_TAINT)
b3eb6a9b 2928 pmop->op_pmpermflags |= PMf_RETAINT;
3280af22 2929 if (PL_hints & HINT_LOCALE)
b3eb6a9b
GS
2930 pmop->op_pmpermflags |= PMf_LOCALE;
2931 pmop->op_pmflags = pmop->op_pmpermflags;
36477c24 2932
79072805 2933 /* link into pm list */
3280af22
NIS
2934 if (type != OP_TRANS && PL_curstash) {
2935 pmop->op_pmnext = HvPMROOT(PL_curstash);
2936 HvPMROOT(PL_curstash) = pmop;
79072805
LW
2937 }
2938
2939 return (OP*)pmop;
2940}
2941
2942OP *
864dbfa3 2943Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
79072805
LW
2944{
2945 PMOP *pm;
2946 LOGOP *rcop;
ce862d02 2947 I32 repl_has_vars = 0;
79072805 2948
11343788
MB
2949 if (o->op_type == OP_TRANS)
2950 return pmtrans(o, expr, repl);
79072805 2951
3280af22 2952 PL_hints |= HINT_BLOCK_SCOPE;
11343788 2953 pm = (PMOP*)o;
79072805
LW
2954
2955 if (expr->op_type == OP_CONST) {
463ee0b2 2956 STRLEN plen;
79072805 2957 SV *pat = ((SVOP*)expr)->op_sv;
463ee0b2 2958 char *p = SvPV(pat, plen);
11343788 2959 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
93a17b20 2960 sv_setpvn(pat, "\\s+", 3);
463ee0b2 2961 p = SvPV(pat, plen);
79072805
LW
2962 pm->op_pmflags |= PMf_SKIPWHITE;
2963 }
1fd7b382 2964 if ((PL_hints & HINT_UTF8) || DO_UTF8(pat))
7e2040f0 2965 pm->op_pmdynflags |= PMdf_UTF8;
cea2e8a9 2966 pm->op_pmregexp = CALLREGCOMP(aTHX_ p, p + plen, pm);
aeea060c 2967 if (strEQ("\\s+", pm->op_pmregexp->precomp))
85e6fe83 2968 pm->op_pmflags |= PMf_WHITE;
79072805
LW
2969 op_free(expr);
2970 }
2971 else {
393fec97
GS
2972 if (PL_hints & HINT_UTF8)
2973 pm->op_pmdynflags |= PMdf_UTF8;
3280af22 2974 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
1c846c1f 2975 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2cd61cdb
IZ
2976 ? OP_REGCRESET
2977 : OP_REGCMAYBE),0,expr);
463ee0b2 2978
b7dc083c 2979 NewOp(1101, rcop, 1, LOGOP);
79072805 2980 rcop->op_type = OP_REGCOMP;
22c35a8c 2981 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
79072805 2982 rcop->op_first = scalar(expr);
1c846c1f 2983 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2cd61cdb
IZ
2984 ? (OPf_SPECIAL | OPf_KIDS)
2985 : OPf_KIDS);
79072805 2986 rcop->op_private = 1;
11343788 2987 rcop->op_other = o;
79072805
LW
2988
2989 /* establish postfix order */
3280af22 2990 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
463ee0b2
LW
2991 LINKLIST(expr);
2992 rcop->op_next = expr;
2993 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2994 }
2995 else {
2996 rcop->op_next = LINKLIST(expr);
2997 expr->op_next = (OP*)rcop;
2998 }
79072805 2999
11343788 3000 prepend_elem(o->op_type, scalar((OP*)rcop), o);
79072805
LW
3001 }
3002
3003 if (repl) {
748a9306 3004 OP *curop;
0244c3a4 3005 if (pm->op_pmflags & PMf_EVAL) {
748a9306 3006 curop = 0;
57843af0
GS
3007 if (CopLINE(PL_curcop) < PL_multi_end)
3008 CopLINE_set(PL_curcop, PL_multi_end);
0244c3a4 3009 }
554b3eca 3010#ifdef USE_THREADS
2faa37cc 3011 else if (repl->op_type == OP_THREADSV
554b3eca 3012 && strchr("&`'123456789+",
533c011a 3013 PL_threadsv_names[repl->op_targ]))
554b3eca
MB
3014 {
3015 curop = 0;
3016 }
3017#endif /* USE_THREADS */
748a9306
LW
3018 else if (repl->op_type == OP_CONST)
3019 curop = repl;
79072805 3020 else {
79072805
LW
3021 OP *lastop = 0;
3022 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
22c35a8c 3023 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
554b3eca 3024#ifdef USE_THREADS
ce862d02
IZ
3025 if (curop->op_type == OP_THREADSV) {
3026 repl_has_vars = 1;
be949f6f 3027 if (strchr("&`'123456789+", curop->op_private))
ce862d02 3028 break;
554b3eca
MB
3029 }
3030#else
79072805 3031 if (curop->op_type == OP_GV) {
638eceb6 3032 GV *gv = cGVOPx_gv(curop);
ce862d02 3033 repl_has_vars = 1;
93a17b20 3034 if (strchr("&`'123456789+", *GvENAME(gv)))
79072805
LW
3035 break;
3036 }
554b3eca 3037#endif /* USE_THREADS */
79072805
LW
3038 else if (curop->op_type == OP_RV2CV)
3039 break;
3040 else if (curop->op_type == OP_RV2SV ||
3041 curop->op_type == OP_RV2AV ||
3042 curop->op_type == OP_RV2HV ||
3043 curop->op_type == OP_RV2GV) {
3044 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
3045 break;
3046 }
748a9306
LW
3047 else if (curop->op_type == OP_PADSV ||
3048 curop->op_type == OP_PADAV ||
3049 curop->op_type == OP_PADHV ||
554b3eca 3050 curop->op_type == OP_PADANY) {
ce862d02 3051 repl_has_vars = 1;
748a9306 3052 }
1167e5da
SM
3053 else if (curop->op_type == OP_PUSHRE)
3054 ; /* Okay here, dangerous in newASSIGNOP */
79072805
LW
3055 else
3056 break;
3057 }
3058 lastop = curop;
3059 }
748a9306 3060 }
ce862d02 3061 if (curop == repl
1c846c1f
NIS
3062 && !(repl_has_vars
3063 && (!pm->op_pmregexp
ce862d02 3064 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
748a9306 3065 pm->op_pmflags |= PMf_CONST; /* const for long enough */
4633a7c4 3066 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
11343788 3067 prepend_elem(o->op_type, scalar(repl), o);
748a9306
LW
3068 }
3069 else {
ce862d02
IZ
3070 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
3071 pm->op_pmflags |= PMf_MAYBE_CONST;
3072 pm->op_pmpermflags |= PMf_MAYBE_CONST;
3073 }
b7dc083c 3074 NewOp(1101, rcop, 1, LOGOP);
748a9306 3075 rcop->op_type = OP_SUBSTCONT;
22c35a8c 3076 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
748a9306
LW
3077 rcop->op_first = scalar(repl);
3078 rcop->op_flags |= OPf_KIDS;
3079 rcop->op_private = 1;
11343788 3080 rcop->op_other = o;
748a9306
LW
3081
3082 /* establish postfix order */
3083 rcop->op_next = LINKLIST(repl);
3084 repl->op_next = (OP*)rcop;
3085
3086 pm->op_pmreplroot = scalar((OP*)rcop);
3087 pm->op_pmreplstart = LINKLIST(rcop);
3088 rcop->op_next = 0;
79072805
LW
3089 }
3090 }
3091
3092 return (OP*)pm;
3093}
3094
3095OP *
864dbfa3 3096Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
79072805
LW
3097{
3098 SVOP *svop;
b7dc083c 3099 NewOp(1101, svop, 1, SVOP);
79072805 3100 svop->op_type = type;
22c35a8c 3101 svop->op_ppaddr = PL_ppaddr[type];
79072805
LW
3102 svop->op_sv = sv;
3103 svop->op_next = (OP*)svop;
3104 svop->op_flags = flags;
22c35a8c 3105 if (PL_opargs[type] & OA_RETSCALAR)
463ee0b2 3106 scalar((OP*)svop);
22c35a8c 3107 if (PL_opargs[type] & OA_TARGET)
ed6116ce 3108 svop->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 3109 return CHECKOP(type, svop);
79072805
LW
3110}
3111
3112OP *
350de78d
GS
3113Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
3114{
3115 PADOP *padop;
3116 NewOp(1101, padop, 1, PADOP);
3117 padop->op_type = type;
3118 padop->op_ppaddr = PL_ppaddr[type];
3119 padop->op_padix = pad_alloc(type, SVs_PADTMP);
7766f137 3120 SvREFCNT_dec(PL_curpad[padop->op_padix]);
350de78d 3121 PL_curpad[padop->op_padix] = sv;
7766f137 3122 SvPADTMP_on(sv);
350de78d
GS
3123 padop->op_next = (OP*)padop;
3124 padop->op_flags = flags;
3125 if (PL_opargs[type] & OA_RETSCALAR)
3126 scalar((OP*)padop);
3127 if (PL_opargs[type] & OA_TARGET)
3128 padop->op_targ = pad_alloc(type, SVs_PADTMP);
3129 return CHECKOP(type, padop);
3130}
3131
3132OP *
864dbfa3 3133Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
79072805 3134{
350de78d 3135#ifdef USE_ITHREADS
743e66e6 3136 GvIN_PAD_on(gv);
350de78d
GS
3137 return newPADOP(type, flags, SvREFCNT_inc(gv));
3138#else
7934575e 3139 return newSVOP(type, flags, SvREFCNT_inc(gv));
350de78d 3140#endif
79072805
LW
3141}
3142
3143OP *
864dbfa3 3144Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
79072805
LW
3145{
3146 PVOP *pvop;
b7dc083c 3147 NewOp(1101, pvop, 1, PVOP);
79072805 3148 pvop->op_type = type;
22c35a8c 3149 pvop->op_ppaddr = PL_ppaddr[type];
79072805
LW
3150 pvop->op_pv = pv;
3151 pvop->op_next = (OP*)pvop;
3152 pvop->op_flags = flags;
22c35a8c 3153 if (PL_opargs[type] & OA_RETSCALAR)
463ee0b2 3154 scalar((OP*)pvop);
22c35a8c 3155 if (PL_opargs[type] & OA_TARGET)
ed6116ce 3156 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 3157 return CHECKOP(type, pvop);
79072805
LW
3158}
3159
79072805 3160void
864dbfa3 3161Perl_package(pTHX_ OP *o)
79072805 3162{
93a17b20 3163 SV *sv;
79072805 3164
3280af22
NIS
3165 save_hptr(&PL_curstash);
3166 save_item(PL_curstname);
11343788 3167 if (o) {
463ee0b2
LW
3168 STRLEN len;
3169 char *name;
11343788 3170 sv = cSVOPo->op_sv;
463ee0b2 3171 name = SvPV(sv, len);
3280af22
NIS
3172 PL_curstash = gv_stashpvn(name,len,TRUE);
3173 sv_setpvn(PL_curstname, name, len);
11343788 3174 op_free(o);
93a17b20
LW
3175 }
3176 else {
3280af22
NIS
3177 sv_setpv(PL_curstname,"<none>");
3178 PL_curstash = Nullhv;
93a17b20 3179 }
7ad382f4 3180 PL_hints |= HINT_BLOCK_SCOPE;
3280af22
NIS
3181 PL_copline = NOLINE;
3182 PL_expect = XSTATE;
79072805
LW
3183}
3184
85e6fe83 3185void
864dbfa3 3186Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg)
85e6fe83 3187{
a0d0e21e 3188 OP *pack;
a0d0e21e
LW
3189 OP *rqop;
3190 OP *imop;
b1cb66bf 3191 OP *veop;
78ca652e 3192 GV *gv;
85e6fe83 3193
a0d0e21e 3194 if (id->op_type != OP_CONST)
cea2e8a9 3195 Perl_croak(aTHX_ "Module name must be constant");
85e6fe83 3196
b1cb66bf 3197 veop = Nullop;
3198
0f79a09d 3199 if (version != Nullop) {
b1cb66bf 3200 SV *vesv = ((SVOP*)version)->op_sv;
3201
44dcb63b 3202 if (arg == Nullop && !SvNIOKp(vesv)) {
b1cb66bf 3203 arg = version;
3204 }
3205 else {
3206 OP *pack;
0f79a09d 3207 SV *meth;
b1cb66bf 3208
44dcb63b 3209 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
cea2e8a9 3210 Perl_croak(aTHX_ "Version number must be constant number");
b1cb66bf 3211
3212 /* Make copy of id so we don't free it twice */
3213 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3214
3215 /* Fake up a method call to VERSION */
0f79a09d
GS
3216 meth = newSVpvn("VERSION",7);
3217 sv_upgrade(meth, SVt_PVIV);
155aba94 3218 (void)SvIOK_on(meth);
0f79a09d 3219 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
b1cb66bf 3220 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3221 append_elem(OP_LIST,
0f79a09d
GS
3222 prepend_elem(OP_LIST, pack, list(version)),
3223 newSVOP(OP_METHOD_NAMED, 0, meth)));
b1cb66bf 3224 }
3225 }
aeea060c 3226
a0d0e21e 3227 /* Fake up an import/unimport */
4633a7c4
LW
3228 if (arg && arg->op_type == OP_STUB)
3229 imop = arg; /* no import on explicit () */
44dcb63b 3230 else if (SvNIOKp(((SVOP*)id)->op_sv)) {
b1cb66bf 3231 imop = Nullop; /* use 5.0; */
3232 }
4633a7c4 3233 else {
0f79a09d
GS
3234 SV *meth;
3235
4633a7c4
LW
3236 /* Make copy of id so we don't free it twice */
3237 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
0f79a09d
GS
3238
3239 /* Fake up a method call to import/unimport */
3240 meth = aver ? newSVpvn("import",6) : newSVpvn("unimport", 8);;
3241 sv_upgrade(meth, SVt_PVIV);
155aba94 3242 (void)SvIOK_on(meth);
0f79a09d 3243 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
4633a7c4 3244 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
0f79a09d
GS
3245 append_elem(OP_LIST,
3246 prepend_elem(OP_LIST, pack, list(arg)),
3247 newSVOP(OP_METHOD_NAMED, 0, meth)));
4633a7c4
LW
3248 }
3249
78ca652e
GS
3250 /* Fake up a require, handle override, if any */
3251 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
3252 if (!(gv && GvIMPORTED_CV(gv)))
3253 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
3254
3255 if (gv && GvIMPORTED_CV(gv)) {
3256 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3257 append_elem(OP_LIST, id,
3258 scalar(newUNOP(OP_RV2CV, 0,
3259 newGVOP(OP_GV, 0,
3260 gv))))));
3261 }
3262 else {
3263 rqop = newUNOP(OP_REQUIRE, 0, id);
3264 }
a0d0e21e
LW
3265
3266 /* Fake up the BEGIN {}, which does its thing immediately. */
09bef843 3267 newATTRSUB(floor,
79cb57f6 3268 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
4633a7c4 3269 Nullop,
09bef843 3270 Nullop,
a0d0e21e 3271 append_elem(OP_LINESEQ,
b1cb66bf 3272 append_elem(OP_LINESEQ,
3273 newSTATEOP(0, Nullch, rqop),
3274 newSTATEOP(0, Nullch, veop)),
a0d0e21e 3275 newSTATEOP(0, Nullch, imop) ));
85e6fe83 3276
c305c6a0 3277 PL_hints |= HINT_BLOCK_SCOPE;
3280af22
NIS
3278 PL_copline = NOLINE;
3279 PL_expect = XSTATE;
85e6fe83
LW
3280}
3281
e4783991
GS
3282void
3283Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
3284{
3285 va_list args;
3286 va_start(args, ver);
3287 vload_module(flags, name, ver, &args);
3288 va_end(args);
3289}
3290
3291#ifdef PERL_IMPLICIT_CONTEXT
3292void
3293Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
3294{
3295 dTHX;
3296 va_list args;
3297 va_start(args, ver);
3298 vload_module(flags, name, ver, &args);
3299 va_end(args);
3300}
3301#endif
3302
3303void
3304Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
3305{
3306 OP *modname, *veop, *imop;
3307
3308 modname = newSVOP(OP_CONST, 0, name);
3309 modname->op_private |= OPpCONST_BARE;
3310 if (ver) {
3311 veop = newSVOP(OP_CONST, 0, ver);
3312 }
3313 else
3314 veop = Nullop;
3315 if (flags & PERL_LOADMOD_NOIMPORT) {
3316 imop = sawparens(newNULLLIST());
3317 }
3318 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
3319 imop = va_arg(*args, OP*);
3320 }
3321 else {
3322 SV *sv;
3323 imop = Nullop;
3324 sv = va_arg(*args, SV*);
3325 while (sv) {
3326 imop = append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
3327 sv = va_arg(*args, SV*);
3328 }
3329 }
81885997
GS
3330 {
3331 line_t ocopline = PL_copline;
3332 int oexpect = PL_expect;
3333
3334 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
3335 veop, modname, imop);
3336 PL_expect = oexpect;
3337 PL_copline = ocopline;
3338 }
e4783991
GS
3339}
3340
79072805 3341OP *
864dbfa3 3342Perl_dofile(pTHX_ OP *term)
78ca652e
GS
3343{
3344 OP *doop;
3345 GV *gv;
3346
3347 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
3348 if (!(gv && GvIMPORTED_CV(gv)))
3349 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
3350
3351 if (gv && GvIMPORTED_CV(gv)) {
3352 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3353 append_elem(OP_LIST, term,
3354 scalar(newUNOP(OP_RV2CV, 0,
3355 newGVOP(OP_GV, 0,
3356 gv))))));
3357 }
3358 else {
3359 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3360 }
3361 return doop;
3362}
3363
3364OP *
864dbfa3 3365Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
79072805
LW
3366{
3367 return newBINOP(OP_LSLICE, flags,
8990e307
LW
3368 list(force_list(subscript)),
3369 list(force_list(listval)) );
79072805
LW
3370}
3371
76e3520e 3372STATIC I32
cea2e8a9 3373S_list_assignment(pTHX_ register OP *o)
79072805 3374{
11343788 3375 if (!o)
79072805
LW
3376 return TRUE;
3377
11343788
MB
3378 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3379 o = cUNOPo->op_first;
79072805 3380
11343788 3381 if (o->op_type == OP_COND_EXPR) {
1a67a97c
SM
3382 I32 t = list_assignment(cLOGOPo->op_first->op_sibling);
3383 I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
79072805
LW
3384
3385 if (t && f)
3386 return TRUE;
3387 if (t || f)
3388 yyerror("Assignment to both a list and a scalar");
3389 return FALSE;
3390 }
3391
11343788
MB
3392 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3393 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3394 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
79072805
LW
3395 return TRUE;
3396
11343788 3397 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
93a17b20
LW
3398 return TRUE;
3399
11343788 3400 if (o->op_type == OP_RV2SV)
79072805
LW
3401 return FALSE;
3402
3403 return FALSE;
3404}
3405
3406OP *
864dbfa3 3407Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
79072805 3408{
11343788 3409 OP *o;
79072805 3410
a0d0e21e
LW
3411 if (optype) {
3412 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
3413 return newLOGOP(optype, 0,
3414 mod(scalar(left), optype),
3415 newUNOP(OP_SASSIGN, 0, scalar(right)));
3416 }
3417 else {
3418 return newBINOP(optype, OPf_STACKED,
3419 mod(scalar(left), optype), scalar(right));
3420 }
3421 }
3422
79072805 3423 if (list_assignment(left)) {
10c8fecd
GS
3424 OP *curop;
3425
3280af22
NIS
3426 PL_modcount = 0;
3427 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
463ee0b2 3428 left = mod(left, OP_AASSIGN);
3280af22
NIS
3429 if (PL_eval_start)
3430 PL_eval_start = 0;
748a9306 3431 else {
a0d0e21e
LW
3432 op_free(left);
3433 op_free(right);
3434 return Nullop;
3435 }
10c8fecd
GS
3436 curop = list(force_list(left));
3437 o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
11343788 3438 o->op_private = 0 | (flags >> 8);
10c8fecd
GS
3439 for (curop = ((LISTOP*)curop)->op_first;
3440 curop; curop = curop->op_sibling)
3441 {
3442 if (curop->op_type == OP_RV2HV &&
3443 ((UNOP*)curop)->op_first->op_type != OP_GV) {
3444 o->op_private |= OPpASSIGN_HASH;
3445 break;
3446 }
3447 }
a0d0e21e 3448 if (!(left->op_private & OPpLVAL_INTRO)) {
11343788 3449 OP *lastop = o;
3280af22 3450 PL_generation++;
11343788 3451 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
22c35a8c 3452 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
79072805 3453 if (curop->op_type == OP_GV) {
638eceb6 3454 GV *gv = cGVOPx_gv(curop);
3280af22 3455 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
79072805 3456 break;
3280af22 3457 SvCUR(gv) = PL_generation;
79072805 3458 }
748a9306
LW
3459 else if (curop->op_type == OP_PADSV ||
3460 curop->op_type == OP_PADAV ||
3461 curop->op_type == OP_PADHV ||
3462 curop->op_type == OP_PADANY) {
3280af22 3463 SV **svp = AvARRAY(PL_comppad_name);
8e07c86e 3464 SV *sv = svp[curop->op_targ];
3280af22 3465 if (SvCUR(sv) == PL_generation)
748a9306 3466 break;
3280af22 3467 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
748a9306 3468 }
79072805
LW
3469 else if (curop->op_type == OP_RV2CV)
3470 break;
3471 else if (curop->op_type == OP_RV2SV ||
3472 curop->op_type == OP_RV2AV ||
3473 curop->op_type == OP_RV2HV ||
3474 curop->op_type == OP_RV2GV) {
3475 if (lastop->op_type != OP_GV) /* funny deref? */
3476 break;
3477 }
1167e5da
SM
3478 else if (curop->op_type == OP_PUSHRE) {
3479 if (((PMOP*)curop)->op_pmreplroot) {
b3f5893f
GS
3480#ifdef USE_ITHREADS
3481 GV *gv = (GV*)PL_curpad[(PADOFFSET)((PMOP*)curop)->op_pmreplroot];
3482#else
1167e5da 3483 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
b3f5893f 3484#endif
3280af22 3485 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
1167e5da 3486 break;
3280af22 3487 SvCUR(gv) = PL_generation;
1167e5da
SM
3488 }
3489 }
79072805
LW
3490 else
3491 break;
3492 }
3493 lastop = curop;
3494 }
11343788 3495 if (curop != o)
10c8fecd 3496 o->op_private |= OPpASSIGN_COMMON;
79072805 3497 }
c07a80fd 3498 if (right && right->op_type == OP_SPLIT) {
3499 OP* tmpop;
3500 if ((tmpop = ((LISTOP*)right)->op_first) &&
3501 tmpop->op_type == OP_PUSHRE)
3502 {
3503 PMOP *pm = (PMOP*)tmpop;
3504 if (left->op_type == OP_RV2AV &&
3505 !(left->op_private & OPpLVAL_INTRO) &&
11343788 3506 !(o->op_private & OPpASSIGN_COMMON) )
c07a80fd 3507 {
3508 tmpop = ((UNOP*)left)->op_first;
3509 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
971a9dd3
GS
3510#ifdef USE_ITHREADS
3511 pm->op_pmreplroot = (OP*)cPADOPx(tmpop)->op_padix;
3512 cPADOPx(tmpop)->op_padix = 0; /* steal it */
3513#else
3514 pm->op_pmreplroot = (OP*)cSVOPx(tmpop)->op_sv;
3515 cSVOPx(tmpop)->op_sv = Nullsv; /* steal it */
3516#endif
c07a80fd 3517 pm->op_pmflags |= PMf_ONCE;
11343788 3518 tmpop = cUNOPo->op_first; /* to list (nulled) */
c07a80fd 3519 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3520 tmpop->op_sibling = Nullop; /* don't free split */
3521 right->op_next = tmpop->op_next; /* fix starting loc */
11343788 3522 op_free(o); /* blow off assign */
54310121 3523 right->op_flags &= ~OPf_WANT;
a5f75d66 3524 /* "I don't know and I don't care." */
c07a80fd 3525 return right;
3526 }
3527 }
3528 else {
e6438c1a 3529 if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
c07a80fd 3530 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3531 {
3532 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3533 if (SvIVX(sv) == 0)
3280af22 3534 sv_setiv(sv, PL_modcount+1);
c07a80fd 3535 }
3536 }
3537 }
3538 }
11343788 3539 return o;
79072805
LW
3540 }
3541 if (!right)
3542 right = newOP(OP_UNDEF, 0);
3543 if (right->op_type == OP_READLINE) {
3544 right->op_flags |= OPf_STACKED;
463ee0b2 3545 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
79072805 3546 }
a0d0e21e 3547 else {
3280af22 3548 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
11343788 3549 o = newBINOP(OP_SASSIGN, flags,
463ee0b2 3550 scalar(right), mod(scalar(left), OP_SASSIGN) );
3280af22
NIS
3551 if (PL_eval_start)
3552 PL_eval_start = 0;
748a9306 3553 else {
11343788 3554 op_free(o);
a0d0e21e
LW
3555 return Nullop;
3556 }
3557 }
11343788