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