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