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