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