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