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