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