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