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)
89 bool doproto = SvTYPE(gv) > SVt_NULL;
90 char *proto = (doproto && SvPOK(gv)) ? SvPVX(gv) : NULL;
92 sv_upgrade((SV*)gv, SVt_PVGV);
101 Newz(602, gp, 1, GP);
102 GvGP(gv) = gp_ref(gp);
103 GvSV(gv) = NEWSV(72,0);
104 GvLINE(gv) = curcop->cop_line;
105 GvFILEGV(gv) = curcop->cop_filegv;
108 sv_magic((SV*)gv, (SV*)gv, '*', name, len);
109 GvSTASH(gv) = (HV*)SvREFCNT_inc(stash);
110 GvNAME(gv) = savepvn(name, len);
114 if (doproto) { /* Replicate part of newSUB here. */
117 start_subparse(0,0); /* Create CV in compcv. */
122 CvGV(GvCV(gv)) = (GV*)SvREFCNT_inc(gv);
123 CvFILEGV(GvCV(gv)) = curcop->cop_filegv;
124 CvSTASH(GvCV(gv)) = curstash;
126 CvOWNER(GvCV(gv)) = 0;
127 New(666, CvMUTEXP(GvCV(gv)), 1, perl_mutex);
128 MUTEX_INIT(CvMUTEXP(GvCV(gv)));
129 #endif /* USE_THREADS */
131 sv_setpv((SV*)GvCV(gv), proto);
138 gv_init_sv(GV *gv, I32 sv_type)
154 gv_fetchmeth(HV *stash, char *name, STRLEN len, I32 level)
164 if ((level > 100) || (level < -100))
165 croak("Recursive inheritance detected while looking for method '%s' in package '%s'",
166 name, HvNAME(stash));
168 DEBUG_o( deb("Looking for method %s in package %s\n",name,HvNAME(stash)) );
170 gvp = (GV**)hv_fetch(stash, name, len, (level >= 0));
175 if (SvTYPE(topgv) != SVt_PVGV)
176 gv_init(topgv, stash, name, len, TRUE);
177 if (cv = GvCV(topgv)) {
178 /* If genuine method or valid cache entry, use it */
179 if (!GvCVGEN(topgv) || GvCVGEN(topgv) == sub_generation)
181 /* Stale cached entry: junk it */
183 GvCV(topgv) = cv = Nullcv;
186 else if (GvCVGEN(topgv) == sub_generation)
187 return 0; /* cache indicates sub doesn't exist */
190 gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE);
191 av = (gvp && (gv = *gvp) && gv != (GV*)&sv_undef) ? GvAV(gv) : Nullav;
193 /* create and re-create @.*::SUPER::ISA on demand */
194 if (!av || !SvMAGIC(av)) {
195 char* packname = HvNAME(stash);
196 STRLEN packlen = strlen(packname);
198 if (packlen >= 7 && strEQ(packname + packlen - 7, "::SUPER")) {
202 basestash = gv_stashpvn(packname, packlen, TRUE);
203 gvp = (GV**)hv_fetch(basestash, "ISA", 3, FALSE);
204 if (gvp && (gv = *gvp) != (GV*)&sv_undef && (av = GvAV(gv))) {
205 dTHR; /* just for SvREFCNT_dec */
206 gvp = (GV**)hv_fetch(stash, "ISA", 3, TRUE);
207 if (!gvp || !(gv = *gvp))
208 croak("Cannot create %s::ISA", HvNAME(stash));
209 if (SvTYPE(gv) != SVt_PVGV)
210 gv_init(gv, stash, "ISA", 3, TRUE);
211 SvREFCNT_dec(GvAV(gv));
212 GvAV(gv) = (AV*)SvREFCNT_inc(av);
218 SV** svp = AvARRAY(av);
219 /* NOTE: No support for tied ISA */
220 I32 items = AvFILLp(av) + 1;
223 HV* basestash = gv_stashsv(sv, FALSE);
226 warn("Can't locate package %s for @%s::ISA",
227 SvPVX(sv), HvNAME(stash));
230 gv = gv_fetchmeth(basestash, name, len,
231 (level >= 0) ? level + 1 : level - 1);
237 /* if at top level, try UNIVERSAL */
239 if (level == 0 || level == -1) {
242 if (lastchance = gv_stashpvn("UNIVERSAL", 9, FALSE)) {
243 if (gv = gv_fetchmeth(lastchance, name, len,
244 (level >= 0) ? level + 1 : level - 1)) {
247 * Cache method in topgv if:
248 * 1. topgv has no synonyms (else inheritance crosses wires)
249 * 2. method isn't a stub (else AUTOLOAD fails spectacularly)
252 GvREFCNT(topgv) == 1 &&
254 (CvROOT(cv) || CvXSUB(cv)))
256 if (cv = GvCV(topgv))
258 GvCV(topgv) = (CV*)SvREFCNT_inc(GvCV(gv));
259 GvCVGEN(topgv) = sub_generation;
263 else if (topgv && GvREFCNT(topgv) == 1) {
264 /* cache the fact that the method is not defined */
265 GvCVGEN(topgv) = sub_generation;
274 gv_fetchmethod(HV *stash, char *name)
276 return gv_fetchmethod_autoload(stash, name, TRUE);
280 gv_fetchmethod_autoload(HV *stash, char *name, I32 autoload)
287 for (nend = name; *nend; nend++) {
290 else if (*nend == ':' && *(nend + 1) == ':')
294 char *origname = name;
298 if ((nsplit - origname) == 5 && strnEQ(origname, "SUPER", 5)) {
299 /* ->SUPER::method should really be looked up in original stash */
300 SV *tmpstr = sv_2mortal(newSVpvf("%s::SUPER",
301 HvNAME(curcop->cop_stash)));
302 stash = gv_stashpvn(SvPVX(tmpstr), SvCUR(tmpstr), TRUE);
303 DEBUG_o( deb("Treating %s as %s::%s\n",
304 origname, HvNAME(stash), name) );
307 stash = gv_stashpvn(origname, nsplit - origname, TRUE);
310 gv = gv_fetchmeth(stash, name, nend - name, 0);
312 if (strEQ(name,"import"))
315 gv = gv_autoload4(stash, name, nend - name, TRUE);
319 if (!CvROOT(cv) && !CvXSUB(cv)) {
327 if (GvCV(stubgv) != cv) /* orphaned import */
330 autogv = gv_autoload4(GvSTASH(stubgv),
331 GvNAME(stubgv), GvNAMELEN(stubgv), TRUE);
341 gv_autoload4(HV *stash, char *name, STRLEN len, I32 method)
343 static char autoload[] = "AUTOLOAD";
344 static STRLEN autolen = 8;
351 if (len == autolen && strnEQ(name, autoload, autolen))
353 if (!(gv = gv_fetchmeth(stash, autoload, autolen, FALSE)))
358 * Inheriting AUTOLOAD for non-methods works ... for now.
360 if (dowarn && !method && (GvCVGEN(gv) || GvSTASH(gv) != stash))
362 "Use of inherited AUTOLOAD for non-method %s::%.*s() is deprecated",
363 HvNAME(stash), (int)len, name);
366 * Given &FOO::AUTOLOAD, set $FOO::AUTOLOAD to desired function name.
367 * The subroutine's original name may not be "AUTOLOAD", so we don't
368 * use that, but for lack of anything better we will use the sub's
369 * original package to look up $AUTOLOAD.
371 varstash = GvSTASH(CvGV(cv));
372 vargv = *(GV**)hv_fetch(varstash, autoload, autolen, TRUE);
374 gv_init(vargv, varstash, autoload, autolen, FALSE);
376 sv_setpv(varsv, HvNAME(stash));
377 sv_catpvn(varsv, "::", 2);
378 sv_catpvn(varsv, name, len);
379 SvTAINTED_off(varsv);
384 gv_stashpv(char *name, I32 create)
386 return gv_stashpvn(name, strlen(name), create);
390 gv_stashpvn(char *name, U32 namelen, I32 create)
397 if (namelen + 3 < sizeof smallbuf)
400 New(606, tmpbuf, namelen + 3, char);
401 Copy(name,tmpbuf,namelen,char);
402 tmpbuf[namelen++] = ':';
403 tmpbuf[namelen++] = ':';
404 tmpbuf[namelen] = '\0';
405 tmpgv = gv_fetchpv(tmpbuf, create, SVt_PVHV);
406 if (tmpbuf != smallbuf)
411 GvHV(tmpgv) = newHV();
414 HvNAME(stash) = savepv(name);
419 gv_stashsv(SV *sv, I32 create)
424 return gv_stashpvn(ptr, len, create);
429 gv_fetchpv(char *nambeg, I32 add, I32 sv_type)
432 register char *name = nambeg;
436 register char *namend;
440 if (*name == '*' && isALPHA(name[1])) /* accidental stringify on a GV? */
443 for (namend = name; *namend; namend++) {
444 if ((*namend == '\'' && namend[1]) ||
445 (*namend == ':' && namend[1] == ':'))
449 if (!stash || !SvREFCNT(stash)) /* symbol table under destruction */
457 if (len + 3 < sizeof smallbuf)
460 New(601, tmpbuf, len+3, char);
461 Copy(name, tmpbuf, len, char);
465 gvp = (GV**)hv_fetch(stash,tmpbuf,len,add);
466 gv = gvp ? *gvp : Nullgv;
467 if (gv && gv != (GV*)&sv_undef) {
468 if (SvTYPE(gv) != SVt_PVGV)
469 gv_init(gv, stash, tmpbuf, len, (add & GV_ADDMULTI));
473 if (tmpbuf != smallbuf)
475 if (!gv || gv == (GV*)&sv_undef)
478 if (!(stash = GvHV(gv)))
479 stash = GvHV(gv) = newHV();
482 HvNAME(stash) = savepvn(nambeg, namend - nambeg);
490 return gv ? gv : (GV*)*hv_fetch(defstash, "main::", 6, TRUE);
497 /* No stash in name, so see how we can default */
500 if (isIDFIRST(*name)) {
503 if (isUPPER(*name)) {
505 if (*name == 'S' && (
506 strEQ(name, "SIG") ||
507 strEQ(name, "STDIN") ||
508 strEQ(name, "STDOUT") ||
509 strEQ(name, "STDERR") ))
512 else if (*name > 'E') {
513 if (*name == 'I' && strEQ(name, "INC"))
516 else if (*name > 'A') {
517 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*)curcop == &compiling) {
532 if (add && (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*)&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 = curcop->cop_stash;
567 /* By this point we should have a stash and a name */
572 if (add & ~GV_ADDMULTI) {
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 = curstash ? curstash : 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*)&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")) {
673 hv_magic(hv, siggv, 'S');
674 for(i=1;sig_name[i];i++) {
676 init=hv_fetch(hv,sig_name[i],strlen(sig_name[i]),1);
678 sv_setsv(*init,&sv_undef);
709 sv_setpv(GvSV(gv),chopset);
715 #ifdef COMPLEX_STATUS
716 sv_upgrade(GvSV(gv), SVt_PVLV);
723 if (sv_type > SVt_PV && curcop != &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");
738 if (dowarn && len == 1 && sv_type == SVt_PV)
739 warn("Use of $%s is deprecated", name);
782 SvREADONLY_on(GvSV(gv));
784 sv_magic(GvSV(gv), (SV*)gv, 0, name, len);
790 sv_setpv(GvSV(gv),"\f");
796 sv_setpv(GvSV(gv),"\034");
801 sv_upgrade(sv, SVt_PVNV);
802 sv_setpv(sv, patchlevel);
812 gv_fullname3(SV *sv, GV *gv, char *prefix)
814 HV *hv = GvSTASH(gv);
819 sv_setpv(sv, prefix ? prefix : "");
820 sv_catpv(sv,HvNAME(hv));
821 sv_catpvn(sv,"::", 2);
822 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
826 gv_efullname3(SV *sv, GV *gv, char *prefix)
831 gv_fullname3(sv, egv, prefix);
834 /* XXX compatibility with versions <= 5.003. */
836 gv_fullname(SV *sv, GV *gv)
838 gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : "");
841 /* XXX compatibility with versions <= 5.003. */
843 gv_efullname(SV *sv, GV *gv)
845 gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : "");
855 io = (IO*)NEWSV(0,0);
856 sv_upgrade((SV *)io,SVt_PVIO);
859 iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV);
861 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
862 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
878 for (i = 0; i <= (I32) HvMAX(stash); i++) {
879 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
880 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
881 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)) && HvNAME(hv))
884 gv_check(hv); /* nested package */
886 else if (isALPHA(*HeKEY(entry))) {
887 gv = (GV*)HeVAL(entry);
888 if (SvTYPE(gv) != SVt_PVGV || GvMULTI(gv))
890 curcop->cop_line = GvLINE(gv);
891 filegv = GvFILEGV(gv);
892 curcop->cop_filegv = filegv;
893 if (filegv && GvMULTI(filegv)) /* Filename began with slash */
895 warn("Name \"%s::%s\" used only once: possible typo",
896 HvNAME(stash), GvNAME(gv));
905 return gv_fetchpv(form("%s::_GEN_%ld", pack, (long)gensym++),
909 /* hopefully this is only called on local symbol table entries */
917 /* multi-named GPs cannot be used for method cache */
918 SvREFCNT_dec(gp->gp_cv);
923 /* Adding a new name to a subroutine invalidates method cache */
936 if (!gv || !(gp = GvGP(gv)))
938 if (gp->gp_refcnt == 0) {
939 warn("Attempt to free unreferenced glob pointers");
943 /* Deleting the name of a subroutine invalidates method cache */
946 if (--gp->gp_refcnt > 0) {
947 if (gp->gp_egv == gv)
952 SvREFCNT_dec(gp->gp_sv);
953 SvREFCNT_dec(gp->gp_av);
954 SvREFCNT_dec(gp->gp_hv);
955 SvREFCNT_dec(gp->gp_io);
956 SvREFCNT_dec(gp->gp_cv);
957 SvREFCNT_dec(gp->gp_form);
963 #if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
967 #ifdef MICROPORT /* Microport 2.4 hack */
972 return GvGP(gv)->gp_av;
974 return GvGP(gv_AVadd(gv))->gp_av;
981 return GvGP(gv)->gp_hv;
983 return GvGP(gv_HVadd(gv))->gp_hv;
985 #endif /* Microport 2.4 hack */
988 /* Updates and caches the CV's */
991 Gv_AMupdate(HV *stash)
998 MAGIC* mg=mg_find((SV*)stash,'c');
999 AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL;
1002 if (mg && amtp->was_ok_am == amagic_generation
1003 && amtp->was_ok_sub == sub_generation)
1004 return AMT_AMAGIC(amtp);
1005 if (amtp && AMT_AMAGIC(amtp)) { /* Have table. */
1007 for (i=1; i<NofAMmeth; i++) {
1008 if (amtp->table[i]) {
1009 SvREFCNT_dec(amtp->table[i]);
1013 sv_unmagic((SV*)stash, 'c');
1015 DEBUG_o( deb("Recalcing overload magic in package %s\n",HvNAME(stash)) );
1017 amt.was_ok_am = amagic_generation;
1018 amt.was_ok_sub = sub_generation;
1019 amt.fallback = AMGfallNO;
1022 #ifdef OVERLOAD_VIA_HASH
1023 gvp=(GV**)hv_fetch(stash,"OVERLOAD",8,FALSE); /* A shortcut */
1024 if (gvp && ((gv = *gvp) != (GV*)&sv_undef && (hv = GvHV(gv)))) {
1031 /* Work with "fallback" key, which we assume to be first in AMG_names */
1033 if (( cp = (char *)AMG_names[0] ) &&
1034 (svp = (SV**)hv_fetch(hv,cp,strlen(cp),FALSE)) && (sv = *svp)) {
1035 if (SvTRUE(sv)) amt.fallback=AMGfallYES;
1036 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
1038 for (i = 1; i < NofAMmeth; i++) {
1040 cp = (char *)AMG_names[i];
1042 svp = (SV**)hv_fetch(hv, cp, strlen(cp), FALSE);
1043 if (svp && ((sv = *svp) != &sv_undef)) {
1044 switch (SvTYPE(sv)) {
1047 if (!SvOK(sv)) break;
1048 gv = gv_fetchmethod(stash, SvPV(sv, na));
1049 if (gv) cv = GvCV(gv);
1053 if (SvTYPE(cv) == SVt_PVCV)
1058 croak("Not a subroutine reference in overload table");
1064 if (!(cv = GvCVu((GV*)sv)))
1065 cv = sv_2cv(sv, &stash, &gv, TRUE);
1070 croak("Method for operation %s not found in package %.256s during blessing\n",
1083 /* Work with "fallback" key, which we assume to be first in AMG_names */
1085 if ( cp = AMG_names[0] ) {
1086 /* Try to find via inheritance. */
1087 gv = gv_fetchmeth(stash, "()", 2, -1); /* A cookie: "()". */
1088 if (gv) sv = GvSV(gv);
1090 if (!gv) goto no_table;
1091 else if (SvTRUE(sv)) amt.fallback=AMGfallYES;
1092 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
1095 for (i = 1; i < NofAMmeth; i++) {
1096 SV *cookie = sv_2mortal(newSVpvf("(%s", cp = AMG_names[i]));
1097 DEBUG_o( deb("Checking overloading of `%s' in package `%.256s'\n",
1098 cp, HvNAME(stash)) );
1099 /* don't fill the cache while looking up! */
1100 gv = gv_fetchmeth(stash, SvPVX(cookie), SvCUR(cookie), -1);
1102 if(gv && (cv = GvCV(gv))) {
1103 if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil")
1104 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) {
1105 /* GvSV contains the name of the method. */
1108 DEBUG_o( deb("Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n",
1109 SvPV(GvSV(gv), na), cp, HvNAME(stash)) );
1110 if (!SvPOK(GvSV(gv))
1111 || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)),
1114 /* Can be an import stub (created by `can'). */
1116 croak("Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'",
1117 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1120 croak("Cannot resolve method `%.256s' overloading `%s' in package `%.256s'",
1121 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1124 cv = GvCV(gv = ngv);
1126 DEBUG_o( deb("Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n",
1127 cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))),
1128 GvNAME(CvGV(cv))) );
1132 amt.table[i]=(CV*)SvREFCNT_inc(cv);
1135 AMT_AMAGIC_on(&amt);
1136 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMT));
1140 /* Here we have no table: */
1142 AMT_AMAGIC_off(&amt);
1143 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMTS));
1148 amagic_call(SV *left, SV *right, int method, int flags)
1153 CV **cvp=NULL, **ocvp=NULL;
1155 int fl=0, off, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
1156 int postpr=0, inc_dec_ass=0, assignshift=assign?1:0;
1158 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
1159 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),'c'))
1160 && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1161 ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table
1163 && ((cv = cvp[off=method+assignshift])
1164 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
1166 (fl = 1, cv = cvp[off=method])))) {
1167 lr = -1; /* Call method for left argument */
1169 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
1172 /* look for substituted methods */
1175 if (((cv = cvp[off=add_ass_amg]) && (inc_dec_ass=1))
1176 || ((cv = cvp[off=add_amg]) && (postpr=1))) {
1177 right = &sv_yes; lr = -1; assign = 1;
1181 if (((cv = cvp[off=subtr_ass_amg]) && (inc_dec_ass=1))
1182 || ((cv = cvp[off=subtr_amg]) && (postpr=1))) {
1183 right = &sv_yes; lr = -1; assign = 1;
1187 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
1190 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1193 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1196 (void)((cv = cvp[off=bool__amg])
1197 || (cv = cvp[off=numer_amg])
1198 || (cv = cvp[off=string_amg]));
1204 * SV* ref causes confusion with the interpreter variable of
1207 SV* tmpRef=SvRV(left);
1208 if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) {
1210 * Just to be extra cautious. Maybe in some
1211 * additional cases sv_setsv is safe, too.
1213 SV* newref = newSVsv(tmpRef);
1214 SvOBJECT_on(newref);
1215 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(tmpRef));
1221 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
1222 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
1223 SV* nullsv=sv_2mortal(newSViv(0));
1225 SV* lessp = amagic_call(left,nullsv,
1226 lt_amg,AMGf_noright);
1227 logic = SvTRUE(lessp);
1229 SV* lessp = amagic_call(left,nullsv,
1230 ncmp_amg,AMGf_noright);
1231 logic = (SvNV(lessp) < 0);
1234 if (off==subtr_amg) {
1245 if (cv = cvp[off=subtr_amg]) {
1247 left = sv_2mortal(newSViv(0));
1254 if (!cv) goto not_found;
1255 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1256 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c'))
1257 && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1258 ? (amtp = (AMT*)mg->mg_ptr)->table
1260 && (cv = cvp[off=method])) { /* Method for right
1263 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
1264 && (cvp=ocvp) && (lr = -1))
1265 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1266 && !(flags & AMGf_unary)) {
1267 /* We look for substitution for
1268 * comparison operations and
1270 if (method==concat_amg || method==concat_ass_amg
1271 || method==repeat_amg || method==repeat_ass_amg) {
1272 return NULL; /* Delegate operation to string conversion */
1282 postpr = 1; off=ncmp_amg; break;
1289 postpr = 1; off=scmp_amg; break;
1291 if (off != -1) cv = cvp[off];
1296 not_found: /* No method found, either report or croak */
1297 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1298 notfound = 1; lr = -1;
1299 } else if (cvp && (cv=cvp[nomethod_amg])) {
1300 notfound = 1; lr = 1;
1303 if (off==-1) off=method;
1304 msg = sv_2mortal(newSVpvf(
1305 "Operation `%s': no method found,%sargument %s%s%s%s",
1306 AMG_names[method + assignshift],
1307 (flags & AMGf_unary ? " " : "\n\tleft "),
1309 "in overloaded package ":
1310 "has no overloaded magic",
1312 HvNAME(SvSTASH(SvRV(left))):
1315 ",\n\tright argument in overloaded package ":
1318 : ",\n\tright argument has no overloaded magic"),
1320 HvNAME(SvSTASH(SvRV(right))):
1322 if (amtp && amtp->fallback >= AMGfallYES) {
1323 DEBUG_o( deb("%s", SvPVX(msg)) );
1333 "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n",
1335 method+assignshift==off? "" :
1337 method+assignshift==off? "" :
1338 AMG_names[method+assignshift],
1339 method+assignshift==off? "" : "')",
1340 flags & AMGf_unary? "" :
1341 lr==1 ? " for right argument": " for left argument",
1342 flags & AMGf_unary? " for argument" : "",
1344 fl? ",\n\tassignment variant used": "") );
1345 /* Since we use shallow copy during assignment, we need
1346 * to dublicate the contents, probably calling user-supplied
1347 * version of copy operator
1349 if ((method + assignshift==off
1350 && (assign || method==inc_amg || method==dec_amg))
1351 || inc_dec_ass) RvDEEPCP(left);
1357 bool oldcatch = CATCH_GET;
1360 Zero(&myop, 1, BINOP);
1361 myop.op_last = (OP *) &myop;
1362 myop.op_next = Nullop;
1363 myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED;
1365 PUSHSTACKi(PERLSI_OVERLOAD);
1369 if (PERLDB_SUB && curstash != debstash)
1370 op->op_private |= OPpENTERSUB_DB;
1374 EXTEND(SP, notfound + 5);
1375 PUSHs(lr>0? right: left);
1376 PUSHs(lr>0? left: right);
1377 PUSHs( lr > 0 ? &sv_yes : ( assign ? &sv_undef : &sv_no ));
1379 PUSHs( sv_2mortal(newSVpv((char *)AMG_names[method + assignshift],0)) );
1384 if (op = pp_entersub(ARGS))
1391 CATCH_SET(oldcatch);
1398 ans=SvIV(res)<=0; break;
1401 ans=SvIV(res)<0; break;
1404 ans=SvIV(res)>=0; break;
1407 ans=SvIV(res)>0; break;
1410 ans=SvIV(res)==0; break;
1413 ans=SvIV(res)!=0; break;
1416 SvSetSV(left,res); return left;
1418 ans=!SvOK(res); break;
1421 } else if (method==copy_amg) {
1423 croak("Copy method did not return a reference");
1425 return SvREFCNT_inc(SvRV(res));
1431 #endif /* OVERLOAD */