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