Commit | Line | Data |
---|---|---|
a0d0e21e LW |
1 | /* pp_hot.c |
2 | * | |
e6906430 | 3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
2c351e65 | 4 | * 2000, 2001, 2002, 2003, 2004, 2005, 2006, 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 | * | |
15 | * Awake! Awake! Fear, Fire, Foes! Awake! | |
16 | * Fire, Foes! Awake! | |
17 | */ | |
18 | ||
40d34c0d SB |
19 | /* This file contains 'hot' pp ("push/pop") functions that |
20 | * execute the opcodes that make up a perl program. A typical pp function | |
21 | * expects to find its arguments on the stack, and usually pushes its | |
22 | * results onto the stack, hence the 'pp' terminology. Each OP structure | |
23 | * contains a pointer to the relevant pp_foo() function. | |
24 | * | |
25 | * By 'hot', we mean common ops whose execution speed is critical. | |
26 | * By gathering them together into a single file, we encourage | |
27 | * CPU cache hits on hot code. Also it could be taken as a warning not to | |
28 | * change any code in this file unless you're sure it won't affect | |
29 | * performance. | |
30 | */ | |
31 | ||
a0d0e21e | 32 | #include "EXTERN.h" |
864dbfa3 | 33 | #define PERL_IN_PP_HOT_C |
a0d0e21e LW |
34 | #include "perl.h" |
35 | ||
36 | /* Hot code. */ | |
37 | ||
4d1ff10f | 38 | #ifdef USE_5005THREADS |
acfe0abc | 39 | static void unset_cvowner(pTHX_ void *cvarg); |
4d1ff10f | 40 | #endif /* USE_5005THREADS */ |
11343788 | 41 | |
a0d0e21e LW |
42 | PP(pp_const) |
43 | { | |
39644a26 | 44 | dSP; |
1d7c1841 | 45 | XPUSHs(cSVOP_sv); |
a0d0e21e LW |
46 | RETURN; |
47 | } | |
48 | ||
49 | PP(pp_nextstate) | |
50 | { | |
533c011a | 51 | PL_curcop = (COP*)PL_op; |
a0d0e21e | 52 | TAINT_NOT; /* Each statement is presumed innocent */ |
3280af22 | 53 | PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp; |
a0d0e21e LW |
54 | FREETMPS; |
55 | return NORMAL; | |
56 | } | |
57 | ||
58 | PP(pp_gvsv) | |
59 | { | |
39644a26 | 60 | dSP; |
924508f0 | 61 | EXTEND(SP,1); |
533c011a | 62 | if (PL_op->op_private & OPpLVAL_INTRO) |
1d7c1841 | 63 | PUSHs(save_scalar(cGVOP_gv)); |
a0d0e21e | 64 | else |
83fd6193 | 65 | PUSHs(GvSVn(cGVOP_gv)); |
a0d0e21e LW |
66 | RETURN; |
67 | } | |
68 | ||
69 | PP(pp_null) | |
70 | { | |
71 | return NORMAL; | |
72 | } | |
73 | ||
7399586d HS |
74 | PP(pp_setstate) |
75 | { | |
76 | PL_curcop = (COP*)PL_op; | |
77 | return NORMAL; | |
78 | } | |
79 | ||
a0d0e21e LW |
80 | PP(pp_pushmark) |
81 | { | |
3280af22 | 82 | PUSHMARK(PL_stack_sp); |
a0d0e21e LW |
83 | return NORMAL; |
84 | } | |
85 | ||
86 | PP(pp_stringify) | |
87 | { | |
39644a26 | 88 | dSP; dTARGET; |
6050d10e | 89 | sv_copypv(TARG,TOPs); |
a0d0e21e LW |
90 | SETTARG; |
91 | RETURN; | |
92 | } | |
93 | ||
94 | PP(pp_gv) | |
95 | { | |
39644a26 | 96 | dSP; |
1d7c1841 | 97 | XPUSHs((SV*)cGVOP_gv); |
a0d0e21e LW |
98 | RETURN; |
99 | } | |
100 | ||
101 | PP(pp_and) | |
102 | { | |
39644a26 | 103 | dSP; |
a0d0e21e LW |
104 | if (!SvTRUE(TOPs)) |
105 | RETURN; | |
106 | else { | |
35273943 AL |
107 | if (PL_op->op_type == OP_AND) |
108 | --SP; | |
a0d0e21e LW |
109 | RETURNOP(cLOGOP->op_other); |
110 | } | |
111 | } | |
112 | ||
113 | PP(pp_sassign) | |
114 | { | |
39644a26 | 115 | dSP; dPOPTOPssrl; |
748a9306 | 116 | |
533c011a | 117 | if (PL_op->op_private & OPpASSIGN_BACKWARDS) { |
8c89da26 AL |
118 | SV * const temp = left; |
119 | left = right; right = temp; | |
a0d0e21e | 120 | } |
3280af22 | 121 | if (PL_tainting && PL_tainted && !SvTAINTED(left)) |
a0d0e21e | 122 | TAINT_NOT; |
6aa01a30 | 123 | if (PL_op->op_private & OPpASSIGN_CV_TO_GV) { |
8916b559 | 124 | SV * const cv = SvRV(left); |
6aa01a30 NC |
125 | const U32 cv_type = SvTYPE(cv); |
126 | const U32 gv_type = SvTYPE(right); | |
8916b559 | 127 | const bool got_coderef = cv_type == SVt_PVCV || cv_type == SVt_PVFM; |
6aa01a30 NC |
128 | |
129 | if (!got_coderef) { | |
130 | assert(SvROK(cv)); | |
131 | } | |
132 | ||
133 | /* Can do the optimisation if right (LVAUE) is not a typeglob, | |
134 | left (RVALUE) is a reference to something, and we're in void | |
135 | context. */ | |
136 | if (!got_coderef && gv_type != SVt_PVGV && GIMME_V == G_VOID) { | |
137 | /* Is the target symbol table currently empty? */ | |
8916b559 | 138 | GV * const gv = gv_fetchsv(right, GV_NOINIT, SVt_PVGV); |
6aa01a30 NC |
139 | if (SvTYPE(gv) != SVt_PVGV && !SvOK(gv)) { |
140 | /* Good. Create a new proxy constant subroutine in the target. | |
141 | The gv becomes a(nother) reference to the constant. */ | |
142 | SV *const value = SvRV(cv); | |
143 | ||
144 | SvUPGRADE((SV *)gv, SVt_RV); | |
145 | SvROK_on(gv); | |
146 | SvRV_set(gv, value); | |
147 | SvREFCNT_inc(value); | |
148 | SETs(right); | |
149 | RETURN; | |
150 | } | |
151 | } | |
152 | ||
153 | /* Need to fix things up. */ | |
154 | if (gv_type != SVt_PVGV) { | |
155 | /* Need to fix GV. */ | |
156 | right = (SV*)gv_fetchsv(right, GV_ADD, SVt_PVGV); | |
157 | } | |
158 | ||
159 | if (!got_coderef) { | |
160 | /* We've been returned a constant rather than a full subroutine, | |
161 | but they expect a subroutine reference to apply. */ | |
162 | ENTER; | |
163 | SvREFCNT_inc(SvRV(cv)); | |
164 | /* newCONSTSUB takes a reference count on the passed in SV | |
165 | from us. We set the name to NULL, otherwise we get into | |
166 | all sorts of fun as the reference to our new sub is | |
167 | donated to the GV that we're about to assign to. | |
168 | */ | |
169 | SvRV_set(left, (SV *)newCONSTSUB(GvSTASH(right), NULL, | |
170 | SvRV(cv))); | |
171 | SvREFCNT_dec(cv); | |
172 | LEAVE; | |
173 | } | |
174 | ||
175 | } | |
54310121 | 176 | SvSetMagicSV(right, left); |
a0d0e21e LW |
177 | SETs(right); |
178 | RETURN; | |
179 | } | |
180 | ||
181 | PP(pp_cond_expr) | |
182 | { | |
39644a26 | 183 | dSP; |
a0d0e21e | 184 | if (SvTRUEx(POPs)) |
1a67a97c | 185 | RETURNOP(cLOGOP->op_other); |
a0d0e21e | 186 | else |
1a67a97c | 187 | RETURNOP(cLOGOP->op_next); |
a0d0e21e LW |
188 | } |
189 | ||
190 | PP(pp_unstack) | |
191 | { | |
192 | I32 oldsave; | |
193 | TAINT_NOT; /* Each statement is presumed innocent */ | |
3280af22 | 194 | PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp; |
a0d0e21e | 195 | FREETMPS; |
3280af22 | 196 | oldsave = PL_scopestack[PL_scopestack_ix - 1]; |
a0d0e21e LW |
197 | LEAVE_SCOPE(oldsave); |
198 | return NORMAL; | |
199 | } | |
200 | ||
a0d0e21e LW |
201 | PP(pp_concat) |
202 | { | |
39644a26 | 203 | dSP; dATARGET; tryAMAGICbin(concat,opASSIGN); |
748a9306 LW |
204 | { |
205 | dPOPTOPssrl; | |
8d6d96c1 HS |
206 | bool lbyte; |
207 | STRLEN rlen; | |
8ec8dcb0 | 208 | const char *rpv = SvPV_const(right, rlen); /* mg_get(right) happens here */ |
c985f954 AL |
209 | const bool rbyte = !DO_UTF8(right); |
210 | bool rcopied = FALSE; | |
8d6d96c1 HS |
211 | |
212 | if (TARG == right && right != left) { | |
213 | right = sv_2mortal(newSVpvn(rpv, rlen)); | |
23c2bb70 | 214 | rpv = SvPV_const(right, rlen); /* no point setting UTF-8 here */ |
5835a535 | 215 | rcopied = TRUE; |
8d6d96c1 | 216 | } |
7889fe52 | 217 | |
8d6d96c1 | 218 | if (TARG != left) { |
c985f954 | 219 | STRLEN llen; |
8ec8dcb0 | 220 | const char* const lpv = SvPV_const(left, llen); /* mg_get(left) may happen here */ |
738abb1b | 221 | lbyte = !DO_UTF8(left); |
8d6d96c1 HS |
222 | sv_setpvn(TARG, lpv, llen); |
223 | if (!lbyte) | |
224 | SvUTF8_on(TARG); | |
225 | else | |
226 | SvUTF8_off(TARG); | |
227 | } | |
228 | else { /* TARG == left */ | |
c985f954 | 229 | STRLEN llen; |
255c29c3 | 230 | SvGETMAGIC(left); /* or mg_get(left) may happen here */ |
8d6d96c1 | 231 | if (!SvOK(TARG)) |
2a8de9e2 | 232 | sv_setpvn(left, "", 0); |
c06c673c | 233 | (void)SvPV_nomg_const(left, llen); /* Needed to set UTF8 flag */ |
738abb1b TS |
234 | lbyte = !DO_UTF8(left); |
235 | if (IN_BYTES) | |
236 | SvUTF8_off(TARG); | |
8d6d96c1 | 237 | } |
a12c0f56 | 238 | |
fd15e783 PN |
239 | #if defined(PERL_Y2KWARN) |
240 | if ((SvIOK(right) || SvNOK(right)) && ckWARN(WARN_Y2K) && SvOK(TARG)) { | |
8d6d96c1 HS |
241 | if (llen >= 2 && lpv[llen - 2] == '1' && lpv[llen - 1] == '9' |
242 | && (llen == 2 || !isDIGIT(lpv[llen - 3]))) | |
243 | { | |
9014280d | 244 | Perl_warner(aTHX_ packWARN(WARN_Y2K), "Possible Y2K bug: %s", |
8d6d96c1 HS |
245 | "about to append an integer to '19'"); |
246 | } | |
fd15e783 PN |
247 | } |
248 | #endif | |
249 | ||
8d6d96c1 HS |
250 | if (lbyte != rbyte) { |
251 | if (lbyte) | |
252 | sv_utf8_upgrade_nomg(TARG); | |
253 | else { | |
5835a535 JH |
254 | if (!rcopied) |
255 | right = sv_2mortal(newSVpvn(rpv, rlen)); | |
8d6d96c1 | 256 | sv_utf8_upgrade_nomg(right); |
23c2bb70 | 257 | rpv = SvPV_const(right, rlen); |
69b47968 | 258 | } |
a0d0e21e | 259 | } |
8d6d96c1 | 260 | sv_catpvn_nomg(TARG, rpv, rlen); |
43ebc500 | 261 | |
a0d0e21e LW |
262 | SETTARG; |
263 | RETURN; | |
748a9306 | 264 | } |
a0d0e21e LW |
265 | } |
266 | ||
267 | PP(pp_padsv) | |
268 | { | |
39644a26 | 269 | dSP; dTARGET; |
a0d0e21e | 270 | XPUSHs(TARG); |
533c011a NIS |
271 | if (PL_op->op_flags & OPf_MOD) { |
272 | if (PL_op->op_private & OPpLVAL_INTRO) | |
9755d405 | 273 | SAVECLEARSV(PAD_SVl(PL_op->op_targ)); |
533c011a | 274 | else if (PL_op->op_private & OPpDEREF) { |
8ec5e241 | 275 | PUTBACK; |
9755d405 | 276 | vivify_ref(PAD_SVl(PL_op->op_targ), PL_op->op_private & OPpDEREF); |
8ec5e241 NIS |
277 | SPAGAIN; |
278 | } | |
4633a7c4 | 279 | } |
a0d0e21e LW |
280 | RETURN; |
281 | } | |
282 | ||
283 | PP(pp_readline) | |
284 | { | |
f5284f61 | 285 | tryAMAGICunTARGET(iter, 0); |
3280af22 | 286 | PL_last_in_gv = (GV*)(*PL_stack_sp--); |
8efb3254 | 287 | if (SvTYPE(PL_last_in_gv) != SVt_PVGV) { |
1c846c1f | 288 | if (SvROK(PL_last_in_gv) && SvTYPE(SvRV(PL_last_in_gv)) == SVt_PVGV) |
f5284f61 | 289 | PL_last_in_gv = (GV*)SvRV(PL_last_in_gv); |
8efb3254 | 290 | else { |
f5284f61 IZ |
291 | dSP; |
292 | XPUSHs((SV*)PL_last_in_gv); | |
293 | PUTBACK; | |
cea2e8a9 | 294 | pp_rv2gv(); |
f5284f61 | 295 | PL_last_in_gv = (GV*)(*PL_stack_sp--); |
f5284f61 IZ |
296 | } |
297 | } | |
a0d0e21e LW |
298 | return do_readline(); |
299 | } | |
300 | ||
301 | PP(pp_eq) | |
302 | { | |
39644a26 | 303 | dSP; tryAMAGICbinSET(eq,0); |
4c9fe80f | 304 | #ifndef NV_PRESERVES_UV |
8ded1040 | 305 | if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) { |
e61d22ef NC |
306 | SP--; |
307 | SETs(boolSV(SvRV(TOPs) == SvRV(TOPp1s))); | |
4c9fe80f AS |
308 | RETURN; |
309 | } | |
310 | #endif | |
28e5dec8 JH |
311 | #ifdef PERL_PRESERVE_IVUV |
312 | SvIV_please(TOPs); | |
313 | if (SvIOK(TOPs)) { | |
4c9fe80f AS |
314 | /* Unless the left argument is integer in range we are going |
315 | to have to use NV maths. Hence only attempt to coerce the | |
316 | right argument if we know the left is integer. */ | |
28e5dec8 JH |
317 | SvIV_please(TOPm1s); |
318 | if (SvIOK(TOPm1s)) { | |
8c89da26 AL |
319 | const bool auvok = SvUOK(TOPm1s); |
320 | const bool buvok = SvUOK(TOPs); | |
a12c0f56 | 321 | |
1605159e NC |
322 | if (auvok == buvok) { /* ## IV == IV or UV == UV ## */ |
323 | /* Casting IV to UV before comparison isn't going to matter | |
324 | on 2s complement. On 1s complement or sign&magnitude | |
325 | (if we have any of them) it could to make negative zero | |
326 | differ from normal zero. As I understand it. (Need to | |
327 | check - is negative zero implementation defined behaviour | |
328 | anyway?). NWC */ | |
8c89da26 AL |
329 | const UV buv = SvUVX(POPs); |
330 | const UV auv = SvUVX(TOPs); | |
28e5dec8 | 331 | |
28e5dec8 JH |
332 | SETs(boolSV(auv == buv)); |
333 | RETURN; | |
334 | } | |
335 | { /* ## Mixed IV,UV ## */ | |
1605159e | 336 | SV *ivp, *uvp; |
28e5dec8 | 337 | IV iv; |
28e5dec8 | 338 | |
1605159e | 339 | /* == is commutative so doesn't matter which is left or right */ |
28e5dec8 | 340 | if (auvok) { |
1605159e NC |
341 | /* top of stack (b) is the iv */ |
342 | ivp = *SP; | |
343 | uvp = *--SP; | |
344 | } else { | |
345 | uvp = *SP; | |
346 | ivp = *--SP; | |
347 | } | |
348 | iv = SvIVX(ivp); | |
349 | if (iv < 0) { | |
350 | /* As uv is a UV, it's >0, so it cannot be == */ | |
351 | SETs(&PL_sv_no); | |
352 | RETURN; | |
353 | } | |
28e5dec8 | 354 | /* we know iv is >= 0 */ |
1605159e | 355 | SETs(boolSV((UV)iv == SvUVX(uvp))); |
28e5dec8 JH |
356 | RETURN; |
357 | } | |
358 | } | |
359 | } | |
360 | #endif | |
a0d0e21e LW |
361 | { |
362 | dPOPnv; | |
54310121 | 363 | SETs(boolSV(TOPn == value)); |
a0d0e21e LW |
364 | RETURN; |
365 | } | |
366 | } | |
367 | ||
368 | PP(pp_preinc) | |
369 | { | |
39644a26 | 370 | dSP; |
3510b4a1 | 371 | if (SvTYPE(TOPs) > SVt_PVLV) |
d470f89e | 372 | DIE(aTHX_ PL_no_modify); |
3510b4a1 NC |
373 | if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) |
374 | && SvIVX(TOPs) != IV_MAX) | |
55497cff | 375 | { |
0da6cfda | 376 | SvIV_set(TOPs, SvIVX(TOPs) + 1); |
55497cff | 377 | SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK); |
748a9306 | 378 | } |
28e5dec8 | 379 | else /* Do all the PERL_PRESERVE_IVUV conditionals in sv_inc */ |
748a9306 | 380 | sv_inc(TOPs); |
a0d0e21e LW |
381 | SvSETMAGIC(TOPs); |
382 | return NORMAL; | |
383 | } | |
384 | ||
385 | PP(pp_or) | |
386 | { | |
39644a26 | 387 | dSP; |
a0d0e21e LW |
388 | if (SvTRUE(TOPs)) |
389 | RETURN; | |
390 | else { | |
35273943 AL |
391 | if (PL_op->op_type == OP_OR) |
392 | --SP; | |
a0d0e21e LW |
393 | RETURNOP(cLOGOP->op_other); |
394 | } | |
395 | } | |
396 | ||
397 | PP(pp_add) | |
398 | { | |
39644a26 | 399 | dSP; dATARGET; bool useleft; tryAMAGICbin(add,opASSIGN); |
28e5dec8 JH |
400 | useleft = USE_LEFT(TOPm1s); |
401 | #ifdef PERL_PRESERVE_IVUV | |
402 | /* We must see if we can perform the addition with integers if possible, | |
403 | as the integer code detects overflow while the NV code doesn't. | |
404 | If either argument hasn't had a numeric conversion yet attempt to get | |
405 | the IV. It's important to do this now, rather than just assuming that | |
406 | it's not IOK as a PV of "9223372036854775806" may not take well to NV | |
407 | addition, and an SV which is NOK, NV=6.0 ought to be coerced to | |
408 | integer in case the second argument is IV=9223372036854775806 | |
409 | We can (now) rely on sv_2iv to do the right thing, only setting the | |
410 | public IOK flag if the value in the NV (or PV) slot is truly integer. | |
411 | ||
412 | A side effect is that this also aggressively prefers integer maths over | |
7dca457a NC |
413 | fp maths for integer values. |
414 | ||
a00b5bd3 | 415 | How to detect overflow? |
7dca457a NC |
416 | |
417 | C 99 section 6.2.6.1 says | |
418 | ||
419 | The range of nonnegative values of a signed integer type is a subrange | |
420 | of the corresponding unsigned integer type, and the representation of | |
421 | the same value in each type is the same. A computation involving | |
422 | unsigned operands can never overflow, because a result that cannot be | |
423 | represented by the resulting unsigned integer type is reduced modulo | |
424 | the number that is one greater than the largest value that can be | |
425 | represented by the resulting type. | |
426 | ||
427 | (the 9th paragraph) | |
428 | ||
429 | which I read as "unsigned ints wrap." | |
430 | ||
431 | signed integer overflow seems to be classed as "exception condition" | |
432 | ||
433 | If an exceptional condition occurs during the evaluation of an | |
434 | expression (that is, if the result is not mathematically defined or not | |
435 | in the range of representable values for its type), the behavior is | |
436 | undefined. | |
437 | ||
438 | (6.5, the 5th paragraph) | |
439 | ||
440 | I had assumed that on 2s complement machines signed arithmetic would | |
441 | wrap, hence coded pp_add and pp_subtract on the assumption that | |
442 | everything perl builds on would be happy. After much wailing and | |
443 | gnashing of teeth it would seem that irix64 knows its ANSI spec well, | |
444 | knows that it doesn't need to, and doesn't. Bah. Anyway, the all- | |
445 | unsigned code below is actually shorter than the old code. :-) | |
446 | */ | |
447 | ||
28e5dec8 JH |
448 | SvIV_please(TOPs); |
449 | if (SvIOK(TOPs)) { | |
450 | /* Unless the left argument is integer in range we are going to have to | |
451 | use NV maths. Hence only attempt to coerce the right argument if | |
452 | we know the left is integer. */ | |
9c5ffd7c JH |
453 | register UV auv = 0; |
454 | bool auvok = FALSE; | |
7dca457a NC |
455 | bool a_valid = 0; |
456 | ||
28e5dec8 | 457 | if (!useleft) { |
7dca457a NC |
458 | auv = 0; |
459 | a_valid = auvok = 1; | |
460 | /* left operand is undef, treat as zero. + 0 is identity, | |
461 | Could SETi or SETu right now, but space optimise by not adding | |
462 | lots of code to speed up what is probably a rarish case. */ | |
463 | } else { | |
464 | /* Left operand is defined, so is it IV? */ | |
465 | SvIV_please(TOPm1s); | |
466 | if (SvIOK(TOPm1s)) { | |
467 | if ((auvok = SvUOK(TOPm1s))) | |
468 | auv = SvUVX(TOPm1s); | |
469 | else { | |
8c18bf38 | 470 | register const IV aiv = SvIVX(TOPm1s); |
7dca457a NC |
471 | if (aiv >= 0) { |
472 | auv = aiv; | |
473 | auvok = 1; /* Now acting as a sign flag. */ | |
474 | } else { /* 2s complement assumption for IV_MIN */ | |
475 | auv = (UV)-aiv; | |
476 | } | |
477 | } | |
478 | a_valid = 1; | |
28e5dec8 JH |
479 | } |
480 | } | |
7dca457a NC |
481 | if (a_valid) { |
482 | bool result_good = 0; | |
483 | UV result; | |
484 | register UV buv; | |
28e5dec8 | 485 | bool buvok = SvUOK(TOPs); |
a00b5bd3 | 486 | |
7dca457a NC |
487 | if (buvok) |
488 | buv = SvUVX(TOPs); | |
489 | else { | |
8c18bf38 | 490 | register const IV biv = SvIVX(TOPs); |
7dca457a NC |
491 | if (biv >= 0) { |
492 | buv = biv; | |
493 | buvok = 1; | |
494 | } else | |
495 | buv = (UV)-biv; | |
496 | } | |
497 | /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve, | |
602f51c4 | 498 | else "IV" now, independent of how it came in. |
7dca457a NC |
499 | if a, b represents positive, A, B negative, a maps to -A etc |
500 | a + b => (a + b) | |
501 | A + b => -(a - b) | |
502 | a + B => (a - b) | |
503 | A + B => -(a + b) | |
504 | all UV maths. negate result if A negative. | |
505 | add if signs same, subtract if signs differ. */ | |
506 | ||
507 | if (auvok ^ buvok) { | |
508 | /* Signs differ. */ | |
509 | if (auv >= buv) { | |
510 | result = auv - buv; | |
511 | /* Must get smaller */ | |
512 | if (result <= auv) | |
513 | result_good = 1; | |
514 | } else { | |
515 | result = buv - auv; | |
516 | if (result <= buv) { | |
517 | /* result really should be -(auv-buv). as its negation | |
518 | of true value, need to swap our result flag */ | |
519 | auvok = !auvok; | |
520 | result_good = 1; | |
28e5dec8 JH |
521 | } |
522 | } | |
7dca457a NC |
523 | } else { |
524 | /* Signs same */ | |
525 | result = auv + buv; | |
526 | if (result >= auv) | |
527 | result_good = 1; | |
528 | } | |
529 | if (result_good) { | |
530 | SP--; | |
531 | if (auvok) | |
28e5dec8 | 532 | SETu( result ); |
7dca457a NC |
533 | else { |
534 | /* Negate result */ | |
535 | if (result <= (UV)IV_MIN) | |
536 | SETi( -(IV)result ); | |
537 | else { | |
538 | /* result valid, but out of range for IV. */ | |
539 | SETn( -(NV)result ); | |
28e5dec8 JH |
540 | } |
541 | } | |
7dca457a NC |
542 | RETURN; |
543 | } /* Overflow, drop through to NVs. */ | |
28e5dec8 JH |
544 | } |
545 | } | |
546 | #endif | |
a0d0e21e | 547 | { |
28e5dec8 JH |
548 | dPOPnv; |
549 | if (!useleft) { | |
550 | /* left operand is undef, treat as zero. + 0.0 is identity. */ | |
551 | SETn(value); | |
552 | RETURN; | |
553 | } | |
554 | SETn( value + TOPn ); | |
555 | RETURN; | |
a0d0e21e LW |
556 | } |
557 | } | |
558 | ||
559 | PP(pp_aelemfast) | |
560 | { | |
39644a26 | 561 | dSP; |
f4362cdc | 562 | AV * const av = PL_op->op_flags & OPf_SPECIAL ? |
c9f5579e | 563 | (AV*)PAD_SV(PL_op->op_targ) : GvAV(cGVOP_gv); |
8c18bf38 | 564 | const U32 lval = PL_op->op_flags & OPf_MOD; |
8c89da26 | 565 | SV** const svp = av_fetch(av, PL_op->op_private, lval); |
3280af22 | 566 | SV *sv = (svp ? *svp : &PL_sv_undef); |
6ff81951 | 567 | EXTEND(SP, 1); |
be6c24e0 GS |
568 | if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */ |
569 | sv = sv_mortalcopy(sv); | |
570 | PUSHs(sv); | |
a0d0e21e LW |
571 | RETURN; |
572 | } | |
573 | ||
574 | PP(pp_join) | |
575 | { | |
39644a26 | 576 | dSP; dMARK; dTARGET; |
a0d0e21e LW |
577 | MARK++; |
578 | do_join(TARG, *MARK, MARK, SP); | |
579 | SP = MARK; | |
580 | SETs(TARG); | |
581 | RETURN; | |
582 | } | |
583 | ||
584 | PP(pp_pushre) | |
585 | { | |
39644a26 | 586 | dSP; |
44a8e56a | 587 | #ifdef DEBUGGING |
588 | /* | |
589 | * We ass_u_me that LvTARGOFF() comes first, and that two STRLENs | |
590 | * will be enough to hold an OP*. | |
591 | */ | |
1a9219e7 | 592 | SV* const sv = sv_newmortal(); |
44a8e56a | 593 | sv_upgrade(sv, SVt_PVLV); |
594 | LvTYPE(sv) = '/'; | |
533c011a | 595 | Copy(&PL_op, &LvTARGOFF(sv), 1, OP*); |
44a8e56a | 596 | XPUSHs(sv); |
597 | #else | |
6b88bc9c | 598 | XPUSHs((SV*)PL_op); |
44a8e56a | 599 | #endif |
a0d0e21e LW |
600 | RETURN; |
601 | } | |
602 | ||
603 | /* Oversized hot code. */ | |
604 | ||
605 | PP(pp_print) | |
606 | { | |
39644a26 | 607 | dSP; dMARK; dORIGMARK; |
a0d0e21e | 608 | IO *io; |
760ac839 | 609 | register PerlIO *fp; |
236988e4 | 610 | MAGIC *mg; |
8c89da26 | 611 | GV * const gv = (PL_op->op_flags & OPf_STACKED) ? (GV*)*++MARK : PL_defoutgv; |
5b468f54 AMS |
612 | |
613 | if (gv && (io = GvIO(gv)) | |
614 | && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) | |
615 | { | |
01bb7c6d | 616 | had_magic: |
68dc0745 | 617 | if (MARK == ORIGMARK) { |
1c846c1f | 618 | /* If using default handle then we need to make space to |
a60c0954 NIS |
619 | * pass object as 1st arg, so move other args up ... |
620 | */ | |
4352c267 | 621 | MEXTEND(SP, 1); |
68dc0745 | 622 | ++MARK; |
623 | Move(MARK, MARK + 1, (SP - MARK) + 1, SV*); | |
624 | ++SP; | |
625 | } | |
626 | PUSHMARK(MARK - 1); | |
5b468f54 | 627 | *MARK = SvTIED_obj((SV*)io, mg); |
68dc0745 | 628 | PUTBACK; |
236988e4 | 629 | ENTER; |
864dbfa3 | 630 | call_method("PRINT", G_SCALAR); |
236988e4 | 631 | LEAVE; |
632 | SPAGAIN; | |
68dc0745 | 633 | MARK = ORIGMARK + 1; |
634 | *MARK = *SP; | |
635 | SP = MARK; | |
236988e4 | 636 | RETURN; |
637 | } | |
a0d0e21e | 638 | if (!(io = GvIO(gv))) { |
5b468f54 AMS |
639 | if ((GvEGV(gv)) && (io = GvIO(GvEGV(gv))) |
640 | && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) | |
01bb7c6d | 641 | goto had_magic; |
2dd78f96 JH |
642 | if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) |
643 | report_evil_fh(gv, io, PL_op->op_type); | |
5b7ea690 | 644 | SETERRNO(EBADF,RMS_IFI); |
a0d0e21e LW |
645 | goto just_say_no; |
646 | } | |
647 | else if (!(fp = IoOFP(io))) { | |
599cee73 | 648 | if (ckWARN2(WARN_CLOSED, WARN_IO)) { |
4c80c0b2 NC |
649 | if (IoIFP(io)) |
650 | report_evil_fh(gv, io, OP_phoney_INPUT_ONLY); | |
2dd78f96 | 651 | else if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) |
bc37a18f | 652 | report_evil_fh(gv, io, PL_op->op_type); |
a0d0e21e | 653 | } |
5b7ea690 | 654 | SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI); |
a0d0e21e LW |
655 | goto just_say_no; |
656 | } | |
657 | else { | |
658 | MARK++; | |
7889fe52 | 659 | if (PL_ofs_sv && SvOK(PL_ofs_sv)) { |
a0d0e21e LW |
660 | while (MARK <= SP) { |
661 | if (!do_print(*MARK, fp)) | |
662 | break; | |
663 | MARK++; | |
664 | if (MARK <= SP) { | |
7889fe52 | 665 | if (!do_print(PL_ofs_sv, fp)) { /* $, */ |
a0d0e21e LW |
666 | MARK--; |
667 | break; | |
668 | } | |
669 | } | |
670 | } | |
671 | } | |
672 | else { | |
673 | while (MARK <= SP) { | |
674 | if (!do_print(*MARK, fp)) | |
675 | break; | |
676 | MARK++; | |
677 | } | |
678 | } | |
679 | if (MARK <= SP) | |
680 | goto just_say_no; | |
681 | else { | |
7889fe52 NIS |
682 | if (PL_ors_sv && SvOK(PL_ors_sv)) |
683 | if (!do_print(PL_ors_sv, fp)) /* $\ */ | |
a0d0e21e LW |
684 | goto just_say_no; |
685 | ||
686 | if (IoFLAGS(io) & IOf_FLUSH) | |
760ac839 | 687 | if (PerlIO_flush(fp) == EOF) |
a0d0e21e LW |
688 | goto just_say_no; |
689 | } | |
690 | } | |
691 | SP = ORIGMARK; | |
937d2850 | 692 | XPUSHs(&PL_sv_yes); |
a0d0e21e LW |
693 | RETURN; |
694 | ||
695 | just_say_no: | |
696 | SP = ORIGMARK; | |
937d2850 | 697 | XPUSHs(&PL_sv_undef); |
a0d0e21e LW |
698 | RETURN; |
699 | } | |
700 | ||
701 | PP(pp_rv2av) | |
702 | { | |
39644a26 | 703 | dSP; dTOPss; |
a0d0e21e LW |
704 | AV *av; |
705 | ||
706 | if (SvROK(sv)) { | |
707 | wasref: | |
f5284f61 IZ |
708 | tryAMAGICunDEREF(to_av); |
709 | ||
a0d0e21e LW |
710 | av = (AV*)SvRV(sv); |
711 | if (SvTYPE(av) != SVt_PVAV) | |
cea2e8a9 | 712 | DIE(aTHX_ "Not an ARRAY reference"); |
533c011a | 713 | if (PL_op->op_flags & OPf_REF) { |
f5284f61 | 714 | SETs((SV*)av); |
a0d0e21e LW |
715 | RETURN; |
716 | } | |
78f9721b SM |
717 | else if (LVRET) { |
718 | if (GIMME == G_SCALAR) | |
719 | Perl_croak(aTHX_ "Can't return array to lvalue scalar context"); | |
720 | SETs((SV*)av); | |
721 | RETURN; | |
722 | } | |
5835a535 JH |
723 | else if (PL_op->op_flags & OPf_MOD |
724 | && PL_op->op_private & OPpLVAL_INTRO) | |
725 | Perl_croak(aTHX_ PL_no_localize_ref); | |
a0d0e21e LW |
726 | } |
727 | else { | |
728 | if (SvTYPE(sv) == SVt_PVAV) { | |
729 | av = (AV*)sv; | |
533c011a | 730 | if (PL_op->op_flags & OPf_REF) { |
f5284f61 | 731 | SETs((SV*)av); |
a0d0e21e LW |
732 | RETURN; |
733 | } | |
78f9721b SM |
734 | else if (LVRET) { |
735 | if (GIMME == G_SCALAR) | |
736 | Perl_croak(aTHX_ "Can't return array to lvalue" | |
737 | " scalar context"); | |
738 | SETs((SV*)av); | |
739 | RETURN; | |
740 | } | |
a0d0e21e LW |
741 | } |
742 | else { | |
67955e0c | 743 | GV *gv; |
1c846c1f | 744 | |
a0d0e21e LW |
745 | if (SvTYPE(sv) != SVt_PVGV) { |
746 | if (SvGMAGICAL(sv)) { | |
747 | mg_get(sv); | |
748 | if (SvROK(sv)) | |
749 | goto wasref; | |
750 | } | |
751 | if (!SvOK(sv)) { | |
533c011a NIS |
752 | if (PL_op->op_flags & OPf_REF || |
753 | PL_op->op_private & HINT_STRICT_REFS) | |
cea2e8a9 | 754 | DIE(aTHX_ PL_no_usym, "an ARRAY"); |
599cee73 | 755 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 756 | report_uninit(); |
f5284f61 | 757 | if (GIMME == G_ARRAY) { |
c2444246 | 758 | (void)POPs; |
4633a7c4 | 759 | RETURN; |
f5284f61 IZ |
760 | } |
761 | RETSETUNDEF; | |
a0d0e21e | 762 | } |
35cd451c GS |
763 | if ((PL_op->op_flags & OPf_SPECIAL) && |
764 | !(PL_op->op_flags & OPf_MOD)) | |
765 | { | |
057b822e | 766 | gv = (GV*)gv_fetchsv(sv, 0, SVt_PVAV); |
c9d5ac95 | 767 | if (!gv |
057b822e NC |
768 | && (!is_gv_magical_sv(sv,0) |
769 | || !(gv = (GV*)gv_fetchsv(sv, GV_ADD, SVt_PVAV)))) | |
c9d5ac95 | 770 | { |
35cd451c | 771 | RETSETUNDEF; |
c9d5ac95 | 772 | } |
35cd451c GS |
773 | } |
774 | else { | |
775 | if (PL_op->op_private & HINT_STRICT_REFS) | |
057b822e NC |
776 | DIE(aTHX_ PL_no_symref_sv, sv, "an ARRAY"); |
777 | gv = (GV*)gv_fetchsv(sv, GV_ADD, SVt_PVAV); | |
35cd451c GS |
778 | } |
779 | } | |
780 | else { | |
67955e0c | 781 | gv = (GV*)sv; |
a0d0e21e | 782 | } |
67955e0c | 783 | av = GvAVn(gv); |
533c011a | 784 | if (PL_op->op_private & OPpLVAL_INTRO) |
67955e0c | 785 | av = save_ary(gv); |
533c011a | 786 | if (PL_op->op_flags & OPf_REF) { |
f5284f61 | 787 | SETs((SV*)av); |
a0d0e21e LW |
788 | RETURN; |
789 | } | |
78f9721b SM |
790 | else if (LVRET) { |
791 | if (GIMME == G_SCALAR) | |
792 | Perl_croak(aTHX_ "Can't return array to lvalue" | |
793 | " scalar context"); | |
794 | SETs((SV*)av); | |
795 | RETURN; | |
796 | } | |
a0d0e21e LW |
797 | } |
798 | } | |
799 | ||
800 | if (GIMME == G_ARRAY) { | |
8c18bf38 | 801 | const I32 maxarg = AvFILL(av) + 1; |
c2444246 | 802 | (void)POPs; /* XXXX May be optimized away? */ |
1c846c1f | 803 | EXTEND(SP, maxarg); |
93965878 | 804 | if (SvRMAGICAL(av)) { |
1c846c1f | 805 | U32 i; |
eb160463 | 806 | for (i=0; i < (U32)maxarg; i++) { |
7508116b | 807 | SV ** const svp = av_fetch(av, i, FALSE); |
59e7bac0 HS |
808 | /* See note in pp_helem, and bug id #27839 */ |
809 | SP[i+1] = svp | |
810 | ? SvGMAGICAL(*svp) ? sv_mortalcopy(*svp) : *svp | |
811 | : &PL_sv_undef; | |
93965878 | 812 | } |
1c846c1f | 813 | } |
93965878 NIS |
814 | else { |
815 | Copy(AvARRAY(av), SP+1, maxarg, SV*); | |
816 | } | |
a0d0e21e LW |
817 | SP += maxarg; |
818 | } | |
c754c3d7 | 819 | else if (GIMME_V == G_SCALAR) { |
a0d0e21e | 820 | dTARGET; |
8c18bf38 | 821 | const I32 maxarg = AvFILL(av) + 1; |
f5284f61 | 822 | SETi(maxarg); |
a0d0e21e LW |
823 | } |
824 | RETURN; | |
825 | } | |
826 | ||
827 | PP(pp_rv2hv) | |
828 | { | |
39644a26 | 829 | dSP; dTOPss; |
a0d0e21e | 830 | HV *hv; |
8c18bf38 AL |
831 | const I32 gimme = GIMME_V; |
832 | static const char return_hash_to_lvalue_scalar[] = "Can't return hash to lvalue scalar context"; | |
a0d0e21e LW |
833 | |
834 | if (SvROK(sv)) { | |
835 | wasref: | |
f5284f61 IZ |
836 | tryAMAGICunDEREF(to_hv); |
837 | ||
a0d0e21e | 838 | hv = (HV*)SvRV(sv); |
c750a3ec | 839 | if (SvTYPE(hv) != SVt_PVHV && SvTYPE(hv) != SVt_PVAV) |
cea2e8a9 | 840 | DIE(aTHX_ "Not a HASH reference"); |
533c011a | 841 | if (PL_op->op_flags & OPf_REF) { |
a0d0e21e LW |
842 | SETs((SV*)hv); |
843 | RETURN; | |
844 | } | |
78f9721b | 845 | else if (LVRET) { |
207438fc | 846 | if (gimme != G_ARRAY) |
8c18bf38 | 847 | Perl_croak(aTHX_ return_hash_to_lvalue_scalar ); |
78f9721b SM |
848 | SETs((SV*)hv); |
849 | RETURN; | |
850 | } | |
5835a535 JH |
851 | else if (PL_op->op_flags & OPf_MOD |
852 | && PL_op->op_private & OPpLVAL_INTRO) | |
853 | Perl_croak(aTHX_ PL_no_localize_ref); | |
a0d0e21e LW |
854 | } |
855 | else { | |
c750a3ec | 856 | if (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV) { |
a0d0e21e | 857 | hv = (HV*)sv; |
533c011a | 858 | if (PL_op->op_flags & OPf_REF) { |
a0d0e21e LW |
859 | SETs((SV*)hv); |
860 | RETURN; | |
861 | } | |
78f9721b | 862 | else if (LVRET) { |
207438fc | 863 | if (gimme != G_ARRAY) |
8c18bf38 | 864 | Perl_croak(aTHX_ return_hash_to_lvalue_scalar ); |
78f9721b SM |
865 | SETs((SV*)hv); |
866 | RETURN; | |
867 | } | |
a0d0e21e LW |
868 | } |
869 | else { | |
67955e0c | 870 | GV *gv; |
1c846c1f | 871 | |
a0d0e21e LW |
872 | if (SvTYPE(sv) != SVt_PVGV) { |
873 | if (SvGMAGICAL(sv)) { | |
874 | mg_get(sv); | |
875 | if (SvROK(sv)) | |
876 | goto wasref; | |
877 | } | |
878 | if (!SvOK(sv)) { | |
533c011a NIS |
879 | if (PL_op->op_flags & OPf_REF || |
880 | PL_op->op_private & HINT_STRICT_REFS) | |
cea2e8a9 | 881 | DIE(aTHX_ PL_no_usym, "a HASH"); |
599cee73 | 882 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 883 | report_uninit(); |
207438fc | 884 | if (gimme == G_ARRAY) { |
4633a7c4 LW |
885 | SP--; |
886 | RETURN; | |
887 | } | |
a0d0e21e LW |
888 | RETSETUNDEF; |
889 | } | |
35cd451c GS |
890 | if ((PL_op->op_flags & OPf_SPECIAL) && |
891 | !(PL_op->op_flags & OPf_MOD)) | |
892 | { | |
057b822e | 893 | gv = (GV*)gv_fetchsv(sv, 0, SVt_PVHV); |
c9d5ac95 | 894 | if (!gv |
057b822e NC |
895 | && (!is_gv_magical_sv(sv,0) |
896 | || !(gv = (GV*)gv_fetchsv(sv, GV_ADD, SVt_PVHV)))) | |
c9d5ac95 | 897 | { |
35cd451c | 898 | RETSETUNDEF; |
c9d5ac95 | 899 | } |
35cd451c GS |
900 | } |
901 | else { | |
902 | if (PL_op->op_private & HINT_STRICT_REFS) | |
057b822e NC |
903 | DIE(aTHX_ PL_no_symref_sv, sv, "a HASH"); |
904 | gv = (GV*)gv_fetchsv(sv, GV_ADD, SVt_PVHV); | |
35cd451c GS |
905 | } |
906 | } | |
907 | else { | |
67955e0c | 908 | gv = (GV*)sv; |
a0d0e21e | 909 | } |
67955e0c | 910 | hv = GvHVn(gv); |
533c011a | 911 | if (PL_op->op_private & OPpLVAL_INTRO) |
67955e0c | 912 | hv = save_hash(gv); |
533c011a | 913 | if (PL_op->op_flags & OPf_REF) { |
a0d0e21e LW |
914 | SETs((SV*)hv); |
915 | RETURN; | |
916 | } | |
78f9721b | 917 | else if (LVRET) { |
207438fc | 918 | if (gimme != G_ARRAY) |
8c18bf38 | 919 | Perl_croak(aTHX_ return_hash_to_lvalue_scalar ); |
78f9721b SM |
920 | SETs((SV*)hv); |
921 | RETURN; | |
922 | } | |
a0d0e21e LW |
923 | } |
924 | } | |
925 | ||
207438fc | 926 | if (gimme == G_ARRAY) { /* array wanted */ |
3280af22 | 927 | *PL_stack_sp = (SV*)hv; |
cea2e8a9 | 928 | return do_kv(); |
a0d0e21e | 929 | } |
207438fc | 930 | else if (gimme == G_SCALAR) { |
a0d0e21e | 931 | dTARGET; |
207438fc | 932 | |
4b154ab5 GA |
933 | if (SvTYPE(hv) == SVt_PVAV) |
934 | hv = avhv_keys((AV*)hv); | |
207438fc | 935 | |
59679316 | 936 | TARG = Perl_hv_scalar(aTHX_ hv); |
a0d0e21e | 937 | SETTARG; |
a0d0e21e | 938 | } |
207438fc | 939 | RETURN; |
a0d0e21e LW |
940 | } |
941 | ||
10c8fecd GS |
942 | STATIC int |
943 | S_do_maybe_phash(pTHX_ AV *ary, SV **lelem, SV **firstlelem, SV **relem, | |
944 | SV **lastrelem) | |
945 | { | |
946 | OP *leftop; | |
10c8fecd GS |
947 | I32 i; |
948 | ||
949 | leftop = ((BINOP*)PL_op)->op_last; | |
950 | assert(leftop); | |
951 | assert(leftop->op_type == OP_NULL && leftop->op_targ == OP_LIST); | |
952 | leftop = ((LISTOP*)leftop)->op_first; | |
953 | assert(leftop); | |
954 | /* Skip PUSHMARK and each element already assigned to. */ | |
955 | for (i = lelem - firstlelem; i > 0; i--) { | |
956 | leftop = leftop->op_sibling; | |
957 | assert(leftop); | |
958 | } | |
959 | if (leftop->op_type != OP_RV2HV) | |
960 | return 0; | |
961 | ||
962 | /* pseudohash */ | |
963 | if (av_len(ary) > 0) | |
964 | av_fill(ary, 0); /* clear all but the fields hash */ | |
965 | if (lastrelem >= relem) { | |
966 | while (relem < lastrelem) { /* gobble up all the rest */ | |
967 | SV *tmpstr; | |
968 | assert(relem[0]); | |
969 | assert(relem[1]); | |
970 | /* Avoid a memory leak when avhv_store_ent dies. */ | |
971 | tmpstr = sv_newmortal(); | |
972 | sv_setsv(tmpstr,relem[1]); /* value */ | |
973 | relem[1] = tmpstr; | |
974 | if (avhv_store_ent(ary,relem[0],tmpstr,0)) | |
d16e9ed9 | 975 | (void)SvREFCNT_inc(tmpstr); |
10c8fecd GS |
976 | if (SvMAGICAL(ary) != 0 && SvSMAGICAL(tmpstr)) |
977 | mg_set(tmpstr); | |
978 | relem += 2; | |
979 | TAINT_NOT; | |
980 | } | |
981 | } | |
982 | if (relem == lastrelem) | |
983 | return 1; | |
984 | return 2; | |
985 | } | |
986 | ||
987 | STATIC void | |
988 | S_do_oddball(pTHX_ HV *hash, SV **relem, SV **firstrelem) | |
989 | { | |
990 | if (*relem) { | |
991 | SV *tmpstr; | |
992 | if (ckWARN(WARN_MISC)) { | |
8c18bf38 | 993 | const char *err; |
10c8fecd GS |
994 | if (relem == firstrelem && |
995 | SvROK(*relem) && | |
996 | (SvTYPE(SvRV(*relem)) == SVt_PVAV || | |
997 | SvTYPE(SvRV(*relem)) == SVt_PVHV)) | |
998 | { | |
8c18bf38 | 999 | err = "Reference found where even-sized list expected"; |
10c8fecd GS |
1000 | } |
1001 | else | |
8c18bf38 AL |
1002 | err = "Odd number of elements in hash assignment"; |
1003 | Perl_warner(aTHX_ packWARN(WARN_MISC), err); | |
10c8fecd GS |
1004 | } |
1005 | if (SvTYPE(hash) == SVt_PVAV) { | |
1006 | /* pseudohash */ | |
1007 | tmpstr = sv_newmortal(); | |
1008 | if (avhv_store_ent((AV*)hash,*relem,tmpstr,0)) | |
d16e9ed9 | 1009 | (void)SvREFCNT_inc(tmpstr); |
10c8fecd GS |
1010 | if (SvMAGICAL(hash) && SvSMAGICAL(tmpstr)) |
1011 | mg_set(tmpstr); | |
1012 | } | |
1013 | else { | |
1014 | HE *didstore; | |
133cdda0 | 1015 | tmpstr = newSV(0); |
10c8fecd GS |
1016 | didstore = hv_store_ent(hash,*relem,tmpstr,0); |
1017 | if (SvMAGICAL(hash)) { | |
1018 | if (SvSMAGICAL(tmpstr)) | |
1019 | mg_set(tmpstr); | |
1020 | if (!didstore) | |
1021 | sv_2mortal(tmpstr); | |
1022 | } | |
1023 | } | |
1024 | TAINT_NOT; | |
1025 | } | |
1026 | } | |
1027 | ||
a0d0e21e LW |
1028 | PP(pp_aassign) |
1029 | { | |
39644a26 | 1030 | dSP; |
3280af22 NIS |
1031 | SV **lastlelem = PL_stack_sp; |
1032 | SV **lastrelem = PL_stack_base + POPMARK; | |
1033 | SV **firstrelem = PL_stack_base + POPMARK + 1; | |
a0d0e21e LW |
1034 | SV **firstlelem = lastrelem + 1; |
1035 | ||
1036 | register SV **relem; | |
1037 | register SV **lelem; | |
1038 | ||
1039 | register SV *sv; | |
1040 | register AV *ary; | |
1041 | ||
54310121 | 1042 | I32 gimme; |
a0d0e21e LW |
1043 | HV *hash; |
1044 | I32 i; | |
1045 | int magic; | |
48703b5e | 1046 | int duplicates = 0; |
4358d444 | 1047 | SV **firsthashrelem = NULL; /* "= 0" keeps gcc 2.95 quiet */ |
ae4b46c5 | 1048 | |
a0d0e21e | 1049 | |
3280af22 | 1050 | PL_delaymagic = DM_DELAY; /* catch simultaneous items */ |
48703b5e | 1051 | gimme = GIMME_V; |
a0d0e21e LW |
1052 | |
1053 | /* If there's a common identifier on both sides we have to take | |
1054 | * special care that assigning the identifier on the left doesn't | |
1055 | * clobber a value on the right that's used later in the list. | |
1056 | */ | |
10c8fecd | 1057 | if (PL_op->op_private & (OPpASSIGN_COMMON)) { |
cc5e57d2 | 1058 | EXTEND_MORTAL(lastrelem - firstrelem + 1); |
10c8fecd | 1059 | for (relem = firstrelem; relem <= lastrelem; relem++) { |
155aba94 | 1060 | if ((sv = *relem)) { |
a1f49e72 | 1061 | TAINT_NOT; /* Each item is independent */ |
10c8fecd | 1062 | *relem = sv_mortalcopy(sv); |
a1f49e72 | 1063 | } |
10c8fecd | 1064 | } |
a0d0e21e LW |
1065 | } |
1066 | ||
1067 | relem = firstrelem; | |
1068 | lelem = firstlelem; | |
0e2d6244 SS |
1069 | ary = NULL; |
1070 | hash = NULL; | |
10c8fecd | 1071 | |
a0d0e21e | 1072 | while (lelem <= lastlelem) { |
bbce6d69 | 1073 | TAINT_NOT; /* Each item stands on its own, taintwise. */ |
a0d0e21e LW |
1074 | sv = *lelem++; |
1075 | switch (SvTYPE(sv)) { | |
1076 | case SVt_PVAV: | |
1077 | ary = (AV*)sv; | |
748a9306 | 1078 | magic = SvMAGICAL(ary) != 0; |
10c8fecd GS |
1079 | if (PL_op->op_private & OPpASSIGN_HASH) { |
1080 | switch (do_maybe_phash(ary, lelem, firstlelem, relem, | |
1081 | lastrelem)) | |
1082 | { | |
1083 | case 0: | |
1084 | goto normal_array; | |
1085 | case 1: | |
1086 | do_oddball((HV*)ary, relem, firstrelem); | |
1087 | } | |
1088 | relem = lastrelem + 1; | |
1089 | break; | |
1090 | } | |
1091 | normal_array: | |
a0d0e21e | 1092 | av_clear(ary); |
7e42bd57 | 1093 | av_extend(ary, lastrelem - relem); |
a0d0e21e LW |
1094 | i = 0; |
1095 | while (relem <= lastrelem) { /* gobble up all the rest */ | |
5117ca91 | 1096 | SV **didstore; |
a0d0e21e | 1097 | assert(*relem); |
04851bb3 | 1098 | sv = newSVsv(*relem); |
a0d0e21e | 1099 | *(relem++) = sv; |
5117ca91 GS |
1100 | didstore = av_store(ary,i++,sv); |
1101 | if (magic) { | |
fb73857a | 1102 | if (SvSMAGICAL(sv)) |
1103 | mg_set(sv); | |
5117ca91 | 1104 | if (!didstore) |
8127e0e3 | 1105 | sv_2mortal(sv); |
5117ca91 | 1106 | } |
bbce6d69 | 1107 | TAINT_NOT; |
a0d0e21e LW |
1108 | } |
1109 | break; | |
10c8fecd | 1110 | case SVt_PVHV: { /* normal hash */ |
a0d0e21e LW |
1111 | SV *tmpstr; |
1112 | ||
1113 | hash = (HV*)sv; | |
748a9306 | 1114 | magic = SvMAGICAL(hash) != 0; |
a0d0e21e | 1115 | hv_clear(hash); |
48703b5e | 1116 | firsthashrelem = relem; |
a0d0e21e LW |
1117 | |
1118 | while (relem < lastrelem) { /* gobble up all the rest */ | |
5117ca91 | 1119 | HE *didstore; |
8916b559 NC |
1120 | sv = *relem ? *relem : &PL_sv_no; |
1121 | relem++; | |
133cdda0 | 1122 | tmpstr = newSV(0); |
a0d0e21e LW |
1123 | if (*relem) |
1124 | sv_setsv(tmpstr,*relem); /* value */ | |
1125 | *(relem++) = tmpstr; | |
48703b5e NC |
1126 | if (gimme != G_VOID && hv_exists_ent(hash, sv, 0)) |
1127 | /* key overwrites an existing entry */ | |
1128 | duplicates += 2; | |
5117ca91 GS |
1129 | didstore = hv_store_ent(hash,sv,tmpstr,0); |
1130 | if (magic) { | |
fb73857a | 1131 | if (SvSMAGICAL(tmpstr)) |
1132 | mg_set(tmpstr); | |
5117ca91 | 1133 | if (!didstore) |
8127e0e3 | 1134 | sv_2mortal(tmpstr); |
5117ca91 | 1135 | } |
bbce6d69 | 1136 | TAINT_NOT; |
8e07c86e | 1137 | } |
6a0deba8 | 1138 | if (relem == lastrelem) { |
10c8fecd | 1139 | do_oddball(hash, relem, firstrelem); |
6a0deba8 | 1140 | relem++; |
1930e939 | 1141 | } |
a0d0e21e LW |
1142 | } |
1143 | break; | |
1144 | default: | |
6fc92669 GS |
1145 | if (SvIMMORTAL(sv)) { |
1146 | if (relem <= lastrelem) | |
1147 | relem++; | |
1148 | break; | |
a0d0e21e LW |
1149 | } |
1150 | if (relem <= lastrelem) { | |
1151 | sv_setsv(sv, *relem); | |
1152 | *(relem++) = sv; | |
1153 | } | |
1154 | else | |
3280af22 | 1155 | sv_setsv(sv, &PL_sv_undef); |
a0d0e21e LW |
1156 | SvSETMAGIC(sv); |
1157 | break; | |
1158 | } | |
1159 | } | |
3280af22 NIS |
1160 | if (PL_delaymagic & ~DM_DELAY) { |
1161 | if (PL_delaymagic & DM_UID) { | |
a0d0e21e | 1162 | #ifdef HAS_SETRESUID |
6e67c536 RD |
1163 | (void)setresuid((PL_delaymagic & DM_RUID) ? PL_uid : (Uid_t)-1, |
1164 | (PL_delaymagic & DM_EUID) ? PL_euid : (Uid_t)-1, | |
1165 | (Uid_t)-1); | |
56febc5e AD |
1166 | #else |
1167 | # ifdef HAS_SETREUID | |
6e67c536 RD |
1168 | (void)setreuid((PL_delaymagic & DM_RUID) ? PL_uid : (Uid_t)-1, |
1169 | (PL_delaymagic & DM_EUID) ? PL_euid : (Uid_t)-1); | |
56febc5e AD |
1170 | # else |
1171 | # ifdef HAS_SETRUID | |
b28d0864 NIS |
1172 | if ((PL_delaymagic & DM_UID) == DM_RUID) { |
1173 | (void)setruid(PL_uid); | |
1174 | PL_delaymagic &= ~DM_RUID; | |
a0d0e21e | 1175 | } |
56febc5e AD |
1176 | # endif /* HAS_SETRUID */ |
1177 | # ifdef HAS_SETEUID | |
b28d0864 | 1178 | if ((PL_delaymagic & DM_UID) == DM_EUID) { |
6e67c536 | 1179 | (void)seteuid(PL_euid); |
b28d0864 | 1180 | PL_delaymagic &= ~DM_EUID; |
a0d0e21e | 1181 | } |
56febc5e | 1182 | # endif /* HAS_SETEUID */ |
b28d0864 NIS |
1183 | if (PL_delaymagic & DM_UID) { |
1184 | if (PL_uid != PL_euid) | |
cea2e8a9 | 1185 | DIE(aTHX_ "No setreuid available"); |
b28d0864 | 1186 | (void)PerlProc_setuid(PL_uid); |
a0d0e21e | 1187 | } |
56febc5e AD |
1188 | # endif /* HAS_SETREUID */ |
1189 | #endif /* HAS_SETRESUID */ | |
d8eceb89 JH |
1190 | PL_uid = PerlProc_getuid(); |
1191 | PL_euid = PerlProc_geteuid(); | |
a0d0e21e | 1192 | } |
3280af22 | 1193 | if (PL_delaymagic & DM_GID) { |
a0d0e21e | 1194 | #ifdef HAS_SETRESGID |
6e67c536 RD |
1195 | (void)setresgid((PL_delaymagic & DM_RGID) ? PL_gid : (Gid_t)-1, |
1196 | (PL_delaymagic & DM_EGID) ? PL_egid : (Gid_t)-1, | |
1197 | (Gid_t)-1); | |
56febc5e AD |
1198 | #else |
1199 | # ifdef HAS_SETREGID | |
6e67c536 RD |
1200 | (void)setregid((PL_delaymagic & DM_RGID) ? PL_gid : (Gid_t)-1, |
1201 | (PL_delaymagic & DM_EGID) ? PL_egid : (Gid_t)-1); | |
56febc5e AD |
1202 | # else |
1203 | # ifdef HAS_SETRGID | |
b28d0864 NIS |
1204 | if ((PL_delaymagic & DM_GID) == DM_RGID) { |
1205 | (void)setrgid(PL_gid); | |
1206 | PL_delaymagic &= ~DM_RGID; | |
a0d0e21e | 1207 | } |
56febc5e AD |
1208 | # endif /* HAS_SETRGID */ |
1209 | # ifdef HAS_SETEGID | |
b28d0864 | 1210 | if ((PL_delaymagic & DM_GID) == DM_EGID) { |
6e67c536 | 1211 | (void)setegid(PL_egid); |
b28d0864 | 1212 | PL_delaymagic &= ~DM_EGID; |
a0d0e21e | 1213 | } |
56febc5e | 1214 | # endif /* HAS_SETEGID */ |
b28d0864 NIS |
1215 | if (PL_delaymagic & DM_GID) { |
1216 | if (PL_gid != PL_egid) | |
cea2e8a9 | 1217 | DIE(aTHX_ "No setregid available"); |
b28d0864 | 1218 | (void)PerlProc_setgid(PL_gid); |
a0d0e21e | 1219 | } |
56febc5e AD |
1220 | # endif /* HAS_SETREGID */ |
1221 | #endif /* HAS_SETRESGID */ | |
d8eceb89 JH |
1222 | PL_gid = PerlProc_getgid(); |
1223 | PL_egid = PerlProc_getegid(); | |
a0d0e21e | 1224 | } |
3280af22 | 1225 | PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid)); |
a0d0e21e | 1226 | } |
3280af22 | 1227 | PL_delaymagic = 0; |
54310121 | 1228 | |
54310121 | 1229 | if (gimme == G_VOID) |
1230 | SP = firstrelem - 1; | |
1231 | else if (gimme == G_SCALAR) { | |
1232 | dTARGET; | |
1233 | SP = firstrelem; | |
48703b5e | 1234 | SETi(lastrelem - firstrelem + 1 - duplicates); |
54310121 | 1235 | } |
1236 | else { | |
48703b5e | 1237 | if (ary) |
a0d0e21e | 1238 | SP = lastrelem; |
48703b5e NC |
1239 | else if (hash) { |
1240 | if (duplicates) { | |
1241 | /* Removes from the stack the entries which ended up as | |
1242 | * duplicated keys in the hash (fix for [perl #24380]) */ | |
1243 | Move(firsthashrelem + duplicates, | |
1244 | firsthashrelem, duplicates, SV**); | |
1245 | lastrelem -= duplicates; | |
1246 | } | |
1247 | SP = lastrelem; | |
1248 | } | |
a0d0e21e LW |
1249 | else |
1250 | SP = firstrelem + (lastlelem - firstlelem); | |
0c8c7a05 | 1251 | lelem = firstlelem + (relem - firstrelem); |
5f05dabc | 1252 | while (relem <= SP) |
3280af22 | 1253 | *relem++ = (lelem <= lastlelem) ? *lelem++ : &PL_sv_undef; |
a0d0e21e | 1254 | } |
54310121 | 1255 | RETURN; |
a0d0e21e LW |
1256 | } |
1257 | ||
8782bef2 GB |
1258 | PP(pp_qr) |
1259 | { | |
39644a26 | 1260 | dSP; |
1a9219e7 AL |
1261 | register PMOP * const pm = cPMOP; |
1262 | SV * const rv = sv_newmortal(); | |
1263 | SV * const sv = newSVrv(rv, "Regexp"); | |
e08e52cf AMS |
1264 | if (pm->op_pmdynflags & PMdf_TAINTED) |
1265 | SvTAINTED_on(rv); | |
aaa362c4 | 1266 | sv_magic(sv,(SV*)ReREFCNT_inc(PM_GETRE(pm)), PERL_MAGIC_qr,0,0); |
8782bef2 GB |
1267 | RETURNX(PUSHs(rv)); |
1268 | } | |
1269 | ||
a0d0e21e LW |
1270 | PP(pp_match) |
1271 | { | |
39644a26 | 1272 | dSP; dTARG; |
a0d0e21e | 1273 | register PMOP *pm = cPMOP; |
d65afb4b | 1274 | PMOP *dynpm = pm; |
3bad88ff AL |
1275 | register const char *t; |
1276 | register const char *s; | |
8ec8dcb0 | 1277 | const char *strend; |
a0d0e21e | 1278 | I32 global; |
f722798b | 1279 | I32 r_flags = REXEC_CHECKED; |
8ec8dcb0 | 1280 | const char *truebase; /* Start of string */ |
aaa362c4 | 1281 | register REGEXP *rx = PM_GETRE(pm); |
b3eb6a9b | 1282 | bool rxtainted; |
8c18bf38 | 1283 | const I32 gimme = GIMME; |
a0d0e21e | 1284 | STRLEN len; |
748a9306 | 1285 | I32 minmatch = 0; |
8c18bf38 | 1286 | const I32 oldsave = PL_savestack_ix; |
f86702cc | 1287 | I32 update_minmatch = 1; |
e60df1fa | 1288 | I32 had_zerolen = 0; |
a0d0e21e | 1289 | |
533c011a | 1290 | if (PL_op->op_flags & OPf_STACKED) |
a0d0e21e LW |
1291 | TARG = POPs; |
1292 | else { | |
54b9620d | 1293 | TARG = DEFSV; |
a0d0e21e LW |
1294 | EXTEND(SP,1); |
1295 | } | |
d9f424b2 | 1296 | |
c277df42 | 1297 | PUTBACK; /* EVAL blocks need stack_sp. */ |
8ec8dcb0 | 1298 | s = SvPV_const(TARG, len); |
a0d0e21e | 1299 | if (!s) |
2269b42e | 1300 | DIE(aTHX_ "panic: pp_match"); |
c6d79d47 | 1301 | strend = s + len; |
b3eb6a9b | 1302 | rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) || |
3280af22 | 1303 | (PL_tainted && (pm->op_pmflags & PMf_RETAINT))); |
9212bbba | 1304 | TAINT_NOT; |
a0d0e21e | 1305 | |
2ea5368e | 1306 | RX_MATCH_UTF8_set(rx, DO_UTF8(TARG)); |
d9f424b2 | 1307 | |
d65afb4b | 1308 | /* PMdf_USED is set after a ?? matches once */ |
48c036b1 | 1309 | if (pm->op_pmdynflags & PMdf_USED) { |
c277df42 | 1310 | failure: |
a0d0e21e LW |
1311 | if (gimme == G_ARRAY) |
1312 | RETURN; | |
1313 | RETPUSHNO; | |
1314 | } | |
1315 | ||
d65afb4b | 1316 | /* empty pattern special-cased to use last successful pattern if possible */ |
3280af22 NIS |
1317 | if (!rx->prelen && PL_curpm) { |
1318 | pm = PL_curpm; | |
aaa362c4 | 1319 | rx = PM_GETRE(pm); |
a0d0e21e | 1320 | } |
d65afb4b | 1321 | |
eb160463 | 1322 | if (rx->minlen > (I32)len) |
d65afb4b | 1323 | goto failure; |
c277df42 | 1324 | |
a0d0e21e | 1325 | truebase = t = s; |
ad94a511 IZ |
1326 | |
1327 | /* XXXX What part of this is needed with true \G-support? */ | |
d65afb4b | 1328 | if ((global = dynpm->op_pmflags & PMf_GLOBAL)) { |
cf93c79d | 1329 | rx->startp[0] = -1; |
a0d0e21e | 1330 | if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) { |
f4362cdc | 1331 | MAGIC* const mg = mg_find(TARG, PERL_MAGIC_regex_global); |
565764a8 | 1332 | if (mg && mg->mg_len >= 0) { |
b7a35066 | 1333 | if (!(rx->reganch & ROPT_GPOS_SEEN)) |
1c846c1f | 1334 | rx->endp[0] = rx->startp[0] = mg->mg_len; |
0ef3e39e HS |
1335 | else if (rx->reganch & ROPT_ANCH_GPOS) { |
1336 | r_flags |= REXEC_IGNOREPOS; | |
1c846c1f | 1337 | rx->endp[0] = rx->startp[0] = mg->mg_len; |
0ef3e39e | 1338 | } |
748a9306 | 1339 | minmatch = (mg->mg_flags & MGf_MINMATCH); |
f86702cc | 1340 | update_minmatch = 0; |
748a9306 | 1341 | } |
a0d0e21e LW |
1342 | } |
1343 | } | |
14977893 JH |
1344 | if ((!global && rx->nparens) |
1345 | || SvTEMP(TARG) || PL_sawampersand) | |
1346 | r_flags |= REXEC_COPY_STR; | |
1c846c1f | 1347 | if (SvSCREAM(TARG)) |
22e551b9 IZ |
1348 | r_flags |= REXEC_SCREAM; |
1349 | ||
a0d0e21e | 1350 | if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) { |
3280af22 NIS |
1351 | SAVEINT(PL_multiline); |
1352 | PL_multiline = pm->op_pmflags & PMf_MULTILINE; | |
a0d0e21e LW |
1353 | } |
1354 | ||
1355 | play_it_again: | |
cf93c79d IZ |
1356 | if (global && rx->startp[0] != -1) { |
1357 | t = s = rx->endp[0] + truebase; | |
d9f97599 | 1358 | if ((s + rx->minlen) > strend) |
a0d0e21e | 1359 | goto nope; |
f86702cc | 1360 | if (update_minmatch++) |
e60df1fa | 1361 | minmatch = had_zerolen; |
a0d0e21e | 1362 | } |
60aeb6fd NIS |
1363 | if (rx->reganch & RE_USE_INTUIT && |
1364 | DO_UTF8(TARG) == ((rx->reganch & ROPT_UTF8) != 0)) { | |
8ec8dcb0 NC |
1365 | /* FIXME - can PL_bostr be made const char *? */ |
1366 | PL_bostr = (char *)truebase; | |
1367 | s = CALLREG_INTUIT_START(aTHX_ rx, TARG, (char *)s, (char *)strend, r_flags, NULL); | |
f722798b IZ |
1368 | |
1369 | if (!s) | |
1370 | goto nope; | |
1371 | if ( (rx->reganch & ROPT_CHECK_ALL) | |
14977893 | 1372 | && !PL_sawampersand |
f722798b IZ |
1373 | && ((rx->reganch & ROPT_NOSCAN) |
1374 | || !((rx->reganch & RE_INTUIT_TAIL) | |
05b4157f GS |
1375 | && (r_flags & REXEC_SCREAM))) |
1376 | && !SvROK(TARG)) /* Cannot trust since INTUIT cannot guess ^ */ | |
f722798b | 1377 | goto yup; |
a0d0e21e | 1378 | } |
8ec8dcb0 | 1379 | if (CALLREGEXEC(aTHX_ rx, (char*)s, (char *)strend, (char*)truebase, minmatch, TARG, NULL, r_flags)) |
bbce6d69 | 1380 | { |
3280af22 | 1381 | PL_curpm = pm; |
d65afb4b HS |
1382 | if (dynpm->op_pmflags & PMf_ONCE) |
1383 | dynpm->op_pmdynflags |= PMdf_USED; | |
a0d0e21e LW |
1384 | goto gotcha; |
1385 | } | |
1386 | else | |
1387 | goto ret_no; | |
1388 | /*NOTREACHED*/ | |
1389 | ||
1390 | gotcha: | |
72311751 GS |
1391 | if (rxtainted) |
1392 | RX_MATCH_TAINTED_on(rx); | |
1393 | TAINT_IF(RX_MATCH_TAINTED(rx)); | |
a0d0e21e | 1394 | if (gimme == G_ARRAY) { |
8c18bf38 AL |
1395 | const I32 nparens = rx->nparens; |
1396 | I32 i = (global && !nparens) ? 1 : 0; | |
a0d0e21e | 1397 | |
c277df42 | 1398 | SPAGAIN; /* EVAL blocks could move the stack. */ |
ffc61ed2 JH |
1399 | EXTEND(SP, nparens + i); |
1400 | EXTEND_MORTAL(nparens + i); | |
1401 | for (i = !i; i <= nparens; i++) { | |
a0d0e21e | 1402 | PUSHs(sv_newmortal()); |
cf93c79d | 1403 | if ((rx->startp[i] != -1) && rx->endp[i] != -1 ) { |
481da01c | 1404 | const I32 len = rx->endp[i] - rx->startp[i]; |
e62e6cf9 | 1405 | s = rx->startp[i] + truebase; |
290deeac A |
1406 | if (rx->endp[i] < 0 || rx->startp[i] < 0 || |
1407 | len < 0 || len > strend - s) | |
1408 | DIE(aTHX_ "panic: pp_match start/end pointers"); | |
a0d0e21e | 1409 | sv_setpvn(*SP, s, len); |
cce850e4 | 1410 | if (DO_UTF8(TARG) && is_utf8_string((U8*)s, len)) |
a197cbdd | 1411 | SvUTF8_on(*SP); |
a0d0e21e LW |
1412 | } |
1413 | } | |
1414 | if (global) { | |
d65afb4b | 1415 | if (dynpm->op_pmflags & PMf_CONTINUE) { |
8916b559 | 1416 | MAGIC* mg = NULL; |
0af80b60 HS |
1417 | if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) |
1418 | mg = mg_find(TARG, PERL_MAGIC_regex_global); | |
1419 | if (!mg) { | |
8916b559 | 1420 | sv_magic(TARG, NULL, PERL_MAGIC_regex_global, NULL, 0); |
0af80b60 HS |
1421 | mg = mg_find(TARG, PERL_MAGIC_regex_global); |
1422 | } | |
1423 | if (rx->startp[0] != -1) { | |
1424 | mg->mg_len = rx->endp[0]; | |
1425 | if (rx->startp[0] == rx->endp[0]) | |
1426 | mg->mg_flags |= MGf_MINMATCH; | |
1427 | else | |
1428 | mg->mg_flags &= ~MGf_MINMATCH; | |
1429 | } | |
1430 | } | |
cf93c79d IZ |
1431 | had_zerolen = (rx->startp[0] != -1 |
1432 | && rx->startp[0] == rx->endp[0]); | |
c277df42 | 1433 | PUTBACK; /* EVAL blocks may use stack */ |
cf93c79d | 1434 | r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST; |
a0d0e21e LW |
1435 | goto play_it_again; |
1436 | } | |
ffc61ed2 | 1437 | else if (!nparens) |
bde848c5 | 1438 | XPUSHs(&PL_sv_yes); |
4633a7c4 | 1439 | LEAVE_SCOPE(oldsave); |
a0d0e21e LW |
1440 | RETURN; |
1441 | } | |
1442 | else { | |
1443 | if (global) { | |
4358d444 | 1444 | MAGIC* mg; |
a0d0e21e | 1445 | if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) |
14befaf4 | 1446 | mg = mg_find(TARG, PERL_MAGIC_regex_global); |
4358d444 NC |
1447 | else |
1448 | mg = NULL; | |
a0d0e21e | 1449 | if (!mg) { |
8916b559 | 1450 | sv_magic(TARG, NULL, PERL_MAGIC_regex_global, NULL, 0); |
14befaf4 | 1451 | mg = mg_find(TARG, PERL_MAGIC_regex_global); |
a0d0e21e | 1452 | } |
cf93c79d IZ |
1453 | if (rx->startp[0] != -1) { |
1454 | mg->mg_len = rx->endp[0]; | |
d9f97599 | 1455 | if (rx->startp[0] == rx->endp[0]) |
748a9306 LW |
1456 | mg->mg_flags |= MGf_MINMATCH; |
1457 | else | |
1458 | mg->mg_flags &= ~MGf_MINMATCH; | |
1459 | } | |
a0d0e21e | 1460 | } |
4633a7c4 | 1461 | LEAVE_SCOPE(oldsave); |
a0d0e21e LW |
1462 | RETPUSHYES; |
1463 | } | |
1464 | ||
f722798b | 1465 | yup: /* Confirmed by INTUIT */ |
72311751 GS |
1466 | if (rxtainted) |
1467 | RX_MATCH_TAINTED_on(rx); | |
1468 | TAINT_IF(RX_MATCH_TAINTED(rx)); | |
3280af22 | 1469 | PL_curpm = pm; |
d65afb4b HS |
1470 | if (dynpm->op_pmflags & PMf_ONCE) |
1471 | dynpm->op_pmdynflags |= PMdf_USED; | |
cf93c79d IZ |
1472 | if (RX_MATCH_COPIED(rx)) |
1473 | Safefree(rx->subbeg); | |
1474 | RX_MATCH_COPIED_off(rx); | |
8916b559 | 1475 | rx->subbeg = NULL; |
a0d0e21e | 1476 | if (global) { |
8ec8dcb0 NC |
1477 | /* FIXME - should rx->subbeg be const char *? */ |
1478 | rx->subbeg = (char *) truebase; | |
cf93c79d | 1479 | rx->startp[0] = s - truebase; |
2ea5368e | 1480 | if (RX_MATCH_UTF8(rx)) { |
7508116b | 1481 | char * const t = (char*)utf8_hop((U8*)s, rx->minlen); |
60aeb6fd NIS |
1482 | rx->endp[0] = t - truebase; |
1483 | } | |
1484 | else { | |
1485 | rx->endp[0] = s - truebase + rx->minlen; | |
1486 | } | |
cf93c79d | 1487 | rx->sublen = strend - truebase; |
a0d0e21e | 1488 | goto gotcha; |
1c846c1f | 1489 | } |
14977893 JH |
1490 | if (PL_sawampersand) { |
1491 | I32 off; | |
1492 | ||
1493 | rx->subbeg = savepvn(t, strend - t); | |
1494 | rx->sublen = strend - t; | |
1495 | RX_MATCH_COPIED_on(rx); | |
1496 | off = rx->startp[0] = s - t; | |
1497 | rx->endp[0] = off + rx->minlen; | |
1498 | } | |
1499 | else { /* startp/endp are used by @- @+. */ | |
1500 | rx->startp[0] = s - truebase; | |
1501 | rx->endp[0] = s - truebase + rx->minlen; | |
1502 | } | |
4e58e0cf | 1503 | rx->nparens = rx->lastparen = rx->lastcloseparen = 0; /* used by @-, @+, and $^N */ |
4633a7c4 | 1504 | LEAVE_SCOPE(oldsave); |
a0d0e21e LW |
1505 | RETPUSHYES; |
1506 | ||
1507 | nope: | |
a0d0e21e | 1508 | ret_no: |
d65afb4b | 1509 | if (global && !(dynpm->op_pmflags & PMf_CONTINUE)) { |
a0d0e21e | 1510 | if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) { |
8916b559 | 1511 | MAGIC* const mg = mg_find(TARG, PERL_MAGIC_regex_global); |
a0d0e21e | 1512 | if (mg) |
565764a8 | 1513 | mg->mg_len = -1; |
a0d0e21e LW |
1514 | } |
1515 | } | |
4633a7c4 | 1516 | LEAVE_SCOPE(oldsave); |
a0d0e21e LW |
1517 | if (gimme == G_ARRAY) |
1518 | RETURN; | |
1519 | RETPUSHNO; | |
1520 | } | |
1521 | ||
1522 | OP * | |
864dbfa3 | 1523 | Perl_do_readline(pTHX) |
a0d0e21e LW |
1524 | { |
1525 | dSP; dTARGETSTACKED; | |
1526 | register SV *sv; | |
1527 | STRLEN tmplen = 0; | |
1528 | STRLEN offset; | |
760ac839 | 1529 | PerlIO *fp; |
8c18bf38 AL |
1530 | register IO * const io = GvIO(PL_last_in_gv); |
1531 | register const I32 type = PL_op->op_type; | |
1532 | const I32 gimme = GIMME_V; | |
a0d0e21e | 1533 | |
8916b559 NC |
1534 | if (io) { |
1535 | MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar); | |
1536 | if (mg) { | |
1537 | PUSHMARK(SP); | |
1538 | XPUSHs(SvTIED_obj((SV*)io, mg)); | |
1539 | PUTBACK; | |
1540 | ENTER; | |
1541 | call_method("READLINE", gimme); | |
1542 | LEAVE; | |
1543 | SPAGAIN; | |
1544 | if (gimme == G_SCALAR) { | |
1545 | SV* const result = POPs; | |
1546 | SvSetSV_nosteal(TARG, result); | |
1547 | PUSHTARG; | |
1548 | } | |
1549 | RETURN; | |
5b7ea690 | 1550 | } |
e79b0511 | 1551 | } |
0e2d6244 | 1552 | fp = NULL; |
a0d0e21e LW |
1553 | if (io) { |
1554 | fp = IoIFP(io); | |
1555 | if (!fp) { | |
1556 | if (IoFLAGS(io) & IOf_ARGV) { | |
1557 | if (IoFLAGS(io) & IOf_START) { | |
a0d0e21e | 1558 | IoLINES(io) = 0; |
3280af22 | 1559 | if (av_len(GvAVn(PL_last_in_gv)) < 0) { |
1d7c1841 | 1560 | IoFLAGS(io) &= ~IOf_START; |
0e2d6244 | 1561 | do_open(PL_last_in_gv,"-",1,FALSE,O_RDONLY,0,NULL); |
83fd6193 | 1562 | sv_setpvn(GvSVn(PL_last_in_gv), "-", 1); |
3280af22 | 1563 | SvSETMAGIC(GvSV(PL_last_in_gv)); |
a2008d6d GS |
1564 | fp = IoIFP(io); |
1565 | goto have_fp; | |
a0d0e21e LW |
1566 | } |
1567 | } | |
3280af22 | 1568 | fp = nextargv(PL_last_in_gv); |
a0d0e21e | 1569 | if (!fp) { /* Note: fp != IoIFP(io) */ |
3280af22 | 1570 | (void)do_close(PL_last_in_gv, FALSE); /* now it does*/ |
a0d0e21e LW |
1571 | } |
1572 | } | |
0d44d22b NC |
1573 | else if (type == OP_GLOB) |
1574 | fp = Perl_start_glob(aTHX_ POPs, io); | |
a0d0e21e LW |
1575 | } |
1576 | else if (type == OP_GLOB) | |
1577 | SP--; | |
a00b5bd3 | 1578 | else if (ckWARN(WARN_IO) && IoTYPE(io) == IoTYPE_WRONLY) { |
4c80c0b2 | 1579 | report_evil_fh(PL_last_in_gv, io, OP_phoney_OUTPUT_ONLY); |
a00b5bd3 | 1580 | } |
a0d0e21e LW |
1581 | } |
1582 | if (!fp) { | |
f5e9f069 NC |
1583 | if ((!io || !(IoFLAGS(io) & IOf_START)) |
1584 | && ckWARN2(WARN_GLOB, WARN_CLOSED)) | |
1585 | { | |
3f4520fe | 1586 | if (type == OP_GLOB) |
9014280d | 1587 | Perl_warner(aTHX_ packWARN(WARN_GLOB), |
af8c498a GS |
1588 | "glob failed (can't start child: %s)", |
1589 | Strerror(errno)); | |
69282e91 | 1590 | else |
bc37a18f | 1591 | report_evil_fh(PL_last_in_gv, io, PL_op->op_type); |
3f4520fe | 1592 | } |
54310121 | 1593 | if (gimme == G_SCALAR) { |
ff05d6ab | 1594 | /* undef TARG, and push that undefined value */ |
1844fdae JH |
1595 | if (type != OP_RCATLINE) { |
1596 | SV_CHECK_THINKFIRST(TARG); | |
7460c263 | 1597 | SvOK_off(TARG); |
1844fdae | 1598 | } |
a0d0e21e LW |
1599 | PUSHTARG; |
1600 | } | |
1601 | RETURN; | |
1602 | } | |
a2008d6d | 1603 | have_fp: |
54310121 | 1604 | if (gimme == G_SCALAR) { |
a0d0e21e | 1605 | sv = TARG; |
9607fc9c | 1606 | if (SvROK(sv)) |
1607 | sv_unref(sv); | |
a0d0e21e LW |
1608 | (void)SvUPGRADE(sv, SVt_PV); |
1609 | tmplen = SvLEN(sv); /* remember if already alloced */ | |
1624793d | 1610 | if (!tmplen && !SvREADONLY(sv)) |
a0d0e21e | 1611 | Sv_Grow(sv, 80); /* try short-buffering it */ |
5b7ea690 JH |
1612 | offset = 0; |
1613 | if (type == OP_RCATLINE && SvOK(sv)) { | |
1614 | if (!SvPOK(sv)) { | |
db1c9db1 | 1615 | SvPV_force_nolen(sv); |
5b7ea690 | 1616 | } |
a0d0e21e | 1617 | offset = SvCUR(sv); |
5b7ea690 | 1618 | } |
a0d0e21e | 1619 | } |
54310121 | 1620 | else { |
133cdda0 | 1621 | sv = sv_2mortal(newSV(80)); |
54310121 | 1622 | offset = 0; |
1623 | } | |
fbad3eb5 | 1624 | |
3887d568 AP |
1625 | /* This should not be marked tainted if the fp is marked clean */ |
1626 | #define MAYBE_TAINT_LINE(io, sv) \ | |
1627 | if (!(IoFLAGS(io) & IOf_UNTAINT)) { \ | |
1628 | TAINT; \ | |
1629 | SvTAINTED_on(sv); \ | |
1630 | } | |
1631 | ||
684bef36 | 1632 | /* delay EOF state for a snarfed empty file */ |
fbad3eb5 | 1633 | #define SNARF_EOF(gimme,rs,io,sv) \ |
684bef36 | 1634 | (gimme != G_SCALAR || SvCUR(sv) \ |
b9fee9ba | 1635 | || (IoFLAGS(io) & IOf_NOLINE) || !RsSNARF(rs)) |
fbad3eb5 | 1636 | |
a0d0e21e | 1637 | for (;;) { |
09e8efcc | 1638 | PUTBACK; |
fbad3eb5 | 1639 | if (!sv_gets(sv, fp, offset) |
1624793d NC |
1640 | && (type == OP_GLOB |
1641 | || SNARF_EOF(gimme, PL_rs, io, sv) | |
1642 | || PerlIO_error(fp))) | |
fbad3eb5 | 1643 | { |
760ac839 | 1644 | PerlIO_clearerr(fp); |
a0d0e21e | 1645 | if (IoFLAGS(io) & IOf_ARGV) { |
3280af22 | 1646 | fp = nextargv(PL_last_in_gv); |
a0d0e21e LW |
1647 | if (fp) |
1648 | continue; | |
3280af22 | 1649 | (void)do_close(PL_last_in_gv, FALSE); |
a0d0e21e LW |
1650 | } |
1651 | else if (type == OP_GLOB) { | |
e476b1b5 | 1652 | if (!do_close(PL_last_in_gv, FALSE) && ckWARN(WARN_GLOB)) { |
9014280d | 1653 | Perl_warner(aTHX_ packWARN(WARN_GLOB), |
4eb79ab5 | 1654 | "glob failed (child exited with status %d%s)", |
894356b3 | 1655 | (int)(STATUS_CURRENT >> 8), |
cf494569 | 1656 | (STATUS_CURRENT & 0x80) ? ", core dumped" : ""); |
4eb79ab5 | 1657 | } |
a0d0e21e | 1658 | } |
54310121 | 1659 | if (gimme == G_SCALAR) { |
1844fdae JH |
1660 | if (type != OP_RCATLINE) { |
1661 | SV_CHECK_THINKFIRST(TARG); | |
7460c263 | 1662 | SvOK_off(TARG); |
1844fdae | 1663 | } |
09e8efcc | 1664 | SPAGAIN; |
a0d0e21e LW |
1665 | PUSHTARG; |
1666 | } | |
3887d568 | 1667 | MAYBE_TAINT_LINE(io, sv); |
a0d0e21e LW |
1668 | RETURN; |
1669 | } | |
3887d568 | 1670 | MAYBE_TAINT_LINE(io, sv); |
a0d0e21e | 1671 | IoLINES(io)++; |
b9fee9ba | 1672 | IoFLAGS(io) |= IOf_NOLINE; |
71be2cbc | 1673 | SvSETMAGIC(sv); |
09e8efcc | 1674 | SPAGAIN; |
a0d0e21e | 1675 | XPUSHs(sv); |
a0d0e21e | 1676 | if (type == OP_GLOB) { |
23c2bb70 | 1677 | const char *t1; |
a0d0e21e | 1678 | |
3280af22 | 1679 | if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) { |
8916b559 | 1680 | char * const tmps = SvEND(sv) - 1; |
fdac8c4b | 1681 | if (*tmps == *SvPVX_const(PL_rs)) { |
c07a80fd | 1682 | *tmps = '\0'; |
a8dc4fe8 | 1683 | SvCUR_set(sv, SvCUR(sv) - 1); |
c07a80fd | 1684 | } |
1685 | } | |
23c2bb70 NC |
1686 | for (t1 = SvPVX_const(sv); *t1; t1++) |
1687 | if (!isALPHA(*t1) && !isDIGIT(*t1) && | |
1688 | strchr("$&*(){}[]'\";\\|?<>~`", *t1)) | |
a0d0e21e | 1689 | break; |
23c2bb70 | 1690 | if (*t1 && PerlLIO_lstat(SvPVX_const(sv), &PL_statbuf) < 0) { |
a0d0e21e LW |
1691 | (void)POPs; /* Unmatched wildcard? Chuck it... */ |
1692 | continue; | |
1693 | } | |
9c20fa4a | 1694 | } else if (SvUTF8(sv)) { /* OP_READLINE, OP_RCATLINE */ |
f4362cdc | 1695 | const U8 * const s = (const U8*)SvPVX_const(sv) + offset; |
1f49be52 AL |
1696 | const STRLEN len = SvCUR(sv) - offset; |
1697 | const U8 *f; | |
9c20fa4a JH |
1698 | |
1699 | if (ckWARN(WARN_UTF8) && | |
b9701ede | 1700 | !is_utf8_string_loc((U8 *) s, len, &f)) |
9c20fa4a JH |
1701 | /* Emulate :encoding(utf8) warning in the same case. */ |
1702 | Perl_warner(aTHX_ packWARN(WARN_UTF8), | |
1703 | "utf8 \"\\x%02X\" does not map to Unicode", | |
1704 | f < (U8*)SvEND(sv) ? *f : 0); | |
a0d0e21e | 1705 | } |
54310121 | 1706 | if (gimme == G_ARRAY) { |
a0d0e21e | 1707 | if (SvLEN(sv) - SvCUR(sv) > 20) { |
ea5389ca | 1708 | SvPV_shrink_to_cur(sv); |
a0d0e21e | 1709 | } |
133cdda0 | 1710 | sv = sv_2mortal(newSV(80)); |
a0d0e21e LW |
1711 | continue; |
1712 | } | |
54310121 | 1713 | else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) { |
a0d0e21e | 1714 | /* try to reclaim a bit of scalar space (only on 1st alloc) */ |
2a8de9e2 AL |
1715 | const STRLEN new_len |
1716 | = SvCUR(sv) < 60 ? 80 : SvCUR(sv)+40; /* allow some slop */ | |
ea5389ca | 1717 | SvPV_renew(sv, new_len); |
a0d0e21e LW |
1718 | } |
1719 | RETURN; | |
1720 | } | |
1721 | } | |
1722 | ||
1723 | PP(pp_enter) | |
1724 | { | |
39644a26 | 1725 | dSP; |
c09156bb | 1726 | register PERL_CONTEXT *cx; |
533c011a | 1727 | I32 gimme = OP_GIMME(PL_op, -1); |
a0d0e21e | 1728 | |
54310121 | 1729 | if (gimme == -1) { |
1730 | if (cxstack_ix >= 0) | |
1731 | gimme = cxstack[cxstack_ix].blk_gimme; | |
1732 | else | |
1733 | gimme = G_SCALAR; | |
1734 | } | |
a0d0e21e LW |
1735 | |
1736 | ENTER; | |
1737 | ||
1738 | SAVETMPS; | |
924508f0 | 1739 | PUSHBLOCK(cx, CXt_BLOCK, SP); |
a0d0e21e LW |
1740 | |
1741 | RETURN; | |
1742 | } | |
1743 | ||
1744 | PP(pp_helem) | |
1745 | { | |
39644a26 | 1746 | dSP; |
760ac839 | 1747 | HE* he; |
ae77835f | 1748 | SV **svp; |
f4362cdc AL |
1749 | SV * const keysv = POPs; |
1750 | HV * const hv = (HV*)POPs; | |
8c18bf38 AL |
1751 | const U32 lval = PL_op->op_flags & OPf_MOD || LVRET; |
1752 | const U32 defer = PL_op->op_private & OPpLVAL_DEFER; | |
be6c24e0 | 1753 | SV *sv; |
2a979b61 | 1754 | const U32 hash = (SvIsCOW_shared_hash(keysv)) ? SvSHARED_HASH(keysv) : 0; |
9c5ffd7c | 1755 | I32 preeminent = 0; |
a0d0e21e | 1756 | |
ae77835f | 1757 | if (SvTYPE(hv) == SVt_PVHV) { |
8d1f198f DM |
1758 | if (PL_op->op_private & OPpLVAL_INTRO) { |
1759 | MAGIC *mg; | |
1760 | HV *stash; | |
1761 | /* does the element we're localizing already exist? */ | |
c39e6ab0 | 1762 | preeminent = |
8d1f198f DM |
1763 | /* can we determine whether it exists? */ |
1764 | ( !SvRMAGICAL(hv) | |
1765 | || mg_find((SV*)hv, PERL_MAGIC_env) | |
1766 | || ( (mg = mg_find((SV*)hv, PERL_MAGIC_tied)) | |
1767 | /* Try to preserve the existenceness of a tied hash | |
1768 | * element by using EXISTS and DELETE if possible. | |
1769 | * Fallback to FETCH and STORE otherwise */ | |
1770 | && (stash = SvSTASH(SvRV(SvTIED_obj((SV*)hv, mg)))) | |
1771 | && gv_fetchmethod_autoload(stash, "EXISTS", TRUE) | |
1772 | && gv_fetchmethod_autoload(stash, "DELETE", TRUE) | |
1773 | ) | |
1774 | ) ? hv_exists_ent(hv, keysv, 0) : 1; | |
c39e6ab0 | 1775 | |
8d1f198f | 1776 | } |
1c846c1f | 1777 | he = hv_fetch_ent(hv, keysv, lval && !defer, hash); |
8916b559 | 1778 | svp = he ? &HeVAL(he) : NULL; |
ae77835f MB |
1779 | } |
1780 | else if (SvTYPE(hv) == SVt_PVAV) { | |
0ebe0038 | 1781 | if (PL_op->op_private & OPpLVAL_INTRO) |
cea2e8a9 | 1782 | DIE(aTHX_ "Can't localize pseudo-hash element"); |
1c846c1f | 1783 | svp = avhv_fetch_ent((AV*)hv, keysv, lval && !defer, hash); |
ae77835f | 1784 | } |
c750a3ec | 1785 | else { |
a0d0e21e | 1786 | RETPUSHUNDEF; |
c750a3ec | 1787 | } |
a0d0e21e | 1788 | if (lval) { |
3280af22 | 1789 | if (!svp || *svp == &PL_sv_undef) { |
68dc0745 | 1790 | SV* lv; |
1791 | SV* key2; | |
2d8e6c8d | 1792 | if (!defer) { |
6b53674b | 1793 | DIE(aTHX_ PL_no_helem_sv, keysv); |
2d8e6c8d | 1794 | } |
68dc0745 | 1795 | lv = sv_newmortal(); |
1796 | sv_upgrade(lv, SVt_PVLV); | |
1797 | LvTYPE(lv) = 'y'; | |
8916b559 | 1798 | sv_magic(lv, key2 = newSVsv(keysv), PERL_MAGIC_defelem, NULL, 0); |
68dc0745 | 1799 | SvREFCNT_dec(key2); /* sv_magic() increments refcount */ |
be2d5e07 | 1800 | LvTARG(lv) = SvREFCNT_inc_simple(hv); |
68dc0745 | 1801 | LvTARGLEN(lv) = 1; |
1802 | PUSHs(lv); | |
1803 | RETURN; | |
1804 | } | |
533c011a | 1805 | if (PL_op->op_private & OPpLVAL_INTRO) { |
26ab6a78 | 1806 | if (HvNAME_get(hv) && isGV(*svp)) |
533c011a | 1807 | save_gp((GV*)*svp, !(PL_op->op_flags & OPf_SPECIAL)); |
1f5346dc SC |
1808 | else { |
1809 | if (!preeminent) { | |
1810 | STRLEN keylen; | |
c06c673c | 1811 | const char * const key = SvPV_const(keysv, keylen); |
57813020 | 1812 | SAVEDELETE(hv, savepvn(key,keylen), keylen); |
bfc4de9f | 1813 | } else |
1f5346dc SC |
1814 | save_helem(hv, keysv, svp); |
1815 | } | |
5f05dabc | 1816 | } |
533c011a NIS |
1817 | else if (PL_op->op_private & OPpDEREF) |
1818 | vivify_ref(*svp, PL_op->op_private & OPpDEREF); | |
a0d0e21e | 1819 | } |
3280af22 | 1820 | sv = (svp ? *svp : &PL_sv_undef); |
be6c24e0 GS |
1821 | /* This makes C<local $tied{foo} = $tied{foo}> possible. |
1822 | * Pushing the magical RHS on to the stack is useless, since | |
1823 | * that magic is soon destined to be misled by the local(), | |
1824 | * and thus the later pp_sassign() will fail to mg_get() the | |
1825 | * old value. This should also cure problems with delayed | |
1826 | * mg_get()s. GSAR 98-07-03 */ | |
1827 | if (!lval && SvGMAGICAL(sv)) | |
1828 | sv = sv_mortalcopy(sv); | |
1829 | PUSHs(sv); | |
a0d0e21e LW |
1830 | RETURN; |
1831 | } | |
1832 | ||
1833 | PP(pp_leave) | |
1834 | { | |
39644a26 | 1835 | dSP; |
c09156bb | 1836 | register PERL_CONTEXT *cx; |
a0d0e21e LW |
1837 | SV **newsp; |
1838 | PMOP *newpm; | |
1839 | I32 gimme; | |
1840 | ||
533c011a | 1841 | if (PL_op->op_flags & OPf_SPECIAL) { |
a0d0e21e | 1842 | cx = &cxstack[cxstack_ix]; |
3280af22 | 1843 | cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */ |
a0d0e21e LW |
1844 | } |
1845 | ||
1846 | POPBLOCK(cx,newpm); | |
1847 | ||
533c011a | 1848 | gimme = OP_GIMME(PL_op, -1); |
54310121 | 1849 | if (gimme == -1) { |
1850 | if (cxstack_ix >= 0) | |
1851 | gimme = cxstack[cxstack_ix].blk_gimme; | |
1852 | else | |
1853 | gimme = G_SCALAR; | |
1854 | } | |
a0d0e21e | 1855 | |
a1f49e72 | 1856 | TAINT_NOT; |
54310121 | 1857 | if (gimme == G_VOID) |
1858 | SP = newsp; | |
1859 | else if (gimme == G_SCALAR) { | |
8c18bf38 | 1860 | register SV **mark; |
54310121 | 1861 | MARK = newsp + 1; |
09256e2f | 1862 | if (MARK <= SP) { |
54310121 | 1863 | if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP)) |
1864 | *MARK = TOPs; | |
1865 | else | |
1866 | *MARK = sv_mortalcopy(TOPs); | |
09256e2f | 1867 | } else { |
54310121 | 1868 | MEXTEND(mark,0); |
3280af22 | 1869 | *MARK = &PL_sv_undef; |
a0d0e21e | 1870 | } |
54310121 | 1871 | SP = MARK; |
a0d0e21e | 1872 | } |
54310121 | 1873 | else if (gimme == G_ARRAY) { |
a1f49e72 | 1874 | /* in case LEAVE wipes old return values */ |
8c18bf38 | 1875 | register SV **mark; |
a1f49e72 CS |
1876 | for (mark = newsp + 1; mark <= SP; mark++) { |
1877 | if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) { | |
a0d0e21e | 1878 | *mark = sv_mortalcopy(*mark); |
a1f49e72 CS |
1879 | TAINT_NOT; /* Each item is independent */ |
1880 | } | |
1881 | } | |
a0d0e21e | 1882 | } |
3280af22 | 1883 | PL_curpm = newpm; /* Don't pop $1 et al till now */ |
a0d0e21e LW |
1884 | |
1885 | LEAVE; | |
1886 | ||
1887 | RETURN; | |
1888 | } | |
1889 | ||
1890 | PP(pp_iter) | |
1891 | { | |
39644a26 | 1892 | dSP; |
c09156bb | 1893 | register PERL_CONTEXT *cx; |
8b3a4b74 | 1894 | SV *sv, *oldsv; |
4633a7c4 | 1895 | AV* av; |
1d7c1841 | 1896 | SV **itersvp; |
a0d0e21e | 1897 | |
924508f0 | 1898 | EXTEND(SP, 1); |
a0d0e21e | 1899 | cx = &cxstack[cxstack_ix]; |
6b35e009 | 1900 | if (CxTYPE(cx) != CXt_LOOP) |
cea2e8a9 | 1901 | DIE(aTHX_ "panic: pp_iter"); |
a0d0e21e | 1902 | |
1d7c1841 | 1903 | itersvp = CxITERVAR(cx); |
4633a7c4 | 1904 | av = cx->blk_loop.iterary; |
89ea2908 GA |
1905 | if (SvTYPE(av) != SVt_PVAV) { |
1906 | /* iterate ($min .. $max) */ | |
1907 | if (cx->blk_loop.iterlval) { | |
1908 | /* string increment */ | |
1909 | register SV* cur = cx->blk_loop.iterlval; | |
249829d9 | 1910 | STRLEN maxlen = 0; |
c06c673c | 1911 | const char *max = SvOK((SV*)av) ? SvPV_const((SV*)av, maxlen) : ""; |
89ea2908 | 1912 | if (!SvNIOK(cur) && SvCUR(cur) <= maxlen) { |
4d1ff10f | 1913 | #ifndef USE_5005THREADS /* don't risk potential race */ |
1d7c1841 | 1914 | if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) { |
eaa5c2d6 | 1915 | /* safe to reuse old SV */ |
1d7c1841 | 1916 | sv_setsv(*itersvp, cur); |
eaa5c2d6 | 1917 | } |
1c846c1f | 1918 | else |
eaa5c2d6 GA |
1919 | #endif |
1920 | { | |
1921 | /* we need a fresh SV every time so that loop body sees a | |
1922 | * completely new SV for closures/references to work as | |
1923 | * they used to */ | |
8b3a4b74 | 1924 | oldsv = *itersvp; |
1d7c1841 | 1925 | *itersvp = newSVsv(cur); |
8b3a4b74 | 1926 | SvREFCNT_dec(oldsv); |
eaa5c2d6 | 1927 | } |
fdac8c4b | 1928 | if (strEQ(SvPVX_const(cur), max)) |
89ea2908 GA |
1929 | sv_setiv(cur, 0); /* terminate next time */ |
1930 | else | |
1931 | sv_inc(cur); | |
1932 | RETPUSHYES; | |
1933 | } | |
1934 | RETPUSHNO; | |
1935 | } | |
1936 | /* integer increment */ | |
1937 | if (cx->blk_loop.iterix > cx->blk_loop.itermax) | |
1938 | RETPUSHNO; | |
7f61b687 | 1939 | |
4d1ff10f | 1940 | #ifndef USE_5005THREADS /* don't risk potential race */ |
1d7c1841 | 1941 | if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) { |
eaa5c2d6 | 1942 | /* safe to reuse old SV */ |
1d7c1841 | 1943 | sv_setiv(*itersvp, cx->blk_loop.iterix++); |
eaa5c2d6 | 1944 | } |
1c846c1f | 1945 | else |
eaa5c2d6 GA |
1946 | #endif |
1947 | { | |
1948 | /* we need a fresh SV every time so that loop body sees a | |
1949 | * completely new SV for closures/references to work as they | |
1950 | * used to */ | |
8b3a4b74 | 1951 | oldsv = *itersvp; |
1d7c1841 | 1952 | *itersvp = newSViv(cx->blk_loop.iterix++); |
8b3a4b74 | 1953 | SvREFCNT_dec(oldsv); |
eaa5c2d6 | 1954 | } |
89ea2908 GA |
1955 | RETPUSHYES; |
1956 | } | |
1957 | ||
1958 | /* iterate array */ | |
9f6a40a7 NC |
1959 | if (PL_op->op_private & OPpITER_REVERSED) { |
1960 | /* In reverse, use itermax as the min :-) */ | |
1961 | if (cx->blk_loop.iterix <= cx->blk_loop.itermax) | |
1962 | RETPUSHNO; | |
a0d0e21e | 1963 | |
9f6a40a7 | 1964 | if (SvMAGICAL(av) || AvREIFY(av)) { |
f4362cdc | 1965 | SV * const * const svp = av_fetch(av, --cx->blk_loop.iterix, FALSE); |
0e2d6244 | 1966 | sv = svp ? *svp : NULL; |
9f6a40a7 NC |
1967 | } |
1968 | else { | |
7b146116 | 1969 | sv = AvARRAY(av)[--cx->blk_loop.iterix]; |
9f6a40a7 | 1970 | } |
d42935ef JH |
1971 | } |
1972 | else { | |
9f6a40a7 NC |
1973 | if (cx->blk_loop.iterix >= (av == PL_curstack ? cx->blk_oldsp : |
1974 | AvFILL(av))) | |
1975 | RETPUSHNO; | |
1976 | ||
1977 | if (SvMAGICAL(av) || AvREIFY(av)) { | |
f4362cdc | 1978 | SV * const * const svp = av_fetch(av, ++cx->blk_loop.iterix, FALSE); |
7508116b | 1979 | sv = svp ? *svp : NULL; |
9f6a40a7 NC |
1980 | } |
1981 | else { | |
1982 | sv = AvARRAY(av)[++cx->blk_loop.iterix]; | |
1983 | } | |
d42935ef | 1984 | } |
9f6a40a7 | 1985 | |
546fbbb9 | 1986 | if (sv && SvIS_FREED(sv)) { |
0e2d6244 | 1987 | *itersvp = NULL; |
3f6f8ae1 | 1988 | Perl_croak(aTHX_ "Use of freed value in iteration"); |
3bcc2cae JH |
1989 | } |
1990 | ||
d42935ef | 1991 | if (sv) |
a0d0e21e | 1992 | SvTEMP_off(sv); |
a0d0e21e | 1993 | else |
3280af22 | 1994 | sv = &PL_sv_undef; |
8b530633 | 1995 | if (av != PL_curstack && sv == &PL_sv_undef) { |
5f05dabc | 1996 | SV *lv = cx->blk_loop.iterlval; |
71be2cbc | 1997 | if (lv && SvREFCNT(lv) > 1) { |
1998 | SvREFCNT_dec(lv); | |
0e2d6244 | 1999 | lv = NULL; |
71be2cbc | 2000 | } |
5f05dabc | 2001 | if (lv) |
2002 | SvREFCNT_dec(LvTARG(lv)); | |
2003 | else { | |
133cdda0 | 2004 | lv = cx->blk_loop.iterlval = newSV(0); |
5f05dabc | 2005 | sv_upgrade(lv, SVt_PVLV); |
5f05dabc | 2006 | LvTYPE(lv) = 'y'; |
8916b559 | 2007 | sv_magic(lv, NULL, PERL_MAGIC_defelem, NULL, 0); |
5f05dabc | 2008 | } |
be2d5e07 | 2009 | LvTARG(lv) = SvREFCNT_inc_simple(av); |
5f05dabc | 2010 | LvTARGOFF(lv) = cx->blk_loop.iterix; |
42718184 | 2011 | LvTARGLEN(lv) = (STRLEN)UV_MAX; |
5f05dabc | 2012 | sv = (SV*)lv; |
2013 | } | |
a0d0e21e | 2014 | |
8b3a4b74 | 2015 | oldsv = *itersvp; |
be2d5e07 | 2016 | *itersvp = SvREFCNT_inc_simple_NN(sv); |
8b3a4b74 NC |
2017 | SvREFCNT_dec(oldsv); |
2018 | ||
a0d0e21e LW |
2019 | RETPUSHYES; |
2020 | } | |
2021 | ||
2022 | PP(pp_subst) | |
2023 | { | |
39644a26 | 2024 | dSP; dTARG; |
a0d0e21e LW |
2025 | register PMOP *pm = cPMOP; |
2026 | PMOP *rpm = pm; | |
a0d0e21e LW |
2027 | register char *s; |
2028 | char *strend; | |
2029 | register char *m; | |
8ec8dcb0 | 2030 | const char *c; |
a0d0e21e LW |
2031 | register char *d; |
2032 | STRLEN clen; | |
2033 | I32 iters = 0; | |
2034 | I32 maxiters; | |
2035 | register I32 i; | |
2036 | bool once; | |
71be2cbc | 2037 | bool rxtainted; |
a0d0e21e | 2038 | char *orig; |
22e551b9 | 2039 | I32 r_flags; |
aaa362c4 | 2040 | register REGEXP *rx = PM_GETRE(pm); |
a0d0e21e LW |
2041 | STRLEN len; |
2042 | int force_on_match = 0; | |
7508116b | 2043 | const I32 oldsave = PL_savestack_ix; |
792b2c16 | 2044 | STRLEN slen; |
f272994b | 2045 | bool doutf8 = FALSE; |
0e2d6244 | 2046 | SV *nsv = NULL; |
a0d0e21e | 2047 | |
5cd24f17 | 2048 | /* known replacement string? */ |
be2d5e07 | 2049 | register SV *dstr = (pm->op_pmflags & PMf_CONST) ? POPs : NULL; |
533c011a | 2050 | if (PL_op->op_flags & OPf_STACKED) |
a0d0e21e LW |
2051 | TARG = POPs; |
2052 | else { | |
54b9620d | 2053 | TARG = DEFSV; |
a0d0e21e | 2054 | EXTEND(SP,1); |
1c846c1f | 2055 | } |
d9f424b2 | 2056 | |
eca06228 NIS |
2057 | if (SvFAKE(TARG) && SvREADONLY(TARG)) |
2058 | sv_force_normal(TARG); | |
68dc0745 | 2059 | if (SvREADONLY(TARG) |
2060 | || (SvTYPE(TARG) > SVt_PVLV | |
2061 | && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG)))) | |
d470f89e | 2062 | DIE(aTHX_ PL_no_modify); |
8ec5e241 NIS |
2063 | PUTBACK; |
2064 | ||
23c2bb70 | 2065 | s = SvPV_mutable(TARG, len); |
68dc0745 | 2066 | if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV) |
a0d0e21e | 2067 | force_on_match = 1; |
b3eb6a9b | 2068 | rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) || |
3280af22 NIS |
2069 | (PL_tainted && (pm->op_pmflags & PMf_RETAINT))); |
2070 | if (PL_tainted) | |
b3eb6a9b | 2071 | rxtainted |= 2; |
9212bbba | 2072 | TAINT_NOT; |
a12c0f56 | 2073 | |
2ea5368e | 2074 | RX_MATCH_UTF8_set(rx, DO_UTF8(TARG)); |
d9f424b2 | 2075 | |
a0d0e21e LW |
2076 | force_it: |
2077 | if (!pm || !s) | |
2269b42e | 2078 | DIE(aTHX_ "panic: pp_subst"); |
a0d0e21e LW |
2079 | |
2080 | strend = s + len; | |
2ea5368e | 2081 | slen = RX_MATCH_UTF8(rx) ? utf8_length((U8*)s, (U8*)strend) : len; |
792b2c16 JH |
2082 | maxiters = 2 * slen + 10; /* We can match twice at each |
2083 | position, once with zero-length, | |
2084 | second time with non-zero. */ | |
a0d0e21e | 2085 | |
3280af22 NIS |
2086 | if (!rx->prelen && PL_curpm) { |
2087 | pm = PL_curpm; | |
aaa362c4 | 2088 | rx = PM_GETRE(pm); |
a0d0e21e | 2089 | } |
22e551b9 | 2090 | r_flags = (rx->nparens || SvTEMP(TARG) || PL_sawampersand) |
9d080a66 | 2091 | ? REXEC_COPY_STR : 0; |
f722798b | 2092 | if (SvSCREAM(TARG)) |
22e551b9 | 2093 | r_flags |= REXEC_SCREAM; |
a0d0e21e | 2094 | if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) { |
3280af22 NIS |
2095 | SAVEINT(PL_multiline); |
2096 | PL_multiline = pm->op_pmflags & PMf_MULTILINE; | |
a0d0e21e LW |
2097 | } |
2098 | orig = m = s; | |
f722798b | 2099 | if (rx->reganch & RE_USE_INTUIT) { |
ee0b7718 | 2100 | PL_bostr = orig; |
f722798b IZ |
2101 | s = CALLREG_INTUIT_START(aTHX_ rx, TARG, s, strend, r_flags, NULL); |
2102 | ||
2103 | if (!s) | |
2104 | goto nope; | |
2105 | /* How to do it in subst? */ | |
2106 | /* if ( (rx->reganch & ROPT_CHECK_ALL) | |
1c846c1f | 2107 | && !PL_sawampersand |
f722798b IZ |
2108 | && ((rx->reganch & ROPT_NOSCAN) |
2109 | || !((rx->reganch & RE_INTUIT_TAIL) | |
2110 | && (r_flags & REXEC_SCREAM)))) | |
2111 | goto yup; | |
2112 | */ | |
a0d0e21e | 2113 | } |
71be2cbc | 2114 | |
2115 | /* only replace once? */ | |
a0d0e21e | 2116 | once = !(rpm->op_pmflags & PMf_GLOBAL); |
71be2cbc | 2117 | |
2118 | /* known replacement string? */ | |
f272994b | 2119 | if (dstr) { |
8514a05a JH |
2120 | /* replacement needing upgrading? */ |
2121 | if (DO_UTF8(TARG) && !doutf8) { | |
5835a535 | 2122 | nsv = sv_newmortal(); |
4a176938 | 2123 | SvSetSV(nsv, dstr); |
8514a05a JH |
2124 | if (PL_encoding) |
2125 | sv_recode_to_utf8(nsv, PL_encoding); | |
2126 | else | |
2127 | sv_utf8_upgrade(nsv); | |
8ec8dcb0 | 2128 | c = SvPV_const(nsv, clen); |
4a176938 JH |
2129 | doutf8 = TRUE; |
2130 | } | |
2131 | else { | |
8ec8dcb0 | 2132 | c = SvPV_const(dstr, clen); |
4a176938 | 2133 | doutf8 = DO_UTF8(dstr); |
8514a05a | 2134 | } |
f272994b A |
2135 | } |
2136 | else { | |
8916b559 | 2137 | c = NULL; |
f272994b A |
2138 | doutf8 = FALSE; |
2139 | } | |
2140 | ||
71be2cbc | 2141 | /* can do inplace substitution? */ |
eb160463 | 2142 | if (c && (I32)clen <= rx->minlen && (once || !(r_flags & REXEC_COPY_STR)) |
5835a535 JH |
2143 | && !(rx->reganch & ROPT_LOOKBEHIND_SEEN) |
2144 | && (!doutf8 || SvUTF8(TARG))) { | |
f722798b IZ |
2145 | if (!CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL, |
2146 | r_flags | REXEC_CHECKED)) | |
2147 | { | |
8ec5e241 | 2148 | SPAGAIN; |
3280af22 | 2149 | PUSHs(&PL_sv_no); |
71be2cbc | 2150 | LEAVE_SCOPE(oldsave); |
2151 | RETURN; | |
2152 | } | |
2153 | if (force_on_match) { | |
2154 | force_on_match = 0; | |
2155 | s = SvPV_force(TARG, len); | |
2156 | goto force_it; | |
2157 | } | |
71be2cbc | 2158 | d = s; |
3280af22 | 2159 | PL_curpm = pm; |
71be2cbc | 2160 | SvSCREAM_off(TARG); /* disable possible screamer */ |
2161 | if (once) { | |
48c036b1 | 2162 | rxtainted |= RX_MATCH_TAINTED(rx); |
cf93c79d IZ |
2163 | m = orig + rx->startp[0]; |
2164 | d = orig + rx->endp[0]; | |
71be2cbc | 2165 | s = orig; |
2166 | if (m - s > strend - d) { /* faster to shorten from end */ | |
2167 | if (clen) { | |
2168 | Copy(c, m, clen, char); | |
2169 | m += clen; | |
a0d0e21e | 2170 | } |
71be2cbc | 2171 | i = strend - d; |
2172 | if (i > 0) { | |
2173 | Move(d, m, i, char); | |
2174 | m += i; | |
a0d0e21e | 2175 | } |
71be2cbc | 2176 | *m = '\0'; |
2177 | SvCUR_set(TARG, m - s); | |
2178 | } | |
155aba94 | 2179 | else if ((i = m - s)) { /* faster from front */ |
71be2cbc | 2180 | d -= clen; |
2181 | m = d; | |
2182 | sv_chop(TARG, d-i); | |
2183 | s += i; | |
2184 | while (i--) | |
2185 | *--d = *--s; | |
2186 | if (clen) | |
2187 | Copy(c, m, clen, char); | |
2188 | } | |
2189 | else if (clen) { | |
2190 | d -= clen; | |
2191 | sv_chop(TARG, d); | |
2192 | Copy(c, d, clen, char); | |
2193 | } | |
2194 | else { | |
2195 | sv_chop(TARG, d); | |
2196 | } | |
48c036b1 | 2197 | TAINT_IF(rxtainted & 1); |
8ec5e241 | 2198 | SPAGAIN; |
3280af22 | 2199 | PUSHs(&PL_sv_yes); |
71be2cbc | 2200 | } |
2201 | else { | |
71be2cbc | 2202 | do { |
2203 | if (iters++ > maxiters) | |
cea2e8a9 | 2204 | DIE(aTHX_ "Substitution loop"); |
d9f97599 | 2205 | rxtainted |= RX_MATCH_TAINTED(rx); |
cf93c79d | 2206 | m = rx->startp[0] + orig; |
155aba94 | 2207 | if ((i = m - s)) { |
71be2cbc | 2208 | if (s != d) |
2209 | Move(s, d, i, char); | |
2210 | d += i; | |
a0d0e21e | 2211 | } |
71be2cbc | 2212 | if (clen) { |
2213 | Copy(c, d, clen, char); | |
2214 | d += clen; | |
2215 | } | |
cf93c79d | 2216 | s = rx->endp[0] + orig; |
cea2e8a9 | 2217 | } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m, |
f722798b IZ |
2218 | TARG, NULL, |
2219 | /* don't match same null twice */ | |
2220 | REXEC_NOT_FIRST|REXEC_IGNOREPOS)); | |
71be2cbc | 2221 | if (s != d) { |
2222 | i = strend - s; | |
fdac8c4b | 2223 | SvCUR_set(TARG, d - SvPVX_const(TARG) + i); |
71be2cbc | 2224 | Move(s, d, i+1, char); /* include the NUL */ |
a0d0e21e | 2225 | } |
48c036b1 | 2226 | TAINT_IF(rxtainted & 1); |
8ec5e241 | 2227 | SPAGAIN; |
71be2cbc | 2228 | PUSHs(sv_2mortal(newSViv((I32)iters))); |
a0d0e21e | 2229 | } |
80b498e0 | 2230 | (void)SvPOK_only_UTF8(TARG); |
48c036b1 | 2231 | TAINT_IF(rxtainted); |
8ec5e241 NIS |
2232 | if (SvSMAGICAL(TARG)) { |
2233 | PUTBACK; | |
2234 | mg_set(TARG); | |
2235 | SPAGAIN; | |
2236 | } | |
9212bbba | 2237 | SvTAINT(TARG); |
aefe6dfc JH |
2238 | if (doutf8) |
2239 | SvUTF8_on(TARG); | |
71be2cbc | 2240 | LEAVE_SCOPE(oldsave); |
2241 | RETURN; | |
a0d0e21e | 2242 | } |
71be2cbc | 2243 | |
f722798b IZ |
2244 | if (CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL, |
2245 | r_flags | REXEC_CHECKED)) | |
2246 | { | |
a0d0e21e LW |
2247 | if (force_on_match) { |
2248 | force_on_match = 0; | |
2249 | s = SvPV_force(TARG, len); | |
2250 | goto force_it; | |
2251 | } | |
48c036b1 | 2252 | rxtainted |= RX_MATCH_TAINTED(rx); |
04851bb3 | 2253 | dstr = newSVpvn(m, s-m); |
ffc61ed2 JH |
2254 | if (DO_UTF8(TARG)) |
2255 | SvUTF8_on(dstr); | |
3280af22 | 2256 | PL_curpm = pm; |
a0d0e21e | 2257 | if (!c) { |
c09156bb | 2258 | register PERL_CONTEXT *cx; |
8ec5e241 | 2259 | SPAGAIN; |
9469442c | 2260 | (void)ReREFCNT_inc(rx); |
a0d0e21e LW |
2261 | PUSHSUBST(cx); |
2262 | RETURNOP(cPMOP->op_pmreplroot); | |
2263 | } | |
cf93c79d | 2264 | r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST; |
a0d0e21e LW |
2265 | do { |
2266 | if (iters++ > maxiters) | |
cea2e8a9 | 2267 | DIE(aTHX_ "Substitution loop"); |
d9f97599 | 2268 | rxtainted |= RX_MATCH_TAINTED(rx); |
cf93c79d | 2269 | if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) { |
a0d0e21e LW |
2270 | m = s; |
2271 | s = orig; | |
cf93c79d | 2272 | orig = rx->subbeg; |
a0d0e21e LW |
2273 | s = orig + (m - s); |
2274 | strend = s + (strend - m); | |
2275 | } | |
cf93c79d | 2276 | m = rx->startp[0] + orig; |
5835a535 JH |
2277 | if (doutf8 && !SvUTF8(dstr)) |
2278 | sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv); | |
2279 | else | |
2280 | sv_catpvn(dstr, s, m-s); | |
cf93c79d | 2281 | s = rx->endp[0] + orig; |
a0d0e21e LW |
2282 | if (clen) |
2283 | sv_catpvn(dstr, c, clen); | |
2284 | if (once) | |
2285 | break; | |
ffc61ed2 JH |
2286 | } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m, |
2287 | TARG, NULL, r_flags)); | |
5835a535 JH |
2288 | if (doutf8 && !DO_UTF8(TARG)) |
2289 | sv_catpvn_utf8_upgrade(dstr, s, strend - s, nsv); | |
89afcb60 A |
2290 | else |
2291 | sv_catpvn(dstr, s, strend - s); | |
748a9306 | 2292 | |
676a626c | 2293 | SvPV_free(TARG); |
0da6cfda | 2294 | SvPV_set(TARG, SvPVX(dstr)); |
748a9306 LW |
2295 | SvCUR_set(TARG, SvCUR(dstr)); |
2296 | SvLEN_set(TARG, SvLEN(dstr)); | |
f272994b | 2297 | doutf8 |= DO_UTF8(dstr); |
8916b559 | 2298 | SvPV_set(dstr, NULL); |
748a9306 LW |
2299 | sv_free(dstr); |
2300 | ||
48c036b1 | 2301 | TAINT_IF(rxtainted & 1); |
f878fbec | 2302 | SPAGAIN; |
48c036b1 GS |
2303 | PUSHs(sv_2mortal(newSViv((I32)iters))); |
2304 | ||
a0d0e21e | 2305 | (void)SvPOK_only(TARG); |
f272994b | 2306 | if (doutf8) |
60aeb6fd | 2307 | SvUTF8_on(TARG); |
48c036b1 | 2308 | TAINT_IF(rxtainted); |
a0d0e21e | 2309 | SvSETMAGIC(TARG); |
9212bbba | 2310 | SvTAINT(TARG); |
4633a7c4 | 2311 | LEAVE_SCOPE(oldsave); |
a0d0e21e LW |
2312 | RETURN; |
2313 | } | |
5cd24f17 | 2314 | goto ret_no; |
a0d0e21e LW |
2315 | |
2316 | nope: | |
1c846c1f | 2317 | ret_no: |
8ec5e241 | 2318 | SPAGAIN; |
3280af22 | 2319 | PUSHs(&PL_sv_no); |
4633a7c4 | 2320 | LEAVE_SCOPE(oldsave); |
a0d0e21e LW |
2321 | RETURN; |
2322 | } | |
2323 | ||
2324 | PP(pp_grepwhile) | |
2325 | { | |
39644a26 | 2326 | dSP; |
a0d0e21e LW |
2327 | |
2328 | if (SvTRUEx(POPs)) | |
3280af22 NIS |
2329 | PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr]; |
2330 | ++*PL_markstack_ptr; | |
a0d0e21e LW |
2331 | LEAVE; /* exit inner scope */ |
2332 | ||
2333 | /* All done yet? */ | |
3280af22 | 2334 | if (PL_stack_base + *PL_markstack_ptr > SP) { |
a0d0e21e | 2335 | I32 items; |
1a9219e7 | 2336 | const I32 gimme = GIMME_V; |
a0d0e21e LW |
2337 | |
2338 | LEAVE; /* exit outer scope */ | |
2339 | (void)POPMARK; /* pop src */ | |
3280af22 | 2340 | items = --*PL_markstack_ptr - PL_markstack_ptr[-1]; |
a0d0e21e | 2341 | (void)POPMARK; /* pop dst */ |
3280af22 | 2342 | SP = PL_stack_base + POPMARK; /* pop original mark */ |
54310121 | 2343 | if (gimme == G_SCALAR) { |
a0d0e21e LW |
2344 | dTARGET; |
2345 | XPUSHi(items); | |
a0d0e21e | 2346 | } |
54310121 | 2347 | else if (gimme == G_ARRAY) |
2348 | SP += items; | |
a0d0e21e LW |
2349 | RETURN; |
2350 | } | |
2351 | else { | |
2352 | SV *src; | |
2353 | ||
2354 | ENTER; /* enter inner scope */ | |
1d7c1841 | 2355 | SAVEVPTR(PL_curpm); |
a0d0e21e | 2356 | |
3280af22 | 2357 | src = PL_stack_base[*PL_markstack_ptr]; |
a0d0e21e | 2358 | SvTEMP_off(src); |
54b9620d | 2359 | DEFSV = src; |
a0d0e21e LW |
2360 | |
2361 | RETURNOP(cLOGOP->op_other); | |
2362 | } | |
2363 | } | |
2364 | ||
2365 | PP(pp_leavesub) | |
2366 | { | |
39644a26 | 2367 | dSP; |
a0d0e21e LW |
2368 | SV **mark; |
2369 | SV **newsp; | |
2370 | PMOP *newpm; | |
2371 | I32 gimme; | |
c09156bb | 2372 | register PERL_CONTEXT *cx; |
b0d9ce38 | 2373 | SV *sv; |
a0d0e21e | 2374 | |
4127a31c RH |
2375 | if (CxMULTICALL(&cxstack[cxstack_ix])) |
2376 | return 0; | |
2377 | ||
a0d0e21e | 2378 | POPBLOCK(cx,newpm); |
13057ebc | 2379 | cxstack_ix++; /* temporarily protect top context */ |
1c846c1f | 2380 | |
a1f49e72 | 2381 | TAINT_NOT; |
a0d0e21e LW |
2382 | if (gimme == G_SCALAR) { |
2383 | MARK = newsp + 1; | |
a29cdaf0 | 2384 | if (MARK <= SP) { |
a8bba7fa | 2385 | if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) { |
a29cdaf0 IZ |
2386 | if (SvTEMP(TOPs)) { |
2387 | *MARK = SvREFCNT_inc(TOPs); | |
2388 | FREETMPS; | |
2389 | sv_2mortal(*MARK); | |
cd06dffe GS |
2390 | } |
2391 | else { | |
959e3673 | 2392 | sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */ |
a29cdaf0 | 2393 | FREETMPS; |
959e3673 GS |
2394 | *MARK = sv_mortalcopy(sv); |
2395 | SvREFCNT_dec(sv); | |
a29cdaf0 | 2396 | } |
cd06dffe GS |
2397 | } |
2398 | else | |
a29cdaf0 | 2399 | *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs); |
cd06dffe GS |
2400 | } |
2401 | else { | |
f86702cc | 2402 | MEXTEND(MARK, 0); |
3280af22 | 2403 | *MARK = &PL_sv_undef; |
a0d0e21e LW |
2404 | } |
2405 | SP = MARK; | |
2406 | } | |
54310121 | 2407 | else if (gimme == G_ARRAY) { |
f86702cc | 2408 | for (MARK = newsp + 1; MARK <= SP; MARK++) { |
a1f49e72 | 2409 | if (!SvTEMP(*MARK)) { |
f86702cc | 2410 | *MARK = sv_mortalcopy(*MARK); |
a1f49e72 CS |
2411 | TAINT_NOT; /* Each item is independent */ |
2412 | } | |
f86702cc | 2413 | } |
a0d0e21e | 2414 | } |
f86702cc | 2415 | PUTBACK; |
1c846c1f | 2416 | |
13057ebc DM |
2417 | LEAVE; |
2418 | cxstack_ix--; | |
b0d9ce38 | 2419 | POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */ |
3280af22 | 2420 | PL_curpm = newpm; /* ... and pop $1 et al */ |
a0d0e21e | 2421 | |
b0d9ce38 | 2422 | LEAVESUB(sv); |
a0d0e21e LW |
2423 | return pop_return(); |
2424 | } | |
2425 | ||
cd06dffe GS |
2426 | /* This duplicates the above code because the above code must not |
2427 | * get any slower by more conditions */ | |
2428 | PP(pp_leavesublv) | |
2429 | { | |
39644a26 | 2430 | dSP; |
cd06dffe GS |
2431 | SV **mark; |
2432 | SV **newsp; | |
2433 | PMOP *newpm; | |
2434 | I32 gimme; | |
2435 | register PERL_CONTEXT *cx; | |
b0d9ce38 | 2436 | SV *sv; |
cd06dffe | 2437 | |
4127a31c RH |
2438 | if (CxMULTICALL(&cxstack[cxstack_ix])) |
2439 | return 0; | |
2440 | ||
cd06dffe | 2441 | POPBLOCK(cx,newpm); |
13057ebc | 2442 | cxstack_ix++; /* temporarily protect top context */ |
1c846c1f | 2443 | |
cd06dffe GS |
2444 | TAINT_NOT; |
2445 | ||
2446 | if (cx->blk_sub.lval & OPpENTERSUB_INARGS) { | |
2447 | /* We are an argument to a function or grep(). | |
2448 | * This kind of lvalueness was legal before lvalue | |
2449 | * subroutines too, so be backward compatible: | |
2450 | * cannot report errors. */ | |
2451 | ||
2452 | /* Scalar context *is* possible, on the LHS of -> only, | |
2453 | * as in f()->meth(). But this is not an lvalue. */ | |
2454 | if (gimme == G_SCALAR) | |
2455 | goto temporise; | |
2456 | if (gimme == G_ARRAY) { | |
a8bba7fa | 2457 | if (!CvLVALUE(cx->blk_sub.cv)) |
cd06dffe GS |
2458 | goto temporise_array; |
2459 | EXTEND_MORTAL(SP - newsp); | |
2460 | for (mark = newsp + 1; mark <= SP; mark++) { | |
2461 | if (SvTEMP(*mark)) | |
2462 | /* empty */ ; | |
2463 | else if (SvFLAGS(*mark) & (SVs_PADTMP | SVf_READONLY)) | |
2464 | *mark = sv_mortalcopy(*mark); | |
2465 | else { | |
2466 | /* Can be a localized value subject to deletion. */ | |
2467 | PL_tmps_stack[++PL_tmps_ix] = *mark; | |
be2d5e07 | 2468 | SvREFCNT_inc_void(*mark); |
cd06dffe GS |
2469 | } |
2470 | } | |
2471 | } | |
2472 | } | |
2473 | else if (cx->blk_sub.lval) { /* Leave it as it is if we can. */ | |
2474 | /* Here we go for robustness, not for speed, so we change all | |
2475 | * the refcounts so the caller gets a live guy. Cannot set | |
2476 | * TEMP, so sv_2mortal is out of question. */ | |
a8bba7fa | 2477 | if (!CvLVALUE(cx->blk_sub.cv)) { |
13057ebc DM |
2478 | LEAVE; |
2479 | cxstack_ix--; | |
b0d9ce38 | 2480 | POPSUB(cx,sv); |
d470f89e | 2481 | PL_curpm = newpm; |
b0d9ce38 | 2482 | LEAVESUB(sv); |
d470f89e GS |
2483 | DIE(aTHX_ "Can't modify non-lvalue subroutine call"); |
2484 | } | |
cd06dffe GS |
2485 | if (gimme == G_SCALAR) { |
2486 | MARK = newsp + 1; | |
2487 | EXTEND_MORTAL(1); | |
2488 | if (MARK == SP) { | |
43e23f82 NC |
2489 | /* Temporaries are bad unless they happen to be elements |
2490 | * of a tied hash or array */ | |
2491 | if (SvFLAGS(TOPs) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY) && | |
2492 | !(SvRMAGICAL(TOPs) && mg_find(TOPs, PERL_MAGIC_tiedelem))) { | |
13057ebc DM |
2493 | LEAVE; |
2494 | cxstack_ix--; | |
b0d9ce38 | 2495 | POPSUB(cx,sv); |
d470f89e | 2496 | PL_curpm = newpm; |
b0d9ce38 | 2497 | LEAVESUB(sv); |
3dcf2cb6 HS |
2498 | DIE(aTHX_ "Can't return %s from lvalue subroutine", |
2499 | SvREADONLY(TOPs) ? (TOPs == &PL_sv_undef) ? "undef" | |
2500 | : "a readonly value" : "a temporary"); | |
d470f89e | 2501 | } |
cd06dffe GS |
2502 | else { /* Can be a localized value |
2503 | * subject to deletion. */ | |
2504 | PL_tmps_stack[++PL_tmps_ix] = *mark; | |
be2d5e07 | 2505 | SvREFCNT_inc_void(*mark); |
cd06dffe GS |
2506 | } |
2507 | } | |
d470f89e | 2508 | else { /* Should not happen? */ |
13057ebc DM |
2509 | LEAVE; |
2510 | cxstack_ix--; | |
b0d9ce38 | 2511 | POPSUB(cx,sv); |
d470f89e | 2512 | PL_curpm = newpm; |
b0d9ce38 | 2513 | LEAVESUB(sv); |
d470f89e | 2514 | DIE(aTHX_ "%s returned from lvalue subroutine in scalar context", |
cd06dffe | 2515 | (MARK > SP ? "Empty array" : "Array")); |
d470f89e | 2516 | } |
cd06dffe GS |
2517 | SP = MARK; |
2518 | } | |
2519 | else if (gimme == G_ARRAY) { | |
2520 | EXTEND_MORTAL(SP - newsp); | |
2521 | for (mark = newsp + 1; mark <= SP; mark++) { | |
f206cdda AMS |
2522 | if (*mark != &PL_sv_undef |
2523 | && SvFLAGS(*mark) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) { | |
d470f89e GS |
2524 | /* Might be flattened array after $#array = */ |
2525 | PUTBACK; | |
13057ebc DM |
2526 | LEAVE; |
2527 | cxstack_ix--; | |
b0d9ce38 | 2528 | POPSUB(cx,sv); |
d470f89e | 2529 | PL_curpm = newpm; |
b0d9ce38 | 2530 | LEAVESUB(sv); |
f206cdda AMS |
2531 | DIE(aTHX_ "Can't return a %s from lvalue subroutine", |
2532 | SvREADONLY(TOPs) ? "readonly value" : "temporary"); | |
d470f89e | 2533 | } |
cd06dffe | 2534 | else { |
cd06dffe GS |
2535 | /* Can be a localized value subject to deletion. */ |
2536 | PL_tmps_stack[++PL_tmps_ix] = *mark; | |
be2d5e07 | 2537 | SvREFCNT_inc_void(*mark); |
cd06dffe GS |
2538 | } |
2539 | } | |
2540 | } | |
2541 | } | |
2542 | else { | |
2543 | if (gimme == G_SCALAR) { | |
2544 | temporise: | |
2545 | MARK = newsp + 1; | |
2546 | if (MARK <= SP) { | |
a8bba7fa | 2547 | if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) { |
cd06dffe GS |
2548 | if (SvTEMP(TOPs)) { |
2549 | *MARK = SvREFCNT_inc(TOPs); | |
2550 | FREETMPS; | |
2551 | sv_2mortal(*MARK); | |
2552 | } | |
2553 | else { | |
959e3673 | 2554 | sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */ |
cd06dffe | 2555 | FREETMPS; |
959e3673 GS |
2556 | *MARK = sv_mortalcopy(sv); |
2557 | SvREFCNT_dec(sv); | |
cd06dffe GS |
2558 | } |
2559 | } | |
2560 | else | |
2561 | *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs); | |
2562 | } | |
2563 | else { | |
2564 | MEXTEND(MARK, 0); | |
2565 | *MARK = &PL_sv_undef; | |
2566 | } | |
2567 | SP = MARK; | |
2568 | } | |
2569 | else if (gimme == G_ARRAY) { | |
2570 | temporise_array: | |
2571 | for (MARK = newsp + 1; MARK <= SP; MARK++) { | |
2572 | if (!SvTEMP(*MARK)) { | |
2573 | *MARK = sv_mortalcopy(*MARK); | |
2574 | TAINT_NOT; /* Each item is independent */ | |
2575 | } | |
2576 | } | |
2577 | } | |
2578 | } | |
2579 | PUTBACK; | |
1c846c1f | 2580 | |
13057ebc DM |
2581 | LEAVE; |
2582 | cxstack_ix--; | |
b0d9ce38 | 2583 | POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */ |
cd06dffe GS |
2584 | PL_curpm = newpm; /* ... and pop $1 et al */ |
2585 | ||
b0d9ce38 | 2586 | LEAVESUB(sv); |
cd06dffe GS |
2587 | return pop_return(); |
2588 | } | |
2589 | ||
2590 | ||
76e3520e | 2591 | STATIC CV * |
cea2e8a9 | 2592 | S_get_db_sub(pTHX_ SV **svp, CV *cv) |
3de9ffa1 | 2593 | { |
7508116b | 2594 | SV * const dbsv = GvSVn(PL_DBsub); |
491527d0 | 2595 | |
2a8de9e2 | 2596 | save_item(dbsv); |
491527d0 GS |
2597 | if (!PERLDB_SUB_NN) { |
2598 | GV *gv = CvGV(cv); | |
2599 | ||
491527d0 | 2600 | if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED)) |
1c846c1f | 2601 | || strEQ(GvNAME(gv), "END") |
491527d0 GS |
2602 | || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */ |
2603 | !( (SvTYPE(*svp) == SVt_PVGV) && (GvCV((GV*)*svp) == cv) | |
2604 | && (gv = (GV*)*svp) ))) { | |
2605 | /* Use GV from the stack as a fallback. */ | |
2606 | /* GV is potentially non-unique, or contain different CV. */ | |
3bad88ff | 2607 | SV * const tmp = newRV((SV*)cv); |
c2e66d9e GS |
2608 | sv_setsv(dbsv, tmp); |
2609 | SvREFCNT_dec(tmp); | |
491527d0 GS |
2610 | } |
2611 | else { | |
8916b559 | 2612 | gv_efullname3(dbsv, gv, NULL); |
491527d0 | 2613 | } |
3de9ffa1 MB |
2614 | } |
2615 | else { | |
c985f954 | 2616 | const int type = SvTYPE(dbsv); |
2a8de9e2 AL |
2617 | if (type < SVt_PVIV && type != SVt_IV) |
2618 | sv_upgrade(dbsv, SVt_PVIV); | |
155aba94 | 2619 | (void)SvIOK_on(dbsv); |
0da6cfda | 2620 | SvIV_set(dbsv, PTR2IV(cv)); /* Do it the quickest way */ |
3de9ffa1 | 2621 | } |
491527d0 | 2622 | |
3de9ffa1 | 2623 | if (CvXSUB(cv)) |
3280af22 NIS |
2624 | PL_curcopdb = PL_curcop; |
2625 | cv = GvCV(PL_DBsub); | |
3de9ffa1 MB |
2626 | return cv; |
2627 | } | |
2628 | ||
a0d0e21e LW |
2629 | PP(pp_entersub) |
2630 | { | |
39644a26 | 2631 | dSP; dPOPss; |
a0d0e21e | 2632 | GV *gv; |
a0d0e21e | 2633 | register CV *cv; |
c09156bb | 2634 | register PERL_CONTEXT *cx; |
5d94fbed | 2635 | I32 gimme; |
c985f954 | 2636 | const bool hasargs = (PL_op->op_flags & OPf_STACKED) != 0; |
a0d0e21e LW |
2637 | |
2638 | if (!sv) | |
cea2e8a9 | 2639 | DIE(aTHX_ "Not a CODE reference"); |
a0d0e21e | 2640 | switch (SvTYPE(sv)) { |
88070769 YH |
2641 | /* This is overwhelming the most common case: */ |
2642 | case SVt_PVGV: | |
2643 | if (!(cv = GvCVu((GV*)sv))) { | |
2644 | HV *stash; | |
2645 | cv = sv_2cv(sv, &stash, &gv, 0); | |
2646 | } | |
2647 | if (!cv) { | |
2648 | ENTER; | |
2649 | SAVETMPS; | |
2650 | goto try_autoload; | |
2651 | } | |
2652 | break; | |
a0d0e21e LW |
2653 | default: |
2654 | if (!SvROK(sv)) { | |
c985f954 | 2655 | const char *sym; |
3280af22 | 2656 | if (sv == &PL_sv_yes) { /* unfound import, ignore */ |
fb73857a | 2657 | if (hasargs) |
3280af22 | 2658 | SP = PL_stack_base + POPMARK; |
a0d0e21e | 2659 | RETURN; |
fb73857a | 2660 | } |
15ff848f CS |
2661 | if (SvGMAGICAL(sv)) { |
2662 | mg_get(sv); | |
f5f1d18e AMS |
2663 | if (SvROK(sv)) |
2664 | goto got_rv; | |
8916b559 | 2665 | sym = SvPOKp(sv) ? SvPVX_const(sv) : NULL; |
15ff848f | 2666 | } |
c985f954 | 2667 | else { |
db1c9db1 | 2668 | sym = SvPV_nolen_const(sv); |
c985f954 | 2669 | } |
15ff848f | 2670 | if (!sym) |
cea2e8a9 | 2671 | DIE(aTHX_ PL_no_usym, "a subroutine"); |
533c011a | 2672 | if (PL_op->op_private & HINT_STRICT_REFS) |
cea2e8a9 | 2673 | DIE(aTHX_ PL_no_symref, sym, "a subroutine"); |
864dbfa3 | 2674 | cv = get_cv(sym, TRUE); |
a0d0e21e LW |
2675 | break; |
2676 | } | |
f5f1d18e | 2677 | got_rv: |
f5284f61 | 2678 | { |
3bad88ff | 2679 | SV * const * sp = &sv; /* Used in tryAMAGICunDEREF macro. */ |
f5284f61 IZ |
2680 | tryAMAGICunDEREF(to_cv); |
2681 | } | |
a0d0e21e LW |
2682 | cv = (CV*)SvRV(sv); |
2683 | if (SvTYPE(cv) == SVt_PVCV) | |
2684 | break; | |
2685 | /* FALL THROUGH */ | |
2686 | case SVt_PVHV: | |
2687 | case SVt_PVAV: | |
cea2e8a9 | 2688 | DIE(aTHX_ "Not a CODE reference"); |
88070769 | 2689 | /* This is the second most common case: */ |
a0d0e21e LW |
2690 | case SVt_PVCV: |
2691 | cv = (CV*)sv; | |
2692 | break; | |
a0d0e21e LW |
2693 | } |
2694 | ||
2695 | ENTER; | |
2696 | SAVETMPS; | |
2697 | ||
2698 | retry: | |
a0d0e21e | 2699 | if (!CvROOT(cv) && !CvXSUB(cv)) { |
44a8e56a | 2700 | GV* autogv; |
22239a37 | 2701 | SV* sub_name; |
44a8e56a | 2702 | |
2703 | /* anonymous or undef'd function leaves us no recourse */ | |
2704 | if (CvANON(cv) || !(gv = CvGV(cv))) | |
cea2e8a9 | 2705 | DIE(aTHX_ "Undefined subroutine called"); |
67caa1fe | 2706 | |
44a8e56a | 2707 | /* autoloaded stub? */ |
2708 | if (cv != GvCV(gv)) { | |
2709 | cv = GvCV(gv); | |
a0d0e21e | 2710 | } |
44a8e56a | 2711 | /* should call AUTOLOAD now? */ |
67caa1fe | 2712 | else { |
f6ec51f7 GS |
2713 | try_autoload: |
2714 | if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), | |
2715 | FALSE))) | |
2716 | { | |
2717 | cv = GvCV(autogv); | |
2718 | } | |
2719 | /* sorry */ | |
2720 | else { | |
2721 | sub_name = sv_newmortal(); | |
8916b559 | 2722 | gv_efullname3(sub_name, gv, NULL); |
c293eb2b | 2723 | DIE(aTHX_ "Undefined subroutine &%"SVf" called", sub_name); |
f6ec51f7 | 2724 | } |
67caa1fe GS |
2725 | } |
2726 | if (!cv) | |
cea2e8a9 | 2727 | DIE(aTHX_ "Not a CODE reference"); |
67caa1fe | 2728 | goto retry; |
a0d0e21e LW |
2729 | } |
2730 | ||
54310121 | 2731 | gimme = GIMME_V; |
67caa1fe | 2732 | if ((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub) && !CvNODEBUG(cv)) { |
4f01c5a5 | 2733 | cv = get_db_sub(&sv, cv); |
9648d1c1 NC |
2734 | if (!cv || (!CvXSUB(cv) && !CvSTART(cv))) |
2735 | DIE(aTHX_ "No DB::sub routine defined"); | |
67caa1fe | 2736 | } |
a0d0e21e | 2737 | |
4d1ff10f | 2738 | #ifdef USE_5005THREADS |
3de9ffa1 MB |
2739 | /* |
2740 | * First we need to check if the sub or method requires locking. | |
458fb581 MB |
2741 | * If so, we gain a lock on the CV, the first argument or the |
2742 | * stash (for static methods), as appropriate. This has to be | |
2743 | * inline because for FAKE_THREADS, COND_WAIT inlines code to | |
2744 | * reschedule by returning a new op. | |
3de9ffa1 | 2745 | */ |
11343788 | 2746 | MUTEX_LOCK(CvMUTEXP(cv)); |
77a005ab MB |
2747 | if (CvFLAGS(cv) & CVf_LOCKED) { |
2748 | MAGIC *mg; | |
2749 | if (CvFLAGS(cv) & CVf_METHOD) { | |
533c011a NIS |
2750 | if (SP > PL_stack_base + TOPMARK) |
2751 | sv = *(PL_stack_base + TOPMARK + 1); | |
77a005ab | 2752 | else { |
9755d405 | 2753 | AV *av = (AV*)PAD_SVl(0); |
13e08037 GS |
2754 | if (hasargs || !av || AvFILLp(av) < 0 |
2755 | || !(sv = AvARRAY(av)[0])) | |
2756 | { | |
2757 | MUTEX_UNLOCK(CvMUTEXP(cv)); | |
d470f89e | 2758 | DIE(aTHX_ "no argument for locked method call"); |
13e08037 | 2759 | } |
77a005ab MB |
2760 | } |
2761 | if (SvROK(sv)) | |
2762 | sv = SvRV(sv); | |
458fb581 MB |
2763 | else { |
2764 | STRLEN len; | |
2765 | char *stashname = SvPV(sv, len); | |
2766 | sv = (SV*)gv_stashpvn(stashname, len, TRUE); | |
2767 | } | |
77a005ab MB |
2768 | } |
2769 | else { | |
2770 | sv = (SV*)cv; | |
2771 | } | |
2772 | MUTEX_UNLOCK(CvMUTEXP(cv)); | |
2773 | mg = condpair_magic(sv); | |
2774 | MUTEX_LOCK(MgMUTEXP(mg)); | |
2775 | if (MgOWNER(mg) == thr) | |
2776 | MUTEX_UNLOCK(MgMUTEXP(mg)); | |
2777 | else { | |
2778 | while (MgOWNER(mg)) | |
2779 | COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg)); | |
2780 | MgOWNER(mg) = thr; | |
bf49b057 | 2781 | DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: pp_entersub lock %p\n", |
a674cc95 | 2782 | thr, sv)); |
77a005ab | 2783 | MUTEX_UNLOCK(MgMUTEXP(mg)); |
c76ac1ee | 2784 | SAVEDESTRUCTOR_X(Perl_unlock_condpair, sv); |
11343788 | 2785 | } |
77a005ab | 2786 | MUTEX_LOCK(CvMUTEXP(cv)); |
11343788 | 2787 | } |
3de9ffa1 MB |
2788 | /* |
2789 | * Now we have permission to enter the sub, we must distinguish | |
2790 | * four cases. (0) It's an XSUB (in which case we don't care | |
2791 | * about ownership); (1) it's ours already (and we're recursing); | |
2792 | * (2) it's free (but we may already be using a cached clone); | |
2793 | * (3) another thread owns it. Case (1) is easy: we just use it. | |
2794 | * Case (2) means we look for a clone--if we have one, use it | |
2795 | * otherwise grab ownership of cv. Case (3) means we look for a | |
2796 | * clone (for non-XSUBs) and have to create one if we don't | |
2797 | * already have one. | |
2798 | * Why look for a clone in case (2) when we could just grab | |
2799 | * ownership of cv straight away? Well, we could be recursing, | |
2800 | * i.e. we originally tried to enter cv while another thread | |
2801 | * owned it (hence we used a clone) but it has been freed up | |
2802 | * and we're now recursing into it. It may or may not be "better" | |
2803 | * to use the clone but at least CvDEPTH can be trusted. | |
2804 | */ | |
2805 | if (CvOWNER(cv) == thr || CvXSUB(cv)) | |
2806 | MUTEX_UNLOCK(CvMUTEXP(cv)); | |
11343788 | 2807 | else { |
3de9ffa1 MB |
2808 | /* Case (2) or (3) */ |
2809 | SV **svp; | |
2810 | ||
11343788 | 2811 | /* |
3de9ffa1 MB |
2812 | * XXX Might it be better to release CvMUTEXP(cv) while we |
2813 | * do the hv_fetch? We might find someone has pinched it | |
2814 | * when we look again, in which case we would be in case | |
2815 | * (3) instead of (2) so we'd have to clone. Would the fact | |
2816 | * that we released the mutex more quickly make up for this? | |
2817 | */ | |
b099ddc0 | 2818 | if ((svp = hv_fetch(thr->cvcache, (char *)cv, sizeof(cv), FALSE))) |
6ee623d5 | 2819 | { |
3de9ffa1 | 2820 | /* We already have a clone to use */ |
11343788 | 2821 | MUTEX_UNLOCK(CvMUTEXP(cv)); |
3de9ffa1 | 2822 | cv = *(CV**)svp; |
bf49b057 | 2823 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
1fd28e87 MB |
2824 | "entersub: %p already has clone %p:%s\n", |
2825 | thr, cv, SvPEEK((SV*)cv))); | |
3de9ffa1 MB |
2826 | CvOWNER(cv) = thr; |
2827 | SvREFCNT_inc(cv); | |
2828 | if (CvDEPTH(cv) == 0) | |
c76ac1ee | 2829 | SAVEDESTRUCTOR_X(unset_cvowner, (void*) cv); |
3de9ffa1 | 2830 | } |
11343788 | 2831 | else { |
3de9ffa1 MB |
2832 | /* (2) => grab ownership of cv. (3) => make clone */ |
2833 | if (!CvOWNER(cv)) { | |
2834 | CvOWNER(cv) = thr; | |
2835 | SvREFCNT_inc(cv); | |
11343788 | 2836 | MUTEX_UNLOCK(CvMUTEXP(cv)); |
bf49b057 | 2837 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
1fd28e87 MB |
2838 | "entersub: %p grabbing %p:%s in stash %s\n", |
2839 | thr, cv, SvPEEK((SV*)cv), CvSTASH(cv) ? | |
3de9ffa1 | 2840 | HvNAME(CvSTASH(cv)) : "(none)")); |
cd06dffe GS |
2841 | } |
2842 | else { | |
3de9ffa1 MB |
2843 | /* Make a new clone. */ |
2844 | CV *clonecv; | |
2845 | SvREFCNT_inc(cv); /* don't let it vanish from under us */ | |
2846 | MUTEX_UNLOCK(CvMUTEXP(cv)); | |
bf49b057 | 2847 | DEBUG_S((PerlIO_printf(Perl_debug_log, |
1fd28e87 MB |
2848 | "entersub: %p cloning %p:%s\n", |
2849 | thr, cv, SvPEEK((SV*)cv)))); | |
3de9ffa1 MB |
2850 | /* |
2851 | * We're creating a new clone so there's no race | |
2852 | * between the original MUTEX_UNLOCK and the | |
2853 | * SvREFCNT_inc since no one will be trying to undef | |
2854 | * it out from underneath us. At least, I don't think | |
2855 | * there's a race... | |
2856 | */ | |
2857 | clonecv = cv_clone(cv); | |
2858 | SvREFCNT_dec(cv); /* finished with this */ | |
199100c8 | 2859 | hv_store(thr->cvcache, (char*)cv, sizeof(cv), (SV*)clonecv,0); |
3de9ffa1 MB |
2860 | CvOWNER(clonecv) = thr; |
2861 | cv = clonecv; | |
11343788 | 2862 | SvREFCNT_inc(cv); |
11343788 | 2863 | } |
8b73bbec | 2864 | DEBUG_S(if (CvDEPTH(cv) != 0) |
bf49b057 | 2865 | PerlIO_printf(Perl_debug_log, "depth %ld != 0\n", |
755b0776 | 2866 | CvDEPTH(cv))); |
c76ac1ee | 2867 | SAVEDESTRUCTOR_X(unset_cvowner, (void*) cv); |
11343788 | 2868 | } |
3de9ffa1 | 2869 | } |
4d1ff10f | 2870 | #endif /* USE_5005THREADS */ |
11343788 | 2871 | |
a0d0e21e | 2872 | if (CvXSUB(cv)) { |
67caa1fe | 2873 | #ifdef PERL_XSUB_OLDSTYLE |
a0d0e21e | 2874 | if (CvOLDSTYLE(cv)) { |
20ce7b12 | 2875 | I32 (*fp3)(int,int,int); |
a0d0e21e LW |
2876 | dMARK; |
2877 | register I32 items = SP - MARK; | |
67955e0c | 2878 | /* We dont worry to copy from @_. */ |
924508f0 GS |
2879 | while (SP > mark) { |
2880 | SP[1] = SP[0]; | |
2881 | SP--; | |
a0d0e21e | 2882 | } |
3280af22 | 2883 | PL_stack_sp = mark + 1; |
1d7c1841 | 2884 | fp3 = (I32(*)(int,int,int))CvXSUB(cv); |
1c846c1f | 2885 | items = (*fp3)(CvXSUBANY(cv).any_i32, |
3280af22 | 2886 | MARK - PL_stack_base + 1, |
ecfc5424 | 2887 | items); |
3280af22 | 2888 | PL_stack_sp = PL_stack_base + items; |
a0d0e21e | 2889 | } |
67caa1fe GS |
2890 | else |
2891 | #endif /* PERL_XSUB_OLDSTYLE */ | |
2892 | { | |
748a9306 LW |
2893 | I32 markix = TOPMARK; |
2894 | ||
a0d0e21e | 2895 | PUTBACK; |
67955e0c | 2896 | |
2897 | if (!hasargs) { | |
2898 | /* Need to copy @_ to stack. Alternative may be to | |
2899 | * switch stack to @_, and copy return values | |
2900 | * back. This would allow popping @_ in XSUB, e.g.. XXXX */ | |
4d1ff10f | 2901 | #ifdef USE_5005THREADS |
8c18bf38 | 2902 | AV * const av = (AV*)PAD_SVl(0); |
6d4ff0d2 | 2903 | #else |
8c18bf38 | 2904 | AV * const av = GvAV(PL_defgv); |
4d1ff10f | 2905 | #endif /* USE_5005THREADS */ |
8c18bf38 AL |
2906 | const I32 items = AvFILLp(av) + 1; /* @_ is not tieable */ |
2907 | ||
67955e0c | 2908 | |
2909 | if (items) { | |
2910 | /* Mark is at the end of the stack. */ | |
924508f0 GS |
2911 | EXTEND(SP, items); |
2912 | Copy(AvARRAY(av), SP + 1, items, SV*); | |
2913 | SP += items; | |
1c846c1f | 2914 | PUTBACK ; |
67955e0c | 2915 | } |
2916 | } | |
67caa1fe GS |
2917 | /* We assume first XSUB in &DB::sub is the called one. */ |
2918 | if (PL_curcopdb) { | |
1d7c1841 | 2919 | SAVEVPTR(PL_curcop); |
3280af22 NIS |
2920 | PL_curcop = PL_curcopdb; |
2921 | PL_curcopdb = NULL; | |
67955e0c | 2922 | } |
2923 | /* Do we need to open block here? XXXX */ | |
acfe0abc | 2924 | (void)(*CvXSUB(cv))(aTHX_ cv); |
748a9306 LW |
2925 | |
2926 | /* Enforce some sanity in scalar context. */ | |
3280af22 NIS |
2927 | if (gimme == G_SCALAR && ++markix != PL_stack_sp - PL_stack_base ) { |
2928 | if (markix > PL_stack_sp - PL_stack_base) | |
2929 | *(PL_stack_base + markix) = &PL_sv_undef; | |
748a9306 | 2930 | else |
3280af22 NIS |
2931 | *(PL_stack_base + markix) = *PL_stack_sp; |
2932 | PL_stack_sp = PL_stack_base + markix; | |
748a9306 | 2933 | } |
a0d0e21e LW |
2934 | } |
2935 | LEAVE; | |
2936 | return NORMAL; | |
2937 | } | |
2938 | else { | |
2939 | dMARK; | |
2940 | register I32 items = SP - MARK; | |
7508116b | 2941 | AV* const padlist = CvPADLIST(cv); |
533c011a | 2942 | push_return(PL_op->op_next); |
a0d0e21e LW |
2943 | PUSHBLOCK(cx, CXt_SUB, MARK); |
2944 | PUSHSUB(cx); | |
2945 | CvDEPTH(cv)++; | |
6b35e009 GS |
2946 | /* XXX This would be a natural place to set C<PL_compcv = cv> so |
2947 | * that eval'' ops within this sub know the correct lexical space. | |
d7afa7f5 JH |
2948 | * Owing the speed considerations, we choose instead to search for |
2949 | * the cv using find_runcv() when calling doeval(). | |
6b35e009 | 2950 | */ |
c2ef0052 | 2951 | if (CvDEPTH(cv) >= 2) { |
1d7c1841 | 2952 | PERL_STACK_OVERFLOW_CHECK(); |
9755d405 | 2953 | pad_push(padlist, CvDEPTH(cv), 1); |
a0d0e21e | 2954 | } |
4d1ff10f | 2955 | #ifdef USE_5005THREADS |
6d4ff0d2 | 2956 | if (!hasargs) { |
9755d405 | 2957 | AV* av = (AV*)PAD_SVl(0); |
6d4ff0d2 | 2958 | |
a2592645 | 2959 | /*NOTREACHED*/ |
93965878 | 2960 | items = AvFILLp(av) + 1; |
6d4ff0d2 MB |
2961 | if (items) { |
2962 | /* Mark is at the end of the stack. */ | |
924508f0 GS |
2963 | EXTEND(SP, items); |
2964 | Copy(AvARRAY(av), SP + 1, items, SV*); | |
2965 | SP += items; | |
1c846c1f | 2966 | PUTBACK ; |
6d4ff0d2 MB |
2967 | } |
2968 | } | |
4d1ff10f | 2969 | #endif /* USE_5005THREADS */ |
cc09671d RB |
2970 | SAVECOMPPAD(); |
2971 | PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv)); | |
4d1ff10f | 2972 | #ifndef USE_5005THREADS |
6d4ff0d2 | 2973 | if (hasargs) |
4d1ff10f | 2974 | #endif /* USE_5005THREADS */ |
6d4ff0d2 | 2975 | { |
7508116b | 2976 | AV *const av = (AV*)PAD_SVl(0); |
221373f0 GS |
2977 | if (AvREAL(av)) { |
2978 | /* @_ is normally not REAL--this should only ever | |
2979 | * happen when DB::sub() calls things that modify @_ */ | |
2980 | av_clear(av); | |
2981 | AvREAL_off(av); | |
2982 | AvREIFY_on(av); | |
2983 | } | |
4d1ff10f | 2984 | #ifndef USE_5005THREADS |
3280af22 | 2985 | cx->blk_sub.savearray = GvAV(PL_defgv); |
be2d5e07 | 2986 | GvAV(PL_defgv) = (AV*)SvREFCNT_inc_simple(av); |
4d1ff10f | 2987 | #endif /* USE_5005THREADS */ |
9755d405 | 2988 | CX_CURPAD_SAVE(cx->blk_sub); |
6d4ff0d2 | 2989 | cx->blk_sub.argarray = av; |
a0d0e21e LW |
2990 | ++MARK; |
2991 | ||
2992 | if (items > AvMAX(av) + 1) { | |
7508116b | 2993 | SV **ary = AvALLOC(av); |
a0d0e21e LW |
2994 | if (AvARRAY(av) != ary) { |
2995 | AvMAX(av) += AvARRAY(av) - AvALLOC(av); | |
2996 | SvPVX(av) = (char*)ary; | |
2997 | } | |
2998 | if (items > AvMAX(av) + 1) { | |
2999 | AvMAX(av) = items - 1; | |
3000 | Renew(ary,items,SV*); | |
3001 | AvALLOC(av) = ary; | |
3002 | SvPVX(av) = (char*)ary; | |
3003 | } | |
3004 | } | |
3005 | Copy(MARK,AvARRAY(av),items,SV*); | |
93965878 | 3006 | AvFILLp(av) = items - 1; |
1c846c1f | 3007 | |
a0d0e21e LW |
3008 | while (items--) { |
3009 | if (*MARK) | |
3010 | SvTEMP_off(*MARK); | |
3011 | MARK++; | |
3012 | } | |
3013 | } | |
4a925ff6 GS |
3014 | /* warning must come *after* we fully set up the context |
3015 | * stuff so that __WARN__ handlers can safely dounwind() | |
3016 | * if they want to | |
3017 | */ | |
3018 | if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION) | |
3019 | && !(PERLDB_SUB && cv == GvCV(PL_DBsub))) | |
3020 | sub_crush_depth(cv); | |
77a005ab | 3021 | #if 0 |
bf49b057 | 3022 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
0f15f207 | 3023 | "%p entersub returning %p\n", thr, CvSTART(cv))); |
77a005ab | 3024 | #endif |
a0d0e21e LW |
3025 | RETURNOP(CvSTART(cv)); |
3026 | } | |
3027 | } | |
3028 | ||
44a8e56a | 3029 | void |
864dbfa3 | 3030 | Perl_sub_crush_depth(pTHX_ CV *cv) |
44a8e56a | 3031 | { |
3032 | if (CvANON(cv)) | |
9014280d | 3033 | Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on anonymous subroutine"); |
44a8e56a | 3034 | else { |
c9dc1ff4 | 3035 | SV* const tmpstr = sv_newmortal(); |
8916b559 | 3036 | gv_efullname3(tmpstr, CvGV(cv), NULL); |
c293eb2b NC |
3037 | Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on subroutine \"%"SVf"\"", |
3038 | tmpstr); | |
44a8e56a | 3039 | } |
3040 | } | |
3041 | ||
a0d0e21e LW |
3042 | PP(pp_aelem) |
3043 | { | |
39644a26 | 3044 | dSP; |
a0d0e21e | 3045 | SV** svp; |
8c18bf38 | 3046 | SV* const elemsv = POPs; |
d804643f | 3047 | IV elem = SvIV(elemsv); |
7508116b | 3048 | AV* const av = (AV*)POPs; |
c05e0e2f AL |
3049 | const U32 lval = PL_op->op_flags & OPf_MOD || LVRET; |
3050 | const U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > av_len(av)); | |
be6c24e0 | 3051 | SV *sv; |
a0d0e21e | 3052 | |
e35c1634 | 3053 | if (SvROK(elemsv) && !SvGAMAGIC(elemsv) && ckWARN(WARN_MISC)) |
c293eb2b | 3054 | Perl_warner(aTHX_ packWARN(WARN_MISC), "Use of reference \"%"SVf"\" as array index", elemsv); |
748a9306 | 3055 | if (elem > 0) |
3280af22 | 3056 | elem -= PL_curcop->cop_arybase; |
a0d0e21e LW |
3057 | if (SvTYPE(av) != SVt_PVAV) |
3058 | RETPUSHUNDEF; | |
68dc0745 | 3059 | svp = av_fetch(av, elem, lval && !defer); |
a0d0e21e | 3060 | if (lval) { |
0912201f | 3061 | #ifdef PERL_MALLOC_WRAP |
0912201f | 3062 | if (SvUOK(elemsv)) { |
c985f954 | 3063 | const UV uv = SvUV(elemsv); |
0912201f JH |
3064 | elem = uv > IV_MAX ? IV_MAX : uv; |
3065 | } | |
3066 | else if (SvNOK(elemsv)) | |
3067 | elem = (IV)SvNV(elemsv); | |
8c18bf38 AL |
3068 | if (elem > 0) { |
3069 | static const char oom_array_extend[] = | |
3070 | "Out of memory during array extend"; /* Duplicated in av.c */ | |
0912201f | 3071 | MEM_WRAP_CHECK_1(elem,SV*,oom_array_extend); |
8c18bf38 | 3072 | } |
0912201f | 3073 | #endif |
3280af22 | 3074 | if (!svp || *svp == &PL_sv_undef) { |
68dc0745 | 3075 | SV* lv; |
3076 | if (!defer) | |
cea2e8a9 | 3077 | DIE(aTHX_ PL_no_aelem, elem); |
68dc0745 | 3078 | lv = sv_newmortal(); |
3079 | sv_upgrade(lv, SVt_PVLV); | |
3080 | LvTYPE(lv) = 'y'; | |
8916b559 | 3081 | sv_magic(lv, NULL, PERL_MAGIC_defelem, NULL, 0); |
be2d5e07 | 3082 | LvTARG(lv) = SvREFCNT_inc_simple(av); |
68dc0745 | 3083 | LvTARGOFF(lv) = elem; |
3084 | LvTARGLEN(lv) = 1; | |
3085 | PUSHs(lv); | |
3086 | RETURN; | |
3087 | } | |
bfc4de9f | 3088 | if (PL_op->op_private & OPpLVAL_INTRO) |
161b7d16 | 3089 | save_aelem(av, elem, svp); |
533c011a NIS |
3090 | else if (PL_op->op_private & OPpDEREF) |
3091 | vivify_ref(*svp, PL_op->op_private & OPpDEREF); | |
a0d0e21e | 3092 | } |
3280af22 | 3093 | sv = (svp ? *svp : &PL_sv_undef); |
be6c24e0 GS |
3094 | if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */ |
3095 | sv = sv_mortalcopy(sv); | |
3096 | PUSHs(sv); | |
a0d0e21e LW |
3097 | RETURN; |
3098 | } | |
3099 | ||
02a9e968 | 3100 | void |
864dbfa3 | 3101 | Perl_vivify_ref(pTHX_ SV *sv, U32 to_what) |
02a9e968 | 3102 | { |
255c29c3 | 3103 | SvGETMAGIC(sv); |
02a9e968 CS |
3104 | if (!SvOK(sv)) { |
3105 | if (SvREADONLY(sv)) | |
cea2e8a9 | 3106 | Perl_croak(aTHX_ PL_no_modify); |
5f05dabc | 3107 | if (SvTYPE(sv) < SVt_RV) |
3108 | sv_upgrade(sv, SVt_RV); | |
3109 | else if (SvTYPE(sv) >= SVt_PV) { | |
676a626c | 3110 | SvPV_free(sv); |
a8dc4fe8 SP |
3111 | SvLEN_set(sv, 0); |
3112 | SvCUR_set(sv, 0); | |
5f05dabc | 3113 | } |
68dc0745 | 3114 | switch (to_what) { |
5f05dabc | 3115 | case OPpDEREF_SV: |
133cdda0 | 3116 | SvRV_set(sv, newSV(0)); |
5f05dabc | 3117 | break; |
3118 | case OPpDEREF_AV: | |
a8dc4fe8 | 3119 | SvRV_set(sv, (SV*)newAV()); |
5f05dabc | 3120 | break; |
3121 | case OPpDEREF_HV: | |
a8dc4fe8 | 3122 | SvRV_set(sv, (SV*)newHV()); |
5f05dabc | 3123 | break; |
3124 | } | |
02a9e968 CS |
3125 | SvROK_on(sv); |
3126 | SvSETMAGIC(sv); | |
3127 | } | |
3128 | } | |
3129 | ||
a0d0e21e LW |
3130 | PP(pp_method) |
3131 | { | |
39644a26 | 3132 | dSP; |
c6d79d47 | 3133 | SV* const sv = TOPs; |
f5d5a27c CS |
3134 | |
3135 | if (SvROK(sv)) { | |
c6d79d47 | 3136 | SV* const rsv = SvRV(sv); |
f5d5a27c CS |
3137 | if (SvTYPE(rsv) == SVt_PVCV) { |
3138 | SETs(rsv); | |
3139 | RETURN; | |
3140 | } | |
3141 | } | |
3142 | ||
0e2d6244 | 3143 | SETs(method_common(sv, NULL)); |
f5d5a27c CS |
3144 | RETURN; |
3145 | } | |
3146 | ||
3147 | PP(pp_method_named) | |
3148 | { | |
39644a26 | 3149 | dSP; |
c6d79d47 | 3150 | SV* const sv = cSVOP_sv; |
2a979b61 | 3151 | U32 hash = SvSHARED_HASH(sv); |
f5d5a27c CS |
3152 | |
3153 | XPUSHs(method_common(sv, &hash)); | |
3154 | RETURN; | |
3155 | } | |
3156 | ||
3157 | STATIC SV * | |
3158 | S_method_common(pTHX_ SV* meth, U32* hashp) | |
3159 | { | |
a0d0e21e LW |
3160 | SV* ob; |
3161 | GV* gv; | |
56304f61 | 3162 | HV* stash; |
f5d5a27c | 3163 | STRLEN namelen; |
8916b559 | 3164 | const char* packname = NULL; |
0e2d6244 | 3165 | SV *packsv = NULL; |
ac91690f | 3166 | STRLEN packlen; |
217ef1d7 AL |
3167 | const char * const name = SvPV_const(meth, namelen); |
3168 | SV * const sv = *(PL_stack_base + TOPMARK + 1); | |
f5d5a27c | 3169 | |
4f1b7578 SC |
3170 | if (!sv) |
3171 | Perl_croak(aTHX_ "Can't call method \"%s\" on an undefined value", name); | |
3172 | ||
255c29c3 | 3173 | SvGETMAGIC(sv); |
a0d0e21e | 3174 | if (SvROK(sv)) |
16d20bd9 | 3175 | ob = (SV*)SvRV(sv); |
a0d0e21e LW |
3176 | else { |
3177 | GV* iogv; | |
a0d0e21e | 3178 | |
af09ea45 | 3179 | /* this isn't a reference */ |
8ec8dcb0 | 3180 | if(SvOK(sv) && (packname = SvPV_const(sv, packlen))) { |
481da01c | 3181 | const HE* const he = hv_fetch_ent(PL_stashcache, sv, 0, 0); |
c0401c5d | 3182 | if (he) { |
f1b0c4e1 | 3183 | stash = INT2PTR(HV*,SvIV(HeVAL(he))); |
c0401c5d JH |
3184 | goto fetch; |
3185 | } | |
3186 | } | |
3187 | ||
a0d0e21e | 3188 | if (!SvOK(sv) || |
c0401c5d | 3189 | !(packname) || |
057b822e | 3190 | !(iogv = gv_fetchsv(sv, 0, SVt_PVIO)) || |
a0d0e21e LW |
3191 | !(ob=(SV*)GvIO(iogv))) |
3192 | { | |
af09ea45 | 3193 | /* this isn't the name of a filehandle either */ |
1c846c1f | 3194 | if (!packname || |
fd400ab9 | 3195 | ((UTF8_IS_START(*packname) && DO_UTF8(sv)) |
b86a2fa7 | 3196 | ? !isIDFIRST_utf8((U8*)packname) |
834a4ddd LW |
3197 | : !isIDFIRST(*packname) |
3198 | )) | |
3199 | { | |
f5d5a27c CS |
3200 | Perl_croak(aTHX_ "Can't call method \"%s\" %s", name, |
3201 | SvOK(sv) ? "without a package or object reference" | |
3202 | : "on an undefined value"); | |
834a4ddd | 3203 | } |
af09ea45 IK |
3204 | /* assume it's a package name */ |
3205 | stash = gv_stashpvn(packname, packlen, FALSE); | |
c240c76d JH |
3206 | if (!stash) |
3207 | packsv = sv; | |
c0401c5d | 3208 | else { |
f1b0c4e1 | 3209 | SV* ref = newSViv(PTR2IV(stash)); |
efb84706 JH |
3210 | hv_store(PL_stashcache, packname, packlen, ref, 0); |
3211 | } | |
ac91690f | 3212 | goto fetch; |
a0d0e21e | 3213 | } |
af09ea45 | 3214 | /* it _is_ a filehandle name -- replace with a reference */ |
3280af22 | 3215 | *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv)); |
a0d0e21e LW |
3216 | } |
3217 | ||
af09ea45 | 3218 | /* if we got here, ob should be a reference or a glob */ |
f0d43078 GS |
3219 | if (!ob || !(SvOBJECT(ob) |
3220 | || (SvTYPE(ob) == SVt_PVGV && (ob = (SV*)GvIO((GV*)ob)) | |
3221 | && SvOBJECT(ob)))) | |
3222 | { | |
f5d5a27c CS |
3223 | Perl_croak(aTHX_ "Can't call method \"%s\" on unblessed reference", |
3224 | name); | |
f0d43078 | 3225 | } |
a0d0e21e | 3226 | |
56304f61 | 3227 | stash = SvSTASH(ob); |
a0d0e21e | 3228 | |
ac91690f | 3229 | fetch: |
af09ea45 IK |
3230 | /* NOTE: stash may be null, hope hv_fetch_ent and |
3231 | gv_fetchmethod can cope (it seems they can) */ | |
3232 | ||
f5d5a27c CS |
3233 | /* shortcut for simple names */ |
3234 | if (hashp) { | |
481da01c | 3235 | const HE* const he = hv_fetch_ent(stash, meth, 0, *hashp); |
f5d5a27c CS |
3236 | if (he) { |
3237 | gv = (GV*)HeVAL(he); | |
3238 | if (isGV(gv) && GvCV(gv) && | |
3239 | (!GvCVGEN(gv) || GvCVGEN(gv) == PL_sub_generation)) | |
3240 | return (SV*)GvCV(gv); | |
3241 | } | |
3242 | } | |
3243 | ||
c240c76d | 3244 | gv = gv_fetchmethod(stash ? stash : (HV*)packsv, name); |
af09ea45 | 3245 | |
56304f61 | 3246 | if (!gv) { |
af09ea45 IK |
3247 | /* This code tries to figure out just what went wrong with |
3248 | gv_fetchmethod. It therefore needs to duplicate a lot of | |
3249 | the internals of that function. We can't move it inside | |
3250 | Perl_gv_fetchmethod_autoload(), however, since that would | |
3251 | cause UNIVERSAL->can("NoSuchPackage::foo") to croak, and we | |
3252 | don't want that. | |
3253 | */ | |
c985f954 | 3254 | const char* leaf = name; |
8916b559 | 3255 | const char* sep = NULL; |
c985f954 | 3256 | const char* p; |
56304f61 CS |
3257 | |
3258 | for (p = name; *p; p++) { | |
3259 | if (*p == '\'') | |
3260 | sep = p, leaf = p + 1; | |
3261 | else if (*p == ':' && *(p + 1) == ':') | |
3262 | sep = p, leaf = p + 2; | |
3263 | } | |
3264 | if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) { | |
af09ea45 IK |
3265 | /* the method name is unqualified or starts with SUPER:: */ |
3266 | packname = sep ? CopSTASHPV(PL_curcop) : | |
26ab6a78 | 3267 | stash ? HvNAME_get(stash) : packname; |
3198e4e5 AV |
3268 | if (!packname) |
3269 | Perl_croak(aTHX_ | |
3270 | "Can't use anonymous symbol table for method lookup"); | |
3271 | else | |
3272 | packlen = strlen(packname); | |
56304f61 CS |
3273 | } |
3274 | else { | |
af09ea45 | 3275 | /* the method name is qualified */ |
56304f61 CS |
3276 | packname = name; |
3277 | packlen = sep - name; | |
3278 | } | |
af09ea45 IK |
3279 | |
3280 | /* we're relying on gv_fetchmethod not autovivifying the stash */ | |
3281 | if (gv_stashpvn(packname, packlen, FALSE)) { | |
c1899e02 | 3282 | Perl_croak(aTHX_ |
af09ea45 IK |
3283 | "Can't locate object method \"%s\" via package \"%.*s\"", |
3284 | leaf, (int)packlen, packname); | |
c1899e02 GS |
3285 | } |
3286 | else { | |
3287 | Perl_croak(aTHX_ | |
af09ea45 IK |
3288 | "Can't locate object method \"%s\" via package \"%.*s\"" |
3289 | " (perhaps you forgot to load \"%.*s\"?)", | |
3290 | leaf, (int)packlen, packname, (int)packlen, packname); | |
c1899e02 | 3291 | } |
56304f61 | 3292 | } |
f5d5a27c | 3293 | return isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv; |
a0d0e21e | 3294 | } |
22239a37 | 3295 | |
4d1ff10f | 3296 | #ifdef USE_5005THREADS |
51371543 | 3297 | static void |
acfe0abc | 3298 | unset_cvowner(pTHX_ void *cvarg) |
51371543 GS |
3299 | { |
3300 | register CV* cv = (CV *) cvarg; | |
51371543 | 3301 | |
bf49b057 | 3302 | DEBUG_S((PerlIO_printf(Perl_debug_log, "%p unsetting CvOWNER of %p:%s\n", |
51371543 GS |
3303 | thr, cv, SvPEEK((SV*)cv)))); |
3304 | MUTEX_LOCK(CvMUTEXP(cv)); | |
3305 | DEBUG_S(if (CvDEPTH(cv) != 0) | |
bf49b057 | 3306 | PerlIO_printf(Perl_debug_log, "depth %ld != 0\n", |
755b0776 | 3307 | CvDEPTH(cv))); |
51371543 GS |
3308 | assert(thr == CvOWNER(cv)); |
3309 | CvOWNER(cv) = 0; | |
3310 | MUTEX_UNLOCK(CvMUTEXP(cv)); | |
3311 | SvREFCNT_dec(cv); | |
3312 | } | |
4d1ff10f | 3313 | #endif /* USE_5005THREADS */ |
583439ab NC |
3314 | |
3315 | /* | |
3316 | * Local variables: | |
3317 | * c-indentation-style: bsd | |
3318 | * c-basic-offset: 4 | |
3319 | * indent-tabs-mode: t | |
3320 | * End: | |
3321 | * | |
d8294a4d NC |
3322 | * ex: set ts=8 sts=4 sw=4 noet: |
3323 | */ |