This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
add padrange op
[perl5.git] / pp_hot.c
CommitLineData
a0d0e21e
LW
1/* pp_hot.c
2 *
1129b882
NC
3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
a0d0e21e
LW
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
9 */
10
11/*
12 * Then he heard Merry change the note, and up went the Horn-cry of Buckland,
13 * shaking the air.
14 *
4ac71550
TC
15 * Awake! Awake! Fear, Fire, Foes! Awake!
16 * Fire, Foes! Awake!
17 *
18 * [p.1007 of _The Lord of the Rings_, VI/viii: "The Scouring of the Shire"]
a0d0e21e
LW
19 */
20
166f8a29
DM
21/* This file contains 'hot' pp ("push/pop") functions that
22 * execute the opcodes that make up a perl program. A typical pp function
23 * expects to find its arguments on the stack, and usually pushes its
24 * results onto the stack, hence the 'pp' terminology. Each OP structure
25 * contains a pointer to the relevant pp_foo() function.
26 *
27 * By 'hot', we mean common ops whose execution speed is critical.
28 * By gathering them together into a single file, we encourage
29 * CPU cache hits on hot code. Also it could be taken as a warning not to
30 * change any code in this file unless you're sure it won't affect
31 * performance.
32 */
33
a0d0e21e 34#include "EXTERN.h"
864dbfa3 35#define PERL_IN_PP_HOT_C
a0d0e21e
LW
36#include "perl.h"
37
38/* Hot code. */
39
40PP(pp_const)
41{
97aff369 42 dVAR;
39644a26 43 dSP;
996c9baa 44 XPUSHs(cSVOP_sv);
a0d0e21e
LW
45 RETURN;
46}
47
48PP(pp_nextstate)
49{
97aff369 50 dVAR;
533c011a 51 PL_curcop = (COP*)PL_op;
a0d0e21e 52 TAINT_NOT; /* Each statement is presumed innocent */
3280af22 53 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e 54 FREETMPS;
f410a211 55 PERL_ASYNC_CHECK();
a0d0e21e
LW
56 return NORMAL;
57}
58
59PP(pp_gvsv)
60{
97aff369 61 dVAR;
39644a26 62 dSP;
924508f0 63 EXTEND(SP,1);
533c011a 64 if (PL_op->op_private & OPpLVAL_INTRO)
1d7c1841 65 PUSHs(save_scalar(cGVOP_gv));
a0d0e21e 66 else
c69033f2 67 PUSHs(GvSVn(cGVOP_gv));
a0d0e21e
LW
68 RETURN;
69}
70
71PP(pp_null)
72{
97aff369 73 dVAR;
a0d0e21e
LW
74 return NORMAL;
75}
76
5d8673bc 77/* This is sometimes called directly by pp_coreargs and pp_grepstart. */
a0d0e21e
LW
78PP(pp_pushmark)
79{
97aff369 80 dVAR;
3280af22 81 PUSHMARK(PL_stack_sp);
a0d0e21e
LW
82 return NORMAL;
83}
84
85PP(pp_stringify)
86{
97aff369 87 dVAR; dSP; dTARGET;
6050d10e 88 sv_copypv(TARG,TOPs);
a0d0e21e
LW
89 SETTARG;
90 RETURN;
91}
92
93PP(pp_gv)
94{
97aff369 95 dVAR; dSP;
ad64d0ec 96 XPUSHs(MUTABLE_SV(cGVOP_gv));
a0d0e21e
LW
97 RETURN;
98}
99
100PP(pp_and)
101{
97aff369 102 dVAR; dSP;
f410a211 103 PERL_ASYNC_CHECK();
a0d0e21e
LW
104 if (!SvTRUE(TOPs))
105 RETURN;
106 else {
c960fc3b
SP
107 if (PL_op->op_type == OP_AND)
108 --SP;
a0d0e21e
LW
109 RETURNOP(cLOGOP->op_other);
110 }
111}
112
113PP(pp_sassign)
114{
3e75a3c4
RU
115 dVAR; dSP;
116 /* sassign keeps its args in the optree traditionally backwards.
117 So we pop them differently.
118 */
119 SV *left = POPs; SV *right = TOPs;
748a9306 120
533c011a 121 if (PL_op->op_private & OPpASSIGN_BACKWARDS) {
0bd48802
AL
122 SV * const temp = left;
123 left = right; right = temp;
a0d0e21e 124 }
284167a5 125 if (TAINTING_get && TAINT_get && !SvTAINTED(right))
a0d0e21e 126 TAINT_NOT;
e26df76a 127 if (PL_op->op_private & OPpASSIGN_CV_TO_GV) {
3e75a3c4 128 SV * const cv = SvRV(right);
e26df76a 129 const U32 cv_type = SvTYPE(cv);
3e75a3c4 130 const bool is_gv = isGV_with_GP(left);
6136c704 131 const bool got_coderef = cv_type == SVt_PVCV || cv_type == SVt_PVFM;
e26df76a
NC
132
133 if (!got_coderef) {
134 assert(SvROK(cv));
135 }
136
3e75a3c4
RU
137 /* Can do the optimisation if left (LVALUE) is not a typeglob,
138 right (RVALUE) is a reference to something, and we're in void
e26df76a 139 context. */
13be902c 140 if (!got_coderef && !is_gv && GIMME_V == G_VOID) {
e26df76a 141 /* Is the target symbol table currently empty? */
3e75a3c4 142 GV * const gv = gv_fetchsv_nomg(left, GV_NOINIT, SVt_PVGV);
bb112e5a 143 if (SvTYPE(gv) != SVt_PVGV && !SvOK(gv)) {
e26df76a
NC
144 /* Good. Create a new proxy constant subroutine in the target.
145 The gv becomes a(nother) reference to the constant. */
146 SV *const value = SvRV(cv);
147
ad64d0ec 148 SvUPGRADE(MUTABLE_SV(gv), SVt_IV);
1ccdb730 149 SvPCS_IMPORTED_on(gv);
e26df76a 150 SvRV_set(gv, value);
b37c2d43 151 SvREFCNT_inc_simple_void(value);
3e75a3c4 152 SETs(left);
e26df76a
NC
153 RETURN;
154 }
155 }
156
157 /* Need to fix things up. */
13be902c 158 if (!is_gv) {
e26df76a 159 /* Need to fix GV. */
3e75a3c4 160 left = MUTABLE_SV(gv_fetchsv_nomg(left,GV_ADD, SVt_PVGV));
e26df76a
NC
161 }
162
163 if (!got_coderef) {
164 /* We've been returned a constant rather than a full subroutine,
165 but they expect a subroutine reference to apply. */
53a42478 166 if (SvROK(cv)) {
d343c3ef 167 ENTER_with_name("sassign_coderef");
53a42478
NC
168 SvREFCNT_inc_void(SvRV(cv));
169 /* newCONSTSUB takes a reference count on the passed in SV
170 from us. We set the name to NULL, otherwise we get into
171 all sorts of fun as the reference to our new sub is
172 donated to the GV that we're about to assign to.
173 */
3e75a3c4 174 SvRV_set(right, MUTABLE_SV(newCONSTSUB(GvSTASH(left), NULL,
ad64d0ec 175 SvRV(cv))));
53a42478 176 SvREFCNT_dec(cv);
d343c3ef 177 LEAVE_with_name("sassign_coderef");
53a42478
NC
178 } else {
179 /* What can happen for the corner case *{"BONK"} = \&{"BONK"};
180 is that
181 First: ops for \&{"BONK"}; return us the constant in the
182 symbol table
183 Second: ops for *{"BONK"} cause that symbol table entry
184 (and our reference to it) to be upgraded from RV
185 to typeblob)
186 Thirdly: We get here. cv is actually PVGV now, and its
187 GvCV() is actually the subroutine we're looking for
188
189 So change the reference so that it points to the subroutine
190 of that typeglob, as that's what they were after all along.
191 */
159b6efe 192 GV *const upgraded = MUTABLE_GV(cv);
53a42478
NC
193 CV *const source = GvCV(upgraded);
194
195 assert(source);
196 assert(CvFLAGS(source) & CVf_CONST);
197
198 SvREFCNT_inc_void(source);
199 SvREFCNT_dec(upgraded);
3e75a3c4 200 SvRV_set(right, MUTABLE_SV(source));
53a42478 201 }
e26df76a 202 }
53a42478 203
e26df76a 204 }
8fe85e3f 205 if (
3e75a3c4
RU
206 SvTEMP(left) && !SvSMAGICAL(left) && SvREFCNT(left) == 1 &&
207 (!isGV_with_GP(left) || SvFAKE(left)) && ckWARN(WARN_MISC)
8fe85e3f
FC
208 )
209 Perl_warner(aTHX_
210 packWARN(WARN_MISC), "Useless assignment to a temporary"
211 );
3e75a3c4
RU
212 SvSetMagicSV(left, right);
213 SETs(left);
a0d0e21e
LW
214 RETURN;
215}
216
217PP(pp_cond_expr)
218{
97aff369 219 dVAR; dSP;
f410a211 220 PERL_ASYNC_CHECK();
a0d0e21e 221 if (SvTRUEx(POPs))
1a67a97c 222 RETURNOP(cLOGOP->op_other);
a0d0e21e 223 else
1a67a97c 224 RETURNOP(cLOGOP->op_next);
a0d0e21e
LW
225}
226
227PP(pp_unstack)
228{
97aff369 229 dVAR;
8f3964af 230 PERL_ASYNC_CHECK();
a0d0e21e 231 TAINT_NOT; /* Each statement is presumed innocent */
3280af22 232 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e 233 FREETMPS;
eae48c89
Z
234 if (!(PL_op->op_flags & OPf_SPECIAL)) {
235 I32 oldsave = PL_scopestack[PL_scopestack_ix - 1];
236 LEAVE_SCOPE(oldsave);
237 }
a0d0e21e
LW
238 return NORMAL;
239}
240
a0d0e21e
LW
241PP(pp_concat)
242{
6f1401dc 243 dVAR; dSP; dATARGET; tryAMAGICbin_MG(concat_amg, AMGf_assign);
748a9306
LW
244 {
245 dPOPTOPssrl;
8d6d96c1
HS
246 bool lbyte;
247 STRLEN rlen;
d4c19fe8 248 const char *rpv = NULL;
a6b599c7 249 bool rbyte = FALSE;
a9c4fd4e 250 bool rcopied = FALSE;
8d6d96c1 251
6f1401dc
DM
252 if (TARG == right && right != left) { /* $r = $l.$r */
253 rpv = SvPV_nomg_const(right, rlen);
c75ab21a 254 rbyte = !DO_UTF8(right);
59cd0e26 255 right = newSVpvn_flags(rpv, rlen, SVs_TEMP);
349d4f2f 256 rpv = SvPV_const(right, rlen); /* no point setting UTF-8 here */
db79b45b 257 rcopied = TRUE;
8d6d96c1 258 }
7889fe52 259
89734059 260 if (TARG != left) { /* not $l .= $r */
a9c4fd4e 261 STRLEN llen;
6f1401dc 262 const char* const lpv = SvPV_nomg_const(left, llen);
90f5826e 263 lbyte = !DO_UTF8(left);
8d6d96c1
HS
264 sv_setpvn(TARG, lpv, llen);
265 if (!lbyte)
266 SvUTF8_on(TARG);
267 else
268 SvUTF8_off(TARG);
269 }
89734059 270 else { /* $l .= $r */
c75ab21a 271 if (!SvOK(TARG)) {
89734059 272 if (left == right && ckWARN(WARN_UNINITIALIZED)) /* $l .= $l */
c75ab21a 273 report_uninit(right);
76f68e9b 274 sv_setpvs(left, "");
c75ab21a 275 }
583a5589
FC
276 SvPV_force_nomg_nolen(left);
277 lbyte = !DO_UTF8(left);
90f5826e
TS
278 if (IN_BYTES)
279 SvUTF8_off(TARG);
8d6d96c1 280 }
a12c0f56 281
c75ab21a 282 if (!rcopied) {
6f1401dc 283 if (left == right)
89734059 284 /* $r.$r: do magic twice: tied might return different 2nd time */
6f1401dc
DM
285 SvGETMAGIC(right);
286 rpv = SvPV_nomg_const(right, rlen);
c75ab21a
RH
287 rbyte = !DO_UTF8(right);
288 }
8d6d96c1 289 if (lbyte != rbyte) {
e3393f51
NT
290 /* sv_utf8_upgrade_nomg() may reallocate the stack */
291 PUTBACK;
8d6d96c1
HS
292 if (lbyte)
293 sv_utf8_upgrade_nomg(TARG);
294 else {
db79b45b 295 if (!rcopied)
59cd0e26 296 right = newSVpvn_flags(rpv, rlen, SVs_TEMP);
8d6d96c1 297 sv_utf8_upgrade_nomg(right);
6f1401dc 298 rpv = SvPV_nomg_const(right, rlen);
69b47968 299 }
e3393f51 300 SPAGAIN;
a0d0e21e 301 }
8d6d96c1 302 sv_catpvn_nomg(TARG, rpv, rlen);
43ebc500 303
a0d0e21e
LW
304 SETTARG;
305 RETURN;
748a9306 306 }
a0d0e21e
LW
307}
308
a7fd8ef6
DM
309/* ($lex1,@lex2,...) or my ($lex1,@lex2,...) */
310
311PP(pp_padrange)
312{
313 dVAR; dSP;
314 PADOFFSET base = PL_op->op_targ;
315 int count = (int)(PL_op->op_private) & OPpPADRANGE_COUNTMASK;
316 int i;
317 /* note, this is only skipped for compile-time-known void cxt */
318 if ((PL_op->op_flags & OPf_WANT) != OPf_WANT_VOID) {
319 EXTEND(SP, count);
320 PUSHMARK(SP);
321 for (i = 0; i <count; i++)
322 *++SP = PAD_SV(base+i);
323 }
324 if (PL_op->op_private & OPpLVAL_INTRO) {
325 for (i = 0; i <count; i++)
326 SAVECLEARSV(PAD_SVl(base+i));
327 }
328 RETURN;
329}
330
331
a0d0e21e
LW
332PP(pp_padsv)
333{
97aff369 334 dVAR; dSP; dTARGET;
a0d0e21e 335 XPUSHs(TARG);
533c011a
NIS
336 if (PL_op->op_flags & OPf_MOD) {
337 if (PL_op->op_private & OPpLVAL_INTRO)
952306ac
RGS
338 if (!(PL_op->op_private & OPpPAD_STATE))
339 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
a62b51b8 340 if (PL_op->op_private & OPpDEREF) {
8ec5e241 341 PUTBACK;
9026059d 342 TOPs = vivify_ref(TOPs, PL_op->op_private & OPpDEREF);
8ec5e241
NIS
343 SPAGAIN;
344 }
4633a7c4 345 }
a0d0e21e
LW
346 RETURN;
347}
348
349PP(pp_readline)
350{
97aff369 351 dVAR;
30901a8a
FC
352 dSP;
353 if (TOPs) {
354 SvGETMAGIC(TOPs);
67288365 355 tryAMAGICunTARGETlist(iter_amg, 0, 0);
30901a8a
FC
356 PL_last_in_gv = MUTABLE_GV(*PL_stack_sp--);
357 }
358 else PL_last_in_gv = PL_argvgv, PL_stack_sp--;
6e592b3a
BM
359 if (!isGV_with_GP(PL_last_in_gv)) {
360 if (SvROK(PL_last_in_gv) && isGV_with_GP(SvRV(PL_last_in_gv)))
159b6efe 361 PL_last_in_gv = MUTABLE_GV(SvRV(PL_last_in_gv));
8efb3254 362 else {
f5284f61 363 dSP;
ad64d0ec 364 XPUSHs(MUTABLE_SV(PL_last_in_gv));
f5284f61 365 PUTBACK;
897d3989 366 Perl_pp_rv2gv(aTHX);
159b6efe 367 PL_last_in_gv = MUTABLE_GV(*PL_stack_sp--);
f5284f61
IZ
368 }
369 }
a0d0e21e
LW
370 return do_readline();
371}
372
373PP(pp_eq)
374{
6f1401dc 375 dVAR; dSP;
33efebe6
DM
376 SV *left, *right;
377
a42d0242 378 tryAMAGICbin_MG(eq_amg, AMGf_set|AMGf_numeric);
33efebe6
DM
379 right = POPs;
380 left = TOPs;
381 SETs(boolSV(
382 (SvIOK_notUV(left) && SvIOK_notUV(right))
383 ? (SvIVX(left) == SvIVX(right))
384 : ( do_ncmp(left, right) == 0)
385 ));
386 RETURN;
a0d0e21e
LW
387}
388
389PP(pp_preinc)
390{
97aff369 391 dVAR; dSP;
17058fe0
FC
392 const bool inc =
393 PL_op->op_type == OP_PREINC || PL_op->op_type == OP_I_PREINC;
60092ce4 394 if (SvTYPE(TOPs) >= SVt_PVAV || (isGV_with_GP(TOPs) && !SvFAKE(TOPs)))
6ad8f254 395 Perl_croak_no_modify(aTHX);
4bac9ae4 396 if (!SvREADONLY(TOPs) && !SvGMAGICAL(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
17058fe0 397 && SvIVX(TOPs) != (inc ? IV_MAX : IV_MIN))
55497cff 398 {
17058fe0 399 SvIV_set(TOPs, SvIVX(TOPs) + (inc ? 1 : -1));
55497cff 400 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306 401 }
28e5dec8 402 else /* Do all the PERL_PRESERVE_IVUV conditionals in sv_inc */
17058fe0
FC
403 if (inc) sv_inc(TOPs);
404 else sv_dec(TOPs);
a0d0e21e
LW
405 SvSETMAGIC(TOPs);
406 return NORMAL;
407}
408
409PP(pp_or)
410{
97aff369 411 dVAR; dSP;
f410a211 412 PERL_ASYNC_CHECK();
a0d0e21e
LW
413 if (SvTRUE(TOPs))
414 RETURN;
415 else {
c960fc3b
SP
416 if (PL_op->op_type == OP_OR)
417 --SP;
a0d0e21e
LW
418 RETURNOP(cLOGOP->op_other);
419 }
420}
421
25a55bd7 422PP(pp_defined)
c963b151 423{
97aff369 424 dVAR; dSP;
eb578fdb 425 SV* sv;
6136c704 426 bool defined;
25a55bd7 427 const int op_type = PL_op->op_type;
ea5195b7 428 const bool is_dor = (op_type == OP_DOR || op_type == OP_DORASSIGN);
c963b151 429
6136c704 430 if (is_dor) {
f410a211 431 PERL_ASYNC_CHECK();
25a55bd7
SP
432 sv = TOPs;
433 if (!sv || !SvANY(sv)) {
2bd49cfc
NC
434 if (op_type == OP_DOR)
435 --SP;
25a55bd7
SP
436 RETURNOP(cLOGOP->op_other);
437 }
b7c44293
RGS
438 }
439 else {
440 /* OP_DEFINED */
25a55bd7
SP
441 sv = POPs;
442 if (!sv || !SvANY(sv))
443 RETPUSHNO;
b7c44293 444 }
25a55bd7 445
6136c704 446 defined = FALSE;
c963b151
BD
447 switch (SvTYPE(sv)) {
448 case SVt_PVAV:
449 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
25a55bd7 450 defined = TRUE;
c963b151
BD
451 break;
452 case SVt_PVHV:
453 if (HvARRAY(sv) || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
25a55bd7 454 defined = TRUE;
c963b151
BD
455 break;
456 case SVt_PVCV:
457 if (CvROOT(sv) || CvXSUB(sv))
25a55bd7 458 defined = TRUE;
c963b151
BD
459 break;
460 default:
5b295bef 461 SvGETMAGIC(sv);
c963b151 462 if (SvOK(sv))
25a55bd7 463 defined = TRUE;
6136c704 464 break;
c963b151 465 }
6136c704
AL
466
467 if (is_dor) {
c960fc3b
SP
468 if(defined)
469 RETURN;
470 if(op_type == OP_DOR)
471 --SP;
25a55bd7 472 RETURNOP(cLOGOP->op_other);
25a55bd7 473 }
d9aa96a4
SP
474 /* assuming OP_DEFINED */
475 if(defined)
476 RETPUSHYES;
477 RETPUSHNO;
c963b151
BD
478}
479
a0d0e21e
LW
480PP(pp_add)
481{
800401ee 482 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
6f1401dc
DM
483 tryAMAGICbin_MG(add_amg, AMGf_assign|AMGf_numeric);
484 svr = TOPs;
485 svl = TOPm1s;
486
800401ee 487 useleft = USE_LEFT(svl);
28e5dec8
JH
488#ifdef PERL_PRESERVE_IVUV
489 /* We must see if we can perform the addition with integers if possible,
490 as the integer code detects overflow while the NV code doesn't.
491 If either argument hasn't had a numeric conversion yet attempt to get
492 the IV. It's important to do this now, rather than just assuming that
493 it's not IOK as a PV of "9223372036854775806" may not take well to NV
494 addition, and an SV which is NOK, NV=6.0 ought to be coerced to
495 integer in case the second argument is IV=9223372036854775806
496 We can (now) rely on sv_2iv to do the right thing, only setting the
497 public IOK flag if the value in the NV (or PV) slot is truly integer.
498
499 A side effect is that this also aggressively prefers integer maths over
7dca457a
NC
500 fp maths for integer values.
501
a00b5bd3 502 How to detect overflow?
7dca457a
NC
503
504 C 99 section 6.2.6.1 says
505
506 The range of nonnegative values of a signed integer type is a subrange
507 of the corresponding unsigned integer type, and the representation of
508 the same value in each type is the same. A computation involving
509 unsigned operands can never overflow, because a result that cannot be
510 represented by the resulting unsigned integer type is reduced modulo
511 the number that is one greater than the largest value that can be
512 represented by the resulting type.
513
514 (the 9th paragraph)
515
516 which I read as "unsigned ints wrap."
517
518 signed integer overflow seems to be classed as "exception condition"
519
520 If an exceptional condition occurs during the evaluation of an
521 expression (that is, if the result is not mathematically defined or not
522 in the range of representable values for its type), the behavior is
523 undefined.
524
525 (6.5, the 5th paragraph)
526
527 I had assumed that on 2s complement machines signed arithmetic would
528 wrap, hence coded pp_add and pp_subtract on the assumption that
529 everything perl builds on would be happy. After much wailing and
530 gnashing of teeth it would seem that irix64 knows its ANSI spec well,
531 knows that it doesn't need to, and doesn't. Bah. Anyway, the all-
532 unsigned code below is actually shorter than the old code. :-)
533 */
534
01f91bf2 535 if (SvIV_please_nomg(svr)) {
28e5dec8
JH
536 /* Unless the left argument is integer in range we are going to have to
537 use NV maths. Hence only attempt to coerce the right argument if
538 we know the left is integer. */
eb578fdb 539 UV auv = 0;
9c5ffd7c 540 bool auvok = FALSE;
7dca457a
NC
541 bool a_valid = 0;
542
28e5dec8 543 if (!useleft) {
7dca457a
NC
544 auv = 0;
545 a_valid = auvok = 1;
546 /* left operand is undef, treat as zero. + 0 is identity,
547 Could SETi or SETu right now, but space optimise by not adding
548 lots of code to speed up what is probably a rarish case. */
549 } else {
550 /* Left operand is defined, so is it IV? */
01f91bf2 551 if (SvIV_please_nomg(svl)) {
800401ee
JH
552 if ((auvok = SvUOK(svl)))
553 auv = SvUVX(svl);
7dca457a 554 else {
eb578fdb 555 const IV aiv = SvIVX(svl);
7dca457a
NC
556 if (aiv >= 0) {
557 auv = aiv;
558 auvok = 1; /* Now acting as a sign flag. */
559 } else { /* 2s complement assumption for IV_MIN */
560 auv = (UV)-aiv;
561 }
562 }
563 a_valid = 1;
28e5dec8
JH
564 }
565 }
7dca457a
NC
566 if (a_valid) {
567 bool result_good = 0;
568 UV result;
eb578fdb 569 UV buv;
800401ee 570 bool buvok = SvUOK(svr);
a00b5bd3 571
7dca457a 572 if (buvok)
800401ee 573 buv = SvUVX(svr);
7dca457a 574 else {
eb578fdb 575 const IV biv = SvIVX(svr);
7dca457a
NC
576 if (biv >= 0) {
577 buv = biv;
578 buvok = 1;
579 } else
580 buv = (UV)-biv;
581 }
582 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
602f51c4 583 else "IV" now, independent of how it came in.
7dca457a
NC
584 if a, b represents positive, A, B negative, a maps to -A etc
585 a + b => (a + b)
586 A + b => -(a - b)
587 a + B => (a - b)
588 A + B => -(a + b)
589 all UV maths. negate result if A negative.
590 add if signs same, subtract if signs differ. */
591
592 if (auvok ^ buvok) {
593 /* Signs differ. */
594 if (auv >= buv) {
595 result = auv - buv;
596 /* Must get smaller */
597 if (result <= auv)
598 result_good = 1;
599 } else {
600 result = buv - auv;
601 if (result <= buv) {
602 /* result really should be -(auv-buv). as its negation
603 of true value, need to swap our result flag */
604 auvok = !auvok;
605 result_good = 1;
28e5dec8
JH
606 }
607 }
7dca457a
NC
608 } else {
609 /* Signs same */
610 result = auv + buv;
611 if (result >= auv)
612 result_good = 1;
613 }
614 if (result_good) {
615 SP--;
616 if (auvok)
28e5dec8 617 SETu( result );
7dca457a
NC
618 else {
619 /* Negate result */
620 if (result <= (UV)IV_MIN)
621 SETi( -(IV)result );
622 else {
623 /* result valid, but out of range for IV. */
624 SETn( -(NV)result );
28e5dec8
JH
625 }
626 }
7dca457a
NC
627 RETURN;
628 } /* Overflow, drop through to NVs. */
28e5dec8
JH
629 }
630 }
631#endif
a0d0e21e 632 {
6f1401dc 633 NV value = SvNV_nomg(svr);
4efa5a16 634 (void)POPs;
28e5dec8
JH
635 if (!useleft) {
636 /* left operand is undef, treat as zero. + 0.0 is identity. */
637 SETn(value);
638 RETURN;
639 }
6f1401dc 640 SETn( value + SvNV_nomg(svl) );
28e5dec8 641 RETURN;
a0d0e21e
LW
642 }
643}
644
645PP(pp_aelemfast)
646{
97aff369 647 dVAR; dSP;
93bad3fd 648 AV * const av = PL_op->op_type == OP_AELEMFAST_LEX
8f878375 649 ? MUTABLE_AV(PAD_SV(PL_op->op_targ)) : GvAVn(cGVOP_gv);
a3b680e6 650 const U32 lval = PL_op->op_flags & OPf_MOD;
0bd48802 651 SV** const svp = av_fetch(av, PL_op->op_private, lval);
3280af22 652 SV *sv = (svp ? *svp : &PL_sv_undef);
6ff81951 653 EXTEND(SP, 1);
39cf747a 654 if (!lval && SvRMAGICAL(av) && SvGMAGICAL(sv)) /* see note in pp_helem() */
fd69380d 655 mg_get(sv);
be6c24e0 656 PUSHs(sv);
a0d0e21e
LW
657 RETURN;
658}
659
660PP(pp_join)
661{
97aff369 662 dVAR; dSP; dMARK; dTARGET;
a0d0e21e
LW
663 MARK++;
664 do_join(TARG, *MARK, MARK, SP);
665 SP = MARK;
666 SETs(TARG);
667 RETURN;
668}
669
670PP(pp_pushre)
671{
97aff369 672 dVAR; dSP;
44a8e56a 673#ifdef DEBUGGING
674 /*
675 * We ass_u_me that LvTARGOFF() comes first, and that two STRLENs
676 * will be enough to hold an OP*.
677 */
c4420975 678 SV* const sv = sv_newmortal();
44a8e56a 679 sv_upgrade(sv, SVt_PVLV);
680 LvTYPE(sv) = '/';
533c011a 681 Copy(&PL_op, &LvTARGOFF(sv), 1, OP*);
44a8e56a 682 XPUSHs(sv);
683#else
ad64d0ec 684 XPUSHs(MUTABLE_SV(PL_op));
44a8e56a 685#endif
a0d0e21e
LW
686 RETURN;
687}
688
689/* Oversized hot code. */
690
691PP(pp_print)
692{
27da23d5 693 dVAR; dSP; dMARK; dORIGMARK;
eb578fdb 694 PerlIO *fp;
236988e4 695 MAGIC *mg;
159b6efe
NC
696 GV * const gv
697 = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv;
9c9f25b8 698 IO *io = GvIO(gv);
5b468f54 699
9c9f25b8 700 if (io
ad64d0ec 701 && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
5b468f54 702 {
01bb7c6d 703 had_magic:
68dc0745 704 if (MARK == ORIGMARK) {
1c846c1f 705 /* If using default handle then we need to make space to
a60c0954
NIS
706 * pass object as 1st arg, so move other args up ...
707 */
4352c267 708 MEXTEND(SP, 1);
68dc0745 709 ++MARK;
710 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
711 ++SP;
712 }
94bc412f
NC
713 return Perl_tied_method(aTHX_ "PRINT", mark - 1, MUTABLE_SV(io),
714 mg,
715 (G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK
716 | (PL_op->op_type == OP_SAY
717 ? TIED_METHOD_SAY : 0)), sp - mark);
236988e4 718 }
9c9f25b8 719 if (!io) {
68b590d9 720 if ( gv && GvEGVx(gv) && (io = GvIO(GvEGV(gv)))
ad64d0ec 721 && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
01bb7c6d 722 goto had_magic;
51087808 723 report_evil_fh(gv);
93189314 724 SETERRNO(EBADF,RMS_IFI);
a0d0e21e
LW
725 goto just_say_no;
726 }
727 else if (!(fp = IoOFP(io))) {
7716c5c5
NC
728 if (IoIFP(io))
729 report_wrongway_fh(gv, '<');
51087808 730 else
7716c5c5 731 report_evil_fh(gv);
93189314 732 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
a0d0e21e
LW
733 goto just_say_no;
734 }
735 else {
e23d9e2f 736 SV * const ofs = GvSV(PL_ofsgv); /* $, */
a0d0e21e 737 MARK++;
e23d9e2f 738 if (ofs && (SvGMAGICAL(ofs) || SvOK(ofs))) {
a0d0e21e
LW
739 while (MARK <= SP) {
740 if (!do_print(*MARK, fp))
741 break;
742 MARK++;
743 if (MARK <= SP) {
e23d9e2f
CS
744 /* don't use 'ofs' here - it may be invalidated by magic callbacks */
745 if (!do_print(GvSV(PL_ofsgv), fp)) {
a0d0e21e
LW
746 MARK--;
747 break;
748 }
749 }
750 }
751 }
752 else {
753 while (MARK <= SP) {
754 if (!do_print(*MARK, fp))
755 break;
756 MARK++;
757 }
758 }
759 if (MARK <= SP)
760 goto just_say_no;
761 else {
cfc4a7da
GA
762 if (PL_op->op_type == OP_SAY) {
763 if (PerlIO_write(fp, "\n", 1) == 0 || PerlIO_error(fp))
764 goto just_say_no;
765 }
766 else if (PL_ors_sv && SvOK(PL_ors_sv))
7889fe52 767 if (!do_print(PL_ors_sv, fp)) /* $\ */
a0d0e21e
LW
768 goto just_say_no;
769
770 if (IoFLAGS(io) & IOf_FLUSH)
760ac839 771 if (PerlIO_flush(fp) == EOF)
a0d0e21e
LW
772 goto just_say_no;
773 }
774 }
775 SP = ORIGMARK;
e52fd6f4 776 XPUSHs(&PL_sv_yes);
a0d0e21e
LW
777 RETURN;
778
779 just_say_no:
780 SP = ORIGMARK;
e52fd6f4 781 XPUSHs(&PL_sv_undef);
a0d0e21e
LW
782 RETURN;
783}
784
785PP(pp_rv2av)
786{
97aff369 787 dVAR; dSP; dTOPss;
cde874ca 788 const I32 gimme = GIMME_V;
17ab7946
NC
789 static const char an_array[] = "an ARRAY";
790 static const char a_hash[] = "a HASH";
791 const bool is_pp_rv2av = PL_op->op_type == OP_RV2AV;
d83b45b8 792 const svtype type = is_pp_rv2av ? SVt_PVAV : SVt_PVHV;
a0d0e21e 793
9026059d 794 SvGETMAGIC(sv);
a0d0e21e 795 if (SvROK(sv)) {
93d7320b
DM
796 if (SvAMAGIC(sv)) {
797 sv = amagic_deref_call(sv, is_pp_rv2av ? to_av_amg : to_hv_amg);
93d7320b 798 }
17ab7946
NC
799 sv = SvRV(sv);
800 if (SvTYPE(sv) != type)
dcbac5bb 801 /* diag_listed_as: Not an ARRAY reference */
17ab7946 802 DIE(aTHX_ "Not %s reference", is_pp_rv2av ? an_array : a_hash);
3da99855
FC
803 else if (PL_op->op_flags & OPf_MOD
804 && PL_op->op_private & OPpLVAL_INTRO)
805 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
a0d0e21e 806 }
9f527363 807 else if (SvTYPE(sv) != type) {
67955e0c 808 GV *gv;
1c846c1f 809
6e592b3a 810 if (!isGV_with_GP(sv)) {
dc3c76f8
NC
811 gv = Perl_softref2xv(aTHX_ sv, is_pp_rv2av ? an_array : a_hash,
812 type, &sp);
813 if (!gv)
814 RETURN;
35cd451c
GS
815 }
816 else {
159b6efe 817 gv = MUTABLE_GV(sv);
a0d0e21e 818 }
ad64d0ec 819 sv = is_pp_rv2av ? MUTABLE_SV(GvAVn(gv)) : MUTABLE_SV(GvHVn(gv));
533c011a 820 if (PL_op->op_private & OPpLVAL_INTRO)
ad64d0ec 821 sv = is_pp_rv2av ? MUTABLE_SV(save_ary(gv)) : MUTABLE_SV(save_hash(gv));
9f527363
FC
822 }
823 if (PL_op->op_flags & OPf_REF) {
17ab7946 824 SETs(sv);
a0d0e21e 825 RETURN;
9f527363
FC
826 }
827 else if (PL_op->op_private & OPpMAYBE_LVSUB) {
40c94d11
FC
828 const I32 flags = is_lvalue_sub();
829 if (flags && !(flags & OPpENTERSUB_INARGS)) {
cde874ca 830 if (gimme != G_ARRAY)
042560a6 831 goto croak_cant_return;
17ab7946 832 SETs(sv);
78f9721b 833 RETURN;
40c94d11 834 }
a0d0e21e
LW
835 }
836
17ab7946 837 if (is_pp_rv2av) {
502c6561 838 AV *const av = MUTABLE_AV(sv);
486ec47a 839 /* The guts of pp_rv2av, with no intending change to preserve history
17ab7946
NC
840 (until such time as we get tools that can do blame annotation across
841 whitespace changes. */
96913b52
VP
842 if (gimme == G_ARRAY) {
843 const I32 maxarg = AvFILL(av) + 1;
844 (void)POPs; /* XXXX May be optimized away? */
845 EXTEND(SP, maxarg);
846 if (SvRMAGICAL(av)) {
847 U32 i;
848 for (i=0; i < (U32)maxarg; i++) {
849 SV ** const svp = av_fetch(av, i, FALSE);
850 /* See note in pp_helem, and bug id #27839 */
851 SP[i+1] = svp
852 ? SvGMAGICAL(*svp) ? (mg_get(*svp), *svp) : *svp
853 : &PL_sv_undef;
854 }
855 }
856 else {
857 Copy(AvARRAY(av), SP+1, maxarg, SV*);
93965878 858 }
96913b52 859 SP += maxarg;
1c846c1f 860 }
96913b52
VP
861 else if (gimme == G_SCALAR) {
862 dTARGET;
863 const I32 maxarg = AvFILL(av) + 1;
864 SETi(maxarg);
93965878 865 }
17ab7946
NC
866 } else {
867 /* The guts of pp_rv2hv */
96913b52
VP
868 if (gimme == G_ARRAY) { /* array wanted */
869 *PL_stack_sp = sv;
981b7185 870 return Perl_do_kv(aTHX);
96913b52 871 }
c8fe3bdf 872 else if ((PL_op->op_private & OPpTRUEBOOL
adc42c31 873 || ( PL_op->op_private & OPpMAYBE_TRUEBOOL
c8fe3bdf
FC
874 && block_gimme() == G_VOID ))
875 && (!SvRMAGICAL(sv) || !mg_find(sv, PERL_MAGIC_tied)))
876 SETs(HvUSEDKEYS(sv) ? &PL_sv_yes : sv_2mortal(newSViv(0)));
96913b52
VP
877 else if (gimme == G_SCALAR) {
878 dTARGET;
879 TARG = Perl_hv_scalar(aTHX_ MUTABLE_HV(sv));
880 SPAGAIN;
881 SETTARG;
882 }
17ab7946 883 }
be85d344 884 RETURN;
042560a6
NC
885
886 croak_cant_return:
887 Perl_croak(aTHX_ "Can't return %s to lvalue scalar context",
888 is_pp_rv2av ? "array" : "hash");
77e217c6 889 RETURN;
a0d0e21e
LW
890}
891
10c8fecd
GS
892STATIC void
893S_do_oddball(pTHX_ HV *hash, SV **relem, SV **firstrelem)
894{
97aff369 895 dVAR;
7918f24d
NC
896
897 PERL_ARGS_ASSERT_DO_ODDBALL;
898
10c8fecd
GS
899 if (*relem) {
900 SV *tmpstr;
b464bac0 901 const HE *didstore;
6d822dc4
MS
902
903 if (ckWARN(WARN_MISC)) {
a3b680e6 904 const char *err;
10c8fecd
GS
905 if (relem == firstrelem &&
906 SvROK(*relem) &&
907 (SvTYPE(SvRV(*relem)) == SVt_PVAV ||
908 SvTYPE(SvRV(*relem)) == SVt_PVHV))
909 {
a3b680e6 910 err = "Reference found where even-sized list expected";
10c8fecd
GS
911 }
912 else
a3b680e6 913 err = "Odd number of elements in hash assignment";
f1f66076 914 Perl_warner(aTHX_ packWARN(WARN_MISC), "%s", err);
10c8fecd 915 }
6d822dc4 916
561b68a9 917 tmpstr = newSV(0);
6d822dc4
MS
918 didstore = hv_store_ent(hash,*relem,tmpstr,0);
919 if (SvMAGICAL(hash)) {
920 if (SvSMAGICAL(tmpstr))
921 mg_set(tmpstr);
922 if (!didstore)
923 sv_2mortal(tmpstr);
924 }
925 TAINT_NOT;
10c8fecd
GS
926 }
927}
928
a0d0e21e
LW
929PP(pp_aassign)
930{
27da23d5 931 dVAR; dSP;
3280af22
NIS
932 SV **lastlelem = PL_stack_sp;
933 SV **lastrelem = PL_stack_base + POPMARK;
934 SV **firstrelem = PL_stack_base + POPMARK + 1;
a0d0e21e
LW
935 SV **firstlelem = lastrelem + 1;
936
eb578fdb
KW
937 SV **relem;
938 SV **lelem;
a0d0e21e 939
eb578fdb
KW
940 SV *sv;
941 AV *ary;
a0d0e21e 942
54310121 943 I32 gimme;
a0d0e21e
LW
944 HV *hash;
945 I32 i;
946 int magic;
ca65944e 947 int duplicates = 0;
cbbf8932 948 SV **firsthashrelem = NULL; /* "= 0" keeps gcc 2.95 quiet */
5637b936 949
3280af22 950 PL_delaymagic = DM_DELAY; /* catch simultaneous items */
ca65944e 951 gimme = GIMME_V;
a0d0e21e
LW
952
953 /* If there's a common identifier on both sides we have to take
954 * special care that assigning the identifier on the left doesn't
955 * clobber a value on the right that's used later in the list.
acdea6f0 956 * Don't bother if LHS is just an empty hash or array.
a0d0e21e 957 */
acdea6f0
DM
958
959 if ( (PL_op->op_private & OPpASSIGN_COMMON)
960 && (
961 firstlelem != lastlelem
962 || ! ((sv = *firstlelem))
963 || SvMAGICAL(sv)
964 || ! (SvTYPE(sv) == SVt_PVAV || SvTYPE(sv) == SVt_PVHV)
965 || (SvTYPE(sv) == SVt_PVAV && AvFILL((AV*)sv) != -1)
1b95d04f 966 || (SvTYPE(sv) == SVt_PVHV && HvUSEDKEYS((HV*)sv) != 0)
acdea6f0
DM
967 )
968 ) {
cc5e57d2 969 EXTEND_MORTAL(lastrelem - firstrelem + 1);
10c8fecd 970 for (relem = firstrelem; relem <= lastrelem; relem++) {
155aba94 971 if ((sv = *relem)) {
a1f49e72 972 TAINT_NOT; /* Each item is independent */
61e5f455
NC
973
974 /* Dear TODO test in t/op/sort.t, I love you.
975 (It's relying on a panic, not a "semi-panic" from newSVsv()
976 and then an assertion failure below.) */
977 if (SvIS_FREED(sv)) {
978 Perl_croak(aTHX_ "panic: attempt to copy freed scalar %p",
979 (void*)sv);
980 }
2203fb5a
FC
981 /* Not newSVsv(), as it does not allow copy-on-write,
982 resulting in wasteful copies. We need a second copy of
983 a temp here, hence the SV_NOSTEAL. */
984 *relem = sv_mortalcopy_flags(sv,SV_GMAGIC|SV_DO_COW_SVSETSV
985 |SV_NOSTEAL);
a1f49e72 986 }
10c8fecd 987 }
a0d0e21e
LW
988 }
989
990 relem = firstrelem;
991 lelem = firstlelem;
4608196e
RGS
992 ary = NULL;
993 hash = NULL;
10c8fecd 994
a0d0e21e 995 while (lelem <= lastlelem) {
bbce6d69 996 TAINT_NOT; /* Each item stands on its own, taintwise. */
a0d0e21e
LW
997 sv = *lelem++;
998 switch (SvTYPE(sv)) {
999 case SVt_PVAV:
60edcf09 1000 ary = MUTABLE_AV(sv);
748a9306 1001 magic = SvMAGICAL(ary) != 0;
60edcf09
FC
1002 ENTER;
1003 SAVEFREESV(SvREFCNT_inc_simple_NN(sv));
a0d0e21e 1004 av_clear(ary);
7e42bd57 1005 av_extend(ary, lastrelem - relem);
a0d0e21e
LW
1006 i = 0;
1007 while (relem <= lastrelem) { /* gobble up all the rest */
5117ca91 1008 SV **didstore;
a0d0e21e 1009 assert(*relem);
18024492
FC
1010 SvGETMAGIC(*relem); /* before newSV, in case it dies */
1011 sv = newSV(0);
1012 sv_setsv_nomg(sv, *relem);
a0d0e21e 1013 *(relem++) = sv;
5117ca91
GS
1014 didstore = av_store(ary,i++,sv);
1015 if (magic) {
18024492
FC
1016 if (!didstore)
1017 sv_2mortal(sv);
8ef24240 1018 if (SvSMAGICAL(sv))
fb73857a 1019 mg_set(sv);
5117ca91 1020 }
bbce6d69 1021 TAINT_NOT;
a0d0e21e 1022 }
354b0578 1023 if (PL_delaymagic & DM_ARRAY_ISA)
ad64d0ec 1024 SvSETMAGIC(MUTABLE_SV(ary));
60edcf09 1025 LEAVE;
a0d0e21e 1026 break;
10c8fecd 1027 case SVt_PVHV: { /* normal hash */
a0d0e21e 1028 SV *tmpstr;
45960564 1029 SV** topelem = relem;
a0d0e21e 1030
60edcf09 1031 hash = MUTABLE_HV(sv);
748a9306 1032 magic = SvMAGICAL(hash) != 0;
60edcf09
FC
1033 ENTER;
1034 SAVEFREESV(SvREFCNT_inc_simple_NN(sv));
a0d0e21e 1035 hv_clear(hash);
ca65944e 1036 firsthashrelem = relem;
a0d0e21e
LW
1037
1038 while (relem < lastrelem) { /* gobble up all the rest */
5117ca91 1039 HE *didstore;
6136c704
AL
1040 sv = *relem ? *relem : &PL_sv_no;
1041 relem++;
9c744f4f 1042 tmpstr = sv_newmortal();
a0d0e21e
LW
1043 if (*relem)
1044 sv_setsv(tmpstr,*relem); /* value */
45960564
DM
1045 relem++;
1046 if (gimme != G_VOID) {
1047 if (hv_exists_ent(hash, sv, 0))
1048 /* key overwrites an existing entry */
1049 duplicates += 2;
1050 else
1051 if (gimme == G_ARRAY) {
1052 /* copy element back: possibly to an earlier
1053 * stack location if we encountered dups earlier */
1054 *topelem++ = sv;
1055 *topelem++ = tmpstr;
1056 }
1057 }
5117ca91 1058 didstore = hv_store_ent(hash,sv,tmpstr,0);
9c744f4f 1059 if (didstore) SvREFCNT_inc_simple_void_NN(tmpstr);
5117ca91 1060 if (magic) {
8ef24240 1061 if (SvSMAGICAL(tmpstr))
fb73857a 1062 mg_set(tmpstr);
5117ca91 1063 }
bbce6d69 1064 TAINT_NOT;
8e07c86e 1065 }
6a0deba8 1066 if (relem == lastrelem) {
10c8fecd 1067 do_oddball(hash, relem, firstrelem);
6a0deba8 1068 relem++;
1930e939 1069 }
60edcf09 1070 LEAVE;
a0d0e21e
LW
1071 }
1072 break;
1073 default:
6fc92669
GS
1074 if (SvIMMORTAL(sv)) {
1075 if (relem <= lastrelem)
1076 relem++;
1077 break;
a0d0e21e
LW
1078 }
1079 if (relem <= lastrelem) {
1c70fb82
FC
1080 if (
1081 SvTEMP(sv) && !SvSMAGICAL(sv) && SvREFCNT(sv) == 1 &&
1082 (!isGV_with_GP(sv) || SvFAKE(sv)) && ckWARN(WARN_MISC)
1083 )
1084 Perl_warner(aTHX_
1085 packWARN(WARN_MISC),
1086 "Useless assignment to a temporary"
1087 );
a0d0e21e
LW
1088 sv_setsv(sv, *relem);
1089 *(relem++) = sv;
1090 }
1091 else
3280af22 1092 sv_setsv(sv, &PL_sv_undef);
8ef24240 1093 SvSETMAGIC(sv);
a0d0e21e
LW
1094 break;
1095 }
1096 }
3280af22 1097 if (PL_delaymagic & ~DM_DELAY) {
985213f2
AB
1098 /* Will be used to set PL_tainting below */
1099 UV tmp_uid = PerlProc_getuid();
1100 UV tmp_euid = PerlProc_geteuid();
1101 UV tmp_gid = PerlProc_getgid();
1102 UV tmp_egid = PerlProc_getegid();
1103
3280af22 1104 if (PL_delaymagic & DM_UID) {
a0d0e21e 1105#ifdef HAS_SETRESUID
985213f2
AB
1106 (void)setresuid((PL_delaymagic & DM_RUID) ? PL_delaymagic_uid : (Uid_t)-1,
1107 (PL_delaymagic & DM_EUID) ? PL_delaymagic_euid : (Uid_t)-1,
fb934a90 1108 (Uid_t)-1);
56febc5e
AD
1109#else
1110# ifdef HAS_SETREUID
985213f2
AB
1111 (void)setreuid((PL_delaymagic & DM_RUID) ? PL_delaymagic_uid : (Uid_t)-1,
1112 (PL_delaymagic & DM_EUID) ? PL_delaymagic_euid : (Uid_t)-1);
56febc5e
AD
1113# else
1114# ifdef HAS_SETRUID
b28d0864 1115 if ((PL_delaymagic & DM_UID) == DM_RUID) {
985213f2 1116 (void)setruid(PL_delaymagic_uid);
b28d0864 1117 PL_delaymagic &= ~DM_RUID;
a0d0e21e 1118 }
56febc5e
AD
1119# endif /* HAS_SETRUID */
1120# ifdef HAS_SETEUID
b28d0864 1121 if ((PL_delaymagic & DM_UID) == DM_EUID) {
985213f2 1122 (void)seteuid(PL_delaymagic_euid);
b28d0864 1123 PL_delaymagic &= ~DM_EUID;
a0d0e21e 1124 }
56febc5e 1125# endif /* HAS_SETEUID */
b28d0864 1126 if (PL_delaymagic & DM_UID) {
985213f2 1127 if (PL_delaymagic_uid != PL_delaymagic_euid)
cea2e8a9 1128 DIE(aTHX_ "No setreuid available");
985213f2 1129 (void)PerlProc_setuid(PL_delaymagic_uid);
a0d0e21e 1130 }
56febc5e
AD
1131# endif /* HAS_SETREUID */
1132#endif /* HAS_SETRESUID */
985213f2
AB
1133 tmp_uid = PerlProc_getuid();
1134 tmp_euid = PerlProc_geteuid();
a0d0e21e 1135 }
3280af22 1136 if (PL_delaymagic & DM_GID) {
a0d0e21e 1137#ifdef HAS_SETRESGID
985213f2
AB
1138 (void)setresgid((PL_delaymagic & DM_RGID) ? PL_delaymagic_gid : (Gid_t)-1,
1139 (PL_delaymagic & DM_EGID) ? PL_delaymagic_egid : (Gid_t)-1,
fb934a90 1140 (Gid_t)-1);
56febc5e
AD
1141#else
1142# ifdef HAS_SETREGID
985213f2
AB
1143 (void)setregid((PL_delaymagic & DM_RGID) ? PL_delaymagic_gid : (Gid_t)-1,
1144 (PL_delaymagic & DM_EGID) ? PL_delaymagic_egid : (Gid_t)-1);
56febc5e
AD
1145# else
1146# ifdef HAS_SETRGID
b28d0864 1147 if ((PL_delaymagic & DM_GID) == DM_RGID) {
985213f2 1148 (void)setrgid(PL_delaymagic_gid);
b28d0864 1149 PL_delaymagic &= ~DM_RGID;
a0d0e21e 1150 }
56febc5e
AD
1151# endif /* HAS_SETRGID */
1152# ifdef HAS_SETEGID
b28d0864 1153 if ((PL_delaymagic & DM_GID) == DM_EGID) {
985213f2 1154 (void)setegid(PL_delaymagic_egid);
b28d0864 1155 PL_delaymagic &= ~DM_EGID;
a0d0e21e 1156 }
56febc5e 1157# endif /* HAS_SETEGID */
b28d0864 1158 if (PL_delaymagic & DM_GID) {
985213f2 1159 if (PL_delaymagic_gid != PL_delaymagic_egid)
cea2e8a9 1160 DIE(aTHX_ "No setregid available");
985213f2 1161 (void)PerlProc_setgid(PL_delaymagic_gid);
a0d0e21e 1162 }
56febc5e
AD
1163# endif /* HAS_SETREGID */
1164#endif /* HAS_SETRESGID */
985213f2
AB
1165 tmp_gid = PerlProc_getgid();
1166 tmp_egid = PerlProc_getegid();
a0d0e21e 1167 }
284167a5 1168 TAINTING_set( TAINTING_get | (tmp_uid && (tmp_euid != tmp_uid || tmp_egid != tmp_gid)) );
a0d0e21e 1169 }
3280af22 1170 PL_delaymagic = 0;
54310121 1171
54310121 1172 if (gimme == G_VOID)
1173 SP = firstrelem - 1;
1174 else if (gimme == G_SCALAR) {
1175 dTARGET;
1176 SP = firstrelem;
ca65944e 1177 SETi(lastrelem - firstrelem + 1 - duplicates);
54310121 1178 }
1179 else {
ca65944e 1180 if (ary)
a0d0e21e 1181 SP = lastrelem;
ca65944e
RGS
1182 else if (hash) {
1183 if (duplicates) {
45960564
DM
1184 /* at this point we have removed the duplicate key/value
1185 * pairs from the stack, but the remaining values may be
1186 * wrong; i.e. with (a 1 a 2 b 3) on the stack we've removed
1187 * the (a 2), but the stack now probably contains
1188 * (a <freed> b 3), because { hv_save(a,1); hv_save(a,2) }
1189 * obliterates the earlier key. So refresh all values. */
ca65944e 1190 lastrelem -= duplicates;
45960564
DM
1191 relem = firsthashrelem;
1192 while (relem < lastrelem) {
1193 HE *he;
1194 sv = *relem++;
1195 he = hv_fetch_ent(hash, sv, 0, 0);
1196 *relem++ = (he ? HeVAL(he) : &PL_sv_undef);
1197 }
ca65944e
RGS
1198 }
1199 SP = lastrelem;
1200 }
a0d0e21e
LW
1201 else
1202 SP = firstrelem + (lastlelem - firstlelem);
0c8c7a05 1203 lelem = firstlelem + (relem - firstrelem);
5f05dabc 1204 while (relem <= SP)
3280af22 1205 *relem++ = (lelem <= lastlelem) ? *lelem++ : &PL_sv_undef;
a0d0e21e 1206 }
08aeb9f7 1207
54310121 1208 RETURN;
a0d0e21e
LW
1209}
1210
8782bef2
GB
1211PP(pp_qr)
1212{
97aff369 1213 dVAR; dSP;
eb578fdb 1214 PMOP * const pm = cPMOP;
fe578d7f 1215 REGEXP * rx = PM_GETRE(pm);
10599a69 1216 SV * const pkg = rx ? CALLREG_PACKAGE(rx) : NULL;
c4420975 1217 SV * const rv = sv_newmortal();
d63c20f2
DM
1218 CV **cvp;
1219 CV *cv;
288b8c02
NC
1220
1221 SvUPGRADE(rv, SVt_IV);
c2123ae3
NC
1222 /* For a subroutine describing itself as "This is a hacky workaround" I'm
1223 loathe to use it here, but it seems to be the right fix. Or close.
1224 The key part appears to be that it's essential for pp_qr to return a new
1225 object (SV), which implies that there needs to be an effective way to
1226 generate a new SV from the existing SV that is pre-compiled in the
1227 optree. */
1228 SvRV_set(rv, MUTABLE_SV(reg_temp_copy(NULL, rx)));
288b8c02
NC
1229 SvROK_on(rv);
1230
8d919b0a 1231 cvp = &( ReANY((REGEXP *)SvRV(rv))->qr_anoncv);
d63c20f2
DM
1232 if ((cv = *cvp) && CvCLONE(*cvp)) {
1233 *cvp = cv_clone(cv);
1234 SvREFCNT_dec(cv);
1235 }
1236
288b8c02 1237 if (pkg) {
f815daf2 1238 HV *const stash = gv_stashsv(pkg, GV_ADD);
a954f6ee 1239 SvREFCNT_dec(pkg);
288b8c02
NC
1240 (void)sv_bless(rv, stash);
1241 }
1242
284167a5 1243 if (RX_ISTAINTED(rx)) {
e08e52cf 1244 SvTAINTED_on(rv);
9274aefd
DM
1245 SvTAINTED_on(SvRV(rv));
1246 }
c8c13c22
JB
1247 XPUSHs(rv);
1248 RETURN;
8782bef2
GB
1249}
1250
a0d0e21e
LW
1251PP(pp_match)
1252{
97aff369 1253 dVAR; dSP; dTARG;
eb578fdb 1254 PMOP *pm = cPMOP;
d65afb4b 1255 PMOP *dynpm = pm;
eb578fdb
KW
1256 const char *t;
1257 const char *s;
5c144d81 1258 const char *strend;
a0d0e21e 1259 I32 global;
1ed74d04 1260 U8 r_flags = REXEC_CHECKED;
5c144d81 1261 const char *truebase; /* Start of string */
eb578fdb 1262 REGEXP *rx = PM_GETRE(pm);
b3eb6a9b 1263 bool rxtainted;
a3b680e6 1264 const I32 gimme = GIMME;
a0d0e21e 1265 STRLEN len;
748a9306 1266 I32 minmatch = 0;
a3b680e6 1267 const I32 oldsave = PL_savestack_ix;
f86702cc 1268 I32 update_minmatch = 1;
e60df1fa 1269 I32 had_zerolen = 0;
58e23c8d 1270 U32 gpos = 0;
a0d0e21e 1271
533c011a 1272 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e 1273 TARG = POPs;
59f00321
RGS
1274 else if (PL_op->op_private & OPpTARGET_MY)
1275 GETTARGET;
a0d0e21e 1276 else {
54b9620d 1277 TARG = DEFSV;
a0d0e21e
LW
1278 EXTEND(SP,1);
1279 }
d9f424b2 1280
c277df42 1281 PUTBACK; /* EVAL blocks need stack_sp. */
69dc4b30
FC
1282 /* Skip get-magic if this is a qr// clone, because regcomp has
1283 already done it. */
8d919b0a 1284 s = ReANY(rx)->mother_re
69dc4b30
FC
1285 ? SvPV_nomg_const(TARG, len)
1286 : SvPV_const(TARG, len);
a0d0e21e 1287 if (!s)
2269b42e 1288 DIE(aTHX_ "panic: pp_match");
890ce7af 1289 strend = s + len;
284167a5
S
1290 rxtainted = (RX_ISTAINTED(rx) ||
1291 (TAINT_get && (pm->op_pmflags & PMf_RETAINT)));
9212bbba 1292 TAINT_NOT;
a0d0e21e 1293
a30b2f1f 1294 RX_MATCH_UTF8_set(rx, DO_UTF8(TARG));
d9f424b2 1295
d65afb4b 1296 /* PMdf_USED is set after a ?? matches once */
c737faaf
YO
1297 if (
1298#ifdef USE_ITHREADS
1299 SvREADONLY(PL_regex_pad[pm->op_pmoffset])
1300#else
1301 pm->op_pmflags & PMf_USED
1302#endif
1303 ) {
e5dc5375 1304 DEBUG_r(PerlIO_printf(Perl_debug_log, "?? already matched once"));
c277df42 1305 failure:
e5dc5375 1306
a0d0e21e
LW
1307 if (gimme == G_ARRAY)
1308 RETURN;
1309 RETPUSHNO;
1310 }
1311
c737faaf
YO
1312
1313
7e313637
FC
1314 /* empty pattern special-cased to use last successful pattern if
1315 possible, except for qr// */
8d919b0a 1316 if (!ReANY(rx)->mother_re && !RX_PRELEN(rx)
7e313637 1317 && PL_curpm) {
3280af22 1318 pm = PL_curpm;
aaa362c4 1319 rx = PM_GETRE(pm);
a0d0e21e 1320 }
d65afb4b 1321
e5dc5375
KW
1322 if (RX_MINLEN(rx) > (I32)len) {
1323 DEBUG_r(PerlIO_printf(Perl_debug_log, "String shorter than min possible regex match\n"));
d65afb4b 1324 goto failure;
e5dc5375 1325 }
c277df42 1326
a0d0e21e 1327 truebase = t = s;
ad94a511
IZ
1328
1329 /* XXXX What part of this is needed with true \G-support? */
d65afb4b 1330 if ((global = dynpm->op_pmflags & PMf_GLOBAL)) {
07bc277f 1331 RX_OFFS(rx)[0].start = -1;
a0d0e21e 1332 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
c445ea15 1333 MAGIC* const mg = mg_find(TARG, PERL_MAGIC_regex_global);
565764a8 1334 if (mg && mg->mg_len >= 0) {
07bc277f
NC
1335 if (!(RX_EXTFLAGS(rx) & RXf_GPOS_SEEN))
1336 RX_OFFS(rx)[0].end = RX_OFFS(rx)[0].start = mg->mg_len;
1337 else if (RX_EXTFLAGS(rx) & RXf_ANCH_GPOS) {
0ef3e39e 1338 r_flags |= REXEC_IGNOREPOS;
07bc277f
NC
1339 RX_OFFS(rx)[0].end = RX_OFFS(rx)[0].start = mg->mg_len;
1340 } else if (RX_EXTFLAGS(rx) & RXf_GPOS_FLOAT)
58e23c8d
YO
1341 gpos = mg->mg_len;
1342 else
07bc277f
NC
1343 RX_OFFS(rx)[0].end = RX_OFFS(rx)[0].start = mg->mg_len;
1344 minmatch = (mg->mg_flags & MGf_MINMATCH) ? RX_GOFS(rx) + 1 : 0;
f86702cc 1345 update_minmatch = 0;
748a9306 1346 }
a0d0e21e
LW
1347 }
1348 }
a41aa44d 1349 if ( RX_NPARENS(rx)
6502e081 1350 || PL_sawampersand
6502e081
DM
1351 || (RX_EXTFLAGS(rx) & (RXf_EVAL_SEEN|RXf_PMf_KEEPCOPY))
1352 ) {
1353 r_flags |= (REXEC_COPY_STR|REXEC_COPY_SKIP_PRE);
1354 /* in @a =~ /(.)/g, we iterate multiple times, but copy the buffer
1355 * only on the first iteration. Therefore we need to copy $' as well
1356 * as $&, to make the rest of the string available for captures in
1357 * subsequent iterations */
1358 if (! (global && gimme == G_ARRAY))
1359 r_flags |= REXEC_COPY_SKIP_POST;
1360 };
22e551b9 1361
d7be1480 1362 play_it_again:
07bc277f
NC
1363 if (global && RX_OFFS(rx)[0].start != -1) {
1364 t = s = RX_OFFS(rx)[0].end + truebase - RX_GOFS(rx);
e5dc5375
KW
1365 if ((s + RX_MINLEN(rx)) > strend || s < truebase) {
1366 DEBUG_r(PerlIO_printf(Perl_debug_log, "Regex match can't succeed, so not even tried\n"));
a0d0e21e 1367 goto nope;
e5dc5375 1368 }
f86702cc 1369 if (update_minmatch++)
e60df1fa 1370 minmatch = had_zerolen;
a0d0e21e 1371 }
07bc277f 1372 if (RX_EXTFLAGS(rx) & RXf_USE_INTUIT &&
3c8556c3 1373 DO_UTF8(TARG) == (RX_UTF8(rx) != 0)) {
5c144d81
NC
1374 /* FIXME - can PL_bostr be made const char *? */
1375 PL_bostr = (char *)truebase;
f9f4320a 1376 s = CALLREG_INTUIT_START(rx, TARG, (char *)s, (char *)strend, r_flags, NULL);
f722798b
IZ
1377
1378 if (!s)
1379 goto nope;
07bc277f 1380 if ( (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
14977893 1381 && !PL_sawampersand
07bc277f 1382 && !(RX_EXTFLAGS(rx) & RXf_PMf_KEEPCOPY)
05b4157f 1383 && !SvROK(TARG)) /* Cannot trust since INTUIT cannot guess ^ */
f722798b 1384 goto yup;
a0d0e21e 1385 }
77da2310
NC
1386 if (!CALLREGEXEC(rx, (char*)s, (char *)strend, (char*)truebase,
1387 minmatch, TARG, NUM2PTR(void*, gpos), r_flags))
1388 goto ret_no;
1389
1390 PL_curpm = pm;
1391 if (dynpm->op_pmflags & PMf_ONCE) {
c737faaf 1392#ifdef USE_ITHREADS
77da2310 1393 SvREADONLY_on(PL_regex_pad[dynpm->op_pmoffset]);
c737faaf 1394#else
77da2310 1395 dynpm->op_pmflags |= PMf_USED;
c737faaf 1396#endif
a0d0e21e 1397 }
a0d0e21e
LW
1398
1399 gotcha:
72311751
GS
1400 if (rxtainted)
1401 RX_MATCH_TAINTED_on(rx);
1402 TAINT_IF(RX_MATCH_TAINTED(rx));
a0d0e21e 1403 if (gimme == G_ARRAY) {
07bc277f 1404 const I32 nparens = RX_NPARENS(rx);
a3b680e6 1405 I32 i = (global && !nparens) ? 1 : 0;
a0d0e21e 1406
c277df42 1407 SPAGAIN; /* EVAL blocks could move the stack. */
ffc61ed2
JH
1408 EXTEND(SP, nparens + i);
1409 EXTEND_MORTAL(nparens + i);
1410 for (i = !i; i <= nparens; i++) {
a0d0e21e 1411 PUSHs(sv_newmortal());
07bc277f
NC
1412 if ((RX_OFFS(rx)[i].start != -1) && RX_OFFS(rx)[i].end != -1 ) {
1413 const I32 len = RX_OFFS(rx)[i].end - RX_OFFS(rx)[i].start;
1414 s = RX_OFFS(rx)[i].start + truebase;
1415 if (RX_OFFS(rx)[i].end < 0 || RX_OFFS(rx)[i].start < 0 ||
290deeac 1416 len < 0 || len > strend - s)
5637ef5b
NC
1417 DIE(aTHX_ "panic: pp_match start/end pointers, i=%ld, "
1418 "start=%ld, end=%ld, s=%p, strend=%p, len=%"UVuf,
1419 (long) i, (long) RX_OFFS(rx)[i].start,
1420 (long)RX_OFFS(rx)[i].end, s, strend, (UV) len);
a0d0e21e 1421 sv_setpvn(*SP, s, len);
cce850e4 1422 if (DO_UTF8(TARG) && is_utf8_string((U8*)s, len))
a197cbdd 1423 SvUTF8_on(*SP);
a0d0e21e
LW
1424 }
1425 }
1426 if (global) {
d65afb4b 1427 if (dynpm->op_pmflags & PMf_CONTINUE) {
6136c704 1428 MAGIC* mg = NULL;
0af80b60
HS
1429 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
1430 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1431 if (!mg) {
d83f0a82
NC
1432#ifdef PERL_OLD_COPY_ON_WRITE
1433 if (SvIsCOW(TARG))
1434 sv_force_normal_flags(TARG, 0);
1435#endif
1436 mg = sv_magicext(TARG, NULL, PERL_MAGIC_regex_global,
1437 &PL_vtbl_mglob, NULL, 0);
0af80b60 1438 }
07bc277f
NC
1439 if (RX_OFFS(rx)[0].start != -1) {
1440 mg->mg_len = RX_OFFS(rx)[0].end;
1441 if (RX_OFFS(rx)[0].start + RX_GOFS(rx) == (UV)RX_OFFS(rx)[0].end)
0af80b60
HS
1442 mg->mg_flags |= MGf_MINMATCH;
1443 else
1444 mg->mg_flags &= ~MGf_MINMATCH;
1445 }
1446 }
07bc277f
NC
1447 had_zerolen = (RX_OFFS(rx)[0].start != -1
1448 && (RX_OFFS(rx)[0].start + RX_GOFS(rx)
1449 == (UV)RX_OFFS(rx)[0].end));
c277df42 1450 PUTBACK; /* EVAL blocks may use stack */
cf93c79d 1451 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
a0d0e21e
LW
1452 goto play_it_again;
1453 }
ffc61ed2 1454 else if (!nparens)
bde848c5 1455 XPUSHs(&PL_sv_yes);
4633a7c4 1456 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1457 RETURN;
1458 }
1459 else {
1460 if (global) {
cbbf8932 1461 MAGIC* mg;
a0d0e21e 1462 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
14befaf4 1463 mg = mg_find(TARG, PERL_MAGIC_regex_global);
cbbf8932
AL
1464 else
1465 mg = NULL;
a0d0e21e 1466 if (!mg) {
d83f0a82
NC
1467#ifdef PERL_OLD_COPY_ON_WRITE
1468 if (SvIsCOW(TARG))
1469 sv_force_normal_flags(TARG, 0);
1470#endif
1471 mg = sv_magicext(TARG, NULL, PERL_MAGIC_regex_global,
1472 &PL_vtbl_mglob, NULL, 0);
a0d0e21e 1473 }
07bc277f
NC
1474 if (RX_OFFS(rx)[0].start != -1) {
1475 mg->mg_len = RX_OFFS(rx)[0].end;
1476 if (RX_OFFS(rx)[0].start + RX_GOFS(rx) == (UV)RX_OFFS(rx)[0].end)
748a9306
LW
1477 mg->mg_flags |= MGf_MINMATCH;
1478 else
1479 mg->mg_flags &= ~MGf_MINMATCH;
1480 }
a0d0e21e 1481 }
4633a7c4 1482 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1483 RETPUSHYES;
1484 }
1485
f722798b 1486yup: /* Confirmed by INTUIT */
72311751
GS
1487 if (rxtainted)
1488 RX_MATCH_TAINTED_on(rx);
1489 TAINT_IF(RX_MATCH_TAINTED(rx));
3280af22 1490 PL_curpm = pm;
c737faaf
YO
1491 if (dynpm->op_pmflags & PMf_ONCE) {
1492#ifdef USE_ITHREADS
1493 SvREADONLY_on(PL_regex_pad[dynpm->op_pmoffset]);
1494#else
1495 dynpm->op_pmflags |= PMf_USED;
1496#endif
1497 }
cf93c79d 1498 if (RX_MATCH_COPIED(rx))
07bc277f 1499 Safefree(RX_SUBBEG(rx));
cf93c79d 1500 RX_MATCH_COPIED_off(rx);
07bc277f 1501 RX_SUBBEG(rx) = NULL;
a0d0e21e 1502 if (global) {
5c144d81 1503 /* FIXME - should rx->subbeg be const char *? */
07bc277f 1504 RX_SUBBEG(rx) = (char *) truebase;
6502e081
DM
1505 RX_SUBOFFSET(rx) = 0;
1506 RX_SUBCOFFSET(rx) = 0;
07bc277f 1507 RX_OFFS(rx)[0].start = s - truebase;
a30b2f1f 1508 if (RX_MATCH_UTF8(rx)) {
07bc277f
NC
1509 char * const t = (char*)utf8_hop((U8*)s, RX_MINLENRET(rx));
1510 RX_OFFS(rx)[0].end = t - truebase;
60aeb6fd
NIS
1511 }
1512 else {
07bc277f 1513 RX_OFFS(rx)[0].end = s - truebase + RX_MINLENRET(rx);
60aeb6fd 1514 }
07bc277f 1515 RX_SUBLEN(rx) = strend - truebase;
a0d0e21e 1516 goto gotcha;
1c846c1f 1517 }
07bc277f 1518 if (PL_sawampersand || RX_EXTFLAGS(rx) & RXf_PMf_KEEPCOPY) {
14977893 1519 I32 off;
f8c7b90f 1520#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
1521 if (SvIsCOW(TARG) || (SvFLAGS(TARG) & CAN_COW_MASK) == CAN_COW_FLAGS) {
1522 if (DEBUG_C_TEST) {
1523 PerlIO_printf(Perl_debug_log,
1524 "Copy on write: pp_match $& capture, type %d, truebase=%p, t=%p, difference %d\n",
6c9570dc 1525 (int) SvTYPE(TARG), (void*)truebase, (void*)t,
ed252734
NC
1526 (int)(t-truebase));
1527 }
bdd9a1b1
NC
1528 RX_SAVED_COPY(rx) = sv_setsv_cow(RX_SAVED_COPY(rx), TARG);
1529 RX_SUBBEG(rx)
1530 = (char *) SvPVX_const(RX_SAVED_COPY(rx)) + (t - truebase);
1531 assert (SvPOKp(RX_SAVED_COPY(rx)));
ed252734
NC
1532 } else
1533#endif
1534 {
14977893 1535
07bc277f 1536 RX_SUBBEG(rx) = savepvn(t, strend - t);
f8c7b90f 1537#ifdef PERL_OLD_COPY_ON_WRITE
bdd9a1b1 1538 RX_SAVED_COPY(rx) = NULL;
ed252734
NC
1539#endif
1540 }
07bc277f 1541 RX_SUBLEN(rx) = strend - t;
6502e081
DM
1542 RX_SUBOFFSET(rx) = 0;
1543 RX_SUBCOFFSET(rx) = 0;
14977893 1544 RX_MATCH_COPIED_on(rx);
07bc277f
NC
1545 off = RX_OFFS(rx)[0].start = s - t;
1546 RX_OFFS(rx)[0].end = off + RX_MINLENRET(rx);
14977893
JH
1547 }
1548 else { /* startp/endp are used by @- @+. */
07bc277f
NC
1549 RX_OFFS(rx)[0].start = s - truebase;
1550 RX_OFFS(rx)[0].end = s - truebase + RX_MINLENRET(rx);
14977893 1551 }
7e1a2c8d
DM
1552 /* match via INTUIT shouldn't have any captures. Let @-, @+, $^N know */
1553 assert(!RX_NPARENS(rx));
1554 RX_LASTPAREN(rx) = RX_LASTCLOSEPAREN(rx) = 0;
4633a7c4 1555 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1556 RETPUSHYES;
1557
1558nope:
a0d0e21e 1559ret_no:
d65afb4b 1560 if (global && !(dynpm->op_pmflags & PMf_CONTINUE)) {
a0d0e21e 1561 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
6136c704 1562 MAGIC* const mg = mg_find(TARG, PERL_MAGIC_regex_global);
a0d0e21e 1563 if (mg)
565764a8 1564 mg->mg_len = -1;
a0d0e21e
LW
1565 }
1566 }
4633a7c4 1567 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1568 if (gimme == G_ARRAY)
1569 RETURN;
1570 RETPUSHNO;
1571}
1572
1573OP *
864dbfa3 1574Perl_do_readline(pTHX)
a0d0e21e 1575{
27da23d5 1576 dVAR; dSP; dTARGETSTACKED;
eb578fdb 1577 SV *sv;
a0d0e21e
LW
1578 STRLEN tmplen = 0;
1579 STRLEN offset;
760ac839 1580 PerlIO *fp;
eb578fdb
KW
1581 IO * const io = GvIO(PL_last_in_gv);
1582 const I32 type = PL_op->op_type;
a3b680e6 1583 const I32 gimme = GIMME_V;
a0d0e21e 1584
6136c704 1585 if (io) {
50db69d8 1586 const MAGIC *const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
6136c704 1587 if (mg) {
50db69d8 1588 Perl_tied_method(aTHX_ "READLINE", SP, MUTABLE_SV(io), mg, gimme, 0);
6136c704 1589 if (gimme == G_SCALAR) {
50db69d8
NC
1590 SPAGAIN;
1591 SvSetSV_nosteal(TARG, TOPs);
1592 SETTARG;
6136c704 1593 }
50db69d8 1594 return NORMAL;
0b7c7b4f 1595 }
e79b0511 1596 }
4608196e 1597 fp = NULL;
a0d0e21e
LW
1598 if (io) {
1599 fp = IoIFP(io);
1600 if (!fp) {
1601 if (IoFLAGS(io) & IOf_ARGV) {
1602 if (IoFLAGS(io) & IOf_START) {
a0d0e21e 1603 IoLINES(io) = 0;
3280af22 1604 if (av_len(GvAVn(PL_last_in_gv)) < 0) {
1d7c1841 1605 IoFLAGS(io) &= ~IOf_START;
4608196e 1606 do_open(PL_last_in_gv,"-",1,FALSE,O_RDONLY,0,NULL);
4bac9ae4 1607 SvTAINTED_off(GvSVn(PL_last_in_gv)); /* previous tainting irrelevant */
76f68e9b 1608 sv_setpvs(GvSVn(PL_last_in_gv), "-");
3280af22 1609 SvSETMAGIC(GvSV(PL_last_in_gv));
a2008d6d
GS
1610 fp = IoIFP(io);
1611 goto have_fp;
a0d0e21e
LW
1612 }
1613 }
3280af22 1614 fp = nextargv(PL_last_in_gv);
a0d0e21e 1615 if (!fp) { /* Note: fp != IoIFP(io) */
3280af22 1616 (void)do_close(PL_last_in_gv, FALSE); /* now it does*/
a0d0e21e
LW
1617 }
1618 }
0d44d22b
NC
1619 else if (type == OP_GLOB)
1620 fp = Perl_start_glob(aTHX_ POPs, io);
a0d0e21e
LW
1621 }
1622 else if (type == OP_GLOB)
1623 SP--;
7716c5c5 1624 else if (IoTYPE(io) == IoTYPE_WRONLY) {
a5390457 1625 report_wrongway_fh(PL_last_in_gv, '>');
a00b5bd3 1626 }
a0d0e21e
LW
1627 }
1628 if (!fp) {
041457d9
DM
1629 if ((!io || !(IoFLAGS(io) & IOf_START))
1630 && ckWARN2(WARN_GLOB, WARN_CLOSED))
1631 {
3f4520fe 1632 if (type == OP_GLOB)
63922903 1633 Perl_ck_warner_d(aTHX_ packWARN(WARN_GLOB),
af8c498a
GS
1634 "glob failed (can't start child: %s)",
1635 Strerror(errno));
69282e91 1636 else
831e4cc3 1637 report_evil_fh(PL_last_in_gv);
3f4520fe 1638 }
54310121 1639 if (gimme == G_SCALAR) {
79628082 1640 /* undef TARG, and push that undefined value */
ba92458f
AE
1641 if (type != OP_RCATLINE) {
1642 SV_CHECK_THINKFIRST_COW_DROP(TARG);
0c34ef67 1643 SvOK_off(TARG);
ba92458f 1644 }
a0d0e21e
LW
1645 PUSHTARG;
1646 }
1647 RETURN;
1648 }
a2008d6d 1649 have_fp:
54310121 1650 if (gimme == G_SCALAR) {
a0d0e21e 1651 sv = TARG;
0f722b55
RGS
1652 if (type == OP_RCATLINE && SvGMAGICAL(sv))
1653 mg_get(sv);
48de12d9
RGS
1654 if (SvROK(sv)) {
1655 if (type == OP_RCATLINE)
5668452f 1656 SvPV_force_nomg_nolen(sv);
48de12d9
RGS
1657 else
1658 sv_unref(sv);
1659 }
f7877b28 1660 else if (isGV_with_GP(sv)) {
5668452f 1661 SvPV_force_nomg_nolen(sv);
f7877b28 1662 }
862a34c6 1663 SvUPGRADE(sv, SVt_PV);
a0d0e21e 1664 tmplen = SvLEN(sv); /* remember if already alloced */
f72e8700
JJ
1665 if (!tmplen && !SvREADONLY(sv)) {
1666 /* try short-buffering it. Please update t/op/readline.t
1667 * if you change the growth length.
1668 */
1669 Sv_Grow(sv, 80);
1670 }
2b5e58c4
AMS
1671 offset = 0;
1672 if (type == OP_RCATLINE && SvOK(sv)) {
1673 if (!SvPOK(sv)) {
5668452f 1674 SvPV_force_nomg_nolen(sv);
2b5e58c4 1675 }
a0d0e21e 1676 offset = SvCUR(sv);
2b5e58c4 1677 }
a0d0e21e 1678 }
54310121 1679 else {
561b68a9 1680 sv = sv_2mortal(newSV(80));
54310121 1681 offset = 0;
1682 }
fbad3eb5 1683
3887d568
AP
1684 /* This should not be marked tainted if the fp is marked clean */
1685#define MAYBE_TAINT_LINE(io, sv) \
1686 if (!(IoFLAGS(io) & IOf_UNTAINT)) { \
1687 TAINT; \
1688 SvTAINTED_on(sv); \
1689 }
1690
684bef36 1691/* delay EOF state for a snarfed empty file */
fbad3eb5 1692#define SNARF_EOF(gimme,rs,io,sv) \
684bef36 1693 (gimme != G_SCALAR || SvCUR(sv) \
b9fee9ba 1694 || (IoFLAGS(io) & IOf_NOLINE) || !RsSNARF(rs))
fbad3eb5 1695
a0d0e21e 1696 for (;;) {
09e8efcc 1697 PUTBACK;
fbad3eb5 1698 if (!sv_gets(sv, fp, offset)
2d726892
TF
1699 && (type == OP_GLOB
1700 || SNARF_EOF(gimme, PL_rs, io, sv)
1701 || PerlIO_error(fp)))
fbad3eb5 1702 {
760ac839 1703 PerlIO_clearerr(fp);
a0d0e21e 1704 if (IoFLAGS(io) & IOf_ARGV) {
3280af22 1705 fp = nextargv(PL_last_in_gv);
a0d0e21e
LW
1706 if (fp)
1707 continue;
3280af22 1708 (void)do_close(PL_last_in_gv, FALSE);
a0d0e21e
LW
1709 }
1710 else if (type == OP_GLOB) {
a2a5de95
NC
1711 if (!do_close(PL_last_in_gv, FALSE)) {
1712 Perl_ck_warner(aTHX_ packWARN(WARN_GLOB),
1713 "glob failed (child exited with status %d%s)",
1714 (int)(STATUS_CURRENT >> 8),
1715 (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
4eb79ab5 1716 }
a0d0e21e 1717 }
54310121 1718 if (gimme == G_SCALAR) {
ba92458f
AE
1719 if (type != OP_RCATLINE) {
1720 SV_CHECK_THINKFIRST_COW_DROP(TARG);
0c34ef67 1721 SvOK_off(TARG);
ba92458f 1722 }
09e8efcc 1723 SPAGAIN;
a0d0e21e
LW
1724 PUSHTARG;
1725 }
3887d568 1726 MAYBE_TAINT_LINE(io, sv);
a0d0e21e
LW
1727 RETURN;
1728 }
3887d568 1729 MAYBE_TAINT_LINE(io, sv);
a0d0e21e 1730 IoLINES(io)++;
b9fee9ba 1731 IoFLAGS(io) |= IOf_NOLINE;
71be2cbc 1732 SvSETMAGIC(sv);
09e8efcc 1733 SPAGAIN;
a0d0e21e 1734 XPUSHs(sv);
a0d0e21e 1735 if (type == OP_GLOB) {
349d4f2f 1736 const char *t1;
a0d0e21e 1737
3280af22 1738 if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) {
6136c704 1739 char * const tmps = SvEND(sv) - 1;
aa07b2f6 1740 if (*tmps == *SvPVX_const(PL_rs)) {
c07a80fd 1741 *tmps = '\0';
b162af07 1742 SvCUR_set(sv, SvCUR(sv) - 1);
c07a80fd 1743 }
1744 }
349d4f2f 1745 for (t1 = SvPVX_const(sv); *t1; t1++)
937b2e03 1746 if (!isALNUMC(*t1) &&
349d4f2f 1747 strchr("$&*(){}[]'\";\\|?<>~`", *t1))
a0d0e21e 1748 break;
349d4f2f 1749 if (*t1 && PerlLIO_lstat(SvPVX_const(sv), &PL_statbuf) < 0) {
a0d0e21e
LW
1750 (void)POPs; /* Unmatched wildcard? Chuck it... */
1751 continue;
1752 }
2d79bf7f 1753 } else if (SvUTF8(sv)) { /* OP_READLINE, OP_RCATLINE */
d4c19fe8
AL
1754 if (ckWARN(WARN_UTF8)) {
1755 const U8 * const s = (const U8*)SvPVX_const(sv) + offset;
1756 const STRLEN len = SvCUR(sv) - offset;
1757 const U8 *f;
1758
1759 if (!is_utf8_string_loc(s, len, &f))
1760 /* Emulate :encoding(utf8) warning in the same case. */
1761 Perl_warner(aTHX_ packWARN(WARN_UTF8),
1762 "utf8 \"\\x%02X\" does not map to Unicode",
1763 f < (U8*)SvEND(sv) ? *f : 0);
1764 }
a0d0e21e 1765 }
54310121 1766 if (gimme == G_ARRAY) {
a0d0e21e 1767 if (SvLEN(sv) - SvCUR(sv) > 20) {
1da4ca5f 1768 SvPV_shrink_to_cur(sv);
a0d0e21e 1769 }
561b68a9 1770 sv = sv_2mortal(newSV(80));
a0d0e21e
LW
1771 continue;
1772 }
54310121 1773 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
a0d0e21e 1774 /* try to reclaim a bit of scalar space (only on 1st alloc) */
d5b5861b
NC
1775 const STRLEN new_len
1776 = SvCUR(sv) < 60 ? 80 : SvCUR(sv)+40; /* allow some slop */
1da4ca5f 1777 SvPV_renew(sv, new_len);
a0d0e21e
LW
1778 }
1779 RETURN;
1780 }
1781}
1782
a0d0e21e
LW
1783PP(pp_helem)
1784{
97aff369 1785 dVAR; dSP;
760ac839 1786 HE* he;
ae77835f 1787 SV **svp;
c445ea15 1788 SV * const keysv = POPs;
85fbaab2 1789 HV * const hv = MUTABLE_HV(POPs);
a3b680e6
AL
1790 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
1791 const U32 defer = PL_op->op_private & OPpLVAL_DEFER;
be6c24e0 1792 SV *sv;
92970b93 1793 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
d30e492c 1794 bool preeminent = TRUE;
a0d0e21e 1795
d4c19fe8 1796 if (SvTYPE(hv) != SVt_PVHV)
a0d0e21e 1797 RETPUSHUNDEF;
d4c19fe8 1798
92970b93 1799 if (localizing) {
d4c19fe8
AL
1800 MAGIC *mg;
1801 HV *stash;
d30e492c
VP
1802
1803 /* If we can determine whether the element exist,
1804 * Try to preserve the existenceness of a tied hash
1805 * element by using EXISTS and DELETE if possible.
1806 * Fallback to FETCH and STORE otherwise. */
2c5f48c2 1807 if (SvCANEXISTDELETE(hv))
d30e492c 1808 preeminent = hv_exists_ent(hv, keysv, 0);
d4c19fe8 1809 }
d30e492c 1810
5f9d7e2b 1811 he = hv_fetch_ent(hv, keysv, lval && !defer, 0);
d4c19fe8 1812 svp = he ? &HeVAL(he) : NULL;
a0d0e21e 1813 if (lval) {
746f6409 1814 if (!svp || !*svp || *svp == &PL_sv_undef) {
68dc0745 1815 SV* lv;
1816 SV* key2;
2d8e6c8d 1817 if (!defer) {
be2597df 1818 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
2d8e6c8d 1819 }
68dc0745 1820 lv = sv_newmortal();
1821 sv_upgrade(lv, SVt_PVLV);
1822 LvTYPE(lv) = 'y';
6136c704 1823 sv_magic(lv, key2 = newSVsv(keysv), PERL_MAGIC_defelem, NULL, 0);
68dc0745 1824 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
b37c2d43 1825 LvTARG(lv) = SvREFCNT_inc_simple(hv);
68dc0745 1826 LvTARGLEN(lv) = 1;
1827 PUSHs(lv);
1828 RETURN;
1829 }
92970b93 1830 if (localizing) {
bfcb3514 1831 if (HvNAME_get(hv) && isGV(*svp))
159b6efe 1832 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
47cfc530
VP
1833 else if (preeminent)
1834 save_helem_flags(hv, keysv, svp,
1835 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
1836 else
1837 SAVEHDELETE(hv, keysv);
5f05dabc 1838 }
9026059d
GG
1839 else if (PL_op->op_private & OPpDEREF) {
1840 PUSHs(vivify_ref(*svp, PL_op->op_private & OPpDEREF));
1841 RETURN;
1842 }
a0d0e21e 1843 }
746f6409 1844 sv = (svp && *svp ? *svp : &PL_sv_undef);
fd69380d
DM
1845 /* Originally this did a conditional C<sv = sv_mortalcopy(sv)>; this
1846 * was to make C<local $tied{foo} = $tied{foo}> possible.
1847 * However, it seems no longer to be needed for that purpose, and
1848 * introduced a new bug: stuff like C<while ($hash{taintedval} =~ /.../g>
1849 * would loop endlessly since the pos magic is getting set on the
1850 * mortal copy and lost. However, the copy has the effect of
1851 * triggering the get magic, and losing it altogether made things like
1852 * c<$tied{foo};> in void context no longer do get magic, which some
1853 * code relied on. Also, delayed triggering of magic on @+ and friends
1854 * meant the original regex may be out of scope by now. So as a
1855 * compromise, do the get magic here. (The MGf_GSKIP flag will stop it
1856 * being called too many times). */
39cf747a 1857 if (!lval && SvRMAGICAL(hv) && SvGMAGICAL(sv))
fd69380d 1858 mg_get(sv);
be6c24e0 1859 PUSHs(sv);
a0d0e21e
LW
1860 RETURN;
1861}
1862
a0d0e21e
LW
1863PP(pp_iter)
1864{
97aff369 1865 dVAR; dSP;
eb578fdb 1866 PERL_CONTEXT *cx;
dc09a129 1867 SV *sv, *oldsv;
1d7c1841 1868 SV **itersvp;
d01136d6
BS
1869 AV *av = NULL; /* used for LOOP_FOR on arrays and the stack */
1870 bool av_is_stack = FALSE;
a0d0e21e 1871
924508f0 1872 EXTEND(SP, 1);
a0d0e21e 1873 cx = &cxstack[cxstack_ix];
3b719c58 1874 if (!CxTYPE_is_LOOP(cx))
5637ef5b 1875 DIE(aTHX_ "panic: pp_iter, type=%u", CxTYPE(cx));
a0d0e21e 1876
1d7c1841 1877 itersvp = CxITERVAR(cx);
d01136d6 1878 if (CxTYPE(cx) == CXt_LOOP_LAZYSV) {
89ea2908 1879 /* string increment */
d01136d6
BS
1880 SV* cur = cx->blk_loop.state_u.lazysv.cur;
1881 SV *end = cx->blk_loop.state_u.lazysv.end;
267cc4a8
NC
1882 /* If the maximum is !SvOK(), pp_enteriter substitutes PL_sv_no.
1883 It has SvPVX of "" and SvCUR of 0, which is what we want. */
4fe3f0fa 1884 STRLEN maxlen = 0;
d01136d6 1885 const char *max = SvPV_const(end, maxlen);
89ea2908 1886 if (!SvNIOK(cur) && SvCUR(cur) <= maxlen) {
1d7c1841 1887 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
eaa5c2d6 1888 /* safe to reuse old SV */
1d7c1841 1889 sv_setsv(*itersvp, cur);
eaa5c2d6 1890 }
1c846c1f 1891 else
eaa5c2d6
GA
1892 {
1893 /* we need a fresh SV every time so that loop body sees a
1894 * completely new SV for closures/references to work as
1895 * they used to */
dc09a129 1896 oldsv = *itersvp;
1d7c1841 1897 *itersvp = newSVsv(cur);
dc09a129 1898 SvREFCNT_dec(oldsv);
eaa5c2d6 1899 }
aa07b2f6 1900 if (strEQ(SvPVX_const(cur), max))
89ea2908
GA
1901 sv_setiv(cur, 0); /* terminate next time */
1902 else
1903 sv_inc(cur);
1904 RETPUSHYES;
1905 }
1906 RETPUSHNO;
d01136d6
BS
1907 }
1908 else if (CxTYPE(cx) == CXt_LOOP_LAZYIV) {
89ea2908 1909 /* integer increment */
d01136d6 1910 if (cx->blk_loop.state_u.lazyiv.cur > cx->blk_loop.state_u.lazyiv.end)
89ea2908 1911 RETPUSHNO;
7f61b687 1912
3db8f154 1913 /* don't risk potential race */
1d7c1841 1914 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
eaa5c2d6 1915 /* safe to reuse old SV */
cdc1aa42 1916 sv_setiv(*itersvp, cx->blk_loop.state_u.lazyiv.cur);
eaa5c2d6 1917 }
1c846c1f 1918 else
eaa5c2d6
GA
1919 {
1920 /* we need a fresh SV every time so that loop body sees a
1921 * completely new SV for closures/references to work as they
1922 * used to */
dc09a129 1923 oldsv = *itersvp;
cdc1aa42 1924 *itersvp = newSViv(cx->blk_loop.state_u.lazyiv.cur);
dc09a129 1925 SvREFCNT_dec(oldsv);
eaa5c2d6 1926 }
a2309040 1927
cdc1aa42
NC
1928 if (cx->blk_loop.state_u.lazyiv.cur == IV_MAX) {
1929 /* Handle end of range at IV_MAX */
1930 cx->blk_loop.state_u.lazyiv.end = IV_MIN;
1931 } else
1932 ++cx->blk_loop.state_u.lazyiv.cur;
a2309040 1933
89ea2908
GA
1934 RETPUSHYES;
1935 }
1936
1937 /* iterate array */
d01136d6
BS
1938 assert(CxTYPE(cx) == CXt_LOOP_FOR);
1939 av = cx->blk_loop.state_u.ary.ary;
1940 if (!av) {
1941 av_is_stack = TRUE;
1942 av = PL_curstack;
1943 }
ef3e5ea9 1944 if (PL_op->op_private & OPpITER_REVERSED) {
d01136d6
BS
1945 if (cx->blk_loop.state_u.ary.ix <= (av_is_stack
1946 ? cx->blk_loop.resetsp + 1 : 0))
ef3e5ea9 1947 RETPUSHNO;
a0d0e21e 1948
ef3e5ea9 1949 if (SvMAGICAL(av) || AvREIFY(av)) {
d01136d6 1950 SV * const * const svp = av_fetch(av, --cx->blk_loop.state_u.ary.ix, FALSE);
a0714e2c 1951 sv = svp ? *svp : NULL;
ef3e5ea9
NC
1952 }
1953 else {
d01136d6 1954 sv = AvARRAY(av)[--cx->blk_loop.state_u.ary.ix];
ef3e5ea9 1955 }
d42935ef
JH
1956 }
1957 else {
d01136d6 1958 if (cx->blk_loop.state_u.ary.ix >= (av_is_stack ? cx->blk_oldsp :
ef3e5ea9
NC
1959 AvFILL(av)))
1960 RETPUSHNO;
1961
1962 if (SvMAGICAL(av) || AvREIFY(av)) {
d01136d6 1963 SV * const * const svp = av_fetch(av, ++cx->blk_loop.state_u.ary.ix, FALSE);
a0714e2c 1964 sv = svp ? *svp : NULL;
ef3e5ea9
NC
1965 }
1966 else {
d01136d6 1967 sv = AvARRAY(av)[++cx->blk_loop.state_u.ary.ix];
ef3e5ea9 1968 }
d42935ef 1969 }
ef3e5ea9 1970
0565a181 1971 if (sv && SvIS_FREED(sv)) {
a0714e2c 1972 *itersvp = NULL;
b6c83531 1973 Perl_croak(aTHX_ "Use of freed value in iteration");
cccede53
DM
1974 }
1975
d01136d6 1976 if (sv) {
a0d0e21e 1977 SvTEMP_off(sv);
d01136d6
BS
1978 SvREFCNT_inc_simple_void_NN(sv);
1979 }
a0d0e21e 1980 else
3280af22 1981 sv = &PL_sv_undef;
d01136d6
BS
1982 if (!av_is_stack && sv == &PL_sv_undef) {
1983 SV *lv = newSV_type(SVt_PVLV);
1984 LvTYPE(lv) = 'y';
1985 sv_magic(lv, NULL, PERL_MAGIC_defelem, NULL, 0);
b37c2d43 1986 LvTARG(lv) = SvREFCNT_inc_simple(av);
d01136d6 1987 LvTARGOFF(lv) = cx->blk_loop.state_u.ary.ix;
42718184 1988 LvTARGLEN(lv) = (STRLEN)UV_MAX;
d01136d6 1989 sv = lv;
5f05dabc 1990 }
a0d0e21e 1991
dc09a129 1992 oldsv = *itersvp;
d01136d6 1993 *itersvp = sv;
dc09a129
DM
1994 SvREFCNT_dec(oldsv);
1995
a0d0e21e
LW
1996 RETPUSHYES;
1997}
1998
ef07e810
DM
1999/*
2000A description of how taint works in pattern matching and substitution.
2001
284167a5
S
2002This is all conditional on NO_TAINT_SUPPORT not being defined. Under
2003NO_TAINT_SUPPORT, taint-related operations should become no-ops.
2004
4e19c54b 2005While the pattern is being assembled/concatenated and then compiled,
284167a5
S
2006PL_tainted will get set (via TAINT_set) if any component of the pattern
2007is tainted, e.g. /.*$tainted/. At the end of pattern compilation,
2008the RXf_TAINTED flag is set on the pattern if PL_tainted is set (via
2009TAINT_get).
ef07e810 2010
0ab462a6
DM
2011When the pattern is copied, e.g. $r = qr/..../, the SV holding the ref to
2012the pattern is marked as tainted. This means that subsequent usage, such
284167a5
S
2013as /x$r/, will set PL_tainted using TAINT_set, and thus RXf_TAINTED,
2014on the new pattern too.
ef07e810
DM
2015
2016During execution of a pattern, locale-variant ops such as ALNUML set the
2017local flag RF_tainted. At the end of execution, the engine sets the
0ab462a6
DM
2018RXf_TAINTED_SEEN on the pattern if RF_tainted got set, or clears it
2019otherwise.
ef07e810
DM
2020
2021In addition, RXf_TAINTED_SEEN is used post-execution by the get magic code
2022of $1 et al to indicate whether the returned value should be tainted.
2023It is the responsibility of the caller of the pattern (i.e. pp_match,
2024pp_subst etc) to set this flag for any other circumstances where $1 needs
2025to be tainted.
2026
2027The taint behaviour of pp_subst (and pp_substcont) is quite complex.
2028
2029There are three possible sources of taint
2030 * the source string
2031 * the pattern (both compile- and run-time, RXf_TAINTED / RXf_TAINTED_SEEN)
2032 * the replacement string (or expression under /e)
2033
2034There are four destinations of taint and they are affected by the sources
2035according to the rules below:
2036
2037 * the return value (not including /r):
2038 tainted by the source string and pattern, but only for the
2039 number-of-iterations case; boolean returns aren't tainted;
2040 * the modified string (or modified copy under /r):
2041 tainted by the source string, pattern, and replacement strings;
2042 * $1 et al:
2043 tainted by the pattern, and under 'use re "taint"', by the source
2044 string too;
2045 * PL_taint - i.e. whether subsequent code (e.g. in a /e block) is tainted:
2046 should always be unset before executing subsequent code.
2047
2048The overall action of pp_subst is:
2049
2050 * at the start, set bits in rxtainted indicating the taint status of
2051 the various sources.
2052
2053 * After each pattern execution, update the SUBST_TAINT_PAT bit in
2054 rxtainted if RXf_TAINTED_SEEN has been set, to indicate that the
2055 pattern has subsequently become tainted via locale ops.
2056
2057 * If control is being passed to pp_substcont to execute a /e block,
2058 save rxtainted in the CXt_SUBST block, for future use by
2059 pp_substcont.
2060
2061 * Whenever control is being returned to perl code (either by falling
2062 off the "end" of pp_subst/pp_substcont, or by entering a /e block),
2063 use the flag bits in rxtainted to make all the appropriate types of
0ab462a6
DM
2064 destination taint visible; e.g. set RXf_TAINTED_SEEN so that $1
2065 et al will appear tainted.
ef07e810
DM
2066
2067pp_match is just a simpler version of the above.
2068
2069*/
2070
a0d0e21e
LW
2071PP(pp_subst)
2072{
97aff369 2073 dVAR; dSP; dTARG;
eb578fdb 2074 PMOP *pm = cPMOP;
a0d0e21e 2075 PMOP *rpm = pm;
eb578fdb 2076 char *s;
a0d0e21e 2077 char *strend;
eb578fdb 2078 char *m;
5c144d81 2079 const char *c;
eb578fdb 2080 char *d;
a0d0e21e
LW
2081 STRLEN clen;
2082 I32 iters = 0;
2083 I32 maxiters;
eb578fdb 2084 I32 i;
a0d0e21e 2085 bool once;
ef07e810
DM
2086 U8 rxtainted = 0; /* holds various SUBST_TAINT_* flag bits.
2087 See "how taint works" above */
a0d0e21e 2088 char *orig;
1ed74d04 2089 U8 r_flags;
eb578fdb 2090 REGEXP *rx = PM_GETRE(pm);
a0d0e21e
LW
2091 STRLEN len;
2092 int force_on_match = 0;
0bcc34c2 2093 const I32 oldsave = PL_savestack_ix;
792b2c16 2094 STRLEN slen;
26a74523 2095 bool doutf8 = FALSE; /* whether replacement is in utf8 */
f8c7b90f 2096#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
2097 bool is_cow;
2098#endif
a0714e2c 2099 SV *nsv = NULL;
b770e143 2100 /* known replacement string? */
eb578fdb 2101 SV *dstr = (pm->op_pmflags & PMf_CONST) ? POPs : NULL;
a0d0e21e 2102
f410a211
NC
2103 PERL_ASYNC_CHECK();
2104
533c011a 2105 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e 2106 TARG = POPs;
59f00321
RGS
2107 else if (PL_op->op_private & OPpTARGET_MY)
2108 GETTARGET;
a0d0e21e 2109 else {
54b9620d 2110 TARG = DEFSV;
a0d0e21e 2111 EXTEND(SP,1);
1c846c1f 2112 }
d9f424b2 2113
64534138 2114 SvGETMAGIC(TARG); /* must come before cow check */
f8c7b90f 2115#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
2116 /* Awooga. Awooga. "bool" types that are actually char are dangerous,
2117 because they make integers such as 256 "false". */
2118 is_cow = SvIsCOW(TARG) ? TRUE : FALSE;
2119#else
765f542d
NC
2120 if (SvIsCOW(TARG))
2121 sv_force_normal_flags(TARG,0);
ed252734 2122#endif
8ca8a454 2123 if (!(rpm->op_pmflags & PMf_NONDESTRUCT)
f8c7b90f 2124#ifdef PERL_OLD_COPY_ON_WRITE
8ca8a454 2125 && !is_cow
ed252734 2126#endif
8ca8a454
NC
2127 && (SvREADONLY(TARG)
2128 || ( ((SvTYPE(TARG) == SVt_PVGV && isGV_with_GP(TARG))
2129 || SvTYPE(TARG) > SVt_PVLV)
2130 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG)))))
6ad8f254 2131 Perl_croak_no_modify(aTHX);
8ec5e241
NIS
2132 PUTBACK;
2133
64534138 2134 s = SvPV_nomg(TARG, len);
4499db73 2135 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV || SvVOK(TARG))
a0d0e21e 2136 force_on_match = 1;
20be6587
DM
2137
2138 /* only replace once? */
2139 once = !(rpm->op_pmflags & PMf_GLOBAL);
2140
ef07e810 2141 /* See "how taint works" above */
284167a5 2142 if (TAINTING_get) {
20be6587
DM
2143 rxtainted = (
2144 (SvTAINTED(TARG) ? SUBST_TAINT_STR : 0)
284167a5 2145 | (RX_ISTAINTED(rx) ? SUBST_TAINT_PAT : 0)
20be6587
DM
2146 | ((pm->op_pmflags & PMf_RETAINT) ? SUBST_TAINT_RETAINT : 0)
2147 | ((once && !(rpm->op_pmflags & PMf_NONDESTRUCT))
2148 ? SUBST_TAINT_BOOLRET : 0));
2149 TAINT_NOT;
2150 }
a12c0f56 2151
a30b2f1f 2152 RX_MATCH_UTF8_set(rx, DO_UTF8(TARG));
d9f424b2 2153
a0d0e21e
LW
2154 force_it:
2155 if (!pm || !s)
5637ef5b 2156 DIE(aTHX_ "panic: pp_subst, pm=%p, s=%p", pm, s);
a0d0e21e
LW
2157
2158 strend = s + len;
a30b2f1f 2159 slen = RX_MATCH_UTF8(rx) ? utf8_length((U8*)s, (U8*)strend) : len;
792b2c16
JH
2160 maxiters = 2 * slen + 10; /* We can match twice at each
2161 position, once with zero-length,
2162 second time with non-zero. */
a0d0e21e 2163
6a97c51d 2164 if (!RX_PRELEN(rx) && PL_curpm
8d919b0a 2165 && !ReANY(rx)->mother_re) {
3280af22 2166 pm = PL_curpm;
aaa362c4 2167 rx = PM_GETRE(pm);
a0d0e21e 2168 }
6502e081
DM
2169
2170 r_flags = ( RX_NPARENS(rx)
2171 || PL_sawampersand
6502e081
DM
2172 || (RX_EXTFLAGS(rx) & (RXf_EVAL_SEEN|RXf_PMf_KEEPCOPY))
2173 )
2174 ? REXEC_COPY_STR
2175 : 0;
7fba1cd6 2176
a0d0e21e 2177 orig = m = s;
07bc277f 2178 if (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) {
ee0b7718 2179 PL_bostr = orig;
f9f4320a 2180 s = CALLREG_INTUIT_START(rx, TARG, s, strend, r_flags, NULL);
f722798b
IZ
2181
2182 if (!s)
df34c13a 2183 goto ret_no;
f722798b 2184 /* How to do it in subst? */
07bc277f 2185/* if ( (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
1c846c1f 2186 && !PL_sawampersand
a91cc451 2187 && !(RX_EXTFLAGS(rx) & RXf_KEEPCOPY))
f722798b
IZ
2188 goto yup;
2189*/
a0d0e21e 2190 }
71be2cbc 2191
8b64c330
DM
2192 if (!CALLREGEXEC(rx, s, strend, orig, 0, TARG, NULL,
2193 r_flags | REXEC_CHECKED))
2194 {
5e79dfb9
DM
2195 ret_no:
2196 SPAGAIN;
2197 PUSHs(rpm->op_pmflags & PMf_NONDESTRUCT ? TARG : &PL_sv_no);
2198 LEAVE_SCOPE(oldsave);
2199 RETURN;
2200 }
2201
1754320d
FC
2202 PL_curpm = pm;
2203
71be2cbc 2204 /* known replacement string? */
f272994b 2205 if (dstr) {
8514a05a
JH
2206 /* replacement needing upgrading? */
2207 if (DO_UTF8(TARG) && !doutf8) {
db79b45b 2208 nsv = sv_newmortal();
4a176938 2209 SvSetSV(nsv, dstr);
8514a05a
JH
2210 if (PL_encoding)
2211 sv_recode_to_utf8(nsv, PL_encoding);
2212 else
2213 sv_utf8_upgrade(nsv);
5c144d81 2214 c = SvPV_const(nsv, clen);
4a176938
JH
2215 doutf8 = TRUE;
2216 }
2217 else {
5c144d81 2218 c = SvPV_const(dstr, clen);
4a176938 2219 doutf8 = DO_UTF8(dstr);
8514a05a 2220 }
bb933b9b
FC
2221
2222 if (SvTAINTED(dstr))
2223 rxtainted |= SUBST_TAINT_REPL;
f272994b
A
2224 }
2225 else {
6136c704 2226 c = NULL;
f272994b
A
2227 doutf8 = FALSE;
2228 }
2229
71be2cbc 2230 /* can do inplace substitution? */
ed252734 2231 if (c
f8c7b90f 2232#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
2233 && !is_cow
2234#endif
fbfb1899
DM
2235 && (I32)clen <= RX_MINLENRET(rx)
2236 && (once || !(r_flags & REXEC_COPY_STR))
1754320d 2237 && !(RX_EXTFLAGS(rx) & (RXf_LOOKBEHIND_SEEN|RXf_MODIFIES_VARS))
8ca8a454
NC
2238 && (!doutf8 || SvUTF8(TARG))
2239 && !(rpm->op_pmflags & PMf_NONDESTRUCT))
8b030b38 2240 {
ec911639 2241
f8c7b90f 2242#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
2243 if (SvIsCOW(TARG)) {
2244 assert (!force_on_match);
2245 goto have_a_cow;
2246 }
2247#endif
71be2cbc 2248 if (force_on_match) {
2249 force_on_match = 0;
5c1648b0 2250 s = SvPV_force_nomg(TARG, len);
71be2cbc 2251 goto force_it;
2252 }
71be2cbc 2253 d = s;
71be2cbc 2254 if (once) {
20be6587
DM
2255 if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
2256 rxtainted |= SUBST_TAINT_PAT;
07bc277f
NC
2257 m = orig + RX_OFFS(rx)[0].start;
2258 d = orig + RX_OFFS(rx)[0].end;
71be2cbc 2259 s = orig;
2260 if (m - s > strend - d) { /* faster to shorten from end */
2261 if (clen) {
2262 Copy(c, m, clen, char);
2263 m += clen;
a0d0e21e 2264 }
71be2cbc 2265 i = strend - d;
2266 if (i > 0) {
2267 Move(d, m, i, char);
2268 m += i;
a0d0e21e 2269 }
71be2cbc 2270 *m = '\0';
2271 SvCUR_set(TARG, m - s);
2272 }
155aba94 2273 else if ((i = m - s)) { /* faster from front */
71be2cbc 2274 d -= clen;
2275 m = d;
0d3c21b0 2276 Move(s, d - i, i, char);
71be2cbc 2277 sv_chop(TARG, d-i);
71be2cbc 2278 if (clen)
2279 Copy(c, m, clen, char);
2280 }
2281 else if (clen) {
2282 d -= clen;
2283 sv_chop(TARG, d);
2284 Copy(c, d, clen, char);
2285 }
2286 else {
2287 sv_chop(TARG, d);
2288 }
8ec5e241 2289 SPAGAIN;
8ca8a454 2290 PUSHs(&PL_sv_yes);
71be2cbc 2291 }
2292 else {
71be2cbc 2293 do {
2294 if (iters++ > maxiters)
cea2e8a9 2295 DIE(aTHX_ "Substitution loop");
20be6587
DM
2296 if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
2297 rxtainted |= SUBST_TAINT_PAT;
07bc277f 2298 m = RX_OFFS(rx)[0].start + orig;
155aba94 2299 if ((i = m - s)) {
71be2cbc 2300 if (s != d)
2301 Move(s, d, i, char);
2302 d += i;
a0d0e21e 2303 }
71be2cbc 2304 if (clen) {
2305 Copy(c, d, clen, char);
2306 d += clen;
2307 }
07bc277f 2308 s = RX_OFFS(rx)[0].end + orig;
f9f4320a 2309 } while (CALLREGEXEC(rx, s, strend, orig, s == m,
f722798b
IZ
2310 TARG, NULL,
2311 /* don't match same null twice */
2312 REXEC_NOT_FIRST|REXEC_IGNOREPOS));
71be2cbc 2313 if (s != d) {
2314 i = strend - s;
aa07b2f6 2315 SvCUR_set(TARG, d - SvPVX_const(TARG) + i);
71be2cbc 2316 Move(s, d, i+1, char); /* include the NUL */
a0d0e21e 2317 }
8ec5e241 2318 SPAGAIN;
8ca8a454 2319 mPUSHi((I32)iters);
a0d0e21e
LW
2320 }
2321 }
ff6e92e8 2322 else {
1754320d
FC
2323 bool first;
2324 SV *repl;
a0d0e21e
LW
2325 if (force_on_match) {
2326 force_on_match = 0;
0c1438a1
NC
2327 if (rpm->op_pmflags & PMf_NONDESTRUCT) {
2328 /* I feel that it should be possible to avoid this mortal copy
2329 given that the code below copies into a new destination.
2330 However, I suspect it isn't worth the complexity of
2331 unravelling the C<goto force_it> for the small number of
2332 cases where it would be viable to drop into the copy code. */
2333 TARG = sv_2mortal(newSVsv(TARG));
2334 }
5c1648b0 2335 s = SvPV_force_nomg(TARG, len);
a0d0e21e
LW
2336 goto force_it;
2337 }
f8c7b90f 2338#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
2339 have_a_cow:
2340#endif
20be6587
DM
2341 if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
2342 rxtainted |= SUBST_TAINT_PAT;
1754320d 2343 repl = dstr;
815dd406 2344 dstr = newSVpvn_flags(m, s-m, SVs_TEMP | (DO_UTF8(TARG) ? SVf_UTF8 : 0));
a0d0e21e 2345 if (!c) {
eb578fdb 2346 PERL_CONTEXT *cx;
8ec5e241 2347 SPAGAIN;
20be6587
DM
2348 /* note that a whole bunch of local vars are saved here for
2349 * use by pp_substcont: here's a list of them in case you're
2350 * searching for places in this sub that uses a particular var:
2351 * iters maxiters r_flags oldsave rxtainted orig dstr targ
2352 * s m strend rx once */
a0d0e21e 2353 PUSHSUBST(cx);
20e98b0f 2354 RETURNOP(cPMOP->op_pmreplrootu.op_pmreplroot);
a0d0e21e 2355 }
cf93c79d 2356 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
1754320d 2357 first = TRUE;
a0d0e21e
LW
2358 do {
2359 if (iters++ > maxiters)
cea2e8a9 2360 DIE(aTHX_ "Substitution loop");
20be6587
DM
2361 if (RX_MATCH_TAINTED(rx))
2362 rxtainted |= SUBST_TAINT_PAT;
07bc277f 2363 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
a0d0e21e
LW
2364 m = s;
2365 s = orig;
6502e081 2366 assert(RX_SUBOFFSET(rx) == 0);
07bc277f 2367 orig = RX_SUBBEG(rx);
a0d0e21e
LW
2368 s = orig + (m - s);
2369 strend = s + (strend - m);
2370 }
07bc277f 2371 m = RX_OFFS(rx)[0].start + orig;
64534138 2372 sv_catpvn_nomg_maybeutf8(dstr, s, m - s, DO_UTF8(TARG));
07bc277f 2373 s = RX_OFFS(rx)[0].end + orig;
1754320d
FC
2374 if (first) {
2375 /* replacement already stringified */
2376 if (clen)
64534138 2377 sv_catpvn_nomg_maybeutf8(dstr, c, clen, doutf8);
1754320d
FC
2378 first = FALSE;
2379 }
2380 else {
1754320d
FC
2381 if (PL_encoding) {
2382 if (!nsv) nsv = sv_newmortal();
2383 sv_copypv(nsv, repl);
2384 if (!DO_UTF8(nsv)) sv_recode_to_utf8(nsv, PL_encoding);
2385 sv_catsv(dstr, nsv);
2386 }
2387 else sv_catsv(dstr, repl);
bb933b9b
FC
2388 if (SvTAINTED(repl))
2389 rxtainted |= SUBST_TAINT_REPL;
1754320d 2390 }
a0d0e21e
LW
2391 if (once)
2392 break;
f9f4320a 2393 } while (CALLREGEXEC(rx, s, strend, orig, s == m,
ffc61ed2 2394 TARG, NULL, r_flags));
64534138 2395 sv_catpvn_nomg_maybeutf8(dstr, s, strend - s, DO_UTF8(TARG));
748a9306 2396
8ca8a454
NC
2397 if (rpm->op_pmflags & PMf_NONDESTRUCT) {
2398 /* From here on down we're using the copy, and leaving the original
2399 untouched. */
2400 TARG = dstr;
2401 SPAGAIN;
2402 PUSHs(dstr);
2403 } else {
f8c7b90f 2404#ifdef PERL_OLD_COPY_ON_WRITE
8ca8a454
NC
2405 /* The match may make the string COW. If so, brilliant, because
2406 that's just saved us one malloc, copy and free - the regexp has
2407 donated the old buffer, and we malloc an entirely new one, rather
2408 than the regexp malloc()ing a buffer and copying our original,
2409 only for us to throw it away here during the substitution. */
2410 if (SvIsCOW(TARG)) {
2411 sv_force_normal_flags(TARG, SV_COW_DROP_PV);
2412 } else
ed252734 2413#endif
8ca8a454
NC
2414 {
2415 SvPV_free(TARG);
2416 }
2417 SvPV_set(TARG, SvPVX(dstr));
2418 SvCUR_set(TARG, SvCUR(dstr));
2419 SvLEN_set(TARG, SvLEN(dstr));
64534138 2420 SvFLAGS(TARG) |= SvUTF8(dstr);
8ca8a454 2421 SvPV_set(dstr, NULL);
748a9306 2422
8ca8a454 2423 SPAGAIN;
4f4d7508 2424 mPUSHi((I32)iters);
8ca8a454
NC
2425 }
2426 }
2427
2428 if (!(rpm->op_pmflags & PMf_NONDESTRUCT)) {
2429 (void)SvPOK_only_UTF8(TARG);
a0d0e21e 2430 }
20be6587 2431
ef07e810 2432 /* See "how taint works" above */
284167a5 2433 if (TAINTING_get) {
20be6587
DM
2434 if ((rxtainted & SUBST_TAINT_PAT) ||
2435 ((rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) ==
2436 (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
2437 )
2438 (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */
2439
2440 if (!(rxtainted & SUBST_TAINT_BOOLRET)
2441 && (rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT))
2442 )
2443 SvTAINTED_on(TOPs); /* taint return value */
2444 else
2445 SvTAINTED_off(TOPs); /* may have got tainted earlier */
2446
2447 /* needed for mg_set below */
284167a5
S
2448 TAINT_set(
2449 cBOOL(rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL))
2450 );
20be6587
DM
2451 SvTAINT(TARG);
2452 }
2453 SvSETMAGIC(TARG); /* PL_tainted must be correctly set for this mg_set */
2454 TAINT_NOT;
f1a76097
DM
2455 LEAVE_SCOPE(oldsave);
2456 RETURN;
a0d0e21e
LW
2457}
2458
2459PP(pp_grepwhile)
2460{
27da23d5 2461 dVAR; dSP;
a0d0e21e
LW
2462
2463 if (SvTRUEx(POPs))
3280af22
NIS
2464 PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr];
2465 ++*PL_markstack_ptr;
b2a2a901 2466 FREETMPS;
d343c3ef 2467 LEAVE_with_name("grep_item"); /* exit inner scope */
a0d0e21e
LW
2468
2469 /* All done yet? */
3280af22 2470 if (PL_stack_base + *PL_markstack_ptr > SP) {
a0d0e21e 2471 I32 items;
c4420975 2472 const I32 gimme = GIMME_V;
a0d0e21e 2473
d343c3ef 2474 LEAVE_with_name("grep"); /* exit outer scope */
a0d0e21e 2475 (void)POPMARK; /* pop src */
3280af22 2476 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
a0d0e21e 2477 (void)POPMARK; /* pop dst */
3280af22 2478 SP = PL_stack_base + POPMARK; /* pop original mark */
54310121 2479 if (gimme == G_SCALAR) {
7cc47870 2480 if (PL_op->op_private & OPpGREP_LEX) {
c4420975 2481 SV* const sv = sv_newmortal();
7cc47870
RGS
2482 sv_setiv(sv, items);
2483 PUSHs(sv);
2484 }
2485 else {
2486 dTARGET;
2487 XPUSHi(items);
2488 }
a0d0e21e 2489 }
54310121 2490 else if (gimme == G_ARRAY)
2491 SP += items;
a0d0e21e
LW
2492 RETURN;
2493 }
2494 else {
2495 SV *src;
2496
d343c3ef 2497 ENTER_with_name("grep_item"); /* enter inner scope */
1d7c1841 2498 SAVEVPTR(PL_curpm);
a0d0e21e 2499
3280af22 2500 src = PL_stack_base[*PL_markstack_ptr];
a0d0e21e 2501 SvTEMP_off(src);
59f00321
RGS
2502 if (PL_op->op_private & OPpGREP_LEX)
2503 PAD_SVl(PL_op->op_targ) = src;
2504 else
414bf5ae 2505 DEFSV_set(src);
a0d0e21e
LW
2506
2507 RETURNOP(cLOGOP->op_other);
2508 }
2509}
2510
2511PP(pp_leavesub)
2512{
27da23d5 2513 dVAR; dSP;
a0d0e21e
LW
2514 SV **mark;
2515 SV **newsp;
2516 PMOP *newpm;
2517 I32 gimme;
eb578fdb 2518 PERL_CONTEXT *cx;
b0d9ce38 2519 SV *sv;
a0d0e21e 2520
9850bf21
RH
2521 if (CxMULTICALL(&cxstack[cxstack_ix]))
2522 return 0;
2523
a0d0e21e 2524 POPBLOCK(cx,newpm);
5dd42e15 2525 cxstack_ix++; /* temporarily protect top context */
1c846c1f 2526
a1f49e72 2527 TAINT_NOT;
a0d0e21e
LW
2528 if (gimme == G_SCALAR) {
2529 MARK = newsp + 1;
a29cdaf0 2530 if (MARK <= SP) {
a8bba7fa 2531 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
6f48390a
FC
2532 if (SvTEMP(TOPs) && SvREFCNT(TOPs) == 1
2533 && !SvMAGICAL(TOPs)) {
a29cdaf0
IZ
2534 *MARK = SvREFCNT_inc(TOPs);
2535 FREETMPS;
2536 sv_2mortal(*MARK);
cd06dffe
GS
2537 }
2538 else {
959e3673 2539 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
a29cdaf0 2540 FREETMPS;
959e3673
GS
2541 *MARK = sv_mortalcopy(sv);
2542 SvREFCNT_dec(sv);
a29cdaf0 2543 }
cd06dffe 2544 }
6f48390a
FC
2545 else if (SvTEMP(TOPs) && SvREFCNT(TOPs) == 1
2546 && !SvMAGICAL(TOPs)) {
767eda44 2547 *MARK = TOPs;
767eda44 2548 }
cd06dffe 2549 else
767eda44 2550 *MARK = sv_mortalcopy(TOPs);
cd06dffe
GS
2551 }
2552 else {
f86702cc 2553 MEXTEND(MARK, 0);
3280af22 2554 *MARK = &PL_sv_undef;
a0d0e21e
LW
2555 }
2556 SP = MARK;
2557 }
54310121 2558 else if (gimme == G_ARRAY) {
f86702cc 2559 for (MARK = newsp + 1; MARK <= SP; MARK++) {
6f48390a
FC
2560 if (!SvTEMP(*MARK) || SvREFCNT(*MARK) != 1
2561 || SvMAGICAL(*MARK)) {
f86702cc 2562 *MARK = sv_mortalcopy(*MARK);
a1f49e72
CS
2563 TAINT_NOT; /* Each item is independent */
2564 }
f86702cc 2565 }
a0d0e21e 2566 }
f86702cc 2567 PUTBACK;
1c846c1f 2568
a57c6685 2569 LEAVE;
5dd42e15 2570 cxstack_ix--;
b0d9ce38 2571 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
3280af22 2572 PL_curpm = newpm; /* ... and pop $1 et al */
a0d0e21e 2573
b0d9ce38 2574 LEAVESUB(sv);
f39bc417 2575 return cx->blk_sub.retop;
a0d0e21e
LW
2576}
2577
2578PP(pp_entersub)
2579{
27da23d5 2580 dVAR; dSP; dPOPss;
a0d0e21e 2581 GV *gv;
eb578fdb
KW
2582 CV *cv;
2583 PERL_CONTEXT *cx;
5d94fbed 2584 I32 gimme;
a9c4fd4e 2585 const bool hasargs = (PL_op->op_flags & OPf_STACKED) != 0;
a0d0e21e
LW
2586
2587 if (!sv)
cea2e8a9 2588 DIE(aTHX_ "Not a CODE reference");
a0d0e21e 2589 switch (SvTYPE(sv)) {
f1025168
NC
2590 /* This is overwhelming the most common case: */
2591 case SVt_PVGV:
13be902c 2592 we_have_a_glob:
159b6efe 2593 if (!(cv = GvCVu((const GV *)sv))) {
f730a42d 2594 HV *stash;
f2c0649b 2595 cv = sv_2cv(sv, &stash, &gv, 0);
f730a42d 2596 }
f1025168 2597 if (!cv) {
a57c6685 2598 ENTER;
f1025168
NC
2599 SAVETMPS;
2600 goto try_autoload;
2601 }
2602 break;
13be902c
FC
2603 case SVt_PVLV:
2604 if(isGV_with_GP(sv)) goto we_have_a_glob;
2605 /*FALLTHROUGH*/
a0d0e21e 2606 default:
7c75014e
DM
2607 if (sv == &PL_sv_yes) { /* unfound import, ignore */
2608 if (hasargs)
2609 SP = PL_stack_base + POPMARK;
4d198de3
DM
2610 else
2611 (void)POPMARK;
7c75014e
DM
2612 RETURN;
2613 }
2614 SvGETMAGIC(sv);
2615 if (SvROK(sv)) {
93d7320b
DM
2616 if (SvAMAGIC(sv)) {
2617 sv = amagic_deref_call(sv, to_cv_amg);
2618 /* Don't SPAGAIN here. */
2619 }
7c75014e
DM
2620 }
2621 else {
a9c4fd4e 2622 const char *sym;
780a5241 2623 STRLEN len;
79a3e5ea 2624 if (!SvOK(sv))
cea2e8a9 2625 DIE(aTHX_ PL_no_usym, "a subroutine");
79a3e5ea 2626 sym = SvPV_nomg_const(sv, len);
533c011a 2627 if (PL_op->op_private & HINT_STRICT_REFS)
b375e37b 2628 DIE(aTHX_ "Can't use string (\"%" SVf32 "\"%s) as a subroutine ref while \"strict refs\" in use", sv, len>32 ? "..." : "");
780a5241 2629 cv = get_cvn_flags(sym, len, GV_ADD|SvUTF8(sv));
a0d0e21e
LW
2630 break;
2631 }
ea726b52 2632 cv = MUTABLE_CV(SvRV(sv));
a0d0e21e
LW
2633 if (SvTYPE(cv) == SVt_PVCV)
2634 break;
2635 /* FALL THROUGH */
2636 case SVt_PVHV:
2637 case SVt_PVAV:
cea2e8a9 2638 DIE(aTHX_ "Not a CODE reference");
f1025168 2639 /* This is the second most common case: */
a0d0e21e 2640 case SVt_PVCV:
ea726b52 2641 cv = MUTABLE_CV(sv);
a0d0e21e 2642 break;
a0d0e21e
LW
2643 }
2644
a57c6685 2645 ENTER;
a0d0e21e
LW
2646 SAVETMPS;
2647
2648 retry:
541ed3a9
FC
2649 if (CvCLONE(cv) && ! CvCLONED(cv))
2650 DIE(aTHX_ "Closure prototype called");
a0d0e21e 2651 if (!CvROOT(cv) && !CvXSUB(cv)) {
2f349aa0
NC
2652 GV* autogv;
2653 SV* sub_name;
2654
2655 /* anonymous or undef'd function leaves us no recourse */
7d2057d8
FC
2656 if (CvANON(cv) || !(gv = CvGV(cv))) {
2657 if (CvNAMED(cv))
2658 DIE(aTHX_ "Undefined subroutine &%"HEKf" called",
2659 HEKfARG(CvNAME_HEK(cv)));
2f349aa0 2660 DIE(aTHX_ "Undefined subroutine called");
7d2057d8 2661 }
2f349aa0
NC
2662
2663 /* autoloaded stub? */
2664 if (cv != GvCV(gv)) {
2665 cv = GvCV(gv);
2666 }
2667 /* should call AUTOLOAD now? */
2668 else {
7e623da3 2669try_autoload:
d1089224
BF
2670 if ((autogv = gv_autoload_pvn(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
2671 GvNAMEUTF8(gv) ? SVf_UTF8 : 0)))
2f349aa0
NC
2672 {
2673 cv = GvCV(autogv);
2674 }
2f349aa0 2675 else {
c220e1a1 2676 sorry:
2f349aa0 2677 sub_name = sv_newmortal();
6136c704 2678 gv_efullname3(sub_name, gv, NULL);
be2597df 2679 DIE(aTHX_ "Undefined subroutine &%"SVf" called", SVfARG(sub_name));
2f349aa0
NC
2680 }
2681 }
2682 if (!cv)
c220e1a1 2683 goto sorry;
2f349aa0 2684 goto retry;
a0d0e21e
LW
2685 }
2686
54310121 2687 gimme = GIMME_V;
67caa1fe 2688 if ((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub) && !CvNODEBUG(cv)) {
005a8a35 2689 Perl_get_db_sub(aTHX_ &sv, cv);
a9ef256d
NC
2690 if (CvISXSUB(cv))
2691 PL_curcopdb = PL_curcop;
1ad62f64
BR
2692 if (CvLVALUE(cv)) {
2693 /* check for lsub that handles lvalue subroutines */
ae5c1e95 2694 cv = GvCV(gv_HVadd(gv_fetchpvs("DB::lsub", GV_ADDMULTI, SVt_PVHV)));
1ad62f64
BR
2695 /* if lsub not found then fall back to DB::sub */
2696 if (!cv) cv = GvCV(PL_DBsub);
2697 } else {
2698 cv = GvCV(PL_DBsub);
2699 }
a9ef256d 2700
ccafdc96
RGS
2701 if (!cv || (!CvXSUB(cv) && !CvSTART(cv)))
2702 DIE(aTHX_ "No DB::sub routine defined");
67caa1fe 2703 }
a0d0e21e 2704
aed2304a 2705 if (!(CvISXSUB(cv))) {
f1025168 2706 /* This path taken at least 75% of the time */
a0d0e21e 2707 dMARK;
eb578fdb 2708 I32 items = SP - MARK;
b70d5558 2709 PADLIST * const padlist = CvPADLIST(cv);
a0d0e21e
LW
2710 PUSHBLOCK(cx, CXt_SUB, MARK);
2711 PUSHSUB(cx);
f39bc417 2712 cx->blk_sub.retop = PL_op->op_next;
a0d0e21e 2713 CvDEPTH(cv)++;
3a76ca88
RGS
2714 if (CvDEPTH(cv) >= 2) {
2715 PERL_STACK_OVERFLOW_CHECK();
2716 pad_push(padlist, CvDEPTH(cv));
a0d0e21e 2717 }
3a76ca88
RGS
2718 SAVECOMPPAD();
2719 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
2720 if (hasargs) {
10533ace 2721 AV *const av = MUTABLE_AV(PAD_SVl(0));
221373f0
GS
2722 if (AvREAL(av)) {
2723 /* @_ is normally not REAL--this should only ever
2724 * happen when DB::sub() calls things that modify @_ */
2725 av_clear(av);
2726 AvREAL_off(av);
2727 AvREIFY_on(av);
2728 }
3280af22 2729 cx->blk_sub.savearray = GvAV(PL_defgv);
502c6561 2730 GvAV(PL_defgv) = MUTABLE_AV(SvREFCNT_inc_simple(av));
dd2155a4 2731 CX_CURPAD_SAVE(cx->blk_sub);
6d4ff0d2 2732 cx->blk_sub.argarray = av;
a0d0e21e
LW
2733 ++MARK;
2734
2735 if (items > AvMAX(av) + 1) {
504618e9 2736 SV **ary = AvALLOC(av);
a0d0e21e
LW
2737 if (AvARRAY(av) != ary) {
2738 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
9c6bc640 2739 AvARRAY(av) = ary;
a0d0e21e
LW
2740 }
2741 if (items > AvMAX(av) + 1) {
2742 AvMAX(av) = items - 1;
2743 Renew(ary,items,SV*);
2744 AvALLOC(av) = ary;
9c6bc640 2745 AvARRAY(av) = ary;
a0d0e21e
LW
2746 }
2747 }
2748 Copy(MARK,AvARRAY(av),items,SV*);
93965878 2749 AvFILLp(av) = items - 1;
1c846c1f 2750
a0d0e21e
LW
2751 while (items--) {
2752 if (*MARK)
2753 SvTEMP_off(*MARK);
2754 MARK++;
2755 }
2756 }
da1dff94
FC
2757 if ((cx->blk_u16 & OPpENTERSUB_LVAL_MASK) == OPpLVAL_INTRO &&
2758 !CvLVALUE(cv))
2759 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
4a925ff6
GS
2760 /* warning must come *after* we fully set up the context
2761 * stuff so that __WARN__ handlers can safely dounwind()
2762 * if they want to
2763 */
2b9dff67 2764 if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION)
4a925ff6
GS
2765 && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))
2766 sub_crush_depth(cv);
a0d0e21e
LW
2767 RETURNOP(CvSTART(cv));
2768 }
f1025168 2769 else {
3a76ca88 2770 I32 markix = TOPMARK;
f1025168 2771
3a76ca88 2772 PUTBACK;
f1025168 2773
3a76ca88
RGS
2774 if (!hasargs) {
2775 /* Need to copy @_ to stack. Alternative may be to
2776 * switch stack to @_, and copy return values
2777 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
2778 AV * const av = GvAV(PL_defgv);
2779 const I32 items = AvFILLp(av) + 1; /* @_ is not tieable */
2780
2781 if (items) {
2782 /* Mark is at the end of the stack. */
2783 EXTEND(SP, items);
2784 Copy(AvARRAY(av), SP + 1, items, SV*);
2785 SP += items;
2786 PUTBACK ;
2787 }
2788 }
2789 /* We assume first XSUB in &DB::sub is the called one. */
2790 if (PL_curcopdb) {
2791 SAVEVPTR(PL_curcop);
2792 PL_curcop = PL_curcopdb;
2793 PL_curcopdb = NULL;
2794 }
2795 /* Do we need to open block here? XXXX */
72df79cf
GF
2796
2797 /* CvXSUB(cv) must not be NULL because newXS() refuses NULL xsub address */
2798 assert(CvXSUB(cv));
16c91539 2799 CvXSUB(cv)(aTHX_ cv);
3a76ca88
RGS
2800
2801 /* Enforce some sanity in scalar context. */
2802 if (gimme == G_SCALAR && ++markix != PL_stack_sp - PL_stack_base ) {
2803 if (markix > PL_stack_sp - PL_stack_base)
2804 *(PL_stack_base + markix) = &PL_sv_undef;
2805 else
2806 *(PL_stack_base + markix) = *PL_stack_sp;
2807 PL_stack_sp = PL_stack_base + markix;
2808 }
a57c6685 2809 LEAVE;
f1025168
NC
2810 return NORMAL;
2811 }
a0d0e21e
LW
2812}
2813
44a8e56a 2814void
864dbfa3 2815Perl_sub_crush_depth(pTHX_ CV *cv)
44a8e56a 2816{
7918f24d
NC
2817 PERL_ARGS_ASSERT_SUB_CRUSH_DEPTH;
2818
44a8e56a 2819 if (CvANON(cv))
9014280d 2820 Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on anonymous subroutine");
44a8e56a 2821 else {
aec46f14 2822 SV* const tmpstr = sv_newmortal();
6136c704 2823 gv_efullname3(tmpstr, CvGV(cv), NULL);
35c1215d 2824 Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on subroutine \"%"SVf"\"",
be2597df 2825 SVfARG(tmpstr));
44a8e56a 2826 }
2827}
2828
a0d0e21e
LW
2829PP(pp_aelem)
2830{
97aff369 2831 dVAR; dSP;
a0d0e21e 2832 SV** svp;
a3b680e6 2833 SV* const elemsv = POPs;
d804643f 2834 IV elem = SvIV(elemsv);
502c6561 2835 AV *const av = MUTABLE_AV(POPs);
e1ec3a88
AL
2836 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
2837 const U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > av_len(av));
4ad10a0b
VP
2838 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
2839 bool preeminent = TRUE;
be6c24e0 2840 SV *sv;
a0d0e21e 2841
e35c1634 2842 if (SvROK(elemsv) && !SvGAMAGIC(elemsv) && ckWARN(WARN_MISC))
95b63a38
JH
2843 Perl_warner(aTHX_ packWARN(WARN_MISC),
2844 "Use of reference \"%"SVf"\" as array index",
be2597df 2845 SVfARG(elemsv));
a0d0e21e
LW
2846 if (SvTYPE(av) != SVt_PVAV)
2847 RETPUSHUNDEF;
4ad10a0b
VP
2848
2849 if (localizing) {
2850 MAGIC *mg;
2851 HV *stash;
2852
2853 /* If we can determine whether the element exist,
2854 * Try to preserve the existenceness of a tied array
2855 * element by using EXISTS and DELETE if possible.
2856 * Fallback to FETCH and STORE otherwise. */
2857 if (SvCANEXISTDELETE(av))
2858 preeminent = av_exists(av, elem);
2859 }
2860
68dc0745 2861 svp = av_fetch(av, elem, lval && !defer);
a0d0e21e 2862 if (lval) {
2b573ace 2863#ifdef PERL_MALLOC_WRAP
2b573ace 2864 if (SvUOK(elemsv)) {
a9c4fd4e 2865 const UV uv = SvUV(elemsv);
2b573ace
JH
2866 elem = uv > IV_MAX ? IV_MAX : uv;
2867 }
2868 else if (SvNOK(elemsv))
2869 elem = (IV)SvNV(elemsv);
a3b680e6
AL
2870 if (elem > 0) {
2871 static const char oom_array_extend[] =
2872 "Out of memory during array extend"; /* Duplicated in av.c */
2b573ace 2873 MEM_WRAP_CHECK_1(elem,SV*,oom_array_extend);
a3b680e6 2874 }
2b573ace 2875#endif
3280af22 2876 if (!svp || *svp == &PL_sv_undef) {
68dc0745 2877 SV* lv;
2878 if (!defer)
cea2e8a9 2879 DIE(aTHX_ PL_no_aelem, elem);
68dc0745 2880 lv = sv_newmortal();
2881 sv_upgrade(lv, SVt_PVLV);
2882 LvTYPE(lv) = 'y';
a0714e2c 2883 sv_magic(lv, NULL, PERL_MAGIC_defelem, NULL, 0);
b37c2d43 2884 LvTARG(lv) = SvREFCNT_inc_simple(av);
68dc0745 2885 LvTARGOFF(lv) = elem;
2886 LvTARGLEN(lv) = 1;
2887 PUSHs(lv);
2888 RETURN;
2889 }
4ad10a0b
VP
2890 if (localizing) {
2891 if (preeminent)
2892 save_aelem(av, elem, svp);
2893 else
2894 SAVEADELETE(av, elem);
2895 }
9026059d
GG
2896 else if (PL_op->op_private & OPpDEREF) {
2897 PUSHs(vivify_ref(*svp, PL_op->op_private & OPpDEREF));
2898 RETURN;
2899 }
a0d0e21e 2900 }
3280af22 2901 sv = (svp ? *svp : &PL_sv_undef);
39cf747a 2902 if (!lval && SvRMAGICAL(av) && SvGMAGICAL(sv)) /* see note in pp_helem() */
fd69380d 2903 mg_get(sv);
be6c24e0 2904 PUSHs(sv);
a0d0e21e
LW
2905 RETURN;
2906}
2907
9026059d 2908SV*
864dbfa3 2909Perl_vivify_ref(pTHX_ SV *sv, U32 to_what)
02a9e968 2910{
7918f24d
NC
2911 PERL_ARGS_ASSERT_VIVIFY_REF;
2912
5b295bef 2913 SvGETMAGIC(sv);
02a9e968
CS
2914 if (!SvOK(sv)) {
2915 if (SvREADONLY(sv))
6ad8f254 2916 Perl_croak_no_modify(aTHX);
43230e26 2917 prepare_SV_for_RV(sv);
68dc0745 2918 switch (to_what) {
5f05dabc 2919 case OPpDEREF_SV:
561b68a9 2920 SvRV_set(sv, newSV(0));
5f05dabc 2921 break;
2922 case OPpDEREF_AV:
ad64d0ec 2923 SvRV_set(sv, MUTABLE_SV(newAV()));
5f05dabc 2924 break;
2925 case OPpDEREF_HV:
ad64d0ec 2926 SvRV_set(sv, MUTABLE_SV(newHV()));
5f05dabc 2927 break;
2928 }
02a9e968
CS
2929 SvROK_on(sv);
2930 SvSETMAGIC(sv);
7e482323 2931 SvGETMAGIC(sv);
02a9e968 2932 }
9026059d
GG
2933 if (SvGMAGICAL(sv)) {
2934 /* copy the sv without magic to prevent magic from being
2935 executed twice */
2936 SV* msv = sv_newmortal();
2937 sv_setsv_nomg(msv, sv);
2938 return msv;
2939 }
2940 return sv;
02a9e968
CS
2941}
2942
a0d0e21e
LW
2943PP(pp_method)
2944{
97aff369 2945 dVAR; dSP;
890ce7af 2946 SV* const sv = TOPs;
f5d5a27c
CS
2947
2948 if (SvROK(sv)) {
890ce7af 2949 SV* const rsv = SvRV(sv);
f5d5a27c
CS
2950 if (SvTYPE(rsv) == SVt_PVCV) {
2951 SETs(rsv);
2952 RETURN;
2953 }
2954 }
2955
4608196e 2956 SETs(method_common(sv, NULL));
f5d5a27c
CS
2957 RETURN;
2958}
2959
2960PP(pp_method_named)
2961{
97aff369 2962 dVAR; dSP;
890ce7af 2963 SV* const sv = cSVOP_sv;
c158a4fd 2964 U32 hash = SvSHARED_HASH(sv);
f5d5a27c
CS
2965
2966 XPUSHs(method_common(sv, &hash));
2967 RETURN;
2968}
2969
2970STATIC SV *
2971S_method_common(pTHX_ SV* meth, U32* hashp)
2972{
97aff369 2973 dVAR;
a0d0e21e
LW
2974 SV* ob;
2975 GV* gv;
56304f61 2976 HV* stash;
a0714e2c 2977 SV *packsv = NULL;
f226e9be
FC
2978 SV * const sv = PL_stack_base + TOPMARK == PL_stack_sp
2979 ? (Perl_croak(aTHX_ "Can't call method \"%"SVf"\" without a "
2980 "package or object reference", SVfARG(meth)),
2981 (SV *)NULL)
2982 : *(PL_stack_base + TOPMARK + 1);
f5d5a27c 2983
7918f24d
NC
2984 PERL_ARGS_ASSERT_METHOD_COMMON;
2985
4f1b7578 2986 if (!sv)
7156e69a 2987 undefined:
a214957f
VP
2988 Perl_croak(aTHX_ "Can't call method \"%"SVf"\" on an undefined value",
2989 SVfARG(meth));
4f1b7578 2990
5b295bef 2991 SvGETMAGIC(sv);
a0d0e21e 2992 if (SvROK(sv))
ad64d0ec 2993 ob = MUTABLE_SV(SvRV(sv));
7156e69a 2994 else if (!SvOK(sv)) goto undefined;
a0d0e21e 2995 else {
89269094 2996 /* this isn't a reference */
a0d0e21e 2997 GV* iogv;
f937af42 2998 STRLEN packlen;
89269094 2999 const char * const packname = SvPV_nomg_const(sv, packlen);
b3ebc221 3000 const bool packname_is_utf8 = !!SvUTF8(sv);
89269094 3001 const HE* const he =
b3ebc221
NC
3002 (const HE *)hv_common(
3003 PL_stashcache, NULL, packname, packlen,
3004 packname_is_utf8 ? HVhek_UTF8 : 0, 0, NULL, 0
da6b625f
FC
3005 );
3006
89269094 3007 if (he) {
5e6396ae 3008 stash = INT2PTR(HV*,SvIV(HeVAL(he)));
103f5a36
NC
3009 DEBUG_o(Perl_deb(aTHX_ "PL_stashcache hit %p for '%"SVf"'\n",
3010 stash, sv));
081fc587 3011 goto fetch;
081fc587
AB
3012 }
3013
89269094 3014 if (!(iogv = gv_fetchpvn_flags(
da6b625f
FC
3015 packname, packlen, SVf_UTF8 * packname_is_utf8, SVt_PVIO
3016 )) ||
ad64d0ec 3017 !(ob=MUTABLE_SV(GvIO(iogv))))
a0d0e21e 3018 {
af09ea45 3019 /* this isn't the name of a filehandle either */
89269094 3020 if (!packlen)
834a4ddd 3021 {
7156e69a
FC
3022 Perl_croak(aTHX_ "Can't call method \"%"SVf"\" "
3023 "without a package or object reference",
3024 SVfARG(meth));
834a4ddd 3025 }
af09ea45 3026 /* assume it's a package name */
f937af42 3027 stash = gv_stashpvn(packname, packlen, packname_is_utf8 ? SVf_UTF8 : 0);
0dae17bd
GS
3028 if (!stash)
3029 packsv = sv;
081fc587 3030 else {
d4c19fe8 3031 SV* const ref = newSViv(PTR2IV(stash));
f937af42 3032 (void)hv_store(PL_stashcache, packname,
c60dbbc3 3033 packname_is_utf8 ? -(I32)packlen : (I32)packlen, ref, 0);
103f5a36
NC
3034 DEBUG_o(Perl_deb(aTHX_ "PL_stashcache caching %p for '%"SVf"'\n",
3035 stash, sv));
7e8961ec 3036 }
ac91690f 3037 goto fetch;
a0d0e21e 3038 }
af09ea45 3039 /* it _is_ a filehandle name -- replace with a reference */
ad64d0ec 3040 *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV(MUTABLE_SV(iogv)));
a0d0e21e
LW
3041 }
3042
af09ea45 3043 /* if we got here, ob should be a reference or a glob */
f0d43078 3044 if (!ob || !(SvOBJECT(ob)
6e592b3a
BM
3045 || (SvTYPE(ob) == SVt_PVGV
3046 && isGV_with_GP(ob)
159b6efe 3047 && (ob = MUTABLE_SV(GvIO((const GV *)ob)))
f0d43078
GS
3048 && SvOBJECT(ob))))
3049 {
b375e37b
BF
3050 Perl_croak(aTHX_ "Can't call method \"%"SVf"\" on unblessed reference",
3051 SVfARG((SvSCREAM(meth) && strEQ(SvPV_nolen_const(meth),"isa"))
3052 ? newSVpvs_flags("DOES", SVs_TEMP)
3053 : meth));
f0d43078 3054 }
a0d0e21e 3055
56304f61 3056 stash = SvSTASH(ob);
a0d0e21e 3057
ac91690f 3058 fetch:
af09ea45
IK
3059 /* NOTE: stash may be null, hope hv_fetch_ent and
3060 gv_fetchmethod can cope (it seems they can) */
3061
f5d5a27c
CS
3062 /* shortcut for simple names */
3063 if (hashp) {
b464bac0 3064 const HE* const he = hv_fetch_ent(stash, meth, 0, *hashp);
f5d5a27c 3065 if (he) {
159b6efe 3066 gv = MUTABLE_GV(HeVAL(he));
f5d5a27c 3067 if (isGV(gv) && GvCV(gv) &&
e1a479c5 3068 (!GvCVGEN(gv) || GvCVGEN(gv)
dd69841b 3069 == (PL_sub_generation + HvMROMETA(stash)->cache_gen)))
ad64d0ec 3070 return MUTABLE_SV(GvCV(gv));
f5d5a27c
CS
3071 }
3072 }
3073
f937af42
BF
3074 gv = gv_fetchmethod_sv_flags(stash ? stash : MUTABLE_HV(packsv),
3075 meth, GV_AUTOLOAD | GV_CROAK);
9b9d0b15 3076
256d1bb2 3077 assert(gv);
9b9d0b15 3078
ad64d0ec 3079 return isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv);
a0d0e21e 3080}
241d1a3b
NC
3081
3082/*
3083 * Local variables:
3084 * c-indentation-style: bsd
3085 * c-basic-offset: 4
14d04a33 3086 * indent-tabs-mode: nil
241d1a3b
NC
3087 * End:
3088 *
14d04a33 3089 * ex: set ts=8 sts=4 sw=4 et:
37442d52 3090 */