This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
revert POP{SUB,LOOP}{1,2} logic to the simpler pre-5.003_24
[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;
a0d0e21e
LW
43 XPUSHs(cSVOP->op_sv);
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)
a0d0e21e
LW
61 PUSHs(save_scalar(cGVOP->op_gv));
62 else
63 PUSHs(GvSV(cGVOP->op_gv));
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;
a0d0e21e
LW
98 XPUSHs((SV*)cGVOP->op_gv);
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;
a0d0e21e 274 AV *av = GvAV((GV*)cSVOP->op_sv);
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
GS
370 Perl_warner(aTHX_ WARN_CLOSED,
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))
cea2e8a9 462 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_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))
cea2e8a9 562 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_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) {
1088 IoFLAGS(io) &= ~IOf_START;
1089 IoLINES(io) = 0;
3280af22 1090 if (av_len(GvAVn(PL_last_in_gv)) < 0) {
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 IoFLAGS(io) |= IOf_START;
1102 }
1103 }
1104 else if (type == OP_GLOB) {
1105 SV *tmpcmd = NEWSV(55, 0);
1106 SV *tmpglob = POPs;
1107 ENTER;
1108 SAVEFREESV(tmpcmd);
1109#ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
1110 /* since spawning off a process is a real performance hit */
1111 {
1112#include <descrip.h>
1113#include <lib$routines.h>
1114#include <nam.h>
1115#include <rmsdef.h>
1116 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
1117 char vmsspec[NAM$C_MAXRSS+1];
1118 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
1119 char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
1120 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
760ac839 1121 PerlIO *tmpfp;
a0d0e21e
LW
1122 STRLEN i;
1123 struct dsc$descriptor_s wilddsc
1124 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
1125 struct dsc$descriptor_vs rsdsc
1126 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
1127 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
1128
1129 /* We could find out if there's an explicit dev/dir or version
1130 by peeking into lib$find_file's internal context at
1131 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
1132 but that's unsupported, so I don't want to do it now and
1133 have it bite someone in the future. */
6ad3d225 1134 strcat(tmpfnam,PerlLIO_tmpnam(NULL));
a0d0e21e
LW
1135 cp = SvPV(tmpglob,i);
1136 for (; i; i--) {
1137 if (cp[i] == ';') hasver = 1;
1138 if (cp[i] == '.') {
1139 if (sts) hasver = 1;
1140 else sts = 1;
1141 }
1142 if (cp[i] == '/') {
1143 hasdir = isunix = 1;
1144 break;
748a9306 1145 }
a0d0e21e
LW
1146 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
1147 hasdir = 1;
1148 break;
1149 }
1150 }
760ac839 1151 if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
09b7f37c 1152 Stat_t st;
c6ed36e1 1153 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
09b7f37c
CB
1154 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
1155 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
a0d0e21e
LW
1156 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
1157 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
1158 &dfltdsc,NULL,NULL,NULL))&1)) {
1159 end = rstr + (unsigned long int) *rslt;
1160 if (!hasver) while (*end != ';') end--;
1161 *(end++) = '\n'; *end = '\0';
1162 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
1163 if (hasdir) {
f86702cc 1164 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
a0d0e21e
LW
1165 begin = rstr;
1166 }
1167 else {
1168 begin = end;
1169 while (*(--begin) != ']' && *begin != '>') ;
1170 ++begin;
1171 }
760ac839 1172 ok = (PerlIO_puts(tmpfp,begin) != EOF);
a0d0e21e
LW
1173 }
1174 if (cxt) (void)lib$find_file_end(&cxt);
748a9306
LW
1175 if (ok && sts != RMS$_NMF &&
1176 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
a0d0e21e 1177 if (!ok) {
c07a80fd
PP
1178 if (!(sts & 1)) {
1179 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
1180 }
760ac839 1181 PerlIO_close(tmpfp);
a0d0e21e
LW
1182 fp = NULL;
1183 }
1184 else {
760ac839 1185 PerlIO_rewind(tmpfp);
a0d0e21e
LW
1186 IoTYPE(io) = '<';
1187 IoIFP(io) = fp = tmpfp;
1e422769 1188 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
a0d0e21e
LW
1189 }
1190 }
1191 }
1192#else /* !VMS */
1193#ifdef DOSISH
67955e0c
PP
1194#ifdef OS2
1195 sv_setpv(tmpcmd, "for a in ");
1196 sv_catsv(tmpcmd, tmpglob);
1197 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
1198#else
39e571d4
ML
1199#ifdef DJGPP
1200 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
1201 sv_catsv(tmpcmd, tmpglob);
1202#else
a0d0e21e
LW
1203 sv_setpv(tmpcmd, "perlglob ");
1204 sv_catsv(tmpcmd, tmpglob);
1205 sv_catpv(tmpcmd, " |");
39e571d4 1206#endif /* !DJGPP */
67955e0c
PP
1207#endif /* !OS2 */
1208#else /* !DOSISH */
1209#if defined(CSH)
3280af22 1210 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
a0d0e21e
LW
1211 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
1212 sv_catsv(tmpcmd, tmpglob);
16d20bd9 1213 sv_catpv(tmpcmd, "' 2>/dev/null |");
a0d0e21e
LW
1214#else
1215 sv_setpv(tmpcmd, "echo ");
1216 sv_catsv(tmpcmd, tmpglob);
1217#if 'z' - 'a' == 25
1218 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
1219#else
1220 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
1221#endif
1222#endif /* !CSH */
67955e0c 1223#endif /* !DOSISH */
3280af22 1224 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
9d116dd7 1225 FALSE, O_RDONLY, 0, Nullfp);
a0d0e21e
LW
1226 fp = IoIFP(io);
1227#endif /* !VMS */
1228 LEAVE;
1229 }
1230 }
1231 else if (type == OP_GLOB)
1232 SP--;
af8c498a
GS
1233 else if (ckWARN(WARN_IO) /* stdout/stderr or other write fh */
1234 && (IoTYPE(io) == '>' || fp == PerlIO_stdout()
1235 || fp == PerlIO_stderr()))
1236 {
1237 SV* sv = sv_newmortal();
1238 gv_efullname3(sv, PL_last_in_gv, Nullch);
1239 Perl_warner(aTHX_ WARN_IO, "Filehandle %s opened only for output",
1240 SvPV_nolen(sv));
1241 }
a0d0e21e
LW
1242 }
1243 if (!fp) {
3f4520fe
GS
1244 if (ckWARN(WARN_CLOSED) && io && !(IoFLAGS(io) & IOf_START)) {
1245 if (type == OP_GLOB)
af8c498a
GS
1246 Perl_warner(aTHX_ WARN_CLOSED,
1247 "glob failed (can't start child: %s)",
1248 Strerror(errno));
1249 else {
1250 SV* sv = sv_newmortal();
1251 gv_efullname3(sv, PL_last_in_gv, Nullch);
1252 Perl_warner(aTHX_ WARN_CLOSED,
1253 "Read on closed filehandle %s",
1254 SvPV_nolen(sv));
1255 }
3f4520fe 1256 }
54310121 1257 if (gimme == G_SCALAR) {
a0d0e21e
LW
1258 (void)SvOK_off(TARG);
1259 PUSHTARG;
1260 }
1261 RETURN;
1262 }
a2008d6d 1263 have_fp:
54310121 1264 if (gimme == G_SCALAR) {
a0d0e21e 1265 sv = TARG;
9607fc9c
PP
1266 if (SvROK(sv))
1267 sv_unref(sv);
a0d0e21e
LW
1268 (void)SvUPGRADE(sv, SVt_PV);
1269 tmplen = SvLEN(sv); /* remember if already alloced */
1270 if (!tmplen)
1271 Sv_Grow(sv, 80); /* try short-buffering it */
1272 if (type == OP_RCATLINE)
1273 offset = SvCUR(sv);
1274 else
1275 offset = 0;
1276 }
54310121
PP
1277 else {
1278 sv = sv_2mortal(NEWSV(57, 80));
1279 offset = 0;
1280 }
fbad3eb5
GS
1281
1282/* flip-flop EOF state for a snarfed empty file */
1283#define SNARF_EOF(gimme,rs,io,sv) \
1284 ((gimme != G_SCALAR || SvCUR(sv) \
1285 || (IoFLAGS(io) & IOf_NOLINE) || IoLINES(io) || !RsSNARF(rs)) \
1286 ? ((IoFLAGS(io) &= ~IOf_NOLINE), TRUE) \
1287 : ((IoFLAGS(io) |= IOf_NOLINE), FALSE))
1288
a0d0e21e 1289 for (;;) {
fbad3eb5
GS
1290 if (!sv_gets(sv, fp, offset)
1291 && (type == OP_GLOB || SNARF_EOF(gimme, PL_rs, io, sv)))
1292 {
760ac839 1293 PerlIO_clearerr(fp);
a0d0e21e 1294 if (IoFLAGS(io) & IOf_ARGV) {
3280af22 1295 fp = nextargv(PL_last_in_gv);
a0d0e21e
LW
1296 if (fp)
1297 continue;
3280af22 1298 (void)do_close(PL_last_in_gv, FALSE);
a0d0e21e
LW
1299 IoFLAGS(io) |= IOf_START;
1300 }
1301 else if (type == OP_GLOB) {
4eb79ab5 1302 if (!do_close(PL_last_in_gv, FALSE) && ckWARN(WARN_CLOSED)) {
cea2e8a9 1303 Perl_warner(aTHX_ WARN_CLOSED,
4eb79ab5
GS
1304 "glob failed (child exited with status %d%s)",
1305 STATUS_CURRENT >> 8,
cf494569 1306 (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
4eb79ab5 1307 }
a0d0e21e 1308 }
54310121 1309 if (gimme == G_SCALAR) {
a0d0e21e
LW
1310 (void)SvOK_off(TARG);
1311 PUSHTARG;
1312 }
1313 RETURN;
1314 }
bbce6d69
PP
1315 /* This should not be marked tainted if the fp is marked clean */
1316 if (!(IoFLAGS(io) & IOf_UNTAINT)) {
1317 TAINT;
1318 SvTAINTED_on(sv);
1319 }
a0d0e21e 1320 IoLINES(io)++;
71be2cbc 1321 SvSETMAGIC(sv);
a0d0e21e 1322 XPUSHs(sv);
a0d0e21e
LW
1323 if (type == OP_GLOB) {
1324 char *tmps;
1325
3280af22 1326 if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) {
c07a80fd 1327 tmps = SvEND(sv) - 1;
3280af22 1328 if (*tmps == *SvPVX(PL_rs)) {
c07a80fd
PP
1329 *tmps = '\0';
1330 SvCUR(sv)--;
1331 }
1332 }
a0d0e21e
LW
1333 for (tmps = SvPVX(sv); *tmps; tmps++)
1334 if (!isALPHA(*tmps) && !isDIGIT(*tmps) &&
1335 strchr("$&*(){}[]'\";\\|?<>~`", *tmps))
1336 break;
43384a1a 1337 if (*tmps && PerlLIO_lstat(SvPVX(sv), &PL_statbuf) < 0) {
a0d0e21e
LW
1338 (void)POPs; /* Unmatched wildcard? Chuck it... */
1339 continue;
1340 }
1341 }
54310121 1342 if (gimme == G_ARRAY) {
a0d0e21e
LW
1343 if (SvLEN(sv) - SvCUR(sv) > 20) {
1344 SvLEN_set(sv, SvCUR(sv)+1);
1345 Renew(SvPVX(sv), SvLEN(sv), char);
1346 }
1347 sv = sv_2mortal(NEWSV(58, 80));
1348 continue;
1349 }
54310121 1350 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
a0d0e21e
LW
1351 /* try to reclaim a bit of scalar space (only on 1st alloc) */
1352 if (SvCUR(sv) < 60)
1353 SvLEN_set(sv, 80);
1354 else
1355 SvLEN_set(sv, SvCUR(sv)+40); /* allow some slop */
1356 Renew(SvPVX(sv), SvLEN(sv), char);
1357 }
1358 RETURN;
1359 }
1360}
1361
1362PP(pp_enter)
1363{
4e35701f 1364 djSP;
c09156bb 1365 register PERL_CONTEXT *cx;
533c011a 1366 I32 gimme = OP_GIMME(PL_op, -1);
a0d0e21e 1367
54310121
PP
1368 if (gimme == -1) {
1369 if (cxstack_ix >= 0)
1370 gimme = cxstack[cxstack_ix].blk_gimme;
1371 else
1372 gimme = G_SCALAR;
1373 }
a0d0e21e
LW
1374
1375 ENTER;
1376
1377 SAVETMPS;
924508f0 1378 PUSHBLOCK(cx, CXt_BLOCK, SP);
a0d0e21e
LW
1379
1380 RETURN;
1381}
1382
1383PP(pp_helem)
1384{
4e35701f 1385 djSP;
760ac839 1386 HE* he;
ae77835f 1387 SV **svp;
a0d0e21e 1388 SV *keysv = POPs;
a0d0e21e 1389 HV *hv = (HV*)POPs;
533c011a
NIS
1390 U32 lval = PL_op->op_flags & OPf_MOD;
1391 U32 defer = PL_op->op_private & OPpLVAL_DEFER;
be6c24e0 1392 SV *sv;
a0d0e21e 1393
ae77835f
MB
1394 if (SvTYPE(hv) == SVt_PVHV) {
1395 he = hv_fetch_ent(hv, keysv, lval && !defer, 0);
97fcbf96 1396 svp = he ? &HeVAL(he) : 0;
ae77835f
MB
1397 }
1398 else if (SvTYPE(hv) == SVt_PVAV) {
0ebe0038 1399 if (PL_op->op_private & OPpLVAL_INTRO)
cea2e8a9 1400 DIE(aTHX_ "Can't localize pseudo-hash element");
97fcbf96 1401 svp = avhv_fetch_ent((AV*)hv, keysv, lval && !defer, 0);
ae77835f 1402 }
c750a3ec 1403 else {
a0d0e21e 1404 RETPUSHUNDEF;
c750a3ec 1405 }
a0d0e21e 1406 if (lval) {
3280af22 1407 if (!svp || *svp == &PL_sv_undef) {
68dc0745
PP
1408 SV* lv;
1409 SV* key2;
2d8e6c8d
GS
1410 if (!defer) {
1411 STRLEN n_a;
cea2e8a9 1412 DIE(aTHX_ PL_no_helem, SvPV(keysv, n_a));
2d8e6c8d 1413 }
68dc0745
PP
1414 lv = sv_newmortal();
1415 sv_upgrade(lv, SVt_PVLV);
1416 LvTYPE(lv) = 'y';
1417 sv_magic(lv, key2 = newSVsv(keysv), 'y', Nullch, 0);
1418 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1419 LvTARG(lv) = SvREFCNT_inc(hv);
1420 LvTARGLEN(lv) = 1;
1421 PUSHs(lv);
1422 RETURN;
1423 }
533c011a 1424 if (PL_op->op_private & OPpLVAL_INTRO) {
ae77835f 1425 if (HvNAME(hv) && isGV(*svp))
533c011a 1426 save_gp((GV*)*svp, !(PL_op->op_flags & OPf_SPECIAL));
5f05dabc 1427 else
161b7d16 1428 save_helem(hv, keysv, svp);
5f05dabc 1429 }
533c011a
NIS
1430 else if (PL_op->op_private & OPpDEREF)
1431 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
a0d0e21e 1432 }
3280af22 1433 sv = (svp ? *svp : &PL_sv_undef);
be6c24e0
GS
1434 /* This makes C<local $tied{foo} = $tied{foo}> possible.
1435 * Pushing the magical RHS on to the stack is useless, since
1436 * that magic is soon destined to be misled by the local(),
1437 * and thus the later pp_sassign() will fail to mg_get() the
1438 * old value. This should also cure problems with delayed
1439 * mg_get()s. GSAR 98-07-03 */
1440 if (!lval && SvGMAGICAL(sv))
1441 sv = sv_mortalcopy(sv);
1442 PUSHs(sv);
a0d0e21e
LW
1443 RETURN;
1444}
1445
1446PP(pp_leave)
1447{
4e35701f 1448 djSP;
c09156bb 1449 register PERL_CONTEXT *cx;
a0d0e21e
LW
1450 register SV **mark;
1451 SV **newsp;
1452 PMOP *newpm;
1453 I32 gimme;
1454
533c011a 1455 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e 1456 cx = &cxstack[cxstack_ix];
3280af22 1457 cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */
a0d0e21e
LW
1458 }
1459
1460 POPBLOCK(cx,newpm);
1461
533c011a 1462 gimme = OP_GIMME(PL_op, -1);
54310121
PP
1463 if (gimme == -1) {
1464 if (cxstack_ix >= 0)
1465 gimme = cxstack[cxstack_ix].blk_gimme;
1466 else
1467 gimme = G_SCALAR;
1468 }
a0d0e21e 1469
a1f49e72 1470 TAINT_NOT;
54310121
PP
1471 if (gimme == G_VOID)
1472 SP = newsp;
1473 else if (gimme == G_SCALAR) {
1474 MARK = newsp + 1;
1475 if (MARK <= SP)
1476 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1477 *MARK = TOPs;
1478 else
1479 *MARK = sv_mortalcopy(TOPs);
a0d0e21e 1480 else {
54310121 1481 MEXTEND(mark,0);
3280af22 1482 *MARK = &PL_sv_undef;
a0d0e21e 1483 }
54310121 1484 SP = MARK;
a0d0e21e 1485 }
54310121 1486 else if (gimme == G_ARRAY) {
a1f49e72
CS
1487 /* in case LEAVE wipes old return values */
1488 for (mark = newsp + 1; mark <= SP; mark++) {
1489 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
a0d0e21e 1490 *mark = sv_mortalcopy(*mark);
a1f49e72
CS
1491 TAINT_NOT; /* Each item is independent */
1492 }
1493 }
a0d0e21e 1494 }
3280af22 1495 PL_curpm = newpm; /* Don't pop $1 et al till now */
a0d0e21e
LW
1496
1497 LEAVE;
1498
1499 RETURN;
1500}
1501
1502PP(pp_iter)
1503{
4e35701f 1504 djSP;
c09156bb 1505 register PERL_CONTEXT *cx;
5f05dabc 1506 SV* sv;
4633a7c4 1507 AV* av;
a0d0e21e 1508
924508f0 1509 EXTEND(SP, 1);
a0d0e21e 1510 cx = &cxstack[cxstack_ix];
6b35e009 1511 if (CxTYPE(cx) != CXt_LOOP)
cea2e8a9 1512 DIE(aTHX_ "panic: pp_iter");
a0d0e21e 1513
4633a7c4 1514 av = cx->blk_loop.iterary;
89ea2908
GA
1515 if (SvTYPE(av) != SVt_PVAV) {
1516 /* iterate ($min .. $max) */
1517 if (cx->blk_loop.iterlval) {
1518 /* string increment */
1519 register SV* cur = cx->blk_loop.iterlval;
1520 STRLEN maxlen;
1521 char *max = SvPV((SV*)av, maxlen);
1522 if (!SvNIOK(cur) && SvCUR(cur) <= maxlen) {
eaa5c2d6 1523#ifndef USE_THREADS /* don't risk potential race */
39ca2bb7
SM
1524 if (SvREFCNT(*cx->blk_loop.itervar) == 1
1525 && !SvMAGICAL(*cx->blk_loop.itervar))
1526 {
eaa5c2d6
GA
1527 /* safe to reuse old SV */
1528 sv_setsv(*cx->blk_loop.itervar, cur);
1529 }
1530 else
1531#endif
1532 {
1533 /* we need a fresh SV every time so that loop body sees a
1534 * completely new SV for closures/references to work as
1535 * they used to */
1536 SvREFCNT_dec(*cx->blk_loop.itervar);
1537 *cx->blk_loop.itervar = newSVsv(cur);
1538 }
89ea2908
GA
1539 if (strEQ(SvPVX(cur), max))
1540 sv_setiv(cur, 0); /* terminate next time */
1541 else
1542 sv_inc(cur);
1543 RETPUSHYES;
1544 }
1545 RETPUSHNO;
1546 }
1547 /* integer increment */
1548 if (cx->blk_loop.iterix > cx->blk_loop.itermax)
1549 RETPUSHNO;
7f61b687 1550
eaa5c2d6 1551#ifndef USE_THREADS /* don't risk potential race */
39ca2bb7
SM
1552 if (SvREFCNT(*cx->blk_loop.itervar) == 1
1553 && !SvMAGICAL(*cx->blk_loop.itervar))
1554 {
eaa5c2d6
GA
1555 /* safe to reuse old SV */
1556 sv_setiv(*cx->blk_loop.itervar, cx->blk_loop.iterix++);
1557 }
1558 else
1559#endif
1560 {
1561 /* we need a fresh SV every time so that loop body sees a
1562 * completely new SV for closures/references to work as they
1563 * used to */
1564 SvREFCNT_dec(*cx->blk_loop.itervar);
1565 *cx->blk_loop.itervar = newSViv(cx->blk_loop.iterix++);
1566 }
89ea2908
GA
1567 RETPUSHYES;
1568 }
1569
1570 /* iterate array */
3280af22 1571 if (cx->blk_loop.iterix >= (av == PL_curstack ? cx->blk_oldsp : AvFILL(av)))
4633a7c4 1572 RETPUSHNO;
a0d0e21e 1573
7a4c00b4 1574 SvREFCNT_dec(*cx->blk_loop.itervar);
a0d0e21e 1575
93965878
NIS
1576 if (sv = (SvMAGICAL(av))
1577 ? *av_fetch(av, ++cx->blk_loop.iterix, FALSE)
1578 : AvARRAY(av)[++cx->blk_loop.iterix])
a0d0e21e 1579 SvTEMP_off(sv);
a0d0e21e 1580 else
3280af22
NIS
1581 sv = &PL_sv_undef;
1582 if (av != PL_curstack && SvIMMORTAL(sv)) {
5f05dabc 1583 SV *lv = cx->blk_loop.iterlval;
71be2cbc
PP
1584 if (lv && SvREFCNT(lv) > 1) {
1585 SvREFCNT_dec(lv);
1586 lv = Nullsv;
1587 }
5f05dabc
PP
1588 if (lv)
1589 SvREFCNT_dec(LvTARG(lv));
1590 else {
68dc0745 1591 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
5f05dabc 1592 sv_upgrade(lv, SVt_PVLV);
5f05dabc 1593 LvTYPE(lv) = 'y';
68dc0745 1594 sv_magic(lv, Nullsv, 'y', Nullch, 0);
5f05dabc
PP
1595 }
1596 LvTARG(lv) = SvREFCNT_inc(av);
1597 LvTARGOFF(lv) = cx->blk_loop.iterix;
42718184 1598 LvTARGLEN(lv) = (STRLEN)UV_MAX;
5f05dabc
PP
1599 sv = (SV*)lv;
1600 }
a0d0e21e 1601
7a4c00b4 1602 *cx->blk_loop.itervar = SvREFCNT_inc(sv);
a0d0e21e
LW
1603 RETPUSHYES;
1604}
1605
1606PP(pp_subst)
1607{
4e35701f 1608 djSP; dTARG;
a0d0e21e
LW
1609 register PMOP *pm = cPMOP;
1610 PMOP *rpm = pm;
1611 register SV *dstr;
1612 register char *s;
1613 char *strend;
1614 register char *m;
1615 char *c;
1616 register char *d;
1617 STRLEN clen;
1618 I32 iters = 0;
1619 I32 maxiters;
1620 register I32 i;
1621 bool once;
71be2cbc 1622 bool rxtainted;
a0d0e21e 1623 char *orig;
22e551b9 1624 I32 r_flags;
d9f97599 1625 register REGEXP *rx = pm->op_pmregexp;
a0d0e21e
LW
1626 STRLEN len;
1627 int force_on_match = 0;
3280af22 1628 I32 oldsave = PL_savestack_ix;
c277df42 1629 I32 update_minmatch = 1;
a0d0e21e 1630
5cd24f17
PP
1631 /* known replacement string? */
1632 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
533c011a 1633 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
1634 TARG = POPs;
1635 else {
54b9620d 1636 TARG = DEFSV;
a0d0e21e 1637 EXTEND(SP,1);
8ec5e241 1638 }
68dc0745
PP
1639 if (SvREADONLY(TARG)
1640 || (SvTYPE(TARG) > SVt_PVLV
1641 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG))))
d470f89e 1642 DIE(aTHX_ PL_no_modify);
8ec5e241
NIS
1643 PUTBACK;
1644
a0d0e21e 1645 s = SvPV(TARG, len);
68dc0745 1646 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
a0d0e21e 1647 force_on_match = 1;
b3eb6a9b 1648 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
3280af22
NIS
1649 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
1650 if (PL_tainted)
b3eb6a9b 1651 rxtainted |= 2;
9212bbba 1652 TAINT_NOT;
a0d0e21e
LW
1653
1654 force_it:
1655 if (!pm || !s)
cea2e8a9 1656 DIE(aTHX_ "panic: do_subst");
a0d0e21e
LW
1657
1658 strend = s + len;
2beec16e
IZ
1659 maxiters = 2*(strend - s) + 10; /* We can match twice at each
1660 position, once with zero-length,
1661 second time with non-zero. */
a0d0e21e 1662
3280af22
NIS
1663 if (!rx->prelen && PL_curpm) {
1664 pm = PL_curpm;
d9f97599 1665 rx = pm->op_pmregexp;
a0d0e21e 1666 }
22e551b9 1667 r_flags = (rx->nparens || SvTEMP(TARG) || PL_sawampersand)
9d080a66 1668 ? REXEC_COPY_STR : 0;
f722798b 1669 if (SvSCREAM(TARG))
22e551b9 1670 r_flags |= REXEC_SCREAM;
a0d0e21e 1671 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
3280af22
NIS
1672 SAVEINT(PL_multiline);
1673 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
a0d0e21e
LW
1674 }
1675 orig = m = s;
f722798b
IZ
1676 if (rx->reganch & RE_USE_INTUIT) {
1677 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, s, strend, r_flags, NULL);
1678
1679 if (!s)
1680 goto nope;
1681 /* How to do it in subst? */
1682/* if ( (rx->reganch & ROPT_CHECK_ALL)
1683 && !PL_sawampersand
1684 && ((rx->reganch & ROPT_NOSCAN)
1685 || !((rx->reganch & RE_INTUIT_TAIL)
1686 && (r_flags & REXEC_SCREAM))))
1687 goto yup;
1688*/
a0d0e21e 1689 }
71be2cbc
PP
1690
1691 /* only replace once? */
a0d0e21e 1692 once = !(rpm->op_pmflags & PMf_GLOBAL);
71be2cbc
PP
1693
1694 /* known replacement string? */
5cd24f17 1695 c = dstr ? SvPV(dstr, clen) : Nullch;
71be2cbc
PP
1696
1697 /* can do inplace substitution? */
22e551b9 1698 if (c && clen <= rx->minlen && (once || !(r_flags & REXEC_COPY_STR))
d9f97599 1699 && !(rx->reganch & ROPT_LOOKBEHIND_SEEN)) {
f722798b
IZ
1700 if (!CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
1701 r_flags | REXEC_CHECKED))
1702 {
8ec5e241 1703 SPAGAIN;
3280af22 1704 PUSHs(&PL_sv_no);
71be2cbc
PP
1705 LEAVE_SCOPE(oldsave);
1706 RETURN;
1707 }
1708 if (force_on_match) {
1709 force_on_match = 0;
1710 s = SvPV_force(TARG, len);
1711 goto force_it;
1712 }
71be2cbc 1713 d = s;
3280af22 1714 PL_curpm = pm;
71be2cbc
PP
1715 SvSCREAM_off(TARG); /* disable possible screamer */
1716 if (once) {
48c036b1 1717 rxtainted |= RX_MATCH_TAINTED(rx);
cf93c79d
IZ
1718 m = orig + rx->startp[0];
1719 d = orig + rx->endp[0];
71be2cbc
PP
1720 s = orig;
1721 if (m - s > strend - d) { /* faster to shorten from end */
1722 if (clen) {
1723 Copy(c, m, clen, char);
1724 m += clen;
a0d0e21e 1725 }
71be2cbc
PP
1726 i = strend - d;
1727 if (i > 0) {
1728 Move(d, m, i, char);
1729 m += i;
a0d0e21e 1730 }
71be2cbc
PP
1731 *m = '\0';
1732 SvCUR_set(TARG, m - s);
1733 }
1734 /*SUPPRESS 560*/
1735 else if (i = m - s) { /* faster from front */
1736 d -= clen;
1737 m = d;
1738 sv_chop(TARG, d-i);
1739 s += i;
1740 while (i--)
1741 *--d = *--s;
1742 if (clen)
1743 Copy(c, m, clen, char);
1744 }
1745 else if (clen) {
1746 d -= clen;
1747 sv_chop(TARG, d);
1748 Copy(c, d, clen, char);
1749 }
1750 else {
1751 sv_chop(TARG, d);
1752 }
48c036b1 1753 TAINT_IF(rxtainted & 1);
8ec5e241 1754 SPAGAIN;
3280af22 1755 PUSHs(&PL_sv_yes);
71be2cbc
PP
1756 }
1757 else {
71be2cbc
PP
1758 do {
1759 if (iters++ > maxiters)
cea2e8a9 1760 DIE(aTHX_ "Substitution loop");
d9f97599 1761 rxtainted |= RX_MATCH_TAINTED(rx);
cf93c79d 1762 m = rx->startp[0] + orig;
71be2cbc
PP
1763 /*SUPPRESS 560*/
1764 if (i = m - s) {
1765 if (s != d)
1766 Move(s, d, i, char);
1767 d += i;
a0d0e21e 1768 }
71be2cbc
PP
1769 if (clen) {
1770 Copy(c, d, clen, char);
1771 d += clen;
1772 }
cf93c79d 1773 s = rx->endp[0] + orig;
cea2e8a9 1774 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
f722798b
IZ
1775 TARG, NULL,
1776 /* don't match same null twice */
1777 REXEC_NOT_FIRST|REXEC_IGNOREPOS));
71be2cbc
PP
1778 if (s != d) {
1779 i = strend - s;
1780 SvCUR_set(TARG, d - SvPVX(TARG) + i);
1781 Move(s, d, i+1, char); /* include the NUL */
a0d0e21e 1782 }
48c036b1 1783 TAINT_IF(rxtainted & 1);
8ec5e241 1784 SPAGAIN;
71be2cbc 1785 PUSHs(sv_2mortal(newSViv((I32)iters)));
a0d0e21e 1786 }
71be2cbc 1787 (void)SvPOK_only(TARG);
48c036b1 1788 TAINT_IF(rxtainted);
8ec5e241
NIS
1789 if (SvSMAGICAL(TARG)) {
1790 PUTBACK;
1791 mg_set(TARG);
1792 SPAGAIN;
1793 }
9212bbba 1794 SvTAINT(TARG);
71be2cbc
PP
1795 LEAVE_SCOPE(oldsave);
1796 RETURN;
a0d0e21e 1797 }
71be2cbc 1798
f722798b
IZ
1799 if (CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
1800 r_flags | REXEC_CHECKED))
1801 {
a0d0e21e
LW
1802 if (force_on_match) {
1803 force_on_match = 0;
1804 s = SvPV_force(TARG, len);
1805 goto force_it;
1806 }
48c036b1 1807 rxtainted |= RX_MATCH_TAINTED(rx);
8ec5e241 1808 dstr = NEWSV(25, len);
a0d0e21e 1809 sv_setpvn(dstr, m, s-m);
3280af22 1810 PL_curpm = pm;
a0d0e21e 1811 if (!c) {
c09156bb 1812 register PERL_CONTEXT *cx;
8ec5e241 1813 SPAGAIN;
a0d0e21e
LW
1814 PUSHSUBST(cx);
1815 RETURNOP(cPMOP->op_pmreplroot);
1816 }
cf93c79d 1817 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
a0d0e21e
LW
1818 do {
1819 if (iters++ > maxiters)
cea2e8a9 1820 DIE(aTHX_ "Substitution loop");
d9f97599 1821 rxtainted |= RX_MATCH_TAINTED(rx);
cf93c79d 1822 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
a0d0e21e
LW
1823 m = s;
1824 s = orig;
cf93c79d 1825 orig = rx->subbeg;
a0d0e21e
LW
1826 s = orig + (m - s);
1827 strend = s + (strend - m);
1828 }
cf93c79d 1829 m = rx->startp[0] + orig;
a0d0e21e 1830 sv_catpvn(dstr, s, m-s);
cf93c79d 1831 s = rx->endp[0] + orig;
a0d0e21e
LW
1832 if (clen)
1833 sv_catpvn(dstr, c, clen);
1834 if (once)
1835 break;
cea2e8a9 1836 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m, TARG, NULL, r_flags));
a0d0e21e 1837 sv_catpvn(dstr, s, strend - s);
748a9306 1838
4633a7c4 1839 (void)SvOOK_off(TARG);
cb0b1708 1840 Safefree(SvPVX(TARG));
748a9306
LW
1841 SvPVX(TARG) = SvPVX(dstr);
1842 SvCUR_set(TARG, SvCUR(dstr));
1843 SvLEN_set(TARG, SvLEN(dstr));
1844 SvPVX(dstr) = 0;
1845 sv_free(dstr);
1846
48c036b1 1847 TAINT_IF(rxtainted & 1);
f878fbec 1848 SPAGAIN;
48c036b1
GS
1849 PUSHs(sv_2mortal(newSViv((I32)iters)));
1850
a0d0e21e 1851 (void)SvPOK_only(TARG);
48c036b1 1852 TAINT_IF(rxtainted);
a0d0e21e 1853 SvSETMAGIC(TARG);
9212bbba 1854 SvTAINT(TARG);
4633a7c4 1855 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1856 RETURN;
1857 }
5cd24f17 1858 goto ret_no;
a0d0e21e
LW
1859
1860nope:
8ec5e241
NIS
1861ret_no:
1862 SPAGAIN;
3280af22 1863 PUSHs(&PL_sv_no);
4633a7c4 1864 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1865 RETURN;
1866}
1867
1868PP(pp_grepwhile)
1869{
4e35701f 1870 djSP;
a0d0e21e
LW
1871
1872 if (SvTRUEx(POPs))
3280af22
NIS
1873 PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr];
1874 ++*PL_markstack_ptr;
a0d0e21e
LW
1875 LEAVE; /* exit inner scope */
1876
1877 /* All done yet? */
3280af22 1878 if (PL_stack_base + *PL_markstack_ptr > SP) {
a0d0e21e 1879 I32 items;
54310121 1880 I32 gimme = GIMME_V;
a0d0e21e
LW
1881
1882 LEAVE; /* exit outer scope */
1883 (void)POPMARK; /* pop src */
3280af22 1884 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
a0d0e21e 1885 (void)POPMARK; /* pop dst */
3280af22 1886 SP = PL_stack_base + POPMARK; /* pop original mark */
54310121 1887 if (gimme == G_SCALAR) {
a0d0e21e
LW
1888 dTARGET;
1889 XPUSHi(items);
a0d0e21e 1890 }
54310121
PP
1891 else if (gimme == G_ARRAY)
1892 SP += items;
a0d0e21e
LW
1893 RETURN;
1894 }
1895 else {
1896 SV *src;
1897
1898 ENTER; /* enter inner scope */
3280af22 1899 SAVESPTR(PL_curpm);
a0d0e21e 1900
3280af22 1901 src = PL_stack_base[*PL_markstack_ptr];
a0d0e21e 1902 SvTEMP_off(src);
54b9620d 1903 DEFSV = src;
a0d0e21e
LW
1904
1905 RETURNOP(cLOGOP->op_other);
1906 }
1907}
1908
1909PP(pp_leavesub)
1910{
4e35701f 1911 djSP;
a0d0e21e
LW
1912 SV **mark;
1913 SV **newsp;
1914 PMOP *newpm;
1915 I32 gimme;
c09156bb 1916 register PERL_CONTEXT *cx;
a0d0e21e
LW
1917
1918 POPBLOCK(cx,newpm);
f86702cc 1919
a1f49e72 1920 TAINT_NOT;
a0d0e21e
LW
1921 if (gimme == G_SCALAR) {
1922 MARK = newsp + 1;
a29cdaf0 1923 if (MARK <= SP) {
a8bba7fa 1924 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
a29cdaf0
IZ
1925 if (SvTEMP(TOPs)) {
1926 *MARK = SvREFCNT_inc(TOPs);
1927 FREETMPS;
1928 sv_2mortal(*MARK);
cd06dffe
GS
1929 }
1930 else {
a29cdaf0
IZ
1931 FREETMPS;
1932 *MARK = sv_mortalcopy(TOPs);
1933 }
cd06dffe
GS
1934 }
1935 else
a29cdaf0 1936 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
cd06dffe
GS
1937 }
1938 else {
f86702cc 1939 MEXTEND(MARK, 0);
3280af22 1940 *MARK = &PL_sv_undef;
a0d0e21e
LW
1941 }
1942 SP = MARK;
1943 }
54310121 1944 else if (gimme == G_ARRAY) {
f86702cc 1945 for (MARK = newsp + 1; MARK <= SP; MARK++) {
a1f49e72 1946 if (!SvTEMP(*MARK)) {
f86702cc 1947 *MARK = sv_mortalcopy(*MARK);
a1f49e72
CS
1948 TAINT_NOT; /* Each item is independent */
1949 }
f86702cc 1950 }
a0d0e21e 1951 }
f86702cc
PP
1952 PUTBACK;
1953
a8bba7fa 1954 POPSUB(cx); /* Stack values are safe: release CV and @_ ... */
3280af22 1955 PL_curpm = newpm; /* ... and pop $1 et al */
a0d0e21e
LW
1956
1957 LEAVE;
a0d0e21e
LW
1958 return pop_return();
1959}
1960
cd06dffe
GS
1961/* This duplicates the above code because the above code must not
1962 * get any slower by more conditions */
1963PP(pp_leavesublv)
1964{
1965 djSP;
1966 SV **mark;
1967 SV **newsp;
1968 PMOP *newpm;
1969 I32 gimme;
1970 register PERL_CONTEXT *cx;
cd06dffe
GS
1971
1972 POPBLOCK(cx,newpm);
cd06dffe
GS
1973
1974 TAINT_NOT;
1975
1976 if (cx->blk_sub.lval & OPpENTERSUB_INARGS) {
1977 /* We are an argument to a function or grep().
1978 * This kind of lvalueness was legal before lvalue
1979 * subroutines too, so be backward compatible:
1980 * cannot report errors. */
1981
1982 /* Scalar context *is* possible, on the LHS of -> only,
1983 * as in f()->meth(). But this is not an lvalue. */
1984 if (gimme == G_SCALAR)
1985 goto temporise;
1986 if (gimme == G_ARRAY) {
a8bba7fa 1987 if (!CvLVALUE(cx->blk_sub.cv))
cd06dffe
GS
1988 goto temporise_array;
1989 EXTEND_MORTAL(SP - newsp);
1990 for (mark = newsp + 1; mark <= SP; mark++) {
1991 if (SvTEMP(*mark))
1992 /* empty */ ;
1993 else if (SvFLAGS(*mark) & (SVs_PADTMP | SVf_READONLY))
1994 *mark = sv_mortalcopy(*mark);
1995 else {
1996 /* Can be a localized value subject to deletion. */
1997 PL_tmps_stack[++PL_tmps_ix] = *mark;
1998 SvREFCNT_inc(*mark);
1999 }
2000 }
2001 }
2002 }
2003 else if (cx->blk_sub.lval) { /* Leave it as it is if we can. */
2004 /* Here we go for robustness, not for speed, so we change all
2005 * the refcounts so the caller gets a live guy. Cannot set
2006 * TEMP, so sv_2mortal is out of question. */
a8bba7fa
GS
2007 if (!CvLVALUE(cx->blk_sub.cv)) {
2008 POPSUB(cx);
d470f89e
GS
2009 PL_curpm = newpm;
2010 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
2011 }
cd06dffe
GS
2012 if (gimme == G_SCALAR) {
2013 MARK = newsp + 1;
2014 EXTEND_MORTAL(1);
2015 if (MARK == SP) {
d470f89e 2016 if (SvFLAGS(TOPs) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) {
a8bba7fa 2017 POPSUB(cx);
d470f89e
GS
2018 PL_curpm = newpm;
2019 DIE(aTHX_ "Can't return a %s from lvalue subroutine",
cd06dffe 2020 SvREADONLY(TOPs) ? "readonly value" : "temporary");
d470f89e 2021 }
cd06dffe
GS
2022 else { /* Can be a localized value
2023 * subject to deletion. */
2024 PL_tmps_stack[++PL_tmps_ix] = *mark;
2025 SvREFCNT_inc(*mark);
2026 }
2027 }
d470f89e 2028 else { /* Should not happen? */
a8bba7fa 2029 POPSUB(cx);
d470f89e
GS
2030 PL_curpm = newpm;
2031 DIE(aTHX_ "%s returned from lvalue subroutine in scalar context",
cd06dffe 2032 (MARK > SP ? "Empty array" : "Array"));
d470f89e 2033 }
cd06dffe
GS
2034 SP = MARK;
2035 }
2036 else if (gimme == G_ARRAY) {
2037 EXTEND_MORTAL(SP - newsp);
2038 for (mark = newsp + 1; mark <= SP; mark++) {
d470f89e
GS
2039 if (SvFLAGS(*mark) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) {
2040 /* Might be flattened array after $#array = */
2041 PUTBACK;
a8bba7fa 2042 POPSUB(cx);
d470f89e
GS
2043 PL_curpm = newpm;
2044 DIE(aTHX_ "Can't return %s from lvalue subroutine",
cd06dffe
GS
2045 (*mark != &PL_sv_undef)
2046 ? (SvREADONLY(TOPs)
2047 ? "a readonly value" : "a temporary")
2048 : "an uninitialized value");
d470f89e 2049 }
cd06dffe
GS
2050 else {
2051 mortalize:
2052 /* Can be a localized value subject to deletion. */
2053 PL_tmps_stack[++PL_tmps_ix] = *mark;
2054 SvREFCNT_inc(*mark);
2055 }
2056 }
2057 }
2058 }
2059 else {
2060 if (gimme == G_SCALAR) {
2061 temporise:
2062 MARK = newsp + 1;
2063 if (MARK <= SP) {
a8bba7fa 2064 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
cd06dffe
GS
2065 if (SvTEMP(TOPs)) {
2066 *MARK = SvREFCNT_inc(TOPs);
2067 FREETMPS;
2068 sv_2mortal(*MARK);
2069 }
2070 else {
2071 FREETMPS;
2072 *MARK = sv_mortalcopy(TOPs);
2073 }
2074 }
2075 else
2076 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
2077 }
2078 else {
2079 MEXTEND(MARK, 0);
2080 *MARK = &PL_sv_undef;
2081 }
2082 SP = MARK;
2083 }
2084 else if (gimme == G_ARRAY) {
2085 temporise_array:
2086 for (MARK = newsp + 1; MARK <= SP; MARK++) {
2087 if (!SvTEMP(*MARK)) {
2088 *MARK = sv_mortalcopy(*MARK);
2089 TAINT_NOT; /* Each item is independent */
2090 }
2091 }
2092 }
2093 }
2094 PUTBACK;
2095
a8bba7fa 2096 POPSUB(cx); /* Stack values are safe: release CV and @_ ... */
cd06dffe
GS
2097 PL_curpm = newpm; /* ... and pop $1 et al */
2098
2099 LEAVE;
2100 return pop_return();
2101}
2102
2103
76e3520e 2104STATIC CV *
cea2e8a9 2105S_get_db_sub(pTHX_ SV **svp, CV *cv)
3de9ffa1
MB
2106{
2107 dTHR;
3280af22 2108 SV *dbsv = GvSV(PL_DBsub);
491527d0
GS
2109
2110 if (!PERLDB_SUB_NN) {
2111 GV *gv = CvGV(cv);
2112
2113 save_item(dbsv);
2114 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
2115 || strEQ(GvNAME(gv), "END")
2116 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
2117 !( (SvTYPE(*svp) == SVt_PVGV) && (GvCV((GV*)*svp) == cv)
2118 && (gv = (GV*)*svp) ))) {
2119 /* Use GV from the stack as a fallback. */
2120 /* GV is potentially non-unique, or contain different CV. */
2121 sv_setsv(dbsv, newRV((SV*)cv));
2122 }
2123 else {
2124 gv_efullname3(dbsv, gv, Nullch);
2125 }
3de9ffa1
MB
2126 }
2127 else {
491527d0
GS
2128 SvUPGRADE(dbsv, SVt_PVIV);
2129 SvIOK_on(dbsv);
2130 SAVEIV(SvIVX(dbsv));
56431972 2131 SvIVX(dbsv) = PTR2IV(cv); /* Do it the quickest way */
3de9ffa1 2132 }
491527d0 2133
3de9ffa1 2134 if (CvXSUB(cv))
3280af22
NIS
2135 PL_curcopdb = PL_curcop;
2136 cv = GvCV(PL_DBsub);
3de9ffa1
MB
2137 return cv;
2138}
2139
a0d0e21e
LW
2140PP(pp_entersub)
2141{
4e35701f 2142 djSP; dPOPss;
a0d0e21e
LW
2143 GV *gv;
2144 HV *stash;
2145 register CV *cv;
c09156bb 2146 register PERL_CONTEXT *cx;
5d94fbed 2147 I32 gimme;
533c011a 2148 bool hasargs = (PL_op->op_flags & OPf_STACKED) != 0;
a0d0e21e
LW
2149
2150 if (!sv)
cea2e8a9 2151 DIE(aTHX_ "Not a CODE reference");
a0d0e21e
LW
2152 switch (SvTYPE(sv)) {
2153 default:
2154 if (!SvROK(sv)) {
748a9306 2155 char *sym;
2d8e6c8d 2156 STRLEN n_a;
748a9306 2157
3280af22 2158 if (sv == &PL_sv_yes) { /* unfound import, ignore */
fb73857a 2159 if (hasargs)
3280af22 2160 SP = PL_stack_base + POPMARK;
a0d0e21e 2161 RETURN;
fb73857a 2162 }
15ff848f
CS
2163 if (SvGMAGICAL(sv)) {
2164 mg_get(sv);
2165 sym = SvPOKp(sv) ? SvPVX(sv) : Nullch;
2166 }
2167 else
2d8e6c8d 2168 sym = SvPV(sv, n_a);
15ff848f 2169 if (!sym)
cea2e8a9 2170 DIE(aTHX_ PL_no_usym, "a subroutine");
533c011a 2171 if (PL_op->op_private & HINT_STRICT_REFS)
cea2e8a9 2172 DIE(aTHX_ PL_no_symref, sym, "a subroutine");
864dbfa3 2173 cv = get_cv(sym, TRUE);
a0d0e21e
LW
2174 break;
2175 }
f5284f61
IZ
2176 {
2177 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
2178 tryAMAGICunDEREF(to_cv);
2179 }
a0d0e21e
LW
2180 cv = (CV*)SvRV(sv);
2181 if (SvTYPE(cv) == SVt_PVCV)
2182 break;
2183 /* FALL THROUGH */
2184 case SVt_PVHV:
2185 case SVt_PVAV:
cea2e8a9 2186 DIE(aTHX_ "Not a CODE reference");
a0d0e21e
LW
2187 case SVt_PVCV:
2188 cv = (CV*)sv;
2189 break;
2190 case SVt_PVGV:
8ebc5c01 2191 if (!(cv = GvCVu((GV*)sv)))
f6ec51f7
GS
2192 cv = sv_2cv(sv, &stash, &gv, FALSE);
2193 if (!cv) {
2194 ENTER;
2195 SAVETMPS;
2196 goto try_autoload;
2197 }
2198 break;
a0d0e21e
LW
2199 }
2200
2201 ENTER;
2202 SAVETMPS;
2203
2204 retry:
a0d0e21e 2205 if (!CvROOT(cv) && !CvXSUB(cv)) {
44a8e56a 2206 GV* autogv;
22239a37 2207 SV* sub_name;
44a8e56a
PP
2208
2209 /* anonymous or undef'd function leaves us no recourse */
2210 if (CvANON(cv) || !(gv = CvGV(cv)))
cea2e8a9 2211 DIE(aTHX_ "Undefined subroutine called");
67caa1fe 2212
44a8e56a
PP
2213 /* autoloaded stub? */
2214 if (cv != GvCV(gv)) {
2215 cv = GvCV(gv);
a0d0e21e 2216 }
44a8e56a 2217 /* should call AUTOLOAD now? */
67caa1fe 2218 else {
f6ec51f7
GS
2219try_autoload:
2220 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
2221 FALSE)))
2222 {
2223 cv = GvCV(autogv);
2224 }
2225 /* sorry */
2226 else {
2227 sub_name = sv_newmortal();
2228 gv_efullname3(sub_name, gv, Nullch);
cea2e8a9 2229 DIE(aTHX_ "Undefined subroutine &%s called", SvPVX(sub_name));
f6ec51f7 2230 }
67caa1fe
GS
2231 }
2232 if (!cv)
cea2e8a9 2233 DIE(aTHX_ "Not a CODE reference");
67caa1fe 2234 goto retry;
a0d0e21e
LW
2235 }
2236
54310121 2237 gimme = GIMME_V;
67caa1fe 2238 if ((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub) && !CvNODEBUG(cv)) {
4f01c5a5 2239 cv = get_db_sub(&sv, cv);
67caa1fe 2240 if (!cv)
cea2e8a9 2241 DIE(aTHX_ "No DBsub routine");
67caa1fe 2242 }
a0d0e21e 2243
11343788 2244#ifdef USE_THREADS
3de9ffa1
MB
2245 /*
2246 * First we need to check if the sub or method requires locking.
458fb581
MB
2247 * If so, we gain a lock on the CV, the first argument or the
2248 * stash (for static methods), as appropriate. This has to be
2249 * inline because for FAKE_THREADS, COND_WAIT inlines code to
2250 * reschedule by returning a new op.
3de9ffa1 2251 */
11343788 2252 MUTEX_LOCK(CvMUTEXP(cv));
77a005ab
MB
2253 if (CvFLAGS(cv) & CVf_LOCKED) {
2254 MAGIC *mg;
2255 if (CvFLAGS(cv) & CVf_METHOD) {
533c011a
NIS
2256 if (SP > PL_stack_base + TOPMARK)
2257 sv = *(PL_stack_base + TOPMARK + 1);
77a005ab 2258 else {
13e08037
GS
2259 AV *av = (AV*)PL_curpad[0];
2260 if (hasargs || !av || AvFILLp(av) < 0
2261 || !(sv = AvARRAY(av)[0]))
2262 {
2263 MUTEX_UNLOCK(CvMUTEXP(cv));
d470f89e 2264 DIE(aTHX_ "no argument for locked method call");
13e08037 2265 }
77a005ab
MB
2266 }
2267 if (SvROK(sv))
2268 sv = SvRV(sv);
458fb581
MB
2269 else {
2270 STRLEN len;
2271 char *stashname = SvPV(sv, len);
2272 sv = (SV*)gv_stashpvn(stashname, len, TRUE);
2273 }
77a005ab
MB
2274 }
2275 else {
2276 sv = (SV*)cv;
2277 }
2278 MUTEX_UNLOCK(CvMUTEXP(cv));
2279 mg = condpair_magic(sv);
2280 MUTEX_LOCK(MgMUTEXP(mg));
2281 if (MgOWNER(mg) == thr)
2282 MUTEX_UNLOCK(MgMUTEXP(mg));
2283 else {
2284 while (MgOWNER(mg))
2285 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
2286 MgOWNER(mg) = thr;
bf49b057 2287 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: pp_entersub lock %p\n",
1fd28e87 2288 thr, sv);)
77a005ab 2289 MUTEX_UNLOCK(MgMUTEXP(mg));
51371543 2290 SAVEDESTRUCTOR(Perl_unlock_condpair, sv);
11343788 2291 }
77a005ab 2292 MUTEX_LOCK(CvMUTEXP(cv));
11343788 2293 }
3de9ffa1
MB
2294 /*
2295 * Now we have permission to enter the sub, we must distinguish
2296 * four cases. (0) It's an XSUB (in which case we don't care
2297 * about ownership); (1) it's ours already (and we're recursing);
2298 * (2) it's free (but we may already be using a cached clone);
2299 * (3) another thread owns it. Case (1) is easy: we just use it.
2300 * Case (2) means we look for a clone--if we have one, use it
2301 * otherwise grab ownership of cv. Case (3) means we look for a
2302 * clone (for non-XSUBs) and have to create one if we don't
2303 * already have one.
2304 * Why look for a clone in case (2) when we could just grab
2305 * ownership of cv straight away? Well, we could be recursing,
2306 * i.e. we originally tried to enter cv while another thread
2307 * owned it (hence we used a clone) but it has been freed up
2308 * and we're now recursing into it. It may or may not be "better"
2309 * to use the clone but at least CvDEPTH can be trusted.
2310 */
2311 if (CvOWNER(cv) == thr || CvXSUB(cv))
2312 MUTEX_UNLOCK(CvMUTEXP(cv));
11343788 2313 else {
3de9ffa1
MB
2314 /* Case (2) or (3) */
2315 SV **svp;
2316
11343788 2317 /*
3de9ffa1
MB
2318 * XXX Might it be better to release CvMUTEXP(cv) while we
2319 * do the hv_fetch? We might find someone has pinched it
2320 * when we look again, in which case we would be in case
2321 * (3) instead of (2) so we'd have to clone. Would the fact
2322 * that we released the mutex more quickly make up for this?
2323 */
b099ddc0 2324 if ((svp = hv_fetch(thr->cvcache, (char *)cv, sizeof(cv), FALSE)))
6ee623d5 2325 {
3de9ffa1 2326 /* We already have a clone to use */
11343788 2327 MUTEX_UNLOCK(CvMUTEXP(cv));
3de9ffa1 2328 cv = *(CV**)svp;
bf49b057 2329 DEBUG_S(PerlIO_printf(Perl_debug_log,
1fd28e87
MB
2330 "entersub: %p already has clone %p:%s\n",
2331 thr, cv, SvPEEK((SV*)cv)));
3de9ffa1
MB
2332 CvOWNER(cv) = thr;
2333 SvREFCNT_inc(cv);
2334 if (CvDEPTH(cv) == 0)
51371543 2335 SAVEDESTRUCTOR(unset_cvowner, (void*) cv);
3de9ffa1 2336 }
11343788 2337 else {
3de9ffa1
MB
2338 /* (2) => grab ownership of cv. (3) => make clone */
2339 if (!CvOWNER(cv)) {
2340 CvOWNER(cv) = thr;
2341 SvREFCNT_inc(cv);
11343788 2342 MUTEX_UNLOCK(CvMUTEXP(cv));
bf49b057 2343 DEBUG_S(PerlIO_printf(Perl_debug_log,
1fd28e87
MB
2344 "entersub: %p grabbing %p:%s in stash %s\n",
2345 thr, cv, SvPEEK((SV*)cv), CvSTASH(cv) ?
3de9ffa1 2346 HvNAME(CvSTASH(cv)) : "(none)"));
cd06dffe
GS
2347 }
2348 else {
3de9ffa1
MB
2349 /* Make a new clone. */
2350 CV *clonecv;
2351 SvREFCNT_inc(cv); /* don't let it vanish from under us */
2352 MUTEX_UNLOCK(CvMUTEXP(cv));
bf49b057 2353 DEBUG_S((PerlIO_printf(Perl_debug_log,
1fd28e87
MB
2354 "entersub: %p cloning %p:%s\n",
2355 thr, cv, SvPEEK((SV*)cv))));
3de9ffa1
MB
2356 /*
2357 * We're creating a new clone so there's no race
2358 * between the original MUTEX_UNLOCK and the
2359 * SvREFCNT_inc since no one will be trying to undef
2360 * it out from underneath us. At least, I don't think
2361 * there's a race...
2362 */
2363 clonecv = cv_clone(cv);
2364 SvREFCNT_dec(cv); /* finished with this */
199100c8 2365 hv_store(thr->cvcache, (char*)cv, sizeof(cv), (SV*)clonecv,0);
3de9ffa1
MB
2366 CvOWNER(clonecv) = thr;
2367 cv = clonecv;
11343788 2368 SvREFCNT_inc(cv);
11343788 2369 }
8b73bbec 2370 DEBUG_S(if (CvDEPTH(cv) != 0)
bf49b057 2371 PerlIO_printf(Perl_debug_log, "depth %ld != 0\n",
3de9ffa1 2372 CvDEPTH(cv)););
51371543 2373 SAVEDESTRUCTOR(unset_cvowner, (void*) cv);
11343788 2374 }
3de9ffa1 2375 }
11343788
MB
2376#endif /* USE_THREADS */
2377
a0d0e21e 2378 if (CvXSUB(cv)) {
67caa1fe 2379#ifdef PERL_XSUB_OLDSTYLE
a0d0e21e 2380 if (CvOLDSTYLE(cv)) {
20ce7b12 2381 I32 (*fp3)(int,int,int);
a0d0e21e
LW
2382 dMARK;
2383 register I32 items = SP - MARK;
67955e0c 2384 /* We dont worry to copy from @_. */
924508f0
GS
2385 while (SP > mark) {
2386 SP[1] = SP[0];
2387 SP--;
a0d0e21e 2388 }
3280af22 2389 PL_stack_sp = mark + 1;
20ce7b12 2390 fp3 = (I32(*)(int,int,int)))CvXSUB(cv;
ecfc5424 2391 items = (*fp3)(CvXSUBANY(cv).any_i32,
3280af22 2392 MARK - PL_stack_base + 1,
ecfc5424 2393 items);
3280af22 2394 PL_stack_sp = PL_stack_base + items;
a0d0e21e 2395 }
67caa1fe
GS
2396 else
2397#endif /* PERL_XSUB_OLDSTYLE */
2398 {
748a9306
LW
2399 I32 markix = TOPMARK;
2400
a0d0e21e 2401 PUTBACK;
67955e0c
PP
2402
2403 if (!hasargs) {
2404 /* Need to copy @_ to stack. Alternative may be to
2405 * switch stack to @_, and copy return values
2406 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
6d4ff0d2
MB
2407 AV* av;
2408 I32 items;
2409#ifdef USE_THREADS
533c011a 2410 av = (AV*)PL_curpad[0];
6d4ff0d2 2411#else
3280af22 2412 av = GvAV(PL_defgv);
6d4ff0d2 2413#endif /* USE_THREADS */
93965878 2414 items = AvFILLp(av) + 1; /* @_ is not tieable */
67955e0c
PP
2415
2416 if (items) {
2417 /* Mark is at the end of the stack. */
924508f0
GS
2418 EXTEND(SP, items);
2419 Copy(AvARRAY(av), SP + 1, items, SV*);
2420 SP += items;
67955e0c
PP
2421 PUTBACK ;
2422 }
2423 }
67caa1fe
GS
2424 /* We assume first XSUB in &DB::sub is the called one. */
2425 if (PL_curcopdb) {
3280af22
NIS
2426 SAVESPTR(PL_curcop);
2427 PL_curcop = PL_curcopdb;
2428 PL_curcopdb = NULL;
67955e0c
PP
2429 }
2430 /* Do we need to open block here? XXXX */
0cb96387 2431 (void)(*CvXSUB(cv))(aTHXo_ cv);
748a9306
LW
2432
2433 /* Enforce some sanity in scalar context. */
3280af22
NIS
2434 if (gimme == G_SCALAR && ++markix != PL_stack_sp - PL_stack_base ) {
2435 if (markix > PL_stack_sp - PL_stack_base)
2436 *(PL_stack_base + markix) = &PL_sv_undef;
748a9306 2437 else
3280af22
NIS
2438 *(PL_stack_base + markix) = *PL_stack_sp;
2439 PL_stack_sp = PL_stack_base + markix;
748a9306 2440 }
a0d0e21e
LW
2441 }
2442 LEAVE;
2443 return NORMAL;
2444 }
2445 else {
2446 dMARK;
2447 register I32 items = SP - MARK;
a0d0e21e
LW
2448 AV* padlist = CvPADLIST(cv);
2449 SV** svp = AvARRAY(padlist);
533c011a 2450 push_return(PL_op->op_next);
a0d0e21e
LW
2451 PUSHBLOCK(cx, CXt_SUB, MARK);
2452 PUSHSUB(cx);
2453 CvDEPTH(cv)++;
6b35e009
GS
2454 /* XXX This would be a natural place to set C<PL_compcv = cv> so
2455 * that eval'' ops within this sub know the correct lexical space.
2456 * Owing the speed considerations, we choose to search for the cv
2457 * in doeval() instead.
2458 */
a0d0e21e
LW
2459 if (CvDEPTH(cv) < 2)
2460 (void)SvREFCNT_inc(cv);
2461 else { /* save temporaries on recursion? */
93965878 2462 if (CvDEPTH(cv) > AvFILLp(padlist)) {
a0d0e21e
LW
2463 AV *av;
2464 AV *newpad = newAV();
4aa0a1f7 2465 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
93965878 2466 I32 ix = AvFILLp((AV*)svp[1]);
a0d0e21e 2467 svp = AvARRAY(svp[0]);
748a9306 2468 for ( ;ix > 0; ix--) {
3280af22 2469 if (svp[ix] != &PL_sv_undef) {
748a9306 2470 char *name = SvPVX(svp[ix]);
5f05dabc
PP
2471 if ((SvFLAGS(svp[ix]) & SVf_FAKE) /* outer lexical? */
2472 || *name == '&') /* anonymous code? */
2473 {
2474 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix]));
748a9306
LW
2475 }
2476 else { /* our own lexical */
2477 if (*name == '@')
2478 av_store(newpad, ix, sv = (SV*)newAV());
2479 else if (*name == '%')
2480 av_store(newpad, ix, sv = (SV*)newHV());
2481 else
2482 av_store(newpad, ix, sv = NEWSV(0,0));
2483 SvPADMY_on(sv);
2484 }
a0d0e21e
LW
2485 }
2486 else {
748a9306 2487 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
2488 SvPADTMP_on(sv);
2489 }
2490 }
2491 av = newAV(); /* will be @_ */
2492 av_extend(av, 0);
2493 av_store(newpad, 0, (SV*)av);
2494 AvFLAGS(av) = AVf_REIFY;
2495 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
93965878 2496 AvFILLp(padlist) = CvDEPTH(cv);
a0d0e21e
LW
2497 svp = AvARRAY(padlist);
2498 }
2499 }
6d4ff0d2
MB
2500#ifdef USE_THREADS
2501 if (!hasargs) {
533c011a 2502 AV* av = (AV*)PL_curpad[0];
6d4ff0d2 2503
93965878 2504 items = AvFILLp(av) + 1;
6d4ff0d2
MB
2505 if (items) {
2506 /* Mark is at the end of the stack. */
924508f0
GS
2507 EXTEND(SP, items);
2508 Copy(AvARRAY(av), SP + 1, items, SV*);
2509 SP += items;
6d4ff0d2
MB
2510 PUTBACK ;
2511 }
2512 }
2513#endif /* USE_THREADS */
3280af22
NIS
2514 SAVESPTR(PL_curpad);
2515 PL_curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
6d4ff0d2
MB
2516#ifndef USE_THREADS
2517 if (hasargs)
2518#endif /* USE_THREADS */
2519 {
2520 AV* av;
a0d0e21e
LW
2521 SV** ary;
2522
77a005ab 2523#if 0
bf49b057 2524 DEBUG_S(PerlIO_printf(Perl_debug_log,
0f15f207 2525 "%p entersub preparing @_\n", thr));
77a005ab 2526#endif
3280af22 2527 av = (AV*)PL_curpad[0];
d8b46c1b 2528 assert(!AvREAL(av));
6d4ff0d2 2529#ifndef USE_THREADS
3280af22
NIS
2530 cx->blk_sub.savearray = GvAV(PL_defgv);
2531 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
6d4ff0d2
MB
2532#endif /* USE_THREADS */
2533 cx->blk_sub.argarray = av;
a0d0e21e
LW
2534 ++MARK;
2535
2536 if (items > AvMAX(av) + 1) {
2537 ary = AvALLOC(av);
2538 if (AvARRAY(av) != ary) {
2539 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2540 SvPVX(av) = (char*)ary;
2541 }
2542 if (items > AvMAX(av) + 1) {
2543 AvMAX(av) = items - 1;
2544 Renew(ary,items,SV*);
2545 AvALLOC(av) = ary;
2546 SvPVX(av) = (char*)ary;
2547 }
2548 }
2549 Copy(MARK,AvARRAY(av),items,SV*);
93965878 2550 AvFILLp(av) = items - 1;
a0d0e21e
LW
2551
2552 while (items--) {
2553 if (*MARK)
2554 SvTEMP_off(*MARK);
2555 MARK++;
2556 }
2557 }
4a925ff6
GS
2558 /* warning must come *after* we fully set up the context
2559 * stuff so that __WARN__ handlers can safely dounwind()
2560 * if they want to
2561 */
2562 if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION)
2563 && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))
2564 sub_crush_depth(cv);
77a005ab 2565#if 0
bf49b057 2566 DEBUG_S(PerlIO_printf(Perl_debug_log,
0f15f207 2567 "%p entersub returning %p\n", thr, CvSTART(cv)));
77a005ab 2568#endif
a0d0e21e
LW
2569 RETURNOP(CvSTART(cv));
2570 }
2571}
2572
44a8e56a 2573void
864dbfa3 2574Perl_sub_crush_depth(pTHX_ CV *cv)
44a8e56a
PP
2575{
2576 if (CvANON(cv))
cea2e8a9 2577 Perl_warner(aTHX_ WARN_RECURSION, "Deep recursion on anonymous subroutine");
44a8e56a
PP
2578 else {
2579 SV* tmpstr = sv_newmortal();
2580 gv_efullname3(tmpstr, CvGV(cv), Nullch);
cea2e8a9 2581 Perl_warner(aTHX_ WARN_RECURSION, "Deep recursion on subroutine \"%s\"",
599cee73 2582 SvPVX(tmpstr));
44a8e56a
PP
2583 }
2584}
2585
a0d0e21e
LW
2586PP(pp_aelem)
2587{
4e35701f 2588 djSP;
a0d0e21e 2589 SV** svp;
748a9306 2590 I32 elem = POPi;
68dc0745 2591 AV* av = (AV*)POPs;
533c011a
NIS
2592 U32 lval = PL_op->op_flags & OPf_MOD;
2593 U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > AvFILL(av));
be6c24e0 2594 SV *sv;
a0d0e21e 2595
748a9306 2596 if (elem > 0)
3280af22 2597 elem -= PL_curcop->cop_arybase;
a0d0e21e
LW
2598 if (SvTYPE(av) != SVt_PVAV)
2599 RETPUSHUNDEF;
68dc0745 2600 svp = av_fetch(av, elem, lval && !defer);
a0d0e21e 2601 if (lval) {
3280af22 2602 if (!svp || *svp == &PL_sv_undef) {
68dc0745
PP
2603 SV* lv;
2604 if (!defer)
cea2e8a9 2605 DIE(aTHX_ PL_no_aelem, elem);
68dc0745
PP
2606 lv = sv_newmortal();
2607 sv_upgrade(lv, SVt_PVLV);
2608 LvTYPE(lv) = 'y';
2609 sv_magic(lv, Nullsv, 'y', Nullch, 0);
2610 LvTARG(lv) = SvREFCNT_inc(av);
2611 LvTARGOFF(lv) = elem;
2612 LvTARGLEN(lv) = 1;
2613 PUSHs(lv);
2614 RETURN;
2615 }
533c011a 2616 if (PL_op->op_private & OPpLVAL_INTRO)
161b7d16 2617 save_aelem(av, elem, svp);
533c011a
NIS
2618 else if (PL_op->op_private & OPpDEREF)
2619 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
a0d0e21e 2620 }
3280af22 2621 sv = (svp ? *svp : &PL_sv_undef);
be6c24e0
GS
2622 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
2623 sv = sv_mortalcopy(sv);
2624 PUSHs(sv);
a0d0e21e
LW
2625 RETURN;
2626}
2627
02a9e968 2628void
864dbfa3 2629Perl_vivify_ref(pTHX_ SV *sv, U32 to_what)
02a9e968
CS
2630{
2631 if (SvGMAGICAL(sv))
2632 mg_get(sv);
2633 if (!SvOK(sv)) {
2634 if (SvREADONLY(sv))
cea2e8a9 2635 Perl_croak(aTHX_ PL_no_modify);
5f05dabc
PP
2636 if (SvTYPE(sv) < SVt_RV)
2637 sv_upgrade(sv, SVt_RV);
2638 else if (SvTYPE(sv) >= SVt_PV) {
2639 (void)SvOOK_off(sv);
2640 Safefree(SvPVX(sv));
2641 SvLEN(sv) = SvCUR(sv) = 0;
2642 }
68dc0745 2643 switch (to_what) {
5f05dabc 2644 case OPpDEREF_SV:
8c52afec 2645 SvRV(sv) = NEWSV(355,0);
5f05dabc
PP
2646 break;
2647 case OPpDEREF_AV:
2648 SvRV(sv) = (SV*)newAV();
2649 break;
2650 case OPpDEREF_HV:
2651 SvRV(sv) = (SV*)newHV();
2652 break;
2653 }
02a9e968
CS
2654 SvROK_on(sv);
2655 SvSETMAGIC(sv);
2656 }
2657}
2658
a0d0e21e
LW
2659PP(pp_method)
2660{
4e35701f 2661 djSP;
f5d5a27c
CS
2662 SV* sv = TOPs;
2663
2664 if (SvROK(sv)) {
eda383f2 2665 SV* rsv = SvRV(sv);
f5d5a27c
CS
2666 if (SvTYPE(rsv) == SVt_PVCV) {
2667 SETs(rsv);
2668 RETURN;
2669 }
2670 }
2671
2672 SETs(method_common(sv, Null(U32*)));
2673 RETURN;
2674}
2675
2676PP(pp_method_named)
2677{
2678 djSP;
2679 SV* sv = cSVOP->op_sv;
2680 U32 hash = SvUVX(sv);
2681
2682 XPUSHs(method_common(sv, &hash));
2683 RETURN;
2684}
2685
2686STATIC SV *
2687S_method_common(pTHX_ SV* meth, U32* hashp)
2688{
2689 djSP;
a0d0e21e
LW
2690 SV* sv;
2691 SV* ob;
2692 GV* gv;
56304f61
CS
2693 HV* stash;
2694 char* name;
f5d5a27c 2695 STRLEN namelen;
ac91690f
CS
2696 char* packname;
2697 STRLEN packlen;
a0d0e21e 2698
f5d5a27c 2699 name = SvPV(meth, namelen);
3280af22 2700 sv = *(PL_stack_base + TOPMARK + 1);
f5d5a27c 2701
16d20bd9
AD
2702 if (SvGMAGICAL(sv))
2703 mg_get(sv);
a0d0e21e 2704 if (SvROK(sv))
16d20bd9 2705 ob = (SV*)SvRV(sv);
a0d0e21e
LW
2706 else {
2707 GV* iogv;
a0d0e21e 2708
56304f61 2709 packname = Nullch;
a0d0e21e 2710 if (!SvOK(sv) ||
56304f61 2711 !(packname = SvPV(sv, packlen)) ||
a0d0e21e
LW
2712 !(iogv = gv_fetchpv(packname, FALSE, SVt_PVIO)) ||
2713 !(ob=(SV*)GvIO(iogv)))
2714 {
834a4ddd
LW
2715 if (!packname ||
2716 ((*(U8*)packname >= 0xc0 && IN_UTF8)
b86a2fa7 2717 ? !isIDFIRST_utf8((U8*)packname)
834a4ddd
LW
2718 : !isIDFIRST(*packname)
2719 ))
2720 {
f5d5a27c
CS
2721 Perl_croak(aTHX_ "Can't call method \"%s\" %s", name,
2722 SvOK(sv) ? "without a package or object reference"
2723 : "on an undefined value");
834a4ddd 2724 }
56304f61 2725 stash = gv_stashpvn(packname, packlen, TRUE);
ac91690f 2726 goto fetch;
a0d0e21e 2727 }
3280af22 2728 *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
a0d0e21e
LW
2729 }
2730
ac91690f 2731 if (!ob || !SvOBJECT(ob))
f5d5a27c
CS
2732 Perl_croak(aTHX_ "Can't call method \"%s\" on unblessed reference",
2733 name);
a0d0e21e 2734
56304f61 2735 stash = SvSTASH(ob);
a0d0e21e 2736
ac91690f 2737 fetch:
f5d5a27c
CS
2738 /* shortcut for simple names */
2739 if (hashp) {
2740 HE* he = hv_fetch_ent(stash, meth, 0, *hashp);
2741 if (he) {
2742 gv = (GV*)HeVAL(he);
2743 if (isGV(gv) && GvCV(gv) &&
2744 (!GvCVGEN(gv) || GvCVGEN(gv) == PL_sub_generation))
2745 return (SV*)GvCV(gv);
2746 }
2747 }
2748
ac91690f 2749 gv = gv_fetchmethod(stash, name);
56304f61
CS
2750 if (!gv) {
2751 char* leaf = name;
2752 char* sep = Nullch;
2753 char* p;
2754
2755 for (p = name; *p; p++) {
2756 if (*p == '\'')
2757 sep = p, leaf = p + 1;
2758 else if (*p == ':' && *(p + 1) == ':')
2759 sep = p, leaf = p + 2;
2760 }
2761 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
3280af22 2762 packname = HvNAME(sep ? PL_curcop->cop_stash : stash);
56304f61
CS
2763 packlen = strlen(packname);
2764 }
2765 else {
2766 packname = name;
2767 packlen = sep - name;
2768 }
f5d5a27c
CS
2769 Perl_croak(aTHX_
2770 "Can't locate object method \"%s\" via package \"%s\"",
2771 leaf, packname);
56304f61 2772 }
f5d5a27c 2773 return isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv;
a0d0e21e 2774}
22239a37 2775
51371543
GS
2776#ifdef USE_THREADS
2777static void
2778unset_cvowner(pTHXo_ void *cvarg)
2779{
2780 register CV* cv = (CV *) cvarg;
2781#ifdef DEBUGGING
2782 dTHR;
2783#endif /* DEBUGGING */
2784
bf49b057 2785 DEBUG_S((PerlIO_printf(Perl_debug_log, "%p unsetting CvOWNER of %p:%s\n",
51371543
GS
2786 thr, cv, SvPEEK((SV*)cv))));
2787 MUTEX_LOCK(CvMUTEXP(cv));
2788 DEBUG_S(if (CvDEPTH(cv) != 0)
bf49b057 2789 PerlIO_printf(Perl_debug_log, "depth %ld != 0\n",
51371543
GS
2790 CvDEPTH(cv)););
2791 assert(thr == CvOWNER(cv));
2792 CvOWNER(cv) = 0;
2793 MUTEX_UNLOCK(CvMUTEXP(cv));
2794 SvREFCNT_dec(cv);
2795}
2796#endif /* USE_THREADS */