3 * Copyright (c) 1991-1997, Larry Wall
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.
11 * 'Mercy!' cried Gandalf. 'If the giving of information is to be the cure
12 * of your inquisitiveness, I shall spend all the rest of my days answering
13 * you. What more do you want to know?'
14 * 'The names of all the stars, and of all living things, and the whole
15 * history of Middle-earth and Over-heaven and of the Sundering Seas,'
23 gv_AVadd(register GV *gv)
25 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
26 croak("Bad symbol for array");
33 gv_HVadd(register GV *gv)
35 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
36 croak("Bad symbol for hash");
43 gv_IOadd(register GV *gv)
45 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
46 croak("Bad symbol for filehandle");
53 gv_fetchfile(char *name)
61 tmplen = strlen(name) + 2;
62 if (tmplen < sizeof smallbuf)
65 New(603, tmpbuf, tmplen + 1, char);
68 strcpy(tmpbuf + 2, name);
69 gv = *(GV**)hv_fetch(PL_defstash, tmpbuf, tmplen, TRUE);
71 gv_init(gv, PL_defstash, tmpbuf, tmplen, FALSE);
72 if (tmpbuf != smallbuf)
74 sv_setpv(GvSV(gv), name);
75 if (*name == '/' && (instr(name, "/lib/") || instr(name, ".pm")))
78 hv_magic(GvHVn(gv_AVadd(gv)), gv, 'L');
83 gv_init(GV *gv, HV *stash, char *name, STRLEN len, int multi)
87 bool doproto = SvTYPE(gv) > SVt_NULL;
88 char *proto = (doproto && SvPOK(gv)) ? SvPVX(gv) : NULL;
90 sv_upgrade((SV*)gv, SVt_PVGV);
100 GvGP(gv) = gp_ref(gp);
101 GvSV(gv) = NEWSV(72,0);
102 GvLINE(gv) = PL_curcop->cop_line;
103 GvFILEGV(gv) = PL_curcop->cop_filegv;
106 sv_magic((SV*)gv, (SV*)gv, '*', name, len);
107 GvSTASH(gv) = (HV*)SvREFCNT_inc(stash);
108 GvNAME(gv) = savepvn(name, len);
112 if (doproto) { /* Replicate part of newSUB here. */
115 /* XXX unsafe for threads if eval_owner isn't held */
116 start_subparse(0,0); /* Create CV in compcv. */
117 GvCV(gv) = PL_compcv;
121 CvGV(GvCV(gv)) = (GV*)SvREFCNT_inc(gv);
122 CvFILEGV(GvCV(gv)) = PL_curcop->cop_filegv;
123 CvSTASH(GvCV(gv)) = PL_curstash;
125 CvOWNER(GvCV(gv)) = 0;
126 if (!CvMUTEXP(GvCV(gv))) {
127 New(666, CvMUTEXP(GvCV(gv)), 1, perl_mutex);
128 MUTEX_INIT(CvMUTEXP(GvCV(gv)));
130 #endif /* USE_THREADS */
132 sv_setpv((SV*)GvCV(gv), proto);
139 gv_init_sv(GV *gv, I32 sv_type)
155 gv_fetchmeth(HV *stash, char *name, STRLEN len, I32 level)
165 if ((level > 100) || (level < -100))
166 croak("Recursive inheritance detected while looking for method '%s' in package '%s'",
167 name, HvNAME(stash));
169 DEBUG_o( deb("Looking for method %s in package %s\n",name,HvNAME(stash)) );
171 gvp = (GV**)hv_fetch(stash, name, len, (level >= 0));
176 if (SvTYPE(topgv) != SVt_PVGV)
177 gv_init(topgv, stash, name, len, TRUE);
178 if (cv = GvCV(topgv)) {
179 /* If genuine method or valid cache entry, use it */
180 if (!GvCVGEN(topgv) || GvCVGEN(topgv) == PL_sub_generation)
182 /* Stale cached entry: junk it */
184 GvCV(topgv) = cv = Nullcv;
187 else if (GvCVGEN(topgv) == PL_sub_generation)
188 return 0; /* cache indicates sub doesn't exist */
191 gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE);
192 av = (gvp && (gv = *gvp) && gv != (GV*)&PL_sv_undef) ? GvAV(gv) : Nullav;
194 /* create and re-create @.*::SUPER::ISA on demand */
195 if (!av || !SvMAGIC(av)) {
196 char* packname = HvNAME(stash);
197 STRLEN packlen = strlen(packname);
199 if (packlen >= 7 && strEQ(packname + packlen - 7, "::SUPER")) {
203 basestash = gv_stashpvn(packname, packlen, TRUE);
204 gvp = (GV**)hv_fetch(basestash, "ISA", 3, FALSE);
205 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
206 dTHR; /* just for SvREFCNT_dec */
207 gvp = (GV**)hv_fetch(stash, "ISA", 3, TRUE);
208 if (!gvp || !(gv = *gvp))
209 croak("Cannot create %s::ISA", HvNAME(stash));
210 if (SvTYPE(gv) != SVt_PVGV)
211 gv_init(gv, stash, "ISA", 3, TRUE);
212 SvREFCNT_dec(GvAV(gv));
213 GvAV(gv) = (AV*)SvREFCNT_inc(av);
219 SV** svp = AvARRAY(av);
220 /* NOTE: No support for tied ISA */
221 I32 items = AvFILLp(av) + 1;
224 HV* basestash = gv_stashsv(sv, FALSE);
226 dTHR; /* just for ckWARN */
227 if (ckWARN(WARN_MISC))
228 warner(WARN_MISC, "Can't locate package %s for @%s::ISA",
229 SvPVX(sv), HvNAME(stash));
232 gv = gv_fetchmeth(basestash, name, len,
233 (level >= 0) ? level + 1 : level - 1);
239 /* if at top level, try UNIVERSAL */
241 if (level == 0 || level == -1) {
244 if (lastchance = gv_stashpvn("UNIVERSAL", 9, FALSE)) {
245 if (gv = gv_fetchmeth(lastchance, name, len,
246 (level >= 0) ? level + 1 : level - 1)) {
249 * Cache method in topgv if:
250 * 1. topgv has no synonyms (else inheritance crosses wires)
251 * 2. method isn't a stub (else AUTOLOAD fails spectacularly)
254 GvREFCNT(topgv) == 1 &&
256 (CvROOT(cv) || CvXSUB(cv)))
258 if (cv = GvCV(topgv))
260 GvCV(topgv) = (CV*)SvREFCNT_inc(GvCV(gv));
261 GvCVGEN(topgv) = PL_sub_generation;
265 else if (topgv && GvREFCNT(topgv) == 1) {
266 /* cache the fact that the method is not defined */
267 GvCVGEN(topgv) = PL_sub_generation;
276 gv_fetchmethod(HV *stash, char *name)
278 return gv_fetchmethod_autoload(stash, name, TRUE);
282 gv_fetchmethod_autoload(HV *stash, char *name, I32 autoload)
289 for (nend = name; *nend; nend++) {
292 else if (*nend == ':' && *(nend + 1) == ':')
296 char *origname = name;
300 if ((nsplit - origname) == 5 && strnEQ(origname, "SUPER", 5)) {
301 /* ->SUPER::method should really be looked up in original stash */
302 SV *tmpstr = sv_2mortal(newSVpvf("%s::SUPER",
303 HvNAME(PL_curcop->cop_stash)));
304 stash = gv_stashpvn(SvPVX(tmpstr), SvCUR(tmpstr), TRUE);
305 DEBUG_o( deb("Treating %s as %s::%s\n",
306 origname, HvNAME(stash), name) );
309 stash = gv_stashpvn(origname, nsplit - origname, TRUE);
312 gv = gv_fetchmeth(stash, name, nend - name, 0);
314 if (strEQ(name,"import"))
315 gv = (GV*)&PL_sv_yes;
317 gv = gv_autoload4(stash, name, nend - name, TRUE);
321 if (!CvROOT(cv) && !CvXSUB(cv)) {
329 if (GvCV(stubgv) != cv) /* orphaned import */
332 autogv = gv_autoload4(GvSTASH(stubgv),
333 GvNAME(stubgv), GvNAMELEN(stubgv), TRUE);
343 gv_autoload4(HV *stash, char *name, STRLEN len, I32 method)
346 static char autoload[] = "AUTOLOAD";
347 static STRLEN autolen = 8;
354 if (len == autolen && strnEQ(name, autoload, autolen))
356 if (!(gv = gv_fetchmeth(stash, autoload, autolen, FALSE)))
361 * Inheriting AUTOLOAD for non-methods works ... for now.
363 if (ckWARN(WARN_DEPRECATED) && !method &&
364 (GvCVGEN(gv) || GvSTASH(gv) != stash))
365 warner(WARN_DEPRECATED,
366 "Use of inherited AUTOLOAD for non-method %s::%.*s() is deprecated",
367 HvNAME(stash), (int)len, name);
370 * Given &FOO::AUTOLOAD, set $FOO::AUTOLOAD to desired function name.
371 * The subroutine's original name may not be "AUTOLOAD", so we don't
372 * use that, but for lack of anything better we will use the sub's
373 * original package to look up $AUTOLOAD.
375 varstash = GvSTASH(CvGV(cv));
376 vargv = *(GV**)hv_fetch(varstash, autoload, autolen, TRUE);
378 gv_init(vargv, varstash, autoload, autolen, FALSE);
380 sv_setpv(varsv, HvNAME(stash));
381 sv_catpvn(varsv, "::", 2);
382 sv_catpvn(varsv, name, len);
383 SvTAINTED_off(varsv);
388 gv_stashpv(char *name, I32 create)
390 return gv_stashpvn(name, strlen(name), create);
394 gv_stashpvn(char *name, U32 namelen, I32 create)
401 if (namelen + 3 < sizeof smallbuf)
404 New(606, tmpbuf, namelen + 3, char);
405 Copy(name,tmpbuf,namelen,char);
406 tmpbuf[namelen++] = ':';
407 tmpbuf[namelen++] = ':';
408 tmpbuf[namelen] = '\0';
409 tmpgv = gv_fetchpv(tmpbuf, create, SVt_PVHV);
410 if (tmpbuf != smallbuf)
415 GvHV(tmpgv) = newHV();
418 HvNAME(stash) = savepv(name);
423 gv_stashsv(SV *sv, I32 create)
428 return gv_stashpvn(ptr, len, create);
433 gv_fetchpv(char *nambeg, I32 add, I32 sv_type)
436 register char *name = nambeg;
440 register char *namend;
444 if (*name == '*' && isALPHA(name[1])) /* accidental stringify on a GV? */
447 for (namend = name; *namend; namend++) {
448 if ((*namend == '\'' && namend[1]) ||
449 (*namend == ':' && namend[1] == ':'))
453 if (!stash || !SvREFCNT(stash)) /* symbol table under destruction */
461 if (len + 3 < sizeof smallbuf)
464 New(601, tmpbuf, len+3, char);
465 Copy(name, tmpbuf, len, char);
469 gvp = (GV**)hv_fetch(stash,tmpbuf,len,add);
470 gv = gvp ? *gvp : Nullgv;
471 if (gv && gv != (GV*)&PL_sv_undef) {
472 if (SvTYPE(gv) != SVt_PVGV)
473 gv_init(gv, stash, tmpbuf, len, (add & GV_ADDMULTI));
477 if (tmpbuf != smallbuf)
479 if (!gv || gv == (GV*)&PL_sv_undef)
482 if (!(stash = GvHV(gv)))
483 stash = GvHV(gv) = newHV();
486 HvNAME(stash) = savepvn(nambeg, namend - nambeg);
494 return gv ? gv : (GV*)*hv_fetch(PL_defstash, "main::", 6, TRUE);
501 /* No stash in name, so see how we can default */
505 || (IN_UTF8 && ((*name & 0xc0) == 0xc0) && isIDFIRST_utf8((U8*)name)))
509 if (isUPPER(*name)) {
510 if (*name == 'S' && (
511 strEQ(name, "SIG") ||
512 strEQ(name, "STDIN") ||
513 strEQ(name, "STDOUT") ||
514 strEQ(name, "STDERR")))
516 else if (*name == 'I' && strEQ(name, "INC"))
518 else if (*name == 'E' && strEQ(name, "ENV"))
520 else if (*name == 'A' && (
521 strEQ(name, "ARGV") ||
522 strEQ(name, "ARGVOUT")))
525 else if (*name == '_' && !name[1])
530 else if ((COP*)PL_curcop == &PL_compiling) {
532 if (add && (PL_hints & HINT_STRICT_VARS) &&
533 sv_type != SVt_PVCV &&
534 sv_type != SVt_PVGV &&
535 sv_type != SVt_PVFM &&
536 sv_type != SVt_PVIO &&
537 !(len == 1 && sv_type == SVt_PV && strchr("ab",*name)) )
539 gvp = (GV**)hv_fetch(stash,name,len,0);
541 *gvp == (GV*)&PL_sv_undef ||
542 SvTYPE(*gvp) != SVt_PVGV)
546 else if (sv_type == SVt_PV && !GvIMPORTED_SV(*gvp) ||
547 sv_type == SVt_PVAV && !GvIMPORTED_AV(*gvp) ||
548 sv_type == SVt_PVHV && !GvIMPORTED_HV(*gvp) )
550 warn("Variable \"%c%s\" is not imported",
551 sv_type == SVt_PVAV ? '@' :
552 sv_type == SVt_PVHV ? '%' : '$',
555 warn("(Did you mean &%s instead?)\n", name);
561 stash = PL_curcop->cop_stash;
567 /* By this point we should have a stash and a name */
573 char sv_type_char = ((sv_type == SVt_PV) ? '$'
574 : (sv_type == SVt_PVAV) ? '@'
575 : (sv_type == SVt_PVHV) ? '%'
578 warn("Global symbol \"%c%s\" requires explicit package name",
581 warn("Global symbol \"%s\" requires explicit package name",
585 stash = PL_curstash ? PL_curstash : PL_defstash; /* avoid core dumps */
586 add_gvflags = ((sv_type == SVt_PV) ? GVf_IMPORTED_SV
587 : (sv_type == SVt_PVAV) ? GVf_IMPORTED_AV
588 : (sv_type == SVt_PVHV) ? GVf_IMPORTED_HV
592 if (!SvREFCNT(stash)) /* symbol table under destruction */
595 gvp = (GV**)hv_fetch(stash,name,len,add);
596 if (!gvp || *gvp == (GV*)&PL_sv_undef)
599 if (SvTYPE(gv) == SVt_PVGV) {
602 gv_init_sv(gv, sv_type);
605 } else if (add & GV_NOINIT) {
609 /* Adding a new symbol */
611 if (add & GV_ADDWARN)
612 warn("Had to create %s unexpectedly", nambeg);
613 gv_init(gv, stash, name, len, add & GV_ADDMULTI);
614 gv_init_sv(gv, sv_type);
615 GvFLAGS(gv) |= add_gvflags;
617 /* set up magic where warranted */
620 if (strEQ(name, "ARGV")) {
621 IoFLAGS(GvIOn(gv)) |= IOf_ARGV|IOf_START;
631 if (strnEQ(name, "EXPORT", 6))
635 if (strEQ(name, "ISA")) {
638 sv_magic((SV*)av, (SV*)gv, 'I', Nullch, 0);
639 /* NOTE: No support for tied ISA */
640 if ((add & GV_ADDMULTI) && strEQ(nambeg,"AnyDBM_File::ISA")
641 && AvFILLp(av) == -1)
644 av_push(av, newSVpv(pname = "NDBM_File",0));
645 gv_stashpvn(pname, 9, TRUE);
646 av_push(av, newSVpv(pname = "DB_File",0));
647 gv_stashpvn(pname, 7, TRUE);
648 av_push(av, newSVpv(pname = "GDBM_File",0));
649 gv_stashpvn(pname, 9, TRUE);
650 av_push(av, newSVpv(pname = "SDBM_File",0));
651 gv_stashpvn(pname, 9, TRUE);
652 av_push(av, newSVpv(pname = "ODBM_File",0));
653 gv_stashpvn(pname, 9, TRUE);
659 if (strEQ(name, "OVERLOAD")) {
662 hv_magic(hv, gv, 'A');
665 #endif /* OVERLOAD */
667 if (strEQ(name, "SIG")) {
671 GvMULTI_on(PL_siggv);
672 hv = GvHVn(PL_siggv);
673 hv_magic(hv, PL_siggv, 'S');
674 for(i=1;PL_sig_name[i];i++) {
676 init=hv_fetch(hv,PL_sig_name[i],strlen(PL_sig_name[i]),1);
678 sv_setsv(*init,&PL_sv_undef);
689 PL_sawampersand = TRUE;
696 PL_sawampersand = TRUE;
703 PL_sawampersand = TRUE;
709 sv_setpv(GvSV(gv),PL_chopset);
715 #ifdef COMPLEX_STATUS
716 (void)SvUPGRADE(GvSV(gv), SVt_PVLV);
723 if (sv_type > SVt_PV && PL_curcop != &PL_compiling) {
724 HV* stash = gv_stashpvn("Errno",5,FALSE);
725 if(!stash || !(gv_fetchmethod(stash, "TIEHASH"))) {
728 perl_require_pv("Errno.pm");
730 stash = gv_stashpvn("Errno",5,FALSE);
731 if (!stash || !(gv_fetchmethod(stash, "TIEHASH")))
732 croak("Can't use %%! because Errno.pm is not available");
741 sv_magic((SV*)av, Nullsv, 'D', Nullch, 0);
746 if (ckWARN(WARN_DEPRECATED) && len == 1 && sv_type == SVt_PV)
747 warner(WARN_DEPRECATED, "Use of $%s is deprecated", name);
770 case '\011': /* NOT \t in EBCDIC */
788 sv_magic((SV*)av, (SV*)av, 'D', Nullch, 0);
801 SvREADONLY_on(GvSV(gv));
803 sv_magic(GvSV(gv), (SV*)gv, 0, name, len);
809 sv_setpv(GvSV(gv),"\f");
810 PL_formfeed = GvSV(gv);
815 sv_setpv(GvSV(gv),"\034");
820 (void)SvUPGRADE(sv, SVt_PVNV);
821 sv_setpv(sv, PL_patchlevel);
831 gv_fullname3(SV *sv, GV *gv, char *prefix)
833 HV *hv = GvSTASH(gv);
838 sv_setpv(sv, prefix ? prefix : "");
839 sv_catpv(sv,HvNAME(hv));
840 sv_catpvn(sv,"::", 2);
841 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
845 gv_efullname3(SV *sv, GV *gv, char *prefix)
850 gv_fullname3(sv, egv, prefix);
853 /* XXX compatibility with versions <= 5.003. */
855 gv_fullname(SV *sv, GV *gv)
857 gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : "");
860 /* XXX compatibility with versions <= 5.003. */
862 gv_efullname(SV *sv, GV *gv)
864 gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : "");
874 io = (IO*)NEWSV(0,0);
875 sv_upgrade((SV *)io,SVt_PVIO);
878 iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV);
879 /* unless exists($main::{FileHandle}) and defined(%main::FileHandle::) */
880 if (!(iogv && GvHV(iogv) && HvARRAY(GvHV(iogv))))
881 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
882 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
898 for (i = 0; i <= (I32) HvMAX(stash); i++) {
899 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
900 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
901 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)) && HvNAME(hv))
903 if (hv != PL_defstash)
904 gv_check(hv); /* nested package */
906 else if (isALPHA(*HeKEY(entry))) {
907 gv = (GV*)HeVAL(entry);
908 if (SvTYPE(gv) != SVt_PVGV || GvMULTI(gv))
910 PL_curcop->cop_line = GvLINE(gv);
911 filegv = GvFILEGV(gv);
912 PL_curcop->cop_filegv = filegv;
913 if (filegv && GvMULTI(filegv)) /* Filename began with slash */
916 "Name \"%s::%s\" used only once: possible typo",
917 HvNAME(stash), GvNAME(gv));
926 return gv_fetchpv(form("%s::_GEN_%ld", pack, (long)PL_gensym++),
930 /* hopefully this is only called on local symbol table entries */
938 /* multi-named GPs cannot be used for method cache */
939 SvREFCNT_dec(gp->gp_cv);
944 /* Adding a new name to a subroutine invalidates method cache */
957 if (!gv || !(gp = GvGP(gv)))
959 if (gp->gp_refcnt == 0) {
960 warn("Attempt to free unreferenced glob pointers");
964 /* Deleting the name of a subroutine invalidates method cache */
967 if (--gp->gp_refcnt > 0) {
968 if (gp->gp_egv == gv)
973 SvREFCNT_dec(gp->gp_sv);
974 SvREFCNT_dec(gp->gp_av);
975 SvREFCNT_dec(gp->gp_hv);
976 SvREFCNT_dec(gp->gp_io);
977 SvREFCNT_dec(gp->gp_cv);
978 SvREFCNT_dec(gp->gp_form);
984 #if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
988 #ifdef MICROPORT /* Microport 2.4 hack */
993 return GvGP(gv)->gp_av;
995 return GvGP(gv_AVadd(gv))->gp_av;
1001 if (GvGP(gv)->gp_hv)
1002 return GvGP(gv)->gp_hv;
1004 return GvGP(gv_HVadd(gv))->gp_hv;
1006 #endif /* Microport 2.4 hack */
1009 /* Updates and caches the CV's */
1012 Gv_AMupdate(HV *stash)
1019 MAGIC* mg=mg_find((SV*)stash,'c');
1020 AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL;
1024 if (mg && amtp->was_ok_am == PL_amagic_generation
1025 && amtp->was_ok_sub == PL_sub_generation)
1026 return AMT_AMAGIC(amtp);
1027 if (amtp && AMT_AMAGIC(amtp)) { /* Have table. */
1029 for (i=1; i<NofAMmeth; i++) {
1030 if (amtp->table[i]) {
1031 SvREFCNT_dec(amtp->table[i]);
1035 sv_unmagic((SV*)stash, 'c');
1037 DEBUG_o( deb("Recalcing overload magic in package %s\n",HvNAME(stash)) );
1039 amt.was_ok_am = PL_amagic_generation;
1040 amt.was_ok_sub = PL_sub_generation;
1041 amt.fallback = AMGfallNO;
1044 #ifdef OVERLOAD_VIA_HASH
1045 gvp=(GV**)hv_fetch(stash,"OVERLOAD",8,FALSE); /* A shortcut */
1046 if (gvp && ((gv = *gvp) != (GV*)&PL_sv_undef && (hv = GvHV(gv)))) {
1053 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
1055 if (( cp = (char *)PL_AMG_names[0] ) &&
1056 (svp = (SV**)hv_fetch(hv,cp,strlen(cp),FALSE)) && (sv = *svp)) {
1057 if (SvTRUE(sv)) amt.fallback=AMGfallYES;
1058 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
1060 for (i = 1; i < NofAMmeth; i++) {
1062 cp = (char *)PL_AMG_names[i];
1064 svp = (SV**)hv_fetch(hv, cp, strlen(cp), FALSE);
1065 if (svp && ((sv = *svp) != &PL_sv_undef)) {
1066 switch (SvTYPE(sv)) {
1069 if (!SvOK(sv)) break;
1070 gv = gv_fetchmethod(stash, SvPV(sv, n_a));
1071 if (gv) cv = GvCV(gv);
1075 if (SvTYPE(cv) == SVt_PVCV)
1080 croak("Not a subroutine reference in overload table");
1086 if (!(cv = GvCVu((GV*)sv)))
1087 cv = sv_2cv(sv, &stash, &gv, TRUE);
1092 croak("Method for operation %s not found in package %.256s during blessing\n",
1105 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
1107 if ( cp = PL_AMG_names[0] ) {
1108 /* Try to find via inheritance. */
1109 gv = gv_fetchmeth(stash, "()", 2, -1); /* A cookie: "()". */
1110 if (gv) sv = GvSV(gv);
1112 if (!gv) goto no_table;
1113 else if (SvTRUE(sv)) amt.fallback=AMGfallYES;
1114 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
1117 for (i = 1; i < NofAMmeth; i++) {
1118 SV *cookie = sv_2mortal(newSVpvf("(%s", cp = PL_AMG_names[i]));
1119 DEBUG_o( deb("Checking overloading of `%s' in package `%.256s'\n",
1120 cp, HvNAME(stash)) );
1121 /* don't fill the cache while looking up! */
1122 gv = gv_fetchmeth(stash, SvPVX(cookie), SvCUR(cookie), -1);
1124 if(gv && (cv = GvCV(gv))) {
1125 if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil")
1126 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) {
1127 /* GvSV contains the name of the method. */
1130 DEBUG_o( deb("Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n",
1131 SvPV(GvSV(gv), n_a), cp, HvNAME(stash)) );
1132 if (!SvPOK(GvSV(gv))
1133 || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)),
1136 /* Can be an import stub (created by `can'). */
1138 croak("Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'",
1139 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1142 croak("Cannot resolve method `%.256s' overloading `%s' in package `%.256s'",
1143 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1146 cv = GvCV(gv = ngv);
1148 DEBUG_o( deb("Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n",
1149 cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))),
1150 GvNAME(CvGV(cv))) );
1154 amt.table[i]=(CV*)SvREFCNT_inc(cv);
1157 AMT_AMAGIC_on(&amt);
1158 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMT));
1162 /* Here we have no table: */
1164 AMT_AMAGIC_off(&amt);
1165 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMTS));
1170 amagic_call(SV *left, SV *right, int method, int flags)
1175 CV **cvp=NULL, **ocvp=NULL;
1177 int fl=0, off, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
1178 int postpr = 0, force_cpy = 0, assignshift = assign ? 1 : 0;
1180 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
1181 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),'c'))
1182 && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1183 ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table
1185 && ((cv = cvp[off=method+assignshift])
1186 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
1188 (fl = 1, cv = cvp[off=method])))) {
1189 lr = -1; /* Call method for left argument */
1191 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
1194 /* look for substituted methods */
1195 /* In all the covered cases we should be called with assign==0. */
1199 if ((cv = cvp[off=add_ass_amg])
1200 || ((cv = cvp[off = add_amg]) && (force_cpy = 0, postpr = 1))) {
1201 right = &PL_sv_yes; lr = -1; assign = 1;
1206 if ((cv = cvp[off = subtr_ass_amg])
1207 || ((cv = cvp[off = subtr_amg]) && (force_cpy = 0, postpr=1))) {
1208 right = &PL_sv_yes; lr = -1; assign = 1;
1212 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
1215 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1218 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1221 (void)((cv = cvp[off=bool__amg])
1222 || (cv = cvp[off=numer_amg])
1223 || (cv = cvp[off=string_amg]));
1229 * SV* ref causes confusion with the interpreter variable of
1232 SV* tmpRef=SvRV(left);
1233 if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) {
1235 * Just to be extra cautious. Maybe in some
1236 * additional cases sv_setsv is safe, too.
1238 SV* newref = newSVsv(tmpRef);
1239 SvOBJECT_on(newref);
1240 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(tmpRef));
1246 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
1247 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
1248 SV* nullsv=sv_2mortal(newSViv(0));
1250 SV* lessp = amagic_call(left,nullsv,
1251 lt_amg,AMGf_noright);
1252 logic = SvTRUE(lessp);
1254 SV* lessp = amagic_call(left,nullsv,
1255 ncmp_amg,AMGf_noright);
1256 logic = (SvNV(lessp) < 0);
1259 if (off==subtr_amg) {
1270 if (cv = cvp[off=subtr_amg]) {
1272 left = sv_2mortal(newSViv(0));
1276 case iter_amg: /* XXXX Eventually should do to_gv. */
1283 return NULL; /* Delegate operation to standard mechanisms. */
1288 if (!cv) goto not_found;
1289 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1290 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c'))
1291 && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1292 ? (amtp = (AMT*)mg->mg_ptr)->table
1294 && (cv = cvp[off=method])) { /* Method for right
1297 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
1298 && (cvp=ocvp) && (lr = -1))
1299 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1300 && !(flags & AMGf_unary)) {
1301 /* We look for substitution for
1302 * comparison operations and
1304 if (method==concat_amg || method==concat_ass_amg
1305 || method==repeat_amg || method==repeat_ass_amg) {
1306 return NULL; /* Delegate operation to string conversion */
1316 postpr = 1; off=ncmp_amg; break;
1323 postpr = 1; off=scmp_amg; break;
1325 if (off != -1) cv = cvp[off];
1330 not_found: /* No method found, either report or croak */
1331 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1332 notfound = 1; lr = -1;
1333 } else if (cvp && (cv=cvp[nomethod_amg])) {
1334 notfound = 1; lr = 1;
1337 if (off==-1) off=method;
1338 msg = sv_2mortal(newSVpvf(
1339 "Operation `%s': no method found,%sargument %s%s%s%s",
1340 PL_AMG_names[method + assignshift],
1341 (flags & AMGf_unary ? " " : "\n\tleft "),
1343 "in overloaded package ":
1344 "has no overloaded magic",
1346 HvNAME(SvSTASH(SvRV(left))):
1349 ",\n\tright argument in overloaded package ":
1352 : ",\n\tright argument has no overloaded magic"),
1354 HvNAME(SvSTASH(SvRV(right))):
1356 if (amtp && amtp->fallback >= AMGfallYES) {
1357 DEBUG_o( deb("%s", SvPVX(msg)) );
1363 force_cpy = force_cpy || assign;
1368 "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n",
1370 method+assignshift==off? "" :
1372 method+assignshift==off? "" :
1373 PL_AMG_names[method+assignshift],
1374 method+assignshift==off? "" : "')",
1375 flags & AMGf_unary? "" :
1376 lr==1 ? " for right argument": " for left argument",
1377 flags & AMGf_unary? " for argument" : "",
1379 fl? ",\n\tassignment variant used": "") );
1381 /* Since we use shallow copy during assignment, we need
1382 * to dublicate the contents, probably calling user-supplied
1383 * version of copy operator
1385 /* We need to copy in following cases:
1386 * a) Assignment form was called.
1387 * assignshift==1, assign==T, method + 1 == off
1388 * b) Increment or decrement, called directly.
1389 * assignshift==0, assign==0, method + 0 == off
1390 * c) Increment or decrement, translated to assignment add/subtr.
1391 * assignshift==0, assign==T,
1393 * d) Increment or decrement, translated to nomethod.
1394 * assignshift==0, assign==0,
1396 * e) Assignment form translated to nomethod.
1397 * assignshift==1, assign==T, method + 1 != off
1400 /* off is method, method+assignshift, or a result of opcode substitution.
1401 * In the latter case assignshift==0, so only notfound case is important.
1403 if (( (method + assignshift == off)
1404 && (assign || (method == inc_amg) || (method == dec_amg)))
1411 bool oldcatch = CATCH_GET;
1414 Zero(&myop, 1, BINOP);
1415 myop.op_last = (OP *) &myop;
1416 myop.op_next = Nullop;
1417 myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED;
1419 PUSHSTACKi(PERLSI_OVERLOAD);
1422 PL_op = (OP *) &myop;
1423 if (PERLDB_SUB && PL_curstash != PL_debstash)
1424 PL_op->op_private |= OPpENTERSUB_DB;
1428 EXTEND(SP, notfound + 5);
1429 PUSHs(lr>0? right: left);
1430 PUSHs(lr>0? left: right);
1431 PUSHs( lr > 0 ? &PL_sv_yes : ( assign ? &PL_sv_undef : &PL_sv_no ));
1433 PUSHs( sv_2mortal(newSVpv((char *)PL_AMG_names[method + assignshift],0)));
1438 if (PL_op = pp_entersub(ARGS))
1446 CATCH_SET(oldcatch);
1453 ans=SvIV(res)<=0; break;
1456 ans=SvIV(res)<0; break;
1459 ans=SvIV(res)>=0; break;
1462 ans=SvIV(res)>0; break;
1465 ans=SvIV(res)==0; break;
1468 ans=SvIV(res)!=0; break;
1471 SvSetSV(left,res); return left;
1473 ans=!SvOK(res); break;
1476 } else if (method==copy_amg) {
1478 croak("Copy method did not return a reference");
1480 return SvREFCNT_inc(SvRV(res));
1486 #endif /* OVERLOAD */