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