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