This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[ID 20000904.008] Tiny fix for perldiag
[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 255 if (SvTYPE(PL_last_in_gv) != SVt_PVGV) {
1c846c1f 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{
1c846c1f 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{
1c846c1f 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) {
1c846c1f 377 /* If using default handle then we need to make space to
a60c0954
NIS
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 497 GV *gv;
1c846c1f 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? */
1c846c1f 554 EXTEND(SP, maxarg);
93965878 555 if (SvRMAGICAL(av)) {
1c846c1f 556 U32 i;
93965878
NIS
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 560 }
1c846c1f 561 }
93965878
NIS
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 601 GV *gv;
1c846c1f 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))
1c846c1f 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;
1c846c1f 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;
1c846c1f 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)
1c846c1f 1072 && !PL_sawampersand
f722798b
IZ
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;
1c846c1f 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;
1c846c1f 1544 U32 hash = (SvFAKE(keysv) && SvREADONLY(keysv)) ? SvUVX(keysv) : 0;
a0d0e21e 1545
ae77835f 1546 if (SvTYPE(hv) == SVt_PVHV) {
1c846c1f 1547 he = hv_fetch_ent(hv, keysv, lval && !defer, hash);
97fcbf96 1548 svp = he ? &HeVAL(he) : 0;
ae77835f
MB
1549 }
1550 else if (SvTYPE(hv) == SVt_PVAV) {
0ebe0038 1551 if (PL_op->op_private & OPpLVAL_INTRO)
cea2e8a9 1552 DIE(aTHX_ "Can't localize pseudo-hash element");
1c846c1f 1553 svp = avhv_fetch_ent((AV*)hv, keysv, lval && !defer, hash);
ae77835f 1554 }
c750a3ec 1555 else {
a0d0e21e 1556 RETPUSHUNDEF;
c750a3ec 1557 }
a0d0e21e 1558 if (lval) {
3280af22 1559 if (!svp || *svp == &PL_sv_undef) {
68dc0745
PP
1560 SV* lv;
1561 SV* key2;
2d8e6c8d
GS
1562 if (!defer) {
1563 STRLEN n_a;
cea2e8a9 1564 DIE(aTHX_ PL_no_helem, SvPV(keysv, n_a));
2d8e6c8d 1565 }
68dc0745
PP
1566 lv = sv_newmortal();
1567 sv_upgrade(lv, SVt_PVLV);
1568 LvTYPE(lv) = 'y';
1569 sv_magic(lv, key2 = newSVsv(keysv), 'y', Nullch, 0);
1570 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1571 LvTARG(lv) = SvREFCNT_inc(hv);
1572 LvTARGLEN(lv) = 1;
1573 PUSHs(lv);
1574 RETURN;
1575 }
533c011a 1576 if (PL_op->op_private & OPpLVAL_INTRO) {
ae77835f 1577 if (HvNAME(hv) && isGV(*svp))
533c011a 1578 save_gp((GV*)*svp, !(PL_op->op_flags & OPf_SPECIAL));
5f05dabc 1579 else
161b7d16 1580 save_helem(hv, keysv, svp);
5f05dabc 1581 }
533c011a
NIS
1582 else if (PL_op->op_private & OPpDEREF)
1583 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
a0d0e21e 1584 }
3280af22 1585 sv = (svp ? *svp : &PL_sv_undef);
be6c24e0
GS
1586 /* This makes C<local $tied{foo} = $tied{foo}> possible.
1587 * Pushing the magical RHS on to the stack is useless, since
1588 * that magic is soon destined to be misled by the local(),
1589 * and thus the later pp_sassign() will fail to mg_get() the
1590 * old value. This should also cure problems with delayed
1591 * mg_get()s. GSAR 98-07-03 */
1592 if (!lval && SvGMAGICAL(sv))
1593 sv = sv_mortalcopy(sv);
1594 PUSHs(sv);
a0d0e21e
LW
1595 RETURN;
1596}
1597
1598PP(pp_leave)
1599{
4e35701f 1600 djSP;
c09156bb 1601 register PERL_CONTEXT *cx;
a0d0e21e
LW
1602 register SV **mark;
1603 SV **newsp;
1604 PMOP *newpm;
1605 I32 gimme;
1606
533c011a 1607 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e 1608 cx = &cxstack[cxstack_ix];
3280af22 1609 cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */
a0d0e21e
LW
1610 }
1611
1612 POPBLOCK(cx,newpm);
1613
533c011a 1614 gimme = OP_GIMME(PL_op, -1);
54310121
PP
1615 if (gimme == -1) {
1616 if (cxstack_ix >= 0)
1617 gimme = cxstack[cxstack_ix].blk_gimme;
1618 else
1619 gimme = G_SCALAR;
1620 }
a0d0e21e 1621
a1f49e72 1622 TAINT_NOT;
54310121
PP
1623 if (gimme == G_VOID)
1624 SP = newsp;
1625 else if (gimme == G_SCALAR) {
1626 MARK = newsp + 1;
1627 if (MARK <= SP)
1628 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1629 *MARK = TOPs;
1630 else
1631 *MARK = sv_mortalcopy(TOPs);
a0d0e21e 1632 else {
54310121 1633 MEXTEND(mark,0);
3280af22 1634 *MARK = &PL_sv_undef;
a0d0e21e 1635 }
54310121 1636 SP = MARK;
a0d0e21e 1637 }
54310121 1638 else if (gimme == G_ARRAY) {
a1f49e72
CS
1639 /* in case LEAVE wipes old return values */
1640 for (mark = newsp + 1; mark <= SP; mark++) {
1641 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
a0d0e21e 1642 *mark = sv_mortalcopy(*mark);
a1f49e72
CS
1643 TAINT_NOT; /* Each item is independent */
1644 }
1645 }
a0d0e21e 1646 }
3280af22 1647 PL_curpm = newpm; /* Don't pop $1 et al till now */
a0d0e21e
LW
1648
1649 LEAVE;
1650
1651 RETURN;
1652}
1653
1654PP(pp_iter)
1655{
4e35701f 1656 djSP;
c09156bb 1657 register PERL_CONTEXT *cx;
5f05dabc 1658 SV* sv;
4633a7c4 1659 AV* av;
1d7c1841 1660 SV **itersvp;
a0d0e21e 1661
924508f0 1662 EXTEND(SP, 1);
a0d0e21e 1663 cx = &cxstack[cxstack_ix];
6b35e009 1664 if (CxTYPE(cx) != CXt_LOOP)
cea2e8a9 1665 DIE(aTHX_ "panic: pp_iter");
a0d0e21e 1666
1d7c1841 1667 itersvp = CxITERVAR(cx);
4633a7c4 1668 av = cx->blk_loop.iterary;
89ea2908
GA
1669 if (SvTYPE(av) != SVt_PVAV) {
1670 /* iterate ($min .. $max) */
1671 if (cx->blk_loop.iterlval) {
1672 /* string increment */
1673 register SV* cur = cx->blk_loop.iterlval;
1674 STRLEN maxlen;
1675 char *max = SvPV((SV*)av, maxlen);
1676 if (!SvNIOK(cur) && SvCUR(cur) <= maxlen) {
eaa5c2d6 1677#ifndef USE_THREADS /* don't risk potential race */
1d7c1841 1678 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
eaa5c2d6 1679 /* safe to reuse old SV */
1d7c1841 1680 sv_setsv(*itersvp, cur);
eaa5c2d6 1681 }
1c846c1f 1682 else
eaa5c2d6
GA
1683#endif
1684 {
1685 /* we need a fresh SV every time so that loop body sees a
1686 * completely new SV for closures/references to work as
1687 * they used to */
1d7c1841
GS
1688 SvREFCNT_dec(*itersvp);
1689 *itersvp = newSVsv(cur);
eaa5c2d6 1690 }
89ea2908
GA
1691 if (strEQ(SvPVX(cur), max))
1692 sv_setiv(cur, 0); /* terminate next time */
1693 else
1694 sv_inc(cur);
1695 RETPUSHYES;
1696 }
1697 RETPUSHNO;
1698 }
1699 /* integer increment */
1700 if (cx->blk_loop.iterix > cx->blk_loop.itermax)
1701 RETPUSHNO;
7f61b687 1702
eaa5c2d6 1703#ifndef USE_THREADS /* don't risk potential race */
1d7c1841 1704 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
eaa5c2d6 1705 /* safe to reuse old SV */
1d7c1841 1706 sv_setiv(*itersvp, cx->blk_loop.iterix++);
eaa5c2d6 1707 }
1c846c1f 1708 else
eaa5c2d6
GA
1709#endif
1710 {
1711 /* we need a fresh SV every time so that loop body sees a
1712 * completely new SV for closures/references to work as they
1713 * used to */
1d7c1841
GS
1714 SvREFCNT_dec(*itersvp);
1715 *itersvp = newSViv(cx->blk_loop.iterix++);
eaa5c2d6 1716 }
89ea2908
GA
1717 RETPUSHYES;
1718 }
1719
1720 /* iterate array */
3280af22 1721 if (cx->blk_loop.iterix >= (av == PL_curstack ? cx->blk_oldsp : AvFILL(av)))
4633a7c4 1722 RETPUSHNO;
a0d0e21e 1723
1d7c1841 1724 SvREFCNT_dec(*itersvp);
a0d0e21e 1725
155aba94 1726 if ((sv = SvMAGICAL(av)
1c846c1f 1727 ? *av_fetch(av, ++cx->blk_loop.iterix, FALSE)
155aba94 1728 : AvARRAY(av)[++cx->blk_loop.iterix]))
a0d0e21e 1729 SvTEMP_off(sv);
a0d0e21e 1730 else
3280af22
NIS
1731 sv = &PL_sv_undef;
1732 if (av != PL_curstack && SvIMMORTAL(sv)) {
5f05dabc 1733 SV *lv = cx->blk_loop.iterlval;
71be2cbc
PP
1734 if (lv && SvREFCNT(lv) > 1) {
1735 SvREFCNT_dec(lv);
1736 lv = Nullsv;
1737 }
5f05dabc
PP
1738 if (lv)
1739 SvREFCNT_dec(LvTARG(lv));
1740 else {
68dc0745 1741 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
5f05dabc 1742 sv_upgrade(lv, SVt_PVLV);
5f05dabc 1743 LvTYPE(lv) = 'y';
68dc0745 1744 sv_magic(lv, Nullsv, 'y', Nullch, 0);
5f05dabc
PP
1745 }
1746 LvTARG(lv) = SvREFCNT_inc(av);
1747 LvTARGOFF(lv) = cx->blk_loop.iterix;
42718184 1748 LvTARGLEN(lv) = (STRLEN)UV_MAX;
5f05dabc
PP
1749 sv = (SV*)lv;
1750 }
a0d0e21e 1751
1d7c1841 1752 *itersvp = SvREFCNT_inc(sv);
a0d0e21e
LW
1753 RETPUSHYES;
1754}
1755
1756PP(pp_subst)
1757{
4e35701f 1758 djSP; dTARG;
a0d0e21e
LW
1759 register PMOP *pm = cPMOP;
1760 PMOP *rpm = pm;
1761 register SV *dstr;
1762 register char *s;
1763 char *strend;
1764 register char *m;
1765 char *c;
1766 register char *d;
1767 STRLEN clen;
1768 I32 iters = 0;
1769 I32 maxiters;
1770 register I32 i;
1771 bool once;
71be2cbc 1772 bool rxtainted;
a0d0e21e 1773 char *orig;
22e551b9 1774 I32 r_flags;
d9f97599 1775 register REGEXP *rx = pm->op_pmregexp;
a0d0e21e
LW
1776 STRLEN len;
1777 int force_on_match = 0;
3280af22 1778 I32 oldsave = PL_savestack_ix;
a0d0e21e 1779
5cd24f17
PP
1780 /* known replacement string? */
1781 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
533c011a 1782 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
1783 TARG = POPs;
1784 else {
54b9620d 1785 TARG = DEFSV;
a0d0e21e 1786 EXTEND(SP,1);
1c846c1f 1787 }
68dc0745
PP
1788 if (SvREADONLY(TARG)
1789 || (SvTYPE(TARG) > SVt_PVLV
1790 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG))))
d470f89e 1791 DIE(aTHX_ PL_no_modify);
8ec5e241
NIS
1792 PUTBACK;
1793
a0d0e21e 1794 s = SvPV(TARG, len);
68dc0745 1795 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
a0d0e21e 1796 force_on_match = 1;
b3eb6a9b 1797 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
3280af22
NIS
1798 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
1799 if (PL_tainted)
b3eb6a9b 1800 rxtainted |= 2;
9212bbba 1801 TAINT_NOT;
a0d0e21e
LW
1802
1803 force_it:
1804 if (!pm || !s)
cea2e8a9 1805 DIE(aTHX_ "panic: do_subst");
a0d0e21e
LW
1806
1807 strend = s + len;
1c846c1f 1808 maxiters = 2*(strend - s) + 10; /* We can match twice at each
2beec16e
IZ
1809 position, once with zero-length,
1810 second time with non-zero. */
a0d0e21e 1811
3280af22
NIS
1812 if (!rx->prelen && PL_curpm) {
1813 pm = PL_curpm;
d9f97599 1814 rx = pm->op_pmregexp;
a0d0e21e 1815 }
22e551b9 1816 r_flags = (rx->nparens || SvTEMP(TARG) || PL_sawampersand)
9d080a66 1817 ? REXEC_COPY_STR : 0;
f722798b 1818 if (SvSCREAM(TARG))
22e551b9 1819 r_flags |= REXEC_SCREAM;
a0d0e21e 1820 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
3280af22
NIS
1821 SAVEINT(PL_multiline);
1822 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
a0d0e21e
LW
1823 }
1824 orig = m = s;
f722798b
IZ
1825 if (rx->reganch & RE_USE_INTUIT) {
1826 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, s, strend, r_flags, NULL);
1827
1828 if (!s)
1829 goto nope;
1830 /* How to do it in subst? */
1831/* if ( (rx->reganch & ROPT_CHECK_ALL)
1c846c1f 1832 && !PL_sawampersand
f722798b
IZ
1833 && ((rx->reganch & ROPT_NOSCAN)
1834 || !((rx->reganch & RE_INTUIT_TAIL)
1835 && (r_flags & REXEC_SCREAM))))
1836 goto yup;
1837*/
a0d0e21e 1838 }
71be2cbc
PP
1839
1840 /* only replace once? */
a0d0e21e 1841 once = !(rpm->op_pmflags & PMf_GLOBAL);
71be2cbc
PP
1842
1843 /* known replacement string? */
5cd24f17 1844 c = dstr ? SvPV(dstr, clen) : Nullch;
71be2cbc
PP
1845
1846 /* can do inplace substitution? */
22e551b9 1847 if (c && clen <= rx->minlen && (once || !(r_flags & REXEC_COPY_STR))
d9f97599 1848 && !(rx->reganch & ROPT_LOOKBEHIND_SEEN)) {
f722798b
IZ
1849 if (!CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
1850 r_flags | REXEC_CHECKED))
1851 {
8ec5e241 1852 SPAGAIN;
3280af22 1853 PUSHs(&PL_sv_no);
71be2cbc
PP
1854 LEAVE_SCOPE(oldsave);
1855 RETURN;
1856 }
1857 if (force_on_match) {
1858 force_on_match = 0;
1859 s = SvPV_force(TARG, len);
1860 goto force_it;
1861 }
71be2cbc 1862 d = s;
3280af22 1863 PL_curpm = pm;
71be2cbc
PP
1864 SvSCREAM_off(TARG); /* disable possible screamer */
1865 if (once) {
48c036b1 1866 rxtainted |= RX_MATCH_TAINTED(rx);
cf93c79d
IZ
1867 m = orig + rx->startp[0];
1868 d = orig + rx->endp[0];
71be2cbc
PP
1869 s = orig;
1870 if (m - s > strend - d) { /* faster to shorten from end */
1871 if (clen) {
1872 Copy(c, m, clen, char);
1873 m += clen;
a0d0e21e 1874 }
71be2cbc
PP
1875 i = strend - d;
1876 if (i > 0) {
1877 Move(d, m, i, char);
1878 m += i;
a0d0e21e 1879 }
71be2cbc
PP
1880 *m = '\0';
1881 SvCUR_set(TARG, m - s);
1882 }
1883 /*SUPPRESS 560*/
155aba94 1884 else if ((i = m - s)) { /* faster from front */
71be2cbc
PP
1885 d -= clen;
1886 m = d;
1887 sv_chop(TARG, d-i);
1888 s += i;
1889 while (i--)
1890 *--d = *--s;
1891 if (clen)
1892 Copy(c, m, clen, char);
1893 }
1894 else if (clen) {
1895 d -= clen;
1896 sv_chop(TARG, d);
1897 Copy(c, d, clen, char);
1898 }
1899 else {
1900 sv_chop(TARG, d);
1901 }
48c036b1 1902 TAINT_IF(rxtainted & 1);
8ec5e241 1903 SPAGAIN;
3280af22 1904 PUSHs(&PL_sv_yes);
71be2cbc
PP
1905 }
1906 else {
71be2cbc
PP
1907 do {
1908 if (iters++ > maxiters)
cea2e8a9 1909 DIE(aTHX_ "Substitution loop");
d9f97599 1910 rxtainted |= RX_MATCH_TAINTED(rx);
cf93c79d 1911 m = rx->startp[0] + orig;
71be2cbc 1912 /*SUPPRESS 560*/
155aba94 1913 if ((i = m - s)) {
71be2cbc
PP
1914 if (s != d)
1915 Move(s, d, i, char);
1916 d += i;
a0d0e21e 1917 }
71be2cbc
PP
1918 if (clen) {
1919 Copy(c, d, clen, char);
1920 d += clen;
1921 }
cf93c79d 1922 s = rx->endp[0] + orig;
cea2e8a9 1923 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
f722798b
IZ
1924 TARG, NULL,
1925 /* don't match same null twice */
1926 REXEC_NOT_FIRST|REXEC_IGNOREPOS));
71be2cbc
PP
1927 if (s != d) {
1928 i = strend - s;
1929 SvCUR_set(TARG, d - SvPVX(TARG) + i);
1930 Move(s, d, i+1, char); /* include the NUL */
a0d0e21e 1931 }
48c036b1 1932 TAINT_IF(rxtainted & 1);
8ec5e241 1933 SPAGAIN;
71be2cbc 1934 PUSHs(sv_2mortal(newSViv((I32)iters)));
a0d0e21e 1935 }
80b498e0 1936 (void)SvPOK_only_UTF8(TARG);
48c036b1 1937 TAINT_IF(rxtainted);
8ec5e241
NIS
1938 if (SvSMAGICAL(TARG)) {
1939 PUTBACK;
1940 mg_set(TARG);
1941 SPAGAIN;
1942 }
9212bbba 1943 SvTAINT(TARG);
71be2cbc
PP
1944 LEAVE_SCOPE(oldsave);
1945 RETURN;
a0d0e21e 1946 }
71be2cbc 1947
f722798b
IZ
1948 if (CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
1949 r_flags | REXEC_CHECKED))
1950 {
a0d0e21e
LW
1951 if (force_on_match) {
1952 force_on_match = 0;
1953 s = SvPV_force(TARG, len);
1954 goto force_it;
1955 }
48c036b1 1956 rxtainted |= RX_MATCH_TAINTED(rx);
8ec5e241 1957 dstr = NEWSV(25, len);
a0d0e21e 1958 sv_setpvn(dstr, m, s-m);
3280af22 1959 PL_curpm = pm;
a0d0e21e 1960 if (!c) {
c09156bb 1961 register PERL_CONTEXT *cx;
8ec5e241 1962 SPAGAIN;
a0d0e21e
LW
1963 PUSHSUBST(cx);
1964 RETURNOP(cPMOP->op_pmreplroot);
1965 }
cf93c79d 1966 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
a0d0e21e
LW
1967 do {
1968 if (iters++ > maxiters)
cea2e8a9 1969 DIE(aTHX_ "Substitution loop");
d9f97599 1970 rxtainted |= RX_MATCH_TAINTED(rx);
cf93c79d 1971 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
a0d0e21e
LW
1972 m = s;
1973 s = orig;
cf93c79d 1974 orig = rx->subbeg;
a0d0e21e
LW
1975 s = orig + (m - s);
1976 strend = s + (strend - m);
1977 }
cf93c79d 1978 m = rx->startp[0] + orig;
a0d0e21e 1979 sv_catpvn(dstr, s, m-s);
cf93c79d 1980 s = rx->endp[0] + orig;
a0d0e21e
LW
1981 if (clen)
1982 sv_catpvn(dstr, c, clen);
1983 if (once)
1984 break;
cea2e8a9 1985 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m, TARG, NULL, r_flags));
a0d0e21e 1986 sv_catpvn(dstr, s, strend - s);
748a9306 1987
4633a7c4 1988 (void)SvOOK_off(TARG);
cb0b1708 1989 Safefree(SvPVX(TARG));
748a9306
LW
1990 SvPVX(TARG) = SvPVX(dstr);
1991 SvCUR_set(TARG, SvCUR(dstr));
1992 SvLEN_set(TARG, SvLEN(dstr));
1993 SvPVX(dstr) = 0;
1994 sv_free(dstr);
1995
48c036b1 1996 TAINT_IF(rxtainted & 1);
f878fbec 1997 SPAGAIN;
48c036b1
GS
1998 PUSHs(sv_2mortal(newSViv((I32)iters)));
1999
a0d0e21e 2000 (void)SvPOK_only(TARG);
48c036b1 2001 TAINT_IF(rxtainted);
a0d0e21e 2002 SvSETMAGIC(TARG);
9212bbba 2003 SvTAINT(TARG);
4633a7c4 2004 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
2005 RETURN;
2006 }
5cd24f17 2007 goto ret_no;
a0d0e21e
LW
2008
2009nope:
1c846c1f 2010ret_no:
8ec5e241 2011 SPAGAIN;
3280af22 2012 PUSHs(&PL_sv_no);
4633a7c4 2013 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
2014 RETURN;
2015}
2016
2017PP(pp_grepwhile)
2018{
4e35701f 2019 djSP;
a0d0e21e
LW
2020
2021 if (SvTRUEx(POPs))
3280af22
NIS
2022 PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr];
2023 ++*PL_markstack_ptr;
a0d0e21e
LW
2024 LEAVE; /* exit inner scope */
2025
2026 /* All done yet? */
3280af22 2027 if (PL_stack_base + *PL_markstack_ptr > SP) {
a0d0e21e 2028 I32 items;
54310121 2029 I32 gimme = GIMME_V;
a0d0e21e
LW
2030
2031 LEAVE; /* exit outer scope */
2032 (void)POPMARK; /* pop src */
3280af22 2033 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
a0d0e21e 2034 (void)POPMARK; /* pop dst */
3280af22 2035 SP = PL_stack_base + POPMARK; /* pop original mark */
54310121 2036 if (gimme == G_SCALAR) {
a0d0e21e
LW
2037 dTARGET;
2038 XPUSHi(items);
a0d0e21e 2039 }
54310121
PP
2040 else if (gimme == G_ARRAY)
2041 SP += items;
a0d0e21e
LW
2042 RETURN;
2043 }
2044 else {
2045 SV *src;
2046
2047 ENTER; /* enter inner scope */
1d7c1841 2048 SAVEVPTR(PL_curpm);
a0d0e21e 2049
3280af22 2050 src = PL_stack_base[*PL_markstack_ptr];
a0d0e21e 2051 SvTEMP_off(src);
54b9620d 2052 DEFSV = src;
a0d0e21e
LW
2053
2054 RETURNOP(cLOGOP->op_other);
2055 }
2056}
2057
2058PP(pp_leavesub)
2059{
4e35701f 2060 djSP;
a0d0e21e
LW
2061 SV **mark;
2062 SV **newsp;
2063 PMOP *newpm;
2064 I32 gimme;
c09156bb 2065 register PERL_CONTEXT *cx;
b0d9ce38 2066 SV *sv;
a0d0e21e
LW
2067
2068 POPBLOCK(cx,newpm);
1c846c1f 2069
a1f49e72 2070 TAINT_NOT;
a0d0e21e
LW
2071 if (gimme == G_SCALAR) {
2072 MARK = newsp + 1;
a29cdaf0 2073 if (MARK <= SP) {
a8bba7fa 2074 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
a29cdaf0
IZ
2075 if (SvTEMP(TOPs)) {
2076 *MARK = SvREFCNT_inc(TOPs);
2077 FREETMPS;
2078 sv_2mortal(*MARK);
cd06dffe
GS
2079 }
2080 else {
959e3673 2081 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
a29cdaf0 2082 FREETMPS;
959e3673
GS
2083 *MARK = sv_mortalcopy(sv);
2084 SvREFCNT_dec(sv);
a29cdaf0 2085 }
cd06dffe
GS
2086 }
2087 else
a29cdaf0 2088 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
cd06dffe
GS
2089 }
2090 else {
f86702cc 2091 MEXTEND(MARK, 0);
3280af22 2092 *MARK = &PL_sv_undef;
a0d0e21e
LW
2093 }
2094 SP = MARK;
2095 }
54310121 2096 else if (gimme == G_ARRAY) {
f86702cc 2097 for (MARK = newsp + 1; MARK <= SP; MARK++) {
a1f49e72 2098 if (!SvTEMP(*MARK)) {
f86702cc 2099 *MARK = sv_mortalcopy(*MARK);
a1f49e72
CS
2100 TAINT_NOT; /* Each item is independent */
2101 }
f86702cc 2102 }
a0d0e21e 2103 }
f86702cc 2104 PUTBACK;
1c846c1f 2105
b0d9ce38 2106 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
3280af22 2107 PL_curpm = newpm; /* ... and pop $1 et al */
a0d0e21e
LW
2108
2109 LEAVE;
b0d9ce38 2110 LEAVESUB(sv);
a0d0e21e
LW
2111 return pop_return();
2112}
2113
cd06dffe
GS
2114/* This duplicates the above code because the above code must not
2115 * get any slower by more conditions */
2116PP(pp_leavesublv)
2117{
2118 djSP;
2119 SV **mark;
2120 SV **newsp;
2121 PMOP *newpm;
2122 I32 gimme;
2123 register PERL_CONTEXT *cx;
b0d9ce38 2124 SV *sv;
cd06dffe
GS
2125
2126 POPBLOCK(cx,newpm);
1c846c1f 2127
cd06dffe
GS
2128 TAINT_NOT;
2129
2130 if (cx->blk_sub.lval & OPpENTERSUB_INARGS) {
2131 /* We are an argument to a function or grep().
2132 * This kind of lvalueness was legal before lvalue
2133 * subroutines too, so be backward compatible:
2134 * cannot report errors. */
2135
2136 /* Scalar context *is* possible, on the LHS of -> only,
2137 * as in f()->meth(). But this is not an lvalue. */
2138 if (gimme == G_SCALAR)
2139 goto temporise;
2140 if (gimme == G_ARRAY) {
a8bba7fa 2141 if (!CvLVALUE(cx->blk_sub.cv))
cd06dffe
GS
2142 goto temporise_array;
2143 EXTEND_MORTAL(SP - newsp);
2144 for (mark = newsp + 1; mark <= SP; mark++) {
2145 if (SvTEMP(*mark))
2146 /* empty */ ;
2147 else if (SvFLAGS(*mark) & (SVs_PADTMP | SVf_READONLY))
2148 *mark = sv_mortalcopy(*mark);
2149 else {
2150 /* Can be a localized value subject to deletion. */
2151 PL_tmps_stack[++PL_tmps_ix] = *mark;
e1f15930 2152 (void)SvREFCNT_inc(*mark);
cd06dffe
GS
2153 }
2154 }
2155 }
2156 }
2157 else if (cx->blk_sub.lval) { /* Leave it as it is if we can. */
2158 /* Here we go for robustness, not for speed, so we change all
2159 * the refcounts so the caller gets a live guy. Cannot set
2160 * TEMP, so sv_2mortal is out of question. */
a8bba7fa 2161 if (!CvLVALUE(cx->blk_sub.cv)) {
b0d9ce38 2162 POPSUB(cx,sv);
d470f89e 2163 PL_curpm = newpm;
b0d9ce38
GS
2164 LEAVE;
2165 LEAVESUB(sv);
d470f89e
GS
2166 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
2167 }
cd06dffe
GS
2168 if (gimme == G_SCALAR) {
2169 MARK = newsp + 1;
2170 EXTEND_MORTAL(1);
2171 if (MARK == SP) {
d470f89e 2172 if (SvFLAGS(TOPs) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) {
b0d9ce38 2173 POPSUB(cx,sv);
d470f89e 2174 PL_curpm = newpm;
b0d9ce38
GS
2175 LEAVE;
2176 LEAVESUB(sv);
d470f89e 2177 DIE(aTHX_ "Can't return a %s from lvalue subroutine",
cd06dffe 2178 SvREADONLY(TOPs) ? "readonly value" : "temporary");
d470f89e 2179 }
cd06dffe
GS
2180 else { /* Can be a localized value
2181 * subject to deletion. */
2182 PL_tmps_stack[++PL_tmps_ix] = *mark;
e1f15930 2183 (void)SvREFCNT_inc(*mark);
cd06dffe
GS
2184 }
2185 }
d470f89e 2186 else { /* Should not happen? */
b0d9ce38 2187 POPSUB(cx,sv);
d470f89e 2188 PL_curpm = newpm;
b0d9ce38
GS
2189 LEAVE;
2190 LEAVESUB(sv);
d470f89e 2191 DIE(aTHX_ "%s returned from lvalue subroutine in scalar context",
cd06dffe 2192 (MARK > SP ? "Empty array" : "Array"));
d470f89e 2193 }
cd06dffe
GS
2194 SP = MARK;
2195 }
2196 else if (gimme == G_ARRAY) {
2197 EXTEND_MORTAL(SP - newsp);
2198 for (mark = newsp + 1; mark <= SP; mark++) {
d470f89e
GS
2199 if (SvFLAGS(*mark) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) {
2200 /* Might be flattened array after $#array = */
2201 PUTBACK;
b0d9ce38 2202 POPSUB(cx,sv);
d470f89e 2203 PL_curpm = newpm;
b0d9ce38
GS
2204 LEAVE;
2205 LEAVESUB(sv);
d470f89e 2206 DIE(aTHX_ "Can't return %s from lvalue subroutine",
cd06dffe
GS
2207 (*mark != &PL_sv_undef)
2208 ? (SvREADONLY(TOPs)
2209 ? "a readonly value" : "a temporary")
2210 : "an uninitialized value");
d470f89e 2211 }
cd06dffe 2212 else {
cd06dffe
GS
2213 /* Can be a localized value subject to deletion. */
2214 PL_tmps_stack[++PL_tmps_ix] = *mark;
e1f15930 2215 (void)SvREFCNT_inc(*mark);
cd06dffe
GS
2216 }
2217 }
2218 }
2219 }
2220 else {
2221 if (gimme == G_SCALAR) {
2222 temporise:
2223 MARK = newsp + 1;
2224 if (MARK <= SP) {
a8bba7fa 2225 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
cd06dffe
GS
2226 if (SvTEMP(TOPs)) {
2227 *MARK = SvREFCNT_inc(TOPs);
2228 FREETMPS;
2229 sv_2mortal(*MARK);
2230 }
2231 else {
959e3673 2232 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
cd06dffe 2233 FREETMPS;
959e3673
GS
2234 *MARK = sv_mortalcopy(sv);
2235 SvREFCNT_dec(sv);
cd06dffe
GS
2236 }
2237 }
2238 else
2239 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
2240 }
2241 else {
2242 MEXTEND(MARK, 0);
2243 *MARK = &PL_sv_undef;
2244 }
2245 SP = MARK;
2246 }
2247 else if (gimme == G_ARRAY) {
2248 temporise_array:
2249 for (MARK = newsp + 1; MARK <= SP; MARK++) {
2250 if (!SvTEMP(*MARK)) {
2251 *MARK = sv_mortalcopy(*MARK);
2252 TAINT_NOT; /* Each item is independent */
2253 }
2254 }
2255 }
2256 }
2257 PUTBACK;
1c846c1f 2258
b0d9ce38 2259 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
cd06dffe
GS
2260 PL_curpm = newpm; /* ... and pop $1 et al */
2261
2262 LEAVE;
b0d9ce38 2263 LEAVESUB(sv);
cd06dffe
GS
2264 return pop_return();
2265}
2266
2267
76e3520e 2268STATIC CV *
cea2e8a9 2269S_get_db_sub(pTHX_ SV **svp, CV *cv)
3de9ffa1
MB
2270{
2271 dTHR;
3280af22 2272 SV *dbsv = GvSV(PL_DBsub);
491527d0
GS
2273
2274 if (!PERLDB_SUB_NN) {
2275 GV *gv = CvGV(cv);
2276
2277 save_item(dbsv);
2278 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
1c846c1f 2279 || strEQ(GvNAME(gv), "END")
491527d0
GS
2280 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
2281 !( (SvTYPE(*svp) == SVt_PVGV) && (GvCV((GV*)*svp) == cv)
2282 && (gv = (GV*)*svp) ))) {
2283 /* Use GV from the stack as a fallback. */
2284 /* GV is potentially non-unique, or contain different CV. */
c2e66d9e
GS
2285 SV *tmp = newRV((SV*)cv);
2286 sv_setsv(dbsv, tmp);
2287 SvREFCNT_dec(tmp);
491527d0
GS
2288 }
2289 else {
2290 gv_efullname3(dbsv, gv, Nullch);
2291 }
3de9ffa1
MB
2292 }
2293 else {
155aba94
GS
2294 (void)SvUPGRADE(dbsv, SVt_PVIV);
2295 (void)SvIOK_on(dbsv);
491527d0 2296 SAVEIV(SvIVX(dbsv));
5bc28da9 2297 SvIVX(dbsv) = PTR2IV(cv); /* Do it the quickest way */
3de9ffa1 2298 }
491527d0 2299
3de9ffa1 2300 if (CvXSUB(cv))
3280af22
NIS
2301 PL_curcopdb = PL_curcop;
2302 cv = GvCV(PL_DBsub);
3de9ffa1
MB
2303 return cv;
2304}
2305
a0d0e21e
LW
2306PP(pp_entersub)
2307{
4e35701f 2308 djSP; dPOPss;
a0d0e21e
LW
2309 GV *gv;
2310 HV *stash;
2311 register CV *cv;
c09156bb 2312 register PERL_CONTEXT *cx;
5d94fbed 2313 I32 gimme;
533c011a 2314 bool hasargs = (PL_op->op_flags & OPf_STACKED) != 0;
a0d0e21e
LW
2315
2316 if (!sv)
cea2e8a9 2317 DIE(aTHX_ "Not a CODE reference");
a0d0e21e
LW
2318 switch (SvTYPE(sv)) {
2319 default:
2320 if (!SvROK(sv)) {
748a9306 2321 char *sym;
2d8e6c8d 2322 STRLEN n_a;
748a9306 2323
3280af22 2324 if (sv == &PL_sv_yes) { /* unfound import, ignore */
fb73857a 2325 if (hasargs)
3280af22 2326 SP = PL_stack_base + POPMARK;
a0d0e21e 2327 RETURN;
fb73857a 2328 }
15ff848f
CS
2329 if (SvGMAGICAL(sv)) {
2330 mg_get(sv);
2331 sym = SvPOKp(sv) ? SvPVX(sv) : Nullch;
2332 }
2333 else
2d8e6c8d 2334 sym = SvPV(sv, n_a);
15ff848f 2335 if (!sym)
cea2e8a9 2336 DIE(aTHX_ PL_no_usym, "a subroutine");
533c011a 2337 if (PL_op->op_private & HINT_STRICT_REFS)
cea2e8a9 2338 DIE(aTHX_ PL_no_symref, sym, "a subroutine");
864dbfa3 2339 cv = get_cv(sym, TRUE);
a0d0e21e
LW
2340 break;
2341 }
f5284f61
IZ
2342 {
2343 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
2344 tryAMAGICunDEREF(to_cv);
2345 }
a0d0e21e
LW
2346 cv = (CV*)SvRV(sv);
2347 if (SvTYPE(cv) == SVt_PVCV)
2348 break;
2349 /* FALL THROUGH */
2350 case SVt_PVHV:
2351 case SVt_PVAV:
cea2e8a9 2352 DIE(aTHX_ "Not a CODE reference");
a0d0e21e
LW
2353 case SVt_PVCV:
2354 cv = (CV*)sv;
2355 break;
2356 case SVt_PVGV:
8ebc5c01 2357 if (!(cv = GvCVu((GV*)sv)))
f6ec51f7
GS
2358 cv = sv_2cv(sv, &stash, &gv, FALSE);
2359 if (!cv) {
2360 ENTER;
2361 SAVETMPS;
2362 goto try_autoload;
2363 }
2364 break;
a0d0e21e
LW
2365 }
2366
2367 ENTER;
2368 SAVETMPS;
2369
2370 retry:
a0d0e21e 2371 if (!CvROOT(cv) && !CvXSUB(cv)) {
44a8e56a 2372 GV* autogv;
22239a37 2373 SV* sub_name;
44a8e56a
PP
2374
2375 /* anonymous or undef'd function leaves us no recourse */
2376 if (CvANON(cv) || !(gv = CvGV(cv)))
cea2e8a9 2377 DIE(aTHX_ "Undefined subroutine called");
67caa1fe 2378
44a8e56a
PP
2379 /* autoloaded stub? */
2380 if (cv != GvCV(gv)) {
2381 cv = GvCV(gv);
a0d0e21e 2382 }
44a8e56a 2383 /* should call AUTOLOAD now? */
67caa1fe 2384 else {
f6ec51f7
GS
2385try_autoload:
2386 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
2387 FALSE)))
2388 {
2389 cv = GvCV(autogv);
2390 }
2391 /* sorry */
2392 else {
2393 sub_name = sv_newmortal();
2394 gv_efullname3(sub_name, gv, Nullch);
cea2e8a9 2395 DIE(aTHX_ "Undefined subroutine &%s called", SvPVX(sub_name));
f6ec51f7 2396 }
67caa1fe
GS
2397 }
2398 if (!cv)
cea2e8a9 2399 DIE(aTHX_ "Not a CODE reference");
67caa1fe 2400 goto retry;
a0d0e21e
LW
2401 }
2402
54310121 2403 gimme = GIMME_V;
67caa1fe 2404 if ((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub) && !CvNODEBUG(cv)) {
4f01c5a5 2405 cv = get_db_sub(&sv, cv);
67caa1fe 2406 if (!cv)
cea2e8a9 2407 DIE(aTHX_ "No DBsub routine");
67caa1fe 2408 }
a0d0e21e 2409
11343788 2410#ifdef USE_THREADS
3de9ffa1
MB
2411 /*
2412 * First we need to check if the sub or method requires locking.
458fb581
MB
2413 * If so, we gain a lock on the CV, the first argument or the
2414 * stash (for static methods), as appropriate. This has to be
2415 * inline because for FAKE_THREADS, COND_WAIT inlines code to
2416 * reschedule by returning a new op.
3de9ffa1 2417 */
11343788 2418 MUTEX_LOCK(CvMUTEXP(cv));
77a005ab
MB
2419 if (CvFLAGS(cv) & CVf_LOCKED) {
2420 MAGIC *mg;
2421 if (CvFLAGS(cv) & CVf_METHOD) {
533c011a
NIS
2422 if (SP > PL_stack_base + TOPMARK)
2423 sv = *(PL_stack_base + TOPMARK + 1);
77a005ab 2424 else {
13e08037
GS
2425 AV *av = (AV*)PL_curpad[0];
2426 if (hasargs || !av || AvFILLp(av) < 0
2427 || !(sv = AvARRAY(av)[0]))
2428 {
2429 MUTEX_UNLOCK(CvMUTEXP(cv));
d470f89e 2430 DIE(aTHX_ "no argument for locked method call");
13e08037 2431 }
77a005ab
MB
2432 }
2433 if (SvROK(sv))
2434 sv = SvRV(sv);
458fb581
MB
2435 else {
2436 STRLEN len;
2437 char *stashname = SvPV(sv, len);
2438 sv = (SV*)gv_stashpvn(stashname, len, TRUE);
2439 }
77a005ab
MB
2440 }
2441 else {
2442 sv = (SV*)cv;
2443 }
2444 MUTEX_UNLOCK(CvMUTEXP(cv));
2445 mg = condpair_magic(sv);
2446 MUTEX_LOCK(MgMUTEXP(mg));
2447 if (MgOWNER(mg) == thr)
2448 MUTEX_UNLOCK(MgMUTEXP(mg));
2449 else {
2450 while (MgOWNER(mg))
2451 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
2452 MgOWNER(mg) = thr;
bf49b057 2453 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: pp_entersub lock %p\n",
1fd28e87 2454 thr, sv);)
77a005ab 2455 MUTEX_UNLOCK(MgMUTEXP(mg));
c76ac1ee 2456 SAVEDESTRUCTOR_X(Perl_unlock_condpair, sv);
11343788 2457 }
77a005ab 2458 MUTEX_LOCK(CvMUTEXP(cv));
11343788 2459 }
3de9ffa1
MB
2460 /*
2461 * Now we have permission to enter the sub, we must distinguish
2462 * four cases. (0) It's an XSUB (in which case we don't care
2463 * about ownership); (1) it's ours already (and we're recursing);
2464 * (2) it's free (but we may already be using a cached clone);
2465 * (3) another thread owns it. Case (1) is easy: we just use it.
2466 * Case (2) means we look for a clone--if we have one, use it
2467 * otherwise grab ownership of cv. Case (3) means we look for a
2468 * clone (for non-XSUBs) and have to create one if we don't
2469 * already have one.
2470 * Why look for a clone in case (2) when we could just grab
2471 * ownership of cv straight away? Well, we could be recursing,
2472 * i.e. we originally tried to enter cv while another thread
2473 * owned it (hence we used a clone) but it has been freed up
2474 * and we're now recursing into it. It may or may not be "better"
2475 * to use the clone but at least CvDEPTH can be trusted.
2476 */
2477 if (CvOWNER(cv) == thr || CvXSUB(cv))
2478 MUTEX_UNLOCK(CvMUTEXP(cv));
11343788 2479 else {
3de9ffa1
MB
2480 /* Case (2) or (3) */
2481 SV **svp;
2482
11343788 2483 /*
3de9ffa1
MB
2484 * XXX Might it be better to release CvMUTEXP(cv) while we
2485 * do the hv_fetch? We might find someone has pinched it
2486 * when we look again, in which case we would be in case
2487 * (3) instead of (2) so we'd have to clone. Would the fact
2488 * that we released the mutex more quickly make up for this?
2489 */
b099ddc0 2490 if ((svp = hv_fetch(thr->cvcache, (char *)cv, sizeof(cv), FALSE)))
6ee623d5 2491 {
3de9ffa1 2492 /* We already have a clone to use */
11343788 2493 MUTEX_UNLOCK(CvMUTEXP(cv));
3de9ffa1 2494 cv = *(CV**)svp;
bf49b057 2495 DEBUG_S(PerlIO_printf(Perl_debug_log,
1fd28e87
MB
2496 "entersub: %p already has clone %p:%s\n",
2497 thr, cv, SvPEEK((SV*)cv)));
3de9ffa1
MB
2498 CvOWNER(cv) = thr;
2499 SvREFCNT_inc(cv);
2500 if (CvDEPTH(cv) == 0)
c76ac1ee 2501 SAVEDESTRUCTOR_X(unset_cvowner, (void*) cv);
3de9ffa1 2502 }
11343788 2503 else {
3de9ffa1
MB
2504 /* (2) => grab ownership of cv. (3) => make clone */
2505 if (!CvOWNER(cv)) {
2506 CvOWNER(cv) = thr;
2507 SvREFCNT_inc(cv);
11343788 2508 MUTEX_UNLOCK(CvMUTEXP(cv));
bf49b057 2509 DEBUG_S(PerlIO_printf(Perl_debug_log,
1fd28e87
MB
2510 "entersub: %p grabbing %p:%s in stash %s\n",
2511 thr, cv, SvPEEK((SV*)cv), CvSTASH(cv) ?
3de9ffa1 2512 HvNAME(CvSTASH(cv)) : "(none)"));
cd06dffe
GS
2513 }
2514 else {
3de9ffa1
MB
2515 /* Make a new clone. */
2516 CV *clonecv;
2517 SvREFCNT_inc(cv); /* don't let it vanish from under us */
2518 MUTEX_UNLOCK(CvMUTEXP(cv));
bf49b057 2519 DEBUG_S((PerlIO_printf(Perl_debug_log,
1fd28e87
MB
2520 "entersub: %p cloning %p:%s\n",
2521 thr, cv, SvPEEK((SV*)cv))));
3de9ffa1
MB
2522 /*
2523 * We're creating a new clone so there's no race
2524 * between the original MUTEX_UNLOCK and the
2525 * SvREFCNT_inc since no one will be trying to undef
2526 * it out from underneath us. At least, I don't think
2527 * there's a race...
2528 */
2529 clonecv = cv_clone(cv);
2530 SvREFCNT_dec(cv); /* finished with this */
199100c8 2531 hv_store(thr->cvcache, (char*)cv, sizeof(cv), (SV*)clonecv,0);
3de9ffa1
MB
2532 CvOWNER(clonecv) = thr;
2533 cv = clonecv;
11343788 2534 SvREFCNT_inc(cv);
11343788 2535 }
8b73bbec 2536 DEBUG_S(if (CvDEPTH(cv) != 0)
bf49b057 2537 PerlIO_printf(Perl_debug_log, "depth %ld != 0\n",
3de9ffa1 2538 CvDEPTH(cv)););
c76ac1ee 2539 SAVEDESTRUCTOR_X(unset_cvowner, (void*) cv);
11343788 2540 }
3de9ffa1 2541 }
11343788
MB
2542#endif /* USE_THREADS */
2543
a0d0e21e 2544 if (CvXSUB(cv)) {
67caa1fe 2545#ifdef PERL_XSUB_OLDSTYLE
a0d0e21e 2546 if (CvOLDSTYLE(cv)) {
20ce7b12 2547 I32 (*fp3)(int,int,int);
a0d0e21e
LW
2548 dMARK;
2549 register I32 items = SP - MARK;
67955e0c 2550 /* We dont worry to copy from @_. */
924508f0
GS
2551 while (SP > mark) {
2552 SP[1] = SP[0];
2553 SP--;
a0d0e21e 2554 }
3280af22 2555 PL_stack_sp = mark + 1;
1d7c1841 2556 fp3 = (I32(*)(int,int,int))CvXSUB(cv);
1c846c1f 2557 items = (*fp3)(CvXSUBANY(cv).any_i32,
3280af22 2558 MARK - PL_stack_base + 1,
ecfc5424 2559 items);
3280af22 2560 PL_stack_sp = PL_stack_base + items;
a0d0e21e 2561 }
67caa1fe
GS
2562 else
2563#endif /* PERL_XSUB_OLDSTYLE */
2564 {
748a9306
LW
2565 I32 markix = TOPMARK;
2566
a0d0e21e 2567 PUTBACK;
67955e0c
PP
2568
2569 if (!hasargs) {
2570 /* Need to copy @_ to stack. Alternative may be to
2571 * switch stack to @_, and copy return values
2572 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
6d4ff0d2
MB
2573 AV* av;
2574 I32 items;
2575#ifdef USE_THREADS
533c011a 2576 av = (AV*)PL_curpad[0];
6d4ff0d2 2577#else
3280af22 2578 av = GvAV(PL_defgv);
6d4ff0d2 2579#endif /* USE_THREADS */
93965878 2580 items = AvFILLp(av) + 1; /* @_ is not tieable */
67955e0c
PP
2581
2582 if (items) {
2583 /* Mark is at the end of the stack. */
924508f0
GS
2584 EXTEND(SP, items);
2585 Copy(AvARRAY(av), SP + 1, items, SV*);
2586 SP += items;
1c846c1f 2587 PUTBACK ;
67955e0c
PP
2588 }
2589 }
67caa1fe
GS
2590 /* We assume first XSUB in &DB::sub is the called one. */
2591 if (PL_curcopdb) {
1d7c1841 2592 SAVEVPTR(PL_curcop);
3280af22
NIS
2593 PL_curcop = PL_curcopdb;
2594 PL_curcopdb = NULL;
67955e0c
PP
2595 }
2596 /* Do we need to open block here? XXXX */
0cb96387 2597 (void)(*CvXSUB(cv))(aTHXo_ cv);
748a9306
LW
2598
2599 /* Enforce some sanity in scalar context. */
3280af22
NIS
2600 if (gimme == G_SCALAR && ++markix != PL_stack_sp - PL_stack_base ) {
2601 if (markix > PL_stack_sp - PL_stack_base)
2602 *(PL_stack_base + markix) = &PL_sv_undef;
748a9306 2603 else
3280af22
NIS
2604 *(PL_stack_base + markix) = *PL_stack_sp;
2605 PL_stack_sp = PL_stack_base + markix;
748a9306 2606 }
a0d0e21e
LW
2607 }
2608 LEAVE;
2609 return NORMAL;
2610 }
2611 else {
2612 dMARK;
2613 register I32 items = SP - MARK;
a0d0e21e
LW
2614 AV* padlist = CvPADLIST(cv);
2615 SV** svp = AvARRAY(padlist);
533c011a 2616 push_return(PL_op->op_next);
a0d0e21e
LW
2617 PUSHBLOCK(cx, CXt_SUB, MARK);
2618 PUSHSUB(cx);
2619 CvDEPTH(cv)++;
6b35e009
GS
2620 /* XXX This would be a natural place to set C<PL_compcv = cv> so
2621 * that eval'' ops within this sub know the correct lexical space.
2622 * Owing the speed considerations, we choose to search for the cv
2623 * in doeval() instead.
2624 */
a0d0e21e
LW
2625 if (CvDEPTH(cv) < 2)
2626 (void)SvREFCNT_inc(cv);
2627 else { /* save temporaries on recursion? */
1d7c1841 2628 PERL_STACK_OVERFLOW_CHECK();
93965878 2629 if (CvDEPTH(cv) > AvFILLp(padlist)) {
a0d0e21e
LW
2630 AV *av;
2631 AV *newpad = newAV();
4aa0a1f7 2632 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
93965878 2633 I32 ix = AvFILLp((AV*)svp[1]);
1d7c1841 2634 I32 names_fill = AvFILLp((AV*)svp[0]);
a0d0e21e 2635 svp = AvARRAY(svp[0]);
748a9306 2636 for ( ;ix > 0; ix--) {
1d7c1841 2637 if (names_fill >= ix && svp[ix] != &PL_sv_undef) {
748a9306 2638 char *name = SvPVX(svp[ix]);
5f05dabc
PP
2639 if ((SvFLAGS(svp[ix]) & SVf_FAKE) /* outer lexical? */
2640 || *name == '&') /* anonymous code? */
2641 {
2642 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix]));
748a9306
LW
2643 }
2644 else { /* our own lexical */
2645 if (*name == '@')
2646 av_store(newpad, ix, sv = (SV*)newAV());
2647 else if (*name == '%')
2648 av_store(newpad, ix, sv = (SV*)newHV());
2649 else
2650 av_store(newpad, ix, sv = NEWSV(0,0));
2651 SvPADMY_on(sv);
2652 }
a0d0e21e 2653 }
1d7c1841
GS
2654 else if (IS_PADGV(oldpad[ix]) || IS_PADCONST(oldpad[ix])) {
2655 av_store(newpad, ix, sv = SvREFCNT_inc(oldpad[ix]));
2656 }
a0d0e21e 2657 else {
748a9306 2658 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
2659 SvPADTMP_on(sv);
2660 }
2661 }
2662 av = newAV(); /* will be @_ */
2663 av_extend(av, 0);
2664 av_store(newpad, 0, (SV*)av);
2665 AvFLAGS(av) = AVf_REIFY;
2666 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
93965878 2667 AvFILLp(padlist) = CvDEPTH(cv);
a0d0e21e
LW
2668 svp = AvARRAY(padlist);
2669 }
2670 }
6d4ff0d2
MB
2671#ifdef USE_THREADS
2672 if (!hasargs) {
533c011a 2673 AV* av = (AV*)PL_curpad[0];
6d4ff0d2 2674
93965878 2675 items = AvFILLp(av) + 1;
6d4ff0d2
MB
2676 if (items) {
2677 /* Mark is at the end of the stack. */
924508f0
GS
2678 EXTEND(SP, items);
2679 Copy(AvARRAY(av), SP + 1, items, SV*);
2680 SP += items;
1c846c1f 2681 PUTBACK ;
6d4ff0d2
MB
2682 }
2683 }
2684#endif /* USE_THREADS */
1d7c1841 2685 SAVEVPTR(PL_curpad);
3280af22 2686 PL_curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
6d4ff0d2
MB
2687#ifndef USE_THREADS
2688 if (hasargs)
2689#endif /* USE_THREADS */
2690 {
2691 AV* av;
a0d0e21e
LW
2692 SV** ary;
2693
77a005ab 2694#if 0
bf49b057 2695 DEBUG_S(PerlIO_printf(Perl_debug_log,
0f15f207 2696 "%p entersub preparing @_\n", thr));
77a005ab 2697#endif
3280af22 2698 av = (AV*)PL_curpad[0];
221373f0
GS
2699 if (AvREAL(av)) {
2700 /* @_ is normally not REAL--this should only ever
2701 * happen when DB::sub() calls things that modify @_ */
2702 av_clear(av);
2703 AvREAL_off(av);
2704 AvREIFY_on(av);
2705 }
6d4ff0d2 2706#ifndef USE_THREADS
3280af22
NIS
2707 cx->blk_sub.savearray = GvAV(PL_defgv);
2708 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
6d4ff0d2 2709#endif /* USE_THREADS */
7032098e 2710 cx->blk_sub.oldcurpad = PL_curpad;
6d4ff0d2 2711 cx->blk_sub.argarray = av;
a0d0e21e
LW
2712 ++MARK;
2713
2714 if (items > AvMAX(av) + 1) {
2715 ary = AvALLOC(av);
2716 if (AvARRAY(av) != ary) {
2717 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2718 SvPVX(av) = (char*)ary;
2719 }
2720 if (items > AvMAX(av) + 1) {
2721 AvMAX(av) = items - 1;
2722 Renew(ary,items,SV*);
2723 AvALLOC(av) = ary;
2724 SvPVX(av) = (char*)ary;
2725 }
2726 }
2727 Copy(MARK,AvARRAY(av),items,SV*);
93965878 2728 AvFILLp(av) = items - 1;
1c846c1f 2729
a0d0e21e
LW
2730 while (items--) {
2731 if (*MARK)
2732 SvTEMP_off(*MARK);
2733 MARK++;
2734 }
2735 }
4a925ff6
GS
2736 /* warning must come *after* we fully set up the context
2737 * stuff so that __WARN__ handlers can safely dounwind()
2738 * if they want to
2739 */
2740 if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION)
2741 && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))
2742 sub_crush_depth(cv);
77a005ab 2743#if 0
bf49b057 2744 DEBUG_S(PerlIO_printf(Perl_debug_log,
0f15f207 2745 "%p entersub returning %p\n", thr, CvSTART(cv)));
77a005ab 2746#endif
a0d0e21e
LW
2747 RETURNOP(CvSTART(cv));
2748 }
2749}
2750
44a8e56a 2751void
864dbfa3 2752Perl_sub_crush_depth(pTHX_ CV *cv)
44a8e56a
PP
2753{
2754 if (CvANON(cv))
cea2e8a9 2755 Perl_warner(aTHX_ WARN_RECURSION, "Deep recursion on anonymous subroutine");
44a8e56a
PP
2756 else {
2757 SV* tmpstr = sv_newmortal();
2758 gv_efullname3(tmpstr, CvGV(cv), Nullch);
1c846c1f 2759 Perl_warner(aTHX_ WARN_RECURSION, "Deep recursion on subroutine \"%s\"",
599cee73 2760 SvPVX(tmpstr));
44a8e56a
PP
2761 }
2762}
2763
a0d0e21e
LW
2764PP(pp_aelem)
2765{
4e35701f 2766 djSP;
a0d0e21e 2767 SV** svp;
748a9306 2768 I32 elem = POPi;
68dc0745 2769 AV* av = (AV*)POPs;
533c011a
NIS
2770 U32 lval = PL_op->op_flags & OPf_MOD;
2771 U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > AvFILL(av));
be6c24e0 2772 SV *sv;
a0d0e21e 2773
748a9306 2774 if (elem > 0)
3280af22 2775 elem -= PL_curcop->cop_arybase;
a0d0e21e
LW
2776 if (SvTYPE(av) != SVt_PVAV)
2777 RETPUSHUNDEF;
68dc0745 2778 svp = av_fetch(av, elem, lval && !defer);
a0d0e21e 2779 if (lval) {
3280af22 2780 if (!svp || *svp == &PL_sv_undef) {
68dc0745
PP
2781 SV* lv;
2782 if (!defer)
cea2e8a9 2783 DIE(aTHX_ PL_no_aelem, elem);
68dc0745
PP
2784 lv = sv_newmortal();
2785 sv_upgrade(lv, SVt_PVLV);
2786 LvTYPE(lv) = 'y';
2787 sv_magic(lv, Nullsv, 'y', Nullch, 0);
2788 LvTARG(lv) = SvREFCNT_inc(av);
2789 LvTARGOFF(lv) = elem;
2790 LvTARGLEN(lv) = 1;
2791 PUSHs(lv);
2792 RETURN;
2793 }
533c011a 2794 if (PL_op->op_private & OPpLVAL_INTRO)
161b7d16 2795 save_aelem(av, elem, svp);
533c011a
NIS
2796 else if (PL_op->op_private & OPpDEREF)
2797 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
a0d0e21e 2798 }
3280af22 2799 sv = (svp ? *svp : &PL_sv_undef);
be6c24e0
GS
2800 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
2801 sv = sv_mortalcopy(sv);
2802 PUSHs(sv);
a0d0e21e
LW
2803 RETURN;
2804}
2805
02a9e968 2806void
864dbfa3 2807Perl_vivify_ref(pTHX_ SV *sv, U32 to_what)
02a9e968
CS
2808{
2809 if (SvGMAGICAL(sv))
2810 mg_get(sv);
2811 if (!SvOK(sv)) {
2812 if (SvREADONLY(sv))
cea2e8a9 2813 Perl_croak(aTHX_ PL_no_modify);
5f05dabc
PP
2814 if (SvTYPE(sv) < SVt_RV)
2815 sv_upgrade(sv, SVt_RV);
2816 else if (SvTYPE(sv) >= SVt_PV) {
2817 (void)SvOOK_off(sv);
2818 Safefree(SvPVX(sv));
2819 SvLEN(sv) = SvCUR(sv) = 0;
2820 }
68dc0745 2821 switch (to_what) {
5f05dabc 2822 case OPpDEREF_SV:
8c52afec 2823 SvRV(sv) = NEWSV(355,0);
5f05dabc
PP
2824 break;
2825 case OPpDEREF_AV:
2826 SvRV(sv) = (SV*)newAV();
2827 break;
2828 case OPpDEREF_HV:
2829 SvRV(sv) = (SV*)newHV();
2830 break;
2831 }
02a9e968
CS
2832 SvROK_on(sv);
2833 SvSETMAGIC(sv);
2834 }
2835}
2836
a0d0e21e
LW
2837PP(pp_method)
2838{
4e35701f 2839 djSP;
f5d5a27c
CS
2840 SV* sv = TOPs;
2841
2842 if (SvROK(sv)) {
eda383f2 2843 SV* rsv = SvRV(sv);
f5d5a27c
CS
2844 if (SvTYPE(rsv) == SVt_PVCV) {
2845 SETs(rsv);
2846 RETURN;
2847 }
2848 }
2849
2850 SETs(method_common(sv, Null(U32*)));
2851 RETURN;
2852}
2853
2854PP(pp_method_named)
2855{
2856 djSP;
2857 SV* sv = cSVOP->op_sv;
2858 U32 hash = SvUVX(sv);
2859
2860 XPUSHs(method_common(sv, &hash));
2861 RETURN;
2862}
2863
2864STATIC SV *
2865S_method_common(pTHX_ SV* meth, U32* hashp)
2866{
a0d0e21e
LW
2867 SV* sv;
2868 SV* ob;
2869 GV* gv;
56304f61
CS
2870 HV* stash;
2871 char* name;
f5d5a27c 2872 STRLEN namelen;
ac91690f
CS
2873 char* packname;
2874 STRLEN packlen;
a0d0e21e 2875
f5d5a27c 2876 name = SvPV(meth, namelen);
3280af22 2877 sv = *(PL_stack_base + TOPMARK + 1);
f5d5a27c 2878
16d20bd9
AD
2879 if (SvGMAGICAL(sv))
2880 mg_get(sv);
a0d0e21e 2881 if (SvROK(sv))
16d20bd9 2882 ob = (SV*)SvRV(sv);
a0d0e21e
LW
2883 else {
2884 GV* iogv;
a0d0e21e 2885
56304f61 2886 packname = Nullch;
a0d0e21e 2887 if (!SvOK(sv) ||
56304f61 2888 !(packname = SvPV(sv, packlen)) ||
a0d0e21e
LW
2889 !(iogv = gv_fetchpv(packname, FALSE, SVt_PVIO)) ||
2890 !(ob=(SV*)GvIO(iogv)))
2891 {
1c846c1f 2892 if (!packname ||
7e2040f0 2893 ((*(U8*)packname >= 0xc0 && DO_UTF8(sv))
b86a2fa7 2894 ? !isIDFIRST_utf8((U8*)packname)
834a4ddd
LW
2895 : !isIDFIRST(*packname)
2896 ))
2897 {
f5d5a27c
CS
2898 Perl_croak(aTHX_ "Can't call method \"%s\" %s", name,
2899 SvOK(sv) ? "without a package or object reference"
2900 : "on an undefined value");
834a4ddd 2901 }
56304f61 2902 stash = gv_stashpvn(packname, packlen, TRUE);
ac91690f 2903 goto fetch;
a0d0e21e 2904 }
3280af22 2905 *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
a0d0e21e
LW
2906 }
2907
f0d43078
GS
2908 if (!ob || !(SvOBJECT(ob)
2909 || (SvTYPE(ob) == SVt_PVGV && (ob = (SV*)GvIO((GV*)ob))
2910 && SvOBJECT(ob))))
2911 {
f5d5a27c
CS
2912 Perl_croak(aTHX_ "Can't call method \"%s\" on unblessed reference",
2913 name);
f0d43078 2914 }
a0d0e21e 2915
56304f61 2916 stash = SvSTASH(ob);
a0d0e21e 2917
ac91690f 2918 fetch:
f5d5a27c
CS
2919 /* shortcut for simple names */
2920 if (hashp) {
2921 HE* he = hv_fetch_ent(stash, meth, 0, *hashp);
2922 if (he) {
2923 gv = (GV*)HeVAL(he);
2924 if (isGV(gv) && GvCV(gv) &&
2925 (!GvCVGEN(gv) || GvCVGEN(gv) == PL_sub_generation))
2926 return (SV*)GvCV(gv);
2927 }
2928 }
2929
ac91690f 2930 gv = gv_fetchmethod(stash, name);
56304f61
CS
2931 if (!gv) {
2932 char* leaf = name;
2933 char* sep = Nullch;
2934 char* p;
c1899e02 2935 GV* gv;
56304f61
CS
2936
2937 for (p = name; *p; p++) {
2938 if (*p == '\'')
2939 sep = p, leaf = p + 1;
2940 else if (*p == ':' && *(p + 1) == ':')
2941 sep = p, leaf = p + 2;
2942 }
2943 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
1d7c1841 2944 packname = sep ? CopSTASHPV(PL_curcop) : HvNAME(stash);
56304f61
CS
2945 packlen = strlen(packname);
2946 }
2947 else {
2948 packname = name;
2949 packlen = sep - name;
2950 }
c1899e02
GS
2951 gv = gv_fetchpv(packname, 0, SVt_PVHV);
2952 if (gv && isGV(gv)) {
2953 Perl_croak(aTHX_
2954 "Can't locate object method \"%s\" via package \"%s\"",
2955 leaf, packname);
2956 }
2957 else {
2958 Perl_croak(aTHX_
f6e565ef 2959 "Can't locate object method \"%s\" via package \"%s\""
c1899e02
GS
2960 " (perhaps you forgot to load \"%s\"?)",
2961 leaf, packname, packname);
2962 }
56304f61 2963 }
f5d5a27c 2964 return isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv;
a0d0e21e 2965}
22239a37 2966
51371543
GS
2967#ifdef USE_THREADS
2968static void
2969unset_cvowner(pTHXo_ void *cvarg)
2970{
2971 register CV* cv = (CV *) cvarg;
2972#ifdef DEBUGGING
2973 dTHR;
2974#endif /* DEBUGGING */
2975
bf49b057 2976 DEBUG_S((PerlIO_printf(Perl_debug_log, "%p unsetting CvOWNER of %p:%s\n",
51371543
GS
2977 thr, cv, SvPEEK((SV*)cv))));
2978 MUTEX_LOCK(CvMUTEXP(cv));
2979 DEBUG_S(if (CvDEPTH(cv) != 0)
bf49b057 2980 PerlIO_printf(Perl_debug_log, "depth %ld != 0\n",
51371543
GS
2981 CvDEPTH(cv)););
2982 assert(thr == CvOWNER(cv));
2983 CvOWNER(cv) = 0;
2984 MUTEX_UNLOCK(CvMUTEXP(cv));
2985 SvREFCNT_dec(cv);
2986}
2987#endif /* USE_THREADS */