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