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