3 * Copyright (c) 1991-1999, 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(const 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, const 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);
110 if (multi || doproto) /* doproto means it _was_ mentioned */
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, const 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, const char *name)
278 return gv_fetchmethod_autoload(stash, name, TRUE);
282 gv_fetchmethod_autoload(HV *stash, const char *name, I32 autoload)
285 register const char *nend;
286 const char *nsplit = 0;
289 for (nend = name; *nend; nend++) {
292 else if (*nend == ':' && *(nend + 1) == ':')
296 const 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, const 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(const char *name, I32 create)
390 return gv_stashpvn(name, strlen(name), create);
394 gv_stashpvn(const 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(const char *nambeg, I32 add, I32 sv_type)
436 register const char *name = nambeg;
440 register const 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;
625 if (strnEQ(name, "EXPORT", 6))
629 if (strEQ(name, "ISA")) {
632 sv_magic((SV*)av, (SV*)gv, 'I', Nullch, 0);
633 /* NOTE: No support for tied ISA */
634 if ((add & GV_ADDMULTI) && strEQ(nambeg,"AnyDBM_File::ISA")
635 && AvFILLp(av) == -1)
638 av_push(av, newSVpvn(pname = "NDBM_File",9));
639 gv_stashpvn(pname, 9, TRUE);
640 av_push(av, newSVpvn(pname = "DB_File",7));
641 gv_stashpvn(pname, 7, TRUE);
642 av_push(av, newSVpvn(pname = "GDBM_File",9));
643 gv_stashpvn(pname, 9, TRUE);
644 av_push(av, newSVpvn(pname = "SDBM_File",9));
645 gv_stashpvn(pname, 9, TRUE);
646 av_push(av, newSVpvn(pname = "ODBM_File",9));
647 gv_stashpvn(pname, 9, TRUE);
652 if (strEQ(name, "OVERLOAD")) {
655 hv_magic(hv, gv, 'A');
659 if (strEQ(name, "SIG")) {
663 GvMULTI_on(PL_siggv);
664 hv = GvHVn(PL_siggv);
665 hv_magic(hv, PL_siggv, 'S');
666 for(i=1;PL_sig_name[i];i++) {
668 init=hv_fetch(hv,PL_sig_name[i],strlen(PL_sig_name[i]),1);
670 sv_setsv(*init,&PL_sv_undef);
681 PL_sawampersand = TRUE;
688 PL_sawampersand = TRUE;
695 PL_sawampersand = TRUE;
701 sv_setpv(GvSV(gv),PL_chopset);
707 #ifdef COMPLEX_STATUS
708 (void)SvUPGRADE(GvSV(gv), SVt_PVLV);
715 if (sv_type > SVt_PV && PL_curcop != &PL_compiling) {
716 HV* stash = gv_stashpvn("Errno",5,FALSE);
717 if(!stash || !(gv_fetchmethod(stash, "TIEHASH"))) {
720 perl_require_pv("Errno.pm");
722 stash = gv_stashpvn("Errno",5,FALSE);
723 if (!stash || !(gv_fetchmethod(stash, "TIEHASH")))
724 croak("Can't use %%! because Errno.pm is not available");
733 sv_magic((SV*)av, Nullsv, 'D', Nullch, 0);
738 if (ckWARN(WARN_DEPRECATED) && len == 1 && sv_type == SVt_PV)
739 warner(WARN_DEPRECATED, "Use of $%s is deprecated", name);
762 case '\011': /* NOT \t in EBCDIC */
780 sv_magic((SV*)av, (SV*)av, 'D', Nullch, 0);
793 SvREADONLY_on(GvSV(gv));
795 sv_magic(GvSV(gv), (SV*)gv, 0, name, len);
801 sv_setpv(GvSV(gv),"\f");
802 PL_formfeed = GvSV(gv);
807 sv_setpv(GvSV(gv),"\034");
812 (void)SvUPGRADE(sv, SVt_PVNV);
813 sv_setpv(sv, PL_patchlevel);
823 gv_fullname3(SV *sv, GV *gv, const char *prefix)
825 HV *hv = GvSTASH(gv);
830 sv_setpv(sv, prefix ? prefix : "");
831 sv_catpv(sv,HvNAME(hv));
832 sv_catpvn(sv,"::", 2);
833 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
837 gv_efullname3(SV *sv, GV *gv, const char *prefix)
842 gv_fullname3(sv, egv, prefix);
845 /* XXX compatibility with versions <= 5.003. */
847 gv_fullname(SV *sv, GV *gv)
849 gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : "");
852 /* XXX compatibility with versions <= 5.003. */
854 gv_efullname(SV *sv, GV *gv)
856 gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : "");
866 io = (IO*)NEWSV(0,0);
867 sv_upgrade((SV *)io,SVt_PVIO);
870 iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV);
871 /* unless exists($main::{FileHandle}) and defined(%main::FileHandle::) */
872 if (!(iogv && GvHV(iogv) && HvARRAY(GvHV(iogv))))
873 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
874 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
890 for (i = 0; i <= (I32) HvMAX(stash); i++) {
891 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
892 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
893 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)) && HvNAME(hv))
895 if (hv != PL_defstash)
896 gv_check(hv); /* nested package */
898 else if (isALPHA(*HeKEY(entry))) {
899 gv = (GV*)HeVAL(entry);
900 if (SvTYPE(gv) != SVt_PVGV || GvMULTI(gv))
902 PL_curcop->cop_line = GvLINE(gv);
903 filegv = GvFILEGV(gv);
904 PL_curcop->cop_filegv = filegv;
905 if (filegv && GvMULTI(filegv)) /* Filename began with slash */
908 "Name \"%s::%s\" used only once: possible typo",
909 HvNAME(stash), GvNAME(gv));
918 return gv_fetchpv(form("%s::_GEN_%ld", pack, (long)PL_gensym++),
922 /* hopefully this is only called on local symbol table entries */
930 /* multi-named GPs cannot be used for method cache */
931 SvREFCNT_dec(gp->gp_cv);
936 /* Adding a new name to a subroutine invalidates method cache */
949 if (!gv || !(gp = GvGP(gv)))
951 if (gp->gp_refcnt == 0) {
952 warn("Attempt to free unreferenced glob pointers");
956 /* Deleting the name of a subroutine invalidates method cache */
959 if (--gp->gp_refcnt > 0) {
960 if (gp->gp_egv == gv)
965 SvREFCNT_dec(gp->gp_sv);
966 SvREFCNT_dec(gp->gp_av);
967 SvREFCNT_dec(gp->gp_hv);
968 SvREFCNT_dec(gp->gp_io);
969 SvREFCNT_dec(gp->gp_cv);
970 SvREFCNT_dec(gp->gp_form);
976 #if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
980 #ifdef MICROPORT /* Microport 2.4 hack */
985 return GvGP(gv)->gp_av;
987 return GvGP(gv_AVadd(gv))->gp_av;
994 return GvGP(gv)->gp_hv;
996 return GvGP(gv_HVadd(gv))->gp_hv;
998 #endif /* Microport 2.4 hack */
1000 /* Updates and caches the CV's */
1003 Gv_AMupdate(HV *stash)
1010 MAGIC* mg=mg_find((SV*)stash,'c');
1011 AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL;
1015 if (mg && amtp->was_ok_am == PL_amagic_generation
1016 && amtp->was_ok_sub == PL_sub_generation)
1017 return AMT_AMAGIC(amtp);
1018 if (amtp && AMT_AMAGIC(amtp)) { /* Have table. */
1020 for (i=1; i<NofAMmeth; i++) {
1021 if (amtp->table[i]) {
1022 SvREFCNT_dec(amtp->table[i]);
1026 sv_unmagic((SV*)stash, 'c');
1028 DEBUG_o( deb("Recalcing overload magic in package %s\n",HvNAME(stash)) );
1030 amt.was_ok_am = PL_amagic_generation;
1031 amt.was_ok_sub = PL_sub_generation;
1032 amt.fallback = AMGfallNO;
1035 #ifdef OVERLOAD_VIA_HASH
1036 gvp=(GV**)hv_fetch(stash,"OVERLOAD",8,FALSE); /* A shortcut */
1037 if (gvp && ((gv = *gvp) != (GV*)&PL_sv_undef && (hv = GvHV(gv)))) {
1044 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
1046 if (( cp = (char *)PL_AMG_names[0] ) &&
1047 (svp = (SV**)hv_fetch(hv,cp,strlen(cp),FALSE)) && (sv = *svp)) {
1048 if (SvTRUE(sv)) amt.fallback=AMGfallYES;
1049 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
1051 for (i = 1; i < NofAMmeth; i++) {
1053 cp = (char *)PL_AMG_names[i];
1055 svp = (SV**)hv_fetch(hv, cp, strlen(cp), FALSE);
1056 if (svp && ((sv = *svp) != &PL_sv_undef)) {
1057 switch (SvTYPE(sv)) {
1060 if (!SvOK(sv)) break;
1061 gv = gv_fetchmethod(stash, SvPV(sv, n_a));
1062 if (gv) cv = GvCV(gv);
1066 if (SvTYPE(cv) == SVt_PVCV)
1071 croak("Not a subroutine reference in overload table");
1077 if (!(cv = GvCVu((GV*)sv)))
1078 cv = sv_2cv(sv, &stash, &gv, TRUE);
1083 croak("Method for operation %s not found in package %.256s during blessing\n",
1096 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
1098 if ( cp = PL_AMG_names[0] ) {
1099 /* Try to find via inheritance. */
1100 gv = gv_fetchmeth(stash, "()", 2, -1); /* A cookie: "()". */
1101 if (gv) sv = GvSV(gv);
1103 if (!gv) goto no_table;
1104 else if (SvTRUE(sv)) amt.fallback=AMGfallYES;
1105 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
1108 for (i = 1; i < NofAMmeth; i++) {
1109 SV *cookie = sv_2mortal(newSVpvf("(%s", cp = PL_AMG_names[i]));
1110 DEBUG_o( deb("Checking overloading of `%s' in package `%.256s'\n",
1111 cp, HvNAME(stash)) );
1112 /* don't fill the cache while looking up! */
1113 gv = gv_fetchmeth(stash, SvPVX(cookie), SvCUR(cookie), -1);
1115 if(gv && (cv = GvCV(gv))) {
1116 if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil")
1117 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) {
1118 /* GvSV contains the name of the method. */
1121 DEBUG_o( deb("Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n",
1122 SvPV(GvSV(gv), n_a), cp, HvNAME(stash)) );
1123 if (!SvPOK(GvSV(gv))
1124 || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)),
1127 /* Can be an import stub (created by `can'). */
1129 croak("Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'",
1130 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1133 croak("Cannot resolve method `%.256s' overloading `%s' in package `%.256s'",
1134 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1137 cv = GvCV(gv = ngv);
1139 DEBUG_o( deb("Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n",
1140 cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))),
1141 GvNAME(CvGV(cv))) );
1145 amt.table[i]=(CV*)SvREFCNT_inc(cv);
1148 AMT_AMAGIC_on(&amt);
1149 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMT));
1153 /* Here we have no table: */
1155 AMT_AMAGIC_off(&amt);
1156 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMTS));
1161 amagic_call(SV *left, SV *right, int method, int flags)
1166 CV **cvp=NULL, **ocvp=NULL;
1168 int fl=0, off, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
1169 int postpr = 0, force_cpy = 0, assignshift = assign ? 1 : 0;
1171 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
1172 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),'c'))
1173 && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1174 ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table
1176 && ((cv = cvp[off=method+assignshift])
1177 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
1179 (fl = 1, cv = cvp[off=method])))) {
1180 lr = -1; /* Call method for left argument */
1182 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
1185 /* look for substituted methods */
1186 /* In all the covered cases we should be called with assign==0. */
1190 if ((cv = cvp[off=add_ass_amg])
1191 || ((cv = cvp[off = add_amg]) && (force_cpy = 0, postpr = 1))) {
1192 right = &PL_sv_yes; lr = -1; assign = 1;
1197 if ((cv = cvp[off = subtr_ass_amg])
1198 || ((cv = cvp[off = subtr_amg]) && (force_cpy = 0, postpr=1))) {
1199 right = &PL_sv_yes; lr = -1; assign = 1;
1203 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
1206 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1209 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1212 (void)((cv = cvp[off=bool__amg])
1213 || (cv = cvp[off=numer_amg])
1214 || (cv = cvp[off=string_amg]));
1220 * SV* ref causes confusion with the interpreter variable of
1223 SV* tmpRef=SvRV(left);
1224 if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) {
1226 * Just to be extra cautious. Maybe in some
1227 * additional cases sv_setsv is safe, too.
1229 SV* newref = newSVsv(tmpRef);
1230 SvOBJECT_on(newref);
1231 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(tmpRef));
1237 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
1238 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
1239 SV* nullsv=sv_2mortal(newSViv(0));
1241 SV* lessp = amagic_call(left,nullsv,
1242 lt_amg,AMGf_noright);
1243 logic = SvTRUE(lessp);
1245 SV* lessp = amagic_call(left,nullsv,
1246 ncmp_amg,AMGf_noright);
1247 logic = (SvNV(lessp) < 0);
1250 if (off==subtr_amg) {
1261 if (cv = cvp[off=subtr_amg]) {
1263 left = sv_2mortal(newSViv(0));
1267 case iter_amg: /* XXXX Eventually should do to_gv. */
1274 return NULL; /* Delegate operation to standard mechanisms. */
1279 if (!cv) goto not_found;
1280 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1281 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c'))
1282 && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1283 ? (amtp = (AMT*)mg->mg_ptr)->table
1285 && (cv = cvp[off=method])) { /* Method for right
1288 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
1289 && (cvp=ocvp) && (lr = -1))
1290 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1291 && !(flags & AMGf_unary)) {
1292 /* We look for substitution for
1293 * comparison operations and
1295 if (method==concat_amg || method==concat_ass_amg
1296 || method==repeat_amg || method==repeat_ass_amg) {
1297 return NULL; /* Delegate operation to string conversion */
1307 postpr = 1; off=ncmp_amg; break;
1314 postpr = 1; off=scmp_amg; break;
1316 if (off != -1) cv = cvp[off];
1321 not_found: /* No method found, either report or croak */
1322 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1323 notfound = 1; lr = -1;
1324 } else if (cvp && (cv=cvp[nomethod_amg])) {
1325 notfound = 1; lr = 1;
1328 if (off==-1) off=method;
1329 msg = sv_2mortal(newSVpvf(
1330 "Operation `%s': no method found,%sargument %s%s%s%s",
1331 PL_AMG_names[method + assignshift],
1332 (flags & AMGf_unary ? " " : "\n\tleft "),
1334 "in overloaded package ":
1335 "has no overloaded magic",
1337 HvNAME(SvSTASH(SvRV(left))):
1340 ",\n\tright argument in overloaded package ":
1343 : ",\n\tright argument has no overloaded magic"),
1345 HvNAME(SvSTASH(SvRV(right))):
1347 if (amtp && amtp->fallback >= AMGfallYES) {
1348 DEBUG_o( deb("%s", SvPVX(msg)) );
1354 force_cpy = force_cpy || assign;
1359 "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n",
1361 method+assignshift==off? "" :
1363 method+assignshift==off? "" :
1364 PL_AMG_names[method+assignshift],
1365 method+assignshift==off? "" : "')",
1366 flags & AMGf_unary? "" :
1367 lr==1 ? " for right argument": " for left argument",
1368 flags & AMGf_unary? " for argument" : "",
1370 fl? ",\n\tassignment variant used": "") );
1372 /* Since we use shallow copy during assignment, we need
1373 * to dublicate the contents, probably calling user-supplied
1374 * version of copy operator
1376 /* We need to copy in following cases:
1377 * a) Assignment form was called.
1378 * assignshift==1, assign==T, method + 1 == off
1379 * b) Increment or decrement, called directly.
1380 * assignshift==0, assign==0, method + 0 == off
1381 * c) Increment or decrement, translated to assignment add/subtr.
1382 * assignshift==0, assign==T,
1384 * d) Increment or decrement, translated to nomethod.
1385 * assignshift==0, assign==0,
1387 * e) Assignment form translated to nomethod.
1388 * assignshift==1, assign==T, method + 1 != off
1391 /* off is method, method+assignshift, or a result of opcode substitution.
1392 * In the latter case assignshift==0, so only notfound case is important.
1394 if (( (method + assignshift == off)
1395 && (assign || (method == inc_amg) || (method == dec_amg)))
1402 bool oldcatch = CATCH_GET;
1405 Zero(&myop, 1, BINOP);
1406 myop.op_last = (OP *) &myop;
1407 myop.op_next = Nullop;
1408 myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED;
1410 PUSHSTACKi(PERLSI_OVERLOAD);
1413 PL_op = (OP *) &myop;
1414 if (PERLDB_SUB && PL_curstash != PL_debstash)
1415 PL_op->op_private |= OPpENTERSUB_DB;
1419 EXTEND(SP, notfound + 5);
1420 PUSHs(lr>0? right: left);
1421 PUSHs(lr>0? left: right);
1422 PUSHs( lr > 0 ? &PL_sv_yes : ( assign ? &PL_sv_undef : &PL_sv_no ));
1424 PUSHs( sv_2mortal(newSVpv((char *)PL_AMG_names[method + assignshift],0)));
1429 if (PL_op = pp_entersub(ARGS))
1437 CATCH_SET(oldcatch);
1444 ans=SvIV(res)<=0; break;
1447 ans=SvIV(res)<0; break;
1450 ans=SvIV(res)>=0; break;
1453 ans=SvIV(res)>0; break;
1456 ans=SvIV(res)==0; break;
1459 ans=SvIV(res)!=0; break;
1462 SvSetSV(left,res); return left;
1464 ans=!SvOK(res); break;
1467 } else if (method==copy_amg) {
1469 croak("Copy method did not return a reference");
1471 return SvREFCNT_inc(SvRV(res));