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