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)
62 tmplen = strlen(name) + 2;
63 if (tmplen < sizeof smallbuf)
66 New(603, tmpbuf, tmplen + 1, char);
69 strcpy(tmpbuf + 2, name);
70 gv = *(GV**)hv_fetch(PL_defstash, tmpbuf, tmplen, TRUE);
72 gv_init(gv, PL_defstash, tmpbuf, tmplen, FALSE);
73 if (tmpbuf != smallbuf)
75 sv_setpv(GvSV(gv), name);
76 if (*name == '/' && (instr(name, "/lib/") || instr(name, ".pm")))
79 hv_magic(GvHVn(gv_AVadd(gv)), gv, 'L');
84 Perl_gv_init(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len, int multi)
88 bool doproto = SvTYPE(gv) > SVt_NULL;
89 char *proto = (doproto && SvPOK(gv)) ? SvPVX(gv) : NULL;
91 sv_upgrade((SV*)gv, SVt_PVGV);
100 Newz(602, gp, 1, GP);
101 GvGP(gv) = gp_ref(gp);
102 GvSV(gv) = NEWSV(72,0);
103 GvLINE(gv) = PL_curcop->cop_line;
104 GvFILEGV(gv) = PL_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);
111 if (multi || doproto) /* doproto means it _was_ mentioned */
113 if (doproto) { /* Replicate part of newSUB here. */
116 /* XXX unsafe for threads if eval_owner isn't held */
117 start_subparse(0,0); /* Create CV in compcv. */
118 GvCV(gv) = PL_compcv;
122 CvGV(GvCV(gv)) = (GV*)SvREFCNT_inc(gv);
123 CvFILEGV(GvCV(gv)) = PL_curcop->cop_filegv;
124 CvSTASH(GvCV(gv)) = PL_curstash;
126 CvOWNER(GvCV(gv)) = 0;
127 if (!CvMUTEXP(GvCV(gv))) {
128 New(666, CvMUTEXP(GvCV(gv)), 1, perl_mutex);
129 MUTEX_INIT(CvMUTEXP(GvCV(gv)));
131 #endif /* USE_THREADS */
133 sv_setpv((SV*)GvCV(gv), proto);
140 S_gv_init_sv(pTHX_ GV *gv, I32 sv_type)
156 Perl_gv_fetchmeth(pTHX_ HV *stash, const char *name, STRLEN len, I32 level)
166 if ((level > 100) || (level < -100))
167 Perl_croak(aTHX_ "Recursive inheritance detected while looking for method '%s' in package '%s'",
168 name, HvNAME(stash));
170 DEBUG_o( Perl_deb(aTHX_ "Looking for method %s in package %s\n",name,HvNAME(stash)) );
172 gvp = (GV**)hv_fetch(stash, name, len, (level >= 0));
177 if (SvTYPE(topgv) != SVt_PVGV)
178 gv_init(topgv, stash, name, len, TRUE);
179 if (cv = GvCV(topgv)) {
180 /* If genuine method or valid cache entry, use it */
181 if (!GvCVGEN(topgv) || GvCVGEN(topgv) == PL_sub_generation)
183 /* Stale cached entry: junk it */
185 GvCV(topgv) = cv = Nullcv;
188 else if (GvCVGEN(topgv) == PL_sub_generation)
189 return 0; /* cache indicates sub doesn't exist */
192 gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE);
193 av = (gvp && (gv = *gvp) && gv != (GV*)&PL_sv_undef) ? GvAV(gv) : Nullav;
195 /* create and re-create @.*::SUPER::ISA on demand */
196 if (!av || !SvMAGIC(av)) {
197 char* packname = HvNAME(stash);
198 STRLEN packlen = strlen(packname);
200 if (packlen >= 7 && strEQ(packname + packlen - 7, "::SUPER")) {
204 basestash = gv_stashpvn(packname, packlen, TRUE);
205 gvp = (GV**)hv_fetch(basestash, "ISA", 3, FALSE);
206 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
207 dTHR; /* just for SvREFCNT_dec */
208 gvp = (GV**)hv_fetch(stash, "ISA", 3, TRUE);
209 if (!gvp || !(gv = *gvp))
210 Perl_croak(aTHX_ "Cannot create %s::ISA", HvNAME(stash));
211 if (SvTYPE(gv) != SVt_PVGV)
212 gv_init(gv, stash, "ISA", 3, TRUE);
213 SvREFCNT_dec(GvAV(gv));
214 GvAV(gv) = (AV*)SvREFCNT_inc(av);
220 SV** svp = AvARRAY(av);
221 /* NOTE: No support for tied ISA */
222 I32 items = AvFILLp(av) + 1;
225 HV* basestash = gv_stashsv(sv, FALSE);
227 dTHR; /* just for ckWARN */
228 if (ckWARN(WARN_MISC))
229 Perl_warner(aTHX_ WARN_MISC, "Can't locate package %s for @%s::ISA",
230 SvPVX(sv), HvNAME(stash));
233 gv = gv_fetchmeth(basestash, name, len,
234 (level >= 0) ? level + 1 : level - 1);
240 /* if at top level, try UNIVERSAL */
242 if (level == 0 || level == -1) {
245 if (lastchance = gv_stashpvn("UNIVERSAL", 9, FALSE)) {
246 if (gv = gv_fetchmeth(lastchance, name, len,
247 (level >= 0) ? level + 1 : level - 1)) {
250 * Cache method in topgv if:
251 * 1. topgv has no synonyms (else inheritance crosses wires)
252 * 2. method isn't a stub (else AUTOLOAD fails spectacularly)
255 GvREFCNT(topgv) == 1 &&
257 (CvROOT(cv) || CvXSUB(cv)))
259 if (cv = GvCV(topgv))
261 GvCV(topgv) = (CV*)SvREFCNT_inc(GvCV(gv));
262 GvCVGEN(topgv) = PL_sub_generation;
266 else if (topgv && GvREFCNT(topgv) == 1) {
267 /* cache the fact that the method is not defined */
268 GvCVGEN(topgv) = PL_sub_generation;
277 Perl_gv_fetchmethod(pTHX_ HV *stash, const char *name)
279 return gv_fetchmethod_autoload(stash, name, TRUE);
283 Perl_gv_fetchmethod_autoload(pTHX_ HV *stash, const char *name, I32 autoload)
286 register const char *nend;
287 const char *nsplit = 0;
290 for (nend = name; *nend; nend++) {
293 else if (*nend == ':' && *(nend + 1) == ':')
297 const char *origname = name;
301 if ((nsplit - origname) == 5 && strnEQ(origname, "SUPER", 5)) {
302 /* ->SUPER::method should really be looked up in original stash */
303 SV *tmpstr = sv_2mortal(Perl_newSVpvf(aTHX_ "%s::SUPER",
304 HvNAME(PL_curcop->cop_stash)));
305 stash = gv_stashpvn(SvPVX(tmpstr), SvCUR(tmpstr), TRUE);
306 DEBUG_o( Perl_deb(aTHX_ "Treating %s as %s::%s\n",
307 origname, HvNAME(stash), name) );
310 stash = gv_stashpvn(origname, nsplit - origname, TRUE);
313 gv = gv_fetchmeth(stash, name, nend - name, 0);
315 if (strEQ(name,"import"))
316 gv = (GV*)&PL_sv_yes;
318 gv = gv_autoload4(stash, name, nend - name, TRUE);
322 if (!CvROOT(cv) && !CvXSUB(cv)) {
330 if (GvCV(stubgv) != cv) /* orphaned import */
333 autogv = gv_autoload4(GvSTASH(stubgv),
334 GvNAME(stubgv), GvNAMELEN(stubgv), TRUE);
344 Perl_gv_autoload4(pTHX_ HV *stash, const char *name, STRLEN len, I32 method)
347 static char autoload[] = "AUTOLOAD";
348 static STRLEN autolen = 8;
355 if (len == autolen && strnEQ(name, autoload, autolen))
357 if (!(gv = gv_fetchmeth(stash, autoload, autolen, FALSE)))
362 * Inheriting AUTOLOAD for non-methods works ... for now.
364 if (ckWARN(WARN_DEPRECATED) && !method &&
365 (GvCVGEN(gv) || GvSTASH(gv) != stash))
366 Perl_warner(aTHX_ WARN_DEPRECATED,
367 "Use of inherited AUTOLOAD for non-method %s::%.*s() is deprecated",
368 HvNAME(stash), (int)len, name);
371 * Given &FOO::AUTOLOAD, set $FOO::AUTOLOAD to desired function name.
372 * The subroutine's original name may not be "AUTOLOAD", so we don't
373 * use that, but for lack of anything better we will use the sub's
374 * original package to look up $AUTOLOAD.
376 varstash = GvSTASH(CvGV(cv));
377 vargv = *(GV**)hv_fetch(varstash, autoload, autolen, TRUE);
379 gv_init(vargv, varstash, autoload, autolen, FALSE);
381 sv_setpv(varsv, HvNAME(stash));
382 sv_catpvn(varsv, "::", 2);
383 sv_catpvn(varsv, name, len);
384 SvTAINTED_off(varsv);
389 Perl_gv_stashpv(pTHX_ const char *name, I32 create)
391 return gv_stashpvn(name, strlen(name), create);
395 Perl_gv_stashpvn(pTHX_ const char *name, U32 namelen, I32 create)
402 if (namelen + 3 < sizeof smallbuf)
405 New(606, tmpbuf, namelen + 3, char);
406 Copy(name,tmpbuf,namelen,char);
407 tmpbuf[namelen++] = ':';
408 tmpbuf[namelen++] = ':';
409 tmpbuf[namelen] = '\0';
410 tmpgv = gv_fetchpv(tmpbuf, create, SVt_PVHV);
411 if (tmpbuf != smallbuf)
416 GvHV(tmpgv) = newHV();
419 HvNAME(stash) = savepv(name);
424 Perl_gv_stashsv(pTHX_ SV *sv, I32 create)
429 return gv_stashpvn(ptr, len, create);
434 Perl_gv_fetchpv(pTHX_ const char *nambeg, I32 add, I32 sv_type)
437 register const char *name = nambeg;
441 register const char *namend;
445 if (*name == '*' && isALPHA(name[1])) /* accidental stringify on a GV? */
448 for (namend = name; *namend; namend++) {
449 if ((*namend == '\'' && namend[1]) ||
450 (*namend == ':' && namend[1] == ':'))
454 if (!stash || !SvREFCNT(stash)) /* symbol table under destruction */
462 if (len + 3 < sizeof smallbuf)
465 New(601, tmpbuf, len+3, char);
466 Copy(name, tmpbuf, len, char);
470 gvp = (GV**)hv_fetch(stash,tmpbuf,len,add);
471 gv = gvp ? *gvp : Nullgv;
472 if (gv && gv != (GV*)&PL_sv_undef) {
473 if (SvTYPE(gv) != SVt_PVGV)
474 gv_init(gv, stash, tmpbuf, len, (add & GV_ADDMULTI));
478 if (tmpbuf != smallbuf)
480 if (!gv || gv == (GV*)&PL_sv_undef)
483 if (!(stash = GvHV(gv)))
484 stash = GvHV(gv) = newHV();
487 HvNAME(stash) = savepvn(nambeg, namend - nambeg);
495 return gv ? gv : (GV*)*hv_fetch(PL_defstash, "main::", 6, TRUE);
502 /* No stash in name, so see how we can default */
506 || (IN_UTF8 && ((*name & 0xc0) == 0xc0) && isIDFIRST_utf8((U8*)name)))
510 if (isUPPER(*name)) {
511 if (*name == 'S' && (
512 strEQ(name, "SIG") ||
513 strEQ(name, "STDIN") ||
514 strEQ(name, "STDOUT") ||
515 strEQ(name, "STDERR")))
517 else if (*name == 'I' && strEQ(name, "INC"))
519 else if (*name == 'E' && strEQ(name, "ENV"))
521 else if (*name == 'A' && (
522 strEQ(name, "ARGV") ||
523 strEQ(name, "ARGVOUT")))
526 else if (*name == '_' && !name[1])
531 else if ((COP*)PL_curcop == &PL_compiling) {
533 if (add && (PL_hints & HINT_STRICT_VARS) &&
534 sv_type != SVt_PVCV &&
535 sv_type != SVt_PVGV &&
536 sv_type != SVt_PVFM &&
537 sv_type != SVt_PVIO &&
538 !(len == 1 && sv_type == SVt_PV && strchr("ab",*name)) )
540 gvp = (GV**)hv_fetch(stash,name,len,0);
542 *gvp == (GV*)&PL_sv_undef ||
543 SvTYPE(*gvp) != SVt_PVGV)
547 else if (sv_type == SVt_PV && !GvIMPORTED_SV(*gvp) ||
548 sv_type == SVt_PVAV && !GvIMPORTED_AV(*gvp) ||
549 sv_type == SVt_PVHV && !GvIMPORTED_HV(*gvp) )
551 Perl_warn(aTHX_ "Variable \"%c%s\" is not imported",
552 sv_type == SVt_PVAV ? '@' :
553 sv_type == SVt_PVHV ? '%' : '$',
556 Perl_warn(aTHX_ "(Did you mean &%s instead?)\n", name);
562 stash = PL_curcop->cop_stash;
568 /* By this point we should have a stash and a name */
572 qerror(Perl_mess(aTHX_
573 "Global symbol \"%s%s\" requires explicit package name",
574 (sv_type == SVt_PV ? "$"
575 : sv_type == SVt_PVAV ? "@"
576 : sv_type == SVt_PVHV ? "%"
582 if (!SvREFCNT(stash)) /* symbol table under destruction */
585 gvp = (GV**)hv_fetch(stash,name,len,add);
586 if (!gvp || *gvp == (GV*)&PL_sv_undef)
589 if (SvTYPE(gv) == SVt_PVGV) {
592 gv_init_sv(gv, sv_type);
595 } else if (add & GV_NOINIT) {
599 /* Adding a new symbol */
601 if (add & GV_ADDWARN && ckWARN_d(WARN_INTERNAL))
602 Perl_warner(aTHX_ WARN_INTERNAL, "Had to create %s unexpectedly", nambeg);
603 gv_init(gv, stash, name, len, add & GV_ADDMULTI);
604 gv_init_sv(gv, sv_type);
605 GvFLAGS(gv) |= add_gvflags;
607 if (isLEXWARN_on && isALPHA(name[0]) && ! ckWARN(WARN_ONCE))
610 /* set up magic where warranted */
613 if (strEQ(name, "ARGV")) {
614 IoFLAGS(GvIOn(gv)) |= IOf_ARGV|IOf_START;
618 if (strnEQ(name, "EXPORT", 6))
622 if (strEQ(name, "ISA")) {
625 sv_magic((SV*)av, (SV*)gv, 'I', Nullch, 0);
626 /* NOTE: No support for tied ISA */
627 if ((add & GV_ADDMULTI) && strEQ(nambeg,"AnyDBM_File::ISA")
628 && AvFILLp(av) == -1)
631 av_push(av, newSVpvn(pname = "NDBM_File",9));
632 gv_stashpvn(pname, 9, TRUE);
633 av_push(av, newSVpvn(pname = "DB_File",7));
634 gv_stashpvn(pname, 7, TRUE);
635 av_push(av, newSVpvn(pname = "GDBM_File",9));
636 gv_stashpvn(pname, 9, TRUE);
637 av_push(av, newSVpvn(pname = "SDBM_File",9));
638 gv_stashpvn(pname, 9, TRUE);
639 av_push(av, newSVpvn(pname = "ODBM_File",9));
640 gv_stashpvn(pname, 9, TRUE);
645 if (strEQ(name, "OVERLOAD")) {
648 hv_magic(hv, gv, 'A');
652 if (strEQ(name, "SIG")) {
656 GvMULTI_on(PL_siggv);
657 hv = GvHVn(PL_siggv);
658 hv_magic(hv, PL_siggv, 'S');
659 for(i=1;PL_sig_name[i];i++) {
661 init=hv_fetch(hv,PL_sig_name[i],strlen(PL_sig_name[i]),1);
663 sv_setsv(*init,&PL_sv_undef);
670 if (strEQ(name, "VERSION"))
678 PL_sawampersand = TRUE;
685 PL_sawampersand = TRUE;
692 PL_sawampersand = TRUE;
698 sv_setpv(GvSV(gv),PL_chopset);
704 #ifdef COMPLEX_STATUS
705 (void)SvUPGRADE(GvSV(gv), SVt_PVLV);
712 if (sv_type > SVt_PV && PL_curcop != &PL_compiling) {
713 HV* stash = gv_stashpvn("Errno",5,FALSE);
714 if (!stash || !(gv_fetchmethod(stash, "TIEHASH"))) {
717 require_pv("Errno.pm");
719 stash = gv_stashpvn("Errno",5,FALSE);
720 if (!stash || !(gv_fetchmethod(stash, "TIEHASH")))
721 Perl_croak(aTHX_ "Can't use %%! because Errno.pm is not available");
730 sv_magic((SV*)av, Nullsv, 'D', Nullch, 0);
735 if (ckWARN(WARN_DEPRECATED) && len == 1 && sv_type == SVt_PV)
736 Perl_warner(aTHX_ WARN_DEPRECATED, "Use of $%s is deprecated", name);
758 case '\011': /* NOT \t in EBCDIC */
769 case '\027': /* $^W & $^Warnings */
770 if (len > 1 && strNE(name, "\027arnings"))
779 sv_magic((SV*)av, (SV*)av, 'D', Nullch, 0);
792 SvREADONLY_on(GvSV(gv));
794 sv_magic(GvSV(gv), (SV*)gv, 0, name, len);
800 sv_setpv(GvSV(gv),"\f");
801 PL_formfeed = GvSV(gv);
806 sv_setpv(GvSV(gv),"\034");
811 (void)SvUPGRADE(sv, SVt_PVNV);
812 sv_setpv(sv, PL_patchlevel);
822 Perl_gv_fullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
824 HV *hv = GvSTASH(gv);
829 sv_setpv(sv, prefix ? prefix : "");
830 sv_catpv(sv,HvNAME(hv));
831 sv_catpvn(sv,"::", 2);
832 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
836 Perl_gv_efullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
841 gv_fullname3(sv, egv, prefix);
844 /* XXX compatibility with versions <= 5.003. */
846 Perl_gv_fullname(pTHX_ SV *sv, GV *gv)
848 gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : "");
851 /* XXX compatibility with versions <= 5.003. */
853 Perl_gv_efullname(pTHX_ SV *sv, GV *gv)
855 gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : "");
865 io = (IO*)NEWSV(0,0);
866 sv_upgrade((SV *)io,SVt_PVIO);
869 iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV);
870 /* unless exists($main::{FileHandle}) and defined(%main::FileHandle::) */
871 if (!(iogv && GvHV(iogv) && HvARRAY(GvHV(iogv))))
872 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
873 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
878 Perl_gv_check(pTHX_ HV *stash)
889 for (i = 0; i <= (I32) HvMAX(stash); i++) {
890 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
891 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
892 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)) && HvNAME(hv))
894 if (hv != PL_defstash && hv != stash)
895 gv_check(hv); /* nested package */
897 else if (isALPHA(*HeKEY(entry))) {
898 gv = (GV*)HeVAL(entry);
899 if (SvTYPE(gv) != SVt_PVGV || GvMULTI(gv))
901 PL_curcop->cop_line = GvLINE(gv);
902 filegv = GvFILEGV(gv);
903 PL_curcop->cop_filegv = filegv;
904 if (filegv && GvMULTI(filegv)) /* Filename began with slash */
906 Perl_warner(aTHX_ WARN_ONCE,
907 "Name \"%s::%s\" used only once: possible typo",
908 HvNAME(stash), GvNAME(gv));
915 Perl_newGVgen(pTHX_ char *pack)
917 return gv_fetchpv(Perl_form(aTHX_ "%s::_GEN_%ld", pack, (long)PL_gensym++),
921 /* hopefully this is only called on local symbol table entries */
924 Perl_gp_ref(pTHX_ GP *gp)
929 /* multi-named GPs cannot be used for method cache */
930 SvREFCNT_dec(gp->gp_cv);
935 /* Adding a new name to a subroutine invalidates method cache */
943 Perl_gp_free(pTHX_ GV *gv)
949 if (!gv || !(gp = GvGP(gv)))
951 if (gp->gp_refcnt == 0) {
952 if (ckWARN_d(WARN_INTERNAL))
953 Perl_warner(aTHX_ WARN_INTERNAL,
954 "Attempt to free unreferenced glob pointers");
958 /* Deleting the name of a subroutine invalidates method cache */
961 if (--gp->gp_refcnt > 0) {
962 if (gp->gp_egv == gv)
967 SvREFCNT_dec(gp->gp_sv);
968 SvREFCNT_dec(gp->gp_av);
969 SvREFCNT_dec(gp->gp_hv);
970 SvREFCNT_dec(gp->gp_io);
971 SvREFCNT_dec(gp->gp_cv);
972 SvREFCNT_dec(gp->gp_form);
978 #if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
982 #ifdef MICROPORT /* Microport 2.4 hack */
987 return GvGP(gv)->gp_av;
989 return GvGP(gv_AVadd(gv))->gp_av;
996 return GvGP(gv)->gp_hv;
998 return GvGP(gv_HVadd(gv))->gp_hv;
1000 #endif /* Microport 2.4 hack */
1002 /* Updates and caches the CV's */
1005 Perl_Gv_AMupdate(pTHX_ HV *stash)
1012 MAGIC* mg=mg_find((SV*)stash,'c');
1013 AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL;
1017 if (mg && amtp->was_ok_am == PL_amagic_generation
1018 && amtp->was_ok_sub == PL_sub_generation)
1019 return AMT_AMAGIC(amtp);
1020 if (amtp && AMT_AMAGIC(amtp)) { /* Have table. */
1022 for (i=1; i<NofAMmeth; i++) {
1023 if (amtp->table[i]) {
1024 SvREFCNT_dec(amtp->table[i]);
1028 sv_unmagic((SV*)stash, 'c');
1030 DEBUG_o( Perl_deb(aTHX_ "Recalcing overload magic in package %s\n",HvNAME(stash)) );
1032 amt.was_ok_am = PL_amagic_generation;
1033 amt.was_ok_sub = PL_sub_generation;
1034 amt.fallback = AMGfallNO;
1037 #ifdef OVERLOAD_VIA_HASH
1038 gvp=(GV**)hv_fetch(stash,"OVERLOAD",8,FALSE); /* A shortcut */
1039 if (gvp && ((gv = *gvp) != (GV*)&PL_sv_undef && (hv = GvHV(gv)))) {
1046 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
1048 if (( cp = (char *)PL_AMG_names[0] ) &&
1049 (svp = (SV**)hv_fetch(hv,cp,strlen(cp),FALSE)) && (sv = *svp)) {
1050 if (SvTRUE(sv)) amt.fallback=AMGfallYES;
1051 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
1053 for (i = 1; i < NofAMmeth; i++) {
1055 cp = (char *)PL_AMG_names[i];
1057 svp = (SV**)hv_fetch(hv, cp, strlen(cp), FALSE);
1058 if (svp && ((sv = *svp) != &PL_sv_undef)) {
1059 switch (SvTYPE(sv)) {
1062 if (!SvOK(sv)) break;
1063 gv = gv_fetchmethod(stash, SvPV(sv, n_a));
1064 if (gv) cv = GvCV(gv);
1068 if (SvTYPE(cv) == SVt_PVCV)
1073 Perl_croak(aTHX_ "Not a subroutine reference in overload table");
1079 if (!(cv = GvCVu((GV*)sv)))
1080 cv = sv_2cv(sv, &stash, &gv, FALSE);
1085 Perl_croak(aTHX_ "Method for operation %s not found in package %.256s during blessing\n",
1098 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
1100 if ( cp = PL_AMG_names[0] ) {
1101 /* Try to find via inheritance. */
1102 gv = gv_fetchmeth(stash, "()", 2, -1); /* A cookie: "()". */
1103 if (gv) sv = GvSV(gv);
1105 if (!gv) goto no_table;
1106 else if (SvTRUE(sv)) amt.fallback=AMGfallYES;
1107 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
1110 for (i = 1; i < NofAMmeth; i++) {
1111 SV *cookie = sv_2mortal(Perl_newSVpvf(aTHX_ "(%s", cp = PL_AMG_names[i]));
1112 DEBUG_o( Perl_deb(aTHX_ "Checking overloading of `%s' in package `%.256s'\n",
1113 cp, HvNAME(stash)) );
1114 /* don't fill the cache while looking up! */
1115 gv = gv_fetchmeth(stash, SvPVX(cookie), SvCUR(cookie), -1);
1117 if(gv && (cv = GvCV(gv))) {
1118 if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil")
1119 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) {
1120 /* GvSV contains the name of the method. */
1123 DEBUG_o( Perl_deb(aTHX_ "Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n",
1124 SvPV(GvSV(gv), n_a), cp, HvNAME(stash)) );
1125 if (!SvPOK(GvSV(gv))
1126 || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)),
1129 /* Can be an import stub (created by `can'). */
1131 Perl_croak(aTHX_ "Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'",
1132 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1135 Perl_croak(aTHX_ "Can't resolve method `%.256s' overloading `%s' in package `%.256s'",
1136 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1139 cv = GvCV(gv = ngv);
1141 DEBUG_o( Perl_deb(aTHX_ "Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n",
1142 cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))),
1143 GvNAME(CvGV(cv))) );
1147 amt.table[i]=(CV*)SvREFCNT_inc(cv);
1150 AMT_AMAGIC_on(&amt);
1151 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMT));
1155 /* Here we have no table: */
1157 AMT_AMAGIC_off(&amt);
1158 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMTS));
1163 Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags)
1168 CV **cvp=NULL, **ocvp=NULL;
1170 int fl=0, off, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
1171 int postpr = 0, force_cpy = 0, assignshift = assign ? 1 : 0;
1173 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
1174 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),'c'))
1175 && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1176 ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table
1178 && ((cv = cvp[off=method+assignshift])
1179 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
1181 (fl = 1, cv = cvp[off=method])))) {
1182 lr = -1; /* Call method for left argument */
1184 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
1187 /* look for substituted methods */
1188 /* In all the covered cases we should be called with assign==0. */
1192 if ((cv = cvp[off=add_ass_amg])
1193 || ((cv = cvp[off = add_amg]) && (force_cpy = 0, postpr = 1))) {
1194 right = &PL_sv_yes; lr = -1; assign = 1;
1199 if ((cv = cvp[off = subtr_ass_amg])
1200 || ((cv = cvp[off = subtr_amg]) && (force_cpy = 0, postpr=1))) {
1201 right = &PL_sv_yes; lr = -1; assign = 1;
1205 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
1208 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1211 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1214 (void)((cv = cvp[off=bool__amg])
1215 || (cv = cvp[off=numer_amg])
1216 || (cv = cvp[off=string_amg]));
1222 * SV* ref causes confusion with the interpreter variable of
1225 SV* tmpRef=SvRV(left);
1226 if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) {
1228 * Just to be extra cautious. Maybe in some
1229 * additional cases sv_setsv is safe, too.
1231 SV* newref = newSVsv(tmpRef);
1232 SvOBJECT_on(newref);
1233 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(tmpRef));
1239 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
1240 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
1241 SV* nullsv=sv_2mortal(newSViv(0));
1243 SV* lessp = amagic_call(left,nullsv,
1244 lt_amg,AMGf_noright);
1245 logic = SvTRUE(lessp);
1247 SV* lessp = amagic_call(left,nullsv,
1248 ncmp_amg,AMGf_noright);
1249 logic = (SvNV(lessp) < 0);
1252 if (off==subtr_amg) {
1263 if (cv = cvp[off=subtr_amg]) {
1265 left = sv_2mortal(newSViv(0));
1269 case iter_amg: /* XXXX Eventually should do to_gv. */
1276 return NULL; /* Delegate operation to standard mechanisms. */
1281 if (!cv) goto not_found;
1282 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1283 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c'))
1284 && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1285 ? (amtp = (AMT*)mg->mg_ptr)->table
1287 && (cv = cvp[off=method])) { /* Method for right
1290 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
1291 && (cvp=ocvp) && (lr = -1))
1292 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1293 && !(flags & AMGf_unary)) {
1294 /* We look for substitution for
1295 * comparison operations and
1297 if (method==concat_amg || method==concat_ass_amg
1298 || method==repeat_amg || method==repeat_ass_amg) {
1299 return NULL; /* Delegate operation to string conversion */
1309 postpr = 1; off=ncmp_amg; break;
1316 postpr = 1; off=scmp_amg; break;
1318 if (off != -1) cv = cvp[off];
1323 not_found: /* No method found, either report or croak */
1324 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1325 notfound = 1; lr = -1;
1326 } else if (cvp && (cv=cvp[nomethod_amg])) {
1327 notfound = 1; lr = 1;
1330 if (off==-1) off=method;
1331 msg = sv_2mortal(Perl_newSVpvf(aTHX_
1332 "Operation `%s': no method found,%sargument %s%s%s%s",
1333 PL_AMG_names[method + assignshift],
1334 (flags & AMGf_unary ? " " : "\n\tleft "),
1336 "in overloaded package ":
1337 "has no overloaded magic",
1339 HvNAME(SvSTASH(SvRV(left))):
1342 ",\n\tright argument in overloaded package ":
1345 : ",\n\tright argument has no overloaded magic"),
1347 HvNAME(SvSTASH(SvRV(right))):
1349 if (amtp && amtp->fallback >= AMGfallYES) {
1350 DEBUG_o( Perl_deb(aTHX_ "%s", SvPVX(msg)) );
1352 Perl_croak(aTHX_ "%_", msg);
1356 force_cpy = force_cpy || assign;
1360 DEBUG_o( Perl_deb(aTHX_
1361 "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n",
1363 method+assignshift==off? "" :
1365 method+assignshift==off? "" :
1366 PL_AMG_names[method+assignshift],
1367 method+assignshift==off? "" : "')",
1368 flags & AMGf_unary? "" :
1369 lr==1 ? " for right argument": " for left argument",
1370 flags & AMGf_unary? " for argument" : "",
1372 fl? ",\n\tassignment variant used": "") );
1374 /* Since we use shallow copy during assignment, we need
1375 * to dublicate the contents, probably calling user-supplied
1376 * version of copy operator
1378 /* We need to copy in following cases:
1379 * a) Assignment form was called.
1380 * assignshift==1, assign==T, method + 1 == off
1381 * b) Increment or decrement, called directly.
1382 * assignshift==0, assign==0, method + 0 == off
1383 * c) Increment or decrement, translated to assignment add/subtr.
1384 * assignshift==0, assign==T,
1386 * d) Increment or decrement, translated to nomethod.
1387 * assignshift==0, assign==0,
1389 * e) Assignment form translated to nomethod.
1390 * assignshift==1, assign==T, method + 1 != off
1393 /* off is method, method+assignshift, or a result of opcode substitution.
1394 * In the latter case assignshift==0, so only notfound case is important.
1396 if (( (method + assignshift == off)
1397 && (assign || (method == inc_amg) || (method == dec_amg)))
1404 bool oldcatch = CATCH_GET;
1407 Zero(&myop, 1, BINOP);
1408 myop.op_last = (OP *) &myop;
1409 myop.op_next = Nullop;
1410 myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED;
1412 PUSHSTACKi(PERLSI_OVERLOAD);
1415 PL_op = (OP *) &myop;
1416 if (PERLDB_SUB && PL_curstash != PL_debstash)
1417 PL_op->op_private |= OPpENTERSUB_DB;
1421 EXTEND(SP, notfound + 5);
1422 PUSHs(lr>0? right: left);
1423 PUSHs(lr>0? left: right);
1424 PUSHs( lr > 0 ? &PL_sv_yes : ( assign ? &PL_sv_undef : &PL_sv_no ));
1426 PUSHs( sv_2mortal(newSVpv((char *)PL_AMG_names[method + assignshift],0)));
1431 if (PL_op = Perl_pp_entersub(aTHX))
1439 CATCH_SET(oldcatch);
1446 ans=SvIV(res)<=0; break;
1449 ans=SvIV(res)<0; break;
1452 ans=SvIV(res)>=0; break;
1455 ans=SvIV(res)>0; break;
1458 ans=SvIV(res)==0; break;
1461 ans=SvIV(res)!=0; break;
1464 SvSetSV(left,res); return left;
1466 ans=!SvTRUE(res); break;
1469 } else if (method==copy_amg) {
1471 Perl_croak(aTHX_ "Copy method did not return a reference");
1473 return SvREFCNT_inc(SvRV(res));