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