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;
107 sv_magic((SV*)gv, (SV*)gv, '*', name, len);
108 GvSTASH(gv) = (HV*)SvREFCNT_inc(stash);
109 GvNAME(gv) = savepvn(name, len);
113 if (doproto) { /* Replicate part of newSUB here. */
116 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");
167 DEBUG_o( deb("Looking for method %s in package %s\n",name,HvNAME(stash)) );
169 gvp = (GV**)hv_fetch(stash, name, len, (level >= 0));
174 if (SvTYPE(topgv) != SVt_PVGV)
175 gv_init(topgv, stash, name, len, TRUE);
176 if (cv = GvCV(topgv)) {
177 /* If genuine method or valid cache entry, use it */
178 if (!GvCVGEN(topgv) || GvCVGEN(topgv) >= sub_generation)
180 /* Stale cached entry: junk it */
182 GvCV(topgv) = cv = Nullcv;
187 gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE);
188 av = (gvp && (gv = *gvp) && gv != (GV*)&sv_undef) ? GvAV(gv) : Nullav;
190 /* create and re-create @.*::SUPER::ISA on demand */
191 if (!av || !SvMAGIC(av)) {
192 char* packname = HvNAME(stash);
193 STRLEN packlen = strlen(packname);
195 if (packlen >= 7 && strEQ(packname + packlen - 7, "::SUPER")) {
199 basestash = gv_stashpvn(packname, packlen, TRUE);
200 gvp = (GV**)hv_fetch(basestash, "ISA", 3, FALSE);
201 if (gvp && (gv = *gvp) != (GV*)&sv_undef && (av = GvAV(gv))) {
202 dTHR; /* just for SvREFCNT_dec */
203 gvp = (GV**)hv_fetch(stash, "ISA", 3, TRUE);
204 if (!gvp || !(gv = *gvp))
205 croak("Cannot create %s::ISA", HvNAME(stash));
206 if (SvTYPE(gv) != SVt_PVGV)
207 gv_init(gv, stash, "ISA", 3, TRUE);
208 SvREFCNT_dec(GvAV(gv));
209 GvAV(gv) = (AV*)SvREFCNT_inc(av);
215 SV** svp = AvARRAY(av);
216 /* NOTE: No support for tied ISA */
217 I32 items = AvFILLp(av) + 1;
220 HV* basestash = gv_stashsv(sv, FALSE);
223 warn("Can't locate package %s for @%s::ISA",
224 SvPVX(sv), HvNAME(stash));
227 gv = gv_fetchmeth(basestash, name, len,
228 (level >= 0) ? level + 1 : level - 1);
234 /* if at top level, try UNIVERSAL */
236 if (level == 0 || level == -1) {
239 if (lastchance = gv_stashpvn("UNIVERSAL", 9, FALSE)) {
240 if (gv = gv_fetchmeth(lastchance, name, len,
241 (level >= 0) ? level + 1 : level - 1)) {
244 * Cache method in topgv if:
245 * 1. topgv has no synonyms (else inheritance crosses wires)
246 * 2. method isn't a stub (else AUTOLOAD fails spectacularly)
249 GvREFCNT(topgv) == 1 &&
251 (CvROOT(cv) || CvXSUB(cv)))
253 if (cv = GvCV(topgv))
255 GvCV(topgv) = (CV*)SvREFCNT_inc(GvCV(gv));
256 GvCVGEN(topgv) = sub_generation;
267 gv_fetchmethod(HV *stash, char *name)
269 return gv_fetchmethod_autoload(stash, name, TRUE);
273 gv_fetchmethod_autoload(HV *stash, char *name, I32 autoload)
280 for (nend = name; *nend; nend++) {
283 else if (*nend == ':' && *(nend + 1) == ':')
287 char *origname = name;
291 if ((nsplit - origname) == 5 && strnEQ(origname, "SUPER", 5)) {
292 /* ->SUPER::method should really be looked up in original stash */
293 SV *tmpstr = sv_2mortal(newSVpvf("%s::SUPER",
294 HvNAME(curcop->cop_stash)));
295 stash = gv_stashpvn(SvPVX(tmpstr), SvCUR(tmpstr), TRUE);
296 DEBUG_o( deb("Treating %s as %s::%s\n",
297 origname, HvNAME(stash), name) );
300 stash = gv_stashpvn(origname, nsplit - origname, TRUE);
303 gv = gv_fetchmeth(stash, name, nend - name, 0);
305 if (strEQ(name,"import"))
308 gv = gv_autoload4(stash, name, nend - name, TRUE);
312 if (!CvROOT(cv) && !CvXSUB(cv)) {
320 if (GvCV(stubgv) != cv) /* orphaned import */
323 autogv = gv_autoload4(GvSTASH(stubgv),
324 GvNAME(stubgv), GvNAMELEN(stubgv), TRUE);
334 gv_autoload4(HV *stash, char *name, STRLEN len, I32 method)
336 static char autoload[] = "AUTOLOAD";
337 static STRLEN autolen = 8;
344 if (len == autolen && strnEQ(name, autoload, autolen))
346 if (!(gv = gv_fetchmeth(stash, autoload, autolen, FALSE)))
351 * Inheriting AUTOLOAD for non-methods works ... for now.
353 if (dowarn && !method && (GvCVGEN(gv) || GvSTASH(gv) != stash))
355 "Use of inherited AUTOLOAD for non-method %s::%.*s() is deprecated",
356 HvNAME(stash), (int)len, name);
359 * Given &FOO::AUTOLOAD, set $FOO::AUTOLOAD to desired function name.
360 * The subroutine's original name may not be "AUTOLOAD", so we don't
361 * use that, but for lack of anything better we will use the sub's
362 * original package to look up $AUTOLOAD.
364 varstash = GvSTASH(CvGV(cv));
365 vargv = *(GV**)hv_fetch(varstash, autoload, autolen, TRUE);
367 gv_init(vargv, varstash, autoload, autolen, FALSE);
369 sv_setpv(varsv, HvNAME(stash));
370 sv_catpvn(varsv, "::", 2);
371 sv_catpvn(varsv, name, len);
372 SvTAINTED_off(varsv);
377 gv_stashpv(char *name, I32 create)
379 return gv_stashpvn(name, strlen(name), create);
383 gv_stashpvn(char *name, U32 namelen, I32 create)
390 if (namelen + 3 < sizeof smallbuf)
393 New(606, tmpbuf, namelen + 3, char);
394 Copy(name,tmpbuf,namelen,char);
395 tmpbuf[namelen++] = ':';
396 tmpbuf[namelen++] = ':';
397 tmpbuf[namelen] = '\0';
398 tmpgv = gv_fetchpv(tmpbuf, create, SVt_PVHV);
399 if (tmpbuf != smallbuf)
404 GvHV(tmpgv) = newHV();
407 HvNAME(stash) = savepv(name);
412 gv_stashsv(SV *sv, I32 create)
417 return gv_stashpvn(ptr, len, create);
422 gv_fetchpv(char *nambeg, I32 add, I32 sv_type)
425 register char *name = nambeg;
429 register char *namend;
433 if (*name == '*' && isALPHA(name[1])) /* accidental stringify on a GV? */
436 for (namend = name; *namend; namend++) {
437 if ((*namend == '\'' && namend[1]) ||
438 (*namend == ':' && namend[1] == ':'))
442 if (!stash || !SvREFCNT(stash)) /* symbol table under destruction */
450 if (len + 3 < sizeof smallbuf)
453 New(601, tmpbuf, len+3, char);
454 Copy(name, tmpbuf, len, char);
458 gvp = (GV**)hv_fetch(stash,tmpbuf,len,add);
459 gv = gvp ? *gvp : Nullgv;
460 if (gv && gv != (GV*)&sv_undef) {
461 if (SvTYPE(gv) != SVt_PVGV)
462 gv_init(gv, stash, tmpbuf, len, (add & 2));
466 if (tmpbuf != smallbuf)
468 if (!gv || gv == (GV*)&sv_undef)
471 if (!(stash = GvHV(gv)))
472 stash = GvHV(gv) = newHV();
475 HvNAME(stash) = savepvn(nambeg, namend - nambeg);
483 return gv ? gv : (GV*)*hv_fetch(defstash, "main::", 6, TRUE);
490 /* No stash in name, so see how we can default */
493 if (isIDFIRST(*name)) {
496 if (isUPPER(*name)) {
498 if (*name == 'S' && (
499 strEQ(name, "SIG") ||
500 strEQ(name, "STDIN") ||
501 strEQ(name, "STDOUT") ||
502 strEQ(name, "STDERR") ))
505 else if (*name > 'E') {
506 if (*name == 'I' && strEQ(name, "INC"))
509 else if (*name > 'A') {
510 if (*name == 'E' && strEQ(name, "ENV"))
513 else if (*name == 'A' && (
514 strEQ(name, "ARGV") ||
515 strEQ(name, "ARGVOUT") ))
518 else if (*name == '_' && !name[1])
523 else if ((COP*)curcop == &compiling) {
525 if (add && (hints & HINT_STRICT_VARS) &&
526 sv_type != SVt_PVCV &&
527 sv_type != SVt_PVGV &&
528 sv_type != SVt_PVFM &&
529 sv_type != SVt_PVIO &&
530 !(len == 1 && sv_type == SVt_PV && strchr("ab",*name)) )
532 gvp = (GV**)hv_fetch(stash,name,len,0);
534 *gvp == (GV*)&sv_undef ||
535 SvTYPE(*gvp) != SVt_PVGV)
539 else if (sv_type == SVt_PV && !GvIMPORTED_SV(*gvp) ||
540 sv_type == SVt_PVAV && !GvIMPORTED_AV(*gvp) ||
541 sv_type == SVt_PVHV && !GvIMPORTED_HV(*gvp) )
543 warn("Variable \"%c%s\" is not imported",
544 sv_type == SVt_PVAV ? '@' :
545 sv_type == SVt_PVHV ? '%' : '$',
548 warn("(Did you mean &%s instead?)\n", name);
554 stash = curcop->cop_stash;
560 /* By this point we should have a stash and a name */
566 char sv_type_char = ((sv_type == SVt_PV) ? '$'
567 : (sv_type == SVt_PVAV) ? '@'
568 : (sv_type == SVt_PVHV) ? '%'
571 warn("Global symbol \"%c%s\" requires explicit package name",
574 warn("Global symbol \"%s\" requires explicit package name",
578 stash = curstash ? curstash : defstash; /* avoid core dumps */
579 add_gvflags = ((sv_type == SVt_PV) ? GVf_IMPORTED_SV
580 : (sv_type == SVt_PVAV) ? GVf_IMPORTED_AV
581 : (sv_type == SVt_PVHV) ? GVf_IMPORTED_HV
585 if (!SvREFCNT(stash)) /* symbol table under destruction */
588 gvp = (GV**)hv_fetch(stash,name,len,add);
589 if (!gvp || *gvp == (GV*)&sv_undef)
592 if (SvTYPE(gv) == SVt_PVGV) {
595 gv_init_sv(gv, sv_type);
598 } else if (add & GV_NOINIT) {
602 /* Adding a new symbol */
604 if (add & GV_ADDWARN)
605 warn("Had to create %s unexpectedly", nambeg);
606 gv_init(gv, stash, name, len, add & GV_ADDMULTI);
607 gv_init_sv(gv, sv_type);
608 GvFLAGS(gv) |= add_gvflags;
610 /* set up magic where warranted */
613 if (strEQ(name, "ARGV")) {
614 IoFLAGS(GvIOn(gv)) |= IOf_ARGV|IOf_START;
624 if (strnEQ(name, "EXPORT", 6))
628 if (strEQ(name, "ISA")) {
631 sv_magic((SV*)av, (SV*)gv, 'I', Nullch, 0);
632 /* NOTE: No support for tied ISA */
633 if ((add & GV_ADDMULTI) && strEQ(nambeg,"AnyDBM_File::ISA")
634 && AvFILLp(av) == -1)
637 av_push(av, newSVpv(pname = "NDBM_File",0));
638 gv_stashpvn(pname, 9, TRUE);
639 av_push(av, newSVpv(pname = "DB_File",0));
640 gv_stashpvn(pname, 7, TRUE);
641 av_push(av, newSVpv(pname = "GDBM_File",0));
642 gv_stashpvn(pname, 9, TRUE);
643 av_push(av, newSVpv(pname = "SDBM_File",0));
644 gv_stashpvn(pname, 9, TRUE);
645 av_push(av, newSVpv(pname = "ODBM_File",0));
646 gv_stashpvn(pname, 9, TRUE);
652 if (strEQ(name, "OVERLOAD")) {
655 hv_magic(hv, gv, 'A');
658 #endif /* OVERLOAD */
660 if (strEQ(name, "SIG")) {
666 hv_magic(hv, siggv, 'S');
667 for(i=1;sig_name[i];i++) {
669 init=hv_fetch(hv,sig_name[i],strlen(sig_name[i]),1);
671 sv_setsv(*init,&sv_undef);
702 sv_setpv(GvSV(gv),chopset);
708 #ifdef COMPLEX_STATUS
709 sv_upgrade(GvSV(gv), SVt_PVLV);
716 if(sv_type > SVt_PV) {
717 HV* stash = gv_stashpvn("Errno",5,FALSE);
718 if(!stash || !(gv_fetchmethod(stash, "TIEHASH"))) {
721 perl_require_pv("Errno.pm");
723 stash = gv_stashpvn("Errno",5,FALSE);
724 if (!stash || !(gv_fetchmethod(stash, "TIEHASH")))
725 croak("Can't use %%! because Errno.pm is not available");
731 if (dowarn && len == 1 && sv_type == SVt_PV)
732 warn("Use of $%s is deprecated", name);
775 SvREADONLY_on(GvSV(gv));
777 sv_magic(GvSV(gv), (SV*)gv, 0, name, len);
783 sv_setpv(GvSV(gv),"\f");
789 sv_setpv(GvSV(gv),"\034");
794 sv_upgrade(sv, SVt_PVNV);
795 sv_setpv(sv, patchlevel);
805 gv_fullname3(SV *sv, GV *gv, char *prefix)
807 HV *hv = GvSTASH(gv);
812 sv_setpv(sv, prefix ? prefix : "");
813 sv_catpv(sv,HvNAME(hv));
814 sv_catpvn(sv,"::", 2);
815 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
819 gv_efullname3(SV *sv, GV *gv, char *prefix)
824 gv_fullname3(sv, egv, prefix);
827 /* XXX compatibility with versions <= 5.003. */
829 gv_fullname(SV *sv, GV *gv)
831 gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : "");
834 /* XXX compatibility with versions <= 5.003. */
836 gv_efullname(SV *sv, GV *gv)
838 gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : "");
848 io = (IO*)NEWSV(0,0);
849 sv_upgrade((SV *)io,SVt_PVIO);
852 iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV);
854 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
855 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
871 for (i = 0; i <= (I32) HvMAX(stash); i++) {
872 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
873 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
874 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)) && HvNAME(hv))
877 gv_check(hv); /* nested package */
879 else if (isALPHA(*HeKEY(entry))) {
880 gv = (GV*)HeVAL(entry);
881 if (SvTYPE(gv) != SVt_PVGV || GvMULTI(gv))
883 curcop->cop_line = GvLINE(gv);
884 filegv = GvFILEGV(gv);
885 curcop->cop_filegv = filegv;
886 if (filegv && GvMULTI(filegv)) /* Filename began with slash */
888 warn("Name \"%s::%s\" used only once: possible typo",
889 HvNAME(stash), GvNAME(gv));
898 return gv_fetchpv(form("%s::_GEN_%ld", pack, (long)gensym++),
902 /* hopefully this is only called on local symbol table entries */
910 /* multi-named GPs cannot be used for method cache */
911 SvREFCNT_dec(gp->gp_cv);
916 /* Adding a new name to a subroutine invalidates method cache */
929 if (!gv || !(gp = GvGP(gv)))
931 if (gp->gp_refcnt == 0) {
932 warn("Attempt to free unreferenced glob pointers");
936 /* Deleting the name of a subroutine invalidates method cache */
939 if (--gp->gp_refcnt > 0) {
940 if (gp->gp_egv == gv)
945 SvREFCNT_dec(gp->gp_sv);
946 SvREFCNT_dec(gp->gp_av);
947 SvREFCNT_dec(gp->gp_hv);
948 SvREFCNT_dec(gp->gp_io);
949 SvREFCNT_dec(gp->gp_cv);
950 SvREFCNT_dec(gp->gp_form);
956 #if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
960 #ifdef MICROPORT /* Microport 2.4 hack */
965 return GvGP(gv)->gp_av;
967 return GvGP(gv_AVadd(gv))->gp_av;
974 return GvGP(gv)->gp_hv;
976 return GvGP(gv_HVadd(gv))->gp_hv;
978 #endif /* Microport 2.4 hack */
981 /* Updates and caches the CV's */
984 Gv_AMupdate(HV *stash)
991 MAGIC* mg=mg_find((SV*)stash,'c');
992 AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL;
995 if (mg && amtp->was_ok_am == amagic_generation
996 && amtp->was_ok_sub == sub_generation)
997 return AMT_AMAGIC(amtp);
998 if (amtp && AMT_AMAGIC(amtp)) { /* Have table. */
1000 for (i=1; i<NofAMmeth; i++) {
1001 if (amtp->table[i]) {
1002 SvREFCNT_dec(amtp->table[i]);
1006 sv_unmagic((SV*)stash, 'c');
1008 DEBUG_o( deb("Recalcing overload magic in package %s\n",HvNAME(stash)) );
1010 amt.was_ok_am = amagic_generation;
1011 amt.was_ok_sub = sub_generation;
1012 amt.fallback = AMGfallNO;
1015 #ifdef OVERLOAD_VIA_HASH
1016 gvp=(GV**)hv_fetch(stash,"OVERLOAD",8,FALSE); /* A shortcut */
1017 if (gvp && ((gv = *gvp) != (GV*)&sv_undef && (hv = GvHV(gv)))) {
1024 /* Work with "fallback" key, which we assume to be first in AMG_names */
1026 if (( cp = (char *)AMG_names[0] ) &&
1027 (svp = (SV**)hv_fetch(hv,cp,strlen(cp),FALSE)) && (sv = *svp)) {
1028 if (SvTRUE(sv)) amt.fallback=AMGfallYES;
1029 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
1031 for (i = 1; i < NofAMmeth; i++) {
1033 cp = (char *)AMG_names[i];
1035 svp = (SV**)hv_fetch(hv, cp, strlen(cp), FALSE);
1036 if (svp && ((sv = *svp) != &sv_undef)) {
1037 switch (SvTYPE(sv)) {
1040 if (!SvOK(sv)) break;
1041 gv = gv_fetchmethod(stash, SvPV(sv, na));
1042 if (gv) cv = GvCV(gv);
1046 if (SvTYPE(cv) == SVt_PVCV)
1051 croak("Not a subroutine reference in overload table");
1057 if (!(cv = GvCVu((GV*)sv)))
1058 cv = sv_2cv(sv, &stash, &gv, TRUE);
1063 croak("Method for operation %s not found in package %.256s during blessing\n",
1076 /* Work with "fallback" key, which we assume to be first in AMG_names */
1078 if ( cp = AMG_names[0] ) {
1079 /* Try to find via inheritance. */
1080 gv = gv_fetchmeth(stash, "()", 2, -1); /* A cookie: "()". */
1081 if (gv) sv = GvSV(gv);
1083 if (!gv) goto no_table;
1084 else if (SvTRUE(sv)) amt.fallback=AMGfallYES;
1085 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
1088 for (i = 1; i < NofAMmeth; i++) {
1089 SV *cookie = sv_2mortal(newSVpvf("(%s", cp = AMG_names[i]));
1090 DEBUG_o( deb("Checking overloading of `%s' in package `%.256s'\n",
1091 cp, HvNAME(stash)) );
1092 /* don't fill the cache while looking up! */
1093 gv = gv_fetchmeth(stash, SvPVX(cookie), SvCUR(cookie), -1);
1095 if(gv && (cv = GvCV(gv))) {
1096 if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil")
1097 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) {
1098 /* GvSV contains the name of the method. */
1101 DEBUG_o( deb("Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n",
1102 SvPV(GvSV(gv), na), cp, HvNAME(stash)) );
1103 if (!SvPOK(GvSV(gv))
1104 || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)),
1107 /* Can be an import stub (created by `can'). */
1109 croak("Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'",
1110 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1113 croak("Cannot resolve method `%.256s' overloading `%s' in package `%.256s'",
1114 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1117 cv = GvCV(gv = ngv);
1119 DEBUG_o( deb("Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n",
1120 cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))),
1121 GvNAME(CvGV(cv))) );
1125 amt.table[i]=(CV*)SvREFCNT_inc(cv);
1128 AMT_AMAGIC_on(&amt);
1129 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMT));
1133 /* Here we have no table: */
1135 AMT_AMAGIC_off(&amt);
1136 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMTS));
1141 amagic_call(SV *left, SV *right, int method, int flags)
1146 CV **cvp=NULL, **ocvp=NULL;
1148 int fl=0, off, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
1149 int postpr=0, inc_dec_ass=0, assignshift=assign?1:0;
1151 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
1152 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),'c'))
1153 && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1154 ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table
1156 && ((cv = cvp[off=method+assignshift])
1157 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
1159 (fl = 1, cv = cvp[off=method])))) {
1160 lr = -1; /* Call method for left argument */
1162 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
1165 /* look for substituted methods */
1168 if (((cv = cvp[off=add_ass_amg]) && (inc_dec_ass=1))
1169 || ((cv = cvp[off=add_amg]) && (postpr=1))) {
1170 right = &sv_yes; lr = -1; assign = 1;
1174 if (((cv = cvp[off=subtr_ass_amg]) && (inc_dec_ass=1))
1175 || ((cv = cvp[off=subtr_amg]) && (postpr=1))) {
1176 right = &sv_yes; lr = -1; assign = 1;
1180 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
1183 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1186 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1189 (void)((cv = cvp[off=bool__amg])
1190 || (cv = cvp[off=numer_amg])
1191 || (cv = cvp[off=string_amg]));
1197 * SV* ref causes confusion with the interpreter variable of
1200 SV* tmpRef=SvRV(left);
1201 if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) {
1203 * Just to be extra cautious. Maybe in some
1204 * additional cases sv_setsv is safe, too.
1206 SV* newref = newSVsv(tmpRef);
1207 SvOBJECT_on(newref);
1208 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(tmpRef));
1214 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
1215 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
1216 SV* nullsv=sv_2mortal(newSViv(0));
1218 SV* lessp = amagic_call(left,nullsv,
1219 lt_amg,AMGf_noright);
1220 logic = SvTRUE(lessp);
1222 SV* lessp = amagic_call(left,nullsv,
1223 ncmp_amg,AMGf_noright);
1224 logic = (SvNV(lessp) < 0);
1227 if (off==subtr_amg) {
1238 if (cv = cvp[off=subtr_amg]) {
1240 left = sv_2mortal(newSViv(0));
1247 if (!cv) goto not_found;
1248 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1249 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c'))
1250 && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1251 ? (amtp = (AMT*)mg->mg_ptr)->table
1253 && (cv = cvp[off=method])) { /* Method for right
1256 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
1257 && (cvp=ocvp) && (lr = -1))
1258 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1259 && !(flags & AMGf_unary)) {
1260 /* We look for substitution for
1261 * comparison operations and
1263 if (method==concat_amg || method==concat_ass_amg
1264 || method==repeat_amg || method==repeat_ass_amg) {
1265 return NULL; /* Delegate operation to string conversion */
1275 postpr = 1; off=ncmp_amg; break;
1282 postpr = 1; off=scmp_amg; break;
1284 if (off != -1) cv = cvp[off];
1289 not_found: /* No method found, either report or croak */
1290 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1291 notfound = 1; lr = -1;
1292 } else if (cvp && (cv=cvp[nomethod_amg])) {
1293 notfound = 1; lr = 1;
1296 if (off==-1) off=method;
1297 msg = sv_2mortal(newSVpvf(
1298 "Operation `%s': no method found,%sargument %s%s%s%s",
1299 AMG_names[method + assignshift],
1300 (flags & AMGf_unary ? " " : "\n\tleft "),
1302 "in overloaded package ":
1303 "has no overloaded magic",
1305 HvNAME(SvSTASH(SvRV(left))):
1308 ",\n\tright argument in overloaded package ":
1311 : ",\n\tright argument has no overloaded magic"),
1313 HvNAME(SvSTASH(SvRV(right))):
1315 if (amtp && amtp->fallback >= AMGfallYES) {
1316 DEBUG_o( deb("%s", SvPVX(msg)) );
1326 "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n",
1328 method+assignshift==off? "" :
1330 method+assignshift==off? "" :
1331 AMG_names[method+assignshift],
1332 method+assignshift==off? "" : "')",
1333 flags & AMGf_unary? "" :
1334 lr==1 ? " for right argument": " for left argument",
1335 flags & AMGf_unary? " for argument" : "",
1337 fl? ",\n\tassignment variant used": "") );
1338 /* Since we use shallow copy during assignment, we need
1339 * to dublicate the contents, probably calling user-supplied
1340 * version of copy operator
1342 if ((method + assignshift==off
1343 && (assign || method==inc_amg || method==dec_amg))
1344 || inc_dec_ass) RvDEEPCP(left);
1350 bool oldcatch = CATCH_GET;
1353 Zero(&myop, 1, BINOP);
1354 myop.op_last = (OP *) &myop;
1355 myop.op_next = Nullop;
1356 myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED;
1358 PUSHSTACK(SI_OVERLOAD);
1362 if (PERLDB_SUB && curstash != debstash)
1363 op->op_private |= OPpENTERSUB_DB;
1367 EXTEND(SP, notfound + 5);
1368 PUSHs(lr>0? right: left);
1369 PUSHs(lr>0? left: right);
1370 PUSHs( lr > 0 ? &sv_yes : ( assign ? &sv_undef : &sv_no ));
1372 PUSHs( sv_2mortal(newSVpv((char *)AMG_names[method + assignshift],0)) );
1377 if (op = pp_entersub(ARGS))
1384 CATCH_SET(oldcatch);
1391 ans=SvIV(res)<=0; break;
1394 ans=SvIV(res)<0; break;
1397 ans=SvIV(res)>=0; break;
1400 ans=SvIV(res)>0; break;
1403 ans=SvIV(res)==0; break;
1406 ans=SvIV(res)!=0; break;
1409 SvSetSV(left,res); return left;
1411 ans=!SvOK(res); break;
1414 } else if (method==copy_amg) {
1416 croak("Copy method did not return a reference");
1418 return SvREFCNT_inc(SvRV(res));
1424 #endif /* OVERLOAD */