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