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