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