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);
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 I32 items = AvFILL(av) + 1;
189 HV* basestash = gv_stashsv(sv, FALSE);
192 warn("Can't locate package %s for @%s::ISA",
193 SvPVX(sv), HvNAME(stash));
196 gv = gv_fetchmeth(basestash, name, len,
197 (level >= 0) ? level + 1 : level - 1);
203 /* if at top level, try UNIVERSAL */
205 if (level == 0 || level == -1) {
208 if (lastchance = gv_stashpvn("UNIVERSAL", 9, FALSE)) {
209 if (gv = gv_fetchmeth(lastchance, name, len,
210 (level >= 0) ? level + 1 : level - 1)) {
213 * Cache method in topgv if:
214 * 1. topgv has no synonyms (else inheritance crosses wires)
215 * 2. method isn't a stub (else AUTOLOAD fails spectacularly)
218 GvREFCNT(topgv) == 1 &&
220 (CvROOT(cv) || CvXSUB(cv)))
222 if (cv = GvCV(topgv))
224 GvCV(topgv) = (CV*)SvREFCNT_inc(GvCV(gv));
225 GvCVGEN(topgv) = sub_generation;
236 gv_fetchmethod(HV *stash, char *name)
238 return gv_fetchmethod_autoload(stash, name, TRUE);
242 gv_fetchmethod_autoload(HV *stash, char *name, I32 autoload)
249 for (nend = name; *nend; nend++) {
252 else if (*nend == ':' && *(nend + 1) == ':')
256 char *origname = name;
260 if ((nsplit - origname) == 5 && strnEQ(origname, "SUPER", 5)) {
261 /* ->SUPER::method should really be looked up in original stash */
262 SV *tmpstr = sv_2mortal(newSVpvf("%s::SUPER",
263 HvNAME(curcop->cop_stash)));
264 stash = gv_stashpvn(SvPVX(tmpstr), SvCUR(tmpstr), TRUE);
265 DEBUG_o( deb("Treating %s as %s::%s\n",
266 origname, HvNAME(stash), name) );
269 stash = gv_stashpvn(origname, nsplit - origname, TRUE);
272 gv = gv_fetchmeth(stash, name, nend - name, 0);
274 if (strEQ(name,"import"))
277 gv = gv_autoload4(stash, name, nend - name, TRUE);
281 if (!CvROOT(cv) && !CvXSUB(cv)) {
289 if (GvCV(stubgv) != cv) /* orphaned import */
292 autogv = gv_autoload4(GvSTASH(stubgv),
293 GvNAME(stubgv), GvNAMELEN(stubgv), TRUE);
303 gv_autoload4(HV *stash, char *name, STRLEN len, I32 method)
305 static char autoload[] = "AUTOLOAD";
306 static STRLEN autolen = 8;
313 if (len == autolen && strnEQ(name, autoload, autolen))
315 if (!(gv = gv_fetchmeth(stash, autoload, autolen, FALSE)))
320 * Inheriting AUTOLOAD for non-methods works ... for now.
322 if (dowarn && !method && (GvCVGEN(gv) || GvSTASH(gv) != stash))
324 "Use of inherited AUTOLOAD for non-method %s::%.*s() is deprecated",
325 HvNAME(stash), (int)len, name);
328 * Given &FOO::AUTOLOAD, set $FOO::AUTOLOAD to desired function name.
329 * The subroutine's original name may not be "AUTOLOAD", so we don't
330 * use that, but for lack of anything better we will use the sub's
331 * original package to look up $AUTOLOAD.
333 varstash = GvSTASH(CvGV(cv));
334 vargv = *(GV**)hv_fetch(varstash, autoload, autolen, TRUE);
336 gv_init(vargv, varstash, autoload, autolen, FALSE);
338 sv_setpv(varsv, HvNAME(stash));
339 sv_catpvn(varsv, "::", 2);
340 sv_catpvn(varsv, name, len);
341 SvTAINTED_off(varsv);
346 gv_stashpv(char *name, I32 create)
348 return gv_stashpvn(name, strlen(name), create);
352 gv_stashpvn(char *name, U32 namelen, I32 create)
359 if (namelen + 3 < sizeof smallbuf)
362 New(606, tmpbuf, namelen + 3, char);
363 Copy(name,tmpbuf,namelen,char);
364 tmpbuf[namelen++] = ':';
365 tmpbuf[namelen++] = ':';
366 tmpbuf[namelen] = '\0';
367 tmpgv = gv_fetchpv(tmpbuf, create, SVt_PVHV);
368 if (tmpbuf != smallbuf)
373 GvHV(tmpgv) = newHV();
376 HvNAME(stash) = savepv(name);
381 gv_stashsv(SV *sv, I32 create)
386 return gv_stashpvn(ptr, len, create);
391 gv_fetchpv(char *nambeg, I32 add, I32 sv_type)
394 register char *name = nambeg;
398 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 */
417 New(601, tmpbuf, len+3, char);
418 Copy(name, tmpbuf, len, char);
422 gvp = (GV**)hv_fetch(stash,tmpbuf,len,add);
424 if (!gvp || *gvp == (GV*)&sv_undef)
428 if (SvTYPE(gv) == SVt_PVGV)
433 gv_init(gv, stash, nambeg, namend - nambeg, (add & 2));
435 if (!(stash = GvHV(gv)))
436 stash = GvHV(gv) = newHV();
439 HvNAME(stash) = savepvn(nambeg, namend - nambeg);
447 return gv ? gv : (GV*)*hv_fetch(defstash, "main::", 6, TRUE);
454 /* No stash in name, so see how we can default */
457 if (isIDFIRST(*name)) {
460 if (isUPPER(*name)) {
462 if (*name == 'S' && (
463 strEQ(name, "SIG") ||
464 strEQ(name, "STDIN") ||
465 strEQ(name, "STDOUT") ||
466 strEQ(name, "STDERR") ))
469 else if (*name > 'E') {
470 if (*name == 'I' && strEQ(name, "INC"))
473 else if (*name > 'A') {
474 if (*name == 'E' && strEQ(name, "ENV"))
477 else if (*name == 'A' && (
478 strEQ(name, "ARGV") ||
479 strEQ(name, "ARGVOUT") ))
482 else if (*name == '_' && !name[1])
487 else if ((COP*)curcop == &compiling) {
489 if (add && (hints & HINT_STRICT_VARS) &&
490 sv_type != SVt_PVCV &&
491 sv_type != SVt_PVGV &&
492 sv_type != SVt_PVFM &&
493 sv_type != SVt_PVIO &&
494 !(len == 1 && sv_type == SVt_PV && strchr("ab",*name)) )
496 gvp = (GV**)hv_fetch(stash,name,len,0);
498 *gvp == (GV*)&sv_undef ||
499 SvTYPE(*gvp) != SVt_PVGV)
503 else if (sv_type == SVt_PV && !GvIMPORTED_SV(*gvp) ||
504 sv_type == SVt_PVAV && !GvIMPORTED_AV(*gvp) ||
505 sv_type == SVt_PVHV && !GvIMPORTED_HV(*gvp) )
507 warn("Variable \"%c%s\" is not imported",
508 sv_type == SVt_PVAV ? '@' :
509 sv_type == SVt_PVHV ? '%' : '$',
512 warn("(Did you mean &%s instead?)\n", name);
518 stash = curcop->cop_stash;
524 /* By this point we should have a stash and a name */
528 warn("Global symbol \"%s\" requires explicit package name", name);
530 stash = curstash ? curstash : defstash; /* avoid core dumps */
531 add_gvflags = ((sv_type == SVt_PV) ? GVf_IMPORTED_SV
532 : (sv_type == SVt_PVAV) ? GVf_IMPORTED_AV
533 : (sv_type == SVt_PVHV) ? GVf_IMPORTED_HV
540 if (!SvREFCNT(stash)) /* symbol table under destruction */
543 gvp = (GV**)hv_fetch(stash,name,len,add);
544 if (!gvp || *gvp == (GV*)&sv_undef)
547 if (SvTYPE(gv) == SVt_PVGV) {
550 gv_init_sv(gv, sv_type);
555 /* Adding a new symbol */
558 warn("Had to create %s unexpectedly", nambeg);
559 gv_init(gv, stash, name, len, add & 2);
560 gv_init_sv(gv, sv_type);
561 GvFLAGS(gv) |= add_gvflags;
563 /* set up magic where warranted */
566 if (strEQ(name, "ARGV")) {
567 IoFLAGS(GvIOn(gv)) |= IOf_ARGV|IOf_START;
577 if (strnEQ(name, "EXPORT", 6))
581 if (strEQ(name, "ISA")) {
584 sv_magic((SV*)av, (SV*)gv, 'I', Nullch, 0);
585 if (add & 2 && strEQ(nambeg,"AnyDBM_File::ISA") && AvFILL(av) == -1)
588 av_push(av, newSVpv(pname = "NDBM_File",0));
589 gv_stashpvn(pname, 9, TRUE);
590 av_push(av, newSVpv(pname = "DB_File",0));
591 gv_stashpvn(pname, 7, TRUE);
592 av_push(av, newSVpv(pname = "GDBM_File",0));
593 gv_stashpvn(pname, 9, TRUE);
594 av_push(av, newSVpv(pname = "SDBM_File",0));
595 gv_stashpvn(pname, 9, TRUE);
596 av_push(av, newSVpv(pname = "ODBM_File",0));
597 gv_stashpvn(pname, 9, TRUE);
603 if (strEQ(name, "OVERLOAD")) {
606 hv_magic(hv, gv, 'A');
609 #endif /* OVERLOAD */
611 if (strEQ(name, "SIG")) {
617 hv_magic(hv, siggv, 'S');
618 for(i=1;sig_name[i];i++) {
620 init=hv_fetch(hv,sig_name[i],strlen(sig_name[i]),1);
622 sv_setsv(*init,&sv_undef);
626 /* initialize signal stack */
627 signalstack = newAV();
628 AvREAL_off(signalstack);
629 av_extend(signalstack, 30);
630 av_fill(signalstack, 0);
658 sv_setpv(GvSV(gv),chopset);
664 #ifdef COMPLEX_STATUS
665 sv_upgrade(GvSV(gv), SVt_PVLV);
671 if (dowarn && len == 1 && sv_type == SVt_PV)
672 warn("Use of $%s is deprecated", name);
716 SvREADONLY_on(GvSV(gv));
718 sv_magic(GvSV(gv), (SV*)gv, 0, name, len);
724 sv_setpv(GvSV(gv),"\f");
730 sv_setpv(GvSV(gv),"\034");
735 sv_upgrade(sv, SVt_PVNV);
736 sv_setpv(sv, patchlevel);
746 gv_fullname3(SV *sv, GV *gv, char *prefix)
748 HV *hv = GvSTASH(gv);
753 sv_setpv(sv, prefix ? prefix : "");
754 sv_catpv(sv,HvNAME(hv));
755 sv_catpvn(sv,"::", 2);
756 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
760 gv_efullname3(SV *sv, GV *gv, char *prefix)
765 gv_fullname3(sv, egv, prefix);
768 /* XXX compatibility with versions <= 5.003. */
770 gv_fullname(SV *sv, GV *gv)
772 gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : "");
775 /* XXX compatibility with versions <= 5.003. */
777 gv_efullname(SV *sv, GV *gv)
779 gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : "");
789 io = (IO*)NEWSV(0,0);
790 sv_upgrade((SV *)io,SVt_PVIO);
793 iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV);
795 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
796 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
812 for (i = 0; i <= (I32) HvMAX(stash); i++) {
813 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
814 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
815 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)) && HvNAME(hv))
818 gv_check(hv); /* nested package */
820 else if (isALPHA(*HeKEY(entry))) {
821 gv = (GV*)HeVAL(entry);
824 curcop->cop_line = GvLINE(gv);
825 filegv = GvFILEGV(gv);
826 curcop->cop_filegv = filegv;
827 if (filegv && GvMULTI(filegv)) /* Filename began with slash */
829 warn("Name \"%s::%s\" used only once: possible typo",
830 HvNAME(stash), GvNAME(gv));
839 return gv_fetchpv(form("%s::_GEN_%ld", pack, (long)gensym++),
843 /* hopefully this is only called on local symbol table entries */
851 /* multi-named GPs cannot be used for method cache */
852 SvREFCNT_dec(gp->gp_cv);
857 /* Adding a new name to a subroutine invalidates method cache */
870 if (!gv || !(gp = GvGP(gv)))
872 if (gp->gp_refcnt == 0) {
873 warn("Attempt to free unreferenced glob pointers");
877 /* Deleting the name of a subroutine invalidates method cache */
880 if (--gp->gp_refcnt > 0) {
881 if (gp->gp_egv == gv)
886 SvREFCNT_dec(gp->gp_sv);
887 SvREFCNT_dec(gp->gp_av);
888 SvREFCNT_dec(gp->gp_hv);
889 SvREFCNT_dec(gp->gp_io);
890 SvREFCNT_dec(gp->gp_cv);
891 SvREFCNT_dec(gp->gp_form);
897 #if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
901 #ifdef MICROPORT /* Microport 2.4 hack */
906 return GvGP(gv)->gp_av;
908 return GvGP(gv_AVadd(gv))->gp_av;
915 return GvGP(gv)->gp_hv;
917 return GvGP(gv_HVadd(gv))->gp_hv;
919 #endif /* Microport 2.4 hack */
922 /* Updates and caches the CV's */
925 Gv_AMupdate(HV *stash)
932 MAGIC* mg=mg_find((SV*)stash,'c');
933 AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL;
936 if (mg && amtp->was_ok_am == amagic_generation
937 && amtp->was_ok_sub == sub_generation)
938 return AMT_AMAGIC(amtp);
939 if (amtp && AMT_AMAGIC(amtp)) { /* Have table. */
941 for (i=1; i<NofAMmeth; i++) {
942 if (amtp->table[i]) {
943 SvREFCNT_dec(amtp->table[i]);
947 sv_unmagic((SV*)stash, 'c');
949 DEBUG_o( deb("Recalcing overload magic in package %s\n",HvNAME(stash)) );
951 amt.was_ok_am = amagic_generation;
952 amt.was_ok_sub = sub_generation;
953 amt.fallback = AMGfallNO;
956 #ifdef OVERLOAD_VIA_HASH
957 gvp=(GV**)hv_fetch(stash,"OVERLOAD",8,FALSE); /* A shortcut */
958 if (gvp && ((gv = *gvp) != (GV*)&sv_undef && (hv = GvHV(gv)))) {
965 /* Work with "fallback" key, which we assume to be first in AMG_names */
967 if (( cp = (char *)AMG_names[0] ) &&
968 (svp = (SV**)hv_fetch(hv,cp,strlen(cp),FALSE)) && (sv = *svp)) {
969 if (SvTRUE(sv)) amt.fallback=AMGfallYES;
970 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
972 for (i = 1; i < NofAMmeth; i++) {
974 cp = (char *)AMG_names[i];
976 svp = (SV**)hv_fetch(hv, cp, strlen(cp), FALSE);
977 if (svp && ((sv = *svp) != &sv_undef)) {
978 switch (SvTYPE(sv)) {
981 if (!SvOK(sv)) break;
982 gv = gv_fetchmethod(stash, SvPV(sv, na));
983 if (gv) cv = GvCV(gv);
987 if (SvTYPE(cv) == SVt_PVCV)
992 croak("Not a subroutine reference in overload table");
998 if (!(cv = GvCVu((GV*)sv)))
999 cv = sv_2cv(sv, &stash, &gv, TRUE);
1004 croak("Method for operation %s not found in package %.256s during blessing\n",
1017 /* Work with "fallback" key, which we assume to be first in AMG_names */
1019 if ( cp = AMG_names[0] ) {
1020 /* Try to find via inheritance. */
1021 gv = gv_fetchmeth(stash, "()", 2, -1); /* A cookie: "()". */
1022 if (gv) sv = GvSV(gv);
1024 if (!gv) goto no_table;
1025 else if (SvTRUE(sv)) amt.fallback=AMGfallYES;
1026 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
1029 for (i = 1; i < NofAMmeth; i++) {
1030 SV *cookie = sv_2mortal(newSVpvf("(%s", cp = AMG_names[i]));
1031 DEBUG_o( deb("Checking overloading of `%s' in package `%.256s'\n",
1032 cp, HvNAME(stash)) );
1033 /* don't fill the cache while looking up! */
1034 gv = gv_fetchmeth(stash, SvPVX(cookie), SvCUR(cookie), -1);
1036 if(gv && (cv = GvCV(gv))) {
1037 if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil")
1038 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) {
1039 /* GvSV contains the name of the method. */
1042 DEBUG_o( deb("Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n",
1043 SvPV(GvSV(gv), na), cp, HvNAME(stash)) );
1044 if (!SvPOK(GvSV(gv))
1045 || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)),
1048 /* Can be an import stub (created by `can'). */
1050 croak("Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'",
1051 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1054 croak("Cannot resolve method `%.256s' overloading `%s' in package `%.256s'",
1055 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1058 cv = GvCV(gv = ngv);
1060 DEBUG_o( deb("Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n",
1061 cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))),
1062 GvNAME(CvGV(cv))) );
1066 amt.table[i]=(CV*)SvREFCNT_inc(cv);
1069 AMT_AMAGIC_on(&amt);
1070 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMT));
1074 /* Here we have no table: */
1076 AMT_AMAGIC_off(&amt);
1077 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMTS));
1081 /* During call to this subroutine stack can be reallocated. It is
1082 * advised to call SPAGAIN macro in your code after call */
1085 amagic_call(SV *left, SV *right, int method, int flags)
1090 CV **cvp=NULL, **ocvp=NULL;
1092 int fl=0, off, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
1093 int postpr=0, inc_dec_ass=0, assignshift=assign?1:0;
1095 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
1096 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),'c'))
1097 && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1098 ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table
1100 && ((cv = cvp[off=method+assignshift])
1101 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
1103 (fl = 1, cv = cvp[off=method])))) {
1104 lr = -1; /* Call method for left argument */
1106 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
1109 /* look for substituted methods */
1112 if (((cv = cvp[off=add_ass_amg]) && (inc_dec_ass=1))
1113 || ((cv = cvp[off=add_amg]) && (postpr=1))) {
1114 right = &sv_yes; lr = -1; assign = 1;
1118 if (((cv = cvp[off=subtr_ass_amg]) && (inc_dec_ass=1))
1119 || ((cv = cvp[off=subtr_amg]) && (postpr=1))) {
1120 right = &sv_yes; lr = -1; assign = 1;
1124 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
1127 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1130 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1133 (void)((cv = cvp[off=bool__amg])
1134 || (cv = cvp[off=numer_amg])
1135 || (cv = cvp[off=string_amg]));
1141 * SV* ref causes confusion with the interpreter variable of
1144 SV* tmpRef=SvRV(left);
1145 if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) {
1147 * Just to be extra cautious. Maybe in some
1148 * additional cases sv_setsv is safe, too.
1150 SV* newref = newSVsv(tmpRef);
1151 SvOBJECT_on(newref);
1152 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(tmpRef));
1158 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
1159 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
1160 SV* nullsv=sv_2mortal(newSViv(0));
1162 SV* lessp = amagic_call(left,nullsv,
1163 lt_amg,AMGf_noright);
1164 logic = SvTRUE(lessp);
1166 SV* lessp = amagic_call(left,nullsv,
1167 ncmp_amg,AMGf_noright);
1168 logic = (SvNV(lessp) < 0);
1171 if (off==subtr_amg) {
1182 if (cv = cvp[off=subtr_amg]) {
1184 left = sv_2mortal(newSViv(0));
1191 if (!cv) goto not_found;
1192 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1193 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c'))
1194 && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1195 ? (amtp = (AMT*)mg->mg_ptr)->table
1197 && (cv = cvp[off=method])) { /* Method for right
1200 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
1201 && (cvp=ocvp) && (lr = -1))
1202 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1203 && !(flags & AMGf_unary)) {
1204 /* We look for substitution for
1205 * comparison operations and
1207 if (method==concat_amg || method==concat_ass_amg
1208 || method==repeat_amg || method==repeat_ass_amg) {
1209 return NULL; /* Delegate operation to string conversion */
1219 postpr = 1; off=ncmp_amg; break;
1226 postpr = 1; off=scmp_amg; break;
1228 if (off != -1) cv = cvp[off];
1233 not_found: /* No method found, either report or croak */
1234 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1235 notfound = 1; lr = -1;
1236 } else if (cvp && (cv=cvp[nomethod_amg])) {
1237 notfound = 1; lr = 1;
1240 if (off==-1) off=method;
1241 msg = sv_2mortal(newSVpvf(
1242 "Operation `%s': no method found,%sargument %s%s%s%s",
1243 AMG_names[method + assignshift],
1244 (flags & AMGf_unary ? " " : "\n\tleft "),
1246 "in overloaded package ":
1247 "has no overloaded magic",
1249 HvNAME(SvSTASH(SvRV(left))):
1252 ",\n\tright argument in overloaded package ":
1255 : ",\n\tright argument has no overloaded magic"),
1257 HvNAME(SvSTASH(SvRV(right))):
1259 if (amtp && amtp->fallback >= AMGfallYES) {
1260 DEBUG_o( deb("%s", SvPVX(msg)) );
1270 "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n",
1272 method+assignshift==off? "" :
1274 method+assignshift==off? "" :
1275 AMG_names[method+assignshift],
1276 method+assignshift==off? "" : "')",
1277 flags & AMGf_unary? "" :
1278 lr==1 ? " for right argument": " for left argument",
1279 flags & AMGf_unary? " for argument" : "",
1281 fl? ",\n\tassignment variant used": "") );
1282 /* Since we use shallow copy during assignment, we need
1283 * to dublicate the contents, probably calling user-supplied
1284 * version of copy operator
1286 if ((method + assignshift==off
1287 && (assign || method==inc_amg || method==dec_amg))
1288 || inc_dec_ass) RvDEEPCP(left);
1294 bool oldcatch = CATCH_GET;
1297 Zero(&myop, 1, BINOP);
1298 myop.op_last = (OP *) &myop;
1299 myop.op_next = Nullop;
1300 myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED;
1305 if (PERLDB_SUB && curstash != debstash)
1306 op->op_private |= OPpENTERSUB_DB;
1310 EXTEND(sp, notfound + 5);
1311 PUSHs(lr>0? right: left);
1312 PUSHs(lr>0? left: right);
1313 PUSHs( lr > 0 ? &sv_yes : ( assign ? &sv_undef : &sv_no ));
1315 PUSHs( sv_2mortal(newSVpv((char *)AMG_names[method + assignshift],0)) );
1320 if (op = pp_entersub(ARGS))
1327 CATCH_SET(oldcatch);
1334 ans=SvIV(res)<=0; break;
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 SvSetSV(left,res); return left;
1354 ans=!SvOK(res); break;
1357 } else if (method==copy_amg) {
1359 croak("Copy method did not return a reference");
1361 return SvREFCNT_inc(SvRV(res));
1367 #endif /* OVERLOAD */