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,'
25 gv_AVadd(register GV *gv)
27 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
28 croak("Bad symbol for array");
35 gv_HVadd(register GV *gv)
37 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
38 croak("Bad symbol for hash");
45 gv_IOadd(register GV *gv)
47 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
48 croak("Bad symbol for filehandle");
55 gv_fetchfile(char *name)
63 tmplen = strlen(name) + 2;
64 if (tmplen < sizeof smallbuf)
67 New(603, tmpbuf, tmplen + 1, char);
70 strcpy(tmpbuf + 2, name);
71 gv = *(GV**)hv_fetch(defstash, tmpbuf, tmplen, TRUE);
73 gv_init(gv, defstash, tmpbuf, tmplen, FALSE);
74 if (tmpbuf != smallbuf)
76 sv_setpv(GvSV(gv), name);
77 if (*name == '/' && (instr(name, "/lib/") || instr(name, ".pm")))
80 hv_magic(GvHVn(gv_AVadd(gv)), gv, 'L');
85 gv_init(GV *gv, HV *stash, char *name, STRLEN len, int multi)
90 sv_upgrade((SV*)gv, SVt_PVGV);
94 GvGP(gv) = gp_ref(gp);
95 GvSV(gv) = NEWSV(72,0);
96 GvLINE(gv) = curcop->cop_line;
97 GvFILEGV(gv) = curcop->cop_filegv;
99 sv_magic((SV*)gv, (SV*)gv, '*', name, len);
100 GvSTASH(gv) = (HV*)SvREFCNT_inc(stash);
101 GvNAME(gv) = savepvn(name, len);
108 gv_init_sv(GV *gv, I32 sv_type)
124 gv_fetchmeth(HV *stash, char *name, STRLEN len, I32 level)
134 if ((level > 100) || (level < -100))
135 croak("Recursive inheritance detected");
137 DEBUG_o( deb("Looking for method %s in package %s\n",name,HvNAME(stash)) );
139 gvp = (GV**)hv_fetch(stash, name, len, (level >= 0));
144 if (SvTYPE(topgv) != SVt_PVGV)
145 gv_init(topgv, stash, name, len, TRUE);
146 if (cv = GvCV(topgv)) {
147 /* If genuine method or valid cache entry, use it */
148 if (!GvCVGEN(topgv) || GvCVGEN(topgv) >= sub_generation)
150 /* Stale cached entry: junk it */
152 GvCV(topgv) = cv = Nullcv;
157 gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE);
158 av = (gvp && (gv = *gvp) && gv != (GV*)&sv_undef) ? GvAV(gv) : Nullav;
160 /* create and re-create @.*::SUPER::ISA on demand */
161 if (!av || !SvMAGIC(av)) {
162 char* packname = HvNAME(stash);
163 STRLEN packlen = strlen(packname);
165 if (packlen >= 7 && strEQ(packname + packlen - 7, "::SUPER")) {
169 basestash = gv_stashpvn(packname, packlen, TRUE);
170 gvp = (GV**)hv_fetch(basestash, "ISA", 3, FALSE);
171 if (gvp && (gv = *gvp) != (GV*)&sv_undef && (av = GvAV(gv))) {
172 dTHR; /* just for SvREFCNT_dec */
173 gvp = (GV**)hv_fetch(stash, "ISA", 3, TRUE);
174 if (!gvp || !(gv = *gvp))
175 croak("Cannot create %s::ISA", HvNAME(stash));
176 if (SvTYPE(gv) != SVt_PVGV)
177 gv_init(gv, stash, "ISA", 3, TRUE);
178 SvREFCNT_dec(GvAV(gv));
179 GvAV(gv) = (AV*)SvREFCNT_inc(av);
185 SV** svp = AvARRAY(av);
186 /* NOTE: No support for tied ISA */
187 I32 items = AvFILLp(av) + 1;
190 HV* basestash = gv_stashsv(sv, FALSE);
193 warn("Can't locate package %s for @%s::ISA",
194 SvPVX(sv), HvNAME(stash));
197 gv = gv_fetchmeth(basestash, name, len,
198 (level >= 0) ? level + 1 : level - 1);
204 /* if at top level, try UNIVERSAL */
206 if (level == 0 || level == -1) {
209 if (lastchance = gv_stashpvn("UNIVERSAL", 9, FALSE)) {
210 if (gv = gv_fetchmeth(lastchance, name, len,
211 (level >= 0) ? level + 1 : level - 1)) {
214 * Cache method in topgv if:
215 * 1. topgv has no synonyms (else inheritance crosses wires)
216 * 2. method isn't a stub (else AUTOLOAD fails spectacularly)
219 GvREFCNT(topgv) == 1 &&
221 (CvROOT(cv) || CvXSUB(cv)))
223 if (cv = GvCV(topgv))
225 GvCV(topgv) = (CV*)SvREFCNT_inc(GvCV(gv));
226 GvCVGEN(topgv) = sub_generation;
237 gv_fetchmethod(HV *stash, char *name)
239 return gv_fetchmethod_autoload(stash, name, TRUE);
243 gv_fetchmethod_autoload(HV *stash, char *name, I32 autoload)
250 for (nend = name; *nend; nend++) {
253 else if (*nend == ':' && *(nend + 1) == ':')
257 char *origname = name;
261 if ((nsplit - origname) == 5 && strnEQ(origname, "SUPER", 5)) {
262 /* ->SUPER::method should really be looked up in original stash */
263 SV *tmpstr = sv_2mortal(newSVpvf("%s::SUPER",
264 HvNAME(curcop->cop_stash)));
265 stash = gv_stashpvn(SvPVX(tmpstr), SvCUR(tmpstr), TRUE);
266 DEBUG_o( deb("Treating %s as %s::%s\n",
267 origname, HvNAME(stash), name) );
270 stash = gv_stashpvn(origname, nsplit - origname, TRUE);
273 gv = gv_fetchmeth(stash, name, nend - name, 0);
275 if (strEQ(name,"import"))
278 gv = gv_autoload4(stash, name, nend - name, TRUE);
282 if (!CvROOT(cv) && !CvXSUB(cv)) {
290 if (GvCV(stubgv) != cv) /* orphaned import */
293 autogv = gv_autoload4(GvSTASH(stubgv),
294 GvNAME(stubgv), GvNAMELEN(stubgv), TRUE);
304 gv_autoload4(HV *stash, char *name, STRLEN len, I32 method)
306 static char autoload[] = "AUTOLOAD";
307 static STRLEN autolen = 8;
314 if (len == autolen && strnEQ(name, autoload, autolen))
316 if (!(gv = gv_fetchmeth(stash, autoload, autolen, FALSE)))
321 * Inheriting AUTOLOAD for non-methods works ... for now.
323 if (dowarn && !method && (GvCVGEN(gv) || GvSTASH(gv) != stash))
325 "Use of inherited AUTOLOAD for non-method %s::%.*s() is deprecated",
326 HvNAME(stash), (int)len, name);
329 * Given &FOO::AUTOLOAD, set $FOO::AUTOLOAD to desired function name.
330 * The subroutine's original name may not be "AUTOLOAD", so we don't
331 * use that, but for lack of anything better we will use the sub's
332 * original package to look up $AUTOLOAD.
334 varstash = GvSTASH(CvGV(cv));
335 vargv = *(GV**)hv_fetch(varstash, autoload, autolen, TRUE);
337 gv_init(vargv, varstash, autoload, autolen, FALSE);
339 sv_setpv(varsv, HvNAME(stash));
340 sv_catpvn(varsv, "::", 2);
341 sv_catpvn(varsv, name, len);
342 SvTAINTED_off(varsv);
347 gv_stashpv(char *name, I32 create)
349 return gv_stashpvn(name, strlen(name), create);
353 gv_stashpvn(char *name, U32 namelen, I32 create)
360 if (namelen + 3 < sizeof smallbuf)
363 New(606, tmpbuf, namelen + 3, char);
364 Copy(name,tmpbuf,namelen,char);
365 tmpbuf[namelen++] = ':';
366 tmpbuf[namelen++] = ':';
367 tmpbuf[namelen] = '\0';
368 tmpgv = gv_fetchpv(tmpbuf, create, SVt_PVHV);
369 if (tmpbuf != smallbuf)
374 GvHV(tmpgv) = newHV();
377 HvNAME(stash) = savepv(name);
382 gv_stashsv(SV *sv, I32 create)
387 return gv_stashpvn(ptr, len, create);
392 gv_fetchpv(char *nambeg, I32 add, I32 sv_type)
395 register char *name = nambeg;
399 register char *namend;
403 if (*name == '*' && isALPHA(name[1])) /* accidental stringify on a GV? */
406 for (namend = name; *namend; namend++) {
407 if ((*namend == '\'' && namend[1]) ||
408 (*namend == ':' && namend[1] == ':'))
412 if (!stash || !SvREFCNT(stash)) /* symbol table under destruction */
420 if (len < sizeof(autobuf) - 2)
423 New(601, tmpbuf, len+3, char);
424 Copy(name, tmpbuf, len, char);
428 gvp = (GV**)hv_fetch(stash,tmpbuf,len,add);
429 gv = gvp ? *gvp : Nullgv;
430 if (gv && gv != (GV*)&sv_undef) {
431 if (SvTYPE(gv) != SVt_PVGV)
432 gv_init(gv, stash, tmpbuf, len, (add & 2));
436 if (tmpbuf != autobuf)
438 if (!gv || gv == (GV*)&sv_undef)
441 if (!(stash = GvHV(gv)))
442 stash = GvHV(gv) = newHV();
445 HvNAME(stash) = savepvn(nambeg, namend - nambeg);
453 return gv ? gv : (GV*)*hv_fetch(defstash, "main::", 6, TRUE);
460 /* No stash in name, so see how we can default */
463 if (isIDFIRST(*name)) {
466 if (isUPPER(*name)) {
468 if (*name == 'S' && (
469 strEQ(name, "SIG") ||
470 strEQ(name, "STDIN") ||
471 strEQ(name, "STDOUT") ||
472 strEQ(name, "STDERR") ))
475 else if (*name > 'E') {
476 if (*name == 'I' && strEQ(name, "INC"))
479 else if (*name > 'A') {
480 if (*name == 'E' && strEQ(name, "ENV"))
483 else if (*name == 'A' && (
484 strEQ(name, "ARGV") ||
485 strEQ(name, "ARGVOUT") ))
488 else if (*name == '_' && !name[1])
493 else if ((COP*)curcop == &compiling) {
495 if (add && (hints & HINT_STRICT_VARS) &&
496 sv_type != SVt_PVCV &&
497 sv_type != SVt_PVGV &&
498 sv_type != SVt_PVFM &&
499 sv_type != SVt_PVIO &&
500 !(len == 1 && sv_type == SVt_PV && strchr("ab",*name)) )
502 gvp = (GV**)hv_fetch(stash,name,len,0);
504 *gvp == (GV*)&sv_undef ||
505 SvTYPE(*gvp) != SVt_PVGV)
509 else if (sv_type == SVt_PV && !GvIMPORTED_SV(*gvp) ||
510 sv_type == SVt_PVAV && !GvIMPORTED_AV(*gvp) ||
511 sv_type == SVt_PVHV && !GvIMPORTED_HV(*gvp) )
513 warn("Variable \"%c%s\" is not imported",
514 sv_type == SVt_PVAV ? '@' :
515 sv_type == SVt_PVHV ? '%' : '$',
518 warn("(Did you mean &%s instead?)\n", name);
524 stash = curcop->cop_stash;
530 /* By this point we should have a stash and a name */
534 warn("Global symbol \"%s\" requires explicit package name", name);
536 stash = curstash ? curstash : defstash; /* avoid core dumps */
537 add_gvflags = ((sv_type == SVt_PV) ? GVf_IMPORTED_SV
538 : (sv_type == SVt_PVAV) ? GVf_IMPORTED_AV
539 : (sv_type == SVt_PVHV) ? GVf_IMPORTED_HV
546 if (!SvREFCNT(stash)) /* symbol table under destruction */
549 gvp = (GV**)hv_fetch(stash,name,len,add);
550 if (!gvp || *gvp == (GV*)&sv_undef)
553 if (SvTYPE(gv) == SVt_PVGV) {
556 gv_init_sv(gv, sv_type);
561 /* Adding a new symbol */
564 warn("Had to create %s unexpectedly", nambeg);
565 gv_init(gv, stash, name, len, add & 2);
566 gv_init_sv(gv, sv_type);
567 GvFLAGS(gv) |= add_gvflags;
569 /* set up magic where warranted */
572 if (strEQ(name, "ARGV")) {
573 IoFLAGS(GvIOn(gv)) |= IOf_ARGV|IOf_START;
583 if (strnEQ(name, "EXPORT", 6))
587 if (strEQ(name, "ISA")) {
590 sv_magic((SV*)av, (SV*)gv, 'I', Nullch, 0);
591 /* NOTE: No support for tied ISA */
592 if (add & 2 && strEQ(nambeg,"AnyDBM_File::ISA") && AvFILLp(av) == -1)
595 av_push(av, newSVpv(pname = "NDBM_File",0));
596 gv_stashpvn(pname, 9, TRUE);
597 av_push(av, newSVpv(pname = "DB_File",0));
598 gv_stashpvn(pname, 7, TRUE);
599 av_push(av, newSVpv(pname = "GDBM_File",0));
600 gv_stashpvn(pname, 9, TRUE);
601 av_push(av, newSVpv(pname = "SDBM_File",0));
602 gv_stashpvn(pname, 9, TRUE);
603 av_push(av, newSVpv(pname = "ODBM_File",0));
604 gv_stashpvn(pname, 9, TRUE);
610 if (strEQ(name, "OVERLOAD")) {
613 hv_magic(hv, gv, 'A');
616 #endif /* OVERLOAD */
618 if (strEQ(name, "SIG")) {
624 hv_magic(hv, siggv, 'S');
625 for(i=1;sig_name[i];i++) {
627 init=hv_fetch(hv,sig_name[i],strlen(sig_name[i]),1);
629 sv_setsv(*init,&sv_undef);
633 /* initialize signal stack */
634 signalstack = newAV();
635 AvREAL_off(signalstack);
636 av_extend(signalstack, 30);
637 av_fill(signalstack, 0);
665 sv_setpv(GvSV(gv),chopset);
671 #ifdef COMPLEX_STATUS
672 sv_upgrade(GvSV(gv), SVt_PVLV);
678 if (dowarn && len == 1 && sv_type == SVt_PV)
679 warn("Use of $%s is deprecated", name);
723 SvREADONLY_on(GvSV(gv));
725 sv_magic(GvSV(gv), (SV*)gv, 0, name, len);
731 sv_setpv(GvSV(gv),"\f");
737 sv_setpv(GvSV(gv),"\034");
742 sv_upgrade(sv, SVt_PVNV);
743 sv_setpv(sv, patchlevel);
753 gv_fullname3(SV *sv, GV *gv, char *prefix)
755 HV *hv = GvSTASH(gv);
760 sv_setpv(sv, prefix ? prefix : "");
761 sv_catpv(sv,HvNAME(hv));
762 sv_catpvn(sv,"::", 2);
763 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
767 gv_efullname3(SV *sv, GV *gv, char *prefix)
772 gv_fullname3(sv, egv, prefix);
775 /* XXX compatibility with versions <= 5.003. */
777 gv_fullname(SV *sv, GV *gv)
779 gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : "");
782 /* XXX compatibility with versions <= 5.003. */
784 gv_efullname(SV *sv, GV *gv)
786 gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : "");
796 io = (IO*)NEWSV(0,0);
797 sv_upgrade((SV *)io,SVt_PVIO);
800 iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV);
802 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
803 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
819 for (i = 0; i <= (I32) HvMAX(stash); i++) {
820 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
821 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
822 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)) && HvNAME(hv))
825 gv_check(hv); /* nested package */
827 else if (isALPHA(*HeKEY(entry))) {
828 gv = (GV*)HeVAL(entry);
831 curcop->cop_line = GvLINE(gv);
832 filegv = GvFILEGV(gv);
833 curcop->cop_filegv = filegv;
834 if (filegv && GvMULTI(filegv)) /* Filename began with slash */
836 warn("Name \"%s::%s\" used only once: possible typo",
837 HvNAME(stash), GvNAME(gv));
846 return gv_fetchpv(form("%s::_GEN_%ld", pack, (long)gensym++),
850 /* hopefully this is only called on local symbol table entries */
858 /* multi-named GPs cannot be used for method cache */
859 SvREFCNT_dec(gp->gp_cv);
864 /* Adding a new name to a subroutine invalidates method cache */
877 if (!gv || !(gp = GvGP(gv)))
879 if (gp->gp_refcnt == 0) {
880 warn("Attempt to free unreferenced glob pointers");
884 /* Deleting the name of a subroutine invalidates method cache */
887 if (--gp->gp_refcnt > 0) {
888 if (gp->gp_egv == gv)
893 SvREFCNT_dec(gp->gp_sv);
894 SvREFCNT_dec(gp->gp_av);
895 SvREFCNT_dec(gp->gp_hv);
896 SvREFCNT_dec(gp->gp_io);
897 SvREFCNT_dec(gp->gp_cv);
898 SvREFCNT_dec(gp->gp_form);
904 #if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
908 #ifdef MICROPORT /* Microport 2.4 hack */
913 return GvGP(gv)->gp_av;
915 return GvGP(gv_AVadd(gv))->gp_av;
922 return GvGP(gv)->gp_hv;
924 return GvGP(gv_HVadd(gv))->gp_hv;
926 #endif /* Microport 2.4 hack */
929 /* Updates and caches the CV's */
932 Gv_AMupdate(HV *stash)
939 MAGIC* mg=mg_find((SV*)stash,'c');
940 AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL;
943 if (mg && amtp->was_ok_am == amagic_generation
944 && amtp->was_ok_sub == sub_generation)
945 return AMT_AMAGIC(amtp);
946 if (amtp && AMT_AMAGIC(amtp)) { /* Have table. */
948 for (i=1; i<NofAMmeth; i++) {
949 if (amtp->table[i]) {
950 SvREFCNT_dec(amtp->table[i]);
954 sv_unmagic((SV*)stash, 'c');
956 DEBUG_o( deb("Recalcing overload magic in package %s\n",HvNAME(stash)) );
958 amt.was_ok_am = amagic_generation;
959 amt.was_ok_sub = sub_generation;
960 amt.fallback = AMGfallNO;
963 #ifdef OVERLOAD_VIA_HASH
964 gvp=(GV**)hv_fetch(stash,"OVERLOAD",8,FALSE); /* A shortcut */
965 if (gvp && ((gv = *gvp) != (GV*)&sv_undef && (hv = GvHV(gv)))) {
972 /* Work with "fallback" key, which we assume to be first in AMG_names */
974 if (( cp = (char *)AMG_names[0] ) &&
975 (svp = (SV**)hv_fetch(hv,cp,strlen(cp),FALSE)) && (sv = *svp)) {
976 if (SvTRUE(sv)) amt.fallback=AMGfallYES;
977 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
979 for (i = 1; i < NofAMmeth; i++) {
981 cp = (char *)AMG_names[i];
983 svp = (SV**)hv_fetch(hv, cp, strlen(cp), FALSE);
984 if (svp && ((sv = *svp) != &sv_undef)) {
985 switch (SvTYPE(sv)) {
988 if (!SvOK(sv)) break;
989 gv = gv_fetchmethod(stash, SvPV(sv, na));
990 if (gv) cv = GvCV(gv);
994 if (SvTYPE(cv) == SVt_PVCV)
999 croak("Not a subroutine reference in overload table");
1005 if (!(cv = GvCVu((GV*)sv)))
1006 cv = sv_2cv(sv, &stash, &gv, TRUE);
1011 croak("Method for operation %s not found in package %.256s during blessing\n",
1024 /* Work with "fallback" key, which we assume to be first in AMG_names */
1026 if ( cp = AMG_names[0] ) {
1027 /* Try to find via inheritance. */
1028 gv = gv_fetchmeth(stash, "()", 2, -1); /* A cookie: "()". */
1029 if (gv) sv = GvSV(gv);
1031 if (!gv) goto no_table;
1032 else if (SvTRUE(sv)) amt.fallback=AMGfallYES;
1033 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
1036 for (i = 1; i < NofAMmeth; i++) {
1037 SV *cookie = sv_2mortal(newSVpvf("(%s", cp = AMG_names[i]));
1038 DEBUG_o( deb("Checking overloading of `%s' in package `%.256s'\n",
1039 cp, HvNAME(stash)) );
1040 /* don't fill the cache while looking up! */
1041 gv = gv_fetchmeth(stash, SvPVX(cookie), SvCUR(cookie), -1);
1043 if(gv && (cv = GvCV(gv))) {
1044 if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil")
1045 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) {
1046 /* GvSV contains the name of the method. */
1049 DEBUG_o( deb("Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n",
1050 SvPV(GvSV(gv), na), cp, HvNAME(stash)) );
1051 if (!SvPOK(GvSV(gv))
1052 || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)),
1055 /* Can be an import stub (created by `can'). */
1057 croak("Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'",
1058 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1061 croak("Cannot resolve method `%.256s' overloading `%s' in package `%.256s'",
1062 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1065 cv = GvCV(gv = ngv);
1067 DEBUG_o( deb("Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n",
1068 cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))),
1069 GvNAME(CvGV(cv))) );
1073 amt.table[i]=(CV*)SvREFCNT_inc(cv);
1076 AMT_AMAGIC_on(&amt);
1077 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMT));
1081 /* Here we have no table: */
1083 AMT_AMAGIC_off(&amt);
1084 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMTS));
1088 /* During call to this subroutine stack can be reallocated. It is
1089 * advised to call SPAGAIN macro in your code after call */
1092 amagic_call(SV *left, SV *right, int method, int flags)
1097 CV **cvp=NULL, **ocvp=NULL;
1099 int fl=0, off, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
1100 int postpr=0, inc_dec_ass=0, assignshift=assign?1:0;
1102 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
1103 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),'c'))
1104 && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1105 ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table
1107 && ((cv = cvp[off=method+assignshift])
1108 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
1110 (fl = 1, cv = cvp[off=method])))) {
1111 lr = -1; /* Call method for left argument */
1113 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
1116 /* look for substituted methods */
1119 if (((cv = cvp[off=add_ass_amg]) && (inc_dec_ass=1))
1120 || ((cv = cvp[off=add_amg]) && (postpr=1))) {
1121 right = &sv_yes; lr = -1; assign = 1;
1125 if (((cv = cvp[off=subtr_ass_amg]) && (inc_dec_ass=1))
1126 || ((cv = cvp[off=subtr_amg]) && (postpr=1))) {
1127 right = &sv_yes; lr = -1; assign = 1;
1131 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
1134 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1137 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1140 (void)((cv = cvp[off=bool__amg])
1141 || (cv = cvp[off=numer_amg])
1142 || (cv = cvp[off=string_amg]));
1148 if (!SvROK(ref) && SvTYPE(ref) <= SVt_PVMG) {
1150 * Just to be extra cautious. Maybe in some
1151 * additional cases sv_setsv is safe, too.
1153 SV* newref = newSVsv(ref);
1154 SvOBJECT_on(newref);
1155 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(ref));
1161 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
1162 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
1163 SV* nullsv=sv_2mortal(newSViv(0));
1165 SV* lessp = amagic_call(left,nullsv,
1166 lt_amg,AMGf_noright);
1167 logic = SvTRUE(lessp);
1169 SV* lessp = amagic_call(left,nullsv,
1170 ncmp_amg,AMGf_noright);
1171 logic = (SvNV(lessp) < 0);
1174 if (off==subtr_amg) {
1185 if (cv = cvp[off=subtr_amg]) {
1187 left = sv_2mortal(newSViv(0));
1194 if (!cv) goto not_found;
1195 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1196 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c'))
1197 && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1198 ? (amtp = (AMT*)mg->mg_ptr)->table
1200 && (cv = cvp[off=method])) { /* Method for right
1203 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
1204 && (cvp=ocvp) && (lr = -1))
1205 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1206 && !(flags & AMGf_unary)) {
1207 /* We look for substitution for
1208 * comparison operations and
1210 if (method==concat_amg || method==concat_ass_amg
1211 || method==repeat_amg || method==repeat_ass_amg) {
1212 return NULL; /* Delegate operation to string conversion */
1222 postpr = 1; off=ncmp_amg; break;
1229 postpr = 1; off=scmp_amg; break;
1231 if (off != -1) cv = cvp[off];
1236 not_found: /* No method found, either report or croak */
1237 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1238 notfound = 1; lr = -1;
1239 } else if (cvp && (cv=cvp[nomethod_amg])) {
1240 notfound = 1; lr = 1;
1243 if (off==-1) off=method;
1244 msg = sv_2mortal(newSVpvf(
1245 "Operation `%s': no method found,%sargument %s%s%s%s",
1246 AMG_names[method + assignshift],
1247 (flags & AMGf_unary ? " " : "\n\tleft "),
1249 "in overloaded package ":
1250 "has no overloaded magic",
1252 HvNAME(SvSTASH(SvRV(left))):
1255 ",\n\tright argument in overloaded package ":
1258 : ",\n\tright argument has no overloaded magic"),
1260 HvNAME(SvSTASH(SvRV(right))):
1262 if (amtp && amtp->fallback >= AMGfallYES) {
1263 DEBUG_o( deb("%s", SvPVX(msg)) );
1273 "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n",
1275 method+assignshift==off? "" :
1277 method+assignshift==off? "" :
1278 AMG_names[method+assignshift],
1279 method+assignshift==off? "" : "')",
1280 flags & AMGf_unary? "" :
1281 lr==1 ? " for right argument": " for left argument",
1282 flags & AMGf_unary? " for argument" : "",
1284 fl? ",\n\tassignment variant used": "") );
1285 /* Since we use shallow copy during assignment, we need
1286 * to dublicate the contents, probably calling user-supplied
1287 * version of copy operator
1289 if ((method + assignshift==off
1290 && (assign || method==inc_amg || method==dec_amg))
1291 || inc_dec_ass) RvDEEPCP(left);
1297 bool oldcatch = CATCH_GET;
1300 Zero(&myop, 1, BINOP);
1301 myop.op_last = (OP *) &myop;
1302 myop.op_next = Nullop;
1303 myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED;
1308 if (PERLDB_SUB && curstash != debstash)
1309 op->op_private |= OPpENTERSUB_DB;
1313 EXTEND(sp, notfound + 5);
1314 PUSHs(lr>0? right: left);
1315 PUSHs(lr>0? left: right);
1316 PUSHs( lr > 0 ? &sv_yes : ( assign ? &sv_undef : &sv_no ));
1318 PUSHs( sv_2mortal(newSVpv((char *)AMG_names[method + assignshift],0)) );
1323 if (op = pp_entersub(ARGS))
1330 CATCH_SET(oldcatch);
1337 ans=SvIV(res)<=0; break;
1340 ans=SvIV(res)<0; break;
1343 ans=SvIV(res)>=0; break;
1346 ans=SvIV(res)>0; break;
1349 ans=SvIV(res)==0; break;
1352 ans=SvIV(res)!=0; break;
1355 SvSetSV(left,res); return left;
1357 ans=!SvOK(res); break;
1360 } else if (method==copy_amg) {
1362 croak("Copy method did not return a reference");
1364 return SvREFCNT_inc(SvRV(res));
1370 #endif /* OVERLOAD */