This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
I don't think it's sensible or portable to test the strtou*
[perl5.git] / pp_hot.c
CommitLineData
a0d0e21e
LW
1/* pp_hot.c
2 *
3818b22b 3 * Copyright (c) 1991-2000, Larry Wall
a0d0e21e
LW
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
8 */
9
10/*
11 * Then he heard Merry change the note, and up went the Horn-cry of Buckland,
12 * shaking the air.
13 *
14 * Awake! Awake! Fear, Fire, Foes! Awake!
15 * Fire, Foes! Awake!
16 */
17
18#include "EXTERN.h"
864dbfa3 19#define PERL_IN_PP_HOT_C
a0d0e21e
LW
20#include "perl.h"
21
22/* Hot code. */
23
11343788 24#ifdef USE_THREADS
51371543 25static void unset_cvowner(pTHXo_ void *cvarg);
11343788
MB
26#endif /* USE_THREADS */
27
a0d0e21e
LW
28PP(pp_const)
29{
4e35701f 30 djSP;
1d7c1841 31 XPUSHs(cSVOP_sv);
a0d0e21e
LW
32 RETURN;
33}
34
35PP(pp_nextstate)
36{
533c011a 37 PL_curcop = (COP*)PL_op;
a0d0e21e 38 TAINT_NOT; /* Each statement is presumed innocent */
3280af22 39 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e
LW
40 FREETMPS;
41 return NORMAL;
42}
43
44PP(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
LW
52 RETURN;
53}
54
55PP(pp_null)
56{
57 return NORMAL;
58}
59
7399586d
HS
60PP(pp_setstate)
61{
62 PL_curcop = (COP*)PL_op;
63 return NORMAL;
64}
65
a0d0e21e
LW
66PP(pp_pushmark)
67{
3280af22 68 PUSHMARK(PL_stack_sp);
a0d0e21e
LW
69 return NORMAL;
70}
71
72PP(pp_stringify)
73{
4e35701f 74 djSP; dTARGET;
a0d0e21e
LW
75 STRLEN len;
76 char *s;
77 s = SvPV(TOPs,len);
78 sv_setpvn(TARG,s,len);
234a4bc6
GS
79 if (SvUTF8(TOPs) && !IN_BYTE)
80 SvUTF8_on(TARG);
a0d0e21e
LW
81 SETTARG;
82 RETURN;
83}
84
85PP(pp_gv)
86{
4e35701f 87 djSP;
1d7c1841 88 XPUSHs((SV*)cGVOP_gv);
a0d0e21e
LW
89 RETURN;
90}
91
92PP(pp_and)
93{
4e35701f 94 djSP;
a0d0e21e
LW
95 if (!SvTRUE(TOPs))
96 RETURN;
97 else {
98 --SP;
99 RETURNOP(cLOGOP->op_other);
100 }
101}
102
103PP(pp_sassign)
104{
4e35701f 105 djSP; dPOPTOPssrl;
748a9306 106
533c011a 107 if (PL_op->op_private & OPpASSIGN_BACKWARDS) {
a0d0e21e
LW
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
LW
114 SETs(right);
115 RETURN;
116}
117
118PP(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
LW
125}
126
127PP(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
LW
134 LEAVE_SCOPE(oldsave);
135 return NORMAL;
136}
137
a0d0e21e
LW
138PP(pp_concat)
139{
4e35701f 140 djSP; dATARGET; tryAMAGICbin(concat,opASSIGN);
748a9306
LW
141 {
142 dPOPTOPssrl;
a0d0e21e 143 STRLEN len;
37931a30 144 U8 *s;
ed646e6e
SC
145 bool left_utf8;
146 bool right_utf8;
69b47968 147
82f2f503
SC
148 if (TARG == right && SvGMAGICAL(right))
149 mg_get(right);
150 if (SvGMAGICAL(left))
151 mg_get(left);
152
ed646e6e
SC
153 left_utf8 = DO_UTF8(left);
154 right_utf8 = DO_UTF8(right);
7889fe52 155
ed646e6e
SC
156 if (left_utf8 != right_utf8) {
157 if (TARG == right && !right_utf8) {
37931a30
JH
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
JH
172 if (SvREADONLY(left))
173 left = sv_2mortal(newSVsv(left));
689440ec
JH
174 else
175 sv_setpv(left, ""); /* Suppress warning. */
176 }
37931a30 177 l = (U8*)SvPV(left, targlen);
ed646e6e 178 left_utf8 |= DO_UTF8(left);
37931a30
JH
179 if (TARG != left)
180 sv_setpvn(TARG, (char*)l, targlen);
ed646e6e 181 if (!left_utf8)
37931a30
JH
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
JH
189 targlen++;
190 }
191 }
192 SvGROW(TARG, targlen+1);
193 /* And now copy, maybe upgrading right to UTF8 on the fly */
ed646e6e
SC
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
JH
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
GS
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
NIS
221 if (SvOK(TARG)) {
222#if defined(PERL_Y2KWARN)
e476b1b5 223 if ((SvIOK(right) || SvNOK(right)) && ckWARN(WARN_Y2K)) {
5bc28da9
NIS
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
NIS
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
LW
240 SETTARG;
241 RETURN;
748a9306 242 }
a0d0e21e
LW
243}
244
245PP(pp_padsv)
246{
4e35701f 247 djSP; dTARGET;
a0d0e21e 248 XPUSHs(TARG);
533c011a
NIS
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
NIS
255 SPAGAIN;
256 }
4633a7c4 257 }
a0d0e21e
LW
258 RETURN;
259}
260
261PP(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
IZ
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
IZ
274 }
275 }
a0d0e21e
LW
276 return do_readline();
277}
278
279PP(pp_eq)
280{
1c846c1f 281 djSP; tryAMAGICbinSET(eq,0);
a0d0e21e
LW
282 {
283 dPOPnv;
54310121 284 SETs(boolSV(TOPn == value));
a0d0e21e
LW
285 RETURN;
286 }
287}
288
289PP(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
PP
295 SvIVX(TOPs) != IV_MAX)
296 {
748a9306 297 ++SvIVX(TOPs);
55497cff 298 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306 299 }
98a29390 300 else
748a9306 301 sv_inc(TOPs);
a0d0e21e
LW
302 SvSETMAGIC(TOPs);
303 return NORMAL;
304}
305
306PP(pp_or)
307{
4e35701f 308 djSP;
a0d0e21e
LW
309 if (SvTRUE(TOPs))
310 RETURN;
311 else {
312 --SP;
313 RETURNOP(cLOGOP->op_other);
314 }
315}
316
317PP(pp_add)
318{
98a29390 319 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
a0d0e21e 320 {
98a29390
JH
321 dPOPTOPnnrl_ul;
322 SETn( left + right );
323 RETURN;
a0d0e21e
LW
324 }
325}
326
327PP(pp_aelemfast)
328{
4e35701f 329 djSP;
1d7c1841 330 AV *av = GvAV(cGVOP_gv);
533c011a
NIS
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
GS
335 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
336 sv = sv_mortalcopy(sv);
337 PUSHs(sv);
a0d0e21e
LW
338 RETURN;
339}
340
341PP(pp_join)
342{
4e35701f 343 djSP; dMARK; dTARGET;
a0d0e21e
LW
344 MARK++;
345 do_join(TARG, *MARK, MARK, SP);
346 SP = MARK;
347 SETs(TARG);
348 RETURN;
349}
350
351PP(pp_pushre)
352{
4e35701f 353 djSP;
44a8e56a
PP
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
PP
363 XPUSHs(sv);
364#else
6b88bc9c 365 XPUSHs((SV*)PL_op);
44a8e56a 366#endif
a0d0e21e
LW
367 RETURN;
368}
369
370/* Oversized hot code. */
371
372PP(pp_print)
373{
4e35701f 374 djSP; dMARK; dORIGMARK;
a0d0e21e
LW
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
LW
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
NIS
389 * pass object as 1st arg, so move other args up ...
390 */
4352c267 391 MEXTEND(SP, 1);
68dc0745
PP
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
PP
401 LEAVE;
402 SPAGAIN;
68dc0745
PP
403 MARK = ORIGMARK + 1;
404 *MARK = *SP;
405 SP = MARK;
236988e4
PP
406 RETURN;
407 }
a0d0e21e 408 if (!(io = GvIO(gv))) {
01bb7c6d
DC
409 if ((GvEGV(gv)) && (mg = SvTIED_mg((SV*)GvEGV(gv),'q')))
410 goto had_magic;
2dd78f96
JH
411 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
412 report_evil_fh(gv, io, PL_op->op_type);
748a9306 413 SETERRNO(EBADF,RMS$_IFI);
a0d0e21e
LW
414 goto just_say_no;
415 }
416 else if (!(fp = IoOFP(io))) {
599cee73 417 if (ckWARN2(WARN_CLOSED, WARN_IO)) {
4c80c0b2
NC
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
LW
424 goto just_say_no;
425 }
426 else {
427 MARK++;
7889fe52 428 if (PL_ofs_sv && SvOK(PL_ofs_sv)) {
a0d0e21e
LW
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
LW
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
NIS
451 if (PL_ors_sv && SvOK(PL_ors_sv))
452 if (!do_print(PL_ors_sv, fp)) /* $\ */
a0d0e21e
LW
453 goto just_say_no;
454
455 if (IoFLAGS(io) & IOf_FLUSH)
760ac839 456 if (PerlIO_flush(fp) == EOF)
a0d0e21e
LW
457 goto just_say_no;
458 }
459 }
460 SP = ORIGMARK;
3280af22 461 PUSHs(&PL_sv_yes);
a0d0e21e
LW
462 RETURN;
463
464 just_say_no:
465 SP = ORIGMARK;
3280af22 466 PUSHs(&PL_sv_undef);
a0d0e21e
LW
467 RETURN;
468}
469
470PP(pp_rv2av)
471{
f5284f61 472 djSP; dTOPss;
a0d0e21e
LW
473 AV *av;
474
475 if (SvROK(sv)) {
476 wasref:
f5284f61
IZ
477 tryAMAGICunDEREF(to_av);
478
a0d0e21e
LW
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
LW
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
LW
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
LW
502 if (SvGMAGICAL(sv)) {
503 mg_get(sv);
504 if (SvROK(sv))
505 goto wasref;
506 }
507 if (!SvOK(sv)) {
533c011a
NIS
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
IZ
516 }
517 RETSETUNDEF;
a0d0e21e 518 }
c9d5ac95 519 sym = SvPV(sv,len);
35cd451c
GS
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
GS
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
GS
530 }
531 else {
532 if (PL_op->op_private & HINT_STRICT_REFS)
cea2e8a9 533 DIE(aTHX_ PL_no_symref, sym, "an ARRAY");
35cd451c
GS
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
LW
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
NIS
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
NIS
561 else {
562 Copy(AvARRAY(av), SP+1, maxarg, SV*);
563 }
a0d0e21e
LW
564 SP += maxarg;
565 }
566 else {
567 dTARGET;
568 I32 maxarg = AvFILL(av) + 1;
f5284f61 569 SETi(maxarg);
a0d0e21e
LW
570 }
571 RETURN;
572}
573
574PP(pp_rv2hv)
575{
4e35701f 576 djSP; dTOPss;
a0d0e21e
LW
577 HV *hv;
578
579 if (SvROK(sv)) {
580 wasref:
f5284f61
IZ
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
LW
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
LW
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
LW
606 if (SvGMAGICAL(sv)) {
607 mg_get(sv);
608 if (SvROK(sv))
609 goto wasref;
610 }
611 if (!SvOK(sv)) {
533c011a
NIS
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
LW
617 if (GIMME == G_ARRAY) {
618 SP--;
619 RETURN;
620 }
a0d0e21e
LW
621 RETSETUNDEF;
622 }
c9d5ac95 623 sym = SvPV(sv,len);
35cd451c
GS
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
GS
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
GS
634 }
635 else {
636 if (PL_op->op_private & HINT_STRICT_REFS)
cea2e8a9 637 DIE(aTHX_ PL_no_symref, sym, "a HASH");
35cd451c
GS
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
LW
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
LW
657 }
658 else {
659 dTARGET;
4b154ab5
GA
660 if (SvTYPE(hv) == SVt_PVAV)
661 hv = avhv_keys((AV*)hv);
b9c39e73 662 if (HvFILL(hv))
57def98f
JH
663 Perl_sv_setpvf(aTHX_ TARG, "%"IVdf"/%"IVdf,
664 (IV)HvFILL(hv), (IV)HvMAX(hv) + 1);
a0d0e21e
LW
665 else
666 sv_setiv(TARG, 0);
c750a3ec 667
a0d0e21e
LW
668 SETTARG;
669 RETURN;
670 }
671}
672
10c8fecd
GS
673STATIC int
674S_do_maybe_phash(pTHX_ AV *ary, SV **lelem, SV **firstlelem, SV **relem,
675 SV **lastrelem)
676{
677 OP *leftop;
10c8fecd
GS
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
GS
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
718STATIC void
719S_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
GS
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
LW
759PP(pp_aassign)
760{
4e35701f 761 djSP;
3280af22
NIS
762 SV **lastlelem = PL_stack_sp;
763 SV **lastrelem = PL_stack_base + POPMARK;
764 SV **firstrelem = PL_stack_base + POPMARK + 1;
a0d0e21e
LW
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
LW
774 HV *hash;
775 I32 i;
776 int magic;
777
3280af22 778 PL_delaymagic = DM_DELAY; /* catch simultaneous items */
a0d0e21e
LW
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
GS
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
LW
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
LW
802 sv = *lelem++;
803 switch (SvTYPE(sv)) {
804 case SVt_PVAV:
805 ary = (AV*)sv;
748a9306 806 magic = SvMAGICAL(ary) != 0;
10c8fecd
GS
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
LW
822 i = 0;
823 while (relem <= lastrelem) { /* gobble up all the rest */
5117ca91 824 SV **didstore;
a0d0e21e
LW
825 sv = NEWSV(28,0);
826 assert(*relem);
827 sv_setsv(sv,*relem);
828 *(relem++) = sv;
5117ca91
GS
829 didstore = av_store(ary,i++,sv);
830 if (magic) {
fb73857a
PP
831 if (SvSMAGICAL(sv))
832 mg_set(sv);
5117ca91 833 if (!didstore)
8127e0e3 834 sv_2mortal(sv);
5117ca91 835 }
bbce6d69 836 TAINT_NOT;
a0d0e21e
LW
837 }
838 break;
10c8fecd 839 case SVt_PVHV: { /* normal hash */
a0d0e21e
LW
840 SV *tmpstr;
841
842 hash = (HV*)sv;
748a9306 843 magic = SvMAGICAL(hash) != 0;
a0d0e21e
LW
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
LW
852 tmpstr = NEWSV(29,0);
853 if (*relem)
854 sv_setsv(tmpstr,*relem); /* value */
855 *(relem++) = tmpstr;
5117ca91
GS
856 didstore = hv_store_ent(hash,sv,tmpstr,0);
857 if (magic) {
fb73857a
PP
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
LW
869 }
870 break;
871 default:
6fc92669
GS
872 if (SvIMMORTAL(sv)) {
873 if (relem <= lastrelem)
874 relem++;
875 break;
a0d0e21e
LW
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
LW
883 SvSETMAGIC(sv);
884 break;
885 }
886 }
3280af22
NIS
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
AD
891#else
892# ifdef HAS_SETREUID
3280af22 893 (void)setreuid(PL_uid,PL_euid);
56febc5e
AD
894# else
895# ifdef HAS_SETRUID
b28d0864
NIS
896 if ((PL_delaymagic & DM_UID) == DM_RUID) {
897 (void)setruid(PL_uid);
898 PL_delaymagic &= ~DM_RUID;
a0d0e21e 899 }
56febc5e
AD
900# endif /* HAS_SETRUID */
901# ifdef HAS_SETEUID
b28d0864
NIS
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
NIS
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
AD
912# endif /* HAS_SETREUID */
913#endif /* HAS_SETRESUID */
d8eceb89
JH
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
AD
920#else
921# ifdef HAS_SETREGID
3280af22 922 (void)setregid(PL_gid,PL_egid);
56febc5e
AD
923# else
924# ifdef HAS_SETRGID
b28d0864
NIS
925 if ((PL_delaymagic & DM_GID) == DM_RGID) {
926 (void)setrgid(PL_gid);
927 PL_delaymagic &= ~DM_RGID;
a0d0e21e 928 }
56febc5e
AD
929# endif /* HAS_SETRGID */
930# ifdef HAS_SETEGID
b28d0864
NIS
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
NIS
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
AD
941# endif /* HAS_SETREGID */
942#endif /* HAS_SETRESGID */
d8eceb89
JH
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
PP
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
LW
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
LW
968}
969
8782bef2
GB
970PP(pp_qr)
971{
972 djSP;
973 register PMOP *pm = cPMOP;
974 SV *rv = sv_newmortal();
57668c4d 975 SV *sv = newSVrv(rv, "Regexp");
8782bef2
GB
976 sv_magic(sv,(SV*)ReREFCNT_inc(pm->op_pmregexp),'r',0,0);
977 RETURNX(PUSHs(rv));
978}
979
a0d0e21e
LW
980PP(pp_match)
981{
4e35701f 982 djSP; dTARG;
a0d0e21e
LW
983 register PMOP *pm = cPMOP;
984 register char *t;
985 register char *s;
986 char *strend;
987 I32 global;
f722798b
IZ
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
LW
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
LW
1000 TARG = POPs;
1001 else {
54b9620d 1002 TARG = DEFSV;
a0d0e21e
LW
1003 EXTEND(SP,1);
1004 }
c277df42 1005 PUTBACK; /* EVAL blocks need stack_sp. */
a0d0e21e
LW
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
LW
1016 if (gimme == G_ARRAY)
1017 RETURN;
1018 RETPUSHNO;
1019 }
1020
3280af22
NIS
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
IZ
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
LW
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
HS
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
LW
1044 }
1045 }
0ef3e39e
HS
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
IZ
1050 r_flags |= REXEC_SCREAM;
1051
a0d0e21e 1052 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
3280af22
NIS
1053 SAVEINT(PL_multiline);
1054 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
a0d0e21e
LW
1055 }
1056
1057play_it_again:
cf93c79d
IZ
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
IZ
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
IZ
1072 && ((rx->reganch & ROPT_NOSCAN)
1073 || !((rx->reganch & RE_INTUIT_TAIL)
05b4157f
GS
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
LW
1083 goto gotcha;
1084 }
1085 else
1086 goto ret_no;
1087 /*NOTREACHED*/
1088
1089 gotcha:
72311751
GS
1090 if (rxtainted)
1091 RX_MATCH_TAINTED_on(rx);
1092 TAINT_IF(RX_MATCH_TAINTED(rx));
a0d0e21e
LW
1093 if (gimme == G_ARRAY) {
1094 I32 iters, i, len;
1095
d9f97599 1096 iters = rx->nparens;
a0d0e21e
LW
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
LW
1104 for (i = !i; i <= iters; i++) {
1105 PUSHs(sv_newmortal());
1106 /*SUPPRESS 560*/
cf93c79d
IZ
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
GS
1111 if ((pm->op_pmdynflags & PMdf_UTF8) && !IN_BYTE) {
1112 SvUTF8_on(*SP);
1113 sv_utf8_downgrade(*SP, TRUE);
1114 }
a0d0e21e
LW
1115 }
1116 }
1117 if (global) {
cf93c79d
IZ
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
LW
1122 goto play_it_again;
1123 }
f7e33566 1124 else if (!iters)
bde848c5 1125 XPUSHs(&PL_sv_yes);
4633a7c4 1126 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
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
IZ
1138 if (rx->startp[0] != -1) {
1139 mg->mg_len = rx->endp[0];
d9f97599 1140 if (rx->startp[0] == rx->endp[0])
748a9306
LW
1141 mg->mg_flags |= MGf_MINMATCH;
1142 else
1143 mg->mg_flags &= ~MGf_MINMATCH;
1144 }
a0d0e21e 1145 }
4633a7c4 1146 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1147 RETPUSHYES;
1148 }
1149
f722798b 1150yup: /* Confirmed by INTUIT */
72311751
GS
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
IZ
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
IZ
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
IZ
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
LW
1183 RETPUSHYES;
1184
1185nope:
a0d0e21e 1186ret_no:
c90c0ff4 1187 if (global && !(pm->op_pmflags & PMf_CONTINUE)) {
a0d0e21e
LW
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
LW
1192 }
1193 }
4633a7c4 1194 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1195 if (gimme == G_ARRAY)
1196 RETURN;
1197 RETPUSHNO;
1198}
1199
1200OP *
864dbfa3 1201Perl_do_readline(pTHX)
a0d0e21e
LW
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
PP
1216 PUTBACK;
1217 ENTER;
864dbfa3 1218 call_method("READLINE", gimme);
e79b0511
PP
1219 LEAVE;
1220 SPAGAIN;
54310121
PP
1221 if (gimme == G_SCALAR)
1222 SvSetMagicSV_nosteal(TARG, TOPs);
e79b0511
PP
1223 RETURN;
1224 }
a0d0e21e
LW
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
NIS
1235 sv_setpvn(GvSV(PL_last_in_gv), "-", 1);
1236 SvSETMAGIC(GvSV(PL_last_in_gv));
a2008d6d
GS
1237 fp = IoIFP(io);
1238 goto have_fp;
a0d0e21e
LW
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
LW
1244 }
1245 }
0d44d22b
NC
1246 else if (type == OP_GLOB)
1247 fp = Perl_start_glob(aTHX_ POPs, io);
a0d0e21e
LW
1248 }
1249 else if (type == OP_GLOB)
1250 SP--;
af8c498a 1251 else if (ckWARN(WARN_IO) /* stdout/stderr or other write fh */
9f37169a 1252 && (IoTYPE(io) == IoTYPE_WRONLY || fp == PerlIO_stdout()
af8c498a 1253 || fp == PerlIO_stderr()))
4c80c0b2 1254 report_evil_fh(PL_last_in_gv, io, OP_phoney_OUTPUT_ONLY);
a0d0e21e
LW
1255 }
1256 if (!fp) {
790090df
HS
1257 if (ckWARN2(WARN_GLOB, WARN_CLOSED)
1258 && (!io || !(IoFLAGS(io) & IOf_START))) {
3f4520fe 1259 if (type == OP_GLOB)
e476b1b5 1260 Perl_warner(aTHX_ WARN_GLOB,
af8c498a
GS
1261 "glob failed (can't start child: %s)",
1262 Strerror(errno));
69282e91 1263 else
bc37a18f 1264 report_evil_fh(PL_last_in_gv, io, PL_op->op_type);
3f4520fe 1265 }
54310121 1266 if (gimme == G_SCALAR) {
a0d0e21e
LW
1267 (void)SvOK_off(TARG);
1268 PUSHTARG;
1269 }
1270 RETURN;
1271 }
a2008d6d 1272 have_fp:
54310121 1273 if (gimme == G_SCALAR) {
a0d0e21e 1274 sv = TARG;
9607fc9c
PP
1275 if (SvROK(sv))
1276 sv_unref(sv);
a0d0e21e
LW
1277 (void)SvUPGRADE(sv, SVt_PV);
1278 tmplen = SvLEN(sv); /* remember if already alloced */
1279 if (!tmplen)
1280 Sv_Grow(sv, 80); /* try short-buffering it */
1281 if (type == OP_RCATLINE)
1282 offset = SvCUR(sv);
1283 else
1284 offset = 0;
1285 }
54310121
PP
1286 else {
1287 sv = sv_2mortal(NEWSV(57, 80));
1288 offset = 0;
1289 }
fbad3eb5 1290
3887d568
AP
1291 /* This should not be marked tainted if the fp is marked clean */
1292#define MAYBE_TAINT_LINE(io, sv) \
1293 if (!(IoFLAGS(io) & IOf_UNTAINT)) { \
1294 TAINT; \
1295 SvTAINTED_on(sv); \
1296 }
1297
684bef36 1298/* delay EOF state for a snarfed empty file */
fbad3eb5 1299#define SNARF_EOF(gimme,rs,io,sv) \
684bef36 1300 (gimme != G_SCALAR || SvCUR(sv) \
b9fee9ba 1301 || (IoFLAGS(io) & IOf_NOLINE) || !RsSNARF(rs))
fbad3eb5 1302
a0d0e21e 1303 for (;;) {
fbad3eb5
GS
1304 if (!sv_gets(sv, fp, offset)
1305 && (type == OP_GLOB || SNARF_EOF(gimme, PL_rs, io, sv)))
1306 {
760ac839 1307 PerlIO_clearerr(fp);
a0d0e21e 1308 if (IoFLAGS(io) & IOf_ARGV) {
3280af22 1309 fp = nextargv(PL_last_in_gv);
a0d0e21e
LW
1310 if (fp)
1311 continue;
3280af22 1312 (void)do_close(PL_last_in_gv, FALSE);
a0d0e21e
LW
1313 }
1314 else if (type == OP_GLOB) {
e476b1b5
GS
1315 if (!do_close(PL_last_in_gv, FALSE) && ckWARN(WARN_GLOB)) {
1316 Perl_warner(aTHX_ WARN_GLOB,
4eb79ab5 1317 "glob failed (child exited with status %d%s)",
894356b3 1318 (int)(STATUS_CURRENT >> 8),
cf494569 1319 (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
4eb79ab5 1320 }
a0d0e21e 1321 }
54310121 1322 if (gimme == G_SCALAR) {
a0d0e21e
LW
1323 (void)SvOK_off(TARG);
1324 PUSHTARG;
1325 }
3887d568 1326 MAYBE_TAINT_LINE(io, sv);
a0d0e21e
LW
1327 RETURN;
1328 }
3887d568 1329 MAYBE_TAINT_LINE(io, sv);
a0d0e21e 1330 IoLINES(io)++;
b9fee9ba 1331 IoFLAGS(io) |= IOf_NOLINE;
71be2cbc 1332 SvSETMAGIC(sv);
a0d0e21e 1333 XPUSHs(sv);
a0d0e21e
LW
1334 if (type == OP_GLOB) {
1335 char *tmps;
1336
3280af22 1337 if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) {
c07a80fd 1338 tmps = SvEND(sv) - 1;
3280af22 1339 if (*tmps == *SvPVX(PL_rs)) {
c07a80fd
PP
1340 *tmps = '\0';
1341 SvCUR(sv)--;
1342 }
1343 }
a0d0e21e
LW
1344 for (tmps = SvPVX(sv); *tmps; tmps++)
1345 if (!isALPHA(*tmps) && !isDIGIT(*tmps) &&
1346 strchr("$&*(){}[]'\";\\|?<>~`", *tmps))
1347 break;
43384a1a 1348 if (*tmps && PerlLIO_lstat(SvPVX(sv), &PL_statbuf) < 0) {
a0d0e21e
LW
1349 (void)POPs; /* Unmatched wildcard? Chuck it... */
1350 continue;
1351 }
1352 }
54310121 1353 if (gimme == G_ARRAY) {
a0d0e21e
LW
1354 if (SvLEN(sv) - SvCUR(sv) > 20) {
1355 SvLEN_set(sv, SvCUR(sv)+1);
1356 Renew(SvPVX(sv), SvLEN(sv), char);
1357 }
1358 sv = sv_2mortal(NEWSV(58, 80));
1359 continue;
1360 }
54310121 1361 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
a0d0e21e
LW
1362 /* try to reclaim a bit of scalar space (only on 1st alloc) */
1363 if (SvCUR(sv) < 60)
1364 SvLEN_set(sv, 80);
1365 else
1366 SvLEN_set(sv, SvCUR(sv)+40); /* allow some slop */
1367 Renew(SvPVX(sv), SvLEN(sv), char);
1368 }
1369 RETURN;
1370 }
1371}
1372
1373PP(pp_enter)
1374{
4e35701f 1375 djSP;
c09156bb 1376 register PERL_CONTEXT *cx;
533c011a 1377 I32 gimme = OP_GIMME(PL_op, -1);
a0d0e21e 1378
54310121
PP
1379 if (gimme == -1) {
1380 if (cxstack_ix >= 0)
1381 gimme = cxstack[cxstack_ix].blk_gimme;
1382 else
1383 gimme = G_SCALAR;
1384 }
a0d0e21e
LW
1385
1386 ENTER;
1387
1388 SAVETMPS;
924508f0 1389 PUSHBLOCK(cx, CXt_BLOCK, SP);
a0d0e21e
LW
1390
1391 RETURN;
1392}
1393
1394PP(pp_helem)
1395{
4e35701f 1396 djSP;
760ac839 1397 HE* he;
ae77835f 1398 SV **svp;
a0d0e21e 1399 SV *keysv = POPs;
a0d0e21e 1400 HV *hv = (HV*)POPs;
533c011a
NIS
1401 U32 lval = PL_op->op_flags & OPf_MOD;
1402 U32 defer = PL_op->op_private & OPpLVAL_DEFER;
be6c24e0 1403 SV *sv;
1c846c1f 1404 U32 hash = (SvFAKE(keysv) && SvREADONLY(keysv)) ? SvUVX(keysv) : 0;
1f5346dc 1405 I32 preeminent;
a0d0e21e 1406
ae77835f 1407 if (SvTYPE(hv) == SVt_PVHV) {
1f5346dc
SC
1408 if (PL_op->op_private & OPpLVAL_INTRO)
1409 preeminent = SvRMAGICAL(hv) ? 1 : hv_exists_ent(hv, keysv, 0);
1c846c1f 1410 he = hv_fetch_ent(hv, keysv, lval && !defer, hash);
97fcbf96 1411 svp = he ? &HeVAL(he) : 0;
ae77835f
MB
1412 }
1413 else if (SvTYPE(hv) == SVt_PVAV) {
0ebe0038 1414 if (PL_op->op_private & OPpLVAL_INTRO)
cea2e8a9 1415 DIE(aTHX_ "Can't localize pseudo-hash element");
1c846c1f 1416 svp = avhv_fetch_ent((AV*)hv, keysv, lval && !defer, hash);
ae77835f 1417 }
c750a3ec 1418 else {
a0d0e21e 1419 RETPUSHUNDEF;
c750a3ec 1420 }
a0d0e21e 1421 if (lval) {
3280af22 1422 if (!svp || *svp == &PL_sv_undef) {
68dc0745
PP
1423 SV* lv;
1424 SV* key2;
2d8e6c8d
GS
1425 if (!defer) {
1426 STRLEN n_a;
cea2e8a9 1427 DIE(aTHX_ PL_no_helem, SvPV(keysv, n_a));
2d8e6c8d 1428 }
68dc0745
PP
1429 lv = sv_newmortal();
1430 sv_upgrade(lv, SVt_PVLV);
1431 LvTYPE(lv) = 'y';
1432 sv_magic(lv, key2 = newSVsv(keysv), 'y', Nullch, 0);
1433 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1434 LvTARG(lv) = SvREFCNT_inc(hv);
1435 LvTARGLEN(lv) = 1;
1436 PUSHs(lv);
1437 RETURN;
1438 }
533c011a 1439 if (PL_op->op_private & OPpLVAL_INTRO) {
ae77835f 1440 if (HvNAME(hv) && isGV(*svp))
533c011a 1441 save_gp((GV*)*svp, !(PL_op->op_flags & OPf_SPECIAL));
1f5346dc
SC
1442 else {
1443 if (!preeminent) {
1444 STRLEN keylen;
1445 char *key = SvPV(keysv, keylen);
1446 save_delete(hv, key, keylen);
1447 } else
1448 save_helem(hv, keysv, svp);
1449 }
5f05dabc 1450 }
533c011a
NIS
1451 else if (PL_op->op_private & OPpDEREF)
1452 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
a0d0e21e 1453 }
3280af22 1454 sv = (svp ? *svp : &PL_sv_undef);
be6c24e0
GS
1455 /* This makes C<local $tied{foo} = $tied{foo}> possible.
1456 * Pushing the magical RHS on to the stack is useless, since
1457 * that magic is soon destined to be misled by the local(),
1458 * and thus the later pp_sassign() will fail to mg_get() the
1459 * old value. This should also cure problems with delayed
1460 * mg_get()s. GSAR 98-07-03 */
1461 if (!lval && SvGMAGICAL(sv))
1462 sv = sv_mortalcopy(sv);
1463 PUSHs(sv);
a0d0e21e
LW
1464 RETURN;
1465}
1466
1467PP(pp_leave)
1468{
4e35701f 1469 djSP;
c09156bb 1470 register PERL_CONTEXT *cx;
a0d0e21e
LW
1471 register SV **mark;
1472 SV **newsp;
1473 PMOP *newpm;
1474 I32 gimme;
1475
533c011a 1476 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e 1477 cx = &cxstack[cxstack_ix];
3280af22 1478 cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */
a0d0e21e
LW
1479 }
1480
1481 POPBLOCK(cx,newpm);
1482
533c011a 1483 gimme = OP_GIMME(PL_op, -1);
54310121
PP
1484 if (gimme == -1) {
1485 if (cxstack_ix >= 0)
1486 gimme = cxstack[cxstack_ix].blk_gimme;
1487 else
1488 gimme = G_SCALAR;
1489 }
a0d0e21e 1490
a1f49e72 1491 TAINT_NOT;
54310121
PP
1492 if (gimme == G_VOID)
1493 SP = newsp;
1494 else if (gimme == G_SCALAR) {
1495 MARK = newsp + 1;
1496 if (MARK <= SP)
1497 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1498 *MARK = TOPs;
1499 else
1500 *MARK = sv_mortalcopy(TOPs);
a0d0e21e 1501 else {
54310121 1502 MEXTEND(mark,0);
3280af22 1503 *MARK = &PL_sv_undef;
a0d0e21e 1504 }
54310121 1505 SP = MARK;
a0d0e21e 1506 }
54310121 1507 else if (gimme == G_ARRAY) {
a1f49e72
CS
1508 /* in case LEAVE wipes old return values */
1509 for (mark = newsp + 1; mark <= SP; mark++) {
1510 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
a0d0e21e 1511 *mark = sv_mortalcopy(*mark);
a1f49e72
CS
1512 TAINT_NOT; /* Each item is independent */
1513 }
1514 }
a0d0e21e 1515 }
3280af22 1516 PL_curpm = newpm; /* Don't pop $1 et al till now */
a0d0e21e
LW
1517
1518 LEAVE;
1519
1520 RETURN;
1521}
1522
1523PP(pp_iter)
1524{
4e35701f 1525 djSP;
c09156bb 1526 register PERL_CONTEXT *cx;
5f05dabc 1527 SV* sv;
4633a7c4 1528 AV* av;
1d7c1841 1529 SV **itersvp;
a0d0e21e 1530
924508f0 1531 EXTEND(SP, 1);
a0d0e21e 1532 cx = &cxstack[cxstack_ix];
6b35e009 1533 if (CxTYPE(cx) != CXt_LOOP)
cea2e8a9 1534 DIE(aTHX_ "panic: pp_iter");
a0d0e21e 1535
1d7c1841 1536 itersvp = CxITERVAR(cx);
4633a7c4 1537 av = cx->blk_loop.iterary;
89ea2908
GA
1538 if (SvTYPE(av) != SVt_PVAV) {
1539 /* iterate ($min .. $max) */
1540 if (cx->blk_loop.iterlval) {
1541 /* string increment */
1542 register SV* cur = cx->blk_loop.iterlval;
1543 STRLEN maxlen;
1544 char *max = SvPV((SV*)av, maxlen);
1545 if (!SvNIOK(cur) && SvCUR(cur) <= maxlen) {
eaa5c2d6 1546#ifndef USE_THREADS /* don't risk potential race */
1d7c1841 1547 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
eaa5c2d6 1548 /* safe to reuse old SV */
1d7c1841 1549 sv_setsv(*itersvp, cur);
eaa5c2d6 1550 }
1c846c1f 1551 else
eaa5c2d6
GA
1552#endif
1553 {
1554 /* we need a fresh SV every time so that loop body sees a
1555 * completely new SV for closures/references to work as
1556 * they used to */
1d7c1841
GS
1557 SvREFCNT_dec(*itersvp);
1558 *itersvp = newSVsv(cur);
eaa5c2d6 1559 }
89ea2908
GA
1560 if (strEQ(SvPVX(cur), max))
1561 sv_setiv(cur, 0); /* terminate next time */
1562 else
1563 sv_inc(cur);
1564 RETPUSHYES;
1565 }
1566 RETPUSHNO;
1567 }
1568 /* integer increment */
1569 if (cx->blk_loop.iterix > cx->blk_loop.itermax)
1570 RETPUSHNO;
7f61b687 1571
eaa5c2d6 1572#ifndef USE_THREADS /* don't risk potential race */
1d7c1841 1573 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
eaa5c2d6 1574 /* safe to reuse old SV */
1d7c1841 1575 sv_setiv(*itersvp, cx->blk_loop.iterix++);
eaa5c2d6 1576 }
1c846c1f 1577 else
eaa5c2d6
GA
1578#endif
1579 {
1580 /* we need a fresh SV every time so that loop body sees a
1581 * completely new SV for closures/references to work as they
1582 * used to */
1d7c1841
GS
1583 SvREFCNT_dec(*itersvp);
1584 *itersvp = newSViv(cx->blk_loop.iterix++);
eaa5c2d6 1585 }
89ea2908
GA
1586 RETPUSHYES;
1587 }
1588
1589 /* iterate array */
3280af22 1590 if (cx->blk_loop.iterix >= (av == PL_curstack ? cx->blk_oldsp : AvFILL(av)))
4633a7c4 1591 RETPUSHNO;
a0d0e21e 1592
1d7c1841 1593 SvREFCNT_dec(*itersvp);
a0d0e21e 1594
155aba94 1595 if ((sv = SvMAGICAL(av)
1c846c1f 1596 ? *av_fetch(av, ++cx->blk_loop.iterix, FALSE)
155aba94 1597 : AvARRAY(av)[++cx->blk_loop.iterix]))
a0d0e21e 1598 SvTEMP_off(sv);
a0d0e21e 1599 else
3280af22
NIS
1600 sv = &PL_sv_undef;
1601 if (av != PL_curstack && SvIMMORTAL(sv)) {
5f05dabc 1602 SV *lv = cx->blk_loop.iterlval;
71be2cbc
PP
1603 if (lv && SvREFCNT(lv) > 1) {
1604 SvREFCNT_dec(lv);
1605 lv = Nullsv;
1606 }
5f05dabc
PP
1607 if (lv)
1608 SvREFCNT_dec(LvTARG(lv));
1609 else {
68dc0745 1610 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
5f05dabc 1611 sv_upgrade(lv, SVt_PVLV);
5f05dabc 1612 LvTYPE(lv) = 'y';
68dc0745 1613 sv_magic(lv, Nullsv, 'y', Nullch, 0);
5f05dabc
PP
1614 }
1615 LvTARG(lv) = SvREFCNT_inc(av);
1616 LvTARGOFF(lv) = cx->blk_loop.iterix;
42718184 1617 LvTARGLEN(lv) = (STRLEN)UV_MAX;
5f05dabc
PP
1618 sv = (SV*)lv;
1619 }
a0d0e21e 1620
1d7c1841 1621 *itersvp = SvREFCNT_inc(sv);
a0d0e21e
LW
1622 RETPUSHYES;
1623}
1624
1625PP(pp_subst)
1626{
4e35701f 1627 djSP; dTARG;
a0d0e21e
LW
1628 register PMOP *pm = cPMOP;
1629 PMOP *rpm = pm;
1630 register SV *dstr;
1631 register char *s;
1632 char *strend;
1633 register char *m;
1634 char *c;
1635 register char *d;
1636 STRLEN clen;
1637 I32 iters = 0;
1638 I32 maxiters;
1639 register I32 i;
1640 bool once;
71be2cbc 1641 bool rxtainted;
a0d0e21e 1642 char *orig;
22e551b9 1643 I32 r_flags;
d9f97599 1644 register REGEXP *rx = pm->op_pmregexp;
a0d0e21e
LW
1645 STRLEN len;
1646 int force_on_match = 0;
3280af22 1647 I32 oldsave = PL_savestack_ix;
a0d0e21e 1648
5cd24f17
PP
1649 /* known replacement string? */
1650 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
533c011a 1651 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
1652 TARG = POPs;
1653 else {
54b9620d 1654 TARG = DEFSV;
a0d0e21e 1655 EXTEND(SP,1);
1c846c1f 1656 }
eca06228
NIS
1657 if (SvFAKE(TARG) && SvREADONLY(TARG))
1658 sv_force_normal(TARG);
68dc0745
PP
1659 if (SvREADONLY(TARG)
1660 || (SvTYPE(TARG) > SVt_PVLV
1661 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG))))
d470f89e 1662 DIE(aTHX_ PL_no_modify);
8ec5e241
NIS
1663 PUTBACK;
1664
a0d0e21e 1665 s = SvPV(TARG, len);
68dc0745 1666 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
a0d0e21e 1667 force_on_match = 1;
b3eb6a9b 1668 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
3280af22
NIS
1669 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
1670 if (PL_tainted)
b3eb6a9b 1671 rxtainted |= 2;
9212bbba 1672 TAINT_NOT;
a0d0e21e
LW
1673
1674 force_it:
1675 if (!pm || !s)
cea2e8a9 1676 DIE(aTHX_ "panic: do_subst");
a0d0e21e
LW
1677
1678 strend = s + len;
1c846c1f 1679 maxiters = 2*(strend - s) + 10; /* We can match twice at each
2beec16e
IZ
1680 position, once with zero-length,
1681 second time with non-zero. */
a0d0e21e 1682
3280af22
NIS
1683 if (!rx->prelen && PL_curpm) {
1684 pm = PL_curpm;
d9f97599 1685 rx = pm->op_pmregexp;
a0d0e21e 1686 }
22e551b9 1687 r_flags = (rx->nparens || SvTEMP(TARG) || PL_sawampersand)
9d080a66 1688 ? REXEC_COPY_STR : 0;
f722798b 1689 if (SvSCREAM(TARG))
22e551b9 1690 r_flags |= REXEC_SCREAM;
a0d0e21e 1691 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
3280af22
NIS
1692 SAVEINT(PL_multiline);
1693 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
a0d0e21e
LW
1694 }
1695 orig = m = s;
f722798b
IZ
1696 if (rx->reganch & RE_USE_INTUIT) {
1697 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, s, strend, r_flags, NULL);
1698
1699 if (!s)
1700 goto nope;
1701 /* How to do it in subst? */
1702/* if ( (rx->reganch & ROPT_CHECK_ALL)
1c846c1f 1703 && !PL_sawampersand
f722798b
IZ
1704 && ((rx->reganch & ROPT_NOSCAN)
1705 || !((rx->reganch & RE_INTUIT_TAIL)
1706 && (r_flags & REXEC_SCREAM))))
1707 goto yup;
1708*/
a0d0e21e 1709 }
71be2cbc
PP
1710
1711 /* only replace once? */
a0d0e21e 1712 once = !(rpm->op_pmflags & PMf_GLOBAL);
71be2cbc
PP
1713
1714 /* known replacement string? */
5cd24f17 1715 c = dstr ? SvPV(dstr, clen) : Nullch;
71be2cbc
PP
1716
1717 /* can do inplace substitution? */
22e551b9 1718 if (c && clen <= rx->minlen && (once || !(r_flags & REXEC_COPY_STR))
d9f97599 1719 && !(rx->reganch & ROPT_LOOKBEHIND_SEEN)) {
f722798b
IZ
1720 if (!CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
1721 r_flags | REXEC_CHECKED))
1722 {
8ec5e241 1723 SPAGAIN;
3280af22 1724 PUSHs(&PL_sv_no);
71be2cbc
PP
1725 LEAVE_SCOPE(oldsave);
1726 RETURN;
1727 }
1728 if (force_on_match) {
1729 force_on_match = 0;
1730 s = SvPV_force(TARG, len);
1731 goto force_it;
1732 }
71be2cbc 1733 d = s;
3280af22 1734 PL_curpm = pm;
71be2cbc
PP
1735 SvSCREAM_off(TARG); /* disable possible screamer */
1736 if (once) {
48c036b1 1737 rxtainted |= RX_MATCH_TAINTED(rx);
cf93c79d
IZ
1738 m = orig + rx->startp[0];
1739 d = orig + rx->endp[0];
71be2cbc
PP
1740 s = orig;
1741 if (m - s > strend - d) { /* faster to shorten from end */
1742 if (clen) {
1743 Copy(c, m, clen, char);
1744 m += clen;
a0d0e21e 1745 }
71be2cbc
PP
1746 i = strend - d;
1747 if (i > 0) {
1748 Move(d, m, i, char);
1749 m += i;
a0d0e21e 1750 }
71be2cbc
PP
1751 *m = '\0';
1752 SvCUR_set(TARG, m - s);
1753 }
1754 /*SUPPRESS 560*/
155aba94 1755 else if ((i = m - s)) { /* faster from front */
71be2cbc
PP
1756 d -= clen;
1757 m = d;
1758 sv_chop(TARG, d-i);
1759 s += i;
1760 while (i--)
1761 *--d = *--s;
1762 if (clen)
1763 Copy(c, m, clen, char);
1764 }
1765 else if (clen) {
1766 d -= clen;
1767 sv_chop(TARG, d);
1768 Copy(c, d, clen, char);
1769 }
1770 else {
1771 sv_chop(TARG, d);
1772 }
48c036b1 1773 TAINT_IF(rxtainted & 1);
8ec5e241 1774 SPAGAIN;
3280af22 1775 PUSHs(&PL_sv_yes);
71be2cbc
PP
1776 }
1777 else {
71be2cbc
PP
1778 do {
1779 if (iters++ > maxiters)
cea2e8a9 1780 DIE(aTHX_ "Substitution loop");
d9f97599 1781 rxtainted |= RX_MATCH_TAINTED(rx);
cf93c79d 1782 m = rx->startp[0] + orig;
71be2cbc 1783 /*SUPPRESS 560*/
155aba94 1784 if ((i = m - s)) {
71be2cbc
PP
1785 if (s != d)
1786 Move(s, d, i, char);
1787 d += i;
a0d0e21e 1788 }
71be2cbc
PP
1789 if (clen) {
1790 Copy(c, d, clen, char);
1791 d += clen;
1792 }
cf93c79d 1793 s = rx->endp[0] + orig;
cea2e8a9 1794 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
f722798b
IZ
1795 TARG, NULL,
1796 /* don't match same null twice */
1797 REXEC_NOT_FIRST|REXEC_IGNOREPOS));
71be2cbc
PP
1798 if (s != d) {
1799 i = strend - s;
1800 SvCUR_set(TARG, d - SvPVX(TARG) + i);
1801 Move(s, d, i+1, char); /* include the NUL */
a0d0e21e 1802 }
48c036b1 1803 TAINT_IF(rxtainted & 1);
8ec5e241 1804 SPAGAIN;
71be2cbc 1805 PUSHs(sv_2mortal(newSViv((I32)iters)));
a0d0e21e 1806 }
80b498e0 1807 (void)SvPOK_only_UTF8(TARG);
48c036b1 1808 TAINT_IF(rxtainted);
8ec5e241
NIS
1809 if (SvSMAGICAL(TARG)) {
1810 PUTBACK;
1811 mg_set(TARG);
1812 SPAGAIN;
1813 }
9212bbba 1814 SvTAINT(TARG);
71be2cbc
PP
1815 LEAVE_SCOPE(oldsave);
1816 RETURN;
a0d0e21e 1817 }
71be2cbc 1818
f722798b
IZ
1819 if (CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
1820 r_flags | REXEC_CHECKED))
1821 {
a0d0e21e
LW
1822 if (force_on_match) {
1823 force_on_match = 0;
1824 s = SvPV_force(TARG, len);
1825 goto force_it;
1826 }
48c036b1 1827 rxtainted |= RX_MATCH_TAINTED(rx);
8ec5e241 1828 dstr = NEWSV(25, len);
a0d0e21e 1829 sv_setpvn(dstr, m, s-m);
3280af22 1830 PL_curpm = pm;
a0d0e21e 1831 if (!c) {
c09156bb 1832 register PERL_CONTEXT *cx;
8ec5e241 1833 SPAGAIN;
a0d0e21e
LW
1834 PUSHSUBST(cx);
1835 RETURNOP(cPMOP->op_pmreplroot);
1836 }
cf93c79d 1837 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
a0d0e21e
LW
1838 do {
1839 if (iters++ > maxiters)
cea2e8a9 1840 DIE(aTHX_ "Substitution loop");
d9f97599 1841 rxtainted |= RX_MATCH_TAINTED(rx);
cf93c79d 1842 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
a0d0e21e
LW
1843 m = s;
1844 s = orig;
cf93c79d 1845 orig = rx->subbeg;
a0d0e21e
LW
1846 s = orig + (m - s);
1847 strend = s + (strend - m);
1848 }
cf93c79d 1849 m = rx->startp[0] + orig;
a0d0e21e 1850 sv_catpvn(dstr, s, m-s);
cf93c79d 1851 s = rx->endp[0] + orig;
a0d0e21e
LW
1852 if (clen)
1853 sv_catpvn(dstr, c, clen);
1854 if (once)
1855 break;
cea2e8a9 1856 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m, TARG, NULL, r_flags));
a0d0e21e 1857 sv_catpvn(dstr, s, strend - s);
748a9306 1858
4633a7c4 1859 (void)SvOOK_off(TARG);
cb0b1708 1860 Safefree(SvPVX(TARG));
748a9306
LW
1861 SvPVX(TARG) = SvPVX(dstr);
1862 SvCUR_set(TARG, SvCUR(dstr));
1863 SvLEN_set(TARG, SvLEN(dstr));
1864 SvPVX(dstr) = 0;
1865 sv_free(dstr);
1866
48c036b1 1867 TAINT_IF(rxtainted & 1);
f878fbec 1868 SPAGAIN;
48c036b1
GS
1869 PUSHs(sv_2mortal(newSViv((I32)iters)));
1870
a0d0e21e 1871 (void)SvPOK_only(TARG);
48c036b1 1872 TAINT_IF(rxtainted);
a0d0e21e 1873 SvSETMAGIC(TARG);
9212bbba 1874 SvTAINT(TARG);
4633a7c4 1875 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1876 RETURN;
1877 }
5cd24f17 1878 goto ret_no;
a0d0e21e
LW
1879
1880nope:
1c846c1f 1881ret_no:
8ec5e241 1882 SPAGAIN;
3280af22 1883 PUSHs(&PL_sv_no);
4633a7c4 1884 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1885 RETURN;
1886}
1887
1888PP(pp_grepwhile)
1889{
4e35701f 1890 djSP;
a0d0e21e
LW
1891
1892 if (SvTRUEx(POPs))
3280af22
NIS
1893 PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr];
1894 ++*PL_markstack_ptr;
a0d0e21e
LW
1895 LEAVE; /* exit inner scope */
1896
1897 /* All done yet? */
3280af22 1898 if (PL_stack_base + *PL_markstack_ptr > SP) {
a0d0e21e 1899 I32 items;
54310121 1900 I32 gimme = GIMME_V;
a0d0e21e
LW
1901
1902 LEAVE; /* exit outer scope */
1903 (void)POPMARK; /* pop src */
3280af22 1904 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
a0d0e21e 1905 (void)POPMARK; /* pop dst */
3280af22 1906 SP = PL_stack_base + POPMARK; /* pop original mark */
54310121 1907 if (gimme == G_SCALAR) {
a0d0e21e
LW
1908 dTARGET;
1909 XPUSHi(items);
a0d0e21e 1910 }
54310121
PP
1911 else if (gimme == G_ARRAY)
1912 SP += items;
a0d0e21e
LW
1913 RETURN;
1914 }
1915 else {
1916 SV *src;
1917
1918 ENTER; /* enter inner scope */
1d7c1841 1919 SAVEVPTR(PL_curpm);
a0d0e21e 1920
3280af22 1921 src = PL_stack_base[*PL_markstack_ptr];
a0d0e21e 1922 SvTEMP_off(src);
54b9620d 1923 DEFSV = src;
a0d0e21e
LW
1924
1925 RETURNOP(cLOGOP->op_other);
1926 }
1927}
1928
1929PP(pp_leavesub)
1930{
4e35701f 1931 djSP;
a0d0e21e
LW
1932 SV **mark;
1933 SV **newsp;
1934 PMOP *newpm;
1935 I32 gimme;
c09156bb 1936 register PERL_CONTEXT *cx;
b0d9ce38 1937 SV *sv;
a0d0e21e
LW
1938
1939 POPBLOCK(cx,newpm);
1c846c1f 1940
a1f49e72 1941 TAINT_NOT;
a0d0e21e
LW
1942 if (gimme == G_SCALAR) {
1943 MARK = newsp + 1;
a29cdaf0 1944 if (MARK <= SP) {
a8bba7fa 1945 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
a29cdaf0
IZ
1946 if (SvTEMP(TOPs)) {
1947 *MARK = SvREFCNT_inc(TOPs);
1948 FREETMPS;
1949 sv_2mortal(*MARK);
cd06dffe
GS
1950 }
1951 else {
959e3673 1952 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
a29cdaf0 1953 FREETMPS;
959e3673
GS
1954 *MARK = sv_mortalcopy(sv);
1955 SvREFCNT_dec(sv);
a29cdaf0 1956 }
cd06dffe
GS
1957 }
1958 else
a29cdaf0 1959 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
cd06dffe
GS
1960 }
1961 else {
f86702cc 1962 MEXTEND(MARK, 0);
3280af22 1963 *MARK = &PL_sv_undef;
a0d0e21e
LW
1964 }
1965 SP = MARK;
1966 }
54310121 1967 else if (gimme == G_ARRAY) {
f86702cc 1968 for (MARK = newsp + 1; MARK <= SP; MARK++) {
a1f49e72 1969 if (!SvTEMP(*MARK)) {
f86702cc 1970 *MARK = sv_mortalcopy(*MARK);
a1f49e72
CS
1971 TAINT_NOT; /* Each item is independent */
1972 }
f86702cc 1973 }
a0d0e21e 1974 }
f86702cc 1975 PUTBACK;
1c846c1f 1976
b0d9ce38 1977 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
3280af22 1978 PL_curpm = newpm; /* ... and pop $1 et al */
a0d0e21e
LW
1979
1980 LEAVE;
b0d9ce38 1981 LEAVESUB(sv);
a0d0e21e
LW
1982 return pop_return();
1983}
1984
cd06dffe
GS
1985/* This duplicates the above code because the above code must not
1986 * get any slower by more conditions */
1987PP(pp_leavesublv)
1988{
1989 djSP;
1990 SV **mark;
1991 SV **newsp;
1992 PMOP *newpm;
1993 I32 gimme;
1994 register PERL_CONTEXT *cx;
b0d9ce38 1995 SV *sv;
cd06dffe
GS
1996
1997 POPBLOCK(cx,newpm);
1c846c1f 1998
cd06dffe
GS
1999 TAINT_NOT;
2000
2001 if (cx->blk_sub.lval & OPpENTERSUB_INARGS) {
2002 /* We are an argument to a function or grep().
2003 * This kind of lvalueness was legal before lvalue
2004 * subroutines too, so be backward compatible:
2005 * cannot report errors. */
2006
2007 /* Scalar context *is* possible, on the LHS of -> only,
2008 * as in f()->meth(). But this is not an lvalue. */
2009 if (gimme == G_SCALAR)
2010 goto temporise;
2011 if (gimme == G_ARRAY) {
a8bba7fa 2012 if (!CvLVALUE(cx->blk_sub.cv))
cd06dffe
GS
2013 goto temporise_array;
2014 EXTEND_MORTAL(SP - newsp);
2015 for (mark = newsp + 1; mark <= SP; mark++) {
2016 if (SvTEMP(*mark))
2017 /* empty */ ;
2018 else if (SvFLAGS(*mark) & (SVs_PADTMP | SVf_READONLY))
2019 *mark = sv_mortalcopy(*mark);
2020 else {
2021 /* Can be a localized value subject to deletion. */
2022 PL_tmps_stack[++PL_tmps_ix] = *mark;
e1f15930 2023 (void)SvREFCNT_inc(*mark);
cd06dffe
GS
2024 }
2025 }
2026 }
2027 }
2028 else if (cx->blk_sub.lval) { /* Leave it as it is if we can. */
2029 /* Here we go for robustness, not for speed, so we change all
2030 * the refcounts so the caller gets a live guy. Cannot set
2031 * TEMP, so sv_2mortal is out of question. */
a8bba7fa 2032 if (!CvLVALUE(cx->blk_sub.cv)) {
b0d9ce38 2033 POPSUB(cx,sv);
d470f89e 2034 PL_curpm = newpm;
b0d9ce38
GS
2035 LEAVE;
2036 LEAVESUB(sv);
d470f89e
GS
2037 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
2038 }
cd06dffe
GS
2039 if (gimme == G_SCALAR) {
2040 MARK = newsp + 1;
2041 EXTEND_MORTAL(1);
2042 if (MARK == SP) {
d470f89e 2043 if (SvFLAGS(TOPs) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) {
b0d9ce38 2044 POPSUB(cx,sv);
d470f89e 2045 PL_curpm = newpm;
b0d9ce38
GS
2046 LEAVE;
2047 LEAVESUB(sv);
d470f89e 2048 DIE(aTHX_ "Can't return a %s from lvalue subroutine",
cd06dffe 2049 SvREADONLY(TOPs) ? "readonly value" : "temporary");
d470f89e 2050 }
cd06dffe
GS
2051 else { /* Can be a localized value
2052 * subject to deletion. */
2053 PL_tmps_stack[++PL_tmps_ix] = *mark;
e1f15930 2054 (void)SvREFCNT_inc(*mark);
cd06dffe
GS
2055 }
2056 }
d470f89e 2057 else { /* Should not happen? */
b0d9ce38 2058 POPSUB(cx,sv);
d470f89e 2059 PL_curpm = newpm;
b0d9ce38
GS
2060 LEAVE;
2061 LEAVESUB(sv);
d470f89e 2062 DIE(aTHX_ "%s returned from lvalue subroutine in scalar context",
cd06dffe 2063 (MARK > SP ? "Empty array" : "Array"));
d470f89e 2064 }
cd06dffe
GS
2065 SP = MARK;
2066 }
2067 else if (gimme == G_ARRAY) {
2068 EXTEND_MORTAL(SP - newsp);
2069 for (mark = newsp + 1; mark <= SP; mark++) {
d470f89e
GS
2070 if (SvFLAGS(*mark) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) {
2071 /* Might be flattened array after $#array = */
2072 PUTBACK;
b0d9ce38 2073 POPSUB(cx,sv);
d470f89e 2074 PL_curpm = newpm;
b0d9ce38
GS
2075 LEAVE;
2076 LEAVESUB(sv);
d470f89e 2077 DIE(aTHX_ "Can't return %s from lvalue subroutine",
cd06dffe
GS
2078 (*mark != &PL_sv_undef)
2079 ? (SvREADONLY(TOPs)
2080 ? "a readonly value" : "a temporary")
2081 : "an uninitialized value");
d470f89e 2082 }
cd06dffe 2083 else {
cd06dffe
GS
2084 /* Can be a localized value subject to deletion. */
2085 PL_tmps_stack[++PL_tmps_ix] = *mark;
e1f15930 2086 (void)SvREFCNT_inc(*mark);
cd06dffe
GS
2087 }
2088 }
2089 }
2090 }
2091 else {
2092 if (gimme == G_SCALAR) {
2093 temporise:
2094 MARK = newsp + 1;
2095 if (MARK <= SP) {
a8bba7fa 2096 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
cd06dffe
GS
2097 if (SvTEMP(TOPs)) {
2098 *MARK = SvREFCNT_inc(TOPs);
2099 FREETMPS;
2100 sv_2mortal(*MARK);
2101 }
2102 else {
959e3673 2103 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
cd06dffe 2104 FREETMPS;
959e3673
GS
2105 *MARK = sv_mortalcopy(sv);
2106 SvREFCNT_dec(sv);
cd06dffe
GS
2107 }
2108 }
2109 else
2110 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
2111 }
2112 else {
2113 MEXTEND(MARK, 0);
2114 *MARK = &PL_sv_undef;
2115 }
2116 SP = MARK;
2117 }
2118 else if (gimme == G_ARRAY) {
2119 temporise_array:
2120 for (MARK = newsp + 1; MARK <= SP; MARK++) {
2121 if (!SvTEMP(*MARK)) {
2122 *MARK = sv_mortalcopy(*MARK);
2123 TAINT_NOT; /* Each item is independent */
2124 }
2125 }
2126 }
2127 }
2128 PUTBACK;
1c846c1f 2129
b0d9ce38 2130 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
cd06dffe
GS
2131 PL_curpm = newpm; /* ... and pop $1 et al */
2132
2133 LEAVE;
b0d9ce38 2134 LEAVESUB(sv);
cd06dffe
GS
2135 return pop_return();
2136}
2137
2138
76e3520e 2139STATIC CV *
cea2e8a9 2140S_get_db_sub(pTHX_ SV **svp, CV *cv)
3de9ffa1 2141{
3280af22 2142 SV *dbsv = GvSV(PL_DBsub);
491527d0
GS
2143
2144 if (!PERLDB_SUB_NN) {
2145 GV *gv = CvGV(cv);
2146
2147 save_item(dbsv);
2148 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
1c846c1f 2149 || strEQ(GvNAME(gv), "END")
491527d0
GS
2150 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
2151 !( (SvTYPE(*svp) == SVt_PVGV) && (GvCV((GV*)*svp) == cv)
2152 && (gv = (GV*)*svp) ))) {
2153 /* Use GV from the stack as a fallback. */
2154 /* GV is potentially non-unique, or contain different CV. */
c2e66d9e
GS
2155 SV *tmp = newRV((SV*)cv);
2156 sv_setsv(dbsv, tmp);
2157 SvREFCNT_dec(tmp);
491527d0
GS
2158 }
2159 else {
2160 gv_efullname3(dbsv, gv, Nullch);
2161 }
3de9ffa1
MB
2162 }
2163 else {
155aba94
GS
2164 (void)SvUPGRADE(dbsv, SVt_PVIV);
2165 (void)SvIOK_on(dbsv);
491527d0 2166 SAVEIV(SvIVX(dbsv));
5bc28da9 2167 SvIVX(dbsv) = PTR2IV(cv); /* Do it the quickest way */
3de9ffa1 2168 }
491527d0 2169
3de9ffa1 2170 if (CvXSUB(cv))
3280af22
NIS
2171 PL_curcopdb = PL_curcop;
2172 cv = GvCV(PL_DBsub);
3de9ffa1
MB
2173 return cv;
2174}
2175
a0d0e21e
LW
2176PP(pp_entersub)
2177{
4e35701f 2178 djSP; dPOPss;
a0d0e21e
LW
2179 GV *gv;
2180 HV *stash;
2181 register CV *cv;
c09156bb 2182 register PERL_CONTEXT *cx;
5d94fbed 2183 I32 gimme;
533c011a 2184 bool hasargs = (PL_op->op_flags & OPf_STACKED) != 0;
a0d0e21e
LW
2185
2186 if (!sv)
cea2e8a9 2187 DIE(aTHX_ "Not a CODE reference");
a0d0e21e
LW
2188 switch (SvTYPE(sv)) {
2189 default:
2190 if (!SvROK(sv)) {
748a9306 2191 char *sym;
2d8e6c8d 2192 STRLEN n_a;
748a9306 2193
3280af22 2194 if (sv == &PL_sv_yes) { /* unfound import, ignore */
fb73857a 2195 if (hasargs)
3280af22 2196 SP = PL_stack_base + POPMARK;
a0d0e21e 2197 RETURN;
fb73857a 2198 }
15ff848f
CS
2199 if (SvGMAGICAL(sv)) {
2200 mg_get(sv);
2201 sym = SvPOKp(sv) ? SvPVX(sv) : Nullch;
2202 }
2203 else
2d8e6c8d 2204 sym = SvPV(sv, n_a);
15ff848f 2205 if (!sym)
cea2e8a9 2206 DIE(aTHX_ PL_no_usym, "a subroutine");
533c011a 2207 if (PL_op->op_private & HINT_STRICT_REFS)
cea2e8a9 2208 DIE(aTHX_ PL_no_symref, sym, "a subroutine");
864dbfa3 2209 cv = get_cv(sym, TRUE);
a0d0e21e
LW
2210 break;
2211 }
f5284f61
IZ
2212 {
2213 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
2214 tryAMAGICunDEREF(to_cv);
2215 }
a0d0e21e
LW
2216 cv = (CV*)SvRV(sv);
2217 if (SvTYPE(cv) == SVt_PVCV)
2218 break;
2219 /* FALL THROUGH */
2220 case SVt_PVHV:
2221 case SVt_PVAV:
cea2e8a9 2222 DIE(aTHX_ "Not a CODE reference");
a0d0e21e
LW
2223 case SVt_PVCV:
2224 cv = (CV*)sv;
2225 break;
2226 case SVt_PVGV:
8ebc5c01 2227 if (!(cv = GvCVu((GV*)sv)))
f6ec51f7
GS
2228 cv = sv_2cv(sv, &stash, &gv, FALSE);
2229 if (!cv) {
2230 ENTER;
2231 SAVETMPS;
2232 goto try_autoload;
2233 }
2234 break;
a0d0e21e
LW
2235 }
2236
2237 ENTER;
2238 SAVETMPS;
2239
2240 retry:
a0d0e21e 2241 if (!CvROOT(cv) && !CvXSUB(cv)) {
44a8e56a 2242 GV* autogv;
22239a37 2243 SV* sub_name;
44a8e56a
PP
2244
2245 /* anonymous or undef'd function leaves us no recourse */
2246 if (CvANON(cv) || !(gv = CvGV(cv)))
cea2e8a9 2247 DIE(aTHX_ "Undefined subroutine called");
67caa1fe 2248
44a8e56a
PP
2249 /* autoloaded stub? */
2250 if (cv != GvCV(gv)) {
2251 cv = GvCV(gv);
a0d0e21e 2252 }
44a8e56a 2253 /* should call AUTOLOAD now? */
67caa1fe 2254 else {
f6ec51f7
GS
2255try_autoload:
2256 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
2257 FALSE)))
2258 {
2259 cv = GvCV(autogv);
2260 }
2261 /* sorry */
2262 else {
2263 sub_name = sv_newmortal();
2264 gv_efullname3(sub_name, gv, Nullch);
cea2e8a9 2265 DIE(aTHX_ "Undefined subroutine &%s called", SvPVX(sub_name));
f6ec51f7 2266 }
67caa1fe
GS
2267 }
2268 if (!cv)
cea2e8a9 2269 DIE(aTHX_ "Not a CODE reference");
67caa1fe 2270 goto retry;
a0d0e21e
LW
2271 }
2272
54310121 2273 gimme = GIMME_V;
67caa1fe 2274 if ((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub) && !CvNODEBUG(cv)) {
4f01c5a5 2275 cv = get_db_sub(&sv, cv);
67caa1fe 2276 if (!cv)
cea2e8a9 2277 DIE(aTHX_ "No DBsub routine");
67caa1fe 2278 }
a0d0e21e 2279
11343788 2280#ifdef USE_THREADS
3de9ffa1
MB
2281 /*
2282 * First we need to check if the sub or method requires locking.
458fb581
MB
2283 * If so, we gain a lock on the CV, the first argument or the
2284 * stash (for static methods), as appropriate. This has to be
2285 * inline because for FAKE_THREADS, COND_WAIT inlines code to
2286 * reschedule by returning a new op.
3de9ffa1 2287 */
11343788 2288 MUTEX_LOCK(CvMUTEXP(cv));
77a005ab
MB
2289 if (CvFLAGS(cv) & CVf_LOCKED) {
2290 MAGIC *mg;
2291 if (CvFLAGS(cv) & CVf_METHOD) {
533c011a
NIS
2292 if (SP > PL_stack_base + TOPMARK)
2293 sv = *(PL_stack_base + TOPMARK + 1);
77a005ab 2294 else {
13e08037
GS
2295 AV *av = (AV*)PL_curpad[0];
2296 if (hasargs || !av || AvFILLp(av) < 0
2297 || !(sv = AvARRAY(av)[0]))
2298 {
2299 MUTEX_UNLOCK(CvMUTEXP(cv));
d470f89e 2300 DIE(aTHX_ "no argument for locked method call");
13e08037 2301 }
77a005ab
MB
2302 }
2303 if (SvROK(sv))
2304 sv = SvRV(sv);
458fb581
MB
2305 else {
2306 STRLEN len;
2307 char *stashname = SvPV(sv, len);
2308 sv = (SV*)gv_stashpvn(stashname, len, TRUE);
2309 }
77a005ab
MB
2310 }
2311 else {
2312 sv = (SV*)cv;
2313 }
2314 MUTEX_UNLOCK(CvMUTEXP(cv));
2315 mg = condpair_magic(sv);
2316 MUTEX_LOCK(MgMUTEXP(mg));
2317 if (MgOWNER(mg) == thr)
2318 MUTEX_UNLOCK(MgMUTEXP(mg));
2319 else {
2320 while (MgOWNER(mg))
2321 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
2322 MgOWNER(mg) = thr;
bf49b057 2323 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: pp_entersub lock %p\n",
1fd28e87 2324 thr, sv);)
77a005ab 2325 MUTEX_UNLOCK(MgMUTEXP(mg));
c76ac1ee 2326 SAVEDESTRUCTOR_X(Perl_unlock_condpair, sv);
11343788 2327 }
77a005ab 2328 MUTEX_LOCK(CvMUTEXP(cv));
11343788 2329 }
3de9ffa1
MB
2330 /*
2331 * Now we have permission to enter the sub, we must distinguish
2332 * four cases. (0) It's an XSUB (in which case we don't care
2333 * about ownership); (1) it's ours already (and we're recursing);
2334 * (2) it's free (but we may already be using a cached clone);
2335 * (3) another thread owns it. Case (1) is easy: we just use it.
2336 * Case (2) means we look for a clone--if we have one, use it
2337 * otherwise grab ownership of cv. Case (3) means we look for a
2338 * clone (for non-XSUBs) and have to create one if we don't
2339 * already have one.
2340 * Why look for a clone in case (2) when we could just grab
2341 * ownership of cv straight away? Well, we could be recursing,
2342 * i.e. we originally tried to enter cv while another thread
2343 * owned it (hence we used a clone) but it has been freed up
2344 * and we're now recursing into it. It may or may not be "better"
2345 * to use the clone but at least CvDEPTH can be trusted.
2346 */
2347 if (CvOWNER(cv) == thr || CvXSUB(cv))
2348 MUTEX_UNLOCK(CvMUTEXP(cv));
11343788 2349 else {
3de9ffa1
MB
2350 /* Case (2) or (3) */
2351 SV **svp;
2352
11343788 2353 /*
3de9ffa1
MB
2354 * XXX Might it be better to release CvMUTEXP(cv) while we
2355 * do the hv_fetch? We might find someone has pinched it
2356 * when we look again, in which case we would be in case
2357 * (3) instead of (2) so we'd have to clone. Would the fact
2358 * that we released the mutex more quickly make up for this?
2359 */
b099ddc0 2360 if ((svp = hv_fetch(thr->cvcache, (char *)cv, sizeof(cv), FALSE)))
6ee623d5 2361 {
3de9ffa1 2362 /* We already have a clone to use */
11343788 2363 MUTEX_UNLOCK(CvMUTEXP(cv));
3de9ffa1 2364 cv = *(CV**)svp;
bf49b057 2365 DEBUG_S(PerlIO_printf(Perl_debug_log,
1fd28e87
MB
2366 "entersub: %p already has clone %p:%s\n",
2367 thr, cv, SvPEEK((SV*)cv)));
3de9ffa1
MB
2368 CvOWNER(cv) = thr;
2369 SvREFCNT_inc(cv);
2370 if (CvDEPTH(cv) == 0)
c76ac1ee 2371 SAVEDESTRUCTOR_X(unset_cvowner, (void*) cv);
3de9ffa1 2372 }
11343788 2373 else {
3de9ffa1
MB
2374 /* (2) => grab ownership of cv. (3) => make clone */
2375 if (!CvOWNER(cv)) {
2376 CvOWNER(cv) = thr;
2377 SvREFCNT_inc(cv);
11343788 2378 MUTEX_UNLOCK(CvMUTEXP(cv));
bf49b057 2379 DEBUG_S(PerlIO_printf(Perl_debug_log,
1fd28e87
MB
2380 "entersub: %p grabbing %p:%s in stash %s\n",
2381 thr, cv, SvPEEK((SV*)cv), CvSTASH(cv) ?
3de9ffa1 2382 HvNAME(CvSTASH(cv)) : "(none)"));
cd06dffe
GS
2383 }
2384 else {
3de9ffa1
MB
2385 /* Make a new clone. */
2386 CV *clonecv;
2387 SvREFCNT_inc(cv); /* don't let it vanish from under us */
2388 MUTEX_UNLOCK(CvMUTEXP(cv));
bf49b057 2389 DEBUG_S((PerlIO_printf(Perl_debug_log,
1fd28e87
MB
2390 "entersub: %p cloning %p:%s\n",
2391 thr, cv, SvPEEK((SV*)cv))));
3de9ffa1
MB
2392 /*
2393 * We're creating a new clone so there's no race
2394 * between the original MUTEX_UNLOCK and the
2395 * SvREFCNT_inc since no one will be trying to undef
2396 * it out from underneath us. At least, I don't think
2397 * there's a race...
2398 */
2399 clonecv = cv_clone(cv);
2400 SvREFCNT_dec(cv); /* finished with this */
199100c8 2401 hv_store(thr->cvcache, (char*)cv, sizeof(cv), (SV*)clonecv,0);
3de9ffa1
MB
2402 CvOWNER(clonecv) = thr;
2403 cv = clonecv;
11343788 2404 SvREFCNT_inc(cv);
11343788 2405 }
8b73bbec 2406 DEBUG_S(if (CvDEPTH(cv) != 0)
bf49b057 2407 PerlIO_printf(Perl_debug_log, "depth %ld != 0\n",
3de9ffa1 2408 CvDEPTH(cv)););
c76ac1ee 2409 SAVEDESTRUCTOR_X(unset_cvowner, (void*) cv);
11343788 2410 }
3de9ffa1 2411 }
11343788
MB
2412#endif /* USE_THREADS */
2413
a0d0e21e 2414 if (CvXSUB(cv)) {
67caa1fe 2415#ifdef PERL_XSUB_OLDSTYLE
a0d0e21e 2416 if (CvOLDSTYLE(cv)) {
20ce7b12 2417 I32 (*fp3)(int,int,int);
a0d0e21e
LW
2418 dMARK;
2419 register I32 items = SP - MARK;
67955e0c 2420 /* We dont worry to copy from @_. */
924508f0
GS
2421 while (SP > mark) {
2422 SP[1] = SP[0];
2423 SP--;
a0d0e21e 2424 }
3280af22 2425 PL_stack_sp = mark + 1;
1d7c1841 2426 fp3 = (I32(*)(int,int,int))CvXSUB(cv);
1c846c1f 2427 items = (*fp3)(CvXSUBANY(cv).any_i32,
3280af22 2428 MARK - PL_stack_base + 1,
ecfc5424 2429 items);
3280af22 2430 PL_stack_sp = PL_stack_base + items;
a0d0e21e 2431 }
67caa1fe
GS
2432 else
2433#endif /* PERL_XSUB_OLDSTYLE */
2434 {
748a9306
LW
2435 I32 markix = TOPMARK;
2436
a0d0e21e 2437 PUTBACK;
67955e0c
PP
2438
2439 if (!hasargs) {
2440 /* Need to copy @_ to stack. Alternative may be to
2441 * switch stack to @_, and copy return values
2442 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
6d4ff0d2
MB
2443 AV* av;
2444 I32 items;
2445#ifdef USE_THREADS
533c011a 2446 av = (AV*)PL_curpad[0];
6d4ff0d2 2447#else
3280af22 2448 av = GvAV(PL_defgv);
6d4ff0d2 2449#endif /* USE_THREADS */
93965878 2450 items = AvFILLp(av) + 1; /* @_ is not tieable */
67955e0c
PP
2451
2452 if (items) {
2453 /* Mark is at the end of the stack. */
924508f0
GS
2454 EXTEND(SP, items);
2455 Copy(AvARRAY(av), SP + 1, items, SV*);
2456 SP += items;
1c846c1f 2457 PUTBACK ;
67955e0c
PP
2458 }
2459 }
67caa1fe
GS
2460 /* We assume first XSUB in &DB::sub is the called one. */
2461 if (PL_curcopdb) {
1d7c1841 2462 SAVEVPTR(PL_curcop);
3280af22
NIS
2463 PL_curcop = PL_curcopdb;
2464 PL_curcopdb = NULL;
67955e0c
PP
2465 }
2466 /* Do we need to open block here? XXXX */
0cb96387 2467 (void)(*CvXSUB(cv))(aTHXo_ cv);
748a9306
LW
2468
2469 /* Enforce some sanity in scalar context. */
3280af22
NIS
2470 if (gimme == G_SCALAR && ++markix != PL_stack_sp - PL_stack_base ) {
2471 if (markix > PL_stack_sp - PL_stack_base)
2472 *(PL_stack_base + markix) = &PL_sv_undef;
748a9306 2473 else
3280af22
NIS
2474 *(PL_stack_base + markix) = *PL_stack_sp;
2475 PL_stack_sp = PL_stack_base + markix;
748a9306 2476 }
a0d0e21e
LW
2477 }
2478 LEAVE;
2479 return NORMAL;
2480 }
2481 else {
2482 dMARK;
2483 register I32 items = SP - MARK;
a0d0e21e
LW
2484 AV* padlist = CvPADLIST(cv);
2485 SV** svp = AvARRAY(padlist);
533c011a 2486 push_return(PL_op->op_next);
a0d0e21e
LW
2487 PUSHBLOCK(cx, CXt_SUB, MARK);
2488 PUSHSUB(cx);
2489 CvDEPTH(cv)++;
6b35e009
GS
2490 /* XXX This would be a natural place to set C<PL_compcv = cv> so
2491 * that eval'' ops within this sub know the correct lexical space.
2492 * Owing the speed considerations, we choose to search for the cv
2493 * in doeval() instead.
2494 */
a0d0e21e
LW
2495 if (CvDEPTH(cv) < 2)
2496 (void)SvREFCNT_inc(cv);
2497 else { /* save temporaries on recursion? */
1d7c1841 2498 PERL_STACK_OVERFLOW_CHECK();
93965878 2499 if (CvDEPTH(cv) > AvFILLp(padlist)) {
a0d0e21e
LW
2500 AV *av;
2501 AV *newpad = newAV();
4aa0a1f7 2502 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
93965878 2503 I32 ix = AvFILLp((AV*)svp[1]);
1d7c1841 2504 I32 names_fill = AvFILLp((AV*)svp[0]);
a0d0e21e 2505 svp = AvARRAY(svp[0]);
748a9306 2506 for ( ;ix > 0; ix--) {
1d7c1841 2507 if (names_fill >= ix && svp[ix] != &PL_sv_undef) {
748a9306 2508 char *name = SvPVX(svp[ix]);
5f05dabc
PP
2509 if ((SvFLAGS(svp[ix]) & SVf_FAKE) /* outer lexical? */
2510 || *name == '&') /* anonymous code? */
2511 {
2512 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix]));
748a9306
LW
2513 }
2514 else { /* our own lexical */
2515 if (*name == '@')
2516 av_store(newpad, ix, sv = (SV*)newAV());
2517 else if (*name == '%')
2518 av_store(newpad, ix, sv = (SV*)newHV());
2519 else
2520 av_store(newpad, ix, sv = NEWSV(0,0));
2521 SvPADMY_on(sv);
2522 }
a0d0e21e 2523 }
1d7c1841
GS
2524 else if (IS_PADGV(oldpad[ix]) || IS_PADCONST(oldpad[ix])) {
2525 av_store(newpad, ix, sv = SvREFCNT_inc(oldpad[ix]));
2526 }
a0d0e21e 2527 else {
748a9306 2528 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
2529 SvPADTMP_on(sv);
2530 }
2531 }
2532 av = newAV(); /* will be @_ */
2533 av_extend(av, 0);
2534 av_store(newpad, 0, (SV*)av);
2535 AvFLAGS(av) = AVf_REIFY;
2536 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
93965878 2537 AvFILLp(padlist) = CvDEPTH(cv);
a0d0e21e
LW
2538 svp = AvARRAY(padlist);
2539 }
2540 }
6d4ff0d2
MB
2541#ifdef USE_THREADS
2542 if (!hasargs) {
533c011a 2543 AV* av = (AV*)PL_curpad[0];
6d4ff0d2 2544
93965878 2545 items = AvFILLp(av) + 1;
6d4ff0d2
MB
2546 if (items) {
2547 /* Mark is at the end of the stack. */
924508f0
GS
2548 EXTEND(SP, items);
2549 Copy(AvARRAY(av), SP + 1, items, SV*);
2550 SP += items;
1c846c1f 2551 PUTBACK ;
6d4ff0d2
MB
2552 }
2553 }
2554#endif /* USE_THREADS */
1d7c1841 2555 SAVEVPTR(PL_curpad);
3280af22 2556 PL_curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
6d4ff0d2
MB
2557#ifndef USE_THREADS
2558 if (hasargs)
2559#endif /* USE_THREADS */
2560 {
2561 AV* av;
a0d0e21e
LW
2562 SV** ary;
2563
77a005ab 2564#if 0
bf49b057 2565 DEBUG_S(PerlIO_printf(Perl_debug_log,
0f15f207 2566 "%p entersub preparing @_\n", thr));
77a005ab 2567#endif
3280af22 2568 av = (AV*)PL_curpad[0];
221373f0
GS
2569 if (AvREAL(av)) {
2570 /* @_ is normally not REAL--this should only ever
2571 * happen when DB::sub() calls things that modify @_ */
2572 av_clear(av);
2573 AvREAL_off(av);
2574 AvREIFY_on(av);
2575 }
6d4ff0d2 2576#ifndef USE_THREADS
3280af22
NIS
2577 cx->blk_sub.savearray = GvAV(PL_defgv);
2578 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
6d4ff0d2 2579#endif /* USE_THREADS */
7032098e 2580 cx->blk_sub.oldcurpad = PL_curpad;
6d4ff0d2 2581 cx->blk_sub.argarray = av;
a0d0e21e
LW
2582 ++MARK;
2583
2584 if (items > AvMAX(av) + 1) {
2585 ary = AvALLOC(av);
2586 if (AvARRAY(av) != ary) {
2587 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2588 SvPVX(av) = (char*)ary;
2589 }
2590 if (items > AvMAX(av) + 1) {
2591 AvMAX(av) = items - 1;
2592 Renew(ary,items,SV*);
2593 AvALLOC(av) = ary;
2594 SvPVX(av) = (char*)ary;
2595 }
2596 }
2597 Copy(MARK,AvARRAY(av),items,SV*);
93965878 2598 AvFILLp(av) = items - 1;
1c846c1f 2599
a0d0e21e
LW
2600 while (items--) {
2601 if (*MARK)
2602 SvTEMP_off(*MARK);
2603 MARK++;
2604 }
2605 }
4a925ff6
GS
2606 /* warning must come *after* we fully set up the context
2607 * stuff so that __WARN__ handlers can safely dounwind()
2608 * if they want to
2609 */
2610 if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION)
2611 && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))
2612 sub_crush_depth(cv);
77a005ab 2613#if 0
bf49b057 2614 DEBUG_S(PerlIO_printf(Perl_debug_log,
0f15f207 2615 "%p entersub returning %p\n", thr, CvSTART(cv)));
77a005ab 2616#endif
a0d0e21e
LW
2617 RETURNOP(CvSTART(cv));
2618 }
2619}
2620
44a8e56a 2621void
864dbfa3 2622Perl_sub_crush_depth(pTHX_ CV *cv)
44a8e56a
PP
2623{
2624 if (CvANON(cv))
cea2e8a9 2625 Perl_warner(aTHX_ WARN_RECURSION, "Deep recursion on anonymous subroutine");
44a8e56a
PP
2626 else {
2627 SV* tmpstr = sv_newmortal();
2628 gv_efullname3(tmpstr, CvGV(cv), Nullch);
1c846c1f 2629 Perl_warner(aTHX_ WARN_RECURSION, "Deep recursion on subroutine \"%s\"",
599cee73 2630 SvPVX(tmpstr));
44a8e56a
PP
2631 }
2632}
2633
a0d0e21e
LW
2634PP(pp_aelem)
2635{
4e35701f 2636 djSP;
a0d0e21e 2637 SV** svp;
467f0320 2638 IV elem = POPi;
68dc0745 2639 AV* av = (AV*)POPs;
533c011a
NIS
2640 U32 lval = PL_op->op_flags & OPf_MOD;
2641 U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > AvFILL(av));
be6c24e0 2642 SV *sv;
a0d0e21e 2643
748a9306 2644 if (elem > 0)
3280af22 2645 elem -= PL_curcop->cop_arybase;
a0d0e21e
LW
2646 if (SvTYPE(av) != SVt_PVAV)
2647 RETPUSHUNDEF;
68dc0745 2648 svp = av_fetch(av, elem, lval && !defer);
a0d0e21e 2649 if (lval) {
3280af22 2650 if (!svp || *svp == &PL_sv_undef) {
68dc0745
PP
2651 SV* lv;
2652 if (!defer)
cea2e8a9 2653 DIE(aTHX_ PL_no_aelem, elem);
68dc0745
PP
2654 lv = sv_newmortal();
2655 sv_upgrade(lv, SVt_PVLV);
2656 LvTYPE(lv) = 'y';
2657 sv_magic(lv, Nullsv, 'y', Nullch, 0);
2658 LvTARG(lv) = SvREFCNT_inc(av);
2659 LvTARGOFF(lv) = elem;
2660 LvTARGLEN(lv) = 1;
2661 PUSHs(lv);
2662 RETURN;
2663 }
533c011a 2664 if (PL_op->op_private & OPpLVAL_INTRO)
161b7d16 2665 save_aelem(av, elem, svp);
533c011a
NIS
2666 else if (PL_op->op_private & OPpDEREF)
2667 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
a0d0e21e 2668 }
3280af22 2669 sv = (svp ? *svp : &PL_sv_undef);
be6c24e0
GS
2670 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
2671 sv = sv_mortalcopy(sv);
2672 PUSHs(sv);
a0d0e21e
LW
2673 RETURN;
2674}
2675
02a9e968 2676void
864dbfa3 2677Perl_vivify_ref(pTHX_ SV *sv, U32 to_what)
02a9e968
CS
2678{
2679 if (SvGMAGICAL(sv))
2680 mg_get(sv);
2681 if (!SvOK(sv)) {
2682 if (SvREADONLY(sv))
cea2e8a9 2683 Perl_croak(aTHX_ PL_no_modify);
5f05dabc
PP
2684 if (SvTYPE(sv) < SVt_RV)
2685 sv_upgrade(sv, SVt_RV);
2686 else if (SvTYPE(sv) >= SVt_PV) {
2687 (void)SvOOK_off(sv);
2688 Safefree(SvPVX(sv));
2689 SvLEN(sv) = SvCUR(sv) = 0;
2690 }
68dc0745 2691 switch (to_what) {
5f05dabc 2692 case OPpDEREF_SV:
8c52afec 2693 SvRV(sv) = NEWSV(355,0);
5f05dabc
PP
2694 break;
2695 case OPpDEREF_AV:
2696 SvRV(sv) = (SV*)newAV();
2697 break;
2698 case OPpDEREF_HV:
2699 SvRV(sv) = (SV*)newHV();
2700 break;
2701 }
02a9e968
CS
2702 SvROK_on(sv);
2703 SvSETMAGIC(sv);
2704 }
2705}
2706
a0d0e21e
LW
2707PP(pp_method)
2708{
4e35701f 2709 djSP;
f5d5a27c
CS
2710 SV* sv = TOPs;
2711
2712 if (SvROK(sv)) {
eda383f2 2713 SV* rsv = SvRV(sv);
f5d5a27c
CS
2714 if (SvTYPE(rsv) == SVt_PVCV) {
2715 SETs(rsv);
2716 RETURN;
2717 }
2718 }
2719
2720 SETs(method_common(sv, Null(U32*)));
2721 RETURN;
2722}
2723
2724PP(pp_method_named)
2725{
2726 djSP;
2727 SV* sv = cSVOP->op_sv;
2728 U32 hash = SvUVX(sv);
2729
2730 XPUSHs(method_common(sv, &hash));
2731 RETURN;
2732}
2733
2734STATIC SV *
2735S_method_common(pTHX_ SV* meth, U32* hashp)
2736{
a0d0e21e
LW
2737 SV* sv;
2738 SV* ob;
2739 GV* gv;
56304f61
CS
2740 HV* stash;
2741 char* name;
f5d5a27c 2742 STRLEN namelen;
ac91690f
CS
2743 char* packname;
2744 STRLEN packlen;
a0d0e21e 2745
f5d5a27c 2746 name = SvPV(meth, namelen);
3280af22 2747 sv = *(PL_stack_base + TOPMARK + 1);
f5d5a27c 2748
4f1b7578
SC
2749 if (!sv)
2750 Perl_croak(aTHX_ "Can't call method \"%s\" on an undefined value", name);
2751
16d20bd9
AD
2752 if (SvGMAGICAL(sv))
2753 mg_get(sv);
a0d0e21e 2754 if (SvROK(sv))
16d20bd9 2755 ob = (SV*)SvRV(sv);
a0d0e21e
LW
2756 else {
2757 GV* iogv;
a0d0e21e 2758
56304f61 2759 packname = Nullch;
a0d0e21e 2760 if (!SvOK(sv) ||
56304f61 2761 !(packname = SvPV(sv, packlen)) ||
a0d0e21e
LW
2762 !(iogv = gv_fetchpv(packname, FALSE, SVt_PVIO)) ||
2763 !(ob=(SV*)GvIO(iogv)))
2764 {
1c846c1f 2765 if (!packname ||
7e2040f0 2766 ((*(U8*)packname >= 0xc0 && DO_UTF8(sv))
b86a2fa7 2767 ? !isIDFIRST_utf8((U8*)packname)
834a4ddd
LW
2768 : !isIDFIRST(*packname)
2769 ))
2770 {
f5d5a27c
CS
2771 Perl_croak(aTHX_ "Can't call method \"%s\" %s", name,
2772 SvOK(sv) ? "without a package or object reference"
2773 : "on an undefined value");
834a4ddd 2774 }
56304f61 2775 stash = gv_stashpvn(packname, packlen, TRUE);
ac91690f 2776 goto fetch;
a0d0e21e 2777 }
3280af22 2778 *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
a0d0e21e
LW
2779 }
2780
f0d43078
GS
2781 if (!ob || !(SvOBJECT(ob)
2782 || (SvTYPE(ob) == SVt_PVGV && (ob = (SV*)GvIO((GV*)ob))
2783 && SvOBJECT(ob))))
2784 {
f5d5a27c
CS
2785 Perl_croak(aTHX_ "Can't call method \"%s\" on unblessed reference",
2786 name);
f0d43078 2787 }
a0d0e21e 2788
56304f61 2789 stash = SvSTASH(ob);
a0d0e21e 2790
ac91690f 2791 fetch:
f5d5a27c
CS
2792 /* shortcut for simple names */
2793 if (hashp) {
2794 HE* he = hv_fetch_ent(stash, meth, 0, *hashp);
2795 if (he) {
2796 gv = (GV*)HeVAL(he);
2797 if (isGV(gv) && GvCV(gv) &&
2798 (!GvCVGEN(gv) || GvCVGEN(gv) == PL_sub_generation))
2799 return (SV*)GvCV(gv);
2800 }
2801 }
2802
ac91690f 2803 gv = gv_fetchmethod(stash, name);
56304f61
CS
2804 if (!gv) {
2805 char* leaf = name;
2806 char* sep = Nullch;
2807 char* p;
c1899e02 2808 GV* gv;
56304f61
CS
2809
2810 for (p = name; *p; p++) {
2811 if (*p == '\'')
2812 sep = p, leaf = p + 1;
2813 else if (*p == ':' && *(p + 1) == ':')
2814 sep = p, leaf = p + 2;
2815 }
2816 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
1d7c1841 2817 packname = sep ? CopSTASHPV(PL_curcop) : HvNAME(stash);
56304f61
CS
2818 packlen = strlen(packname);
2819 }
2820 else {
2821 packname = name;
2822 packlen = sep - name;
2823 }
c1899e02
GS
2824 gv = gv_fetchpv(packname, 0, SVt_PVHV);
2825 if (gv && isGV(gv)) {
2826 Perl_croak(aTHX_
2827 "Can't locate object method \"%s\" via package \"%s\"",
2828 leaf, packname);
2829 }
2830 else {
2831 Perl_croak(aTHX_
f6e565ef 2832 "Can't locate object method \"%s\" via package \"%s\""
c1899e02
GS
2833 " (perhaps you forgot to load \"%s\"?)",
2834 leaf, packname, packname);
2835 }
56304f61 2836 }
f5d5a27c 2837 return isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv;
a0d0e21e 2838}
22239a37 2839
51371543
GS
2840#ifdef USE_THREADS
2841static void
2842unset_cvowner(pTHXo_ void *cvarg)
2843{
2844 register CV* cv = (CV *) cvarg;
51371543 2845
bf49b057 2846 DEBUG_S((PerlIO_printf(Perl_debug_log, "%p unsetting CvOWNER of %p:%s\n",
51371543
GS
2847 thr, cv, SvPEEK((SV*)cv))));
2848 MUTEX_LOCK(CvMUTEXP(cv));
2849 DEBUG_S(if (CvDEPTH(cv) != 0)
bf49b057 2850 PerlIO_printf(Perl_debug_log, "depth %ld != 0\n",
51371543
GS
2851 CvDEPTH(cv)););
2852 assert(thr == CvOWNER(cv));
2853 CvOWNER(cv) = 0;
2854 MUTEX_UNLOCK(CvMUTEXP(cv));
2855 SvREFCNT_dec(cv);
2856}
2857#endif /* USE_THREADS */