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