This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix example #4 in perlXStut
[perl5.git] / pp_hot.c
CommitLineData
a0d0e21e
LW
1/* pp_hot.c
2 *
3 * Copyright (c) 1991-1994, Larry Wall
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"
19#include "perl.h"
20
21/* Hot code. */
22
23PP(pp_const)
24{
25 dSP;
26 XPUSHs(cSVOP->op_sv);
27 RETURN;
28}
29
30PP(pp_nextstate)
31{
32 curcop = (COP*)op;
33 TAINT_NOT; /* Each statement is presumed innocent */
34 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
35 FREETMPS;
36 return NORMAL;
37}
38
39PP(pp_gvsv)
40{
41 dSP;
42 EXTEND(sp,1);
43 if (op->op_private & OPpLVAL_INTRO)
44 PUSHs(save_scalar(cGVOP->op_gv));
45 else
46 PUSHs(GvSV(cGVOP->op_gv));
47 RETURN;
48}
49
50PP(pp_null)
51{
52 return NORMAL;
53}
54
55PP(pp_pushmark)
56{
57 PUSHMARK(stack_sp);
58 return NORMAL;
59}
60
61PP(pp_stringify)
62{
63 dSP; dTARGET;
64 STRLEN len;
65 char *s;
66 s = SvPV(TOPs,len);
67 sv_setpvn(TARG,s,len);
68 SETTARG;
69 RETURN;
70}
71
72PP(pp_gv)
73{
74 dSP;
75 XPUSHs((SV*)cGVOP->op_gv);
76 RETURN;
77}
78
c07a80fd
PP
79PP(pp_gelem)
80{
81 GV *gv;
82 SV *sv;
83 SV *ref;
84 char *elem;
85 dSP;
86
87 sv = POPs;
88 elem = SvPV(sv, na);
89 gv = (GV*)POPs;
90 ref = Nullsv;
91 sv = Nullsv;
92 switch (elem ? *elem : '\0')
93 {
94 case 'A':
95 if (strEQ(elem, "ARRAY"))
96 ref = (SV*)GvAV(gv);
97 break;
98 case 'C':
99 if (strEQ(elem, "CODE"))
8ebc5c01 100 ref = (SV*)GvCVu(gv);
c07a80fd
PP
101 break;
102 case 'F':
36477c24 103 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
c07a80fd
PP
104 ref = (SV*)GvIOp(gv);
105 break;
106 case 'G':
107 if (strEQ(elem, "GLOB"))
108 ref = (SV*)gv;
109 break;
110 case 'H':
111 if (strEQ(elem, "HASH"))
112 ref = (SV*)GvHV(gv);
113 break;
36477c24
PP
114 case 'I':
115 if (strEQ(elem, "IO"))
116 ref = (SV*)GvIOp(gv);
117 break;
c07a80fd
PP
118 case 'N':
119 if (strEQ(elem, "NAME"))
120 sv = newSVpv(GvNAME(gv), GvNAMELEN(gv));
121 break;
122 case 'P':
123 if (strEQ(elem, "PACKAGE"))
124 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
125 break;
126 case 'S':
127 if (strEQ(elem, "SCALAR"))
128 ref = GvSV(gv);
129 break;
130 }
131 if (ref)
132 sv = newRV(ref);
133 if (sv)
134 sv_2mortal(sv);
135 else
136 sv = &sv_undef;
137 XPUSHs(sv);
138 RETURN;
139}
140
a0d0e21e
LW
141PP(pp_and)
142{
143 dSP;
144 if (!SvTRUE(TOPs))
145 RETURN;
146 else {
147 --SP;
148 RETURNOP(cLOGOP->op_other);
149 }
150}
151
152PP(pp_sassign)
153{
154 dSP; dPOPTOPssrl;
748a9306
LW
155 MAGIC *mg;
156
a0d0e21e
LW
157 if (op->op_private & OPpASSIGN_BACKWARDS) {
158 SV *temp;
159 temp = left; left = right; right = temp;
160 }
bbce6d69 161 if (tainting && tainted && !SvTAINTED(left))
a0d0e21e 162 TAINT_NOT;
a0d0e21e
LW
163 SvSetSV(right, left);
164 SvSETMAGIC(right);
165 SETs(right);
166 RETURN;
167}
168
169PP(pp_cond_expr)
170{
171 dSP;
172 if (SvTRUEx(POPs))
173 RETURNOP(cCONDOP->op_true);
174 else
175 RETURNOP(cCONDOP->op_false);
176}
177
178PP(pp_unstack)
179{
180 I32 oldsave;
181 TAINT_NOT; /* Each statement is presumed innocent */
182 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
183 FREETMPS;
184 oldsave = scopestack[scopestack_ix - 1];
185 LEAVE_SCOPE(oldsave);
186 return NORMAL;
187}
188
a0d0e21e
LW
189PP(pp_concat)
190{
748a9306
LW
191 dSP; dATARGET; tryAMAGICbin(concat,opASSIGN);
192 {
193 dPOPTOPssrl;
a0d0e21e
LW
194 STRLEN len;
195 char *s;
196 if (TARG != left) {
197 s = SvPV(left,len);
198 sv_setpvn(TARG,s,len);
199 }
c07a80fd
PP
200 else if (SvGMAGICAL(TARG))
201 mg_get(TARG);
7a4c00b4 202 else if (!SvOK(TARG) && SvTYPE(TARG) <= SVt_PVMG) {
748a9306 203 sv_setpv(TARG, ""); /* Suppress warning. */
7a4c00b4 204 s = SvPV_force(TARG, len);
4633a7c4 205 }
a0d0e21e
LW
206 s = SvPV(right,len);
207 sv_catpvn(TARG,s,len);
208 SETTARG;
209 RETURN;
748a9306 210 }
a0d0e21e
LW
211}
212
213PP(pp_padsv)
214{
215 dSP; dTARGET;
216 XPUSHs(TARG);
4633a7c4
LW
217 if (op->op_flags & OPf_MOD) {
218 if (op->op_private & OPpLVAL_INTRO)
219 SAVECLEARSV(curpad[op->op_targ]);
5f05dabc 220 else if (op->op_private & OPpDEREF)
02a9e968 221 provide_ref(op, curpad[op->op_targ]);
4633a7c4 222 }
a0d0e21e
LW
223 RETURN;
224}
225
226PP(pp_readline)
227{
228 last_in_gv = (GV*)(*stack_sp--);
229 return do_readline();
230}
231
232PP(pp_eq)
233{
234 dSP; tryAMAGICbinSET(eq,0);
235 {
236 dPOPnv;
237 SETs((TOPn == value) ? &sv_yes : &sv_no);
238 RETURN;
239 }
240}
241
242PP(pp_preinc)
243{
244 dSP;
55497cff
PP
245 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
246 SvIVX(TOPs) != IV_MAX)
247 {
248 ++SvIVX(TOPs);
249 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
250 }
251 else
252 sv_inc(TOPs);
a0d0e21e
LW
253 SvSETMAGIC(TOPs);
254 return NORMAL;
255}
256
257PP(pp_or)
258{
259 dSP;
260 if (SvTRUE(TOPs))
261 RETURN;
262 else {
263 --SP;
264 RETURNOP(cLOGOP->op_other);
265 }
266}
267
268PP(pp_add)
269{
270 dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
271 {
7a4c00b4 272 dPOPTOPnnrl_ul;
a0d0e21e
LW
273 SETn( left + right );
274 RETURN;
275 }
276}
277
278PP(pp_aelemfast)
279{
280 dSP;
281 AV *av = GvAV((GV*)cSVOP->op_sv);
282 SV** svp = av_fetch(av, op->op_private, op->op_flags & OPf_MOD);
283 PUSHs(svp ? *svp : &sv_undef);
284 RETURN;
285}
286
287PP(pp_join)
288{
289 dSP; dMARK; dTARGET;
290 MARK++;
291 do_join(TARG, *MARK, MARK, SP);
292 SP = MARK;
293 SETs(TARG);
294 RETURN;
295}
296
297PP(pp_pushre)
298{
299 dSP;
300 XPUSHs((SV*)op);
301 RETURN;
302}
303
304/* Oversized hot code. */
305
306PP(pp_print)
307{
308 dSP; dMARK; dORIGMARK;
309 GV *gv;
310 IO *io;
760ac839 311 register PerlIO *fp;
236988e4 312 MAGIC *mg;
a0d0e21e
LW
313
314 if (op->op_flags & OPf_STACKED)
315 gv = (GV*)*++MARK;
316 else
317 gv = defoutgv;
236988e4
PP
318 if (SvMAGICAL(gv) && (mg = mg_find((SV*)gv, 'q'))) {
319 SV *sv;
320
321 PUSHMARK(MARK-1);
322 *MARK = mg->mg_obj;
323 ENTER;
324 perl_call_method("PRINT", G_SCALAR);
325 LEAVE;
326 SPAGAIN;
327 sv = POPs;
328 SP = ORIGMARK;
329 PUSHs(sv);
330 RETURN;
331 }
a0d0e21e 332 if (!(io = GvIO(gv))) {
748a9306
LW
333 if (dowarn) {
334 SV* sv = sv_newmortal();
4f732624 335 gv_fullname3(sv, gv, Nullch);
748a9306
LW
336 warn("Filehandle %s never opened", SvPV(sv,na));
337 }
338
339 SETERRNO(EBADF,RMS$_IFI);
a0d0e21e
LW
340 goto just_say_no;
341 }
342 else if (!(fp = IoOFP(io))) {
343 if (dowarn) {
748a9306 344 SV* sv = sv_newmortal();
4f732624 345 gv_fullname3(sv, gv, Nullch);
a0d0e21e 346 if (IoIFP(io))
748a9306 347 warn("Filehandle %s opened only for input", SvPV(sv,na));
a0d0e21e 348 else
748a9306 349 warn("print on closed filehandle %s", SvPV(sv,na));
a0d0e21e 350 }
748a9306 351 SETERRNO(EBADF,IoIFP(io)?RMS$_FAC:RMS$_IFI);
a0d0e21e
LW
352 goto just_say_no;
353 }
354 else {
355 MARK++;
356 if (ofslen) {
357 while (MARK <= SP) {
358 if (!do_print(*MARK, fp))
359 break;
360 MARK++;
361 if (MARK <= SP) {
760ac839 362 if (PerlIO_write(fp, ofs, ofslen) == 0 || PerlIO_error(fp)) {
a0d0e21e
LW
363 MARK--;
364 break;
365 }
366 }
367 }
368 }
369 else {
370 while (MARK <= SP) {
371 if (!do_print(*MARK, fp))
372 break;
373 MARK++;
374 }
375 }
376 if (MARK <= SP)
377 goto just_say_no;
378 else {
379 if (orslen)
760ac839 380 if (PerlIO_write(fp, ors, orslen) == 0 || PerlIO_error(fp))
a0d0e21e
LW
381 goto just_say_no;
382
383 if (IoFLAGS(io) & IOf_FLUSH)
760ac839 384 if (PerlIO_flush(fp) == EOF)
a0d0e21e
LW
385 goto just_say_no;
386 }
387 }
388 SP = ORIGMARK;
389 PUSHs(&sv_yes);
390 RETURN;
391
392 just_say_no:
393 SP = ORIGMARK;
394 PUSHs(&sv_undef);
395 RETURN;
396}
397
398PP(pp_rv2av)
399{
400 dSP; dPOPss;
401
402 AV *av;
403
404 if (SvROK(sv)) {
405 wasref:
406 av = (AV*)SvRV(sv);
407 if (SvTYPE(av) != SVt_PVAV)
408 DIE("Not an ARRAY reference");
409 if (op->op_private & OPpLVAL_INTRO)
410 av = (AV*)save_svref((SV**)sv);
411 if (op->op_flags & OPf_REF) {
412 PUSHs((SV*)av);
413 RETURN;
414 }
415 }
416 else {
417 if (SvTYPE(sv) == SVt_PVAV) {
418 av = (AV*)sv;
419 if (op->op_flags & OPf_REF) {
420 PUSHs((SV*)av);
421 RETURN;
422 }
423 }
424 else {
67955e0c
PP
425 GV *gv;
426
a0d0e21e 427 if (SvTYPE(sv) != SVt_PVGV) {
748a9306
LW
428 char *sym;
429
a0d0e21e
LW
430 if (SvGMAGICAL(sv)) {
431 mg_get(sv);
432 if (SvROK(sv))
433 goto wasref;
434 }
435 if (!SvOK(sv)) {
436 if (op->op_flags & OPf_REF ||
437 op->op_private & HINT_STRICT_REFS)
438 DIE(no_usym, "an ARRAY");
4633a7c4
LW
439 if (GIMME == G_ARRAY)
440 RETURN;
a0d0e21e
LW
441 RETPUSHUNDEF;
442 }
748a9306 443 sym = SvPV(sv,na);
a0d0e21e 444 if (op->op_private & HINT_STRICT_REFS)
748a9306 445 DIE(no_symref, sym, "an ARRAY");
67955e0c
PP
446 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVAV);
447 } else {
448 gv = (GV*)sv;
a0d0e21e 449 }
67955e0c 450 av = GvAVn(gv);
a0d0e21e 451 if (op->op_private & OPpLVAL_INTRO)
67955e0c 452 av = save_ary(gv);
a0d0e21e
LW
453 if (op->op_flags & OPf_REF) {
454 PUSHs((SV*)av);
455 RETURN;
456 }
457 }
458 }
459
460 if (GIMME == G_ARRAY) {
461 I32 maxarg = AvFILL(av) + 1;
462 EXTEND(SP, maxarg);
463 Copy(AvARRAY(av), SP+1, maxarg, SV*);
464 SP += maxarg;
465 }
466 else {
467 dTARGET;
468 I32 maxarg = AvFILL(av) + 1;
469 PUSHi(maxarg);
470 }
471 RETURN;
472}
473
474PP(pp_rv2hv)
475{
476
477 dSP; dTOPss;
478
479 HV *hv;
480
481 if (SvROK(sv)) {
482 wasref:
483 hv = (HV*)SvRV(sv);
484 if (SvTYPE(hv) != SVt_PVHV)
485 DIE("Not a HASH reference");
486 if (op->op_private & OPpLVAL_INTRO)
487 hv = (HV*)save_svref((SV**)sv);
488 if (op->op_flags & OPf_REF) {
489 SETs((SV*)hv);
490 RETURN;
491 }
492 }
493 else {
494 if (SvTYPE(sv) == SVt_PVHV) {
495 hv = (HV*)sv;
496 if (op->op_flags & OPf_REF) {
497 SETs((SV*)hv);
498 RETURN;
499 }
500 }
501 else {
67955e0c
PP
502 GV *gv;
503
a0d0e21e 504 if (SvTYPE(sv) != SVt_PVGV) {
748a9306
LW
505 char *sym;
506
a0d0e21e
LW
507 if (SvGMAGICAL(sv)) {
508 mg_get(sv);
509 if (SvROK(sv))
510 goto wasref;
511 }
512 if (!SvOK(sv)) {
513 if (op->op_flags & OPf_REF ||
514 op->op_private & HINT_STRICT_REFS)
515 DIE(no_usym, "a HASH");
4633a7c4
LW
516 if (GIMME == G_ARRAY) {
517 SP--;
518 RETURN;
519 }
a0d0e21e
LW
520 RETSETUNDEF;
521 }
748a9306 522 sym = SvPV(sv,na);
a0d0e21e 523 if (op->op_private & HINT_STRICT_REFS)
748a9306 524 DIE(no_symref, sym, "a HASH");
67955e0c
PP
525 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVHV);
526 } else {
527 gv = (GV*)sv;
a0d0e21e 528 }
67955e0c 529 hv = GvHVn(gv);
a0d0e21e 530 if (op->op_private & OPpLVAL_INTRO)
67955e0c 531 hv = save_hash(gv);
a0d0e21e
LW
532 if (op->op_flags & OPf_REF) {
533 SETs((SV*)hv);
534 RETURN;
535 }
536 }
537 }
538
539 if (GIMME == G_ARRAY) { /* array wanted */
540 *stack_sp = (SV*)hv;
541 return do_kv(ARGS);
542 }
543 else {
544 dTARGET;
545 if (HvFILL(hv)) {
546 sprintf(buf, "%d/%d", HvFILL(hv), HvMAX(hv)+1);
547 sv_setpv(TARG, buf);
548 }
549 else
550 sv_setiv(TARG, 0);
551 SETTARG;
552 RETURN;
553 }
554}
555
556PP(pp_aassign)
557{
558 dSP;
559 SV **lastlelem = stack_sp;
560 SV **lastrelem = stack_base + POPMARK;
561 SV **firstrelem = stack_base + POPMARK + 1;
562 SV **firstlelem = lastrelem + 1;
563
564 register SV **relem;
565 register SV **lelem;
566
567 register SV *sv;
568 register AV *ary;
569
570 HV *hash;
571 I32 i;
572 int magic;
573
574 delaymagic = DM_DELAY; /* catch simultaneous items */
575
576 /* If there's a common identifier on both sides we have to take
577 * special care that assigning the identifier on the left doesn't
578 * clobber a value on the right that's used later in the list.
579 */
580 if (op->op_private & OPpASSIGN_COMMON) {
581 for (relem = firstrelem; relem <= lastrelem; relem++) {
582 /*SUPPRESS 560*/
583 if (sv = *relem)
584 *relem = sv_mortalcopy(sv);
585 }
586 }
587
588 relem = firstrelem;
589 lelem = firstlelem;
590 ary = Null(AV*);
591 hash = Null(HV*);
592 while (lelem <= lastlelem) {
bbce6d69 593 TAINT_NOT; /* Each item stands on its own, taintwise. */
a0d0e21e
LW
594 sv = *lelem++;
595 switch (SvTYPE(sv)) {
596 case SVt_PVAV:
597 ary = (AV*)sv;
748a9306 598 magic = SvMAGICAL(ary) != 0;
a0d0e21e
LW
599
600 av_clear(ary);
601 i = 0;
602 while (relem <= lastrelem) { /* gobble up all the rest */
603 sv = NEWSV(28,0);
604 assert(*relem);
605 sv_setsv(sv,*relem);
606 *(relem++) = sv;
607 (void)av_store(ary,i++,sv);
608 if (magic)
609 mg_set(sv);
bbce6d69 610 TAINT_NOT;
a0d0e21e
LW
611 }
612 break;
613 case SVt_PVHV: {
a0d0e21e
LW
614 SV *tmpstr;
615
616 hash = (HV*)sv;
748a9306 617 magic = SvMAGICAL(hash) != 0;
a0d0e21e
LW
618 hv_clear(hash);
619
620 while (relem < lastrelem) { /* gobble up all the rest */
621 STRLEN len;
4633a7c4 622 if (*relem)
a0d0e21e 623 sv = *(relem++);
4633a7c4 624 else
a0d0e21e 625 sv = &sv_no, relem++;
a0d0e21e
LW
626 tmpstr = NEWSV(29,0);
627 if (*relem)
628 sv_setsv(tmpstr,*relem); /* value */
629 *(relem++) = tmpstr;
760ac839 630 (void)hv_store_ent(hash,sv,tmpstr,0);
a0d0e21e
LW
631 if (magic)
632 mg_set(tmpstr);
bbce6d69 633 TAINT_NOT;
8e07c86e 634 }
760ac839
LW
635 if (relem == lastrelem)
636 warn("Odd number of elements in hash list");
a0d0e21e
LW
637 }
638 break;
639 default:
640 if (SvTHINKFIRST(sv)) {
641 if (SvREADONLY(sv) && curcop != &compiling) {
642 if (sv != &sv_undef && sv != &sv_yes && sv != &sv_no)
643 DIE(no_modify);
644 if (relem <= lastrelem)
645 relem++;
646 break;
647 }
648 if (SvROK(sv))
649 sv_unref(sv);
650 }
651 if (relem <= lastrelem) {
652 sv_setsv(sv, *relem);
653 *(relem++) = sv;
654 }
655 else
656 sv_setsv(sv, &sv_undef);
657 SvSETMAGIC(sv);
658 break;
659 }
660 }
661 if (delaymagic & ~DM_DELAY) {
662 if (delaymagic & DM_UID) {
663#ifdef HAS_SETRESUID
664 (void)setresuid(uid,euid,(Uid_t)-1);
56febc5e
AD
665#else
666# ifdef HAS_SETREUID
a0d0e21e 667 (void)setreuid(uid,euid);
56febc5e
AD
668# else
669# ifdef HAS_SETRUID
a0d0e21e
LW
670 if ((delaymagic & DM_UID) == DM_RUID) {
671 (void)setruid(uid);
748a9306 672 delaymagic &= ~DM_RUID;
a0d0e21e 673 }
56febc5e
AD
674# endif /* HAS_SETRUID */
675# ifdef HAS_SETEUID
a0d0e21e
LW
676 if ((delaymagic & DM_UID) == DM_EUID) {
677 (void)seteuid(uid);
748a9306 678 delaymagic &= ~DM_EUID;
a0d0e21e 679 }
56febc5e 680# endif /* HAS_SETEUID */
a0d0e21e
LW
681 if (delaymagic & DM_UID) {
682 if (uid != euid)
683 DIE("No setreuid available");
684 (void)setuid(uid);
685 }
56febc5e
AD
686# endif /* HAS_SETREUID */
687#endif /* HAS_SETRESUID */
a0d0e21e
LW
688 uid = (int)getuid();
689 euid = (int)geteuid();
690 }
691 if (delaymagic & DM_GID) {
692#ifdef HAS_SETRESGID
693 (void)setresgid(gid,egid,(Gid_t)-1);
56febc5e
AD
694#else
695# ifdef HAS_SETREGID
a0d0e21e 696 (void)setregid(gid,egid);
56febc5e
AD
697# else
698# ifdef HAS_SETRGID
a0d0e21e
LW
699 if ((delaymagic & DM_GID) == DM_RGID) {
700 (void)setrgid(gid);
748a9306 701 delaymagic &= ~DM_RGID;
a0d0e21e 702 }
56febc5e
AD
703# endif /* HAS_SETRGID */
704# ifdef HAS_SETEGID
a0d0e21e
LW
705 if ((delaymagic & DM_GID) == DM_EGID) {
706 (void)setegid(gid);
748a9306 707 delaymagic &= ~DM_EGID;
a0d0e21e 708 }
56febc5e 709# endif /* HAS_SETEGID */
a0d0e21e
LW
710 if (delaymagic & DM_GID) {
711 if (gid != egid)
712 DIE("No setregid available");
713 (void)setgid(gid);
714 }
56febc5e
AD
715# endif /* HAS_SETREGID */
716#endif /* HAS_SETRESGID */
a0d0e21e
LW
717 gid = (int)getgid();
718 egid = (int)getegid();
719 }
4633a7c4 720 tainting |= (uid && (euid != uid || egid != gid));
a0d0e21e
LW
721 }
722 delaymagic = 0;
723 if (GIMME == G_ARRAY) {
724 if (ary || hash)
725 SP = lastrelem;
726 else
727 SP = firstrelem + (lastlelem - firstlelem);
0c8c7a05 728 lelem = firstlelem + (relem - firstrelem);
5f05dabc 729 while (relem <= SP)
0c8c7a05 730 *relem++ = (lelem <= lastlelem) ? *lelem++ : &sv_undef;
a0d0e21e
LW
731 RETURN;
732 }
733 else {
4633a7c4 734 dTARGET;
a0d0e21e 735 SP = firstrelem;
a0d0e21e 736
4633a7c4
LW
737 SETi(lastrelem - firstrelem + 1);
738 RETURN;
a0d0e21e
LW
739 }
740}
741
742PP(pp_match)
743{
744 dSP; dTARG;
745 register PMOP *pm = cPMOP;
746 register char *t;
747 register char *s;
748 char *strend;
749 I32 global;
750 I32 safebase;
751 char *truebase;
752 register REGEXP *rx = pm->op_pmregexp;
753 I32 gimme = GIMME;
754 STRLEN len;
748a9306 755 I32 minmatch = 0;
4633a7c4 756 I32 oldsave = savestack_ix;
a0d0e21e
LW
757
758 if (op->op_flags & OPf_STACKED)
759 TARG = POPs;
760 else {
761 TARG = GvSV(defgv);
762 EXTEND(SP,1);
763 }
764 s = SvPV(TARG, len);
765 strend = s + len;
766 if (!s)
767 DIE("panic: do_match");
768
769 if (pm->op_pmflags & PMf_USED) {
770 if (gimme == G_ARRAY)
771 RETURN;
772 RETPUSHNO;
773 }
774
775 if (!rx->prelen && curpm) {
776 pm = curpm;
777 rx = pm->op_pmregexp;
778 }
779 truebase = t = s;
780 if (global = pm->op_pmflags & PMf_GLOBAL) {
781 rx->startp[0] = 0;
782 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
783 MAGIC* mg = mg_find(TARG, 'g');
748a9306 784 if (mg && mg->mg_len >= 0) {
a0d0e21e 785 rx->endp[0] = rx->startp[0] = s + mg->mg_len;
748a9306
LW
786 minmatch = (mg->mg_flags & MGf_MINMATCH);
787 }
a0d0e21e
LW
788 }
789 }
748a9306
LW
790 if (!rx->nparens && !global)
791 gimme = G_SCALAR; /* accidental array context? */
5f05dabc 792 safebase = (((gimme == G_ARRAY) || global) && !sawampersand);
a0d0e21e
LW
793 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
794 SAVEINT(multiline);
795 multiline = pm->op_pmflags & PMf_MULTILINE;
796 }
797
798play_it_again:
799 if (global && rx->startp[0]) {
800 t = s = rx->endp[0];
5f05dabc 801 if (s >= strend)
a0d0e21e 802 goto nope;
748a9306 803 minmatch = (s == rx->startp[0]);
a0d0e21e
LW
804 }
805 if (pm->op_pmshort) {
806 if (pm->op_pmflags & PMf_SCANFIRST) {
807 if (SvSCREAM(TARG)) {
808 if (screamfirst[BmRARE(pm->op_pmshort)] < 0)
809 goto nope;
810 else if (!(s = screaminstr(TARG, pm->op_pmshort)))
811 goto nope;
812 else if (pm->op_pmflags & PMf_ALL)
813 goto yup;
814 }
815 else if (!(s = fbm_instr((unsigned char*)s,
816 (unsigned char*)strend, pm->op_pmshort)))
817 goto nope;
818 else if (pm->op_pmflags & PMf_ALL)
819 goto yup;
820 if (s && rx->regback >= 0) {
821 ++BmUSEFUL(pm->op_pmshort);
822 s -= rx->regback;
823 if (s < t)
824 s = t;
825 }
826 else
827 s = t;
828 }
829 else if (!multiline) {
bbce6d69
PP
830 if (*SvPVX(pm->op_pmshort) != *s
831 || (pm->op_pmslen > 1
36477c24 832 && memNE(SvPVX(pm->op_pmshort), s, pm->op_pmslen)))
bbce6d69 833 goto nope;
a0d0e21e
LW
834 }
835 if (!rx->naughty && --BmUSEFUL(pm->op_pmshort) < 0) {
836 SvREFCNT_dec(pm->op_pmshort);
837 pm->op_pmshort = Nullsv; /* opt is being useless */
838 }
839 }
e50aee73 840 if (pregexec(rx, s, strend, truebase, minmatch,
bbce6d69
PP
841 SvSCREAM(TARG) ? TARG : Nullsv, safebase))
842 {
a0d0e21e
LW
843 curpm = pm;
844 if (pm->op_pmflags & PMf_ONCE)
845 pm->op_pmflags |= PMf_USED;
846 goto gotcha;
847 }
848 else
849 goto ret_no;
850 /*NOTREACHED*/
851
852 gotcha:
853 if (gimme == G_ARRAY) {
854 I32 iters, i, len;
855
bbce6d69 856 TAINT_IF(rx->exec_tainted);
a0d0e21e
LW
857 iters = rx->nparens;
858 if (global && !iters)
859 i = 1;
860 else
861 i = 0;
862 EXTEND(SP, iters + i);
bbce6d69 863 EXTEND_MORTAL(iters + i);
a0d0e21e
LW
864 for (i = !i; i <= iters; i++) {
865 PUSHs(sv_newmortal());
866 /*SUPPRESS 560*/
867 if ((s = rx->startp[i]) && rx->endp[i] ) {
868 len = rx->endp[i] - s;
869 sv_setpvn(*SP, s, len);
870 }
871 }
872 if (global) {
873 truebase = rx->subbeg;
5f05dabc 874 strend = rx->subend;
a0d0e21e
LW
875 if (rx->startp[0] && rx->startp[0] == rx->endp[0])
876 ++rx->endp[0];
877 goto play_it_again;
878 }
4633a7c4 879 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
880 RETURN;
881 }
882 else {
883 if (global) {
884 MAGIC* mg = 0;
885 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
886 mg = mg_find(TARG, 'g');
887 if (!mg) {
888 sv_magic(TARG, (SV*)0, 'g', Nullch, 0);
889 mg = mg_find(TARG, 'g');
890 }
748a9306 891 if (rx->startp[0]) {
5f05dabc 892 mg->mg_len = rx->endp[0] - rx->subbeg;
748a9306
LW
893 if (rx->startp[0] == rx->endp[0])
894 mg->mg_flags |= MGf_MINMATCH;
895 else
896 mg->mg_flags &= ~MGf_MINMATCH;
897 }
a0d0e21e 898 }
4633a7c4 899 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
900 RETPUSHYES;
901 }
902
903yup:
904 ++BmUSEFUL(pm->op_pmshort);
905 curpm = pm;
906 if (pm->op_pmflags & PMf_ONCE)
907 pm->op_pmflags |= PMf_USED;
5f05dabc
PP
908 Safefree(rx->subbase);
909 rx->subbase = Nullch;
a0d0e21e
LW
910 if (global) {
911 rx->subbeg = truebase;
912 rx->subend = strend;
913 rx->startp[0] = s;
914 rx->endp[0] = s + SvCUR(pm->op_pmshort);
915 goto gotcha;
916 }
917 if (sawampersand) {
918 char *tmps;
919
a0d0e21e
LW
920 tmps = rx->subbase = savepvn(t, strend-t);
921 rx->subbeg = tmps;
922 rx->subend = tmps + (strend-t);
923 tmps = rx->startp[0] = tmps + (s - t);
924 rx->endp[0] = tmps + SvCUR(pm->op_pmshort);
925 }
4633a7c4 926 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
927 RETPUSHYES;
928
929nope:
930 if (pm->op_pmshort)
931 ++BmUSEFUL(pm->op_pmshort);
932
933ret_no:
4633a7c4 934 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
935 if (gimme == G_ARRAY)
936 RETURN;
937 RETPUSHNO;
938}
939
940OP *
941do_readline()
942{
943 dSP; dTARGETSTACKED;
944 register SV *sv;
945 STRLEN tmplen = 0;
946 STRLEN offset;
760ac839 947 PerlIO *fp;
a0d0e21e
LW
948 register IO *io = GvIO(last_in_gv);
949 register I32 type = op->op_type;
e79b0511 950 MAGIC *mg;
a0d0e21e 951
e79b0511
PP
952 if (SvMAGICAL(last_in_gv) && (mg = mg_find((SV*)last_in_gv, 'q'))) {
953 PUSHMARK(SP);
954 XPUSHs(mg->mg_obj);
955 PUTBACK;
956 ENTER;
957 perl_call_method("READLINE", GIMME);
958 LEAVE;
959 SPAGAIN;
8ebc5c01
PP
960 if (GIMME == G_SCALAR)
961 SvSetSV_nosteal(TARG, TOPs);
e79b0511
PP
962 RETURN;
963 }
a0d0e21e
LW
964 fp = Nullfp;
965 if (io) {
966 fp = IoIFP(io);
967 if (!fp) {
968 if (IoFLAGS(io) & IOf_ARGV) {
969 if (IoFLAGS(io) & IOf_START) {
970 IoFLAGS(io) &= ~IOf_START;
971 IoLINES(io) = 0;
972 if (av_len(GvAVn(last_in_gv)) < 0) {
973 SV *tmpstr = newSVpv("-", 1); /* assume stdin */
974 av_push(GvAVn(last_in_gv), tmpstr);
975 }
976 }
977 fp = nextargv(last_in_gv);
978 if (!fp) { /* Note: fp != IoIFP(io) */
979 (void)do_close(last_in_gv, FALSE); /* now it does*/
980 IoFLAGS(io) |= IOf_START;
981 }
982 }
983 else if (type == OP_GLOB) {
984 SV *tmpcmd = NEWSV(55, 0);
985 SV *tmpglob = POPs;
986 ENTER;
987 SAVEFREESV(tmpcmd);
988#ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
989 /* since spawning off a process is a real performance hit */
990 {
991#include <descrip.h>
992#include <lib$routines.h>
993#include <nam.h>
994#include <rmsdef.h>
995 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
996 char vmsspec[NAM$C_MAXRSS+1];
997 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
998 char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
999 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
760ac839 1000 PerlIO *tmpfp;
a0d0e21e
LW
1001 STRLEN i;
1002 struct dsc$descriptor_s wilddsc
1003 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
1004 struct dsc$descriptor_vs rsdsc
1005 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
1006 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
1007
1008 /* We could find out if there's an explicit dev/dir or version
1009 by peeking into lib$find_file's internal context at
1010 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
1011 but that's unsupported, so I don't want to do it now and
1012 have it bite someone in the future. */
1013 strcat(tmpfnam,tmpnam(NULL));
1014 cp = SvPV(tmpglob,i);
1015 for (; i; i--) {
1016 if (cp[i] == ';') hasver = 1;
1017 if (cp[i] == '.') {
1018 if (sts) hasver = 1;
1019 else sts = 1;
1020 }
1021 if (cp[i] == '/') {
1022 hasdir = isunix = 1;
1023 break;
748a9306 1024 }
a0d0e21e
LW
1025 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
1026 hasdir = 1;
1027 break;
1028 }
1029 }
760ac839 1030 if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
a0d0e21e
LW
1031 ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
1032 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
1033 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
1034 &dfltdsc,NULL,NULL,NULL))&1)) {
1035 end = rstr + (unsigned long int) *rslt;
1036 if (!hasver) while (*end != ';') end--;
1037 *(end++) = '\n'; *end = '\0';
1038 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
1039 if (hasdir) {
271c2963 1040 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob));
a0d0e21e
LW
1041 begin = rstr;
1042 }
1043 else {
1044 begin = end;
1045 while (*(--begin) != ']' && *begin != '>') ;
1046 ++begin;
1047 }
760ac839 1048 ok = (PerlIO_puts(tmpfp,begin) != EOF);
a0d0e21e
LW
1049 }
1050 if (cxt) (void)lib$find_file_end(&cxt);
748a9306
LW
1051 if (ok && sts != RMS$_NMF &&
1052 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
a0d0e21e 1053 if (!ok) {
c07a80fd
PP
1054 if (!(sts & 1)) {
1055 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
1056 }
760ac839 1057 PerlIO_close(tmpfp);
a0d0e21e
LW
1058 fp = NULL;
1059 }
1060 else {
760ac839 1061 PerlIO_rewind(tmpfp);
a0d0e21e
LW
1062 IoTYPE(io) = '<';
1063 IoIFP(io) = fp = tmpfp;
1064 }
1065 }
1066 }
1067#else /* !VMS */
1068#ifdef DOSISH
67955e0c
PP
1069#ifdef OS2
1070 sv_setpv(tmpcmd, "for a in ");
1071 sv_catsv(tmpcmd, tmpglob);
1072 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
1073#else
a0d0e21e
LW
1074 sv_setpv(tmpcmd, "perlglob ");
1075 sv_catsv(tmpcmd, tmpglob);
1076 sv_catpv(tmpcmd, " |");
67955e0c
PP
1077#endif /* !OS2 */
1078#else /* !DOSISH */
1079#if defined(CSH)
a0d0e21e
LW
1080 sv_setpvn(tmpcmd, cshname, cshlen);
1081 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
1082 sv_catsv(tmpcmd, tmpglob);
16d20bd9 1083 sv_catpv(tmpcmd, "' 2>/dev/null |");
a0d0e21e
LW
1084#else
1085 sv_setpv(tmpcmd, "echo ");
1086 sv_catsv(tmpcmd, tmpglob);
1087#if 'z' - 'a' == 25
1088 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
1089#else
1090 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
1091#endif
1092#endif /* !CSH */
67955e0c 1093#endif /* !DOSISH */
c07a80fd
PP
1094 (void)do_open(last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
1095 FALSE, 0, 0, Nullfp);
a0d0e21e
LW
1096 fp = IoIFP(io);
1097#endif /* !VMS */
1098 LEAVE;
1099 }
1100 }
1101 else if (type == OP_GLOB)
1102 SP--;
1103 }
1104 if (!fp) {
4633a7c4 1105 if (dowarn && io && !(IoFLAGS(io) & IOf_START))
a0d0e21e
LW
1106 warn("Read on closed filehandle <%s>", GvENAME(last_in_gv));
1107 if (GIMME == G_SCALAR) {
1108 (void)SvOK_off(TARG);
1109 PUSHTARG;
1110 }
1111 RETURN;
1112 }
1113 if (GIMME == G_ARRAY) {
1114 sv = sv_2mortal(NEWSV(57, 80));
1115 offset = 0;
1116 }
1117 else {
1118 sv = TARG;
1119 (void)SvUPGRADE(sv, SVt_PV);
1120 tmplen = SvLEN(sv); /* remember if already alloced */
1121 if (!tmplen)
1122 Sv_Grow(sv, 80); /* try short-buffering it */
1123 if (type == OP_RCATLINE)
1124 offset = SvCUR(sv);
1125 else
1126 offset = 0;
1127 }
1128 for (;;) {
1129 if (!sv_gets(sv, fp, offset)) {
760ac839 1130 PerlIO_clearerr(fp);
a0d0e21e
LW
1131 if (IoFLAGS(io) & IOf_ARGV) {
1132 fp = nextargv(last_in_gv);
1133 if (fp)
1134 continue;
1135 (void)do_close(last_in_gv, FALSE);
1136 IoFLAGS(io) |= IOf_START;
1137 }
1138 else if (type == OP_GLOB) {
1139 (void)do_close(last_in_gv, FALSE);
1140 }
1141 if (GIMME == G_SCALAR) {
1142 (void)SvOK_off(TARG);
1143 PUSHTARG;
1144 }
1145 RETURN;
1146 }
bbce6d69
PP
1147 /* This should not be marked tainted if the fp is marked clean */
1148 if (!(IoFLAGS(io) & IOf_UNTAINT)) {
1149 TAINT;
1150 SvTAINTED_on(sv);
1151 }
a0d0e21e 1152 IoLINES(io)++;
71be2cbc 1153 SvSETMAGIC(sv);
a0d0e21e 1154 XPUSHs(sv);
a0d0e21e
LW
1155 if (type == OP_GLOB) {
1156 char *tmps;
1157
c07a80fd
PP
1158 if (SvCUR(sv) > 0 && SvCUR(rs) > 0) {
1159 tmps = SvEND(sv) - 1;
1160 if (*tmps == *SvPVX(rs)) {
1161 *tmps = '\0';
1162 SvCUR(sv)--;
1163 }
1164 }
a0d0e21e
LW
1165 for (tmps = SvPVX(sv); *tmps; tmps++)
1166 if (!isALPHA(*tmps) && !isDIGIT(*tmps) &&
1167 strchr("$&*(){}[]'\";\\|?<>~`", *tmps))
1168 break;
1169 if (*tmps && Stat(SvPVX(sv), &statbuf) < 0) {
1170 (void)POPs; /* Unmatched wildcard? Chuck it... */
1171 continue;
1172 }
1173 }
1174 if (GIMME == G_ARRAY) {
1175 if (SvLEN(sv) - SvCUR(sv) > 20) {
1176 SvLEN_set(sv, SvCUR(sv)+1);
1177 Renew(SvPVX(sv), SvLEN(sv), char);
1178 }
1179 sv = sv_2mortal(NEWSV(58, 80));
1180 continue;
1181 }
1182 else if (!tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
1183 /* try to reclaim a bit of scalar space (only on 1st alloc) */
1184 if (SvCUR(sv) < 60)
1185 SvLEN_set(sv, 80);
1186 else
1187 SvLEN_set(sv, SvCUR(sv)+40); /* allow some slop */
1188 Renew(SvPVX(sv), SvLEN(sv), char);
1189 }
1190 RETURN;
1191 }
1192}
1193
1194PP(pp_enter)
1195{
1196 dSP;
1197 register CONTEXT *cx;
1198 I32 gimme;
1199
1200 /*
1201 * We don't just use the GIMME macro here because it assumes there's
1202 * already a context, which ain't necessarily so at initial startup.
1203 */
1204
1205 if (op->op_flags & OPf_KNOW)
1206 gimme = op->op_flags & OPf_LIST;
1207 else if (cxstack_ix >= 0)
1208 gimme = cxstack[cxstack_ix].blk_gimme;
1209 else
1210 gimme = G_SCALAR;
1211
1212 ENTER;
1213
1214 SAVETMPS;
1215 PUSHBLOCK(cx, CXt_BLOCK, sp);
1216
1217 RETURN;
1218}
1219
1220PP(pp_helem)
1221{
1222 dSP;
760ac839 1223 HE* he;
a0d0e21e 1224 SV *keysv = POPs;
a0d0e21e
LW
1225 HV *hv = (HV*)POPs;
1226 I32 lval = op->op_flags & OPf_MOD;
1227
1228 if (SvTYPE(hv) != SVt_PVHV)
1229 RETPUSHUNDEF;
760ac839 1230 he = hv_fetch_ent(hv, keysv, lval, 0);
a0d0e21e 1231 if (lval) {
760ac839
LW
1232 if (!he || HeVAL(he) == &sv_undef)
1233 DIE(no_helem, SvPV(keysv, na));
5f05dabc
PP
1234 if (op->op_private & OPpLVAL_INTRO) {
1235 if (HvNAME(hv) && isGV(HeVAL(he)))
1236 save_gp((GV*)HeVAL(he), !(op->op_flags & OPf_SPECIAL));
1237 else
1238 save_svref(&HeVAL(he));
1239 }
1240 else if (op->op_private & OPpDEREF)
760ac839 1241 provide_ref(op, HeVAL(he));
a0d0e21e 1242 }
760ac839 1243 PUSHs(he ? HeVAL(he) : &sv_undef);
a0d0e21e
LW
1244 RETURN;
1245}
1246
1247PP(pp_leave)
1248{
1249 dSP;
1250 register CONTEXT *cx;
1251 register SV **mark;
1252 SV **newsp;
1253 PMOP *newpm;
1254 I32 gimme;
1255
1256 if (op->op_flags & OPf_SPECIAL) {
1257 cx = &cxstack[cxstack_ix];
1258 cx->blk_oldpm = curpm; /* fake block should preserve $1 et al */
1259 }
1260
1261 POPBLOCK(cx,newpm);
1262
1263 if (op->op_flags & OPf_KNOW)
1264 gimme = op->op_flags & OPf_LIST;
1265 else if (cxstack_ix >= 0)
1266 gimme = cxstack[cxstack_ix].blk_gimme;
1267 else
1268 gimme = G_SCALAR;
1269
1270 if (gimme == G_SCALAR) {
1271 if (op->op_private & OPpLEAVE_VOID)
1272 SP = newsp;
1273 else {
1274 MARK = newsp + 1;
1275 if (MARK <= SP)
1276 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1277 *MARK = TOPs;
1278 else
1279 *MARK = sv_mortalcopy(TOPs);
1280 else {
1281 MEXTEND(mark,0);
1282 *MARK = &sv_undef;
1283 }
1284 SP = MARK;
1285 }
1286 }
1287 else {
1288 for (mark = newsp + 1; mark <= SP; mark++)
1289 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP)))
1290 *mark = sv_mortalcopy(*mark);
1291 /* in case LEAVE wipes old return values */
1292 }
1293 curpm = newpm; /* Don't pop $1 et al till now */
1294
1295 LEAVE;
1296
1297 RETURN;
1298}
1299
1300PP(pp_iter)
1301{
1302 dSP;
1303 register CONTEXT *cx;
5f05dabc 1304 SV* sv;
4633a7c4 1305 AV* av;
a0d0e21e
LW
1306
1307 EXTEND(sp, 1);
1308 cx = &cxstack[cxstack_ix];
1309 if (cx->cx_type != CXt_LOOP)
1310 DIE("panic: pp_iter");
1311
71be2cbc
PP
1312 av = cx->blk_loop.iterary;
1313 if (cx->blk_loop.iterix >= (av == curstack ? cx->blk_oldsp : AvFILL(av)))
a0d0e21e
LW
1314 RETPUSHNO;
1315
7a4c00b4
PP
1316 SvREFCNT_dec(*cx->blk_loop.itervar);
1317
5f05dabc 1318 if (sv = AvARRAY(av)[++cx->blk_loop.iterix])
a0d0e21e 1319 SvTEMP_off(sv);
a0d0e21e 1320 else
5f05dabc
PP
1321 sv = &sv_undef;
1322 if (av != curstack && SvIMMORTAL(sv)) {
1323 SV *lv = cx->blk_loop.iterlval;
71be2cbc
PP
1324 if (lv && SvREFCNT(lv) > 1) {
1325 SvREFCNT_dec(lv);
1326 lv = Nullsv;
1327 }
5f05dabc
PP
1328 if (lv)
1329 SvREFCNT_dec(LvTARG(lv));
1330 else {
1331 lv = cx->blk_loop.iterlval = newSVsv(sv);
1332 sv_upgrade(lv, SVt_PVLV);
1333 sv_magic(lv, Nullsv, 'y', Nullch, 0);
1334 LvTYPE(lv) = 'y';
1335 }
1336 LvTARG(lv) = SvREFCNT_inc(av);
1337 LvTARGOFF(lv) = cx->blk_loop.iterix;
1338 LvTARGLEN(lv) = 1;
1339 sv = (SV*)lv;
1340 }
7a4c00b4
PP
1341
1342 *cx->blk_loop.itervar = SvREFCNT_inc(sv);
a0d0e21e
LW
1343 RETPUSHYES;
1344}
1345
1346PP(pp_subst)
1347{
1348 dSP; dTARG;
1349 register PMOP *pm = cPMOP;
1350 PMOP *rpm = pm;
1351 register SV *dstr;
1352 register char *s;
1353 char *strend;
1354 register char *m;
1355 char *c;
1356 register char *d;
1357 STRLEN clen;
1358 I32 iters = 0;
1359 I32 maxiters;
1360 register I32 i;
1361 bool once;
71be2cbc 1362 bool rxtainted;
a0d0e21e
LW
1363 char *orig;
1364 I32 safebase;
1365 register REGEXP *rx = pm->op_pmregexp;
1366 STRLEN len;
1367 int force_on_match = 0;
4633a7c4 1368 I32 oldsave = savestack_ix;
a0d0e21e
LW
1369
1370 if (pm->op_pmflags & PMf_CONST) /* known replacement string? */
1371 dstr = POPs;
1372 if (op->op_flags & OPf_STACKED)
1373 TARG = POPs;
1374 else {
1375 TARG = GvSV(defgv);
1376 EXTEND(SP,1);
1377 }
1378 s = SvPV(TARG, len);
8e07c86e 1379 if (!SvPOKp(TARG) || SvREADONLY(TARG) || (SvTYPE(TARG) == SVt_PVGV))
a0d0e21e
LW
1380 force_on_match = 1;
1381
1382 force_it:
1383 if (!pm || !s)
1384 DIE("panic: do_subst");
1385
1386 strend = s + len;
1387 maxiters = (strend - s) + 10;
1388
1389 if (!rx->prelen && curpm) {
1390 pm = curpm;
1391 rx = pm->op_pmregexp;
1392 }
5f05dabc 1393 safebase = (!rx->nparens && !sawampersand);
a0d0e21e
LW
1394 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
1395 SAVEINT(multiline);
1396 multiline = pm->op_pmflags & PMf_MULTILINE;
1397 }
1398 orig = m = s;
1399 if (pm->op_pmshort) {
1400 if (pm->op_pmflags & PMf_SCANFIRST) {
1401 if (SvSCREAM(TARG)) {
1402 if (screamfirst[BmRARE(pm->op_pmshort)] < 0)
1403 goto nope;
1404 else if (!(s = screaminstr(TARG, pm->op_pmshort)))
1405 goto nope;
1406 }
1407 else if (!(s = fbm_instr((unsigned char*)s, (unsigned char*)strend,
1408 pm->op_pmshort)))
1409 goto nope;
1410 if (s && rx->regback >= 0) {
1411 ++BmUSEFUL(pm->op_pmshort);
1412 s -= rx->regback;
1413 if (s < m)
1414 s = m;
1415 }
1416 else
1417 s = m;
1418 }
1419 else if (!multiline) {
bbce6d69
PP
1420 if (*SvPVX(pm->op_pmshort) != *s
1421 || (pm->op_pmslen > 1
36477c24 1422 && memNE(SvPVX(pm->op_pmshort), s, pm->op_pmslen)))
bbce6d69 1423 goto nope;
a0d0e21e
LW
1424 }
1425 if (!rx->naughty && --BmUSEFUL(pm->op_pmshort) < 0) {
1426 SvREFCNT_dec(pm->op_pmshort);
1427 pm->op_pmshort = Nullsv; /* opt is being useless */
1428 }
1429 }
71be2cbc
PP
1430
1431 /* only replace once? */
a0d0e21e 1432 once = !(rpm->op_pmflags & PMf_GLOBAL);
71be2cbc
PP
1433
1434 /* known replacement string? */
1435 c = (rpm->op_pmflags & PMf_CONST) ? SvPV(dstr, clen) : Nullch;
1436
1437 /* can do inplace substitution? */
1438 if (c && clen <= rx->minlen) {
1439 if (! pregexec(rx, s, strend, orig, 0,
1440 SvSCREAM(TARG) ? TARG : Nullsv, safebase)) {
1441 PUSHs(&sv_no);
1442 LEAVE_SCOPE(oldsave);
1443 RETURN;
1444 }
1445 if (force_on_match) {
1446 force_on_match = 0;
1447 s = SvPV_force(TARG, len);
1448 goto force_it;
1449 }
1450 if (rx->subbase) /* oops, no we can't */
1451 goto long_way;
1452 d = s;
1453 curpm = pm;
1454 SvSCREAM_off(TARG); /* disable possible screamer */
1455 if (once) {
1456 rxtainted = rx->exec_tainted;
1457 m = rx->startp[0];
1458 d = rx->endp[0];
1459 s = orig;
1460 if (m - s > strend - d) { /* faster to shorten from end */
1461 if (clen) {
1462 Copy(c, m, clen, char);
1463 m += clen;
a0d0e21e 1464 }
71be2cbc
PP
1465 i = strend - d;
1466 if (i > 0) {
1467 Move(d, m, i, char);
1468 m += i;
a0d0e21e 1469 }
71be2cbc
PP
1470 *m = '\0';
1471 SvCUR_set(TARG, m - s);
1472 }
1473 /*SUPPRESS 560*/
1474 else if (i = m - s) { /* faster from front */
1475 d -= clen;
1476 m = d;
1477 sv_chop(TARG, d-i);
1478 s += i;
1479 while (i--)
1480 *--d = *--s;
1481 if (clen)
1482 Copy(c, m, clen, char);
1483 }
1484 else if (clen) {
1485 d -= clen;
1486 sv_chop(TARG, d);
1487 Copy(c, d, clen, char);
1488 }
1489 else {
1490 sv_chop(TARG, d);
1491 }
1492 PUSHs(&sv_yes);
1493 }
1494 else {
1495 rxtainted = 0;
1496 do {
1497 if (iters++ > maxiters)
1498 DIE("Substitution loop");
1499 rxtainted |= rx->exec_tainted;
1500 m = rx->startp[0];
1501 /*SUPPRESS 560*/
1502 if (i = m - s) {
1503 if (s != d)
1504 Move(s, d, i, char);
1505 d += i;
a0d0e21e 1506 }
71be2cbc
PP
1507 if (clen) {
1508 Copy(c, d, clen, char);
1509 d += clen;
1510 }
1511 s = rx->endp[0];
1512 } while (pregexec(rx, s, strend, orig, s == m,
1513 Nullsv, TRUE)); /* don't match same null twice */
1514 if (s != d) {
1515 i = strend - s;
1516 SvCUR_set(TARG, d - SvPVX(TARG) + i);
1517 Move(s, d, i+1, char); /* include the NUL */
a0d0e21e 1518 }
71be2cbc 1519 PUSHs(sv_2mortal(newSViv((I32)iters)));
a0d0e21e 1520 }
71be2cbc
PP
1521 (void)SvPOK_only(TARG);
1522 SvSETMAGIC(TARG);
1523 if (rxtainted)
1524 SvTAINTED_on(TARG);
1525 LEAVE_SCOPE(oldsave);
1526 RETURN;
a0d0e21e 1527 }
71be2cbc 1528
e50aee73 1529 if (pregexec(rx, s, strend, orig, 0,
5f05dabc 1530 SvSCREAM(TARG) ? TARG : Nullsv, safebase)) {
a0d0e21e
LW
1531 long_way:
1532 if (force_on_match) {
1533 force_on_match = 0;
1534 s = SvPV_force(TARG, len);
1535 goto force_it;
1536 }
71be2cbc 1537 rxtainted = rx->exec_tainted;
a0d0e21e
LW
1538 dstr = NEWSV(25, sv_len(TARG));
1539 sv_setpvn(dstr, m, s-m);
1540 curpm = pm;
1541 if (!c) {
1542 register CONTEXT *cx;
1543 PUSHSUBST(cx);
1544 RETURNOP(cPMOP->op_pmreplroot);
1545 }
1546 do {
1547 if (iters++ > maxiters)
1548 DIE("Substitution loop");
71be2cbc 1549 rxtainted |= rx->exec_tainted;
a0d0e21e
LW
1550 if (rx->subbase && rx->subbase != orig) {
1551 m = s;
1552 s = orig;
1553 orig = rx->subbase;
1554 s = orig + (m - s);
1555 strend = s + (strend - m);
1556 }
1557 m = rx->startp[0];
1558 sv_catpvn(dstr, s, m-s);
1559 s = rx->endp[0];
1560 if (clen)
1561 sv_catpvn(dstr, c, clen);
1562 if (once)
1563 break;
5f05dabc 1564 } while (pregexec(rx, s, strend, orig, s == m, Nullsv, safebase));
a0d0e21e 1565 sv_catpvn(dstr, s, strend - s);
748a9306 1566
4633a7c4 1567 (void)SvOOK_off(TARG);
cb0b1708 1568 Safefree(SvPVX(TARG));
748a9306
LW
1569 SvPVX(TARG) = SvPVX(dstr);
1570 SvCUR_set(TARG, SvCUR(dstr));
1571 SvLEN_set(TARG, SvLEN(dstr));
1572 SvPVX(dstr) = 0;
1573 sv_free(dstr);
1574
a0d0e21e
LW
1575 (void)SvPOK_only(TARG);
1576 SvSETMAGIC(TARG);
71be2cbc
PP
1577 if (rxtainted)
1578 SvTAINTED_on(TARG);
a0d0e21e 1579 PUSHs(sv_2mortal(newSViv((I32)iters)));
4633a7c4 1580 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1581 RETURN;
1582 }
1583 PUSHs(&sv_no);
4633a7c4 1584 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1585 RETURN;
1586
1587nope:
1588 ++BmUSEFUL(pm->op_pmshort);
1589 PUSHs(&sv_no);
4633a7c4 1590 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1591 RETURN;
1592}
1593
1594PP(pp_grepwhile)
1595{
1596 dSP;
1597
1598 if (SvTRUEx(POPs))
1599 stack_base[markstack_ptr[-1]++] = stack_base[*markstack_ptr];
1600 ++*markstack_ptr;
1601 LEAVE; /* exit inner scope */
1602
1603 /* All done yet? */
1604 if (stack_base + *markstack_ptr > sp) {
1605 I32 items;
1606
1607 LEAVE; /* exit outer scope */
1608 (void)POPMARK; /* pop src */
1609 items = --*markstack_ptr - markstack_ptr[-1];
1610 (void)POPMARK; /* pop dst */
1611 SP = stack_base + POPMARK; /* pop original mark */
1612 if (GIMME != G_ARRAY) {
1613 dTARGET;
1614 XPUSHi(items);
1615 RETURN;
1616 }
1617 SP += items;
1618 RETURN;
1619 }
1620 else {
1621 SV *src;
1622
1623 ENTER; /* enter inner scope */
1624 SAVESPTR(curpm);
1625
1626 src = stack_base[*markstack_ptr];
1627 SvTEMP_off(src);
1628 GvSV(defgv) = src;
1629
1630 RETURNOP(cLOGOP->op_other);
1631 }
1632}
1633
1634PP(pp_leavesub)
1635{
1636 dSP;
1637 SV **mark;
1638 SV **newsp;
1639 PMOP *newpm;
1640 I32 gimme;
1641 register CONTEXT *cx;
1642
1643 POPBLOCK(cx,newpm);
1644 POPSUB(cx);
1645
1646 if (gimme == G_SCALAR) {
1647 MARK = newsp + 1;
1648 if (MARK <= SP)
1649 if (SvFLAGS(TOPs) & SVs_TEMP)
1650 *MARK = TOPs;
1651 else
1652 *MARK = sv_mortalcopy(TOPs);
1653 else {
1654 MEXTEND(mark,0);
1655 *MARK = &sv_undef;
1656 }
1657 SP = MARK;
1658 }
1659 else {
1660 for (mark = newsp + 1; mark <= SP; mark++)
1661 if (!(SvFLAGS(*mark) & SVs_TEMP))
1662 *mark = sv_mortalcopy(*mark);
1663 /* in case LEAVE wipes old return values */
1664 }
1665
1666 if (cx->blk_sub.hasargs) { /* You don't exist; go away. */
1667 AV* av = cx->blk_sub.argarray;
1668
1669 av_clear(av);
1670 AvREAL_off(av);
1671 }
1672 curpm = newpm; /* Don't pop $1 et al till now */
1673
1674 LEAVE;
1675 PUTBACK;
1676 return pop_return();
1677}
1678
1679PP(pp_entersub)
1680{
1681 dSP; dPOPss;
1682 GV *gv;
1683 HV *stash;
1684 register CV *cv;
1685 register CONTEXT *cx;
5d94fbed 1686 I32 gimme;
07055b4c 1687 bool hasargs = (op->op_flags & OPf_STACKED) != 0;
a0d0e21e
LW
1688
1689 if (!sv)
1690 DIE("Not a CODE reference");
1691 switch (SvTYPE(sv)) {
1692 default:
1693 if (!SvROK(sv)) {
748a9306
LW
1694 char *sym;
1695
a0d0e21e
LW
1696 if (sv == &sv_yes) /* unfound import, ignore */
1697 RETURN;
1698 if (!SvOK(sv))
1699 DIE(no_usym, "a subroutine");
748a9306 1700 sym = SvPV(sv,na);
a0d0e21e 1701 if (op->op_private & HINT_STRICT_REFS)
748a9306
LW
1702 DIE(no_symref, sym, "a subroutine");
1703 cv = perl_get_cv(sym, TRUE);
a0d0e21e
LW
1704 break;
1705 }
1706 cv = (CV*)SvRV(sv);
28757baa 1707 if (SvTYPE(cv) == SVt_PVCV)
a0d0e21e
LW
1708 break;
1709 /* FALL THROUGH */
1710 case SVt_PVHV:
1711 case SVt_PVAV:
1712 DIE("Not a CODE reference");
1713 case SVt_PVCV:
1714 cv = (CV*)sv;
1715 break;
1716 case SVt_PVGV:
8ebc5c01 1717 if (!(cv = GvCVu((GV*)sv)))
a0d0e21e
LW
1718 cv = sv_2cv(sv, &stash, &gv, TRUE);
1719 break;
1720 }
1721
8ebc5c01
PP
1722 ENTER;
1723 SAVETMPS;
1724
a0d0e21e
LW
1725 retry:
1726 if (!cv)
1727 DIE("Not a CODE reference");
1728
1729 if (!CvROOT(cv) && !CvXSUB(cv)) {
1730 if (gv = CvGV(cv)) {
4633a7c4 1731 SV *tmpstr;
a0d0e21e 1732 GV *ngv;
8ebc5c01 1733 if (cv != GvCV(gv)) { /* autoloaded stub? */
4633a7c4 1734 cv = GvCV(gv);
4633a7c4
LW
1735 goto retry;
1736 }
1737 tmpstr = sv_newmortal();
4f732624 1738 gv_efullname3(tmpstr, gv, Nullch);
a0d0e21e
LW
1739 ngv = gv_fetchmethod(GvESTASH(gv), "AUTOLOAD");
1740 if (ngv && ngv != gv && (cv = GvCV(ngv))) { /* One more chance... */
1741 gv = ngv;
1742 sv_setsv(GvSV(CvGV(cv)), tmpstr); /* Set CV's $AUTOLOAD */
bbce6d69 1743 SvTAINTED_off(GvSV(CvGV(cv)));
a0d0e21e
LW
1744 goto retry;
1745 }
1746 else
1747 DIE("Undefined subroutine &%s called",SvPVX(tmpstr));
1748 }
1749 DIE("Undefined subroutine called");
1750 }
1751
4633a7c4 1752 gimme = GIMME;
28757baa 1753 if ((op->op_private & OPpENTERSUB_DB) && GvCV(DBsub) && !CvNODEBUG(cv)) {
71be2cbc 1754 SV *oldsv = sv;
a0d0e21e
LW
1755 sv = GvSV(DBsub);
1756 save_item(sv);
67955e0c 1757 gv = CvGV(cv);
71be2cbc
PP
1758 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
1759 || strEQ(GvNAME(gv), "END")
1760 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
1761 !( (SvTYPE(oldsv) == SVt_PVGV) && (GvCV((GV*)oldsv) == cv)
1762 && (gv = (GV*)oldsv) ))) { /* Use GV from the stack as a fallback. */
1763 /* GV is potentially non-unique, or contain different CV. */
748a9306 1764 sv_setsv(sv, newRV((SV*)cv));
a5f75d66 1765 }
748a9306 1766 else {
4f732624 1767 gv_efullname3(sv, gv, Nullch);
748a9306 1768 }
a0d0e21e 1769 cv = GvCV(DBsub);
67955e0c 1770 if (CvXSUB(cv)) curcopdb = curcop;
a0d0e21e
LW
1771 if (!cv)
1772 DIE("No DBsub routine");
1773 }
1774
1775 if (CvXSUB(cv)) {
1776 if (CvOLDSTYLE(cv)) {
ecfc5424 1777 I32 (*fp3)_((int,int,int));
a0d0e21e
LW
1778 dMARK;
1779 register I32 items = SP - MARK;
67955e0c 1780 /* We dont worry to copy from @_. */
a0d0e21e
LW
1781 while (sp > mark) {
1782 sp[1] = sp[0];
1783 sp--;
1784 }
1785 stack_sp = mark + 1;
ecfc5424
AD
1786 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
1787 items = (*fp3)(CvXSUBANY(cv).any_i32,
1788 MARK - stack_base + 1,
1789 items);
a0d0e21e
LW
1790 stack_sp = stack_base + items;
1791 }
1792 else {
748a9306
LW
1793 I32 markix = TOPMARK;
1794
a0d0e21e 1795 PUTBACK;
67955e0c
PP
1796
1797 if (!hasargs) {
1798 /* Need to copy @_ to stack. Alternative may be to
1799 * switch stack to @_, and copy return values
1800 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
1801 AV* av = GvAV(defgv);
1802 I32 items = AvFILL(av) + 1;
1803
1804 if (items) {
1805 /* Mark is at the end of the stack. */
1806 EXTEND(sp, items);
1807 Copy(AvARRAY(av), sp + 1, items, SV*);
1808 sp += items;
1809 PUTBACK ;
1810 }
1811 }
1812 if (curcopdb) { /* We assume that the first
1813 XSUB in &DB::sub is the
1814 called one. */
1815 SAVESPTR(curcop);
1816 curcop = curcopdb;
1817 curcopdb = NULL;
1818 }
1819 /* Do we need to open block here? XXXX */
a0d0e21e 1820 (void)(*CvXSUB(cv))(cv);
748a9306
LW
1821
1822 /* Enforce some sanity in scalar context. */
5d94fbed 1823 if (gimme == G_SCALAR && ++markix != stack_sp - stack_base ) {
748a9306
LW
1824 if (markix > stack_sp - stack_base)
1825 *(stack_base + markix) = &sv_undef;
1826 else
1827 *(stack_base + markix) = *stack_sp;
1828 stack_sp = stack_base + markix;
1829 }
a0d0e21e
LW
1830 }
1831 LEAVE;
1832 return NORMAL;
1833 }
1834 else {
1835 dMARK;
1836 register I32 items = SP - MARK;
a0d0e21e
LW
1837 AV* padlist = CvPADLIST(cv);
1838 SV** svp = AvARRAY(padlist);
1839 push_return(op->op_next);
1840 PUSHBLOCK(cx, CXt_SUB, MARK);
1841 PUSHSUB(cx);
1842 CvDEPTH(cv)++;
1843 if (CvDEPTH(cv) < 2)
1844 (void)SvREFCNT_inc(cv);
1845 else { /* save temporaries on recursion? */
5f05dabc
PP
1846 if (CvDEPTH(cv) == 100 && dowarn
1847 && !(perldb && cv == GvCV(DBsub)))
a0d0e21e
LW
1848 warn("Deep recursion on subroutine \"%s\"",GvENAME(CvGV(cv)));
1849 if (CvDEPTH(cv) > AvFILL(padlist)) {
1850 AV *av;
1851 AV *newpad = newAV();
4aa0a1f7 1852 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
a0d0e21e
LW
1853 I32 ix = AvFILL((AV*)svp[1]);
1854 svp = AvARRAY(svp[0]);
748a9306 1855 for ( ;ix > 0; ix--) {
a0d0e21e 1856 if (svp[ix] != &sv_undef) {
748a9306 1857 char *name = SvPVX(svp[ix]);
5f05dabc
PP
1858 if ((SvFLAGS(svp[ix]) & SVf_FAKE) /* outer lexical? */
1859 || *name == '&') /* anonymous code? */
1860 {
1861 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix]));
748a9306
LW
1862 }
1863 else { /* our own lexical */
1864 if (*name == '@')
1865 av_store(newpad, ix, sv = (SV*)newAV());
1866 else if (*name == '%')
1867 av_store(newpad, ix, sv = (SV*)newHV());
1868 else
1869 av_store(newpad, ix, sv = NEWSV(0,0));
1870 SvPADMY_on(sv);
1871 }
a0d0e21e
LW
1872 }
1873 else {
748a9306 1874 av_store(newpad, ix, sv = NEWSV(0,0));
a0d0e21e
LW
1875 SvPADTMP_on(sv);
1876 }
1877 }
1878 av = newAV(); /* will be @_ */
1879 av_extend(av, 0);
1880 av_store(newpad, 0, (SV*)av);
1881 AvFLAGS(av) = AVf_REIFY;
1882 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
1883 AvFILL(padlist) = CvDEPTH(cv);
1884 svp = AvARRAY(padlist);
1885 }
1886 }
1887 SAVESPTR(curpad);
1888 curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
1889 if (hasargs) {
1890 AV* av = (AV*)curpad[0];
1891 SV** ary;
1892
1893 if (AvREAL(av)) {
1894 av_clear(av);
1895 AvREAL_off(av);
1896 }
1897 cx->blk_sub.savearray = GvAV(defgv);
1898 cx->blk_sub.argarray = av;
0c8c7a05 1899 GvAV(defgv) = (AV*)SvREFCNT_inc(av);
a0d0e21e
LW
1900 ++MARK;
1901
1902 if (items > AvMAX(av) + 1) {
1903 ary = AvALLOC(av);
1904 if (AvARRAY(av) != ary) {
1905 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
1906 SvPVX(av) = (char*)ary;
1907 }
1908 if (items > AvMAX(av) + 1) {
1909 AvMAX(av) = items - 1;
1910 Renew(ary,items,SV*);
1911 AvALLOC(av) = ary;
1912 SvPVX(av) = (char*)ary;
1913 }
1914 }
1915 Copy(MARK,AvARRAY(av),items,SV*);
1916 AvFILL(av) = items - 1;
1917
1918 while (items--) {
1919 if (*MARK)
1920 SvTEMP_off(*MARK);
1921 MARK++;
1922 }
1923 }
1924 RETURNOP(CvSTART(cv));
1925 }
1926}
1927
1928PP(pp_aelem)
1929{
1930 dSP;
1931 SV** svp;
748a9306 1932 I32 elem = POPi;
a0d0e21e
LW
1933 AV *av = (AV*)POPs;
1934 I32 lval = op->op_flags & OPf_MOD;
1935
748a9306
LW
1936 if (elem > 0)
1937 elem -= curcop->cop_arybase;
a0d0e21e
LW
1938 if (SvTYPE(av) != SVt_PVAV)
1939 RETPUSHUNDEF;
1940 svp = av_fetch(av, elem, lval);
1941 if (lval) {
1942 if (!svp || *svp == &sv_undef)
1943 DIE(no_aelem, elem);
1944 if (op->op_private & OPpLVAL_INTRO)
1945 save_svref(svp);
5f05dabc 1946 else if (op->op_private & OPpDEREF)
02a9e968 1947 provide_ref(op, *svp);
a0d0e21e
LW
1948 }
1949 PUSHs(svp ? *svp : &sv_undef);
1950 RETURN;
1951}
1952
02a9e968
CS
1953void
1954provide_ref(op, sv)
1955OP* op;
1956SV* sv;
1957{
1958 if (SvGMAGICAL(sv))
1959 mg_get(sv);
1960 if (!SvOK(sv)) {
1961 if (SvREADONLY(sv))
1962 croak(no_modify);
5f05dabc
PP
1963 if (SvTYPE(sv) < SVt_RV)
1964 sv_upgrade(sv, SVt_RV);
1965 else if (SvTYPE(sv) >= SVt_PV) {
1966 (void)SvOOK_off(sv);
1967 Safefree(SvPVX(sv));
1968 SvLEN(sv) = SvCUR(sv) = 0;
1969 }
1970 switch (op->op_private & OPpDEREF)
1971 {
1972 case OPpDEREF_SV:
1973 SvRV(sv) = newSV(0);
1974 break;
1975 case OPpDEREF_AV:
1976 SvRV(sv) = (SV*)newAV();
1977 break;
1978 case OPpDEREF_HV:
1979 SvRV(sv) = (SV*)newHV();
1980 break;
1981 }
02a9e968
CS
1982 SvROK_on(sv);
1983 SvSETMAGIC(sv);
1984 }
1985}
1986
a0d0e21e
LW
1987PP(pp_method)
1988{
1989 dSP;
1990 SV* sv;
1991 SV* ob;
1992 GV* gv;
1993 SV* nm;
1994
1995 nm = TOPs;
1996 sv = *(stack_base + TOPMARK + 1);
1997
1998 gv = 0;
16d20bd9
AD
1999 if (SvGMAGICAL(sv))
2000 mg_get(sv);
a0d0e21e 2001 if (SvROK(sv))
16d20bd9 2002 ob = (SV*)SvRV(sv);
a0d0e21e
LW
2003 else {
2004 GV* iogv;
2005 char* packname = 0;
67955e0c 2006 STRLEN packlen;
a0d0e21e
LW
2007
2008 if (!SvOK(sv) ||
67955e0c 2009 !(packname = SvPV(sv, packlen)) ||
a0d0e21e
LW
2010 !(iogv = gv_fetchpv(packname, FALSE, SVt_PVIO)) ||
2011 !(ob=(SV*)GvIO(iogv)))
2012 {
2013 char *name = SvPV(nm, na);
2014 HV *stash;
2015 if (!packname || !isALPHA(*packname))
2016DIE("Can't call method \"%s\" without a package or object reference", name);
67955e0c
PP
2017 if (!(stash = gv_stashpvn(packname, packlen, FALSE))) {
2018 if (gv_stashpvn("UNIVERSAL", 9, FALSE))
2019 stash = gv_stashpvn(packname, packlen, TRUE);
a0d0e21e
LW
2020 else
2021 DIE("Can't call method \"%s\" in empty package \"%s\"",
2022 name, packname);
2023 }
2024 gv = gv_fetchmethod(stash,name);
2025 if (!gv)
2026 DIE("Can't locate object method \"%s\" via package \"%s\"",
2027 name, packname);
8ebc5c01 2028 SETs(isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv);
a0d0e21e
LW
2029 RETURN;
2030 }
67955e0c 2031 *(stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
a0d0e21e
LW
2032 }
2033
2034 if (!ob || !SvOBJECT(ob)) {
2035 char *name = SvPV(nm, na);
2036 DIE("Can't call method \"%s\" on unblessed reference", name);
2037 }
2038
2039 if (!gv) { /* nothing cached */
2040 char *name = SvPV(nm, na);
2041 gv = gv_fetchmethod(SvSTASH(ob),name);
2042 if (!gv)
2043 DIE("Can't locate object method \"%s\" via package \"%s\"",
2044 name, HvNAME(SvSTASH(ob)));
2045 }
2046
8ebc5c01 2047 SETs(isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv);
a0d0e21e
LW
2048 RETURN;
2049}
2050