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