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