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,'
24 Perl_gv_AVadd(pTHX_ register GV *gv)
26 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
27 Perl_croak(aTHX_ "Bad symbol for array");
34 Perl_gv_HVadd(pTHX_ register GV *gv)
36 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
37 Perl_croak(aTHX_ "Bad symbol for hash");
44 Perl_gv_IOadd(pTHX_ register GV *gv)
46 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
47 Perl_croak(aTHX_ "Bad symbol for filehandle");
54 Perl_gv_fetchfile(pTHX_ const char *name)
65 tmplen = strlen(name) + 2;
66 if (tmplen < sizeof smallbuf)
69 New(603, tmpbuf, tmplen + 1, char);
72 strcpy(tmpbuf + 2, name);
73 gv = *(GV**)hv_fetch(PL_defstash, tmpbuf, tmplen, TRUE);
75 gv_init(gv, PL_defstash, tmpbuf, tmplen, FALSE);
76 sv_setpv(GvSV(gv), name);
78 hv_magic(GvHVn(gv_AVadd(gv)), gv, 'L');
80 if (tmpbuf != smallbuf)
86 Perl_gv_init(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len, int multi)
90 bool doproto = SvTYPE(gv) > SVt_NULL;
91 char *proto = (doproto && SvPOK(gv)) ? SvPVX(gv) : NULL;
93 sv_upgrade((SV*)gv, SVt_PVGV);
102 Newz(602, gp, 1, GP);
103 GvGP(gv) = gp_ref(gp);
104 GvSV(gv) = NEWSV(72,0);
105 GvLINE(gv) = CopLINE(PL_curcop);
106 GvFILE(gv) = CopFILE(PL_curcop) ? CopFILE(PL_curcop) : "";
109 sv_magic((SV*)gv, (SV*)gv, '*', name, len);
110 GvSTASH(gv) = (HV*)SvREFCNT_inc(stash);
111 GvNAME(gv) = savepvn(name, len);
113 if (multi || doproto) /* doproto means it _was_ mentioned */
115 if (doproto) { /* Replicate part of newSUB here. */
118 /* XXX unsafe for threads if eval_owner isn't held */
119 start_subparse(0,0); /* Create CV in compcv. */
120 GvCV(gv) = PL_compcv;
124 CvGV(GvCV(gv)) = (GV*)SvREFCNT_inc(gv);
125 CvFILE(GvCV(gv)) = CopFILE(PL_curcop);
126 CvSTASH(GvCV(gv)) = PL_curstash;
128 CvOWNER(GvCV(gv)) = 0;
129 if (!CvMUTEXP(GvCV(gv))) {
130 New(666, CvMUTEXP(GvCV(gv)), 1, perl_mutex);
131 MUTEX_INIT(CvMUTEXP(GvCV(gv)));
133 #endif /* USE_THREADS */
135 sv_setpv((SV*)GvCV(gv), proto);
142 S_gv_init_sv(pTHX_ GV *gv, I32 sv_type)
158 Perl_gv_fetchmeth(pTHX_ HV *stash, const char *name, STRLEN len, I32 level)
168 if ((level > 100) || (level < -100))
169 Perl_croak(aTHX_ "Recursive inheritance detected while looking for method '%s' in package '%s'",
170 name, HvNAME(stash));
172 DEBUG_o( Perl_deb(aTHX_ "Looking for method %s in package %s\n",name,HvNAME(stash)) );
174 gvp = (GV**)hv_fetch(stash, name, len, (level >= 0));
179 if (SvTYPE(topgv) != SVt_PVGV)
180 gv_init(topgv, stash, name, len, TRUE);
181 if (cv = GvCV(topgv)) {
182 /* If genuine method or valid cache entry, use it */
183 if (!GvCVGEN(topgv) || GvCVGEN(topgv) == PL_sub_generation)
185 /* Stale cached entry: junk it */
187 GvCV(topgv) = cv = Nullcv;
190 else if (GvCVGEN(topgv) == PL_sub_generation)
191 return 0; /* cache indicates sub doesn't exist */
194 gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE);
195 av = (gvp && (gv = *gvp) && gv != (GV*)&PL_sv_undef) ? GvAV(gv) : Nullav;
197 /* create and re-create @.*::SUPER::ISA on demand */
198 if (!av || !SvMAGIC(av)) {
199 char* packname = HvNAME(stash);
200 STRLEN packlen = strlen(packname);
202 if (packlen >= 7 && strEQ(packname + packlen - 7, "::SUPER")) {
206 basestash = gv_stashpvn(packname, packlen, TRUE);
207 gvp = (GV**)hv_fetch(basestash, "ISA", 3, FALSE);
208 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
209 dTHR; /* just for SvREFCNT_dec */
210 gvp = (GV**)hv_fetch(stash, "ISA", 3, TRUE);
211 if (!gvp || !(gv = *gvp))
212 Perl_croak(aTHX_ "Cannot create %s::ISA", HvNAME(stash));
213 if (SvTYPE(gv) != SVt_PVGV)
214 gv_init(gv, stash, "ISA", 3, TRUE);
215 SvREFCNT_dec(GvAV(gv));
216 GvAV(gv) = (AV*)SvREFCNT_inc(av);
222 SV** svp = AvARRAY(av);
223 /* NOTE: No support for tied ISA */
224 I32 items = AvFILLp(av) + 1;
227 HV* basestash = gv_stashsv(sv, FALSE);
229 dTHR; /* just for ckWARN */
230 if (ckWARN(WARN_MISC))
231 Perl_warner(aTHX_ WARN_MISC, "Can't locate package %s for @%s::ISA",
232 SvPVX(sv), HvNAME(stash));
235 gv = gv_fetchmeth(basestash, name, len,
236 (level >= 0) ? level + 1 : level - 1);
242 /* if at top level, try UNIVERSAL */
244 if (level == 0 || level == -1) {
247 if (lastchance = gv_stashpvn("UNIVERSAL", 9, FALSE)) {
248 if (gv = gv_fetchmeth(lastchance, name, len,
249 (level >= 0) ? level + 1 : level - 1)) {
252 * Cache method in topgv if:
253 * 1. topgv has no synonyms (else inheritance crosses wires)
254 * 2. method isn't a stub (else AUTOLOAD fails spectacularly)
257 GvREFCNT(topgv) == 1 &&
259 (CvROOT(cv) || CvXSUB(cv)))
261 if (cv = GvCV(topgv))
263 GvCV(topgv) = (CV*)SvREFCNT_inc(GvCV(gv));
264 GvCVGEN(topgv) = PL_sub_generation;
268 else if (topgv && GvREFCNT(topgv) == 1) {
269 /* cache the fact that the method is not defined */
270 GvCVGEN(topgv) = PL_sub_generation;
279 Perl_gv_fetchmethod(pTHX_ HV *stash, const char *name)
281 return gv_fetchmethod_autoload(stash, name, TRUE);
285 Perl_gv_fetchmethod_autoload(pTHX_ HV *stash, const char *name, I32 autoload)
288 register const char *nend;
289 const char *nsplit = 0;
292 for (nend = name; *nend; nend++) {
295 else if (*nend == ':' && *(nend + 1) == ':')
299 const char *origname = name;
303 if ((nsplit - origname) == 5 && strnEQ(origname, "SUPER", 5)) {
304 /* ->SUPER::method should really be looked up in original stash */
305 SV *tmpstr = sv_2mortal(Perl_newSVpvf(aTHX_ "%s::SUPER",
306 CopSTASHPV(PL_curcop)));
307 stash = gv_stashpvn(SvPVX(tmpstr), SvCUR(tmpstr), TRUE);
308 DEBUG_o( Perl_deb(aTHX_ "Treating %s as %s::%s\n",
309 origname, HvNAME(stash), name) );
312 stash = gv_stashpvn(origname, nsplit - origname, TRUE);
315 gv = gv_fetchmeth(stash, name, nend - name, 0);
317 if (strEQ(name,"import"))
318 gv = (GV*)&PL_sv_yes;
320 gv = gv_autoload4(stash, name, nend - name, TRUE);
324 if (!CvROOT(cv) && !CvXSUB(cv)) {
332 if (GvCV(stubgv) != cv) /* orphaned import */
335 autogv = gv_autoload4(GvSTASH(stubgv),
336 GvNAME(stubgv), GvNAMELEN(stubgv), TRUE);
346 Perl_gv_autoload4(pTHX_ HV *stash, const char *name, STRLEN len, I32 method)
349 static char autoload[] = "AUTOLOAD";
350 static STRLEN autolen = 8;
357 if (len == autolen && strnEQ(name, autoload, autolen))
359 if (!(gv = gv_fetchmeth(stash, autoload, autolen, FALSE)))
364 * Inheriting AUTOLOAD for non-methods works ... for now.
366 if (ckWARN(WARN_DEPRECATED) && !method &&
367 (GvCVGEN(gv) || GvSTASH(gv) != stash))
368 Perl_warner(aTHX_ WARN_DEPRECATED,
369 "Use of inherited AUTOLOAD for non-method %s::%.*s() is deprecated",
370 HvNAME(stash), (int)len, name);
373 * Given &FOO::AUTOLOAD, set $FOO::AUTOLOAD to desired function name.
374 * The subroutine's original name may not be "AUTOLOAD", so we don't
375 * use that, but for lack of anything better we will use the sub's
376 * original package to look up $AUTOLOAD.
378 varstash = GvSTASH(CvGV(cv));
379 vargv = *(GV**)hv_fetch(varstash, autoload, autolen, TRUE);
381 gv_init(vargv, varstash, autoload, autolen, FALSE);
383 sv_setpv(varsv, HvNAME(stash));
384 sv_catpvn(varsv, "::", 2);
385 sv_catpvn(varsv, name, len);
386 SvTAINTED_off(varsv);
391 Perl_gv_stashpv(pTHX_ const char *name, I32 create)
393 return gv_stashpvn(name, strlen(name), create);
397 Perl_gv_stashpvn(pTHX_ const char *name, U32 namelen, I32 create)
404 if (namelen + 3 < sizeof smallbuf)
407 New(606, tmpbuf, namelen + 3, char);
408 Copy(name,tmpbuf,namelen,char);
409 tmpbuf[namelen++] = ':';
410 tmpbuf[namelen++] = ':';
411 tmpbuf[namelen] = '\0';
412 tmpgv = gv_fetchpv(tmpbuf, create, SVt_PVHV);
413 if (tmpbuf != smallbuf)
418 GvHV(tmpgv) = newHV();
421 HvNAME(stash) = savepv(name);
426 Perl_gv_stashsv(pTHX_ SV *sv, I32 create)
431 return gv_stashpvn(ptr, len, create);
436 Perl_gv_fetchpv(pTHX_ const char *nambeg, I32 add, I32 sv_type)
439 register const char *name = nambeg;
443 register const char *namend;
447 if (*name == '*' && isALPHA(name[1])) /* accidental stringify on a GV? */
450 for (namend = name; *namend; namend++) {
451 if ((*namend == ':' && namend[1] == ':')
452 || (*namend == '\'' && namend[1]))
456 if (!stash || !SvREFCNT(stash)) /* symbol table under destruction */
464 if (len + 3 < sizeof smallbuf)
467 New(601, tmpbuf, len+3, char);
468 Copy(name, tmpbuf, len, char);
472 gvp = (GV**)hv_fetch(stash,tmpbuf,len,add);
473 gv = gvp ? *gvp : Nullgv;
474 if (gv && gv != (GV*)&PL_sv_undef) {
475 if (SvTYPE(gv) != SVt_PVGV)
476 gv_init(gv, stash, tmpbuf, len, (add & GV_ADDMULTI));
480 if (tmpbuf != smallbuf)
482 if (!gv || gv == (GV*)&PL_sv_undef)
485 if (!(stash = GvHV(gv)))
486 stash = GvHV(gv) = newHV();
489 HvNAME(stash) = savepvn(nambeg, namend - nambeg);
497 return gv ? gv : (GV*)*hv_fetch(PL_defstash, "main::", 6, TRUE);
504 /* No stash in name, so see how we can default */
508 || (IN_UTF8 && ((*name & 0xc0) == 0xc0) && isIDFIRST_utf8((U8*)name)))
512 if (isUPPER(*name)) {
513 if (*name == 'S' && (
514 strEQ(name, "SIG") ||
515 strEQ(name, "STDIN") ||
516 strEQ(name, "STDOUT") ||
517 strEQ(name, "STDERR")))
519 else if (*name == 'I' && strEQ(name, "INC"))
521 else if (*name == 'E' && strEQ(name, "ENV"))
523 else if (*name == 'A' && (
524 strEQ(name, "ARGV") ||
525 strEQ(name, "ARGVOUT")))
528 else if (*name == '_' && !name[1])
533 else if ((COP*)PL_curcop == &PL_compiling) {
535 if (add && (PL_hints & HINT_STRICT_VARS) &&
536 !(add & GV_ADDOUR) &&
537 sv_type != SVt_PVCV &&
538 sv_type != SVt_PVGV &&
539 sv_type != SVt_PVFM &&
540 sv_type != SVt_PVIO &&
541 !(len == 1 && sv_type == SVt_PV && strchr("ab",*name)) )
543 gvp = (GV**)hv_fetch(stash,name,len,0);
545 *gvp == (GV*)&PL_sv_undef ||
546 SvTYPE(*gvp) != SVt_PVGV)
550 else if (sv_type == SVt_PV && !GvIMPORTED_SV(*gvp) ||
551 sv_type == SVt_PVAV && !GvIMPORTED_AV(*gvp) ||
552 sv_type == SVt_PVHV && !GvIMPORTED_HV(*gvp) )
554 Perl_warn(aTHX_ "Variable \"%c%s\" is not imported",
555 sv_type == SVt_PVAV ? '@' :
556 sv_type == SVt_PVHV ? '%' : '$',
559 Perl_warn(aTHX_ "(Did you mean &%s instead?)\n", name);
565 stash = CopSTASH(PL_curcop);
571 /* By this point we should have a stash and a name */
575 qerror(Perl_mess(aTHX_
576 "Global symbol \"%s%s\" requires explicit package name",
577 (sv_type == SVt_PV ? "$"
578 : sv_type == SVt_PVAV ? "@"
579 : sv_type == SVt_PVHV ? "%"
585 if (!SvREFCNT(stash)) /* symbol table under destruction */
588 gvp = (GV**)hv_fetch(stash,name,len,add);
589 if (!gvp || *gvp == (GV*)&PL_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 && ckWARN_d(WARN_INTERNAL))
605 Perl_warner(aTHX_ WARN_INTERNAL, "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 if (isLEXWARN_on && isALPHA(name[0]) && ! ckWARN(WARN_ONCE))
613 /* set up magic where warranted */
616 if (strEQ(name, "ARGV")) {
617 IoFLAGS(GvIOn(gv)) |= IOf_ARGV|IOf_START;
621 if (strnEQ(name, "EXPORT", 6))
625 if (strEQ(name, "ISA")) {
628 sv_magic((SV*)av, (SV*)gv, 'I', Nullch, 0);
629 /* NOTE: No support for tied ISA */
630 if ((add & GV_ADDMULTI) && strEQ(nambeg,"AnyDBM_File::ISA")
631 && AvFILLp(av) == -1)
634 av_push(av, newSVpvn(pname = "NDBM_File",9));
635 gv_stashpvn(pname, 9, TRUE);
636 av_push(av, newSVpvn(pname = "DB_File",7));
637 gv_stashpvn(pname, 7, TRUE);
638 av_push(av, newSVpvn(pname = "GDBM_File",9));
639 gv_stashpvn(pname, 9, TRUE);
640 av_push(av, newSVpvn(pname = "SDBM_File",9));
641 gv_stashpvn(pname, 9, TRUE);
642 av_push(av, newSVpvn(pname = "ODBM_File",9));
643 gv_stashpvn(pname, 9, TRUE);
648 if (strEQ(name, "OVERLOAD")) {
651 hv_magic(hv, gv, 'A');
655 if (strEQ(name, "SIG")) {
659 int sig_num[] = { SIG_NUM };
660 New(73, PL_psig_ptr, sizeof(sig_num)/sizeof(*sig_num), SV*);
661 New(73, PL_psig_name, sizeof(sig_num)/sizeof(*sig_num), SV*);
665 hv_magic(hv, gv, '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);
677 if (strEQ(name, "VERSION"))
684 PL_sawampersand = TRUE;
690 PL_sawampersand = TRUE;
696 PL_sawampersand = TRUE;
702 sv_setpv(GvSV(gv),PL_chopset);
708 #ifdef COMPLEX_STATUS
709 (void)SvUPGRADE(GvSV(gv), SVt_PVLV);
716 if (sv_type > SVt_PV && PL_curcop != &PL_compiling) {
717 HV* stash = gv_stashpvn("Errno",5,FALSE);
718 if (!stash || !(gv_fetchmethod(stash, "TIEHASH"))) {
721 require_pv("Errno.pm");
723 stash = gv_stashpvn("Errno",5,FALSE);
724 if (!stash || !(gv_fetchmethod(stash, "TIEHASH")))
725 Perl_croak(aTHX_ "Can't use %%! because Errno.pm is not available");
734 sv_magic((SV*)av, Nullsv, 'D', Nullch, 0);
739 if (ckWARN(WARN_DEPRECATED) && len == 1 && sv_type == SVt_PV)
740 Perl_warner(aTHX_ WARN_DEPRECATED, "Use of $%s is deprecated", name);
762 case '\011': /* NOT \t in EBCDIC */
773 case '\027': /* $^W & $^Warnings */
774 if (len > 1 && strNE(name, "\027arnings"))
783 sv_magic((SV*)av, (SV*)av, 'D', Nullch, 0);
796 SvREADONLY_on(GvSV(gv));
798 sv_magic(GvSV(gv), (SV*)gv, 0, name, len);
804 sv_setpv(GvSV(gv),"\f");
805 PL_formfeed = GvSV(gv);
810 sv_setpv(GvSV(gv),"\034");
815 (void)SvUPGRADE(sv, SVt_PVNV);
816 sv_setpv(sv, PL_patchlevel);
826 Perl_gv_fullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
828 HV *hv = GvSTASH(gv);
833 sv_setpv(sv, prefix ? prefix : "");
834 sv_catpv(sv,HvNAME(hv));
835 sv_catpvn(sv,"::", 2);
836 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
840 Perl_gv_efullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
845 gv_fullname3(sv, egv, prefix);
848 /* XXX compatibility with versions <= 5.003. */
850 Perl_gv_fullname(pTHX_ SV *sv, GV *gv)
852 gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : "");
855 /* XXX compatibility with versions <= 5.003. */
857 Perl_gv_efullname(pTHX_ SV *sv, GV *gv)
859 gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : "");
869 io = (IO*)NEWSV(0,0);
870 sv_upgrade((SV *)io,SVt_PVIO);
873 iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV);
874 /* unless exists($main::{FileHandle}) and defined(%main::FileHandle::) */
875 if (!(iogv && GvHV(iogv) && HvARRAY(GvHV(iogv))))
876 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
877 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
882 Perl_gv_check(pTHX_ HV *stash)
892 for (i = 0; i <= (I32) HvMAX(stash); i++) {
893 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
894 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
895 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)) && HvNAME(hv))
897 if (hv != PL_defstash && hv != stash)
898 gv_check(hv); /* nested package */
900 else if (isALPHA(*HeKEY(entry))) {
902 gv = (GV*)HeVAL(entry);
903 if (SvTYPE(gv) != SVt_PVGV || GvMULTI(gv))
906 /* performance hack: if filename is absolute and it's a standard
907 * module, don't bother warning */
909 && PERL_FILE_IS_ABSOLUTE(file)
910 && (instr(file, "/lib/") || instr(file, ".pm")))
914 CopLINE_set(PL_curcop, GvLINE(gv));
916 CopFILE(PL_curcop) = file; /* set for warning */
918 CopFILEGV(PL_curcop) = gv_fetchfile(file);
920 Perl_warner(aTHX_ WARN_ONCE,
921 "Name \"%s::%s\" used only once: possible typo",
922 HvNAME(stash), GvNAME(gv));
929 Perl_newGVgen(pTHX_ char *pack)
931 return gv_fetchpv(Perl_form(aTHX_ "%s::_GEN_%ld", pack, (long)PL_gensym++),
935 /* hopefully this is only called on local symbol table entries */
938 Perl_gp_ref(pTHX_ GP *gp)
945 /* multi-named GPs cannot be used for method cache */
946 SvREFCNT_dec(gp->gp_cv);
951 /* Adding a new name to a subroutine invalidates method cache */
959 Perl_gp_free(pTHX_ GV *gv)
965 if (!gv || !(gp = GvGP(gv)))
967 if (gp->gp_refcnt == 0) {
968 if (ckWARN_d(WARN_INTERNAL))
969 Perl_warner(aTHX_ WARN_INTERNAL,
970 "Attempt to free unreferenced glob pointers");
974 /* Deleting the name of a subroutine invalidates method cache */
977 if (--gp->gp_refcnt > 0) {
978 if (gp->gp_egv == gv)
983 SvREFCNT_dec(gp->gp_sv);
984 SvREFCNT_dec(gp->gp_av);
985 SvREFCNT_dec(gp->gp_hv);
986 SvREFCNT_dec(gp->gp_io);
987 SvREFCNT_dec(gp->gp_cv);
988 SvREFCNT_dec(gp->gp_form);
994 #if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
998 #ifdef MICROPORT /* Microport 2.4 hack */
1002 if (GvGP(gv)->gp_av)
1003 return GvGP(gv)->gp_av;
1005 return GvGP(gv_AVadd(gv))->gp_av;
1011 if (GvGP(gv)->gp_hv)
1012 return GvGP(gv)->gp_hv;
1014 return GvGP(gv_HVadd(gv))->gp_hv;
1016 #endif /* Microport 2.4 hack */
1018 /* Updates and caches the CV's */
1021 Perl_Gv_AMupdate(pTHX_ HV *stash)
1028 MAGIC* mg=mg_find((SV*)stash,'c');
1029 AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL;
1033 if (mg && amtp->was_ok_am == PL_amagic_generation
1034 && amtp->was_ok_sub == PL_sub_generation)
1035 return AMT_AMAGIC(amtp);
1036 if (amtp && AMT_AMAGIC(amtp)) { /* Have table. */
1038 for (i=1; i<NofAMmeth; i++) {
1039 if (amtp->table[i]) {
1040 SvREFCNT_dec(amtp->table[i]);
1044 sv_unmagic((SV*)stash, 'c');
1046 DEBUG_o( Perl_deb(aTHX_ "Recalcing overload magic in package %s\n",HvNAME(stash)) );
1048 amt.was_ok_am = PL_amagic_generation;
1049 amt.was_ok_sub = PL_sub_generation;
1050 amt.fallback = AMGfallNO;
1053 #ifdef OVERLOAD_VIA_HASH
1054 gvp=(GV**)hv_fetch(stash,"OVERLOAD",8,FALSE); /* A shortcut */
1055 if (gvp && ((gv = *gvp) != (GV*)&PL_sv_undef && (hv = GvHV(gv)))) {
1062 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
1064 if (( cp = (char *)PL_AMG_names[0] ) &&
1065 (svp = (SV**)hv_fetch(hv,cp,strlen(cp),FALSE)) && (sv = *svp)) {
1066 if (SvTRUE(sv)) amt.fallback=AMGfallYES;
1067 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
1069 for (i = 1; i < NofAMmeth; i++) {
1071 cp = (char *)PL_AMG_names[i];
1073 svp = (SV**)hv_fetch(hv, cp, strlen(cp), FALSE);
1074 if (svp && ((sv = *svp) != &PL_sv_undef)) {
1075 switch (SvTYPE(sv)) {
1078 if (!SvOK(sv)) break;
1079 gv = gv_fetchmethod(stash, SvPV(sv, n_a));
1080 if (gv) cv = GvCV(gv);
1084 if (SvTYPE(cv) == SVt_PVCV)
1089 Perl_croak(aTHX_ "Not a subroutine reference in overload table");
1095 if (!(cv = GvCVu((GV*)sv)))
1096 cv = sv_2cv(sv, &stash, &gv, FALSE);
1101 Perl_croak(aTHX_ "Method for operation %s not found in package %.256s during blessing\n",
1114 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
1116 if ( cp = PL_AMG_names[0] ) {
1117 /* Try to find via inheritance. */
1118 gv = gv_fetchmeth(stash, "()", 2, -1); /* A cookie: "()". */
1119 if (gv) sv = GvSV(gv);
1121 if (!gv) goto no_table;
1122 else if (SvTRUE(sv)) amt.fallback=AMGfallYES;
1123 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
1126 for (i = 1; i < NofAMmeth; i++) {
1127 SV *cookie = sv_2mortal(Perl_newSVpvf(aTHX_ "(%s", cp = PL_AMG_names[i]));
1128 DEBUG_o( Perl_deb(aTHX_ "Checking overloading of `%s' in package `%.256s'\n",
1129 cp, HvNAME(stash)) );
1130 /* don't fill the cache while looking up! */
1131 gv = gv_fetchmeth(stash, SvPVX(cookie), SvCUR(cookie), -1);
1133 if(gv && (cv = GvCV(gv))) {
1134 if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil")
1135 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) {
1136 /* GvSV contains the name of the method. */
1139 DEBUG_o( Perl_deb(aTHX_ "Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n",
1140 SvPV(GvSV(gv), n_a), cp, HvNAME(stash)) );
1141 if (!SvPOK(GvSV(gv))
1142 || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)),
1145 /* Can be an import stub (created by `can'). */
1147 Perl_croak(aTHX_ "Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'",
1148 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1151 Perl_croak(aTHX_ "Can't resolve method `%.256s' overloading `%s' in package `%.256s'",
1152 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1155 cv = GvCV(gv = ngv);
1157 DEBUG_o( Perl_deb(aTHX_ "Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n",
1158 cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))),
1159 GvNAME(CvGV(cv))) );
1163 amt.table[i]=(CV*)SvREFCNT_inc(cv);
1166 AMT_AMAGIC_on(&amt);
1167 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMT));
1171 /* Here we have no table: */
1173 AMT_AMAGIC_off(&amt);
1174 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMTS));
1179 Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags)
1184 CV **cvp=NULL, **ocvp=NULL;
1186 int fl=0, off, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
1187 int postpr = 0, force_cpy = 0, assignshift = assign ? 1 : 0;
1189 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
1190 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),'c'))
1191 && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1192 ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table
1194 && ((cv = cvp[off=method+assignshift])
1195 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
1197 (fl = 1, cv = cvp[off=method])))) {
1198 lr = -1; /* Call method for left argument */
1200 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
1203 /* look for substituted methods */
1204 /* In all the covered cases we should be called with assign==0. */
1208 if ((cv = cvp[off=add_ass_amg])
1209 || ((cv = cvp[off = add_amg]) && (force_cpy = 0, postpr = 1))) {
1210 right = &PL_sv_yes; lr = -1; assign = 1;
1215 if ((cv = cvp[off = subtr_ass_amg])
1216 || ((cv = cvp[off = subtr_amg]) && (force_cpy = 0, postpr=1))) {
1217 right = &PL_sv_yes; lr = -1; assign = 1;
1221 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
1224 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1227 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1230 (void)((cv = cvp[off=bool__amg])
1231 || (cv = cvp[off=numer_amg])
1232 || (cv = cvp[off=string_amg]));
1238 * SV* ref causes confusion with the interpreter variable of
1241 SV* tmpRef=SvRV(left);
1242 if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) {
1244 * Just to be extra cautious. Maybe in some
1245 * additional cases sv_setsv is safe, too.
1247 SV* newref = newSVsv(tmpRef);
1248 SvOBJECT_on(newref);
1249 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(tmpRef));
1255 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
1256 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
1257 SV* nullsv=sv_2mortal(newSViv(0));
1259 SV* lessp = amagic_call(left,nullsv,
1260 lt_amg,AMGf_noright);
1261 logic = SvTRUE(lessp);
1263 SV* lessp = amagic_call(left,nullsv,
1264 ncmp_amg,AMGf_noright);
1265 logic = (SvNV(lessp) < 0);
1268 if (off==subtr_amg) {
1279 if (cv = cvp[off=subtr_amg]) {
1281 left = sv_2mortal(newSViv(0));
1285 case iter_amg: /* XXXX Eventually should do to_gv. */
1292 return NULL; /* Delegate operation to standard mechanisms. */
1297 if (!cv) goto not_found;
1298 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1299 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c'))
1300 && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1301 ? (amtp = (AMT*)mg->mg_ptr)->table
1303 && (cv = cvp[off=method])) { /* Method for right
1306 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
1307 && (cvp=ocvp) && (lr = -1))
1308 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1309 && !(flags & AMGf_unary)) {
1310 /* We look for substitution for
1311 * comparison operations and
1313 if (method==concat_amg || method==concat_ass_amg
1314 || method==repeat_amg || method==repeat_ass_amg) {
1315 return NULL; /* Delegate operation to string conversion */
1325 postpr = 1; off=ncmp_amg; break;
1332 postpr = 1; off=scmp_amg; break;
1334 if (off != -1) cv = cvp[off];
1339 not_found: /* No method found, either report or croak */
1340 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1341 notfound = 1; lr = -1;
1342 } else if (cvp && (cv=cvp[nomethod_amg])) {
1343 notfound = 1; lr = 1;
1346 if (off==-1) off=method;
1347 msg = sv_2mortal(Perl_newSVpvf(aTHX_
1348 "Operation `%s': no method found,%sargument %s%s%s%s",
1349 PL_AMG_names[method + assignshift],
1350 (flags & AMGf_unary ? " " : "\n\tleft "),
1352 "in overloaded package ":
1353 "has no overloaded magic",
1355 HvNAME(SvSTASH(SvRV(left))):
1358 ",\n\tright argument in overloaded package ":
1361 : ",\n\tright argument has no overloaded magic"),
1363 HvNAME(SvSTASH(SvRV(right))):
1365 if (amtp && amtp->fallback >= AMGfallYES) {
1366 DEBUG_o( Perl_deb(aTHX_ "%s", SvPVX(msg)) );
1368 Perl_croak(aTHX_ "%_", msg);
1372 force_cpy = force_cpy || assign;
1376 DEBUG_o( Perl_deb(aTHX_
1377 "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n",
1379 method+assignshift==off? "" :
1381 method+assignshift==off? "" :
1382 PL_AMG_names[method+assignshift],
1383 method+assignshift==off? "" : "')",
1384 flags & AMGf_unary? "" :
1385 lr==1 ? " for right argument": " for left argument",
1386 flags & AMGf_unary? " for argument" : "",
1388 fl? ",\n\tassignment variant used": "") );
1390 /* Since we use shallow copy during assignment, we need
1391 * to dublicate the contents, probably calling user-supplied
1392 * version of copy operator
1394 /* We need to copy in following cases:
1395 * a) Assignment form was called.
1396 * assignshift==1, assign==T, method + 1 == off
1397 * b) Increment or decrement, called directly.
1398 * assignshift==0, assign==0, method + 0 == off
1399 * c) Increment or decrement, translated to assignment add/subtr.
1400 * assignshift==0, assign==T,
1402 * d) Increment or decrement, translated to nomethod.
1403 * assignshift==0, assign==0,
1405 * e) Assignment form translated to nomethod.
1406 * assignshift==1, assign==T, method + 1 != off
1409 /* off is method, method+assignshift, or a result of opcode substitution.
1410 * In the latter case assignshift==0, so only notfound case is important.
1412 if (( (method + assignshift == off)
1413 && (assign || (method == inc_amg) || (method == dec_amg)))
1420 bool oldcatch = CATCH_GET;
1423 Zero(&myop, 1, BINOP);
1424 myop.op_last = (OP *) &myop;
1425 myop.op_next = Nullop;
1426 myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED;
1428 PUSHSTACKi(PERLSI_OVERLOAD);
1431 PL_op = (OP *) &myop;
1432 if (PERLDB_SUB && PL_curstash != PL_debstash)
1433 PL_op->op_private |= OPpENTERSUB_DB;
1437 EXTEND(SP, notfound + 5);
1438 PUSHs(lr>0? right: left);
1439 PUSHs(lr>0? left: right);
1440 PUSHs( lr > 0 ? &PL_sv_yes : ( assign ? &PL_sv_undef : &PL_sv_no ));
1442 PUSHs( sv_2mortal(newSVpv((char *)PL_AMG_names[method + assignshift],0)));
1447 if (PL_op = Perl_pp_entersub(aTHX))
1455 CATCH_SET(oldcatch);
1462 ans=SvIV(res)<=0; break;
1465 ans=SvIV(res)<0; break;
1468 ans=SvIV(res)>=0; break;
1471 ans=SvIV(res)>0; break;
1474 ans=SvIV(res)==0; break;
1477 ans=SvIV(res)!=0; break;
1480 SvSetSV(left,res); return left;
1482 ans=!SvTRUE(res); break;
1485 } else if (method==copy_amg) {
1487 Perl_croak(aTHX_ "Copy method did not return a reference");
1489 return SvREFCNT_inc(SvRV(res));